ftrace.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  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 int
  160. do_ftrace_mod_code(unsigned long ip, void *new_code)
  161. {
  162. mod_code_ip = (void *)ip;
  163. mod_code_newcode = new_code;
  164. /* The buffers need to be visible before we let NMIs write them */
  165. smp_mb();
  166. wait_for_nmi_and_set_mod_flag();
  167. /* Make sure all running NMIs have finished before we write the code */
  168. smp_mb();
  169. ftrace_mod_code();
  170. /* Make sure the write happens before clearing the bit */
  171. smp_mb();
  172. clear_mod_flag();
  173. wait_for_nmi();
  174. return mod_code_status;
  175. }
  176. static unsigned char ftrace_nop[MCOUNT_INSN_SIZE];
  177. static unsigned char *ftrace_nop_replace(void)
  178. {
  179. return ftrace_nop;
  180. }
  181. static int
  182. ftrace_modify_code(unsigned long ip, unsigned char *old_code,
  183. unsigned char *new_code)
  184. {
  185. unsigned char replaced[MCOUNT_INSN_SIZE];
  186. /*
  187. * Note: Due to modules and __init, code can
  188. * disappear and change, we need to protect against faulting
  189. * as well as code changing. We do this by using the
  190. * probe_kernel_* functions.
  191. *
  192. * No real locking needed, this code is run through
  193. * kstop_machine, or before SMP starts.
  194. */
  195. /* read the text we want to modify */
  196. if (probe_kernel_read(replaced, (void *)ip, MCOUNT_INSN_SIZE))
  197. return -EFAULT;
  198. /* Make sure it is what we expect it to be */
  199. if (memcmp(replaced, old_code, MCOUNT_INSN_SIZE) != 0)
  200. return -EINVAL;
  201. /* replace the text with the new text */
  202. if (do_ftrace_mod_code(ip, new_code))
  203. return -EPERM;
  204. sync_core();
  205. return 0;
  206. }
  207. int ftrace_make_nop(struct module *mod,
  208. struct dyn_ftrace *rec, unsigned long addr)
  209. {
  210. unsigned char *new, *old;
  211. unsigned long ip = rec->ip;
  212. old = ftrace_call_replace(ip, addr);
  213. new = ftrace_nop_replace();
  214. return ftrace_modify_code(rec->ip, old, new);
  215. }
  216. int ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr)
  217. {
  218. unsigned char *new, *old;
  219. unsigned long ip = rec->ip;
  220. old = ftrace_nop_replace();
  221. new = ftrace_call_replace(ip, addr);
  222. return ftrace_modify_code(rec->ip, old, new);
  223. }
  224. int ftrace_update_ftrace_func(ftrace_func_t func)
  225. {
  226. unsigned long ip = (unsigned long)(&ftrace_call);
  227. unsigned char old[MCOUNT_INSN_SIZE], *new;
  228. int ret;
  229. memcpy(old, &ftrace_call, MCOUNT_INSN_SIZE);
  230. new = ftrace_call_replace(ip, (unsigned long)func);
  231. ret = ftrace_modify_code(ip, old, new);
  232. return ret;
  233. }
  234. int __init ftrace_dyn_arch_init(void *data)
  235. {
  236. extern const unsigned char ftrace_test_p6nop[];
  237. extern const unsigned char ftrace_test_nop5[];
  238. extern const unsigned char ftrace_test_jmp[];
  239. int faulted = 0;
  240. /*
  241. * There is no good nop for all x86 archs.
  242. * We will default to using the P6_NOP5, but first we
  243. * will test to make sure that the nop will actually
  244. * work on this CPU. If it faults, we will then
  245. * go to a lesser efficient 5 byte nop. If that fails
  246. * we then just use a jmp as our nop. This isn't the most
  247. * efficient nop, but we can not use a multi part nop
  248. * since we would then risk being preempted in the middle
  249. * of that nop, and if we enabled tracing then, it might
  250. * cause a system crash.
  251. *
  252. * TODO: check the cpuid to determine the best nop.
  253. */
  254. asm volatile (
  255. "ftrace_test_jmp:"
  256. "jmp ftrace_test_p6nop\n"
  257. "nop\n"
  258. "nop\n"
  259. "nop\n" /* 2 byte jmp + 3 bytes */
  260. "ftrace_test_p6nop:"
  261. P6_NOP5
  262. "jmp 1f\n"
  263. "ftrace_test_nop5:"
  264. ".byte 0x66,0x66,0x66,0x66,0x90\n"
  265. "1:"
  266. ".section .fixup, \"ax\"\n"
  267. "2: movl $1, %0\n"
  268. " jmp ftrace_test_nop5\n"
  269. "3: movl $2, %0\n"
  270. " jmp 1b\n"
  271. ".previous\n"
  272. _ASM_EXTABLE(ftrace_test_p6nop, 2b)
  273. _ASM_EXTABLE(ftrace_test_nop5, 3b)
  274. : "=r"(faulted) : "0" (faulted));
  275. switch (faulted) {
  276. case 0:
  277. pr_info("converting mcount calls to 0f 1f 44 00 00\n");
  278. memcpy(ftrace_nop, ftrace_test_p6nop, MCOUNT_INSN_SIZE);
  279. break;
  280. case 1:
  281. pr_info("converting mcount calls to 66 66 66 66 90\n");
  282. memcpy(ftrace_nop, ftrace_test_nop5, MCOUNT_INSN_SIZE);
  283. break;
  284. case 2:
  285. pr_info("converting mcount calls to jmp . + 5\n");
  286. memcpy(ftrace_nop, ftrace_test_jmp, MCOUNT_INSN_SIZE);
  287. break;
  288. }
  289. /* The return code is retured via data */
  290. *(unsigned long *)data = 0;
  291. return 0;
  292. }
  293. #endif
  294. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  295. #ifdef CONFIG_DYNAMIC_FTRACE
  296. extern void ftrace_graph_call(void);
  297. static int ftrace_mod_jmp(unsigned long ip,
  298. int old_offset, int new_offset)
  299. {
  300. unsigned char code[MCOUNT_INSN_SIZE];
  301. if (probe_kernel_read(code, (void *)ip, MCOUNT_INSN_SIZE))
  302. return -EFAULT;
  303. if (code[0] != 0xe9 || old_offset != *(int *)(&code[1]))
  304. return -EINVAL;
  305. *(int *)(&code[1]) = new_offset;
  306. if (do_ftrace_mod_code(ip, &code))
  307. return -EPERM;
  308. return 0;
  309. }
  310. int ftrace_enable_ftrace_graph_caller(void)
  311. {
  312. unsigned long ip = (unsigned long)(&ftrace_graph_call);
  313. int old_offset, new_offset;
  314. old_offset = (unsigned long)(&ftrace_stub) - (ip + MCOUNT_INSN_SIZE);
  315. new_offset = (unsigned long)(&ftrace_graph_caller) - (ip + MCOUNT_INSN_SIZE);
  316. return ftrace_mod_jmp(ip, old_offset, new_offset);
  317. }
  318. int ftrace_disable_ftrace_graph_caller(void)
  319. {
  320. unsigned long ip = (unsigned long)(&ftrace_graph_call);
  321. int old_offset, new_offset;
  322. old_offset = (unsigned long)(&ftrace_graph_caller) - (ip + MCOUNT_INSN_SIZE);
  323. new_offset = (unsigned long)(&ftrace_stub) - (ip + MCOUNT_INSN_SIZE);
  324. return ftrace_mod_jmp(ip, old_offset, new_offset);
  325. }
  326. #endif /* !CONFIG_DYNAMIC_FTRACE */
  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. unsigned long frame_pointer)
  333. {
  334. unsigned long old;
  335. int faulted;
  336. struct ftrace_graph_ent trace;
  337. unsigned long return_hooker = (unsigned long)
  338. &return_to_handler;
  339. if (unlikely(atomic_read(&current->tracing_graph_pause)))
  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: " _ASM_MOV " (%[parent]), %[old]\n"
  348. "2: " _ASM_MOV " %[return_hooker], (%[parent])\n"
  349. " movl $0, %[faulted]\n"
  350. "3:\n"
  351. ".section .fixup, \"ax\"\n"
  352. "4: movl $1, %[faulted]\n"
  353. " jmp 3b\n"
  354. ".previous\n"
  355. _ASM_EXTABLE(1b, 4b)
  356. _ASM_EXTABLE(2b, 4b)
  357. : [old] "=&r" (old), [faulted] "=r" (faulted)
  358. : [parent] "r" (parent), [return_hooker] "r" (return_hooker)
  359. : "memory"
  360. );
  361. if (unlikely(faulted)) {
  362. ftrace_graph_stop();
  363. WARN_ON(1);
  364. return;
  365. }
  366. if (ftrace_push_return_trace(old, self_addr, &trace.depth,
  367. frame_pointer) == -EBUSY) {
  368. *parent = old;
  369. return;
  370. }
  371. trace.func = self_addr;
  372. /* Only trace if the calling function expects to */
  373. if (!ftrace_graph_entry(&trace)) {
  374. current->curr_ret_stack--;
  375. *parent = old;
  376. }
  377. }
  378. #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
  379. #ifdef CONFIG_FTRACE_SYSCALLS
  380. extern unsigned long *sys_call_table;
  381. unsigned long __init arch_syscall_addr(int nr)
  382. {
  383. return (unsigned long)(&sys_call_table)[nr];
  384. }
  385. #endif