trace_selftest.c 20 KB

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