]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - kernel/trace/ftrace.c
ftrace: dynamic ftrace process only text section
[mirror_ubuntu-artful-kernel.git] / kernel / trace / ftrace.c
CommitLineData
16444a8a
ACM
1/*
2 * Infrastructure for profiling code inserted by 'gcc -pg'.
3 *
4 * Copyright (C) 2007-2008 Steven Rostedt <srostedt@redhat.com>
5 * Copyright (C) 2004-2008 Ingo Molnar <mingo@redhat.com>
6 *
7 * Originally ported from the -rt patch by:
8 * Copyright (C) 2007 Arnaldo Carvalho de Melo <acme@redhat.com>
9 *
10 * Based on code in the latency_tracer, that is:
11 *
12 * Copyright (C) 2004-2006 Ingo Molnar
13 * Copyright (C) 2004 William Lee Irwin III
14 */
15
3d083395
SR
16#include <linux/stop_machine.h>
17#include <linux/clocksource.h>
18#include <linux/kallsyms.h>
5072c59f
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>
2d8b820b 28#include <linux/hash.h>
3d083395
SR
29#include <linux/list.h>
30
395a59d0
AS
31#include <asm/ftrace.h>
32
3d083395 33#include "trace.h"
16444a8a 34
4eebcc81
SR
35/* ftrace_enabled is a method to turn ftrace on or off */
36int ftrace_enabled __read_mostly;
d61f82d0 37static int last_ftrace_enabled;
b0fc494f 38
4eebcc81
SR
39/*
40 * ftrace_disabled is set when an anomaly is discovered.
41 * ftrace_disabled is much stronger than ftrace_enabled.
42 */
43static int ftrace_disabled __read_mostly;
44
3d083395 45static DEFINE_SPINLOCK(ftrace_lock);
b0fc494f
SR
46static DEFINE_MUTEX(ftrace_sysctl_lock);
47
16444a8a
ACM
48static struct ftrace_ops ftrace_list_end __read_mostly =
49{
50 .func = ftrace_stub,
51};
52
53static struct ftrace_ops *ftrace_list __read_mostly = &ftrace_list_end;
54ftrace_func_t ftrace_trace_function __read_mostly = ftrace_stub;
55
f2252935 56static void ftrace_list_func(unsigned long ip, unsigned long parent_ip)
16444a8a
ACM
57{
58 struct ftrace_ops *op = ftrace_list;
59
60 /* in case someone actually ports this to alpha! */
61 read_barrier_depends();
62
63 while (op != &ftrace_list_end) {
64 /* silly alpha */
65 read_barrier_depends();
66 op->func(ip, parent_ip);
67 op = op->next;
68 };
69}
70
71/**
3d083395 72 * clear_ftrace_function - reset the ftrace function
16444a8a 73 *
3d083395
SR
74 * This NULLs the ftrace function and in essence stops
75 * tracing. There may be lag
16444a8a 76 */
3d083395 77void clear_ftrace_function(void)
16444a8a 78{
3d083395
SR
79 ftrace_trace_function = ftrace_stub;
80}
81
e309b41d 82static int __register_ftrace_function(struct ftrace_ops *ops)
3d083395 83{
99ecdc43 84 /* should not be called from interrupt context */
3d083395 85 spin_lock(&ftrace_lock);
16444a8a 86
16444a8a
ACM
87 ops->next = ftrace_list;
88 /*
89 * We are entering ops into the ftrace_list but another
90 * CPU might be walking that list. We need to make sure
91 * the ops->next pointer is valid before another CPU sees
92 * the ops pointer included into the ftrace_list.
93 */
94 smp_wmb();
95 ftrace_list = ops;
3d083395 96
b0fc494f
SR
97 if (ftrace_enabled) {
98 /*
99 * For one func, simply call it directly.
100 * For more than one func, call the chain.
101 */
102 if (ops->next == &ftrace_list_end)
103 ftrace_trace_function = ops->func;
104 else
105 ftrace_trace_function = ftrace_list_func;
106 }
3d083395
SR
107
108 spin_unlock(&ftrace_lock);
16444a8a
ACM
109
110 return 0;
111}
112
e309b41d 113static int __unregister_ftrace_function(struct ftrace_ops *ops)
16444a8a 114{
16444a8a
ACM
115 struct ftrace_ops **p;
116 int ret = 0;
117
99ecdc43 118 /* should not be called from interrupt context */
3d083395 119 spin_lock(&ftrace_lock);
16444a8a
ACM
120
121 /*
3d083395
SR
122 * If we are removing the last function, then simply point
123 * to the ftrace_stub.
16444a8a
ACM
124 */
125 if (ftrace_list == ops && ops->next == &ftrace_list_end) {
126 ftrace_trace_function = ftrace_stub;
127 ftrace_list = &ftrace_list_end;
128 goto out;
129 }
130
131 for (p = &ftrace_list; *p != &ftrace_list_end; p = &(*p)->next)
132 if (*p == ops)
133 break;
134
135 if (*p != ops) {
136 ret = -1;
137 goto out;
138 }
139
140 *p = (*p)->next;
141
b0fc494f
SR
142 if (ftrace_enabled) {
143 /* If we only have one func left, then call that directly */
144 if (ftrace_list == &ftrace_list_end ||
145 ftrace_list->next == &ftrace_list_end)
146 ftrace_trace_function = ftrace_list->func;
147 }
16444a8a
ACM
148
149 out:
3d083395
SR
150 spin_unlock(&ftrace_lock);
151
152 return ret;
153}
154
155#ifdef CONFIG_DYNAMIC_FTRACE
156
99ecdc43
SR
157#ifndef CONFIG_FTRACE_MCOUNT_RECORD
158/*
159 * The hash lock is only needed when the recording of the mcount
160 * callers are dynamic. That is, by the caller themselves and
161 * not recorded via the compilation.
162 */
163static DEFINE_SPINLOCK(ftrace_hash_lock);
2d7da80f 164#define ftrace_hash_lock(flags) spin_lock_irqsave(&ftrace_hash_lock, flags)
644f991d
SR
165#define ftrace_hash_unlock(flags) \
166 spin_unlock_irqrestore(&ftrace_hash_lock, flags)
bd95b88d 167static void ftrace_release_hash(unsigned long start, unsigned long end);
99ecdc43
SR
168#else
169/* This is protected via the ftrace_lock with MCOUNT_RECORD. */
ac8825ec 170#define ftrace_hash_lock(flags) do { (void)(flags); } while (0)
99ecdc43 171#define ftrace_hash_unlock(flags) do { } while(0)
bd95b88d
SR
172static inline void ftrace_release_hash(unsigned long start, unsigned long end)
173{
174}
99ecdc43
SR
175#endif
176
71c67d58
SN
177/*
178 * Since MCOUNT_ADDR may point to mcount itself, we do not want
179 * to get it confused by reading a reference in the code as we
180 * are parsing on objcopy output of text. Use a variable for
181 * it instead.
182 */
183static unsigned long mcount_addr = MCOUNT_ADDR;
184
e1c08bdd 185static struct task_struct *ftraced_task;
e1c08bdd 186
d61f82d0
SR
187enum {
188 FTRACE_ENABLE_CALLS = (1 << 0),
189 FTRACE_DISABLE_CALLS = (1 << 1),
190 FTRACE_UPDATE_TRACE_FUNC = (1 << 2),
191 FTRACE_ENABLE_MCOUNT = (1 << 3),
192 FTRACE_DISABLE_MCOUNT = (1 << 4),
193};
194
5072c59f 195static int ftrace_filtered;
ecea656d
AS
196static int tracing_on;
197static int frozen_record_count;
5072c59f 198
3d083395
SR
199static struct hlist_head ftrace_hash[FTRACE_HASHSIZE];
200
201static DEFINE_PER_CPU(int, ftrace_shutdown_disable_cpu);
202
3d083395 203static DEFINE_MUTEX(ftraced_lock);
41c52c0d 204static DEFINE_MUTEX(ftrace_regex_lock);
3d083395 205
3c1720f0
SR
206struct ftrace_page {
207 struct ftrace_page *next;
aa5e5cea 208 unsigned long index;
3c1720f0 209 struct dyn_ftrace records[];
aa5e5cea 210};
3c1720f0
SR
211
212#define ENTRIES_PER_PAGE \
213 ((PAGE_SIZE - sizeof(struct ftrace_page)) / sizeof(struct dyn_ftrace))
214
215/* estimate from running different kernels */
216#define NR_TO_INIT 10000
217
218static struct ftrace_page *ftrace_pages_start;
219static struct ftrace_page *ftrace_pages;
220
3d083395
SR
221static int ftraced_trigger;
222static int ftraced_suspend;
ad90c0e3 223static int ftraced_stop;
3d083395
SR
224
225static int ftrace_record_suspend;
226
37ad5084
SR
227static struct dyn_ftrace *ftrace_free_records;
228
ecea656d
AS
229
230#ifdef CONFIG_KPROBES
231static inline void freeze_record(struct dyn_ftrace *rec)
232{
233 if (!(rec->flags & FTRACE_FL_FROZEN)) {
234 rec->flags |= FTRACE_FL_FROZEN;
235 frozen_record_count++;
236 }
237}
238
239static inline void unfreeze_record(struct dyn_ftrace *rec)
240{
241 if (rec->flags & FTRACE_FL_FROZEN) {
242 rec->flags &= ~FTRACE_FL_FROZEN;
243 frozen_record_count--;
244 }
245}
246
247static inline int record_frozen(struct dyn_ftrace *rec)
248{
249 return rec->flags & FTRACE_FL_FROZEN;
250}
251#else
252# define freeze_record(rec) ({ 0; })
253# define unfreeze_record(rec) ({ 0; })
254# define record_frozen(rec) ({ 0; })
255#endif /* CONFIG_KPROBES */
256
257int skip_trace(unsigned long ip)
258{
259 unsigned long fl;
260 struct dyn_ftrace *rec;
261 struct hlist_node *t;
262 struct hlist_head *head;
263
264 if (frozen_record_count == 0)
265 return 0;
266
267 head = &ftrace_hash[hash_long(ip, FTRACE_HASHBITS)];
268 hlist_for_each_entry_rcu(rec, t, head, node) {
269 if (rec->ip == ip) {
270 if (record_frozen(rec)) {
271 if (rec->flags & FTRACE_FL_FAILED)
272 return 1;
273
274 if (!(rec->flags & FTRACE_FL_CONVERTED))
275 return 1;
276
277 if (!tracing_on || !ftrace_enabled)
278 return 1;
279
280 if (ftrace_filtered) {
281 fl = rec->flags & (FTRACE_FL_FILTER |
282 FTRACE_FL_NOTRACE);
283 if (!fl || (fl & FTRACE_FL_NOTRACE))
284 return 1;
285 }
286 }
287 break;
288 }
289 }
290
291 return 0;
292}
293
e309b41d 294static inline int
9ff9cdb2 295ftrace_ip_in_hash(unsigned long ip, unsigned long key)
3d083395
SR
296{
297 struct dyn_ftrace *p;
298 struct hlist_node *t;
299 int found = 0;
300
ffdaa358 301 hlist_for_each_entry_rcu(p, t, &ftrace_hash[key], node) {
3d083395
SR
302 if (p->ip == ip) {
303 found = 1;
304 break;
305 }
306 }
307
308 return found;
309}
310
e309b41d 311static inline void
3d083395
SR
312ftrace_add_hash(struct dyn_ftrace *node, unsigned long key)
313{
ffdaa358 314 hlist_add_head_rcu(&node->node, &ftrace_hash[key]);
3d083395
SR
315}
316
0eb96701
AS
317/* called from kstop_machine */
318static inline void ftrace_del_hash(struct dyn_ftrace *node)
319{
320 hlist_del(&node->node);
321}
322
e309b41d 323static void ftrace_free_rec(struct dyn_ftrace *rec)
37ad5084 324{
37ad5084
SR
325 rec->ip = (unsigned long)ftrace_free_records;
326 ftrace_free_records = rec;
327 rec->flags |= FTRACE_FL_FREE;
328}
329
fed1939c
SR
330void ftrace_release(void *start, unsigned long size)
331{
332 struct dyn_ftrace *rec;
333 struct ftrace_page *pg;
334 unsigned long s = (unsigned long)start;
335 unsigned long e = s + size;
336 int i;
337
00fd61ae 338 if (ftrace_disabled || !start)
fed1939c
SR
339 return;
340
99ecdc43 341 /* should not be called from interrupt context */
fed1939c
SR
342 spin_lock(&ftrace_lock);
343
344 for (pg = ftrace_pages_start; pg; pg = pg->next) {
345 for (i = 0; i < pg->index; i++) {
346 rec = &pg->records[i];
347
348 if ((rec->ip >= s) && (rec->ip < e))
349 ftrace_free_rec(rec);
350 }
351 }
352 spin_unlock(&ftrace_lock);
353
bd95b88d 354 ftrace_release_hash(s, e);
fed1939c
SR
355}
356
e309b41d 357static struct dyn_ftrace *ftrace_alloc_dyn_node(unsigned long ip)
3c1720f0 358{
37ad5084
SR
359 struct dyn_ftrace *rec;
360
361 /* First check for freed records */
362 if (ftrace_free_records) {
363 rec = ftrace_free_records;
364
37ad5084
SR
365 if (unlikely(!(rec->flags & FTRACE_FL_FREE))) {
366 WARN_ON_ONCE(1);
367 ftrace_free_records = NULL;
4eebcc81
SR
368 ftrace_disabled = 1;
369 ftrace_enabled = 0;
37ad5084
SR
370 return NULL;
371 }
372
373 ftrace_free_records = (void *)rec->ip;
374 memset(rec, 0, sizeof(*rec));
375 return rec;
376 }
377
3c1720f0
SR
378 if (ftrace_pages->index == ENTRIES_PER_PAGE) {
379 if (!ftrace_pages->next)
380 return NULL;
381 ftrace_pages = ftrace_pages->next;
382 }
383
384 return &ftrace_pages->records[ftrace_pages->index++];
385}
386
e309b41d 387static void
d61f82d0 388ftrace_record_ip(unsigned long ip)
3d083395
SR
389{
390 struct dyn_ftrace *node;
391 unsigned long flags;
392 unsigned long key;
393 int resched;
2bb6f8d6 394 int cpu;
3d083395 395
4eebcc81 396 if (!ftrace_enabled || ftrace_disabled)
d61f82d0
SR
397 return;
398
3d083395
SR
399 resched = need_resched();
400 preempt_disable_notrace();
401
2bb6f8d6
SR
402 /*
403 * We simply need to protect against recursion.
404 * Use the the raw version of smp_processor_id and not
405 * __get_cpu_var which can call debug hooks that can
406 * cause a recursive crash here.
407 */
408 cpu = raw_smp_processor_id();
409 per_cpu(ftrace_shutdown_disable_cpu, cpu)++;
410 if (per_cpu(ftrace_shutdown_disable_cpu, cpu) != 1)
3d083395
SR
411 goto out;
412
413 if (unlikely(ftrace_record_suspend))
414 goto out;
415
416 key = hash_long(ip, FTRACE_HASHBITS);
417
418 WARN_ON_ONCE(key >= FTRACE_HASHSIZE);
419
420 if (ftrace_ip_in_hash(ip, key))
421 goto out;
422
99ecdc43 423 ftrace_hash_lock(flags);
3d083395
SR
424
425 /* This ip may have hit the hash before the lock */
426 if (ftrace_ip_in_hash(ip, key))
427 goto out_unlock;
428
d61f82d0 429 node = ftrace_alloc_dyn_node(ip);
3d083395
SR
430 if (!node)
431 goto out_unlock;
432
433 node->ip = ip;
434
435 ftrace_add_hash(node, key);
436
437 ftraced_trigger = 1;
438
439 out_unlock:
99ecdc43 440 ftrace_hash_unlock(flags);
3d083395 441 out:
2bb6f8d6 442 per_cpu(ftrace_shutdown_disable_cpu, cpu)--;
3d083395
SR
443
444 /* prevent recursion with scheduler */
445 if (resched)
446 preempt_enable_no_resched_notrace();
447 else
448 preempt_enable_notrace();
449}
450
caf8cdeb 451#define FTRACE_ADDR ((long)(ftrace_caller))
3c1720f0 452
0eb96701 453static int
5072c59f
SR
454__ftrace_replace_code(struct dyn_ftrace *rec,
455 unsigned char *old, unsigned char *new, int enable)
456{
41c52c0d 457 unsigned long ip, fl;
5072c59f
SR
458
459 ip = rec->ip;
460
461 if (ftrace_filtered && enable) {
5072c59f
SR
462 /*
463 * If filtering is on:
464 *
465 * If this record is set to be filtered and
466 * is enabled then do nothing.
467 *
468 * If this record is set to be filtered and
469 * it is not enabled, enable it.
470 *
471 * If this record is not set to be filtered
472 * and it is not enabled do nothing.
473 *
41c52c0d
SR
474 * If this record is set not to trace then
475 * do nothing.
476 *
a4500b84
AS
477 * If this record is set not to trace and
478 * it is enabled then disable it.
479 *
5072c59f
SR
480 * If this record is not set to be filtered and
481 * it is enabled, disable it.
482 */
a4500b84
AS
483
484 fl = rec->flags & (FTRACE_FL_FILTER | FTRACE_FL_NOTRACE |
485 FTRACE_FL_ENABLED);
5072c59f
SR
486
487 if ((fl == (FTRACE_FL_FILTER | FTRACE_FL_ENABLED)) ||
a4500b84
AS
488 (fl == (FTRACE_FL_FILTER | FTRACE_FL_NOTRACE)) ||
489 !fl || (fl == FTRACE_FL_NOTRACE))
0eb96701 490 return 0;
5072c59f
SR
491
492 /*
493 * If it is enabled disable it,
494 * otherwise enable it!
495 */
a4500b84 496 if (fl & FTRACE_FL_ENABLED) {
5072c59f
SR
497 /* swap new and old */
498 new = old;
499 old = ftrace_call_replace(ip, FTRACE_ADDR);
500 rec->flags &= ~FTRACE_FL_ENABLED;
501 } else {
502 new = ftrace_call_replace(ip, FTRACE_ADDR);
503 rec->flags |= FTRACE_FL_ENABLED;
504 }
505 } else {
506
41c52c0d
SR
507 if (enable) {
508 /*
509 * If this record is set not to trace and is
510 * not enabled, do nothing.
511 */
512 fl = rec->flags & (FTRACE_FL_NOTRACE | FTRACE_FL_ENABLED);
513 if (fl == FTRACE_FL_NOTRACE)
0eb96701 514 return 0;
41c52c0d 515
5072c59f 516 new = ftrace_call_replace(ip, FTRACE_ADDR);
41c52c0d 517 } else
5072c59f
SR
518 old = ftrace_call_replace(ip, FTRACE_ADDR);
519
520 if (enable) {
521 if (rec->flags & FTRACE_FL_ENABLED)
0eb96701 522 return 0;
5072c59f
SR
523 rec->flags |= FTRACE_FL_ENABLED;
524 } else {
525 if (!(rec->flags & FTRACE_FL_ENABLED))
0eb96701 526 return 0;
5072c59f
SR
527 rec->flags &= ~FTRACE_FL_ENABLED;
528 }
529 }
530
0eb96701 531 return ftrace_modify_code(ip, old, new);
5072c59f
SR
532}
533
e309b41d 534static void ftrace_replace_code(int enable)
3c1720f0 535{
0eb96701 536 int i, failed;
3c1720f0
SR
537 unsigned char *new = NULL, *old = NULL;
538 struct dyn_ftrace *rec;
539 struct ftrace_page *pg;
3c1720f0 540
5072c59f 541 if (enable)
3c1720f0
SR
542 old = ftrace_nop_replace();
543 else
544 new = ftrace_nop_replace();
545
546 for (pg = ftrace_pages_start; pg; pg = pg->next) {
547 for (i = 0; i < pg->index; i++) {
548 rec = &pg->records[i];
549
550 /* don't modify code that has already faulted */
551 if (rec->flags & FTRACE_FL_FAILED)
552 continue;
553
f22f9a89 554 /* ignore updates to this record's mcount site */
98a05ed4
AS
555 if (get_kprobe((void *)rec->ip)) {
556 freeze_record(rec);
f22f9a89 557 continue;
98a05ed4
AS
558 } else {
559 unfreeze_record(rec);
560 }
f22f9a89 561
0eb96701
AS
562 failed = __ftrace_replace_code(rec, old, new, enable);
563 if (failed && (rec->flags & FTRACE_FL_CONVERTED)) {
564 rec->flags |= FTRACE_FL_FAILED;
565 if ((system_state == SYSTEM_BOOTING) ||
34078a5e 566 !core_kernel_text(rec->ip)) {
0eb96701
AS
567 ftrace_del_hash(rec);
568 ftrace_free_rec(rec);
569 }
570 }
3c1720f0
SR
571 }
572 }
573}
574
e309b41d 575static void ftrace_shutdown_replenish(void)
3c1720f0
SR
576{
577 if (ftrace_pages->next)
578 return;
579
580 /* allocate another page */
581 ftrace_pages->next = (void *)get_zeroed_page(GFP_KERNEL);
582}
3d083395 583
05736a42
SR
584static void print_ip_ins(const char *fmt, unsigned char *p)
585{
586 int i;
587
588 printk(KERN_CONT "%s", fmt);
589
590 for (i = 0; i < MCOUNT_INSN_SIZE; i++)
591 printk(KERN_CONT "%s%02x", i ? ":" : "", p[i]);
592}
593
492a7ea5 594static int
d61f82d0 595ftrace_code_disable(struct dyn_ftrace *rec)
3c1720f0
SR
596{
597 unsigned long ip;
598 unsigned char *nop, *call;
599 int failed;
600
601 ip = rec->ip;
602
603 nop = ftrace_nop_replace();
3b47bfc1 604 call = ftrace_call_replace(ip, mcount_addr);
3c1720f0
SR
605
606 failed = ftrace_modify_code(ip, call, nop);
37ad5084 607 if (failed) {
05736a42
SR
608 switch (failed) {
609 case 1:
610 WARN_ON_ONCE(1);
611 pr_info("ftrace faulted on modifying ");
612 print_ip_sym(ip);
613 break;
614 case 2:
615 WARN_ON_ONCE(1);
616 pr_info("ftrace failed to modify ");
617 print_ip_sym(ip);
618 print_ip_ins(" expected: ", call);
619 print_ip_ins(" actual: ", (unsigned char *)ip);
620 print_ip_ins(" replace: ", nop);
621 printk(KERN_CONT "\n");
622 break;
623 }
624
3c1720f0 625 rec->flags |= FTRACE_FL_FAILED;
492a7ea5 626 return 0;
37ad5084 627 }
492a7ea5 628 return 1;
3c1720f0
SR
629}
630
ad90c0e3
SR
631static int __ftrace_update_code(void *ignore);
632
e309b41d 633static int __ftrace_modify_code(void *data)
3d083395 634{
d61f82d0
SR
635 unsigned long addr;
636 int *command = data;
637
ad90c0e3
SR
638 if (*command & FTRACE_ENABLE_CALLS) {
639 /*
640 * Update any recorded ips now that we have the
641 * machine stopped
642 */
643 __ftrace_update_code(NULL);
d61f82d0 644 ftrace_replace_code(1);
ecea656d
AS
645 tracing_on = 1;
646 } else if (*command & FTRACE_DISABLE_CALLS) {
d61f82d0 647 ftrace_replace_code(0);
ecea656d
AS
648 tracing_on = 0;
649 }
d61f82d0
SR
650
651 if (*command & FTRACE_UPDATE_TRACE_FUNC)
652 ftrace_update_ftrace_func(ftrace_trace_function);
653
654 if (*command & FTRACE_ENABLE_MCOUNT) {
655 addr = (unsigned long)ftrace_record_ip;
656 ftrace_mcount_set(&addr);
657 } else if (*command & FTRACE_DISABLE_MCOUNT) {
658 addr = (unsigned long)ftrace_stub;
659 ftrace_mcount_set(&addr);
660 }
661
662 return 0;
3d083395
SR
663}
664
e309b41d 665static void ftrace_run_update_code(int command)
3d083395 666{
784e2d76 667 stop_machine(__ftrace_modify_code, &command, NULL);
3d083395
SR
668}
669
ad90c0e3
SR
670void ftrace_disable_daemon(void)
671{
672 /* Stop the daemon from calling kstop_machine */
673 mutex_lock(&ftraced_lock);
674 ftraced_stop = 1;
675 mutex_unlock(&ftraced_lock);
676
677 ftrace_force_update();
678}
679
680void ftrace_enable_daemon(void)
681{
682 mutex_lock(&ftraced_lock);
683 ftraced_stop = 0;
684 mutex_unlock(&ftraced_lock);
685
686 ftrace_force_update();
687}
688
d61f82d0
SR
689static ftrace_func_t saved_ftrace_func;
690
e309b41d 691static void ftrace_startup(void)
3d083395 692{
d61f82d0
SR
693 int command = 0;
694
4eebcc81
SR
695 if (unlikely(ftrace_disabled))
696 return;
697
3d083395
SR
698 mutex_lock(&ftraced_lock);
699 ftraced_suspend++;
d61f82d0
SR
700 if (ftraced_suspend == 1)
701 command |= FTRACE_ENABLE_CALLS;
702
703 if (saved_ftrace_func != ftrace_trace_function) {
704 saved_ftrace_func = ftrace_trace_function;
705 command |= FTRACE_UPDATE_TRACE_FUNC;
706 }
707
708 if (!command || !ftrace_enabled)
3d083395 709 goto out;
3d083395 710
d61f82d0 711 ftrace_run_update_code(command);
3d083395
SR
712 out:
713 mutex_unlock(&ftraced_lock);
714}
715
e309b41d 716static void ftrace_shutdown(void)
3d083395 717{
d61f82d0
SR
718 int command = 0;
719
4eebcc81
SR
720 if (unlikely(ftrace_disabled))
721 return;
722
3d083395
SR
723 mutex_lock(&ftraced_lock);
724 ftraced_suspend--;
d61f82d0
SR
725 if (!ftraced_suspend)
726 command |= FTRACE_DISABLE_CALLS;
3d083395 727
d61f82d0
SR
728 if (saved_ftrace_func != ftrace_trace_function) {
729 saved_ftrace_func = ftrace_trace_function;
730 command |= FTRACE_UPDATE_TRACE_FUNC;
731 }
3d083395 732
d61f82d0
SR
733 if (!command || !ftrace_enabled)
734 goto out;
735
736 ftrace_run_update_code(command);
3d083395
SR
737 out:
738 mutex_unlock(&ftraced_lock);
739}
740
e309b41d 741static void ftrace_startup_sysctl(void)
b0fc494f 742{
d61f82d0
SR
743 int command = FTRACE_ENABLE_MCOUNT;
744
4eebcc81
SR
745 if (unlikely(ftrace_disabled))
746 return;
747
b0fc494f 748 mutex_lock(&ftraced_lock);
d61f82d0
SR
749 /* Force update next time */
750 saved_ftrace_func = NULL;
b0fc494f
SR
751 /* ftraced_suspend is true if we want ftrace running */
752 if (ftraced_suspend)
d61f82d0
SR
753 command |= FTRACE_ENABLE_CALLS;
754
755 ftrace_run_update_code(command);
b0fc494f
SR
756 mutex_unlock(&ftraced_lock);
757}
758
e309b41d 759static void ftrace_shutdown_sysctl(void)
b0fc494f 760{
d61f82d0
SR
761 int command = FTRACE_DISABLE_MCOUNT;
762
4eebcc81
SR
763 if (unlikely(ftrace_disabled))
764 return;
765
b0fc494f
SR
766 mutex_lock(&ftraced_lock);
767 /* ftraced_suspend is true if ftrace is running */
768 if (ftraced_suspend)
d61f82d0
SR
769 command |= FTRACE_DISABLE_CALLS;
770
771 ftrace_run_update_code(command);
b0fc494f
SR
772 mutex_unlock(&ftraced_lock);
773}
774
3d083395
SR
775static cycle_t ftrace_update_time;
776static unsigned long ftrace_update_cnt;
777unsigned long ftrace_update_tot_cnt;
778
e309b41d 779static int __ftrace_update_code(void *ignore)
3d083395 780{
f22f9a89
AS
781 int i, save_ftrace_enabled;
782 cycle_t start, stop;
3d083395 783 struct dyn_ftrace *p;
0eb96701 784 struct hlist_node *t, *n;
f22f9a89 785 struct hlist_head *head, temp_list;
3d083395 786
d61f82d0 787 /* Don't be recording funcs now */
ad90c0e3 788 ftrace_record_suspend++;
d61f82d0
SR
789 save_ftrace_enabled = ftrace_enabled;
790 ftrace_enabled = 0;
3d083395 791
750ed1a4 792 start = ftrace_now(raw_smp_processor_id());
3d083395
SR
793 ftrace_update_cnt = 0;
794
795 /* No locks needed, the machine is stopped! */
796 for (i = 0; i < FTRACE_HASHSIZE; i++) {
f22f9a89
AS
797 INIT_HLIST_HEAD(&temp_list);
798 head = &ftrace_hash[i];
799
0eb96701 800 /* all CPUS are stopped, we are safe to modify code */
f22f9a89 801 hlist_for_each_entry_safe(p, t, n, head, node) {
0eb96701
AS
802 /* Skip over failed records which have not been
803 * freed. */
804 if (p->flags & FTRACE_FL_FAILED)
805 continue;
3d083395 806
0eb96701
AS
807 /* Unconverted records are always at the head of the
808 * hash bucket. Once we encounter a converted record,
809 * simply skip over to the next bucket. Saves ftraced
810 * some processor cycles (ftrace does its bid for
811 * global warming :-p ). */
812 if (p->flags & (FTRACE_FL_CONVERTED))
813 break;
3d083395 814
f22f9a89
AS
815 /* Ignore updates to this record's mcount site.
816 * Reintroduce this record at the head of this
817 * bucket to attempt to "convert" it again if
818 * the kprobe on it is unregistered before the
819 * next run. */
820 if (get_kprobe((void *)p->ip)) {
821 ftrace_del_hash(p);
822 INIT_HLIST_NODE(&p->node);
823 hlist_add_head(&p->node, &temp_list);
98a05ed4 824 freeze_record(p);
f22f9a89 825 continue;
98a05ed4
AS
826 } else {
827 unfreeze_record(p);
f22f9a89
AS
828 }
829
830 /* convert record (i.e, patch mcount-call with NOP) */
0eb96701
AS
831 if (ftrace_code_disable(p)) {
832 p->flags |= FTRACE_FL_CONVERTED;
492a7ea5 833 ftrace_update_cnt++;
0eb96701
AS
834 } else {
835 if ((system_state == SYSTEM_BOOTING) ||
34078a5e 836 !core_kernel_text(p->ip)) {
0eb96701
AS
837 ftrace_del_hash(p);
838 ftrace_free_rec(p);
0eb96701
AS
839 }
840 }
3d083395 841 }
f22f9a89
AS
842
843 hlist_for_each_entry_safe(p, t, n, &temp_list, node) {
844 hlist_del(&p->node);
845 INIT_HLIST_NODE(&p->node);
846 hlist_add_head(&p->node, head);
847 }
3d083395
SR
848 }
849
750ed1a4 850 stop = ftrace_now(raw_smp_processor_id());
3d083395
SR
851 ftrace_update_time = stop - start;
852 ftrace_update_tot_cnt += ftrace_update_cnt;
ad90c0e3 853 ftraced_trigger = 0;
3d083395 854
d61f82d0 855 ftrace_enabled = save_ftrace_enabled;
ad90c0e3 856 ftrace_record_suspend--;
16444a8a
ACM
857
858 return 0;
859}
860
ad90c0e3 861static int ftrace_update_code(void)
3d083395 862{
ad90c0e3
SR
863 if (unlikely(ftrace_disabled) ||
864 !ftrace_enabled || !ftraced_trigger)
865 return 0;
4eebcc81 866
784e2d76 867 stop_machine(__ftrace_update_code, NULL, NULL);
ad90c0e3
SR
868
869 return 1;
3d083395
SR
870}
871
68bf21aa 872static int __init ftrace_dyn_table_alloc(unsigned long num_to_init)
3c1720f0
SR
873{
874 struct ftrace_page *pg;
875 int cnt;
876 int i;
3c1720f0
SR
877
878 /* allocate a few pages */
879 ftrace_pages_start = (void *)get_zeroed_page(GFP_KERNEL);
880 if (!ftrace_pages_start)
881 return -1;
882
883 /*
884 * Allocate a few more pages.
885 *
886 * TODO: have some parser search vmlinux before
887 * final linking to find all calls to ftrace.
888 * Then we can:
889 * a) know how many pages to allocate.
890 * and/or
891 * b) set up the table then.
892 *
893 * The dynamic code is still necessary for
894 * modules.
895 */
896
897 pg = ftrace_pages = ftrace_pages_start;
898
68bf21aa
SR
899 cnt = num_to_init / ENTRIES_PER_PAGE;
900 pr_info("ftrace: allocating %ld hash entries in %d pages\n",
901 num_to_init, cnt);
3c1720f0
SR
902
903 for (i = 0; i < cnt; i++) {
904 pg->next = (void *)get_zeroed_page(GFP_KERNEL);
905
906 /* If we fail, we'll try later anyway */
907 if (!pg->next)
908 break;
909
910 pg = pg->next;
911 }
912
913 return 0;
914}
915
5072c59f
SR
916enum {
917 FTRACE_ITER_FILTER = (1 << 0),
918 FTRACE_ITER_CONT = (1 << 1),
41c52c0d 919 FTRACE_ITER_NOTRACE = (1 << 2),
eb9a7bf0 920 FTRACE_ITER_FAILURES = (1 << 3),
5072c59f
SR
921};
922
923#define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
924
925struct ftrace_iterator {
926 loff_t pos;
927 struct ftrace_page *pg;
928 unsigned idx;
929 unsigned flags;
930 unsigned char buffer[FTRACE_BUFF_MAX+1];
931 unsigned buffer_idx;
932 unsigned filtered;
933};
934
e309b41d 935static void *
5072c59f
SR
936t_next(struct seq_file *m, void *v, loff_t *pos)
937{
938 struct ftrace_iterator *iter = m->private;
939 struct dyn_ftrace *rec = NULL;
940
941 (*pos)++;
942
99ecdc43
SR
943 /* should not be called from interrupt context */
944 spin_lock(&ftrace_lock);
5072c59f
SR
945 retry:
946 if (iter->idx >= iter->pg->index) {
947 if (iter->pg->next) {
948 iter->pg = iter->pg->next;
949 iter->idx = 0;
950 goto retry;
951 }
952 } else {
953 rec = &iter->pg->records[iter->idx++];
a9fdda33
SR
954 if ((rec->flags & FTRACE_FL_FREE) ||
955
956 (!(iter->flags & FTRACE_ITER_FAILURES) &&
eb9a7bf0
AS
957 (rec->flags & FTRACE_FL_FAILED)) ||
958
959 ((iter->flags & FTRACE_ITER_FAILURES) &&
a9fdda33 960 !(rec->flags & FTRACE_FL_FAILED)) ||
eb9a7bf0 961
41c52c0d
SR
962 ((iter->flags & FTRACE_ITER_NOTRACE) &&
963 !(rec->flags & FTRACE_FL_NOTRACE))) {
5072c59f
SR
964 rec = NULL;
965 goto retry;
966 }
967 }
99ecdc43 968 spin_unlock(&ftrace_lock);
5072c59f
SR
969
970 iter->pos = *pos;
971
972 return rec;
973}
974
975static void *t_start(struct seq_file *m, loff_t *pos)
976{
977 struct ftrace_iterator *iter = m->private;
978 void *p = NULL;
979 loff_t l = -1;
980
981 if (*pos != iter->pos) {
982 for (p = t_next(m, p, &l); p && l < *pos; p = t_next(m, p, &l))
983 ;
984 } else {
985 l = *pos;
986 p = t_next(m, p, &l);
987 }
988
989 return p;
990}
991
992static void t_stop(struct seq_file *m, void *p)
993{
994}
995
996static int t_show(struct seq_file *m, void *v)
997{
998 struct dyn_ftrace *rec = v;
999 char str[KSYM_SYMBOL_LEN];
1000
1001 if (!rec)
1002 return 0;
1003
1004 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
1005
1006 seq_printf(m, "%s\n", str);
1007
1008 return 0;
1009}
1010
1011static struct seq_operations show_ftrace_seq_ops = {
1012 .start = t_start,
1013 .next = t_next,
1014 .stop = t_stop,
1015 .show = t_show,
1016};
1017
e309b41d 1018static int
5072c59f
SR
1019ftrace_avail_open(struct inode *inode, struct file *file)
1020{
1021 struct ftrace_iterator *iter;
1022 int ret;
1023
4eebcc81
SR
1024 if (unlikely(ftrace_disabled))
1025 return -ENODEV;
1026
5072c59f
SR
1027 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
1028 if (!iter)
1029 return -ENOMEM;
1030
1031 iter->pg = ftrace_pages_start;
1032 iter->pos = -1;
1033
1034 ret = seq_open(file, &show_ftrace_seq_ops);
1035 if (!ret) {
1036 struct seq_file *m = file->private_data;
4bf39a94 1037
5072c59f 1038 m->private = iter;
4bf39a94 1039 } else {
5072c59f 1040 kfree(iter);
4bf39a94 1041 }
5072c59f
SR
1042
1043 return ret;
1044}
1045
1046int ftrace_avail_release(struct inode *inode, struct file *file)
1047{
1048 struct seq_file *m = (struct seq_file *)file->private_data;
1049 struct ftrace_iterator *iter = m->private;
1050
1051 seq_release(inode, file);
1052 kfree(iter);
4bf39a94 1053
5072c59f
SR
1054 return 0;
1055}
1056
eb9a7bf0
AS
1057static int
1058ftrace_failures_open(struct inode *inode, struct file *file)
1059{
1060 int ret;
1061 struct seq_file *m;
1062 struct ftrace_iterator *iter;
1063
1064 ret = ftrace_avail_open(inode, file);
1065 if (!ret) {
1066 m = (struct seq_file *)file->private_data;
1067 iter = (struct ftrace_iterator *)m->private;
1068 iter->flags = FTRACE_ITER_FAILURES;
1069 }
1070
1071 return ret;
1072}
1073
1074
41c52c0d 1075static void ftrace_filter_reset(int enable)
5072c59f
SR
1076{
1077 struct ftrace_page *pg;
1078 struct dyn_ftrace *rec;
41c52c0d 1079 unsigned long type = enable ? FTRACE_FL_FILTER : FTRACE_FL_NOTRACE;
5072c59f
SR
1080 unsigned i;
1081
99ecdc43
SR
1082 /* should not be called from interrupt context */
1083 spin_lock(&ftrace_lock);
41c52c0d
SR
1084 if (enable)
1085 ftrace_filtered = 0;
5072c59f
SR
1086 pg = ftrace_pages_start;
1087 while (pg) {
1088 for (i = 0; i < pg->index; i++) {
1089 rec = &pg->records[i];
1090 if (rec->flags & FTRACE_FL_FAILED)
1091 continue;
41c52c0d 1092 rec->flags &= ~type;
5072c59f
SR
1093 }
1094 pg = pg->next;
1095 }
99ecdc43 1096 spin_unlock(&ftrace_lock);
5072c59f
SR
1097}
1098
e309b41d 1099static int
41c52c0d 1100ftrace_regex_open(struct inode *inode, struct file *file, int enable)
5072c59f
SR
1101{
1102 struct ftrace_iterator *iter;
1103 int ret = 0;
1104
4eebcc81
SR
1105 if (unlikely(ftrace_disabled))
1106 return -ENODEV;
1107
5072c59f
SR
1108 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
1109 if (!iter)
1110 return -ENOMEM;
1111
41c52c0d 1112 mutex_lock(&ftrace_regex_lock);
5072c59f
SR
1113 if ((file->f_mode & FMODE_WRITE) &&
1114 !(file->f_flags & O_APPEND))
41c52c0d 1115 ftrace_filter_reset(enable);
5072c59f
SR
1116
1117 if (file->f_mode & FMODE_READ) {
1118 iter->pg = ftrace_pages_start;
1119 iter->pos = -1;
41c52c0d
SR
1120 iter->flags = enable ? FTRACE_ITER_FILTER :
1121 FTRACE_ITER_NOTRACE;
5072c59f
SR
1122
1123 ret = seq_open(file, &show_ftrace_seq_ops);
1124 if (!ret) {
1125 struct seq_file *m = file->private_data;
1126 m->private = iter;
1127 } else
1128 kfree(iter);
1129 } else
1130 file->private_data = iter;
41c52c0d 1131 mutex_unlock(&ftrace_regex_lock);
5072c59f
SR
1132
1133 return ret;
1134}
1135
41c52c0d
SR
1136static int
1137ftrace_filter_open(struct inode *inode, struct file *file)
1138{
1139 return ftrace_regex_open(inode, file, 1);
1140}
1141
1142static int
1143ftrace_notrace_open(struct inode *inode, struct file *file)
1144{
1145 return ftrace_regex_open(inode, file, 0);
1146}
1147
e309b41d 1148static ssize_t
41c52c0d 1149ftrace_regex_read(struct file *file, char __user *ubuf,
5072c59f
SR
1150 size_t cnt, loff_t *ppos)
1151{
1152 if (file->f_mode & FMODE_READ)
1153 return seq_read(file, ubuf, cnt, ppos);
1154 else
1155 return -EPERM;
1156}
1157
e309b41d 1158static loff_t
41c52c0d 1159ftrace_regex_lseek(struct file *file, loff_t offset, int origin)
5072c59f
SR
1160{
1161 loff_t ret;
1162
1163 if (file->f_mode & FMODE_READ)
1164 ret = seq_lseek(file, offset, origin);
1165 else
1166 file->f_pos = ret = 1;
1167
1168 return ret;
1169}
1170
1171enum {
1172 MATCH_FULL,
1173 MATCH_FRONT_ONLY,
1174 MATCH_MIDDLE_ONLY,
1175 MATCH_END_ONLY,
1176};
1177
e309b41d 1178static void
41c52c0d 1179ftrace_match(unsigned char *buff, int len, int enable)
5072c59f
SR
1180{
1181 char str[KSYM_SYMBOL_LEN];
1182 char *search = NULL;
1183 struct ftrace_page *pg;
1184 struct dyn_ftrace *rec;
1185 int type = MATCH_FULL;
41c52c0d 1186 unsigned long flag = enable ? FTRACE_FL_FILTER : FTRACE_FL_NOTRACE;
5072c59f
SR
1187 unsigned i, match = 0, search_len = 0;
1188
1189 for (i = 0; i < len; i++) {
1190 if (buff[i] == '*') {
1191 if (!i) {
1192 search = buff + i + 1;
1193 type = MATCH_END_ONLY;
1194 search_len = len - (i + 1);
1195 } else {
1196 if (type == MATCH_END_ONLY) {
1197 type = MATCH_MIDDLE_ONLY;
1198 } else {
1199 match = i;
1200 type = MATCH_FRONT_ONLY;
1201 }
1202 buff[i] = 0;
1203 break;
1204 }
1205 }
1206 }
1207
99ecdc43
SR
1208 /* should not be called from interrupt context */
1209 spin_lock(&ftrace_lock);
41c52c0d
SR
1210 if (enable)
1211 ftrace_filtered = 1;
5072c59f
SR
1212 pg = ftrace_pages_start;
1213 while (pg) {
1214 for (i = 0; i < pg->index; i++) {
1215 int matched = 0;
1216 char *ptr;
1217
1218 rec = &pg->records[i];
1219 if (rec->flags & FTRACE_FL_FAILED)
1220 continue;
1221 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
1222 switch (type) {
1223 case MATCH_FULL:
1224 if (strcmp(str, buff) == 0)
1225 matched = 1;
1226 break;
1227 case MATCH_FRONT_ONLY:
1228 if (memcmp(str, buff, match) == 0)
1229 matched = 1;
1230 break;
1231 case MATCH_MIDDLE_ONLY:
1232 if (strstr(str, search))
1233 matched = 1;
1234 break;
1235 case MATCH_END_ONLY:
1236 ptr = strstr(str, search);
1237 if (ptr && (ptr[search_len] == 0))
1238 matched = 1;
1239 break;
1240 }
1241 if (matched)
41c52c0d 1242 rec->flags |= flag;
5072c59f
SR
1243 }
1244 pg = pg->next;
1245 }
99ecdc43 1246 spin_unlock(&ftrace_lock);
5072c59f
SR
1247}
1248
e309b41d 1249static ssize_t
41c52c0d
SR
1250ftrace_regex_write(struct file *file, const char __user *ubuf,
1251 size_t cnt, loff_t *ppos, int enable)
5072c59f
SR
1252{
1253 struct ftrace_iterator *iter;
1254 char ch;
1255 size_t read = 0;
1256 ssize_t ret;
1257
1258 if (!cnt || cnt < 0)
1259 return 0;
1260
41c52c0d 1261 mutex_lock(&ftrace_regex_lock);
5072c59f
SR
1262
1263 if (file->f_mode & FMODE_READ) {
1264 struct seq_file *m = file->private_data;
1265 iter = m->private;
1266 } else
1267 iter = file->private_data;
1268
1269 if (!*ppos) {
1270 iter->flags &= ~FTRACE_ITER_CONT;
1271 iter->buffer_idx = 0;
1272 }
1273
1274 ret = get_user(ch, ubuf++);
1275 if (ret)
1276 goto out;
1277 read++;
1278 cnt--;
1279
1280 if (!(iter->flags & ~FTRACE_ITER_CONT)) {
1281 /* skip white space */
1282 while (cnt && isspace(ch)) {
1283 ret = get_user(ch, ubuf++);
1284 if (ret)
1285 goto out;
1286 read++;
1287 cnt--;
1288 }
1289
5072c59f
SR
1290 if (isspace(ch)) {
1291 file->f_pos += read;
1292 ret = read;
1293 goto out;
1294 }
1295
1296 iter->buffer_idx = 0;
1297 }
1298
1299 while (cnt && !isspace(ch)) {
1300 if (iter->buffer_idx < FTRACE_BUFF_MAX)
1301 iter->buffer[iter->buffer_idx++] = ch;
1302 else {
1303 ret = -EINVAL;
1304 goto out;
1305 }
1306 ret = get_user(ch, ubuf++);
1307 if (ret)
1308 goto out;
1309 read++;
1310 cnt--;
1311 }
1312
1313 if (isspace(ch)) {
1314 iter->filtered++;
1315 iter->buffer[iter->buffer_idx] = 0;
41c52c0d 1316 ftrace_match(iter->buffer, iter->buffer_idx, enable);
5072c59f
SR
1317 iter->buffer_idx = 0;
1318 } else
1319 iter->flags |= FTRACE_ITER_CONT;
1320
1321
1322 file->f_pos += read;
1323
1324 ret = read;
1325 out:
41c52c0d 1326 mutex_unlock(&ftrace_regex_lock);
5072c59f
SR
1327
1328 return ret;
1329}
1330
41c52c0d
SR
1331static ssize_t
1332ftrace_filter_write(struct file *file, const char __user *ubuf,
1333 size_t cnt, loff_t *ppos)
1334{
1335 return ftrace_regex_write(file, ubuf, cnt, ppos, 1);
1336}
1337
1338static ssize_t
1339ftrace_notrace_write(struct file *file, const char __user *ubuf,
1340 size_t cnt, loff_t *ppos)
1341{
1342 return ftrace_regex_write(file, ubuf, cnt, ppos, 0);
1343}
1344
1345static void
1346ftrace_set_regex(unsigned char *buf, int len, int reset, int enable)
1347{
1348 if (unlikely(ftrace_disabled))
1349 return;
1350
1351 mutex_lock(&ftrace_regex_lock);
1352 if (reset)
1353 ftrace_filter_reset(enable);
1354 if (buf)
1355 ftrace_match(buf, len, enable);
1356 mutex_unlock(&ftrace_regex_lock);
1357}
1358
77a2b37d
SR
1359/**
1360 * ftrace_set_filter - set a function to filter on in ftrace
1361 * @buf - the string that holds the function filter text.
1362 * @len - the length of the string.
1363 * @reset - non zero to reset all filters before applying this filter.
1364 *
1365 * Filters denote which functions should be enabled when tracing is enabled.
1366 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
1367 */
e309b41d 1368void ftrace_set_filter(unsigned char *buf, int len, int reset)
77a2b37d 1369{
41c52c0d
SR
1370 ftrace_set_regex(buf, len, reset, 1);
1371}
4eebcc81 1372
41c52c0d
SR
1373/**
1374 * ftrace_set_notrace - set a function to not trace in ftrace
1375 * @buf - the string that holds the function notrace text.
1376 * @len - the length of the string.
1377 * @reset - non zero to reset all filters before applying this filter.
1378 *
1379 * Notrace Filters denote which functions should not be enabled when tracing
1380 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
1381 * for tracing.
1382 */
1383void ftrace_set_notrace(unsigned char *buf, int len, int reset)
1384{
1385 ftrace_set_regex(buf, len, reset, 0);
77a2b37d
SR
1386}
1387
e309b41d 1388static int
41c52c0d 1389ftrace_regex_release(struct inode *inode, struct file *file, int enable)
5072c59f
SR
1390{
1391 struct seq_file *m = (struct seq_file *)file->private_data;
1392 struct ftrace_iterator *iter;
1393
41c52c0d 1394 mutex_lock(&ftrace_regex_lock);
5072c59f
SR
1395 if (file->f_mode & FMODE_READ) {
1396 iter = m->private;
1397
1398 seq_release(inode, file);
1399 } else
1400 iter = file->private_data;
1401
1402 if (iter->buffer_idx) {
1403 iter->filtered++;
1404 iter->buffer[iter->buffer_idx] = 0;
41c52c0d 1405 ftrace_match(iter->buffer, iter->buffer_idx, enable);
5072c59f
SR
1406 }
1407
1408 mutex_lock(&ftrace_sysctl_lock);
1409 mutex_lock(&ftraced_lock);
1410 if (iter->filtered && ftraced_suspend && ftrace_enabled)
1411 ftrace_run_update_code(FTRACE_ENABLE_CALLS);
1412 mutex_unlock(&ftraced_lock);
1413 mutex_unlock(&ftrace_sysctl_lock);
1414
1415 kfree(iter);
41c52c0d 1416 mutex_unlock(&ftrace_regex_lock);
5072c59f
SR
1417 return 0;
1418}
1419
41c52c0d
SR
1420static int
1421ftrace_filter_release(struct inode *inode, struct file *file)
1422{
1423 return ftrace_regex_release(inode, file, 1);
1424}
1425
1426static int
1427ftrace_notrace_release(struct inode *inode, struct file *file)
1428{
1429 return ftrace_regex_release(inode, file, 0);
1430}
1431
ad90c0e3
SR
1432static ssize_t
1433ftraced_read(struct file *filp, char __user *ubuf,
1434 size_t cnt, loff_t *ppos)
1435{
1436 /* don't worry about races */
1437 char *buf = ftraced_stop ? "disabled\n" : "enabled\n";
1438 int r = strlen(buf);
1439
1440 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
1441}
1442
1443static ssize_t
1444ftraced_write(struct file *filp, const char __user *ubuf,
1445 size_t cnt, loff_t *ppos)
1446{
1447 char buf[64];
1448 long val;
1449 int ret;
1450
1451 if (cnt >= sizeof(buf))
1452 return -EINVAL;
1453
1454 if (copy_from_user(&buf, ubuf, cnt))
1455 return -EFAULT;
1456
1457 if (strncmp(buf, "enable", 6) == 0)
1458 val = 1;
1459 else if (strncmp(buf, "disable", 7) == 0)
1460 val = 0;
1461 else {
1462 buf[cnt] = 0;
1463
1464 ret = strict_strtoul(buf, 10, &val);
1465 if (ret < 0)
1466 return ret;
1467
1468 val = !!val;
1469 }
1470
1471 if (val)
1472 ftrace_enable_daemon();
1473 else
1474 ftrace_disable_daemon();
1475
1476 filp->f_pos += cnt;
1477
1478 return cnt;
1479}
1480
5072c59f
SR
1481static struct file_operations ftrace_avail_fops = {
1482 .open = ftrace_avail_open,
1483 .read = seq_read,
1484 .llseek = seq_lseek,
1485 .release = ftrace_avail_release,
1486};
1487
eb9a7bf0
AS
1488static struct file_operations ftrace_failures_fops = {
1489 .open = ftrace_failures_open,
1490 .read = seq_read,
1491 .llseek = seq_lseek,
1492 .release = ftrace_avail_release,
1493};
1494
5072c59f
SR
1495static struct file_operations ftrace_filter_fops = {
1496 .open = ftrace_filter_open,
41c52c0d 1497 .read = ftrace_regex_read,
5072c59f 1498 .write = ftrace_filter_write,
41c52c0d 1499 .llseek = ftrace_regex_lseek,
5072c59f
SR
1500 .release = ftrace_filter_release,
1501};
1502
41c52c0d
SR
1503static struct file_operations ftrace_notrace_fops = {
1504 .open = ftrace_notrace_open,
1505 .read = ftrace_regex_read,
1506 .write = ftrace_notrace_write,
1507 .llseek = ftrace_regex_lseek,
1508 .release = ftrace_notrace_release,
1509};
1510
ad90c0e3
SR
1511static struct file_operations ftraced_fops = {
1512 .open = tracing_open_generic,
1513 .read = ftraced_read,
1514 .write = ftraced_write,
1515};
1516
e1c08bdd
SR
1517/**
1518 * ftrace_force_update - force an update to all recording ftrace functions
e1c08bdd
SR
1519 */
1520int ftrace_force_update(void)
1521{
e1c08bdd
SR
1522 int ret = 0;
1523
4eebcc81 1524 if (unlikely(ftrace_disabled))
e1c08bdd
SR
1525 return -ENODEV;
1526
ad90c0e3 1527 mutex_lock(&ftrace_sysctl_lock);
e1c08bdd 1528 mutex_lock(&ftraced_lock);
e1c08bdd 1529
ad90c0e3
SR
1530 /*
1531 * If ftraced_trigger is not set, then there is nothing
1532 * to update.
1533 */
1534 if (ftraced_trigger && !ftrace_update_code())
1535 ret = -EBUSY;
e1c08bdd
SR
1536
1537 mutex_unlock(&ftraced_lock);
ad90c0e3 1538 mutex_unlock(&ftrace_sysctl_lock);
e1c08bdd
SR
1539
1540 return ret;
1541}
1542
4eebcc81
SR
1543static void ftrace_force_shutdown(void)
1544{
1545 struct task_struct *task;
1546 int command = FTRACE_DISABLE_CALLS | FTRACE_UPDATE_TRACE_FUNC;
1547
1548 mutex_lock(&ftraced_lock);
1549 task = ftraced_task;
1550 ftraced_task = NULL;
1551 ftraced_suspend = -1;
1552 ftrace_run_update_code(command);
1553 mutex_unlock(&ftraced_lock);
1554
1555 if (task)
1556 kthread_stop(task);
1557}
1558
5072c59f
SR
1559static __init int ftrace_init_debugfs(void)
1560{
1561 struct dentry *d_tracer;
1562 struct dentry *entry;
1563
1564 d_tracer = tracing_init_dentry();
1565
1566 entry = debugfs_create_file("available_filter_functions", 0444,
1567 d_tracer, NULL, &ftrace_avail_fops);
1568 if (!entry)
1569 pr_warning("Could not create debugfs "
1570 "'available_filter_functions' entry\n");
1571
eb9a7bf0
AS
1572 entry = debugfs_create_file("failures", 0444,
1573 d_tracer, NULL, &ftrace_failures_fops);
1574 if (!entry)
1575 pr_warning("Could not create debugfs 'failures' entry\n");
1576
5072c59f
SR
1577 entry = debugfs_create_file("set_ftrace_filter", 0644, d_tracer,
1578 NULL, &ftrace_filter_fops);
1579 if (!entry)
1580 pr_warning("Could not create debugfs "
1581 "'set_ftrace_filter' entry\n");
41c52c0d
SR
1582
1583 entry = debugfs_create_file("set_ftrace_notrace", 0644, d_tracer,
1584 NULL, &ftrace_notrace_fops);
1585 if (!entry)
1586 pr_warning("Could not create debugfs "
1587 "'set_ftrace_notrace' entry\n");
ad90c0e3
SR
1588
1589 entry = debugfs_create_file("ftraced_enabled", 0644, d_tracer,
1590 NULL, &ftraced_fops);
1591 if (!entry)
1592 pr_warning("Could not create debugfs "
1593 "'ftraced_enabled' entry\n");
5072c59f
SR
1594 return 0;
1595}
1596
1597fs_initcall(ftrace_init_debugfs);
1598
68bf21aa
SR
1599#ifdef CONFIG_FTRACE_MCOUNT_RECORD
1600static int ftrace_convert_nops(unsigned long *start,
1601 unsigned long *end)
1602{
1603 unsigned long *p;
1604 unsigned long addr;
1605 unsigned long flags;
1606
1607 p = start;
1608 while (p < end) {
1609 addr = ftrace_call_adjust(*p++);
99ecdc43 1610 /* should not be called from interrupt context */
fed1939c 1611 spin_lock(&ftrace_lock);
68bf21aa 1612 ftrace_record_ip(addr);
fed1939c 1613 spin_unlock(&ftrace_lock);
68bf21aa
SR
1614 ftrace_shutdown_replenish();
1615 }
1616
1617 /* p is ignored */
1618 local_irq_save(flags);
1619 __ftrace_update_code(p);
1620 local_irq_restore(flags);
1621
1622 return 0;
1623}
1624
90d595fe
SR
1625void ftrace_init_module(unsigned long *start, unsigned long *end)
1626{
00fd61ae 1627 if (ftrace_disabled || start == end)
fed1939c 1628 return;
90d595fe
SR
1629 ftrace_convert_nops(start, end);
1630}
1631
68bf21aa
SR
1632extern unsigned long __start_mcount_loc[];
1633extern unsigned long __stop_mcount_loc[];
1634
1635void __init ftrace_init(void)
1636{
1637 unsigned long count, addr, flags;
1638 int ret;
1639
1640 /* Keep the ftrace pointer to the stub */
1641 addr = (unsigned long)ftrace_stub;
1642
1643 local_irq_save(flags);
1644 ftrace_dyn_arch_init(&addr);
1645 local_irq_restore(flags);
1646
1647 /* ftrace_dyn_arch_init places the return code in addr */
1648 if (addr)
1649 goto failed;
1650
1651 count = __stop_mcount_loc - __start_mcount_loc;
1652
1653 ret = ftrace_dyn_table_alloc(count);
1654 if (ret)
1655 goto failed;
1656
1657 last_ftrace_enabled = ftrace_enabled = 1;
1658
1659 ret = ftrace_convert_nops(__start_mcount_loc,
1660 __stop_mcount_loc);
1661
1662 return;
1663 failed:
1664 ftrace_disabled = 1;
1665}
1666#else /* CONFIG_FTRACE_MCOUNT_RECORD */
bd95b88d
SR
1667
1668static void ftrace_release_hash(unsigned long start, unsigned long end)
1669{
1670 struct dyn_ftrace *rec;
1671 struct hlist_node *t, *n;
1672 struct hlist_head *head, temp_list;
1673 unsigned long flags;
1674 int i, cpu;
1675
1676 preempt_disable_notrace();
1677
1678 /* disable incase we call something that calls mcount */
1679 cpu = raw_smp_processor_id();
1680 per_cpu(ftrace_shutdown_disable_cpu, cpu)++;
1681
1682 ftrace_hash_lock(flags);
1683
1684 for (i = 0; i < FTRACE_HASHSIZE; i++) {
1685 INIT_HLIST_HEAD(&temp_list);
1686 head = &ftrace_hash[i];
1687
1688 /* all CPUS are stopped, we are safe to modify code */
1689 hlist_for_each_entry_safe(rec, t, n, head, node) {
1690 if (rec->flags & FTRACE_FL_FREE)
1691 continue;
1692
1693 if ((rec->ip >= start) && (rec->ip < end))
1694 ftrace_free_rec(rec);
1695 }
1696 }
1697
1698 ftrace_hash_unlock(flags);
1699
1700 per_cpu(ftrace_shutdown_disable_cpu, cpu)--;
1701 preempt_enable_notrace();
1702
1703}
1704
68bf21aa
SR
1705static int ftraced(void *ignore)
1706{
1707 unsigned long usecs;
1708
1709 while (!kthread_should_stop()) {
1710
1711 set_current_state(TASK_INTERRUPTIBLE);
1712
1713 /* check once a second */
1714 schedule_timeout(HZ);
1715
1716 if (unlikely(ftrace_disabled))
1717 continue;
1718
1719 mutex_lock(&ftrace_sysctl_lock);
1720 mutex_lock(&ftraced_lock);
1721 if (!ftraced_suspend && !ftraced_stop &&
1722 ftrace_update_code()) {
1723 usecs = nsecs_to_usecs(ftrace_update_time);
1724 if (ftrace_update_tot_cnt > 100000) {
1725 ftrace_update_tot_cnt = 0;
1726 pr_info("hm, dftrace overflow: %lu change%s"
1727 " (%lu total) in %lu usec%s\n",
1728 ftrace_update_cnt,
1729 ftrace_update_cnt != 1 ? "s" : "",
1730 ftrace_update_tot_cnt,
1731 usecs, usecs != 1 ? "s" : "");
1732 ftrace_disabled = 1;
1733 WARN_ON_ONCE(1);
1734 }
1735 }
1736 mutex_unlock(&ftraced_lock);
1737 mutex_unlock(&ftrace_sysctl_lock);
1738
1739 ftrace_shutdown_replenish();
1740 }
1741 __set_current_state(TASK_RUNNING);
1742 return 0;
1743}
1744
e309b41d 1745static int __init ftrace_dynamic_init(void)
3d083395
SR
1746{
1747 struct task_struct *p;
d61f82d0 1748 unsigned long addr;
3d083395
SR
1749 int ret;
1750
d61f82d0 1751 addr = (unsigned long)ftrace_record_ip;
9ff9cdb2 1752
784e2d76 1753 stop_machine(ftrace_dyn_arch_init, &addr, NULL);
d61f82d0
SR
1754
1755 /* ftrace_dyn_arch_init places the return code in addr */
4eebcc81
SR
1756 if (addr) {
1757 ret = (int)addr;
1758 goto failed;
1759 }
d61f82d0 1760
68bf21aa 1761 ret = ftrace_dyn_table_alloc(NR_TO_INIT);
3d083395 1762 if (ret)
4eebcc81 1763 goto failed;
3d083395
SR
1764
1765 p = kthread_run(ftraced, NULL, "ftraced");
4eebcc81
SR
1766 if (IS_ERR(p)) {
1767 ret = -1;
1768 goto failed;
1769 }
3d083395 1770
d61f82d0 1771 last_ftrace_enabled = ftrace_enabled = 1;
e1c08bdd 1772 ftraced_task = p;
3d083395
SR
1773
1774 return 0;
4eebcc81
SR
1775
1776 failed:
1777 ftrace_disabled = 1;
1778 return ret;
3d083395
SR
1779}
1780
d61f82d0 1781core_initcall(ftrace_dynamic_init);
68bf21aa
SR
1782#endif /* CONFIG_FTRACE_MCOUNT_RECORD */
1783
3d083395 1784#else
c7aafc54
IM
1785# define ftrace_startup() do { } while (0)
1786# define ftrace_shutdown() do { } while (0)
1787# define ftrace_startup_sysctl() do { } while (0)
1788# define ftrace_shutdown_sysctl() do { } while (0)
4eebcc81 1789# define ftrace_force_shutdown() do { } while (0)
3d083395
SR
1790#endif /* CONFIG_DYNAMIC_FTRACE */
1791
a2bb6a3d
SR
1792/**
1793 * ftrace_kill_atomic - kill ftrace from critical sections
1794 *
1795 * This function should be used by panic code. It stops ftrace
1796 * but in a not so nice way. If you need to simply kill ftrace
1797 * from a non-atomic section, use ftrace_kill.
1798 */
1799void ftrace_kill_atomic(void)
1800{
1801 ftrace_disabled = 1;
1802 ftrace_enabled = 0;
b2613e37 1803#ifdef CONFIG_DYNAMIC_FTRACE
a2bb6a3d 1804 ftraced_suspend = -1;
b2613e37 1805#endif
a2bb6a3d
SR
1806 clear_ftrace_function();
1807}
1808
4eebcc81
SR
1809/**
1810 * ftrace_kill - totally shutdown ftrace
1811 *
1812 * This is a safety measure. If something was detected that seems
1813 * wrong, calling this function will keep ftrace from doing
1814 * any more modifications, and updates.
1815 * used when something went wrong.
1816 */
1817void ftrace_kill(void)
1818{
1819 mutex_lock(&ftrace_sysctl_lock);
1820 ftrace_disabled = 1;
1821 ftrace_enabled = 0;
1822
1823 clear_ftrace_function();
1824 mutex_unlock(&ftrace_sysctl_lock);
1825
1826 /* Try to totally disable ftrace */
1827 ftrace_force_shutdown();
1828}
1829
16444a8a 1830/**
3d083395
SR
1831 * register_ftrace_function - register a function for profiling
1832 * @ops - ops structure that holds the function for profiling.
16444a8a 1833 *
3d083395
SR
1834 * Register a function to be called by all functions in the
1835 * kernel.
1836 *
1837 * Note: @ops->func and all the functions it calls must be labeled
1838 * with "notrace", otherwise it will go into a
1839 * recursive loop.
16444a8a 1840 */
3d083395 1841int register_ftrace_function(struct ftrace_ops *ops)
16444a8a 1842{
b0fc494f
SR
1843 int ret;
1844
4eebcc81
SR
1845 if (unlikely(ftrace_disabled))
1846 return -1;
1847
b0fc494f 1848 mutex_lock(&ftrace_sysctl_lock);
b0fc494f 1849 ret = __register_ftrace_function(ops);
d61f82d0 1850 ftrace_startup();
b0fc494f
SR
1851 mutex_unlock(&ftrace_sysctl_lock);
1852
1853 return ret;
3d083395
SR
1854}
1855
1856/**
1857 * unregister_ftrace_function - unresgister a function for profiling.
1858 * @ops - ops structure that holds the function to unregister
1859 *
1860 * Unregister a function that was added to be called by ftrace profiling.
1861 */
1862int unregister_ftrace_function(struct ftrace_ops *ops)
1863{
1864 int ret;
1865
b0fc494f 1866 mutex_lock(&ftrace_sysctl_lock);
3d083395 1867 ret = __unregister_ftrace_function(ops);
d61f82d0 1868 ftrace_shutdown();
b0fc494f
SR
1869 mutex_unlock(&ftrace_sysctl_lock);
1870
1871 return ret;
1872}
1873
e309b41d 1874int
b0fc494f 1875ftrace_enable_sysctl(struct ctl_table *table, int write,
5072c59f 1876 struct file *file, void __user *buffer, size_t *lenp,
b0fc494f
SR
1877 loff_t *ppos)
1878{
1879 int ret;
1880
4eebcc81
SR
1881 if (unlikely(ftrace_disabled))
1882 return -ENODEV;
1883
b0fc494f
SR
1884 mutex_lock(&ftrace_sysctl_lock);
1885
5072c59f 1886 ret = proc_dointvec(table, write, file, buffer, lenp, ppos);
b0fc494f
SR
1887
1888 if (ret || !write || (last_ftrace_enabled == ftrace_enabled))
1889 goto out;
1890
1891 last_ftrace_enabled = ftrace_enabled;
1892
1893 if (ftrace_enabled) {
1894
1895 ftrace_startup_sysctl();
1896
1897 /* we are starting ftrace again */
1898 if (ftrace_list != &ftrace_list_end) {
1899 if (ftrace_list->next == &ftrace_list_end)
1900 ftrace_trace_function = ftrace_list->func;
1901 else
1902 ftrace_trace_function = ftrace_list_func;
1903 }
1904
1905 } else {
1906 /* stopping ftrace calls (just send to ftrace_stub) */
1907 ftrace_trace_function = ftrace_stub;
1908
1909 ftrace_shutdown_sysctl();
1910 }
1911
1912 out:
1913 mutex_unlock(&ftrace_sysctl_lock);
3d083395 1914 return ret;
16444a8a 1915}