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