trace_selftest.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223
  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 trace_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 trace_test_buffer(struct trace_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->trace_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->trace_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->trace_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. /*
  614. * Pretty much the same than for the function tracer from which the selftest
  615. * has been borrowed.
  616. */
  617. __init int
  618. trace_selftest_startup_function_graph(struct tracer *trace,
  619. struct trace_array *tr)
  620. {
  621. int ret;
  622. unsigned long count;
  623. #ifdef CONFIG_DYNAMIC_FTRACE
  624. if (ftrace_filter_param) {
  625. printk(KERN_CONT " ... kernel command line filter set: force PASS ... ");
  626. return 0;
  627. }
  628. #endif
  629. /*
  630. * Simulate the init() callback but we attach a watchdog callback
  631. * to detect and recover from possible hangs
  632. */
  633. tracing_reset_online_cpus(&tr->trace_buffer);
  634. set_graph_array(tr);
  635. ret = register_ftrace_graph(&trace_graph_return,
  636. &trace_graph_entry_watchdog);
  637. if (ret) {
  638. warn_failed_init_tracer(trace, ret);
  639. goto out;
  640. }
  641. tracing_start_cmdline_record();
  642. /* Sleep for a 1/10 of a second */
  643. msleep(100);
  644. /* Have we just recovered from a hang? */
  645. if (graph_hang_thresh > GRAPH_MAX_FUNC_TEST) {
  646. tracing_selftest_disabled = true;
  647. ret = -1;
  648. goto out;
  649. }
  650. tracing_stop();
  651. /* check the trace buffer */
  652. ret = trace_test_buffer(&tr->trace_buffer, &count);
  653. trace->reset(tr);
  654. tracing_start();
  655. if (!ret && !count) {
  656. printk(KERN_CONT ".. no entries found ..");
  657. ret = -1;
  658. goto out;
  659. }
  660. /* Don't test dynamic tracing, the function tracer already did */
  661. out:
  662. /* Stop it if we failed */
  663. if (ret)
  664. ftrace_graph_stop();
  665. return ret;
  666. }
  667. #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
  668. #ifdef CONFIG_IRQSOFF_TRACER
  669. int
  670. trace_selftest_startup_irqsoff(struct tracer *trace, struct trace_array *tr)
  671. {
  672. unsigned long save_max = tr->max_latency;
  673. unsigned long count;
  674. int ret;
  675. /* start the tracing */
  676. ret = tracer_init(trace, tr);
  677. if (ret) {
  678. warn_failed_init_tracer(trace, ret);
  679. return ret;
  680. }
  681. /* reset the max latency */
  682. tr->max_latency = 0;
  683. /* disable interrupts for a bit */
  684. local_irq_disable();
  685. udelay(100);
  686. local_irq_enable();
  687. /*
  688. * Stop the tracer to avoid a warning subsequent
  689. * to buffer flipping failure because tracing_stop()
  690. * disables the tr and max buffers, making flipping impossible
  691. * in case of parallels max irqs off latencies.
  692. */
  693. trace->stop(tr);
  694. /* stop the tracing. */
  695. tracing_stop();
  696. /* check both trace buffers */
  697. ret = trace_test_buffer(&tr->trace_buffer, NULL);
  698. if (!ret)
  699. ret = trace_test_buffer(&tr->max_buffer, &count);
  700. trace->reset(tr);
  701. tracing_start();
  702. if (!ret && !count) {
  703. printk(KERN_CONT ".. no entries found ..");
  704. ret = -1;
  705. }
  706. tr->max_latency = save_max;
  707. return ret;
  708. }
  709. #endif /* CONFIG_IRQSOFF_TRACER */
  710. #ifdef CONFIG_PREEMPT_TRACER
  711. int
  712. trace_selftest_startup_preemptoff(struct tracer *trace, struct trace_array *tr)
  713. {
  714. unsigned long save_max = tr->max_latency;
  715. unsigned long count;
  716. int ret;
  717. /*
  718. * Now that the big kernel lock is no longer preemptable,
  719. * and this is called with the BKL held, it will always
  720. * fail. If preemption is already disabled, simply
  721. * pass the test. When the BKL is removed, or becomes
  722. * preemptible again, we will once again test this,
  723. * so keep it in.
  724. */
  725. if (preempt_count()) {
  726. printk(KERN_CONT "can not test ... force ");
  727. return 0;
  728. }
  729. /* start the tracing */
  730. ret = tracer_init(trace, tr);
  731. if (ret) {
  732. warn_failed_init_tracer(trace, ret);
  733. return ret;
  734. }
  735. /* reset the max latency */
  736. tr->max_latency = 0;
  737. /* disable preemption for a bit */
  738. preempt_disable();
  739. udelay(100);
  740. preempt_enable();
  741. /*
  742. * Stop the tracer to avoid a warning subsequent
  743. * to buffer flipping failure because tracing_stop()
  744. * disables the tr and max buffers, making flipping impossible
  745. * in case of parallels max preempt off latencies.
  746. */
  747. trace->stop(tr);
  748. /* stop the tracing. */
  749. tracing_stop();
  750. /* check both trace buffers */
  751. ret = trace_test_buffer(&tr->trace_buffer, NULL);
  752. if (!ret)
  753. ret = trace_test_buffer(&tr->max_buffer, &count);
  754. trace->reset(tr);
  755. tracing_start();
  756. if (!ret && !count) {
  757. printk(KERN_CONT ".. no entries found ..");
  758. ret = -1;
  759. }
  760. tr->max_latency = save_max;
  761. return ret;
  762. }
  763. #endif /* CONFIG_PREEMPT_TRACER */
  764. #if defined(CONFIG_IRQSOFF_TRACER) && defined(CONFIG_PREEMPT_TRACER)
  765. int
  766. trace_selftest_startup_preemptirqsoff(struct tracer *trace, struct trace_array *tr)
  767. {
  768. unsigned long save_max = tr->max_latency;
  769. unsigned long count;
  770. int ret;
  771. /*
  772. * Now that the big kernel lock is no longer preemptable,
  773. * and this is called with the BKL held, it will always
  774. * fail. If preemption is already disabled, simply
  775. * pass the test. When the BKL is removed, or becomes
  776. * preemptible again, we will once again test this,
  777. * so keep it in.
  778. */
  779. if (preempt_count()) {
  780. printk(KERN_CONT "can not test ... force ");
  781. return 0;
  782. }
  783. /* start the tracing */
  784. ret = tracer_init(trace, tr);
  785. if (ret) {
  786. warn_failed_init_tracer(trace, ret);
  787. goto out_no_start;
  788. }
  789. /* reset the max latency */
  790. tr->max_latency = 0;
  791. /* disable preemption and interrupts for a bit */
  792. preempt_disable();
  793. local_irq_disable();
  794. udelay(100);
  795. preempt_enable();
  796. /* reverse the order of preempt vs irqs */
  797. local_irq_enable();
  798. /*
  799. * Stop the tracer to avoid a warning subsequent
  800. * to buffer flipping failure because tracing_stop()
  801. * disables the tr and max buffers, making flipping impossible
  802. * in case of parallels max irqs/preempt off latencies.
  803. */
  804. trace->stop(tr);
  805. /* stop the tracing. */
  806. tracing_stop();
  807. /* check both trace buffers */
  808. ret = trace_test_buffer(&tr->trace_buffer, NULL);
  809. if (ret)
  810. goto out;
  811. ret = trace_test_buffer(&tr->max_buffer, &count);
  812. if (ret)
  813. goto out;
  814. if (!ret && !count) {
  815. printk(KERN_CONT ".. no entries found ..");
  816. ret = -1;
  817. goto out;
  818. }
  819. /* do the test by disabling interrupts first this time */
  820. tr->max_latency = 0;
  821. tracing_start();
  822. trace->start(tr);
  823. preempt_disable();
  824. local_irq_disable();
  825. udelay(100);
  826. preempt_enable();
  827. /* reverse the order of preempt vs irqs */
  828. local_irq_enable();
  829. trace->stop(tr);
  830. /* stop the tracing. */
  831. tracing_stop();
  832. /* check both trace buffers */
  833. ret = trace_test_buffer(&tr->trace_buffer, NULL);
  834. if (ret)
  835. goto out;
  836. ret = trace_test_buffer(&tr->max_buffer, &count);
  837. if (!ret && !count) {
  838. printk(KERN_CONT ".. no entries found ..");
  839. ret = -1;
  840. goto out;
  841. }
  842. out:
  843. tracing_start();
  844. out_no_start:
  845. trace->reset(tr);
  846. tr->max_latency = save_max;
  847. return ret;
  848. }
  849. #endif /* CONFIG_IRQSOFF_TRACER && CONFIG_PREEMPT_TRACER */
  850. #ifdef CONFIG_NOP_TRACER
  851. int
  852. trace_selftest_startup_nop(struct tracer *trace, struct trace_array *tr)
  853. {
  854. /* What could possibly go wrong? */
  855. return 0;
  856. }
  857. #endif
  858. #ifdef CONFIG_SCHED_TRACER
  859. struct wakeup_test_data {
  860. struct completion is_ready;
  861. int go;
  862. };
  863. static int trace_wakeup_test_thread(void *data)
  864. {
  865. /* Make this a -deadline thread */
  866. static const struct sched_attr attr = {
  867. .sched_policy = SCHED_DEADLINE,
  868. .sched_runtime = 100000ULL,
  869. .sched_deadline = 10000000ULL,
  870. .sched_period = 10000000ULL
  871. };
  872. struct wakeup_test_data *x = data;
  873. sched_setattr(current, &attr);
  874. /* Make it know we have a new prio */
  875. complete(&x->is_ready);
  876. /* now go to sleep and let the test wake us up */
  877. set_current_state(TASK_INTERRUPTIBLE);
  878. while (!x->go) {
  879. schedule();
  880. set_current_state(TASK_INTERRUPTIBLE);
  881. }
  882. complete(&x->is_ready);
  883. set_current_state(TASK_INTERRUPTIBLE);
  884. /* we are awake, now wait to disappear */
  885. while (!kthread_should_stop()) {
  886. schedule();
  887. set_current_state(TASK_INTERRUPTIBLE);
  888. }
  889. __set_current_state(TASK_RUNNING);
  890. return 0;
  891. }
  892. int
  893. trace_selftest_startup_wakeup(struct tracer *trace, struct trace_array *tr)
  894. {
  895. unsigned long save_max = tr->max_latency;
  896. struct task_struct *p;
  897. struct wakeup_test_data data;
  898. unsigned long count;
  899. int ret;
  900. memset(&data, 0, sizeof(data));
  901. init_completion(&data.is_ready);
  902. /* create a -deadline thread */
  903. p = kthread_run(trace_wakeup_test_thread, &data, "ftrace-test");
  904. if (IS_ERR(p)) {
  905. printk(KERN_CONT "Failed to create ftrace wakeup test thread ");
  906. return -1;
  907. }
  908. /* make sure the thread is running at -deadline policy */
  909. wait_for_completion(&data.is_ready);
  910. /* start the tracing */
  911. ret = tracer_init(trace, tr);
  912. if (ret) {
  913. warn_failed_init_tracer(trace, ret);
  914. return ret;
  915. }
  916. /* reset the max latency */
  917. tr->max_latency = 0;
  918. while (p->on_rq) {
  919. /*
  920. * Sleep to make sure the -deadline thread is asleep too.
  921. * On virtual machines we can't rely on timings,
  922. * but we want to make sure this test still works.
  923. */
  924. msleep(100);
  925. }
  926. init_completion(&data.is_ready);
  927. data.go = 1;
  928. /* memory barrier is in the wake_up_process() */
  929. wake_up_process(p);
  930. /* Wait for the task to wake up */
  931. wait_for_completion(&data.is_ready);
  932. /* stop the tracing. */
  933. tracing_stop();
  934. /* check both trace buffers */
  935. ret = trace_test_buffer(&tr->trace_buffer, NULL);
  936. if (!ret)
  937. ret = trace_test_buffer(&tr->max_buffer, &count);
  938. trace->reset(tr);
  939. tracing_start();
  940. tr->max_latency = save_max;
  941. /* kill the thread */
  942. kthread_stop(p);
  943. if (!ret && !count) {
  944. printk(KERN_CONT ".. no entries found ..");
  945. ret = -1;
  946. }
  947. return ret;
  948. }
  949. #endif /* CONFIG_SCHED_TRACER */
  950. #ifdef CONFIG_CONTEXT_SWITCH_TRACER
  951. int
  952. trace_selftest_startup_sched_switch(struct tracer *trace, struct trace_array *tr)
  953. {
  954. unsigned long count;
  955. int ret;
  956. /* start the tracing */
  957. ret = tracer_init(trace, tr);
  958. if (ret) {
  959. warn_failed_init_tracer(trace, ret);
  960. return ret;
  961. }
  962. /* Sleep for a 1/10 of a second */
  963. msleep(100);
  964. /* stop the tracing. */
  965. tracing_stop();
  966. /* check the trace buffer */
  967. ret = trace_test_buffer(&tr->trace_buffer, &count);
  968. trace->reset(tr);
  969. tracing_start();
  970. if (!ret && !count) {
  971. printk(KERN_CONT ".. no entries found ..");
  972. ret = -1;
  973. }
  974. return ret;
  975. }
  976. #endif /* CONFIG_CONTEXT_SWITCH_TRACER */
  977. #ifdef CONFIG_BRANCH_TRACER
  978. int
  979. trace_selftest_startup_branch(struct tracer *trace, struct trace_array *tr)
  980. {
  981. unsigned long count;
  982. int ret;
  983. /* start the tracing */
  984. ret = tracer_init(trace, tr);
  985. if (ret) {
  986. warn_failed_init_tracer(trace, ret);
  987. return ret;
  988. }
  989. /* Sleep for a 1/10 of a second */
  990. msleep(100);
  991. /* stop the tracing. */
  992. tracing_stop();
  993. /* check the trace buffer */
  994. ret = trace_test_buffer(&tr->trace_buffer, &count);
  995. trace->reset(tr);
  996. tracing_start();
  997. if (!ret && !count) {
  998. printk(KERN_CONT ".. no entries found ..");
  999. ret = -1;
  1000. }
  1001. return ret;
  1002. }
  1003. #endif /* CONFIG_BRANCH_TRACER */