ftrace.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534
  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 <trace/syscall.h>
  20. #include <asm/cacheflush.h>
  21. #include <asm/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) Wait for any running NMIs to finish and set a flag that says
  71. * we are modifying code, it is done in an atomic operation.
  72. * 3) Write the code
  73. * 4) clear the flag.
  74. * 5) 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. #define MOD_CODE_WRITE_FLAG (1 << 31) /* set when NMI should do the write */
  86. static atomic_t nmi_running = ATOMIC_INIT(0);
  87. static int mod_code_status; /* holds return value of text 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 clear_mod_flag(void)
  101. {
  102. int old = atomic_read(&nmi_running);
  103. for (;;) {
  104. int new = old & ~MOD_CODE_WRITE_FLAG;
  105. if (old == new)
  106. break;
  107. old = atomic_cmpxchg(&nmi_running, old, new);
  108. }
  109. }
  110. static void ftrace_mod_code(void)
  111. {
  112. /*
  113. * Yes, more than one CPU process can be writing to mod_code_status.
  114. * (and the code itself)
  115. * But if one were to fail, then they all should, and if one were
  116. * to succeed, then they all should.
  117. */
  118. mod_code_status = probe_kernel_write(mod_code_ip, mod_code_newcode,
  119. MCOUNT_INSN_SIZE);
  120. /* if we fail, then kill any new writers */
  121. if (mod_code_status)
  122. clear_mod_flag();
  123. }
  124. void ftrace_nmi_enter(void)
  125. {
  126. if (atomic_inc_return(&nmi_running) & MOD_CODE_WRITE_FLAG) {
  127. smp_rmb();
  128. ftrace_mod_code();
  129. atomic_inc(&nmi_update_count);
  130. }
  131. /* Must have previous changes seen before executions */
  132. smp_mb();
  133. }
  134. void ftrace_nmi_exit(void)
  135. {
  136. /* Finish all executions before clearing nmi_running */
  137. smp_mb();
  138. atomic_dec(&nmi_running);
  139. }
  140. static void wait_for_nmi_and_set_mod_flag(void)
  141. {
  142. if (!atomic_cmpxchg(&nmi_running, 0, MOD_CODE_WRITE_FLAG))
  143. return;
  144. do {
  145. cpu_relax();
  146. } while (atomic_cmpxchg(&nmi_running, 0, MOD_CODE_WRITE_FLAG));
  147. nmi_wait_count++;
  148. }
  149. static void wait_for_nmi(void)
  150. {
  151. if (!atomic_read(&nmi_running))
  152. return;
  153. do {
  154. cpu_relax();
  155. } while (atomic_read(&nmi_running));
  156. nmi_wait_count++;
  157. }
  158. static int
  159. do_ftrace_mod_code(unsigned long ip, void *new_code)
  160. {
  161. mod_code_ip = (void *)ip;
  162. mod_code_newcode = new_code;
  163. /* The buffers need to be visible before we let NMIs write them */
  164. smp_mb();
  165. wait_for_nmi_and_set_mod_flag();
  166. /* Make sure all running NMIs have finished before we write the code */
  167. smp_mb();
  168. ftrace_mod_code();
  169. /* Make sure the write happens before clearing the bit */
  170. smp_mb();
  171. clear_mod_flag();
  172. wait_for_nmi();
  173. return mod_code_status;
  174. }
  175. static unsigned char ftrace_nop[MCOUNT_INSN_SIZE];
  176. static unsigned char *ftrace_nop_replace(void)
  177. {
  178. return ftrace_nop;
  179. }
  180. static int
  181. ftrace_modify_code(unsigned long ip, unsigned char *old_code,
  182. unsigned char *new_code)
  183. {
  184. unsigned char replaced[MCOUNT_INSN_SIZE];
  185. /*
  186. * Note: Due to modules and __init, code can
  187. * disappear and change, we need to protect against faulting
  188. * as well as code changing. We do this by using the
  189. * probe_kernel_* functions.
  190. *
  191. * No real locking needed, this code is run through
  192. * kstop_machine, or before SMP starts.
  193. */
  194. /* read the text we want to modify */
  195. if (probe_kernel_read(replaced, (void *)ip, MCOUNT_INSN_SIZE))
  196. return -EFAULT;
  197. /* Make sure it is what we expect it to be */
  198. if (memcmp(replaced, old_code, MCOUNT_INSN_SIZE) != 0)
  199. return -EINVAL;
  200. /* replace the text with the new text */
  201. if (do_ftrace_mod_code(ip, new_code))
  202. return -EPERM;
  203. sync_core();
  204. return 0;
  205. }
  206. int ftrace_make_nop(struct module *mod,
  207. struct dyn_ftrace *rec, unsigned long addr)
  208. {
  209. unsigned char *new, *old;
  210. unsigned long ip = rec->ip;
  211. old = ftrace_call_replace(ip, addr);
  212. new = ftrace_nop_replace();
  213. return ftrace_modify_code(rec->ip, old, new);
  214. }
  215. int ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr)
  216. {
  217. unsigned char *new, *old;
  218. unsigned long ip = rec->ip;
  219. old = ftrace_nop_replace();
  220. new = ftrace_call_replace(ip, addr);
  221. return ftrace_modify_code(rec->ip, old, new);
  222. }
  223. int ftrace_update_ftrace_func(ftrace_func_t func)
  224. {
  225. unsigned long ip = (unsigned long)(&ftrace_call);
  226. unsigned char old[MCOUNT_INSN_SIZE], *new;
  227. int ret;
  228. memcpy(old, &ftrace_call, MCOUNT_INSN_SIZE);
  229. new = ftrace_call_replace(ip, (unsigned long)func);
  230. ret = ftrace_modify_code(ip, old, new);
  231. return ret;
  232. }
  233. int __init ftrace_dyn_arch_init(void *data)
  234. {
  235. extern const unsigned char ftrace_test_p6nop[];
  236. extern const unsigned char ftrace_test_nop5[];
  237. extern const unsigned char ftrace_test_jmp[];
  238. int faulted = 0;
  239. /*
  240. * There is no good nop for all x86 archs.
  241. * We will default to using the P6_NOP5, but first we
  242. * will test to make sure that the nop will actually
  243. * work on this CPU. If it faults, we will then
  244. * go to a lesser efficient 5 byte nop. If that fails
  245. * we then just use a jmp as our nop. This isn't the most
  246. * efficient nop, but we can not use a multi part nop
  247. * since we would then risk being preempted in the middle
  248. * of that nop, and if we enabled tracing then, it might
  249. * cause a system crash.
  250. *
  251. * TODO: check the cpuid to determine the best nop.
  252. */
  253. asm volatile (
  254. "ftrace_test_jmp:"
  255. "jmp ftrace_test_p6nop\n"
  256. "nop\n"
  257. "nop\n"
  258. "nop\n" /* 2 byte jmp + 3 bytes */
  259. "ftrace_test_p6nop:"
  260. P6_NOP5
  261. "jmp 1f\n"
  262. "ftrace_test_nop5:"
  263. ".byte 0x66,0x66,0x66,0x66,0x90\n"
  264. "1:"
  265. ".section .fixup, \"ax\"\n"
  266. "2: movl $1, %0\n"
  267. " jmp ftrace_test_nop5\n"
  268. "3: movl $2, %0\n"
  269. " jmp 1b\n"
  270. ".previous\n"
  271. _ASM_EXTABLE(ftrace_test_p6nop, 2b)
  272. _ASM_EXTABLE(ftrace_test_nop5, 3b)
  273. : "=r"(faulted) : "0" (faulted));
  274. switch (faulted) {
  275. case 0:
  276. pr_info("ftrace: converting mcount calls to 0f 1f 44 00 00\n");
  277. memcpy(ftrace_nop, ftrace_test_p6nop, MCOUNT_INSN_SIZE);
  278. break;
  279. case 1:
  280. pr_info("ftrace: converting mcount calls to 66 66 66 66 90\n");
  281. memcpy(ftrace_nop, ftrace_test_nop5, MCOUNT_INSN_SIZE);
  282. break;
  283. case 2:
  284. pr_info("ftrace: converting mcount calls to jmp . + 5\n");
  285. memcpy(ftrace_nop, ftrace_test_jmp, MCOUNT_INSN_SIZE);
  286. break;
  287. }
  288. /* The return code is retured via data */
  289. *(unsigned long *)data = 0;
  290. return 0;
  291. }
  292. #endif
  293. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  294. #ifdef CONFIG_DYNAMIC_FTRACE
  295. extern void ftrace_graph_call(void);
  296. static int ftrace_mod_jmp(unsigned long ip,
  297. int old_offset, int new_offset)
  298. {
  299. unsigned char code[MCOUNT_INSN_SIZE];
  300. if (probe_kernel_read(code, (void *)ip, MCOUNT_INSN_SIZE))
  301. return -EFAULT;
  302. if (code[0] != 0xe9 || old_offset != *(int *)(&code[1]))
  303. return -EINVAL;
  304. *(int *)(&code[1]) = new_offset;
  305. if (do_ftrace_mod_code(ip, &code))
  306. return -EPERM;
  307. return 0;
  308. }
  309. int ftrace_enable_ftrace_graph_caller(void)
  310. {
  311. unsigned long ip = (unsigned long)(&ftrace_graph_call);
  312. int old_offset, new_offset;
  313. old_offset = (unsigned long)(&ftrace_stub) - (ip + MCOUNT_INSN_SIZE);
  314. new_offset = (unsigned long)(&ftrace_graph_caller) - (ip + MCOUNT_INSN_SIZE);
  315. return ftrace_mod_jmp(ip, old_offset, new_offset);
  316. }
  317. int ftrace_disable_ftrace_graph_caller(void)
  318. {
  319. unsigned long ip = (unsigned long)(&ftrace_graph_call);
  320. int old_offset, new_offset;
  321. old_offset = (unsigned long)(&ftrace_graph_caller) - (ip + MCOUNT_INSN_SIZE);
  322. new_offset = (unsigned long)(&ftrace_stub) - (ip + MCOUNT_INSN_SIZE);
  323. return ftrace_mod_jmp(ip, old_offset, new_offset);
  324. }
  325. #endif /* !CONFIG_DYNAMIC_FTRACE */
  326. /*
  327. * Hook the return address and push it in the stack of return addrs
  328. * in current thread info.
  329. */
  330. void prepare_ftrace_return(unsigned long *parent, unsigned long self_addr,
  331. unsigned long frame_pointer)
  332. {
  333. unsigned long old;
  334. int faulted;
  335. struct ftrace_graph_ent trace;
  336. unsigned long return_hooker = (unsigned long)
  337. &return_to_handler;
  338. /* Nmi's are currently unsupported */
  339. if (unlikely(in_nmi()))
  340. return;
  341. if (unlikely(atomic_read(&current->tracing_graph_pause)))
  342. return;
  343. /*
  344. * Protect against fault, even if it shouldn't
  345. * happen. This tool is too much intrusive to
  346. * ignore such a protection.
  347. */
  348. asm volatile(
  349. "1: " _ASM_MOV " (%[parent]), %[old]\n"
  350. "2: " _ASM_MOV " %[return_hooker], (%[parent])\n"
  351. " movl $0, %[faulted]\n"
  352. "3:\n"
  353. ".section .fixup, \"ax\"\n"
  354. "4: movl $1, %[faulted]\n"
  355. " jmp 3b\n"
  356. ".previous\n"
  357. _ASM_EXTABLE(1b, 4b)
  358. _ASM_EXTABLE(2b, 4b)
  359. : [old] "=&r" (old), [faulted] "=r" (faulted)
  360. : [parent] "r" (parent), [return_hooker] "r" (return_hooker)
  361. : "memory"
  362. );
  363. if (unlikely(faulted)) {
  364. ftrace_graph_stop();
  365. WARN_ON(1);
  366. return;
  367. }
  368. if (ftrace_push_return_trace(old, self_addr, &trace.depth,
  369. frame_pointer) == -EBUSY) {
  370. *parent = old;
  371. return;
  372. }
  373. trace.func = self_addr;
  374. /* Only trace if the calling function expects to */
  375. if (!ftrace_graph_entry(&trace)) {
  376. current->curr_ret_stack--;
  377. *parent = old;
  378. }
  379. }
  380. #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
  381. #ifdef CONFIG_FTRACE_SYSCALLS
  382. extern unsigned long __start_syscalls_metadata[];
  383. extern unsigned long __stop_syscalls_metadata[];
  384. extern unsigned long *sys_call_table;
  385. static struct syscall_metadata **syscalls_metadata;
  386. static struct syscall_metadata *find_syscall_meta(unsigned long *syscall)
  387. {
  388. struct syscall_metadata *start;
  389. struct syscall_metadata *stop;
  390. char str[KSYM_SYMBOL_LEN];
  391. start = (struct syscall_metadata *)__start_syscalls_metadata;
  392. stop = (struct syscall_metadata *)__stop_syscalls_metadata;
  393. kallsyms_lookup((unsigned long) syscall, NULL, NULL, NULL, str);
  394. for ( ; start < stop; start++) {
  395. if (start->name && !strcmp(start->name, str))
  396. return start;
  397. }
  398. return NULL;
  399. }
  400. struct syscall_metadata *syscall_nr_to_meta(int nr)
  401. {
  402. if (!syscalls_metadata || nr >= FTRACE_SYSCALL_MAX || nr < 0)
  403. return NULL;
  404. return syscalls_metadata[nr];
  405. }
  406. void arch_init_ftrace_syscalls(void)
  407. {
  408. int i;
  409. struct syscall_metadata *meta;
  410. unsigned long **psys_syscall_table = &sys_call_table;
  411. static atomic_t refs;
  412. if (atomic_inc_return(&refs) != 1)
  413. goto end;
  414. syscalls_metadata = kzalloc(sizeof(*syscalls_metadata) *
  415. FTRACE_SYSCALL_MAX, GFP_KERNEL);
  416. if (!syscalls_metadata) {
  417. WARN_ON(1);
  418. return;
  419. }
  420. for (i = 0; i < FTRACE_SYSCALL_MAX; i++) {
  421. meta = find_syscall_meta(psys_syscall_table[i]);
  422. syscalls_metadata[i] = meta;
  423. }
  424. return;
  425. /* Paranoid: avoid overflow */
  426. end:
  427. atomic_dec(&refs);
  428. }
  429. #endif