trace_selftest.c 26 KB

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