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