ftrace.c 17 KB

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