]> git.proxmox.com Git - mirror_ubuntu-focal-kernel.git/blame - kernel/trace/ftrace.c
ftrace: Add selftest to test function save-regs support
[mirror_ubuntu-focal-kernel.git] / kernel / trace / ftrace.c
CommitLineData
16444a8a
ACM
1/*
2 * Infrastructure for profiling code inserted by 'gcc -pg'.
3 *
4 * Copyright (C) 2007-2008 Steven Rostedt <srostedt@redhat.com>
5 * Copyright (C) 2004-2008 Ingo Molnar <mingo@redhat.com>
6 *
7 * Originally ported from the -rt patch by:
8 * Copyright (C) 2007 Arnaldo Carvalho de Melo <acme@redhat.com>
9 *
10 * Based on code in the latency_tracer, that is:
11 *
12 * Copyright (C) 2004-2006 Ingo Molnar
13 * Copyright (C) 2004 William Lee Irwin III
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
e248491a
JO
65#define FL_GLOBAL_CONTROL_MASK (FTRACE_OPS_FL_GLOBAL | FTRACE_OPS_FL_CONTROL)
66
2f5f6ad9
SR
67static struct ftrace_ops ftrace_list_end __read_mostly = {
68 .func = ftrace_stub,
4740974a 69 .flags = FTRACE_OPS_FL_RECURSION_SAFE,
2f5f6ad9
SR
70};
71
4eebcc81
SR
72/* ftrace_enabled is a method to turn ftrace on or off */
73int ftrace_enabled __read_mostly;
d61f82d0 74static int last_ftrace_enabled;
b0fc494f 75
60a7ecf4 76/* Quick disabling of function tracer. */
2f5f6ad9
SR
77int function_trace_stop __read_mostly;
78
79/* Current function tracing op */
80struct ftrace_ops *function_trace_op __read_mostly = &ftrace_list_end;
60a7ecf4 81
756d17ee 82/* List for set_ftrace_pid's pids. */
83LIST_HEAD(ftrace_pids);
84struct ftrace_pid {
85 struct list_head list;
86 struct pid *pid;
87};
88
4eebcc81
SR
89/*
90 * ftrace_disabled is set when an anomaly is discovered.
91 * ftrace_disabled is much stronger than ftrace_enabled.
92 */
93static int ftrace_disabled __read_mostly;
94
52baf119 95static DEFINE_MUTEX(ftrace_lock);
b0fc494f 96
b848914c 97static struct ftrace_ops *ftrace_global_list __read_mostly = &ftrace_list_end;
e248491a 98static struct ftrace_ops *ftrace_control_list __read_mostly = &ftrace_list_end;
b848914c 99static struct ftrace_ops *ftrace_ops_list __read_mostly = &ftrace_list_end;
16444a8a 100ftrace_func_t ftrace_trace_function __read_mostly = ftrace_stub;
df4fc315 101ftrace_func_t ftrace_pid_function __read_mostly = ftrace_stub;
2b499381 102static struct ftrace_ops global_ops;
e248491a 103static struct ftrace_ops control_ops;
16444a8a 104
2f5f6ad9
SR
105#if ARCH_SUPPORTS_FTRACE_OPS
106static void ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
a1e2e31d 107 struct ftrace_ops *op, struct pt_regs *regs);
2f5f6ad9
SR
108#else
109/* See comment below, where ftrace_ops_list_func is defined */
110static void ftrace_ops_no_ops(unsigned long ip, unsigned long parent_ip);
111#define ftrace_ops_list_func ((ftrace_func_t)ftrace_ops_no_ops)
112#endif
b848914c 113
ea701f11
SR
114/**
115 * ftrace_nr_registered_ops - return number of ops registered
116 *
117 * Returns the number of ftrace_ops registered and tracing functions
118 */
119int ftrace_nr_registered_ops(void)
120{
121 struct ftrace_ops *ops;
122 int cnt = 0;
123
124 mutex_lock(&ftrace_lock);
125
126 for (ops = ftrace_ops_list;
127 ops != &ftrace_list_end; ops = ops->next)
128 cnt++;
129
130 mutex_unlock(&ftrace_lock);
131
132 return cnt;
133}
134
3f379b03 135/*
b848914c 136 * Traverse the ftrace_global_list, invoking all entries. The reason that we
3f379b03
PM
137 * can use rcu_dereference_raw() is that elements removed from this list
138 * are simply leaked, so there is no need to interact with a grace-period
139 * mechanism. The rcu_dereference_raw() calls are needed to handle
b848914c 140 * concurrent insertions into the ftrace_global_list.
3f379b03
PM
141 *
142 * Silly Alpha and silly pointer-speculation compiler optimizations!
143 */
2f5f6ad9
SR
144static void
145ftrace_global_list_func(unsigned long ip, unsigned long parent_ip,
a1e2e31d 146 struct ftrace_ops *op, struct pt_regs *regs)
16444a8a 147{
b1cff0ad
SR
148 if (unlikely(trace_recursion_test(TRACE_GLOBAL_BIT)))
149 return;
16444a8a 150
b1cff0ad
SR
151 trace_recursion_set(TRACE_GLOBAL_BIT);
152 op = rcu_dereference_raw(ftrace_global_list); /*see above*/
16444a8a 153 while (op != &ftrace_list_end) {
a1e2e31d 154 op->func(ip, parent_ip, op, regs);
3f379b03 155 op = rcu_dereference_raw(op->next); /*see above*/
16444a8a 156 };
b1cff0ad 157 trace_recursion_clear(TRACE_GLOBAL_BIT);
16444a8a
ACM
158}
159
2f5f6ad9 160static void ftrace_pid_func(unsigned long ip, unsigned long parent_ip,
a1e2e31d 161 struct ftrace_ops *op, struct pt_regs *regs)
df4fc315 162{
0ef8cde5 163 if (!test_tsk_trace_trace(current))
df4fc315
SR
164 return;
165
a1e2e31d 166 ftrace_pid_function(ip, parent_ip, op, regs);
df4fc315
SR
167}
168
169static void set_ftrace_pid_function(ftrace_func_t func)
170{
171 /* do not set ftrace_pid_function to itself! */
172 if (func != ftrace_pid_func)
173 ftrace_pid_function = func;
174}
175
16444a8a 176/**
3d083395 177 * clear_ftrace_function - reset the ftrace function
16444a8a 178 *
3d083395
SR
179 * This NULLs the ftrace function and in essence stops
180 * tracing. There may be lag
16444a8a 181 */
3d083395 182void clear_ftrace_function(void)
16444a8a 183{
3d083395 184 ftrace_trace_function = ftrace_stub;
df4fc315 185 ftrace_pid_function = ftrace_stub;
3d083395
SR
186}
187
e248491a
JO
188static void control_ops_disable_all(struct ftrace_ops *ops)
189{
190 int cpu;
191
192 for_each_possible_cpu(cpu)
193 *per_cpu_ptr(ops->disabled, cpu) = 1;
194}
195
196static int control_ops_alloc(struct ftrace_ops *ops)
197{
198 int __percpu *disabled;
199
200 disabled = alloc_percpu(int);
201 if (!disabled)
202 return -ENOMEM;
203
204 ops->disabled = disabled;
205 control_ops_disable_all(ops);
206 return 0;
207}
208
209static void control_ops_free(struct ftrace_ops *ops)
210{
211 free_percpu(ops->disabled);
212}
213
2b499381 214static void update_global_ops(void)
491d0dcf
SR
215{
216 ftrace_func_t func;
217
218 /*
219 * If there's only one function registered, then call that
220 * function directly. Otherwise, we need to iterate over the
221 * registered callers.
222 */
b848914c
SR
223 if (ftrace_global_list == &ftrace_list_end ||
224 ftrace_global_list->next == &ftrace_list_end)
225 func = ftrace_global_list->func;
491d0dcf 226 else
b848914c 227 func = ftrace_global_list_func;
491d0dcf
SR
228
229 /* If we filter on pids, update to use the pid function */
230 if (!list_empty(&ftrace_pids)) {
231 set_ftrace_pid_function(func);
232 func = ftrace_pid_func;
233 }
2b499381
SR
234
235 global_ops.func = func;
236}
237
238static void update_ftrace_function(void)
239{
240 ftrace_func_t func;
241
242 update_global_ops();
243
cdbe61bf
SR
244 /*
245 * If we are at the end of the list and this ops is
4740974a
SR
246 * recursion safe and not dynamic and the arch supports passing ops,
247 * then have the mcount trampoline call the function directly.
cdbe61bf 248 */
b848914c 249 if (ftrace_ops_list == &ftrace_list_end ||
cdbe61bf 250 (ftrace_ops_list->next == &ftrace_list_end &&
2f5f6ad9 251 !(ftrace_ops_list->flags & FTRACE_OPS_FL_DYNAMIC) &&
4740974a 252 (ftrace_ops_list->flags & FTRACE_OPS_FL_RECURSION_SAFE) &&
ccf3672d 253 !FTRACE_FORCE_LIST_FUNC)) {
2f5f6ad9
SR
254 /* Set the ftrace_ops that the arch callback uses */
255 if (ftrace_ops_list == &global_ops)
256 function_trace_op = ftrace_global_list;
257 else
258 function_trace_op = ftrace_ops_list;
b848914c 259 func = ftrace_ops_list->func;
2f5f6ad9
SR
260 } else {
261 /* Just use the default ftrace_ops */
262 function_trace_op = &ftrace_list_end;
b848914c 263 func = ftrace_ops_list_func;
2f5f6ad9 264 }
2b499381 265
491d0dcf 266 ftrace_trace_function = func;
491d0dcf
SR
267}
268
2b499381 269static void add_ftrace_ops(struct ftrace_ops **list, struct ftrace_ops *ops)
3d083395 270{
2b499381 271 ops->next = *list;
16444a8a 272 /*
b848914c 273 * We are entering ops into the list but another
16444a8a
ACM
274 * CPU might be walking that list. We need to make sure
275 * the ops->next pointer is valid before another CPU sees
b848914c 276 * the ops pointer included into the list.
16444a8a 277 */
2b499381 278 rcu_assign_pointer(*list, ops);
16444a8a
ACM
279}
280
2b499381 281static int remove_ftrace_ops(struct ftrace_ops **list, struct ftrace_ops *ops)
16444a8a 282{
16444a8a 283 struct ftrace_ops **p;
16444a8a
ACM
284
285 /*
3d083395
SR
286 * If we are removing the last function, then simply point
287 * to the ftrace_stub.
16444a8a 288 */
2b499381
SR
289 if (*list == ops && ops->next == &ftrace_list_end) {
290 *list = &ftrace_list_end;
e6ea44e9 291 return 0;
16444a8a
ACM
292 }
293
2b499381 294 for (p = list; *p != &ftrace_list_end; p = &(*p)->next)
16444a8a
ACM
295 if (*p == ops)
296 break;
297
e6ea44e9
SR
298 if (*p != ops)
299 return -1;
16444a8a
ACM
300
301 *p = (*p)->next;
2b499381
SR
302 return 0;
303}
16444a8a 304
e248491a
JO
305static void add_ftrace_list_ops(struct ftrace_ops **list,
306 struct ftrace_ops *main_ops,
307 struct ftrace_ops *ops)
308{
309 int first = *list == &ftrace_list_end;
310 add_ftrace_ops(list, ops);
311 if (first)
312 add_ftrace_ops(&ftrace_ops_list, main_ops);
313}
314
315static int remove_ftrace_list_ops(struct ftrace_ops **list,
316 struct ftrace_ops *main_ops,
317 struct ftrace_ops *ops)
318{
319 int ret = remove_ftrace_ops(list, ops);
320 if (!ret && *list == &ftrace_list_end)
321 ret = remove_ftrace_ops(&ftrace_ops_list, main_ops);
322 return ret;
323}
324
2b499381
SR
325static int __register_ftrace_function(struct ftrace_ops *ops)
326{
8d240dd8 327 if (unlikely(ftrace_disabled))
2b499381
SR
328 return -ENODEV;
329
330 if (FTRACE_WARN_ON(ops == &global_ops))
331 return -EINVAL;
332
b848914c
SR
333 if (WARN_ON(ops->flags & FTRACE_OPS_FL_ENABLED))
334 return -EBUSY;
335
e248491a
JO
336 /* We don't support both control and global flags set. */
337 if ((ops->flags & FL_GLOBAL_CONTROL_MASK) == FL_GLOBAL_CONTROL_MASK)
338 return -EINVAL;
339
08f6fba5
SR
340#ifndef ARCH_SUPPORTS_FTRACE_SAVE_REGS
341 /*
342 * If the ftrace_ops specifies SAVE_REGS, then it only can be used
343 * if the arch supports it, or SAVE_REGS_IF_SUPPORTED is also set.
344 * Setting SAVE_REGS_IF_SUPPORTED makes SAVE_REGS irrelevant.
345 */
346 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS &&
347 !(ops->flags & FTRACE_OPS_FL_SAVE_REGS_IF_SUPPORTED))
348 return -EINVAL;
349
350 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS_IF_SUPPORTED)
351 ops->flags |= FTRACE_OPS_FL_SAVE_REGS;
352#endif
353
cdbe61bf
SR
354 if (!core_kernel_data((unsigned long)ops))
355 ops->flags |= FTRACE_OPS_FL_DYNAMIC;
356
b848914c 357 if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
e248491a 358 add_ftrace_list_ops(&ftrace_global_list, &global_ops, ops);
b848914c 359 ops->flags |= FTRACE_OPS_FL_ENABLED;
e248491a
JO
360 } else if (ops->flags & FTRACE_OPS_FL_CONTROL) {
361 if (control_ops_alloc(ops))
362 return -ENOMEM;
363 add_ftrace_list_ops(&ftrace_control_list, &control_ops, ops);
b848914c
SR
364 } else
365 add_ftrace_ops(&ftrace_ops_list, ops);
366
2b499381
SR
367 if (ftrace_enabled)
368 update_ftrace_function();
369
370 return 0;
371}
372
373static int __unregister_ftrace_function(struct ftrace_ops *ops)
374{
375 int ret;
376
377 if (ftrace_disabled)
378 return -ENODEV;
379
b848914c
SR
380 if (WARN_ON(!(ops->flags & FTRACE_OPS_FL_ENABLED)))
381 return -EBUSY;
382
2b499381
SR
383 if (FTRACE_WARN_ON(ops == &global_ops))
384 return -EINVAL;
385
b848914c 386 if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
e248491a
JO
387 ret = remove_ftrace_list_ops(&ftrace_global_list,
388 &global_ops, ops);
b848914c
SR
389 if (!ret)
390 ops->flags &= ~FTRACE_OPS_FL_ENABLED;
e248491a
JO
391 } else if (ops->flags & FTRACE_OPS_FL_CONTROL) {
392 ret = remove_ftrace_list_ops(&ftrace_control_list,
393 &control_ops, ops);
394 if (!ret) {
395 /*
396 * The ftrace_ops is now removed from the list,
397 * so there'll be no new users. We must ensure
398 * all current users are done before we free
399 * the control data.
400 */
401 synchronize_sched();
402 control_ops_free(ops);
403 }
b848914c
SR
404 } else
405 ret = remove_ftrace_ops(&ftrace_ops_list, ops);
406
2b499381
SR
407 if (ret < 0)
408 return ret;
b848914c 409
491d0dcf
SR
410 if (ftrace_enabled)
411 update_ftrace_function();
16444a8a 412
cdbe61bf
SR
413 /*
414 * Dynamic ops may be freed, we must make sure that all
415 * callers are done before leaving this function.
416 */
417 if (ops->flags & FTRACE_OPS_FL_DYNAMIC)
418 synchronize_sched();
419
e6ea44e9 420 return 0;
3d083395
SR
421}
422
df4fc315
SR
423static void ftrace_update_pid_func(void)
424{
491d0dcf 425 /* Only do something if we are tracing something */
df4fc315 426 if (ftrace_trace_function == ftrace_stub)
10dd3ebe 427 return;
df4fc315 428
491d0dcf 429 update_ftrace_function();
df4fc315
SR
430}
431
493762fc
SR
432#ifdef CONFIG_FUNCTION_PROFILER
433struct ftrace_profile {
434 struct hlist_node node;
435 unsigned long ip;
436 unsigned long counter;
0706f1c4
SR
437#ifdef CONFIG_FUNCTION_GRAPH_TRACER
438 unsigned long long time;
e330b3bc 439 unsigned long long time_squared;
0706f1c4 440#endif
8fc0c701
SR
441};
442
493762fc
SR
443struct ftrace_profile_page {
444 struct ftrace_profile_page *next;
445 unsigned long index;
446 struct ftrace_profile records[];
d61f82d0
SR
447};
448
cafb168a
SR
449struct ftrace_profile_stat {
450 atomic_t disabled;
451 struct hlist_head *hash;
452 struct ftrace_profile_page *pages;
453 struct ftrace_profile_page *start;
454 struct tracer_stat stat;
455};
456
493762fc
SR
457#define PROFILE_RECORDS_SIZE \
458 (PAGE_SIZE - offsetof(struct ftrace_profile_page, records))
5072c59f 459
493762fc
SR
460#define PROFILES_PER_PAGE \
461 (PROFILE_RECORDS_SIZE / sizeof(struct ftrace_profile))
3d083395 462
fb9fb015
SR
463static int ftrace_profile_bits __read_mostly;
464static int ftrace_profile_enabled __read_mostly;
465
466/* ftrace_profile_lock - synchronize the enable and disable of the profiler */
bac429f0
SR
467static DEFINE_MUTEX(ftrace_profile_lock);
468
cafb168a 469static DEFINE_PER_CPU(struct ftrace_profile_stat, ftrace_profile_stats);
493762fc
SR
470
471#define FTRACE_PROFILE_HASH_SIZE 1024 /* must be power of 2 */
472
bac429f0
SR
473static void *
474function_stat_next(void *v, int idx)
475{
493762fc
SR
476 struct ftrace_profile *rec = v;
477 struct ftrace_profile_page *pg;
bac429f0 478
493762fc 479 pg = (struct ftrace_profile_page *)((unsigned long)rec & PAGE_MASK);
bac429f0
SR
480
481 again:
0296e425
LZ
482 if (idx != 0)
483 rec++;
484
bac429f0
SR
485 if ((void *)rec >= (void *)&pg->records[pg->index]) {
486 pg = pg->next;
487 if (!pg)
488 return NULL;
489 rec = &pg->records[0];
493762fc
SR
490 if (!rec->counter)
491 goto again;
bac429f0
SR
492 }
493
bac429f0
SR
494 return rec;
495}
496
497static void *function_stat_start(struct tracer_stat *trace)
498{
cafb168a
SR
499 struct ftrace_profile_stat *stat =
500 container_of(trace, struct ftrace_profile_stat, stat);
501
502 if (!stat || !stat->start)
503 return NULL;
504
505 return function_stat_next(&stat->start->records[0], 0);
bac429f0
SR
506}
507
0706f1c4
SR
508#ifdef CONFIG_FUNCTION_GRAPH_TRACER
509/* function graph compares on total time */
510static int function_stat_cmp(void *p1, void *p2)
511{
512 struct ftrace_profile *a = p1;
513 struct ftrace_profile *b = p2;
514
515 if (a->time < b->time)
516 return -1;
517 if (a->time > b->time)
518 return 1;
519 else
520 return 0;
521}
522#else
523/* not function graph compares against hits */
bac429f0
SR
524static int function_stat_cmp(void *p1, void *p2)
525{
493762fc
SR
526 struct ftrace_profile *a = p1;
527 struct ftrace_profile *b = p2;
bac429f0
SR
528
529 if (a->counter < b->counter)
530 return -1;
531 if (a->counter > b->counter)
532 return 1;
533 else
534 return 0;
535}
0706f1c4 536#endif
bac429f0
SR
537
538static int function_stat_headers(struct seq_file *m)
539{
0706f1c4 540#ifdef CONFIG_FUNCTION_GRAPH_TRACER
34886c8b 541 seq_printf(m, " Function "
e330b3bc 542 "Hit Time Avg s^2\n"
34886c8b 543 " -------- "
e330b3bc 544 "--- ---- --- ---\n");
0706f1c4 545#else
bac429f0
SR
546 seq_printf(m, " Function Hit\n"
547 " -------- ---\n");
0706f1c4 548#endif
bac429f0
SR
549 return 0;
550}
551
552static int function_stat_show(struct seq_file *m, void *v)
553{
493762fc 554 struct ftrace_profile *rec = v;
bac429f0 555 char str[KSYM_SYMBOL_LEN];
3aaba20f 556 int ret = 0;
0706f1c4 557#ifdef CONFIG_FUNCTION_GRAPH_TRACER
34886c8b
SR
558 static struct trace_seq s;
559 unsigned long long avg;
e330b3bc 560 unsigned long long stddev;
0706f1c4 561#endif
3aaba20f
LZ
562 mutex_lock(&ftrace_profile_lock);
563
564 /* we raced with function_profile_reset() */
565 if (unlikely(rec->counter == 0)) {
566 ret = -EBUSY;
567 goto out;
568 }
bac429f0
SR
569
570 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
0706f1c4
SR
571 seq_printf(m, " %-30.30s %10lu", str, rec->counter);
572
573#ifdef CONFIG_FUNCTION_GRAPH_TRACER
574 seq_printf(m, " ");
34886c8b
SR
575 avg = rec->time;
576 do_div(avg, rec->counter);
577
e330b3bc
CD
578 /* Sample standard deviation (s^2) */
579 if (rec->counter <= 1)
580 stddev = 0;
581 else {
582 stddev = rec->time_squared - rec->counter * avg * avg;
583 /*
584 * Divide only 1000 for ns^2 -> us^2 conversion.
585 * trace_print_graph_duration will divide 1000 again.
586 */
587 do_div(stddev, (rec->counter - 1) * 1000);
588 }
589
34886c8b
SR
590 trace_seq_init(&s);
591 trace_print_graph_duration(rec->time, &s);
592 trace_seq_puts(&s, " ");
593 trace_print_graph_duration(avg, &s);
e330b3bc
CD
594 trace_seq_puts(&s, " ");
595 trace_print_graph_duration(stddev, &s);
0706f1c4 596 trace_print_seq(m, &s);
0706f1c4
SR
597#endif
598 seq_putc(m, '\n');
3aaba20f
LZ
599out:
600 mutex_unlock(&ftrace_profile_lock);
bac429f0 601
3aaba20f 602 return ret;
bac429f0
SR
603}
604
cafb168a 605static void ftrace_profile_reset(struct ftrace_profile_stat *stat)
bac429f0 606{
493762fc 607 struct ftrace_profile_page *pg;
bac429f0 608
cafb168a 609 pg = stat->pages = stat->start;
bac429f0 610
493762fc
SR
611 while (pg) {
612 memset(pg->records, 0, PROFILE_RECORDS_SIZE);
613 pg->index = 0;
614 pg = pg->next;
bac429f0
SR
615 }
616
cafb168a 617 memset(stat->hash, 0,
493762fc
SR
618 FTRACE_PROFILE_HASH_SIZE * sizeof(struct hlist_head));
619}
bac429f0 620
cafb168a 621int ftrace_profile_pages_init(struct ftrace_profile_stat *stat)
493762fc
SR
622{
623 struct ftrace_profile_page *pg;
318e0a73
SR
624 int functions;
625 int pages;
493762fc 626 int i;
bac429f0 627
493762fc 628 /* If we already allocated, do nothing */
cafb168a 629 if (stat->pages)
493762fc 630 return 0;
bac429f0 631
cafb168a
SR
632 stat->pages = (void *)get_zeroed_page(GFP_KERNEL);
633 if (!stat->pages)
493762fc 634 return -ENOMEM;
bac429f0 635
318e0a73
SR
636#ifdef CONFIG_DYNAMIC_FTRACE
637 functions = ftrace_update_tot_cnt;
638#else
639 /*
640 * We do not know the number of functions that exist because
641 * dynamic tracing is what counts them. With past experience
642 * we have around 20K functions. That should be more than enough.
643 * It is highly unlikely we will execute every function in
644 * the kernel.
645 */
646 functions = 20000;
647#endif
648
cafb168a 649 pg = stat->start = stat->pages;
bac429f0 650
318e0a73
SR
651 pages = DIV_ROUND_UP(functions, PROFILES_PER_PAGE);
652
653 for (i = 0; i < pages; i++) {
493762fc 654 pg->next = (void *)get_zeroed_page(GFP_KERNEL);
493762fc 655 if (!pg->next)
318e0a73 656 goto out_free;
493762fc
SR
657 pg = pg->next;
658 }
659
660 return 0;
318e0a73
SR
661
662 out_free:
663 pg = stat->start;
664 while (pg) {
665 unsigned long tmp = (unsigned long)pg;
666
667 pg = pg->next;
668 free_page(tmp);
669 }
670
671 free_page((unsigned long)stat->pages);
672 stat->pages = NULL;
673 stat->start = NULL;
674
675 return -ENOMEM;
bac429f0
SR
676}
677
cafb168a 678static int ftrace_profile_init_cpu(int cpu)
bac429f0 679{
cafb168a 680 struct ftrace_profile_stat *stat;
493762fc 681 int size;
bac429f0 682
cafb168a
SR
683 stat = &per_cpu(ftrace_profile_stats, cpu);
684
685 if (stat->hash) {
493762fc 686 /* If the profile is already created, simply reset it */
cafb168a 687 ftrace_profile_reset(stat);
493762fc
SR
688 return 0;
689 }
bac429f0 690
493762fc
SR
691 /*
692 * We are profiling all functions, but usually only a few thousand
693 * functions are hit. We'll make a hash of 1024 items.
694 */
695 size = FTRACE_PROFILE_HASH_SIZE;
bac429f0 696
cafb168a 697 stat->hash = kzalloc(sizeof(struct hlist_head) * size, GFP_KERNEL);
493762fc 698
cafb168a 699 if (!stat->hash)
493762fc
SR
700 return -ENOMEM;
701
cafb168a
SR
702 if (!ftrace_profile_bits) {
703 size--;
493762fc 704
cafb168a
SR
705 for (; size; size >>= 1)
706 ftrace_profile_bits++;
707 }
493762fc 708
318e0a73 709 /* Preallocate the function profiling pages */
cafb168a
SR
710 if (ftrace_profile_pages_init(stat) < 0) {
711 kfree(stat->hash);
712 stat->hash = NULL;
493762fc
SR
713 return -ENOMEM;
714 }
715
716 return 0;
bac429f0
SR
717}
718
cafb168a
SR
719static int ftrace_profile_init(void)
720{
721 int cpu;
722 int ret = 0;
723
724 for_each_online_cpu(cpu) {
725 ret = ftrace_profile_init_cpu(cpu);
726 if (ret)
727 break;
728 }
729
730 return ret;
731}
732
493762fc 733/* interrupts must be disabled */
cafb168a
SR
734static struct ftrace_profile *
735ftrace_find_profiled_func(struct ftrace_profile_stat *stat, unsigned long ip)
bac429f0 736{
493762fc 737 struct ftrace_profile *rec;
bac429f0
SR
738 struct hlist_head *hhd;
739 struct hlist_node *n;
bac429f0
SR
740 unsigned long key;
741
bac429f0 742 key = hash_long(ip, ftrace_profile_bits);
cafb168a 743 hhd = &stat->hash[key];
bac429f0
SR
744
745 if (hlist_empty(hhd))
746 return NULL;
747
bac429f0
SR
748 hlist_for_each_entry_rcu(rec, n, hhd, node) {
749 if (rec->ip == ip)
493762fc
SR
750 return rec;
751 }
752
753 return NULL;
754}
755
cafb168a
SR
756static void ftrace_add_profile(struct ftrace_profile_stat *stat,
757 struct ftrace_profile *rec)
493762fc
SR
758{
759 unsigned long key;
760
761 key = hash_long(rec->ip, ftrace_profile_bits);
cafb168a 762 hlist_add_head_rcu(&rec->node, &stat->hash[key]);
493762fc
SR
763}
764
318e0a73
SR
765/*
766 * The memory is already allocated, this simply finds a new record to use.
767 */
493762fc 768static struct ftrace_profile *
318e0a73 769ftrace_profile_alloc(struct ftrace_profile_stat *stat, unsigned long ip)
493762fc
SR
770{
771 struct ftrace_profile *rec = NULL;
772
318e0a73 773 /* prevent recursion (from NMIs) */
cafb168a 774 if (atomic_inc_return(&stat->disabled) != 1)
493762fc
SR
775 goto out;
776
493762fc 777 /*
318e0a73
SR
778 * Try to find the function again since an NMI
779 * could have added it
493762fc 780 */
cafb168a 781 rec = ftrace_find_profiled_func(stat, ip);
493762fc 782 if (rec)
cafb168a 783 goto out;
493762fc 784
cafb168a
SR
785 if (stat->pages->index == PROFILES_PER_PAGE) {
786 if (!stat->pages->next)
787 goto out;
788 stat->pages = stat->pages->next;
bac429f0 789 }
493762fc 790
cafb168a 791 rec = &stat->pages->records[stat->pages->index++];
493762fc 792 rec->ip = ip;
cafb168a 793 ftrace_add_profile(stat, rec);
493762fc 794
bac429f0 795 out:
cafb168a 796 atomic_dec(&stat->disabled);
bac429f0
SR
797
798 return rec;
799}
800
801static void
2f5f6ad9 802function_profile_call(unsigned long ip, unsigned long parent_ip,
a1e2e31d 803 struct ftrace_ops *ops, struct pt_regs *regs)
bac429f0 804{
cafb168a 805 struct ftrace_profile_stat *stat;
493762fc 806 struct ftrace_profile *rec;
bac429f0
SR
807 unsigned long flags;
808
809 if (!ftrace_profile_enabled)
810 return;
811
812 local_irq_save(flags);
cafb168a
SR
813
814 stat = &__get_cpu_var(ftrace_profile_stats);
0f6ce3de 815 if (!stat->hash || !ftrace_profile_enabled)
cafb168a
SR
816 goto out;
817
818 rec = ftrace_find_profiled_func(stat, ip);
493762fc 819 if (!rec) {
318e0a73 820 rec = ftrace_profile_alloc(stat, ip);
493762fc
SR
821 if (!rec)
822 goto out;
823 }
bac429f0
SR
824
825 rec->counter++;
826 out:
827 local_irq_restore(flags);
828}
829
0706f1c4
SR
830#ifdef CONFIG_FUNCTION_GRAPH_TRACER
831static int profile_graph_entry(struct ftrace_graph_ent *trace)
832{
a1e2e31d 833 function_profile_call(trace->func, 0, NULL, NULL);
0706f1c4
SR
834 return 1;
835}
836
837static void profile_graph_return(struct ftrace_graph_ret *trace)
838{
cafb168a 839 struct ftrace_profile_stat *stat;
a2a16d6a 840 unsigned long long calltime;
0706f1c4 841 struct ftrace_profile *rec;
cafb168a 842 unsigned long flags;
0706f1c4
SR
843
844 local_irq_save(flags);
cafb168a 845 stat = &__get_cpu_var(ftrace_profile_stats);
0f6ce3de 846 if (!stat->hash || !ftrace_profile_enabled)
cafb168a
SR
847 goto out;
848
37e44bc5
SR
849 /* If the calltime was zero'd ignore it */
850 if (!trace->calltime)
851 goto out;
852
a2a16d6a
SR
853 calltime = trace->rettime - trace->calltime;
854
855 if (!(trace_flags & TRACE_ITER_GRAPH_TIME)) {
856 int index;
857
858 index = trace->depth;
859
860 /* Append this call time to the parent time to subtract */
861 if (index)
862 current->ret_stack[index - 1].subtime += calltime;
863
864 if (current->ret_stack[index].subtime < calltime)
865 calltime -= current->ret_stack[index].subtime;
866 else
867 calltime = 0;
868 }
869
cafb168a 870 rec = ftrace_find_profiled_func(stat, trace->func);
e330b3bc 871 if (rec) {
a2a16d6a 872 rec->time += calltime;
e330b3bc
CD
873 rec->time_squared += calltime * calltime;
874 }
a2a16d6a 875
cafb168a 876 out:
0706f1c4
SR
877 local_irq_restore(flags);
878}
879
880static int register_ftrace_profiler(void)
881{
882 return register_ftrace_graph(&profile_graph_return,
883 &profile_graph_entry);
884}
885
886static void unregister_ftrace_profiler(void)
887{
888 unregister_ftrace_graph();
889}
890#else
bd38c0e6 891static struct ftrace_ops ftrace_profile_ops __read_mostly = {
fb9fb015 892 .func = function_profile_call,
4740974a 893 .flags = FTRACE_OPS_FL_RECURSION_SAFE,
bac429f0
SR
894};
895
0706f1c4
SR
896static int register_ftrace_profiler(void)
897{
898 return register_ftrace_function(&ftrace_profile_ops);
899}
900
901static void unregister_ftrace_profiler(void)
902{
903 unregister_ftrace_function(&ftrace_profile_ops);
904}
905#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
906
bac429f0
SR
907static ssize_t
908ftrace_profile_write(struct file *filp, const char __user *ubuf,
909 size_t cnt, loff_t *ppos)
910{
911 unsigned long val;
bac429f0
SR
912 int ret;
913
22fe9b54
PH
914 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
915 if (ret)
bac429f0
SR
916 return ret;
917
918 val = !!val;
919
920 mutex_lock(&ftrace_profile_lock);
921 if (ftrace_profile_enabled ^ val) {
922 if (val) {
493762fc
SR
923 ret = ftrace_profile_init();
924 if (ret < 0) {
925 cnt = ret;
926 goto out;
927 }
928
0706f1c4
SR
929 ret = register_ftrace_profiler();
930 if (ret < 0) {
931 cnt = ret;
932 goto out;
933 }
bac429f0
SR
934 ftrace_profile_enabled = 1;
935 } else {
936 ftrace_profile_enabled = 0;
0f6ce3de
SR
937 /*
938 * unregister_ftrace_profiler calls stop_machine
939 * so this acts like an synchronize_sched.
940 */
0706f1c4 941 unregister_ftrace_profiler();
bac429f0
SR
942 }
943 }
493762fc 944 out:
bac429f0
SR
945 mutex_unlock(&ftrace_profile_lock);
946
cf8517cf 947 *ppos += cnt;
bac429f0
SR
948
949 return cnt;
950}
951
493762fc
SR
952static ssize_t
953ftrace_profile_read(struct file *filp, char __user *ubuf,
954 size_t cnt, loff_t *ppos)
955{
fb9fb015 956 char buf[64]; /* big enough to hold a number */
493762fc
SR
957 int r;
958
959 r = sprintf(buf, "%u\n", ftrace_profile_enabled);
960 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
961}
962
bac429f0
SR
963static const struct file_operations ftrace_profile_fops = {
964 .open = tracing_open_generic,
965 .read = ftrace_profile_read,
966 .write = ftrace_profile_write,
6038f373 967 .llseek = default_llseek,
bac429f0
SR
968};
969
cafb168a
SR
970/* used to initialize the real stat files */
971static struct tracer_stat function_stats __initdata = {
fb9fb015
SR
972 .name = "functions",
973 .stat_start = function_stat_start,
974 .stat_next = function_stat_next,
975 .stat_cmp = function_stat_cmp,
976 .stat_headers = function_stat_headers,
977 .stat_show = function_stat_show
cafb168a
SR
978};
979
6ab5d668 980static __init void ftrace_profile_debugfs(struct dentry *d_tracer)
bac429f0 981{
cafb168a 982 struct ftrace_profile_stat *stat;
bac429f0 983 struct dentry *entry;
cafb168a 984 char *name;
bac429f0 985 int ret;
cafb168a
SR
986 int cpu;
987
988 for_each_possible_cpu(cpu) {
989 stat = &per_cpu(ftrace_profile_stats, cpu);
990
991 /* allocate enough for function name + cpu number */
992 name = kmalloc(32, GFP_KERNEL);
993 if (!name) {
994 /*
995 * The files created are permanent, if something happens
996 * we still do not free memory.
997 */
cafb168a
SR
998 WARN(1,
999 "Could not allocate stat file for cpu %d\n",
1000 cpu);
1001 return;
1002 }
1003 stat->stat = function_stats;
1004 snprintf(name, 32, "function%d", cpu);
1005 stat->stat.name = name;
1006 ret = register_stat_tracer(&stat->stat);
1007 if (ret) {
1008 WARN(1,
1009 "Could not register function stat for cpu %d\n",
1010 cpu);
1011 kfree(name);
1012 return;
1013 }
bac429f0
SR
1014 }
1015
1016 entry = debugfs_create_file("function_profile_enabled", 0644,
1017 d_tracer, NULL, &ftrace_profile_fops);
1018 if (!entry)
1019 pr_warning("Could not create debugfs "
1020 "'function_profile_enabled' entry\n");
1021}
1022
bac429f0 1023#else /* CONFIG_FUNCTION_PROFILER */
6ab5d668 1024static __init void ftrace_profile_debugfs(struct dentry *d_tracer)
bac429f0
SR
1025{
1026}
bac429f0
SR
1027#endif /* CONFIG_FUNCTION_PROFILER */
1028
493762fc
SR
1029static struct pid * const ftrace_swapper_pid = &init_struct_pid;
1030
1031#ifdef CONFIG_DYNAMIC_FTRACE
1032
1033#ifndef CONFIG_FTRACE_MCOUNT_RECORD
1034# error Dynamic ftrace depends on MCOUNT_RECORD
1035#endif
1036
1037static struct hlist_head ftrace_func_hash[FTRACE_FUNC_HASHSIZE] __read_mostly;
1038
1039struct ftrace_func_probe {
1040 struct hlist_node node;
1041 struct ftrace_probe_ops *ops;
1042 unsigned long flags;
1043 unsigned long ip;
1044 void *data;
1045 struct rcu_head rcu;
1046};
1047
b448c4e3
SR
1048struct ftrace_func_entry {
1049 struct hlist_node hlist;
1050 unsigned long ip;
1051};
1052
1053struct ftrace_hash {
1054 unsigned long size_bits;
1055 struct hlist_head *buckets;
1056 unsigned long count;
07fd5515 1057 struct rcu_head rcu;
b448c4e3
SR
1058};
1059
33dc9b12
SR
1060/*
1061 * We make these constant because no one should touch them,
1062 * but they are used as the default "empty hash", to avoid allocating
1063 * it all the time. These are in a read only section such that if
1064 * anyone does try to modify it, it will cause an exception.
1065 */
1066static const struct hlist_head empty_buckets[1];
1067static const struct ftrace_hash empty_hash = {
1068 .buckets = (struct hlist_head *)empty_buckets,
1cf41dd7 1069};
33dc9b12 1070#define EMPTY_HASH ((struct ftrace_hash *)&empty_hash)
493762fc 1071
2b499381 1072static struct ftrace_ops global_ops = {
f45948e8 1073 .func = ftrace_stub,
33dc9b12
SR
1074 .notrace_hash = EMPTY_HASH,
1075 .filter_hash = EMPTY_HASH,
4740974a 1076 .flags = FTRACE_OPS_FL_RECURSION_SAFE,
f45948e8
SR
1077};
1078
493762fc
SR
1079static DEFINE_MUTEX(ftrace_regex_lock);
1080
1081struct ftrace_page {
1082 struct ftrace_page *next;
a7900875 1083 struct dyn_ftrace *records;
493762fc 1084 int index;
a7900875 1085 int size;
493762fc
SR
1086};
1087
85ae32ae
SR
1088static struct ftrace_page *ftrace_new_pgs;
1089
a7900875
SR
1090#define ENTRY_SIZE sizeof(struct dyn_ftrace)
1091#define ENTRIES_PER_PAGE (PAGE_SIZE / ENTRY_SIZE)
493762fc
SR
1092
1093/* estimate from running different kernels */
1094#define NR_TO_INIT 10000
1095
1096static struct ftrace_page *ftrace_pages_start;
1097static struct ftrace_page *ftrace_pages;
1098
06a51d93
SR
1099static bool ftrace_hash_empty(struct ftrace_hash *hash)
1100{
1101 return !hash || !hash->count;
1102}
1103
b448c4e3
SR
1104static struct ftrace_func_entry *
1105ftrace_lookup_ip(struct ftrace_hash *hash, unsigned long ip)
1106{
1107 unsigned long key;
1108 struct ftrace_func_entry *entry;
1109 struct hlist_head *hhd;
1110 struct hlist_node *n;
1111
06a51d93 1112 if (ftrace_hash_empty(hash))
b448c4e3
SR
1113 return NULL;
1114
1115 if (hash->size_bits > 0)
1116 key = hash_long(ip, hash->size_bits);
1117 else
1118 key = 0;
1119
1120 hhd = &hash->buckets[key];
1121
1122 hlist_for_each_entry_rcu(entry, n, hhd, hlist) {
1123 if (entry->ip == ip)
1124 return entry;
1125 }
1126 return NULL;
1127}
1128
33dc9b12
SR
1129static void __add_hash_entry(struct ftrace_hash *hash,
1130 struct ftrace_func_entry *entry)
b448c4e3 1131{
b448c4e3
SR
1132 struct hlist_head *hhd;
1133 unsigned long key;
1134
b448c4e3 1135 if (hash->size_bits)
33dc9b12 1136 key = hash_long(entry->ip, hash->size_bits);
b448c4e3
SR
1137 else
1138 key = 0;
1139
b448c4e3
SR
1140 hhd = &hash->buckets[key];
1141 hlist_add_head(&entry->hlist, hhd);
1142 hash->count++;
33dc9b12
SR
1143}
1144
1145static int add_hash_entry(struct ftrace_hash *hash, unsigned long ip)
1146{
1147 struct ftrace_func_entry *entry;
1148
1149 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
1150 if (!entry)
1151 return -ENOMEM;
1152
1153 entry->ip = ip;
1154 __add_hash_entry(hash, entry);
b448c4e3
SR
1155
1156 return 0;
1157}
1158
1159static void
33dc9b12 1160free_hash_entry(struct ftrace_hash *hash,
b448c4e3
SR
1161 struct ftrace_func_entry *entry)
1162{
1163 hlist_del(&entry->hlist);
1164 kfree(entry);
1165 hash->count--;
1166}
1167
33dc9b12
SR
1168static void
1169remove_hash_entry(struct ftrace_hash *hash,
1170 struct ftrace_func_entry *entry)
1171{
1172 hlist_del(&entry->hlist);
1173 hash->count--;
1174}
1175
b448c4e3
SR
1176static void ftrace_hash_clear(struct ftrace_hash *hash)
1177{
1178 struct hlist_head *hhd;
1179 struct hlist_node *tp, *tn;
1180 struct ftrace_func_entry *entry;
1181 int size = 1 << hash->size_bits;
1182 int i;
1183
33dc9b12
SR
1184 if (!hash->count)
1185 return;
1186
b448c4e3
SR
1187 for (i = 0; i < size; i++) {
1188 hhd = &hash->buckets[i];
1189 hlist_for_each_entry_safe(entry, tp, tn, hhd, hlist)
33dc9b12 1190 free_hash_entry(hash, entry);
b448c4e3
SR
1191 }
1192 FTRACE_WARN_ON(hash->count);
1193}
1194
33dc9b12
SR
1195static void free_ftrace_hash(struct ftrace_hash *hash)
1196{
1197 if (!hash || hash == EMPTY_HASH)
1198 return;
1199 ftrace_hash_clear(hash);
1200 kfree(hash->buckets);
1201 kfree(hash);
1202}
1203
07fd5515
SR
1204static void __free_ftrace_hash_rcu(struct rcu_head *rcu)
1205{
1206 struct ftrace_hash *hash;
1207
1208 hash = container_of(rcu, struct ftrace_hash, rcu);
1209 free_ftrace_hash(hash);
1210}
1211
1212static void free_ftrace_hash_rcu(struct ftrace_hash *hash)
1213{
1214 if (!hash || hash == EMPTY_HASH)
1215 return;
1216 call_rcu_sched(&hash->rcu, __free_ftrace_hash_rcu);
1217}
1218
5500fa51
JO
1219void ftrace_free_filter(struct ftrace_ops *ops)
1220{
1221 free_ftrace_hash(ops->filter_hash);
1222 free_ftrace_hash(ops->notrace_hash);
1223}
1224
33dc9b12
SR
1225static struct ftrace_hash *alloc_ftrace_hash(int size_bits)
1226{
1227 struct ftrace_hash *hash;
1228 int size;
1229
1230 hash = kzalloc(sizeof(*hash), GFP_KERNEL);
1231 if (!hash)
1232 return NULL;
1233
1234 size = 1 << size_bits;
47b0edcb 1235 hash->buckets = kcalloc(size, sizeof(*hash->buckets), GFP_KERNEL);
33dc9b12
SR
1236
1237 if (!hash->buckets) {
1238 kfree(hash);
1239 return NULL;
1240 }
1241
1242 hash->size_bits = size_bits;
1243
1244 return hash;
1245}
1246
1247static struct ftrace_hash *
1248alloc_and_copy_ftrace_hash(int size_bits, struct ftrace_hash *hash)
1249{
1250 struct ftrace_func_entry *entry;
1251 struct ftrace_hash *new_hash;
1252 struct hlist_node *tp;
1253 int size;
1254 int ret;
1255 int i;
1256
1257 new_hash = alloc_ftrace_hash(size_bits);
1258 if (!new_hash)
1259 return NULL;
1260
1261 /* Empty hash? */
06a51d93 1262 if (ftrace_hash_empty(hash))
33dc9b12
SR
1263 return new_hash;
1264
1265 size = 1 << hash->size_bits;
1266 for (i = 0; i < size; i++) {
1267 hlist_for_each_entry(entry, tp, &hash->buckets[i], hlist) {
1268 ret = add_hash_entry(new_hash, entry->ip);
1269 if (ret < 0)
1270 goto free_hash;
1271 }
1272 }
1273
1274 FTRACE_WARN_ON(new_hash->count != hash->count);
1275
1276 return new_hash;
1277
1278 free_hash:
1279 free_ftrace_hash(new_hash);
1280 return NULL;
1281}
1282
41fb61c2
SR
1283static void
1284ftrace_hash_rec_disable(struct ftrace_ops *ops, int filter_hash);
1285static void
1286ftrace_hash_rec_enable(struct ftrace_ops *ops, int filter_hash);
1287
33dc9b12 1288static int
41fb61c2
SR
1289ftrace_hash_move(struct ftrace_ops *ops, int enable,
1290 struct ftrace_hash **dst, struct ftrace_hash *src)
33dc9b12
SR
1291{
1292 struct ftrace_func_entry *entry;
1293 struct hlist_node *tp, *tn;
1294 struct hlist_head *hhd;
07fd5515
SR
1295 struct ftrace_hash *old_hash;
1296 struct ftrace_hash *new_hash;
33dc9b12
SR
1297 unsigned long key;
1298 int size = src->count;
1299 int bits = 0;
41fb61c2 1300 int ret;
33dc9b12
SR
1301 int i;
1302
41fb61c2
SR
1303 /*
1304 * Remove the current set, update the hash and add
1305 * them back.
1306 */
1307 ftrace_hash_rec_disable(ops, enable);
1308
33dc9b12
SR
1309 /*
1310 * If the new source is empty, just free dst and assign it
1311 * the empty_hash.
1312 */
1313 if (!src->count) {
07fd5515
SR
1314 free_ftrace_hash_rcu(*dst);
1315 rcu_assign_pointer(*dst, EMPTY_HASH);
d4d34b98
SR
1316 /* still need to update the function records */
1317 ret = 0;
1318 goto out;
33dc9b12
SR
1319 }
1320
33dc9b12
SR
1321 /*
1322 * Make the hash size about 1/2 the # found
1323 */
1324 for (size /= 2; size; size >>= 1)
1325 bits++;
1326
1327 /* Don't allocate too much */
1328 if (bits > FTRACE_HASH_MAX_BITS)
1329 bits = FTRACE_HASH_MAX_BITS;
1330
41fb61c2 1331 ret = -ENOMEM;
07fd5515
SR
1332 new_hash = alloc_ftrace_hash(bits);
1333 if (!new_hash)
41fb61c2 1334 goto out;
33dc9b12
SR
1335
1336 size = 1 << src->size_bits;
1337 for (i = 0; i < size; i++) {
1338 hhd = &src->buckets[i];
1339 hlist_for_each_entry_safe(entry, tp, tn, hhd, hlist) {
1340 if (bits > 0)
1341 key = hash_long(entry->ip, bits);
1342 else
1343 key = 0;
1344 remove_hash_entry(src, entry);
07fd5515 1345 __add_hash_entry(new_hash, entry);
33dc9b12
SR
1346 }
1347 }
1348
07fd5515
SR
1349 old_hash = *dst;
1350 rcu_assign_pointer(*dst, new_hash);
1351 free_ftrace_hash_rcu(old_hash);
1352
41fb61c2
SR
1353 ret = 0;
1354 out:
1355 /*
1356 * Enable regardless of ret:
1357 * On success, we enable the new hash.
1358 * On failure, we re-enable the original hash.
1359 */
1360 ftrace_hash_rec_enable(ops, enable);
1361
1362 return ret;
33dc9b12
SR
1363}
1364
b848914c
SR
1365/*
1366 * Test the hashes for this ops to see if we want to call
1367 * the ops->func or not.
1368 *
1369 * It's a match if the ip is in the ops->filter_hash or
1370 * the filter_hash does not exist or is empty,
1371 * AND
1372 * the ip is not in the ops->notrace_hash.
cdbe61bf
SR
1373 *
1374 * This needs to be called with preemption disabled as
1375 * the hashes are freed with call_rcu_sched().
b848914c
SR
1376 */
1377static int
1378ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip)
1379{
1380 struct ftrace_hash *filter_hash;
1381 struct ftrace_hash *notrace_hash;
1382 int ret;
1383
b848914c
SR
1384 filter_hash = rcu_dereference_raw(ops->filter_hash);
1385 notrace_hash = rcu_dereference_raw(ops->notrace_hash);
1386
06a51d93 1387 if ((ftrace_hash_empty(filter_hash) ||
b848914c 1388 ftrace_lookup_ip(filter_hash, ip)) &&
06a51d93 1389 (ftrace_hash_empty(notrace_hash) ||
b848914c
SR
1390 !ftrace_lookup_ip(notrace_hash, ip)))
1391 ret = 1;
1392 else
1393 ret = 0;
b848914c
SR
1394
1395 return ret;
1396}
1397
493762fc
SR
1398/*
1399 * This is a double for. Do not use 'break' to break out of the loop,
1400 * you must use a goto.
1401 */
1402#define do_for_each_ftrace_rec(pg, rec) \
1403 for (pg = ftrace_pages_start; pg; pg = pg->next) { \
1404 int _____i; \
1405 for (_____i = 0; _____i < pg->index; _____i++) { \
1406 rec = &pg->records[_____i];
1407
1408#define while_for_each_ftrace_rec() \
1409 } \
1410 }
1411
5855fead
SR
1412
1413static int ftrace_cmp_recs(const void *a, const void *b)
1414{
a650e02a
SR
1415 const struct dyn_ftrace *key = a;
1416 const struct dyn_ftrace *rec = b;
5855fead 1417
a650e02a 1418 if (key->flags < rec->ip)
5855fead 1419 return -1;
a650e02a
SR
1420 if (key->ip >= rec->ip + MCOUNT_INSN_SIZE)
1421 return 1;
5855fead
SR
1422 return 0;
1423}
1424
f0cf973a 1425static unsigned long ftrace_location_range(unsigned long start, unsigned long end)
c88fd863
SR
1426{
1427 struct ftrace_page *pg;
1428 struct dyn_ftrace *rec;
5855fead 1429 struct dyn_ftrace key;
c88fd863 1430
a650e02a
SR
1431 key.ip = start;
1432 key.flags = end; /* overload flags, as it is unsigned long */
5855fead
SR
1433
1434 for (pg = ftrace_pages_start; pg; pg = pg->next) {
a650e02a
SR
1435 if (end < pg->records[0].ip ||
1436 start >= (pg->records[pg->index - 1].ip + MCOUNT_INSN_SIZE))
9644302e 1437 continue;
5855fead
SR
1438 rec = bsearch(&key, pg->records, pg->index,
1439 sizeof(struct dyn_ftrace),
1440 ftrace_cmp_recs);
1441 if (rec)
f0cf973a 1442 return rec->ip;
5855fead 1443 }
c88fd863
SR
1444
1445 return 0;
1446}
1447
a650e02a
SR
1448/**
1449 * ftrace_location - return true if the ip giving is a traced location
1450 * @ip: the instruction pointer to check
1451 *
f0cf973a 1452 * Returns rec->ip if @ip given is a pointer to a ftrace location.
a650e02a
SR
1453 * That is, the instruction that is either a NOP or call to
1454 * the function tracer. It checks the ftrace internal tables to
1455 * determine if the address belongs or not.
1456 */
f0cf973a 1457unsigned long ftrace_location(unsigned long ip)
a650e02a
SR
1458{
1459 return ftrace_location_range(ip, ip);
1460}
1461
1462/**
1463 * ftrace_text_reserved - return true if range contains an ftrace location
1464 * @start: start of range to search
1465 * @end: end of range to search (inclusive). @end points to the last byte to check.
1466 *
1467 * Returns 1 if @start and @end contains a ftrace location.
1468 * That is, the instruction that is either a NOP or call to
1469 * the function tracer. It checks the ftrace internal tables to
1470 * determine if the address belongs or not.
1471 */
1472int ftrace_text_reserved(void *start, void *end)
1473{
f0cf973a
SR
1474 unsigned long ret;
1475
1476 ret = ftrace_location_range((unsigned long)start,
1477 (unsigned long)end);
1478
1479 return (int)!!ret;
a650e02a
SR
1480}
1481
ed926f9b
SR
1482static void __ftrace_hash_rec_update(struct ftrace_ops *ops,
1483 int filter_hash,
1484 bool inc)
1485{
1486 struct ftrace_hash *hash;
1487 struct ftrace_hash *other_hash;
1488 struct ftrace_page *pg;
1489 struct dyn_ftrace *rec;
1490 int count = 0;
1491 int all = 0;
1492
1493 /* Only update if the ops has been registered */
1494 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
1495 return;
1496
1497 /*
1498 * In the filter_hash case:
1499 * If the count is zero, we update all records.
1500 * Otherwise we just update the items in the hash.
1501 *
1502 * In the notrace_hash case:
1503 * We enable the update in the hash.
1504 * As disabling notrace means enabling the tracing,
1505 * and enabling notrace means disabling, the inc variable
1506 * gets inversed.
1507 */
1508 if (filter_hash) {
1509 hash = ops->filter_hash;
1510 other_hash = ops->notrace_hash;
06a51d93 1511 if (ftrace_hash_empty(hash))
ed926f9b
SR
1512 all = 1;
1513 } else {
1514 inc = !inc;
1515 hash = ops->notrace_hash;
1516 other_hash = ops->filter_hash;
1517 /*
1518 * If the notrace hash has no items,
1519 * then there's nothing to do.
1520 */
06a51d93 1521 if (ftrace_hash_empty(hash))
ed926f9b
SR
1522 return;
1523 }
1524
1525 do_for_each_ftrace_rec(pg, rec) {
1526 int in_other_hash = 0;
1527 int in_hash = 0;
1528 int match = 0;
1529
1530 if (all) {
1531 /*
1532 * Only the filter_hash affects all records.
1533 * Update if the record is not in the notrace hash.
1534 */
b848914c 1535 if (!other_hash || !ftrace_lookup_ip(other_hash, rec->ip))
ed926f9b
SR
1536 match = 1;
1537 } else {
06a51d93
SR
1538 in_hash = !!ftrace_lookup_ip(hash, rec->ip);
1539 in_other_hash = !!ftrace_lookup_ip(other_hash, rec->ip);
ed926f9b
SR
1540
1541 /*
1542 *
1543 */
1544 if (filter_hash && in_hash && !in_other_hash)
1545 match = 1;
1546 else if (!filter_hash && in_hash &&
06a51d93 1547 (in_other_hash || ftrace_hash_empty(other_hash)))
ed926f9b
SR
1548 match = 1;
1549 }
1550 if (!match)
1551 continue;
1552
1553 if (inc) {
1554 rec->flags++;
1555 if (FTRACE_WARN_ON((rec->flags & ~FTRACE_FL_MASK) == FTRACE_REF_MAX))
1556 return;
08f6fba5
SR
1557 /*
1558 * If any ops wants regs saved for this function
1559 * then all ops will get saved regs.
1560 */
1561 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS)
1562 rec->flags |= FTRACE_FL_REGS;
ed926f9b
SR
1563 } else {
1564 if (FTRACE_WARN_ON((rec->flags & ~FTRACE_FL_MASK) == 0))
1565 return;
1566 rec->flags--;
1567 }
1568 count++;
1569 /* Shortcut, if we handled all records, we are done. */
1570 if (!all && count == hash->count)
1571 return;
1572 } while_for_each_ftrace_rec();
1573}
1574
1575static void ftrace_hash_rec_disable(struct ftrace_ops *ops,
1576 int filter_hash)
1577{
1578 __ftrace_hash_rec_update(ops, filter_hash, 0);
1579}
1580
1581static void ftrace_hash_rec_enable(struct ftrace_ops *ops,
1582 int filter_hash)
1583{
1584 __ftrace_hash_rec_update(ops, filter_hash, 1);
1585}
1586
b17e8a37
SR
1587static void print_ip_ins(const char *fmt, unsigned char *p)
1588{
1589 int i;
1590
1591 printk(KERN_CONT "%s", fmt);
1592
1593 for (i = 0; i < MCOUNT_INSN_SIZE; i++)
1594 printk(KERN_CONT "%s%02x", i ? ":" : "", p[i]);
1595}
1596
c88fd863
SR
1597/**
1598 * ftrace_bug - report and shutdown function tracer
1599 * @failed: The failed type (EFAULT, EINVAL, EPERM)
1600 * @ip: The address that failed
1601 *
1602 * The arch code that enables or disables the function tracing
1603 * can call ftrace_bug() when it has detected a problem in
1604 * modifying the code. @failed should be one of either:
1605 * EFAULT - if the problem happens on reading the @ip address
1606 * EINVAL - if what is read at @ip is not what was expected
1607 * EPERM - if the problem happens on writting to the @ip address
1608 */
1609void ftrace_bug(int failed, unsigned long ip)
b17e8a37
SR
1610{
1611 switch (failed) {
1612 case -EFAULT:
1613 FTRACE_WARN_ON_ONCE(1);
1614 pr_info("ftrace faulted on modifying ");
1615 print_ip_sym(ip);
1616 break;
1617 case -EINVAL:
1618 FTRACE_WARN_ON_ONCE(1);
1619 pr_info("ftrace failed to modify ");
1620 print_ip_sym(ip);
b17e8a37 1621 print_ip_ins(" actual: ", (unsigned char *)ip);
b17e8a37
SR
1622 printk(KERN_CONT "\n");
1623 break;
1624 case -EPERM:
1625 FTRACE_WARN_ON_ONCE(1);
1626 pr_info("ftrace faulted on writing ");
1627 print_ip_sym(ip);
1628 break;
1629 default:
1630 FTRACE_WARN_ON_ONCE(1);
1631 pr_info("ftrace faulted on unknown error ");
1632 print_ip_sym(ip);
1633 }
1634}
1635
c88fd863 1636static int ftrace_check_record(struct dyn_ftrace *rec, int enable, int update)
5072c59f 1637{
64fbcd16 1638 unsigned long flag = 0UL;
e7d3737e 1639
982c350b 1640 /*
30fb6aa7 1641 * If we are updating calls:
982c350b 1642 *
ed926f9b
SR
1643 * If the record has a ref count, then we need to enable it
1644 * because someone is using it.
982c350b 1645 *
ed926f9b
SR
1646 * Otherwise we make sure its disabled.
1647 *
30fb6aa7 1648 * If we are disabling calls, then disable all records that
ed926f9b 1649 * are enabled.
982c350b 1650 */
c88fd863 1651 if (enable && (rec->flags & ~FTRACE_FL_MASK))
ed926f9b 1652 flag = FTRACE_FL_ENABLED;
982c350b 1653
08f6fba5
SR
1654 /*
1655 * If enabling and the REGS flag does not match the REGS_EN, then
1656 * do not ignore this record. Set flags to fail the compare against
1657 * ENABLED.
1658 */
1659 if (flag &&
1660 (!(rec->flags & FTRACE_FL_REGS) != !(rec->flags & FTRACE_FL_REGS_EN)))
1661 flag |= FTRACE_FL_REGS;
1662
64fbcd16
XG
1663 /* If the state of this record hasn't changed, then do nothing */
1664 if ((rec->flags & FTRACE_FL_ENABLED) == flag)
c88fd863 1665 return FTRACE_UPDATE_IGNORE;
982c350b 1666
64fbcd16 1667 if (flag) {
08f6fba5
SR
1668 /* Save off if rec is being enabled (for return value) */
1669 flag ^= rec->flags & FTRACE_FL_ENABLED;
1670
1671 if (update) {
c88fd863 1672 rec->flags |= FTRACE_FL_ENABLED;
08f6fba5
SR
1673 if (flag & FTRACE_FL_REGS) {
1674 if (rec->flags & FTRACE_FL_REGS)
1675 rec->flags |= FTRACE_FL_REGS_EN;
1676 else
1677 rec->flags &= ~FTRACE_FL_REGS_EN;
1678 }
1679 }
1680
1681 /*
1682 * If this record is being updated from a nop, then
1683 * return UPDATE_MAKE_CALL.
1684 * Otherwise, if the EN flag is set, then return
1685 * UPDATE_MODIFY_CALL_REGS to tell the caller to convert
1686 * from the non-save regs, to a save regs function.
1687 * Otherwise,
1688 * return UPDATE_MODIFY_CALL to tell the caller to convert
1689 * from the save regs, to a non-save regs function.
1690 */
1691 if (flag & FTRACE_FL_ENABLED)
1692 return FTRACE_UPDATE_MAKE_CALL;
1693 else if (rec->flags & FTRACE_FL_REGS_EN)
1694 return FTRACE_UPDATE_MODIFY_CALL_REGS;
1695 else
1696 return FTRACE_UPDATE_MODIFY_CALL;
c88fd863
SR
1697 }
1698
08f6fba5
SR
1699 if (update) {
1700 /* If there's no more users, clear all flags */
1701 if (!(rec->flags & ~FTRACE_FL_MASK))
1702 rec->flags = 0;
1703 else
1704 /* Just disable the record (keep REGS state) */
1705 rec->flags &= ~FTRACE_FL_ENABLED;
1706 }
c88fd863
SR
1707
1708 return FTRACE_UPDATE_MAKE_NOP;
1709}
1710
1711/**
1712 * ftrace_update_record, set a record that now is tracing or not
1713 * @rec: the record to update
1714 * @enable: set to 1 if the record is tracing, zero to force disable
1715 *
1716 * The records that represent all functions that can be traced need
1717 * to be updated when tracing has been enabled.
1718 */
1719int ftrace_update_record(struct dyn_ftrace *rec, int enable)
1720{
1721 return ftrace_check_record(rec, enable, 1);
1722}
1723
1724/**
1725 * ftrace_test_record, check if the record has been enabled or not
1726 * @rec: the record to test
1727 * @enable: set to 1 to check if enabled, 0 if it is disabled
1728 *
1729 * The arch code may need to test if a record is already set to
1730 * tracing to determine how to modify the function code that it
1731 * represents.
1732 */
1733int ftrace_test_record(struct dyn_ftrace *rec, int enable)
1734{
1735 return ftrace_check_record(rec, enable, 0);
1736}
1737
1738static int
1739__ftrace_replace_code(struct dyn_ftrace *rec, int enable)
1740{
08f6fba5 1741 unsigned long ftrace_old_addr;
c88fd863
SR
1742 unsigned long ftrace_addr;
1743 int ret;
1744
c88fd863
SR
1745 ret = ftrace_update_record(rec, enable);
1746
08f6fba5
SR
1747 if (rec->flags & FTRACE_FL_REGS)
1748 ftrace_addr = (unsigned long)FTRACE_REGS_ADDR;
1749 else
1750 ftrace_addr = (unsigned long)FTRACE_ADDR;
1751
c88fd863
SR
1752 switch (ret) {
1753 case FTRACE_UPDATE_IGNORE:
1754 return 0;
1755
1756 case FTRACE_UPDATE_MAKE_CALL:
64fbcd16 1757 return ftrace_make_call(rec, ftrace_addr);
c88fd863
SR
1758
1759 case FTRACE_UPDATE_MAKE_NOP:
1760 return ftrace_make_nop(NULL, rec, ftrace_addr);
08f6fba5
SR
1761
1762 case FTRACE_UPDATE_MODIFY_CALL_REGS:
1763 case FTRACE_UPDATE_MODIFY_CALL:
1764 if (rec->flags & FTRACE_FL_REGS)
1765 ftrace_old_addr = (unsigned long)FTRACE_ADDR;
1766 else
1767 ftrace_old_addr = (unsigned long)FTRACE_REGS_ADDR;
1768
1769 return ftrace_modify_call(rec, ftrace_old_addr, ftrace_addr);
5072c59f
SR
1770 }
1771
c88fd863 1772 return -1; /* unknow ftrace bug */
5072c59f
SR
1773}
1774
e4f5d544 1775void __weak ftrace_replace_code(int enable)
3c1720f0 1776{
3c1720f0
SR
1777 struct dyn_ftrace *rec;
1778 struct ftrace_page *pg;
6a24a244 1779 int failed;
3c1720f0 1780
45a4a237
SR
1781 if (unlikely(ftrace_disabled))
1782 return;
1783
265c831c 1784 do_for_each_ftrace_rec(pg, rec) {
e4f5d544 1785 failed = __ftrace_replace_code(rec, enable);
fa9d13cf 1786 if (failed) {
3279ba37
SR
1787 ftrace_bug(failed, rec->ip);
1788 /* Stop processing */
1789 return;
3c1720f0 1790 }
265c831c 1791 } while_for_each_ftrace_rec();
3c1720f0
SR
1792}
1793
c88fd863
SR
1794struct ftrace_rec_iter {
1795 struct ftrace_page *pg;
1796 int index;
1797};
1798
1799/**
1800 * ftrace_rec_iter_start, start up iterating over traced functions
1801 *
1802 * Returns an iterator handle that is used to iterate over all
1803 * the records that represent address locations where functions
1804 * are traced.
1805 *
1806 * May return NULL if no records are available.
1807 */
1808struct ftrace_rec_iter *ftrace_rec_iter_start(void)
1809{
1810 /*
1811 * We only use a single iterator.
1812 * Protected by the ftrace_lock mutex.
1813 */
1814 static struct ftrace_rec_iter ftrace_rec_iter;
1815 struct ftrace_rec_iter *iter = &ftrace_rec_iter;
1816
1817 iter->pg = ftrace_pages_start;
1818 iter->index = 0;
1819
1820 /* Could have empty pages */
1821 while (iter->pg && !iter->pg->index)
1822 iter->pg = iter->pg->next;
1823
1824 if (!iter->pg)
1825 return NULL;
1826
1827 return iter;
1828}
1829
1830/**
1831 * ftrace_rec_iter_next, get the next record to process.
1832 * @iter: The handle to the iterator.
1833 *
1834 * Returns the next iterator after the given iterator @iter.
1835 */
1836struct ftrace_rec_iter *ftrace_rec_iter_next(struct ftrace_rec_iter *iter)
1837{
1838 iter->index++;
1839
1840 if (iter->index >= iter->pg->index) {
1841 iter->pg = iter->pg->next;
1842 iter->index = 0;
1843
1844 /* Could have empty pages */
1845 while (iter->pg && !iter->pg->index)
1846 iter->pg = iter->pg->next;
1847 }
1848
1849 if (!iter->pg)
1850 return NULL;
1851
1852 return iter;
1853}
1854
1855/**
1856 * ftrace_rec_iter_record, get the record at the iterator location
1857 * @iter: The current iterator location
1858 *
1859 * Returns the record that the current @iter is at.
1860 */
1861struct dyn_ftrace *ftrace_rec_iter_record(struct ftrace_rec_iter *iter)
1862{
1863 return &iter->pg->records[iter->index];
1864}
1865
492a7ea5 1866static int
31e88909 1867ftrace_code_disable(struct module *mod, struct dyn_ftrace *rec)
3c1720f0
SR
1868{
1869 unsigned long ip;
593eb8a2 1870 int ret;
3c1720f0
SR
1871
1872 ip = rec->ip;
1873
45a4a237
SR
1874 if (unlikely(ftrace_disabled))
1875 return 0;
1876
25aac9dc 1877 ret = ftrace_make_nop(mod, rec, MCOUNT_ADDR);
593eb8a2 1878 if (ret) {
31e88909 1879 ftrace_bug(ret, ip);
492a7ea5 1880 return 0;
37ad5084 1881 }
492a7ea5 1882 return 1;
3c1720f0
SR
1883}
1884
000ab691
SR
1885/*
1886 * archs can override this function if they must do something
1887 * before the modifying code is performed.
1888 */
1889int __weak ftrace_arch_code_modify_prepare(void)
1890{
1891 return 0;
1892}
1893
1894/*
1895 * archs can override this function if they must do something
1896 * after the modifying code is performed.
1897 */
1898int __weak ftrace_arch_code_modify_post_process(void)
1899{
1900 return 0;
1901}
1902
8ed3e2cf 1903void ftrace_modify_all_code(int command)
3d083395 1904{
8ed3e2cf 1905 if (command & FTRACE_UPDATE_CALLS)
d61f82d0 1906 ftrace_replace_code(1);
8ed3e2cf 1907 else if (command & FTRACE_DISABLE_CALLS)
d61f82d0
SR
1908 ftrace_replace_code(0);
1909
8ed3e2cf 1910 if (command & FTRACE_UPDATE_TRACE_FUNC)
d61f82d0
SR
1911 ftrace_update_ftrace_func(ftrace_trace_function);
1912
8ed3e2cf 1913 if (command & FTRACE_START_FUNC_RET)
5a45cfe1 1914 ftrace_enable_ftrace_graph_caller();
8ed3e2cf 1915 else if (command & FTRACE_STOP_FUNC_RET)
5a45cfe1 1916 ftrace_disable_ftrace_graph_caller();
8ed3e2cf
SR
1917}
1918
1919static int __ftrace_modify_code(void *data)
1920{
1921 int *command = data;
1922
1923 ftrace_modify_all_code(*command);
5a45cfe1 1924
d61f82d0 1925 return 0;
3d083395
SR
1926}
1927
c88fd863
SR
1928/**
1929 * ftrace_run_stop_machine, go back to the stop machine method
1930 * @command: The command to tell ftrace what to do
1931 *
1932 * If an arch needs to fall back to the stop machine method, the
1933 * it can call this function.
1934 */
1935void ftrace_run_stop_machine(int command)
1936{
1937 stop_machine(__ftrace_modify_code, &command, NULL);
1938}
1939
1940/**
1941 * arch_ftrace_update_code, modify the code to trace or not trace
1942 * @command: The command that needs to be done
1943 *
1944 * Archs can override this function if it does not need to
1945 * run stop_machine() to modify code.
1946 */
1947void __weak arch_ftrace_update_code(int command)
1948{
1949 ftrace_run_stop_machine(command);
1950}
1951
e309b41d 1952static void ftrace_run_update_code(int command)
3d083395 1953{
000ab691
SR
1954 int ret;
1955
1956 ret = ftrace_arch_code_modify_prepare();
1957 FTRACE_WARN_ON(ret);
1958 if (ret)
1959 return;
c88fd863
SR
1960 /*
1961 * Do not call function tracer while we update the code.
1962 * We are in stop machine.
1963 */
1964 function_trace_stop++;
000ab691 1965
c88fd863
SR
1966 /*
1967 * By default we use stop_machine() to modify the code.
1968 * But archs can do what ever they want as long as it
1969 * is safe. The stop_machine() is the safest, but also
1970 * produces the most overhead.
1971 */
1972 arch_ftrace_update_code(command);
1973
c88fd863 1974 function_trace_stop--;
000ab691
SR
1975
1976 ret = ftrace_arch_code_modify_post_process();
1977 FTRACE_WARN_ON(ret);
3d083395
SR
1978}
1979
d61f82d0 1980static ftrace_func_t saved_ftrace_func;
60a7ecf4 1981static int ftrace_start_up;
b848914c 1982static int global_start_up;
df4fc315
SR
1983
1984static void ftrace_startup_enable(int command)
1985{
1986 if (saved_ftrace_func != ftrace_trace_function) {
1987 saved_ftrace_func = ftrace_trace_function;
1988 command |= FTRACE_UPDATE_TRACE_FUNC;
1989 }
1990
1991 if (!command || !ftrace_enabled)
1992 return;
1993
1994 ftrace_run_update_code(command);
1995}
d61f82d0 1996
a1cd6173 1997static int ftrace_startup(struct ftrace_ops *ops, int command)
3d083395 1998{
b848914c
SR
1999 bool hash_enable = true;
2000
4eebcc81 2001 if (unlikely(ftrace_disabled))
a1cd6173 2002 return -ENODEV;
4eebcc81 2003
60a7ecf4 2004 ftrace_start_up++;
30fb6aa7 2005 command |= FTRACE_UPDATE_CALLS;
d61f82d0 2006
b848914c
SR
2007 /* ops marked global share the filter hashes */
2008 if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
2009 ops = &global_ops;
2010 /* Don't update hash if global is already set */
2011 if (global_start_up)
2012 hash_enable = false;
2013 global_start_up++;
2014 }
2015
ed926f9b 2016 ops->flags |= FTRACE_OPS_FL_ENABLED;
b848914c 2017 if (hash_enable)
ed926f9b
SR
2018 ftrace_hash_rec_enable(ops, 1);
2019
df4fc315 2020 ftrace_startup_enable(command);
a1cd6173
SR
2021
2022 return 0;
3d083395
SR
2023}
2024
bd69c30b 2025static void ftrace_shutdown(struct ftrace_ops *ops, int command)
3d083395 2026{
b848914c
SR
2027 bool hash_disable = true;
2028
4eebcc81
SR
2029 if (unlikely(ftrace_disabled))
2030 return;
2031
60a7ecf4 2032 ftrace_start_up--;
9ea1a153
FW
2033 /*
2034 * Just warn in case of unbalance, no need to kill ftrace, it's not
2035 * critical but the ftrace_call callers may be never nopped again after
2036 * further ftrace uses.
2037 */
2038 WARN_ON_ONCE(ftrace_start_up < 0);
2039
b848914c
SR
2040 if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
2041 ops = &global_ops;
2042 global_start_up--;
2043 WARN_ON_ONCE(global_start_up < 0);
2044 /* Don't update hash if global still has users */
2045 if (global_start_up) {
2046 WARN_ON_ONCE(!ftrace_start_up);
2047 hash_disable = false;
2048 }
2049 }
2050
2051 if (hash_disable)
ed926f9b
SR
2052 ftrace_hash_rec_disable(ops, 1);
2053
b848914c 2054 if (ops != &global_ops || !global_start_up)
ed926f9b 2055 ops->flags &= ~FTRACE_OPS_FL_ENABLED;
b848914c 2056
30fb6aa7 2057 command |= FTRACE_UPDATE_CALLS;
3d083395 2058
d61f82d0
SR
2059 if (saved_ftrace_func != ftrace_trace_function) {
2060 saved_ftrace_func = ftrace_trace_function;
2061 command |= FTRACE_UPDATE_TRACE_FUNC;
2062 }
3d083395 2063
d61f82d0 2064 if (!command || !ftrace_enabled)
e6ea44e9 2065 return;
d61f82d0
SR
2066
2067 ftrace_run_update_code(command);
3d083395
SR
2068}
2069
e309b41d 2070static void ftrace_startup_sysctl(void)
b0fc494f 2071{
4eebcc81
SR
2072 if (unlikely(ftrace_disabled))
2073 return;
2074
d61f82d0
SR
2075 /* Force update next time */
2076 saved_ftrace_func = NULL;
60a7ecf4
SR
2077 /* ftrace_start_up is true if we want ftrace running */
2078 if (ftrace_start_up)
30fb6aa7 2079 ftrace_run_update_code(FTRACE_UPDATE_CALLS);
b0fc494f
SR
2080}
2081
e309b41d 2082static void ftrace_shutdown_sysctl(void)
b0fc494f 2083{
4eebcc81
SR
2084 if (unlikely(ftrace_disabled))
2085 return;
2086
60a7ecf4
SR
2087 /* ftrace_start_up is true if ftrace is running */
2088 if (ftrace_start_up)
79e406d7 2089 ftrace_run_update_code(FTRACE_DISABLE_CALLS);
b0fc494f
SR
2090}
2091
3d083395
SR
2092static cycle_t ftrace_update_time;
2093static unsigned long ftrace_update_cnt;
2094unsigned long ftrace_update_tot_cnt;
2095
f7bc8b61
SR
2096static int ops_traces_mod(struct ftrace_ops *ops)
2097{
2098 struct ftrace_hash *hash;
2099
2100 hash = ops->filter_hash;
06a51d93 2101 return ftrace_hash_empty(hash);
f7bc8b61
SR
2102}
2103
31e88909 2104static int ftrace_update_code(struct module *mod)
3d083395 2105{
85ae32ae 2106 struct ftrace_page *pg;
e94142a6 2107 struct dyn_ftrace *p;
f22f9a89 2108 cycle_t start, stop;
f7bc8b61 2109 unsigned long ref = 0;
85ae32ae 2110 int i;
f7bc8b61
SR
2111
2112 /*
2113 * When adding a module, we need to check if tracers are
2114 * currently enabled and if they are set to trace all functions.
2115 * If they are, we need to enable the module functions as well
2116 * as update the reference counts for those function records.
2117 */
2118 if (mod) {
2119 struct ftrace_ops *ops;
2120
2121 for (ops = ftrace_ops_list;
2122 ops != &ftrace_list_end; ops = ops->next) {
2123 if (ops->flags & FTRACE_OPS_FL_ENABLED &&
2124 ops_traces_mod(ops))
2125 ref++;
2126 }
2127 }
3d083395 2128
750ed1a4 2129 start = ftrace_now(raw_smp_processor_id());
3d083395
SR
2130 ftrace_update_cnt = 0;
2131
85ae32ae 2132 for (pg = ftrace_new_pgs; pg; pg = pg->next) {
3d083395 2133
85ae32ae
SR
2134 for (i = 0; i < pg->index; i++) {
2135 /* If something went wrong, bail without enabling anything */
2136 if (unlikely(ftrace_disabled))
2137 return -1;
f22f9a89 2138
85ae32ae
SR
2139 p = &pg->records[i];
2140 p->flags = ref;
f22f9a89 2141
85ae32ae
SR
2142 /*
2143 * Do the initial record conversion from mcount jump
2144 * to the NOP instructions.
2145 */
2146 if (!ftrace_code_disable(mod, p))
2147 break;
5cb084bb 2148
85ae32ae 2149 ftrace_update_cnt++;
5cb084bb 2150
85ae32ae
SR
2151 /*
2152 * If the tracing is enabled, go ahead and enable the record.
2153 *
2154 * The reason not to enable the record immediatelly is the
2155 * inherent check of ftrace_make_nop/ftrace_make_call for
2156 * correct previous instructions. Making first the NOP
2157 * conversion puts the module to the correct state, thus
2158 * passing the ftrace_make_call check.
2159 */
2160 if (ftrace_start_up && ref) {
2161 int failed = __ftrace_replace_code(p, 1);
2162 if (failed)
2163 ftrace_bug(failed, p->ip);
2164 }
5cb084bb 2165 }
3d083395
SR
2166 }
2167
85ae32ae
SR
2168 ftrace_new_pgs = NULL;
2169
750ed1a4 2170 stop = ftrace_now(raw_smp_processor_id());
3d083395
SR
2171 ftrace_update_time = stop - start;
2172 ftrace_update_tot_cnt += ftrace_update_cnt;
2173
16444a8a
ACM
2174 return 0;
2175}
2176
a7900875 2177static int ftrace_allocate_records(struct ftrace_page *pg, int count)
3c1720f0 2178{
a7900875 2179 int order;
3c1720f0 2180 int cnt;
3c1720f0 2181
a7900875
SR
2182 if (WARN_ON(!count))
2183 return -EINVAL;
2184
2185 order = get_count_order(DIV_ROUND_UP(count, ENTRIES_PER_PAGE));
3c1720f0
SR
2186
2187 /*
a7900875
SR
2188 * We want to fill as much as possible. No more than a page
2189 * may be empty.
3c1720f0 2190 */
a7900875
SR
2191 while ((PAGE_SIZE << order) / ENTRY_SIZE >= count + ENTRIES_PER_PAGE)
2192 order--;
3c1720f0 2193
a7900875
SR
2194 again:
2195 pg->records = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, order);
3c1720f0 2196
a7900875
SR
2197 if (!pg->records) {
2198 /* if we can't allocate this size, try something smaller */
2199 if (!order)
2200 return -ENOMEM;
2201 order >>= 1;
2202 goto again;
2203 }
3c1720f0 2204
a7900875
SR
2205 cnt = (PAGE_SIZE << order) / ENTRY_SIZE;
2206 pg->size = cnt;
3c1720f0 2207
a7900875
SR
2208 if (cnt > count)
2209 cnt = count;
2210
2211 return cnt;
2212}
2213
2214static struct ftrace_page *
2215ftrace_allocate_pages(unsigned long num_to_init)
2216{
2217 struct ftrace_page *start_pg;
2218 struct ftrace_page *pg;
2219 int order;
2220 int cnt;
2221
2222 if (!num_to_init)
2223 return 0;
2224
2225 start_pg = pg = kzalloc(sizeof(*pg), GFP_KERNEL);
2226 if (!pg)
2227 return NULL;
2228
2229 /*
2230 * Try to allocate as much as possible in one continues
2231 * location that fills in all of the space. We want to
2232 * waste as little space as possible.
2233 */
2234 for (;;) {
2235 cnt = ftrace_allocate_records(pg, num_to_init);
2236 if (cnt < 0)
2237 goto free_pages;
2238
2239 num_to_init -= cnt;
2240 if (!num_to_init)
3c1720f0
SR
2241 break;
2242
a7900875
SR
2243 pg->next = kzalloc(sizeof(*pg), GFP_KERNEL);
2244 if (!pg->next)
2245 goto free_pages;
2246
3c1720f0
SR
2247 pg = pg->next;
2248 }
2249
a7900875
SR
2250 return start_pg;
2251
2252 free_pages:
2253 while (start_pg) {
2254 order = get_count_order(pg->size / ENTRIES_PER_PAGE);
2255 free_pages((unsigned long)pg->records, order);
2256 start_pg = pg->next;
2257 kfree(pg);
2258 pg = start_pg;
2259 }
2260 pr_info("ftrace: FAILED to allocate memory for functions\n");
2261 return NULL;
2262}
2263
2264static int __init ftrace_dyn_table_alloc(unsigned long num_to_init)
2265{
2266 int cnt;
2267
2268 if (!num_to_init) {
2269 pr_info("ftrace: No functions to be traced?\n");
2270 return -1;
2271 }
2272
2273 cnt = num_to_init / ENTRIES_PER_PAGE;
2274 pr_info("ftrace: allocating %ld entries in %d pages\n",
2275 num_to_init, cnt + 1);
2276
3c1720f0
SR
2277 return 0;
2278}
2279
5072c59f
SR
2280#define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
2281
2282struct ftrace_iterator {
98c4fd04 2283 loff_t pos;
4aeb6967
SR
2284 loff_t func_pos;
2285 struct ftrace_page *pg;
2286 struct dyn_ftrace *func;
2287 struct ftrace_func_probe *probe;
2288 struct trace_parser parser;
1cf41dd7 2289 struct ftrace_hash *hash;
33dc9b12 2290 struct ftrace_ops *ops;
4aeb6967
SR
2291 int hidx;
2292 int idx;
2293 unsigned flags;
5072c59f
SR
2294};
2295
8fc0c701 2296static void *
4aeb6967 2297t_hash_next(struct seq_file *m, loff_t *pos)
8fc0c701
SR
2298{
2299 struct ftrace_iterator *iter = m->private;
4aeb6967 2300 struct hlist_node *hnd = NULL;
8fc0c701
SR
2301 struct hlist_head *hhd;
2302
8fc0c701 2303 (*pos)++;
98c4fd04 2304 iter->pos = *pos;
8fc0c701 2305
4aeb6967
SR
2306 if (iter->probe)
2307 hnd = &iter->probe->node;
8fc0c701
SR
2308 retry:
2309 if (iter->hidx >= FTRACE_FUNC_HASHSIZE)
2310 return NULL;
2311
2312 hhd = &ftrace_func_hash[iter->hidx];
2313
2314 if (hlist_empty(hhd)) {
2315 iter->hidx++;
2316 hnd = NULL;
2317 goto retry;
2318 }
2319
2320 if (!hnd)
2321 hnd = hhd->first;
2322 else {
2323 hnd = hnd->next;
2324 if (!hnd) {
2325 iter->hidx++;
2326 goto retry;
2327 }
2328 }
2329
4aeb6967
SR
2330 if (WARN_ON_ONCE(!hnd))
2331 return NULL;
2332
2333 iter->probe = hlist_entry(hnd, struct ftrace_func_probe, node);
2334
2335 return iter;
8fc0c701
SR
2336}
2337
2338static void *t_hash_start(struct seq_file *m, loff_t *pos)
2339{
2340 struct ftrace_iterator *iter = m->private;
2341 void *p = NULL;
d82d6244
LZ
2342 loff_t l;
2343
69a3083c
SR
2344 if (!(iter->flags & FTRACE_ITER_DO_HASH))
2345 return NULL;
2346
2bccfffd
SR
2347 if (iter->func_pos > *pos)
2348 return NULL;
8fc0c701 2349
d82d6244 2350 iter->hidx = 0;
2bccfffd 2351 for (l = 0; l <= (*pos - iter->func_pos); ) {
4aeb6967 2352 p = t_hash_next(m, &l);
d82d6244
LZ
2353 if (!p)
2354 break;
2355 }
4aeb6967
SR
2356 if (!p)
2357 return NULL;
2358
98c4fd04
SR
2359 /* Only set this if we have an item */
2360 iter->flags |= FTRACE_ITER_HASH;
2361
4aeb6967 2362 return iter;
8fc0c701
SR
2363}
2364
4aeb6967
SR
2365static int
2366t_hash_show(struct seq_file *m, struct ftrace_iterator *iter)
8fc0c701 2367{
b6887d79 2368 struct ftrace_func_probe *rec;
8fc0c701 2369
4aeb6967
SR
2370 rec = iter->probe;
2371 if (WARN_ON_ONCE(!rec))
2372 return -EIO;
8fc0c701 2373
809dcf29
SR
2374 if (rec->ops->print)
2375 return rec->ops->print(m, rec->ip, rec->ops, rec->data);
2376
b375a11a 2377 seq_printf(m, "%ps:%ps", (void *)rec->ip, (void *)rec->ops->func);
8fc0c701
SR
2378
2379 if (rec->data)
2380 seq_printf(m, ":%p", rec->data);
2381 seq_putc(m, '\n');
2382
2383 return 0;
2384}
2385
e309b41d 2386static void *
5072c59f
SR
2387t_next(struct seq_file *m, void *v, loff_t *pos)
2388{
2389 struct ftrace_iterator *iter = m->private;
fc13cb0c 2390 struct ftrace_ops *ops = iter->ops;
5072c59f
SR
2391 struct dyn_ftrace *rec = NULL;
2392
45a4a237
SR
2393 if (unlikely(ftrace_disabled))
2394 return NULL;
2395
8fc0c701 2396 if (iter->flags & FTRACE_ITER_HASH)
4aeb6967 2397 return t_hash_next(m, pos);
8fc0c701 2398
5072c59f 2399 (*pos)++;
1106b699 2400 iter->pos = iter->func_pos = *pos;
5072c59f 2401
0c75a3ed 2402 if (iter->flags & FTRACE_ITER_PRINTALL)
57c072c7 2403 return t_hash_start(m, pos);
0c75a3ed 2404
5072c59f
SR
2405 retry:
2406 if (iter->idx >= iter->pg->index) {
2407 if (iter->pg->next) {
2408 iter->pg = iter->pg->next;
2409 iter->idx = 0;
2410 goto retry;
2411 }
2412 } else {
2413 rec = &iter->pg->records[iter->idx++];
32082309 2414 if (((iter->flags & FTRACE_ITER_FILTER) &&
f45948e8 2415 !(ftrace_lookup_ip(ops->filter_hash, rec->ip))) ||
0183fb1c 2416
41c52c0d 2417 ((iter->flags & FTRACE_ITER_NOTRACE) &&
647bcd03
SR
2418 !ftrace_lookup_ip(ops->notrace_hash, rec->ip)) ||
2419
2420 ((iter->flags & FTRACE_ITER_ENABLED) &&
2421 !(rec->flags & ~FTRACE_FL_MASK))) {
2422
5072c59f
SR
2423 rec = NULL;
2424 goto retry;
2425 }
2426 }
2427
4aeb6967 2428 if (!rec)
57c072c7 2429 return t_hash_start(m, pos);
4aeb6967
SR
2430
2431 iter->func = rec;
2432
2433 return iter;
5072c59f
SR
2434}
2435
98c4fd04
SR
2436static void reset_iter_read(struct ftrace_iterator *iter)
2437{
2438 iter->pos = 0;
2439 iter->func_pos = 0;
2440 iter->flags &= ~(FTRACE_ITER_PRINTALL & FTRACE_ITER_HASH);
5072c59f
SR
2441}
2442
2443static void *t_start(struct seq_file *m, loff_t *pos)
2444{
2445 struct ftrace_iterator *iter = m->private;
fc13cb0c 2446 struct ftrace_ops *ops = iter->ops;
5072c59f 2447 void *p = NULL;
694ce0a5 2448 loff_t l;
5072c59f 2449
8fc0c701 2450 mutex_lock(&ftrace_lock);
45a4a237
SR
2451
2452 if (unlikely(ftrace_disabled))
2453 return NULL;
2454
98c4fd04
SR
2455 /*
2456 * If an lseek was done, then reset and start from beginning.
2457 */
2458 if (*pos < iter->pos)
2459 reset_iter_read(iter);
2460
0c75a3ed
SR
2461 /*
2462 * For set_ftrace_filter reading, if we have the filter
2463 * off, we can short cut and just print out that all
2464 * functions are enabled.
2465 */
06a51d93
SR
2466 if (iter->flags & FTRACE_ITER_FILTER &&
2467 ftrace_hash_empty(ops->filter_hash)) {
0c75a3ed 2468 if (*pos > 0)
8fc0c701 2469 return t_hash_start(m, pos);
0c75a3ed 2470 iter->flags |= FTRACE_ITER_PRINTALL;
df091625
CW
2471 /* reset in case of seek/pread */
2472 iter->flags &= ~FTRACE_ITER_HASH;
0c75a3ed
SR
2473 return iter;
2474 }
2475
8fc0c701
SR
2476 if (iter->flags & FTRACE_ITER_HASH)
2477 return t_hash_start(m, pos);
2478
98c4fd04
SR
2479 /*
2480 * Unfortunately, we need to restart at ftrace_pages_start
2481 * every time we let go of the ftrace_mutex. This is because
2482 * those pointers can change without the lock.
2483 */
694ce0a5
LZ
2484 iter->pg = ftrace_pages_start;
2485 iter->idx = 0;
2486 for (l = 0; l <= *pos; ) {
2487 p = t_next(m, p, &l);
2488 if (!p)
2489 break;
50cdaf08 2490 }
5821e1b7 2491
69a3083c
SR
2492 if (!p)
2493 return t_hash_start(m, pos);
4aeb6967
SR
2494
2495 return iter;
5072c59f
SR
2496}
2497
2498static void t_stop(struct seq_file *m, void *p)
2499{
8fc0c701 2500 mutex_unlock(&ftrace_lock);
5072c59f
SR
2501}
2502
2503static int t_show(struct seq_file *m, void *v)
2504{
0c75a3ed 2505 struct ftrace_iterator *iter = m->private;
4aeb6967 2506 struct dyn_ftrace *rec;
5072c59f 2507
8fc0c701 2508 if (iter->flags & FTRACE_ITER_HASH)
4aeb6967 2509 return t_hash_show(m, iter);
8fc0c701 2510
0c75a3ed
SR
2511 if (iter->flags & FTRACE_ITER_PRINTALL) {
2512 seq_printf(m, "#### all functions enabled ####\n");
2513 return 0;
2514 }
2515
4aeb6967
SR
2516 rec = iter->func;
2517
5072c59f
SR
2518 if (!rec)
2519 return 0;
2520
647bcd03
SR
2521 seq_printf(m, "%ps", (void *)rec->ip);
2522 if (iter->flags & FTRACE_ITER_ENABLED)
08f6fba5
SR
2523 seq_printf(m, " (%ld)%s",
2524 rec->flags & ~FTRACE_FL_MASK,
2525 rec->flags & FTRACE_FL_REGS ? " R" : "");
647bcd03 2526 seq_printf(m, "\n");
5072c59f
SR
2527
2528 return 0;
2529}
2530
88e9d34c 2531static const struct seq_operations show_ftrace_seq_ops = {
5072c59f
SR
2532 .start = t_start,
2533 .next = t_next,
2534 .stop = t_stop,
2535 .show = t_show,
2536};
2537
e309b41d 2538static int
5072c59f
SR
2539ftrace_avail_open(struct inode *inode, struct file *file)
2540{
2541 struct ftrace_iterator *iter;
5072c59f 2542
4eebcc81
SR
2543 if (unlikely(ftrace_disabled))
2544 return -ENODEV;
2545
50e18b94
JO
2546 iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter));
2547 if (iter) {
2548 iter->pg = ftrace_pages_start;
2549 iter->ops = &global_ops;
4bf39a94 2550 }
5072c59f 2551
50e18b94 2552 return iter ? 0 : -ENOMEM;
5072c59f
SR
2553}
2554
647bcd03
SR
2555static int
2556ftrace_enabled_open(struct inode *inode, struct file *file)
2557{
2558 struct ftrace_iterator *iter;
647bcd03
SR
2559
2560 if (unlikely(ftrace_disabled))
2561 return -ENODEV;
2562
50e18b94
JO
2563 iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter));
2564 if (iter) {
2565 iter->pg = ftrace_pages_start;
2566 iter->flags = FTRACE_ITER_ENABLED;
2567 iter->ops = &global_ops;
647bcd03
SR
2568 }
2569
50e18b94 2570 return iter ? 0 : -ENOMEM;
647bcd03
SR
2571}
2572
1cf41dd7 2573static void ftrace_filter_reset(struct ftrace_hash *hash)
5072c59f 2574{
52baf119 2575 mutex_lock(&ftrace_lock);
1cf41dd7 2576 ftrace_hash_clear(hash);
52baf119 2577 mutex_unlock(&ftrace_lock);
5072c59f
SR
2578}
2579
fc13cb0c
SR
2580/**
2581 * ftrace_regex_open - initialize function tracer filter files
2582 * @ops: The ftrace_ops that hold the hash filters
2583 * @flag: The type of filter to process
2584 * @inode: The inode, usually passed in to your open routine
2585 * @file: The file, usually passed in to your open routine
2586 *
2587 * ftrace_regex_open() initializes the filter files for the
2588 * @ops. Depending on @flag it may process the filter hash or
2589 * the notrace hash of @ops. With this called from the open
2590 * routine, you can use ftrace_filter_write() for the write
2591 * routine if @flag has FTRACE_ITER_FILTER set, or
2592 * ftrace_notrace_write() if @flag has FTRACE_ITER_NOTRACE set.
2593 * ftrace_regex_lseek() should be used as the lseek routine, and
2594 * release must call ftrace_regex_release().
2595 */
2596int
f45948e8 2597ftrace_regex_open(struct ftrace_ops *ops, int flag,
1cf41dd7 2598 struct inode *inode, struct file *file)
5072c59f
SR
2599{
2600 struct ftrace_iterator *iter;
f45948e8 2601 struct ftrace_hash *hash;
5072c59f
SR
2602 int ret = 0;
2603
4eebcc81
SR
2604 if (unlikely(ftrace_disabled))
2605 return -ENODEV;
2606
5072c59f
SR
2607 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2608 if (!iter)
2609 return -ENOMEM;
2610
689fd8b6 2611 if (trace_parser_get_init(&iter->parser, FTRACE_BUFF_MAX)) {
2612 kfree(iter);
2613 return -ENOMEM;
2614 }
2615
f45948e8
SR
2616 if (flag & FTRACE_ITER_NOTRACE)
2617 hash = ops->notrace_hash;
2618 else
2619 hash = ops->filter_hash;
2620
33dc9b12
SR
2621 iter->ops = ops;
2622 iter->flags = flag;
2623
2624 if (file->f_mode & FMODE_WRITE) {
2625 mutex_lock(&ftrace_lock);
2626 iter->hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, hash);
2627 mutex_unlock(&ftrace_lock);
2628
2629 if (!iter->hash) {
2630 trace_parser_put(&iter->parser);
2631 kfree(iter);
2632 return -ENOMEM;
2633 }
2634 }
1cf41dd7 2635
41c52c0d 2636 mutex_lock(&ftrace_regex_lock);
33dc9b12 2637
5072c59f 2638 if ((file->f_mode & FMODE_WRITE) &&
8650ae32 2639 (file->f_flags & O_TRUNC))
33dc9b12 2640 ftrace_filter_reset(iter->hash);
5072c59f
SR
2641
2642 if (file->f_mode & FMODE_READ) {
2643 iter->pg = ftrace_pages_start;
5072c59f
SR
2644
2645 ret = seq_open(file, &show_ftrace_seq_ops);
2646 if (!ret) {
2647 struct seq_file *m = file->private_data;
2648 m->private = iter;
79fe249c 2649 } else {
33dc9b12
SR
2650 /* Failed */
2651 free_ftrace_hash(iter->hash);
79fe249c 2652 trace_parser_put(&iter->parser);
5072c59f 2653 kfree(iter);
79fe249c 2654 }
5072c59f
SR
2655 } else
2656 file->private_data = iter;
41c52c0d 2657 mutex_unlock(&ftrace_regex_lock);
5072c59f
SR
2658
2659 return ret;
2660}
2661
41c52c0d
SR
2662static int
2663ftrace_filter_open(struct inode *inode, struct file *file)
2664{
69a3083c
SR
2665 return ftrace_regex_open(&global_ops,
2666 FTRACE_ITER_FILTER | FTRACE_ITER_DO_HASH,
2667 inode, file);
41c52c0d
SR
2668}
2669
2670static int
2671ftrace_notrace_open(struct inode *inode, struct file *file)
2672{
f45948e8 2673 return ftrace_regex_open(&global_ops, FTRACE_ITER_NOTRACE,
1cf41dd7 2674 inode, file);
41c52c0d
SR
2675}
2676
fc13cb0c 2677loff_t
41c52c0d 2678ftrace_regex_lseek(struct file *file, loff_t offset, int origin)
5072c59f
SR
2679{
2680 loff_t ret;
2681
2682 if (file->f_mode & FMODE_READ)
2683 ret = seq_lseek(file, offset, origin);
2684 else
2685 file->f_pos = ret = 1;
2686
2687 return ret;
2688}
2689
64e7c440 2690static int ftrace_match(char *str, char *regex, int len, int type)
9f4801e3 2691{
9f4801e3 2692 int matched = 0;
751e9983 2693 int slen;
9f4801e3 2694
9f4801e3
SR
2695 switch (type) {
2696 case MATCH_FULL:
2697 if (strcmp(str, regex) == 0)
2698 matched = 1;
2699 break;
2700 case MATCH_FRONT_ONLY:
2701 if (strncmp(str, regex, len) == 0)
2702 matched = 1;
2703 break;
2704 case MATCH_MIDDLE_ONLY:
2705 if (strstr(str, regex))
2706 matched = 1;
2707 break;
2708 case MATCH_END_ONLY:
751e9983
LZ
2709 slen = strlen(str);
2710 if (slen >= len && memcmp(str + slen - len, regex, len) == 0)
9f4801e3
SR
2711 matched = 1;
2712 break;
2713 }
2714
2715 return matched;
2716}
2717
b448c4e3 2718static int
1cf41dd7 2719enter_record(struct ftrace_hash *hash, struct dyn_ftrace *rec, int not)
996e87be 2720{
b448c4e3 2721 struct ftrace_func_entry *entry;
b448c4e3
SR
2722 int ret = 0;
2723
1cf41dd7
SR
2724 entry = ftrace_lookup_ip(hash, rec->ip);
2725 if (not) {
2726 /* Do nothing if it doesn't exist */
2727 if (!entry)
2728 return 0;
b448c4e3 2729
33dc9b12 2730 free_hash_entry(hash, entry);
1cf41dd7
SR
2731 } else {
2732 /* Do nothing if it exists */
2733 if (entry)
2734 return 0;
b448c4e3 2735
1cf41dd7 2736 ret = add_hash_entry(hash, rec->ip);
b448c4e3
SR
2737 }
2738 return ret;
996e87be
SR
2739}
2740
64e7c440 2741static int
b9df92d2
SR
2742ftrace_match_record(struct dyn_ftrace *rec, char *mod,
2743 char *regex, int len, int type)
64e7c440
SR
2744{
2745 char str[KSYM_SYMBOL_LEN];
b9df92d2
SR
2746 char *modname;
2747
2748 kallsyms_lookup(rec->ip, NULL, NULL, &modname, str);
2749
2750 if (mod) {
2751 /* module lookup requires matching the module */
2752 if (!modname || strcmp(modname, mod))
2753 return 0;
2754
2755 /* blank search means to match all funcs in the mod */
2756 if (!len)
2757 return 1;
2758 }
64e7c440 2759
64e7c440
SR
2760 return ftrace_match(str, regex, len, type);
2761}
2762
1cf41dd7
SR
2763static int
2764match_records(struct ftrace_hash *hash, char *buff,
2765 int len, char *mod, int not)
9f4801e3 2766{
b9df92d2 2767 unsigned search_len = 0;
9f4801e3
SR
2768 struct ftrace_page *pg;
2769 struct dyn_ftrace *rec;
b9df92d2
SR
2770 int type = MATCH_FULL;
2771 char *search = buff;
311d16da 2772 int found = 0;
b448c4e3 2773 int ret;
9f4801e3 2774
b9df92d2
SR
2775 if (len) {
2776 type = filter_parse_regex(buff, len, &search, &not);
2777 search_len = strlen(search);
2778 }
9f4801e3 2779
52baf119 2780 mutex_lock(&ftrace_lock);
265c831c 2781
b9df92d2
SR
2782 if (unlikely(ftrace_disabled))
2783 goto out_unlock;
9f4801e3 2784
265c831c 2785 do_for_each_ftrace_rec(pg, rec) {
b9df92d2 2786 if (ftrace_match_record(rec, mod, search, search_len, type)) {
1cf41dd7 2787 ret = enter_record(hash, rec, not);
b448c4e3
SR
2788 if (ret < 0) {
2789 found = ret;
2790 goto out_unlock;
2791 }
311d16da 2792 found = 1;
265c831c
SR
2793 }
2794 } while_for_each_ftrace_rec();
b9df92d2 2795 out_unlock:
52baf119 2796 mutex_unlock(&ftrace_lock);
311d16da
LZ
2797
2798 return found;
5072c59f
SR
2799}
2800
64e7c440 2801static int
1cf41dd7 2802ftrace_match_records(struct ftrace_hash *hash, char *buff, int len)
64e7c440 2803{
1cf41dd7 2804 return match_records(hash, buff, len, NULL, 0);
64e7c440
SR
2805}
2806
1cf41dd7
SR
2807static int
2808ftrace_match_module_records(struct ftrace_hash *hash, char *buff, char *mod)
64e7c440 2809{
64e7c440 2810 int not = 0;
6a24a244 2811
64e7c440
SR
2812 /* blank or '*' mean the same */
2813 if (strcmp(buff, "*") == 0)
2814 buff[0] = 0;
2815
2816 /* handle the case of 'dont filter this module' */
2817 if (strcmp(buff, "!") == 0 || strcmp(buff, "!*") == 0) {
2818 buff[0] = 0;
2819 not = 1;
2820 }
2821
1cf41dd7 2822 return match_records(hash, buff, strlen(buff), mod, not);
64e7c440
SR
2823}
2824
f6180773
SR
2825/*
2826 * We register the module command as a template to show others how
2827 * to register the a command as well.
2828 */
2829
2830static int
43dd61c9
SR
2831ftrace_mod_callback(struct ftrace_hash *hash,
2832 char *func, char *cmd, char *param, int enable)
f6180773
SR
2833{
2834 char *mod;
b448c4e3 2835 int ret = -EINVAL;
f6180773
SR
2836
2837 /*
2838 * cmd == 'mod' because we only registered this func
2839 * for the 'mod' ftrace_func_command.
2840 * But if you register one func with multiple commands,
2841 * you can tell which command was used by the cmd
2842 * parameter.
2843 */
2844
2845 /* we must have a module name */
2846 if (!param)
b448c4e3 2847 return ret;
f6180773
SR
2848
2849 mod = strsep(&param, ":");
2850 if (!strlen(mod))
b448c4e3 2851 return ret;
f6180773 2852
1cf41dd7 2853 ret = ftrace_match_module_records(hash, func, mod);
b448c4e3
SR
2854 if (!ret)
2855 ret = -EINVAL;
2856 if (ret < 0)
2857 return ret;
2858
2859 return 0;
f6180773
SR
2860}
2861
2862static struct ftrace_func_command ftrace_mod_cmd = {
2863 .name = "mod",
2864 .func = ftrace_mod_callback,
2865};
2866
2867static int __init ftrace_mod_cmd_init(void)
2868{
2869 return register_ftrace_command(&ftrace_mod_cmd);
2870}
2871device_initcall(ftrace_mod_cmd_init);
2872
2f5f6ad9 2873static void function_trace_probe_call(unsigned long ip, unsigned long parent_ip,
a1e2e31d 2874 struct ftrace_ops *op, struct pt_regs *pt_regs)
59df055f 2875{
b6887d79 2876 struct ftrace_func_probe *entry;
59df055f
SR
2877 struct hlist_head *hhd;
2878 struct hlist_node *n;
2879 unsigned long key;
59df055f
SR
2880
2881 key = hash_long(ip, FTRACE_HASH_BITS);
2882
2883 hhd = &ftrace_func_hash[key];
2884
2885 if (hlist_empty(hhd))
2886 return;
2887
2888 /*
2889 * Disable preemption for these calls to prevent a RCU grace
2890 * period. This syncs the hash iteration and freeing of items
2891 * on the hash. rcu_read_lock is too dangerous here.
2892 */
5168ae50 2893 preempt_disable_notrace();
59df055f
SR
2894 hlist_for_each_entry_rcu(entry, n, hhd, node) {
2895 if (entry->ip == ip)
2896 entry->ops->func(ip, parent_ip, &entry->data);
2897 }
5168ae50 2898 preempt_enable_notrace();
59df055f
SR
2899}
2900
b6887d79 2901static struct ftrace_ops trace_probe_ops __read_mostly =
59df055f 2902{
fb9fb015 2903 .func = function_trace_probe_call,
59df055f
SR
2904};
2905
b6887d79 2906static int ftrace_probe_registered;
59df055f 2907
b6887d79 2908static void __enable_ftrace_function_probe(void)
59df055f 2909{
b848914c 2910 int ret;
59df055f
SR
2911 int i;
2912
b6887d79 2913 if (ftrace_probe_registered)
59df055f
SR
2914 return;
2915
2916 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
2917 struct hlist_head *hhd = &ftrace_func_hash[i];
2918 if (hhd->first)
2919 break;
2920 }
2921 /* Nothing registered? */
2922 if (i == FTRACE_FUNC_HASHSIZE)
2923 return;
2924
b848914c
SR
2925 ret = __register_ftrace_function(&trace_probe_ops);
2926 if (!ret)
a1cd6173 2927 ret = ftrace_startup(&trace_probe_ops, 0);
b848914c 2928
b6887d79 2929 ftrace_probe_registered = 1;
59df055f
SR
2930}
2931
b6887d79 2932static void __disable_ftrace_function_probe(void)
59df055f 2933{
b848914c 2934 int ret;
59df055f
SR
2935 int i;
2936
b6887d79 2937 if (!ftrace_probe_registered)
59df055f
SR
2938 return;
2939
2940 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
2941 struct hlist_head *hhd = &ftrace_func_hash[i];
2942 if (hhd->first)
2943 return;
2944 }
2945
2946 /* no more funcs left */
b848914c
SR
2947 ret = __unregister_ftrace_function(&trace_probe_ops);
2948 if (!ret)
2949 ftrace_shutdown(&trace_probe_ops, 0);
2950
b6887d79 2951 ftrace_probe_registered = 0;
59df055f
SR
2952}
2953
2954
2955static void ftrace_free_entry_rcu(struct rcu_head *rhp)
2956{
b6887d79
SR
2957 struct ftrace_func_probe *entry =
2958 container_of(rhp, struct ftrace_func_probe, rcu);
59df055f
SR
2959
2960 if (entry->ops->free)
2961 entry->ops->free(&entry->data);
2962 kfree(entry);
2963}
2964
2965
2966int
b6887d79 2967register_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
59df055f
SR
2968 void *data)
2969{
b6887d79 2970 struct ftrace_func_probe *entry;
59df055f
SR
2971 struct ftrace_page *pg;
2972 struct dyn_ftrace *rec;
59df055f 2973 int type, len, not;
6a24a244 2974 unsigned long key;
59df055f
SR
2975 int count = 0;
2976 char *search;
2977
3f6fe06d 2978 type = filter_parse_regex(glob, strlen(glob), &search, &not);
59df055f
SR
2979 len = strlen(search);
2980
b6887d79 2981 /* we do not support '!' for function probes */
59df055f
SR
2982 if (WARN_ON(not))
2983 return -EINVAL;
2984
2985 mutex_lock(&ftrace_lock);
59df055f 2986
45a4a237
SR
2987 if (unlikely(ftrace_disabled))
2988 goto out_unlock;
59df055f 2989
45a4a237 2990 do_for_each_ftrace_rec(pg, rec) {
59df055f 2991
b9df92d2 2992 if (!ftrace_match_record(rec, NULL, search, len, type))
59df055f
SR
2993 continue;
2994
2995 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
2996 if (!entry) {
b6887d79 2997 /* If we did not process any, then return error */
59df055f
SR
2998 if (!count)
2999 count = -ENOMEM;
3000 goto out_unlock;
3001 }
3002
3003 count++;
3004
3005 entry->data = data;
3006
3007 /*
3008 * The caller might want to do something special
3009 * for each function we find. We call the callback
3010 * to give the caller an opportunity to do so.
3011 */
3012 if (ops->callback) {
3013 if (ops->callback(rec->ip, &entry->data) < 0) {
3014 /* caller does not like this func */
3015 kfree(entry);
3016 continue;
3017 }
3018 }
3019
3020 entry->ops = ops;
3021 entry->ip = rec->ip;
3022
3023 key = hash_long(entry->ip, FTRACE_HASH_BITS);
3024 hlist_add_head_rcu(&entry->node, &ftrace_func_hash[key]);
3025
3026 } while_for_each_ftrace_rec();
b6887d79 3027 __enable_ftrace_function_probe();
59df055f
SR
3028
3029 out_unlock:
3030 mutex_unlock(&ftrace_lock);
3031
3032 return count;
3033}
3034
3035enum {
b6887d79
SR
3036 PROBE_TEST_FUNC = 1,
3037 PROBE_TEST_DATA = 2
59df055f
SR
3038};
3039
3040static void
b6887d79 3041__unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
59df055f
SR
3042 void *data, int flags)
3043{
b6887d79 3044 struct ftrace_func_probe *entry;
59df055f
SR
3045 struct hlist_node *n, *tmp;
3046 char str[KSYM_SYMBOL_LEN];
3047 int type = MATCH_FULL;
3048 int i, len = 0;
3049 char *search;
3050
b36461da 3051 if (glob && (strcmp(glob, "*") == 0 || !strlen(glob)))
59df055f 3052 glob = NULL;
b36461da 3053 else if (glob) {
59df055f
SR
3054 int not;
3055
3f6fe06d 3056 type = filter_parse_regex(glob, strlen(glob), &search, &not);
59df055f
SR
3057 len = strlen(search);
3058
b6887d79 3059 /* we do not support '!' for function probes */
59df055f
SR
3060 if (WARN_ON(not))
3061 return;
3062 }
3063
3064 mutex_lock(&ftrace_lock);
3065 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
3066 struct hlist_head *hhd = &ftrace_func_hash[i];
3067
3068 hlist_for_each_entry_safe(entry, n, tmp, hhd, node) {
3069
3070 /* break up if statements for readability */
b6887d79 3071 if ((flags & PROBE_TEST_FUNC) && entry->ops != ops)
59df055f
SR
3072 continue;
3073
b6887d79 3074 if ((flags & PROBE_TEST_DATA) && entry->data != data)
59df055f
SR
3075 continue;
3076
3077 /* do this last, since it is the most expensive */
3078 if (glob) {
3079 kallsyms_lookup(entry->ip, NULL, NULL,
3080 NULL, str);
3081 if (!ftrace_match(str, glob, len, type))
3082 continue;
3083 }
3084
3085 hlist_del(&entry->node);
3086 call_rcu(&entry->rcu, ftrace_free_entry_rcu);
3087 }
3088 }
b6887d79 3089 __disable_ftrace_function_probe();
59df055f
SR
3090 mutex_unlock(&ftrace_lock);
3091}
3092
3093void
b6887d79 3094unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
59df055f
SR
3095 void *data)
3096{
b6887d79
SR
3097 __unregister_ftrace_function_probe(glob, ops, data,
3098 PROBE_TEST_FUNC | PROBE_TEST_DATA);
59df055f
SR
3099}
3100
3101void
b6887d79 3102unregister_ftrace_function_probe_func(char *glob, struct ftrace_probe_ops *ops)
59df055f 3103{
b6887d79 3104 __unregister_ftrace_function_probe(glob, ops, NULL, PROBE_TEST_FUNC);
59df055f
SR
3105}
3106
b6887d79 3107void unregister_ftrace_function_probe_all(char *glob)
59df055f 3108{
b6887d79 3109 __unregister_ftrace_function_probe(glob, NULL, NULL, 0);
59df055f
SR
3110}
3111
f6180773
SR
3112static LIST_HEAD(ftrace_commands);
3113static DEFINE_MUTEX(ftrace_cmd_mutex);
3114
3115int register_ftrace_command(struct ftrace_func_command *cmd)
3116{
3117 struct ftrace_func_command *p;
3118 int ret = 0;
3119
3120 mutex_lock(&ftrace_cmd_mutex);
3121 list_for_each_entry(p, &ftrace_commands, list) {
3122 if (strcmp(cmd->name, p->name) == 0) {
3123 ret = -EBUSY;
3124 goto out_unlock;
3125 }
3126 }
3127 list_add(&cmd->list, &ftrace_commands);
3128 out_unlock:
3129 mutex_unlock(&ftrace_cmd_mutex);
3130
3131 return ret;
3132}
3133
3134int unregister_ftrace_command(struct ftrace_func_command *cmd)
3135{
3136 struct ftrace_func_command *p, *n;
3137 int ret = -ENODEV;
3138
3139 mutex_lock(&ftrace_cmd_mutex);
3140 list_for_each_entry_safe(p, n, &ftrace_commands, list) {
3141 if (strcmp(cmd->name, p->name) == 0) {
3142 ret = 0;
3143 list_del_init(&p->list);
3144 goto out_unlock;
3145 }
3146 }
3147 out_unlock:
3148 mutex_unlock(&ftrace_cmd_mutex);
3149
3150 return ret;
3151}
3152
33dc9b12
SR
3153static int ftrace_process_regex(struct ftrace_hash *hash,
3154 char *buff, int len, int enable)
64e7c440 3155{
f6180773 3156 char *func, *command, *next = buff;
6a24a244 3157 struct ftrace_func_command *p;
0aff1c0c 3158 int ret = -EINVAL;
64e7c440
SR
3159
3160 func = strsep(&next, ":");
3161
3162 if (!next) {
1cf41dd7 3163 ret = ftrace_match_records(hash, func, len);
b448c4e3
SR
3164 if (!ret)
3165 ret = -EINVAL;
3166 if (ret < 0)
3167 return ret;
3168 return 0;
64e7c440
SR
3169 }
3170
f6180773 3171 /* command found */
64e7c440
SR
3172
3173 command = strsep(&next, ":");
3174
f6180773
SR
3175 mutex_lock(&ftrace_cmd_mutex);
3176 list_for_each_entry(p, &ftrace_commands, list) {
3177 if (strcmp(p->name, command) == 0) {
43dd61c9 3178 ret = p->func(hash, func, command, next, enable);
f6180773
SR
3179 goto out_unlock;
3180 }
64e7c440 3181 }
f6180773
SR
3182 out_unlock:
3183 mutex_unlock(&ftrace_cmd_mutex);
64e7c440 3184
f6180773 3185 return ret;
64e7c440
SR
3186}
3187
e309b41d 3188static ssize_t
41c52c0d
SR
3189ftrace_regex_write(struct file *file, const char __user *ubuf,
3190 size_t cnt, loff_t *ppos, int enable)
5072c59f
SR
3191{
3192 struct ftrace_iterator *iter;
689fd8b6 3193 struct trace_parser *parser;
3194 ssize_t ret, read;
5072c59f 3195
4ba7978e 3196 if (!cnt)
5072c59f
SR
3197 return 0;
3198
41c52c0d 3199 mutex_lock(&ftrace_regex_lock);
5072c59f 3200
45a4a237
SR
3201 ret = -ENODEV;
3202 if (unlikely(ftrace_disabled))
3203 goto out_unlock;
3204
5072c59f
SR
3205 if (file->f_mode & FMODE_READ) {
3206 struct seq_file *m = file->private_data;
3207 iter = m->private;
3208 } else
3209 iter = file->private_data;
3210
689fd8b6 3211 parser = &iter->parser;
3212 read = trace_get_user(parser, ubuf, cnt, ppos);
5072c59f 3213
4ba7978e 3214 if (read >= 0 && trace_parser_loaded(parser) &&
689fd8b6 3215 !trace_parser_cont(parser)) {
33dc9b12 3216 ret = ftrace_process_regex(iter->hash, parser->buffer,
689fd8b6 3217 parser->idx, enable);
313254a9 3218 trace_parser_clear(parser);
5072c59f 3219 if (ret)
ed146b25 3220 goto out_unlock;
eda1e328 3221 }
5072c59f 3222
5072c59f 3223 ret = read;
ed146b25 3224out_unlock:
689fd8b6 3225 mutex_unlock(&ftrace_regex_lock);
ed146b25 3226
5072c59f
SR
3227 return ret;
3228}
3229
fc13cb0c 3230ssize_t
41c52c0d
SR
3231ftrace_filter_write(struct file *file, const char __user *ubuf,
3232 size_t cnt, loff_t *ppos)
3233{
3234 return ftrace_regex_write(file, ubuf, cnt, ppos, 1);
3235}
3236
fc13cb0c 3237ssize_t
41c52c0d
SR
3238ftrace_notrace_write(struct file *file, const char __user *ubuf,
3239 size_t cnt, loff_t *ppos)
3240{
3241 return ftrace_regex_write(file, ubuf, cnt, ppos, 0);
3242}
3243
33dc9b12 3244static int
f45948e8
SR
3245ftrace_set_regex(struct ftrace_ops *ops, unsigned char *buf, int len,
3246 int reset, int enable)
41c52c0d 3247{
33dc9b12 3248 struct ftrace_hash **orig_hash;
f45948e8 3249 struct ftrace_hash *hash;
33dc9b12 3250 int ret;
f45948e8 3251
936e074b
SR
3252 /* All global ops uses the global ops filters */
3253 if (ops->flags & FTRACE_OPS_FL_GLOBAL)
3254 ops = &global_ops;
3255
41c52c0d 3256 if (unlikely(ftrace_disabled))
33dc9b12 3257 return -ENODEV;
41c52c0d 3258
f45948e8 3259 if (enable)
33dc9b12 3260 orig_hash = &ops->filter_hash;
f45948e8 3261 else
33dc9b12
SR
3262 orig_hash = &ops->notrace_hash;
3263
3264 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
3265 if (!hash)
3266 return -ENOMEM;
f45948e8 3267
41c52c0d
SR
3268 mutex_lock(&ftrace_regex_lock);
3269 if (reset)
1cf41dd7 3270 ftrace_filter_reset(hash);
ac483c44
JO
3271 if (buf && !ftrace_match_records(hash, buf, len)) {
3272 ret = -EINVAL;
3273 goto out_regex_unlock;
3274 }
33dc9b12
SR
3275
3276 mutex_lock(&ftrace_lock);
41fb61c2 3277 ret = ftrace_hash_move(ops, enable, orig_hash, hash);
072126f4
SR
3278 if (!ret && ops->flags & FTRACE_OPS_FL_ENABLED
3279 && ftrace_enabled)
30fb6aa7 3280 ftrace_run_update_code(FTRACE_UPDATE_CALLS);
072126f4 3281
33dc9b12
SR
3282 mutex_unlock(&ftrace_lock);
3283
ac483c44 3284 out_regex_unlock:
41c52c0d 3285 mutex_unlock(&ftrace_regex_lock);
33dc9b12
SR
3286
3287 free_ftrace_hash(hash);
3288 return ret;
41c52c0d
SR
3289}
3290
77a2b37d
SR
3291/**
3292 * ftrace_set_filter - set a function to filter on in ftrace
936e074b
SR
3293 * @ops - the ops to set the filter with
3294 * @buf - the string that holds the function filter text.
3295 * @len - the length of the string.
3296 * @reset - non zero to reset all filters before applying this filter.
3297 *
3298 * Filters denote which functions should be enabled when tracing is enabled.
3299 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
3300 */
ac483c44 3301int ftrace_set_filter(struct ftrace_ops *ops, unsigned char *buf,
936e074b
SR
3302 int len, int reset)
3303{
ac483c44 3304 return ftrace_set_regex(ops, buf, len, reset, 1);
936e074b
SR
3305}
3306EXPORT_SYMBOL_GPL(ftrace_set_filter);
3307
3308/**
3309 * ftrace_set_notrace - set a function to not trace in ftrace
3310 * @ops - the ops to set the notrace filter with
3311 * @buf - the string that holds the function notrace text.
3312 * @len - the length of the string.
3313 * @reset - non zero to reset all filters before applying this filter.
3314 *
3315 * Notrace Filters denote which functions should not be enabled when tracing
3316 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
3317 * for tracing.
3318 */
ac483c44 3319int ftrace_set_notrace(struct ftrace_ops *ops, unsigned char *buf,
936e074b
SR
3320 int len, int reset)
3321{
ac483c44 3322 return ftrace_set_regex(ops, buf, len, reset, 0);
936e074b
SR
3323}
3324EXPORT_SYMBOL_GPL(ftrace_set_notrace);
3325/**
3326 * ftrace_set_filter - set a function to filter on in ftrace
3327 * @ops - the ops to set the filter with
77a2b37d
SR
3328 * @buf - the string that holds the function filter text.
3329 * @len - the length of the string.
3330 * @reset - non zero to reset all filters before applying this filter.
3331 *
3332 * Filters denote which functions should be enabled when tracing is enabled.
3333 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
3334 */
936e074b 3335void ftrace_set_global_filter(unsigned char *buf, int len, int reset)
77a2b37d 3336{
f45948e8 3337 ftrace_set_regex(&global_ops, buf, len, reset, 1);
41c52c0d 3338}
936e074b 3339EXPORT_SYMBOL_GPL(ftrace_set_global_filter);
4eebcc81 3340
41c52c0d
SR
3341/**
3342 * ftrace_set_notrace - set a function to not trace in ftrace
936e074b 3343 * @ops - the ops to set the notrace filter with
41c52c0d
SR
3344 * @buf - the string that holds the function notrace text.
3345 * @len - the length of the string.
3346 * @reset - non zero to reset all filters before applying this filter.
3347 *
3348 * Notrace Filters denote which functions should not be enabled when tracing
3349 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
3350 * for tracing.
3351 */
936e074b 3352void ftrace_set_global_notrace(unsigned char *buf, int len, int reset)
41c52c0d 3353{
f45948e8 3354 ftrace_set_regex(&global_ops, buf, len, reset, 0);
77a2b37d 3355}
936e074b 3356EXPORT_SYMBOL_GPL(ftrace_set_global_notrace);
77a2b37d 3357
2af15d6a
SR
3358/*
3359 * command line interface to allow users to set filters on boot up.
3360 */
3361#define FTRACE_FILTER_SIZE COMMAND_LINE_SIZE
3362static char ftrace_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
3363static char ftrace_filter_buf[FTRACE_FILTER_SIZE] __initdata;
3364
3365static int __init set_ftrace_notrace(char *str)
3366{
3367 strncpy(ftrace_notrace_buf, str, FTRACE_FILTER_SIZE);
3368 return 1;
3369}
3370__setup("ftrace_notrace=", set_ftrace_notrace);
3371
3372static int __init set_ftrace_filter(char *str)
3373{
3374 strncpy(ftrace_filter_buf, str, FTRACE_FILTER_SIZE);
3375 return 1;
3376}
3377__setup("ftrace_filter=", set_ftrace_filter);
3378
369bc18f 3379#ifdef CONFIG_FUNCTION_GRAPH_TRACER
f6060f46 3380static char ftrace_graph_buf[FTRACE_FILTER_SIZE] __initdata;
801c29fd
SR
3381static int ftrace_set_func(unsigned long *array, int *idx, char *buffer);
3382
369bc18f
SA
3383static int __init set_graph_function(char *str)
3384{
06f43d66 3385 strlcpy(ftrace_graph_buf, str, FTRACE_FILTER_SIZE);
369bc18f
SA
3386 return 1;
3387}
3388__setup("ftrace_graph_filter=", set_graph_function);
3389
3390static void __init set_ftrace_early_graph(char *buf)
3391{
3392 int ret;
3393 char *func;
3394
3395 while (buf) {
3396 func = strsep(&buf, ",");
3397 /* we allow only one expression at a time */
3398 ret = ftrace_set_func(ftrace_graph_funcs, &ftrace_graph_count,
3399 func);
3400 if (ret)
3401 printk(KERN_DEBUG "ftrace: function %s not "
3402 "traceable\n", func);
3403 }
3404}
3405#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
3406
2a85a37f
SR
3407void __init
3408ftrace_set_early_filter(struct ftrace_ops *ops, char *buf, int enable)
2af15d6a
SR
3409{
3410 char *func;
3411
3412 while (buf) {
3413 func = strsep(&buf, ",");
f45948e8 3414 ftrace_set_regex(ops, func, strlen(func), 0, enable);
2af15d6a
SR
3415 }
3416}
3417
3418static void __init set_ftrace_early_filters(void)
3419{
3420 if (ftrace_filter_buf[0])
2a85a37f 3421 ftrace_set_early_filter(&global_ops, ftrace_filter_buf, 1);
2af15d6a 3422 if (ftrace_notrace_buf[0])
2a85a37f 3423 ftrace_set_early_filter(&global_ops, ftrace_notrace_buf, 0);
369bc18f
SA
3424#ifdef CONFIG_FUNCTION_GRAPH_TRACER
3425 if (ftrace_graph_buf[0])
3426 set_ftrace_early_graph(ftrace_graph_buf);
3427#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
2af15d6a
SR
3428}
3429
fc13cb0c 3430int ftrace_regex_release(struct inode *inode, struct file *file)
5072c59f
SR
3431{
3432 struct seq_file *m = (struct seq_file *)file->private_data;
3433 struct ftrace_iterator *iter;
33dc9b12 3434 struct ftrace_hash **orig_hash;
689fd8b6 3435 struct trace_parser *parser;
ed926f9b 3436 int filter_hash;
33dc9b12 3437 int ret;
5072c59f 3438
41c52c0d 3439 mutex_lock(&ftrace_regex_lock);
5072c59f
SR
3440 if (file->f_mode & FMODE_READ) {
3441 iter = m->private;
3442
3443 seq_release(inode, file);
3444 } else
3445 iter = file->private_data;
3446
689fd8b6 3447 parser = &iter->parser;
3448 if (trace_parser_loaded(parser)) {
3449 parser->buffer[parser->idx] = 0;
1cf41dd7 3450 ftrace_match_records(iter->hash, parser->buffer, parser->idx);
5072c59f
SR
3451 }
3452
689fd8b6 3453 trace_parser_put(parser);
689fd8b6 3454
058e297d 3455 if (file->f_mode & FMODE_WRITE) {
ed926f9b
SR
3456 filter_hash = !!(iter->flags & FTRACE_ITER_FILTER);
3457
3458 if (filter_hash)
33dc9b12 3459 orig_hash = &iter->ops->filter_hash;
ed926f9b
SR
3460 else
3461 orig_hash = &iter->ops->notrace_hash;
33dc9b12 3462
058e297d 3463 mutex_lock(&ftrace_lock);
41fb61c2
SR
3464 ret = ftrace_hash_move(iter->ops, filter_hash,
3465 orig_hash, iter->hash);
3466 if (!ret && (iter->ops->flags & FTRACE_OPS_FL_ENABLED)
3467 && ftrace_enabled)
30fb6aa7 3468 ftrace_run_update_code(FTRACE_UPDATE_CALLS);
41fb61c2 3469
058e297d
SR
3470 mutex_unlock(&ftrace_lock);
3471 }
33dc9b12
SR
3472 free_ftrace_hash(iter->hash);
3473 kfree(iter);
058e297d 3474
41c52c0d 3475 mutex_unlock(&ftrace_regex_lock);
5072c59f
SR
3476 return 0;
3477}
3478
5e2336a0 3479static const struct file_operations ftrace_avail_fops = {
5072c59f
SR
3480 .open = ftrace_avail_open,
3481 .read = seq_read,
3482 .llseek = seq_lseek,
3be04b47 3483 .release = seq_release_private,
5072c59f
SR
3484};
3485
647bcd03
SR
3486static const struct file_operations ftrace_enabled_fops = {
3487 .open = ftrace_enabled_open,
3488 .read = seq_read,
3489 .llseek = seq_lseek,
3490 .release = seq_release_private,
3491};
3492
5e2336a0 3493static const struct file_operations ftrace_filter_fops = {
5072c59f 3494 .open = ftrace_filter_open,
850a80cf 3495 .read = seq_read,
5072c59f 3496 .write = ftrace_filter_write,
98c4fd04 3497 .llseek = ftrace_regex_lseek,
1cf41dd7 3498 .release = ftrace_regex_release,
5072c59f
SR
3499};
3500
5e2336a0 3501static const struct file_operations ftrace_notrace_fops = {
41c52c0d 3502 .open = ftrace_notrace_open,
850a80cf 3503 .read = seq_read,
41c52c0d
SR
3504 .write = ftrace_notrace_write,
3505 .llseek = ftrace_regex_lseek,
1cf41dd7 3506 .release = ftrace_regex_release,
41c52c0d
SR
3507};
3508
ea4e2bc4
SR
3509#ifdef CONFIG_FUNCTION_GRAPH_TRACER
3510
3511static DEFINE_MUTEX(graph_lock);
3512
3513int ftrace_graph_count;
c7c6b1fe 3514int ftrace_graph_filter_enabled;
ea4e2bc4
SR
3515unsigned long ftrace_graph_funcs[FTRACE_GRAPH_MAX_FUNCS] __read_mostly;
3516
3517static void *
85951842 3518__g_next(struct seq_file *m, loff_t *pos)
ea4e2bc4 3519{
85951842 3520 if (*pos >= ftrace_graph_count)
ea4e2bc4 3521 return NULL;
a4ec5e0c 3522 return &ftrace_graph_funcs[*pos];
85951842 3523}
ea4e2bc4 3524
85951842
LZ
3525static void *
3526g_next(struct seq_file *m, void *v, loff_t *pos)
3527{
3528 (*pos)++;
3529 return __g_next(m, pos);
ea4e2bc4
SR
3530}
3531
3532static void *g_start(struct seq_file *m, loff_t *pos)
3533{
ea4e2bc4
SR
3534 mutex_lock(&graph_lock);
3535
f9349a8f 3536 /* Nothing, tell g_show to print all functions are enabled */
c7c6b1fe 3537 if (!ftrace_graph_filter_enabled && !*pos)
f9349a8f
FW
3538 return (void *)1;
3539
85951842 3540 return __g_next(m, pos);
ea4e2bc4
SR
3541}
3542
3543static void g_stop(struct seq_file *m, void *p)
3544{
3545 mutex_unlock(&graph_lock);
3546}
3547
3548static int g_show(struct seq_file *m, void *v)
3549{
3550 unsigned long *ptr = v;
ea4e2bc4
SR
3551
3552 if (!ptr)
3553 return 0;
3554
f9349a8f
FW
3555 if (ptr == (unsigned long *)1) {
3556 seq_printf(m, "#### all functions enabled ####\n");
3557 return 0;
3558 }
3559
b375a11a 3560 seq_printf(m, "%ps\n", (void *)*ptr);
ea4e2bc4
SR
3561
3562 return 0;
3563}
3564
88e9d34c 3565static const struct seq_operations ftrace_graph_seq_ops = {
ea4e2bc4
SR
3566 .start = g_start,
3567 .next = g_next,
3568 .stop = g_stop,
3569 .show = g_show,
3570};
3571
3572static int
3573ftrace_graph_open(struct inode *inode, struct file *file)
3574{
3575 int ret = 0;
3576
3577 if (unlikely(ftrace_disabled))
3578 return -ENODEV;
3579
3580 mutex_lock(&graph_lock);
3581 if ((file->f_mode & FMODE_WRITE) &&
8650ae32 3582 (file->f_flags & O_TRUNC)) {
c7c6b1fe 3583 ftrace_graph_filter_enabled = 0;
ea4e2bc4
SR
3584 ftrace_graph_count = 0;
3585 memset(ftrace_graph_funcs, 0, sizeof(ftrace_graph_funcs));
3586 }
a4ec5e0c 3587 mutex_unlock(&graph_lock);
ea4e2bc4 3588
a4ec5e0c 3589 if (file->f_mode & FMODE_READ)
ea4e2bc4 3590 ret = seq_open(file, &ftrace_graph_seq_ops);
ea4e2bc4
SR
3591
3592 return ret;
3593}
3594
87827111
LZ
3595static int
3596ftrace_graph_release(struct inode *inode, struct file *file)
3597{
3598 if (file->f_mode & FMODE_READ)
3599 seq_release(inode, file);
3600 return 0;
3601}
3602
ea4e2bc4 3603static int
f9349a8f 3604ftrace_set_func(unsigned long *array, int *idx, char *buffer)
ea4e2bc4 3605{
ea4e2bc4
SR
3606 struct dyn_ftrace *rec;
3607 struct ftrace_page *pg;
f9349a8f 3608 int search_len;
c7c6b1fe 3609 int fail = 1;
f9349a8f
FW
3610 int type, not;
3611 char *search;
3612 bool exists;
3613 int i;
ea4e2bc4 3614
f9349a8f 3615 /* decode regex */
3f6fe06d 3616 type = filter_parse_regex(buffer, strlen(buffer), &search, &not);
c7c6b1fe
LZ
3617 if (!not && *idx >= FTRACE_GRAPH_MAX_FUNCS)
3618 return -EBUSY;
f9349a8f
FW
3619
3620 search_len = strlen(search);
3621
52baf119 3622 mutex_lock(&ftrace_lock);
45a4a237
SR
3623
3624 if (unlikely(ftrace_disabled)) {
3625 mutex_unlock(&ftrace_lock);
3626 return -ENODEV;
3627 }
3628
265c831c
SR
3629 do_for_each_ftrace_rec(pg, rec) {
3630
b9df92d2 3631 if (ftrace_match_record(rec, NULL, search, search_len, type)) {
c7c6b1fe 3632 /* if it is in the array */
f9349a8f 3633 exists = false;
c7c6b1fe 3634 for (i = 0; i < *idx; i++) {
f9349a8f
FW
3635 if (array[i] == rec->ip) {
3636 exists = true;
265c831c
SR
3637 break;
3638 }
c7c6b1fe
LZ
3639 }
3640
3641 if (!not) {
3642 fail = 0;
3643 if (!exists) {
3644 array[(*idx)++] = rec->ip;
3645 if (*idx >= FTRACE_GRAPH_MAX_FUNCS)
3646 goto out;
3647 }
3648 } else {
3649 if (exists) {
3650 array[i] = array[--(*idx)];
3651 array[*idx] = 0;
3652 fail = 0;
3653 }
3654 }
ea4e2bc4 3655 }
265c831c 3656 } while_for_each_ftrace_rec();
c7c6b1fe 3657out:
52baf119 3658 mutex_unlock(&ftrace_lock);
ea4e2bc4 3659
c7c6b1fe
LZ
3660 if (fail)
3661 return -EINVAL;
3662
3663 ftrace_graph_filter_enabled = 1;
3664 return 0;
ea4e2bc4
SR
3665}
3666
3667static ssize_t
3668ftrace_graph_write(struct file *file, const char __user *ubuf,
3669 size_t cnt, loff_t *ppos)
3670{
689fd8b6 3671 struct trace_parser parser;
4ba7978e 3672 ssize_t read, ret;
ea4e2bc4 3673
c7c6b1fe 3674 if (!cnt)
ea4e2bc4
SR
3675 return 0;
3676
3677 mutex_lock(&graph_lock);
3678
689fd8b6 3679 if (trace_parser_get_init(&parser, FTRACE_BUFF_MAX)) {
3680 ret = -ENOMEM;
1eb90f13 3681 goto out_unlock;
ea4e2bc4
SR
3682 }
3683
689fd8b6 3684 read = trace_get_user(&parser, ubuf, cnt, ppos);
ea4e2bc4 3685
4ba7978e 3686 if (read >= 0 && trace_parser_loaded((&parser))) {
689fd8b6 3687 parser.buffer[parser.idx] = 0;
3688
3689 /* we allow only one expression at a time */
a4ec5e0c 3690 ret = ftrace_set_func(ftrace_graph_funcs, &ftrace_graph_count,
689fd8b6 3691 parser.buffer);
ea4e2bc4 3692 if (ret)
1eb90f13 3693 goto out_free;
ea4e2bc4 3694 }
ea4e2bc4
SR
3695
3696 ret = read;
1eb90f13
LZ
3697
3698out_free:
689fd8b6 3699 trace_parser_put(&parser);
1eb90f13 3700out_unlock:
ea4e2bc4
SR
3701 mutex_unlock(&graph_lock);
3702
3703 return ret;
3704}
3705
3706static const struct file_operations ftrace_graph_fops = {
87827111
LZ
3707 .open = ftrace_graph_open,
3708 .read = seq_read,
3709 .write = ftrace_graph_write,
3710 .release = ftrace_graph_release,
6038f373 3711 .llseek = seq_lseek,
ea4e2bc4
SR
3712};
3713#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
3714
df4fc315 3715static __init int ftrace_init_dyn_debugfs(struct dentry *d_tracer)
5072c59f 3716{
5072c59f 3717
5452af66
FW
3718 trace_create_file("available_filter_functions", 0444,
3719 d_tracer, NULL, &ftrace_avail_fops);
5072c59f 3720
647bcd03
SR
3721 trace_create_file("enabled_functions", 0444,
3722 d_tracer, NULL, &ftrace_enabled_fops);
3723
5452af66
FW
3724 trace_create_file("set_ftrace_filter", 0644, d_tracer,
3725 NULL, &ftrace_filter_fops);
41c52c0d 3726
5452af66 3727 trace_create_file("set_ftrace_notrace", 0644, d_tracer,
41c52c0d 3728 NULL, &ftrace_notrace_fops);
ad90c0e3 3729
ea4e2bc4 3730#ifdef CONFIG_FUNCTION_GRAPH_TRACER
5452af66 3731 trace_create_file("set_graph_function", 0444, d_tracer,
ea4e2bc4
SR
3732 NULL,
3733 &ftrace_graph_fops);
ea4e2bc4
SR
3734#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
3735
5072c59f
SR
3736 return 0;
3737}
3738
9fd49328 3739static int ftrace_cmp_ips(const void *a, const void *b)
68950619 3740{
9fd49328
SR
3741 const unsigned long *ipa = a;
3742 const unsigned long *ipb = b;
68950619 3743
9fd49328
SR
3744 if (*ipa > *ipb)
3745 return 1;
3746 if (*ipa < *ipb)
3747 return -1;
3748 return 0;
3749}
3750
3751static void ftrace_swap_ips(void *a, void *b, int size)
3752{
3753 unsigned long *ipa = a;
3754 unsigned long *ipb = b;
3755 unsigned long t;
3756
3757 t = *ipa;
3758 *ipa = *ipb;
3759 *ipb = t;
68950619
SR
3760}
3761
5cb084bb 3762static int ftrace_process_locs(struct module *mod,
31e88909 3763 unsigned long *start,
68bf21aa
SR
3764 unsigned long *end)
3765{
706c81f8 3766 struct ftrace_page *start_pg;
a7900875 3767 struct ftrace_page *pg;
706c81f8 3768 struct dyn_ftrace *rec;
a7900875 3769 unsigned long count;
68bf21aa
SR
3770 unsigned long *p;
3771 unsigned long addr;
4376cac6 3772 unsigned long flags = 0; /* Shut up gcc */
a7900875
SR
3773 int ret = -ENOMEM;
3774
3775 count = end - start;
3776
3777 if (!count)
3778 return 0;
3779
9fd49328
SR
3780 sort(start, count, sizeof(*start),
3781 ftrace_cmp_ips, ftrace_swap_ips);
3782
706c81f8
SR
3783 start_pg = ftrace_allocate_pages(count);
3784 if (!start_pg)
a7900875 3785 return -ENOMEM;
68bf21aa 3786
e6ea44e9 3787 mutex_lock(&ftrace_lock);
a7900875 3788
32082309
SR
3789 /*
3790 * Core and each module needs their own pages, as
3791 * modules will free them when they are removed.
3792 * Force a new page to be allocated for modules.
3793 */
a7900875
SR
3794 if (!mod) {
3795 WARN_ON(ftrace_pages || ftrace_pages_start);
3796 /* First initialization */
706c81f8 3797 ftrace_pages = ftrace_pages_start = start_pg;
a7900875 3798 } else {
32082309 3799 if (!ftrace_pages)
a7900875 3800 goto out;
32082309 3801
a7900875
SR
3802 if (WARN_ON(ftrace_pages->next)) {
3803 /* Hmm, we have free pages? */
3804 while (ftrace_pages->next)
3805 ftrace_pages = ftrace_pages->next;
32082309 3806 }
a7900875 3807
706c81f8 3808 ftrace_pages->next = start_pg;
32082309
SR
3809 }
3810
68bf21aa 3811 p = start;
706c81f8 3812 pg = start_pg;
68bf21aa
SR
3813 while (p < end) {
3814 addr = ftrace_call_adjust(*p++);
20e5227e
SR
3815 /*
3816 * Some architecture linkers will pad between
3817 * the different mcount_loc sections of different
3818 * object files to satisfy alignments.
3819 * Skip any NULL pointers.
3820 */
3821 if (!addr)
3822 continue;
706c81f8
SR
3823
3824 if (pg->index == pg->size) {
3825 /* We should have allocated enough */
3826 if (WARN_ON(!pg->next))
3827 break;
3828 pg = pg->next;
3829 }
3830
3831 rec = &pg->records[pg->index++];
3832 rec->ip = addr;
68bf21aa
SR
3833 }
3834
706c81f8
SR
3835 /* We should have used all pages */
3836 WARN_ON(pg->next);
3837
3838 /* Assign the last page to ftrace_pages */
3839 ftrace_pages = pg;
3840
85ae32ae 3841 /* These new locations need to be initialized */
706c81f8 3842 ftrace_new_pgs = start_pg;
85ae32ae 3843
a4f18ed1 3844 /*
4376cac6
SR
3845 * We only need to disable interrupts on start up
3846 * because we are modifying code that an interrupt
3847 * may execute, and the modification is not atomic.
3848 * But for modules, nothing runs the code we modify
3849 * until we are finished with it, and there's no
3850 * reason to cause large interrupt latencies while we do it.
a4f18ed1 3851 */
4376cac6
SR
3852 if (!mod)
3853 local_irq_save(flags);
31e88909 3854 ftrace_update_code(mod);
4376cac6
SR
3855 if (!mod)
3856 local_irq_restore(flags);
a7900875
SR
3857 ret = 0;
3858 out:
e6ea44e9 3859 mutex_unlock(&ftrace_lock);
68bf21aa 3860
a7900875 3861 return ret;
68bf21aa
SR
3862}
3863
93eb677d 3864#ifdef CONFIG_MODULES
32082309
SR
3865
3866#define next_to_ftrace_page(p) container_of(p, struct ftrace_page, next)
3867
e7247a15 3868void ftrace_release_mod(struct module *mod)
93eb677d
SR
3869{
3870 struct dyn_ftrace *rec;
32082309 3871 struct ftrace_page **last_pg;
93eb677d 3872 struct ftrace_page *pg;
a7900875 3873 int order;
93eb677d 3874
45a4a237
SR
3875 mutex_lock(&ftrace_lock);
3876
e7247a15 3877 if (ftrace_disabled)
45a4a237 3878 goto out_unlock;
93eb677d 3879
32082309
SR
3880 /*
3881 * Each module has its own ftrace_pages, remove
3882 * them from the list.
3883 */
3884 last_pg = &ftrace_pages_start;
3885 for (pg = ftrace_pages_start; pg; pg = *last_pg) {
3886 rec = &pg->records[0];
e7247a15 3887 if (within_module_core(rec->ip, mod)) {
93eb677d 3888 /*
32082309
SR
3889 * As core pages are first, the first
3890 * page should never be a module page.
93eb677d 3891 */
32082309
SR
3892 if (WARN_ON(pg == ftrace_pages_start))
3893 goto out_unlock;
3894
3895 /* Check if we are deleting the last page */
3896 if (pg == ftrace_pages)
3897 ftrace_pages = next_to_ftrace_page(last_pg);
3898
3899 *last_pg = pg->next;
a7900875
SR
3900 order = get_count_order(pg->size / ENTRIES_PER_PAGE);
3901 free_pages((unsigned long)pg->records, order);
3902 kfree(pg);
32082309
SR
3903 } else
3904 last_pg = &pg->next;
3905 }
45a4a237 3906 out_unlock:
93eb677d
SR
3907 mutex_unlock(&ftrace_lock);
3908}
3909
3910static void ftrace_init_module(struct module *mod,
3911 unsigned long *start, unsigned long *end)
90d595fe 3912{
00fd61ae 3913 if (ftrace_disabled || start == end)
fed1939c 3914 return;
5cb084bb 3915 ftrace_process_locs(mod, start, end);
90d595fe
SR
3916}
3917
93eb677d
SR
3918static int ftrace_module_notify(struct notifier_block *self,
3919 unsigned long val, void *data)
3920{
3921 struct module *mod = data;
3922
3923 switch (val) {
3924 case MODULE_STATE_COMING:
3925 ftrace_init_module(mod, mod->ftrace_callsites,
3926 mod->ftrace_callsites +
3927 mod->num_ftrace_callsites);
3928 break;
3929 case MODULE_STATE_GOING:
e7247a15 3930 ftrace_release_mod(mod);
93eb677d
SR
3931 break;
3932 }
3933
3934 return 0;
3935}
3936#else
3937static int ftrace_module_notify(struct notifier_block *self,
3938 unsigned long val, void *data)
3939{
3940 return 0;
3941}
3942#endif /* CONFIG_MODULES */
3943
3944struct notifier_block ftrace_module_nb = {
3945 .notifier_call = ftrace_module_notify,
3946 .priority = 0,
3947};
3948
68bf21aa
SR
3949extern unsigned long __start_mcount_loc[];
3950extern unsigned long __stop_mcount_loc[];
3951
3952void __init ftrace_init(void)
3953{
3954 unsigned long count, addr, flags;
3955 int ret;
3956
3957 /* Keep the ftrace pointer to the stub */
3958 addr = (unsigned long)ftrace_stub;
3959
3960 local_irq_save(flags);
3961 ftrace_dyn_arch_init(&addr);
3962 local_irq_restore(flags);
3963
3964 /* ftrace_dyn_arch_init places the return code in addr */
3965 if (addr)
3966 goto failed;
3967
3968 count = __stop_mcount_loc - __start_mcount_loc;
3969
3970 ret = ftrace_dyn_table_alloc(count);
3971 if (ret)
3972 goto failed;
3973
3974 last_ftrace_enabled = ftrace_enabled = 1;
3975
5cb084bb 3976 ret = ftrace_process_locs(NULL,
31e88909 3977 __start_mcount_loc,
68bf21aa
SR
3978 __stop_mcount_loc);
3979
93eb677d 3980 ret = register_module_notifier(&ftrace_module_nb);
24ed0c4b 3981 if (ret)
93eb677d
SR
3982 pr_warning("Failed to register trace ftrace module notifier\n");
3983
2af15d6a
SR
3984 set_ftrace_early_filters();
3985
68bf21aa
SR
3986 return;
3987 failed:
3988 ftrace_disabled = 1;
3989}
68bf21aa 3990
3d083395 3991#else
0b6e4d56 3992
2b499381 3993static struct ftrace_ops global_ops = {
bd69c30b 3994 .func = ftrace_stub,
4740974a 3995 .flags = FTRACE_OPS_FL_RECURSION_SAFE,
bd69c30b
SR
3996};
3997
0b6e4d56
FW
3998static int __init ftrace_nodyn_init(void)
3999{
4000 ftrace_enabled = 1;
4001 return 0;
4002}
4003device_initcall(ftrace_nodyn_init);
4004
df4fc315
SR
4005static inline int ftrace_init_dyn_debugfs(struct dentry *d_tracer) { return 0; }
4006static inline void ftrace_startup_enable(int command) { }
5a45cfe1 4007/* Keep as macros so we do not need to define the commands */
3b6cfdb1
SR
4008# define ftrace_startup(ops, command) \
4009 ({ \
4010 (ops)->flags |= FTRACE_OPS_FL_ENABLED; \
4011 0; \
4012 })
bd69c30b 4013# define ftrace_shutdown(ops, command) do { } while (0)
c7aafc54
IM
4014# define ftrace_startup_sysctl() do { } while (0)
4015# define ftrace_shutdown_sysctl() do { } while (0)
b848914c
SR
4016
4017static inline int
4018ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip)
4019{
4020 return 1;
4021}
4022
3d083395
SR
4023#endif /* CONFIG_DYNAMIC_FTRACE */
4024
e248491a 4025static void
2f5f6ad9 4026ftrace_ops_control_func(unsigned long ip, unsigned long parent_ip,
a1e2e31d 4027 struct ftrace_ops *op, struct pt_regs *regs)
e248491a 4028{
e248491a
JO
4029 if (unlikely(trace_recursion_test(TRACE_CONTROL_BIT)))
4030 return;
4031
4032 /*
4033 * Some of the ops may be dynamically allocated,
4034 * they must be freed after a synchronize_sched().
4035 */
4036 preempt_disable_notrace();
4037 trace_recursion_set(TRACE_CONTROL_BIT);
4038 op = rcu_dereference_raw(ftrace_control_list);
4039 while (op != &ftrace_list_end) {
4040 if (!ftrace_function_local_disabled(op) &&
4041 ftrace_ops_test(op, ip))
a1e2e31d 4042 op->func(ip, parent_ip, op, regs);
e248491a
JO
4043
4044 op = rcu_dereference_raw(op->next);
4045 };
4046 trace_recursion_clear(TRACE_CONTROL_BIT);
4047 preempt_enable_notrace();
4048}
4049
4050static struct ftrace_ops control_ops = {
4051 .func = ftrace_ops_control_func,
4740974a 4052 .flags = FTRACE_OPS_FL_RECURSION_SAFE,
e248491a
JO
4053};
4054
2f5f6ad9
SR
4055static inline void
4056__ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
a1e2e31d 4057 struct ftrace_ops *ignored, struct pt_regs *regs)
b848914c 4058{
cdbe61bf 4059 struct ftrace_ops *op;
b848914c 4060
ccf3672d
SR
4061 if (function_trace_stop)
4062 return;
4063
b1cff0ad
SR
4064 if (unlikely(trace_recursion_test(TRACE_INTERNAL_BIT)))
4065 return;
4066
4067 trace_recursion_set(TRACE_INTERNAL_BIT);
cdbe61bf
SR
4068 /*
4069 * Some of the ops may be dynamically allocated,
4070 * they must be freed after a synchronize_sched().
4071 */
4072 preempt_disable_notrace();
4073 op = rcu_dereference_raw(ftrace_ops_list);
b848914c
SR
4074 while (op != &ftrace_list_end) {
4075 if (ftrace_ops_test(op, ip))
a1e2e31d 4076 op->func(ip, parent_ip, op, regs);
b848914c
SR
4077 op = rcu_dereference_raw(op->next);
4078 };
cdbe61bf 4079 preempt_enable_notrace();
b1cff0ad 4080 trace_recursion_clear(TRACE_INTERNAL_BIT);
b848914c
SR
4081}
4082
2f5f6ad9
SR
4083/*
4084 * Some archs only support passing ip and parent_ip. Even though
4085 * the list function ignores the op parameter, we do not want any
4086 * C side effects, where a function is called without the caller
4087 * sending a third parameter.
a1e2e31d
SR
4088 * Archs are to support both the regs and ftrace_ops at the same time.
4089 * If they support ftrace_ops, it is assumed they support regs.
4090 * If call backs want to use regs, they must either check for regs
4091 * being NULL, or ARCH_SUPPORTS_FTRACE_SAVE_REGS.
4092 * Note, ARCH_SUPPORT_SAVE_REGS expects a full regs to be saved.
4093 * An architecture can pass partial regs with ftrace_ops and still
4094 * set the ARCH_SUPPORT_FTARCE_OPS.
2f5f6ad9
SR
4095 */
4096#if ARCH_SUPPORTS_FTRACE_OPS
4097static void ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
a1e2e31d 4098 struct ftrace_ops *op, struct pt_regs *regs)
2f5f6ad9 4099{
a1e2e31d 4100 __ftrace_ops_list_func(ip, parent_ip, NULL, regs);
2f5f6ad9
SR
4101}
4102#else
4103static void ftrace_ops_no_ops(unsigned long ip, unsigned long parent_ip)
4104{
a1e2e31d 4105 __ftrace_ops_list_func(ip, parent_ip, NULL, NULL);
2f5f6ad9
SR
4106}
4107#endif
4108
e32d8956 4109static void clear_ftrace_swapper(void)
978f3a45
SR
4110{
4111 struct task_struct *p;
e32d8956 4112 int cpu;
978f3a45 4113
e32d8956
SR
4114 get_online_cpus();
4115 for_each_online_cpu(cpu) {
4116 p = idle_task(cpu);
978f3a45 4117 clear_tsk_trace_trace(p);
e32d8956
SR
4118 }
4119 put_online_cpus();
4120}
978f3a45 4121
e32d8956
SR
4122static void set_ftrace_swapper(void)
4123{
4124 struct task_struct *p;
4125 int cpu;
4126
4127 get_online_cpus();
4128 for_each_online_cpu(cpu) {
4129 p = idle_task(cpu);
4130 set_tsk_trace_trace(p);
4131 }
4132 put_online_cpus();
978f3a45
SR
4133}
4134
e32d8956
SR
4135static void clear_ftrace_pid(struct pid *pid)
4136{
4137 struct task_struct *p;
4138
229c4ef8 4139 rcu_read_lock();
e32d8956
SR
4140 do_each_pid_task(pid, PIDTYPE_PID, p) {
4141 clear_tsk_trace_trace(p);
4142 } while_each_pid_task(pid, PIDTYPE_PID, p);
229c4ef8
ON
4143 rcu_read_unlock();
4144
e32d8956
SR
4145 put_pid(pid);
4146}
4147
4148static void set_ftrace_pid(struct pid *pid)
978f3a45
SR
4149{
4150 struct task_struct *p;
4151
229c4ef8 4152 rcu_read_lock();
978f3a45
SR
4153 do_each_pid_task(pid, PIDTYPE_PID, p) {
4154 set_tsk_trace_trace(p);
4155 } while_each_pid_task(pid, PIDTYPE_PID, p);
229c4ef8 4156 rcu_read_unlock();
978f3a45
SR
4157}
4158
756d17ee 4159static void clear_ftrace_pid_task(struct pid *pid)
e32d8956 4160{
756d17ee 4161 if (pid == ftrace_swapper_pid)
e32d8956
SR
4162 clear_ftrace_swapper();
4163 else
756d17ee 4164 clear_ftrace_pid(pid);
e32d8956
SR
4165}
4166
4167static void set_ftrace_pid_task(struct pid *pid)
4168{
4169 if (pid == ftrace_swapper_pid)
4170 set_ftrace_swapper();
4171 else
4172 set_ftrace_pid(pid);
4173}
4174
756d17ee 4175static int ftrace_pid_add(int p)
df4fc315 4176{
978f3a45 4177 struct pid *pid;
756d17ee 4178 struct ftrace_pid *fpid;
4179 int ret = -EINVAL;
df4fc315 4180
756d17ee 4181 mutex_lock(&ftrace_lock);
df4fc315 4182
756d17ee 4183 if (!p)
4184 pid = ftrace_swapper_pid;
4185 else
4186 pid = find_get_pid(p);
df4fc315 4187
756d17ee 4188 if (!pid)
4189 goto out;
df4fc315 4190
756d17ee 4191 ret = 0;
df4fc315 4192
756d17ee 4193 list_for_each_entry(fpid, &ftrace_pids, list)
4194 if (fpid->pid == pid)
4195 goto out_put;
978f3a45 4196
756d17ee 4197 ret = -ENOMEM;
df4fc315 4198
756d17ee 4199 fpid = kmalloc(sizeof(*fpid), GFP_KERNEL);
4200 if (!fpid)
4201 goto out_put;
df4fc315 4202
756d17ee 4203 list_add(&fpid->list, &ftrace_pids);
4204 fpid->pid = pid;
0ef8cde5 4205
756d17ee 4206 set_ftrace_pid_task(pid);
978f3a45 4207
756d17ee 4208 ftrace_update_pid_func();
4209 ftrace_startup_enable(0);
4210
4211 mutex_unlock(&ftrace_lock);
4212 return 0;
4213
4214out_put:
4215 if (pid != ftrace_swapper_pid)
4216 put_pid(pid);
978f3a45 4217
756d17ee 4218out:
4219 mutex_unlock(&ftrace_lock);
4220 return ret;
4221}
4222
4223static void ftrace_pid_reset(void)
4224{
4225 struct ftrace_pid *fpid, *safe;
978f3a45 4226
756d17ee 4227 mutex_lock(&ftrace_lock);
4228 list_for_each_entry_safe(fpid, safe, &ftrace_pids, list) {
4229 struct pid *pid = fpid->pid;
4230
4231 clear_ftrace_pid_task(pid);
4232
4233 list_del(&fpid->list);
4234 kfree(fpid);
df4fc315
SR
4235 }
4236
df4fc315
SR
4237 ftrace_update_pid_func();
4238 ftrace_startup_enable(0);
4239
e6ea44e9 4240 mutex_unlock(&ftrace_lock);
756d17ee 4241}
df4fc315 4242
756d17ee 4243static void *fpid_start(struct seq_file *m, loff_t *pos)
4244{
4245 mutex_lock(&ftrace_lock);
4246
4247 if (list_empty(&ftrace_pids) && (!*pos))
4248 return (void *) 1;
4249
4250 return seq_list_start(&ftrace_pids, *pos);
4251}
4252
4253static void *fpid_next(struct seq_file *m, void *v, loff_t *pos)
4254{
4255 if (v == (void *)1)
4256 return NULL;
4257
4258 return seq_list_next(v, &ftrace_pids, pos);
4259}
4260
4261static void fpid_stop(struct seq_file *m, void *p)
4262{
4263 mutex_unlock(&ftrace_lock);
4264}
4265
4266static int fpid_show(struct seq_file *m, void *v)
4267{
4268 const struct ftrace_pid *fpid = list_entry(v, struct ftrace_pid, list);
4269
4270 if (v == (void *)1) {
4271 seq_printf(m, "no pid\n");
4272 return 0;
4273 }
4274
4275 if (fpid->pid == ftrace_swapper_pid)
4276 seq_printf(m, "swapper tasks\n");
4277 else
4278 seq_printf(m, "%u\n", pid_vnr(fpid->pid));
4279
4280 return 0;
4281}
4282
4283static const struct seq_operations ftrace_pid_sops = {
4284 .start = fpid_start,
4285 .next = fpid_next,
4286 .stop = fpid_stop,
4287 .show = fpid_show,
4288};
4289
4290static int
4291ftrace_pid_open(struct inode *inode, struct file *file)
4292{
4293 int ret = 0;
4294
4295 if ((file->f_mode & FMODE_WRITE) &&
4296 (file->f_flags & O_TRUNC))
4297 ftrace_pid_reset();
4298
4299 if (file->f_mode & FMODE_READ)
4300 ret = seq_open(file, &ftrace_pid_sops);
4301
4302 return ret;
4303}
4304
df4fc315
SR
4305static ssize_t
4306ftrace_pid_write(struct file *filp, const char __user *ubuf,
4307 size_t cnt, loff_t *ppos)
4308{
457dc928 4309 char buf[64], *tmp;
df4fc315
SR
4310 long val;
4311 int ret;
4312
4313 if (cnt >= sizeof(buf))
4314 return -EINVAL;
4315
4316 if (copy_from_user(&buf, ubuf, cnt))
4317 return -EFAULT;
4318
4319 buf[cnt] = 0;
4320
756d17ee 4321 /*
4322 * Allow "echo > set_ftrace_pid" or "echo -n '' > set_ftrace_pid"
4323 * to clean the filter quietly.
4324 */
457dc928
IM
4325 tmp = strstrip(buf);
4326 if (strlen(tmp) == 0)
756d17ee 4327 return 1;
4328
457dc928 4329 ret = strict_strtol(tmp, 10, &val);
df4fc315
SR
4330 if (ret < 0)
4331 return ret;
4332
756d17ee 4333 ret = ftrace_pid_add(val);
df4fc315 4334
756d17ee 4335 return ret ? ret : cnt;
4336}
df4fc315 4337
756d17ee 4338static int
4339ftrace_pid_release(struct inode *inode, struct file *file)
4340{
4341 if (file->f_mode & FMODE_READ)
4342 seq_release(inode, file);
df4fc315 4343
756d17ee 4344 return 0;
df4fc315
SR
4345}
4346
5e2336a0 4347static const struct file_operations ftrace_pid_fops = {
756d17ee 4348 .open = ftrace_pid_open,
4349 .write = ftrace_pid_write,
4350 .read = seq_read,
4351 .llseek = seq_lseek,
4352 .release = ftrace_pid_release,
df4fc315
SR
4353};
4354
4355static __init int ftrace_init_debugfs(void)
4356{
4357 struct dentry *d_tracer;
df4fc315
SR
4358
4359 d_tracer = tracing_init_dentry();
4360 if (!d_tracer)
4361 return 0;
4362
4363 ftrace_init_dyn_debugfs(d_tracer);
4364
5452af66
FW
4365 trace_create_file("set_ftrace_pid", 0644, d_tracer,
4366 NULL, &ftrace_pid_fops);
493762fc
SR
4367
4368 ftrace_profile_debugfs(d_tracer);
4369
df4fc315
SR
4370 return 0;
4371}
df4fc315
SR
4372fs_initcall(ftrace_init_debugfs);
4373
a2bb6a3d 4374/**
81adbdc0 4375 * ftrace_kill - kill ftrace
a2bb6a3d
SR
4376 *
4377 * This function should be used by panic code. It stops ftrace
4378 * but in a not so nice way. If you need to simply kill ftrace
4379 * from a non-atomic section, use ftrace_kill.
4380 */
81adbdc0 4381void ftrace_kill(void)
a2bb6a3d
SR
4382{
4383 ftrace_disabled = 1;
4384 ftrace_enabled = 0;
a2bb6a3d
SR
4385 clear_ftrace_function();
4386}
4387
e0a413f6
SR
4388/**
4389 * Test if ftrace is dead or not.
4390 */
4391int ftrace_is_dead(void)
4392{
4393 return ftrace_disabled;
4394}
4395
16444a8a 4396/**
3d083395
SR
4397 * register_ftrace_function - register a function for profiling
4398 * @ops - ops structure that holds the function for profiling.
16444a8a 4399 *
3d083395
SR
4400 * Register a function to be called by all functions in the
4401 * kernel.
4402 *
4403 * Note: @ops->func and all the functions it calls must be labeled
4404 * with "notrace", otherwise it will go into a
4405 * recursive loop.
16444a8a 4406 */
3d083395 4407int register_ftrace_function(struct ftrace_ops *ops)
16444a8a 4408{
45a4a237 4409 int ret = -1;
4eebcc81 4410
e6ea44e9 4411 mutex_lock(&ftrace_lock);
e7d3737e 4412
b0fc494f 4413 ret = __register_ftrace_function(ops);
b848914c 4414 if (!ret)
a1cd6173 4415 ret = ftrace_startup(ops, 0);
b848914c 4416
e6ea44e9 4417 mutex_unlock(&ftrace_lock);
8d240dd8 4418
b0fc494f 4419 return ret;
3d083395 4420}
cdbe61bf 4421EXPORT_SYMBOL_GPL(register_ftrace_function);
3d083395
SR
4422
4423/**
32632920 4424 * unregister_ftrace_function - unregister a function for profiling.
3d083395
SR
4425 * @ops - ops structure that holds the function to unregister
4426 *
4427 * Unregister a function that was added to be called by ftrace profiling.
4428 */
4429int unregister_ftrace_function(struct ftrace_ops *ops)
4430{
4431 int ret;
4432
e6ea44e9 4433 mutex_lock(&ftrace_lock);
3d083395 4434 ret = __unregister_ftrace_function(ops);
b848914c
SR
4435 if (!ret)
4436 ftrace_shutdown(ops, 0);
e6ea44e9 4437 mutex_unlock(&ftrace_lock);
b0fc494f
SR
4438
4439 return ret;
4440}
cdbe61bf 4441EXPORT_SYMBOL_GPL(unregister_ftrace_function);
b0fc494f 4442
e309b41d 4443int
b0fc494f 4444ftrace_enable_sysctl(struct ctl_table *table, int write,
8d65af78 4445 void __user *buffer, size_t *lenp,
b0fc494f
SR
4446 loff_t *ppos)
4447{
45a4a237 4448 int ret = -ENODEV;
4eebcc81 4449
e6ea44e9 4450 mutex_lock(&ftrace_lock);
b0fc494f 4451
45a4a237
SR
4452 if (unlikely(ftrace_disabled))
4453 goto out;
4454
4455 ret = proc_dointvec(table, write, buffer, lenp, ppos);
b0fc494f 4456
a32c7765 4457 if (ret || !write || (last_ftrace_enabled == !!ftrace_enabled))
b0fc494f
SR
4458 goto out;
4459
a32c7765 4460 last_ftrace_enabled = !!ftrace_enabled;
b0fc494f
SR
4461
4462 if (ftrace_enabled) {
4463
4464 ftrace_startup_sysctl();
4465
4466 /* we are starting ftrace again */
b848914c
SR
4467 if (ftrace_ops_list != &ftrace_list_end) {
4468 if (ftrace_ops_list->next == &ftrace_list_end)
4469 ftrace_trace_function = ftrace_ops_list->func;
b0fc494f 4470 else
b848914c 4471 ftrace_trace_function = ftrace_ops_list_func;
b0fc494f
SR
4472 }
4473
4474 } else {
4475 /* stopping ftrace calls (just send to ftrace_stub) */
4476 ftrace_trace_function = ftrace_stub;
4477
4478 ftrace_shutdown_sysctl();
4479 }
4480
4481 out:
e6ea44e9 4482 mutex_unlock(&ftrace_lock);
3d083395 4483 return ret;
16444a8a 4484}
f17845e5 4485
fb52607a 4486#ifdef CONFIG_FUNCTION_GRAPH_TRACER
e7d3737e 4487
597af815 4488static int ftrace_graph_active;
4a2b8dda 4489static struct notifier_block ftrace_suspend_notifier;
e7d3737e 4490
e49dc19c
SR
4491int ftrace_graph_entry_stub(struct ftrace_graph_ent *trace)
4492{
4493 return 0;
4494}
4495
287b6e68
FW
4496/* The callbacks that hook a function */
4497trace_func_graph_ret_t ftrace_graph_return =
4498 (trace_func_graph_ret_t)ftrace_stub;
e49dc19c 4499trace_func_graph_ent_t ftrace_graph_entry = ftrace_graph_entry_stub;
f201ae23
FW
4500
4501/* Try to assign a return stack array on FTRACE_RETSTACK_ALLOC_SIZE tasks. */
4502static int alloc_retstack_tasklist(struct ftrace_ret_stack **ret_stack_list)
4503{
4504 int i;
4505 int ret = 0;
4506 unsigned long flags;
4507 int start = 0, end = FTRACE_RETSTACK_ALLOC_SIZE;
4508 struct task_struct *g, *t;
4509
4510 for (i = 0; i < FTRACE_RETSTACK_ALLOC_SIZE; i++) {
4511 ret_stack_list[i] = kmalloc(FTRACE_RETFUNC_DEPTH
4512 * sizeof(struct ftrace_ret_stack),
4513 GFP_KERNEL);
4514 if (!ret_stack_list[i]) {
4515 start = 0;
4516 end = i;
4517 ret = -ENOMEM;
4518 goto free;
4519 }
4520 }
4521
4522 read_lock_irqsave(&tasklist_lock, flags);
4523 do_each_thread(g, t) {
4524 if (start == end) {
4525 ret = -EAGAIN;
4526 goto unlock;
4527 }
4528
4529 if (t->ret_stack == NULL) {
380c4b14 4530 atomic_set(&t->tracing_graph_pause, 0);
f201ae23 4531 atomic_set(&t->trace_overrun, 0);
26c01624
SR
4532 t->curr_ret_stack = -1;
4533 /* Make sure the tasks see the -1 first: */
4534 smp_wmb();
4535 t->ret_stack = ret_stack_list[start++];
f201ae23
FW
4536 }
4537 } while_each_thread(g, t);
4538
4539unlock:
4540 read_unlock_irqrestore(&tasklist_lock, flags);
4541free:
4542 for (i = start; i < end; i++)
4543 kfree(ret_stack_list[i]);
4544 return ret;
4545}
4546
8aef2d28 4547static void
38516ab5
SR
4548ftrace_graph_probe_sched_switch(void *ignore,
4549 struct task_struct *prev, struct task_struct *next)
8aef2d28
SR
4550{
4551 unsigned long long timestamp;
4552 int index;
4553
be6f164a
SR
4554 /*
4555 * Does the user want to count the time a function was asleep.
4556 * If so, do not update the time stamps.
4557 */
4558 if (trace_flags & TRACE_ITER_SLEEP_TIME)
4559 return;
4560
8aef2d28
SR
4561 timestamp = trace_clock_local();
4562
4563 prev->ftrace_timestamp = timestamp;
4564
4565 /* only process tasks that we timestamped */
4566 if (!next->ftrace_timestamp)
4567 return;
4568
4569 /*
4570 * Update all the counters in next to make up for the
4571 * time next was sleeping.
4572 */
4573 timestamp -= next->ftrace_timestamp;
4574
4575 for (index = next->curr_ret_stack; index >= 0; index--)
4576 next->ret_stack[index].calltime += timestamp;
4577}
4578
f201ae23 4579/* Allocate a return stack for each task */
fb52607a 4580static int start_graph_tracing(void)
f201ae23
FW
4581{
4582 struct ftrace_ret_stack **ret_stack_list;
5b058bcd 4583 int ret, cpu;
f201ae23
FW
4584
4585 ret_stack_list = kmalloc(FTRACE_RETSTACK_ALLOC_SIZE *
4586 sizeof(struct ftrace_ret_stack *),
4587 GFP_KERNEL);
4588
4589 if (!ret_stack_list)
4590 return -ENOMEM;
4591
5b058bcd 4592 /* The cpu_boot init_task->ret_stack will never be freed */
179c498a
SR
4593 for_each_online_cpu(cpu) {
4594 if (!idle_task(cpu)->ret_stack)
868baf07 4595 ftrace_graph_init_idle_task(idle_task(cpu), cpu);
179c498a 4596 }
5b058bcd 4597
f201ae23
FW
4598 do {
4599 ret = alloc_retstack_tasklist(ret_stack_list);
4600 } while (ret == -EAGAIN);
4601
8aef2d28 4602 if (!ret) {
38516ab5 4603 ret = register_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
8aef2d28
SR
4604 if (ret)
4605 pr_info("ftrace_graph: Couldn't activate tracepoint"
4606 " probe to kernel_sched_switch\n");
4607 }
4608
f201ae23
FW
4609 kfree(ret_stack_list);
4610 return ret;
4611}
4612
4a2b8dda
FW
4613/*
4614 * Hibernation protection.
4615 * The state of the current task is too much unstable during
4616 * suspend/restore to disk. We want to protect against that.
4617 */
4618static int
4619ftrace_suspend_notifier_call(struct notifier_block *bl, unsigned long state,
4620 void *unused)
4621{
4622 switch (state) {
4623 case PM_HIBERNATION_PREPARE:
4624 pause_graph_tracing();
4625 break;
4626
4627 case PM_POST_HIBERNATION:
4628 unpause_graph_tracing();
4629 break;
4630 }
4631 return NOTIFY_DONE;
4632}
4633
287b6e68
FW
4634int register_ftrace_graph(trace_func_graph_ret_t retfunc,
4635 trace_func_graph_ent_t entryfunc)
15e6cb36 4636{
e7d3737e
FW
4637 int ret = 0;
4638
e6ea44e9 4639 mutex_lock(&ftrace_lock);
e7d3737e 4640
05ce5818 4641 /* we currently allow only one tracer registered at a time */
597af815 4642 if (ftrace_graph_active) {
05ce5818
SR
4643 ret = -EBUSY;
4644 goto out;
4645 }
4646
4a2b8dda
FW
4647 ftrace_suspend_notifier.notifier_call = ftrace_suspend_notifier_call;
4648 register_pm_notifier(&ftrace_suspend_notifier);
4649
597af815 4650 ftrace_graph_active++;
fb52607a 4651 ret = start_graph_tracing();
f201ae23 4652 if (ret) {
597af815 4653 ftrace_graph_active--;
f201ae23
FW
4654 goto out;
4655 }
e53a6319 4656
287b6e68
FW
4657 ftrace_graph_return = retfunc;
4658 ftrace_graph_entry = entryfunc;
e53a6319 4659
a1cd6173 4660 ret = ftrace_startup(&global_ops, FTRACE_START_FUNC_RET);
e7d3737e
FW
4661
4662out:
e6ea44e9 4663 mutex_unlock(&ftrace_lock);
e7d3737e 4664 return ret;
15e6cb36
FW
4665}
4666
fb52607a 4667void unregister_ftrace_graph(void)
15e6cb36 4668{
e6ea44e9 4669 mutex_lock(&ftrace_lock);
e7d3737e 4670
597af815 4671 if (unlikely(!ftrace_graph_active))
2aad1b76
SR
4672 goto out;
4673
597af815 4674 ftrace_graph_active--;
287b6e68 4675 ftrace_graph_return = (trace_func_graph_ret_t)ftrace_stub;
e49dc19c 4676 ftrace_graph_entry = ftrace_graph_entry_stub;
bd69c30b 4677 ftrace_shutdown(&global_ops, FTRACE_STOP_FUNC_RET);
4a2b8dda 4678 unregister_pm_notifier(&ftrace_suspend_notifier);
38516ab5 4679 unregister_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
e7d3737e 4680
2aad1b76 4681 out:
e6ea44e9 4682 mutex_unlock(&ftrace_lock);
15e6cb36 4683}
f201ae23 4684
868baf07
SR
4685static DEFINE_PER_CPU(struct ftrace_ret_stack *, idle_ret_stack);
4686
4687static void
4688graph_init_task(struct task_struct *t, struct ftrace_ret_stack *ret_stack)
4689{
4690 atomic_set(&t->tracing_graph_pause, 0);
4691 atomic_set(&t->trace_overrun, 0);
4692 t->ftrace_timestamp = 0;
25985edc 4693 /* make curr_ret_stack visible before we add the ret_stack */
868baf07
SR
4694 smp_wmb();
4695 t->ret_stack = ret_stack;
4696}
4697
4698/*
4699 * Allocate a return stack for the idle task. May be the first
4700 * time through, or it may be done by CPU hotplug online.
4701 */
4702void ftrace_graph_init_idle_task(struct task_struct *t, int cpu)
4703{
4704 t->curr_ret_stack = -1;
4705 /*
4706 * The idle task has no parent, it either has its own
4707 * stack or no stack at all.
4708 */
4709 if (t->ret_stack)
4710 WARN_ON(t->ret_stack != per_cpu(idle_ret_stack, cpu));
4711
4712 if (ftrace_graph_active) {
4713 struct ftrace_ret_stack *ret_stack;
4714
4715 ret_stack = per_cpu(idle_ret_stack, cpu);
4716 if (!ret_stack) {
4717 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
4718 * sizeof(struct ftrace_ret_stack),
4719 GFP_KERNEL);
4720 if (!ret_stack)
4721 return;
4722 per_cpu(idle_ret_stack, cpu) = ret_stack;
4723 }
4724 graph_init_task(t, ret_stack);
4725 }
4726}
4727
f201ae23 4728/* Allocate a return stack for newly created task */
fb52607a 4729void ftrace_graph_init_task(struct task_struct *t)
f201ae23 4730{
84047e36
SR
4731 /* Make sure we do not use the parent ret_stack */
4732 t->ret_stack = NULL;
ea14eb71 4733 t->curr_ret_stack = -1;
84047e36 4734
597af815 4735 if (ftrace_graph_active) {
82310a32
SR
4736 struct ftrace_ret_stack *ret_stack;
4737
4738 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
f201ae23
FW
4739 * sizeof(struct ftrace_ret_stack),
4740 GFP_KERNEL);
82310a32 4741 if (!ret_stack)
f201ae23 4742 return;
868baf07 4743 graph_init_task(t, ret_stack);
84047e36 4744 }
f201ae23
FW
4745}
4746
fb52607a 4747void ftrace_graph_exit_task(struct task_struct *t)
f201ae23 4748{
eae849ca
FW
4749 struct ftrace_ret_stack *ret_stack = t->ret_stack;
4750
f201ae23 4751 t->ret_stack = NULL;
eae849ca
FW
4752 /* NULL must become visible to IRQs before we free it: */
4753 barrier();
4754
4755 kfree(ret_stack);
f201ae23 4756}
14a866c5
SR
4757
4758void ftrace_graph_stop(void)
4759{
4760 ftrace_stop();
4761}
15e6cb36 4762#endif