1 // SPDX-License-Identifier: GPL-2.0
5 * Copyright (C) 2008 Red Hat Inc, Steven Rostedt <srostedt@redhat.com>
7 * - Added format output of fields of the trace point.
8 * This was based off of work by Tom Zanussi <tzanussi@gmail.com>.
12 #define pr_fmt(fmt) fmt
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>
26 #include <trace/events/sched.h>
27 #include <trace/syscall.h>
29 #include <asm/setup.h>
31 #include "trace_output.h"
34 #define TRACE_SYSTEM "TRACE_SYSTEM"
36 DEFINE_MUTEX(event_mutex);
38 LIST_HEAD(ftrace_events);
39 static LIST_HEAD(ftrace_generic_fields);
40 static LIST_HEAD(ftrace_common_fields);
41 static bool eventdir_initialized;
43 static LIST_HEAD(module_strings);
45 struct module_string {
46 struct list_head next;
47 struct module *module;
51 #define GFP_TRACE (GFP_KERNEL | __GFP_ZERO)
53 static struct kmem_cache *field_cachep;
54 static struct kmem_cache *file_cachep;
56 static inline int system_refcount(struct event_subsystem *system)
58 return system->ref_count;
61 static int system_refcount_inc(struct event_subsystem *system)
63 return system->ref_count++;
66 static int system_refcount_dec(struct event_subsystem *system)
68 return --system->ref_count;
71 /* Double loops, do not use break, only goto's work */
72 #define do_for_each_event_file(tr, file) \
73 list_for_each_entry(tr, &ftrace_trace_arrays, list) { \
74 list_for_each_entry(file, &tr->events, list)
76 #define do_for_each_event_file_safe(tr, file) \
77 list_for_each_entry(tr, &ftrace_trace_arrays, list) { \
78 struct trace_event_file *___n; \
79 list_for_each_entry_safe(file, ___n, &tr->events, list)
81 #define while_for_each_event_file() \
84 static struct ftrace_event_field *
85 __find_event_field(struct list_head *head, char *name)
87 struct ftrace_event_field *field;
89 list_for_each_entry(field, head, link) {
90 if (!strcmp(field->name, name))
97 struct ftrace_event_field *
98 trace_find_event_field(struct trace_event_call *call, char *name)
100 struct ftrace_event_field *field;
101 struct list_head *head;
103 head = trace_get_fields(call);
104 field = __find_event_field(head, name);
108 field = __find_event_field(&ftrace_generic_fields, name);
112 return __find_event_field(&ftrace_common_fields, name);
115 static int __trace_define_field(struct list_head *head, const char *type,
116 const char *name, int offset, int size,
117 int is_signed, int filter_type)
119 struct ftrace_event_field *field;
121 field = kmem_cache_alloc(field_cachep, GFP_TRACE);
128 if (filter_type == FILTER_OTHER)
129 field->filter_type = filter_assign_type(type);
131 field->filter_type = filter_type;
133 field->offset = offset;
135 field->is_signed = is_signed;
137 list_add(&field->link, head);
142 int trace_define_field(struct trace_event_call *call, const char *type,
143 const char *name, int offset, int size, int is_signed,
146 struct list_head *head;
148 if (WARN_ON(!call->class))
151 head = trace_get_fields(call);
152 return __trace_define_field(head, type, name, offset, size,
153 is_signed, filter_type);
155 EXPORT_SYMBOL_GPL(trace_define_field);
157 #define __generic_field(type, item, filter_type) \
158 ret = __trace_define_field(&ftrace_generic_fields, #type, \
159 #item, 0, 0, is_signed_type(type), \
164 #define __common_field(type, item) \
165 ret = __trace_define_field(&ftrace_common_fields, #type, \
167 offsetof(typeof(ent), item), \
169 is_signed_type(type), FILTER_OTHER); \
173 static int trace_define_generic_fields(void)
177 __generic_field(int, CPU, FILTER_CPU);
178 __generic_field(int, cpu, FILTER_CPU);
179 __generic_field(int, common_cpu, FILTER_CPU);
180 __generic_field(char *, COMM, FILTER_COMM);
181 __generic_field(char *, comm, FILTER_COMM);
186 static int trace_define_common_fields(void)
189 struct trace_entry ent;
191 __common_field(unsigned short, type);
192 __common_field(unsigned char, flags);
193 /* Holds both preempt_count and migrate_disable */
194 __common_field(unsigned char, preempt_count);
195 __common_field(int, pid);
200 static void trace_destroy_fields(struct trace_event_call *call)
202 struct ftrace_event_field *field, *next;
203 struct list_head *head;
205 head = trace_get_fields(call);
206 list_for_each_entry_safe(field, next, head, link) {
207 list_del(&field->link);
208 kmem_cache_free(field_cachep, field);
213 * run-time version of trace_event_get_offsets_<call>() that returns the last
214 * accessible offset of trace fields excluding __dynamic_array bytes
216 int trace_event_get_offsets(struct trace_event_call *call)
218 struct ftrace_event_field *tail;
219 struct list_head *head;
221 head = trace_get_fields(call);
223 * head->next points to the last field with the largest offset,
224 * since it was added last by trace_define_field()
226 tail = list_first_entry(head, struct ftrace_event_field, link);
227 return tail->offset + tail->size;
231 * Check if the referenced field is an array and return true,
232 * as arrays are OK to dereference.
234 static bool test_field(const char *fmt, struct trace_event_call *call)
236 struct trace_event_fields *field = call->class->fields_array;
237 const char *array_descriptor;
241 if (!(len = str_has_prefix(fmt, "REC->")))
244 for (p = fmt; *p; p++) {
245 if (!isalnum(*p) && *p != '_')
250 for (; field->type; field++) {
251 if (strncmp(field->name, fmt, len) ||
254 array_descriptor = strchr(field->type, '[');
255 /* This is an array and is OK to dereference. */
256 return array_descriptor != NULL;
262 * Examine the print fmt of the event looking for unsafe dereference
263 * pointers using %p* that could be recorded in the trace event and
264 * much later referenced after the pointer was freed. Dereferencing
265 * pointers are OK, if it is dereferenced into the event itself.
267 static void test_event_printk(struct trace_event_call *call)
269 u64 dereference_flags = 0;
271 const char *fmt, *c, *r, *a;
278 fmt = call->print_fmt;
283 for (i = 0; fmt[i]; i++) {
293 * The print fmt starts with a string that
294 * is processed first to find %p* usage,
295 * then after the first string, the print fmt
296 * contains arguments that are used to check
297 * if the dereferenced %p* usage is safe.
306 * If there was no %p* uses
309 if (!dereference_flags)
314 if (in_quote == fmt[i])
321 if (!first || !in_quote)
330 /* Find dereferencing fields */
331 switch (fmt[i + 1]) {
332 case 'B': case 'R': case 'r':
333 case 'b': case 'M': case 'm':
334 case 'I': case 'i': case 'E':
335 case 'U': case 'V': case 'N':
336 case 'a': case 'd': case 'D':
337 case 'g': case 't': case 'C':
339 if (WARN_ONCE(arg == 63,
340 "Too many args for event: %s",
341 trace_event_name(call)))
343 dereference_flags |= 1ULL << arg;
351 /* Increment arg if %*s exists. */
352 for (j = 0; fmt[i + j]; j++) {
353 if (isdigit(fmt[i + j]) ||
356 if (fmt[i + j] == '*') {
360 if ((fmt[i + j] == 's') && star)
379 if (WARN_ONCE(parens < 0,
380 "Paren mismatch for event: %s\narg='%s'\n%*s",
381 trace_event_name(call),
383 (i - start_arg) + 5, "^"))
387 if (in_quote || parens)
390 while (isspace(fmt[i]))
393 if (!(dereference_flags & (1ULL << arg)))
396 /* Find the REC-> in the argument */
397 c = strchr(fmt + i, ',');
398 r = strstr(fmt + i, "REC->");
399 if (r && (!c || r < c)) {
401 * Addresses of events on the buffer,
402 * or an array on the buffer is
404 * There's ways to fool this, but
405 * this is to catch common mistakes,
406 * not malicious code.
408 a = strchr(fmt + i, '&');
409 if ((a && (a < r)) || test_field(r, call))
410 dereference_flags &= ~(1ULL << arg);
411 } else if ((r = strstr(fmt + i, "__get_dynamic_array(")) &&
413 dereference_flags &= ~(1ULL << arg);
414 } else if ((r = strstr(fmt + i, "__get_sockaddr(")) &&
416 dereference_flags &= ~(1ULL << arg);
426 * If you triggered the below warning, the trace event reported
427 * uses an unsafe dereference pointer %p*. As the data stored
428 * at the trace event time may no longer exist when the trace
429 * event is printed, dereferencing to the original source is
430 * unsafe. The source of the dereference must be copied into the
431 * event itself, and the dereference must access the copy instead.
433 if (WARN_ON_ONCE(dereference_flags)) {
435 while (!(dereference_flags & 1)) {
436 dereference_flags >>= 1;
439 pr_warn("event %s has unsafe dereference of argument %d\n",
440 trace_event_name(call), arg);
441 pr_warn("print_fmt: %s\n", fmt);
445 int trace_event_raw_init(struct trace_event_call *call)
449 id = register_trace_event(&call->event);
453 test_event_printk(call);
457 EXPORT_SYMBOL_GPL(trace_event_raw_init);
459 bool trace_event_ignore_this_pid(struct trace_event_file *trace_file)
461 struct trace_array *tr = trace_file->tr;
462 struct trace_array_cpu *data;
463 struct trace_pid_list *no_pid_list;
464 struct trace_pid_list *pid_list;
466 pid_list = rcu_dereference_raw(tr->filtered_pids);
467 no_pid_list = rcu_dereference_raw(tr->filtered_no_pids);
469 if (!pid_list && !no_pid_list)
472 data = this_cpu_ptr(tr->array_buffer.data);
474 return data->ignore_pid;
476 EXPORT_SYMBOL_GPL(trace_event_ignore_this_pid);
478 void *trace_event_buffer_reserve(struct trace_event_buffer *fbuffer,
479 struct trace_event_file *trace_file,
482 struct trace_event_call *event_call = trace_file->event_call;
484 if ((trace_file->flags & EVENT_FILE_FL_PID_FILTER) &&
485 trace_event_ignore_this_pid(trace_file))
489 * If CONFIG_PREEMPTION is enabled, then the tracepoint itself disables
490 * preemption (adding one to the preempt_count). Since we are
491 * interested in the preempt_count at the time the tracepoint was
492 * hit, we need to subtract one to offset the increment.
494 fbuffer->trace_ctx = tracing_gen_ctx_dec();
495 fbuffer->trace_file = trace_file;
498 trace_event_buffer_lock_reserve(&fbuffer->buffer, trace_file,
499 event_call->event.type, len,
504 fbuffer->regs = NULL;
505 fbuffer->entry = ring_buffer_event_data(fbuffer->event);
506 return fbuffer->entry;
508 EXPORT_SYMBOL_GPL(trace_event_buffer_reserve);
510 int trace_event_reg(struct trace_event_call *call,
511 enum trace_reg type, void *data)
513 struct trace_event_file *file = data;
515 WARN_ON(!(call->flags & TRACE_EVENT_FL_TRACEPOINT));
517 case TRACE_REG_REGISTER:
518 return tracepoint_probe_register(call->tp,
521 case TRACE_REG_UNREGISTER:
522 tracepoint_probe_unregister(call->tp,
527 #ifdef CONFIG_PERF_EVENTS
528 case TRACE_REG_PERF_REGISTER:
529 return tracepoint_probe_register(call->tp,
530 call->class->perf_probe,
532 case TRACE_REG_PERF_UNREGISTER:
533 tracepoint_probe_unregister(call->tp,
534 call->class->perf_probe,
537 case TRACE_REG_PERF_OPEN:
538 case TRACE_REG_PERF_CLOSE:
539 case TRACE_REG_PERF_ADD:
540 case TRACE_REG_PERF_DEL:
546 EXPORT_SYMBOL_GPL(trace_event_reg);
548 void trace_event_enable_cmd_record(bool enable)
550 struct trace_event_file *file;
551 struct trace_array *tr;
553 lockdep_assert_held(&event_mutex);
555 do_for_each_event_file(tr, file) {
557 if (!(file->flags & EVENT_FILE_FL_ENABLED))
561 tracing_start_cmdline_record();
562 set_bit(EVENT_FILE_FL_RECORDED_CMD_BIT, &file->flags);
564 tracing_stop_cmdline_record();
565 clear_bit(EVENT_FILE_FL_RECORDED_CMD_BIT, &file->flags);
567 } while_for_each_event_file();
570 void trace_event_enable_tgid_record(bool enable)
572 struct trace_event_file *file;
573 struct trace_array *tr;
575 lockdep_assert_held(&event_mutex);
577 do_for_each_event_file(tr, file) {
578 if (!(file->flags & EVENT_FILE_FL_ENABLED))
582 tracing_start_tgid_record();
583 set_bit(EVENT_FILE_FL_RECORDED_TGID_BIT, &file->flags);
585 tracing_stop_tgid_record();
586 clear_bit(EVENT_FILE_FL_RECORDED_TGID_BIT,
589 } while_for_each_event_file();
592 static int __ftrace_event_enable_disable(struct trace_event_file *file,
593 int enable, int soft_disable)
595 struct trace_event_call *call = file->event_call;
596 struct trace_array *tr = file->tr;
597 unsigned long file_flags = file->flags;
604 * When soft_disable is set and enable is cleared, the sm_ref
605 * reference counter is decremented. If it reaches 0, we want
606 * to clear the SOFT_DISABLED flag but leave the event in the
607 * state that it was. That is, if the event was enabled and
608 * SOFT_DISABLED isn't set, then do nothing. But if SOFT_DISABLED
609 * is set we do not want the event to be enabled before we
612 * When soft_disable is not set but the SOFT_MODE flag is,
613 * we do nothing. Do not disable the tracepoint, otherwise
614 * "soft enable"s (clearing the SOFT_DISABLED bit) wont work.
617 if (atomic_dec_return(&file->sm_ref) > 0)
619 disable = file->flags & EVENT_FILE_FL_SOFT_DISABLED;
620 clear_bit(EVENT_FILE_FL_SOFT_MODE_BIT, &file->flags);
622 disable = !(file->flags & EVENT_FILE_FL_SOFT_MODE);
624 if (disable && (file->flags & EVENT_FILE_FL_ENABLED)) {
625 clear_bit(EVENT_FILE_FL_ENABLED_BIT, &file->flags);
626 if (file->flags & EVENT_FILE_FL_RECORDED_CMD) {
627 tracing_stop_cmdline_record();
628 clear_bit(EVENT_FILE_FL_RECORDED_CMD_BIT, &file->flags);
631 if (file->flags & EVENT_FILE_FL_RECORDED_TGID) {
632 tracing_stop_tgid_record();
633 clear_bit(EVENT_FILE_FL_RECORDED_TGID_BIT, &file->flags);
636 call->class->reg(call, TRACE_REG_UNREGISTER, file);
638 /* If in SOFT_MODE, just set the SOFT_DISABLE_BIT, else clear it */
639 if (file->flags & EVENT_FILE_FL_SOFT_MODE)
640 set_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &file->flags);
642 clear_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &file->flags);
646 * When soft_disable is set and enable is set, we want to
647 * register the tracepoint for the event, but leave the event
648 * as is. That means, if the event was already enabled, we do
649 * nothing (but set SOFT_MODE). If the event is disabled, we
650 * set SOFT_DISABLED before enabling the event tracepoint, so
651 * it still seems to be disabled.
654 clear_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &file->flags);
656 if (atomic_inc_return(&file->sm_ref) > 1)
658 set_bit(EVENT_FILE_FL_SOFT_MODE_BIT, &file->flags);
661 if (!(file->flags & EVENT_FILE_FL_ENABLED)) {
662 bool cmd = false, tgid = false;
664 /* Keep the event disabled, when going to SOFT_MODE. */
666 set_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &file->flags);
668 if (tr->trace_flags & TRACE_ITER_RECORD_CMD) {
670 tracing_start_cmdline_record();
671 set_bit(EVENT_FILE_FL_RECORDED_CMD_BIT, &file->flags);
674 if (tr->trace_flags & TRACE_ITER_RECORD_TGID) {
676 tracing_start_tgid_record();
677 set_bit(EVENT_FILE_FL_RECORDED_TGID_BIT, &file->flags);
680 ret = call->class->reg(call, TRACE_REG_REGISTER, file);
683 tracing_stop_cmdline_record();
685 tracing_stop_tgid_record();
686 pr_info("event trace: Could not enable event "
687 "%s\n", trace_event_name(call));
690 set_bit(EVENT_FILE_FL_ENABLED_BIT, &file->flags);
692 /* WAS_ENABLED gets set but never cleared. */
693 set_bit(EVENT_FILE_FL_WAS_ENABLED_BIT, &file->flags);
698 /* Enable or disable use of trace_buffered_event */
699 if ((file_flags & EVENT_FILE_FL_SOFT_DISABLED) !=
700 (file->flags & EVENT_FILE_FL_SOFT_DISABLED)) {
701 if (file->flags & EVENT_FILE_FL_SOFT_DISABLED)
702 trace_buffered_event_enable();
704 trace_buffered_event_disable();
710 int trace_event_enable_disable(struct trace_event_file *file,
711 int enable, int soft_disable)
713 return __ftrace_event_enable_disable(file, enable, soft_disable);
716 static int ftrace_event_enable_disable(struct trace_event_file *file,
719 return __ftrace_event_enable_disable(file, enable, 0);
722 static void ftrace_clear_events(struct trace_array *tr)
724 struct trace_event_file *file;
726 mutex_lock(&event_mutex);
727 list_for_each_entry(file, &tr->events, list) {
728 ftrace_event_enable_disable(file, 0);
730 mutex_unlock(&event_mutex);
734 event_filter_pid_sched_process_exit(void *data, struct task_struct *task)
736 struct trace_pid_list *pid_list;
737 struct trace_array *tr = data;
739 pid_list = rcu_dereference_raw(tr->filtered_pids);
740 trace_filter_add_remove_task(pid_list, NULL, task);
742 pid_list = rcu_dereference_raw(tr->filtered_no_pids);
743 trace_filter_add_remove_task(pid_list, NULL, task);
747 event_filter_pid_sched_process_fork(void *data,
748 struct task_struct *self,
749 struct task_struct *task)
751 struct trace_pid_list *pid_list;
752 struct trace_array *tr = data;
754 pid_list = rcu_dereference_sched(tr->filtered_pids);
755 trace_filter_add_remove_task(pid_list, self, task);
757 pid_list = rcu_dereference_sched(tr->filtered_no_pids);
758 trace_filter_add_remove_task(pid_list, self, task);
761 void trace_event_follow_fork(struct trace_array *tr, bool enable)
764 register_trace_prio_sched_process_fork(event_filter_pid_sched_process_fork,
766 register_trace_prio_sched_process_free(event_filter_pid_sched_process_exit,
769 unregister_trace_sched_process_fork(event_filter_pid_sched_process_fork,
771 unregister_trace_sched_process_free(event_filter_pid_sched_process_exit,
777 event_filter_pid_sched_switch_probe_pre(void *data, bool preempt,
778 struct task_struct *prev,
779 struct task_struct *next,
780 unsigned int prev_state)
782 struct trace_array *tr = data;
783 struct trace_pid_list *no_pid_list;
784 struct trace_pid_list *pid_list;
787 pid_list = rcu_dereference_sched(tr->filtered_pids);
788 no_pid_list = rcu_dereference_sched(tr->filtered_no_pids);
791 * Sched switch is funny, as we only want to ignore it
792 * in the notrace case if both prev and next should be ignored.
794 ret = trace_ignore_this_task(NULL, no_pid_list, prev) &&
795 trace_ignore_this_task(NULL, no_pid_list, next);
797 this_cpu_write(tr->array_buffer.data->ignore_pid, ret ||
798 (trace_ignore_this_task(pid_list, NULL, prev) &&
799 trace_ignore_this_task(pid_list, NULL, next)));
803 event_filter_pid_sched_switch_probe_post(void *data, bool preempt,
804 struct task_struct *prev,
805 struct task_struct *next,
806 unsigned int prev_state)
808 struct trace_array *tr = data;
809 struct trace_pid_list *no_pid_list;
810 struct trace_pid_list *pid_list;
812 pid_list = rcu_dereference_sched(tr->filtered_pids);
813 no_pid_list = rcu_dereference_sched(tr->filtered_no_pids);
815 this_cpu_write(tr->array_buffer.data->ignore_pid,
816 trace_ignore_this_task(pid_list, no_pid_list, next));
820 event_filter_pid_sched_wakeup_probe_pre(void *data, struct task_struct *task)
822 struct trace_array *tr = data;
823 struct trace_pid_list *no_pid_list;
824 struct trace_pid_list *pid_list;
826 /* Nothing to do if we are already tracing */
827 if (!this_cpu_read(tr->array_buffer.data->ignore_pid))
830 pid_list = rcu_dereference_sched(tr->filtered_pids);
831 no_pid_list = rcu_dereference_sched(tr->filtered_no_pids);
833 this_cpu_write(tr->array_buffer.data->ignore_pid,
834 trace_ignore_this_task(pid_list, no_pid_list, task));
838 event_filter_pid_sched_wakeup_probe_post(void *data, struct task_struct *task)
840 struct trace_array *tr = data;
841 struct trace_pid_list *no_pid_list;
842 struct trace_pid_list *pid_list;
844 /* Nothing to do if we are not tracing */
845 if (this_cpu_read(tr->array_buffer.data->ignore_pid))
848 pid_list = rcu_dereference_sched(tr->filtered_pids);
849 no_pid_list = rcu_dereference_sched(tr->filtered_no_pids);
851 /* Set tracing if current is enabled */
852 this_cpu_write(tr->array_buffer.data->ignore_pid,
853 trace_ignore_this_task(pid_list, no_pid_list, current));
856 static void unregister_pid_events(struct trace_array *tr)
858 unregister_trace_sched_switch(event_filter_pid_sched_switch_probe_pre, tr);
859 unregister_trace_sched_switch(event_filter_pid_sched_switch_probe_post, tr);
861 unregister_trace_sched_wakeup(event_filter_pid_sched_wakeup_probe_pre, tr);
862 unregister_trace_sched_wakeup(event_filter_pid_sched_wakeup_probe_post, tr);
864 unregister_trace_sched_wakeup_new(event_filter_pid_sched_wakeup_probe_pre, tr);
865 unregister_trace_sched_wakeup_new(event_filter_pid_sched_wakeup_probe_post, tr);
867 unregister_trace_sched_waking(event_filter_pid_sched_wakeup_probe_pre, tr);
868 unregister_trace_sched_waking(event_filter_pid_sched_wakeup_probe_post, tr);
871 static void __ftrace_clear_event_pids(struct trace_array *tr, int type)
873 struct trace_pid_list *pid_list;
874 struct trace_pid_list *no_pid_list;
875 struct trace_event_file *file;
878 pid_list = rcu_dereference_protected(tr->filtered_pids,
879 lockdep_is_held(&event_mutex));
880 no_pid_list = rcu_dereference_protected(tr->filtered_no_pids,
881 lockdep_is_held(&event_mutex));
883 /* Make sure there's something to do */
884 if (!pid_type_enabled(type, pid_list, no_pid_list))
887 if (!still_need_pid_events(type, pid_list, no_pid_list)) {
888 unregister_pid_events(tr);
890 list_for_each_entry(file, &tr->events, list) {
891 clear_bit(EVENT_FILE_FL_PID_FILTER_BIT, &file->flags);
894 for_each_possible_cpu(cpu)
895 per_cpu_ptr(tr->array_buffer.data, cpu)->ignore_pid = false;
898 if (type & TRACE_PIDS)
899 rcu_assign_pointer(tr->filtered_pids, NULL);
901 if (type & TRACE_NO_PIDS)
902 rcu_assign_pointer(tr->filtered_no_pids, NULL);
904 /* Wait till all users are no longer using pid filtering */
905 tracepoint_synchronize_unregister();
907 if ((type & TRACE_PIDS) && pid_list)
908 trace_pid_list_free(pid_list);
910 if ((type & TRACE_NO_PIDS) && no_pid_list)
911 trace_pid_list_free(no_pid_list);
914 static void ftrace_clear_event_pids(struct trace_array *tr, int type)
916 mutex_lock(&event_mutex);
917 __ftrace_clear_event_pids(tr, type);
918 mutex_unlock(&event_mutex);
921 static void __put_system(struct event_subsystem *system)
923 struct event_filter *filter = system->filter;
925 WARN_ON_ONCE(system_refcount(system) == 0);
926 if (system_refcount_dec(system))
929 list_del(&system->list);
932 kfree(filter->filter_string);
935 kfree_const(system->name);
939 static void __get_system(struct event_subsystem *system)
941 WARN_ON_ONCE(system_refcount(system) == 0);
942 system_refcount_inc(system);
945 static void __get_system_dir(struct trace_subsystem_dir *dir)
947 WARN_ON_ONCE(dir->ref_count == 0);
949 __get_system(dir->subsystem);
952 static void __put_system_dir(struct trace_subsystem_dir *dir)
954 WARN_ON_ONCE(dir->ref_count == 0);
955 /* If the subsystem is about to be freed, the dir must be too */
956 WARN_ON_ONCE(system_refcount(dir->subsystem) == 1 && dir->ref_count != 1);
958 __put_system(dir->subsystem);
959 if (!--dir->ref_count)
963 static void put_system(struct trace_subsystem_dir *dir)
965 mutex_lock(&event_mutex);
966 __put_system_dir(dir);
967 mutex_unlock(&event_mutex);
970 static void remove_subsystem(struct trace_subsystem_dir *dir)
975 if (!--dir->nr_events) {
976 tracefs_remove(dir->entry);
977 list_del(&dir->list);
978 __put_system_dir(dir);
982 static void remove_event_file_dir(struct trace_event_file *file)
984 struct dentry *dir = file->dir;
985 struct dentry *child;
988 spin_lock(&dir->d_lock); /* probably unneeded */
989 list_for_each_entry(child, &dir->d_subdirs, d_child) {
990 if (d_really_is_positive(child)) /* probably unneeded */
991 d_inode(child)->i_private = NULL;
993 spin_unlock(&dir->d_lock);
998 list_del(&file->list);
999 remove_subsystem(file->system);
1000 free_event_filter(file->filter);
1001 kmem_cache_free(file_cachep, file);
1005 * __ftrace_set_clr_event(NULL, NULL, NULL, set) will set/unset all events.
1008 __ftrace_set_clr_event_nolock(struct trace_array *tr, const char *match,
1009 const char *sub, const char *event, int set)
1011 struct trace_event_file *file;
1012 struct trace_event_call *call;
1017 list_for_each_entry(file, &tr->events, list) {
1019 call = file->event_call;
1020 name = trace_event_name(call);
1022 if (!name || !call->class || !call->class->reg)
1025 if (call->flags & TRACE_EVENT_FL_IGNORE_ENABLE)
1029 strcmp(match, name) != 0 &&
1030 strcmp(match, call->class->system) != 0)
1033 if (sub && strcmp(sub, call->class->system) != 0)
1036 if (event && strcmp(event, name) != 0)
1039 ret = ftrace_event_enable_disable(file, set);
1042 * Save the first error and return that. Some events
1043 * may still have been enabled, but let the user
1044 * know that something went wrong.
1055 static int __ftrace_set_clr_event(struct trace_array *tr, const char *match,
1056 const char *sub, const char *event, int set)
1060 mutex_lock(&event_mutex);
1061 ret = __ftrace_set_clr_event_nolock(tr, match, sub, event, set);
1062 mutex_unlock(&event_mutex);
1067 int ftrace_set_clr_event(struct trace_array *tr, char *buf, int set)
1069 char *event = NULL, *sub = NULL, *match;
1075 * The buf format can be <subsystem>:<event-name>
1076 * *:<event-name> means any event by that name.
1077 * :<event-name> is the same.
1079 * <subsystem>:* means all events in that subsystem
1080 * <subsystem>: means the same.
1082 * <name> (no ':') means all events in a subsystem with
1083 * the name <name> or any event that matches <name>
1086 match = strsep(&buf, ":");
1092 if (!strlen(sub) || strcmp(sub, "*") == 0)
1094 if (!strlen(event) || strcmp(event, "*") == 0)
1098 ret = __ftrace_set_clr_event(tr, match, sub, event, set);
1100 /* Put back the colon to allow this to be called again */
1108 * trace_set_clr_event - enable or disable an event
1109 * @system: system name to match (NULL for any system)
1110 * @event: event name to match (NULL for all events, within system)
1111 * @set: 1 to enable, 0 to disable
1113 * This is a way for other parts of the kernel to enable or disable
1116 * Returns 0 on success, -EINVAL if the parameters do not match any
1117 * registered events.
1119 int trace_set_clr_event(const char *system, const char *event, int set)
1121 struct trace_array *tr = top_trace_array();
1126 return __ftrace_set_clr_event(tr, NULL, system, event, set);
1128 EXPORT_SYMBOL_GPL(trace_set_clr_event);
1131 * trace_array_set_clr_event - enable or disable an event for a trace array.
1132 * @tr: concerned trace array.
1133 * @system: system name to match (NULL for any system)
1134 * @event: event name to match (NULL for all events, within system)
1135 * @enable: true to enable, false to disable
1137 * This is a way for other parts of the kernel to enable or disable
1140 * Returns 0 on success, -EINVAL if the parameters do not match any
1141 * registered events.
1143 int trace_array_set_clr_event(struct trace_array *tr, const char *system,
1144 const char *event, bool enable)
1151 set = (enable == true) ? 1 : 0;
1152 return __ftrace_set_clr_event(tr, NULL, system, event, set);
1154 EXPORT_SYMBOL_GPL(trace_array_set_clr_event);
1156 /* 128 should be much more than enough */
1157 #define EVENT_BUF_SIZE 127
1160 ftrace_event_write(struct file *file, const char __user *ubuf,
1161 size_t cnt, loff_t *ppos)
1163 struct trace_parser parser;
1164 struct seq_file *m = file->private_data;
1165 struct trace_array *tr = m->private;
1171 ret = tracing_update_buffers();
1175 if (trace_parser_get_init(&parser, EVENT_BUF_SIZE + 1))
1178 read = trace_get_user(&parser, ubuf, cnt, ppos);
1180 if (read >= 0 && trace_parser_loaded((&parser))) {
1183 if (*parser.buffer == '!')
1186 ret = ftrace_set_clr_event(tr, parser.buffer + !set, set);
1194 trace_parser_put(&parser);
1200 t_next(struct seq_file *m, void *v, loff_t *pos)
1202 struct trace_event_file *file = v;
1203 struct trace_event_call *call;
1204 struct trace_array *tr = m->private;
1208 list_for_each_entry_continue(file, &tr->events, list) {
1209 call = file->event_call;
1211 * The ftrace subsystem is for showing formats only.
1212 * They can not be enabled or disabled via the event files.
1214 if (call->class && call->class->reg &&
1215 !(call->flags & TRACE_EVENT_FL_IGNORE_ENABLE))
1222 static void *t_start(struct seq_file *m, loff_t *pos)
1224 struct trace_event_file *file;
1225 struct trace_array *tr = m->private;
1228 mutex_lock(&event_mutex);
1230 file = list_entry(&tr->events, struct trace_event_file, list);
1231 for (l = 0; l <= *pos; ) {
1232 file = t_next(m, file, &l);
1240 s_next(struct seq_file *m, void *v, loff_t *pos)
1242 struct trace_event_file *file = v;
1243 struct trace_array *tr = m->private;
1247 list_for_each_entry_continue(file, &tr->events, list) {
1248 if (file->flags & EVENT_FILE_FL_ENABLED)
1255 static void *s_start(struct seq_file *m, loff_t *pos)
1257 struct trace_event_file *file;
1258 struct trace_array *tr = m->private;
1261 mutex_lock(&event_mutex);
1263 file = list_entry(&tr->events, struct trace_event_file, list);
1264 for (l = 0; l <= *pos; ) {
1265 file = s_next(m, file, &l);
1272 static int t_show(struct seq_file *m, void *v)
1274 struct trace_event_file *file = v;
1275 struct trace_event_call *call = file->event_call;
1277 if (strcmp(call->class->system, TRACE_SYSTEM) != 0)
1278 seq_printf(m, "%s:", call->class->system);
1279 seq_printf(m, "%s\n", trace_event_name(call));
1284 static void t_stop(struct seq_file *m, void *p)
1286 mutex_unlock(&event_mutex);
1290 __next(struct seq_file *m, void *v, loff_t *pos, int type)
1292 struct trace_array *tr = m->private;
1293 struct trace_pid_list *pid_list;
1295 if (type == TRACE_PIDS)
1296 pid_list = rcu_dereference_sched(tr->filtered_pids);
1298 pid_list = rcu_dereference_sched(tr->filtered_no_pids);
1300 return trace_pid_next(pid_list, v, pos);
1304 p_next(struct seq_file *m, void *v, loff_t *pos)
1306 return __next(m, v, pos, TRACE_PIDS);
1310 np_next(struct seq_file *m, void *v, loff_t *pos)
1312 return __next(m, v, pos, TRACE_NO_PIDS);
1315 static void *__start(struct seq_file *m, loff_t *pos, int type)
1318 struct trace_pid_list *pid_list;
1319 struct trace_array *tr = m->private;
1322 * Grab the mutex, to keep calls to p_next() having the same
1323 * tr->filtered_pids as p_start() has.
1324 * If we just passed the tr->filtered_pids around, then RCU would
1325 * have been enough, but doing that makes things more complex.
1327 mutex_lock(&event_mutex);
1328 rcu_read_lock_sched();
1330 if (type == TRACE_PIDS)
1331 pid_list = rcu_dereference_sched(tr->filtered_pids);
1333 pid_list = rcu_dereference_sched(tr->filtered_no_pids);
1338 return trace_pid_start(pid_list, pos);
1341 static void *p_start(struct seq_file *m, loff_t *pos)
1344 return __start(m, pos, TRACE_PIDS);
1347 static void *np_start(struct seq_file *m, loff_t *pos)
1350 return __start(m, pos, TRACE_NO_PIDS);
1353 static void p_stop(struct seq_file *m, void *p)
1356 rcu_read_unlock_sched();
1357 mutex_unlock(&event_mutex);
1361 event_enable_read(struct file *filp, char __user *ubuf, size_t cnt,
1364 struct trace_event_file *file;
1365 unsigned long flags;
1368 mutex_lock(&event_mutex);
1369 file = event_file_data(filp);
1371 flags = file->flags;
1372 mutex_unlock(&event_mutex);
1377 if (flags & EVENT_FILE_FL_ENABLED &&
1378 !(flags & EVENT_FILE_FL_SOFT_DISABLED))
1381 if (flags & EVENT_FILE_FL_SOFT_DISABLED ||
1382 flags & EVENT_FILE_FL_SOFT_MODE)
1387 return simple_read_from_buffer(ubuf, cnt, ppos, buf, strlen(buf));
1391 event_enable_write(struct file *filp, const char __user *ubuf, size_t cnt,
1394 struct trace_event_file *file;
1398 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
1402 ret = tracing_update_buffers();
1410 mutex_lock(&event_mutex);
1411 file = event_file_data(filp);
1413 ret = ftrace_event_enable_disable(file, val);
1414 mutex_unlock(&event_mutex);
1423 return ret ? ret : cnt;
1427 system_enable_read(struct file *filp, char __user *ubuf, size_t cnt,
1430 const char set_to_char[4] = { '?', '0', '1', 'X' };
1431 struct trace_subsystem_dir *dir = filp->private_data;
1432 struct event_subsystem *system = dir->subsystem;
1433 struct trace_event_call *call;
1434 struct trace_event_file *file;
1435 struct trace_array *tr = dir->tr;
1440 mutex_lock(&event_mutex);
1441 list_for_each_entry(file, &tr->events, list) {
1442 call = file->event_call;
1443 if ((call->flags & TRACE_EVENT_FL_IGNORE_ENABLE) ||
1444 !trace_event_name(call) || !call->class || !call->class->reg)
1447 if (system && strcmp(call->class->system, system->name) != 0)
1451 * We need to find out if all the events are set
1452 * or if all events or cleared, or if we have
1455 set |= (1 << !!(file->flags & EVENT_FILE_FL_ENABLED));
1458 * If we have a mixture, no need to look further.
1463 mutex_unlock(&event_mutex);
1465 buf[0] = set_to_char[set];
1468 ret = simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
1474 system_enable_write(struct file *filp, const char __user *ubuf, size_t cnt,
1477 struct trace_subsystem_dir *dir = filp->private_data;
1478 struct event_subsystem *system = dir->subsystem;
1479 const char *name = NULL;
1483 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
1487 ret = tracing_update_buffers();
1491 if (val != 0 && val != 1)
1495 * Opening of "enable" adds a ref count to system,
1496 * so the name is safe to use.
1499 name = system->name;
1501 ret = __ftrace_set_clr_event(dir->tr, NULL, name, NULL, val);
1515 FORMAT_FIELD_SEPERATOR = 2,
1516 FORMAT_PRINTFMT = 3,
1519 static void *f_next(struct seq_file *m, void *v, loff_t *pos)
1521 struct trace_event_call *call = event_file_data(m->private);
1522 struct list_head *common_head = &ftrace_common_fields;
1523 struct list_head *head = trace_get_fields(call);
1524 struct list_head *node = v;
1528 switch ((unsigned long)v) {
1533 case FORMAT_FIELD_SEPERATOR:
1537 case FORMAT_PRINTFMT:
1543 if (node == common_head)
1544 return (void *)FORMAT_FIELD_SEPERATOR;
1545 else if (node == head)
1546 return (void *)FORMAT_PRINTFMT;
1551 static int f_show(struct seq_file *m, void *v)
1553 struct trace_event_call *call = event_file_data(m->private);
1554 struct ftrace_event_field *field;
1555 const char *array_descriptor;
1557 switch ((unsigned long)v) {
1559 seq_printf(m, "name: %s\n", trace_event_name(call));
1560 seq_printf(m, "ID: %d\n", call->event.type);
1561 seq_puts(m, "format:\n");
1564 case FORMAT_FIELD_SEPERATOR:
1568 case FORMAT_PRINTFMT:
1569 seq_printf(m, "\nprint fmt: %s\n",
1574 field = list_entry(v, struct ftrace_event_field, link);
1576 * Smartly shows the array type(except dynamic array).
1579 * If TYPE := TYPE[LEN], it is shown:
1580 * field:TYPE VAR[LEN]
1582 array_descriptor = strchr(field->type, '[');
1584 if (str_has_prefix(field->type, "__data_loc"))
1585 array_descriptor = NULL;
1587 if (!array_descriptor)
1588 seq_printf(m, "\tfield:%s %s;\toffset:%u;\tsize:%u;\tsigned:%d;\n",
1589 field->type, field->name, field->offset,
1590 field->size, !!field->is_signed);
1592 seq_printf(m, "\tfield:%.*s %s%s;\toffset:%u;\tsize:%u;\tsigned:%d;\n",
1593 (int)(array_descriptor - field->type),
1594 field->type, field->name,
1595 array_descriptor, field->offset,
1596 field->size, !!field->is_signed);
1601 static void *f_start(struct seq_file *m, loff_t *pos)
1603 void *p = (void *)FORMAT_HEADER;
1606 /* ->stop() is called even if ->start() fails */
1607 mutex_lock(&event_mutex);
1608 if (!event_file_data(m->private))
1609 return ERR_PTR(-ENODEV);
1611 while (l < *pos && p)
1612 p = f_next(m, p, &l);
1617 static void f_stop(struct seq_file *m, void *p)
1619 mutex_unlock(&event_mutex);
1622 static const struct seq_operations trace_format_seq_ops = {
1629 static int trace_format_open(struct inode *inode, struct file *file)
1634 /* Do we want to hide event format files on tracefs lockdown? */
1636 ret = seq_open(file, &trace_format_seq_ops);
1640 m = file->private_data;
1647 event_id_read(struct file *filp, char __user *ubuf, size_t cnt, loff_t *ppos)
1649 int id = (long)event_file_data(filp);
1656 len = sprintf(buf, "%d\n", id);
1658 return simple_read_from_buffer(ubuf, cnt, ppos, buf, len);
1662 event_filter_read(struct file *filp, char __user *ubuf, size_t cnt,
1665 struct trace_event_file *file;
1666 struct trace_seq *s;
1672 s = kmalloc(sizeof(*s), GFP_KERNEL);
1679 mutex_lock(&event_mutex);
1680 file = event_file_data(filp);
1682 print_event_filter(file, s);
1683 mutex_unlock(&event_mutex);
1686 r = simple_read_from_buffer(ubuf, cnt, ppos,
1687 s->buffer, trace_seq_used(s));
1695 event_filter_write(struct file *filp, const char __user *ubuf, size_t cnt,
1698 struct trace_event_file *file;
1702 if (cnt >= PAGE_SIZE)
1705 buf = memdup_user_nul(ubuf, cnt);
1707 return PTR_ERR(buf);
1709 mutex_lock(&event_mutex);
1710 file = event_file_data(filp);
1712 err = apply_event_filter(file, buf);
1713 mutex_unlock(&event_mutex);
1724 static LIST_HEAD(event_subsystems);
1726 static int subsystem_open(struct inode *inode, struct file *filp)
1728 struct trace_subsystem_dir *dir = NULL, *iter_dir;
1729 struct trace_array *tr = NULL, *iter_tr;
1730 struct event_subsystem *system = NULL;
1733 if (tracing_is_disabled())
1736 /* Make sure the system still exists */
1737 mutex_lock(&event_mutex);
1738 mutex_lock(&trace_types_lock);
1739 list_for_each_entry(iter_tr, &ftrace_trace_arrays, list) {
1740 list_for_each_entry(iter_dir, &iter_tr->systems, list) {
1741 if (iter_dir == inode->i_private) {
1742 /* Don't open systems with no events */
1745 if (dir->nr_events) {
1746 __get_system_dir(dir);
1747 system = dir->subsystem;
1754 mutex_unlock(&trace_types_lock);
1755 mutex_unlock(&event_mutex);
1760 /* Still need to increment the ref count of the system */
1761 if (trace_array_get(tr) < 0) {
1766 ret = tracing_open_generic(inode, filp);
1768 trace_array_put(tr);
1775 static int system_tr_open(struct inode *inode, struct file *filp)
1777 struct trace_subsystem_dir *dir;
1778 struct trace_array *tr = inode->i_private;
1781 /* Make a temporary dir that has no system but points to tr */
1782 dir = kzalloc(sizeof(*dir), GFP_KERNEL);
1786 ret = tracing_open_generic_tr(inode, filp);
1792 filp->private_data = dir;
1797 static int subsystem_release(struct inode *inode, struct file *file)
1799 struct trace_subsystem_dir *dir = file->private_data;
1801 trace_array_put(dir->tr);
1804 * If dir->subsystem is NULL, then this is a temporary
1805 * descriptor that was made for a trace_array to enable
1817 subsystem_filter_read(struct file *filp, char __user *ubuf, size_t cnt,
1820 struct trace_subsystem_dir *dir = filp->private_data;
1821 struct event_subsystem *system = dir->subsystem;
1822 struct trace_seq *s;
1828 s = kmalloc(sizeof(*s), GFP_KERNEL);
1834 print_subsystem_event_filter(system, s);
1835 r = simple_read_from_buffer(ubuf, cnt, ppos,
1836 s->buffer, trace_seq_used(s));
1844 subsystem_filter_write(struct file *filp, const char __user *ubuf, size_t cnt,
1847 struct trace_subsystem_dir *dir = filp->private_data;
1851 if (cnt >= PAGE_SIZE)
1854 buf = memdup_user_nul(ubuf, cnt);
1856 return PTR_ERR(buf);
1858 err = apply_subsystem_event_filter(dir, buf);
1869 show_header(struct file *filp, char __user *ubuf, size_t cnt, loff_t *ppos)
1871 int (*func)(struct trace_seq *s) = filp->private_data;
1872 struct trace_seq *s;
1878 s = kmalloc(sizeof(*s), GFP_KERNEL);
1885 r = simple_read_from_buffer(ubuf, cnt, ppos,
1886 s->buffer, trace_seq_used(s));
1893 static void ignore_task_cpu(void *data)
1895 struct trace_array *tr = data;
1896 struct trace_pid_list *pid_list;
1897 struct trace_pid_list *no_pid_list;
1900 * This function is called by on_each_cpu() while the
1901 * event_mutex is held.
1903 pid_list = rcu_dereference_protected(tr->filtered_pids,
1904 mutex_is_locked(&event_mutex));
1905 no_pid_list = rcu_dereference_protected(tr->filtered_no_pids,
1906 mutex_is_locked(&event_mutex));
1908 this_cpu_write(tr->array_buffer.data->ignore_pid,
1909 trace_ignore_this_task(pid_list, no_pid_list, current));
1912 static void register_pid_events(struct trace_array *tr)
1915 * Register a probe that is called before all other probes
1916 * to set ignore_pid if next or prev do not match.
1917 * Register a probe this is called after all other probes
1918 * to only keep ignore_pid set if next pid matches.
1920 register_trace_prio_sched_switch(event_filter_pid_sched_switch_probe_pre,
1922 register_trace_prio_sched_switch(event_filter_pid_sched_switch_probe_post,
1925 register_trace_prio_sched_wakeup(event_filter_pid_sched_wakeup_probe_pre,
1927 register_trace_prio_sched_wakeup(event_filter_pid_sched_wakeup_probe_post,
1930 register_trace_prio_sched_wakeup_new(event_filter_pid_sched_wakeup_probe_pre,
1932 register_trace_prio_sched_wakeup_new(event_filter_pid_sched_wakeup_probe_post,
1935 register_trace_prio_sched_waking(event_filter_pid_sched_wakeup_probe_pre,
1937 register_trace_prio_sched_waking(event_filter_pid_sched_wakeup_probe_post,
1942 event_pid_write(struct file *filp, const char __user *ubuf,
1943 size_t cnt, loff_t *ppos, int type)
1945 struct seq_file *m = filp->private_data;
1946 struct trace_array *tr = m->private;
1947 struct trace_pid_list *filtered_pids = NULL;
1948 struct trace_pid_list *other_pids = NULL;
1949 struct trace_pid_list *pid_list;
1950 struct trace_event_file *file;
1956 ret = tracing_update_buffers();
1960 mutex_lock(&event_mutex);
1962 if (type == TRACE_PIDS) {
1963 filtered_pids = rcu_dereference_protected(tr->filtered_pids,
1964 lockdep_is_held(&event_mutex));
1965 other_pids = rcu_dereference_protected(tr->filtered_no_pids,
1966 lockdep_is_held(&event_mutex));
1968 filtered_pids = rcu_dereference_protected(tr->filtered_no_pids,
1969 lockdep_is_held(&event_mutex));
1970 other_pids = rcu_dereference_protected(tr->filtered_pids,
1971 lockdep_is_held(&event_mutex));
1974 ret = trace_pid_write(filtered_pids, &pid_list, ubuf, cnt);
1978 if (type == TRACE_PIDS)
1979 rcu_assign_pointer(tr->filtered_pids, pid_list);
1981 rcu_assign_pointer(tr->filtered_no_pids, pid_list);
1983 list_for_each_entry(file, &tr->events, list) {
1984 set_bit(EVENT_FILE_FL_PID_FILTER_BIT, &file->flags);
1987 if (filtered_pids) {
1988 tracepoint_synchronize_unregister();
1989 trace_pid_list_free(filtered_pids);
1990 } else if (pid_list && !other_pids) {
1991 register_pid_events(tr);
1995 * Ignoring of pids is done at task switch. But we have to
1996 * check for those tasks that are currently running.
1997 * Always do this in case a pid was appended or removed.
1999 on_each_cpu(ignore_task_cpu, tr, 1);
2002 mutex_unlock(&event_mutex);
2011 ftrace_event_pid_write(struct file *filp, const char __user *ubuf,
2012 size_t cnt, loff_t *ppos)
2014 return event_pid_write(filp, ubuf, cnt, ppos, TRACE_PIDS);
2018 ftrace_event_npid_write(struct file *filp, const char __user *ubuf,
2019 size_t cnt, loff_t *ppos)
2021 return event_pid_write(filp, ubuf, cnt, ppos, TRACE_NO_PIDS);
2024 static int ftrace_event_avail_open(struct inode *inode, struct file *file);
2025 static int ftrace_event_set_open(struct inode *inode, struct file *file);
2026 static int ftrace_event_set_pid_open(struct inode *inode, struct file *file);
2027 static int ftrace_event_set_npid_open(struct inode *inode, struct file *file);
2028 static int ftrace_event_release(struct inode *inode, struct file *file);
2030 static const struct seq_operations show_event_seq_ops = {
2037 static const struct seq_operations show_set_event_seq_ops = {
2044 static const struct seq_operations show_set_pid_seq_ops = {
2047 .show = trace_pid_show,
2051 static const struct seq_operations show_set_no_pid_seq_ops = {
2054 .show = trace_pid_show,
2058 static const struct file_operations ftrace_avail_fops = {
2059 .open = ftrace_event_avail_open,
2061 .llseek = seq_lseek,
2062 .release = seq_release,
2065 static const struct file_operations ftrace_set_event_fops = {
2066 .open = ftrace_event_set_open,
2068 .write = ftrace_event_write,
2069 .llseek = seq_lseek,
2070 .release = ftrace_event_release,
2073 static const struct file_operations ftrace_set_event_pid_fops = {
2074 .open = ftrace_event_set_pid_open,
2076 .write = ftrace_event_pid_write,
2077 .llseek = seq_lseek,
2078 .release = ftrace_event_release,
2081 static const struct file_operations ftrace_set_event_notrace_pid_fops = {
2082 .open = ftrace_event_set_npid_open,
2084 .write = ftrace_event_npid_write,
2085 .llseek = seq_lseek,
2086 .release = ftrace_event_release,
2089 static const struct file_operations ftrace_enable_fops = {
2090 .open = tracing_open_generic,
2091 .read = event_enable_read,
2092 .write = event_enable_write,
2093 .llseek = default_llseek,
2096 static const struct file_operations ftrace_event_format_fops = {
2097 .open = trace_format_open,
2099 .llseek = seq_lseek,
2100 .release = seq_release,
2103 static const struct file_operations ftrace_event_id_fops = {
2104 .read = event_id_read,
2105 .llseek = default_llseek,
2108 static const struct file_operations ftrace_event_filter_fops = {
2109 .open = tracing_open_generic,
2110 .read = event_filter_read,
2111 .write = event_filter_write,
2112 .llseek = default_llseek,
2115 static const struct file_operations ftrace_subsystem_filter_fops = {
2116 .open = subsystem_open,
2117 .read = subsystem_filter_read,
2118 .write = subsystem_filter_write,
2119 .llseek = default_llseek,
2120 .release = subsystem_release,
2123 static const struct file_operations ftrace_system_enable_fops = {
2124 .open = subsystem_open,
2125 .read = system_enable_read,
2126 .write = system_enable_write,
2127 .llseek = default_llseek,
2128 .release = subsystem_release,
2131 static const struct file_operations ftrace_tr_enable_fops = {
2132 .open = system_tr_open,
2133 .read = system_enable_read,
2134 .write = system_enable_write,
2135 .llseek = default_llseek,
2136 .release = subsystem_release,
2139 static const struct file_operations ftrace_show_header_fops = {
2140 .open = tracing_open_generic,
2141 .read = show_header,
2142 .llseek = default_llseek,
2146 ftrace_event_open(struct inode *inode, struct file *file,
2147 const struct seq_operations *seq_ops)
2152 ret = security_locked_down(LOCKDOWN_TRACEFS);
2156 ret = seq_open(file, seq_ops);
2159 m = file->private_data;
2160 /* copy tr over to seq ops */
2161 m->private = inode->i_private;
2166 static int ftrace_event_release(struct inode *inode, struct file *file)
2168 struct trace_array *tr = inode->i_private;
2170 trace_array_put(tr);
2172 return seq_release(inode, file);
2176 ftrace_event_avail_open(struct inode *inode, struct file *file)
2178 const struct seq_operations *seq_ops = &show_event_seq_ops;
2180 /* Checks for tracefs lockdown */
2181 return ftrace_event_open(inode, file, seq_ops);
2185 ftrace_event_set_open(struct inode *inode, struct file *file)
2187 const struct seq_operations *seq_ops = &show_set_event_seq_ops;
2188 struct trace_array *tr = inode->i_private;
2191 ret = tracing_check_open_get_tr(tr);
2195 if ((file->f_mode & FMODE_WRITE) &&
2196 (file->f_flags & O_TRUNC))
2197 ftrace_clear_events(tr);
2199 ret = ftrace_event_open(inode, file, seq_ops);
2201 trace_array_put(tr);
2206 ftrace_event_set_pid_open(struct inode *inode, struct file *file)
2208 const struct seq_operations *seq_ops = &show_set_pid_seq_ops;
2209 struct trace_array *tr = inode->i_private;
2212 ret = tracing_check_open_get_tr(tr);
2216 if ((file->f_mode & FMODE_WRITE) &&
2217 (file->f_flags & O_TRUNC))
2218 ftrace_clear_event_pids(tr, TRACE_PIDS);
2220 ret = ftrace_event_open(inode, file, seq_ops);
2222 trace_array_put(tr);
2227 ftrace_event_set_npid_open(struct inode *inode, struct file *file)
2229 const struct seq_operations *seq_ops = &show_set_no_pid_seq_ops;
2230 struct trace_array *tr = inode->i_private;
2233 ret = tracing_check_open_get_tr(tr);
2237 if ((file->f_mode & FMODE_WRITE) &&
2238 (file->f_flags & O_TRUNC))
2239 ftrace_clear_event_pids(tr, TRACE_NO_PIDS);
2241 ret = ftrace_event_open(inode, file, seq_ops);
2243 trace_array_put(tr);
2247 static struct event_subsystem *
2248 create_new_subsystem(const char *name)
2250 struct event_subsystem *system;
2252 /* need to create new entry */
2253 system = kmalloc(sizeof(*system), GFP_KERNEL);
2257 system->ref_count = 1;
2259 /* Only allocate if dynamic (kprobes and modules) */
2260 system->name = kstrdup_const(name, GFP_KERNEL);
2264 system->filter = NULL;
2266 system->filter = kzalloc(sizeof(struct event_filter), GFP_KERNEL);
2267 if (!system->filter)
2270 list_add(&system->list, &event_subsystems);
2275 kfree_const(system->name);
2280 static struct dentry *
2281 event_subsystem_dir(struct trace_array *tr, const char *name,
2282 struct trace_event_file *file, struct dentry *parent)
2284 struct event_subsystem *system, *iter;
2285 struct trace_subsystem_dir *dir;
2286 struct dentry *entry;
2288 /* First see if we did not already create this dir */
2289 list_for_each_entry(dir, &tr->systems, list) {
2290 system = dir->subsystem;
2291 if (strcmp(system->name, name) == 0) {
2298 /* Now see if the system itself exists. */
2300 list_for_each_entry(iter, &event_subsystems, list) {
2301 if (strcmp(iter->name, name) == 0) {
2307 dir = kmalloc(sizeof(*dir), GFP_KERNEL);
2312 system = create_new_subsystem(name);
2316 __get_system(system);
2318 dir->entry = tracefs_create_dir(name, parent);
2320 pr_warn("Failed to create system directory %s\n", name);
2321 __put_system(system);
2328 dir->subsystem = system;
2331 /* the ftrace system is special, do not create enable or filter files */
2332 if (strcmp(name, "ftrace") != 0) {
2334 entry = tracefs_create_file("filter", TRACE_MODE_WRITE,
2336 &ftrace_subsystem_filter_fops);
2338 kfree(system->filter);
2339 system->filter = NULL;
2340 pr_warn("Could not create tracefs '%s/filter' entry\n", name);
2343 trace_create_file("enable", TRACE_MODE_WRITE, dir->entry, dir,
2344 &ftrace_system_enable_fops);
2347 list_add(&dir->list, &tr->systems);
2354 /* Only print this message if failed on memory allocation */
2355 if (!dir || !system)
2356 pr_warn("No memory to create event subsystem %s\n", name);
2361 event_define_fields(struct trace_event_call *call)
2363 struct list_head *head;
2367 * Other events may have the same class. Only update
2368 * the fields if they are not already defined.
2370 head = trace_get_fields(call);
2371 if (list_empty(head)) {
2372 struct trace_event_fields *field = call->class->fields_array;
2373 unsigned int offset = sizeof(struct trace_entry);
2375 for (; field->type; field++) {
2376 if (field->type == TRACE_FUNCTION_TYPE) {
2377 field->define_fields(call);
2381 offset = ALIGN(offset, field->align);
2382 ret = trace_define_field(call, field->type, field->name,
2383 offset, field->size,
2384 field->is_signed, field->filter_type);
2385 if (WARN_ON_ONCE(ret)) {
2386 pr_err("error code is %d\n", ret);
2390 offset += field->size;
2398 event_create_dir(struct dentry *parent, struct trace_event_file *file)
2400 struct trace_event_call *call = file->event_call;
2401 struct trace_array *tr = file->tr;
2402 struct dentry *d_events;
2407 * If the trace point header did not define TRACE_SYSTEM
2408 * then the system would be called "TRACE_SYSTEM".
2410 if (strcmp(call->class->system, TRACE_SYSTEM) != 0) {
2411 d_events = event_subsystem_dir(tr, call->class->system, file, parent);
2417 name = trace_event_name(call);
2418 file->dir = tracefs_create_dir(name, d_events);
2420 pr_warn("Could not create tracefs '%s' directory\n", name);
2424 if (call->class->reg && !(call->flags & TRACE_EVENT_FL_IGNORE_ENABLE))
2425 trace_create_file("enable", TRACE_MODE_WRITE, file->dir, file,
2426 &ftrace_enable_fops);
2428 #ifdef CONFIG_PERF_EVENTS
2429 if (call->event.type && call->class->reg)
2430 trace_create_file("id", TRACE_MODE_READ, file->dir,
2431 (void *)(long)call->event.type,
2432 &ftrace_event_id_fops);
2435 ret = event_define_fields(call);
2437 pr_warn("Could not initialize trace point events/%s\n", name);
2442 * Only event directories that can be enabled should have
2443 * triggers or filters.
2445 if (!(call->flags & TRACE_EVENT_FL_IGNORE_ENABLE)) {
2446 trace_create_file("filter", TRACE_MODE_WRITE, file->dir,
2447 file, &ftrace_event_filter_fops);
2449 trace_create_file("trigger", TRACE_MODE_WRITE, file->dir,
2450 file, &event_trigger_fops);
2453 #ifdef CONFIG_HIST_TRIGGERS
2454 trace_create_file("hist", TRACE_MODE_READ, file->dir, file,
2457 #ifdef CONFIG_HIST_TRIGGERS_DEBUG
2458 trace_create_file("hist_debug", TRACE_MODE_READ, file->dir, file,
2459 &event_hist_debug_fops);
2461 trace_create_file("format", TRACE_MODE_READ, file->dir, call,
2462 &ftrace_event_format_fops);
2464 #ifdef CONFIG_TRACE_EVENT_INJECT
2465 if (call->event.type && call->class->reg)
2466 trace_create_file("inject", 0200, file->dir, file,
2467 &event_inject_fops);
2473 static void remove_event_from_tracers(struct trace_event_call *call)
2475 struct trace_event_file *file;
2476 struct trace_array *tr;
2478 do_for_each_event_file_safe(tr, file) {
2479 if (file->event_call != call)
2482 remove_event_file_dir(file);
2484 * The do_for_each_event_file_safe() is
2485 * a double loop. After finding the call for this
2486 * trace_array, we use break to jump to the next
2490 } while_for_each_event_file();
2493 static void event_remove(struct trace_event_call *call)
2495 struct trace_array *tr;
2496 struct trace_event_file *file;
2498 do_for_each_event_file(tr, file) {
2499 if (file->event_call != call)
2502 if (file->flags & EVENT_FILE_FL_WAS_ENABLED)
2503 tr->clear_trace = true;
2505 ftrace_event_enable_disable(file, 0);
2507 * The do_for_each_event_file() is
2508 * a double loop. After finding the call for this
2509 * trace_array, we use break to jump to the next
2513 } while_for_each_event_file();
2515 if (call->event.funcs)
2516 __unregister_trace_event(&call->event);
2517 remove_event_from_tracers(call);
2518 list_del(&call->list);
2521 static int event_init(struct trace_event_call *call)
2526 name = trace_event_name(call);
2530 if (call->class->raw_init) {
2531 ret = call->class->raw_init(call);
2532 if (ret < 0 && ret != -ENOSYS)
2533 pr_warn("Could not initialize trace events/%s\n", name);
2540 __register_event(struct trace_event_call *call, struct module *mod)
2544 ret = event_init(call);
2548 list_add(&call->list, &ftrace_events);
2549 if (call->flags & TRACE_EVENT_FL_DYNAMIC)
2550 atomic_set(&call->refcnt, 0);
2557 static char *eval_replace(char *ptr, struct trace_eval_map *map, int len)
2562 /* Find the length of the eval value as a string */
2563 elen = snprintf(ptr, 0, "%ld", map->eval_value);
2564 /* Make sure there's enough room to replace the string with the value */
2568 snprintf(ptr, elen + 1, "%ld", map->eval_value);
2570 /* Get the rest of the string of ptr */
2571 rlen = strlen(ptr + len);
2572 memmove(ptr + elen, ptr + len, rlen);
2573 /* Make sure we end the new string */
2574 ptr[elen + rlen] = 0;
2579 static void update_event_printk(struct trace_event_call *call,
2580 struct trace_eval_map *map)
2584 int len = strlen(map->eval_string);
2586 for (ptr = call->print_fmt; *ptr; ptr++) {
2600 if (isdigit(*ptr)) {
2604 /* Check for alpha chars like ULL */
2605 } while (isalnum(*ptr));
2609 * A number must have some kind of delimiter after
2610 * it, and we can ignore that too.
2614 if (isalpha(*ptr) || *ptr == '_') {
2615 if (strncmp(map->eval_string, ptr, len) == 0 &&
2616 !isalnum(ptr[len]) && ptr[len] != '_') {
2617 ptr = eval_replace(ptr, map, len);
2618 /* enum/sizeof string smaller than value */
2619 if (WARN_ON_ONCE(!ptr))
2622 * No need to decrement here, as eval_replace()
2623 * returns the pointer to the character passed
2624 * the eval, and two evals can not be placed
2625 * back to back without something in between.
2626 * We can skip that something in between.
2633 } while (isalnum(*ptr) || *ptr == '_');
2637 * If what comes after this variable is a '.' or
2638 * '->' then we can continue to ignore that string.
2640 if (*ptr == '.' || (ptr[0] == '-' && ptr[1] == '>')) {
2641 ptr += *ptr == '.' ? 1 : 2;
2647 * Once again, we can skip the delimiter that came
2655 static void add_str_to_module(struct module *module, char *str)
2657 struct module_string *modstr;
2659 modstr = kmalloc(sizeof(*modstr), GFP_KERNEL);
2662 * If we failed to allocate memory here, then we'll just
2663 * let the str memory leak when the module is removed.
2664 * If this fails to allocate, there's worse problems than
2665 * a leaked string on module removal.
2667 if (WARN_ON_ONCE(!modstr))
2670 modstr->module = module;
2673 list_add(&modstr->next, &module_strings);
2676 static void update_event_fields(struct trace_event_call *call,
2677 struct trace_eval_map *map)
2679 struct ftrace_event_field *field;
2680 struct list_head *head;
2683 int len = strlen(map->eval_string);
2685 /* Dynamic events should never have field maps */
2686 if (WARN_ON_ONCE(call->flags & TRACE_EVENT_FL_DYNAMIC))
2689 head = trace_get_fields(call);
2690 list_for_each_entry(field, head, link) {
2691 ptr = strchr(field->type, '[');
2696 if (!isalpha(*ptr) && *ptr != '_')
2699 if (strncmp(map->eval_string, ptr, len) != 0)
2702 str = kstrdup(field->type, GFP_KERNEL);
2703 if (WARN_ON_ONCE(!str))
2705 ptr = str + (ptr - field->type);
2706 ptr = eval_replace(ptr, map, len);
2707 /* enum/sizeof string smaller than value */
2708 if (WARN_ON_ONCE(!ptr)) {
2714 * If the event is part of a module, then we need to free the string
2715 * when the module is removed. Otherwise, it will stay allocated
2719 add_str_to_module(call->module, str);
2725 void trace_event_eval_update(struct trace_eval_map **map, int len)
2727 struct trace_event_call *call, *p;
2728 const char *last_system = NULL;
2733 down_write(&trace_event_sem);
2734 list_for_each_entry_safe(call, p, &ftrace_events, list) {
2735 /* events are usually grouped together with systems */
2736 if (!last_system || call->class->system != last_system) {
2739 last_system = call->class->system;
2743 * Since calls are grouped by systems, the likelihood that the
2744 * next call in the iteration belongs to the same system as the
2745 * previous call is high. As an optimization, we skip searching
2746 * for a map[] that matches the call's system if the last call
2747 * was from the same system. That's what last_i is for. If the
2748 * call has the same system as the previous call, then last_i
2749 * will be the index of the first map[] that has a matching
2752 for (i = last_i; i < len; i++) {
2753 if (call->class->system == map[i]->system) {
2754 /* Save the first system if need be */
2759 update_event_printk(call, map[i]);
2760 update_event_fields(call, map[i]);
2764 up_write(&trace_event_sem);
2767 static struct trace_event_file *
2768 trace_create_new_event(struct trace_event_call *call,
2769 struct trace_array *tr)
2771 struct trace_pid_list *no_pid_list;
2772 struct trace_pid_list *pid_list;
2773 struct trace_event_file *file;
2776 file = kmem_cache_alloc(file_cachep, GFP_TRACE);
2780 pid_list = rcu_dereference_protected(tr->filtered_pids,
2781 lockdep_is_held(&event_mutex));
2782 no_pid_list = rcu_dereference_protected(tr->filtered_no_pids,
2783 lockdep_is_held(&event_mutex));
2785 if (!trace_pid_list_first(pid_list, &first) ||
2786 !trace_pid_list_first(no_pid_list, &first))
2787 file->flags |= EVENT_FILE_FL_PID_FILTER;
2789 file->event_call = call;
2791 atomic_set(&file->sm_ref, 0);
2792 atomic_set(&file->tm_ref, 0);
2793 INIT_LIST_HEAD(&file->triggers);
2794 list_add(&file->list, &tr->events);
2799 /* Add an event to a trace directory */
2801 __trace_add_new_event(struct trace_event_call *call, struct trace_array *tr)
2803 struct trace_event_file *file;
2805 file = trace_create_new_event(call, tr);
2809 if (eventdir_initialized)
2810 return event_create_dir(tr->event_dir, file);
2812 return event_define_fields(call);
2816 * Just create a descriptor for early init. A descriptor is required
2817 * for enabling events at boot. We want to enable events before
2818 * the filesystem is initialized.
2821 __trace_early_add_new_event(struct trace_event_call *call,
2822 struct trace_array *tr)
2824 struct trace_event_file *file;
2826 file = trace_create_new_event(call, tr);
2830 return event_define_fields(call);
2833 struct ftrace_module_file_ops;
2834 static void __add_event_to_tracers(struct trace_event_call *call);
2836 /* Add an additional event_call dynamically */
2837 int trace_add_event_call(struct trace_event_call *call)
2840 lockdep_assert_held(&event_mutex);
2842 mutex_lock(&trace_types_lock);
2844 ret = __register_event(call, NULL);
2846 __add_event_to_tracers(call);
2848 mutex_unlock(&trace_types_lock);
2851 EXPORT_SYMBOL_GPL(trace_add_event_call);
2854 * Must be called under locking of trace_types_lock, event_mutex and
2857 static void __trace_remove_event_call(struct trace_event_call *call)
2860 trace_destroy_fields(call);
2861 free_event_filter(call->filter);
2862 call->filter = NULL;
2865 static int probe_remove_event_call(struct trace_event_call *call)
2867 struct trace_array *tr;
2868 struct trace_event_file *file;
2870 #ifdef CONFIG_PERF_EVENTS
2871 if (call->perf_refcount)
2874 do_for_each_event_file(tr, file) {
2875 if (file->event_call != call)
2878 * We can't rely on ftrace_event_enable_disable(enable => 0)
2879 * we are going to do, EVENT_FILE_FL_SOFT_MODE can suppress
2880 * TRACE_REG_UNREGISTER.
2882 if (file->flags & EVENT_FILE_FL_ENABLED)
2885 * The do_for_each_event_file_safe() is
2886 * a double loop. After finding the call for this
2887 * trace_array, we use break to jump to the next
2891 } while_for_each_event_file();
2893 __trace_remove_event_call(call);
2898 /* Remove an event_call */
2899 int trace_remove_event_call(struct trace_event_call *call)
2903 lockdep_assert_held(&event_mutex);
2905 mutex_lock(&trace_types_lock);
2906 down_write(&trace_event_sem);
2907 ret = probe_remove_event_call(call);
2908 up_write(&trace_event_sem);
2909 mutex_unlock(&trace_types_lock);
2913 EXPORT_SYMBOL_GPL(trace_remove_event_call);
2915 #define for_each_event(event, start, end) \
2916 for (event = start; \
2917 (unsigned long)event < (unsigned long)end; \
2920 #ifdef CONFIG_MODULES
2922 static void trace_module_add_events(struct module *mod)
2924 struct trace_event_call **call, **start, **end;
2926 if (!mod->num_trace_events)
2929 /* Don't add infrastructure for mods without tracepoints */
2930 if (trace_module_has_bad_taint(mod)) {
2931 pr_err("%s: module has bad taint, not creating trace events\n",
2936 start = mod->trace_events;
2937 end = mod->trace_events + mod->num_trace_events;
2939 for_each_event(call, start, end) {
2940 __register_event(*call, mod);
2941 __add_event_to_tracers(*call);
2945 static void trace_module_remove_events(struct module *mod)
2947 struct trace_event_call *call, *p;
2948 struct module_string *modstr, *m;
2950 down_write(&trace_event_sem);
2951 list_for_each_entry_safe(call, p, &ftrace_events, list) {
2952 if ((call->flags & TRACE_EVENT_FL_DYNAMIC) || !call->module)
2954 if (call->module == mod)
2955 __trace_remove_event_call(call);
2957 /* Check for any strings allocade for this module */
2958 list_for_each_entry_safe(modstr, m, &module_strings, next) {
2959 if (modstr->module != mod)
2961 list_del(&modstr->next);
2965 up_write(&trace_event_sem);
2968 * It is safest to reset the ring buffer if the module being unloaded
2969 * registered any events that were used. The only worry is if
2970 * a new module gets loaded, and takes on the same id as the events
2971 * of this module. When printing out the buffer, traced events left
2972 * over from this module may be passed to the new module events and
2973 * unexpected results may occur.
2975 tracing_reset_all_online_cpus();
2978 static int trace_module_notify(struct notifier_block *self,
2979 unsigned long val, void *data)
2981 struct module *mod = data;
2983 mutex_lock(&event_mutex);
2984 mutex_lock(&trace_types_lock);
2986 case MODULE_STATE_COMING:
2987 trace_module_add_events(mod);
2989 case MODULE_STATE_GOING:
2990 trace_module_remove_events(mod);
2993 mutex_unlock(&trace_types_lock);
2994 mutex_unlock(&event_mutex);
2999 static struct notifier_block trace_module_nb = {
3000 .notifier_call = trace_module_notify,
3001 .priority = 1, /* higher than trace.c module notify */
3003 #endif /* CONFIG_MODULES */
3005 /* Create a new event directory structure for a trace directory. */
3007 __trace_add_event_dirs(struct trace_array *tr)
3009 struct trace_event_call *call;
3012 list_for_each_entry(call, &ftrace_events, list) {
3013 ret = __trace_add_new_event(call, tr);
3015 pr_warn("Could not create directory for event %s\n",
3016 trace_event_name(call));
3020 /* Returns any file that matches the system and event */
3021 struct trace_event_file *
3022 __find_event_file(struct trace_array *tr, const char *system, const char *event)
3024 struct trace_event_file *file;
3025 struct trace_event_call *call;
3028 list_for_each_entry(file, &tr->events, list) {
3030 call = file->event_call;
3031 name = trace_event_name(call);
3033 if (!name || !call->class)
3036 if (strcmp(event, name) == 0 &&
3037 strcmp(system, call->class->system) == 0)
3043 /* Returns valid trace event files that match system and event */
3044 struct trace_event_file *
3045 find_event_file(struct trace_array *tr, const char *system, const char *event)
3047 struct trace_event_file *file;
3049 file = __find_event_file(tr, system, event);
3050 if (!file || !file->event_call->class->reg ||
3051 file->event_call->flags & TRACE_EVENT_FL_IGNORE_ENABLE)
3058 * trace_get_event_file - Find and return a trace event file
3059 * @instance: The name of the trace instance containing the event
3060 * @system: The name of the system containing the event
3061 * @event: The name of the event
3063 * Return a trace event file given the trace instance name, trace
3064 * system, and trace event name. If the instance name is NULL, it
3065 * refers to the top-level trace array.
3067 * This function will look it up and return it if found, after calling
3068 * trace_array_get() to prevent the instance from going away, and
3069 * increment the event's module refcount to prevent it from being
3072 * To release the file, call trace_put_event_file(), which will call
3073 * trace_array_put() and decrement the event's module refcount.
3075 * Return: The trace event on success, ERR_PTR otherwise.
3077 struct trace_event_file *trace_get_event_file(const char *instance,
3081 struct trace_array *tr = top_trace_array();
3082 struct trace_event_file *file = NULL;
3086 tr = trace_array_find_get(instance);
3088 return ERR_PTR(-ENOENT);
3090 ret = trace_array_get(tr);
3092 return ERR_PTR(ret);
3095 mutex_lock(&event_mutex);
3097 file = find_event_file(tr, system, event);
3099 trace_array_put(tr);
3104 /* Don't let event modules unload while in use */
3105 ret = trace_event_try_get_ref(file->event_call);
3107 trace_array_put(tr);
3114 mutex_unlock(&event_mutex);
3117 file = ERR_PTR(ret);
3121 EXPORT_SYMBOL_GPL(trace_get_event_file);
3124 * trace_put_event_file - Release a file from trace_get_event_file()
3125 * @file: The trace event file
3127 * If a file was retrieved using trace_get_event_file(), this should
3128 * be called when it's no longer needed. It will cancel the previous
3129 * trace_array_get() called by that function, and decrement the
3130 * event's module refcount.
3132 void trace_put_event_file(struct trace_event_file *file)
3134 mutex_lock(&event_mutex);
3135 trace_event_put_ref(file->event_call);
3136 mutex_unlock(&event_mutex);
3138 trace_array_put(file->tr);
3140 EXPORT_SYMBOL_GPL(trace_put_event_file);
3142 #ifdef CONFIG_DYNAMIC_FTRACE
3145 #define ENABLE_EVENT_STR "enable_event"
3146 #define DISABLE_EVENT_STR "disable_event"
3148 struct event_probe_data {
3149 struct trace_event_file *file;
3150 unsigned long count;
3155 static void update_event_probe(struct event_probe_data *data)
3158 clear_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &data->file->flags);
3160 set_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &data->file->flags);
3164 event_enable_probe(unsigned long ip, unsigned long parent_ip,
3165 struct trace_array *tr, struct ftrace_probe_ops *ops,
3168 struct ftrace_func_mapper *mapper = data;
3169 struct event_probe_data *edata;
3172 pdata = ftrace_func_mapper_find_ip(mapper, ip);
3173 if (!pdata || !*pdata)
3177 update_event_probe(edata);
3181 event_enable_count_probe(unsigned long ip, unsigned long parent_ip,
3182 struct trace_array *tr, struct ftrace_probe_ops *ops,
3185 struct ftrace_func_mapper *mapper = data;
3186 struct event_probe_data *edata;
3189 pdata = ftrace_func_mapper_find_ip(mapper, ip);
3190 if (!pdata || !*pdata)
3198 /* Skip if the event is in a state we want to switch to */
3199 if (edata->enable == !(edata->file->flags & EVENT_FILE_FL_SOFT_DISABLED))
3202 if (edata->count != -1)
3205 update_event_probe(edata);
3209 event_enable_print(struct seq_file *m, unsigned long ip,
3210 struct ftrace_probe_ops *ops, void *data)
3212 struct ftrace_func_mapper *mapper = data;
3213 struct event_probe_data *edata;
3216 pdata = ftrace_func_mapper_find_ip(mapper, ip);
3218 if (WARN_ON_ONCE(!pdata || !*pdata))
3223 seq_printf(m, "%ps:", (void *)ip);
3225 seq_printf(m, "%s:%s:%s",
3226 edata->enable ? ENABLE_EVENT_STR : DISABLE_EVENT_STR,
3227 edata->file->event_call->class->system,
3228 trace_event_name(edata->file->event_call));
3230 if (edata->count == -1)
3231 seq_puts(m, ":unlimited\n");
3233 seq_printf(m, ":count=%ld\n", edata->count);
3239 event_enable_init(struct ftrace_probe_ops *ops, struct trace_array *tr,
3240 unsigned long ip, void *init_data, void **data)
3242 struct ftrace_func_mapper *mapper = *data;
3243 struct event_probe_data *edata = init_data;
3247 mapper = allocate_ftrace_func_mapper();
3253 ret = ftrace_func_mapper_add_ip(mapper, ip, edata);
3262 static int free_probe_data(void *data)
3264 struct event_probe_data *edata = data;
3268 /* Remove the SOFT_MODE flag */
3269 __ftrace_event_enable_disable(edata->file, 0, 1);
3270 trace_event_put_ref(edata->file->event_call);
3277 event_enable_free(struct ftrace_probe_ops *ops, struct trace_array *tr,
3278 unsigned long ip, void *data)
3280 struct ftrace_func_mapper *mapper = data;
3281 struct event_probe_data *edata;
3286 free_ftrace_func_mapper(mapper, free_probe_data);
3290 edata = ftrace_func_mapper_remove_ip(mapper, ip);
3292 if (WARN_ON_ONCE(!edata))
3295 if (WARN_ON_ONCE(edata->ref <= 0))
3298 free_probe_data(edata);
3301 static struct ftrace_probe_ops event_enable_probe_ops = {
3302 .func = event_enable_probe,
3303 .print = event_enable_print,
3304 .init = event_enable_init,
3305 .free = event_enable_free,
3308 static struct ftrace_probe_ops event_enable_count_probe_ops = {
3309 .func = event_enable_count_probe,
3310 .print = event_enable_print,
3311 .init = event_enable_init,
3312 .free = event_enable_free,
3315 static struct ftrace_probe_ops event_disable_probe_ops = {
3316 .func = event_enable_probe,
3317 .print = event_enable_print,
3318 .init = event_enable_init,
3319 .free = event_enable_free,
3322 static struct ftrace_probe_ops event_disable_count_probe_ops = {
3323 .func = event_enable_count_probe,
3324 .print = event_enable_print,
3325 .init = event_enable_init,
3326 .free = event_enable_free,
3330 event_enable_func(struct trace_array *tr, struct ftrace_hash *hash,
3331 char *glob, char *cmd, char *param, int enabled)
3333 struct trace_event_file *file;
3334 struct ftrace_probe_ops *ops;
3335 struct event_probe_data *data;
3345 /* hash funcs only work with set_ftrace_filter */
3346 if (!enabled || !param)
3349 system = strsep(¶m, ":");
3353 event = strsep(¶m, ":");
3355 mutex_lock(&event_mutex);
3358 file = find_event_file(tr, system, event);
3362 enable = strcmp(cmd, ENABLE_EVENT_STR) == 0;
3365 ops = param ? &event_enable_count_probe_ops : &event_enable_probe_ops;
3367 ops = param ? &event_disable_count_probe_ops : &event_disable_probe_ops;
3369 if (glob[0] == '!') {
3370 ret = unregister_ftrace_function_probe_func(glob+1, tr, ops);
3376 data = kzalloc(sizeof(*data), GFP_KERNEL);
3380 data->enable = enable;
3387 number = strsep(¶m, ":");
3390 if (!strlen(number))
3394 * We use the callback data field (which is a pointer)
3397 ret = kstrtoul(number, 0, &data->count);
3402 /* Don't let event modules unload while probe registered */
3403 ret = trace_event_try_get_ref(file->event_call);
3409 ret = __ftrace_event_enable_disable(file, 1, 1);
3413 ret = register_ftrace_function_probe(glob, tr, ops, data);
3415 * The above returns on success the # of functions enabled,
3416 * but if it didn't find any functions it returns zero.
3417 * Consider no functions a failure too.
3424 /* Just return zero, not the number of enabled functions */
3427 mutex_unlock(&event_mutex);
3431 __ftrace_event_enable_disable(file, 0, 1);
3433 trace_event_put_ref(file->event_call);
3439 static struct ftrace_func_command event_enable_cmd = {
3440 .name = ENABLE_EVENT_STR,
3441 .func = event_enable_func,
3444 static struct ftrace_func_command event_disable_cmd = {
3445 .name = DISABLE_EVENT_STR,
3446 .func = event_enable_func,
3449 static __init int register_event_cmds(void)
3453 ret = register_ftrace_command(&event_enable_cmd);
3454 if (WARN_ON(ret < 0))
3456 ret = register_ftrace_command(&event_disable_cmd);
3457 if (WARN_ON(ret < 0))
3458 unregister_ftrace_command(&event_enable_cmd);
3462 static inline int register_event_cmds(void) { return 0; }
3463 #endif /* CONFIG_DYNAMIC_FTRACE */
3466 * The top level array and trace arrays created by boot-time tracing
3467 * have already had its trace_event_file descriptors created in order
3468 * to allow for early events to be recorded.
3469 * This function is called after the tracefs has been initialized,
3470 * and we now have to create the files associated to the events.
3472 static void __trace_early_add_event_dirs(struct trace_array *tr)
3474 struct trace_event_file *file;
3478 list_for_each_entry(file, &tr->events, list) {
3479 ret = event_create_dir(tr->event_dir, file);
3481 pr_warn("Could not create directory for event %s\n",
3482 trace_event_name(file->event_call));
3487 * For early boot up, the top trace array and the trace arrays created
3488 * by boot-time tracing require to have a list of events that can be
3489 * enabled. This must be done before the filesystem is set up in order
3490 * to allow events to be traced early.
3492 void __trace_early_add_events(struct trace_array *tr)
3494 struct trace_event_call *call;
3497 list_for_each_entry(call, &ftrace_events, list) {
3498 /* Early boot up should not have any modules loaded */
3499 if (!(call->flags & TRACE_EVENT_FL_DYNAMIC) &&
3500 WARN_ON_ONCE(call->module))
3503 ret = __trace_early_add_new_event(call, tr);
3505 pr_warn("Could not create early event %s\n",
3506 trace_event_name(call));
3510 /* Remove the event directory structure for a trace directory. */
3512 __trace_remove_event_dirs(struct trace_array *tr)
3514 struct trace_event_file *file, *next;
3516 list_for_each_entry_safe(file, next, &tr->events, list)
3517 remove_event_file_dir(file);
3520 static void __add_event_to_tracers(struct trace_event_call *call)
3522 struct trace_array *tr;
3524 list_for_each_entry(tr, &ftrace_trace_arrays, list)
3525 __trace_add_new_event(call, tr);
3528 extern struct trace_event_call *__start_ftrace_events[];
3529 extern struct trace_event_call *__stop_ftrace_events[];
3531 static char bootup_event_buf[COMMAND_LINE_SIZE] __initdata;
3533 static __init int setup_trace_event(char *str)
3535 strlcpy(bootup_event_buf, str, COMMAND_LINE_SIZE);
3536 ring_buffer_expanded = true;
3537 disable_tracing_selftest("running event tracing");
3541 __setup("trace_event=", setup_trace_event);
3543 /* Expects to have event_mutex held when called */
3545 create_event_toplevel_files(struct dentry *parent, struct trace_array *tr)
3547 struct dentry *d_events;
3548 struct dentry *entry;
3550 entry = trace_create_file("set_event", TRACE_MODE_WRITE, parent,
3551 tr, &ftrace_set_event_fops);
3555 d_events = tracefs_create_dir("events", parent);
3557 pr_warn("Could not create tracefs 'events' directory\n");
3561 entry = trace_create_file("enable", TRACE_MODE_WRITE, d_events,
3562 tr, &ftrace_tr_enable_fops);
3566 /* There are not as crucial, just warn if they are not created */
3568 trace_create_file("set_event_pid", TRACE_MODE_WRITE, parent,
3569 tr, &ftrace_set_event_pid_fops);
3571 trace_create_file("set_event_notrace_pid",
3572 TRACE_MODE_WRITE, parent, tr,
3573 &ftrace_set_event_notrace_pid_fops);
3575 /* ring buffer internal formats */
3576 trace_create_file("header_page", TRACE_MODE_READ, d_events,
3577 ring_buffer_print_page_header,
3578 &ftrace_show_header_fops);
3580 trace_create_file("header_event", TRACE_MODE_READ, d_events,
3581 ring_buffer_print_entry_header,
3582 &ftrace_show_header_fops);
3584 tr->event_dir = d_events;
3590 * event_trace_add_tracer - add a instance of a trace_array to events
3591 * @parent: The parent dentry to place the files/directories for events in
3592 * @tr: The trace array associated with these events
3594 * When a new instance is created, it needs to set up its events
3595 * directory, as well as other files associated with events. It also
3596 * creates the event hierarchy in the @parent/events directory.
3598 * Returns 0 on success.
3600 * Must be called with event_mutex held.
3602 int event_trace_add_tracer(struct dentry *parent, struct trace_array *tr)
3606 lockdep_assert_held(&event_mutex);
3608 ret = create_event_toplevel_files(parent, tr);
3612 down_write(&trace_event_sem);
3613 /* If tr already has the event list, it is initialized in early boot. */
3614 if (unlikely(!list_empty(&tr->events)))
3615 __trace_early_add_event_dirs(tr);
3617 __trace_add_event_dirs(tr);
3618 up_write(&trace_event_sem);
3625 * The top trace array already had its file descriptors created.
3626 * Now the files themselves need to be created.
3629 early_event_add_tracer(struct dentry *parent, struct trace_array *tr)
3633 mutex_lock(&event_mutex);
3635 ret = create_event_toplevel_files(parent, tr);
3639 down_write(&trace_event_sem);
3640 __trace_early_add_event_dirs(tr);
3641 up_write(&trace_event_sem);
3644 mutex_unlock(&event_mutex);
3649 /* Must be called with event_mutex held */
3650 int event_trace_del_tracer(struct trace_array *tr)
3652 lockdep_assert_held(&event_mutex);
3654 /* Disable any event triggers and associated soft-disabled events */
3655 clear_event_triggers(tr);
3657 /* Clear the pid list */
3658 __ftrace_clear_event_pids(tr, TRACE_PIDS | TRACE_NO_PIDS);
3660 /* Disable any running events */
3661 __ftrace_set_clr_event_nolock(tr, NULL, NULL, NULL, 0);
3663 /* Make sure no more events are being executed */
3664 tracepoint_synchronize_unregister();
3666 down_write(&trace_event_sem);
3667 __trace_remove_event_dirs(tr);
3668 tracefs_remove(tr->event_dir);
3669 up_write(&trace_event_sem);
3671 tr->event_dir = NULL;
3676 static __init int event_trace_memsetup(void)
3678 field_cachep = KMEM_CACHE(ftrace_event_field, SLAB_PANIC);
3679 file_cachep = KMEM_CACHE(trace_event_file, SLAB_PANIC);
3684 early_enable_events(struct trace_array *tr, bool disable_first)
3686 char *buf = bootup_event_buf;
3691 token = strsep(&buf, ",");
3697 /* Restarting syscalls requires that we stop them first */
3699 ftrace_set_clr_event(tr, token, 0);
3701 ret = ftrace_set_clr_event(tr, token, 1);
3703 pr_warn("Failed to enable trace event: %s\n", token);
3706 /* Put back the comma to allow this to be called again */
3712 static __init int event_trace_enable(void)
3714 struct trace_array *tr = top_trace_array();
3715 struct trace_event_call **iter, *call;
3721 for_each_event(iter, __start_ftrace_events, __stop_ftrace_events) {
3724 ret = event_init(call);
3726 list_add(&call->list, &ftrace_events);
3730 * We need the top trace array to have a working set of trace
3731 * points at early init, before the debug files and directories
3732 * are created. Create the file entries now, and attach them
3733 * to the actual file dentries later.
3735 __trace_early_add_events(tr);
3737 early_enable_events(tr, false);
3739 trace_printk_start_comm();
3741 register_event_cmds();
3743 register_trigger_cmds();
3749 * event_trace_enable() is called from trace_event_init() first to
3750 * initialize events and perhaps start any events that are on the
3751 * command line. Unfortunately, there are some events that will not
3752 * start this early, like the system call tracepoints that need
3753 * to set the %SYSCALL_WORK_SYSCALL_TRACEPOINT flag of pid 1. But
3754 * event_trace_enable() is called before pid 1 starts, and this flag
3755 * is never set, making the syscall tracepoint never get reached, but
3756 * the event is enabled regardless (and not doing anything).
3758 static __init int event_trace_enable_again(void)
3760 struct trace_array *tr;
3762 tr = top_trace_array();
3766 early_enable_events(tr, true);
3771 early_initcall(event_trace_enable_again);
3773 /* Init fields which doesn't related to the tracefs */
3774 static __init int event_trace_init_fields(void)
3776 if (trace_define_generic_fields())
3777 pr_warn("tracing: Failed to allocated generic fields");
3779 if (trace_define_common_fields())
3780 pr_warn("tracing: Failed to allocate common fields");
3785 __init int event_trace_init(void)
3787 struct trace_array *tr;
3790 tr = top_trace_array();
3794 trace_create_file("available_events", TRACE_MODE_READ,
3795 NULL, tr, &ftrace_avail_fops);
3797 ret = early_event_add_tracer(NULL, tr);
3801 #ifdef CONFIG_MODULES
3802 ret = register_module_notifier(&trace_module_nb);
3804 pr_warn("Failed to register trace events module notifier\n");
3807 eventdir_initialized = true;
3812 void __init trace_event_init(void)
3814 event_trace_memsetup();
3815 init_ftrace_syscalls();
3816 event_trace_enable();
3817 event_trace_init_fields();
3820 #ifdef CONFIG_EVENT_TRACE_STARTUP_TEST
3822 static DEFINE_SPINLOCK(test_spinlock);
3823 static DEFINE_SPINLOCK(test_spinlock_irq);
3824 static DEFINE_MUTEX(test_mutex);
3826 static __init void test_work(struct work_struct *dummy)
3828 spin_lock(&test_spinlock);
3829 spin_lock_irq(&test_spinlock_irq);
3831 spin_unlock_irq(&test_spinlock_irq);
3832 spin_unlock(&test_spinlock);
3834 mutex_lock(&test_mutex);
3836 mutex_unlock(&test_mutex);
3839 static __init int event_test_thread(void *unused)
3843 test_malloc = kmalloc(1234, GFP_KERNEL);
3845 pr_info("failed to kmalloc\n");
3847 schedule_on_each_cpu(test_work);
3851 set_current_state(TASK_INTERRUPTIBLE);
3852 while (!kthread_should_stop()) {
3854 set_current_state(TASK_INTERRUPTIBLE);
3856 __set_current_state(TASK_RUNNING);
3862 * Do various things that may trigger events.
3864 static __init void event_test_stuff(void)
3866 struct task_struct *test_thread;
3868 test_thread = kthread_run(event_test_thread, NULL, "test-events");
3870 kthread_stop(test_thread);
3874 * For every trace event defined, we will test each trace point separately,
3875 * and then by groups, and finally all trace points.
3877 static __init void event_trace_self_tests(void)
3879 struct trace_subsystem_dir *dir;
3880 struct trace_event_file *file;
3881 struct trace_event_call *call;
3882 struct event_subsystem *system;
3883 struct trace_array *tr;
3886 tr = top_trace_array();
3890 pr_info("Running tests on trace events:\n");
3892 list_for_each_entry(file, &tr->events, list) {
3894 call = file->event_call;
3896 /* Only test those that have a probe */
3897 if (!call->class || !call->class->probe)
3901 * Testing syscall events here is pretty useless, but
3902 * we still do it if configured. But this is time consuming.
3903 * What we really need is a user thread to perform the
3904 * syscalls as we test.
3906 #ifndef CONFIG_EVENT_TRACE_TEST_SYSCALLS
3907 if (call->class->system &&
3908 strcmp(call->class->system, "syscalls") == 0)
3912 pr_info("Testing event %s: ", trace_event_name(call));
3915 * If an event is already enabled, someone is using
3916 * it and the self test should not be on.
3918 if (file->flags & EVENT_FILE_FL_ENABLED) {
3919 pr_warn("Enabled event during self test!\n");
3924 ftrace_event_enable_disable(file, 1);
3926 ftrace_event_enable_disable(file, 0);
3931 /* Now test at the sub system level */
3933 pr_info("Running tests on trace event systems:\n");
3935 list_for_each_entry(dir, &tr->systems, list) {
3937 system = dir->subsystem;
3939 /* the ftrace system is special, skip it */
3940 if (strcmp(system->name, "ftrace") == 0)
3943 pr_info("Testing event system %s: ", system->name);
3945 ret = __ftrace_set_clr_event(tr, NULL, system->name, NULL, 1);
3946 if (WARN_ON_ONCE(ret)) {
3947 pr_warn("error enabling system %s\n",
3954 ret = __ftrace_set_clr_event(tr, NULL, system->name, NULL, 0);
3955 if (WARN_ON_ONCE(ret)) {
3956 pr_warn("error disabling system %s\n",
3964 /* Test with all events enabled */
3966 pr_info("Running tests on all trace events:\n");
3967 pr_info("Testing all events: ");
3969 ret = __ftrace_set_clr_event(tr, NULL, NULL, NULL, 1);
3970 if (WARN_ON_ONCE(ret)) {
3971 pr_warn("error enabling all events\n");
3978 ret = __ftrace_set_clr_event(tr, NULL, NULL, NULL, 0);
3979 if (WARN_ON_ONCE(ret)) {
3980 pr_warn("error disabling all events\n");
3987 #ifdef CONFIG_FUNCTION_TRACER
3989 static DEFINE_PER_CPU(atomic_t, ftrace_test_event_disable);
3991 static struct trace_event_file event_trace_file __initdata;
3994 function_test_events_call(unsigned long ip, unsigned long parent_ip,
3995 struct ftrace_ops *op, struct ftrace_regs *regs)
3997 struct trace_buffer *buffer;
3998 struct ring_buffer_event *event;
3999 struct ftrace_entry *entry;
4000 unsigned int trace_ctx;
4004 trace_ctx = tracing_gen_ctx();
4005 preempt_disable_notrace();
4006 cpu = raw_smp_processor_id();
4007 disabled = atomic_inc_return(&per_cpu(ftrace_test_event_disable, cpu));
4012 event = trace_event_buffer_lock_reserve(&buffer, &event_trace_file,
4013 TRACE_FN, sizeof(*entry),
4017 entry = ring_buffer_event_data(event);
4019 entry->parent_ip = parent_ip;
4021 event_trigger_unlock_commit(&event_trace_file, buffer, event,
4024 atomic_dec(&per_cpu(ftrace_test_event_disable, cpu));
4025 preempt_enable_notrace();
4028 static struct ftrace_ops trace_ops __initdata =
4030 .func = function_test_events_call,
4033 static __init void event_trace_self_test_with_function(void)
4037 event_trace_file.tr = top_trace_array();
4038 if (WARN_ON(!event_trace_file.tr))
4041 ret = register_ftrace_function(&trace_ops);
4042 if (WARN_ON(ret < 0)) {
4043 pr_info("Failed to enable function tracer for event tests\n");
4046 pr_info("Running tests again, along with the function tracer\n");
4047 event_trace_self_tests();
4048 unregister_ftrace_function(&trace_ops);
4051 static __init void event_trace_self_test_with_function(void)
4056 static __init int event_trace_self_tests_init(void)
4058 if (!tracing_selftest_disabled) {
4059 event_trace_self_tests();
4060 event_trace_self_test_with_function();
4066 late_initcall(event_trace_self_tests_init);