1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * event tracer
4 *
5 * Copyright (C) 2008 Red Hat Inc, Steven Rostedt <srostedt@redhat.com>
6 *
7 * - Added format output of fields of the trace point.
8 * This was based off of work by Tom Zanussi <tzanussi@gmail.com>.
9 *
10 */
11
12 #define pr_fmt(fmt) fmt
13
14 #include <linux/workqueue.h>
15 #include <linux/security.h>
16 #include <linux/spinlock.h>
17 #include <linux/kthread.h>
18 #include <linux/tracefs.h>
19 #include <linux/uaccess.h>
20 #include <linux/module.h>
21 #include <linux/ctype.h>
22 #include <linux/sort.h>
23 #include <linux/slab.h>
24 #include <linux/delay.h>
25
26 #include <trace/events/sched.h>
27 #include <trace/syscall.h>
28
29 #include <asm/setup.h>
30
31 #include "trace_output.h"
32
33 #undef TRACE_SYSTEM
34 #define TRACE_SYSTEM "TRACE_SYSTEM"
35
36 DEFINE_MUTEX(event_mutex);
37
38 LIST_HEAD(ftrace_events);
39 static LIST_HEAD(ftrace_generic_fields);
40 static LIST_HEAD(ftrace_common_fields);
41 static bool eventdir_initialized;
42
43 static LIST_HEAD(module_strings);
44
45 struct module_string {
46 struct list_head next;
47 struct module *module;
48 char *str;
49 };
50
51 #define GFP_TRACE (GFP_KERNEL | __GFP_ZERO)
52
53 static struct kmem_cache *field_cachep;
54 static struct kmem_cache *file_cachep;
55
system_refcount(struct event_subsystem * system)56 static inline int system_refcount(struct event_subsystem *system)
57 {
58 return system->ref_count;
59 }
60
system_refcount_inc(struct event_subsystem * system)61 static int system_refcount_inc(struct event_subsystem *system)
62 {
63 return system->ref_count++;
64 }
65
system_refcount_dec(struct event_subsystem * system)66 static int system_refcount_dec(struct event_subsystem *system)
67 {
68 return --system->ref_count;
69 }
70
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)
75
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)
80
81 #define while_for_each_event_file() \
82 }
83
84 static struct ftrace_event_field *
__find_event_field(struct list_head * head,const char * name)85 __find_event_field(struct list_head *head, const char *name)
86 {
87 struct ftrace_event_field *field;
88
89 list_for_each_entry(field, head, link) {
90 if (!strcmp(field->name, name))
91 return field;
92 }
93
94 return NULL;
95 }
96
97 struct ftrace_event_field *
trace_find_event_field(struct trace_event_call * call,char * name)98 trace_find_event_field(struct trace_event_call *call, char *name)
99 {
100 struct ftrace_event_field *field;
101 struct list_head *head;
102
103 head = trace_get_fields(call);
104 field = __find_event_field(head, name);
105 if (field)
106 return field;
107
108 field = __find_event_field(&ftrace_generic_fields, name);
109 if (field)
110 return field;
111
112 return __find_event_field(&ftrace_common_fields, name);
113 }
114
__trace_define_field(struct list_head * head,const char * type,const char * name,int offset,int size,int is_signed,int filter_type,int len,int need_test)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, int len,
118 int need_test)
119 {
120 struct ftrace_event_field *field;
121
122 field = kmem_cache_alloc(field_cachep, GFP_TRACE);
123 if (!field)
124 return -ENOMEM;
125
126 field->name = name;
127 field->type = type;
128
129 if (filter_type == FILTER_OTHER)
130 field->filter_type = filter_assign_type(type);
131 else
132 field->filter_type = filter_type;
133
134 field->offset = offset;
135 field->size = size;
136 field->is_signed = is_signed;
137 field->needs_test = need_test;
138 field->len = len;
139
140 list_add(&field->link, head);
141
142 return 0;
143 }
144
trace_define_field(struct trace_event_call * call,const char * type,const char * name,int offset,int size,int is_signed,int filter_type)145 int trace_define_field(struct trace_event_call *call, const char *type,
146 const char *name, int offset, int size, int is_signed,
147 int filter_type)
148 {
149 struct list_head *head;
150
151 if (WARN_ON(!call->class))
152 return 0;
153
154 head = trace_get_fields(call);
155 return __trace_define_field(head, type, name, offset, size,
156 is_signed, filter_type, 0, 0);
157 }
158 EXPORT_SYMBOL_GPL(trace_define_field);
159
trace_define_field_ext(struct trace_event_call * call,const char * type,const char * name,int offset,int size,int is_signed,int filter_type,int len,int need_test)160 static int trace_define_field_ext(struct trace_event_call *call, const char *type,
161 const char *name, int offset, int size, int is_signed,
162 int filter_type, int len, int need_test)
163 {
164 struct list_head *head;
165
166 if (WARN_ON(!call->class))
167 return 0;
168
169 head = trace_get_fields(call);
170 return __trace_define_field(head, type, name, offset, size,
171 is_signed, filter_type, len, need_test);
172 }
173
174 #define __generic_field(type, item, filter_type) \
175 ret = __trace_define_field(&ftrace_generic_fields, #type, \
176 #item, 0, 0, is_signed_type(type), \
177 filter_type, 0, 0); \
178 if (ret) \
179 return ret;
180
181 #define __common_field(type, item) \
182 ret = __trace_define_field(&ftrace_common_fields, #type, \
183 "common_" #item, \
184 offsetof(typeof(ent), item), \
185 sizeof(ent.item), \
186 is_signed_type(type), FILTER_OTHER, \
187 0, 0); \
188 if (ret) \
189 return ret;
190
trace_define_generic_fields(void)191 static int trace_define_generic_fields(void)
192 {
193 int ret;
194
195 __generic_field(int, CPU, FILTER_CPU);
196 __generic_field(int, cpu, FILTER_CPU);
197 __generic_field(int, common_cpu, FILTER_CPU);
198 __generic_field(char *, COMM, FILTER_COMM);
199 __generic_field(char *, comm, FILTER_COMM);
200 __generic_field(char *, stacktrace, FILTER_STACKTRACE);
201 __generic_field(char *, STACKTRACE, FILTER_STACKTRACE);
202
203 return ret;
204 }
205
trace_define_common_fields(void)206 static int trace_define_common_fields(void)
207 {
208 int ret;
209 struct trace_entry ent;
210
211 __common_field(unsigned short, type);
212 __common_field(unsigned char, flags);
213 /* Holds both preempt_count and migrate_disable */
214 __common_field(unsigned char, preempt_count);
215 __common_field(int, pid);
216
217 return ret;
218 }
219
trace_destroy_fields(struct trace_event_call * call)220 static void trace_destroy_fields(struct trace_event_call *call)
221 {
222 struct ftrace_event_field *field, *next;
223 struct list_head *head;
224
225 head = trace_get_fields(call);
226 list_for_each_entry_safe(field, next, head, link) {
227 list_del(&field->link);
228 kmem_cache_free(field_cachep, field);
229 }
230 }
231
232 /*
233 * run-time version of trace_event_get_offsets_<call>() that returns the last
234 * accessible offset of trace fields excluding __dynamic_array bytes
235 */
trace_event_get_offsets(struct trace_event_call * call)236 int trace_event_get_offsets(struct trace_event_call *call)
237 {
238 struct ftrace_event_field *tail;
239 struct list_head *head;
240
241 head = trace_get_fields(call);
242 /*
243 * head->next points to the last field with the largest offset,
244 * since it was added last by trace_define_field()
245 */
246 tail = list_first_entry(head, struct ftrace_event_field, link);
247 return tail->offset + tail->size;
248 }
249
250
find_event_field(const char * fmt,struct trace_event_call * call)251 static struct trace_event_fields *find_event_field(const char *fmt,
252 struct trace_event_call *call)
253 {
254 struct trace_event_fields *field = call->class->fields_array;
255 const char *p = fmt;
256 int len;
257
258 if (!(len = str_has_prefix(fmt, "REC->")))
259 return NULL;
260 fmt += len;
261 for (p = fmt; *p; p++) {
262 if (!isalnum(*p) && *p != '_')
263 break;
264 }
265 len = p - fmt;
266
267 for (; field->type; field++) {
268 if (strncmp(field->name, fmt, len) || field->name[len])
269 continue;
270
271 return field;
272 }
273 return NULL;
274 }
275
276 /*
277 * Check if the referenced field is an array and return true,
278 * as arrays are OK to dereference.
279 */
test_field(const char * fmt,struct trace_event_call * call)280 static bool test_field(const char *fmt, struct trace_event_call *call)
281 {
282 struct trace_event_fields *field;
283
284 field = find_event_field(fmt, call);
285 if (!field)
286 return false;
287
288 /* This is an array and is OK to dereference. */
289 return strchr(field->type, '[') != NULL;
290 }
291
292 /* Look for a string within an argument */
find_print_string(const char * arg,const char * str,const char * end)293 static bool find_print_string(const char *arg, const char *str, const char *end)
294 {
295 const char *r;
296
297 r = strstr(arg, str);
298 return r && r < end;
299 }
300
301 /* Return true if the argument pointer is safe */
process_pointer(const char * fmt,int len,struct trace_event_call * call)302 static bool process_pointer(const char *fmt, int len, struct trace_event_call *call)
303 {
304 const char *r, *e, *a;
305
306 e = fmt + len;
307
308 /* Find the REC-> in the argument */
309 r = strstr(fmt, "REC->");
310 if (r && r < e) {
311 /*
312 * Addresses of events on the buffer, or an array on the buffer is
313 * OK to dereference. There's ways to fool this, but
314 * this is to catch common mistakes, not malicious code.
315 */
316 a = strchr(fmt, '&');
317 if ((a && (a < r)) || test_field(r, call))
318 return true;
319 } else if (find_print_string(fmt, "__get_dynamic_array(", e)) {
320 return true;
321 } else if (find_print_string(fmt, "__get_rel_dynamic_array(", e)) {
322 return true;
323 } else if (find_print_string(fmt, "__get_dynamic_array_len(", e)) {
324 return true;
325 } else if (find_print_string(fmt, "__get_rel_dynamic_array_len(", e)) {
326 return true;
327 } else if (find_print_string(fmt, "__get_sockaddr(", e)) {
328 return true;
329 } else if (find_print_string(fmt, "__get_rel_sockaddr(", e)) {
330 return true;
331 }
332 return false;
333 }
334
335 /* Return true if the string is safe */
process_string(const char * fmt,int len,struct trace_event_call * call)336 static bool process_string(const char *fmt, int len, struct trace_event_call *call)
337 {
338 struct trace_event_fields *field;
339 const char *r, *e, *s;
340
341 e = fmt + len;
342
343 /*
344 * There are several helper functions that return strings.
345 * If the argument contains a function, then assume its field is valid.
346 * It is considered that the argument has a function if it has:
347 * alphanumeric or '_' before a parenthesis.
348 */
349 s = fmt;
350 do {
351 r = strstr(s, "(");
352 if (!r || r >= e)
353 break;
354 for (int i = 1; r - i >= s; i++) {
355 char ch = *(r - i);
356 if (isspace(ch))
357 continue;
358 if (isalnum(ch) || ch == '_')
359 return true;
360 /* Anything else, this isn't a function */
361 break;
362 }
363 /* A function could be wrapped in parethesis, try the next one */
364 s = r + 1;
365 } while (s < e);
366
367 /*
368 * Check for arrays. If the argument has: foo[REC->val]
369 * then it is very likely that foo is an array of strings
370 * that are safe to use.
371 */
372 r = strstr(s, "[");
373 if (r && r < e) {
374 r = strstr(r, "REC->");
375 if (r && r < e)
376 return true;
377 }
378
379 /*
380 * If there's any strings in the argument consider this arg OK as it
381 * could be: REC->field ? "foo" : "bar" and we don't want to get into
382 * verifying that logic here.
383 */
384 if (find_print_string(fmt, "\"", e))
385 return true;
386
387 /* Dereferenced strings are also valid like any other pointer */
388 if (process_pointer(fmt, len, call))
389 return true;
390
391 /* Make sure the field is found */
392 field = find_event_field(fmt, call);
393 if (!field)
394 return false;
395
396 /* Test this field's string before printing the event */
397 call->flags |= TRACE_EVENT_FL_TEST_STR;
398 field->needs_test = 1;
399
400 return true;
401 }
402
handle_dereference_arg(const char * arg_str,u64 string_flags,int len,u64 * dereference_flags,int arg,struct trace_event_call * call)403 static void handle_dereference_arg(const char *arg_str, u64 string_flags, int len,
404 u64 *dereference_flags, int arg,
405 struct trace_event_call *call)
406 {
407 if (string_flags & (1ULL << arg)) {
408 if (process_string(arg_str, len, call))
409 *dereference_flags &= ~(1ULL << arg);
410 } else if (process_pointer(arg_str, len, call))
411 *dereference_flags &= ~(1ULL << arg);
412 else
413 pr_warn("TRACE EVENT ERROR: Bad dereference argument: '%.*s'\n",
414 len, arg_str);
415 }
416
417 /*
418 * Examine the print fmt of the event looking for unsafe dereference
419 * pointers using %p* that could be recorded in the trace event and
420 * much later referenced after the pointer was freed. Dereferencing
421 * pointers are OK, if it is dereferenced into the event itself.
422 */
test_event_printk(struct trace_event_call * call)423 static void test_event_printk(struct trace_event_call *call)
424 {
425 u64 dereference_flags = 0;
426 u64 string_flags = 0;
427 bool first = true;
428 const char *fmt;
429 int parens = 0;
430 char in_quote = 0;
431 int start_arg = 0;
432 int arg = 0;
433 int i, e;
434
435 fmt = call->print_fmt;
436
437 if (!fmt)
438 return;
439
440 for (i = 0; fmt[i]; i++) {
441 switch (fmt[i]) {
442 case '\\':
443 i++;
444 if (!fmt[i])
445 return;
446 continue;
447 case '"':
448 case '\'':
449 /*
450 * The print fmt starts with a string that
451 * is processed first to find %p* usage,
452 * then after the first string, the print fmt
453 * contains arguments that are used to check
454 * if the dereferenced %p* usage is safe.
455 */
456 if (first) {
457 if (fmt[i] == '\'')
458 continue;
459 if (in_quote) {
460 arg = 0;
461 first = false;
462 /*
463 * If there was no %p* uses
464 * the fmt is OK.
465 */
466 if (!dereference_flags)
467 return;
468 }
469 }
470 if (in_quote) {
471 if (in_quote == fmt[i])
472 in_quote = 0;
473 } else {
474 in_quote = fmt[i];
475 }
476 continue;
477 case '%':
478 if (!first || !in_quote)
479 continue;
480 i++;
481 if (!fmt[i])
482 return;
483 switch (fmt[i]) {
484 case '%':
485 continue;
486 case 'p':
487 do_pointer:
488 /* Find dereferencing fields */
489 switch (fmt[i + 1]) {
490 case 'B': case 'R': case 'r':
491 case 'b': case 'M': case 'm':
492 case 'I': case 'i': case 'E':
493 case 'U': case 'V': case 'N':
494 case 'a': case 'd': case 'D':
495 case 'g': case 't': case 'C':
496 case 'O': case 'f':
497 if (WARN_ONCE(arg == 63,
498 "Too many args for event: %s",
499 trace_event_name(call)))
500 return;
501 dereference_flags |= 1ULL << arg;
502 }
503 break;
504 default:
505 {
506 bool star = false;
507 int j;
508
509 /* Increment arg if %*s exists. */
510 for (j = 0; fmt[i + j]; j++) {
511 if (isdigit(fmt[i + j]) ||
512 fmt[i + j] == '.')
513 continue;
514 if (fmt[i + j] == '*') {
515 star = true;
516 /* Handle %*pbl case */
517 if (!j && fmt[i + 1] == 'p') {
518 arg++;
519 i++;
520 goto do_pointer;
521 }
522 continue;
523 }
524 if ((fmt[i + j] == 's')) {
525 if (star)
526 arg++;
527 if (WARN_ONCE(arg == 63,
528 "Too many args for event: %s",
529 trace_event_name(call)))
530 return;
531 dereference_flags |= 1ULL << arg;
532 string_flags |= 1ULL << arg;
533 }
534 break;
535 }
536 break;
537 } /* default */
538
539 } /* switch */
540 arg++;
541 continue;
542 case '(':
543 if (in_quote)
544 continue;
545 parens++;
546 continue;
547 case ')':
548 if (in_quote)
549 continue;
550 parens--;
551 if (WARN_ONCE(parens < 0,
552 "Paren mismatch for event: %s\narg='%s'\n%*s",
553 trace_event_name(call),
554 fmt + start_arg,
555 (i - start_arg) + 5, "^"))
556 return;
557 continue;
558 case ',':
559 if (in_quote || parens)
560 continue;
561 e = i;
562 i++;
563 while (isspace(fmt[i]))
564 i++;
565
566 /*
567 * If start_arg is zero, then this is the start of the
568 * first argument. The processing of the argument happens
569 * when the end of the argument is found, as it needs to
570 * handle paranthesis and such.
571 */
572 if (!start_arg) {
573 start_arg = i;
574 /* Balance out the i++ in the for loop */
575 i--;
576 continue;
577 }
578
579 if (dereference_flags & (1ULL << arg)) {
580 handle_dereference_arg(fmt + start_arg, string_flags,
581 e - start_arg,
582 &dereference_flags, arg, call);
583 }
584
585 start_arg = i;
586 arg++;
587 /* Balance out the i++ in the for loop */
588 i--;
589 }
590 }
591
592 if (dereference_flags & (1ULL << arg)) {
593 handle_dereference_arg(fmt + start_arg, string_flags,
594 i - start_arg,
595 &dereference_flags, arg, call);
596 }
597
598 /*
599 * If you triggered the below warning, the trace event reported
600 * uses an unsafe dereference pointer %p*. As the data stored
601 * at the trace event time may no longer exist when the trace
602 * event is printed, dereferencing to the original source is
603 * unsafe. The source of the dereference must be copied into the
604 * event itself, and the dereference must access the copy instead.
605 */
606 if (WARN_ON_ONCE(dereference_flags)) {
607 arg = 1;
608 while (!(dereference_flags & 1)) {
609 dereference_flags >>= 1;
610 arg++;
611 }
612 pr_warn("event %s has unsafe dereference of argument %d\n",
613 trace_event_name(call), arg);
614 pr_warn("print_fmt: %s\n", fmt);
615 }
616 }
617
trace_event_raw_init(struct trace_event_call * call)618 int trace_event_raw_init(struct trace_event_call *call)
619 {
620 int id;
621
622 id = register_trace_event(&call->event);
623 if (!id)
624 return -ENODEV;
625
626 test_event_printk(call);
627
628 return 0;
629 }
630 EXPORT_SYMBOL_GPL(trace_event_raw_init);
631
trace_event_ignore_this_pid(struct trace_event_file * trace_file)632 bool trace_event_ignore_this_pid(struct trace_event_file *trace_file)
633 {
634 struct trace_array *tr = trace_file->tr;
635 struct trace_pid_list *no_pid_list;
636 struct trace_pid_list *pid_list;
637
638 pid_list = rcu_dereference_raw(tr->filtered_pids);
639 no_pid_list = rcu_dereference_raw(tr->filtered_no_pids);
640
641 if (!pid_list && !no_pid_list)
642 return false;
643
644 /*
645 * This is recorded at every sched_switch for this task.
646 * Thus, even if the task migrates the ignore value will be the same.
647 */
648 return this_cpu_read(tr->array_buffer.data->ignore_pid) != 0;
649 }
650 EXPORT_SYMBOL_GPL(trace_event_ignore_this_pid);
651
trace_event_buffer_reserve(struct trace_event_buffer * fbuffer,struct trace_event_file * trace_file,unsigned long len)652 void *trace_event_buffer_reserve(struct trace_event_buffer *fbuffer,
653 struct trace_event_file *trace_file,
654 unsigned long len)
655 {
656 struct trace_event_call *event_call = trace_file->event_call;
657
658 if ((trace_file->flags & EVENT_FILE_FL_PID_FILTER) &&
659 trace_event_ignore_this_pid(trace_file))
660 return NULL;
661
662 /*
663 * If CONFIG_PREEMPTION is enabled, then the tracepoint itself disables
664 * preemption (adding one to the preempt_count). Since we are
665 * interested in the preempt_count at the time the tracepoint was
666 * hit, we need to subtract one to offset the increment.
667 */
668 fbuffer->trace_ctx = tracing_gen_ctx_dec();
669 fbuffer->trace_file = trace_file;
670
671 fbuffer->event =
672 trace_event_buffer_lock_reserve(&fbuffer->buffer, trace_file,
673 event_call->event.type, len,
674 fbuffer->trace_ctx);
675 if (!fbuffer->event)
676 return NULL;
677
678 fbuffer->regs = NULL;
679 fbuffer->entry = ring_buffer_event_data(fbuffer->event);
680 return fbuffer->entry;
681 }
682 EXPORT_SYMBOL_GPL(trace_event_buffer_reserve);
683
trace_event_reg(struct trace_event_call * call,enum trace_reg type,void * data)684 int trace_event_reg(struct trace_event_call *call,
685 enum trace_reg type, void *data)
686 {
687 struct trace_event_file *file = data;
688
689 WARN_ON(!(call->flags & TRACE_EVENT_FL_TRACEPOINT));
690 switch (type) {
691 case TRACE_REG_REGISTER:
692 return tracepoint_probe_register(call->tp,
693 call->class->probe,
694 file);
695 case TRACE_REG_UNREGISTER:
696 tracepoint_probe_unregister(call->tp,
697 call->class->probe,
698 file);
699 return 0;
700
701 #ifdef CONFIG_PERF_EVENTS
702 case TRACE_REG_PERF_REGISTER:
703 return tracepoint_probe_register(call->tp,
704 call->class->perf_probe,
705 call);
706 case TRACE_REG_PERF_UNREGISTER:
707 tracepoint_probe_unregister(call->tp,
708 call->class->perf_probe,
709 call);
710 return 0;
711 case TRACE_REG_PERF_OPEN:
712 case TRACE_REG_PERF_CLOSE:
713 case TRACE_REG_PERF_ADD:
714 case TRACE_REG_PERF_DEL:
715 return 0;
716 #endif
717 }
718 return 0;
719 }
720 EXPORT_SYMBOL_GPL(trace_event_reg);
721
trace_event_enable_cmd_record(bool enable)722 void trace_event_enable_cmd_record(bool enable)
723 {
724 struct trace_event_file *file;
725 struct trace_array *tr;
726
727 lockdep_assert_held(&event_mutex);
728
729 do_for_each_event_file(tr, file) {
730
731 if (!(file->flags & EVENT_FILE_FL_ENABLED))
732 continue;
733
734 if (enable) {
735 tracing_start_cmdline_record();
736 set_bit(EVENT_FILE_FL_RECORDED_CMD_BIT, &file->flags);
737 } else {
738 tracing_stop_cmdline_record();
739 clear_bit(EVENT_FILE_FL_RECORDED_CMD_BIT, &file->flags);
740 }
741 } while_for_each_event_file();
742 }
743
trace_event_enable_tgid_record(bool enable)744 void trace_event_enable_tgid_record(bool enable)
745 {
746 struct trace_event_file *file;
747 struct trace_array *tr;
748
749 lockdep_assert_held(&event_mutex);
750
751 do_for_each_event_file(tr, file) {
752 if (!(file->flags & EVENT_FILE_FL_ENABLED))
753 continue;
754
755 if (enable) {
756 tracing_start_tgid_record();
757 set_bit(EVENT_FILE_FL_RECORDED_TGID_BIT, &file->flags);
758 } else {
759 tracing_stop_tgid_record();
760 clear_bit(EVENT_FILE_FL_RECORDED_TGID_BIT,
761 &file->flags);
762 }
763 } while_for_each_event_file();
764 }
765
__ftrace_event_enable_disable(struct trace_event_file * file,int enable,int soft_disable)766 static int __ftrace_event_enable_disable(struct trace_event_file *file,
767 int enable, int soft_disable)
768 {
769 struct trace_event_call *call = file->event_call;
770 struct trace_array *tr = file->tr;
771 int ret = 0;
772 int disable;
773
774 switch (enable) {
775 case 0:
776 /*
777 * When soft_disable is set and enable is cleared, the sm_ref
778 * reference counter is decremented. If it reaches 0, we want
779 * to clear the SOFT_DISABLED flag but leave the event in the
780 * state that it was. That is, if the event was enabled and
781 * SOFT_DISABLED isn't set, then do nothing. But if SOFT_DISABLED
782 * is set we do not want the event to be enabled before we
783 * clear the bit.
784 *
785 * When soft_disable is not set but the SOFT_MODE flag is,
786 * we do nothing. Do not disable the tracepoint, otherwise
787 * "soft enable"s (clearing the SOFT_DISABLED bit) wont work.
788 */
789 if (soft_disable) {
790 if (atomic_dec_return(&file->sm_ref) > 0)
791 break;
792 disable = file->flags & EVENT_FILE_FL_SOFT_DISABLED;
793 clear_bit(EVENT_FILE_FL_SOFT_MODE_BIT, &file->flags);
794 /* Disable use of trace_buffered_event */
795 trace_buffered_event_disable();
796 } else
797 disable = !(file->flags & EVENT_FILE_FL_SOFT_MODE);
798
799 if (disable && (file->flags & EVENT_FILE_FL_ENABLED)) {
800 clear_bit(EVENT_FILE_FL_ENABLED_BIT, &file->flags);
801 if (file->flags & EVENT_FILE_FL_RECORDED_CMD) {
802 tracing_stop_cmdline_record();
803 clear_bit(EVENT_FILE_FL_RECORDED_CMD_BIT, &file->flags);
804 }
805
806 if (file->flags & EVENT_FILE_FL_RECORDED_TGID) {
807 tracing_stop_tgid_record();
808 clear_bit(EVENT_FILE_FL_RECORDED_TGID_BIT, &file->flags);
809 }
810
811 ret = call->class->reg(call, TRACE_REG_UNREGISTER, file);
812
813 WARN_ON_ONCE(ret);
814 }
815 /* If in SOFT_MODE, just set the SOFT_DISABLE_BIT, else clear it */
816 if (file->flags & EVENT_FILE_FL_SOFT_MODE)
817 set_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &file->flags);
818 else
819 clear_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &file->flags);
820 break;
821 case 1:
822 /*
823 * When soft_disable is set and enable is set, we want to
824 * register the tracepoint for the event, but leave the event
825 * as is. That means, if the event was already enabled, we do
826 * nothing (but set SOFT_MODE). If the event is disabled, we
827 * set SOFT_DISABLED before enabling the event tracepoint, so
828 * it still seems to be disabled.
829 */
830 if (!soft_disable)
831 clear_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &file->flags);
832 else {
833 if (atomic_inc_return(&file->sm_ref) > 1)
834 break;
835 set_bit(EVENT_FILE_FL_SOFT_MODE_BIT, &file->flags);
836 /* Enable use of trace_buffered_event */
837 trace_buffered_event_enable();
838 }
839
840 if (!(file->flags & EVENT_FILE_FL_ENABLED)) {
841 bool cmd = false, tgid = false;
842
843 /* Keep the event disabled, when going to SOFT_MODE. */
844 if (soft_disable)
845 set_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &file->flags);
846
847 if (tr->trace_flags & TRACE_ITER_RECORD_CMD) {
848 cmd = true;
849 tracing_start_cmdline_record();
850 set_bit(EVENT_FILE_FL_RECORDED_CMD_BIT, &file->flags);
851 }
852
853 if (tr->trace_flags & TRACE_ITER_RECORD_TGID) {
854 tgid = true;
855 tracing_start_tgid_record();
856 set_bit(EVENT_FILE_FL_RECORDED_TGID_BIT, &file->flags);
857 }
858
859 ret = call->class->reg(call, TRACE_REG_REGISTER, file);
860 if (ret) {
861 if (cmd)
862 tracing_stop_cmdline_record();
863 if (tgid)
864 tracing_stop_tgid_record();
865 pr_info("event trace: Could not enable event "
866 "%s\n", trace_event_name(call));
867 break;
868 }
869 set_bit(EVENT_FILE_FL_ENABLED_BIT, &file->flags);
870
871 /* WAS_ENABLED gets set but never cleared. */
872 set_bit(EVENT_FILE_FL_WAS_ENABLED_BIT, &file->flags);
873 }
874 break;
875 }
876
877 return ret;
878 }
879
trace_event_enable_disable(struct trace_event_file * file,int enable,int soft_disable)880 int trace_event_enable_disable(struct trace_event_file *file,
881 int enable, int soft_disable)
882 {
883 return __ftrace_event_enable_disable(file, enable, soft_disable);
884 }
885
ftrace_event_enable_disable(struct trace_event_file * file,int enable)886 static int ftrace_event_enable_disable(struct trace_event_file *file,
887 int enable)
888 {
889 return __ftrace_event_enable_disable(file, enable, 0);
890 }
891
892 #ifdef CONFIG_MODULES
893 struct event_mod_load {
894 struct list_head list;
895 char *module;
896 char *match;
897 char *system;
898 char *event;
899 };
900
free_event_mod(struct event_mod_load * event_mod)901 static void free_event_mod(struct event_mod_load *event_mod)
902 {
903 list_del(&event_mod->list);
904 kfree(event_mod->module);
905 kfree(event_mod->match);
906 kfree(event_mod->system);
907 kfree(event_mod->event);
908 kfree(event_mod);
909 }
910
clear_mod_events(struct trace_array * tr)911 static void clear_mod_events(struct trace_array *tr)
912 {
913 struct event_mod_load *event_mod, *n;
914
915 list_for_each_entry_safe(event_mod, n, &tr->mod_events, list) {
916 free_event_mod(event_mod);
917 }
918 }
919
remove_cache_mod(struct trace_array * tr,const char * mod,const char * match,const char * system,const char * event)920 static int remove_cache_mod(struct trace_array *tr, const char *mod,
921 const char *match, const char *system, const char *event)
922 {
923 struct event_mod_load *event_mod, *n;
924 int ret = -EINVAL;
925
926 list_for_each_entry_safe(event_mod, n, &tr->mod_events, list) {
927 if (strcmp(event_mod->module, mod) != 0)
928 continue;
929
930 if (match && strcmp(event_mod->match, match) != 0)
931 continue;
932
933 if (system &&
934 (!event_mod->system || strcmp(event_mod->system, system) != 0))
935 continue;
936
937 if (event &&
938 (!event_mod->event || strcmp(event_mod->event, event) != 0))
939 continue;
940
941 free_event_mod(event_mod);
942 ret = 0;
943 }
944
945 return ret;
946 }
947
cache_mod(struct trace_array * tr,const char * mod,int set,const char * match,const char * system,const char * event)948 static int cache_mod(struct trace_array *tr, const char *mod, int set,
949 const char *match, const char *system, const char *event)
950 {
951 struct event_mod_load *event_mod;
952
953 /* If the module exists, then this just failed to find an event */
954 if (module_exists(mod))
955 return -EINVAL;
956
957 /* See if this is to remove a cached filter */
958 if (!set)
959 return remove_cache_mod(tr, mod, match, system, event);
960
961 event_mod = kzalloc(sizeof(*event_mod), GFP_KERNEL);
962 if (!event_mod)
963 return -ENOMEM;
964
965 INIT_LIST_HEAD(&event_mod->list);
966 event_mod->module = kstrdup(mod, GFP_KERNEL);
967 if (!event_mod->module)
968 goto out_free;
969
970 if (match) {
971 event_mod->match = kstrdup(match, GFP_KERNEL);
972 if (!event_mod->match)
973 goto out_free;
974 }
975
976 if (system) {
977 event_mod->system = kstrdup(system, GFP_KERNEL);
978 if (!event_mod->system)
979 goto out_free;
980 }
981
982 if (event) {
983 event_mod->event = kstrdup(event, GFP_KERNEL);
984 if (!event_mod->event)
985 goto out_free;
986 }
987
988 list_add(&event_mod->list, &tr->mod_events);
989
990 return 0;
991
992 out_free:
993 free_event_mod(event_mod);
994
995 return -ENOMEM;
996 }
997 #else /* CONFIG_MODULES */
clear_mod_events(struct trace_array * tr)998 static inline void clear_mod_events(struct trace_array *tr) { }
cache_mod(struct trace_array * tr,const char * mod,int set,const char * match,const char * system,const char * event)999 static int cache_mod(struct trace_array *tr, const char *mod, int set,
1000 const char *match, const char *system, const char *event)
1001 {
1002 return -EINVAL;
1003 }
1004 #endif
1005
ftrace_clear_events(struct trace_array * tr)1006 static void ftrace_clear_events(struct trace_array *tr)
1007 {
1008 struct trace_event_file *file;
1009
1010 mutex_lock(&event_mutex);
1011 list_for_each_entry(file, &tr->events, list) {
1012 ftrace_event_enable_disable(file, 0);
1013 }
1014 clear_mod_events(tr);
1015 mutex_unlock(&event_mutex);
1016 }
1017
1018 static void
event_filter_pid_sched_process_exit(void * data,struct task_struct * task)1019 event_filter_pid_sched_process_exit(void *data, struct task_struct *task)
1020 {
1021 struct trace_pid_list *pid_list;
1022 struct trace_array *tr = data;
1023
1024 pid_list = rcu_dereference_raw(tr->filtered_pids);
1025 trace_filter_add_remove_task(pid_list, NULL, task);
1026
1027 pid_list = rcu_dereference_raw(tr->filtered_no_pids);
1028 trace_filter_add_remove_task(pid_list, NULL, task);
1029 }
1030
1031 static void
event_filter_pid_sched_process_fork(void * data,struct task_struct * self,struct task_struct * task)1032 event_filter_pid_sched_process_fork(void *data,
1033 struct task_struct *self,
1034 struct task_struct *task)
1035 {
1036 struct trace_pid_list *pid_list;
1037 struct trace_array *tr = data;
1038
1039 pid_list = rcu_dereference_sched(tr->filtered_pids);
1040 trace_filter_add_remove_task(pid_list, self, task);
1041
1042 pid_list = rcu_dereference_sched(tr->filtered_no_pids);
1043 trace_filter_add_remove_task(pid_list, self, task);
1044 }
1045
trace_event_follow_fork(struct trace_array * tr,bool enable)1046 void trace_event_follow_fork(struct trace_array *tr, bool enable)
1047 {
1048 if (enable) {
1049 register_trace_prio_sched_process_fork(event_filter_pid_sched_process_fork,
1050 tr, INT_MIN);
1051 register_trace_prio_sched_process_free(event_filter_pid_sched_process_exit,
1052 tr, INT_MAX);
1053 } else {
1054 unregister_trace_sched_process_fork(event_filter_pid_sched_process_fork,
1055 tr);
1056 unregister_trace_sched_process_free(event_filter_pid_sched_process_exit,
1057 tr);
1058 }
1059 }
1060
1061 static void
event_filter_pid_sched_switch_probe_pre(void * data,bool preempt,struct task_struct * prev,struct task_struct * next,unsigned int prev_state)1062 event_filter_pid_sched_switch_probe_pre(void *data, bool preempt,
1063 struct task_struct *prev,
1064 struct task_struct *next,
1065 unsigned int prev_state)
1066 {
1067 struct trace_array *tr = data;
1068 struct trace_pid_list *no_pid_list;
1069 struct trace_pid_list *pid_list;
1070 bool ret;
1071
1072 pid_list = rcu_dereference_sched(tr->filtered_pids);
1073 no_pid_list = rcu_dereference_sched(tr->filtered_no_pids);
1074
1075 /*
1076 * Sched switch is funny, as we only want to ignore it
1077 * in the notrace case if both prev and next should be ignored.
1078 */
1079 ret = trace_ignore_this_task(NULL, no_pid_list, prev) &&
1080 trace_ignore_this_task(NULL, no_pid_list, next);
1081
1082 this_cpu_write(tr->array_buffer.data->ignore_pid, ret ||
1083 (trace_ignore_this_task(pid_list, NULL, prev) &&
1084 trace_ignore_this_task(pid_list, NULL, next)));
1085 }
1086
1087 static void
event_filter_pid_sched_switch_probe_post(void * data,bool preempt,struct task_struct * prev,struct task_struct * next,unsigned int prev_state)1088 event_filter_pid_sched_switch_probe_post(void *data, bool preempt,
1089 struct task_struct *prev,
1090 struct task_struct *next,
1091 unsigned int prev_state)
1092 {
1093 struct trace_array *tr = data;
1094 struct trace_pid_list *no_pid_list;
1095 struct trace_pid_list *pid_list;
1096
1097 pid_list = rcu_dereference_sched(tr->filtered_pids);
1098 no_pid_list = rcu_dereference_sched(tr->filtered_no_pids);
1099
1100 this_cpu_write(tr->array_buffer.data->ignore_pid,
1101 trace_ignore_this_task(pid_list, no_pid_list, next));
1102 }
1103
1104 static void
event_filter_pid_sched_wakeup_probe_pre(void * data,struct task_struct * task)1105 event_filter_pid_sched_wakeup_probe_pre(void *data, struct task_struct *task)
1106 {
1107 struct trace_array *tr = data;
1108 struct trace_pid_list *no_pid_list;
1109 struct trace_pid_list *pid_list;
1110
1111 /* Nothing to do if we are already tracing */
1112 if (!this_cpu_read(tr->array_buffer.data->ignore_pid))
1113 return;
1114
1115 pid_list = rcu_dereference_sched(tr->filtered_pids);
1116 no_pid_list = rcu_dereference_sched(tr->filtered_no_pids);
1117
1118 this_cpu_write(tr->array_buffer.data->ignore_pid,
1119 trace_ignore_this_task(pid_list, no_pid_list, task));
1120 }
1121
1122 static void
event_filter_pid_sched_wakeup_probe_post(void * data,struct task_struct * task)1123 event_filter_pid_sched_wakeup_probe_post(void *data, struct task_struct *task)
1124 {
1125 struct trace_array *tr = data;
1126 struct trace_pid_list *no_pid_list;
1127 struct trace_pid_list *pid_list;
1128
1129 /* Nothing to do if we are not tracing */
1130 if (this_cpu_read(tr->array_buffer.data->ignore_pid))
1131 return;
1132
1133 pid_list = rcu_dereference_sched(tr->filtered_pids);
1134 no_pid_list = rcu_dereference_sched(tr->filtered_no_pids);
1135
1136 /* Set tracing if current is enabled */
1137 this_cpu_write(tr->array_buffer.data->ignore_pid,
1138 trace_ignore_this_task(pid_list, no_pid_list, current));
1139 }
1140
unregister_pid_events(struct trace_array * tr)1141 static void unregister_pid_events(struct trace_array *tr)
1142 {
1143 unregister_trace_sched_switch(event_filter_pid_sched_switch_probe_pre, tr);
1144 unregister_trace_sched_switch(event_filter_pid_sched_switch_probe_post, tr);
1145
1146 unregister_trace_sched_wakeup(event_filter_pid_sched_wakeup_probe_pre, tr);
1147 unregister_trace_sched_wakeup(event_filter_pid_sched_wakeup_probe_post, tr);
1148
1149 unregister_trace_sched_wakeup_new(event_filter_pid_sched_wakeup_probe_pre, tr);
1150 unregister_trace_sched_wakeup_new(event_filter_pid_sched_wakeup_probe_post, tr);
1151
1152 unregister_trace_sched_waking(event_filter_pid_sched_wakeup_probe_pre, tr);
1153 unregister_trace_sched_waking(event_filter_pid_sched_wakeup_probe_post, tr);
1154 }
1155
__ftrace_clear_event_pids(struct trace_array * tr,int type)1156 static void __ftrace_clear_event_pids(struct trace_array *tr, int type)
1157 {
1158 struct trace_pid_list *pid_list;
1159 struct trace_pid_list *no_pid_list;
1160 struct trace_event_file *file;
1161 int cpu;
1162
1163 pid_list = rcu_dereference_protected(tr->filtered_pids,
1164 lockdep_is_held(&event_mutex));
1165 no_pid_list = rcu_dereference_protected(tr->filtered_no_pids,
1166 lockdep_is_held(&event_mutex));
1167
1168 /* Make sure there's something to do */
1169 if (!pid_type_enabled(type, pid_list, no_pid_list))
1170 return;
1171
1172 if (!still_need_pid_events(type, pid_list, no_pid_list)) {
1173 unregister_pid_events(tr);
1174
1175 list_for_each_entry(file, &tr->events, list) {
1176 clear_bit(EVENT_FILE_FL_PID_FILTER_BIT, &file->flags);
1177 }
1178
1179 for_each_possible_cpu(cpu)
1180 per_cpu_ptr(tr->array_buffer.data, cpu)->ignore_pid = false;
1181 }
1182
1183 if (type & TRACE_PIDS)
1184 rcu_assign_pointer(tr->filtered_pids, NULL);
1185
1186 if (type & TRACE_NO_PIDS)
1187 rcu_assign_pointer(tr->filtered_no_pids, NULL);
1188
1189 /* Wait till all users are no longer using pid filtering */
1190 tracepoint_synchronize_unregister();
1191
1192 if ((type & TRACE_PIDS) && pid_list)
1193 trace_pid_list_free(pid_list);
1194
1195 if ((type & TRACE_NO_PIDS) && no_pid_list)
1196 trace_pid_list_free(no_pid_list);
1197 }
1198
ftrace_clear_event_pids(struct trace_array * tr,int type)1199 static void ftrace_clear_event_pids(struct trace_array *tr, int type)
1200 {
1201 mutex_lock(&event_mutex);
1202 __ftrace_clear_event_pids(tr, type);
1203 mutex_unlock(&event_mutex);
1204 }
1205
__put_system(struct event_subsystem * system)1206 static void __put_system(struct event_subsystem *system)
1207 {
1208 struct event_filter *filter = system->filter;
1209
1210 WARN_ON_ONCE(system_refcount(system) == 0);
1211 if (system_refcount_dec(system))
1212 return;
1213
1214 list_del(&system->list);
1215
1216 if (filter) {
1217 kfree(filter->filter_string);
1218 kfree(filter);
1219 }
1220 kfree_const(system->name);
1221 kfree(system);
1222 }
1223
__get_system(struct event_subsystem * system)1224 static void __get_system(struct event_subsystem *system)
1225 {
1226 WARN_ON_ONCE(system_refcount(system) == 0);
1227 system_refcount_inc(system);
1228 }
1229
__get_system_dir(struct trace_subsystem_dir * dir)1230 static void __get_system_dir(struct trace_subsystem_dir *dir)
1231 {
1232 WARN_ON_ONCE(dir->ref_count == 0);
1233 dir->ref_count++;
1234 __get_system(dir->subsystem);
1235 }
1236
__put_system_dir(struct trace_subsystem_dir * dir)1237 static void __put_system_dir(struct trace_subsystem_dir *dir)
1238 {
1239 WARN_ON_ONCE(dir->ref_count == 0);
1240 /* If the subsystem is about to be freed, the dir must be too */
1241 WARN_ON_ONCE(system_refcount(dir->subsystem) == 1 && dir->ref_count != 1);
1242
1243 __put_system(dir->subsystem);
1244 if (!--dir->ref_count)
1245 kfree(dir);
1246 }
1247
put_system(struct trace_subsystem_dir * dir)1248 static void put_system(struct trace_subsystem_dir *dir)
1249 {
1250 mutex_lock(&event_mutex);
1251 __put_system_dir(dir);
1252 mutex_unlock(&event_mutex);
1253 }
1254
remove_subsystem(struct trace_subsystem_dir * dir)1255 static void remove_subsystem(struct trace_subsystem_dir *dir)
1256 {
1257 if (!dir)
1258 return;
1259
1260 if (!--dir->nr_events) {
1261 eventfs_remove_dir(dir->ei);
1262 list_del(&dir->list);
1263 __put_system_dir(dir);
1264 }
1265 }
1266
event_file_get(struct trace_event_file * file)1267 void event_file_get(struct trace_event_file *file)
1268 {
1269 refcount_inc(&file->ref);
1270 }
1271
event_file_put(struct trace_event_file * file)1272 void event_file_put(struct trace_event_file *file)
1273 {
1274 if (WARN_ON_ONCE(!refcount_read(&file->ref))) {
1275 if (file->flags & EVENT_FILE_FL_FREED)
1276 kmem_cache_free(file_cachep, file);
1277 return;
1278 }
1279
1280 if (refcount_dec_and_test(&file->ref)) {
1281 /* Count should only go to zero when it is freed */
1282 if (WARN_ON_ONCE(!(file->flags & EVENT_FILE_FL_FREED)))
1283 return;
1284 kmem_cache_free(file_cachep, file);
1285 }
1286 }
1287
remove_event_file_dir(struct trace_event_file * file)1288 static void remove_event_file_dir(struct trace_event_file *file)
1289 {
1290 eventfs_remove_dir(file->ei);
1291 list_del(&file->list);
1292 remove_subsystem(file->system);
1293 free_event_filter(file->filter);
1294 file->flags |= EVENT_FILE_FL_FREED;
1295 event_file_put(file);
1296 }
1297
1298 /*
1299 * __ftrace_set_clr_event(NULL, NULL, NULL, set) will set/unset all events.
1300 */
1301 static int
__ftrace_set_clr_event_nolock(struct trace_array * tr,const char * match,const char * sub,const char * event,int set,const char * mod)1302 __ftrace_set_clr_event_nolock(struct trace_array *tr, const char *match,
1303 const char *sub, const char *event, int set,
1304 const char *mod)
1305 {
1306 struct trace_event_file *file;
1307 struct trace_event_call *call;
1308 char *module __free(kfree) = NULL;
1309 const char *name;
1310 int ret = -EINVAL;
1311 int eret = 0;
1312
1313 if (mod) {
1314 char *p;
1315
1316 module = kstrdup(mod, GFP_KERNEL);
1317 if (!module)
1318 return -ENOMEM;
1319
1320 /* Replace all '-' with '_' as that's what modules do */
1321 for (p = strchr(module, '-'); p; p = strchr(p + 1, '-'))
1322 *p = '_';
1323 }
1324
1325 list_for_each_entry(file, &tr->events, list) {
1326
1327 call = file->event_call;
1328
1329 /* If a module is specified, skip events that are not that module */
1330 if (module && (!call->module || strcmp(module_name(call->module), module)))
1331 continue;
1332
1333 name = trace_event_name(call);
1334
1335 if (!name || !call->class || !call->class->reg)
1336 continue;
1337
1338 if (call->flags & TRACE_EVENT_FL_IGNORE_ENABLE)
1339 continue;
1340
1341 if (match &&
1342 strcmp(match, name) != 0 &&
1343 strcmp(match, call->class->system) != 0)
1344 continue;
1345
1346 if (sub && strcmp(sub, call->class->system) != 0)
1347 continue;
1348
1349 if (event && strcmp(event, name) != 0)
1350 continue;
1351
1352 ret = ftrace_event_enable_disable(file, set);
1353
1354 /*
1355 * Save the first error and return that. Some events
1356 * may still have been enabled, but let the user
1357 * know that something went wrong.
1358 */
1359 if (ret && !eret)
1360 eret = ret;
1361
1362 ret = eret;
1363 }
1364
1365 /*
1366 * If this is a module setting and nothing was found,
1367 * check if the module was loaded. If it wasn't cache it.
1368 */
1369 if (module && ret == -EINVAL && !eret)
1370 ret = cache_mod(tr, module, set, match, sub, event);
1371
1372 return ret;
1373 }
1374
__ftrace_set_clr_event(struct trace_array * tr,const char * match,const char * sub,const char * event,int set,const char * mod)1375 static int __ftrace_set_clr_event(struct trace_array *tr, const char *match,
1376 const char *sub, const char *event, int set,
1377 const char *mod)
1378 {
1379 int ret;
1380
1381 mutex_lock(&event_mutex);
1382 ret = __ftrace_set_clr_event_nolock(tr, match, sub, event, set, mod);
1383 mutex_unlock(&event_mutex);
1384
1385 return ret;
1386 }
1387
ftrace_set_clr_event(struct trace_array * tr,char * buf,int set)1388 int ftrace_set_clr_event(struct trace_array *tr, char *buf, int set)
1389 {
1390 char *event = NULL, *sub = NULL, *match, *mod;
1391 int ret;
1392
1393 if (!tr)
1394 return -ENOENT;
1395
1396 /* Modules events can be appened with :mod:<module> */
1397 mod = strstr(buf, ":mod:");
1398 if (mod) {
1399 *mod = '\0';
1400 /* move to the module name */
1401 mod += 5;
1402 }
1403
1404 /*
1405 * The buf format can be <subsystem>:<event-name>
1406 * *:<event-name> means any event by that name.
1407 * :<event-name> is the same.
1408 *
1409 * <subsystem>:* means all events in that subsystem
1410 * <subsystem>: means the same.
1411 *
1412 * <name> (no ':') means all events in a subsystem with
1413 * the name <name> or any event that matches <name>
1414 */
1415
1416 match = strsep(&buf, ":");
1417 if (buf) {
1418 sub = match;
1419 event = buf;
1420 match = NULL;
1421
1422 if (!strlen(sub) || strcmp(sub, "*") == 0)
1423 sub = NULL;
1424 if (!strlen(event) || strcmp(event, "*") == 0)
1425 event = NULL;
1426 } else if (mod) {
1427 /* Allow wildcard for no length or star */
1428 if (!strlen(match) || strcmp(match, "*") == 0)
1429 match = NULL;
1430 }
1431
1432 ret = __ftrace_set_clr_event(tr, match, sub, event, set, mod);
1433
1434 /* Put back the colon to allow this to be called again */
1435 if (buf)
1436 *(buf - 1) = ':';
1437
1438 return ret;
1439 }
1440
1441 /**
1442 * trace_set_clr_event - enable or disable an event
1443 * @system: system name to match (NULL for any system)
1444 * @event: event name to match (NULL for all events, within system)
1445 * @set: 1 to enable, 0 to disable
1446 *
1447 * This is a way for other parts of the kernel to enable or disable
1448 * event recording.
1449 *
1450 * Returns 0 on success, -EINVAL if the parameters do not match any
1451 * registered events.
1452 */
trace_set_clr_event(const char * system,const char * event,int set)1453 int trace_set_clr_event(const char *system, const char *event, int set)
1454 {
1455 struct trace_array *tr = top_trace_array();
1456
1457 if (!tr)
1458 return -ENODEV;
1459
1460 return __ftrace_set_clr_event(tr, NULL, system, event, set, NULL);
1461 }
1462 EXPORT_SYMBOL_GPL(trace_set_clr_event);
1463
1464 /**
1465 * trace_array_set_clr_event - enable or disable an event for a trace array.
1466 * @tr: concerned trace array.
1467 * @system: system name to match (NULL for any system)
1468 * @event: event name to match (NULL for all events, within system)
1469 * @enable: true to enable, false to disable
1470 *
1471 * This is a way for other parts of the kernel to enable or disable
1472 * event recording.
1473 *
1474 * Returns 0 on success, -EINVAL if the parameters do not match any
1475 * registered events.
1476 */
trace_array_set_clr_event(struct trace_array * tr,const char * system,const char * event,bool enable)1477 int trace_array_set_clr_event(struct trace_array *tr, const char *system,
1478 const char *event, bool enable)
1479 {
1480 int set;
1481
1482 if (!tr)
1483 return -ENOENT;
1484
1485 set = (enable == true) ? 1 : 0;
1486 return __ftrace_set_clr_event(tr, NULL, system, event, set, NULL);
1487 }
1488 EXPORT_SYMBOL_GPL(trace_array_set_clr_event);
1489
1490 /* 128 should be much more than enough */
1491 #define EVENT_BUF_SIZE 127
1492
1493 static ssize_t
ftrace_event_write(struct file * file,const char __user * ubuf,size_t cnt,loff_t * ppos)1494 ftrace_event_write(struct file *file, const char __user *ubuf,
1495 size_t cnt, loff_t *ppos)
1496 {
1497 struct trace_parser parser;
1498 struct seq_file *m = file->private_data;
1499 struct trace_array *tr = m->private;
1500 ssize_t read, ret;
1501
1502 if (!cnt)
1503 return 0;
1504
1505 ret = tracing_update_buffers(tr);
1506 if (ret < 0)
1507 return ret;
1508
1509 if (trace_parser_get_init(&parser, EVENT_BUF_SIZE + 1))
1510 return -ENOMEM;
1511
1512 read = trace_get_user(&parser, ubuf, cnt, ppos);
1513
1514 if (read >= 0 && trace_parser_loaded((&parser))) {
1515 int set = 1;
1516
1517 if (*parser.buffer == '!')
1518 set = 0;
1519
1520 ret = ftrace_set_clr_event(tr, parser.buffer + !set, set);
1521 if (ret)
1522 goto out_put;
1523 }
1524
1525 ret = read;
1526
1527 out_put:
1528 trace_parser_put(&parser);
1529
1530 return ret;
1531 }
1532
1533 static void *
t_next(struct seq_file * m,void * v,loff_t * pos)1534 t_next(struct seq_file *m, void *v, loff_t *pos)
1535 {
1536 struct trace_event_file *file = v;
1537 struct trace_event_call *call;
1538 struct trace_array *tr = m->private;
1539
1540 (*pos)++;
1541
1542 list_for_each_entry_continue(file, &tr->events, list) {
1543 call = file->event_call;
1544 /*
1545 * The ftrace subsystem is for showing formats only.
1546 * They can not be enabled or disabled via the event files.
1547 */
1548 if (call->class && call->class->reg &&
1549 !(call->flags & TRACE_EVENT_FL_IGNORE_ENABLE))
1550 return file;
1551 }
1552
1553 return NULL;
1554 }
1555
t_start(struct seq_file * m,loff_t * pos)1556 static void *t_start(struct seq_file *m, loff_t *pos)
1557 {
1558 struct trace_event_file *file;
1559 struct trace_array *tr = m->private;
1560 loff_t l;
1561
1562 mutex_lock(&event_mutex);
1563
1564 file = list_entry(&tr->events, struct trace_event_file, list);
1565 for (l = 0; l <= *pos; ) {
1566 file = t_next(m, file, &l);
1567 if (!file)
1568 break;
1569 }
1570 return file;
1571 }
1572
1573 enum set_event_iter_type {
1574 SET_EVENT_FILE,
1575 SET_EVENT_MOD,
1576 };
1577
1578 struct set_event_iter {
1579 enum set_event_iter_type type;
1580 union {
1581 struct trace_event_file *file;
1582 struct event_mod_load *event_mod;
1583 };
1584 };
1585
1586 static void *
s_next(struct seq_file * m,void * v,loff_t * pos)1587 s_next(struct seq_file *m, void *v, loff_t *pos)
1588 {
1589 struct set_event_iter *iter = v;
1590 struct trace_event_file *file;
1591 struct trace_array *tr = m->private;
1592
1593 (*pos)++;
1594
1595 if (iter->type == SET_EVENT_FILE) {
1596 file = iter->file;
1597 list_for_each_entry_continue(file, &tr->events, list) {
1598 if (file->flags & EVENT_FILE_FL_ENABLED) {
1599 iter->file = file;
1600 return iter;
1601 }
1602 }
1603 #ifdef CONFIG_MODULES
1604 iter->type = SET_EVENT_MOD;
1605 iter->event_mod = list_entry(&tr->mod_events, struct event_mod_load, list);
1606 #endif
1607 }
1608
1609 #ifdef CONFIG_MODULES
1610 list_for_each_entry_continue(iter->event_mod, &tr->mod_events, list)
1611 return iter;
1612 #endif
1613
1614 /*
1615 * The iter is allocated in s_start() and passed via the 'v'
1616 * parameter. To stop the iterator, NULL must be returned. But
1617 * the return value is what the 'v' parameter in s_stop() receives
1618 * and frees. Free iter here as it will no longer be used.
1619 */
1620 kfree(iter);
1621 return NULL;
1622 }
1623
s_start(struct seq_file * m,loff_t * pos)1624 static void *s_start(struct seq_file *m, loff_t *pos)
1625 {
1626 struct trace_array *tr = m->private;
1627 struct set_event_iter *iter;
1628 loff_t l;
1629
1630 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
1631 if (!iter)
1632 return NULL;
1633
1634 mutex_lock(&event_mutex);
1635
1636 iter->type = SET_EVENT_FILE;
1637 iter->file = list_entry(&tr->events, struct trace_event_file, list);
1638
1639 for (l = 0; l <= *pos; ) {
1640 iter = s_next(m, iter, &l);
1641 if (!iter)
1642 break;
1643 }
1644 return iter;
1645 }
1646
t_show(struct seq_file * m,void * v)1647 static int t_show(struct seq_file *m, void *v)
1648 {
1649 struct trace_event_file *file = v;
1650 struct trace_event_call *call = file->event_call;
1651
1652 if (strcmp(call->class->system, TRACE_SYSTEM) != 0)
1653 seq_printf(m, "%s:", call->class->system);
1654 seq_printf(m, "%s\n", trace_event_name(call));
1655
1656 return 0;
1657 }
1658
t_stop(struct seq_file * m,void * p)1659 static void t_stop(struct seq_file *m, void *p)
1660 {
1661 mutex_unlock(&event_mutex);
1662 }
1663
1664 #ifdef CONFIG_MODULES
s_show(struct seq_file * m,void * v)1665 static int s_show(struct seq_file *m, void *v)
1666 {
1667 struct set_event_iter *iter = v;
1668 const char *system;
1669 const char *event;
1670
1671 if (iter->type == SET_EVENT_FILE)
1672 return t_show(m, iter->file);
1673
1674 /* When match is set, system and event are not */
1675 if (iter->event_mod->match) {
1676 seq_printf(m, "%s:mod:%s\n", iter->event_mod->match,
1677 iter->event_mod->module);
1678 return 0;
1679 }
1680
1681 system = iter->event_mod->system ? : "*";
1682 event = iter->event_mod->event ? : "*";
1683
1684 seq_printf(m, "%s:%s:mod:%s\n", system, event, iter->event_mod->module);
1685
1686 return 0;
1687 }
1688 #else /* CONFIG_MODULES */
s_show(struct seq_file * m,void * v)1689 static int s_show(struct seq_file *m, void *v)
1690 {
1691 struct set_event_iter *iter = v;
1692
1693 return t_show(m, iter->file);
1694 }
1695 #endif
1696
s_stop(struct seq_file * m,void * v)1697 static void s_stop(struct seq_file *m, void *v)
1698 {
1699 kfree(v);
1700 t_stop(m, NULL);
1701 }
1702
1703 static void *
__next(struct seq_file * m,void * v,loff_t * pos,int type)1704 __next(struct seq_file *m, void *v, loff_t *pos, int type)
1705 {
1706 struct trace_array *tr = m->private;
1707 struct trace_pid_list *pid_list;
1708
1709 if (type == TRACE_PIDS)
1710 pid_list = rcu_dereference_sched(tr->filtered_pids);
1711 else
1712 pid_list = rcu_dereference_sched(tr->filtered_no_pids);
1713
1714 return trace_pid_next(pid_list, v, pos);
1715 }
1716
1717 static void *
p_next(struct seq_file * m,void * v,loff_t * pos)1718 p_next(struct seq_file *m, void *v, loff_t *pos)
1719 {
1720 return __next(m, v, pos, TRACE_PIDS);
1721 }
1722
1723 static void *
np_next(struct seq_file * m,void * v,loff_t * pos)1724 np_next(struct seq_file *m, void *v, loff_t *pos)
1725 {
1726 return __next(m, v, pos, TRACE_NO_PIDS);
1727 }
1728
__start(struct seq_file * m,loff_t * pos,int type)1729 static void *__start(struct seq_file *m, loff_t *pos, int type)
1730 __acquires(RCU)
1731 {
1732 struct trace_pid_list *pid_list;
1733 struct trace_array *tr = m->private;
1734
1735 /*
1736 * Grab the mutex, to keep calls to p_next() having the same
1737 * tr->filtered_pids as p_start() has.
1738 * If we just passed the tr->filtered_pids around, then RCU would
1739 * have been enough, but doing that makes things more complex.
1740 */
1741 mutex_lock(&event_mutex);
1742 rcu_read_lock_sched();
1743
1744 if (type == TRACE_PIDS)
1745 pid_list = rcu_dereference_sched(tr->filtered_pids);
1746 else
1747 pid_list = rcu_dereference_sched(tr->filtered_no_pids);
1748
1749 if (!pid_list)
1750 return NULL;
1751
1752 return trace_pid_start(pid_list, pos);
1753 }
1754
p_start(struct seq_file * m,loff_t * pos)1755 static void *p_start(struct seq_file *m, loff_t *pos)
1756 __acquires(RCU)
1757 {
1758 return __start(m, pos, TRACE_PIDS);
1759 }
1760
np_start(struct seq_file * m,loff_t * pos)1761 static void *np_start(struct seq_file *m, loff_t *pos)
1762 __acquires(RCU)
1763 {
1764 return __start(m, pos, TRACE_NO_PIDS);
1765 }
1766
p_stop(struct seq_file * m,void * p)1767 static void p_stop(struct seq_file *m, void *p)
1768 __releases(RCU)
1769 {
1770 rcu_read_unlock_sched();
1771 mutex_unlock(&event_mutex);
1772 }
1773
1774 static ssize_t
event_enable_read(struct file * filp,char __user * ubuf,size_t cnt,loff_t * ppos)1775 event_enable_read(struct file *filp, char __user *ubuf, size_t cnt,
1776 loff_t *ppos)
1777 {
1778 struct trace_event_file *file;
1779 unsigned long flags;
1780 char buf[4] = "0";
1781
1782 mutex_lock(&event_mutex);
1783 file = event_file_file(filp);
1784 if (likely(file))
1785 flags = file->flags;
1786 mutex_unlock(&event_mutex);
1787
1788 if (!file)
1789 return -ENODEV;
1790
1791 if (flags & EVENT_FILE_FL_ENABLED &&
1792 !(flags & EVENT_FILE_FL_SOFT_DISABLED))
1793 strcpy(buf, "1");
1794
1795 if (flags & EVENT_FILE_FL_SOFT_DISABLED ||
1796 flags & EVENT_FILE_FL_SOFT_MODE)
1797 strcat(buf, "*");
1798
1799 strcat(buf, "\n");
1800
1801 return simple_read_from_buffer(ubuf, cnt, ppos, buf, strlen(buf));
1802 }
1803
1804 static ssize_t
event_enable_write(struct file * filp,const char __user * ubuf,size_t cnt,loff_t * ppos)1805 event_enable_write(struct file *filp, const char __user *ubuf, size_t cnt,
1806 loff_t *ppos)
1807 {
1808 struct trace_event_file *file;
1809 unsigned long val;
1810 int ret;
1811
1812 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
1813 if (ret)
1814 return ret;
1815
1816 guard(mutex)(&event_mutex);
1817
1818 switch (val) {
1819 case 0:
1820 case 1:
1821 file = event_file_file(filp);
1822 if (!file)
1823 return -ENODEV;
1824 ret = tracing_update_buffers(file->tr);
1825 if (ret < 0)
1826 return ret;
1827 ret = ftrace_event_enable_disable(file, val);
1828 if (ret < 0)
1829 return ret;
1830 break;
1831
1832 default:
1833 return -EINVAL;
1834 }
1835
1836 *ppos += cnt;
1837
1838 return cnt;
1839 }
1840
1841 /*
1842 * Returns:
1843 * 0 : no events exist?
1844 * 1 : all events are disabled
1845 * 2 : all events are enabled
1846 * 3 : some events are enabled and some are enabled
1847 */
trace_events_enabled(struct trace_array * tr,const char * system)1848 int trace_events_enabled(struct trace_array *tr, const char *system)
1849 {
1850 struct trace_event_call *call;
1851 struct trace_event_file *file;
1852 int set = 0;
1853
1854 guard(mutex)(&event_mutex);
1855
1856 list_for_each_entry(file, &tr->events, list) {
1857 call = file->event_call;
1858 if ((call->flags & TRACE_EVENT_FL_IGNORE_ENABLE) ||
1859 !trace_event_name(call) || !call->class || !call->class->reg)
1860 continue;
1861
1862 if (system && strcmp(call->class->system, system) != 0)
1863 continue;
1864
1865 /*
1866 * We need to find out if all the events are set
1867 * or if all events or cleared, or if we have
1868 * a mixture.
1869 */
1870 set |= (1 << !!(file->flags & EVENT_FILE_FL_ENABLED));
1871
1872 /*
1873 * If we have a mixture, no need to look further.
1874 */
1875 if (set == 3)
1876 break;
1877 }
1878
1879 return set;
1880 }
1881
1882 static ssize_t
system_enable_read(struct file * filp,char __user * ubuf,size_t cnt,loff_t * ppos)1883 system_enable_read(struct file *filp, char __user *ubuf, size_t cnt,
1884 loff_t *ppos)
1885 {
1886 const char set_to_char[4] = { '?', '0', '1', 'X' };
1887 struct trace_subsystem_dir *dir = filp->private_data;
1888 struct event_subsystem *system = dir->subsystem;
1889 struct trace_array *tr = dir->tr;
1890 char buf[2];
1891 int set;
1892 int ret;
1893
1894 set = trace_events_enabled(tr, system ? system->name : NULL);
1895
1896 buf[0] = set_to_char[set];
1897 buf[1] = '\n';
1898
1899 ret = simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
1900
1901 return ret;
1902 }
1903
1904 static ssize_t
system_enable_write(struct file * filp,const char __user * ubuf,size_t cnt,loff_t * ppos)1905 system_enable_write(struct file *filp, const char __user *ubuf, size_t cnt,
1906 loff_t *ppos)
1907 {
1908 struct trace_subsystem_dir *dir = filp->private_data;
1909 struct event_subsystem *system = dir->subsystem;
1910 const char *name = NULL;
1911 unsigned long val;
1912 ssize_t ret;
1913
1914 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
1915 if (ret)
1916 return ret;
1917
1918 ret = tracing_update_buffers(dir->tr);
1919 if (ret < 0)
1920 return ret;
1921
1922 if (val != 0 && val != 1)
1923 return -EINVAL;
1924
1925 /*
1926 * Opening of "enable" adds a ref count to system,
1927 * so the name is safe to use.
1928 */
1929 if (system)
1930 name = system->name;
1931
1932 ret = __ftrace_set_clr_event(dir->tr, NULL, name, NULL, val, NULL);
1933 if (ret)
1934 goto out;
1935
1936 ret = cnt;
1937
1938 out:
1939 *ppos += cnt;
1940
1941 return ret;
1942 }
1943
1944 enum {
1945 FORMAT_HEADER = 1,
1946 FORMAT_FIELD_SEPERATOR = 2,
1947 FORMAT_PRINTFMT = 3,
1948 };
1949
f_next(struct seq_file * m,void * v,loff_t * pos)1950 static void *f_next(struct seq_file *m, void *v, loff_t *pos)
1951 {
1952 struct trace_event_file *file = event_file_data(m->private);
1953 struct trace_event_call *call = file->event_call;
1954 struct list_head *common_head = &ftrace_common_fields;
1955 struct list_head *head = trace_get_fields(call);
1956 struct list_head *node = v;
1957
1958 (*pos)++;
1959
1960 switch ((unsigned long)v) {
1961 case FORMAT_HEADER:
1962 node = common_head;
1963 break;
1964
1965 case FORMAT_FIELD_SEPERATOR:
1966 node = head;
1967 break;
1968
1969 case FORMAT_PRINTFMT:
1970 /* all done */
1971 return NULL;
1972 }
1973
1974 node = node->prev;
1975 if (node == common_head)
1976 return (void *)FORMAT_FIELD_SEPERATOR;
1977 else if (node == head)
1978 return (void *)FORMAT_PRINTFMT;
1979 else
1980 return node;
1981 }
1982
f_show(struct seq_file * m,void * v)1983 static int f_show(struct seq_file *m, void *v)
1984 {
1985 struct trace_event_file *file = event_file_data(m->private);
1986 struct trace_event_call *call = file->event_call;
1987 struct ftrace_event_field *field;
1988 const char *array_descriptor;
1989
1990 switch ((unsigned long)v) {
1991 case FORMAT_HEADER:
1992 seq_printf(m, "name: %s\n", trace_event_name(call));
1993 seq_printf(m, "ID: %d\n", call->event.type);
1994 seq_puts(m, "format:\n");
1995 return 0;
1996
1997 case FORMAT_FIELD_SEPERATOR:
1998 seq_putc(m, '\n');
1999 return 0;
2000
2001 case FORMAT_PRINTFMT:
2002 seq_printf(m, "\nprint fmt: %s\n",
2003 call->print_fmt);
2004 return 0;
2005 }
2006
2007 field = list_entry(v, struct ftrace_event_field, link);
2008 /*
2009 * Smartly shows the array type(except dynamic array).
2010 * Normal:
2011 * field:TYPE VAR
2012 * If TYPE := TYPE[LEN], it is shown:
2013 * field:TYPE VAR[LEN]
2014 */
2015 array_descriptor = strchr(field->type, '[');
2016
2017 if (str_has_prefix(field->type, "__data_loc"))
2018 array_descriptor = NULL;
2019
2020 if (!array_descriptor)
2021 seq_printf(m, "\tfield:%s %s;\toffset:%u;\tsize:%u;\tsigned:%d;\n",
2022 field->type, field->name, field->offset,
2023 field->size, !!field->is_signed);
2024 else if (field->len)
2025 seq_printf(m, "\tfield:%.*s %s[%d];\toffset:%u;\tsize:%u;\tsigned:%d;\n",
2026 (int)(array_descriptor - field->type),
2027 field->type, field->name,
2028 field->len, field->offset,
2029 field->size, !!field->is_signed);
2030 else
2031 seq_printf(m, "\tfield:%.*s %s[];\toffset:%u;\tsize:%u;\tsigned:%d;\n",
2032 (int)(array_descriptor - field->type),
2033 field->type, field->name,
2034 field->offset, field->size, !!field->is_signed);
2035
2036 return 0;
2037 }
2038
f_start(struct seq_file * m,loff_t * pos)2039 static void *f_start(struct seq_file *m, loff_t *pos)
2040 {
2041 struct trace_event_file *file;
2042 void *p = (void *)FORMAT_HEADER;
2043 loff_t l = 0;
2044
2045 /* ->stop() is called even if ->start() fails */
2046 mutex_lock(&event_mutex);
2047 file = event_file_file(m->private);
2048 if (!file)
2049 return ERR_PTR(-ENODEV);
2050
2051 while (l < *pos && p)
2052 p = f_next(m, p, &l);
2053
2054 return p;
2055 }
2056
f_stop(struct seq_file * m,void * p)2057 static void f_stop(struct seq_file *m, void *p)
2058 {
2059 mutex_unlock(&event_mutex);
2060 }
2061
2062 static const struct seq_operations trace_format_seq_ops = {
2063 .start = f_start,
2064 .next = f_next,
2065 .stop = f_stop,
2066 .show = f_show,
2067 };
2068
trace_format_open(struct inode * inode,struct file * file)2069 static int trace_format_open(struct inode *inode, struct file *file)
2070 {
2071 struct seq_file *m;
2072 int ret;
2073
2074 /* Do we want to hide event format files on tracefs lockdown? */
2075
2076 ret = seq_open(file, &trace_format_seq_ops);
2077 if (ret < 0)
2078 return ret;
2079
2080 m = file->private_data;
2081 m->private = file;
2082
2083 return 0;
2084 }
2085
2086 #ifdef CONFIG_PERF_EVENTS
2087 static ssize_t
event_id_read(struct file * filp,char __user * ubuf,size_t cnt,loff_t * ppos)2088 event_id_read(struct file *filp, char __user *ubuf, size_t cnt, loff_t *ppos)
2089 {
2090 int id = (long)event_file_data(filp);
2091 char buf[32];
2092 int len;
2093
2094 if (unlikely(!id))
2095 return -ENODEV;
2096
2097 len = sprintf(buf, "%d\n", id);
2098
2099 return simple_read_from_buffer(ubuf, cnt, ppos, buf, len);
2100 }
2101 #endif
2102
2103 static ssize_t
event_filter_read(struct file * filp,char __user * ubuf,size_t cnt,loff_t * ppos)2104 event_filter_read(struct file *filp, char __user *ubuf, size_t cnt,
2105 loff_t *ppos)
2106 {
2107 struct trace_event_file *file;
2108 struct trace_seq *s;
2109 int r = -ENODEV;
2110
2111 if (*ppos)
2112 return 0;
2113
2114 s = kmalloc(sizeof(*s), GFP_KERNEL);
2115
2116 if (!s)
2117 return -ENOMEM;
2118
2119 trace_seq_init(s);
2120
2121 mutex_lock(&event_mutex);
2122 file = event_file_file(filp);
2123 if (file)
2124 print_event_filter(file, s);
2125 mutex_unlock(&event_mutex);
2126
2127 if (file)
2128 r = simple_read_from_buffer(ubuf, cnt, ppos,
2129 s->buffer, trace_seq_used(s));
2130
2131 kfree(s);
2132
2133 return r;
2134 }
2135
2136 static ssize_t
event_filter_write(struct file * filp,const char __user * ubuf,size_t cnt,loff_t * ppos)2137 event_filter_write(struct file *filp, const char __user *ubuf, size_t cnt,
2138 loff_t *ppos)
2139 {
2140 struct trace_event_file *file;
2141 char *buf;
2142 int err = -ENODEV;
2143
2144 if (cnt >= PAGE_SIZE)
2145 return -EINVAL;
2146
2147 buf = memdup_user_nul(ubuf, cnt);
2148 if (IS_ERR(buf))
2149 return PTR_ERR(buf);
2150
2151 mutex_lock(&event_mutex);
2152 file = event_file_file(filp);
2153 if (file) {
2154 if (file->flags & EVENT_FILE_FL_FREED)
2155 err = -ENODEV;
2156 else
2157 err = apply_event_filter(file, buf);
2158 }
2159 mutex_unlock(&event_mutex);
2160
2161 kfree(buf);
2162 if (err < 0)
2163 return err;
2164
2165 *ppos += cnt;
2166
2167 return cnt;
2168 }
2169
2170 static LIST_HEAD(event_subsystems);
2171
subsystem_open(struct inode * inode,struct file * filp)2172 static int subsystem_open(struct inode *inode, struct file *filp)
2173 {
2174 struct trace_subsystem_dir *dir = NULL, *iter_dir;
2175 struct trace_array *tr = NULL, *iter_tr;
2176 struct event_subsystem *system = NULL;
2177 int ret;
2178
2179 if (tracing_is_disabled())
2180 return -ENODEV;
2181
2182 /* Make sure the system still exists */
2183 mutex_lock(&event_mutex);
2184 mutex_lock(&trace_types_lock);
2185 list_for_each_entry(iter_tr, &ftrace_trace_arrays, list) {
2186 list_for_each_entry(iter_dir, &iter_tr->systems, list) {
2187 if (iter_dir == inode->i_private) {
2188 /* Don't open systems with no events */
2189 tr = iter_tr;
2190 dir = iter_dir;
2191 if (dir->nr_events) {
2192 __get_system_dir(dir);
2193 system = dir->subsystem;
2194 }
2195 goto exit_loop;
2196 }
2197 }
2198 }
2199 exit_loop:
2200 mutex_unlock(&trace_types_lock);
2201 mutex_unlock(&event_mutex);
2202
2203 if (!system)
2204 return -ENODEV;
2205
2206 /* Still need to increment the ref count of the system */
2207 if (trace_array_get(tr) < 0) {
2208 put_system(dir);
2209 return -ENODEV;
2210 }
2211
2212 ret = tracing_open_generic(inode, filp);
2213 if (ret < 0) {
2214 trace_array_put(tr);
2215 put_system(dir);
2216 }
2217
2218 return ret;
2219 }
2220
system_tr_open(struct inode * inode,struct file * filp)2221 static int system_tr_open(struct inode *inode, struct file *filp)
2222 {
2223 struct trace_subsystem_dir *dir;
2224 struct trace_array *tr = inode->i_private;
2225 int ret;
2226
2227 /* Make a temporary dir that has no system but points to tr */
2228 dir = kzalloc(sizeof(*dir), GFP_KERNEL);
2229 if (!dir)
2230 return -ENOMEM;
2231
2232 ret = tracing_open_generic_tr(inode, filp);
2233 if (ret < 0) {
2234 kfree(dir);
2235 return ret;
2236 }
2237 dir->tr = tr;
2238 filp->private_data = dir;
2239
2240 return 0;
2241 }
2242
subsystem_release(struct inode * inode,struct file * file)2243 static int subsystem_release(struct inode *inode, struct file *file)
2244 {
2245 struct trace_subsystem_dir *dir = file->private_data;
2246
2247 trace_array_put(dir->tr);
2248
2249 /*
2250 * If dir->subsystem is NULL, then this is a temporary
2251 * descriptor that was made for a trace_array to enable
2252 * all subsystems.
2253 */
2254 if (dir->subsystem)
2255 put_system(dir);
2256 else
2257 kfree(dir);
2258
2259 return 0;
2260 }
2261
2262 static ssize_t
subsystem_filter_read(struct file * filp,char __user * ubuf,size_t cnt,loff_t * ppos)2263 subsystem_filter_read(struct file *filp, char __user *ubuf, size_t cnt,
2264 loff_t *ppos)
2265 {
2266 struct trace_subsystem_dir *dir = filp->private_data;
2267 struct event_subsystem *system = dir->subsystem;
2268 struct trace_seq *s;
2269 int r;
2270
2271 if (*ppos)
2272 return 0;
2273
2274 s = kmalloc(sizeof(*s), GFP_KERNEL);
2275 if (!s)
2276 return -ENOMEM;
2277
2278 trace_seq_init(s);
2279
2280 print_subsystem_event_filter(system, s);
2281 r = simple_read_from_buffer(ubuf, cnt, ppos,
2282 s->buffer, trace_seq_used(s));
2283
2284 kfree(s);
2285
2286 return r;
2287 }
2288
2289 static ssize_t
subsystem_filter_write(struct file * filp,const char __user * ubuf,size_t cnt,loff_t * ppos)2290 subsystem_filter_write(struct file *filp, const char __user *ubuf, size_t cnt,
2291 loff_t *ppos)
2292 {
2293 struct trace_subsystem_dir *dir = filp->private_data;
2294 char *buf;
2295 int err;
2296
2297 if (cnt >= PAGE_SIZE)
2298 return -EINVAL;
2299
2300 buf = memdup_user_nul(ubuf, cnt);
2301 if (IS_ERR(buf))
2302 return PTR_ERR(buf);
2303
2304 err = apply_subsystem_event_filter(dir, buf);
2305 kfree(buf);
2306 if (err < 0)
2307 return err;
2308
2309 *ppos += cnt;
2310
2311 return cnt;
2312 }
2313
2314 static ssize_t
show_header_page_file(struct file * filp,char __user * ubuf,size_t cnt,loff_t * ppos)2315 show_header_page_file(struct file *filp, char __user *ubuf, size_t cnt, loff_t *ppos)
2316 {
2317 struct trace_array *tr = filp->private_data;
2318 struct trace_seq *s;
2319 int r;
2320
2321 if (*ppos)
2322 return 0;
2323
2324 s = kmalloc(sizeof(*s), GFP_KERNEL);
2325 if (!s)
2326 return -ENOMEM;
2327
2328 trace_seq_init(s);
2329
2330 ring_buffer_print_page_header(tr->array_buffer.buffer, s);
2331 r = simple_read_from_buffer(ubuf, cnt, ppos,
2332 s->buffer, trace_seq_used(s));
2333
2334 kfree(s);
2335
2336 return r;
2337 }
2338
2339 static ssize_t
show_header_event_file(struct file * filp,char __user * ubuf,size_t cnt,loff_t * ppos)2340 show_header_event_file(struct file *filp, char __user *ubuf, size_t cnt, loff_t *ppos)
2341 {
2342 struct trace_seq *s;
2343 int r;
2344
2345 if (*ppos)
2346 return 0;
2347
2348 s = kmalloc(sizeof(*s), GFP_KERNEL);
2349 if (!s)
2350 return -ENOMEM;
2351
2352 trace_seq_init(s);
2353
2354 ring_buffer_print_entry_header(s);
2355 r = simple_read_from_buffer(ubuf, cnt, ppos,
2356 s->buffer, trace_seq_used(s));
2357
2358 kfree(s);
2359
2360 return r;
2361 }
2362
ignore_task_cpu(void * data)2363 static void ignore_task_cpu(void *data)
2364 {
2365 struct trace_array *tr = data;
2366 struct trace_pid_list *pid_list;
2367 struct trace_pid_list *no_pid_list;
2368
2369 /*
2370 * This function is called by on_each_cpu() while the
2371 * event_mutex is held.
2372 */
2373 pid_list = rcu_dereference_protected(tr->filtered_pids,
2374 mutex_is_locked(&event_mutex));
2375 no_pid_list = rcu_dereference_protected(tr->filtered_no_pids,
2376 mutex_is_locked(&event_mutex));
2377
2378 this_cpu_write(tr->array_buffer.data->ignore_pid,
2379 trace_ignore_this_task(pid_list, no_pid_list, current));
2380 }
2381
register_pid_events(struct trace_array * tr)2382 static void register_pid_events(struct trace_array *tr)
2383 {
2384 /*
2385 * Register a probe that is called before all other probes
2386 * to set ignore_pid if next or prev do not match.
2387 * Register a probe this is called after all other probes
2388 * to only keep ignore_pid set if next pid matches.
2389 */
2390 register_trace_prio_sched_switch(event_filter_pid_sched_switch_probe_pre,
2391 tr, INT_MAX);
2392 register_trace_prio_sched_switch(event_filter_pid_sched_switch_probe_post,
2393 tr, 0);
2394
2395 register_trace_prio_sched_wakeup(event_filter_pid_sched_wakeup_probe_pre,
2396 tr, INT_MAX);
2397 register_trace_prio_sched_wakeup(event_filter_pid_sched_wakeup_probe_post,
2398 tr, 0);
2399
2400 register_trace_prio_sched_wakeup_new(event_filter_pid_sched_wakeup_probe_pre,
2401 tr, INT_MAX);
2402 register_trace_prio_sched_wakeup_new(event_filter_pid_sched_wakeup_probe_post,
2403 tr, 0);
2404
2405 register_trace_prio_sched_waking(event_filter_pid_sched_wakeup_probe_pre,
2406 tr, INT_MAX);
2407 register_trace_prio_sched_waking(event_filter_pid_sched_wakeup_probe_post,
2408 tr, 0);
2409 }
2410
2411 static ssize_t
event_pid_write(struct file * filp,const char __user * ubuf,size_t cnt,loff_t * ppos,int type)2412 event_pid_write(struct file *filp, const char __user *ubuf,
2413 size_t cnt, loff_t *ppos, int type)
2414 {
2415 struct seq_file *m = filp->private_data;
2416 struct trace_array *tr = m->private;
2417 struct trace_pid_list *filtered_pids = NULL;
2418 struct trace_pid_list *other_pids = NULL;
2419 struct trace_pid_list *pid_list;
2420 struct trace_event_file *file;
2421 ssize_t ret;
2422
2423 if (!cnt)
2424 return 0;
2425
2426 ret = tracing_update_buffers(tr);
2427 if (ret < 0)
2428 return ret;
2429
2430 guard(mutex)(&event_mutex);
2431
2432 if (type == TRACE_PIDS) {
2433 filtered_pids = rcu_dereference_protected(tr->filtered_pids,
2434 lockdep_is_held(&event_mutex));
2435 other_pids = rcu_dereference_protected(tr->filtered_no_pids,
2436 lockdep_is_held(&event_mutex));
2437 } else {
2438 filtered_pids = rcu_dereference_protected(tr->filtered_no_pids,
2439 lockdep_is_held(&event_mutex));
2440 other_pids = rcu_dereference_protected(tr->filtered_pids,
2441 lockdep_is_held(&event_mutex));
2442 }
2443
2444 ret = trace_pid_write(filtered_pids, &pid_list, ubuf, cnt);
2445 if (ret < 0)
2446 return ret;
2447
2448 if (type == TRACE_PIDS)
2449 rcu_assign_pointer(tr->filtered_pids, pid_list);
2450 else
2451 rcu_assign_pointer(tr->filtered_no_pids, pid_list);
2452
2453 list_for_each_entry(file, &tr->events, list) {
2454 set_bit(EVENT_FILE_FL_PID_FILTER_BIT, &file->flags);
2455 }
2456
2457 if (filtered_pids) {
2458 tracepoint_synchronize_unregister();
2459 trace_pid_list_free(filtered_pids);
2460 } else if (pid_list && !other_pids) {
2461 register_pid_events(tr);
2462 }
2463
2464 /*
2465 * Ignoring of pids is done at task switch. But we have to
2466 * check for those tasks that are currently running.
2467 * Always do this in case a pid was appended or removed.
2468 */
2469 on_each_cpu(ignore_task_cpu, tr, 1);
2470
2471 *ppos += ret;
2472
2473 return ret;
2474 }
2475
2476 static ssize_t
ftrace_event_pid_write(struct file * filp,const char __user * ubuf,size_t cnt,loff_t * ppos)2477 ftrace_event_pid_write(struct file *filp, const char __user *ubuf,
2478 size_t cnt, loff_t *ppos)
2479 {
2480 return event_pid_write(filp, ubuf, cnt, ppos, TRACE_PIDS);
2481 }
2482
2483 static ssize_t
ftrace_event_npid_write(struct file * filp,const char __user * ubuf,size_t cnt,loff_t * ppos)2484 ftrace_event_npid_write(struct file *filp, const char __user *ubuf,
2485 size_t cnt, loff_t *ppos)
2486 {
2487 return event_pid_write(filp, ubuf, cnt, ppos, TRACE_NO_PIDS);
2488 }
2489
2490 static int ftrace_event_avail_open(struct inode *inode, struct file *file);
2491 static int ftrace_event_set_open(struct inode *inode, struct file *file);
2492 static int ftrace_event_set_pid_open(struct inode *inode, struct file *file);
2493 static int ftrace_event_set_npid_open(struct inode *inode, struct file *file);
2494 static int ftrace_event_release(struct inode *inode, struct file *file);
2495
2496 static const struct seq_operations show_event_seq_ops = {
2497 .start = t_start,
2498 .next = t_next,
2499 .show = t_show,
2500 .stop = t_stop,
2501 };
2502
2503 static const struct seq_operations show_set_event_seq_ops = {
2504 .start = s_start,
2505 .next = s_next,
2506 .show = s_show,
2507 .stop = s_stop,
2508 };
2509
2510 static const struct seq_operations show_set_pid_seq_ops = {
2511 .start = p_start,
2512 .next = p_next,
2513 .show = trace_pid_show,
2514 .stop = p_stop,
2515 };
2516
2517 static const struct seq_operations show_set_no_pid_seq_ops = {
2518 .start = np_start,
2519 .next = np_next,
2520 .show = trace_pid_show,
2521 .stop = p_stop,
2522 };
2523
2524 static const struct file_operations ftrace_avail_fops = {
2525 .open = ftrace_event_avail_open,
2526 .read = seq_read,
2527 .llseek = seq_lseek,
2528 .release = seq_release,
2529 };
2530
2531 static const struct file_operations ftrace_set_event_fops = {
2532 .open = ftrace_event_set_open,
2533 .read = seq_read,
2534 .write = ftrace_event_write,
2535 .llseek = seq_lseek,
2536 .release = ftrace_event_release,
2537 };
2538
2539 static const struct file_operations ftrace_set_event_pid_fops = {
2540 .open = ftrace_event_set_pid_open,
2541 .read = seq_read,
2542 .write = ftrace_event_pid_write,
2543 .llseek = seq_lseek,
2544 .release = ftrace_event_release,
2545 };
2546
2547 static const struct file_operations ftrace_set_event_notrace_pid_fops = {
2548 .open = ftrace_event_set_npid_open,
2549 .read = seq_read,
2550 .write = ftrace_event_npid_write,
2551 .llseek = seq_lseek,
2552 .release = ftrace_event_release,
2553 };
2554
2555 static const struct file_operations ftrace_enable_fops = {
2556 .open = tracing_open_file_tr,
2557 .read = event_enable_read,
2558 .write = event_enable_write,
2559 .release = tracing_release_file_tr,
2560 .llseek = default_llseek,
2561 };
2562
2563 static const struct file_operations ftrace_event_format_fops = {
2564 .open = trace_format_open,
2565 .read = seq_read,
2566 .llseek = seq_lseek,
2567 .release = seq_release,
2568 };
2569
2570 #ifdef CONFIG_PERF_EVENTS
2571 static const struct file_operations ftrace_event_id_fops = {
2572 .read = event_id_read,
2573 .llseek = default_llseek,
2574 };
2575 #endif
2576
2577 static const struct file_operations ftrace_event_filter_fops = {
2578 .open = tracing_open_file_tr,
2579 .read = event_filter_read,
2580 .write = event_filter_write,
2581 .release = tracing_release_file_tr,
2582 .llseek = default_llseek,
2583 };
2584
2585 static const struct file_operations ftrace_subsystem_filter_fops = {
2586 .open = subsystem_open,
2587 .read = subsystem_filter_read,
2588 .write = subsystem_filter_write,
2589 .llseek = default_llseek,
2590 .release = subsystem_release,
2591 };
2592
2593 static const struct file_operations ftrace_system_enable_fops = {
2594 .open = subsystem_open,
2595 .read = system_enable_read,
2596 .write = system_enable_write,
2597 .llseek = default_llseek,
2598 .release = subsystem_release,
2599 };
2600
2601 static const struct file_operations ftrace_tr_enable_fops = {
2602 .open = system_tr_open,
2603 .read = system_enable_read,
2604 .write = system_enable_write,
2605 .llseek = default_llseek,
2606 .release = subsystem_release,
2607 };
2608
2609 static const struct file_operations ftrace_show_header_page_fops = {
2610 .open = tracing_open_generic_tr,
2611 .read = show_header_page_file,
2612 .llseek = default_llseek,
2613 .release = tracing_release_generic_tr,
2614 };
2615
2616 static const struct file_operations ftrace_show_header_event_fops = {
2617 .open = tracing_open_generic_tr,
2618 .read = show_header_event_file,
2619 .llseek = default_llseek,
2620 .release = tracing_release_generic_tr,
2621 };
2622
2623 static int
ftrace_event_open(struct inode * inode,struct file * file,const struct seq_operations * seq_ops)2624 ftrace_event_open(struct inode *inode, struct file *file,
2625 const struct seq_operations *seq_ops)
2626 {
2627 struct seq_file *m;
2628 int ret;
2629
2630 ret = security_locked_down(LOCKDOWN_TRACEFS);
2631 if (ret)
2632 return ret;
2633
2634 ret = seq_open(file, seq_ops);
2635 if (ret < 0)
2636 return ret;
2637 m = file->private_data;
2638 /* copy tr over to seq ops */
2639 m->private = inode->i_private;
2640
2641 return ret;
2642 }
2643
ftrace_event_release(struct inode * inode,struct file * file)2644 static int ftrace_event_release(struct inode *inode, struct file *file)
2645 {
2646 struct trace_array *tr = inode->i_private;
2647
2648 trace_array_put(tr);
2649
2650 return seq_release(inode, file);
2651 }
2652
2653 static int
ftrace_event_avail_open(struct inode * inode,struct file * file)2654 ftrace_event_avail_open(struct inode *inode, struct file *file)
2655 {
2656 const struct seq_operations *seq_ops = &show_event_seq_ops;
2657
2658 /* Checks for tracefs lockdown */
2659 return ftrace_event_open(inode, file, seq_ops);
2660 }
2661
2662 static int
ftrace_event_set_open(struct inode * inode,struct file * file)2663 ftrace_event_set_open(struct inode *inode, struct file *file)
2664 {
2665 const struct seq_operations *seq_ops = &show_set_event_seq_ops;
2666 struct trace_array *tr = inode->i_private;
2667 int ret;
2668
2669 ret = tracing_check_open_get_tr(tr);
2670 if (ret)
2671 return ret;
2672
2673 if ((file->f_mode & FMODE_WRITE) &&
2674 (file->f_flags & O_TRUNC))
2675 ftrace_clear_events(tr);
2676
2677 ret = ftrace_event_open(inode, file, seq_ops);
2678 if (ret < 0)
2679 trace_array_put(tr);
2680 return ret;
2681 }
2682
2683 static int
ftrace_event_set_pid_open(struct inode * inode,struct file * file)2684 ftrace_event_set_pid_open(struct inode *inode, struct file *file)
2685 {
2686 const struct seq_operations *seq_ops = &show_set_pid_seq_ops;
2687 struct trace_array *tr = inode->i_private;
2688 int ret;
2689
2690 ret = tracing_check_open_get_tr(tr);
2691 if (ret)
2692 return ret;
2693
2694 if ((file->f_mode & FMODE_WRITE) &&
2695 (file->f_flags & O_TRUNC))
2696 ftrace_clear_event_pids(tr, TRACE_PIDS);
2697
2698 ret = ftrace_event_open(inode, file, seq_ops);
2699 if (ret < 0)
2700 trace_array_put(tr);
2701 return ret;
2702 }
2703
2704 static int
ftrace_event_set_npid_open(struct inode * inode,struct file * file)2705 ftrace_event_set_npid_open(struct inode *inode, struct file *file)
2706 {
2707 const struct seq_operations *seq_ops = &show_set_no_pid_seq_ops;
2708 struct trace_array *tr = inode->i_private;
2709 int ret;
2710
2711 ret = tracing_check_open_get_tr(tr);
2712 if (ret)
2713 return ret;
2714
2715 if ((file->f_mode & FMODE_WRITE) &&
2716 (file->f_flags & O_TRUNC))
2717 ftrace_clear_event_pids(tr, TRACE_NO_PIDS);
2718
2719 ret = ftrace_event_open(inode, file, seq_ops);
2720 if (ret < 0)
2721 trace_array_put(tr);
2722 return ret;
2723 }
2724
2725 static struct event_subsystem *
create_new_subsystem(const char * name)2726 create_new_subsystem(const char *name)
2727 {
2728 struct event_subsystem *system;
2729
2730 /* need to create new entry */
2731 system = kmalloc(sizeof(*system), GFP_KERNEL);
2732 if (!system)
2733 return NULL;
2734
2735 system->ref_count = 1;
2736
2737 /* Only allocate if dynamic (kprobes and modules) */
2738 system->name = kstrdup_const(name, GFP_KERNEL);
2739 if (!system->name)
2740 goto out_free;
2741
2742 system->filter = kzalloc(sizeof(struct event_filter), GFP_KERNEL);
2743 if (!system->filter)
2744 goto out_free;
2745
2746 list_add(&system->list, &event_subsystems);
2747
2748 return system;
2749
2750 out_free:
2751 kfree_const(system->name);
2752 kfree(system);
2753 return NULL;
2754 }
2755
system_callback(const char * name,umode_t * mode,void ** data,const struct file_operations ** fops)2756 static int system_callback(const char *name, umode_t *mode, void **data,
2757 const struct file_operations **fops)
2758 {
2759 if (strcmp(name, "filter") == 0)
2760 *fops = &ftrace_subsystem_filter_fops;
2761
2762 else if (strcmp(name, "enable") == 0)
2763 *fops = &ftrace_system_enable_fops;
2764
2765 else
2766 return 0;
2767
2768 *mode = TRACE_MODE_WRITE;
2769 return 1;
2770 }
2771
2772 static struct eventfs_inode *
event_subsystem_dir(struct trace_array * tr,const char * name,struct trace_event_file * file,struct eventfs_inode * parent)2773 event_subsystem_dir(struct trace_array *tr, const char *name,
2774 struct trace_event_file *file, struct eventfs_inode *parent)
2775 {
2776 struct event_subsystem *system, *iter;
2777 struct trace_subsystem_dir *dir;
2778 struct eventfs_inode *ei;
2779 int nr_entries;
2780 static struct eventfs_entry system_entries[] = {
2781 {
2782 .name = "filter",
2783 .callback = system_callback,
2784 },
2785 {
2786 .name = "enable",
2787 .callback = system_callback,
2788 }
2789 };
2790
2791 /* First see if we did not already create this dir */
2792 list_for_each_entry(dir, &tr->systems, list) {
2793 system = dir->subsystem;
2794 if (strcmp(system->name, name) == 0) {
2795 dir->nr_events++;
2796 file->system = dir;
2797 return dir->ei;
2798 }
2799 }
2800
2801 /* Now see if the system itself exists. */
2802 system = NULL;
2803 list_for_each_entry(iter, &event_subsystems, list) {
2804 if (strcmp(iter->name, name) == 0) {
2805 system = iter;
2806 break;
2807 }
2808 }
2809
2810 dir = kmalloc(sizeof(*dir), GFP_KERNEL);
2811 if (!dir)
2812 goto out_fail;
2813
2814 if (!system) {
2815 system = create_new_subsystem(name);
2816 if (!system)
2817 goto out_free;
2818 } else
2819 __get_system(system);
2820
2821 /* ftrace only has directories no files */
2822 if (strcmp(name, "ftrace") == 0)
2823 nr_entries = 0;
2824 else
2825 nr_entries = ARRAY_SIZE(system_entries);
2826
2827 ei = eventfs_create_dir(name, parent, system_entries, nr_entries, dir);
2828 if (IS_ERR(ei)) {
2829 pr_warn("Failed to create system directory %s\n", name);
2830 __put_system(system);
2831 goto out_free;
2832 }
2833
2834 dir->ei = ei;
2835 dir->tr = tr;
2836 dir->ref_count = 1;
2837 dir->nr_events = 1;
2838 dir->subsystem = system;
2839 file->system = dir;
2840
2841 list_add(&dir->list, &tr->systems);
2842
2843 return dir->ei;
2844
2845 out_free:
2846 kfree(dir);
2847 out_fail:
2848 /* Only print this message if failed on memory allocation */
2849 if (!dir || !system)
2850 pr_warn("No memory to create event subsystem %s\n", name);
2851 return NULL;
2852 }
2853
2854 static int
event_define_fields(struct trace_event_call * call)2855 event_define_fields(struct trace_event_call *call)
2856 {
2857 struct list_head *head;
2858 int ret = 0;
2859
2860 /*
2861 * Other events may have the same class. Only update
2862 * the fields if they are not already defined.
2863 */
2864 head = trace_get_fields(call);
2865 if (list_empty(head)) {
2866 struct trace_event_fields *field = call->class->fields_array;
2867 unsigned int offset = sizeof(struct trace_entry);
2868
2869 for (; field->type; field++) {
2870 if (field->type == TRACE_FUNCTION_TYPE) {
2871 field->define_fields(call);
2872 break;
2873 }
2874
2875 offset = ALIGN(offset, field->align);
2876 ret = trace_define_field_ext(call, field->type, field->name,
2877 offset, field->size,
2878 field->is_signed, field->filter_type,
2879 field->len, field->needs_test);
2880 if (WARN_ON_ONCE(ret)) {
2881 pr_err("error code is %d\n", ret);
2882 break;
2883 }
2884
2885 offset += field->size;
2886 }
2887 }
2888
2889 return ret;
2890 }
2891
event_callback(const char * name,umode_t * mode,void ** data,const struct file_operations ** fops)2892 static int event_callback(const char *name, umode_t *mode, void **data,
2893 const struct file_operations **fops)
2894 {
2895 struct trace_event_file *file = *data;
2896 struct trace_event_call *call = file->event_call;
2897
2898 if (strcmp(name, "format") == 0) {
2899 *mode = TRACE_MODE_READ;
2900 *fops = &ftrace_event_format_fops;
2901 return 1;
2902 }
2903
2904 /*
2905 * Only event directories that can be enabled should have
2906 * triggers or filters, with the exception of the "print"
2907 * event that can have a "trigger" file.
2908 */
2909 if (!(call->flags & TRACE_EVENT_FL_IGNORE_ENABLE)) {
2910 if (call->class->reg && strcmp(name, "enable") == 0) {
2911 *mode = TRACE_MODE_WRITE;
2912 *fops = &ftrace_enable_fops;
2913 return 1;
2914 }
2915
2916 if (strcmp(name, "filter") == 0) {
2917 *mode = TRACE_MODE_WRITE;
2918 *fops = &ftrace_event_filter_fops;
2919 return 1;
2920 }
2921 }
2922
2923 if (!(call->flags & TRACE_EVENT_FL_IGNORE_ENABLE) ||
2924 strcmp(trace_event_name(call), "print") == 0) {
2925 if (strcmp(name, "trigger") == 0) {
2926 *mode = TRACE_MODE_WRITE;
2927 *fops = &event_trigger_fops;
2928 return 1;
2929 }
2930 }
2931
2932 #ifdef CONFIG_PERF_EVENTS
2933 if (call->event.type && call->class->reg &&
2934 strcmp(name, "id") == 0) {
2935 *mode = TRACE_MODE_READ;
2936 *data = (void *)(long)call->event.type;
2937 *fops = &ftrace_event_id_fops;
2938 return 1;
2939 }
2940 #endif
2941
2942 #ifdef CONFIG_HIST_TRIGGERS
2943 if (strcmp(name, "hist") == 0) {
2944 *mode = TRACE_MODE_READ;
2945 *fops = &event_hist_fops;
2946 return 1;
2947 }
2948 #endif
2949 #ifdef CONFIG_HIST_TRIGGERS_DEBUG
2950 if (strcmp(name, "hist_debug") == 0) {
2951 *mode = TRACE_MODE_READ;
2952 *fops = &event_hist_debug_fops;
2953 return 1;
2954 }
2955 #endif
2956 #ifdef CONFIG_TRACE_EVENT_INJECT
2957 if (call->event.type && call->class->reg &&
2958 strcmp(name, "inject") == 0) {
2959 *mode = 0200;
2960 *fops = &event_inject_fops;
2961 return 1;
2962 }
2963 #endif
2964 return 0;
2965 }
2966
2967 /* The file is incremented on creation and freeing the enable file decrements it */
event_release(const char * name,void * data)2968 static void event_release(const char *name, void *data)
2969 {
2970 struct trace_event_file *file = data;
2971
2972 event_file_put(file);
2973 }
2974
2975 static int
event_create_dir(struct eventfs_inode * parent,struct trace_event_file * file)2976 event_create_dir(struct eventfs_inode *parent, struct trace_event_file *file)
2977 {
2978 struct trace_event_call *call = file->event_call;
2979 struct trace_array *tr = file->tr;
2980 struct eventfs_inode *e_events;
2981 struct eventfs_inode *ei;
2982 const char *name;
2983 int nr_entries;
2984 int ret;
2985 static struct eventfs_entry event_entries[] = {
2986 {
2987 .name = "enable",
2988 .callback = event_callback,
2989 .release = event_release,
2990 },
2991 {
2992 .name = "filter",
2993 .callback = event_callback,
2994 },
2995 {
2996 .name = "trigger",
2997 .callback = event_callback,
2998 },
2999 {
3000 .name = "format",
3001 .callback = event_callback,
3002 },
3003 #ifdef CONFIG_PERF_EVENTS
3004 {
3005 .name = "id",
3006 .callback = event_callback,
3007 },
3008 #endif
3009 #ifdef CONFIG_HIST_TRIGGERS
3010 {
3011 .name = "hist",
3012 .callback = event_callback,
3013 },
3014 #endif
3015 #ifdef CONFIG_HIST_TRIGGERS_DEBUG
3016 {
3017 .name = "hist_debug",
3018 .callback = event_callback,
3019 },
3020 #endif
3021 #ifdef CONFIG_TRACE_EVENT_INJECT
3022 {
3023 .name = "inject",
3024 .callback = event_callback,
3025 },
3026 #endif
3027 };
3028
3029 /*
3030 * If the trace point header did not define TRACE_SYSTEM
3031 * then the system would be called "TRACE_SYSTEM". This should
3032 * never happen.
3033 */
3034 if (WARN_ON_ONCE(strcmp(call->class->system, TRACE_SYSTEM) == 0))
3035 return -ENODEV;
3036
3037 e_events = event_subsystem_dir(tr, call->class->system, file, parent);
3038 if (!e_events)
3039 return -ENOMEM;
3040
3041 nr_entries = ARRAY_SIZE(event_entries);
3042
3043 name = trace_event_name(call);
3044 ei = eventfs_create_dir(name, e_events, event_entries, nr_entries, file);
3045 if (IS_ERR(ei)) {
3046 pr_warn("Could not create tracefs '%s' directory\n", name);
3047 return -1;
3048 }
3049
3050 file->ei = ei;
3051
3052 ret = event_define_fields(call);
3053 if (ret < 0) {
3054 pr_warn("Could not initialize trace point events/%s\n", name);
3055 return ret;
3056 }
3057
3058 /* Gets decremented on freeing of the "enable" file */
3059 event_file_get(file);
3060
3061 return 0;
3062 }
3063
remove_event_from_tracers(struct trace_event_call * call)3064 static void remove_event_from_tracers(struct trace_event_call *call)
3065 {
3066 struct trace_event_file *file;
3067 struct trace_array *tr;
3068
3069 do_for_each_event_file_safe(tr, file) {
3070 if (file->event_call != call)
3071 continue;
3072
3073 remove_event_file_dir(file);
3074 /*
3075 * The do_for_each_event_file_safe() is
3076 * a double loop. After finding the call for this
3077 * trace_array, we use break to jump to the next
3078 * trace_array.
3079 */
3080 break;
3081 } while_for_each_event_file();
3082 }
3083
event_remove(struct trace_event_call * call)3084 static void event_remove(struct trace_event_call *call)
3085 {
3086 struct trace_array *tr;
3087 struct trace_event_file *file;
3088
3089 do_for_each_event_file(tr, file) {
3090 if (file->event_call != call)
3091 continue;
3092
3093 if (file->flags & EVENT_FILE_FL_WAS_ENABLED)
3094 tr->clear_trace = true;
3095
3096 ftrace_event_enable_disable(file, 0);
3097 /*
3098 * The do_for_each_event_file() is
3099 * a double loop. After finding the call for this
3100 * trace_array, we use break to jump to the next
3101 * trace_array.
3102 */
3103 break;
3104 } while_for_each_event_file();
3105
3106 if (call->event.funcs)
3107 __unregister_trace_event(&call->event);
3108 remove_event_from_tracers(call);
3109 list_del(&call->list);
3110 }
3111
event_init(struct trace_event_call * call)3112 static int event_init(struct trace_event_call *call)
3113 {
3114 int ret = 0;
3115 const char *name;
3116
3117 name = trace_event_name(call);
3118 if (WARN_ON(!name))
3119 return -EINVAL;
3120
3121 if (call->class->raw_init) {
3122 ret = call->class->raw_init(call);
3123 if (ret < 0 && ret != -ENOSYS)
3124 pr_warn("Could not initialize trace events/%s\n", name);
3125 }
3126
3127 return ret;
3128 }
3129
3130 static int
__register_event(struct trace_event_call * call,struct module * mod)3131 __register_event(struct trace_event_call *call, struct module *mod)
3132 {
3133 int ret;
3134
3135 ret = event_init(call);
3136 if (ret < 0)
3137 return ret;
3138
3139 list_add(&call->list, &ftrace_events);
3140 if (call->flags & TRACE_EVENT_FL_DYNAMIC)
3141 atomic_set(&call->refcnt, 0);
3142 else
3143 call->module = mod;
3144
3145 return 0;
3146 }
3147
eval_replace(char * ptr,struct trace_eval_map * map,int len)3148 static char *eval_replace(char *ptr, struct trace_eval_map *map, int len)
3149 {
3150 int rlen;
3151 int elen;
3152
3153 /* Find the length of the eval value as a string */
3154 elen = snprintf(ptr, 0, "%ld", map->eval_value);
3155 /* Make sure there's enough room to replace the string with the value */
3156 if (len < elen)
3157 return NULL;
3158
3159 snprintf(ptr, elen + 1, "%ld", map->eval_value);
3160
3161 /* Get the rest of the string of ptr */
3162 rlen = strlen(ptr + len);
3163 memmove(ptr + elen, ptr + len, rlen);
3164 /* Make sure we end the new string */
3165 ptr[elen + rlen] = 0;
3166
3167 return ptr + elen;
3168 }
3169
update_event_printk(struct trace_event_call * call,struct trace_eval_map * map)3170 static void update_event_printk(struct trace_event_call *call,
3171 struct trace_eval_map *map)
3172 {
3173 char *ptr;
3174 int quote = 0;
3175 int len = strlen(map->eval_string);
3176
3177 for (ptr = call->print_fmt; *ptr; ptr++) {
3178 if (*ptr == '\\') {
3179 ptr++;
3180 /* paranoid */
3181 if (!*ptr)
3182 break;
3183 continue;
3184 }
3185 if (*ptr == '"') {
3186 quote ^= 1;
3187 continue;
3188 }
3189 if (quote)
3190 continue;
3191 if (isdigit(*ptr)) {
3192 /* skip numbers */
3193 do {
3194 ptr++;
3195 /* Check for alpha chars like ULL */
3196 } while (isalnum(*ptr));
3197 if (!*ptr)
3198 break;
3199 /*
3200 * A number must have some kind of delimiter after
3201 * it, and we can ignore that too.
3202 */
3203 continue;
3204 }
3205 if (isalpha(*ptr) || *ptr == '_') {
3206 if (strncmp(map->eval_string, ptr, len) == 0 &&
3207 !isalnum(ptr[len]) && ptr[len] != '_') {
3208 ptr = eval_replace(ptr, map, len);
3209 /* enum/sizeof string smaller than value */
3210 if (WARN_ON_ONCE(!ptr))
3211 return;
3212 /*
3213 * No need to decrement here, as eval_replace()
3214 * returns the pointer to the character passed
3215 * the eval, and two evals can not be placed
3216 * back to back without something in between.
3217 * We can skip that something in between.
3218 */
3219 continue;
3220 }
3221 skip_more:
3222 do {
3223 ptr++;
3224 } while (isalnum(*ptr) || *ptr == '_');
3225 if (!*ptr)
3226 break;
3227 /*
3228 * If what comes after this variable is a '.' or
3229 * '->' then we can continue to ignore that string.
3230 */
3231 if (*ptr == '.' || (ptr[0] == '-' && ptr[1] == '>')) {
3232 ptr += *ptr == '.' ? 1 : 2;
3233 if (!*ptr)
3234 break;
3235 goto skip_more;
3236 }
3237 /*
3238 * Once again, we can skip the delimiter that came
3239 * after the string.
3240 */
3241 continue;
3242 }
3243 }
3244 }
3245
add_str_to_module(struct module * module,char * str)3246 static void add_str_to_module(struct module *module, char *str)
3247 {
3248 struct module_string *modstr;
3249
3250 modstr = kmalloc(sizeof(*modstr), GFP_KERNEL);
3251
3252 /*
3253 * If we failed to allocate memory here, then we'll just
3254 * let the str memory leak when the module is removed.
3255 * If this fails to allocate, there's worse problems than
3256 * a leaked string on module removal.
3257 */
3258 if (WARN_ON_ONCE(!modstr))
3259 return;
3260
3261 modstr->module = module;
3262 modstr->str = str;
3263
3264 list_add(&modstr->next, &module_strings);
3265 }
3266
update_event_fields(struct trace_event_call * call,struct trace_eval_map * map)3267 static void update_event_fields(struct trace_event_call *call,
3268 struct trace_eval_map *map)
3269 {
3270 struct ftrace_event_field *field;
3271 struct list_head *head;
3272 char *ptr;
3273 char *str;
3274 int len = strlen(map->eval_string);
3275
3276 /* Dynamic events should never have field maps */
3277 if (WARN_ON_ONCE(call->flags & TRACE_EVENT_FL_DYNAMIC))
3278 return;
3279
3280 head = trace_get_fields(call);
3281 list_for_each_entry(field, head, link) {
3282 ptr = strchr(field->type, '[');
3283 if (!ptr)
3284 continue;
3285 ptr++;
3286
3287 if (!isalpha(*ptr) && *ptr != '_')
3288 continue;
3289
3290 if (strncmp(map->eval_string, ptr, len) != 0)
3291 continue;
3292
3293 str = kstrdup(field->type, GFP_KERNEL);
3294 if (WARN_ON_ONCE(!str))
3295 return;
3296 ptr = str + (ptr - field->type);
3297 ptr = eval_replace(ptr, map, len);
3298 /* enum/sizeof string smaller than value */
3299 if (WARN_ON_ONCE(!ptr)) {
3300 kfree(str);
3301 continue;
3302 }
3303
3304 /*
3305 * If the event is part of a module, then we need to free the string
3306 * when the module is removed. Otherwise, it will stay allocated
3307 * until a reboot.
3308 */
3309 if (call->module)
3310 add_str_to_module(call->module, str);
3311
3312 field->type = str;
3313 }
3314 }
3315
trace_event_eval_update(struct trace_eval_map ** map,int len)3316 void trace_event_eval_update(struct trace_eval_map **map, int len)
3317 {
3318 struct trace_event_call *call, *p;
3319 const char *last_system = NULL;
3320 bool first = false;
3321 int last_i;
3322 int i;
3323
3324 down_write(&trace_event_sem);
3325 list_for_each_entry_safe(call, p, &ftrace_events, list) {
3326 /* events are usually grouped together with systems */
3327 if (!last_system || call->class->system != last_system) {
3328 first = true;
3329 last_i = 0;
3330 last_system = call->class->system;
3331 }
3332
3333 /*
3334 * Since calls are grouped by systems, the likelihood that the
3335 * next call in the iteration belongs to the same system as the
3336 * previous call is high. As an optimization, we skip searching
3337 * for a map[] that matches the call's system if the last call
3338 * was from the same system. That's what last_i is for. If the
3339 * call has the same system as the previous call, then last_i
3340 * will be the index of the first map[] that has a matching
3341 * system.
3342 */
3343 for (i = last_i; i < len; i++) {
3344 if (call->class->system == map[i]->system) {
3345 /* Save the first system if need be */
3346 if (first) {
3347 last_i = i;
3348 first = false;
3349 }
3350 update_event_printk(call, map[i]);
3351 update_event_fields(call, map[i]);
3352 }
3353 }
3354 cond_resched();
3355 }
3356 up_write(&trace_event_sem);
3357 }
3358
event_in_systems(struct trace_event_call * call,const char * systems)3359 static bool event_in_systems(struct trace_event_call *call,
3360 const char *systems)
3361 {
3362 const char *system;
3363 const char *p;
3364
3365 if (!systems)
3366 return true;
3367
3368 system = call->class->system;
3369 p = strstr(systems, system);
3370 if (!p)
3371 return false;
3372
3373 if (p != systems && !isspace(*(p - 1)) && *(p - 1) != ',')
3374 return false;
3375
3376 p += strlen(system);
3377 return !*p || isspace(*p) || *p == ',';
3378 }
3379
3380 #ifdef CONFIG_HIST_TRIGGERS
3381 /*
3382 * Wake up waiter on the hist_poll_wq from irq_work because the hist trigger
3383 * may happen in any context.
3384 */
hist_poll_event_irq_work(struct irq_work * work)3385 static void hist_poll_event_irq_work(struct irq_work *work)
3386 {
3387 wake_up_all(&hist_poll_wq);
3388 }
3389
3390 DEFINE_IRQ_WORK(hist_poll_work, hist_poll_event_irq_work);
3391 DECLARE_WAIT_QUEUE_HEAD(hist_poll_wq);
3392 #endif
3393
3394 static struct trace_event_file *
trace_create_new_event(struct trace_event_call * call,struct trace_array * tr)3395 trace_create_new_event(struct trace_event_call *call,
3396 struct trace_array *tr)
3397 {
3398 struct trace_pid_list *no_pid_list;
3399 struct trace_pid_list *pid_list;
3400 struct trace_event_file *file;
3401 unsigned int first;
3402
3403 if (!event_in_systems(call, tr->system_names))
3404 return NULL;
3405
3406 file = kmem_cache_alloc(file_cachep, GFP_TRACE);
3407 if (!file)
3408 return ERR_PTR(-ENOMEM);
3409
3410 pid_list = rcu_dereference_protected(tr->filtered_pids,
3411 lockdep_is_held(&event_mutex));
3412 no_pid_list = rcu_dereference_protected(tr->filtered_no_pids,
3413 lockdep_is_held(&event_mutex));
3414
3415 if (!trace_pid_list_first(pid_list, &first) ||
3416 !trace_pid_list_first(no_pid_list, &first))
3417 file->flags |= EVENT_FILE_FL_PID_FILTER;
3418
3419 file->event_call = call;
3420 file->tr = tr;
3421 atomic_set(&file->sm_ref, 0);
3422 atomic_set(&file->tm_ref, 0);
3423 INIT_LIST_HEAD(&file->triggers);
3424 list_add(&file->list, &tr->events);
3425 refcount_set(&file->ref, 1);
3426
3427 return file;
3428 }
3429
3430 #define MAX_BOOT_TRIGGERS 32
3431
3432 static struct boot_triggers {
3433 const char *event;
3434 char *trigger;
3435 } bootup_triggers[MAX_BOOT_TRIGGERS];
3436
3437 static char bootup_trigger_buf[COMMAND_LINE_SIZE];
3438 static int nr_boot_triggers;
3439
setup_trace_triggers(char * str)3440 static __init int setup_trace_triggers(char *str)
3441 {
3442 char *trigger;
3443 char *buf;
3444 int i;
3445
3446 strscpy(bootup_trigger_buf, str, COMMAND_LINE_SIZE);
3447 trace_set_ring_buffer_expanded(NULL);
3448 disable_tracing_selftest("running event triggers");
3449
3450 buf = bootup_trigger_buf;
3451 for (i = 0; i < MAX_BOOT_TRIGGERS; i++) {
3452 trigger = strsep(&buf, ",");
3453 if (!trigger)
3454 break;
3455 bootup_triggers[i].event = strsep(&trigger, ".");
3456 bootup_triggers[i].trigger = trigger;
3457 if (!bootup_triggers[i].trigger)
3458 break;
3459 }
3460
3461 nr_boot_triggers = i;
3462 return 1;
3463 }
3464 __setup("trace_trigger=", setup_trace_triggers);
3465
3466 /* Add an event to a trace directory */
3467 static int
__trace_add_new_event(struct trace_event_call * call,struct trace_array * tr)3468 __trace_add_new_event(struct trace_event_call *call, struct trace_array *tr)
3469 {
3470 struct trace_event_file *file;
3471
3472 file = trace_create_new_event(call, tr);
3473 /*
3474 * trace_create_new_event() returns ERR_PTR(-ENOMEM) if failed
3475 * allocation, or NULL if the event is not part of the tr->system_names.
3476 * When the event is not part of the tr->system_names, return zero, not
3477 * an error.
3478 */
3479 if (!file)
3480 return 0;
3481
3482 if (IS_ERR(file))
3483 return PTR_ERR(file);
3484
3485 if (eventdir_initialized)
3486 return event_create_dir(tr->event_dir, file);
3487 else
3488 return event_define_fields(call);
3489 }
3490
trace_early_triggers(struct trace_event_file * file,const char * name)3491 static void trace_early_triggers(struct trace_event_file *file, const char *name)
3492 {
3493 int ret;
3494 int i;
3495
3496 for (i = 0; i < nr_boot_triggers; i++) {
3497 if (strcmp(name, bootup_triggers[i].event))
3498 continue;
3499 mutex_lock(&event_mutex);
3500 ret = trigger_process_regex(file, bootup_triggers[i].trigger);
3501 mutex_unlock(&event_mutex);
3502 if (ret)
3503 pr_err("Failed to register trigger '%s' on event %s\n",
3504 bootup_triggers[i].trigger,
3505 bootup_triggers[i].event);
3506 }
3507 }
3508
3509 /*
3510 * Just create a descriptor for early init. A descriptor is required
3511 * for enabling events at boot. We want to enable events before
3512 * the filesystem is initialized.
3513 */
3514 static int
__trace_early_add_new_event(struct trace_event_call * call,struct trace_array * tr)3515 __trace_early_add_new_event(struct trace_event_call *call,
3516 struct trace_array *tr)
3517 {
3518 struct trace_event_file *file;
3519 int ret;
3520
3521 file = trace_create_new_event(call, tr);
3522 /*
3523 * trace_create_new_event() returns ERR_PTR(-ENOMEM) if failed
3524 * allocation, or NULL if the event is not part of the tr->system_names.
3525 * When the event is not part of the tr->system_names, return zero, not
3526 * an error.
3527 */
3528 if (!file)
3529 return 0;
3530
3531 if (IS_ERR(file))
3532 return PTR_ERR(file);
3533
3534 ret = event_define_fields(call);
3535 if (ret)
3536 return ret;
3537
3538 trace_early_triggers(file, trace_event_name(call));
3539
3540 return 0;
3541 }
3542
3543 struct ftrace_module_file_ops;
3544 static void __add_event_to_tracers(struct trace_event_call *call);
3545
3546 /* Add an additional event_call dynamically */
trace_add_event_call(struct trace_event_call * call)3547 int trace_add_event_call(struct trace_event_call *call)
3548 {
3549 int ret;
3550 lockdep_assert_held(&event_mutex);
3551
3552 guard(mutex)(&trace_types_lock);
3553
3554 ret = __register_event(call, NULL);
3555 if (ret < 0)
3556 return ret;
3557
3558 __add_event_to_tracers(call);
3559 return ret;
3560 }
3561 EXPORT_SYMBOL_GPL(trace_add_event_call);
3562
3563 /*
3564 * Must be called under locking of trace_types_lock, event_mutex and
3565 * trace_event_sem.
3566 */
__trace_remove_event_call(struct trace_event_call * call)3567 static void __trace_remove_event_call(struct trace_event_call *call)
3568 {
3569 event_remove(call);
3570 trace_destroy_fields(call);
3571 }
3572
probe_remove_event_call(struct trace_event_call * call)3573 static int probe_remove_event_call(struct trace_event_call *call)
3574 {
3575 struct trace_array *tr;
3576 struct trace_event_file *file;
3577
3578 #ifdef CONFIG_PERF_EVENTS
3579 if (call->perf_refcount)
3580 return -EBUSY;
3581 #endif
3582 do_for_each_event_file(tr, file) {
3583 if (file->event_call != call)
3584 continue;
3585 /*
3586 * We can't rely on ftrace_event_enable_disable(enable => 0)
3587 * we are going to do, EVENT_FILE_FL_SOFT_MODE can suppress
3588 * TRACE_REG_UNREGISTER.
3589 */
3590 if (file->flags & EVENT_FILE_FL_ENABLED)
3591 goto busy;
3592
3593 if (file->flags & EVENT_FILE_FL_WAS_ENABLED)
3594 tr->clear_trace = true;
3595 /*
3596 * The do_for_each_event_file_safe() is
3597 * a double loop. After finding the call for this
3598 * trace_array, we use break to jump to the next
3599 * trace_array.
3600 */
3601 break;
3602 } while_for_each_event_file();
3603
3604 __trace_remove_event_call(call);
3605
3606 return 0;
3607 busy:
3608 /* No need to clear the trace now */
3609 list_for_each_entry(tr, &ftrace_trace_arrays, list) {
3610 tr->clear_trace = false;
3611 }
3612 return -EBUSY;
3613 }
3614
3615 /* Remove an event_call */
trace_remove_event_call(struct trace_event_call * call)3616 int trace_remove_event_call(struct trace_event_call *call)
3617 {
3618 int ret;
3619
3620 lockdep_assert_held(&event_mutex);
3621
3622 mutex_lock(&trace_types_lock);
3623 down_write(&trace_event_sem);
3624 ret = probe_remove_event_call(call);
3625 up_write(&trace_event_sem);
3626 mutex_unlock(&trace_types_lock);
3627
3628 return ret;
3629 }
3630 EXPORT_SYMBOL_GPL(trace_remove_event_call);
3631
3632 #define for_each_event(event, start, end) \
3633 for (event = start; \
3634 (unsigned long)event < (unsigned long)end; \
3635 event++)
3636
3637 #ifdef CONFIG_MODULES
update_mod_cache(struct trace_array * tr,struct module * mod)3638 static void update_mod_cache(struct trace_array *tr, struct module *mod)
3639 {
3640 struct event_mod_load *event_mod, *n;
3641
3642 list_for_each_entry_safe(event_mod, n, &tr->mod_events, list) {
3643 if (strcmp(event_mod->module, mod->name) != 0)
3644 continue;
3645
3646 __ftrace_set_clr_event_nolock(tr, event_mod->match,
3647 event_mod->system,
3648 event_mod->event, 1, mod->name);
3649 free_event_mod(event_mod);
3650 }
3651 }
3652
update_cache_events(struct module * mod)3653 static void update_cache_events(struct module *mod)
3654 {
3655 struct trace_array *tr;
3656
3657 list_for_each_entry(tr, &ftrace_trace_arrays, list)
3658 update_mod_cache(tr, mod);
3659 }
3660
trace_module_add_events(struct module * mod)3661 static void trace_module_add_events(struct module *mod)
3662 {
3663 struct trace_event_call **call, **start, **end;
3664
3665 if (!mod->num_trace_events)
3666 return;
3667
3668 /* Don't add infrastructure for mods without tracepoints */
3669 if (trace_module_has_bad_taint(mod)) {
3670 pr_err("%s: module has bad taint, not creating trace events\n",
3671 mod->name);
3672 return;
3673 }
3674
3675 start = mod->trace_events;
3676 end = mod->trace_events + mod->num_trace_events;
3677
3678 for_each_event(call, start, end) {
3679 __register_event(*call, mod);
3680 __add_event_to_tracers(*call);
3681 }
3682
3683 update_cache_events(mod);
3684 }
3685
trace_module_remove_events(struct module * mod)3686 static void trace_module_remove_events(struct module *mod)
3687 {
3688 struct trace_event_call *call, *p;
3689 struct module_string *modstr, *m;
3690
3691 down_write(&trace_event_sem);
3692 list_for_each_entry_safe(call, p, &ftrace_events, list) {
3693 if ((call->flags & TRACE_EVENT_FL_DYNAMIC) || !call->module)
3694 continue;
3695 if (call->module == mod)
3696 __trace_remove_event_call(call);
3697 }
3698 /* Check for any strings allocade for this module */
3699 list_for_each_entry_safe(modstr, m, &module_strings, next) {
3700 if (modstr->module != mod)
3701 continue;
3702 list_del(&modstr->next);
3703 kfree(modstr->str);
3704 kfree(modstr);
3705 }
3706 up_write(&trace_event_sem);
3707
3708 /*
3709 * It is safest to reset the ring buffer if the module being unloaded
3710 * registered any events that were used. The only worry is if
3711 * a new module gets loaded, and takes on the same id as the events
3712 * of this module. When printing out the buffer, traced events left
3713 * over from this module may be passed to the new module events and
3714 * unexpected results may occur.
3715 */
3716 tracing_reset_all_online_cpus_unlocked();
3717 }
3718
trace_module_notify(struct notifier_block * self,unsigned long val,void * data)3719 static int trace_module_notify(struct notifier_block *self,
3720 unsigned long val, void *data)
3721 {
3722 struct module *mod = data;
3723
3724 mutex_lock(&event_mutex);
3725 mutex_lock(&trace_types_lock);
3726 switch (val) {
3727 case MODULE_STATE_COMING:
3728 trace_module_add_events(mod);
3729 break;
3730 case MODULE_STATE_GOING:
3731 trace_module_remove_events(mod);
3732 break;
3733 }
3734 mutex_unlock(&trace_types_lock);
3735 mutex_unlock(&event_mutex);
3736
3737 return NOTIFY_OK;
3738 }
3739
3740 static struct notifier_block trace_module_nb = {
3741 .notifier_call = trace_module_notify,
3742 .priority = 1, /* higher than trace.c module notify */
3743 };
3744 #endif /* CONFIG_MODULES */
3745
3746 /* Create a new event directory structure for a trace directory. */
3747 static void
__trace_add_event_dirs(struct trace_array * tr)3748 __trace_add_event_dirs(struct trace_array *tr)
3749 {
3750 struct trace_event_call *call;
3751 int ret;
3752
3753 list_for_each_entry(call, &ftrace_events, list) {
3754 ret = __trace_add_new_event(call, tr);
3755 if (ret < 0)
3756 pr_warn("Could not create directory for event %s\n",
3757 trace_event_name(call));
3758 }
3759 }
3760
3761 /* Returns any file that matches the system and event */
3762 struct trace_event_file *
__find_event_file(struct trace_array * tr,const char * system,const char * event)3763 __find_event_file(struct trace_array *tr, const char *system, const char *event)
3764 {
3765 struct trace_event_file *file;
3766 struct trace_event_call *call;
3767 const char *name;
3768
3769 list_for_each_entry(file, &tr->events, list) {
3770
3771 call = file->event_call;
3772 name = trace_event_name(call);
3773
3774 if (!name || !call->class)
3775 continue;
3776
3777 if (strcmp(event, name) == 0 &&
3778 strcmp(system, call->class->system) == 0)
3779 return file;
3780 }
3781 return NULL;
3782 }
3783
3784 /* Returns valid trace event files that match system and event */
3785 struct trace_event_file *
find_event_file(struct trace_array * tr,const char * system,const char * event)3786 find_event_file(struct trace_array *tr, const char *system, const char *event)
3787 {
3788 struct trace_event_file *file;
3789
3790 file = __find_event_file(tr, system, event);
3791 if (!file || !file->event_call->class->reg ||
3792 file->event_call->flags & TRACE_EVENT_FL_IGNORE_ENABLE)
3793 return NULL;
3794
3795 return file;
3796 }
3797
3798 /**
3799 * trace_get_event_file - Find and return a trace event file
3800 * @instance: The name of the trace instance containing the event
3801 * @system: The name of the system containing the event
3802 * @event: The name of the event
3803 *
3804 * Return a trace event file given the trace instance name, trace
3805 * system, and trace event name. If the instance name is NULL, it
3806 * refers to the top-level trace array.
3807 *
3808 * This function will look it up and return it if found, after calling
3809 * trace_array_get() to prevent the instance from going away, and
3810 * increment the event's module refcount to prevent it from being
3811 * removed.
3812 *
3813 * To release the file, call trace_put_event_file(), which will call
3814 * trace_array_put() and decrement the event's module refcount.
3815 *
3816 * Return: The trace event on success, ERR_PTR otherwise.
3817 */
trace_get_event_file(const char * instance,const char * system,const char * event)3818 struct trace_event_file *trace_get_event_file(const char *instance,
3819 const char *system,
3820 const char *event)
3821 {
3822 struct trace_array *tr = top_trace_array();
3823 struct trace_event_file *file = NULL;
3824 int ret = -EINVAL;
3825
3826 if (instance) {
3827 tr = trace_array_find_get(instance);
3828 if (!tr)
3829 return ERR_PTR(-ENOENT);
3830 } else {
3831 ret = trace_array_get(tr);
3832 if (ret)
3833 return ERR_PTR(ret);
3834 }
3835
3836 guard(mutex)(&event_mutex);
3837
3838 file = find_event_file(tr, system, event);
3839 if (!file) {
3840 trace_array_put(tr);
3841 return ERR_PTR(-EINVAL);
3842 }
3843
3844 /* Don't let event modules unload while in use */
3845 ret = trace_event_try_get_ref(file->event_call);
3846 if (!ret) {
3847 trace_array_put(tr);
3848 return ERR_PTR(-EBUSY);
3849 }
3850
3851 return file;
3852 }
3853 EXPORT_SYMBOL_GPL(trace_get_event_file);
3854
3855 /**
3856 * trace_put_event_file - Release a file from trace_get_event_file()
3857 * @file: The trace event file
3858 *
3859 * If a file was retrieved using trace_get_event_file(), this should
3860 * be called when it's no longer needed. It will cancel the previous
3861 * trace_array_get() called by that function, and decrement the
3862 * event's module refcount.
3863 */
trace_put_event_file(struct trace_event_file * file)3864 void trace_put_event_file(struct trace_event_file *file)
3865 {
3866 mutex_lock(&event_mutex);
3867 trace_event_put_ref(file->event_call);
3868 mutex_unlock(&event_mutex);
3869
3870 trace_array_put(file->tr);
3871 }
3872 EXPORT_SYMBOL_GPL(trace_put_event_file);
3873
3874 #ifdef CONFIG_DYNAMIC_FTRACE
3875
3876 /* Avoid typos */
3877 #define ENABLE_EVENT_STR "enable_event"
3878 #define DISABLE_EVENT_STR "disable_event"
3879
3880 struct event_probe_data {
3881 struct trace_event_file *file;
3882 unsigned long count;
3883 int ref;
3884 bool enable;
3885 };
3886
update_event_probe(struct event_probe_data * data)3887 static void update_event_probe(struct event_probe_data *data)
3888 {
3889 if (data->enable)
3890 clear_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &data->file->flags);
3891 else
3892 set_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &data->file->flags);
3893 }
3894
3895 static void
event_enable_probe(unsigned long ip,unsigned long parent_ip,struct trace_array * tr,struct ftrace_probe_ops * ops,void * data)3896 event_enable_probe(unsigned long ip, unsigned long parent_ip,
3897 struct trace_array *tr, struct ftrace_probe_ops *ops,
3898 void *data)
3899 {
3900 struct ftrace_func_mapper *mapper = data;
3901 struct event_probe_data *edata;
3902 void **pdata;
3903
3904 pdata = ftrace_func_mapper_find_ip(mapper, ip);
3905 if (!pdata || !*pdata)
3906 return;
3907
3908 edata = *pdata;
3909 update_event_probe(edata);
3910 }
3911
3912 static void
event_enable_count_probe(unsigned long ip,unsigned long parent_ip,struct trace_array * tr,struct ftrace_probe_ops * ops,void * data)3913 event_enable_count_probe(unsigned long ip, unsigned long parent_ip,
3914 struct trace_array *tr, struct ftrace_probe_ops *ops,
3915 void *data)
3916 {
3917 struct ftrace_func_mapper *mapper = data;
3918 struct event_probe_data *edata;
3919 void **pdata;
3920
3921 pdata = ftrace_func_mapper_find_ip(mapper, ip);
3922 if (!pdata || !*pdata)
3923 return;
3924
3925 edata = *pdata;
3926
3927 if (!edata->count)
3928 return;
3929
3930 /* Skip if the event is in a state we want to switch to */
3931 if (edata->enable == !(edata->file->flags & EVENT_FILE_FL_SOFT_DISABLED))
3932 return;
3933
3934 if (edata->count != -1)
3935 (edata->count)--;
3936
3937 update_event_probe(edata);
3938 }
3939
3940 static int
event_enable_print(struct seq_file * m,unsigned long ip,struct ftrace_probe_ops * ops,void * data)3941 event_enable_print(struct seq_file *m, unsigned long ip,
3942 struct ftrace_probe_ops *ops, void *data)
3943 {
3944 struct ftrace_func_mapper *mapper = data;
3945 struct event_probe_data *edata;
3946 void **pdata;
3947
3948 pdata = ftrace_func_mapper_find_ip(mapper, ip);
3949
3950 if (WARN_ON_ONCE(!pdata || !*pdata))
3951 return 0;
3952
3953 edata = *pdata;
3954
3955 seq_printf(m, "%ps:", (void *)ip);
3956
3957 seq_printf(m, "%s:%s:%s",
3958 edata->enable ? ENABLE_EVENT_STR : DISABLE_EVENT_STR,
3959 edata->file->event_call->class->system,
3960 trace_event_name(edata->file->event_call));
3961
3962 if (edata->count == -1)
3963 seq_puts(m, ":unlimited\n");
3964 else
3965 seq_printf(m, ":count=%ld\n", edata->count);
3966
3967 return 0;
3968 }
3969
3970 static int
event_enable_init(struct ftrace_probe_ops * ops,struct trace_array * tr,unsigned long ip,void * init_data,void ** data)3971 event_enable_init(struct ftrace_probe_ops *ops, struct trace_array *tr,
3972 unsigned long ip, void *init_data, void **data)
3973 {
3974 struct ftrace_func_mapper *mapper = *data;
3975 struct event_probe_data *edata = init_data;
3976 int ret;
3977
3978 if (!mapper) {
3979 mapper = allocate_ftrace_func_mapper();
3980 if (!mapper)
3981 return -ENODEV;
3982 *data = mapper;
3983 }
3984
3985 ret = ftrace_func_mapper_add_ip(mapper, ip, edata);
3986 if (ret < 0)
3987 return ret;
3988
3989 edata->ref++;
3990
3991 return 0;
3992 }
3993
free_probe_data(void * data)3994 static int free_probe_data(void *data)
3995 {
3996 struct event_probe_data *edata = data;
3997
3998 edata->ref--;
3999 if (!edata->ref) {
4000 /* Remove the SOFT_MODE flag */
4001 __ftrace_event_enable_disable(edata->file, 0, 1);
4002 trace_event_put_ref(edata->file->event_call);
4003 kfree(edata);
4004 }
4005 return 0;
4006 }
4007
4008 static void
event_enable_free(struct ftrace_probe_ops * ops,struct trace_array * tr,unsigned long ip,void * data)4009 event_enable_free(struct ftrace_probe_ops *ops, struct trace_array *tr,
4010 unsigned long ip, void *data)
4011 {
4012 struct ftrace_func_mapper *mapper = data;
4013 struct event_probe_data *edata;
4014
4015 if (!ip) {
4016 if (!mapper)
4017 return;
4018 free_ftrace_func_mapper(mapper, free_probe_data);
4019 return;
4020 }
4021
4022 edata = ftrace_func_mapper_remove_ip(mapper, ip);
4023
4024 if (WARN_ON_ONCE(!edata))
4025 return;
4026
4027 if (WARN_ON_ONCE(edata->ref <= 0))
4028 return;
4029
4030 free_probe_data(edata);
4031 }
4032
4033 static struct ftrace_probe_ops event_enable_probe_ops = {
4034 .func = event_enable_probe,
4035 .print = event_enable_print,
4036 .init = event_enable_init,
4037 .free = event_enable_free,
4038 };
4039
4040 static struct ftrace_probe_ops event_enable_count_probe_ops = {
4041 .func = event_enable_count_probe,
4042 .print = event_enable_print,
4043 .init = event_enable_init,
4044 .free = event_enable_free,
4045 };
4046
4047 static struct ftrace_probe_ops event_disable_probe_ops = {
4048 .func = event_enable_probe,
4049 .print = event_enable_print,
4050 .init = event_enable_init,
4051 .free = event_enable_free,
4052 };
4053
4054 static struct ftrace_probe_ops event_disable_count_probe_ops = {
4055 .func = event_enable_count_probe,
4056 .print = event_enable_print,
4057 .init = event_enable_init,
4058 .free = event_enable_free,
4059 };
4060
4061 static int
event_enable_func(struct trace_array * tr,struct ftrace_hash * hash,char * glob,char * cmd,char * param,int enabled)4062 event_enable_func(struct trace_array *tr, struct ftrace_hash *hash,
4063 char *glob, char *cmd, char *param, int enabled)
4064 {
4065 struct trace_event_file *file;
4066 struct ftrace_probe_ops *ops;
4067 struct event_probe_data *data;
4068 unsigned long count = -1;
4069 const char *system;
4070 const char *event;
4071 char *number;
4072 bool enable;
4073 int ret;
4074
4075 if (!tr)
4076 return -ENODEV;
4077
4078 /* hash funcs only work with set_ftrace_filter */
4079 if (!enabled || !param)
4080 return -EINVAL;
4081
4082 system = strsep(¶m, ":");
4083 if (!param)
4084 return -EINVAL;
4085
4086 event = strsep(¶m, ":");
4087
4088 guard(mutex)(&event_mutex);
4089
4090 file = find_event_file(tr, system, event);
4091 if (!file)
4092 return -EINVAL;
4093
4094 enable = strcmp(cmd, ENABLE_EVENT_STR) == 0;
4095
4096 if (enable)
4097 ops = param ? &event_enable_count_probe_ops : &event_enable_probe_ops;
4098 else
4099 ops = param ? &event_disable_count_probe_ops : &event_disable_probe_ops;
4100
4101 if (glob[0] == '!')
4102 return unregister_ftrace_function_probe_func(glob+1, tr, ops);
4103
4104 if (param) {
4105 number = strsep(¶m, ":");
4106
4107 if (!strlen(number))
4108 return -EINVAL;
4109
4110 /*
4111 * We use the callback data field (which is a pointer)
4112 * as our counter.
4113 */
4114 ret = kstrtoul(number, 0, &count);
4115 if (ret)
4116 return ret;
4117 }
4118
4119 /* Don't let event modules unload while probe registered */
4120 ret = trace_event_try_get_ref(file->event_call);
4121 if (!ret)
4122 return -EBUSY;
4123
4124 ret = __ftrace_event_enable_disable(file, 1, 1);
4125 if (ret < 0)
4126 goto out_put;
4127
4128 ret = -ENOMEM;
4129 data = kzalloc(sizeof(*data), GFP_KERNEL);
4130 if (!data)
4131 goto out_put;
4132
4133 data->enable = enable;
4134 data->count = count;
4135 data->file = file;
4136
4137 ret = register_ftrace_function_probe(glob, tr, ops, data);
4138 /*
4139 * The above returns on success the # of functions enabled,
4140 * but if it didn't find any functions it returns zero.
4141 * Consider no functions a failure too.
4142 */
4143
4144 /* Just return zero, not the number of enabled functions */
4145 if (ret > 0)
4146 return 0;
4147
4148 kfree(data);
4149
4150 if (!ret)
4151 ret = -ENOENT;
4152
4153 __ftrace_event_enable_disable(file, 0, 1);
4154 out_put:
4155 trace_event_put_ref(file->event_call);
4156 return ret;
4157 }
4158
4159 static struct ftrace_func_command event_enable_cmd = {
4160 .name = ENABLE_EVENT_STR,
4161 .func = event_enable_func,
4162 };
4163
4164 static struct ftrace_func_command event_disable_cmd = {
4165 .name = DISABLE_EVENT_STR,
4166 .func = event_enable_func,
4167 };
4168
register_event_cmds(void)4169 static __init int register_event_cmds(void)
4170 {
4171 int ret;
4172
4173 ret = register_ftrace_command(&event_enable_cmd);
4174 if (WARN_ON(ret < 0))
4175 return ret;
4176 ret = register_ftrace_command(&event_disable_cmd);
4177 if (WARN_ON(ret < 0))
4178 unregister_ftrace_command(&event_enable_cmd);
4179 return ret;
4180 }
4181 #else
register_event_cmds(void)4182 static inline int register_event_cmds(void) { return 0; }
4183 #endif /* CONFIG_DYNAMIC_FTRACE */
4184
4185 /*
4186 * The top level array and trace arrays created by boot-time tracing
4187 * have already had its trace_event_file descriptors created in order
4188 * to allow for early events to be recorded.
4189 * This function is called after the tracefs has been initialized,
4190 * and we now have to create the files associated to the events.
4191 */
__trace_early_add_event_dirs(struct trace_array * tr)4192 static void __trace_early_add_event_dirs(struct trace_array *tr)
4193 {
4194 struct trace_event_file *file;
4195 int ret;
4196
4197
4198 list_for_each_entry(file, &tr->events, list) {
4199 ret = event_create_dir(tr->event_dir, file);
4200 if (ret < 0)
4201 pr_warn("Could not create directory for event %s\n",
4202 trace_event_name(file->event_call));
4203 }
4204 }
4205
4206 /*
4207 * For early boot up, the top trace array and the trace arrays created
4208 * by boot-time tracing require to have a list of events that can be
4209 * enabled. This must be done before the filesystem is set up in order
4210 * to allow events to be traced early.
4211 */
__trace_early_add_events(struct trace_array * tr)4212 void __trace_early_add_events(struct trace_array *tr)
4213 {
4214 struct trace_event_call *call;
4215 int ret;
4216
4217 list_for_each_entry(call, &ftrace_events, list) {
4218 /* Early boot up should not have any modules loaded */
4219 if (!(call->flags & TRACE_EVENT_FL_DYNAMIC) &&
4220 WARN_ON_ONCE(call->module))
4221 continue;
4222
4223 ret = __trace_early_add_new_event(call, tr);
4224 if (ret < 0)
4225 pr_warn("Could not create early event %s\n",
4226 trace_event_name(call));
4227 }
4228 }
4229
4230 /* Remove the event directory structure for a trace directory. */
4231 static void
__trace_remove_event_dirs(struct trace_array * tr)4232 __trace_remove_event_dirs(struct trace_array *tr)
4233 {
4234 struct trace_event_file *file, *next;
4235
4236 list_for_each_entry_safe(file, next, &tr->events, list)
4237 remove_event_file_dir(file);
4238 }
4239
__add_event_to_tracers(struct trace_event_call * call)4240 static void __add_event_to_tracers(struct trace_event_call *call)
4241 {
4242 struct trace_array *tr;
4243
4244 list_for_each_entry(tr, &ftrace_trace_arrays, list)
4245 __trace_add_new_event(call, tr);
4246 }
4247
4248 extern struct trace_event_call *__start_ftrace_events[];
4249 extern struct trace_event_call *__stop_ftrace_events[];
4250
4251 static char bootup_event_buf[COMMAND_LINE_SIZE] __initdata;
4252
setup_trace_event(char * str)4253 static __init int setup_trace_event(char *str)
4254 {
4255 strscpy(bootup_event_buf, str, COMMAND_LINE_SIZE);
4256 trace_set_ring_buffer_expanded(NULL);
4257 disable_tracing_selftest("running event tracing");
4258
4259 return 1;
4260 }
4261 __setup("trace_event=", setup_trace_event);
4262
events_callback(const char * name,umode_t * mode,void ** data,const struct file_operations ** fops)4263 static int events_callback(const char *name, umode_t *mode, void **data,
4264 const struct file_operations **fops)
4265 {
4266 if (strcmp(name, "enable") == 0) {
4267 *mode = TRACE_MODE_WRITE;
4268 *fops = &ftrace_tr_enable_fops;
4269 return 1;
4270 }
4271
4272 if (strcmp(name, "header_page") == 0) {
4273 *mode = TRACE_MODE_READ;
4274 *fops = &ftrace_show_header_page_fops;
4275
4276 } else if (strcmp(name, "header_event") == 0) {
4277 *mode = TRACE_MODE_READ;
4278 *fops = &ftrace_show_header_event_fops;
4279 } else
4280 return 0;
4281
4282 return 1;
4283 }
4284
4285 /* Expects to have event_mutex held when called */
4286 static int
create_event_toplevel_files(struct dentry * parent,struct trace_array * tr)4287 create_event_toplevel_files(struct dentry *parent, struct trace_array *tr)
4288 {
4289 struct eventfs_inode *e_events;
4290 struct dentry *entry;
4291 int nr_entries;
4292 static struct eventfs_entry events_entries[] = {
4293 {
4294 .name = "enable",
4295 .callback = events_callback,
4296 },
4297 {
4298 .name = "header_page",
4299 .callback = events_callback,
4300 },
4301 {
4302 .name = "header_event",
4303 .callback = events_callback,
4304 },
4305 };
4306
4307 entry = trace_create_file("set_event", TRACE_MODE_WRITE, parent,
4308 tr, &ftrace_set_event_fops);
4309 if (!entry)
4310 return -ENOMEM;
4311
4312 nr_entries = ARRAY_SIZE(events_entries);
4313
4314 e_events = eventfs_create_events_dir("events", parent, events_entries,
4315 nr_entries, tr);
4316 if (IS_ERR(e_events)) {
4317 pr_warn("Could not create tracefs 'events' directory\n");
4318 return -ENOMEM;
4319 }
4320
4321 /* There are not as crucial, just warn if they are not created */
4322
4323 trace_create_file("set_event_pid", TRACE_MODE_WRITE, parent,
4324 tr, &ftrace_set_event_pid_fops);
4325
4326 trace_create_file("set_event_notrace_pid",
4327 TRACE_MODE_WRITE, parent, tr,
4328 &ftrace_set_event_notrace_pid_fops);
4329
4330 tr->event_dir = e_events;
4331
4332 return 0;
4333 }
4334
4335 /**
4336 * event_trace_add_tracer - add a instance of a trace_array to events
4337 * @parent: The parent dentry to place the files/directories for events in
4338 * @tr: The trace array associated with these events
4339 *
4340 * When a new instance is created, it needs to set up its events
4341 * directory, as well as other files associated with events. It also
4342 * creates the event hierarchy in the @parent/events directory.
4343 *
4344 * Returns 0 on success.
4345 *
4346 * Must be called with event_mutex held.
4347 */
event_trace_add_tracer(struct dentry * parent,struct trace_array * tr)4348 int event_trace_add_tracer(struct dentry *parent, struct trace_array *tr)
4349 {
4350 int ret;
4351
4352 lockdep_assert_held(&event_mutex);
4353
4354 ret = create_event_toplevel_files(parent, tr);
4355 if (ret)
4356 goto out;
4357
4358 down_write(&trace_event_sem);
4359 /* If tr already has the event list, it is initialized in early boot. */
4360 if (unlikely(!list_empty(&tr->events)))
4361 __trace_early_add_event_dirs(tr);
4362 else
4363 __trace_add_event_dirs(tr);
4364 up_write(&trace_event_sem);
4365
4366 out:
4367 return ret;
4368 }
4369
4370 /*
4371 * The top trace array already had its file descriptors created.
4372 * Now the files themselves need to be created.
4373 */
4374 static __init int
early_event_add_tracer(struct dentry * parent,struct trace_array * tr)4375 early_event_add_tracer(struct dentry *parent, struct trace_array *tr)
4376 {
4377 int ret;
4378
4379 guard(mutex)(&event_mutex);
4380
4381 ret = create_event_toplevel_files(parent, tr);
4382 if (ret)
4383 return ret;
4384
4385 down_write(&trace_event_sem);
4386 __trace_early_add_event_dirs(tr);
4387 up_write(&trace_event_sem);
4388
4389 return 0;
4390 }
4391
4392 /* Must be called with event_mutex held */
event_trace_del_tracer(struct trace_array * tr)4393 int event_trace_del_tracer(struct trace_array *tr)
4394 {
4395 lockdep_assert_held(&event_mutex);
4396
4397 /* Disable any event triggers and associated soft-disabled events */
4398 clear_event_triggers(tr);
4399
4400 /* Clear the pid list */
4401 __ftrace_clear_event_pids(tr, TRACE_PIDS | TRACE_NO_PIDS);
4402
4403 /* Disable any running events */
4404 __ftrace_set_clr_event_nolock(tr, NULL, NULL, NULL, 0, NULL);
4405
4406 /* Make sure no more events are being executed */
4407 tracepoint_synchronize_unregister();
4408
4409 down_write(&trace_event_sem);
4410 __trace_remove_event_dirs(tr);
4411 eventfs_remove_events_dir(tr->event_dir);
4412 up_write(&trace_event_sem);
4413
4414 tr->event_dir = NULL;
4415
4416 return 0;
4417 }
4418
event_trace_memsetup(void)4419 static __init int event_trace_memsetup(void)
4420 {
4421 field_cachep = KMEM_CACHE(ftrace_event_field, SLAB_PANIC);
4422 file_cachep = KMEM_CACHE(trace_event_file, SLAB_PANIC);
4423 return 0;
4424 }
4425
4426 __init void
early_enable_events(struct trace_array * tr,char * buf,bool disable_first)4427 early_enable_events(struct trace_array *tr, char *buf, bool disable_first)
4428 {
4429 char *token;
4430 int ret;
4431
4432 while (true) {
4433 token = strsep(&buf, ",");
4434
4435 if (!token)
4436 break;
4437
4438 if (*token) {
4439 /* Restarting syscalls requires that we stop them first */
4440 if (disable_first)
4441 ftrace_set_clr_event(tr, token, 0);
4442
4443 ret = ftrace_set_clr_event(tr, token, 1);
4444 if (ret)
4445 pr_warn("Failed to enable trace event: %s\n", token);
4446 }
4447
4448 /* Put back the comma to allow this to be called again */
4449 if (buf)
4450 *(buf - 1) = ',';
4451 }
4452 }
4453
event_trace_enable(void)4454 static __init int event_trace_enable(void)
4455 {
4456 struct trace_array *tr = top_trace_array();
4457 struct trace_event_call **iter, *call;
4458 int ret;
4459
4460 if (!tr)
4461 return -ENODEV;
4462
4463 for_each_event(iter, __start_ftrace_events, __stop_ftrace_events) {
4464
4465 call = *iter;
4466 ret = event_init(call);
4467 if (!ret)
4468 list_add(&call->list, &ftrace_events);
4469 }
4470
4471 register_trigger_cmds();
4472
4473 /*
4474 * We need the top trace array to have a working set of trace
4475 * points at early init, before the debug files and directories
4476 * are created. Create the file entries now, and attach them
4477 * to the actual file dentries later.
4478 */
4479 __trace_early_add_events(tr);
4480
4481 early_enable_events(tr, bootup_event_buf, false);
4482
4483 trace_printk_start_comm();
4484
4485 register_event_cmds();
4486
4487
4488 return 0;
4489 }
4490
4491 /*
4492 * event_trace_enable() is called from trace_event_init() first to
4493 * initialize events and perhaps start any events that are on the
4494 * command line. Unfortunately, there are some events that will not
4495 * start this early, like the system call tracepoints that need
4496 * to set the %SYSCALL_WORK_SYSCALL_TRACEPOINT flag of pid 1. But
4497 * event_trace_enable() is called before pid 1 starts, and this flag
4498 * is never set, making the syscall tracepoint never get reached, but
4499 * the event is enabled regardless (and not doing anything).
4500 */
event_trace_enable_again(void)4501 static __init int event_trace_enable_again(void)
4502 {
4503 struct trace_array *tr;
4504
4505 tr = top_trace_array();
4506 if (!tr)
4507 return -ENODEV;
4508
4509 early_enable_events(tr, bootup_event_buf, true);
4510
4511 return 0;
4512 }
4513
4514 early_initcall(event_trace_enable_again);
4515
4516 /* Init fields which doesn't related to the tracefs */
event_trace_init_fields(void)4517 static __init int event_trace_init_fields(void)
4518 {
4519 if (trace_define_generic_fields())
4520 pr_warn("tracing: Failed to allocated generic fields");
4521
4522 if (trace_define_common_fields())
4523 pr_warn("tracing: Failed to allocate common fields");
4524
4525 return 0;
4526 }
4527
event_trace_init(void)4528 __init int event_trace_init(void)
4529 {
4530 struct trace_array *tr;
4531 int ret;
4532
4533 tr = top_trace_array();
4534 if (!tr)
4535 return -ENODEV;
4536
4537 trace_create_file("available_events", TRACE_MODE_READ,
4538 NULL, tr, &ftrace_avail_fops);
4539
4540 ret = early_event_add_tracer(NULL, tr);
4541 if (ret)
4542 return ret;
4543
4544 #ifdef CONFIG_MODULES
4545 ret = register_module_notifier(&trace_module_nb);
4546 if (ret)
4547 pr_warn("Failed to register trace events module notifier\n");
4548 #endif
4549
4550 eventdir_initialized = true;
4551
4552 return 0;
4553 }
4554
trace_event_init(void)4555 void __init trace_event_init(void)
4556 {
4557 event_trace_memsetup();
4558 init_ftrace_syscalls();
4559 event_trace_enable();
4560 event_trace_init_fields();
4561 }
4562
4563 #ifdef CONFIG_EVENT_TRACE_STARTUP_TEST
4564
4565 static DEFINE_SPINLOCK(test_spinlock);
4566 static DEFINE_SPINLOCK(test_spinlock_irq);
4567 static DEFINE_MUTEX(test_mutex);
4568
test_work(struct work_struct * dummy)4569 static __init void test_work(struct work_struct *dummy)
4570 {
4571 spin_lock(&test_spinlock);
4572 spin_lock_irq(&test_spinlock_irq);
4573 udelay(1);
4574 spin_unlock_irq(&test_spinlock_irq);
4575 spin_unlock(&test_spinlock);
4576
4577 mutex_lock(&test_mutex);
4578 msleep(1);
4579 mutex_unlock(&test_mutex);
4580 }
4581
event_test_thread(void * unused)4582 static __init int event_test_thread(void *unused)
4583 {
4584 void *test_malloc;
4585
4586 test_malloc = kmalloc(1234, GFP_KERNEL);
4587 if (!test_malloc)
4588 pr_info("failed to kmalloc\n");
4589
4590 schedule_on_each_cpu(test_work);
4591
4592 kfree(test_malloc);
4593
4594 set_current_state(TASK_INTERRUPTIBLE);
4595 while (!kthread_should_stop()) {
4596 schedule();
4597 set_current_state(TASK_INTERRUPTIBLE);
4598 }
4599 __set_current_state(TASK_RUNNING);
4600
4601 return 0;
4602 }
4603
4604 /*
4605 * Do various things that may trigger events.
4606 */
event_test_stuff(void)4607 static __init void event_test_stuff(void)
4608 {
4609 struct task_struct *test_thread;
4610
4611 test_thread = kthread_run(event_test_thread, NULL, "test-events");
4612 msleep(1);
4613 kthread_stop(test_thread);
4614 }
4615
4616 /*
4617 * For every trace event defined, we will test each trace point separately,
4618 * and then by groups, and finally all trace points.
4619 */
event_trace_self_tests(void)4620 static __init void event_trace_self_tests(void)
4621 {
4622 struct trace_subsystem_dir *dir;
4623 struct trace_event_file *file;
4624 struct trace_event_call *call;
4625 struct event_subsystem *system;
4626 struct trace_array *tr;
4627 int ret;
4628
4629 tr = top_trace_array();
4630 if (!tr)
4631 return;
4632
4633 pr_info("Running tests on trace events:\n");
4634
4635 list_for_each_entry(file, &tr->events, list) {
4636
4637 call = file->event_call;
4638
4639 /* Only test those that have a probe */
4640 if (!call->class || !call->class->probe)
4641 continue;
4642
4643 /*
4644 * Testing syscall events here is pretty useless, but
4645 * we still do it if configured. But this is time consuming.
4646 * What we really need is a user thread to perform the
4647 * syscalls as we test.
4648 */
4649 #ifndef CONFIG_EVENT_TRACE_TEST_SYSCALLS
4650 if (call->class->system &&
4651 strcmp(call->class->system, "syscalls") == 0)
4652 continue;
4653 #endif
4654
4655 pr_info("Testing event %s: ", trace_event_name(call));
4656
4657 /*
4658 * If an event is already enabled, someone is using
4659 * it and the self test should not be on.
4660 */
4661 if (file->flags & EVENT_FILE_FL_ENABLED) {
4662 pr_warn("Enabled event during self test!\n");
4663 WARN_ON_ONCE(1);
4664 continue;
4665 }
4666
4667 ftrace_event_enable_disable(file, 1);
4668 event_test_stuff();
4669 ftrace_event_enable_disable(file, 0);
4670
4671 pr_cont("OK\n");
4672 }
4673
4674 /* Now test at the sub system level */
4675
4676 pr_info("Running tests on trace event systems:\n");
4677
4678 list_for_each_entry(dir, &tr->systems, list) {
4679
4680 system = dir->subsystem;
4681
4682 /* the ftrace system is special, skip it */
4683 if (strcmp(system->name, "ftrace") == 0)
4684 continue;
4685
4686 pr_info("Testing event system %s: ", system->name);
4687
4688 ret = __ftrace_set_clr_event(tr, NULL, system->name, NULL, 1, NULL);
4689 if (WARN_ON_ONCE(ret)) {
4690 pr_warn("error enabling system %s\n",
4691 system->name);
4692 continue;
4693 }
4694
4695 event_test_stuff();
4696
4697 ret = __ftrace_set_clr_event(tr, NULL, system->name, NULL, 0, NULL);
4698 if (WARN_ON_ONCE(ret)) {
4699 pr_warn("error disabling system %s\n",
4700 system->name);
4701 continue;
4702 }
4703
4704 pr_cont("OK\n");
4705 }
4706
4707 /* Test with all events enabled */
4708
4709 pr_info("Running tests on all trace events:\n");
4710 pr_info("Testing all events: ");
4711
4712 ret = __ftrace_set_clr_event(tr, NULL, NULL, NULL, 1, NULL);
4713 if (WARN_ON_ONCE(ret)) {
4714 pr_warn("error enabling all events\n");
4715 return;
4716 }
4717
4718 event_test_stuff();
4719
4720 /* reset sysname */
4721 ret = __ftrace_set_clr_event(tr, NULL, NULL, NULL, 0, NULL);
4722 if (WARN_ON_ONCE(ret)) {
4723 pr_warn("error disabling all events\n");
4724 return;
4725 }
4726
4727 pr_cont("OK\n");
4728 }
4729
4730 #ifdef CONFIG_FUNCTION_TRACER
4731
4732 static DEFINE_PER_CPU(atomic_t, ftrace_test_event_disable);
4733
4734 static struct trace_event_file event_trace_file __initdata;
4735
4736 static void __init
function_test_events_call(unsigned long ip,unsigned long parent_ip,struct ftrace_ops * op,struct ftrace_regs * regs)4737 function_test_events_call(unsigned long ip, unsigned long parent_ip,
4738 struct ftrace_ops *op, struct ftrace_regs *regs)
4739 {
4740 struct trace_buffer *buffer;
4741 struct ring_buffer_event *event;
4742 struct ftrace_entry *entry;
4743 unsigned int trace_ctx;
4744 long disabled;
4745 int cpu;
4746
4747 trace_ctx = tracing_gen_ctx();
4748 preempt_disable_notrace();
4749 cpu = raw_smp_processor_id();
4750 disabled = atomic_inc_return(&per_cpu(ftrace_test_event_disable, cpu));
4751
4752 if (disabled != 1)
4753 goto out;
4754
4755 event = trace_event_buffer_lock_reserve(&buffer, &event_trace_file,
4756 TRACE_FN, sizeof(*entry),
4757 trace_ctx);
4758 if (!event)
4759 goto out;
4760 entry = ring_buffer_event_data(event);
4761 entry->ip = ip;
4762 entry->parent_ip = parent_ip;
4763
4764 event_trigger_unlock_commit(&event_trace_file, buffer, event,
4765 entry, trace_ctx);
4766 out:
4767 atomic_dec(&per_cpu(ftrace_test_event_disable, cpu));
4768 preempt_enable_notrace();
4769 }
4770
4771 static struct ftrace_ops trace_ops __initdata =
4772 {
4773 .func = function_test_events_call,
4774 };
4775
event_trace_self_test_with_function(void)4776 static __init void event_trace_self_test_with_function(void)
4777 {
4778 int ret;
4779
4780 event_trace_file.tr = top_trace_array();
4781 if (WARN_ON(!event_trace_file.tr))
4782 return;
4783
4784 ret = register_ftrace_function(&trace_ops);
4785 if (WARN_ON(ret < 0)) {
4786 pr_info("Failed to enable function tracer for event tests\n");
4787 return;
4788 }
4789 pr_info("Running tests again, along with the function tracer\n");
4790 event_trace_self_tests();
4791 unregister_ftrace_function(&trace_ops);
4792 }
4793 #else
event_trace_self_test_with_function(void)4794 static __init void event_trace_self_test_with_function(void)
4795 {
4796 }
4797 #endif
4798
event_trace_self_tests_init(void)4799 static __init int event_trace_self_tests_init(void)
4800 {
4801 if (!tracing_selftest_disabled) {
4802 event_trace_self_tests();
4803 event_trace_self_test_with_function();
4804 }
4805
4806 return 0;
4807 }
4808
4809 late_initcall(event_trace_self_tests_init);
4810
4811 #endif
4812