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