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