trace_selftest.c 26 KB

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