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