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