trace_selftest.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216
  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_free;
  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. ftrace_enabled = 1;
  320. tracing_start();
  321. /* we should only have one item */
  322. if (!ret && count != 1) {
  323. trace->reset(tr);
  324. printk(KERN_CONT ".. filter failed count=%ld ..", count);
  325. ret = -1;
  326. goto out;
  327. }
  328. /* Test the ops with global tracing running */
  329. ret = trace_selftest_ops(tr, 1);
  330. trace->reset(tr);
  331. out:
  332. ftrace_enabled = save_ftrace_enabled;
  333. /* Enable tracing on all functions again */
  334. ftrace_set_global_filter(NULL, 0, 1);
  335. /* Test the ops with global tracing off */
  336. if (!ret)
  337. ret = trace_selftest_ops(tr, 2);
  338. return ret;
  339. }
  340. static int trace_selftest_recursion_cnt;
  341. static void trace_selftest_test_recursion_func(unsigned long ip,
  342. unsigned long pip,
  343. struct ftrace_ops *op,
  344. struct pt_regs *pt_regs)
  345. {
  346. /*
  347. * This function is registered without the recursion safe flag.
  348. * The ftrace infrastructure should provide the recursion
  349. * protection. If not, this will crash the kernel!
  350. */
  351. if (trace_selftest_recursion_cnt++ > 10)
  352. return;
  353. DYN_FTRACE_TEST_NAME();
  354. }
  355. static void trace_selftest_test_recursion_safe_func(unsigned long ip,
  356. unsigned long pip,
  357. struct ftrace_ops *op,
  358. struct pt_regs *pt_regs)
  359. {
  360. /*
  361. * We said we would provide our own recursion. By calling
  362. * this function again, we should recurse back into this function
  363. * and count again. But this only happens if the arch supports
  364. * all of ftrace features and nothing else is using the function
  365. * tracing utility.
  366. */
  367. if (trace_selftest_recursion_cnt++)
  368. return;
  369. DYN_FTRACE_TEST_NAME();
  370. }
  371. static struct ftrace_ops test_rec_probe = {
  372. .func = trace_selftest_test_recursion_func,
  373. };
  374. static struct ftrace_ops test_recsafe_probe = {
  375. .func = trace_selftest_test_recursion_safe_func,
  376. .flags = FTRACE_OPS_FL_RECURSION_SAFE,
  377. };
  378. static int
  379. trace_selftest_function_recursion(void)
  380. {
  381. int save_ftrace_enabled = ftrace_enabled;
  382. char *func_name;
  383. int len;
  384. int ret;
  385. /* The previous test PASSED */
  386. pr_cont("PASSED\n");
  387. pr_info("Testing ftrace recursion: ");
  388. /* enable tracing, and record the filter function */
  389. ftrace_enabled = 1;
  390. /* Handle PPC64 '.' name */
  391. func_name = "*" __stringify(DYN_FTRACE_TEST_NAME);
  392. len = strlen(func_name);
  393. ret = ftrace_set_filter(&test_rec_probe, func_name, len, 1);
  394. if (ret) {
  395. pr_cont("*Could not set filter* ");
  396. goto out;
  397. }
  398. ret = register_ftrace_function(&test_rec_probe);
  399. if (ret) {
  400. pr_cont("*could not register callback* ");
  401. goto out;
  402. }
  403. DYN_FTRACE_TEST_NAME();
  404. unregister_ftrace_function(&test_rec_probe);
  405. ret = -1;
  406. if (trace_selftest_recursion_cnt != 1) {
  407. pr_cont("*callback not called once (%d)* ",
  408. trace_selftest_recursion_cnt);
  409. goto out;
  410. }
  411. trace_selftest_recursion_cnt = 1;
  412. pr_cont("PASSED\n");
  413. pr_info("Testing ftrace recursion safe: ");
  414. ret = ftrace_set_filter(&test_recsafe_probe, func_name, len, 1);
  415. if (ret) {
  416. pr_cont("*Could not set filter* ");
  417. goto out;
  418. }
  419. ret = register_ftrace_function(&test_recsafe_probe);
  420. if (ret) {
  421. pr_cont("*could not register callback* ");
  422. goto out;
  423. }
  424. DYN_FTRACE_TEST_NAME();
  425. unregister_ftrace_function(&test_recsafe_probe);
  426. ret = -1;
  427. if (trace_selftest_recursion_cnt != 2) {
  428. pr_cont("*callback not called expected 2 times (%d)* ",
  429. trace_selftest_recursion_cnt);
  430. goto out;
  431. }
  432. ret = 0;
  433. out:
  434. ftrace_enabled = save_ftrace_enabled;
  435. return ret;
  436. }
  437. #else
  438. # define trace_selftest_startup_dynamic_tracing(trace, tr, func) ({ 0; })
  439. # define trace_selftest_function_recursion() ({ 0; })
  440. #endif /* CONFIG_DYNAMIC_FTRACE */
  441. static enum {
  442. TRACE_SELFTEST_REGS_START,
  443. TRACE_SELFTEST_REGS_FOUND,
  444. TRACE_SELFTEST_REGS_NOT_FOUND,
  445. } trace_selftest_regs_stat;
  446. static void trace_selftest_test_regs_func(unsigned long ip,
  447. unsigned long pip,
  448. struct ftrace_ops *op,
  449. struct pt_regs *pt_regs)
  450. {
  451. if (pt_regs)
  452. trace_selftest_regs_stat = TRACE_SELFTEST_REGS_FOUND;
  453. else
  454. trace_selftest_regs_stat = TRACE_SELFTEST_REGS_NOT_FOUND;
  455. }
  456. static struct ftrace_ops test_regs_probe = {
  457. .func = trace_selftest_test_regs_func,
  458. .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_SAVE_REGS,
  459. };
  460. static int
  461. trace_selftest_function_regs(void)
  462. {
  463. int save_ftrace_enabled = ftrace_enabled;
  464. char *func_name;
  465. int len;
  466. int ret;
  467. int supported = 0;
  468. #ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS
  469. supported = 1;
  470. #endif
  471. /* The previous test PASSED */
  472. pr_cont("PASSED\n");
  473. pr_info("Testing ftrace regs%s: ",
  474. !supported ? "(no arch support)" : "");
  475. /* enable tracing, and record the filter function */
  476. ftrace_enabled = 1;
  477. /* Handle PPC64 '.' name */
  478. func_name = "*" __stringify(DYN_FTRACE_TEST_NAME);
  479. len = strlen(func_name);
  480. ret = ftrace_set_filter(&test_regs_probe, func_name, len, 1);
  481. /*
  482. * If DYNAMIC_FTRACE is not set, then we just trace all functions.
  483. * This test really doesn't care.
  484. */
  485. if (ret && ret != -ENODEV) {
  486. pr_cont("*Could not set filter* ");
  487. goto out;
  488. }
  489. ret = register_ftrace_function(&test_regs_probe);
  490. /*
  491. * Now if the arch does not support passing regs, then this should
  492. * have failed.
  493. */
  494. if (!supported) {
  495. if (!ret) {
  496. pr_cont("*registered save-regs without arch support* ");
  497. goto out;
  498. }
  499. test_regs_probe.flags |= FTRACE_OPS_FL_SAVE_REGS_IF_SUPPORTED;
  500. ret = register_ftrace_function(&test_regs_probe);
  501. }
  502. if (ret) {
  503. pr_cont("*could not register callback* ");
  504. goto out;
  505. }
  506. DYN_FTRACE_TEST_NAME();
  507. unregister_ftrace_function(&test_regs_probe);
  508. ret = -1;
  509. switch (trace_selftest_regs_stat) {
  510. case TRACE_SELFTEST_REGS_START:
  511. pr_cont("*callback never called* ");
  512. goto out;
  513. case TRACE_SELFTEST_REGS_FOUND:
  514. if (supported)
  515. break;
  516. pr_cont("*callback received regs without arch support* ");
  517. goto out;
  518. case TRACE_SELFTEST_REGS_NOT_FOUND:
  519. if (!supported)
  520. break;
  521. pr_cont("*callback received NULL regs* ");
  522. goto out;
  523. }
  524. ret = 0;
  525. out:
  526. ftrace_enabled = save_ftrace_enabled;
  527. return ret;
  528. }
  529. /*
  530. * Simple verification test of ftrace function tracer.
  531. * Enable ftrace, sleep 1/10 second, and then read the trace
  532. * buffer to see if all is in order.
  533. */
  534. __init int
  535. trace_selftest_startup_function(struct tracer *trace, struct trace_array *tr)
  536. {
  537. int save_ftrace_enabled = ftrace_enabled;
  538. unsigned long count;
  539. int ret;
  540. #ifdef CONFIG_DYNAMIC_FTRACE
  541. if (ftrace_filter_param) {
  542. printk(KERN_CONT " ... kernel command line filter set: force PASS ... ");
  543. return 0;
  544. }
  545. #endif
  546. /* make sure msleep has been recorded */
  547. msleep(1);
  548. /* start the tracing */
  549. ftrace_enabled = 1;
  550. ret = tracer_init(trace, tr);
  551. if (ret) {
  552. warn_failed_init_tracer(trace, ret);
  553. goto out;
  554. }
  555. /* Sleep for a 1/10 of a second */
  556. msleep(100);
  557. /* stop the tracing. */
  558. tracing_stop();
  559. ftrace_enabled = 0;
  560. /* check the trace buffer */
  561. ret = trace_test_buffer(&tr->trace_buffer, &count);
  562. ftrace_enabled = 1;
  563. trace->reset(tr);
  564. tracing_start();
  565. if (!ret && !count) {
  566. printk(KERN_CONT ".. no entries found ..");
  567. ret = -1;
  568. goto out;
  569. }
  570. ret = trace_selftest_startup_dynamic_tracing(trace, tr,
  571. DYN_FTRACE_TEST_NAME);
  572. if (ret)
  573. goto out;
  574. ret = trace_selftest_function_recursion();
  575. if (ret)
  576. goto out;
  577. ret = trace_selftest_function_regs();
  578. out:
  579. ftrace_enabled = save_ftrace_enabled;
  580. /* kill ftrace totally if we failed */
  581. if (ret)
  582. ftrace_kill();
  583. return ret;
  584. }
  585. #endif /* CONFIG_FUNCTION_TRACER */
  586. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  587. /* Maximum number of functions to trace before diagnosing a hang */
  588. #define GRAPH_MAX_FUNC_TEST 100000000
  589. static unsigned int graph_hang_thresh;
  590. /* Wrap the real function entry probe to avoid possible hanging */
  591. static int trace_graph_entry_watchdog(struct ftrace_graph_ent *trace)
  592. {
  593. /* This is harmlessly racy, we want to approximately detect a hang */
  594. if (unlikely(++graph_hang_thresh > GRAPH_MAX_FUNC_TEST)) {
  595. ftrace_graph_stop();
  596. printk(KERN_WARNING "BUG: Function graph tracer hang!\n");
  597. if (ftrace_dump_on_oops) {
  598. ftrace_dump(DUMP_ALL);
  599. /* ftrace_dump() disables tracing */
  600. tracing_on();
  601. }
  602. return 0;
  603. }
  604. return trace_graph_entry(trace);
  605. }
  606. /*
  607. * Pretty much the same than for the function tracer from which the selftest
  608. * has been borrowed.
  609. */
  610. __init int
  611. trace_selftest_startup_function_graph(struct tracer *trace,
  612. struct trace_array *tr)
  613. {
  614. int ret;
  615. unsigned long count;
  616. #ifdef CONFIG_DYNAMIC_FTRACE
  617. if (ftrace_filter_param) {
  618. printk(KERN_CONT " ... kernel command line filter set: force PASS ... ");
  619. return 0;
  620. }
  621. #endif
  622. /*
  623. * Simulate the init() callback but we attach a watchdog callback
  624. * to detect and recover from possible hangs
  625. */
  626. tracing_reset_online_cpus(&tr->trace_buffer);
  627. set_graph_array(tr);
  628. ret = register_ftrace_graph(&trace_graph_return,
  629. &trace_graph_entry_watchdog);
  630. if (ret) {
  631. warn_failed_init_tracer(trace, ret);
  632. goto out;
  633. }
  634. tracing_start_cmdline_record();
  635. /* Sleep for a 1/10 of a second */
  636. msleep(100);
  637. /* Have we just recovered from a hang? */
  638. if (graph_hang_thresh > GRAPH_MAX_FUNC_TEST) {
  639. tracing_selftest_disabled = true;
  640. ret = -1;
  641. goto out;
  642. }
  643. tracing_stop();
  644. /* check the trace buffer */
  645. ret = trace_test_buffer(&tr->trace_buffer, &count);
  646. trace->reset(tr);
  647. tracing_start();
  648. if (!ret && !count) {
  649. printk(KERN_CONT ".. no entries found ..");
  650. ret = -1;
  651. goto out;
  652. }
  653. /* Don't test dynamic tracing, the function tracer already did */
  654. out:
  655. /* Stop it if we failed */
  656. if (ret)
  657. ftrace_graph_stop();
  658. return ret;
  659. }
  660. #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
  661. #ifdef CONFIG_IRQSOFF_TRACER
  662. int
  663. trace_selftest_startup_irqsoff(struct tracer *trace, struct trace_array *tr)
  664. {
  665. unsigned long save_max = tr->max_latency;
  666. unsigned long count;
  667. int ret;
  668. /* start the tracing */
  669. ret = tracer_init(trace, tr);
  670. if (ret) {
  671. warn_failed_init_tracer(trace, ret);
  672. return ret;
  673. }
  674. /* reset the max latency */
  675. tr->max_latency = 0;
  676. /* disable interrupts for a bit */
  677. local_irq_disable();
  678. udelay(100);
  679. local_irq_enable();
  680. /*
  681. * Stop the tracer to avoid a warning subsequent
  682. * to buffer flipping failure because tracing_stop()
  683. * disables the tr and max buffers, making flipping impossible
  684. * in case of parallels max irqs off latencies.
  685. */
  686. trace->stop(tr);
  687. /* stop the tracing. */
  688. tracing_stop();
  689. /* check both trace buffers */
  690. ret = trace_test_buffer(&tr->trace_buffer, NULL);
  691. if (!ret)
  692. ret = trace_test_buffer(&tr->max_buffer, &count);
  693. trace->reset(tr);
  694. tracing_start();
  695. if (!ret && !count) {
  696. printk(KERN_CONT ".. no entries found ..");
  697. ret = -1;
  698. }
  699. tr->max_latency = save_max;
  700. return ret;
  701. }
  702. #endif /* CONFIG_IRQSOFF_TRACER */
  703. #ifdef CONFIG_PREEMPT_TRACER
  704. int
  705. trace_selftest_startup_preemptoff(struct tracer *trace, struct trace_array *tr)
  706. {
  707. unsigned long save_max = tr->max_latency;
  708. unsigned long count;
  709. int ret;
  710. /*
  711. * Now that the big kernel lock is no longer preemptable,
  712. * and this is called with the BKL held, it will always
  713. * fail. If preemption is already disabled, simply
  714. * pass the test. When the BKL is removed, or becomes
  715. * preemptible again, we will once again test this,
  716. * so keep it in.
  717. */
  718. if (preempt_count()) {
  719. printk(KERN_CONT "can not test ... force ");
  720. return 0;
  721. }
  722. /* start the tracing */
  723. ret = tracer_init(trace, tr);
  724. if (ret) {
  725. warn_failed_init_tracer(trace, ret);
  726. return ret;
  727. }
  728. /* reset the max latency */
  729. tr->max_latency = 0;
  730. /* disable preemption for a bit */
  731. preempt_disable();
  732. udelay(100);
  733. preempt_enable();
  734. /*
  735. * Stop the tracer to avoid a warning subsequent
  736. * to buffer flipping failure because tracing_stop()
  737. * disables the tr and max buffers, making flipping impossible
  738. * in case of parallels max preempt off latencies.
  739. */
  740. trace->stop(tr);
  741. /* stop the tracing. */
  742. tracing_stop();
  743. /* check both trace buffers */
  744. ret = trace_test_buffer(&tr->trace_buffer, NULL);
  745. if (!ret)
  746. ret = trace_test_buffer(&tr->max_buffer, &count);
  747. trace->reset(tr);
  748. tracing_start();
  749. if (!ret && !count) {
  750. printk(KERN_CONT ".. no entries found ..");
  751. ret = -1;
  752. }
  753. tr->max_latency = save_max;
  754. return ret;
  755. }
  756. #endif /* CONFIG_PREEMPT_TRACER */
  757. #if defined(CONFIG_IRQSOFF_TRACER) && defined(CONFIG_PREEMPT_TRACER)
  758. int
  759. trace_selftest_startup_preemptirqsoff(struct tracer *trace, struct trace_array *tr)
  760. {
  761. unsigned long save_max = tr->max_latency;
  762. unsigned long count;
  763. int ret;
  764. /*
  765. * Now that the big kernel lock is no longer preemptable,
  766. * and this is called with the BKL held, it will always
  767. * fail. If preemption is already disabled, simply
  768. * pass the test. When the BKL is removed, or becomes
  769. * preemptible again, we will once again test this,
  770. * so keep it in.
  771. */
  772. if (preempt_count()) {
  773. printk(KERN_CONT "can not test ... force ");
  774. return 0;
  775. }
  776. /* start the tracing */
  777. ret = tracer_init(trace, tr);
  778. if (ret) {
  779. warn_failed_init_tracer(trace, ret);
  780. goto out_no_start;
  781. }
  782. /* reset the max latency */
  783. tr->max_latency = 0;
  784. /* disable preemption and interrupts for a bit */
  785. preempt_disable();
  786. local_irq_disable();
  787. udelay(100);
  788. preempt_enable();
  789. /* reverse the order of preempt vs irqs */
  790. local_irq_enable();
  791. /*
  792. * Stop the tracer to avoid a warning subsequent
  793. * to buffer flipping failure because tracing_stop()
  794. * disables the tr and max buffers, making flipping impossible
  795. * in case of parallels max irqs/preempt off latencies.
  796. */
  797. trace->stop(tr);
  798. /* stop the tracing. */
  799. tracing_stop();
  800. /* check both trace buffers */
  801. ret = trace_test_buffer(&tr->trace_buffer, NULL);
  802. if (ret)
  803. goto out;
  804. ret = trace_test_buffer(&tr->max_buffer, &count);
  805. if (ret)
  806. goto out;
  807. if (!ret && !count) {
  808. printk(KERN_CONT ".. no entries found ..");
  809. ret = -1;
  810. goto out;
  811. }
  812. /* do the test by disabling interrupts first this time */
  813. tr->max_latency = 0;
  814. tracing_start();
  815. trace->start(tr);
  816. preempt_disable();
  817. local_irq_disable();
  818. udelay(100);
  819. preempt_enable();
  820. /* reverse the order of preempt vs irqs */
  821. local_irq_enable();
  822. trace->stop(tr);
  823. /* stop the tracing. */
  824. tracing_stop();
  825. /* check both trace buffers */
  826. ret = trace_test_buffer(&tr->trace_buffer, NULL);
  827. if (ret)
  828. goto out;
  829. ret = trace_test_buffer(&tr->max_buffer, &count);
  830. if (!ret && !count) {
  831. printk(KERN_CONT ".. no entries found ..");
  832. ret = -1;
  833. goto out;
  834. }
  835. out:
  836. tracing_start();
  837. out_no_start:
  838. trace->reset(tr);
  839. tr->max_latency = save_max;
  840. return ret;
  841. }
  842. #endif /* CONFIG_IRQSOFF_TRACER && CONFIG_PREEMPT_TRACER */
  843. #ifdef CONFIG_NOP_TRACER
  844. int
  845. trace_selftest_startup_nop(struct tracer *trace, struct trace_array *tr)
  846. {
  847. /* What could possibly go wrong? */
  848. return 0;
  849. }
  850. #endif
  851. #ifdef CONFIG_SCHED_TRACER
  852. struct wakeup_test_data {
  853. struct completion is_ready;
  854. int go;
  855. };
  856. static int trace_wakeup_test_thread(void *data)
  857. {
  858. /* Make this a -deadline thread */
  859. static const struct sched_attr attr = {
  860. .sched_policy = SCHED_DEADLINE,
  861. .sched_runtime = 100000ULL,
  862. .sched_deadline = 10000000ULL,
  863. .sched_period = 10000000ULL
  864. };
  865. struct wakeup_test_data *x = data;
  866. sched_setattr(current, &attr);
  867. /* Make it know we have a new prio */
  868. complete(&x->is_ready);
  869. /* now go to sleep and let the test wake us up */
  870. set_current_state(TASK_INTERRUPTIBLE);
  871. while (!x->go) {
  872. schedule();
  873. set_current_state(TASK_INTERRUPTIBLE);
  874. }
  875. complete(&x->is_ready);
  876. set_current_state(TASK_INTERRUPTIBLE);
  877. /* we are awake, now wait to disappear */
  878. while (!kthread_should_stop()) {
  879. schedule();
  880. set_current_state(TASK_INTERRUPTIBLE);
  881. }
  882. __set_current_state(TASK_RUNNING);
  883. return 0;
  884. }
  885. int
  886. trace_selftest_startup_wakeup(struct tracer *trace, struct trace_array *tr)
  887. {
  888. unsigned long save_max = tr->max_latency;
  889. struct task_struct *p;
  890. struct wakeup_test_data data;
  891. unsigned long count;
  892. int ret;
  893. memset(&data, 0, sizeof(data));
  894. init_completion(&data.is_ready);
  895. /* create a -deadline thread */
  896. p = kthread_run(trace_wakeup_test_thread, &data, "ftrace-test");
  897. if (IS_ERR(p)) {
  898. printk(KERN_CONT "Failed to create ftrace wakeup test thread ");
  899. return -1;
  900. }
  901. /* make sure the thread is running at -deadline policy */
  902. wait_for_completion(&data.is_ready);
  903. /* start the tracing */
  904. ret = tracer_init(trace, tr);
  905. if (ret) {
  906. warn_failed_init_tracer(trace, ret);
  907. return ret;
  908. }
  909. /* reset the max latency */
  910. tr->max_latency = 0;
  911. while (p->on_rq) {
  912. /*
  913. * Sleep to make sure the -deadline thread is asleep too.
  914. * On virtual machines we can't rely on timings,
  915. * but we want to make sure this test still works.
  916. */
  917. msleep(100);
  918. }
  919. init_completion(&data.is_ready);
  920. data.go = 1;
  921. /* memory barrier is in the wake_up_process() */
  922. wake_up_process(p);
  923. /* Wait for the task to wake up */
  924. wait_for_completion(&data.is_ready);
  925. /* stop the tracing. */
  926. tracing_stop();
  927. /* check both trace buffers */
  928. ret = trace_test_buffer(&tr->trace_buffer, NULL);
  929. if (!ret)
  930. ret = trace_test_buffer(&tr->max_buffer, &count);
  931. trace->reset(tr);
  932. tracing_start();
  933. tr->max_latency = save_max;
  934. /* kill the thread */
  935. kthread_stop(p);
  936. if (!ret && !count) {
  937. printk(KERN_CONT ".. no entries found ..");
  938. ret = -1;
  939. }
  940. return ret;
  941. }
  942. #endif /* CONFIG_SCHED_TRACER */
  943. #ifdef CONFIG_CONTEXT_SWITCH_TRACER
  944. int
  945. trace_selftest_startup_sched_switch(struct tracer *trace, struct trace_array *tr)
  946. {
  947. unsigned long count;
  948. int ret;
  949. /* start the tracing */
  950. ret = tracer_init(trace, tr);
  951. if (ret) {
  952. warn_failed_init_tracer(trace, ret);
  953. return ret;
  954. }
  955. /* Sleep for a 1/10 of a second */
  956. msleep(100);
  957. /* stop the tracing. */
  958. tracing_stop();
  959. /* check the trace buffer */
  960. ret = trace_test_buffer(&tr->trace_buffer, &count);
  961. trace->reset(tr);
  962. tracing_start();
  963. if (!ret && !count) {
  964. printk(KERN_CONT ".. no entries found ..");
  965. ret = -1;
  966. }
  967. return ret;
  968. }
  969. #endif /* CONFIG_CONTEXT_SWITCH_TRACER */
  970. #ifdef CONFIG_BRANCH_TRACER
  971. int
  972. trace_selftest_startup_branch(struct tracer *trace, struct trace_array *tr)
  973. {
  974. unsigned long count;
  975. int ret;
  976. /* start the tracing */
  977. ret = tracer_init(trace, tr);
  978. if (ret) {
  979. warn_failed_init_tracer(trace, ret);
  980. return ret;
  981. }
  982. /* Sleep for a 1/10 of a second */
  983. msleep(100);
  984. /* stop the tracing. */
  985. tracing_stop();
  986. /* check the trace buffer */
  987. ret = trace_test_buffer(&tr->trace_buffer, &count);
  988. trace->reset(tr);
  989. tracing_start();
  990. if (!ret && !count) {
  991. printk(KERN_CONT ".. no entries found ..");
  992. ret = -1;
  993. }
  994. return ret;
  995. }
  996. #endif /* CONFIG_BRANCH_TRACER */