Linux 6.16-rc6
[linux-block.git] / arch / x86 / kernel / ftrace.c
CommitLineData
b2441318 1// SPDX-License-Identifier: GPL-2.0
3d083395 2/*
9d2099ab 3 * Dynamic function tracing support.
3d083395
SR
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
3bb258bf
JP
13#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
14
3d083395
SR
15#include <linux/spinlock.h>
16#include <linux/hardirq.h>
6f93fc07 17#include <linux/uaccess.h>
3d083395
SR
18#include <linux/ftrace.h>
19#include <linux/percpu.h>
19b3e967 20#include <linux/sched.h>
f3bea491 21#include <linux/slab.h>
3d083395
SR
22#include <linux/init.h>
23#include <linux/list.h>
84e1c6bb 24#include <linux/module.h>
d5b844a2 25#include <linux/memory.h>
ac0b14dc 26#include <linux/vmalloc.h>
d48567c9 27#include <linux/set_memory.h>
12af2b83 28#include <linux/execmem.h>
3d083395 29
47788c58
FW
30#include <trace/syscall.h>
31
59a094c9 32#include <asm/kprobes.h>
395a59d0 33#include <asm/ftrace.h>
732f3ca7 34#include <asm/nops.h>
9e298e86 35#include <asm/text-patching.h>
3d083395 36
caf4b323 37#ifdef CONFIG_DYNAMIC_FTRACE
3d083395 38
768ae440
PZ
39static int ftrace_poke_late = 0;
40
3a2bfec0 41void ftrace_arch_code_modify_prepare(void)
074376ac 42 __acquires(&text_mutex)
16239630 43{
39611265
SRV
44 /*
45 * Need to grab text_mutex to prevent a race from module loading
46 * and live kernel patching from changing the text permissions while
47 * ftrace has it set to "read/write".
48 */
d5b844a2 49 mutex_lock(&text_mutex);
768ae440 50 ftrace_poke_late = 1;
16239630
SR
51}
52
3a2bfec0 53void ftrace_arch_code_modify_post_process(void)
074376ac 54 __releases(&text_mutex)
16239630 55{
768ae440
PZ
56 /*
57 * ftrace_make_{call,nop}() may be called during
732c7c33 58 * module load, and we need to finish the smp_text_poke_batch_add()
768ae440
PZ
59 * that they do, here.
60 */
e8d7b8c2 61 smp_text_poke_batch_finish();
768ae440 62 ftrace_poke_late = 0;
d5b844a2 63 mutex_unlock(&text_mutex);
16239630
SR
64}
65
768ae440 66static const char *ftrace_nop_replace(void)
17666f02 67{
a89dfde3 68 return x86_nops[5];
17666f02
SR
69}
70
768ae440 71static const char *ftrace_call_replace(unsigned long ip, unsigned long addr)
caf4b323 72{
ee3e2469
PZ
73 /*
74 * No need to translate into a callthunk. The trampoline does
75 * the depth accounting itself.
76 */
67c1d4a2 77 return text_gen_insn(CALL_INSN_OPCODE, (void *)ip, (void *)addr);
caf4b323
FW
78}
79
768ae440 80static int ftrace_verify_code(unsigned long ip, const char *old_code)
3d083395 81{
768ae440 82 char cur_code[MCOUNT_INSN_SIZE];
b05086c7 83
3d083395 84 /*
c5d641f9
LB
85 * Note:
86 * We are paranoid about modifying text, as if a bug was to happen, it
87 * could cause us to read or write to someplace that could cause harm.
88 * Carefully read and modify the code with probe_kernel_*(), and make
89 * sure what we read is what we expected it to be before modifying it.
3d083395 90 */
76aefee5 91 /* read the text we want to modify */
fe557319 92 if (copy_from_kernel_nofault(cur_code, (void *)ip, MCOUNT_INSN_SIZE)) {
768ae440 93 WARN_ON(1);
593eb8a2 94 return -EFAULT;
768ae440 95 }
6f93fc07 96
76aefee5 97 /* Make sure it is what we expect it to be */
768ae440 98 if (memcmp(cur_code, old_code, MCOUNT_INSN_SIZE) != 0) {
ac6c1b2c 99 ftrace_expected = old_code;
768ae440 100 WARN_ON(1);
593eb8a2 101 return -EINVAL;
768ae440 102 }
3d083395 103
768ae440
PZ
104 return 0;
105}
6f93fc07 106
38ebd8d1
BP
107/*
108 * Marked __ref because it calls text_poke_early() which is .init.text. That is
109 * ok because that call will happen early, during boot, when .init sections are
110 * still present.
111 */
112static int __ref
768ae440
PZ
113ftrace_modify_code_direct(unsigned long ip, const char *old_code,
114 const char *new_code)
115{
116 int ret = ftrace_verify_code(ip, old_code);
117 if (ret)
118 return ret;
3d083395 119
768ae440 120 /* replace the text with the new text */
1d7e707a 121 if (ftrace_poke_late)
732c7c33 122 smp_text_poke_batch_add((void *)ip, new_code, MCOUNT_INSN_SIZE, NULL);
1d7e707a
MRM
123 else
124 text_poke_early((void *)ip, new_code, MCOUNT_INSN_SIZE);
6f93fc07 125 return 0;
3d083395
SR
126}
127
768ae440 128int ftrace_make_nop(struct module *mod, struct dyn_ftrace *rec, unsigned long addr)
31e88909 129{
31e88909 130 unsigned long ip = rec->ip;
768ae440 131 const char *new, *old;
31e88909
SR
132
133 old = ftrace_call_replace(ip, addr);
134 new = ftrace_nop_replace();
135
8a4d0a68
SR
136 /*
137 * On boot up, and when modules are loaded, the MCOUNT_ADDR
138 * is converted to a nop, and will never become MCOUNT_ADDR
139 * again. This code is either running before SMP (on boot up)
140 * or before the code will ever be executed (module load).
141 * We do not want to use the breakpoint version in this case,
142 * just modify the code directly.
143 */
144 if (addr == MCOUNT_ADDR)
768ae440 145 return ftrace_modify_code_direct(ip, old, new);
b05086c7 146
768ae440
PZ
147 /*
148 * x86 overrides ftrace_replace_code -- this function will never be used
149 * in this case.
150 */
8a4d0a68
SR
151 WARN_ONCE(1, "invalid use of ftrace_make_nop");
152 return -EINVAL;
31e88909
SR
153}
154
155int ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr)
156{
31e88909 157 unsigned long ip = rec->ip;
768ae440 158 const char *new, *old;
31e88909
SR
159
160 old = ftrace_nop_replace();
161 new = ftrace_call_replace(ip, addr);
162
8a4d0a68
SR
163 /* Should only be called when module is loaded */
164 return ftrace_modify_code_direct(rec->ip, old, new);
d61f82d0
SR
165}
166
08f6fba5
SR
167/*
168 * Should never be called:
169 * As it is only called by __ftrace_replace_code() which is called by
170 * ftrace_replace_code() that x86 overrides, and by ftrace_update_code()
171 * which is called to turn mcount into nops or nops into function calls
172 * but not to convert a function from not using regs to one that uses
173 * regs, which ftrace_modify_call() is for.
174 */
175int ftrace_modify_call(struct dyn_ftrace *rec, unsigned long old_addr,
176 unsigned long addr)
177{
178 WARN_ON(1);
179 return -EINVAL;
180}
08f6fba5 181
87fbb2ac 182int ftrace_update_ftrace_func(ftrace_func_t func)
08d636b6 183{
ab4ead02 184 unsigned long ip;
768ae440 185 const char *new;
ab4ead02 186
768ae440
PZ
187 ip = (unsigned long)(&ftrace_call);
188 new = ftrace_call_replace(ip, (unsigned long)func);
9586ae48 189 smp_text_poke_single((void *)ip, new, MCOUNT_INSN_SIZE, NULL);
08d636b6 190
768ae440
PZ
191 ip = (unsigned long)(&ftrace_regs_call);
192 new = ftrace_call_replace(ip, (unsigned long)func);
9586ae48 193 smp_text_poke_single((void *)ip, new, MCOUNT_INSN_SIZE, NULL);
08d636b6
SR
194
195 return 0;
196}
197
e4f5d544 198void ftrace_replace_code(int enable)
08d636b6
SR
199{
200 struct ftrace_rec_iter *iter;
201 struct dyn_ftrace *rec;
768ae440 202 const char *new, *old;
08d636b6
SR
203 int ret;
204
205 for_ftrace_rec_iter(iter) {
206 rec = ftrace_rec_iter_record(iter);
207
768ae440
PZ
208 switch (ftrace_test_record(rec, enable)) {
209 case FTRACE_UPDATE_IGNORE:
210 default:
211 continue;
08d636b6 212
768ae440
PZ
213 case FTRACE_UPDATE_MAKE_CALL:
214 old = ftrace_nop_replace();
215 break;
08d636b6 216
768ae440
PZ
217 case FTRACE_UPDATE_MODIFY_CALL:
218 case FTRACE_UPDATE_MAKE_NOP:
219 old = ftrace_call_replace(rec->ip, ftrace_get_addr_curr(rec));
220 break;
221 }
08d636b6 222
768ae440
PZ
223 ret = ftrace_verify_code(rec->ip, old);
224 if (ret) {
fd3dc562 225 ftrace_expected = old;
768ae440 226 ftrace_bug(ret, rec);
fd3dc562 227 ftrace_expected = NULL;
768ae440
PZ
228 return;
229 }
08d636b6
SR
230 }
231
08d636b6
SR
232 for_ftrace_rec_iter(iter) {
233 rec = ftrace_rec_iter_record(iter);
234
768ae440
PZ
235 switch (ftrace_test_record(rec, enable)) {
236 case FTRACE_UPDATE_IGNORE:
237 default:
238 continue;
08d636b6 239
768ae440
PZ
240 case FTRACE_UPDATE_MAKE_CALL:
241 case FTRACE_UPDATE_MODIFY_CALL:
242 new = ftrace_call_replace(rec->ip, ftrace_get_addr_new(rec));
243 break;
08d636b6 244
768ae440
PZ
245 case FTRACE_UPDATE_MAKE_NOP:
246 new = ftrace_nop_replace();
247 break;
248 }
08d636b6 249
732c7c33 250 smp_text_poke_batch_add((void *)rec->ip, new, MCOUNT_INSN_SIZE, NULL);
768ae440 251 ftrace_update_record(rec, enable);
08d636b6 252 }
e8d7b8c2 253 smp_text_poke_batch_finish();
8a4d0a68
SR
254}
255
08d636b6
SR
256void arch_ftrace_update_code(int command)
257{
e4f5d544 258 ftrace_modify_all_code(command);
08d636b6
SR
259}
260
f3bea491
SRRH
261/* Currently only x86_64 supports dynamic trampolines */
262#ifdef CONFIG_X86_64
263
f3bea491
SRRH
264static inline void *alloc_tramp(unsigned long size)
265{
12af2b83 266 return execmem_alloc(EXECMEM_FTRACE, size);
f3bea491 267}
7fdfe1e4 268static inline void tramp_free(void *tramp)
f3bea491 269{
12af2b83 270 execmem_free(tramp);
f3bea491 271}
f3bea491
SRRH
272
273/* Defined as markers to the end of the ftrace default trampolines */
f3bea491 274extern void ftrace_regs_caller_end(void);
0298739b 275extern void ftrace_caller_end(void);
f3bea491
SRRH
276extern void ftrace_caller_op_ptr(void);
277extern void ftrace_regs_caller_op_ptr(void);
fe58acef 278extern void ftrace_regs_caller_jmp(void);
f3bea491
SRRH
279
280/* movq function_trace_op(%rip), %rdx */
281/* 0x48 0x8b 0x15 <offset-to-ftrace_trace_op (4 bytes)> */
282#define OP_REF_SIZE 7
283
284/*
285 * The ftrace_ops is passed to the function callback. Since the
286 * trampoline only services a single ftrace_ops, we can pass in
287 * that ops directly.
288 *
289 * The ftrace_op_code_union is used to create a pointer to the
290 * ftrace_ops that will be passed to the callback function.
291 */
292union ftrace_op_code_union {
293 char code[OP_REF_SIZE];
294 struct {
295 char op[3];
296 int offset;
297 } __attribute__((packed));
298};
299
7b75782f
BL
300#define RET_SIZE \
301 (IS_ENABLED(CONFIG_MITIGATION_RETPOLINE) ? 5 : 1 + IS_ENABLED(CONFIG_MITIGATION_SLS))
d2a68c4e 302
aec0be2d
SRRH
303static unsigned long
304create_trampoline(struct ftrace_ops *ops, unsigned int *tramp_size)
f3bea491 305{
f3bea491
SRRH
306 unsigned long start_offset;
307 unsigned long end_offset;
308 unsigned long op_offset;
768ae440 309 unsigned long call_offset;
fe58acef 310 unsigned long jmp_offset;
f3bea491 311 unsigned long offset;
3c0dab44 312 unsigned long npages;
f3bea491 313 unsigned long size;
f3bea491
SRRH
314 unsigned long *ptr;
315 void *trampoline;
ee3e2469 316 void *ip, *dest;
f3bea491
SRRH
317 /* 48 8b 15 <offset> is movq <offset>(%rip), %rdx */
318 unsigned const char op_ref[] = { 0x48, 0x8b, 0x15 };
e52fc2cf 319 unsigned const char retq[] = { RET_INSN_OPCODE, INT3_INSN_OPCODE };
f3bea491 320 union ftrace_op_code_union op_ptr;
1d7e707a 321 int ret;
f3bea491
SRRH
322
323 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS) {
324 start_offset = (unsigned long)ftrace_regs_caller;
325 end_offset = (unsigned long)ftrace_regs_caller_end;
326 op_offset = (unsigned long)ftrace_regs_caller_op_ptr;
768ae440 327 call_offset = (unsigned long)ftrace_regs_call;
fe58acef 328 jmp_offset = (unsigned long)ftrace_regs_caller_jmp;
f3bea491
SRRH
329 } else {
330 start_offset = (unsigned long)ftrace_caller;
0298739b 331 end_offset = (unsigned long)ftrace_caller_end;
f3bea491 332 op_offset = (unsigned long)ftrace_caller_op_ptr;
768ae440 333 call_offset = (unsigned long)ftrace_call;
fe58acef 334 jmp_offset = 0;
f3bea491
SRRH
335 }
336
337 size = end_offset - start_offset;
338
339 /*
340 * Allocate enough size to store the ftrace_caller code,
d2a68c4e
SRV
341 * the iret , as well as the address of the ftrace_ops this
342 * trampoline is used for.
f3bea491 343 */
d2a68c4e 344 trampoline = alloc_tramp(size + RET_SIZE + sizeof(void *));
f3bea491
SRRH
345 if (!trampoline)
346 return 0;
347
d2a68c4e 348 *tramp_size = size + RET_SIZE + sizeof(void *);
3c0dab44 349 npages = DIV_ROUND_UP(*tramp_size, PAGE_SIZE);
aec0be2d 350
f3bea491 351 /* Copy ftrace_caller onto the trampoline memory */
1d7e707a
MRM
352 ret = copy_from_kernel_nofault(trampoline, (void *)start_offset, size);
353 if (WARN_ON(ret < 0))
d2a68c4e 354 goto fail;
f3bea491 355
d2a68c4e 356 ip = trampoline + size;
a75bf27f 357 if (cpu_wants_rethunk_at(ip))
770ae1b7 358 __text_gen_insn(ip, JMP32_INSN_OPCODE, ip, x86_return_thunk, JMP32_INSN_SIZE);
1f001e9d 359 else
1d7e707a 360 memcpy(ip, retq, sizeof(retq));
f3bea491 361
fe58acef
SRV
362 /* No need to test direct calls on created trampolines */
363 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS) {
364 /* NOP the jnz 1f; but make sure it's a 2 byte jnz */
365 ip = trampoline + (jmp_offset - start_offset);
366 if (WARN_ON(*(char *)ip != 0x75))
367 goto fail;
1d7e707a
MRM
368 ret = copy_from_kernel_nofault(ip, x86_nops[2], 2);
369 if (ret < 0)
fe58acef
SRV
370 goto fail;
371 }
372
f3bea491
SRRH
373 /*
374 * The address of the ftrace_ops that is used for this trampoline
375 * is stored at the end of the trampoline. This will be used to
376 * load the third parameter for the callback. Basically, that
377 * location at the end of the trampoline takes the place of
378 * the global function_trace_op variable.
379 */
380
d2a68c4e 381 ptr = (unsigned long *)(trampoline + size + RET_SIZE);
1d7e707a 382 *ptr = (unsigned long)ops;
f3bea491
SRRH
383
384 op_offset -= start_offset;
385 memcpy(&op_ptr, trampoline + op_offset, OP_REF_SIZE);
386
387 /* Are we pointing to the reference? */
d2a68c4e
SRV
388 if (WARN_ON(memcmp(op_ptr.op, op_ref, 3) != 0))
389 goto fail;
f3bea491
SRRH
390
391 /* Load the contents of ptr into the callback parameter */
392 offset = (unsigned long)ptr;
393 offset -= (unsigned long)trampoline + op_offset + OP_REF_SIZE;
394
395 op_ptr.offset = offset;
396
397 /* put in the new offset to the ftrace_ops */
1d7e707a 398 memcpy(trampoline + op_offset, &op_ptr, OP_REF_SIZE);
f3bea491 399
768ae440
PZ
400 /* put in the call to the function */
401 mutex_lock(&text_mutex);
402 call_offset -= start_offset;
ee3e2469
PZ
403 /*
404 * No need to translate into a callthunk. The trampoline does
405 * the depth accounting before the call already.
406 */
407 dest = ftrace_ops_get_func(ops);
1d7e707a
MRM
408 memcpy(trampoline + call_offset,
409 text_gen_insn(CALL_INSN_OPCODE, trampoline + call_offset, dest),
410 CALL_INSN_SIZE);
768ae440
PZ
411 mutex_unlock(&text_mutex);
412
f3bea491
SRRH
413 /* ALLOC_TRAMP flags lets us know we created it */
414 ops->flags |= FTRACE_OPS_FL_ALLOC_TRAMP;
415
d48567c9 416 set_memory_rox((unsigned long)trampoline, npages);
f3bea491 417 return (unsigned long)trampoline;
d2a68c4e 418fail:
7fdfe1e4 419 tramp_free(trampoline);
d2a68c4e 420 return 0;
f3bea491
SRRH
421}
422
59566b0b
SRV
423void set_ftrace_ops_ro(void)
424{
425 struct ftrace_ops *ops;
426 unsigned long start_offset;
427 unsigned long end_offset;
428 unsigned long npages;
429 unsigned long size;
430
431 do_for_each_ftrace_op(ops, ftrace_ops_list) {
432 if (!(ops->flags & FTRACE_OPS_FL_ALLOC_TRAMP))
433 continue;
434
435 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS) {
436 start_offset = (unsigned long)ftrace_regs_caller;
437 end_offset = (unsigned long)ftrace_regs_caller_end;
438 } else {
439 start_offset = (unsigned long)ftrace_caller;
7c0577f4 440 end_offset = (unsigned long)ftrace_caller_end;
59566b0b
SRV
441 }
442 size = end_offset - start_offset;
443 size = size + RET_SIZE + sizeof(void *);
444 npages = DIV_ROUND_UP(size, PAGE_SIZE);
445 set_memory_ro((unsigned long)ops->trampoline, npages);
446 } while_for_each_ftrace_op(ops);
447}
448
15d5b02c
SRRH
449static unsigned long calc_trampoline_call_offset(bool save_regs)
450{
451 unsigned long start_offset;
452 unsigned long call_offset;
453
454 if (save_regs) {
455 start_offset = (unsigned long)ftrace_regs_caller;
456 call_offset = (unsigned long)ftrace_regs_call;
457 } else {
458 start_offset = (unsigned long)ftrace_caller;
459 call_offset = (unsigned long)ftrace_call;
460 }
461
462 return call_offset - start_offset;
463}
464
f3bea491
SRRH
465void arch_ftrace_update_trampoline(struct ftrace_ops *ops)
466{
467 ftrace_func_t func;
f3bea491
SRRH
468 unsigned long offset;
469 unsigned long ip;
aec0be2d 470 unsigned int size;
768ae440 471 const char *new;
f3bea491 472
768ae440 473 if (!ops->trampoline) {
aec0be2d 474 ops->trampoline = create_trampoline(ops, &size);
f3bea491
SRRH
475 if (!ops->trampoline)
476 return;
aec0be2d 477 ops->trampoline_size = size;
768ae440 478 return;
f3bea491
SRRH
479 }
480
768ae440
PZ
481 /*
482 * The ftrace_ops caller may set up its own trampoline.
483 * In such a case, this code must not modify it.
484 */
485 if (!(ops->flags & FTRACE_OPS_FL_ALLOC_TRAMP))
486 return;
487
15d5b02c 488 offset = calc_trampoline_call_offset(ops->flags & FTRACE_OPS_FL_SAVE_REGS);
f3bea491 489 ip = ops->trampoline + offset;
f3bea491
SRRH
490 func = ftrace_ops_get_func(ops);
491
768ae440 492 mutex_lock(&text_mutex);
f3bea491
SRRH
493 /* Do a safe modify in case the trampoline is executing */
494 new = ftrace_call_replace(ip, (unsigned long)func);
9586ae48 495 smp_text_poke_single((void *)ip, new, MCOUNT_INSN_SIZE, NULL);
768ae440 496 mutex_unlock(&text_mutex);
f3bea491 497}
15d5b02c
SRRH
498
499/* Return the address of the function the trampoline calls */
500static void *addr_from_call(void *ptr)
501{
67c1d4a2 502 union text_poke_insn call;
15d5b02c
SRRH
503 int ret;
504
fe557319 505 ret = copy_from_kernel_nofault(&call, ptr, CALL_INSN_SIZE);
15d5b02c
SRRH
506 if (WARN_ON_ONCE(ret < 0))
507 return NULL;
508
509 /* Make sure this is a call */
67c1d4a2
PZ
510 if (WARN_ON_ONCE(call.opcode != CALL_INSN_OPCODE)) {
511 pr_warn("Expected E8, got %x\n", call.opcode);
15d5b02c
SRRH
512 return NULL;
513 }
514
67c1d4a2 515 return ptr + CALL_INSN_SIZE + call.disp;
15d5b02c
SRRH
516}
517
15d5b02c
SRRH
518/*
519 * If the ops->trampoline was not allocated, then it probably
520 * has a static trampoline func, or is the ftrace caller itself.
521 */
522static void *static_tramp_func(struct ftrace_ops *ops, struct dyn_ftrace *rec)
523{
524 unsigned long offset;
525 bool save_regs = rec->flags & FTRACE_FL_REGS_EN;
526 void *ptr;
527
528 if (ops && ops->trampoline) {
0c0593b4
SRV
529#if !defined(CONFIG_HAVE_DYNAMIC_FTRACE_WITH_ARGS) && \
530 defined(CONFIG_FUNCTION_GRAPH_TRACER)
15d5b02c
SRRH
531 /*
532 * We only know about function graph tracer setting as static
533 * trampoline.
534 */
535 if (ops->trampoline == FTRACE_GRAPH_ADDR)
536 return (void *)prepare_ftrace_return;
537#endif
538 return NULL;
539 }
540
541 offset = calc_trampoline_call_offset(save_regs);
542
543 if (save_regs)
544 ptr = (void *)FTRACE_REGS_ADDR + offset;
545 else
546 ptr = (void *)FTRACE_ADDR + offset;
547
548 return addr_from_call(ptr);
549}
550
551void *arch_ftrace_trampoline_func(struct ftrace_ops *ops, struct dyn_ftrace *rec)
552{
553 unsigned long offset;
554
555 /* If we didn't allocate this trampoline, consider it static */
556 if (!ops || !(ops->flags & FTRACE_OPS_FL_ALLOC_TRAMP))
557 return static_tramp_func(ops, rec);
558
559 offset = calc_trampoline_call_offset(ops->flags & FTRACE_OPS_FL_SAVE_REGS);
560 return addr_from_call((void *)ops->trampoline + offset);
561}
562
12cce594
SRRH
563void arch_ftrace_trampoline_free(struct ftrace_ops *ops)
564{
565 if (!ops || !(ops->flags & FTRACE_OPS_FL_ALLOC_TRAMP))
566 return;
567
7fdfe1e4 568 tramp_free((void *)ops->trampoline);
12cce594
SRRH
569 ops->trampoline = 0;
570}
15d5b02c 571
f3bea491
SRRH
572#endif /* CONFIG_X86_64 */
573#endif /* CONFIG_DYNAMIC_FTRACE */
574
575#ifdef CONFIG_FUNCTION_GRAPH_TRACER
576
e999995c 577#if defined(CONFIG_DYNAMIC_FTRACE) && !defined(CONFIG_HAVE_DYNAMIC_FTRACE_WITH_ARGS)
0c0593b4 578extern void ftrace_graph_call(void);
768ae440 579static const char *ftrace_jmp_replace(unsigned long ip, unsigned long addr)
745cfeaa 580{
67c1d4a2 581 return text_gen_insn(JMP32_INSN_OPCODE, (void *)ip, (void *)addr);
745cfeaa
SRV
582}
583
87fbb2ac
SRRH
584static int ftrace_mod_jmp(unsigned long ip, void *func)
585{
768ae440 586 const char *new;
5a45cfe1 587
87fbb2ac 588 new = ftrace_jmp_replace(ip, (unsigned long)func);
9586ae48 589 smp_text_poke_single((void *)ip, new, MCOUNT_INSN_SIZE, NULL);
768ae440 590 return 0;
5a45cfe1
SR
591}
592
593int ftrace_enable_ftrace_graph_caller(void)
594{
595 unsigned long ip = (unsigned long)(&ftrace_graph_call);
5a45cfe1 596
87fbb2ac 597 return ftrace_mod_jmp(ip, &ftrace_graph_caller);
5a45cfe1
SR
598}
599
600int ftrace_disable_ftrace_graph_caller(void)
601{
602 unsigned long ip = (unsigned long)(&ftrace_graph_call);
5a45cfe1 603
87fbb2ac 604 return ftrace_mod_jmp(ip, &ftrace_stub);
5a45cfe1 605}
e999995c 606#endif /* CONFIG_DYNAMIC_FTRACE && !CONFIG_HAVE_DYNAMIC_FTRACE_WITH_ARGS */
e7d3737e 607
41705c42 608static inline bool skip_ftrace_return(void)
e7d3737e 609{
34a477e5
JP
610 /*
611 * When resuming from suspend-to-ram, this function can be indirectly
612 * called from early CPU startup code while the CPU is in real mode,
613 * which would fail miserably. Make sure the stack pointer is a
614 * virtual address.
615 *
616 * This check isn't as accurate as virt_addr_valid(), but it should be
617 * good enough for this purpose, and it's fast.
618 */
41705c42
MHG
619 if ((long)__builtin_frame_address(0) >= 0)
620 return true;
34a477e5 621
41705c42
MHG
622 if (ftrace_graph_is_dead())
623 return true;
624
625 if (atomic_read(&current->tracing_graph_pause))
626 return true;
627 return false;
628}
629
630/*
631 * Hook the return address and push it in the stack of return addrs
632 * in current thread info.
633 */
634void prepare_ftrace_return(unsigned long ip, unsigned long *parent,
635 unsigned long frame_pointer)
636{
637 unsigned long return_hooker = (unsigned long)&return_to_handler;
84b2bc7f 638
41705c42 639 if (unlikely(skip_ftrace_return()))
e7d3737e
FW
640 return;
641
8646698a
SRV
642 if (!function_graph_enter(*parent, ip, frame_pointer, parent))
643 *parent = return_hooker;
0c0593b4
SRV
644}
645
646#ifdef CONFIG_HAVE_DYNAMIC_FTRACE_WITH_ARGS
647void ftrace_graph_func(unsigned long ip, unsigned long parent_ip,
648 struct ftrace_ops *op, struct ftrace_regs *fregs)
649{
7888af41 650 struct pt_regs *regs = &arch_ftrace_regs(fregs)->regs;
0c0593b4 651 unsigned long *stack = (unsigned long *)kernel_stack_pointer(regs);
41705c42
MHG
652 unsigned long return_hooker = (unsigned long)&return_to_handler;
653 unsigned long *parent = (unsigned long *)stack;
654
655 if (unlikely(skip_ftrace_return()))
656 return;
657
0c0593b4 658
41705c42
MHG
659 if (!function_graph_enter_regs(*parent, ip, 0, parent, fregs))
660 *parent = return_hooker;
e7d3737e 661}
0c0593b4
SRV
662#endif
663
fb52607a 664#endif /* CONFIG_FUNCTION_GRAPH_TRACER */