Merge branches 'tracing/ftrace', 'tracing/fastboot', 'tracing/nmisafe' and 'tracing...
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / kernel / trace / ftrace.c
CommitLineData
16444a8a
ACM
1/*
2 * Infrastructure for profiling code inserted by 'gcc -pg'.
3 *
4 * Copyright (C) 2007-2008 Steven Rostedt <srostedt@redhat.com>
5 * Copyright (C) 2004-2008 Ingo Molnar <mingo@redhat.com>
6 *
7 * Originally ported from the -rt patch by:
8 * Copyright (C) 2007 Arnaldo Carvalho de Melo <acme@redhat.com>
9 *
10 * Based on code in the latency_tracer, that is:
11 *
12 * Copyright (C) 2004-2006 Ingo Molnar
13 * Copyright (C) 2004 William Lee Irwin III
14 */
15
3d083395
SR
16#include <linux/stop_machine.h>
17#include <linux/clocksource.h>
18#include <linux/kallsyms.h>
5072c59f
SR
19#include <linux/seq_file.h>
20#include <linux/debugfs.h>
3d083395 21#include <linux/hardirq.h>
2d8b820b 22#include <linux/kthread.h>
5072c59f 23#include <linux/uaccess.h>
f22f9a89 24#include <linux/kprobes.h>
2d8b820b 25#include <linux/ftrace.h>
b0fc494f 26#include <linux/sysctl.h>
5072c59f 27#include <linux/ctype.h>
3d083395
SR
28#include <linux/list.h>
29
395a59d0
AS
30#include <asm/ftrace.h>
31
3d083395 32#include "trace.h"
16444a8a 33
6912896e
SR
34#define FTRACE_WARN_ON(cond) \
35 do { \
36 if (WARN_ON(cond)) \
37 ftrace_kill(); \
38 } while (0)
39
40#define FTRACE_WARN_ON_ONCE(cond) \
41 do { \
42 if (WARN_ON_ONCE(cond)) \
43 ftrace_kill(); \
44 } while (0)
45
4eebcc81
SR
46/* ftrace_enabled is a method to turn ftrace on or off */
47int ftrace_enabled __read_mostly;
d61f82d0 48static int last_ftrace_enabled;
b0fc494f 49
60a7ecf4
SR
50/* Quick disabling of function tracer. */
51int function_trace_stop;
52
4eebcc81
SR
53/*
54 * ftrace_disabled is set when an anomaly is discovered.
55 * ftrace_disabled is much stronger than ftrace_enabled.
56 */
57static int ftrace_disabled __read_mostly;
58
3d083395 59static DEFINE_SPINLOCK(ftrace_lock);
b0fc494f
SR
60static DEFINE_MUTEX(ftrace_sysctl_lock);
61
16444a8a
ACM
62static struct ftrace_ops ftrace_list_end __read_mostly =
63{
64 .func = ftrace_stub,
65};
66
67static struct ftrace_ops *ftrace_list __read_mostly = &ftrace_list_end;
68ftrace_func_t ftrace_trace_function __read_mostly = ftrace_stub;
60a7ecf4 69ftrace_func_t __ftrace_trace_function __read_mostly = ftrace_stub;
16444a8a 70
f2252935 71static void ftrace_list_func(unsigned long ip, unsigned long parent_ip)
16444a8a
ACM
72{
73 struct ftrace_ops *op = ftrace_list;
74
75 /* in case someone actually ports this to alpha! */
76 read_barrier_depends();
77
78 while (op != &ftrace_list_end) {
79 /* silly alpha */
80 read_barrier_depends();
81 op->func(ip, parent_ip);
82 op = op->next;
83 };
84}
85
86/**
3d083395 87 * clear_ftrace_function - reset the ftrace function
16444a8a 88 *
3d083395
SR
89 * This NULLs the ftrace function and in essence stops
90 * tracing. There may be lag
16444a8a 91 */
3d083395 92void clear_ftrace_function(void)
16444a8a 93{
3d083395 94 ftrace_trace_function = ftrace_stub;
60a7ecf4 95 __ftrace_trace_function = ftrace_stub;
3d083395
SR
96}
97
60a7ecf4
SR
98#ifndef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
99/*
100 * For those archs that do not test ftrace_trace_stop in their
101 * mcount call site, we need to do it from C.
102 */
103static void ftrace_test_stop_func(unsigned long ip, unsigned long parent_ip)
104{
105 if (function_trace_stop)
106 return;
107
108 __ftrace_trace_function(ip, parent_ip);
109}
110#endif
111
e309b41d 112static int __register_ftrace_function(struct ftrace_ops *ops)
3d083395 113{
99ecdc43 114 /* should not be called from interrupt context */
3d083395 115 spin_lock(&ftrace_lock);
16444a8a 116
16444a8a
ACM
117 ops->next = ftrace_list;
118 /*
119 * We are entering ops into the ftrace_list but another
120 * CPU might be walking that list. We need to make sure
121 * the ops->next pointer is valid before another CPU sees
122 * the ops pointer included into the ftrace_list.
123 */
124 smp_wmb();
125 ftrace_list = ops;
3d083395 126
b0fc494f
SR
127 if (ftrace_enabled) {
128 /*
129 * For one func, simply call it directly.
130 * For more than one func, call the chain.
131 */
60a7ecf4 132#ifdef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
b0fc494f
SR
133 if (ops->next == &ftrace_list_end)
134 ftrace_trace_function = ops->func;
135 else
136 ftrace_trace_function = ftrace_list_func;
60a7ecf4
SR
137#else
138 if (ops->next == &ftrace_list_end)
139 __ftrace_trace_function = ops->func;
140 else
141 __ftrace_trace_function = ftrace_list_func;
142 ftrace_trace_function = ftrace_test_stop_func;
143#endif
b0fc494f 144 }
3d083395
SR
145
146 spin_unlock(&ftrace_lock);
16444a8a
ACM
147
148 return 0;
149}
150
e309b41d 151static int __unregister_ftrace_function(struct ftrace_ops *ops)
16444a8a 152{
16444a8a
ACM
153 struct ftrace_ops **p;
154 int ret = 0;
155
99ecdc43 156 /* should not be called from interrupt context */
3d083395 157 spin_lock(&ftrace_lock);
16444a8a
ACM
158
159 /*
3d083395
SR
160 * If we are removing the last function, then simply point
161 * to the ftrace_stub.
16444a8a
ACM
162 */
163 if (ftrace_list == ops && ops->next == &ftrace_list_end) {
164 ftrace_trace_function = ftrace_stub;
165 ftrace_list = &ftrace_list_end;
166 goto out;
167 }
168
169 for (p = &ftrace_list; *p != &ftrace_list_end; p = &(*p)->next)
170 if (*p == ops)
171 break;
172
173 if (*p != ops) {
174 ret = -1;
175 goto out;
176 }
177
178 *p = (*p)->next;
179
b0fc494f
SR
180 if (ftrace_enabled) {
181 /* If we only have one func left, then call that directly */
182 if (ftrace_list == &ftrace_list_end ||
183 ftrace_list->next == &ftrace_list_end)
184 ftrace_trace_function = ftrace_list->func;
185 }
16444a8a
ACM
186
187 out:
3d083395
SR
188 spin_unlock(&ftrace_lock);
189
190 return ret;
191}
192
193#ifdef CONFIG_DYNAMIC_FTRACE
99ecdc43 194#ifndef CONFIG_FTRACE_MCOUNT_RECORD
cb7be3b2 195# error Dynamic ftrace depends on MCOUNT_RECORD
99ecdc43
SR
196#endif
197
71c67d58
SN
198/*
199 * Since MCOUNT_ADDR may point to mcount itself, we do not want
200 * to get it confused by reading a reference in the code as we
201 * are parsing on objcopy output of text. Use a variable for
202 * it instead.
203 */
204static unsigned long mcount_addr = MCOUNT_ADDR;
205
d61f82d0
SR
206enum {
207 FTRACE_ENABLE_CALLS = (1 << 0),
208 FTRACE_DISABLE_CALLS = (1 << 1),
209 FTRACE_UPDATE_TRACE_FUNC = (1 << 2),
210 FTRACE_ENABLE_MCOUNT = (1 << 3),
211 FTRACE_DISABLE_MCOUNT = (1 << 4),
212};
213
5072c59f 214static int ftrace_filtered;
ecea656d 215static int tracing_on;
5072c59f 216
08f5ac90 217static LIST_HEAD(ftrace_new_addrs);
3d083395 218
41c52c0d 219static DEFINE_MUTEX(ftrace_regex_lock);
3d083395 220
3c1720f0
SR
221struct ftrace_page {
222 struct ftrace_page *next;
aa5e5cea 223 unsigned long index;
3c1720f0 224 struct dyn_ftrace records[];
aa5e5cea 225};
3c1720f0
SR
226
227#define ENTRIES_PER_PAGE \
228 ((PAGE_SIZE - sizeof(struct ftrace_page)) / sizeof(struct dyn_ftrace))
229
230/* estimate from running different kernels */
231#define NR_TO_INIT 10000
232
233static struct ftrace_page *ftrace_pages_start;
234static struct ftrace_page *ftrace_pages;
235
37ad5084
SR
236static struct dyn_ftrace *ftrace_free_records;
237
ecea656d
AS
238
239#ifdef CONFIG_KPROBES
f17845e5
IM
240
241static int frozen_record_count;
242
ecea656d
AS
243static inline void freeze_record(struct dyn_ftrace *rec)
244{
245 if (!(rec->flags & FTRACE_FL_FROZEN)) {
246 rec->flags |= FTRACE_FL_FROZEN;
247 frozen_record_count++;
248 }
249}
250
251static inline void unfreeze_record(struct dyn_ftrace *rec)
252{
253 if (rec->flags & FTRACE_FL_FROZEN) {
254 rec->flags &= ~FTRACE_FL_FROZEN;
255 frozen_record_count--;
256 }
257}
258
259static inline int record_frozen(struct dyn_ftrace *rec)
260{
261 return rec->flags & FTRACE_FL_FROZEN;
262}
263#else
264# define freeze_record(rec) ({ 0; })
265# define unfreeze_record(rec) ({ 0; })
266# define record_frozen(rec) ({ 0; })
267#endif /* CONFIG_KPROBES */
268
e309b41d 269static void ftrace_free_rec(struct dyn_ftrace *rec)
37ad5084 270{
37ad5084
SR
271 rec->ip = (unsigned long)ftrace_free_records;
272 ftrace_free_records = rec;
273 rec->flags |= FTRACE_FL_FREE;
274}
275
fed1939c
SR
276void ftrace_release(void *start, unsigned long size)
277{
278 struct dyn_ftrace *rec;
279 struct ftrace_page *pg;
280 unsigned long s = (unsigned long)start;
281 unsigned long e = s + size;
282 int i;
283
00fd61ae 284 if (ftrace_disabled || !start)
fed1939c
SR
285 return;
286
99ecdc43 287 /* should not be called from interrupt context */
fed1939c
SR
288 spin_lock(&ftrace_lock);
289
290 for (pg = ftrace_pages_start; pg; pg = pg->next) {
291 for (i = 0; i < pg->index; i++) {
292 rec = &pg->records[i];
293
294 if ((rec->ip >= s) && (rec->ip < e))
295 ftrace_free_rec(rec);
296 }
297 }
298 spin_unlock(&ftrace_lock);
fed1939c
SR
299}
300
e309b41d 301static struct dyn_ftrace *ftrace_alloc_dyn_node(unsigned long ip)
3c1720f0 302{
37ad5084
SR
303 struct dyn_ftrace *rec;
304
305 /* First check for freed records */
306 if (ftrace_free_records) {
307 rec = ftrace_free_records;
308
37ad5084 309 if (unlikely(!(rec->flags & FTRACE_FL_FREE))) {
6912896e 310 FTRACE_WARN_ON_ONCE(1);
37ad5084
SR
311 ftrace_free_records = NULL;
312 return NULL;
313 }
314
315 ftrace_free_records = (void *)rec->ip;
316 memset(rec, 0, sizeof(*rec));
317 return rec;
318 }
319
3c1720f0 320 if (ftrace_pages->index == ENTRIES_PER_PAGE) {
08f5ac90
SR
321 if (!ftrace_pages->next) {
322 /* allocate another page */
323 ftrace_pages->next =
324 (void *)get_zeroed_page(GFP_KERNEL);
325 if (!ftrace_pages->next)
326 return NULL;
327 }
3c1720f0
SR
328 ftrace_pages = ftrace_pages->next;
329 }
330
331 return &ftrace_pages->records[ftrace_pages->index++];
332}
333
08f5ac90 334static struct dyn_ftrace *
d61f82d0 335ftrace_record_ip(unsigned long ip)
3d083395 336{
08f5ac90 337 struct dyn_ftrace *rec;
3d083395 338
4eebcc81 339 if (!ftrace_enabled || ftrace_disabled)
08f5ac90 340 return NULL;
3d083395 341
08f5ac90
SR
342 rec = ftrace_alloc_dyn_node(ip);
343 if (!rec)
344 return NULL;
3d083395 345
08f5ac90 346 rec->ip = ip;
3d083395 347
08f5ac90 348 list_add(&rec->list, &ftrace_new_addrs);
3d083395 349
08f5ac90 350 return rec;
3d083395
SR
351}
352
caf8cdeb 353#define FTRACE_ADDR ((long)(ftrace_caller))
3c1720f0 354
0eb96701 355static int
5072c59f
SR
356__ftrace_replace_code(struct dyn_ftrace *rec,
357 unsigned char *old, unsigned char *new, int enable)
358{
41c52c0d 359 unsigned long ip, fl;
5072c59f
SR
360
361 ip = rec->ip;
362
363 if (ftrace_filtered && enable) {
5072c59f
SR
364 /*
365 * If filtering is on:
366 *
367 * If this record is set to be filtered and
368 * is enabled then do nothing.
369 *
370 * If this record is set to be filtered and
371 * it is not enabled, enable it.
372 *
373 * If this record is not set to be filtered
374 * and it is not enabled do nothing.
375 *
41c52c0d
SR
376 * If this record is set not to trace then
377 * do nothing.
378 *
a4500b84
AS
379 * If this record is set not to trace and
380 * it is enabled then disable it.
381 *
5072c59f
SR
382 * If this record is not set to be filtered and
383 * it is enabled, disable it.
384 */
a4500b84
AS
385
386 fl = rec->flags & (FTRACE_FL_FILTER | FTRACE_FL_NOTRACE |
387 FTRACE_FL_ENABLED);
5072c59f
SR
388
389 if ((fl == (FTRACE_FL_FILTER | FTRACE_FL_ENABLED)) ||
a4500b84
AS
390 (fl == (FTRACE_FL_FILTER | FTRACE_FL_NOTRACE)) ||
391 !fl || (fl == FTRACE_FL_NOTRACE))
0eb96701 392 return 0;
5072c59f
SR
393
394 /*
395 * If it is enabled disable it,
396 * otherwise enable it!
397 */
a4500b84 398 if (fl & FTRACE_FL_ENABLED) {
5072c59f
SR
399 /* swap new and old */
400 new = old;
401 old = ftrace_call_replace(ip, FTRACE_ADDR);
402 rec->flags &= ~FTRACE_FL_ENABLED;
403 } else {
404 new = ftrace_call_replace(ip, FTRACE_ADDR);
405 rec->flags |= FTRACE_FL_ENABLED;
406 }
407 } else {
408
41c52c0d
SR
409 if (enable) {
410 /*
411 * If this record is set not to trace and is
412 * not enabled, do nothing.
413 */
414 fl = rec->flags & (FTRACE_FL_NOTRACE | FTRACE_FL_ENABLED);
415 if (fl == FTRACE_FL_NOTRACE)
0eb96701 416 return 0;
41c52c0d 417
5072c59f 418 new = ftrace_call_replace(ip, FTRACE_ADDR);
41c52c0d 419 } else
5072c59f
SR
420 old = ftrace_call_replace(ip, FTRACE_ADDR);
421
422 if (enable) {
423 if (rec->flags & FTRACE_FL_ENABLED)
0eb96701 424 return 0;
5072c59f
SR
425 rec->flags |= FTRACE_FL_ENABLED;
426 } else {
427 if (!(rec->flags & FTRACE_FL_ENABLED))
0eb96701 428 return 0;
5072c59f
SR
429 rec->flags &= ~FTRACE_FL_ENABLED;
430 }
431 }
432
0eb96701 433 return ftrace_modify_code(ip, old, new);
5072c59f
SR
434}
435
e309b41d 436static void ftrace_replace_code(int enable)
3c1720f0 437{
0eb96701 438 int i, failed;
3c1720f0
SR
439 unsigned char *new = NULL, *old = NULL;
440 struct dyn_ftrace *rec;
441 struct ftrace_page *pg;
3c1720f0 442
5072c59f 443 if (enable)
3c1720f0
SR
444 old = ftrace_nop_replace();
445 else
446 new = ftrace_nop_replace();
447
448 for (pg = ftrace_pages_start; pg; pg = pg->next) {
449 for (i = 0; i < pg->index; i++) {
450 rec = &pg->records[i];
451
452 /* don't modify code that has already faulted */
453 if (rec->flags & FTRACE_FL_FAILED)
454 continue;
455
f22f9a89 456 /* ignore updates to this record's mcount site */
98a05ed4
AS
457 if (get_kprobe((void *)rec->ip)) {
458 freeze_record(rec);
f22f9a89 459 continue;
98a05ed4
AS
460 } else {
461 unfreeze_record(rec);
462 }
f22f9a89 463
0eb96701
AS
464 failed = __ftrace_replace_code(rec, old, new, enable);
465 if (failed && (rec->flags & FTRACE_FL_CONVERTED)) {
466 rec->flags |= FTRACE_FL_FAILED;
467 if ((system_state == SYSTEM_BOOTING) ||
34078a5e 468 !core_kernel_text(rec->ip)) {
0eb96701
AS
469 ftrace_free_rec(rec);
470 }
471 }
3c1720f0
SR
472 }
473 }
474}
475
05736a42
SR
476static void print_ip_ins(const char *fmt, unsigned char *p)
477{
478 int i;
479
480 printk(KERN_CONT "%s", fmt);
481
482 for (i = 0; i < MCOUNT_INSN_SIZE; i++)
483 printk(KERN_CONT "%s%02x", i ? ":" : "", p[i]);
484}
485
492a7ea5 486static int
d61f82d0 487ftrace_code_disable(struct dyn_ftrace *rec)
3c1720f0
SR
488{
489 unsigned long ip;
490 unsigned char *nop, *call;
593eb8a2 491 int ret;
3c1720f0
SR
492
493 ip = rec->ip;
494
495 nop = ftrace_nop_replace();
3b47bfc1 496 call = ftrace_call_replace(ip, mcount_addr);
3c1720f0 497
593eb8a2
SR
498 ret = ftrace_modify_code(ip, call, nop);
499 if (ret) {
500 switch (ret) {
501 case -EFAULT:
6912896e 502 FTRACE_WARN_ON_ONCE(1);
05736a42
SR
503 pr_info("ftrace faulted on modifying ");
504 print_ip_sym(ip);
505 break;
593eb8a2 506 case -EINVAL:
6912896e 507 FTRACE_WARN_ON_ONCE(1);
05736a42
SR
508 pr_info("ftrace failed to modify ");
509 print_ip_sym(ip);
510 print_ip_ins(" expected: ", call);
511 print_ip_ins(" actual: ", (unsigned char *)ip);
512 print_ip_ins(" replace: ", nop);
513 printk(KERN_CONT "\n");
514 break;
593eb8a2 515 case -EPERM:
6912896e 516 FTRACE_WARN_ON_ONCE(1);
593eb8a2
SR
517 pr_info("ftrace faulted on writing ");
518 print_ip_sym(ip);
519 break;
520 default:
6912896e 521 FTRACE_WARN_ON_ONCE(1);
593eb8a2
SR
522 pr_info("ftrace faulted on unknown error ");
523 print_ip_sym(ip);
05736a42
SR
524 }
525
3c1720f0 526 rec->flags |= FTRACE_FL_FAILED;
492a7ea5 527 return 0;
37ad5084 528 }
492a7ea5 529 return 1;
3c1720f0
SR
530}
531
e309b41d 532static int __ftrace_modify_code(void *data)
3d083395 533{
d61f82d0
SR
534 int *command = data;
535
ad90c0e3 536 if (*command & FTRACE_ENABLE_CALLS) {
d61f82d0 537 ftrace_replace_code(1);
ecea656d
AS
538 tracing_on = 1;
539 } else if (*command & FTRACE_DISABLE_CALLS) {
d61f82d0 540 ftrace_replace_code(0);
ecea656d
AS
541 tracing_on = 0;
542 }
d61f82d0
SR
543
544 if (*command & FTRACE_UPDATE_TRACE_FUNC)
545 ftrace_update_ftrace_func(ftrace_trace_function);
546
d61f82d0 547 return 0;
3d083395
SR
548}
549
e309b41d 550static void ftrace_run_update_code(int command)
3d083395 551{
784e2d76 552 stop_machine(__ftrace_modify_code, &command, NULL);
3d083395
SR
553}
554
d61f82d0 555static ftrace_func_t saved_ftrace_func;
60a7ecf4 556static int ftrace_start_up;
cb7be3b2 557static DEFINE_MUTEX(ftrace_start_lock);
d61f82d0 558
e309b41d 559static void ftrace_startup(void)
3d083395 560{
d61f82d0
SR
561 int command = 0;
562
4eebcc81
SR
563 if (unlikely(ftrace_disabled))
564 return;
565
cb7be3b2 566 mutex_lock(&ftrace_start_lock);
60a7ecf4
SR
567 ftrace_start_up++;
568 if (ftrace_start_up == 1)
d61f82d0
SR
569 command |= FTRACE_ENABLE_CALLS;
570
571 if (saved_ftrace_func != ftrace_trace_function) {
572 saved_ftrace_func = ftrace_trace_function;
573 command |= FTRACE_UPDATE_TRACE_FUNC;
574 }
575
576 if (!command || !ftrace_enabled)
3d083395 577 goto out;
3d083395 578
d61f82d0 579 ftrace_run_update_code(command);
3d083395 580 out:
cb7be3b2 581 mutex_unlock(&ftrace_start_lock);
3d083395
SR
582}
583
e309b41d 584static void ftrace_shutdown(void)
3d083395 585{
d61f82d0
SR
586 int command = 0;
587
4eebcc81
SR
588 if (unlikely(ftrace_disabled))
589 return;
590
cb7be3b2 591 mutex_lock(&ftrace_start_lock);
60a7ecf4
SR
592 ftrace_start_up--;
593 if (!ftrace_start_up)
d61f82d0 594 command |= FTRACE_DISABLE_CALLS;
3d083395 595
d61f82d0
SR
596 if (saved_ftrace_func != ftrace_trace_function) {
597 saved_ftrace_func = ftrace_trace_function;
598 command |= FTRACE_UPDATE_TRACE_FUNC;
599 }
3d083395 600
d61f82d0
SR
601 if (!command || !ftrace_enabled)
602 goto out;
603
604 ftrace_run_update_code(command);
3d083395 605 out:
cb7be3b2 606 mutex_unlock(&ftrace_start_lock);
3d083395
SR
607}
608
e309b41d 609static void ftrace_startup_sysctl(void)
b0fc494f 610{
d61f82d0
SR
611 int command = FTRACE_ENABLE_MCOUNT;
612
4eebcc81
SR
613 if (unlikely(ftrace_disabled))
614 return;
615
cb7be3b2 616 mutex_lock(&ftrace_start_lock);
d61f82d0
SR
617 /* Force update next time */
618 saved_ftrace_func = NULL;
60a7ecf4
SR
619 /* ftrace_start_up is true if we want ftrace running */
620 if (ftrace_start_up)
d61f82d0
SR
621 command |= FTRACE_ENABLE_CALLS;
622
623 ftrace_run_update_code(command);
cb7be3b2 624 mutex_unlock(&ftrace_start_lock);
b0fc494f
SR
625}
626
e309b41d 627static void ftrace_shutdown_sysctl(void)
b0fc494f 628{
d61f82d0
SR
629 int command = FTRACE_DISABLE_MCOUNT;
630
4eebcc81
SR
631 if (unlikely(ftrace_disabled))
632 return;
633
cb7be3b2 634 mutex_lock(&ftrace_start_lock);
60a7ecf4
SR
635 /* ftrace_start_up is true if ftrace is running */
636 if (ftrace_start_up)
d61f82d0
SR
637 command |= FTRACE_DISABLE_CALLS;
638
639 ftrace_run_update_code(command);
cb7be3b2 640 mutex_unlock(&ftrace_start_lock);
b0fc494f
SR
641}
642
3d083395
SR
643static cycle_t ftrace_update_time;
644static unsigned long ftrace_update_cnt;
645unsigned long ftrace_update_tot_cnt;
646
08f5ac90 647static int ftrace_update_code(void)
3d083395 648{
08f5ac90 649 struct dyn_ftrace *p, *t;
f22f9a89 650 cycle_t start, stop;
3d083395 651
750ed1a4 652 start = ftrace_now(raw_smp_processor_id());
3d083395
SR
653 ftrace_update_cnt = 0;
654
08f5ac90 655 list_for_each_entry_safe(p, t, &ftrace_new_addrs, list) {
3d083395 656
08f5ac90
SR
657 /* If something went wrong, bail without enabling anything */
658 if (unlikely(ftrace_disabled))
659 return -1;
f22f9a89 660
08f5ac90 661 list_del_init(&p->list);
f22f9a89 662
08f5ac90
SR
663 /* convert record (i.e, patch mcount-call with NOP) */
664 if (ftrace_code_disable(p)) {
665 p->flags |= FTRACE_FL_CONVERTED;
666 ftrace_update_cnt++;
667 } else
668 ftrace_free_rec(p);
3d083395
SR
669 }
670
750ed1a4 671 stop = ftrace_now(raw_smp_processor_id());
3d083395
SR
672 ftrace_update_time = stop - start;
673 ftrace_update_tot_cnt += ftrace_update_cnt;
674
16444a8a
ACM
675 return 0;
676}
677
68bf21aa 678static int __init ftrace_dyn_table_alloc(unsigned long num_to_init)
3c1720f0
SR
679{
680 struct ftrace_page *pg;
681 int cnt;
682 int i;
3c1720f0
SR
683
684 /* allocate a few pages */
685 ftrace_pages_start = (void *)get_zeroed_page(GFP_KERNEL);
686 if (!ftrace_pages_start)
687 return -1;
688
689 /*
690 * Allocate a few more pages.
691 *
692 * TODO: have some parser search vmlinux before
693 * final linking to find all calls to ftrace.
694 * Then we can:
695 * a) know how many pages to allocate.
696 * and/or
697 * b) set up the table then.
698 *
699 * The dynamic code is still necessary for
700 * modules.
701 */
702
703 pg = ftrace_pages = ftrace_pages_start;
704
68bf21aa 705 cnt = num_to_init / ENTRIES_PER_PAGE;
08f5ac90 706 pr_info("ftrace: allocating %ld entries in %d pages\n",
68bf21aa 707 num_to_init, cnt);
3c1720f0
SR
708
709 for (i = 0; i < cnt; i++) {
710 pg->next = (void *)get_zeroed_page(GFP_KERNEL);
711
712 /* If we fail, we'll try later anyway */
713 if (!pg->next)
714 break;
715
716 pg = pg->next;
717 }
718
719 return 0;
720}
721
5072c59f
SR
722enum {
723 FTRACE_ITER_FILTER = (1 << 0),
724 FTRACE_ITER_CONT = (1 << 1),
41c52c0d 725 FTRACE_ITER_NOTRACE = (1 << 2),
eb9a7bf0 726 FTRACE_ITER_FAILURES = (1 << 3),
5072c59f
SR
727};
728
729#define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
730
731struct ftrace_iterator {
732 loff_t pos;
733 struct ftrace_page *pg;
734 unsigned idx;
735 unsigned flags;
736 unsigned char buffer[FTRACE_BUFF_MAX+1];
737 unsigned buffer_idx;
738 unsigned filtered;
739};
740
e309b41d 741static void *
5072c59f
SR
742t_next(struct seq_file *m, void *v, loff_t *pos)
743{
744 struct ftrace_iterator *iter = m->private;
745 struct dyn_ftrace *rec = NULL;
746
747 (*pos)++;
748
99ecdc43
SR
749 /* should not be called from interrupt context */
750 spin_lock(&ftrace_lock);
5072c59f
SR
751 retry:
752 if (iter->idx >= iter->pg->index) {
753 if (iter->pg->next) {
754 iter->pg = iter->pg->next;
755 iter->idx = 0;
756 goto retry;
757 }
758 } else {
759 rec = &iter->pg->records[iter->idx++];
a9fdda33
SR
760 if ((rec->flags & FTRACE_FL_FREE) ||
761
762 (!(iter->flags & FTRACE_ITER_FAILURES) &&
eb9a7bf0
AS
763 (rec->flags & FTRACE_FL_FAILED)) ||
764
765 ((iter->flags & FTRACE_ITER_FAILURES) &&
a9fdda33 766 !(rec->flags & FTRACE_FL_FAILED)) ||
eb9a7bf0 767
41c52c0d
SR
768 ((iter->flags & FTRACE_ITER_NOTRACE) &&
769 !(rec->flags & FTRACE_FL_NOTRACE))) {
5072c59f
SR
770 rec = NULL;
771 goto retry;
772 }
773 }
99ecdc43 774 spin_unlock(&ftrace_lock);
5072c59f
SR
775
776 iter->pos = *pos;
777
778 return rec;
779}
780
781static void *t_start(struct seq_file *m, loff_t *pos)
782{
783 struct ftrace_iterator *iter = m->private;
784 void *p = NULL;
785 loff_t l = -1;
786
787 if (*pos != iter->pos) {
788 for (p = t_next(m, p, &l); p && l < *pos; p = t_next(m, p, &l))
789 ;
790 } else {
791 l = *pos;
792 p = t_next(m, p, &l);
793 }
794
795 return p;
796}
797
798static void t_stop(struct seq_file *m, void *p)
799{
800}
801
802static int t_show(struct seq_file *m, void *v)
803{
804 struct dyn_ftrace *rec = v;
805 char str[KSYM_SYMBOL_LEN];
806
807 if (!rec)
808 return 0;
809
810 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
811
812 seq_printf(m, "%s\n", str);
813
814 return 0;
815}
816
817static struct seq_operations show_ftrace_seq_ops = {
818 .start = t_start,
819 .next = t_next,
820 .stop = t_stop,
821 .show = t_show,
822};
823
e309b41d 824static int
5072c59f
SR
825ftrace_avail_open(struct inode *inode, struct file *file)
826{
827 struct ftrace_iterator *iter;
828 int ret;
829
4eebcc81
SR
830 if (unlikely(ftrace_disabled))
831 return -ENODEV;
832
5072c59f
SR
833 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
834 if (!iter)
835 return -ENOMEM;
836
837 iter->pg = ftrace_pages_start;
838 iter->pos = -1;
839
840 ret = seq_open(file, &show_ftrace_seq_ops);
841 if (!ret) {
842 struct seq_file *m = file->private_data;
4bf39a94 843
5072c59f 844 m->private = iter;
4bf39a94 845 } else {
5072c59f 846 kfree(iter);
4bf39a94 847 }
5072c59f
SR
848
849 return ret;
850}
851
852int ftrace_avail_release(struct inode *inode, struct file *file)
853{
854 struct seq_file *m = (struct seq_file *)file->private_data;
855 struct ftrace_iterator *iter = m->private;
856
857 seq_release(inode, file);
858 kfree(iter);
4bf39a94 859
5072c59f
SR
860 return 0;
861}
862
eb9a7bf0
AS
863static int
864ftrace_failures_open(struct inode *inode, struct file *file)
865{
866 int ret;
867 struct seq_file *m;
868 struct ftrace_iterator *iter;
869
870 ret = ftrace_avail_open(inode, file);
871 if (!ret) {
872 m = (struct seq_file *)file->private_data;
873 iter = (struct ftrace_iterator *)m->private;
874 iter->flags = FTRACE_ITER_FAILURES;
875 }
876
877 return ret;
878}
879
880
41c52c0d 881static void ftrace_filter_reset(int enable)
5072c59f
SR
882{
883 struct ftrace_page *pg;
884 struct dyn_ftrace *rec;
41c52c0d 885 unsigned long type = enable ? FTRACE_FL_FILTER : FTRACE_FL_NOTRACE;
5072c59f
SR
886 unsigned i;
887
99ecdc43
SR
888 /* should not be called from interrupt context */
889 spin_lock(&ftrace_lock);
41c52c0d
SR
890 if (enable)
891 ftrace_filtered = 0;
5072c59f
SR
892 pg = ftrace_pages_start;
893 while (pg) {
894 for (i = 0; i < pg->index; i++) {
895 rec = &pg->records[i];
896 if (rec->flags & FTRACE_FL_FAILED)
897 continue;
41c52c0d 898 rec->flags &= ~type;
5072c59f
SR
899 }
900 pg = pg->next;
901 }
99ecdc43 902 spin_unlock(&ftrace_lock);
5072c59f
SR
903}
904
e309b41d 905static int
41c52c0d 906ftrace_regex_open(struct inode *inode, struct file *file, int enable)
5072c59f
SR
907{
908 struct ftrace_iterator *iter;
909 int ret = 0;
910
4eebcc81
SR
911 if (unlikely(ftrace_disabled))
912 return -ENODEV;
913
5072c59f
SR
914 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
915 if (!iter)
916 return -ENOMEM;
917
41c52c0d 918 mutex_lock(&ftrace_regex_lock);
5072c59f
SR
919 if ((file->f_mode & FMODE_WRITE) &&
920 !(file->f_flags & O_APPEND))
41c52c0d 921 ftrace_filter_reset(enable);
5072c59f
SR
922
923 if (file->f_mode & FMODE_READ) {
924 iter->pg = ftrace_pages_start;
925 iter->pos = -1;
41c52c0d
SR
926 iter->flags = enable ? FTRACE_ITER_FILTER :
927 FTRACE_ITER_NOTRACE;
5072c59f
SR
928
929 ret = seq_open(file, &show_ftrace_seq_ops);
930 if (!ret) {
931 struct seq_file *m = file->private_data;
932 m->private = iter;
933 } else
934 kfree(iter);
935 } else
936 file->private_data = iter;
41c52c0d 937 mutex_unlock(&ftrace_regex_lock);
5072c59f
SR
938
939 return ret;
940}
941
41c52c0d
SR
942static int
943ftrace_filter_open(struct inode *inode, struct file *file)
944{
945 return ftrace_regex_open(inode, file, 1);
946}
947
948static int
949ftrace_notrace_open(struct inode *inode, struct file *file)
950{
951 return ftrace_regex_open(inode, file, 0);
952}
953
e309b41d 954static ssize_t
41c52c0d 955ftrace_regex_read(struct file *file, char __user *ubuf,
5072c59f
SR
956 size_t cnt, loff_t *ppos)
957{
958 if (file->f_mode & FMODE_READ)
959 return seq_read(file, ubuf, cnt, ppos);
960 else
961 return -EPERM;
962}
963
e309b41d 964static loff_t
41c52c0d 965ftrace_regex_lseek(struct file *file, loff_t offset, int origin)
5072c59f
SR
966{
967 loff_t ret;
968
969 if (file->f_mode & FMODE_READ)
970 ret = seq_lseek(file, offset, origin);
971 else
972 file->f_pos = ret = 1;
973
974 return ret;
975}
976
977enum {
978 MATCH_FULL,
979 MATCH_FRONT_ONLY,
980 MATCH_MIDDLE_ONLY,
981 MATCH_END_ONLY,
982};
983
e309b41d 984static void
41c52c0d 985ftrace_match(unsigned char *buff, int len, int enable)
5072c59f
SR
986{
987 char str[KSYM_SYMBOL_LEN];
988 char *search = NULL;
989 struct ftrace_page *pg;
990 struct dyn_ftrace *rec;
991 int type = MATCH_FULL;
41c52c0d 992 unsigned long flag = enable ? FTRACE_FL_FILTER : FTRACE_FL_NOTRACE;
5072c59f
SR
993 unsigned i, match = 0, search_len = 0;
994
995 for (i = 0; i < len; i++) {
996 if (buff[i] == '*') {
997 if (!i) {
998 search = buff + i + 1;
999 type = MATCH_END_ONLY;
1000 search_len = len - (i + 1);
1001 } else {
1002 if (type == MATCH_END_ONLY) {
1003 type = MATCH_MIDDLE_ONLY;
1004 } else {
1005 match = i;
1006 type = MATCH_FRONT_ONLY;
1007 }
1008 buff[i] = 0;
1009 break;
1010 }
1011 }
1012 }
1013
99ecdc43
SR
1014 /* should not be called from interrupt context */
1015 spin_lock(&ftrace_lock);
41c52c0d
SR
1016 if (enable)
1017 ftrace_filtered = 1;
5072c59f
SR
1018 pg = ftrace_pages_start;
1019 while (pg) {
1020 for (i = 0; i < pg->index; i++) {
1021 int matched = 0;
1022 char *ptr;
1023
1024 rec = &pg->records[i];
1025 if (rec->flags & FTRACE_FL_FAILED)
1026 continue;
1027 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
1028 switch (type) {
1029 case MATCH_FULL:
1030 if (strcmp(str, buff) == 0)
1031 matched = 1;
1032 break;
1033 case MATCH_FRONT_ONLY:
1034 if (memcmp(str, buff, match) == 0)
1035 matched = 1;
1036 break;
1037 case MATCH_MIDDLE_ONLY:
1038 if (strstr(str, search))
1039 matched = 1;
1040 break;
1041 case MATCH_END_ONLY:
1042 ptr = strstr(str, search);
1043 if (ptr && (ptr[search_len] == 0))
1044 matched = 1;
1045 break;
1046 }
1047 if (matched)
41c52c0d 1048 rec->flags |= flag;
5072c59f
SR
1049 }
1050 pg = pg->next;
1051 }
99ecdc43 1052 spin_unlock(&ftrace_lock);
5072c59f
SR
1053}
1054
e309b41d 1055static ssize_t
41c52c0d
SR
1056ftrace_regex_write(struct file *file, const char __user *ubuf,
1057 size_t cnt, loff_t *ppos, int enable)
5072c59f
SR
1058{
1059 struct ftrace_iterator *iter;
1060 char ch;
1061 size_t read = 0;
1062 ssize_t ret;
1063
1064 if (!cnt || cnt < 0)
1065 return 0;
1066
41c52c0d 1067 mutex_lock(&ftrace_regex_lock);
5072c59f
SR
1068
1069 if (file->f_mode & FMODE_READ) {
1070 struct seq_file *m = file->private_data;
1071 iter = m->private;
1072 } else
1073 iter = file->private_data;
1074
1075 if (!*ppos) {
1076 iter->flags &= ~FTRACE_ITER_CONT;
1077 iter->buffer_idx = 0;
1078 }
1079
1080 ret = get_user(ch, ubuf++);
1081 if (ret)
1082 goto out;
1083 read++;
1084 cnt--;
1085
1086 if (!(iter->flags & ~FTRACE_ITER_CONT)) {
1087 /* skip white space */
1088 while (cnt && isspace(ch)) {
1089 ret = get_user(ch, ubuf++);
1090 if (ret)
1091 goto out;
1092 read++;
1093 cnt--;
1094 }
1095
5072c59f
SR
1096 if (isspace(ch)) {
1097 file->f_pos += read;
1098 ret = read;
1099 goto out;
1100 }
1101
1102 iter->buffer_idx = 0;
1103 }
1104
1105 while (cnt && !isspace(ch)) {
1106 if (iter->buffer_idx < FTRACE_BUFF_MAX)
1107 iter->buffer[iter->buffer_idx++] = ch;
1108 else {
1109 ret = -EINVAL;
1110 goto out;
1111 }
1112 ret = get_user(ch, ubuf++);
1113 if (ret)
1114 goto out;
1115 read++;
1116 cnt--;
1117 }
1118
1119 if (isspace(ch)) {
1120 iter->filtered++;
1121 iter->buffer[iter->buffer_idx] = 0;
41c52c0d 1122 ftrace_match(iter->buffer, iter->buffer_idx, enable);
5072c59f
SR
1123 iter->buffer_idx = 0;
1124 } else
1125 iter->flags |= FTRACE_ITER_CONT;
1126
1127
1128 file->f_pos += read;
1129
1130 ret = read;
1131 out:
41c52c0d 1132 mutex_unlock(&ftrace_regex_lock);
5072c59f
SR
1133
1134 return ret;
1135}
1136
41c52c0d
SR
1137static ssize_t
1138ftrace_filter_write(struct file *file, const char __user *ubuf,
1139 size_t cnt, loff_t *ppos)
1140{
1141 return ftrace_regex_write(file, ubuf, cnt, ppos, 1);
1142}
1143
1144static ssize_t
1145ftrace_notrace_write(struct file *file, const char __user *ubuf,
1146 size_t cnt, loff_t *ppos)
1147{
1148 return ftrace_regex_write(file, ubuf, cnt, ppos, 0);
1149}
1150
1151static void
1152ftrace_set_regex(unsigned char *buf, int len, int reset, int enable)
1153{
1154 if (unlikely(ftrace_disabled))
1155 return;
1156
1157 mutex_lock(&ftrace_regex_lock);
1158 if (reset)
1159 ftrace_filter_reset(enable);
1160 if (buf)
1161 ftrace_match(buf, len, enable);
1162 mutex_unlock(&ftrace_regex_lock);
1163}
1164
77a2b37d
SR
1165/**
1166 * ftrace_set_filter - set a function to filter on in ftrace
1167 * @buf - the string that holds the function filter text.
1168 * @len - the length of the string.
1169 * @reset - non zero to reset all filters before applying this filter.
1170 *
1171 * Filters denote which functions should be enabled when tracing is enabled.
1172 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
1173 */
e309b41d 1174void ftrace_set_filter(unsigned char *buf, int len, int reset)
77a2b37d 1175{
41c52c0d
SR
1176 ftrace_set_regex(buf, len, reset, 1);
1177}
4eebcc81 1178
41c52c0d
SR
1179/**
1180 * ftrace_set_notrace - set a function to not trace in ftrace
1181 * @buf - the string that holds the function notrace text.
1182 * @len - the length of the string.
1183 * @reset - non zero to reset all filters before applying this filter.
1184 *
1185 * Notrace Filters denote which functions should not be enabled when tracing
1186 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
1187 * for tracing.
1188 */
1189void ftrace_set_notrace(unsigned char *buf, int len, int reset)
1190{
1191 ftrace_set_regex(buf, len, reset, 0);
77a2b37d
SR
1192}
1193
e309b41d 1194static int
41c52c0d 1195ftrace_regex_release(struct inode *inode, struct file *file, int enable)
5072c59f
SR
1196{
1197 struct seq_file *m = (struct seq_file *)file->private_data;
1198 struct ftrace_iterator *iter;
1199
41c52c0d 1200 mutex_lock(&ftrace_regex_lock);
5072c59f
SR
1201 if (file->f_mode & FMODE_READ) {
1202 iter = m->private;
1203
1204 seq_release(inode, file);
1205 } else
1206 iter = file->private_data;
1207
1208 if (iter->buffer_idx) {
1209 iter->filtered++;
1210 iter->buffer[iter->buffer_idx] = 0;
41c52c0d 1211 ftrace_match(iter->buffer, iter->buffer_idx, enable);
5072c59f
SR
1212 }
1213
1214 mutex_lock(&ftrace_sysctl_lock);
cb7be3b2 1215 mutex_lock(&ftrace_start_lock);
60a7ecf4 1216 if (iter->filtered && ftrace_start_up && ftrace_enabled)
5072c59f 1217 ftrace_run_update_code(FTRACE_ENABLE_CALLS);
cb7be3b2 1218 mutex_unlock(&ftrace_start_lock);
5072c59f
SR
1219 mutex_unlock(&ftrace_sysctl_lock);
1220
1221 kfree(iter);
41c52c0d 1222 mutex_unlock(&ftrace_regex_lock);
5072c59f
SR
1223 return 0;
1224}
1225
41c52c0d
SR
1226static int
1227ftrace_filter_release(struct inode *inode, struct file *file)
1228{
1229 return ftrace_regex_release(inode, file, 1);
1230}
1231
1232static int
1233ftrace_notrace_release(struct inode *inode, struct file *file)
1234{
1235 return ftrace_regex_release(inode, file, 0);
1236}
1237
5072c59f
SR
1238static struct file_operations ftrace_avail_fops = {
1239 .open = ftrace_avail_open,
1240 .read = seq_read,
1241 .llseek = seq_lseek,
1242 .release = ftrace_avail_release,
1243};
1244
eb9a7bf0
AS
1245static struct file_operations ftrace_failures_fops = {
1246 .open = ftrace_failures_open,
1247 .read = seq_read,
1248 .llseek = seq_lseek,
1249 .release = ftrace_avail_release,
1250};
1251
5072c59f
SR
1252static struct file_operations ftrace_filter_fops = {
1253 .open = ftrace_filter_open,
41c52c0d 1254 .read = ftrace_regex_read,
5072c59f 1255 .write = ftrace_filter_write,
41c52c0d 1256 .llseek = ftrace_regex_lseek,
5072c59f
SR
1257 .release = ftrace_filter_release,
1258};
1259
41c52c0d
SR
1260static struct file_operations ftrace_notrace_fops = {
1261 .open = ftrace_notrace_open,
1262 .read = ftrace_regex_read,
1263 .write = ftrace_notrace_write,
1264 .llseek = ftrace_regex_lseek,
1265 .release = ftrace_notrace_release,
1266};
1267
5072c59f
SR
1268static __init int ftrace_init_debugfs(void)
1269{
1270 struct dentry *d_tracer;
1271 struct dentry *entry;
1272
1273 d_tracer = tracing_init_dentry();
1274
1275 entry = debugfs_create_file("available_filter_functions", 0444,
1276 d_tracer, NULL, &ftrace_avail_fops);
1277 if (!entry)
1278 pr_warning("Could not create debugfs "
1279 "'available_filter_functions' entry\n");
1280
eb9a7bf0
AS
1281 entry = debugfs_create_file("failures", 0444,
1282 d_tracer, NULL, &ftrace_failures_fops);
1283 if (!entry)
1284 pr_warning("Could not create debugfs 'failures' entry\n");
1285
5072c59f
SR
1286 entry = debugfs_create_file("set_ftrace_filter", 0644, d_tracer,
1287 NULL, &ftrace_filter_fops);
1288 if (!entry)
1289 pr_warning("Could not create debugfs "
1290 "'set_ftrace_filter' entry\n");
41c52c0d
SR
1291
1292 entry = debugfs_create_file("set_ftrace_notrace", 0644, d_tracer,
1293 NULL, &ftrace_notrace_fops);
1294 if (!entry)
1295 pr_warning("Could not create debugfs "
1296 "'set_ftrace_notrace' entry\n");
ad90c0e3 1297
5072c59f
SR
1298 return 0;
1299}
1300
1301fs_initcall(ftrace_init_debugfs);
1302
68bf21aa
SR
1303static int ftrace_convert_nops(unsigned long *start,
1304 unsigned long *end)
1305{
1306 unsigned long *p;
1307 unsigned long addr;
1308 unsigned long flags;
1309
08f5ac90 1310 mutex_lock(&ftrace_start_lock);
68bf21aa
SR
1311 p = start;
1312 while (p < end) {
1313 addr = ftrace_call_adjust(*p++);
1314 ftrace_record_ip(addr);
68bf21aa
SR
1315 }
1316
08f5ac90 1317 /* disable interrupts to prevent kstop machine */
68bf21aa 1318 local_irq_save(flags);
08f5ac90 1319 ftrace_update_code();
68bf21aa 1320 local_irq_restore(flags);
08f5ac90 1321 mutex_unlock(&ftrace_start_lock);
68bf21aa
SR
1322
1323 return 0;
1324}
1325
90d595fe
SR
1326void ftrace_init_module(unsigned long *start, unsigned long *end)
1327{
00fd61ae 1328 if (ftrace_disabled || start == end)
fed1939c 1329 return;
90d595fe
SR
1330 ftrace_convert_nops(start, end);
1331}
1332
68bf21aa
SR
1333extern unsigned long __start_mcount_loc[];
1334extern unsigned long __stop_mcount_loc[];
1335
1336void __init ftrace_init(void)
1337{
1338 unsigned long count, addr, flags;
1339 int ret;
1340
1341 /* Keep the ftrace pointer to the stub */
1342 addr = (unsigned long)ftrace_stub;
1343
1344 local_irq_save(flags);
1345 ftrace_dyn_arch_init(&addr);
1346 local_irq_restore(flags);
1347
1348 /* ftrace_dyn_arch_init places the return code in addr */
1349 if (addr)
1350 goto failed;
1351
1352 count = __stop_mcount_loc - __start_mcount_loc;
1353
1354 ret = ftrace_dyn_table_alloc(count);
1355 if (ret)
1356 goto failed;
1357
1358 last_ftrace_enabled = ftrace_enabled = 1;
1359
1360 ret = ftrace_convert_nops(__start_mcount_loc,
1361 __stop_mcount_loc);
1362
1363 return;
1364 failed:
1365 ftrace_disabled = 1;
1366}
68bf21aa 1367
3d083395 1368#else
0b6e4d56
FW
1369
1370static int __init ftrace_nodyn_init(void)
1371{
1372 ftrace_enabled = 1;
1373 return 0;
1374}
1375device_initcall(ftrace_nodyn_init);
1376
c7aafc54
IM
1377# define ftrace_startup() do { } while (0)
1378# define ftrace_shutdown() do { } while (0)
1379# define ftrace_startup_sysctl() do { } while (0)
1380# define ftrace_shutdown_sysctl() do { } while (0)
3d083395
SR
1381#endif /* CONFIG_DYNAMIC_FTRACE */
1382
a2bb6a3d 1383/**
81adbdc0 1384 * ftrace_kill - kill ftrace
a2bb6a3d
SR
1385 *
1386 * This function should be used by panic code. It stops ftrace
1387 * but in a not so nice way. If you need to simply kill ftrace
1388 * from a non-atomic section, use ftrace_kill.
1389 */
81adbdc0 1390void ftrace_kill(void)
a2bb6a3d
SR
1391{
1392 ftrace_disabled = 1;
1393 ftrace_enabled = 0;
a2bb6a3d
SR
1394 clear_ftrace_function();
1395}
1396
16444a8a 1397/**
3d083395
SR
1398 * register_ftrace_function - register a function for profiling
1399 * @ops - ops structure that holds the function for profiling.
16444a8a 1400 *
3d083395
SR
1401 * Register a function to be called by all functions in the
1402 * kernel.
1403 *
1404 * Note: @ops->func and all the functions it calls must be labeled
1405 * with "notrace", otherwise it will go into a
1406 * recursive loop.
16444a8a 1407 */
3d083395 1408int register_ftrace_function(struct ftrace_ops *ops)
16444a8a 1409{
b0fc494f
SR
1410 int ret;
1411
4eebcc81
SR
1412 if (unlikely(ftrace_disabled))
1413 return -1;
1414
b0fc494f 1415 mutex_lock(&ftrace_sysctl_lock);
b0fc494f 1416 ret = __register_ftrace_function(ops);
d61f82d0 1417 ftrace_startup();
b0fc494f
SR
1418 mutex_unlock(&ftrace_sysctl_lock);
1419
1420 return ret;
3d083395
SR
1421}
1422
1423/**
1424 * unregister_ftrace_function - unresgister a function for profiling.
1425 * @ops - ops structure that holds the function to unregister
1426 *
1427 * Unregister a function that was added to be called by ftrace profiling.
1428 */
1429int unregister_ftrace_function(struct ftrace_ops *ops)
1430{
1431 int ret;
1432
b0fc494f 1433 mutex_lock(&ftrace_sysctl_lock);
3d083395 1434 ret = __unregister_ftrace_function(ops);
d61f82d0 1435 ftrace_shutdown();
b0fc494f
SR
1436 mutex_unlock(&ftrace_sysctl_lock);
1437
1438 return ret;
1439}
1440
e309b41d 1441int
b0fc494f 1442ftrace_enable_sysctl(struct ctl_table *table, int write,
5072c59f 1443 struct file *file, void __user *buffer, size_t *lenp,
b0fc494f
SR
1444 loff_t *ppos)
1445{
1446 int ret;
1447
4eebcc81
SR
1448 if (unlikely(ftrace_disabled))
1449 return -ENODEV;
1450
b0fc494f
SR
1451 mutex_lock(&ftrace_sysctl_lock);
1452
5072c59f 1453 ret = proc_dointvec(table, write, file, buffer, lenp, ppos);
b0fc494f
SR
1454
1455 if (ret || !write || (last_ftrace_enabled == ftrace_enabled))
1456 goto out;
1457
1458 last_ftrace_enabled = ftrace_enabled;
1459
1460 if (ftrace_enabled) {
1461
1462 ftrace_startup_sysctl();
1463
1464 /* we are starting ftrace again */
1465 if (ftrace_list != &ftrace_list_end) {
1466 if (ftrace_list->next == &ftrace_list_end)
1467 ftrace_trace_function = ftrace_list->func;
1468 else
1469 ftrace_trace_function = ftrace_list_func;
1470 }
1471
1472 } else {
1473 /* stopping ftrace calls (just send to ftrace_stub) */
1474 ftrace_trace_function = ftrace_stub;
1475
1476 ftrace_shutdown_sysctl();
1477 }
1478
1479 out:
1480 mutex_unlock(&ftrace_sysctl_lock);
3d083395 1481 return ret;
16444a8a 1482}
f17845e5 1483