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