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