patch.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2020 SiFive
  4. */
  5. #include <linux/spinlock.h>
  6. #include <linux/mm.h>
  7. #include <linux/memory.h>
  8. #include <linux/string.h>
  9. #include <linux/uaccess.h>
  10. #include <linux/stop_machine.h>
  11. #include <asm/kprobes.h>
  12. #include <asm/cacheflush.h>
  13. #include <asm/fixmap.h>
  14. #include <asm/ftrace.h>
  15. #include <asm/patch.h>
  16. #include <asm/sections.h>
  17. struct patch_insn {
  18. void *addr;
  19. u32 *insns;
  20. size_t len;
  21. atomic_t cpu_count;
  22. };
  23. int riscv_patch_in_stop_machine = false;
  24. #ifdef CONFIG_MMU
  25. static inline bool is_kernel_exittext(uintptr_t addr)
  26. {
  27. return system_state < SYSTEM_RUNNING &&
  28. addr >= (uintptr_t)__exittext_begin &&
  29. addr < (uintptr_t)__exittext_end;
  30. }
  31. /*
  32. * The fix_to_virt(, idx) needs a const value (not a dynamic variable of
  33. * reg-a0) or BUILD_BUG_ON failed with "idx >= __end_of_fixed_addresses".
  34. * So use '__always_inline' and 'const unsigned int fixmap' here.
  35. */
  36. static __always_inline void *patch_map(void *addr, const unsigned int fixmap)
  37. {
  38. uintptr_t uintaddr = (uintptr_t) addr;
  39. struct page *page;
  40. if (core_kernel_text(uintaddr) || is_kernel_exittext(uintaddr))
  41. page = phys_to_page(__pa_symbol(addr));
  42. else if (IS_ENABLED(CONFIG_STRICT_MODULE_RWX))
  43. page = vmalloc_to_page(addr);
  44. else
  45. return addr;
  46. BUG_ON(!page);
  47. return (void *)set_fixmap_offset(fixmap, page_to_phys(page) +
  48. offset_in_page(addr));
  49. }
  50. static void patch_unmap(int fixmap)
  51. {
  52. clear_fixmap(fixmap);
  53. }
  54. NOKPROBE_SYMBOL(patch_unmap);
  55. static int __patch_insn_set(void *addr, u8 c, size_t len)
  56. {
  57. bool across_pages = (offset_in_page(addr) + len) > PAGE_SIZE;
  58. void *waddr = addr;
  59. /*
  60. * Only two pages can be mapped at a time for writing.
  61. */
  62. if (len + offset_in_page(addr) > 2 * PAGE_SIZE)
  63. return -EINVAL;
  64. /*
  65. * Before reaching here, it was expected to lock the text_mutex
  66. * already, so we don't need to give another lock here and could
  67. * ensure that it was safe between each cores.
  68. */
  69. lockdep_assert_held(&text_mutex);
  70. preempt_disable();
  71. if (across_pages)
  72. patch_map(addr + PAGE_SIZE, FIX_TEXT_POKE1);
  73. waddr = patch_map(addr, FIX_TEXT_POKE0);
  74. memset(waddr, c, len);
  75. /*
  76. * We could have just patched a function that is about to be
  77. * called so make sure we don't execute partially patched
  78. * instructions by flushing the icache as soon as possible.
  79. */
  80. local_flush_icache_range((unsigned long)waddr,
  81. (unsigned long)waddr + len);
  82. patch_unmap(FIX_TEXT_POKE0);
  83. if (across_pages)
  84. patch_unmap(FIX_TEXT_POKE1);
  85. preempt_enable();
  86. return 0;
  87. }
  88. NOKPROBE_SYMBOL(__patch_insn_set);
  89. static int __patch_insn_write(void *addr, const void *insn, size_t len)
  90. {
  91. bool across_pages = (offset_in_page(addr) + len) > PAGE_SIZE;
  92. void *waddr = addr;
  93. int ret;
  94. /*
  95. * Only two pages can be mapped at a time for writing.
  96. */
  97. if (len + offset_in_page(addr) > 2 * PAGE_SIZE)
  98. return -EINVAL;
  99. /*
  100. * Before reaching here, it was expected to lock the text_mutex
  101. * already, so we don't need to give another lock here and could
  102. * ensure that it was safe between each cores.
  103. *
  104. * We're currently using stop_machine() for ftrace & kprobes, and while
  105. * that ensures text_mutex is held before installing the mappings it
  106. * does not ensure text_mutex is held by the calling thread. That's
  107. * safe but triggers a lockdep failure, so just elide it for that
  108. * specific case.
  109. */
  110. if (!riscv_patch_in_stop_machine)
  111. lockdep_assert_held(&text_mutex);
  112. preempt_disable();
  113. if (across_pages)
  114. patch_map(addr + PAGE_SIZE, FIX_TEXT_POKE1);
  115. waddr = patch_map(addr, FIX_TEXT_POKE0);
  116. ret = copy_to_kernel_nofault(waddr, insn, len);
  117. /*
  118. * We could have just patched a function that is about to be
  119. * called so make sure we don't execute partially patched
  120. * instructions by flushing the icache as soon as possible.
  121. */
  122. local_flush_icache_range((unsigned long)waddr,
  123. (unsigned long)waddr + len);
  124. patch_unmap(FIX_TEXT_POKE0);
  125. if (across_pages)
  126. patch_unmap(FIX_TEXT_POKE1);
  127. preempt_enable();
  128. return ret;
  129. }
  130. NOKPROBE_SYMBOL(__patch_insn_write);
  131. #else
  132. static int __patch_insn_set(void *addr, u8 c, size_t len)
  133. {
  134. memset(addr, c, len);
  135. return 0;
  136. }
  137. NOKPROBE_SYMBOL(__patch_insn_set);
  138. static int __patch_insn_write(void *addr, const void *insn, size_t len)
  139. {
  140. return copy_to_kernel_nofault(addr, insn, len);
  141. }
  142. NOKPROBE_SYMBOL(__patch_insn_write);
  143. #endif /* CONFIG_MMU */
  144. static int patch_insn_set(void *addr, u8 c, size_t len)
  145. {
  146. size_t size;
  147. int ret;
  148. /*
  149. * __patch_insn_set() can only work on 2 pages at a time so call it in a
  150. * loop with len <= 2 * PAGE_SIZE.
  151. */
  152. while (len) {
  153. size = min(len, PAGE_SIZE * 2 - offset_in_page(addr));
  154. ret = __patch_insn_set(addr, c, size);
  155. if (ret)
  156. return ret;
  157. addr += size;
  158. len -= size;
  159. }
  160. return 0;
  161. }
  162. NOKPROBE_SYMBOL(patch_insn_set);
  163. int patch_text_set_nosync(void *addr, u8 c, size_t len)
  164. {
  165. int ret;
  166. ret = patch_insn_set(addr, c, len);
  167. if (!ret)
  168. flush_icache_range((uintptr_t)addr, (uintptr_t)addr + len);
  169. return ret;
  170. }
  171. NOKPROBE_SYMBOL(patch_text_set_nosync);
  172. int patch_insn_write(void *addr, const void *insn, size_t len)
  173. {
  174. size_t size;
  175. int ret;
  176. /*
  177. * Copy the instructions to the destination address, two pages at a time
  178. * because __patch_insn_write() can only handle len <= 2 * PAGE_SIZE.
  179. */
  180. while (len) {
  181. size = min(len, PAGE_SIZE * 2 - offset_in_page(addr));
  182. ret = __patch_insn_write(addr, insn, size);
  183. if (ret)
  184. return ret;
  185. addr += size;
  186. insn += size;
  187. len -= size;
  188. }
  189. return 0;
  190. }
  191. NOKPROBE_SYMBOL(patch_insn_write);
  192. int patch_text_nosync(void *addr, const void *insns, size_t len)
  193. {
  194. int ret;
  195. ret = patch_insn_write(addr, insns, len);
  196. if (!ret)
  197. flush_icache_range((uintptr_t)addr, (uintptr_t)addr + len);
  198. return ret;
  199. }
  200. NOKPROBE_SYMBOL(patch_text_nosync);
  201. static int patch_text_cb(void *data)
  202. {
  203. struct patch_insn *patch = data;
  204. int ret = 0;
  205. if (atomic_inc_return(&patch->cpu_count) == num_online_cpus()) {
  206. ret = patch_insn_write(patch->addr, patch->insns, patch->len);
  207. /*
  208. * Make sure the patching store is effective *before* we
  209. * increment the counter which releases all waiting CPUs
  210. * by using the release variant of atomic increment. The
  211. * release pairs with the call to local_flush_icache_all()
  212. * on the waiting CPU.
  213. */
  214. atomic_inc_return_release(&patch->cpu_count);
  215. } else {
  216. while (atomic_read(&patch->cpu_count) <= num_online_cpus())
  217. cpu_relax();
  218. local_flush_icache_all();
  219. }
  220. return ret;
  221. }
  222. NOKPROBE_SYMBOL(patch_text_cb);
  223. int patch_text(void *addr, u32 *insns, size_t len)
  224. {
  225. int ret;
  226. struct patch_insn patch = {
  227. .addr = addr,
  228. .insns = insns,
  229. .len = len,
  230. .cpu_count = ATOMIC_INIT(0),
  231. };
  232. /*
  233. * kprobes takes text_mutex, before calling patch_text(), but as we call
  234. * calls stop_machine(), the lockdep assertion in patch_insn_write()
  235. * gets confused by the context in which the lock is taken.
  236. * Instead, ensure the lock is held before calling stop_machine(), and
  237. * set riscv_patch_in_stop_machine to skip the check in
  238. * patch_insn_write().
  239. */
  240. lockdep_assert_held(&text_mutex);
  241. riscv_patch_in_stop_machine = true;
  242. ret = stop_machine_cpuslocked(patch_text_cb, &patch, cpu_online_mask);
  243. riscv_patch_in_stop_machine = false;
  244. return ret;
  245. }
  246. NOKPROBE_SYMBOL(patch_text);