drm/vmwgfx: return -EFAULT for copy_to_user errors
[safe/jmp/linux-2.6] / kernel / trace / trace_selftest.c
index 0c9aa14..250e7f9 100644 (file)
@@ -1,7 +1,9 @@
 /* Include in trace.c */
 
+#include <linux/stringify.h>
 #include <linux/kthread.h>
 #include <linux/delay.h>
+#include <linux/slab.h>
 
 static inline int trace_valid_entry(struct trace_entry *entry)
 {
@@ -15,6 +17,7 @@ static inline int trace_valid_entry(struct trace_entry *entry)
        case TRACE_BRANCH:
        case TRACE_GRAPH_ENT:
        case TRACE_GRAPH_RET:
+       case TRACE_KSYM:
                return 1;
        }
        return 0;
@@ -24,10 +27,20 @@ static int trace_test_buffer_cpu(struct trace_array *tr, int cpu)
 {
        struct ring_buffer_event *event;
        struct trace_entry *entry;
+       unsigned int loops = 0;
 
-       while ((event = ring_buffer_consume(tr->buffer, cpu, NULL))) {
+       while ((event = ring_buffer_consume(tr->buffer, cpu, NULL, NULL))) {
                entry = ring_buffer_event_data(event);
 
+               /*
+                * The ring buffer is a size of trace_buf_size, if
+                * we loop more than the size, there's something wrong
+                * with the ring buffer.
+                */
+               if (loops++ > trace_buf_size) {
+                       printk(KERN_CONT ".. bad ring buffer ");
+                       goto failed;
+               }
                if (!trace_valid_entry(entry)) {
                        printk(KERN_CONT ".. invalid entry %d ",
                                entry->type);
@@ -54,16 +67,25 @@ static int trace_test_buffer(struct trace_array *tr, unsigned long *count)
 
        /* Don't allow flipping of max traces now */
        local_irq_save(flags);
-       __raw_spin_lock(&ftrace_max_lock);
+       arch_spin_lock(&ftrace_max_lock);
 
        cnt = ring_buffer_entries(tr->buffer);
 
+       /*
+        * The trace_test_buffer_cpu runs a while loop to consume all data.
+        * If the calling tracer is broken, and is constantly filling
+        * the buffer, this will run forever, and hard lock the box.
+        * We disable the ring buffer while we do this test to prevent
+        * a hard lock up.
+        */
+       tracing_off();
        for_each_possible_cpu(cpu) {
                ret = trace_test_buffer_cpu(tr, cpu);
                if (ret)
                        break;
        }
-       __raw_spin_unlock(&ftrace_max_lock);
+       tracing_on();
+       arch_spin_unlock(&ftrace_max_lock);
        local_irq_restore(flags);
 
        if (count)
@@ -81,9 +103,6 @@ static inline void warn_failed_init_tracer(struct tracer *trace, int init_ret)
 
 #ifdef CONFIG_DYNAMIC_FTRACE
 
-#define __STR(x) #x
-#define STR(x) __STR(x)
-
 /* Test dynamic code modification and ftrace filters */
 int trace_selftest_startup_dynamic_tracing(struct tracer *trace,
                                           struct trace_array *tr,
@@ -107,11 +126,11 @@ int trace_selftest_startup_dynamic_tracing(struct tracer *trace,
        func();
 
        /*
-        * Some archs *cough*PowerPC*cough* add charachters to the
+        * Some archs *cough*PowerPC*cough* add characters to the
         * start of the function names. We simply put a '*' to
-        * accomodate them.
+        * accommodate them.
         */
-       func_name = "*" STR(DYN_FTRACE_TEST_NAME);
+       func_name = "*" __stringify(DYN_FTRACE_TEST_NAME);
 
        /* filter only on our function */
        ftrace_set_filter(func_name, strlen(func_name), 1);
@@ -171,6 +190,7 @@ int trace_selftest_startup_dynamic_tracing(struct tracer *trace,
 #else
 # define trace_selftest_startup_dynamic_tracing(trace, tr, func) ({ 0; })
 #endif /* CONFIG_DYNAMIC_FTRACE */
+
 /*
  * Simple verification test of ftrace function tracer.
  * Enable ftrace, sleep 1/10 second, and then read the trace
@@ -231,6 +251,29 @@ trace_selftest_startup_function(struct tracer *trace, struct trace_array *tr)
 
 
 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
+
+/* Maximum number of functions to trace before diagnosing a hang */
+#define GRAPH_MAX_FUNC_TEST    100000000
+
+static void
+__ftrace_dump(bool disable_tracing, enum ftrace_dump_mode oops_dump_mode);
+static unsigned int graph_hang_thresh;
+
+/* Wrap the real function entry probe to avoid possible hanging */
+static int trace_graph_entry_watchdog(struct ftrace_graph_ent *trace)
+{
+       /* This is harmlessly racy, we want to approximately detect a hang */
+       if (unlikely(++graph_hang_thresh > GRAPH_MAX_FUNC_TEST)) {
+               ftrace_graph_stop();
+               printk(KERN_WARNING "BUG: Function graph tracer hang!\n");
+               if (ftrace_dump_on_oops)
+                       __ftrace_dump(false, DUMP_ALL);
+               return 0;
+       }
+
+       return trace_graph_entry(trace);
+}
+
 /*
  * Pretty much the same than for the function tracer from which the selftest
  * has been borrowed.
@@ -242,15 +285,30 @@ trace_selftest_startup_function_graph(struct tracer *trace,
        int ret;
        unsigned long count;
 
-       ret = tracer_init(trace, tr);
+       /*
+        * Simulate the init() callback but we attach a watchdog callback
+        * to detect and recover from possible hangs
+        */
+       tracing_reset_online_cpus(tr);
+       set_graph_array(tr);
+       ret = register_ftrace_graph(&trace_graph_return,
+                                   &trace_graph_entry_watchdog);
        if (ret) {
                warn_failed_init_tracer(trace, ret);
                goto out;
        }
+       tracing_start_cmdline_record();
 
        /* Sleep for a 1/10 of a second */
        msleep(100);
 
+       /* Have we just recovered from a hang? */
+       if (graph_hang_thresh > GRAPH_MAX_FUNC_TEST) {
+               tracing_selftest_disabled = true;
+               ret = -1;
+               goto out;
+       }
+
        tracing_stop();
 
        /* check the trace buffer */
@@ -298,6 +356,14 @@ trace_selftest_startup_irqsoff(struct tracer *trace, struct trace_array *tr)
        local_irq_disable();
        udelay(100);
        local_irq_enable();
+
+       /*
+        * Stop the tracer to avoid a warning subsequent
+        * to buffer flipping failure because tracing_stop()
+        * disables the tr and max buffers, making flipping impossible
+        * in case of parallels max irqs off latencies.
+        */
+       trace->stop(tr);
        /* stop the tracing. */
        tracing_stop();
        /* check both trace buffers */
@@ -352,6 +418,14 @@ trace_selftest_startup_preemptoff(struct tracer *trace, struct trace_array *tr)
        preempt_disable();
        udelay(100);
        preempt_enable();
+
+       /*
+        * Stop the tracer to avoid a warning subsequent
+        * to buffer flipping failure because tracing_stop()
+        * disables the tr and max buffers, making flipping impossible
+        * in case of parallels max preempt off latencies.
+        */
+       trace->stop(tr);
        /* stop the tracing. */
        tracing_stop();
        /* check both trace buffers */
@@ -397,7 +471,7 @@ trace_selftest_startup_preemptirqsoff(struct tracer *trace, struct trace_array *
        ret = tracer_init(trace, tr);
        if (ret) {
                warn_failed_init_tracer(trace, ret);
-               goto out;
+               goto out_no_start;
        }
 
        /* reset the max latency */
@@ -411,31 +485,35 @@ trace_selftest_startup_preemptirqsoff(struct tracer *trace, struct trace_array *
        /* reverse the order of preempt vs irqs */
        local_irq_enable();
 
+       /*
+        * Stop the tracer to avoid a warning subsequent
+        * to buffer flipping failure because tracing_stop()
+        * disables the tr and max buffers, making flipping impossible
+        * in case of parallels max irqs/preempt off latencies.
+        */
+       trace->stop(tr);
        /* stop the tracing. */
        tracing_stop();
        /* check both trace buffers */
        ret = trace_test_buffer(tr, NULL);
-       if (ret) {
-               tracing_start();
+       if (ret)
                goto out;
-       }
 
        ret = trace_test_buffer(&max_tr, &count);
-       if (ret) {
-               tracing_start();
+       if (ret)
                goto out;
-       }
 
        if (!ret && !count) {
                printk(KERN_CONT ".. no entries found ..");
                ret = -1;
-               tracing_start();
                goto out;
        }
 
        /* do the test by disabling interrupts first this time */
        tracing_max_latency = 0;
        tracing_start();
+       trace->start(tr);
+
        preempt_disable();
        local_irq_disable();
        udelay(100);
@@ -443,6 +521,7 @@ trace_selftest_startup_preemptirqsoff(struct tracer *trace, struct trace_array *
        /* reverse the order of preempt vs irqs */
        local_irq_enable();
 
+       trace->stop(tr);
        /* stop the tracing. */
        tracing_stop();
        /* check both trace buffers */
@@ -458,9 +537,10 @@ trace_selftest_startup_preemptirqsoff(struct tracer *trace, struct trace_array *
                goto out;
        }
 
- out:
-       trace->reset(tr);
+out:
        tracing_start();
+out_no_start:
+       trace->reset(tr);
        tracing_max_latency = save_max;
 
        return ret;
@@ -622,7 +702,7 @@ trace_selftest_startup_sysprof(struct tracer *trace, struct trace_array *tr)
        ret = tracer_init(trace, tr);
        if (ret) {
                warn_failed_init_tracer(trace, ret);
-               return 0;
+               return ret;
        }
 
        /* Sleep for a 1/10 of a second */
@@ -634,6 +714,11 @@ trace_selftest_startup_sysprof(struct tracer *trace, struct trace_array *tr)
        trace->reset(tr);
        tracing_start();
 
+       if (!ret && !count) {
+               printk(KERN_CONT ".. no entries found ..");
+               ret = -1;
+       }
+
        return ret;
 }
 #endif /* CONFIG_SYSPROF_TRACER */
@@ -661,6 +746,65 @@ trace_selftest_startup_branch(struct tracer *trace, struct trace_array *tr)
        trace->reset(tr);
        tracing_start();
 
+       if (!ret && !count) {
+               printk(KERN_CONT ".. no entries found ..");
+               ret = -1;
+       }
+
        return ret;
 }
 #endif /* CONFIG_BRANCH_TRACER */
+
+#ifdef CONFIG_KSYM_TRACER
+static int ksym_selftest_dummy;
+
+int
+trace_selftest_startup_ksym(struct tracer *trace, struct trace_array *tr)
+{
+       unsigned long count;
+       int ret;
+
+       /* start the tracing */
+       ret = tracer_init(trace, tr);
+       if (ret) {
+               warn_failed_init_tracer(trace, ret);
+               return ret;
+       }
+
+       ksym_selftest_dummy = 0;
+       /* Register the read-write tracing request */
+
+       ret = process_new_ksym_entry("ksym_selftest_dummy",
+                                    HW_BREAKPOINT_R | HW_BREAKPOINT_W,
+                                       (unsigned long)(&ksym_selftest_dummy));
+
+       if (ret < 0) {
+               printk(KERN_CONT "ksym_trace read-write startup test failed\n");
+               goto ret_path;
+       }
+       /* Perform a read and a write operation over the dummy variable to
+        * trigger the tracer
+        */
+       if (ksym_selftest_dummy == 0)
+               ksym_selftest_dummy++;
+
+       /* stop the tracing. */
+       tracing_stop();
+       /* check the trace buffer */
+       ret = trace_test_buffer(tr, &count);
+       trace->reset(tr);
+       tracing_start();
+
+       /* read & write operations - one each is performed on the dummy variable
+        * triggering two entries in the trace buffer
+        */
+       if (!ret && count != 2) {
+               printk(KERN_CONT "Ksym tracer startup test failed");
+               ret = -1;
+       }
+
+ret_path:
+       return ret;
+}
+#endif /* CONFIG_KSYM_TRACER */
+