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