perf_events: Undo some recursion damage
[safe/jmp/linux-2.6] / kernel / trace / trace_kprobe.c
index 3313fa7..79ce6a2 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * kprobe based kernel tracer
+ * Kprobes-based tracing events
  *
  * Created by Masami Hiramatsu <mhiramat@redhat.com>
  *
 #define MAX_EVENT_NAME_LEN 64
 #define KPROBE_EVENT_SYSTEM "kprobes"
 
-/* currently, trace_kprobe only supports X86. */
+/* Reserved field names */
+#define FIELD_STRING_IP "__probe_ip"
+#define FIELD_STRING_NARGS "__probe_nargs"
+#define FIELD_STRING_RETIP "__probe_ret_ip"
+#define FIELD_STRING_FUNC "__probe_func"
+
+const char *reserved_field_names[] = {
+       "common_type",
+       "common_flags",
+       "common_preempt_count",
+       "common_pid",
+       "common_tgid",
+       "common_lock_depth",
+       FIELD_STRING_IP,
+       FIELD_STRING_NARGS,
+       FIELD_STRING_RETIP,
+       FIELD_STRING_FUNC,
+};
 
 struct fetch_func {
        unsigned long (*func)(struct pt_regs *, void *);
@@ -172,7 +189,7 @@ static __kprobes void free_indirect_fetch_data(struct indirect_fetch_data *data)
 }
 
 /**
- * Kprobe tracer core functions
+ * Kprobe event core functions
  */
 
 struct probe_arg {
@@ -334,12 +351,14 @@ static void free_trace_probe(struct trace_probe *tp)
        kfree(tp);
 }
 
-static struct trace_probe *find_probe_event(const char *event)
+static struct trace_probe *find_probe_event(const char *event,
+                                           const char *group)
 {
        struct trace_probe *tp;
 
        list_for_each_entry(tp, &probe_list, list)
-               if (!strcmp(tp->call.name, event))
+               if (strcmp(tp->call.name, event) == 0 &&
+                   strcmp(tp->call.system, group) == 0)
                        return tp;
        return NULL;
 }
@@ -364,7 +383,7 @@ static int register_trace_probe(struct trace_probe *tp)
        mutex_lock(&probe_lock);
 
        /* register as an event */
-       old_tp = find_probe_event(tp->call.name);
+       old_tp = find_probe_event(tp->call.name, tp->call.system);
        if (old_tp) {
                /* delete old event */
                unregister_trace_probe(old_tp);
@@ -537,6 +556,20 @@ static int parse_probe_arg(char *arg, struct fetch_func *ff, int is_return)
        return ret;
 }
 
+/* Return 1 if name is reserved or already used by another argument */
+static int conflict_field_name(const char *name,
+                              struct probe_arg *args, int narg)
+{
+       int i;
+       for (i = 0; i < ARRAY_SIZE(reserved_field_names); i++)
+               if (strcmp(reserved_field_names[i], name) == 0)
+                       return 1;
+       for (i = 0; i < narg; i++)
+               if (strcmp(args[i].name, name) == 0)
+                       return 1;
+       return 0;
+}
+
 static int create_trace_probe(int argc, char **argv)
 {
        /*
@@ -564,15 +597,19 @@ static int create_trace_probe(int argc, char **argv)
        void *addr = NULL;
        char buf[MAX_EVENT_NAME_LEN];
 
-       if (argc < 2)
+       if (argc < 2) {
+               pr_info("Probe point is not specified.\n");
                return -EINVAL;
+       }
 
        if (argv[0][0] == 'p')
                is_return = 0;
        else if (argv[0][0] == 'r')
                is_return = 1;
-       else
+       else {
+               pr_info("Probe definition must be started with 'p' or 'r'.\n");
                return -EINVAL;
+       }
 
        if (argv[0][1] == ':') {
                event = &argv[0][2];
@@ -592,21 +629,29 @@ static int create_trace_probe(int argc, char **argv)
        }
 
        if (isdigit(argv[1][0])) {
-               if (is_return)
+               if (is_return) {
+                       pr_info("Return probe point must be a symbol.\n");
                        return -EINVAL;
+               }
                /* an address specified */
                ret = strict_strtoul(&argv[0][2], 0, (unsigned long *)&addr);
-               if (ret)
+               if (ret) {
+                       pr_info("Failed to parse address.\n");
                        return ret;
+               }
        } else {
                /* a symbol specified */
                symbol = argv[1];
                /* TODO: support .init module functions */
                ret = split_symbol_offset(symbol, &offset);
-               if (ret)
+               if (ret) {
+                       pr_info("Failed to parse symbol.\n");
                        return ret;
-               if (offset && is_return)
+               }
+               if (offset && is_return) {
+                       pr_info("Return probe must be used without offset.\n");
                        return -EINVAL;
+               }
        }
        argc -= 2; argv += 2;
 
