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