ftrace.c 12 KB

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