2 * Code for replacing ftrace calls with jumps.
4 * Copyright (C) 2007-2008 Steven Rostedt <srostedt@redhat.com>
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.
12 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
14 #include <linux/spinlock.h>
15 #include <linux/hardirq.h>
16 #include <linux/uaccess.h>
17 #include <linux/ftrace.h>
18 #include <linux/percpu.h>
19 #include <linux/sched.h>
20 #include <linux/init.h>
21 #include <linux/list.h>
22 #include <linux/module.h>
23 #include <linux/kprobes.h>
25 #include <trace/syscall.h>
27 #include <asm/cacheflush.h>
28 #include <asm/ftrace.h>
33 #ifdef CONFIG_DYNAMIC_FTRACE
36 * modifying_code is set to notify NMIs that they need to use
37 * memory barriers when entering or exiting. But we don't want
38 * to burden NMIs with unnecessary memory barriers when code
39 * modification is not being done (which is most of the time).
41 * A mutex is already held when ftrace_arch_code_modify_prepare
42 * and post_process are called. No locks need to be taken here.
44 * Stop machine will make sure currently running NMIs are done
45 * and new NMIs will see the updated variable before we need
46 * to worry about NMIs doing memory barriers.
48 static int modifying_code __read_mostly;
49 static DEFINE_PER_CPU(int, save_modifying_code);
51 int ftrace_arch_code_modify_prepare(void)
54 set_all_modules_text_rw();
59 int ftrace_arch_code_modify_post_process(void)
62 set_all_modules_text_ro();
67 union ftrace_code_union {
68 char code[MCOUNT_INSN_SIZE];
72 } __attribute__((packed));
75 static int ftrace_calc_offset(long ip, long addr)
77 return (int)(addr - ip);
80 static unsigned char *ftrace_call_replace(unsigned long ip, unsigned long addr)
82 static union ftrace_code_union calc;
85 calc.offset = ftrace_calc_offset(ip + MCOUNT_INSN_SIZE, addr);
88 * No locking needed, this must be called via kstop_machine
89 * which in essence is like running on a uniprocessor machine.
95 * Modifying code must take extra care. On an SMP machine, if
96 * the code being modified is also being executed on another CPU
97 * that CPU will have undefined results and possibly take a GPF.
98 * We use kstop_machine to stop other CPUS from exectuing code.
99 * But this does not stop NMIs from happening. We still need
100 * to protect against that. We separate out the modification of
101 * the code to take care of this.
103 * Two buffers are added: An IP buffer and a "code" buffer.
105 * 1) Put the instruction pointer into the IP buffer
106 * and the new code into the "code" buffer.
107 * 2) Wait for any running NMIs to finish and set a flag that says
108 * we are modifying code, it is done in an atomic operation.
111 * 5) Wait for any running NMIs to finish.
113 * If an NMI is executed, the first thing it does is to call
114 * "ftrace_nmi_enter". This will check if the flag is set to write
115 * and if it is, it will write what is in the IP and "code" buffers.
117 * The trick is, it does not matter if everyone is writing the same
118 * content to the code location. Also, if a CPU is executing code
119 * it is OK to write to that code location if the contents being written
120 * are the same as what exists.
123 #define MOD_CODE_WRITE_FLAG (1 << 31) /* set when NMI should do the write */
124 static atomic_t nmi_running = ATOMIC_INIT(0);
125 static int mod_code_status; /* holds return value of text write */
126 static void *mod_code_ip; /* holds the IP to write to */
127 static const void *mod_code_newcode; /* holds the text to write to the IP */
129 static unsigned nmi_wait_count;
130 static atomic_t nmi_update_count = ATOMIC_INIT(0);
132 int ftrace_arch_read_dyn_info(char *buf, int size)
136 r = snprintf(buf, size, "%u %u",
138 atomic_read(&nmi_update_count));
142 static void clear_mod_flag(void)
144 int old = atomic_read(&nmi_running);
147 int new = old & ~MOD_CODE_WRITE_FLAG;
152 old = atomic_cmpxchg(&nmi_running, old, new);
156 static void ftrace_mod_code(void)
159 * Yes, more than one CPU process can be writing to mod_code_status.
160 * (and the code itself)
161 * But if one were to fail, then they all should, and if one were
162 * to succeed, then they all should.
164 mod_code_status = probe_kernel_write(mod_code_ip, mod_code_newcode,
167 /* if we fail, then kill any new writers */
172 void ftrace_nmi_enter(void)
174 __this_cpu_write(save_modifying_code, modifying_code);
176 if (!__this_cpu_read(save_modifying_code))
179 if (atomic_inc_return(&nmi_running) & MOD_CODE_WRITE_FLAG) {
182 atomic_inc(&nmi_update_count);
184 /* Must have previous changes seen before executions */
188 void ftrace_nmi_exit(void)
190 if (!__this_cpu_read(save_modifying_code))
193 /* Finish all executions before clearing nmi_running */
195 atomic_dec(&nmi_running);
198 static void wait_for_nmi_and_set_mod_flag(void)
200 if (!atomic_cmpxchg(&nmi_running, 0, MOD_CODE_WRITE_FLAG))
205 } while (atomic_cmpxchg(&nmi_running, 0, MOD_CODE_WRITE_FLAG));
210 static void wait_for_nmi(void)
212 if (!atomic_read(&nmi_running))
217 } while (atomic_read(&nmi_running));
223 within(unsigned long addr, unsigned long start, unsigned long end)
225 return addr >= start && addr < end;
229 do_ftrace_mod_code(unsigned long ip, const void *new_code)
232 * On x86_64, kernel text mappings are mapped read-only with
233 * CONFIG_DEBUG_RODATA. So we use the kernel identity mapping instead
234 * of the kernel text mapping to modify the kernel text.
236 * For 32bit kernels, these mappings are same and we can use
237 * kernel identity mapping to modify code.
239 if (within(ip, (unsigned long)_text, (unsigned long)_etext))
240 ip = (unsigned long)__va(__pa(ip));
242 mod_code_ip = (void *)ip;
243 mod_code_newcode = new_code;
245 /* The buffers need to be visible before we let NMIs write them */
248 wait_for_nmi_and_set_mod_flag();
250 /* Make sure all running NMIs have finished before we write the code */
255 /* Make sure the write happens before clearing the bit */
261 return mod_code_status;
264 static const unsigned char *ftrace_nop_replace(void)
266 return ideal_nops[NOP_ATOMIC5];
270 ftrace_modify_code(unsigned long ip, unsigned const char *old_code,
271 unsigned const char *new_code)
273 unsigned char replaced[MCOUNT_INSN_SIZE];
276 * Note: Due to modules and __init, code can
277 * disappear and change, we need to protect against faulting
278 * as well as code changing. We do this by using the
279 * probe_kernel_* functions.
281 * No real locking needed, this code is run through
282 * kstop_machine, or before SMP starts.
285 /* read the text we want to modify */
286 if (probe_kernel_read(replaced, (void *)ip, MCOUNT_INSN_SIZE))
289 /* Make sure it is what we expect it to be */
290 if (memcmp(replaced, old_code, MCOUNT_INSN_SIZE) != 0)
293 /* replace the text with the new text */
294 if (do_ftrace_mod_code(ip, new_code))
302 int ftrace_make_nop(struct module *mod,
303 struct dyn_ftrace *rec, unsigned long addr)
305 unsigned const char *new, *old;
306 unsigned long ip = rec->ip;
308 old = ftrace_call_replace(ip, addr);
309 new = ftrace_nop_replace();
311 return ftrace_modify_code(rec->ip, old, new);
314 int ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr)
316 unsigned const char *new, *old;
317 unsigned long ip = rec->ip;
319 old = ftrace_nop_replace();
320 new = ftrace_call_replace(ip, addr);
322 return ftrace_modify_code(rec->ip, old, new);
325 int ftrace_update_ftrace_func(ftrace_func_t func)
327 unsigned long ip = (unsigned long)(&ftrace_call);
328 unsigned char old[MCOUNT_INSN_SIZE], *new;
331 memcpy(old, &ftrace_call, MCOUNT_INSN_SIZE);
332 new = ftrace_call_replace(ip, (unsigned long)func);
333 ret = ftrace_modify_code(ip, old, new);
338 int modifying_ftrace_code __read_mostly;
341 * A breakpoint was added to the code address we are about to
342 * modify, and this is the handle that will just skip over it.
343 * We are either changing a nop into a trace call, or a trace
344 * call to a nop. While the change is taking place, we treat
345 * it just like it was a nop.
347 int ftrace_int3_handler(struct pt_regs *regs)
349 if (WARN_ON_ONCE(!regs))
352 if (!ftrace_location(regs->ip - 1))
355 regs->ip += MCOUNT_INSN_SIZE - 1;
360 static int ftrace_write(unsigned long ip, const char *val, int size)
363 * On x86_64, kernel text mappings are mapped read-only with
364 * CONFIG_DEBUG_RODATA. So we use the kernel identity mapping instead
365 * of the kernel text mapping to modify the kernel text.
367 * For 32bit kernels, these mappings are same and we can use
368 * kernel identity mapping to modify code.
370 if (within(ip, (unsigned long)_text, (unsigned long)_etext))
371 ip = (unsigned long)__va(__pa(ip));
373 return probe_kernel_write((void *)ip, val, size);
376 static int add_break(unsigned long ip, const char *old)
378 unsigned char replaced[MCOUNT_INSN_SIZE];
379 unsigned char brk = BREAKPOINT_INSTRUCTION;
381 if (probe_kernel_read(replaced, (void *)ip, MCOUNT_INSN_SIZE))
384 /* Make sure it is what we expect it to be */
385 if (memcmp(replaced, old, MCOUNT_INSN_SIZE) != 0)
388 if (ftrace_write(ip, &brk, 1))
394 static int add_brk_on_call(struct dyn_ftrace *rec, unsigned long addr)
396 unsigned const char *old;
397 unsigned long ip = rec->ip;
399 old = ftrace_call_replace(ip, addr);
401 return add_break(rec->ip, old);
405 static int add_brk_on_nop(struct dyn_ftrace *rec)
407 unsigned const char *old;
409 old = ftrace_nop_replace();
411 return add_break(rec->ip, old);
414 static int add_breakpoints(struct dyn_ftrace *rec, int enable)
416 unsigned long ftrace_addr;
419 ret = ftrace_test_record(rec, enable);
421 ftrace_addr = (unsigned long)FTRACE_ADDR;
424 case FTRACE_UPDATE_IGNORE:
427 case FTRACE_UPDATE_MAKE_CALL:
428 /* converting nop to call */
429 return add_brk_on_nop(rec);
431 case FTRACE_UPDATE_MAKE_NOP:
432 /* converting a call to a nop */
433 return add_brk_on_call(rec, ftrace_addr);
439 * On error, we need to remove breakpoints. This needs to
440 * be done caefully. If the address does not currently have a
441 * breakpoint, we know we are done. Otherwise, we look at the
442 * remaining 4 bytes of the instruction. If it matches a nop
443 * we replace the breakpoint with the nop. Otherwise we replace
444 * it with the call instruction.
446 static int remove_breakpoint(struct dyn_ftrace *rec)
448 unsigned char ins[MCOUNT_INSN_SIZE];
449 unsigned char brk = BREAKPOINT_INSTRUCTION;
450 const unsigned char *nop;
451 unsigned long ftrace_addr;
452 unsigned long ip = rec->ip;
454 /* If we fail the read, just give up */
455 if (probe_kernel_read(ins, (void *)ip, MCOUNT_INSN_SIZE))
458 /* If this does not have a breakpoint, we are done */
462 nop = ftrace_nop_replace();
465 * If the last 4 bytes of the instruction do not match
466 * a nop, then we assume that this is a call to ftrace_addr.
468 if (memcmp(&ins[1], &nop[1], MCOUNT_INSN_SIZE - 1) != 0) {
470 * For extra paranoidism, we check if the breakpoint is on
471 * a call that would actually jump to the ftrace_addr.
472 * If not, don't touch the breakpoint, we make just create
475 ftrace_addr = (unsigned long)FTRACE_ADDR;
476 nop = ftrace_call_replace(ip, ftrace_addr);
478 if (memcmp(&ins[1], &nop[1], MCOUNT_INSN_SIZE - 1) != 0)
482 return probe_kernel_write((void *)ip, &nop[0], 1);
485 static int add_update_code(unsigned long ip, unsigned const char *new)
487 /* skip breakpoint */
490 if (ftrace_write(ip, new, MCOUNT_INSN_SIZE - 1))
495 static int add_update_call(struct dyn_ftrace *rec, unsigned long addr)
497 unsigned long ip = rec->ip;
498 unsigned const char *new;
500 new = ftrace_call_replace(ip, addr);
501 return add_update_code(ip, new);
504 static int add_update_nop(struct dyn_ftrace *rec)
506 unsigned long ip = rec->ip;
507 unsigned const char *new;
509 new = ftrace_nop_replace();
510 return add_update_code(ip, new);
513 static int add_update(struct dyn_ftrace *rec, int enable)
515 unsigned long ftrace_addr;
518 ret = ftrace_test_record(rec, enable);
520 ftrace_addr = (unsigned long)FTRACE_ADDR;
523 case FTRACE_UPDATE_IGNORE:
526 case FTRACE_UPDATE_MAKE_CALL:
527 /* converting nop to call */
528 return add_update_call(rec, ftrace_addr);
530 case FTRACE_UPDATE_MAKE_NOP:
531 /* converting a call to a nop */
532 return add_update_nop(rec);
538 static int finish_update_call(struct dyn_ftrace *rec, unsigned long addr)
540 unsigned long ip = rec->ip;
541 unsigned const char *new;
543 new = ftrace_call_replace(ip, addr);
545 if (ftrace_write(ip, new, 1))
551 static int finish_update_nop(struct dyn_ftrace *rec)
553 unsigned long ip = rec->ip;
554 unsigned const char *new;
556 new = ftrace_nop_replace();
558 if (ftrace_write(ip, new, 1))
563 static int finish_update(struct dyn_ftrace *rec, int enable)
565 unsigned long ftrace_addr;
568 ret = ftrace_update_record(rec, enable);
570 ftrace_addr = (unsigned long)FTRACE_ADDR;
573 case FTRACE_UPDATE_IGNORE:
576 case FTRACE_UPDATE_MAKE_CALL:
577 /* converting nop to call */
578 return finish_update_call(rec, ftrace_addr);
580 case FTRACE_UPDATE_MAKE_NOP:
581 /* converting a call to a nop */
582 return finish_update_nop(rec);
588 static void do_sync_core(void *data)
593 static void run_sync(void)
595 int enable_irqs = irqs_disabled();
597 /* We may be called with interrupts disbled (on bootup). */
600 on_each_cpu(do_sync_core, NULL, 1);
605 static void ftrace_replace_code(int enable)
607 struct ftrace_rec_iter *iter;
608 struct dyn_ftrace *rec;
609 const char *report = "adding breakpoints";
613 for_ftrace_rec_iter(iter) {
614 rec = ftrace_rec_iter_record(iter);
616 ret = add_breakpoints(rec, enable);
618 goto remove_breakpoints;
624 report = "updating code";
626 for_ftrace_rec_iter(iter) {
627 rec = ftrace_rec_iter_record(iter);
629 ret = add_update(rec, enable);
631 goto remove_breakpoints;
636 report = "removing breakpoints";
638 for_ftrace_rec_iter(iter) {
639 rec = ftrace_rec_iter_record(iter);
641 ret = finish_update(rec, enable);
643 goto remove_breakpoints;
651 ftrace_bug(ret, rec ? rec->ip : 0);
652 printk(KERN_WARNING "Failed on %s (%d):\n", report, count);
653 for_ftrace_rec_iter(iter) {
654 rec = ftrace_rec_iter_record(iter);
655 remove_breakpoint(rec);
659 void arch_ftrace_update_code(int command)
661 modifying_ftrace_code++;
663 if (command & FTRACE_UPDATE_CALLS)
664 ftrace_replace_code(1);
665 else if (command & FTRACE_DISABLE_CALLS)
666 ftrace_replace_code(0);
668 if (command & FTRACE_UPDATE_TRACE_FUNC)
669 ftrace_update_ftrace_func(ftrace_trace_function);
671 if (command & FTRACE_START_FUNC_RET)
672 ftrace_enable_ftrace_graph_caller();
673 else if (command & FTRACE_STOP_FUNC_RET)
674 ftrace_disable_ftrace_graph_caller();
676 modifying_ftrace_code--;
679 int __init ftrace_dyn_arch_init(void *data)
681 /* The return code is retured via data */
682 *(unsigned long *)data = 0;
688 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
690 #ifdef CONFIG_DYNAMIC_FTRACE
691 extern void ftrace_graph_call(void);
693 static int ftrace_mod_jmp(unsigned long ip,
694 int old_offset, int new_offset)
696 unsigned char code[MCOUNT_INSN_SIZE];
698 if (probe_kernel_read(code, (void *)ip, MCOUNT_INSN_SIZE))
701 if (code[0] != 0xe9 || old_offset != *(int *)(&code[1]))
704 *(int *)(&code[1]) = new_offset;
706 if (do_ftrace_mod_code(ip, &code))
712 int ftrace_enable_ftrace_graph_caller(void)
714 unsigned long ip = (unsigned long)(&ftrace_graph_call);
715 int old_offset, new_offset;
717 old_offset = (unsigned long)(&ftrace_stub) - (ip + MCOUNT_INSN_SIZE);
718 new_offset = (unsigned long)(&ftrace_graph_caller) - (ip + MCOUNT_INSN_SIZE);
720 return ftrace_mod_jmp(ip, old_offset, new_offset);
723 int ftrace_disable_ftrace_graph_caller(void)
725 unsigned long ip = (unsigned long)(&ftrace_graph_call);
726 int old_offset, new_offset;
728 old_offset = (unsigned long)(&ftrace_graph_caller) - (ip + MCOUNT_INSN_SIZE);
729 new_offset = (unsigned long)(&ftrace_stub) - (ip + MCOUNT_INSN_SIZE);
731 return ftrace_mod_jmp(ip, old_offset, new_offset);
734 #endif /* !CONFIG_DYNAMIC_FTRACE */
737 * Hook the return address and push it in the stack of return addrs
738 * in current thread info.
740 void prepare_ftrace_return(unsigned long *parent, unsigned long self_addr,
741 unsigned long frame_pointer)
745 struct ftrace_graph_ent trace;
746 unsigned long return_hooker = (unsigned long)
749 if (unlikely(atomic_read(¤t->tracing_graph_pause)))
753 * Protect against fault, even if it shouldn't
754 * happen. This tool is too much intrusive to
755 * ignore such a protection.
758 "1: " _ASM_MOV " (%[parent]), %[old]\n"
759 "2: " _ASM_MOV " %[return_hooker], (%[parent])\n"
760 " movl $0, %[faulted]\n"
763 ".section .fixup, \"ax\"\n"
764 "4: movl $1, %[faulted]\n"
771 : [old] "=&r" (old), [faulted] "=r" (faulted)
772 : [parent] "r" (parent), [return_hooker] "r" (return_hooker)
776 if (unlikely(faulted)) {
782 trace.func = self_addr;
783 trace.depth = current->curr_ret_stack + 1;
785 /* Only trace if the calling function expects to */
786 if (!ftrace_graph_entry(&trace)) {
791 if (ftrace_push_return_trace(old, self_addr, &trace.depth,
792 frame_pointer) == -EBUSY) {
797 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */