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