ftrace.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  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_RET_TRACER
  267. #ifndef CONFIG_DYNAMIC_FTRACE
  268. /*
  269. * These functions are picked from those used on
  270. * this page for dynamic ftrace. They have been
  271. * simplified to ignore all traces in NMI context.
  272. */
  273. static atomic_t in_nmi;
  274. void ftrace_nmi_enter(void)
  275. {
  276. atomic_inc(&in_nmi);
  277. }
  278. void ftrace_nmi_exit(void)
  279. {
  280. atomic_dec(&in_nmi);
  281. }
  282. #endif /* !CONFIG_DYNAMIC_FTRACE */
  283. /* Add a function return address to the trace stack on thread info.*/
  284. static int push_return_trace(unsigned long ret, unsigned long long time,
  285. unsigned long func)
  286. {
  287. int index;
  288. struct thread_info *ti = current_thread_info();
  289. /* The return trace stack is full */
  290. if (ti->curr_ret_stack == FTRACE_RET_STACK_SIZE - 1) {
  291. atomic_inc(&ti->trace_overrun);
  292. return -EBUSY;
  293. }
  294. index = ++ti->curr_ret_stack;
  295. barrier();
  296. ti->ret_stack[index].ret = ret;
  297. ti->ret_stack[index].func = func;
  298. ti->ret_stack[index].calltime = time;
  299. return 0;
  300. }
  301. /* Retrieve a function return address to the trace stack on thread info.*/
  302. static void pop_return_trace(unsigned long *ret, unsigned long long *time,
  303. unsigned long *func, unsigned long *overrun)
  304. {
  305. int index;
  306. struct thread_info *ti = current_thread_info();
  307. index = ti->curr_ret_stack;
  308. *ret = ti->ret_stack[index].ret;
  309. *func = ti->ret_stack[index].func;
  310. *time = ti->ret_stack[index].calltime;
  311. *overrun = atomic_read(&ti->trace_overrun);
  312. ti->curr_ret_stack--;
  313. }
  314. /*
  315. * Send the trace to the ring-buffer.
  316. * @return the original return address.
  317. */
  318. unsigned long ftrace_return_to_handler(void)
  319. {
  320. struct ftrace_retfunc trace;
  321. pop_return_trace(&trace.ret, &trace.calltime, &trace.func,
  322. &trace.overrun);
  323. trace.rettime = cpu_clock(raw_smp_processor_id());
  324. ftrace_function_return(&trace);
  325. return trace.ret;
  326. }
  327. /*
  328. * Hook the return address and push it in the stack of return addrs
  329. * in current thread info.
  330. */
  331. void prepare_ftrace_return(unsigned long *parent, unsigned long self_addr)
  332. {
  333. unsigned long old;
  334. unsigned long long calltime;
  335. int faulted;
  336. unsigned long return_hooker = (unsigned long)
  337. &return_to_handler;
  338. /* Nmi's are currently unsupported */
  339. if (atomic_read(&in_nmi))
  340. return;
  341. /*
  342. * Protect against fault, even if it shouldn't
  343. * happen. This tool is too much intrusive to
  344. * ignore such a protection.
  345. */
  346. asm volatile(
  347. "1: movl (%[parent_old]), %[old]\n"
  348. "2: movl %[return_hooker], (%[parent_replaced])\n"
  349. " movl $0, %[faulted]\n"
  350. ".section .fixup, \"ax\"\n"
  351. "3: movl $1, %[faulted]\n"
  352. ".previous\n"
  353. ".section __ex_table, \"a\"\n"
  354. " .long 1b, 3b\n"
  355. " .long 2b, 3b\n"
  356. ".previous\n"
  357. : [parent_replaced] "=r" (parent), [old] "=r" (old),
  358. [faulted] "=r" (faulted)
  359. : [parent_old] "0" (parent), [return_hooker] "r" (return_hooker)
  360. : "memory"
  361. );
  362. if (WARN_ON(faulted)) {
  363. unregister_ftrace_return();
  364. return;
  365. }
  366. if (WARN_ON(!__kernel_text_address(old))) {
  367. unregister_ftrace_return();
  368. *parent = old;
  369. return;
  370. }
  371. calltime = cpu_clock(raw_smp_processor_id());
  372. if (push_return_trace(old, calltime, self_addr) == -EBUSY)
  373. *parent = old;
  374. }
  375. #endif /* CONFIG_FUNCTION_RET_TRACER */