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