1
2
3
4
5
6
7
8
9
10#include <linux/suspend.h>
11#include <linux/ftrace.h>
12#include <linux/slab.h>
13
14#include <trace/events/sched.h>
15
16#include "ftrace_internal.h"
17
18#ifdef CONFIG_DYNAMIC_FTRACE
19#define ASSIGN_OPS_HASH(opsname, val) \
20 .func_hash = val, \
21 .local_hash.regex_lock = __MUTEX_INITIALIZER(opsname.local_hash.regex_lock),
22#else
23#define ASSIGN_OPS_HASH(opsname, val)
24#endif
25
26static bool kill_ftrace_graph;
27int ftrace_graph_active;
28
29
30static bool fgraph_sleep_time = true;
31
32
33
34
35
36
37
38
39bool ftrace_graph_is_dead(void)
40{
41 return kill_ftrace_graph;
42}
43
44
45
46
47
48
49
50
51
52void ftrace_graph_stop(void)
53{
54 kill_ftrace_graph = true;
55}
56
57
58static int
59ftrace_push_return_trace(unsigned long ret, unsigned long func,
60 unsigned long frame_pointer, unsigned long *retp)
61{
62 unsigned long long calltime;
63 int index;
64
65 if (unlikely(ftrace_graph_is_dead()))
66 return -EBUSY;
67
68 if (!current->ret_stack)
69 return -EBUSY;
70
71
72
73
74
75 smp_rmb();
76
77
78 if (current->curr_ret_stack == FTRACE_RETFUNC_DEPTH - 1) {
79 atomic_inc(¤t->trace_overrun);
80 return -EBUSY;
81 }
82
83 calltime = trace_clock_local();
84
85 index = ++current->curr_ret_stack;
86 barrier();
87 current->ret_stack[index].ret = ret;
88 current->ret_stack[index].func = func;
89 current->ret_stack[index].calltime = calltime;
90#ifdef HAVE_FUNCTION_GRAPH_FP_TEST
91 current->ret_stack[index].fp = frame_pointer;
92#endif
93#ifdef HAVE_FUNCTION_GRAPH_RET_ADDR_PTR
94 current->ret_stack[index].retp = retp;
95#endif
96 return 0;
97}
98
99int function_graph_enter(unsigned long ret, unsigned long func,
100 unsigned long frame_pointer, unsigned long *retp)
101{
102 struct ftrace_graph_ent trace;
103
104 trace.func = func;
105 trace.depth = ++current->curr_ret_depth;
106
107 if (ftrace_push_return_trace(ret, func, frame_pointer, retp))
108 goto out;
109
110
111 if (!ftrace_graph_entry(&trace))
112 goto out_ret;
113
114 return 0;
115 out_ret:
116 current->curr_ret_stack--;
117 out:
118 current->curr_ret_depth--;
119 return -EBUSY;
120}
121
122
123static void
124ftrace_pop_return_trace(struct ftrace_graph_ret *trace, unsigned long *ret,
125 unsigned long frame_pointer)
126{
127 int index;
128
129 index = current->curr_ret_stack;
130
131 if (unlikely(index < 0 || index >= FTRACE_RETFUNC_DEPTH)) {
132 ftrace_graph_stop();
133 WARN_ON(1);
134
135 *ret = (unsigned long)panic;
136 return;
137 }
138
139#ifdef HAVE_FUNCTION_GRAPH_FP_TEST
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154 if (unlikely(current->ret_stack[index].fp != frame_pointer)) {
155 ftrace_graph_stop();
156 WARN(1, "Bad frame pointer: expected %lx, received %lx\n"
157 " from func %ps return to %lx\n",
158 current->ret_stack[index].fp,
159 frame_pointer,
160 (void *)current->ret_stack[index].func,
161 current->ret_stack[index].ret);
162 *ret = (unsigned long)panic;
163 return;
164 }
165#endif
166
167 *ret = current->ret_stack[index].ret;
168 trace->func = current->ret_stack[index].func;
169 trace->calltime = current->ret_stack[index].calltime;
170 trace->overrun = atomic_read(¤t->trace_overrun);
171 trace->depth = current->curr_ret_depth--;
172
173
174
175
176
177 barrier();
178}
179
180
181
182
183
184
185static int
186ftrace_suspend_notifier_call(struct notifier_block *bl, unsigned long state,
187 void *unused)
188{
189 switch (state) {
190 case PM_HIBERNATION_PREPARE:
191 pause_graph_tracing();
192 break;
193
194 case PM_POST_HIBERNATION:
195 unpause_graph_tracing();
196 break;
197 }
198 return NOTIFY_DONE;
199}
200
201static struct notifier_block ftrace_suspend_notifier = {
202 .notifier_call = ftrace_suspend_notifier_call,
203};
204
205
206
207
208
209unsigned long ftrace_return_to_handler(unsigned long frame_pointer)
210{
211 struct ftrace_graph_ret trace;
212 unsigned long ret;
213
214 ftrace_pop_return_trace(&trace, &ret, frame_pointer);
215 trace.rettime = trace_clock_local();
216 ftrace_graph_return(&trace);
217
218
219
220
221
222 barrier();
223 current->curr_ret_stack--;
224
225 if (unlikely(!ret)) {
226 ftrace_graph_stop();
227 WARN_ON(1);
228
229 ret = (unsigned long)panic;
230 }
231
232 return ret;
233}
234
235
236
237
238
239
240
241
242
243
244
245
246struct ftrace_ret_stack *
247ftrace_graph_get_ret_stack(struct task_struct *task, int idx)
248{
249 idx = task->curr_ret_stack - idx;
250
251 if (idx >= 0 && idx <= task->curr_ret_stack)
252 return &task->ret_stack[idx];
253
254 return NULL;
255}
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272#ifdef HAVE_FUNCTION_GRAPH_RET_ADDR_PTR
273unsigned long ftrace_graph_ret_addr(struct task_struct *task, int *idx,
274 unsigned long ret, unsigned long *retp)
275{
276 int index = task->curr_ret_stack;
277 int i;
278
279 if (ret != (unsigned long)return_to_handler)
280 return ret;
281
282 if (index < 0)
283 return ret;
284
285 for (i = 0; i <= index; i++)
286 if (task->ret_stack[i].retp == retp)
287 return task->ret_stack[i].ret;
288
289 return ret;
290}
291#else
292unsigned long ftrace_graph_ret_addr(struct task_struct *task, int *idx,
293 unsigned long ret, unsigned long *retp)
294{
295 int task_idx;
296
297 if (ret != (unsigned long)return_to_handler)
298 return ret;
299
300 task_idx = task->curr_ret_stack;
301
302 if (!task->ret_stack || task_idx < *idx)
303 return ret;
304
305 task_idx -= *idx;
306 (*idx)++;
307
308 return task->ret_stack[task_idx].ret;
309}
310#endif
311
312static struct ftrace_ops graph_ops = {
313 .func = ftrace_stub,
314 .flags = FTRACE_OPS_FL_RECURSION_SAFE |
315 FTRACE_OPS_FL_INITIALIZED |
316 FTRACE_OPS_FL_PID |
317 FTRACE_OPS_FL_STUB,
318#ifdef FTRACE_GRAPH_TRAMP_ADDR
319 .trampoline = FTRACE_GRAPH_TRAMP_ADDR,
320
321#endif
322 ASSIGN_OPS_HASH(graph_ops, &global_ops.local_hash)
323};
324
325void ftrace_graph_sleep_time_control(bool enable)
326{
327 fgraph_sleep_time = enable;
328}
329
330int ftrace_graph_entry_stub(struct ftrace_graph_ent *trace)
331{
332 return 0;
333}
334
335
336trace_func_graph_ret_t ftrace_graph_return =
337 (trace_func_graph_ret_t)ftrace_stub;
338trace_func_graph_ent_t ftrace_graph_entry = ftrace_graph_entry_stub;
339static trace_func_graph_ent_t __ftrace_graph_entry = ftrace_graph_entry_stub;
340
341
342static int alloc_retstack_tasklist(struct ftrace_ret_stack **ret_stack_list)
343{
344 int i;
345 int ret = 0;
346 int start = 0, end = FTRACE_RETSTACK_ALLOC_SIZE;
347 struct task_struct *g, *t;
348
349 for (i = 0; i < FTRACE_RETSTACK_ALLOC_SIZE; i++) {
350 ret_stack_list[i] =
351 kmalloc_array(FTRACE_RETFUNC_DEPTH,
352 sizeof(struct ftrace_ret_stack),
353 GFP_KERNEL);
354 if (!ret_stack_list[i]) {
355 start = 0;
356 end = i;
357 ret = -ENOMEM;
358 goto free;
359 }
360 }
361
362 read_lock(&tasklist_lock);
363 do_each_thread(g, t) {
364 if (start == end) {
365 ret = -EAGAIN;
366 goto unlock;
367 }
368
369 if (t->ret_stack == NULL) {
370 atomic_set(&t->tracing_graph_pause, 0);
371 atomic_set(&t->trace_overrun, 0);
372 t->curr_ret_stack = -1;
373 t->curr_ret_depth = -1;
374
375 smp_wmb();
376 t->ret_stack = ret_stack_list[start++];
377 }
378 } while_each_thread(g, t);
379
380unlock:
381 read_unlock(&tasklist_lock);
382free:
383 for (i = start; i < end; i++)
384 kfree(ret_stack_list[i]);
385 return ret;
386}
387
388static void
389ftrace_graph_probe_sched_switch(void *ignore, bool preempt,
390 struct task_struct *prev, struct task_struct *next)
391{
392 unsigned long long timestamp;
393 int index;
394
395
396
397
398
399 if (fgraph_sleep_time)
400 return;
401
402 timestamp = trace_clock_local();
403
404 prev->ftrace_timestamp = timestamp;
405
406
407 if (!next->ftrace_timestamp)
408 return;
409
410
411
412
413
414 timestamp -= next->ftrace_timestamp;
415
416 for (index = next->curr_ret_stack; index >= 0; index--)
417 next->ret_stack[index].calltime += timestamp;
418}
419
420static int ftrace_graph_entry_test(struct ftrace_graph_ent *trace)
421{
422 if (!ftrace_ops_test(&global_ops, trace->func, NULL))
423 return 0;
424 return __ftrace_graph_entry(trace);
425}
426
427
428
429
430
431
432
433
434void update_function_graph_func(void)
435{
436 struct ftrace_ops *op;
437 bool do_test = false;
438
439
440
441
442
443
444
445 do_for_each_ftrace_op(op, ftrace_ops_list) {
446 if (op != &global_ops && op != &graph_ops &&
447 op != &ftrace_list_end) {
448 do_test = true;
449
450 goto out;
451 }
452 } while_for_each_ftrace_op(op);
453 out:
454 if (do_test)
455 ftrace_graph_entry = ftrace_graph_entry_test;
456 else
457 ftrace_graph_entry = __ftrace_graph_entry;
458}
459
460static DEFINE_PER_CPU(struct ftrace_ret_stack *, idle_ret_stack);
461
462static void
463graph_init_task(struct task_struct *t, struct ftrace_ret_stack *ret_stack)
464{
465 atomic_set(&t->tracing_graph_pause, 0);
466 atomic_set(&t->trace_overrun, 0);
467 t->ftrace_timestamp = 0;
468
469 smp_wmb();
470 t->ret_stack = ret_stack;
471}
472
473
474
475
476
477void ftrace_graph_init_idle_task(struct task_struct *t, int cpu)
478{
479 t->curr_ret_stack = -1;
480 t->curr_ret_depth = -1;
481
482
483
484
485 if (t->ret_stack)
486 WARN_ON(t->ret_stack != per_cpu(idle_ret_stack, cpu));
487
488 if (ftrace_graph_active) {
489 struct ftrace_ret_stack *ret_stack;
490
491 ret_stack = per_cpu(idle_ret_stack, cpu);
492 if (!ret_stack) {
493 ret_stack =
494 kmalloc_array(FTRACE_RETFUNC_DEPTH,
495 sizeof(struct ftrace_ret_stack),
496 GFP_KERNEL);
497 if (!ret_stack)
498 return;
499 per_cpu(idle_ret_stack, cpu) = ret_stack;
500 }
501 graph_init_task(t, ret_stack);
502 }
503}
504
505
506void ftrace_graph_init_task(struct task_struct *t)
507{
508
509 t->ret_stack = NULL;
510 t->curr_ret_stack = -1;
511 t->curr_ret_depth = -1;
512
513 if (ftrace_graph_active) {
514 struct ftrace_ret_stack *ret_stack;
515
516 ret_stack = kmalloc_array(FTRACE_RETFUNC_DEPTH,
517 sizeof(struct ftrace_ret_stack),
518 GFP_KERNEL);
519 if (!ret_stack)
520 return;
521 graph_init_task(t, ret_stack);
522 }
523}
524
525void ftrace_graph_exit_task(struct task_struct *t)
526{
527 struct ftrace_ret_stack *ret_stack = t->ret_stack;
528
529 t->ret_stack = NULL;
530
531 barrier();
532
533 kfree(ret_stack);
534}
535
536
537static int start_graph_tracing(void)
538{
539 struct ftrace_ret_stack **ret_stack_list;
540 int ret, cpu;
541
542 ret_stack_list = kmalloc_array(FTRACE_RETSTACK_ALLOC_SIZE,
543 sizeof(struct ftrace_ret_stack *),
544 GFP_KERNEL);
545
546 if (!ret_stack_list)
547 return -ENOMEM;
548
549
550 for_each_online_cpu(cpu) {
551 if (!idle_task(cpu)->ret_stack)
552 ftrace_graph_init_idle_task(idle_task(cpu), cpu);
553 }
554
555 do {
556 ret = alloc_retstack_tasklist(ret_stack_list);
557 } while (ret == -EAGAIN);
558
559 if (!ret) {
560 ret = register_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
561 if (ret)
562 pr_info("ftrace_graph: Couldn't activate tracepoint"
563 " probe to kernel_sched_switch\n");
564 }
565
566 kfree(ret_stack_list);
567 return ret;
568}
569
570int register_ftrace_graph(struct fgraph_ops *gops)
571{
572 int ret = 0;
573
574 mutex_lock(&ftrace_lock);
575
576
577 if (ftrace_graph_active) {
578 ret = -EBUSY;
579 goto out;
580 }
581
582 register_pm_notifier(&ftrace_suspend_notifier);
583
584 ftrace_graph_active++;
585 ret = start_graph_tracing();
586 if (ret) {
587 ftrace_graph_active--;
588 goto out;
589 }
590
591 ftrace_graph_return = gops->retfunc;
592
593
594
595
596
597
598
599 __ftrace_graph_entry = gops->entryfunc;
600 ftrace_graph_entry = ftrace_graph_entry_test;
601 update_function_graph_func();
602
603 ret = ftrace_startup(&graph_ops, FTRACE_START_FUNC_RET);
604out:
605 mutex_unlock(&ftrace_lock);
606 return ret;
607}
608
609void unregister_ftrace_graph(struct fgraph_ops *gops)
610{
611 mutex_lock(&ftrace_lock);
612
613 if (unlikely(!ftrace_graph_active))
614 goto out;
615
616 ftrace_graph_active--;
617 ftrace_graph_return = (trace_func_graph_ret_t)ftrace_stub;
618 ftrace_graph_entry = ftrace_graph_entry_stub;
619 __ftrace_graph_entry = ftrace_graph_entry_stub;
620 ftrace_shutdown(&graph_ops, FTRACE_STOP_FUNC_RET);
621 unregister_pm_notifier(&ftrace_suspend_notifier);
622 unregister_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
623
624 out:
625 mutex_unlock(&ftrace_lock);
626}
627