trace_sched_wakeup.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * trace task wakeup timings
  4. *
  5. * Copyright (C) 2007-2008 Steven Rostedt <srostedt@redhat.com>
  6. * Copyright (C) 2008 Ingo Molnar <mingo@redhat.com>
  7. *
  8. * Based on code from the latency_tracer, that is:
  9. *
  10. * Copyright (C) 2004-2006 Ingo Molnar
  11. * Copyright (C) 2004 Nadia Yvette Chambers
  12. */
  13. #include <linux/module.h>
  14. #include <linux/kallsyms.h>
  15. #include <linux/uaccess.h>
  16. #include <linux/ftrace.h>
  17. #include <linux/sched/rt.h>
  18. #include <linux/sched/deadline.h>
  19. #include <trace/events/sched.h>
  20. #include "trace.h"
  21. static struct trace_array *wakeup_trace;
  22. static int __read_mostly tracer_enabled;
  23. static struct task_struct *wakeup_task;
  24. static int wakeup_cpu;
  25. static int wakeup_current_cpu;
  26. static unsigned wakeup_prio = -1;
  27. static int wakeup_rt;
  28. static int wakeup_dl;
  29. static int tracing_dl = 0;
  30. static arch_spinlock_t wakeup_lock =
  31. (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED;
  32. static void wakeup_reset(struct trace_array *tr);
  33. static void __wakeup_reset(struct trace_array *tr);
  34. static int save_flags;
  35. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  36. static int wakeup_display_graph(struct trace_array *tr, int set);
  37. # define is_graph(tr) ((tr)->trace_flags & TRACE_ITER_DISPLAY_GRAPH)
  38. #else
  39. static inline int wakeup_display_graph(struct trace_array *tr, int set)
  40. {
  41. return 0;
  42. }
  43. # define is_graph(tr) false
  44. #endif
  45. #ifdef CONFIG_FUNCTION_TRACER
  46. static int wakeup_graph_entry(struct ftrace_graph_ent *trace);
  47. static void wakeup_graph_return(struct ftrace_graph_ret *trace);
  48. static bool function_enabled;
  49. /*
  50. * Prologue for the wakeup function tracers.
  51. *
  52. * Returns 1 if it is OK to continue, and preemption
  53. * is disabled and data->disabled is incremented.
  54. * 0 if the trace is to be ignored, and preemption
  55. * is not disabled and data->disabled is
  56. * kept the same.
  57. *
  58. * Note, this function is also used outside this ifdef but
  59. * inside the #ifdef of the function graph tracer below.
  60. * This is OK, since the function graph tracer is
  61. * dependent on the function tracer.
  62. */
  63. static int
  64. func_prolog_preempt_disable(struct trace_array *tr,
  65. struct trace_array_cpu **data,
  66. int *pc)
  67. {
  68. long disabled;
  69. int cpu;
  70. if (likely(!wakeup_task))
  71. return 0;
  72. *pc = preempt_count();
  73. preempt_disable_notrace();
  74. cpu = raw_smp_processor_id();
  75. if (cpu != wakeup_current_cpu)
  76. goto out_enable;
  77. *data = per_cpu_ptr(tr->trace_buffer.data, cpu);
  78. disabled = atomic_inc_return(&(*data)->disabled);
  79. if (unlikely(disabled != 1))
  80. goto out;
  81. return 1;
  82. out:
  83. atomic_dec(&(*data)->disabled);
  84. out_enable:
  85. preempt_enable_notrace();
  86. return 0;
  87. }
  88. /*
  89. * wakeup uses its own tracer function to keep the overhead down:
  90. */
  91. static void
  92. wakeup_tracer_call(unsigned long ip, unsigned long parent_ip,
  93. struct ftrace_ops *op, struct pt_regs *pt_regs)
  94. {
  95. struct trace_array *tr = wakeup_trace;
  96. struct trace_array_cpu *data;
  97. unsigned long flags;
  98. int pc;
  99. if (!func_prolog_preempt_disable(tr, &data, &pc))
  100. return;
  101. local_irq_save(flags);
  102. trace_function(tr, ip, parent_ip, flags, pc);
  103. local_irq_restore(flags);
  104. atomic_dec(&data->disabled);
  105. preempt_enable_notrace();
  106. }
  107. static int register_wakeup_function(struct trace_array *tr, int graph, int set)
  108. {
  109. int ret;
  110. /* 'set' is set if TRACE_ITER_FUNCTION is about to be set */
  111. if (function_enabled || (!set && !(tr->trace_flags & TRACE_ITER_FUNCTION)))
  112. return 0;
  113. if (graph)
  114. ret = register_ftrace_graph(&wakeup_graph_return,
  115. &wakeup_graph_entry);
  116. else
  117. ret = register_ftrace_function(tr->ops);
  118. if (!ret)
  119. function_enabled = true;
  120. return ret;
  121. }
  122. static void unregister_wakeup_function(struct trace_array *tr, int graph)
  123. {
  124. if (!function_enabled)
  125. return;
  126. if (graph)
  127. unregister_ftrace_graph();
  128. else
  129. unregister_ftrace_function(tr->ops);
  130. function_enabled = false;
  131. }
  132. static int wakeup_function_set(struct trace_array *tr, u32 mask, int set)
  133. {
  134. if (!(mask & TRACE_ITER_FUNCTION))
  135. return 0;
  136. if (set)
  137. register_wakeup_function(tr, is_graph(tr), 1);
  138. else
  139. unregister_wakeup_function(tr, is_graph(tr));
  140. return 1;
  141. }
  142. #else
  143. static int register_wakeup_function(struct trace_array *tr, int graph, int set)
  144. {
  145. return 0;
  146. }
  147. static void unregister_wakeup_function(struct trace_array *tr, int graph) { }
  148. static int wakeup_function_set(struct trace_array *tr, u32 mask, int set)
  149. {
  150. return 0;
  151. }
  152. #endif /* CONFIG_FUNCTION_TRACER */
  153. static int wakeup_flag_changed(struct trace_array *tr, u32 mask, int set)
  154. {
  155. struct tracer *tracer = tr->current_trace;
  156. if (wakeup_function_set(tr, mask, set))
  157. return 0;
  158. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  159. if (mask & TRACE_ITER_DISPLAY_GRAPH)
  160. return wakeup_display_graph(tr, set);
  161. #endif
  162. return trace_keep_overwrite(tracer, mask, set);
  163. }
  164. static int start_func_tracer(struct trace_array *tr, int graph)
  165. {
  166. int ret;
  167. ret = register_wakeup_function(tr, graph, 0);
  168. if (!ret && tracing_is_enabled())
  169. tracer_enabled = 1;
  170. else
  171. tracer_enabled = 0;
  172. return ret;
  173. }
  174. static void stop_func_tracer(struct trace_array *tr, int graph)
  175. {
  176. tracer_enabled = 0;
  177. unregister_wakeup_function(tr, graph);
  178. }
  179. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  180. static int wakeup_display_graph(struct trace_array *tr, int set)
  181. {
  182. if (!(is_graph(tr) ^ set))
  183. return 0;
  184. stop_func_tracer(tr, !set);
  185. wakeup_reset(wakeup_trace);
  186. tr->max_latency = 0;
  187. return start_func_tracer(tr, set);
  188. }
  189. static int wakeup_graph_entry(struct ftrace_graph_ent *trace)
  190. {
  191. struct trace_array *tr = wakeup_trace;
  192. struct trace_array_cpu *data;
  193. unsigned long flags;
  194. int pc, ret = 0;
  195. if (ftrace_graph_ignore_func(trace))
  196. return 0;
  197. /*
  198. * Do not trace a function if it's filtered by set_graph_notrace.
  199. * Make the index of ret stack negative to indicate that it should
  200. * ignore further functions. But it needs its own ret stack entry
  201. * to recover the original index in order to continue tracing after
  202. * returning from the function.
  203. */
  204. if (ftrace_graph_notrace_addr(trace->func))
  205. return 1;
  206. if (!func_prolog_preempt_disable(tr, &data, &pc))
  207. return 0;
  208. local_save_flags(flags);
  209. ret = __trace_graph_entry(tr, trace, flags, pc);
  210. atomic_dec(&data->disabled);
  211. preempt_enable_notrace();
  212. return ret;
  213. }
  214. static void wakeup_graph_return(struct ftrace_graph_ret *trace)
  215. {
  216. struct trace_array *tr = wakeup_trace;
  217. struct trace_array_cpu *data;
  218. unsigned long flags;
  219. int pc;
  220. ftrace_graph_addr_finish(trace);
  221. if (!func_prolog_preempt_disable(tr, &data, &pc))
  222. return;
  223. local_save_flags(flags);
  224. __trace_graph_return(tr, trace, flags, pc);
  225. atomic_dec(&data->disabled);
  226. preempt_enable_notrace();
  227. return;
  228. }
  229. static void wakeup_trace_open(struct trace_iterator *iter)
  230. {
  231. if (is_graph(iter->tr))
  232. graph_trace_open(iter);
  233. }
  234. static void wakeup_trace_close(struct trace_iterator *iter)
  235. {
  236. if (iter->private)
  237. graph_trace_close(iter);
  238. }
  239. #define GRAPH_TRACER_FLAGS (TRACE_GRAPH_PRINT_PROC | \
  240. TRACE_GRAPH_PRINT_ABS_TIME | \
  241. TRACE_GRAPH_PRINT_DURATION)
  242. static enum print_line_t wakeup_print_line(struct trace_iterator *iter)
  243. {
  244. /*
  245. * In graph mode call the graph tracer output function,
  246. * otherwise go with the TRACE_FN event handler
  247. */
  248. if (is_graph(iter->tr))
  249. return print_graph_function_flags(iter, GRAPH_TRACER_FLAGS);
  250. return TRACE_TYPE_UNHANDLED;
  251. }
  252. static void wakeup_print_header(struct seq_file *s)
  253. {
  254. if (is_graph(wakeup_trace))
  255. print_graph_headers_flags(s, GRAPH_TRACER_FLAGS);
  256. else
  257. trace_default_header(s);
  258. }
  259. static void
  260. __trace_function(struct trace_array *tr,
  261. unsigned long ip, unsigned long parent_ip,
  262. unsigned long flags, int pc)
  263. {
  264. if (is_graph(tr))
  265. trace_graph_function(tr, ip, parent_ip, flags, pc);
  266. else
  267. trace_function(tr, ip, parent_ip, flags, pc);
  268. }
  269. #else
  270. #define __trace_function trace_function
  271. static enum print_line_t wakeup_print_line(struct trace_iterator *iter)
  272. {
  273. return TRACE_TYPE_UNHANDLED;
  274. }
  275. static void wakeup_trace_open(struct trace_iterator *iter) { }
  276. static void wakeup_trace_close(struct trace_iterator *iter) { }
  277. #ifdef CONFIG_FUNCTION_TRACER
  278. static int wakeup_graph_entry(struct ftrace_graph_ent *trace)
  279. {
  280. return -1;
  281. }
  282. static void wakeup_graph_return(struct ftrace_graph_ret *trace) { }
  283. static void wakeup_print_header(struct seq_file *s)
  284. {
  285. trace_default_header(s);
  286. }
  287. #else
  288. static void wakeup_print_header(struct seq_file *s)
  289. {
  290. trace_latency_header(s);
  291. }
  292. #endif /* CONFIG_FUNCTION_TRACER */
  293. #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
  294. /*
  295. * Should this new latency be reported/recorded?
  296. */
  297. static bool report_latency(struct trace_array *tr, u64 delta)
  298. {
  299. if (tracing_thresh) {
  300. if (delta < tracing_thresh)
  301. return false;
  302. } else {
  303. if (delta <= tr->max_latency)
  304. return false;
  305. }
  306. return true;
  307. }
  308. static void
  309. probe_wakeup_migrate_task(void *ignore, struct task_struct *task, int cpu)
  310. {
  311. if (task != wakeup_task)
  312. return;
  313. wakeup_current_cpu = cpu;
  314. }
  315. static void
  316. tracing_sched_switch_trace(struct trace_array *tr,
  317. struct task_struct *prev,
  318. struct task_struct *next,
  319. unsigned long flags, int pc)
  320. {
  321. struct trace_event_call *call = &event_context_switch;
  322. struct ring_buffer *buffer = tr->trace_buffer.buffer;
  323. struct ring_buffer_event *event;
  324. struct ctx_switch_entry *entry;
  325. event = trace_buffer_lock_reserve(buffer, TRACE_CTX,
  326. sizeof(*entry), flags, pc);
  327. if (!event)
  328. return;
  329. entry = ring_buffer_event_data(event);
  330. entry->prev_pid = prev->pid;
  331. entry->prev_prio = prev->prio;
  332. entry->prev_state = task_state_index(prev);
  333. entry->next_pid = next->pid;
  334. entry->next_prio = next->prio;
  335. entry->next_state = task_state_index(next);
  336. entry->next_cpu = task_cpu(next);
  337. if (!call_filter_check_discard(call, entry, buffer, event))
  338. trace_buffer_unlock_commit(tr, buffer, event, flags, pc);
  339. }
  340. static void
  341. tracing_sched_wakeup_trace(struct trace_array *tr,
  342. struct task_struct *wakee,
  343. struct task_struct *curr,
  344. unsigned long flags, int pc)
  345. {
  346. struct trace_event_call *call = &event_wakeup;
  347. struct ring_buffer_event *event;
  348. struct ctx_switch_entry *entry;
  349. struct ring_buffer *buffer = tr->trace_buffer.buffer;
  350. event = trace_buffer_lock_reserve(buffer, TRACE_WAKE,
  351. sizeof(*entry), flags, pc);
  352. if (!event)
  353. return;
  354. entry = ring_buffer_event_data(event);
  355. entry->prev_pid = curr->pid;
  356. entry->prev_prio = curr->prio;
  357. entry->prev_state = task_state_index(curr);
  358. entry->next_pid = wakee->pid;
  359. entry->next_prio = wakee->prio;
  360. entry->next_state = task_state_index(wakee);
  361. entry->next_cpu = task_cpu(wakee);
  362. if (!call_filter_check_discard(call, entry, buffer, event))
  363. trace_buffer_unlock_commit(tr, buffer, event, flags, pc);
  364. }
  365. static void notrace
  366. probe_wakeup_sched_switch(void *ignore, bool preempt,
  367. struct task_struct *prev, struct task_struct *next)
  368. {
  369. struct trace_array_cpu *data;
  370. u64 T0, T1, delta;
  371. unsigned long flags;
  372. long disabled;
  373. int cpu;
  374. int pc;
  375. tracing_record_cmdline(prev);
  376. if (unlikely(!tracer_enabled))
  377. return;
  378. /*
  379. * When we start a new trace, we set wakeup_task to NULL
  380. * and then set tracer_enabled = 1. We want to make sure
  381. * that another CPU does not see the tracer_enabled = 1
  382. * and the wakeup_task with an older task, that might
  383. * actually be the same as next.
  384. */
  385. smp_rmb();
  386. if (next != wakeup_task)
  387. return;
  388. pc = preempt_count();
  389. /* disable local data, not wakeup_cpu data */
  390. cpu = raw_smp_processor_id();
  391. disabled = atomic_inc_return(&per_cpu_ptr(wakeup_trace->trace_buffer.data, cpu)->disabled);
  392. if (likely(disabled != 1))
  393. goto out;
  394. local_irq_save(flags);
  395. arch_spin_lock(&wakeup_lock);
  396. /* We could race with grabbing wakeup_lock */
  397. if (unlikely(!tracer_enabled || next != wakeup_task))
  398. goto out_unlock;
  399. /* The task we are waiting for is waking up */
  400. data = per_cpu_ptr(wakeup_trace->trace_buffer.data, wakeup_cpu);
  401. __trace_function(wakeup_trace, CALLER_ADDR0, CALLER_ADDR1, flags, pc);
  402. tracing_sched_switch_trace(wakeup_trace, prev, next, flags, pc);
  403. T0 = data->preempt_timestamp;
  404. T1 = ftrace_now(cpu);
  405. delta = T1-T0;
  406. if (!report_latency(wakeup_trace, delta))
  407. goto out_unlock;
  408. if (likely(!is_tracing_stopped())) {
  409. wakeup_trace->max_latency = delta;
  410. update_max_tr(wakeup_trace, wakeup_task, wakeup_cpu);
  411. }
  412. out_unlock:
  413. __wakeup_reset(wakeup_trace);
  414. arch_spin_unlock(&wakeup_lock);
  415. local_irq_restore(flags);
  416. out:
  417. atomic_dec(&per_cpu_ptr(wakeup_trace->trace_buffer.data, cpu)->disabled);
  418. }
  419. static void __wakeup_reset(struct trace_array *tr)
  420. {
  421. wakeup_cpu = -1;
  422. wakeup_prio = -1;
  423. tracing_dl = 0;
  424. if (wakeup_task)
  425. put_task_struct(wakeup_task);
  426. wakeup_task = NULL;
  427. }
  428. static void wakeup_reset(struct trace_array *tr)
  429. {
  430. unsigned long flags;
  431. tracing_reset_online_cpus(&tr->trace_buffer);
  432. local_irq_save(flags);
  433. arch_spin_lock(&wakeup_lock);
  434. __wakeup_reset(tr);
  435. arch_spin_unlock(&wakeup_lock);
  436. local_irq_restore(flags);
  437. }
  438. static void
  439. probe_wakeup(void *ignore, struct task_struct *p)
  440. {
  441. struct trace_array_cpu *data;
  442. int cpu = smp_processor_id();
  443. unsigned long flags;
  444. long disabled;
  445. int pc;
  446. if (likely(!tracer_enabled))
  447. return;
  448. tracing_record_cmdline(p);
  449. tracing_record_cmdline(current);
  450. /*
  451. * Semantic is like this:
  452. * - wakeup tracer handles all tasks in the system, independently
  453. * from their scheduling class;
  454. * - wakeup_rt tracer handles tasks belonging to sched_dl and
  455. * sched_rt class;
  456. * - wakeup_dl handles tasks belonging to sched_dl class only.
  457. */
  458. if (tracing_dl || (wakeup_dl && !dl_task(p)) ||
  459. (wakeup_rt && !dl_task(p) && !rt_task(p)) ||
  460. (!dl_task(p) && (p->prio >= wakeup_prio || p->prio >= current->prio)))
  461. return;
  462. pc = preempt_count();
  463. disabled = atomic_inc_return(&per_cpu_ptr(wakeup_trace->trace_buffer.data, cpu)->disabled);
  464. if (unlikely(disabled != 1))
  465. goto out;
  466. /* interrupts should be off from try_to_wake_up */
  467. arch_spin_lock(&wakeup_lock);
  468. /* check for races. */
  469. if (!tracer_enabled || tracing_dl ||
  470. (!dl_task(p) && p->prio >= wakeup_prio))
  471. goto out_locked;
  472. /* reset the trace */
  473. __wakeup_reset(wakeup_trace);
  474. wakeup_cpu = task_cpu(p);
  475. wakeup_current_cpu = wakeup_cpu;
  476. wakeup_prio = p->prio;
  477. /*
  478. * Once you start tracing a -deadline task, don't bother tracing
  479. * another task until the first one wakes up.
  480. */
  481. if (dl_task(p))
  482. tracing_dl = 1;
  483. else
  484. tracing_dl = 0;
  485. wakeup_task = p;
  486. get_task_struct(wakeup_task);
  487. local_save_flags(flags);
  488. data = per_cpu_ptr(wakeup_trace->trace_buffer.data, wakeup_cpu);
  489. data->preempt_timestamp = ftrace_now(cpu);
  490. tracing_sched_wakeup_trace(wakeup_trace, p, current, flags, pc);
  491. /*
  492. * We must be careful in using CALLER_ADDR2. But since wake_up
  493. * is not called by an assembly function (where as schedule is)
  494. * it should be safe to use it here.
  495. */
  496. __trace_function(wakeup_trace, CALLER_ADDR1, CALLER_ADDR2, flags, pc);
  497. out_locked:
  498. arch_spin_unlock(&wakeup_lock);
  499. out:
  500. atomic_dec(&per_cpu_ptr(wakeup_trace->trace_buffer.data, cpu)->disabled);
  501. }
  502. static void start_wakeup_tracer(struct trace_array *tr)
  503. {
  504. int ret;
  505. ret = register_trace_sched_wakeup(probe_wakeup, NULL);
  506. if (ret) {
  507. pr_info("wakeup trace: Couldn't activate tracepoint"
  508. " probe to kernel_sched_wakeup\n");
  509. return;
  510. }
  511. ret = register_trace_sched_wakeup_new(probe_wakeup, NULL);
  512. if (ret) {
  513. pr_info("wakeup trace: Couldn't activate tracepoint"
  514. " probe to kernel_sched_wakeup_new\n");
  515. goto fail_deprobe;
  516. }
  517. ret = register_trace_sched_switch(probe_wakeup_sched_switch, NULL);
  518. if (ret) {
  519. pr_info("sched trace: Couldn't activate tracepoint"
  520. " probe to kernel_sched_switch\n");
  521. goto fail_deprobe_wake_new;
  522. }
  523. ret = register_trace_sched_migrate_task(probe_wakeup_migrate_task, NULL);
  524. if (ret) {
  525. pr_info("wakeup trace: Couldn't activate tracepoint"
  526. " probe to kernel_sched_migrate_task\n");
  527. goto fail_deprobe_sched_switch;
  528. }
  529. wakeup_reset(tr);
  530. /*
  531. * Don't let the tracer_enabled = 1 show up before
  532. * the wakeup_task is reset. This may be overkill since
  533. * wakeup_reset does a spin_unlock after setting the
  534. * wakeup_task to NULL, but I want to be safe.
  535. * This is a slow path anyway.
  536. */
  537. smp_wmb();
  538. if (start_func_tracer(tr, is_graph(tr)))
  539. printk(KERN_ERR "failed to start wakeup tracer\n");
  540. return;
  541. fail_deprobe_sched_switch:
  542. unregister_trace_sched_switch(probe_wakeup_sched_switch, NULL);
  543. fail_deprobe_wake_new:
  544. unregister_trace_sched_wakeup_new(probe_wakeup, NULL);
  545. fail_deprobe:
  546. unregister_trace_sched_wakeup(probe_wakeup, NULL);
  547. }
  548. static void stop_wakeup_tracer(struct trace_array *tr)
  549. {
  550. tracer_enabled = 0;
  551. stop_func_tracer(tr, is_graph(tr));
  552. unregister_trace_sched_switch(probe_wakeup_sched_switch, NULL);
  553. unregister_trace_sched_wakeup_new(probe_wakeup, NULL);
  554. unregister_trace_sched_wakeup(probe_wakeup, NULL);
  555. unregister_trace_sched_migrate_task(probe_wakeup_migrate_task, NULL);
  556. }
  557. static bool wakeup_busy;
  558. static int __wakeup_tracer_init(struct trace_array *tr)
  559. {
  560. save_flags = tr->trace_flags;
  561. /* non overwrite screws up the latency tracers */
  562. set_tracer_flag(tr, TRACE_ITER_OVERWRITE, 1);
  563. set_tracer_flag(tr, TRACE_ITER_LATENCY_FMT, 1);
  564. tr->max_latency = 0;
  565. wakeup_trace = tr;
  566. ftrace_init_array_ops(tr, wakeup_tracer_call);
  567. start_wakeup_tracer(tr);
  568. wakeup_busy = true;
  569. return 0;
  570. }
  571. static int wakeup_tracer_init(struct trace_array *tr)
  572. {
  573. if (wakeup_busy)
  574. return -EBUSY;
  575. wakeup_dl = 0;
  576. wakeup_rt = 0;
  577. return __wakeup_tracer_init(tr);
  578. }
  579. static int wakeup_rt_tracer_init(struct trace_array *tr)
  580. {
  581. if (wakeup_busy)
  582. return -EBUSY;
  583. wakeup_dl = 0;
  584. wakeup_rt = 1;
  585. return __wakeup_tracer_init(tr);
  586. }
  587. static int wakeup_dl_tracer_init(struct trace_array *tr)
  588. {
  589. if (wakeup_busy)
  590. return -EBUSY;
  591. wakeup_dl = 1;
  592. wakeup_rt = 0;
  593. return __wakeup_tracer_init(tr);
  594. }
  595. static void wakeup_tracer_reset(struct trace_array *tr)
  596. {
  597. int lat_flag = save_flags & TRACE_ITER_LATENCY_FMT;
  598. int overwrite_flag = save_flags & TRACE_ITER_OVERWRITE;
  599. stop_wakeup_tracer(tr);
  600. /* make sure we put back any tasks we are tracing */
  601. wakeup_reset(tr);
  602. set_tracer_flag(tr, TRACE_ITER_LATENCY_FMT, lat_flag);
  603. set_tracer_flag(tr, TRACE_ITER_OVERWRITE, overwrite_flag);
  604. ftrace_reset_array_ops(tr);
  605. wakeup_busy = false;
  606. }
  607. static void wakeup_tracer_start(struct trace_array *tr)
  608. {
  609. wakeup_reset(tr);
  610. tracer_enabled = 1;
  611. }
  612. static void wakeup_tracer_stop(struct trace_array *tr)
  613. {
  614. tracer_enabled = 0;
  615. }
  616. static struct tracer wakeup_tracer __read_mostly =
  617. {
  618. .name = "wakeup",
  619. .init = wakeup_tracer_init,
  620. .reset = wakeup_tracer_reset,
  621. .start = wakeup_tracer_start,
  622. .stop = wakeup_tracer_stop,
  623. .print_max = true,
  624. .print_header = wakeup_print_header,
  625. .print_line = wakeup_print_line,
  626. .flag_changed = wakeup_flag_changed,
  627. #ifdef CONFIG_FTRACE_SELFTEST
  628. .selftest = trace_selftest_startup_wakeup,
  629. #endif
  630. .open = wakeup_trace_open,
  631. .close = wakeup_trace_close,
  632. .allow_instances = true,
  633. .use_max_tr = true,
  634. };
  635. static struct tracer wakeup_rt_tracer __read_mostly =
  636. {
  637. .name = "wakeup_rt",
  638. .init = wakeup_rt_tracer_init,
  639. .reset = wakeup_tracer_reset,
  640. .start = wakeup_tracer_start,
  641. .stop = wakeup_tracer_stop,
  642. .print_max = true,
  643. .print_header = wakeup_print_header,
  644. .print_line = wakeup_print_line,
  645. .flag_changed = wakeup_flag_changed,
  646. #ifdef CONFIG_FTRACE_SELFTEST
  647. .selftest = trace_selftest_startup_wakeup,
  648. #endif
  649. .open = wakeup_trace_open,
  650. .close = wakeup_trace_close,
  651. .allow_instances = true,
  652. .use_max_tr = true,
  653. };
  654. static struct tracer wakeup_dl_tracer __read_mostly =
  655. {
  656. .name = "wakeup_dl",
  657. .init = wakeup_dl_tracer_init,
  658. .reset = wakeup_tracer_reset,
  659. .start = wakeup_tracer_start,
  660. .stop = wakeup_tracer_stop,
  661. .print_max = true,
  662. .print_header = wakeup_print_header,
  663. .print_line = wakeup_print_line,
  664. .flag_changed = wakeup_flag_changed,
  665. #ifdef CONFIG_FTRACE_SELFTEST
  666. .selftest = trace_selftest_startup_wakeup,
  667. #endif
  668. .open = wakeup_trace_open,
  669. .close = wakeup_trace_close,
  670. .allow_instances = true,
  671. .use_max_tr = true,
  672. };
  673. __init static int init_wakeup_tracer(void)
  674. {
  675. int ret;
  676. ret = register_tracer(&wakeup_tracer);
  677. if (ret)
  678. return ret;
  679. ret = register_tracer(&wakeup_rt_tracer);
  680. if (ret)
  681. return ret;
  682. ret = register_tracer(&wakeup_dl_tracer);
  683. if (ret)
  684. return ret;
  685. return 0;
  686. }
  687. core_initcall(init_wakeup_tracer);