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
16 #include <linux/stop_machine.h>
17 #include <linux/clocksource.h>
18 #include <linux/kallsyms.h>
19 #include <linux/seq_file.h>
20 #include <linux/debugfs.h>
21 #include <linux/hardirq.h>
22 #include <linux/kthread.h>
23 #include <linux/uaccess.h>
24 #include <linux/ftrace.h>
25 #include <linux/sysctl.h>
26 #include <linux/ctype.h>
27 #include <linux/hash.h>
28 #include <linux/list.h>
29
30 #include "trace.h"
31
32 /* ftrace_enabled is a method to turn ftrace on or off */
33 int ftrace_enabled __read_mostly;
34 static int last_ftrace_enabled;
35
36 /*
37 * ftrace_disabled is set when an anomaly is discovered.
38 * ftrace_disabled is much stronger than ftrace_enabled.
39 */
40 static int ftrace_disabled __read_mostly;
41
42 static DEFINE_RAW_SPINLOCK(ftrace_lock);
43 static DEFINE_MUTEX(ftrace_sysctl_lock);
44
45 static struct ftrace_ops ftrace_list_end __read_mostly =
46 {
47 .func = ftrace_stub,
48 };
49
50 static struct ftrace_ops *ftrace_list __read_mostly = &ftrace_list_end;
51 ftrace_func_t ftrace_trace_function __read_mostly = ftrace_stub;
52
53 void ftrace_list_func(unsigned long ip, unsigned long parent_ip)
54 {
55 struct ftrace_ops *op = ftrace_list;
56
57 /* in case someone actually ports this to alpha! */
58 read_barrier_depends();
59
60 while (op != &ftrace_list_end) {
61 /* silly alpha */
62 read_barrier_depends();
63 op->func(ip, parent_ip);
64 op = op->next;
65 };
66 }
67
68 /**
69 * clear_ftrace_function - reset the ftrace function
70 *
71 * This NULLs the ftrace function and in essence stops
72 * tracing. There may be lag
73 */
74 void clear_ftrace_function(void)
75 {
76 ftrace_trace_function = ftrace_stub;
77 }
78
79 static int __register_ftrace_function(struct ftrace_ops *ops)
80 {
81 /* Should never be called by interrupts */
82 spin_lock(&ftrace_lock);
83
84 ops->next = ftrace_list;
85 /*
86 * We are entering ops into the ftrace_list but another
87 * CPU might be walking that list. We need to make sure
88 * the ops->next pointer is valid before another CPU sees
89 * the ops pointer included into the ftrace_list.
90 */
91 smp_wmb();
92 ftrace_list = ops;
93
94 if (ftrace_enabled) {
95 /*
96 * For one func, simply call it directly.
97 * For more than one func, call the chain.
98 */
99 if (ops->next == &ftrace_list_end)
100 ftrace_trace_function = ops->func;
101 else
102 ftrace_trace_function = ftrace_list_func;
103 }
104
105 spin_unlock(&ftrace_lock);
106
107 return 0;
108 }
109
110 static int __unregister_ftrace_function(struct ftrace_ops *ops)
111 {
112 struct ftrace_ops **p;
113 int ret = 0;
114
115 spin_lock(&ftrace_lock);
116
117 /*
118 * If we are removing the last function, then simply point
119 * to the ftrace_stub.
120 */
121 if (ftrace_list == ops && ops->next == &ftrace_list_end) {
122 ftrace_trace_function = ftrace_stub;
123 ftrace_list = &ftrace_list_end;
124 goto out;
125 }
126
127 for (p = &ftrace_list; *p != &ftrace_list_end; p = &(*p)->next)
128 if (*p == ops)
129 break;
130
131 if (*p != ops) {
132 ret = -1;
133 goto out;
134 }
135
136 *p = (*p)->next;
137
138 if (ftrace_enabled) {
139 /* If we only have one func left, then call that directly */
140 if (ftrace_list == &ftrace_list_end ||
141 ftrace_list->next == &ftrace_list_end)
142 ftrace_trace_function = ftrace_list->func;
143 }
144
145 out:
146 spin_unlock(&ftrace_lock);
147
148 return ret;
149 }
150
151 static int ftrace_disabled_count;
152 static int save_ftrace_enabled;
153
154 void ftrace_disable(void)
155 {
156 mutex_lock(&ftrace_sysctl_lock);
157
158 save_ftrace_enabled = ftrace_enabled;
159 ftrace_enabled = 0;
160 }
161
162 void ftrace_enable(void)
163 {
164 /* ftrace_enable must be paired with ftrace_disable */
165 if (!mutex_is_locked(&ftrace_sysctl_lock)) {
166 WARN_ON(1);
167 return;
168 }
169
170 ftrace_enabled = save_ftrace_enabled;
171
172 mutex_unlock(&ftrace_sysctl_lock);
173 }
174
175 #ifdef CONFIG_DYNAMIC_FTRACE
176
177 static struct task_struct *ftraced_task;
178
179 enum {
180 FTRACE_ENABLE_CALLS = (1 << 0),
181 FTRACE_DISABLE_CALLS = (1 << 1),
182 FTRACE_UPDATE_TRACE_FUNC = (1 << 2),
183 FTRACE_ENABLE_MCOUNT = (1 << 3),
184 FTRACE_DISABLE_MCOUNT = (1 << 4),
185 };
186
187 static int ftrace_filtered;
188
189 static struct hlist_head ftrace_hash[FTRACE_HASHSIZE];
190
191 static DEFINE_PER_CPU(int, ftrace_shutdown_disable_cpu);
192
193 static DEFINE_RAW_SPINLOCK(ftrace_shutdown_lock);
194 static DEFINE_MUTEX(ftraced_lock);
195 static DEFINE_MUTEX(ftrace_regex_lock);
196
197 struct ftrace_page {
198 struct ftrace_page *next;
199 unsigned long index;
200 struct dyn_ftrace records[];
201 };
202
203 #define ENTRIES_PER_PAGE \
204 ((PAGE_SIZE - sizeof(struct ftrace_page)) / sizeof(struct dyn_ftrace))
205
206 /* estimate from running different kernels */
207 #define NR_TO_INIT 10000
208
209 static struct ftrace_page *ftrace_pages_start;
210 static struct ftrace_page *ftrace_pages;
211
212 static int ftraced_trigger;
213 static int ftraced_suspend;
214 static int ftraced_stop;
215
216 static int ftrace_record_suspend;
217
218 static struct dyn_ftrace *ftrace_free_records;
219
220 static inline int
221 ftrace_ip_in_hash(unsigned long ip, unsigned long key)
222 {
223 struct dyn_ftrace *p;
224 struct hlist_node *t;
225 int found = 0;
226
227 hlist_for_each_entry_rcu(p, t, &ftrace_hash[key], node) {
228 if (p->ip == ip) {
229 found = 1;
230 break;
231 }
232 }
233
234 return found;
235 }
236
237 static inline void
238 ftrace_add_hash(struct dyn_ftrace *node, unsigned long key)
239 {
240 hlist_add_head_rcu(&node->node, &ftrace_hash[key]);
241 }
242
243 static void ftrace_free_rec(struct dyn_ftrace *rec)
244 {
245 /* no locking, only called from kstop_machine */
246
247 rec->ip = (unsigned long)ftrace_free_records;
248 ftrace_free_records = rec;
249 rec->flags |= FTRACE_FL_FREE;
250 }
251
252 static struct dyn_ftrace *ftrace_alloc_dyn_node(unsigned long ip)
253 {
254 struct dyn_ftrace *rec;
255
256 /* First check for freed records */
257 if (ftrace_free_records) {
258 rec = ftrace_free_records;
259
260 if (unlikely(!(rec->flags & FTRACE_FL_FREE))) {
261 WARN_ON_ONCE(1);
262 ftrace_free_records = NULL;
263 ftrace_disabled = 1;
264 ftrace_enabled = 0;
265 return NULL;
266 }
267
268 ftrace_free_records = (void *)rec->ip;
269 memset(rec, 0, sizeof(*rec));
270 return rec;
271 }
272
273 if (ftrace_pages->index == ENTRIES_PER_PAGE) {
274 if (!ftrace_pages->next)
275 return NULL;
276 ftrace_pages = ftrace_pages->next;
277 }
278
279 return &ftrace_pages->records[ftrace_pages->index++];
280 }
281
282 static void
283 ftrace_record_ip(unsigned long ip)
284 {
285 struct dyn_ftrace *node;
286 unsigned long flags;
287 unsigned long key;
288 int resched;
289 int atomic;
290 int cpu;
291
292 if (!ftrace_enabled || ftrace_disabled)
293 return;
294
295 resched = need_resched();
296 preempt_disable_notrace();
297
298 /*
299 * We simply need to protect against recursion.
300 * Use the the raw version of smp_processor_id and not
301 * __get_cpu_var which can call debug hooks that can
302 * cause a recursive crash here.
303 */
304 cpu = raw_smp_processor_id();
305 per_cpu(ftrace_shutdown_disable_cpu, cpu)++;
306 if (per_cpu(ftrace_shutdown_disable_cpu, cpu) != 1)
307 goto out;
308
309 if (unlikely(ftrace_record_suspend))
310 goto out;
311
312 key = hash_long(ip, FTRACE_HASHBITS);
313
314 WARN_ON_ONCE(key >= FTRACE_HASHSIZE);
315
316 if (ftrace_ip_in_hash(ip, key))
317 goto out;
318
319 atomic = irqs_disabled();
320
321 spin_lock_irqsave(&ftrace_shutdown_lock, flags);
322
323 /* This ip may have hit the hash before the lock */
324 if (ftrace_ip_in_hash(ip, key))
325 goto out_unlock;
326
327 /*
328 * There's a slight race that the ftraced will update the
329 * hash and reset here. If it is already converted, skip it.
330 */
331 if (ftrace_ip_converted(ip))
332 goto out_unlock;
333
334 node = ftrace_alloc_dyn_node(ip);
335 if (!node)
336 goto out_unlock;
337
338 node->ip = ip;
339
340 ftrace_add_hash(node, key);
341
342 ftraced_trigger = 1;
343
344 out_unlock:
345 spin_unlock_irqrestore(&ftrace_shutdown_lock, flags);
346 out:
347 per_cpu(ftrace_shutdown_disable_cpu, cpu)--;
348
349 /* prevent recursion with scheduler */
350 if (resched)
351 preempt_enable_no_resched_notrace();
352 else
353 preempt_enable_notrace();
354 }
355
356 #define FTRACE_ADDR ((long)(ftrace_caller))
357 #define MCOUNT_ADDR ((long)(mcount))
358
359 static void
360 __ftrace_replace_code(struct dyn_ftrace *rec,
361 unsigned char *old, unsigned char *new, int enable)
362 {
363 unsigned long ip, fl;
364 int failed;
365
366 ip = rec->ip;
367
368 if (ftrace_filtered && enable) {
369 /*
370 * If filtering is on:
371 *
372 * If this record is set to be filtered and
373 * is enabled then do nothing.
374 *
375 * If this record is set to be filtered and
376 * it is not enabled, enable it.
377 *
378 * If this record is not set to be filtered
379 * and it is not enabled do nothing.
380 *
381 * If this record is set not to trace then
382 * do nothing.
383 *
384 * If this record is not set to be filtered and
385 * it is enabled, disable it.
386 */
387 fl = rec->flags & (FTRACE_FL_FILTER | FTRACE_FL_ENABLED);
388
389 if ((fl == (FTRACE_FL_FILTER | FTRACE_FL_ENABLED)) ||
390 (fl == 0) || (rec->flags & FTRACE_FL_NOTRACE))
391 return;
392
393 /*
394 * If it is enabled disable it,
395 * otherwise enable it!
396 */
397 if (fl == FTRACE_FL_ENABLED) {
398 /* swap new and old */
399 new = old;
400 old = ftrace_call_replace(ip, FTRACE_ADDR);
401 rec->flags &= ~FTRACE_FL_ENABLED;
402 } else {
403 new = ftrace_call_replace(ip, FTRACE_ADDR);
404 rec->flags |= FTRACE_FL_ENABLED;
405 }
406 } else {
407
408 if (enable) {
409 /*
410 * If this record is set not to trace and is
411 * not enabled, do nothing.
412 */
413 fl = rec->flags & (FTRACE_FL_NOTRACE | FTRACE_FL_ENABLED);
414 if (fl == FTRACE_FL_NOTRACE)
415 return;
416
417 new = ftrace_call_replace(ip, FTRACE_ADDR);
418 } else
419 old = ftrace_call_replace(ip, FTRACE_ADDR);
420
421 if (enable) {
422 if (rec->flags & FTRACE_FL_ENABLED)
423 return;
424 rec->flags |= FTRACE_FL_ENABLED;
425 } else {
426 if (!(rec->flags & FTRACE_FL_ENABLED))
427 return;
428 rec->flags &= ~FTRACE_FL_ENABLED;
429 }
430 }
431
432 failed = ftrace_modify_code(ip, old, new);
433 if (failed) {
434 unsigned long key;
435 /* It is possible that the function hasn't been converted yet */
436 key = hash_long(ip, FTRACE_HASHBITS);
437 if (!ftrace_ip_in_hash(ip, key)) {
438 rec->flags |= FTRACE_FL_FAILED;
439 ftrace_free_rec(rec);
440 }
441
442 }
443 }
444
445 static void ftrace_replace_code(int enable)
446 {
447 unsigned char *new = NULL, *old = NULL;
448 struct dyn_ftrace *rec;
449 struct ftrace_page *pg;
450 int i;
451
452 if (enable)
453 old = ftrace_nop_replace();
454 else
455 new = ftrace_nop_replace();
456
457 for (pg = ftrace_pages_start; pg; pg = pg->next) {
458 for (i = 0; i < pg->index; i++) {
459 rec = &pg->records[i];
460
461 /* don't modify code that has already faulted */
462 if (rec->flags & FTRACE_FL_FAILED)
463 continue;
464
465 __ftrace_replace_code(rec, old, new, enable);
466 }
467 }
468 }
469
470 static void ftrace_shutdown_replenish(void)
471 {
472 if (ftrace_pages->next)
473 return;
474
475 /* allocate another page */
476 ftrace_pages->next = (void *)get_zeroed_page(GFP_KERNEL);
477 }
478
479 static int
480 ftrace_code_disable(struct dyn_ftrace *rec)
481 {
482 unsigned long ip;
483 unsigned char *nop, *call;
484 int failed;
485
486 ip = rec->ip;
487
488 nop = ftrace_nop_replace();
489 call = ftrace_call_replace(ip, MCOUNT_ADDR);
490
491 failed = ftrace_modify_code(ip, call, nop);
492 if (failed) {
493 rec->flags |= FTRACE_FL_FAILED;
494 ftrace_free_rec(rec);
495 return 0;
496 }
497 return 1;
498 }
499
500 static int __ftrace_update_code(void *ignore);
501
502 static int __ftrace_modify_code(void *data)
503 {
504 unsigned long addr;
505 int *command = data;
506
507 if (*command & FTRACE_ENABLE_CALLS) {
508 /*
509 * Update any recorded ips now that we have the
510 * machine stopped
511 */
512 __ftrace_update_code(NULL);
513 ftrace_replace_code(1);
514 } else if (*command & FTRACE_DISABLE_CALLS)
515 ftrace_replace_code(0);
516
517 if (*command & FTRACE_UPDATE_TRACE_FUNC)
518 ftrace_update_ftrace_func(ftrace_trace_function);
519
520 if (*command & FTRACE_ENABLE_MCOUNT) {
521 addr = (unsigned long)ftrace_record_ip;
522 ftrace_mcount_set(&addr);
523 } else if (*command & FTRACE_DISABLE_MCOUNT) {
524 addr = (unsigned long)ftrace_stub;
525 ftrace_mcount_set(&addr);
526 }
527
528 return 0;
529 }
530
531 static void ftrace_run_update_code(int command)
532 {
533 stop_machine_run(__ftrace_modify_code, &command, NR_CPUS);
534 }
535
536 void ftrace_disable_daemon(void)
537 {
538 /* Stop the daemon from calling kstop_machine */
539 mutex_lock(&ftraced_lock);
540 ftraced_stop = 1;
541 mutex_unlock(&ftraced_lock);
542
543 ftrace_force_update();
544 }
545
546 void ftrace_enable_daemon(void)
547 {
548 mutex_lock(&ftraced_lock);
549 ftraced_stop = 0;
550 mutex_unlock(&ftraced_lock);
551
552 ftrace_force_update();
553 }
554
555 static ftrace_func_t saved_ftrace_func;
556
557 static void ftrace_startup(void)
558 {
559 int command = 0;
560
561 if (unlikely(ftrace_disabled))
562 return;
563
564 mutex_lock(&ftraced_lock);
565 ftraced_suspend++;
566 if (ftraced_suspend == 1)
567 command |= FTRACE_ENABLE_CALLS;
568
569 if (saved_ftrace_func != ftrace_trace_function) {
570 saved_ftrace_func = ftrace_trace_function;
571 command |= FTRACE_UPDATE_TRACE_FUNC;
572 }
573
574 if (!command || !ftrace_enabled)
575 goto out;
576
577 ftrace_run_update_code(command);
578 out:
579 mutex_unlock(&ftraced_lock);
580 }
581
582 static void ftrace_shutdown(void)
583 {
584 int command = 0;
585
586 if (unlikely(ftrace_disabled))
587 return;
588
589 mutex_lock(&ftraced_lock);
590 ftraced_suspend--;
591 if (!ftraced_suspend)
592 command |= FTRACE_DISABLE_CALLS;
593
594 if (saved_ftrace_func != ftrace_trace_function) {
595 saved_ftrace_func = ftrace_trace_function;
596 command |= FTRACE_UPDATE_TRACE_FUNC;
597 }
598
599 if (!command || !ftrace_enabled)
600 goto out;
601
602 ftrace_run_update_code(command);
603 out:
604 mutex_unlock(&ftraced_lock);
605 }
606
607 static void ftrace_startup_sysctl(void)
608 {
609 int command = FTRACE_ENABLE_MCOUNT;
610
611 if (unlikely(ftrace_disabled))
612 return;
613
614 mutex_lock(&ftraced_lock);
615 /* Force update next time */
616 saved_ftrace_func = NULL;
617 /* ftraced_suspend is true if we want ftrace running */
618 if (ftraced_suspend)
619 command |= FTRACE_ENABLE_CALLS;
620
621 ftrace_run_update_code(command);
622 mutex_unlock(&ftraced_lock);
623 }
624
625 static void ftrace_shutdown_sysctl(void)
626 {
627 int command = FTRACE_DISABLE_MCOUNT;
628
629 if (unlikely(ftrace_disabled))
630 return;
631
632 mutex_lock(&ftraced_lock);
633 /* ftraced_suspend is true if ftrace is running */
634 if (ftraced_suspend)
635 command |= FTRACE_DISABLE_CALLS;
636
637 ftrace_run_update_code(command);
638 mutex_unlock(&ftraced_lock);
639 }
640
641 static cycle_t ftrace_update_time;
642 static unsigned long ftrace_update_cnt;
643 unsigned long ftrace_update_tot_cnt;
644
645 static int __ftrace_update_code(void *ignore)
646 {
647 struct dyn_ftrace *p;
648 struct hlist_head head;
649 struct hlist_node *t;
650 int save_ftrace_enabled;
651 cycle_t start, stop;
652 int i;
653
654 /* Don't be recording funcs now */
655 ftrace_record_suspend++;
656 save_ftrace_enabled = ftrace_enabled;
657 ftrace_enabled = 0;
658
659 start = ftrace_now(raw_smp_processor_id());
660 ftrace_update_cnt = 0;
661
662 /* No locks needed, the machine is stopped! */
663 for (i = 0; i < FTRACE_HASHSIZE; i++) {
664 if (hlist_empty(&ftrace_hash[i]))
665 continue;
666
667 head = ftrace_hash[i];
668 INIT_HLIST_HEAD(&ftrace_hash[i]);
669
670 /* all CPUS are stopped, we are safe to modify code */
671 hlist_for_each_entry(p, t, &head, node) {
672 if (ftrace_code_disable(p))
673 ftrace_update_cnt++;
674 }
675
676 }
677
678 stop = ftrace_now(raw_smp_processor_id());
679 ftrace_update_time = stop - start;
680 ftrace_update_tot_cnt += ftrace_update_cnt;
681 ftraced_trigger = 0;
682
683 ftrace_enabled = save_ftrace_enabled;
684 ftrace_record_suspend--;
685
686 return 0;
687 }
688
689 static int ftrace_update_code(void)
690 {
691 if (unlikely(ftrace_disabled) ||
692 !ftrace_enabled || !ftraced_trigger)
693 return 0;
694
695 stop_machine_run(__ftrace_update_code, NULL, NR_CPUS);
696
697 return 1;
698 }
699
700 static int ftraced(void *ignore)
701 {
702 unsigned long usecs;
703
704 while (!kthread_should_stop()) {
705
706 set_current_state(TASK_INTERRUPTIBLE);
707
708 /* check once a second */
709 schedule_timeout(HZ);
710
711 if (unlikely(ftrace_disabled))
712 continue;
713
714 mutex_lock(&ftrace_sysctl_lock);
715 mutex_lock(&ftraced_lock);
716 if (!ftraced_suspend && !ftraced_stop &&
717 ftrace_update_code()) {
718 usecs = nsecs_to_usecs(ftrace_update_time);
719 if (ftrace_update_tot_cnt > 100000) {
720 ftrace_update_tot_cnt = 0;
721 pr_info("hm, dftrace overflow: %lu change%s"
722 " (%lu total) in %lu usec%s\n",
723 ftrace_update_cnt,
724 ftrace_update_cnt != 1 ? "s" : "",
725 ftrace_update_tot_cnt,
726 usecs, usecs != 1 ? "s" : "");
727 ftrace_disabled = 1;
728 WARN_ON_ONCE(1);
729 }
730 }
731 mutex_unlock(&ftraced_lock);
732 mutex_unlock(&ftrace_sysctl_lock);
733
734 ftrace_shutdown_replenish();
735 }
736 __set_current_state(TASK_RUNNING);
737 return 0;
738 }
739
740 static int __init ftrace_dyn_table_alloc(void)
741 {
742 struct ftrace_page *pg;
743 int cnt;
744 int i;
745
746 /* allocate a few pages */
747 ftrace_pages_start = (void *)get_zeroed_page(GFP_KERNEL);
748 if (!ftrace_pages_start)
749 return -1;
750
751 /*
752 * Allocate a few more pages.
753 *
754 * TODO: have some parser search vmlinux before
755 * final linking to find all calls to ftrace.
756 * Then we can:
757 * a) know how many pages to allocate.
758 * and/or
759 * b) set up the table then.
760 *
761 * The dynamic code is still necessary for
762 * modules.
763 */
764
765 pg = ftrace_pages = ftrace_pages_start;
766
767 cnt = NR_TO_INIT / ENTRIES_PER_PAGE;
768
769 for (i = 0; i < cnt; i++) {
770 pg->next = (void *)get_zeroed_page(GFP_KERNEL);
771
772 /* If we fail, we'll try later anyway */
773 if (!pg->next)
774 break;
775
776 pg = pg->next;
777 }
778
779 return 0;
780 }
781
782 enum {
783 FTRACE_ITER_FILTER = (1 << 0),
784 FTRACE_ITER_CONT = (1 << 1),
785 FTRACE_ITER_NOTRACE = (1 << 2),
786 };
787
788 #define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
789
790 struct ftrace_iterator {
791 loff_t pos;
792 struct ftrace_page *pg;
793 unsigned idx;
794 unsigned flags;
795 unsigned char buffer[FTRACE_BUFF_MAX+1];
796 unsigned buffer_idx;
797 unsigned filtered;
798 };
799
800 static void *
801 t_next(struct seq_file *m, void *v, loff_t *pos)
802 {
803 struct ftrace_iterator *iter = m->private;
804 struct dyn_ftrace *rec = NULL;
805
806 (*pos)++;
807
808 retry:
809 if (iter->idx >= iter->pg->index) {
810 if (iter->pg->next) {
811 iter->pg = iter->pg->next;
812 iter->idx = 0;
813 goto retry;
814 }
815 } else {
816 rec = &iter->pg->records[iter->idx++];
817 if ((rec->flags & FTRACE_FL_FAILED) ||
818 ((iter->flags & FTRACE_ITER_FILTER) &&
819 !(rec->flags & FTRACE_FL_FILTER)) ||
820 ((iter->flags & FTRACE_ITER_NOTRACE) &&
821 !(rec->flags & FTRACE_FL_NOTRACE))) {
822 rec = NULL;
823 goto retry;
824 }
825 }
826
827 iter->pos = *pos;
828
829 return rec;
830 }
831
832 static void *t_start(struct seq_file *m, loff_t *pos)
833 {
834 struct ftrace_iterator *iter = m->private;
835 void *p = NULL;
836 loff_t l = -1;
837
838 if (*pos != iter->pos) {
839 for (p = t_next(m, p, &l); p && l < *pos; p = t_next(m, p, &l))
840 ;
841 } else {
842 l = *pos;
843 p = t_next(m, p, &l);
844 }
845
846 return p;
847 }
848
849 static void t_stop(struct seq_file *m, void *p)
850 {
851 }
852
853 static int t_show(struct seq_file *m, void *v)
854 {
855 struct dyn_ftrace *rec = v;
856 char str[KSYM_SYMBOL_LEN];
857
858 if (!rec)
859 return 0;
860
861 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
862
863 seq_printf(m, "%s\n", str);
864
865 return 0;
866 }
867
868 static struct seq_operations show_ftrace_seq_ops = {
869 .start = t_start,
870 .next = t_next,
871 .stop = t_stop,
872 .show = t_show,
873 };
874
875 static int
876 ftrace_avail_open(struct inode *inode, struct file *file)
877 {
878 struct ftrace_iterator *iter;
879 int ret;
880
881 if (unlikely(ftrace_disabled))
882 return -ENODEV;
883
884 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
885 if (!iter)
886 return -ENOMEM;
887
888 iter->pg = ftrace_pages_start;
889 iter->pos = -1;
890
891 ret = seq_open(file, &show_ftrace_seq_ops);
892 if (!ret) {
893 struct seq_file *m = file->private_data;
894
895 m->private = iter;
896 } else {
897 kfree(iter);
898 }
899
900 return ret;
901 }
902
903 int ftrace_avail_release(struct inode *inode, struct file *file)
904 {
905 struct seq_file *m = (struct seq_file *)file->private_data;
906 struct ftrace_iterator *iter = m->private;
907
908 seq_release(inode, file);
909 kfree(iter);
910
911 return 0;
912 }
913
914 static void ftrace_filter_reset(int enable)
915 {
916 struct ftrace_page *pg;
917 struct dyn_ftrace *rec;
918 unsigned long type = enable ? FTRACE_FL_FILTER : FTRACE_FL_NOTRACE;
919 unsigned i;
920
921 /* keep kstop machine from running */
922 preempt_disable();
923 if (enable)
924 ftrace_filtered = 0;
925 pg = ftrace_pages_start;
926 while (pg) {
927 for (i = 0; i < pg->index; i++) {
928 rec = &pg->records[i];
929 if (rec->flags & FTRACE_FL_FAILED)
930 continue;
931 rec->flags &= ~type;
932 }
933 pg = pg->next;
934 }
935 preempt_enable();
936 }
937
938 static int
939 ftrace_regex_open(struct inode *inode, struct file *file, int enable)
940 {
941 struct ftrace_iterator *iter;
942 int ret = 0;
943
944 if (unlikely(ftrace_disabled))
945 return -ENODEV;
946
947 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
948 if (!iter)
949 return -ENOMEM;
950
951 mutex_lock(&ftrace_regex_lock);
952 if ((file->f_mode & FMODE_WRITE) &&
953 !(file->f_flags & O_APPEND))
954 ftrace_filter_reset(enable);
955
956 if (file->f_mode & FMODE_READ) {
957 iter->pg = ftrace_pages_start;
958 iter->pos = -1;
959 iter->flags = enable ? FTRACE_ITER_FILTER :
960 FTRACE_ITER_NOTRACE;
961
962 ret = seq_open(file, &show_ftrace_seq_ops);
963 if (!ret) {
964 struct seq_file *m = file->private_data;
965 m->private = iter;
966 } else
967 kfree(iter);
968 } else
969 file->private_data = iter;
970 mutex_unlock(&ftrace_regex_lock);
971
972 return ret;
973 }
974
975 static int
976 ftrace_filter_open(struct inode *inode, struct file *file)
977 {
978 return ftrace_regex_open(inode, file, 1);
979 }
980
981 static int
982 ftrace_notrace_open(struct inode *inode, struct file *file)
983 {
984 return ftrace_regex_open(inode, file, 0);
985 }
986
987 static ssize_t
988 ftrace_regex_read(struct file *file, char __user *ubuf,
989 size_t cnt, loff_t *ppos)
990 {
991 if (file->f_mode & FMODE_READ)
992 return seq_read(file, ubuf, cnt, ppos);
993 else
994 return -EPERM;
995 }
996
997 static loff_t
998 ftrace_regex_lseek(struct file *file, loff_t offset, int origin)
999 {
1000 loff_t ret;
1001
1002 if (file->f_mode & FMODE_READ)
1003 ret = seq_lseek(file, offset, origin);
1004 else
1005 file->f_pos = ret = 1;
1006
1007 return ret;
1008 }
1009
1010 enum {
1011 MATCH_FULL,
1012 MATCH_FRONT_ONLY,
1013 MATCH_MIDDLE_ONLY,
1014 MATCH_END_ONLY,
1015 };
1016
1017 static void
1018 ftrace_match(unsigned char *buff, int len, int enable)
1019 {
1020 char str[KSYM_SYMBOL_LEN];
1021 char *search = NULL;
1022 struct ftrace_page *pg;
1023 struct dyn_ftrace *rec;
1024 int type = MATCH_FULL;
1025 unsigned long flag = enable ? FTRACE_FL_FILTER : FTRACE_FL_NOTRACE;
1026 unsigned i, match = 0, search_len = 0;
1027
1028 for (i = 0; i < len; i++) {
1029 if (buff[i] == '*') {
1030 if (!i) {
1031 search = buff + i + 1;
1032 type = MATCH_END_ONLY;
1033 search_len = len - (i + 1);
1034 } else {
1035 if (type == MATCH_END_ONLY) {
1036 type = MATCH_MIDDLE_ONLY;
1037 } else {
1038 match = i;
1039 type = MATCH_FRONT_ONLY;
1040 }
1041 buff[i] = 0;
1042 break;
1043 }
1044 }
1045 }
1046
1047 /* keep kstop machine from running */
1048 preempt_disable();
1049 if (enable)
1050 ftrace_filtered = 1;
1051 pg = ftrace_pages_start;
1052 while (pg) {
1053 for (i = 0; i < pg->index; i++) {
1054 int matched = 0;
1055 char *ptr;
1056
1057 rec = &pg->records[i];
1058 if (rec->flags & FTRACE_FL_FAILED)
1059 continue;
1060 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
1061 switch (type) {
1062 case MATCH_FULL:
1063 if (strcmp(str, buff) == 0)
1064 matched = 1;
1065 break;
1066 case MATCH_FRONT_ONLY:
1067 if (memcmp(str, buff, match) == 0)
1068 matched = 1;
1069 break;
1070 case MATCH_MIDDLE_ONLY:
1071 if (strstr(str, search))
1072 matched = 1;
1073 break;
1074 case MATCH_END_ONLY:
1075 ptr = strstr(str, search);
1076 if (ptr && (ptr[search_len] == 0))
1077 matched = 1;
1078 break;
1079 }
1080 if (matched)
1081 rec->flags |= flag;
1082 }
1083 pg = pg->next;
1084 }
1085 preempt_enable();
1086 }
1087
1088 static ssize_t
1089 ftrace_regex_write(struct file *file, const char __user *ubuf,
1090 size_t cnt, loff_t *ppos, int enable)
1091 {
1092 struct ftrace_iterator *iter;
1093 char ch;
1094 size_t read = 0;
1095 ssize_t ret;
1096
1097 if (!cnt || cnt < 0)
1098 return 0;
1099
1100 mutex_lock(&ftrace_regex_lock);
1101
1102 if (file->f_mode & FMODE_READ) {
1103 struct seq_file *m = file->private_data;
1104 iter = m->private;
1105 } else
1106 iter = file->private_data;
1107
1108 if (!*ppos) {
1109 iter->flags &= ~FTRACE_ITER_CONT;
1110 iter->buffer_idx = 0;
1111 }
1112
1113 ret = get_user(ch, ubuf++);
1114 if (ret)
1115 goto out;
1116 read++;
1117 cnt--;
1118
1119 if (!(iter->flags & ~FTRACE_ITER_CONT)) {
1120 /* skip white space */
1121 while (cnt && isspace(ch)) {
1122 ret = get_user(ch, ubuf++);
1123 if (ret)
1124 goto out;
1125 read++;
1126 cnt--;
1127 }
1128
1129 if (isspace(ch)) {
1130 file->f_pos += read;
1131 ret = read;
1132 goto out;
1133 }
1134
1135 iter->buffer_idx = 0;
1136 }
1137
1138 while (cnt && !isspace(ch)) {
1139 if (iter->buffer_idx < FTRACE_BUFF_MAX)
1140 iter->buffer[iter->buffer_idx++] = ch;
1141 else {
1142 ret = -EINVAL;
1143 goto out;
1144 }
1145 ret = get_user(ch, ubuf++);
1146 if (ret)
1147 goto out;
1148 read++;
1149 cnt--;
1150 }
1151
1152 if (isspace(ch)) {
1153 iter->filtered++;
1154 iter->buffer[iter->buffer_idx] = 0;
1155 ftrace_match(iter->buffer, iter->buffer_idx, enable);
1156 iter->buffer_idx = 0;
1157 } else
1158 iter->flags |= FTRACE_ITER_CONT;
1159
1160
1161 file->f_pos += read;
1162
1163 ret = read;
1164 out:
1165 mutex_unlock(&ftrace_regex_lock);
1166
1167 return ret;
1168 }
1169
1170 static ssize_t
1171 ftrace_filter_write(struct file *file, const char __user *ubuf,
1172 size_t cnt, loff_t *ppos)
1173 {
1174 return ftrace_regex_write(file, ubuf, cnt, ppos, 1);
1175 }
1176
1177 static ssize_t
1178 ftrace_notrace_write(struct file *file, const char __user *ubuf,
1179 size_t cnt, loff_t *ppos)
1180 {
1181 return ftrace_regex_write(file, ubuf, cnt, ppos, 0);
1182 }
1183
1184 static void
1185 ftrace_set_regex(unsigned char *buf, int len, int reset, int enable)
1186 {
1187 if (unlikely(ftrace_disabled))
1188 return;
1189
1190 mutex_lock(&ftrace_regex_lock);
1191 if (reset)
1192 ftrace_filter_reset(enable);
1193 if (buf)
1194 ftrace_match(buf, len, enable);
1195 mutex_unlock(&ftrace_regex_lock);
1196 }
1197
1198 /**
1199 * ftrace_set_filter - set a function to filter on in ftrace
1200 * @buf - the string that holds the function filter text.
1201 * @len - the length of the string.
1202 * @reset - non zero to reset all filters before applying this filter.
1203 *
1204 * Filters denote which functions should be enabled when tracing is enabled.
1205 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
1206 */
1207 void ftrace_set_filter(unsigned char *buf, int len, int reset)
1208 {
1209 ftrace_set_regex(buf, len, reset, 1);
1210 }
1211
1212 /**
1213 * ftrace_set_notrace - set a function to not trace in ftrace
1214 * @buf - the string that holds the function notrace text.
1215 * @len - the length of the string.
1216 * @reset - non zero to reset all filters before applying this filter.
1217 *
1218 * Notrace Filters denote which functions should not be enabled when tracing
1219 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
1220 * for tracing.
1221 */
1222 void ftrace_set_notrace(unsigned char *buf, int len, int reset)
1223 {
1224 ftrace_set_regex(buf, len, reset, 0);
1225 }
1226
1227 static int
1228 ftrace_regex_release(struct inode *inode, struct file *file, int enable)
1229 {
1230 struct seq_file *m = (struct seq_file *)file->private_data;
1231 struct ftrace_iterator *iter;
1232
1233 mutex_lock(&ftrace_regex_lock);
1234 if (file->f_mode & FMODE_READ) {
1235 iter = m->private;
1236
1237 seq_release(inode, file);
1238 } else
1239 iter = file->private_data;
1240
1241 if (iter->buffer_idx) {
1242 iter->filtered++;
1243 iter->buffer[iter->buffer_idx] = 0;
1244 ftrace_match(iter->buffer, iter->buffer_idx, enable);
1245 }
1246
1247 mutex_lock(&ftrace_sysctl_lock);
1248 mutex_lock(&ftraced_lock);
1249 if (iter->filtered && ftraced_suspend && ftrace_enabled)
1250 ftrace_run_update_code(FTRACE_ENABLE_CALLS);
1251 mutex_unlock(&ftraced_lock);
1252 mutex_unlock(&ftrace_sysctl_lock);
1253
1254 kfree(iter);
1255 mutex_unlock(&ftrace_regex_lock);
1256 return 0;
1257 }
1258
1259 static int
1260 ftrace_filter_release(struct inode *inode, struct file *file)
1261 {
1262 return ftrace_regex_release(inode, file, 1);
1263 }
1264
1265 static int
1266 ftrace_notrace_release(struct inode *inode, struct file *file)
1267 {
1268 return ftrace_regex_release(inode, file, 0);
1269 }
1270
1271 static ssize_t
1272 ftraced_read(struct file *filp, char __user *ubuf,
1273 size_t cnt, loff_t *ppos)
1274 {
1275 /* don't worry about races */
1276 char *buf = ftraced_stop ? "disabled\n" : "enabled\n";
1277 int r = strlen(buf);
1278
1279 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
1280 }
1281
1282 static ssize_t
1283 ftraced_write(struct file *filp, const char __user *ubuf,
1284 size_t cnt, loff_t *ppos)
1285 {
1286 char buf[64];
1287 long val;
1288 int ret;
1289
1290 if (cnt >= sizeof(buf))
1291 return -EINVAL;
1292
1293 if (copy_from_user(&buf, ubuf, cnt))
1294 return -EFAULT;
1295
1296 if (strncmp(buf, "enable", 6) == 0)
1297 val = 1;
1298 else if (strncmp(buf, "disable", 7) == 0)
1299 val = 0;
1300 else {
1301 buf[cnt] = 0;
1302
1303 ret = strict_strtoul(buf, 10, &val);
1304 if (ret < 0)
1305 return ret;
1306
1307 val = !!val;
1308 }
1309
1310 if (val)
1311 ftrace_enable_daemon();
1312 else
1313 ftrace_disable_daemon();
1314
1315 filp->f_pos += cnt;
1316
1317 return cnt;
1318 }
1319
1320 static struct file_operations ftrace_avail_fops = {
1321 .open = ftrace_avail_open,
1322 .read = seq_read,
1323 .llseek = seq_lseek,
1324 .release = ftrace_avail_release,
1325 };
1326
1327 static struct file_operations ftrace_filter_fops = {
1328 .open = ftrace_filter_open,
1329 .read = ftrace_regex_read,
1330 .write = ftrace_filter_write,
1331 .llseek = ftrace_regex_lseek,
1332 .release = ftrace_filter_release,
1333 };
1334
1335 static struct file_operations ftrace_notrace_fops = {
1336 .open = ftrace_notrace_open,
1337 .read = ftrace_regex_read,
1338 .write = ftrace_notrace_write,
1339 .llseek = ftrace_regex_lseek,
1340 .release = ftrace_notrace_release,
1341 };
1342
1343 static struct file_operations ftraced_fops = {
1344 .open = tracing_open_generic,
1345 .read = ftraced_read,
1346 .write = ftraced_write,
1347 };
1348
1349 /**
1350 * ftrace_force_update - force an update to all recording ftrace functions
1351 */
1352 int ftrace_force_update(void)
1353 {
1354 int ret = 0;
1355
1356 if (unlikely(ftrace_disabled))
1357 return -ENODEV;
1358
1359 mutex_lock(&ftrace_sysctl_lock);
1360 mutex_lock(&ftraced_lock);
1361
1362 /*
1363 * If ftraced_trigger is not set, then there is nothing
1364 * to update.
1365 */
1366 if (ftraced_trigger && !ftrace_update_code())
1367 ret = -EBUSY;
1368
1369 mutex_unlock(&ftraced_lock);
1370 mutex_unlock(&ftrace_sysctl_lock);
1371
1372 return ret;
1373 }
1374
1375 static void ftrace_force_shutdown(void)
1376 {
1377 struct task_struct *task;
1378 int command = FTRACE_DISABLE_CALLS | FTRACE_UPDATE_TRACE_FUNC;
1379
1380 mutex_lock(&ftraced_lock);
1381 task = ftraced_task;
1382 ftraced_task = NULL;
1383 ftraced_suspend = -1;
1384 ftrace_run_update_code(command);
1385 mutex_unlock(&ftraced_lock);
1386
1387 if (task)
1388 kthread_stop(task);
1389 }
1390
1391 static __init int ftrace_init_debugfs(void)
1392 {
1393 struct dentry *d_tracer;
1394 struct dentry *entry;
1395
1396 d_tracer = tracing_init_dentry();
1397
1398 entry = debugfs_create_file("available_filter_functions", 0444,
1399 d_tracer, NULL, &ftrace_avail_fops);
1400 if (!entry)
1401 pr_warning("Could not create debugfs "
1402 "'available_filter_functions' entry\n");
1403
1404 entry = debugfs_create_file("set_ftrace_filter", 0644, d_tracer,
1405 NULL, &ftrace_filter_fops);
1406 if (!entry)
1407 pr_warning("Could not create debugfs "
1408 "'set_ftrace_filter' entry\n");
1409
1410 entry = debugfs_create_file("set_ftrace_notrace", 0644, d_tracer,
1411 NULL, &ftrace_notrace_fops);
1412 if (!entry)
1413 pr_warning("Could not create debugfs "
1414 "'set_ftrace_notrace' entry\n");
1415
1416 entry = debugfs_create_file("ftraced_enabled", 0644, d_tracer,
1417 NULL, &ftraced_fops);
1418 if (!entry)
1419 pr_warning("Could not create debugfs "
1420 "'ftraced_enabled' entry\n");
1421 return 0;
1422 }
1423
1424 fs_initcall(ftrace_init_debugfs);
1425
1426 static int __init ftrace_dynamic_init(void)
1427 {
1428 struct task_struct *p;
1429 unsigned long addr;
1430 int ret;
1431
1432 addr = (unsigned long)ftrace_record_ip;
1433
1434 stop_machine_run(ftrace_dyn_arch_init, &addr, NR_CPUS);
1435
1436 /* ftrace_dyn_arch_init places the return code in addr */
1437 if (addr) {
1438 ret = (int)addr;
1439 goto failed;
1440 }
1441
1442 ret = ftrace_dyn_table_alloc();
1443 if (ret)
1444 goto failed;
1445
1446 p = kthread_run(ftraced, NULL, "ftraced");
1447 if (IS_ERR(p)) {
1448 ret = -1;
1449 goto failed;
1450 }
1451
1452 last_ftrace_enabled = ftrace_enabled = 1;
1453 ftraced_task = p;
1454
1455 return 0;
1456
1457 failed:
1458 ftrace_disabled = 1;
1459 return ret;
1460 }
1461
1462 core_initcall(ftrace_dynamic_init);
1463 #else
1464 # define ftrace_startup() do { } while (0)
1465 # define ftrace_shutdown() do { } while (0)
1466 # define ftrace_startup_sysctl() do { } while (0)
1467 # define ftrace_shutdown_sysctl() do { } while (0)
1468 # define ftrace_force_shutdown() do { } while (0)
1469 #endif /* CONFIG_DYNAMIC_FTRACE */
1470
1471 /**
1472 * ftrace_kill - totally shutdown ftrace
1473 *
1474 * This is a safety measure. If something was detected that seems
1475 * wrong, calling this function will keep ftrace from doing
1476 * any more modifications, and updates.
1477 * used when something went wrong.
1478 */
1479 void ftrace_kill(void)
1480 {
1481 mutex_lock(&ftrace_sysctl_lock);
1482 ftrace_disabled = 1;
1483 ftrace_enabled = 0;
1484
1485 clear_ftrace_function();
1486 mutex_unlock(&ftrace_sysctl_lock);
1487
1488 /* Try to totally disable ftrace */
1489 ftrace_force_shutdown();
1490 }
1491
1492 /**
1493 * __ftrace_kill - shutdown ftrace in a mean fashion
1494 *
1495 * In case of system failure we want to stop ftrace as soon as
1496 * possible. This is like ftrace_kill but does not grab the
1497 * mutexes nor does it call the kstop machine.
1498 *
1499 * This one is save to use in atomic.
1500 */
1501 void __ftrace_kill(void)
1502 {
1503 ftrace_disabled = 1;
1504 ftrace_enabled = 0;
1505
1506 clear_ftrace_function();
1507 }
1508
1509 /**
1510 * register_ftrace_function - register a function for profiling
1511 * @ops - ops structure that holds the function for profiling.
1512 *
1513 * Register a function to be called by all functions in the
1514 * kernel.
1515 *
1516 * Note: @ops->func and all the functions it calls must be labeled
1517 * with "notrace", otherwise it will go into a
1518 * recursive loop.
1519 */
1520 int register_ftrace_function(struct ftrace_ops *ops)
1521 {
1522 int ret;
1523
1524 if (unlikely(ftrace_disabled))
1525 return -1;
1526
1527 mutex_lock(&ftrace_sysctl_lock);
1528 ret = __register_ftrace_function(ops);
1529 ftrace_startup();
1530 mutex_unlock(&ftrace_sysctl_lock);
1531
1532 return ret;
1533 }
1534
1535 /**
1536 * unregister_ftrace_function - unresgister a function for profiling.
1537 * @ops - ops structure that holds the function to unregister
1538 *
1539 * Unregister a function that was added to be called by ftrace profiling.
1540 */
1541 int unregister_ftrace_function(struct ftrace_ops *ops)
1542 {
1543 int ret;
1544
1545 mutex_lock(&ftrace_sysctl_lock);
1546 ret = __unregister_ftrace_function(ops);
1547 ftrace_shutdown();
1548 mutex_unlock(&ftrace_sysctl_lock);
1549
1550 return ret;
1551 }
1552
1553 int
1554 ftrace_enable_sysctl(struct ctl_table *table, int write,
1555 struct file *file, void __user *buffer, size_t *lenp,
1556 loff_t *ppos)
1557 {
1558 int ret;
1559
1560 if (unlikely(ftrace_disabled))
1561 return -ENODEV;
1562
1563 mutex_lock(&ftrace_sysctl_lock);
1564
1565 ret = proc_dointvec(table, write, file, buffer, lenp, ppos);
1566
1567 if (ret || !write || (last_ftrace_enabled == ftrace_enabled))
1568 goto out;
1569
1570 last_ftrace_enabled = ftrace_enabled;
1571
1572 if (ftrace_enabled) {
1573
1574 ftrace_startup_sysctl();
1575
1576 /* we are starting ftrace again */
1577 if (ftrace_list != &ftrace_list_end) {
1578 if (ftrace_list->next == &ftrace_list_end)
1579 ftrace_trace_function = ftrace_list->func;
1580 else
1581 ftrace_trace_function = ftrace_list_func;
1582 }
1583
1584 } else {
1585 /* stopping ftrace calls (just send to ftrace_stub) */
1586 ftrace_trace_function = ftrace_stub;
1587
1588 ftrace_shutdown_sysctl();
1589 }
1590
1591 out:
1592 mutex_unlock(&ftrace_sysctl_lock);
1593 return ret;
1594 }
1595
|
This page was automatically generated by the
LXR engine.
|