trace_selftest.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715
  1. /* Include in trace.c */
  2. #include <linux/stringify.h>
  3. #include <linux/kthread.h>
  4. #include <linux/delay.h>
  5. static inline int trace_valid_entry(struct trace_entry *entry)
  6. {
  7. switch (entry->type) {
  8. case TRACE_FN:
  9. case TRACE_CTX:
  10. case TRACE_WAKE:
  11. case TRACE_STACK:
  12. case TRACE_PRINT:
  13. case TRACE_SPECIAL:
  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))) {
  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. __raw_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. __raw_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. /* Test dynamic code modification and ftrace filters */
  90. int trace_selftest_startup_dynamic_tracing(struct tracer *trace,
  91. struct trace_array *tr,
  92. int (*func)(void))
  93. {
  94. int save_ftrace_enabled = ftrace_enabled;
  95. int save_tracer_enabled = tracer_enabled;
  96. unsigned long count;
  97. char *func_name;
  98. int ret;
  99. /* The ftrace test PASSED */
  100. printk(KERN_CONT "PASSED\n");
  101. pr_info("Testing dynamic ftrace: ");
  102. /* enable tracing, and record the filter function */
  103. ftrace_enabled = 1;
  104. tracer_enabled = 1;
  105. /* passed in by parameter to fool gcc from optimizing */
  106. func();
  107. /*
  108. * Some archs *cough*PowerPC*cough* add characters to the
  109. * start of the function names. We simply put a '*' to
  110. * accommodate them.
  111. */
  112. func_name = "*" __stringify(DYN_FTRACE_TEST_NAME);
  113. /* filter only on our function */
  114. ftrace_set_filter(func_name, strlen(func_name), 1);
  115. /* enable tracing */
  116. ret = tracer_init(trace, tr);
  117. if (ret) {
  118. warn_failed_init_tracer(trace, ret);
  119. goto out;
  120. }
  121. /* Sleep for a 1/10 of a second */
  122. msleep(100);
  123. /* we should have nothing in the buffer */
  124. ret = trace_test_buffer(tr, &count);
  125. if (ret)
  126. goto out;
  127. if (count) {
  128. ret = -1;
  129. printk(KERN_CONT ".. filter did not filter .. ");
  130. goto out;
  131. }
  132. /* call our function again */
  133. func();
  134. /* sleep again */
  135. msleep(100);
  136. /* stop the tracing. */
  137. tracing_stop();
  138. ftrace_enabled = 0;
  139. /* check the trace buffer */
  140. ret = trace_test_buffer(tr, &count);
  141. trace->reset(tr);
  142. tracing_start();
  143. /* we should only have one item */
  144. if (!ret && count != 1) {
  145. printk(KERN_CONT ".. filter failed count=%ld ..", count);
  146. ret = -1;
  147. goto out;
  148. }
  149. out:
  150. ftrace_enabled = save_ftrace_enabled;
  151. tracer_enabled = save_tracer_enabled;
  152. /* Enable tracing on all functions again */
  153. ftrace_set_filter(NULL, 0, 1);
  154. return ret;
  155. }
  156. #else
  157. # define trace_selftest_startup_dynamic_tracing(trace, tr, func) ({ 0; })
  158. #endif /* CONFIG_DYNAMIC_FTRACE */
  159. /*
  160. * Simple verification test of ftrace function tracer.
  161. * Enable ftrace, sleep 1/10 second, and then read the trace
  162. * buffer to see if all is in order.
  163. */
  164. int
  165. trace_selftest_startup_function(struct tracer *trace, struct trace_array *tr)
  166. {
  167. int save_ftrace_enabled = ftrace_enabled;
  168. int save_tracer_enabled = tracer_enabled;
  169. unsigned long count;
  170. int ret;
  171. /* make sure msleep has been recorded */
  172. msleep(1);
  173. /* start the tracing */
  174. ftrace_enabled = 1;
  175. tracer_enabled = 1;
  176. ret = tracer_init(trace, tr);
  177. if (ret) {
  178. warn_failed_init_tracer(trace, ret);
  179. goto out;
  180. }
  181. /* Sleep for a 1/10 of a second */
  182. msleep(100);
  183. /* stop the tracing. */
  184. tracing_stop();
  185. ftrace_enabled = 0;
  186. /* check the trace buffer */
  187. ret = trace_test_buffer(tr, &count);
  188. trace->reset(tr);
  189. tracing_start();
  190. if (!ret && !count) {
  191. printk(KERN_CONT ".. no entries found ..");
  192. ret = -1;
  193. goto out;
  194. }
  195. ret = trace_selftest_startup_dynamic_tracing(trace, tr,
  196. DYN_FTRACE_TEST_NAME);
  197. out:
  198. ftrace_enabled = save_ftrace_enabled;
  199. tracer_enabled = save_tracer_enabled;
  200. /* kill ftrace totally if we failed */
  201. if (ret)
  202. ftrace_kill();
  203. return ret;
  204. }
  205. #endif /* CONFIG_FUNCTION_TRACER */
  206. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  207. /*
  208. * Pretty much the same than for the function tracer from which the selftest
  209. * has been borrowed.
  210. */
  211. int
  212. trace_selftest_startup_function_graph(struct tracer *trace,
  213. struct trace_array *tr)
  214. {
  215. int ret;
  216. unsigned long count;
  217. ret = tracer_init(trace, tr);
  218. if (ret) {
  219. warn_failed_init_tracer(trace, ret);
  220. goto out;
  221. }
  222. /* Sleep for a 1/10 of a second */
  223. msleep(100);
  224. tracing_stop();
  225. /* check the trace buffer */
  226. ret = trace_test_buffer(tr, &count);
  227. trace->reset(tr);
  228. tracing_start();
  229. if (!ret && !count) {
  230. printk(KERN_CONT ".. no entries found ..");
  231. ret = -1;
  232. goto out;
  233. }
  234. /* Don't test dynamic tracing, the function tracer already did */
  235. out:
  236. /* Stop it if we failed */
  237. if (ret)
  238. ftrace_graph_stop();
  239. return ret;
  240. }
  241. #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
  242. #ifdef CONFIG_IRQSOFF_TRACER
  243. int
  244. trace_selftest_startup_irqsoff(struct tracer *trace, struct trace_array *tr)
  245. {
  246. unsigned long save_max = tracing_max_latency;
  247. unsigned long count;
  248. int ret;
  249. /* start the tracing */
  250. ret = tracer_init(trace, tr);
  251. if (ret) {
  252. warn_failed_init_tracer(trace, ret);
  253. return ret;
  254. }
  255. /* reset the max latency */
  256. tracing_max_latency = 0;
  257. /* disable interrupts for a bit */
  258. local_irq_disable();
  259. udelay(100);
  260. local_irq_enable();
  261. /*
  262. * Stop the tracer to avoid a warning subsequent
  263. * to buffer flipping failure because tracing_stop()
  264. * disables the tr and max buffers, making flipping impossible
  265. * in case of parallels max irqs off latencies.
  266. */
  267. trace->stop(tr);
  268. /* stop the tracing. */
  269. tracing_stop();
  270. /* check both trace buffers */
  271. ret = trace_test_buffer(tr, NULL);
  272. if (!ret)
  273. ret = trace_test_buffer(&max_tr, &count);
  274. trace->reset(tr);
  275. tracing_start();
  276. if (!ret && !count) {
  277. printk(KERN_CONT ".. no entries found ..");
  278. ret = -1;
  279. }
  280. tracing_max_latency = save_max;
  281. return ret;
  282. }
  283. #endif /* CONFIG_IRQSOFF_TRACER */
  284. #ifdef CONFIG_PREEMPT_TRACER
  285. int
  286. trace_selftest_startup_preemptoff(struct tracer *trace, struct trace_array *tr)
  287. {
  288. unsigned long save_max = tracing_max_latency;
  289. unsigned long count;
  290. int ret;
  291. /*
  292. * Now that the big kernel lock is no longer preemptable,
  293. * and this is called with the BKL held, it will always
  294. * fail. If preemption is already disabled, simply
  295. * pass the test. When the BKL is removed, or becomes
  296. * preemptible again, we will once again test this,
  297. * so keep it in.
  298. */
  299. if (preempt_count()) {
  300. printk(KERN_CONT "can not test ... force ");
  301. return 0;
  302. }
  303. /* start the tracing */
  304. ret = tracer_init(trace, tr);
  305. if (ret) {
  306. warn_failed_init_tracer(trace, ret);
  307. return ret;
  308. }
  309. /* reset the max latency */
  310. tracing_max_latency = 0;
  311. /* disable preemption for a bit */
  312. preempt_disable();
  313. udelay(100);
  314. preempt_enable();
  315. /*
  316. * Stop the tracer to avoid a warning subsequent
  317. * to buffer flipping failure because tracing_stop()
  318. * disables the tr and max buffers, making flipping impossible
  319. * in case of parallels max preempt off latencies.
  320. */
  321. trace->stop(tr);
  322. /* stop the tracing. */
  323. tracing_stop();
  324. /* check both trace buffers */
  325. ret = trace_test_buffer(tr, NULL);
  326. if (!ret)
  327. ret = trace_test_buffer(&max_tr, &count);
  328. trace->reset(tr);
  329. tracing_start();
  330. if (!ret && !count) {
  331. printk(KERN_CONT ".. no entries found ..");
  332. ret = -1;
  333. }
  334. tracing_max_latency = save_max;
  335. return ret;
  336. }
  337. #endif /* CONFIG_PREEMPT_TRACER */
  338. #if defined(CONFIG_IRQSOFF_TRACER) && defined(CONFIG_PREEMPT_TRACER)
  339. int
  340. trace_selftest_startup_preemptirqsoff(struct tracer *trace, struct trace_array *tr)
  341. {
  342. unsigned long save_max = tracing_max_latency;
  343. unsigned long count;
  344. int ret;
  345. /*
  346. * Now that the big kernel lock is no longer preemptable,
  347. * and this is called with the BKL held, it will always
  348. * fail. If preemption is already disabled, simply
  349. * pass the test. When the BKL is removed, or becomes
  350. * preemptible again, we will once again test this,
  351. * so keep it in.
  352. */
  353. if (preempt_count()) {
  354. printk(KERN_CONT "can not test ... force ");
  355. return 0;
  356. }
  357. /* start the tracing */
  358. ret = tracer_init(trace, tr);
  359. if (ret) {
  360. warn_failed_init_tracer(trace, ret);
  361. goto out_no_start;
  362. }
  363. /* reset the max latency */
  364. tracing_max_latency = 0;
  365. /* disable preemption and interrupts for a bit */
  366. preempt_disable();
  367. local_irq_disable();
  368. udelay(100);
  369. preempt_enable();
  370. /* reverse the order of preempt vs irqs */
  371. local_irq_enable();
  372. /*
  373. * Stop the tracer to avoid a warning subsequent
  374. * to buffer flipping failure because tracing_stop()
  375. * disables the tr and max buffers, making flipping impossible
  376. * in case of parallels max irqs/preempt off latencies.
  377. */
  378. trace->stop(tr);
  379. /* stop the tracing. */
  380. tracing_stop();
  381. /* check both trace buffers */
  382. ret = trace_test_buffer(tr, NULL);
  383. if (ret)
  384. goto out;
  385. ret = trace_test_buffer(&max_tr, &count);
  386. if (ret)
  387. goto out;
  388. if (!ret && !count) {
  389. printk(KERN_CONT ".. no entries found ..");
  390. ret = -1;
  391. goto out;
  392. }
  393. /* do the test by disabling interrupts first this time */
  394. tracing_max_latency = 0;
  395. tracing_start();
  396. trace->start(tr);
  397. preempt_disable();
  398. local_irq_disable();
  399. udelay(100);
  400. preempt_enable();
  401. /* reverse the order of preempt vs irqs */
  402. local_irq_enable();
  403. trace->stop(tr);
  404. /* stop the tracing. */
  405. tracing_stop();
  406. /* check both trace buffers */
  407. ret = trace_test_buffer(tr, NULL);
  408. if (ret)
  409. goto out;
  410. ret = trace_test_buffer(&max_tr, &count);
  411. if (!ret && !count) {
  412. printk(KERN_CONT ".. no entries found ..");
  413. ret = -1;
  414. goto out;
  415. }
  416. out:
  417. tracing_start();
  418. out_no_start:
  419. trace->reset(tr);
  420. tracing_max_latency = save_max;
  421. return ret;
  422. }
  423. #endif /* CONFIG_IRQSOFF_TRACER && CONFIG_PREEMPT_TRACER */
  424. #ifdef CONFIG_NOP_TRACER
  425. int
  426. trace_selftest_startup_nop(struct tracer *trace, struct trace_array *tr)
  427. {
  428. /* What could possibly go wrong? */
  429. return 0;
  430. }
  431. #endif
  432. #ifdef CONFIG_SCHED_TRACER
  433. static int trace_wakeup_test_thread(void *data)
  434. {
  435. /* Make this a RT thread, doesn't need to be too high */
  436. struct sched_param param = { .sched_priority = 5 };
  437. struct completion *x = data;
  438. sched_setscheduler(current, SCHED_FIFO, &param);
  439. /* Make it know we have a new prio */
  440. complete(x);
  441. /* now go to sleep and let the test wake us up */
  442. set_current_state(TASK_INTERRUPTIBLE);
  443. schedule();
  444. /* we are awake, now wait to disappear */
  445. while (!kthread_should_stop()) {
  446. /*
  447. * This is an RT task, do short sleeps to let
  448. * others run.
  449. */
  450. msleep(100);
  451. }
  452. return 0;
  453. }
  454. int
  455. trace_selftest_startup_wakeup(struct tracer *trace, struct trace_array *tr)
  456. {
  457. unsigned long save_max = tracing_max_latency;
  458. struct task_struct *p;
  459. struct completion isrt;
  460. unsigned long count;
  461. int ret;
  462. init_completion(&isrt);
  463. /* create a high prio thread */
  464. p = kthread_run(trace_wakeup_test_thread, &isrt, "ftrace-test");
  465. if (IS_ERR(p)) {
  466. printk(KERN_CONT "Failed to create ftrace wakeup test thread ");
  467. return -1;
  468. }
  469. /* make sure the thread is running at an RT prio */
  470. wait_for_completion(&isrt);
  471. /* start the tracing */
  472. ret = tracer_init(trace, tr);
  473. if (ret) {
  474. warn_failed_init_tracer(trace, ret);
  475. return ret;
  476. }
  477. /* reset the max latency */
  478. tracing_max_latency = 0;
  479. /* sleep to let the RT thread sleep too */
  480. msleep(100);
  481. /*
  482. * Yes this is slightly racy. It is possible that for some
  483. * strange reason that the RT thread we created, did not
  484. * call schedule for 100ms after doing the completion,
  485. * and we do a wakeup on a task that already is awake.
  486. * But that is extremely unlikely, and the worst thing that
  487. * happens in such a case, is that we disable tracing.
  488. * Honestly, if this race does happen something is horrible
  489. * wrong with the system.
  490. */
  491. wake_up_process(p);
  492. /* give a little time to let the thread wake up */
  493. msleep(100);
  494. /* stop the tracing. */
  495. tracing_stop();
  496. /* check both trace buffers */
  497. ret = trace_test_buffer(tr, NULL);
  498. if (!ret)
  499. ret = trace_test_buffer(&max_tr, &count);
  500. trace->reset(tr);
  501. tracing_start();
  502. tracing_max_latency = save_max;
  503. /* kill the thread */
  504. kthread_stop(p);
  505. if (!ret && !count) {
  506. printk(KERN_CONT ".. no entries found ..");
  507. ret = -1;
  508. }
  509. return ret;
  510. }
  511. #endif /* CONFIG_SCHED_TRACER */
  512. #ifdef CONFIG_CONTEXT_SWITCH_TRACER
  513. int
  514. trace_selftest_startup_sched_switch(struct tracer *trace, struct trace_array *tr)
  515. {
  516. unsigned long count;
  517. int ret;
  518. /* start the tracing */
  519. ret = tracer_init(trace, tr);
  520. if (ret) {
  521. warn_failed_init_tracer(trace, ret);
  522. return ret;
  523. }
  524. /* Sleep for a 1/10 of a second */
  525. msleep(100);
  526. /* stop the tracing. */
  527. tracing_stop();
  528. /* check the trace buffer */
  529. ret = trace_test_buffer(tr, &count);
  530. trace->reset(tr);
  531. tracing_start();
  532. if (!ret && !count) {
  533. printk(KERN_CONT ".. no entries found ..");
  534. ret = -1;
  535. }
  536. return ret;
  537. }
  538. #endif /* CONFIG_CONTEXT_SWITCH_TRACER */
  539. #ifdef CONFIG_SYSPROF_TRACER
  540. int
  541. trace_selftest_startup_sysprof(struct tracer *trace, struct trace_array *tr)
  542. {
  543. unsigned long count;
  544. int ret;
  545. /* start the tracing */
  546. ret = tracer_init(trace, tr);
  547. if (ret) {
  548. warn_failed_init_tracer(trace, ret);
  549. return ret;
  550. }
  551. /* Sleep for a 1/10 of a second */
  552. msleep(100);
  553. /* stop the tracing. */
  554. tracing_stop();
  555. /* check the trace buffer */
  556. ret = trace_test_buffer(tr, &count);
  557. trace->reset(tr);
  558. tracing_start();
  559. if (!ret && !count) {
  560. printk(KERN_CONT ".. no entries found ..");
  561. ret = -1;
  562. }
  563. return ret;
  564. }
  565. #endif /* CONFIG_SYSPROF_TRACER */
  566. #ifdef CONFIG_BRANCH_TRACER
  567. int
  568. trace_selftest_startup_branch(struct tracer *trace, struct trace_array *tr)
  569. {
  570. unsigned long count;
  571. int ret;
  572. /* start the tracing */
  573. ret = tracer_init(trace, tr);
  574. if (ret) {
  575. warn_failed_init_tracer(trace, ret);
  576. return ret;
  577. }
  578. /* Sleep for a 1/10 of a second */
  579. msleep(100);
  580. /* stop the tracing. */
  581. tracing_stop();
  582. /* check the trace buffer */
  583. ret = trace_test_buffer(tr, &count);
  584. trace->reset(tr);
  585. tracing_start();
  586. if (!ret && !count) {
  587. printk(KERN_CONT ".. no entries found ..");
  588. ret = -1;
  589. }
  590. return ret;
  591. }
  592. #endif /* CONFIG_BRANCH_TRACER */