ftrace.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794
  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 unsigned long text_ip_addr(unsigned long ip)
  67. {
  68. /*
  69. * On x86_64, kernel text mappings are mapped read-only with
  70. * CONFIG_DEBUG_RODATA. So we use the kernel identity mapping instead
  71. * of the kernel text mapping to modify the kernel text.
  72. *
  73. * For 32bit kernels, these mappings are same and we can use
  74. * kernel identity mapping to modify code.
  75. */
  76. if (within(ip, (unsigned long)_text, (unsigned long)_etext))
  77. ip = (unsigned long)__va(__pa_symbol(ip));
  78. return ip;
  79. }
  80. static const unsigned char *ftrace_nop_replace(void)
  81. {
  82. return ideal_nops[NOP_ATOMIC5];
  83. }
  84. static int
  85. ftrace_modify_code_direct(unsigned long ip, unsigned const char *old_code,
  86. unsigned const char *new_code)
  87. {
  88. unsigned char replaced[MCOUNT_INSN_SIZE];
  89. /*
  90. * Note: Due to modules and __init, code can
  91. * disappear and change, we need to protect against faulting
  92. * as well as code changing. We do this by using the
  93. * probe_kernel_* functions.
  94. *
  95. * No real locking needed, this code is run through
  96. * kstop_machine, or before SMP starts.
  97. */
  98. /* read the text we want to modify */
  99. if (probe_kernel_read(replaced, (void *)ip, MCOUNT_INSN_SIZE))
  100. return -EFAULT;
  101. /* Make sure it is what we expect it to be */
  102. if (memcmp(replaced, old_code, MCOUNT_INSN_SIZE) != 0)
  103. return -EINVAL;
  104. ip = text_ip_addr(ip);
  105. /* replace the text with the new text */
  106. if (probe_kernel_write((void *)ip, new_code, MCOUNT_INSN_SIZE))
  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. static unsigned long ftrace_update_func;
  191. static int update_ftrace_func(unsigned long ip, void *new)
  192. {
  193. unsigned char old[MCOUNT_INSN_SIZE];
  194. int ret;
  195. memcpy(old, (void *)ip, MCOUNT_INSN_SIZE);
  196. ftrace_update_func = ip;
  197. /* Make sure the breakpoints see the ftrace_update_func update */
  198. smp_wmb();
  199. /* See comment above by declaration of modifying_ftrace_code */
  200. atomic_inc(&modifying_ftrace_code);
  201. ret = ftrace_modify_code(ip, old, new);
  202. atomic_dec(&modifying_ftrace_code);
  203. return ret;
  204. }
  205. int ftrace_update_ftrace_func(ftrace_func_t func)
  206. {
  207. unsigned long ip = (unsigned long)(&ftrace_call);
  208. unsigned char *new;
  209. int ret;
  210. new = ftrace_call_replace(ip, (unsigned long)func);
  211. ret = update_ftrace_func(ip, new);
  212. /* Also update the regs callback function */
  213. if (!ret) {
  214. ip = (unsigned long)(&ftrace_regs_call);
  215. new = ftrace_call_replace(ip, (unsigned long)func);
  216. ret = update_ftrace_func(ip, new);
  217. }
  218. return ret;
  219. }
  220. static int is_ftrace_caller(unsigned long ip)
  221. {
  222. if (ip == ftrace_update_func)
  223. return 1;
  224. return 0;
  225. }
  226. /*
  227. * A breakpoint was added to the code address we are about to
  228. * modify, and this is the handle that will just skip over it.
  229. * We are either changing a nop into a trace call, or a trace
  230. * call to a nop. While the change is taking place, we treat
  231. * it just like it was a nop.
  232. */
  233. int ftrace_int3_handler(struct pt_regs *regs)
  234. {
  235. unsigned long ip;
  236. if (WARN_ON_ONCE(!regs))
  237. return 0;
  238. ip = regs->ip - 1;
  239. if (!ftrace_location(ip) && !is_ftrace_caller(ip))
  240. return 0;
  241. regs->ip += MCOUNT_INSN_SIZE - 1;
  242. return 1;
  243. }
  244. static int ftrace_write(unsigned long ip, const char *val, int size)
  245. {
  246. /*
  247. * On x86_64, kernel text mappings are mapped read-only with
  248. * CONFIG_DEBUG_RODATA. So we use the kernel identity mapping instead
  249. * of the kernel text mapping to modify the kernel text.
  250. *
  251. * For 32bit kernels, these mappings are same and we can use
  252. * kernel identity mapping to modify code.
  253. */
  254. if (within(ip, (unsigned long)_text, (unsigned long)_etext))
  255. ip = (unsigned long)__va(__pa_symbol(ip));
  256. if (probe_kernel_write((void *)ip, val, size))
  257. return -EPERM;
  258. return 0;
  259. }
  260. static int add_break(unsigned long ip, const char *old)
  261. {
  262. unsigned char replaced[MCOUNT_INSN_SIZE];
  263. unsigned char brk = BREAKPOINT_INSTRUCTION;
  264. if (probe_kernel_read(replaced, (void *)ip, MCOUNT_INSN_SIZE))
  265. return -EFAULT;
  266. /* Make sure it is what we expect it to be */
  267. if (memcmp(replaced, old, MCOUNT_INSN_SIZE) != 0)
  268. return -EINVAL;
  269. return ftrace_write(ip, &brk, 1);
  270. }
  271. static int add_brk_on_call(struct dyn_ftrace *rec, unsigned long addr)
  272. {
  273. unsigned const char *old;
  274. unsigned long ip = rec->ip;
  275. old = ftrace_call_replace(ip, addr);
  276. return add_break(rec->ip, old);
  277. }
  278. static int add_brk_on_nop(struct dyn_ftrace *rec)
  279. {
  280. unsigned const char *old;
  281. old = ftrace_nop_replace();
  282. return add_break(rec->ip, old);
  283. }
  284. /*
  285. * If the record has the FTRACE_FL_REGS set, that means that it
  286. * wants to convert to a callback that saves all regs. If FTRACE_FL_REGS
  287. * is not not set, then it wants to convert to the normal callback.
  288. */
  289. static unsigned long get_ftrace_addr(struct dyn_ftrace *rec)
  290. {
  291. if (rec->flags & FTRACE_FL_REGS)
  292. return (unsigned long)FTRACE_REGS_ADDR;
  293. else
  294. return (unsigned long)FTRACE_ADDR;
  295. }
  296. /*
  297. * The FTRACE_FL_REGS_EN is set when the record already points to
  298. * a function that saves all the regs. Basically the '_EN' version
  299. * represents the current state of the function.
  300. */
  301. static unsigned long get_ftrace_old_addr(struct dyn_ftrace *rec)
  302. {
  303. if (rec->flags & FTRACE_FL_REGS_EN)
  304. return (unsigned long)FTRACE_REGS_ADDR;
  305. else
  306. return (unsigned long)FTRACE_ADDR;
  307. }
  308. static int add_breakpoints(struct dyn_ftrace *rec, int enable)
  309. {
  310. unsigned long ftrace_addr;
  311. int ret;
  312. ret = ftrace_test_record(rec, enable);
  313. ftrace_addr = get_ftrace_addr(rec);
  314. switch (ret) {
  315. case FTRACE_UPDATE_IGNORE:
  316. return 0;
  317. case FTRACE_UPDATE_MAKE_CALL:
  318. /* converting nop to call */
  319. return add_brk_on_nop(rec);
  320. case FTRACE_UPDATE_MODIFY_CALL_REGS:
  321. case FTRACE_UPDATE_MODIFY_CALL:
  322. ftrace_addr = get_ftrace_old_addr(rec);
  323. /* fall through */
  324. case FTRACE_UPDATE_MAKE_NOP:
  325. /* converting a call to a nop */
  326. return add_brk_on_call(rec, ftrace_addr);
  327. }
  328. return 0;
  329. }
  330. /*
  331. * On error, we need to remove breakpoints. This needs to
  332. * be done caefully. If the address does not currently have a
  333. * breakpoint, we know we are done. Otherwise, we look at the
  334. * remaining 4 bytes of the instruction. If it matches a nop
  335. * we replace the breakpoint with the nop. Otherwise we replace
  336. * it with the call instruction.
  337. */
  338. static int remove_breakpoint(struct dyn_ftrace *rec)
  339. {
  340. unsigned char ins[MCOUNT_INSN_SIZE];
  341. unsigned char brk = BREAKPOINT_INSTRUCTION;
  342. const unsigned char *nop;
  343. unsigned long ftrace_addr;
  344. unsigned long ip = rec->ip;
  345. /* If we fail the read, just give up */
  346. if (probe_kernel_read(ins, (void *)ip, MCOUNT_INSN_SIZE))
  347. return -EFAULT;
  348. /* If this does not have a breakpoint, we are done */
  349. if (ins[0] != brk)
  350. return 0;
  351. nop = ftrace_nop_replace();
  352. /*
  353. * If the last 4 bytes of the instruction do not match
  354. * a nop, then we assume that this is a call to ftrace_addr.
  355. */
  356. if (memcmp(&ins[1], &nop[1], MCOUNT_INSN_SIZE - 1) != 0) {
  357. /*
  358. * For extra paranoidism, we check if the breakpoint is on
  359. * a call that would actually jump to the ftrace_addr.
  360. * If not, don't touch the breakpoint, we make just create
  361. * a disaster.
  362. */
  363. ftrace_addr = get_ftrace_addr(rec);
  364. nop = ftrace_call_replace(ip, ftrace_addr);
  365. if (memcmp(&ins[1], &nop[1], MCOUNT_INSN_SIZE - 1) == 0)
  366. goto update;
  367. /* Check both ftrace_addr and ftrace_old_addr */
  368. ftrace_addr = get_ftrace_old_addr(rec);
  369. nop = ftrace_call_replace(ip, ftrace_addr);
  370. if (memcmp(&ins[1], &nop[1], MCOUNT_INSN_SIZE - 1) != 0)
  371. return -EINVAL;
  372. }
  373. update:
  374. return ftrace_write(ip, nop, 1);
  375. }
  376. static int add_update_code(unsigned long ip, unsigned const char *new)
  377. {
  378. /* skip breakpoint */
  379. ip++;
  380. new++;
  381. return ftrace_write(ip, new, MCOUNT_INSN_SIZE - 1);
  382. }
  383. static int add_update_call(struct dyn_ftrace *rec, unsigned long addr)
  384. {
  385. unsigned long ip = rec->ip;
  386. unsigned const char *new;
  387. new = ftrace_call_replace(ip, addr);
  388. return add_update_code(ip, new);
  389. }
  390. static int add_update_nop(struct dyn_ftrace *rec)
  391. {
  392. unsigned long ip = rec->ip;
  393. unsigned const char *new;
  394. new = ftrace_nop_replace();
  395. return add_update_code(ip, new);
  396. }
  397. static int add_update(struct dyn_ftrace *rec, int enable)
  398. {
  399. unsigned long ftrace_addr;
  400. int ret;
  401. ret = ftrace_test_record(rec, enable);
  402. ftrace_addr = get_ftrace_addr(rec);
  403. switch (ret) {
  404. case FTRACE_UPDATE_IGNORE:
  405. return 0;
  406. case FTRACE_UPDATE_MODIFY_CALL_REGS:
  407. case FTRACE_UPDATE_MODIFY_CALL:
  408. case FTRACE_UPDATE_MAKE_CALL:
  409. /* converting nop to call */
  410. return add_update_call(rec, ftrace_addr);
  411. case FTRACE_UPDATE_MAKE_NOP:
  412. /* converting a call to a nop */
  413. return add_update_nop(rec);
  414. }
  415. return 0;
  416. }
  417. static int finish_update_call(struct dyn_ftrace *rec, unsigned long addr)
  418. {
  419. unsigned long ip = rec->ip;
  420. unsigned const char *new;
  421. new = ftrace_call_replace(ip, addr);
  422. return ftrace_write(ip, new, 1);
  423. }
  424. static int finish_update_nop(struct dyn_ftrace *rec)
  425. {
  426. unsigned long ip = rec->ip;
  427. unsigned const char *new;
  428. new = ftrace_nop_replace();
  429. return ftrace_write(ip, new, 1);
  430. }
  431. static int finish_update(struct dyn_ftrace *rec, int enable)
  432. {
  433. unsigned long ftrace_addr;
  434. int ret;
  435. ret = ftrace_update_record(rec, enable);
  436. ftrace_addr = get_ftrace_addr(rec);
  437. switch (ret) {
  438. case FTRACE_UPDATE_IGNORE:
  439. return 0;
  440. case FTRACE_UPDATE_MODIFY_CALL_REGS:
  441. case FTRACE_UPDATE_MODIFY_CALL:
  442. case FTRACE_UPDATE_MAKE_CALL:
  443. /* converting nop to call */
  444. return finish_update_call(rec, ftrace_addr);
  445. case FTRACE_UPDATE_MAKE_NOP:
  446. /* converting a call to a nop */
  447. return finish_update_nop(rec);
  448. }
  449. return 0;
  450. }
  451. static void do_sync_core(void *data)
  452. {
  453. sync_core();
  454. }
  455. static void run_sync(void)
  456. {
  457. int enable_irqs = irqs_disabled();
  458. /* We may be called with interrupts disbled (on bootup). */
  459. if (enable_irqs)
  460. local_irq_enable();
  461. on_each_cpu(do_sync_core, NULL, 1);
  462. if (enable_irqs)
  463. local_irq_disable();
  464. }
  465. void ftrace_replace_code(int enable)
  466. {
  467. struct ftrace_rec_iter *iter;
  468. struct dyn_ftrace *rec;
  469. const char *report = "adding breakpoints";
  470. int count = 0;
  471. int ret;
  472. for_ftrace_rec_iter(iter) {
  473. rec = ftrace_rec_iter_record(iter);
  474. ret = add_breakpoints(rec, enable);
  475. if (ret)
  476. goto remove_breakpoints;
  477. count++;
  478. }
  479. run_sync();
  480. report = "updating code";
  481. for_ftrace_rec_iter(iter) {
  482. rec = ftrace_rec_iter_record(iter);
  483. ret = add_update(rec, enable);
  484. if (ret)
  485. goto remove_breakpoints;
  486. }
  487. run_sync();
  488. report = "removing breakpoints";
  489. for_ftrace_rec_iter(iter) {
  490. rec = ftrace_rec_iter_record(iter);
  491. ret = finish_update(rec, enable);
  492. if (ret)
  493. goto remove_breakpoints;
  494. }
  495. run_sync();
  496. return;
  497. remove_breakpoints:
  498. ftrace_bug(ret, rec ? rec->ip : 0);
  499. printk(KERN_WARNING "Failed on %s (%d):\n", report, count);
  500. for_ftrace_rec_iter(iter) {
  501. rec = ftrace_rec_iter_record(iter);
  502. /*
  503. * Breakpoints are handled only when this function is in
  504. * progress. The system could not work with them.
  505. */
  506. if (remove_breakpoint(rec))
  507. BUG();
  508. }
  509. run_sync();
  510. }
  511. static int
  512. ftrace_modify_code(unsigned long ip, unsigned const char *old_code,
  513. unsigned const char *new_code)
  514. {
  515. int ret;
  516. ret = add_break(ip, old_code);
  517. if (ret)
  518. goto out;
  519. run_sync();
  520. ret = add_update_code(ip, new_code);
  521. if (ret)
  522. goto fail_update;
  523. run_sync();
  524. ret = ftrace_write(ip, new_code, 1);
  525. /*
  526. * The breakpoint is handled only when this function is in progress.
  527. * The system could not work if we could not remove it.
  528. */
  529. BUG_ON(ret);
  530. out:
  531. run_sync();
  532. return ret;
  533. fail_update:
  534. /* Also here the system could not work with the breakpoint */
  535. if (ftrace_write(ip, old_code, 1))
  536. BUG();
  537. goto out;
  538. }
  539. void arch_ftrace_update_code(int command)
  540. {
  541. /* See comment above by declaration of modifying_ftrace_code */
  542. atomic_inc(&modifying_ftrace_code);
  543. ftrace_modify_all_code(command);
  544. atomic_dec(&modifying_ftrace_code);
  545. }
  546. int __init ftrace_dyn_arch_init(void)
  547. {
  548. return 0;
  549. }
  550. #endif
  551. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  552. #ifdef CONFIG_DYNAMIC_FTRACE
  553. extern void ftrace_graph_call(void);
  554. static unsigned char *ftrace_jmp_replace(unsigned long ip, unsigned long addr)
  555. {
  556. static union ftrace_code_union calc;
  557. /* Jmp not a call (ignore the .e8) */
  558. calc.e8 = 0xe9;
  559. calc.offset = ftrace_calc_offset(ip + MCOUNT_INSN_SIZE, addr);
  560. /*
  561. * ftrace external locks synchronize the access to the static variable.
  562. */
  563. return calc.code;
  564. }
  565. static int ftrace_mod_jmp(unsigned long ip, void *func)
  566. {
  567. unsigned char *new;
  568. new = ftrace_jmp_replace(ip, (unsigned long)func);
  569. return update_ftrace_func(ip, new);
  570. }
  571. int ftrace_enable_ftrace_graph_caller(void)
  572. {
  573. unsigned long ip = (unsigned long)(&ftrace_graph_call);
  574. return ftrace_mod_jmp(ip, &ftrace_graph_caller);
  575. }
  576. int ftrace_disable_ftrace_graph_caller(void)
  577. {
  578. unsigned long ip = (unsigned long)(&ftrace_graph_call);
  579. return ftrace_mod_jmp(ip, &ftrace_stub);
  580. }
  581. #endif /* !CONFIG_DYNAMIC_FTRACE */
  582. /*
  583. * Hook the return address and push it in the stack of return addrs
  584. * in current thread info.
  585. */
  586. void prepare_ftrace_return(unsigned long *parent, unsigned long self_addr,
  587. unsigned long frame_pointer)
  588. {
  589. unsigned long old;
  590. int faulted;
  591. struct ftrace_graph_ent trace;
  592. unsigned long return_hooker = (unsigned long)
  593. &return_to_handler;
  594. if (unlikely(atomic_read(&current->tracing_graph_pause)))
  595. return;
  596. /*
  597. * Protect against fault, even if it shouldn't
  598. * happen. This tool is too much intrusive to
  599. * ignore such a protection.
  600. */
  601. asm volatile(
  602. "1: " _ASM_MOV " (%[parent]), %[old]\n"
  603. "2: " _ASM_MOV " %[return_hooker], (%[parent])\n"
  604. " movl $0, %[faulted]\n"
  605. "3:\n"
  606. ".section .fixup, \"ax\"\n"
  607. "4: movl $1, %[faulted]\n"
  608. " jmp 3b\n"
  609. ".previous\n"
  610. _ASM_EXTABLE(1b, 4b)
  611. _ASM_EXTABLE(2b, 4b)
  612. : [old] "=&r" (old), [faulted] "=r" (faulted)
  613. : [parent] "r" (parent), [return_hooker] "r" (return_hooker)
  614. : "memory"
  615. );
  616. if (unlikely(faulted)) {
  617. ftrace_graph_stop();
  618. WARN_ON(1);
  619. return;
  620. }
  621. trace.func = self_addr;
  622. trace.depth = current->curr_ret_stack + 1;
  623. /* Only trace if the calling function expects to */
  624. if (!ftrace_graph_entry(&trace)) {
  625. *parent = old;
  626. return;
  627. }
  628. if (ftrace_push_return_trace(old, self_addr, &trace.depth,
  629. frame_pointer) == -EBUSY) {
  630. *parent = old;
  631. return;
  632. }
  633. }
  634. #endif /* CONFIG_FUNCTION_GRAPH_TRACER */