ftrace.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Dynamic function tracing support.
  4. *
  5. * Copyright (C) 2007-2008 Steven Rostedt <srostedt@redhat.com>
  6. *
  7. * Thanks goes to Ingo Molnar, for suggesting the idea.
  8. * Mathieu Desnoyers, for suggesting postponing the modifications.
  9. * Arjan van de Ven, for keeping me straight, and explaining to me
  10. * the dangers of modifying code on the run.
  11. */
  12. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  13. #include <linux/spinlock.h>
  14. #include <linux/hardirq.h>
  15. #include <linux/uaccess.h>
  16. #include <linux/ftrace.h>
  17. #include <linux/percpu.h>
  18. #include <linux/sched.h>
  19. #include <linux/slab.h>
  20. #include <linux/init.h>
  21. #include <linux/list.h>
  22. #include <linux/module.h>
  23. #include <linux/memory.h>
  24. #include <linux/vmalloc.h>
  25. #include <trace/syscall.h>
  26. #include <asm/set_memory.h>
  27. #include <asm/kprobes.h>
  28. #include <asm/ftrace.h>
  29. #include <asm/nops.h>
  30. #include <asm/text-patching.h>
  31. #ifdef CONFIG_DYNAMIC_FTRACE
  32. static int ftrace_poke_late = 0;
  33. int ftrace_arch_code_modify_prepare(void)
  34. __acquires(&text_mutex)
  35. {
  36. /*
  37. * Need to grab text_mutex to prevent a race from module loading
  38. * and live kernel patching from changing the text permissions while
  39. * ftrace has it set to "read/write".
  40. */
  41. mutex_lock(&text_mutex);
  42. ftrace_poke_late = 1;
  43. return 0;
  44. }
  45. int ftrace_arch_code_modify_post_process(void)
  46. __releases(&text_mutex)
  47. {
  48. /*
  49. * ftrace_make_{call,nop}() may be called during
  50. * module load, and we need to finish the text_poke_queue()
  51. * that they do, here.
  52. */
  53. text_poke_finish();
  54. ftrace_poke_late = 0;
  55. mutex_unlock(&text_mutex);
  56. return 0;
  57. }
  58. static const char *ftrace_nop_replace(void)
  59. {
  60. return ideal_nops[NOP_ATOMIC5];
  61. }
  62. static const char *ftrace_call_replace(unsigned long ip, unsigned long addr)
  63. {
  64. return text_gen_insn(CALL_INSN_OPCODE, (void *)ip, (void *)addr);
  65. }
  66. static int ftrace_verify_code(unsigned long ip, const char *old_code)
  67. {
  68. char cur_code[MCOUNT_INSN_SIZE];
  69. /*
  70. * Note:
  71. * We are paranoid about modifying text, as if a bug was to happen, it
  72. * could cause us to read or write to someplace that could cause harm.
  73. * Carefully read and modify the code with probe_kernel_*(), and make
  74. * sure what we read is what we expected it to be before modifying it.
  75. */
  76. /* read the text we want to modify */
  77. if (copy_from_kernel_nofault(cur_code, (void *)ip, MCOUNT_INSN_SIZE)) {
  78. WARN_ON(1);
  79. return -EFAULT;
  80. }
  81. /* Make sure it is what we expect it to be */
  82. if (memcmp(cur_code, old_code, MCOUNT_INSN_SIZE) != 0) {
  83. WARN_ON(1);
  84. return -EINVAL;
  85. }
  86. return 0;
  87. }
  88. /*
  89. * Marked __ref because it calls text_poke_early() which is .init.text. That is
  90. * ok because that call will happen early, during boot, when .init sections are
  91. * still present.
  92. */
  93. static int __ref
  94. ftrace_modify_code_direct(unsigned long ip, const char *old_code,
  95. const char *new_code)
  96. {
  97. int ret = ftrace_verify_code(ip, old_code);
  98. if (ret)
  99. return ret;
  100. /* replace the text with the new text */
  101. if (ftrace_poke_late)
  102. text_poke_queue((void *)ip, new_code, MCOUNT_INSN_SIZE, NULL);
  103. else
  104. text_poke_early((void *)ip, new_code, MCOUNT_INSN_SIZE);
  105. return 0;
  106. }
  107. int ftrace_make_nop(struct module *mod, struct dyn_ftrace *rec, unsigned long addr)
  108. {
  109. unsigned long ip = rec->ip;
  110. const char *new, *old;
  111. old = ftrace_call_replace(ip, addr);
  112. new = ftrace_nop_replace();
  113. /*
  114. * On boot up, and when modules are loaded, the MCOUNT_ADDR
  115. * is converted to a nop, and will never become MCOUNT_ADDR
  116. * again. This code is either running before SMP (on boot up)
  117. * or before the code will ever be executed (module load).
  118. * We do not want to use the breakpoint version in this case,
  119. * just modify the code directly.
  120. */
  121. if (addr == MCOUNT_ADDR)
  122. return ftrace_modify_code_direct(ip, old, new);
  123. /*
  124. * x86 overrides ftrace_replace_code -- this function will never be used
  125. * in this case.
  126. */
  127. WARN_ONCE(1, "invalid use of ftrace_make_nop");
  128. return -EINVAL;
  129. }
  130. int ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr)
  131. {
  132. unsigned long ip = rec->ip;
  133. const char *new, *old;
  134. old = ftrace_nop_replace();
  135. new = ftrace_call_replace(ip, addr);
  136. /* Should only be called when module is loaded */
  137. return ftrace_modify_code_direct(rec->ip, old, new);
  138. }
  139. /*
  140. * Should never be called:
  141. * As it is only called by __ftrace_replace_code() which is called by
  142. * ftrace_replace_code() that x86 overrides, and by ftrace_update_code()
  143. * which is called to turn mcount into nops or nops into function calls
  144. * but not to convert a function from not using regs to one that uses
  145. * regs, which ftrace_modify_call() is for.
  146. */
  147. int ftrace_modify_call(struct dyn_ftrace *rec, unsigned long old_addr,
  148. unsigned long addr)
  149. {
  150. WARN_ON(1);
  151. return -EINVAL;
  152. }
  153. int ftrace_update_ftrace_func(ftrace_func_t func)
  154. {
  155. unsigned long ip;
  156. const char *new;
  157. ip = (unsigned long)(&ftrace_call);
  158. new = ftrace_call_replace(ip, (unsigned long)func);
  159. text_poke_bp((void *)ip, new, MCOUNT_INSN_SIZE, NULL);
  160. ip = (unsigned long)(&ftrace_regs_call);
  161. new = ftrace_call_replace(ip, (unsigned long)func);
  162. text_poke_bp((void *)ip, new, MCOUNT_INSN_SIZE, NULL);
  163. return 0;
  164. }
  165. void ftrace_replace_code(int enable)
  166. {
  167. struct ftrace_rec_iter *iter;
  168. struct dyn_ftrace *rec;
  169. const char *new, *old;
  170. int ret;
  171. for_ftrace_rec_iter(iter) {
  172. rec = ftrace_rec_iter_record(iter);
  173. switch (ftrace_test_record(rec, enable)) {
  174. case FTRACE_UPDATE_IGNORE:
  175. default:
  176. continue;
  177. case FTRACE_UPDATE_MAKE_CALL:
  178. old = ftrace_nop_replace();
  179. break;
  180. case FTRACE_UPDATE_MODIFY_CALL:
  181. case FTRACE_UPDATE_MAKE_NOP:
  182. old = ftrace_call_replace(rec->ip, ftrace_get_addr_curr(rec));
  183. break;
  184. }
  185. ret = ftrace_verify_code(rec->ip, old);
  186. if (ret) {
  187. ftrace_bug(ret, rec);
  188. return;
  189. }
  190. }
  191. for_ftrace_rec_iter(iter) {
  192. rec = ftrace_rec_iter_record(iter);
  193. switch (ftrace_test_record(rec, enable)) {
  194. case FTRACE_UPDATE_IGNORE:
  195. default:
  196. continue;
  197. case FTRACE_UPDATE_MAKE_CALL:
  198. case FTRACE_UPDATE_MODIFY_CALL:
  199. new = ftrace_call_replace(rec->ip, ftrace_get_addr_new(rec));
  200. break;
  201. case FTRACE_UPDATE_MAKE_NOP:
  202. new = ftrace_nop_replace();
  203. break;
  204. }
  205. text_poke_queue((void *)rec->ip, new, MCOUNT_INSN_SIZE, NULL);
  206. ftrace_update_record(rec, enable);
  207. }
  208. text_poke_finish();
  209. }
  210. void arch_ftrace_update_code(int command)
  211. {
  212. ftrace_modify_all_code(command);
  213. }
  214. int __init ftrace_dyn_arch_init(void)
  215. {
  216. return 0;
  217. }
  218. /* Currently only x86_64 supports dynamic trampolines */
  219. #ifdef CONFIG_X86_64
  220. #ifdef CONFIG_MODULES
  221. #include <linux/moduleloader.h>
  222. /* Module allocation simplifies allocating memory for code */
  223. static inline void *alloc_tramp(unsigned long size)
  224. {
  225. return module_alloc(size);
  226. }
  227. static inline void tramp_free(void *tramp)
  228. {
  229. module_memfree(tramp);
  230. }
  231. #else
  232. /* Trampolines can only be created if modules are supported */
  233. static inline void *alloc_tramp(unsigned long size)
  234. {
  235. return NULL;
  236. }
  237. static inline void tramp_free(void *tramp) { }
  238. #endif
  239. /* Defined as markers to the end of the ftrace default trampolines */
  240. extern void ftrace_regs_caller_end(void);
  241. extern void ftrace_regs_caller_ret(void);
  242. extern void ftrace_caller_end(void);
  243. extern void ftrace_caller_op_ptr(void);
  244. extern void ftrace_regs_caller_op_ptr(void);
  245. extern void ftrace_regs_caller_jmp(void);
  246. /* movq function_trace_op(%rip), %rdx */
  247. /* 0x48 0x8b 0x15 <offset-to-ftrace_trace_op (4 bytes)> */
  248. #define OP_REF_SIZE 7
  249. /*
  250. * The ftrace_ops is passed to the function callback. Since the
  251. * trampoline only services a single ftrace_ops, we can pass in
  252. * that ops directly.
  253. *
  254. * The ftrace_op_code_union is used to create a pointer to the
  255. * ftrace_ops that will be passed to the callback function.
  256. */
  257. union ftrace_op_code_union {
  258. char code[OP_REF_SIZE];
  259. struct {
  260. char op[3];
  261. int offset;
  262. } __attribute__((packed));
  263. };
  264. #define RET_SIZE 1
  265. static unsigned long
  266. create_trampoline(struct ftrace_ops *ops, unsigned int *tramp_size)
  267. {
  268. unsigned long start_offset;
  269. unsigned long end_offset;
  270. unsigned long op_offset;
  271. unsigned long call_offset;
  272. unsigned long jmp_offset;
  273. unsigned long offset;
  274. unsigned long npages;
  275. unsigned long size;
  276. unsigned long retq;
  277. unsigned long *ptr;
  278. void *trampoline;
  279. void *ip;
  280. /* 48 8b 15 <offset> is movq <offset>(%rip), %rdx */
  281. unsigned const char op_ref[] = { 0x48, 0x8b, 0x15 };
  282. union ftrace_op_code_union op_ptr;
  283. int ret;
  284. if (ops->flags & FTRACE_OPS_FL_SAVE_REGS) {
  285. start_offset = (unsigned long)ftrace_regs_caller;
  286. end_offset = (unsigned long)ftrace_regs_caller_end;
  287. op_offset = (unsigned long)ftrace_regs_caller_op_ptr;
  288. call_offset = (unsigned long)ftrace_regs_call;
  289. jmp_offset = (unsigned long)ftrace_regs_caller_jmp;
  290. } else {
  291. start_offset = (unsigned long)ftrace_caller;
  292. end_offset = (unsigned long)ftrace_caller_end;
  293. op_offset = (unsigned long)ftrace_caller_op_ptr;
  294. call_offset = (unsigned long)ftrace_call;
  295. jmp_offset = 0;
  296. }
  297. size = end_offset - start_offset;
  298. /*
  299. * Allocate enough size to store the ftrace_caller code,
  300. * the iret , as well as the address of the ftrace_ops this
  301. * trampoline is used for.
  302. */
  303. trampoline = alloc_tramp(size + RET_SIZE + sizeof(void *));
  304. if (!trampoline)
  305. return 0;
  306. *tramp_size = size + RET_SIZE + sizeof(void *);
  307. npages = DIV_ROUND_UP(*tramp_size, PAGE_SIZE);
  308. /* Copy ftrace_caller onto the trampoline memory */
  309. ret = copy_from_kernel_nofault(trampoline, (void *)start_offset, size);
  310. if (WARN_ON(ret < 0))
  311. goto fail;
  312. ip = trampoline + size;
  313. /* The trampoline ends with ret(q) */
  314. retq = (unsigned long)ftrace_stub;
  315. ret = copy_from_kernel_nofault(ip, (void *)retq, RET_SIZE);
  316. if (WARN_ON(ret < 0))
  317. goto fail;
  318. /* No need to test direct calls on created trampolines */
  319. if (ops->flags & FTRACE_OPS_FL_SAVE_REGS) {
  320. /* NOP the jnz 1f; but make sure it's a 2 byte jnz */
  321. ip = trampoline + (jmp_offset - start_offset);
  322. if (WARN_ON(*(char *)ip != 0x75))
  323. goto fail;
  324. ret = copy_from_kernel_nofault(ip, ideal_nops[2], 2);
  325. if (ret < 0)
  326. goto fail;
  327. }
  328. /*
  329. * The address of the ftrace_ops that is used for this trampoline
  330. * is stored at the end of the trampoline. This will be used to
  331. * load the third parameter for the callback. Basically, that
  332. * location at the end of the trampoline takes the place of
  333. * the global function_trace_op variable.
  334. */
  335. ptr = (unsigned long *)(trampoline + size + RET_SIZE);
  336. *ptr = (unsigned long)ops;
  337. op_offset -= start_offset;
  338. memcpy(&op_ptr, trampoline + op_offset, OP_REF_SIZE);
  339. /* Are we pointing to the reference? */
  340. if (WARN_ON(memcmp(op_ptr.op, op_ref, 3) != 0))
  341. goto fail;
  342. /* Load the contents of ptr into the callback parameter */
  343. offset = (unsigned long)ptr;
  344. offset -= (unsigned long)trampoline + op_offset + OP_REF_SIZE;
  345. op_ptr.offset = offset;
  346. /* put in the new offset to the ftrace_ops */
  347. memcpy(trampoline + op_offset, &op_ptr, OP_REF_SIZE);
  348. /* put in the call to the function */
  349. mutex_lock(&text_mutex);
  350. call_offset -= start_offset;
  351. memcpy(trampoline + call_offset,
  352. text_gen_insn(CALL_INSN_OPCODE,
  353. trampoline + call_offset,
  354. ftrace_ops_get_func(ops)), CALL_INSN_SIZE);
  355. mutex_unlock(&text_mutex);
  356. /* ALLOC_TRAMP flags lets us know we created it */
  357. ops->flags |= FTRACE_OPS_FL_ALLOC_TRAMP;
  358. set_vm_flush_reset_perms(trampoline);
  359. if (likely(system_state != SYSTEM_BOOTING))
  360. set_memory_ro((unsigned long)trampoline, npages);
  361. set_memory_x((unsigned long)trampoline, npages);
  362. return (unsigned long)trampoline;
  363. fail:
  364. tramp_free(trampoline);
  365. return 0;
  366. }
  367. void set_ftrace_ops_ro(void)
  368. {
  369. struct ftrace_ops *ops;
  370. unsigned long start_offset;
  371. unsigned long end_offset;
  372. unsigned long npages;
  373. unsigned long size;
  374. do_for_each_ftrace_op(ops, ftrace_ops_list) {
  375. if (!(ops->flags & FTRACE_OPS_FL_ALLOC_TRAMP))
  376. continue;
  377. if (ops->flags & FTRACE_OPS_FL_SAVE_REGS) {
  378. start_offset = (unsigned long)ftrace_regs_caller;
  379. end_offset = (unsigned long)ftrace_regs_caller_end;
  380. } else {
  381. start_offset = (unsigned long)ftrace_caller;
  382. end_offset = (unsigned long)ftrace_caller_end;
  383. }
  384. size = end_offset - start_offset;
  385. size = size + RET_SIZE + sizeof(void *);
  386. npages = DIV_ROUND_UP(size, PAGE_SIZE);
  387. set_memory_ro((unsigned long)ops->trampoline, npages);
  388. } while_for_each_ftrace_op(ops);
  389. }
  390. static unsigned long calc_trampoline_call_offset(bool save_regs)
  391. {
  392. unsigned long start_offset;
  393. unsigned long call_offset;
  394. if (save_regs) {
  395. start_offset = (unsigned long)ftrace_regs_caller;
  396. call_offset = (unsigned long)ftrace_regs_call;
  397. } else {
  398. start_offset = (unsigned long)ftrace_caller;
  399. call_offset = (unsigned long)ftrace_call;
  400. }
  401. return call_offset - start_offset;
  402. }
  403. void arch_ftrace_update_trampoline(struct ftrace_ops *ops)
  404. {
  405. ftrace_func_t func;
  406. unsigned long offset;
  407. unsigned long ip;
  408. unsigned int size;
  409. const char *new;
  410. if (!ops->trampoline) {
  411. ops->trampoline = create_trampoline(ops, &size);
  412. if (!ops->trampoline)
  413. return;
  414. ops->trampoline_size = size;
  415. return;
  416. }
  417. /*
  418. * The ftrace_ops caller may set up its own trampoline.
  419. * In such a case, this code must not modify it.
  420. */
  421. if (!(ops->flags & FTRACE_OPS_FL_ALLOC_TRAMP))
  422. return;
  423. offset = calc_trampoline_call_offset(ops->flags & FTRACE_OPS_FL_SAVE_REGS);
  424. ip = ops->trampoline + offset;
  425. func = ftrace_ops_get_func(ops);
  426. mutex_lock(&text_mutex);
  427. /* Do a safe modify in case the trampoline is executing */
  428. new = ftrace_call_replace(ip, (unsigned long)func);
  429. text_poke_bp((void *)ip, new, MCOUNT_INSN_SIZE, NULL);
  430. mutex_unlock(&text_mutex);
  431. }
  432. /* Return the address of the function the trampoline calls */
  433. static void *addr_from_call(void *ptr)
  434. {
  435. union text_poke_insn call;
  436. int ret;
  437. ret = copy_from_kernel_nofault(&call, ptr, CALL_INSN_SIZE);
  438. if (WARN_ON_ONCE(ret < 0))
  439. return NULL;
  440. /* Make sure this is a call */
  441. if (WARN_ON_ONCE(call.opcode != CALL_INSN_OPCODE)) {
  442. pr_warn("Expected E8, got %x\n", call.opcode);
  443. return NULL;
  444. }
  445. return ptr + CALL_INSN_SIZE + call.disp;
  446. }
  447. void prepare_ftrace_return(unsigned long self_addr, unsigned long *parent,
  448. unsigned long frame_pointer);
  449. /*
  450. * If the ops->trampoline was not allocated, then it probably
  451. * has a static trampoline func, or is the ftrace caller itself.
  452. */
  453. static void *static_tramp_func(struct ftrace_ops *ops, struct dyn_ftrace *rec)
  454. {
  455. unsigned long offset;
  456. bool save_regs = rec->flags & FTRACE_FL_REGS_EN;
  457. void *ptr;
  458. if (ops && ops->trampoline) {
  459. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  460. /*
  461. * We only know about function graph tracer setting as static
  462. * trampoline.
  463. */
  464. if (ops->trampoline == FTRACE_GRAPH_ADDR)
  465. return (void *)prepare_ftrace_return;
  466. #endif
  467. return NULL;
  468. }
  469. offset = calc_trampoline_call_offset(save_regs);
  470. if (save_regs)
  471. ptr = (void *)FTRACE_REGS_ADDR + offset;
  472. else
  473. ptr = (void *)FTRACE_ADDR + offset;
  474. return addr_from_call(ptr);
  475. }
  476. void *arch_ftrace_trampoline_func(struct ftrace_ops *ops, struct dyn_ftrace *rec)
  477. {
  478. unsigned long offset;
  479. /* If we didn't allocate this trampoline, consider it static */
  480. if (!ops || !(ops->flags & FTRACE_OPS_FL_ALLOC_TRAMP))
  481. return static_tramp_func(ops, rec);
  482. offset = calc_trampoline_call_offset(ops->flags & FTRACE_OPS_FL_SAVE_REGS);
  483. return addr_from_call((void *)ops->trampoline + offset);
  484. }
  485. void arch_ftrace_trampoline_free(struct ftrace_ops *ops)
  486. {
  487. if (!ops || !(ops->flags & FTRACE_OPS_FL_ALLOC_TRAMP))
  488. return;
  489. tramp_free((void *)ops->trampoline);
  490. ops->trampoline = 0;
  491. }
  492. #endif /* CONFIG_X86_64 */
  493. #endif /* CONFIG_DYNAMIC_FTRACE */
  494. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  495. #ifdef CONFIG_DYNAMIC_FTRACE
  496. extern void ftrace_graph_call(void);
  497. static const char *ftrace_jmp_replace(unsigned long ip, unsigned long addr)
  498. {
  499. return text_gen_insn(JMP32_INSN_OPCODE, (void *)ip, (void *)addr);
  500. }
  501. static int ftrace_mod_jmp(unsigned long ip, void *func)
  502. {
  503. const char *new;
  504. new = ftrace_jmp_replace(ip, (unsigned long)func);
  505. text_poke_bp((void *)ip, new, MCOUNT_INSN_SIZE, NULL);
  506. return 0;
  507. }
  508. int ftrace_enable_ftrace_graph_caller(void)
  509. {
  510. unsigned long ip = (unsigned long)(&ftrace_graph_call);
  511. return ftrace_mod_jmp(ip, &ftrace_graph_caller);
  512. }
  513. int ftrace_disable_ftrace_graph_caller(void)
  514. {
  515. unsigned long ip = (unsigned long)(&ftrace_graph_call);
  516. return ftrace_mod_jmp(ip, &ftrace_stub);
  517. }
  518. #endif /* !CONFIG_DYNAMIC_FTRACE */
  519. /*
  520. * Hook the return address and push it in the stack of return addrs
  521. * in current thread info.
  522. */
  523. void prepare_ftrace_return(unsigned long self_addr, unsigned long *parent,
  524. unsigned long frame_pointer)
  525. {
  526. unsigned long return_hooker = (unsigned long)&return_to_handler;
  527. unsigned long old;
  528. int faulted;
  529. /*
  530. * When resuming from suspend-to-ram, this function can be indirectly
  531. * called from early CPU startup code while the CPU is in real mode,
  532. * which would fail miserably. Make sure the stack pointer is a
  533. * virtual address.
  534. *
  535. * This check isn't as accurate as virt_addr_valid(), but it should be
  536. * good enough for this purpose, and it's fast.
  537. */
  538. if (unlikely((long)__builtin_frame_address(0) >= 0))
  539. return;
  540. if (unlikely(ftrace_graph_is_dead()))
  541. return;
  542. if (unlikely(atomic_read(&current->tracing_graph_pause)))
  543. return;
  544. /*
  545. * Protect against fault, even if it shouldn't
  546. * happen. This tool is too much intrusive to
  547. * ignore such a protection.
  548. */
  549. asm volatile(
  550. "1: " _ASM_MOV " (%[parent]), %[old]\n"
  551. "2: " _ASM_MOV " %[return_hooker], (%[parent])\n"
  552. " movl $0, %[faulted]\n"
  553. "3:\n"
  554. ".section .fixup, \"ax\"\n"
  555. "4: movl $1, %[faulted]\n"
  556. " jmp 3b\n"
  557. ".previous\n"
  558. _ASM_EXTABLE(1b, 4b)
  559. _ASM_EXTABLE(2b, 4b)
  560. : [old] "=&r" (old), [faulted] "=r" (faulted)
  561. : [parent] "r" (parent), [return_hooker] "r" (return_hooker)
  562. : "memory"
  563. );
  564. if (unlikely(faulted)) {
  565. ftrace_graph_stop();
  566. WARN_ON(1);
  567. return;
  568. }
  569. if (function_graph_enter(old, self_addr, frame_pointer, parent))
  570. *parent = old;
  571. }
  572. #endif /* CONFIG_FUNCTION_GRAPH_TRACER */