@@ -625,8 +670,11 @@ static int create_trace_probe(int argc, char **argv)
        }
        tp = alloc_trace_probe(group, event, addr, symbol, offset, argc,
                               is_return);
-       if (IS_ERR(tp))
+       if (IS_ERR(tp)) {
+               pr_info("Failed to allocate trace_probe.(%d)\n",
+                       (int)PTR_ERR(tp));
                return PTR_ERR(tp);
+       }
 
        /* parse arguments */
        ret = 0;
@@ -637,6 +685,14 @@ static int create_trace_probe(int argc, char **argv)
                        *arg++ = '\0';
                else
                        arg = argv[i];
+
+               if (conflict_field_name(argv[i], tp->args, i)) {
+                       pr_info("Argument%d name '%s' conflicts with "
+                               "another field.\n", i, argv[i]);
+                       ret = -EINVAL;
+                       goto error;
+               }
+
                tp->args[i].name = kstrdup(argv[i], GFP_KERNEL);
 
                /* Parse fetch argument */
@@ -646,8 +702,10 @@ static int create_trace_probe(int argc, char **argv)
                        goto error;
                }
                ret = parse_probe_arg(arg, &tp->args[i].fetch, is_return);
-               if (ret)
+               if (ret) {
+                       pr_info("Parse error at argument%d. (%d)\n", i, ret);
                        goto error;
+               }
        }
        tp->nr_args = i;
 
@@ -1039,8 +1097,8 @@ static int kprobe_event_define_fields(struct ftrace_event_call *event_call)
        if (!ret)
                return ret;
 
-       DEFINE_FIELD(unsigned long, ip, "ip", 0);
-       DEFINE_FIELD(int, nargs, "nargs", 1);
+       DEFINE_FIELD(unsigned long, ip, FIELD_STRING_IP, 0);
+       DEFINE_FIELD(int, nargs, FIELD_STRING_NARGS, 1);
        /* Set argument names as fields */
        for (i = 0; i < tp->nr_args; i++)
                DEFINE_FIELD(unsigned long, args[i], tp->args[i].name, 0);
@@ -1057,9 +1115,9 @@ static int kretprobe_event_define_fields(struct ftrace_event_call *event_call)
        if (!ret)
                return ret;
 
-       DEFINE_FIELD(unsigned long, func, "func", 0);
-       DEFINE_FIELD(unsigned long, ret_ip, "ret_ip", 0);
-       DEFINE_FIELD(int, nargs, "nargs", 1);
+       DEFINE_FIELD(unsigned long, func, FIELD_STRING_FUNC, 0);
+       DEFINE_FIELD(unsigned long, ret_ip, FIELD_STRING_RETIP, 0);
+       DEFINE_FIELD(int, nargs, FIELD_STRING_NARGS, 1);
        /* Set argument names as fields */
        for (i = 0; i < tp->nr_args; i++)
                DEFINE_FIELD(unsigned long, args[i], tp->args[i].name, 0);
