trace_selftest.c 27 KB

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