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