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