trace_selftest.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933
  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 unsigned int graph_hang_thresh;
  377. /* Wrap the real function entry probe to avoid possible hanging */
  378. static int trace_graph_entry_watchdog(struct ftrace_graph_ent *trace)
  379. {
  380. /* This is harmlessly racy, we want to approximately detect a hang */
  381. if (unlikely(++graph_hang_thresh > GRAPH_MAX_FUNC_TEST)) {
  382. ftrace_graph_stop();
  383. printk(KERN_WARNING "BUG: Function graph tracer hang!\n");
  384. if (ftrace_dump_on_oops) {
  385. ftrace_dump(DUMP_ALL);
  386. /* ftrace_dump() disables tracing */
  387. tracing_on();
  388. }
  389. return 0;
  390. }
  391. return trace_graph_entry(trace);
  392. }
  393. /*
  394. * Pretty much the same than for the function tracer from which the selftest
  395. * has been borrowed.
  396. */
  397. int
  398. trace_selftest_startup_function_graph(struct tracer *trace,
  399. struct trace_array *tr)
  400. {
  401. int ret;
  402. unsigned long count;
  403. /*
  404. * Simulate the init() callback but we attach a watchdog callback
  405. * to detect and recover from possible hangs
  406. */
  407. tracing_reset_online_cpus(tr);
  408. set_graph_array(tr);
  409. ret = register_ftrace_graph(&trace_graph_return,
  410. &trace_graph_entry_watchdog);
  411. if (ret) {
  412. warn_failed_init_tracer(trace, ret);
  413. goto out;
  414. }
  415. tracing_start_cmdline_record();
  416. /* Sleep for a 1/10 of a second */
  417. msleep(100);
  418. /* Have we just recovered from a hang? */
  419. if (graph_hang_thresh > GRAPH_MAX_FUNC_TEST) {
  420. tracing_selftest_disabled = true;
  421. ret = -1;
  422. goto out;
  423. }
  424. tracing_stop();
  425. /* check the trace buffer */
  426. ret = trace_test_buffer(tr, &count);
  427. trace->reset(tr);
  428. tracing_start();
  429. if (!ret && !count) {
  430. printk(KERN_CONT ".. no entries found ..");
  431. ret = -1;
  432. goto out;
  433. }
  434. /* Don't test dynamic tracing, the function tracer already did */
  435. out:
  436. /* Stop it if we failed */
  437. if (ret)
  438. ftrace_graph_stop();
  439. return ret;
  440. }
  441. #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
  442. #ifdef CONFIG_IRQSOFF_TRACER
  443. int
  444. trace_selftest_startup_irqsoff(struct tracer *trace, struct trace_array *tr)
  445. {
  446. unsigned long save_max = tracing_max_latency;
  447. unsigned long count;
  448. int ret;
  449. /* start the tracing */
  450. ret = tracer_init(trace, tr);
  451. if (ret) {
  452. warn_failed_init_tracer(trace, ret);
  453. return ret;
  454. }
  455. /* reset the max latency */
  456. tracing_max_latency = 0;
  457. /* disable interrupts for a bit */
  458. local_irq_disable();
  459. udelay(100);
  460. local_irq_enable();
  461. /*
  462. * Stop the tracer to avoid a warning subsequent
  463. * to buffer flipping failure because tracing_stop()
  464. * disables the tr and max buffers, making flipping impossible
  465. * in case of parallels max irqs off latencies.
  466. */
  467. trace->stop(tr);
  468. /* stop the tracing. */
  469. tracing_stop();
  470. /* check both trace buffers */
  471. ret = trace_test_buffer(tr, NULL);
  472. if (!ret)
  473. ret = trace_test_buffer(&max_tr, &count);
  474. trace->reset(tr);
  475. tracing_start();
  476. if (!ret && !count) {
  477. printk(KERN_CONT ".. no entries found ..");
  478. ret = -1;
  479. }
  480. tracing_max_latency = save_max;
  481. return ret;
  482. }
  483. #endif /* CONFIG_IRQSOFF_TRACER */
  484. #ifdef CONFIG_PREEMPT_TRACER
  485. int
  486. trace_selftest_startup_preemptoff(struct tracer *trace, struct trace_array *tr)
  487. {
  488. unsigned long save_max = tracing_max_latency;
  489. unsigned long count;
  490. int ret;
  491. /*
  492. * Now that the big kernel lock is no longer preemptable,
  493. * and this is called with the BKL held, it will always
  494. * fail. If preemption is already disabled, simply
  495. * pass the test. When the BKL is removed, or becomes
  496. * preemptible again, we will once again test this,
  497. * so keep it in.
  498. */
  499. if (preempt_count()) {
  500. printk(KERN_CONT "can not test ... force ");
  501. return 0;
  502. }
  503. /* start the tracing */
  504. ret = tracer_init(trace, tr);
  505. if (ret) {
  506. warn_failed_init_tracer(trace, ret);
  507. return ret;
  508. }
  509. /* reset the max latency */
  510. tracing_max_latency = 0;
  511. /* disable preemption for a bit */
  512. preempt_disable();
  513. udelay(100);
  514. preempt_enable();
  515. /*
  516. * Stop the tracer to avoid a warning subsequent
  517. * to buffer flipping failure because tracing_stop()
  518. * disables the tr and max buffers, making flipping impossible
  519. * in case of parallels max preempt off latencies.
  520. */
  521. trace->stop(tr);
  522. /* stop the tracing. */
  523. tracing_stop();
  524. /* check both trace buffers */
  525. ret = trace_test_buffer(tr, NULL);
  526. if (!ret)
  527. ret = trace_test_buffer(&max_tr, &count);
  528. trace->reset(tr);
  529. tracing_start();
  530. if (!ret && !count) {
  531. printk(KERN_CONT ".. no entries found ..");
  532. ret = -1;
  533. }
  534. tracing_max_latency = save_max;
  535. return ret;
  536. }
  537. #endif /* CONFIG_PREEMPT_TRACER */
  538. #if defined(CONFIG_IRQSOFF_TRACER) && defined(CONFIG_PREEMPT_TRACER)
  539. int
  540. trace_selftest_startup_preemptirqsoff(struct tracer *trace, struct trace_array *tr)
  541. {
  542. unsigned long save_max = tracing_max_latency;
  543. unsigned long count;
  544. int ret;
  545. /*
  546. * Now that the big kernel lock is no longer preemptable,
  547. * and this is called with the BKL held, it will always
  548. * fail. If preemption is already disabled, simply
  549. * pass the test. When the BKL is removed, or becomes
  550. * preemptible again, we will once again test this,
  551. * so keep it in.
  552. */
  553. if (preempt_count()) {
  554. printk(KERN_CONT "can not test ... force ");
  555. return 0;
  556. }
  557. /* start the tracing */
  558. ret = tracer_init(trace, tr);
  559. if (ret) {
  560. warn_failed_init_tracer(trace, ret);
  561. goto out_no_start;
  562. }
  563. /* reset the max latency */
  564. tracing_max_latency = 0;
  565. /* disable preemption and interrupts for a bit */
  566. preempt_disable();
  567. local_irq_disable();
  568. udelay(100);
  569. preempt_enable();
  570. /* reverse the order of preempt vs irqs */
  571. local_irq_enable();
  572. /*
  573. * Stop the tracer to avoid a warning subsequent
  574. * to buffer flipping failure because tracing_stop()
  575. * disables the tr and max buffers, making flipping impossible
  576. * in case of parallels max irqs/preempt off latencies.
  577. */
  578. trace->stop(tr);
  579. /* stop the tracing. */
  580. tracing_stop();
  581. /* check both trace buffers */
  582. ret = trace_test_buffer(tr, NULL);
  583. if (ret)
  584. goto out;
  585. ret = trace_test_buffer(&max_tr, &count);
  586. if (ret)
  587. goto out;
  588. if (!ret && !count) {
  589. printk(KERN_CONT ".. no entries found ..");
  590. ret = -1;
  591. goto out;
  592. }
  593. /* do the test by disabling interrupts first this time */
  594. tracing_max_latency = 0;
  595. tracing_start();
  596. trace->start(tr);
  597. preempt_disable();
  598. local_irq_disable();
  599. udelay(100);
  600. preempt_enable();
  601. /* reverse the order of preempt vs irqs */
  602. local_irq_enable();
  603. trace->stop(tr);
  604. /* stop the tracing. */
  605. tracing_stop();
  606. /* check both trace buffers */
  607. ret = trace_test_buffer(tr, NULL);
  608. if (ret)
  609. goto out;
  610. ret = trace_test_buffer(&max_tr, &count);
  611. if (!ret && !count) {
  612. printk(KERN_CONT ".. no entries found ..");
  613. ret = -1;
  614. goto out;
  615. }
  616. out:
  617. tracing_start();
  618. out_no_start:
  619. trace->reset(tr);
  620. tracing_max_latency = save_max;
  621. return ret;
  622. }
  623. #endif /* CONFIG_IRQSOFF_TRACER && CONFIG_PREEMPT_TRACER */
  624. #ifdef CONFIG_NOP_TRACER
  625. int
  626. trace_selftest_startup_nop(struct tracer *trace, struct trace_array *tr)
  627. {
  628. /* What could possibly go wrong? */
  629. return 0;
  630. }
  631. #endif
  632. #ifdef CONFIG_SCHED_TRACER
  633. static int trace_wakeup_test_thread(void *data)
  634. {
  635. /* Make this a RT thread, doesn't need to be too high */
  636. static const struct sched_param param = { .sched_priority = 5 };
  637. struct completion *x = data;
  638. sched_setscheduler(current, SCHED_FIFO, &param);
  639. /* Make it know we have a new prio */
  640. complete(x);
  641. /* now go to sleep and let the test wake us up */
  642. set_current_state(TASK_INTERRUPTIBLE);
  643. schedule();
  644. /* we are awake, now wait to disappear */
  645. while (!kthread_should_stop()) {
  646. /*
  647. * This is an RT task, do short sleeps to let
  648. * others run.
  649. */
  650. msleep(100);
  651. }
  652. return 0;
  653. }
  654. int
  655. trace_selftest_startup_wakeup(struct tracer *trace, struct trace_array *tr)
  656. {
  657. unsigned long save_max = tracing_max_latency;
  658. struct task_struct *p;
  659. struct completion isrt;
  660. unsigned long count;
  661. int ret;
  662. init_completion(&isrt);
  663. /* create a high prio thread */
  664. p = kthread_run(trace_wakeup_test_thread, &isrt, "ftrace-test");
  665. if (IS_ERR(p)) {
  666. printk(KERN_CONT "Failed to create ftrace wakeup test thread ");
  667. return -1;
  668. }
  669. /* make sure the thread is running at an RT prio */
  670. wait_for_completion(&isrt);
  671. /* start the tracing */
  672. ret = tracer_init(trace, tr);
  673. if (ret) {
  674. warn_failed_init_tracer(trace, ret);
  675. return ret;
  676. }
  677. /* reset the max latency */
  678. tracing_max_latency = 0;
  679. /* sleep to let the RT thread sleep too */
  680. msleep(100);
  681. /*
  682. * Yes this is slightly racy. It is possible that for some
  683. * strange reason that the RT thread we created, did not
  684. * call schedule for 100ms after doing the completion,
  685. * and we do a wakeup on a task that already is awake.
  686. * But that is extremely unlikely, and the worst thing that
  687. * happens in such a case, is that we disable tracing.
  688. * Honestly, if this race does happen something is horrible
  689. * wrong with the system.
  690. */
  691. wake_up_process(p);
  692. /* give a little time to let the thread wake up */
  693. msleep(100);
  694. /* stop the tracing. */
  695. tracing_stop();
  696. /* check both trace buffers */
  697. ret = trace_test_buffer(tr, NULL);
  698. if (!ret)
  699. ret = trace_test_buffer(&max_tr, &count);
  700. trace->reset(tr);
  701. tracing_start();
  702. tracing_max_latency = save_max;
  703. /* kill the thread */
  704. kthread_stop(p);
  705. if (!ret && !count) {
  706. printk(KERN_CONT ".. no entries found ..");
  707. ret = -1;
  708. }
  709. return ret;
  710. }
  711. #endif /* CONFIG_SCHED_TRACER */
  712. #ifdef CONFIG_CONTEXT_SWITCH_TRACER
  713. int
  714. trace_selftest_startup_sched_switch(struct tracer *trace, struct trace_array *tr)
  715. {
  716. unsigned long count;
  717. int ret;
  718. /* start the tracing */
  719. ret = tracer_init(trace, tr);
  720. if (ret) {
  721. warn_failed_init_tracer(trace, ret);
  722. return ret;
  723. }
  724. /* Sleep for a 1/10 of a second */
  725. msleep(100);
  726. /* stop the tracing. */
  727. tracing_stop();
  728. /* check the trace buffer */
  729. ret = trace_test_buffer(tr, &count);
  730. trace->reset(tr);
  731. tracing_start();
  732. if (!ret && !count) {
  733. printk(KERN_CONT ".. no entries found ..");
  734. ret = -1;
  735. }
  736. return ret;
  737. }
  738. #endif /* CONFIG_CONTEXT_SWITCH_TRACER */
  739. #ifdef CONFIG_BRANCH_TRACER
  740. int
  741. trace_selftest_startup_branch(struct tracer *trace, struct trace_array *tr)
  742. {
  743. unsigned long count;
  744. int ret;
  745. /* start the tracing */
  746. ret = tracer_init(trace, tr);
  747. if (ret) {
  748. warn_failed_init_tracer(trace, ret);
  749. return ret;
  750. }
  751. /* Sleep for a 1/10 of a second */
  752. msleep(100);
  753. /* stop the tracing. */
  754. tracing_stop();
  755. /* check the trace buffer */
  756. ret = trace_test_buffer(tr, &count);
  757. trace->reset(tr);
  758. tracing_start();
  759. if (!ret && !count) {
  760. printk(KERN_CONT ".. no entries found ..");
  761. ret = -1;
  762. }
  763. return ret;
  764. }
  765. #endif /* CONFIG_BRANCH_TRACER */