ftrace.c 13 KB

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