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