tracing/dynevent: Delegate parsing to create function
[linux-2.6-microblaze.git] / kernel / trace / trace_events.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * event tracer
4  *
5  * Copyright (C) 2008 Red Hat Inc, Steven Rostedt <srostedt@redhat.com>
6  *
7  *  - Added format output of fields of the trace point.
8  *    This was based off of work by Tom Zanussi <tzanussi@gmail.com>.
9  *
10  */
11
12 #define pr_fmt(fmt) fmt
13
14 #include <linux/workqueue.h>
15 #include <linux/security.h>
16 #include <linux/spinlock.h>
17 #include <linux/kthread.h>
18 #include <linux/tracefs.h>
19 #include <linux/uaccess.h>
20 #include <linux/module.h>
21 #include <linux/ctype.h>
22 #include <linux/sort.h>
23 #include <linux/slab.h>
24 #include <linux/delay.h>
25
26 #include <trace/events/sched.h>
27 #include <trace/syscall.h>
28
29 #include <asm/setup.h>
30
31 #include "trace_output.h"
32
33 #undef TRACE_SYSTEM
34 #define TRACE_SYSTEM "TRACE_SYSTEM"
35
36 DEFINE_MUTEX(event_mutex);
37
38 LIST_HEAD(ftrace_events);
39 static LIST_HEAD(ftrace_generic_fields);
40 static LIST_HEAD(ftrace_common_fields);
41 static bool eventdir_initialized;
42
43 #define GFP_TRACE (GFP_KERNEL | __GFP_ZERO)
44
45 static struct kmem_cache *field_cachep;
46 static struct kmem_cache *file_cachep;
47
48 static inline int system_refcount(struct event_subsystem *system)
49 {
50         return system->ref_count;
51 }
52
53 static int system_refcount_inc(struct event_subsystem *system)
54 {
55         return system->ref_count++;
56 }
57
58 static int system_refcount_dec(struct event_subsystem *system)
59 {
60         return --system->ref_count;
61 }
62
63 /* Double loops, do not use break, only goto's work */
64 #define do_for_each_event_file(tr, file)                        \
65         list_for_each_entry(tr, &ftrace_trace_arrays, list) {   \
66                 list_for_each_entry(file, &tr->events, list)
67
68 #define do_for_each_event_file_safe(tr, file)                   \
69         list_for_each_entry(tr, &ftrace_trace_arrays, list) {   \
70                 struct trace_event_file *___n;                          \
71                 list_for_each_entry_safe(file, ___n, &tr->events, list)
72
73 #define while_for_each_event_file()             \
74         }
75
76 static struct ftrace_event_field *
77 __find_event_field(struct list_head *head, char *name)
78 {
79         struct ftrace_event_field *field;
80
81         list_for_each_entry(field, head, link) {
82                 if (!strcmp(field->name, name))
83                         return field;
84         }
85
86         return NULL;
87 }
88
89 struct ftrace_event_field *
90 trace_find_event_field(struct trace_event_call *call, char *name)
91 {
92         struct ftrace_event_field *field;
93         struct list_head *head;
94
95         head = trace_get_fields(call);
96         field = __find_event_field(head, name);
97         if (field)
98                 return field;
99
100         field = __find_event_field(&ftrace_generic_fields, name);
101         if (field)
102                 return field;
103
104         return __find_event_field(&ftrace_common_fields, name);
105 }
106
107 static int __trace_define_field(struct list_head *head, const char *type,
108                                 const char *name, int offset, int size,
109                                 int is_signed, int filter_type)
110 {
111         struct ftrace_event_field *field;
112
113         field = kmem_cache_alloc(field_cachep, GFP_TRACE);
114         if (!field)
115                 return -ENOMEM;
116
117         field->name = name;
118         field->type = type;
119
120         if (filter_type == FILTER_OTHER)
121                 field->filter_type = filter_assign_type(type);
122         else
123                 field->filter_type = filter_type;
124
125         field->offset = offset;
126         field->size = size;
127         field->is_signed = is_signed;
128
129         list_add(&field->link, head);
130
131         return 0;
132 }
133
134 int trace_define_field(struct trace_event_call *call, const char *type,
135                        const char *name, int offset, int size, int is_signed,
136                        int filter_type)
137 {
138         struct list_head *head;
139
140         if (WARN_ON(!call->class))
141                 return 0;
142
143         head = trace_get_fields(call);
144         return __trace_define_field(head, type, name, offset, size,
145                                     is_signed, filter_type);
146 }
147 EXPORT_SYMBOL_GPL(trace_define_field);
148
149 #define __generic_field(type, item, filter_type)                        \
150         ret = __trace_define_field(&ftrace_generic_fields, #type,       \
151                                    #item, 0, 0, is_signed_type(type),   \
152                                    filter_type);                        \
153         if (ret)                                                        \
154                 return ret;
155
156 #define __common_field(type, item)                                      \
157         ret = __trace_define_field(&ftrace_common_fields, #type,        \
158                                    "common_" #item,                     \
159                                    offsetof(typeof(ent), item),         \
160                                    sizeof(ent.item),                    \
161                                    is_signed_type(type), FILTER_OTHER); \
162         if (ret)                                                        \
163                 return ret;
164
165 static int trace_define_generic_fields(void)
166 {
167         int ret;
168
169         __generic_field(int, CPU, FILTER_CPU);
170         __generic_field(int, cpu, FILTER_CPU);
171         __generic_field(char *, COMM, FILTER_COMM);
172         __generic_field(char *, comm, FILTER_COMM);
173
174         return ret;
175 }
176
177 static int trace_define_common_fields(void)
178 {
179         int ret;
180         struct trace_entry ent;
181
182         __common_field(unsigned short, type);
183         __common_field(unsigned char, flags);
184         __common_field(unsigned char, preempt_count);
185         __common_field(int, pid);
186
187         return ret;
188 }
189
190 static void trace_destroy_fields(struct trace_event_call *call)
191 {
192         struct ftrace_event_field *field, *next;
193         struct list_head *head;
194
195         head = trace_get_fields(call);
196         list_for_each_entry_safe(field, next, head, link) {
197                 list_del(&field->link);
198                 kmem_cache_free(field_cachep, field);
199         }
200 }
201
202 /*
203  * run-time version of trace_event_get_offsets_<call>() that returns the last
204  * accessible offset of trace fields excluding __dynamic_array bytes
205  */
206 int trace_event_get_offsets(struct trace_event_call *call)
207 {
208         struct ftrace_event_field *tail;
209         struct list_head *head;
210
211         head = trace_get_fields(call);
212         /*
213          * head->next points to the last field with the largest offset,
214          * since it was added last by trace_define_field()
215          */
216         tail = list_first_entry(head, struct ftrace_event_field, link);
217         return tail->offset + tail->size;
218 }
219
220 int trace_event_raw_init(struct trace_event_call *call)
221 {
222         int id;
223
224         id = register_trace_event(&call->event);
225         if (!id)
226                 return -ENODEV;
227
228         return 0;
229 }
230 EXPORT_SYMBOL_GPL(trace_event_raw_init);
231
232 bool trace_event_ignore_this_pid(struct trace_event_file *trace_file)
233 {
234         struct trace_array *tr = trace_file->tr;
235         struct trace_array_cpu *data;
236         struct trace_pid_list *no_pid_list;
237         struct trace_pid_list *pid_list;
238
239         pid_list = rcu_dereference_raw(tr->filtered_pids);
240         no_pid_list = rcu_dereference_raw(tr->filtered_no_pids);
241
242         if (!pid_list && !no_pid_list)
243                 return false;
244
245         data = this_cpu_ptr(tr->array_buffer.data);
246
247         return data->ignore_pid;
248 }
249 EXPORT_SYMBOL_GPL(trace_event_ignore_this_pid);
250
251 void *trace_event_buffer_reserve(struct trace_event_buffer *fbuffer,
252                                  struct trace_event_file *trace_file,
253                                  unsigned long len)
254 {
255         struct trace_event_call *event_call = trace_file->event_call;
256
257         if ((trace_file->flags & EVENT_FILE_FL_PID_FILTER) &&
258             trace_event_ignore_this_pid(trace_file))
259                 return NULL;
260
261         /*
262          * If CONFIG_PREEMPTION is enabled, then the tracepoint itself disables
263          * preemption (adding one to the preempt_count). Since we are
264          * interested in the preempt_count at the time the tracepoint was
265          * hit, we need to subtract one to offset the increment.
266          */
267         fbuffer->trace_ctx = tracing_gen_ctx_dec();
268         fbuffer->trace_file = trace_file;
269
270         fbuffer->event =
271                 trace_event_buffer_lock_reserve(&fbuffer->buffer, trace_file,
272                                                 event_call->event.type, len,
273                                                 fbuffer->trace_ctx);
274         if (!fbuffer->event)
275                 return NULL;
276
277         fbuffer->regs = NULL;
278         fbuffer->entry = ring_buffer_event_data(fbuffer->event);
279         return fbuffer->entry;
280 }
281 EXPORT_SYMBOL_GPL(trace_event_buffer_reserve);
282
283 int trace_event_reg(struct trace_event_call *call,
284                     enum trace_reg type, void *data)
285 {
286         struct trace_event_file *file = data;
287
288         WARN_ON(!(call->flags & TRACE_EVENT_FL_TRACEPOINT));
289         switch (type) {
290         case TRACE_REG_REGISTER:
291                 return tracepoint_probe_register(call->tp,
292                                                  call->class->probe,
293                                                  file);
294         case TRACE_REG_UNREGISTER:
295                 tracepoint_probe_unregister(call->tp,
296                                             call->class->probe,
297                                             file);
298                 return 0;
299
300 #ifdef CONFIG_PERF_EVENTS
301         case TRACE_REG_PERF_REGISTER:
302                 return tracepoint_probe_register(call->tp,
303                                                  call->class->perf_probe,
304                                                  call);
305         case TRACE_REG_PERF_UNREGISTER:
306                 tracepoint_probe_unregister(call->tp,
307                                             call->class->perf_probe,
308                                             call);
309                 return 0;
310         case TRACE_REG_PERF_OPEN:
311         case TRACE_REG_PERF_CLOSE:
312         case TRACE_REG_PERF_ADD:
313         case TRACE_REG_PERF_DEL:
314                 return 0;
315 #endif
316         }
317         return 0;
318 }
319 EXPORT_SYMBOL_GPL(trace_event_reg);
320
321 void trace_event_enable_cmd_record(bool enable)
322 {
323         struct trace_event_file *file;
324         struct trace_array *tr;
325
326         lockdep_assert_held(&event_mutex);
327
328         do_for_each_event_file(tr, file) {
329
330                 if (!(file->flags & EVENT_FILE_FL_ENABLED))
331                         continue;
332
333                 if (enable) {
334                         tracing_start_cmdline_record();
335                         set_bit(EVENT_FILE_FL_RECORDED_CMD_BIT, &file->flags);
336                 } else {
337                         tracing_stop_cmdline_record();
338                         clear_bit(EVENT_FILE_FL_RECORDED_CMD_BIT, &file->flags);
339                 }
340         } while_for_each_event_file();
341 }
342
343 void trace_event_enable_tgid_record(bool enable)
344 {
345         struct trace_event_file *file;
346         struct trace_array *tr;
347
348         lockdep_assert_held(&event_mutex);
349
350         do_for_each_event_file(tr, file) {
351                 if (!(file->flags & EVENT_FILE_FL_ENABLED))
352                         continue;
353
354                 if (enable) {
355                         tracing_start_tgid_record();
356                         set_bit(EVENT_FILE_FL_RECORDED_TGID_BIT, &file->flags);
357                 } else {
358                         tracing_stop_tgid_record();
359                         clear_bit(EVENT_FILE_FL_RECORDED_TGID_BIT,
360                                   &file->flags);
361                 }
362         } while_for_each_event_file();
363 }
364
365 static int __ftrace_event_enable_disable(struct trace_event_file *file,
366                                          int enable, int soft_disable)
367 {
368         struct trace_event_call *call = file->event_call;
369         struct trace_array *tr = file->tr;
370         unsigned long file_flags = file->flags;
371         int ret = 0;
372         int disable;
373
374         switch (enable) {
375         case 0:
376                 /*
377                  * When soft_disable is set and enable is cleared, the sm_ref
378                  * reference counter is decremented. If it reaches 0, we want
379                  * to clear the SOFT_DISABLED flag but leave the event in the
380                  * state that it was. That is, if the event was enabled and
381                  * SOFT_DISABLED isn't set, then do nothing. But if SOFT_DISABLED
382                  * is set we do not want the event to be enabled before we
383                  * clear the bit.
384                  *
385                  * When soft_disable is not set but the SOFT_MODE flag is,
386                  * we do nothing. Do not disable the tracepoint, otherwise
387                  * "soft enable"s (clearing the SOFT_DISABLED bit) wont work.
388                  */
389                 if (soft_disable) {
390                         if (atomic_dec_return(&file->sm_ref) > 0)
391                                 break;
392                         disable = file->flags & EVENT_FILE_FL_SOFT_DISABLED;
393                         clear_bit(EVENT_FILE_FL_SOFT_MODE_BIT, &file->flags);
394                 } else
395                         disable = !(file->flags & EVENT_FILE_FL_SOFT_MODE);
396
397                 if (disable && (file->flags & EVENT_FILE_FL_ENABLED)) {
398                         clear_bit(EVENT_FILE_FL_ENABLED_BIT, &file->flags);
399                         if (file->flags & EVENT_FILE_FL_RECORDED_CMD) {
400                                 tracing_stop_cmdline_record();
401                                 clear_bit(EVENT_FILE_FL_RECORDED_CMD_BIT, &file->flags);
402                         }
403
404                         if (file->flags & EVENT_FILE_FL_RECORDED_TGID) {
405                                 tracing_stop_tgid_record();
406                                 clear_bit(EVENT_FILE_FL_RECORDED_TGID_BIT, &file->flags);
407                         }
408
409                         call->class->reg(call, TRACE_REG_UNREGISTER, file);
410                 }
411                 /* If in SOFT_MODE, just set the SOFT_DISABLE_BIT, else clear it */
412                 if (file->flags & EVENT_FILE_FL_SOFT_MODE)
413                         set_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &file->flags);
414                 else
415                         clear_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &file->flags);
416                 break;
417         case 1:
418                 /*
419                  * When soft_disable is set and enable is set, we want to
420                  * register the tracepoint for the event, but leave the event
421                  * as is. That means, if the event was already enabled, we do
422                  * nothing (but set SOFT_MODE). If the event is disabled, we
423                  * set SOFT_DISABLED before enabling the event tracepoint, so
424                  * it still seems to be disabled.
425                  */
426                 if (!soft_disable)
427                         clear_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &file->flags);
428                 else {
429                         if (atomic_inc_return(&file->sm_ref) > 1)
430                                 break;
431                         set_bit(EVENT_FILE_FL_SOFT_MODE_BIT, &file->flags);
432                 }
433
434                 if (!(file->flags & EVENT_FILE_FL_ENABLED)) {
435                         bool cmd = false, tgid = false;
436
437                         /* Keep the event disabled, when going to SOFT_MODE. */
438                         if (soft_disable)
439                                 set_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &file->flags);
440
441                         if (tr->trace_flags & TRACE_ITER_RECORD_CMD) {
442                                 cmd = true;
443                                 tracing_start_cmdline_record();
444                                 set_bit(EVENT_FILE_FL_RECORDED_CMD_BIT, &file->flags);
445                         }
446
447                         if (tr->trace_flags & TRACE_ITER_RECORD_TGID) {
448                                 tgid = true;
449                                 tracing_start_tgid_record();
450                                 set_bit(EVENT_FILE_FL_RECORDED_TGID_BIT, &file->flags);
451                         }
452
453                         ret = call->class->reg(call, TRACE_REG_REGISTER, file);
454                         if (ret) {
455                                 if (cmd)
456                                         tracing_stop_cmdline_record();
457                                 if (tgid)
458                                         tracing_stop_tgid_record();
459                                 pr_info("event trace: Could not enable event "
460                                         "%s\n", trace_event_name(call));
461                                 break;
462                         }
463                         set_bit(EVENT_FILE_FL_ENABLED_BIT, &file->flags);
464
465                         /* WAS_ENABLED gets set but never cleared. */
466                         set_bit(EVENT_FILE_FL_WAS_ENABLED_BIT, &file->flags);
467                 }
468                 break;
469         }
470
471         /* Enable or disable use of trace_buffered_event */
472         if ((file_flags & EVENT_FILE_FL_SOFT_DISABLED) !=
473             (file->flags & EVENT_FILE_FL_SOFT_DISABLED)) {
474                 if (file->flags & EVENT_FILE_FL_SOFT_DISABLED)
475                         trace_buffered_event_enable();
476                 else
477                         trace_buffered_event_disable();
478         }
479
480         return ret;
481 }
482
483 int trace_event_enable_disable(struct trace_event_file *file,
484                                int enable, int soft_disable)
485 {
486         return __ftrace_event_enable_disable(file, enable, soft_disable);
487 }
488
489 static int ftrace_event_enable_disable(struct trace_event_file *file,
490                                        int enable)
491 {
492         return __ftrace_event_enable_disable(file, enable, 0);
493 }
494
495 static void ftrace_clear_events(struct trace_array *tr)
496 {
497         struct trace_event_file *file;
498
499         mutex_lock(&event_mutex);
500         list_for_each_entry(file, &tr->events, list) {
501                 ftrace_event_enable_disable(file, 0);
502         }
503         mutex_unlock(&event_mutex);
504 }
505
506 static void
507 event_filter_pid_sched_process_exit(void *data, struct task_struct *task)
508 {
509         struct trace_pid_list *pid_list;
510         struct trace_array *tr = data;
511
512         pid_list = rcu_dereference_raw(tr->filtered_pids);
513         trace_filter_add_remove_task(pid_list, NULL, task);
514
515         pid_list = rcu_dereference_raw(tr->filtered_no_pids);
516         trace_filter_add_remove_task(pid_list, NULL, task);
517 }
518
519 static void
520 event_filter_pid_sched_process_fork(void *data,
521                                     struct task_struct *self,
522                                     struct task_struct *task)
523 {
524         struct trace_pid_list *pid_list;
525         struct trace_array *tr = data;
526
527         pid_list = rcu_dereference_sched(tr->filtered_pids);
528         trace_filter_add_remove_task(pid_list, self, task);
529
530         pid_list = rcu_dereference_sched(tr->filtered_no_pids);
531         trace_filter_add_remove_task(pid_list, self, task);
532 }
533
534 void trace_event_follow_fork(struct trace_array *tr, bool enable)
535 {
536         if (enable) {
537                 register_trace_prio_sched_process_fork(event_filter_pid_sched_process_fork,
538                                                        tr, INT_MIN);
539                 register_trace_prio_sched_process_free(event_filter_pid_sched_process_exit,
540                                                        tr, INT_MAX);
541         } else {
542                 unregister_trace_sched_process_fork(event_filter_pid_sched_process_fork,
543                                                     tr);
544                 unregister_trace_sched_process_free(event_filter_pid_sched_process_exit,
545                                                     tr);
546         }
547 }
548
549 static void
550 event_filter_pid_sched_switch_probe_pre(void *data, bool preempt,
551                     struct task_struct *prev, struct task_struct *next)
552 {
553         struct trace_array *tr = data;
554         struct trace_pid_list *no_pid_list;
555         struct trace_pid_list *pid_list;
556         bool ret;
557
558         pid_list = rcu_dereference_sched(tr->filtered_pids);
559         no_pid_list = rcu_dereference_sched(tr->filtered_no_pids);
560
561         /*
562          * Sched switch is funny, as we only want to ignore it
563          * in the notrace case if both prev and next should be ignored.
564          */
565         ret = trace_ignore_this_task(NULL, no_pid_list, prev) &&
566                 trace_ignore_this_task(NULL, no_pid_list, next);
567
568         this_cpu_write(tr->array_buffer.data->ignore_pid, ret ||
569                        (trace_ignore_this_task(pid_list, NULL, prev) &&
570                         trace_ignore_this_task(pid_list, NULL, next)));
571 }
572
573 static void
574 event_filter_pid_sched_switch_probe_post(void *data, bool preempt,
575                     struct task_struct *prev, struct task_struct *next)
576 {
577         struct trace_array *tr = data;
578         struct trace_pid_list *no_pid_list;
579         struct trace_pid_list *pid_list;
580
581         pid_list = rcu_dereference_sched(tr->filtered_pids);
582         no_pid_list = rcu_dereference_sched(tr->filtered_no_pids);
583
584         this_cpu_write(tr->array_buffer.data->ignore_pid,
585                        trace_ignore_this_task(pid_list, no_pid_list, next));
586 }
587
588 static void
589 event_filter_pid_sched_wakeup_probe_pre(void *data, struct task_struct *task)
590 {
591         struct trace_array *tr = data;
592         struct trace_pid_list *no_pid_list;
593         struct trace_pid_list *pid_list;
594
595         /* Nothing to do if we are already tracing */
596         if (!this_cpu_read(tr->array_buffer.data->ignore_pid))
597                 return;
598
599         pid_list = rcu_dereference_sched(tr->filtered_pids);
600         no_pid_list = rcu_dereference_sched(tr->filtered_no_pids);
601
602         this_cpu_write(tr->array_buffer.data->ignore_pid,
603                        trace_ignore_this_task(pid_list, no_pid_list, task));
604 }
605
606 static void
607 event_filter_pid_sched_wakeup_probe_post(void *data, struct task_struct *task)
608 {
609         struct trace_array *tr = data;
610         struct trace_pid_list *no_pid_list;
611         struct trace_pid_list *pid_list;
612
613         /* Nothing to do if we are not tracing */
614         if (this_cpu_read(tr->array_buffer.data->ignore_pid))
615                 return;
616
617         pid_list = rcu_dereference_sched(tr->filtered_pids);
618         no_pid_list = rcu_dereference_sched(tr->filtered_no_pids);
619
620         /* Set tracing if current is enabled */
621         this_cpu_write(tr->array_buffer.data->ignore_pid,
622                        trace_ignore_this_task(pid_list, no_pid_list, current));
623 }
624
625 static void unregister_pid_events(struct trace_array *tr)
626 {
627         unregister_trace_sched_switch(event_filter_pid_sched_switch_probe_pre, tr);
628         unregister_trace_sched_switch(event_filter_pid_sched_switch_probe_post, tr);
629
630         unregister_trace_sched_wakeup(event_filter_pid_sched_wakeup_probe_pre, tr);
631         unregister_trace_sched_wakeup(event_filter_pid_sched_wakeup_probe_post, tr);
632
633         unregister_trace_sched_wakeup_new(event_filter_pid_sched_wakeup_probe_pre, tr);
634         unregister_trace_sched_wakeup_new(event_filter_pid_sched_wakeup_probe_post, tr);
635
636         unregister_trace_sched_waking(event_filter_pid_sched_wakeup_probe_pre, tr);
637         unregister_trace_sched_waking(event_filter_pid_sched_wakeup_probe_post, tr);
638 }
639
640 static void __ftrace_clear_event_pids(struct trace_array *tr, int type)
641 {
642         struct trace_pid_list *pid_list;
643         struct trace_pid_list *no_pid_list;
644         struct trace_event_file *file;
645         int cpu;
646
647         pid_list = rcu_dereference_protected(tr->filtered_pids,
648                                              lockdep_is_held(&event_mutex));
649         no_pid_list = rcu_dereference_protected(tr->filtered_no_pids,
650                                              lockdep_is_held(&event_mutex));
651
652         /* Make sure there's something to do */
653         if (!pid_type_enabled(type, pid_list, no_pid_list))
654                 return;
655
656         if (!still_need_pid_events(type, pid_list, no_pid_list)) {
657                 unregister_pid_events(tr);
658
659                 list_for_each_entry(file, &tr->events, list) {
660                         clear_bit(EVENT_FILE_FL_PID_FILTER_BIT, &file->flags);
661                 }
662
663                 for_each_possible_cpu(cpu)
664                         per_cpu_ptr(tr->array_buffer.data, cpu)->ignore_pid = false;
665         }
666
667         if (type & TRACE_PIDS)
668                 rcu_assign_pointer(tr->filtered_pids, NULL);
669
670         if (type & TRACE_NO_PIDS)
671                 rcu_assign_pointer(tr->filtered_no_pids, NULL);
672
673         /* Wait till all users are no longer using pid filtering */
674         tracepoint_synchronize_unregister();
675
676         if ((type & TRACE_PIDS) && pid_list)
677                 trace_free_pid_list(pid_list);
678
679         if ((type & TRACE_NO_PIDS) && no_pid_list)
680                 trace_free_pid_list(no_pid_list);
681 }
682
683 static void ftrace_clear_event_pids(struct trace_array *tr, int type)
684 {
685         mutex_lock(&event_mutex);
686         __ftrace_clear_event_pids(tr, type);
687         mutex_unlock(&event_mutex);
688 }
689
690 static void __put_system(struct event_subsystem *system)
691 {
692         struct event_filter *filter = system->filter;
693
694         WARN_ON_ONCE(system_refcount(system) == 0);
695         if (system_refcount_dec(system))
696                 return;
697
698         list_del(&system->list);
699
700         if (filter) {
701                 kfree(filter->filter_string);
702                 kfree(filter);
703         }
704         kfree_const(system->name);
705         kfree(system);
706 }
707
708 static void __get_system(struct event_subsystem *system)
709 {
710         WARN_ON_ONCE(system_refcount(system) == 0);
711         system_refcount_inc(system);
712 }
713
714 static void __get_system_dir(struct trace_subsystem_dir *dir)
715 {
716         WARN_ON_ONCE(dir->ref_count == 0);
717         dir->ref_count++;
718         __get_system(dir->subsystem);
719 }
720
721 static void __put_system_dir(struct trace_subsystem_dir *dir)
722 {
723         WARN_ON_ONCE(dir->ref_count == 0);
724         /* If the subsystem is about to be freed, the dir must be too */
725         WARN_ON_ONCE(system_refcount(dir->subsystem) == 1 && dir->ref_count != 1);
726
727         __put_system(dir->subsystem);
728         if (!--dir->ref_count)
729                 kfree(dir);
730 }
731
732 static void put_system(struct trace_subsystem_dir *dir)
733 {
734         mutex_lock(&event_mutex);
735         __put_system_dir(dir);
736         mutex_unlock(&event_mutex);
737 }
738
739 static void remove_subsystem(struct trace_subsystem_dir *dir)
740 {
741         if (!dir)
742                 return;
743
744         if (!--dir->nr_events) {
745                 tracefs_remove(dir->entry);
746                 list_del(&dir->list);
747                 __put_system_dir(dir);
748         }
749 }
750
751 static void remove_event_file_dir(struct trace_event_file *file)
752 {
753         struct dentry *dir = file->dir;
754         struct dentry *child;
755
756         if (dir) {
757                 spin_lock(&dir->d_lock);        /* probably unneeded */
758                 list_for_each_entry(child, &dir->d_subdirs, d_child) {
759                         if (d_really_is_positive(child))        /* probably unneeded */
760                                 d_inode(child)->i_private = NULL;
761                 }
762                 spin_unlock(&dir->d_lock);
763
764                 tracefs_remove(dir);
765         }
766
767         list_del(&file->list);
768         remove_subsystem(file->system);
769         free_event_filter(file->filter);
770         kmem_cache_free(file_cachep, file);
771 }
772
773 /*
774  * __ftrace_set_clr_event(NULL, NULL, NULL, set) will set/unset all events.
775  */
776 static int
777 __ftrace_set_clr_event_nolock(struct trace_array *tr, const char *match,
778                               const char *sub, const char *event, int set)
779 {
780         struct trace_event_file *file;
781         struct trace_event_call *call;
782         const char *name;
783         int ret = -EINVAL;
784         int eret = 0;
785
786         list_for_each_entry(file, &tr->events, list) {
787
788                 call = file->event_call;
789                 name = trace_event_name(call);
790
791                 if (!name || !call->class || !call->class->reg)
792                         continue;
793
794                 if (call->flags & TRACE_EVENT_FL_IGNORE_ENABLE)
795                         continue;
796
797                 if (match &&
798                     strcmp(match, name) != 0 &&
799                     strcmp(match, call->class->system) != 0)
800                         continue;
801
802                 if (sub && strcmp(sub, call->class->system) != 0)
803                         continue;
804
805                 if (event && strcmp(event, name) != 0)
806                         continue;
807
808                 ret = ftrace_event_enable_disable(file, set);
809
810                 /*
811                  * Save the first error and return that. Some events
812                  * may still have been enabled, but let the user
813                  * know that something went wrong.
814                  */
815                 if (ret && !eret)
816                         eret = ret;
817
818                 ret = eret;
819         }
820
821         return ret;
822 }
823
824 static int __ftrace_set_clr_event(struct trace_array *tr, const char *match,
825                                   const char *sub, const char *event, int set)
826 {
827         int ret;
828
829         mutex_lock(&event_mutex);
830         ret = __ftrace_set_clr_event_nolock(tr, match, sub, event, set);
831         mutex_unlock(&event_mutex);
832
833         return ret;
834 }
835
836 int ftrace_set_clr_event(struct trace_array *tr, char *buf, int set)
837 {
838         char *event = NULL, *sub = NULL, *match;
839         int ret;
840
841         if (!tr)
842                 return -ENOENT;
843         /*
844          * The buf format can be <subsystem>:<event-name>
845          *  *:<event-name> means any event by that name.
846          *  :<event-name> is the same.
847          *
848          *  <subsystem>:* means all events in that subsystem
849          *  <subsystem>: means the same.
850          *
851          *  <name> (no ':') means all events in a subsystem with
852          *  the name <name> or any event that matches <name>
853          */
854
855         match = strsep(&buf, ":");
856         if (buf) {
857                 sub = match;
858                 event = buf;
859                 match = NULL;
860
861                 if (!strlen(sub) || strcmp(sub, "*") == 0)
862                         sub = NULL;
863                 if (!strlen(event) || strcmp(event, "*") == 0)
864                         event = NULL;
865         }
866
867         ret = __ftrace_set_clr_event(tr, match, sub, event, set);
868
869         /* Put back the colon to allow this to be called again */
870         if (buf)
871                 *(buf - 1) = ':';
872
873         return ret;
874 }
875
876 /**
877  * trace_set_clr_event - enable or disable an event
878  * @system: system name to match (NULL for any system)
879  * @event: event name to match (NULL for all events, within system)
880  * @set: 1 to enable, 0 to disable
881  *
882  * This is a way for other parts of the kernel to enable or disable
883  * event recording.
884  *
885  * Returns 0 on success, -EINVAL if the parameters do not match any
886  * registered events.
887  */
888 int trace_set_clr_event(const char *system, const char *event, int set)
889 {
890         struct trace_array *tr = top_trace_array();
891
892         if (!tr)
893                 return -ENODEV;
894
895         return __ftrace_set_clr_event(tr, NULL, system, event, set);
896 }
897 EXPORT_SYMBOL_GPL(trace_set_clr_event);
898
899 /**
900  * trace_array_set_clr_event - enable or disable an event for a trace array.
901  * @tr: concerned trace array.
902  * @system: system name to match (NULL for any system)
903  * @event: event name to match (NULL for all events, within system)
904  * @enable: true to enable, false to disable
905  *
906  * This is a way for other parts of the kernel to enable or disable
907  * event recording.
908  *
909  * Returns 0 on success, -EINVAL if the parameters do not match any
910  * registered events.
911  */
912 int trace_array_set_clr_event(struct trace_array *tr, const char *system,
913                 const char *event, bool enable)
914 {
915         int set;
916
917         if (!tr)
918                 return -ENOENT;
919
920         set = (enable == true) ? 1 : 0;
921         return __ftrace_set_clr_event(tr, NULL, system, event, set);
922 }
923 EXPORT_SYMBOL_GPL(trace_array_set_clr_event);
924
925 /* 128 should be much more than enough */
926 #define EVENT_BUF_SIZE          127
927
928 static ssize_t
929 ftrace_event_write(struct file *file, const char __user *ubuf,
930                    size_t cnt, loff_t *ppos)
931 {
932         struct trace_parser parser;
933         struct seq_file *m = file->private_data;
934         struct trace_array *tr = m->private;
935         ssize_t read, ret;
936
937         if (!cnt)
938                 return 0;
939
940         ret = tracing_update_buffers();
941         if (ret < 0)
942                 return ret;
943
944         if (trace_parser_get_init(&parser, EVENT_BUF_SIZE + 1))
945                 return -ENOMEM;
946
947         read = trace_get_user(&parser, ubuf, cnt, ppos);
948
949         if (read >= 0 && trace_parser_loaded((&parser))) {
950                 int set = 1;
951
952                 if (*parser.buffer == '!')
953                         set = 0;
954
955                 ret = ftrace_set_clr_event(tr, parser.buffer + !set, set);
956                 if (ret)
957                         goto out_put;
958         }
959
960         ret = read;
961
962  out_put:
963         trace_parser_put(&parser);
964
965         return ret;
966 }
967
968 static void *
969 t_next(struct seq_file *m, void *v, loff_t *pos)
970 {
971         struct trace_event_file *file = v;
972         struct trace_event_call *call;
973         struct trace_array *tr = m->private;
974
975         (*pos)++;
976
977         list_for_each_entry_continue(file, &tr->events, list) {
978                 call = file->event_call;
979                 /*
980                  * The ftrace subsystem is for showing formats only.
981                  * They can not be enabled or disabled via the event files.
982                  */
983                 if (call->class && call->class->reg &&
984                     !(call->flags & TRACE_EVENT_FL_IGNORE_ENABLE))
985                         return file;
986         }
987
988         return NULL;
989 }
990
991 static void *t_start(struct seq_file *m, loff_t *pos)
992 {
993         struct trace_event_file *file;
994         struct trace_array *tr = m->private;
995         loff_t l;
996
997         mutex_lock(&event_mutex);
998
999         file = list_entry(&tr->events, struct trace_event_file, list);
1000         for (l = 0; l <= *pos; ) {
1001                 file = t_next(m, file, &l);
1002                 if (!file)
1003                         break;
1004         }
1005         return file;
1006 }
1007
1008 static void *
1009 s_next(struct seq_file *m, void *v, loff_t *pos)
1010 {
1011         struct trace_event_file *file = v;
1012         struct trace_array *tr = m->private;
1013
1014         (*pos)++;
1015
1016         list_for_each_entry_continue(file, &tr->events, list) {
1017                 if (file->flags & EVENT_FILE_FL_ENABLED)
1018                         return file;
1019         }
1020
1021         return NULL;
1022 }
1023
1024 static void *s_start(struct seq_file *m, loff_t *pos)
1025 {
1026         struct trace_event_file *file;
1027         struct trace_array *tr = m->private;
1028         loff_t l;
1029
1030         mutex_lock(&event_mutex);
1031
1032         file = list_entry(&tr->events, struct trace_event_file, list);
1033         for (l = 0; l <= *pos; ) {
1034                 file = s_next(m, file, &l);
1035                 if (!file)
1036                         break;
1037         }
1038         return file;
1039 }
1040
1041 static int t_show(struct seq_file *m, void *v)
1042 {
1043         struct trace_event_file *file = v;
1044         struct trace_event_call *call = file->event_call;
1045
1046         if (strcmp(call->class->system, TRACE_SYSTEM) != 0)
1047                 seq_printf(m, "%s:", call->class->system);
1048         seq_printf(m, "%s\n", trace_event_name(call));
1049
1050         return 0;
1051 }
1052
1053 static void t_stop(struct seq_file *m, void *p)
1054 {
1055         mutex_unlock(&event_mutex);
1056 }
1057
1058 static void *
1059 __next(struct seq_file *m, void *v, loff_t *pos, int type)
1060 {
1061         struct trace_array *tr = m->private;
1062         struct trace_pid_list *pid_list;
1063
1064         if (type == TRACE_PIDS)
1065                 pid_list = rcu_dereference_sched(tr->filtered_pids);
1066         else
1067                 pid_list = rcu_dereference_sched(tr->filtered_no_pids);
1068
1069         return trace_pid_next(pid_list, v, pos);
1070 }
1071
1072 static void *
1073 p_next(struct seq_file *m, void *v, loff_t *pos)
1074 {
1075         return __next(m, v, pos, TRACE_PIDS);
1076 }
1077
1078 static void *
1079 np_next(struct seq_file *m, void *v, loff_t *pos)
1080 {
1081         return __next(m, v, pos, TRACE_NO_PIDS);
1082 }
1083
1084 static void *__start(struct seq_file *m, loff_t *pos, int type)
1085         __acquires(RCU)
1086 {
1087         struct trace_pid_list *pid_list;
1088         struct trace_array *tr = m->private;
1089
1090         /*
1091          * Grab the mutex, to keep calls to p_next() having the same
1092          * tr->filtered_pids as p_start() has.
1093          * If we just passed the tr->filtered_pids around, then RCU would
1094          * have been enough, but doing that makes things more complex.
1095          */
1096         mutex_lock(&event_mutex);
1097         rcu_read_lock_sched();
1098
1099         if (type == TRACE_PIDS)
1100                 pid_list = rcu_dereference_sched(tr->filtered_pids);
1101         else
1102                 pid_list = rcu_dereference_sched(tr->filtered_no_pids);
1103
1104         if (!pid_list)
1105                 return NULL;
1106
1107         return trace_pid_start(pid_list, pos);
1108 }
1109
1110 static void *p_start(struct seq_file *m, loff_t *pos)
1111         __acquires(RCU)
1112 {
1113         return __start(m, pos, TRACE_PIDS);
1114 }
1115
1116 static void *np_start(struct seq_file *m, loff_t *pos)
1117         __acquires(RCU)
1118 {
1119         return __start(m, pos, TRACE_NO_PIDS);
1120 }
1121
1122 static void p_stop(struct seq_file *m, void *p)
1123         __releases(RCU)
1124 {
1125         rcu_read_unlock_sched();
1126         mutex_unlock(&event_mutex);
1127 }
1128
1129 static ssize_t
1130 event_enable_read(struct file *filp, char __user *ubuf, size_t cnt,
1131                   loff_t *ppos)
1132 {
1133         struct trace_event_file *file;
1134         unsigned long flags;
1135         char buf[4] = "0";
1136
1137         mutex_lock(&event_mutex);
1138         file = event_file_data(filp);
1139         if (likely(file))
1140                 flags = file->flags;
1141         mutex_unlock(&event_mutex);
1142
1143         if (!file)
1144                 return -ENODEV;
1145
1146         if (flags & EVENT_FILE_FL_ENABLED &&
1147             !(flags & EVENT_FILE_FL_SOFT_DISABLED))
1148                 strcpy(buf, "1");
1149
1150         if (flags & EVENT_FILE_FL_SOFT_DISABLED ||
1151             flags & EVENT_FILE_FL_SOFT_MODE)
1152                 strcat(buf, "*");
1153
1154         strcat(buf, "\n");
1155
1156         return simple_read_from_buffer(ubuf, cnt, ppos, buf, strlen(buf));
1157 }
1158
1159 static ssize_t
1160 event_enable_write(struct file *filp, const char __user *ubuf, size_t cnt,
1161                    loff_t *ppos)
1162 {
1163         struct trace_event_file *file;
1164         unsigned long val;
1165         int ret;
1166
1167         ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
1168         if (ret)
1169                 return ret;
1170
1171         ret = tracing_update_buffers();
1172         if (ret < 0)
1173                 return ret;
1174
1175         switch (val) {
1176         case 0:
1177         case 1:
1178                 ret = -ENODEV;
1179                 mutex_lock(&event_mutex);
1180                 file = event_file_data(filp);
1181                 if (likely(file))
1182                         ret = ftrace_event_enable_disable(file, val);
1183                 mutex_unlock(&event_mutex);
1184                 break;
1185
1186         default:
1187                 return -EINVAL;
1188         }
1189
1190         *ppos += cnt;
1191
1192         return ret ? ret : cnt;
1193 }
1194
1195 static ssize_t
1196 system_enable_read(struct file *filp, char __user *ubuf, size_t cnt,
1197                    loff_t *ppos)
1198 {
1199         const char set_to_char[4] = { '?', '0', '1', 'X' };
1200         struct trace_subsystem_dir *dir = filp->private_data;
1201         struct event_subsystem *system = dir->subsystem;
1202         struct trace_event_call *call;
1203         struct trace_event_file *file;
1204         struct trace_array *tr = dir->tr;
1205         char buf[2];
1206         int set = 0;
1207         int ret;
1208
1209         mutex_lock(&event_mutex);
1210         list_for_each_entry(file, &tr->events, list) {
1211                 call = file->event_call;
1212                 if (!trace_event_name(call) || !call->class || !call->class->reg)
1213                         continue;
1214
1215                 if (system && strcmp(call->class->system, system->name) != 0)
1216                         continue;
1217
1218                 /*
1219                  * We need to find out if all the events are set
1220                  * or if all events or cleared, or if we have
1221                  * a mixture.
1222                  */
1223                 set |= (1 << !!(file->flags & EVENT_FILE_FL_ENABLED));
1224
1225                 /*
1226                  * If we have a mixture, no need to look further.
1227                  */
1228                 if (set == 3)
1229                         break;
1230         }
1231         mutex_unlock(&event_mutex);
1232
1233         buf[0] = set_to_char[set];
1234         buf[1] = '\n';
1235
1236         ret = simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
1237
1238         return ret;
1239 }
1240
1241 static ssize_t
1242 system_enable_write(struct file *filp, const char __user *ubuf, size_t cnt,
1243                     loff_t *ppos)
1244 {
1245         struct trace_subsystem_dir *dir = filp->private_data;
1246         struct event_subsystem *system = dir->subsystem;
1247         const char *name = NULL;
1248         unsigned long val;
1249         ssize_t ret;
1250
1251         ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
1252         if (ret)
1253                 return ret;
1254
1255         ret = tracing_update_buffers();
1256         if (ret < 0)
1257                 return ret;
1258
1259         if (val != 0 && val != 1)
1260                 return -EINVAL;
1261
1262         /*
1263          * Opening of "enable" adds a ref count to system,
1264          * so the name is safe to use.
1265          */
1266         if (system)
1267                 name = system->name;
1268
1269         ret = __ftrace_set_clr_event(dir->tr, NULL, name, NULL, val);
1270         if (ret)
1271                 goto out;
1272
1273         ret = cnt;
1274
1275 out:
1276         *ppos += cnt;
1277
1278         return ret;
1279 }
1280
1281 enum {
1282         FORMAT_HEADER           = 1,
1283         FORMAT_FIELD_SEPERATOR  = 2,
1284         FORMAT_PRINTFMT         = 3,
1285 };
1286
1287 static void *f_next(struct seq_file *m, void *v, loff_t *pos)
1288 {
1289         struct trace_event_call *call = event_file_data(m->private);
1290         struct list_head *common_head = &ftrace_common_fields;
1291         struct list_head *head = trace_get_fields(call);
1292         struct list_head *node = v;
1293
1294         (*pos)++;
1295
1296         switch ((unsigned long)v) {
1297         case FORMAT_HEADER:
1298                 node = common_head;
1299                 break;
1300
1301         case FORMAT_FIELD_SEPERATOR:
1302                 node = head;
1303                 break;
1304
1305         case FORMAT_PRINTFMT:
1306                 /* all done */
1307                 return NULL;
1308         }
1309
1310         node = node->prev;
1311         if (node == common_head)
1312                 return (void *)FORMAT_FIELD_SEPERATOR;
1313         else if (node == head)
1314                 return (void *)FORMAT_PRINTFMT;
1315         else
1316                 return node;
1317 }
1318
1319 static int f_show(struct seq_file *m, void *v)
1320 {
1321         struct trace_event_call *call = event_file_data(m->private);
1322         struct ftrace_event_field *field;
1323         const char *array_descriptor;
1324
1325         switch ((unsigned long)v) {
1326         case FORMAT_HEADER:
1327                 seq_printf(m, "name: %s\n", trace_event_name(call));
1328                 seq_printf(m, "ID: %d\n", call->event.type);
1329                 seq_puts(m, "format:\n");
1330                 return 0;
1331
1332         case FORMAT_FIELD_SEPERATOR:
1333                 seq_putc(m, '\n');
1334                 return 0;
1335
1336         case FORMAT_PRINTFMT:
1337                 seq_printf(m, "\nprint fmt: %s\n",
1338                            call->print_fmt);
1339                 return 0;
1340         }
1341
1342         field = list_entry(v, struct ftrace_event_field, link);
1343         /*
1344          * Smartly shows the array type(except dynamic array).
1345          * Normal:
1346          *      field:TYPE VAR
1347          * If TYPE := TYPE[LEN], it is shown:
1348          *      field:TYPE VAR[LEN]
1349          */
1350         array_descriptor = strchr(field->type, '[');
1351
1352         if (str_has_prefix(field->type, "__data_loc"))
1353                 array_descriptor = NULL;
1354
1355         if (!array_descriptor)
1356                 seq_printf(m, "\tfield:%s %s;\toffset:%u;\tsize:%u;\tsigned:%d;\n",
1357                            field->type, field->name, field->offset,
1358                            field->size, !!field->is_signed);
1359         else
1360                 seq_printf(m, "\tfield:%.*s %s%s;\toffset:%u;\tsize:%u;\tsigned:%d;\n",
1361                            (int)(array_descriptor - field->type),
1362                            field->type, field->name,
1363                            array_descriptor, field->offset,
1364                            field->size, !!field->is_signed);
1365
1366         return 0;
1367 }
1368
1369 static void *f_start(struct seq_file *m, loff_t *pos)
1370 {
1371         void *p = (void *)FORMAT_HEADER;
1372         loff_t l = 0;
1373
1374         /* ->stop() is called even if ->start() fails */
1375         mutex_lock(&event_mutex);
1376         if (!event_file_data(m->private))
1377                 return ERR_PTR(-ENODEV);
1378
1379         while (l < *pos && p)
1380                 p = f_next(m, p, &l);
1381
1382         return p;
1383 }
1384
1385 static void f_stop(struct seq_file *m, void *p)
1386 {
1387         mutex_unlock(&event_mutex);
1388 }
1389
1390 static const struct seq_operations trace_format_seq_ops = {
1391         .start          = f_start,
1392         .next           = f_next,
1393         .stop           = f_stop,
1394         .show           = f_show,
1395 };
1396
1397 static int trace_format_open(struct inode *inode, struct file *file)
1398 {
1399         struct seq_file *m;
1400         int ret;
1401
1402         /* Do we want to hide event format files on tracefs lockdown? */
1403
1404         ret = seq_open(file, &trace_format_seq_ops);
1405         if (ret < 0)
1406                 return ret;
1407
1408         m = file->private_data;
1409         m->private = file;
1410
1411         return 0;
1412 }
1413
1414 static ssize_t
1415 event_id_read(struct file *filp, char __user *ubuf, size_t cnt, loff_t *ppos)
1416 {
1417         int id = (long)event_file_data(filp);
1418         char buf[32];
1419         int len;
1420
1421         if (unlikely(!id))
1422                 return -ENODEV;
1423
1424         len = sprintf(buf, "%d\n", id);
1425
1426         return simple_read_from_buffer(ubuf, cnt, ppos, buf, len);
1427 }
1428
1429 static ssize_t
1430 event_filter_read(struct file *filp, char __user *ubuf, size_t cnt,
1431                   loff_t *ppos)
1432 {
1433         struct trace_event_file *file;
1434         struct trace_seq *s;
1435         int r = -ENODEV;
1436
1437         if (*ppos)
1438                 return 0;
1439
1440         s = kmalloc(sizeof(*s), GFP_KERNEL);
1441
1442         if (!s)
1443                 return -ENOMEM;
1444
1445         trace_seq_init(s);
1446
1447         mutex_lock(&event_mutex);
1448         file = event_file_data(filp);
1449         if (file)
1450                 print_event_filter(file, s);
1451         mutex_unlock(&event_mutex);
1452
1453         if (file)
1454                 r = simple_read_from_buffer(ubuf, cnt, ppos,
1455                                             s->buffer, trace_seq_used(s));
1456
1457         kfree(s);
1458
1459         return r;
1460 }
1461
1462 static ssize_t
1463 event_filter_write(struct file *filp, const char __user *ubuf, size_t cnt,
1464                    loff_t *ppos)
1465 {
1466         struct trace_event_file *file;
1467         char *buf;
1468         int err = -ENODEV;
1469
1470         if (cnt >= PAGE_SIZE)
1471                 return -EINVAL;
1472
1473         buf = memdup_user_nul(ubuf, cnt);
1474         if (IS_ERR(buf))
1475                 return PTR_ERR(buf);
1476
1477         mutex_lock(&event_mutex);
1478         file = event_file_data(filp);
1479         if (file)
1480                 err = apply_event_filter(file, buf);
1481         mutex_unlock(&event_mutex);
1482
1483         kfree(buf);
1484         if (err < 0)
1485                 return err;
1486
1487         *ppos += cnt;
1488
1489         return cnt;
1490 }
1491
1492 static LIST_HEAD(event_subsystems);
1493
1494 static int subsystem_open(struct inode *inode, struct file *filp)
1495 {
1496         struct event_subsystem *system = NULL;
1497         struct trace_subsystem_dir *dir = NULL; /* Initialize for gcc */
1498         struct trace_array *tr;
1499         int ret;
1500
1501         if (tracing_is_disabled())
1502                 return -ENODEV;
1503
1504         /* Make sure the system still exists */
1505         mutex_lock(&event_mutex);
1506         mutex_lock(&trace_types_lock);
1507         list_for_each_entry(tr, &ftrace_trace_arrays, list) {
1508                 list_for_each_entry(dir, &tr->systems, list) {
1509                         if (dir == inode->i_private) {
1510                                 /* Don't open systems with no events */
1511                                 if (dir->nr_events) {
1512                                         __get_system_dir(dir);
1513                                         system = dir->subsystem;
1514                                 }
1515                                 goto exit_loop;
1516                         }
1517                 }
1518         }
1519  exit_loop:
1520         mutex_unlock(&trace_types_lock);
1521         mutex_unlock(&event_mutex);
1522
1523         if (!system)
1524                 return -ENODEV;
1525
1526         /* Some versions of gcc think dir can be uninitialized here */
1527         WARN_ON(!dir);
1528
1529         /* Still need to increment the ref count of the system */
1530         if (trace_array_get(tr) < 0) {
1531                 put_system(dir);
1532                 return -ENODEV;
1533         }
1534
1535         ret = tracing_open_generic(inode, filp);
1536         if (ret < 0) {
1537                 trace_array_put(tr);
1538                 put_system(dir);
1539         }
1540
1541         return ret;
1542 }
1543
1544 static int system_tr_open(struct inode *inode, struct file *filp)
1545 {
1546         struct trace_subsystem_dir *dir;
1547         struct trace_array *tr = inode->i_private;
1548         int ret;
1549
1550         /* Make a temporary dir that has no system but points to tr */
1551         dir = kzalloc(sizeof(*dir), GFP_KERNEL);
1552         if (!dir)
1553                 return -ENOMEM;
1554
1555         ret = tracing_open_generic_tr(inode, filp);
1556         if (ret < 0) {
1557                 kfree(dir);
1558                 return ret;
1559         }
1560         dir->tr = tr;
1561         filp->private_data = dir;
1562
1563         return 0;
1564 }
1565
1566 static int subsystem_release(struct inode *inode, struct file *file)
1567 {
1568         struct trace_subsystem_dir *dir = file->private_data;
1569
1570         trace_array_put(dir->tr);
1571
1572         /*
1573          * If dir->subsystem is NULL, then this is a temporary
1574          * descriptor that was made for a trace_array to enable
1575          * all subsystems.
1576          */
1577         if (dir->subsystem)
1578                 put_system(dir);
1579         else
1580                 kfree(dir);
1581
1582         return 0;
1583 }
1584
1585 static ssize_t
1586 subsystem_filter_read(struct file *filp, char __user *ubuf, size_t cnt,
1587                       loff_t *ppos)
1588 {
1589         struct trace_subsystem_dir *dir = filp->private_data;
1590         struct event_subsystem *system = dir->subsystem;
1591         struct trace_seq *s;
1592         int r;
1593
1594         if (*ppos)
1595                 return 0;
1596
1597         s = kmalloc(sizeof(*s), GFP_KERNEL);
1598         if (!s)
1599                 return -ENOMEM;
1600
1601         trace_seq_init(s);
1602
1603         print_subsystem_event_filter(system, s);
1604         r = simple_read_from_buffer(ubuf, cnt, ppos,
1605                                     s->buffer, trace_seq_used(s));
1606
1607         kfree(s);
1608
1609         return r;
1610 }
1611
1612 static ssize_t
1613 subsystem_filter_write(struct file *filp, const char __user *ubuf, size_t cnt,
1614                        loff_t *ppos)
1615 {
1616         struct trace_subsystem_dir *dir = filp->private_data;
1617         char *buf;
1618         int err;
1619
1620         if (cnt >= PAGE_SIZE)
1621                 return -EINVAL;
1622
1623         buf = memdup_user_nul(ubuf, cnt);
1624         if (IS_ERR(buf))
1625                 return PTR_ERR(buf);
1626
1627         err = apply_subsystem_event_filter(dir, buf);
1628         kfree(buf);
1629         if (err < 0)
1630                 return err;
1631
1632         *ppos += cnt;
1633
1634         return cnt;
1635 }
1636
1637 static ssize_t
1638 show_header(struct file *filp, char __user *ubuf, size_t cnt, loff_t *ppos)
1639 {
1640         int (*func)(struct trace_seq *s) = filp->private_data;
1641         struct trace_seq *s;
1642         int r;
1643
1644         if (*ppos)
1645                 return 0;
1646
1647         s = kmalloc(sizeof(*s), GFP_KERNEL);
1648         if (!s)
1649                 return -ENOMEM;
1650
1651         trace_seq_init(s);
1652
1653         func(s);
1654         r = simple_read_from_buffer(ubuf, cnt, ppos,
1655                                     s->buffer, trace_seq_used(s));
1656
1657         kfree(s);
1658
1659         return r;
1660 }
1661
1662 static void ignore_task_cpu(void *data)
1663 {
1664         struct trace_array *tr = data;
1665         struct trace_pid_list *pid_list;
1666         struct trace_pid_list *no_pid_list;
1667
1668         /*
1669          * This function is called by on_each_cpu() while the
1670          * event_mutex is held.
1671          */
1672         pid_list = rcu_dereference_protected(tr->filtered_pids,
1673                                              mutex_is_locked(&event_mutex));
1674         no_pid_list = rcu_dereference_protected(tr->filtered_no_pids,
1675                                              mutex_is_locked(&event_mutex));
1676
1677         this_cpu_write(tr->array_buffer.data->ignore_pid,
1678                        trace_ignore_this_task(pid_list, no_pid_list, current));
1679 }
1680
1681 static void register_pid_events(struct trace_array *tr)
1682 {
1683         /*
1684          * Register a probe that is called before all other probes
1685          * to set ignore_pid if next or prev do not match.
1686          * Register a probe this is called after all other probes
1687          * to only keep ignore_pid set if next pid matches.
1688          */
1689         register_trace_prio_sched_switch(event_filter_pid_sched_switch_probe_pre,
1690                                          tr, INT_MAX);
1691         register_trace_prio_sched_switch(event_filter_pid_sched_switch_probe_post,
1692                                          tr, 0);
1693
1694         register_trace_prio_sched_wakeup(event_filter_pid_sched_wakeup_probe_pre,
1695                                          tr, INT_MAX);
1696         register_trace_prio_sched_wakeup(event_filter_pid_sched_wakeup_probe_post,
1697                                          tr, 0);
1698
1699         register_trace_prio_sched_wakeup_new(event_filter_pid_sched_wakeup_probe_pre,
1700                                              tr, INT_MAX);
1701         register_trace_prio_sched_wakeup_new(event_filter_pid_sched_wakeup_probe_post,
1702                                              tr, 0);
1703
1704         register_trace_prio_sched_waking(event_filter_pid_sched_wakeup_probe_pre,
1705                                          tr, INT_MAX);
1706         register_trace_prio_sched_waking(event_filter_pid_sched_wakeup_probe_post,
1707                                          tr, 0);
1708 }
1709
1710 static ssize_t
1711 event_pid_write(struct file *filp, const char __user *ubuf,
1712                 size_t cnt, loff_t *ppos, int type)
1713 {
1714         struct seq_file *m = filp->private_data;
1715         struct trace_array *tr = m->private;
1716         struct trace_pid_list *filtered_pids = NULL;
1717         struct trace_pid_list *other_pids = NULL;
1718         struct trace_pid_list *pid_list;
1719         struct trace_event_file *file;
1720         ssize_t ret;
1721
1722         if (!cnt)
1723                 return 0;
1724
1725         ret = tracing_update_buffers();
1726         if (ret < 0)
1727                 return ret;
1728
1729         mutex_lock(&event_mutex);
1730
1731         if (type == TRACE_PIDS) {
1732                 filtered_pids = rcu_dereference_protected(tr->filtered_pids,
1733                                                           lockdep_is_held(&event_mutex));
1734                 other_pids = rcu_dereference_protected(tr->filtered_no_pids,
1735                                                           lockdep_is_held(&event_mutex));
1736         } else {
1737                 filtered_pids = rcu_dereference_protected(tr->filtered_no_pids,
1738                                                           lockdep_is_held(&event_mutex));
1739                 other_pids = rcu_dereference_protected(tr->filtered_pids,
1740                                                           lockdep_is_held(&event_mutex));
1741         }
1742
1743         ret = trace_pid_write(filtered_pids, &pid_list, ubuf, cnt);
1744         if (ret < 0)
1745                 goto out;
1746
1747         if (type == TRACE_PIDS)
1748                 rcu_assign_pointer(tr->filtered_pids, pid_list);
1749         else
1750                 rcu_assign_pointer(tr->filtered_no_pids, pid_list);
1751
1752         list_for_each_entry(file, &tr->events, list) {
1753                 set_bit(EVENT_FILE_FL_PID_FILTER_BIT, &file->flags);
1754         }
1755
1756         if (filtered_pids) {
1757                 tracepoint_synchronize_unregister();
1758                 trace_free_pid_list(filtered_pids);
1759         } else if (pid_list && !other_pids) {
1760                 register_pid_events(tr);
1761         }
1762
1763         /*
1764          * Ignoring of pids is done at task switch. But we have to
1765          * check for those tasks that are currently running.
1766          * Always do this in case a pid was appended or removed.
1767          */
1768         on_each_cpu(ignore_task_cpu, tr, 1);
1769
1770  out:
1771         mutex_unlock(&event_mutex);
1772
1773         if (ret > 0)
1774                 *ppos += ret;
1775
1776         return ret;
1777 }
1778
1779 static ssize_t
1780 ftrace_event_pid_write(struct file *filp, const char __user *ubuf,
1781                        size_t cnt, loff_t *ppos)
1782 {
1783         return event_pid_write(filp, ubuf, cnt, ppos, TRACE_PIDS);
1784 }
1785
1786 static ssize_t
1787 ftrace_event_npid_write(struct file *filp, const char __user *ubuf,
1788                         size_t cnt, loff_t *ppos)
1789 {
1790         return event_pid_write(filp, ubuf, cnt, ppos, TRACE_NO_PIDS);
1791 }
1792
1793 static int ftrace_event_avail_open(struct inode *inode, struct file *file);
1794 static int ftrace_event_set_open(struct inode *inode, struct file *file);
1795 static int ftrace_event_set_pid_open(struct inode *inode, struct file *file);
1796 static int ftrace_event_set_npid_open(struct inode *inode, struct file *file);
1797 static int ftrace_event_release(struct inode *inode, struct file *file);
1798
1799 static const struct seq_operations show_event_seq_ops = {
1800         .start = t_start,
1801         .next = t_next,
1802         .show = t_show,
1803         .stop = t_stop,
1804 };
1805
1806 static const struct seq_operations show_set_event_seq_ops = {
1807         .start = s_start,
1808         .next = s_next,
1809         .show = t_show,
1810         .stop = t_stop,
1811 };
1812
1813 static const struct seq_operations show_set_pid_seq_ops = {
1814         .start = p_start,
1815         .next = p_next,
1816         .show = trace_pid_show,
1817         .stop = p_stop,
1818 };
1819
1820 static const struct seq_operations show_set_no_pid_seq_ops = {
1821         .start = np_start,
1822         .next = np_next,
1823         .show = trace_pid_show,
1824         .stop = p_stop,
1825 };
1826
1827 static const struct file_operations ftrace_avail_fops = {
1828         .open = ftrace_event_avail_open,
1829         .read = seq_read,
1830         .llseek = seq_lseek,
1831         .release = seq_release,
1832 };
1833
1834 static const struct file_operations ftrace_set_event_fops = {
1835         .open = ftrace_event_set_open,
1836         .read = seq_read,
1837         .write = ftrace_event_write,
1838         .llseek = seq_lseek,
1839         .release = ftrace_event_release,
1840 };
1841
1842 static const struct file_operations ftrace_set_event_pid_fops = {
1843         .open = ftrace_event_set_pid_open,
1844         .read = seq_read,
1845         .write = ftrace_event_pid_write,
1846         .llseek = seq_lseek,
1847         .release = ftrace_event_release,
1848 };
1849
1850 static const struct file_operations ftrace_set_event_notrace_pid_fops = {
1851         .open = ftrace_event_set_npid_open,
1852         .read = seq_read,
1853         .write = ftrace_event_npid_write,
1854         .llseek = seq_lseek,
1855         .release = ftrace_event_release,
1856 };
1857
1858 static const struct file_operations ftrace_enable_fops = {
1859         .open = tracing_open_generic,
1860         .read = event_enable_read,
1861         .write = event_enable_write,
1862         .llseek = default_llseek,
1863 };
1864
1865 static const struct file_operations ftrace_event_format_fops = {
1866         .open = trace_format_open,
1867         .read = seq_read,
1868         .llseek = seq_lseek,
1869         .release = seq_release,
1870 };
1871
1872 static const struct file_operations ftrace_event_id_fops = {
1873         .read = event_id_read,
1874         .llseek = default_llseek,
1875 };
1876
1877 static const struct file_operations ftrace_event_filter_fops = {
1878         .open = tracing_open_generic,
1879         .read = event_filter_read,
1880         .write = event_filter_write,
1881         .llseek = default_llseek,
1882 };
1883
1884 static const struct file_operations ftrace_subsystem_filter_fops = {
1885         .open = subsystem_open,
1886         .read = subsystem_filter_read,
1887         .write = subsystem_filter_write,
1888         .llseek = default_llseek,
1889         .release = subsystem_release,
1890 };
1891
1892 static const struct file_operations ftrace_system_enable_fops = {
1893         .open = subsystem_open,
1894         .read = system_enable_read,
1895         .write = system_enable_write,
1896         .llseek = default_llseek,
1897         .release = subsystem_release,
1898 };
1899
1900 static const struct file_operations ftrace_tr_enable_fops = {
1901         .open = system_tr_open,
1902         .read = system_enable_read,
1903         .write = system_enable_write,
1904         .llseek = default_llseek,
1905         .release = subsystem_release,
1906 };
1907
1908 static const struct file_operations ftrace_show_header_fops = {
1909         .open = tracing_open_generic,
1910         .read = show_header,
1911         .llseek = default_llseek,
1912 };
1913
1914 static int
1915 ftrace_event_open(struct inode *inode, struct file *file,
1916                   const struct seq_operations *seq_ops)
1917 {
1918         struct seq_file *m;
1919         int ret;
1920
1921         ret = security_locked_down(LOCKDOWN_TRACEFS);
1922         if (ret)
1923                 return ret;
1924
1925         ret = seq_open(file, seq_ops);
1926         if (ret < 0)
1927                 return ret;
1928         m = file->private_data;
1929         /* copy tr over to seq ops */
1930         m->private = inode->i_private;
1931
1932         return ret;
1933 }
1934
1935 static int ftrace_event_release(struct inode *inode, struct file *file)
1936 {
1937         struct trace_array *tr = inode->i_private;
1938
1939         trace_array_put(tr);
1940
1941         return seq_release(inode, file);
1942 }
1943
1944 static int
1945 ftrace_event_avail_open(struct inode *inode, struct file *file)
1946 {
1947         const struct seq_operations *seq_ops = &show_event_seq_ops;
1948
1949         /* Checks for tracefs lockdown */
1950         return ftrace_event_open(inode, file, seq_ops);
1951 }
1952
1953 static int
1954 ftrace_event_set_open(struct inode *inode, struct file *file)
1955 {
1956         const struct seq_operations *seq_ops = &show_set_event_seq_ops;
1957         struct trace_array *tr = inode->i_private;
1958         int ret;
1959
1960         ret = tracing_check_open_get_tr(tr);
1961         if (ret)
1962                 return ret;
1963
1964         if ((file->f_mode & FMODE_WRITE) &&
1965             (file->f_flags & O_TRUNC))
1966                 ftrace_clear_events(tr);
1967
1968         ret = ftrace_event_open(inode, file, seq_ops);
1969         if (ret < 0)
1970                 trace_array_put(tr);
1971         return ret;
1972 }
1973
1974 static int
1975 ftrace_event_set_pid_open(struct inode *inode, struct file *file)
1976 {
1977         const struct seq_operations *seq_ops = &show_set_pid_seq_ops;
1978         struct trace_array *tr = inode->i_private;
1979         int ret;
1980
1981         ret = tracing_check_open_get_tr(tr);
1982         if (ret)
1983                 return ret;
1984
1985         if ((file->f_mode & FMODE_WRITE) &&
1986             (file->f_flags & O_TRUNC))
1987                 ftrace_clear_event_pids(tr, TRACE_PIDS);
1988
1989         ret = ftrace_event_open(inode, file, seq_ops);
1990         if (ret < 0)
1991                 trace_array_put(tr);
1992         return ret;
1993 }
1994
1995 static int
1996 ftrace_event_set_npid_open(struct inode *inode, struct file *file)
1997 {
1998         const struct seq_operations *seq_ops = &show_set_no_pid_seq_ops;
1999         struct trace_array *tr = inode->i_private;
2000         int ret;
2001
2002         ret = tracing_check_open_get_tr(tr);
2003         if (ret)
2004                 return ret;
2005
2006         if ((file->f_mode & FMODE_WRITE) &&
2007             (file->f_flags & O_TRUNC))
2008                 ftrace_clear_event_pids(tr, TRACE_NO_PIDS);
2009
2010         ret = ftrace_event_open(inode, file, seq_ops);
2011         if (ret < 0)
2012                 trace_array_put(tr);
2013         return ret;
2014 }
2015
2016 static struct event_subsystem *
2017 create_new_subsystem(const char *name)
2018 {
2019         struct event_subsystem *system;
2020
2021         /* need to create new entry */
2022         system = kmalloc(sizeof(*system), GFP_KERNEL);
2023         if (!system)
2024                 return NULL;
2025
2026         system->ref_count = 1;
2027
2028         /* Only allocate if dynamic (kprobes and modules) */
2029         system->name = kstrdup_const(name, GFP_KERNEL);
2030         if (!system->name)
2031                 goto out_free;
2032
2033         system->filter = NULL;
2034
2035         system->filter = kzalloc(sizeof(struct event_filter), GFP_KERNEL);
2036         if (!system->filter)
2037                 goto out_free;
2038
2039         list_add(&system->list, &event_subsystems);
2040
2041         return system;
2042
2043  out_free:
2044         kfree_const(system->name);
2045         kfree(system);
2046         return NULL;
2047 }
2048
2049 static struct dentry *
2050 event_subsystem_dir(struct trace_array *tr, const char *name,
2051                     struct trace_event_file *file, struct dentry *parent)
2052 {
2053         struct trace_subsystem_dir *dir;
2054         struct event_subsystem *system;
2055         struct dentry *entry;
2056
2057         /* First see if we did not already create this dir */
2058         list_for_each_entry(dir, &tr->systems, list) {
2059                 system = dir->subsystem;
2060                 if (strcmp(system->name, name) == 0) {
2061                         dir->nr_events++;
2062                         file->system = dir;
2063                         return dir->entry;
2064                 }
2065         }
2066
2067         /* Now see if the system itself exists. */
2068         list_for_each_entry(system, &event_subsystems, list) {
2069                 if (strcmp(system->name, name) == 0)
2070                         break;
2071         }
2072         /* Reset system variable when not found */
2073         if (&system->list == &event_subsystems)
2074                 system = NULL;
2075
2076         dir = kmalloc(sizeof(*dir), GFP_KERNEL);
2077         if (!dir)
2078                 goto out_fail;
2079
2080         if (!system) {
2081                 system = create_new_subsystem(name);
2082                 if (!system)
2083                         goto out_free;
2084         } else
2085                 __get_system(system);
2086
2087         dir->entry = tracefs_create_dir(name, parent);
2088         if (!dir->entry) {
2089                 pr_warn("Failed to create system directory %s\n", name);
2090                 __put_system(system);
2091                 goto out_free;
2092         }
2093
2094         dir->tr = tr;
2095         dir->ref_count = 1;
2096         dir->nr_events = 1;
2097         dir->subsystem = system;
2098         file->system = dir;
2099
2100         /* the ftrace system is special, do not create enable or filter files */
2101         if (strcmp(name, "ftrace") != 0) {
2102
2103                 entry = tracefs_create_file("filter", 0644, dir->entry, dir,
2104                                             &ftrace_subsystem_filter_fops);
2105                 if (!entry) {
2106                         kfree(system->filter);
2107                         system->filter = NULL;
2108                         pr_warn("Could not create tracefs '%s/filter' entry\n", name);
2109                 }
2110
2111                 trace_create_file("enable", 0644, dir->entry, dir,
2112                                   &ftrace_system_enable_fops);
2113         }
2114
2115         list_add(&dir->list, &tr->systems);
2116
2117         return dir->entry;
2118
2119  out_free:
2120         kfree(dir);
2121  out_fail:
2122         /* Only print this message if failed on memory allocation */
2123         if (!dir || !system)
2124                 pr_warn("No memory to create event subsystem %s\n", name);
2125         return NULL;
2126 }
2127
2128 static int
2129 event_define_fields(struct trace_event_call *call)
2130 {
2131         struct list_head *head;
2132         int ret = 0;
2133
2134         /*
2135          * Other events may have the same class. Only update
2136          * the fields if they are not already defined.
2137          */
2138         head = trace_get_fields(call);
2139         if (list_empty(head)) {
2140                 struct trace_event_fields *field = call->class->fields_array;
2141                 unsigned int offset = sizeof(struct trace_entry);
2142
2143                 for (; field->type; field++) {
2144                         if (field->type == TRACE_FUNCTION_TYPE) {
2145                                 field->define_fields(call);
2146                                 break;
2147                         }
2148
2149                         offset = ALIGN(offset, field->align);
2150                         ret = trace_define_field(call, field->type, field->name,
2151                                                  offset, field->size,
2152                                                  field->is_signed, field->filter_type);
2153                         if (WARN_ON_ONCE(ret)) {
2154                                 pr_err("error code is %d\n", ret);
2155                                 break;
2156                         }
2157
2158                         offset += field->size;
2159                 }
2160         }
2161
2162         return ret;
2163 }
2164
2165 static int
2166 event_create_dir(struct dentry *parent, struct trace_event_file *file)
2167 {
2168         struct trace_event_call *call = file->event_call;
2169         struct trace_array *tr = file->tr;
2170         struct dentry *d_events;
2171         const char *name;
2172         int ret;
2173
2174         /*
2175          * If the trace point header did not define TRACE_SYSTEM
2176          * then the system would be called "TRACE_SYSTEM".
2177          */
2178         if (strcmp(call->class->system, TRACE_SYSTEM) != 0) {
2179                 d_events = event_subsystem_dir(tr, call->class->system, file, parent);
2180                 if (!d_events)
2181                         return -ENOMEM;
2182         } else
2183                 d_events = parent;
2184
2185         name = trace_event_name(call);
2186         file->dir = tracefs_create_dir(name, d_events);
2187         if (!file->dir) {
2188                 pr_warn("Could not create tracefs '%s' directory\n", name);
2189                 return -1;
2190         }
2191
2192         if (call->class->reg && !(call->flags & TRACE_EVENT_FL_IGNORE_ENABLE))
2193                 trace_create_file("enable", 0644, file->dir, file,
2194                                   &ftrace_enable_fops);
2195
2196 #ifdef CONFIG_PERF_EVENTS
2197         if (call->event.type && call->class->reg)
2198                 trace_create_file("id", 0444, file->dir,
2199                                   (void *)(long)call->event.type,
2200                                   &ftrace_event_id_fops);
2201 #endif
2202
2203         ret = event_define_fields(call);
2204         if (ret < 0) {
2205                 pr_warn("Could not initialize trace point events/%s\n", name);
2206                 return ret;
2207         }
2208
2209         /*
2210          * Only event directories that can be enabled should have
2211          * triggers or filters.
2212          */
2213         if (!(call->flags & TRACE_EVENT_FL_IGNORE_ENABLE)) {
2214                 trace_create_file("filter", 0644, file->dir, file,
2215                                   &ftrace_event_filter_fops);
2216
2217                 trace_create_file("trigger", 0644, file->dir, file,
2218                                   &event_trigger_fops);
2219         }
2220
2221 #ifdef CONFIG_HIST_TRIGGERS
2222         trace_create_file("hist", 0444, file->dir, file,
2223                           &event_hist_fops);
2224 #endif
2225 #ifdef CONFIG_HIST_TRIGGERS_DEBUG
2226         trace_create_file("hist_debug", 0444, file->dir, file,
2227                           &event_hist_debug_fops);
2228 #endif
2229         trace_create_file("format", 0444, file->dir, call,
2230                           &ftrace_event_format_fops);
2231
2232 #ifdef CONFIG_TRACE_EVENT_INJECT
2233         if (call->event.type && call->class->reg)
2234                 trace_create_file("inject", 0200, file->dir, file,
2235                                   &event_inject_fops);
2236 #endif
2237
2238         return 0;
2239 }
2240
2241 static void remove_event_from_tracers(struct trace_event_call *call)
2242 {
2243         struct trace_event_file *file;
2244         struct trace_array *tr;
2245
2246         do_for_each_event_file_safe(tr, file) {
2247                 if (file->event_call != call)
2248                         continue;
2249
2250                 remove_event_file_dir(file);
2251                 /*
2252                  * The do_for_each_event_file_safe() is
2253                  * a double loop. After finding the call for this
2254                  * trace_array, we use break to jump to the next
2255                  * trace_array.
2256                  */
2257                 break;
2258         } while_for_each_event_file();
2259 }
2260
2261 static void event_remove(struct trace_event_call *call)
2262 {
2263         struct trace_array *tr;
2264         struct trace_event_file *file;
2265
2266         do_for_each_event_file(tr, file) {
2267                 if (file->event_call != call)
2268                         continue;
2269
2270                 if (file->flags & EVENT_FILE_FL_WAS_ENABLED)
2271                         tr->clear_trace = true;
2272
2273                 ftrace_event_enable_disable(file, 0);
2274                 /*
2275                  * The do_for_each_event_file() is
2276                  * a double loop. After finding the call for this
2277                  * trace_array, we use break to jump to the next
2278                  * trace_array.
2279                  */
2280                 break;
2281         } while_for_each_event_file();
2282
2283         if (call->event.funcs)
2284                 __unregister_trace_event(&call->event);
2285         remove_event_from_tracers(call);
2286         list_del(&call->list);
2287 }
2288
2289 static int event_init(struct trace_event_call *call)
2290 {
2291         int ret = 0;
2292         const char *name;
2293
2294         name = trace_event_name(call);
2295         if (WARN_ON(!name))
2296                 return -EINVAL;
2297
2298         if (call->class->raw_init) {
2299                 ret = call->class->raw_init(call);
2300                 if (ret < 0 && ret != -ENOSYS)
2301                         pr_warn("Could not initialize trace events/%s\n", name);
2302         }
2303
2304         return ret;
2305 }
2306
2307 static int
2308 __register_event(struct trace_event_call *call, struct module *mod)
2309 {
2310         int ret;
2311
2312         ret = event_init(call);
2313         if (ret < 0)
2314                 return ret;
2315
2316         list_add(&call->list, &ftrace_events);
2317         call->mod = mod;
2318
2319         return 0;
2320 }
2321
2322 static char *eval_replace(char *ptr, struct trace_eval_map *map, int len)
2323 {
2324         int rlen;
2325         int elen;
2326
2327         /* Find the length of the eval value as a string */
2328         elen = snprintf(ptr, 0, "%ld", map->eval_value);
2329         /* Make sure there's enough room to replace the string with the value */
2330         if (len < elen)
2331                 return NULL;
2332
2333         snprintf(ptr, elen + 1, "%ld", map->eval_value);
2334
2335         /* Get the rest of the string of ptr */
2336         rlen = strlen(ptr + len);
2337         memmove(ptr + elen, ptr + len, rlen);
2338         /* Make sure we end the new string */
2339         ptr[elen + rlen] = 0;
2340
2341         return ptr + elen;
2342 }
2343
2344 static void update_event_printk(struct trace_event_call *call,
2345                                 struct trace_eval_map *map)
2346 {
2347         char *ptr;
2348         int quote = 0;
2349         int len = strlen(map->eval_string);
2350
2351         for (ptr = call->print_fmt; *ptr; ptr++) {
2352                 if (*ptr == '\\') {
2353                         ptr++;
2354                         /* paranoid */
2355                         if (!*ptr)
2356                                 break;
2357                         continue;
2358                 }
2359                 if (*ptr == '"') {
2360                         quote ^= 1;
2361                         continue;
2362                 }
2363                 if (quote)
2364                         continue;
2365                 if (isdigit(*ptr)) {
2366                         /* skip numbers */
2367                         do {
2368                                 ptr++;
2369                                 /* Check for alpha chars like ULL */
2370                         } while (isalnum(*ptr));
2371                         if (!*ptr)
2372                                 break;
2373                         /*
2374                          * A number must have some kind of delimiter after
2375                          * it, and we can ignore that too.
2376                          */
2377                         continue;
2378                 }
2379                 if (isalpha(*ptr) || *ptr == '_') {
2380                         if (strncmp(map->eval_string, ptr, len) == 0 &&
2381                             !isalnum(ptr[len]) && ptr[len] != '_') {
2382                                 ptr = eval_replace(ptr, map, len);
2383                                 /* enum/sizeof string smaller than value */
2384                                 if (WARN_ON_ONCE(!ptr))
2385                                         return;
2386                                 /*
2387                                  * No need to decrement here, as eval_replace()
2388                                  * returns the pointer to the character passed
2389                                  * the eval, and two evals can not be placed
2390                                  * back to back without something in between.
2391                                  * We can skip that something in between.
2392                                  */
2393                                 continue;
2394                         }
2395                 skip_more:
2396                         do {
2397                                 ptr++;
2398                         } while (isalnum(*ptr) || *ptr == '_');
2399                         if (!*ptr)
2400                                 break;
2401                         /*
2402                          * If what comes after this variable is a '.' or
2403                          * '->' then we can continue to ignore that string.
2404                          */
2405                         if (*ptr == '.' || (ptr[0] == '-' && ptr[1] == '>')) {
2406                                 ptr += *ptr == '.' ? 1 : 2;
2407                                 if (!*ptr)
2408                                         break;
2409                                 goto skip_more;
2410                         }
2411                         /*
2412                          * Once again, we can skip the delimiter that came
2413                          * after the string.
2414                          */
2415                         continue;
2416                 }
2417         }
2418 }
2419
2420 void trace_event_eval_update(struct trace_eval_map **map, int len)
2421 {
2422         struct trace_event_call *call, *p;
2423         const char *last_system = NULL;
2424         bool first = false;
2425         int last_i;
2426         int i;
2427
2428         down_write(&trace_event_sem);
2429         list_for_each_entry_safe(call, p, &ftrace_events, list) {
2430                 /* events are usually grouped together with systems */
2431                 if (!last_system || call->class->system != last_system) {
2432                         first = true;
2433                         last_i = 0;
2434                         last_system = call->class->system;
2435                 }
2436
2437                 /*
2438                  * Since calls are grouped by systems, the likelyhood that the
2439                  * next call in the iteration belongs to the same system as the
2440                  * previous call is high. As an optimization, we skip searching
2441                  * for a map[] that matches the call's system if the last call
2442                  * was from the same system. That's what last_i is for. If the
2443                  * call has the same system as the previous call, then last_i
2444                  * will be the index of the first map[] that has a matching
2445                  * system.
2446                  */
2447                 for (i = last_i; i < len; i++) {
2448                         if (call->class->system == map[i]->system) {
2449                                 /* Save the first system if need be */
2450                                 if (first) {
2451                                         last_i = i;
2452                                         first = false;
2453                                 }
2454                                 update_event_printk(call, map[i]);
2455                         }
2456                 }
2457         }
2458         up_write(&trace_event_sem);
2459 }
2460
2461 static struct trace_event_file *
2462 trace_create_new_event(struct trace_event_call *call,
2463                        struct trace_array *tr)
2464 {
2465         struct trace_event_file *file;
2466
2467         file = kmem_cache_alloc(file_cachep, GFP_TRACE);
2468         if (!file)
2469                 return NULL;
2470
2471         file->event_call = call;
2472         file->tr = tr;
2473         atomic_set(&file->sm_ref, 0);
2474         atomic_set(&file->tm_ref, 0);
2475         INIT_LIST_HEAD(&file->triggers);
2476         list_add(&file->list, &tr->events);
2477
2478         return file;
2479 }
2480
2481 /* Add an event to a trace directory */
2482 static int
2483 __trace_add_new_event(struct trace_event_call *call, struct trace_array *tr)
2484 {
2485         struct trace_event_file *file;
2486
2487         file = trace_create_new_event(call, tr);
2488         if (!file)
2489                 return -ENOMEM;
2490
2491         if (eventdir_initialized)
2492                 return event_create_dir(tr->event_dir, file);
2493         else
2494                 return event_define_fields(call);
2495 }
2496
2497 /*
2498  * Just create a decriptor for early init. A descriptor is required
2499  * for enabling events at boot. We want to enable events before
2500  * the filesystem is initialized.
2501  */
2502 static int
2503 __trace_early_add_new_event(struct trace_event_call *call,
2504                             struct trace_array *tr)
2505 {
2506         struct trace_event_file *file;
2507
2508         file = trace_create_new_event(call, tr);
2509         if (!file)
2510                 return -ENOMEM;
2511
2512         return event_define_fields(call);
2513 }
2514
2515 struct ftrace_module_file_ops;
2516 static void __add_event_to_tracers(struct trace_event_call *call);
2517
2518 /* Add an additional event_call dynamically */
2519 int trace_add_event_call(struct trace_event_call *call)
2520 {
2521         int ret;
2522         lockdep_assert_held(&event_mutex);
2523
2524         mutex_lock(&trace_types_lock);
2525
2526         ret = __register_event(call, NULL);
2527         if (ret >= 0)
2528                 __add_event_to_tracers(call);
2529
2530         mutex_unlock(&trace_types_lock);
2531         return ret;
2532 }
2533
2534 /*
2535  * Must be called under locking of trace_types_lock, event_mutex and
2536  * trace_event_sem.
2537  */
2538 static void __trace_remove_event_call(struct trace_event_call *call)
2539 {
2540         event_remove(call);
2541         trace_destroy_fields(call);
2542         free_event_filter(call->filter);
2543         call->filter = NULL;
2544 }
2545
2546 static int probe_remove_event_call(struct trace_event_call *call)
2547 {
2548         struct trace_array *tr;
2549         struct trace_event_file *file;
2550
2551 #ifdef CONFIG_PERF_EVENTS
2552         if (call->perf_refcount)
2553                 return -EBUSY;
2554 #endif
2555         do_for_each_event_file(tr, file) {
2556                 if (file->event_call != call)
2557                         continue;
2558                 /*
2559                  * We can't rely on ftrace_event_enable_disable(enable => 0)
2560                  * we are going to do, EVENT_FILE_FL_SOFT_MODE can suppress
2561                  * TRACE_REG_UNREGISTER.
2562                  */
2563                 if (file->flags & EVENT_FILE_FL_ENABLED)
2564                         return -EBUSY;
2565                 /*
2566                  * The do_for_each_event_file_safe() is
2567                  * a double loop. After finding the call for this
2568                  * trace_array, we use break to jump to the next
2569                  * trace_array.
2570                  */
2571                 break;
2572         } while_for_each_event_file();
2573
2574         __trace_remove_event_call(call);
2575
2576         return 0;
2577 }
2578
2579 /* Remove an event_call */
2580 int trace_remove_event_call(struct trace_event_call *call)
2581 {
2582         int ret;
2583
2584         lockdep_assert_held(&event_mutex);
2585
2586         mutex_lock(&trace_types_lock);
2587         down_write(&trace_event_sem);
2588         ret = probe_remove_event_call(call);
2589         up_write(&trace_event_sem);
2590         mutex_unlock(&trace_types_lock);
2591
2592         return ret;
2593 }
2594
2595 #define for_each_event(event, start, end)                       \
2596         for (event = start;                                     \
2597              (unsigned long)event < (unsigned long)end;         \
2598              event++)
2599
2600 #ifdef CONFIG_MODULES
2601
2602 static void trace_module_add_events(struct module *mod)
2603 {
2604         struct trace_event_call **call, **start, **end;
2605
2606         if (!mod->num_trace_events)
2607                 return;
2608
2609         /* Don't add infrastructure for mods without tracepoints */
2610         if (trace_module_has_bad_taint(mod)) {
2611                 pr_err("%s: module has bad taint, not creating trace events\n",
2612                        mod->name);
2613                 return;
2614         }
2615
2616         start = mod->trace_events;
2617         end = mod->trace_events + mod->num_trace_events;
2618
2619         for_each_event(call, start, end) {
2620                 __register_event(*call, mod);
2621                 __add_event_to_tracers(*call);
2622         }
2623 }
2624
2625 static void trace_module_remove_events(struct module *mod)
2626 {
2627         struct trace_event_call *call, *p;
2628
2629         down_write(&trace_event_sem);
2630         list_for_each_entry_safe(call, p, &ftrace_events, list) {
2631                 if (call->mod == mod)
2632                         __trace_remove_event_call(call);
2633         }
2634         up_write(&trace_event_sem);
2635
2636         /*
2637          * It is safest to reset the ring buffer if the module being unloaded
2638          * registered any events that were used. The only worry is if
2639          * a new module gets loaded, and takes on the same id as the events
2640          * of this module. When printing out the buffer, traced events left
2641          * over from this module may be passed to the new module events and
2642          * unexpected results may occur.
2643          */
2644         tracing_reset_all_online_cpus();
2645 }
2646
2647 static int trace_module_notify(struct notifier_block *self,
2648                                unsigned long val, void *data)
2649 {
2650         struct module *mod = data;
2651
2652         mutex_lock(&event_mutex);
2653         mutex_lock(&trace_types_lock);
2654         switch (val) {
2655         case MODULE_STATE_COMING:
2656                 trace_module_add_events(mod);
2657                 break;
2658         case MODULE_STATE_GOING:
2659                 trace_module_remove_events(mod);
2660                 break;
2661         }
2662         mutex_unlock(&trace_types_lock);
2663         mutex_unlock(&event_mutex);
2664
2665         return NOTIFY_OK;
2666 }
2667
2668 static struct notifier_block trace_module_nb = {
2669         .notifier_call = trace_module_notify,
2670         .priority = 1, /* higher than trace.c module notify */
2671 };
2672 #endif /* CONFIG_MODULES */
2673
2674 /* Create a new event directory structure for a trace directory. */
2675 static void
2676 __trace_add_event_dirs(struct trace_array *tr)
2677 {
2678         struct trace_event_call *call;
2679         int ret;
2680
2681         list_for_each_entry(call, &ftrace_events, list) {
2682                 ret = __trace_add_new_event(call, tr);
2683                 if (ret < 0)
2684                         pr_warn("Could not create directory for event %s\n",
2685                                 trace_event_name(call));
2686         }
2687 }
2688
2689 /* Returns any file that matches the system and event */
2690 struct trace_event_file *
2691 __find_event_file(struct trace_array *tr, const char *system, const char *event)
2692 {
2693         struct trace_event_file *file;
2694         struct trace_event_call *call;
2695         const char *name;
2696
2697         list_for_each_entry(file, &tr->events, list) {
2698
2699                 call = file->event_call;
2700                 name = trace_event_name(call);
2701
2702                 if (!name || !call->class)
2703                         continue;
2704
2705                 if (strcmp(event, name) == 0 &&
2706                     strcmp(system, call->class->system) == 0)
2707                         return file;
2708         }
2709         return NULL;
2710 }
2711
2712 /* Returns valid trace event files that match system and event */
2713 struct trace_event_file *
2714 find_event_file(struct trace_array *tr, const char *system, const char *event)
2715 {
2716         struct trace_event_file *file;
2717
2718         file = __find_event_file(tr, system, event);
2719         if (!file || !file->event_call->class->reg ||
2720             file->event_call->flags & TRACE_EVENT_FL_IGNORE_ENABLE)
2721                 return NULL;
2722
2723         return file;
2724 }
2725
2726 /**
2727  * trace_get_event_file - Find and return a trace event file
2728  * @instance: The name of the trace instance containing the event
2729  * @system: The name of the system containing the event
2730  * @event: The name of the event
2731  *
2732  * Return a trace event file given the trace instance name, trace
2733  * system, and trace event name.  If the instance name is NULL, it
2734  * refers to the top-level trace array.
2735  *
2736  * This function will look it up and return it if found, after calling
2737  * trace_array_get() to prevent the instance from going away, and
2738  * increment the event's module refcount to prevent it from being
2739  * removed.
2740  *
2741  * To release the file, call trace_put_event_file(), which will call
2742  * trace_array_put() and decrement the event's module refcount.
2743  *
2744  * Return: The trace event on success, ERR_PTR otherwise.
2745  */
2746 struct trace_event_file *trace_get_event_file(const char *instance,
2747                                               const char *system,
2748                                               const char *event)
2749 {
2750         struct trace_array *tr = top_trace_array();
2751         struct trace_event_file *file = NULL;
2752         int ret = -EINVAL;
2753
2754         if (instance) {
2755                 tr = trace_array_find_get(instance);
2756                 if (!tr)
2757                         return ERR_PTR(-ENOENT);
2758         } else {
2759                 ret = trace_array_get(tr);
2760                 if (ret)
2761                         return ERR_PTR(ret);
2762         }
2763
2764         mutex_lock(&event_mutex);
2765
2766         file = find_event_file(tr, system, event);
2767         if (!file) {
2768                 trace_array_put(tr);
2769                 ret = -EINVAL;
2770                 goto out;
2771         }
2772
2773         /* Don't let event modules unload while in use */
2774         ret = try_module_get(file->event_call->mod);
2775         if (!ret) {
2776                 trace_array_put(tr);
2777                 ret = -EBUSY;
2778                 goto out;
2779         }
2780
2781         ret = 0;
2782  out:
2783         mutex_unlock(&event_mutex);
2784
2785         if (ret)
2786                 file = ERR_PTR(ret);
2787
2788         return file;
2789 }
2790 EXPORT_SYMBOL_GPL(trace_get_event_file);
2791
2792 /**
2793  * trace_put_event_file - Release a file from trace_get_event_file()
2794  * @file: The trace event file
2795  *
2796  * If a file was retrieved using trace_get_event_file(), this should
2797  * be called when it's no longer needed.  It will cancel the previous
2798  * trace_array_get() called by that function, and decrement the
2799  * event's module refcount.
2800  */
2801 void trace_put_event_file(struct trace_event_file *file)
2802 {
2803         mutex_lock(&event_mutex);
2804         module_put(file->event_call->mod);
2805         mutex_unlock(&event_mutex);
2806
2807         trace_array_put(file->tr);
2808 }
2809 EXPORT_SYMBOL_GPL(trace_put_event_file);
2810
2811 #ifdef CONFIG_DYNAMIC_FTRACE
2812
2813 /* Avoid typos */
2814 #define ENABLE_EVENT_STR        "enable_event"
2815 #define DISABLE_EVENT_STR       "disable_event"
2816
2817 struct event_probe_data {
2818         struct trace_event_file *file;
2819         unsigned long                   count;
2820         int                             ref;
2821         bool                            enable;
2822 };
2823
2824 static void update_event_probe(struct event_probe_data *data)
2825 {
2826         if (data->enable)
2827                 clear_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &data->file->flags);
2828         else
2829                 set_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &data->file->flags);
2830 }
2831
2832 static void
2833 event_enable_probe(unsigned long ip, unsigned long parent_ip,
2834                    struct trace_array *tr, struct ftrace_probe_ops *ops,
2835                    void *data)
2836 {
2837         struct ftrace_func_mapper *mapper = data;
2838         struct event_probe_data *edata;
2839         void **pdata;
2840
2841         pdata = ftrace_func_mapper_find_ip(mapper, ip);
2842         if (!pdata || !*pdata)
2843                 return;
2844
2845         edata = *pdata;
2846         update_event_probe(edata);
2847 }
2848
2849 static void
2850 event_enable_count_probe(unsigned long ip, unsigned long parent_ip,
2851                          struct trace_array *tr, struct ftrace_probe_ops *ops,
2852                          void *data)
2853 {
2854         struct ftrace_func_mapper *mapper = data;
2855         struct event_probe_data *edata;
2856         void **pdata;
2857
2858         pdata = ftrace_func_mapper_find_ip(mapper, ip);
2859         if (!pdata || !*pdata)
2860                 return;
2861
2862         edata = *pdata;
2863
2864         if (!edata->count)
2865                 return;
2866
2867         /* Skip if the event is in a state we want to switch to */
2868         if (edata->enable == !(edata->file->flags & EVENT_FILE_FL_SOFT_DISABLED))
2869                 return;
2870
2871         if (edata->count != -1)
2872                 (edata->count)--;
2873
2874         update_event_probe(edata);
2875 }
2876
2877 static int
2878 event_enable_print(struct seq_file *m, unsigned long ip,
2879                    struct ftrace_probe_ops *ops, void *data)
2880 {
2881         struct ftrace_func_mapper *mapper = data;
2882         struct event_probe_data *edata;
2883         void **pdata;
2884
2885         pdata = ftrace_func_mapper_find_ip(mapper, ip);
2886
2887         if (WARN_ON_ONCE(!pdata || !*pdata))
2888                 return 0;
2889
2890         edata = *pdata;
2891
2892         seq_printf(m, "%ps:", (void *)ip);
2893
2894         seq_printf(m, "%s:%s:%s",
2895                    edata->enable ? ENABLE_EVENT_STR : DISABLE_EVENT_STR,
2896                    edata->file->event_call->class->system,
2897                    trace_event_name(edata->file->event_call));
2898
2899         if (edata->count == -1)
2900                 seq_puts(m, ":unlimited\n");
2901         else
2902                 seq_printf(m, ":count=%ld\n", edata->count);
2903
2904         return 0;
2905 }
2906
2907 static int
2908 event_enable_init(struct ftrace_probe_ops *ops, struct trace_array *tr,
2909                   unsigned long ip, void *init_data, void **data)
2910 {
2911         struct ftrace_func_mapper *mapper = *data;
2912         struct event_probe_data *edata = init_data;
2913         int ret;
2914
2915         if (!mapper) {
2916                 mapper = allocate_ftrace_func_mapper();
2917                 if (!mapper)
2918                         return -ENODEV;
2919                 *data = mapper;
2920         }
2921
2922         ret = ftrace_func_mapper_add_ip(mapper, ip, edata);
2923         if (ret < 0)
2924                 return ret;
2925
2926         edata->ref++;
2927
2928         return 0;
2929 }
2930
2931 static int free_probe_data(void *data)
2932 {
2933         struct event_probe_data *edata = data;
2934
2935         edata->ref--;
2936         if (!edata->ref) {
2937                 /* Remove the SOFT_MODE flag */
2938                 __ftrace_event_enable_disable(edata->file, 0, 1);
2939                 module_put(edata->file->event_call->mod);
2940                 kfree(edata);
2941         }
2942         return 0;
2943 }
2944
2945 static void
2946 event_enable_free(struct ftrace_probe_ops *ops, struct trace_array *tr,
2947                   unsigned long ip, void *data)
2948 {
2949         struct ftrace_func_mapper *mapper = data;
2950         struct event_probe_data *edata;
2951
2952         if (!ip) {
2953                 if (!mapper)
2954                         return;
2955                 free_ftrace_func_mapper(mapper, free_probe_data);
2956                 return;
2957         }
2958
2959         edata = ftrace_func_mapper_remove_ip(mapper, ip);
2960
2961         if (WARN_ON_ONCE(!edata))
2962                 return;
2963
2964         if (WARN_ON_ONCE(edata->ref <= 0))
2965                 return;
2966
2967         free_probe_data(edata);
2968 }
2969
2970 static struct ftrace_probe_ops event_enable_probe_ops = {
2971         .func                   = event_enable_probe,
2972         .print                  = event_enable_print,
2973         .init                   = event_enable_init,
2974         .free                   = event_enable_free,
2975 };
2976
2977 static struct ftrace_probe_ops event_enable_count_probe_ops = {
2978         .func                   = event_enable_count_probe,
2979         .print                  = event_enable_print,
2980         .init                   = event_enable_init,
2981         .free                   = event_enable_free,
2982 };
2983
2984 static struct ftrace_probe_ops event_disable_probe_ops = {
2985         .func                   = event_enable_probe,
2986         .print                  = event_enable_print,
2987         .init                   = event_enable_init,
2988         .free                   = event_enable_free,
2989 };
2990
2991 static struct ftrace_probe_ops event_disable_count_probe_ops = {
2992         .func                   = event_enable_count_probe,
2993         .print                  = event_enable_print,
2994         .init                   = event_enable_init,
2995         .free                   = event_enable_free,
2996 };
2997
2998 static int
2999 event_enable_func(struct trace_array *tr, struct ftrace_hash *hash,
3000                   char *glob, char *cmd, char *param, int enabled)
3001 {
3002         struct trace_event_file *file;
3003         struct ftrace_probe_ops *ops;
3004         struct event_probe_data *data;
3005         const char *system;
3006         const char *event;
3007         char *number;
3008         bool enable;
3009         int ret;
3010
3011         if (!tr)
3012                 return -ENODEV;
3013
3014         /* hash funcs only work with set_ftrace_filter */
3015         if (!enabled || !param)
3016                 return -EINVAL;
3017
3018         system = strsep(&param, ":");
3019         if (!param)
3020                 return -EINVAL;
3021
3022         event = strsep(&param, ":");
3023
3024         mutex_lock(&event_mutex);
3025
3026         ret = -EINVAL;
3027         file = find_event_file(tr, system, event);
3028         if (!file)
3029                 goto out;
3030
3031         enable = strcmp(cmd, ENABLE_EVENT_STR) == 0;
3032
3033         if (enable)
3034                 ops = param ? &event_enable_count_probe_ops : &event_enable_probe_ops;
3035         else
3036                 ops = param ? &event_disable_count_probe_ops : &event_disable_probe_ops;
3037
3038         if (glob[0] == '!') {
3039                 ret = unregister_ftrace_function_probe_func(glob+1, tr, ops);
3040                 goto out;
3041         }
3042
3043         ret = -ENOMEM;
3044
3045         data = kzalloc(sizeof(*data), GFP_KERNEL);
3046         if (!data)
3047                 goto out;
3048
3049         data->enable = enable;
3050         data->count = -1;
3051         data->file = file;
3052
3053         if (!param)
3054                 goto out_reg;
3055
3056         number = strsep(&param, ":");
3057
3058         ret = -EINVAL;
3059         if (!strlen(number))
3060                 goto out_free;
3061
3062         /*
3063          * We use the callback data field (which is a pointer)
3064          * as our counter.
3065          */
3066         ret = kstrtoul(number, 0, &data->count);
3067         if (ret)
3068                 goto out_free;
3069
3070  out_reg:
3071         /* Don't let event modules unload while probe registered */
3072         ret = try_module_get(file->event_call->mod);
3073         if (!ret) {
3074                 ret = -EBUSY;
3075                 goto out_free;
3076         }
3077
3078         ret = __ftrace_event_enable_disable(file, 1, 1);
3079         if (ret < 0)
3080                 goto out_put;
3081
3082         ret = register_ftrace_function_probe(glob, tr, ops, data);
3083         /*
3084          * The above returns on success the # of functions enabled,
3085          * but if it didn't find any functions it returns zero.
3086          * Consider no functions a failure too.
3087          */
3088         if (!ret) {
3089                 ret = -ENOENT;
3090                 goto out_disable;
3091         } else if (ret < 0)
3092                 goto out_disable;
3093         /* Just return zero, not the number of enabled functions */
3094         ret = 0;
3095  out:
3096         mutex_unlock(&event_mutex);
3097         return ret;
3098
3099  out_disable:
3100         __ftrace_event_enable_disable(file, 0, 1);
3101  out_put:
3102         module_put(file->event_call->mod);
3103  out_free:
3104         kfree(data);
3105         goto out;
3106 }
3107
3108 static struct ftrace_func_command event_enable_cmd = {
3109         .name                   = ENABLE_EVENT_STR,
3110         .func                   = event_enable_func,
3111 };
3112
3113 static struct ftrace_func_command event_disable_cmd = {
3114         .name                   = DISABLE_EVENT_STR,
3115         .func                   = event_enable_func,
3116 };
3117
3118 static __init int register_event_cmds(void)
3119 {
3120         int ret;
3121
3122         ret = register_ftrace_command(&event_enable_cmd);
3123         if (WARN_ON(ret < 0))
3124                 return ret;
3125         ret = register_ftrace_command(&event_disable_cmd);
3126         if (WARN_ON(ret < 0))
3127                 unregister_ftrace_command(&event_enable_cmd);
3128         return ret;
3129 }
3130 #else
3131 static inline int register_event_cmds(void) { return 0; }
3132 #endif /* CONFIG_DYNAMIC_FTRACE */
3133
3134 /*
3135  * The top level array and trace arrays created by boot-time tracing
3136  * have already had its trace_event_file descriptors created in order
3137  * to allow for early events to be recorded.
3138  * This function is called after the tracefs has been initialized,
3139  * and we now have to create the files associated to the events.
3140  */
3141 static void __trace_early_add_event_dirs(struct trace_array *tr)
3142 {
3143         struct trace_event_file *file;
3144         int ret;
3145
3146
3147         list_for_each_entry(file, &tr->events, list) {
3148                 ret = event_create_dir(tr->event_dir, file);
3149                 if (ret < 0)
3150                         pr_warn("Could not create directory for event %s\n",
3151                                 trace_event_name(file->event_call));
3152         }
3153 }
3154
3155 /*
3156  * For early boot up, the top trace array and the trace arrays created
3157  * by boot-time tracing require to have a list of events that can be
3158  * enabled. This must be done before the filesystem is set up in order
3159  * to allow events to be traced early.
3160  */
3161 void __trace_early_add_events(struct trace_array *tr)
3162 {
3163         struct trace_event_call *call;
3164         int ret;
3165
3166         list_for_each_entry(call, &ftrace_events, list) {
3167                 /* Early boot up should not have any modules loaded */
3168                 if (WARN_ON_ONCE(call->mod))
3169                         continue;
3170
3171                 ret = __trace_early_add_new_event(call, tr);
3172                 if (ret < 0)
3173                         pr_warn("Could not create early event %s\n",
3174                                 trace_event_name(call));
3175         }
3176 }
3177
3178 /* Remove the event directory structure for a trace directory. */
3179 static void
3180 __trace_remove_event_dirs(struct trace_array *tr)
3181 {
3182         struct trace_event_file *file, *next;
3183
3184         list_for_each_entry_safe(file, next, &tr->events, list)
3185                 remove_event_file_dir(file);
3186 }
3187
3188 static void __add_event_to_tracers(struct trace_event_call *call)
3189 {
3190         struct trace_array *tr;
3191
3192         list_for_each_entry(tr, &ftrace_trace_arrays, list)
3193                 __trace_add_new_event(call, tr);
3194 }
3195
3196 extern struct trace_event_call *__start_ftrace_events[];
3197 extern struct trace_event_call *__stop_ftrace_events[];
3198
3199 static char bootup_event_buf[COMMAND_LINE_SIZE] __initdata;
3200
3201 static __init int setup_trace_event(char *str)
3202 {
3203         strlcpy(bootup_event_buf, str, COMMAND_LINE_SIZE);
3204         ring_buffer_expanded = true;
3205         disable_tracing_selftest("running event tracing");
3206
3207         return 1;
3208 }
3209 __setup("trace_event=", setup_trace_event);
3210
3211 /* Expects to have event_mutex held when called */
3212 static int
3213 create_event_toplevel_files(struct dentry *parent, struct trace_array *tr)
3214 {
3215         struct dentry *d_events;
3216         struct dentry *entry;
3217
3218         entry = tracefs_create_file("set_event", 0644, parent,
3219                                     tr, &ftrace_set_event_fops);
3220         if (!entry) {
3221                 pr_warn("Could not create tracefs 'set_event' entry\n");
3222                 return -ENOMEM;
3223         }
3224
3225         d_events = tracefs_create_dir("events", parent);
3226         if (!d_events) {
3227                 pr_warn("Could not create tracefs 'events' directory\n");
3228                 return -ENOMEM;
3229         }
3230
3231         entry = trace_create_file("enable", 0644, d_events,
3232                                   tr, &ftrace_tr_enable_fops);
3233         if (!entry) {
3234                 pr_warn("Could not create tracefs 'enable' entry\n");
3235                 return -ENOMEM;
3236         }
3237
3238         /* There are not as crucial, just warn if they are not created */
3239
3240         entry = tracefs_create_file("set_event_pid", 0644, parent,
3241                                     tr, &ftrace_set_event_pid_fops);
3242         if (!entry)
3243                 pr_warn("Could not create tracefs 'set_event_pid' entry\n");
3244
3245         entry = tracefs_create_file("set_event_notrace_pid", 0644, parent,
3246                                     tr, &ftrace_set_event_notrace_pid_fops);
3247         if (!entry)
3248                 pr_warn("Could not create tracefs 'set_event_notrace_pid' entry\n");
3249
3250         /* ring buffer internal formats */
3251         entry = trace_create_file("header_page", 0444, d_events,
3252                                   ring_buffer_print_page_header,
3253                                   &ftrace_show_header_fops);
3254         if (!entry)
3255                 pr_warn("Could not create tracefs 'header_page' entry\n");
3256
3257         entry = trace_create_file("header_event", 0444, d_events,
3258                                   ring_buffer_print_entry_header,
3259                                   &ftrace_show_header_fops);
3260         if (!entry)
3261                 pr_warn("Could not create tracefs 'header_event' entry\n");
3262
3263         tr->event_dir = d_events;
3264
3265         return 0;
3266 }
3267
3268 /**
3269  * event_trace_add_tracer - add a instance of a trace_array to events
3270  * @parent: The parent dentry to place the files/directories for events in
3271  * @tr: The trace array associated with these events
3272  *
3273  * When a new instance is created, it needs to set up its events
3274  * directory, as well as other files associated with events. It also
3275  * creates the event hierarchy in the @parent/events directory.
3276  *
3277  * Returns 0 on success.
3278  *
3279  * Must be called with event_mutex held.
3280  */
3281 int event_trace_add_tracer(struct dentry *parent, struct trace_array *tr)
3282 {
3283         int ret;
3284
3285         lockdep_assert_held(&event_mutex);
3286
3287         ret = create_event_toplevel_files(parent, tr);
3288         if (ret)
3289                 goto out;
3290
3291         down_write(&trace_event_sem);
3292         /* If tr already has the event list, it is initialized in early boot. */
3293         if (unlikely(!list_empty(&tr->events)))
3294                 __trace_early_add_event_dirs(tr);
3295         else
3296                 __trace_add_event_dirs(tr);
3297         up_write(&trace_event_sem);
3298
3299  out:
3300         return ret;
3301 }
3302
3303 /*
3304  * The top trace array already had its file descriptors created.
3305  * Now the files themselves need to be created.
3306  */
3307 static __init int
3308 early_event_add_tracer(struct dentry *parent, struct trace_array *tr)
3309 {
3310         int ret;
3311
3312         mutex_lock(&event_mutex);
3313
3314         ret = create_event_toplevel_files(parent, tr);
3315         if (ret)
3316                 goto out_unlock;
3317
3318         down_write(&trace_event_sem);
3319         __trace_early_add_event_dirs(tr);
3320         up_write(&trace_event_sem);
3321
3322  out_unlock:
3323         mutex_unlock(&event_mutex);
3324
3325         return ret;
3326 }
3327
3328 /* Must be called with event_mutex held */
3329 int event_trace_del_tracer(struct trace_array *tr)
3330 {
3331         lockdep_assert_held(&event_mutex);
3332
3333         /* Disable any event triggers and associated soft-disabled events */
3334         clear_event_triggers(tr);
3335
3336         /* Clear the pid list */
3337         __ftrace_clear_event_pids(tr, TRACE_PIDS | TRACE_NO_PIDS);
3338
3339         /* Disable any running events */
3340         __ftrace_set_clr_event_nolock(tr, NULL, NULL, NULL, 0);
3341
3342         /* Make sure no more events are being executed */
3343         tracepoint_synchronize_unregister();
3344
3345         down_write(&trace_event_sem);
3346         __trace_remove_event_dirs(tr);
3347         tracefs_remove(tr->event_dir);
3348         up_write(&trace_event_sem);
3349
3350         tr->event_dir = NULL;
3351
3352         return 0;
3353 }
3354
3355 static __init int event_trace_memsetup(void)
3356 {
3357         field_cachep = KMEM_CACHE(ftrace_event_field, SLAB_PANIC);
3358         file_cachep = KMEM_CACHE(trace_event_file, SLAB_PANIC);
3359         return 0;
3360 }
3361
3362 static __init void
3363 early_enable_events(struct trace_array *tr, bool disable_first)
3364 {
3365         char *buf = bootup_event_buf;
3366         char *token;
3367         int ret;
3368
3369         while (true) {
3370                 token = strsep(&buf, ",");
3371
3372                 if (!token)
3373                         break;
3374
3375                 if (*token) {
3376                         /* Restarting syscalls requires that we stop them first */
3377                         if (disable_first)
3378                                 ftrace_set_clr_event(tr, token, 0);
3379
3380                         ret = ftrace_set_clr_event(tr, token, 1);
3381                         if (ret)
3382                                 pr_warn("Failed to enable trace event: %s\n", token);
3383                 }
3384
3385                 /* Put back the comma to allow this to be called again */
3386                 if (buf)
3387                         *(buf - 1) = ',';
3388         }
3389 }
3390
3391 static __init int event_trace_enable(void)
3392 {
3393         struct trace_array *tr = top_trace_array();
3394         struct trace_event_call **iter, *call;
3395         int ret;
3396
3397         if (!tr)
3398                 return -ENODEV;
3399
3400         for_each_event(iter, __start_ftrace_events, __stop_ftrace_events) {
3401
3402                 call = *iter;
3403                 ret = event_init(call);
3404                 if (!ret)
3405                         list_add(&call->list, &ftrace_events);
3406         }
3407
3408         /*
3409          * We need the top trace array to have a working set of trace
3410          * points at early init, before the debug files and directories
3411          * are created. Create the file entries now, and attach them
3412          * to the actual file dentries later.
3413          */
3414         __trace_early_add_events(tr);
3415
3416         early_enable_events(tr, false);
3417
3418         trace_printk_start_comm();
3419
3420         register_event_cmds();
3421
3422         register_trigger_cmds();
3423
3424         return 0;
3425 }
3426
3427 /*
3428  * event_trace_enable() is called from trace_event_init() first to
3429  * initialize events and perhaps start any events that are on the
3430  * command line. Unfortunately, there are some events that will not
3431  * start this early, like the system call tracepoints that need
3432  * to set the %SYSCALL_WORK_SYSCALL_TRACEPOINT flag of pid 1. But
3433  * event_trace_enable() is called before pid 1 starts, and this flag
3434  * is never set, making the syscall tracepoint never get reached, but
3435  * the event is enabled regardless (and not doing anything).
3436  */
3437 static __init int event_trace_enable_again(void)
3438 {
3439         struct trace_array *tr;
3440
3441         tr = top_trace_array();
3442         if (!tr)
3443                 return -ENODEV;
3444
3445         early_enable_events(tr, true);
3446
3447         return 0;
3448 }
3449
3450 early_initcall(event_trace_enable_again);
3451
3452 /* Init fields which doesn't related to the tracefs */
3453 static __init int event_trace_init_fields(void)
3454 {
3455         if (trace_define_generic_fields())
3456                 pr_warn("tracing: Failed to allocated generic fields");
3457
3458         if (trace_define_common_fields())
3459                 pr_warn("tracing: Failed to allocate common fields");
3460
3461         return 0;
3462 }
3463
3464 __init int event_trace_init(void)
3465 {
3466         struct trace_array *tr;
3467         struct dentry *entry;
3468         int ret;
3469
3470         tr = top_trace_array();
3471         if (!tr)
3472                 return -ENODEV;
3473
3474         entry = tracefs_create_file("available_events", 0444, NULL,
3475                                     tr, &ftrace_avail_fops);
3476         if (!entry)
3477                 pr_warn("Could not create tracefs 'available_events' entry\n");
3478
3479         ret = early_event_add_tracer(NULL, tr);
3480         if (ret)
3481                 return ret;
3482
3483 #ifdef CONFIG_MODULES
3484         ret = register_module_notifier(&trace_module_nb);
3485         if (ret)
3486                 pr_warn("Failed to register trace events module notifier\n");
3487 #endif
3488
3489         eventdir_initialized = true;
3490
3491         return 0;
3492 }
3493
3494 void __init trace_event_init(void)
3495 {
3496         event_trace_memsetup();
3497         init_ftrace_syscalls();
3498         event_trace_enable();
3499         event_trace_init_fields();
3500 }
3501
3502 #ifdef CONFIG_EVENT_TRACE_STARTUP_TEST
3503
3504 static DEFINE_SPINLOCK(test_spinlock);
3505 static DEFINE_SPINLOCK(test_spinlock_irq);
3506 static DEFINE_MUTEX(test_mutex);
3507
3508 static __init void test_work(struct work_struct *dummy)
3509 {
3510         spin_lock(&test_spinlock);
3511         spin_lock_irq(&test_spinlock_irq);
3512         udelay(1);
3513         spin_unlock_irq(&test_spinlock_irq);
3514         spin_unlock(&test_spinlock);
3515
3516         mutex_lock(&test_mutex);
3517         msleep(1);
3518         mutex_unlock(&test_mutex);
3519 }
3520
3521 static __init int event_test_thread(void *unused)
3522 {
3523         void *test_malloc;
3524
3525         test_malloc = kmalloc(1234, GFP_KERNEL);
3526         if (!test_malloc)
3527                 pr_info("failed to kmalloc\n");
3528
3529         schedule_on_each_cpu(test_work);
3530
3531         kfree(test_malloc);
3532
3533         set_current_state(TASK_INTERRUPTIBLE);
3534         while (!kthread_should_stop()) {
3535                 schedule();
3536                 set_current_state(TASK_INTERRUPTIBLE);
3537         }
3538         __set_current_state(TASK_RUNNING);
3539
3540         return 0;
3541 }
3542
3543 /*
3544  * Do various things that may trigger events.
3545  */
3546 static __init void event_test_stuff(void)
3547 {
3548         struct task_struct *test_thread;
3549
3550         test_thread = kthread_run(event_test_thread, NULL, "test-events");
3551         msleep(1);
3552         kthread_stop(test_thread);
3553 }
3554
3555 /*
3556  * For every trace event defined, we will test each trace point separately,
3557  * and then by groups, and finally all trace points.
3558  */
3559 static __init void event_trace_self_tests(void)
3560 {
3561         struct trace_subsystem_dir *dir;
3562         struct trace_event_file *file;
3563         struct trace_event_call *call;
3564         struct event_subsystem *system;
3565         struct trace_array *tr;
3566         int ret;
3567
3568         tr = top_trace_array();
3569         if (!tr)
3570                 return;
3571
3572         pr_info("Running tests on trace events:\n");
3573
3574         list_for_each_entry(file, &tr->events, list) {
3575
3576                 call = file->event_call;
3577
3578                 /* Only test those that have a probe */
3579                 if (!call->class || !call->class->probe)
3580                         continue;
3581
3582 /*
3583  * Testing syscall events here is pretty useless, but
3584  * we still do it if configured. But this is time consuming.
3585  * What we really need is a user thread to perform the
3586  * syscalls as we test.
3587  */
3588 #ifndef CONFIG_EVENT_TRACE_TEST_SYSCALLS
3589                 if (call->class->system &&
3590                     strcmp(call->class->system, "syscalls") == 0)
3591                         continue;
3592 #endif
3593
3594                 pr_info("Testing event %s: ", trace_event_name(call));
3595
3596                 /*
3597                  * If an event is already enabled, someone is using
3598                  * it and the self test should not be on.
3599                  */
3600                 if (file->flags & EVENT_FILE_FL_ENABLED) {
3601                         pr_warn("Enabled event during self test!\n");
3602                         WARN_ON_ONCE(1);
3603                         continue;
3604                 }
3605
3606                 ftrace_event_enable_disable(file, 1);
3607                 event_test_stuff();
3608                 ftrace_event_enable_disable(file, 0);
3609
3610                 pr_cont("OK\n");
3611         }
3612
3613         /* Now test at the sub system level */
3614
3615         pr_info("Running tests on trace event systems:\n");
3616
3617         list_for_each_entry(dir, &tr->systems, list) {
3618
3619                 system = dir->subsystem;
3620
3621                 /* the ftrace system is special, skip it */
3622                 if (strcmp(system->name, "ftrace") == 0)
3623                         continue;
3624
3625                 pr_info("Testing event system %s: ", system->name);
3626
3627                 ret = __ftrace_set_clr_event(tr, NULL, system->name, NULL, 1);
3628                 if (WARN_ON_ONCE(ret)) {
3629                         pr_warn("error enabling system %s\n",
3630                                 system->name);
3631                         continue;
3632                 }
3633
3634                 event_test_stuff();
3635
3636                 ret = __ftrace_set_clr_event(tr, NULL, system->name, NULL, 0);
3637                 if (WARN_ON_ONCE(ret)) {
3638                         pr_warn("error disabling system %s\n",
3639                                 system->name);
3640                         continue;
3641                 }
3642
3643                 pr_cont("OK\n");
3644         }
3645
3646         /* Test with all events enabled */
3647
3648         pr_info("Running tests on all trace events:\n");
3649         pr_info("Testing all events: ");
3650
3651         ret = __ftrace_set_clr_event(tr, NULL, NULL, NULL, 1);
3652         if (WARN_ON_ONCE(ret)) {
3653                 pr_warn("error enabling all events\n");
3654                 return;
3655         }
3656
3657         event_test_stuff();
3658
3659         /* reset sysname */
3660         ret = __ftrace_set_clr_event(tr, NULL, NULL, NULL, 0);
3661         if (WARN_ON_ONCE(ret)) {
3662                 pr_warn("error disabling all events\n");
3663                 return;
3664         }
3665
3666         pr_cont("OK\n");
3667 }
3668
3669 #ifdef CONFIG_FUNCTION_TRACER
3670
3671 static DEFINE_PER_CPU(atomic_t, ftrace_test_event_disable);
3672
3673 static struct trace_event_file event_trace_file __initdata;
3674
3675 static void __init
3676 function_test_events_call(unsigned long ip, unsigned long parent_ip,
3677                           struct ftrace_ops *op, struct ftrace_regs *regs)
3678 {
3679         struct trace_buffer *buffer;
3680         struct ring_buffer_event *event;
3681         struct ftrace_entry *entry;
3682         unsigned int trace_ctx;
3683         long disabled;
3684         int cpu;
3685
3686         trace_ctx = tracing_gen_ctx();
3687         preempt_disable_notrace();
3688         cpu = raw_smp_processor_id();
3689         disabled = atomic_inc_return(&per_cpu(ftrace_test_event_disable, cpu));
3690
3691         if (disabled != 1)
3692                 goto out;
3693
3694         event = trace_event_buffer_lock_reserve(&buffer, &event_trace_file,
3695                                                 TRACE_FN, sizeof(*entry),
3696                                                 trace_ctx);
3697         if (!event)
3698                 goto out;
3699         entry   = ring_buffer_event_data(event);
3700         entry->ip                       = ip;
3701         entry->parent_ip                = parent_ip;
3702
3703         event_trigger_unlock_commit(&event_trace_file, buffer, event,
3704                                     entry, trace_ctx);
3705  out:
3706         atomic_dec(&per_cpu(ftrace_test_event_disable, cpu));
3707         preempt_enable_notrace();
3708 }
3709
3710 static struct ftrace_ops trace_ops __initdata  =
3711 {
3712         .func = function_test_events_call,
3713 };
3714
3715 static __init void event_trace_self_test_with_function(void)
3716 {
3717         int ret;
3718
3719         event_trace_file.tr = top_trace_array();
3720         if (WARN_ON(!event_trace_file.tr))
3721                 return;
3722
3723         ret = register_ftrace_function(&trace_ops);
3724         if (WARN_ON(ret < 0)) {
3725                 pr_info("Failed to enable function tracer for event tests\n");
3726                 return;
3727         }
3728         pr_info("Running tests again, along with the function tracer\n");
3729         event_trace_self_tests();
3730         unregister_ftrace_function(&trace_ops);
3731 }
3732 #else
3733 static __init void event_trace_self_test_with_function(void)
3734 {
3735 }
3736 #endif
3737
3738 static __init int event_trace_self_tests_init(void)
3739 {
3740         if (!tracing_selftest_disabled) {
3741                 event_trace_self_tests();
3742                 event_trace_self_test_with_function();
3743         }
3744
3745         return 0;
3746 }
3747
3748 late_initcall(event_trace_self_tests_init);
3749
3750 #endif