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