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