tracing: Check all tasks on each CPU when filtering pids
[linux-2.6-block.git] / kernel / trace / trace_events.c
1 /*
2  * event tracer
3  *
4  * Copyright (C) 2008 Red Hat Inc, Steven Rostedt <srostedt@redhat.com>
5  *
6  *  - Added format output of fields of the trace point.
7  *    This was based off of work by Tom Zanussi <tzanussi@gmail.com>.
8  *
9  */
10
11 #define pr_fmt(fmt) fmt
12
13 #include <linux/workqueue.h>
14 #include <linux/spinlock.h>
15 #include <linux/kthread.h>
16 #include <linux/tracefs.h>
17 #include <linux/uaccess.h>
18 #include <linux/bsearch.h>
19 #include <linux/module.h>
20 #include <linux/ctype.h>
21 #include <linux/sort.h>
22 #include <linux/slab.h>
23 #include <linux/delay.h>
24
25 #include <trace/events/sched.h>
26
27 #include <asm/setup.h>
28
29 #include "trace_output.h"
30
31 #undef TRACE_SYSTEM
32 #define TRACE_SYSTEM "TRACE_SYSTEM"
33
34 DEFINE_MUTEX(event_mutex);
35
36 LIST_HEAD(ftrace_events);
37 static LIST_HEAD(ftrace_generic_fields);
38 static LIST_HEAD(ftrace_common_fields);
39
40 #define GFP_TRACE (GFP_KERNEL | __GFP_ZERO)
41
42 static struct kmem_cache *field_cachep;
43 static struct kmem_cache *file_cachep;
44
45 static inline int system_refcount(struct event_subsystem *system)
46 {
47         return system->ref_count;
48 }
49
50 static int system_refcount_inc(struct event_subsystem *system)
51 {
52         return system->ref_count++;
53 }
54
55 static int system_refcount_dec(struct event_subsystem *system)
56 {
57         return --system->ref_count;
58 }
59
60 /* Double loops, do not use break, only goto's work */
61 #define do_for_each_event_file(tr, file)                        \
62         list_for_each_entry(tr, &ftrace_trace_arrays, list) {   \
63                 list_for_each_entry(file, &tr->events, list)
64
65 #define do_for_each_event_file_safe(tr, file)                   \
66         list_for_each_entry(tr, &ftrace_trace_arrays, list) {   \
67                 struct trace_event_file *___n;                          \
68                 list_for_each_entry_safe(file, ___n, &tr->events, list)
69
70 #define while_for_each_event_file()             \
71         }
72
73 static struct list_head *
74 trace_get_fields(struct trace_event_call *event_call)
75 {
76         if (!event_call->class->get_fields)
77                 return &event_call->class->fields;
78         return event_call->class->get_fields(event_call);
79 }
80
81 static struct ftrace_event_field *
82 __find_event_field(struct list_head *head, char *name)
83 {
84         struct ftrace_event_field *field;
85
86         list_for_each_entry(field, head, link) {
87                 if (!strcmp(field->name, name))
88                         return field;
89         }
90
91         return NULL;
92 }
93
94 struct ftrace_event_field *
95 trace_find_event_field(struct trace_event_call *call, char *name)
96 {
97         struct ftrace_event_field *field;
98         struct list_head *head;
99
100         field = __find_event_field(&ftrace_generic_fields, name);
101         if (field)
102                 return field;
103
104         field = __find_event_field(&ftrace_common_fields, name);
105         if (field)
106                 return field;
107
108         head = trace_get_fields(call);
109         return __find_event_field(head, name);
110 }
111
112 static int __trace_define_field(struct list_head *head, const char *type,
113                                 const char *name, int offset, int size,
114                                 int is_signed, int filter_type)
115 {
116         struct ftrace_event_field *field;
117
118         field = kmem_cache_alloc(field_cachep, GFP_TRACE);
119         if (!field)
120                 return -ENOMEM;
121
122         field->name = name;
123         field->type = type;
124
125         if (filter_type == FILTER_OTHER)
126                 field->filter_type = filter_assign_type(type);
127         else
128                 field->filter_type = filter_type;
129
130         field->offset = offset;
131         field->size = size;
132         field->is_signed = is_signed;
133
134         list_add(&field->link, head);
135
136         return 0;
137 }
138
139 int trace_define_field(struct trace_event_call *call, const char *type,
140                        const char *name, int offset, int size, int is_signed,
141                        int filter_type)
142 {
143         struct list_head *head;
144
145         if (WARN_ON(!call->class))
146                 return 0;
147
148         head = trace_get_fields(call);
149         return __trace_define_field(head, type, name, offset, size,
150                                     is_signed, filter_type);
151 }
152 EXPORT_SYMBOL_GPL(trace_define_field);
153
154 #define __generic_field(type, item, filter_type)                        \
155         ret = __trace_define_field(&ftrace_generic_fields, #type,       \
156                                    #item, 0, 0, is_signed_type(type),   \
157                                    filter_type);                        \
158         if (ret)                                                        \
159                 return ret;
160
161 #define __common_field(type, item)                                      \
162         ret = __trace_define_field(&ftrace_common_fields, #type,        \
163                                    "common_" #item,                     \
164                                    offsetof(typeof(ent), item),         \
165                                    sizeof(ent.item),                    \
166                                    is_signed_type(type), FILTER_OTHER); \
167         if (ret)                                                        \
168                 return ret;
169
170 static int trace_define_generic_fields(void)
171 {
172         int ret;
173
174         __generic_field(int, cpu, FILTER_OTHER);
175         __generic_field(char *, comm, FILTER_PTR_STRING);
176
177         return ret;
178 }
179
180 static int trace_define_common_fields(void)
181 {
182         int ret;
183         struct trace_entry ent;
184
185         __common_field(unsigned short, type);
186         __common_field(unsigned char, flags);
187         __common_field(unsigned char, preempt_count);
188         __common_field(int, pid);
189
190         return ret;
191 }
192
193 static void trace_destroy_fields(struct trace_event_call *call)
194 {
195         struct ftrace_event_field *field, *next;
196         struct list_head *head;
197
198         head = trace_get_fields(call);
199         list_for_each_entry_safe(field, next, head, link) {
200                 list_del(&field->link);
201                 kmem_cache_free(field_cachep, field);
202         }
203 }
204
205 int trace_event_raw_init(struct trace_event_call *call)
206 {
207         int id;
208
209         id = register_trace_event(&call->event);
210         if (!id)
211                 return -ENODEV;
212
213         return 0;
214 }
215 EXPORT_SYMBOL_GPL(trace_event_raw_init);
216
217 bool trace_event_ignore_this_pid(struct trace_event_file *trace_file)
218 {
219         struct trace_array *tr = trace_file->tr;
220         struct trace_array_cpu *data;
221         struct trace_pid_list *pid_list;
222
223         pid_list = rcu_dereference_sched(tr->filtered_pids);
224         if (!pid_list)
225                 return false;
226
227         data = this_cpu_ptr(tr->trace_buffer.data);
228
229         return data->ignore_pid;
230 }
231 EXPORT_SYMBOL_GPL(trace_event_ignore_this_pid);
232
233 void *trace_event_buffer_reserve(struct trace_event_buffer *fbuffer,
234                                  struct trace_event_file *trace_file,
235                                  unsigned long len)
236 {
237         struct trace_event_call *event_call = trace_file->event_call;
238
239         if ((trace_file->flags & EVENT_FILE_FL_PID_FILTER) &&
240             trace_event_ignore_this_pid(trace_file))
241                 return NULL;
242
243         local_save_flags(fbuffer->flags);
244         fbuffer->pc = preempt_count();
245         fbuffer->trace_file = trace_file;
246
247         fbuffer->event =
248                 trace_event_buffer_lock_reserve(&fbuffer->buffer, trace_file,
249                                                 event_call->event.type, len,
250                                                 fbuffer->flags, fbuffer->pc);
251         if (!fbuffer->event)
252                 return NULL;
253
254         fbuffer->entry = ring_buffer_event_data(fbuffer->event);
255         return fbuffer->entry;
256 }
257 EXPORT_SYMBOL_GPL(trace_event_buffer_reserve);
258
259 static DEFINE_SPINLOCK(tracepoint_iter_lock);
260
261 static void output_printk(struct trace_event_buffer *fbuffer)
262 {
263         struct trace_event_call *event_call;
264         struct trace_event *event;
265         unsigned long flags;
266         struct trace_iterator *iter = tracepoint_print_iter;
267
268         if (!iter)
269                 return;
270
271         event_call = fbuffer->trace_file->event_call;
272         if (!event_call || !event_call->event.funcs ||
273             !event_call->event.funcs->trace)
274                 return;
275
276         event = &fbuffer->trace_file->event_call->event;
277
278         spin_lock_irqsave(&tracepoint_iter_lock, flags);
279         trace_seq_init(&iter->seq);
280         iter->ent = fbuffer->entry;
281         event_call->event.funcs->trace(iter, 0, event);
282         trace_seq_putc(&iter->seq, 0);
283         printk("%s", iter->seq.buffer);
284
285         spin_unlock_irqrestore(&tracepoint_iter_lock, flags);
286 }
287
288 void trace_event_buffer_commit(struct trace_event_buffer *fbuffer)
289 {
290         if (tracepoint_printk)
291                 output_printk(fbuffer);
292
293         event_trigger_unlock_commit(fbuffer->trace_file, fbuffer->buffer,
294                                     fbuffer->event, fbuffer->entry,
295                                     fbuffer->flags, fbuffer->pc);
296 }
297 EXPORT_SYMBOL_GPL(trace_event_buffer_commit);
298
299 int trace_event_reg(struct trace_event_call *call,
300                     enum trace_reg type, void *data)
301 {
302         struct trace_event_file *file = data;
303
304         WARN_ON(!(call->flags & TRACE_EVENT_FL_TRACEPOINT));
305         switch (type) {
306         case TRACE_REG_REGISTER:
307                 return tracepoint_probe_register(call->tp,
308                                                  call->class->probe,
309                                                  file);
310         case TRACE_REG_UNREGISTER:
311                 tracepoint_probe_unregister(call->tp,
312                                             call->class->probe,
313                                             file);
314                 return 0;
315
316 #ifdef CONFIG_PERF_EVENTS
317         case TRACE_REG_PERF_REGISTER:
318                 return tracepoint_probe_register(call->tp,
319                                                  call->class->perf_probe,
320                                                  call);
321         case TRACE_REG_PERF_UNREGISTER:
322                 tracepoint_probe_unregister(call->tp,
323                                             call->class->perf_probe,
324                                             call);
325                 return 0;
326         case TRACE_REG_PERF_OPEN:
327         case TRACE_REG_PERF_CLOSE:
328         case TRACE_REG_PERF_ADD:
329         case TRACE_REG_PERF_DEL:
330                 return 0;
331 #endif
332         }
333         return 0;
334 }
335 EXPORT_SYMBOL_GPL(trace_event_reg);
336
337 void trace_event_enable_cmd_record(bool enable)
338 {
339         struct trace_event_file *file;
340         struct trace_array *tr;
341
342         mutex_lock(&event_mutex);
343         do_for_each_event_file(tr, file) {
344
345                 if (!(file->flags & EVENT_FILE_FL_ENABLED))
346                         continue;
347
348                 if (enable) {
349                         tracing_start_cmdline_record();
350                         set_bit(EVENT_FILE_FL_RECORDED_CMD_BIT, &file->flags);
351                 } else {
352                         tracing_stop_cmdline_record();
353                         clear_bit(EVENT_FILE_FL_RECORDED_CMD_BIT, &file->flags);
354                 }
355         } while_for_each_event_file();
356         mutex_unlock(&event_mutex);
357 }
358
359 static int __ftrace_event_enable_disable(struct trace_event_file *file,
360                                          int enable, int soft_disable)
361 {
362         struct trace_event_call *call = file->event_call;
363         struct trace_array *tr = file->tr;
364         int ret = 0;
365         int disable;
366
367         switch (enable) {
368         case 0:
369                 /*
370                  * When soft_disable is set and enable is cleared, the sm_ref
371                  * reference counter is decremented. If it reaches 0, we want
372                  * to clear the SOFT_DISABLED flag but leave the event in the
373                  * state that it was. That is, if the event was enabled and
374                  * SOFT_DISABLED isn't set, then do nothing. But if SOFT_DISABLED
375                  * is set we do not want the event to be enabled before we
376                  * clear the bit.
377                  *
378                  * When soft_disable is not set but the SOFT_MODE flag is,
379                  * we do nothing. Do not disable the tracepoint, otherwise
380                  * "soft enable"s (clearing the SOFT_DISABLED bit) wont work.
381                  */
382                 if (soft_disable) {
383                         if (atomic_dec_return(&file->sm_ref) > 0)
384                                 break;
385                         disable = file->flags & EVENT_FILE_FL_SOFT_DISABLED;
386                         clear_bit(EVENT_FILE_FL_SOFT_MODE_BIT, &file->flags);
387                 } else
388                         disable = !(file->flags & EVENT_FILE_FL_SOFT_MODE);
389
390                 if (disable && (file->flags & EVENT_FILE_FL_ENABLED)) {
391                         clear_bit(EVENT_FILE_FL_ENABLED_BIT, &file->flags);
392                         if (file->flags & EVENT_FILE_FL_RECORDED_CMD) {
393                                 tracing_stop_cmdline_record();
394                                 clear_bit(EVENT_FILE_FL_RECORDED_CMD_BIT, &file->flags);
395                         }
396                         call->class->reg(call, TRACE_REG_UNREGISTER, file);
397                 }
398                 /* If in SOFT_MODE, just set the SOFT_DISABLE_BIT, else clear it */
399                 if (file->flags & EVENT_FILE_FL_SOFT_MODE)
400                         set_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &file->flags);
401                 else
402                         clear_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &file->flags);
403                 break;
404         case 1:
405                 /*
406                  * When soft_disable is set and enable is set, we want to
407                  * register the tracepoint for the event, but leave the event
408                  * as is. That means, if the event was already enabled, we do
409                  * nothing (but set SOFT_MODE). If the event is disabled, we
410                  * set SOFT_DISABLED before enabling the event tracepoint, so
411                  * it still seems to be disabled.
412                  */
413                 if (!soft_disable)
414                         clear_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &file->flags);
415                 else {
416                         if (atomic_inc_return(&file->sm_ref) > 1)
417                                 break;
418                         set_bit(EVENT_FILE_FL_SOFT_MODE_BIT, &file->flags);
419                 }
420
421                 if (!(file->flags & EVENT_FILE_FL_ENABLED)) {
422
423                         /* Keep the event disabled, when going to SOFT_MODE. */
424                         if (soft_disable)
425                                 set_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &file->flags);
426
427                         if (tr->trace_flags & TRACE_ITER_RECORD_CMD) {
428                                 tracing_start_cmdline_record();
429                                 set_bit(EVENT_FILE_FL_RECORDED_CMD_BIT, &file->flags);
430                         }
431                         ret = call->class->reg(call, TRACE_REG_REGISTER, file);
432                         if (ret) {
433                                 tracing_stop_cmdline_record();
434                                 pr_info("event trace: Could not enable event "
435                                         "%s\n", trace_event_name(call));
436                                 break;
437                         }
438                         set_bit(EVENT_FILE_FL_ENABLED_BIT, &file->flags);
439
440                         /* WAS_ENABLED gets set but never cleared. */
441                         call->flags |= TRACE_EVENT_FL_WAS_ENABLED;
442                 }
443                 break;
444         }
445
446         return ret;
447 }
448
449 int trace_event_enable_disable(struct trace_event_file *file,
450                                int enable, int soft_disable)
451 {
452         return __ftrace_event_enable_disable(file, enable, soft_disable);
453 }
454
455 static int ftrace_event_enable_disable(struct trace_event_file *file,
456                                        int enable)
457 {
458         return __ftrace_event_enable_disable(file, enable, 0);
459 }
460
461 static void ftrace_clear_events(struct trace_array *tr)
462 {
463         struct trace_event_file *file;
464
465         mutex_lock(&event_mutex);
466         list_for_each_entry(file, &tr->events, list) {
467                 ftrace_event_enable_disable(file, 0);
468         }
469         mutex_unlock(&event_mutex);
470 }
471
472 static int cmp_pid(const void *key, const void *elt)
473 {
474         const pid_t *search_pid = key;
475         const pid_t *pid = elt;
476
477         if (*search_pid == *pid)
478                 return 0;
479         if (*search_pid < *pid)
480                 return -1;
481         return 1;
482 }
483
484 static bool
485 check_ignore_pid(struct trace_pid_list *filtered_pids, struct task_struct *task)
486 {
487         pid_t search_pid;
488         pid_t *pid;
489
490         /*
491          * Return false, because if filtered_pids does not exist,
492          * all pids are good to trace.
493          */
494         if (!filtered_pids)
495                 return false;
496
497         search_pid = task->pid;
498
499         pid = bsearch(&search_pid, filtered_pids->pids,
500                       filtered_pids->nr_pids, sizeof(pid_t),
501                       cmp_pid);
502         if (!pid)
503                 return true;
504
505         return false;
506 }
507
508 static void
509 event_filter_pid_sched_switch_probe_pre(void *data,
510                     struct task_struct *prev, struct task_struct *next)
511 {
512         struct trace_array *tr = data;
513         struct trace_pid_list *pid_list;
514
515         pid_list = rcu_dereference_sched(tr->filtered_pids);
516
517         this_cpu_write(tr->trace_buffer.data->ignore_pid,
518                        check_ignore_pid(pid_list, prev) &&
519                        check_ignore_pid(pid_list, next));
520 }
521
522 static void
523 event_filter_pid_sched_switch_probe_post(void *data,
524                     struct task_struct *prev, struct task_struct *next)
525 {
526         struct trace_array *tr = data;
527         struct trace_pid_list *pid_list;
528
529         pid_list = rcu_dereference_sched(tr->filtered_pids);
530
531         this_cpu_write(tr->trace_buffer.data->ignore_pid,
532                        check_ignore_pid(pid_list, next));
533 }
534
535 static void
536 event_filter_pid_sched_wakeup_probe_pre(void *data, struct task_struct *task)
537 {
538         struct trace_array *tr = data;
539         struct trace_pid_list *pid_list;
540
541         /* Nothing to do if we are already tracing */
542         if (!this_cpu_read(tr->trace_buffer.data->ignore_pid))
543                 return;
544
545         pid_list = rcu_dereference_sched(tr->filtered_pids);
546
547         this_cpu_write(tr->trace_buffer.data->ignore_pid,
548                        check_ignore_pid(pid_list, task));
549 }
550
551 static void
552 event_filter_pid_sched_wakeup_probe_post(void *data, struct task_struct *task)
553 {
554         struct trace_array *tr = data;
555         struct trace_pid_list *pid_list;
556
557         /* Nothing to do if we are not tracing */
558         if (this_cpu_read(tr->trace_buffer.data->ignore_pid))
559                 return;
560
561         pid_list = rcu_dereference_sched(tr->filtered_pids);
562
563         /* Set tracing if current is enabled */
564         this_cpu_write(tr->trace_buffer.data->ignore_pid,
565                        check_ignore_pid(pid_list, current));
566 }
567
568 static void __ftrace_clear_event_pids(struct trace_array *tr)
569 {
570         struct trace_pid_list *pid_list;
571         struct trace_event_file *file;
572         int cpu;
573
574         pid_list = rcu_dereference_protected(tr->filtered_pids,
575                                              lockdep_is_held(&event_mutex));
576         if (!pid_list)
577                 return;
578
579         unregister_trace_sched_switch(event_filter_pid_sched_switch_probe_pre, tr);
580         unregister_trace_sched_switch(event_filter_pid_sched_switch_probe_post, tr);
581
582         unregister_trace_sched_wakeup(event_filter_pid_sched_wakeup_probe_pre, tr);
583         unregister_trace_sched_wakeup(event_filter_pid_sched_wakeup_probe_post, tr);
584
585         list_for_each_entry(file, &tr->events, list) {
586                 clear_bit(EVENT_FILE_FL_PID_FILTER_BIT, &file->flags);
587         }
588
589         for_each_possible_cpu(cpu)
590                 per_cpu_ptr(tr->trace_buffer.data, cpu)->ignore_pid = false;
591
592         rcu_assign_pointer(tr->filtered_pids, NULL);
593
594         /* Wait till all users are no longer using pid filtering */
595         synchronize_sched();
596
597         free_pages((unsigned long)pid_list->pids, pid_list->order);
598         kfree(pid_list);
599 }
600
601 static void ftrace_clear_event_pids(struct trace_array *tr)
602 {
603         mutex_lock(&event_mutex);
604         __ftrace_clear_event_pids(tr);
605         mutex_unlock(&event_mutex);
606 }
607
608 static void __put_system(struct event_subsystem *system)
609 {
610         struct event_filter *filter = system->filter;
611
612         WARN_ON_ONCE(system_refcount(system) == 0);
613         if (system_refcount_dec(system))
614                 return;
615
616         list_del(&system->list);
617
618         if (filter) {
619                 kfree(filter->filter_string);
620                 kfree(filter);
621         }
622         kfree_const(system->name);
623         kfree(system);
624 }
625
626 static void __get_system(struct event_subsystem *system)
627 {
628         WARN_ON_ONCE(system_refcount(system) == 0);
629         system_refcount_inc(system);
630 }
631
632 static void __get_system_dir(struct trace_subsystem_dir *dir)
633 {
634         WARN_ON_ONCE(dir->ref_count == 0);
635         dir->ref_count++;
636         __get_system(dir->subsystem);
637 }
638
639 static void __put_system_dir(struct trace_subsystem_dir *dir)
640 {
641         WARN_ON_ONCE(dir->ref_count == 0);
642         /* If the subsystem is about to be freed, the dir must be too */
643         WARN_ON_ONCE(system_refcount(dir->subsystem) == 1 && dir->ref_count != 1);
644
645         __put_system(dir->subsystem);
646         if (!--dir->ref_count)
647                 kfree(dir);
648 }
649
650 static void put_system(struct trace_subsystem_dir *dir)
651 {
652         mutex_lock(&event_mutex);
653         __put_system_dir(dir);
654         mutex_unlock(&event_mutex);
655 }
656
657 static void remove_subsystem(struct trace_subsystem_dir *dir)
658 {
659         if (!dir)
660                 return;
661
662         if (!--dir->nr_events) {
663                 tracefs_remove_recursive(dir->entry);
664                 list_del(&dir->list);
665                 __put_system_dir(dir);
666         }
667 }
668
669 static void remove_event_file_dir(struct trace_event_file *file)
670 {
671         struct dentry *dir = file->dir;
672         struct dentry *child;
673
674         if (dir) {
675                 spin_lock(&dir->d_lock);        /* probably unneeded */
676                 list_for_each_entry(child, &dir->d_subdirs, d_child) {
677                         if (d_really_is_positive(child))        /* probably unneeded */
678                                 d_inode(child)->i_private = NULL;
679                 }
680                 spin_unlock(&dir->d_lock);
681
682                 tracefs_remove_recursive(dir);
683         }
684
685         list_del(&file->list);
686         remove_subsystem(file->system);
687         free_event_filter(file->filter);
688         kmem_cache_free(file_cachep, file);
689 }
690
691 /*
692  * __ftrace_set_clr_event(NULL, NULL, NULL, set) will set/unset all events.
693  */
694 static int
695 __ftrace_set_clr_event_nolock(struct trace_array *tr, const char *match,
696                               const char *sub, const char *event, int set)
697 {
698         struct trace_event_file *file;
699         struct trace_event_call *call;
700         const char *name;
701         int ret = -EINVAL;
702
703         list_for_each_entry(file, &tr->events, list) {
704
705                 call = file->event_call;
706                 name = trace_event_name(call);
707
708                 if (!name || !call->class || !call->class->reg)
709                         continue;
710
711                 if (call->flags & TRACE_EVENT_FL_IGNORE_ENABLE)
712                         continue;
713
714                 if (match &&
715                     strcmp(match, name) != 0 &&
716                     strcmp(match, call->class->system) != 0)
717                         continue;
718
719                 if (sub && strcmp(sub, call->class->system) != 0)
720                         continue;
721
722                 if (event && strcmp(event, name) != 0)
723                         continue;
724
725                 ftrace_event_enable_disable(file, set);
726
727                 ret = 0;
728         }
729
730         return ret;
731 }
732
733 static int __ftrace_set_clr_event(struct trace_array *tr, const char *match,
734                                   const char *sub, const char *event, int set)
735 {
736         int ret;
737
738         mutex_lock(&event_mutex);
739         ret = __ftrace_set_clr_event_nolock(tr, match, sub, event, set);
740         mutex_unlock(&event_mutex);
741
742         return ret;
743 }
744
745 static int ftrace_set_clr_event(struct trace_array *tr, char *buf, int set)
746 {
747         char *event = NULL, *sub = NULL, *match;
748         int ret;
749
750         /*
751          * The buf format can be <subsystem>:<event-name>
752          *  *:<event-name> means any event by that name.
753          *  :<event-name> is the same.
754          *
755          *  <subsystem>:* means all events in that subsystem
756          *  <subsystem>: means the same.
757          *
758          *  <name> (no ':') means all events in a subsystem with
759          *  the name <name> or any event that matches <name>
760          */
761
762         match = strsep(&buf, ":");
763         if (buf) {
764                 sub = match;
765                 event = buf;
766                 match = NULL;
767
768                 if (!strlen(sub) || strcmp(sub, "*") == 0)
769                         sub = NULL;
770                 if (!strlen(event) || strcmp(event, "*") == 0)
771                         event = NULL;
772         }
773
774         ret = __ftrace_set_clr_event(tr, match, sub, event, set);
775
776         /* Put back the colon to allow this to be called again */
777         if (buf)
778                 *(buf - 1) = ':';
779
780         return ret;
781 }
782
783 /**
784  * trace_set_clr_event - enable or disable an event
785  * @system: system name to match (NULL for any system)
786  * @event: event name to match (NULL for all events, within system)
787  * @set: 1 to enable, 0 to disable
788  *
789  * This is a way for other parts of the kernel to enable or disable
790  * event recording.
791  *
792  * Returns 0 on success, -EINVAL if the parameters do not match any
793  * registered events.
794  */
795 int trace_set_clr_event(const char *system, const char *event, int set)
796 {
797         struct trace_array *tr = top_trace_array();
798
799         if (!tr)
800                 return -ENODEV;
801
802         return __ftrace_set_clr_event(tr, NULL, system, event, set);
803 }
804 EXPORT_SYMBOL_GPL(trace_set_clr_event);
805
806 /* 128 should be much more than enough */
807 #define EVENT_BUF_SIZE          127
808
809 static ssize_t
810 ftrace_event_write(struct file *file, const char __user *ubuf,
811                    size_t cnt, loff_t *ppos)
812 {
813         struct trace_parser parser;
814         struct seq_file *m = file->private_data;
815         struct trace_array *tr = m->private;
816         ssize_t read, ret;
817
818         if (!cnt)
819                 return 0;
820
821         ret = tracing_update_buffers();
822         if (ret < 0)
823                 return ret;
824
825         if (trace_parser_get_init(&parser, EVENT_BUF_SIZE + 1))
826                 return -ENOMEM;
827
828         read = trace_get_user(&parser, ubuf, cnt, ppos);
829
830         if (read >= 0 && trace_parser_loaded((&parser))) {
831                 int set = 1;
832
833                 if (*parser.buffer == '!')
834                         set = 0;
835
836                 parser.buffer[parser.idx] = 0;
837
838                 ret = ftrace_set_clr_event(tr, parser.buffer + !set, set);
839                 if (ret)
840                         goto out_put;
841         }
842
843         ret = read;
844
845  out_put:
846         trace_parser_put(&parser);
847
848         return ret;
849 }
850
851 static void *
852 t_next(struct seq_file *m, void *v, loff_t *pos)
853 {
854         struct trace_event_file *file = v;
855         struct trace_event_call *call;
856         struct trace_array *tr = m->private;
857
858         (*pos)++;
859
860         list_for_each_entry_continue(file, &tr->events, list) {
861                 call = file->event_call;
862                 /*
863                  * The ftrace subsystem is for showing formats only.
864                  * They can not be enabled or disabled via the event files.
865                  */
866                 if (call->class && call->class->reg)
867                         return file;
868         }
869
870         return NULL;
871 }
872
873 static void *t_start(struct seq_file *m, loff_t *pos)
874 {
875         struct trace_event_file *file;
876         struct trace_array *tr = m->private;
877         loff_t l;
878
879         mutex_lock(&event_mutex);
880
881         file = list_entry(&tr->events, struct trace_event_file, list);
882         for (l = 0; l <= *pos; ) {
883                 file = t_next(m, file, &l);
884                 if (!file)
885                         break;
886         }
887         return file;
888 }
889
890 static void *
891 s_next(struct seq_file *m, void *v, loff_t *pos)
892 {
893         struct trace_event_file *file = v;
894         struct trace_array *tr = m->private;
895
896         (*pos)++;
897
898         list_for_each_entry_continue(file, &tr->events, list) {
899                 if (file->flags & EVENT_FILE_FL_ENABLED)
900                         return file;
901         }
902
903         return NULL;
904 }
905
906 static void *s_start(struct seq_file *m, loff_t *pos)
907 {
908         struct trace_event_file *file;
909         struct trace_array *tr = m->private;
910         loff_t l;
911
912         mutex_lock(&event_mutex);
913
914         file = list_entry(&tr->events, struct trace_event_file, list);
915         for (l = 0; l <= *pos; ) {
916                 file = s_next(m, file, &l);
917                 if (!file)
918                         break;
919         }
920         return file;
921 }
922
923 static int t_show(struct seq_file *m, void *v)
924 {
925         struct trace_event_file *file = v;
926         struct trace_event_call *call = file->event_call;
927
928         if (strcmp(call->class->system, TRACE_SYSTEM) != 0)
929                 seq_printf(m, "%s:", call->class->system);
930         seq_printf(m, "%s\n", trace_event_name(call));
931
932         return 0;
933 }
934
935 static void t_stop(struct seq_file *m, void *p)
936 {
937         mutex_unlock(&event_mutex);
938 }
939
940 static void *p_start(struct seq_file *m, loff_t *pos)
941 {
942         struct trace_pid_list *pid_list;
943         struct trace_array *tr = m->private;
944
945         /*
946          * Grab the mutex, to keep calls to p_next() having the same
947          * tr->filtered_pids as p_start() has.
948          * If we just passed the tr->filtered_pids around, then RCU would
949          * have been enough, but doing that makes things more complex.
950          */
951         mutex_lock(&event_mutex);
952         rcu_read_lock_sched();
953
954         pid_list = rcu_dereference_sched(tr->filtered_pids);
955
956         if (!pid_list || *pos >= pid_list->nr_pids)
957                 return NULL;
958
959         return (void *)&pid_list->pids[*pos];
960 }
961
962 static void p_stop(struct seq_file *m, void *p)
963 {
964         rcu_read_unlock_sched();
965         mutex_unlock(&event_mutex);
966 }
967
968 static void *
969 p_next(struct seq_file *m, void *v, loff_t *pos)
970 {
971         struct trace_array *tr = m->private;
972         struct trace_pid_list *pid_list = rcu_dereference_sched(tr->filtered_pids);
973
974         (*pos)++;
975
976         if (*pos >= pid_list->nr_pids)
977                 return NULL;
978
979         return (void *)&pid_list->pids[*pos];
980 }
981
982 static int p_show(struct seq_file *m, void *v)
983 {
984         pid_t *pid = v;
985
986         seq_printf(m, "%d\n", *pid);
987         return 0;
988 }
989
990 static ssize_t
991 event_enable_read(struct file *filp, char __user *ubuf, size_t cnt,
992                   loff_t *ppos)
993 {
994         struct trace_event_file *file;
995         unsigned long flags;
996         char buf[4] = "0";
997
998         mutex_lock(&event_mutex);
999         file = event_file_data(filp);
1000         if (likely(file))
1001                 flags = file->flags;
1002         mutex_unlock(&event_mutex);
1003
1004         if (!file)
1005                 return -ENODEV;
1006
1007         if (flags & EVENT_FILE_FL_ENABLED &&
1008             !(flags & EVENT_FILE_FL_SOFT_DISABLED))
1009                 strcpy(buf, "1");
1010
1011         if (flags & EVENT_FILE_FL_SOFT_DISABLED ||
1012             flags & EVENT_FILE_FL_SOFT_MODE)
1013                 strcat(buf, "*");
1014
1015         strcat(buf, "\n");
1016
1017         return simple_read_from_buffer(ubuf, cnt, ppos, buf, strlen(buf));
1018 }
1019
1020 static ssize_t
1021 event_enable_write(struct file *filp, const char __user *ubuf, size_t cnt,
1022                    loff_t *ppos)
1023 {
1024         struct trace_event_file *file;
1025         unsigned long val;
1026         int ret;
1027
1028         ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
1029         if (ret)
1030                 return ret;
1031
1032         ret = tracing_update_buffers();
1033         if (ret < 0)
1034                 return ret;
1035
1036         switch (val) {
1037         case 0:
1038         case 1:
1039                 ret = -ENODEV;
1040                 mutex_lock(&event_mutex);
1041                 file = event_file_data(filp);
1042                 if (likely(file))
1043                         ret = ftrace_event_enable_disable(file, val);
1044                 mutex_unlock(&event_mutex);
1045                 break;
1046
1047         default:
1048                 return -EINVAL;
1049         }
1050
1051         *ppos += cnt;
1052
1053         return ret ? ret : cnt;
1054 }
1055
1056 static ssize_t
1057 system_enable_read(struct file *filp, char __user *ubuf, size_t cnt,
1058                    loff_t *ppos)
1059 {
1060         const char set_to_char[4] = { '?', '0', '1', 'X' };
1061         struct trace_subsystem_dir *dir = filp->private_data;
1062         struct event_subsystem *system = dir->subsystem;
1063         struct trace_event_call *call;
1064         struct trace_event_file *file;
1065         struct trace_array *tr = dir->tr;
1066         char buf[2];
1067         int set = 0;
1068         int ret;
1069
1070         mutex_lock(&event_mutex);
1071         list_for_each_entry(file, &tr->events, list) {
1072                 call = file->event_call;
1073                 if (!trace_event_name(call) || !call->class || !call->class->reg)
1074                         continue;
1075
1076                 if (system && strcmp(call->class->system, system->name) != 0)
1077                         continue;
1078
1079                 /*
1080                  * We need to find out if all the events are set
1081                  * or if all events or cleared, or if we have
1082                  * a mixture.
1083                  */
1084                 set |= (1 << !!(file->flags & EVENT_FILE_FL_ENABLED));
1085
1086                 /*
1087                  * If we have a mixture, no need to look further.
1088                  */
1089                 if (set == 3)
1090                         break;
1091         }
1092         mutex_unlock(&event_mutex);
1093
1094         buf[0] = set_to_char[set];
1095         buf[1] = '\n';
1096
1097         ret = simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
1098
1099         return ret;
1100 }
1101
1102 static ssize_t
1103 system_enable_write(struct file *filp, const char __user *ubuf, size_t cnt,
1104                     loff_t *ppos)
1105 {
1106         struct trace_subsystem_dir *dir = filp->private_data;
1107         struct event_subsystem *system = dir->subsystem;
1108         const char *name = NULL;
1109         unsigned long val;
1110         ssize_t ret;
1111
1112         ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
1113         if (ret)
1114                 return ret;
1115
1116         ret = tracing_update_buffers();
1117         if (ret < 0)
1118                 return ret;
1119
1120         if (val != 0 && val != 1)
1121                 return -EINVAL;
1122
1123         /*
1124          * Opening of "enable" adds a ref count to system,
1125          * so the name is safe to use.
1126          */
1127         if (system)
1128                 name = system->name;
1129
1130         ret = __ftrace_set_clr_event(dir->tr, NULL, name, NULL, val);
1131         if (ret)
1132                 goto out;
1133
1134         ret = cnt;
1135
1136 out:
1137         *ppos += cnt;
1138
1139         return ret;
1140 }
1141
1142 enum {
1143         FORMAT_HEADER           = 1,
1144         FORMAT_FIELD_SEPERATOR  = 2,
1145         FORMAT_PRINTFMT         = 3,
1146 };
1147
1148 static void *f_next(struct seq_file *m, void *v, loff_t *pos)
1149 {
1150         struct trace_event_call *call = event_file_data(m->private);
1151         struct list_head *common_head = &ftrace_common_fields;
1152         struct list_head *head = trace_get_fields(call);
1153         struct list_head *node = v;
1154
1155         (*pos)++;
1156
1157         switch ((unsigned long)v) {
1158         case FORMAT_HEADER:
1159                 node = common_head;
1160                 break;
1161
1162         case FORMAT_FIELD_SEPERATOR:
1163                 node = head;
1164                 break;
1165
1166         case FORMAT_PRINTFMT:
1167                 /* all done */
1168                 return NULL;
1169         }
1170
1171         node = node->prev;
1172         if (node == common_head)
1173                 return (void *)FORMAT_FIELD_SEPERATOR;
1174         else if (node == head)
1175                 return (void *)FORMAT_PRINTFMT;
1176         else
1177                 return node;
1178 }
1179
1180 static int f_show(struct seq_file *m, void *v)
1181 {
1182         struct trace_event_call *call = event_file_data(m->private);
1183         struct ftrace_event_field *field;
1184         const char *array_descriptor;
1185
1186         switch ((unsigned long)v) {
1187         case FORMAT_HEADER:
1188                 seq_printf(m, "name: %s\n", trace_event_name(call));
1189                 seq_printf(m, "ID: %d\n", call->event.type);
1190                 seq_puts(m, "format:\n");
1191                 return 0;
1192
1193         case FORMAT_FIELD_SEPERATOR:
1194                 seq_putc(m, '\n');
1195                 return 0;
1196
1197         case FORMAT_PRINTFMT:
1198                 seq_printf(m, "\nprint fmt: %s\n",
1199                            call->print_fmt);
1200                 return 0;
1201         }
1202
1203         field = list_entry(v, struct ftrace_event_field, link);
1204         /*
1205          * Smartly shows the array type(except dynamic array).
1206          * Normal:
1207          *      field:TYPE VAR
1208          * If TYPE := TYPE[LEN], it is shown:
1209          *      field:TYPE VAR[LEN]
1210          */
1211         array_descriptor = strchr(field->type, '[');
1212
1213         if (!strncmp(field->type, "__data_loc", 10))
1214                 array_descriptor = NULL;
1215
1216         if (!array_descriptor)
1217                 seq_printf(m, "\tfield:%s %s;\toffset:%u;\tsize:%u;\tsigned:%d;\n",
1218                            field->type, field->name, field->offset,
1219                            field->size, !!field->is_signed);
1220         else
1221                 seq_printf(m, "\tfield:%.*s %s%s;\toffset:%u;\tsize:%u;\tsigned:%d;\n",
1222                            (int)(array_descriptor - field->type),
1223                            field->type, field->name,
1224                            array_descriptor, field->offset,
1225                            field->size, !!field->is_signed);
1226
1227         return 0;
1228 }
1229
1230 static void *f_start(struct seq_file *m, loff_t *pos)
1231 {
1232         void *p = (void *)FORMAT_HEADER;
1233         loff_t l = 0;
1234
1235         /* ->stop() is called even if ->start() fails */
1236         mutex_lock(&event_mutex);
1237         if (!event_file_data(m->private))
1238                 return ERR_PTR(-ENODEV);
1239
1240         while (l < *pos && p)
1241                 p = f_next(m, p, &l);
1242
1243         return p;
1244 }
1245
1246 static void f_stop(struct seq_file *m, void *p)
1247 {
1248         mutex_unlock(&event_mutex);
1249 }
1250
1251 static const struct seq_operations trace_format_seq_ops = {
1252         .start          = f_start,
1253         .next           = f_next,
1254         .stop           = f_stop,
1255         .show           = f_show,
1256 };
1257
1258 static int trace_format_open(struct inode *inode, struct file *file)
1259 {
1260         struct seq_file *m;
1261         int ret;
1262
1263         ret = seq_open(file, &trace_format_seq_ops);
1264         if (ret < 0)
1265                 return ret;
1266
1267         m = file->private_data;
1268         m->private = file;
1269
1270         return 0;
1271 }
1272
1273 static ssize_t
1274 event_id_read(struct file *filp, char __user *ubuf, size_t cnt, loff_t *ppos)
1275 {
1276         int id = (long)event_file_data(filp);
1277         char buf[32];
1278         int len;
1279
1280         if (*ppos)
1281                 return 0;
1282
1283         if (unlikely(!id))
1284                 return -ENODEV;
1285
1286         len = sprintf(buf, "%d\n", id);
1287
1288         return simple_read_from_buffer(ubuf, cnt, ppos, buf, len);
1289 }
1290
1291 static ssize_t
1292 event_filter_read(struct file *filp, char __user *ubuf, size_t cnt,
1293                   loff_t *ppos)
1294 {
1295         struct trace_event_file *file;
1296         struct trace_seq *s;
1297         int r = -ENODEV;
1298
1299         if (*ppos)
1300                 return 0;
1301
1302         s = kmalloc(sizeof(*s), GFP_KERNEL);
1303
1304         if (!s)
1305                 return -ENOMEM;
1306
1307         trace_seq_init(s);
1308
1309         mutex_lock(&event_mutex);
1310         file = event_file_data(filp);
1311         if (file)
1312                 print_event_filter(file, s);
1313         mutex_unlock(&event_mutex);
1314
1315         if (file)
1316                 r = simple_read_from_buffer(ubuf, cnt, ppos,
1317                                             s->buffer, trace_seq_used(s));
1318
1319         kfree(s);
1320
1321         return r;
1322 }
1323
1324 static ssize_t
1325 event_filter_write(struct file *filp, const char __user *ubuf, size_t cnt,
1326                    loff_t *ppos)
1327 {
1328         struct trace_event_file *file;
1329         char *buf;
1330         int err = -ENODEV;
1331
1332         if (cnt >= PAGE_SIZE)
1333                 return -EINVAL;
1334
1335         buf = (char *)__get_free_page(GFP_TEMPORARY);
1336         if (!buf)
1337                 return -ENOMEM;
1338
1339         if (copy_from_user(buf, ubuf, cnt)) {
1340                 free_page((unsigned long) buf);
1341                 return -EFAULT;
1342         }
1343         buf[cnt] = '\0';
1344
1345         mutex_lock(&event_mutex);
1346         file = event_file_data(filp);
1347         if (file)
1348                 err = apply_event_filter(file, buf);
1349         mutex_unlock(&event_mutex);
1350
1351         free_page((unsigned long) buf);
1352         if (err < 0)
1353                 return err;
1354
1355         *ppos += cnt;
1356
1357         return cnt;
1358 }
1359
1360 static LIST_HEAD(event_subsystems);
1361
1362 static int subsystem_open(struct inode *inode, struct file *filp)
1363 {
1364         struct event_subsystem *system = NULL;
1365         struct trace_subsystem_dir *dir = NULL; /* Initialize for gcc */
1366         struct trace_array *tr;
1367         int ret;
1368
1369         if (tracing_is_disabled())
1370                 return -ENODEV;
1371
1372         /* Make sure the system still exists */
1373         mutex_lock(&trace_types_lock);
1374         mutex_lock(&event_mutex);
1375         list_for_each_entry(tr, &ftrace_trace_arrays, list) {
1376                 list_for_each_entry(dir, &tr->systems, list) {
1377                         if (dir == inode->i_private) {
1378                                 /* Don't open systems with no events */
1379                                 if (dir->nr_events) {
1380                                         __get_system_dir(dir);
1381                                         system = dir->subsystem;
1382                                 }
1383                                 goto exit_loop;
1384                         }
1385                 }
1386         }
1387  exit_loop:
1388         mutex_unlock(&event_mutex);
1389         mutex_unlock(&trace_types_lock);
1390
1391         if (!system)
1392                 return -ENODEV;
1393
1394         /* Some versions of gcc think dir can be uninitialized here */
1395         WARN_ON(!dir);
1396
1397         /* Still need to increment the ref count of the system */
1398         if (trace_array_get(tr) < 0) {
1399                 put_system(dir);
1400                 return -ENODEV;
1401         }
1402
1403         ret = tracing_open_generic(inode, filp);
1404         if (ret < 0) {
1405                 trace_array_put(tr);
1406                 put_system(dir);
1407         }
1408
1409         return ret;
1410 }
1411
1412 static int system_tr_open(struct inode *inode, struct file *filp)
1413 {
1414         struct trace_subsystem_dir *dir;
1415         struct trace_array *tr = inode->i_private;
1416         int ret;
1417
1418         if (tracing_is_disabled())
1419                 return -ENODEV;
1420
1421         if (trace_array_get(tr) < 0)
1422                 return -ENODEV;
1423
1424         /* Make a temporary dir that has no system but points to tr */
1425         dir = kzalloc(sizeof(*dir), GFP_KERNEL);
1426         if (!dir) {
1427                 trace_array_put(tr);
1428                 return -ENOMEM;
1429         }
1430
1431         dir->tr = tr;
1432
1433         ret = tracing_open_generic(inode, filp);
1434         if (ret < 0) {
1435                 trace_array_put(tr);
1436                 kfree(dir);
1437                 return ret;
1438         }
1439
1440         filp->private_data = dir;
1441
1442         return 0;
1443 }
1444
1445 static int subsystem_release(struct inode *inode, struct file *file)
1446 {
1447         struct trace_subsystem_dir *dir = file->private_data;
1448
1449         trace_array_put(dir->tr);
1450
1451         /*
1452          * If dir->subsystem is NULL, then this is a temporary
1453          * descriptor that was made for a trace_array to enable
1454          * all subsystems.
1455          */
1456         if (dir->subsystem)
1457                 put_system(dir);
1458         else
1459                 kfree(dir);
1460
1461         return 0;
1462 }
1463
1464 static ssize_t
1465 subsystem_filter_read(struct file *filp, char __user *ubuf, size_t cnt,
1466                       loff_t *ppos)
1467 {
1468         struct trace_subsystem_dir *dir = filp->private_data;
1469         struct event_subsystem *system = dir->subsystem;
1470         struct trace_seq *s;
1471         int r;
1472
1473         if (*ppos)
1474                 return 0;
1475
1476         s = kmalloc(sizeof(*s), GFP_KERNEL);
1477         if (!s)
1478                 return -ENOMEM;
1479
1480         trace_seq_init(s);
1481
1482         print_subsystem_event_filter(system, s);
1483         r = simple_read_from_buffer(ubuf, cnt, ppos,
1484                                     s->buffer, trace_seq_used(s));
1485
1486         kfree(s);
1487
1488         return r;
1489 }
1490
1491 static ssize_t
1492 subsystem_filter_write(struct file *filp, const char __user *ubuf, size_t cnt,
1493                        loff_t *ppos)
1494 {
1495         struct trace_subsystem_dir *dir = filp->private_data;
1496         char *buf;
1497         int err;
1498
1499         if (cnt >= PAGE_SIZE)
1500                 return -EINVAL;
1501
1502         buf = (char *)__get_free_page(GFP_TEMPORARY);
1503         if (!buf)
1504                 return -ENOMEM;
1505
1506         if (copy_from_user(buf, ubuf, cnt)) {
1507                 free_page((unsigned long) buf);
1508                 return -EFAULT;
1509         }
1510         buf[cnt] = '\0';
1511
1512         err = apply_subsystem_event_filter(dir, buf);
1513         free_page((unsigned long) buf);
1514         if (err < 0)
1515                 return err;
1516
1517         *ppos += cnt;
1518
1519         return cnt;
1520 }
1521
1522 static ssize_t
1523 show_header(struct file *filp, char __user *ubuf, size_t cnt, loff_t *ppos)
1524 {
1525         int (*func)(struct trace_seq *s) = filp->private_data;
1526         struct trace_seq *s;
1527         int r;
1528
1529         if (*ppos)
1530                 return 0;
1531
1532         s = kmalloc(sizeof(*s), GFP_KERNEL);
1533         if (!s)
1534                 return -ENOMEM;
1535
1536         trace_seq_init(s);
1537
1538         func(s);
1539         r = simple_read_from_buffer(ubuf, cnt, ppos,
1540                                     s->buffer, trace_seq_used(s));
1541
1542         kfree(s);
1543
1544         return r;
1545 }
1546
1547 static int max_pids(struct trace_pid_list *pid_list)
1548 {
1549         return (PAGE_SIZE << pid_list->order) / sizeof(pid_t);
1550 }
1551
1552 static void ignore_task_cpu(void *data)
1553 {
1554         struct trace_array *tr = data;
1555         struct trace_pid_list *pid_list;
1556
1557         /*
1558          * This function is called by on_each_cpu() while the
1559          * event_mutex is held.
1560          */
1561         pid_list = rcu_dereference_protected(tr->filtered_pids,
1562                                              mutex_is_locked(&event_mutex));
1563
1564         this_cpu_write(tr->trace_buffer.data->ignore_pid,
1565                        check_ignore_pid(pid_list, current));
1566 }
1567
1568 static ssize_t
1569 ftrace_event_pid_write(struct file *filp, const char __user *ubuf,
1570                        size_t cnt, loff_t *ppos)
1571 {
1572         struct seq_file *m = filp->private_data;
1573         struct trace_array *tr = m->private;
1574         struct trace_pid_list *filtered_pids = NULL;
1575         struct trace_pid_list *pid_list = NULL;
1576         struct trace_event_file *file;
1577         struct trace_parser parser;
1578         unsigned long val;
1579         loff_t this_pos;
1580         ssize_t read = 0;
1581         ssize_t ret = 0;
1582         pid_t pid;
1583         int i;
1584
1585         if (!cnt)
1586                 return 0;
1587
1588         ret = tracing_update_buffers();
1589         if (ret < 0)
1590                 return ret;
1591
1592         if (trace_parser_get_init(&parser, EVENT_BUF_SIZE + 1))
1593                 return -ENOMEM;
1594
1595         mutex_lock(&event_mutex);
1596         /*
1597          * Load as many pids into the array before doing a
1598          * swap from the tr->filtered_pids to the new list.
1599          */
1600         while (cnt > 0) {
1601
1602                 this_pos = 0;
1603
1604                 ret = trace_get_user(&parser, ubuf, cnt, &this_pos);
1605                 if (ret < 0 || !trace_parser_loaded(&parser))
1606                         break;
1607
1608                 read += ret;
1609                 ubuf += ret;
1610                 cnt -= ret;
1611
1612                 parser.buffer[parser.idx] = 0;
1613
1614                 ret = -EINVAL;
1615                 if (kstrtoul(parser.buffer, 0, &val))
1616                         break;
1617                 if (val > INT_MAX)
1618                         break;
1619
1620                 pid = (pid_t)val;
1621
1622                 ret = -ENOMEM;
1623                 if (!pid_list) {
1624                         pid_list = kmalloc(sizeof(*pid_list), GFP_KERNEL);
1625                         if (!pid_list)
1626                                 break;
1627
1628                         filtered_pids = rcu_dereference_protected(tr->filtered_pids,
1629                                                         lockdep_is_held(&event_mutex));
1630                         if (filtered_pids)
1631                                 pid_list->order = filtered_pids->order;
1632                         else
1633                                 pid_list->order = 0;
1634
1635                         pid_list->pids = (void *)__get_free_pages(GFP_KERNEL,
1636                                                                   pid_list->order);
1637                         if (!pid_list->pids)
1638                                 break;
1639
1640                         if (filtered_pids) {
1641                                 pid_list->nr_pids = filtered_pids->nr_pids;
1642                                 memcpy(pid_list->pids, filtered_pids->pids,
1643                                        pid_list->nr_pids * sizeof(pid_t));
1644                         } else
1645                                 pid_list->nr_pids = 0;
1646                 }
1647
1648                 if (pid_list->nr_pids >= max_pids(pid_list)) {
1649                         pid_t *pid_page;
1650
1651                         pid_page = (void *)__get_free_pages(GFP_KERNEL,
1652                                                             pid_list->order + 1);
1653                         if (!pid_page)
1654                                 break;
1655                         memcpy(pid_page, pid_list->pids,
1656                                pid_list->nr_pids * sizeof(pid_t));
1657                         free_pages((unsigned long)pid_list->pids, pid_list->order);
1658
1659                         pid_list->order++;
1660                         pid_list->pids = pid_page;
1661                 }
1662
1663                 pid_list->pids[pid_list->nr_pids++] = pid;
1664                 trace_parser_clear(&parser);
1665                 ret = 0;
1666         }
1667         trace_parser_put(&parser);
1668
1669         if (ret < 0) {
1670                 if (pid_list)
1671                         free_pages((unsigned long)pid_list->pids, pid_list->order);
1672                 kfree(pid_list);
1673                 mutex_unlock(&event_mutex);
1674                 return ret;
1675         }
1676
1677         if (!pid_list) {
1678                 mutex_unlock(&event_mutex);
1679                 return ret;
1680         }
1681
1682         sort(pid_list->pids, pid_list->nr_pids, sizeof(pid_t), cmp_pid, NULL);
1683
1684         /* Remove duplicates */
1685         for (i = 1; i < pid_list->nr_pids; i++) {
1686                 int start = i;
1687
1688                 while (i < pid_list->nr_pids &&
1689                        pid_list->pids[i - 1] == pid_list->pids[i])
1690                         i++;
1691
1692                 if (start != i) {
1693                         if (i < pid_list->nr_pids) {
1694                                 memmove(&pid_list->pids[start], &pid_list->pids[i],
1695                                         (pid_list->nr_pids - i) * sizeof(pid_t));
1696                                 pid_list->nr_pids -= i - start;
1697                                 i = start;
1698                         } else
1699                                 pid_list->nr_pids = start;
1700                 }
1701         }
1702
1703         rcu_assign_pointer(tr->filtered_pids, pid_list);
1704
1705         list_for_each_entry(file, &tr->events, list) {
1706                 set_bit(EVENT_FILE_FL_PID_FILTER_BIT, &file->flags);
1707         }
1708
1709         if (filtered_pids) {
1710                 synchronize_sched();
1711
1712                 free_pages((unsigned long)filtered_pids->pids, filtered_pids->order);
1713                 kfree(filtered_pids);
1714         } else {
1715                 /*
1716                  * Register a probe that is called before all other probes
1717                  * to set ignore_pid if next or prev do not match.
1718                  * Register a probe this is called after all other probes
1719                  * to only keep ignore_pid set if next pid matches.
1720                  */
1721                 register_trace_prio_sched_switch(event_filter_pid_sched_switch_probe_pre,
1722                                                  tr, INT_MAX);
1723                 register_trace_prio_sched_switch(event_filter_pid_sched_switch_probe_post,
1724                                                  tr, 0);
1725
1726                 register_trace_prio_sched_wakeup(event_filter_pid_sched_wakeup_probe_pre,
1727                                                  tr, INT_MAX);
1728                 register_trace_prio_sched_wakeup(event_filter_pid_sched_wakeup_probe_post,
1729                                                  tr, 0);
1730
1731                 /*
1732                  * Ignoring of pids is done at task switch. But we have to
1733                  * check for those tasks that are currently running.
1734                  */
1735                 on_each_cpu(ignore_task_cpu, tr, 1);
1736         }
1737
1738         mutex_unlock(&event_mutex);
1739
1740         ret = read;
1741         *ppos += read;
1742
1743         return ret;
1744 }
1745
1746 static int ftrace_event_avail_open(struct inode *inode, struct file *file);
1747 static int ftrace_event_set_open(struct inode *inode, struct file *file);
1748 static int ftrace_event_set_pid_open(struct inode *inode, struct file *file);
1749 static int ftrace_event_release(struct inode *inode, struct file *file);
1750
1751 static const struct seq_operations show_event_seq_ops = {
1752         .start = t_start,
1753         .next = t_next,
1754         .show = t_show,
1755         .stop = t_stop,
1756 };
1757
1758 static const struct seq_operations show_set_event_seq_ops = {
1759         .start = s_start,
1760         .next = s_next,
1761         .show = t_show,
1762         .stop = t_stop,
1763 };
1764
1765 static const struct seq_operations show_set_pid_seq_ops = {
1766         .start = p_start,
1767         .next = p_next,
1768         .show = p_show,
1769         .stop = p_stop,
1770 };
1771
1772 static const struct file_operations ftrace_avail_fops = {
1773         .open = ftrace_event_avail_open,
1774         .read = seq_read,
1775         .llseek = seq_lseek,
1776         .release = seq_release,
1777 };
1778
1779 static const struct file_operations ftrace_set_event_fops = {
1780         .open = ftrace_event_set_open,
1781         .read = seq_read,
1782         .write = ftrace_event_write,
1783         .llseek = seq_lseek,
1784         .release = ftrace_event_release,
1785 };
1786
1787 static const struct file_operations ftrace_set_event_pid_fops = {
1788         .open = ftrace_event_set_pid_open,
1789         .read = seq_read,
1790         .write = ftrace_event_pid_write,
1791         .llseek = seq_lseek,
1792         .release = ftrace_event_release,
1793 };
1794
1795 static const struct file_operations ftrace_enable_fops = {
1796         .open = tracing_open_generic,
1797         .read = event_enable_read,
1798         .write = event_enable_write,
1799         .llseek = default_llseek,
1800 };
1801
1802 static const struct file_operations ftrace_event_format_fops = {
1803         .open = trace_format_open,
1804         .read = seq_read,
1805         .llseek = seq_lseek,
1806         .release = seq_release,
1807 };
1808
1809 static const struct file_operations ftrace_event_id_fops = {
1810         .read = event_id_read,
1811         .llseek = default_llseek,
1812 };
1813
1814 static const struct file_operations ftrace_event_filter_fops = {
1815         .open = tracing_open_generic,
1816         .read = event_filter_read,
1817         .write = event_filter_write,
1818         .llseek = default_llseek,
1819 };
1820
1821 static const struct file_operations ftrace_subsystem_filter_fops = {
1822         .open = subsystem_open,
1823         .read = subsystem_filter_read,
1824         .write = subsystem_filter_write,
1825         .llseek = default_llseek,
1826         .release = subsystem_release,
1827 };
1828
1829 static const struct file_operations ftrace_system_enable_fops = {
1830         .open = subsystem_open,
1831         .read = system_enable_read,
1832         .write = system_enable_write,
1833         .llseek = default_llseek,
1834         .release = subsystem_release,
1835 };
1836
1837 static const struct file_operations ftrace_tr_enable_fops = {
1838         .open = system_tr_open,
1839         .read = system_enable_read,
1840         .write = system_enable_write,
1841         .llseek = default_llseek,
1842         .release = subsystem_release,
1843 };
1844
1845 static const struct file_operations ftrace_show_header_fops = {
1846         .open = tracing_open_generic,
1847         .read = show_header,
1848         .llseek = default_llseek,
1849 };
1850
1851 static int
1852 ftrace_event_open(struct inode *inode, struct file *file,
1853                   const struct seq_operations *seq_ops)
1854 {
1855         struct seq_file *m;
1856         int ret;
1857
1858         ret = seq_open(file, seq_ops);
1859         if (ret < 0)
1860                 return ret;
1861         m = file->private_data;
1862         /* copy tr over to seq ops */
1863         m->private = inode->i_private;
1864
1865         return ret;
1866 }
1867
1868 static int ftrace_event_release(struct inode *inode, struct file *file)
1869 {
1870         struct trace_array *tr = inode->i_private;
1871
1872         trace_array_put(tr);
1873
1874         return seq_release(inode, file);
1875 }
1876
1877 static int
1878 ftrace_event_avail_open(struct inode *inode, struct file *file)
1879 {
1880         const struct seq_operations *seq_ops = &show_event_seq_ops;
1881
1882         return ftrace_event_open(inode, file, seq_ops);
1883 }
1884
1885 static int
1886 ftrace_event_set_open(struct inode *inode, struct file *file)
1887 {
1888         const struct seq_operations *seq_ops = &show_set_event_seq_ops;
1889         struct trace_array *tr = inode->i_private;
1890         int ret;
1891
1892         if (trace_array_get(tr) < 0)
1893                 return -ENODEV;
1894
1895         if ((file->f_mode & FMODE_WRITE) &&
1896             (file->f_flags & O_TRUNC))
1897                 ftrace_clear_events(tr);
1898
1899         ret = ftrace_event_open(inode, file, seq_ops);
1900         if (ret < 0)
1901                 trace_array_put(tr);
1902         return ret;
1903 }
1904
1905 static int
1906 ftrace_event_set_pid_open(struct inode *inode, struct file *file)
1907 {
1908         const struct seq_operations *seq_ops = &show_set_pid_seq_ops;
1909         struct trace_array *tr = inode->i_private;
1910         int ret;
1911
1912         if (trace_array_get(tr) < 0)
1913                 return -ENODEV;
1914
1915         if ((file->f_mode & FMODE_WRITE) &&
1916             (file->f_flags & O_TRUNC))
1917                 ftrace_clear_event_pids(tr);
1918
1919         ret = ftrace_event_open(inode, file, seq_ops);
1920         if (ret < 0)
1921                 trace_array_put(tr);
1922         return ret;
1923 }
1924
1925 static struct event_subsystem *
1926 create_new_subsystem(const char *name)
1927 {
1928         struct event_subsystem *system;
1929
1930         /* need to create new entry */
1931         system = kmalloc(sizeof(*system), GFP_KERNEL);
1932         if (!system)
1933                 return NULL;
1934
1935         system->ref_count = 1;
1936
1937         /* Only allocate if dynamic (kprobes and modules) */
1938         system->name = kstrdup_const(name, GFP_KERNEL);
1939         if (!system->name)
1940                 goto out_free;
1941
1942         system->filter = NULL;
1943
1944         system->filter = kzalloc(sizeof(struct event_filter), GFP_KERNEL);
1945         if (!system->filter)
1946                 goto out_free;
1947
1948         list_add(&system->list, &event_subsystems);
1949
1950         return system;
1951
1952  out_free:
1953         kfree_const(system->name);
1954         kfree(system);
1955         return NULL;
1956 }
1957
1958 static struct dentry *
1959 event_subsystem_dir(struct trace_array *tr, const char *name,
1960                     struct trace_event_file *file, struct dentry *parent)
1961 {
1962         struct trace_subsystem_dir *dir;
1963         struct event_subsystem *system;
1964         struct dentry *entry;
1965
1966         /* First see if we did not already create this dir */
1967         list_for_each_entry(dir, &tr->systems, list) {
1968                 system = dir->subsystem;
1969                 if (strcmp(system->name, name) == 0) {
1970                         dir->nr_events++;
1971                         file->system = dir;
1972                         return dir->entry;
1973                 }
1974         }
1975
1976         /* Now see if the system itself exists. */
1977         list_for_each_entry(system, &event_subsystems, list) {
1978                 if (strcmp(system->name, name) == 0)
1979                         break;
1980         }
1981         /* Reset system variable when not found */
1982         if (&system->list == &event_subsystems)
1983                 system = NULL;
1984
1985         dir = kmalloc(sizeof(*dir), GFP_KERNEL);
1986         if (!dir)
1987                 goto out_fail;
1988
1989         if (!system) {
1990                 system = create_new_subsystem(name);
1991                 if (!system)
1992                         goto out_free;
1993         } else
1994                 __get_system(system);
1995
1996         dir->entry = tracefs_create_dir(name, parent);
1997         if (!dir->entry) {
1998                 pr_warn("Failed to create system directory %s\n", name);
1999                 __put_system(system);
2000                 goto out_free;
2001         }
2002
2003         dir->tr = tr;
2004         dir->ref_count = 1;
2005         dir->nr_events = 1;
2006         dir->subsystem = system;
2007         file->system = dir;
2008
2009         entry = tracefs_create_file("filter", 0644, dir->entry, dir,
2010                                     &ftrace_subsystem_filter_fops);
2011         if (!entry) {
2012                 kfree(system->filter);
2013                 system->filter = NULL;
2014                 pr_warn("Could not create tracefs '%s/filter' entry\n", name);
2015         }
2016
2017         trace_create_file("enable", 0644, dir->entry, dir,
2018                           &ftrace_system_enable_fops);
2019
2020         list_add(&dir->list, &tr->systems);
2021
2022         return dir->entry;
2023
2024  out_free:
2025         kfree(dir);
2026  out_fail:
2027         /* Only print this message if failed on memory allocation */
2028         if (!dir || !system)
2029                 pr_warn("No memory to create event subsystem %s\n", name);
2030         return NULL;
2031 }
2032
2033 static int
2034 event_create_dir(struct dentry *parent, struct trace_event_file *file)
2035 {
2036         struct trace_event_call *call = file->event_call;
2037         struct trace_array *tr = file->tr;
2038         struct list_head *head;
2039         struct dentry *d_events;
2040         const char *name;
2041         int ret;
2042
2043         /*
2044          * If the trace point header did not define TRACE_SYSTEM
2045          * then the system would be called "TRACE_SYSTEM".
2046          */
2047         if (strcmp(call->class->system, TRACE_SYSTEM) != 0) {
2048                 d_events = event_subsystem_dir(tr, call->class->system, file, parent);
2049                 if (!d_events)
2050                         return -ENOMEM;
2051         } else
2052                 d_events = parent;
2053
2054         name = trace_event_name(call);
2055         file->dir = tracefs_create_dir(name, d_events);
2056         if (!file->dir) {
2057                 pr_warn("Could not create tracefs '%s' directory\n", name);
2058                 return -1;
2059         }
2060
2061         if (call->class->reg && !(call->flags & TRACE_EVENT_FL_IGNORE_ENABLE))
2062                 trace_create_file("enable", 0644, file->dir, file,
2063                                   &ftrace_enable_fops);
2064
2065 #ifdef CONFIG_PERF_EVENTS
2066         if (call->event.type && call->class->reg)
2067                 trace_create_file("id", 0444, file->dir,
2068                                   (void *)(long)call->event.type,
2069                                   &ftrace_event_id_fops);
2070 #endif
2071
2072         /*
2073          * Other events may have the same class. Only update
2074          * the fields if they are not already defined.
2075          */
2076         head = trace_get_fields(call);
2077         if (list_empty(head)) {
2078                 ret = call->class->define_fields(call);
2079                 if (ret < 0) {
2080                         pr_warn("Could not initialize trace point events/%s\n",
2081                                 name);
2082                         return -1;
2083                 }
2084         }
2085         trace_create_file("filter", 0644, file->dir, file,
2086                           &ftrace_event_filter_fops);
2087
2088         trace_create_file("trigger", 0644, file->dir, file,
2089                           &event_trigger_fops);
2090
2091         trace_create_file("format", 0444, file->dir, call,
2092                           &ftrace_event_format_fops);
2093
2094         return 0;
2095 }
2096
2097 static void remove_event_from_tracers(struct trace_event_call *call)
2098 {
2099         struct trace_event_file *file;
2100         struct trace_array *tr;
2101
2102         do_for_each_event_file_safe(tr, file) {
2103                 if (file->event_call != call)
2104                         continue;
2105
2106                 remove_event_file_dir(file);
2107                 /*
2108                  * The do_for_each_event_file_safe() is
2109                  * a double loop. After finding the call for this
2110                  * trace_array, we use break to jump to the next
2111                  * trace_array.
2112                  */
2113                 break;
2114         } while_for_each_event_file();
2115 }
2116
2117 static void event_remove(struct trace_event_call *call)
2118 {
2119         struct trace_array *tr;
2120         struct trace_event_file *file;
2121
2122         do_for_each_event_file(tr, file) {
2123                 if (file->event_call != call)
2124                         continue;
2125                 ftrace_event_enable_disable(file, 0);
2126                 /*
2127                  * The do_for_each_event_file() is
2128                  * a double loop. After finding the call for this
2129                  * trace_array, we use break to jump to the next
2130                  * trace_array.
2131                  */
2132                 break;
2133         } while_for_each_event_file();
2134
2135         if (call->event.funcs)
2136                 __unregister_trace_event(&call->event);
2137         remove_event_from_tracers(call);
2138         list_del(&call->list);
2139 }
2140
2141 static int event_init(struct trace_event_call *call)
2142 {
2143         int ret = 0;
2144         const char *name;
2145
2146         name = trace_event_name(call);
2147         if (WARN_ON(!name))
2148                 return -EINVAL;
2149
2150         if (call->class->raw_init) {
2151                 ret = call->class->raw_init(call);
2152                 if (ret < 0 && ret != -ENOSYS)
2153                         pr_warn("Could not initialize trace events/%s\n", name);
2154         }
2155
2156         return ret;
2157 }
2158
2159 static int
2160 __register_event(struct trace_event_call *call, struct module *mod)
2161 {
2162         int ret;
2163
2164         ret = event_init(call);
2165         if (ret < 0)
2166                 return ret;
2167
2168         list_add(&call->list, &ftrace_events);
2169         call->mod = mod;
2170
2171         return 0;
2172 }
2173
2174 static char *enum_replace(char *ptr, struct trace_enum_map *map, int len)
2175 {
2176         int rlen;
2177         int elen;
2178
2179         /* Find the length of the enum value as a string */
2180         elen = snprintf(ptr, 0, "%ld", map->enum_value);
2181         /* Make sure there's enough room to replace the string with the value */
2182         if (len < elen)
2183                 return NULL;
2184
2185         snprintf(ptr, elen + 1, "%ld", map->enum_value);
2186
2187         /* Get the rest of the string of ptr */
2188         rlen = strlen(ptr + len);
2189         memmove(ptr + elen, ptr + len, rlen);
2190         /* Make sure we end the new string */
2191         ptr[elen + rlen] = 0;
2192
2193         return ptr + elen;
2194 }
2195
2196 static void update_event_printk(struct trace_event_call *call,
2197                                 struct trace_enum_map *map)
2198 {
2199         char *ptr;
2200         int quote = 0;
2201         int len = strlen(map->enum_string);
2202
2203         for (ptr = call->print_fmt; *ptr; ptr++) {
2204                 if (*ptr == '\\') {
2205                         ptr++;
2206                         /* paranoid */
2207                         if (!*ptr)
2208                                 break;
2209                         continue;
2210                 }
2211                 if (*ptr == '"') {
2212                         quote ^= 1;
2213                         continue;
2214                 }
2215                 if (quote)
2216                         continue;
2217                 if (isdigit(*ptr)) {
2218                         /* skip numbers */
2219                         do {
2220                                 ptr++;
2221                                 /* Check for alpha chars like ULL */
2222                         } while (isalnum(*ptr));
2223                         if (!*ptr)
2224                                 break;
2225                         /*
2226                          * A number must have some kind of delimiter after
2227                          * it, and we can ignore that too.
2228                          */
2229                         continue;
2230                 }
2231                 if (isalpha(*ptr) || *ptr == '_') {
2232                         if (strncmp(map->enum_string, ptr, len) == 0 &&
2233                             !isalnum(ptr[len]) && ptr[len] != '_') {
2234                                 ptr = enum_replace(ptr, map, len);
2235                                 /* Hmm, enum string smaller than value */
2236                                 if (WARN_ON_ONCE(!ptr))
2237                                         return;
2238                                 /*
2239                                  * No need to decrement here, as enum_replace()
2240                                  * returns the pointer to the character passed
2241                                  * the enum, and two enums can not be placed
2242                                  * back to back without something in between.
2243                                  * We can skip that something in between.
2244                                  */
2245                                 continue;
2246                         }
2247                 skip_more:
2248                         do {
2249                                 ptr++;
2250                         } while (isalnum(*ptr) || *ptr == '_');
2251                         if (!*ptr)
2252                                 break;
2253                         /*
2254                          * If what comes after this variable is a '.' or
2255                          * '->' then we can continue to ignore that string.
2256                          */
2257                         if (*ptr == '.' || (ptr[0] == '-' && ptr[1] == '>')) {
2258                                 ptr += *ptr == '.' ? 1 : 2;
2259                                 if (!*ptr)
2260                                         break;
2261                                 goto skip_more;
2262                         }
2263                         /*
2264                          * Once again, we can skip the delimiter that came
2265                          * after the string.
2266                          */
2267                         continue;
2268                 }
2269         }
2270 }
2271
2272 void trace_event_enum_update(struct trace_enum_map **map, int len)
2273 {
2274         struct trace_event_call *call, *p;
2275         const char *last_system = NULL;
2276         int last_i;
2277         int i;
2278
2279         down_write(&trace_event_sem);
2280         list_for_each_entry_safe(call, p, &ftrace_events, list) {
2281                 /* events are usually grouped together with systems */
2282                 if (!last_system || call->class->system != last_system) {
2283                         last_i = 0;
2284                         last_system = call->class->system;
2285                 }
2286
2287                 for (i = last_i; i < len; i++) {
2288                         if (call->class->system == map[i]->system) {
2289                                 /* Save the first system if need be */
2290                                 if (!last_i)
2291                                         last_i = i;
2292                                 update_event_printk(call, map[i]);
2293                         }
2294                 }
2295         }
2296         up_write(&trace_event_sem);
2297 }
2298
2299 static struct trace_event_file *
2300 trace_create_new_event(struct trace_event_call *call,
2301                        struct trace_array *tr)
2302 {
2303         struct trace_event_file *file;
2304
2305         file = kmem_cache_alloc(file_cachep, GFP_TRACE);
2306         if (!file)
2307                 return NULL;
2308
2309         file->event_call = call;
2310         file->tr = tr;
2311         atomic_set(&file->sm_ref, 0);
2312         atomic_set(&file->tm_ref, 0);
2313         INIT_LIST_HEAD(&file->triggers);
2314         list_add(&file->list, &tr->events);
2315
2316         return file;
2317 }
2318
2319 /* Add an event to a trace directory */
2320 static int
2321 __trace_add_new_event(struct trace_event_call *call, struct trace_array *tr)
2322 {
2323         struct trace_event_file *file;
2324
2325         file = trace_create_new_event(call, tr);
2326         if (!file)
2327                 return -ENOMEM;
2328
2329         return event_create_dir(tr->event_dir, file);
2330 }
2331
2332 /*
2333  * Just create a decriptor for early init. A descriptor is required
2334  * for enabling events at boot. We want to enable events before
2335  * the filesystem is initialized.
2336  */
2337 static __init int
2338 __trace_early_add_new_event(struct trace_event_call *call,
2339                             struct trace_array *tr)
2340 {
2341         struct trace_event_file *file;
2342
2343         file = trace_create_new_event(call, tr);
2344         if (!file)
2345                 return -ENOMEM;
2346
2347         return 0;
2348 }
2349
2350 struct ftrace_module_file_ops;
2351 static void __add_event_to_tracers(struct trace_event_call *call);
2352
2353 /* Add an additional event_call dynamically */
2354 int trace_add_event_call(struct trace_event_call *call)
2355 {
2356         int ret;
2357         mutex_lock(&trace_types_lock);
2358         mutex_lock(&event_mutex);
2359
2360         ret = __register_event(call, NULL);
2361         if (ret >= 0)
2362                 __add_event_to_tracers(call);
2363
2364         mutex_unlock(&event_mutex);
2365         mutex_unlock(&trace_types_lock);
2366         return ret;
2367 }
2368
2369 /*
2370  * Must be called under locking of trace_types_lock, event_mutex and
2371  * trace_event_sem.
2372  */
2373 static void __trace_remove_event_call(struct trace_event_call *call)
2374 {
2375         event_remove(call);
2376         trace_destroy_fields(call);
2377         free_event_filter(call->filter);
2378         call->filter = NULL;
2379 }
2380
2381 static int probe_remove_event_call(struct trace_event_call *call)
2382 {
2383         struct trace_array *tr;
2384         struct trace_event_file *file;
2385
2386 #ifdef CONFIG_PERF_EVENTS
2387         if (call->perf_refcount)
2388                 return -EBUSY;
2389 #endif
2390         do_for_each_event_file(tr, file) {
2391                 if (file->event_call != call)
2392                         continue;
2393                 /*
2394                  * We can't rely on ftrace_event_enable_disable(enable => 0)
2395                  * we are going to do, EVENT_FILE_FL_SOFT_MODE can suppress
2396                  * TRACE_REG_UNREGISTER.
2397                  */
2398                 if (file->flags & EVENT_FILE_FL_ENABLED)
2399                         return -EBUSY;
2400                 /*
2401                  * The do_for_each_event_file_safe() is
2402                  * a double loop. After finding the call for this
2403                  * trace_array, we use break to jump to the next
2404                  * trace_array.
2405                  */
2406                 break;
2407         } while_for_each_event_file();
2408
2409         __trace_remove_event_call(call);
2410
2411         return 0;
2412 }
2413
2414 /* Remove an event_call */
2415 int trace_remove_event_call(struct trace_event_call *call)
2416 {
2417         int ret;
2418
2419         mutex_lock(&trace_types_lock);
2420         mutex_lock(&event_mutex);
2421         down_write(&trace_event_sem);
2422         ret = probe_remove_event_call(call);
2423         up_write(&trace_event_sem);
2424         mutex_unlock(&event_mutex);
2425         mutex_unlock(&trace_types_lock);
2426
2427         return ret;
2428 }
2429
2430 #define for_each_event(event, start, end)                       \
2431         for (event = start;                                     \
2432              (unsigned long)event < (unsigned long)end;         \
2433              event++)
2434
2435 #ifdef CONFIG_MODULES
2436
2437 static void trace_module_add_events(struct module *mod)
2438 {
2439         struct trace_event_call **call, **start, **end;
2440
2441         if (!mod->num_trace_events)
2442                 return;
2443
2444         /* Don't add infrastructure for mods without tracepoints */
2445         if (trace_module_has_bad_taint(mod)) {
2446                 pr_err("%s: module has bad taint, not creating trace events\n",
2447                        mod->name);
2448                 return;
2449         }
2450
2451         start = mod->trace_events;
2452         end = mod->trace_events + mod->num_trace_events;
2453
2454         for_each_event(call, start, end) {
2455                 __register_event(*call, mod);
2456                 __add_event_to_tracers(*call);
2457         }
2458 }
2459
2460 static void trace_module_remove_events(struct module *mod)
2461 {
2462         struct trace_event_call *call, *p;
2463         bool clear_trace = false;
2464
2465         down_write(&trace_event_sem);
2466         list_for_each_entry_safe(call, p, &ftrace_events, list) {
2467                 if (call->mod == mod) {
2468                         if (call->flags & TRACE_EVENT_FL_WAS_ENABLED)
2469                                 clear_trace = true;
2470                         __trace_remove_event_call(call);
2471                 }
2472         }
2473         up_write(&trace_event_sem);
2474
2475         /*
2476          * It is safest to reset the ring buffer if the module being unloaded
2477          * registered any events that were used. The only worry is if
2478          * a new module gets loaded, and takes on the same id as the events
2479          * of this module. When printing out the buffer, traced events left
2480          * over from this module may be passed to the new module events and
2481          * unexpected results may occur.
2482          */
2483         if (clear_trace)
2484                 tracing_reset_all_online_cpus();
2485 }
2486
2487 static int trace_module_notify(struct notifier_block *self,
2488                                unsigned long val, void *data)
2489 {
2490         struct module *mod = data;
2491
2492         mutex_lock(&trace_types_lock);
2493         mutex_lock(&event_mutex);
2494         switch (val) {
2495         case MODULE_STATE_COMING:
2496                 trace_module_add_events(mod);
2497                 break;
2498         case MODULE_STATE_GOING:
2499                 trace_module_remove_events(mod);
2500                 break;
2501         }
2502         mutex_unlock(&event_mutex);
2503         mutex_unlock(&trace_types_lock);
2504
2505         return 0;
2506 }
2507
2508 static struct notifier_block trace_module_nb = {
2509         .notifier_call = trace_module_notify,
2510         .priority = 1, /* higher than trace.c module notify */
2511 };
2512 #endif /* CONFIG_MODULES */
2513
2514 /* Create a new event directory structure for a trace directory. */
2515 static void
2516 __trace_add_event_dirs(struct trace_array *tr)
2517 {
2518         struct trace_event_call *call;
2519         int ret;
2520
2521         list_for_each_entry(call, &ftrace_events, list) {
2522                 ret = __trace_add_new_event(call, tr);
2523                 if (ret < 0)
2524                         pr_warn("Could not create directory for event %s\n",
2525                                 trace_event_name(call));
2526         }
2527 }
2528
2529 struct trace_event_file *
2530 find_event_file(struct trace_array *tr, const char *system,  const char *event)
2531 {
2532         struct trace_event_file *file;
2533         struct trace_event_call *call;
2534         const char *name;
2535
2536         list_for_each_entry(file, &tr->events, list) {
2537
2538                 call = file->event_call;
2539                 name = trace_event_name(call);
2540
2541                 if (!name || !call->class || !call->class->reg)
2542                         continue;
2543
2544                 if (call->flags & TRACE_EVENT_FL_IGNORE_ENABLE)
2545                         continue;
2546
2547                 if (strcmp(event, name) == 0 &&
2548                     strcmp(system, call->class->system) == 0)
2549                         return file;
2550         }
2551         return NULL;
2552 }
2553
2554 #ifdef CONFIG_DYNAMIC_FTRACE
2555
2556 /* Avoid typos */
2557 #define ENABLE_EVENT_STR        "enable_event"
2558 #define DISABLE_EVENT_STR       "disable_event"
2559
2560 struct event_probe_data {
2561         struct trace_event_file *file;
2562         unsigned long                   count;
2563         int                             ref;
2564         bool                            enable;
2565 };
2566
2567 static void
2568 event_enable_probe(unsigned long ip, unsigned long parent_ip, void **_data)
2569 {
2570         struct event_probe_data **pdata = (struct event_probe_data **)_data;
2571         struct event_probe_data *data = *pdata;
2572
2573         if (!data)
2574                 return;
2575
2576         if (data->enable)
2577                 clear_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &data->file->flags);
2578         else
2579                 set_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &data->file->flags);
2580 }
2581
2582 static void
2583 event_enable_count_probe(unsigned long ip, unsigned long parent_ip, void **_data)
2584 {
2585         struct event_probe_data **pdata = (struct event_probe_data **)_data;
2586         struct event_probe_data *data = *pdata;
2587
2588         if (!data)
2589                 return;
2590
2591         if (!data->count)
2592                 return;
2593
2594         /* Skip if the event is in a state we want to switch to */
2595         if (data->enable == !(data->file->flags & EVENT_FILE_FL_SOFT_DISABLED))
2596                 return;
2597
2598         if (data->count != -1)
2599                 (data->count)--;
2600
2601         event_enable_probe(ip, parent_ip, _data);
2602 }
2603
2604 static int
2605 event_enable_print(struct seq_file *m, unsigned long ip,
2606                       struct ftrace_probe_ops *ops, void *_data)
2607 {
2608         struct event_probe_data *data = _data;
2609
2610         seq_printf(m, "%ps:", (void *)ip);
2611
2612         seq_printf(m, "%s:%s:%s",
2613                    data->enable ? ENABLE_EVENT_STR : DISABLE_EVENT_STR,
2614                    data->file->event_call->class->system,
2615                    trace_event_name(data->file->event_call));
2616
2617         if (data->count == -1)
2618                 seq_puts(m, ":unlimited\n");
2619         else
2620                 seq_printf(m, ":count=%ld\n", data->count);
2621
2622         return 0;
2623 }
2624
2625 static int
2626 event_enable_init(struct ftrace_probe_ops *ops, unsigned long ip,
2627                   void **_data)
2628 {
2629         struct event_probe_data **pdata = (struct event_probe_data **)_data;
2630         struct event_probe_data *data = *pdata;
2631
2632         data->ref++;
2633         return 0;
2634 }
2635
2636 static void
2637 event_enable_free(struct ftrace_probe_ops *ops, unsigned long ip,
2638                   void **_data)
2639 {
2640         struct event_probe_data **pdata = (struct event_probe_data **)_data;
2641         struct event_probe_data *data = *pdata;
2642
2643         if (WARN_ON_ONCE(data->ref <= 0))
2644                 return;
2645
2646         data->ref--;
2647         if (!data->ref) {
2648                 /* Remove the SOFT_MODE flag */
2649                 __ftrace_event_enable_disable(data->file, 0, 1);
2650                 module_put(data->file->event_call->mod);
2651                 kfree(data);
2652         }
2653         *pdata = NULL;
2654 }
2655
2656 static struct ftrace_probe_ops event_enable_probe_ops = {
2657         .func                   = event_enable_probe,
2658         .print                  = event_enable_print,
2659         .init                   = event_enable_init,
2660         .free                   = event_enable_free,
2661 };
2662
2663 static struct ftrace_probe_ops event_enable_count_probe_ops = {
2664         .func                   = event_enable_count_probe,
2665         .print                  = event_enable_print,
2666         .init                   = event_enable_init,
2667         .free                   = event_enable_free,
2668 };
2669
2670 static struct ftrace_probe_ops event_disable_probe_ops = {
2671         .func                   = event_enable_probe,
2672         .print                  = event_enable_print,
2673         .init                   = event_enable_init,
2674         .free                   = event_enable_free,
2675 };
2676
2677 static struct ftrace_probe_ops event_disable_count_probe_ops = {
2678         .func                   = event_enable_count_probe,
2679         .print                  = event_enable_print,
2680         .init                   = event_enable_init,
2681         .free                   = event_enable_free,
2682 };
2683
2684 static int
2685 event_enable_func(struct ftrace_hash *hash,
2686                   char *glob, char *cmd, char *param, int enabled)
2687 {
2688         struct trace_array *tr = top_trace_array();
2689         struct trace_event_file *file;
2690         struct ftrace_probe_ops *ops;
2691         struct event_probe_data *data;
2692         const char *system;
2693         const char *event;
2694         char *number;
2695         bool enable;
2696         int ret;
2697
2698         if (!tr)
2699                 return -ENODEV;
2700
2701         /* hash funcs only work with set_ftrace_filter */
2702         if (!enabled || !param)
2703                 return -EINVAL;
2704
2705         system = strsep(&param, ":");
2706         if (!param)
2707                 return -EINVAL;
2708
2709         event = strsep(&param, ":");
2710
2711         mutex_lock(&event_mutex);
2712
2713         ret = -EINVAL;
2714         file = find_event_file(tr, system, event);
2715         if (!file)
2716                 goto out;
2717
2718         enable = strcmp(cmd, ENABLE_EVENT_STR) == 0;
2719
2720         if (enable)
2721                 ops = param ? &event_enable_count_probe_ops : &event_enable_probe_ops;
2722         else
2723                 ops = param ? &event_disable_count_probe_ops : &event_disable_probe_ops;
2724
2725         if (glob[0] == '!') {
2726                 unregister_ftrace_function_probe_func(glob+1, ops);
2727                 ret = 0;
2728                 goto out;
2729         }
2730
2731         ret = -ENOMEM;
2732         data = kzalloc(sizeof(*data), GFP_KERNEL);
2733         if (!data)
2734                 goto out;
2735
2736         data->enable = enable;
2737         data->count = -1;
2738         data->file = file;
2739
2740         if (!param)
2741                 goto out_reg;
2742
2743         number = strsep(&param, ":");
2744
2745         ret = -EINVAL;
2746         if (!strlen(number))
2747                 goto out_free;
2748
2749         /*
2750          * We use the callback data field (which is a pointer)
2751          * as our counter.
2752          */
2753         ret = kstrtoul(number, 0, &data->count);
2754         if (ret)
2755                 goto out_free;
2756
2757  out_reg:
2758         /* Don't let event modules unload while probe registered */
2759         ret = try_module_get(file->event_call->mod);
2760         if (!ret) {
2761                 ret = -EBUSY;
2762                 goto out_free;
2763         }
2764
2765         ret = __ftrace_event_enable_disable(file, 1, 1);
2766         if (ret < 0)
2767                 goto out_put;
2768         ret = register_ftrace_function_probe(glob, ops, data);
2769         /*
2770          * The above returns on success the # of functions enabled,
2771          * but if it didn't find any functions it returns zero.
2772          * Consider no functions a failure too.
2773          */
2774         if (!ret) {
2775                 ret = -ENOENT;
2776                 goto out_disable;
2777         } else if (ret < 0)
2778                 goto out_disable;
2779         /* Just return zero, not the number of enabled functions */
2780         ret = 0;
2781  out:
2782         mutex_unlock(&event_mutex);
2783         return ret;
2784
2785  out_disable:
2786         __ftrace_event_enable_disable(file, 0, 1);
2787  out_put:
2788         module_put(file->event_call->mod);
2789  out_free:
2790         kfree(data);
2791         goto out;
2792 }
2793
2794 static struct ftrace_func_command event_enable_cmd = {
2795         .name                   = ENABLE_EVENT_STR,
2796         .func                   = event_enable_func,
2797 };
2798
2799 static struct ftrace_func_command event_disable_cmd = {
2800         .name                   = DISABLE_EVENT_STR,
2801         .func                   = event_enable_func,
2802 };
2803
2804 static __init int register_event_cmds(void)
2805 {
2806         int ret;
2807
2808         ret = register_ftrace_command(&event_enable_cmd);
2809         if (WARN_ON(ret < 0))
2810                 return ret;
2811         ret = register_ftrace_command(&event_disable_cmd);
2812         if (WARN_ON(ret < 0))
2813                 unregister_ftrace_command(&event_enable_cmd);
2814         return ret;
2815 }
2816 #else
2817 static inline int register_event_cmds(void) { return 0; }
2818 #endif /* CONFIG_DYNAMIC_FTRACE */
2819
2820 /*
2821  * The top level array has already had its trace_event_file
2822  * descriptors created in order to allow for early events to
2823  * be recorded. This function is called after the tracefs has been
2824  * initialized, and we now have to create the files associated
2825  * to the events.
2826  */
2827 static __init void
2828 __trace_early_add_event_dirs(struct trace_array *tr)
2829 {
2830         struct trace_event_file *file;
2831         int ret;
2832
2833
2834         list_for_each_entry(file, &tr->events, list) {
2835                 ret = event_create_dir(tr->event_dir, file);
2836                 if (ret < 0)
2837                         pr_warn("Could not create directory for event %s\n",
2838                                 trace_event_name(file->event_call));
2839         }
2840 }
2841
2842 /*
2843  * For early boot up, the top trace array requires to have
2844  * a list of events that can be enabled. This must be done before
2845  * the filesystem is set up in order to allow events to be traced
2846  * early.
2847  */
2848 static __init void
2849 __trace_early_add_events(struct trace_array *tr)
2850 {
2851         struct trace_event_call *call;
2852         int ret;
2853
2854         list_for_each_entry(call, &ftrace_events, list) {
2855                 /* Early boot up should not have any modules loaded */
2856                 if (WARN_ON_ONCE(call->mod))
2857                         continue;
2858
2859                 ret = __trace_early_add_new_event(call, tr);
2860                 if (ret < 0)
2861                         pr_warn("Could not create early event %s\n",
2862                                 trace_event_name(call));
2863         }
2864 }
2865
2866 /* Remove the event directory structure for a trace directory. */
2867 static void
2868 __trace_remove_event_dirs(struct trace_array *tr)
2869 {
2870         struct trace_event_file *file, *next;
2871
2872         list_for_each_entry_safe(file, next, &tr->events, list)
2873                 remove_event_file_dir(file);
2874 }
2875
2876 static void __add_event_to_tracers(struct trace_event_call *call)
2877 {
2878         struct trace_array *tr;
2879
2880         list_for_each_entry(tr, &ftrace_trace_arrays, list)
2881                 __trace_add_new_event(call, tr);
2882 }
2883
2884 extern struct trace_event_call *__start_ftrace_events[];
2885 extern struct trace_event_call *__stop_ftrace_events[];
2886
2887 static char bootup_event_buf[COMMAND_LINE_SIZE] __initdata;
2888
2889 static __init int setup_trace_event(char *str)
2890 {
2891         strlcpy(bootup_event_buf, str, COMMAND_LINE_SIZE);
2892         ring_buffer_expanded = true;
2893         tracing_selftest_disabled = true;
2894
2895         return 1;
2896 }
2897 __setup("trace_event=", setup_trace_event);
2898
2899 /* Expects to have event_mutex held when called */
2900 static int
2901 create_event_toplevel_files(struct dentry *parent, struct trace_array *tr)
2902 {
2903         struct dentry *d_events;
2904         struct dentry *entry;
2905
2906         entry = tracefs_create_file("set_event", 0644, parent,
2907                                     tr, &ftrace_set_event_fops);
2908         if (!entry) {
2909                 pr_warn("Could not create tracefs 'set_event' entry\n");
2910                 return -ENOMEM;
2911         }
2912
2913         d_events = tracefs_create_dir("events", parent);
2914         if (!d_events) {
2915                 pr_warn("Could not create tracefs 'events' directory\n");
2916                 return -ENOMEM;
2917         }
2918
2919         entry = tracefs_create_file("set_event_pid", 0644, parent,
2920                                     tr, &ftrace_set_event_pid_fops);
2921
2922         /* ring buffer internal formats */
2923         trace_create_file("header_page", 0444, d_events,
2924                           ring_buffer_print_page_header,
2925                           &ftrace_show_header_fops);
2926
2927         trace_create_file("header_event", 0444, d_events,
2928                           ring_buffer_print_entry_header,
2929                           &ftrace_show_header_fops);
2930
2931         trace_create_file("enable", 0644, d_events,
2932                           tr, &ftrace_tr_enable_fops);
2933
2934         tr->event_dir = d_events;
2935
2936         return 0;
2937 }
2938
2939 /**
2940  * event_trace_add_tracer - add a instance of a trace_array to events
2941  * @parent: The parent dentry to place the files/directories for events in
2942  * @tr: The trace array associated with these events
2943  *
2944  * When a new instance is created, it needs to set up its events
2945  * directory, as well as other files associated with events. It also
2946  * creates the event hierachry in the @parent/events directory.
2947  *
2948  * Returns 0 on success.
2949  */
2950 int event_trace_add_tracer(struct dentry *parent, struct trace_array *tr)
2951 {
2952         int ret;
2953
2954         mutex_lock(&event_mutex);
2955
2956         ret = create_event_toplevel_files(parent, tr);
2957         if (ret)
2958                 goto out_unlock;
2959
2960         down_write(&trace_event_sem);
2961         __trace_add_event_dirs(tr);
2962         up_write(&trace_event_sem);
2963
2964  out_unlock:
2965         mutex_unlock(&event_mutex);
2966
2967         return ret;
2968 }
2969
2970 /*
2971  * The top trace array already had its file descriptors created.
2972  * Now the files themselves need to be created.
2973  */
2974 static __init int
2975 early_event_add_tracer(struct dentry *parent, struct trace_array *tr)
2976 {
2977         int ret;
2978
2979         mutex_lock(&event_mutex);
2980
2981         ret = create_event_toplevel_files(parent, tr);
2982         if (ret)
2983                 goto out_unlock;
2984
2985         down_write(&trace_event_sem);
2986         __trace_early_add_event_dirs(tr);
2987         up_write(&trace_event_sem);
2988
2989  out_unlock:
2990         mutex_unlock(&event_mutex);
2991
2992         return ret;
2993 }
2994
2995 int event_trace_del_tracer(struct trace_array *tr)
2996 {
2997         mutex_lock(&event_mutex);
2998
2999         /* Disable any event triggers and associated soft-disabled events */
3000         clear_event_triggers(tr);
3001
3002         /* Clear the pid list */
3003         __ftrace_clear_event_pids(tr);
3004
3005         /* Disable any running events */
3006         __ftrace_set_clr_event_nolock(tr, NULL, NULL, NULL, 0);
3007
3008         /* Access to events are within rcu_read_lock_sched() */
3009         synchronize_sched();
3010
3011         down_write(&trace_event_sem);
3012         __trace_remove_event_dirs(tr);
3013         tracefs_remove_recursive(tr->event_dir);
3014         up_write(&trace_event_sem);
3015
3016         tr->event_dir = NULL;
3017
3018         mutex_unlock(&event_mutex);
3019
3020         return 0;
3021 }
3022
3023 static __init int event_trace_memsetup(void)
3024 {
3025         field_cachep = KMEM_CACHE(ftrace_event_field, SLAB_PANIC);
3026         file_cachep = KMEM_CACHE(trace_event_file, SLAB_PANIC);
3027         return 0;
3028 }
3029
3030 static __init void
3031 early_enable_events(struct trace_array *tr, bool disable_first)
3032 {
3033         char *buf = bootup_event_buf;
3034         char *token;
3035         int ret;
3036
3037         while (true) {
3038                 token = strsep(&buf, ",");
3039
3040                 if (!token)
3041                         break;
3042                 if (!*token)
3043                         continue;
3044
3045                 /* Restarting syscalls requires that we stop them first */
3046                 if (disable_first)
3047                         ftrace_set_clr_event(tr, token, 0);
3048
3049                 ret = ftrace_set_clr_event(tr, token, 1);
3050                 if (ret)
3051                         pr_warn("Failed to enable trace event: %s\n", token);
3052
3053                 /* Put back the comma to allow this to be called again */
3054                 if (buf)
3055                         *(buf - 1) = ',';
3056         }
3057 }
3058
3059 static __init int event_trace_enable(void)
3060 {
3061         struct trace_array *tr = top_trace_array();
3062         struct trace_event_call **iter, *call;
3063         int ret;
3064
3065         if (!tr)
3066                 return -ENODEV;
3067
3068         for_each_event(iter, __start_ftrace_events, __stop_ftrace_events) {
3069
3070                 call = *iter;
3071                 ret = event_init(call);
3072                 if (!ret)
3073                         list_add(&call->list, &ftrace_events);
3074         }
3075
3076         /*
3077          * We need the top trace array to have a working set of trace
3078          * points at early init, before the debug files and directories
3079          * are created. Create the file entries now, and attach them
3080          * to the actual file dentries later.
3081          */
3082         __trace_early_add_events(tr);
3083
3084         early_enable_events(tr, false);
3085
3086         trace_printk_start_comm();
3087
3088         register_event_cmds();
3089
3090         register_trigger_cmds();
3091
3092         return 0;
3093 }
3094
3095 /*
3096  * event_trace_enable() is called from trace_event_init() first to
3097  * initialize events and perhaps start any events that are on the
3098  * command line. Unfortunately, there are some events that will not
3099  * start this early, like the system call tracepoints that need
3100  * to set the TIF_SYSCALL_TRACEPOINT flag of pid 1. But event_trace_enable()
3101  * is called before pid 1 starts, and this flag is never set, making
3102  * the syscall tracepoint never get reached, but the event is enabled
3103  * regardless (and not doing anything).
3104  */
3105 static __init int event_trace_enable_again(void)
3106 {
3107         struct trace_array *tr;
3108
3109         tr = top_trace_array();
3110         if (!tr)
3111                 return -ENODEV;
3112
3113         early_enable_events(tr, true);
3114
3115         return 0;
3116 }
3117
3118 early_initcall(event_trace_enable_again);
3119
3120 static __init int event_trace_init(void)
3121 {
3122         struct trace_array *tr;
3123         struct dentry *d_tracer;
3124         struct dentry *entry;
3125         int ret;
3126
3127         tr = top_trace_array();
3128         if (!tr)
3129                 return -ENODEV;
3130
3131         d_tracer = tracing_init_dentry();
3132         if (IS_ERR(d_tracer))
3133                 return 0;
3134
3135         entry = tracefs_create_file("available_events", 0444, d_tracer,
3136                                     tr, &ftrace_avail_fops);
3137         if (!entry)
3138                 pr_warn("Could not create tracefs 'available_events' entry\n");
3139
3140         if (trace_define_generic_fields())
3141                 pr_warn("tracing: Failed to allocated generic fields");
3142
3143         if (trace_define_common_fields())
3144                 pr_warn("tracing: Failed to allocate common fields");
3145
3146         ret = early_event_add_tracer(d_tracer, tr);
3147         if (ret)
3148                 return ret;
3149
3150 #ifdef CONFIG_MODULES
3151         ret = register_module_notifier(&trace_module_nb);
3152         if (ret)
3153                 pr_warn("Failed to register trace events module notifier\n");
3154 #endif
3155         return 0;
3156 }
3157
3158 void __init trace_event_init(void)
3159 {
3160         event_trace_memsetup();
3161         init_ftrace_syscalls();
3162         event_trace_enable();
3163 }
3164
3165 fs_initcall(event_trace_init);
3166
3167 #ifdef CONFIG_FTRACE_STARTUP_TEST
3168
3169 static DEFINE_SPINLOCK(test_spinlock);
3170 static DEFINE_SPINLOCK(test_spinlock_irq);
3171 static DEFINE_MUTEX(test_mutex);
3172
3173 static __init void test_work(struct work_struct *dummy)
3174 {
3175         spin_lock(&test_spinlock);
3176         spin_lock_irq(&test_spinlock_irq);
3177         udelay(1);
3178         spin_unlock_irq(&test_spinlock_irq);
3179         spin_unlock(&test_spinlock);
3180
3181         mutex_lock(&test_mutex);
3182         msleep(1);
3183         mutex_unlock(&test_mutex);
3184 }
3185
3186 static __init int event_test_thread(void *unused)
3187 {
3188         void *test_malloc;
3189
3190         test_malloc = kmalloc(1234, GFP_KERNEL);
3191         if (!test_malloc)
3192                 pr_info("failed to kmalloc\n");
3193
3194         schedule_on_each_cpu(test_work);
3195
3196         kfree(test_malloc);
3197
3198         set_current_state(TASK_INTERRUPTIBLE);
3199         while (!kthread_should_stop()) {
3200                 schedule();
3201                 set_current_state(TASK_INTERRUPTIBLE);
3202         }
3203         __set_current_state(TASK_RUNNING);
3204
3205         return 0;
3206 }
3207
3208 /*
3209  * Do various things that may trigger events.
3210  */
3211 static __init void event_test_stuff(void)
3212 {
3213         struct task_struct *test_thread;
3214
3215         test_thread = kthread_run(event_test_thread, NULL, "test-events");
3216         msleep(1);
3217         kthread_stop(test_thread);
3218 }
3219
3220 /*
3221  * For every trace event defined, we will test each trace point separately,
3222  * and then by groups, and finally all trace points.
3223  */
3224 static __init void event_trace_self_tests(void)
3225 {
3226         struct trace_subsystem_dir *dir;
3227         struct trace_event_file *file;
3228         struct trace_event_call *call;
3229         struct event_subsystem *system;
3230         struct trace_array *tr;
3231         int ret;
3232
3233         tr = top_trace_array();
3234         if (!tr)
3235                 return;
3236
3237         pr_info("Running tests on trace events:\n");
3238
3239         list_for_each_entry(file, &tr->events, list) {
3240
3241                 call = file->event_call;
3242
3243                 /* Only test those that have a probe */
3244                 if (!call->class || !call->class->probe)
3245                         continue;
3246
3247 /*
3248  * Testing syscall events here is pretty useless, but
3249  * we still do it if configured. But this is time consuming.
3250  * What we really need is a user thread to perform the
3251  * syscalls as we test.
3252  */
3253 #ifndef CONFIG_EVENT_TRACE_TEST_SYSCALLS
3254                 if (call->class->system &&
3255                     strcmp(call->class->system, "syscalls") == 0)
3256                         continue;
3257 #endif
3258
3259                 pr_info("Testing event %s: ", trace_event_name(call));
3260
3261                 /*
3262                  * If an event is already enabled, someone is using
3263                  * it and the self test should not be on.
3264                  */
3265                 if (file->flags & EVENT_FILE_FL_ENABLED) {
3266                         pr_warn("Enabled event during self test!\n");
3267                         WARN_ON_ONCE(1);
3268                         continue;
3269                 }
3270
3271                 ftrace_event_enable_disable(file, 1);
3272                 event_test_stuff();
3273                 ftrace_event_enable_disable(file, 0);
3274
3275                 pr_cont("OK\n");
3276         }
3277
3278         /* Now test at the sub system level */
3279
3280         pr_info("Running tests on trace event systems:\n");
3281
3282         list_for_each_entry(dir, &tr->systems, list) {
3283
3284                 system = dir->subsystem;
3285
3286                 /* the ftrace system is special, skip it */
3287                 if (strcmp(system->name, "ftrace") == 0)
3288                         continue;
3289
3290                 pr_info("Testing event system %s: ", system->name);
3291
3292                 ret = __ftrace_set_clr_event(tr, NULL, system->name, NULL, 1);
3293                 if (WARN_ON_ONCE(ret)) {
3294                         pr_warn("error enabling system %s\n",
3295                                 system->name);
3296                         continue;
3297                 }
3298
3299                 event_test_stuff();
3300
3301                 ret = __ftrace_set_clr_event(tr, NULL, system->name, NULL, 0);
3302                 if (WARN_ON_ONCE(ret)) {
3303                         pr_warn("error disabling system %s\n",
3304                                 system->name);
3305                         continue;
3306                 }
3307
3308                 pr_cont("OK\n");
3309         }
3310
3311         /* Test with all events enabled */
3312
3313         pr_info("Running tests on all trace events:\n");
3314         pr_info("Testing all events: ");
3315
3316         ret = __ftrace_set_clr_event(tr, NULL, NULL, NULL, 1);
3317         if (WARN_ON_ONCE(ret)) {
3318                 pr_warn("error enabling all events\n");
3319                 return;
3320         }
3321
3322         event_test_stuff();
3323
3324         /* reset sysname */
3325         ret = __ftrace_set_clr_event(tr, NULL, NULL, NULL, 0);
3326         if (WARN_ON_ONCE(ret)) {
3327                 pr_warn("error disabling all events\n");
3328                 return;
3329         }
3330
3331         pr_cont("OK\n");
3332 }
3333
3334 #ifdef CONFIG_FUNCTION_TRACER
3335
3336 static DEFINE_PER_CPU(atomic_t, ftrace_test_event_disable);
3337
3338 static struct trace_array *event_tr;
3339
3340 static void __init
3341 function_test_events_call(unsigned long ip, unsigned long parent_ip,
3342                           struct ftrace_ops *op, struct pt_regs *pt_regs)
3343 {
3344         struct ring_buffer_event *event;
3345         struct ring_buffer *buffer;
3346         struct ftrace_entry *entry;
3347         unsigned long flags;
3348         long disabled;
3349         int cpu;
3350         int pc;
3351
3352         pc = preempt_count();
3353         preempt_disable_notrace();
3354         cpu = raw_smp_processor_id();
3355         disabled = atomic_inc_return(&per_cpu(ftrace_test_event_disable, cpu));
3356
3357         if (disabled != 1)
3358                 goto out;
3359
3360         local_save_flags(flags);
3361
3362         event = trace_current_buffer_lock_reserve(&buffer,
3363                                                   TRACE_FN, sizeof(*entry),
3364                                                   flags, pc);
3365         if (!event)
3366                 goto out;
3367         entry   = ring_buffer_event_data(event);
3368         entry->ip                       = ip;
3369         entry->parent_ip                = parent_ip;
3370
3371         trace_buffer_unlock_commit(event_tr, buffer, event, flags, pc);
3372
3373  out:
3374         atomic_dec(&per_cpu(ftrace_test_event_disable, cpu));
3375         preempt_enable_notrace();
3376 }
3377
3378 static struct ftrace_ops trace_ops __initdata  =
3379 {
3380         .func = function_test_events_call,
3381         .flags = FTRACE_OPS_FL_RECURSION_SAFE,
3382 };
3383
3384 static __init void event_trace_self_test_with_function(void)
3385 {
3386         int ret;
3387         event_tr = top_trace_array();
3388         if (WARN_ON(!event_tr))
3389                 return;
3390         ret = register_ftrace_function(&trace_ops);
3391         if (WARN_ON(ret < 0)) {
3392                 pr_info("Failed to enable function tracer for event tests\n");
3393                 return;
3394         }
3395         pr_info("Running tests again, along with the function tracer\n");
3396         event_trace_self_tests();
3397         unregister_ftrace_function(&trace_ops);
3398 }
3399 #else
3400 static __init void event_trace_self_test_with_function(void)
3401 {
3402 }
3403 #endif
3404
3405 static __init int event_trace_self_tests_init(void)
3406 {
3407         if (!tracing_selftest_disabled) {
3408                 event_trace_self_tests();
3409                 event_trace_self_test_with_function();
3410         }
3411
3412         return 0;
3413 }
3414
3415 late_initcall(event_trace_self_tests_init);
3416
3417 #endif