ftrace.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  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. #include <linux/spinlock.h>
  12. #include <linux/hardirq.h>
  13. #include <linux/uaccess.h>
  14. #include <linux/ftrace.h>
  15. #include <linux/percpu.h>
  16. #include <linux/sched.h>
  17. #include <linux/init.h>
  18. #include <linux/list.h>
  19. #include <asm/ftrace.h>
  20. #include <linux/ftrace.h>
  21. #include <asm/nops.h>
  22. #include <asm/nmi.h>
  23. #ifdef CONFIG_DYNAMIC_FTRACE
  24. union ftrace_code_union {
  25. char code[MCOUNT_INSN_SIZE];
  26. struct {
  27. char e8;
  28. int offset;
  29. } __attribute__((packed));
  30. };
  31. static int ftrace_calc_offset(long ip, long addr)
  32. {
  33. return (int)(addr - ip);
  34. }
  35. static unsigned char *ftrace_call_replace(unsigned long ip, unsigned long addr)
  36. {
  37. static union ftrace_code_union calc;
  38. calc.e8 = 0xe8;
  39. calc.offset = ftrace_calc_offset(ip + MCOUNT_INSN_SIZE, addr);
  40. /*
  41. * No locking needed, this must be called via kstop_machine
  42. * which in essence is like running on a uniprocessor machine.
  43. */
  44. return calc.code;
  45. }
  46. /*
  47. * Modifying code must take extra care. On an SMP machine, if
  48. * the code being modified is also being executed on another CPU
  49. * that CPU will have undefined results and possibly take a GPF.
  50. * We use kstop_machine to stop other CPUS from exectuing code.
  51. * But this does not stop NMIs from happening. We still need
  52. * to protect against that. We separate out the modification of
  53. * the code to take care of this.
  54. *
  55. * Two buffers are added: An IP buffer and a "code" buffer.
  56. *
  57. * 1) Put the instruction pointer into the IP buffer
  58. * and the new code into the "code" buffer.
  59. * 2) Set a flag that says we are modifying code
  60. * 3) Wait for any running NMIs to finish.
  61. * 4) Write the code
  62. * 5) clear the flag.
  63. * 6) Wait for any running NMIs to finish.
  64. *
  65. * If an NMI is executed, the first thing it does is to call
  66. * "ftrace_nmi_enter". This will check if the flag is set to write
  67. * and if it is, it will write what is in the IP and "code" buffers.
  68. *
  69. * The trick is, it does not matter if everyone is writing the same
  70. * content to the code location. Also, if a CPU is executing code
  71. * it is OK to write to that code location if the contents being written
  72. * are the same as what exists.
  73. */
  74. static atomic_t nmi_running = ATOMIC_INIT(0);
  75. static int mod_code_status; /* holds return value of text write */
  76. static int mod_code_write; /* set when NMI should do the write */
  77. static void *mod_code_ip; /* holds the IP to write to */
  78. static void *mod_code_newcode; /* holds the text to write to the IP */
  79. static unsigned nmi_wait_count;
  80. static atomic_t nmi_update_count = ATOMIC_INIT(0);
  81. int ftrace_arch_read_dyn_info(char *buf, int size)
  82. {
  83. int r;
  84. r = snprintf(buf, size, "%u %u",
  85. nmi_wait_count,
  86. atomic_read(&nmi_update_count));
  87. return r;
  88. }
  89. static void ftrace_mod_code(void)
  90. {
  91. /*
  92. * Yes, more than one CPU process can be writing to mod_code_status.
  93. * (and the code itself)
  94. * But if one were to fail, then they all should, and if one were
  95. * to succeed, then they all should.
  96. */
  97. mod_code_status = probe_kernel_write(mod_code_ip, mod_code_newcode,
  98. MCOUNT_INSN_SIZE);
  99. }
  100. void ftrace_nmi_enter(void)
  101. {
  102. atomic_inc(&nmi_running);
  103. /* Must have nmi_running seen before reading write flag */
  104. smp_mb();
  105. if (mod_code_write) {
  106. ftrace_mod_code();
  107. atomic_inc(&nmi_update_count);
  108. }
  109. }
  110. void ftrace_nmi_exit(void)
  111. {
  112. /* Finish all executions before clearing nmi_running */
  113. smp_wmb();
  114. atomic_dec(&nmi_running);
  115. }
  116. static void wait_for_nmi(void)
  117. {
  118. if (!atomic_read(&nmi_running))
  119. return;
  120. do {
  121. cpu_relax();
  122. } while (atomic_read(&nmi_running));
  123. nmi_wait_count++;
  124. }
  125. static int
  126. do_ftrace_mod_code(unsigned long ip, void *new_code)
  127. {
  128. mod_code_ip = (void *)ip;
  129. mod_code_newcode = new_code;
  130. /* The buffers need to be visible before we let NMIs write them */
  131. smp_wmb();
  132. mod_code_write = 1;
  133. /* Make sure write bit is visible before we wait on NMIs */
  134. smp_mb();
  135. wait_for_nmi();
  136. /* Make sure all running NMIs have finished before we write the code */
  137. smp_mb();
  138. ftrace_mod_code();
  139. /* Make sure the write happens before clearing the bit */
  140. smp_wmb();
  141. mod_code_write = 0;
  142. /* make sure NMIs see the cleared bit */
  143. smp_mb();
  144. wait_for_nmi();
  145. return mod_code_status;
  146. }
  147. static unsigned char ftrace_nop[MCOUNT_INSN_SIZE];
  148. static unsigned char *ftrace_nop_replace(void)
  149. {
  150. return ftrace_nop;
  151. }
  152. static int
  153. ftrace_modify_code(unsigned long ip, unsigned char *old_code,
  154. unsigned char *new_code)
  155. {
  156. unsigned char replaced[MCOUNT_INSN_SIZE];
  157. /*
  158. * Note: Due to modules and __init, code can
  159. * disappear and change, we need to protect against faulting
  160. * as well as code changing. We do this by using the
  161. * probe_kernel_* functions.
  162. *
  163. * No real locking needed, this code is run through
  164. * kstop_machine, or before SMP starts.
  165. */
  166. /* read the text we want to modify */
  167. if (probe_kernel_read(replaced, (void *)ip, MCOUNT_INSN_SIZE))
  168. return -EFAULT;
  169. /* Make sure it is what we expect it to be */
  170. if (memcmp(replaced, old_code, MCOUNT_INSN_SIZE) != 0)
  171. return -EINVAL;
  172. /* replace the text with the new text */
  173. if (do_ftrace_mod_code(ip, new_code))
  174. return -EPERM;
  175. sync_core();
  176. return 0;
  177. }
  178. int ftrace_make_nop(struct module *mod,
  179. struct dyn_ftrace *rec, unsigned long addr)
  180. {
  181. unsigned char *new, *old;
  182. unsigned long ip = rec->ip;
  183. old = ftrace_call_replace(ip, addr);
  184. new = ftrace_nop_replace();
  185. return ftrace_modify_code(rec->ip, old, new);
  186. }
  187. int ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr)
  188. {
  189. unsigned char *new, *old;
  190. unsigned long ip = rec->ip;
  191. old = ftrace_nop_replace();
  192. new = ftrace_call_replace(ip, addr);
  193. return ftrace_modify_code(rec->ip, old, new);
  194. }
  195. int ftrace_update_ftrace_func(ftrace_func_t func)
  196. {
  197. unsigned long ip = (unsigned long)(&ftrace_call);
  198. unsigned char old[MCOUNT_INSN_SIZE], *new;
  199. int ret;
  200. memcpy(old, &ftrace_call, MCOUNT_INSN_SIZE);
  201. new = ftrace_call_replace(ip, (unsigned long)func);
  202. ret = ftrace_modify_code(ip, old, new);
  203. return ret;
  204. }
  205. int __init ftrace_dyn_arch_init(void *data)
  206. {
  207. extern const unsigned char ftrace_test_p6nop[];
  208. extern const unsigned char ftrace_test_nop5[];
  209. extern const unsigned char ftrace_test_jmp[];
  210. int faulted = 0;
  211. /*
  212. * There is no good nop for all x86 archs.
  213. * We will default to using the P6_NOP5, but first we
  214. * will test to make sure that the nop will actually
  215. * work on this CPU. If it faults, we will then
  216. * go to a lesser efficient 5 byte nop. If that fails
  217. * we then just use a jmp as our nop. This isn't the most
  218. * efficient nop, but we can not use a multi part nop
  219. * since we would then risk being preempted in the middle
  220. * of that nop, and if we enabled tracing then, it might
  221. * cause a system crash.
  222. *
  223. * TODO: check the cpuid to determine the best nop.
  224. */
  225. asm volatile (
  226. "ftrace_test_jmp:"
  227. "jmp ftrace_test_p6nop\n"
  228. "nop\n"
  229. "nop\n"
  230. "nop\n" /* 2 byte jmp + 3 bytes */
  231. "ftrace_test_p6nop:"
  232. P6_NOP5
  233. "jmp 1f\n"
  234. "ftrace_test_nop5:"
  235. ".byte 0x66,0x66,0x66,0x66,0x90\n"
  236. "1:"
  237. ".section .fixup, \"ax\"\n"
  238. "2: movl $1, %0\n"
  239. " jmp ftrace_test_nop5\n"
  240. "3: movl $2, %0\n"
  241. " jmp 1b\n"
  242. ".previous\n"
  243. _ASM_EXTABLE(ftrace_test_p6nop, 2b)
  244. _ASM_EXTABLE(ftrace_test_nop5, 3b)
  245. : "=r"(faulted) : "0" (faulted));
  246. switch (faulted) {
  247. case 0:
  248. pr_info("ftrace: converting mcount calls to 0f 1f 44 00 00\n");
  249. memcpy(ftrace_nop, ftrace_test_p6nop, MCOUNT_INSN_SIZE);
  250. break;
  251. case 1:
  252. pr_info("ftrace: converting mcount calls to 66 66 66 66 90\n");
  253. memcpy(ftrace_nop, ftrace_test_nop5, MCOUNT_INSN_SIZE);
  254. break;
  255. case 2:
  256. pr_info("ftrace: converting mcount calls to jmp . + 5\n");
  257. memcpy(ftrace_nop, ftrace_test_jmp, MCOUNT_INSN_SIZE);
  258. break;
  259. }
  260. /* The return code is retured via data */
  261. *(unsigned long *)data = 0;
  262. return 0;
  263. }
  264. #endif
  265. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  266. #ifdef CONFIG_DYNAMIC_FTRACE
  267. extern void ftrace_graph_call(void);
  268. static int ftrace_mod_jmp(unsigned long ip,
  269. int old_offset, int new_offset)
  270. {
  271. unsigned char code[MCOUNT_INSN_SIZE];
  272. if (probe_kernel_read(code, (void *)ip, MCOUNT_INSN_SIZE))
  273. return -EFAULT;
  274. if (code[0] != 0xe9 || old_offset != *(int *)(&code[1]))
  275. return -EINVAL;
  276. *(int *)(&code[1]) = new_offset;
  277. if (do_ftrace_mod_code(ip, &code))
  278. return -EPERM;
  279. return 0;
  280. }
  281. int ftrace_enable_ftrace_graph_caller(void)
  282. {
  283. unsigned long ip = (unsigned long)(&ftrace_graph_call);
  284. int old_offset, new_offset;
  285. old_offset = (unsigned long)(&ftrace_stub) - (ip + MCOUNT_INSN_SIZE);
  286. new_offset = (unsigned long)(&ftrace_graph_caller) - (ip + MCOUNT_INSN_SIZE);
  287. return ftrace_mod_jmp(ip, old_offset, new_offset);
  288. }
  289. int ftrace_disable_ftrace_graph_caller(void)
  290. {
  291. unsigned long ip = (unsigned long)(&ftrace_graph_call);
  292. int old_offset, new_offset;
  293. old_offset = (unsigned long)(&ftrace_graph_caller) - (ip + MCOUNT_INSN_SIZE);
  294. new_offset = (unsigned long)(&ftrace_stub) - (ip + MCOUNT_INSN_SIZE);
  295. return ftrace_mod_jmp(ip, old_offset, new_offset);
  296. }
  297. #endif /* !CONFIG_DYNAMIC_FTRACE */
  298. /* Add a function return address to the trace stack on thread info.*/
  299. static int push_return_trace(unsigned long ret, unsigned long long time,
  300. unsigned long func, int *depth)
  301. {
  302. int index;
  303. if (!current->ret_stack)
  304. return -EBUSY;
  305. /* The return trace stack is full */
  306. if (current->curr_ret_stack == FTRACE_RETFUNC_DEPTH - 1) {
  307. atomic_inc(&current->trace_overrun);
  308. return -EBUSY;
  309. }
  310. index = ++current->curr_ret_stack;
  311. barrier();
  312. current->ret_stack[index].ret = ret;
  313. current->ret_stack[index].func = func;
  314. current->ret_stack[index].calltime = time;
  315. *depth = index;
  316. return 0;
  317. }
  318. /* Retrieve a function return address to the trace stack on thread info.*/
  319. static void pop_return_trace(struct ftrace_graph_ret *trace, unsigned long *ret)
  320. {
  321. int index;
  322. index = current->curr_ret_stack;
  323. if (unlikely(index < 0)) {
  324. ftrace_graph_stop();
  325. WARN_ON(1);
  326. /* Might as well panic, otherwise we have no where to go */
  327. *ret = (unsigned long)panic;
  328. return;
  329. }
  330. *ret = current->ret_stack[index].ret;
  331. trace->func = current->ret_stack[index].func;
  332. trace->calltime = current->ret_stack[index].calltime;
  333. trace->overrun = atomic_read(&current->trace_overrun);
  334. trace->depth = index;
  335. barrier();
  336. current->curr_ret_stack--;
  337. }
  338. /*
  339. * Send the trace to the ring-buffer.
  340. * @return the original return address.
  341. */
  342. unsigned long ftrace_return_to_handler(void)
  343. {
  344. struct ftrace_graph_ret trace;
  345. unsigned long ret;
  346. pop_return_trace(&trace, &ret);
  347. trace.rettime = cpu_clock(raw_smp_processor_id());
  348. ftrace_graph_return(&trace);
  349. if (unlikely(!ret)) {
  350. ftrace_graph_stop();
  351. WARN_ON(1);
  352. /* Might as well panic. What else to do? */
  353. ret = (unsigned long)panic;
  354. }
  355. return ret;
  356. }
  357. /*
  358. * Hook the return address and push it in the stack of return addrs
  359. * in current thread info.
  360. */
  361. void prepare_ftrace_return(unsigned long *parent, unsigned long self_addr)
  362. {
  363. unsigned long old;
  364. unsigned long long calltime;
  365. int faulted;
  366. struct ftrace_graph_ent trace;
  367. unsigned long return_hooker = (unsigned long)
  368. &return_to_handler;
  369. /* Nmi's are currently unsupported */
  370. if (unlikely(in_nmi()))
  371. return;
  372. if (unlikely(atomic_read(&current->tracing_graph_pause)))
  373. return;
  374. /*
  375. * Protect against fault, even if it shouldn't
  376. * happen. This tool is too much intrusive to
  377. * ignore such a protection.
  378. */
  379. asm volatile(
  380. "1: " _ASM_MOV " (%[parent_old]), %[old]\n"
  381. "2: " _ASM_MOV " %[return_hooker], (%[parent_replaced])\n"
  382. " movl $0, %[faulted]\n"
  383. ".section .fixup, \"ax\"\n"
  384. "3: movl $1, %[faulted]\n"
  385. ".previous\n"
  386. _ASM_EXTABLE(1b, 3b)
  387. _ASM_EXTABLE(2b, 3b)
  388. : [parent_replaced] "=r" (parent), [old] "=r" (old),
  389. [faulted] "=r" (faulted)
  390. : [parent_old] "0" (parent), [return_hooker] "r" (return_hooker)
  391. : "memory"
  392. );
  393. if (unlikely(faulted)) {
  394. ftrace_graph_stop();
  395. WARN_ON(1);
  396. return;
  397. }
  398. calltime = cpu_clock(raw_smp_processor_id());
  399. if (push_return_trace(old, calltime,
  400. self_addr, &trace.depth) == -EBUSY) {
  401. *parent = old;
  402. return;
  403. }
  404. trace.func = self_addr;
  405. /* Only trace if the calling function expects to */
  406. if (!ftrace_graph_entry(&trace)) {
  407. current->curr_ret_stack--;
  408. *parent = old;
  409. }
  410. }
  411. #endif /* CONFIG_FUNCTION_GRAPH_TRACER */