]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - kernel/trace/trace_selftest.c
ftrace: remove ctrl_update method
[mirror_ubuntu-bionic-kernel.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:
60a11774
SR
16 return 1;
17 }
18 return 0;
19}
20
3928a8a2 21static int trace_test_buffer_cpu(struct trace_array *tr, int cpu)
60a11774 22{
3928a8a2
SR
23 struct ring_buffer_event *event;
24 struct trace_entry *entry;
60a11774 25
3928a8a2
SR
26 while ((event = ring_buffer_consume(tr->buffer, cpu, NULL))) {
27 entry = ring_buffer_event_data(event);
60a11774 28
3928a8a2 29 if (!trace_valid_entry(entry)) {
c7aafc54 30 printk(KERN_CONT ".. invalid entry %d ",
3928a8a2 31 entry->type);
60a11774
SR
32 goto failed;
33 }
60a11774 34 }
60a11774
SR
35 return 0;
36
37 failed:
08bafa0e
SR
38 /* disable tracing */
39 tracing_disabled = 1;
60a11774
SR
40 printk(KERN_CONT ".. corrupted trace buffer .. ");
41 return -1;
42}
43
44/*
45 * Test the trace buffer to see if all the elements
46 * are still sane.
47 */
48static int trace_test_buffer(struct trace_array *tr, unsigned long *count)
49{
30afdcb1
SR
50 unsigned long flags, cnt = 0;
51 int cpu, ret = 0;
60a11774 52
30afdcb1
SR
53 /* Don't allow flipping of max traces now */
54 raw_local_irq_save(flags);
55 __raw_spin_lock(&ftrace_max_lock);
60a11774 56
3928a8a2 57 cnt = ring_buffer_entries(tr->buffer);
60a11774 58
3928a8a2
SR
59 for_each_possible_cpu(cpu) {
60 ret = trace_test_buffer_cpu(tr, cpu);
60a11774
SR
61 if (ret)
62 break;
63 }
30afdcb1
SR
64 __raw_spin_unlock(&ftrace_max_lock);
65 raw_local_irq_restore(flags);
60a11774
SR
66
67 if (count)
68 *count = cnt;
69
70 return ret;
71}
72
606576ce 73#ifdef CONFIG_FUNCTION_TRACER
77a2b37d
SR
74
75#ifdef CONFIG_DYNAMIC_FTRACE
76
77a2b37d
SR
77#define __STR(x) #x
78#define STR(x) __STR(x)
77a2b37d
SR
79
80/* Test dynamic code modification and ftrace filters */
81int trace_selftest_startup_dynamic_tracing(struct tracer *trace,
82 struct trace_array *tr,
83 int (*func)(void))
84{
77a2b37d
SR
85 int save_ftrace_enabled = ftrace_enabled;
86 int save_tracer_enabled = tracer_enabled;
dd0e545f 87 unsigned long count;
4e491d14 88 char *func_name;
dd0e545f 89 int ret;
77a2b37d
SR
90
91 /* The ftrace test PASSED */
92 printk(KERN_CONT "PASSED\n");
93 pr_info("Testing dynamic ftrace: ");
94
95 /* enable tracing, and record the filter function */
96 ftrace_enabled = 1;
97 tracer_enabled = 1;
98
99 /* passed in by parameter to fool gcc from optimizing */
100 func();
101
4e491d14
SR
102 /*
103 * Some archs *cough*PowerPC*cough* add charachters to the
104 * start of the function names. We simply put a '*' to
105 * accomodate them.
106 */
107 func_name = "*" STR(DYN_FTRACE_TEST_NAME);
108
77a2b37d 109 /* filter only on our function */
4e491d14 110 ftrace_set_filter(func_name, strlen(func_name), 1);
77a2b37d
SR
111
112 /* enable tracing */
113 tr->ctrl = 1;
114 trace->init(tr);
dd0e545f 115
77a2b37d
SR
116 /* Sleep for a 1/10 of a second */
117 msleep(100);
118
119 /* we should have nothing in the buffer */
120 ret = trace_test_buffer(tr, &count);
121 if (ret)
122 goto out;
123
124 if (count) {
125 ret = -1;
126 printk(KERN_CONT ".. filter did not filter .. ");
127 goto out;
128 }
129
130 /* call our function again */
131 func();
132
133 /* sleep again */
134 msleep(100);
135
136 /* stop the tracing. */
bbf5b1a0 137 tracing_stop();
77a2b37d
SR
138 ftrace_enabled = 0;
139
140 /* check the trace buffer */
141 ret = trace_test_buffer(tr, &count);
142 trace->reset(tr);
bbf5b1a0 143 tracing_start();
77a2b37d
SR
144
145 /* we should only have one item */
146 if (!ret && count != 1) {
06fa75ab 147 printk(KERN_CONT ".. filter failed count=%ld ..", count);
77a2b37d
SR
148 ret = -1;
149 goto out;
150 }
bbf5b1a0 151
77a2b37d
SR
152 out:
153 ftrace_enabled = save_ftrace_enabled;
154 tracer_enabled = save_tracer_enabled;
155
156 /* Enable tracing on all functions again */
157 ftrace_set_filter(NULL, 0, 1);
158
159 return ret;
160}
161#else
162# define trace_selftest_startup_dynamic_tracing(trace, tr, func) ({ 0; })
163#endif /* CONFIG_DYNAMIC_FTRACE */
60a11774
SR
164/*
165 * Simple verification test of ftrace function tracer.
166 * Enable ftrace, sleep 1/10 second, and then read the trace
167 * buffer to see if all is in order.
168 */
169int
170trace_selftest_startup_function(struct tracer *trace, struct trace_array *tr)
171{
77a2b37d
SR
172 int save_ftrace_enabled = ftrace_enabled;
173 int save_tracer_enabled = tracer_enabled;
dd0e545f
SR
174 unsigned long count;
175 int ret;
60a11774 176
77a2b37d
SR
177 /* make sure msleep has been recorded */
178 msleep(1);
179
60a11774 180 /* start the tracing */
c7aafc54 181 ftrace_enabled = 1;
77a2b37d 182 tracer_enabled = 1;
c7aafc54 183
60a11774
SR
184 tr->ctrl = 1;
185 trace->init(tr);
186 /* Sleep for a 1/10 of a second */
187 msleep(100);
188 /* stop the tracing. */
bbf5b1a0 189 tracing_stop();
c7aafc54
IM
190 ftrace_enabled = 0;
191
60a11774
SR
192 /* check the trace buffer */
193 ret = trace_test_buffer(tr, &count);
194 trace->reset(tr);
bbf5b1a0 195 tracing_start();
60a11774
SR
196
197 if (!ret && !count) {
198 printk(KERN_CONT ".. no entries found ..");
199 ret = -1;
77a2b37d 200 goto out;
60a11774
SR
201 }
202
77a2b37d
SR
203 ret = trace_selftest_startup_dynamic_tracing(trace, tr,
204 DYN_FTRACE_TEST_NAME);
205
206 out:
207 ftrace_enabled = save_ftrace_enabled;
208 tracer_enabled = save_tracer_enabled;
209
4eebcc81
SR
210 /* kill ftrace totally if we failed */
211 if (ret)
212 ftrace_kill();
213
60a11774
SR
214 return ret;
215}
606576ce 216#endif /* CONFIG_FUNCTION_TRACER */
60a11774
SR
217
218#ifdef CONFIG_IRQSOFF_TRACER
219int
220trace_selftest_startup_irqsoff(struct tracer *trace, struct trace_array *tr)
221{
222 unsigned long save_max = tracing_max_latency;
223 unsigned long count;
224 int ret;
225
226 /* start the tracing */
227 tr->ctrl = 1;
228 trace->init(tr);
229 /* reset the max latency */
230 tracing_max_latency = 0;
231 /* disable interrupts for a bit */
232 local_irq_disable();
233 udelay(100);
234 local_irq_enable();
235 /* stop the tracing. */
bbf5b1a0 236 tracing_stop();
60a11774
SR
237 /* check both trace buffers */
238 ret = trace_test_buffer(tr, NULL);
239 if (!ret)
240 ret = trace_test_buffer(&max_tr, &count);
241 trace->reset(tr);
bbf5b1a0 242 tracing_start();
60a11774
SR
243
244 if (!ret && !count) {
245 printk(KERN_CONT ".. no entries found ..");
246 ret = -1;
247 }
248
249 tracing_max_latency = save_max;
250
251 return ret;
252}
253#endif /* CONFIG_IRQSOFF_TRACER */
254
255#ifdef CONFIG_PREEMPT_TRACER
256int
257trace_selftest_startup_preemptoff(struct tracer *trace, struct trace_array *tr)
258{
259 unsigned long save_max = tracing_max_latency;
260 unsigned long count;
261 int ret;
262
263 /* start the tracing */
264 tr->ctrl = 1;
265 trace->init(tr);
266 /* reset the max latency */
267 tracing_max_latency = 0;
268 /* disable preemption for a bit */
269 preempt_disable();
270 udelay(100);
271 preempt_enable();
272 /* stop the tracing. */
bbf5b1a0 273 tracing_stop();
60a11774
SR
274 /* check both trace buffers */
275 ret = trace_test_buffer(tr, NULL);
276 if (!ret)
277 ret = trace_test_buffer(&max_tr, &count);
278 trace->reset(tr);
bbf5b1a0 279 tracing_start();
60a11774
SR
280
281 if (!ret && !count) {
282 printk(KERN_CONT ".. no entries found ..");
283 ret = -1;
284 }
285
286 tracing_max_latency = save_max;
287
288 return ret;
289}
290#endif /* CONFIG_PREEMPT_TRACER */
291
292#if defined(CONFIG_IRQSOFF_TRACER) && defined(CONFIG_PREEMPT_TRACER)
293int
294trace_selftest_startup_preemptirqsoff(struct tracer *trace, struct trace_array *tr)
295{
296 unsigned long save_max = tracing_max_latency;
297 unsigned long count;
298 int ret;
299
300 /* start the tracing */
301 tr->ctrl = 1;
302 trace->init(tr);
303
304 /* reset the max latency */
305 tracing_max_latency = 0;
306
307 /* disable preemption and interrupts for a bit */
308 preempt_disable();
309 local_irq_disable();
310 udelay(100);
311 preempt_enable();
312 /* reverse the order of preempt vs irqs */
313 local_irq_enable();
314
315 /* stop the tracing. */
bbf5b1a0 316 tracing_stop();
60a11774
SR
317 /* check both trace buffers */
318 ret = trace_test_buffer(tr, NULL);
bbf5b1a0
SR
319 if (ret) {
320 tracing_start();
60a11774 321 goto out;
bbf5b1a0 322 }
60a11774
SR
323
324 ret = trace_test_buffer(&max_tr, &count);
bbf5b1a0
SR
325 if (ret) {
326 tracing_start();
60a11774 327 goto out;
bbf5b1a0 328 }
60a11774
SR
329
330 if (!ret && !count) {
331 printk(KERN_CONT ".. no entries found ..");
332 ret = -1;
bbf5b1a0 333 tracing_start();
60a11774
SR
334 goto out;
335 }
336
337 /* do the test by disabling interrupts first this time */
338 tracing_max_latency = 0;
bbf5b1a0 339 tracing_start();
60a11774
SR
340 preempt_disable();
341 local_irq_disable();
342 udelay(100);
343 preempt_enable();
344 /* reverse the order of preempt vs irqs */
345 local_irq_enable();
346
347 /* stop the tracing. */
bbf5b1a0 348 tracing_stop();
60a11774
SR
349 /* check both trace buffers */
350 ret = trace_test_buffer(tr, NULL);
351 if (ret)
352 goto out;
353
354 ret = trace_test_buffer(&max_tr, &count);
355
356 if (!ret && !count) {
357 printk(KERN_CONT ".. no entries found ..");
358 ret = -1;
359 goto out;
360 }
361
362 out:
363 trace->reset(tr);
bbf5b1a0 364 tracing_start();
60a11774
SR
365 tracing_max_latency = save_max;
366
367 return ret;
368}
369#endif /* CONFIG_IRQSOFF_TRACER && CONFIG_PREEMPT_TRACER */
370
fb1b6d8b
SN
371#ifdef CONFIG_NOP_TRACER
372int
373trace_selftest_startup_nop(struct tracer *trace, struct trace_array *tr)
374{
375 /* What could possibly go wrong? */
376 return 0;
377}
378#endif
379
60a11774
SR
380#ifdef CONFIG_SCHED_TRACER
381static int trace_wakeup_test_thread(void *data)
382{
60a11774 383 /* Make this a RT thread, doesn't need to be too high */
05bd68c5
SR
384 struct sched_param param = { .sched_priority = 5 };
385 struct completion *x = data;
60a11774 386
05bd68c5 387 sched_setscheduler(current, SCHED_FIFO, &param);
60a11774
SR
388
389 /* Make it know we have a new prio */
390 complete(x);
391
392 /* now go to sleep and let the test wake us up */
393 set_current_state(TASK_INTERRUPTIBLE);
394 schedule();
395
396 /* we are awake, now wait to disappear */
397 while (!kthread_should_stop()) {
398 /*
399 * This is an RT task, do short sleeps to let
400 * others run.
401 */
402 msleep(100);
403 }
404
405 return 0;
406}
407
408int
409trace_selftest_startup_wakeup(struct tracer *trace, struct trace_array *tr)
410{
411 unsigned long save_max = tracing_max_latency;
412 struct task_struct *p;
413 struct completion isrt;
414 unsigned long count;
415 int ret;
416
417 init_completion(&isrt);
418
419 /* create a high prio thread */
420 p = kthread_run(trace_wakeup_test_thread, &isrt, "ftrace-test");
c7aafc54 421 if (IS_ERR(p)) {
60a11774
SR
422 printk(KERN_CONT "Failed to create ftrace wakeup test thread ");
423 return -1;
424 }
425
426 /* make sure the thread is running at an RT prio */
427 wait_for_completion(&isrt);
428
429 /* start the tracing */
430 tr->ctrl = 1;
431 trace->init(tr);
432 /* reset the max latency */
433 tracing_max_latency = 0;
434
435 /* sleep to let the RT thread sleep too */
436 msleep(100);
437
438 /*
439 * Yes this is slightly racy. It is possible that for some
440 * strange reason that the RT thread we created, did not
441 * call schedule for 100ms after doing the completion,
442 * and we do a wakeup on a task that already is awake.
443 * But that is extremely unlikely, and the worst thing that
444 * happens in such a case, is that we disable tracing.
445 * Honestly, if this race does happen something is horrible
446 * wrong with the system.
447 */
448
449 wake_up_process(p);
450
5aa60c60
SR
451 /* give a little time to let the thread wake up */
452 msleep(100);
453
60a11774 454 /* stop the tracing. */
bbf5b1a0 455 tracing_stop();
60a11774
SR
456 /* check both trace buffers */
457 ret = trace_test_buffer(tr, NULL);
458 if (!ret)
459 ret = trace_test_buffer(&max_tr, &count);
460
461
462 trace->reset(tr);
bbf5b1a0 463 tracing_start();
60a11774
SR
464
465 tracing_max_latency = save_max;
466
467 /* kill the thread */
468 kthread_stop(p);
469
470 if (!ret && !count) {
471 printk(KERN_CONT ".. no entries found ..");
472 ret = -1;
473 }
474
475 return ret;
476}
477#endif /* CONFIG_SCHED_TRACER */
478
479#ifdef CONFIG_CONTEXT_SWITCH_TRACER
480int
481trace_selftest_startup_sched_switch(struct tracer *trace, struct trace_array *tr)
482{
483 unsigned long count;
484 int ret;
485
486 /* start the tracing */
487 tr->ctrl = 1;
488 trace->init(tr);
489 /* Sleep for a 1/10 of a second */
490 msleep(100);
491 /* stop the tracing. */
bbf5b1a0 492 tracing_stop();
60a11774
SR
493 /* check the trace buffer */
494 ret = trace_test_buffer(tr, &count);
495 trace->reset(tr);
bbf5b1a0 496 tracing_start();
60a11774
SR
497
498 if (!ret && !count) {
499 printk(KERN_CONT ".. no entries found ..");
500 ret = -1;
501 }
502
503 return ret;
504}
505#endif /* CONFIG_CONTEXT_SWITCH_TRACER */
a6dd24f8
IM
506
507#ifdef CONFIG_SYSPROF_TRACER
508int
509trace_selftest_startup_sysprof(struct tracer *trace, struct trace_array *tr)
510{
511 unsigned long count;
512 int ret;
513
514 /* start the tracing */
515 tr->ctrl = 1;
516 trace->init(tr);
517 /* Sleep for a 1/10 of a second */
518 msleep(100);
519 /* stop the tracing. */
bbf5b1a0 520 tracing_stop();
a6dd24f8
IM
521 /* check the trace buffer */
522 ret = trace_test_buffer(tr, &count);
523 trace->reset(tr);
bbf5b1a0 524 tracing_start();
a6dd24f8 525
a6dd24f8
IM
526 return ret;
527}
528#endif /* CONFIG_SYSPROF_TRACER */