trace_selftest.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198
  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_buffer *buf, 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(buf->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_buffer *buf, 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(&buf->tr->max_lock);
  61. cnt = ring_buffer_entries(buf->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(buf, cpu);
  72. if (ret)
  73. break;
  74. }
  75. tracing_on();
  76. arch_spin_unlock(&buf->tr->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. .flags = FTRACE_OPS_FL_RECURSION_SAFE,
  132. };
  133. static struct ftrace_ops test_probe2 = {
  134. .func = trace_selftest_test_probe2_func,
  135. .flags = FTRACE_OPS_FL_RECURSION_SAFE,
  136. };
  137. static struct ftrace_ops test_probe3 = {
  138. .func = trace_selftest_test_probe3_func,
  139. .flags = FTRACE_OPS_FL_RECURSION_SAFE,
  140. };
  141. static void print_counts(void)
  142. {
  143. printk("(%d %d %d %d %d) ",
  144. trace_selftest_test_probe1_cnt,
  145. trace_selftest_test_probe2_cnt,
  146. trace_selftest_test_probe3_cnt,
  147. trace_selftest_test_global_cnt,
  148. trace_selftest_test_dyn_cnt);
  149. }
  150. static void reset_counts(void)
  151. {
  152. trace_selftest_test_probe1_cnt = 0;
  153. trace_selftest_test_probe2_cnt = 0;
  154. trace_selftest_test_probe3_cnt = 0;
  155. trace_selftest_test_global_cnt = 0;
  156. trace_selftest_test_dyn_cnt = 0;
  157. }
  158. static int trace_selftest_ops(struct trace_array *tr, int cnt)
  159. {
  160. int save_ftrace_enabled = ftrace_enabled;
  161. struct ftrace_ops *dyn_ops;
  162. char *func1_name;
  163. char *func2_name;
  164. int len1;
  165. int len2;
  166. int ret = -1;
  167. printk(KERN_CONT "PASSED\n");
  168. pr_info("Testing dynamic ftrace ops #%d: ", cnt);
  169. ftrace_enabled = 1;
  170. reset_counts();
  171. /* Handle PPC64 '.' name */
  172. func1_name = "*" __stringify(DYN_FTRACE_TEST_NAME);
  173. func2_name = "*" __stringify(DYN_FTRACE_TEST_NAME2);
  174. len1 = strlen(func1_name);
  175. len2 = strlen(func2_name);
  176. /*
  177. * Probe 1 will trace function 1.
  178. * Probe 2 will trace function 2.
  179. * Probe 3 will trace functions 1 and 2.
  180. */
  181. ftrace_set_filter(&test_probe1, func1_name, len1, 1);
  182. ftrace_set_filter(&test_probe2, func2_name, len2, 1);
  183. ftrace_set_filter(&test_probe3, func1_name, len1, 1);
  184. ftrace_set_filter(&test_probe3, func2_name, len2, 0);
  185. register_ftrace_function(&test_probe1);
  186. register_ftrace_function(&test_probe2);
  187. register_ftrace_function(&test_probe3);
  188. /* First time we are running with main function */
  189. if (cnt > 1) {
  190. ftrace_init_array_ops(tr, trace_selftest_test_global_func);
  191. register_ftrace_function(tr->ops);
  192. }
  193. DYN_FTRACE_TEST_NAME();
  194. print_counts();
  195. if (trace_selftest_test_probe1_cnt != 1)
  196. goto out;
  197. if (trace_selftest_test_probe2_cnt != 0)
  198. goto out;
  199. if (trace_selftest_test_probe3_cnt != 1)
  200. goto out;
  201. if (cnt > 1) {
  202. if (trace_selftest_test_global_cnt == 0)
  203. goto out;
  204. }
  205. DYN_FTRACE_TEST_NAME2();
  206. print_counts();
  207. if (trace_selftest_test_probe1_cnt != 1)
  208. goto out;
  209. if (trace_selftest_test_probe2_cnt != 1)
  210. goto out;
  211. if (trace_selftest_test_probe3_cnt != 2)
  212. goto out;
  213. /* Add a dynamic probe */
  214. dyn_ops = kzalloc(sizeof(*dyn_ops), GFP_KERNEL);
  215. if (!dyn_ops) {
  216. printk("MEMORY ERROR ");
  217. goto out;
  218. }
  219. dyn_ops->func = trace_selftest_test_dyn_func;
  220. register_ftrace_function(dyn_ops);
  221. trace_selftest_test_global_cnt = 0;
  222. DYN_FTRACE_TEST_NAME();
  223. print_counts();
  224. if (trace_selftest_test_probe1_cnt != 2)
  225. goto out_free;
  226. if (trace_selftest_test_probe2_cnt != 1)
  227. goto out_free;
  228. if (trace_selftest_test_probe3_cnt != 3)
  229. goto out_free;
  230. if (cnt > 1) {
  231. if (trace_selftest_test_global_cnt == 0)
  232. goto out;
  233. }
  234. if (trace_selftest_test_dyn_cnt == 0)
  235. goto out_free;
  236. DYN_FTRACE_TEST_NAME2();
  237. print_counts();
  238. if (trace_selftest_test_probe1_cnt != 2)
  239. goto out_free;
  240. if (trace_selftest_test_probe2_cnt != 2)
  241. goto out_free;
  242. if (trace_selftest_test_probe3_cnt != 4)
  243. goto out_free;
  244. ret = 0;
  245. out_free:
  246. unregister_ftrace_function(dyn_ops);
  247. kfree(dyn_ops);
  248. out:
  249. /* Purposely unregister in the same order */
  250. unregister_ftrace_function(&test_probe1);
  251. unregister_ftrace_function(&test_probe2);
  252. unregister_ftrace_function(&test_probe3);
  253. if (cnt > 1)
  254. unregister_ftrace_function(tr->ops);
  255. ftrace_reset_array_ops(tr);
  256. /* Make sure everything is off */
  257. reset_counts();
  258. DYN_FTRACE_TEST_NAME();
  259. DYN_FTRACE_TEST_NAME();
  260. if (trace_selftest_test_probe1_cnt ||
  261. trace_selftest_test_probe2_cnt ||
  262. trace_selftest_test_probe3_cnt ||
  263. trace_selftest_test_global_cnt ||
  264. trace_selftest_test_dyn_cnt)
  265. ret = -1;
  266. ftrace_enabled = save_ftrace_enabled;
  267. return ret;
  268. }
  269. /* Test dynamic code modification and ftrace filters */
  270. static int trace_selftest_startup_dynamic_tracing(struct tracer *trace,
  271. struct trace_array *tr,
  272. int (*func)(void))
  273. {
  274. int save_ftrace_enabled = ftrace_enabled;
  275. unsigned long count;
  276. char *func_name;
  277. int ret;
  278. /* The ftrace test PASSED */
  279. printk(KERN_CONT "PASSED\n");
  280. pr_info("Testing dynamic ftrace: ");
  281. /* enable tracing, and record the filter function */
  282. ftrace_enabled = 1;
  283. /* passed in by parameter to fool gcc from optimizing */
  284. func();
  285. /*
  286. * Some archs *cough*PowerPC*cough* add characters to the
  287. * start of the function names. We simply put a '*' to
  288. * accommodate them.
  289. */
  290. func_name = "*" __stringify(DYN_FTRACE_TEST_NAME);
  291. /* filter only on our function */
  292. ftrace_set_global_filter(func_name, strlen(func_name), 1);
  293. /* enable tracing */
  294. ret = tracer_init(trace, tr);
  295. if (ret) {
  296. warn_failed_init_tracer(trace, ret);
  297. goto out;
  298. }
  299. /* Sleep for a 1/10 of a second */
  300. msleep(100);
  301. /* we should have nothing in the buffer */
  302. ret = trace_test_buffer(&tr->trace_buffer, &count);
  303. if (ret)
  304. goto out;
  305. if (count) {
  306. ret = -1;
  307. printk(KERN_CONT ".. filter did not filter .. ");
  308. goto out;
  309. }
  310. /* call our function again */
  311. func();
  312. /* sleep again */
  313. msleep(100);
  314. /* stop the tracing. */
  315. tracing_stop();
  316. ftrace_enabled = 0;
  317. /* check the trace buffer */
  318. ret = trace_test_buffer(&tr->trace_buffer, &count);
  319. tracing_start();
  320. /* we should only have one item */
  321. if (!ret && count != 1) {
  322. trace->reset(tr);
  323. printk(KERN_CONT ".. filter failed count=%ld ..", count);
  324. ret = -1;
  325. goto out;
  326. }
  327. /* Test the ops with global tracing running */
  328. ret = trace_selftest_ops(tr, 1);
  329. trace->reset(tr);
  330. out:
  331. ftrace_enabled = save_ftrace_enabled;
  332. /* Enable tracing on all functions again */
  333. ftrace_set_global_filter(NULL, 0, 1);
  334. /* Test the ops with global tracing off */
  335. if (!ret)
  336. ret = trace_selftest_ops(tr, 2);
  337. return ret;
  338. }
  339. static int trace_selftest_recursion_cnt;
  340. static void trace_selftest_test_recursion_func(unsigned long ip,
  341. unsigned long pip,
  342. struct ftrace_ops *op,
  343. struct pt_regs *pt_regs)
  344. {
  345. /*
  346. * This function is registered without the recursion safe flag.
  347. * The ftrace infrastructure should provide the recursion
  348. * protection. If not, this will crash the kernel!
  349. */
  350. if (trace_selftest_recursion_cnt++ > 10)
  351. return;
  352. DYN_FTRACE_TEST_NAME();
  353. }
  354. static void trace_selftest_test_recursion_safe_func(unsigned long ip,
  355. unsigned long pip,
  356. struct ftrace_ops *op,
  357. struct pt_regs *pt_regs)
  358. {
  359. /*
  360. * We said we would provide our own recursion. By calling
  361. * this function again, we should recurse back into this function
  362. * and count again. But this only happens if the arch supports
  363. * all of ftrace features and nothing else is using the function
  364. * tracing utility.
  365. */
  366. if (trace_selftest_recursion_cnt++)
  367. return;
  368. DYN_FTRACE_TEST_NAME();
  369. }
  370. static struct ftrace_ops test_rec_probe = {
  371. .func = trace_selftest_test_recursion_func,
  372. };
  373. static struct ftrace_ops test_recsafe_probe = {
  374. .func = trace_selftest_test_recursion_safe_func,
  375. .flags = FTRACE_OPS_FL_RECURSION_SAFE,
  376. };
  377. static int
  378. trace_selftest_function_recursion(void)
  379. {
  380. int save_ftrace_enabled = ftrace_enabled;
  381. char *func_name;
  382. int len;
  383. int ret;
  384. /* The previous test PASSED */
  385. pr_cont("PASSED\n");
  386. pr_info("Testing ftrace recursion: ");
  387. /* enable tracing, and record the filter function */
  388. ftrace_enabled = 1;
  389. /* Handle PPC64 '.' name */
  390. func_name = "*" __stringify(DYN_FTRACE_TEST_NAME);
  391. len = strlen(func_name);
  392. ret = ftrace_set_filter(&test_rec_probe, func_name, len, 1);
  393. if (ret) {
  394. pr_cont("*Could not set filter* ");
  395. goto out;
  396. }
  397. ret = register_ftrace_function(&test_rec_probe);
  398. if (ret) {
  399. pr_cont("*could not register callback* ");
  400. goto out;
  401. }
  402. DYN_FTRACE_TEST_NAME();
  403. unregister_ftrace_function(&test_rec_probe);
  404. ret = -1;
  405. if (trace_selftest_recursion_cnt != 1) {
  406. pr_cont("*callback not called once (%d)* ",
  407. trace_selftest_recursion_cnt);
  408. goto out;
  409. }
  410. trace_selftest_recursion_cnt = 1;
  411. pr_cont("PASSED\n");
  412. pr_info("Testing ftrace recursion safe: ");
  413. ret = ftrace_set_filter(&test_recsafe_probe, func_name, len, 1);
  414. if (ret) {
  415. pr_cont("*Could not set filter* ");
  416. goto out;
  417. }
  418. ret = register_ftrace_function(&test_recsafe_probe);
  419. if (ret) {
  420. pr_cont("*could not register callback* ");
  421. goto out;
  422. }
  423. DYN_FTRACE_TEST_NAME();
  424. unregister_ftrace_function(&test_recsafe_probe);
  425. ret = -1;
  426. if (trace_selftest_recursion_cnt != 2) {
  427. pr_cont("*callback not called expected 2 times (%d)* ",
  428. trace_selftest_recursion_cnt);
  429. goto out;
  430. }
  431. ret = 0;
  432. out:
  433. ftrace_enabled = save_ftrace_enabled;
  434. return ret;
  435. }
  436. #else
  437. # define trace_selftest_startup_dynamic_tracing(trace, tr, func) ({ 0; })
  438. # define trace_selftest_function_recursion() ({ 0; })
  439. #endif /* CONFIG_DYNAMIC_FTRACE */
  440. static enum {
  441. TRACE_SELFTEST_REGS_START,
  442. TRACE_SELFTEST_REGS_FOUND,
  443. TRACE_SELFTEST_REGS_NOT_FOUND,
  444. } trace_selftest_regs_stat;
  445. static void trace_selftest_test_regs_func(unsigned long ip,
  446. unsigned long pip,
  447. struct ftrace_ops *op,
  448. struct pt_regs *pt_regs)
  449. {
  450. if (pt_regs)
  451. trace_selftest_regs_stat = TRACE_SELFTEST_REGS_FOUND;
  452. else
  453. trace_selftest_regs_stat = TRACE_SELFTEST_REGS_NOT_FOUND;
  454. }
  455. static struct ftrace_ops test_regs_probe = {
  456. .func = trace_selftest_test_regs_func,
  457. .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_SAVE_REGS,
  458. };
  459. static int
  460. trace_selftest_function_regs(void)
  461. {
  462. int save_ftrace_enabled = ftrace_enabled;
  463. char *func_name;
  464. int len;
  465. int ret;
  466. int supported = 0;
  467. #ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS
  468. supported = 1;
  469. #endif
  470. /* The previous test PASSED */
  471. pr_cont("PASSED\n");
  472. pr_info("Testing ftrace regs%s: ",
  473. !supported ? "(no arch support)" : "");
  474. /* enable tracing, and record the filter function */
  475. ftrace_enabled = 1;
  476. /* Handle PPC64 '.' name */
  477. func_name = "*" __stringify(DYN_FTRACE_TEST_NAME);
  478. len = strlen(func_name);
  479. ret = ftrace_set_filter(&test_regs_probe, func_name, len, 1);
  480. /*
  481. * If DYNAMIC_FTRACE is not set, then we just trace all functions.
  482. * This test really doesn't care.
  483. */
  484. if (ret && ret != -ENODEV) {
  485. pr_cont("*Could not set filter* ");
  486. goto out;
  487. }
  488. ret = register_ftrace_function(&test_regs_probe);
  489. /*
  490. * Now if the arch does not support passing regs, then this should
  491. * have failed.
  492. */
  493. if (!supported) {
  494. if (!ret) {
  495. pr_cont("*registered save-regs without arch support* ");
  496. goto out;
  497. }
  498. test_regs_probe.flags |= FTRACE_OPS_FL_SAVE_REGS_IF_SUPPORTED;
  499. ret = register_ftrace_function(&test_regs_probe);
  500. }
  501. if (ret) {
  502. pr_cont("*could not register callback* ");
  503. goto out;
  504. }
  505. DYN_FTRACE_TEST_NAME();
  506. unregister_ftrace_function(&test_regs_probe);
  507. ret = -1;
  508. switch (trace_selftest_regs_stat) {
  509. case TRACE_SELFTEST_REGS_START:
  510. pr_cont("*callback never called* ");
  511. goto out;
  512. case TRACE_SELFTEST_REGS_FOUND:
  513. if (supported)
  514. break;
  515. pr_cont("*callback received regs without arch support* ");
  516. goto out;
  517. case TRACE_SELFTEST_REGS_NOT_FOUND:
  518. if (!supported)
  519. break;
  520. pr_cont("*callback received NULL regs* ");
  521. goto out;
  522. }
  523. ret = 0;
  524. out:
  525. ftrace_enabled = save_ftrace_enabled;
  526. return ret;
  527. }
  528. /*
  529. * Simple verification test of ftrace function tracer.
  530. * Enable ftrace, sleep 1/10 second, and then read the trace
  531. * buffer to see if all is in order.
  532. */
  533. __init int
  534. trace_selftest_startup_function(struct tracer *trace, struct trace_array *tr)
  535. {
  536. int save_ftrace_enabled = ftrace_enabled;
  537. unsigned long count;
  538. int ret;
  539. #ifdef CONFIG_DYNAMIC_FTRACE
  540. if (ftrace_filter_param) {
  541. printk(KERN_CONT " ... kernel command line filter set: force PASS ... ");
  542. return 0;
  543. }
  544. #endif
  545. /* make sure msleep has been recorded */
  546. msleep(1);
  547. /* start the tracing */
  548. ftrace_enabled = 1;
  549. ret = tracer_init(trace, tr);
  550. if (ret) {
  551. warn_failed_init_tracer(trace, ret);
  552. goto out;
  553. }
  554. /* Sleep for a 1/10 of a second */
  555. msleep(100);
  556. /* stop the tracing. */
  557. tracing_stop();
  558. ftrace_enabled = 0;
  559. /* check the trace buffer */
  560. ret = trace_test_buffer(&tr->trace_buffer, &count);
  561. trace->reset(tr);
  562. tracing_start();
  563. if (!ret && !count) {
  564. printk(KERN_CONT ".. no entries found ..");
  565. ret = -1;
  566. goto out;
  567. }
  568. ret = trace_selftest_startup_dynamic_tracing(trace, tr,
  569. DYN_FTRACE_TEST_NAME);
  570. if (ret)
  571. goto out;
  572. ret = trace_selftest_function_recursion();
  573. if (ret)
  574. goto out;
  575. ret = trace_selftest_function_regs();
  576. out:
  577. ftrace_enabled = save_ftrace_enabled;
  578. /* kill ftrace totally if we failed */
  579. if (ret)
  580. ftrace_kill();
  581. return ret;
  582. }
  583. #endif /* CONFIG_FUNCTION_TRACER */
  584. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  585. /* Maximum number of functions to trace before diagnosing a hang */
  586. #define GRAPH_MAX_FUNC_TEST 100000000
  587. static unsigned int graph_hang_thresh;
  588. /* Wrap the real function entry probe to avoid possible hanging */
  589. static int trace_graph_entry_watchdog(struct ftrace_graph_ent *trace)
  590. {
  591. /* This is harmlessly racy, we want to approximately detect a hang */
  592. if (unlikely(++graph_hang_thresh > GRAPH_MAX_FUNC_TEST)) {
  593. ftrace_graph_stop();
  594. printk(KERN_WARNING "BUG: Function graph tracer hang!\n");
  595. if (ftrace_dump_on_oops) {
  596. ftrace_dump(DUMP_ALL);
  597. /* ftrace_dump() disables tracing */
  598. tracing_on();
  599. }
  600. return 0;
  601. }
  602. return trace_graph_entry(trace);
  603. }
  604. /*
  605. * Pretty much the same than for the function tracer from which the selftest
  606. * has been borrowed.
  607. */
  608. __init int
  609. trace_selftest_startup_function_graph(struct tracer *trace,
  610. struct trace_array *tr)
  611. {
  612. int ret;
  613. unsigned long count;
  614. #ifdef CONFIG_DYNAMIC_FTRACE
  615. if (ftrace_filter_param) {
  616. printk(KERN_CONT " ... kernel command line filter set: force PASS ... ");
  617. return 0;
  618. }
  619. #endif
  620. /*
  621. * Simulate the init() callback but we attach a watchdog callback
  622. * to detect and recover from possible hangs
  623. */
  624. tracing_reset_online_cpus(&tr->trace_buffer);
  625. set_graph_array(tr);
  626. ret = register_ftrace_graph(&trace_graph_return,
  627. &trace_graph_entry_watchdog);
  628. if (ret) {
  629. warn_failed_init_tracer(trace, ret);
  630. goto out;
  631. }
  632. tracing_start_cmdline_record();
  633. /* Sleep for a 1/10 of a second */
  634. msleep(100);
  635. /* Have we just recovered from a hang? */
  636. if (graph_hang_thresh > GRAPH_MAX_FUNC_TEST) {
  637. tracing_selftest_disabled = true;
  638. ret = -1;
  639. goto out;
  640. }
  641. tracing_stop();
  642. /* check the trace buffer */
  643. ret = trace_test_buffer(&tr->trace_buffer, &count);
  644. trace->reset(tr);
  645. tracing_start();
  646. if (!ret && !count) {
  647. printk(KERN_CONT ".. no entries found ..");
  648. ret = -1;
  649. goto out;
  650. }
  651. /* Don't test dynamic tracing, the function tracer already did */
  652. out:
  653. /* Stop it if we failed */
  654. if (ret)
  655. ftrace_graph_stop();
  656. return ret;
  657. }
  658. #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
  659. #ifdef CONFIG_IRQSOFF_TRACER
  660. int
  661. trace_selftest_startup_irqsoff(struct tracer *trace, struct trace_array *tr)
  662. {
  663. unsigned long save_max = tr->max_latency;
  664. unsigned long count;
  665. int ret;
  666. /* start the tracing */
  667. ret = tracer_init(trace, tr);
  668. if (ret) {
  669. warn_failed_init_tracer(trace, ret);
  670. return ret;
  671. }
  672. /* reset the max latency */
  673. tr->max_latency = 0;
  674. /* disable interrupts for a bit */
  675. local_irq_disable();
  676. udelay(100);
  677. local_irq_enable();
  678. /*
  679. * Stop the tracer to avoid a warning subsequent
  680. * to buffer flipping failure because tracing_stop()
  681. * disables the tr and max buffers, making flipping impossible
  682. * in case of parallels max irqs off latencies.
  683. */
  684. trace->stop(tr);
  685. /* stop the tracing. */
  686. tracing_stop();
  687. /* check both trace buffers */
  688. ret = trace_test_buffer(&tr->trace_buffer, NULL);
  689. if (!ret)
  690. ret = trace_test_buffer(&tr->max_buffer, &count);
  691. trace->reset(tr);
  692. tracing_start();
  693. if (!ret && !count) {
  694. printk(KERN_CONT ".. no entries found ..");
  695. ret = -1;
  696. }
  697. tr->max_latency = save_max;
  698. return ret;
  699. }
  700. #endif /* CONFIG_IRQSOFF_TRACER */
  701. #ifdef CONFIG_PREEMPT_TRACER
  702. int
  703. trace_selftest_startup_preemptoff(struct tracer *trace, struct trace_array *tr)
  704. {
  705. unsigned long save_max = tr->max_latency;
  706. unsigned long count;
  707. int ret;
  708. /*
  709. * Now that the big kernel lock is no longer preemptable,
  710. * and this is called with the BKL held, it will always
  711. * fail. If preemption is already disabled, simply
  712. * pass the test. When the BKL is removed, or becomes
  713. * preemptible again, we will once again test this,
  714. * so keep it in.
  715. */
  716. if (preempt_count()) {
  717. printk(KERN_CONT "can not test ... force ");
  718. return 0;
  719. }
  720. /* start the tracing */
  721. ret = tracer_init(trace, tr);
  722. if (ret) {
  723. warn_failed_init_tracer(trace, ret);
  724. return ret;
  725. }
  726. /* reset the max latency */
  727. tr->max_latency = 0;
  728. /* disable preemption for a bit */
  729. preempt_disable();
  730. udelay(100);
  731. preempt_enable();
  732. /*
  733. * Stop the tracer to avoid a warning subsequent
  734. * to buffer flipping failure because tracing_stop()
  735. * disables the tr and max buffers, making flipping impossible
  736. * in case of parallels max preempt off latencies.
  737. */
  738. trace->stop(tr);
  739. /* stop the tracing. */
  740. tracing_stop();
  741. /* check both trace buffers */
  742. ret = trace_test_buffer(&tr->trace_buffer, NULL);
  743. if (!ret)
  744. ret = trace_test_buffer(&tr->max_buffer, &count);
  745. trace->reset(tr);
  746. tracing_start();
  747. if (!ret && !count) {
  748. printk(KERN_CONT ".. no entries found ..");
  749. ret = -1;
  750. }
  751. tr->max_latency = save_max;
  752. return ret;
  753. }
  754. #endif /* CONFIG_PREEMPT_TRACER */
  755. #if defined(CONFIG_IRQSOFF_TRACER) && defined(CONFIG_PREEMPT_TRACER)
  756. int
  757. trace_selftest_startup_preemptirqsoff(struct tracer *trace, struct trace_array *tr)
  758. {
  759. unsigned long save_max = tr->max_latency;
  760. unsigned long count;
  761. int ret;
  762. /*
  763. * Now that the big kernel lock is no longer preemptable,
  764. * and this is called with the BKL held, it will always
  765. * fail. If preemption is already disabled, simply
  766. * pass the test. When the BKL is removed, or becomes
  767. * preemptible again, we will once again test this,
  768. * so keep it in.
  769. */
  770. if (preempt_count()) {
  771. printk(KERN_CONT "can not test ... force ");
  772. return 0;
  773. }
  774. /* start the tracing */
  775. ret = tracer_init(trace, tr);
  776. if (ret) {
  777. warn_failed_init_tracer(trace, ret);
  778. goto out_no_start;
  779. }
  780. /* reset the max latency */
  781. tr->max_latency = 0;
  782. /* disable preemption and interrupts for a bit */
  783. preempt_disable();
  784. local_irq_disable();
  785. udelay(100);
  786. preempt_enable();
  787. /* reverse the order of preempt vs irqs */
  788. local_irq_enable();
  789. /*
  790. * Stop the tracer to avoid a warning subsequent
  791. * to buffer flipping failure because tracing_stop()
  792. * disables the tr and max buffers, making flipping impossible
  793. * in case of parallels max irqs/preempt off latencies.
  794. */
  795. trace->stop(tr);
  796. /* stop the tracing. */
  797. tracing_stop();
  798. /* check both trace buffers */
  799. ret = trace_test_buffer(&tr->trace_buffer, NULL);
  800. if (ret)
  801. goto out;
  802. ret = trace_test_buffer(&tr->max_buffer, &count);
  803. if (ret)
  804. goto out;
  805. if (!ret && !count) {
  806. printk(KERN_CONT ".. no entries found ..");
  807. ret = -1;
  808. goto out;
  809. }
  810. /* do the test by disabling interrupts first this time */
  811. tr->max_latency = 0;
  812. tracing_start();
  813. trace->start(tr);
  814. preempt_disable();
  815. local_irq_disable();
  816. udelay(100);
  817. preempt_enable();
  818. /* reverse the order of preempt vs irqs */
  819. local_irq_enable();
  820. trace->stop(tr);
  821. /* stop the tracing. */
  822. tracing_stop();
  823. /* check both trace buffers */
  824. ret = trace_test_buffer(&tr->trace_buffer, NULL);
  825. if (ret)
  826. goto out;
  827. ret = trace_test_buffer(&tr->max_buffer, &count);
  828. if (!ret && !count) {
  829. printk(KERN_CONT ".. no entries found ..");
  830. ret = -1;
  831. goto out;
  832. }
  833. out:
  834. tracing_start();
  835. out_no_start:
  836. trace->reset(tr);
  837. tr->max_latency = save_max;
  838. return ret;
  839. }
  840. #endif /* CONFIG_IRQSOFF_TRACER && CONFIG_PREEMPT_TRACER */
  841. #ifdef CONFIG_NOP_TRACER
  842. int
  843. trace_selftest_startup_nop(struct tracer *trace, struct trace_array *tr)
  844. {
  845. /* What could possibly go wrong? */
  846. return 0;
  847. }
  848. #endif
  849. #ifdef CONFIG_SCHED_TRACER
  850. static int trace_wakeup_test_thread(void *data)
  851. {
  852. /* Make this a -deadline thread */
  853. static const struct sched_attr attr = {
  854. .sched_policy = SCHED_DEADLINE,
  855. .sched_runtime = 100000ULL,
  856. .sched_deadline = 10000000ULL,
  857. .sched_period = 10000000ULL
  858. };
  859. struct completion *x = data;
  860. sched_setattr(current, &attr);
  861. /* Make it know we have a new prio */
  862. complete(x);
  863. /* now go to sleep and let the test wake us up */
  864. set_current_state(TASK_INTERRUPTIBLE);
  865. schedule();
  866. complete(x);
  867. /* we are awake, now wait to disappear */
  868. while (!kthread_should_stop()) {
  869. /*
  870. * This will likely be the system top priority
  871. * task, do short sleeps to let others run.
  872. */
  873. msleep(100);
  874. }
  875. return 0;
  876. }
  877. int
  878. trace_selftest_startup_wakeup(struct tracer *trace, struct trace_array *tr)
  879. {
  880. unsigned long save_max = tr->max_latency;
  881. struct task_struct *p;
  882. struct completion is_ready;
  883. unsigned long count;
  884. int ret;
  885. init_completion(&is_ready);
  886. /* create a -deadline thread */
  887. p = kthread_run(trace_wakeup_test_thread, &is_ready, "ftrace-test");
  888. if (IS_ERR(p)) {
  889. printk(KERN_CONT "Failed to create ftrace wakeup test thread ");
  890. return -1;
  891. }
  892. /* make sure the thread is running at -deadline policy */
  893. wait_for_completion(&is_ready);
  894. /* start the tracing */
  895. ret = tracer_init(trace, tr);
  896. if (ret) {
  897. warn_failed_init_tracer(trace, ret);
  898. return ret;
  899. }
  900. /* reset the max latency */
  901. tr->max_latency = 0;
  902. while (p->on_rq) {
  903. /*
  904. * Sleep to make sure the -deadline thread is asleep too.
  905. * On virtual machines we can't rely on timings,
  906. * but we want to make sure this test still works.
  907. */
  908. msleep(100);
  909. }
  910. init_completion(&is_ready);
  911. wake_up_process(p);
  912. /* Wait for the task to wake up */
  913. wait_for_completion(&is_ready);
  914. /* stop the tracing. */
  915. tracing_stop();
  916. /* check both trace buffers */
  917. ret = trace_test_buffer(&tr->trace_buffer, NULL);
  918. printk("ret = %d\n", ret);
  919. if (!ret)
  920. ret = trace_test_buffer(&tr->max_buffer, &count);
  921. trace->reset(tr);
  922. tracing_start();
  923. tr->max_latency = save_max;
  924. /* kill the thread */
  925. kthread_stop(p);
  926. if (!ret && !count) {
  927. printk(KERN_CONT ".. no entries found ..");
  928. ret = -1;
  929. }
  930. return ret;
  931. }
  932. #endif /* CONFIG_SCHED_TRACER */
  933. #ifdef CONFIG_CONTEXT_SWITCH_TRACER
  934. int
  935. trace_selftest_startup_sched_switch(struct tracer *trace, struct trace_array *tr)
  936. {
  937. unsigned long count;
  938. int ret;
  939. /* start the tracing */
  940. ret = tracer_init(trace, tr);
  941. if (ret) {
  942. warn_failed_init_tracer(trace, ret);
  943. return ret;
  944. }
  945. /* Sleep for a 1/10 of a second */
  946. msleep(100);
  947. /* stop the tracing. */
  948. tracing_stop();
  949. /* check the trace buffer */
  950. ret = trace_test_buffer(&tr->trace_buffer, &count);
  951. trace->reset(tr);
  952. tracing_start();
  953. if (!ret && !count) {
  954. printk(KERN_CONT ".. no entries found ..");
  955. ret = -1;
  956. }
  957. return ret;
  958. }
  959. #endif /* CONFIG_CONTEXT_SWITCH_TRACER */
  960. #ifdef CONFIG_BRANCH_TRACER
  961. int
  962. trace_selftest_startup_branch(struct tracer *trace, struct trace_array *tr)
  963. {
  964. unsigned long count;
  965. int ret;
  966. /* start the tracing */
  967. ret = tracer_init(trace, tr);
  968. if (ret) {
  969. warn_failed_init_tracer(trace, ret);
  970. return ret;
  971. }
  972. /* Sleep for a 1/10 of a second */
  973. msleep(100);
  974. /* stop the tracing. */
  975. tracing_stop();
  976. /* check the trace buffer */
  977. ret = trace_test_buffer(&tr->trace_buffer, &count);
  978. trace->reset(tr);
  979. tracing_start();
  980. if (!ret && !count) {
  981. printk(KERN_CONT ".. no entries found ..");
  982. ret = -1;
  983. }
  984. return ret;
  985. }
  986. #endif /* CONFIG_BRANCH_TRACER */