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