trace_selftest.c 26 KB

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