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