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