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