]> git.proxmox.com Git - mirror_ubuntu-kernels.git/blame - kernel/trace/ftrace.c
tracing/function-graph-tracer: make set_graph_function file support ftrace regex
[mirror_ubuntu-kernels.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>
f22f9a89 25#include <linux/kprobes.h>
2d8b820b 26#include <linux/ftrace.h>
b0fc494f 27#include <linux/sysctl.h>
5072c59f 28#include <linux/ctype.h>
3d083395 29#include <linux/list.h>
59df055f 30#include <linux/hash.h>
3d083395 31
395a59d0
AS
32#include <asm/ftrace.h>
33
3d083395 34#include "trace.h"
16444a8a 35
6912896e
SR
36#define FTRACE_WARN_ON(cond) \
37 do { \
38 if (WARN_ON(cond)) \
39 ftrace_kill(); \
40 } while (0)
41
42#define FTRACE_WARN_ON_ONCE(cond) \
43 do { \
44 if (WARN_ON_ONCE(cond)) \
45 ftrace_kill(); \
46 } while (0)
47
8fc0c701
SR
48/* hash bits for specific function selection */
49#define FTRACE_HASH_BITS 7
50#define FTRACE_FUNC_HASHSIZE (1 << FTRACE_HASH_BITS)
51
4eebcc81
SR
52/* ftrace_enabled is a method to turn ftrace on or off */
53int ftrace_enabled __read_mostly;
d61f82d0 54static int last_ftrace_enabled;
b0fc494f 55
60a7ecf4
SR
56/* Quick disabling of function tracer. */
57int function_trace_stop;
58
4eebcc81
SR
59/*
60 * ftrace_disabled is set when an anomaly is discovered.
61 * ftrace_disabled is much stronger than ftrace_enabled.
62 */
63static int ftrace_disabled __read_mostly;
64
52baf119 65static DEFINE_MUTEX(ftrace_lock);
b0fc494f 66
16444a8a
ACM
67static struct ftrace_ops ftrace_list_end __read_mostly =
68{
69 .func = ftrace_stub,
70};
71
72static struct ftrace_ops *ftrace_list __read_mostly = &ftrace_list_end;
73ftrace_func_t ftrace_trace_function __read_mostly = ftrace_stub;
60a7ecf4 74ftrace_func_t __ftrace_trace_function __read_mostly = ftrace_stub;
df4fc315 75ftrace_func_t ftrace_pid_function __read_mostly = ftrace_stub;
16444a8a 76
f2252935 77static void ftrace_list_func(unsigned long ip, unsigned long parent_ip)
16444a8a
ACM
78{
79 struct ftrace_ops *op = ftrace_list;
80
81 /* in case someone actually ports this to alpha! */
82 read_barrier_depends();
83
84 while (op != &ftrace_list_end) {
85 /* silly alpha */
86 read_barrier_depends();
87 op->func(ip, parent_ip);
88 op = op->next;
89 };
90}
91
df4fc315
SR
92static void ftrace_pid_func(unsigned long ip, unsigned long parent_ip)
93{
0ef8cde5 94 if (!test_tsk_trace_trace(current))
df4fc315
SR
95 return;
96
97 ftrace_pid_function(ip, parent_ip);
98}
99
100static void set_ftrace_pid_function(ftrace_func_t func)
101{
102 /* do not set ftrace_pid_function to itself! */
103 if (func != ftrace_pid_func)
104 ftrace_pid_function = func;
105}
106
16444a8a 107/**
3d083395 108 * clear_ftrace_function - reset the ftrace function
16444a8a 109 *
3d083395
SR
110 * This NULLs the ftrace function and in essence stops
111 * tracing. There may be lag
16444a8a 112 */
3d083395 113void clear_ftrace_function(void)
16444a8a 114{
3d083395 115 ftrace_trace_function = ftrace_stub;
60a7ecf4 116 __ftrace_trace_function = ftrace_stub;
df4fc315 117 ftrace_pid_function = ftrace_stub;
3d083395
SR
118}
119
60a7ecf4
SR
120#ifndef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
121/*
122 * For those archs that do not test ftrace_trace_stop in their
123 * mcount call site, we need to do it from C.
124 */
125static void ftrace_test_stop_func(unsigned long ip, unsigned long parent_ip)
126{
127 if (function_trace_stop)
128 return;
129
130 __ftrace_trace_function(ip, parent_ip);
131}
132#endif
133
e309b41d 134static int __register_ftrace_function(struct ftrace_ops *ops)
3d083395 135{
16444a8a
ACM
136 ops->next = ftrace_list;
137 /*
138 * We are entering ops into the ftrace_list but another
139 * CPU might be walking that list. We need to make sure
140 * the ops->next pointer is valid before another CPU sees
141 * the ops pointer included into the ftrace_list.
142 */
143 smp_wmb();
144 ftrace_list = ops;
3d083395 145
b0fc494f 146 if (ftrace_enabled) {
df4fc315
SR
147 ftrace_func_t func;
148
149 if (ops->next == &ftrace_list_end)
150 func = ops->func;
151 else
152 func = ftrace_list_func;
153
978f3a45 154 if (ftrace_pid_trace) {
df4fc315
SR
155 set_ftrace_pid_function(func);
156 func = ftrace_pid_func;
157 }
158
b0fc494f
SR
159 /*
160 * For one func, simply call it directly.
161 * For more than one func, call the chain.
162 */
60a7ecf4 163#ifdef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
df4fc315 164 ftrace_trace_function = func;
60a7ecf4 165#else
df4fc315 166 __ftrace_trace_function = func;
60a7ecf4
SR
167 ftrace_trace_function = ftrace_test_stop_func;
168#endif
b0fc494f 169 }
3d083395 170
16444a8a
ACM
171 return 0;
172}
173
e309b41d 174static int __unregister_ftrace_function(struct ftrace_ops *ops)
16444a8a 175{
16444a8a 176 struct ftrace_ops **p;
16444a8a
ACM
177
178 /*
3d083395
SR
179 * If we are removing the last function, then simply point
180 * to the ftrace_stub.
16444a8a
ACM
181 */
182 if (ftrace_list == ops && ops->next == &ftrace_list_end) {
183 ftrace_trace_function = ftrace_stub;
184 ftrace_list = &ftrace_list_end;
e6ea44e9 185 return 0;
16444a8a
ACM
186 }
187
188 for (p = &ftrace_list; *p != &ftrace_list_end; p = &(*p)->next)
189 if (*p == ops)
190 break;
191
e6ea44e9
SR
192 if (*p != ops)
193 return -1;
16444a8a
ACM
194
195 *p = (*p)->next;
196
b0fc494f
SR
197 if (ftrace_enabled) {
198 /* If we only have one func left, then call that directly */
df4fc315
SR
199 if (ftrace_list->next == &ftrace_list_end) {
200 ftrace_func_t func = ftrace_list->func;
201
978f3a45 202 if (ftrace_pid_trace) {
df4fc315
SR
203 set_ftrace_pid_function(func);
204 func = ftrace_pid_func;
205 }
206#ifdef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
207 ftrace_trace_function = func;
208#else
209 __ftrace_trace_function = func;
210#endif
211 }
b0fc494f 212 }
16444a8a 213
e6ea44e9 214 return 0;
3d083395
SR
215}
216
df4fc315
SR
217static void ftrace_update_pid_func(void)
218{
219 ftrace_func_t func;
220
52baf119 221 mutex_lock(&ftrace_lock);
df4fc315
SR
222
223 if (ftrace_trace_function == ftrace_stub)
224 goto out;
225
226 func = ftrace_trace_function;
227
978f3a45 228 if (ftrace_pid_trace) {
df4fc315
SR
229 set_ftrace_pid_function(func);
230 func = ftrace_pid_func;
231 } else {
66eafebc
LW
232 if (func == ftrace_pid_func)
233 func = ftrace_pid_function;
df4fc315
SR
234 }
235
236#ifdef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
237 ftrace_trace_function = func;
238#else
239 __ftrace_trace_function = func;
240#endif
241
242 out:
52baf119 243 mutex_unlock(&ftrace_lock);
df4fc315
SR
244}
245
73d3fd96
IM
246/* set when tracing only a pid */
247struct pid *ftrace_pid_trace;
248static struct pid * const ftrace_swapper_pid = &init_struct_pid;
249
3d083395 250#ifdef CONFIG_DYNAMIC_FTRACE
73d3fd96 251
99ecdc43 252#ifndef CONFIG_FTRACE_MCOUNT_RECORD
cb7be3b2 253# error Dynamic ftrace depends on MCOUNT_RECORD
99ecdc43
SR
254#endif
255
8fc0c701
SR
256static struct hlist_head ftrace_func_hash[FTRACE_FUNC_HASHSIZE] __read_mostly;
257
b6887d79 258struct ftrace_func_probe {
8fc0c701 259 struct hlist_node node;
b6887d79 260 struct ftrace_probe_ops *ops;
8fc0c701
SR
261 unsigned long flags;
262 unsigned long ip;
263 void *data;
264 struct rcu_head rcu;
265};
266
267
d61f82d0
SR
268enum {
269 FTRACE_ENABLE_CALLS = (1 << 0),
270 FTRACE_DISABLE_CALLS = (1 << 1),
271 FTRACE_UPDATE_TRACE_FUNC = (1 << 2),
272 FTRACE_ENABLE_MCOUNT = (1 << 3),
273 FTRACE_DISABLE_MCOUNT = (1 << 4),
5a45cfe1
SR
274 FTRACE_START_FUNC_RET = (1 << 5),
275 FTRACE_STOP_FUNC_RET = (1 << 6),
d61f82d0
SR
276};
277
5072c59f
SR
278static int ftrace_filtered;
279
08f5ac90 280static LIST_HEAD(ftrace_new_addrs);
3d083395 281
41c52c0d 282static DEFINE_MUTEX(ftrace_regex_lock);
3d083395 283
3c1720f0
SR
284struct ftrace_page {
285 struct ftrace_page *next;
431aa3fb 286 int index;
3c1720f0 287 struct dyn_ftrace records[];
aa5e5cea 288};
3c1720f0
SR
289
290#define ENTRIES_PER_PAGE \
291 ((PAGE_SIZE - sizeof(struct ftrace_page)) / sizeof(struct dyn_ftrace))
292
293/* estimate from running different kernels */
294#define NR_TO_INIT 10000
295
296static struct ftrace_page *ftrace_pages_start;
297static struct ftrace_page *ftrace_pages;
298
37ad5084
SR
299static struct dyn_ftrace *ftrace_free_records;
300
265c831c
SR
301/*
302 * This is a double for. Do not use 'break' to break out of the loop,
303 * you must use a goto.
304 */
305#define do_for_each_ftrace_rec(pg, rec) \
306 for (pg = ftrace_pages_start; pg; pg = pg->next) { \
307 int _____i; \
308 for (_____i = 0; _____i < pg->index; _____i++) { \
309 rec = &pg->records[_____i];
310
311#define while_for_each_ftrace_rec() \
312 } \
313 }
ecea656d
AS
314
315#ifdef CONFIG_KPROBES
f17845e5
IM
316
317static int frozen_record_count;
318
ecea656d
AS
319static inline void freeze_record(struct dyn_ftrace *rec)
320{
321 if (!(rec->flags & FTRACE_FL_FROZEN)) {
322 rec->flags |= FTRACE_FL_FROZEN;
323 frozen_record_count++;
324 }
325}
326
327static inline void unfreeze_record(struct dyn_ftrace *rec)
328{
329 if (rec->flags & FTRACE_FL_FROZEN) {
330 rec->flags &= ~FTRACE_FL_FROZEN;
331 frozen_record_count--;
332 }
333}
334
335static inline int record_frozen(struct dyn_ftrace *rec)
336{
337 return rec->flags & FTRACE_FL_FROZEN;
338}
339#else
340# define freeze_record(rec) ({ 0; })
341# define unfreeze_record(rec) ({ 0; })
342# define record_frozen(rec) ({ 0; })
343#endif /* CONFIG_KPROBES */
344
e309b41d 345static void ftrace_free_rec(struct dyn_ftrace *rec)
37ad5084 346{
37ad5084
SR
347 rec->ip = (unsigned long)ftrace_free_records;
348 ftrace_free_records = rec;
349 rec->flags |= FTRACE_FL_FREE;
350}
351
fed1939c
SR
352void ftrace_release(void *start, unsigned long size)
353{
354 struct dyn_ftrace *rec;
355 struct ftrace_page *pg;
356 unsigned long s = (unsigned long)start;
357 unsigned long e = s + size;
fed1939c 358
00fd61ae 359 if (ftrace_disabled || !start)
fed1939c
SR
360 return;
361
52baf119 362 mutex_lock(&ftrace_lock);
265c831c
SR
363 do_for_each_ftrace_rec(pg, rec) {
364 if ((rec->ip >= s) && (rec->ip < e))
365 ftrace_free_rec(rec);
366 } while_for_each_ftrace_rec();
52baf119 367 mutex_unlock(&ftrace_lock);
fed1939c
SR
368}
369
e309b41d 370static struct dyn_ftrace *ftrace_alloc_dyn_node(unsigned long ip)
3c1720f0 371{
37ad5084
SR
372 struct dyn_ftrace *rec;
373
374 /* First check for freed records */
375 if (ftrace_free_records) {
376 rec = ftrace_free_records;
377
37ad5084 378 if (unlikely(!(rec->flags & FTRACE_FL_FREE))) {
6912896e 379 FTRACE_WARN_ON_ONCE(1);
37ad5084
SR
380 ftrace_free_records = NULL;
381 return NULL;
382 }
383
384 ftrace_free_records = (void *)rec->ip;
385 memset(rec, 0, sizeof(*rec));
386 return rec;
387 }
388
3c1720f0 389 if (ftrace_pages->index == ENTRIES_PER_PAGE) {
08f5ac90
SR
390 if (!ftrace_pages->next) {
391 /* allocate another page */
392 ftrace_pages->next =
393 (void *)get_zeroed_page(GFP_KERNEL);
394 if (!ftrace_pages->next)
395 return NULL;
396 }
3c1720f0
SR
397 ftrace_pages = ftrace_pages->next;
398 }
399
400 return &ftrace_pages->records[ftrace_pages->index++];
401}
402
08f5ac90 403static struct dyn_ftrace *
d61f82d0 404ftrace_record_ip(unsigned long ip)
3d083395 405{
08f5ac90 406 struct dyn_ftrace *rec;
3d083395 407
f3c7ac40 408 if (ftrace_disabled)
08f5ac90 409 return NULL;
3d083395 410
08f5ac90
SR
411 rec = ftrace_alloc_dyn_node(ip);
412 if (!rec)
413 return NULL;
3d083395 414
08f5ac90 415 rec->ip = ip;
3d083395 416
08f5ac90 417 list_add(&rec->list, &ftrace_new_addrs);
3d083395 418
08f5ac90 419 return rec;
3d083395
SR
420}
421
b17e8a37
SR
422static void print_ip_ins(const char *fmt, unsigned char *p)
423{
424 int i;
425
426 printk(KERN_CONT "%s", fmt);
427
428 for (i = 0; i < MCOUNT_INSN_SIZE; i++)
429 printk(KERN_CONT "%s%02x", i ? ":" : "", p[i]);
430}
431
31e88909 432static void ftrace_bug(int failed, unsigned long ip)
b17e8a37
SR
433{
434 switch (failed) {
435 case -EFAULT:
436 FTRACE_WARN_ON_ONCE(1);
437 pr_info("ftrace faulted on modifying ");
438 print_ip_sym(ip);
439 break;
440 case -EINVAL:
441 FTRACE_WARN_ON_ONCE(1);
442 pr_info("ftrace failed to modify ");
443 print_ip_sym(ip);
b17e8a37 444 print_ip_ins(" actual: ", (unsigned char *)ip);
b17e8a37
SR
445 printk(KERN_CONT "\n");
446 break;
447 case -EPERM:
448 FTRACE_WARN_ON_ONCE(1);
449 pr_info("ftrace faulted on writing ");
450 print_ip_sym(ip);
451 break;
452 default:
453 FTRACE_WARN_ON_ONCE(1);
454 pr_info("ftrace faulted on unknown error ");
455 print_ip_sym(ip);
456 }
457}
458
3c1720f0 459
0eb96701 460static int
31e88909 461__ftrace_replace_code(struct dyn_ftrace *rec, int enable)
5072c59f 462{
e7d3737e 463 unsigned long ftrace_addr;
6a24a244 464 unsigned long ip, fl;
e7d3737e 465
f0001207 466 ftrace_addr = (unsigned long)FTRACE_ADDR;
5072c59f
SR
467
468 ip = rec->ip;
469
982c350b
SR
470 /*
471 * If this record is not to be traced and
472 * it is not enabled then do nothing.
473 *
474 * If this record is not to be traced and
57794a9d 475 * it is enabled then disable it.
982c350b
SR
476 *
477 */
478 if (rec->flags & FTRACE_FL_NOTRACE) {
479 if (rec->flags & FTRACE_FL_ENABLED)
480 rec->flags &= ~FTRACE_FL_ENABLED;
481 else
482 return 0;
483
484 } else if (ftrace_filtered && enable) {
5072c59f 485 /*
982c350b 486 * Filtering is on:
5072c59f 487 */
a4500b84 488
982c350b 489 fl = rec->flags & (FTRACE_FL_FILTER | FTRACE_FL_ENABLED);
5072c59f 490
982c350b
SR
491 /* Record is filtered and enabled, do nothing */
492 if (fl == (FTRACE_FL_FILTER | FTRACE_FL_ENABLED))
0eb96701 493 return 0;
5072c59f 494
57794a9d 495 /* Record is not filtered or enabled, do nothing */
982c350b
SR
496 if (!fl)
497 return 0;
498
499 /* Record is not filtered but enabled, disable it */
500 if (fl == FTRACE_FL_ENABLED)
5072c59f 501 rec->flags &= ~FTRACE_FL_ENABLED;
982c350b
SR
502 else
503 /* Otherwise record is filtered but not enabled, enable it */
5072c59f 504 rec->flags |= FTRACE_FL_ENABLED;
5072c59f 505 } else {
982c350b 506 /* Disable or not filtered */
5072c59f 507
41c52c0d 508 if (enable) {
982c350b 509 /* if record is enabled, do nothing */
5072c59f 510 if (rec->flags & FTRACE_FL_ENABLED)
0eb96701 511 return 0;
982c350b 512
5072c59f 513 rec->flags |= FTRACE_FL_ENABLED;
982c350b 514
5072c59f 515 } else {
982c350b 516
57794a9d 517 /* if record is not enabled, do nothing */
5072c59f 518 if (!(rec->flags & FTRACE_FL_ENABLED))
0eb96701 519 return 0;
982c350b 520
5072c59f
SR
521 rec->flags &= ~FTRACE_FL_ENABLED;
522 }
523 }
524
982c350b 525 if (rec->flags & FTRACE_FL_ENABLED)
e7d3737e 526 return ftrace_make_call(rec, ftrace_addr);
31e88909 527 else
e7d3737e 528 return ftrace_make_nop(NULL, rec, ftrace_addr);
5072c59f
SR
529}
530
e309b41d 531static void ftrace_replace_code(int enable)
3c1720f0 532{
3c1720f0
SR
533 struct dyn_ftrace *rec;
534 struct ftrace_page *pg;
6a24a244 535 int failed;
3c1720f0 536
265c831c
SR
537 do_for_each_ftrace_rec(pg, rec) {
538 /*
539 * Skip over free records and records that have
540 * failed.
541 */
542 if (rec->flags & FTRACE_FL_FREE ||
543 rec->flags & FTRACE_FL_FAILED)
544 continue;
545
546 /* ignore updates to this record's mcount site */
547 if (get_kprobe((void *)rec->ip)) {
548 freeze_record(rec);
549 continue;
550 } else {
551 unfreeze_record(rec);
552 }
f22f9a89 553
265c831c
SR
554 failed = __ftrace_replace_code(rec, enable);
555 if (failed && (rec->flags & FTRACE_FL_CONVERTED)) {
556 rec->flags |= FTRACE_FL_FAILED;
557 if ((system_state == SYSTEM_BOOTING) ||
558 !core_kernel_text(rec->ip)) {
559 ftrace_free_rec(rec);
560 } else
561 ftrace_bug(failed, rec->ip);
3c1720f0 562 }
265c831c 563 } while_for_each_ftrace_rec();
3c1720f0
SR
564}
565
492a7ea5 566static int
31e88909 567ftrace_code_disable(struct module *mod, struct dyn_ftrace *rec)
3c1720f0
SR
568{
569 unsigned long ip;
593eb8a2 570 int ret;
3c1720f0
SR
571
572 ip = rec->ip;
573
25aac9dc 574 ret = ftrace_make_nop(mod, rec, MCOUNT_ADDR);
593eb8a2 575 if (ret) {
31e88909 576 ftrace_bug(ret, ip);
3c1720f0 577 rec->flags |= FTRACE_FL_FAILED;
492a7ea5 578 return 0;
37ad5084 579 }
492a7ea5 580 return 1;
3c1720f0
SR
581}
582
e309b41d 583static int __ftrace_modify_code(void *data)
3d083395 584{
d61f82d0
SR
585 int *command = data;
586
a3583244 587 if (*command & FTRACE_ENABLE_CALLS)
d61f82d0 588 ftrace_replace_code(1);
a3583244 589 else if (*command & FTRACE_DISABLE_CALLS)
d61f82d0
SR
590 ftrace_replace_code(0);
591
592 if (*command & FTRACE_UPDATE_TRACE_FUNC)
593 ftrace_update_ftrace_func(ftrace_trace_function);
594
5a45cfe1
SR
595 if (*command & FTRACE_START_FUNC_RET)
596 ftrace_enable_ftrace_graph_caller();
597 else if (*command & FTRACE_STOP_FUNC_RET)
598 ftrace_disable_ftrace_graph_caller();
599
d61f82d0 600 return 0;
3d083395
SR
601}
602
e309b41d 603static void ftrace_run_update_code(int command)
3d083395 604{
784e2d76 605 stop_machine(__ftrace_modify_code, &command, NULL);
3d083395
SR
606}
607
d61f82d0 608static ftrace_func_t saved_ftrace_func;
60a7ecf4 609static int ftrace_start_up;
df4fc315
SR
610
611static void ftrace_startup_enable(int command)
612{
613 if (saved_ftrace_func != ftrace_trace_function) {
614 saved_ftrace_func = ftrace_trace_function;
615 command |= FTRACE_UPDATE_TRACE_FUNC;
616 }
617
618 if (!command || !ftrace_enabled)
619 return;
620
621 ftrace_run_update_code(command);
622}
d61f82d0 623
5a45cfe1 624static void ftrace_startup(int command)
3d083395 625{
4eebcc81
SR
626 if (unlikely(ftrace_disabled))
627 return;
628
60a7ecf4 629 ftrace_start_up++;
982c350b 630 command |= FTRACE_ENABLE_CALLS;
d61f82d0 631
df4fc315 632 ftrace_startup_enable(command);
3d083395
SR
633}
634
5a45cfe1 635static void ftrace_shutdown(int command)
3d083395 636{
4eebcc81
SR
637 if (unlikely(ftrace_disabled))
638 return;
639
60a7ecf4
SR
640 ftrace_start_up--;
641 if (!ftrace_start_up)
d61f82d0 642 command |= FTRACE_DISABLE_CALLS;
3d083395 643
d61f82d0
SR
644 if (saved_ftrace_func != ftrace_trace_function) {
645 saved_ftrace_func = ftrace_trace_function;
646 command |= FTRACE_UPDATE_TRACE_FUNC;
647 }
3d083395 648
d61f82d0 649 if (!command || !ftrace_enabled)
e6ea44e9 650 return;
d61f82d0
SR
651
652 ftrace_run_update_code(command);
3d083395
SR
653}
654
e309b41d 655static void ftrace_startup_sysctl(void)
b0fc494f 656{
d61f82d0
SR
657 int command = FTRACE_ENABLE_MCOUNT;
658
4eebcc81
SR
659 if (unlikely(ftrace_disabled))
660 return;
661
d61f82d0
SR
662 /* Force update next time */
663 saved_ftrace_func = NULL;
60a7ecf4
SR
664 /* ftrace_start_up is true if we want ftrace running */
665 if (ftrace_start_up)
d61f82d0
SR
666 command |= FTRACE_ENABLE_CALLS;
667
668 ftrace_run_update_code(command);
b0fc494f
SR
669}
670
e309b41d 671static void ftrace_shutdown_sysctl(void)
b0fc494f 672{
d61f82d0
SR
673 int command = FTRACE_DISABLE_MCOUNT;
674
4eebcc81
SR
675 if (unlikely(ftrace_disabled))
676 return;
677
60a7ecf4
SR
678 /* ftrace_start_up is true if ftrace is running */
679 if (ftrace_start_up)
d61f82d0
SR
680 command |= FTRACE_DISABLE_CALLS;
681
682 ftrace_run_update_code(command);
b0fc494f
SR
683}
684
3d083395
SR
685static cycle_t ftrace_update_time;
686static unsigned long ftrace_update_cnt;
687unsigned long ftrace_update_tot_cnt;
688
31e88909 689static int ftrace_update_code(struct module *mod)
3d083395 690{
08f5ac90 691 struct dyn_ftrace *p, *t;
f22f9a89 692 cycle_t start, stop;
3d083395 693
750ed1a4 694 start = ftrace_now(raw_smp_processor_id());
3d083395
SR
695 ftrace_update_cnt = 0;
696
08f5ac90 697 list_for_each_entry_safe(p, t, &ftrace_new_addrs, list) {
3d083395 698
08f5ac90
SR
699 /* If something went wrong, bail without enabling anything */
700 if (unlikely(ftrace_disabled))
701 return -1;
f22f9a89 702
08f5ac90 703 list_del_init(&p->list);
f22f9a89 704
08f5ac90 705 /* convert record (i.e, patch mcount-call with NOP) */
31e88909 706 if (ftrace_code_disable(mod, p)) {
08f5ac90
SR
707 p->flags |= FTRACE_FL_CONVERTED;
708 ftrace_update_cnt++;
709 } else
710 ftrace_free_rec(p);
3d083395
SR
711 }
712
750ed1a4 713 stop = ftrace_now(raw_smp_processor_id());
3d083395
SR
714 ftrace_update_time = stop - start;
715 ftrace_update_tot_cnt += ftrace_update_cnt;
716
16444a8a
ACM
717 return 0;
718}
719
68bf21aa 720static int __init ftrace_dyn_table_alloc(unsigned long num_to_init)
3c1720f0
SR
721{
722 struct ftrace_page *pg;
723 int cnt;
724 int i;
3c1720f0
SR
725
726 /* allocate a few pages */
727 ftrace_pages_start = (void *)get_zeroed_page(GFP_KERNEL);
728 if (!ftrace_pages_start)
729 return -1;
730
731 /*
732 * Allocate a few more pages.
733 *
734 * TODO: have some parser search vmlinux before
735 * final linking to find all calls to ftrace.
736 * Then we can:
737 * a) know how many pages to allocate.
738 * and/or
739 * b) set up the table then.
740 *
741 * The dynamic code is still necessary for
742 * modules.
743 */
744
745 pg = ftrace_pages = ftrace_pages_start;
746
68bf21aa 747 cnt = num_to_init / ENTRIES_PER_PAGE;
08f5ac90 748 pr_info("ftrace: allocating %ld entries in %d pages\n",
5821e1b7 749 num_to_init, cnt + 1);
3c1720f0
SR
750
751 for (i = 0; i < cnt; i++) {
752 pg->next = (void *)get_zeroed_page(GFP_KERNEL);
753
754 /* If we fail, we'll try later anyway */
755 if (!pg->next)
756 break;
757
758 pg = pg->next;
759 }
760
761 return 0;
762}
763
5072c59f
SR
764enum {
765 FTRACE_ITER_FILTER = (1 << 0),
766 FTRACE_ITER_CONT = (1 << 1),
41c52c0d 767 FTRACE_ITER_NOTRACE = (1 << 2),
eb9a7bf0 768 FTRACE_ITER_FAILURES = (1 << 3),
0c75a3ed 769 FTRACE_ITER_PRINTALL = (1 << 4),
8fc0c701 770 FTRACE_ITER_HASH = (1 << 5),
5072c59f
SR
771};
772
773#define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
774
775struct ftrace_iterator {
5072c59f 776 struct ftrace_page *pg;
8fc0c701 777 int hidx;
431aa3fb 778 int idx;
5072c59f
SR
779 unsigned flags;
780 unsigned char buffer[FTRACE_BUFF_MAX+1];
781 unsigned buffer_idx;
782 unsigned filtered;
783};
784
8fc0c701
SR
785static void *
786t_hash_next(struct seq_file *m, void *v, loff_t *pos)
787{
788 struct ftrace_iterator *iter = m->private;
789 struct hlist_node *hnd = v;
790 struct hlist_head *hhd;
791
792 WARN_ON(!(iter->flags & FTRACE_ITER_HASH));
793
794 (*pos)++;
795
796 retry:
797 if (iter->hidx >= FTRACE_FUNC_HASHSIZE)
798 return NULL;
799
800 hhd = &ftrace_func_hash[iter->hidx];
801
802 if (hlist_empty(hhd)) {
803 iter->hidx++;
804 hnd = NULL;
805 goto retry;
806 }
807
808 if (!hnd)
809 hnd = hhd->first;
810 else {
811 hnd = hnd->next;
812 if (!hnd) {
813 iter->hidx++;
814 goto retry;
815 }
816 }
817
818 return hnd;
819}
820
821static void *t_hash_start(struct seq_file *m, loff_t *pos)
822{
823 struct ftrace_iterator *iter = m->private;
824 void *p = NULL;
825
826 iter->flags |= FTRACE_ITER_HASH;
827
828 return t_hash_next(m, p, pos);
829}
830
831static int t_hash_show(struct seq_file *m, void *v)
832{
b6887d79 833 struct ftrace_func_probe *rec;
8fc0c701
SR
834 struct hlist_node *hnd = v;
835 char str[KSYM_SYMBOL_LEN];
836
b6887d79 837 rec = hlist_entry(hnd, struct ftrace_func_probe, node);
8fc0c701 838
809dcf29
SR
839 if (rec->ops->print)
840 return rec->ops->print(m, rec->ip, rec->ops, rec->data);
841
8fc0c701
SR
842 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
843 seq_printf(m, "%s:", str);
844
845 kallsyms_lookup((unsigned long)rec->ops->func, NULL, NULL, NULL, str);
846 seq_printf(m, "%s", str);
847
848 if (rec->data)
849 seq_printf(m, ":%p", rec->data);
850 seq_putc(m, '\n');
851
852 return 0;
853}
854
e309b41d 855static void *
5072c59f
SR
856t_next(struct seq_file *m, void *v, loff_t *pos)
857{
858 struct ftrace_iterator *iter = m->private;
859 struct dyn_ftrace *rec = NULL;
860
8fc0c701
SR
861 if (iter->flags & FTRACE_ITER_HASH)
862 return t_hash_next(m, v, pos);
863
5072c59f
SR
864 (*pos)++;
865
0c75a3ed
SR
866 if (iter->flags & FTRACE_ITER_PRINTALL)
867 return NULL;
868
5072c59f
SR
869 retry:
870 if (iter->idx >= iter->pg->index) {
871 if (iter->pg->next) {
872 iter->pg = iter->pg->next;
873 iter->idx = 0;
874 goto retry;
50cdaf08
LW
875 } else {
876 iter->idx = -1;
5072c59f
SR
877 }
878 } else {
879 rec = &iter->pg->records[iter->idx++];
a9fdda33
SR
880 if ((rec->flags & FTRACE_FL_FREE) ||
881
882 (!(iter->flags & FTRACE_ITER_FAILURES) &&
eb9a7bf0
AS
883 (rec->flags & FTRACE_FL_FAILED)) ||
884
885 ((iter->flags & FTRACE_ITER_FAILURES) &&
a9fdda33 886 !(rec->flags & FTRACE_FL_FAILED)) ||
eb9a7bf0 887
0183fb1c
SR
888 ((iter->flags & FTRACE_ITER_FILTER) &&
889 !(rec->flags & FTRACE_FL_FILTER)) ||
890
41c52c0d
SR
891 ((iter->flags & FTRACE_ITER_NOTRACE) &&
892 !(rec->flags & FTRACE_FL_NOTRACE))) {
5072c59f
SR
893 rec = NULL;
894 goto retry;
895 }
896 }
897
5072c59f
SR
898 return rec;
899}
900
901static void *t_start(struct seq_file *m, loff_t *pos)
902{
903 struct ftrace_iterator *iter = m->private;
904 void *p = NULL;
5072c59f 905
8fc0c701 906 mutex_lock(&ftrace_lock);
0c75a3ed
SR
907 /*
908 * For set_ftrace_filter reading, if we have the filter
909 * off, we can short cut and just print out that all
910 * functions are enabled.
911 */
912 if (iter->flags & FTRACE_ITER_FILTER && !ftrace_filtered) {
913 if (*pos > 0)
8fc0c701 914 return t_hash_start(m, pos);
0c75a3ed
SR
915 iter->flags |= FTRACE_ITER_PRINTALL;
916 (*pos)++;
917 return iter;
918 }
919
8fc0c701
SR
920 if (iter->flags & FTRACE_ITER_HASH)
921 return t_hash_start(m, pos);
922
50cdaf08
LW
923 if (*pos > 0) {
924 if (iter->idx < 0)
925 return p;
926 (*pos)--;
927 iter->idx--;
928 }
5821e1b7 929
50cdaf08 930 p = t_next(m, p, pos);
5072c59f 931
8fc0c701
SR
932 if (!p)
933 return t_hash_start(m, pos);
934
5072c59f
SR
935 return p;
936}
937
938static void t_stop(struct seq_file *m, void *p)
939{
8fc0c701 940 mutex_unlock(&ftrace_lock);
5072c59f
SR
941}
942
943static int t_show(struct seq_file *m, void *v)
944{
0c75a3ed 945 struct ftrace_iterator *iter = m->private;
5072c59f
SR
946 struct dyn_ftrace *rec = v;
947 char str[KSYM_SYMBOL_LEN];
948
8fc0c701
SR
949 if (iter->flags & FTRACE_ITER_HASH)
950 return t_hash_show(m, v);
951
0c75a3ed
SR
952 if (iter->flags & FTRACE_ITER_PRINTALL) {
953 seq_printf(m, "#### all functions enabled ####\n");
954 return 0;
955 }
956
5072c59f
SR
957 if (!rec)
958 return 0;
959
960 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
961
50cdaf08 962 seq_printf(m, "%s\n", str);
5072c59f
SR
963
964 return 0;
965}
966
967static struct seq_operations show_ftrace_seq_ops = {
968 .start = t_start,
969 .next = t_next,
970 .stop = t_stop,
971 .show = t_show,
972};
973
e309b41d 974static int
5072c59f
SR
975ftrace_avail_open(struct inode *inode, struct file *file)
976{
977 struct ftrace_iterator *iter;
978 int ret;
979
4eebcc81
SR
980 if (unlikely(ftrace_disabled))
981 return -ENODEV;
982
5072c59f
SR
983 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
984 if (!iter)
985 return -ENOMEM;
986
987 iter->pg = ftrace_pages_start;
5072c59f
SR
988
989 ret = seq_open(file, &show_ftrace_seq_ops);
990 if (!ret) {
991 struct seq_file *m = file->private_data;
4bf39a94 992
5072c59f 993 m->private = iter;
4bf39a94 994 } else {
5072c59f 995 kfree(iter);
4bf39a94 996 }
5072c59f
SR
997
998 return ret;
999}
1000
1001int ftrace_avail_release(struct inode *inode, struct file *file)
1002{
1003 struct seq_file *m = (struct seq_file *)file->private_data;
1004 struct ftrace_iterator *iter = m->private;
1005
1006 seq_release(inode, file);
1007 kfree(iter);
4bf39a94 1008
5072c59f
SR
1009 return 0;
1010}
1011
eb9a7bf0
AS
1012static int
1013ftrace_failures_open(struct inode *inode, struct file *file)
1014{
1015 int ret;
1016 struct seq_file *m;
1017 struct ftrace_iterator *iter;
1018
1019 ret = ftrace_avail_open(inode, file);
1020 if (!ret) {
1021 m = (struct seq_file *)file->private_data;
1022 iter = (struct ftrace_iterator *)m->private;
1023 iter->flags = FTRACE_ITER_FAILURES;
1024 }
1025
1026 return ret;
1027}
1028
1029
41c52c0d 1030static void ftrace_filter_reset(int enable)
5072c59f
SR
1031{
1032 struct ftrace_page *pg;
1033 struct dyn_ftrace *rec;
41c52c0d 1034 unsigned long type = enable ? FTRACE_FL_FILTER : FTRACE_FL_NOTRACE;
5072c59f 1035
52baf119 1036 mutex_lock(&ftrace_lock);
41c52c0d
SR
1037 if (enable)
1038 ftrace_filtered = 0;
265c831c
SR
1039 do_for_each_ftrace_rec(pg, rec) {
1040 if (rec->flags & FTRACE_FL_FAILED)
1041 continue;
1042 rec->flags &= ~type;
1043 } while_for_each_ftrace_rec();
52baf119 1044 mutex_unlock(&ftrace_lock);
5072c59f
SR
1045}
1046
e309b41d 1047static int
41c52c0d 1048ftrace_regex_open(struct inode *inode, struct file *file, int enable)
5072c59f
SR
1049{
1050 struct ftrace_iterator *iter;
1051 int ret = 0;
1052
4eebcc81
SR
1053 if (unlikely(ftrace_disabled))
1054 return -ENODEV;
1055
5072c59f
SR
1056 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
1057 if (!iter)
1058 return -ENOMEM;
1059
41c52c0d 1060 mutex_lock(&ftrace_regex_lock);
5072c59f
SR
1061 if ((file->f_mode & FMODE_WRITE) &&
1062 !(file->f_flags & O_APPEND))
41c52c0d 1063 ftrace_filter_reset(enable);
5072c59f
SR
1064
1065 if (file->f_mode & FMODE_READ) {
1066 iter->pg = ftrace_pages_start;
41c52c0d
SR
1067 iter->flags = enable ? FTRACE_ITER_FILTER :
1068 FTRACE_ITER_NOTRACE;
5072c59f
SR
1069
1070 ret = seq_open(file, &show_ftrace_seq_ops);
1071 if (!ret) {
1072 struct seq_file *m = file->private_data;
1073 m->private = iter;
1074 } else
1075 kfree(iter);
1076 } else
1077 file->private_data = iter;
41c52c0d 1078 mutex_unlock(&ftrace_regex_lock);
5072c59f
SR
1079
1080 return ret;
1081}
1082
41c52c0d
SR
1083static int
1084ftrace_filter_open(struct inode *inode, struct file *file)
1085{
1086 return ftrace_regex_open(inode, file, 1);
1087}
1088
1089static int
1090ftrace_notrace_open(struct inode *inode, struct file *file)
1091{
1092 return ftrace_regex_open(inode, file, 0);
1093}
1094
e309b41d 1095static ssize_t
41c52c0d 1096ftrace_regex_read(struct file *file, char __user *ubuf,
5072c59f
SR
1097 size_t cnt, loff_t *ppos)
1098{
1099 if (file->f_mode & FMODE_READ)
1100 return seq_read(file, ubuf, cnt, ppos);
1101 else
1102 return -EPERM;
1103}
1104
e309b41d 1105static loff_t
41c52c0d 1106ftrace_regex_lseek(struct file *file, loff_t offset, int origin)
5072c59f
SR
1107{
1108 loff_t ret;
1109
1110 if (file->f_mode & FMODE_READ)
1111 ret = seq_lseek(file, offset, origin);
1112 else
1113 file->f_pos = ret = 1;
1114
1115 return ret;
1116}
1117
1118enum {
1119 MATCH_FULL,
1120 MATCH_FRONT_ONLY,
1121 MATCH_MIDDLE_ONLY,
1122 MATCH_END_ONLY,
1123};
1124
9f4801e3
SR
1125/*
1126 * (static function - no need for kernel doc)
1127 *
1128 * Pass in a buffer containing a glob and this function will
1129 * set search to point to the search part of the buffer and
1130 * return the type of search it is (see enum above).
1131 * This does modify buff.
1132 *
1133 * Returns enum type.
1134 * search returns the pointer to use for comparison.
1135 * not returns 1 if buff started with a '!'
1136 * 0 otherwise.
1137 */
1138static int
64e7c440 1139ftrace_setup_glob(char *buff, int len, char **search, int *not)
5072c59f 1140{
5072c59f 1141 int type = MATCH_FULL;
9f4801e3 1142 int i;
ea3a6d6d
SR
1143
1144 if (buff[0] == '!') {
9f4801e3 1145 *not = 1;
ea3a6d6d
SR
1146 buff++;
1147 len--;
9f4801e3
SR
1148 } else
1149 *not = 0;
1150
1151 *search = buff;
5072c59f
SR
1152
1153 for (i = 0; i < len; i++) {
1154 if (buff[i] == '*') {
1155 if (!i) {
9f4801e3 1156 *search = buff + 1;
5072c59f 1157 type = MATCH_END_ONLY;
5072c59f 1158 } else {
9f4801e3 1159 if (type == MATCH_END_ONLY)
5072c59f 1160 type = MATCH_MIDDLE_ONLY;
9f4801e3 1161 else
5072c59f 1162 type = MATCH_FRONT_ONLY;
5072c59f
SR
1163 buff[i] = 0;
1164 break;
1165 }
1166 }
1167 }
1168
9f4801e3
SR
1169 return type;
1170}
1171
64e7c440 1172static int ftrace_match(char *str, char *regex, int len, int type)
9f4801e3 1173{
9f4801e3
SR
1174 int matched = 0;
1175 char *ptr;
1176
9f4801e3
SR
1177 switch (type) {
1178 case MATCH_FULL:
1179 if (strcmp(str, regex) == 0)
1180 matched = 1;
1181 break;
1182 case MATCH_FRONT_ONLY:
1183 if (strncmp(str, regex, len) == 0)
1184 matched = 1;
1185 break;
1186 case MATCH_MIDDLE_ONLY:
1187 if (strstr(str, regex))
1188 matched = 1;
1189 break;
1190 case MATCH_END_ONLY:
1191 ptr = strstr(str, regex);
1192 if (ptr && (ptr[len] == 0))
1193 matched = 1;
1194 break;
1195 }
1196
1197 return matched;
1198}
1199
64e7c440
SR
1200static int
1201ftrace_match_record(struct dyn_ftrace *rec, char *regex, int len, int type)
1202{
1203 char str[KSYM_SYMBOL_LEN];
1204
1205 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
1206 return ftrace_match(str, regex, len, type);
1207}
1208
9f4801e3
SR
1209static void ftrace_match_records(char *buff, int len, int enable)
1210{
6a24a244 1211 unsigned int search_len;
9f4801e3
SR
1212 struct ftrace_page *pg;
1213 struct dyn_ftrace *rec;
6a24a244
SR
1214 unsigned long flag;
1215 char *search;
9f4801e3 1216 int type;
9f4801e3
SR
1217 int not;
1218
6a24a244 1219 flag = enable ? FTRACE_FL_FILTER : FTRACE_FL_NOTRACE;
9f4801e3
SR
1220 type = ftrace_setup_glob(buff, len, &search, &not);
1221
1222 search_len = strlen(search);
1223
52baf119 1224 mutex_lock(&ftrace_lock);
265c831c 1225 do_for_each_ftrace_rec(pg, rec) {
265c831c
SR
1226
1227 if (rec->flags & FTRACE_FL_FAILED)
1228 continue;
9f4801e3
SR
1229
1230 if (ftrace_match_record(rec, search, search_len, type)) {
265c831c
SR
1231 if (not)
1232 rec->flags &= ~flag;
1233 else
1234 rec->flags |= flag;
1235 }
e68746a2
SR
1236 /*
1237 * Only enable filtering if we have a function that
1238 * is filtered on.
1239 */
1240 if (enable && (rec->flags & FTRACE_FL_FILTER))
1241 ftrace_filtered = 1;
265c831c 1242 } while_for_each_ftrace_rec();
52baf119 1243 mutex_unlock(&ftrace_lock);
5072c59f
SR
1244}
1245
64e7c440
SR
1246static int
1247ftrace_match_module_record(struct dyn_ftrace *rec, char *mod,
1248 char *regex, int len, int type)
1249{
1250 char str[KSYM_SYMBOL_LEN];
1251 char *modname;
1252
1253 kallsyms_lookup(rec->ip, NULL, NULL, &modname, str);
1254
1255 if (!modname || strcmp(modname, mod))
1256 return 0;
1257
1258 /* blank search means to match all funcs in the mod */
1259 if (len)
1260 return ftrace_match(str, regex, len, type);
1261 else
1262 return 1;
1263}
1264
1265static void ftrace_match_module_records(char *buff, char *mod, int enable)
1266{
6a24a244 1267 unsigned search_len = 0;
64e7c440
SR
1268 struct ftrace_page *pg;
1269 struct dyn_ftrace *rec;
1270 int type = MATCH_FULL;
6a24a244
SR
1271 char *search = buff;
1272 unsigned long flag;
64e7c440
SR
1273 int not = 0;
1274
6a24a244
SR
1275 flag = enable ? FTRACE_FL_FILTER : FTRACE_FL_NOTRACE;
1276
64e7c440
SR
1277 /* blank or '*' mean the same */
1278 if (strcmp(buff, "*") == 0)
1279 buff[0] = 0;
1280
1281 /* handle the case of 'dont filter this module' */
1282 if (strcmp(buff, "!") == 0 || strcmp(buff, "!*") == 0) {
1283 buff[0] = 0;
1284 not = 1;
1285 }
1286
1287 if (strlen(buff)) {
1288 type = ftrace_setup_glob(buff, strlen(buff), &search, &not);
1289 search_len = strlen(search);
1290 }
1291
52baf119 1292 mutex_lock(&ftrace_lock);
64e7c440
SR
1293 do_for_each_ftrace_rec(pg, rec) {
1294
1295 if (rec->flags & FTRACE_FL_FAILED)
1296 continue;
1297
1298 if (ftrace_match_module_record(rec, mod,
1299 search, search_len, type)) {
1300 if (not)
1301 rec->flags &= ~flag;
1302 else
1303 rec->flags |= flag;
1304 }
e68746a2
SR
1305 if (enable && (rec->flags & FTRACE_FL_FILTER))
1306 ftrace_filtered = 1;
64e7c440
SR
1307
1308 } while_for_each_ftrace_rec();
52baf119 1309 mutex_unlock(&ftrace_lock);
64e7c440
SR
1310}
1311
f6180773
SR
1312/*
1313 * We register the module command as a template to show others how
1314 * to register the a command as well.
1315 */
1316
1317static int
1318ftrace_mod_callback(char *func, char *cmd, char *param, int enable)
1319{
1320 char *mod;
1321
1322 /*
1323 * cmd == 'mod' because we only registered this func
1324 * for the 'mod' ftrace_func_command.
1325 * But if you register one func with multiple commands,
1326 * you can tell which command was used by the cmd
1327 * parameter.
1328 */
1329
1330 /* we must have a module name */
1331 if (!param)
1332 return -EINVAL;
1333
1334 mod = strsep(&param, ":");
1335 if (!strlen(mod))
1336 return -EINVAL;
1337
1338 ftrace_match_module_records(func, mod, enable);
1339 return 0;
1340}
1341
1342static struct ftrace_func_command ftrace_mod_cmd = {
1343 .name = "mod",
1344 .func = ftrace_mod_callback,
1345};
1346
1347static int __init ftrace_mod_cmd_init(void)
1348{
1349 return register_ftrace_command(&ftrace_mod_cmd);
1350}
1351device_initcall(ftrace_mod_cmd_init);
1352
59df055f 1353static void
b6887d79 1354function_trace_probe_call(unsigned long ip, unsigned long parent_ip)
59df055f 1355{
b6887d79 1356 struct ftrace_func_probe *entry;
59df055f
SR
1357 struct hlist_head *hhd;
1358 struct hlist_node *n;
1359 unsigned long key;
1360 int resched;
1361
1362 key = hash_long(ip, FTRACE_HASH_BITS);
1363
1364 hhd = &ftrace_func_hash[key];
1365
1366 if (hlist_empty(hhd))
1367 return;
1368
1369 /*
1370 * Disable preemption for these calls to prevent a RCU grace
1371 * period. This syncs the hash iteration and freeing of items
1372 * on the hash. rcu_read_lock is too dangerous here.
1373 */
1374 resched = ftrace_preempt_disable();
1375 hlist_for_each_entry_rcu(entry, n, hhd, node) {
1376 if (entry->ip == ip)
1377 entry->ops->func(ip, parent_ip, &entry->data);
1378 }
1379 ftrace_preempt_enable(resched);
1380}
1381
b6887d79 1382static struct ftrace_ops trace_probe_ops __read_mostly =
59df055f 1383{
b6887d79 1384 .func = function_trace_probe_call,
59df055f
SR
1385};
1386
b6887d79 1387static int ftrace_probe_registered;
59df055f 1388
b6887d79 1389static void __enable_ftrace_function_probe(void)
59df055f
SR
1390{
1391 int i;
1392
b6887d79 1393 if (ftrace_probe_registered)
59df055f
SR
1394 return;
1395
1396 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
1397 struct hlist_head *hhd = &ftrace_func_hash[i];
1398 if (hhd->first)
1399 break;
1400 }
1401 /* Nothing registered? */
1402 if (i == FTRACE_FUNC_HASHSIZE)
1403 return;
1404
b6887d79 1405 __register_ftrace_function(&trace_probe_ops);
59df055f 1406 ftrace_startup(0);
b6887d79 1407 ftrace_probe_registered = 1;
59df055f
SR
1408}
1409
b6887d79 1410static void __disable_ftrace_function_probe(void)
59df055f
SR
1411{
1412 int i;
1413
b6887d79 1414 if (!ftrace_probe_registered)
59df055f
SR
1415 return;
1416
1417 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
1418 struct hlist_head *hhd = &ftrace_func_hash[i];
1419 if (hhd->first)
1420 return;
1421 }
1422
1423 /* no more funcs left */
b6887d79 1424 __unregister_ftrace_function(&trace_probe_ops);
59df055f 1425 ftrace_shutdown(0);
b6887d79 1426 ftrace_probe_registered = 0;
59df055f
SR
1427}
1428
1429
1430static void ftrace_free_entry_rcu(struct rcu_head *rhp)
1431{
b6887d79
SR
1432 struct ftrace_func_probe *entry =
1433 container_of(rhp, struct ftrace_func_probe, rcu);
59df055f
SR
1434
1435 if (entry->ops->free)
1436 entry->ops->free(&entry->data);
1437 kfree(entry);
1438}
1439
1440
1441int
b6887d79 1442register_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
59df055f
SR
1443 void *data)
1444{
b6887d79 1445 struct ftrace_func_probe *entry;
59df055f
SR
1446 struct ftrace_page *pg;
1447 struct dyn_ftrace *rec;
59df055f 1448 int type, len, not;
6a24a244 1449 unsigned long key;
59df055f
SR
1450 int count = 0;
1451 char *search;
1452
1453 type = ftrace_setup_glob(glob, strlen(glob), &search, &not);
1454 len = strlen(search);
1455
b6887d79 1456 /* we do not support '!' for function probes */
59df055f
SR
1457 if (WARN_ON(not))
1458 return -EINVAL;
1459
1460 mutex_lock(&ftrace_lock);
1461 do_for_each_ftrace_rec(pg, rec) {
1462
1463 if (rec->flags & FTRACE_FL_FAILED)
1464 continue;
1465
1466 if (!ftrace_match_record(rec, search, len, type))
1467 continue;
1468
1469 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
1470 if (!entry) {
b6887d79 1471 /* If we did not process any, then return error */
59df055f
SR
1472 if (!count)
1473 count = -ENOMEM;
1474 goto out_unlock;
1475 }
1476
1477 count++;
1478
1479 entry->data = data;
1480
1481 /*
1482 * The caller might want to do something special
1483 * for each function we find. We call the callback
1484 * to give the caller an opportunity to do so.
1485 */
1486 if (ops->callback) {
1487 if (ops->callback(rec->ip, &entry->data) < 0) {
1488 /* caller does not like this func */
1489 kfree(entry);
1490 continue;
1491 }
1492 }
1493
1494 entry->ops = ops;
1495 entry->ip = rec->ip;
1496
1497 key = hash_long(entry->ip, FTRACE_HASH_BITS);
1498 hlist_add_head_rcu(&entry->node, &ftrace_func_hash[key]);
1499
1500 } while_for_each_ftrace_rec();
b6887d79 1501 __enable_ftrace_function_probe();
59df055f
SR
1502
1503 out_unlock:
1504 mutex_unlock(&ftrace_lock);
1505
1506 return count;
1507}
1508
1509enum {
b6887d79
SR
1510 PROBE_TEST_FUNC = 1,
1511 PROBE_TEST_DATA = 2
59df055f
SR
1512};
1513
1514static void
b6887d79 1515__unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
59df055f
SR
1516 void *data, int flags)
1517{
b6887d79 1518 struct ftrace_func_probe *entry;
59df055f
SR
1519 struct hlist_node *n, *tmp;
1520 char str[KSYM_SYMBOL_LEN];
1521 int type = MATCH_FULL;
1522 int i, len = 0;
1523 char *search;
1524
1525 if (glob && (strcmp(glob, "*") || !strlen(glob)))
1526 glob = NULL;
1527 else {
1528 int not;
1529
1530 type = ftrace_setup_glob(glob, strlen(glob), &search, &not);
1531 len = strlen(search);
1532
b6887d79 1533 /* we do not support '!' for function probes */
59df055f
SR
1534 if (WARN_ON(not))
1535 return;
1536 }
1537
1538 mutex_lock(&ftrace_lock);
1539 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
1540 struct hlist_head *hhd = &ftrace_func_hash[i];
1541
1542 hlist_for_each_entry_safe(entry, n, tmp, hhd, node) {
1543
1544 /* break up if statements for readability */
b6887d79 1545 if ((flags & PROBE_TEST_FUNC) && entry->ops != ops)
59df055f
SR
1546 continue;
1547
b6887d79 1548 if ((flags & PROBE_TEST_DATA) && entry->data != data)
59df055f
SR
1549 continue;
1550
1551 /* do this last, since it is the most expensive */
1552 if (glob) {
1553 kallsyms_lookup(entry->ip, NULL, NULL,
1554 NULL, str);
1555 if (!ftrace_match(str, glob, len, type))
1556 continue;
1557 }
1558
1559 hlist_del(&entry->node);
1560 call_rcu(&entry->rcu, ftrace_free_entry_rcu);
1561 }
1562 }
b6887d79 1563 __disable_ftrace_function_probe();
59df055f
SR
1564 mutex_unlock(&ftrace_lock);
1565}
1566
1567void
b6887d79 1568unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
59df055f
SR
1569 void *data)
1570{
b6887d79
SR
1571 __unregister_ftrace_function_probe(glob, ops, data,
1572 PROBE_TEST_FUNC | PROBE_TEST_DATA);
59df055f
SR
1573}
1574
1575void
b6887d79 1576unregister_ftrace_function_probe_func(char *glob, struct ftrace_probe_ops *ops)
59df055f 1577{
b6887d79 1578 __unregister_ftrace_function_probe(glob, ops, NULL, PROBE_TEST_FUNC);
59df055f
SR
1579}
1580
b6887d79 1581void unregister_ftrace_function_probe_all(char *glob)
59df055f 1582{
b6887d79 1583 __unregister_ftrace_function_probe(glob, NULL, NULL, 0);
59df055f
SR
1584}
1585
f6180773
SR
1586static LIST_HEAD(ftrace_commands);
1587static DEFINE_MUTEX(ftrace_cmd_mutex);
1588
1589int register_ftrace_command(struct ftrace_func_command *cmd)
1590{
1591 struct ftrace_func_command *p;
1592 int ret = 0;
1593
1594 mutex_lock(&ftrace_cmd_mutex);
1595 list_for_each_entry(p, &ftrace_commands, list) {
1596 if (strcmp(cmd->name, p->name) == 0) {
1597 ret = -EBUSY;
1598 goto out_unlock;
1599 }
1600 }
1601 list_add(&cmd->list, &ftrace_commands);
1602 out_unlock:
1603 mutex_unlock(&ftrace_cmd_mutex);
1604
1605 return ret;
1606}
1607
1608int unregister_ftrace_command(struct ftrace_func_command *cmd)
1609{
1610 struct ftrace_func_command *p, *n;
1611 int ret = -ENODEV;
1612
1613 mutex_lock(&ftrace_cmd_mutex);
1614 list_for_each_entry_safe(p, n, &ftrace_commands, list) {
1615 if (strcmp(cmd->name, p->name) == 0) {
1616 ret = 0;
1617 list_del_init(&p->list);
1618 goto out_unlock;
1619 }
1620 }
1621 out_unlock:
1622 mutex_unlock(&ftrace_cmd_mutex);
1623
1624 return ret;
1625}
1626
64e7c440
SR
1627static int ftrace_process_regex(char *buff, int len, int enable)
1628{
f6180773 1629 char *func, *command, *next = buff;
6a24a244 1630 struct ftrace_func_command *p;
f6180773 1631 int ret = -EINVAL;
64e7c440
SR
1632
1633 func = strsep(&next, ":");
1634
1635 if (!next) {
1636 ftrace_match_records(func, len, enable);
1637 return 0;
1638 }
1639
f6180773 1640 /* command found */
64e7c440
SR
1641
1642 command = strsep(&next, ":");
1643
f6180773
SR
1644 mutex_lock(&ftrace_cmd_mutex);
1645 list_for_each_entry(p, &ftrace_commands, list) {
1646 if (strcmp(p->name, command) == 0) {
1647 ret = p->func(func, command, next, enable);
1648 goto out_unlock;
1649 }
64e7c440 1650 }
f6180773
SR
1651 out_unlock:
1652 mutex_unlock(&ftrace_cmd_mutex);
64e7c440 1653
f6180773 1654 return ret;
64e7c440
SR
1655}
1656
e309b41d 1657static ssize_t
41c52c0d
SR
1658ftrace_regex_write(struct file *file, const char __user *ubuf,
1659 size_t cnt, loff_t *ppos, int enable)
5072c59f
SR
1660{
1661 struct ftrace_iterator *iter;
1662 char ch;
1663 size_t read = 0;
1664 ssize_t ret;
1665
1666 if (!cnt || cnt < 0)
1667 return 0;
1668
41c52c0d 1669 mutex_lock(&ftrace_regex_lock);
5072c59f
SR
1670
1671 if (file->f_mode & FMODE_READ) {
1672 struct seq_file *m = file->private_data;
1673 iter = m->private;
1674 } else
1675 iter = file->private_data;
1676
1677 if (!*ppos) {
1678 iter->flags &= ~FTRACE_ITER_CONT;
1679 iter->buffer_idx = 0;
1680 }
1681
1682 ret = get_user(ch, ubuf++);
1683 if (ret)
1684 goto out;
1685 read++;
1686 cnt--;
1687
1688 if (!(iter->flags & ~FTRACE_ITER_CONT)) {
1689 /* skip white space */
1690 while (cnt && isspace(ch)) {
1691 ret = get_user(ch, ubuf++);
1692 if (ret)
1693 goto out;
1694 read++;
1695 cnt--;
1696 }
1697
5072c59f
SR
1698 if (isspace(ch)) {
1699 file->f_pos += read;
1700 ret = read;
1701 goto out;
1702 }
1703
1704 iter->buffer_idx = 0;
1705 }
1706
1707 while (cnt && !isspace(ch)) {
1708 if (iter->buffer_idx < FTRACE_BUFF_MAX)
1709 iter->buffer[iter->buffer_idx++] = ch;
1710 else {
1711 ret = -EINVAL;
1712 goto out;
1713 }
1714 ret = get_user(ch, ubuf++);
1715 if (ret)
1716 goto out;
1717 read++;
1718 cnt--;
1719 }
1720
1721 if (isspace(ch)) {
1722 iter->filtered++;
1723 iter->buffer[iter->buffer_idx] = 0;
64e7c440
SR
1724 ret = ftrace_process_regex(iter->buffer,
1725 iter->buffer_idx, enable);
1726 if (ret)
1727 goto out;
5072c59f
SR
1728 iter->buffer_idx = 0;
1729 } else
1730 iter->flags |= FTRACE_ITER_CONT;
1731
1732
1733 file->f_pos += read;
1734
1735 ret = read;
1736 out:
41c52c0d 1737 mutex_unlock(&ftrace_regex_lock);
5072c59f
SR
1738
1739 return ret;
1740}
1741
41c52c0d
SR
1742static ssize_t
1743ftrace_filter_write(struct file *file, const char __user *ubuf,
1744 size_t cnt, loff_t *ppos)
1745{
1746 return ftrace_regex_write(file, ubuf, cnt, ppos, 1);
1747}
1748
1749static ssize_t
1750ftrace_notrace_write(struct file *file, const char __user *ubuf,
1751 size_t cnt, loff_t *ppos)
1752{
1753 return ftrace_regex_write(file, ubuf, cnt, ppos, 0);
1754}
1755
1756static void
1757ftrace_set_regex(unsigned char *buf, int len, int reset, int enable)
1758{
1759 if (unlikely(ftrace_disabled))
1760 return;
1761
1762 mutex_lock(&ftrace_regex_lock);
1763 if (reset)
1764 ftrace_filter_reset(enable);
1765 if (buf)
7f24b31b 1766 ftrace_match_records(buf, len, enable);
41c52c0d
SR
1767 mutex_unlock(&ftrace_regex_lock);
1768}
1769
77a2b37d
SR
1770/**
1771 * ftrace_set_filter - set a function to filter on in ftrace
1772 * @buf - the string that holds the function filter text.
1773 * @len - the length of the string.
1774 * @reset - non zero to reset all filters before applying this filter.
1775 *
1776 * Filters denote which functions should be enabled when tracing is enabled.
1777 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
1778 */
e309b41d 1779void ftrace_set_filter(unsigned char *buf, int len, int reset)
77a2b37d 1780{
41c52c0d
SR
1781 ftrace_set_regex(buf, len, reset, 1);
1782}
4eebcc81 1783
41c52c0d
SR
1784/**
1785 * ftrace_set_notrace - set a function to not trace in ftrace
1786 * @buf - the string that holds the function notrace text.
1787 * @len - the length of the string.
1788 * @reset - non zero to reset all filters before applying this filter.
1789 *
1790 * Notrace Filters denote which functions should not be enabled when tracing
1791 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
1792 * for tracing.
1793 */
1794void ftrace_set_notrace(unsigned char *buf, int len, int reset)
1795{
1796 ftrace_set_regex(buf, len, reset, 0);
77a2b37d
SR
1797}
1798
e309b41d 1799static int
41c52c0d 1800ftrace_regex_release(struct inode *inode, struct file *file, int enable)
5072c59f
SR
1801{
1802 struct seq_file *m = (struct seq_file *)file->private_data;
1803 struct ftrace_iterator *iter;
1804
41c52c0d 1805 mutex_lock(&ftrace_regex_lock);
5072c59f
SR
1806 if (file->f_mode & FMODE_READ) {
1807 iter = m->private;
1808
1809 seq_release(inode, file);
1810 } else
1811 iter = file->private_data;
1812
1813 if (iter->buffer_idx) {
1814 iter->filtered++;
1815 iter->buffer[iter->buffer_idx] = 0;
7f24b31b 1816 ftrace_match_records(iter->buffer, iter->buffer_idx, enable);
5072c59f
SR
1817 }
1818
e6ea44e9 1819 mutex_lock(&ftrace_lock);
ee02a2e5 1820 if (ftrace_start_up && ftrace_enabled)
5072c59f 1821 ftrace_run_update_code(FTRACE_ENABLE_CALLS);
e6ea44e9 1822 mutex_unlock(&ftrace_lock);
5072c59f
SR
1823
1824 kfree(iter);
41c52c0d 1825 mutex_unlock(&ftrace_regex_lock);
5072c59f
SR
1826 return 0;
1827}
1828
41c52c0d
SR
1829static int
1830ftrace_filter_release(struct inode *inode, struct file *file)
1831{
1832 return ftrace_regex_release(inode, file, 1);
1833}
1834
1835static int
1836ftrace_notrace_release(struct inode *inode, struct file *file)
1837{
1838 return ftrace_regex_release(inode, file, 0);
1839}
1840
5072c59f
SR
1841static struct file_operations ftrace_avail_fops = {
1842 .open = ftrace_avail_open,
1843 .read = seq_read,
1844 .llseek = seq_lseek,
1845 .release = ftrace_avail_release,
1846};
1847
eb9a7bf0
AS
1848static struct file_operations ftrace_failures_fops = {
1849 .open = ftrace_failures_open,
1850 .read = seq_read,
1851 .llseek = seq_lseek,
1852 .release = ftrace_avail_release,
1853};
1854
5072c59f
SR
1855static struct file_operations ftrace_filter_fops = {
1856 .open = ftrace_filter_open,
41c52c0d 1857 .read = ftrace_regex_read,
5072c59f 1858 .write = ftrace_filter_write,
41c52c0d 1859 .llseek = ftrace_regex_lseek,
5072c59f
SR
1860 .release = ftrace_filter_release,
1861};
1862
41c52c0d
SR
1863static struct file_operations ftrace_notrace_fops = {
1864 .open = ftrace_notrace_open,
1865 .read = ftrace_regex_read,
1866 .write = ftrace_notrace_write,
1867 .llseek = ftrace_regex_lseek,
1868 .release = ftrace_notrace_release,
1869};
1870
ea4e2bc4
SR
1871#ifdef CONFIG_FUNCTION_GRAPH_TRACER
1872
1873static DEFINE_MUTEX(graph_lock);
1874
1875int ftrace_graph_count;
1876unsigned long ftrace_graph_funcs[FTRACE_GRAPH_MAX_FUNCS] __read_mostly;
1877
1878static void *
1879g_next(struct seq_file *m, void *v, loff_t *pos)
1880{
1881 unsigned long *array = m->private;
1882 int index = *pos;
1883
1884 (*pos)++;
1885
1886 if (index >= ftrace_graph_count)
1887 return NULL;
1888
1889 return &array[index];
1890}
1891
1892static void *g_start(struct seq_file *m, loff_t *pos)
1893{
1894 void *p = NULL;
1895
1896 mutex_lock(&graph_lock);
1897
f9349a8f
FW
1898 /* Nothing, tell g_show to print all functions are enabled */
1899 if (!ftrace_graph_count && !*pos)
1900 return (void *)1;
1901
ea4e2bc4
SR
1902 p = g_next(m, p, pos);
1903
1904 return p;
1905}
1906
1907static void g_stop(struct seq_file *m, void *p)
1908{
1909 mutex_unlock(&graph_lock);
1910}
1911
1912static int g_show(struct seq_file *m, void *v)
1913{
1914 unsigned long *ptr = v;
1915 char str[KSYM_SYMBOL_LEN];
1916
1917 if (!ptr)
1918 return 0;
1919
f9349a8f
FW
1920 if (ptr == (unsigned long *)1) {
1921 seq_printf(m, "#### all functions enabled ####\n");
1922 return 0;
1923 }
1924
ea4e2bc4
SR
1925 kallsyms_lookup(*ptr, NULL, NULL, NULL, str);
1926
1927 seq_printf(m, "%s\n", str);
1928
1929 return 0;
1930}
1931
1932static struct seq_operations ftrace_graph_seq_ops = {
1933 .start = g_start,
1934 .next = g_next,
1935 .stop = g_stop,
1936 .show = g_show,
1937};
1938
1939static int
1940ftrace_graph_open(struct inode *inode, struct file *file)
1941{
1942 int ret = 0;
1943
1944 if (unlikely(ftrace_disabled))
1945 return -ENODEV;
1946
1947 mutex_lock(&graph_lock);
1948 if ((file->f_mode & FMODE_WRITE) &&
1949 !(file->f_flags & O_APPEND)) {
1950 ftrace_graph_count = 0;
1951 memset(ftrace_graph_funcs, 0, sizeof(ftrace_graph_funcs));
1952 }
1953
1954 if (file->f_mode & FMODE_READ) {
1955 ret = seq_open(file, &ftrace_graph_seq_ops);
1956 if (!ret) {
1957 struct seq_file *m = file->private_data;
1958 m->private = ftrace_graph_funcs;
1959 }
1960 } else
1961 file->private_data = ftrace_graph_funcs;
1962 mutex_unlock(&graph_lock);
1963
1964 return ret;
1965}
1966
1967static ssize_t
1968ftrace_graph_read(struct file *file, char __user *ubuf,
1969 size_t cnt, loff_t *ppos)
1970{
1971 if (file->f_mode & FMODE_READ)
1972 return seq_read(file, ubuf, cnt, ppos);
1973 else
1974 return -EPERM;
1975}
1976
1977static int
f9349a8f 1978ftrace_set_func(unsigned long *array, int *idx, char *buffer)
ea4e2bc4 1979{
ea4e2bc4
SR
1980 struct dyn_ftrace *rec;
1981 struct ftrace_page *pg;
f9349a8f 1982 int search_len;
ea4e2bc4 1983 int found = 0;
f9349a8f
FW
1984 int type, not;
1985 char *search;
1986 bool exists;
1987 int i;
ea4e2bc4
SR
1988
1989 if (ftrace_disabled)
1990 return -ENODEV;
1991
f9349a8f
FW
1992 /* decode regex */
1993 type = ftrace_setup_glob(buffer, strlen(buffer), &search, &not);
1994 if (not)
1995 return -EINVAL;
1996
1997 search_len = strlen(search);
1998
52baf119 1999 mutex_lock(&ftrace_lock);
265c831c
SR
2000 do_for_each_ftrace_rec(pg, rec) {
2001
f9349a8f
FW
2002 if (*idx >= FTRACE_GRAPH_MAX_FUNCS)
2003 break;
2004
265c831c
SR
2005 if (rec->flags & (FTRACE_FL_FAILED | FTRACE_FL_FREE))
2006 continue;
2007
f9349a8f
FW
2008 if (ftrace_match_record(rec, search, search_len, type)) {
2009 /* ensure it is not already in the array */
2010 exists = false;
2011 for (i = 0; i < *idx; i++)
2012 if (array[i] == rec->ip) {
2013 exists = true;
265c831c
SR
2014 break;
2015 }
f9349a8f
FW
2016 if (!exists) {
2017 array[(*idx)++] = rec->ip;
2018 found = 1;
2019 }
ea4e2bc4 2020 }
265c831c 2021 } while_for_each_ftrace_rec();
f9349a8f 2022
52baf119 2023 mutex_unlock(&ftrace_lock);
ea4e2bc4
SR
2024
2025 return found ? 0 : -EINVAL;
2026}
2027
2028static ssize_t
2029ftrace_graph_write(struct file *file, const char __user *ubuf,
2030 size_t cnt, loff_t *ppos)
2031{
2032 unsigned char buffer[FTRACE_BUFF_MAX+1];
2033 unsigned long *array;
2034 size_t read = 0;
2035 ssize_t ret;
2036 int index = 0;
2037 char ch;
2038
2039 if (!cnt || cnt < 0)
2040 return 0;
2041
2042 mutex_lock(&graph_lock);
2043
2044 if (ftrace_graph_count >= FTRACE_GRAPH_MAX_FUNCS) {
2045 ret = -EBUSY;
2046 goto out;
2047 }
2048
2049 if (file->f_mode & FMODE_READ) {
2050 struct seq_file *m = file->private_data;
2051 array = m->private;
2052 } else
2053 array = file->private_data;
2054
2055 ret = get_user(ch, ubuf++);
2056 if (ret)
2057 goto out;
2058 read++;
2059 cnt--;
2060
2061 /* skip white space */
2062 while (cnt && isspace(ch)) {
2063 ret = get_user(ch, ubuf++);
2064 if (ret)
2065 goto out;
2066 read++;
2067 cnt--;
2068 }
2069
2070 if (isspace(ch)) {
2071 *ppos += read;
2072 ret = read;
2073 goto out;
2074 }
2075
2076 while (cnt && !isspace(ch)) {
2077 if (index < FTRACE_BUFF_MAX)
2078 buffer[index++] = ch;
2079 else {
2080 ret = -EINVAL;
2081 goto out;
2082 }
2083 ret = get_user(ch, ubuf++);
2084 if (ret)
2085 goto out;
2086 read++;
2087 cnt--;
2088 }
2089 buffer[index] = 0;
2090
f9349a8f
FW
2091 /* we allow only one expression at a time */
2092 ret = ftrace_set_func(array, &ftrace_graph_count, buffer);
ea4e2bc4
SR
2093 if (ret)
2094 goto out;
2095
ea4e2bc4
SR
2096 file->f_pos += read;
2097
2098 ret = read;
2099 out:
2100 mutex_unlock(&graph_lock);
2101
2102 return ret;
2103}
2104
2105static const struct file_operations ftrace_graph_fops = {
2106 .open = ftrace_graph_open,
2107 .read = ftrace_graph_read,
2108 .write = ftrace_graph_write,
2109};
2110#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
2111
df4fc315 2112static __init int ftrace_init_dyn_debugfs(struct dentry *d_tracer)
5072c59f 2113{
5072c59f
SR
2114 struct dentry *entry;
2115
5072c59f
SR
2116 entry = debugfs_create_file("available_filter_functions", 0444,
2117 d_tracer, NULL, &ftrace_avail_fops);
2118 if (!entry)
2119 pr_warning("Could not create debugfs "
2120 "'available_filter_functions' entry\n");
2121
eb9a7bf0
AS
2122 entry = debugfs_create_file("failures", 0444,
2123 d_tracer, NULL, &ftrace_failures_fops);
2124 if (!entry)
2125 pr_warning("Could not create debugfs 'failures' entry\n");
2126
5072c59f
SR
2127 entry = debugfs_create_file("set_ftrace_filter", 0644, d_tracer,
2128 NULL, &ftrace_filter_fops);
2129 if (!entry)
2130 pr_warning("Could not create debugfs "
2131 "'set_ftrace_filter' entry\n");
41c52c0d
SR
2132
2133 entry = debugfs_create_file("set_ftrace_notrace", 0644, d_tracer,
2134 NULL, &ftrace_notrace_fops);
2135 if (!entry)
2136 pr_warning("Could not create debugfs "
2137 "'set_ftrace_notrace' entry\n");
ad90c0e3 2138
ea4e2bc4
SR
2139#ifdef CONFIG_FUNCTION_GRAPH_TRACER
2140 entry = debugfs_create_file("set_graph_function", 0444, d_tracer,
2141 NULL,
2142 &ftrace_graph_fops);
2143 if (!entry)
2144 pr_warning("Could not create debugfs "
2145 "'set_graph_function' entry\n");
2146#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
2147
5072c59f
SR
2148 return 0;
2149}
2150
31e88909
SR
2151static int ftrace_convert_nops(struct module *mod,
2152 unsigned long *start,
68bf21aa
SR
2153 unsigned long *end)
2154{
2155 unsigned long *p;
2156 unsigned long addr;
2157 unsigned long flags;
2158
e6ea44e9 2159 mutex_lock(&ftrace_lock);
68bf21aa
SR
2160 p = start;
2161 while (p < end) {
2162 addr = ftrace_call_adjust(*p++);
20e5227e
SR
2163 /*
2164 * Some architecture linkers will pad between
2165 * the different mcount_loc sections of different
2166 * object files to satisfy alignments.
2167 * Skip any NULL pointers.
2168 */
2169 if (!addr)
2170 continue;
68bf21aa 2171 ftrace_record_ip(addr);
68bf21aa
SR
2172 }
2173
08f5ac90 2174 /* disable interrupts to prevent kstop machine */
68bf21aa 2175 local_irq_save(flags);
31e88909 2176 ftrace_update_code(mod);
68bf21aa 2177 local_irq_restore(flags);
e6ea44e9 2178 mutex_unlock(&ftrace_lock);
68bf21aa
SR
2179
2180 return 0;
2181}
2182
31e88909
SR
2183void ftrace_init_module(struct module *mod,
2184 unsigned long *start, unsigned long *end)
90d595fe 2185{
00fd61ae 2186 if (ftrace_disabled || start == end)
fed1939c 2187 return;
31e88909 2188 ftrace_convert_nops(mod, start, end);
90d595fe
SR
2189}
2190
68bf21aa
SR
2191extern unsigned long __start_mcount_loc[];
2192extern unsigned long __stop_mcount_loc[];
2193
2194void __init ftrace_init(void)
2195{
2196 unsigned long count, addr, flags;
2197 int ret;
2198
2199 /* Keep the ftrace pointer to the stub */
2200 addr = (unsigned long)ftrace_stub;
2201
2202 local_irq_save(flags);
2203 ftrace_dyn_arch_init(&addr);
2204 local_irq_restore(flags);
2205
2206 /* ftrace_dyn_arch_init places the return code in addr */
2207 if (addr)
2208 goto failed;
2209
2210 count = __stop_mcount_loc - __start_mcount_loc;
2211
2212 ret = ftrace_dyn_table_alloc(count);
2213 if (ret)
2214 goto failed;
2215
2216 last_ftrace_enabled = ftrace_enabled = 1;
2217
31e88909
SR
2218 ret = ftrace_convert_nops(NULL,
2219 __start_mcount_loc,
68bf21aa
SR
2220 __stop_mcount_loc);
2221
2222 return;
2223 failed:
2224 ftrace_disabled = 1;
2225}
68bf21aa 2226
3d083395 2227#else
0b6e4d56
FW
2228
2229static int __init ftrace_nodyn_init(void)
2230{
2231 ftrace_enabled = 1;
2232 return 0;
2233}
2234device_initcall(ftrace_nodyn_init);
2235
df4fc315
SR
2236static inline int ftrace_init_dyn_debugfs(struct dentry *d_tracer) { return 0; }
2237static inline void ftrace_startup_enable(int command) { }
5a45cfe1
SR
2238/* Keep as macros so we do not need to define the commands */
2239# define ftrace_startup(command) do { } while (0)
2240# define ftrace_shutdown(command) do { } while (0)
c7aafc54
IM
2241# define ftrace_startup_sysctl() do { } while (0)
2242# define ftrace_shutdown_sysctl() do { } while (0)
3d083395
SR
2243#endif /* CONFIG_DYNAMIC_FTRACE */
2244
df4fc315
SR
2245static ssize_t
2246ftrace_pid_read(struct file *file, char __user *ubuf,
2247 size_t cnt, loff_t *ppos)
2248{
2249 char buf[64];
2250 int r;
2251
e32d8956
SR
2252 if (ftrace_pid_trace == ftrace_swapper_pid)
2253 r = sprintf(buf, "swapper tasks\n");
2254 else if (ftrace_pid_trace)
978f3a45 2255 r = sprintf(buf, "%u\n", pid_nr(ftrace_pid_trace));
df4fc315
SR
2256 else
2257 r = sprintf(buf, "no pid\n");
2258
2259 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2260}
2261
e32d8956 2262static void clear_ftrace_swapper(void)
978f3a45
SR
2263{
2264 struct task_struct *p;
e32d8956 2265 int cpu;
978f3a45 2266
e32d8956
SR
2267 get_online_cpus();
2268 for_each_online_cpu(cpu) {
2269 p = idle_task(cpu);
978f3a45 2270 clear_tsk_trace_trace(p);
e32d8956
SR
2271 }
2272 put_online_cpus();
2273}
978f3a45 2274
e32d8956
SR
2275static void set_ftrace_swapper(void)
2276{
2277 struct task_struct *p;
2278 int cpu;
2279
2280 get_online_cpus();
2281 for_each_online_cpu(cpu) {
2282 p = idle_task(cpu);
2283 set_tsk_trace_trace(p);
2284 }
2285 put_online_cpus();
978f3a45
SR
2286}
2287
e32d8956
SR
2288static void clear_ftrace_pid(struct pid *pid)
2289{
2290 struct task_struct *p;
2291
229c4ef8 2292 rcu_read_lock();
e32d8956
SR
2293 do_each_pid_task(pid, PIDTYPE_PID, p) {
2294 clear_tsk_trace_trace(p);
2295 } while_each_pid_task(pid, PIDTYPE_PID, p);
229c4ef8
ON
2296 rcu_read_unlock();
2297
e32d8956
SR
2298 put_pid(pid);
2299}
2300
2301static void set_ftrace_pid(struct pid *pid)
978f3a45
SR
2302{
2303 struct task_struct *p;
2304
229c4ef8 2305 rcu_read_lock();
978f3a45
SR
2306 do_each_pid_task(pid, PIDTYPE_PID, p) {
2307 set_tsk_trace_trace(p);
2308 } while_each_pid_task(pid, PIDTYPE_PID, p);
229c4ef8 2309 rcu_read_unlock();
978f3a45
SR
2310}
2311
e32d8956
SR
2312static void clear_ftrace_pid_task(struct pid **pid)
2313{
2314 if (*pid == ftrace_swapper_pid)
2315 clear_ftrace_swapper();
2316 else
2317 clear_ftrace_pid(*pid);
2318
2319 *pid = NULL;
2320}
2321
2322static void set_ftrace_pid_task(struct pid *pid)
2323{
2324 if (pid == ftrace_swapper_pid)
2325 set_ftrace_swapper();
2326 else
2327 set_ftrace_pid(pid);
2328}
2329
df4fc315
SR
2330static ssize_t
2331ftrace_pid_write(struct file *filp, const char __user *ubuf,
2332 size_t cnt, loff_t *ppos)
2333{
978f3a45 2334 struct pid *pid;
df4fc315
SR
2335 char buf[64];
2336 long val;
2337 int ret;
2338
2339 if (cnt >= sizeof(buf))
2340 return -EINVAL;
2341
2342 if (copy_from_user(&buf, ubuf, cnt))
2343 return -EFAULT;
2344
2345 buf[cnt] = 0;
2346
2347 ret = strict_strtol(buf, 10, &val);
2348 if (ret < 0)
2349 return ret;
2350
e6ea44e9 2351 mutex_lock(&ftrace_lock);
978f3a45 2352 if (val < 0) {
df4fc315 2353 /* disable pid tracing */
978f3a45 2354 if (!ftrace_pid_trace)
df4fc315 2355 goto out;
978f3a45
SR
2356
2357 clear_ftrace_pid_task(&ftrace_pid_trace);
df4fc315
SR
2358
2359 } else {
e32d8956
SR
2360 /* swapper task is special */
2361 if (!val) {
2362 pid = ftrace_swapper_pid;
2363 if (pid == ftrace_pid_trace)
2364 goto out;
2365 } else {
2366 pid = find_get_pid(val);
df4fc315 2367
e32d8956
SR
2368 if (pid == ftrace_pid_trace) {
2369 put_pid(pid);
2370 goto out;
2371 }
0ef8cde5 2372 }
0ef8cde5 2373
978f3a45
SR
2374 if (ftrace_pid_trace)
2375 clear_ftrace_pid_task(&ftrace_pid_trace);
2376
2377 if (!pid)
2378 goto out;
2379
2380 ftrace_pid_trace = pid;
2381
2382 set_ftrace_pid_task(ftrace_pid_trace);
df4fc315
SR
2383 }
2384
2385 /* update the function call */
2386 ftrace_update_pid_func();
2387 ftrace_startup_enable(0);
2388
2389 out:
e6ea44e9 2390 mutex_unlock(&ftrace_lock);
df4fc315
SR
2391
2392 return cnt;
2393}
2394
2395static struct file_operations ftrace_pid_fops = {
2396 .read = ftrace_pid_read,
2397 .write = ftrace_pid_write,
2398};
2399
2400static __init int ftrace_init_debugfs(void)
2401{
2402 struct dentry *d_tracer;
2403 struct dentry *entry;
2404
2405 d_tracer = tracing_init_dentry();
2406 if (!d_tracer)
2407 return 0;
2408
2409 ftrace_init_dyn_debugfs(d_tracer);
2410
2411 entry = debugfs_create_file("set_ftrace_pid", 0644, d_tracer,
2412 NULL, &ftrace_pid_fops);
2413 if (!entry)
2414 pr_warning("Could not create debugfs "
2415 "'set_ftrace_pid' entry\n");
2416 return 0;
2417}
df4fc315
SR
2418fs_initcall(ftrace_init_debugfs);
2419
a2bb6a3d 2420/**
81adbdc0 2421 * ftrace_kill - kill ftrace
a2bb6a3d
SR
2422 *
2423 * This function should be used by panic code. It stops ftrace
2424 * but in a not so nice way. If you need to simply kill ftrace
2425 * from a non-atomic section, use ftrace_kill.
2426 */
81adbdc0 2427void ftrace_kill(void)
a2bb6a3d
SR
2428{
2429 ftrace_disabled = 1;
2430 ftrace_enabled = 0;
a2bb6a3d
SR
2431 clear_ftrace_function();
2432}
2433
16444a8a 2434/**
3d083395
SR
2435 * register_ftrace_function - register a function for profiling
2436 * @ops - ops structure that holds the function for profiling.
16444a8a 2437 *
3d083395
SR
2438 * Register a function to be called by all functions in the
2439 * kernel.
2440 *
2441 * Note: @ops->func and all the functions it calls must be labeled
2442 * with "notrace", otherwise it will go into a
2443 * recursive loop.
16444a8a 2444 */
3d083395 2445int register_ftrace_function(struct ftrace_ops *ops)
16444a8a 2446{
b0fc494f
SR
2447 int ret;
2448
4eebcc81
SR
2449 if (unlikely(ftrace_disabled))
2450 return -1;
2451
e6ea44e9 2452 mutex_lock(&ftrace_lock);
e7d3737e 2453
b0fc494f 2454 ret = __register_ftrace_function(ops);
5a45cfe1 2455 ftrace_startup(0);
b0fc494f 2456
e6ea44e9 2457 mutex_unlock(&ftrace_lock);
b0fc494f 2458 return ret;
3d083395
SR
2459}
2460
2461/**
32632920 2462 * unregister_ftrace_function - unregister a function for profiling.
3d083395
SR
2463 * @ops - ops structure that holds the function to unregister
2464 *
2465 * Unregister a function that was added to be called by ftrace profiling.
2466 */
2467int unregister_ftrace_function(struct ftrace_ops *ops)
2468{
2469 int ret;
2470
e6ea44e9 2471 mutex_lock(&ftrace_lock);
3d083395 2472 ret = __unregister_ftrace_function(ops);
5a45cfe1 2473 ftrace_shutdown(0);
e6ea44e9 2474 mutex_unlock(&ftrace_lock);
b0fc494f
SR
2475
2476 return ret;
2477}
2478
e309b41d 2479int
b0fc494f 2480ftrace_enable_sysctl(struct ctl_table *table, int write,
5072c59f 2481 struct file *file, void __user *buffer, size_t *lenp,
b0fc494f
SR
2482 loff_t *ppos)
2483{
2484 int ret;
2485
4eebcc81
SR
2486 if (unlikely(ftrace_disabled))
2487 return -ENODEV;
2488
e6ea44e9 2489 mutex_lock(&ftrace_lock);
b0fc494f 2490
5072c59f 2491 ret = proc_dointvec(table, write, file, buffer, lenp, ppos);
b0fc494f
SR
2492
2493 if (ret || !write || (last_ftrace_enabled == ftrace_enabled))
2494 goto out;
2495
2496 last_ftrace_enabled = ftrace_enabled;
2497
2498 if (ftrace_enabled) {
2499
2500 ftrace_startup_sysctl();
2501
2502 /* we are starting ftrace again */
2503 if (ftrace_list != &ftrace_list_end) {
2504 if (ftrace_list->next == &ftrace_list_end)
2505 ftrace_trace_function = ftrace_list->func;
2506 else
2507 ftrace_trace_function = ftrace_list_func;
2508 }
2509
2510 } else {
2511 /* stopping ftrace calls (just send to ftrace_stub) */
2512 ftrace_trace_function = ftrace_stub;
2513
2514 ftrace_shutdown_sysctl();
2515 }
2516
2517 out:
e6ea44e9 2518 mutex_unlock(&ftrace_lock);
3d083395 2519 return ret;
16444a8a 2520}
f17845e5 2521
fb52607a 2522#ifdef CONFIG_FUNCTION_GRAPH_TRACER
e7d3737e 2523
287b6e68 2524static atomic_t ftrace_graph_active;
4a2b8dda 2525static struct notifier_block ftrace_suspend_notifier;
e7d3737e 2526
e49dc19c
SR
2527int ftrace_graph_entry_stub(struct ftrace_graph_ent *trace)
2528{
2529 return 0;
2530}
2531
287b6e68
FW
2532/* The callbacks that hook a function */
2533trace_func_graph_ret_t ftrace_graph_return =
2534 (trace_func_graph_ret_t)ftrace_stub;
e49dc19c 2535trace_func_graph_ent_t ftrace_graph_entry = ftrace_graph_entry_stub;
f201ae23
FW
2536
2537/* Try to assign a return stack array on FTRACE_RETSTACK_ALLOC_SIZE tasks. */
2538static int alloc_retstack_tasklist(struct ftrace_ret_stack **ret_stack_list)
2539{
2540 int i;
2541 int ret = 0;
2542 unsigned long flags;
2543 int start = 0, end = FTRACE_RETSTACK_ALLOC_SIZE;
2544 struct task_struct *g, *t;
2545
2546 for (i = 0; i < FTRACE_RETSTACK_ALLOC_SIZE; i++) {
2547 ret_stack_list[i] = kmalloc(FTRACE_RETFUNC_DEPTH
2548 * sizeof(struct ftrace_ret_stack),
2549 GFP_KERNEL);
2550 if (!ret_stack_list[i]) {
2551 start = 0;
2552 end = i;
2553 ret = -ENOMEM;
2554 goto free;
2555 }
2556 }
2557
2558 read_lock_irqsave(&tasklist_lock, flags);
2559 do_each_thread(g, t) {
2560 if (start == end) {
2561 ret = -EAGAIN;
2562 goto unlock;
2563 }
2564
2565 if (t->ret_stack == NULL) {
f201ae23 2566 t->curr_ret_stack = -1;
48d68b20
FW
2567 /* Make sure IRQs see the -1 first: */
2568 barrier();
2569 t->ret_stack = ret_stack_list[start++];
380c4b14 2570 atomic_set(&t->tracing_graph_pause, 0);
f201ae23
FW
2571 atomic_set(&t->trace_overrun, 0);
2572 }
2573 } while_each_thread(g, t);
2574
2575unlock:
2576 read_unlock_irqrestore(&tasklist_lock, flags);
2577free:
2578 for (i = start; i < end; i++)
2579 kfree(ret_stack_list[i]);
2580 return ret;
2581}
2582
2583/* Allocate a return stack for each task */
fb52607a 2584static int start_graph_tracing(void)
f201ae23
FW
2585{
2586 struct ftrace_ret_stack **ret_stack_list;
5b058bcd 2587 int ret, cpu;
f201ae23
FW
2588
2589 ret_stack_list = kmalloc(FTRACE_RETSTACK_ALLOC_SIZE *
2590 sizeof(struct ftrace_ret_stack *),
2591 GFP_KERNEL);
2592
2593 if (!ret_stack_list)
2594 return -ENOMEM;
2595
5b058bcd
FW
2596 /* The cpu_boot init_task->ret_stack will never be freed */
2597 for_each_online_cpu(cpu)
2598 ftrace_graph_init_task(idle_task(cpu));
2599
f201ae23
FW
2600 do {
2601 ret = alloc_retstack_tasklist(ret_stack_list);
2602 } while (ret == -EAGAIN);
2603
2604 kfree(ret_stack_list);
2605 return ret;
2606}
2607
4a2b8dda
FW
2608/*
2609 * Hibernation protection.
2610 * The state of the current task is too much unstable during
2611 * suspend/restore to disk. We want to protect against that.
2612 */
2613static int
2614ftrace_suspend_notifier_call(struct notifier_block *bl, unsigned long state,
2615 void *unused)
2616{
2617 switch (state) {
2618 case PM_HIBERNATION_PREPARE:
2619 pause_graph_tracing();
2620 break;
2621
2622 case PM_POST_HIBERNATION:
2623 unpause_graph_tracing();
2624 break;
2625 }
2626 return NOTIFY_DONE;
2627}
2628
287b6e68
FW
2629int register_ftrace_graph(trace_func_graph_ret_t retfunc,
2630 trace_func_graph_ent_t entryfunc)
15e6cb36 2631{
e7d3737e
FW
2632 int ret = 0;
2633
e6ea44e9 2634 mutex_lock(&ftrace_lock);
e7d3737e 2635
4a2b8dda
FW
2636 ftrace_suspend_notifier.notifier_call = ftrace_suspend_notifier_call;
2637 register_pm_notifier(&ftrace_suspend_notifier);
2638
287b6e68 2639 atomic_inc(&ftrace_graph_active);
fb52607a 2640 ret = start_graph_tracing();
f201ae23 2641 if (ret) {
287b6e68 2642 atomic_dec(&ftrace_graph_active);
f201ae23
FW
2643 goto out;
2644 }
e53a6319 2645
287b6e68
FW
2646 ftrace_graph_return = retfunc;
2647 ftrace_graph_entry = entryfunc;
e53a6319 2648
5a45cfe1 2649 ftrace_startup(FTRACE_START_FUNC_RET);
e7d3737e
FW
2650
2651out:
e6ea44e9 2652 mutex_unlock(&ftrace_lock);
e7d3737e 2653 return ret;
15e6cb36
FW
2654}
2655
fb52607a 2656void unregister_ftrace_graph(void)
15e6cb36 2657{
e6ea44e9 2658 mutex_lock(&ftrace_lock);
e7d3737e 2659
287b6e68
FW
2660 atomic_dec(&ftrace_graph_active);
2661 ftrace_graph_return = (trace_func_graph_ret_t)ftrace_stub;
e49dc19c 2662 ftrace_graph_entry = ftrace_graph_entry_stub;
5a45cfe1 2663 ftrace_shutdown(FTRACE_STOP_FUNC_RET);
4a2b8dda 2664 unregister_pm_notifier(&ftrace_suspend_notifier);
e7d3737e 2665
e6ea44e9 2666 mutex_unlock(&ftrace_lock);
15e6cb36 2667}
f201ae23
FW
2668
2669/* Allocate a return stack for newly created task */
fb52607a 2670void ftrace_graph_init_task(struct task_struct *t)
f201ae23 2671{
287b6e68 2672 if (atomic_read(&ftrace_graph_active)) {
f201ae23
FW
2673 t->ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
2674 * sizeof(struct ftrace_ret_stack),
2675 GFP_KERNEL);
2676 if (!t->ret_stack)
2677 return;
2678 t->curr_ret_stack = -1;
380c4b14 2679 atomic_set(&t->tracing_graph_pause, 0);
f201ae23
FW
2680 atomic_set(&t->trace_overrun, 0);
2681 } else
2682 t->ret_stack = NULL;
2683}
2684
fb52607a 2685void ftrace_graph_exit_task(struct task_struct *t)
f201ae23 2686{
eae849ca
FW
2687 struct ftrace_ret_stack *ret_stack = t->ret_stack;
2688
f201ae23 2689 t->ret_stack = NULL;
eae849ca
FW
2690 /* NULL must become visible to IRQs before we free it: */
2691 barrier();
2692
2693 kfree(ret_stack);
f201ae23 2694}
14a866c5
SR
2695
2696void ftrace_graph_stop(void)
2697{
2698 ftrace_stop();
2699}
15e6cb36
FW
2700#endif
2701