]> rtime.felk.cvut.cz Git - linux-imx.git/blob - kernel/trace/trace_events.c
tracing: Add ref_data to function and fgraph tracer structs
[linux-imx.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 ftrace_event_field *field;
830         struct list_head *common_head = &ftrace_common_fields;
831         struct list_head *head = trace_get_fields(call);
832
833         (*pos)++;
834
835         switch ((unsigned long)v) {
836         case FORMAT_HEADER:
837                 if (unlikely(list_empty(common_head)))
838                         return NULL;
839
840                 field = list_entry(common_head->prev,
841                                    struct ftrace_event_field, link);
842                 return field;
843
844         case FORMAT_FIELD_SEPERATOR:
845                 if (unlikely(list_empty(head)))
846                         return NULL;
847
848                 field = list_entry(head->prev, struct ftrace_event_field, link);
849                 return field;
850
851         case FORMAT_PRINTFMT:
852                 /* all done */
853                 return NULL;
854         }
855
856         field = v;
857         if (field->link.prev == common_head)
858                 return (void *)FORMAT_FIELD_SEPERATOR;
859         else if (field->link.prev == head)
860                 return (void *)FORMAT_PRINTFMT;
861
862         field = list_entry(field->link.prev, struct ftrace_event_field, link);
863
864         return field;
865 }
866
867 static void *f_start(struct seq_file *m, loff_t *pos)
868 {
869         loff_t l = 0;
870         void *p;
871
872         /* Start by showing the header */
873         if (!*pos)
874                 return (void *)FORMAT_HEADER;
875
876         p = (void *)FORMAT_HEADER;
877         do {
878                 p = f_next(m, p, &l);
879         } while (p && l < *pos);
880
881         return p;
882 }
883
884 static int f_show(struct seq_file *m, void *v)
885 {
886         struct ftrace_event_call *call = m->private;
887         struct ftrace_event_field *field;
888         const char *array_descriptor;
889
890         switch ((unsigned long)v) {
891         case FORMAT_HEADER:
892                 seq_printf(m, "name: %s\n", call->name);
893                 seq_printf(m, "ID: %d\n", call->event.type);
894                 seq_printf(m, "format:\n");
895                 return 0;
896
897         case FORMAT_FIELD_SEPERATOR:
898                 seq_putc(m, '\n');
899                 return 0;
900
901         case FORMAT_PRINTFMT:
902                 seq_printf(m, "\nprint fmt: %s\n",
903                            call->print_fmt);
904                 return 0;
905         }
906
907         field = v;
908
909         /*
910          * Smartly shows the array type(except dynamic array).
911          * Normal:
912          *      field:TYPE VAR
913          * If TYPE := TYPE[LEN], it is shown:
914          *      field:TYPE VAR[LEN]
915          */
916         array_descriptor = strchr(field->type, '[');
917
918         if (!strncmp(field->type, "__data_loc", 10))
919                 array_descriptor = NULL;
920
921         if (!array_descriptor)
922                 seq_printf(m, "\tfield:%s %s;\toffset:%u;\tsize:%u;\tsigned:%d;\n",
923                            field->type, field->name, field->offset,
924                            field->size, !!field->is_signed);
925         else
926                 seq_printf(m, "\tfield:%.*s %s%s;\toffset:%u;\tsize:%u;\tsigned:%d;\n",
927                            (int)(array_descriptor - field->type),
928                            field->type, field->name,
929                            array_descriptor, field->offset,
930                            field->size, !!field->is_signed);
931
932         return 0;
933 }
934
935 static void f_stop(struct seq_file *m, void *p)
936 {
937 }
938
939 static const struct seq_operations trace_format_seq_ops = {
940         .start          = f_start,
941         .next           = f_next,
942         .stop           = f_stop,
943         .show           = f_show,
944 };
945
946 static int trace_format_open(struct inode *inode, struct file *file)
947 {
948         struct ftrace_event_call *call = inode->i_private;
949         struct seq_file *m;
950         int ret;
951
952         ret = seq_open(file, &trace_format_seq_ops);
953         if (ret < 0)
954                 return ret;
955
956         m = file->private_data;
957         m->private = call;
958
959         return 0;
960 }
961
962 static ssize_t
963 event_id_read(struct file *filp, char __user *ubuf, size_t cnt, loff_t *ppos)
964 {
965         struct ftrace_event_call *call = filp->private_data;
966         struct trace_seq *s;
967         int r;
968
969         if (*ppos)
970                 return 0;
971
972         s = kmalloc(sizeof(*s), GFP_KERNEL);
973         if (!s)
974                 return -ENOMEM;
975
976         trace_seq_init(s);
977         trace_seq_printf(s, "%d\n", call->event.type);
978
979         r = simple_read_from_buffer(ubuf, cnt, ppos,
980                                     s->buffer, s->len);
981         kfree(s);
982         return r;
983 }
984
985 static ssize_t
986 event_filter_read(struct file *filp, char __user *ubuf, size_t cnt,
987                   loff_t *ppos)
988 {
989         struct ftrace_event_call *call = filp->private_data;
990         struct trace_seq *s;
991         int r;
992
993         if (*ppos)
994                 return 0;
995
996         s = kmalloc(sizeof(*s), GFP_KERNEL);
997         if (!s)
998                 return -ENOMEM;
999
1000         trace_seq_init(s);
1001
1002         print_event_filter(call, s);
1003         r = simple_read_from_buffer(ubuf, cnt, ppos, s->buffer, s->len);
1004
1005         kfree(s);
1006
1007         return r;
1008 }
1009
1010 static ssize_t
1011 event_filter_write(struct file *filp, const char __user *ubuf, size_t cnt,
1012                    loff_t *ppos)
1013 {
1014         struct ftrace_event_call *call = filp->private_data;
1015         char *buf;
1016         int err;
1017
1018         if (cnt >= PAGE_SIZE)
1019                 return -EINVAL;
1020
1021         buf = (char *)__get_free_page(GFP_TEMPORARY);
1022         if (!buf)
1023                 return -ENOMEM;
1024
1025         if (copy_from_user(buf, ubuf, cnt)) {
1026                 free_page((unsigned long) buf);
1027                 return -EFAULT;
1028         }
1029         buf[cnt] = '\0';
1030
1031         err = apply_event_filter(call, buf);
1032         free_page((unsigned long) buf);
1033         if (err < 0)
1034                 return err;
1035
1036         *ppos += cnt;
1037
1038         return cnt;
1039 }
1040
1041 static LIST_HEAD(event_subsystems);
1042
1043 static int subsystem_open(struct inode *inode, struct file *filp)
1044 {
1045         struct event_subsystem *system = NULL;
1046         struct ftrace_subsystem_dir *dir = NULL; /* Initialize for gcc */
1047         struct trace_array *tr;
1048         int ret;
1049
1050         /* Make sure the system still exists */
1051         mutex_lock(&trace_types_lock);
1052         mutex_lock(&event_mutex);
1053         list_for_each_entry(tr, &ftrace_trace_arrays, list) {
1054                 list_for_each_entry(dir, &tr->systems, list) {
1055                         if (dir == inode->i_private) {
1056                                 /* Don't open systems with no events */
1057                                 if (dir->nr_events) {
1058                                         __get_system_dir(dir);
1059                                         system = dir->subsystem;
1060                                 }
1061                                 goto exit_loop;
1062                         }
1063                 }
1064         }
1065  exit_loop:
1066         mutex_unlock(&event_mutex);
1067         mutex_unlock(&trace_types_lock);
1068
1069         if (!system)
1070                 return -ENODEV;
1071
1072         /* Some versions of gcc think dir can be uninitialized here */
1073         WARN_ON(!dir);
1074
1075         /* Still need to increment the ref count of the system */
1076         if (trace_array_get(tr) < 0) {
1077                 put_system(dir);
1078                 return -ENODEV;
1079         }
1080
1081         ret = tracing_open_generic(inode, filp);
1082         if (ret < 0) {
1083                 trace_array_put(tr);
1084                 put_system(dir);
1085         }
1086
1087         return ret;
1088 }
1089
1090 static int system_tr_open(struct inode *inode, struct file *filp)
1091 {
1092         struct ftrace_subsystem_dir *dir;
1093         struct trace_array *tr = inode->i_private;
1094         int ret;
1095
1096         if (trace_array_get(tr) < 0)
1097                 return -ENODEV;
1098
1099         /* Make a temporary dir that has no system but points to tr */
1100         dir = kzalloc(sizeof(*dir), GFP_KERNEL);
1101         if (!dir) {
1102                 trace_array_put(tr);
1103                 return -ENOMEM;
1104         }
1105
1106         dir->tr = tr;
1107
1108         ret = tracing_open_generic(inode, filp);
1109         if (ret < 0) {
1110                 trace_array_put(tr);
1111                 kfree(dir);
1112         }
1113
1114         filp->private_data = dir;
1115
1116         return ret;
1117 }
1118
1119 static int subsystem_release(struct inode *inode, struct file *file)
1120 {
1121         struct ftrace_subsystem_dir *dir = file->private_data;
1122
1123         trace_array_put(dir->tr);
1124
1125         /*
1126          * If dir->subsystem is NULL, then this is a temporary
1127          * descriptor that was made for a trace_array to enable
1128          * all subsystems.
1129          */
1130         if (dir->subsystem)
1131                 put_system(dir);
1132         else
1133                 kfree(dir);
1134
1135         return 0;
1136 }
1137
1138 static ssize_t
1139 subsystem_filter_read(struct file *filp, char __user *ubuf, size_t cnt,
1140                       loff_t *ppos)
1141 {
1142         struct ftrace_subsystem_dir *dir = filp->private_data;
1143         struct event_subsystem *system = dir->subsystem;
1144         struct trace_seq *s;
1145         int r;
1146
1147         if (*ppos)
1148                 return 0;
1149
1150         s = kmalloc(sizeof(*s), GFP_KERNEL);
1151         if (!s)
1152                 return -ENOMEM;
1153
1154         trace_seq_init(s);
1155
1156         print_subsystem_event_filter(system, s);
1157         r = simple_read_from_buffer(ubuf, cnt, ppos, s->buffer, s->len);
1158
1159         kfree(s);
1160
1161         return r;
1162 }
1163
1164 static ssize_t
1165 subsystem_filter_write(struct file *filp, const char __user *ubuf, size_t cnt,
1166                        loff_t *ppos)
1167 {
1168         struct ftrace_subsystem_dir *dir = filp->private_data;
1169         char *buf;
1170         int err;
1171
1172         if (cnt >= PAGE_SIZE)
1173                 return -EINVAL;
1174
1175         buf = (char *)__get_free_page(GFP_TEMPORARY);
1176         if (!buf)
1177                 return -ENOMEM;
1178
1179         if (copy_from_user(buf, ubuf, cnt)) {
1180                 free_page((unsigned long) buf);
1181                 return -EFAULT;
1182         }
1183         buf[cnt] = '\0';
1184
1185         err = apply_subsystem_event_filter(dir, buf);
1186         free_page((unsigned long) buf);
1187         if (err < 0)
1188                 return err;
1189
1190         *ppos += cnt;
1191
1192         return cnt;
1193 }
1194
1195 static ssize_t
1196 show_header(struct file *filp, char __user *ubuf, size_t cnt, loff_t *ppos)
1197 {
1198         int (*func)(struct trace_seq *s) = filp->private_data;
1199         struct trace_seq *s;
1200         int r;
1201
1202         if (*ppos)
1203                 return 0;
1204
1205         s = kmalloc(sizeof(*s), GFP_KERNEL);
1206         if (!s)
1207                 return -ENOMEM;
1208
1209         trace_seq_init(s);
1210
1211         func(s);
1212         r = simple_read_from_buffer(ubuf, cnt, ppos, s->buffer, s->len);
1213
1214         kfree(s);
1215
1216         return r;
1217 }
1218
1219 static int ftrace_event_avail_open(struct inode *inode, struct file *file);
1220 static int ftrace_event_set_open(struct inode *inode, struct file *file);
1221 static int ftrace_event_release(struct inode *inode, struct file *file);
1222
1223 static const struct seq_operations show_event_seq_ops = {
1224         .start = t_start,
1225         .next = t_next,
1226         .show = t_show,
1227         .stop = t_stop,
1228 };
1229
1230 static const struct seq_operations show_set_event_seq_ops = {
1231         .start = s_start,
1232         .next = s_next,
1233         .show = t_show,
1234         .stop = t_stop,
1235 };
1236
1237 static const struct file_operations ftrace_avail_fops = {
1238         .open = ftrace_event_avail_open,
1239         .read = seq_read,
1240         .llseek = seq_lseek,
1241         .release = seq_release,
1242 };
1243
1244 static const struct file_operations ftrace_set_event_fops = {
1245         .open = ftrace_event_set_open,
1246         .read = seq_read,
1247         .write = ftrace_event_write,
1248         .llseek = seq_lseek,
1249         .release = ftrace_event_release,
1250 };
1251
1252 static const struct file_operations ftrace_enable_fops = {
1253         .open = tracing_open_generic_file,
1254         .read = event_enable_read,
1255         .write = event_enable_write,
1256         .release = tracing_release_generic_file,
1257         .llseek = default_llseek,
1258 };
1259
1260 static const struct file_operations ftrace_event_format_fops = {
1261         .open = trace_format_open,
1262         .read = seq_read,
1263         .llseek = seq_lseek,
1264         .release = seq_release,
1265 };
1266
1267 static const struct file_operations ftrace_event_id_fops = {
1268         .open = tracing_open_generic,
1269         .read = event_id_read,
1270         .llseek = default_llseek,
1271 };
1272
1273 static const struct file_operations ftrace_event_filter_fops = {
1274         .open = tracing_open_generic,
1275         .read = event_filter_read,
1276         .write = event_filter_write,
1277         .llseek = default_llseek,
1278 };
1279
1280 static const struct file_operations ftrace_subsystem_filter_fops = {
1281         .open = subsystem_open,
1282         .read = subsystem_filter_read,
1283         .write = subsystem_filter_write,
1284         .llseek = default_llseek,
1285         .release = subsystem_release,
1286 };
1287
1288 static const struct file_operations ftrace_system_enable_fops = {
1289         .open = subsystem_open,
1290         .read = system_enable_read,
1291         .write = system_enable_write,
1292         .llseek = default_llseek,
1293         .release = subsystem_release,
1294 };
1295
1296 static const struct file_operations ftrace_tr_enable_fops = {
1297         .open = system_tr_open,
1298         .read = system_enable_read,
1299         .write = system_enable_write,
1300         .llseek = default_llseek,
1301         .release = subsystem_release,
1302 };
1303
1304 static const struct file_operations ftrace_show_header_fops = {
1305         .open = tracing_open_generic,
1306         .read = show_header,
1307         .llseek = default_llseek,
1308 };
1309
1310 static int
1311 ftrace_event_open(struct inode *inode, struct file *file,
1312                   const struct seq_operations *seq_ops)
1313 {
1314         struct seq_file *m;
1315         int ret;
1316
1317         ret = seq_open(file, seq_ops);
1318         if (ret < 0)
1319                 return ret;
1320         m = file->private_data;
1321         /* copy tr over to seq ops */
1322         m->private = inode->i_private;
1323
1324         return ret;
1325 }
1326
1327 static int ftrace_event_release(struct inode *inode, struct file *file)
1328 {
1329         struct trace_array *tr = inode->i_private;
1330
1331         trace_array_put(tr);
1332
1333         return seq_release(inode, file);
1334 }
1335
1336 static int
1337 ftrace_event_avail_open(struct inode *inode, struct file *file)
1338 {
1339         const struct seq_operations *seq_ops = &show_event_seq_ops;
1340
1341         return ftrace_event_open(inode, file, seq_ops);
1342 }
1343
1344 static int
1345 ftrace_event_set_open(struct inode *inode, struct file *file)
1346 {
1347         const struct seq_operations *seq_ops = &show_set_event_seq_ops;
1348         struct trace_array *tr = inode->i_private;
1349         int ret;
1350
1351         if (trace_array_get(tr) < 0)
1352                 return -ENODEV;
1353
1354         if ((file->f_mode & FMODE_WRITE) &&
1355             (file->f_flags & O_TRUNC))
1356                 ftrace_clear_events(tr);
1357
1358         ret = ftrace_event_open(inode, file, seq_ops);
1359         if (ret < 0)
1360                 trace_array_put(tr);
1361         return ret;
1362 }
1363
1364 static struct event_subsystem *
1365 create_new_subsystem(const char *name)
1366 {
1367         struct event_subsystem *system;
1368
1369         /* need to create new entry */
1370         system = kmalloc(sizeof(*system), GFP_KERNEL);
1371         if (!system)
1372                 return NULL;
1373
1374         system->ref_count = 1;
1375
1376         /* Only allocate if dynamic (kprobes and modules) */
1377         if (!core_kernel_data((unsigned long)name)) {
1378                 system->ref_count |= SYSTEM_FL_FREE_NAME;
1379                 system->name = kstrdup(name, GFP_KERNEL);
1380                 if (!system->name)
1381                         goto out_free;
1382         } else
1383                 system->name = name;
1384
1385         system->filter = NULL;
1386
1387         system->filter = kzalloc(sizeof(struct event_filter), GFP_KERNEL);
1388         if (!system->filter)
1389                 goto out_free;
1390
1391         list_add(&system->list, &event_subsystems);
1392
1393         return system;
1394
1395  out_free:
1396         if (system->ref_count & SYSTEM_FL_FREE_NAME)
1397                 kfree(system->name);
1398         kfree(system);
1399         return NULL;
1400 }
1401
1402 static struct dentry *
1403 event_subsystem_dir(struct trace_array *tr, const char *name,
1404                     struct ftrace_event_file *file, struct dentry *parent)
1405 {
1406         struct ftrace_subsystem_dir *dir;
1407         struct event_subsystem *system;
1408         struct dentry *entry;
1409
1410         /* First see if we did not already create this dir */
1411         list_for_each_entry(dir, &tr->systems, list) {
1412                 system = dir->subsystem;
1413                 if (strcmp(system->name, name) == 0) {
1414                         dir->nr_events++;
1415                         file->system = dir;
1416                         return dir->entry;
1417                 }
1418         }
1419
1420         /* Now see if the system itself exists. */
1421         list_for_each_entry(system, &event_subsystems, list) {
1422                 if (strcmp(system->name, name) == 0)
1423                         break;
1424         }
1425         /* Reset system variable when not found */
1426         if (&system->list == &event_subsystems)
1427                 system = NULL;
1428
1429         dir = kmalloc(sizeof(*dir), GFP_KERNEL);
1430         if (!dir)
1431                 goto out_fail;
1432
1433         if (!system) {
1434                 system = create_new_subsystem(name);
1435                 if (!system)
1436                         goto out_free;
1437         } else
1438                 __get_system(system);
1439
1440         dir->entry = debugfs_create_dir(name, parent);
1441         if (!dir->entry) {
1442                 pr_warning("Failed to create system directory %s\n", name);
1443                 __put_system(system);
1444                 goto out_free;
1445         }
1446
1447         dir->tr = tr;
1448         dir->ref_count = 1;
1449         dir->nr_events = 1;
1450         dir->subsystem = system;
1451         file->system = dir;
1452
1453         entry = debugfs_create_file("filter", 0644, dir->entry, dir,
1454                                     &ftrace_subsystem_filter_fops);
1455         if (!entry) {
1456                 kfree(system->filter);
1457                 system->filter = NULL;
1458                 pr_warning("Could not create debugfs '%s/filter' entry\n", name);
1459         }
1460
1461         trace_create_file("enable", 0644, dir->entry, dir,
1462                           &ftrace_system_enable_fops);
1463
1464         list_add(&dir->list, &tr->systems);
1465
1466         return dir->entry;
1467
1468  out_free:
1469         kfree(dir);
1470  out_fail:
1471         /* Only print this message if failed on memory allocation */
1472         if (!dir || !system)
1473                 pr_warning("No memory to create event subsystem %s\n",
1474                            name);
1475         return NULL;
1476 }
1477
1478 static int
1479 event_create_dir(struct dentry *parent,
1480                  struct ftrace_event_file *file,
1481                  const struct file_operations *id,
1482                  const struct file_operations *enable,
1483                  const struct file_operations *filter,
1484                  const struct file_operations *format)
1485 {
1486         struct ftrace_event_call *call = file->event_call;
1487         struct trace_array *tr = file->tr;
1488         struct list_head *head;
1489         struct dentry *d_events;
1490         int ret;
1491
1492         /*
1493          * If the trace point header did not define TRACE_SYSTEM
1494          * then the system would be called "TRACE_SYSTEM".
1495          */
1496         if (strcmp(call->class->system, TRACE_SYSTEM) != 0) {
1497                 d_events = event_subsystem_dir(tr, call->class->system, file, parent);
1498                 if (!d_events)
1499                         return -ENOMEM;
1500         } else
1501                 d_events = parent;
1502
1503         file->dir = debugfs_create_dir(call->name, d_events);
1504         if (!file->dir) {
1505                 pr_warning("Could not create debugfs '%s' directory\n",
1506                            call->name);
1507                 return -1;
1508         }
1509
1510         if (call->class->reg && !(call->flags & TRACE_EVENT_FL_IGNORE_ENABLE))
1511                 trace_create_file("enable", 0644, file->dir, file,
1512                                   enable);
1513
1514 #ifdef CONFIG_PERF_EVENTS
1515         if (call->event.type && call->class->reg)
1516                 trace_create_file("id", 0444, file->dir, call,
1517                                   id);
1518 #endif
1519
1520         /*
1521          * Other events may have the same class. Only update
1522          * the fields if they are not already defined.
1523          */
1524         head = trace_get_fields(call);
1525         if (list_empty(head)) {
1526                 ret = call->class->define_fields(call);
1527                 if (ret < 0) {
1528                         pr_warning("Could not initialize trace point"
1529                                    " events/%s\n", call->name);
1530                         return -1;
1531                 }
1532         }
1533         trace_create_file("filter", 0644, file->dir, call,
1534                           filter);
1535
1536         trace_create_file("format", 0444, file->dir, call,
1537                           format);
1538
1539         return 0;
1540 }
1541
1542 static void remove_subsystem(struct ftrace_subsystem_dir *dir)
1543 {
1544         if (!dir)
1545                 return;
1546
1547         if (!--dir->nr_events) {
1548                 debugfs_remove_recursive(dir->entry);
1549                 list_del(&dir->list);
1550                 __put_system_dir(dir);
1551         }
1552 }
1553
1554 static void remove_event_from_tracers(struct ftrace_event_call *call)
1555 {
1556         struct ftrace_event_file *file;
1557         struct trace_array *tr;
1558
1559         do_for_each_event_file_safe(tr, file) {
1560
1561                 if (file->event_call != call)
1562                         continue;
1563
1564                 list_del(&file->list);
1565                 debugfs_remove_recursive(file->dir);
1566                 remove_subsystem(file->system);
1567                 kmem_cache_free(file_cachep, file);
1568
1569                 /*
1570                  * The do_for_each_event_file_safe() is
1571                  * a double loop. After finding the call for this
1572                  * trace_array, we use break to jump to the next
1573                  * trace_array.
1574                  */
1575                 break;
1576         } while_for_each_event_file();
1577 }
1578
1579 static void event_remove(struct ftrace_event_call *call)
1580 {
1581         struct trace_array *tr;
1582         struct ftrace_event_file *file;
1583
1584         do_for_each_event_file(tr, file) {
1585                 if (file->event_call != call)
1586                         continue;
1587                 ftrace_event_enable_disable(file, 0);
1588                 /*
1589                  * The do_for_each_event_file() is
1590                  * a double loop. After finding the call for this
1591                  * trace_array, we use break to jump to the next
1592                  * trace_array.
1593                  */
1594                 break;
1595         } while_for_each_event_file();
1596
1597         if (call->event.funcs)
1598                 __unregister_ftrace_event(&call->event);
1599         remove_event_from_tracers(call);
1600         list_del(&call->list);
1601 }
1602
1603 static int event_init(struct ftrace_event_call *call)
1604 {
1605         int ret = 0;
1606
1607         if (WARN_ON(!call->name))
1608                 return -EINVAL;
1609
1610         if (call->class->raw_init) {
1611                 ret = call->class->raw_init(call);
1612                 if (ret < 0 && ret != -ENOSYS)
1613                         pr_warn("Could not initialize trace events/%s\n",
1614                                 call->name);
1615         }
1616
1617         return ret;
1618 }
1619
1620 static int
1621 __register_event(struct ftrace_event_call *call, struct module *mod)
1622 {
1623         int ret;
1624
1625         ret = event_init(call);
1626         if (ret < 0)
1627                 return ret;
1628
1629         list_add(&call->list, &ftrace_events);
1630         call->mod = mod;
1631
1632         return 0;
1633 }
1634
1635 static struct ftrace_event_file *
1636 trace_create_new_event(struct ftrace_event_call *call,
1637                        struct trace_array *tr)
1638 {
1639         struct ftrace_event_file *file;
1640
1641         file = kmem_cache_alloc(file_cachep, GFP_TRACE);
1642         if (!file)
1643                 return NULL;
1644
1645         file->event_call = call;
1646         file->tr = tr;
1647         atomic_set(&file->sm_ref, 0);
1648         list_add(&file->list, &tr->events);
1649
1650         return file;
1651 }
1652
1653 /* Add an event to a trace directory */
1654 static int
1655 __trace_add_new_event(struct ftrace_event_call *call,
1656                       struct trace_array *tr,
1657                       const struct file_operations *id,
1658                       const struct file_operations *enable,
1659                       const struct file_operations *filter,
1660                       const struct file_operations *format)
1661 {
1662         struct ftrace_event_file *file;
1663
1664         file = trace_create_new_event(call, tr);
1665         if (!file)
1666                 return -ENOMEM;
1667
1668         return event_create_dir(tr->event_dir, file, id, enable, filter, format);
1669 }
1670
1671 /*
1672  * Just create a decriptor for early init. A descriptor is required
1673  * for enabling events at boot. We want to enable events before
1674  * the filesystem is initialized.
1675  */
1676 static __init int
1677 __trace_early_add_new_event(struct ftrace_event_call *call,
1678                             struct trace_array *tr)
1679 {
1680         struct ftrace_event_file *file;
1681
1682         file = trace_create_new_event(call, tr);
1683         if (!file)
1684                 return -ENOMEM;
1685
1686         return 0;
1687 }
1688
1689 struct ftrace_module_file_ops;
1690 static void __add_event_to_tracers(struct ftrace_event_call *call,
1691                                    struct ftrace_module_file_ops *file_ops);
1692
1693 /* Add an additional event_call dynamically */
1694 int trace_add_event_call(struct ftrace_event_call *call)
1695 {
1696         int ret;
1697         mutex_lock(&trace_types_lock);
1698         mutex_lock(&event_mutex);
1699
1700         ret = __register_event(call, NULL);
1701         if (ret >= 0)
1702                 __add_event_to_tracers(call, NULL);
1703
1704         mutex_unlock(&event_mutex);
1705         mutex_unlock(&trace_types_lock);
1706         return ret;
1707 }
1708
1709 /*
1710  * Must be called under locking of trace_types_lock, event_mutex and
1711  * trace_event_sem.
1712  */
1713 static void __trace_remove_event_call(struct ftrace_event_call *call)
1714 {
1715         event_remove(call);
1716         trace_destroy_fields(call);
1717         destroy_preds(call);
1718 }
1719
1720 /* Remove an event_call */
1721 void trace_remove_event_call(struct ftrace_event_call *call)
1722 {
1723         mutex_lock(&trace_types_lock);
1724         mutex_lock(&event_mutex);
1725         down_write(&trace_event_sem);
1726         __trace_remove_event_call(call);
1727         up_write(&trace_event_sem);
1728         mutex_unlock(&event_mutex);
1729         mutex_unlock(&trace_types_lock);
1730 }
1731
1732 #define for_each_event(event, start, end)                       \
1733         for (event = start;                                     \
1734              (unsigned long)event < (unsigned long)end;         \
1735              event++)
1736
1737 #ifdef CONFIG_MODULES
1738
1739 static LIST_HEAD(ftrace_module_file_list);
1740
1741 /*
1742  * Modules must own their file_operations to keep up with
1743  * reference counting.
1744  */
1745 struct ftrace_module_file_ops {
1746         struct list_head                list;
1747         struct module                   *mod;
1748         struct file_operations          id;
1749         struct file_operations          enable;
1750         struct file_operations          format;
1751         struct file_operations          filter;
1752 };
1753
1754 static struct ftrace_module_file_ops *
1755 find_ftrace_file_ops(struct ftrace_module_file_ops *file_ops, struct module *mod)
1756 {
1757         /*
1758          * As event_calls are added in groups by module,
1759          * when we find one file_ops, we don't need to search for
1760          * each call in that module, as the rest should be the
1761          * same. Only search for a new one if the last one did
1762          * not match.
1763          */
1764         if (file_ops && mod == file_ops->mod)
1765                 return file_ops;
1766
1767         list_for_each_entry(file_ops, &ftrace_module_file_list, list) {
1768                 if (file_ops->mod == mod)
1769                         return file_ops;
1770         }
1771         return NULL;
1772 }
1773
1774 static struct ftrace_module_file_ops *
1775 trace_create_file_ops(struct module *mod)
1776 {
1777         struct ftrace_module_file_ops *file_ops;
1778
1779         /*
1780          * This is a bit of a PITA. To allow for correct reference
1781          * counting, modules must "own" their file_operations.
1782          * To do this, we allocate the file operations that will be
1783          * used in the event directory.
1784          */
1785
1786         file_ops = kmalloc(sizeof(*file_ops), GFP_KERNEL);
1787         if (!file_ops)
1788                 return NULL;
1789
1790         file_ops->mod = mod;
1791
1792         file_ops->id = ftrace_event_id_fops;
1793         file_ops->id.owner = mod;
1794
1795         file_ops->enable = ftrace_enable_fops;
1796         file_ops->enable.owner = mod;
1797
1798         file_ops->filter = ftrace_event_filter_fops;
1799         file_ops->filter.owner = mod;
1800
1801         file_ops->format = ftrace_event_format_fops;
1802         file_ops->format.owner = mod;
1803
1804         list_add(&file_ops->list, &ftrace_module_file_list);
1805
1806         return file_ops;
1807 }
1808
1809 static void trace_module_add_events(struct module *mod)
1810 {
1811         struct ftrace_module_file_ops *file_ops = NULL;
1812         struct ftrace_event_call **call, **start, **end;
1813
1814         start = mod->trace_events;
1815         end = mod->trace_events + mod->num_trace_events;
1816
1817         if (start == end)
1818                 return;
1819
1820         file_ops = trace_create_file_ops(mod);
1821         if (!file_ops)
1822                 return;
1823
1824         for_each_event(call, start, end) {
1825                 __register_event(*call, mod);
1826                 __add_event_to_tracers(*call, file_ops);
1827         }
1828 }
1829
1830 static void trace_module_remove_events(struct module *mod)
1831 {
1832         struct ftrace_module_file_ops *file_ops;
1833         struct ftrace_event_call *call, *p;
1834         bool clear_trace = false;
1835
1836         down_write(&trace_event_sem);
1837         list_for_each_entry_safe(call, p, &ftrace_events, list) {
1838                 if (call->mod == mod) {
1839                         if (call->flags & TRACE_EVENT_FL_WAS_ENABLED)
1840                                 clear_trace = true;
1841                         __trace_remove_event_call(call);
1842                 }
1843         }
1844
1845         /* Now free the file_operations */
1846         list_for_each_entry(file_ops, &ftrace_module_file_list, list) {
1847                 if (file_ops->mod == mod)
1848                         break;
1849         }
1850         if (&file_ops->list != &ftrace_module_file_list) {
1851                 list_del(&file_ops->list);
1852                 kfree(file_ops);
1853         }
1854         up_write(&trace_event_sem);
1855
1856         /*
1857          * It is safest to reset the ring buffer if the module being unloaded
1858          * registered any events that were used. The only worry is if
1859          * a new module gets loaded, and takes on the same id as the events
1860          * of this module. When printing out the buffer, traced events left
1861          * over from this module may be passed to the new module events and
1862          * unexpected results may occur.
1863          */
1864         if (clear_trace)
1865                 tracing_reset_all_online_cpus();
1866 }
1867
1868 static int trace_module_notify(struct notifier_block *self,
1869                                unsigned long val, void *data)
1870 {
1871         struct module *mod = data;
1872
1873         mutex_lock(&trace_types_lock);
1874         mutex_lock(&event_mutex);
1875         switch (val) {
1876         case MODULE_STATE_COMING:
1877                 trace_module_add_events(mod);
1878                 break;
1879         case MODULE_STATE_GOING:
1880                 trace_module_remove_events(mod);
1881                 break;
1882         }
1883         mutex_unlock(&event_mutex);
1884         mutex_unlock(&trace_types_lock);
1885
1886         return 0;
1887 }
1888
1889 static int
1890 __trace_add_new_mod_event(struct ftrace_event_call *call,
1891                           struct trace_array *tr,
1892                           struct ftrace_module_file_ops *file_ops)
1893 {
1894         return __trace_add_new_event(call, tr,
1895                                      &file_ops->id, &file_ops->enable,
1896                                      &file_ops->filter, &file_ops->format);
1897 }
1898
1899 #else
1900 static inline struct ftrace_module_file_ops *
1901 find_ftrace_file_ops(struct ftrace_module_file_ops *file_ops, struct module *mod)
1902 {
1903         return NULL;
1904 }
1905 static inline int trace_module_notify(struct notifier_block *self,
1906                                       unsigned long val, void *data)
1907 {
1908         return 0;
1909 }
1910 static inline int
1911 __trace_add_new_mod_event(struct ftrace_event_call *call,
1912                           struct trace_array *tr,
1913                           struct ftrace_module_file_ops *file_ops)
1914 {
1915         return -ENODEV;
1916 }
1917 #endif /* CONFIG_MODULES */
1918
1919 /* Create a new event directory structure for a trace directory. */
1920 static void
1921 __trace_add_event_dirs(struct trace_array *tr)
1922 {
1923         struct ftrace_module_file_ops *file_ops = NULL;
1924         struct ftrace_event_call *call;
1925         int ret;
1926
1927         list_for_each_entry(call, &ftrace_events, list) {
1928                 if (call->mod) {
1929                         /*
1930                          * Directories for events by modules need to
1931                          * keep module ref counts when opened (as we don't
1932                          * want the module to disappear when reading one
1933                          * of these files). The file_ops keep account of
1934                          * the module ref count.
1935                          */
1936                         file_ops = find_ftrace_file_ops(file_ops, call->mod);
1937                         if (!file_ops)
1938                                 continue; /* Warn? */
1939                         ret = __trace_add_new_mod_event(call, tr, file_ops);
1940                         if (ret < 0)
1941                                 pr_warning("Could not create directory for event %s\n",
1942                                            call->name);
1943                         continue;
1944                 }
1945                 ret = __trace_add_new_event(call, tr,
1946                                             &ftrace_event_id_fops,
1947                                             &ftrace_enable_fops,
1948                                             &ftrace_event_filter_fops,
1949                                             &ftrace_event_format_fops);
1950                 if (ret < 0)
1951                         pr_warning("Could not create directory for event %s\n",
1952                                    call->name);
1953         }
1954 }
1955
1956 #ifdef CONFIG_DYNAMIC_FTRACE
1957
1958 /* Avoid typos */
1959 #define ENABLE_EVENT_STR        "enable_event"
1960 #define DISABLE_EVENT_STR       "disable_event"
1961
1962 struct event_probe_data {
1963         struct ftrace_event_file        *file;
1964         unsigned long                   count;
1965         int                             ref;
1966         bool                            enable;
1967 };
1968
1969 static struct ftrace_event_file *
1970 find_event_file(struct trace_array *tr, const char *system,  const char *event)
1971 {
1972         struct ftrace_event_file *file;
1973         struct ftrace_event_call *call;
1974
1975         list_for_each_entry(file, &tr->events, list) {
1976
1977                 call = file->event_call;
1978
1979                 if (!call->name || !call->class || !call->class->reg)
1980                         continue;
1981
1982                 if (call->flags & TRACE_EVENT_FL_IGNORE_ENABLE)
1983                         continue;
1984
1985                 if (strcmp(event, call->name) == 0 &&
1986                     strcmp(system, call->class->system) == 0)
1987                         return file;
1988         }
1989         return NULL;
1990 }
1991
1992 static void
1993 event_enable_probe(unsigned long ip, unsigned long parent_ip, void **_data)
1994 {
1995         struct event_probe_data **pdata = (struct event_probe_data **)_data;
1996         struct event_probe_data *data = *pdata;
1997
1998         if (!data)
1999                 return;
2000
2001         if (data->enable)
2002                 clear_bit(FTRACE_EVENT_FL_SOFT_DISABLED_BIT, &data->file->flags);
2003         else
2004                 set_bit(FTRACE_EVENT_FL_SOFT_DISABLED_BIT, &data->file->flags);
2005 }
2006
2007 static void
2008 event_enable_count_probe(unsigned long ip, unsigned long parent_ip, void **_data)
2009 {
2010         struct event_probe_data **pdata = (struct event_probe_data **)_data;
2011         struct event_probe_data *data = *pdata;
2012
2013         if (!data)
2014                 return;
2015
2016         if (!data->count)
2017                 return;
2018
2019         /* Skip if the event is in a state we want to switch to */
2020         if (data->enable == !(data->file->flags & FTRACE_EVENT_FL_SOFT_DISABLED))
2021                 return;
2022
2023         if (data->count != -1)
2024                 (data->count)--;
2025
2026         event_enable_probe(ip, parent_ip, _data);
2027 }
2028
2029 static int
2030 event_enable_print(struct seq_file *m, unsigned long ip,
2031                       struct ftrace_probe_ops *ops, void *_data)
2032 {
2033         struct event_probe_data *data = _data;
2034
2035         seq_printf(m, "%ps:", (void *)ip);
2036
2037         seq_printf(m, "%s:%s:%s",
2038                    data->enable ? ENABLE_EVENT_STR : DISABLE_EVENT_STR,
2039                    data->file->event_call->class->system,
2040                    data->file->event_call->name);
2041
2042         if (data->count == -1)
2043                 seq_printf(m, ":unlimited\n");
2044         else
2045                 seq_printf(m, ":count=%ld\n", data->count);
2046
2047         return 0;
2048 }
2049
2050 static int
2051 event_enable_init(struct ftrace_probe_ops *ops, unsigned long ip,
2052                   void **_data)
2053 {
2054         struct event_probe_data **pdata = (struct event_probe_data **)_data;
2055         struct event_probe_data *data = *pdata;
2056
2057         data->ref++;
2058         return 0;
2059 }
2060
2061 static void
2062 event_enable_free(struct ftrace_probe_ops *ops, unsigned long ip,
2063                   void **_data)
2064 {
2065         struct event_probe_data **pdata = (struct event_probe_data **)_data;
2066         struct event_probe_data *data = *pdata;
2067
2068         if (WARN_ON_ONCE(data->ref <= 0))
2069                 return;
2070
2071         data->ref--;
2072         if (!data->ref) {
2073                 /* Remove the SOFT_MODE flag */
2074                 __ftrace_event_enable_disable(data->file, 0, 1);
2075                 module_put(data->file->event_call->mod);
2076                 kfree(data);
2077         }
2078         *pdata = NULL;
2079 }
2080
2081 static struct ftrace_probe_ops event_enable_probe_ops = {
2082         .func                   = event_enable_probe,
2083         .print                  = event_enable_print,
2084         .init                   = event_enable_init,
2085         .free                   = event_enable_free,
2086 };
2087
2088 static struct ftrace_probe_ops event_enable_count_probe_ops = {
2089         .func                   = event_enable_count_probe,
2090         .print                  = event_enable_print,
2091         .init                   = event_enable_init,
2092         .free                   = event_enable_free,
2093 };
2094
2095 static struct ftrace_probe_ops event_disable_probe_ops = {
2096         .func                   = event_enable_probe,
2097         .print                  = event_enable_print,
2098         .init                   = event_enable_init,
2099         .free                   = event_enable_free,
2100 };
2101
2102 static struct ftrace_probe_ops event_disable_count_probe_ops = {
2103         .func                   = event_enable_count_probe,
2104         .print                  = event_enable_print,
2105         .init                   = event_enable_init,
2106         .free                   = event_enable_free,
2107 };
2108
2109 static int
2110 event_enable_func(struct ftrace_hash *hash,
2111                   char *glob, char *cmd, char *param, int enabled)
2112 {
2113         struct trace_array *tr = top_trace_array();
2114         struct ftrace_event_file *file;
2115         struct ftrace_probe_ops *ops;
2116         struct event_probe_data *data;
2117         const char *system;
2118         const char *event;
2119         char *number;
2120         bool enable;
2121         int ret;
2122
2123         /* hash funcs only work with set_ftrace_filter */
2124         if (!enabled || !param)
2125                 return -EINVAL;
2126
2127         system = strsep(&param, ":");
2128         if (!param)
2129                 return -EINVAL;
2130
2131         event = strsep(&param, ":");
2132
2133         mutex_lock(&event_mutex);
2134
2135         ret = -EINVAL;
2136         file = find_event_file(tr, system, event);
2137         if (!file)
2138                 goto out;
2139
2140         enable = strcmp(cmd, ENABLE_EVENT_STR) == 0;
2141
2142         if (enable)
2143                 ops = param ? &event_enable_count_probe_ops : &event_enable_probe_ops;
2144         else
2145                 ops = param ? &event_disable_count_probe_ops : &event_disable_probe_ops;
2146
2147         if (glob[0] == '!') {
2148                 unregister_ftrace_function_probe_func(glob+1, ops);
2149                 ret = 0;
2150                 goto out;
2151         }
2152
2153         ret = -ENOMEM;
2154         data = kzalloc(sizeof(*data), GFP_KERNEL);
2155         if (!data)
2156                 goto out;
2157
2158         data->enable = enable;
2159         data->count = -1;
2160         data->file = file;
2161
2162         if (!param)
2163                 goto out_reg;
2164
2165         number = strsep(&param, ":");
2166
2167         ret = -EINVAL;
2168         if (!strlen(number))
2169                 goto out_free;
2170
2171         /*
2172          * We use the callback data field (which is a pointer)
2173          * as our counter.
2174          */
2175         ret = kstrtoul(number, 0, &data->count);
2176         if (ret)
2177                 goto out_free;
2178
2179  out_reg:
2180         /* Don't let event modules unload while probe registered */
2181         ret = try_module_get(file->event_call->mod);
2182         if (!ret) {
2183                 ret = -EBUSY;
2184                 goto out_free;
2185         }
2186
2187         ret = __ftrace_event_enable_disable(file, 1, 1);
2188         if (ret < 0)
2189                 goto out_put;
2190         ret = register_ftrace_function_probe(glob, ops, data);
2191         /*
2192          * The above returns on success the # of functions enabled,
2193          * but if it didn't find any functions it returns zero.
2194          * Consider no functions a failure too.
2195          */
2196         if (!ret) {
2197                 ret = -ENOENT;
2198                 goto out_disable;
2199         } else if (ret < 0)
2200                 goto out_disable;
2201         /* Just return zero, not the number of enabled functions */
2202         ret = 0;
2203  out:
2204         mutex_unlock(&event_mutex);
2205         return ret;
2206
2207  out_disable:
2208         __ftrace_event_enable_disable(file, 0, 1);
2209  out_put:
2210         module_put(file->event_call->mod);
2211  out_free:
2212         kfree(data);
2213         goto out;
2214 }
2215
2216 static struct ftrace_func_command event_enable_cmd = {
2217         .name                   = ENABLE_EVENT_STR,
2218         .func                   = event_enable_func,
2219 };
2220
2221 static struct ftrace_func_command event_disable_cmd = {
2222         .name                   = DISABLE_EVENT_STR,
2223         .func                   = event_enable_func,
2224 };
2225
2226 static __init int register_event_cmds(void)
2227 {
2228         int ret;
2229
2230         ret = register_ftrace_command(&event_enable_cmd);
2231         if (WARN_ON(ret < 0))
2232                 return ret;
2233         ret = register_ftrace_command(&event_disable_cmd);
2234         if (WARN_ON(ret < 0))
2235                 unregister_ftrace_command(&event_enable_cmd);
2236         return ret;
2237 }
2238 #else
2239 static inline int register_event_cmds(void) { return 0; }
2240 #endif /* CONFIG_DYNAMIC_FTRACE */
2241
2242 /*
2243  * The top level array has already had its ftrace_event_file
2244  * descriptors created in order to allow for early events to
2245  * be recorded. This function is called after the debugfs has been
2246  * initialized, and we now have to create the files associated
2247  * to the events.
2248  */
2249 static __init void
2250 __trace_early_add_event_dirs(struct trace_array *tr)
2251 {
2252         struct ftrace_event_file *file;
2253         int ret;
2254
2255
2256         list_for_each_entry(file, &tr->events, list) {
2257                 ret = event_create_dir(tr->event_dir, file,
2258                                        &ftrace_event_id_fops,
2259                                        &ftrace_enable_fops,
2260                                        &ftrace_event_filter_fops,
2261                                        &ftrace_event_format_fops);
2262                 if (ret < 0)
2263                         pr_warning("Could not create directory for event %s\n",
2264                                    file->event_call->name);
2265         }
2266 }
2267
2268 /*
2269  * For early boot up, the top trace array requires to have
2270  * a list of events that can be enabled. This must be done before
2271  * the filesystem is set up in order to allow events to be traced
2272  * early.
2273  */
2274 static __init void
2275 __trace_early_add_events(struct trace_array *tr)
2276 {
2277         struct ftrace_event_call *call;
2278         int ret;
2279
2280         list_for_each_entry(call, &ftrace_events, list) {
2281                 /* Early boot up should not have any modules loaded */
2282                 if (WARN_ON_ONCE(call->mod))
2283                         continue;
2284
2285                 ret = __trace_early_add_new_event(call, tr);
2286                 if (ret < 0)
2287                         pr_warning("Could not create early event %s\n",
2288                                    call->name);
2289         }
2290 }
2291
2292 /* Remove the event directory structure for a trace directory. */
2293 static void
2294 __trace_remove_event_dirs(struct trace_array *tr)
2295 {
2296         struct ftrace_event_file *file, *next;
2297
2298         list_for_each_entry_safe(file, next, &tr->events, list) {
2299                 list_del(&file->list);
2300                 debugfs_remove_recursive(file->dir);
2301                 remove_subsystem(file->system);
2302                 kmem_cache_free(file_cachep, file);
2303         }
2304 }
2305
2306 static void
2307 __add_event_to_tracers(struct ftrace_event_call *call,
2308                        struct ftrace_module_file_ops *file_ops)
2309 {
2310         struct trace_array *tr;
2311
2312         list_for_each_entry(tr, &ftrace_trace_arrays, list) {
2313                 if (file_ops)
2314                         __trace_add_new_mod_event(call, tr, file_ops);
2315                 else
2316                         __trace_add_new_event(call, tr,
2317                                               &ftrace_event_id_fops,
2318                                               &ftrace_enable_fops,
2319                                               &ftrace_event_filter_fops,
2320                                               &ftrace_event_format_fops);
2321         }
2322 }
2323
2324 static struct notifier_block trace_module_nb = {
2325         .notifier_call = trace_module_notify,
2326         .priority = 0,
2327 };
2328
2329 extern struct ftrace_event_call *__start_ftrace_events[];
2330 extern struct ftrace_event_call *__stop_ftrace_events[];
2331
2332 static char bootup_event_buf[COMMAND_LINE_SIZE] __initdata;
2333
2334 static __init int setup_trace_event(char *str)
2335 {
2336         strlcpy(bootup_event_buf, str, COMMAND_LINE_SIZE);
2337         ring_buffer_expanded = true;
2338         tracing_selftest_disabled = true;
2339
2340         return 1;
2341 }
2342 __setup("trace_event=", setup_trace_event);
2343
2344 /* Expects to have event_mutex held when called */
2345 static int
2346 create_event_toplevel_files(struct dentry *parent, struct trace_array *tr)
2347 {
2348         struct dentry *d_events;
2349         struct dentry *entry;
2350
2351         entry = debugfs_create_file("set_event", 0644, parent,
2352                                     tr, &ftrace_set_event_fops);
2353         if (!entry) {
2354                 pr_warning("Could not create debugfs 'set_event' entry\n");
2355                 return -ENOMEM;
2356         }
2357
2358         d_events = debugfs_create_dir("events", parent);
2359         if (!d_events) {
2360                 pr_warning("Could not create debugfs 'events' directory\n");
2361                 return -ENOMEM;
2362         }
2363
2364         /* ring buffer internal formats */
2365         trace_create_file("header_page", 0444, d_events,
2366                           ring_buffer_print_page_header,
2367                           &ftrace_show_header_fops);
2368
2369         trace_create_file("header_event", 0444, d_events,
2370                           ring_buffer_print_entry_header,
2371                           &ftrace_show_header_fops);
2372
2373         trace_create_file("enable", 0644, d_events,
2374                           tr, &ftrace_tr_enable_fops);
2375
2376         tr->event_dir = d_events;
2377
2378         return 0;
2379 }
2380
2381 /**
2382  * event_trace_add_tracer - add a instance of a trace_array to events
2383  * @parent: The parent dentry to place the files/directories for events in
2384  * @tr: The trace array associated with these events
2385  *
2386  * When a new instance is created, it needs to set up its events
2387  * directory, as well as other files associated with events. It also
2388  * creates the event hierachry in the @parent/events directory.
2389  *
2390  * Returns 0 on success.
2391  */
2392 int event_trace_add_tracer(struct dentry *parent, struct trace_array *tr)
2393 {
2394         int ret;
2395
2396         mutex_lock(&event_mutex);
2397
2398         ret = create_event_toplevel_files(parent, tr);
2399         if (ret)
2400                 goto out_unlock;
2401
2402         down_write(&trace_event_sem);
2403         __trace_add_event_dirs(tr);
2404         up_write(&trace_event_sem);
2405
2406  out_unlock:
2407         mutex_unlock(&event_mutex);
2408
2409         return ret;
2410 }
2411
2412 /*
2413  * The top trace array already had its file descriptors created.
2414  * Now the files themselves need to be created.
2415  */
2416 static __init int
2417 early_event_add_tracer(struct dentry *parent, struct trace_array *tr)
2418 {
2419         int ret;
2420
2421         mutex_lock(&event_mutex);
2422
2423         ret = create_event_toplevel_files(parent, tr);
2424         if (ret)
2425                 goto out_unlock;
2426
2427         down_write(&trace_event_sem);
2428         __trace_early_add_event_dirs(tr);
2429         up_write(&trace_event_sem);
2430
2431  out_unlock:
2432         mutex_unlock(&event_mutex);
2433
2434         return ret;
2435 }
2436
2437 int event_trace_del_tracer(struct trace_array *tr)
2438 {
2439         mutex_lock(&event_mutex);
2440
2441         /* Disable any running events */
2442         __ftrace_set_clr_event_nolock(tr, NULL, NULL, NULL, 0);
2443
2444         down_write(&trace_event_sem);
2445         __trace_remove_event_dirs(tr);
2446         debugfs_remove_recursive(tr->event_dir);
2447         up_write(&trace_event_sem);
2448
2449         tr->event_dir = NULL;
2450
2451         mutex_unlock(&event_mutex);
2452
2453         return 0;
2454 }
2455
2456 static __init int event_trace_memsetup(void)
2457 {
2458         field_cachep = KMEM_CACHE(ftrace_event_field, SLAB_PANIC);
2459         file_cachep = KMEM_CACHE(ftrace_event_file, SLAB_PANIC);
2460         return 0;
2461 }
2462
2463 static __init int event_trace_enable(void)
2464 {
2465         struct trace_array *tr = top_trace_array();
2466         struct ftrace_event_call **iter, *call;
2467         char *buf = bootup_event_buf;
2468         char *token;
2469         int ret;
2470
2471         for_each_event(iter, __start_ftrace_events, __stop_ftrace_events) {
2472
2473                 call = *iter;
2474                 ret = event_init(call);
2475                 if (!ret)
2476                         list_add(&call->list, &ftrace_events);
2477         }
2478
2479         /*
2480          * We need the top trace array to have a working set of trace
2481          * points at early init, before the debug files and directories
2482          * are created. Create the file entries now, and attach them
2483          * to the actual file dentries later.
2484          */
2485         __trace_early_add_events(tr);
2486
2487         while (true) {
2488                 token = strsep(&buf, ",");
2489
2490                 if (!token)
2491                         break;
2492                 if (!*token)
2493                         continue;
2494
2495                 ret = ftrace_set_clr_event(tr, token, 1);
2496                 if (ret)
2497                         pr_warn("Failed to enable trace event: %s\n", token);
2498         }
2499
2500         trace_printk_start_comm();
2501
2502         register_event_cmds();
2503
2504         return 0;
2505 }
2506
2507 static __init int event_trace_init(void)
2508 {
2509         struct trace_array *tr;
2510         struct dentry *d_tracer;
2511         struct dentry *entry;
2512         int ret;
2513
2514         tr = top_trace_array();
2515
2516         d_tracer = tracing_init_dentry();
2517         if (!d_tracer)
2518                 return 0;
2519
2520         entry = debugfs_create_file("available_events", 0444, d_tracer,
2521                                     tr, &ftrace_avail_fops);
2522         if (!entry)
2523                 pr_warning("Could not create debugfs "
2524                            "'available_events' entry\n");
2525
2526         if (trace_define_common_fields())
2527                 pr_warning("tracing: Failed to allocate common fields");
2528
2529         ret = early_event_add_tracer(d_tracer, tr);
2530         if (ret)
2531                 return ret;
2532
2533         ret = register_module_notifier(&trace_module_nb);
2534         if (ret)
2535                 pr_warning("Failed to register trace events module notifier\n");
2536
2537         return 0;
2538 }
2539 early_initcall(event_trace_memsetup);
2540 core_initcall(event_trace_enable);
2541 fs_initcall(event_trace_init);
2542
2543 #ifdef CONFIG_FTRACE_STARTUP_TEST
2544
2545 static DEFINE_SPINLOCK(test_spinlock);
2546 static DEFINE_SPINLOCK(test_spinlock_irq);
2547 static DEFINE_MUTEX(test_mutex);
2548
2549 static __init void test_work(struct work_struct *dummy)
2550 {
2551         spin_lock(&test_spinlock);
2552         spin_lock_irq(&test_spinlock_irq);
2553         udelay(1);
2554         spin_unlock_irq(&test_spinlock_irq);
2555         spin_unlock(&test_spinlock);
2556
2557         mutex_lock(&test_mutex);
2558         msleep(1);
2559         mutex_unlock(&test_mutex);
2560 }
2561
2562 static __init int event_test_thread(void *unused)
2563 {
2564         void *test_malloc;
2565
2566         test_malloc = kmalloc(1234, GFP_KERNEL);
2567         if (!test_malloc)
2568                 pr_info("failed to kmalloc\n");
2569
2570         schedule_on_each_cpu(test_work);
2571
2572         kfree(test_malloc);
2573
2574         set_current_state(TASK_INTERRUPTIBLE);
2575         while (!kthread_should_stop())
2576                 schedule();
2577
2578         return 0;
2579 }
2580
2581 /*
2582  * Do various things that may trigger events.
2583  */
2584 static __init void event_test_stuff(void)
2585 {
2586         struct task_struct *test_thread;
2587
2588         test_thread = kthread_run(event_test_thread, NULL, "test-events");
2589         msleep(1);
2590         kthread_stop(test_thread);
2591 }
2592
2593 /*
2594  * For every trace event defined, we will test each trace point separately,
2595  * and then by groups, and finally all trace points.
2596  */
2597 static __init void event_trace_self_tests(void)
2598 {
2599         struct ftrace_subsystem_dir *dir;
2600         struct ftrace_event_file *file;
2601         struct ftrace_event_call *call;
2602         struct event_subsystem *system;
2603         struct trace_array *tr;
2604         int ret;
2605
2606         tr = top_trace_array();
2607
2608         pr_info("Running tests on trace events:\n");
2609
2610         list_for_each_entry(file, &tr->events, list) {
2611
2612                 call = file->event_call;
2613
2614                 /* Only test those that have a probe */
2615                 if (!call->class || !call->class->probe)
2616                         continue;
2617
2618 /*
2619  * Testing syscall events here is pretty useless, but
2620  * we still do it if configured. But this is time consuming.
2621  * What we really need is a user thread to perform the
2622  * syscalls as we test.
2623  */
2624 #ifndef CONFIG_EVENT_TRACE_TEST_SYSCALLS
2625                 if (call->class->system &&
2626                     strcmp(call->class->system, "syscalls") == 0)
2627                         continue;
2628 #endif
2629
2630                 pr_info("Testing event %s: ", call->name);
2631
2632                 /*
2633                  * If an event is already enabled, someone is using
2634                  * it and the self test should not be on.
2635                  */
2636                 if (file->flags & FTRACE_EVENT_FL_ENABLED) {
2637                         pr_warning("Enabled event during self test!\n");
2638                         WARN_ON_ONCE(1);
2639                         continue;
2640                 }
2641
2642                 ftrace_event_enable_disable(file, 1);
2643                 event_test_stuff();
2644                 ftrace_event_enable_disable(file, 0);
2645
2646                 pr_cont("OK\n");
2647         }
2648
2649         /* Now test at the sub system level */
2650
2651         pr_info("Running tests on trace event systems:\n");
2652
2653         list_for_each_entry(dir, &tr->systems, list) {
2654
2655                 system = dir->subsystem;
2656
2657                 /* the ftrace system is special, skip it */
2658                 if (strcmp(system->name, "ftrace") == 0)
2659                         continue;
2660
2661                 pr_info("Testing event system %s: ", system->name);
2662
2663                 ret = __ftrace_set_clr_event(tr, NULL, system->name, NULL, 1);
2664                 if (WARN_ON_ONCE(ret)) {
2665                         pr_warning("error enabling system %s\n",
2666                                    system->name);
2667                         continue;
2668                 }
2669
2670                 event_test_stuff();
2671
2672                 ret = __ftrace_set_clr_event(tr, NULL, system->name, NULL, 0);
2673                 if (WARN_ON_ONCE(ret)) {
2674                         pr_warning("error disabling system %s\n",
2675                                    system->name);
2676                         continue;
2677                 }
2678
2679                 pr_cont("OK\n");
2680         }
2681
2682         /* Test with all events enabled */
2683
2684         pr_info("Running tests on all trace events:\n");
2685         pr_info("Testing all events: ");
2686
2687         ret = __ftrace_set_clr_event(tr, NULL, NULL, NULL, 1);
2688         if (WARN_ON_ONCE(ret)) {
2689                 pr_warning("error enabling all events\n");
2690                 return;
2691         }
2692
2693         event_test_stuff();
2694
2695         /* reset sysname */
2696         ret = __ftrace_set_clr_event(tr, NULL, NULL, NULL, 0);
2697         if (WARN_ON_ONCE(ret)) {
2698                 pr_warning("error disabling all events\n");
2699                 return;
2700         }
2701
2702         pr_cont("OK\n");
2703 }
2704
2705 #ifdef CONFIG_FUNCTION_TRACER
2706
2707 static DEFINE_PER_CPU(atomic_t, ftrace_test_event_disable);
2708
2709 static void
2710 function_test_events_call(unsigned long ip, unsigned long parent_ip,
2711                           struct ftrace_ops *op, struct pt_regs *pt_regs)
2712 {
2713         struct ring_buffer_event *event;
2714         struct ring_buffer *buffer;
2715         struct ftrace_entry *entry;
2716         unsigned long flags;
2717         long disabled;
2718         int cpu;
2719         int pc;
2720
2721         pc = preempt_count();
2722         preempt_disable_notrace();
2723         cpu = raw_smp_processor_id();
2724         disabled = atomic_inc_return(&per_cpu(ftrace_test_event_disable, cpu));
2725
2726         if (disabled != 1)
2727                 goto out;
2728
2729         local_save_flags(flags);
2730
2731         event = trace_current_buffer_lock_reserve(&buffer,
2732                                                   TRACE_FN, sizeof(*entry),
2733                                                   flags, pc);
2734         if (!event)
2735                 goto out;
2736         entry   = ring_buffer_event_data(event);
2737         entry->ip                       = ip;
2738         entry->parent_ip                = parent_ip;
2739
2740         trace_buffer_unlock_commit(buffer, event, flags, pc);
2741
2742  out:
2743         atomic_dec(&per_cpu(ftrace_test_event_disable, cpu));
2744         preempt_enable_notrace();
2745 }
2746
2747 static struct ftrace_ops trace_ops __initdata  =
2748 {
2749         .func = function_test_events_call,
2750         .flags = FTRACE_OPS_FL_RECURSION_SAFE,
2751 };
2752
2753 static __init void event_trace_self_test_with_function(void)
2754 {
2755         int ret;
2756         ret = register_ftrace_function(&trace_ops);
2757         if (WARN_ON(ret < 0)) {
2758                 pr_info("Failed to enable function tracer for event tests\n");
2759                 return;
2760         }
2761         pr_info("Running tests again, along with the function tracer\n");
2762         event_trace_self_tests();
2763         unregister_ftrace_function(&trace_ops);
2764 }
2765 #else
2766 static __init void event_trace_self_test_with_function(void)
2767 {
2768 }
2769 #endif
2770
2771 static __init int event_trace_self_tests_init(void)
2772 {
2773         if (!tracing_selftest_disabled) {
2774                 event_trace_self_tests();
2775                 event_trace_self_test_with_function();
2776         }
2777
2778         return 0;
2779 }
2780
2781 late_initcall(event_trace_self_tests_init);
2782
2783 #endif