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