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