trace_selftest.c 21 KB

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