]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - kernel/trace/ftrace.c
ftrace: state that all functions are enabled in set_ftrace_filter
[mirror_ubuntu-bionic-kernel.git] / kernel / trace / ftrace.c
CommitLineData
16444a8a
ACM
1/*
2 * Infrastructure for profiling code inserted by 'gcc -pg'.
3 *
4 * Copyright (C) 2007-2008 Steven Rostedt <srostedt@redhat.com>
5 * Copyright (C) 2004-2008 Ingo Molnar <mingo@redhat.com>
6 *
7 * Originally ported from the -rt patch by:
8 * Copyright (C) 2007 Arnaldo Carvalho de Melo <acme@redhat.com>
9 *
10 * Based on code in the latency_tracer, that is:
11 *
12 * Copyright (C) 2004-2006 Ingo Molnar
13 * Copyright (C) 2004 William Lee Irwin III
14 */
15
3d083395
SR
16#include <linux/stop_machine.h>
17#include <linux/clocksource.h>
18#include <linux/kallsyms.h>
5072c59f 19#include <linux/seq_file.h>
4a2b8dda 20#include <linux/suspend.h>
5072c59f 21#include <linux/debugfs.h>
3d083395 22#include <linux/hardirq.h>
2d8b820b 23#include <linux/kthread.h>
5072c59f 24#include <linux/uaccess.h>
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
ecea656d
AS
300
301#ifdef CONFIG_KPROBES
f17845e5
IM
302
303static int frozen_record_count;
304
ecea656d
AS
305static inline void freeze_record(struct dyn_ftrace *rec)
306{
307 if (!(rec->flags & FTRACE_FL_FROZEN)) {
308 rec->flags |= FTRACE_FL_FROZEN;
309 frozen_record_count++;
310 }
311}
312
313static inline void unfreeze_record(struct dyn_ftrace *rec)
314{
315 if (rec->flags & FTRACE_FL_FROZEN) {
316 rec->flags &= ~FTRACE_FL_FROZEN;
317 frozen_record_count--;
318 }
319}
320
321static inline int record_frozen(struct dyn_ftrace *rec)
322{
323 return rec->flags & FTRACE_FL_FROZEN;
324}
325#else
326# define freeze_record(rec) ({ 0; })
327# define unfreeze_record(rec) ({ 0; })
328# define record_frozen(rec) ({ 0; })
329#endif /* CONFIG_KPROBES */
330
e309b41d 331static void ftrace_free_rec(struct dyn_ftrace *rec)
37ad5084 332{
37ad5084
SR
333 rec->ip = (unsigned long)ftrace_free_records;
334 ftrace_free_records = rec;
335 rec->flags |= FTRACE_FL_FREE;
336}
337
fed1939c
SR
338void ftrace_release(void *start, unsigned long size)
339{
340 struct dyn_ftrace *rec;
341 struct ftrace_page *pg;
342 unsigned long s = (unsigned long)start;
343 unsigned long e = s + size;
344 int i;
345
00fd61ae 346 if (ftrace_disabled || !start)
fed1939c
SR
347 return;
348
99ecdc43 349 /* should not be called from interrupt context */
fed1939c
SR
350 spin_lock(&ftrace_lock);
351
352 for (pg = ftrace_pages_start; pg; pg = pg->next) {
353 for (i = 0; i < pg->index; i++) {
354 rec = &pg->records[i];
355
356 if ((rec->ip >= s) && (rec->ip < e))
357 ftrace_free_rec(rec);
358 }
359 }
360 spin_unlock(&ftrace_lock);
fed1939c
SR
361}
362
e309b41d 363static struct dyn_ftrace *ftrace_alloc_dyn_node(unsigned long ip)
3c1720f0 364{
37ad5084
SR
365 struct dyn_ftrace *rec;
366
367 /* First check for freed records */
368 if (ftrace_free_records) {
369 rec = ftrace_free_records;
370
37ad5084 371 if (unlikely(!(rec->flags & FTRACE_FL_FREE))) {
6912896e 372 FTRACE_WARN_ON_ONCE(1);
37ad5084
SR
373 ftrace_free_records = NULL;
374 return NULL;
375 }
376
377 ftrace_free_records = (void *)rec->ip;
378 memset(rec, 0, sizeof(*rec));
379 return rec;
380 }
381
3c1720f0 382 if (ftrace_pages->index == ENTRIES_PER_PAGE) {
08f5ac90
SR
383 if (!ftrace_pages->next) {
384 /* allocate another page */
385 ftrace_pages->next =
386 (void *)get_zeroed_page(GFP_KERNEL);
387 if (!ftrace_pages->next)
388 return NULL;
389 }
3c1720f0
SR
390 ftrace_pages = ftrace_pages->next;
391 }
392
393 return &ftrace_pages->records[ftrace_pages->index++];
394}
395
08f5ac90 396static struct dyn_ftrace *
d61f82d0 397ftrace_record_ip(unsigned long ip)
3d083395 398{
08f5ac90 399 struct dyn_ftrace *rec;
3d083395 400
f3c7ac40 401 if (ftrace_disabled)
08f5ac90 402 return NULL;
3d083395 403
08f5ac90
SR
404 rec = ftrace_alloc_dyn_node(ip);
405 if (!rec)
406 return NULL;
3d083395 407
08f5ac90 408 rec->ip = ip;
3d083395 409
08f5ac90 410 list_add(&rec->list, &ftrace_new_addrs);
3d083395 411
08f5ac90 412 return rec;
3d083395
SR
413}
414
b17e8a37
SR
415static void print_ip_ins(const char *fmt, unsigned char *p)
416{
417 int i;
418
419 printk(KERN_CONT "%s", fmt);
420
421 for (i = 0; i < MCOUNT_INSN_SIZE; i++)
422 printk(KERN_CONT "%s%02x", i ? ":" : "", p[i]);
423}
424
31e88909 425static void ftrace_bug(int failed, unsigned long ip)
b17e8a37
SR
426{
427 switch (failed) {
428 case -EFAULT:
429 FTRACE_WARN_ON_ONCE(1);
430 pr_info("ftrace faulted on modifying ");
431 print_ip_sym(ip);
432 break;
433 case -EINVAL:
434 FTRACE_WARN_ON_ONCE(1);
435 pr_info("ftrace failed to modify ");
436 print_ip_sym(ip);
b17e8a37 437 print_ip_ins(" actual: ", (unsigned char *)ip);
b17e8a37
SR
438 printk(KERN_CONT "\n");
439 break;
440 case -EPERM:
441 FTRACE_WARN_ON_ONCE(1);
442 pr_info("ftrace faulted on writing ");
443 print_ip_sym(ip);
444 break;
445 default:
446 FTRACE_WARN_ON_ONCE(1);
447 pr_info("ftrace faulted on unknown error ");
448 print_ip_sym(ip);
449 }
450}
451
3c1720f0 452
0eb96701 453static int
31e88909 454__ftrace_replace_code(struct dyn_ftrace *rec, int enable)
5072c59f 455{
41c52c0d 456 unsigned long ip, fl;
e7d3737e
FW
457 unsigned long ftrace_addr;
458
f0001207 459 ftrace_addr = (unsigned long)FTRACE_ADDR;
5072c59f
SR
460
461 ip = rec->ip;
462
982c350b
SR
463 /*
464 * If this record is not to be traced and
465 * it is not enabled then do nothing.
466 *
467 * If this record is not to be traced and
57794a9d 468 * it is enabled then disable it.
982c350b
SR
469 *
470 */
471 if (rec->flags & FTRACE_FL_NOTRACE) {
472 if (rec->flags & FTRACE_FL_ENABLED)
473 rec->flags &= ~FTRACE_FL_ENABLED;
474 else
475 return 0;
476
477 } else if (ftrace_filtered && enable) {
5072c59f 478 /*
982c350b 479 * Filtering is on:
5072c59f 480 */
a4500b84 481
982c350b 482 fl = rec->flags & (FTRACE_FL_FILTER | FTRACE_FL_ENABLED);
5072c59f 483
982c350b
SR
484 /* Record is filtered and enabled, do nothing */
485 if (fl == (FTRACE_FL_FILTER | FTRACE_FL_ENABLED))
0eb96701 486 return 0;
5072c59f 487
57794a9d 488 /* Record is not filtered or enabled, do nothing */
982c350b
SR
489 if (!fl)
490 return 0;
491
492 /* Record is not filtered but enabled, disable it */
493 if (fl == FTRACE_FL_ENABLED)
5072c59f 494 rec->flags &= ~FTRACE_FL_ENABLED;
982c350b
SR
495 else
496 /* Otherwise record is filtered but not enabled, enable it */
5072c59f 497 rec->flags |= FTRACE_FL_ENABLED;
5072c59f 498 } else {
982c350b 499 /* Disable or not filtered */
5072c59f 500
41c52c0d 501 if (enable) {
982c350b 502 /* if record is enabled, do nothing */
5072c59f 503 if (rec->flags & FTRACE_FL_ENABLED)
0eb96701 504 return 0;
982c350b 505
5072c59f 506 rec->flags |= FTRACE_FL_ENABLED;
982c350b 507
5072c59f 508 } else {
982c350b 509
57794a9d 510 /* if record is not enabled, do nothing */
5072c59f 511 if (!(rec->flags & FTRACE_FL_ENABLED))
0eb96701 512 return 0;
982c350b 513
5072c59f
SR
514 rec->flags &= ~FTRACE_FL_ENABLED;
515 }
516 }
517
982c350b 518 if (rec->flags & FTRACE_FL_ENABLED)
e7d3737e 519 return ftrace_make_call(rec, ftrace_addr);
31e88909 520 else
e7d3737e 521 return ftrace_make_nop(NULL, rec, ftrace_addr);
5072c59f
SR
522}
523
e309b41d 524static void ftrace_replace_code(int enable)
3c1720f0 525{
0eb96701 526 int i, failed;
3c1720f0
SR
527 struct dyn_ftrace *rec;
528 struct ftrace_page *pg;
3c1720f0 529
3c1720f0
SR
530 for (pg = ftrace_pages_start; pg; pg = pg->next) {
531 for (i = 0; i < pg->index; i++) {
532 rec = &pg->records[i];
533
918c1154
SR
534 /*
535 * Skip over free records and records that have
536 * failed.
537 */
538 if (rec->flags & FTRACE_FL_FREE ||
539 rec->flags & FTRACE_FL_FAILED)
3c1720f0
SR
540 continue;
541
f22f9a89 542 /* ignore updates to this record's mcount site */
98a05ed4
AS
543 if (get_kprobe((void *)rec->ip)) {
544 freeze_record(rec);
f22f9a89 545 continue;
98a05ed4
AS
546 } else {
547 unfreeze_record(rec);
548 }
f22f9a89 549
31e88909 550 failed = __ftrace_replace_code(rec, enable);
0eb96701
AS
551 if (failed && (rec->flags & FTRACE_FL_CONVERTED)) {
552 rec->flags |= FTRACE_FL_FAILED;
553 if ((system_state == SYSTEM_BOOTING) ||
34078a5e 554 !core_kernel_text(rec->ip)) {
0eb96701 555 ftrace_free_rec(rec);
b17e8a37 556 } else
31e88909 557 ftrace_bug(failed, rec->ip);
0eb96701 558 }
3c1720f0
SR
559 }
560 }
561}
562
492a7ea5 563static int
31e88909 564ftrace_code_disable(struct module *mod, struct dyn_ftrace *rec)
3c1720f0
SR
565{
566 unsigned long ip;
593eb8a2 567 int ret;
3c1720f0
SR
568
569 ip = rec->ip;
570
25aac9dc 571 ret = ftrace_make_nop(mod, rec, MCOUNT_ADDR);
593eb8a2 572 if (ret) {
31e88909 573 ftrace_bug(ret, ip);
3c1720f0 574 rec->flags |= FTRACE_FL_FAILED;
492a7ea5 575 return 0;
37ad5084 576 }
492a7ea5 577 return 1;
3c1720f0
SR
578}
579
e309b41d 580static int __ftrace_modify_code(void *data)
3d083395 581{
d61f82d0
SR
582 int *command = data;
583
a3583244 584 if (*command & FTRACE_ENABLE_CALLS)
d61f82d0 585 ftrace_replace_code(1);
a3583244 586 else if (*command & FTRACE_DISABLE_CALLS)
d61f82d0
SR
587 ftrace_replace_code(0);
588
589 if (*command & FTRACE_UPDATE_TRACE_FUNC)
590 ftrace_update_ftrace_func(ftrace_trace_function);
591
5a45cfe1
SR
592 if (*command & FTRACE_START_FUNC_RET)
593 ftrace_enable_ftrace_graph_caller();
594 else if (*command & FTRACE_STOP_FUNC_RET)
595 ftrace_disable_ftrace_graph_caller();
596
d61f82d0 597 return 0;
3d083395
SR
598}
599
e309b41d 600static void ftrace_run_update_code(int command)
3d083395 601{
784e2d76 602 stop_machine(__ftrace_modify_code, &command, NULL);
3d083395
SR
603}
604
d61f82d0 605static ftrace_func_t saved_ftrace_func;
60a7ecf4 606static int ftrace_start_up;
df4fc315
SR
607
608static void ftrace_startup_enable(int command)
609{
610 if (saved_ftrace_func != ftrace_trace_function) {
611 saved_ftrace_func = ftrace_trace_function;
612 command |= FTRACE_UPDATE_TRACE_FUNC;
613 }
614
615 if (!command || !ftrace_enabled)
616 return;
617
618 ftrace_run_update_code(command);
619}
d61f82d0 620
5a45cfe1 621static void ftrace_startup(int command)
3d083395 622{
4eebcc81
SR
623 if (unlikely(ftrace_disabled))
624 return;
625
cb7be3b2 626 mutex_lock(&ftrace_start_lock);
60a7ecf4 627 ftrace_start_up++;
982c350b 628 command |= FTRACE_ENABLE_CALLS;
d61f82d0 629
df4fc315 630 ftrace_startup_enable(command);
3d083395 631
cb7be3b2 632 mutex_unlock(&ftrace_start_lock);
3d083395
SR
633}
634
5a45cfe1 635static void ftrace_shutdown(int command)
3d083395 636{
4eebcc81
SR
637 if (unlikely(ftrace_disabled))
638 return;
639
cb7be3b2 640 mutex_lock(&ftrace_start_lock);
60a7ecf4
SR
641 ftrace_start_up--;
642 if (!ftrace_start_up)
d61f82d0 643 command |= FTRACE_DISABLE_CALLS;
3d083395 644
d61f82d0
SR
645 if (saved_ftrace_func != ftrace_trace_function) {
646 saved_ftrace_func = ftrace_trace_function;
647 command |= FTRACE_UPDATE_TRACE_FUNC;
648 }
3d083395 649
d61f82d0
SR
650 if (!command || !ftrace_enabled)
651 goto out;
652
653 ftrace_run_update_code(command);
3d083395 654 out:
cb7be3b2 655 mutex_unlock(&ftrace_start_lock);
3d083395
SR
656}
657
e309b41d 658static void ftrace_startup_sysctl(void)
b0fc494f 659{
d61f82d0
SR
660 int command = FTRACE_ENABLE_MCOUNT;
661
4eebcc81
SR
662 if (unlikely(ftrace_disabled))
663 return;
664
cb7be3b2 665 mutex_lock(&ftrace_start_lock);
d61f82d0
SR
666 /* Force update next time */
667 saved_ftrace_func = NULL;
60a7ecf4
SR
668 /* ftrace_start_up is true if we want ftrace running */
669 if (ftrace_start_up)
d61f82d0
SR
670 command |= FTRACE_ENABLE_CALLS;
671
672 ftrace_run_update_code(command);
cb7be3b2 673 mutex_unlock(&ftrace_start_lock);
b0fc494f
SR
674}
675
e309b41d 676static void ftrace_shutdown_sysctl(void)
b0fc494f 677{
d61f82d0
SR
678 int command = FTRACE_DISABLE_MCOUNT;
679
4eebcc81
SR
680 if (unlikely(ftrace_disabled))
681 return;
682
cb7be3b2 683 mutex_lock(&ftrace_start_lock);
60a7ecf4
SR
684 /* ftrace_start_up is true if ftrace is running */
685 if (ftrace_start_up)
d61f82d0
SR
686 command |= FTRACE_DISABLE_CALLS;
687
688 ftrace_run_update_code(command);
cb7be3b2 689 mutex_unlock(&ftrace_start_lock);
b0fc494f
SR
690}
691
3d083395
SR
692static cycle_t ftrace_update_time;
693static unsigned long ftrace_update_cnt;
694unsigned long ftrace_update_tot_cnt;
695
31e88909 696static int ftrace_update_code(struct module *mod)
3d083395 697{
08f5ac90 698 struct dyn_ftrace *p, *t;
f22f9a89 699 cycle_t start, stop;
3d083395 700
750ed1a4 701 start = ftrace_now(raw_smp_processor_id());
3d083395
SR
702 ftrace_update_cnt = 0;
703
08f5ac90 704 list_for_each_entry_safe(p, t, &ftrace_new_addrs, list) {
3d083395 705
08f5ac90
SR
706 /* If something went wrong, bail without enabling anything */
707 if (unlikely(ftrace_disabled))
708 return -1;
f22f9a89 709
08f5ac90 710 list_del_init(&p->list);
f22f9a89 711
08f5ac90 712 /* convert record (i.e, patch mcount-call with NOP) */
31e88909 713 if (ftrace_code_disable(mod, p)) {
08f5ac90
SR
714 p->flags |= FTRACE_FL_CONVERTED;
715 ftrace_update_cnt++;
716 } else
717 ftrace_free_rec(p);
3d083395
SR
718 }
719
750ed1a4 720 stop = ftrace_now(raw_smp_processor_id());
3d083395
SR
721 ftrace_update_time = stop - start;
722 ftrace_update_tot_cnt += ftrace_update_cnt;
723
16444a8a
ACM
724 return 0;
725}
726
68bf21aa 727static int __init ftrace_dyn_table_alloc(unsigned long num_to_init)
3c1720f0
SR
728{
729 struct ftrace_page *pg;
730 int cnt;
731 int i;
3c1720f0
SR
732
733 /* allocate a few pages */
734 ftrace_pages_start = (void *)get_zeroed_page(GFP_KERNEL);
735 if (!ftrace_pages_start)
736 return -1;
737
738 /*
739 * Allocate a few more pages.
740 *
741 * TODO: have some parser search vmlinux before
742 * final linking to find all calls to ftrace.
743 * Then we can:
744 * a) know how many pages to allocate.
745 * and/or
746 * b) set up the table then.
747 *
748 * The dynamic code is still necessary for
749 * modules.
750 */
751
752 pg = ftrace_pages = ftrace_pages_start;
753
68bf21aa 754 cnt = num_to_init / ENTRIES_PER_PAGE;
08f5ac90 755 pr_info("ftrace: allocating %ld entries in %d pages\n",
5821e1b7 756 num_to_init, cnt + 1);
3c1720f0
SR
757
758 for (i = 0; i < cnt; i++) {
759 pg->next = (void *)get_zeroed_page(GFP_KERNEL);
760
761 /* If we fail, we'll try later anyway */
762 if (!pg->next)
763 break;
764
765 pg = pg->next;
766 }
767
768 return 0;
769}
770
5072c59f
SR
771enum {
772 FTRACE_ITER_FILTER = (1 << 0),
773 FTRACE_ITER_CONT = (1 << 1),
41c52c0d 774 FTRACE_ITER_NOTRACE = (1 << 2),
eb9a7bf0 775 FTRACE_ITER_FAILURES = (1 << 3),
0c75a3ed 776 FTRACE_ITER_PRINTALL = (1 << 4),
5072c59f
SR
777};
778
779#define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
780
781struct ftrace_iterator {
5072c59f 782 struct ftrace_page *pg;
431aa3fb 783 int idx;
5072c59f
SR
784 unsigned flags;
785 unsigned char buffer[FTRACE_BUFF_MAX+1];
786 unsigned buffer_idx;
787 unsigned filtered;
788};
789
e309b41d 790static void *
5072c59f
SR
791t_next(struct seq_file *m, void *v, loff_t *pos)
792{
793 struct ftrace_iterator *iter = m->private;
794 struct dyn_ftrace *rec = NULL;
795
796 (*pos)++;
797
0c75a3ed
SR
798 if (iter->flags & FTRACE_ITER_PRINTALL)
799 return NULL;
800
99ecdc43
SR
801 /* should not be called from interrupt context */
802 spin_lock(&ftrace_lock);
5072c59f
SR
803 retry:
804 if (iter->idx >= iter->pg->index) {
805 if (iter->pg->next) {
806 iter->pg = iter->pg->next;
807 iter->idx = 0;
808 goto retry;
50cdaf08
LW
809 } else {
810 iter->idx = -1;
5072c59f
SR
811 }
812 } else {
813 rec = &iter->pg->records[iter->idx++];
a9fdda33
SR
814 if ((rec->flags & FTRACE_FL_FREE) ||
815
816 (!(iter->flags & FTRACE_ITER_FAILURES) &&
eb9a7bf0
AS
817 (rec->flags & FTRACE_FL_FAILED)) ||
818
819 ((iter->flags & FTRACE_ITER_FAILURES) &&
a9fdda33 820 !(rec->flags & FTRACE_FL_FAILED)) ||
eb9a7bf0 821
0183fb1c
SR
822 ((iter->flags & FTRACE_ITER_FILTER) &&
823 !(rec->flags & FTRACE_FL_FILTER)) ||
824
41c52c0d
SR
825 ((iter->flags & FTRACE_ITER_NOTRACE) &&
826 !(rec->flags & FTRACE_FL_NOTRACE))) {
5072c59f
SR
827 rec = NULL;
828 goto retry;
829 }
830 }
99ecdc43 831 spin_unlock(&ftrace_lock);
5072c59f 832
5072c59f
SR
833 return rec;
834}
835
836static void *t_start(struct seq_file *m, loff_t *pos)
837{
838 struct ftrace_iterator *iter = m->private;
839 void *p = NULL;
5072c59f 840
0c75a3ed
SR
841 /*
842 * For set_ftrace_filter reading, if we have the filter
843 * off, we can short cut and just print out that all
844 * functions are enabled.
845 */
846 if (iter->flags & FTRACE_ITER_FILTER && !ftrace_filtered) {
847 if (*pos > 0)
848 return NULL;
849 iter->flags |= FTRACE_ITER_PRINTALL;
850 (*pos)++;
851 return iter;
852 }
853
50cdaf08
LW
854 if (*pos > 0) {
855 if (iter->idx < 0)
856 return p;
857 (*pos)--;
858 iter->idx--;
859 }
5821e1b7 860
50cdaf08 861 p = t_next(m, p, pos);
5072c59f
SR
862
863 return p;
864}
865
866static void t_stop(struct seq_file *m, void *p)
867{
868}
869
870static int t_show(struct seq_file *m, void *v)
871{
0c75a3ed 872 struct ftrace_iterator *iter = m->private;
5072c59f
SR
873 struct dyn_ftrace *rec = v;
874 char str[KSYM_SYMBOL_LEN];
875
0c75a3ed
SR
876 if (iter->flags & FTRACE_ITER_PRINTALL) {
877 seq_printf(m, "#### all functions enabled ####\n");
878 return 0;
879 }
880
5072c59f
SR
881 if (!rec)
882 return 0;
883
884 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
885
50cdaf08 886 seq_printf(m, "%s\n", str);
5072c59f
SR
887
888 return 0;
889}
890
891static struct seq_operations show_ftrace_seq_ops = {
892 .start = t_start,
893 .next = t_next,
894 .stop = t_stop,
895 .show = t_show,
896};
897
e309b41d 898static int
5072c59f
SR
899ftrace_avail_open(struct inode *inode, struct file *file)
900{
901 struct ftrace_iterator *iter;
902 int ret;
903
4eebcc81
SR
904 if (unlikely(ftrace_disabled))
905 return -ENODEV;
906
5072c59f
SR
907 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
908 if (!iter)
909 return -ENOMEM;
910
911 iter->pg = ftrace_pages_start;
5072c59f
SR
912
913 ret = seq_open(file, &show_ftrace_seq_ops);
914 if (!ret) {
915 struct seq_file *m = file->private_data;
4bf39a94 916
5072c59f 917 m->private = iter;
4bf39a94 918 } else {
5072c59f 919 kfree(iter);
4bf39a94 920 }
5072c59f
SR
921
922 return ret;
923}
924
925int ftrace_avail_release(struct inode *inode, struct file *file)
926{
927 struct seq_file *m = (struct seq_file *)file->private_data;
928 struct ftrace_iterator *iter = m->private;
929
930 seq_release(inode, file);
931 kfree(iter);
4bf39a94 932
5072c59f
SR
933 return 0;
934}
935
eb9a7bf0
AS
936static int
937ftrace_failures_open(struct inode *inode, struct file *file)
938{
939 int ret;
940 struct seq_file *m;
941 struct ftrace_iterator *iter;
942
943 ret = ftrace_avail_open(inode, file);
944 if (!ret) {
945 m = (struct seq_file *)file->private_data;
946 iter = (struct ftrace_iterator *)m->private;
947 iter->flags = FTRACE_ITER_FAILURES;
948 }
949
950 return ret;
951}
952
953
41c52c0d 954static void ftrace_filter_reset(int enable)
5072c59f
SR
955{
956 struct ftrace_page *pg;
957 struct dyn_ftrace *rec;
41c52c0d 958 unsigned long type = enable ? FTRACE_FL_FILTER : FTRACE_FL_NOTRACE;
5072c59f
SR
959 unsigned i;
960
99ecdc43
SR
961 /* should not be called from interrupt context */
962 spin_lock(&ftrace_lock);
41c52c0d
SR
963 if (enable)
964 ftrace_filtered = 0;
5072c59f
SR
965 pg = ftrace_pages_start;
966 while (pg) {
967 for (i = 0; i < pg->index; i++) {
968 rec = &pg->records[i];
969 if (rec->flags & FTRACE_FL_FAILED)
970 continue;
41c52c0d 971 rec->flags &= ~type;
5072c59f
SR
972 }
973 pg = pg->next;
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
e309b41d 1056static void
41c52c0d 1057ftrace_match(unsigned char *buff, int len, int enable)
5072c59f
SR
1058{
1059 char str[KSYM_SYMBOL_LEN];
1060 char *search = NULL;
1061 struct ftrace_page *pg;
1062 struct dyn_ftrace *rec;
1063 int type = MATCH_FULL;
41c52c0d 1064 unsigned long flag = enable ? FTRACE_FL_FILTER : FTRACE_FL_NOTRACE;
5072c59f 1065 unsigned i, match = 0, search_len = 0;
ea3a6d6d
SR
1066 int not = 0;
1067
1068 if (buff[0] == '!') {
1069 not = 1;
1070 buff++;
1071 len--;
1072 }
5072c59f
SR
1073
1074 for (i = 0; i < len; i++) {
1075 if (buff[i] == '*') {
1076 if (!i) {
1077 search = buff + i + 1;
1078 type = MATCH_END_ONLY;
1079 search_len = len - (i + 1);
1080 } else {
1081 if (type == MATCH_END_ONLY) {
1082 type = MATCH_MIDDLE_ONLY;
1083 } else {
1084 match = i;
1085 type = MATCH_FRONT_ONLY;
1086 }
1087 buff[i] = 0;
1088 break;
1089 }
1090 }
1091 }
1092
99ecdc43
SR
1093 /* should not be called from interrupt context */
1094 spin_lock(&ftrace_lock);
41c52c0d
SR
1095 if (enable)
1096 ftrace_filtered = 1;
5072c59f
SR
1097 pg = ftrace_pages_start;
1098 while (pg) {
1099 for (i = 0; i < pg->index; i++) {
1100 int matched = 0;
1101 char *ptr;
1102
1103 rec = &pg->records[i];
1104 if (rec->flags & FTRACE_FL_FAILED)
1105 continue;
1106 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
1107 switch (type) {
1108 case MATCH_FULL:
1109 if (strcmp(str, buff) == 0)
1110 matched = 1;
1111 break;
1112 case MATCH_FRONT_ONLY:
1113 if (memcmp(str, buff, match) == 0)
1114 matched = 1;
1115 break;
1116 case MATCH_MIDDLE_ONLY:
1117 if (strstr(str, search))
1118 matched = 1;
1119 break;
1120 case MATCH_END_ONLY:
1121 ptr = strstr(str, search);
1122 if (ptr && (ptr[search_len] == 0))
1123 matched = 1;
1124 break;
1125 }
ea3a6d6d
SR
1126 if (matched) {
1127 if (not)
1128 rec->flags &= ~flag;
1129 else
1130 rec->flags |= flag;
1131 }
5072c59f
SR
1132 }
1133 pg = pg->next;
1134 }
99ecdc43 1135 spin_unlock(&ftrace_lock);
5072c59f
SR
1136}
1137
e309b41d 1138static ssize_t
41c52c0d
SR
1139ftrace_regex_write(struct file *file, const char __user *ubuf,
1140 size_t cnt, loff_t *ppos, int enable)
5072c59f
SR
1141{
1142 struct ftrace_iterator *iter;
1143 char ch;
1144 size_t read = 0;
1145 ssize_t ret;
1146
1147 if (!cnt || cnt < 0)
1148 return 0;
1149
41c52c0d 1150 mutex_lock(&ftrace_regex_lock);
5072c59f
SR
1151
1152 if (file->f_mode & FMODE_READ) {
1153 struct seq_file *m = file->private_data;
1154 iter = m->private;
1155 } else
1156 iter = file->private_data;
1157
1158 if (!*ppos) {
1159 iter->flags &= ~FTRACE_ITER_CONT;
1160 iter->buffer_idx = 0;
1161 }
1162
1163 ret = get_user(ch, ubuf++);
1164 if (ret)
1165 goto out;
1166 read++;
1167 cnt--;
1168
1169 if (!(iter->flags & ~FTRACE_ITER_CONT)) {
1170 /* skip white space */
1171 while (cnt && isspace(ch)) {
1172 ret = get_user(ch, ubuf++);
1173 if (ret)
1174 goto out;
1175 read++;
1176 cnt--;
1177 }
1178
5072c59f
SR
1179 if (isspace(ch)) {
1180 file->f_pos += read;
1181 ret = read;
1182 goto out;
1183 }
1184
1185 iter->buffer_idx = 0;
1186 }
1187
1188 while (cnt && !isspace(ch)) {
1189 if (iter->buffer_idx < FTRACE_BUFF_MAX)
1190 iter->buffer[iter->buffer_idx++] = ch;
1191 else {
1192 ret = -EINVAL;
1193 goto out;
1194 }
1195 ret = get_user(ch, ubuf++);
1196 if (ret)
1197 goto out;
1198 read++;
1199 cnt--;
1200 }
1201
1202 if (isspace(ch)) {
1203 iter->filtered++;
1204 iter->buffer[iter->buffer_idx] = 0;
41c52c0d 1205 ftrace_match(iter->buffer, iter->buffer_idx, enable);
5072c59f
SR
1206 iter->buffer_idx = 0;
1207 } else
1208 iter->flags |= FTRACE_ITER_CONT;
1209
1210
1211 file->f_pos += read;
1212
1213 ret = read;
1214 out:
41c52c0d 1215 mutex_unlock(&ftrace_regex_lock);
5072c59f
SR
1216
1217 return ret;
1218}
1219
41c52c0d
SR
1220static ssize_t
1221ftrace_filter_write(struct file *file, const char __user *ubuf,
1222 size_t cnt, loff_t *ppos)
1223{
1224 return ftrace_regex_write(file, ubuf, cnt, ppos, 1);
1225}
1226
1227static ssize_t
1228ftrace_notrace_write(struct file *file, const char __user *ubuf,
1229 size_t cnt, loff_t *ppos)
1230{
1231 return ftrace_regex_write(file, ubuf, cnt, ppos, 0);
1232}
1233
1234static void
1235ftrace_set_regex(unsigned char *buf, int len, int reset, int enable)
1236{
1237 if (unlikely(ftrace_disabled))
1238 return;
1239
1240 mutex_lock(&ftrace_regex_lock);
1241 if (reset)
1242 ftrace_filter_reset(enable);
1243 if (buf)
1244 ftrace_match(buf, len, enable);
1245 mutex_unlock(&ftrace_regex_lock);
1246}
1247
77a2b37d
SR
1248/**
1249 * ftrace_set_filter - set a function to filter on in ftrace
1250 * @buf - the string that holds the function filter text.
1251 * @len - the length of the string.
1252 * @reset - non zero to reset all filters before applying this filter.
1253 *
1254 * Filters denote which functions should be enabled when tracing is enabled.
1255 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
1256 */
e309b41d 1257void ftrace_set_filter(unsigned char *buf, int len, int reset)
77a2b37d 1258{
41c52c0d
SR
1259 ftrace_set_regex(buf, len, reset, 1);
1260}
4eebcc81 1261
41c52c0d
SR
1262/**
1263 * ftrace_set_notrace - set a function to not trace in ftrace
1264 * @buf - the string that holds the function notrace text.
1265 * @len - the length of the string.
1266 * @reset - non zero to reset all filters before applying this filter.
1267 *
1268 * Notrace Filters denote which functions should not be enabled when tracing
1269 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
1270 * for tracing.
1271 */
1272void ftrace_set_notrace(unsigned char *buf, int len, int reset)
1273{
1274 ftrace_set_regex(buf, len, reset, 0);
77a2b37d
SR
1275}
1276
e309b41d 1277static int
41c52c0d 1278ftrace_regex_release(struct inode *inode, struct file *file, int enable)
5072c59f
SR
1279{
1280 struct seq_file *m = (struct seq_file *)file->private_data;
1281 struct ftrace_iterator *iter;
1282
41c52c0d 1283 mutex_lock(&ftrace_regex_lock);
5072c59f
SR
1284 if (file->f_mode & FMODE_READ) {
1285 iter = m->private;
1286
1287 seq_release(inode, file);
1288 } else
1289 iter = file->private_data;
1290
1291 if (iter->buffer_idx) {
1292 iter->filtered++;
1293 iter->buffer[iter->buffer_idx] = 0;
41c52c0d 1294 ftrace_match(iter->buffer, iter->buffer_idx, enable);
5072c59f
SR
1295 }
1296
1297 mutex_lock(&ftrace_sysctl_lock);
cb7be3b2 1298 mutex_lock(&ftrace_start_lock);
ee02a2e5 1299 if (ftrace_start_up && ftrace_enabled)
5072c59f 1300 ftrace_run_update_code(FTRACE_ENABLE_CALLS);
cb7be3b2 1301 mutex_unlock(&ftrace_start_lock);
5072c59f
SR
1302 mutex_unlock(&ftrace_sysctl_lock);
1303
1304 kfree(iter);
41c52c0d 1305 mutex_unlock(&ftrace_regex_lock);
5072c59f
SR
1306 return 0;
1307}
1308
41c52c0d
SR
1309static int
1310ftrace_filter_release(struct inode *inode, struct file *file)
1311{
1312 return ftrace_regex_release(inode, file, 1);
1313}
1314
1315static int
1316ftrace_notrace_release(struct inode *inode, struct file *file)
1317{
1318 return ftrace_regex_release(inode, file, 0);
1319}
1320
5072c59f
SR
1321static struct file_operations ftrace_avail_fops = {
1322 .open = ftrace_avail_open,
1323 .read = seq_read,
1324 .llseek = seq_lseek,
1325 .release = ftrace_avail_release,
1326};
1327
eb9a7bf0
AS
1328static struct file_operations ftrace_failures_fops = {
1329 .open = ftrace_failures_open,
1330 .read = seq_read,
1331 .llseek = seq_lseek,
1332 .release = ftrace_avail_release,
1333};
1334
5072c59f
SR
1335static struct file_operations ftrace_filter_fops = {
1336 .open = ftrace_filter_open,
41c52c0d 1337 .read = ftrace_regex_read,
5072c59f 1338 .write = ftrace_filter_write,
41c52c0d 1339 .llseek = ftrace_regex_lseek,
5072c59f
SR
1340 .release = ftrace_filter_release,
1341};
1342
41c52c0d
SR
1343static struct file_operations ftrace_notrace_fops = {
1344 .open = ftrace_notrace_open,
1345 .read = ftrace_regex_read,
1346 .write = ftrace_notrace_write,
1347 .llseek = ftrace_regex_lseek,
1348 .release = ftrace_notrace_release,
1349};
1350
ea4e2bc4
SR
1351#ifdef CONFIG_FUNCTION_GRAPH_TRACER
1352
1353static DEFINE_MUTEX(graph_lock);
1354
1355int ftrace_graph_count;
1356unsigned long ftrace_graph_funcs[FTRACE_GRAPH_MAX_FUNCS] __read_mostly;
1357
1358static void *
1359g_next(struct seq_file *m, void *v, loff_t *pos)
1360{
1361 unsigned long *array = m->private;
1362 int index = *pos;
1363
1364 (*pos)++;
1365
1366 if (index >= ftrace_graph_count)
1367 return NULL;
1368
1369 return &array[index];
1370}
1371
1372static void *g_start(struct seq_file *m, loff_t *pos)
1373{
1374 void *p = NULL;
1375
1376 mutex_lock(&graph_lock);
1377
1378 p = g_next(m, p, pos);
1379
1380 return p;
1381}
1382
1383static void g_stop(struct seq_file *m, void *p)
1384{
1385 mutex_unlock(&graph_lock);
1386}
1387
1388static int g_show(struct seq_file *m, void *v)
1389{
1390 unsigned long *ptr = v;
1391 char str[KSYM_SYMBOL_LEN];
1392
1393 if (!ptr)
1394 return 0;
1395
1396 kallsyms_lookup(*ptr, NULL, NULL, NULL, str);
1397
1398 seq_printf(m, "%s\n", str);
1399
1400 return 0;
1401}
1402
1403static struct seq_operations ftrace_graph_seq_ops = {
1404 .start = g_start,
1405 .next = g_next,
1406 .stop = g_stop,
1407 .show = g_show,
1408};
1409
1410static int
1411ftrace_graph_open(struct inode *inode, struct file *file)
1412{
1413 int ret = 0;
1414
1415 if (unlikely(ftrace_disabled))
1416 return -ENODEV;
1417
1418 mutex_lock(&graph_lock);
1419 if ((file->f_mode & FMODE_WRITE) &&
1420 !(file->f_flags & O_APPEND)) {
1421 ftrace_graph_count = 0;
1422 memset(ftrace_graph_funcs, 0, sizeof(ftrace_graph_funcs));
1423 }
1424
1425 if (file->f_mode & FMODE_READ) {
1426 ret = seq_open(file, &ftrace_graph_seq_ops);
1427 if (!ret) {
1428 struct seq_file *m = file->private_data;
1429 m->private = ftrace_graph_funcs;
1430 }
1431 } else
1432 file->private_data = ftrace_graph_funcs;
1433 mutex_unlock(&graph_lock);
1434
1435 return ret;
1436}
1437
1438static ssize_t
1439ftrace_graph_read(struct file *file, char __user *ubuf,
1440 size_t cnt, loff_t *ppos)
1441{
1442 if (file->f_mode & FMODE_READ)
1443 return seq_read(file, ubuf, cnt, ppos);
1444 else
1445 return -EPERM;
1446}
1447
1448static int
1449ftrace_set_func(unsigned long *array, int idx, char *buffer)
1450{
1451 char str[KSYM_SYMBOL_LEN];
1452 struct dyn_ftrace *rec;
1453 struct ftrace_page *pg;
1454 int found = 0;
faec2ec5 1455 int i, j;
ea4e2bc4
SR
1456
1457 if (ftrace_disabled)
1458 return -ENODEV;
1459
1460 /* should not be called from interrupt context */
1461 spin_lock(&ftrace_lock);
1462
1463 for (pg = ftrace_pages_start; pg; pg = pg->next) {
1464 for (i = 0; i < pg->index; i++) {
1465 rec = &pg->records[i];
1466
1467 if (rec->flags & (FTRACE_FL_FAILED | FTRACE_FL_FREE))
1468 continue;
1469
1470 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
1471 if (strcmp(str, buffer) == 0) {
1472 found = 1;
faec2ec5
LW
1473 for (j = 0; j < idx; j++)
1474 if (array[j] == rec->ip) {
1475 found = 0;
1476 break;
1477 }
1478 if (found)
1479 array[idx] = rec->ip;
ea4e2bc4
SR
1480 break;
1481 }
1482 }
1483 }
1484 spin_unlock(&ftrace_lock);
1485
1486 return found ? 0 : -EINVAL;
1487}
1488
1489static ssize_t
1490ftrace_graph_write(struct file *file, const char __user *ubuf,
1491 size_t cnt, loff_t *ppos)
1492{
1493 unsigned char buffer[FTRACE_BUFF_MAX+1];
1494 unsigned long *array;
1495 size_t read = 0;
1496 ssize_t ret;
1497 int index = 0;
1498 char ch;
1499
1500 if (!cnt || cnt < 0)
1501 return 0;
1502
1503 mutex_lock(&graph_lock);
1504
1505 if (ftrace_graph_count >= FTRACE_GRAPH_MAX_FUNCS) {
1506 ret = -EBUSY;
1507 goto out;
1508 }
1509
1510 if (file->f_mode & FMODE_READ) {
1511 struct seq_file *m = file->private_data;
1512 array = m->private;
1513 } else
1514 array = file->private_data;
1515
1516 ret = get_user(ch, ubuf++);
1517 if (ret)
1518 goto out;
1519 read++;
1520 cnt--;
1521
1522 /* skip white space */
1523 while (cnt && isspace(ch)) {
1524 ret = get_user(ch, ubuf++);
1525 if (ret)
1526 goto out;
1527 read++;
1528 cnt--;
1529 }
1530
1531 if (isspace(ch)) {
1532 *ppos += read;
1533 ret = read;
1534 goto out;
1535 }
1536
1537 while (cnt && !isspace(ch)) {
1538 if (index < FTRACE_BUFF_MAX)
1539 buffer[index++] = ch;
1540 else {
1541 ret = -EINVAL;
1542 goto out;
1543 }
1544 ret = get_user(ch, ubuf++);
1545 if (ret)
1546 goto out;
1547 read++;
1548 cnt--;
1549 }
1550 buffer[index] = 0;
1551
1552 /* we allow only one at a time */
1553 ret = ftrace_set_func(array, ftrace_graph_count, buffer);
1554 if (ret)
1555 goto out;
1556
1557 ftrace_graph_count++;
1558
1559 file->f_pos += read;
1560
1561 ret = read;
1562 out:
1563 mutex_unlock(&graph_lock);
1564
1565 return ret;
1566}
1567
1568static const struct file_operations ftrace_graph_fops = {
1569 .open = ftrace_graph_open,
1570 .read = ftrace_graph_read,
1571 .write = ftrace_graph_write,
1572};
1573#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
1574
df4fc315 1575static __init int ftrace_init_dyn_debugfs(struct dentry *d_tracer)
5072c59f 1576{
5072c59f
SR
1577 struct dentry *entry;
1578
5072c59f
SR
1579 entry = debugfs_create_file("available_filter_functions", 0444,
1580 d_tracer, NULL, &ftrace_avail_fops);
1581 if (!entry)
1582 pr_warning("Could not create debugfs "
1583 "'available_filter_functions' entry\n");
1584
eb9a7bf0
AS
1585 entry = debugfs_create_file("failures", 0444,
1586 d_tracer, NULL, &ftrace_failures_fops);
1587 if (!entry)
1588 pr_warning("Could not create debugfs 'failures' entry\n");
1589
5072c59f
SR
1590 entry = debugfs_create_file("set_ftrace_filter", 0644, d_tracer,
1591 NULL, &ftrace_filter_fops);
1592 if (!entry)
1593 pr_warning("Could not create debugfs "
1594 "'set_ftrace_filter' entry\n");
41c52c0d
SR
1595
1596 entry = debugfs_create_file("set_ftrace_notrace", 0644, d_tracer,
1597 NULL, &ftrace_notrace_fops);
1598 if (!entry)
1599 pr_warning("Could not create debugfs "
1600 "'set_ftrace_notrace' entry\n");
ad90c0e3 1601
ea4e2bc4
SR
1602#ifdef CONFIG_FUNCTION_GRAPH_TRACER
1603 entry = debugfs_create_file("set_graph_function", 0444, d_tracer,
1604 NULL,
1605 &ftrace_graph_fops);
1606 if (!entry)
1607 pr_warning("Could not create debugfs "
1608 "'set_graph_function' entry\n");
1609#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
1610
5072c59f
SR
1611 return 0;
1612}
1613
31e88909
SR
1614static int ftrace_convert_nops(struct module *mod,
1615 unsigned long *start,
68bf21aa
SR
1616 unsigned long *end)
1617{
1618 unsigned long *p;
1619 unsigned long addr;
1620 unsigned long flags;
1621
08f5ac90 1622 mutex_lock(&ftrace_start_lock);
68bf21aa
SR
1623 p = start;
1624 while (p < end) {
1625 addr = ftrace_call_adjust(*p++);
20e5227e
SR
1626 /*
1627 * Some architecture linkers will pad between
1628 * the different mcount_loc sections of different
1629 * object files to satisfy alignments.
1630 * Skip any NULL pointers.
1631 */
1632 if (!addr)
1633 continue;
68bf21aa 1634 ftrace_record_ip(addr);
68bf21aa
SR
1635 }
1636
08f5ac90 1637 /* disable interrupts to prevent kstop machine */
68bf21aa 1638 local_irq_save(flags);
31e88909 1639 ftrace_update_code(mod);
68bf21aa 1640 local_irq_restore(flags);
08f5ac90 1641 mutex_unlock(&ftrace_start_lock);
68bf21aa
SR
1642
1643 return 0;
1644}
1645
31e88909
SR
1646void ftrace_init_module(struct module *mod,
1647 unsigned long *start, unsigned long *end)
90d595fe 1648{
00fd61ae 1649 if (ftrace_disabled || start == end)
fed1939c 1650 return;
31e88909 1651 ftrace_convert_nops(mod, start, end);
90d595fe
SR
1652}
1653
68bf21aa
SR
1654extern unsigned long __start_mcount_loc[];
1655extern unsigned long __stop_mcount_loc[];
1656
1657void __init ftrace_init(void)
1658{
1659 unsigned long count, addr, flags;
1660 int ret;
1661
1662 /* Keep the ftrace pointer to the stub */
1663 addr = (unsigned long)ftrace_stub;
1664
1665 local_irq_save(flags);
1666 ftrace_dyn_arch_init(&addr);
1667 local_irq_restore(flags);
1668
1669 /* ftrace_dyn_arch_init places the return code in addr */
1670 if (addr)
1671 goto failed;
1672
1673 count = __stop_mcount_loc - __start_mcount_loc;
1674
1675 ret = ftrace_dyn_table_alloc(count);
1676 if (ret)
1677 goto failed;
1678
1679 last_ftrace_enabled = ftrace_enabled = 1;
1680
31e88909
SR
1681 ret = ftrace_convert_nops(NULL,
1682 __start_mcount_loc,
68bf21aa
SR
1683 __stop_mcount_loc);
1684
1685 return;
1686 failed:
1687 ftrace_disabled = 1;
1688}
68bf21aa 1689
3d083395 1690#else
0b6e4d56
FW
1691
1692static int __init ftrace_nodyn_init(void)
1693{
1694 ftrace_enabled = 1;
1695 return 0;
1696}
1697device_initcall(ftrace_nodyn_init);
1698
df4fc315
SR
1699static inline int ftrace_init_dyn_debugfs(struct dentry *d_tracer) { return 0; }
1700static inline void ftrace_startup_enable(int command) { }
5a45cfe1
SR
1701/* Keep as macros so we do not need to define the commands */
1702# define ftrace_startup(command) do { } while (0)
1703# define ftrace_shutdown(command) do { } while (0)
c7aafc54
IM
1704# define ftrace_startup_sysctl() do { } while (0)
1705# define ftrace_shutdown_sysctl() do { } while (0)
3d083395
SR
1706#endif /* CONFIG_DYNAMIC_FTRACE */
1707
df4fc315
SR
1708static ssize_t
1709ftrace_pid_read(struct file *file, char __user *ubuf,
1710 size_t cnt, loff_t *ppos)
1711{
1712 char buf[64];
1713 int r;
1714
e32d8956
SR
1715 if (ftrace_pid_trace == ftrace_swapper_pid)
1716 r = sprintf(buf, "swapper tasks\n");
1717 else if (ftrace_pid_trace)
978f3a45 1718 r = sprintf(buf, "%u\n", pid_nr(ftrace_pid_trace));
df4fc315
SR
1719 else
1720 r = sprintf(buf, "no pid\n");
1721
1722 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
1723}
1724
e32d8956 1725static void clear_ftrace_swapper(void)
978f3a45
SR
1726{
1727 struct task_struct *p;
e32d8956 1728 int cpu;
978f3a45 1729
e32d8956
SR
1730 get_online_cpus();
1731 for_each_online_cpu(cpu) {
1732 p = idle_task(cpu);
978f3a45 1733 clear_tsk_trace_trace(p);
e32d8956
SR
1734 }
1735 put_online_cpus();
1736}
978f3a45 1737
e32d8956
SR
1738static void set_ftrace_swapper(void)
1739{
1740 struct task_struct *p;
1741 int cpu;
1742
1743 get_online_cpus();
1744 for_each_online_cpu(cpu) {
1745 p = idle_task(cpu);
1746 set_tsk_trace_trace(p);
1747 }
1748 put_online_cpus();
978f3a45
SR
1749}
1750
e32d8956
SR
1751static void clear_ftrace_pid(struct pid *pid)
1752{
1753 struct task_struct *p;
1754
229c4ef8 1755 rcu_read_lock();
e32d8956
SR
1756 do_each_pid_task(pid, PIDTYPE_PID, p) {
1757 clear_tsk_trace_trace(p);
1758 } while_each_pid_task(pid, PIDTYPE_PID, p);
229c4ef8
ON
1759 rcu_read_unlock();
1760
e32d8956
SR
1761 put_pid(pid);
1762}
1763
1764static void set_ftrace_pid(struct pid *pid)
978f3a45
SR
1765{
1766 struct task_struct *p;
1767
229c4ef8 1768 rcu_read_lock();
978f3a45
SR
1769 do_each_pid_task(pid, PIDTYPE_PID, p) {
1770 set_tsk_trace_trace(p);
1771 } while_each_pid_task(pid, PIDTYPE_PID, p);
229c4ef8 1772 rcu_read_unlock();
978f3a45
SR
1773}
1774
e32d8956
SR
1775static void clear_ftrace_pid_task(struct pid **pid)
1776{
1777 if (*pid == ftrace_swapper_pid)
1778 clear_ftrace_swapper();
1779 else
1780 clear_ftrace_pid(*pid);
1781
1782 *pid = NULL;
1783}
1784
1785static void set_ftrace_pid_task(struct pid *pid)
1786{
1787 if (pid == ftrace_swapper_pid)
1788 set_ftrace_swapper();
1789 else
1790 set_ftrace_pid(pid);
1791}
1792
df4fc315
SR
1793static ssize_t
1794ftrace_pid_write(struct file *filp, const char __user *ubuf,
1795 size_t cnt, loff_t *ppos)
1796{
978f3a45 1797 struct pid *pid;
df4fc315
SR
1798 char buf[64];
1799 long val;
1800 int ret;
1801
1802 if (cnt >= sizeof(buf))
1803 return -EINVAL;
1804
1805 if (copy_from_user(&buf, ubuf, cnt))
1806 return -EFAULT;
1807
1808 buf[cnt] = 0;
1809
1810 ret = strict_strtol(buf, 10, &val);
1811 if (ret < 0)
1812 return ret;
1813
1814 mutex_lock(&ftrace_start_lock);
978f3a45 1815 if (val < 0) {
df4fc315 1816 /* disable pid tracing */
978f3a45 1817 if (!ftrace_pid_trace)
df4fc315 1818 goto out;
978f3a45
SR
1819
1820 clear_ftrace_pid_task(&ftrace_pid_trace);
df4fc315
SR
1821
1822 } else {
e32d8956
SR
1823 /* swapper task is special */
1824 if (!val) {
1825 pid = ftrace_swapper_pid;
1826 if (pid == ftrace_pid_trace)
1827 goto out;
1828 } else {
1829 pid = find_get_pid(val);
df4fc315 1830
e32d8956
SR
1831 if (pid == ftrace_pid_trace) {
1832 put_pid(pid);
1833 goto out;
1834 }
0ef8cde5 1835 }
0ef8cde5 1836
978f3a45
SR
1837 if (ftrace_pid_trace)
1838 clear_ftrace_pid_task(&ftrace_pid_trace);
1839
1840 if (!pid)
1841 goto out;
1842
1843 ftrace_pid_trace = pid;
1844
1845 set_ftrace_pid_task(ftrace_pid_trace);
df4fc315
SR
1846 }
1847
1848 /* update the function call */
1849 ftrace_update_pid_func();
1850 ftrace_startup_enable(0);
1851
1852 out:
1853 mutex_unlock(&ftrace_start_lock);
1854
1855 return cnt;
1856}
1857
1858static struct file_operations ftrace_pid_fops = {
1859 .read = ftrace_pid_read,
1860 .write = ftrace_pid_write,
1861};
1862
1863static __init int ftrace_init_debugfs(void)
1864{
1865 struct dentry *d_tracer;
1866 struct dentry *entry;
1867
1868 d_tracer = tracing_init_dentry();
1869 if (!d_tracer)
1870 return 0;
1871
1872 ftrace_init_dyn_debugfs(d_tracer);
1873
1874 entry = debugfs_create_file("set_ftrace_pid", 0644, d_tracer,
1875 NULL, &ftrace_pid_fops);
1876 if (!entry)
1877 pr_warning("Could not create debugfs "
1878 "'set_ftrace_pid' entry\n");
1879 return 0;
1880}
1881
1882fs_initcall(ftrace_init_debugfs);
1883
a2bb6a3d 1884/**
81adbdc0 1885 * ftrace_kill - kill ftrace
a2bb6a3d
SR
1886 *
1887 * This function should be used by panic code. It stops ftrace
1888 * but in a not so nice way. If you need to simply kill ftrace
1889 * from a non-atomic section, use ftrace_kill.
1890 */
81adbdc0 1891void ftrace_kill(void)
a2bb6a3d
SR
1892{
1893 ftrace_disabled = 1;
1894 ftrace_enabled = 0;
a2bb6a3d
SR
1895 clear_ftrace_function();
1896}
1897
16444a8a 1898/**
3d083395
SR
1899 * register_ftrace_function - register a function for profiling
1900 * @ops - ops structure that holds the function for profiling.
16444a8a 1901 *
3d083395
SR
1902 * Register a function to be called by all functions in the
1903 * kernel.
1904 *
1905 * Note: @ops->func and all the functions it calls must be labeled
1906 * with "notrace", otherwise it will go into a
1907 * recursive loop.
16444a8a 1908 */
3d083395 1909int register_ftrace_function(struct ftrace_ops *ops)
16444a8a 1910{
b0fc494f
SR
1911 int ret;
1912
4eebcc81
SR
1913 if (unlikely(ftrace_disabled))
1914 return -1;
1915
b0fc494f 1916 mutex_lock(&ftrace_sysctl_lock);
e7d3737e 1917
b0fc494f 1918 ret = __register_ftrace_function(ops);
5a45cfe1 1919 ftrace_startup(0);
b0fc494f 1920
e7d3737e 1921 mutex_unlock(&ftrace_sysctl_lock);
b0fc494f 1922 return ret;
3d083395
SR
1923}
1924
1925/**
32632920 1926 * unregister_ftrace_function - unregister a function for profiling.
3d083395
SR
1927 * @ops - ops structure that holds the function to unregister
1928 *
1929 * Unregister a function that was added to be called by ftrace profiling.
1930 */
1931int unregister_ftrace_function(struct ftrace_ops *ops)
1932{
1933 int ret;
1934
b0fc494f 1935 mutex_lock(&ftrace_sysctl_lock);
3d083395 1936 ret = __unregister_ftrace_function(ops);
5a45cfe1 1937 ftrace_shutdown(0);
b0fc494f
SR
1938 mutex_unlock(&ftrace_sysctl_lock);
1939
1940 return ret;
1941}
1942
e309b41d 1943int
b0fc494f 1944ftrace_enable_sysctl(struct ctl_table *table, int write,
5072c59f 1945 struct file *file, void __user *buffer, size_t *lenp,
b0fc494f
SR
1946 loff_t *ppos)
1947{
1948 int ret;
1949
4eebcc81
SR
1950 if (unlikely(ftrace_disabled))
1951 return -ENODEV;
1952
b0fc494f
SR
1953 mutex_lock(&ftrace_sysctl_lock);
1954
5072c59f 1955 ret = proc_dointvec(table, write, file, buffer, lenp, ppos);
b0fc494f
SR
1956
1957 if (ret || !write || (last_ftrace_enabled == ftrace_enabled))
1958 goto out;
1959
1960 last_ftrace_enabled = ftrace_enabled;
1961
1962 if (ftrace_enabled) {
1963
1964 ftrace_startup_sysctl();
1965
1966 /* we are starting ftrace again */
1967 if (ftrace_list != &ftrace_list_end) {
1968 if (ftrace_list->next == &ftrace_list_end)
1969 ftrace_trace_function = ftrace_list->func;
1970 else
1971 ftrace_trace_function = ftrace_list_func;
1972 }
1973
1974 } else {
1975 /* stopping ftrace calls (just send to ftrace_stub) */
1976 ftrace_trace_function = ftrace_stub;
1977
1978 ftrace_shutdown_sysctl();
1979 }
1980
1981 out:
1982 mutex_unlock(&ftrace_sysctl_lock);
3d083395 1983 return ret;
16444a8a 1984}
f17845e5 1985
fb52607a 1986#ifdef CONFIG_FUNCTION_GRAPH_TRACER
e7d3737e 1987
287b6e68 1988static atomic_t ftrace_graph_active;
4a2b8dda 1989static struct notifier_block ftrace_suspend_notifier;
e7d3737e 1990
e49dc19c
SR
1991int ftrace_graph_entry_stub(struct ftrace_graph_ent *trace)
1992{
1993 return 0;
1994}
1995
287b6e68
FW
1996/* The callbacks that hook a function */
1997trace_func_graph_ret_t ftrace_graph_return =
1998 (trace_func_graph_ret_t)ftrace_stub;
e49dc19c 1999trace_func_graph_ent_t ftrace_graph_entry = ftrace_graph_entry_stub;
f201ae23
FW
2000
2001/* Try to assign a return stack array on FTRACE_RETSTACK_ALLOC_SIZE tasks. */
2002static int alloc_retstack_tasklist(struct ftrace_ret_stack **ret_stack_list)
2003{
2004 int i;
2005 int ret = 0;
2006 unsigned long flags;
2007 int start = 0, end = FTRACE_RETSTACK_ALLOC_SIZE;
2008 struct task_struct *g, *t;
2009
2010 for (i = 0; i < FTRACE_RETSTACK_ALLOC_SIZE; i++) {
2011 ret_stack_list[i] = kmalloc(FTRACE_RETFUNC_DEPTH
2012 * sizeof(struct ftrace_ret_stack),
2013 GFP_KERNEL);
2014 if (!ret_stack_list[i]) {
2015 start = 0;
2016 end = i;
2017 ret = -ENOMEM;
2018 goto free;
2019 }
2020 }
2021
2022 read_lock_irqsave(&tasklist_lock, flags);
2023 do_each_thread(g, t) {
2024 if (start == end) {
2025 ret = -EAGAIN;
2026 goto unlock;
2027 }
2028
2029 if (t->ret_stack == NULL) {
f201ae23 2030 t->curr_ret_stack = -1;
48d68b20
FW
2031 /* Make sure IRQs see the -1 first: */
2032 barrier();
2033 t->ret_stack = ret_stack_list[start++];
380c4b14 2034 atomic_set(&t->tracing_graph_pause, 0);
f201ae23
FW
2035 atomic_set(&t->trace_overrun, 0);
2036 }
2037 } while_each_thread(g, t);
2038
2039unlock:
2040 read_unlock_irqrestore(&tasklist_lock, flags);
2041free:
2042 for (i = start; i < end; i++)
2043 kfree(ret_stack_list[i]);
2044 return ret;
2045}
2046
2047/* Allocate a return stack for each task */
fb52607a 2048static int start_graph_tracing(void)
f201ae23
FW
2049{
2050 struct ftrace_ret_stack **ret_stack_list;
2051 int ret;
2052
2053 ret_stack_list = kmalloc(FTRACE_RETSTACK_ALLOC_SIZE *
2054 sizeof(struct ftrace_ret_stack *),
2055 GFP_KERNEL);
2056
2057 if (!ret_stack_list)
2058 return -ENOMEM;
2059
2060 do {
2061 ret = alloc_retstack_tasklist(ret_stack_list);
2062 } while (ret == -EAGAIN);
2063
2064 kfree(ret_stack_list);
2065 return ret;
2066}
2067
4a2b8dda
FW
2068/*
2069 * Hibernation protection.
2070 * The state of the current task is too much unstable during
2071 * suspend/restore to disk. We want to protect against that.
2072 */
2073static int
2074ftrace_suspend_notifier_call(struct notifier_block *bl, unsigned long state,
2075 void *unused)
2076{
2077 switch (state) {
2078 case PM_HIBERNATION_PREPARE:
2079 pause_graph_tracing();
2080 break;
2081
2082 case PM_POST_HIBERNATION:
2083 unpause_graph_tracing();
2084 break;
2085 }
2086 return NOTIFY_DONE;
2087}
2088
287b6e68
FW
2089int register_ftrace_graph(trace_func_graph_ret_t retfunc,
2090 trace_func_graph_ent_t entryfunc)
15e6cb36 2091{
e7d3737e
FW
2092 int ret = 0;
2093
2094 mutex_lock(&ftrace_sysctl_lock);
2095
4a2b8dda
FW
2096 ftrace_suspend_notifier.notifier_call = ftrace_suspend_notifier_call;
2097 register_pm_notifier(&ftrace_suspend_notifier);
2098
287b6e68 2099 atomic_inc(&ftrace_graph_active);
fb52607a 2100 ret = start_graph_tracing();
f201ae23 2101 if (ret) {
287b6e68 2102 atomic_dec(&ftrace_graph_active);
f201ae23
FW
2103 goto out;
2104 }
e53a6319 2105
287b6e68
FW
2106 ftrace_graph_return = retfunc;
2107 ftrace_graph_entry = entryfunc;
e53a6319 2108
5a45cfe1 2109 ftrace_startup(FTRACE_START_FUNC_RET);
e7d3737e
FW
2110
2111out:
2112 mutex_unlock(&ftrace_sysctl_lock);
2113 return ret;
15e6cb36
FW
2114}
2115
fb52607a 2116void unregister_ftrace_graph(void)
15e6cb36 2117{
e7d3737e
FW
2118 mutex_lock(&ftrace_sysctl_lock);
2119
287b6e68
FW
2120 atomic_dec(&ftrace_graph_active);
2121 ftrace_graph_return = (trace_func_graph_ret_t)ftrace_stub;
e49dc19c 2122 ftrace_graph_entry = ftrace_graph_entry_stub;
5a45cfe1 2123 ftrace_shutdown(FTRACE_STOP_FUNC_RET);
4a2b8dda 2124 unregister_pm_notifier(&ftrace_suspend_notifier);
e7d3737e
FW
2125
2126 mutex_unlock(&ftrace_sysctl_lock);
15e6cb36 2127}
f201ae23
FW
2128
2129/* Allocate a return stack for newly created task */
fb52607a 2130void ftrace_graph_init_task(struct task_struct *t)
f201ae23 2131{
287b6e68 2132 if (atomic_read(&ftrace_graph_active)) {
f201ae23
FW
2133 t->ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
2134 * sizeof(struct ftrace_ret_stack),
2135 GFP_KERNEL);
2136 if (!t->ret_stack)
2137 return;
2138 t->curr_ret_stack = -1;
380c4b14 2139 atomic_set(&t->tracing_graph_pause, 0);
f201ae23
FW
2140 atomic_set(&t->trace_overrun, 0);
2141 } else
2142 t->ret_stack = NULL;
2143}
2144
fb52607a 2145void ftrace_graph_exit_task(struct task_struct *t)
f201ae23 2146{
eae849ca
FW
2147 struct ftrace_ret_stack *ret_stack = t->ret_stack;
2148
f201ae23 2149 t->ret_stack = NULL;
eae849ca
FW
2150 /* NULL must become visible to IRQs before we free it: */
2151 barrier();
2152
2153 kfree(ret_stack);
f201ae23 2154}
14a866c5
SR
2155
2156void ftrace_graph_stop(void)
2157{
2158 ftrace_stop();
2159}
15e6cb36
FW
2160#endif
2161