trace_selftest.c 27 KB

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