]> git.proxmox.com Git - mirror_ubuntu-focal-kernel.git/blame - kernel/trace/ftrace.c
tracing: Convert pr_warning() to pr_warn() in trace_events.c
[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;
1309 int i;
1310
1311 /*
1312 * If the new source is empty, just free dst and assign it
1313 * the empty_hash.
1314 */
1315 if (!src->count) {
5c27c775
MH
1316 new_hash = EMPTY_HASH;
1317 goto update;
33dc9b12
SR
1318 }
1319
33dc9b12
SR
1320 /*
1321 * Make the hash size about 1/2 the # found
1322 */
1323 for (size /= 2; size; size >>= 1)
1324 bits++;
1325
1326 /* Don't allocate too much */
1327 if (bits > FTRACE_HASH_MAX_BITS)
1328 bits = FTRACE_HASH_MAX_BITS;
1329
07fd5515
SR
1330 new_hash = alloc_ftrace_hash(bits);
1331 if (!new_hash)
5c27c775 1332 return -ENOMEM;
33dc9b12
SR
1333
1334 size = 1 << src->size_bits;
1335 for (i = 0; i < size; i++) {
1336 hhd = &src->buckets[i];
b67bfe0d 1337 hlist_for_each_entry_safe(entry, tn, hhd, hlist) {
33dc9b12 1338 remove_hash_entry(src, entry);
07fd5515 1339 __add_hash_entry(new_hash, entry);
33dc9b12
SR
1340 }
1341 }
1342
5c27c775
MH
1343update:
1344 /*
1345 * Remove the current set, update the hash and add
1346 * them back.
1347 */
1348 ftrace_hash_rec_disable(ops, enable);
1349
07fd5515
SR
1350 old_hash = *dst;
1351 rcu_assign_pointer(*dst, new_hash);
1352 free_ftrace_hash_rcu(old_hash);
1353
41fb61c2
SR
1354 ftrace_hash_rec_enable(ops, enable);
1355
5c27c775 1356 return 0;
33dc9b12
SR
1357}
1358
b848914c
SR
1359/*
1360 * Test the hashes for this ops to see if we want to call
1361 * the ops->func or not.
1362 *
1363 * It's a match if the ip is in the ops->filter_hash or
1364 * the filter_hash does not exist or is empty,
1365 * AND
1366 * the ip is not in the ops->notrace_hash.
cdbe61bf
SR
1367 *
1368 * This needs to be called with preemption disabled as
1369 * the hashes are freed with call_rcu_sched().
b848914c
SR
1370 */
1371static int
195a8afc 1372ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip, void *regs)
b848914c
SR
1373{
1374 struct ftrace_hash *filter_hash;
1375 struct ftrace_hash *notrace_hash;
1376 int ret;
1377
195a8afc
SRRH
1378#ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS
1379 /*
1380 * There's a small race when adding ops that the ftrace handler
1381 * that wants regs, may be called without them. We can not
1382 * allow that handler to be called if regs is NULL.
1383 */
1384 if (regs == NULL && (ops->flags & FTRACE_OPS_FL_SAVE_REGS))
1385 return 0;
1386#endif
1387
1bb539ca
SR
1388 filter_hash = rcu_dereference_raw_notrace(ops->filter_hash);
1389 notrace_hash = rcu_dereference_raw_notrace(ops->notrace_hash);
b848914c 1390
06a51d93 1391 if ((ftrace_hash_empty(filter_hash) ||
b848914c 1392 ftrace_lookup_ip(filter_hash, ip)) &&
06a51d93 1393 (ftrace_hash_empty(notrace_hash) ||
b848914c
SR
1394 !ftrace_lookup_ip(notrace_hash, ip)))
1395 ret = 1;
1396 else
1397 ret = 0;
b848914c
SR
1398
1399 return ret;
1400}
1401
493762fc
SR
1402/*
1403 * This is a double for. Do not use 'break' to break out of the loop,
1404 * you must use a goto.
1405 */
1406#define do_for_each_ftrace_rec(pg, rec) \
1407 for (pg = ftrace_pages_start; pg; pg = pg->next) { \
1408 int _____i; \
1409 for (_____i = 0; _____i < pg->index; _____i++) { \
1410 rec = &pg->records[_____i];
1411
1412#define while_for_each_ftrace_rec() \
1413 } \
1414 }
1415
5855fead
SR
1416
1417static int ftrace_cmp_recs(const void *a, const void *b)
1418{
a650e02a
SR
1419 const struct dyn_ftrace *key = a;
1420 const struct dyn_ftrace *rec = b;
5855fead 1421
a650e02a 1422 if (key->flags < rec->ip)
5855fead 1423 return -1;
a650e02a
SR
1424 if (key->ip >= rec->ip + MCOUNT_INSN_SIZE)
1425 return 1;
5855fead
SR
1426 return 0;
1427}
1428
f0cf973a 1429static unsigned long ftrace_location_range(unsigned long start, unsigned long end)
c88fd863
SR
1430{
1431 struct ftrace_page *pg;
1432 struct dyn_ftrace *rec;
5855fead 1433 struct dyn_ftrace key;
c88fd863 1434
a650e02a
SR
1435 key.ip = start;
1436 key.flags = end; /* overload flags, as it is unsigned long */
5855fead
SR
1437
1438 for (pg = ftrace_pages_start; pg; pg = pg->next) {
a650e02a
SR
1439 if (end < pg->records[0].ip ||
1440 start >= (pg->records[pg->index - 1].ip + MCOUNT_INSN_SIZE))
9644302e 1441 continue;
5855fead
SR
1442 rec = bsearch(&key, pg->records, pg->index,
1443 sizeof(struct dyn_ftrace),
1444 ftrace_cmp_recs);
1445 if (rec)
f0cf973a 1446 return rec->ip;
5855fead 1447 }
c88fd863
SR
1448
1449 return 0;
1450}
1451
a650e02a
SR
1452/**
1453 * ftrace_location - return true if the ip giving is a traced location
1454 * @ip: the instruction pointer to check
1455 *
f0cf973a 1456 * Returns rec->ip if @ip given is a pointer to a ftrace location.
a650e02a
SR
1457 * That is, the instruction that is either a NOP or call to
1458 * the function tracer. It checks the ftrace internal tables to
1459 * determine if the address belongs or not.
1460 */
f0cf973a 1461unsigned long ftrace_location(unsigned long ip)
a650e02a
SR
1462{
1463 return ftrace_location_range(ip, ip);
1464}
1465
1466/**
1467 * ftrace_text_reserved - return true if range contains an ftrace location
1468 * @start: start of range to search
1469 * @end: end of range to search (inclusive). @end points to the last byte to check.
1470 *
1471 * Returns 1 if @start and @end contains a ftrace location.
1472 * That is, the instruction that is either a NOP or call to
1473 * the function tracer. It checks the ftrace internal tables to
1474 * determine if the address belongs or not.
1475 */
d88471cb 1476int ftrace_text_reserved(const void *start, const void *end)
a650e02a 1477{
f0cf973a
SR
1478 unsigned long ret;
1479
1480 ret = ftrace_location_range((unsigned long)start,
1481 (unsigned long)end);
1482
1483 return (int)!!ret;
a650e02a
SR
1484}
1485
4fbb48cb
SRRH
1486/* Test if ops registered to this rec needs regs */
1487static bool test_rec_ops_needs_regs(struct dyn_ftrace *rec)
1488{
1489 struct ftrace_ops *ops;
1490 bool keep_regs = false;
1491
1492 for (ops = ftrace_ops_list;
1493 ops != &ftrace_list_end; ops = ops->next) {
1494 /* pass rec in as regs to have non-NULL val */
1495 if (ftrace_ops_test(ops, rec->ip, rec)) {
1496 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS) {
1497 keep_regs = true;
1498 break;
1499 }
1500 }
1501 }
1502
1503 return keep_regs;
1504}
1505
79922b80
SRRH
1506static void ftrace_remove_tramp(struct ftrace_ops *ops,
1507 struct dyn_ftrace *rec)
1508{
1509 struct ftrace_func_entry *entry;
1510
1511 entry = ftrace_lookup_ip(ops->tramp_hash, rec->ip);
1512 if (!entry)
1513 return;
1514
1515 /*
1516 * The tramp_hash entry will be removed at time
1517 * of update.
1518 */
1519 ops->trampolines--;
1520 rec->flags &= ~FTRACE_FL_TRAMP;
1521}
1522
1523static void ftrace_clear_tramps(struct dyn_ftrace *rec)
1524{
1525 struct ftrace_ops *op;
1526
1527 do_for_each_ftrace_op(op, ftrace_ops_list) {
1528 if (op->trampolines)
1529 ftrace_remove_tramp(op, rec);
1530 } while_for_each_ftrace_op(op);
1531}
1532
ed926f9b
SR
1533static void __ftrace_hash_rec_update(struct ftrace_ops *ops,
1534 int filter_hash,
1535 bool inc)
1536{
1537 struct ftrace_hash *hash;
1538 struct ftrace_hash *other_hash;
1539 struct ftrace_page *pg;
1540 struct dyn_ftrace *rec;
1541 int count = 0;
1542 int all = 0;
1543
1544 /* Only update if the ops has been registered */
1545 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
1546 return;
1547
1548 /*
1549 * In the filter_hash case:
1550 * If the count is zero, we update all records.
1551 * Otherwise we just update the items in the hash.
1552 *
1553 * In the notrace_hash case:
1554 * We enable the update in the hash.
1555 * As disabling notrace means enabling the tracing,
1556 * and enabling notrace means disabling, the inc variable
1557 * gets inversed.
1558 */
1559 if (filter_hash) {
1560 hash = ops->filter_hash;
1561 other_hash = ops->notrace_hash;
06a51d93 1562 if (ftrace_hash_empty(hash))
ed926f9b
SR
1563 all = 1;
1564 } else {
1565 inc = !inc;
1566 hash = ops->notrace_hash;
1567 other_hash = ops->filter_hash;
1568 /*
1569 * If the notrace hash has no items,
1570 * then there's nothing to do.
1571 */
06a51d93 1572 if (ftrace_hash_empty(hash))
ed926f9b
SR
1573 return;
1574 }
1575
1576 do_for_each_ftrace_rec(pg, rec) {
1577 int in_other_hash = 0;
1578 int in_hash = 0;
1579 int match = 0;
1580
1581 if (all) {
1582 /*
1583 * Only the filter_hash affects all records.
1584 * Update if the record is not in the notrace hash.
1585 */
b848914c 1586 if (!other_hash || !ftrace_lookup_ip(other_hash, rec->ip))
ed926f9b
SR
1587 match = 1;
1588 } else {
06a51d93
SR
1589 in_hash = !!ftrace_lookup_ip(hash, rec->ip);
1590 in_other_hash = !!ftrace_lookup_ip(other_hash, rec->ip);
ed926f9b
SR
1591
1592 /*
19eab4a4
SRRH
1593 * If filter_hash is set, we want to match all functions
1594 * that are in the hash but not in the other hash.
ed926f9b 1595 *
19eab4a4
SRRH
1596 * If filter_hash is not set, then we are decrementing.
1597 * That means we match anything that is in the hash
1598 * and also in the other_hash. That is, we need to turn
1599 * off functions in the other hash because they are disabled
1600 * by this hash.
ed926f9b
SR
1601 */
1602 if (filter_hash && in_hash && !in_other_hash)
1603 match = 1;
1604 else if (!filter_hash && in_hash &&
06a51d93 1605 (in_other_hash || ftrace_hash_empty(other_hash)))
ed926f9b
SR
1606 match = 1;
1607 }
1608 if (!match)
1609 continue;
1610
1611 if (inc) {
1612 rec->flags++;
0376bde1 1613 if (FTRACE_WARN_ON(ftrace_rec_count(rec) == FTRACE_REF_MAX))
ed926f9b 1614 return;
79922b80
SRRH
1615
1616 /*
1617 * If there's only a single callback registered to a
1618 * function, and the ops has a trampoline registered
1619 * for it, then we can call it directly.
1620 */
1621 if (ftrace_rec_count(rec) == 1 && ops->trampoline) {
1622 rec->flags |= FTRACE_FL_TRAMP;
1623 ops->trampolines++;
1624 } else {
1625 /*
1626 * If we are adding another function callback
1627 * to this function, and the previous had a
1628 * trampoline used, then we need to go back to
1629 * the default trampoline.
1630 */
1631 rec->flags &= ~FTRACE_FL_TRAMP;
1632
1633 /* remove trampolines from any ops for this rec */
1634 ftrace_clear_tramps(rec);
1635 }
1636
08f6fba5
SR
1637 /*
1638 * If any ops wants regs saved for this function
1639 * then all ops will get saved regs.
1640 */
1641 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS)
1642 rec->flags |= FTRACE_FL_REGS;
ed926f9b 1643 } else {
0376bde1 1644 if (FTRACE_WARN_ON(ftrace_rec_count(rec) == 0))
ed926f9b
SR
1645 return;
1646 rec->flags--;
79922b80
SRRH
1647
1648 if (ops->trampoline && !ftrace_rec_count(rec))
1649 ftrace_remove_tramp(ops, rec);
1650
4fbb48cb
SRRH
1651 /*
1652 * If the rec had REGS enabled and the ops that is
1653 * being removed had REGS set, then see if there is
1654 * still any ops for this record that wants regs.
1655 * If not, we can stop recording them.
1656 */
0376bde1 1657 if (ftrace_rec_count(rec) > 0 &&
4fbb48cb
SRRH
1658 rec->flags & FTRACE_FL_REGS &&
1659 ops->flags & FTRACE_OPS_FL_SAVE_REGS) {
1660 if (!test_rec_ops_needs_regs(rec))
1661 rec->flags &= ~FTRACE_FL_REGS;
1662 }
79922b80
SRRH
1663
1664 /*
1665 * flags will be cleared in ftrace_check_record()
1666 * if rec count is zero.
1667 */
ed926f9b
SR
1668 }
1669 count++;
1670 /* Shortcut, if we handled all records, we are done. */
1671 if (!all && count == hash->count)
1672 return;
1673 } while_for_each_ftrace_rec();
1674}
1675
1676static void ftrace_hash_rec_disable(struct ftrace_ops *ops,
1677 int filter_hash)
1678{
1679 __ftrace_hash_rec_update(ops, filter_hash, 0);
1680}
1681
1682static void ftrace_hash_rec_enable(struct ftrace_ops *ops,
1683 int filter_hash)
1684{
1685 __ftrace_hash_rec_update(ops, filter_hash, 1);
1686}
1687
b17e8a37
SR
1688static void print_ip_ins(const char *fmt, unsigned char *p)
1689{
1690 int i;
1691
1692 printk(KERN_CONT "%s", fmt);
1693
1694 for (i = 0; i < MCOUNT_INSN_SIZE; i++)
1695 printk(KERN_CONT "%s%02x", i ? ":" : "", p[i]);
1696}
1697
c88fd863
SR
1698/**
1699 * ftrace_bug - report and shutdown function tracer
1700 * @failed: The failed type (EFAULT, EINVAL, EPERM)
1701 * @ip: The address that failed
1702 *
1703 * The arch code that enables or disables the function tracing
1704 * can call ftrace_bug() when it has detected a problem in
1705 * modifying the code. @failed should be one of either:
1706 * EFAULT - if the problem happens on reading the @ip address
1707 * EINVAL - if what is read at @ip is not what was expected
1708 * EPERM - if the problem happens on writting to the @ip address
1709 */
1710void ftrace_bug(int failed, unsigned long ip)
b17e8a37
SR
1711{
1712 switch (failed) {
1713 case -EFAULT:
1714 FTRACE_WARN_ON_ONCE(1);
1715 pr_info("ftrace faulted on modifying ");
1716 print_ip_sym(ip);
1717 break;
1718 case -EINVAL:
1719 FTRACE_WARN_ON_ONCE(1);
1720 pr_info("ftrace failed to modify ");
1721 print_ip_sym(ip);
b17e8a37 1722 print_ip_ins(" actual: ", (unsigned char *)ip);
b17e8a37
SR
1723 printk(KERN_CONT "\n");
1724 break;
1725 case -EPERM:
1726 FTRACE_WARN_ON_ONCE(1);
1727 pr_info("ftrace faulted on writing ");
1728 print_ip_sym(ip);
1729 break;
1730 default:
1731 FTRACE_WARN_ON_ONCE(1);
1732 pr_info("ftrace faulted on unknown error ");
1733 print_ip_sym(ip);
1734 }
1735}
1736
c88fd863 1737static int ftrace_check_record(struct dyn_ftrace *rec, int enable, int update)
5072c59f 1738{
64fbcd16 1739 unsigned long flag = 0UL;
e7d3737e 1740
982c350b 1741 /*
30fb6aa7 1742 * If we are updating calls:
982c350b 1743 *
ed926f9b
SR
1744 * If the record has a ref count, then we need to enable it
1745 * because someone is using it.
982c350b 1746 *
ed926f9b
SR
1747 * Otherwise we make sure its disabled.
1748 *
30fb6aa7 1749 * If we are disabling calls, then disable all records that
ed926f9b 1750 * are enabled.
982c350b 1751 */
0376bde1 1752 if (enable && ftrace_rec_count(rec))
ed926f9b 1753 flag = FTRACE_FL_ENABLED;
982c350b 1754
08f6fba5 1755 /*
79922b80
SRRH
1756 * If enabling and the REGS flag does not match the REGS_EN, or
1757 * the TRAMP flag doesn't match the TRAMP_EN, then do not ignore
1758 * this record. Set flags to fail the compare against ENABLED.
08f6fba5 1759 */
79922b80
SRRH
1760 if (flag) {
1761 if (!(rec->flags & FTRACE_FL_REGS) !=
1762 !(rec->flags & FTRACE_FL_REGS_EN))
1763 flag |= FTRACE_FL_REGS;
1764
1765 if (!(rec->flags & FTRACE_FL_TRAMP) !=
1766 !(rec->flags & FTRACE_FL_TRAMP_EN))
1767 flag |= FTRACE_FL_TRAMP;
1768 }
08f6fba5 1769
64fbcd16
XG
1770 /* If the state of this record hasn't changed, then do nothing */
1771 if ((rec->flags & FTRACE_FL_ENABLED) == flag)
c88fd863 1772 return FTRACE_UPDATE_IGNORE;
982c350b 1773
64fbcd16 1774 if (flag) {
08f6fba5
SR
1775 /* Save off if rec is being enabled (for return value) */
1776 flag ^= rec->flags & FTRACE_FL_ENABLED;
1777
1778 if (update) {
c88fd863 1779 rec->flags |= FTRACE_FL_ENABLED;
08f6fba5
SR
1780 if (flag & FTRACE_FL_REGS) {
1781 if (rec->flags & FTRACE_FL_REGS)
1782 rec->flags |= FTRACE_FL_REGS_EN;
1783 else
1784 rec->flags &= ~FTRACE_FL_REGS_EN;
1785 }
79922b80
SRRH
1786 if (flag & FTRACE_FL_TRAMP) {
1787 if (rec->flags & FTRACE_FL_TRAMP)
1788 rec->flags |= FTRACE_FL_TRAMP_EN;
1789 else
1790 rec->flags &= ~FTRACE_FL_TRAMP_EN;
1791 }
08f6fba5
SR
1792 }
1793
1794 /*
1795 * If this record is being updated from a nop, then
1796 * return UPDATE_MAKE_CALL.
08f6fba5
SR
1797 * Otherwise,
1798 * return UPDATE_MODIFY_CALL to tell the caller to convert
f1b2f2bd 1799 * from the save regs, to a non-save regs function or
79922b80 1800 * vice versa, or from a trampoline call.
08f6fba5
SR
1801 */
1802 if (flag & FTRACE_FL_ENABLED)
1803 return FTRACE_UPDATE_MAKE_CALL;
f1b2f2bd
SRRH
1804
1805 return FTRACE_UPDATE_MODIFY_CALL;
c88fd863
SR
1806 }
1807
08f6fba5
SR
1808 if (update) {
1809 /* If there's no more users, clear all flags */
0376bde1 1810 if (!ftrace_rec_count(rec))
08f6fba5
SR
1811 rec->flags = 0;
1812 else
1813 /* Just disable the record (keep REGS state) */
1814 rec->flags &= ~FTRACE_FL_ENABLED;
1815 }
c88fd863
SR
1816
1817 return FTRACE_UPDATE_MAKE_NOP;
1818}
1819
1820/**
1821 * ftrace_update_record, set a record that now is tracing or not
1822 * @rec: the record to update
1823 * @enable: set to 1 if the record is tracing, zero to force disable
1824 *
1825 * The records that represent all functions that can be traced need
1826 * to be updated when tracing has been enabled.
1827 */
1828int ftrace_update_record(struct dyn_ftrace *rec, int enable)
1829{
1830 return ftrace_check_record(rec, enable, 1);
1831}
1832
1833/**
1834 * ftrace_test_record, check if the record has been enabled or not
1835 * @rec: the record to test
1836 * @enable: set to 1 to check if enabled, 0 if it is disabled
1837 *
1838 * The arch code may need to test if a record is already set to
1839 * tracing to determine how to modify the function code that it
1840 * represents.
1841 */
1842int ftrace_test_record(struct dyn_ftrace *rec, int enable)
1843{
1844 return ftrace_check_record(rec, enable, 0);
1845}
1846
79922b80
SRRH
1847static struct ftrace_ops *
1848ftrace_find_tramp_ops_curr(struct dyn_ftrace *rec)
1849{
1850 struct ftrace_ops *op;
1851
1852 /* Removed ops need to be tested first */
1853 if (removed_ops && removed_ops->tramp_hash) {
1854 if (ftrace_lookup_ip(removed_ops->tramp_hash, rec->ip))
1855 return removed_ops;
1856 }
1857
1858 do_for_each_ftrace_op(op, ftrace_ops_list) {
1859 if (!op->tramp_hash)
1860 continue;
1861
1862 if (ftrace_lookup_ip(op->tramp_hash, rec->ip))
1863 return op;
1864
1865 } while_for_each_ftrace_op(op);
1866
1867 return NULL;
1868}
1869
1870static struct ftrace_ops *
1871ftrace_find_tramp_ops_new(struct dyn_ftrace *rec)
1872{
1873 struct ftrace_ops *op;
1874
1875 do_for_each_ftrace_op(op, ftrace_ops_list) {
1876 /* pass rec in as regs to have non-NULL val */
1877 if (ftrace_ops_test(op, rec->ip, rec))
1878 return op;
1879 } while_for_each_ftrace_op(op);
1880
1881 return NULL;
1882}
1883
7413af1f
SRRH
1884/**
1885 * ftrace_get_addr_new - Get the call address to set to
1886 * @rec: The ftrace record descriptor
1887 *
1888 * If the record has the FTRACE_FL_REGS set, that means that it
1889 * wants to convert to a callback that saves all regs. If FTRACE_FL_REGS
1890 * is not not set, then it wants to convert to the normal callback.
1891 *
1892 * Returns the address of the trampoline to set to
1893 */
1894unsigned long ftrace_get_addr_new(struct dyn_ftrace *rec)
1895{
79922b80
SRRH
1896 struct ftrace_ops *ops;
1897
1898 /* Trampolines take precedence over regs */
1899 if (rec->flags & FTRACE_FL_TRAMP) {
1900 ops = ftrace_find_tramp_ops_new(rec);
1901 if (FTRACE_WARN_ON(!ops || !ops->trampoline)) {
1902 pr_warning("Bad trampoline accounting at: %p (%pS)\n",
1903 (void *)rec->ip, (void *)rec->ip);
1904 /* Ftrace is shutting down, return anything */
1905 return (unsigned long)FTRACE_ADDR;
1906 }
1907 return ops->trampoline;
1908 }
1909
7413af1f
SRRH
1910 if (rec->flags & FTRACE_FL_REGS)
1911 return (unsigned long)FTRACE_REGS_ADDR;
1912 else
1913 return (unsigned long)FTRACE_ADDR;
1914}
1915
1916/**
1917 * ftrace_get_addr_curr - Get the call address that is already there
1918 * @rec: The ftrace record descriptor
1919 *
1920 * The FTRACE_FL_REGS_EN is set when the record already points to
1921 * a function that saves all the regs. Basically the '_EN' version
1922 * represents the current state of the function.
1923 *
1924 * Returns the address of the trampoline that is currently being called
1925 */
1926unsigned long ftrace_get_addr_curr(struct dyn_ftrace *rec)
1927{
79922b80
SRRH
1928 struct ftrace_ops *ops;
1929
1930 /* Trampolines take precedence over regs */
1931 if (rec->flags & FTRACE_FL_TRAMP_EN) {
1932 ops = ftrace_find_tramp_ops_curr(rec);
1933 if (FTRACE_WARN_ON(!ops)) {
1934 pr_warning("Bad trampoline accounting at: %p (%pS)\n",
1935 (void *)rec->ip, (void *)rec->ip);
1936 /* Ftrace is shutting down, return anything */
1937 return (unsigned long)FTRACE_ADDR;
1938 }
1939 return ops->trampoline;
1940 }
1941
7413af1f
SRRH
1942 if (rec->flags & FTRACE_FL_REGS_EN)
1943 return (unsigned long)FTRACE_REGS_ADDR;
1944 else
1945 return (unsigned long)FTRACE_ADDR;
1946}
1947
c88fd863
SR
1948static int
1949__ftrace_replace_code(struct dyn_ftrace *rec, int enable)
1950{
08f6fba5 1951 unsigned long ftrace_old_addr;
c88fd863
SR
1952 unsigned long ftrace_addr;
1953 int ret;
1954
7c0868e0 1955 ftrace_addr = ftrace_get_addr_new(rec);
c88fd863 1956
7c0868e0
SRRH
1957 /* This needs to be done before we call ftrace_update_record */
1958 ftrace_old_addr = ftrace_get_addr_curr(rec);
1959
1960 ret = ftrace_update_record(rec, enable);
08f6fba5 1961
c88fd863
SR
1962 switch (ret) {
1963 case FTRACE_UPDATE_IGNORE:
1964 return 0;
1965
1966 case FTRACE_UPDATE_MAKE_CALL:
64fbcd16 1967 return ftrace_make_call(rec, ftrace_addr);
c88fd863
SR
1968
1969 case FTRACE_UPDATE_MAKE_NOP:
1970 return ftrace_make_nop(NULL, rec, ftrace_addr);
08f6fba5 1971
08f6fba5 1972 case FTRACE_UPDATE_MODIFY_CALL:
08f6fba5 1973 return ftrace_modify_call(rec, ftrace_old_addr, ftrace_addr);
5072c59f
SR
1974 }
1975
c88fd863 1976 return -1; /* unknow ftrace bug */
5072c59f
SR
1977}
1978
e4f5d544 1979void __weak ftrace_replace_code(int enable)
3c1720f0 1980{
3c1720f0
SR
1981 struct dyn_ftrace *rec;
1982 struct ftrace_page *pg;
6a24a244 1983 int failed;
3c1720f0 1984
45a4a237
SR
1985 if (unlikely(ftrace_disabled))
1986 return;
1987
265c831c 1988 do_for_each_ftrace_rec(pg, rec) {
e4f5d544 1989 failed = __ftrace_replace_code(rec, enable);
fa9d13cf 1990 if (failed) {
3279ba37
SR
1991 ftrace_bug(failed, rec->ip);
1992 /* Stop processing */
1993 return;
3c1720f0 1994 }
265c831c 1995 } while_for_each_ftrace_rec();
3c1720f0
SR
1996}
1997
c88fd863
SR
1998struct ftrace_rec_iter {
1999 struct ftrace_page *pg;
2000 int index;
2001};
2002
2003/**
2004 * ftrace_rec_iter_start, start up iterating over traced functions
2005 *
2006 * Returns an iterator handle that is used to iterate over all
2007 * the records that represent address locations where functions
2008 * are traced.
2009 *
2010 * May return NULL if no records are available.
2011 */
2012struct ftrace_rec_iter *ftrace_rec_iter_start(void)
2013{
2014 /*
2015 * We only use a single iterator.
2016 * Protected by the ftrace_lock mutex.
2017 */
2018 static struct ftrace_rec_iter ftrace_rec_iter;
2019 struct ftrace_rec_iter *iter = &ftrace_rec_iter;
2020
2021 iter->pg = ftrace_pages_start;
2022 iter->index = 0;
2023
2024 /* Could have empty pages */
2025 while (iter->pg && !iter->pg->index)
2026 iter->pg = iter->pg->next;
2027
2028 if (!iter->pg)
2029 return NULL;
2030
2031 return iter;
2032}
2033
2034/**
2035 * ftrace_rec_iter_next, get the next record to process.
2036 * @iter: The handle to the iterator.
2037 *
2038 * Returns the next iterator after the given iterator @iter.
2039 */
2040struct ftrace_rec_iter *ftrace_rec_iter_next(struct ftrace_rec_iter *iter)
2041{
2042 iter->index++;
2043
2044 if (iter->index >= iter->pg->index) {
2045 iter->pg = iter->pg->next;
2046 iter->index = 0;
2047
2048 /* Could have empty pages */
2049 while (iter->pg && !iter->pg->index)
2050 iter->pg = iter->pg->next;
2051 }
2052
2053 if (!iter->pg)
2054 return NULL;
2055
2056 return iter;
2057}
2058
2059/**
2060 * ftrace_rec_iter_record, get the record at the iterator location
2061 * @iter: The current iterator location
2062 *
2063 * Returns the record that the current @iter is at.
2064 */
2065struct dyn_ftrace *ftrace_rec_iter_record(struct ftrace_rec_iter *iter)
2066{
2067 return &iter->pg->records[iter->index];
2068}
2069
492a7ea5 2070static int
31e88909 2071ftrace_code_disable(struct module *mod, struct dyn_ftrace *rec)
3c1720f0
SR
2072{
2073 unsigned long ip;
593eb8a2 2074 int ret;
3c1720f0
SR
2075
2076 ip = rec->ip;
2077
45a4a237
SR
2078 if (unlikely(ftrace_disabled))
2079 return 0;
2080
25aac9dc 2081 ret = ftrace_make_nop(mod, rec, MCOUNT_ADDR);
593eb8a2 2082 if (ret) {
31e88909 2083 ftrace_bug(ret, ip);
492a7ea5 2084 return 0;
37ad5084 2085 }
492a7ea5 2086 return 1;
3c1720f0
SR
2087}
2088
000ab691
SR
2089/*
2090 * archs can override this function if they must do something
2091 * before the modifying code is performed.
2092 */
2093int __weak ftrace_arch_code_modify_prepare(void)
2094{
2095 return 0;
2096}
2097
2098/*
2099 * archs can override this function if they must do something
2100 * after the modifying code is performed.
2101 */
2102int __weak ftrace_arch_code_modify_post_process(void)
2103{
2104 return 0;
2105}
2106
8ed3e2cf 2107void ftrace_modify_all_code(int command)
3d083395 2108{
59338f75 2109 int update = command & FTRACE_UPDATE_TRACE_FUNC;
cd21067f 2110 int err = 0;
59338f75
SRRH
2111
2112 /*
2113 * If the ftrace_caller calls a ftrace_ops func directly,
2114 * we need to make sure that it only traces functions it
2115 * expects to trace. When doing the switch of functions,
2116 * we need to update to the ftrace_ops_list_func first
2117 * before the transition between old and new calls are set,
2118 * as the ftrace_ops_list_func will check the ops hashes
2119 * to make sure the ops are having the right functions
2120 * traced.
2121 */
cd21067f
PM
2122 if (update) {
2123 err = ftrace_update_ftrace_func(ftrace_ops_list_func);
2124 if (FTRACE_WARN_ON(err))
2125 return;
2126 }
59338f75 2127
8ed3e2cf 2128 if (command & FTRACE_UPDATE_CALLS)
d61f82d0 2129 ftrace_replace_code(1);
8ed3e2cf 2130 else if (command & FTRACE_DISABLE_CALLS)
d61f82d0
SR
2131 ftrace_replace_code(0);
2132
405e1d83
SRRH
2133 if (update && ftrace_trace_function != ftrace_ops_list_func) {
2134 function_trace_op = set_function_trace_op;
2135 smp_wmb();
2136 /* If irqs are disabled, we are in stop machine */
2137 if (!irqs_disabled())
2138 smp_call_function(ftrace_sync_ipi, NULL, 1);
cd21067f
PM
2139 err = ftrace_update_ftrace_func(ftrace_trace_function);
2140 if (FTRACE_WARN_ON(err))
2141 return;
405e1d83 2142 }
d61f82d0 2143
8ed3e2cf 2144 if (command & FTRACE_START_FUNC_RET)
cd21067f 2145 err = ftrace_enable_ftrace_graph_caller();
8ed3e2cf 2146 else if (command & FTRACE_STOP_FUNC_RET)
cd21067f
PM
2147 err = ftrace_disable_ftrace_graph_caller();
2148 FTRACE_WARN_ON(err);
8ed3e2cf
SR
2149}
2150
2151static int __ftrace_modify_code(void *data)
2152{
2153 int *command = data;
2154
2155 ftrace_modify_all_code(*command);
5a45cfe1 2156
d61f82d0 2157 return 0;
3d083395
SR
2158}
2159
c88fd863
SR
2160/**
2161 * ftrace_run_stop_machine, go back to the stop machine method
2162 * @command: The command to tell ftrace what to do
2163 *
2164 * If an arch needs to fall back to the stop machine method, the
2165 * it can call this function.
2166 */
2167void ftrace_run_stop_machine(int command)
2168{
2169 stop_machine(__ftrace_modify_code, &command, NULL);
2170}
2171
2172/**
2173 * arch_ftrace_update_code, modify the code to trace or not trace
2174 * @command: The command that needs to be done
2175 *
2176 * Archs can override this function if it does not need to
2177 * run stop_machine() to modify code.
2178 */
2179void __weak arch_ftrace_update_code(int command)
2180{
2181 ftrace_run_stop_machine(command);
2182}
2183
79922b80
SRRH
2184static int ftrace_save_ops_tramp_hash(struct ftrace_ops *ops)
2185{
2186 struct ftrace_page *pg;
2187 struct dyn_ftrace *rec;
2188 int size, bits;
2189 int ret;
2190
2191 size = ops->trampolines;
2192 bits = 0;
2193 /*
2194 * Make the hash size about 1/2 the # found
2195 */
2196 for (size /= 2; size; size >>= 1)
2197 bits++;
2198
2199 ops->tramp_hash = alloc_ftrace_hash(bits);
2200 /*
2201 * TODO: a failed allocation is going to screw up
2202 * the accounting of what needs to be modified
2203 * and not. For now, we kill ftrace if we fail
2204 * to allocate here. But there are ways around this,
2205 * but that will take a little more work.
2206 */
2207 if (!ops->tramp_hash)
2208 return -ENOMEM;
2209
2210 do_for_each_ftrace_rec(pg, rec) {
2211 if (ftrace_rec_count(rec) == 1 &&
2212 ftrace_ops_test(ops, rec->ip, rec)) {
2213
2214 /* This record had better have a trampoline */
2215 if (FTRACE_WARN_ON(!(rec->flags & FTRACE_FL_TRAMP_EN)))
2216 return -1;
2217
2218 ret = add_hash_entry(ops->tramp_hash, rec->ip);
2219 if (ret < 0)
2220 return ret;
2221 }
2222 } while_for_each_ftrace_rec();
2223
2224 return 0;
2225}
2226
2227static int ftrace_save_tramp_hashes(void)
2228{
2229 struct ftrace_ops *op;
2230 int ret;
2231
2232 /*
2233 * Now that any trampoline is being used, we need to save the
2234 * hashes for the ops that have them. This allows the mapping
2235 * back from the record to the ops that has the trampoline to
2236 * know what code is being replaced. Modifying code must always
2237 * verify what it is changing.
2238 */
2239 do_for_each_ftrace_op(op, ftrace_ops_list) {
2240
2241 /* The tramp_hash is recreated each time. */
2242 free_ftrace_hash(op->tramp_hash);
2243 op->tramp_hash = NULL;
2244
2245 if (op->trampolines) {
2246 ret = ftrace_save_ops_tramp_hash(op);
2247 if (ret)
2248 return ret;
2249 }
2250
2251 } while_for_each_ftrace_op(op);
2252
2253 return 0;
2254}
2255
e309b41d 2256static void ftrace_run_update_code(int command)
3d083395 2257{
000ab691
SR
2258 int ret;
2259
2260 ret = ftrace_arch_code_modify_prepare();
2261 FTRACE_WARN_ON(ret);
2262 if (ret)
2263 return;
c88fd863
SR
2264 /*
2265 * Do not call function tracer while we update the code.
2266 * We are in stop machine.
2267 */
2268 function_trace_stop++;
000ab691 2269
c88fd863
SR
2270 /*
2271 * By default we use stop_machine() to modify the code.
2272 * But archs can do what ever they want as long as it
2273 * is safe. The stop_machine() is the safest, but also
2274 * produces the most overhead.
2275 */
2276 arch_ftrace_update_code(command);
2277
c88fd863 2278 function_trace_stop--;
000ab691
SR
2279
2280 ret = ftrace_arch_code_modify_post_process();
2281 FTRACE_WARN_ON(ret);
79922b80
SRRH
2282
2283 ret = ftrace_save_tramp_hashes();
2284 FTRACE_WARN_ON(ret);
3d083395
SR
2285}
2286
d61f82d0 2287static ftrace_func_t saved_ftrace_func;
60a7ecf4 2288static int ftrace_start_up;
df4fc315 2289
db0fbadc
JS
2290static void control_ops_free(struct ftrace_ops *ops)
2291{
2292 free_percpu(ops->disabled);
2293}
2294
df4fc315
SR
2295static void ftrace_startup_enable(int command)
2296{
2297 if (saved_ftrace_func != ftrace_trace_function) {
2298 saved_ftrace_func = ftrace_trace_function;
2299 command |= FTRACE_UPDATE_TRACE_FUNC;
2300 }
2301
2302 if (!command || !ftrace_enabled)
2303 return;
2304
2305 ftrace_run_update_code(command);
2306}
d61f82d0 2307
a1cd6173 2308static int ftrace_startup(struct ftrace_ops *ops, int command)
3d083395 2309{
8a56d776 2310 int ret;
b848914c 2311
4eebcc81 2312 if (unlikely(ftrace_disabled))
a1cd6173 2313 return -ENODEV;
4eebcc81 2314
8a56d776
SRRH
2315 ret = __register_ftrace_function(ops);
2316 if (ret)
2317 return ret;
2318
60a7ecf4 2319 ftrace_start_up++;
30fb6aa7 2320 command |= FTRACE_UPDATE_CALLS;
d61f82d0 2321
ed926f9b 2322 ops->flags |= FTRACE_OPS_FL_ENABLED;
66209a5b
SRRH
2323
2324 ftrace_hash_rec_enable(ops, 1);
ed926f9b 2325
df4fc315 2326 ftrace_startup_enable(command);
a1cd6173
SR
2327
2328 return 0;
3d083395
SR
2329}
2330
8a56d776 2331static int ftrace_shutdown(struct ftrace_ops *ops, int command)
3d083395 2332{
8a56d776 2333 int ret;
b848914c 2334
4eebcc81 2335 if (unlikely(ftrace_disabled))
8a56d776
SRRH
2336 return -ENODEV;
2337
2338 ret = __unregister_ftrace_function(ops);
2339 if (ret)
2340 return ret;
4eebcc81 2341
60a7ecf4 2342 ftrace_start_up--;
9ea1a153
FW
2343 /*
2344 * Just warn in case of unbalance, no need to kill ftrace, it's not
2345 * critical but the ftrace_call callers may be never nopped again after
2346 * further ftrace uses.
2347 */
2348 WARN_ON_ONCE(ftrace_start_up < 0);
2349
66209a5b 2350 ftrace_hash_rec_disable(ops, 1);
ed926f9b 2351
a737e6dd 2352 ops->flags &= ~FTRACE_OPS_FL_ENABLED;
b848914c 2353
30fb6aa7 2354 command |= FTRACE_UPDATE_CALLS;
3d083395 2355
d61f82d0
SR
2356 if (saved_ftrace_func != ftrace_trace_function) {
2357 saved_ftrace_func = ftrace_trace_function;
2358 command |= FTRACE_UPDATE_TRACE_FUNC;
2359 }
3d083395 2360
a4c35ed2
SRRH
2361 if (!command || !ftrace_enabled) {
2362 /*
2363 * If these are control ops, they still need their
2364 * per_cpu field freed. Since, function tracing is
2365 * not currently active, we can just free them
2366 * without synchronizing all CPUs.
2367 */
2368 if (ops->flags & FTRACE_OPS_FL_CONTROL)
2369 control_ops_free(ops);
8a56d776 2370 return 0;
a4c35ed2 2371 }
d61f82d0 2372
79922b80
SRRH
2373 /*
2374 * If the ops uses a trampoline, then it needs to be
2375 * tested first on update.
2376 */
2377 removed_ops = ops;
2378
d61f82d0 2379 ftrace_run_update_code(command);
a4c35ed2 2380
79922b80
SRRH
2381 removed_ops = NULL;
2382
a4c35ed2
SRRH
2383 /*
2384 * Dynamic ops may be freed, we must make sure that all
2385 * callers are done before leaving this function.
2386 * The same goes for freeing the per_cpu data of the control
2387 * ops.
2388 *
2389 * Again, normal synchronize_sched() is not good enough.
2390 * We need to do a hard force of sched synchronization.
2391 * This is because we use preempt_disable() to do RCU, but
2392 * the function tracers can be called where RCU is not watching
2393 * (like before user_exit()). We can not rely on the RCU
2394 * infrastructure to do the synchronization, thus we must do it
2395 * ourselves.
2396 */
2397 if (ops->flags & (FTRACE_OPS_FL_DYNAMIC | FTRACE_OPS_FL_CONTROL)) {
2398 schedule_on_each_cpu(ftrace_sync);
2399
2400 if (ops->flags & FTRACE_OPS_FL_CONTROL)
2401 control_ops_free(ops);
2402 }
2403
8a56d776 2404 return 0;
3d083395
SR
2405}
2406
e309b41d 2407static void ftrace_startup_sysctl(void)
b0fc494f 2408{
4eebcc81
SR
2409 if (unlikely(ftrace_disabled))
2410 return;
2411
d61f82d0
SR
2412 /* Force update next time */
2413 saved_ftrace_func = NULL;
60a7ecf4
SR
2414 /* ftrace_start_up is true if we want ftrace running */
2415 if (ftrace_start_up)
30fb6aa7 2416 ftrace_run_update_code(FTRACE_UPDATE_CALLS);
b0fc494f
SR
2417}
2418
e309b41d 2419static void ftrace_shutdown_sysctl(void)
b0fc494f 2420{
4eebcc81
SR
2421 if (unlikely(ftrace_disabled))
2422 return;
2423
60a7ecf4
SR
2424 /* ftrace_start_up is true if ftrace is running */
2425 if (ftrace_start_up)
79e406d7 2426 ftrace_run_update_code(FTRACE_DISABLE_CALLS);
b0fc494f
SR
2427}
2428
3d083395 2429static cycle_t ftrace_update_time;
3d083395
SR
2430unsigned long ftrace_update_tot_cnt;
2431
8c4f3c3f 2432static inline int ops_traces_mod(struct ftrace_ops *ops)
f7bc8b61 2433{
8c4f3c3f
SRRH
2434 /*
2435 * Filter_hash being empty will default to trace module.
2436 * But notrace hash requires a test of individual module functions.
2437 */
2438 return ftrace_hash_empty(ops->filter_hash) &&
2439 ftrace_hash_empty(ops->notrace_hash);
2440}
2441
2442/*
2443 * Check if the current ops references the record.
2444 *
2445 * If the ops traces all functions, then it was already accounted for.
2446 * If the ops does not trace the current record function, skip it.
2447 * If the ops ignores the function via notrace filter, skip it.
2448 */
2449static inline bool
2450ops_references_rec(struct ftrace_ops *ops, struct dyn_ftrace *rec)
2451{
2452 /* If ops isn't enabled, ignore it */
2453 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
2454 return 0;
2455
2456 /* If ops traces all mods, we already accounted for it */
2457 if (ops_traces_mod(ops))
2458 return 0;
2459
2460 /* The function must be in the filter */
2461 if (!ftrace_hash_empty(ops->filter_hash) &&
2462 !ftrace_lookup_ip(ops->filter_hash, rec->ip))
2463 return 0;
f7bc8b61 2464
8c4f3c3f
SRRH
2465 /* If in notrace hash, we ignore it too */
2466 if (ftrace_lookup_ip(ops->notrace_hash, rec->ip))
2467 return 0;
2468
2469 return 1;
2470}
2471
2472static int referenced_filters(struct dyn_ftrace *rec)
2473{
2474 struct ftrace_ops *ops;
2475 int cnt = 0;
2476
2477 for (ops = ftrace_ops_list; ops != &ftrace_list_end; ops = ops->next) {
2478 if (ops_references_rec(ops, rec))
2479 cnt++;
2480 }
2481
2482 return cnt;
f7bc8b61
SR
2483}
2484
1dc43cf0 2485static int ftrace_update_code(struct module *mod, struct ftrace_page *new_pgs)
3d083395 2486{
85ae32ae 2487 struct ftrace_page *pg;
e94142a6 2488 struct dyn_ftrace *p;
f22f9a89 2489 cycle_t start, stop;
1dc43cf0 2490 unsigned long update_cnt = 0;
f7bc8b61 2491 unsigned long ref = 0;
8c4f3c3f 2492 bool test = false;
85ae32ae 2493 int i;
f7bc8b61
SR
2494
2495 /*
2496 * When adding a module, we need to check if tracers are
2497 * currently enabled and if they are set to trace all functions.
2498 * If they are, we need to enable the module functions as well
2499 * as update the reference counts for those function records.
2500 */
2501 if (mod) {
2502 struct ftrace_ops *ops;
2503
2504 for (ops = ftrace_ops_list;
2505 ops != &ftrace_list_end; ops = ops->next) {
8c4f3c3f
SRRH
2506 if (ops->flags & FTRACE_OPS_FL_ENABLED) {
2507 if (ops_traces_mod(ops))
2508 ref++;
2509 else
2510 test = true;
2511 }
f7bc8b61
SR
2512 }
2513 }
3d083395 2514
750ed1a4 2515 start = ftrace_now(raw_smp_processor_id());
3d083395 2516
1dc43cf0 2517 for (pg = new_pgs; pg; pg = pg->next) {
3d083395 2518
85ae32ae 2519 for (i = 0; i < pg->index; i++) {
8c4f3c3f
SRRH
2520 int cnt = ref;
2521
85ae32ae
SR
2522 /* If something went wrong, bail without enabling anything */
2523 if (unlikely(ftrace_disabled))
2524 return -1;
f22f9a89 2525
85ae32ae 2526 p = &pg->records[i];
8c4f3c3f
SRRH
2527 if (test)
2528 cnt += referenced_filters(p);
2529 p->flags = cnt;
f22f9a89 2530
85ae32ae
SR
2531 /*
2532 * Do the initial record conversion from mcount jump
2533 * to the NOP instructions.
2534 */
2535 if (!ftrace_code_disable(mod, p))
2536 break;
5cb084bb 2537
1dc43cf0 2538 update_cnt++;
5cb084bb 2539
85ae32ae
SR
2540 /*
2541 * If the tracing is enabled, go ahead and enable the record.
2542 *
2543 * The reason not to enable the record immediatelly is the
2544 * inherent check of ftrace_make_nop/ftrace_make_call for
2545 * correct previous instructions. Making first the NOP
2546 * conversion puts the module to the correct state, thus
2547 * passing the ftrace_make_call check.
2548 */
8c4f3c3f 2549 if (ftrace_start_up && cnt) {
85ae32ae
SR
2550 int failed = __ftrace_replace_code(p, 1);
2551 if (failed)
2552 ftrace_bug(failed, p->ip);
2553 }
5cb084bb 2554 }
3d083395
SR
2555 }
2556
750ed1a4 2557 stop = ftrace_now(raw_smp_processor_id());
3d083395 2558 ftrace_update_time = stop - start;
1dc43cf0 2559 ftrace_update_tot_cnt += update_cnt;
3d083395 2560
16444a8a
ACM
2561 return 0;
2562}
2563
a7900875 2564static int ftrace_allocate_records(struct ftrace_page *pg, int count)
3c1720f0 2565{
a7900875 2566 int order;
3c1720f0 2567 int cnt;
3c1720f0 2568
a7900875
SR
2569 if (WARN_ON(!count))
2570 return -EINVAL;
2571
2572 order = get_count_order(DIV_ROUND_UP(count, ENTRIES_PER_PAGE));
3c1720f0
SR
2573
2574 /*
a7900875
SR
2575 * We want to fill as much as possible. No more than a page
2576 * may be empty.
3c1720f0 2577 */
a7900875
SR
2578 while ((PAGE_SIZE << order) / ENTRY_SIZE >= count + ENTRIES_PER_PAGE)
2579 order--;
3c1720f0 2580
a7900875
SR
2581 again:
2582 pg->records = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, order);
3c1720f0 2583
a7900875
SR
2584 if (!pg->records) {
2585 /* if we can't allocate this size, try something smaller */
2586 if (!order)
2587 return -ENOMEM;
2588 order >>= 1;
2589 goto again;
2590 }
3c1720f0 2591
a7900875
SR
2592 cnt = (PAGE_SIZE << order) / ENTRY_SIZE;
2593 pg->size = cnt;
3c1720f0 2594
a7900875
SR
2595 if (cnt > count)
2596 cnt = count;
2597
2598 return cnt;
2599}
2600
2601static struct ftrace_page *
2602ftrace_allocate_pages(unsigned long num_to_init)
2603{
2604 struct ftrace_page *start_pg;
2605 struct ftrace_page *pg;
2606 int order;
2607 int cnt;
2608
2609 if (!num_to_init)
2610 return 0;
2611
2612 start_pg = pg = kzalloc(sizeof(*pg), GFP_KERNEL);
2613 if (!pg)
2614 return NULL;
2615
2616 /*
2617 * Try to allocate as much as possible in one continues
2618 * location that fills in all of the space. We want to
2619 * waste as little space as possible.
2620 */
2621 for (;;) {
2622 cnt = ftrace_allocate_records(pg, num_to_init);
2623 if (cnt < 0)
2624 goto free_pages;
2625
2626 num_to_init -= cnt;
2627 if (!num_to_init)
3c1720f0
SR
2628 break;
2629
a7900875
SR
2630 pg->next = kzalloc(sizeof(*pg), GFP_KERNEL);
2631 if (!pg->next)
2632 goto free_pages;
2633
3c1720f0
SR
2634 pg = pg->next;
2635 }
2636
a7900875
SR
2637 return start_pg;
2638
2639 free_pages:
1f61be00
NK
2640 pg = start_pg;
2641 while (pg) {
a7900875
SR
2642 order = get_count_order(pg->size / ENTRIES_PER_PAGE);
2643 free_pages((unsigned long)pg->records, order);
2644 start_pg = pg->next;
2645 kfree(pg);
2646 pg = start_pg;
2647 }
2648 pr_info("ftrace: FAILED to allocate memory for functions\n");
2649 return NULL;
2650}
2651
5072c59f
SR
2652#define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
2653
2654struct ftrace_iterator {
98c4fd04 2655 loff_t pos;
4aeb6967
SR
2656 loff_t func_pos;
2657 struct ftrace_page *pg;
2658 struct dyn_ftrace *func;
2659 struct ftrace_func_probe *probe;
2660 struct trace_parser parser;
1cf41dd7 2661 struct ftrace_hash *hash;
33dc9b12 2662 struct ftrace_ops *ops;
4aeb6967
SR
2663 int hidx;
2664 int idx;
2665 unsigned flags;
5072c59f
SR
2666};
2667
8fc0c701 2668static void *
4aeb6967 2669t_hash_next(struct seq_file *m, loff_t *pos)
8fc0c701
SR
2670{
2671 struct ftrace_iterator *iter = m->private;
4aeb6967 2672 struct hlist_node *hnd = NULL;
8fc0c701
SR
2673 struct hlist_head *hhd;
2674
8fc0c701 2675 (*pos)++;
98c4fd04 2676 iter->pos = *pos;
8fc0c701 2677
4aeb6967
SR
2678 if (iter->probe)
2679 hnd = &iter->probe->node;
8fc0c701
SR
2680 retry:
2681 if (iter->hidx >= FTRACE_FUNC_HASHSIZE)
2682 return NULL;
2683
2684 hhd = &ftrace_func_hash[iter->hidx];
2685
2686 if (hlist_empty(hhd)) {
2687 iter->hidx++;
2688 hnd = NULL;
2689 goto retry;
2690 }
2691
2692 if (!hnd)
2693 hnd = hhd->first;
2694 else {
2695 hnd = hnd->next;
2696 if (!hnd) {
2697 iter->hidx++;
2698 goto retry;
2699 }
2700 }
2701
4aeb6967
SR
2702 if (WARN_ON_ONCE(!hnd))
2703 return NULL;
2704
2705 iter->probe = hlist_entry(hnd, struct ftrace_func_probe, node);
2706
2707 return iter;
8fc0c701
SR
2708}
2709
2710static void *t_hash_start(struct seq_file *m, loff_t *pos)
2711{
2712 struct ftrace_iterator *iter = m->private;
2713 void *p = NULL;
d82d6244
LZ
2714 loff_t l;
2715
69a3083c
SR
2716 if (!(iter->flags & FTRACE_ITER_DO_HASH))
2717 return NULL;
2718
2bccfffd
SR
2719 if (iter->func_pos > *pos)
2720 return NULL;
8fc0c701 2721
d82d6244 2722 iter->hidx = 0;
2bccfffd 2723 for (l = 0; l <= (*pos - iter->func_pos); ) {
4aeb6967 2724 p = t_hash_next(m, &l);
d82d6244
LZ
2725 if (!p)
2726 break;
2727 }
4aeb6967
SR
2728 if (!p)
2729 return NULL;
2730
98c4fd04
SR
2731 /* Only set this if we have an item */
2732 iter->flags |= FTRACE_ITER_HASH;
2733
4aeb6967 2734 return iter;
8fc0c701
SR
2735}
2736
4aeb6967
SR
2737static int
2738t_hash_show(struct seq_file *m, struct ftrace_iterator *iter)
8fc0c701 2739{
b6887d79 2740 struct ftrace_func_probe *rec;
8fc0c701 2741
4aeb6967
SR
2742 rec = iter->probe;
2743 if (WARN_ON_ONCE(!rec))
2744 return -EIO;
8fc0c701 2745
809dcf29
SR
2746 if (rec->ops->print)
2747 return rec->ops->print(m, rec->ip, rec->ops, rec->data);
2748
b375a11a 2749 seq_printf(m, "%ps:%ps", (void *)rec->ip, (void *)rec->ops->func);
8fc0c701
SR
2750
2751 if (rec->data)
2752 seq_printf(m, ":%p", rec->data);
2753 seq_putc(m, '\n');
2754
2755 return 0;
2756}
2757
e309b41d 2758static void *
5072c59f
SR
2759t_next(struct seq_file *m, void *v, loff_t *pos)
2760{
2761 struct ftrace_iterator *iter = m->private;
fc13cb0c 2762 struct ftrace_ops *ops = iter->ops;
5072c59f
SR
2763 struct dyn_ftrace *rec = NULL;
2764
45a4a237
SR
2765 if (unlikely(ftrace_disabled))
2766 return NULL;
2767
8fc0c701 2768 if (iter->flags & FTRACE_ITER_HASH)
4aeb6967 2769 return t_hash_next(m, pos);
8fc0c701 2770
5072c59f 2771 (*pos)++;
1106b699 2772 iter->pos = iter->func_pos = *pos;
5072c59f 2773
0c75a3ed 2774 if (iter->flags & FTRACE_ITER_PRINTALL)
57c072c7 2775 return t_hash_start(m, pos);
0c75a3ed 2776
5072c59f
SR
2777 retry:
2778 if (iter->idx >= iter->pg->index) {
2779 if (iter->pg->next) {
2780 iter->pg = iter->pg->next;
2781 iter->idx = 0;
2782 goto retry;
2783 }
2784 } else {
2785 rec = &iter->pg->records[iter->idx++];
32082309 2786 if (((iter->flags & FTRACE_ITER_FILTER) &&
f45948e8 2787 !(ftrace_lookup_ip(ops->filter_hash, rec->ip))) ||
0183fb1c 2788
41c52c0d 2789 ((iter->flags & FTRACE_ITER_NOTRACE) &&
647bcd03
SR
2790 !ftrace_lookup_ip(ops->notrace_hash, rec->ip)) ||
2791
2792 ((iter->flags & FTRACE_ITER_ENABLED) &&
23ea9c4d 2793 !(rec->flags & FTRACE_FL_ENABLED))) {
647bcd03 2794
5072c59f
SR
2795 rec = NULL;
2796 goto retry;
2797 }
2798 }
2799
4aeb6967 2800 if (!rec)
57c072c7 2801 return t_hash_start(m, pos);
4aeb6967
SR
2802
2803 iter->func = rec;
2804
2805 return iter;
5072c59f
SR
2806}
2807
98c4fd04
SR
2808static void reset_iter_read(struct ftrace_iterator *iter)
2809{
2810 iter->pos = 0;
2811 iter->func_pos = 0;
70f77b3f 2812 iter->flags &= ~(FTRACE_ITER_PRINTALL | FTRACE_ITER_HASH);
5072c59f
SR
2813}
2814
2815static void *t_start(struct seq_file *m, loff_t *pos)
2816{
2817 struct ftrace_iterator *iter = m->private;
fc13cb0c 2818 struct ftrace_ops *ops = iter->ops;
5072c59f 2819 void *p = NULL;
694ce0a5 2820 loff_t l;
5072c59f 2821
8fc0c701 2822 mutex_lock(&ftrace_lock);
45a4a237
SR
2823
2824 if (unlikely(ftrace_disabled))
2825 return NULL;
2826
98c4fd04
SR
2827 /*
2828 * If an lseek was done, then reset and start from beginning.
2829 */
2830 if (*pos < iter->pos)
2831 reset_iter_read(iter);
2832
0c75a3ed
SR
2833 /*
2834 * For set_ftrace_filter reading, if we have the filter
2835 * off, we can short cut and just print out that all
2836 * functions are enabled.
2837 */
06a51d93
SR
2838 if (iter->flags & FTRACE_ITER_FILTER &&
2839 ftrace_hash_empty(ops->filter_hash)) {
0c75a3ed 2840 if (*pos > 0)
8fc0c701 2841 return t_hash_start(m, pos);
0c75a3ed 2842 iter->flags |= FTRACE_ITER_PRINTALL;
df091625
CW
2843 /* reset in case of seek/pread */
2844 iter->flags &= ~FTRACE_ITER_HASH;
0c75a3ed
SR
2845 return iter;
2846 }
2847
8fc0c701
SR
2848 if (iter->flags & FTRACE_ITER_HASH)
2849 return t_hash_start(m, pos);
2850
98c4fd04
SR
2851 /*
2852 * Unfortunately, we need to restart at ftrace_pages_start
2853 * every time we let go of the ftrace_mutex. This is because
2854 * those pointers can change without the lock.
2855 */
694ce0a5
LZ
2856 iter->pg = ftrace_pages_start;
2857 iter->idx = 0;
2858 for (l = 0; l <= *pos; ) {
2859 p = t_next(m, p, &l);
2860 if (!p)
2861 break;
50cdaf08 2862 }
5821e1b7 2863
69a3083c
SR
2864 if (!p)
2865 return t_hash_start(m, pos);
4aeb6967
SR
2866
2867 return iter;
5072c59f
SR
2868}
2869
2870static void t_stop(struct seq_file *m, void *p)
2871{
8fc0c701 2872 mutex_unlock(&ftrace_lock);
5072c59f
SR
2873}
2874
2875static int t_show(struct seq_file *m, void *v)
2876{
0c75a3ed 2877 struct ftrace_iterator *iter = m->private;
4aeb6967 2878 struct dyn_ftrace *rec;
5072c59f 2879
8fc0c701 2880 if (iter->flags & FTRACE_ITER_HASH)
4aeb6967 2881 return t_hash_show(m, iter);
8fc0c701 2882
0c75a3ed
SR
2883 if (iter->flags & FTRACE_ITER_PRINTALL) {
2884 seq_printf(m, "#### all functions enabled ####\n");
2885 return 0;
2886 }
2887
4aeb6967
SR
2888 rec = iter->func;
2889
5072c59f
SR
2890 if (!rec)
2891 return 0;
2892
647bcd03 2893 seq_printf(m, "%ps", (void *)rec->ip);
9674b2fa 2894 if (iter->flags & FTRACE_ITER_ENABLED) {
08f6fba5 2895 seq_printf(m, " (%ld)%s",
0376bde1 2896 ftrace_rec_count(rec),
9674b2fa
SRRH
2897 rec->flags & FTRACE_FL_REGS ? " R" : " ");
2898 if (rec->flags & FTRACE_FL_TRAMP_EN) {
2899 struct ftrace_ops *ops;
2900
2901 ops = ftrace_find_tramp_ops_curr(rec);
2902 if (ops && ops->trampoline)
2903 seq_printf(m, "\ttramp: %pS",
2904 (void *)ops->trampoline);
2905 else
2906 seq_printf(m, "\ttramp: ERROR!");
2907 }
2908 }
2909
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) {
ef2fbe16
NK
3013 const int size_bits = FTRACE_HASH_DEFAULT_BITS;
3014
3015 if (file->f_flags & O_TRUNC)
3016 iter->hash = alloc_ftrace_hash(size_bits);
3017 else
3018 iter->hash = alloc_and_copy_ftrace_hash(size_bits, hash);
3019
33dc9b12
SR
3020 if (!iter->hash) {
3021 trace_parser_put(&iter->parser);
3022 kfree(iter);
3f2367ba
MH
3023 ret = -ENOMEM;
3024 goto out_unlock;
33dc9b12
SR
3025 }
3026 }
1cf41dd7 3027
5072c59f
SR
3028 if (file->f_mode & FMODE_READ) {
3029 iter->pg = ftrace_pages_start;
5072c59f
SR
3030
3031 ret = seq_open(file, &show_ftrace_seq_ops);
3032 if (!ret) {
3033 struct seq_file *m = file->private_data;
3034 m->private = iter;
79fe249c 3035 } else {
33dc9b12
SR
3036 /* Failed */
3037 free_ftrace_hash(iter->hash);
79fe249c 3038 trace_parser_put(&iter->parser);
5072c59f 3039 kfree(iter);
79fe249c 3040 }
5072c59f
SR
3041 } else
3042 file->private_data = iter;
3f2367ba
MH
3043
3044 out_unlock:
f04f24fb 3045 mutex_unlock(&ops->regex_lock);
5072c59f
SR
3046
3047 return ret;
3048}
3049
41c52c0d
SR
3050static int
3051ftrace_filter_open(struct inode *inode, struct file *file)
3052{
e3b3e2e8
SRRH
3053 struct ftrace_ops *ops = inode->i_private;
3054
3055 return ftrace_regex_open(ops,
69a3083c
SR
3056 FTRACE_ITER_FILTER | FTRACE_ITER_DO_HASH,
3057 inode, file);
41c52c0d
SR
3058}
3059
3060static int
3061ftrace_notrace_open(struct inode *inode, struct file *file)
3062{
e3b3e2e8
SRRH
3063 struct ftrace_ops *ops = inode->i_private;
3064
3065 return ftrace_regex_open(ops, FTRACE_ITER_NOTRACE,
1cf41dd7 3066 inode, file);
41c52c0d
SR
3067}
3068
64e7c440 3069static int ftrace_match(char *str, char *regex, int len, int type)
9f4801e3 3070{
9f4801e3 3071 int matched = 0;
751e9983 3072 int slen;
9f4801e3 3073
9f4801e3
SR
3074 switch (type) {
3075 case MATCH_FULL:
3076 if (strcmp(str, regex) == 0)
3077 matched = 1;
3078 break;
3079 case MATCH_FRONT_ONLY:
3080 if (strncmp(str, regex, len) == 0)
3081 matched = 1;
3082 break;
3083 case MATCH_MIDDLE_ONLY:
3084 if (strstr(str, regex))
3085 matched = 1;
3086 break;
3087 case MATCH_END_ONLY:
751e9983
LZ
3088 slen = strlen(str);
3089 if (slen >= len && memcmp(str + slen - len, regex, len) == 0)
9f4801e3
SR
3090 matched = 1;
3091 break;
3092 }
3093
3094 return matched;
3095}
3096
b448c4e3 3097static int
1cf41dd7 3098enter_record(struct ftrace_hash *hash, struct dyn_ftrace *rec, int not)
996e87be 3099{
b448c4e3 3100 struct ftrace_func_entry *entry;
b448c4e3
SR
3101 int ret = 0;
3102
1cf41dd7
SR
3103 entry = ftrace_lookup_ip(hash, rec->ip);
3104 if (not) {
3105 /* Do nothing if it doesn't exist */
3106 if (!entry)
3107 return 0;
b448c4e3 3108
33dc9b12 3109 free_hash_entry(hash, entry);
1cf41dd7
SR
3110 } else {
3111 /* Do nothing if it exists */
3112 if (entry)
3113 return 0;
b448c4e3 3114
1cf41dd7 3115 ret = add_hash_entry(hash, rec->ip);
b448c4e3
SR
3116 }
3117 return ret;
996e87be
SR
3118}
3119
64e7c440 3120static int
b9df92d2
SR
3121ftrace_match_record(struct dyn_ftrace *rec, char *mod,
3122 char *regex, int len, int type)
64e7c440
SR
3123{
3124 char str[KSYM_SYMBOL_LEN];
b9df92d2
SR
3125 char *modname;
3126
3127 kallsyms_lookup(rec->ip, NULL, NULL, &modname, str);
3128
3129 if (mod) {
3130 /* module lookup requires matching the module */
3131 if (!modname || strcmp(modname, mod))
3132 return 0;
3133
3134 /* blank search means to match all funcs in the mod */
3135 if (!len)
3136 return 1;
3137 }
64e7c440 3138
64e7c440
SR
3139 return ftrace_match(str, regex, len, type);
3140}
3141
1cf41dd7
SR
3142static int
3143match_records(struct ftrace_hash *hash, char *buff,
3144 int len, char *mod, int not)
9f4801e3 3145{
b9df92d2 3146 unsigned search_len = 0;
9f4801e3
SR
3147 struct ftrace_page *pg;
3148 struct dyn_ftrace *rec;
b9df92d2
SR
3149 int type = MATCH_FULL;
3150 char *search = buff;
311d16da 3151 int found = 0;
b448c4e3 3152 int ret;
9f4801e3 3153
b9df92d2
SR
3154 if (len) {
3155 type = filter_parse_regex(buff, len, &search, &not);
3156 search_len = strlen(search);
3157 }
9f4801e3 3158
52baf119 3159 mutex_lock(&ftrace_lock);
265c831c 3160
b9df92d2
SR
3161 if (unlikely(ftrace_disabled))
3162 goto out_unlock;
9f4801e3 3163
265c831c 3164 do_for_each_ftrace_rec(pg, rec) {
b9df92d2 3165 if (ftrace_match_record(rec, mod, search, search_len, type)) {
1cf41dd7 3166 ret = enter_record(hash, rec, not);
b448c4e3
SR
3167 if (ret < 0) {
3168 found = ret;
3169 goto out_unlock;
3170 }
311d16da 3171 found = 1;
265c831c
SR
3172 }
3173 } while_for_each_ftrace_rec();
b9df92d2 3174 out_unlock:
52baf119 3175 mutex_unlock(&ftrace_lock);
311d16da
LZ
3176
3177 return found;
5072c59f
SR
3178}
3179
64e7c440 3180static int
1cf41dd7 3181ftrace_match_records(struct ftrace_hash *hash, char *buff, int len)
64e7c440 3182{
1cf41dd7 3183 return match_records(hash, buff, len, NULL, 0);
64e7c440
SR
3184}
3185
1cf41dd7
SR
3186static int
3187ftrace_match_module_records(struct ftrace_hash *hash, char *buff, char *mod)
64e7c440 3188{
64e7c440 3189 int not = 0;
6a24a244 3190
64e7c440
SR
3191 /* blank or '*' mean the same */
3192 if (strcmp(buff, "*") == 0)
3193 buff[0] = 0;
3194
3195 /* handle the case of 'dont filter this module' */
3196 if (strcmp(buff, "!") == 0 || strcmp(buff, "!*") == 0) {
3197 buff[0] = 0;
3198 not = 1;
3199 }
3200
1cf41dd7 3201 return match_records(hash, buff, strlen(buff), mod, not);
64e7c440
SR
3202}
3203
f6180773
SR
3204/*
3205 * We register the module command as a template to show others how
3206 * to register the a command as well.
3207 */
3208
3209static int
43dd61c9
SR
3210ftrace_mod_callback(struct ftrace_hash *hash,
3211 char *func, char *cmd, char *param, int enable)
f6180773
SR
3212{
3213 char *mod;
b448c4e3 3214 int ret = -EINVAL;
f6180773
SR
3215
3216 /*
3217 * cmd == 'mod' because we only registered this func
3218 * for the 'mod' ftrace_func_command.
3219 * But if you register one func with multiple commands,
3220 * you can tell which command was used by the cmd
3221 * parameter.
3222 */
3223
3224 /* we must have a module name */
3225 if (!param)
b448c4e3 3226 return ret;
f6180773
SR
3227
3228 mod = strsep(&param, ":");
3229 if (!strlen(mod))
b448c4e3 3230 return ret;
f6180773 3231
1cf41dd7 3232 ret = ftrace_match_module_records(hash, func, mod);
b448c4e3
SR
3233 if (!ret)
3234 ret = -EINVAL;
3235 if (ret < 0)
3236 return ret;
3237
3238 return 0;
f6180773
SR
3239}
3240
3241static struct ftrace_func_command ftrace_mod_cmd = {
3242 .name = "mod",
3243 .func = ftrace_mod_callback,
3244};
3245
3246static int __init ftrace_mod_cmd_init(void)
3247{
3248 return register_ftrace_command(&ftrace_mod_cmd);
3249}
6f415672 3250core_initcall(ftrace_mod_cmd_init);
f6180773 3251
2f5f6ad9 3252static void function_trace_probe_call(unsigned long ip, unsigned long parent_ip,
a1e2e31d 3253 struct ftrace_ops *op, struct pt_regs *pt_regs)
59df055f 3254{
b6887d79 3255 struct ftrace_func_probe *entry;
59df055f 3256 struct hlist_head *hhd;
59df055f 3257 unsigned long key;
59df055f
SR
3258
3259 key = hash_long(ip, FTRACE_HASH_BITS);
3260
3261 hhd = &ftrace_func_hash[key];
3262
3263 if (hlist_empty(hhd))
3264 return;
3265
3266 /*
3267 * Disable preemption for these calls to prevent a RCU grace
3268 * period. This syncs the hash iteration and freeing of items
3269 * on the hash. rcu_read_lock is too dangerous here.
3270 */
5168ae50 3271 preempt_disable_notrace();
1bb539ca 3272 hlist_for_each_entry_rcu_notrace(entry, hhd, node) {
59df055f
SR
3273 if (entry->ip == ip)
3274 entry->ops->func(ip, parent_ip, &entry->data);
3275 }
5168ae50 3276 preempt_enable_notrace();
59df055f
SR
3277}
3278
b6887d79 3279static struct ftrace_ops trace_probe_ops __read_mostly =
59df055f 3280{
fb9fb015 3281 .func = function_trace_probe_call,
f04f24fb
MH
3282 .flags = FTRACE_OPS_FL_INITIALIZED,
3283 INIT_REGEX_LOCK(trace_probe_ops)
59df055f
SR
3284};
3285
b6887d79 3286static int ftrace_probe_registered;
59df055f 3287
b6887d79 3288static void __enable_ftrace_function_probe(void)
59df055f 3289{
b848914c 3290 int ret;
59df055f
SR
3291 int i;
3292
19dd603e
SRRH
3293 if (ftrace_probe_registered) {
3294 /* still need to update the function call sites */
3295 if (ftrace_enabled)
3296 ftrace_run_update_code(FTRACE_UPDATE_CALLS);
59df055f 3297 return;
19dd603e 3298 }
59df055f
SR
3299
3300 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
3301 struct hlist_head *hhd = &ftrace_func_hash[i];
3302 if (hhd->first)
3303 break;
3304 }
3305 /* Nothing registered? */
3306 if (i == FTRACE_FUNC_HASHSIZE)
3307 return;
3308
8a56d776 3309 ret = ftrace_startup(&trace_probe_ops, 0);
b848914c 3310
b6887d79 3311 ftrace_probe_registered = 1;
59df055f
SR
3312}
3313
b6887d79 3314static void __disable_ftrace_function_probe(void)
59df055f
SR
3315{
3316 int i;
3317
b6887d79 3318 if (!ftrace_probe_registered)
59df055f
SR
3319 return;
3320
3321 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
3322 struct hlist_head *hhd = &ftrace_func_hash[i];
3323 if (hhd->first)
3324 return;
3325 }
3326
3327 /* no more funcs left */
8a56d776 3328 ftrace_shutdown(&trace_probe_ops, 0);
b848914c 3329
b6887d79 3330 ftrace_probe_registered = 0;
59df055f
SR
3331}
3332
3333
7818b388 3334static void ftrace_free_entry(struct ftrace_func_probe *entry)
59df055f 3335{
59df055f 3336 if (entry->ops->free)
e67efb93 3337 entry->ops->free(entry->ops, entry->ip, &entry->data);
59df055f
SR
3338 kfree(entry);
3339}
3340
59df055f 3341int
b6887d79 3342register_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
59df055f
SR
3343 void *data)
3344{
b6887d79 3345 struct ftrace_func_probe *entry;
e1df4cb6
SRRH
3346 struct ftrace_hash **orig_hash = &trace_probe_ops.filter_hash;
3347 struct ftrace_hash *hash;
59df055f
SR
3348 struct ftrace_page *pg;
3349 struct dyn_ftrace *rec;
59df055f 3350 int type, len, not;
6a24a244 3351 unsigned long key;
59df055f
SR
3352 int count = 0;
3353 char *search;
e1df4cb6 3354 int ret;
59df055f 3355
3f6fe06d 3356 type = filter_parse_regex(glob, strlen(glob), &search, &not);
59df055f
SR
3357 len = strlen(search);
3358
b6887d79 3359 /* we do not support '!' for function probes */
59df055f
SR
3360 if (WARN_ON(not))
3361 return -EINVAL;
3362
3f2367ba 3363 mutex_lock(&trace_probe_ops.regex_lock);
59df055f 3364
e1df4cb6
SRRH
3365 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
3366 if (!hash) {
3367 count = -ENOMEM;
5ae0bf59 3368 goto out;
e1df4cb6
SRRH
3369 }
3370
3371 if (unlikely(ftrace_disabled)) {
3372 count = -ENODEV;
5ae0bf59 3373 goto out;
e1df4cb6 3374 }
59df055f 3375
5ae0bf59
SRRH
3376 mutex_lock(&ftrace_lock);
3377
45a4a237 3378 do_for_each_ftrace_rec(pg, rec) {
59df055f 3379
b9df92d2 3380 if (!ftrace_match_record(rec, NULL, search, len, type))
59df055f
SR
3381 continue;
3382
3383 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
3384 if (!entry) {
b6887d79 3385 /* If we did not process any, then return error */
59df055f
SR
3386 if (!count)
3387 count = -ENOMEM;
3388 goto out_unlock;
3389 }
3390
3391 count++;
3392
3393 entry->data = data;
3394
3395 /*
3396 * The caller might want to do something special
3397 * for each function we find. We call the callback
3398 * to give the caller an opportunity to do so.
3399 */
e67efb93
SRRH
3400 if (ops->init) {
3401 if (ops->init(ops, rec->ip, &entry->data) < 0) {
59df055f
SR
3402 /* caller does not like this func */
3403 kfree(entry);
3404 continue;
3405 }
3406 }
3407
e1df4cb6
SRRH
3408 ret = enter_record(hash, rec, 0);
3409 if (ret < 0) {
3410 kfree(entry);
3411 count = ret;
3412 goto out_unlock;
3413 }
3414
59df055f
SR
3415 entry->ops = ops;
3416 entry->ip = rec->ip;
3417
3418 key = hash_long(entry->ip, FTRACE_HASH_BITS);
3419 hlist_add_head_rcu(&entry->node, &ftrace_func_hash[key]);
3420
3421 } while_for_each_ftrace_rec();
e1df4cb6
SRRH
3422
3423 ret = ftrace_hash_move(&trace_probe_ops, 1, orig_hash, hash);
3424 if (ret < 0)
3425 count = ret;
3426
b6887d79 3427 __enable_ftrace_function_probe();
59df055f
SR
3428
3429 out_unlock:
5ae0bf59
SRRH
3430 mutex_unlock(&ftrace_lock);
3431 out:
3f2367ba 3432 mutex_unlock(&trace_probe_ops.regex_lock);
e1df4cb6 3433 free_ftrace_hash(hash);
59df055f
SR
3434
3435 return count;
3436}
3437
3438enum {
b6887d79
SR
3439 PROBE_TEST_FUNC = 1,
3440 PROBE_TEST_DATA = 2
59df055f
SR
3441};
3442
3443static void
b6887d79 3444__unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
59df055f
SR
3445 void *data, int flags)
3446{
e1df4cb6 3447 struct ftrace_func_entry *rec_entry;
b6887d79 3448 struct ftrace_func_probe *entry;
7818b388 3449 struct ftrace_func_probe *p;
e1df4cb6 3450 struct ftrace_hash **orig_hash = &trace_probe_ops.filter_hash;
7818b388 3451 struct list_head free_list;
e1df4cb6 3452 struct ftrace_hash *hash;
b67bfe0d 3453 struct hlist_node *tmp;
59df055f
SR
3454 char str[KSYM_SYMBOL_LEN];
3455 int type = MATCH_FULL;
3456 int i, len = 0;
3457 char *search;
3458
b36461da 3459 if (glob && (strcmp(glob, "*") == 0 || !strlen(glob)))
59df055f 3460 glob = NULL;
b36461da 3461 else if (glob) {
59df055f
SR
3462 int not;
3463
3f6fe06d 3464 type = filter_parse_regex(glob, strlen(glob), &search, &not);
59df055f
SR
3465 len = strlen(search);
3466
b6887d79 3467 /* we do not support '!' for function probes */
59df055f
SR
3468 if (WARN_ON(not))
3469 return;
3470 }
3471
3f2367ba 3472 mutex_lock(&trace_probe_ops.regex_lock);
e1df4cb6
SRRH
3473
3474 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
3475 if (!hash)
3476 /* Hmm, should report this somehow */
3477 goto out_unlock;
3478
7818b388
SRRH
3479 INIT_LIST_HEAD(&free_list);
3480
59df055f
SR
3481 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
3482 struct hlist_head *hhd = &ftrace_func_hash[i];
3483
b67bfe0d 3484 hlist_for_each_entry_safe(entry, tmp, hhd, node) {
59df055f
SR
3485
3486 /* break up if statements for readability */
b6887d79 3487 if ((flags & PROBE_TEST_FUNC) && entry->ops != ops)
59df055f
SR
3488 continue;
3489
b6887d79 3490 if ((flags & PROBE_TEST_DATA) && entry->data != data)
59df055f
SR
3491 continue;
3492
3493 /* do this last, since it is the most expensive */
3494 if (glob) {
3495 kallsyms_lookup(entry->ip, NULL, NULL,
3496 NULL, str);
3497 if (!ftrace_match(str, glob, len, type))
3498 continue;
3499 }
3500
e1df4cb6
SRRH
3501 rec_entry = ftrace_lookup_ip(hash, entry->ip);
3502 /* It is possible more than one entry had this ip */
3503 if (rec_entry)
3504 free_hash_entry(hash, rec_entry);
3505
740466bc 3506 hlist_del_rcu(&entry->node);
7818b388 3507 list_add(&entry->free_list, &free_list);
59df055f
SR
3508 }
3509 }
3f2367ba 3510 mutex_lock(&ftrace_lock);
b6887d79 3511 __disable_ftrace_function_probe();
e1df4cb6
SRRH
3512 /*
3513 * Remove after the disable is called. Otherwise, if the last
3514 * probe is removed, a null hash means *all enabled*.
3515 */
3516 ftrace_hash_move(&trace_probe_ops, 1, orig_hash, hash);
7818b388
SRRH
3517 synchronize_sched();
3518 list_for_each_entry_safe(entry, p, &free_list, free_list) {
3519 list_del(&entry->free_list);
3520 ftrace_free_entry(entry);
3521 }
3f2367ba 3522 mutex_unlock(&ftrace_lock);
7818b388 3523
e1df4cb6 3524 out_unlock:
3f2367ba 3525 mutex_unlock(&trace_probe_ops.regex_lock);
e1df4cb6 3526 free_ftrace_hash(hash);
59df055f
SR
3527}
3528
3529void
b6887d79 3530unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
59df055f
SR
3531 void *data)
3532{
b6887d79
SR
3533 __unregister_ftrace_function_probe(glob, ops, data,
3534 PROBE_TEST_FUNC | PROBE_TEST_DATA);
59df055f
SR
3535}
3536
3537void
b6887d79 3538unregister_ftrace_function_probe_func(char *glob, struct ftrace_probe_ops *ops)
59df055f 3539{
b6887d79 3540 __unregister_ftrace_function_probe(glob, ops, NULL, PROBE_TEST_FUNC);
59df055f
SR
3541}
3542
b6887d79 3543void unregister_ftrace_function_probe_all(char *glob)
59df055f 3544{
b6887d79 3545 __unregister_ftrace_function_probe(glob, NULL, NULL, 0);
59df055f
SR
3546}
3547
f6180773
SR
3548static LIST_HEAD(ftrace_commands);
3549static DEFINE_MUTEX(ftrace_cmd_mutex);
3550
38de93ab
TZ
3551/*
3552 * Currently we only register ftrace commands from __init, so mark this
3553 * __init too.
3554 */
3555__init int register_ftrace_command(struct ftrace_func_command *cmd)
f6180773
SR
3556{
3557 struct ftrace_func_command *p;
3558 int ret = 0;
3559
3560 mutex_lock(&ftrace_cmd_mutex);
3561 list_for_each_entry(p, &ftrace_commands, list) {
3562 if (strcmp(cmd->name, p->name) == 0) {
3563 ret = -EBUSY;
3564 goto out_unlock;
3565 }
3566 }
3567 list_add(&cmd->list, &ftrace_commands);
3568 out_unlock:
3569 mutex_unlock(&ftrace_cmd_mutex);
3570
3571 return ret;
3572}
3573
38de93ab
TZ
3574/*
3575 * Currently we only unregister ftrace commands from __init, so mark
3576 * this __init too.
3577 */
3578__init int unregister_ftrace_command(struct ftrace_func_command *cmd)
f6180773
SR
3579{
3580 struct ftrace_func_command *p, *n;
3581 int ret = -ENODEV;
3582
3583 mutex_lock(&ftrace_cmd_mutex);
3584 list_for_each_entry_safe(p, n, &ftrace_commands, list) {
3585 if (strcmp(cmd->name, p->name) == 0) {
3586 ret = 0;
3587 list_del_init(&p->list);
3588 goto out_unlock;
3589 }
3590 }
3591 out_unlock:
3592 mutex_unlock(&ftrace_cmd_mutex);
3593
3594 return ret;
3595}
3596
33dc9b12
SR
3597static int ftrace_process_regex(struct ftrace_hash *hash,
3598 char *buff, int len, int enable)
64e7c440 3599{
f6180773 3600 char *func, *command, *next = buff;
6a24a244 3601 struct ftrace_func_command *p;
0aff1c0c 3602 int ret = -EINVAL;
64e7c440
SR
3603
3604 func = strsep(&next, ":");
3605
3606 if (!next) {
1cf41dd7 3607 ret = ftrace_match_records(hash, func, len);
b448c4e3
SR
3608 if (!ret)
3609 ret = -EINVAL;
3610 if (ret < 0)
3611 return ret;
3612 return 0;
64e7c440
SR
3613 }
3614
f6180773 3615 /* command found */
64e7c440
SR
3616
3617 command = strsep(&next, ":");
3618
f6180773
SR
3619 mutex_lock(&ftrace_cmd_mutex);
3620 list_for_each_entry(p, &ftrace_commands, list) {
3621 if (strcmp(p->name, command) == 0) {
43dd61c9 3622 ret = p->func(hash, func, command, next, enable);
f6180773
SR
3623 goto out_unlock;
3624 }
64e7c440 3625 }
f6180773
SR
3626 out_unlock:
3627 mutex_unlock(&ftrace_cmd_mutex);
64e7c440 3628
f6180773 3629 return ret;
64e7c440
SR
3630}
3631
e309b41d 3632static ssize_t
41c52c0d
SR
3633ftrace_regex_write(struct file *file, const char __user *ubuf,
3634 size_t cnt, loff_t *ppos, int enable)
5072c59f
SR
3635{
3636 struct ftrace_iterator *iter;
689fd8b6 3637 struct trace_parser *parser;
3638 ssize_t ret, read;
5072c59f 3639
4ba7978e 3640 if (!cnt)
5072c59f
SR
3641 return 0;
3642
5072c59f
SR
3643 if (file->f_mode & FMODE_READ) {
3644 struct seq_file *m = file->private_data;
3645 iter = m->private;
3646 } else
3647 iter = file->private_data;
3648
f04f24fb 3649 if (unlikely(ftrace_disabled))
3f2367ba
MH
3650 return -ENODEV;
3651
3652 /* iter->hash is a local copy, so we don't need regex_lock */
f04f24fb 3653
689fd8b6 3654 parser = &iter->parser;
3655 read = trace_get_user(parser, ubuf, cnt, ppos);
5072c59f 3656
4ba7978e 3657 if (read >= 0 && trace_parser_loaded(parser) &&
689fd8b6 3658 !trace_parser_cont(parser)) {
33dc9b12 3659 ret = ftrace_process_regex(iter->hash, parser->buffer,
689fd8b6 3660 parser->idx, enable);
313254a9 3661 trace_parser_clear(parser);
7c088b51 3662 if (ret < 0)
3f2367ba 3663 goto out;
eda1e328 3664 }
5072c59f 3665
5072c59f 3666 ret = read;
3f2367ba 3667 out:
5072c59f
SR
3668 return ret;
3669}
3670
fc13cb0c 3671ssize_t
41c52c0d
SR
3672ftrace_filter_write(struct file *file, const char __user *ubuf,
3673 size_t cnt, loff_t *ppos)
3674{
3675 return ftrace_regex_write(file, ubuf, cnt, ppos, 1);
3676}
3677
fc13cb0c 3678ssize_t
41c52c0d
SR
3679ftrace_notrace_write(struct file *file, const char __user *ubuf,
3680 size_t cnt, loff_t *ppos)
3681{
3682 return ftrace_regex_write(file, ubuf, cnt, ppos, 0);
3683}
3684
33dc9b12 3685static int
647664ea
MH
3686ftrace_match_addr(struct ftrace_hash *hash, unsigned long ip, int remove)
3687{
3688 struct ftrace_func_entry *entry;
3689
3690 if (!ftrace_location(ip))
3691 return -EINVAL;
3692
3693 if (remove) {
3694 entry = ftrace_lookup_ip(hash, ip);
3695 if (!entry)
3696 return -ENOENT;
3697 free_hash_entry(hash, entry);
3698 return 0;
3699 }
3700
3701 return add_hash_entry(hash, ip);
3702}
3703
1c80c432
SRRH
3704static void ftrace_ops_update_code(struct ftrace_ops *ops)
3705{
3706 if (ops->flags & FTRACE_OPS_FL_ENABLED && ftrace_enabled)
3707 ftrace_run_update_code(FTRACE_UPDATE_CALLS);
3708}
3709
647664ea
MH
3710static int
3711ftrace_set_hash(struct ftrace_ops *ops, unsigned char *buf, int len,
3712 unsigned long ip, int remove, int reset, int enable)
41c52c0d 3713{
33dc9b12 3714 struct ftrace_hash **orig_hash;
f45948e8 3715 struct ftrace_hash *hash;
33dc9b12 3716 int ret;
f45948e8 3717
41c52c0d 3718 if (unlikely(ftrace_disabled))
33dc9b12 3719 return -ENODEV;
41c52c0d 3720
3f2367ba
MH
3721 mutex_lock(&ops->regex_lock);
3722
f45948e8 3723 if (enable)
33dc9b12 3724 orig_hash = &ops->filter_hash;
f45948e8 3725 else
33dc9b12
SR
3726 orig_hash = &ops->notrace_hash;
3727
3728 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
3f2367ba
MH
3729 if (!hash) {
3730 ret = -ENOMEM;
3731 goto out_regex_unlock;
3732 }
f45948e8 3733
41c52c0d 3734 if (reset)
1cf41dd7 3735 ftrace_filter_reset(hash);
ac483c44
JO
3736 if (buf && !ftrace_match_records(hash, buf, len)) {
3737 ret = -EINVAL;
3738 goto out_regex_unlock;
3739 }
647664ea
MH
3740 if (ip) {
3741 ret = ftrace_match_addr(hash, ip, remove);
3742 if (ret < 0)
3743 goto out_regex_unlock;
3744 }
33dc9b12
SR
3745
3746 mutex_lock(&ftrace_lock);
41fb61c2 3747 ret = ftrace_hash_move(ops, enable, orig_hash, hash);
1c80c432
SRRH
3748 if (!ret)
3749 ftrace_ops_update_code(ops);
072126f4 3750
33dc9b12
SR
3751 mutex_unlock(&ftrace_lock);
3752
ac483c44 3753 out_regex_unlock:
f04f24fb 3754 mutex_unlock(&ops->regex_lock);
33dc9b12
SR
3755
3756 free_ftrace_hash(hash);
3757 return ret;
41c52c0d
SR
3758}
3759
647664ea
MH
3760static int
3761ftrace_set_addr(struct ftrace_ops *ops, unsigned long ip, int remove,
3762 int reset, int enable)
3763{
3764 return ftrace_set_hash(ops, 0, 0, ip, remove, reset, enable);
3765}
3766
3767/**
3768 * ftrace_set_filter_ip - set a function to filter on in ftrace by address
3769 * @ops - the ops to set the filter with
3770 * @ip - the address to add to or remove from the filter.
3771 * @remove - non zero to remove the ip from the filter
3772 * @reset - non zero to reset all filters before applying this filter.
3773 *
3774 * Filters denote which functions should be enabled when tracing is enabled
3775 * If @ip is NULL, it failes to update filter.
3776 */
3777int ftrace_set_filter_ip(struct ftrace_ops *ops, unsigned long ip,
3778 int remove, int reset)
3779{
f04f24fb 3780 ftrace_ops_init(ops);
647664ea
MH
3781 return ftrace_set_addr(ops, ip, remove, reset, 1);
3782}
3783EXPORT_SYMBOL_GPL(ftrace_set_filter_ip);
3784
3785static int
3786ftrace_set_regex(struct ftrace_ops *ops, unsigned char *buf, int len,
3787 int reset, int enable)
3788{
3789 return ftrace_set_hash(ops, buf, len, 0, 0, reset, enable);
3790}
3791
77a2b37d
SR
3792/**
3793 * ftrace_set_filter - set a function to filter on in ftrace
936e074b
SR
3794 * @ops - the ops to set the filter with
3795 * @buf - the string that holds the function filter text.
3796 * @len - the length of the string.
3797 * @reset - non zero to reset all filters before applying this filter.
3798 *
3799 * Filters denote which functions should be enabled when tracing is enabled.
3800 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
3801 */
ac483c44 3802int ftrace_set_filter(struct ftrace_ops *ops, unsigned char *buf,
936e074b
SR
3803 int len, int reset)
3804{
f04f24fb 3805 ftrace_ops_init(ops);
ac483c44 3806 return ftrace_set_regex(ops, buf, len, reset, 1);
936e074b
SR
3807}
3808EXPORT_SYMBOL_GPL(ftrace_set_filter);
3809
3810/**
3811 * ftrace_set_notrace - set a function to not trace in ftrace
3812 * @ops - the ops to set the notrace filter with
3813 * @buf - the string that holds the function notrace text.
3814 * @len - the length of the string.
3815 * @reset - non zero to reset all filters before applying this filter.
3816 *
3817 * Notrace Filters denote which functions should not be enabled when tracing
3818 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
3819 * for tracing.
3820 */
ac483c44 3821int ftrace_set_notrace(struct ftrace_ops *ops, unsigned char *buf,
936e074b
SR
3822 int len, int reset)
3823{
f04f24fb 3824 ftrace_ops_init(ops);
ac483c44 3825 return ftrace_set_regex(ops, buf, len, reset, 0);
936e074b
SR
3826}
3827EXPORT_SYMBOL_GPL(ftrace_set_notrace);
3828/**
8d1b065d 3829 * ftrace_set_global_filter - set a function to filter on with global tracers
77a2b37d
SR
3830 * @buf - the string that holds the function filter text.
3831 * @len - the length of the string.
3832 * @reset - non zero to reset all filters before applying this filter.
3833 *
3834 * Filters denote which functions should be enabled when tracing is enabled.
3835 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
3836 */
936e074b 3837void ftrace_set_global_filter(unsigned char *buf, int len, int reset)
77a2b37d 3838{
f45948e8 3839 ftrace_set_regex(&global_ops, buf, len, reset, 1);
41c52c0d 3840}
936e074b 3841EXPORT_SYMBOL_GPL(ftrace_set_global_filter);
4eebcc81 3842
41c52c0d 3843/**
8d1b065d 3844 * ftrace_set_global_notrace - set a function to not trace with global tracers
41c52c0d
SR
3845 * @buf - the string that holds the function notrace text.
3846 * @len - the length of the string.
3847 * @reset - non zero to reset all filters before applying this filter.
3848 *
3849 * Notrace Filters denote which functions should not be enabled when tracing
3850 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
3851 * for tracing.
3852 */
936e074b 3853void ftrace_set_global_notrace(unsigned char *buf, int len, int reset)
41c52c0d 3854{
f45948e8 3855 ftrace_set_regex(&global_ops, buf, len, reset, 0);
77a2b37d 3856}
936e074b 3857EXPORT_SYMBOL_GPL(ftrace_set_global_notrace);
77a2b37d 3858
2af15d6a
SR
3859/*
3860 * command line interface to allow users to set filters on boot up.
3861 */
3862#define FTRACE_FILTER_SIZE COMMAND_LINE_SIZE
3863static char ftrace_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
3864static char ftrace_filter_buf[FTRACE_FILTER_SIZE] __initdata;
3865
f1ed7c74
SRRH
3866/* Used by function selftest to not test if filter is set */
3867bool ftrace_filter_param __initdata;
3868
2af15d6a
SR
3869static int __init set_ftrace_notrace(char *str)
3870{
f1ed7c74 3871 ftrace_filter_param = true;
75761cc1 3872 strlcpy(ftrace_notrace_buf, str, FTRACE_FILTER_SIZE);
2af15d6a
SR
3873 return 1;
3874}
3875__setup("ftrace_notrace=", set_ftrace_notrace);
3876
3877static int __init set_ftrace_filter(char *str)
3878{
f1ed7c74 3879 ftrace_filter_param = true;
75761cc1 3880 strlcpy(ftrace_filter_buf, str, FTRACE_FILTER_SIZE);
2af15d6a
SR
3881 return 1;
3882}
3883__setup("ftrace_filter=", set_ftrace_filter);
3884
369bc18f 3885#ifdef CONFIG_FUNCTION_GRAPH_TRACER
f6060f46 3886static char ftrace_graph_buf[FTRACE_FILTER_SIZE] __initdata;
faf982a6 3887static int ftrace_set_func(unsigned long *array, int *idx, int size, char *buffer);
801c29fd 3888
369bc18f
SA
3889static int __init set_graph_function(char *str)
3890{
06f43d66 3891 strlcpy(ftrace_graph_buf, str, FTRACE_FILTER_SIZE);
369bc18f
SA
3892 return 1;
3893}
3894__setup("ftrace_graph_filter=", set_graph_function);
3895
3896static void __init set_ftrace_early_graph(char *buf)
3897{
3898 int ret;
3899 char *func;
3900
3901 while (buf) {
3902 func = strsep(&buf, ",");
3903 /* we allow only one expression at a time */
3904 ret = ftrace_set_func(ftrace_graph_funcs, &ftrace_graph_count,
faf982a6 3905 FTRACE_GRAPH_MAX_FUNCS, func);
369bc18f
SA
3906 if (ret)
3907 printk(KERN_DEBUG "ftrace: function %s not "
3908 "traceable\n", func);
3909 }
3910}
3911#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
3912
2a85a37f
SR
3913void __init
3914ftrace_set_early_filter(struct ftrace_ops *ops, char *buf, int enable)
2af15d6a
SR
3915{
3916 char *func;
3917
f04f24fb
MH
3918 ftrace_ops_init(ops);
3919
2af15d6a
SR
3920 while (buf) {
3921 func = strsep(&buf, ",");
f45948e8 3922 ftrace_set_regex(ops, func, strlen(func), 0, enable);
2af15d6a
SR
3923 }
3924}
3925
3926static void __init set_ftrace_early_filters(void)
3927{
3928 if (ftrace_filter_buf[0])
2a85a37f 3929 ftrace_set_early_filter(&global_ops, ftrace_filter_buf, 1);
2af15d6a 3930 if (ftrace_notrace_buf[0])
2a85a37f 3931 ftrace_set_early_filter(&global_ops, ftrace_notrace_buf, 0);
369bc18f
SA
3932#ifdef CONFIG_FUNCTION_GRAPH_TRACER
3933 if (ftrace_graph_buf[0])
3934 set_ftrace_early_graph(ftrace_graph_buf);
3935#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
2af15d6a
SR
3936}
3937
fc13cb0c 3938int ftrace_regex_release(struct inode *inode, struct file *file)
5072c59f
SR
3939{
3940 struct seq_file *m = (struct seq_file *)file->private_data;
3941 struct ftrace_iterator *iter;
33dc9b12 3942 struct ftrace_hash **orig_hash;
689fd8b6 3943 struct trace_parser *parser;
ed926f9b 3944 int filter_hash;
33dc9b12 3945 int ret;
5072c59f 3946
5072c59f
SR
3947 if (file->f_mode & FMODE_READ) {
3948 iter = m->private;
5072c59f
SR
3949 seq_release(inode, file);
3950 } else
3951 iter = file->private_data;
3952
689fd8b6 3953 parser = &iter->parser;
3954 if (trace_parser_loaded(parser)) {
3955 parser->buffer[parser->idx] = 0;
1cf41dd7 3956 ftrace_match_records(iter->hash, parser->buffer, parser->idx);
5072c59f
SR
3957 }
3958
689fd8b6 3959 trace_parser_put(parser);
689fd8b6 3960
3f2367ba
MH
3961 mutex_lock(&iter->ops->regex_lock);
3962
058e297d 3963 if (file->f_mode & FMODE_WRITE) {
ed926f9b
SR
3964 filter_hash = !!(iter->flags & FTRACE_ITER_FILTER);
3965
3966 if (filter_hash)
33dc9b12 3967 orig_hash = &iter->ops->filter_hash;
ed926f9b
SR
3968 else
3969 orig_hash = &iter->ops->notrace_hash;
33dc9b12 3970
058e297d 3971 mutex_lock(&ftrace_lock);
41fb61c2
SR
3972 ret = ftrace_hash_move(iter->ops, filter_hash,
3973 orig_hash, iter->hash);
1c80c432
SRRH
3974 if (!ret)
3975 ftrace_ops_update_code(iter->ops);
41fb61c2 3976
058e297d
SR
3977 mutex_unlock(&ftrace_lock);
3978 }
3f2367ba
MH
3979
3980 mutex_unlock(&iter->ops->regex_lock);
33dc9b12
SR
3981 free_ftrace_hash(iter->hash);
3982 kfree(iter);
058e297d 3983
5072c59f
SR
3984 return 0;
3985}
3986
5e2336a0 3987static const struct file_operations ftrace_avail_fops = {
5072c59f
SR
3988 .open = ftrace_avail_open,
3989 .read = seq_read,
3990 .llseek = seq_lseek,
3be04b47 3991 .release = seq_release_private,
5072c59f
SR
3992};
3993
647bcd03
SR
3994static const struct file_operations ftrace_enabled_fops = {
3995 .open = ftrace_enabled_open,
3996 .read = seq_read,
3997 .llseek = seq_lseek,
3998 .release = seq_release_private,
3999};
4000
5e2336a0 4001static const struct file_operations ftrace_filter_fops = {
5072c59f 4002 .open = ftrace_filter_open,
850a80cf 4003 .read = seq_read,
5072c59f 4004 .write = ftrace_filter_write,
098c879e 4005 .llseek = tracing_lseek,
1cf41dd7 4006 .release = ftrace_regex_release,
5072c59f
SR
4007};
4008
5e2336a0 4009static const struct file_operations ftrace_notrace_fops = {
41c52c0d 4010 .open = ftrace_notrace_open,
850a80cf 4011 .read = seq_read,
41c52c0d 4012 .write = ftrace_notrace_write,
098c879e 4013 .llseek = tracing_lseek,
1cf41dd7 4014 .release = ftrace_regex_release,
41c52c0d
SR
4015};
4016
ea4e2bc4
SR
4017#ifdef CONFIG_FUNCTION_GRAPH_TRACER
4018
4019static DEFINE_MUTEX(graph_lock);
4020
4021int ftrace_graph_count;
29ad23b0 4022int ftrace_graph_notrace_count;
ea4e2bc4 4023unsigned long ftrace_graph_funcs[FTRACE_GRAPH_MAX_FUNCS] __read_mostly;
29ad23b0 4024unsigned long ftrace_graph_notrace_funcs[FTRACE_GRAPH_MAX_FUNCS] __read_mostly;
ea4e2bc4 4025
faf982a6
NK
4026struct ftrace_graph_data {
4027 unsigned long *table;
4028 size_t size;
4029 int *count;
4030 const struct seq_operations *seq_ops;
4031};
4032
ea4e2bc4 4033static void *
85951842 4034__g_next(struct seq_file *m, loff_t *pos)
ea4e2bc4 4035{
faf982a6
NK
4036 struct ftrace_graph_data *fgd = m->private;
4037
4038 if (*pos >= *fgd->count)
ea4e2bc4 4039 return NULL;
faf982a6 4040 return &fgd->table[*pos];
85951842 4041}
ea4e2bc4 4042
85951842
LZ
4043static void *
4044g_next(struct seq_file *m, void *v, loff_t *pos)
4045{
4046 (*pos)++;
4047 return __g_next(m, pos);
ea4e2bc4
SR
4048}
4049
4050static void *g_start(struct seq_file *m, loff_t *pos)
4051{
faf982a6
NK
4052 struct ftrace_graph_data *fgd = m->private;
4053
ea4e2bc4
SR
4054 mutex_lock(&graph_lock);
4055
f9349a8f 4056 /* Nothing, tell g_show to print all functions are enabled */
faf982a6 4057 if (!*fgd->count && !*pos)
f9349a8f
FW
4058 return (void *)1;
4059
85951842 4060 return __g_next(m, pos);
ea4e2bc4
SR
4061}
4062
4063static void g_stop(struct seq_file *m, void *p)
4064{
4065 mutex_unlock(&graph_lock);
4066}
4067
4068static int g_show(struct seq_file *m, void *v)
4069{
4070 unsigned long *ptr = v;
ea4e2bc4
SR
4071
4072 if (!ptr)
4073 return 0;
4074
f9349a8f
FW
4075 if (ptr == (unsigned long *)1) {
4076 seq_printf(m, "#### all functions enabled ####\n");
4077 return 0;
4078 }
4079
b375a11a 4080 seq_printf(m, "%ps\n", (void *)*ptr);
ea4e2bc4
SR
4081
4082 return 0;
4083}
4084
88e9d34c 4085static const struct seq_operations ftrace_graph_seq_ops = {
ea4e2bc4
SR
4086 .start = g_start,
4087 .next = g_next,
4088 .stop = g_stop,
4089 .show = g_show,
4090};
4091
4092static int
faf982a6
NK
4093__ftrace_graph_open(struct inode *inode, struct file *file,
4094 struct ftrace_graph_data *fgd)
ea4e2bc4
SR
4095{
4096 int ret = 0;
4097
ea4e2bc4
SR
4098 mutex_lock(&graph_lock);
4099 if ((file->f_mode & FMODE_WRITE) &&
8650ae32 4100 (file->f_flags & O_TRUNC)) {
faf982a6
NK
4101 *fgd->count = 0;
4102 memset(fgd->table, 0, fgd->size * sizeof(*fgd->table));
ea4e2bc4 4103 }
a4ec5e0c 4104 mutex_unlock(&graph_lock);
ea4e2bc4 4105
faf982a6
NK
4106 if (file->f_mode & FMODE_READ) {
4107 ret = seq_open(file, fgd->seq_ops);
4108 if (!ret) {
4109 struct seq_file *m = file->private_data;
4110 m->private = fgd;
4111 }
4112 } else
4113 file->private_data = fgd;
ea4e2bc4
SR
4114
4115 return ret;
4116}
4117
faf982a6
NK
4118static int
4119ftrace_graph_open(struct inode *inode, struct file *file)
4120{
4121 struct ftrace_graph_data *fgd;
4122
4123 if (unlikely(ftrace_disabled))
4124 return -ENODEV;
4125
4126 fgd = kmalloc(sizeof(*fgd), GFP_KERNEL);
4127 if (fgd == NULL)
4128 return -ENOMEM;
4129
4130 fgd->table = ftrace_graph_funcs;
4131 fgd->size = FTRACE_GRAPH_MAX_FUNCS;
4132 fgd->count = &ftrace_graph_count;
4133 fgd->seq_ops = &ftrace_graph_seq_ops;
4134
4135 return __ftrace_graph_open(inode, file, fgd);
4136}
4137
29ad23b0
NK
4138static int
4139ftrace_graph_notrace_open(struct inode *inode, struct file *file)
4140{
4141 struct ftrace_graph_data *fgd;
4142
4143 if (unlikely(ftrace_disabled))
4144 return -ENODEV;
4145
4146 fgd = kmalloc(sizeof(*fgd), GFP_KERNEL);
4147 if (fgd == NULL)
4148 return -ENOMEM;
4149
4150 fgd->table = ftrace_graph_notrace_funcs;
4151 fgd->size = FTRACE_GRAPH_MAX_FUNCS;
4152 fgd->count = &ftrace_graph_notrace_count;
4153 fgd->seq_ops = &ftrace_graph_seq_ops;
4154
4155 return __ftrace_graph_open(inode, file, fgd);
4156}
4157
87827111
LZ
4158static int
4159ftrace_graph_release(struct inode *inode, struct file *file)
4160{
faf982a6
NK
4161 if (file->f_mode & FMODE_READ) {
4162 struct seq_file *m = file->private_data;
4163
4164 kfree(m->private);
87827111 4165 seq_release(inode, file);
faf982a6
NK
4166 } else {
4167 kfree(file->private_data);
4168 }
4169
87827111
LZ
4170 return 0;
4171}
4172
ea4e2bc4 4173static int
faf982a6 4174ftrace_set_func(unsigned long *array, int *idx, int size, char *buffer)
ea4e2bc4 4175{
ea4e2bc4
SR
4176 struct dyn_ftrace *rec;
4177 struct ftrace_page *pg;
f9349a8f 4178 int search_len;
c7c6b1fe 4179 int fail = 1;
f9349a8f
FW
4180 int type, not;
4181 char *search;
4182 bool exists;
4183 int i;
ea4e2bc4 4184
f9349a8f 4185 /* decode regex */
3f6fe06d 4186 type = filter_parse_regex(buffer, strlen(buffer), &search, &not);
faf982a6 4187 if (!not && *idx >= size)
c7c6b1fe 4188 return -EBUSY;
f9349a8f
FW
4189
4190 search_len = strlen(search);
4191
52baf119 4192 mutex_lock(&ftrace_lock);
45a4a237
SR
4193
4194 if (unlikely(ftrace_disabled)) {
4195 mutex_unlock(&ftrace_lock);
4196 return -ENODEV;
4197 }
4198
265c831c
SR
4199 do_for_each_ftrace_rec(pg, rec) {
4200
b9df92d2 4201 if (ftrace_match_record(rec, NULL, search, search_len, type)) {
c7c6b1fe 4202 /* if it is in the array */
f9349a8f 4203 exists = false;
c7c6b1fe 4204 for (i = 0; i < *idx; i++) {
f9349a8f
FW
4205 if (array[i] == rec->ip) {
4206 exists = true;
265c831c
SR
4207 break;
4208 }
c7c6b1fe
LZ
4209 }
4210
4211 if (!not) {
4212 fail = 0;
4213 if (!exists) {
4214 array[(*idx)++] = rec->ip;
faf982a6 4215 if (*idx >= size)
c7c6b1fe
LZ
4216 goto out;
4217 }
4218 } else {
4219 if (exists) {
4220 array[i] = array[--(*idx)];
4221 array[*idx] = 0;
4222 fail = 0;
4223 }
4224 }
ea4e2bc4 4225 }
265c831c 4226 } while_for_each_ftrace_rec();
c7c6b1fe 4227out:
52baf119 4228 mutex_unlock(&ftrace_lock);
ea4e2bc4 4229
c7c6b1fe
LZ
4230 if (fail)
4231 return -EINVAL;
4232
c7c6b1fe 4233 return 0;
ea4e2bc4
SR
4234}
4235
4236static ssize_t
4237ftrace_graph_write(struct file *file, const char __user *ubuf,
4238 size_t cnt, loff_t *ppos)
4239{
689fd8b6 4240 struct trace_parser parser;
6a10108b 4241 ssize_t read, ret = 0;
faf982a6 4242 struct ftrace_graph_data *fgd = file->private_data;
ea4e2bc4 4243
c7c6b1fe 4244 if (!cnt)
ea4e2bc4
SR
4245 return 0;
4246
6a10108b
NK
4247 if (trace_parser_get_init(&parser, FTRACE_BUFF_MAX))
4248 return -ENOMEM;
ea4e2bc4 4249
689fd8b6 4250 read = trace_get_user(&parser, ubuf, cnt, ppos);
ea4e2bc4 4251
4ba7978e 4252 if (read >= 0 && trace_parser_loaded((&parser))) {
689fd8b6 4253 parser.buffer[parser.idx] = 0;
4254
6a10108b
NK
4255 mutex_lock(&graph_lock);
4256
689fd8b6 4257 /* we allow only one expression at a time */
faf982a6
NK
4258 ret = ftrace_set_func(fgd->table, fgd->count, fgd->size,
4259 parser.buffer);
6a10108b
NK
4260
4261 mutex_unlock(&graph_lock);
ea4e2bc4 4262 }
ea4e2bc4 4263
6a10108b
NK
4264 if (!ret)
4265 ret = read;
1eb90f13 4266
689fd8b6 4267 trace_parser_put(&parser);
ea4e2bc4
SR
4268
4269 return ret;
4270}
4271
4272static const struct file_operations ftrace_graph_fops = {
87827111
LZ
4273 .open = ftrace_graph_open,
4274 .read = seq_read,
4275 .write = ftrace_graph_write,
098c879e 4276 .llseek = tracing_lseek,
87827111 4277 .release = ftrace_graph_release,
ea4e2bc4 4278};
29ad23b0
NK
4279
4280static const struct file_operations ftrace_graph_notrace_fops = {
4281 .open = ftrace_graph_notrace_open,
4282 .read = seq_read,
4283 .write = ftrace_graph_write,
098c879e 4284 .llseek = tracing_lseek,
29ad23b0
NK
4285 .release = ftrace_graph_release,
4286};
ea4e2bc4
SR
4287#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
4288
591dffda
SRRH
4289void ftrace_create_filter_files(struct ftrace_ops *ops,
4290 struct dentry *parent)
4291{
4292
4293 trace_create_file("set_ftrace_filter", 0644, parent,
4294 ops, &ftrace_filter_fops);
4295
4296 trace_create_file("set_ftrace_notrace", 0644, parent,
4297 ops, &ftrace_notrace_fops);
4298}
4299
4300/*
4301 * The name "destroy_filter_files" is really a misnomer. Although
4302 * in the future, it may actualy delete the files, but this is
4303 * really intended to make sure the ops passed in are disabled
4304 * and that when this function returns, the caller is free to
4305 * free the ops.
4306 *
4307 * The "destroy" name is only to match the "create" name that this
4308 * should be paired with.
4309 */
4310void ftrace_destroy_filter_files(struct ftrace_ops *ops)
4311{
4312 mutex_lock(&ftrace_lock);
4313 if (ops->flags & FTRACE_OPS_FL_ENABLED)
4314 ftrace_shutdown(ops, 0);
4315 ops->flags |= FTRACE_OPS_FL_DELETED;
4316 mutex_unlock(&ftrace_lock);
4317}
4318
df4fc315 4319static __init int ftrace_init_dyn_debugfs(struct dentry *d_tracer)
5072c59f 4320{
5072c59f 4321
5452af66
FW
4322 trace_create_file("available_filter_functions", 0444,
4323 d_tracer, NULL, &ftrace_avail_fops);
5072c59f 4324
647bcd03
SR
4325 trace_create_file("enabled_functions", 0444,
4326 d_tracer, NULL, &ftrace_enabled_fops);
4327
591dffda 4328 ftrace_create_filter_files(&global_ops, d_tracer);
ad90c0e3 4329
ea4e2bc4 4330#ifdef CONFIG_FUNCTION_GRAPH_TRACER
5452af66 4331 trace_create_file("set_graph_function", 0444, d_tracer,
ea4e2bc4
SR
4332 NULL,
4333 &ftrace_graph_fops);
29ad23b0
NK
4334 trace_create_file("set_graph_notrace", 0444, d_tracer,
4335 NULL,
4336 &ftrace_graph_notrace_fops);
ea4e2bc4
SR
4337#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
4338
5072c59f
SR
4339 return 0;
4340}
4341
9fd49328 4342static int ftrace_cmp_ips(const void *a, const void *b)
68950619 4343{
9fd49328
SR
4344 const unsigned long *ipa = a;
4345 const unsigned long *ipb = b;
68950619 4346
9fd49328
SR
4347 if (*ipa > *ipb)
4348 return 1;
4349 if (*ipa < *ipb)
4350 return -1;
4351 return 0;
4352}
4353
4354static void ftrace_swap_ips(void *a, void *b, int size)
4355{
4356 unsigned long *ipa = a;
4357 unsigned long *ipb = b;
4358 unsigned long t;
4359
4360 t = *ipa;
4361 *ipa = *ipb;
4362 *ipb = t;
68950619
SR
4363}
4364
5cb084bb 4365static int ftrace_process_locs(struct module *mod,
31e88909 4366 unsigned long *start,
68bf21aa
SR
4367 unsigned long *end)
4368{
706c81f8 4369 struct ftrace_page *start_pg;
a7900875 4370 struct ftrace_page *pg;
706c81f8 4371 struct dyn_ftrace *rec;
a7900875 4372 unsigned long count;
68bf21aa
SR
4373 unsigned long *p;
4374 unsigned long addr;
4376cac6 4375 unsigned long flags = 0; /* Shut up gcc */
a7900875
SR
4376 int ret = -ENOMEM;
4377
4378 count = end - start;
4379
4380 if (!count)
4381 return 0;
4382
9fd49328
SR
4383 sort(start, count, sizeof(*start),
4384 ftrace_cmp_ips, ftrace_swap_ips);
4385
706c81f8
SR
4386 start_pg = ftrace_allocate_pages(count);
4387 if (!start_pg)
a7900875 4388 return -ENOMEM;
68bf21aa 4389
e6ea44e9 4390 mutex_lock(&ftrace_lock);
a7900875 4391
32082309
SR
4392 /*
4393 * Core and each module needs their own pages, as
4394 * modules will free them when they are removed.
4395 * Force a new page to be allocated for modules.
4396 */
a7900875
SR
4397 if (!mod) {
4398 WARN_ON(ftrace_pages || ftrace_pages_start);
4399 /* First initialization */
706c81f8 4400 ftrace_pages = ftrace_pages_start = start_pg;
a7900875 4401 } else {
32082309 4402 if (!ftrace_pages)
a7900875 4403 goto out;
32082309 4404
a7900875
SR
4405 if (WARN_ON(ftrace_pages->next)) {
4406 /* Hmm, we have free pages? */
4407 while (ftrace_pages->next)
4408 ftrace_pages = ftrace_pages->next;
32082309 4409 }
a7900875 4410
706c81f8 4411 ftrace_pages->next = start_pg;
32082309
SR
4412 }
4413
68bf21aa 4414 p = start;
706c81f8 4415 pg = start_pg;
68bf21aa
SR
4416 while (p < end) {
4417 addr = ftrace_call_adjust(*p++);
20e5227e
SR
4418 /*
4419 * Some architecture linkers will pad between
4420 * the different mcount_loc sections of different
4421 * object files to satisfy alignments.
4422 * Skip any NULL pointers.
4423 */
4424 if (!addr)
4425 continue;
706c81f8
SR
4426
4427 if (pg->index == pg->size) {
4428 /* We should have allocated enough */
4429 if (WARN_ON(!pg->next))
4430 break;
4431 pg = pg->next;
4432 }
4433
4434 rec = &pg->records[pg->index++];
4435 rec->ip = addr;
68bf21aa
SR
4436 }
4437
706c81f8
SR
4438 /* We should have used all pages */
4439 WARN_ON(pg->next);
4440
4441 /* Assign the last page to ftrace_pages */
4442 ftrace_pages = pg;
4443
a4f18ed1 4444 /*
4376cac6
SR
4445 * We only need to disable interrupts on start up
4446 * because we are modifying code that an interrupt
4447 * may execute, and the modification is not atomic.
4448 * But for modules, nothing runs the code we modify
4449 * until we are finished with it, and there's no
4450 * reason to cause large interrupt latencies while we do it.
a4f18ed1 4451 */
4376cac6
SR
4452 if (!mod)
4453 local_irq_save(flags);
1dc43cf0 4454 ftrace_update_code(mod, start_pg);
4376cac6
SR
4455 if (!mod)
4456 local_irq_restore(flags);
a7900875
SR
4457 ret = 0;
4458 out:
e6ea44e9 4459 mutex_unlock(&ftrace_lock);
68bf21aa 4460
a7900875 4461 return ret;
68bf21aa
SR
4462}
4463
93eb677d 4464#ifdef CONFIG_MODULES
32082309
SR
4465
4466#define next_to_ftrace_page(p) container_of(p, struct ftrace_page, next)
4467
e7247a15 4468void ftrace_release_mod(struct module *mod)
93eb677d
SR
4469{
4470 struct dyn_ftrace *rec;
32082309 4471 struct ftrace_page **last_pg;
93eb677d 4472 struct ftrace_page *pg;
a7900875 4473 int order;
93eb677d 4474
45a4a237
SR
4475 mutex_lock(&ftrace_lock);
4476
e7247a15 4477 if (ftrace_disabled)
45a4a237 4478 goto out_unlock;
93eb677d 4479
32082309
SR
4480 /*
4481 * Each module has its own ftrace_pages, remove
4482 * them from the list.
4483 */
4484 last_pg = &ftrace_pages_start;
4485 for (pg = ftrace_pages_start; pg; pg = *last_pg) {
4486 rec = &pg->records[0];
e7247a15 4487 if (within_module_core(rec->ip, mod)) {
93eb677d 4488 /*
32082309
SR
4489 * As core pages are first, the first
4490 * page should never be a module page.
93eb677d 4491 */
32082309
SR
4492 if (WARN_ON(pg == ftrace_pages_start))
4493 goto out_unlock;
4494
4495 /* Check if we are deleting the last page */
4496 if (pg == ftrace_pages)
4497 ftrace_pages = next_to_ftrace_page(last_pg);
4498
4499 *last_pg = pg->next;
a7900875
SR
4500 order = get_count_order(pg->size / ENTRIES_PER_PAGE);
4501 free_pages((unsigned long)pg->records, order);
4502 kfree(pg);
32082309
SR
4503 } else
4504 last_pg = &pg->next;
4505 }
45a4a237 4506 out_unlock:
93eb677d
SR
4507 mutex_unlock(&ftrace_lock);
4508}
4509
4510static void ftrace_init_module(struct module *mod,
4511 unsigned long *start, unsigned long *end)
90d595fe 4512{
00fd61ae 4513 if (ftrace_disabled || start == end)
fed1939c 4514 return;
5cb084bb 4515 ftrace_process_locs(mod, start, end);
90d595fe
SR
4516}
4517
a949ae56 4518void ftrace_module_init(struct module *mod)
93eb677d 4519{
a949ae56
SRRH
4520 ftrace_init_module(mod, mod->ftrace_callsites,
4521 mod->ftrace_callsites +
4522 mod->num_ftrace_callsites);
8c189ea6
SRRH
4523}
4524
4525static int ftrace_module_notify_exit(struct notifier_block *self,
4526 unsigned long val, void *data)
4527{
4528 struct module *mod = data;
4529
4530 if (val == MODULE_STATE_GOING)
e7247a15 4531 ftrace_release_mod(mod);
93eb677d
SR
4532
4533 return 0;
4534}
4535#else
8c189ea6
SRRH
4536static int ftrace_module_notify_exit(struct notifier_block *self,
4537 unsigned long val, void *data)
93eb677d
SR
4538{
4539 return 0;
4540}
4541#endif /* CONFIG_MODULES */
4542
8c189ea6
SRRH
4543struct notifier_block ftrace_module_exit_nb = {
4544 .notifier_call = ftrace_module_notify_exit,
4545 .priority = INT_MIN, /* Run after anything that can remove kprobes */
4546};
4547
68bf21aa
SR
4548void __init ftrace_init(void)
4549{
1dc43cf0
JS
4550 extern unsigned long __start_mcount_loc[];
4551 extern unsigned long __stop_mcount_loc[];
3a36cb11 4552 unsigned long count, flags;
68bf21aa
SR
4553 int ret;
4554
68bf21aa 4555 local_irq_save(flags);
3a36cb11 4556 ret = ftrace_dyn_arch_init();
68bf21aa 4557 local_irq_restore(flags);
af64a7cb 4558 if (ret)
68bf21aa
SR
4559 goto failed;
4560
4561 count = __stop_mcount_loc - __start_mcount_loc;
c867ccd8
JS
4562 if (!count) {
4563 pr_info("ftrace: No functions to be traced?\n");
68bf21aa 4564 goto failed;
c867ccd8
JS
4565 }
4566
4567 pr_info("ftrace: allocating %ld entries in %ld pages\n",
4568 count, count / ENTRIES_PER_PAGE + 1);
68bf21aa
SR
4569
4570 last_ftrace_enabled = ftrace_enabled = 1;
4571
5cb084bb 4572 ret = ftrace_process_locs(NULL,
31e88909 4573 __start_mcount_loc,
68bf21aa
SR
4574 __stop_mcount_loc);
4575
8c189ea6 4576 ret = register_module_notifier(&ftrace_module_exit_nb);
24ed0c4b 4577 if (ret)
8c189ea6 4578 pr_warning("Failed to register trace ftrace module exit notifier\n");
93eb677d 4579
2af15d6a
SR
4580 set_ftrace_early_filters();
4581
68bf21aa
SR
4582 return;
4583 failed:
4584 ftrace_disabled = 1;
4585}
68bf21aa 4586
3d083395 4587#else
0b6e4d56 4588
2b499381 4589static struct ftrace_ops global_ops = {
bd69c30b 4590 .func = ftrace_stub,
f04f24fb
MH
4591 .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_INITIALIZED,
4592 INIT_REGEX_LOCK(global_ops)
bd69c30b
SR
4593};
4594
0b6e4d56
FW
4595static int __init ftrace_nodyn_init(void)
4596{
4597 ftrace_enabled = 1;
4598 return 0;
4599}
6f415672 4600core_initcall(ftrace_nodyn_init);
0b6e4d56 4601
df4fc315
SR
4602static inline int ftrace_init_dyn_debugfs(struct dentry *d_tracer) { return 0; }
4603static inline void ftrace_startup_enable(int command) { }
5a45cfe1 4604/* Keep as macros so we do not need to define the commands */
8a56d776
SRRH
4605# define ftrace_startup(ops, command) \
4606 ({ \
4607 int ___ret = __register_ftrace_function(ops); \
4608 if (!___ret) \
4609 (ops)->flags |= FTRACE_OPS_FL_ENABLED; \
4610 ___ret; \
3b6cfdb1 4611 })
1fcc1553
SRRH
4612# define ftrace_shutdown(ops, command) \
4613 ({ \
4614 int ___ret = __unregister_ftrace_function(ops); \
4615 if (!___ret) \
4616 (ops)->flags &= ~FTRACE_OPS_FL_ENABLED; \
4617 ___ret; \
4618 })
8a56d776 4619
c7aafc54
IM
4620# define ftrace_startup_sysctl() do { } while (0)
4621# define ftrace_shutdown_sysctl() do { } while (0)
b848914c
SR
4622
4623static inline int
195a8afc 4624ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip, void *regs)
b848914c
SR
4625{
4626 return 1;
4627}
4628
3d083395
SR
4629#endif /* CONFIG_DYNAMIC_FTRACE */
4630
4104d326
SRRH
4631__init void ftrace_init_global_array_ops(struct trace_array *tr)
4632{
4633 tr->ops = &global_ops;
4634 tr->ops->private = tr;
4635}
4636
4637void ftrace_init_array_ops(struct trace_array *tr, ftrace_func_t func)
4638{
4639 /* If we filter on pids, update to use the pid function */
4640 if (tr->flags & TRACE_ARRAY_FL_GLOBAL) {
4641 if (WARN_ON(tr->ops->func != ftrace_stub))
4642 printk("ftrace ops had %pS for function\n",
4643 tr->ops->func);
4644 /* Only the top level instance does pid tracing */
4645 if (!list_empty(&ftrace_pids)) {
4646 set_ftrace_pid_function(func);
4647 func = ftrace_pid_func;
4648 }
4649 }
4650 tr->ops->func = func;
4651 tr->ops->private = tr;
4652}
4653
4654void ftrace_reset_array_ops(struct trace_array *tr)
4655{
4656 tr->ops->func = ftrace_stub;
4657}
4658
e248491a 4659static void
2f5f6ad9 4660ftrace_ops_control_func(unsigned long ip, unsigned long parent_ip,
a1e2e31d 4661 struct ftrace_ops *op, struct pt_regs *regs)
e248491a 4662{
e248491a
JO
4663 if (unlikely(trace_recursion_test(TRACE_CONTROL_BIT)))
4664 return;
4665
4666 /*
4667 * Some of the ops may be dynamically allocated,
4668 * they must be freed after a synchronize_sched().
4669 */
4670 preempt_disable_notrace();
4671 trace_recursion_set(TRACE_CONTROL_BIT);
b5aa3a47
SRRH
4672
4673 /*
4674 * Control funcs (perf) uses RCU. Only trace if
4675 * RCU is currently active.
4676 */
4677 if (!rcu_is_watching())
4678 goto out;
4679
0a016409 4680 do_for_each_ftrace_op(op, ftrace_control_list) {
395b97a3
SRRH
4681 if (!(op->flags & FTRACE_OPS_FL_STUB) &&
4682 !ftrace_function_local_disabled(op) &&
195a8afc 4683 ftrace_ops_test(op, ip, regs))
a1e2e31d 4684 op->func(ip, parent_ip, op, regs);
0a016409 4685 } while_for_each_ftrace_op(op);
b5aa3a47 4686 out:
e248491a
JO
4687 trace_recursion_clear(TRACE_CONTROL_BIT);
4688 preempt_enable_notrace();
4689}
4690
4691static struct ftrace_ops control_ops = {
f04f24fb
MH
4692 .func = ftrace_ops_control_func,
4693 .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_INITIALIZED,
4694 INIT_REGEX_LOCK(control_ops)
e248491a
JO
4695};
4696
2f5f6ad9
SR
4697static inline void
4698__ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
a1e2e31d 4699 struct ftrace_ops *ignored, struct pt_regs *regs)
b848914c 4700{
cdbe61bf 4701 struct ftrace_ops *op;
edc15caf 4702 int bit;
b848914c 4703
ccf3672d
SR
4704 if (function_trace_stop)
4705 return;
4706
edc15caf
SR
4707 bit = trace_test_and_set_recursion(TRACE_LIST_START, TRACE_LIST_MAX);
4708 if (bit < 0)
4709 return;
b1cff0ad 4710
cdbe61bf
SR
4711 /*
4712 * Some of the ops may be dynamically allocated,
4713 * they must be freed after a synchronize_sched().
4714 */
4715 preempt_disable_notrace();
0a016409 4716 do_for_each_ftrace_op(op, ftrace_ops_list) {
4104d326
SRRH
4717 if (ftrace_ops_test(op, ip, regs)) {
4718 if (WARN_ON(!op->func)) {
4719 function_trace_stop = 1;
4720 printk("op=%p %pS\n", op, op);
4721 goto out;
4722 }
a1e2e31d 4723 op->func(ip, parent_ip, op, regs);
4104d326 4724 }
0a016409 4725 } while_for_each_ftrace_op(op);
4104d326 4726out:
cdbe61bf 4727 preempt_enable_notrace();
edc15caf 4728 trace_clear_recursion(bit);
b848914c
SR
4729}
4730
2f5f6ad9
SR
4731/*
4732 * Some archs only support passing ip and parent_ip. Even though
4733 * the list function ignores the op parameter, we do not want any
4734 * C side effects, where a function is called without the caller
4735 * sending a third parameter.
a1e2e31d
SR
4736 * Archs are to support both the regs and ftrace_ops at the same time.
4737 * If they support ftrace_ops, it is assumed they support regs.
4738 * If call backs want to use regs, they must either check for regs
06aeaaea
MH
4739 * being NULL, or CONFIG_DYNAMIC_FTRACE_WITH_REGS.
4740 * Note, CONFIG_DYNAMIC_FTRACE_WITH_REGS expects a full regs to be saved.
a1e2e31d
SR
4741 * An architecture can pass partial regs with ftrace_ops and still
4742 * set the ARCH_SUPPORT_FTARCE_OPS.
2f5f6ad9
SR
4743 */
4744#if ARCH_SUPPORTS_FTRACE_OPS
4745static void ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
a1e2e31d 4746 struct ftrace_ops *op, struct pt_regs *regs)
2f5f6ad9 4747{
a1e2e31d 4748 __ftrace_ops_list_func(ip, parent_ip, NULL, regs);
2f5f6ad9
SR
4749}
4750#else
4751static void ftrace_ops_no_ops(unsigned long ip, unsigned long parent_ip)
4752{
a1e2e31d 4753 __ftrace_ops_list_func(ip, parent_ip, NULL, NULL);
2f5f6ad9
SR
4754}
4755#endif
4756
e32d8956 4757static void clear_ftrace_swapper(void)
978f3a45
SR
4758{
4759 struct task_struct *p;
e32d8956 4760 int cpu;
978f3a45 4761
e32d8956
SR
4762 get_online_cpus();
4763 for_each_online_cpu(cpu) {
4764 p = idle_task(cpu);
978f3a45 4765 clear_tsk_trace_trace(p);
e32d8956
SR
4766 }
4767 put_online_cpus();
4768}
978f3a45 4769
e32d8956
SR
4770static void set_ftrace_swapper(void)
4771{
4772 struct task_struct *p;
4773 int cpu;
4774
4775 get_online_cpus();
4776 for_each_online_cpu(cpu) {
4777 p = idle_task(cpu);
4778 set_tsk_trace_trace(p);
4779 }
4780 put_online_cpus();
978f3a45
SR
4781}
4782
e32d8956
SR
4783static void clear_ftrace_pid(struct pid *pid)
4784{
4785 struct task_struct *p;
4786
229c4ef8 4787 rcu_read_lock();
e32d8956
SR
4788 do_each_pid_task(pid, PIDTYPE_PID, p) {
4789 clear_tsk_trace_trace(p);
4790 } while_each_pid_task(pid, PIDTYPE_PID, p);
229c4ef8
ON
4791 rcu_read_unlock();
4792
e32d8956
SR
4793 put_pid(pid);
4794}
4795
4796static void set_ftrace_pid(struct pid *pid)
978f3a45
SR
4797{
4798 struct task_struct *p;
4799
229c4ef8 4800 rcu_read_lock();
978f3a45
SR
4801 do_each_pid_task(pid, PIDTYPE_PID, p) {
4802 set_tsk_trace_trace(p);
4803 } while_each_pid_task(pid, PIDTYPE_PID, p);
229c4ef8 4804 rcu_read_unlock();
978f3a45
SR
4805}
4806
756d17ee 4807static void clear_ftrace_pid_task(struct pid *pid)
e32d8956 4808{
756d17ee 4809 if (pid == ftrace_swapper_pid)
e32d8956
SR
4810 clear_ftrace_swapper();
4811 else
756d17ee 4812 clear_ftrace_pid(pid);
e32d8956
SR
4813}
4814
4815static void set_ftrace_pid_task(struct pid *pid)
4816{
4817 if (pid == ftrace_swapper_pid)
4818 set_ftrace_swapper();
4819 else
4820 set_ftrace_pid(pid);
4821}
4822
756d17ee 4823static int ftrace_pid_add(int p)
df4fc315 4824{
978f3a45 4825 struct pid *pid;
756d17ee 4826 struct ftrace_pid *fpid;
4827 int ret = -EINVAL;
df4fc315 4828
756d17ee 4829 mutex_lock(&ftrace_lock);
df4fc315 4830
756d17ee 4831 if (!p)
4832 pid = ftrace_swapper_pid;
4833 else
4834 pid = find_get_pid(p);
df4fc315 4835
756d17ee 4836 if (!pid)
4837 goto out;
df4fc315 4838
756d17ee 4839 ret = 0;
df4fc315 4840
756d17ee 4841 list_for_each_entry(fpid, &ftrace_pids, list)
4842 if (fpid->pid == pid)
4843 goto out_put;
978f3a45 4844
756d17ee 4845 ret = -ENOMEM;
df4fc315 4846
756d17ee 4847 fpid = kmalloc(sizeof(*fpid), GFP_KERNEL);
4848 if (!fpid)
4849 goto out_put;
df4fc315 4850
756d17ee 4851 list_add(&fpid->list, &ftrace_pids);
4852 fpid->pid = pid;
0ef8cde5 4853
756d17ee 4854 set_ftrace_pid_task(pid);
978f3a45 4855
756d17ee 4856 ftrace_update_pid_func();
4857 ftrace_startup_enable(0);
4858
4859 mutex_unlock(&ftrace_lock);
4860 return 0;
4861
4862out_put:
4863 if (pid != ftrace_swapper_pid)
4864 put_pid(pid);
978f3a45 4865
756d17ee 4866out:
4867 mutex_unlock(&ftrace_lock);
4868 return ret;
4869}
4870
4871static void ftrace_pid_reset(void)
4872{
4873 struct ftrace_pid *fpid, *safe;
978f3a45 4874
756d17ee 4875 mutex_lock(&ftrace_lock);
4876 list_for_each_entry_safe(fpid, safe, &ftrace_pids, list) {
4877 struct pid *pid = fpid->pid;
4878
4879 clear_ftrace_pid_task(pid);
4880
4881 list_del(&fpid->list);
4882 kfree(fpid);
df4fc315
SR
4883 }
4884
df4fc315
SR
4885 ftrace_update_pid_func();
4886 ftrace_startup_enable(0);
4887
e6ea44e9 4888 mutex_unlock(&ftrace_lock);
756d17ee 4889}
df4fc315 4890
756d17ee 4891static void *fpid_start(struct seq_file *m, loff_t *pos)
4892{
4893 mutex_lock(&ftrace_lock);
4894
4895 if (list_empty(&ftrace_pids) && (!*pos))
4896 return (void *) 1;
4897
4898 return seq_list_start(&ftrace_pids, *pos);
4899}
4900
4901static void *fpid_next(struct seq_file *m, void *v, loff_t *pos)
4902{
4903 if (v == (void *)1)
4904 return NULL;
4905
4906 return seq_list_next(v, &ftrace_pids, pos);
4907}
4908
4909static void fpid_stop(struct seq_file *m, void *p)
4910{
4911 mutex_unlock(&ftrace_lock);
4912}
4913
4914static int fpid_show(struct seq_file *m, void *v)
4915{
4916 const struct ftrace_pid *fpid = list_entry(v, struct ftrace_pid, list);
4917
4918 if (v == (void *)1) {
4919 seq_printf(m, "no pid\n");
4920 return 0;
4921 }
4922
4923 if (fpid->pid == ftrace_swapper_pid)
4924 seq_printf(m, "swapper tasks\n");
4925 else
4926 seq_printf(m, "%u\n", pid_vnr(fpid->pid));
4927
4928 return 0;
4929}
4930
4931static const struct seq_operations ftrace_pid_sops = {
4932 .start = fpid_start,
4933 .next = fpid_next,
4934 .stop = fpid_stop,
4935 .show = fpid_show,
4936};
4937
4938static int
4939ftrace_pid_open(struct inode *inode, struct file *file)
4940{
4941 int ret = 0;
4942
4943 if ((file->f_mode & FMODE_WRITE) &&
4944 (file->f_flags & O_TRUNC))
4945 ftrace_pid_reset();
4946
4947 if (file->f_mode & FMODE_READ)
4948 ret = seq_open(file, &ftrace_pid_sops);
4949
4950 return ret;
4951}
4952
df4fc315
SR
4953static ssize_t
4954ftrace_pid_write(struct file *filp, const char __user *ubuf,
4955 size_t cnt, loff_t *ppos)
4956{
457dc928 4957 char buf[64], *tmp;
df4fc315
SR
4958 long val;
4959 int ret;
4960
4961 if (cnt >= sizeof(buf))
4962 return -EINVAL;
4963
4964 if (copy_from_user(&buf, ubuf, cnt))
4965 return -EFAULT;
4966
4967 buf[cnt] = 0;
4968
756d17ee 4969 /*
4970 * Allow "echo > set_ftrace_pid" or "echo -n '' > set_ftrace_pid"
4971 * to clean the filter quietly.
4972 */
457dc928
IM
4973 tmp = strstrip(buf);
4974 if (strlen(tmp) == 0)
756d17ee 4975 return 1;
4976
bcd83ea6 4977 ret = kstrtol(tmp, 10, &val);
df4fc315
SR
4978 if (ret < 0)
4979 return ret;
4980
756d17ee 4981 ret = ftrace_pid_add(val);
df4fc315 4982
756d17ee 4983 return ret ? ret : cnt;
4984}
df4fc315 4985
756d17ee 4986static int
4987ftrace_pid_release(struct inode *inode, struct file *file)
4988{
4989 if (file->f_mode & FMODE_READ)
4990 seq_release(inode, file);
df4fc315 4991
756d17ee 4992 return 0;
df4fc315
SR
4993}
4994
5e2336a0 4995static const struct file_operations ftrace_pid_fops = {
756d17ee 4996 .open = ftrace_pid_open,
4997 .write = ftrace_pid_write,
4998 .read = seq_read,
098c879e 4999 .llseek = tracing_lseek,
756d17ee 5000 .release = ftrace_pid_release,
df4fc315
SR
5001};
5002
5003static __init int ftrace_init_debugfs(void)
5004{
5005 struct dentry *d_tracer;
df4fc315
SR
5006
5007 d_tracer = tracing_init_dentry();
5008 if (!d_tracer)
5009 return 0;
5010
5011 ftrace_init_dyn_debugfs(d_tracer);
5012
5452af66
FW
5013 trace_create_file("set_ftrace_pid", 0644, d_tracer,
5014 NULL, &ftrace_pid_fops);
493762fc
SR
5015
5016 ftrace_profile_debugfs(d_tracer);
5017
df4fc315
SR
5018 return 0;
5019}
df4fc315
SR
5020fs_initcall(ftrace_init_debugfs);
5021
a2bb6a3d 5022/**
81adbdc0 5023 * ftrace_kill - kill ftrace
a2bb6a3d
SR
5024 *
5025 * This function should be used by panic code. It stops ftrace
5026 * but in a not so nice way. If you need to simply kill ftrace
5027 * from a non-atomic section, use ftrace_kill.
5028 */
81adbdc0 5029void ftrace_kill(void)
a2bb6a3d
SR
5030{
5031 ftrace_disabled = 1;
5032 ftrace_enabled = 0;
a2bb6a3d
SR
5033 clear_ftrace_function();
5034}
5035
e0a413f6
SR
5036/**
5037 * Test if ftrace is dead or not.
5038 */
5039int ftrace_is_dead(void)
5040{
5041 return ftrace_disabled;
5042}
5043
16444a8a 5044/**
3d083395
SR
5045 * register_ftrace_function - register a function for profiling
5046 * @ops - ops structure that holds the function for profiling.
16444a8a 5047 *
3d083395
SR
5048 * Register a function to be called by all functions in the
5049 * kernel.
5050 *
5051 * Note: @ops->func and all the functions it calls must be labeled
5052 * with "notrace", otherwise it will go into a
5053 * recursive loop.
16444a8a 5054 */
3d083395 5055int register_ftrace_function(struct ftrace_ops *ops)
16444a8a 5056{
45a4a237 5057 int ret = -1;
4eebcc81 5058
f04f24fb
MH
5059 ftrace_ops_init(ops);
5060
e6ea44e9 5061 mutex_lock(&ftrace_lock);
e7d3737e 5062
8a56d776 5063 ret = ftrace_startup(ops, 0);
b848914c 5064
e6ea44e9 5065 mutex_unlock(&ftrace_lock);
8d240dd8 5066
b0fc494f 5067 return ret;
3d083395 5068}
cdbe61bf 5069EXPORT_SYMBOL_GPL(register_ftrace_function);
3d083395
SR
5070
5071/**
32632920 5072 * unregister_ftrace_function - unregister a function for profiling.
3d083395
SR
5073 * @ops - ops structure that holds the function to unregister
5074 *
5075 * Unregister a function that was added to be called by ftrace profiling.
5076 */
5077int unregister_ftrace_function(struct ftrace_ops *ops)
5078{
5079 int ret;
5080
e6ea44e9 5081 mutex_lock(&ftrace_lock);
8a56d776 5082 ret = ftrace_shutdown(ops, 0);
e6ea44e9 5083 mutex_unlock(&ftrace_lock);
b0fc494f
SR
5084
5085 return ret;
5086}
cdbe61bf 5087EXPORT_SYMBOL_GPL(unregister_ftrace_function);
b0fc494f 5088
e309b41d 5089int
b0fc494f 5090ftrace_enable_sysctl(struct ctl_table *table, int write,
8d65af78 5091 void __user *buffer, size_t *lenp,
b0fc494f
SR
5092 loff_t *ppos)
5093{
45a4a237 5094 int ret = -ENODEV;
4eebcc81 5095
e6ea44e9 5096 mutex_lock(&ftrace_lock);
b0fc494f 5097
45a4a237
SR
5098 if (unlikely(ftrace_disabled))
5099 goto out;
5100
5101 ret = proc_dointvec(table, write, buffer, lenp, ppos);
b0fc494f 5102
a32c7765 5103 if (ret || !write || (last_ftrace_enabled == !!ftrace_enabled))
b0fc494f
SR
5104 goto out;
5105
a32c7765 5106 last_ftrace_enabled = !!ftrace_enabled;
b0fc494f
SR
5107
5108 if (ftrace_enabled) {
5109
5110 ftrace_startup_sysctl();
5111
5112 /* we are starting ftrace again */
5000c418
JK
5113 if (ftrace_ops_list != &ftrace_list_end)
5114 update_ftrace_function();
b0fc494f
SR
5115
5116 } else {
5117 /* stopping ftrace calls (just send to ftrace_stub) */
5118 ftrace_trace_function = ftrace_stub;
5119
5120 ftrace_shutdown_sysctl();
5121 }
5122
5123 out:
e6ea44e9 5124 mutex_unlock(&ftrace_lock);
3d083395 5125 return ret;
16444a8a 5126}
f17845e5 5127
fb52607a 5128#ifdef CONFIG_FUNCTION_GRAPH_TRACER
e7d3737e 5129
597af815 5130static int ftrace_graph_active;
e7d3737e 5131
e49dc19c
SR
5132int ftrace_graph_entry_stub(struct ftrace_graph_ent *trace)
5133{
5134 return 0;
5135}
5136
287b6e68
FW
5137/* The callbacks that hook a function */
5138trace_func_graph_ret_t ftrace_graph_return =
5139 (trace_func_graph_ret_t)ftrace_stub;
e49dc19c 5140trace_func_graph_ent_t ftrace_graph_entry = ftrace_graph_entry_stub;
23a8e844 5141static trace_func_graph_ent_t __ftrace_graph_entry = ftrace_graph_entry_stub;
f201ae23
FW
5142
5143/* Try to assign a return stack array on FTRACE_RETSTACK_ALLOC_SIZE tasks. */
5144static int alloc_retstack_tasklist(struct ftrace_ret_stack **ret_stack_list)
5145{
5146 int i;
5147 int ret = 0;
5148 unsigned long flags;
5149 int start = 0, end = FTRACE_RETSTACK_ALLOC_SIZE;
5150 struct task_struct *g, *t;
5151
5152 for (i = 0; i < FTRACE_RETSTACK_ALLOC_SIZE; i++) {
5153 ret_stack_list[i] = kmalloc(FTRACE_RETFUNC_DEPTH
5154 * sizeof(struct ftrace_ret_stack),
5155 GFP_KERNEL);
5156 if (!ret_stack_list[i]) {
5157 start = 0;
5158 end = i;
5159 ret = -ENOMEM;
5160 goto free;
5161 }
5162 }
5163
5164 read_lock_irqsave(&tasklist_lock, flags);
5165 do_each_thread(g, t) {
5166 if (start == end) {
5167 ret = -EAGAIN;
5168 goto unlock;
5169 }
5170
5171 if (t->ret_stack == NULL) {
380c4b14 5172 atomic_set(&t->tracing_graph_pause, 0);
f201ae23 5173 atomic_set(&t->trace_overrun, 0);
26c01624
SR
5174 t->curr_ret_stack = -1;
5175 /* Make sure the tasks see the -1 first: */
5176 smp_wmb();
5177 t->ret_stack = ret_stack_list[start++];
f201ae23
FW
5178 }
5179 } while_each_thread(g, t);
5180
5181unlock:
5182 read_unlock_irqrestore(&tasklist_lock, flags);
5183free:
5184 for (i = start; i < end; i++)
5185 kfree(ret_stack_list[i]);
5186 return ret;
5187}
5188
8aef2d28 5189static void
38516ab5
SR
5190ftrace_graph_probe_sched_switch(void *ignore,
5191 struct task_struct *prev, struct task_struct *next)
8aef2d28
SR
5192{
5193 unsigned long long timestamp;
5194 int index;
5195
be6f164a
SR
5196 /*
5197 * Does the user want to count the time a function was asleep.
5198 * If so, do not update the time stamps.
5199 */
5200 if (trace_flags & TRACE_ITER_SLEEP_TIME)
5201 return;
5202
8aef2d28
SR
5203 timestamp = trace_clock_local();
5204
5205 prev->ftrace_timestamp = timestamp;
5206
5207 /* only process tasks that we timestamped */
5208 if (!next->ftrace_timestamp)
5209 return;
5210
5211 /*
5212 * Update all the counters in next to make up for the
5213 * time next was sleeping.
5214 */
5215 timestamp -= next->ftrace_timestamp;
5216
5217 for (index = next->curr_ret_stack; index >= 0; index--)
5218 next->ret_stack[index].calltime += timestamp;
5219}
5220
f201ae23 5221/* Allocate a return stack for each task */
fb52607a 5222static int start_graph_tracing(void)
f201ae23
FW
5223{
5224 struct ftrace_ret_stack **ret_stack_list;
5b058bcd 5225 int ret, cpu;
f201ae23
FW
5226
5227 ret_stack_list = kmalloc(FTRACE_RETSTACK_ALLOC_SIZE *
5228 sizeof(struct ftrace_ret_stack *),
5229 GFP_KERNEL);
5230
5231 if (!ret_stack_list)
5232 return -ENOMEM;
5233
5b058bcd 5234 /* The cpu_boot init_task->ret_stack will never be freed */
179c498a
SR
5235 for_each_online_cpu(cpu) {
5236 if (!idle_task(cpu)->ret_stack)
868baf07 5237 ftrace_graph_init_idle_task(idle_task(cpu), cpu);
179c498a 5238 }
5b058bcd 5239
f201ae23
FW
5240 do {
5241 ret = alloc_retstack_tasklist(ret_stack_list);
5242 } while (ret == -EAGAIN);
5243
8aef2d28 5244 if (!ret) {
38516ab5 5245 ret = register_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
8aef2d28
SR
5246 if (ret)
5247 pr_info("ftrace_graph: Couldn't activate tracepoint"
5248 " probe to kernel_sched_switch\n");
5249 }
5250
f201ae23
FW
5251 kfree(ret_stack_list);
5252 return ret;
5253}
5254
4a2b8dda
FW
5255/*
5256 * Hibernation protection.
5257 * The state of the current task is too much unstable during
5258 * suspend/restore to disk. We want to protect against that.
5259 */
5260static int
5261ftrace_suspend_notifier_call(struct notifier_block *bl, unsigned long state,
5262 void *unused)
5263{
5264 switch (state) {
5265 case PM_HIBERNATION_PREPARE:
5266 pause_graph_tracing();
5267 break;
5268
5269 case PM_POST_HIBERNATION:
5270 unpause_graph_tracing();
5271 break;
5272 }
5273 return NOTIFY_DONE;
5274}
5275
23a8e844
SRRH
5276static int ftrace_graph_entry_test(struct ftrace_graph_ent *trace)
5277{
5278 if (!ftrace_ops_test(&global_ops, trace->func, NULL))
5279 return 0;
5280 return __ftrace_graph_entry(trace);
5281}
5282
5283/*
5284 * The function graph tracer should only trace the functions defined
5285 * by set_ftrace_filter and set_ftrace_notrace. If another function
5286 * tracer ops is registered, the graph tracer requires testing the
5287 * function against the global ops, and not just trace any function
5288 * that any ftrace_ops registered.
5289 */
5290static void update_function_graph_func(void)
5291{
5292 if (ftrace_ops_list == &ftrace_list_end ||
5293 (ftrace_ops_list == &global_ops &&
5294 global_ops.next == &ftrace_list_end))
5295 ftrace_graph_entry = __ftrace_graph_entry;
5296 else
5297 ftrace_graph_entry = ftrace_graph_entry_test;
5298}
5299
8275f69f
MK
5300static struct notifier_block ftrace_suspend_notifier = {
5301 .notifier_call = ftrace_suspend_notifier_call,
5302};
5303
287b6e68
FW
5304int register_ftrace_graph(trace_func_graph_ret_t retfunc,
5305 trace_func_graph_ent_t entryfunc)
15e6cb36 5306{
e7d3737e
FW
5307 int ret = 0;
5308
e6ea44e9 5309 mutex_lock(&ftrace_lock);
e7d3737e 5310
05ce5818 5311 /* we currently allow only one tracer registered at a time */
597af815 5312 if (ftrace_graph_active) {
05ce5818
SR
5313 ret = -EBUSY;
5314 goto out;
5315 }
5316
4a2b8dda
FW
5317 register_pm_notifier(&ftrace_suspend_notifier);
5318
597af815 5319 ftrace_graph_active++;
fb52607a 5320 ret = start_graph_tracing();
f201ae23 5321 if (ret) {
597af815 5322 ftrace_graph_active--;
f201ae23
FW
5323 goto out;
5324 }
e53a6319 5325
287b6e68 5326 ftrace_graph_return = retfunc;
23a8e844
SRRH
5327
5328 /*
5329 * Update the indirect function to the entryfunc, and the
5330 * function that gets called to the entry_test first. Then
5331 * call the update fgraph entry function to determine if
5332 * the entryfunc should be called directly or not.
5333 */
5334 __ftrace_graph_entry = entryfunc;
5335 ftrace_graph_entry = ftrace_graph_entry_test;
5336 update_function_graph_func();
e53a6319 5337
fd06a549
SRRH
5338 /* Function graph doesn't use the .func field of global_ops */
5339 global_ops.flags |= FTRACE_OPS_FL_STUB;
5340
79922b80
SRRH
5341#ifdef CONFIG_DYNAMIC_FTRACE
5342 /* Optimize function graph calling (if implemented by arch) */
5343 global_ops.trampoline = FTRACE_GRAPH_ADDR;
5344#endif
5345
fd06a549 5346 ret = ftrace_startup(&global_ops, FTRACE_START_FUNC_RET);
e7d3737e
FW
5347
5348out:
e6ea44e9 5349 mutex_unlock(&ftrace_lock);
e7d3737e 5350 return ret;
15e6cb36
FW
5351}
5352
fb52607a 5353void unregister_ftrace_graph(void)
15e6cb36 5354{
e6ea44e9 5355 mutex_lock(&ftrace_lock);
e7d3737e 5356
597af815 5357 if (unlikely(!ftrace_graph_active))
2aad1b76
SR
5358 goto out;
5359
597af815 5360 ftrace_graph_active--;
287b6e68 5361 ftrace_graph_return = (trace_func_graph_ret_t)ftrace_stub;
e49dc19c 5362 ftrace_graph_entry = ftrace_graph_entry_stub;
23a8e844 5363 __ftrace_graph_entry = ftrace_graph_entry_stub;
fd06a549
SRRH
5364 ftrace_shutdown(&global_ops, FTRACE_STOP_FUNC_RET);
5365 global_ops.flags &= ~FTRACE_OPS_FL_STUB;
79922b80
SRRH
5366#ifdef CONFIG_DYNAMIC_FTRACE
5367 global_ops.trampoline = 0;
5368#endif
4a2b8dda 5369 unregister_pm_notifier(&ftrace_suspend_notifier);
38516ab5 5370 unregister_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
e7d3737e 5371
2aad1b76 5372 out:
e6ea44e9 5373 mutex_unlock(&ftrace_lock);
15e6cb36 5374}
f201ae23 5375
868baf07
SR
5376static DEFINE_PER_CPU(struct ftrace_ret_stack *, idle_ret_stack);
5377
5378static void
5379graph_init_task(struct task_struct *t, struct ftrace_ret_stack *ret_stack)
5380{
5381 atomic_set(&t->tracing_graph_pause, 0);
5382 atomic_set(&t->trace_overrun, 0);
5383 t->ftrace_timestamp = 0;
25985edc 5384 /* make curr_ret_stack visible before we add the ret_stack */
868baf07
SR
5385 smp_wmb();
5386 t->ret_stack = ret_stack;
5387}
5388
5389/*
5390 * Allocate a return stack for the idle task. May be the first
5391 * time through, or it may be done by CPU hotplug online.
5392 */
5393void ftrace_graph_init_idle_task(struct task_struct *t, int cpu)
5394{
5395 t->curr_ret_stack = -1;
5396 /*
5397 * The idle task has no parent, it either has its own
5398 * stack or no stack at all.
5399 */
5400 if (t->ret_stack)
5401 WARN_ON(t->ret_stack != per_cpu(idle_ret_stack, cpu));
5402
5403 if (ftrace_graph_active) {
5404 struct ftrace_ret_stack *ret_stack;
5405
5406 ret_stack = per_cpu(idle_ret_stack, cpu);
5407 if (!ret_stack) {
5408 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
5409 * sizeof(struct ftrace_ret_stack),
5410 GFP_KERNEL);
5411 if (!ret_stack)
5412 return;
5413 per_cpu(idle_ret_stack, cpu) = ret_stack;
5414 }
5415 graph_init_task(t, ret_stack);
5416 }
5417}
5418
f201ae23 5419/* Allocate a return stack for newly created task */
fb52607a 5420void ftrace_graph_init_task(struct task_struct *t)
f201ae23 5421{
84047e36
SR
5422 /* Make sure we do not use the parent ret_stack */
5423 t->ret_stack = NULL;
ea14eb71 5424 t->curr_ret_stack = -1;
84047e36 5425
597af815 5426 if (ftrace_graph_active) {
82310a32
SR
5427 struct ftrace_ret_stack *ret_stack;
5428
5429 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
f201ae23
FW
5430 * sizeof(struct ftrace_ret_stack),
5431 GFP_KERNEL);
82310a32 5432 if (!ret_stack)
f201ae23 5433 return;
868baf07 5434 graph_init_task(t, ret_stack);
84047e36 5435 }
f201ae23
FW
5436}
5437
fb52607a 5438void ftrace_graph_exit_task(struct task_struct *t)
f201ae23 5439{
eae849ca
FW
5440 struct ftrace_ret_stack *ret_stack = t->ret_stack;
5441
f201ae23 5442 t->ret_stack = NULL;
eae849ca
FW
5443 /* NULL must become visible to IRQs before we free it: */
5444 barrier();
5445
5446 kfree(ret_stack);
f201ae23 5447}
14a866c5
SR
5448
5449void ftrace_graph_stop(void)
5450{
5451 ftrace_stop();
5452}
15e6cb36 5453#endif