ftrace.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772
  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_direct(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. /*
  119. * On boot up, and when modules are loaded, the MCOUNT_ADDR
  120. * is converted to a nop, and will never become MCOUNT_ADDR
  121. * again. This code is either running before SMP (on boot up)
  122. * or before the code will ever be executed (module load).
  123. * We do not want to use the breakpoint version in this case,
  124. * just modify the code directly.
  125. */
  126. if (addr == MCOUNT_ADDR)
  127. return ftrace_modify_code_direct(rec->ip, old, new);
  128. /* Normal cases use add_brk_on_nop */
  129. WARN_ONCE(1, "invalid use of ftrace_make_nop");
  130. return -EINVAL;
  131. }
  132. int ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr)
  133. {
  134. unsigned const char *new, *old;
  135. unsigned long ip = rec->ip;
  136. old = ftrace_nop_replace();
  137. new = ftrace_call_replace(ip, addr);
  138. /* Should only be called when module is loaded */
  139. return ftrace_modify_code_direct(rec->ip, old, new);
  140. }
  141. /*
  142. * The modifying_ftrace_code is used to tell the breakpoint
  143. * handler to call ftrace_int3_handler(). If it fails to
  144. * call this handler for a breakpoint added by ftrace, then
  145. * the kernel may crash.
  146. *
  147. * As atomic_writes on x86 do not need a barrier, we do not
  148. * need to add smp_mb()s for this to work. It is also considered
  149. * that we can not read the modifying_ftrace_code before
  150. * executing the breakpoint. That would be quite remarkable if
  151. * it could do that. Here's the flow that is required:
  152. *
  153. * CPU-0 CPU-1
  154. *
  155. * atomic_inc(mfc);
  156. * write int3s
  157. * <trap-int3> // implicit (r)mb
  158. * if (atomic_read(mfc))
  159. * call ftrace_int3_handler()
  160. *
  161. * Then when we are finished:
  162. *
  163. * atomic_dec(mfc);
  164. *
  165. * If we hit a breakpoint that was not set by ftrace, it does not
  166. * matter if ftrace_int3_handler() is called or not. It will
  167. * simply be ignored. But it is crucial that a ftrace nop/caller
  168. * breakpoint is handled. No other user should ever place a
  169. * breakpoint on an ftrace nop/caller location. It must only
  170. * be done by this code.
  171. */
  172. atomic_t modifying_ftrace_code __read_mostly;
  173. static int
  174. ftrace_modify_code(unsigned long ip, unsigned const char *old_code,
  175. unsigned const char *new_code);
  176. /*
  177. * Should never be called:
  178. * As it is only called by __ftrace_replace_code() which is called by
  179. * ftrace_replace_code() that x86 overrides, and by ftrace_update_code()
  180. * which is called to turn mcount into nops or nops into function calls
  181. * but not to convert a function from not using regs to one that uses
  182. * regs, which ftrace_modify_call() is for.
  183. */
  184. int ftrace_modify_call(struct dyn_ftrace *rec, unsigned long old_addr,
  185. unsigned long addr)
  186. {
  187. WARN_ON(1);
  188. return -EINVAL;
  189. }
  190. int ftrace_update_ftrace_func(ftrace_func_t func)
  191. {
  192. unsigned long ip = (unsigned long)(&ftrace_call);
  193. unsigned char old[MCOUNT_INSN_SIZE], *new;
  194. int ret;
  195. memcpy(old, &ftrace_call, MCOUNT_INSN_SIZE);
  196. new = ftrace_call_replace(ip, (unsigned long)func);
  197. /* See comment above by declaration of modifying_ftrace_code */
  198. atomic_inc(&modifying_ftrace_code);
  199. ret = ftrace_modify_code(ip, old, new);
  200. /* Also update the regs callback function */
  201. if (!ret) {
  202. ip = (unsigned long)(&ftrace_regs_call);
  203. memcpy(old, &ftrace_regs_call, MCOUNT_INSN_SIZE);
  204. new = ftrace_call_replace(ip, (unsigned long)func);
  205. ret = ftrace_modify_code(ip, old, new);
  206. }
  207. atomic_dec(&modifying_ftrace_code);
  208. return ret;
  209. }
  210. /*
  211. * A breakpoint was added to the code address we are about to
  212. * modify, and this is the handle that will just skip over it.
  213. * We are either changing a nop into a trace call, or a trace
  214. * call to a nop. While the change is taking place, we treat
  215. * it just like it was a nop.
  216. */
  217. int ftrace_int3_handler(struct pt_regs *regs)
  218. {
  219. if (WARN_ON_ONCE(!regs))
  220. return 0;
  221. if (!ftrace_location(regs->ip - 1))
  222. return 0;
  223. regs->ip += MCOUNT_INSN_SIZE - 1;
  224. return 1;
  225. }
  226. static int ftrace_write(unsigned long ip, const char *val, int size)
  227. {
  228. /*
  229. * On x86_64, kernel text mappings are mapped read-only with
  230. * CONFIG_DEBUG_RODATA. So we use the kernel identity mapping instead
  231. * of the kernel text mapping to modify the kernel text.
  232. *
  233. * For 32bit kernels, these mappings are same and we can use
  234. * kernel identity mapping to modify code.
  235. */
  236. if (within(ip, (unsigned long)_text, (unsigned long)_etext))
  237. ip = (unsigned long)__va(__pa(ip));
  238. return probe_kernel_write((void *)ip, val, size);
  239. }
  240. static int add_break(unsigned long ip, const char *old)
  241. {
  242. unsigned char replaced[MCOUNT_INSN_SIZE];
  243. unsigned char brk = BREAKPOINT_INSTRUCTION;
  244. if (probe_kernel_read(replaced, (void *)ip, MCOUNT_INSN_SIZE))
  245. return -EFAULT;
  246. /* Make sure it is what we expect it to be */
  247. if (memcmp(replaced, old, MCOUNT_INSN_SIZE) != 0)
  248. return -EINVAL;
  249. if (ftrace_write(ip, &brk, 1))
  250. return -EPERM;
  251. return 0;
  252. }
  253. static int add_brk_on_call(struct dyn_ftrace *rec, unsigned long addr)
  254. {
  255. unsigned const char *old;
  256. unsigned long ip = rec->ip;
  257. old = ftrace_call_replace(ip, addr);
  258. return add_break(rec->ip, old);
  259. }
  260. static int add_brk_on_nop(struct dyn_ftrace *rec)
  261. {
  262. unsigned const char *old;
  263. old = ftrace_nop_replace();
  264. return add_break(rec->ip, old);
  265. }
  266. /*
  267. * If the record has the FTRACE_FL_REGS set, that means that it
  268. * wants to convert to a callback that saves all regs. If FTRACE_FL_REGS
  269. * is not not set, then it wants to convert to the normal callback.
  270. */
  271. static unsigned long get_ftrace_addr(struct dyn_ftrace *rec)
  272. {
  273. if (rec->flags & FTRACE_FL_REGS)
  274. return (unsigned long)FTRACE_REGS_ADDR;
  275. else
  276. return (unsigned long)FTRACE_ADDR;
  277. }
  278. /*
  279. * The FTRACE_FL_REGS_EN is set when the record already points to
  280. * a function that saves all the regs. Basically the '_EN' version
  281. * represents the current state of the function.
  282. */
  283. static unsigned long get_ftrace_old_addr(struct dyn_ftrace *rec)
  284. {
  285. if (rec->flags & FTRACE_FL_REGS_EN)
  286. return (unsigned long)FTRACE_REGS_ADDR;
  287. else
  288. return (unsigned long)FTRACE_ADDR;
  289. }
  290. static int add_breakpoints(struct dyn_ftrace *rec, int enable)
  291. {
  292. unsigned long ftrace_addr;
  293. int ret;
  294. ret = ftrace_test_record(rec, enable);
  295. ftrace_addr = get_ftrace_addr(rec);
  296. switch (ret) {
  297. case FTRACE_UPDATE_IGNORE:
  298. return 0;
  299. case FTRACE_UPDATE_MAKE_CALL:
  300. /* converting nop to call */
  301. return add_brk_on_nop(rec);
  302. case FTRACE_UPDATE_MODIFY_CALL_REGS:
  303. case FTRACE_UPDATE_MODIFY_CALL:
  304. ftrace_addr = get_ftrace_old_addr(rec);
  305. /* fall through */
  306. case FTRACE_UPDATE_MAKE_NOP:
  307. /* converting a call to a nop */
  308. return add_brk_on_call(rec, ftrace_addr);
  309. }
  310. return 0;
  311. }
  312. /*
  313. * On error, we need to remove breakpoints. This needs to
  314. * be done caefully. If the address does not currently have a
  315. * breakpoint, we know we are done. Otherwise, we look at the
  316. * remaining 4 bytes of the instruction. If it matches a nop
  317. * we replace the breakpoint with the nop. Otherwise we replace
  318. * it with the call instruction.
  319. */
  320. static int remove_breakpoint(struct dyn_ftrace *rec)
  321. {
  322. unsigned char ins[MCOUNT_INSN_SIZE];
  323. unsigned char brk = BREAKPOINT_INSTRUCTION;
  324. const unsigned char *nop;
  325. unsigned long ftrace_addr;
  326. unsigned long ip = rec->ip;
  327. /* If we fail the read, just give up */
  328. if (probe_kernel_read(ins, (void *)ip, MCOUNT_INSN_SIZE))
  329. return -EFAULT;
  330. /* If this does not have a breakpoint, we are done */
  331. if (ins[0] != brk)
  332. return -1;
  333. nop = ftrace_nop_replace();
  334. /*
  335. * If the last 4 bytes of the instruction do not match
  336. * a nop, then we assume that this is a call to ftrace_addr.
  337. */
  338. if (memcmp(&ins[1], &nop[1], MCOUNT_INSN_SIZE - 1) != 0) {
  339. /*
  340. * For extra paranoidism, we check if the breakpoint is on
  341. * a call that would actually jump to the ftrace_addr.
  342. * If not, don't touch the breakpoint, we make just create
  343. * a disaster.
  344. */
  345. ftrace_addr = get_ftrace_addr(rec);
  346. nop = ftrace_call_replace(ip, ftrace_addr);
  347. if (memcmp(&ins[1], &nop[1], MCOUNT_INSN_SIZE - 1) == 0)
  348. goto update;
  349. /* Check both ftrace_addr and ftrace_old_addr */
  350. ftrace_addr = get_ftrace_old_addr(rec);
  351. nop = ftrace_call_replace(ip, ftrace_addr);
  352. if (memcmp(&ins[1], &nop[1], MCOUNT_INSN_SIZE - 1) != 0)
  353. return -EINVAL;
  354. }
  355. update:
  356. return probe_kernel_write((void *)ip, &nop[0], 1);
  357. }
  358. static int add_update_code(unsigned long ip, unsigned const char *new)
  359. {
  360. /* skip breakpoint */
  361. ip++;
  362. new++;
  363. if (ftrace_write(ip, new, MCOUNT_INSN_SIZE - 1))
  364. return -EPERM;
  365. return 0;
  366. }
  367. static int add_update_call(struct dyn_ftrace *rec, unsigned long addr)
  368. {
  369. unsigned long ip = rec->ip;
  370. unsigned const char *new;
  371. new = ftrace_call_replace(ip, addr);
  372. return add_update_code(ip, new);
  373. }
  374. static int add_update_nop(struct dyn_ftrace *rec)
  375. {
  376. unsigned long ip = rec->ip;
  377. unsigned const char *new;
  378. new = ftrace_nop_replace();
  379. return add_update_code(ip, new);
  380. }
  381. static int add_update(struct dyn_ftrace *rec, int enable)
  382. {
  383. unsigned long ftrace_addr;
  384. int ret;
  385. ret = ftrace_test_record(rec, enable);
  386. ftrace_addr = get_ftrace_addr(rec);
  387. switch (ret) {
  388. case FTRACE_UPDATE_IGNORE:
  389. return 0;
  390. case FTRACE_UPDATE_MODIFY_CALL_REGS:
  391. case FTRACE_UPDATE_MODIFY_CALL:
  392. case FTRACE_UPDATE_MAKE_CALL:
  393. /* converting nop to call */
  394. return add_update_call(rec, ftrace_addr);
  395. case FTRACE_UPDATE_MAKE_NOP:
  396. /* converting a call to a nop */
  397. return add_update_nop(rec);
  398. }
  399. return 0;
  400. }
  401. static int finish_update_call(struct dyn_ftrace *rec, unsigned long addr)
  402. {
  403. unsigned long ip = rec->ip;
  404. unsigned const char *new;
  405. new = ftrace_call_replace(ip, addr);
  406. if (ftrace_write(ip, new, 1))
  407. return -EPERM;
  408. return 0;
  409. }
  410. static int finish_update_nop(struct dyn_ftrace *rec)
  411. {
  412. unsigned long ip = rec->ip;
  413. unsigned const char *new;
  414. new = ftrace_nop_replace();
  415. if (ftrace_write(ip, new, 1))
  416. return -EPERM;
  417. return 0;
  418. }
  419. static int finish_update(struct dyn_ftrace *rec, int enable)
  420. {
  421. unsigned long ftrace_addr;
  422. int ret;
  423. ret = ftrace_update_record(rec, enable);
  424. ftrace_addr = get_ftrace_addr(rec);
  425. switch (ret) {
  426. case FTRACE_UPDATE_IGNORE:
  427. return 0;
  428. case FTRACE_UPDATE_MODIFY_CALL_REGS:
  429. case FTRACE_UPDATE_MODIFY_CALL:
  430. case FTRACE_UPDATE_MAKE_CALL:
  431. /* converting nop to call */
  432. return finish_update_call(rec, ftrace_addr);
  433. case FTRACE_UPDATE_MAKE_NOP:
  434. /* converting a call to a nop */
  435. return finish_update_nop(rec);
  436. }
  437. return 0;
  438. }
  439. static void do_sync_core(void *data)
  440. {
  441. sync_core();
  442. }
  443. static void run_sync(void)
  444. {
  445. int enable_irqs = irqs_disabled();
  446. /* We may be called with interrupts disbled (on bootup). */
  447. if (enable_irqs)
  448. local_irq_enable();
  449. on_each_cpu(do_sync_core, NULL, 1);
  450. if (enable_irqs)
  451. local_irq_disable();
  452. }
  453. void ftrace_replace_code(int enable)
  454. {
  455. struct ftrace_rec_iter *iter;
  456. struct dyn_ftrace *rec;
  457. const char *report = "adding breakpoints";
  458. int count = 0;
  459. int ret;
  460. for_ftrace_rec_iter(iter) {
  461. rec = ftrace_rec_iter_record(iter);
  462. ret = add_breakpoints(rec, enable);
  463. if (ret)
  464. goto remove_breakpoints;
  465. count++;
  466. }
  467. run_sync();
  468. report = "updating code";
  469. for_ftrace_rec_iter(iter) {
  470. rec = ftrace_rec_iter_record(iter);
  471. ret = add_update(rec, enable);
  472. if (ret)
  473. goto remove_breakpoints;
  474. }
  475. run_sync();
  476. report = "removing breakpoints";
  477. for_ftrace_rec_iter(iter) {
  478. rec = ftrace_rec_iter_record(iter);
  479. ret = finish_update(rec, enable);
  480. if (ret)
  481. goto remove_breakpoints;
  482. }
  483. run_sync();
  484. return;
  485. remove_breakpoints:
  486. ftrace_bug(ret, rec ? rec->ip : 0);
  487. printk(KERN_WARNING "Failed on %s (%d):\n", report, count);
  488. for_ftrace_rec_iter(iter) {
  489. rec = ftrace_rec_iter_record(iter);
  490. remove_breakpoint(rec);
  491. }
  492. }
  493. static int
  494. ftrace_modify_code(unsigned long ip, unsigned const char *old_code,
  495. unsigned const char *new_code)
  496. {
  497. int ret;
  498. ret = add_break(ip, old_code);
  499. if (ret)
  500. goto out;
  501. run_sync();
  502. ret = add_update_code(ip, new_code);
  503. if (ret)
  504. goto fail_update;
  505. run_sync();
  506. ret = ftrace_write(ip, new_code, 1);
  507. if (ret) {
  508. ret = -EPERM;
  509. goto out;
  510. }
  511. run_sync();
  512. out:
  513. return ret;
  514. fail_update:
  515. probe_kernel_write((void *)ip, &old_code[0], 1);
  516. goto out;
  517. }
  518. void arch_ftrace_update_code(int command)
  519. {
  520. /* See comment above by declaration of modifying_ftrace_code */
  521. atomic_inc(&modifying_ftrace_code);
  522. ftrace_modify_all_code(command);
  523. atomic_dec(&modifying_ftrace_code);
  524. }
  525. int __init ftrace_dyn_arch_init(void *data)
  526. {
  527. /* The return code is retured via data */
  528. *(unsigned long *)data = 0;
  529. return 0;
  530. }
  531. #endif
  532. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  533. #ifdef CONFIG_DYNAMIC_FTRACE
  534. extern void ftrace_graph_call(void);
  535. static int ftrace_mod_jmp(unsigned long ip,
  536. int old_offset, int new_offset)
  537. {
  538. unsigned char code[MCOUNT_INSN_SIZE];
  539. if (probe_kernel_read(code, (void *)ip, MCOUNT_INSN_SIZE))
  540. return -EFAULT;
  541. if (code[0] != 0xe9 || old_offset != *(int *)(&code[1]))
  542. return -EINVAL;
  543. *(int *)(&code[1]) = new_offset;
  544. if (do_ftrace_mod_code(ip, &code))
  545. return -EPERM;
  546. return 0;
  547. }
  548. int ftrace_enable_ftrace_graph_caller(void)
  549. {
  550. unsigned long ip = (unsigned long)(&ftrace_graph_call);
  551. int old_offset, new_offset;
  552. old_offset = (unsigned long)(&ftrace_stub) - (ip + MCOUNT_INSN_SIZE);
  553. new_offset = (unsigned long)(&ftrace_graph_caller) - (ip + MCOUNT_INSN_SIZE);
  554. return ftrace_mod_jmp(ip, old_offset, new_offset);
  555. }
  556. int ftrace_disable_ftrace_graph_caller(void)
  557. {
  558. unsigned long ip = (unsigned long)(&ftrace_graph_call);
  559. int old_offset, new_offset;
  560. old_offset = (unsigned long)(&ftrace_graph_caller) - (ip + MCOUNT_INSN_SIZE);
  561. new_offset = (unsigned long)(&ftrace_stub) - (ip + MCOUNT_INSN_SIZE);
  562. return ftrace_mod_jmp(ip, old_offset, new_offset);
  563. }
  564. #endif /* !CONFIG_DYNAMIC_FTRACE */
  565. /*
  566. * Hook the return address and push it in the stack of return addrs
  567. * in current thread info.
  568. */
  569. void prepare_ftrace_return(unsigned long *parent, unsigned long self_addr,
  570. unsigned long frame_pointer)
  571. {
  572. unsigned long old;
  573. int faulted;
  574. struct ftrace_graph_ent trace;
  575. unsigned long return_hooker = (unsigned long)
  576. &return_to_handler;
  577. if (unlikely(atomic_read(&current->tracing_graph_pause)))
  578. return;
  579. /*
  580. * Protect against fault, even if it shouldn't
  581. * happen. This tool is too much intrusive to
  582. * ignore such a protection.
  583. */
  584. asm volatile(
  585. "1: " _ASM_MOV " (%[parent]), %[old]\n"
  586. "2: " _ASM_MOV " %[return_hooker], (%[parent])\n"
  587. " movl $0, %[faulted]\n"
  588. "3:\n"
  589. ".section .fixup, \"ax\"\n"
  590. "4: movl $1, %[faulted]\n"
  591. " jmp 3b\n"
  592. ".previous\n"
  593. _ASM_EXTABLE(1b, 4b)
  594. _ASM_EXTABLE(2b, 4b)
  595. : [old] "=&r" (old), [faulted] "=r" (faulted)
  596. : [parent] "r" (parent), [return_hooker] "r" (return_hooker)
  597. : "memory"
  598. );
  599. if (unlikely(faulted)) {
  600. ftrace_graph_stop();
  601. WARN_ON(1);
  602. return;
  603. }
  604. trace.func = self_addr;
  605. trace.depth = current->curr_ret_stack + 1;
  606. /* Only trace if the calling function expects to */
  607. if (!ftrace_graph_entry(&trace)) {
  608. *parent = old;
  609. return;
  610. }
  611. if (ftrace_push_return_trace(old, self_addr, &trace.depth,
  612. frame_pointer) == -EBUSY) {
  613. *parent = old;
  614. return;
  615. }
  616. }
  617. #endif /* CONFIG_FUNCTION_GRAPH_TRACER */