1 /*
2 * Infrastructure for profiling code inserted by 'gcc -pg'.
3 *
4 * Copyright (C) 2007-2008 Steven Rostedt <srostedt@redhat.com>
5 * Copyright (C) 2004-2008 Ingo Molnar <mingo@redhat.com>
6 *
7 * Originally ported from the -rt patch by:
8 * Copyright (C) 2007 Arnaldo Carvalho de Melo <acme@redhat.com>
9 *
10 * Based on code in the latency_tracer, that is:
11 *
12 * Copyright (C) 2004-2006 Ingo Molnar
13 * Copyright (C) 2004 Nadia Yvette Chambers
14 */
15
16 #include <linux/stop_machine.h>
17 #include <linux/clocksource.h>
18 #include <linux/kallsyms.h>
19 #include <linux/seq_file.h>
20 #include <linux/suspend.h>
21 #include <linux/tracefs.h>
22 #include <linux/hardirq.h>
23 #include <linux/kthread.h>
24 #include <linux/uaccess.h>
25 #include <linux/bsearch.h>
26 #include <linux/module.h>
27 #include <linux/ftrace.h>
28 #include <linux/sysctl.h>
29 #include <linux/slab.h>
30 #include <linux/ctype.h>
31 #include <linux/sort.h>
32 #include <linux/list.h>
33 #include <linux/hash.h>
34 #include <linux/rcupdate.h>
35 #include <linux/kprobes.h>
36
37 #include <trace/events/sched.h>
38
39 #include <asm/setup.h>
40
41 #include "trace_output.h"
42 #include "trace_stat.h"
43
44 #define FTRACE_WARN_ON(cond) \
45 ({ \
46 int ___r = cond; \
47 if (WARN_ON(___r)) \
48 ftrace_kill(); \
49 ___r; \
50 })
51
52 #define FTRACE_WARN_ON_ONCE(cond) \
53 ({ \
54 int ___r = cond; \
55 if (WARN_ON_ONCE(___r)) \
56 ftrace_kill(); \
57 ___r; \
58 })
59
60 /* hash bits for specific function selection */
61 #define FTRACE_HASH_BITS 7
62 #define FTRACE_FUNC_HASHSIZE (1 << FTRACE_HASH_BITS)
63 #define FTRACE_HASH_DEFAULT_BITS 10
64 #define FTRACE_HASH_MAX_BITS 12
65
66 #define FL_GLOBAL_CONTROL_MASK (FTRACE_OPS_FL_CONTROL)
67
68 #ifdef CONFIG_DYNAMIC_FTRACE
69 #define INIT_OPS_HASH(opsname) \
70 .func_hash = &opsname.local_hash, \
71 .local_hash.regex_lock = __MUTEX_INITIALIZER(opsname.local_hash.regex_lock),
72 #define ASSIGN_OPS_HASH(opsname, val) \
73 .func_hash = val, \
74 .local_hash.regex_lock = __MUTEX_INITIALIZER(opsname.local_hash.regex_lock),
75 #else
76 #define INIT_OPS_HASH(opsname)
77 #define ASSIGN_OPS_HASH(opsname, val)
78 #endif
79
80 static struct ftrace_ops ftrace_list_end __read_mostly = {
81 .func = ftrace_stub,
82 .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_STUB,
83 INIT_OPS_HASH(ftrace_list_end)
84 };
85
86 /* ftrace_enabled is a method to turn ftrace on or off */
87 int ftrace_enabled __read_mostly;
88 static int last_ftrace_enabled;
89
90 /* Current function tracing op */
91 struct ftrace_ops *function_trace_op __read_mostly = &ftrace_list_end;
92 /* What to set function_trace_op to */
93 static struct ftrace_ops *set_function_trace_op;
94
95 /* List for set_ftrace_pid's pids. */
96 LIST_HEAD(ftrace_pids);
97 struct ftrace_pid {
98 struct list_head list;
99 struct pid *pid;
100 };
101
ftrace_pids_enabled(void)102 static bool ftrace_pids_enabled(void)
103 {
104 return !list_empty(&ftrace_pids);
105 }
106
107 static void ftrace_update_trampoline(struct ftrace_ops *ops);
108
109 /*
110 * ftrace_disabled is set when an anomaly is discovered.
111 * ftrace_disabled is much stronger than ftrace_enabled.
112 */
113 static int ftrace_disabled __read_mostly;
114
115 static DEFINE_MUTEX(ftrace_lock);
116
117 static struct ftrace_ops *ftrace_control_list __read_mostly = &ftrace_list_end;
118 static struct ftrace_ops *ftrace_ops_list __read_mostly = &ftrace_list_end;
119 ftrace_func_t ftrace_trace_function __read_mostly = ftrace_stub;
120 static struct ftrace_ops global_ops;
121 static struct ftrace_ops control_ops;
122
123 static void ftrace_ops_recurs_func(unsigned long ip, unsigned long parent_ip,
124 struct ftrace_ops *op, struct pt_regs *regs);
125
126 #if ARCH_SUPPORTS_FTRACE_OPS
127 static void ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
128 struct ftrace_ops *op, struct pt_regs *regs);
129 #else
130 /* See comment below, where ftrace_ops_list_func is defined */
131 static void ftrace_ops_no_ops(unsigned long ip, unsigned long parent_ip);
132 #define ftrace_ops_list_func ((ftrace_func_t)ftrace_ops_no_ops)
133 #endif
134
135 /*
136 * Traverse the ftrace_global_list, invoking all entries. The reason that we
137 * can use rcu_dereference_raw_notrace() is that elements removed from this list
138 * are simply leaked, so there is no need to interact with a grace-period
139 * mechanism. The rcu_dereference_raw_notrace() calls are needed to handle
140 * concurrent insertions into the ftrace_global_list.
141 *
142 * Silly Alpha and silly pointer-speculation compiler optimizations!
143 */
144 #define do_for_each_ftrace_op(op, list) \
145 op = rcu_dereference_raw_notrace(list); \
146 do
147
148 /*
149 * Optimized for just a single item in the list (as that is the normal case).
150 */
151 #define while_for_each_ftrace_op(op) \
152 while (likely(op = rcu_dereference_raw_notrace((op)->next)) && \
153 unlikely((op) != &ftrace_list_end))
154
ftrace_ops_init(struct ftrace_ops * ops)155 static inline void ftrace_ops_init(struct ftrace_ops *ops)
156 {
157 #ifdef CONFIG_DYNAMIC_FTRACE
158 if (!(ops->flags & FTRACE_OPS_FL_INITIALIZED)) {
159 mutex_init(&ops->local_hash.regex_lock);
160 ops->func_hash = &ops->local_hash;
161 ops->flags |= FTRACE_OPS_FL_INITIALIZED;
162 }
163 #endif
164 }
165
166 /**
167 * ftrace_nr_registered_ops - return number of ops registered
168 *
169 * Returns the number of ftrace_ops registered and tracing functions
170 */
ftrace_nr_registered_ops(void)171 int ftrace_nr_registered_ops(void)
172 {
173 struct ftrace_ops *ops;
174 int cnt = 0;
175
176 mutex_lock(&ftrace_lock);
177
178 for (ops = ftrace_ops_list;
179 ops != &ftrace_list_end; ops = ops->next)
180 cnt++;
181
182 mutex_unlock(&ftrace_lock);
183
184 return cnt;
185 }
186
ftrace_pid_func(unsigned long ip,unsigned long parent_ip,struct ftrace_ops * op,struct pt_regs * regs)187 static void ftrace_pid_func(unsigned long ip, unsigned long parent_ip,
188 struct ftrace_ops *op, struct pt_regs *regs)
189 {
190 if (!test_tsk_trace_trace(current))
191 return;
192
193 op->saved_func(ip, parent_ip, op, regs);
194 }
195
196 /**
197 * clear_ftrace_function - reset the ftrace function
198 *
199 * This NULLs the ftrace function and in essence stops
200 * tracing. There may be lag
201 */
clear_ftrace_function(void)202 void clear_ftrace_function(void)
203 {
204 ftrace_trace_function = ftrace_stub;
205 }
206
control_ops_disable_all(struct ftrace_ops * ops)207 static void control_ops_disable_all(struct ftrace_ops *ops)
208 {
209 int cpu;
210
211 for_each_possible_cpu(cpu)
212 *per_cpu_ptr(ops->disabled, cpu) = 1;
213 }
214
control_ops_alloc(struct ftrace_ops * ops)215 static int control_ops_alloc(struct ftrace_ops *ops)
216 {
217 int __percpu *disabled;
218
219 disabled = alloc_percpu(int);
220 if (!disabled)
221 return -ENOMEM;
222
223 ops->disabled = disabled;
224 control_ops_disable_all(ops);
225 return 0;
226 }
227
ftrace_sync(struct work_struct * work)228 static void ftrace_sync(struct work_struct *work)
229 {
230 /*
231 * This function is just a stub to implement a hard force
232 * of synchronize_sched(). This requires synchronizing
233 * tasks even in userspace and idle.
234 *
235 * Yes, function tracing is rude.
236 */
237 }
238
ftrace_sync_ipi(void * data)239 static void ftrace_sync_ipi(void *data)
240 {
241 /* Probably not needed, but do it anyway */
242 smp_rmb();
243 }
244
245 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
246 static void update_function_graph_func(void);
247
248 /* Both enabled by default (can be cleared by function_graph tracer flags */
249 static bool fgraph_sleep_time = true;
250 static bool fgraph_graph_time = true;
251
252 #else
update_function_graph_func(void)253 static inline void update_function_graph_func(void) { }
254 #endif
255
256
ftrace_ops_get_list_func(struct ftrace_ops * ops)257 static ftrace_func_t ftrace_ops_get_list_func(struct ftrace_ops *ops)
258 {
259 /*
260 * If this is a dynamic ops or we force list func,
261 * then it needs to call the list anyway.
262 */
263 if (ops->flags & FTRACE_OPS_FL_DYNAMIC || FTRACE_FORCE_LIST_FUNC)
264 return ftrace_ops_list_func;
265
266 return ftrace_ops_get_func(ops);
267 }
268
update_ftrace_function(void)269 static void update_ftrace_function(void)
270 {
271 ftrace_func_t func;
272
273 /*
274 * Prepare the ftrace_ops that the arch callback will use.
275 * If there's only one ftrace_ops registered, the ftrace_ops_list
276 * will point to the ops we want.
277 */
278 set_function_trace_op = ftrace_ops_list;
279
280 /* If there's no ftrace_ops registered, just call the stub function */
281 if (ftrace_ops_list == &ftrace_list_end) {
282 func = ftrace_stub;
283
284 /*
285 * If we are at the end of the list and this ops is
286 * recursion safe and not dynamic and the arch supports passing ops,
287 * then have the mcount trampoline call the function directly.
288 */
289 } else if (ftrace_ops_list->next == &ftrace_list_end) {
290 func = ftrace_ops_get_list_func(ftrace_ops_list);
291
292 } else {
293 /* Just use the default ftrace_ops */
294 set_function_trace_op = &ftrace_list_end;
295 func = ftrace_ops_list_func;
296 }
297
298 update_function_graph_func();
299
300 /* If there's no change, then do nothing more here */
301 if (ftrace_trace_function == func)
302 return;
303
304 /*
305 * If we are using the list function, it doesn't care
306 * about the function_trace_ops.
307 */
308 if (func == ftrace_ops_list_func) {
309 ftrace_trace_function = func;
310 /*
311 * Don't even bother setting function_trace_ops,
312 * it would be racy to do so anyway.
313 */
314 return;
315 }
316
317 #ifndef CONFIG_DYNAMIC_FTRACE
318 /*
319 * For static tracing, we need to be a bit more careful.
320 * The function change takes affect immediately. Thus,
321 * we need to coorditate the setting of the function_trace_ops
322 * with the setting of the ftrace_trace_function.
323 *
324 * Set the function to the list ops, which will call the
325 * function we want, albeit indirectly, but it handles the
326 * ftrace_ops and doesn't depend on function_trace_op.
327 */
328 ftrace_trace_function = ftrace_ops_list_func;
329 /*
330 * Make sure all CPUs see this. Yes this is slow, but static
331 * tracing is slow and nasty to have enabled.
332 */
333 schedule_on_each_cpu(ftrace_sync);
334 /* Now all cpus are using the list ops. */
335 function_trace_op = set_function_trace_op;
336 /* Make sure the function_trace_op is visible on all CPUs */
337 smp_wmb();
338 /* Nasty way to force a rmb on all cpus */
339 smp_call_function(ftrace_sync_ipi, NULL, 1);
340 /* OK, we are all set to update the ftrace_trace_function now! */
341 #endif /* !CONFIG_DYNAMIC_FTRACE */
342
343 ftrace_trace_function = func;
344 }
345
using_ftrace_ops_list_func(void)346 int using_ftrace_ops_list_func(void)
347 {
348 return ftrace_trace_function == ftrace_ops_list_func;
349 }
350
add_ftrace_ops(struct ftrace_ops ** list,struct ftrace_ops * ops)351 static void add_ftrace_ops(struct ftrace_ops **list, struct ftrace_ops *ops)
352 {
353 ops->next = *list;
354 /*
355 * We are entering ops into the list but another
356 * CPU might be walking that list. We need to make sure
357 * the ops->next pointer is valid before another CPU sees
358 * the ops pointer included into the list.
359 */
360 rcu_assign_pointer(*list, ops);
361 }
362
remove_ftrace_ops(struct ftrace_ops ** list,struct ftrace_ops * ops)363 static int remove_ftrace_ops(struct ftrace_ops **list, struct ftrace_ops *ops)
364 {
365 struct ftrace_ops **p;
366
367 /*
368 * If we are removing the last function, then simply point
369 * to the ftrace_stub.
370 */
371 if (*list == ops && ops->next == &ftrace_list_end) {
372 *list = &ftrace_list_end;
373 return 0;
374 }
375
376 for (p = list; *p != &ftrace_list_end; p = &(*p)->next)
377 if (*p == ops)
378 break;
379
380 if (*p != ops)
381 return -1;
382
383 *p = (*p)->next;
384 return 0;
385 }
386
add_ftrace_list_ops(struct ftrace_ops ** list,struct ftrace_ops * main_ops,struct ftrace_ops * ops)387 static void add_ftrace_list_ops(struct ftrace_ops **list,
388 struct ftrace_ops *main_ops,
389 struct ftrace_ops *ops)
390 {
391 int first = *list == &ftrace_list_end;
392 add_ftrace_ops(list, ops);
393 if (first)
394 add_ftrace_ops(&ftrace_ops_list, main_ops);
395 }
396
remove_ftrace_list_ops(struct ftrace_ops ** list,struct ftrace_ops * main_ops,struct ftrace_ops * ops)397 static int remove_ftrace_list_ops(struct ftrace_ops **list,
398 struct ftrace_ops *main_ops,
399 struct ftrace_ops *ops)
400 {
401 int ret = remove_ftrace_ops(list, ops);
402 if (!ret && *list == &ftrace_list_end)
403 ret = remove_ftrace_ops(&ftrace_ops_list, main_ops);
404 return ret;
405 }
406
407 static void ftrace_update_trampoline(struct ftrace_ops *ops);
408
__register_ftrace_function(struct ftrace_ops * ops)409 static int __register_ftrace_function(struct ftrace_ops *ops)
410 {
411 if (ops->flags & FTRACE_OPS_FL_DELETED)
412 return -EINVAL;
413
414 if (WARN_ON(ops->flags & FTRACE_OPS_FL_ENABLED))
415 return -EBUSY;
416
417 #ifndef CONFIG_DYNAMIC_FTRACE_WITH_REGS
418 /*
419 * If the ftrace_ops specifies SAVE_REGS, then it only can be used
420 * if the arch supports it, or SAVE_REGS_IF_SUPPORTED is also set.
421 * Setting SAVE_REGS_IF_SUPPORTED makes SAVE_REGS irrelevant.
422 */
423 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS &&
424 !(ops->flags & FTRACE_OPS_FL_SAVE_REGS_IF_SUPPORTED))
425 return -EINVAL;
426
427 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS_IF_SUPPORTED)
428 ops->flags |= FTRACE_OPS_FL_SAVE_REGS;
429 #endif
430
431 if (!core_kernel_data((unsigned long)ops))
432 ops->flags |= FTRACE_OPS_FL_DYNAMIC;
433
434 if (ops->flags & FTRACE_OPS_FL_CONTROL) {
435 if (control_ops_alloc(ops))
436 return -ENOMEM;
437 add_ftrace_list_ops(&ftrace_control_list, &control_ops, ops);
438 /* The control_ops needs the trampoline update */
439 ops = &control_ops;
440 } else
441 add_ftrace_ops(&ftrace_ops_list, ops);
442
443 /* Always save the function, and reset at unregistering */
444 ops->saved_func = ops->func;
445
446 if (ops->flags & FTRACE_OPS_FL_PID && ftrace_pids_enabled())
447 ops->func = ftrace_pid_func;
448
449 ftrace_update_trampoline(ops);
450
451 if (ftrace_enabled)
452 update_ftrace_function();
453
454 return 0;
455 }
456
__unregister_ftrace_function(struct ftrace_ops * ops)457 static int __unregister_ftrace_function(struct ftrace_ops *ops)
458 {
459 int ret;
460
461 if (WARN_ON(!(ops->flags & FTRACE_OPS_FL_ENABLED)))
462 return -EBUSY;
463
464 if (ops->flags & FTRACE_OPS_FL_CONTROL) {
465 ret = remove_ftrace_list_ops(&ftrace_control_list,
466 &control_ops, ops);
467 } else
468 ret = remove_ftrace_ops(&ftrace_ops_list, ops);
469
470 if (ret < 0)
471 return ret;
472
473 if (ftrace_enabled)
474 update_ftrace_function();
475
476 ops->func = ops->saved_func;
477
478 return 0;
479 }
480
ftrace_update_pid_func(void)481 static void ftrace_update_pid_func(void)
482 {
483 bool enabled = ftrace_pids_enabled();
484 struct ftrace_ops *op;
485
486 /* Only do something if we are tracing something */
487 if (ftrace_trace_function == ftrace_stub)
488 return;
489
490 do_for_each_ftrace_op(op, ftrace_ops_list) {
491 if (op->flags & FTRACE_OPS_FL_PID) {
492 op->func = enabled ? ftrace_pid_func :
493 op->saved_func;
494 ftrace_update_trampoline(op);
495 }
496 } while_for_each_ftrace_op(op);
497
498 update_ftrace_function();
499 }
500
501 #ifdef CONFIG_FUNCTION_PROFILER
502 struct ftrace_profile {
503 struct hlist_node node;
504 unsigned long ip;
505 unsigned long counter;
506 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
507 unsigned long long time;
508 unsigned long long time_squared;
509 #endif
510 };
511
512 struct ftrace_profile_page {
513 struct ftrace_profile_page *next;
514 unsigned long index;
515 struct ftrace_profile records[];
516 };
517
518 struct ftrace_profile_stat {
519 atomic_t disabled;
520 struct hlist_head *hash;
521 struct ftrace_profile_page *pages;
522 struct ftrace_profile_page *start;
523 struct tracer_stat stat;
524 };
525
526 #define PROFILE_RECORDS_SIZE \
527 (PAGE_SIZE - offsetof(struct ftrace_profile_page, records))
528
529 #define PROFILES_PER_PAGE \
530 (PROFILE_RECORDS_SIZE / sizeof(struct ftrace_profile))
531
532 static int ftrace_profile_enabled __read_mostly;
533
534 /* ftrace_profile_lock - synchronize the enable and disable of the profiler */
535 static DEFINE_MUTEX(ftrace_profile_lock);
536
537 static DEFINE_PER_CPU(struct ftrace_profile_stat, ftrace_profile_stats);
538
539 #define FTRACE_PROFILE_HASH_BITS 10
540 #define FTRACE_PROFILE_HASH_SIZE (1 << FTRACE_PROFILE_HASH_BITS)
541
542 static void *
function_stat_next(void * v,int idx)543 function_stat_next(void *v, int idx)
544 {
545 struct ftrace_profile *rec = v;
546 struct ftrace_profile_page *pg;
547
548 pg = (struct ftrace_profile_page *)((unsigned long)rec & PAGE_MASK);
549
550 again:
551 if (idx != 0)
552 rec++;
553
554 if ((void *)rec >= (void *)&pg->records[pg->index]) {
555 pg = pg->next;
556 if (!pg)
557 return NULL;
558 rec = &pg->records[0];
559 if (!rec->counter)
560 goto again;
561 }
562
563 return rec;
564 }
565
function_stat_start(struct tracer_stat * trace)566 static void *function_stat_start(struct tracer_stat *trace)
567 {
568 struct ftrace_profile_stat *stat =
569 container_of(trace, struct ftrace_profile_stat, stat);
570
571 if (!stat || !stat->start)
572 return NULL;
573
574 return function_stat_next(&stat->start->records[0], 0);
575 }
576
577 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
578 /* function graph compares on total time */
function_stat_cmp(void * p1,void * p2)579 static int function_stat_cmp(void *p1, void *p2)
580 {
581 struct ftrace_profile *a = p1;
582 struct ftrace_profile *b = p2;
583
584 if (a->time < b->time)
585 return -1;
586 if (a->time > b->time)
587 return 1;
588 else
589 return 0;
590 }
591 #else
592 /* not function graph compares against hits */
function_stat_cmp(void * p1,void * p2)593 static int function_stat_cmp(void *p1, void *p2)
594 {
595 struct ftrace_profile *a = p1;
596 struct ftrace_profile *b = p2;
597
598 if (a->counter < b->counter)
599 return -1;
600 if (a->counter > b->counter)
601 return 1;
602 else
603 return 0;
604 }
605 #endif
606
function_stat_headers(struct seq_file * m)607 static int function_stat_headers(struct seq_file *m)
608 {
609 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
610 seq_puts(m, " Function "
611 "Hit Time Avg s^2\n"
612 " -------- "
613 "--- ---- --- ---\n");
614 #else
615 seq_puts(m, " Function Hit\n"
616 " -------- ---\n");
617 #endif
618 return 0;
619 }
620
function_stat_show(struct seq_file * m,void * v)621 static int function_stat_show(struct seq_file *m, void *v)
622 {
623 struct ftrace_profile *rec = v;
624 char str[KSYM_SYMBOL_LEN];
625 int ret = 0;
626 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
627 static struct trace_seq s;
628 unsigned long long avg;
629 unsigned long long stddev;
630 #endif
631 mutex_lock(&ftrace_profile_lock);
632
633 /* we raced with function_profile_reset() */
634 if (unlikely(rec->counter == 0)) {
635 ret = -EBUSY;
636 goto out;
637 }
638
639 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
640 avg = div64_ul(rec->time, rec->counter);
641 if (tracing_thresh && (avg < tracing_thresh))
642 goto out;
643 #endif
644
645 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
646 seq_printf(m, " %-30.30s %10lu", str, rec->counter);
647
648 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
649 seq_puts(m, " ");
650
651 /* Sample standard deviation (s^2) */
652 if (rec->counter <= 1)
653 stddev = 0;
654 else {
655 /*
656 * Apply Welford's method:
657 * s^2 = 1 / (n * (n-1)) * (n * \Sum (x_i)^2 - (\Sum x_i)^2)
658 */
659 stddev = rec->counter * rec->time_squared -
660 rec->time * rec->time;
661
662 /*
663 * Divide only 1000 for ns^2 -> us^2 conversion.
664 * trace_print_graph_duration will divide 1000 again.
665 */
666 stddev = div64_ul(stddev,
667 rec->counter * (rec->counter - 1) * 1000);
668 }
669
670 trace_seq_init(&s);
671 trace_print_graph_duration(rec->time, &s);
672 trace_seq_puts(&s, " ");
673 trace_print_graph_duration(avg, &s);
674 trace_seq_puts(&s, " ");
675 trace_print_graph_duration(stddev, &s);
676 trace_print_seq(m, &s);
677 #endif
678 seq_putc(m, '\n');
679 out:
680 mutex_unlock(&ftrace_profile_lock);
681
682 return ret;
683 }
684
ftrace_profile_reset(struct ftrace_profile_stat * stat)685 static void ftrace_profile_reset(struct ftrace_profile_stat *stat)
686 {
687 struct ftrace_profile_page *pg;
688
689 pg = stat->pages = stat->start;
690
691 while (pg) {
692 memset(pg->records, 0, PROFILE_RECORDS_SIZE);
693 pg->index = 0;
694 pg = pg->next;
695 }
696
697 memset(stat->hash, 0,
698 FTRACE_PROFILE_HASH_SIZE * sizeof(struct hlist_head));
699 }
700
ftrace_profile_pages_init(struct ftrace_profile_stat * stat)701 int ftrace_profile_pages_init(struct ftrace_profile_stat *stat)
702 {
703 struct ftrace_profile_page *pg;
704 int functions;
705 int pages;
706 int i;
707
708 /* If we already allocated, do nothing */
709 if (stat->pages)
710 return 0;
711
712 stat->pages = (void *)get_zeroed_page(GFP_KERNEL);
713 if (!stat->pages)
714 return -ENOMEM;
715
716 #ifdef CONFIG_DYNAMIC_FTRACE
717 functions = ftrace_update_tot_cnt;
718 #else
719 /*
720 * We do not know the number of functions that exist because
721 * dynamic tracing is what counts them. With past experience
722 * we have around 20K functions. That should be more than enough.
723 * It is highly unlikely we will execute every function in
724 * the kernel.
725 */
726 functions = 20000;
727 #endif
728
729 pg = stat->start = stat->pages;
730
731 pages = DIV_ROUND_UP(functions, PROFILES_PER_PAGE);
732
733 for (i = 1; i < pages; i++) {
734 pg->next = (void *)get_zeroed_page(GFP_KERNEL);
735 if (!pg->next)
736 goto out_free;
737 pg = pg->next;
738 }
739
740 return 0;
741
742 out_free:
743 pg = stat->start;
744 while (pg) {
745 unsigned long tmp = (unsigned long)pg;
746
747 pg = pg->next;
748 free_page(tmp);
749 }
750
751 stat->pages = NULL;
752 stat->start = NULL;
753
754 return -ENOMEM;
755 }
756
ftrace_profile_init_cpu(int cpu)757 static int ftrace_profile_init_cpu(int cpu)
758 {
759 struct ftrace_profile_stat *stat;
760 int size;
761
762 stat = &per_cpu(ftrace_profile_stats, cpu);
763
764 if (stat->hash) {
765 /* If the profile is already created, simply reset it */
766 ftrace_profile_reset(stat);
767 return 0;
768 }
769
770 /*
771 * We are profiling all functions, but usually only a few thousand
772 * functions are hit. We'll make a hash of 1024 items.
773 */
774 size = FTRACE_PROFILE_HASH_SIZE;
775
776 stat->hash = kzalloc(sizeof(struct hlist_head) * size, GFP_KERNEL);
777
778 if (!stat->hash)
779 return -ENOMEM;
780
781 /* Preallocate the function profiling pages */
782 if (ftrace_profile_pages_init(stat) < 0) {
783 kfree(stat->hash);
784 stat->hash = NULL;
785 return -ENOMEM;
786 }
787
788 return 0;
789 }
790
ftrace_profile_init(void)791 static int ftrace_profile_init(void)
792 {
793 int cpu;
794 int ret = 0;
795
796 for_each_possible_cpu(cpu) {
797 ret = ftrace_profile_init_cpu(cpu);
798 if (ret)
799 break;
800 }
801
802 return ret;
803 }
804
805 /* interrupts must be disabled */
806 static struct ftrace_profile *
ftrace_find_profiled_func(struct ftrace_profile_stat * stat,unsigned long ip)807 ftrace_find_profiled_func(struct ftrace_profile_stat *stat, unsigned long ip)
808 {
809 struct ftrace_profile *rec;
810 struct hlist_head *hhd;
811 unsigned long key;
812
813 key = hash_long(ip, FTRACE_PROFILE_HASH_BITS);
814 hhd = &stat->hash[key];
815
816 if (hlist_empty(hhd))
817 return NULL;
818
819 hlist_for_each_entry_rcu_notrace(rec, hhd, node) {
820 if (rec->ip == ip)
821 return rec;
822 }
823
824 return NULL;
825 }
826
ftrace_add_profile(struct ftrace_profile_stat * stat,struct ftrace_profile * rec)827 static void ftrace_add_profile(struct ftrace_profile_stat *stat,
828 struct ftrace_profile *rec)
829 {
830 unsigned long key;
831
832 key = hash_long(rec->ip, FTRACE_PROFILE_HASH_BITS);
833 hlist_add_head_rcu(&rec->node, &stat->hash[key]);
834 }
835
836 /*
837 * The memory is already allocated, this simply finds a new record to use.
838 */
839 static struct ftrace_profile *
ftrace_profile_alloc(struct ftrace_profile_stat * stat,unsigned long ip)840 ftrace_profile_alloc(struct ftrace_profile_stat *stat, unsigned long ip)
841 {
842 struct ftrace_profile *rec = NULL;
843
844 /* prevent recursion (from NMIs) */
845 if (atomic_inc_return(&stat->disabled) != 1)
846 goto out;
847
848 /*
849 * Try to find the function again since an NMI
850 * could have added it
851 */
852 rec = ftrace_find_profiled_func(stat, ip);
853 if (rec)
854 goto out;
855
856 if (stat->pages->index == PROFILES_PER_PAGE) {
857 if (!stat->pages->next)
858 goto out;
859 stat->pages = stat->pages->next;
860 }
861
862 rec = &stat->pages->records[stat->pages->index++];
863 rec->ip = ip;
864 ftrace_add_profile(stat, rec);
865
866 out:
867 atomic_dec(&stat->disabled);
868
869 return rec;
870 }
871
872 static void
function_profile_call(unsigned long ip,unsigned long parent_ip,struct ftrace_ops * ops,struct pt_regs * regs)873 function_profile_call(unsigned long ip, unsigned long parent_ip,
874 struct ftrace_ops *ops, struct pt_regs *regs)
875 {
876 struct ftrace_profile_stat *stat;
877 struct ftrace_profile *rec;
878 unsigned long flags;
879
880 if (!ftrace_profile_enabled)
881 return;
882
883 local_irq_save(flags);
884
885 stat = this_cpu_ptr(&ftrace_profile_stats);
886 if (!stat->hash || !ftrace_profile_enabled)
887 goto out;
888
889 rec = ftrace_find_profiled_func(stat, ip);
890 if (!rec) {
891 rec = ftrace_profile_alloc(stat, ip);
892 if (!rec)
893 goto out;
894 }
895
896 rec->counter++;
897 out:
898 local_irq_restore(flags);
899 }
900
901 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
profile_graph_entry(struct ftrace_graph_ent * trace)902 static int profile_graph_entry(struct ftrace_graph_ent *trace)
903 {
904 function_profile_call(trace->func, 0, NULL, NULL);
905 return 1;
906 }
907
profile_graph_return(struct ftrace_graph_ret * trace)908 static void profile_graph_return(struct ftrace_graph_ret *trace)
909 {
910 struct ftrace_profile_stat *stat;
911 unsigned long long calltime;
912 struct ftrace_profile *rec;
913 unsigned long flags;
914
915 local_irq_save(flags);
916 stat = this_cpu_ptr(&ftrace_profile_stats);
917 if (!stat->hash || !ftrace_profile_enabled)
918 goto out;
919
920 /* If the calltime was zero'd ignore it */
921 if (!trace->calltime)
922 goto out;
923
924 calltime = trace->rettime - trace->calltime;
925
926 if (!fgraph_graph_time) {
927 int index;
928
929 index = trace->depth;
930
931 /* Append this call time to the parent time to subtract */
932 if (index)
933 current->ret_stack[index - 1].subtime += calltime;
934
935 if (current->ret_stack[index].subtime < calltime)
936 calltime -= current->ret_stack[index].subtime;
937 else
938 calltime = 0;
939 }
940
941 rec = ftrace_find_profiled_func(stat, trace->func);
942 if (rec) {
943 rec->time += calltime;
944 rec->time_squared += calltime * calltime;
945 }
946
947 out:
948 local_irq_restore(flags);
949 }
950
register_ftrace_profiler(void)951 static int register_ftrace_profiler(void)
952 {
953 return register_ftrace_graph(&profile_graph_return,
954 &profile_graph_entry);
955 }
956
unregister_ftrace_profiler(void)957 static void unregister_ftrace_profiler(void)
958 {
959 unregister_ftrace_graph();
960 }
961 #else
962 static struct ftrace_ops ftrace_profile_ops __read_mostly = {
963 .func = function_profile_call,
964 .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_INITIALIZED,
965 INIT_OPS_HASH(ftrace_profile_ops)
966 };
967
register_ftrace_profiler(void)968 static int register_ftrace_profiler(void)
969 {
970 return register_ftrace_function(&ftrace_profile_ops);
971 }
972
unregister_ftrace_profiler(void)973 static void unregister_ftrace_profiler(void)
974 {
975 unregister_ftrace_function(&ftrace_profile_ops);
976 }
977 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
978
979 static ssize_t
ftrace_profile_write(struct file * filp,const char __user * ubuf,size_t cnt,loff_t * ppos)980 ftrace_profile_write(struct file *filp, const char __user *ubuf,
981 size_t cnt, loff_t *ppos)
982 {
983 unsigned long val;
984 int ret;
985
986 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
987 if (ret)
988 return ret;
989
990 val = !!val;
991
992 mutex_lock(&ftrace_profile_lock);
993 if (ftrace_profile_enabled ^ val) {
994 if (val) {
995 ret = ftrace_profile_init();
996 if (ret < 0) {
997 cnt = ret;
998 goto out;
999 }
1000
1001 ret = register_ftrace_profiler();
1002 if (ret < 0) {
1003 cnt = ret;
1004 goto out;
1005 }
1006 ftrace_profile_enabled = 1;
1007 } else {
1008 ftrace_profile_enabled = 0;
1009 /*
1010 * unregister_ftrace_profiler calls stop_machine
1011 * so this acts like an synchronize_sched.
1012 */
1013 unregister_ftrace_profiler();
1014 }
1015 }
1016 out:
1017 mutex_unlock(&ftrace_profile_lock);
1018
1019 *ppos += cnt;
1020
1021 return cnt;
1022 }
1023
1024 static ssize_t
ftrace_profile_read(struct file * filp,char __user * ubuf,size_t cnt,loff_t * ppos)1025 ftrace_profile_read(struct file *filp, char __user *ubuf,
1026 size_t cnt, loff_t *ppos)
1027 {
1028 char buf[64]; /* big enough to hold a number */
1029 int r;
1030
1031 r = sprintf(buf, "%u\n", ftrace_profile_enabled);
1032 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
1033 }
1034
1035 static const struct file_operations ftrace_profile_fops = {
1036 .open = tracing_open_generic,
1037 .read = ftrace_profile_read,
1038 .write = ftrace_profile_write,
1039 .llseek = default_llseek,
1040 };
1041
1042 /* used to initialize the real stat files */
1043 static struct tracer_stat function_stats __initdata = {
1044 .name = "functions",
1045 .stat_start = function_stat_start,
1046 .stat_next = function_stat_next,
1047 .stat_cmp = function_stat_cmp,
1048 .stat_headers = function_stat_headers,
1049 .stat_show = function_stat_show
1050 };
1051
ftrace_profile_tracefs(struct dentry * d_tracer)1052 static __init void ftrace_profile_tracefs(struct dentry *d_tracer)
1053 {
1054 struct ftrace_profile_stat *stat;
1055 struct dentry *entry;
1056 char *name;
1057 int ret;
1058 int cpu;
1059
1060 for_each_possible_cpu(cpu) {
1061 stat = &per_cpu(ftrace_profile_stats, cpu);
1062
1063 /* allocate enough for function name + cpu number */
1064 name = kmalloc(32, GFP_KERNEL);
1065 if (!name) {
1066 /*
1067 * The files created are permanent, if something happens
1068 * we still do not free memory.
1069 */
1070 WARN(1,
1071 "Could not allocate stat file for cpu %d\n",
1072 cpu);
1073 return;
1074 }
1075 stat->stat = function_stats;
1076 snprintf(name, 32, "function%d", cpu);
1077 stat->stat.name = name;
1078 ret = register_stat_tracer(&stat->stat);
1079 if (ret) {
1080 WARN(1,
1081 "Could not register function stat for cpu %d\n",
1082 cpu);
1083 kfree(name);
1084 return;
1085 }
1086 }
1087
1088 entry = tracefs_create_file("function_profile_enabled", 0644,
1089 d_tracer, NULL, &ftrace_profile_fops);
1090 if (!entry)
1091 pr_warning("Could not create tracefs "
1092 "'function_profile_enabled' entry\n");
1093 }
1094
1095 #else /* CONFIG_FUNCTION_PROFILER */
ftrace_profile_tracefs(struct dentry * d_tracer)1096 static __init void ftrace_profile_tracefs(struct dentry *d_tracer)
1097 {
1098 }
1099 #endif /* CONFIG_FUNCTION_PROFILER */
1100
1101 static struct pid * const ftrace_swapper_pid = &init_struct_pid;
1102
1103 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
1104 static int ftrace_graph_active;
1105 #else
1106 # define ftrace_graph_active 0
1107 #endif
1108
1109 #ifdef CONFIG_DYNAMIC_FTRACE
1110
1111 static struct ftrace_ops *removed_ops;
1112
1113 /*
1114 * Set when doing a global update, like enabling all recs or disabling them.
1115 * It is not set when just updating a single ftrace_ops.
1116 */
1117 static bool update_all_ops;
1118
1119 #ifndef CONFIG_FTRACE_MCOUNT_RECORD
1120 # error Dynamic ftrace depends on MCOUNT_RECORD
1121 #endif
1122
1123 static struct hlist_head ftrace_func_hash[FTRACE_FUNC_HASHSIZE] __read_mostly;
1124
1125 struct ftrace_func_probe {
1126 struct hlist_node node;
1127 struct ftrace_probe_ops *ops;
1128 unsigned long flags;
1129 unsigned long ip;
1130 void *data;
1131 struct list_head free_list;
1132 };
1133
1134 struct ftrace_func_entry {
1135 struct hlist_node hlist;
1136 unsigned long ip;
1137 };
1138
1139 struct ftrace_hash {
1140 unsigned long size_bits;
1141 struct hlist_head *buckets;
1142 unsigned long count;
1143 struct rcu_head rcu;
1144 };
1145
1146 /*
1147 * We make these constant because no one should touch them,
1148 * but they are used as the default "empty hash", to avoid allocating
1149 * it all the time. These are in a read only section such that if
1150 * anyone does try to modify it, it will cause an exception.
1151 */
1152 static const struct hlist_head empty_buckets[1];
1153 static const struct ftrace_hash empty_hash = {
1154 .buckets = (struct hlist_head *)empty_buckets,
1155 };
1156 #define EMPTY_HASH ((struct ftrace_hash *)&empty_hash)
1157
1158 static struct ftrace_ops global_ops = {
1159 .func = ftrace_stub,
1160 .local_hash.notrace_hash = EMPTY_HASH,
1161 .local_hash.filter_hash = EMPTY_HASH,
1162 INIT_OPS_HASH(global_ops)
1163 .flags = FTRACE_OPS_FL_RECURSION_SAFE |
1164 FTRACE_OPS_FL_INITIALIZED |
1165 FTRACE_OPS_FL_PID,
1166 };
1167
1168 /*
1169 * This is used by __kernel_text_address() to return true if the
1170 * address is on a dynamically allocated trampoline that would
1171 * not return true for either core_kernel_text() or
1172 * is_module_text_address().
1173 */
is_ftrace_trampoline(unsigned long addr)1174 bool is_ftrace_trampoline(unsigned long addr)
1175 {
1176 struct ftrace_ops *op;
1177 bool ret = false;
1178
1179 /*
1180 * Some of the ops may be dynamically allocated,
1181 * they are freed after a synchronize_sched().
1182 */
1183 preempt_disable_notrace();
1184
1185 do_for_each_ftrace_op(op, ftrace_ops_list) {
1186 /*
1187 * This is to check for dynamically allocated trampolines.
1188 * Trampolines that are in kernel text will have
1189 * core_kernel_text() return true.
1190 */
1191 if (op->trampoline && op->trampoline_size)
1192 if (addr >= op->trampoline &&
1193 addr < op->trampoline + op->trampoline_size) {
1194 ret = true;
1195 goto out;
1196 }
1197 } while_for_each_ftrace_op(op);
1198
1199 out:
1200 preempt_enable_notrace();
1201
1202 return ret;
1203 }
1204
1205 struct ftrace_page {
1206 struct ftrace_page *next;
1207 struct dyn_ftrace *records;
1208 int index;
1209 int size;
1210 };
1211
1212 #define ENTRY_SIZE sizeof(struct dyn_ftrace)
1213 #define ENTRIES_PER_PAGE (PAGE_SIZE / ENTRY_SIZE)
1214
1215 /* estimate from running different kernels */
1216 #define NR_TO_INIT 10000
1217
1218 static struct ftrace_page *ftrace_pages_start;
1219 static struct ftrace_page *ftrace_pages;
1220
ftrace_hash_empty(struct ftrace_hash * hash)1221 static bool __always_inline ftrace_hash_empty(struct ftrace_hash *hash)
1222 {
1223 return !hash || !hash->count;
1224 }
1225
1226 static struct ftrace_func_entry *
ftrace_lookup_ip(struct ftrace_hash * hash,unsigned long ip)1227 ftrace_lookup_ip(struct ftrace_hash *hash, unsigned long ip)
1228 {
1229 unsigned long key;
1230 struct ftrace_func_entry *entry;
1231 struct hlist_head *hhd;
1232
1233 if (ftrace_hash_empty(hash))
1234 return NULL;
1235
1236 if (hash->size_bits > 0)
1237 key = hash_long(ip, hash->size_bits);
1238 else
1239 key = 0;
1240
1241 hhd = &hash->buckets[key];
1242
1243 hlist_for_each_entry_rcu_notrace(entry, hhd, hlist) {
1244 if (entry->ip == ip)
1245 return entry;
1246 }
1247 return NULL;
1248 }
1249
__add_hash_entry(struct ftrace_hash * hash,struct ftrace_func_entry * entry)1250 static void __add_hash_entry(struct ftrace_hash *hash,
1251 struct ftrace_func_entry *entry)
1252 {
1253 struct hlist_head *hhd;
1254 unsigned long key;
1255
1256 if (hash->size_bits)
1257 key = hash_long(entry->ip, hash->size_bits);
1258 else
1259 key = 0;
1260
1261 hhd = &hash->buckets[key];
1262 hlist_add_head(&entry->hlist, hhd);
1263 hash->count++;
1264 }
1265
add_hash_entry(struct ftrace_hash * hash,unsigned long ip)1266 static int add_hash_entry(struct ftrace_hash *hash, unsigned long ip)
1267 {
1268 struct ftrace_func_entry *entry;
1269
1270 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
1271 if (!entry)
1272 return -ENOMEM;
1273
1274 entry->ip = ip;
1275 __add_hash_entry(hash, entry);
1276
1277 return 0;
1278 }
1279
1280 static void
free_hash_entry(struct ftrace_hash * hash,struct ftrace_func_entry * entry)1281 free_hash_entry(struct ftrace_hash *hash,
1282 struct ftrace_func_entry *entry)
1283 {
1284 hlist_del(&entry->hlist);
1285 kfree(entry);
1286 hash->count--;
1287 }
1288
1289 static void
remove_hash_entry(struct ftrace_hash * hash,struct ftrace_func_entry * entry)1290 remove_hash_entry(struct ftrace_hash *hash,
1291 struct ftrace_func_entry *entry)
1292 {
1293 hlist_del(&entry->hlist);
1294 hash->count--;
1295 }
1296
ftrace_hash_clear(struct ftrace_hash * hash)1297 static void ftrace_hash_clear(struct ftrace_hash *hash)
1298 {
1299 struct hlist_head *hhd;
1300 struct hlist_node *tn;
1301 struct ftrace_func_entry *entry;
1302 int size = 1 << hash->size_bits;
1303 int i;
1304
1305 if (!hash->count)
1306 return;
1307
1308 for (i = 0; i < size; i++) {
1309 hhd = &hash->buckets[i];
1310 hlist_for_each_entry_safe(entry, tn, hhd, hlist)
1311 free_hash_entry(hash, entry);
1312 }
1313 FTRACE_WARN_ON(hash->count);
1314 }
1315
free_ftrace_hash(struct ftrace_hash * hash)1316 static void free_ftrace_hash(struct ftrace_hash *hash)
1317 {
1318 if (!hash || hash == EMPTY_HASH)
1319 return;
1320 ftrace_hash_clear(hash);
1321 kfree(hash->buckets);
1322 kfree(hash);
1323 }
1324
__free_ftrace_hash_rcu(struct rcu_head * rcu)1325 static void __free_ftrace_hash_rcu(struct rcu_head *rcu)
1326 {
1327 struct ftrace_hash *hash;
1328
1329 hash = container_of(rcu, struct ftrace_hash, rcu);
1330 free_ftrace_hash(hash);
1331 }
1332
free_ftrace_hash_rcu(struct ftrace_hash * hash)1333 static void free_ftrace_hash_rcu(struct ftrace_hash *hash)
1334 {
1335 if (!hash || hash == EMPTY_HASH)
1336 return;
1337 call_rcu_sched(&hash->rcu, __free_ftrace_hash_rcu);
1338 }
1339
ftrace_free_filter(struct ftrace_ops * ops)1340 void ftrace_free_filter(struct ftrace_ops *ops)
1341 {
1342 ftrace_ops_init(ops);
1343 free_ftrace_hash(ops->func_hash->filter_hash);
1344 free_ftrace_hash(ops->func_hash->notrace_hash);
1345 }
1346
alloc_ftrace_hash(int size_bits)1347 static struct ftrace_hash *alloc_ftrace_hash(int size_bits)
1348 {
1349 struct ftrace_hash *hash;
1350 int size;
1351
1352 hash = kzalloc(sizeof(*hash), GFP_KERNEL);
1353 if (!hash)
1354 return NULL;
1355
1356 size = 1 << size_bits;
1357 hash->buckets = kcalloc(size, sizeof(*hash->buckets), GFP_KERNEL);
1358
1359 if (!hash->buckets) {
1360 kfree(hash);
1361 return NULL;
1362 }
1363
1364 hash->size_bits = size_bits;
1365
1366 return hash;
1367 }
1368
1369 static struct ftrace_hash *
alloc_and_copy_ftrace_hash(int size_bits,struct ftrace_hash * hash)1370 alloc_and_copy_ftrace_hash(int size_bits, struct ftrace_hash *hash)
1371 {
1372 struct ftrace_func_entry *entry;
1373 struct ftrace_hash *new_hash;
1374 int size;
1375 int ret;
1376 int i;
1377
1378 new_hash = alloc_ftrace_hash(size_bits);
1379 if (!new_hash)
1380 return NULL;
1381
1382 /* Empty hash? */
1383 if (ftrace_hash_empty(hash))
1384 return new_hash;
1385
1386 size = 1 << hash->size_bits;
1387 for (i = 0; i < size; i++) {
1388 hlist_for_each_entry(entry, &hash->buckets[i], hlist) {
1389 ret = add_hash_entry(new_hash, entry->ip);
1390 if (ret < 0)
1391 goto free_hash;
1392 }
1393 }
1394
1395 FTRACE_WARN_ON(new_hash->count != hash->count);
1396
1397 return new_hash;
1398
1399 free_hash:
1400 free_ftrace_hash(new_hash);
1401 return NULL;
1402 }
1403
1404 static void
1405 ftrace_hash_rec_disable_modify(struct ftrace_ops *ops, int filter_hash);
1406 static void
1407 ftrace_hash_rec_enable_modify(struct ftrace_ops *ops, int filter_hash);
1408
1409 static int ftrace_hash_ipmodify_update(struct ftrace_ops *ops,
1410 struct ftrace_hash *new_hash);
1411
1412 static int
ftrace_hash_move(struct ftrace_ops * ops,int enable,struct ftrace_hash ** dst,struct ftrace_hash * src)1413 ftrace_hash_move(struct ftrace_ops *ops, int enable,
1414 struct ftrace_hash **dst, struct ftrace_hash *src)
1415 {
1416 struct ftrace_func_entry *entry;
1417 struct hlist_node *tn;
1418 struct hlist_head *hhd;
1419 struct ftrace_hash *new_hash;
1420 int size = src->count;
1421 int bits = 0;
1422 int ret;
1423 int i;
1424
1425 /* Reject setting notrace hash on IPMODIFY ftrace_ops */
1426 if (ops->flags & FTRACE_OPS_FL_IPMODIFY && !enable)
1427 return -EINVAL;
1428
1429 /*
1430 * If the new source is empty, just free dst and assign it
1431 * the empty_hash.
1432 */
1433 if (!src->count) {
1434 new_hash = EMPTY_HASH;
1435 goto update;
1436 }
1437
1438 /*
1439 * Make the hash size about 1/2 the # found
1440 */
1441 for (size /= 2; size; size >>= 1)
1442 bits++;
1443
1444 /* Don't allocate too much */
1445 if (bits > FTRACE_HASH_MAX_BITS)
1446 bits = FTRACE_HASH_MAX_BITS;
1447
1448 new_hash = alloc_ftrace_hash(bits);
1449 if (!new_hash)
1450 return -ENOMEM;
1451
1452 size = 1 << src->size_bits;
1453 for (i = 0; i < size; i++) {
1454 hhd = &src->buckets[i];
1455 hlist_for_each_entry_safe(entry, tn, hhd, hlist) {
1456 remove_hash_entry(src, entry);
1457 __add_hash_entry(new_hash, entry);
1458 }
1459 }
1460
1461 update:
1462 /* Make sure this can be applied if it is IPMODIFY ftrace_ops */
1463 if (enable) {
1464 /* IPMODIFY should be updated only when filter_hash updating */
1465 ret = ftrace_hash_ipmodify_update(ops, new_hash);
1466 if (ret < 0) {
1467 free_ftrace_hash(new_hash);
1468 return ret;
1469 }
1470 }
1471
1472 /*
1473 * Remove the current set, update the hash and add
1474 * them back.
1475 */
1476 ftrace_hash_rec_disable_modify(ops, enable);
1477
1478 rcu_assign_pointer(*dst, new_hash);
1479
1480 ftrace_hash_rec_enable_modify(ops, enable);
1481
1482 return 0;
1483 }
1484
hash_contains_ip(unsigned long ip,struct ftrace_ops_hash * hash)1485 static bool hash_contains_ip(unsigned long ip,
1486 struct ftrace_ops_hash *hash)
1487 {
1488 /*
1489 * The function record is a match if it exists in the filter
1490 * hash and not in the notrace hash. Note, an emty hash is
1491 * considered a match for the filter hash, but an empty
1492 * notrace hash is considered not in the notrace hash.
1493 */
1494 return (ftrace_hash_empty(hash->filter_hash) ||
1495 ftrace_lookup_ip(hash->filter_hash, ip)) &&
1496 (ftrace_hash_empty(hash->notrace_hash) ||
1497 !ftrace_lookup_ip(hash->notrace_hash, ip));
1498 }
1499
1500 /*
1501 * Test the hashes for this ops to see if we want to call
1502 * the ops->func or not.
1503 *
1504 * It's a match if the ip is in the ops->filter_hash or
1505 * the filter_hash does not exist or is empty,
1506 * AND
1507 * the ip is not in the ops->notrace_hash.
1508 *
1509 * This needs to be called with preemption disabled as
1510 * the hashes are freed with call_rcu_sched().
1511 */
1512 static int
ftrace_ops_test(struct ftrace_ops * ops,unsigned long ip,void * regs)1513 ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip, void *regs)
1514 {
1515 struct ftrace_ops_hash hash;
1516 int ret;
1517
1518 #ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS
1519 /*
1520 * There's a small race when adding ops that the ftrace handler
1521 * that wants regs, may be called without them. We can not
1522 * allow that handler to be called if regs is NULL.
1523 */
1524 if (regs == NULL && (ops->flags & FTRACE_OPS_FL_SAVE_REGS))
1525 return 0;
1526 #endif
1527
1528 hash.filter_hash = rcu_dereference_raw_notrace(ops->func_hash->filter_hash);
1529 hash.notrace_hash = rcu_dereference_raw_notrace(ops->func_hash->notrace_hash);
1530
1531 if (hash_contains_ip(ip, &hash))
1532 ret = 1;
1533 else
1534 ret = 0;
1535
1536 return ret;
1537 }
1538
1539 /*
1540 * This is a double for. Do not use 'break' to break out of the loop,
1541 * you must use a goto.
1542 */
1543 #define do_for_each_ftrace_rec(pg, rec) \
1544 for (pg = ftrace_pages_start; pg; pg = pg->next) { \
1545 int _____i; \
1546 for (_____i = 0; _____i < pg->index; _____i++) { \
1547 rec = &pg->records[_____i];
1548
1549 #define while_for_each_ftrace_rec() \
1550 } \
1551 }
1552
1553
ftrace_cmp_recs(const void * a,const void * b)1554 static int ftrace_cmp_recs(const void *a, const void *b)
1555 {
1556 const struct dyn_ftrace *key = a;
1557 const struct dyn_ftrace *rec = b;
1558
1559 if (key->flags < rec->ip)
1560 return -1;
1561 if (key->ip >= rec->ip + MCOUNT_INSN_SIZE)
1562 return 1;
1563 return 0;
1564 }
1565
ftrace_location_range(unsigned long start,unsigned long end)1566 static unsigned long ftrace_location_range(unsigned long start, unsigned long end)
1567 {
1568 struct ftrace_page *pg;
1569 struct dyn_ftrace *rec;
1570 struct dyn_ftrace key;
1571
1572 key.ip = start;
1573 key.flags = end; /* overload flags, as it is unsigned long */
1574
1575 for (pg = ftrace_pages_start; pg; pg = pg->next) {
1576 if (end < pg->records[0].ip ||
1577 start >= (pg->records[pg->index - 1].ip + MCOUNT_INSN_SIZE))
1578 continue;
1579 rec = bsearch(&key, pg->records, pg->index,
1580 sizeof(struct dyn_ftrace),
1581 ftrace_cmp_recs);
1582 if (rec)
1583 return rec->ip;
1584 }
1585
1586 return 0;
1587 }
1588
1589 /**
1590 * ftrace_location - return true if the ip giving is a traced location
1591 * @ip: the instruction pointer to check
1592 *
1593 * Returns rec->ip if @ip given is a pointer to a ftrace location.
1594 * That is, the instruction that is either a NOP or call to
1595 * the function tracer. It checks the ftrace internal tables to
1596 * determine if the address belongs or not.
1597 */
ftrace_location(unsigned long ip)1598 unsigned long ftrace_location(unsigned long ip)
1599 {
1600 return ftrace_location_range(ip, ip);
1601 }
1602
1603 /**
1604 * ftrace_text_reserved - return true if range contains an ftrace location
1605 * @start: start of range to search
1606 * @end: end of range to search (inclusive). @end points to the last byte to check.
1607 *
1608 * Returns 1 if @start and @end contains a ftrace location.
1609 * That is, the instruction that is either a NOP or call to
1610 * the function tracer. It checks the ftrace internal tables to
1611 * determine if the address belongs or not.
1612 */
ftrace_text_reserved(const void * start,const void * end)1613 int ftrace_text_reserved(const void *start, const void *end)
1614 {
1615 unsigned long ret;
1616
1617 ret = ftrace_location_range((unsigned long)start,
1618 (unsigned long)end);
1619
1620 return (int)!!ret;
1621 }
1622
1623 /* Test if ops registered to this rec needs regs */
test_rec_ops_needs_regs(struct dyn_ftrace * rec)1624 static bool test_rec_ops_needs_regs(struct dyn_ftrace *rec)
1625 {
1626 struct ftrace_ops *ops;
1627 bool keep_regs = false;
1628
1629 for (ops = ftrace_ops_list;
1630 ops != &ftrace_list_end; ops = ops->next) {
1631 /* pass rec in as regs to have non-NULL val */
1632 if (ftrace_ops_test(ops, rec->ip, rec)) {
1633 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS) {
1634 keep_regs = true;
1635 break;
1636 }
1637 }
1638 }
1639
1640 return keep_regs;
1641 }
1642
__ftrace_hash_rec_update(struct ftrace_ops * ops,int filter_hash,bool inc)1643 static void __ftrace_hash_rec_update(struct ftrace_ops *ops,
1644 int filter_hash,
1645 bool inc)
1646 {
1647 struct ftrace_hash *hash;
1648 struct ftrace_hash *other_hash;
1649 struct ftrace_page *pg;
1650 struct dyn_ftrace *rec;
1651 int count = 0;
1652 int all = 0;
1653
1654 /* Only update if the ops has been registered */
1655 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
1656 return;
1657
1658 /*
1659 * In the filter_hash case:
1660 * If the count is zero, we update all records.
1661 * Otherwise we just update the items in the hash.
1662 *
1663 * In the notrace_hash case:
1664 * We enable the update in the hash.
1665 * As disabling notrace means enabling the tracing,
1666 * and enabling notrace means disabling, the inc variable
1667 * gets inversed.
1668 */
1669 if (filter_hash) {
1670 hash = ops->func_hash->filter_hash;
1671 other_hash = ops->func_hash->notrace_hash;
1672 if (ftrace_hash_empty(hash))
1673 all = 1;
1674 } else {
1675 inc = !inc;
1676 hash = ops->func_hash->notrace_hash;
1677 other_hash = ops->func_hash->filter_hash;
1678 /*
1679 * If the notrace hash has no items,
1680 * then there's nothing to do.
1681 */
1682 if (ftrace_hash_empty(hash))
1683 return;
1684 }
1685
1686 do_for_each_ftrace_rec(pg, rec) {
1687 int in_other_hash = 0;
1688 int in_hash = 0;
1689 int match = 0;
1690
1691 if (all) {
1692 /*
1693 * Only the filter_hash affects all records.
1694 * Update if the record is not in the notrace hash.
1695 */
1696 if (!other_hash || !ftrace_lookup_ip(other_hash, rec->ip))
1697 match = 1;
1698 } else {
1699 in_hash = !!ftrace_lookup_ip(hash, rec->ip);
1700 in_other_hash = !!ftrace_lookup_ip(other_hash, rec->ip);
1701
1702 /*
1703 * If filter_hash is set, we want to match all functions
1704 * that are in the hash but not in the other hash.
1705 *
1706 * If filter_hash is not set, then we are decrementing.
1707 * That means we match anything that is in the hash
1708 * and also in the other_hash. That is, we need to turn
1709 * off functions in the other hash because they are disabled
1710 * by this hash.
1711 */
1712 if (filter_hash && in_hash && !in_other_hash)
1713 match = 1;
1714 else if (!filter_hash && in_hash &&
1715 (in_other_hash || ftrace_hash_empty(other_hash)))
1716 match = 1;
1717 }
1718 if (!match)
1719 continue;
1720
1721 if (inc) {
1722 rec->flags++;
1723 if (FTRACE_WARN_ON(ftrace_rec_count(rec) == FTRACE_REF_MAX))
1724 return;
1725
1726 /*
1727 * If there's only a single callback registered to a
1728 * function, and the ops has a trampoline registered
1729 * for it, then we can call it directly.
1730 */
1731 if (ftrace_rec_count(rec) == 1 && ops->trampoline)
1732 rec->flags |= FTRACE_FL_TRAMP;
1733 else
1734 /*
1735 * If we are adding another function callback
1736 * to this function, and the previous had a
1737 * custom trampoline in use, then we need to go
1738 * back to the default trampoline.
1739 */
1740 rec->flags &= ~FTRACE_FL_TRAMP;
1741
1742 /*
1743 * If any ops wants regs saved for this function
1744 * then all ops will get saved regs.
1745 */
1746 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS)
1747 rec->flags |= FTRACE_FL_REGS;
1748 } else {
1749 if (FTRACE_WARN_ON(ftrace_rec_count(rec) == 0))
1750 return;
1751 rec->flags--;
1752
1753 /*
1754 * If the rec had REGS enabled and the ops that is
1755 * being removed had REGS set, then see if there is
1756 * still any ops for this record that wants regs.
1757 * If not, we can stop recording them.
1758 */
1759 if (ftrace_rec_count(rec) > 0 &&
1760 rec->flags & FTRACE_FL_REGS &&
1761 ops->flags & FTRACE_OPS_FL_SAVE_REGS) {
1762 if (!test_rec_ops_needs_regs(rec))
1763 rec->flags &= ~FTRACE_FL_REGS;
1764 }
1765
1766 /*
1767 * If the rec had TRAMP enabled, then it needs to
1768 * be cleared. As TRAMP can only be enabled iff
1769 * there is only a single ops attached to it.
1770 * In otherwords, always disable it on decrementing.
1771 * In the future, we may set it if rec count is
1772 * decremented to one, and the ops that is left
1773 * has a trampoline.
1774 */
1775 rec->flags &= ~FTRACE_FL_TRAMP;
1776
1777 /*
1778 * flags will be cleared in ftrace_check_record()
1779 * if rec count is zero.
1780 */
1781 }
1782 count++;
1783 /* Shortcut, if we handled all records, we are done. */
1784 if (!all && count == hash->count)
1785 return;
1786 } while_for_each_ftrace_rec();
1787 }
1788
ftrace_hash_rec_disable(struct ftrace_ops * ops,int filter_hash)1789 static void ftrace_hash_rec_disable(struct ftrace_ops *ops,
1790 int filter_hash)
1791 {
1792 __ftrace_hash_rec_update(ops, filter_hash, 0);
1793 }
1794
ftrace_hash_rec_enable(struct ftrace_ops * ops,int filter_hash)1795 static void ftrace_hash_rec_enable(struct ftrace_ops *ops,
1796 int filter_hash)
1797 {
1798 __ftrace_hash_rec_update(ops, filter_hash, 1);
1799 }
1800
ftrace_hash_rec_update_modify(struct ftrace_ops * ops,int filter_hash,int inc)1801 static void ftrace_hash_rec_update_modify(struct ftrace_ops *ops,
1802 int filter_hash, int inc)
1803 {
1804 struct ftrace_ops *op;
1805
1806 __ftrace_hash_rec_update(ops, filter_hash, inc);
1807
1808 if (ops->func_hash != &global_ops.local_hash)
1809 return;
1810
1811 /*
1812 * If the ops shares the global_ops hash, then we need to update
1813 * all ops that are enabled and use this hash.
1814 */
1815 do_for_each_ftrace_op(op, ftrace_ops_list) {
1816 /* Already done */
1817 if (op == ops)
1818 continue;
1819 if (op->func_hash == &global_ops.local_hash)
1820 __ftrace_hash_rec_update(op, filter_hash, inc);
1821 } while_for_each_ftrace_op(op);
1822 }
1823
ftrace_hash_rec_disable_modify(struct ftrace_ops * ops,int filter_hash)1824 static void ftrace_hash_rec_disable_modify(struct ftrace_ops *ops,
1825 int filter_hash)
1826 {
1827 ftrace_hash_rec_update_modify(ops, filter_hash, 0);
1828 }
1829
ftrace_hash_rec_enable_modify(struct ftrace_ops * ops,int filter_hash)1830 static void ftrace_hash_rec_enable_modify(struct ftrace_ops *ops,
1831 int filter_hash)
1832 {
1833 ftrace_hash_rec_update_modify(ops, filter_hash, 1);
1834 }
1835
1836 /*
1837 * Try to update IPMODIFY flag on each ftrace_rec. Return 0 if it is OK
1838 * or no-needed to update, -EBUSY if it detects a conflict of the flag
1839 * on a ftrace_rec, and -EINVAL if the new_hash tries to trace all recs.
1840 * Note that old_hash and new_hash has below meanings
1841 * - If the hash is NULL, it hits all recs (if IPMODIFY is set, this is rejected)
1842 * - If the hash is EMPTY_HASH, it hits nothing
1843 * - Anything else hits the recs which match the hash entries.
1844 */
__ftrace_hash_update_ipmodify(struct ftrace_ops * ops,struct ftrace_hash * old_hash,struct ftrace_hash * new_hash)1845 static int __ftrace_hash_update_ipmodify(struct ftrace_ops *ops,
1846 struct ftrace_hash *old_hash,
1847 struct ftrace_hash *new_hash)
1848 {
1849 struct ftrace_page *pg;
1850 struct dyn_ftrace *rec, *end = NULL;
1851 int in_old, in_new;
1852
1853 /* Only update if the ops has been registered */
1854 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
1855 return 0;
1856
1857 if (!(ops->flags & FTRACE_OPS_FL_IPMODIFY))
1858 return 0;
1859
1860 /*
1861 * Since the IPMODIFY is a very address sensitive action, we do not
1862 * allow ftrace_ops to set all functions to new hash.
1863 */
1864 if (!new_hash || !old_hash)
1865 return -EINVAL;
1866
1867 /* Update rec->flags */
1868 do_for_each_ftrace_rec(pg, rec) {
1869 /* We need to update only differences of filter_hash */
1870 in_old = !!ftrace_lookup_ip(old_hash, rec->ip);
1871 in_new = !!ftrace_lookup_ip(new_hash, rec->ip);
1872 if (in_old == in_new)
1873 continue;
1874
1875 if (in_new) {
1876 /* New entries must ensure no others are using it */
1877 if (rec->flags & FTRACE_FL_IPMODIFY)
1878 goto rollback;
1879 rec->flags |= FTRACE_FL_IPMODIFY;
1880 } else /* Removed entry */
1881 rec->flags &= ~FTRACE_FL_IPMODIFY;
1882 } while_for_each_ftrace_rec();
1883
1884 return 0;
1885
1886 rollback:
1887 end = rec;
1888
1889 /* Roll back what we did above */
1890 do_for_each_ftrace_rec(pg, rec) {
1891 if (rec == end)
1892 goto err_out;
1893
1894 in_old = !!ftrace_lookup_ip(old_hash, rec->ip);
1895 in_new = !!ftrace_lookup_ip(new_hash, rec->ip);
1896 if (in_old == in_new)
1897 continue;
1898
1899 if (in_new)
1900 rec->flags &= ~FTRACE_FL_IPMODIFY;
1901 else
1902 rec->flags |= FTRACE_FL_IPMODIFY;
1903 } while_for_each_ftrace_rec();
1904
1905 err_out:
1906 return -EBUSY;
1907 }
1908
ftrace_hash_ipmodify_enable(struct ftrace_ops * ops)1909 static int ftrace_hash_ipmodify_enable(struct ftrace_ops *ops)
1910 {
1911 struct ftrace_hash *hash = ops->func_hash->filter_hash;
1912
1913 if (ftrace_hash_empty(hash))
1914 hash = NULL;
1915
1916 return __ftrace_hash_update_ipmodify(ops, EMPTY_HASH, hash);
1917 }
1918
1919 /* Disabling always succeeds */
ftrace_hash_ipmodify_disable(struct ftrace_ops * ops)1920 static void ftrace_hash_ipmodify_disable(struct ftrace_ops *ops)
1921 {
1922 struct ftrace_hash *hash = ops->func_hash->filter_hash;
1923
1924 if (ftrace_hash_empty(hash))
1925 hash = NULL;
1926
1927 __ftrace_hash_update_ipmodify(ops, hash, EMPTY_HASH);
1928 }
1929
ftrace_hash_ipmodify_update(struct ftrace_ops * ops,struct ftrace_hash * new_hash)1930 static int ftrace_hash_ipmodify_update(struct ftrace_ops *ops,
1931 struct ftrace_hash *new_hash)
1932 {
1933 struct ftrace_hash *old_hash = ops->func_hash->filter_hash;
1934
1935 if (ftrace_hash_empty(old_hash))
1936 old_hash = NULL;
1937
1938 if (ftrace_hash_empty(new_hash))
1939 new_hash = NULL;
1940
1941 return __ftrace_hash_update_ipmodify(ops, old_hash, new_hash);
1942 }
1943
print_ip_ins(const char * fmt,unsigned char * p)1944 static void print_ip_ins(const char *fmt, unsigned char *p)
1945 {
1946 char ins[MCOUNT_INSN_SIZE];
1947 int i;
1948
1949 if (probe_kernel_read(ins, p, MCOUNT_INSN_SIZE)) {
1950 printk(KERN_CONT "%s[FAULT] %px\n", fmt, p);
1951 return;
1952 }
1953
1954 printk(KERN_CONT "%s", fmt);
1955
1956 for (i = 0; i < MCOUNT_INSN_SIZE; i++)
1957 printk(KERN_CONT "%s%02x", i ? ":" : "", ins[i]);
1958 }
1959
1960 static struct ftrace_ops *
1961 ftrace_find_tramp_ops_any(struct dyn_ftrace *rec);
1962
1963 /**
1964 * ftrace_bug - report and shutdown function tracer
1965 * @failed: The failed type (EFAULT, EINVAL, EPERM)
1966 * @rec: The record that failed
1967 *
1968 * The arch code that enables or disables the function tracing
1969 * can call ftrace_bug() when it has detected a problem in
1970 * modifying the code. @failed should be one of either:
1971 * EFAULT - if the problem happens on reading the @ip address
1972 * EINVAL - if what is read at @ip is not what was expected
1973 * EPERM - if the problem happens on writting to the @ip address
1974 */
ftrace_bug(int failed,struct dyn_ftrace * rec)1975 void ftrace_bug(int failed, struct dyn_ftrace *rec)
1976 {
1977 unsigned long ip = rec ? rec->ip : 0;
1978
1979 switch (failed) {
1980 case -EFAULT:
1981 FTRACE_WARN_ON_ONCE(1);
1982 pr_info("ftrace faulted on modifying ");
1983 print_ip_sym(ip);
1984 break;
1985 case -EINVAL:
1986 FTRACE_WARN_ON_ONCE(1);
1987 pr_info("ftrace failed to modify ");
1988 print_ip_sym(ip);
1989 print_ip_ins(" actual: ", (unsigned char *)ip);
1990 pr_cont("\n");
1991 break;
1992 case -EPERM:
1993 FTRACE_WARN_ON_ONCE(1);
1994 pr_info("ftrace faulted on writing ");
1995 print_ip_sym(ip);
1996 break;
1997 default:
1998 FTRACE_WARN_ON_ONCE(1);
1999 pr_info("ftrace faulted on unknown error ");
2000 print_ip_sym(ip);
2001 }
2002 if (rec) {
2003 struct ftrace_ops *ops = NULL;
2004
2005 pr_info("ftrace record flags: %lx\n", rec->flags);
2006 pr_cont(" (%ld)%s", ftrace_rec_count(rec),
2007 rec->flags & FTRACE_FL_REGS ? " R" : " ");
2008 if (rec->flags & FTRACE_FL_TRAMP_EN) {
2009 ops = ftrace_find_tramp_ops_any(rec);
2010 if (ops)
2011 pr_cont("\ttramp: %pS",
2012 (void *)ops->trampoline);
2013 else
2014 pr_cont("\ttramp: ERROR!");
2015
2016 }
2017 ip = ftrace_get_addr_curr(rec);
2018 pr_cont(" expected tramp: %lx\n", ip);
2019 }
2020 }
2021
ftrace_check_record(struct dyn_ftrace * rec,int enable,int update)2022 static int ftrace_check_record(struct dyn_ftrace *rec, int enable, int update)
2023 {
2024 unsigned long flag = 0UL;
2025
2026 /*
2027 * If we are updating calls:
2028 *
2029 * If the record has a ref count, then we need to enable it
2030 * because someone is using it.
2031 *
2032 * Otherwise we make sure its disabled.
2033 *
2034 * If we are disabling calls, then disable all records that
2035 * are enabled.
2036 */
2037 if (enable && ftrace_rec_count(rec))
2038 flag = FTRACE_FL_ENABLED;
2039
2040 /*
2041 * If enabling and the REGS flag does not match the REGS_EN, or
2042 * the TRAMP flag doesn't match the TRAMP_EN, then do not ignore
2043 * this record. Set flags to fail the compare against ENABLED.
2044 */
2045 if (flag) {
2046 if (!(rec->flags & FTRACE_FL_REGS) !=
2047 !(rec->flags & FTRACE_FL_REGS_EN))
2048 flag |= FTRACE_FL_REGS;
2049
2050 if (!(rec->flags & FTRACE_FL_TRAMP) !=
2051 !(rec->flags & FTRACE_FL_TRAMP_EN))
2052 flag |= FTRACE_FL_TRAMP;
2053 }
2054
2055 /* If the state of this record hasn't changed, then do nothing */
2056 if ((rec->flags & FTRACE_FL_ENABLED) == flag)
2057 return FTRACE_UPDATE_IGNORE;
2058
2059 if (flag) {
2060 /* Save off if rec is being enabled (for return value) */
2061 flag ^= rec->flags & FTRACE_FL_ENABLED;
2062
2063 if (update) {
2064 rec->flags |= FTRACE_FL_ENABLED;
2065 if (flag & FTRACE_FL_REGS) {
2066 if (rec->flags & FTRACE_FL_REGS)
2067 rec->flags |= FTRACE_FL_REGS_EN;
2068 else
2069 rec->flags &= ~FTRACE_FL_REGS_EN;
2070 }
2071 if (flag & FTRACE_FL_TRAMP) {
2072 if (rec->flags & FTRACE_FL_TRAMP)
2073 rec->flags |= FTRACE_FL_TRAMP_EN;
2074 else
2075 rec->flags &= ~FTRACE_FL_TRAMP_EN;
2076 }
2077 }
2078
2079 /*
2080 * If this record is being updated from a nop, then
2081 * return UPDATE_MAKE_CALL.
2082 * Otherwise,
2083 * return UPDATE_MODIFY_CALL to tell the caller to convert
2084 * from the save regs, to a non-save regs function or
2085 * vice versa, or from a trampoline call.
2086 */
2087 if (flag & FTRACE_FL_ENABLED)
2088 return FTRACE_UPDATE_MAKE_CALL;
2089
2090 return FTRACE_UPDATE_MODIFY_CALL;
2091 }
2092
2093 if (update) {
2094 /* If there's no more users, clear all flags */
2095 if (!ftrace_rec_count(rec))
2096 rec->flags = 0;
2097 else
2098 /*
2099 * Just disable the record, but keep the ops TRAMP
2100 * and REGS states. The _EN flags must be disabled though.
2101 */
2102 rec->flags &= ~(FTRACE_FL_ENABLED | FTRACE_FL_TRAMP_EN |
2103 FTRACE_FL_REGS_EN);
2104 }
2105
2106 return FTRACE_UPDATE_MAKE_NOP;
2107 }
2108
2109 /**
2110 * ftrace_update_record, set a record that now is tracing or not
2111 * @rec: the record to update
2112 * @enable: set to 1 if the record is tracing, zero to force disable
2113 *
2114 * The records that represent all functions that can be traced need
2115 * to be updated when tracing has been enabled.
2116 */
ftrace_update_record(struct dyn_ftrace * rec,int enable)2117 int ftrace_update_record(struct dyn_ftrace *rec, int enable)
2118 {
2119 return ftrace_check_record(rec, enable, 1);
2120 }
2121
2122 /**
2123 * ftrace_test_record, check if the record has been enabled or not
2124 * @rec: the record to test
2125 * @enable: set to 1 to check if enabled, 0 if it is disabled
2126 *
2127 * The arch code may need to test if a record is already set to
2128 * tracing to determine how to modify the function code that it
2129 * represents.
2130 */
ftrace_test_record(struct dyn_ftrace * rec,int enable)2131 int ftrace_test_record(struct dyn_ftrace *rec, int enable)
2132 {
2133 return ftrace_check_record(rec, enable, 0);
2134 }
2135
2136 static struct ftrace_ops *
ftrace_find_tramp_ops_any(struct dyn_ftrace * rec)2137 ftrace_find_tramp_ops_any(struct dyn_ftrace *rec)
2138 {
2139 struct ftrace_ops *op;
2140 unsigned long ip = rec->ip;
2141
2142 do_for_each_ftrace_op(op, ftrace_ops_list) {
2143
2144 if (!op->trampoline)
2145 continue;
2146
2147 if (hash_contains_ip(ip, op->func_hash))
2148 return op;
2149 } while_for_each_ftrace_op(op);
2150
2151 return NULL;
2152 }
2153
2154 static struct ftrace_ops *
ftrace_find_tramp_ops_curr(struct dyn_ftrace * rec)2155 ftrace_find_tramp_ops_curr(struct dyn_ftrace *rec)
2156 {
2157 struct ftrace_ops *op;
2158 unsigned long ip = rec->ip;
2159
2160 /*
2161 * Need to check removed ops first.
2162 * If they are being removed, and this rec has a tramp,
2163 * and this rec is in the ops list, then it would be the
2164 * one with the tramp.
2165 */
2166 if (removed_ops) {
2167 if (hash_contains_ip(ip, &removed_ops->old_hash))
2168 return removed_ops;
2169 }
2170
2171 /*
2172 * Need to find the current trampoline for a rec.
2173 * Now, a trampoline is only attached to a rec if there
2174 * was a single 'ops' attached to it. But this can be called
2175 * when we are adding another op to the rec or removing the
2176 * current one. Thus, if the op is being added, we can
2177 * ignore it because it hasn't attached itself to the rec
2178 * yet.
2179 *
2180 * If an ops is being modified (hooking to different functions)
2181 * then we don't care about the new functions that are being
2182 * added, just the old ones (that are probably being removed).
2183 *
2184 * If we are adding an ops to a function that already is using
2185 * a trampoline, it needs to be removed (trampolines are only
2186 * for single ops connected), then an ops that is not being
2187 * modified also needs to be checked.
2188 */
2189 do_for_each_ftrace_op(op, ftrace_ops_list) {
2190
2191 if (!op->trampoline)
2192 continue;
2193
2194 /*
2195 * If the ops is being added, it hasn't gotten to
2196 * the point to be removed from this tree yet.
2197 */
2198 if (op->flags & FTRACE_OPS_FL_ADDING)
2199 continue;
2200
2201
2202 /*
2203 * If the ops is being modified and is in the old
2204 * hash, then it is probably being removed from this
2205 * function.
2206 */
2207 if ((op->flags & FTRACE_OPS_FL_MODIFYING) &&
2208 hash_contains_ip(ip, &op->old_hash))
2209 return op;
2210 /*
2211 * If the ops is not being added or modified, and it's
2212 * in its normal filter hash, then this must be the one
2213 * we want!
2214 */
2215 if (!(op->flags & FTRACE_OPS_FL_MODIFYING) &&
2216 hash_contains_ip(ip, op->func_hash))
2217 return op;
2218
2219 } while_for_each_ftrace_op(op);
2220
2221 return NULL;
2222 }
2223
2224 static struct ftrace_ops *
ftrace_find_tramp_ops_new(struct dyn_ftrace * rec)2225 ftrace_find_tramp_ops_new(struct dyn_ftrace *rec)
2226 {
2227 struct ftrace_ops *op;
2228 unsigned long ip = rec->ip;
2229
2230 do_for_each_ftrace_op(op, ftrace_ops_list) {
2231 /* pass rec in as regs to have non-NULL val */
2232 if (hash_contains_ip(ip, op->func_hash))
2233 return op;
2234 } while_for_each_ftrace_op(op);
2235
2236 return NULL;
2237 }
2238
2239 /**
2240 * ftrace_get_addr_new - Get the call address to set to
2241 * @rec: The ftrace record descriptor
2242 *
2243 * If the record has the FTRACE_FL_REGS set, that means that it
2244 * wants to convert to a callback that saves all regs. If FTRACE_FL_REGS
2245 * is not not set, then it wants to convert to the normal callback.
2246 *
2247 * Returns the address of the trampoline to set to
2248 */
ftrace_get_addr_new(struct dyn_ftrace * rec)2249 unsigned long ftrace_get_addr_new(struct dyn_ftrace *rec)
2250 {
2251 struct ftrace_ops *ops;
2252
2253 /* Trampolines take precedence over regs */
2254 if (rec->flags & FTRACE_FL_TRAMP) {
2255 ops = ftrace_find_tramp_ops_new(rec);
2256 if (FTRACE_WARN_ON(!ops || !ops->trampoline)) {
2257 pr_warn("Bad trampoline accounting at: %p (%pS) (%lx)\n",
2258 (void *)rec->ip, (void *)rec->ip, rec->flags);
2259 /* Ftrace is shutting down, return anything */
2260 return (unsigned long)FTRACE_ADDR;
2261 }
2262 return ops->trampoline;
2263 }
2264
2265 if (rec->flags & FTRACE_FL_REGS)
2266 return (unsigned long)FTRACE_REGS_ADDR;
2267 else
2268 return (unsigned long)FTRACE_ADDR;
2269 }
2270
2271 /**
2272 * ftrace_get_addr_curr - Get the call address that is already there
2273 * @rec: The ftrace record descriptor
2274 *
2275 * The FTRACE_FL_REGS_EN is set when the record already points to
2276 * a function that saves all the regs. Basically the '_EN' version
2277 * represents the current state of the function.
2278 *
2279 * Returns the address of the trampoline that is currently being called
2280 */
ftrace_get_addr_curr(struct dyn_ftrace * rec)2281 unsigned long ftrace_get_addr_curr(struct dyn_ftrace *rec)
2282 {
2283 struct ftrace_ops *ops;
2284
2285 /* Trampolines take precedence over regs */
2286 if (rec->flags & FTRACE_FL_TRAMP_EN) {
2287 ops = ftrace_find_tramp_ops_curr(rec);
2288 if (FTRACE_WARN_ON(!ops)) {
2289 pr_warning("Bad trampoline accounting at: %p (%pS)\n",
2290 (void *)rec->ip, (void *)rec->ip);
2291 /* Ftrace is shutting down, return anything */
2292 return (unsigned long)FTRACE_ADDR;
2293 }
2294 return ops->trampoline;
2295 }
2296
2297 if (rec->flags & FTRACE_FL_REGS_EN)
2298 return (unsigned long)FTRACE_REGS_ADDR;
2299 else
2300 return (unsigned long)FTRACE_ADDR;
2301 }
2302
2303 static int
__ftrace_replace_code(struct dyn_ftrace * rec,int enable)2304 __ftrace_replace_code(struct dyn_ftrace *rec, int enable)
2305 {
2306 unsigned long ftrace_old_addr;
2307 unsigned long ftrace_addr;
2308 int ret;
2309
2310 ftrace_addr = ftrace_get_addr_new(rec);
2311
2312 /* This needs to be done before we call ftrace_update_record */
2313 ftrace_old_addr = ftrace_get_addr_curr(rec);
2314
2315 ret = ftrace_update_record(rec, enable);
2316
2317 switch (ret) {
2318 case FTRACE_UPDATE_IGNORE:
2319 return 0;
2320
2321 case FTRACE_UPDATE_MAKE_CALL:
2322 return ftrace_make_call(rec, ftrace_addr);
2323
2324 case FTRACE_UPDATE_MAKE_NOP:
2325 return ftrace_make_nop(NULL, rec, ftrace_old_addr);
2326
2327 case FTRACE_UPDATE_MODIFY_CALL:
2328 return ftrace_modify_call(rec, ftrace_old_addr, ftrace_addr);
2329 }
2330
2331 return -1; /* unknow ftrace bug */
2332 }
2333
ftrace_replace_code(int enable)2334 void __weak ftrace_replace_code(int enable)
2335 {
2336 struct dyn_ftrace *rec;
2337 struct ftrace_page *pg;
2338 int failed;
2339
2340 if (unlikely(ftrace_disabled))
2341 return;
2342
2343 do_for_each_ftrace_rec(pg, rec) {
2344 failed = __ftrace_replace_code(rec, enable);
2345 if (failed) {
2346 ftrace_bug(failed, rec);
2347 /* Stop processing */
2348 return;
2349 }
2350 } while_for_each_ftrace_rec();
2351 }
2352
2353 struct ftrace_rec_iter {
2354 struct ftrace_page *pg;
2355 int index;
2356 };
2357
2358 /**
2359 * ftrace_rec_iter_start, start up iterating over traced functions
2360 *
2361 * Returns an iterator handle that is used to iterate over all
2362 * the records that represent address locations where functions
2363 * are traced.
2364 *
2365 * May return NULL if no records are available.
2366 */
ftrace_rec_iter_start(void)2367 struct ftrace_rec_iter *ftrace_rec_iter_start(void)
2368 {
2369 /*
2370 * We only use a single iterator.
2371 * Protected by the ftrace_lock mutex.
2372 */
2373 static struct ftrace_rec_iter ftrace_rec_iter;
2374 struct ftrace_rec_iter *iter = &ftrace_rec_iter;
2375
2376 iter->pg = ftrace_pages_start;
2377 iter->index = 0;
2378
2379 /* Could have empty pages */
2380 while (iter->pg && !iter->pg->index)
2381 iter->pg = iter->pg->next;
2382
2383 if (!iter->pg)
2384 return NULL;
2385
2386 return iter;
2387 }
2388
2389 /**
2390 * ftrace_rec_iter_next, get the next record to process.
2391 * @iter: The handle to the iterator.
2392 *
2393 * Returns the next iterator after the given iterator @iter.
2394 */
ftrace_rec_iter_next(struct ftrace_rec_iter * iter)2395 struct ftrace_rec_iter *ftrace_rec_iter_next(struct ftrace_rec_iter *iter)
2396 {
2397 iter->index++;
2398
2399 if (iter->index >= iter->pg->index) {
2400 iter->pg = iter->pg->next;
2401 iter->index = 0;
2402
2403 /* Could have empty pages */
2404 while (iter->pg && !iter->pg->index)
2405 iter->pg = iter->pg->next;
2406 }
2407
2408 if (!iter->pg)
2409 return NULL;
2410
2411 return iter;
2412 }
2413
2414 /**
2415 * ftrace_rec_iter_record, get the record at the iterator location
2416 * @iter: The current iterator location
2417 *
2418 * Returns the record that the current @iter is at.
2419 */
ftrace_rec_iter_record(struct ftrace_rec_iter * iter)2420 struct dyn_ftrace *ftrace_rec_iter_record(struct ftrace_rec_iter *iter)
2421 {
2422 return &iter->pg->records[iter->index];
2423 }
2424
2425 static int
ftrace_code_disable(struct module * mod,struct dyn_ftrace * rec)2426 ftrace_code_disable(struct module *mod, struct dyn_ftrace *rec)
2427 {
2428 int ret;
2429
2430 if (unlikely(ftrace_disabled))
2431 return 0;
2432
2433 ret = ftrace_make_nop(mod, rec, MCOUNT_ADDR);
2434 if (ret) {
2435 ftrace_bug(ret, rec);
2436 return 0;
2437 }
2438 return 1;
2439 }
2440
2441 /*
2442 * archs can override this function if they must do something
2443 * before the modifying code is performed.
2444 */
ftrace_arch_code_modify_prepare(void)2445 int __weak ftrace_arch_code_modify_prepare(void)
2446 {
2447 return 0;
2448 }
2449
2450 /*
2451 * archs can override this function if they must do something
2452 * after the modifying code is performed.
2453 */
ftrace_arch_code_modify_post_process(void)2454 int __weak ftrace_arch_code_modify_post_process(void)
2455 {
2456 return 0;
2457 }
2458
ftrace_modify_all_code(int command)2459 void ftrace_modify_all_code(int command)
2460 {
2461 int update = command & FTRACE_UPDATE_TRACE_FUNC;
2462 int err = 0;
2463
2464 /*
2465 * If the ftrace_caller calls a ftrace_ops func directly,
2466 * we need to make sure that it only traces functions it
2467 * expects to trace. When doing the switch of functions,
2468 * we need to update to the ftrace_ops_list_func first
2469 * before the transition between old and new calls are set,
2470 * as the ftrace_ops_list_func will check the ops hashes
2471 * to make sure the ops are having the right functions
2472 * traced.
2473 */
2474 if (update) {
2475 err = ftrace_update_ftrace_func(ftrace_ops_list_func);
2476 if (FTRACE_WARN_ON(err))
2477 return;
2478 }
2479
2480 if (command & FTRACE_UPDATE_CALLS)
2481 ftrace_replace_code(1);
2482 else if (command & FTRACE_DISABLE_CALLS)
2483 ftrace_replace_code(0);
2484
2485 if (update && ftrace_trace_function != ftrace_ops_list_func) {
2486 function_trace_op = set_function_trace_op;
2487 smp_wmb();
2488 /* If irqs are disabled, we are in stop machine */
2489 if (!irqs_disabled())
2490 smp_call_function(ftrace_sync_ipi, NULL, 1);
2491 err = ftrace_update_ftrace_func(ftrace_trace_function);
2492 if (FTRACE_WARN_ON(err))
2493 return;
2494 }
2495
2496 if (command & FTRACE_START_FUNC_RET)
2497 err = ftrace_enable_ftrace_graph_caller();
2498 else if (command & FTRACE_STOP_FUNC_RET)
2499 err = ftrace_disable_ftrace_graph_caller();
2500 FTRACE_WARN_ON(err);
2501 }
2502
__ftrace_modify_code(void * data)2503 static int __ftrace_modify_code(void *data)
2504 {
2505 int *command = data;
2506
2507 ftrace_modify_all_code(*command);
2508
2509 return 0;
2510 }
2511
2512 /**
2513 * ftrace_run_stop_machine, go back to the stop machine method
2514 * @command: The command to tell ftrace what to do
2515 *
2516 * If an arch needs to fall back to the stop machine method, the
2517 * it can call this function.
2518 */
ftrace_run_stop_machine(int command)2519 void ftrace_run_stop_machine(int command)
2520 {
2521 stop_machine(__ftrace_modify_code, &command, NULL);
2522 }
2523
2524 /**
2525 * arch_ftrace_update_code, modify the code to trace or not trace
2526 * @command: The command that needs to be done
2527 *
2528 * Archs can override this function if it does not need to
2529 * run stop_machine() to modify code.
2530 */
arch_ftrace_update_code(int command)2531 void __weak arch_ftrace_update_code(int command)
2532 {
2533 ftrace_run_stop_machine(command);
2534 }
2535
ftrace_run_update_code(int command)2536 static void ftrace_run_update_code(int command)
2537 {
2538 int ret;
2539
2540 ret = ftrace_arch_code_modify_prepare();
2541 FTRACE_WARN_ON(ret);
2542 if (ret)
2543 return;
2544
2545 /*
2546 * By default we use stop_machine() to modify the code.
2547 * But archs can do what ever they want as long as it
2548 * is safe. The stop_machine() is the safest, but also
2549 * produces the most overhead.
2550 */
2551 arch_ftrace_update_code(command);
2552
2553 ret = ftrace_arch_code_modify_post_process();
2554 FTRACE_WARN_ON(ret);
2555 }
2556
ftrace_run_modify_code(struct ftrace_ops * ops,int command,struct ftrace_ops_hash * old_hash)2557 static void ftrace_run_modify_code(struct ftrace_ops *ops, int command,
2558 struct ftrace_ops_hash *old_hash)
2559 {
2560 ops->flags |= FTRACE_OPS_FL_MODIFYING;
2561 ops->old_hash.filter_hash = old_hash->filter_hash;
2562 ops->old_hash.notrace_hash = old_hash->notrace_hash;
2563 ftrace_run_update_code(command);
2564 ops->old_hash.filter_hash = NULL;
2565 ops->old_hash.notrace_hash = NULL;
2566 ops->flags &= ~FTRACE_OPS_FL_MODIFYING;
2567 }
2568
2569 static ftrace_func_t saved_ftrace_func;
2570 static int ftrace_start_up;
2571
arch_ftrace_trampoline_free(struct ftrace_ops * ops)2572 void __weak arch_ftrace_trampoline_free(struct ftrace_ops *ops)
2573 {
2574 }
2575
control_ops_free(struct ftrace_ops * ops)2576 static void control_ops_free(struct ftrace_ops *ops)
2577 {
2578 free_percpu(ops->disabled);
2579 }
2580
ftrace_startup_enable(int command)2581 static void ftrace_startup_enable(int command)
2582 {
2583 if (saved_ftrace_func != ftrace_trace_function) {
2584 saved_ftrace_func = ftrace_trace_function;
2585 command |= FTRACE_UPDATE_TRACE_FUNC;
2586 }
2587
2588 if (!command || !ftrace_enabled)
2589 return;
2590
2591 ftrace_run_update_code(command);
2592 }
2593
ftrace_startup_all(int command)2594 static void ftrace_startup_all(int command)
2595 {
2596 update_all_ops = true;
2597 ftrace_startup_enable(command);
2598 update_all_ops = false;
2599 }
2600
ftrace_startup(struct ftrace_ops * ops,int command)2601 static int ftrace_startup(struct ftrace_ops *ops, int command)
2602 {
2603 int ret;
2604
2605 if (unlikely(ftrace_disabled))
2606 return -ENODEV;
2607
2608 ret = __register_ftrace_function(ops);
2609 if (ret)
2610 return ret;
2611
2612 ftrace_start_up++;
2613 command |= FTRACE_UPDATE_CALLS;
2614
2615 /*
2616 * Note that ftrace probes uses this to start up
2617 * and modify functions it will probe. But we still
2618 * set the ADDING flag for modification, as probes
2619 * do not have trampolines. If they add them in the
2620 * future, then the probes will need to distinguish
2621 * between adding and updating probes.
2622 */
2623 ops->flags |= FTRACE_OPS_FL_ENABLED | FTRACE_OPS_FL_ADDING;
2624
2625 ret = ftrace_hash_ipmodify_enable(ops);
2626 if (ret < 0) {
2627 /* Rollback registration process */
2628 __unregister_ftrace_function(ops);
2629 ftrace_start_up--;
2630 ops->flags &= ~FTRACE_OPS_FL_ENABLED;
2631 return ret;
2632 }
2633
2634 ftrace_hash_rec_enable(ops, 1);
2635
2636 ftrace_startup_enable(command);
2637
2638 ops->flags &= ~FTRACE_OPS_FL_ADDING;
2639
2640 return 0;
2641 }
2642
ftrace_shutdown(struct ftrace_ops * ops,int command)2643 static int ftrace_shutdown(struct ftrace_ops *ops, int command)
2644 {
2645 int ret;
2646
2647 if (unlikely(ftrace_disabled))
2648 return -ENODEV;
2649
2650 ret = __unregister_ftrace_function(ops);
2651 if (ret)
2652 return ret;
2653
2654 ftrace_start_up--;
2655 /*
2656 * Just warn in case of unbalance, no need to kill ftrace, it's not
2657 * critical but the ftrace_call callers may be never nopped again after
2658 * further ftrace uses.
2659 */
2660 WARN_ON_ONCE(ftrace_start_up < 0);
2661
2662 /* Disabling ipmodify never fails */
2663 ftrace_hash_ipmodify_disable(ops);
2664 ftrace_hash_rec_disable(ops, 1);
2665
2666 ops->flags &= ~FTRACE_OPS_FL_ENABLED;
2667
2668 command |= FTRACE_UPDATE_CALLS;
2669
2670 if (saved_ftrace_func != ftrace_trace_function) {
2671 saved_ftrace_func = ftrace_trace_function;
2672 command |= FTRACE_UPDATE_TRACE_FUNC;
2673 }
2674
2675 if (!command || !ftrace_enabled) {
2676 /*
2677 * If these are dynamic or control ops, they still
2678 * need their data freed. Since, function tracing is
2679 * not currently active, we can just free them
2680 * without synchronizing all CPUs.
2681 */
2682 if (ops->flags & (FTRACE_OPS_FL_DYNAMIC | FTRACE_OPS_FL_CONTROL))
2683 goto free_ops;
2684
2685 return 0;
2686 }
2687
2688 /*
2689 * If the ops uses a trampoline, then it needs to be
2690 * tested first on update.
2691 */
2692 ops->flags |= FTRACE_OPS_FL_REMOVING;
2693 removed_ops = ops;
2694
2695 /* The trampoline logic checks the old hashes */
2696 ops->old_hash.filter_hash = ops->func_hash->filter_hash;
2697 ops->old_hash.notrace_hash = ops->func_hash->notrace_hash;
2698
2699 ftrace_run_update_code(command);
2700
2701 /*
2702 * If there's no more ops registered with ftrace, run a
2703 * sanity check to make sure all rec flags are cleared.
2704 */
2705 if (ftrace_ops_list == &ftrace_list_end) {
2706 struct ftrace_page *pg;
2707 struct dyn_ftrace *rec;
2708
2709 do_for_each_ftrace_rec(pg, rec) {
2710 if (FTRACE_WARN_ON_ONCE(rec->flags))
2711 pr_warn(" %pS flags:%lx\n",
2712 (void *)rec->ip, rec->flags);
2713 } while_for_each_ftrace_rec();
2714 }
2715
2716 ops->old_hash.filter_hash = NULL;
2717 ops->old_hash.notrace_hash = NULL;
2718
2719 removed_ops = NULL;
2720 ops->flags &= ~FTRACE_OPS_FL_REMOVING;
2721
2722 /*
2723 * Dynamic ops may be freed, we must make sure that all
2724 * callers are done before leaving this function.
2725 * The same goes for freeing the per_cpu data of the control
2726 * ops.
2727 *
2728 * Again, normal synchronize_sched() is not good enough.
2729 * We need to do a hard force of sched synchronization.
2730 * This is because we use preempt_disable() to do RCU, but
2731 * the function tracers can be called where RCU is not watching
2732 * (like before user_exit()). We can not rely on the RCU
2733 * infrastructure to do the synchronization, thus we must do it
2734 * ourselves.
2735 */
2736 if (ops->flags & (FTRACE_OPS_FL_DYNAMIC | FTRACE_OPS_FL_CONTROL)) {
2737 schedule_on_each_cpu(ftrace_sync);
2738
2739 free_ops:
2740 arch_ftrace_trampoline_free(ops);
2741
2742 if (ops->flags & FTRACE_OPS_FL_CONTROL)
2743 control_ops_free(ops);
2744 }
2745
2746 return 0;
2747 }
2748
ftrace_startup_sysctl(void)2749 static void ftrace_startup_sysctl(void)
2750 {
2751 int command;
2752
2753 if (unlikely(ftrace_disabled))
2754 return;
2755
2756 /* Force update next time */
2757 saved_ftrace_func = NULL;
2758 /* ftrace_start_up is true if we want ftrace running */
2759 if (ftrace_start_up) {
2760 command = FTRACE_UPDATE_CALLS;
2761 if (ftrace_graph_active)
2762 command |= FTRACE_START_FUNC_RET;
2763 ftrace_startup_enable(command);
2764 }
2765 }
2766
ftrace_shutdown_sysctl(void)2767 static void ftrace_shutdown_sysctl(void)
2768 {
2769 int command;
2770
2771 if (unlikely(ftrace_disabled))
2772 return;
2773
2774 /* ftrace_start_up is true if ftrace is running */
2775 if (ftrace_start_up) {
2776 command = FTRACE_DISABLE_CALLS;
2777 if (ftrace_graph_active)
2778 command |= FTRACE_STOP_FUNC_RET;
2779 ftrace_run_update_code(command);
2780 }
2781 }
2782
2783 static cycle_t ftrace_update_time;
2784 unsigned long ftrace_update_tot_cnt;
2785
ops_traces_mod(struct ftrace_ops * ops)2786 static inline int ops_traces_mod(struct ftrace_ops *ops)
2787 {
2788 /*
2789 * Filter_hash being empty will default to trace module.
2790 * But notrace hash requires a test of individual module functions.
2791 */
2792 return ftrace_hash_empty(ops->func_hash->filter_hash) &&
2793 ftrace_hash_empty(ops->func_hash->notrace_hash);
2794 }
2795
2796 /*
2797 * Check if the current ops references the record.
2798 *
2799 * If the ops traces all functions, then it was already accounted for.
2800 * If the ops does not trace the current record function, skip it.
2801 * If the ops ignores the function via notrace filter, skip it.
2802 */
2803 static inline bool
ops_references_rec(struct ftrace_ops * ops,struct dyn_ftrace * rec)2804 ops_references_rec(struct ftrace_ops *ops, struct dyn_ftrace *rec)
2805 {
2806 /* If ops isn't enabled, ignore it */
2807 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
2808 return 0;
2809
2810 /* If ops traces all mods, we already accounted for it */
2811 if (ops_traces_mod(ops))
2812 return 0;
2813
2814 /* The function must be in the filter */
2815 if (!ftrace_hash_empty(ops->func_hash->filter_hash) &&
2816 !ftrace_lookup_ip(ops->func_hash->filter_hash, rec->ip))
2817 return 0;
2818
2819 /* If in notrace hash, we ignore it too */
2820 if (ftrace_lookup_ip(ops->func_hash->notrace_hash, rec->ip))
2821 return 0;
2822
2823 return 1;
2824 }
2825
referenced_filters(struct dyn_ftrace * rec)2826 static int referenced_filters(struct dyn_ftrace *rec)
2827 {
2828 struct ftrace_ops *ops;
2829 int cnt = 0;
2830
2831 for (ops = ftrace_ops_list; ops != &ftrace_list_end; ops = ops->next) {
2832 if (ops_references_rec(ops, rec)) {
2833 cnt++;
2834 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS)
2835 rec->flags |= FTRACE_FL_REGS;
2836 }
2837 }
2838
2839 return cnt;
2840 }
2841
ftrace_update_code(struct module * mod,struct ftrace_page * new_pgs)2842 static int ftrace_update_code(struct module *mod, struct ftrace_page *new_pgs)
2843 {
2844 struct ftrace_page *pg;
2845 struct dyn_ftrace *p;
2846 cycle_t start, stop;
2847 unsigned long update_cnt = 0;
2848 unsigned long ref = 0;
2849 bool test = false;
2850 int i;
2851
2852 /*
2853 * When adding a module, we need to check if tracers are
2854 * currently enabled and if they are set to trace all functions.
2855 * If they are, we need to enable the module functions as well
2856 * as update the reference counts for those function records.
2857 */
2858 if (mod) {
2859 struct ftrace_ops *ops;
2860
2861 for (ops = ftrace_ops_list;
2862 ops != &ftrace_list_end; ops = ops->next) {
2863 if (ops->flags & FTRACE_OPS_FL_ENABLED) {
2864 if (ops_traces_mod(ops))
2865 ref++;
2866 else
2867 test = true;
2868 }
2869 }
2870 }
2871
2872 start = ftrace_now(raw_smp_processor_id());
2873
2874 for (pg = new_pgs; pg; pg = pg->next) {
2875
2876 for (i = 0; i < pg->index; i++) {
2877 int cnt = ref;
2878
2879 /* If something went wrong, bail without enabling anything */
2880 if (unlikely(ftrace_disabled))
2881 return -1;
2882
2883 p = &pg->records[i];
2884 if (test)
2885 cnt += referenced_filters(p);
2886 p->flags += cnt;
2887
2888 /*
2889 * Do the initial record conversion from mcount jump
2890 * to the NOP instructions.
2891 */
2892 if (!ftrace_code_disable(mod, p))
2893 break;
2894
2895 update_cnt++;
2896
2897 /*
2898 * If the tracing is enabled, go ahead and enable the record.
2899 *
2900 * The reason not to enable the record immediatelly is the
2901 * inherent check of ftrace_make_nop/ftrace_make_call for
2902 * correct previous instructions. Making first the NOP
2903 * conversion puts the module to the correct state, thus
2904 * passing the ftrace_make_call check.
2905 */
2906 if (ftrace_start_up && cnt) {
2907 int failed = __ftrace_replace_code(p, 1);
2908 if (failed)
2909 ftrace_bug(failed, p);
2910 }
2911 }
2912 }
2913
2914 stop = ftrace_now(raw_smp_processor_id());
2915 ftrace_update_time = stop - start;
2916 ftrace_update_tot_cnt += update_cnt;
2917
2918 return 0;
2919 }
2920
ftrace_allocate_records(struct ftrace_page * pg,int count)2921 static int ftrace_allocate_records(struct ftrace_page *pg, int count)
2922 {
2923 int order;
2924 int cnt;
2925
2926 if (WARN_ON(!count))
2927 return -EINVAL;
2928
2929 order = get_count_order(DIV_ROUND_UP(count, ENTRIES_PER_PAGE));
2930
2931 /*
2932 * We want to fill as much as possible. No more than a page
2933 * may be empty.
2934 */
2935 while ((PAGE_SIZE << order) / ENTRY_SIZE >= count + ENTRIES_PER_PAGE)
2936 order--;
2937
2938 again:
2939 pg->records = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, order);
2940
2941 if (!pg->records) {
2942 /* if we can't allocate this size, try something smaller */
2943 if (!order)
2944 return -ENOMEM;
2945 order >>= 1;
2946 goto again;
2947 }
2948
2949 cnt = (PAGE_SIZE << order) / ENTRY_SIZE;
2950 pg->size = cnt;
2951
2952 if (cnt > count)
2953 cnt = count;
2954
2955 return cnt;
2956 }
2957
2958 static struct ftrace_page *
ftrace_allocate_pages(unsigned long num_to_init)2959 ftrace_allocate_pages(unsigned long num_to_init)
2960 {
2961 struct ftrace_page *start_pg;
2962 struct ftrace_page *pg;
2963 int order;
2964 int cnt;
2965
2966 if (!num_to_init)
2967 return 0;
2968
2969 start_pg = pg = kzalloc(sizeof(*pg), GFP_KERNEL);
2970 if (!pg)
2971 return NULL;
2972
2973 /*
2974 * Try to allocate as much as possible in one continues
2975 * location that fills in all of the space. We want to
2976 * waste as little space as possible.
2977 */
2978 for (;;) {
2979 cnt = ftrace_allocate_records(pg, num_to_init);
2980 if (cnt < 0)
2981 goto free_pages;
2982
2983 num_to_init -= cnt;
2984 if (!num_to_init)
2985 break;
2986
2987 pg->next = kzalloc(sizeof(*pg), GFP_KERNEL);
2988 if (!pg->next)
2989 goto free_pages;
2990
2991 pg = pg->next;
2992 }
2993
2994 return start_pg;
2995
2996 free_pages:
2997 pg = start_pg;
2998 while (pg) {
2999 order = get_count_order(pg->size / ENTRIES_PER_PAGE);
3000 free_pages((unsigned long)pg->records, order);
3001 start_pg = pg->next;
3002 kfree(pg);
3003 pg = start_pg;
3004 }
3005 pr_info("ftrace: FAILED to allocate memory for functions\n");
3006 return NULL;
3007 }
3008
3009 #define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
3010
3011 struct ftrace_iterator {
3012 loff_t pos;
3013 loff_t func_pos;
3014 struct ftrace_page *pg;
3015 struct dyn_ftrace *func;
3016 struct ftrace_func_probe *probe;
3017 struct trace_parser parser;
3018 struct ftrace_hash *hash;
3019 struct ftrace_ops *ops;
3020 int hidx;
3021 int idx;
3022 unsigned flags;
3023 };
3024
3025 static void *
t_hash_next(struct seq_file * m,loff_t * pos)3026 t_hash_next(struct seq_file *m, loff_t *pos)
3027 {
3028 struct ftrace_iterator *iter = m->private;
3029 struct hlist_node *hnd = NULL;
3030 struct hlist_head *hhd;
3031
3032 (*pos)++;
3033 iter->pos = *pos;
3034
3035 if (iter->probe)
3036 hnd = &iter->probe->node;
3037 retry:
3038 if (iter->hidx >= FTRACE_FUNC_HASHSIZE)
3039 return NULL;
3040
3041 hhd = &ftrace_func_hash[iter->hidx];
3042
3043 if (hlist_empty(hhd)) {
3044 iter->hidx++;
3045 hnd = NULL;
3046 goto retry;
3047 }
3048
3049 if (!hnd)
3050 hnd = hhd->first;
3051 else {
3052 hnd = hnd->next;
3053 if (!hnd) {
3054 iter->hidx++;
3055 goto retry;
3056 }
3057 }
3058
3059 if (WARN_ON_ONCE(!hnd))
3060 return NULL;
3061
3062 iter->probe = hlist_entry(hnd, struct ftrace_func_probe, node);
3063
3064 return iter;
3065 }
3066
t_hash_start(struct seq_file * m,loff_t * pos)3067 static void *t_hash_start(struct seq_file *m, loff_t *pos)
3068 {
3069 struct ftrace_iterator *iter = m->private;
3070 void *p = NULL;
3071 loff_t l;
3072
3073 if (!(iter->flags & FTRACE_ITER_DO_HASH))
3074 return NULL;
3075
3076 if (iter->func_pos > *pos)
3077 return NULL;
3078
3079 iter->hidx = 0;
3080 for (l = 0; l <= (*pos - iter->func_pos); ) {
3081 p = t_hash_next(m, &l);
3082 if (!p)
3083 break;
3084 }
3085 if (!p)
3086 return NULL;
3087
3088 /* Only set this if we have an item */
3089 iter->flags |= FTRACE_ITER_HASH;
3090
3091 return iter;
3092 }
3093
3094 static int
t_hash_show(struct seq_file * m,struct ftrace_iterator * iter)3095 t_hash_show(struct seq_file *m, struct ftrace_iterator *iter)
3096 {
3097 struct ftrace_func_probe *rec;
3098
3099 rec = iter->probe;
3100 if (WARN_ON_ONCE(!rec))
3101 return -EIO;
3102
3103 if (rec->ops->print)
3104 return rec->ops->print(m, rec->ip, rec->ops, rec->data);
3105
3106 seq_printf(m, "%ps:%ps", (void *)rec->ip, (void *)rec->ops->func);
3107
3108 if (rec->data)
3109 seq_printf(m, ":%p", rec->data);
3110 seq_putc(m, '\n');
3111
3112 return 0;
3113 }
3114
3115 static void *
t_next(struct seq_file * m,void * v,loff_t * pos)3116 t_next(struct seq_file *m, void *v, loff_t *pos)
3117 {
3118 struct ftrace_iterator *iter = m->private;
3119 struct ftrace_ops *ops = iter->ops;
3120 struct dyn_ftrace *rec = NULL;
3121
3122 if (unlikely(ftrace_disabled))
3123 return NULL;
3124
3125 if (iter->flags & FTRACE_ITER_HASH)
3126 return t_hash_next(m, pos);
3127
3128 (*pos)++;
3129 iter->pos = iter->func_pos = *pos;
3130
3131 if (iter->flags & FTRACE_ITER_PRINTALL)
3132 return t_hash_start(m, pos);
3133
3134 retry:
3135 if (iter->idx >= iter->pg->index) {
3136 if (iter->pg->next) {
3137 iter->pg = iter->pg->next;
3138 iter->idx = 0;
3139 goto retry;
3140 }
3141 } else {
3142 rec = &iter->pg->records[iter->idx++];
3143 if (((iter->flags & FTRACE_ITER_FILTER) &&
3144 !(ftrace_lookup_ip(ops->func_hash->filter_hash, rec->ip))) ||
3145
3146 ((iter->flags & FTRACE_ITER_NOTRACE) &&
3147 !ftrace_lookup_ip(ops->func_hash->notrace_hash, rec->ip)) ||
3148
3149 ((iter->flags & FTRACE_ITER_ENABLED) &&
3150 !(rec->flags & FTRACE_FL_ENABLED))) {
3151
3152 rec = NULL;
3153 goto retry;
3154 }
3155 }
3156
3157 if (!rec)
3158 return t_hash_start(m, pos);
3159
3160 iter->func = rec;
3161
3162 return iter;
3163 }
3164
reset_iter_read(struct ftrace_iterator * iter)3165 static void reset_iter_read(struct ftrace_iterator *iter)
3166 {
3167 iter->pos = 0;
3168 iter->func_pos = 0;
3169 iter->flags &= ~(FTRACE_ITER_PRINTALL | FTRACE_ITER_HASH);
3170 }
3171
t_start(struct seq_file * m,loff_t * pos)3172 static void *t_start(struct seq_file *m, loff_t *pos)
3173 {
3174 struct ftrace_iterator *iter = m->private;
3175 struct ftrace_ops *ops = iter->ops;
3176 void *p = NULL;
3177 loff_t l;
3178
3179 mutex_lock(&ftrace_lock);
3180
3181 if (unlikely(ftrace_disabled))
3182 return NULL;
3183
3184 /*
3185 * If an lseek was done, then reset and start from beginning.
3186 */
3187 if (*pos < iter->pos)
3188 reset_iter_read(iter);
3189
3190 /*
3191 * For set_ftrace_filter reading, if we have the filter
3192 * off, we can short cut and just print out that all
3193 * functions are enabled.
3194 */
3195 if ((iter->flags & FTRACE_ITER_FILTER &&
3196 ftrace_hash_empty(ops->func_hash->filter_hash)) ||
3197 (iter->flags & FTRACE_ITER_NOTRACE &&
3198 ftrace_hash_empty(ops->func_hash->notrace_hash))) {
3199 if (*pos > 0)
3200 return t_hash_start(m, pos);
3201 iter->flags |= FTRACE_ITER_PRINTALL;
3202 /* reset in case of seek/pread */
3203 iter->flags &= ~FTRACE_ITER_HASH;
3204 return iter;
3205 }
3206
3207 if (iter->flags & FTRACE_ITER_HASH)
3208 return t_hash_start(m, pos);
3209
3210 /*
3211 * Unfortunately, we need to restart at ftrace_pages_start
3212 * every time we let go of the ftrace_mutex. This is because
3213 * those pointers can change without the lock.
3214 */
3215 iter->pg = ftrace_pages_start;
3216 iter->idx = 0;
3217 for (l = 0; l <= *pos; ) {
3218 p = t_next(m, p, &l);
3219 if (!p)
3220 break;
3221 }
3222
3223 if (!p)
3224 return t_hash_start(m, pos);
3225
3226 return iter;
3227 }
3228
t_stop(struct seq_file * m,void * p)3229 static void t_stop(struct seq_file *m, void *p)
3230 {
3231 mutex_unlock(&ftrace_lock);
3232 }
3233
3234 void * __weak
arch_ftrace_trampoline_func(struct ftrace_ops * ops,struct dyn_ftrace * rec)3235 arch_ftrace_trampoline_func(struct ftrace_ops *ops, struct dyn_ftrace *rec)
3236 {
3237 return NULL;
3238 }
3239
add_trampoline_func(struct seq_file * m,struct ftrace_ops * ops,struct dyn_ftrace * rec)3240 static void add_trampoline_func(struct seq_file *m, struct ftrace_ops *ops,
3241 struct dyn_ftrace *rec)
3242 {
3243 void *ptr;
3244
3245 ptr = arch_ftrace_trampoline_func(ops, rec);
3246 if (ptr)
3247 seq_printf(m, " ->%pS", ptr);
3248 }
3249
t_show(struct seq_file * m,void * v)3250 static int t_show(struct seq_file *m, void *v)
3251 {
3252 struct ftrace_iterator *iter = m->private;
3253 struct dyn_ftrace *rec;
3254
3255 if (iter->flags & FTRACE_ITER_HASH)
3256 return t_hash_show(m, iter);
3257
3258 if (iter->flags & FTRACE_ITER_PRINTALL) {
3259 if (iter->flags & FTRACE_ITER_NOTRACE)
3260 seq_puts(m, "#### no functions disabled ####\n");
3261 else
3262 seq_puts(m, "#### all functions enabled ####\n");
3263 return 0;
3264 }
3265
3266 rec = iter->func;
3267
3268 if (!rec)
3269 return 0;
3270
3271 seq_printf(m, "%ps", (void *)rec->ip);
3272 if (iter->flags & FTRACE_ITER_ENABLED) {
3273 struct ftrace_ops *ops = NULL;
3274
3275 seq_printf(m, " (%ld)%s%s",
3276 ftrace_rec_count(rec),
3277 rec->flags & FTRACE_FL_REGS ? " R" : " ",
3278 rec->flags & FTRACE_FL_IPMODIFY ? " I" : " ");
3279 if (rec->flags & FTRACE_FL_TRAMP_EN) {
3280 ops = ftrace_find_tramp_ops_any(rec);
3281 if (ops)
3282 seq_printf(m, "\ttramp: %pS",
3283 (void *)ops->trampoline);
3284 else
3285 seq_puts(m, "\ttramp: ERROR!");
3286
3287 }
3288 add_trampoline_func(m, ops, rec);
3289 }
3290
3291 seq_putc(m, '\n');
3292
3293 return 0;
3294 }
3295
3296 static const struct seq_operations show_ftrace_seq_ops = {
3297 .start = t_start,
3298 .next = t_next,
3299 .stop = t_stop,
3300 .show = t_show,
3301 };
3302
3303 static int
ftrace_avail_open(struct inode * inode,struct file * file)3304 ftrace_avail_open(struct inode *inode, struct file *file)
3305 {
3306 struct ftrace_iterator *iter;
3307
3308 if (unlikely(ftrace_disabled))
3309 return -ENODEV;
3310
3311 iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter));
3312 if (iter) {
3313 iter->pg = ftrace_pages_start;
3314 iter->ops = &global_ops;
3315 }
3316
3317 return iter ? 0 : -ENOMEM;
3318 }
3319
3320 static int
ftrace_enabled_open(struct inode * inode,struct file * file)3321 ftrace_enabled_open(struct inode *inode, struct file *file)
3322 {
3323 struct ftrace_iterator *iter;
3324
3325 iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter));
3326 if (iter) {
3327 iter->pg = ftrace_pages_start;
3328 iter->flags = FTRACE_ITER_ENABLED;
3329 iter->ops = &global_ops;
3330 }
3331
3332 return iter ? 0 : -ENOMEM;
3333 }
3334
3335 /**
3336 * ftrace_regex_open - initialize function tracer filter files
3337 * @ops: The ftrace_ops that hold the hash filters
3338 * @flag: The type of filter to process
3339 * @inode: The inode, usually passed in to your open routine
3340 * @file: The file, usually passed in to your open routine
3341 *
3342 * ftrace_regex_open() initializes the filter files for the
3343 * @ops. Depending on @flag it may process the filter hash or
3344 * the notrace hash of @ops. With this called from the open
3345 * routine, you can use ftrace_filter_write() for the write
3346 * routine if @flag has FTRACE_ITER_FILTER set, or
3347 * ftrace_notrace_write() if @flag has FTRACE_ITER_NOTRACE set.
3348 * tracing_lseek() should be used as the lseek routine, and
3349 * release must call ftrace_regex_release().
3350 */
3351 int
ftrace_regex_open(struct ftrace_ops * ops,int flag,struct inode * inode,struct file * file)3352 ftrace_regex_open(struct ftrace_ops *ops, int flag,
3353 struct inode *inode, struct file *file)
3354 {
3355 struct ftrace_iterator *iter;
3356 struct ftrace_hash *hash;
3357 int ret = 0;
3358
3359 ftrace_ops_init(ops);
3360
3361 if (unlikely(ftrace_disabled))
3362 return -ENODEV;
3363
3364 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
3365 if (!iter)
3366 return -ENOMEM;
3367
3368 if (trace_parser_get_init(&iter->parser, FTRACE_BUFF_MAX)) {
3369 kfree(iter);
3370 return -ENOMEM;
3371 }
3372
3373 iter->ops = ops;
3374 iter->flags = flag;
3375
3376 mutex_lock(&ops->func_hash->regex_lock);
3377
3378 if (flag & FTRACE_ITER_NOTRACE)
3379 hash = ops->func_hash->notrace_hash;
3380 else
3381 hash = ops->func_hash->filter_hash;
3382
3383 if (file->f_mode & FMODE_WRITE) {
3384 const int size_bits = FTRACE_HASH_DEFAULT_BITS;
3385
3386 if (file->f_flags & O_TRUNC)
3387 iter->hash = alloc_ftrace_hash(size_bits);
3388 else
3389 iter->hash = alloc_and_copy_ftrace_hash(size_bits, hash);
3390
3391 if (!iter->hash) {
3392 trace_parser_put(&iter->parser);
3393 kfree(iter);
3394 ret = -ENOMEM;
3395 goto out_unlock;
3396 }
3397 }
3398
3399 if (file->f_mode & FMODE_READ) {
3400 iter->pg = ftrace_pages_start;
3401
3402 ret = seq_open(file, &show_ftrace_seq_ops);
3403 if (!ret) {
3404 struct seq_file *m = file->private_data;
3405 m->private = iter;
3406 } else {
3407 /* Failed */
3408 free_ftrace_hash(iter->hash);
3409 trace_parser_put(&iter->parser);
3410 kfree(iter);
3411 }
3412 } else
3413 file->private_data = iter;
3414
3415 out_unlock:
3416 mutex_unlock(&ops->func_hash->regex_lock);
3417
3418 return ret;
3419 }
3420
3421 static int
ftrace_filter_open(struct inode * inode,struct file * file)3422 ftrace_filter_open(struct inode *inode, struct file *file)
3423 {
3424 struct ftrace_ops *ops = inode->i_private;
3425
3426 return ftrace_regex_open(ops,
3427 FTRACE_ITER_FILTER | FTRACE_ITER_DO_HASH,
3428 inode, file);
3429 }
3430
3431 static int
ftrace_notrace_open(struct inode * inode,struct file * file)3432 ftrace_notrace_open(struct inode *inode, struct file *file)
3433 {
3434 struct ftrace_ops *ops = inode->i_private;
3435
3436 return ftrace_regex_open(ops, FTRACE_ITER_NOTRACE,
3437 inode, file);
3438 }
3439
3440 /* Type for quick search ftrace basic regexes (globs) from filter_parse_regex */
3441 struct ftrace_glob {
3442 char *search;
3443 unsigned len;
3444 int type;
3445 };
3446
ftrace_match(char * str,struct ftrace_glob * g)3447 static int ftrace_match(char *str, struct ftrace_glob *g)
3448 {
3449 int matched = 0;
3450 int slen;
3451
3452 switch (g->type) {
3453 case MATCH_FULL:
3454 if (strcmp(str, g->search) == 0)
3455 matched = 1;
3456 break;
3457 case MATCH_FRONT_ONLY:
3458 if (strncmp(str, g->search, g->len) == 0)
3459 matched = 1;
3460 break;
3461 case MATCH_MIDDLE_ONLY:
3462 if (strstr(str, g->search))
3463 matched = 1;
3464 break;
3465 case MATCH_END_ONLY:
3466 slen = strlen(str);
3467 if (slen >= g->len &&
3468 memcmp(str + slen - g->len, g->search, g->len) == 0)
3469 matched = 1;
3470 break;
3471 }
3472
3473 return matched;
3474 }
3475
3476 static int
enter_record(struct ftrace_hash * hash,struct dyn_ftrace * rec,int clear_filter)3477 enter_record(struct ftrace_hash *hash, struct dyn_ftrace *rec, int clear_filter)
3478 {
3479 struct ftrace_func_entry *entry;
3480 int ret = 0;
3481
3482 entry = ftrace_lookup_ip(hash, rec->ip);
3483 if (clear_filter) {
3484 /* Do nothing if it doesn't exist */
3485 if (!entry)
3486 return 0;
3487
3488 free_hash_entry(hash, entry);
3489 } else {
3490 /* Do nothing if it exists */
3491 if (entry)
3492 return 0;
3493
3494 ret = add_hash_entry(hash, rec->ip);
3495 }
3496 return ret;
3497 }
3498
3499 static int
ftrace_match_record(struct dyn_ftrace * rec,struct ftrace_glob * func_g,struct ftrace_glob * mod_g,int exclude_mod)3500 ftrace_match_record(struct dyn_ftrace *rec, struct ftrace_glob *func_g,
3501 struct ftrace_glob *mod_g, int exclude_mod)
3502 {
3503 char str[KSYM_SYMBOL_LEN];
3504 char *modname;
3505
3506 kallsyms_lookup(rec->ip, NULL, NULL, &modname, str);
3507
3508 if (mod_g) {
3509 int mod_matches = (modname) ? ftrace_match(modname, mod_g) : 0;
3510
3511 /* blank module name to match all modules */
3512 if (!mod_g->len) {
3513 /* blank module globbing: modname xor exclude_mod */
3514 if ((!exclude_mod) != (!modname))
3515 goto func_match;
3516 return 0;
3517 }
3518
3519 /* not matching the module */
3520 if (!modname || !mod_matches) {
3521 if (exclude_mod)
3522 goto func_match;
3523 else
3524 return 0;
3525 }
3526
3527 if (mod_matches && exclude_mod)
3528 return 0;
3529
3530 func_match:
3531 /* blank search means to match all funcs in the mod */
3532 if (!func_g->len)
3533 return 1;
3534 }
3535
3536 return ftrace_match(str, func_g);
3537 }
3538
3539 static int
match_records(struct ftrace_hash * hash,char * func,int len,char * mod)3540 match_records(struct ftrace_hash *hash, char *func, int len, char *mod)
3541 {
3542 struct ftrace_page *pg;
3543 struct dyn_ftrace *rec;
3544 struct ftrace_glob func_g = { .type = MATCH_FULL };
3545 struct ftrace_glob mod_g = { .type = MATCH_FULL };
3546 struct ftrace_glob *mod_match = (mod) ? &mod_g : NULL;
3547 int exclude_mod = 0;
3548 int found = 0;
3549 int ret;
3550 int clear_filter = 0;
3551
3552 if (func) {
3553 func_g.type = filter_parse_regex(func, len, &func_g.search,
3554 &clear_filter);
3555 func_g.len = strlen(func_g.search);
3556 }
3557
3558 if (mod) {
3559 mod_g.type = filter_parse_regex(mod, strlen(mod),
3560 &mod_g.search, &exclude_mod);
3561 mod_g.len = strlen(mod_g.search);
3562 }
3563
3564 mutex_lock(&ftrace_lock);
3565
3566 if (unlikely(ftrace_disabled))
3567 goto out_unlock;
3568
3569 do_for_each_ftrace_rec(pg, rec) {
3570 if (ftrace_match_record(rec, &func_g, mod_match, exclude_mod)) {
3571 ret = enter_record(hash, rec, clear_filter);
3572 if (ret < 0) {
3573 found = ret;
3574 goto out_unlock;
3575 }
3576 found = 1;
3577 }
3578 } while_for_each_ftrace_rec();
3579 out_unlock:
3580 mutex_unlock(&ftrace_lock);
3581
3582 return found;
3583 }
3584
3585 static int
ftrace_match_records(struct ftrace_hash * hash,char * buff,int len)3586 ftrace_match_records(struct ftrace_hash *hash, char *buff, int len)
3587 {
3588 return match_records(hash, buff, len, NULL);
3589 }
3590
3591
3592 /*
3593 * We register the module command as a template to show others how
3594 * to register the a command as well.
3595 */
3596
3597 static int
ftrace_mod_callback(struct ftrace_hash * hash,char * func,char * cmd,char * module,int enable)3598 ftrace_mod_callback(struct ftrace_hash *hash,
3599 char *func, char *cmd, char *module, int enable)
3600 {
3601 int ret;
3602
3603 /*
3604 * cmd == 'mod' because we only registered this func
3605 * for the 'mod' ftrace_func_command.
3606 * But if you register one func with multiple commands,
3607 * you can tell which command was used by the cmd
3608 * parameter.
3609 */
3610 ret = match_records(hash, func, strlen(func), module);
3611 if (!ret)
3612 return -EINVAL;
3613 if (ret < 0)
3614 return ret;
3615 return 0;
3616 }
3617
3618 static struct ftrace_func_command ftrace_mod_cmd = {
3619 .name = "mod",
3620 .func = ftrace_mod_callback,
3621 };
3622
ftrace_mod_cmd_init(void)3623 static int __init ftrace_mod_cmd_init(void)
3624 {
3625 return register_ftrace_command(&ftrace_mod_cmd);
3626 }
3627 core_initcall(ftrace_mod_cmd_init);
3628
function_trace_probe_call(unsigned long ip,unsigned long parent_ip,struct ftrace_ops * op,struct pt_regs * pt_regs)3629 static void function_trace_probe_call(unsigned long ip, unsigned long parent_ip,
3630 struct ftrace_ops *op, struct pt_regs *pt_regs)
3631 {
3632 struct ftrace_func_probe *entry;
3633 struct hlist_head *hhd;
3634 unsigned long key;
3635
3636 key = hash_long(ip, FTRACE_HASH_BITS);
3637
3638 hhd = &ftrace_func_hash[key];
3639
3640 if (hlist_empty(hhd))
3641 return;
3642
3643 /*
3644 * Disable preemption for these calls to prevent a RCU grace
3645 * period. This syncs the hash iteration and freeing of items
3646 * on the hash. rcu_read_lock is too dangerous here.
3647 */
3648 preempt_disable_notrace();
3649 hlist_for_each_entry_rcu_notrace(entry, hhd, node) {
3650 if (entry->ip == ip)
3651 entry->ops->func(ip, parent_ip, &entry->data);
3652 }
3653 preempt_enable_notrace();
3654 }
3655
3656 static struct ftrace_ops trace_probe_ops __read_mostly =
3657 {
3658 .func = function_trace_probe_call,
3659 .flags = FTRACE_OPS_FL_INITIALIZED,
3660 INIT_OPS_HASH(trace_probe_ops)
3661 };
3662
3663 static int ftrace_probe_registered;
3664
__enable_ftrace_function_probe(struct ftrace_ops_hash * old_hash)3665 static void __enable_ftrace_function_probe(struct ftrace_ops_hash *old_hash)
3666 {
3667 int ret;
3668 int i;
3669
3670 if (ftrace_probe_registered) {
3671 /* still need to update the function call sites */
3672 if (ftrace_enabled)
3673 ftrace_run_modify_code(&trace_probe_ops, FTRACE_UPDATE_CALLS,
3674 old_hash);
3675 return;
3676 }
3677
3678 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
3679 struct hlist_head *hhd = &ftrace_func_hash[i];
3680 if (hhd->first)
3681 break;
3682 }
3683 /* Nothing registered? */
3684 if (i == FTRACE_FUNC_HASHSIZE)
3685 return;
3686
3687 ret = ftrace_startup(&trace_probe_ops, 0);
3688
3689 ftrace_probe_registered = 1;
3690 }
3691
__disable_ftrace_function_probe(void)3692 static bool __disable_ftrace_function_probe(void)
3693 {
3694 int i;
3695
3696 if (!ftrace_probe_registered)
3697 return false;
3698
3699 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
3700 struct hlist_head *hhd = &ftrace_func_hash[i];
3701 if (hhd->first)
3702 return false;
3703 }
3704
3705 /* no more funcs left */
3706 ftrace_shutdown(&trace_probe_ops, 0);
3707
3708 ftrace_probe_registered = 0;
3709 return true;
3710 }
3711
3712
ftrace_free_entry(struct ftrace_func_probe * entry)3713 static void ftrace_free_entry(struct ftrace_func_probe *entry)
3714 {
3715 if (entry->ops->free)
3716 entry->ops->free(entry->ops, entry->ip, &entry->data);
3717 kfree(entry);
3718 }
3719
3720 int
register_ftrace_function_probe(char * glob,struct ftrace_probe_ops * ops,void * data)3721 register_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
3722 void *data)
3723 {
3724 struct ftrace_ops_hash old_hash_ops;
3725 struct ftrace_func_probe *entry;
3726 struct ftrace_glob func_g;
3727 struct ftrace_hash **orig_hash = &trace_probe_ops.func_hash->filter_hash;
3728 struct ftrace_hash *old_hash = *orig_hash;
3729 struct ftrace_hash *hash;
3730 struct ftrace_page *pg;
3731 struct dyn_ftrace *rec;
3732 int not;
3733 unsigned long key;
3734 int count = 0;
3735 int ret;
3736
3737 func_g.type = filter_parse_regex(glob, strlen(glob),
3738 &func_g.search, ¬);
3739 func_g.len = strlen(func_g.search);
3740
3741 /* we do not support '!' for function probes */
3742 if (WARN_ON(not))
3743 return -EINVAL;
3744
3745 mutex_lock(&trace_probe_ops.func_hash->regex_lock);
3746
3747 old_hash_ops.filter_hash = old_hash;
3748 /* Probes only have filters */
3749 old_hash_ops.notrace_hash = NULL;
3750
3751 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, old_hash);
3752 if (!hash) {
3753 count = -ENOMEM;
3754 goto out;
3755 }
3756
3757 if (unlikely(ftrace_disabled)) {
3758 count = -ENODEV;
3759 goto out;
3760 }
3761
3762 mutex_lock(&ftrace_lock);
3763
3764 do_for_each_ftrace_rec(pg, rec) {
3765
3766 if (!ftrace_match_record(rec, &func_g, NULL, 0))
3767 continue;
3768
3769 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
3770 if (!entry) {
3771 /* If we did not process any, then return error */
3772 if (!count)
3773 count = -ENOMEM;
3774 goto out_unlock;
3775 }
3776
3777 count++;
3778
3779 entry->data = data;
3780
3781 /*
3782 * The caller might want to do something special
3783 * for each function we find. We call the callback
3784 * to give the caller an opportunity to do so.
3785 */
3786 if (ops->init) {
3787 if (ops->init(ops, rec->ip, &entry->data) < 0) {
3788 /* caller does not like this func */
3789 kfree(entry);
3790 continue;
3791 }
3792 }
3793
3794 ret = enter_record(hash, rec, 0);
3795 if (ret < 0) {
3796 kfree(entry);
3797 count = ret;
3798 goto out_unlock;
3799 }
3800
3801 entry->ops = ops;
3802 entry->ip = rec->ip;
3803
3804 key = hash_long(entry->ip, FTRACE_HASH_BITS);
3805 hlist_add_head_rcu(&entry->node, &ftrace_func_hash[key]);
3806
3807 } while_for_each_ftrace_rec();
3808
3809 ret = ftrace_hash_move(&trace_probe_ops, 1, orig_hash, hash);
3810
3811 __enable_ftrace_function_probe(&old_hash_ops);
3812
3813 if (!ret)
3814 free_ftrace_hash_rcu(old_hash);
3815 else
3816 count = ret;
3817
3818 out_unlock:
3819 mutex_unlock(&ftrace_lock);
3820 out:
3821 mutex_unlock(&trace_probe_ops.func_hash->regex_lock);
3822 free_ftrace_hash(hash);
3823
3824 return count;
3825 }
3826
3827 enum {
3828 PROBE_TEST_FUNC = 1,
3829 PROBE_TEST_DATA = 2
3830 };
3831
3832 static void
__unregister_ftrace_function_probe(char * glob,struct ftrace_probe_ops * ops,void * data,int flags)3833 __unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
3834 void *data, int flags)
3835 {
3836 struct ftrace_ops_hash old_hash_ops;
3837 struct ftrace_func_entry *rec_entry;
3838 struct ftrace_func_probe *entry;
3839 struct ftrace_func_probe *p;
3840 struct ftrace_glob func_g;
3841 struct ftrace_hash **orig_hash = &trace_probe_ops.func_hash->filter_hash;
3842 struct ftrace_hash *old_hash = *orig_hash;
3843 struct list_head free_list;
3844 struct ftrace_hash *hash;
3845 struct hlist_node *tmp;
3846 char str[KSYM_SYMBOL_LEN];
3847 int i, ret;
3848 bool disabled;
3849
3850 if (glob && (strcmp(glob, "*") == 0 || !strlen(glob)))
3851 func_g.search = NULL;
3852 else if (glob) {
3853 int not;
3854
3855 func_g.type = filter_parse_regex(glob, strlen(glob),
3856 &func_g.search, ¬);
3857 func_g.len = strlen(func_g.search);
3858
3859 /* we do not support '!' for function probes */
3860 if (WARN_ON(not))
3861 return;
3862 }
3863
3864 mutex_lock(&trace_probe_ops.func_hash->regex_lock);
3865
3866 old_hash_ops.filter_hash = old_hash;
3867 /* Probes only have filters */
3868 old_hash_ops.notrace_hash = NULL;
3869
3870 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
3871 if (!hash)
3872 /* Hmm, should report this somehow */
3873 goto out_unlock;
3874
3875 INIT_LIST_HEAD(&free_list);
3876
3877 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
3878 struct hlist_head *hhd = &ftrace_func_hash[i];
3879
3880 hlist_for_each_entry_safe(entry, tmp, hhd, node) {
3881
3882 /* break up if statements for readability */
3883 if ((flags & PROBE_TEST_FUNC) && entry->ops != ops)
3884 continue;
3885
3886 if ((flags & PROBE_TEST_DATA) && entry->data != data)
3887 continue;
3888
3889 /* do this last, since it is the most expensive */
3890 if (func_g.search) {
3891 kallsyms_lookup(entry->ip, NULL, NULL,
3892 NULL, str);
3893 if (!ftrace_match(str, &func_g))
3894 continue;
3895 }
3896
3897 rec_entry = ftrace_lookup_ip(hash, entry->ip);
3898 /* It is possible more than one entry had this ip */
3899 if (rec_entry)
3900 free_hash_entry(hash, rec_entry);
3901
3902 hlist_del_rcu(&entry->node);
3903 list_add(&entry->free_list, &free_list);
3904 }
3905 }
3906 mutex_lock(&ftrace_lock);
3907 disabled = __disable_ftrace_function_probe();
3908 /*
3909 * Remove after the disable is called. Otherwise, if the last
3910 * probe is removed, a null hash means *all enabled*.
3911 */
3912 ret = ftrace_hash_move(&trace_probe_ops, 1, orig_hash, hash);
3913
3914 /* still need to update the function call sites */
3915 if (ftrace_enabled && !disabled)
3916 ftrace_run_modify_code(&trace_probe_ops, FTRACE_UPDATE_CALLS,
3917 &old_hash_ops);
3918 synchronize_sched();
3919 if (!ret)
3920 free_ftrace_hash_rcu(old_hash);
3921
3922 list_for_each_entry_safe(entry, p, &free_list, free_list) {
3923 list_del(&entry->free_list);
3924 ftrace_free_entry(entry);
3925 }
3926 mutex_unlock(&ftrace_lock);
3927
3928 out_unlock:
3929 mutex_unlock(&trace_probe_ops.func_hash->regex_lock);
3930 free_ftrace_hash(hash);
3931 }
3932
3933 void
unregister_ftrace_function_probe(char * glob,struct ftrace_probe_ops * ops,void * data)3934 unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
3935 void *data)
3936 {
3937 __unregister_ftrace_function_probe(glob, ops, data,
3938 PROBE_TEST_FUNC | PROBE_TEST_DATA);
3939 }
3940
3941 void
unregister_ftrace_function_probe_func(char * glob,struct ftrace_probe_ops * ops)3942 unregister_ftrace_function_probe_func(char *glob, struct ftrace_probe_ops *ops)
3943 {
3944 __unregister_ftrace_function_probe(glob, ops, NULL, PROBE_TEST_FUNC);
3945 }
3946
unregister_ftrace_function_probe_all(char * glob)3947 void unregister_ftrace_function_probe_all(char *glob)
3948 {
3949 __unregister_ftrace_function_probe(glob, NULL, NULL, 0);
3950 }
3951
3952 static LIST_HEAD(ftrace_commands);
3953 static DEFINE_MUTEX(ftrace_cmd_mutex);
3954
3955 /*
3956 * Currently we only register ftrace commands from __init, so mark this
3957 * __init too.
3958 */
register_ftrace_command(struct ftrace_func_command * cmd)3959 __init int register_ftrace_command(struct ftrace_func_command *cmd)
3960 {
3961 struct ftrace_func_command *p;
3962 int ret = 0;
3963
3964 mutex_lock(&ftrace_cmd_mutex);
3965 list_for_each_entry(p, &ftrace_commands, list) {
3966 if (strcmp(cmd->name, p->name) == 0) {
3967 ret = -EBUSY;
3968 goto out_unlock;
3969 }
3970 }
3971 list_add(&cmd->list, &ftrace_commands);
3972 out_unlock:
3973 mutex_unlock(&ftrace_cmd_mutex);
3974
3975 return ret;
3976 }
3977
3978 /*
3979 * Currently we only unregister ftrace commands from __init, so mark
3980 * this __init too.
3981 */
unregister_ftrace_command(struct ftrace_func_command * cmd)3982 __init int unregister_ftrace_command(struct ftrace_func_command *cmd)
3983 {
3984 struct ftrace_func_command *p, *n;
3985 int ret = -ENODEV;
3986
3987 mutex_lock(&ftrace_cmd_mutex);
3988 list_for_each_entry_safe(p, n, &ftrace_commands, list) {
3989 if (strcmp(cmd->name, p->name) == 0) {
3990 ret = 0;
3991 list_del_init(&p->list);
3992 goto out_unlock;
3993 }
3994 }
3995 out_unlock:
3996 mutex_unlock(&ftrace_cmd_mutex);
3997
3998 return ret;
3999 }
4000
ftrace_process_regex(struct ftrace_hash * hash,char * buff,int len,int enable)4001 static int ftrace_process_regex(struct ftrace_hash *hash,
4002 char *buff, int len, int enable)
4003 {
4004 char *func, *command, *next = buff;
4005 struct ftrace_func_command *p;
4006 int ret = -EINVAL;
4007
4008 func = strsep(&next, ":");
4009
4010 if (!next) {
4011 ret = ftrace_match_records(hash, func, len);
4012 if (!ret)
4013 ret = -EINVAL;
4014 if (ret < 0)
4015 return ret;
4016 return 0;
4017 }
4018
4019 /* command found */
4020
4021 command = strsep(&next, ":");
4022
4023 mutex_lock(&ftrace_cmd_mutex);
4024 list_for_each_entry(p, &ftrace_commands, list) {
4025 if (strcmp(p->name, command) == 0) {
4026 ret = p->func(hash, func, command, next, enable);
4027 goto out_unlock;
4028 }
4029 }
4030 out_unlock:
4031 mutex_unlock(&ftrace_cmd_mutex);
4032
4033 return ret;
4034 }
4035
4036 static ssize_t
ftrace_regex_write(struct file * file,const char __user * ubuf,size_t cnt,loff_t * ppos,int enable)4037 ftrace_regex_write(struct file *file, const char __user *ubuf,
4038 size_t cnt, loff_t *ppos, int enable)
4039 {
4040 struct ftrace_iterator *iter;
4041 struct trace_parser *parser;
4042 ssize_t ret, read;
4043
4044 if (!cnt)
4045 return 0;
4046
4047 if (file->f_mode & FMODE_READ) {
4048 struct seq_file *m = file->private_data;
4049 iter = m->private;
4050 } else
4051 iter = file->private_data;
4052
4053 if (unlikely(ftrace_disabled))
4054 return -ENODEV;
4055
4056 /* iter->hash is a local copy, so we don't need regex_lock */
4057
4058 parser = &iter->parser;
4059 read = trace_get_user(parser, ubuf, cnt, ppos);
4060
4061 if (read >= 0 && trace_parser_loaded(parser) &&
4062 !trace_parser_cont(parser)) {
4063 ret = ftrace_process_regex(iter->hash, parser->buffer,
4064 parser->idx, enable);
4065 trace_parser_clear(parser);
4066 if (ret < 0)
4067 goto out;
4068 }
4069
4070 ret = read;
4071 out:
4072 return ret;
4073 }
4074
4075 ssize_t
ftrace_filter_write(struct file * file,const char __user * ubuf,size_t cnt,loff_t * ppos)4076 ftrace_filter_write(struct file *file, const char __user *ubuf,
4077 size_t cnt, loff_t *ppos)
4078 {
4079 return ftrace_regex_write(file, ubuf, cnt, ppos, 1);
4080 }
4081
4082 ssize_t
ftrace_notrace_write(struct file * file,const char __user * ubuf,size_t cnt,loff_t * ppos)4083 ftrace_notrace_write(struct file *file, const char __user *ubuf,
4084 size_t cnt, loff_t *ppos)
4085 {
4086 return ftrace_regex_write(file, ubuf, cnt, ppos, 0);
4087 }
4088
4089 static int
ftrace_match_addr(struct ftrace_hash * hash,unsigned long ip,int remove)4090 ftrace_match_addr(struct ftrace_hash *hash, unsigned long ip, int remove)
4091 {
4092 struct ftrace_func_entry *entry;
4093
4094 if (!ftrace_location(ip))
4095 return -EINVAL;
4096
4097 if (remove) {
4098 entry = ftrace_lookup_ip(hash, ip);
4099 if (!entry)
4100 return -ENOENT;
4101 free_hash_entry(hash, entry);
4102 return 0;
4103 }
4104
4105 return add_hash_entry(hash, ip);
4106 }
4107
ftrace_ops_update_code(struct ftrace_ops * ops,struct ftrace_ops_hash * old_hash)4108 static void ftrace_ops_update_code(struct ftrace_ops *ops,
4109 struct ftrace_ops_hash *old_hash)
4110 {
4111 struct ftrace_ops *op;
4112
4113 if (!ftrace_enabled)
4114 return;
4115
4116 if (ops->flags & FTRACE_OPS_FL_ENABLED) {
4117 ftrace_run_modify_code(ops, FTRACE_UPDATE_CALLS, old_hash);
4118 return;
4119 }
4120
4121 /*
4122 * If this is the shared global_ops filter, then we need to
4123 * check if there is another ops that shares it, is enabled.
4124 * If so, we still need to run the modify code.
4125 */
4126 if (ops->func_hash != &global_ops.local_hash)
4127 return;
4128
4129 do_for_each_ftrace_op(op, ftrace_ops_list) {
4130 if (op->func_hash == &global_ops.local_hash &&
4131 op->flags & FTRACE_OPS_FL_ENABLED) {
4132 ftrace_run_modify_code(op, FTRACE_UPDATE_CALLS, old_hash);
4133 /* Only need to do this once */
4134 return;
4135 }
4136 } while_for_each_ftrace_op(op);
4137 }
4138
4139 static int
ftrace_set_hash(struct ftrace_ops * ops,unsigned char * buf,int len,unsigned long ip,int remove,int reset,int enable)4140 ftrace_set_hash(struct ftrace_ops *ops, unsigned char *buf, int len,
4141 unsigned long ip, int remove, int reset, int enable)
4142 {
4143 struct ftrace_hash **orig_hash;
4144 struct ftrace_ops_hash old_hash_ops;
4145 struct ftrace_hash *old_hash;
4146 struct ftrace_hash *hash;
4147 int ret;
4148
4149 if (unlikely(ftrace_disabled))
4150 return -ENODEV;
4151
4152 mutex_lock(&ops->func_hash->regex_lock);
4153
4154 if (enable)
4155 orig_hash = &ops->func_hash->filter_hash;
4156 else
4157 orig_hash = &ops->func_hash->notrace_hash;
4158
4159 if (reset)
4160 hash = alloc_ftrace_hash(FTRACE_HASH_DEFAULT_BITS);
4161 else
4162 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
4163
4164 if (!hash) {
4165 ret = -ENOMEM;
4166 goto out_regex_unlock;
4167 }
4168
4169 if (buf && !ftrace_match_records(hash, buf, len)) {
4170 ret = -EINVAL;
4171 goto out_regex_unlock;
4172 }
4173 if (ip) {
4174 ret = ftrace_match_addr(hash, ip, remove);
4175 if (ret < 0)
4176 goto out_regex_unlock;
4177 }
4178
4179 mutex_lock(&ftrace_lock);
4180 old_hash = *orig_hash;
4181 old_hash_ops.filter_hash = ops->func_hash->filter_hash;
4182 old_hash_ops.notrace_hash = ops->func_hash->notrace_hash;
4183 ret = ftrace_hash_move(ops, enable, orig_hash, hash);
4184 if (!ret) {
4185 ftrace_ops_update_code(ops, &old_hash_ops);
4186 free_ftrace_hash_rcu(old_hash);
4187 }
4188 mutex_unlock(&ftrace_lock);
4189
4190 out_regex_unlock:
4191 mutex_unlock(&ops->func_hash->regex_lock);
4192
4193 free_ftrace_hash(hash);
4194 return ret;
4195 }
4196
4197 static int
ftrace_set_addr(struct ftrace_ops * ops,unsigned long ip,int remove,int reset,int enable)4198 ftrace_set_addr(struct ftrace_ops *ops, unsigned long ip, int remove,
4199 int reset, int enable)
4200 {
4201 return ftrace_set_hash(ops, 0, 0, ip, remove, reset, enable);
4202 }
4203
4204 /**
4205 * ftrace_set_filter_ip - set a function to filter on in ftrace by address
4206 * @ops - the ops to set the filter with
4207 * @ip - the address to add to or remove from the filter.
4208 * @remove - non zero to remove the ip from the filter
4209 * @reset - non zero to reset all filters before applying this filter.
4210 *
4211 * Filters denote which functions should be enabled when tracing is enabled
4212 * If @ip is NULL, it failes to update filter.
4213 */
ftrace_set_filter_ip(struct ftrace_ops * ops,unsigned long ip,int remove,int reset)4214 int ftrace_set_filter_ip(struct ftrace_ops *ops, unsigned long ip,
4215 int remove, int reset)
4216 {
4217 ftrace_ops_init(ops);
4218 return ftrace_set_addr(ops, ip, remove, reset, 1);
4219 }
4220 EXPORT_SYMBOL_GPL(ftrace_set_filter_ip);
4221
4222 static int
ftrace_set_regex(struct ftrace_ops * ops,unsigned char * buf,int len,int reset,int enable)4223 ftrace_set_regex(struct ftrace_ops *ops, unsigned char *buf, int len,
4224 int reset, int enable)
4225 {
4226 return ftrace_set_hash(ops, buf, len, 0, 0, reset, enable);
4227 }
4228
4229 /**
4230 * ftrace_set_filter - set a function to filter on in ftrace
4231 * @ops - the ops to set the filter with
4232 * @buf - the string that holds the function filter text.
4233 * @len - the length of the string.
4234 * @reset - non zero to reset all filters before applying this filter.
4235 *
4236 * Filters denote which functions should be enabled when tracing is enabled.
4237 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
4238 */
ftrace_set_filter(struct ftrace_ops * ops,unsigned char * buf,int len,int reset)4239 int ftrace_set_filter(struct ftrace_ops *ops, unsigned char *buf,
4240 int len, int reset)
4241 {
4242 ftrace_ops_init(ops);
4243 return ftrace_set_regex(ops, buf, len, reset, 1);
4244 }
4245 EXPORT_SYMBOL_GPL(ftrace_set_filter);
4246
4247 /**
4248 * ftrace_set_notrace - set a function to not trace in ftrace
4249 * @ops - the ops to set the notrace filter with
4250 * @buf - the string that holds the function notrace text.
4251 * @len - the length of the string.
4252 * @reset - non zero to reset all filters before applying this filter.
4253 *
4254 * Notrace Filters denote which functions should not be enabled when tracing
4255 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
4256 * for tracing.
4257 */
ftrace_set_notrace(struct ftrace_ops * ops,unsigned char * buf,int len,int reset)4258 int ftrace_set_notrace(struct ftrace_ops *ops, unsigned char *buf,
4259 int len, int reset)
4260 {
4261 ftrace_ops_init(ops);
4262 return ftrace_set_regex(ops, buf, len, reset, 0);
4263 }
4264 EXPORT_SYMBOL_GPL(ftrace_set_notrace);
4265 /**
4266 * ftrace_set_global_filter - set a function to filter on with global tracers
4267 * @buf - the string that holds the function filter text.
4268 * @len - the length of the string.
4269 * @reset - non zero to reset all filters before applying this filter.
4270 *
4271 * Filters denote which functions should be enabled when tracing is enabled.
4272 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
4273 */
ftrace_set_global_filter(unsigned char * buf,int len,int reset)4274 void ftrace_set_global_filter(unsigned char *buf, int len, int reset)
4275 {
4276 ftrace_set_regex(&global_ops, buf, len, reset, 1);
4277 }
4278 EXPORT_SYMBOL_GPL(ftrace_set_global_filter);
4279
4280 /**
4281 * ftrace_set_global_notrace - set a function to not trace with global tracers
4282 * @buf - the string that holds the function notrace text.
4283 * @len - the length of the string.
4284 * @reset - non zero to reset all filters before applying this filter.
4285 *
4286 * Notrace Filters denote which functions should not be enabled when tracing
4287 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
4288 * for tracing.
4289 */
ftrace_set_global_notrace(unsigned char * buf,int len,int reset)4290 void ftrace_set_global_notrace(unsigned char *buf, int len, int reset)
4291 {
4292 ftrace_set_regex(&global_ops, buf, len, reset, 0);
4293 }
4294 EXPORT_SYMBOL_GPL(ftrace_set_global_notrace);
4295
4296 /*
4297 * command line interface to allow users to set filters on boot up.
4298 */
4299 #define FTRACE_FILTER_SIZE COMMAND_LINE_SIZE
4300 static char ftrace_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
4301 static char ftrace_filter_buf[FTRACE_FILTER_SIZE] __initdata;
4302
4303 /* Used by function selftest to not test if filter is set */
4304 bool ftrace_filter_param __initdata;
4305
set_ftrace_notrace(char * str)4306 static int __init set_ftrace_notrace(char *str)
4307 {
4308 ftrace_filter_param = true;
4309 strlcpy(ftrace_notrace_buf, str, FTRACE_FILTER_SIZE);
4310 return 1;
4311 }
4312 __setup("ftrace_notrace=", set_ftrace_notrace);
4313
set_ftrace_filter(char * str)4314 static int __init set_ftrace_filter(char *str)
4315 {
4316 ftrace_filter_param = true;
4317 strlcpy(ftrace_filter_buf, str, FTRACE_FILTER_SIZE);
4318 return 1;
4319 }
4320 __setup("ftrace_filter=", set_ftrace_filter);
4321
4322 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
4323 static char ftrace_graph_buf[FTRACE_FILTER_SIZE] __initdata;
4324 static char ftrace_graph_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
4325 static int ftrace_set_func(unsigned long *array, int *idx, int size, char *buffer);
4326
set_graph_function(char * str)4327 static int __init set_graph_function(char *str)
4328 {
4329 strlcpy(ftrace_graph_buf, str, FTRACE_FILTER_SIZE);
4330 return 1;
4331 }
4332 __setup("ftrace_graph_filter=", set_graph_function);
4333
set_graph_notrace_function(char * str)4334 static int __init set_graph_notrace_function(char *str)
4335 {
4336 strlcpy(ftrace_graph_notrace_buf, str, FTRACE_FILTER_SIZE);
4337 return 1;
4338 }
4339 __setup("ftrace_graph_notrace=", set_graph_notrace_function);
4340
set_ftrace_early_graph(char * buf,int enable)4341 static void __init set_ftrace_early_graph(char *buf, int enable)
4342 {
4343 int ret;
4344 char *func;
4345 unsigned long *table = ftrace_graph_funcs;
4346 int *count = &ftrace_graph_count;
4347
4348 if (!enable) {
4349 table = ftrace_graph_notrace_funcs;
4350 count = &ftrace_graph_notrace_count;
4351 }
4352
4353 while (buf) {
4354 func = strsep(&buf, ",");
4355 /* we allow only one expression at a time */
4356 ret = ftrace_set_func(table, count, FTRACE_GRAPH_MAX_FUNCS, func);
4357 if (ret)
4358 printk(KERN_DEBUG "ftrace: function %s not "
4359 "traceable\n", func);
4360 }
4361 }
4362 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
4363
4364 void __init
ftrace_set_early_filter(struct ftrace_ops * ops,char * buf,int enable)4365 ftrace_set_early_filter(struct ftrace_ops *ops, char *buf, int enable)
4366 {
4367 char *func;
4368
4369 ftrace_ops_init(ops);
4370
4371 while (buf) {
4372 func = strsep(&buf, ",");
4373 ftrace_set_regex(ops, func, strlen(func), 0, enable);
4374 }
4375 }
4376
set_ftrace_early_filters(void)4377 static void __init set_ftrace_early_filters(void)
4378 {
4379 if (ftrace_filter_buf[0])
4380 ftrace_set_early_filter(&global_ops, ftrace_filter_buf, 1);
4381 if (ftrace_notrace_buf[0])
4382 ftrace_set_early_filter(&global_ops, ftrace_notrace_buf, 0);
4383 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
4384 if (ftrace_graph_buf[0])
4385 set_ftrace_early_graph(ftrace_graph_buf, 1);
4386 if (ftrace_graph_notrace_buf[0])
4387 set_ftrace_early_graph(ftrace_graph_notrace_buf, 0);
4388 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
4389 }
4390
ftrace_regex_release(struct inode * inode,struct file * file)4391 int ftrace_regex_release(struct inode *inode, struct file *file)
4392 {
4393 struct seq_file *m = (struct seq_file *)file->private_data;
4394 struct ftrace_ops_hash old_hash_ops;
4395 struct ftrace_iterator *iter;
4396 struct ftrace_hash **orig_hash;
4397 struct ftrace_hash *old_hash;
4398 struct trace_parser *parser;
4399 int filter_hash;
4400 int ret;
4401
4402 if (file->f_mode & FMODE_READ) {
4403 iter = m->private;
4404 seq_release(inode, file);
4405 } else
4406 iter = file->private_data;
4407
4408 parser = &iter->parser;
4409 if (trace_parser_loaded(parser)) {
4410 int enable = !(iter->flags & FTRACE_ITER_NOTRACE);
4411
4412 parser->buffer[parser->idx] = 0;
4413 ftrace_process_regex(iter->hash, parser->buffer,
4414 parser->idx, enable);
4415 }
4416
4417 trace_parser_put(parser);
4418
4419 mutex_lock(&iter->ops->func_hash->regex_lock);
4420
4421 if (file->f_mode & FMODE_WRITE) {
4422 filter_hash = !!(iter->flags & FTRACE_ITER_FILTER);
4423
4424 if (filter_hash)
4425 orig_hash = &iter->ops->func_hash->filter_hash;
4426 else
4427 orig_hash = &iter->ops->func_hash->notrace_hash;
4428
4429 mutex_lock(&ftrace_lock);
4430 old_hash = *orig_hash;
4431 old_hash_ops.filter_hash = iter->ops->func_hash->filter_hash;
4432 old_hash_ops.notrace_hash = iter->ops->func_hash->notrace_hash;
4433 ret = ftrace_hash_move(iter->ops, filter_hash,
4434 orig_hash, iter->hash);
4435 if (!ret) {
4436 ftrace_ops_update_code(iter->ops, &old_hash_ops);
4437 free_ftrace_hash_rcu(old_hash);
4438 }
4439 mutex_unlock(&ftrace_lock);
4440 }
4441
4442 mutex_unlock(&iter->ops->func_hash->regex_lock);
4443 free_ftrace_hash(iter->hash);
4444 kfree(iter);
4445
4446 return 0;
4447 }
4448
4449 static const struct file_operations ftrace_avail_fops = {
4450 .open = ftrace_avail_open,
4451 .read = seq_read,
4452 .llseek = seq_lseek,
4453 .release = seq_release_private,
4454 };
4455
4456 static const struct file_operations ftrace_enabled_fops = {
4457 .open = ftrace_enabled_open,
4458 .read = seq_read,
4459 .llseek = seq_lseek,
4460 .release = seq_release_private,
4461 };
4462
4463 static const struct file_operations ftrace_filter_fops = {
4464 .open = ftrace_filter_open,
4465 .read = seq_read,
4466 .write = ftrace_filter_write,
4467 .llseek = tracing_lseek,
4468 .release = ftrace_regex_release,
4469 };
4470
4471 static const struct file_operations ftrace_notrace_fops = {
4472 .open = ftrace_notrace_open,
4473 .read = seq_read,
4474 .write = ftrace_notrace_write,
4475 .llseek = tracing_lseek,
4476 .release = ftrace_regex_release,
4477 };
4478
4479 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
4480
4481 static DEFINE_MUTEX(graph_lock);
4482
4483 int ftrace_graph_count;
4484 int ftrace_graph_notrace_count;
4485 unsigned long ftrace_graph_funcs[FTRACE_GRAPH_MAX_FUNCS] __read_mostly;
4486 unsigned long ftrace_graph_notrace_funcs[FTRACE_GRAPH_MAX_FUNCS] __read_mostly;
4487
4488 struct ftrace_graph_data {
4489 unsigned long *table;
4490 size_t size;
4491 int *count;
4492 const struct seq_operations *seq_ops;
4493 };
4494
4495 static void *
__g_next(struct seq_file * m,loff_t * pos)4496 __g_next(struct seq_file *m, loff_t *pos)
4497 {
4498 struct ftrace_graph_data *fgd = m->private;
4499
4500 if (*pos >= *fgd->count)
4501 return NULL;
4502 return &fgd->table[*pos];
4503 }
4504
4505 static void *
g_next(struct seq_file * m,void * v,loff_t * pos)4506 g_next(struct seq_file *m, void *v, loff_t *pos)
4507 {
4508 (*pos)++;
4509 return __g_next(m, pos);
4510 }
4511
g_start(struct seq_file * m,loff_t * pos)4512 static void *g_start(struct seq_file *m, loff_t *pos)
4513 {
4514 struct ftrace_graph_data *fgd = m->private;
4515
4516 mutex_lock(&graph_lock);
4517
4518 /* Nothing, tell g_show to print all functions are enabled */
4519 if (!*fgd->count && !*pos)
4520 return (void *)1;
4521
4522 return __g_next(m, pos);
4523 }
4524
g_stop(struct seq_file * m,void * p)4525 static void g_stop(struct seq_file *m, void *p)
4526 {
4527 mutex_unlock(&graph_lock);
4528 }
4529
g_show(struct seq_file * m,void * v)4530 static int g_show(struct seq_file *m, void *v)
4531 {
4532 unsigned long *ptr = v;
4533
4534 if (!ptr)
4535 return 0;
4536
4537 if (ptr == (unsigned long *)1) {
4538 struct ftrace_graph_data *fgd = m->private;
4539
4540 if (fgd->table == ftrace_graph_funcs)
4541 seq_puts(m, "#### all functions enabled ####\n");
4542 else
4543 seq_puts(m, "#### no functions disabled ####\n");
4544 return 0;
4545 }
4546
4547 seq_printf(m, "%ps\n", (void *)*ptr);
4548
4549 return 0;
4550 }
4551
4552 static const struct seq_operations ftrace_graph_seq_ops = {
4553 .start = g_start,
4554 .next = g_next,
4555 .stop = g_stop,
4556 .show = g_show,
4557 };
4558
4559 static int
__ftrace_graph_open(struct inode * inode,struct file * file,struct ftrace_graph_data * fgd)4560 __ftrace_graph_open(struct inode *inode, struct file *file,
4561 struct ftrace_graph_data *fgd)
4562 {
4563 int ret = 0;
4564
4565 mutex_lock(&graph_lock);
4566 if ((file->f_mode & FMODE_WRITE) &&
4567 (file->f_flags & O_TRUNC)) {
4568 *fgd->count = 0;
4569 memset(fgd->table, 0, fgd->size * sizeof(*fgd->table));
4570 }
4571 mutex_unlock(&graph_lock);
4572
4573 if (file->f_mode & FMODE_READ) {
4574 ret = seq_open(file, fgd->seq_ops);
4575 if (!ret) {
4576 struct seq_file *m = file->private_data;
4577 m->private = fgd;
4578 }
4579 } else
4580 file->private_data = fgd;
4581
4582 return ret;
4583 }
4584
4585 static int
ftrace_graph_open(struct inode * inode,struct file * file)4586 ftrace_graph_open(struct inode *inode, struct file *file)
4587 {
4588 struct ftrace_graph_data *fgd;
4589
4590 if (unlikely(ftrace_disabled))
4591 return -ENODEV;
4592
4593 fgd = kmalloc(sizeof(*fgd), GFP_KERNEL);
4594 if (fgd == NULL)
4595 return -ENOMEM;
4596
4597 fgd->table = ftrace_graph_funcs;
4598 fgd->size = FTRACE_GRAPH_MAX_FUNCS;
4599 fgd->count = &ftrace_graph_count;
4600 fgd->seq_ops = &ftrace_graph_seq_ops;
4601
4602 return __ftrace_graph_open(inode, file, fgd);
4603 }
4604
4605 static int
ftrace_graph_notrace_open(struct inode * inode,struct file * file)4606 ftrace_graph_notrace_open(struct inode *inode, struct file *file)
4607 {
4608 struct ftrace_graph_data *fgd;
4609
4610 if (unlikely(ftrace_disabled))
4611 return -ENODEV;
4612
4613 fgd = kmalloc(sizeof(*fgd), GFP_KERNEL);
4614 if (fgd == NULL)
4615 return -ENOMEM;
4616
4617 fgd->table = ftrace_graph_notrace_funcs;
4618 fgd->size = FTRACE_GRAPH_MAX_FUNCS;
4619 fgd->count = &ftrace_graph_notrace_count;
4620 fgd->seq_ops = &ftrace_graph_seq_ops;
4621
4622 return __ftrace_graph_open(inode, file, fgd);
4623 }
4624
4625 static int
ftrace_graph_release(struct inode * inode,struct file * file)4626 ftrace_graph_release(struct inode *inode, struct file *file)
4627 {
4628 if (file->f_mode & FMODE_READ) {
4629 struct seq_file *m = file->private_data;
4630
4631 kfree(m->private);
4632 seq_release(inode, file);
4633 } else {
4634 kfree(file->private_data);
4635 }
4636
4637 return 0;
4638 }
4639
4640 static int
ftrace_set_func(unsigned long * array,int * idx,int size,char * buffer)4641 ftrace_set_func(unsigned long *array, int *idx, int size, char *buffer)
4642 {
4643 struct ftrace_glob func_g;
4644 struct dyn_ftrace *rec;
4645 struct ftrace_page *pg;
4646 int fail = 1;
4647 int not;
4648 bool exists;
4649 int i;
4650
4651 /* decode regex */
4652 func_g.type = filter_parse_regex(buffer, strlen(buffer),
4653 &func_g.search, ¬);
4654 if (!not && *idx >= size)
4655 return -EBUSY;
4656
4657 func_g.len = strlen(func_g.search);
4658
4659 mutex_lock(&ftrace_lock);
4660
4661 if (unlikely(ftrace_disabled)) {
4662 mutex_unlock(&ftrace_lock);
4663 return -ENODEV;
4664 }
4665
4666 do_for_each_ftrace_rec(pg, rec) {
4667
4668 if (ftrace_match_record(rec, &func_g, NULL, 0)) {
4669 /* if it is in the array */
4670 exists = false;
4671 for (i = 0; i < *idx; i++) {
4672 if (array[i] == rec->ip) {
4673 exists = true;
4674 break;
4675 }
4676 }
4677
4678 if (!not) {
4679 fail = 0;
4680 if (!exists) {
4681 array[(*idx)++] = rec->ip;
4682 if (*idx >= size)
4683 goto out;
4684 }
4685 } else {
4686 if (exists) {
4687 array[i] = array[--(*idx)];
4688 array[*idx] = 0;
4689 fail = 0;
4690 }
4691 }
4692 }
4693 } while_for_each_ftrace_rec();
4694 out:
4695 mutex_unlock(&ftrace_lock);
4696
4697 if (fail)
4698 return -EINVAL;
4699
4700 return 0;
4701 }
4702
4703 static ssize_t
ftrace_graph_write(struct file * file,const char __user * ubuf,size_t cnt,loff_t * ppos)4704 ftrace_graph_write(struct file *file, const char __user *ubuf,
4705 size_t cnt, loff_t *ppos)
4706 {
4707 struct trace_parser parser;
4708 ssize_t read, ret = 0;
4709 struct ftrace_graph_data *fgd = file->private_data;
4710
4711 if (!cnt)
4712 return 0;
4713
4714 if (trace_parser_get_init(&parser, FTRACE_BUFF_MAX))
4715 return -ENOMEM;
4716
4717 read = trace_get_user(&parser, ubuf, cnt, ppos);
4718
4719 if (read >= 0 && trace_parser_loaded((&parser))) {
4720 parser.buffer[parser.idx] = 0;
4721
4722 mutex_lock(&graph_lock);
4723
4724 /* we allow only one expression at a time */
4725 ret = ftrace_set_func(fgd->table, fgd->count, fgd->size,
4726 parser.buffer);
4727
4728 mutex_unlock(&graph_lock);
4729 }
4730
4731 if (!ret)
4732 ret = read;
4733
4734 trace_parser_put(&parser);
4735
4736 return ret;
4737 }
4738
4739 static const struct file_operations ftrace_graph_fops = {
4740 .open = ftrace_graph_open,
4741 .read = seq_read,
4742 .write = ftrace_graph_write,
4743 .llseek = tracing_lseek,
4744 .release = ftrace_graph_release,
4745 };
4746
4747 static const struct file_operations ftrace_graph_notrace_fops = {
4748 .open = ftrace_graph_notrace_open,
4749 .read = seq_read,
4750 .write = ftrace_graph_write,
4751 .llseek = tracing_lseek,
4752 .release = ftrace_graph_release,
4753 };
4754 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
4755
ftrace_create_filter_files(struct ftrace_ops * ops,struct dentry * parent)4756 void ftrace_create_filter_files(struct ftrace_ops *ops,
4757 struct dentry *parent)
4758 {
4759
4760 trace_create_file("set_ftrace_filter", 0644, parent,
4761 ops, &ftrace_filter_fops);
4762
4763 trace_create_file("set_ftrace_notrace", 0644, parent,
4764 ops, &ftrace_notrace_fops);
4765 }
4766
4767 /*
4768 * The name "destroy_filter_files" is really a misnomer. Although
4769 * in the future, it may actualy delete the files, but this is
4770 * really intended to make sure the ops passed in are disabled
4771 * and that when this function returns, the caller is free to
4772 * free the ops.
4773 *
4774 * The "destroy" name is only to match the "create" name that this
4775 * should be paired with.
4776 */
ftrace_destroy_filter_files(struct ftrace_ops * ops)4777 void ftrace_destroy_filter_files(struct ftrace_ops *ops)
4778 {
4779 mutex_lock(&ftrace_lock);
4780 if (ops->flags & FTRACE_OPS_FL_ENABLED)
4781 ftrace_shutdown(ops, 0);
4782 ops->flags |= FTRACE_OPS_FL_DELETED;
4783 ftrace_free_filter(ops);
4784 mutex_unlock(&ftrace_lock);
4785 }
4786
ftrace_init_dyn_tracefs(struct dentry * d_tracer)4787 static __init int ftrace_init_dyn_tracefs(struct dentry *d_tracer)
4788 {
4789
4790 trace_create_file("available_filter_functions", 0444,
4791 d_tracer, NULL, &ftrace_avail_fops);
4792
4793 trace_create_file("enabled_functions", 0444,
4794 d_tracer, NULL, &ftrace_enabled_fops);
4795
4796 ftrace_create_filter_files(&global_ops, d_tracer);
4797
4798 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
4799 trace_create_file("set_graph_function", 0444, d_tracer,
4800 NULL,
4801 &ftrace_graph_fops);
4802 trace_create_file("set_graph_notrace", 0444, d_tracer,
4803 NULL,
4804 &ftrace_graph_notrace_fops);
4805 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
4806
4807 return 0;
4808 }
4809
ftrace_cmp_ips(const void * a,const void * b)4810 static int ftrace_cmp_ips(const void *a, const void *b)
4811 {
4812 const unsigned long *ipa = a;
4813 const unsigned long *ipb = b;
4814
4815 if (*ipa > *ipb)
4816 return 1;
4817 if (*ipa < *ipb)
4818 return -1;
4819 return 0;
4820 }
4821
ftrace_process_locs(struct module * mod,unsigned long * start,unsigned long * end)4822 static int ftrace_process_locs(struct module *mod,
4823 unsigned long *start,
4824 unsigned long *end)
4825 {
4826 struct ftrace_page *start_pg;
4827 struct ftrace_page *pg;
4828 struct dyn_ftrace *rec;
4829 unsigned long count;
4830 unsigned long *p;
4831 unsigned long addr;
4832 unsigned long flags = 0; /* Shut up gcc */
4833 int ret = -ENOMEM;
4834
4835 count = end - start;
4836
4837 if (!count)
4838 return 0;
4839
4840 sort(start, count, sizeof(*start),
4841 ftrace_cmp_ips, NULL);
4842
4843 start_pg = ftrace_allocate_pages(count);
4844 if (!start_pg)
4845 return -ENOMEM;
4846
4847 mutex_lock(&ftrace_lock);
4848
4849 /*
4850 * Core and each module needs their own pages, as
4851 * modules will free them when they are removed.
4852 * Force a new page to be allocated for modules.
4853 */
4854 if (!mod) {
4855 WARN_ON(ftrace_pages || ftrace_pages_start);
4856 /* First initialization */
4857 ftrace_pages = ftrace_pages_start = start_pg;
4858 } else {
4859 if (!ftrace_pages)
4860 goto out;
4861
4862 if (WARN_ON(ftrace_pages->next)) {
4863 /* Hmm, we have free pages? */
4864 while (ftrace_pages->next)
4865 ftrace_pages = ftrace_pages->next;
4866 }
4867
4868 ftrace_pages->next = start_pg;
4869 }
4870
4871 p = start;
4872 pg = start_pg;
4873 while (p < end) {
4874 addr = ftrace_call_adjust(*p++);
4875 /*
4876 * Some architecture linkers will pad between
4877 * the different mcount_loc sections of different
4878 * object files to satisfy alignments.
4879 * Skip any NULL pointers.
4880 */
4881 if (!addr)
4882 continue;
4883
4884 if (pg->index == pg->size) {
4885 /* We should have allocated enough */
4886 if (WARN_ON(!pg->next))
4887 break;
4888 pg = pg->next;
4889 }
4890
4891 rec = &pg->records[pg->index++];
4892 rec->ip = addr;
4893 }
4894
4895 /* We should have used all pages */
4896 WARN_ON(pg->next);
4897
4898 /* Assign the last page to ftrace_pages */
4899 ftrace_pages = pg;
4900
4901 /*
4902 * We only need to disable interrupts on start up
4903 * because we are modifying code that an interrupt
4904 * may execute, and the modification is not atomic.
4905 * But for modules, nothing runs the code we modify
4906 * until we are finished with it, and there's no
4907 * reason to cause large interrupt latencies while we do it.
4908 */
4909 if (!mod)
4910 local_irq_save(flags);
4911 ftrace_update_code(mod, start_pg);
4912 if (!mod)
4913 local_irq_restore(flags);
4914 ret = 0;
4915 out:
4916 mutex_unlock(&ftrace_lock);
4917
4918 return ret;
4919 }
4920
4921 #ifdef CONFIG_MODULES
4922
4923 #define next_to_ftrace_page(p) container_of(p, struct ftrace_page, next)
4924
ftrace_release_mod(struct module * mod)4925 void ftrace_release_mod(struct module *mod)
4926 {
4927 struct dyn_ftrace *rec;
4928 struct ftrace_page **last_pg;
4929 struct ftrace_page *pg;
4930 int order;
4931
4932 mutex_lock(&ftrace_lock);
4933
4934 if (ftrace_disabled)
4935 goto out_unlock;
4936
4937 /*
4938 * Each module has its own ftrace_pages, remove
4939 * them from the list.
4940 */
4941 last_pg = &ftrace_pages_start;
4942 for (pg = ftrace_pages_start; pg; pg = *last_pg) {
4943 rec = &pg->records[0];
4944 if (within_module_core(rec->ip, mod)) {
4945 /*
4946 * As core pages are first, the first
4947 * page should never be a module page.
4948 */
4949 if (WARN_ON(pg == ftrace_pages_start))
4950 goto out_unlock;
4951
4952 /* Check if we are deleting the last page */
4953 if (pg == ftrace_pages)
4954 ftrace_pages = next_to_ftrace_page(last_pg);
4955
4956 *last_pg = pg->next;
4957 order = get_count_order(pg->size / ENTRIES_PER_PAGE);
4958 free_pages((unsigned long)pg->records, order);
4959 kfree(pg);
4960 } else
4961 last_pg = &pg->next;
4962 }
4963 out_unlock:
4964 mutex_unlock(&ftrace_lock);
4965 }
4966
ftrace_init_module(struct module * mod,unsigned long * start,unsigned long * end)4967 static void ftrace_init_module(struct module *mod,
4968 unsigned long *start, unsigned long *end)
4969 {
4970 if (ftrace_disabled || start == end)
4971 return;
4972 ftrace_process_locs(mod, start, end);
4973 }
4974
ftrace_module_init(struct module * mod)4975 void ftrace_module_init(struct module *mod)
4976 {
4977 ftrace_init_module(mod, mod->ftrace_callsites,
4978 mod->ftrace_callsites +
4979 mod->num_ftrace_callsites);
4980 }
4981
ftrace_module_notify_exit(struct notifier_block * self,unsigned long val,void * data)4982 static int ftrace_module_notify_exit(struct notifier_block *self,
4983 unsigned long val, void *data)
4984 {
4985 struct module *mod = data;
4986
4987 if (val == MODULE_STATE_GOING)
4988 ftrace_release_mod(mod);
4989
4990 return 0;
4991 }
4992 #else
ftrace_module_notify_exit(struct notifier_block * self,unsigned long val,void * data)4993 static int ftrace_module_notify_exit(struct notifier_block *self,
4994 unsigned long val, void *data)
4995 {
4996 return 0;
4997 }
4998 #endif /* CONFIG_MODULES */
4999
5000 struct notifier_block ftrace_module_exit_nb = {
5001 .notifier_call = ftrace_module_notify_exit,
5002 .priority = INT_MIN, /* Run after anything that can remove kprobes */
5003 };
5004
ftrace_init(void)5005 void __init ftrace_init(void)
5006 {
5007 extern unsigned long __start_mcount_loc[];
5008 extern unsigned long __stop_mcount_loc[];
5009 unsigned long count, flags;
5010 int ret;
5011
5012 local_irq_save(flags);
5013 ret = ftrace_dyn_arch_init();
5014 local_irq_restore(flags);
5015 if (ret)
5016 goto failed;
5017
5018 count = __stop_mcount_loc - __start_mcount_loc;
5019 if (!count) {
5020 pr_info("ftrace: No functions to be traced?\n");
5021 goto failed;
5022 }
5023
5024 pr_info("ftrace: allocating %ld entries in %ld pages\n",
5025 count, count / ENTRIES_PER_PAGE + 1);
5026
5027 last_ftrace_enabled = ftrace_enabled = 1;
5028
5029 ret = ftrace_process_locs(NULL,
5030 __start_mcount_loc,
5031 __stop_mcount_loc);
5032
5033 ret = register_module_notifier(&ftrace_module_exit_nb);
5034 if (ret)
5035 pr_warning("Failed to register trace ftrace module exit notifier\n");
5036
5037 set_ftrace_early_filters();
5038
5039 return;
5040 failed:
5041 ftrace_disabled = 1;
5042 }
5043
5044 /* Do nothing if arch does not support this */
arch_ftrace_update_trampoline(struct ftrace_ops * ops)5045 void __weak arch_ftrace_update_trampoline(struct ftrace_ops *ops)
5046 {
5047 }
5048
ftrace_update_trampoline(struct ftrace_ops * ops)5049 static void ftrace_update_trampoline(struct ftrace_ops *ops)
5050 {
5051
5052 /*
5053 * Currently there's no safe way to free a trampoline when the kernel
5054 * is configured with PREEMPT. That is because a task could be preempted
5055 * when it jumped to the trampoline, it may be preempted for a long time
5056 * depending on the system load, and currently there's no way to know
5057 * when it will be off the trampoline. If the trampoline is freed
5058 * too early, when the task runs again, it will be executing on freed
5059 * memory and crash.
5060 */
5061 #ifdef CONFIG_PREEMPT
5062 /* Currently, only non dynamic ops can have a trampoline */
5063 if (ops->flags & FTRACE_OPS_FL_DYNAMIC)
5064 return;
5065 #endif
5066
5067 arch_ftrace_update_trampoline(ops);
5068 }
5069
5070 #else
5071
5072 static struct ftrace_ops global_ops = {
5073 .func = ftrace_stub,
5074 .flags = FTRACE_OPS_FL_RECURSION_SAFE |
5075 FTRACE_OPS_FL_INITIALIZED |
5076 FTRACE_OPS_FL_PID,
5077 };
5078
ftrace_nodyn_init(void)5079 static int __init ftrace_nodyn_init(void)
5080 {
5081 ftrace_enabled = 1;
5082 return 0;
5083 }
5084 core_initcall(ftrace_nodyn_init);
5085
ftrace_init_dyn_tracefs(struct dentry * d_tracer)5086 static inline int ftrace_init_dyn_tracefs(struct dentry *d_tracer) { return 0; }
ftrace_startup_enable(int command)5087 static inline void ftrace_startup_enable(int command) { }
ftrace_startup_all(int command)5088 static inline void ftrace_startup_all(int command) { }
5089 /* Keep as macros so we do not need to define the commands */
5090 # define ftrace_startup(ops, command) \
5091 ({ \
5092 int ___ret = __register_ftrace_function(ops); \
5093 if (!___ret) \
5094 (ops)->flags |= FTRACE_OPS_FL_ENABLED; \
5095 ___ret; \
5096 })
5097 # define ftrace_shutdown(ops, command) \
5098 ({ \
5099 int ___ret = __unregister_ftrace_function(ops); \
5100 if (!___ret) \
5101 (ops)->flags &= ~FTRACE_OPS_FL_ENABLED; \
5102 ___ret; \
5103 })
5104
5105 # define ftrace_startup_sysctl() do { } while (0)
5106 # define ftrace_shutdown_sysctl() do { } while (0)
5107
5108 static inline int
ftrace_ops_test(struct ftrace_ops * ops,unsigned long ip,void * regs)5109 ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip, void *regs)
5110 {
5111 return 1;
5112 }
5113
ftrace_update_trampoline(struct ftrace_ops * ops)5114 static void ftrace_update_trampoline(struct ftrace_ops *ops)
5115 {
5116 }
5117
5118 #endif /* CONFIG_DYNAMIC_FTRACE */
5119
ftrace_init_global_array_ops(struct trace_array * tr)5120 __init void ftrace_init_global_array_ops(struct trace_array *tr)
5121 {
5122 tr->ops = &global_ops;
5123 tr->ops->private = tr;
5124 }
5125
ftrace_init_array_ops(struct trace_array * tr,ftrace_func_t func)5126 void ftrace_init_array_ops(struct trace_array *tr, ftrace_func_t func)
5127 {
5128 /* If we filter on pids, update to use the pid function */
5129 if (tr->flags & TRACE_ARRAY_FL_GLOBAL) {
5130 if (WARN_ON(tr->ops->func != ftrace_stub))
5131 printk("ftrace ops had %pS for function\n",
5132 tr->ops->func);
5133 }
5134 tr->ops->func = func;
5135 tr->ops->private = tr;
5136 }
5137
ftrace_reset_array_ops(struct trace_array * tr)5138 void ftrace_reset_array_ops(struct trace_array *tr)
5139 {
5140 tr->ops->func = ftrace_stub;
5141 }
5142
5143 static void
ftrace_ops_control_func(unsigned long ip,unsigned long parent_ip,struct ftrace_ops * op,struct pt_regs * regs)5144 ftrace_ops_control_func(unsigned long ip, unsigned long parent_ip,
5145 struct ftrace_ops *op, struct pt_regs *regs)
5146 {
5147 if (unlikely(trace_recursion_test(TRACE_CONTROL_BIT)))
5148 return;
5149
5150 /*
5151 * Some of the ops may be dynamically allocated,
5152 * they must be freed after a synchronize_sched().
5153 */
5154 preempt_disable_notrace();
5155 trace_recursion_set(TRACE_CONTROL_BIT);
5156
5157 /*
5158 * Control funcs (perf) uses RCU. Only trace if
5159 * RCU is currently active.
5160 */
5161 if (!rcu_is_watching())
5162 goto out;
5163
5164 do_for_each_ftrace_op(op, ftrace_control_list) {
5165 if (!(op->flags & FTRACE_OPS_FL_STUB) &&
5166 !ftrace_function_local_disabled(op) &&
5167 ftrace_ops_test(op, ip, regs))
5168 op->func(ip, parent_ip, op, regs);
5169 } while_for_each_ftrace_op(op);
5170 out:
5171 trace_recursion_clear(TRACE_CONTROL_BIT);
5172 preempt_enable_notrace();
5173 }
5174
5175 static struct ftrace_ops control_ops = {
5176 .func = ftrace_ops_control_func,
5177 .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_INITIALIZED,
5178 INIT_OPS_HASH(control_ops)
5179 };
5180
5181 static nokprobe_inline void
__ftrace_ops_list_func(unsigned long ip,unsigned long parent_ip,struct ftrace_ops * ignored,struct pt_regs * regs)5182 __ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
5183 struct ftrace_ops *ignored, struct pt_regs *regs)
5184 {
5185 struct ftrace_ops *op;
5186 int bit;
5187
5188 bit = trace_test_and_set_recursion(TRACE_LIST_START);
5189 if (bit < 0)
5190 return;
5191
5192 /*
5193 * Some of the ops may be dynamically allocated,
5194 * they must be freed after a synchronize_sched().
5195 */
5196 preempt_disable_notrace();
5197 do_for_each_ftrace_op(op, ftrace_ops_list) {
5198 if (ftrace_ops_test(op, ip, regs)) {
5199 if (FTRACE_WARN_ON(!op->func)) {
5200 pr_warn("op=%p %pS\n", op, op);
5201 goto out;
5202 }
5203 op->func(ip, parent_ip, op, regs);
5204 }
5205 } while_for_each_ftrace_op(op);
5206 out:
5207 preempt_enable_notrace();
5208 trace_clear_recursion(bit);
5209 }
5210
5211 /*
5212 * Some archs only support passing ip and parent_ip. Even though
5213 * the list function ignores the op parameter, we do not want any
5214 * C side effects, where a function is called without the caller
5215 * sending a third parameter.
5216 * Archs are to support both the regs and ftrace_ops at the same time.
5217 * If they support ftrace_ops, it is assumed they support regs.
5218 * If call backs want to use regs, they must either check for regs
5219 * being NULL, or CONFIG_DYNAMIC_FTRACE_WITH_REGS.
5220 * Note, CONFIG_DYNAMIC_FTRACE_WITH_REGS expects a full regs to be saved.
5221 * An architecture can pass partial regs with ftrace_ops and still
5222 * set the ARCH_SUPPORT_FTARCE_OPS.
5223 */
5224 #if ARCH_SUPPORTS_FTRACE_OPS
ftrace_ops_list_func(unsigned long ip,unsigned long parent_ip,struct ftrace_ops * op,struct pt_regs * regs)5225 static void ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
5226 struct ftrace_ops *op, struct pt_regs *regs)
5227 {
5228 __ftrace_ops_list_func(ip, parent_ip, NULL, regs);
5229 }
5230 NOKPROBE_SYMBOL(ftrace_ops_list_func);
5231 #else
ftrace_ops_no_ops(unsigned long ip,unsigned long parent_ip)5232 static void ftrace_ops_no_ops(unsigned long ip, unsigned long parent_ip)
5233 {
5234 __ftrace_ops_list_func(ip, parent_ip, NULL, NULL);
5235 }
5236 NOKPROBE_SYMBOL(ftrace_ops_no_ops);
5237 #endif
5238
5239 /*
5240 * If there's only one function registered but it does not support
5241 * recursion, this function will be called by the mcount trampoline.
5242 * This function will handle recursion protection.
5243 */
ftrace_ops_recurs_func(unsigned long ip,unsigned long parent_ip,struct ftrace_ops * op,struct pt_regs * regs)5244 static void ftrace_ops_recurs_func(unsigned long ip, unsigned long parent_ip,
5245 struct ftrace_ops *op, struct pt_regs *regs)
5246 {
5247 int bit;
5248
5249 bit = trace_test_and_set_recursion(TRACE_LIST_START);
5250 if (bit < 0)
5251 return;
5252
5253 op->func(ip, parent_ip, op, regs);
5254
5255 trace_clear_recursion(bit);
5256 }
5257 NOKPROBE_SYMBOL(ftrace_ops_recurs_func);
5258
5259 /**
5260 * ftrace_ops_get_func - get the function a trampoline should call
5261 * @ops: the ops to get the function for
5262 *
5263 * Normally the mcount trampoline will call the ops->func, but there
5264 * are times that it should not. For example, if the ops does not
5265 * have its own recursion protection, then it should call the
5266 * ftrace_ops_recurs_func() instead.
5267 *
5268 * Returns the function that the trampoline should call for @ops.
5269 */
ftrace_ops_get_func(struct ftrace_ops * ops)5270 ftrace_func_t ftrace_ops_get_func(struct ftrace_ops *ops)
5271 {
5272 /*
5273 * If the func handles its own recursion, call it directly.
5274 * Otherwise call the recursion protected function that
5275 * will call the ftrace ops function.
5276 */
5277 if (!(ops->flags & FTRACE_OPS_FL_RECURSION_SAFE))
5278 return ftrace_ops_recurs_func;
5279
5280 return ops->func;
5281 }
5282
clear_ftrace_swapper(void)5283 static void clear_ftrace_swapper(void)
5284 {
5285 struct task_struct *p;
5286 int cpu;
5287
5288 get_online_cpus();
5289 for_each_online_cpu(cpu) {
5290 p = idle_task(cpu);
5291 clear_tsk_trace_trace(p);
5292 }
5293 put_online_cpus();
5294 }
5295
set_ftrace_swapper(void)5296 static void set_ftrace_swapper(void)
5297 {
5298 struct task_struct *p;
5299 int cpu;
5300
5301 get_online_cpus();
5302 for_each_online_cpu(cpu) {
5303 p = idle_task(cpu);
5304 set_tsk_trace_trace(p);
5305 }
5306 put_online_cpus();
5307 }
5308
clear_ftrace_pid(struct pid * pid)5309 static void clear_ftrace_pid(struct pid *pid)
5310 {
5311 struct task_struct *p;
5312
5313 rcu_read_lock();
5314 do_each_pid_task(pid, PIDTYPE_PID, p) {
5315 clear_tsk_trace_trace(p);
5316 } while_each_pid_task(pid, PIDTYPE_PID, p);
5317 rcu_read_unlock();
5318
5319 put_pid(pid);
5320 }
5321
set_ftrace_pid(struct pid * pid)5322 static void set_ftrace_pid(struct pid *pid)
5323 {
5324 struct task_struct *p;
5325
5326 rcu_read_lock();
5327 do_each_pid_task(pid, PIDTYPE_PID, p) {
5328 set_tsk_trace_trace(p);
5329 } while_each_pid_task(pid, PIDTYPE_PID, p);
5330 rcu_read_unlock();
5331 }
5332
clear_ftrace_pid_task(struct pid * pid)5333 static void clear_ftrace_pid_task(struct pid *pid)
5334 {
5335 if (pid == ftrace_swapper_pid)
5336 clear_ftrace_swapper();
5337 else
5338 clear_ftrace_pid(pid);
5339 }
5340
set_ftrace_pid_task(struct pid * pid)5341 static void set_ftrace_pid_task(struct pid *pid)
5342 {
5343 if (pid == ftrace_swapper_pid)
5344 set_ftrace_swapper();
5345 else
5346 set_ftrace_pid(pid);
5347 }
5348
ftrace_pid_add(int p)5349 static int ftrace_pid_add(int p)
5350 {
5351 struct pid *pid;
5352 struct ftrace_pid *fpid;
5353 int ret = -EINVAL;
5354
5355 mutex_lock(&ftrace_lock);
5356
5357 if (!p)
5358 pid = ftrace_swapper_pid;
5359 else
5360 pid = find_get_pid(p);
5361
5362 if (!pid)
5363 goto out;
5364
5365 ret = 0;
5366
5367 list_for_each_entry(fpid, &ftrace_pids, list)
5368 if (fpid->pid == pid)
5369 goto out_put;
5370
5371 ret = -ENOMEM;
5372
5373 fpid = kmalloc(sizeof(*fpid), GFP_KERNEL);
5374 if (!fpid)
5375 goto out_put;
5376
5377 list_add(&fpid->list, &ftrace_pids);
5378 fpid->pid = pid;
5379
5380 set_ftrace_pid_task(pid);
5381
5382 ftrace_update_pid_func();
5383
5384 ftrace_startup_all(0);
5385
5386 mutex_unlock(&ftrace_lock);
5387 return 0;
5388
5389 out_put:
5390 if (pid != ftrace_swapper_pid)
5391 put_pid(pid);
5392
5393 out:
5394 mutex_unlock(&ftrace_lock);
5395 return ret;
5396 }
5397
ftrace_pid_reset(void)5398 static void ftrace_pid_reset(void)
5399 {
5400 struct ftrace_pid *fpid, *safe;
5401
5402 mutex_lock(&ftrace_lock);
5403 list_for_each_entry_safe(fpid, safe, &ftrace_pids, list) {
5404 struct pid *pid = fpid->pid;
5405
5406 clear_ftrace_pid_task(pid);
5407
5408 list_del(&fpid->list);
5409 kfree(fpid);
5410 }
5411
5412 ftrace_update_pid_func();
5413 ftrace_startup_all(0);
5414
5415 mutex_unlock(&ftrace_lock);
5416 }
5417
fpid_start(struct seq_file * m,loff_t * pos)5418 static void *fpid_start(struct seq_file *m, loff_t *pos)
5419 {
5420 mutex_lock(&ftrace_lock);
5421
5422 if (!ftrace_pids_enabled() && (!*pos))
5423 return (void *) 1;
5424
5425 return seq_list_start(&ftrace_pids, *pos);
5426 }
5427
fpid_next(struct seq_file * m,void * v,loff_t * pos)5428 static void *fpid_next(struct seq_file *m, void *v, loff_t *pos)
5429 {
5430 if (v == (void *)1)
5431 return NULL;
5432
5433 return seq_list_next(v, &ftrace_pids, pos);
5434 }
5435
fpid_stop(struct seq_file * m,void * p)5436 static void fpid_stop(struct seq_file *m, void *p)
5437 {
5438 mutex_unlock(&ftrace_lock);
5439 }
5440
fpid_show(struct seq_file * m,void * v)5441 static int fpid_show(struct seq_file *m, void *v)
5442 {
5443 const struct ftrace_pid *fpid = list_entry(v, struct ftrace_pid, list);
5444
5445 if (v == (void *)1) {
5446 seq_puts(m, "no pid\n");
5447 return 0;
5448 }
5449
5450 if (fpid->pid == ftrace_swapper_pid)
5451 seq_puts(m, "swapper tasks\n");
5452 else
5453 seq_printf(m, "%u\n", pid_vnr(fpid->pid));
5454
5455 return 0;
5456 }
5457
5458 static const struct seq_operations ftrace_pid_sops = {
5459 .start = fpid_start,
5460 .next = fpid_next,
5461 .stop = fpid_stop,
5462 .show = fpid_show,
5463 };
5464
5465 static int
ftrace_pid_open(struct inode * inode,struct file * file)5466 ftrace_pid_open(struct inode *inode, struct file *file)
5467 {
5468 int ret = 0;
5469
5470 if ((file->f_mode & FMODE_WRITE) &&
5471 (file->f_flags & O_TRUNC))
5472 ftrace_pid_reset();
5473
5474 if (file->f_mode & FMODE_READ)
5475 ret = seq_open(file, &ftrace_pid_sops);
5476
5477 return ret;
5478 }
5479
5480 static ssize_t
ftrace_pid_write(struct file * filp,const char __user * ubuf,size_t cnt,loff_t * ppos)5481 ftrace_pid_write(struct file *filp, const char __user *ubuf,
5482 size_t cnt, loff_t *ppos)
5483 {
5484 char buf[64], *tmp;
5485 long val;
5486 int ret;
5487
5488 if (cnt >= sizeof(buf))
5489 return -EINVAL;
5490
5491 if (copy_from_user(&buf, ubuf, cnt))
5492 return -EFAULT;
5493
5494 buf[cnt] = 0;
5495
5496 /*
5497 * Allow "echo > set_ftrace_pid" or "echo -n '' > set_ftrace_pid"
5498 * to clean the filter quietly.
5499 */
5500 tmp = strstrip(buf);
5501 if (strlen(tmp) == 0)
5502 return 1;
5503
5504 ret = kstrtol(tmp, 10, &val);
5505 if (ret < 0)
5506 return ret;
5507
5508 ret = ftrace_pid_add(val);
5509
5510 return ret ? ret : cnt;
5511 }
5512
5513 static int
ftrace_pid_release(struct inode * inode,struct file * file)5514 ftrace_pid_release(struct inode *inode, struct file *file)
5515 {
5516 if (file->f_mode & FMODE_READ)
5517 seq_release(inode, file);
5518
5519 return 0;
5520 }
5521
5522 static const struct file_operations ftrace_pid_fops = {
5523 .open = ftrace_pid_open,
5524 .write = ftrace_pid_write,
5525 .read = seq_read,
5526 .llseek = tracing_lseek,
5527 .release = ftrace_pid_release,
5528 };
5529
ftrace_init_tracefs(void)5530 static __init int ftrace_init_tracefs(void)
5531 {
5532 struct dentry *d_tracer;
5533
5534 d_tracer = tracing_init_dentry();
5535 if (IS_ERR(d_tracer))
5536 return 0;
5537
5538 ftrace_init_dyn_tracefs(d_tracer);
5539
5540 trace_create_file("set_ftrace_pid", 0644, d_tracer,
5541 NULL, &ftrace_pid_fops);
5542
5543 ftrace_profile_tracefs(d_tracer);
5544
5545 return 0;
5546 }
5547 fs_initcall(ftrace_init_tracefs);
5548
5549 /**
5550 * ftrace_kill - kill ftrace
5551 *
5552 * This function should be used by panic code. It stops ftrace
5553 * but in a not so nice way. If you need to simply kill ftrace
5554 * from a non-atomic section, use ftrace_kill.
5555 */
ftrace_kill(void)5556 void ftrace_kill(void)
5557 {
5558 ftrace_disabled = 1;
5559 ftrace_enabled = 0;
5560 clear_ftrace_function();
5561 }
5562
5563 /**
5564 * Test if ftrace is dead or not.
5565 */
ftrace_is_dead(void)5566 int ftrace_is_dead(void)
5567 {
5568 return ftrace_disabled;
5569 }
5570
5571 /**
5572 * register_ftrace_function - register a function for profiling
5573 * @ops - ops structure that holds the function for profiling.
5574 *
5575 * Register a function to be called by all functions in the
5576 * kernel.
5577 *
5578 * Note: @ops->func and all the functions it calls must be labeled
5579 * with "notrace", otherwise it will go into a
5580 * recursive loop.
5581 */
register_ftrace_function(struct ftrace_ops * ops)5582 int register_ftrace_function(struct ftrace_ops *ops)
5583 {
5584 int ret = -1;
5585
5586 ftrace_ops_init(ops);
5587
5588 mutex_lock(&ftrace_lock);
5589
5590 ret = ftrace_startup(ops, 0);
5591
5592 mutex_unlock(&ftrace_lock);
5593
5594 return ret;
5595 }
5596 EXPORT_SYMBOL_GPL(register_ftrace_function);
5597
5598 /**
5599 * unregister_ftrace_function - unregister a function for profiling.
5600 * @ops - ops structure that holds the function to unregister
5601 *
5602 * Unregister a function that was added to be called by ftrace profiling.
5603 */
unregister_ftrace_function(struct ftrace_ops * ops)5604 int unregister_ftrace_function(struct ftrace_ops *ops)
5605 {
5606 int ret;
5607
5608 mutex_lock(&ftrace_lock);
5609 ret = ftrace_shutdown(ops, 0);
5610 mutex_unlock(&ftrace_lock);
5611
5612 return ret;
5613 }
5614 EXPORT_SYMBOL_GPL(unregister_ftrace_function);
5615
5616 int
ftrace_enable_sysctl(struct ctl_table * table,int write,void __user * buffer,size_t * lenp,loff_t * ppos)5617 ftrace_enable_sysctl(struct ctl_table *table, int write,
5618 void __user *buffer, size_t *lenp,
5619 loff_t *ppos)
5620 {
5621 int ret = -ENODEV;
5622
5623 mutex_lock(&ftrace_lock);
5624
5625 if (unlikely(ftrace_disabled))
5626 goto out;
5627
5628 ret = proc_dointvec(table, write, buffer, lenp, ppos);
5629
5630 if (ret || !write || (last_ftrace_enabled == !!ftrace_enabled))
5631 goto out;
5632
5633 last_ftrace_enabled = !!ftrace_enabled;
5634
5635 if (ftrace_enabled) {
5636
5637 /* we are starting ftrace again */
5638 if (ftrace_ops_list != &ftrace_list_end)
5639 update_ftrace_function();
5640
5641 ftrace_startup_sysctl();
5642
5643 } else {
5644 /* stopping ftrace calls (just send to ftrace_stub) */
5645 ftrace_trace_function = ftrace_stub;
5646
5647 ftrace_shutdown_sysctl();
5648 }
5649
5650 out:
5651 mutex_unlock(&ftrace_lock);
5652 return ret;
5653 }
5654
5655 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
5656
5657 static struct ftrace_ops graph_ops = {
5658 .func = ftrace_stub,
5659 .flags = FTRACE_OPS_FL_RECURSION_SAFE |
5660 FTRACE_OPS_FL_INITIALIZED |
5661 FTRACE_OPS_FL_PID |
5662 FTRACE_OPS_FL_STUB,
5663 #ifdef FTRACE_GRAPH_TRAMP_ADDR
5664 .trampoline = FTRACE_GRAPH_TRAMP_ADDR,
5665 /* trampoline_size is only needed for dynamically allocated tramps */
5666 #endif
5667 ASSIGN_OPS_HASH(graph_ops, &global_ops.local_hash)
5668 };
5669
ftrace_graph_sleep_time_control(bool enable)5670 void ftrace_graph_sleep_time_control(bool enable)
5671 {
5672 fgraph_sleep_time = enable;
5673 }
5674
ftrace_graph_graph_time_control(bool enable)5675 void ftrace_graph_graph_time_control(bool enable)
5676 {
5677 fgraph_graph_time = enable;
5678 }
5679
ftrace_graph_entry_stub(struct ftrace_graph_ent * trace)5680 int ftrace_graph_entry_stub(struct ftrace_graph_ent *trace)
5681 {
5682 return 0;
5683 }
5684
5685 /* The callbacks that hook a function */
5686 trace_func_graph_ret_t ftrace_graph_return =
5687 (trace_func_graph_ret_t)ftrace_stub;
5688 trace_func_graph_ent_t ftrace_graph_entry = ftrace_graph_entry_stub;
5689 static trace_func_graph_ent_t __ftrace_graph_entry = ftrace_graph_entry_stub;
5690
5691 /* Try to assign a return stack array on FTRACE_RETSTACK_ALLOC_SIZE tasks. */
alloc_retstack_tasklist(struct ftrace_ret_stack ** ret_stack_list)5692 static int alloc_retstack_tasklist(struct ftrace_ret_stack **ret_stack_list)
5693 {
5694 int i;
5695 int ret = 0;
5696 unsigned long flags;
5697 int start = 0, end = FTRACE_RETSTACK_ALLOC_SIZE;
5698 struct task_struct *g, *t;
5699
5700 for (i = 0; i < FTRACE_RETSTACK_ALLOC_SIZE; i++) {
5701 ret_stack_list[i] = kmalloc(FTRACE_RETFUNC_DEPTH
5702 * sizeof(struct ftrace_ret_stack),
5703 GFP_KERNEL);
5704 if (!ret_stack_list[i]) {
5705 start = 0;
5706 end = i;
5707 ret = -ENOMEM;
5708 goto free;
5709 }
5710 }
5711
5712 read_lock_irqsave(&tasklist_lock, flags);
5713 do_each_thread(g, t) {
5714 if (start == end) {
5715 ret = -EAGAIN;
5716 goto unlock;
5717 }
5718
5719 if (t->ret_stack == NULL) {
5720 atomic_set(&t->trace_overrun, 0);
5721 t->curr_ret_stack = -1;
5722 /* Make sure the tasks see the -1 first: */
5723 smp_wmb();
5724 t->ret_stack = ret_stack_list[start++];
5725 }
5726 } while_each_thread(g, t);
5727
5728 unlock:
5729 read_unlock_irqrestore(&tasklist_lock, flags);
5730 free:
5731 for (i = start; i < end; i++)
5732 kfree(ret_stack_list[i]);
5733 return ret;
5734 }
5735
5736 static void
ftrace_graph_probe_sched_switch(void * ignore,bool preempt,struct task_struct * prev,struct task_struct * next)5737 ftrace_graph_probe_sched_switch(void *ignore, bool preempt,
5738 struct task_struct *prev, struct task_struct *next)
5739 {
5740 unsigned long long timestamp;
5741 int index;
5742
5743 /*
5744 * Does the user want to count the time a function was asleep.
5745 * If so, do not update the time stamps.
5746 */
5747 if (fgraph_sleep_time)
5748 return;
5749
5750 timestamp = trace_clock_local();
5751
5752 prev->ftrace_timestamp = timestamp;
5753
5754 /* only process tasks that we timestamped */
5755 if (!next->ftrace_timestamp)
5756 return;
5757
5758 /*
5759 * Update all the counters in next to make up for the
5760 * time next was sleeping.
5761 */
5762 timestamp -= next->ftrace_timestamp;
5763
5764 for (index = next->curr_ret_stack; index >= 0; index--)
5765 next->ret_stack[index].calltime += timestamp;
5766 }
5767
5768 /* Allocate a return stack for each task */
start_graph_tracing(void)5769 static int start_graph_tracing(void)
5770 {
5771 struct ftrace_ret_stack **ret_stack_list;
5772 int ret, cpu;
5773
5774 ret_stack_list = kmalloc(FTRACE_RETSTACK_ALLOC_SIZE *
5775 sizeof(struct ftrace_ret_stack *),
5776 GFP_KERNEL);
5777
5778 if (!ret_stack_list)
5779 return -ENOMEM;
5780
5781 /* The cpu_boot init_task->ret_stack will never be freed */
5782 for_each_online_cpu(cpu) {
5783 if (!idle_task(cpu)->ret_stack)
5784 ftrace_graph_init_idle_task(idle_task(cpu), cpu);
5785 }
5786
5787 do {
5788 ret = alloc_retstack_tasklist(ret_stack_list);
5789 } while (ret == -EAGAIN);
5790
5791 if (!ret) {
5792 ret = register_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
5793 if (ret)
5794 pr_info("ftrace_graph: Couldn't activate tracepoint"
5795 " probe to kernel_sched_switch\n");
5796 }
5797
5798 kfree(ret_stack_list);
5799 return ret;
5800 }
5801
5802 /*
5803 * Hibernation protection.
5804 * The state of the current task is too much unstable during
5805 * suspend/restore to disk. We want to protect against that.
5806 */
5807 static int
ftrace_suspend_notifier_call(struct notifier_block * bl,unsigned long state,void * unused)5808 ftrace_suspend_notifier_call(struct notifier_block *bl, unsigned long state,
5809 void *unused)
5810 {
5811 switch (state) {
5812 case PM_HIBERNATION_PREPARE:
5813 pause_graph_tracing();
5814 break;
5815
5816 case PM_POST_HIBERNATION:
5817 unpause_graph_tracing();
5818 break;
5819 }
5820 return NOTIFY_DONE;
5821 }
5822
ftrace_graph_entry_test(struct ftrace_graph_ent * trace)5823 static int ftrace_graph_entry_test(struct ftrace_graph_ent *trace)
5824 {
5825 if (!ftrace_ops_test(&global_ops, trace->func, NULL))
5826 return 0;
5827 return __ftrace_graph_entry(trace);
5828 }
5829
5830 /*
5831 * The function graph tracer should only trace the functions defined
5832 * by set_ftrace_filter and set_ftrace_notrace. If another function
5833 * tracer ops is registered, the graph tracer requires testing the
5834 * function against the global ops, and not just trace any function
5835 * that any ftrace_ops registered.
5836 */
update_function_graph_func(void)5837 static void update_function_graph_func(void)
5838 {
5839 struct ftrace_ops *op;
5840 bool do_test = false;
5841
5842 /*
5843 * The graph and global ops share the same set of functions
5844 * to test. If any other ops is on the list, then
5845 * the graph tracing needs to test if its the function
5846 * it should call.
5847 */
5848 do_for_each_ftrace_op(op, ftrace_ops_list) {
5849 if (op != &global_ops && op != &graph_ops &&
5850 op != &ftrace_list_end) {
5851 do_test = true;
5852 /* in double loop, break out with goto */
5853 goto out;
5854 }
5855 } while_for_each_ftrace_op(op);
5856 out:
5857 if (do_test)
5858 ftrace_graph_entry = ftrace_graph_entry_test;
5859 else
5860 ftrace_graph_entry = __ftrace_graph_entry;
5861 }
5862
5863 static struct notifier_block ftrace_suspend_notifier = {
5864 .notifier_call = ftrace_suspend_notifier_call,
5865 };
5866
register_ftrace_graph(trace_func_graph_ret_t retfunc,trace_func_graph_ent_t entryfunc)5867 int register_ftrace_graph(trace_func_graph_ret_t retfunc,
5868 trace_func_graph_ent_t entryfunc)
5869 {
5870 int ret = 0;
5871
5872 mutex_lock(&ftrace_lock);
5873
5874 /* we currently allow only one tracer registered at a time */
5875 if (ftrace_graph_active) {
5876 ret = -EBUSY;
5877 goto out;
5878 }
5879
5880 register_pm_notifier(&ftrace_suspend_notifier);
5881
5882 ftrace_graph_active++;
5883 ret = start_graph_tracing();
5884 if (ret) {
5885 ftrace_graph_active--;
5886 goto out;
5887 }
5888
5889 ftrace_graph_return = retfunc;
5890
5891 /*
5892 * Update the indirect function to the entryfunc, and the
5893 * function that gets called to the entry_test first. Then
5894 * call the update fgraph entry function to determine if
5895 * the entryfunc should be called directly or not.
5896 */
5897 __ftrace_graph_entry = entryfunc;
5898 ftrace_graph_entry = ftrace_graph_entry_test;
5899 update_function_graph_func();
5900
5901 ret = ftrace_startup(&graph_ops, FTRACE_START_FUNC_RET);
5902 out:
5903 mutex_unlock(&ftrace_lock);
5904 return ret;
5905 }
5906
unregister_ftrace_graph(void)5907 void unregister_ftrace_graph(void)
5908 {
5909 mutex_lock(&ftrace_lock);
5910
5911 if (unlikely(!ftrace_graph_active))
5912 goto out;
5913
5914 ftrace_graph_active--;
5915 ftrace_graph_return = (trace_func_graph_ret_t)ftrace_stub;
5916 ftrace_graph_entry = ftrace_graph_entry_stub;
5917 __ftrace_graph_entry = ftrace_graph_entry_stub;
5918 ftrace_shutdown(&graph_ops, FTRACE_STOP_FUNC_RET);
5919 unregister_pm_notifier(&ftrace_suspend_notifier);
5920 unregister_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
5921
5922 out:
5923 mutex_unlock(&ftrace_lock);
5924 }
5925
5926 static DEFINE_PER_CPU(struct ftrace_ret_stack *, idle_ret_stack);
5927
5928 static void
graph_init_task(struct task_struct * t,struct ftrace_ret_stack * ret_stack)5929 graph_init_task(struct task_struct *t, struct ftrace_ret_stack *ret_stack)
5930 {
5931 atomic_set(&t->trace_overrun, 0);
5932 t->ftrace_timestamp = 0;
5933 /* make curr_ret_stack visible before we add the ret_stack */
5934 smp_wmb();
5935 t->ret_stack = ret_stack;
5936 }
5937
5938 /*
5939 * Allocate a return stack for the idle task. May be the first
5940 * time through, or it may be done by CPU hotplug online.
5941 */
ftrace_graph_init_idle_task(struct task_struct * t,int cpu)5942 void ftrace_graph_init_idle_task(struct task_struct *t, int cpu)
5943 {
5944 t->curr_ret_stack = -1;
5945 /*
5946 * The idle task has no parent, it either has its own
5947 * stack or no stack at all.
5948 */
5949 if (t->ret_stack)
5950 WARN_ON(t->ret_stack != per_cpu(idle_ret_stack, cpu));
5951
5952 if (ftrace_graph_active) {
5953 struct ftrace_ret_stack *ret_stack;
5954
5955 ret_stack = per_cpu(idle_ret_stack, cpu);
5956 if (!ret_stack) {
5957 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
5958 * sizeof(struct ftrace_ret_stack),
5959 GFP_KERNEL);
5960 if (!ret_stack)
5961 return;
5962 per_cpu(idle_ret_stack, cpu) = ret_stack;
5963 }
5964 graph_init_task(t, ret_stack);
5965 }
5966 }
5967
5968 /* Allocate a return stack for newly created task */
ftrace_graph_init_task(struct task_struct * t)5969 void ftrace_graph_init_task(struct task_struct *t)
5970 {
5971 /* Make sure we do not use the parent ret_stack */
5972 t->ret_stack = NULL;
5973 t->curr_ret_stack = -1;
5974
5975 if (ftrace_graph_active) {
5976 struct ftrace_ret_stack *ret_stack;
5977
5978 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
5979 * sizeof(struct ftrace_ret_stack),
5980 GFP_KERNEL);
5981 if (!ret_stack)
5982 return;
5983 graph_init_task(t, ret_stack);
5984 }
5985 }
5986
ftrace_graph_exit_task(struct task_struct * t)5987 void ftrace_graph_exit_task(struct task_struct *t)
5988 {
5989 struct ftrace_ret_stack *ret_stack = t->ret_stack;
5990
5991 t->ret_stack = NULL;
5992 /* NULL must become visible to IRQs before we free it: */
5993 barrier();
5994
5995 kfree(ret_stack);
5996 }
5997 #endif
5998