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