ftrace.c 13 KB

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