ftrace.c 12 KB

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