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