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