ftrace.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630
  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 <linux/module.h>
  21. #include <trace/syscall.h>
  22. #include <asm/cacheflush.h>
  23. #include <asm/kprobes.h>
  24. #include <asm/ftrace.h>
  25. #include <asm/nops.h>
  26. #ifdef CONFIG_DYNAMIC_FTRACE
  27. int ftrace_arch_code_modify_prepare(void)
  28. {
  29. set_kernel_text_rw();
  30. set_all_modules_text_rw();
  31. return 0;
  32. }
  33. int ftrace_arch_code_modify_post_process(void)
  34. {
  35. set_all_modules_text_ro();
  36. set_kernel_text_ro();
  37. return 0;
  38. }
  39. union ftrace_code_union {
  40. char code[MCOUNT_INSN_SIZE];
  41. struct {
  42. char e8;
  43. int offset;
  44. } __attribute__((packed));
  45. };
  46. static int ftrace_calc_offset(long ip, long addr)
  47. {
  48. return (int)(addr - ip);
  49. }
  50. static unsigned char *ftrace_call_replace(unsigned long ip, unsigned long addr)
  51. {
  52. static union ftrace_code_union calc;
  53. calc.e8 = 0xe8;
  54. calc.offset = ftrace_calc_offset(ip + MCOUNT_INSN_SIZE, addr);
  55. /*
  56. * No locking needed, this must be called via kstop_machine
  57. * which in essence is like running on a uniprocessor machine.
  58. */
  59. return calc.code;
  60. }
  61. static inline int
  62. within(unsigned long addr, unsigned long start, unsigned long end)
  63. {
  64. return addr >= start && addr < end;
  65. }
  66. static int
  67. do_ftrace_mod_code(unsigned long ip, const void *new_code)
  68. {
  69. /*
  70. * On x86_64, kernel text mappings are mapped read-only with
  71. * CONFIG_DEBUG_RODATA. So we use the kernel identity mapping instead
  72. * of the kernel text mapping to modify the kernel text.
  73. *
  74. * For 32bit kernels, these mappings are same and we can use
  75. * kernel identity mapping to modify code.
  76. */
  77. if (within(ip, (unsigned long)_text, (unsigned long)_etext))
  78. ip = (unsigned long)__va(__pa(ip));
  79. return probe_kernel_write((void *)ip, new_code, MCOUNT_INSN_SIZE);
  80. }
  81. static const unsigned char *ftrace_nop_replace(void)
  82. {
  83. return ideal_nops[NOP_ATOMIC5];
  84. }
  85. static int
  86. ftrace_modify_code(unsigned long ip, unsigned const char *old_code,
  87. unsigned const char *new_code)
  88. {
  89. unsigned char replaced[MCOUNT_INSN_SIZE];
  90. /*
  91. * Note: Due to modules and __init, code can
  92. * disappear and change, we need to protect against faulting
  93. * as well as code changing. We do this by using the
  94. * probe_kernel_* functions.
  95. *
  96. * No real locking needed, this code is run through
  97. * kstop_machine, or before SMP starts.
  98. */
  99. /* read the text we want to modify */
  100. if (probe_kernel_read(replaced, (void *)ip, MCOUNT_INSN_SIZE))
  101. return -EFAULT;
  102. /* Make sure it is what we expect it to be */
  103. if (memcmp(replaced, old_code, MCOUNT_INSN_SIZE) != 0)
  104. return -EINVAL;
  105. /* replace the text with the new text */
  106. if (do_ftrace_mod_code(ip, new_code))
  107. return -EPERM;
  108. sync_core();
  109. return 0;
  110. }
  111. int ftrace_make_nop(struct module *mod,
  112. struct dyn_ftrace *rec, unsigned long addr)
  113. {
  114. unsigned const char *new, *old;
  115. unsigned long ip = rec->ip;
  116. old = ftrace_call_replace(ip, addr);
  117. new = ftrace_nop_replace();
  118. return ftrace_modify_code(rec->ip, old, new);
  119. }
  120. int ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr)
  121. {
  122. unsigned const char *new, *old;
  123. unsigned long ip = rec->ip;
  124. old = ftrace_nop_replace();
  125. new = ftrace_call_replace(ip, addr);
  126. return ftrace_modify_code(rec->ip, old, new);
  127. }
  128. int ftrace_update_ftrace_func(ftrace_func_t func)
  129. {
  130. unsigned long ip = (unsigned long)(&ftrace_call);
  131. unsigned char old[MCOUNT_INSN_SIZE], *new;
  132. int ret;
  133. memcpy(old, &ftrace_call, MCOUNT_INSN_SIZE);
  134. new = ftrace_call_replace(ip, (unsigned long)func);
  135. ret = ftrace_modify_code(ip, old, new);
  136. return ret;
  137. }
  138. int modifying_ftrace_code __read_mostly;
  139. /*
  140. * A breakpoint was added to the code address we are about to
  141. * modify, and this is the handle that will just skip over it.
  142. * We are either changing a nop into a trace call, or a trace
  143. * call to a nop. While the change is taking place, we treat
  144. * it just like it was a nop.
  145. */
  146. int ftrace_int3_handler(struct pt_regs *regs)
  147. {
  148. if (WARN_ON_ONCE(!regs))
  149. return 0;
  150. if (!ftrace_location(regs->ip - 1))
  151. return 0;
  152. regs->ip += MCOUNT_INSN_SIZE - 1;
  153. return 1;
  154. }
  155. static int ftrace_write(unsigned long ip, const char *val, int size)
  156. {
  157. /*
  158. * On x86_64, kernel text mappings are mapped read-only with
  159. * CONFIG_DEBUG_RODATA. So we use the kernel identity mapping instead
  160. * of the kernel text mapping to modify the kernel text.
  161. *
  162. * For 32bit kernels, these mappings are same and we can use
  163. * kernel identity mapping to modify code.
  164. */
  165. if (within(ip, (unsigned long)_text, (unsigned long)_etext))
  166. ip = (unsigned long)__va(__pa(ip));
  167. return probe_kernel_write((void *)ip, val, size);
  168. }
  169. static int add_break(unsigned long ip, const char *old)
  170. {
  171. unsigned char replaced[MCOUNT_INSN_SIZE];
  172. unsigned char brk = BREAKPOINT_INSTRUCTION;
  173. if (probe_kernel_read(replaced, (void *)ip, MCOUNT_INSN_SIZE))
  174. return -EFAULT;
  175. /* Make sure it is what we expect it to be */
  176. if (memcmp(replaced, old, MCOUNT_INSN_SIZE) != 0)
  177. return -EINVAL;
  178. if (ftrace_write(ip, &brk, 1))
  179. return -EPERM;
  180. return 0;
  181. }
  182. static int add_brk_on_call(struct dyn_ftrace *rec, unsigned long addr)
  183. {
  184. unsigned const char *old;
  185. unsigned long ip = rec->ip;
  186. old = ftrace_call_replace(ip, addr);
  187. return add_break(rec->ip, old);
  188. }
  189. static int add_brk_on_nop(struct dyn_ftrace *rec)
  190. {
  191. unsigned const char *old;
  192. old = ftrace_nop_replace();
  193. return add_break(rec->ip, old);
  194. }
  195. static int add_breakpoints(struct dyn_ftrace *rec, int enable)
  196. {
  197. unsigned long ftrace_addr;
  198. int ret;
  199. ret = ftrace_test_record(rec, enable);
  200. ftrace_addr = (unsigned long)FTRACE_ADDR;
  201. switch (ret) {
  202. case FTRACE_UPDATE_IGNORE:
  203. return 0;
  204. case FTRACE_UPDATE_MAKE_CALL:
  205. /* converting nop to call */
  206. return add_brk_on_nop(rec);
  207. case FTRACE_UPDATE_MAKE_NOP:
  208. /* converting a call to a nop */
  209. return add_brk_on_call(rec, ftrace_addr);
  210. }
  211. return 0;
  212. }
  213. /*
  214. * On error, we need to remove breakpoints. This needs to
  215. * be done caefully. If the address does not currently have a
  216. * breakpoint, we know we are done. Otherwise, we look at the
  217. * remaining 4 bytes of the instruction. If it matches a nop
  218. * we replace the breakpoint with the nop. Otherwise we replace
  219. * it with the call instruction.
  220. */
  221. static int remove_breakpoint(struct dyn_ftrace *rec)
  222. {
  223. unsigned char ins[MCOUNT_INSN_SIZE];
  224. unsigned char brk = BREAKPOINT_INSTRUCTION;
  225. const unsigned char *nop;
  226. unsigned long ftrace_addr;
  227. unsigned long ip = rec->ip;
  228. /* If we fail the read, just give up */
  229. if (probe_kernel_read(ins, (void *)ip, MCOUNT_INSN_SIZE))
  230. return -EFAULT;
  231. /* If this does not have a breakpoint, we are done */
  232. if (ins[0] != brk)
  233. return -1;
  234. nop = ftrace_nop_replace();
  235. /*
  236. * If the last 4 bytes of the instruction do not match
  237. * a nop, then we assume that this is a call to ftrace_addr.
  238. */
  239. if (memcmp(&ins[1], &nop[1], MCOUNT_INSN_SIZE - 1) != 0) {
  240. /*
  241. * For extra paranoidism, we check if the breakpoint is on
  242. * a call that would actually jump to the ftrace_addr.
  243. * If not, don't touch the breakpoint, we make just create
  244. * a disaster.
  245. */
  246. ftrace_addr = (unsigned long)FTRACE_ADDR;
  247. nop = ftrace_call_replace(ip, ftrace_addr);
  248. if (memcmp(&ins[1], &nop[1], MCOUNT_INSN_SIZE - 1) != 0)
  249. return -EINVAL;
  250. }
  251. return probe_kernel_write((void *)ip, &nop[0], 1);
  252. }
  253. static int add_update_code(unsigned long ip, unsigned const char *new)
  254. {
  255. /* skip breakpoint */
  256. ip++;
  257. new++;
  258. if (ftrace_write(ip, new, MCOUNT_INSN_SIZE - 1))
  259. return -EPERM;
  260. return 0;
  261. }
  262. static int add_update_call(struct dyn_ftrace *rec, unsigned long addr)
  263. {
  264. unsigned long ip = rec->ip;
  265. unsigned const char *new;
  266. new = ftrace_call_replace(ip, addr);
  267. return add_update_code(ip, new);
  268. }
  269. static int add_update_nop(struct dyn_ftrace *rec)
  270. {
  271. unsigned long ip = rec->ip;
  272. unsigned const char *new;
  273. new = ftrace_nop_replace();
  274. return add_update_code(ip, new);
  275. }
  276. static int add_update(struct dyn_ftrace *rec, int enable)
  277. {
  278. unsigned long ftrace_addr;
  279. int ret;
  280. ret = ftrace_test_record(rec, enable);
  281. ftrace_addr = (unsigned long)FTRACE_ADDR;
  282. switch (ret) {
  283. case FTRACE_UPDATE_IGNORE:
  284. return 0;
  285. case FTRACE_UPDATE_MAKE_CALL:
  286. /* converting nop to call */
  287. return add_update_call(rec, ftrace_addr);
  288. case FTRACE_UPDATE_MAKE_NOP:
  289. /* converting a call to a nop */
  290. return add_update_nop(rec);
  291. }
  292. return 0;
  293. }
  294. static int finish_update_call(struct dyn_ftrace *rec, unsigned long addr)
  295. {
  296. unsigned long ip = rec->ip;
  297. unsigned const char *new;
  298. new = ftrace_call_replace(ip, addr);
  299. if (ftrace_write(ip, new, 1))
  300. return -EPERM;
  301. return 0;
  302. }
  303. static int finish_update_nop(struct dyn_ftrace *rec)
  304. {
  305. unsigned long ip = rec->ip;
  306. unsigned const char *new;
  307. new = ftrace_nop_replace();
  308. if (ftrace_write(ip, new, 1))
  309. return -EPERM;
  310. return 0;
  311. }
  312. static int finish_update(struct dyn_ftrace *rec, int enable)
  313. {
  314. unsigned long ftrace_addr;
  315. int ret;
  316. ret = ftrace_update_record(rec, enable);
  317. ftrace_addr = (unsigned long)FTRACE_ADDR;
  318. switch (ret) {
  319. case FTRACE_UPDATE_IGNORE:
  320. return 0;
  321. case FTRACE_UPDATE_MAKE_CALL:
  322. /* converting nop to call */
  323. return finish_update_call(rec, ftrace_addr);
  324. case FTRACE_UPDATE_MAKE_NOP:
  325. /* converting a call to a nop */
  326. return finish_update_nop(rec);
  327. }
  328. return 0;
  329. }
  330. static void do_sync_core(void *data)
  331. {
  332. sync_core();
  333. }
  334. static void run_sync(void)
  335. {
  336. int enable_irqs = irqs_disabled();
  337. /* We may be called with interrupts disbled (on bootup). */
  338. if (enable_irqs)
  339. local_irq_enable();
  340. on_each_cpu(do_sync_core, NULL, 1);
  341. if (enable_irqs)
  342. local_irq_disable();
  343. }
  344. static void ftrace_replace_code(int enable)
  345. {
  346. struct ftrace_rec_iter *iter;
  347. struct dyn_ftrace *rec;
  348. const char *report = "adding breakpoints";
  349. int count = 0;
  350. int ret;
  351. for_ftrace_rec_iter(iter) {
  352. rec = ftrace_rec_iter_record(iter);
  353. ret = add_breakpoints(rec, enable);
  354. if (ret)
  355. goto remove_breakpoints;
  356. count++;
  357. }
  358. run_sync();
  359. report = "updating code";
  360. for_ftrace_rec_iter(iter) {
  361. rec = ftrace_rec_iter_record(iter);
  362. ret = add_update(rec, enable);
  363. if (ret)
  364. goto remove_breakpoints;
  365. }
  366. run_sync();
  367. report = "removing breakpoints";
  368. for_ftrace_rec_iter(iter) {
  369. rec = ftrace_rec_iter_record(iter);
  370. ret = finish_update(rec, enable);
  371. if (ret)
  372. goto remove_breakpoints;
  373. }
  374. run_sync();
  375. return;
  376. remove_breakpoints:
  377. ftrace_bug(ret, rec ? rec->ip : 0);
  378. printk(KERN_WARNING "Failed on %s (%d):\n", report, count);
  379. for_ftrace_rec_iter(iter) {
  380. rec = ftrace_rec_iter_record(iter);
  381. remove_breakpoint(rec);
  382. }
  383. }
  384. void arch_ftrace_update_code(int command)
  385. {
  386. modifying_ftrace_code++;
  387. if (command & FTRACE_UPDATE_CALLS)
  388. ftrace_replace_code(1);
  389. else if (command & FTRACE_DISABLE_CALLS)
  390. ftrace_replace_code(0);
  391. if (command & FTRACE_UPDATE_TRACE_FUNC)
  392. ftrace_update_ftrace_func(ftrace_trace_function);
  393. if (command & FTRACE_START_FUNC_RET)
  394. ftrace_enable_ftrace_graph_caller();
  395. else if (command & FTRACE_STOP_FUNC_RET)
  396. ftrace_disable_ftrace_graph_caller();
  397. modifying_ftrace_code--;
  398. }
  399. int __init ftrace_dyn_arch_init(void *data)
  400. {
  401. /* The return code is retured via data */
  402. *(unsigned long *)data = 0;
  403. return 0;
  404. }
  405. #endif
  406. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  407. #ifdef CONFIG_DYNAMIC_FTRACE
  408. extern void ftrace_graph_call(void);
  409. static int ftrace_mod_jmp(unsigned long ip,
  410. int old_offset, int new_offset)
  411. {
  412. unsigned char code[MCOUNT_INSN_SIZE];
  413. if (probe_kernel_read(code, (void *)ip, MCOUNT_INSN_SIZE))
  414. return -EFAULT;
  415. if (code[0] != 0xe9 || old_offset != *(int *)(&code[1]))
  416. return -EINVAL;
  417. *(int *)(&code[1]) = new_offset;
  418. if (do_ftrace_mod_code(ip, &code))
  419. return -EPERM;
  420. return 0;
  421. }
  422. int ftrace_enable_ftrace_graph_caller(void)
  423. {
  424. unsigned long ip = (unsigned long)(&ftrace_graph_call);
  425. int old_offset, new_offset;
  426. old_offset = (unsigned long)(&ftrace_stub) - (ip + MCOUNT_INSN_SIZE);
  427. new_offset = (unsigned long)(&ftrace_graph_caller) - (ip + MCOUNT_INSN_SIZE);
  428. return ftrace_mod_jmp(ip, old_offset, new_offset);
  429. }
  430. int ftrace_disable_ftrace_graph_caller(void)
  431. {
  432. unsigned long ip = (unsigned long)(&ftrace_graph_call);
  433. int old_offset, new_offset;
  434. old_offset = (unsigned long)(&ftrace_graph_caller) - (ip + MCOUNT_INSN_SIZE);
  435. new_offset = (unsigned long)(&ftrace_stub) - (ip + MCOUNT_INSN_SIZE);
  436. return ftrace_mod_jmp(ip, old_offset, new_offset);
  437. }
  438. #endif /* !CONFIG_DYNAMIC_FTRACE */
  439. /*
  440. * Hook the return address and push it in the stack of return addrs
  441. * in current thread info.
  442. */
  443. void prepare_ftrace_return(unsigned long *parent, unsigned long self_addr,
  444. unsigned long frame_pointer)
  445. {
  446. unsigned long old;
  447. int faulted;
  448. struct ftrace_graph_ent trace;
  449. unsigned long return_hooker = (unsigned long)
  450. &return_to_handler;
  451. if (unlikely(atomic_read(&current->tracing_graph_pause)))
  452. return;
  453. /*
  454. * Protect against fault, even if it shouldn't
  455. * happen. This tool is too much intrusive to
  456. * ignore such a protection.
  457. */
  458. asm volatile(
  459. "1: " _ASM_MOV " (%[parent]), %[old]\n"
  460. "2: " _ASM_MOV " %[return_hooker], (%[parent])\n"
  461. " movl $0, %[faulted]\n"
  462. "3:\n"
  463. ".section .fixup, \"ax\"\n"
  464. "4: movl $1, %[faulted]\n"
  465. " jmp 3b\n"
  466. ".previous\n"
  467. _ASM_EXTABLE(1b, 4b)
  468. _ASM_EXTABLE(2b, 4b)
  469. : [old] "=&r" (old), [faulted] "=r" (faulted)
  470. : [parent] "r" (parent), [return_hooker] "r" (return_hooker)
  471. : "memory"
  472. );
  473. if (unlikely(faulted)) {
  474. ftrace_graph_stop();
  475. WARN_ON(1);
  476. return;
  477. }
  478. trace.func = self_addr;
  479. trace.depth = current->curr_ret_stack + 1;
  480. /* Only trace if the calling function expects to */
  481. if (!ftrace_graph_entry(&trace)) {
  482. *parent = old;
  483. return;
  484. }
  485. if (ftrace_push_return_trace(old, self_addr, &trace.depth,
  486. frame_pointer) == -EBUSY) {
  487. *parent = old;
  488. return;
  489. }
  490. }
  491. #endif /* CONFIG_FUNCTION_GRAPH_TRACER */