ftrace: protect running nmi (V3)
[safe/jmp/linux-2.6] / arch / x86 / kernel / ftrace.c
index 16a571d..57b33ed 100644 (file)
 #include <linux/init.h>
 #include <linux/list.h>
 
+#include <asm/cacheflush.h>
 #include <asm/ftrace.h>
 #include <linux/ftrace.h>
 #include <asm/nops.h>
 #include <asm/nmi.h>
 
 
+#ifdef CONFIG_DYNAMIC_FTRACE
 
-#ifdef CONFIG_FUNCTION_RET_TRACER
-
-/*
- * These functions are picked from those used on
- * this page for dynamic ftrace. They have been
- * simplified to ignore all traces in NMI context.
- */
-static atomic_t in_nmi;
-
-void ftrace_nmi_enter(void)
-{
-       atomic_inc(&in_nmi);
-}
-
-void ftrace_nmi_exit(void)
-{
-       atomic_dec(&in_nmi);
-}
-
-/*
- * Synchronize accesses to return adresses stack with
- * interrupts.
- */
-static raw_spinlock_t ret_stack_lock;
-
-/* Add a function return address to the trace stack on thread info.*/
-static int push_return_trace(unsigned long ret, unsigned long long time,
-                               unsigned long func)
-{
-       int index;
-       struct thread_info *ti;
-       unsigned long flags;
-       int err = 0;
-
-       raw_local_irq_save(flags);
-       __raw_spin_lock(&ret_stack_lock);
-
-       ti = current_thread_info();
-       /* The return trace stack is full */
-       if (ti->curr_ret_stack == FTRACE_RET_STACK_SIZE - 1) {
-               err = -EBUSY;
-               goto out;
-       }
-
-       index = ++ti->curr_ret_stack;
-       ti->ret_stack[index].ret = ret;
-       ti->ret_stack[index].func = func;
-       ti->ret_stack[index].calltime = time;
-
-out:
-       __raw_spin_unlock(&ret_stack_lock);
-       raw_local_irq_restore(flags);
-       return err;
-}
-
-/* Retrieve a function return address to the trace stack on thread info.*/
-static void pop_return_trace(unsigned long *ret, unsigned long long *time,
-                               unsigned long *func)
-{
-       struct thread_info *ti;
-       int index;
-       unsigned long flags;
-
-       raw_local_irq_save(flags);
-       __raw_spin_lock(&ret_stack_lock);
-
-       ti = current_thread_info();
-       index = ti->curr_ret_stack;
-       *ret = ti->ret_stack[index].ret;
-       *func = ti->ret_stack[index].func;
-       *time = ti->ret_stack[index].calltime;
-       ti->curr_ret_stack--;
-
-       __raw_spin_unlock(&ret_stack_lock);
-       raw_local_irq_restore(flags);
-}
-
-/*
- * Send the trace to the ring-buffer.
- * @return the original return address.
- */
-unsigned long ftrace_return_to_handler(void)
-{
-       struct ftrace_retfunc trace;
-       pop_return_trace(&trace.ret, &trace.calltime, &trace.func);
-       trace.rettime = cpu_clock(raw_smp_processor_id());
-       ftrace_function_return(&trace);
-
-       return trace.ret;
-}
-
-/*
- * Hook the return address and push it in the stack of return addrs
- * in current thread info.
- */
-asmlinkage
-void prepare_ftrace_return(unsigned long *parent, unsigned long self_addr)
+int ftrace_arch_code_modify_prepare(void)
 {
-       unsigned long old;
-       unsigned long long calltime;
-       int faulted;
-       unsigned long return_hooker = (unsigned long)
-                               &return_to_handler;
-
-       /* Nmi's are currently unsupported */
-       if (atomic_read(&in_nmi))
-               return;
-
-       /*
-        * Protect against fault, even if it shouldn't
-        * happen. This tool is too much intrusive to
-        * ignore such a protection.
-        */
-       asm volatile(
-               "1: movl (%[parent_old]), %[old]\n"
-               "2: movl %[return_hooker], (%[parent_replaced])\n"
-               "   movl $0, %[faulted]\n"
-
-               ".section .fixup, \"ax\"\n"
-               "3: movl $1, %[faulted]\n"
-               ".previous\n"
-
-               ".section __ex_table, \"a\"\n"
-               "   .long 1b, 3b\n"
-               "   .long 2b, 3b\n"
-               ".previous\n"
-
-               : [parent_replaced] "=r" (parent), [old] "=r" (old),
-                 [faulted] "=r" (faulted)
-               : [parent_old] "0" (parent), [return_hooker] "r" (return_hooker)
-               : "memory"
-       );
-
-       if (WARN_ON(faulted)) {
-               unregister_ftrace_return();
-               return;
-       }
-
-       if (WARN_ON(!__kernel_text_address(old))) {
-               unregister_ftrace_return();
-               *parent = old;
-               return;
-       }
-
-       calltime = cpu_clock(raw_smp_processor_id());
-
-       if (push_return_trace(old, calltime, self_addr) == -EBUSY)
-               *parent = old;
+       set_kernel_text_rw();
+       return 0;
 }
 
