ftrace.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537
  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 in_nmi = 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(&in_nmi);
  103. /* Must have in_nmi 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 in_nmi */
  113. smp_wmb();
  114. atomic_dec(&in_nmi);
  115. }
  116. static void wait_for_nmi(void)
  117. {
  118. int waited = 0;
  119. while (atomic_read(&in_nmi)) {
  120. waited = 1;
  121. cpu_relax();
  122. }
  123. if (waited)
  124. nmi_wait_count++;
  125. }
  126. static int
  127. do_ftrace_mod_code(unsigned long ip, void *new_code)
  128. {
  129. mod_code_ip = (void *)ip;
  130. mod_code_newcode = new_code;
  131. /* The buffers need to be visible before we let NMIs write them */
  132. smp_wmb();
  133. mod_code_write = 1;
  134. /* Make sure write bit is visible before we wait on NMIs */
  135. smp_mb();
  136. wait_for_nmi();
  137. /* Make sure all running NMIs have finished before we write the code */
  138. smp_mb();
  139. ftrace_mod_code();
  140. /* Make sure the write happens before clearing the bit */
  141. smp_wmb();
  142. mod_code_write = 0;
  143. /* make sure NMIs see the cleared bit */
  144. smp_mb();
  145. wait_for_nmi();
  146. return mod_code_status;
  147. }
  148. static unsigned char ftrace_nop[MCOUNT_INSN_SIZE];
  149. static unsigned char *ftrace_nop_replace(void)
  150. {
  151. return ftrace_nop;
  152. }
  153. static int
  154. ftrace_modify_code(unsigned long ip, unsigned char *old_code,
  155. unsigned char *new_code)
  156. {
  157. unsigned char replaced[MCOUNT_INSN_SIZE];
  158. /*
  159. * Note: Due to modules and __init, code can
  160. * disappear and change, we need to protect against faulting
  161. * as well as code changing. We do this by using the
  162. * probe_kernel_* functions.
  163. *
  164. * No real locking needed, this code is run through
  165. * kstop_machine, or before SMP starts.
  166. */
  167. /* read the text we want to modify */
  168. if (probe_kernel_read(replaced, (void *)ip, MCOUNT_INSN_SIZE))
  169. return -EFAULT;
  170. /* Make sure it is what we expect it to be */
  171. if (memcmp(replaced, old_code, MCOUNT_INSN_SIZE) != 0)
  172. return -EINVAL;
  173. /* replace the text with the new text */
  174. if (do_ftrace_mod_code(ip, new_code))
  175. return -EPERM;
  176. sync_core();
  177. return 0;
  178. }
  179. int ftrace_make_nop(struct module *mod,
  180. struct dyn_ftrace *rec, unsigned long addr)
  181. {
  182. unsigned char *new, *old;
  183. unsigned long ip = rec->ip;
  184. old = ftrace_call_replace(ip, addr);
  185. new = ftrace_nop_replace();
  186. return ftrace_modify_code(rec->ip, old, new);
  187. }
  188. int ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr)
  189. {
  190. unsigned char *new, *old;
  191. unsigned long ip = rec->ip;
  192. old = ftrace_nop_replace();
  193. new = ftrace_call_replace(ip, addr);
  194. return ftrace_modify_code(rec->ip, old, new);
  195. }
  196. int ftrace_update_ftrace_func(ftrace_func_t func)
  197. {
  198. unsigned long ip = (unsigned long)(&ftrace_call);
  199. unsigned char old[MCOUNT_INSN_SIZE], *new;
  200. int ret;
  201. memcpy(old, &ftrace_call, MCOUNT_INSN_SIZE);
  202. new = ftrace_call_replace(ip, (unsigned long)func);
  203. ret = ftrace_modify_code(ip, old, new);
  204. return ret;
  205. }
  206. int __init ftrace_dyn_arch_init(void *data)
  207. {
  208. extern const unsigned char ftrace_test_p6nop[];
  209. extern const unsigned char ftrace_test_nop5[];
  210. extern const unsigned char ftrace_test_jmp[];
  211. int faulted = 0;
  212. /*
  213. * There is no good nop for all x86 archs.
  214. * We will default to using the P6_NOP5, but first we
  215. * will test to make sure that the nop will actually
  216. * work on this CPU. If it faults, we will then
  217. * go to a lesser efficient 5 byte nop. If that fails
  218. * we then just use a jmp as our nop. This isn't the most
  219. * efficient nop, but we can not use a multi part nop
  220. * since we would then risk being preempted in the middle
  221. * of that nop, and if we enabled tracing then, it might
  222. * cause a system crash.
  223. *
  224. * TODO: check the cpuid to determine the best nop.
  225. */
  226. asm volatile (
  227. "ftrace_test_jmp:"
  228. "jmp ftrace_test_p6nop\n"
  229. "nop\n"
  230. "nop\n"
  231. "nop\n" /* 2 byte jmp + 3 bytes */
  232. "ftrace_test_p6nop:"
  233. P6_NOP5
  234. "jmp 1f\n"
  235. "ftrace_test_nop5:"
  236. ".byte 0x66,0x66,0x66,0x66,0x90\n"
  237. "1:"
  238. ".section .fixup, \"ax\"\n"
  239. "2: movl $1, %0\n"
  240. " jmp ftrace_test_nop5\n"
  241. "3: movl $2, %0\n"
  242. " jmp 1b\n"
  243. ".previous\n"
  244. _ASM_EXTABLE(ftrace_test_p6nop, 2b)
  245. _ASM_EXTABLE(ftrace_test_nop5, 3b)
  246. : "=r"(faulted) : "0" (faulted));
  247. switch (faulted) {
  248. case 0:
  249. pr_info("ftrace: converting mcount calls to 0f 1f 44 00 00\n");
  250. memcpy(ftrace_nop, ftrace_test_p6nop, MCOUNT_INSN_SIZE);
  251. break;
  252. case 1:
  253. pr_info("ftrace: converting mcount calls to 66 66 66 66 90\n");
  254. memcpy(ftrace_nop, ftrace_test_nop5, MCOUNT_INSN_SIZE);
  255. break;
  256. case 2:
  257. pr_info("ftrace: converting mcount calls to jmp . + 5\n");
  258. memcpy(ftrace_nop, ftrace_test_jmp, MCOUNT_INSN_SIZE);
  259. break;
  260. }
  261. /* The return code is retured via data */
  262. *(unsigned long *)data = 0;
  263. return 0;
  264. }
  265. #endif
  266. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  267. #ifdef CONFIG_DYNAMIC_FTRACE
  268. extern void ftrace_graph_call(void);
  269. static int ftrace_mod_jmp(unsigned long ip,
  270. int old_offset, int new_offset)
  271. {
  272. unsigned char code[MCOUNT_INSN_SIZE];
  273. if (probe_kernel_read(code, (void *)ip, MCOUNT_INSN_SIZE))
  274. return -EFAULT;
  275. if (code[0] != 0xe9 || old_offset != *(int *)(&code[1]))
  276. return -EINVAL;
  277. *(int *)(&code[1]) = new_offset;
  278. if (do_ftrace_mod_code(ip, &code))
  279. return -EPERM;
  280. return 0;
  281. }
  282. int ftrace_enable_ftrace_graph_caller(void)
  283. {
  284. unsigned long ip = (unsigned long)(&ftrace_graph_call);
  285. int old_offset, new_offset;
  286. old_offset = (unsigned long)(&ftrace_stub) - (ip + MCOUNT_INSN_SIZE);
  287. new_offset = (unsigned long)(&ftrace_graph_caller) - (ip + MCOUNT_INSN_SIZE);
  288. return ftrace_mod_jmp(ip, old_offset, new_offset);
  289. }
  290. int ftrace_disable_ftrace_graph_caller(void)
  291. {
  292. unsigned long ip = (unsigned long)(&ftrace_graph_call);
  293. int old_offset, new_offset;
  294. old_offset = (unsigned long)(&ftrace_graph_caller) - (ip + MCOUNT_INSN_SIZE);
  295. new_offset = (unsigned long)(&ftrace_stub) - (ip + MCOUNT_INSN_SIZE);
  296. return ftrace_mod_jmp(ip, old_offset, new_offset);
  297. }
  298. #else /* CONFIG_DYNAMIC_FTRACE */
  299. /*
  300. * These functions are picked from those used on
  301. * this page for dynamic ftrace. They have been
  302. * simplified to ignore all traces in NMI context.
  303. */
  304. static atomic_t in_nmi;
  305. void ftrace_nmi_enter(void)
  306. {
  307. atomic_inc(&in_nmi);
  308. }
  309. void ftrace_nmi_exit(void)
  310. {
  311. atomic_dec(&in_nmi);
  312. }
  313. #endif /* !CONFIG_DYNAMIC_FTRACE */
  314. /* Add a function return address to the trace stack on thread info.*/
  315. static int push_return_trace(unsigned long ret, unsigned long long time,
  316. unsigned long func, int *depth)
  317. {
  318. int index;
  319. if (!current->ret_stack)
  320. return -EBUSY;
  321. /* The return trace stack is full */
  322. if (current->curr_ret_stack == FTRACE_RETFUNC_DEPTH - 1) {
  323. atomic_inc(&current->trace_overrun);
  324. return -EBUSY;
  325. }
  326. index = ++current->curr_ret_stack;
  327. barrier();
  328. current->ret_stack[index].ret = ret;
  329. current->ret_stack[index].func = func;
  330. current->ret_stack[index].calltime = time;
  331. *depth = index;
  332. return 0;
  333. }
  334. /* Retrieve a function return address to the trace stack on thread info.*/
  335. static void pop_return_trace(struct ftrace_graph_ret *trace, unsigned long *ret)
  336. {
  337. int index;
  338. index = current->curr_ret_stack;
  339. if (unlikely(index < 0)) {
  340. ftrace_graph_stop();
  341. WARN_ON(1);
  342. /* Might as well panic, otherwise we have no where to go */
  343. *ret = (unsigned long)panic;
  344. return;
  345. }
  346. *ret = current->ret_stack[index].ret;
  347. trace->func = current->ret_stack[index].func;
  348. trace->calltime = current->ret_stack[index].calltime;
  349. trace->overrun = atomic_read(&current->trace_overrun);
  350. trace->depth = index;
  351. barrier();
  352. current->curr_ret_stack--;
  353. }
  354. /*
  355. * Send the trace to the ring-buffer.
  356. * @return the original return address.
  357. */
  358. unsigned long ftrace_return_to_handler(void)
  359. {
  360. struct ftrace_graph_ret trace;
  361. unsigned long ret;
  362. pop_return_trace(&trace, &ret);
  363. trace.rettime = cpu_clock(raw_smp_processor_id());
  364. ftrace_graph_return(&trace);
  365. if (unlikely(!ret)) {
  366. ftrace_graph_stop();
  367. WARN_ON(1);
  368. /* Might as well panic. What else to do? */
  369. ret = (unsigned long)panic;
  370. }
  371. return ret;
  372. }
  373. /*
  374. * Hook the return address and push it in the stack of return addrs
  375. * in current thread info.
  376. */
  377. void prepare_ftrace_return(unsigned long *parent, unsigned long self_addr)
  378. {
  379. unsigned long old;
  380. unsigned long long calltime;
  381. int faulted;
  382. struct ftrace_graph_ent trace;
  383. unsigned long return_hooker = (unsigned long)
  384. &return_to_handler;
  385. /* Nmi's are currently unsupported */
  386. if (unlikely(atomic_read(&in_nmi)))
  387. return;
  388. if (unlikely(atomic_read(&current->tracing_graph_pause)))
  389. return;
  390. /*
  391. * Protect against fault, even if it shouldn't
  392. * happen. This tool is too much intrusive to
  393. * ignore such a protection.
  394. */
  395. asm volatile(
  396. "1: " _ASM_MOV " (%[parent_old]), %[old]\n"
  397. "2: " _ASM_MOV " %[return_hooker], (%[parent_replaced])\n"
  398. " movl $0, %[faulted]\n"
  399. ".section .fixup, \"ax\"\n"
  400. "3: movl $1, %[faulted]\n"
  401. ".previous\n"
  402. _ASM_EXTABLE(1b, 3b)
  403. _ASM_EXTABLE(2b, 3b)
  404. : [parent_replaced] "=r" (parent), [old] "=r" (old),
  405. [faulted] "=r" (faulted)
  406. : [parent_old] "0" (parent), [return_hooker] "r" (return_hooker)
  407. : "memory"
  408. );
  409. if (unlikely(faulted)) {
  410. ftrace_graph_stop();
  411. WARN_ON(1);
  412. return;
  413. }
  414. if (unlikely(!__kernel_text_address(old))) {
  415. ftrace_graph_stop();
  416. *parent = old;
  417. WARN_ON(1);
  418. return;
  419. }
  420. calltime = cpu_clock(raw_smp_processor_id());
  421. if (push_return_trace(old, calltime,
  422. self_addr, &trace.depth) == -EBUSY) {
  423. *parent = old;
  424. return;
  425. }
  426. trace.func = self_addr;
  427. /* Only trace if the calling function expects to */
  428. if (!ftrace_graph_entry(&trace)) {
  429. current->curr_ret_stack--;
  430. *parent = old;
  431. }
  432. }
  433. #endif /* CONFIG_FUNCTION_GRAPH_TRACER */