]> rtime.felk.cvut.cz Git - linux-imx.git/blobdiff - kernel/trace/trace_kprobe.c
tracing/kprobes: Kill probe_enable_lock
[linux-imx.git] / kernel / trace / trace_kprobe.c
index 636d45fe69b37a80eefafe6b3aa6d46220f132dd..282f86cfd30451d7cb23ca57cebbd7875c305202 100644 (file)
@@ -35,7 +35,7 @@ struct trace_probe {
        const char              *symbol;        /* symbol name */
        struct ftrace_event_class       class;
        struct ftrace_event_call        call;
-       struct ftrace_event_file        **files;
+       struct ftrace_event_file * __rcu *files;
        ssize_t                 size;           /* trace entry size */
        unsigned int            nr_args;
        struct probe_arg        args[];
@@ -183,9 +183,13 @@ static struct trace_probe *find_trace_probe(const char *event,
        return NULL;
 }
 
+/*
+ * This and enable_trace_probe/disable_trace_probe rely on event_mutex
+ * held by the caller, __ftrace_set_clr_event().
+ */
 static int trace_probe_nr_files(struct trace_probe *tp)
 {
-       struct ftrace_event_file **file = tp->files;
+       struct ftrace_event_file **file = rcu_dereference_raw(tp->files);
        int ret = 0;
 
        if (file)
@@ -195,8 +199,6 @@ static int trace_probe_nr_files(struct trace_probe *tp)
        return ret;
 }
 
-static DEFINE_MUTEX(probe_enable_lock);
-
 /*
  * Enable trace_probe
  * if the file is NULL, enable "perf" handler, or enable "trace" handler.
@@ -206,18 +208,17 @@ enable_trace_probe(struct trace_probe *tp, struct ftrace_event_file *file)
 {
        int ret = 0;
 
-       mutex_lock(&probe_enable_lock);
-
        if (file) {
-               struct ftrace_event_file **new, **old = tp->files;
+               struct ftrace_event_file **new, **old;
                int n = trace_probe_nr_files(tp);
 
+               old = rcu_dereference_raw(tp->files);
                /* 1 is for new one and 1 is for stopper */
                new = kzalloc((n + 2) * sizeof(struct ftrace_event_file *),
                              GFP_KERNEL);
                if (!new) {
                        ret = -ENOMEM;
-                       goto out_unlock;
+                       goto out;
                }
                memcpy(new, old, n * sizeof(struct ftrace_event_file *));
                new[n] = file;
@@ -234,28 +235,30 @@ enable_trace_probe(struct trace_probe *tp, struct ftrace_event_file *file)
        } else
                tp->flags |= TP_FLAG_PROFILE;
 