-static int __init init_ftrace_function_return(void)
+int ftrace_arch_code_modify_post_process(void)
 {
-       ret_stack_lock = (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
+       set_kernel_text_ro();
        return 0;
 }
-device_initcall(init_ftrace_function_return);
-
-
-#endif
-
-#ifdef CONFIG_DYNAMIC_FTRACE
 
 union ftrace_code_union {
        char code[MCOUNT_INSN_SIZE];
@@ -200,7 +52,7 @@ static int ftrace_calc_offset(long ip, long addr)
        return (int)(addr - ip);
 }
 
-unsigned char *ftrace_call_replace(unsigned long ip, unsigned long addr)
+static unsigned char *ftrace_call_replace(unsigned long ip, unsigned long addr)
 {
        static union ftrace_code_union calc;
 
@@ -227,11 +79,11 @@ unsigned char *ftrace_call_replace(unsigned long ip, unsigned long addr)
  *
  * 1) Put the instruction pointer into the IP buffer
  *    and the new code into the "code" buffer.
- * 2) Set a flag that says we are modifying code
- * 3) Wait for any running NMIs to finish.
- * 4) Write the code
- * 5) clear the flag.
- * 6) Wait for any running NMIs to finish.
+ * 2) Wait for any running NMIs to finish and set a flag that says
+ *    we are modifying code, it is done in an atomic operation.
+ * 3) Write the code
+ * 4) clear the flag.
+ * 5) Wait for any running NMIs to finish.
  *
  * If an NMI is executed, the first thing it does is to call
  * "ftrace_nmi_enter". This will check if the flag is set to write
@@ -243,9 +95,9 @@ unsigned char *ftrace_call_replace(unsigned long ip, unsigned long addr)
  * are the same as what exists.
  */
 
-static atomic_t in_nmi = ATOMIC_INIT(0);
+#define MOD_CODE_WRITE_FLAG (1 << 31)  /* set when NMI should do the write */
+static atomic_t nmi_running = ATOMIC_INIT(0);
 static int mod_code_status;            /* holds return value of text write */
-static int mod_code_write;             /* set when NMI should do the write */
 static void *mod_code_ip;              /* holds the IP to write to */
 static void *mod_code_newcode;         /* holds the text to write to the IP */
 
@@ -262,6 +114,20 @@ int ftrace_arch_read_dyn_info(char *buf, int size)
        return r;
 }
 
