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