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