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