ftrace: safe traversal of ftrace_hash hlist
[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 void
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         }
473 }
474
475 static int __ftrace_modify_code(void *data)
476 {
477         unsigned long addr;
478         int *command = data;
479
480         if (*command & FTRACE_ENABLE_CALLS)
481                 ftrace_replace_code(1);
482         else if (*command & FTRACE_DISABLE_CALLS)
483                 ftrace_replace_code(0);
484
485         if (*command & FTRACE_UPDATE_TRACE_FUNC)
486                 ftrace_update_ftrace_func(ftrace_trace_function);
487
488         if (*command & FTRACE_ENABLE_MCOUNT) {
489                 addr = (unsigned long)ftrace_record_ip;
490                 ftrace_mcount_set(&addr);
491         } else if (*command & FTRACE_DISABLE_MCOUNT) {
492                 addr = (unsigned long)ftrace_stub;
493                 ftrace_mcount_set(&addr);
494         }
495
496         return 0;
497 }
498
499 static void ftrace_run_update_code(int command)
500 {
501         stop_machine_run(__ftrace_modify_code, &command, NR_CPUS);
502 }
503
504 static ftrace_func_t saved_ftrace_func;
505
506 static void ftrace_startup(void)
507 {
508         int command = 0;
509
510         if (unlikely(ftrace_disabled))
511                 return;
512
513         mutex_lock(&ftraced_lock);
514         ftraced_suspend++;
515         if (ftraced_suspend == 1)
516                 command |= FTRACE_ENABLE_CALLS;
517
518         if (saved_ftrace_func != ftrace_trace_function) {
519                 saved_ftrace_func = ftrace_trace_function;
520                 command |= FTRACE_UPDATE_TRACE_FUNC;
521         }
522
523         if (!command || !ftrace_enabled)
524                 goto out;
525
526         ftrace_run_update_code(command);
527  out:
528         mutex_unlock(&ftraced_lock);
529 }
530
531 static void ftrace_shutdown(void)
532 {
533         int command = 0;
534
535         if (unlikely(ftrace_disabled))
536                 return;
537
538         mutex_lock(&ftraced_lock);
539         ftraced_suspend--;
540         if (!ftraced_suspend)
541                 command |= FTRACE_DISABLE_CALLS;
542
543         if (saved_ftrace_func != ftrace_trace_function) {
544                 saved_ftrace_func = ftrace_trace_function;
545                 command |= FTRACE_UPDATE_TRACE_FUNC;
546         }
547
548         if (!command || !ftrace_enabled)
549                 goto out;
550
551         ftrace_run_update_code(command);
552  out:
553         mutex_unlock(&ftraced_lock);
554 }
555
556 static void ftrace_startup_sysctl(void)
557 {
558         int command = FTRACE_ENABLE_MCOUNT;
559
560         if (unlikely(ftrace_disabled))
561                 return;
562
563         mutex_lock(&ftraced_lock);
564         /* Force update next time */
565         saved_ftrace_func = NULL;
566         /* ftraced_suspend is true if we want ftrace running */
567         if (ftraced_suspend)
568                 command |= FTRACE_ENABLE_CALLS;
569
570         ftrace_run_update_code(command);
571         mutex_unlock(&ftraced_lock);
572 }
573
574 static void ftrace_shutdown_sysctl(void)
575 {
576         int command = FTRACE_DISABLE_MCOUNT;
577
578         if (unlikely(ftrace_disabled))
579                 return;
580
581         mutex_lock(&ftraced_lock);
582         /* ftraced_suspend is true if ftrace is running */
583         if (ftraced_suspend)
584                 command |= FTRACE_DISABLE_CALLS;
585
586         ftrace_run_update_code(command);
587         mutex_unlock(&ftraced_lock);
588 }
589
590 static cycle_t          ftrace_update_time;
591 static unsigned long    ftrace_update_cnt;
592 unsigned long           ftrace_update_tot_cnt;
593
594 static int __ftrace_update_code(void *ignore)
595 {
596         struct dyn_ftrace *p;
597         struct hlist_head head;
598         struct hlist_node *t;
599         int save_ftrace_enabled;
600         cycle_t start, stop;
601         int i;
602
603         /* Don't be recording funcs now */
604         save_ftrace_enabled = ftrace_enabled;
605         ftrace_enabled = 0;
606
607         start = ftrace_now(raw_smp_processor_id());
608         ftrace_update_cnt = 0;
609
610         /* No locks needed, the machine is stopped! */
611         for (i = 0; i < FTRACE_HASHSIZE; i++) {
612                 if (hlist_empty(&ftrace_hash[i]))
613                         continue;
614
615                 head = ftrace_hash[i];
616                 INIT_HLIST_HEAD(&ftrace_hash[i]);
617
618                 /* all CPUS are stopped, we are safe to modify code */
619                 hlist_for_each_entry(p, t, &head, node) {
620                         ftrace_code_disable(p);
621                         ftrace_update_cnt++;
622                 }
623
624         }
625
626         stop = ftrace_now(raw_smp_processor_id());
627         ftrace_update_time = stop - start;
628         ftrace_update_tot_cnt += ftrace_update_cnt;
629
630         ftrace_enabled = save_ftrace_enabled;
631
632         return 0;
633 }
634
635 static void ftrace_update_code(void)
636 {
637         if (unlikely(ftrace_disabled))
638                 return;
639
640         stop_machine_run(__ftrace_update_code, NULL, NR_CPUS);
641 }
642
643 static int ftraced(void *ignore)
644 {
645         unsigned long usecs;
646
647         while (!kthread_should_stop()) {
648
649                 set_current_state(TASK_INTERRUPTIBLE);
650
651                 /* check once a second */
652                 schedule_timeout(HZ);
653
654                 if (unlikely(ftrace_disabled))
655                         continue;
656
657                 mutex_lock(&ftrace_sysctl_lock);
658                 mutex_lock(&ftraced_lock);
659                 if (ftrace_enabled && ftraced_trigger && !ftraced_suspend) {
660                         ftrace_record_suspend++;
661                         ftrace_update_code();
662                         usecs = nsecs_to_usecs(ftrace_update_time);
663                         if (ftrace_update_tot_cnt > 100000) {
664                                 ftrace_update_tot_cnt = 0;
665                                 pr_info("hm, dftrace overflow: %lu change%s"
666                                          " (%lu total) in %lu usec%s\n",
667                                         ftrace_update_cnt,
668                                         ftrace_update_cnt != 1 ? "s" : "",
669                                         ftrace_update_tot_cnt,
670                                         usecs, usecs != 1 ? "s" : "");
671                                 ftrace_disabled = 1;
672                                 WARN_ON_ONCE(1);
673                         }
674                         ftraced_trigger = 0;
675                         ftrace_record_suspend--;
676                 }
677                 ftraced_iteration_counter++;
678                 mutex_unlock(&ftraced_lock);
679                 mutex_unlock(&ftrace_sysctl_lock);
680
681                 wake_up_interruptible(&ftraced_waiters);
682
683                 ftrace_shutdown_replenish();
684         }
685         __set_current_state(TASK_RUNNING);
686         return 0;
687 }
688
689 static int __init ftrace_dyn_table_alloc(void)
690 {
691         struct ftrace_page *pg;
692         int cnt;
693         int i;
694
695         /* allocate a few pages */
696         ftrace_pages_start = (void *)get_zeroed_page(GFP_KERNEL);
697         if (!ftrace_pages_start)
698                 return -1;
699
700         /*
701          * Allocate a few more pages.
702          *
703          * TODO: have some parser search vmlinux before
704          *   final linking to find all calls to ftrace.
705          *   Then we can:
706          *    a) know how many pages to allocate.
707          *     and/or
708          *    b) set up the table then.
709          *
710          *  The dynamic code is still necessary for
711          *  modules.
712          */
713
714         pg = ftrace_pages = ftrace_pages_start;
715
716         cnt = NR_TO_INIT / ENTRIES_PER_PAGE;
717
718         for (i = 0; i < cnt; i++) {
719                 pg->next = (void *)get_zeroed_page(GFP_KERNEL);
720
721                 /* If we fail, we'll try later anyway */
722                 if (!pg->next)
723                         break;
724
725                 pg = pg->next;
726         }
727
728         return 0;
729 }
730
731 enum {
732         FTRACE_ITER_FILTER      = (1 << 0),
733         FTRACE_ITER_CONT        = (1 << 1),
734         FTRACE_ITER_NOTRACE     = (1 << 2),
735 };
736
737 #define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
738
739 struct ftrace_iterator {
740         loff_t                  pos;
741         struct ftrace_page      *pg;
742         unsigned                idx;
743         unsigned                flags;
744         unsigned char           buffer[FTRACE_BUFF_MAX+1];
745         unsigned                buffer_idx;
746         unsigned                filtered;
747 };
748
749 static void *
750 t_next(struct seq_file *m, void *v, loff_t *pos)
751 {
752         struct ftrace_iterator *iter = m->private;
753         struct dyn_ftrace *rec = NULL;
754
755         (*pos)++;
756
757  retry:
758         if (iter->idx >= iter->pg->index) {
759                 if (iter->pg->next) {
760                         iter->pg = iter->pg->next;
761                         iter->idx = 0;
762                         goto retry;
763                 }
764         } else {
765                 rec = &iter->pg->records[iter->idx++];
766                 if ((rec->flags & FTRACE_FL_FAILED) ||
767                     ((iter->flags & FTRACE_ITER_FILTER) &&
768                      !(rec->flags & FTRACE_FL_FILTER)) ||
769                     ((iter->flags & FTRACE_ITER_NOTRACE) &&
770                      !(rec->flags & FTRACE_FL_NOTRACE))) {
771                         rec = NULL;
772                         goto retry;
773                 }
774         }
775
776         iter->pos = *pos;
777
778         return rec;
779 }
780
781 static void *t_start(struct seq_file *m, loff_t *pos)
782 {
783         struct ftrace_iterator *iter = m->private;
784         void *p = NULL;
785         loff_t l = -1;
786
787         if (*pos != iter->pos) {
788                 for (p = t_next(m, p, &l); p && l < *pos; p = t_next(m, p, &l))
789                         ;
790         } else {
791                 l = *pos;
792                 p = t_next(m, p, &l);
793         }
794
795         return p;
796 }
797
798 static void t_stop(struct seq_file *m, void *p)
799 {
800 }
801
802 static int t_show(struct seq_file *m, void *v)
803 {
804         struct dyn_ftrace *rec = v;
805         char str[KSYM_SYMBOL_LEN];
806
807         if (!rec)
808                 return 0;
809
810         kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
811
812         seq_printf(m, "%s\n", str);
813
814         return 0;
815 }
816
817 static struct seq_operations show_ftrace_seq_ops = {
818         .start = t_start,
819         .next = t_next,
820         .stop = t_stop,
821         .show = t_show,
822 };
823
824 static int
825 ftrace_avail_open(struct inode *inode, struct file *file)
826 {
827         struct ftrace_iterator *iter;
828         int ret;
829
830         if (unlikely(ftrace_disabled))
831                 return -ENODEV;
832
833         iter = kzalloc(sizeof(*iter), GFP_KERNEL);
834         if (!iter)
835                 return -ENOMEM;
836
837         iter->pg = ftrace_pages_start;
838         iter->pos = -1;
839
840         ret = seq_open(file, &show_ftrace_seq_ops);
841         if (!ret) {
842                 struct seq_file *m = file->private_data;
843
844                 m->private = iter;
845         } else {
846                 kfree(iter);
847         }
848
849         return ret;
850 }
851
852 int ftrace_avail_release(struct inode *inode, struct file *file)
853 {
854         struct seq_file *m = (struct seq_file *)file->private_data;
855         struct ftrace_iterator *iter = m->private;
856
857         seq_release(inode, file);
858         kfree(iter);
859
860         return 0;
861 }
862
863 static void ftrace_filter_reset(int enable)
864 {
865         struct ftrace_page *pg;
866         struct dyn_ftrace *rec;
867         unsigned long type = enable ? FTRACE_FL_FILTER : FTRACE_FL_NOTRACE;
868         unsigned i;
869
870         /* keep kstop machine from running */
871         preempt_disable();
872         if (enable)
873                 ftrace_filtered = 0;
874         pg = ftrace_pages_start;
875         while (pg) {
876                 for (i = 0; i < pg->index; i++) {
877                         rec = &pg->records[i];
878                         if (rec->flags & FTRACE_FL_FAILED)
879                                 continue;
880                         rec->flags &= ~type;
881                 }
882                 pg = pg->next;
883         }
884         preempt_enable();
885 }
886
887 static int
888 ftrace_regex_open(struct inode *inode, struct file *file, int enable)
889 {
890         struct ftrace_iterator *iter;
891         int ret = 0;
892
893         if (unlikely(ftrace_disabled))
894                 return -ENODEV;
895
896         iter = kzalloc(sizeof(*iter), GFP_KERNEL);
897         if (!iter)
898                 return -ENOMEM;
899
900         mutex_lock(&ftrace_regex_lock);
901         if ((file->f_mode & FMODE_WRITE) &&
902             !(file->f_flags & O_APPEND))
903                 ftrace_filter_reset(enable);
904
905         if (file->f_mode & FMODE_READ) {
906                 iter->pg = ftrace_pages_start;
907                 iter->pos = -1;
908                 iter->flags = enable ? FTRACE_ITER_FILTER :
909                         FTRACE_ITER_NOTRACE;
910
911                 ret = seq_open(file, &show_ftrace_seq_ops);
912                 if (!ret) {
913                         struct seq_file *m = file->private_data;
914                         m->private = iter;
915                 } else
916                         kfree(iter);
917         } else
918                 file->private_data = iter;
919         mutex_unlock(&ftrace_regex_lock);
920
921         return ret;
922 }
923
924 static int
925 ftrace_filter_open(struct inode *inode, struct file *file)
926 {
927         return ftrace_regex_open(inode, file, 1);
928 }
929
930 static int
931 ftrace_notrace_open(struct inode *inode, struct file *file)
932 {
933         return ftrace_regex_open(inode, file, 0);
934 }
935
936 static ssize_t
937 ftrace_regex_read(struct file *file, char __user *ubuf,
938                        size_t cnt, loff_t *ppos)
939 {
940         if (file->f_mode & FMODE_READ)
941                 return seq_read(file, ubuf, cnt, ppos);
942         else
943                 return -EPERM;
944 }
945
946 static loff_t
947 ftrace_regex_lseek(struct file *file, loff_t offset, int origin)
948 {
949         loff_t ret;
950
951         if (file->f_mode & FMODE_READ)
952                 ret = seq_lseek(file, offset, origin);
953         else
954                 file->f_pos = ret = 1;
955
956         return ret;
957 }
958
959 enum {
960         MATCH_FULL,
961         MATCH_FRONT_ONLY,
962         MATCH_MIDDLE_ONLY,
963         MATCH_END_ONLY,
964 };
965
966 static void
967 ftrace_match(unsigned char *buff, int len, int enable)
968 {
969         char str[KSYM_SYMBOL_LEN];
970         char *search = NULL;
971         struct ftrace_page *pg;
972         struct dyn_ftrace *rec;
973         int type = MATCH_FULL;
974         unsigned long flag = enable ? FTRACE_FL_FILTER : FTRACE_FL_NOTRACE;
975         unsigned i, match = 0, search_len = 0;
976
977         for (i = 0; i < len; i++) {
978                 if (buff[i] == '*') {
979                         if (!i) {
980                                 search = buff + i + 1;
981                                 type = MATCH_END_ONLY;
982                                 search_len = len - (i + 1);
983                         } else {
984                                 if (type == MATCH_END_ONLY) {
985                                         type = MATCH_MIDDLE_ONLY;
986                                 } else {
987                                         match = i;
988                                         type = MATCH_FRONT_ONLY;
989                                 }
990                                 buff[i] = 0;
991                                 break;
992                         }
993                 }
994         }
995
996         /* keep kstop machine from running */
997         preempt_disable();
998         if (enable)
999                 ftrace_filtered = 1;
1000         pg = ftrace_pages_start;
1001         while (pg) {
1002                 for (i = 0; i < pg->index; i++) {
1003                         int matched = 0;
1004                         char *ptr;
1005
1006                         rec = &pg->records[i];
1007                         if (rec->flags & FTRACE_FL_FAILED)
1008                                 continue;
1009                         kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
1010                         switch (type) {
1011                         case MATCH_FULL:
1012                                 if (strcmp(str, buff) == 0)
1013                                         matched = 1;
1014                                 break;
1015                         case MATCH_FRONT_ONLY:
1016                                 if (memcmp(str, buff, match) == 0)
1017                                         matched = 1;
1018                                 break;
1019                         case MATCH_MIDDLE_ONLY:
1020                                 if (strstr(str, search))
1021                                         matched = 1;
1022                                 break;
1023                         case MATCH_END_ONLY:
1024                                 ptr = strstr(str, search);
1025                                 if (ptr && (ptr[search_len] == 0))
1026                                         matched = 1;
1027                                 break;
1028                         }
1029                         if (matched)
1030                                 rec->flags |= flag;
1031                 }
1032                 pg = pg->next;
1033         }
1034         preempt_enable();
1035 }
1036
1037 static ssize_t
1038 ftrace_regex_write(struct file *file, const char __user *ubuf,
1039                    size_t cnt, loff_t *ppos, int enable)
1040 {
1041         struct ftrace_iterator *iter;
1042         char ch;
1043         size_t read = 0;
1044         ssize_t ret;
1045
1046         if (!cnt || cnt < 0)
1047                 return 0;
1048
1049         mutex_lock(&ftrace_regex_lock);
1050
1051         if (file->f_mode & FMODE_READ) {
1052                 struct seq_file *m = file->private_data;
1053                 iter = m->private;
1054         } else
1055                 iter = file->private_data;
1056
1057         if (!*ppos) {
1058                 iter->flags &= ~FTRACE_ITER_CONT;
1059                 iter->buffer_idx = 0;
1060         }
1061
1062         ret = get_user(ch, ubuf++);
1063         if (ret)
1064                 goto out;
1065         read++;
1066         cnt--;
1067
1068         if (!(iter->flags & ~FTRACE_ITER_CONT)) {
1069                 /* skip white space */
1070                 while (cnt && isspace(ch)) {
1071                         ret = get_user(ch, ubuf++);
1072                         if (ret)
1073                                 goto out;
1074                         read++;
1075                         cnt--;
1076                 }
1077
1078                 if (isspace(ch)) {
1079                         file->f_pos += read;
1080                         ret = read;
1081                         goto out;
1082                 }
1083
1084                 iter->buffer_idx = 0;
1085         }
1086
1087         while (cnt && !isspace(ch)) {
1088                 if (iter->buffer_idx < FTRACE_BUFF_MAX)
1089                         iter->buffer[iter->buffer_idx++] = ch;
1090                 else {
1091                         ret = -EINVAL;
1092                         goto out;
1093                 }
1094                 ret = get_user(ch, ubuf++);
1095                 if (ret)
1096                         goto out;
1097                 read++;
1098                 cnt--;
1099         }
1100
1101         if (isspace(ch)) {
1102                 iter->filtered++;
1103                 iter->buffer[iter->buffer_idx] = 0;
1104                 ftrace_match(iter->buffer, iter->buffer_idx, enable);
1105                 iter->buffer_idx = 0;
1106         } else
1107                 iter->flags |= FTRACE_ITER_CONT;
1108
1109
1110         file->f_pos += read;
1111
1112         ret = read;
1113  out:
1114         mutex_unlock(&ftrace_regex_lock);
1115
1116         return ret;
1117 }
1118
1119 static ssize_t
1120 ftrace_filter_write(struct file *file, const char __user *ubuf,
1121                     size_t cnt, loff_t *ppos)
1122 {
1123         return ftrace_regex_write(file, ubuf, cnt, ppos, 1);
1124 }
1125
1126 static ssize_t
1127 ftrace_notrace_write(struct file *file, const char __user *ubuf,
1128                      size_t cnt, loff_t *ppos)
1129 {
1130         return ftrace_regex_write(file, ubuf, cnt, ppos, 0);
1131 }
1132
1133 static void
1134 ftrace_set_regex(unsigned char *buf, int len, int reset, int enable)
1135 {
1136         if (unlikely(ftrace_disabled))
1137                 return;
1138
1139         mutex_lock(&ftrace_regex_lock);
1140         if (reset)
1141                 ftrace_filter_reset(enable);
1142         if (buf)
1143                 ftrace_match(buf, len, enable);
1144         mutex_unlock(&ftrace_regex_lock);
1145 }
1146
1147 /**
1148  * ftrace_set_filter - set a function to filter on in ftrace
1149  * @buf - the string that holds the function filter text.
1150  * @len - the length of the string.
1151  * @reset - non zero to reset all filters before applying this filter.
1152  *
1153  * Filters denote which functions should be enabled when tracing is enabled.
1154  * If @buf is NULL and reset is set, all functions will be enabled for tracing.
1155  */
1156 void ftrace_set_filter(unsigned char *buf, int len, int reset)
1157 {
1158         ftrace_set_regex(buf, len, reset, 1);
1159 }
1160
1161 /**
1162  * ftrace_set_notrace - set a function to not trace in ftrace
1163  * @buf - the string that holds the function notrace text.
1164  * @len - the length of the string.
1165  * @reset - non zero to reset all filters before applying this filter.
1166  *
1167  * Notrace Filters denote which functions should not be enabled when tracing
1168  * is enabled. If @buf is NULL and reset is set, all functions will be enabled
1169  * for tracing.
1170  */
1171 void ftrace_set_notrace(unsigned char *buf, int len, int reset)
1172 {
1173         ftrace_set_regex(buf, len, reset, 0);
1174 }
1175
1176 static int
1177 ftrace_regex_release(struct inode *inode, struct file *file, int enable)
1178 {
1179         struct seq_file *m = (struct seq_file *)file->private_data;
1180         struct ftrace_iterator *iter;
1181
1182         mutex_lock(&ftrace_regex_lock);
1183         if (file->f_mode & FMODE_READ) {
1184                 iter = m->private;
1185
1186                 seq_release(inode, file);
1187         } else
1188                 iter = file->private_data;
1189
1190         if (iter->buffer_idx) {
1191                 iter->filtered++;
1192                 iter->buffer[iter->buffer_idx] = 0;
1193                 ftrace_match(iter->buffer, iter->buffer_idx, enable);
1194         }
1195
1196         mutex_lock(&ftrace_sysctl_lock);
1197         mutex_lock(&ftraced_lock);
1198         if (iter->filtered && ftraced_suspend && ftrace_enabled)
1199                 ftrace_run_update_code(FTRACE_ENABLE_CALLS);
1200         mutex_unlock(&ftraced_lock);
1201         mutex_unlock(&ftrace_sysctl_lock);
1202
1203         kfree(iter);
1204         mutex_unlock(&ftrace_regex_lock);
1205         return 0;
1206 }
1207
1208 static int
1209 ftrace_filter_release(struct inode *inode, struct file *file)
1210 {
1211         return ftrace_regex_release(inode, file, 1);
1212 }
1213
1214 static int
1215 ftrace_notrace_release(struct inode *inode, struct file *file)
1216 {
1217         return ftrace_regex_release(inode, file, 0);
1218 }
1219
1220 static struct file_operations ftrace_avail_fops = {
1221         .open = ftrace_avail_open,
1222         .read = seq_read,
1223         .llseek = seq_lseek,
1224         .release = ftrace_avail_release,
1225 };
1226
1227 static struct file_operations ftrace_filter_fops = {
1228         .open = ftrace_filter_open,
1229         .read = ftrace_regex_read,
1230         .write = ftrace_filter_write,
1231         .llseek = ftrace_regex_lseek,
1232         .release = ftrace_filter_release,
1233 };
1234
1235 static struct file_operations ftrace_notrace_fops = {
1236         .open = ftrace_notrace_open,
1237         .read = ftrace_regex_read,
1238         .write = ftrace_notrace_write,
1239         .llseek = ftrace_regex_lseek,
1240         .release = ftrace_notrace_release,
1241 };
1242
1243 /**
1244  * ftrace_force_update - force an update to all recording ftrace functions
1245  *
1246  * The ftrace dynamic update daemon only wakes up once a second.
1247  * There may be cases where an update needs to be done immediately
1248  * for tests or internal kernel tracing to begin. This function
1249  * wakes the daemon to do an update and will not return until the
1250  * update is complete.
1251  */
1252 int ftrace_force_update(void)
1253 {
1254         unsigned long last_counter;
1255         DECLARE_WAITQUEUE(wait, current);
1256         int ret = 0;
1257
1258         if (unlikely(ftrace_disabled))
1259                 return -ENODEV;
1260
1261         mutex_lock(&ftraced_lock);
1262         last_counter = ftraced_iteration_counter;
1263
1264         set_current_state(TASK_INTERRUPTIBLE);
1265         add_wait_queue(&ftraced_waiters, &wait);
1266
1267         if (unlikely(!ftraced_task)) {
1268                 ret = -ENODEV;
1269                 goto out;
1270         }
1271
1272         do {
1273                 mutex_unlock(&ftraced_lock);
1274                 wake_up_process(ftraced_task);
1275                 schedule();
1276                 mutex_lock(&ftraced_lock);
1277                 if (signal_pending(current)) {
1278                         ret = -EINTR;
1279                         break;
1280                 }
1281                 set_current_state(TASK_INTERRUPTIBLE);
1282         } while (last_counter == ftraced_iteration_counter);
1283
1284  out:
1285         mutex_unlock(&ftraced_lock);
1286         remove_wait_queue(&ftraced_waiters, &wait);
1287         set_current_state(TASK_RUNNING);
1288
1289         return ret;
1290 }
1291
1292 static void ftrace_force_shutdown(void)
1293 {
1294         struct task_struct *task;
1295         int command = FTRACE_DISABLE_CALLS | FTRACE_UPDATE_TRACE_FUNC;
1296
1297         mutex_lock(&ftraced_lock);
1298         task = ftraced_task;
1299         ftraced_task = NULL;
1300         ftraced_suspend = -1;
1301         ftrace_run_update_code(command);
1302         mutex_unlock(&ftraced_lock);
1303
1304         if (task)
1305                 kthread_stop(task);
1306 }
1307
1308 static __init int ftrace_init_debugfs(void)
1309 {
1310         struct dentry *d_tracer;
1311         struct dentry *entry;
1312
1313         d_tracer = tracing_init_dentry();
1314
1315         entry = debugfs_create_file("available_filter_functions", 0444,
1316                                     d_tracer, NULL, &ftrace_avail_fops);
1317         if (!entry)
1318                 pr_warning("Could not create debugfs "
1319                            "'available_filter_functions' entry\n");
1320
1321         entry = debugfs_create_file("set_ftrace_filter", 0644, d_tracer,
1322                                     NULL, &ftrace_filter_fops);
1323         if (!entry)
1324                 pr_warning("Could not create debugfs "
1325                            "'set_ftrace_filter' entry\n");
1326
1327         entry = debugfs_create_file("set_ftrace_notrace", 0644, d_tracer,
1328                                     NULL, &ftrace_notrace_fops);
1329         if (!entry)
1330                 pr_warning("Could not create debugfs "
1331                            "'set_ftrace_notrace' entry\n");
1332         return 0;
1333 }
1334
1335 fs_initcall(ftrace_init_debugfs);
1336
1337 static int __init ftrace_dynamic_init(void)
1338 {
1339         struct task_struct *p;
1340         unsigned long addr;
1341         int ret;
1342
1343         addr = (unsigned long)ftrace_record_ip;
1344
1345         stop_machine_run(ftrace_dyn_arch_init, &addr, NR_CPUS);
1346
1347         /* ftrace_dyn_arch_init places the return code in addr */
1348         if (addr) {
1349                 ret = (int)addr;
1350                 goto failed;
1351         }
1352
1353         ret = ftrace_dyn_table_alloc();
1354         if (ret)
1355                 goto failed;
1356
1357         p = kthread_run(ftraced, NULL, "ftraced");
1358         if (IS_ERR(p)) {
1359                 ret = -1;
1360                 goto failed;
1361         }
1362
1363         last_ftrace_enabled = ftrace_enabled = 1;
1364         ftraced_task = p;
1365
1366         return 0;
1367
1368  failed:
1369         ftrace_disabled = 1;
1370         return ret;
1371 }
1372
1373 core_initcall(ftrace_dynamic_init);
1374 #else
1375 # define ftrace_startup()               do { } while (0)
1376 # define ftrace_shutdown()              do { } while (0)
1377 # define ftrace_startup_sysctl()        do { } while (0)
1378 # define ftrace_shutdown_sysctl()       do { } while (0)
1379 # define ftrace_force_shutdown()        do { } while (0)
1380 #endif /* CONFIG_DYNAMIC_FTRACE */
1381
1382 /**
1383  * ftrace_kill - totally shutdown ftrace
1384  *
1385  * This is a safety measure. If something was detected that seems
1386  * wrong, calling this function will keep ftrace from doing
1387  * any more modifications, and updates.
1388  * used when something went wrong.
1389  */
1390 void ftrace_kill(void)
1391 {
1392         mutex_lock(&ftrace_sysctl_lock);
1393         ftrace_disabled = 1;
1394         ftrace_enabled = 0;
1395
1396         clear_ftrace_function();
1397         mutex_unlock(&ftrace_sysctl_lock);
1398
1399         /* Try to totally disable ftrace */
1400         ftrace_force_shutdown();
1401 }
1402
1403 /**
1404  * register_ftrace_function - register a function for profiling
1405  * @ops - ops structure that holds the function for profiling.
1406  *
1407  * Register a function to be called by all functions in the
1408  * kernel.
1409  *
1410  * Note: @ops->func and all the functions it calls must be labeled
1411  *       with "notrace", otherwise it will go into a
1412  *       recursive loop.
1413  */
1414 int register_ftrace_function(struct ftrace_ops *ops)
1415 {
1416         int ret;
1417
1418         if (unlikely(ftrace_disabled))
1419                 return -1;
1420
1421         mutex_lock(&ftrace_sysctl_lock);
1422         ret = __register_ftrace_function(ops);
1423         ftrace_startup();
1424         mutex_unlock(&ftrace_sysctl_lock);
1425
1426         return ret;
1427 }
1428
1429 /**
1430  * unregister_ftrace_function - unresgister a function for profiling.
1431  * @ops - ops structure that holds the function to unregister
1432  *
1433  * Unregister a function that was added to be called by ftrace profiling.
1434  */
1435 int unregister_ftrace_function(struct ftrace_ops *ops)
1436 {
1437         int ret;
1438
1439         mutex_lock(&ftrace_sysctl_lock);
1440         ret = __unregister_ftrace_function(ops);
1441         ftrace_shutdown();
1442         mutex_unlock(&ftrace_sysctl_lock);
1443
1444         return ret;
1445 }
1446
1447 int
1448 ftrace_enable_sysctl(struct ctl_table *table, int write,
1449                      struct file *file, void __user *buffer, size_t *lenp,
1450                      loff_t *ppos)
1451 {
1452         int ret;
1453
1454         if (unlikely(ftrace_disabled))
1455                 return -ENODEV;
1456
1457         mutex_lock(&ftrace_sysctl_lock);
1458
1459         ret  = proc_dointvec(table, write, file, buffer, lenp, ppos);
1460
1461         if (ret || !write || (last_ftrace_enabled == ftrace_enabled))
1462                 goto out;
1463
1464         last_ftrace_enabled = ftrace_enabled;
1465
1466         if (ftrace_enabled) {
1467
1468                 ftrace_startup_sysctl();
1469
1470                 /* we are starting ftrace again */
1471                 if (ftrace_list != &ftrace_list_end) {
1472                         if (ftrace_list->next == &ftrace_list_end)
1473                                 ftrace_trace_function = ftrace_list->func;
1474                         else
1475                                 ftrace_trace_function = ftrace_list_func;
1476                 }
1477
1478         } else {
1479                 /* stopping ftrace calls (just send to ftrace_stub) */
1480                 ftrace_trace_function = ftrace_stub;
1481
1482                 ftrace_shutdown_sysctl();
1483         }
1484
1485  out:
1486         mutex_unlock(&ftrace_sysctl_lock);
1487         return ret;
1488 }