-       if (trace_probe_is_enabled(tp) && trace_probe_is_registered(tp) &&
-           !trace_probe_has_gone(tp)) {
+       if (trace_probe_is_registered(tp) && !trace_probe_has_gone(tp)) {
                if (trace_probe_is_return(tp))
                        ret = enable_kretprobe(&tp->rp);
                else
                        ret = enable_kprobe(&tp->rp.kp);
        }
-
- out_unlock:
-       mutex_unlock(&probe_enable_lock);
-
+ out:
        return ret;
 }
 
 static int
 trace_probe_file_index(struct trace_probe *tp, struct ftrace_event_file *file)
 {
+       struct ftrace_event_file **files;
        int i;
 
-       if (tp->files) {
-               for (i = 0; tp->files[i]; i++)
-                       if (tp->files[i] == file)
+       /*
+        * Since all tp->files updater is protected by probe_enable_lock,
+        * we don't need to lock an rcu_read_lock.
+        */
+       files = rcu_dereference_raw(tp->files);
+       if (files) {
+               for (i = 0; files[i]; i++)
+                       if (files[i] == file)
                                return i;
        }
 
@@ -271,16 +274,15 @@ disable_trace_probe(struct trace_probe *tp, struct ftrace_event_file *file)
 {
        int ret = 0;
 
-       mutex_lock(&probe_enable_lock);
-
        if (file) {
-               struct ftrace_event_file **new, **old = tp->files;
+               struct ftrace_event_file **new, **old;
                int n = trace_probe_nr_files(tp);
                int i, j;
 
+               old = rcu_dereference_raw(tp->files);
                if (n == 0 || trace_probe_file_index(tp, file) < 0) {
                        ret = -EINVAL;
-                       goto out_unlock;
+                       goto out;
                }
 
                if (n == 1) {   /* Remove the last file */
@@ -291,7 +293,7 @@ disable_trace_probe(struct trace_probe *tp, struct ftrace_event_file *file)
                                      GFP_KERNEL);
                        if (!new) {
                                ret = -ENOMEM;
-                               goto out_unlock;
+                               goto out;
                        }
 
                        /* This copy & check loop copies the NULL stopper too */
@@ -314,10 +316,7 @@ disable_trace_probe(struct trace_probe *tp, struct ftrace_event_file *file)
                else
                        disable_kprobe(&tp->rp.kp);
        }
-
- out_unlock:
-       mutex_unlock(&probe_enable_lock);
-
+ out:
        return ret;
 }
 
@@ -872,9 +871,16 @@ __kprobe_trace_func(struct trace_probe *tp, struct pt_regs *regs,
 static __kprobes void
 kprobe_trace_func(struct trace_probe *tp, struct pt_regs *regs)
 {
-       struct ftrace_event_file **file = tp->files;
+       /*
+        * Note: preempt is already disabled around the kprobe handler.
+        * However, we still need an smp_read_barrier_depends() corresponding
+        * to smp_wmb() in rcu_assign_pointer() to access the pointer.
+        */
+       struct ftrace_event_file **file = rcu_dereference_raw(tp->files);
+
+       if (unlikely(!file))
+               return;
 
-       /* Note: preempt is already disabled around the kprobe handler */
        while (*file) {
                __kprobe_trace_func(tp, regs, *file);
                file++;
@@ -925,9 +931,16 @@ static __kprobes void
 kretprobe_trace_func(struct trace_probe *tp, struct kretprobe_instance *ri,
                     struct pt_regs *regs)
 {
-       struct ftrace_event_file **file = tp->files;
+       /*
+        * Note: preempt is already disabled around the kprobe handler.
+        * However, we still need an smp_read_barrier_depends() corresponding
+        * to smp_wmb() in rcu_assign_pointer() to access the pointer.
+        */
+       struct ftrace_event_file **file = rcu_dereference_raw(tp->files);
+
+       if (unlikely(!file))
+               return;
 
-       /* Note: preempt is already disabled around the kprobe handler */
        while (*file) {
                __kretprobe_trace_func(tp, ri, regs, *file);
                file++;
@@ -935,7 +948,7 @@ kretprobe_trace_func(struct trace_probe *tp, struct kretprobe_instance *ri,
 }
 
 /* Event entry printers */
-enum print_line_t
+static enum print_line_t
 print_kprobe_event(struct trace_iterator *iter, int flags,
                   struct trace_event *event)
 {
@@ -971,7 +984,7 @@ partial:
        return TRACE_TYPE_PARTIAL_LINE;
 }
 
-enum print_line_t
+static enum print_line_t
 print_kretprobe_event(struct trace_iterator *iter, int flags,
                      struct trace_event *event)
 {
@@ -1130,6 +1143,10 @@ kprobe_perf_func(struct trace_probe *tp, struct pt_regs *regs)
        int size, __size, dsize;
        int rctx;
 
+       head = this_cpu_ptr(call->perf_events);
+       if (hlist_empty(head))
+               return;
+
        dsize = __get_data_size(tp, regs);
        __size = sizeof(*entry) + tp->size + dsize;
        size = ALIGN(__size + sizeof(u32), sizeof(u64));
@@ -1145,8 +1162,6 @@ kprobe_perf_func(struct trace_probe *tp, struct pt_regs *regs)
        entry->ip = (unsigned long)tp->rp.kp.addr;
        memset(&entry[1], 0, dsize);
        store_trace_args(sizeof(*entry), tp, regs, (u8 *)&entry[1], dsize);
-
-       head = this_cpu_ptr(call->perf_events);
        perf_trace_buf_submit(entry, size, rctx,
                                        entry->ip, 1, regs, head, NULL);
 }
@@ -1162,6 +1177,10 @@ kretprobe_perf_func(struct trace_probe *tp, struct kretprobe_instance *ri,
        int size, __size, dsize;
        int rctx;
 
+       head = this_cpu_ptr(call->perf_events);
+       if (hlist_empty(head))
+               return;
+
        dsize = __get_data_size(tp, regs);
        __size = sizeof(*entry) + tp->size + dsize;
        size = ALIGN(__size + sizeof(u32), sizeof(u64));
@@ -1177,13 +1196,17 @@ kretprobe_perf_func(struct trace_probe *tp, struct kretprobe_instance *ri,
        entry->func = (unsigned long)tp->rp.kp.addr;
        entry->ret_ip = (unsigned long)ri->ret_addr;
        store_trace_args(sizeof(*entry), tp, regs, (u8 *)&entry[1], dsize);
-
-       head = this_cpu_ptr(call->perf_events);
        perf_trace_buf_submit(entry, size, rctx,
                                        entry->ret_ip, 1, regs, head, NULL);
 }
 #endif /* CONFIG_PERF_EVENTS */
 
+/*
+ * called by perf_trace_init() or __ftrace_set_clr_event() under event_mutex.
+ *
+ * kprobe_trace_self_tests_init() does enable_trace_probe/disable_trace_probe
+ * lockless, but we can't race with this __init function.
+ */
 static __kprobes
 int kprobe_register(struct ftrace_event_call *event,
                    enum trace_reg type, void *data)
@@ -1349,6 +1372,10 @@ find_trace_probe_file(struct trace_probe *tp, struct trace_array *tr)
        return NULL;
 }
 
+/*
+ * Nobody but us can call enable_trace_probe/disable_trace_probe at this
+ * stage, we can do this lockless.
+ */
 static __init int kprobe_trace_self_tests_init(void)
 {
        int ret, warn = 0;