+static void clear_mod_flag(void)
+{
+       int old = atomic_read(&nmi_running);
+
+       for (;;) {
+               int new = old & ~MOD_CODE_WRITE_FLAG;
+
+               if (old == new)
+                       break;
+
+               old = atomic_cmpxchg(&nmi_running, old, new);
+       }
+}
+
 static void ftrace_mod_code(void)
 {
        /*
@@ -273,37 +139,51 @@ static void ftrace_mod_code(void)
        mod_code_status = probe_kernel_write(mod_code_ip, mod_code_newcode,
                                             MCOUNT_INSN_SIZE);
 
+       /* if we fail, then kill any new writers */
+       if (mod_code_status)
+               clear_mod_flag();
 }
 
 void ftrace_nmi_enter(void)
 {
-       atomic_inc(&in_nmi);
-       /* Must have in_nmi seen before reading write flag */
-       smp_mb();
-       if (mod_code_write) {
+       if (atomic_inc_return(&nmi_running) & MOD_CODE_WRITE_FLAG) {
+               smp_rmb();
                ftrace_mod_code();
                atomic_inc(&nmi_update_count);
        }
+       /* Must have previous changes seen before executions */
+       smp_mb();
 }
 
 void ftrace_nmi_exit(void)
 {
-       /* Finish all executions before clearing in_nmi */
-       smp_wmb();
-       atomic_dec(&in_nmi);
+       /* Finish all executions before clearing nmi_running */
+       smp_mb();
+       atomic_dec(&nmi_running);
+}
+
+static void wait_for_nmi_and_set_mod_flag(void)
+{
+       if (!atomic_cmpxchg(&nmi_running, 0, MOD_CODE_WRITE_FLAG))
+               return;
+
+       do {
+               cpu_relax();
+       } while (atomic_cmpxchg(&nmi_running, 0, MOD_CODE_WRITE_FLAG));
+
+       nmi_wait_count++;
 }
 
 static void wait_for_nmi(void)
 {
-       int waited = 0;
+       if (!atomic_read(&nmi_running))
+               return;
 
-       while (atomic_read(&in_nmi)) {
-               waited = 1;
+       do {
                cpu_relax();
-       }
+       } while (atomic_read(&nmi_running));
 
-       if (waited)
-               nmi_wait_count++;
+       nmi_wait_count++;
 }
 
 static int
@@ -313,14 +193,9 @@ do_ftrace_mod_code(unsigned long ip, void *new_code)
        mod_code_newcode = new_code;
 
        /* The buffers need to be visible before we let NMIs write them */
-       smp_wmb();
-
-       mod_code_write = 1;
-
-       /* Make sure write bit is visible before we wait on NMIs */
        smp_mb();
 
-       wait_for_nmi();
+       wait_for_nmi_and_set_mod_flag();
 
        /* Make sure all running NMIs have finished before we write the code */
        smp_mb();
@@ -328,13 +203,9 @@ do_ftrace_mod_code(unsigned long ip, void *new_code)
        ftrace_mod_code();
 
        /* Make sure the write happens before clearing the bit */
-       smp_wmb();
-
-       mod_code_write = 0;
-
-       /* make sure NMIs see the cleared bit */
        smp_mb();
 
+       clear_mod_flag();
        wait_for_nmi();
 
        return mod_code_status;
@@ -345,12 +216,12 @@ do_ftrace_mod_code(unsigned long ip, void *new_code)
 
 static unsigned char ftrace_nop[MCOUNT_INSN_SIZE];
 
-unsigned char *ftrace_nop_replace(void)
+static unsigned char *ftrace_nop_replace(void)
 {
        return ftrace_nop;
 }
 
-int
+static int
 ftrace_modify_code(unsigned long ip, unsigned char *old_code,
                   unsigned char *new_code)
 {
@@ -383,6 +254,29 @@ ftrace_modify_code(unsigned long ip, unsigned char *old_code,
        return 0;
 }
 
+int ftrace_make_nop(struct module *mod,
+                   struct dyn_ftrace *rec, unsigned long addr)
+{
+       unsigned char *new, *old;
+       unsigned long ip = rec->ip;
+
+       old = ftrace_call_replace(ip, addr);
+       new = ftrace_nop_replace();
+
+       return ftrace_modify_code(rec->ip, old, new);
+}
+
+int ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr)
+{
+       unsigned char *new, *old;
+       unsigned long ip = rec->ip;
+
+       old = ftrace_nop_replace();
+       new = ftrace_call_replace(ip, addr);
+
+       return ftrace_modify_code(rec->ip, old, new);
+}
+
 int ftrace_update_ftrace_func(ftrace_func_t func)
 {
        unsigned long ip = (unsigned long)(&ftrace_call);
@@ -460,3 +354,182 @@ int __init ftrace_dyn_arch_init(void *data)
        return 0;
 }
 #endif
+
+#ifdef CONFIG_FUNCTION_GRAPH_TRACER
+
+#ifdef CONFIG_DYNAMIC_FTRACE
+extern void ftrace_graph_call(void);
+
+static int ftrace_mod_jmp(unsigned long ip,
+                         int old_offset, int new_offset)
+{
+       unsigned char code[MCOUNT_INSN_SIZE];
+
+       if (probe_kernel_read(code, (void *)ip, MCOUNT_INSN_SIZE))
+               return -EFAULT;
+
+       if (code[0] != 0xe9 || old_offset != *(int *)(&code[1]))
+               return -EINVAL;
+
+       *(int *)(&code[1]) = new_offset;
+
+       if (do_ftrace_mod_code(ip, &code))
+               return -EPERM;
+
+       return 0;
+}
+
+int ftrace_enable_ftrace_graph_caller(void)
+{
+       unsigned long ip = (unsigned long)(&ftrace_graph_call);
+       int old_offset, new_offset;
+
+       old_offset = (unsigned long)(&ftrace_stub) - (ip + MCOUNT_INSN_SIZE);
+       new_offset = (unsigned long)(&ftrace_graph_caller) - (ip + MCOUNT_INSN_SIZE);
+
+       return ftrace_mod_jmp(ip, old_offset, new_offset);
+}
+
+int ftrace_disable_ftrace_graph_caller(void)
+{
+       unsigned long ip = (unsigned long)(&ftrace_graph_call);
+       int old_offset, new_offset;
+
+       old_offset = (unsigned long)(&ftrace_graph_caller) - (ip + MCOUNT_INSN_SIZE);
+       new_offset = (unsigned long)(&ftrace_stub) - (ip + MCOUNT_INSN_SIZE);
+
+       return ftrace_mod_jmp(ip, old_offset, new_offset);
+}
+
+#endif /* !CONFIG_DYNAMIC_FTRACE */
+
+/*
+ * Hook the return address and push it in the stack of return addrs
+ * in current thread info.
+ */
+void prepare_ftrace_return(unsigned long *parent, unsigned long self_addr)
+{
+       unsigned long old;
+       unsigned long long calltime;
+       int faulted;
+       struct ftrace_graph_ent trace;
+       unsigned long return_hooker = (unsigned long)
+                               &return_to_handler;
+
+       /* Nmi's are currently unsupported */
+       if (unlikely(in_nmi()))
+               return;
+
+       if (unlikely(atomic_read(&current->tracing_graph_pause)))
+               return;
+
+       /*
+        * Protect against fault, even if it shouldn't
+        * happen. This tool is too much intrusive to
+        * ignore such a protection.
+        */
+       asm volatile(
+               "1: " _ASM_MOV " (%[parent]), %[old]\n"
+               "2: " _ASM_MOV " %[return_hooker], (%[parent])\n"
+               "   movl $0, %[faulted]\n"
+               "3:\n"
+
+               ".section .fixup, \"ax\"\n"
+               "4: movl $1, %[faulted]\n"
+               "   jmp 3b\n"
+               ".previous\n"
+
+               _ASM_EXTABLE(1b, 4b)
+               _ASM_EXTABLE(2b, 4b)
+
+               : [old] "=r" (old), [faulted] "=r" (faulted)
+               : [parent] "r" (parent), [return_hooker] "r" (return_hooker)
+               : "memory"
+       );
+
+       if (unlikely(faulted)) {
+               ftrace_graph_stop();
+               WARN_ON(1);
+               return;
+       }
+
+       calltime = trace_clock_local();
+
+       if (ftrace_push_return_trace(old, calltime,
+                               self_addr, &trace.depth) == -EBUSY) {
+               *parent = old;
+               return;
+       }
+
+       trace.func = self_addr;
+
+       /* Only trace if the calling function expects to */
+       if (!ftrace_graph_entry(&trace)) {
+               current->curr_ret_stack--;
+               *parent = old;
+       }
+}
+#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
+
+#ifdef CONFIG_FTRACE_SYSCALLS
+
+extern unsigned long __start_syscalls_metadata[];
+extern unsigned long __stop_syscalls_metadata[];
+extern unsigned long *sys_call_table;
+
+static struct syscall_metadata **syscalls_metadata;
+
+static struct syscall_metadata *find_syscall_meta(unsigned long *syscall)
+{
+       struct syscall_metadata *start;
+       struct syscall_metadata *stop;
+       char str[KSYM_SYMBOL_LEN];
+
+
+       start = (struct syscall_metadata *)__start_syscalls_metadata;
+       stop = (struct syscall_metadata *)__stop_syscalls_metadata;
+       kallsyms_lookup((unsigned long) syscall, NULL, NULL, NULL, str);
+
+       for ( ; start < stop; start++) {
+               if (start->name && !strcmp(start->name, str))
+                       return start;
+       }
+       return NULL;
+}
+
+struct syscall_metadata *syscall_nr_to_meta(int nr)
+{
+       if (!syscalls_metadata || nr >= FTRACE_SYSCALL_MAX || nr < 0)
+               return NULL;
+
+       return syscalls_metadata[nr];
+}
+
+void arch_init_ftrace_syscalls(void)
+{
+       int i;
+       struct syscall_metadata *meta;
+       unsigned long **psys_syscall_table = &sys_call_table;
+       static atomic_t refs;
+
+       if (atomic_inc_return(&refs) != 1)
+               goto end;
+
+       syscalls_metadata = kzalloc(sizeof(*syscalls_metadata) *
+                                       FTRACE_SYSCALL_MAX, GFP_KERNEL);
+       if (!syscalls_metadata) {
+               WARN_ON(1);
+               return;
+       }
+
+       for (i = 0; i < FTRACE_SYSCALL_MAX; i++) {
+               meta = find_syscall_meta(psys_syscall_table[i]);
+               syscalls_metadata[i] = meta;
+       }
+       return;
+
+       /* Paranoid: avoid overflow */
+end:
+       atomic_dec(&refs);
+}
+#endif