trace_selftest.c 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202
  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;
  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. static int trace_wakeup_test_thread(void *data)
  853. {
  854. /* Make this a -deadline thread */
  855. static const struct sched_attr attr = {
  856. .sched_policy = SCHED_DEADLINE,
  857. .sched_runtime = 100000ULL,
  858. .sched_deadline = 10000000ULL,
  859. .sched_period = 10000000ULL
  860. };
  861. struct completion *x = data;
  862. sched_setattr(current, &attr);
  863. /* Make it know we have a new prio */
  864. complete(x);
  865. /* now go to sleep and let the test wake us up */
  866. set_current_state(TASK_INTERRUPTIBLE);
  867. schedule();
  868. complete(x);
  869. /* we are awake, now wait to disappear */
  870. while (!kthread_should_stop()) {
  871. /*
  872. * This will likely be the system top priority
  873. * task, do short sleeps to let others run.
  874. */
  875. msleep(100);
  876. }
  877. return 0;
  878. }
  879. int
  880. trace_selftest_startup_wakeup(struct tracer *trace, struct trace_array *tr)
  881. {
  882. unsigned long save_max = tr->max_latency;
  883. struct task_struct *p;
  884. struct completion is_ready;
  885. unsigned long count;
  886. int ret;
  887. init_completion(&is_ready);
  888. /* create a -deadline thread */
  889. p = kthread_run(trace_wakeup_test_thread, &is_ready, "ftrace-test");
  890. if (IS_ERR(p)) {
  891. printk(KERN_CONT "Failed to create ftrace wakeup test thread ");
  892. return -1;
  893. }
  894. /* make sure the thread is running at -deadline policy */
  895. wait_for_completion(&is_ready);
  896. /* start the tracing */
  897. ret = tracer_init(trace, tr);
  898. if (ret) {
  899. warn_failed_init_tracer(trace, ret);
  900. return ret;
  901. }
  902. /* reset the max latency */
  903. tr->max_latency = 0;
  904. while (p->on_rq) {
  905. /*
  906. * Sleep to make sure the -deadline thread is asleep too.
  907. * On virtual machines we can't rely on timings,
  908. * but we want to make sure this test still works.
  909. */
  910. msleep(100);
  911. }
  912. init_completion(&is_ready);
  913. wake_up_process(p);
  914. /* Wait for the task to wake up */
  915. wait_for_completion(&is_ready);
  916. /* stop the tracing. */
  917. tracing_stop();
  918. /* check both trace buffers */
  919. ret = trace_test_buffer(&tr->trace_buffer, NULL);
  920. printk("ret = %d\n", ret);
  921. if (!ret)
  922. ret = trace_test_buffer(&tr->max_buffer, &count);
  923. trace->reset(tr);
  924. tracing_start();
  925. tr->max_latency = save_max;
  926. /* kill the thread */
  927. kthread_stop(p);
  928. if (!ret && !count) {
  929. printk(KERN_CONT ".. no entries found ..");
  930. ret = -1;
  931. }
  932. return ret;
  933. }
  934. #endif /* CONFIG_SCHED_TRACER */
  935. #ifdef CONFIG_CONTEXT_SWITCH_TRACER
  936. int
  937. trace_selftest_startup_sched_switch(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->trace_buffer, &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_CONTEXT_SWITCH_TRACER */
  962. #ifdef CONFIG_BRANCH_TRACER
  963. int
  964. trace_selftest_startup_branch(struct tracer *trace, struct trace_array *tr)
  965. {
  966. unsigned long count;
  967. int ret;
  968. /* start the tracing */
  969. ret = tracer_init(trace, tr);
  970. if (ret) {
  971. warn_failed_init_tracer(trace, ret);
  972. return ret;
  973. }
  974. /* Sleep for a 1/10 of a second */
  975. msleep(100);
  976. /* stop the tracing. */
  977. tracing_stop();
  978. /* check the trace buffer */
  979. ret = trace_test_buffer(&tr->trace_buffer, &count);
  980. trace->reset(tr);
  981. tracing_start();
  982. if (!ret && !count) {
  983. printk(KERN_CONT ".. no entries found ..");
  984. ret = -1;
  985. }
  986. return ret;
  987. }
  988. #endif /* CONFIG_BRANCH_TRACER */