ftrace.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  1. /*
  2. * Code for replacing ftrace calls with jumps.
  3. *
  4. * Copyright (C) 2007-2008 Steven Rostedt <srostedt@redhat.com>
  5. *
  6. * Thanks goes to Ingo Molnar, for suggesting the idea.
  7. * Mathieu Desnoyers, for suggesting postponing the modifications.
  8. * Arjan van de Ven, for keeping me straight, and explaining to me
  9. * the dangers of modifying code on the run.
  10. */
  11. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  12. #include <linux/spinlock.h>
  13. #include <linux/hardirq.h>
  14. #include <linux/uaccess.h>
  15. #include <linux/ftrace.h>
  16. #include <linux/percpu.h>
  17. #include <linux/sched.h>
  18. #include <linux/init.h>
  19. #include <linux/list.h>
  20. #include <linux/module.h>
  21. #include <trace/syscall.h>
  22. #include <asm/cacheflush.h>
  23. #include <asm/ftrace.h>
  24. #include <asm/nops.h>
  25. #include <asm/nmi.h>
  26. #ifdef CONFIG_DYNAMIC_FTRACE
  27. /*
  28. * modifying_code is set to notify NMIs that they need to use
  29. * memory barriers when entering or exiting. But we don't want
  30. * to burden NMIs with unnecessary memory barriers when code
  31. * modification is not being done (which is most of the time).
  32. *
  33. * A mutex is already held when ftrace_arch_code_modify_prepare
  34. * and post_process are called. No locks need to be taken here.
  35. *
  36. * Stop machine will make sure currently running NMIs are done
  37. * and new NMIs will see the updated variable before we need
  38. * to worry about NMIs doing memory barriers.
  39. */
  40. static int modifying_code __read_mostly;
  41. static DEFINE_PER_CPU(int, save_modifying_code);
  42. int ftrace_arch_code_modify_prepare(void)
  43. {
  44. set_kernel_text_rw();
  45. set_all_modules_text_rw();
  46. modifying_code = 1;
  47. return 0;
  48. }
  49. int ftrace_arch_code_modify_post_process(void)
  50. {
  51. modifying_code = 0;
  52. set_all_modules_text_ro();
  53. set_kernel_text_ro();
  54. return 0;
  55. }
  56. union ftrace_code_union {
  57. char code[MCOUNT_INSN_SIZE];
  58. struct {
  59. char e8;
  60. int offset;
  61. } __attribute__((packed));
  62. };
  63. static int ftrace_calc_offset(long ip, long addr)
  64. {
  65. return (int)(addr - ip);
  66. }
  67. static unsigned char *ftrace_call_replace(unsigned long ip, unsigned long addr)
  68. {
  69. static union ftrace_code_union calc;
  70. calc.e8 = 0xe8;
  71. calc.offset = ftrace_calc_offset(ip + MCOUNT_INSN_SIZE, addr);
  72. /*
  73. * No locking needed, this must be called via kstop_machine
  74. * which in essence is like running on a uniprocessor machine.
  75. */
  76. return calc.code;
  77. }
  78. /*
  79. * Modifying code must take extra care. On an SMP machine, if
  80. * the code being modified is also being executed on another CPU
  81. * that CPU will have undefined results and possibly take a GPF.
  82. * We use kstop_machine to stop other CPUS from exectuing code.
  83. * But this does not stop NMIs from happening. We still need
  84. * to protect against that. We separate out the modification of
  85. * the code to take care of this.
  86. *
  87. * Two buffers are added: An IP buffer and a "code" buffer.
  88. *
  89. * 1) Put the instruction pointer into the IP buffer
  90. * and the new code into the "code" buffer.
  91. * 2) Wait for any running NMIs to finish and set a flag that says
  92. * we are modifying code, it is done in an atomic operation.
  93. * 3) Write the code
  94. * 4) clear the flag.
  95. * 5) Wait for any running NMIs to finish.
  96. *
  97. * If an NMI is executed, the first thing it does is to call
  98. * "ftrace_nmi_enter". This will check if the flag is set to write
  99. * and if it is, it will write what is in the IP and "code" buffers.
  100. *
  101. * The trick is, it does not matter if everyone is writing the same
  102. * content to the code location. Also, if a CPU is executing code
  103. * it is OK to write to that code location if the contents being written
  104. * are the same as what exists.
  105. */
  106. #define MOD_CODE_WRITE_FLAG (1 << 31) /* set when NMI should do the write */
  107. static atomic_t nmi_running = ATOMIC_INIT(0);
  108. static int mod_code_status; /* holds return value of text write */
  109. static void *mod_code_ip; /* holds the IP to write to */
  110. static const void *mod_code_newcode; /* holds the text to write to the IP */
  111. static unsigned nmi_wait_count;
  112. static atomic_t nmi_update_count = ATOMIC_INIT(0);
  113. int ftrace_arch_read_dyn_info(char *buf, int size)
  114. {
  115. int r;
  116. r = snprintf(buf, size, "%u %u",
  117. nmi_wait_count,
  118. atomic_read(&nmi_update_count));
  119. return r;
  120. }
  121. static void clear_mod_flag(void)
  122. {
  123. int old = atomic_read(&nmi_running);
  124. for (;;) {
  125. int new = old & ~MOD_CODE_WRITE_FLAG;
  126. if (old == new)
  127. break;
  128. old = atomic_cmpxchg(&nmi_running, old, new);
  129. }
  130. }
  131. static void ftrace_mod_code(void)
  132. {
  133. /*
  134. * Yes, more than one CPU process can be writing to mod_code_status.
  135. * (and the code itself)
  136. * But if one were to fail, then they all should, and if one were
  137. * to succeed, then they all should.
  138. */
  139. mod_code_status = probe_kernel_write(mod_code_ip, mod_code_newcode,
  140. MCOUNT_INSN_SIZE);
  141. /* if we fail, then kill any new writers */
  142. if (mod_code_status)
  143. clear_mod_flag();
  144. }
  145. void ftrace_nmi_enter(void)
  146. {
  147. __this_cpu_write(save_modifying_code, modifying_code);
  148. if (!__this_cpu_read(save_modifying_code))
  149. return;
  150. if (atomic_inc_return(&nmi_running) & MOD_CODE_WRITE_FLAG) {
  151. smp_rmb();
  152. ftrace_mod_code();
  153. atomic_inc(&nmi_update_count);
  154. }
  155. /* Must have previous changes seen before executions */
  156. smp_mb();
  157. }
  158. void ftrace_nmi_exit(void)
  159. {
  160. if (!__this_cpu_read(save_modifying_code))
  161. return;
  162. /* Finish all executions before clearing nmi_running */
  163. smp_mb();
  164. atomic_dec(&nmi_running);
  165. }
  166. static void wait_for_nmi_and_set_mod_flag(void)
  167. {
  168. if (!atomic_cmpxchg(&nmi_running, 0, MOD_CODE_WRITE_FLAG))
  169. return;
  170. do {
  171. cpu_relax();
  172. } while (atomic_cmpxchg(&nmi_running, 0, MOD_CODE_WRITE_FLAG));
  173. nmi_wait_count++;
  174. }
  175. static void wait_for_nmi(void)
  176. {
  177. if (!atomic_read(&nmi_running))
  178. return;
  179. do {
  180. cpu_relax();
  181. } while (atomic_read(&nmi_running));
  182. nmi_wait_count++;
  183. }
  184. static inline int
  185. within(unsigned long addr, unsigned long start, unsigned long end)
  186. {
  187. return addr >= start && addr < end;
  188. }
  189. static int
  190. do_ftrace_mod_code(unsigned long ip, const void *new_code)
  191. {
  192. /*
  193. * On x86_64, kernel text mappings are mapped read-only with
  194. * CONFIG_DEBUG_RODATA. So we use the kernel identity mapping instead
  195. * of the kernel text mapping to modify the kernel text.
  196. *
  197. * For 32bit kernels, these mappings are same and we can use
  198. * kernel identity mapping to modify code.
  199. */
  200. if (within(ip, (unsigned long)_text, (unsigned long)_etext))
  201. ip = (unsigned long)__va(__pa(ip));
  202. mod_code_ip = (void *)ip;
  203. mod_code_newcode = new_code;
  204. /* The buffers need to be visible before we let NMIs write them */
  205. smp_mb();
  206. wait_for_nmi_and_set_mod_flag();
  207. /* Make sure all running NMIs have finished before we write the code */
  208. smp_mb();
  209. ftrace_mod_code();
  210. /* Make sure the write happens before clearing the bit */
  211. smp_mb();
  212. clear_mod_flag();
  213. wait_for_nmi();
  214. return mod_code_status;
  215. }
  216. static const unsigned char *ftrace_nop_replace(void)
  217. {
  218. return ideal_nops[NOP_ATOMIC5];
  219. }
  220. static int
  221. ftrace_modify_code(unsigned long ip, unsigned const char *old_code,
  222. unsigned const char *new_code)
  223. {
  224. unsigned char replaced[MCOUNT_INSN_SIZE];
  225. /*
  226. * Note: Due to modules and __init, code can
  227. * disappear and change, we need to protect against faulting
  228. * as well as code changing. We do this by using the
  229. * probe_kernel_* functions.
  230. *
  231. * No real locking needed, this code is run through
  232. * kstop_machine, or before SMP starts.
  233. */
  234. /* read the text we want to modify */
  235. if (probe_kernel_read(replaced, (void *)ip, MCOUNT_INSN_SIZE))
  236. return -EFAULT;
  237. /* Make sure it is what we expect it to be */
  238. if (memcmp(replaced, old_code, MCOUNT_INSN_SIZE) != 0)
  239. return -EINVAL;
  240. /* replace the text with the new text */
  241. if (do_ftrace_mod_code(ip, new_code))
  242. return -EPERM;
  243. sync_core();
  244. return 0;
  245. }
  246. int ftrace_make_nop(struct module *mod,
  247. struct dyn_ftrace *rec, unsigned long addr)
  248. {
  249. unsigned const char *new, *old;
  250. unsigned long ip = rec->ip;
  251. old = ftrace_call_replace(ip, addr);
  252. new = ftrace_nop_replace();
  253. return ftrace_modify_code(rec->ip, old, new);
  254. }
  255. int ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr)
  256. {
  257. unsigned const char *new, *old;
  258. unsigned long ip = rec->ip;
  259. old = ftrace_nop_replace();
  260. new = ftrace_call_replace(ip, addr);
  261. return ftrace_modify_code(rec->ip, old, new);
  262. }
  263. int ftrace_update_ftrace_func(ftrace_func_t func)
  264. {
  265. unsigned long ip = (unsigned long)(&ftrace_call);
  266. unsigned char old[MCOUNT_INSN_SIZE], *new;
  267. int ret;
  268. memcpy(old, &ftrace_call, MCOUNT_INSN_SIZE);
  269. new = ftrace_call_replace(ip, (unsigned long)func);
  270. ret = ftrace_modify_code(ip, old, new);
  271. return ret;
  272. }
  273. int __init ftrace_dyn_arch_init(void *data)
  274. {
  275. /* The return code is retured via data */
  276. *(unsigned long *)data = 0;
  277. return 0;
  278. }
  279. #endif
  280. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  281. #ifdef CONFIG_DYNAMIC_FTRACE
  282. extern void ftrace_graph_call(void);
  283. static int ftrace_mod_jmp(unsigned long ip,
  284. int old_offset, int new_offset)
  285. {
  286. unsigned char code[MCOUNT_INSN_SIZE];
  287. if (probe_kernel_read(code, (void *)ip, MCOUNT_INSN_SIZE))
  288. return -EFAULT;
  289. if (code[0] != 0xe9 || old_offset != *(int *)(&code[1]))
  290. return -EINVAL;
  291. *(int *)(&code[1]) = new_offset;
  292. if (do_ftrace_mod_code(ip, &code))
  293. return -EPERM;
  294. return 0;
  295. }
  296. int ftrace_enable_ftrace_graph_caller(void)
  297. {
  298. unsigned long ip = (unsigned long)(&ftrace_graph_call);
  299. int old_offset, new_offset;
  300. old_offset = (unsigned long)(&ftrace_stub) - (ip + MCOUNT_INSN_SIZE);
  301. new_offset = (unsigned long)(&ftrace_graph_caller) - (ip + MCOUNT_INSN_SIZE);
  302. return ftrace_mod_jmp(ip, old_offset, new_offset);
  303. }
  304. int ftrace_disable_ftrace_graph_caller(void)
  305. {
  306. unsigned long ip = (unsigned long)(&ftrace_graph_call);
  307. int old_offset, new_offset;
  308. old_offset = (unsigned long)(&ftrace_graph_caller) - (ip + MCOUNT_INSN_SIZE);
  309. new_offset = (unsigned long)(&ftrace_stub) - (ip + MCOUNT_INSN_SIZE);
  310. return ftrace_mod_jmp(ip, old_offset, new_offset);
  311. }
  312. #endif /* !CONFIG_DYNAMIC_FTRACE */
  313. /*
  314. * Hook the return address and push it in the stack of return addrs
  315. * in current thread info.
  316. */
  317. void prepare_ftrace_return(unsigned long *parent, unsigned long self_addr,
  318. unsigned long frame_pointer)
  319. {
  320. unsigned long old;
  321. int faulted;
  322. struct ftrace_graph_ent trace;
  323. unsigned long return_hooker = (unsigned long)
  324. &return_to_handler;
  325. if (unlikely(atomic_read(&current->tracing_graph_pause)))
  326. return;
  327. /*
  328. * Protect against fault, even if it shouldn't
  329. * happen. This tool is too much intrusive to
  330. * ignore such a protection.
  331. */
  332. asm volatile(
  333. "1: " _ASM_MOV " (%[parent]), %[old]\n"
  334. "2: " _ASM_MOV " %[return_hooker], (%[parent])\n"
  335. " movl $0, %[faulted]\n"
  336. "3:\n"
  337. ".section .fixup, \"ax\"\n"
  338. "4: movl $1, %[faulted]\n"
  339. " jmp 3b\n"
  340. ".previous\n"
  341. _ASM_EXTABLE(1b, 4b)
  342. _ASM_EXTABLE(2b, 4b)
  343. : [old] "=&r" (old), [faulted] "=r" (faulted)
  344. : [parent] "r" (parent), [return_hooker] "r" (return_hooker)
  345. : "memory"
  346. );
  347. if (unlikely(faulted)) {
  348. ftrace_graph_stop();
  349. WARN_ON(1);
  350. return;
  351. }
  352. trace.func = self_addr;
  353. trace.depth = current->curr_ret_stack + 1;
  354. /* Only trace if the calling function expects to */
  355. if (!ftrace_graph_entry(&trace)) {
  356. *parent = old;
  357. return;
  358. }
  359. if (ftrace_push_return_trace(old, self_addr, &trace.depth,
  360. frame_pointer) == -EBUSY) {
  361. *parent = old;
  362. return;
  363. }
  364. }
  365. #endif /* CONFIG_FUNCTION_GRAPH_TRACER */