@@ -1108,15 +1166,16 @@ static int kprobe_event_show_format(struct ftrace_event_call *call,
        int ret, i;
        struct trace_probe *tp = (struct trace_probe *)call->data;
 
-       SHOW_FIELD(unsigned long, ip, "ip");
-       SHOW_FIELD(int, nargs, "nargs");
+       SHOW_FIELD(unsigned long, ip, FIELD_STRING_IP);
+       SHOW_FIELD(int, nargs, FIELD_STRING_NARGS);
 
        /* Show fields */
        for (i = 0; i < tp->nr_args; i++)
                SHOW_FIELD(unsigned long, args[i], tp->args[i].name);
        trace_seq_puts(s, "\n");
 
-       return __probe_event_show_format(s, tp, "(%lx)", "REC->ip");
+       return __probe_event_show_format(s, tp, "(%lx)",
+                                        "REC->" FIELD_STRING_IP);
 }
 
 static int kretprobe_event_show_format(struct ftrace_event_call *call,
@@ -1126,9 +1185,9 @@ static int kretprobe_event_show_format(struct ftrace_event_call *call,
        int ret, i;
        struct trace_probe *tp = (struct trace_probe *)call->data;
 
-       SHOW_FIELD(unsigned long, func, "func");
-       SHOW_FIELD(unsigned long, ret_ip, "ret_ip");
-       SHOW_FIELD(int, nargs, "nargs");
+       SHOW_FIELD(unsigned long, func, FIELD_STRING_FUNC);
+       SHOW_FIELD(unsigned long, ret_ip, FIELD_STRING_RETIP);
+       SHOW_FIELD(int, nargs, FIELD_STRING_NARGS);
 
        /* Show fields */
        for (i = 0; i < tp->nr_args; i++)
@@ -1136,7 +1195,8 @@ static int kretprobe_event_show_format(struct ftrace_event_call *call,
        trace_seq_puts(s, "\n");
 
        return __probe_event_show_format(s, tp, "(%lx <- %lx)",
-                                         "REC->func, REC->ret_ip");
+                                        "REC->" FIELD_STRING_FUNC
+                                        ", REC->" FIELD_STRING_RETIP);
 }
 
 #ifdef CONFIG_EVENT_PROFILE
@@ -1151,7 +1211,9 @@ static __kprobes int kprobe_profile_func(struct kprobe *kp,
        struct trace_entry *ent;
        int size, __size, i, pc, __cpu;
        unsigned long irq_flags;
+       char *trace_buf;
        char *raw_data;
+       int rctx;
 
        pc = preempt_count();
        __size = SIZEOF_KPROBE_TRACE_ENTRY(tp->nr_args);
@@ -1166,17 +1228,23 @@ static __kprobes int kprobe_profile_func(struct kprobe *kp,
         * This also protects the rcu read side
         */
        local_irq_save(irq_flags);
+
+       rctx = perf_swevent_get_recursion_context();
+       if (rctx < 0)
+               goto end_recursion;
+
        __cpu = smp_processor_id();
 
        if (in_nmi())
-               raw_data = rcu_dereference(trace_profile_buf_nmi);
+               trace_buf = rcu_dereference(perf_trace_buf_nmi);
        else
-               raw_data = rcu_dereference(trace_profile_buf);
+               trace_buf = rcu_dereference(perf_trace_buf);
 
-       if (!raw_data)
+       if (!trace_buf)
                goto end;
 
-       raw_data = per_cpu_ptr(raw_data, __cpu);
+       raw_data = per_cpu_ptr(trace_buf, __cpu);
+
        /* Zero dead bytes from alignment to avoid buffer leak to userspace */
        *(u64 *)(&raw_data[size - sizeof(u64)]) = 0ULL;
        entry = (struct kprobe_trace_entry *)raw_data;
@@ -1189,8 +1257,12 @@ static __kprobes int kprobe_profile_func(struct kprobe *kp,
        for (i = 0; i < tp->nr_args; i++)
                entry->args[i] = call_fetch(&tp->args[i].fetch, regs);
        perf_tp_event(call->id, entry->ip, 1, entry, size);
+
 end:
+       perf_swevent_put_recursion_context(rctx);
+end_recursion:
        local_irq_restore(irq_flags);
+
        return 0;
 }
 
@@ -1204,7 +1276,9 @@ static __kprobes int kretprobe_profile_func(struct kretprobe_instance *ri,
        struct trace_entry *ent;
        int size, __size, i, pc, __cpu;
        unsigned long irq_flags;
+       char *trace_buf;
        char *raw_data;
+       int rctx;
 
        pc = preempt_count();
        __size = SIZEOF_KRETPROBE_TRACE_ENTRY(tp->nr_args);
@@ -1219,17 +1293,23 @@ static __kprobes int kretprobe_profile_func(struct kretprobe_instance *ri,
         * This also protects the rcu read side
         */
        local_irq_save(irq_flags);
+
+       rctx = perf_swevent_get_recursion_context();
+       if (rctx < 0)
+               goto end_recursion;
+
        __cpu = smp_processor_id();
 
        if (in_nmi())
-               raw_data = rcu_dereference(trace_profile_buf_nmi);
+               trace_buf = rcu_dereference(perf_trace_buf_nmi);
        else
-               raw_data = rcu_dereference(trace_profile_buf);
+               trace_buf = rcu_dereference(perf_trace_buf);
 
-       if (!raw_data)
+       if (!trace_buf)
                goto end;
 
-       raw_data = per_cpu_ptr(raw_data, __cpu);
+       raw_data = per_cpu_ptr(trace_buf, __cpu);
+
        /* Zero dead bytes from alignment to avoid buffer leak to userspace */
        *(u64 *)(&raw_data[size - sizeof(u64)]) = 0ULL;
        entry = (struct kretprobe_trace_entry *)raw_data;
@@ -1243,8 +1323,12 @@ static __kprobes int kretprobe_profile_func(struct kretprobe_instance *ri,
        for (i = 0; i < tp->nr_args; i++)
                entry->args[i] = call_fetch(&tp->args[i].fetch, regs);
        perf_tp_event(call->id, entry->ret_ip, 1, entry, size);
+
 end:
+       perf_swevent_put_recursion_context(rctx);
+end_recursion:
        local_irq_restore(irq_flags);
+
        return 0;
 }
 
@@ -1397,12 +1481,12 @@ static __init int kprobe_trace_self_tests_init(void)
        pr_info("Testing kprobe tracing: ");
 
        ret = command_trace_probe("p:testprobe kprobe_trace_selftest_target "
-                                 "a1 a2 a3 a4 a5 a6");
+                                 "$arg1 $arg2 $arg3 $arg4 $stack $stack0");
        if (WARN_ON_ONCE(ret))
                pr_warning("error enabling function entry\n");
 
        ret = command_trace_probe("r:testprobe2 kprobe_trace_selftest_target "
-                                 "ra rv");
+                                 "$retval");
        if (WARN_ON_ONCE(ret))
                pr_warning("error enabling function return\n");