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