trace_selftest.c 26 KB

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