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