]> pilppa.org Git - linux-2.6-omap-h63xx.git/blob - kernel/trace/trace_selftest.c
Merge branch 'x86/asm' into tracing/syscalls
[linux-2.6-omap-h63xx.git] / kernel / trace / trace_selftest.c
1 /* Include in trace.c */
2
3 #include <linux/stringify.h>
4 #include <linux/kthread.h>
5 #include <linux/delay.h>
6
7 static inline int trace_valid_entry(struct trace_entry *entry)
8 {
9         switch (entry->type) {
10         case TRACE_FN:
11         case TRACE_CTX:
12         case TRACE_WAKE:
13         case TRACE_STACK:
14         case TRACE_PRINT:
15         case TRACE_SPECIAL:
16         case TRACE_BRANCH:
17         case TRACE_GRAPH_ENT:
18         case TRACE_GRAPH_RET:
19                 return 1;
20         }
21         return 0;
22 }
23
24 static int trace_test_buffer_cpu(struct trace_array *tr, int cpu)
25 {
26         struct ring_buffer_event *event;
27         struct trace_entry *entry;
28         unsigned int loops = 0;
29
30         while ((event = ring_buffer_consume(tr->buffer, cpu, NULL))) {
31                 entry = ring_buffer_event_data(event);
32
33                 /*
34                  * The ring buffer is a size of trace_buf_size, if
35                  * we loop more than the size, there's something wrong
36                  * with the ring buffer.
37                  */
38                 if (loops++ > trace_buf_size) {
39                         printk(KERN_CONT ".. bad ring buffer ");
40                         goto failed;
41                 }
42                 if (!trace_valid_entry(entry)) {
43                         printk(KERN_CONT ".. invalid entry %d ",
44                                 entry->type);
45                         goto failed;
46                 }
47         }
48         return 0;
49
50  failed:
51         /* disable tracing */
52         tracing_disabled = 1;
53         printk(KERN_CONT ".. corrupted trace buffer .. ");
54         return -1;
55 }
56
57 /*
58  * Test the trace buffer to see if all the elements
59  * are still sane.
60  */
61 static int trace_test_buffer(struct trace_array *tr, unsigned long *count)
62 {
63         unsigned long flags, cnt = 0;
64         int cpu, ret = 0;
65
66         /* Don't allow flipping of max traces now */
67         local_irq_save(flags);
68         __raw_spin_lock(&ftrace_max_lock);
69
70         cnt = ring_buffer_entries(tr->buffer);
71
72         /*
73          * The trace_test_buffer_cpu runs a while loop to consume all data.
74          * If the calling tracer is broken, and is constantly filling
75          * the buffer, this will run forever, and hard lock the box.
76          * We disable the ring buffer while we do this test to prevent
77          * a hard lock up.
78          */
79         tracing_off();
80         for_each_possible_cpu(cpu) {
81                 ret = trace_test_buffer_cpu(tr, cpu);
82                 if (ret)
83                         break;
84         }
85         tracing_on();
86         __raw_spin_unlock(&ftrace_max_lock);
87         local_irq_restore(flags);
88
89         if (count)
90                 *count = cnt;
91
92         return ret;
93 }
94
95 static inline void warn_failed_init_tracer(struct tracer *trace, int init_ret)
96 {
97         printk(KERN_WARNING "Failed to init %s tracer, init returned %d\n",
98                 trace->name, init_ret);
99 }
100 #ifdef CONFIG_FUNCTION_TRACER
101
102 #ifdef CONFIG_DYNAMIC_FTRACE
103
104 /* Test dynamic code modification and ftrace filters */
105 int trace_selftest_startup_dynamic_tracing(struct tracer *trace,
106                                            struct trace_array *tr,
107                                            int (*func)(void))
108 {
109         int save_ftrace_enabled = ftrace_enabled;
110         int save_tracer_enabled = tracer_enabled;
111         unsigned long count;
112         char *func_name;
113         int ret;
114
115         /* The ftrace test PASSED */
116         printk(KERN_CONT "PASSED\n");
117         pr_info("Testing dynamic ftrace: ");
118
119         /* enable tracing, and record the filter function */
120         ftrace_enabled = 1;
121         tracer_enabled = 1;
122
123         /* passed in by parameter to fool gcc from optimizing */
124         func();
125
126         /*
127          * Some archs *cough*PowerPC*cough* add characters to the
128          * start of the function names. We simply put a '*' to
129          * accommodate them.
130          */
131         func_name = "*" __stringify(DYN_FTRACE_TEST_NAME);
132
133         /* filter only on our function */
134         ftrace_set_filter(func_name, strlen(func_name), 1);
135
136         /* enable tracing */
137         ret = tracer_init(trace, tr);
138         if (ret) {
139                 warn_failed_init_tracer(trace, ret);
140                 goto out;
141         }
142
143         /* Sleep for a 1/10 of a second */
144         msleep(100);
145
146         /* we should have nothing in the buffer */
147         ret = trace_test_buffer(tr, &count);
148         if (ret)
149                 goto out;
150
151         if (count) {
152                 ret = -1;
153                 printk(KERN_CONT ".. filter did not filter .. ");
154                 goto out;
155         }
156
157         /* call our function again */
158         func();
159
160         /* sleep again */
161         msleep(100);
162
163         /* stop the tracing. */
164         tracing_stop();
165         ftrace_enabled = 0;
166
167         /* check the trace buffer */
168         ret = trace_test_buffer(tr, &count);
169         trace->reset(tr);
170         tracing_start();
171
172         /* we should only have one item */
173         if (!ret && count != 1) {
174                 printk(KERN_CONT ".. filter failed count=%ld ..", count);
175                 ret = -1;
176                 goto out;
177         }
178
179  out:
180         ftrace_enabled = save_ftrace_enabled;
181         tracer_enabled = save_tracer_enabled;
182
183         /* Enable tracing on all functions again */
184         ftrace_set_filter(NULL, 0, 1);
185
186         return ret;
187 }
188 #else
189 # define trace_selftest_startup_dynamic_tracing(trace, tr, func) ({ 0; })
190 #endif /* CONFIG_DYNAMIC_FTRACE */
191 /*
192  * Simple verification test of ftrace function tracer.
193  * Enable ftrace, sleep 1/10 second, and then read the trace
194  * buffer to see if all is in order.
195  */
196 int
197 trace_selftest_startup_function(struct tracer *trace, struct trace_array *tr)
198 {
199         int save_ftrace_enabled = ftrace_enabled;
200         int save_tracer_enabled = tracer_enabled;
201         unsigned long count;
202         int ret;
203
204         /* make sure msleep has been recorded */
205         msleep(1);
206
207         /* start the tracing */
208         ftrace_enabled = 1;
209         tracer_enabled = 1;
210
211         ret = tracer_init(trace, tr);
212         if (ret) {
213                 warn_failed_init_tracer(trace, ret);
214                 goto out;
215         }
216
217         /* Sleep for a 1/10 of a second */
218         msleep(100);
219         /* stop the tracing. */
220         tracing_stop();
221         ftrace_enabled = 0;
222
223         /* check the trace buffer */
224         ret = trace_test_buffer(tr, &count);
225         trace->reset(tr);
226         tracing_start();
227
228         if (!ret && !count) {
229                 printk(KERN_CONT ".. no entries found ..");
230                 ret = -1;
231                 goto out;
232         }
233
234         ret = trace_selftest_startup_dynamic_tracing(trace, tr,
235                                                      DYN_FTRACE_TEST_NAME);
236
237  out:
238         ftrace_enabled = save_ftrace_enabled;
239         tracer_enabled = save_tracer_enabled;
240
241         /* kill ftrace totally if we failed */
242         if (ret)
243                 ftrace_kill();
244
245         return ret;
246 }
247 #endif /* CONFIG_FUNCTION_TRACER */
248
249
250 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
251 /*
252  * Pretty much the same than for the function tracer from which the selftest
253  * has been borrowed.
254  */
255 int
256 trace_selftest_startup_function_graph(struct tracer *trace,
257                                         struct trace_array *tr)
258 {
259         int ret;
260         unsigned long count;
261
262         ret = tracer_init(trace, tr);
263         if (ret) {
264                 warn_failed_init_tracer(trace, ret);
265                 goto out;
266         }
267
268         /* Sleep for a 1/10 of a second */
269         msleep(100);
270
271         tracing_stop();
272
273         /* check the trace buffer */
274         ret = trace_test_buffer(tr, &count);
275
276         trace->reset(tr);
277         tracing_start();
278
279         if (!ret && !count) {
280                 printk(KERN_CONT ".. no entries found ..");
281                 ret = -1;
282                 goto out;
283         }
284
285         /* Don't test dynamic tracing, the function tracer already did */
286
287 out:
288         /* Stop it if we failed */
289         if (ret)
290                 ftrace_graph_stop();
291
292         return ret;
293 }
294 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
295
296
297 #ifdef CONFIG_IRQSOFF_TRACER
298 int
299 trace_selftest_startup_irqsoff(struct tracer *trace, struct trace_array *tr)
300 {
301         unsigned long save_max = tracing_max_latency;
302         unsigned long count;
303         int ret;
304
305         /* start the tracing */
306         ret = tracer_init(trace, tr);
307         if (ret) {
308                 warn_failed_init_tracer(trace, ret);
309                 return ret;
310         }
311
312         /* reset the max latency */
313         tracing_max_latency = 0;
314         /* disable interrupts for a bit */
315         local_irq_disable();
316         udelay(100);
317         local_irq_enable();
318         /* stop the tracing. */
319         tracing_stop();
320         /* check both trace buffers */
321         ret = trace_test_buffer(tr, NULL);
322         if (!ret)
323                 ret = trace_test_buffer(&max_tr, &count);
324         trace->reset(tr);
325         tracing_start();
326
327         if (!ret && !count) {
328                 printk(KERN_CONT ".. no entries found ..");
329                 ret = -1;
330         }
331
332         tracing_max_latency = save_max;
333
334         return ret;
335 }
336 #endif /* CONFIG_IRQSOFF_TRACER */
337
338 #ifdef CONFIG_PREEMPT_TRACER
339 int
340 trace_selftest_startup_preemptoff(struct tracer *trace, struct trace_array *tr)
341 {
342         unsigned long save_max = tracing_max_latency;
343         unsigned long count;
344         int ret;
345
346         /*
347          * Now that the big kernel lock is no longer preemptable,
348          * and this is called with the BKL held, it will always
349          * fail. If preemption is already disabled, simply
350          * pass the test. When the BKL is removed, or becomes
351          * preemptible again, we will once again test this,
352          * so keep it in.
353          */
354         if (preempt_count()) {
355                 printk(KERN_CONT "can not test ... force ");
356                 return 0;
357         }
358
359         /* start the tracing */
360         ret = tracer_init(trace, tr);
361         if (ret) {
362                 warn_failed_init_tracer(trace, ret);
363                 return ret;
364         }
365
366         /* reset the max latency */
367         tracing_max_latency = 0;
368         /* disable preemption for a bit */
369         preempt_disable();
370         udelay(100);
371         preempt_enable();
372         /* stop the tracing. */
373         tracing_stop();
374         /* check both trace buffers */
375         ret = trace_test_buffer(tr, NULL);
376         if (!ret)
377                 ret = trace_test_buffer(&max_tr, &count);
378         trace->reset(tr);
379         tracing_start();
380
381         if (!ret && !count) {
382                 printk(KERN_CONT ".. no entries found ..");
383                 ret = -1;
384         }
385
386         tracing_max_latency = save_max;
387
388         return ret;
389 }
390 #endif /* CONFIG_PREEMPT_TRACER */
391
392 #if defined(CONFIG_IRQSOFF_TRACER) && defined(CONFIG_PREEMPT_TRACER)
393 int
394 trace_selftest_startup_preemptirqsoff(struct tracer *trace, struct trace_array *tr)
395 {
396         unsigned long save_max = tracing_max_latency;
397         unsigned long count;
398         int ret;
399
400         /*
401          * Now that the big kernel lock is no longer preemptable,
402          * and this is called with the BKL held, it will always
403          * fail. If preemption is already disabled, simply
404          * pass the test. When the BKL is removed, or becomes
405          * preemptible again, we will once again test this,
406          * so keep it in.
407          */
408         if (preempt_count()) {
409                 printk(KERN_CONT "can not test ... force ");
410                 return 0;
411         }
412
413         /* start the tracing */
414         ret = tracer_init(trace, tr);
415         if (ret) {
416                 warn_failed_init_tracer(trace, ret);
417                 goto out;
418         }
419
420         /* reset the max latency */
421         tracing_max_latency = 0;
422
423         /* disable preemption and interrupts for a bit */
424         preempt_disable();
425         local_irq_disable();
426         udelay(100);
427         preempt_enable();
428         /* reverse the order of preempt vs irqs */
429         local_irq_enable();
430
431         /* stop the tracing. */
432         tracing_stop();
433         /* check both trace buffers */
434         ret = trace_test_buffer(tr, NULL);
435         if (ret) {
436                 tracing_start();
437                 goto out;
438         }
439
440         ret = trace_test_buffer(&max_tr, &count);
441         if (ret) {
442                 tracing_start();
443                 goto out;
444         }
445
446         if (!ret && !count) {
447                 printk(KERN_CONT ".. no entries found ..");
448                 ret = -1;
449                 tracing_start();
450                 goto out;
451         }
452
453         /* do the test by disabling interrupts first this time */
454         tracing_max_latency = 0;
455         tracing_start();
456         preempt_disable();
457         local_irq_disable();
458         udelay(100);
459         preempt_enable();
460         /* reverse the order of preempt vs irqs */
461         local_irq_enable();
462
463         /* stop the tracing. */
464         tracing_stop();
465         /* check both trace buffers */
466         ret = trace_test_buffer(tr, NULL);
467         if (ret)
468                 goto out;
469
470         ret = trace_test_buffer(&max_tr, &count);
471
472         if (!ret && !count) {
473                 printk(KERN_CONT ".. no entries found ..");
474                 ret = -1;
475                 goto out;
476         }
477
478  out:
479         trace->reset(tr);
480         tracing_start();
481         tracing_max_latency = save_max;
482
483         return ret;
484 }
485 #endif /* CONFIG_IRQSOFF_TRACER && CONFIG_PREEMPT_TRACER */
486
487 #ifdef CONFIG_NOP_TRACER
488 int
489 trace_selftest_startup_nop(struct tracer *trace, struct trace_array *tr)
490 {
491         /* What could possibly go wrong? */
492         return 0;
493 }
494 #endif
495
496 #ifdef CONFIG_SCHED_TRACER
497 static int trace_wakeup_test_thread(void *data)
498 {
499         /* Make this a RT thread, doesn't need to be too high */
500         struct sched_param param = { .sched_priority = 5 };
501         struct completion *x = data;
502
503         sched_setscheduler(current, SCHED_FIFO, &param);
504
505         /* Make it know we have a new prio */
506         complete(x);
507
508         /* now go to sleep and let the test wake us up */
509         set_current_state(TASK_INTERRUPTIBLE);
510         schedule();
511
512         /* we are awake, now wait to disappear */
513         while (!kthread_should_stop()) {
514                 /*
515                  * This is an RT task, do short sleeps to let
516                  * others run.
517                  */
518                 msleep(100);
519         }
520
521         return 0;
522 }
523
524 int
525 trace_selftest_startup_wakeup(struct tracer *trace, struct trace_array *tr)
526 {
527         unsigned long save_max = tracing_max_latency;
528         struct task_struct *p;
529         struct completion isrt;
530         unsigned long count;
531         int ret;
532
533         init_completion(&isrt);
534
535         /* create a high prio thread */
536         p = kthread_run(trace_wakeup_test_thread, &isrt, "ftrace-test");
537         if (IS_ERR(p)) {
538                 printk(KERN_CONT "Failed to create ftrace wakeup test thread ");
539                 return -1;
540         }
541
542         /* make sure the thread is running at an RT prio */
543         wait_for_completion(&isrt);
544
545         /* start the tracing */
546         ret = tracer_init(trace, tr);
547         if (ret) {
548                 warn_failed_init_tracer(trace, ret);
549                 return ret;
550         }
551
552         /* reset the max latency */
553         tracing_max_latency = 0;
554
555         /* sleep to let the RT thread sleep too */
556         msleep(100);
557
558         /*
559          * Yes this is slightly racy. It is possible that for some
560          * strange reason that the RT thread we created, did not
561          * call schedule for 100ms after doing the completion,
562          * and we do a wakeup on a task that already is awake.
563          * But that is extremely unlikely, and the worst thing that
564          * happens in such a case, is that we disable tracing.
565          * Honestly, if this race does happen something is horrible
566          * wrong with the system.
567          */
568
569         wake_up_process(p);
570
571         /* give a little time to let the thread wake up */
572         msleep(100);
573
574         /* stop the tracing. */
575         tracing_stop();
576         /* check both trace buffers */
577         ret = trace_test_buffer(tr, NULL);
578         if (!ret)
579                 ret = trace_test_buffer(&max_tr, &count);
580
581
582         trace->reset(tr);
583         tracing_start();
584
585         tracing_max_latency = save_max;
586
587         /* kill the thread */
588         kthread_stop(p);
589
590         if (!ret && !count) {
591                 printk(KERN_CONT ".. no entries found ..");
592                 ret = -1;
593         }
594
595         return ret;
596 }
597 #endif /* CONFIG_SCHED_TRACER */
598
599 #ifdef CONFIG_CONTEXT_SWITCH_TRACER
600 int
601 trace_selftest_startup_sched_switch(struct tracer *trace, struct trace_array *tr)
602 {
603         unsigned long count;
604         int ret;
605
606         /* start the tracing */
607         ret = tracer_init(trace, tr);
608         if (ret) {
609                 warn_failed_init_tracer(trace, ret);
610                 return ret;
611         }
612
613         /* Sleep for a 1/10 of a second */
614         msleep(100);
615         /* stop the tracing. */
616         tracing_stop();
617         /* check the trace buffer */
618         ret = trace_test_buffer(tr, &count);
619         trace->reset(tr);
620         tracing_start();
621
622         if (!ret && !count) {
623                 printk(KERN_CONT ".. no entries found ..");
624                 ret = -1;
625         }
626
627         return ret;
628 }
629 #endif /* CONFIG_CONTEXT_SWITCH_TRACER */
630
631 #ifdef CONFIG_SYSPROF_TRACER
632 int
633 trace_selftest_startup_sysprof(struct tracer *trace, struct trace_array *tr)
634 {
635         unsigned long count;
636         int ret;
637
638         /* start the tracing */
639         ret = tracer_init(trace, tr);
640         if (ret) {
641                 warn_failed_init_tracer(trace, ret);
642                 return ret;
643         }
644
645         /* Sleep for a 1/10 of a second */
646         msleep(100);
647         /* stop the tracing. */
648         tracing_stop();
649         /* check the trace buffer */
650         ret = trace_test_buffer(tr, &count);
651         trace->reset(tr);
652         tracing_start();
653
654         if (!ret && !count) {
655                 printk(KERN_CONT ".. no entries found ..");
656                 ret = -1;
657         }
658
659         return ret;
660 }
661 #endif /* CONFIG_SYSPROF_TRACER */
662
663 #ifdef CONFIG_BRANCH_TRACER
664 int
665 trace_selftest_startup_branch(struct tracer *trace, struct trace_array *tr)
666 {
667         unsigned long count;
668         int ret;
669
670         /* start the tracing */
671         ret = tracer_init(trace, tr);
672         if (ret) {
673                 warn_failed_init_tracer(trace, ret);
674                 return ret;
675         }
676
677         /* Sleep for a 1/10 of a second */
678         msleep(100);
679         /* stop the tracing. */
680         tracing_stop();
681         /* check the trace buffer */
682         ret = trace_test_buffer(tr, &count);
683         trace->reset(tr);
684         tracing_start();
685
686         if (!ret && !count) {
687                 printk(KERN_CONT ".. no entries found ..");
688                 ret = -1;
689         }
690
691         return ret;
692 }
693 #endif /* CONFIG_BRANCH_TRACER */