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