422f99dfe699638a2503908f567195cfff2814f0
[linux-3.10.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 #include <linux/workqueue.h>
12 #include <linux/spinlock.h>
13 #include <linux/kthread.h>
14 #include <linux/debugfs.h>
15 #include <linux/uaccess.h>
16 #include <linux/module.h>
17 #include <linux/ctype.h>
18 #include <linux/slab.h>
19 #include <linux/delay.h>
20
21 #include <asm/setup.h>
22
23 #include "trace_output.h"
24
25 #undef TRACE_SYSTEM
26 #define TRACE_SYSTEM "TRACE_SYSTEM"
27
28 DEFINE_MUTEX(event_mutex);
29
30 DEFINE_MUTEX(event_storage_mutex);
31 EXPORT_SYMBOL_GPL(event_storage_mutex);
32
33 char event_storage[EVENT_STORAGE_SIZE];
34 EXPORT_SYMBOL_GPL(event_storage);
35
36 LIST_HEAD(ftrace_events);
37 static LIST_HEAD(ftrace_common_fields);
38
39 #define GFP_TRACE (GFP_KERNEL | __GFP_ZERO)
40
41 static struct kmem_cache *field_cachep;
42 static struct kmem_cache *file_cachep;
43
44 #define SYSTEM_FL_FREE_NAME             (1 << 31)
45
46 static inline int system_refcount(struct event_subsystem *system)
47 {
48         return system->ref_count & ~SYSTEM_FL_FREE_NAME;
49 }
50
51 static int system_refcount_inc(struct event_subsystem *system)
52 {
53         return (system->ref_count++) & ~SYSTEM_FL_FREE_NAME;
54 }
55
56 static int system_refcount_dec(struct event_subsystem *system)
57 {
58         return (--system->ref_count) & ~SYSTEM_FL_FREE_NAME;
59 }
60
61 /* Double loops, do not use break, only goto's work */
62 #define do_for_each_event_file(tr, file)                        \
63         list_for_each_entry(tr, &ftrace_trace_arrays, list) {   \
64                 list_for_each_entry(file, &tr->events, list)
65
66 #define do_for_each_event_file_safe(tr, file)                   \
67         list_for_each_entry(tr, &ftrace_trace_arrays, list) {   \
68                 struct ftrace_event_file *___n;                         \
69                 list_for_each_entry_safe(file, ___n, &tr->events, list)
70
71 #define while_for_each_event_file()             \
72         }
73
74 static struct list_head *
75 trace_get_fields(struct ftrace_event_call *event_call)
76 {
77         if (!event_call->class->get_fields)
78                 return &event_call->class->fields;
79         return event_call->class->get_fields(event_call);
80 }
81
82 static struct ftrace_event_field *
83 __find_event_field(struct list_head *head, char *name)
84 {
85         struct ftrace_event_field *field;
86
87         list_for_each_entry(field, head, link) {
88                 if (!strcmp(field->name, name))
89                         return field;
90         }
91
92         return NULL;
93 }
94
95 struct ftrace_event_field *
96 trace_find_event_field(struct ftrace_event_call *call, char *name)
97 {
98         struct ftrace_event_field *field;
99         struct list_head *head;
100
101         field = __find_event_field(&ftrace_common_fields, name);
102         if (field)
103                 return field;
104
105         head = trace_get_fields(call);
106         return __find_event_field(head, name);
107 }
108
109 static int __trace_define_field(struct list_head *head, const char *type,
110                                 const char *name, int offset, int size,
111                                 int is_signed, int filter_type)
112 {
113         struct ftrace_event_field *field;
114
115         field = kmem_cache_alloc(field_cachep, GFP_TRACE);
116         if (!field)
117                 return -ENOMEM;
118
119         field->name = name;
120         field->type = type;
121
122         if (filter_type == FILTER_OTHER)
123                 field->filter_type = filter_assign_type(type);
124         else
125                 field->filter_type = filter_type;
126
127         field->offset = offset;
128         field->size = size;
129         field->is_signed = is_signed;
130
131         list_add(&field->link, head);
132
133         return 0;
134 }
135
136 int trace_define_field(struct ftrace_event_call *call, const char *type,
137                        const char *name, int offset, int size, int is_signed,
138                        int filter_type)
139 {
140         struct list_head *head;
141
142         if (WARN_ON(!call->class))
143                 return 0;
144
145         head = trace_get_fields(call);
146         return __trace_define_field(head, type, name, offset, size,
147                                     is_signed, filter_type);
148 }
149 EXPORT_SYMBOL_GPL(trace_define_field);
150
151 #define __common_field(type, item)                                      \
152         ret = __trace_define_field(&ftrace_common_fields, #type,        \
153                                    "common_" #item,                     \
154                                    offsetof(typeof(ent), item),         \
155                                    sizeof(ent.item),                    \
156                                    is_signed_type(type), FILTER_OTHER); \
157         if (ret)                                                        \
158                 return ret;
159
160 static int trace_define_common_fields(void)
161 {
162         int ret;
163         struct trace_entry ent;
164
165         __common_field(unsigned short, type);
166         __common_field(unsigned char, flags);
167         __common_field(unsigned char, preempt_count);
168         __common_field(int, pid);
169
170         return ret;
171 }
172
173 static void trace_destroy_fields(struct ftrace_event_call *call)
174 {
175         struct ftrace_event_field *field, *next;
176         struct list_head *head;
177
178         head = trace_get_fields(call);
179         list_for_each_entry_safe(field, next, head, link) {
180                 list_del(&field->link);
181                 kmem_cache_free(field_cachep, field);
182         }
183 }
184
185 int trace_event_raw_init(struct ftrace_event_call *call)
186 {
187         int id;
188
189         id = register_ftrace_event(&call->event);
190         if (!id)
191                 return -ENODEV;
192
193         return 0;
194 }
195 EXPORT_SYMBOL_GPL(trace_event_raw_init);
196
197 int ftrace_event_reg(struct ftrace_event_call *call,
198                      enum trace_reg type, void *data)
199 {
200         struct ftrace_event_file *file = data;
201
202         switch (type) {
203         case TRACE_REG_REGISTER:
204                 return tracepoint_probe_register(call->name,
205                                                  call->class->probe,
206                                                  file);
207         case TRACE_REG_UNREGISTER:
208                 tracepoint_probe_unregister(call->name,
209                                             call->class->probe,
210                                             file);
211                 return 0;
212
213 #ifdef CONFIG_PERF_EVENTS
214         case TRACE_REG_PERF_REGISTER:
215                 return tracepoint_probe_register(call->name,
216                                                  call->class->perf_probe,
217                                                  call);
218         case TRACE_REG_PERF_UNREGISTER:
219                 tracepoint_probe_unregister(call->name,
220                                             call->class->perf_probe,
221                                             call);
222                 return 0;
223         case TRACE_REG_PERF_OPEN:
224         case TRACE_REG_PERF_CLOSE:
225         case TRACE_REG_PERF_ADD:
226         case TRACE_REG_PERF_DEL:
227                 return 0;
228 #endif
229         }
230         return 0;
231 }
232 EXPORT_SYMBOL_GPL(ftrace_event_reg);
233
234 void trace_event_enable_cmd_record(bool enable)
235 {
236         struct ftrace_event_file *file;
237         struct trace_array *tr;
238
239         mutex_lock(&event_mutex);
240         do_for_each_event_file(tr, file) {
241
242                 if (!(file->flags & FTRACE_EVENT_FL_ENABLED))
243                         continue;
244
245                 if (enable) {
246                         tracing_start_cmdline_record();
247                         set_bit(FTRACE_EVENT_FL_RECORDED_CMD_BIT, &file->flags);
248                 } else {
249                         tracing_stop_cmdline_record();
250                         clear_bit(FTRACE_EVENT_FL_RECORDED_CMD_BIT, &file->flags);
251                 }
252         } while_for_each_event_file();
253         mutex_unlock(&event_mutex);
254 }
255
256 static int __ftrace_event_enable_disable(struct ftrace_event_file *file,
257                                          int enable, int soft_disable)
258 {
259         struct ftrace_event_call *call = file->event_call;
260         int ret = 0;
261         int disable;
262
263         switch (enable) {
264         case 0:
265                 /*
266                  * When soft_disable is set and enable is cleared, the sm_ref
267                  * reference counter is decremented. If it reaches 0, we want
268                  * to clear the SOFT_DISABLED flag but leave the event in the
269                  * state that it was. That is, if the event was enabled and
270                  * SOFT_DISABLED isn't set, then do nothing. But if SOFT_DISABLED
271                  * is set we do not want the event to be enabled before we
272                  * clear the bit.
273                  *
274                  * When soft_disable is not set but the SOFT_MODE flag is,
275                  * we do nothing. Do not disable the tracepoint, otherwise
276                  * "soft enable"s (clearing the SOFT_DISABLED bit) wont work.
277                  */
278                 if (soft_disable) {
279                         if (atomic_dec_return(&file->sm_ref) > 0)
280                                 break;
281                         disable = file->flags & FTRACE_EVENT_FL_SOFT_DISABLED;
282                         clear_bit(FTRACE_EVENT_FL_SOFT_MODE_BIT, &file->flags);
283                 } else
284                         disable = !(file->flags & FTRACE_EVENT_FL_SOFT_MODE);
285
286                 if (disable && (file->flags & FTRACE_EVENT_FL_ENABLED)) {
287                         clear_bit(FTRACE_EVENT_FL_ENABLED_BIT, &file->flags);
288                         if (file->flags & FTRACE_EVENT_FL_RECORDED_CMD) {
289                                 tracing_stop_cmdline_record();
290                                 clear_bit(FTRACE_EVENT_FL_RECORDED_CMD_BIT, &file->flags);
291                         }
292                         call->class->reg(call, TRACE_REG_UNREGISTER, file);
293                 }
294                 /* If in SOFT_MODE, just set the SOFT_DISABLE_BIT */
295                 if (file->flags & FTRACE_EVENT_FL_SOFT_MODE)
296                         set_bit(FTRACE_EVENT_FL_SOFT_DISABLED_BIT, &file->flags);
297                 break;
298         case 1:
299                 /*
300                  * When soft_disable is set and enable is set, we want to
301                  * register the tracepoint for the event, but leave the event
302                  * as is. That means, if the event was already enabled, we do
303                  * nothing (but set SOFT_MODE). If the event is disabled, we
304                  * set SOFT_DISABLED before enabling the event tracepoint, so
305                  * it still seems to be disabled.
306                  */
307                 if (!soft_disable)
308                         clear_bit(FTRACE_EVENT_FL_SOFT_DISABLED_BIT, &file->flags);
309                 else {
310                         if (atomic_inc_return(&file->sm_ref) > 1)
311                                 break;
312                         set_bit(FTRACE_EVENT_FL_SOFT_MODE_BIT, &file->flags);
313                 }
314
315                 if (!(file->flags & FTRACE_EVENT_FL_ENABLED)) {
316
317                         /* Keep the event disabled, when going to SOFT_MODE. */
318                         if (soft_disable)
319                                 set_bit(FTRACE_EVENT_FL_SOFT_DISABLED_BIT, &file->flags);
320
321                         if (trace_flags & TRACE_ITER_RECORD_CMD) {
322                                 tracing_start_cmdline_record();
323                                 set_bit(FTRACE_EVENT_FL_RECORDED_CMD_BIT, &file->flags);
324                         }
325                         ret = call->class->reg(call, TRACE_REG_REGISTER, file);
326                         if (ret) {
327                                 tracing_stop_cmdline_record();
328                                 pr_info("event trace: Could not enable event "
329                                         "%s\n", call->name);
330                                 break;
331                         }
332                         set_bit(FTRACE_EVENT_FL_ENABLED_BIT, &file->flags);
333
334                         /* WAS_ENABLED gets set but never cleared. */
335                         call->flags |= TRACE_EVENT_FL_WAS_ENABLED;
336                 }
337                 break;
338         }
339
340         return ret;
341 }
342
343 static int ftrace_event_enable_disable(struct ftrace_event_file *file,
344                                        int enable)
345 {
346         return __ftrace_event_enable_disable(file, enable, 0);
347 }
348
349 static void ftrace_clear_events(struct trace_array *tr)
350 {
351         struct ftrace_event_file *file;
352
353         mutex_lock(&event_mutex);
354         list_for_each_entry(file, &tr->events, list) {
355                 ftrace_event_enable_disable(file, 0);
356         }
357         mutex_unlock(&event_mutex);
358 }
359
360 static void __put_system(struct event_subsystem *system)
361 {
362         struct event_filter *filter = system->filter;
363
364         WARN_ON_ONCE(system_refcount(system) == 0);
365         if (system_refcount_dec(system))
366                 return;
367
368         list_del(&system->list);
369
370         if (filter) {
371                 kfree(filter->filter_string);
372                 kfree(filter);
373         }
374         if (system->ref_count & SYSTEM_FL_FREE_NAME)
375                 kfree(system->name);
376         kfree(system);
377 }
378
379 static void __get_system(struct event_subsystem *system)
380 {
381         WARN_ON_ONCE(system_refcount(system) == 0);
382         system_refcount_inc(system);
383 }
384
385 static void __get_system_dir(struct ftrace_subsystem_dir *dir)
386 {
387         WARN_ON_ONCE(dir->ref_count == 0);
388         dir->ref_count++;
389         __get_system(dir->subsystem);
390 }
391
392 static void __put_system_dir(struct ftrace_subsystem_dir *dir)
393 {
394         WARN_ON_ONCE(dir->ref_count == 0);
395         /* If the subsystem is about to be freed, the dir must be too */
396         WARN_ON_ONCE(system_refcount(dir->subsystem) == 1 && dir->ref_count != 1);
397
398         __put_system(dir->subsystem);
399         if (!--dir->ref_count)
400                 kfree(dir);
401 }
402
403 static void put_system(struct ftrace_subsystem_dir *dir)
404 {
405         mutex_lock(&event_mutex);
406         __put_system_dir(dir);
407         mutex_unlock(&event_mutex);
408 }
409
410 static void remove_subsystem(struct ftrace_subsystem_dir *dir)
411 {
412         if (!dir)
413                 return;
414
415         if (!--dir->nr_events) {
416                 debugfs_remove_recursive(dir->entry);
417                 list_del(&dir->list);
418                 __put_system_dir(dir);
419         }
420 }
421
422 static void *event_file_data(struct file *filp)
423 {
424         return ACCESS_ONCE(file_inode(filp)->i_private);
425 }
426
427 static void remove_event_file_dir(struct ftrace_event_file *file)
428 {
429         list_del(&file->list);
430         debugfs_remove_recursive(file->dir);
431         remove_subsystem(file->system);
432         kmem_cache_free(file_cachep, file);
433 }
434
435 /*
436  * Open and update trace_array ref count.
437  * Must have the current trace_array passed to it.
438  */
439 static int tracing_open_generic_file(struct inode *inode, struct file *filp)
440 {
441         struct ftrace_event_file *file = inode->i_private;
442         struct trace_array *tr = file->tr;
443         int ret;
444
445         if (trace_array_get(tr) < 0)
446                 return -ENODEV;
447
448         ret = tracing_open_generic(inode, filp);
449         if (ret < 0)
450                 trace_array_put(tr);
451         return ret;
452 }
453
454 static int tracing_release_generic_file(struct inode *inode, struct file *filp)
455 {
456         struct ftrace_event_file *file = inode->i_private;
457         struct trace_array *tr = file->tr;
458
459         trace_array_put(tr);
460
461         return 0;
462 }
463
464 /*
465  * __ftrace_set_clr_event(NULL, NULL, NULL, set) will set/unset all events.
466  */
467 static int
468 __ftrace_set_clr_event_nolock(struct trace_array *tr, const char *match,
469                               const char *sub, const char *event, int set)
470 {
471         struct ftrace_event_file *file;
472         struct ftrace_event_call *call;
473         int ret = -EINVAL;
474
475         list_for_each_entry(file, &tr->events, list) {
476
477                 call = file->event_call;
478
479                 if (!call->name || !call->class || !call->class->reg)
480                         continue;
481
482                 if (call->flags & TRACE_EVENT_FL_IGNORE_ENABLE)
483                         continue;
484
485                 if (match &&
486                     strcmp(match, call->name) != 0 &&
487                     strcmp(match, call->class->system) != 0)
488                         continue;
489
490                 if (sub && strcmp(sub, call->class->system) != 0)
491                         continue;
492
493                 if (event && strcmp(event, call->name) != 0)
494                         continue;
495
496                 ftrace_event_enable_disable(file, set);
497
498                 ret = 0;
499         }
500
501         return ret;
502 }
503
504 static int __ftrace_set_clr_event(struct trace_array *tr, const char *match,
505                                   const char *sub, const char *event, int set)
506 {
507         int ret;
508
509         mutex_lock(&event_mutex);
510         ret = __ftrace_set_clr_event_nolock(tr, match, sub, event, set);
511         mutex_unlock(&event_mutex);
512
513         return ret;
514 }
515
516 static int ftrace_set_clr_event(struct trace_array *tr, char *buf, int set)
517 {
518         char *event = NULL, *sub = NULL, *match;
519
520         /*
521          * The buf format can be <subsystem>:<event-name>
522          *  *:<event-name> means any event by that name.
523          *  :<event-name> is the same.
524          *
525          *  <subsystem>:* means all events in that subsystem
526          *  <subsystem>: means the same.
527          *
528          *  <name> (no ':') means all events in a subsystem with
529          *  the name <name> or any event that matches <name>
530          */
531
532         match = strsep(&buf, ":");
533         if (buf) {
534                 sub = match;
535                 event = buf;
536                 match = NULL;
537
538                 if (!strlen(sub) || strcmp(sub, "*") == 0)
539                         sub = NULL;
540                 if (!strlen(event) || strcmp(event, "*") == 0)
541                         event = NULL;
542         }
543
544         return __ftrace_set_clr_event(tr, match, sub, event, set);
545 }
546
547 /**
548  * trace_set_clr_event - enable or disable an event
549  * @system: system name to match (NULL for any system)
550  * @event: event name to match (NULL for all events, within system)
551  * @set: 1 to enable, 0 to disable
552  *
553  * This is a way for other parts of the kernel to enable or disable
554  * event recording.
555  *
556  * Returns 0 on success, -EINVAL if the parameters do not match any
557  * registered events.
558  */
559 int trace_set_clr_event(const char *system, const char *event, int set)
560 {
561         struct trace_array *tr = top_trace_array();
562
563         return __ftrace_set_clr_event(tr, NULL, system, event, set);
564 }
565 EXPORT_SYMBOL_GPL(trace_set_clr_event);
566
567 /* 128 should be much more than enough */
568 #define EVENT_BUF_SIZE          127
569
570 static ssize_t
571 ftrace_event_write(struct file *file, const char __user *ubuf,
572                    size_t cnt, loff_t *ppos)
573 {
574         struct trace_parser parser;
575         struct seq_file *m = file->private_data;
576         struct trace_array *tr = m->private;
577         ssize_t read, ret;
578
579         if (!cnt)
580                 return 0;
581
582         ret = tracing_update_buffers();
583         if (ret < 0)
584                 return ret;
585
586         if (trace_parser_get_init(&parser, EVENT_BUF_SIZE + 1))
587                 return -ENOMEM;
588
589         read = trace_get_user(&parser, ubuf, cnt, ppos);
590
591         if (read >= 0 && trace_parser_loaded((&parser))) {
592                 int set = 1;
593
594                 if (*parser.buffer == '!')
595                         set = 0;
596
597                 parser.buffer[parser.idx] = 0;
598
599                 ret = ftrace_set_clr_event(tr, parser.buffer + !set, set);
600                 if (ret)
601                         goto out_put;
602         }
603
604         ret = read;
605
606  out_put:
607         trace_parser_put(&parser);
608
609         return ret;
610 }
611
612 static void *
613 t_next(struct seq_file *m, void *v, loff_t *pos)
614 {
615         struct ftrace_event_file *file = v;
616         struct ftrace_event_call *call;
617         struct trace_array *tr = m->private;
618
619         (*pos)++;
620
621         list_for_each_entry_continue(file, &tr->events, list) {
622                 call = file->event_call;
623                 /*
624                  * The ftrace subsystem is for showing formats only.
625                  * They can not be enabled or disabled via the event files.
626                  */
627                 if (call->class && call->class->reg)
628                         return file;
629         }
630
631         return NULL;
632 }
633
634 static void *t_start(struct seq_file *m, loff_t *pos)
635 {
636         struct ftrace_event_file *file;
637         struct trace_array *tr = m->private;
638         loff_t l;
639
640         mutex_lock(&event_mutex);
641
642         file = list_entry(&tr->events, struct ftrace_event_file, list);
643         for (l = 0; l <= *pos; ) {
644                 file = t_next(m, file, &l);
645                 if (!file)
646                         break;
647         }
648         return file;
649 }
650
651 static void *
652 s_next(struct seq_file *m, void *v, loff_t *pos)
653 {
654         struct ftrace_event_file *file = v;
655         struct trace_array *tr = m->private;
656
657         (*pos)++;
658
659         list_for_each_entry_continue(file, &tr->events, list) {
660                 if (file->flags & FTRACE_EVENT_FL_ENABLED)
661                         return file;
662         }
663
664         return NULL;
665 }
666
667 static void *s_start(struct seq_file *m, loff_t *pos)
668 {
669         struct ftrace_event_file *file;
670         struct trace_array *tr = m->private;
671         loff_t l;
672
673         mutex_lock(&event_mutex);
674
675         file = list_entry(&tr->events, struct ftrace_event_file, list);
676         for (l = 0; l <= *pos; ) {
677                 file = s_next(m, file, &l);
678                 if (!file)
679                         break;
680         }
681         return file;
682 }
683
684 static int t_show(struct seq_file *m, void *v)
685 {
686         struct ftrace_event_file *file = v;
687         struct ftrace_event_call *call = file->event_call;
688
689         if (strcmp(call->class->system, TRACE_SYSTEM) != 0)
690                 seq_printf(m, "%s:", call->class->system);
691         seq_printf(m, "%s\n", call->name);
692
693         return 0;
694 }
695
696 static void t_stop(struct seq_file *m, void *p)
697 {
698         mutex_unlock(&event_mutex);
699 }
700
701 static ssize_t
702 event_enable_read(struct file *filp, char __user *ubuf, size_t cnt,
703                   loff_t *ppos)
704 {
705         struct ftrace_event_file *file;
706         unsigned long flags;
707         char *buf;
708
709         mutex_lock(&event_mutex);
710         file = event_file_data(filp);
711         if (likely(file))
712                 flags = file->flags;
713         mutex_unlock(&event_mutex);
714
715         if (!file)
716                 return -ENODEV;
717
718         if (flags & FTRACE_EVENT_FL_ENABLED) {
719                 if (flags & FTRACE_EVENT_FL_SOFT_DISABLED)
720                         buf = "0*\n";
721                 else if (flags & FTRACE_EVENT_FL_SOFT_MODE)
722                         buf = "1*\n";
723                 else
724                         buf = "1\n";
725         } else
726                 buf = "0\n";
727
728         return simple_read_from_buffer(ubuf, cnt, ppos, buf, strlen(buf));
729 }
730
731 static ssize_t
732 event_enable_write(struct file *filp, const char __user *ubuf, size_t cnt,
733                    loff_t *ppos)
734 {
735         struct ftrace_event_file *file;
736         unsigned long val;
737         int ret;
738
739         ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
740         if (ret)
741                 return ret;
742
743         ret = tracing_update_buffers();
744         if (ret < 0)
745                 return ret;
746
747         switch (val) {
748         case 0:
749         case 1:
750                 ret = -ENODEV;
751                 mutex_lock(&event_mutex);
752                 file = event_file_data(filp);
753                 if (likely(file))
754                         ret = ftrace_event_enable_disable(file, val);
755                 mutex_unlock(&event_mutex);
756                 break;
757
758         default:
759                 return -EINVAL;
760         }
761
762         *ppos += cnt;
763
764         return ret ? ret : cnt;
765 }
766
767 static ssize_t
768 system_enable_read(struct file *filp, char __user *ubuf, size_t cnt,
769                    loff_t *ppos)
770 {
771         const char set_to_char[4] = { '?', '0', '1', 'X' };
772         struct ftrace_subsystem_dir *dir = filp->private_data;
773         struct event_subsystem *system = dir->subsystem;
774         struct ftrace_event_call *call;
775         struct ftrace_event_file *file;
776         struct trace_array *tr = dir->tr;
777         char buf[2];
778         int set = 0;
779         int ret;
780
781         mutex_lock(&event_mutex);
782         list_for_each_entry(file, &tr->events, list) {
783                 call = file->event_call;
784                 if (!call->name || !call->class || !call->class->reg)
785                         continue;
786
787                 if (system && strcmp(call->class->system, system->name) != 0)
788                         continue;
789
790                 /*
791                  * We need to find out if all the events are set
792                  * or if all events or cleared, or if we have
793                  * a mixture.
794                  */
795                 set |= (1 << !!(file->flags & FTRACE_EVENT_FL_ENABLED));
796
797                 /*
798                  * If we have a mixture, no need to look further.
799                  */
800                 if (set == 3)
801                         break;
802         }
803         mutex_unlock(&event_mutex);
804
805         buf[0] = set_to_char[set];
806         buf[1] = '\n';
807
808         ret = simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
809
810         return ret;
811 }
812
813 static ssize_t
814 system_enable_write(struct file *filp, const char __user *ubuf, size_t cnt,
815                     loff_t *ppos)
816 {
817         struct ftrace_subsystem_dir *dir = filp->private_data;
818         struct event_subsystem *system = dir->subsystem;
819         const char *name = NULL;
820         unsigned long val;
821         ssize_t ret;
822
823         ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
824         if (ret)
825                 return ret;
826
827         ret = tracing_update_buffers();
828         if (ret < 0)
829                 return ret;
830
831         if (val != 0 && val != 1)
832                 return -EINVAL;
833
834         /*
835          * Opening of "enable" adds a ref count to system,
836          * so the name is safe to use.
837          */
838         if (system)
839                 name = system->name;
840
841         ret = __ftrace_set_clr_event(dir->tr, NULL, name, NULL, val);
842         if (ret)
843                 goto out;
844
845         ret = cnt;
846
847 out:
848         *ppos += cnt;
849
850         return ret;
851 }
852
853 enum {
854         FORMAT_HEADER           = 1,
855         FORMAT_FIELD_SEPERATOR  = 2,
856         FORMAT_PRINTFMT         = 3,
857 };
858
859 static void *f_next(struct seq_file *m, void *v, loff_t *pos)
860 {
861         struct ftrace_event_call *call = event_file_data(m->private);
862         struct ftrace_event_field *field;
863         struct list_head *common_head = &ftrace_common_fields;
864         struct list_head *head = trace_get_fields(call);
865
866         (*pos)++;
867
868         switch ((unsigned long)v) {
869         case FORMAT_HEADER:
870                 if (unlikely(list_empty(common_head)))
871                         return NULL;
872
873                 field = list_entry(common_head->prev,
874                                    struct ftrace_event_field, link);
875                 return field;
876
877         case FORMAT_FIELD_SEPERATOR:
878                 if (unlikely(list_empty(head)))
879                         return NULL;
880
881                 field = list_entry(head->prev, struct ftrace_event_field, link);
882                 return field;
883
884         case FORMAT_PRINTFMT:
885                 /* all done */
886                 return NULL;
887         }
888
889         field = v;
890         if (field->link.prev == common_head)
891                 return (void *)FORMAT_FIELD_SEPERATOR;
892         else if (field->link.prev == head)
893                 return (void *)FORMAT_PRINTFMT;
894
895         field = list_entry(field->link.prev, struct ftrace_event_field, link);
896
897         return field;
898 }
899
900 static void *f_start(struct seq_file *m, loff_t *pos)
901 {
902         loff_t l = 0;
903         void *p;
904
905         /* ->stop() is called even if ->start() fails */
906         mutex_lock(&event_mutex);
907         if (!event_file_data(m->private))
908                 return ERR_PTR(-ENODEV);
909
910         /* Start by showing the header */
911         if (!*pos)
912                 return (void *)FORMAT_HEADER;
913
914         p = (void *)FORMAT_HEADER;
915         do {
916                 p = f_next(m, p, &l);
917         } while (p && l < *pos);
918
919         return p;
920 }
921
922 static int f_show(struct seq_file *m, void *v)
923 {
924         struct ftrace_event_call *call = event_file_data(m->private);
925         struct ftrace_event_field *field;
926         const char *array_descriptor;
927
928         switch ((unsigned long)v) {
929         case FORMAT_HEADER:
930                 seq_printf(m, "name: %s\n", call->name);
931                 seq_printf(m, "ID: %d\n", call->event.type);
932                 seq_printf(m, "format:\n");
933                 return 0;
934
935         case FORMAT_FIELD_SEPERATOR:
936                 seq_putc(m, '\n');
937                 return 0;
938
939         case FORMAT_PRINTFMT:
940                 seq_printf(m, "\nprint fmt: %s\n",
941                            call->print_fmt);
942                 return 0;
943         }
944
945         field = v;
946
947         /*
948          * Smartly shows the array type(except dynamic array).
949          * Normal:
950          *      field:TYPE VAR
951          * If TYPE := TYPE[LEN], it is shown:
952          *      field:TYPE VAR[LEN]
953          */
954         array_descriptor = strchr(field->type, '[');
955
956         if (!strncmp(field->type, "__data_loc", 10))
957                 array_descriptor = NULL;
958
959         if (!array_descriptor)
960                 seq_printf(m, "\tfield:%s %s;\toffset:%u;\tsize:%u;\tsigned:%d;\n",
961                            field->type, field->name, field->offset,
962                            field->size, !!field->is_signed);
963         else
964                 seq_printf(m, "\tfield:%.*s %s%s;\toffset:%u;\tsize:%u;\tsigned:%d;\n",
965                            (int)(array_descriptor - field->type),
966                            field->type, field->name,
967                            array_descriptor, field->offset,
968                            field->size, !!field->is_signed);
969
970         return 0;
971 }
972
973 static void f_stop(struct seq_file *m, void *p)
974 {
975         mutex_unlock(&event_mutex);
976 }
977
978 static const struct seq_operations trace_format_seq_ops = {
979         .start          = f_start,
980         .next           = f_next,
981         .stop           = f_stop,
982         .show           = f_show,
983 };
984
985 static int trace_format_open(struct inode *inode, struct file *file)
986 {
987         struct seq_file *m;
988         int ret;
989
990         ret = seq_open(file, &trace_format_seq_ops);
991         if (ret < 0)
992                 return ret;
993
994         m = file->private_data;
995         m->private = file;
996
997         return 0;
998 }
999
1000 static ssize_t
1001 event_id_read(struct file *filp, char __user *ubuf, size_t cnt, loff_t *ppos)
1002 {
1003         int id = (long)event_file_data(filp);
1004         struct trace_seq *s;
1005         int r;
1006
1007         if (*ppos)
1008                 return 0;
1009
1010         if (unlikely(!id))
1011                 return -ENODEV;
1012
1013         s = kmalloc(sizeof(*s), GFP_KERNEL);
1014         if (!s)
1015                 return -ENOMEM;
1016
1017         trace_seq_init(s);
1018         trace_seq_printf(s, "%d\n", id);
1019
1020         r = simple_read_from_buffer(ubuf, cnt, ppos,
1021                                     s->buffer, s->len);
1022         kfree(s);
1023         return r;
1024 }
1025
1026 static ssize_t
1027 event_filter_read(struct file *filp, char __user *ubuf, size_t cnt,
1028                   loff_t *ppos)
1029 {
1030         struct ftrace_event_call *call;
1031         struct trace_seq *s;
1032         int r = -ENODEV;
1033
1034         if (*ppos)
1035                 return 0;
1036
1037         s = kmalloc(sizeof(*s), GFP_KERNEL);
1038
1039         if (!s)
1040                 return -ENOMEM;
1041
1042         trace_seq_init(s);
1043
1044         mutex_lock(&event_mutex);
1045         call = event_file_data(filp);
1046         if (call)
1047                 print_event_filter(call, s);
1048         mutex_unlock(&event_mutex);
1049
1050         if (call)
1051                 r = simple_read_from_buffer(ubuf, cnt, ppos, s->buffer, s->len);
1052
1053         kfree(s);
1054
1055         return r;
1056 }
1057
1058 static ssize_t
1059 event_filter_write(struct file *filp, const char __user *ubuf, size_t cnt,
1060                    loff_t *ppos)
1061 {
1062         struct ftrace_event_call *call;
1063         char *buf;
1064         int err = -ENODEV;
1065
1066         if (cnt >= PAGE_SIZE)
1067                 return -EINVAL;
1068
1069         buf = (char *)__get_free_page(GFP_TEMPORARY);
1070         if (!buf)
1071                 return -ENOMEM;
1072
1073         if (copy_from_user(buf, ubuf, cnt)) {
1074                 free_page((unsigned long) buf);
1075                 return -EFAULT;
1076         }
1077         buf[cnt] = '\0';
1078
1079         mutex_lock(&event_mutex);
1080         call = event_file_data(filp);
1081         if (call)
1082                 err = apply_event_filter(call, buf);
1083         mutex_unlock(&event_mutex);
1084
1085         free_page((unsigned long) buf);
1086         if (err < 0)
1087                 return err;
1088
1089         *ppos += cnt;
1090
1091         return cnt;
1092 }
1093
1094 static LIST_HEAD(event_subsystems);
1095
1096 static int subsystem_open(struct inode *inode, struct file *filp)
1097 {
1098         struct event_subsystem *system = NULL;
1099         struct ftrace_subsystem_dir *dir = NULL; /* Initialize for gcc */
1100         struct trace_array *tr;
1101         int ret;
1102
1103         /* Make sure the system still exists */
1104         mutex_lock(&trace_types_lock);
1105         mutex_lock(&event_mutex);
1106         list_for_each_entry(tr, &ftrace_trace_arrays, list) {
1107                 list_for_each_entry(dir, &tr->systems, list) {
1108                         if (dir == inode->i_private) {
1109                                 /* Don't open systems with no events */
1110                                 if (dir->nr_events) {
1111                                         __get_system_dir(dir);
1112                                         system = dir->subsystem;
1113                                 }
1114                                 goto exit_loop;
1115                         }
1116                 }
1117         }
1118  exit_loop:
1119         mutex_unlock(&event_mutex);
1120         mutex_unlock(&trace_types_lock);
1121
1122         if (!system)
1123                 return -ENODEV;
1124
1125         /* Some versions of gcc think dir can be uninitialized here */
1126         WARN_ON(!dir);
1127
1128         /* Still need to increment the ref count of the system */
1129         if (trace_array_get(tr) < 0) {
1130                 put_system(dir);
1131                 return -ENODEV;
1132         }
1133
1134         ret = tracing_open_generic(inode, filp);
1135         if (ret < 0) {
1136                 trace_array_put(tr);
1137                 put_system(dir);
1138         }
1139
1140         return ret;
1141 }
1142
1143 static int system_tr_open(struct inode *inode, struct file *filp)
1144 {
1145         struct ftrace_subsystem_dir *dir;
1146         struct trace_array *tr = inode->i_private;
1147         int ret;
1148
1149         if (trace_array_get(tr) < 0)
1150                 return -ENODEV;
1151
1152         /* Make a temporary dir that has no system but points to tr */
1153         dir = kzalloc(sizeof(*dir), GFP_KERNEL);
1154         if (!dir) {
1155                 trace_array_put(tr);
1156                 return -ENOMEM;
1157         }
1158
1159         dir->tr = tr;
1160
1161         ret = tracing_open_generic(inode, filp);
1162         if (ret < 0) {
1163                 trace_array_put(tr);
1164                 kfree(dir);
1165         }
1166
1167         filp->private_data = dir;
1168
1169         return ret;
1170 }
1171
1172 static int subsystem_release(struct inode *inode, struct file *file)
1173 {
1174         struct ftrace_subsystem_dir *dir = file->private_data;
1175
1176         trace_array_put(dir->tr);
1177
1178         /*
1179          * If dir->subsystem is NULL, then this is a temporary
1180          * descriptor that was made for a trace_array to enable
1181          * all subsystems.
1182          */
1183         if (dir->subsystem)
1184                 put_system(dir);
1185         else
1186                 kfree(dir);
1187
1188         return 0;
1189 }
1190
1191 static ssize_t
1192 subsystem_filter_read(struct file *filp, char __user *ubuf, size_t cnt,
1193                       loff_t *ppos)
1194 {
1195         struct ftrace_subsystem_dir *dir = filp->private_data;
1196         struct event_subsystem *system = dir->subsystem;
1197         struct trace_seq *s;
1198         int r;
1199
1200         if (*ppos)
1201                 return 0;
1202
1203         s = kmalloc(sizeof(*s), GFP_KERNEL);
1204         if (!s)
1205                 return -ENOMEM;
1206
1207         trace_seq_init(s);
1208
1209         print_subsystem_event_filter(system, s);
1210         r = simple_read_from_buffer(ubuf, cnt, ppos, s->buffer, s->len);
1211
1212         kfree(s);
1213
1214         return r;
1215 }
1216
1217 static ssize_t
1218 subsystem_filter_write(struct file *filp, const char __user *ubuf, size_t cnt,
1219                        loff_t *ppos)
1220 {
1221         struct ftrace_subsystem_dir *dir = filp->private_data;
1222         char *buf;
1223         int err;
1224
1225         if (cnt >= PAGE_SIZE)
1226                 return -EINVAL;
1227
1228         buf = (char *)__get_free_page(GFP_TEMPORARY);
1229         if (!buf)
1230                 return -ENOMEM;
1231
1232         if (copy_from_user(buf, ubuf, cnt)) {
1233                 free_page((unsigned long) buf);
1234                 return -EFAULT;
1235         }
1236         buf[cnt] = '\0';
1237
1238         err = apply_subsystem_event_filter(dir, buf);
1239         free_page((unsigned long) buf);
1240         if (err < 0)
1241                 return err;
1242
1243         *ppos += cnt;
1244
1245         return cnt;
1246 }
1247
1248 static ssize_t
1249 show_header(struct file *filp, char __user *ubuf, size_t cnt, loff_t *ppos)
1250 {
1251         int (*func)(struct trace_seq *s) = filp->private_data;
1252         struct trace_seq *s;
1253         int r;
1254
1255         if (*ppos)
1256                 return 0;
1257
1258         s = kmalloc(sizeof(*s), GFP_KERNEL);
1259         if (!s)
1260                 return -ENOMEM;
1261
1262         trace_seq_init(s);
1263
1264         func(s);
1265         r = simple_read_from_buffer(ubuf, cnt, ppos, s->buffer, s->len);
1266
1267         kfree(s);
1268
1269         return r;
1270 }
1271
1272 static int ftrace_event_avail_open(struct inode *inode, struct file *file);
1273 static int ftrace_event_set_open(struct inode *inode, struct file *file);
1274 static int ftrace_event_release(struct inode *inode, struct file *file);
1275
1276 static const struct seq_operations show_event_seq_ops = {
1277         .start = t_start,
1278         .next = t_next,
1279         .show = t_show,
1280         .stop = t_stop,
1281 };
1282
1283 static const struct seq_operations show_set_event_seq_ops = {
1284         .start = s_start,
1285         .next = s_next,
1286         .show = t_show,
1287         .stop = t_stop,
1288 };
1289
1290 static const struct file_operations ftrace_avail_fops = {
1291         .open = ftrace_event_avail_open,
1292         .read = seq_read,
1293         .llseek = seq_lseek,
1294         .release = seq_release,
1295 };
1296
1297 static const struct file_operations ftrace_set_event_fops = {
1298         .open = ftrace_event_set_open,
1299         .read = seq_read,
1300         .write = ftrace_event_write,
1301         .llseek = seq_lseek,
1302         .release = ftrace_event_release,
1303 };
1304
1305 static const struct file_operations ftrace_enable_fops = {
1306         .open = tracing_open_generic_file,
1307         .read = event_enable_read,
1308         .write = event_enable_write,
1309         .release = tracing_release_generic_file,
1310         .llseek = default_llseek,
1311 };
1312
1313 static const struct file_operations ftrace_event_format_fops = {
1314         .open = trace_format_open,
1315         .read = seq_read,
1316         .llseek = seq_lseek,
1317         .release = seq_release,
1318 };
1319
1320 static const struct file_operations ftrace_event_id_fops = {
1321         .read = event_id_read,
1322         .llseek = default_llseek,
1323 };
1324
1325 static const struct file_operations ftrace_event_filter_fops = {
1326         .open = tracing_open_generic,
1327         .read = event_filter_read,
1328         .write = event_filter_write,
1329         .llseek = default_llseek,
1330 };
1331
1332 static const struct file_operations ftrace_subsystem_filter_fops = {
1333         .open = subsystem_open,
1334         .read = subsystem_filter_read,
1335         .write = subsystem_filter_write,
1336         .llseek = default_llseek,
1337         .release = subsystem_release,
1338 };
1339
1340 static const struct file_operations ftrace_system_enable_fops = {
1341         .open = subsystem_open,
1342         .read = system_enable_read,
1343         .write = system_enable_write,
1344         .llseek = default_llseek,
1345         .release = subsystem_release,
1346 };
1347
1348 static const struct file_operations ftrace_tr_enable_fops = {
1349         .open = system_tr_open,
1350         .read = system_enable_read,
1351         .write = system_enable_write,
1352         .llseek = default_llseek,
1353         .release = subsystem_release,
1354 };
1355
1356 static const struct file_operations ftrace_show_header_fops = {
1357         .open = tracing_open_generic,
1358         .read = show_header,
1359         .llseek = default_llseek,
1360 };
1361
1362 static int
1363 ftrace_event_open(struct inode *inode, struct file *file,
1364                   const struct seq_operations *seq_ops)
1365 {
1366         struct seq_file *m;
1367         int ret;
1368
1369         ret = seq_open(file, seq_ops);
1370         if (ret < 0)
1371                 return ret;
1372         m = file->private_data;
1373         /* copy tr over to seq ops */
1374         m->private = inode->i_private;
1375
1376         return ret;
1377 }
1378
1379 static int ftrace_event_release(struct inode *inode, struct file *file)
1380 {
1381         struct trace_array *tr = inode->i_private;
1382
1383         trace_array_put(tr);
1384
1385         return seq_release(inode, file);
1386 }
1387
1388 static int
1389 ftrace_event_avail_open(struct inode *inode, struct file *file)
1390 {
1391         const struct seq_operations *seq_ops = &show_event_seq_ops;
1392
1393         return ftrace_event_open(inode, file, seq_ops);
1394 }
1395
1396 static int
1397 ftrace_event_set_open(struct inode *inode, struct file *file)
1398 {
1399         const struct seq_operations *seq_ops = &show_set_event_seq_ops;
1400         struct trace_array *tr = inode->i_private;
1401         int ret;
1402
1403         if (trace_array_get(tr) < 0)
1404                 return -ENODEV;
1405
1406         if ((file->f_mode & FMODE_WRITE) &&
1407             (file->f_flags & O_TRUNC))
1408                 ftrace_clear_events(tr);
1409
1410         ret = ftrace_event_open(inode, file, seq_ops);
1411         if (ret < 0)
1412                 trace_array_put(tr);
1413         return ret;
1414 }
1415
1416 static struct event_subsystem *
1417 create_new_subsystem(const char *name)
1418 {
1419         struct event_subsystem *system;
1420
1421         /* need to create new entry */
1422         system = kmalloc(sizeof(*system), GFP_KERNEL);
1423         if (!system)
1424                 return NULL;
1425
1426         system->ref_count = 1;
1427
1428         /* Only allocate if dynamic (kprobes and modules) */
1429         if (!core_kernel_data((unsigned long)name)) {
1430                 system->ref_count |= SYSTEM_FL_FREE_NAME;
1431                 system->name = kstrdup(name, GFP_KERNEL);
1432                 if (!system->name)
1433                         goto out_free;
1434         } else
1435                 system->name = name;
1436
1437         system->filter = NULL;
1438
1439         system->filter = kzalloc(sizeof(struct event_filter), GFP_KERNEL);
1440         if (!system->filter)
1441                 goto out_free;
1442
1443         list_add(&system->list, &event_subsystems);
1444
1445         return system;
1446
1447  out_free:
1448         if (system->ref_count & SYSTEM_FL_FREE_NAME)
1449                 kfree(system->name);
1450         kfree(system);
1451         return NULL;
1452 }
1453
1454 static struct dentry *
1455 event_subsystem_dir(struct trace_array *tr, const char *name,
1456                     struct ftrace_event_file *file, struct dentry *parent)
1457 {
1458         struct ftrace_subsystem_dir *dir;
1459         struct event_subsystem *system;
1460         struct dentry *entry;
1461
1462         /* First see if we did not already create this dir */
1463         list_for_each_entry(dir, &tr->systems, list) {
1464                 system = dir->subsystem;
1465                 if (strcmp(system->name, name) == 0) {
1466                         dir->nr_events++;
1467                         file->system = dir;
1468                         return dir->entry;
1469                 }
1470         }
1471
1472         /* Now see if the system itself exists. */
1473         list_for_each_entry(system, &event_subsystems, list) {
1474                 if (strcmp(system->name, name) == 0)
1475                         break;
1476         }
1477         /* Reset system variable when not found */
1478         if (&system->list == &event_subsystems)
1479                 system = NULL;
1480
1481         dir = kmalloc(sizeof(*dir), GFP_KERNEL);
1482         if (!dir)
1483                 goto out_fail;
1484
1485         if (!system) {
1486                 system = create_new_subsystem(name);
1487                 if (!system)
1488                         goto out_free;
1489         } else
1490                 __get_system(system);
1491
1492         dir->entry = debugfs_create_dir(name, parent);
1493         if (!dir->entry) {
1494                 pr_warning("Failed to create system directory %s\n", name);
1495                 __put_system(system);
1496                 goto out_free;
1497         }
1498
1499         dir->tr = tr;
1500         dir->ref_count = 1;
1501         dir->nr_events = 1;
1502         dir->subsystem = system;
1503         file->system = dir;
1504
1505         entry = debugfs_create_file("filter", 0644, dir->entry, dir,
1506                                     &ftrace_subsystem_filter_fops);
1507         if (!entry) {
1508                 kfree(system->filter);
1509                 system->filter = NULL;
1510                 pr_warning("Could not create debugfs '%s/filter' entry\n", name);
1511         }
1512
1513         trace_create_file("enable", 0644, dir->entry, dir,
1514                           &ftrace_system_enable_fops);
1515
1516         list_add(&dir->list, &tr->systems);
1517
1518         return dir->entry;
1519
1520  out_free:
1521         kfree(dir);
1522  out_fail:
1523         /* Only print this message if failed on memory allocation */
1524         if (!dir || !system)
1525                 pr_warning("No memory to create event subsystem %s\n",
1526                            name);
1527         return NULL;
1528 }
1529
1530 static int
1531 event_create_dir(struct dentry *parent,
1532                  struct ftrace_event_file *file,
1533                  const struct file_operations *id,
1534                  const struct file_operations *enable,
1535                  const struct file_operations *filter,
1536                  const struct file_operations *format)
1537 {
1538         struct ftrace_event_call *call = file->event_call;
1539         struct trace_array *tr = file->tr;
1540         struct list_head *head;
1541         struct dentry *d_events;
1542         int ret;
1543
1544         /*
1545          * If the trace point header did not define TRACE_SYSTEM
1546          * then the system would be called "TRACE_SYSTEM".
1547          */
1548         if (strcmp(call->class->system, TRACE_SYSTEM) != 0) {
1549                 d_events = event_subsystem_dir(tr, call->class->system, file, parent);
1550                 if (!d_events)
1551                         return -ENOMEM;
1552         } else
1553                 d_events = parent;
1554
1555         file->dir = debugfs_create_dir(call->name, d_events);
1556         if (!file->dir) {
1557                 pr_warning("Could not create debugfs '%s' directory\n",
1558                            call->name);
1559                 return -1;
1560         }
1561
1562         if (call->class->reg && !(call->flags & TRACE_EVENT_FL_IGNORE_ENABLE))
1563                 trace_create_file("enable", 0644, file->dir, file,
1564                                   enable);
1565
1566 #ifdef CONFIG_PERF_EVENTS
1567         if (call->event.type && call->class->reg)
1568                 trace_create_file("id", 0444, file->dir,
1569                                   (void *)(long)call->event.type, id);
1570 #endif
1571
1572         /*
1573          * Other events may have the same class. Only update
1574          * the fields if they are not already defined.
1575          */
1576         head = trace_get_fields(call);
1577         if (list_empty(head)) {
1578                 ret = call->class->define_fields(call);
1579                 if (ret < 0) {
1580                         pr_warning("Could not initialize trace point"
1581                                    " events/%s\n", call->name);
1582                         return -1;
1583                 }
1584         }
1585         trace_create_file("filter", 0644, file->dir, call,
1586                           filter);
1587
1588         trace_create_file("format", 0444, file->dir, call,
1589                           format);
1590
1591         return 0;
1592 }
1593
1594 static void remove_event_from_tracers(struct ftrace_event_call *call)
1595 {
1596         struct ftrace_event_file *file;
1597         struct trace_array *tr;
1598
1599         do_for_each_event_file_safe(tr, file) {
1600                 if (file->event_call != call)
1601                         continue;
1602
1603                 remove_event_file_dir(file);
1604                 /*
1605                  * The do_for_each_event_file_safe() is
1606                  * a double loop. After finding the call for this
1607                  * trace_array, we use break to jump to the next
1608                  * trace_array.
1609                  */
1610                 break;
1611         } while_for_each_event_file();
1612 }
1613
1614 static void event_remove(struct ftrace_event_call *call)
1615 {
1616         struct trace_array *tr;
1617         struct ftrace_event_file *file;
1618
1619         do_for_each_event_file(tr, file) {
1620                 if (file->event_call != call)
1621                         continue;
1622                 ftrace_event_enable_disable(file, 0);
1623                 /*
1624                  * The do_for_each_event_file() is
1625                  * a double loop. After finding the call for this
1626                  * trace_array, we use break to jump to the next
1627                  * trace_array.
1628                  */
1629                 break;
1630         } while_for_each_event_file();
1631
1632         if (call->event.funcs)
1633                 __unregister_ftrace_event(&call->event);
1634         remove_event_from_tracers(call);
1635         list_del(&call->list);
1636 }
1637
1638 static int event_init(struct ftrace_event_call *call)
1639 {
1640         int ret = 0;
1641
1642         if (WARN_ON(!call->name))
1643                 return -EINVAL;
1644
1645         if (call->class->raw_init) {
1646                 ret = call->class->raw_init(call);
1647                 if (ret < 0 && ret != -ENOSYS)
1648                         pr_warn("Could not initialize trace events/%s\n",
1649                                 call->name);
1650         }
1651
1652         return ret;
1653 }
1654
1655 static int
1656 __register_event(struct ftrace_event_call *call, struct module *mod)
1657 {
1658         int ret;
1659
1660         ret = event_init(call);
1661         if (ret < 0)
1662                 return ret;
1663
1664         list_add(&call->list, &ftrace_events);
1665         call->mod = mod;
1666
1667         return 0;
1668 }
1669
1670 static struct ftrace_event_file *
1671 trace_create_new_event(struct ftrace_event_call *call,
1672                        struct trace_array *tr)
1673 {
1674         struct ftrace_event_file *file;
1675
1676         file = kmem_cache_alloc(file_cachep, GFP_TRACE);
1677         if (!file)
1678                 return NULL;
1679
1680         file->event_call = call;
1681         file->tr = tr;
1682         atomic_set(&file->sm_ref, 0);
1683         list_add(&file->list, &tr->events);
1684
1685         return file;
1686 }
1687
1688 /* Add an event to a trace directory */
1689 static int
1690 __trace_add_new_event(struct ftrace_event_call *call,
1691                       struct trace_array *tr,
1692                       const struct file_operations *id,
1693                       const struct file_operations *enable,
1694                       const struct file_operations *filter,
1695                       const struct file_operations *format)
1696 {
1697         struct ftrace_event_file *file;
1698
1699         file = trace_create_new_event(call, tr);
1700         if (!file)
1701                 return -ENOMEM;
1702
1703         return event_create_dir(tr->event_dir, file, id, enable, filter, format);
1704 }
1705
1706 /*
1707  * Just create a decriptor for early init. A descriptor is required
1708  * for enabling events at boot. We want to enable events before
1709  * the filesystem is initialized.
1710  */
1711 static __init int
1712 __trace_early_add_new_event(struct ftrace_event_call *call,
1713                             struct trace_array *tr)
1714 {
1715         struct ftrace_event_file *file;
1716
1717         file = trace_create_new_event(call, tr);
1718         if (!file)
1719                 return -ENOMEM;
1720
1721         return 0;
1722 }
1723
1724 struct ftrace_module_file_ops;
1725 static void __add_event_to_tracers(struct ftrace_event_call *call,
1726                                    struct ftrace_module_file_ops *file_ops);
1727
1728 /* Add an additional event_call dynamically */
1729 int trace_add_event_call(struct ftrace_event_call *call)
1730 {
1731         int ret;
1732         mutex_lock(&trace_types_lock);
1733         mutex_lock(&event_mutex);
1734
1735         ret = __register_event(call, NULL);
1736         if (ret >= 0)
1737                 __add_event_to_tracers(call, NULL);
1738
1739         mutex_unlock(&event_mutex);
1740         mutex_unlock(&trace_types_lock);
1741         return ret;
1742 }
1743
1744 /*
1745  * Must be called under locking of trace_types_lock, event_mutex and
1746  * trace_event_sem.
1747  */
1748 static void __trace_remove_event_call(struct ftrace_event_call *call)
1749 {
1750         event_remove(call);
1751         trace_destroy_fields(call);
1752         destroy_preds(call);
1753 }
1754
1755 /* Remove an event_call */
1756 void trace_remove_event_call(struct ftrace_event_call *call)
1757 {
1758         mutex_lock(&trace_types_lock);
1759         mutex_lock(&event_mutex);
1760         down_write(&trace_event_sem);
1761         __trace_remove_event_call(call);
1762         up_write(&trace_event_sem);
1763         mutex_unlock(&event_mutex);
1764         mutex_unlock(&trace_types_lock);
1765 }
1766
1767 #define for_each_event(event, start, end)                       \
1768         for (event = start;                                     \
1769              (unsigned long)event < (unsigned long)end;         \
1770              event++)
1771
1772 #ifdef CONFIG_MODULES
1773
1774 static LIST_HEAD(ftrace_module_file_list);
1775
1776 /*
1777  * Modules must own their file_operations to keep up with
1778  * reference counting.
1779  */
1780 struct ftrace_module_file_ops {
1781         struct list_head                list;
1782         struct module                   *mod;
1783         struct file_operations          id;
1784         struct file_operations          enable;
1785         struct file_operations          format;
1786         struct file_operations          filter;
1787 };
1788
1789 static struct ftrace_module_file_ops *
1790 find_ftrace_file_ops(struct ftrace_module_file_ops *file_ops, struct module *mod)
1791 {
1792         /*
1793          * As event_calls are added in groups by module,
1794          * when we find one file_ops, we don't need to search for
1795          * each call in that module, as the rest should be the
1796          * same. Only search for a new one if the last one did
1797          * not match.
1798          */
1799         if (file_ops && mod == file_ops->mod)
1800                 return file_ops;
1801
1802         list_for_each_entry(file_ops, &ftrace_module_file_list, list) {
1803                 if (file_ops->mod == mod)
1804                         return file_ops;
1805         }
1806         return NULL;
1807 }
1808
1809 static struct ftrace_module_file_ops *
1810 trace_create_file_ops(struct module *mod)
1811 {
1812         struct ftrace_module_file_ops *file_ops;
1813
1814         /*
1815          * This is a bit of a PITA. To allow for correct reference
1816          * counting, modules must "own" their file_operations.
1817          * To do this, we allocate the file operations that will be
1818          * used in the event directory.
1819          */
1820
1821         file_ops = kmalloc(sizeof(*file_ops), GFP_KERNEL);
1822         if (!file_ops)
1823                 return NULL;
1824
1825         file_ops->mod = mod;
1826
1827         file_ops->id = ftrace_event_id_fops;
1828         file_ops->id.owner = mod;
1829
1830         file_ops->enable = ftrace_enable_fops;
1831         file_ops->enable.owner = mod;
1832
1833         file_ops->filter = ftrace_event_filter_fops;
1834         file_ops->filter.owner = mod;
1835
1836         file_ops->format = ftrace_event_format_fops;
1837         file_ops->format.owner = mod;
1838
1839         list_add(&file_ops->list, &ftrace_module_file_list);
1840
1841         return file_ops;
1842 }
1843
1844 static void trace_module_add_events(struct module *mod)
1845 {
1846         struct ftrace_module_file_ops *file_ops = NULL;
1847         struct ftrace_event_call **call, **start, **end;
1848
1849         start = mod->trace_events;
1850         end = mod->trace_events + mod->num_trace_events;
1851
1852         if (start == end)
1853                 return;
1854
1855         file_ops = trace_create_file_ops(mod);
1856         if (!file_ops)
1857                 return;
1858
1859         for_each_event(call, start, end) {
1860                 __register_event(*call, mod);
1861                 __add_event_to_tracers(*call, file_ops);
1862         }
1863 }
1864
1865 static void trace_module_remove_events(struct module *mod)
1866 {
1867         struct ftrace_module_file_ops *file_ops;
1868         struct ftrace_event_call *call, *p;
1869         bool clear_trace = false;
1870
1871         down_write(&trace_event_sem);
1872         list_for_each_entry_safe(call, p, &ftrace_events, list) {
1873                 if (call->mod == mod) {
1874                         if (call->flags & TRACE_EVENT_FL_WAS_ENABLED)
1875                                 clear_trace = true;
1876                         __trace_remove_event_call(call);
1877                 }
1878         }
1879
1880         /* Now free the file_operations */
1881         list_for_each_entry(file_ops, &ftrace_module_file_list, list) {
1882                 if (file_ops->mod == mod)
1883                         break;
1884         }
1885         if (&file_ops->list != &ftrace_module_file_list) {
1886                 list_del(&file_ops->list);
1887                 kfree(file_ops);
1888         }
1889         up_write(&trace_event_sem);
1890
1891         /*
1892          * It is safest to reset the ring buffer if the module being unloaded
1893          * registered any events that were used. The only worry is if
1894          * a new module gets loaded, and takes on the same id as the events
1895          * of this module. When printing out the buffer, traced events left
1896          * over from this module may be passed to the new module events and
1897          * unexpected results may occur.
1898          */
1899         if (clear_trace)
1900                 tracing_reset_all_online_cpus();
1901 }
1902
1903 static int trace_module_notify(struct notifier_block *self,
1904                                unsigned long val, void *data)
1905 {
1906         struct module *mod = data;
1907
1908         mutex_lock(&trace_types_lock);
1909         mutex_lock(&event_mutex);
1910         switch (val) {
1911         case MODULE_STATE_COMING:
1912                 trace_module_add_events(mod);
1913                 break;
1914         case MODULE_STATE_GOING:
1915                 trace_module_remove_events(mod);
1916                 break;
1917         }
1918         mutex_unlock(&event_mutex);
1919         mutex_unlock(&trace_types_lock);
1920
1921         return 0;
1922 }
1923
1924 static int
1925 __trace_add_new_mod_event(struct ftrace_event_call *call,
1926                           struct trace_array *tr,
1927                           struct ftrace_module_file_ops *file_ops)
1928 {
1929         return __trace_add_new_event(call, tr,
1930                                      &file_ops->id, &file_ops->enable,
1931                                      &file_ops->filter, &file_ops->format);
1932 }
1933
1934 #else
1935 static inline struct ftrace_module_file_ops *
1936 find_ftrace_file_ops(struct ftrace_module_file_ops *file_ops, struct module *mod)
1937 {
1938         return NULL;
1939 }
1940 static inline int trace_module_notify(struct notifier_block *self,
1941                                       unsigned long val, void *data)
1942 {
1943         return 0;
1944 }
1945 static inline int
1946 __trace_add_new_mod_event(struct ftrace_event_call *call,
1947                           struct trace_array *tr,
1948                           struct ftrace_module_file_ops *file_ops)
1949 {
1950         return -ENODEV;
1951 }
1952 #endif /* CONFIG_MODULES */
1953
1954 /* Create a new event directory structure for a trace directory. */
1955 static void
1956 __trace_add_event_dirs(struct trace_array *tr)
1957 {
1958         struct ftrace_module_file_ops *file_ops = NULL;
1959         struct ftrace_event_call *call;
1960         int ret;
1961
1962         list_for_each_entry(call, &ftrace_events, list) {
1963                 if (call->mod) {
1964                         /*
1965                          * Directories for events by modules need to
1966                          * keep module ref counts when opened (as we don't
1967                          * want the module to disappear when reading one
1968                          * of these files). The file_ops keep account of
1969                          * the module ref count.
1970                          */
1971                         file_ops = find_ftrace_file_ops(file_ops, call->mod);
1972                         if (!file_ops)
1973                                 continue; /* Warn? */
1974                         ret = __trace_add_new_mod_event(call, tr, file_ops);
1975                         if (ret < 0)
1976                                 pr_warning("Could not create directory for event %s\n",
1977                                            call->name);
1978                         continue;
1979                 }
1980                 ret = __trace_add_new_event(call, tr,
1981                                             &ftrace_event_id_fops,
1982                                             &ftrace_enable_fops,
1983                                             &ftrace_event_filter_fops,
1984                                             &ftrace_event_format_fops);
1985                 if (ret < 0)
1986                         pr_warning("Could not create directory for event %s\n",
1987                                    call->name);
1988         }
1989 }
1990
1991 #ifdef CONFIG_DYNAMIC_FTRACE
1992
1993 /* Avoid typos */
1994 #define ENABLE_EVENT_STR        "enable_event"
1995 #define DISABLE_EVENT_STR       "disable_event"
1996
1997 struct event_probe_data {
1998         struct ftrace_event_file        *file;
1999         unsigned long                   count;
2000         int                             ref;
2001         bool                            enable;
2002 };
2003
2004 static struct ftrace_event_file *
2005 find_event_file(struct trace_array *tr, const char *system,  const char *event)
2006 {
2007         struct ftrace_event_file *file;
2008         struct ftrace_event_call *call;
2009
2010         list_for_each_entry(file, &tr->events, list) {
2011
2012                 call = file->event_call;
2013
2014                 if (!call->name || !call->class || !call->class->reg)
2015                         continue;
2016
2017                 if (call->flags & TRACE_EVENT_FL_IGNORE_ENABLE)
2018                         continue;
2019
2020                 if (strcmp(event, call->name) == 0 &&
2021                     strcmp(system, call->class->system) == 0)
2022                         return file;
2023         }
2024         return NULL;
2025 }
2026
2027 static void
2028 event_enable_probe(unsigned long ip, unsigned long parent_ip, void **_data)
2029 {
2030         struct event_probe_data **pdata = (struct event_probe_data **)_data;
2031         struct event_probe_data *data = *pdata;
2032
2033         if (!data)
2034                 return;
2035
2036         if (data->enable)
2037                 clear_bit(FTRACE_EVENT_FL_SOFT_DISABLED_BIT, &data->file->flags);
2038         else
2039                 set_bit(FTRACE_EVENT_FL_SOFT_DISABLED_BIT, &data->file->flags);
2040 }
2041
2042 static void
2043 event_enable_count_probe(unsigned long ip, unsigned long parent_ip, void **_data)
2044 {
2045         struct event_probe_data **pdata = (struct event_probe_data **)_data;
2046         struct event_probe_data *data = *pdata;
2047
2048         if (!data)
2049                 return;
2050
2051         if (!data->count)
2052                 return;
2053
2054         /* Skip if the event is in a state we want to switch to */
2055         if (data->enable == !(data->file->flags & FTRACE_EVENT_FL_SOFT_DISABLED))
2056                 return;
2057
2058         if (data->count != -1)
2059                 (data->count)--;
2060
2061         event_enable_probe(ip, parent_ip, _data);
2062 }
2063
2064 static int
2065 event_enable_print(struct seq_file *m, unsigned long ip,
2066                       struct ftrace_probe_ops *ops, void *_data)
2067 {
2068         struct event_probe_data *data = _data;
2069
2070         seq_printf(m, "%ps:", (void *)ip);
2071
2072         seq_printf(m, "%s:%s:%s",
2073                    data->enable ? ENABLE_EVENT_STR : DISABLE_EVENT_STR,
2074                    data->file->event_call->class->system,
2075                    data->file->event_call->name);
2076
2077         if (data->count == -1)
2078                 seq_printf(m, ":unlimited\n");
2079         else
2080                 seq_printf(m, ":count=%ld\n", data->count);
2081
2082         return 0;
2083 }
2084
2085 static int
2086 event_enable_init(struct ftrace_probe_ops *ops, unsigned long ip,
2087                   void **_data)
2088 {
2089         struct event_probe_data **pdata = (struct event_probe_data **)_data;
2090         struct event_probe_data *data = *pdata;
2091
2092         data->ref++;
2093         return 0;
2094 }
2095
2096 static void
2097 event_enable_free(struct ftrace_probe_ops *ops, unsigned long ip,
2098                   void **_data)
2099 {
2100         struct event_probe_data **pdata = (struct event_probe_data **)_data;
2101         struct event_probe_data *data = *pdata;
2102
2103         if (WARN_ON_ONCE(data->ref <= 0))
2104                 return;
2105
2106         data->ref--;
2107         if (!data->ref) {
2108                 /* Remove the SOFT_MODE flag */
2109                 __ftrace_event_enable_disable(data->file, 0, 1);
2110                 module_put(data->file->event_call->mod);
2111                 kfree(data);
2112         }
2113         *pdata = NULL;
2114 }
2115
2116 static struct ftrace_probe_ops event_enable_probe_ops = {
2117         .func                   = event_enable_probe,
2118         .print                  = event_enable_print,
2119         .init                   = event_enable_init,
2120         .free                   = event_enable_free,
2121 };
2122
2123 static struct ftrace_probe_ops event_enable_count_probe_ops = {
2124         .func                   = event_enable_count_probe,
2125         .print                  = event_enable_print,
2126         .init                   = event_enable_init,
2127         .free                   = event_enable_free,
2128 };
2129
2130 static struct ftrace_probe_ops event_disable_probe_ops = {
2131         .func                   = event_enable_probe,
2132         .print                  = event_enable_print,
2133         .init                   = event_enable_init,
2134         .free                   = event_enable_free,
2135 };
2136
2137 static struct ftrace_probe_ops event_disable_count_probe_ops = {
2138         .func                   = event_enable_count_probe,
2139         .print                  = event_enable_print,
2140         .init                   = event_enable_init,
2141         .free                   = event_enable_free,
2142 };
2143
2144 static int
2145 event_enable_func(struct ftrace_hash *hash,
2146                   char *glob, char *cmd, char *param, int enabled)
2147 {
2148         struct trace_array *tr = top_trace_array();
2149         struct ftrace_event_file *file;
2150         struct ftrace_probe_ops *ops;
2151         struct event_probe_data *data;
2152         const char *system;
2153         const char *event;
2154         char *number;
2155         bool enable;
2156         int ret;
2157
2158         /* hash funcs only work with set_ftrace_filter */
2159         if (!enabled)
2160                 return -EINVAL;
2161
2162         if (!param)
2163                 return -EINVAL;
2164
2165         system = strsep(&param, ":");
2166         if (!param)
2167                 return -EINVAL;
2168
2169         event = strsep(&param, ":");
2170
2171         mutex_lock(&event_mutex);
2172
2173         ret = -EINVAL;
2174         file = find_event_file(tr, system, event);
2175         if (!file)
2176                 goto out;
2177
2178         enable = strcmp(cmd, ENABLE_EVENT_STR) == 0;
2179
2180         if (enable)
2181                 ops = param ? &event_enable_count_probe_ops : &event_enable_probe_ops;
2182         else
2183                 ops = param ? &event_disable_count_probe_ops : &event_disable_probe_ops;
2184
2185         if (glob[0] == '!') {
2186                 unregister_ftrace_function_probe_func(glob+1, ops);
2187                 ret = 0;
2188                 goto out;
2189         }
2190
2191         ret = -ENOMEM;
2192         data = kzalloc(sizeof(*data), GFP_KERNEL);
2193         if (!data)
2194                 goto out;
2195
2196         data->enable = enable;
2197         data->count = -1;
2198         data->file = file;
2199
2200         if (!param)
2201                 goto out_reg;
2202
2203         number = strsep(&param, ":");
2204
2205         ret = -EINVAL;
2206         if (!strlen(number))
2207                 goto out_free;
2208
2209         /*
2210          * We use the callback data field (which is a pointer)
2211          * as our counter.
2212          */
2213         ret = kstrtoul(number, 0, &data->count);
2214         if (ret)
2215                 goto out_free;
2216
2217  out_reg:
2218         /* Don't let event modules unload while probe registered */
2219         ret = try_module_get(file->event_call->mod);
2220         if (!ret) {
2221                 ret = -EBUSY;
2222                 goto out_free;
2223         }
2224
2225         ret = __ftrace_event_enable_disable(file, 1, 1);
2226         if (ret < 0)
2227                 goto out_put;
2228         ret = register_ftrace_function_probe(glob, ops, data);
2229         /*
2230          * The above returns on success the # of functions enabled,
2231          * but if it didn't find any functions it returns zero.
2232          * Consider no functions a failure too.
2233          */
2234         if (!ret) {
2235                 ret = -ENOENT;
2236                 goto out_disable;
2237         } else if (ret < 0)
2238                 goto out_disable;
2239         /* Just return zero, not the number of enabled functions */
2240         ret = 0;
2241  out:
2242         mutex_unlock(&event_mutex);
2243         return ret;
2244
2245  out_disable:
2246         __ftrace_event_enable_disable(file, 0, 1);
2247  out_put:
2248         module_put(file->event_call->mod);
2249  out_free:
2250         kfree(data);
2251         goto out;
2252 }
2253
2254 static struct ftrace_func_command event_enable_cmd = {
2255         .name                   = ENABLE_EVENT_STR,
2256         .func                   = event_enable_func,
2257 };
2258
2259 static struct ftrace_func_command event_disable_cmd = {
2260         .name                   = DISABLE_EVENT_STR,
2261         .func                   = event_enable_func,
2262 };
2263
2264 static __init int register_event_cmds(void)
2265 {
2266         int ret;
2267
2268         ret = register_ftrace_command(&event_enable_cmd);
2269         if (WARN_ON(ret < 0))
2270                 return ret;
2271         ret = register_ftrace_command(&event_disable_cmd);
2272         if (WARN_ON(ret < 0))
2273                 unregister_ftrace_command(&event_enable_cmd);
2274         return ret;
2275 }
2276 #else
2277 static inline int register_event_cmds(void) { return 0; }
2278 #endif /* CONFIG_DYNAMIC_FTRACE */
2279
2280 /*
2281  * The top level array has already had its ftrace_event_file
2282  * descriptors created in order to allow for early events to
2283  * be recorded. This function is called after the debugfs has been
2284  * initialized, and we now have to create the files associated
2285  * to the events.
2286  */
2287 static __init void
2288 __trace_early_add_event_dirs(struct trace_array *tr)
2289 {
2290         struct ftrace_event_file *file;
2291         int ret;
2292
2293
2294         list_for_each_entry(file, &tr->events, list) {
2295                 ret = event_create_dir(tr->event_dir, file,
2296                                        &ftrace_event_id_fops,
2297                                        &ftrace_enable_fops,
2298                                        &ftrace_event_filter_fops,
2299                                        &ftrace_event_format_fops);
2300                 if (ret < 0)
2301                         pr_warning("Could not create directory for event %s\n",
2302                                    file->event_call->name);
2303         }
2304 }
2305
2306 /*
2307  * For early boot up, the top trace array requires to have
2308  * a list of events that can be enabled. This must be done before
2309  * the filesystem is set up in order to allow events to be traced
2310  * early.
2311  */
2312 static __init void
2313 __trace_early_add_events(struct trace_array *tr)
2314 {
2315         struct ftrace_event_call *call;
2316         int ret;
2317
2318         list_for_each_entry(call, &ftrace_events, list) {
2319                 /* Early boot up should not have any modules loaded */
2320                 if (WARN_ON_ONCE(call->mod))
2321                         continue;
2322
2323                 ret = __trace_early_add_new_event(call, tr);
2324                 if (ret < 0)
2325                         pr_warning("Could not create early event %s\n",
2326                                    call->name);
2327         }
2328 }
2329
2330 /* Remove the event directory structure for a trace directory. */
2331 static void
2332 __trace_remove_event_dirs(struct trace_array *tr)
2333 {
2334         struct ftrace_event_file *file, *next;
2335
2336         list_for_each_entry_safe(file, next, &tr->events, list)
2337                 remove_event_file_dir(file);
2338 }
2339
2340 static void
2341 __add_event_to_tracers(struct ftrace_event_call *call,
2342                        struct ftrace_module_file_ops *file_ops)
2343 {
2344         struct trace_array *tr;
2345
2346         list_for_each_entry(tr, &ftrace_trace_arrays, list) {
2347                 if (file_ops)
2348                         __trace_add_new_mod_event(call, tr, file_ops);
2349                 else
2350                         __trace_add_new_event(call, tr,
2351                                               &ftrace_event_id_fops,
2352                                               &ftrace_enable_fops,
2353                                               &ftrace_event_filter_fops,
2354                                               &ftrace_event_format_fops);
2355         }
2356 }
2357
2358 static struct notifier_block trace_module_nb = {
2359         .notifier_call = trace_module_notify,
2360         .priority = 0,
2361 };
2362
2363 extern struct ftrace_event_call *__start_ftrace_events[];
2364 extern struct ftrace_event_call *__stop_ftrace_events[];
2365
2366 static char bootup_event_buf[COMMAND_LINE_SIZE] __initdata;
2367
2368 static __init int setup_trace_event(char *str)
2369 {
2370         strlcpy(bootup_event_buf, str, COMMAND_LINE_SIZE);
2371         ring_buffer_expanded = true;
2372         tracing_selftest_disabled = true;
2373
2374         return 1;
2375 }
2376 __setup("trace_event=", setup_trace_event);
2377
2378 /* Expects to have event_mutex held when called */
2379 static int
2380 create_event_toplevel_files(struct dentry *parent, struct trace_array *tr)
2381 {
2382         struct dentry *d_events;
2383         struct dentry *entry;
2384
2385         entry = debugfs_create_file("set_event", 0644, parent,
2386                                     tr, &ftrace_set_event_fops);
2387         if (!entry) {
2388                 pr_warning("Could not create debugfs 'set_event' entry\n");
2389                 return -ENOMEM;
2390         }
2391
2392         d_events = debugfs_create_dir("events", parent);
2393         if (!d_events) {
2394                 pr_warning("Could not create debugfs 'events' directory\n");
2395                 return -ENOMEM;
2396         }
2397
2398         /* ring buffer internal formats */
2399         trace_create_file("header_page", 0444, d_events,
2400                           ring_buffer_print_page_header,
2401                           &ftrace_show_header_fops);
2402
2403         trace_create_file("header_event", 0444, d_events,
2404                           ring_buffer_print_entry_header,
2405                           &ftrace_show_header_fops);
2406
2407         trace_create_file("enable", 0644, d_events,
2408                           tr, &ftrace_tr_enable_fops);
2409
2410         tr->event_dir = d_events;
2411
2412         return 0;
2413 }
2414
2415 /**
2416  * event_trace_add_tracer - add a instance of a trace_array to events
2417  * @parent: The parent dentry to place the files/directories for events in
2418  * @tr: The trace array associated with these events
2419  *
2420  * When a new instance is created, it needs to set up its events
2421  * directory, as well as other files associated with events. It also
2422  * creates the event hierachry in the @parent/events directory.
2423  *
2424  * Returns 0 on success.
2425  */
2426 int event_trace_add_tracer(struct dentry *parent, struct trace_array *tr)
2427 {
2428         int ret;
2429
2430         mutex_lock(&event_mutex);
2431
2432         ret = create_event_toplevel_files(parent, tr);
2433         if (ret)
2434                 goto out_unlock;
2435
2436         down_write(&trace_event_sem);
2437         __trace_add_event_dirs(tr);
2438         up_write(&trace_event_sem);
2439
2440  out_unlock:
2441         mutex_unlock(&event_mutex);
2442
2443         return ret;
2444 }
2445
2446 /*
2447  * The top trace array already had its file descriptors created.
2448  * Now the files themselves need to be created.
2449  */
2450 static __init int
2451 early_event_add_tracer(struct dentry *parent, struct trace_array *tr)
2452 {
2453         int ret;
2454
2455         mutex_lock(&event_mutex);
2456
2457         ret = create_event_toplevel_files(parent, tr);
2458         if (ret)
2459                 goto out_unlock;
2460
2461         down_write(&trace_event_sem);
2462         __trace_early_add_event_dirs(tr);
2463         up_write(&trace_event_sem);
2464
2465  out_unlock:
2466         mutex_unlock(&event_mutex);
2467
2468         return ret;
2469 }
2470
2471 int event_trace_del_tracer(struct trace_array *tr)
2472 {
2473         mutex_lock(&event_mutex);
2474
2475         /* Disable any running events */
2476         __ftrace_set_clr_event_nolock(tr, NULL, NULL, NULL, 0);
2477
2478         down_write(&trace_event_sem);
2479         __trace_remove_event_dirs(tr);
2480         debugfs_remove_recursive(tr->event_dir);
2481         up_write(&trace_event_sem);
2482
2483         tr->event_dir = NULL;
2484
2485         mutex_unlock(&event_mutex);
2486
2487         return 0;
2488 }
2489
2490 static __init int event_trace_memsetup(void)
2491 {
2492         field_cachep = KMEM_CACHE(ftrace_event_field, SLAB_PANIC);
2493         file_cachep = KMEM_CACHE(ftrace_event_file, SLAB_PANIC);
2494         return 0;
2495 }
2496
2497 static __init int event_trace_enable(void)
2498 {
2499         struct trace_array *tr = top_trace_array();
2500         struct ftrace_event_call **iter, *call;
2501         char *buf = bootup_event_buf;
2502         char *token;
2503         int ret;
2504
2505         for_each_event(iter, __start_ftrace_events, __stop_ftrace_events) {
2506
2507                 call = *iter;
2508                 ret = event_init(call);
2509                 if (!ret)
2510                         list_add(&call->list, &ftrace_events);
2511         }
2512
2513         /*
2514          * We need the top trace array to have a working set of trace
2515          * points at early init, before the debug files and directories
2516          * are created. Create the file entries now, and attach them
2517          * to the actual file dentries later.
2518          */
2519         __trace_early_add_events(tr);
2520
2521         while (true) {
2522                 token = strsep(&buf, ",");
2523
2524                 if (!token)
2525                         break;
2526                 if (!*token)
2527                         continue;
2528
2529                 ret = ftrace_set_clr_event(tr, token, 1);
2530                 if (ret)
2531                         pr_warn("Failed to enable trace event: %s\n", token);
2532         }
2533
2534         trace_printk_start_comm();
2535
2536         register_event_cmds();
2537
2538         return 0;
2539 }
2540
2541 static __init int event_trace_init(void)
2542 {
2543         struct trace_array *tr;
2544         struct dentry *d_tracer;
2545         struct dentry *entry;
2546         int ret;
2547
2548         tr = top_trace_array();
2549
2550         d_tracer = tracing_init_dentry();
2551         if (!d_tracer)
2552                 return 0;
2553
2554         entry = debugfs_create_file("available_events", 0444, d_tracer,
2555                                     tr, &ftrace_avail_fops);
2556         if (!entry)
2557                 pr_warning("Could not create debugfs "
2558                            "'available_events' entry\n");
2559
2560         if (trace_define_common_fields())
2561                 pr_warning("tracing: Failed to allocate common fields");
2562
2563         ret = early_event_add_tracer(d_tracer, tr);
2564         if (ret)
2565                 return ret;
2566
2567         ret = register_module_notifier(&trace_module_nb);
2568         if (ret)
2569                 pr_warning("Failed to register trace events module notifier\n");
2570
2571         return 0;
2572 }
2573 early_initcall(event_trace_memsetup);
2574 core_initcall(event_trace_enable);
2575 fs_initcall(event_trace_init);
2576
2577 #ifdef CONFIG_FTRACE_STARTUP_TEST
2578
2579 static DEFINE_SPINLOCK(test_spinlock);
2580 static DEFINE_SPINLOCK(test_spinlock_irq);
2581 static DEFINE_MUTEX(test_mutex);
2582
2583 static __init void test_work(struct work_struct *dummy)
2584 {
2585         spin_lock(&test_spinlock);
2586         spin_lock_irq(&test_spinlock_irq);
2587         udelay(1);
2588         spin_unlock_irq(&test_spinlock_irq);
2589         spin_unlock(&test_spinlock);
2590
2591         mutex_lock(&test_mutex);
2592         msleep(1);
2593         mutex_unlock(&test_mutex);
2594 }
2595
2596 static __init int event_test_thread(void *unused)
2597 {
2598         void *test_malloc;
2599
2600         test_malloc = kmalloc(1234, GFP_KERNEL);
2601         if (!test_malloc)
2602                 pr_info("failed to kmalloc\n");
2603
2604         schedule_on_each_cpu(test_work);
2605
2606         kfree(test_malloc);
2607
2608         set_current_state(TASK_INTERRUPTIBLE);
2609         while (!kthread_should_stop())
2610                 schedule();
2611
2612         return 0;
2613 }
2614
2615 /*
2616  * Do various things that may trigger events.
2617  */
2618 static __init void event_test_stuff(void)
2619 {
2620         struct task_struct *test_thread;
2621
2622         test_thread = kthread_run(event_test_thread, NULL, "test-events");
2623         msleep(1);
2624         kthread_stop(test_thread);
2625 }
2626
2627 /*
2628  * For every trace event defined, we will test each trace point separately,
2629  * and then by groups, and finally all trace points.
2630  */
2631 static __init void event_trace_self_tests(void)
2632 {
2633         struct ftrace_subsystem_dir *dir;
2634         struct ftrace_event_file *file;
2635         struct ftrace_event_call *call;
2636         struct event_subsystem *system;
2637         struct trace_array *tr;
2638         int ret;
2639
2640         tr = top_trace_array();
2641
2642         pr_info("Running tests on trace events:\n");
2643
2644         list_for_each_entry(file, &tr->events, list) {
2645
2646                 call = file->event_call;
2647
2648                 /* Only test those that have a probe */
2649                 if (!call->class || !call->class->probe)
2650                         continue;
2651
2652 /*
2653  * Testing syscall events here is pretty useless, but
2654  * we still do it if configured. But this is time consuming.
2655  * What we really need is a user thread to perform the
2656  * syscalls as we test.
2657  */
2658 #ifndef CONFIG_EVENT_TRACE_TEST_SYSCALLS
2659                 if (call->class->system &&
2660                     strcmp(call->class->system, "syscalls") == 0)
2661                         continue;
2662 #endif
2663
2664                 pr_info("Testing event %s: ", call->name);
2665
2666                 /*
2667                  * If an event is already enabled, someone is using
2668                  * it and the self test should not be on.
2669                  */
2670                 if (file->flags & FTRACE_EVENT_FL_ENABLED) {
2671                         pr_warning("Enabled event during self test!\n");
2672                         WARN_ON_ONCE(1);
2673                         continue;
2674                 }
2675
2676                 ftrace_event_enable_disable(file, 1);
2677                 event_test_stuff();
2678                 ftrace_event_enable_disable(file, 0);
2679
2680                 pr_cont("OK\n");
2681         }
2682
2683         /* Now test at the sub system level */
2684
2685         pr_info("Running tests on trace event systems:\n");
2686
2687         list_for_each_entry(dir, &tr->systems, list) {
2688
2689                 system = dir->subsystem;
2690
2691                 /* the ftrace system is special, skip it */
2692                 if (strcmp(system->name, "ftrace") == 0)
2693                         continue;
2694
2695                 pr_info("Testing event system %s: ", system->name);
2696
2697                 ret = __ftrace_set_clr_event(tr, NULL, system->name, NULL, 1);
2698                 if (WARN_ON_ONCE(ret)) {
2699                         pr_warning("error enabling system %s\n",
2700                                    system->name);
2701                         continue;
2702                 }
2703
2704                 event_test_stuff();
2705
2706                 ret = __ftrace_set_clr_event(tr, NULL, system->name, NULL, 0);
2707                 if (WARN_ON_ONCE(ret)) {
2708                         pr_warning("error disabling system %s\n",
2709                                    system->name);
2710                         continue;
2711                 }
2712
2713                 pr_cont("OK\n");
2714         }
2715
2716         /* Test with all events enabled */
2717
2718         pr_info("Running tests on all trace events:\n");
2719         pr_info("Testing all events: ");
2720
2721         ret = __ftrace_set_clr_event(tr, NULL, NULL, NULL, 1);
2722         if (WARN_ON_ONCE(ret)) {
2723                 pr_warning("error enabling all events\n");
2724                 return;
2725         }
2726
2727         event_test_stuff();
2728
2729         /* reset sysname */
2730         ret = __ftrace_set_clr_event(tr, NULL, NULL, NULL, 0);
2731         if (WARN_ON_ONCE(ret)) {
2732                 pr_warning("error disabling all events\n");
2733                 return;
2734         }
2735
2736         pr_cont("OK\n");
2737 }
2738
2739 #ifdef CONFIG_FUNCTION_TRACER
2740
2741 static DEFINE_PER_CPU(atomic_t, ftrace_test_event_disable);
2742
2743 static void
2744 function_test_events_call(unsigned long ip, unsigned long parent_ip,
2745                           struct ftrace_ops *op, struct pt_regs *pt_regs)
2746 {
2747         struct ring_buffer_event *event;
2748         struct ring_buffer *buffer;
2749         struct ftrace_entry *entry;
2750         unsigned long flags;
2751         long disabled;
2752         int cpu;
2753         int pc;
2754
2755         pc = preempt_count();
2756         preempt_disable_notrace();
2757         cpu = raw_smp_processor_id();
2758         disabled = atomic_inc_return(&per_cpu(ftrace_test_event_disable, cpu));
2759
2760         if (disabled != 1)
2761                 goto out;
2762
2763         local_save_flags(flags);
2764
2765         event = trace_current_buffer_lock_reserve(&buffer,
2766                                                   TRACE_FN, sizeof(*entry),
2767                                                   flags, pc);
2768         if (!event)
2769                 goto out;
2770         entry   = ring_buffer_event_data(event);
2771         entry->ip                       = ip;
2772         entry->parent_ip                = parent_ip;
2773
2774         trace_buffer_unlock_commit(buffer, event, flags, pc);
2775
2776  out:
2777         atomic_dec(&per_cpu(ftrace_test_event_disable, cpu));
2778         preempt_enable_notrace();
2779 }
2780
2781 static struct ftrace_ops trace_ops __initdata  =
2782 {
2783         .func = function_test_events_call,
2784         .flags = FTRACE_OPS_FL_RECURSION_SAFE,
2785 };
2786
2787 static __init void event_trace_self_test_with_function(void)
2788 {
2789         int ret;
2790         ret = register_ftrace_function(&trace_ops);
2791         if (WARN_ON(ret < 0)) {
2792                 pr_info("Failed to enable function tracer for event tests\n");
2793                 return;
2794         }
2795         pr_info("Running tests again, along with the function tracer\n");
2796         event_trace_self_tests();
2797         unregister_ftrace_function(&trace_ops);
2798 }
2799 #else
2800 static __init void event_trace_self_test_with_function(void)
2801 {
2802 }
2803 #endif
2804
2805 static __init int event_trace_self_tests_init(void)
2806 {
2807         if (!tracing_selftest_disabled) {
2808                 event_trace_self_tests();
2809                 event_trace_self_test_with_function();
2810         }
2811
2812         return 0;
2813 }
2814
2815 late_initcall(event_trace_self_tests_init);
2816
2817 #endif