blob: f444f57f1338bac47ae44e5a8c6327f6b11c18d5 [file] [log] [blame]
Steven Rostedt60a11772008-05-12 21:20:44 +02001/* Include in trace.c */
2
Steven Rostedt9cc26a22009-03-09 16:00:22 -04003#include <linux/stringify.h>
Steven Rostedt60a11772008-05-12 21:20:44 +02004#include <linux/kthread.h>
Ingo Molnarc7aafc52008-05-12 21:20:45 +02005#include <linux/delay.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +09006#include <linux/slab.h>
Steven Rostedt60a11772008-05-12 21:20:44 +02007
Ingo Molnare309b412008-05-12 21:20:51 +02008static inline int trace_valid_entry(struct trace_entry *entry)
Steven Rostedt60a11772008-05-12 21:20:44 +02009{
10 switch (entry->type) {
11 case TRACE_FN:
12 case TRACE_CTX:
Ingo Molnar57422792008-05-12 21:20:51 +020013 case TRACE_WAKE:
Steven Rostedt06fa75a2008-05-12 21:20:54 +020014 case TRACE_STACK:
Steven Rostedtdd0e5452008-08-01 12:26:41 -040015 case TRACE_PRINT:
Steven Rostedt80e5ea42008-11-12 15:24:24 -050016 case TRACE_BRANCH:
Frederic Weisbecker7447dce2009-02-07 21:33:57 +010017 case TRACE_GRAPH_ENT:
18 case TRACE_GRAPH_RET:
Steven Rostedt60a11772008-05-12 21:20:44 +020019 return 1;
20 }
21 return 0;
22}
23
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -050024static int trace_test_buffer_cpu(struct trace_buffer *buf, int cpu)
Steven Rostedt60a11772008-05-12 21:20:44 +020025{
Steven Rostedt3928a8a2008-09-29 23:02:41 -040026 struct ring_buffer_event *event;
27 struct trace_entry *entry;
Steven Rostedt4b3e3d22009-02-18 22:50:01 -050028 unsigned int loops = 0;
Steven Rostedt60a11772008-05-12 21:20:44 +020029
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -050030 while ((event = ring_buffer_consume(buf->buffer, cpu, NULL, NULL))) {
Steven Rostedt3928a8a2008-09-29 23:02:41 -040031 entry = ring_buffer_event_data(event);
Steven Rostedt60a11772008-05-12 21:20:44 +020032
Steven Rostedt4b3e3d22009-02-18 22:50:01 -050033 /*
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 }
Steven Rostedt3928a8a2008-09-29 23:02:41 -040042 if (!trace_valid_entry(entry)) {
Ingo Molnarc7aafc52008-05-12 21:20:45 +020043 printk(KERN_CONT ".. invalid entry %d ",
Steven Rostedt3928a8a2008-09-29 23:02:41 -040044 entry->type);
Steven Rostedt60a11772008-05-12 21:20:44 +020045 goto failed;
46 }
Steven Rostedt60a11772008-05-12 21:20:44 +020047 }
Steven Rostedt60a11772008-05-12 21:20:44 +020048 return 0;
49
50 failed:
Steven Rostedt08bafa02008-05-12 21:20:45 +020051 /* disable tracing */
52 tracing_disabled = 1;
Steven Rostedt60a11772008-05-12 21:20:44 +020053 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 */
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -050061static int trace_test_buffer(struct trace_buffer *buf, unsigned long *count)
Steven Rostedt60a11772008-05-12 21:20:44 +020062{
Steven Rostedt30afdcb2008-05-12 21:20:56 +020063 unsigned long flags, cnt = 0;
64 int cpu, ret = 0;
Steven Rostedt60a11772008-05-12 21:20:44 +020065
Steven Rostedt30afdcb2008-05-12 21:20:56 +020066 /* Don't allow flipping of max traces now */
Steven Rostedtd51ad7a2008-11-15 15:48:29 -050067 local_irq_save(flags);
Steven Rostedt (Red Hat)0b9b12c2014-01-14 10:04:59 -050068 arch_spin_lock(&buf->tr->max_lock);
Steven Rostedt3928a8a2008-09-29 23:02:41 -040069
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -050070 cnt = ring_buffer_entries(buf->buffer);
Steven Rostedt3928a8a2008-09-29 23:02:41 -040071
Steven Rostedt0c5119c2009-02-18 18:33:57 -050072 /*
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();
Steven Rostedt60a11772008-05-12 21:20:44 +020080 for_each_possible_cpu(cpu) {
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -050081 ret = trace_test_buffer_cpu(buf, cpu);
Steven Rostedt60a11772008-05-12 21:20:44 +020082 if (ret)
83 break;
84 }
Steven Rostedt0c5119c2009-02-18 18:33:57 -050085 tracing_on();
Steven Rostedt (Red Hat)0b9b12c2014-01-14 10:04:59 -050086 arch_spin_unlock(&buf->tr->max_lock);
Steven Rostedtd51ad7a2008-11-15 15:48:29 -050087 local_irq_restore(flags);
Steven Rostedt60a11772008-05-12 21:20:44 +020088
89 if (count)
90 *count = cnt;
91
92 return ret;
93}
94
Frederic Weisbecker1c800252008-11-16 05:57:26 +010095static 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}
Steven Rostedt606576c2008-10-06 19:06:12 -0400100#ifdef CONFIG_FUNCTION_TRACER
Steven Rostedt77a2b372008-05-12 21:20:45 +0200101
102#ifdef CONFIG_DYNAMIC_FTRACE
103
Steven Rostedt95950c22011-05-06 00:08:51 -0400104static int trace_selftest_test_probe1_cnt;
105static void trace_selftest_test_probe1_func(unsigned long ip,
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400106 unsigned long pip,
Steven Rostedta1e2e312011-08-09 12:50:46 -0400107 struct ftrace_ops *op,
108 struct pt_regs *pt_regs)
Steven Rostedt95950c22011-05-06 00:08:51 -0400109{
110 trace_selftest_test_probe1_cnt++;
111}
112
113static int trace_selftest_test_probe2_cnt;
114static void trace_selftest_test_probe2_func(unsigned long ip,
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400115 unsigned long pip,
Steven Rostedta1e2e312011-08-09 12:50:46 -0400116 struct ftrace_ops *op,
117 struct pt_regs *pt_regs)
Steven Rostedt95950c22011-05-06 00:08:51 -0400118{
119 trace_selftest_test_probe2_cnt++;
120}
121
122static int trace_selftest_test_probe3_cnt;
123static void trace_selftest_test_probe3_func(unsigned long ip,
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400124 unsigned long pip,
Steven Rostedta1e2e312011-08-09 12:50:46 -0400125 struct ftrace_ops *op,
126 struct pt_regs *pt_regs)
Steven Rostedt95950c22011-05-06 00:08:51 -0400127{
128 trace_selftest_test_probe3_cnt++;
129}
130
131static int trace_selftest_test_global_cnt;
132static void trace_selftest_test_global_func(unsigned long ip,
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400133 unsigned long pip,
Steven Rostedta1e2e312011-08-09 12:50:46 -0400134 struct ftrace_ops *op,
135 struct pt_regs *pt_regs)
Steven Rostedt95950c22011-05-06 00:08:51 -0400136{
137 trace_selftest_test_global_cnt++;
138}
139
140static int trace_selftest_test_dyn_cnt;
141static void trace_selftest_test_dyn_func(unsigned long ip,
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400142 unsigned long pip,
Steven Rostedta1e2e312011-08-09 12:50:46 -0400143 struct ftrace_ops *op,
144 struct pt_regs *pt_regs)
Steven Rostedt95950c22011-05-06 00:08:51 -0400145{
146 trace_selftest_test_dyn_cnt++;
147}
148
149static struct ftrace_ops test_probe1 = {
150 .func = trace_selftest_test_probe1_func,
Steven Rostedt47409742012-07-20 11:04:44 -0400151 .flags = FTRACE_OPS_FL_RECURSION_SAFE,
Steven Rostedt95950c22011-05-06 00:08:51 -0400152};
153
154static struct ftrace_ops test_probe2 = {
155 .func = trace_selftest_test_probe2_func,
Steven Rostedt47409742012-07-20 11:04:44 -0400156 .flags = FTRACE_OPS_FL_RECURSION_SAFE,
Steven Rostedt95950c22011-05-06 00:08:51 -0400157};
158
159static struct ftrace_ops test_probe3 = {
160 .func = trace_selftest_test_probe3_func,
Steven Rostedt47409742012-07-20 11:04:44 -0400161 .flags = FTRACE_OPS_FL_RECURSION_SAFE,
Steven Rostedt95950c22011-05-06 00:08:51 -0400162};
163
Steven Rostedt95950c22011-05-06 00:08:51 -0400164static void print_counts(void)
165{
166 printk("(%d %d %d %d %d) ",
167 trace_selftest_test_probe1_cnt,
168 trace_selftest_test_probe2_cnt,
169 trace_selftest_test_probe3_cnt,
170 trace_selftest_test_global_cnt,
171 trace_selftest_test_dyn_cnt);
172}
173
174static void reset_counts(void)
175{
176 trace_selftest_test_probe1_cnt = 0;
177 trace_selftest_test_probe2_cnt = 0;
178 trace_selftest_test_probe3_cnt = 0;
179 trace_selftest_test_global_cnt = 0;
180 trace_selftest_test_dyn_cnt = 0;
181}
182
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -0500183static int trace_selftest_ops(struct trace_array *tr, int cnt)
Steven Rostedt95950c22011-05-06 00:08:51 -0400184{
185 int save_ftrace_enabled = ftrace_enabled;
186 struct ftrace_ops *dyn_ops;
187 char *func1_name;
188 char *func2_name;
189 int len1;
190 int len2;
191 int ret = -1;
192
193 printk(KERN_CONT "PASSED\n");
194 pr_info("Testing dynamic ftrace ops #%d: ", cnt);
195
196 ftrace_enabled = 1;
197 reset_counts();
198
199 /* Handle PPC64 '.' name */
200 func1_name = "*" __stringify(DYN_FTRACE_TEST_NAME);
201 func2_name = "*" __stringify(DYN_FTRACE_TEST_NAME2);
202 len1 = strlen(func1_name);
203 len2 = strlen(func2_name);
204
205 /*
206 * Probe 1 will trace function 1.
207 * Probe 2 will trace function 2.
208 * Probe 3 will trace functions 1 and 2.
209 */
210 ftrace_set_filter(&test_probe1, func1_name, len1, 1);
211 ftrace_set_filter(&test_probe2, func2_name, len2, 1);
212 ftrace_set_filter(&test_probe3, func1_name, len1, 1);
213 ftrace_set_filter(&test_probe3, func2_name, len2, 0);
214
215 register_ftrace_function(&test_probe1);
216 register_ftrace_function(&test_probe2);
217 register_ftrace_function(&test_probe3);
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -0500218 /* First time we are running with main function */
219 if (cnt > 1) {
220 ftrace_init_array_ops(tr, trace_selftest_test_global_func);
221 register_ftrace_function(tr->ops);
222 }
Steven Rostedt95950c22011-05-06 00:08:51 -0400223
224 DYN_FTRACE_TEST_NAME();
225
226 print_counts();
227
228 if (trace_selftest_test_probe1_cnt != 1)
229 goto out;
230 if (trace_selftest_test_probe2_cnt != 0)
231 goto out;
232 if (trace_selftest_test_probe3_cnt != 1)
233 goto out;
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -0500234 if (cnt > 1) {
235 if (trace_selftest_test_global_cnt == 0)
236 goto out;
237 }
Steven Rostedt95950c22011-05-06 00:08:51 -0400238
239 DYN_FTRACE_TEST_NAME2();
240
241 print_counts();
242
243 if (trace_selftest_test_probe1_cnt != 1)
244 goto out;
245 if (trace_selftest_test_probe2_cnt != 1)
246 goto out;
247 if (trace_selftest_test_probe3_cnt != 2)
248 goto out;
249
250 /* Add a dynamic probe */
251 dyn_ops = kzalloc(sizeof(*dyn_ops), GFP_KERNEL);
252 if (!dyn_ops) {
253 printk("MEMORY ERROR ");
254 goto out;
255 }
256
257 dyn_ops->func = trace_selftest_test_dyn_func;
258
259 register_ftrace_function(dyn_ops);
260
261 trace_selftest_test_global_cnt = 0;
262
263 DYN_FTRACE_TEST_NAME();
264
265 print_counts();
266
267 if (trace_selftest_test_probe1_cnt != 2)
268 goto out_free;
269 if (trace_selftest_test_probe2_cnt != 1)
270 goto out_free;
271 if (trace_selftest_test_probe3_cnt != 3)
272 goto out_free;
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -0500273 if (cnt > 1) {
274 if (trace_selftest_test_global_cnt == 0)
Steven Rostedt (VMware)df865f82017-09-01 12:04:09 -0400275 goto out_free;
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -0500276 }
Steven Rostedt95950c22011-05-06 00:08:51 -0400277 if (trace_selftest_test_dyn_cnt == 0)
278 goto out_free;
279
280 DYN_FTRACE_TEST_NAME2();
281
282 print_counts();
283
284 if (trace_selftest_test_probe1_cnt != 2)
285 goto out_free;
286 if (trace_selftest_test_probe2_cnt != 2)
287 goto out_free;
288 if (trace_selftest_test_probe3_cnt != 4)
289 goto out_free;
290
291 ret = 0;
292 out_free:
293 unregister_ftrace_function(dyn_ops);
294 kfree(dyn_ops);
295
296 out:
297 /* Purposely unregister in the same order */
298 unregister_ftrace_function(&test_probe1);
299 unregister_ftrace_function(&test_probe2);
300 unregister_ftrace_function(&test_probe3);
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -0500301 if (cnt > 1)
302 unregister_ftrace_function(tr->ops);
303 ftrace_reset_array_ops(tr);
Steven Rostedt95950c22011-05-06 00:08:51 -0400304
305 /* Make sure everything is off */
306 reset_counts();
307 DYN_FTRACE_TEST_NAME();
308 DYN_FTRACE_TEST_NAME();
309
310 if (trace_selftest_test_probe1_cnt ||
311 trace_selftest_test_probe2_cnt ||
312 trace_selftest_test_probe3_cnt ||
313 trace_selftest_test_global_cnt ||
314 trace_selftest_test_dyn_cnt)
315 ret = -1;
316
317 ftrace_enabled = save_ftrace_enabled;
318
319 return ret;
320}
321
Steven Rostedt77a2b372008-05-12 21:20:45 +0200322/* Test dynamic code modification and ftrace filters */
Fabian Frederickad1438a2014-04-17 21:44:42 +0200323static int trace_selftest_startup_dynamic_tracing(struct tracer *trace,
324 struct trace_array *tr,
325 int (*func)(void))
Steven Rostedt77a2b372008-05-12 21:20:45 +0200326{
Steven Rostedt77a2b372008-05-12 21:20:45 +0200327 int save_ftrace_enabled = ftrace_enabled;
Steven Rostedtdd0e5452008-08-01 12:26:41 -0400328 unsigned long count;
Steven Rostedt4e491d12008-05-14 23:49:44 -0400329 char *func_name;
Steven Rostedtdd0e5452008-08-01 12:26:41 -0400330 int ret;
Steven Rostedt77a2b372008-05-12 21:20:45 +0200331
332 /* The ftrace test PASSED */
333 printk(KERN_CONT "PASSED\n");
334 pr_info("Testing dynamic ftrace: ");
335
336 /* enable tracing, and record the filter function */
337 ftrace_enabled = 1;
Steven Rostedt77a2b372008-05-12 21:20:45 +0200338
339 /* passed in by parameter to fool gcc from optimizing */
340 func();
341
Steven Rostedt4e491d12008-05-14 23:49:44 -0400342 /*
Wenji Huang73d8b8b2009-02-17 01:10:02 -0500343 * Some archs *cough*PowerPC*cough* add characters to the
Steven Rostedt4e491d12008-05-14 23:49:44 -0400344 * start of the function names. We simply put a '*' to
Wenji Huang73d8b8b2009-02-17 01:10:02 -0500345 * accommodate them.
Steven Rostedt4e491d12008-05-14 23:49:44 -0400346 */
Steven Rostedt9cc26a22009-03-09 16:00:22 -0400347 func_name = "*" __stringify(DYN_FTRACE_TEST_NAME);
Steven Rostedt4e491d12008-05-14 23:49:44 -0400348
Steven Rostedt77a2b372008-05-12 21:20:45 +0200349 /* filter only on our function */
Steven Rostedt936e0742011-05-05 22:54:01 -0400350 ftrace_set_global_filter(func_name, strlen(func_name), 1);
Steven Rostedt77a2b372008-05-12 21:20:45 +0200351
352 /* enable tracing */
Arnaldo Carvalho de Melob6f11df2009-02-05 18:02:00 -0200353 ret = tracer_init(trace, tr);
Frederic Weisbecker1c800252008-11-16 05:57:26 +0100354 if (ret) {
355 warn_failed_init_tracer(trace, ret);
356 goto out;
357 }
Steven Rostedtdd0e5452008-08-01 12:26:41 -0400358
Steven Rostedt77a2b372008-05-12 21:20:45 +0200359 /* Sleep for a 1/10 of a second */
360 msleep(100);
361
362 /* we should have nothing in the buffer */
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -0500363 ret = trace_test_buffer(&tr->trace_buffer, &count);
Steven Rostedt77a2b372008-05-12 21:20:45 +0200364 if (ret)
365 goto out;
366
367 if (count) {
368 ret = -1;
369 printk(KERN_CONT ".. filter did not filter .. ");
370 goto out;
371 }
372
373 /* call our function again */
374 func();
375
376 /* sleep again */
377 msleep(100);
378
379 /* stop the tracing. */
Steven Rostedtbbf5b1a2008-11-07 22:36:02 -0500380 tracing_stop();
Steven Rostedt77a2b372008-05-12 21:20:45 +0200381 ftrace_enabled = 0;
382
383 /* check the trace buffer */
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -0500384 ret = trace_test_buffer(&tr->trace_buffer, &count);
Steven Rostedt (Red Hat)3ddee632014-09-12 14:26:51 -0400385
386 ftrace_enabled = 1;
Steven Rostedtbbf5b1a2008-11-07 22:36:02 -0500387 tracing_start();
Steven Rostedt77a2b372008-05-12 21:20:45 +0200388
389 /* we should only have one item */
390 if (!ret && count != 1) {
Steven Rostedt95950c22011-05-06 00:08:51 -0400391 trace->reset(tr);
Steven Rostedt06fa75a2008-05-12 21:20:54 +0200392 printk(KERN_CONT ".. filter failed count=%ld ..", count);
Steven Rostedt77a2b372008-05-12 21:20:45 +0200393 ret = -1;
394 goto out;
395 }
Steven Rostedtbbf5b1a2008-11-07 22:36:02 -0500396
Steven Rostedt95950c22011-05-06 00:08:51 -0400397 /* Test the ops with global tracing running */
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -0500398 ret = trace_selftest_ops(tr, 1);
Steven Rostedt95950c22011-05-06 00:08:51 -0400399 trace->reset(tr);
400
Steven Rostedt77a2b372008-05-12 21:20:45 +0200401 out:
402 ftrace_enabled = save_ftrace_enabled;
Steven Rostedt77a2b372008-05-12 21:20:45 +0200403
404 /* Enable tracing on all functions again */
Steven Rostedt936e0742011-05-05 22:54:01 -0400405 ftrace_set_global_filter(NULL, 0, 1);
Steven Rostedt77a2b372008-05-12 21:20:45 +0200406
Steven Rostedt95950c22011-05-06 00:08:51 -0400407 /* Test the ops with global tracing off */
408 if (!ret)
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -0500409 ret = trace_selftest_ops(tr, 2);
Steven Rostedt95950c22011-05-06 00:08:51 -0400410
Steven Rostedt77a2b372008-05-12 21:20:45 +0200411 return ret;
412}
Steven Rostedtea701f12012-07-20 13:08:05 -0400413
414static int trace_selftest_recursion_cnt;
415static void trace_selftest_test_recursion_func(unsigned long ip,
416 unsigned long pip,
417 struct ftrace_ops *op,
418 struct pt_regs *pt_regs)
419{
420 /*
421 * This function is registered without the recursion safe flag.
422 * The ftrace infrastructure should provide the recursion
423 * protection. If not, this will crash the kernel!
424 */
Steven Rostedt96403882012-11-02 17:01:20 -0400425 if (trace_selftest_recursion_cnt++ > 10)
426 return;
Steven Rostedtea701f12012-07-20 13:08:05 -0400427 DYN_FTRACE_TEST_NAME();
428}
429
430static void trace_selftest_test_recursion_safe_func(unsigned long ip,
431 unsigned long pip,
432 struct ftrace_ops *op,
433 struct pt_regs *pt_regs)
434{
435 /*
436 * We said we would provide our own recursion. By calling
437 * this function again, we should recurse back into this function
438 * and count again. But this only happens if the arch supports
439 * all of ftrace features and nothing else is using the function
440 * tracing utility.
441 */
442 if (trace_selftest_recursion_cnt++)
443 return;
444 DYN_FTRACE_TEST_NAME();
445}
446
447static struct ftrace_ops test_rec_probe = {
448 .func = trace_selftest_test_recursion_func,
449};
450
451static struct ftrace_ops test_recsafe_probe = {
452 .func = trace_selftest_test_recursion_safe_func,
453 .flags = FTRACE_OPS_FL_RECURSION_SAFE,
454};
455
456static int
457trace_selftest_function_recursion(void)
458{
459 int save_ftrace_enabled = ftrace_enabled;
Steven Rostedtea701f12012-07-20 13:08:05 -0400460 char *func_name;
461 int len;
462 int ret;
Steven Rostedtea701f12012-07-20 13:08:05 -0400463
464 /* The previous test PASSED */
465 pr_cont("PASSED\n");
466 pr_info("Testing ftrace recursion: ");
467
468
469 /* enable tracing, and record the filter function */
470 ftrace_enabled = 1;
Steven Rostedtea701f12012-07-20 13:08:05 -0400471
472 /* Handle PPC64 '.' name */
473 func_name = "*" __stringify(DYN_FTRACE_TEST_NAME);
474 len = strlen(func_name);
475
476 ret = ftrace_set_filter(&test_rec_probe, func_name, len, 1);
477 if (ret) {
478 pr_cont("*Could not set filter* ");
479 goto out;
480 }
481
482 ret = register_ftrace_function(&test_rec_probe);
483 if (ret) {
484 pr_cont("*could not register callback* ");
485 goto out;
486 }
487
488 DYN_FTRACE_TEST_NAME();
489
490 unregister_ftrace_function(&test_rec_probe);
491
492 ret = -1;
Steven Rostedt (VMware)59cc02c2020-10-29 19:35:08 -0400493 /*
494 * Recursion allows for transitions between context,
495 * and may call the callback twice.
496 */
497 if (trace_selftest_recursion_cnt != 1 &&
498 trace_selftest_recursion_cnt != 2) {
499 pr_cont("*callback not called once (or twice) (%d)* ",
Steven Rostedtea701f12012-07-20 13:08:05 -0400500 trace_selftest_recursion_cnt);
501 goto out;
502 }
503
504 trace_selftest_recursion_cnt = 1;
505
506 pr_cont("PASSED\n");
507 pr_info("Testing ftrace recursion safe: ");
508
509 ret = ftrace_set_filter(&test_recsafe_probe, func_name, len, 1);
510 if (ret) {
511 pr_cont("*Could not set filter* ");
512 goto out;
513 }
514
515 ret = register_ftrace_function(&test_recsafe_probe);
516 if (ret) {
517 pr_cont("*could not register callback* ");
518 goto out;
519 }
520
521 DYN_FTRACE_TEST_NAME();
522
523 unregister_ftrace_function(&test_recsafe_probe);
524
Steven Rostedtea701f12012-07-20 13:08:05 -0400525 ret = -1;
Steven Rostedt05cbbf62013-01-22 23:35:11 -0500526 if (trace_selftest_recursion_cnt != 2) {
527 pr_cont("*callback not called expected 2 times (%d)* ",
528 trace_selftest_recursion_cnt);
Steven Rostedtea701f12012-07-20 13:08:05 -0400529 goto out;
530 }
531
532 ret = 0;
533out:
534 ftrace_enabled = save_ftrace_enabled;
Steven Rostedtea701f12012-07-20 13:08:05 -0400535
536 return ret;
537}
Steven Rostedt77a2b372008-05-12 21:20:45 +0200538#else
539# define trace_selftest_startup_dynamic_tracing(trace, tr, func) ({ 0; })
Steven Rostedtea701f12012-07-20 13:08:05 -0400540# define trace_selftest_function_recursion() ({ 0; })
Steven Rostedt77a2b372008-05-12 21:20:45 +0200541#endif /* CONFIG_DYNAMIC_FTRACE */
Ingo Molnare9a22d12009-03-13 11:54:40 +0100542
Steven Rostedtad977722012-07-20 13:45:59 -0400543static enum {
544 TRACE_SELFTEST_REGS_START,
545 TRACE_SELFTEST_REGS_FOUND,
546 TRACE_SELFTEST_REGS_NOT_FOUND,
547} trace_selftest_regs_stat;
548
549static void trace_selftest_test_regs_func(unsigned long ip,
550 unsigned long pip,
551 struct ftrace_ops *op,
552 struct pt_regs *pt_regs)
553{
554 if (pt_regs)
555 trace_selftest_regs_stat = TRACE_SELFTEST_REGS_FOUND;
556 else
557 trace_selftest_regs_stat = TRACE_SELFTEST_REGS_NOT_FOUND;
558}
559
560static struct ftrace_ops test_regs_probe = {
561 .func = trace_selftest_test_regs_func,
562 .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_SAVE_REGS,
563};
564
565static int
566trace_selftest_function_regs(void)
567{
568 int save_ftrace_enabled = ftrace_enabled;
Steven Rostedtad977722012-07-20 13:45:59 -0400569 char *func_name;
570 int len;
571 int ret;
572 int supported = 0;
573
Masami Hiramatsu06aeaae2012-09-28 17:15:17 +0900574#ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS
Steven Rostedtad977722012-07-20 13:45:59 -0400575 supported = 1;
576#endif
577
578 /* The previous test PASSED */
579 pr_cont("PASSED\n");
580 pr_info("Testing ftrace regs%s: ",
581 !supported ? "(no arch support)" : "");
582
583 /* enable tracing, and record the filter function */
584 ftrace_enabled = 1;
Steven Rostedtad977722012-07-20 13:45:59 -0400585
586 /* Handle PPC64 '.' name */
587 func_name = "*" __stringify(DYN_FTRACE_TEST_NAME);
588 len = strlen(func_name);
589
590 ret = ftrace_set_filter(&test_regs_probe, func_name, len, 1);
591 /*
592 * If DYNAMIC_FTRACE is not set, then we just trace all functions.
593 * This test really doesn't care.
594 */
595 if (ret && ret != -ENODEV) {
596 pr_cont("*Could not set filter* ");
597 goto out;
598 }
599
600 ret = register_ftrace_function(&test_regs_probe);
601 /*
602 * Now if the arch does not support passing regs, then this should
603 * have failed.
604 */
605 if (!supported) {
606 if (!ret) {
607 pr_cont("*registered save-regs without arch support* ");
608 goto out;
609 }
610 test_regs_probe.flags |= FTRACE_OPS_FL_SAVE_REGS_IF_SUPPORTED;
611 ret = register_ftrace_function(&test_regs_probe);
612 }
613 if (ret) {
614 pr_cont("*could not register callback* ");
615 goto out;
616 }
617
618
619 DYN_FTRACE_TEST_NAME();
620
621 unregister_ftrace_function(&test_regs_probe);
622
623 ret = -1;
624
625 switch (trace_selftest_regs_stat) {
626 case TRACE_SELFTEST_REGS_START:
627 pr_cont("*callback never called* ");
628 goto out;
629
630 case TRACE_SELFTEST_REGS_FOUND:
631 if (supported)
632 break;
633 pr_cont("*callback received regs without arch support* ");
634 goto out;
635
636 case TRACE_SELFTEST_REGS_NOT_FOUND:
637 if (!supported)
638 break;
639 pr_cont("*callback received NULL regs* ");
640 goto out;
641 }
642
643 ret = 0;
644out:
645 ftrace_enabled = save_ftrace_enabled;
Steven Rostedtad977722012-07-20 13:45:59 -0400646
647 return ret;
648}
649
Steven Rostedt60a11772008-05-12 21:20:44 +0200650/*
651 * Simple verification test of ftrace function tracer.
652 * Enable ftrace, sleep 1/10 second, and then read the trace
653 * buffer to see if all is in order.
654 */
Steven Rostedt (Red Hat)f1ed7c72013-06-27 22:18:06 -0400655__init int
Steven Rostedt60a11772008-05-12 21:20:44 +0200656trace_selftest_startup_function(struct tracer *trace, struct trace_array *tr)
657{
Steven Rostedt77a2b372008-05-12 21:20:45 +0200658 int save_ftrace_enabled = ftrace_enabled;
Steven Rostedtdd0e5452008-08-01 12:26:41 -0400659 unsigned long count;
660 int ret;
Steven Rostedt60a11772008-05-12 21:20:44 +0200661
Steven Rostedt (Red Hat)f1ed7c72013-06-27 22:18:06 -0400662#ifdef CONFIG_DYNAMIC_FTRACE
663 if (ftrace_filter_param) {
664 printk(KERN_CONT " ... kernel command line filter set: force PASS ... ");
665 return 0;
666 }
667#endif
668
Steven Rostedt77a2b372008-05-12 21:20:45 +0200669 /* make sure msleep has been recorded */
670 msleep(1);
671
Steven Rostedt60a11772008-05-12 21:20:44 +0200672 /* start the tracing */
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200673 ftrace_enabled = 1;
674
Arnaldo Carvalho de Melob6f11df2009-02-05 18:02:00 -0200675 ret = tracer_init(trace, tr);
Frederic Weisbecker1c800252008-11-16 05:57:26 +0100676 if (ret) {
677 warn_failed_init_tracer(trace, ret);
678 goto out;
679 }
680
Steven Rostedt60a11772008-05-12 21:20:44 +0200681 /* Sleep for a 1/10 of a second */
682 msleep(100);
683 /* stop the tracing. */
Steven Rostedtbbf5b1a2008-11-07 22:36:02 -0500684 tracing_stop();
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200685 ftrace_enabled = 0;
686
Steven Rostedt60a11772008-05-12 21:20:44 +0200687 /* check the trace buffer */
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -0500688 ret = trace_test_buffer(&tr->trace_buffer, &count);
Steven Rostedt (Red Hat)3ddee632014-09-12 14:26:51 -0400689
690 ftrace_enabled = 1;
Steven Rostedt60a11772008-05-12 21:20:44 +0200691 trace->reset(tr);
Steven Rostedtbbf5b1a2008-11-07 22:36:02 -0500692 tracing_start();
Steven Rostedt60a11772008-05-12 21:20:44 +0200693
694 if (!ret && !count) {
695 printk(KERN_CONT ".. no entries found ..");
696 ret = -1;
Steven Rostedt77a2b372008-05-12 21:20:45 +0200697 goto out;
Steven Rostedt60a11772008-05-12 21:20:44 +0200698 }
699
Steven Rostedt77a2b372008-05-12 21:20:45 +0200700 ret = trace_selftest_startup_dynamic_tracing(trace, tr,
701 DYN_FTRACE_TEST_NAME);
Steven Rostedtea701f12012-07-20 13:08:05 -0400702 if (ret)
703 goto out;
Steven Rostedt77a2b372008-05-12 21:20:45 +0200704
Steven Rostedtea701f12012-07-20 13:08:05 -0400705 ret = trace_selftest_function_recursion();
Steven Rostedtad977722012-07-20 13:45:59 -0400706 if (ret)
707 goto out;
708
709 ret = trace_selftest_function_regs();
Steven Rostedt77a2b372008-05-12 21:20:45 +0200710 out:
711 ftrace_enabled = save_ftrace_enabled;
Steven Rostedt77a2b372008-05-12 21:20:45 +0200712
Steven Rostedt4eebcc82008-05-12 21:20:48 +0200713 /* kill ftrace totally if we failed */
714 if (ret)
715 ftrace_kill();
716
Steven Rostedt60a11772008-05-12 21:20:44 +0200717 return ret;
718}
Steven Rostedt606576c2008-10-06 19:06:12 -0400719#endif /* CONFIG_FUNCTION_TRACER */
Steven Rostedt60a11772008-05-12 21:20:44 +0200720
Frederic Weisbecker7447dce2009-02-07 21:33:57 +0100721
722#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Frederic Weisbeckercf586b62009-03-22 05:04:35 +0100723
724/* Maximum number of functions to trace before diagnosing a hang */
725#define GRAPH_MAX_FUNC_TEST 100000000
726
Frederic Weisbeckercf586b62009-03-22 05:04:35 +0100727static unsigned int graph_hang_thresh;
728
729/* Wrap the real function entry probe to avoid possible hanging */
730static int trace_graph_entry_watchdog(struct ftrace_graph_ent *trace)
731{
732 /* This is harmlessly racy, we want to approximately detect a hang */
733 if (unlikely(++graph_hang_thresh > GRAPH_MAX_FUNC_TEST)) {
734 ftrace_graph_stop();
735 printk(KERN_WARNING "BUG: Function graph tracer hang!\n");
Steven Rostedt (Red Hat)7fe70b52013-03-15 13:10:35 -0400736 if (ftrace_dump_on_oops) {
737 ftrace_dump(DUMP_ALL);
738 /* ftrace_dump() disables tracing */
739 tracing_on();
740 }
Frederic Weisbeckercf586b62009-03-22 05:04:35 +0100741 return 0;
742 }
743
744 return trace_graph_entry(trace);
745}
746
Frederic Weisbecker7447dce2009-02-07 21:33:57 +0100747/*
748 * Pretty much the same than for the function tracer from which the selftest
749 * has been borrowed.
750 */
Steven Rostedt (Red Hat)f1ed7c72013-06-27 22:18:06 -0400751__init int
Frederic Weisbecker7447dce2009-02-07 21:33:57 +0100752trace_selftest_startup_function_graph(struct tracer *trace,
753 struct trace_array *tr)
754{
755 int ret;
756 unsigned long count;
757
Steven Rostedt (Red Hat)f1ed7c72013-06-27 22:18:06 -0400758#ifdef CONFIG_DYNAMIC_FTRACE
759 if (ftrace_filter_param) {
760 printk(KERN_CONT " ... kernel command line filter set: force PASS ... ");
761 return 0;
762 }
763#endif
764
Frederic Weisbeckercf586b62009-03-22 05:04:35 +0100765 /*
766 * Simulate the init() callback but we attach a watchdog callback
767 * to detect and recover from possible hangs
768 */
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -0500769 tracing_reset_online_cpus(&tr->trace_buffer);
Frederic Weisbecker1a0799a2009-07-29 18:59:58 +0200770 set_graph_array(tr);
Frederic Weisbeckercf586b62009-03-22 05:04:35 +0100771 ret = register_ftrace_graph(&trace_graph_return,
772 &trace_graph_entry_watchdog);
Frederic Weisbecker7447dce2009-02-07 21:33:57 +0100773 if (ret) {
774 warn_failed_init_tracer(trace, ret);
775 goto out;
776 }
Frederic Weisbeckercf586b62009-03-22 05:04:35 +0100777 tracing_start_cmdline_record();
Frederic Weisbecker7447dce2009-02-07 21:33:57 +0100778
779 /* Sleep for a 1/10 of a second */
780 msleep(100);
781
Frederic Weisbeckercf586b62009-03-22 05:04:35 +0100782 /* Have we just recovered from a hang? */
783 if (graph_hang_thresh > GRAPH_MAX_FUNC_TEST) {
Frederic Weisbecker0cf53ff2009-03-22 15:13:07 +0100784 tracing_selftest_disabled = true;
Frederic Weisbeckercf586b62009-03-22 05:04:35 +0100785 ret = -1;
786 goto out;
787 }
788
Frederic Weisbecker7447dce2009-02-07 21:33:57 +0100789 tracing_stop();
790
791 /* check the trace buffer */
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -0500792 ret = trace_test_buffer(&tr->trace_buffer, &count);
Frederic Weisbecker7447dce2009-02-07 21:33:57 +0100793
794 trace->reset(tr);
795 tracing_start();
796
797 if (!ret && !count) {
798 printk(KERN_CONT ".. no entries found ..");
799 ret = -1;
800 goto out;
801 }
802
803 /* Don't test dynamic tracing, the function tracer already did */
804
805out:
806 /* Stop it if we failed */
807 if (ret)
808 ftrace_graph_stop();
809
810 return ret;
811}
812#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
813
814
Steven Rostedt60a11772008-05-12 21:20:44 +0200815#ifdef CONFIG_IRQSOFF_TRACER
816int
817trace_selftest_startup_irqsoff(struct tracer *trace, struct trace_array *tr)
818{
Steven Rostedt (Red Hat)6d9b3fa2014-01-14 11:28:38 -0500819 unsigned long save_max = tr->max_latency;
Steven Rostedt60a11772008-05-12 21:20:44 +0200820 unsigned long count;
821 int ret;
822
823 /* start the tracing */
Arnaldo Carvalho de Melob6f11df2009-02-05 18:02:00 -0200824 ret = tracer_init(trace, tr);
Frederic Weisbecker1c800252008-11-16 05:57:26 +0100825 if (ret) {
826 warn_failed_init_tracer(trace, ret);
827 return ret;
828 }
829
Steven Rostedt60a11772008-05-12 21:20:44 +0200830 /* reset the max latency */
Steven Rostedt (Red Hat)6d9b3fa2014-01-14 11:28:38 -0500831 tr->max_latency = 0;
Steven Rostedt60a11772008-05-12 21:20:44 +0200832 /* disable interrupts for a bit */
833 local_irq_disable();
834 udelay(100);
835 local_irq_enable();
Frederic Weisbecker49036202009-03-17 22:38:58 +0100836
837 /*
838 * Stop the tracer to avoid a warning subsequent
839 * to buffer flipping failure because tracing_stop()
840 * disables the tr and max buffers, making flipping impossible
841 * in case of parallels max irqs off latencies.
842 */
843 trace->stop(tr);
Steven Rostedt60a11772008-05-12 21:20:44 +0200844 /* stop the tracing. */
Steven Rostedtbbf5b1a2008-11-07 22:36:02 -0500845 tracing_stop();
Steven Rostedt60a11772008-05-12 21:20:44 +0200846 /* check both trace buffers */
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -0500847 ret = trace_test_buffer(&tr->trace_buffer, NULL);
Steven Rostedt60a11772008-05-12 21:20:44 +0200848 if (!ret)
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -0500849 ret = trace_test_buffer(&tr->max_buffer, &count);
Steven Rostedt60a11772008-05-12 21:20:44 +0200850 trace->reset(tr);
Steven Rostedtbbf5b1a2008-11-07 22:36:02 -0500851 tracing_start();
Steven Rostedt60a11772008-05-12 21:20:44 +0200852
853 if (!ret && !count) {
854 printk(KERN_CONT ".. no entries found ..");
855 ret = -1;
856 }
857
Steven Rostedt (Red Hat)6d9b3fa2014-01-14 11:28:38 -0500858 tr->max_latency = save_max;
Steven Rostedt60a11772008-05-12 21:20:44 +0200859
860 return ret;
861}
862#endif /* CONFIG_IRQSOFF_TRACER */
863
864#ifdef CONFIG_PREEMPT_TRACER
865int
866trace_selftest_startup_preemptoff(struct tracer *trace, struct trace_array *tr)
867{
Steven Rostedt (Red Hat)6d9b3fa2014-01-14 11:28:38 -0500868 unsigned long save_max = tr->max_latency;
Steven Rostedt60a11772008-05-12 21:20:44 +0200869 unsigned long count;
870 int ret;
871
Steven Rostedt769c48e2008-11-07 22:36:02 -0500872 /*
873 * Now that the big kernel lock is no longer preemptable,
874 * and this is called with the BKL held, it will always
875 * fail. If preemption is already disabled, simply
876 * pass the test. When the BKL is removed, or becomes
877 * preemptible again, we will once again test this,
878 * so keep it in.
879 */
880 if (preempt_count()) {
881 printk(KERN_CONT "can not test ... force ");
882 return 0;
883 }
884
Steven Rostedt60a11772008-05-12 21:20:44 +0200885 /* start the tracing */
Arnaldo Carvalho de Melob6f11df2009-02-05 18:02:00 -0200886 ret = tracer_init(trace, tr);
Frederic Weisbecker1c800252008-11-16 05:57:26 +0100887 if (ret) {
888 warn_failed_init_tracer(trace, ret);
889 return ret;
890 }
891
Steven Rostedt60a11772008-05-12 21:20:44 +0200892 /* reset the max latency */
Steven Rostedt (Red Hat)6d9b3fa2014-01-14 11:28:38 -0500893 tr->max_latency = 0;
Steven Rostedt60a11772008-05-12 21:20:44 +0200894 /* disable preemption for a bit */
895 preempt_disable();
896 udelay(100);
897 preempt_enable();
Frederic Weisbecker49036202009-03-17 22:38:58 +0100898
899 /*
900 * Stop the tracer to avoid a warning subsequent
901 * to buffer flipping failure because tracing_stop()
902 * disables the tr and max buffers, making flipping impossible
903 * in case of parallels max preempt off latencies.
904 */
905 trace->stop(tr);
Steven Rostedt60a11772008-05-12 21:20:44 +0200906 /* stop the tracing. */
Steven Rostedtbbf5b1a2008-11-07 22:36:02 -0500907 tracing_stop();
Steven Rostedt60a11772008-05-12 21:20:44 +0200908 /* check both trace buffers */
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -0500909 ret = trace_test_buffer(&tr->trace_buffer, NULL);
Steven Rostedt60a11772008-05-12 21:20:44 +0200910 if (!ret)
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -0500911 ret = trace_test_buffer(&tr->max_buffer, &count);
Steven Rostedt60a11772008-05-12 21:20:44 +0200912 trace->reset(tr);
Steven Rostedtbbf5b1a2008-11-07 22:36:02 -0500913 tracing_start();
Steven Rostedt60a11772008-05-12 21:20:44 +0200914
915 if (!ret && !count) {
916 printk(KERN_CONT ".. no entries found ..");
917 ret = -1;
918 }
919
Steven Rostedt (Red Hat)6d9b3fa2014-01-14 11:28:38 -0500920 tr->max_latency = save_max;
Steven Rostedt60a11772008-05-12 21:20:44 +0200921
922 return ret;
923}
924#endif /* CONFIG_PREEMPT_TRACER */
925
926#if defined(CONFIG_IRQSOFF_TRACER) && defined(CONFIG_PREEMPT_TRACER)
927int
928trace_selftest_startup_preemptirqsoff(struct tracer *trace, struct trace_array *tr)
929{
Steven Rostedt (Red Hat)6d9b3fa2014-01-14 11:28:38 -0500930 unsigned long save_max = tr->max_latency;
Steven Rostedt60a11772008-05-12 21:20:44 +0200931 unsigned long count;
932 int ret;
933
Steven Rostedt769c48e2008-11-07 22:36:02 -0500934 /*
935 * Now that the big kernel lock is no longer preemptable,
936 * and this is called with the BKL held, it will always
937 * fail. If preemption is already disabled, simply
938 * pass the test. When the BKL is removed, or becomes
939 * preemptible again, we will once again test this,
940 * so keep it in.
941 */
942 if (preempt_count()) {
943 printk(KERN_CONT "can not test ... force ");
944 return 0;
945 }
946
Steven Rostedt60a11772008-05-12 21:20:44 +0200947 /* start the tracing */
Arnaldo Carvalho de Melob6f11df2009-02-05 18:02:00 -0200948 ret = tracer_init(trace, tr);
Frederic Weisbecker1c800252008-11-16 05:57:26 +0100949 if (ret) {
950 warn_failed_init_tracer(trace, ret);
Frederic Weisbeckerac1d52d2009-03-16 00:32:41 +0100951 goto out_no_start;
Frederic Weisbecker1c800252008-11-16 05:57:26 +0100952 }
Steven Rostedt60a11772008-05-12 21:20:44 +0200953
954 /* reset the max latency */
Steven Rostedt (Red Hat)6d9b3fa2014-01-14 11:28:38 -0500955 tr->max_latency = 0;
Steven Rostedt60a11772008-05-12 21:20:44 +0200956
957 /* disable preemption and interrupts for a bit */
958 preempt_disable();
959 local_irq_disable();
960 udelay(100);
961 preempt_enable();
962 /* reverse the order of preempt vs irqs */
963 local_irq_enable();
964
Frederic Weisbecker49036202009-03-17 22:38:58 +0100965 /*
966 * Stop the tracer to avoid a warning subsequent
967 * to buffer flipping failure because tracing_stop()
968 * disables the tr and max buffers, making flipping impossible
969 * in case of parallels max irqs/preempt off latencies.
970 */
971 trace->stop(tr);
Steven Rostedt60a11772008-05-12 21:20:44 +0200972 /* stop the tracing. */
Steven Rostedtbbf5b1a2008-11-07 22:36:02 -0500973 tracing_stop();
Steven Rostedt60a11772008-05-12 21:20:44 +0200974 /* check both trace buffers */
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -0500975 ret = trace_test_buffer(&tr->trace_buffer, NULL);
Frederic Weisbeckerac1d52d2009-03-16 00:32:41 +0100976 if (ret)
Steven Rostedt60a11772008-05-12 21:20:44 +0200977 goto out;
978
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -0500979 ret = trace_test_buffer(&tr->max_buffer, &count);
Frederic Weisbeckerac1d52d2009-03-16 00:32:41 +0100980 if (ret)
Steven Rostedt60a11772008-05-12 21:20:44 +0200981 goto out;
982
983 if (!ret && !count) {
984 printk(KERN_CONT ".. no entries found ..");
985 ret = -1;
986 goto out;
987 }
988
989 /* do the test by disabling interrupts first this time */
Steven Rostedt (Red Hat)6d9b3fa2014-01-14 11:28:38 -0500990 tr->max_latency = 0;
Steven Rostedtbbf5b1a2008-11-07 22:36:02 -0500991 tracing_start();
Frederic Weisbecker49036202009-03-17 22:38:58 +0100992 trace->start(tr);
993
Steven Rostedt60a11772008-05-12 21:20:44 +0200994 preempt_disable();
995 local_irq_disable();
996 udelay(100);
997 preempt_enable();
998 /* reverse the order of preempt vs irqs */
999 local_irq_enable();
1000
Frederic Weisbecker49036202009-03-17 22:38:58 +01001001 trace->stop(tr);
Steven Rostedt60a11772008-05-12 21:20:44 +02001002 /* stop the tracing. */
Steven Rostedtbbf5b1a2008-11-07 22:36:02 -05001003 tracing_stop();
Steven Rostedt60a11772008-05-12 21:20:44 +02001004 /* check both trace buffers */
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05001005 ret = trace_test_buffer(&tr->trace_buffer, NULL);
Steven Rostedt60a11772008-05-12 21:20:44 +02001006 if (ret)
1007 goto out;
1008
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05001009 ret = trace_test_buffer(&tr->max_buffer, &count);
Steven Rostedt60a11772008-05-12 21:20:44 +02001010
1011 if (!ret && !count) {
1012 printk(KERN_CONT ".. no entries found ..");
1013 ret = -1;
1014 goto out;
1015 }
1016
Frederic Weisbeckerac1d52d2009-03-16 00:32:41 +01001017out:
Steven Rostedtbbf5b1a2008-11-07 22:36:02 -05001018 tracing_start();
Frederic Weisbeckerac1d52d2009-03-16 00:32:41 +01001019out_no_start:
1020 trace->reset(tr);
Steven Rostedt (Red Hat)6d9b3fa2014-01-14 11:28:38 -05001021 tr->max_latency = save_max;
Steven Rostedt60a11772008-05-12 21:20:44 +02001022
1023 return ret;
1024}
1025#endif /* CONFIG_IRQSOFF_TRACER && CONFIG_PREEMPT_TRACER */
1026
Steven Noonanfb1b6d82008-09-19 03:06:43 -07001027#ifdef CONFIG_NOP_TRACER
1028int
1029trace_selftest_startup_nop(struct tracer *trace, struct trace_array *tr)
1030{
1031 /* What could possibly go wrong? */
1032 return 0;
1033}
1034#endif
1035
Steven Rostedt60a11772008-05-12 21:20:44 +02001036#ifdef CONFIG_SCHED_TRACER
Steven Rostedtaddff1f2014-10-08 13:52:16 -04001037
1038struct wakeup_test_data {
1039 struct completion is_ready;
1040 int go;
1041};
1042
Steven Rostedt60a11772008-05-12 21:20:44 +02001043static int trace_wakeup_test_thread(void *data)
1044{
Dario Faggioliaf6ace72013-11-07 14:43:42 +01001045 /* Make this a -deadline thread */
1046 static const struct sched_attr attr = {
1047 .sched_policy = SCHED_DEADLINE,
1048 .sched_runtime = 100000ULL,
1049 .sched_deadline = 10000000ULL,
1050 .sched_period = 10000000ULL
1051 };
Steven Rostedtaddff1f2014-10-08 13:52:16 -04001052 struct wakeup_test_data *x = data;
Steven Rostedt60a11772008-05-12 21:20:44 +02001053
Dario Faggioliaf6ace72013-11-07 14:43:42 +01001054 sched_setattr(current, &attr);
Steven Rostedt60a11772008-05-12 21:20:44 +02001055
1056 /* Make it know we have a new prio */
Steven Rostedtaddff1f2014-10-08 13:52:16 -04001057 complete(&x->is_ready);
Steven Rostedt60a11772008-05-12 21:20:44 +02001058
1059 /* now go to sleep and let the test wake us up */
1060 set_current_state(TASK_INTERRUPTIBLE);
Steven Rostedtaddff1f2014-10-08 13:52:16 -04001061 while (!x->go) {
1062 schedule();
1063 set_current_state(TASK_INTERRUPTIBLE);
1064 }
Steven Rostedt60a11772008-05-12 21:20:44 +02001065
Steven Rostedtaddff1f2014-10-08 13:52:16 -04001066 complete(&x->is_ready);
1067
1068 set_current_state(TASK_INTERRUPTIBLE);
Steven Rostedt3c18c102012-07-31 10:23:37 -04001069
Steven Rostedt60a11772008-05-12 21:20:44 +02001070 /* we are awake, now wait to disappear */
1071 while (!kthread_should_stop()) {
Steven Rostedtaddff1f2014-10-08 13:52:16 -04001072 schedule();
1073 set_current_state(TASK_INTERRUPTIBLE);
Steven Rostedt60a11772008-05-12 21:20:44 +02001074 }
1075
Steven Rostedtaddff1f2014-10-08 13:52:16 -04001076 __set_current_state(TASK_RUNNING);
1077
Steven Rostedt60a11772008-05-12 21:20:44 +02001078 return 0;
1079}
Steven Rostedt60a11772008-05-12 21:20:44 +02001080int
1081trace_selftest_startup_wakeup(struct tracer *trace, struct trace_array *tr)
1082{
Steven Rostedt (Red Hat)6d9b3fa2014-01-14 11:28:38 -05001083 unsigned long save_max = tr->max_latency;
Steven Rostedt60a11772008-05-12 21:20:44 +02001084 struct task_struct *p;
Steven Rostedtaddff1f2014-10-08 13:52:16 -04001085 struct wakeup_test_data data;
Steven Rostedt60a11772008-05-12 21:20:44 +02001086 unsigned long count;
1087 int ret;
1088
Steven Rostedtaddff1f2014-10-08 13:52:16 -04001089 memset(&data, 0, sizeof(data));
1090
1091 init_completion(&data.is_ready);
Steven Rostedt60a11772008-05-12 21:20:44 +02001092
Dario Faggioliaf6ace72013-11-07 14:43:42 +01001093 /* create a -deadline thread */
Steven Rostedtaddff1f2014-10-08 13:52:16 -04001094 p = kthread_run(trace_wakeup_test_thread, &data, "ftrace-test");
Ingo Molnarc7aafc52008-05-12 21:20:45 +02001095 if (IS_ERR(p)) {
Steven Rostedt60a11772008-05-12 21:20:44 +02001096 printk(KERN_CONT "Failed to create ftrace wakeup test thread ");
1097 return -1;
1098 }
1099
Dario Faggioliaf6ace72013-11-07 14:43:42 +01001100 /* make sure the thread is running at -deadline policy */
Steven Rostedtaddff1f2014-10-08 13:52:16 -04001101 wait_for_completion(&data.is_ready);
Steven Rostedt60a11772008-05-12 21:20:44 +02001102
1103 /* start the tracing */
Arnaldo Carvalho de Melob6f11df2009-02-05 18:02:00 -02001104 ret = tracer_init(trace, tr);
Frederic Weisbecker1c800252008-11-16 05:57:26 +01001105 if (ret) {
1106 warn_failed_init_tracer(trace, ret);
1107 return ret;
1108 }
1109
Steven Rostedt60a11772008-05-12 21:20:44 +02001110 /* reset the max latency */
Steven Rostedt (Red Hat)6d9b3fa2014-01-14 11:28:38 -05001111 tr->max_latency = 0;
Steven Rostedt60a11772008-05-12 21:20:44 +02001112
Steven Rostedt3c18c102012-07-31 10:23:37 -04001113 while (p->on_rq) {
1114 /*
Dario Faggioliaf6ace72013-11-07 14:43:42 +01001115 * Sleep to make sure the -deadline thread is asleep too.
Steven Rostedt3c18c102012-07-31 10:23:37 -04001116 * On virtual machines we can't rely on timings,
1117 * but we want to make sure this test still works.
1118 */
1119 msleep(100);
1120 }
Steven Rostedt60a11772008-05-12 21:20:44 +02001121
Steven Rostedtaddff1f2014-10-08 13:52:16 -04001122 init_completion(&data.is_ready);
1123
1124 data.go = 1;
1125 /* memory barrier is in the wake_up_process() */
Steven Rostedt60a11772008-05-12 21:20:44 +02001126
1127 wake_up_process(p);
1128
Steven Rostedt3c18c102012-07-31 10:23:37 -04001129 /* Wait for the task to wake up */
Steven Rostedtaddff1f2014-10-08 13:52:16 -04001130 wait_for_completion(&data.is_ready);
Steven Rostedt5aa60c62008-09-29 23:02:37 -04001131
Steven Rostedt60a11772008-05-12 21:20:44 +02001132 /* stop the tracing. */
Steven Rostedtbbf5b1a2008-11-07 22:36:02 -05001133 tracing_stop();
Steven Rostedt60a11772008-05-12 21:20:44 +02001134 /* check both trace buffers */
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05001135 ret = trace_test_buffer(&tr->trace_buffer, NULL);
Steven Rostedt60a11772008-05-12 21:20:44 +02001136 if (!ret)
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05001137 ret = trace_test_buffer(&tr->max_buffer, &count);
Steven Rostedt60a11772008-05-12 21:20:44 +02001138
1139
1140 trace->reset(tr);
Steven Rostedtbbf5b1a2008-11-07 22:36:02 -05001141 tracing_start();
Steven Rostedt60a11772008-05-12 21:20:44 +02001142
Steven Rostedt (Red Hat)6d9b3fa2014-01-14 11:28:38 -05001143 tr->max_latency = save_max;
Steven Rostedt60a11772008-05-12 21:20:44 +02001144
1145 /* kill the thread */
1146 kthread_stop(p);
1147
1148 if (!ret && !count) {
1149 printk(KERN_CONT ".. no entries found ..");
1150 ret = -1;
1151 }
1152
1153 return ret;
1154}
1155#endif /* CONFIG_SCHED_TRACER */
1156
1157#ifdef CONFIG_CONTEXT_SWITCH_TRACER
1158int
1159trace_selftest_startup_sched_switch(struct tracer *trace, struct trace_array *tr)
1160{
1161 unsigned long count;
1162 int ret;
1163
1164 /* start the tracing */
Arnaldo Carvalho de Melob6f11df2009-02-05 18:02:00 -02001165 ret = tracer_init(trace, tr);
Frederic Weisbecker1c800252008-11-16 05:57:26 +01001166 if (ret) {
1167 warn_failed_init_tracer(trace, ret);
1168 return ret;
1169 }
1170
Steven Rostedt60a11772008-05-12 21:20:44 +02001171 /* Sleep for a 1/10 of a second */
1172 msleep(100);
1173 /* stop the tracing. */
Steven Rostedtbbf5b1a2008-11-07 22:36:02 -05001174 tracing_stop();
Steven Rostedt60a11772008-05-12 21:20:44 +02001175 /* check the trace buffer */
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -05001176 ret = trace_test_buffer(&tr->trace_buffer, &count);
Steven Rostedt60a11772008-05-12 21:20:44 +02001177 trace->reset(tr);
Steven Rostedtbbf5b1a2008-11-07 22:36:02 -05001178 tracing_start();
Steven Rostedt60a11772008-05-12 21:20:44 +02001179
1180 if (!ret && !count) {
1181 printk(KERN_CONT ".. no entries found ..");
1182 ret = -1;
1183 }
1184
1185 return ret;
1186}
1187#endif /* CONFIG_CONTEXT_SWITCH_TRACER */
Ingo Molnara6dd24f2008-05-12 21:20:47 +02001188
Steven Rostedt80e5ea42008-11-12 15:24:24 -05001189#ifdef CONFIG_BRANCH_TRACER
1190int
1191trace_selftest_startup_branch(struct tracer *trace, struct trace_array *tr)
1192{
1193 unsigned long count;
1194 int ret;
1195
1196 /* start the tracing */
Arnaldo Carvalho de Melob6f11df2009-02-05 18:02:00 -02001197 ret = tracer_init(trace, tr);
Frederic Weisbecker1c800252008-11-16 05:57:26 +01001198 if (ret) {
1199 warn_failed_init_tracer(trace, ret);
1200 return ret;
1201 }
1202
Steven Rostedt80e5ea42008-11-12 15:24:24 -05001203 /* Sleep for a 1/10 of a second */
1204 msleep(100);
1205 /* stop the tracing. */
1206 tracing_stop();
1207 /* check the trace buffer */
Steven Rostedt (Red Hat)0184d502013-05-29 15:56:49 -04001208 ret = trace_test_buffer(&tr->trace_buffer, &count);
Steven Rostedt80e5ea42008-11-12 15:24:24 -05001209 trace->reset(tr);
1210 tracing_start();
1211
Wenji Huangd2ef7c22009-02-17 01:09:47 -05001212 if (!ret && !count) {
1213 printk(KERN_CONT ".. no entries found ..");
1214 ret = -1;
1215 }
1216
Steven Rostedt80e5ea42008-11-12 15:24:24 -05001217 return ret;
1218}
1219#endif /* CONFIG_BRANCH_TRACER */
Markus Metzger321bb5e2009-03-13 10:50:27 +01001220