]> git.ipfire.org Git - thirdparty/kernel/stable.git/blame - kernel/trace/trace_events.c
Merge tag 'kvm-x86-misc-6.7' of https://github.com/kvm-x86/linux into HEAD
[thirdparty/kernel/stable.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) {
27152bce 987 eventfs_remove(dir->ef);
f6a84bdc
ON
988 list_del(&dir->list);
989 __put_system_dir(dir);
990 }
991}
992
7f1d2f82 993static void remove_event_file_dir(struct trace_event_file *file)
f6a84bdc 994{
27152bce 995 eventfs_remove(file->ef);
f6a84bdc 996 list_del(&file->list);
f6a84bdc 997 remove_subsystem(file->system);
2448e349 998 free_event_filter(file->filter);
f6a84bdc
ON
999 kmem_cache_free(file_cachep, file);
1000}
1001
8f31bfe5
LZ
1002/*
1003 * __ftrace_set_clr_event(NULL, NULL, NULL, set) will set/unset all events.
1004 */
2a6c24af
SRRH
1005static int
1006__ftrace_set_clr_event_nolock(struct trace_array *tr, const char *match,
1007 const char *sub, const char *event, int set)
b77e38aa 1008{
7f1d2f82 1009 struct trace_event_file *file;
2425bcb9 1010 struct trace_event_call *call;
de7b2973 1011 const char *name;
29f93943 1012 int ret = -EINVAL;
989a0a3d 1013 int eret = 0;
8f31bfe5 1014
ae63b31e
SR
1015 list_for_each_entry(file, &tr->events, list) {
1016
1017 call = file->event_call;
687fcc4a 1018 name = trace_event_name(call);
8f31bfe5 1019
de7b2973 1020 if (!name || !call->class || !call->class->reg)
8f31bfe5
LZ
1021 continue;
1022
9b63776f
SR
1023 if (call->flags & TRACE_EVENT_FL_IGNORE_ENABLE)
1024 continue;
1025
8f31bfe5 1026 if (match &&
de7b2973 1027 strcmp(match, name) != 0 &&
8f082018 1028 strcmp(match, call->class->system) != 0)
8f31bfe5
LZ
1029 continue;
1030
8f082018 1031 if (sub && strcmp(sub, call->class->system) != 0)
8f31bfe5
LZ
1032 continue;
1033
de7b2973 1034 if (event && strcmp(event, name) != 0)
8f31bfe5
LZ
1035 continue;
1036
989a0a3d 1037 ret = ftrace_event_enable_disable(file, set);
8f31bfe5 1038
989a0a3d
SRRH
1039 /*
1040 * Save the first error and return that. Some events
1041 * may still have been enabled, but let the user
1042 * know that something went wrong.
1043 */
1044 if (ret && !eret)
1045 eret = ret;
1046
1047 ret = eret;
8f31bfe5 1048 }
2a6c24af
SRRH
1049
1050 return ret;
1051}
1052
1053static int __ftrace_set_clr_event(struct trace_array *tr, const char *match,
1054 const char *sub, const char *event, int set)
1055{
1056 int ret;
1057
1058 mutex_lock(&event_mutex);
1059 ret = __ftrace_set_clr_event_nolock(tr, match, sub, event, set);
8f31bfe5
LZ
1060 mutex_unlock(&event_mutex);
1061
1062 return ret;
1063}
1064
595a438c 1065int ftrace_set_clr_event(struct trace_array *tr, char *buf, int set)
8f31bfe5 1066{
b628b3e6 1067 char *event = NULL, *sub = NULL, *match;
84fce9db 1068 int ret;
b628b3e6 1069
953ae45a
DI
1070 if (!tr)
1071 return -ENOENT;
b628b3e6
SR
1072 /*
1073 * The buf format can be <subsystem>:<event-name>
1074 * *:<event-name> means any event by that name.
1075 * :<event-name> is the same.
1076 *
1077 * <subsystem>:* means all events in that subsystem
1078 * <subsystem>: means the same.
1079 *
1080 * <name> (no ':') means all events in a subsystem with
1081 * the name <name> or any event that matches <name>
1082 */
1083
1084 match = strsep(&buf, ":");
1085 if (buf) {
1086 sub = match;
1087 event = buf;
1088 match = NULL;
1089
1090 if (!strlen(sub) || strcmp(sub, "*") == 0)
1091 sub = NULL;
1092 if (!strlen(event) || strcmp(event, "*") == 0)
1093 event = NULL;
1094 }
b77e38aa 1095
84fce9db
JK
1096 ret = __ftrace_set_clr_event(tr, match, sub, event, set);
1097
1098 /* Put back the colon to allow this to be called again */
1099 if (buf)
1100 *(buf - 1) = ':';
1101
1102 return ret;
b77e38aa
SR
1103}
1104
4671c794
SR
1105/**
1106 * trace_set_clr_event - enable or disable an event
1107 * @system: system name to match (NULL for any system)
1108 * @event: event name to match (NULL for all events, within system)
1109 * @set: 1 to enable, 0 to disable
1110 *
1111 * This is a way for other parts of the kernel to enable or disable
1112 * event recording.
1113 *
1114 * Returns 0 on success, -EINVAL if the parameters do not match any
1115 * registered events.
1116 */
1117int trace_set_clr_event(const char *system, const char *event, int set)
1118{
ae63b31e
SR
1119 struct trace_array *tr = top_trace_array();
1120
dc81e5e3
YY
1121 if (!tr)
1122 return -ENODEV;
1123
ae63b31e 1124 return __ftrace_set_clr_event(tr, NULL, system, event, set);
4671c794 1125}
56355b83 1126EXPORT_SYMBOL_GPL(trace_set_clr_event);
4671c794 1127
28879787
DI
1128/**
1129 * trace_array_set_clr_event - enable or disable an event for a trace array.
1130 * @tr: concerned trace array.
1131 * @system: system name to match (NULL for any system)
1132 * @event: event name to match (NULL for all events, within system)
1133 * @enable: true to enable, false to disable
1134 *
1135 * This is a way for other parts of the kernel to enable or disable
1136 * event recording.
1137 *
1138 * Returns 0 on success, -EINVAL if the parameters do not match any
1139 * registered events.
1140 */
1141int trace_array_set_clr_event(struct trace_array *tr, const char *system,
1142 const char *event, bool enable)
1143{
1144 int set;
1145
1146 if (!tr)
1147 return -ENOENT;
1148
1149 set = (enable == true) ? 1 : 0;
1150 return __ftrace_set_clr_event(tr, NULL, system, event, set);
1151}
1152EXPORT_SYMBOL_GPL(trace_array_set_clr_event);
1153
b77e38aa
SR
1154/* 128 should be much more than enough */
1155#define EVENT_BUF_SIZE 127
1156
1157static ssize_t
1158ftrace_event_write(struct file *file, const char __user *ubuf,
1159 size_t cnt, loff_t *ppos)
1160{
48966364 1161 struct trace_parser parser;
ae63b31e
SR
1162 struct seq_file *m = file->private_data;
1163 struct trace_array *tr = m->private;
4ba7978e 1164 ssize_t read, ret;
b77e38aa 1165
4ba7978e 1166 if (!cnt)
b77e38aa
SR
1167 return 0;
1168
1852fcce
SR
1169 ret = tracing_update_buffers();
1170 if (ret < 0)
1171 return ret;
1172
48966364 1173 if (trace_parser_get_init(&parser, EVENT_BUF_SIZE + 1))
b77e38aa
SR
1174 return -ENOMEM;
1175
48966364 1176 read = trace_get_user(&parser, ubuf, cnt, ppos);
1177
4ba7978e 1178 if (read >= 0 && trace_parser_loaded((&parser))) {
48966364 1179 int set = 1;
b77e38aa 1180
48966364 1181 if (*parser.buffer == '!')
b77e38aa 1182 set = 0;
b77e38aa 1183
ae63b31e 1184 ret = ftrace_set_clr_event(tr, parser.buffer + !set, set);
b77e38aa 1185 if (ret)
48966364 1186 goto out_put;
b77e38aa 1187 }
b77e38aa
SR
1188
1189 ret = read;
1190
48966364 1191 out_put:
1192 trace_parser_put(&parser);
b77e38aa
SR
1193
1194 return ret;
1195}
1196
1197static void *
1198t_next(struct seq_file *m, void *v, loff_t *pos)
1199{
7f1d2f82 1200 struct trace_event_file *file = v;
2425bcb9 1201 struct trace_event_call *call;
ae63b31e 1202 struct trace_array *tr = m->private;
b77e38aa
SR
1203
1204 (*pos)++;
1205
ae63b31e
SR
1206 list_for_each_entry_continue(file, &tr->events, list) {
1207 call = file->event_call;
40e26815
SR
1208 /*
1209 * The ftrace subsystem is for showing formats only.
1210 * They can not be enabled or disabled via the event files.
1211 */
d045437a
SRRH
1212 if (call->class && call->class->reg &&
1213 !(call->flags & TRACE_EVENT_FL_IGNORE_ENABLE))
ae63b31e 1214 return file;
40e26815 1215 }
b77e38aa 1216
30bd39cd 1217 return NULL;
b77e38aa
SR
1218}
1219
1220static void *t_start(struct seq_file *m, loff_t *pos)
1221{
7f1d2f82 1222 struct trace_event_file *file;
ae63b31e 1223 struct trace_array *tr = m->private;
e1c7e2a6
LZ
1224 loff_t l;
1225
20c8928a 1226 mutex_lock(&event_mutex);
e1c7e2a6 1227
7f1d2f82 1228 file = list_entry(&tr->events, struct trace_event_file, list);
e1c7e2a6 1229 for (l = 0; l <= *pos; ) {
ae63b31e
SR
1230 file = t_next(m, file, &l);
1231 if (!file)
e1c7e2a6
LZ
1232 break;
1233 }
ae63b31e 1234 return file;
b77e38aa
SR
1235}
1236
1237static void *
1238s_next(struct seq_file *m, void *v, loff_t *pos)
1239{
7f1d2f82 1240 struct trace_event_file *file = v;
ae63b31e 1241 struct trace_array *tr = m->private;
b77e38aa
SR
1242
1243 (*pos)++;
1244
ae63b31e 1245 list_for_each_entry_continue(file, &tr->events, list) {
5d6ad960 1246 if (file->flags & EVENT_FILE_FL_ENABLED)
ae63b31e 1247 return file;
b77e38aa
SR
1248 }
1249
30bd39cd 1250 return NULL;
b77e38aa
SR
1251}
1252
1253static void *s_start(struct seq_file *m, loff_t *pos)
1254{
7f1d2f82 1255 struct trace_event_file *file;
ae63b31e 1256 struct trace_array *tr = m->private;
e1c7e2a6
LZ
1257 loff_t l;
1258
20c8928a 1259 mutex_lock(&event_mutex);
e1c7e2a6 1260
7f1d2f82 1261 file = list_entry(&tr->events, struct trace_event_file, list);
e1c7e2a6 1262 for (l = 0; l <= *pos; ) {
ae63b31e
SR
1263 file = s_next(m, file, &l);
1264 if (!file)
e1c7e2a6
LZ
1265 break;
1266 }
ae63b31e 1267 return file;
b77e38aa
SR
1268}
1269
1270static int t_show(struct seq_file *m, void *v)
1271{
7f1d2f82 1272 struct trace_event_file *file = v;
2425bcb9 1273 struct trace_event_call *call = file->event_call;
b77e38aa 1274
8f082018
SR
1275 if (strcmp(call->class->system, TRACE_SYSTEM) != 0)
1276 seq_printf(m, "%s:", call->class->system);
687fcc4a 1277 seq_printf(m, "%s\n", trace_event_name(call));
b77e38aa
SR
1278
1279 return 0;
1280}
1281
1282static void t_stop(struct seq_file *m, void *p)
1283{
20c8928a 1284 mutex_unlock(&event_mutex);
b77e38aa
SR
1285}
1286
f4d34a87 1287static void *
27683626 1288__next(struct seq_file *m, void *v, loff_t *pos, int type)
f4d34a87
SR
1289{
1290 struct trace_array *tr = m->private;
27683626
SRV
1291 struct trace_pid_list *pid_list;
1292
1293 if (type == TRACE_PIDS)
1294 pid_list = rcu_dereference_sched(tr->filtered_pids);
1295 else
1296 pid_list = rcu_dereference_sched(tr->filtered_no_pids);
f4d34a87 1297
5cc8976b 1298 return trace_pid_next(pid_list, v, pos);
f4d34a87
SR
1299}
1300
27683626
SRV
1301static void *
1302p_next(struct seq_file *m, void *v, loff_t *pos)
1303{
1304 return __next(m, v, pos, TRACE_PIDS);
1305}
1306
1307static void *
1308np_next(struct seq_file *m, void *v, loff_t *pos)
1309{
1310 return __next(m, v, pos, TRACE_NO_PIDS);
1311}
1312
1313static void *__start(struct seq_file *m, loff_t *pos, int type)
fb662288 1314 __acquires(RCU)
49090107
SRRH
1315{
1316 struct trace_pid_list *pid_list;
1317 struct trace_array *tr = m->private;
1318
1319 /*
1320 * Grab the mutex, to keep calls to p_next() having the same
1321 * tr->filtered_pids as p_start() has.
1322 * If we just passed the tr->filtered_pids around, then RCU would
1323 * have been enough, but doing that makes things more complex.
1324 */
1325 mutex_lock(&event_mutex);
1326 rcu_read_lock_sched();
1327
27683626
SRV
1328 if (type == TRACE_PIDS)
1329 pid_list = rcu_dereference_sched(tr->filtered_pids);
1330 else
1331 pid_list = rcu_dereference_sched(tr->filtered_no_pids);
49090107 1332
f4d34a87 1333 if (!pid_list)
49090107
SRRH
1334 return NULL;
1335
5cc8976b 1336 return trace_pid_start(pid_list, pos);
49090107
SRRH
1337}
1338
27683626
SRV
1339static void *p_start(struct seq_file *m, loff_t *pos)
1340 __acquires(RCU)
1341{
1342 return __start(m, pos, TRACE_PIDS);
1343}
1344
1345static void *np_start(struct seq_file *m, loff_t *pos)
1346 __acquires(RCU)
1347{
1348 return __start(m, pos, TRACE_NO_PIDS);
1349}
1350
49090107 1351static void p_stop(struct seq_file *m, void *p)
fb662288 1352 __releases(RCU)
49090107
SRRH
1353{
1354 rcu_read_unlock_sched();
1355 mutex_unlock(&event_mutex);
1356}
1357
1473e441
SR
1358static ssize_t
1359event_enable_read(struct file *filp, char __user *ubuf, size_t cnt,
1360 loff_t *ppos)
1361{
7f1d2f82 1362 struct trace_event_file *file;
bc6f6b08 1363 unsigned long flags;
a4390596
TZ
1364 char buf[4] = "0";
1365
bc6f6b08
ON
1366 mutex_lock(&event_mutex);
1367 file = event_file_data(filp);
1368 if (likely(file))
1369 flags = file->flags;
1370 mutex_unlock(&event_mutex);
1371
1372 if (!file)
1373 return -ENODEV;
1374
5d6ad960
SRRH
1375 if (flags & EVENT_FILE_FL_ENABLED &&
1376 !(flags & EVENT_FILE_FL_SOFT_DISABLED))
a4390596
TZ
1377 strcpy(buf, "1");
1378
5d6ad960
SRRH
1379 if (flags & EVENT_FILE_FL_SOFT_DISABLED ||
1380 flags & EVENT_FILE_FL_SOFT_MODE)
a4390596
TZ
1381 strcat(buf, "*");
1382
1383 strcat(buf, "\n");
1473e441 1384
417944c4 1385 return simple_read_from_buffer(ubuf, cnt, ppos, buf, strlen(buf));
1473e441
SR
1386}
1387
1388static ssize_t
1389event_enable_write(struct file *filp, const char __user *ubuf, size_t cnt,
1390 loff_t *ppos)
1391{
7f1d2f82 1392 struct trace_event_file *file;
1473e441
SR
1393 unsigned long val;
1394 int ret;
1395
22fe9b54
PH
1396 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
1397 if (ret)
1473e441
SR
1398 return ret;
1399
1852fcce
SR
1400 ret = tracing_update_buffers();
1401 if (ret < 0)
1402 return ret;
1403
1473e441
SR
1404 switch (val) {
1405 case 0:
1473e441 1406 case 1:
bc6f6b08 1407 ret = -ENODEV;
11a241a3 1408 mutex_lock(&event_mutex);
bc6f6b08
ON
1409 file = event_file_data(filp);
1410 if (likely(file))
1411 ret = ftrace_event_enable_disable(file, val);
11a241a3 1412 mutex_unlock(&event_mutex);
1473e441
SR
1413 break;
1414
1415 default:
1416 return -EINVAL;
1417 }
1418
1419 *ppos += cnt;
1420
3b8e4273 1421 return ret ? ret : cnt;
1473e441
SR
1422}
1423
8ae79a13
SR
1424static ssize_t
1425system_enable_read(struct file *filp, char __user *ubuf, size_t cnt,
1426 loff_t *ppos)
1427{
c142b15d 1428 const char set_to_char[4] = { '?', '0', '1', 'X' };
7967b3e0 1429 struct trace_subsystem_dir *dir = filp->private_data;
ae63b31e 1430 struct event_subsystem *system = dir->subsystem;
2425bcb9 1431 struct trace_event_call *call;
7f1d2f82 1432 struct trace_event_file *file;
ae63b31e 1433 struct trace_array *tr = dir->tr;
8ae79a13 1434 char buf[2];
c142b15d 1435 int set = 0;
8ae79a13
SR
1436 int ret;
1437
8ae79a13 1438 mutex_lock(&event_mutex);
ae63b31e
SR
1439 list_for_each_entry(file, &tr->events, list) {
1440 call = file->event_call;
256cfdd6
SRV
1441 if ((call->flags & TRACE_EVENT_FL_IGNORE_ENABLE) ||
1442 !trace_event_name(call) || !call->class || !call->class->reg)
8ae79a13
SR
1443 continue;
1444
40ee4dff 1445 if (system && strcmp(call->class->system, system->name) != 0)
8ae79a13
SR
1446 continue;
1447
1448 /*
1449 * We need to find out if all the events are set
1450 * or if all events or cleared, or if we have
1451 * a mixture.
1452 */
5d6ad960 1453 set |= (1 << !!(file->flags & EVENT_FILE_FL_ENABLED));
c142b15d 1454
8ae79a13
SR
1455 /*
1456 * If we have a mixture, no need to look further.
1457 */
c142b15d 1458 if (set == 3)
8ae79a13
SR
1459 break;
1460 }
1461 mutex_unlock(&event_mutex);
1462
c142b15d 1463 buf[0] = set_to_char[set];
8ae79a13 1464 buf[1] = '\n';
8ae79a13
SR
1465
1466 ret = simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
1467
1468 return ret;
1469}
1470
1471static ssize_t
1472system_enable_write(struct file *filp, const char __user *ubuf, size_t cnt,
1473 loff_t *ppos)
1474{
7967b3e0 1475 struct trace_subsystem_dir *dir = filp->private_data;
ae63b31e 1476 struct event_subsystem *system = dir->subsystem;
40ee4dff 1477 const char *name = NULL;
8ae79a13 1478 unsigned long val;
8ae79a13
SR
1479 ssize_t ret;
1480
22fe9b54
PH
1481 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
1482 if (ret)
8ae79a13
SR
1483 return ret;
1484
1485 ret = tracing_update_buffers();
1486 if (ret < 0)
1487 return ret;
1488
8f31bfe5 1489 if (val != 0 && val != 1)
8ae79a13 1490 return -EINVAL;
8ae79a13 1491
40ee4dff
SR
1492 /*
1493 * Opening of "enable" adds a ref count to system,
1494 * so the name is safe to use.
1495 */
1496 if (system)
1497 name = system->name;
1498
ae63b31e 1499 ret = __ftrace_set_clr_event(dir->tr, NULL, name, NULL, val);
8ae79a13 1500 if (ret)
8f31bfe5 1501 goto out;
8ae79a13
SR
1502
1503 ret = cnt;
1504
8f31bfe5 1505out:
8ae79a13
SR
1506 *ppos += cnt;
1507
1508 return ret;
1509}
1510
2a37a3df
SR
1511enum {
1512 FORMAT_HEADER = 1,
86397dc3
LZ
1513 FORMAT_FIELD_SEPERATOR = 2,
1514 FORMAT_PRINTFMT = 3,
2a37a3df
SR
1515};
1516
1517static void *f_next(struct seq_file *m, void *v, loff_t *pos)
981d081e 1518{
2425bcb9 1519 struct trace_event_call *call = event_file_data(m->private);
86397dc3
LZ
1520 struct list_head *common_head = &ftrace_common_fields;
1521 struct list_head *head = trace_get_fields(call);
7710b639 1522 struct list_head *node = v;
981d081e 1523
2a37a3df 1524 (*pos)++;
5a65e956 1525
2a37a3df
SR
1526 switch ((unsigned long)v) {
1527 case FORMAT_HEADER:
7710b639
ON
1528 node = common_head;
1529 break;
5a65e956 1530
86397dc3 1531 case FORMAT_FIELD_SEPERATOR:
7710b639
ON
1532 node = head;
1533 break;
5a65e956 1534
2a37a3df
SR
1535 case FORMAT_PRINTFMT:
1536 /* all done */
1537 return NULL;
5a65e956
LJ
1538 }
1539
7710b639
ON
1540 node = node->prev;
1541 if (node == common_head)
86397dc3 1542 return (void *)FORMAT_FIELD_SEPERATOR;
7710b639 1543 else if (node == head)
2a37a3df 1544 return (void *)FORMAT_PRINTFMT;
7710b639
ON
1545 else
1546 return node;
2a37a3df
SR
1547}
1548
1549static int f_show(struct seq_file *m, void *v)
1550{
2425bcb9 1551 struct trace_event_call *call = event_file_data(m->private);
2a37a3df
SR
1552 struct ftrace_event_field *field;
1553 const char *array_descriptor;
1554
1555 switch ((unsigned long)v) {
1556 case FORMAT_HEADER:
687fcc4a 1557 seq_printf(m, "name: %s\n", trace_event_name(call));
2a37a3df 1558 seq_printf(m, "ID: %d\n", call->event.type);
fa6f0cc7 1559 seq_puts(m, "format:\n");
8728fe50 1560 return 0;
5a65e956 1561
86397dc3
LZ
1562 case FORMAT_FIELD_SEPERATOR:
1563 seq_putc(m, '\n');
1564 return 0;
1565
2a37a3df
SR
1566 case FORMAT_PRINTFMT:
1567 seq_printf(m, "\nprint fmt: %s\n",
1568 call->print_fmt);
1569 return 0;
981d081e 1570 }
8728fe50 1571
7710b639 1572 field = list_entry(v, struct ftrace_event_field, link);
2a37a3df
SR
1573 /*
1574 * Smartly shows the array type(except dynamic array).
1575 * Normal:
1576 * field:TYPE VAR
1577 * If TYPE := TYPE[LEN], it is shown:
1578 * field:TYPE VAR[LEN]
1579 */
1580 array_descriptor = strchr(field->type, '[');
8728fe50 1581
b6b27355 1582 if (str_has_prefix(field->type, "__data_loc"))
2a37a3df 1583 array_descriptor = NULL;
8728fe50 1584
2a37a3df
SR
1585 if (!array_descriptor)
1586 seq_printf(m, "\tfield:%s %s;\toffset:%u;\tsize:%u;\tsigned:%d;\n",
1587 field->type, field->name, field->offset,
1588 field->size, !!field->is_signed);
b6c7abd1
YS
1589 else if (field->len)
1590 seq_printf(m, "\tfield:%.*s %s[%d];\toffset:%u;\tsize:%u;\tsigned:%d;\n",
2a37a3df
SR
1591 (int)(array_descriptor - field->type),
1592 field->type, field->name,
b6c7abd1 1593 field->len, field->offset,
2a37a3df 1594 field->size, !!field->is_signed);
b6c7abd1
YS
1595 else
1596 seq_printf(m, "\tfield:%.*s %s[];\toffset:%u;\tsize:%u;\tsigned:%d;\n",
1597 (int)(array_descriptor - field->type),
1598 field->type, field->name,
1599 field->offset, field->size, !!field->is_signed);
8728fe50 1600
2a37a3df
SR
1601 return 0;
1602}
5a65e956 1603
7710b639
ON
1604static void *f_start(struct seq_file *m, loff_t *pos)
1605{
1606 void *p = (void *)FORMAT_HEADER;
1607 loff_t l = 0;
1608
c5a44a12
ON
1609 /* ->stop() is called even if ->start() fails */
1610 mutex_lock(&event_mutex);
1611 if (!event_file_data(m->private))
1612 return ERR_PTR(-ENODEV);
1613
7710b639
ON
1614 while (l < *pos && p)
1615 p = f_next(m, p, &l);
1616
1617 return p;
1618}
1619
2a37a3df
SR
1620static void f_stop(struct seq_file *m, void *p)
1621{
c5a44a12 1622 mutex_unlock(&event_mutex);
2a37a3df 1623}
981d081e 1624
2a37a3df
SR
1625static const struct seq_operations trace_format_seq_ops = {
1626 .start = f_start,
1627 .next = f_next,
1628 .stop = f_stop,
1629 .show = f_show,
1630};
1631
1632static int trace_format_open(struct inode *inode, struct file *file)
1633{
2a37a3df
SR
1634 struct seq_file *m;
1635 int ret;
1636
17911ff3
SRV
1637 /* Do we want to hide event format files on tracefs lockdown? */
1638
2a37a3df
SR
1639 ret = seq_open(file, &trace_format_seq_ops);
1640 if (ret < 0)
1641 return ret;
1642
1643 m = file->private_data;
c5a44a12 1644 m->private = file;
2a37a3df
SR
1645
1646 return 0;
981d081e
SR
1647}
1648
23725aee
PZ
1649static ssize_t
1650event_id_read(struct file *filp, char __user *ubuf, size_t cnt, loff_t *ppos)
1651{
1a11126b 1652 int id = (long)event_file_data(filp);
cd458ba9
ON
1653 char buf[32];
1654 int len;
23725aee 1655
1a11126b
ON
1656 if (unlikely(!id))
1657 return -ENODEV;
1658
1659 len = sprintf(buf, "%d\n", id);
1660
cd458ba9 1661 return simple_read_from_buffer(ubuf, cnt, ppos, buf, len);
23725aee
PZ
1662}
1663
7ce7e424
TZ
1664static ssize_t
1665event_filter_read(struct file *filp, char __user *ubuf, size_t cnt,
1666 loff_t *ppos)
1667{
7f1d2f82 1668 struct trace_event_file *file;
7ce7e424 1669 struct trace_seq *s;
e2912b09 1670 int r = -ENODEV;
7ce7e424
TZ
1671
1672 if (*ppos)
1673 return 0;
1674
1675 s = kmalloc(sizeof(*s), GFP_KERNEL);
e2912b09 1676
7ce7e424
TZ
1677 if (!s)
1678 return -ENOMEM;
1679
1680 trace_seq_init(s);
1681
e2912b09 1682 mutex_lock(&event_mutex);
f306cc82
TZ
1683 file = event_file_data(filp);
1684 if (file)
1685 print_event_filter(file, s);
e2912b09
ON
1686 mutex_unlock(&event_mutex);
1687
f306cc82 1688 if (file)
5ac48378
SRRH
1689 r = simple_read_from_buffer(ubuf, cnt, ppos,
1690 s->buffer, trace_seq_used(s));
7ce7e424
TZ
1691
1692 kfree(s);
1693
1694 return r;
1695}
1696
1697static ssize_t
1698event_filter_write(struct file *filp, const char __user *ubuf, size_t cnt,
1699 loff_t *ppos)
1700{
7f1d2f82 1701 struct trace_event_file *file;
8b372562 1702 char *buf;
e2912b09 1703 int err = -ENODEV;
7ce7e424 1704
8b372562 1705 if (cnt >= PAGE_SIZE)
7ce7e424
TZ
1706 return -EINVAL;
1707
70f6cbb6
AV
1708 buf = memdup_user_nul(ubuf, cnt);
1709 if (IS_ERR(buf))
1710 return PTR_ERR(buf);
7ce7e424 1711
e2912b09 1712 mutex_lock(&event_mutex);
f306cc82
TZ
1713 file = event_file_data(filp);
1714 if (file)
1715 err = apply_event_filter(file, buf);
e2912b09
ON
1716 mutex_unlock(&event_mutex);
1717
70f6cbb6 1718 kfree(buf);
8b372562 1719 if (err < 0)
44e9c8b7 1720 return err;
0a19e53c 1721
7ce7e424
TZ
1722 *ppos += cnt;
1723
1724 return cnt;
1725}
1726
e9dbfae5
SR
1727static LIST_HEAD(event_subsystems);
1728
1729static int subsystem_open(struct inode *inode, struct file *filp)
1730{
99d8ae4e
JK
1731 struct trace_subsystem_dir *dir = NULL, *iter_dir;
1732 struct trace_array *tr = NULL, *iter_tr;
e9dbfae5
SR
1733 struct event_subsystem *system = NULL;
1734 int ret;
1735
d6d3523c
GB
1736 if (tracing_is_disabled())
1737 return -ENODEV;
1738
e9dbfae5
SR
1739 /* Make sure the system still exists */
1740 mutex_lock(&event_mutex);
12ecef0c 1741 mutex_lock(&trace_types_lock);
99d8ae4e
JK
1742 list_for_each_entry(iter_tr, &ftrace_trace_arrays, list) {
1743 list_for_each_entry(iter_dir, &iter_tr->systems, list) {
1744 if (iter_dir == inode->i_private) {
ae63b31e 1745 /* Don't open systems with no events */
99d8ae4e
JK
1746 tr = iter_tr;
1747 dir = iter_dir;
ae63b31e
SR
1748 if (dir->nr_events) {
1749 __get_system_dir(dir);
1750 system = dir->subsystem;
1751 }
1752 goto exit_loop;
e9dbfae5 1753 }
e9dbfae5
SR
1754 }
1755 }
ae63b31e 1756 exit_loop:
a8227415 1757 mutex_unlock(&trace_types_lock);
12ecef0c 1758 mutex_unlock(&event_mutex);
e9dbfae5 1759
ae63b31e 1760 if (!system)
e9dbfae5
SR
1761 return -ENODEV;
1762
8e2e2fa4
SRRH
1763 /* Still need to increment the ref count of the system */
1764 if (trace_array_get(tr) < 0) {
1765 put_system(dir);
1766 return -ENODEV;
1767 }
1768
e9dbfae5 1769 ret = tracing_open_generic(inode, filp);
8e2e2fa4
SRRH
1770 if (ret < 0) {
1771 trace_array_put(tr);
ae63b31e 1772 put_system(dir);
8e2e2fa4 1773 }
ae63b31e
SR
1774
1775 return ret;
1776}
1777
1778static int system_tr_open(struct inode *inode, struct file *filp)
1779{
7967b3e0 1780 struct trace_subsystem_dir *dir;
ae63b31e
SR
1781 struct trace_array *tr = inode->i_private;
1782 int ret;
1783
1784 /* Make a temporary dir that has no system but points to tr */
1785 dir = kzalloc(sizeof(*dir), GFP_KERNEL);
aa07d71f 1786 if (!dir)
ae63b31e 1787 return -ENOMEM;
ae63b31e 1788
aa07d71f 1789 ret = tracing_open_generic_tr(inode, filp);
8e2e2fa4 1790 if (ret < 0) {
ae63b31e 1791 kfree(dir);
d6d3523c 1792 return ret;
8e2e2fa4 1793 }
aa07d71f 1794 dir->tr = tr;
ae63b31e 1795 filp->private_data = dir;
e9dbfae5 1796
d6d3523c 1797 return 0;
e9dbfae5
SR
1798}
1799
1800static int subsystem_release(struct inode *inode, struct file *file)
1801{
7967b3e0 1802 struct trace_subsystem_dir *dir = file->private_data;
e9dbfae5 1803
8e2e2fa4
SRRH
1804 trace_array_put(dir->tr);
1805
ae63b31e
SR
1806 /*
1807 * If dir->subsystem is NULL, then this is a temporary
1808 * descriptor that was made for a trace_array to enable
1809 * all subsystems.
1810 */
1811 if (dir->subsystem)
1812 put_system(dir);
1813 else
1814 kfree(dir);
e9dbfae5
SR
1815
1816 return 0;
1817}
1818
cfb180f3
TZ
1819static ssize_t
1820subsystem_filter_read(struct file *filp, char __user *ubuf, size_t cnt,
1821 loff_t *ppos)
1822{
7967b3e0 1823 struct trace_subsystem_dir *dir = filp->private_data;
ae63b31e 1824 struct event_subsystem *system = dir->subsystem;
cfb180f3
TZ
1825 struct trace_seq *s;
1826 int r;
1827
1828 if (*ppos)
1829 return 0;
1830
1831 s = kmalloc(sizeof(*s), GFP_KERNEL);
1832 if (!s)
1833 return -ENOMEM;
1834
1835 trace_seq_init(s);
1836
8b372562 1837 print_subsystem_event_filter(system, s);
5ac48378
SRRH
1838 r = simple_read_from_buffer(ubuf, cnt, ppos,
1839 s->buffer, trace_seq_used(s));
cfb180f3
TZ
1840
1841 kfree(s);
1842
1843 return r;
1844}
1845
1846static ssize_t
1847subsystem_filter_write(struct file *filp, const char __user *ubuf, size_t cnt,
1848 loff_t *ppos)
1849{
7967b3e0 1850 struct trace_subsystem_dir *dir = filp->private_data;
8b372562 1851 char *buf;
cfb180f3
TZ
1852 int err;
1853
8b372562 1854 if (cnt >= PAGE_SIZE)
cfb180f3
TZ
1855 return -EINVAL;
1856
70f6cbb6
AV
1857 buf = memdup_user_nul(ubuf, cnt);
1858 if (IS_ERR(buf))
1859 return PTR_ERR(buf);
cfb180f3 1860
ae63b31e 1861 err = apply_subsystem_event_filter(dir, buf);
70f6cbb6 1862 kfree(buf);
8b372562 1863 if (err < 0)
44e9c8b7 1864 return err;
cfb180f3
TZ
1865
1866 *ppos += cnt;
1867
1868 return cnt;
1869}
1870
d1b182a8
SR
1871static ssize_t
1872show_header(struct file *filp, char __user *ubuf, size_t cnt, loff_t *ppos)
1873{
1874 int (*func)(struct trace_seq *s) = filp->private_data;
1875 struct trace_seq *s;
1876 int r;
1877
1878 if (*ppos)
1879 return 0;
1880
1881 s = kmalloc(sizeof(*s), GFP_KERNEL);
1882 if (!s)
1883 return -ENOMEM;
1884
1885 trace_seq_init(s);
1886
1887 func(s);
5ac48378
SRRH
1888 r = simple_read_from_buffer(ubuf, cnt, ppos,
1889 s->buffer, trace_seq_used(s));
d1b182a8
SR
1890
1891 kfree(s);
1892
1893 return r;
1894}
1895
8ca532ad
SRRH
1896static void ignore_task_cpu(void *data)
1897{
1898 struct trace_array *tr = data;
1899 struct trace_pid_list *pid_list;
27683626 1900 struct trace_pid_list *no_pid_list;
8ca532ad
SRRH
1901
1902 /*
1903 * This function is called by on_each_cpu() while the
1904 * event_mutex is held.
1905 */
1906 pid_list = rcu_dereference_protected(tr->filtered_pids,
1907 mutex_is_locked(&event_mutex));
27683626
SRV
1908 no_pid_list = rcu_dereference_protected(tr->filtered_no_pids,
1909 mutex_is_locked(&event_mutex));
8ca532ad 1910
1c5eb448 1911 this_cpu_write(tr->array_buffer.data->ignore_pid,
27683626
SRV
1912 trace_ignore_this_task(pid_list, no_pid_list, current));
1913}
1914
1915static void register_pid_events(struct trace_array *tr)
1916{
1917 /*
1918 * Register a probe that is called before all other probes
1919 * to set ignore_pid if next or prev do not match.
1920 * Register a probe this is called after all other probes
1921 * to only keep ignore_pid set if next pid matches.
1922 */
1923 register_trace_prio_sched_switch(event_filter_pid_sched_switch_probe_pre,
1924 tr, INT_MAX);
1925 register_trace_prio_sched_switch(event_filter_pid_sched_switch_probe_post,
1926 tr, 0);
1927
1928 register_trace_prio_sched_wakeup(event_filter_pid_sched_wakeup_probe_pre,
1929 tr, INT_MAX);
1930 register_trace_prio_sched_wakeup(event_filter_pid_sched_wakeup_probe_post,
1931 tr, 0);
1932
1933 register_trace_prio_sched_wakeup_new(event_filter_pid_sched_wakeup_probe_pre,
1934 tr, INT_MAX);
1935 register_trace_prio_sched_wakeup_new(event_filter_pid_sched_wakeup_probe_post,
1936 tr, 0);
1937
1938 register_trace_prio_sched_waking(event_filter_pid_sched_wakeup_probe_pre,
1939 tr, INT_MAX);
1940 register_trace_prio_sched_waking(event_filter_pid_sched_wakeup_probe_post,
1941 tr, 0);
8ca532ad
SRRH
1942}
1943
49090107 1944static ssize_t
27683626
SRV
1945event_pid_write(struct file *filp, const char __user *ubuf,
1946 size_t cnt, loff_t *ppos, int type)
49090107 1947{
3fdaf80f 1948 struct seq_file *m = filp->private_data;
49090107
SRRH
1949 struct trace_array *tr = m->private;
1950 struct trace_pid_list *filtered_pids = NULL;
27683626 1951 struct trace_pid_list *other_pids = NULL;
f4d34a87 1952 struct trace_pid_list *pid_list;
3fdaf80f 1953 struct trace_event_file *file;
76c813e2 1954 ssize_t ret;
49090107
SRRH
1955
1956 if (!cnt)
1957 return 0;
1958
1959 ret = tracing_update_buffers();
1960 if (ret < 0)
1961 return ret;
1962
49090107 1963 mutex_lock(&event_mutex);
76c813e2 1964
27683626
SRV
1965 if (type == TRACE_PIDS) {
1966 filtered_pids = rcu_dereference_protected(tr->filtered_pids,
1967 lockdep_is_held(&event_mutex));
1968 other_pids = rcu_dereference_protected(tr->filtered_no_pids,
1969 lockdep_is_held(&event_mutex));
1970 } else {
1971 filtered_pids = rcu_dereference_protected(tr->filtered_no_pids,
1972 lockdep_is_held(&event_mutex));
1973 other_pids = rcu_dereference_protected(tr->filtered_pids,
1974 lockdep_is_held(&event_mutex));
1975 }
f4d34a87 1976
76c813e2
SRRH
1977 ret = trace_pid_write(filtered_pids, &pid_list, ubuf, cnt);
1978 if (ret < 0)
f4d34a87 1979 goto out;
49090107 1980
27683626
SRV
1981 if (type == TRACE_PIDS)
1982 rcu_assign_pointer(tr->filtered_pids, pid_list);
1983 else
1984 rcu_assign_pointer(tr->filtered_no_pids, pid_list);
49090107 1985
3fdaf80f
SRRH
1986 list_for_each_entry(file, &tr->events, list) {
1987 set_bit(EVENT_FILE_FL_PID_FILTER_BIT, &file->flags);
1988 }
49090107
SRRH
1989
1990 if (filtered_pids) {
e0a568dc 1991 tracepoint_synchronize_unregister();
6954e415 1992 trace_pid_list_free(filtered_pids);
27683626
SRV
1993 } else if (pid_list && !other_pids) {
1994 register_pid_events(tr);
49090107
SRRH
1995 }
1996
799fd44c
SRRH
1997 /*
1998 * Ignoring of pids is done at task switch. But we have to
1999 * check for those tasks that are currently running.
2000 * Always do this in case a pid was appended or removed.
2001 */
2002 on_each_cpu(ignore_task_cpu, tr, 1);
2003
f4d34a87 2004 out:
3fdaf80f
SRRH
2005 mutex_unlock(&event_mutex);
2006
76c813e2
SRRH
2007 if (ret > 0)
2008 *ppos += ret;
49090107
SRRH
2009
2010 return ret;
2011}
2012
27683626
SRV
2013static ssize_t
2014ftrace_event_pid_write(struct file *filp, const char __user *ubuf,
2015 size_t cnt, loff_t *ppos)
2016{
2017 return event_pid_write(filp, ubuf, cnt, ppos, TRACE_PIDS);
2018}
2019
2020static ssize_t
2021ftrace_event_npid_write(struct file *filp, const char __user *ubuf,
2022 size_t cnt, loff_t *ppos)
2023{
2024 return event_pid_write(filp, ubuf, cnt, ppos, TRACE_NO_PIDS);
2025}
2026
15075cac
SR
2027static int ftrace_event_avail_open(struct inode *inode, struct file *file);
2028static int ftrace_event_set_open(struct inode *inode, struct file *file);
49090107 2029static int ftrace_event_set_pid_open(struct inode *inode, struct file *file);
27683626 2030static int ftrace_event_set_npid_open(struct inode *inode, struct file *file);
f77d09a3 2031static int ftrace_event_release(struct inode *inode, struct file *file);
15075cac 2032
b77e38aa
SR
2033static const struct seq_operations show_event_seq_ops = {
2034 .start = t_start,
2035 .next = t_next,
2036 .show = t_show,
2037 .stop = t_stop,
2038};
2039
2040static const struct seq_operations show_set_event_seq_ops = {
2041 .start = s_start,
2042 .next = s_next,
2043 .show = t_show,
2044 .stop = t_stop,
2045};
2046
49090107
SRRH
2047static const struct seq_operations show_set_pid_seq_ops = {
2048 .start = p_start,
2049 .next = p_next,
5cc8976b 2050 .show = trace_pid_show,
49090107
SRRH
2051 .stop = p_stop,
2052};
2053
27683626
SRV
2054static const struct seq_operations show_set_no_pid_seq_ops = {
2055 .start = np_start,
2056 .next = np_next,
2057 .show = trace_pid_show,
2058 .stop = p_stop,
2059};
2060
2314c4ae 2061static const struct file_operations ftrace_avail_fops = {
15075cac 2062 .open = ftrace_event_avail_open,
2314c4ae
SR
2063 .read = seq_read,
2064 .llseek = seq_lseek,
2065 .release = seq_release,
2066};
2067
b77e38aa 2068static const struct file_operations ftrace_set_event_fops = {
15075cac 2069 .open = ftrace_event_set_open,
b77e38aa
SR
2070 .read = seq_read,
2071 .write = ftrace_event_write,
2072 .llseek = seq_lseek,
f77d09a3 2073 .release = ftrace_event_release,
b77e38aa
SR
2074};
2075
49090107
SRRH
2076static const struct file_operations ftrace_set_event_pid_fops = {
2077 .open = ftrace_event_set_pid_open,
2078 .read = seq_read,
2079 .write = ftrace_event_pid_write,
2080 .llseek = seq_lseek,
2081 .release = ftrace_event_release,
2082};
2083
27683626
SRV
2084static const struct file_operations ftrace_set_event_notrace_pid_fops = {
2085 .open = ftrace_event_set_npid_open,
2086 .read = seq_read,
2087 .write = ftrace_event_npid_write,
2088 .llseek = seq_lseek,
2089 .release = ftrace_event_release,
2090};
2091
1473e441 2092static const struct file_operations ftrace_enable_fops = {
f5ca233e 2093 .open = tracing_open_file_tr,
1473e441
SR
2094 .read = event_enable_read,
2095 .write = event_enable_write,
f5ca233e 2096 .release = tracing_release_file_tr,
6038f373 2097 .llseek = default_llseek,
1473e441
SR
2098};
2099
981d081e 2100static const struct file_operations ftrace_event_format_fops = {
2a37a3df
SR
2101 .open = trace_format_open,
2102 .read = seq_read,
2103 .llseek = seq_lseek,
2104 .release = seq_release,
981d081e
SR
2105};
2106
23725aee 2107static const struct file_operations ftrace_event_id_fops = {
23725aee 2108 .read = event_id_read,
6038f373 2109 .llseek = default_llseek,
23725aee
PZ
2110};
2111
7ce7e424 2112static const struct file_operations ftrace_event_filter_fops = {
f5ca233e 2113 .open = tracing_open_file_tr,
7ce7e424
TZ
2114 .read = event_filter_read,
2115 .write = event_filter_write,
f5ca233e 2116 .release = tracing_release_file_tr,
6038f373 2117 .llseek = default_llseek,
7ce7e424
TZ
2118};
2119
cfb180f3 2120static const struct file_operations ftrace_subsystem_filter_fops = {
e9dbfae5 2121 .open = subsystem_open,
cfb180f3
TZ
2122 .read = subsystem_filter_read,
2123 .write = subsystem_filter_write,
6038f373 2124 .llseek = default_llseek,
e9dbfae5 2125 .release = subsystem_release,
cfb180f3
TZ
2126};
2127
8ae79a13 2128static const struct file_operations ftrace_system_enable_fops = {
40ee4dff 2129 .open = subsystem_open,
8ae79a13
SR
2130 .read = system_enable_read,
2131 .write = system_enable_write,
6038f373 2132 .llseek = default_llseek,
40ee4dff 2133 .release = subsystem_release,
8ae79a13
SR
2134};
2135
ae63b31e
SR
2136static const struct file_operations ftrace_tr_enable_fops = {
2137 .open = system_tr_open,
2138 .read = system_enable_read,
2139 .write = system_enable_write,
2140 .llseek = default_llseek,
2141 .release = subsystem_release,
2142};
2143
d1b182a8
SR
2144static const struct file_operations ftrace_show_header_fops = {
2145 .open = tracing_open_generic,
2146 .read = show_header,
6038f373 2147 .llseek = default_llseek,
d1b182a8
SR
2148};
2149
ae63b31e
SR
2150static int
2151ftrace_event_open(struct inode *inode, struct file *file,
2152 const struct seq_operations *seq_ops)
1473e441 2153{
ae63b31e
SR
2154 struct seq_file *m;
2155 int ret;
1473e441 2156
17911ff3
SRV
2157 ret = security_locked_down(LOCKDOWN_TRACEFS);
2158 if (ret)
2159 return ret;
2160
ae63b31e
SR
2161 ret = seq_open(file, seq_ops);
2162 if (ret < 0)
2163 return ret;
2164 m = file->private_data;
2165 /* copy tr over to seq ops */
2166 m->private = inode->i_private;
1473e441 2167
ae63b31e 2168 return ret;
1473e441
SR
2169}
2170
f77d09a3
AL
2171static int ftrace_event_release(struct inode *inode, struct file *file)
2172{
2173 struct trace_array *tr = inode->i_private;
2174
2175 trace_array_put(tr);
2176
2177 return seq_release(inode, file);
2178}
2179
15075cac
SR
2180static int
2181ftrace_event_avail_open(struct inode *inode, struct file *file)
2182{
2183 const struct seq_operations *seq_ops = &show_event_seq_ops;
2184
17911ff3 2185 /* Checks for tracefs lockdown */
ae63b31e 2186 return ftrace_event_open(inode, file, seq_ops);
15075cac
SR
2187}
2188
2189static int
2190ftrace_event_set_open(struct inode *inode, struct file *file)
2191{
2192 const struct seq_operations *seq_ops = &show_set_event_seq_ops;
ae63b31e 2193 struct trace_array *tr = inode->i_private;
f77d09a3
AL
2194 int ret;
2195
8530dec6
SRV
2196 ret = tracing_check_open_get_tr(tr);
2197 if (ret)
2198 return ret;
15075cac
SR
2199
2200 if ((file->f_mode & FMODE_WRITE) &&
2201 (file->f_flags & O_TRUNC))
ae63b31e 2202 ftrace_clear_events(tr);
15075cac 2203
f77d09a3
AL
2204 ret = ftrace_event_open(inode, file, seq_ops);
2205 if (ret < 0)
2206 trace_array_put(tr);
2207 return ret;
ae63b31e
SR
2208}
2209
49090107
SRRH
2210static int
2211ftrace_event_set_pid_open(struct inode *inode, struct file *file)
2212{
2213 const struct seq_operations *seq_ops = &show_set_pid_seq_ops;
2214 struct trace_array *tr = inode->i_private;
2215 int ret;
2216
8530dec6
SRV
2217 ret = tracing_check_open_get_tr(tr);
2218 if (ret)
2219 return ret;
49090107
SRRH
2220
2221 if ((file->f_mode & FMODE_WRITE) &&
2222 (file->f_flags & O_TRUNC))
27683626
SRV
2223 ftrace_clear_event_pids(tr, TRACE_PIDS);
2224
2225 ret = ftrace_event_open(inode, file, seq_ops);
2226 if (ret < 0)
2227 trace_array_put(tr);
2228 return ret;
2229}
2230
2231static int
2232ftrace_event_set_npid_open(struct inode *inode, struct file *file)
2233{
2234 const struct seq_operations *seq_ops = &show_set_no_pid_seq_ops;
2235 struct trace_array *tr = inode->i_private;
2236 int ret;
2237
2238 ret = tracing_check_open_get_tr(tr);
2239 if (ret)
2240 return ret;
2241
2242 if ((file->f_mode & FMODE_WRITE) &&
2243 (file->f_flags & O_TRUNC))
2244 ftrace_clear_event_pids(tr, TRACE_NO_PIDS);
49090107
SRRH
2245
2246 ret = ftrace_event_open(inode, file, seq_ops);
2247 if (ret < 0)
2248 trace_array_put(tr);
2249 return ret;
2250}
2251
ae63b31e
SR
2252static struct event_subsystem *
2253create_new_subsystem(const char *name)
2254{
2255 struct event_subsystem *system;
2256
2257 /* need to create new entry */
2258 system = kmalloc(sizeof(*system), GFP_KERNEL);
2259 if (!system)
2260 return NULL;
2261
2262 system->ref_count = 1;
6e94a780
SR
2263
2264 /* Only allocate if dynamic (kprobes and modules) */
79ac6ef5
RV
2265 system->name = kstrdup_const(name, GFP_KERNEL);
2266 if (!system->name)
2267 goto out_free;
ae63b31e 2268
ae63b31e
SR
2269 system->filter = kzalloc(sizeof(struct event_filter), GFP_KERNEL);
2270 if (!system->filter)
2271 goto out_free;
2272
2273 list_add(&system->list, &event_subsystems);
2274
2275 return system;
2276
2277 out_free:
79ac6ef5 2278 kfree_const(system->name);
ae63b31e
SR
2279 kfree(system);
2280 return NULL;
15075cac
SR
2281}
2282
27152bce 2283static struct eventfs_file *
ae63b31e 2284event_subsystem_dir(struct trace_array *tr, const char *name,
7f1d2f82 2285 struct trace_event_file *file, struct dentry *parent)
6ecc2d1c 2286{
ba27d855 2287 struct event_subsystem *system, *iter;
7967b3e0 2288 struct trace_subsystem_dir *dir;
c8414dab 2289 struct eventfs_file *ef;
27152bce 2290 int res;
6ecc2d1c
SR
2291
2292 /* First see if we did not already create this dir */
ae63b31e
SR
2293 list_for_each_entry(dir, &tr->systems, list) {
2294 system = dir->subsystem;
dc82ec98 2295 if (strcmp(system->name, name) == 0) {
ae63b31e
SR
2296 dir->nr_events++;
2297 file->system = dir;
27152bce 2298 return dir->ef;
dc82ec98 2299 }
6ecc2d1c
SR
2300 }
2301
ae63b31e 2302 /* Now see if the system itself exists. */
ba27d855
JK
2303 system = NULL;
2304 list_for_each_entry(iter, &event_subsystems, list) {
2305 if (strcmp(iter->name, name) == 0) {
2306 system = iter;
ae63b31e 2307 break;
ba27d855 2308 }
6ecc2d1c
SR
2309 }
2310
ae63b31e
SR
2311 dir = kmalloc(sizeof(*dir), GFP_KERNEL);
2312 if (!dir)
2313 goto out_fail;
6ecc2d1c 2314
ae63b31e
SR
2315 if (!system) {
2316 system = create_new_subsystem(name);
2317 if (!system)
2318 goto out_free;
2319 } else
2320 __get_system(system);
2321
c8414dab
JR
2322 ef = eventfs_add_subsystem_dir(name, parent);
2323 if (IS_ERR(ef)) {
3448bac3 2324 pr_warn("Failed to create system directory %s\n", name);
ae63b31e
SR
2325 __put_system(system);
2326 goto out_free;
6d723736
SR
2327 }
2328
c8414dab 2329 dir->ef = ef;
ae63b31e
SR
2330 dir->tr = tr;
2331 dir->ref_count = 1;
2332 dir->nr_events = 1;
2333 dir->subsystem = system;
2334 file->system = dir;
8b372562 2335
2d396cb3
SRV
2336 /* the ftrace system is special, do not create enable or filter files */
2337 if (strcmp(name, "ftrace") != 0) {
e1112b4d 2338
27152bce
AK
2339 res = eventfs_add_file("filter", TRACE_MODE_WRITE,
2340 dir->ef, dir,
2d396cb3 2341 &ftrace_subsystem_filter_fops);
27152bce 2342 if (res) {
2d396cb3
SRV
2343 kfree(system->filter);
2344 system->filter = NULL;
2345 pr_warn("Could not create tracefs '%s/filter' entry\n", name);
2346 }
e1112b4d 2347
27152bce 2348 eventfs_add_file("enable", TRACE_MODE_WRITE, dir->ef, dir,
2d396cb3
SRV
2349 &ftrace_system_enable_fops);
2350 }
8ae79a13 2351
ae63b31e
SR
2352 list_add(&dir->list, &tr->systems);
2353
27152bce 2354 return dir->ef;
ae63b31e
SR
2355
2356 out_free:
2357 kfree(dir);
2358 out_fail:
2359 /* Only print this message if failed on memory allocation */
2360 if (!dir || !system)
3448bac3 2361 pr_warn("No memory to create event subsystem %s\n", name);
ae63b31e 2362 return NULL;
6ecc2d1c
SR
2363}
2364
ac343da7
MH
2365static int
2366event_define_fields(struct trace_event_call *call)
2367{
2368 struct list_head *head;
2369 int ret = 0;
2370
2371 /*
2372 * Other events may have the same class. Only update
2373 * the fields if they are not already defined.
2374 */
2375 head = trace_get_fields(call);
2376 if (list_empty(head)) {
2377 struct trace_event_fields *field = call->class->fields_array;
2378 unsigned int offset = sizeof(struct trace_entry);
2379
2380 for (; field->type; field++) {
2381 if (field->type == TRACE_FUNCTION_TYPE) {
2382 field->define_fields(call);
2383 break;
2384 }
2385
2386 offset = ALIGN(offset, field->align);
b6c7abd1 2387 ret = trace_define_field_ext(call, field->type, field->name,
ac343da7 2388 offset, field->size,
b6c7abd1
YS
2389 field->is_signed, field->filter_type,
2390 field->len);
ac343da7
MH
2391 if (WARN_ON_ONCE(ret)) {
2392 pr_err("error code is %d\n", ret);
2393 break;
2394 }
2395
2396 offset += field->size;
2397 }
2398 }
2399
2400 return ret;
2401}
2402
1473e441 2403static int
7f1d2f82 2404event_create_dir(struct dentry *parent, struct trace_event_file *file)
1473e441 2405{
2425bcb9 2406 struct trace_event_call *call = file->event_call;
27152bce 2407 struct eventfs_file *ef_subsystem = NULL;
ae63b31e 2408 struct trace_array *tr = file->tr;
c8414dab 2409 struct eventfs_file *ef;
de7b2973 2410 const char *name;
fd994989 2411 int ret;
1473e441 2412
6ecc2d1c
SR
2413 /*
2414 * If the trace point header did not define TRACE_SYSTEM
ee41106a
SRG
2415 * then the system would be called "TRACE_SYSTEM". This should
2416 * never happen.
6ecc2d1c 2417 */
ee41106a
SRG
2418 if (WARN_ON_ONCE(strcmp(call->class->system, TRACE_SYSTEM) == 0))
2419 return -ENODEV;
2420
27152bce
AK
2421 ef_subsystem = event_subsystem_dir(tr, call->class->system, file, parent);
2422 if (!ef_subsystem)
ee41106a 2423 return -ENOMEM;
ae63b31e 2424
687fcc4a 2425 name = trace_event_name(call);
c8414dab
JR
2426 ef = eventfs_add_dir(name, ef_subsystem);
2427 if (IS_ERR(ef)) {
8434dc93 2428 pr_warn("Could not create tracefs '%s' directory\n", name);
1473e441
SR
2429 return -1;
2430 }
2431
c8414dab
JR
2432 file->ef = ef;
2433
9b63776f 2434 if (call->class->reg && !(call->flags & TRACE_EVENT_FL_IGNORE_ENABLE))
27152bce 2435 eventfs_add_file("enable", TRACE_MODE_WRITE, file->ef, file,
620a30e9 2436 &ftrace_enable_fops);
1473e441 2437
2239291a 2438#ifdef CONFIG_PERF_EVENTS
a1d0ce82 2439 if (call->event.type && call->class->reg)
27152bce 2440 eventfs_add_file("id", TRACE_MODE_READ, file->ef,
620a30e9
ON
2441 (void *)(long)call->event.type,
2442 &ftrace_event_id_fops);
2239291a 2443#endif
23725aee 2444
ac343da7
MH
2445 ret = event_define_fields(call);
2446 if (ret < 0) {
2447 pr_warn("Could not initialize trace point events/%s\n", name);
2448 return ret;
cf027f64
TZ
2449 }
2450
854145e0
CH
2451 /*
2452 * Only event directories that can be enabled should have
5d948c86 2453 * triggers or filters.
854145e0 2454 */
5d948c86 2455 if (!(call->flags & TRACE_EVENT_FL_IGNORE_ENABLE)) {
27152bce 2456 eventfs_add_file("filter", TRACE_MODE_WRITE, file->ef,
21ccc9cd 2457 file, &ftrace_event_filter_fops);
5d948c86 2458
27152bce 2459 eventfs_add_file("trigger", TRACE_MODE_WRITE, file->ef,
21ccc9cd 2460 file, &event_trigger_fops);
5d948c86 2461 }
85f2b082 2462
7ef224d1 2463#ifdef CONFIG_HIST_TRIGGERS
27152bce 2464 eventfs_add_file("hist", TRACE_MODE_READ, file->ef, file,
7ef224d1 2465 &event_hist_fops);
2d19bd79
TZ
2466#endif
2467#ifdef CONFIG_HIST_TRIGGERS_DEBUG
27152bce 2468 eventfs_add_file("hist_debug", TRACE_MODE_READ, file->ef, file,
2d19bd79 2469 &event_hist_debug_fops);
7ef224d1 2470#endif
27152bce 2471 eventfs_add_file("format", TRACE_MODE_READ, file->ef, call,
620a30e9 2472 &ftrace_event_format_fops);
6d723736 2473
6c3edaf9
CW
2474#ifdef CONFIG_TRACE_EVENT_INJECT
2475 if (call->event.type && call->class->reg)
27152bce 2476 eventfs_add_file("inject", 0200, file->ef, file,
6c3edaf9
CW
2477 &event_inject_fops);
2478#endif
2479
6d723736
SR
2480 return 0;
2481}
2482
2425bcb9 2483static void remove_event_from_tracers(struct trace_event_call *call)
ae63b31e 2484{
7f1d2f82 2485 struct trace_event_file *file;
ae63b31e
SR
2486 struct trace_array *tr;
2487
2488 do_for_each_event_file_safe(tr, file) {
ae63b31e
SR
2489 if (file->event_call != call)
2490 continue;
2491
f6a84bdc 2492 remove_event_file_dir(file);
ae63b31e
SR
2493 /*
2494 * The do_for_each_event_file_safe() is
2495 * a double loop. After finding the call for this
2496 * trace_array, we use break to jump to the next
2497 * trace_array.
2498 */
2499 break;
2500 } while_for_each_event_file();
2501}
2502
2425bcb9 2503static void event_remove(struct trace_event_call *call)
8781915a 2504{
ae63b31e 2505 struct trace_array *tr;
7f1d2f82 2506 struct trace_event_file *file;
ae63b31e
SR
2507
2508 do_for_each_event_file(tr, file) {
2509 if (file->event_call != call)
2510 continue;
065e63f9
SRV
2511
2512 if (file->flags & EVENT_FILE_FL_WAS_ENABLED)
2513 tr->clear_trace = true;
2514
ae63b31e
SR
2515 ftrace_event_enable_disable(file, 0);
2516 /*
2517 * The do_for_each_event_file() is
2518 * a double loop. After finding the call for this
2519 * trace_array, we use break to jump to the next
2520 * trace_array.
2521 */
2522 break;
2523 } while_for_each_event_file();
2524
8781915a 2525 if (call->event.funcs)
9023c930 2526 __unregister_trace_event(&call->event);
ae63b31e 2527 remove_event_from_tracers(call);
8781915a
EG
2528 list_del(&call->list);
2529}
2530
2425bcb9 2531static int event_init(struct trace_event_call *call)
8781915a
EG
2532{
2533 int ret = 0;
de7b2973 2534 const char *name;
8781915a 2535
687fcc4a 2536 name = trace_event_name(call);
de7b2973 2537 if (WARN_ON(!name))
8781915a
EG
2538 return -EINVAL;
2539
2540 if (call->class->raw_init) {
2541 ret = call->class->raw_init(call);
2542 if (ret < 0 && ret != -ENOSYS)
3448bac3 2543 pr_warn("Could not initialize trace events/%s\n", name);
8781915a
EG
2544 }
2545
2546 return ret;
2547}
2548
67ead0a6 2549static int
2425bcb9 2550__register_event(struct trace_event_call *call, struct module *mod)
bd1a5c84 2551{
bd1a5c84 2552 int ret;
6d723736 2553
8781915a
EG
2554 ret = event_init(call);
2555 if (ret < 0)
2556 return ret;
701970b3 2557
ae63b31e 2558 list_add(&call->list, &ftrace_events);
1d18538e
SRV
2559 if (call->flags & TRACE_EVENT_FL_DYNAMIC)
2560 atomic_set(&call->refcnt, 0);
2561 else
2562 call->module = mod;
88f70d75 2563
ae63b31e 2564 return 0;
bd1a5c84
MH
2565}
2566
67ec0d85 2567static char *eval_replace(char *ptr, struct trace_eval_map *map, int len)
0c564a53
SRRH
2568{
2569 int rlen;
2570 int elen;
2571
67ec0d85 2572 /* Find the length of the eval value as a string */
00f4b652 2573 elen = snprintf(ptr, 0, "%ld", map->eval_value);
0c564a53
SRRH
2574 /* Make sure there's enough room to replace the string with the value */
2575 if (len < elen)
2576 return NULL;
2577
00f4b652 2578 snprintf(ptr, elen + 1, "%ld", map->eval_value);
0c564a53
SRRH
2579
2580 /* Get the rest of the string of ptr */
2581 rlen = strlen(ptr + len);
2582 memmove(ptr + elen, ptr + len, rlen);
2583 /* Make sure we end the new string */
2584 ptr[elen + rlen] = 0;
2585
2586 return ptr + elen;
2587}
2588
2425bcb9 2589static void update_event_printk(struct trace_event_call *call,
00f4b652 2590 struct trace_eval_map *map)
0c564a53
SRRH
2591{
2592 char *ptr;
2593 int quote = 0;
00f4b652 2594 int len = strlen(map->eval_string);
0c564a53
SRRH
2595
2596 for (ptr = call->print_fmt; *ptr; ptr++) {
2597 if (*ptr == '\\') {
2598 ptr++;
2599 /* paranoid */
2600 if (!*ptr)
2601 break;
2602 continue;
2603 }
2604 if (*ptr == '"') {
2605 quote ^= 1;
2606 continue;
2607 }
2608 if (quote)
2609 continue;
2610 if (isdigit(*ptr)) {
2611 /* skip numbers */
2612 do {
2613 ptr++;
2614 /* Check for alpha chars like ULL */
2615 } while (isalnum(*ptr));
3193899d
SRRH
2616 if (!*ptr)
2617 break;
0c564a53
SRRH
2618 /*
2619 * A number must have some kind of delimiter after
2620 * it, and we can ignore that too.
2621 */
2622 continue;
2623 }
2624 if (isalpha(*ptr) || *ptr == '_') {
00f4b652 2625 if (strncmp(map->eval_string, ptr, len) == 0 &&
0c564a53 2626 !isalnum(ptr[len]) && ptr[len] != '_') {
67ec0d85
JL
2627 ptr = eval_replace(ptr, map, len);
2628 /* enum/sizeof string smaller than value */
0c564a53
SRRH
2629 if (WARN_ON_ONCE(!ptr))
2630 return;
2631 /*
67ec0d85 2632 * No need to decrement here, as eval_replace()
0c564a53 2633 * returns the pointer to the character passed
67ec0d85 2634 * the eval, and two evals can not be placed
0c564a53
SRRH
2635 * back to back without something in between.
2636 * We can skip that something in between.
2637 */
2638 continue;
2639 }
2640 skip_more:
2641 do {
2642 ptr++;
2643 } while (isalnum(*ptr) || *ptr == '_');
3193899d
SRRH
2644 if (!*ptr)
2645 break;
0c564a53
SRRH
2646 /*
2647 * If what comes after this variable is a '.' or
2648 * '->' then we can continue to ignore that string.
2649 */
2650 if (*ptr == '.' || (ptr[0] == '-' && ptr[1] == '>')) {
2651 ptr += *ptr == '.' ? 1 : 2;
3193899d
SRRH
2652 if (!*ptr)
2653 break;
0c564a53
SRRH
2654 goto skip_more;
2655 }
2656 /*
2657 * Once again, we can skip the delimiter that came
2658 * after the string.
2659 */
2660 continue;
2661 }
2662 }
2663}
2664
795301d3
SRG
2665static void add_str_to_module(struct module *module, char *str)
2666{
2667 struct module_string *modstr;
2668
2669 modstr = kmalloc(sizeof(*modstr), GFP_KERNEL);
2670
2671 /*
2672 * If we failed to allocate memory here, then we'll just
2673 * let the str memory leak when the module is removed.
2674 * If this fails to allocate, there's worse problems than
2675 * a leaked string on module removal.
2676 */
2677 if (WARN_ON_ONCE(!modstr))
2678 return;
2679
2680 modstr->module = module;
2681 modstr->str = str;
2682
2683 list_add(&modstr->next, &module_strings);
2684}
2685
b3bc8547
SRG
2686static void update_event_fields(struct trace_event_call *call,
2687 struct trace_eval_map *map)
2688{
2689 struct ftrace_event_field *field;
2690 struct list_head *head;
2691 char *ptr;
795301d3 2692 char *str;
b3bc8547
SRG
2693 int len = strlen(map->eval_string);
2694
795301d3
SRG
2695 /* Dynamic events should never have field maps */
2696 if (WARN_ON_ONCE(call->flags & TRACE_EVENT_FL_DYNAMIC))
2697 return;
2698
b3bc8547
SRG
2699 head = trace_get_fields(call);
2700 list_for_each_entry(field, head, link) {
2701 ptr = strchr(field->type, '[');
2702 if (!ptr)
2703 continue;
2704 ptr++;
2705
2706 if (!isalpha(*ptr) && *ptr != '_')
2707 continue;
2708
2709 if (strncmp(map->eval_string, ptr, len) != 0)
2710 continue;
2711
795301d3
SRG
2712 str = kstrdup(field->type, GFP_KERNEL);
2713 if (WARN_ON_ONCE(!str))
2714 return;
2715 ptr = str + (ptr - field->type);
b3bc8547
SRG
2716 ptr = eval_replace(ptr, map, len);
2717 /* enum/sizeof string smaller than value */
795301d3
SRG
2718 if (WARN_ON_ONCE(!ptr)) {
2719 kfree(str);
2720 continue;
2721 }
2722
2723 /*
2724 * If the event is part of a module, then we need to free the string
2725 * when the module is removed. Otherwise, it will stay allocated
2726 * until a reboot.
2727 */
2728 if (call->module)
2729 add_str_to_module(call->module, str);
2730
2731 field->type = str;
b3bc8547
SRG
2732 }
2733}
2734
f57a4143 2735void trace_event_eval_update(struct trace_eval_map **map, int len)
0c564a53 2736{
2425bcb9 2737 struct trace_event_call *call, *p;
0c564a53 2738 const char *last_system = NULL;
1ebe1eaf 2739 bool first = false;
0c564a53
SRRH
2740 int last_i;
2741 int i;
2742
2743 down_write(&trace_event_sem);
2744 list_for_each_entry_safe(call, p, &ftrace_events, list) {
2745 /* events are usually grouped together with systems */
2746 if (!last_system || call->class->system != last_system) {
1ebe1eaf 2747 first = true;
0c564a53
SRRH
2748 last_i = 0;
2749 last_system = call->class->system;
2750 }
2751
1ebe1eaf 2752 /*
f2cc020d 2753 * Since calls are grouped by systems, the likelihood that the
1ebe1eaf 2754 * next call in the iteration belongs to the same system as the
2b5894cc 2755 * previous call is high. As an optimization, we skip searching
1ebe1eaf
SRV
2756 * for a map[] that matches the call's system if the last call
2757 * was from the same system. That's what last_i is for. If the
2758 * call has the same system as the previous call, then last_i
2759 * will be the index of the first map[] that has a matching
2760 * system.
2761 */
0c564a53
SRRH
2762 for (i = last_i; i < len; i++) {
2763 if (call->class->system == map[i]->system) {
2764 /* Save the first system if need be */
1ebe1eaf 2765 if (first) {
0c564a53 2766 last_i = i;
1ebe1eaf
SRV
2767 first = false;
2768 }
0c564a53 2769 update_event_printk(call, map[i]);
b3bc8547 2770 update_event_fields(call, map[i]);
0c564a53
SRRH
2771 }
2772 }
23cce5f2 2773 cond_resched();
0c564a53
SRRH
2774 }
2775 up_write(&trace_event_sem);
2776}
2777
7f1d2f82 2778static struct trace_event_file *
2425bcb9 2779trace_create_new_event(struct trace_event_call *call,
da511bf3
SRRH
2780 struct trace_array *tr)
2781{
6cb20650
SRV
2782 struct trace_pid_list *no_pid_list;
2783 struct trace_pid_list *pid_list;
7f1d2f82 2784 struct trace_event_file *file;
6cb20650 2785 unsigned int first;
da511bf3
SRRH
2786
2787 file = kmem_cache_alloc(file_cachep, GFP_TRACE);
2788 if (!file)
2789 return NULL;
2790
6cb20650
SRV
2791 pid_list = rcu_dereference_protected(tr->filtered_pids,
2792 lockdep_is_held(&event_mutex));
2793 no_pid_list = rcu_dereference_protected(tr->filtered_no_pids,
2794 lockdep_is_held(&event_mutex));
2795
2796 if (!trace_pid_list_first(pid_list, &first) ||
27ff768f 2797 !trace_pid_list_first(no_pid_list, &first))
6cb20650
SRV
2798 file->flags |= EVENT_FILE_FL_PID_FILTER;
2799
da511bf3
SRRH
2800 file->event_call = call;
2801 file->tr = tr;
2802 atomic_set(&file->sm_ref, 0);
85f2b082
TZ
2803 atomic_set(&file->tm_ref, 0);
2804 INIT_LIST_HEAD(&file->triggers);
da511bf3
SRRH
2805 list_add(&file->list, &tr->events);
2806
2807 return file;
2808}
2809
a01fdc89
SRG
2810#define MAX_BOOT_TRIGGERS 32
2811
2812static struct boot_triggers {
2813 const char *event;
2814 char *trigger;
2815} bootup_triggers[MAX_BOOT_TRIGGERS];
2816
2817static char bootup_trigger_buf[COMMAND_LINE_SIZE];
2818static int nr_boot_triggers;
2819
2820static __init int setup_trace_triggers(char *str)
2821{
2822 char *trigger;
2823 char *buf;
2824 int i;
2825
c7dce4c5 2826 strscpy(bootup_trigger_buf, str, COMMAND_LINE_SIZE);
a01fdc89
SRG
2827 ring_buffer_expanded = true;
2828 disable_tracing_selftest("running event triggers");
2829
2830 buf = bootup_trigger_buf;
2831 for (i = 0; i < MAX_BOOT_TRIGGERS; i++) {
2832 trigger = strsep(&buf, ",");
2833 if (!trigger)
2834 break;
2835 bootup_triggers[i].event = strsep(&trigger, ".");
e6745a4d 2836 bootup_triggers[i].trigger = trigger;
a01fdc89
SRG
2837 if (!bootup_triggers[i].trigger)
2838 break;
2839 }
2840
2841 nr_boot_triggers = i;
2842 return 1;
2843}
2844__setup("trace_trigger=", setup_trace_triggers);
a01fdc89 2845
ae63b31e
SR
2846/* Add an event to a trace directory */
2847static int
2425bcb9 2848__trace_add_new_event(struct trace_event_call *call, struct trace_array *tr)
ae63b31e 2849{
7f1d2f82 2850 struct trace_event_file *file;
ae63b31e 2851
da511bf3 2852 file = trace_create_new_event(call, tr);
ae63b31e
SR
2853 if (!file)
2854 return -ENOMEM;
2855
a838deab
MH
2856 if (eventdir_initialized)
2857 return event_create_dir(tr->event_dir, file);
2858 else
2859 return event_define_fields(call);
ae63b31e
SR
2860}
2861
a01fdc89
SRG
2862static void trace_early_triggers(struct trace_event_file *file, const char *name)
2863{
2864 int ret;
2865 int i;
2866
2867 for (i = 0; i < nr_boot_triggers; i++) {
2868 if (strcmp(name, bootup_triggers[i].event))
2869 continue;
2870 mutex_lock(&event_mutex);
2871 ret = trigger_process_regex(file, bootup_triggers[i].trigger);
2872 mutex_unlock(&event_mutex);
2873 if (ret)
2874 pr_err("Failed to register trigger '%s' on event %s\n",
2875 bootup_triggers[i].trigger,
2876 bootup_triggers[i].event);
2877 }
2878}
a01fdc89 2879
77248221 2880/*
f2cc020d 2881 * Just create a descriptor for early init. A descriptor is required
77248221
SR
2882 * for enabling events at boot. We want to enable events before
2883 * the filesystem is initialized.
2884 */
ce66f613 2885static int
2425bcb9 2886__trace_early_add_new_event(struct trace_event_call *call,
77248221
SR
2887 struct trace_array *tr)
2888{
7f1d2f82 2889 struct trace_event_file *file;
a01fdc89 2890 int ret;
77248221 2891
da511bf3 2892 file = trace_create_new_event(call, tr);
77248221
SR
2893 if (!file)
2894 return -ENOMEM;
2895
a01fdc89
SRG
2896 ret = event_define_fields(call);
2897 if (ret)
2898 return ret;
2899
2900 trace_early_triggers(file, trace_event_name(call));
2901
2902 return 0;
77248221
SR
2903}
2904
ae63b31e 2905struct ftrace_module_file_ops;
2425bcb9 2906static void __add_event_to_tracers(struct trace_event_call *call);
ae63b31e 2907
7e1413ed
SRV
2908/* Add an additional event_call dynamically */
2909int trace_add_event_call(struct trace_event_call *call)
bd1a5c84
MH
2910{
2911 int ret;
fc800a10
MH
2912 lockdep_assert_held(&event_mutex);
2913
12ecef0c 2914 mutex_lock(&trace_types_lock);
701970b3 2915
ae63b31e
SR
2916 ret = __register_event(call, NULL);
2917 if (ret >= 0)
779c5e37 2918 __add_event_to_tracers(call);
a2ca5e03 2919
a8227415 2920 mutex_unlock(&trace_types_lock);
fc800a10
MH
2921 return ret;
2922}
8bcd0663 2923EXPORT_SYMBOL_GPL(trace_add_event_call);
fc800a10 2924
4fead8e4 2925/*
a8227415
AL
2926 * Must be called under locking of trace_types_lock, event_mutex and
2927 * trace_event_sem.
4fead8e4 2928 */
2425bcb9 2929static void __trace_remove_event_call(struct trace_event_call *call)
bd1a5c84 2930{
8781915a 2931 event_remove(call);
bd1a5c84 2932 trace_destroy_fields(call);
57375747
ON
2933 free_event_filter(call->filter);
2934 call->filter = NULL;
bd1a5c84
MH
2935}
2936
2425bcb9 2937static int probe_remove_event_call(struct trace_event_call *call)
2816c551
ON
2938{
2939 struct trace_array *tr;
7f1d2f82 2940 struct trace_event_file *file;
2816c551
ON
2941
2942#ifdef CONFIG_PERF_EVENTS
2943 if (call->perf_refcount)
2944 return -EBUSY;
2945#endif
2946 do_for_each_event_file(tr, file) {
2947 if (file->event_call != call)
2948 continue;
2949 /*
2950 * We can't rely on ftrace_event_enable_disable(enable => 0)
5d6ad960 2951 * we are going to do, EVENT_FILE_FL_SOFT_MODE can suppress
2816c551
ON
2952 * TRACE_REG_UNREGISTER.
2953 */
5d6ad960 2954 if (file->flags & EVENT_FILE_FL_ENABLED)
4313e5a6
SRG
2955 goto busy;
2956
2957 if (file->flags & EVENT_FILE_FL_WAS_ENABLED)
2958 tr->clear_trace = true;
2ba64035
SRRH
2959 /*
2960 * The do_for_each_event_file_safe() is
2961 * a double loop. After finding the call for this
2962 * trace_array, we use break to jump to the next
2963 * trace_array.
2964 */
2816c551
ON
2965 break;
2966 } while_for_each_event_file();
2967
2968 __trace_remove_event_call(call);
2969
2970 return 0;
4313e5a6
SRG
2971 busy:
2972 /* No need to clear the trace now */
2973 list_for_each_entry(tr, &ftrace_trace_arrays, list) {
2974 tr->clear_trace = false;
2975 }
2976 return -EBUSY;
2816c551
ON
2977}
2978
7e1413ed
SRV
2979/* Remove an event_call */
2980int trace_remove_event_call(struct trace_event_call *call)
bd1a5c84 2981{
2816c551
ON
2982 int ret;
2983
fc800a10
MH
2984 lockdep_assert_held(&event_mutex);
2985
12ecef0c 2986 mutex_lock(&trace_types_lock);
52f6ad6d 2987 down_write(&trace_event_sem);
2816c551 2988 ret = probe_remove_event_call(call);
52f6ad6d 2989 up_write(&trace_event_sem);
a8227415 2990 mutex_unlock(&trace_types_lock);
fc800a10
MH
2991
2992 return ret;
2993}
8bcd0663 2994EXPORT_SYMBOL_GPL(trace_remove_event_call);
fc800a10 2995
bd1a5c84
MH
2996#define for_each_event(event, start, end) \
2997 for (event = start; \
2998 (unsigned long)event < (unsigned long)end; \
2999 event++)
3000
3001#ifdef CONFIG_MODULES
3002
6d723736
SR
3003static void trace_module_add_events(struct module *mod)
3004{
2425bcb9 3005 struct trace_event_call **call, **start, **end;
6d723736 3006
45ab2813
SRRH
3007 if (!mod->num_trace_events)
3008 return;
3009
3010 /* Don't add infrastructure for mods without tracepoints */
3011 if (trace_module_has_bad_taint(mod)) {
3012 pr_err("%s: module has bad taint, not creating trace events\n",
3013 mod->name);
3014 return;
3015 }
3016
6d723736
SR
3017 start = mod->trace_events;
3018 end = mod->trace_events + mod->num_trace_events;
3019
6d723736 3020 for_each_event(call, start, end) {
ae63b31e 3021 __register_event(*call, mod);
779c5e37 3022 __add_event_to_tracers(*call);
6d723736
SR
3023 }
3024}
3025
3026static void trace_module_remove_events(struct module *mod)
3027{
2425bcb9 3028 struct trace_event_call *call, *p;
795301d3 3029 struct module_string *modstr, *m;
6d723736 3030
52f6ad6d 3031 down_write(&trace_event_sem);
6d723736 3032 list_for_each_entry_safe(call, p, &ftrace_events, list) {
1d18538e
SRV
3033 if ((call->flags & TRACE_EVENT_FL_DYNAMIC) || !call->module)
3034 continue;
3035 if (call->module == mod)
bd1a5c84 3036 __trace_remove_event_call(call);
6d723736 3037 }
795301d3
SRG
3038 /* Check for any strings allocade for this module */
3039 list_for_each_entry_safe(modstr, m, &module_strings, next) {
3040 if (modstr->module != mod)
3041 continue;
3042 list_del(&modstr->next);
3043 kfree(modstr->str);
3044 kfree(modstr);
3045 }
52f6ad6d 3046 up_write(&trace_event_sem);
9456f0fa
SR
3047
3048 /*
3049 * It is safest to reset the ring buffer if the module being unloaded
873c642f
SRRH
3050 * registered any events that were used. The only worry is if
3051 * a new module gets loaded, and takes on the same id as the events
3052 * of this module. When printing out the buffer, traced events left
3053 * over from this module may be passed to the new module events and
3054 * unexpected results may occur.
9456f0fa 3055 */
e18eb878 3056 tracing_reset_all_online_cpus_unlocked();
6d723736
SR
3057}
3058
61f919a1
SR
3059static int trace_module_notify(struct notifier_block *self,
3060 unsigned long val, void *data)
6d723736
SR
3061{
3062 struct module *mod = data;
3063
3064 mutex_lock(&event_mutex);
12ecef0c 3065 mutex_lock(&trace_types_lock);
6d723736
SR
3066 switch (val) {
3067 case MODULE_STATE_COMING:
3068 trace_module_add_events(mod);
3069 break;
3070 case MODULE_STATE_GOING:
3071 trace_module_remove_events(mod);
3072 break;
3073 }
a8227415 3074 mutex_unlock(&trace_types_lock);
12ecef0c 3075 mutex_unlock(&event_mutex);
fd994989 3076
0340a6b7 3077 return NOTIFY_OK;
1473e441 3078}
315326c1 3079
836d481e
ON
3080static struct notifier_block trace_module_nb = {
3081 .notifier_call = trace_module_notify,
3673b8e4 3082 .priority = 1, /* higher than trace.c module notify */
836d481e 3083};
61f919a1 3084#endif /* CONFIG_MODULES */
1473e441 3085
ae63b31e
SR
3086/* Create a new event directory structure for a trace directory. */
3087static void
3088__trace_add_event_dirs(struct trace_array *tr)
3089{
2425bcb9 3090 struct trace_event_call *call;
ae63b31e
SR
3091 int ret;
3092
3093 list_for_each_entry(call, &ftrace_events, list) {
620a30e9 3094 ret = __trace_add_new_event(call, tr);
ae63b31e 3095 if (ret < 0)
3448bac3 3096 pr_warn("Could not create directory for event %s\n",
687fcc4a 3097 trace_event_name(call));
ae63b31e
SR
3098 }
3099}
3100
3c96529c 3101/* Returns any file that matches the system and event */
7f1d2f82 3102struct trace_event_file *
3c96529c 3103__find_event_file(struct trace_array *tr, const char *system, const char *event)
3cd715de 3104{
7f1d2f82 3105 struct trace_event_file *file;
2425bcb9 3106 struct trace_event_call *call;
de7b2973 3107 const char *name;
3cd715de
SRRH
3108
3109 list_for_each_entry(file, &tr->events, list) {
3110
3111 call = file->event_call;
687fcc4a 3112 name = trace_event_name(call);
3cd715de 3113
3c96529c 3114 if (!name || !call->class)
3cd715de
SRRH
3115 continue;
3116
de7b2973 3117 if (strcmp(event, name) == 0 &&
3cd715de
SRRH
3118 strcmp(system, call->class->system) == 0)
3119 return file;
3120 }
3121 return NULL;
3122}
3123
3c96529c
SRV
3124/* Returns valid trace event files that match system and event */
3125struct trace_event_file *
3126find_event_file(struct trace_array *tr, const char *system, const char *event)
3127{
3128 struct trace_event_file *file;
3129
3130 file = __find_event_file(tr, system, event);
3131 if (!file || !file->event_call->class->reg ||
3132 file->event_call->flags & TRACE_EVENT_FL_IGNORE_ENABLE)
3133 return NULL;
3134
3135 return file;
3136}
3137
e3e2a2cc
TZ
3138/**
3139 * trace_get_event_file - Find and return a trace event file
3140 * @instance: The name of the trace instance containing the event
3141 * @system: The name of the system containing the event
3142 * @event: The name of the event
3143 *
3144 * Return a trace event file given the trace instance name, trace
3145 * system, and trace event name. If the instance name is NULL, it
3146 * refers to the top-level trace array.
3147 *
3148 * This function will look it up and return it if found, after calling
3149 * trace_array_get() to prevent the instance from going away, and
3150 * increment the event's module refcount to prevent it from being
3151 * removed.
3152 *
3153 * To release the file, call trace_put_event_file(), which will call
3154 * trace_array_put() and decrement the event's module refcount.
3155 *
3156 * Return: The trace event on success, ERR_PTR otherwise.
3157 */
3158struct trace_event_file *trace_get_event_file(const char *instance,
3159 const char *system,
3160 const char *event)
3161{
3162 struct trace_array *tr = top_trace_array();
3163 struct trace_event_file *file = NULL;
3164 int ret = -EINVAL;
3165
3166 if (instance) {
3167 tr = trace_array_find_get(instance);
3168 if (!tr)
3169 return ERR_PTR(-ENOENT);
3170 } else {
3171 ret = trace_array_get(tr);
3172 if (ret)
3173 return ERR_PTR(ret);
3174 }
3175
3176 mutex_lock(&event_mutex);
3177
3178 file = find_event_file(tr, system, event);
3179 if (!file) {
3180 trace_array_put(tr);
3181 ret = -EINVAL;
3182 goto out;
3183 }
3184
3185 /* Don't let event modules unload while in use */
1d18538e 3186 ret = trace_event_try_get_ref(file->event_call);
e3e2a2cc
TZ
3187 if (!ret) {
3188 trace_array_put(tr);
3189 ret = -EBUSY;
3190 goto out;
3191 }
3192
3193 ret = 0;
3194 out:
3195 mutex_unlock(&event_mutex);
3196
3197 if (ret)
3198 file = ERR_PTR(ret);
3199
3200 return file;
3201}
3202EXPORT_SYMBOL_GPL(trace_get_event_file);
3203
3204/**
3205 * trace_put_event_file - Release a file from trace_get_event_file()
3206 * @file: The trace event file
3207 *
3208 * If a file was retrieved using trace_get_event_file(), this should
3209 * be called when it's no longer needed. It will cancel the previous
3210 * trace_array_get() called by that function, and decrement the
3211 * event's module refcount.
3212 */
3213void trace_put_event_file(struct trace_event_file *file)
3214{
3215 mutex_lock(&event_mutex);
1d18538e 3216 trace_event_put_ref(file->event_call);
e3e2a2cc
TZ
3217 mutex_unlock(&event_mutex);
3218
3219 trace_array_put(file->tr);
3220}
3221EXPORT_SYMBOL_GPL(trace_put_event_file);
3222
2875a08b
SRRH
3223#ifdef CONFIG_DYNAMIC_FTRACE
3224
3225/* Avoid typos */
3226#define ENABLE_EVENT_STR "enable_event"
3227#define DISABLE_EVENT_STR "disable_event"
3228
3229struct event_probe_data {
7f1d2f82 3230 struct trace_event_file *file;
2875a08b
SRRH
3231 unsigned long count;
3232 int ref;
3233 bool enable;
3234};
3235
41794f19
SRV
3236static void update_event_probe(struct event_probe_data *data)
3237{
3238 if (data->enable)
3239 clear_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &data->file->flags);
3240 else
3241 set_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &data->file->flags);
3242}
3243
3cd715de 3244static void
bca6c8d0 3245event_enable_probe(unsigned long ip, unsigned long parent_ip,
b5f081b5 3246 struct trace_array *tr, struct ftrace_probe_ops *ops,
6e444319 3247 void *data)
3cd715de 3248{
6e444319
SRV
3249 struct ftrace_func_mapper *mapper = data;
3250 struct event_probe_data *edata;
41794f19 3251 void **pdata;
3cd715de 3252
41794f19
SRV
3253 pdata = ftrace_func_mapper_find_ip(mapper, ip);
3254 if (!pdata || !*pdata)
3cd715de
SRRH
3255 return;
3256
6e444319
SRV
3257 edata = *pdata;
3258 update_event_probe(edata);
3cd715de
SRRH
3259}
3260
3261static void
bca6c8d0 3262event_enable_count_probe(unsigned long ip, unsigned long parent_ip,
b5f081b5 3263 struct trace_array *tr, struct ftrace_probe_ops *ops,
6e444319 3264 void *data)
3cd715de 3265{
6e444319
SRV
3266 struct ftrace_func_mapper *mapper = data;
3267 struct event_probe_data *edata;
41794f19 3268 void **pdata;
3cd715de 3269
41794f19
SRV
3270 pdata = ftrace_func_mapper_find_ip(mapper, ip);
3271 if (!pdata || !*pdata)
3cd715de
SRRH
3272 return;
3273
6e444319 3274 edata = *pdata;
41794f19 3275
6e444319 3276 if (!edata->count)
3cd715de
SRRH
3277 return;
3278
3279 /* Skip if the event is in a state we want to switch to */
6e444319 3280 if (edata->enable == !(edata->file->flags & EVENT_FILE_FL_SOFT_DISABLED))
3cd715de
SRRH
3281 return;
3282
6e444319
SRV
3283 if (edata->count != -1)
3284 (edata->count)--;
3cd715de 3285
6e444319 3286 update_event_probe(edata);
3cd715de
SRRH
3287}
3288
3289static int
3290event_enable_print(struct seq_file *m, unsigned long ip,
6e444319 3291 struct ftrace_probe_ops *ops, void *data)
3cd715de 3292{
6e444319
SRV
3293 struct ftrace_func_mapper *mapper = data;
3294 struct event_probe_data *edata;
41794f19
SRV
3295 void **pdata;
3296
3297 pdata = ftrace_func_mapper_find_ip(mapper, ip);
3298
3299 if (WARN_ON_ONCE(!pdata || !*pdata))
3300 return 0;
3301
6e444319 3302 edata = *pdata;
3cd715de
SRRH
3303
3304 seq_printf(m, "%ps:", (void *)ip);
3305
3306 seq_printf(m, "%s:%s:%s",
6e444319
SRV
3307 edata->enable ? ENABLE_EVENT_STR : DISABLE_EVENT_STR,
3308 edata->file->event_call->class->system,
3309 trace_event_name(edata->file->event_call));
3cd715de 3310
6e444319 3311 if (edata->count == -1)
fa6f0cc7 3312 seq_puts(m, ":unlimited\n");
3cd715de 3313 else
6e444319 3314 seq_printf(m, ":count=%ld\n", edata->count);
3cd715de
SRRH
3315
3316 return 0;
3317}
3318
3319static int
b5f081b5 3320event_enable_init(struct ftrace_probe_ops *ops, struct trace_array *tr,
6e444319 3321 unsigned long ip, void *init_data, void **data)
3cd715de 3322{
6e444319
SRV
3323 struct ftrace_func_mapper *mapper = *data;
3324 struct event_probe_data *edata = init_data;
41794f19
SRV
3325 int ret;
3326
6e444319
SRV
3327 if (!mapper) {
3328 mapper = allocate_ftrace_func_mapper();
3329 if (!mapper)
3330 return -ENODEV;
3331 *data = mapper;
3332 }
3333
3334 ret = ftrace_func_mapper_add_ip(mapper, ip, edata);
41794f19
SRV
3335 if (ret < 0)
3336 return ret;
3cd715de 3337
6e444319
SRV
3338 edata->ref++;
3339
3340 return 0;
3341}
3342
3343static int free_probe_data(void *data)
3344{
3345 struct event_probe_data *edata = data;
41794f19 3346
6e444319
SRV
3347 edata->ref--;
3348 if (!edata->ref) {
3349 /* Remove the SOFT_MODE flag */
3350 __ftrace_event_enable_disable(edata->file, 0, 1);
1d18538e 3351 trace_event_put_ref(edata->file->event_call);
6e444319
SRV
3352 kfree(edata);
3353 }
3cd715de
SRRH
3354 return 0;
3355}
3356
3357static void
b5f081b5 3358event_enable_free(struct ftrace_probe_ops *ops, struct trace_array *tr,
6e444319 3359 unsigned long ip, void *data)
3cd715de 3360{
6e444319
SRV
3361 struct ftrace_func_mapper *mapper = data;
3362 struct event_probe_data *edata;
3363
3364 if (!ip) {
3365 if (!mapper)
3366 return;
3367 free_ftrace_func_mapper(mapper, free_probe_data);
3368 return;
3369 }
41794f19 3370
6e444319 3371 edata = ftrace_func_mapper_remove_ip(mapper, ip);
41794f19 3372
6e444319 3373 if (WARN_ON_ONCE(!edata))
41794f19 3374 return;
3cd715de 3375
6e444319 3376 if (WARN_ON_ONCE(edata->ref <= 0))
3cd715de
SRRH
3377 return;
3378
6e444319 3379 free_probe_data(edata);
3cd715de
SRRH
3380}
3381
3382static struct ftrace_probe_ops event_enable_probe_ops = {
3383 .func = event_enable_probe,
3384 .print = event_enable_print,
3385 .init = event_enable_init,
3386 .free = event_enable_free,
3387};
3388
3389static struct ftrace_probe_ops event_enable_count_probe_ops = {
3390 .func = event_enable_count_probe,
3391 .print = event_enable_print,
3392 .init = event_enable_init,
3393 .free = event_enable_free,
3394};
3395
3396static struct ftrace_probe_ops event_disable_probe_ops = {
3397 .func = event_enable_probe,
3398 .print = event_enable_print,
3399 .init = event_enable_init,
3400 .free = event_enable_free,
3401};
3402
3403static struct ftrace_probe_ops event_disable_count_probe_ops = {
3404 .func = event_enable_count_probe,
3405 .print = event_enable_print,
3406 .init = event_enable_init,
3407 .free = event_enable_free,
3408};
3409
3410static int
04ec7bb6 3411event_enable_func(struct trace_array *tr, struct ftrace_hash *hash,
3cd715de
SRRH
3412 char *glob, char *cmd, char *param, int enabled)
3413{
7f1d2f82 3414 struct trace_event_file *file;
3cd715de
SRRH
3415 struct ftrace_probe_ops *ops;
3416 struct event_probe_data *data;
3417 const char *system;
3418 const char *event;
3419 char *number;
3420 bool enable;
3421 int ret;
3422
dc81e5e3
YY
3423 if (!tr)
3424 return -ENODEV;
3425
3cd715de 3426 /* hash funcs only work with set_ftrace_filter */
8092e808 3427 if (!enabled || !param)
3cd715de
SRRH
3428 return -EINVAL;
3429
3430 system = strsep(&param, ":");
3431 if (!param)
3432 return -EINVAL;
3433
3434 event = strsep(&param, ":");
3435
3436 mutex_lock(&event_mutex);
3437
3438 ret = -EINVAL;
3439 file = find_event_file(tr, system, event);
3440 if (!file)
3441 goto out;
3442
3443 enable = strcmp(cmd, ENABLE_EVENT_STR) == 0;
3444
3445 if (enable)
3446 ops = param ? &event_enable_count_probe_ops : &event_enable_probe_ops;
3447 else
3448 ops = param ? &event_disable_count_probe_ops : &event_disable_probe_ops;
3449
3450 if (glob[0] == '!') {
7b60f3d8 3451 ret = unregister_ftrace_function_probe_func(glob+1, tr, ops);
3cd715de
SRRH
3452 goto out;
3453 }
3454
3455 ret = -ENOMEM;
41794f19 3456
3cd715de
SRRH
3457 data = kzalloc(sizeof(*data), GFP_KERNEL);
3458 if (!data)
3459 goto out;
3460
3461 data->enable = enable;
3462 data->count = -1;
3463 data->file = file;
3464
3465 if (!param)
3466 goto out_reg;
3467
3468 number = strsep(&param, ":");
3469
3470 ret = -EINVAL;
3471 if (!strlen(number))
3472 goto out_free;
3473
3474 /*
3475 * We use the callback data field (which is a pointer)
3476 * as our counter.
3477 */
3478 ret = kstrtoul(number, 0, &data->count);
3479 if (ret)
3480 goto out_free;
3481
3482 out_reg:
3483 /* Don't let event modules unload while probe registered */
1d18538e 3484 ret = trace_event_try_get_ref(file->event_call);
6ed01066
MH
3485 if (!ret) {
3486 ret = -EBUSY;
3cd715de 3487 goto out_free;
6ed01066 3488 }
3cd715de
SRRH
3489
3490 ret = __ftrace_event_enable_disable(file, 1, 1);
3491 if (ret < 0)
3492 goto out_put;
41794f19 3493
04ec7bb6 3494 ret = register_ftrace_function_probe(glob, tr, ops, data);
ff305ded
SRRH
3495 /*
3496 * The above returns on success the # of functions enabled,
3497 * but if it didn't find any functions it returns zero.
3498 * Consider no functions a failure too.
3499 */
a5b85bd1
MH
3500 if (!ret) {
3501 ret = -ENOENT;
3cd715de 3502 goto out_disable;
ff305ded
SRRH
3503 } else if (ret < 0)
3504 goto out_disable;
3505 /* Just return zero, not the number of enabled functions */
3506 ret = 0;
3cd715de
SRRH
3507 out:
3508 mutex_unlock(&event_mutex);
3509 return ret;
3510
3511 out_disable:
3512 __ftrace_event_enable_disable(file, 0, 1);
3513 out_put:
1d18538e 3514 trace_event_put_ref(file->event_call);
3cd715de
SRRH
3515 out_free:
3516 kfree(data);
3517 goto out;
3518}
3519
3520static struct ftrace_func_command event_enable_cmd = {
3521 .name = ENABLE_EVENT_STR,
3522 .func = event_enable_func,
3523};
3524
3525static struct ftrace_func_command event_disable_cmd = {
3526 .name = DISABLE_EVENT_STR,
3527 .func = event_enable_func,
3528};
3529
3530static __init int register_event_cmds(void)
3531{
3532 int ret;
3533
3534 ret = register_ftrace_command(&event_enable_cmd);
3535 if (WARN_ON(ret < 0))
3536 return ret;
3537 ret = register_ftrace_command(&event_disable_cmd);
3538 if (WARN_ON(ret < 0))
3539 unregister_ftrace_command(&event_enable_cmd);
3540 return ret;
3541}
3542#else
3543static inline int register_event_cmds(void) { return 0; }
3544#endif /* CONFIG_DYNAMIC_FTRACE */
3545
77248221 3546/*
720dee53
MH
3547 * The top level array and trace arrays created by boot-time tracing
3548 * have already had its trace_event_file descriptors created in order
3549 * to allow for early events to be recorded.
3550 * This function is called after the tracefs has been initialized,
3551 * and we now have to create the files associated to the events.
77248221 3552 */
720dee53 3553static void __trace_early_add_event_dirs(struct trace_array *tr)
77248221 3554{
7f1d2f82 3555 struct trace_event_file *file;
77248221
SR
3556 int ret;
3557
3558
3559 list_for_each_entry(file, &tr->events, list) {
620a30e9 3560 ret = event_create_dir(tr->event_dir, file);
77248221 3561 if (ret < 0)
3448bac3 3562 pr_warn("Could not create directory for event %s\n",
687fcc4a 3563 trace_event_name(file->event_call));
77248221
SR
3564 }
3565}
3566
3567/*
720dee53
MH
3568 * For early boot up, the top trace array and the trace arrays created
3569 * by boot-time tracing require to have a list of events that can be
3570 * enabled. This must be done before the filesystem is set up in order
3571 * to allow events to be traced early.
77248221 3572 */
720dee53 3573void __trace_early_add_events(struct trace_array *tr)
77248221 3574{
2425bcb9 3575 struct trace_event_call *call;
77248221
SR
3576 int ret;
3577
3578 list_for_each_entry(call, &ftrace_events, list) {
3579 /* Early boot up should not have any modules loaded */
1d18538e
SRV
3580 if (!(call->flags & TRACE_EVENT_FL_DYNAMIC) &&
3581 WARN_ON_ONCE(call->module))
77248221
SR
3582 continue;
3583
3584 ret = __trace_early_add_new_event(call, tr);
3585 if (ret < 0)
3448bac3 3586 pr_warn("Could not create early event %s\n",
687fcc4a 3587 trace_event_name(call));
77248221
SR
3588 }
3589}
3590
0c8916c3
SR
3591/* Remove the event directory structure for a trace directory. */
3592static void
3593__trace_remove_event_dirs(struct trace_array *tr)
3594{
7f1d2f82 3595 struct trace_event_file *file, *next;
0c8916c3 3596
f6a84bdc
ON
3597 list_for_each_entry_safe(file, next, &tr->events, list)
3598 remove_event_file_dir(file);
0c8916c3
SR
3599}
3600
2425bcb9 3601static void __add_event_to_tracers(struct trace_event_call *call)
ae63b31e
SR
3602{
3603 struct trace_array *tr;
3604
620a30e9
ON
3605 list_for_each_entry(tr, &ftrace_trace_arrays, list)
3606 __trace_add_new_event(call, tr);
ae63b31e
SR
3607}
3608
2425bcb9
SRRH
3609extern struct trace_event_call *__start_ftrace_events[];
3610extern struct trace_event_call *__stop_ftrace_events[];
a59fd602 3611
020e5f85
LZ
3612static char bootup_event_buf[COMMAND_LINE_SIZE] __initdata;
3613
3614static __init int setup_trace_event(char *str)
3615{
c7dce4c5 3616 strscpy(bootup_event_buf, str, COMMAND_LINE_SIZE);
55034cd6 3617 ring_buffer_expanded = true;
60efe21e 3618 disable_tracing_selftest("running event tracing");
020e5f85
LZ
3619
3620 return 1;
3621}
3622__setup("trace_event=", setup_trace_event);
3623
77248221
SR
3624/* Expects to have event_mutex held when called */
3625static int
3626create_event_toplevel_files(struct dentry *parent, struct trace_array *tr)
ae63b31e
SR
3627{
3628 struct dentry *d_events;
3629 struct dentry *entry;
27152bce 3630 int error = 0;
ae63b31e 3631
e4931b82
YW
3632 entry = trace_create_file("set_event", TRACE_MODE_WRITE, parent,
3633 tr, &ftrace_set_event_fops);
3634 if (!entry)
ae63b31e 3635 return -ENOMEM;
ae63b31e 3636
27152bce
AK
3637 d_events = eventfs_create_events_dir("events", parent);
3638 if (IS_ERR(d_events)) {
8434dc93 3639 pr_warn("Could not create tracefs 'events' directory\n");
277ba044
SR
3640 return -ENOMEM;
3641 }
ae63b31e 3642
27152bce 3643 error = eventfs_add_events_file("enable", TRACE_MODE_WRITE, d_events,
7d436400 3644 tr, &ftrace_tr_enable_fops);
27152bce 3645 if (error)
7d436400 3646 return -ENOMEM;
7d436400
SRRH
3647
3648 /* There are not as crucial, just warn if they are not created */
3649
e4931b82
YW
3650 trace_create_file("set_event_pid", TRACE_MODE_WRITE, parent,
3651 tr, &ftrace_set_event_pid_fops);
49090107 3652
e4931b82
YW
3653 trace_create_file("set_event_notrace_pid",
3654 TRACE_MODE_WRITE, parent, tr,
3655 &ftrace_set_event_notrace_pid_fops);
27683626 3656
ae63b31e 3657 /* ring buffer internal formats */
27152bce 3658 eventfs_add_events_file("header_page", TRACE_MODE_READ, d_events,
7d436400
SRRH
3659 ring_buffer_print_page_header,
3660 &ftrace_show_header_fops);
7d436400 3661
27152bce 3662 eventfs_add_events_file("header_event", TRACE_MODE_READ, d_events,
7d436400
SRRH
3663 ring_buffer_print_entry_header,
3664 &ftrace_show_header_fops);
ae63b31e
SR
3665
3666 tr->event_dir = d_events;
77248221
SR
3667
3668 return 0;
3669}
3670
3671/**
3672 * event_trace_add_tracer - add a instance of a trace_array to events
3673 * @parent: The parent dentry to place the files/directories for events in
3674 * @tr: The trace array associated with these events
3675 *
3676 * When a new instance is created, it needs to set up its events
3677 * directory, as well as other files associated with events. It also
2b5894cc 3678 * creates the event hierarchy in the @parent/events directory.
77248221
SR
3679 *
3680 * Returns 0 on success.
12ecef0c
SRV
3681 *
3682 * Must be called with event_mutex held.
77248221
SR
3683 */
3684int event_trace_add_tracer(struct dentry *parent, struct trace_array *tr)
3685{
3686 int ret;
3687
12ecef0c 3688 lockdep_assert_held(&event_mutex);
77248221
SR
3689
3690 ret = create_event_toplevel_files(parent, tr);
3691 if (ret)
12ecef0c 3692 goto out;
77248221 3693
52f6ad6d 3694 down_write(&trace_event_sem);
720dee53
MH
3695 /* If tr already has the event list, it is initialized in early boot. */
3696 if (unlikely(!list_empty(&tr->events)))
3697 __trace_early_add_event_dirs(tr);
3698 else
3699 __trace_add_event_dirs(tr);
52f6ad6d 3700 up_write(&trace_event_sem);
277ba044 3701
12ecef0c 3702 out:
77248221
SR
3703 return ret;
3704}
3705
3706/*
3707 * The top trace array already had its file descriptors created.
3708 * Now the files themselves need to be created.
3709 */
3710static __init int
3711early_event_add_tracer(struct dentry *parent, struct trace_array *tr)
3712{
3713 int ret;
3714
3715 mutex_lock(&event_mutex);
3716
3717 ret = create_event_toplevel_files(parent, tr);
3718 if (ret)
3719 goto out_unlock;
3720
52f6ad6d 3721 down_write(&trace_event_sem);
77248221 3722 __trace_early_add_event_dirs(tr);
52f6ad6d 3723 up_write(&trace_event_sem);
77248221
SR
3724
3725 out_unlock:
3726 mutex_unlock(&event_mutex);
3727
3728 return ret;
ae63b31e
SR
3729}
3730
12ecef0c 3731/* Must be called with event_mutex held */
0c8916c3
SR
3732int event_trace_del_tracer(struct trace_array *tr)
3733{
12ecef0c 3734 lockdep_assert_held(&event_mutex);
0c8916c3 3735
85f2b082
TZ
3736 /* Disable any event triggers and associated soft-disabled events */
3737 clear_event_triggers(tr);
3738
49090107 3739 /* Clear the pid list */
27683626 3740 __ftrace_clear_event_pids(tr, TRACE_PIDS | TRACE_NO_PIDS);
49090107 3741
2a6c24af
SRRH
3742 /* Disable any running events */
3743 __ftrace_set_clr_event_nolock(tr, NULL, NULL, NULL, 0);
3744
e0a568dc
SRV
3745 /* Make sure no more events are being executed */
3746 tracepoint_synchronize_unregister();
3ccb0123 3747
52f6ad6d 3748 down_write(&trace_event_sem);
0c8916c3 3749 __trace_remove_event_dirs(tr);
27152bce 3750 eventfs_remove_events_dir(tr->event_dir);
52f6ad6d 3751 up_write(&trace_event_sem);
0c8916c3
SR
3752
3753 tr->event_dir = NULL;
3754
0c8916c3
SR
3755 return 0;
3756}
3757
d1a29143
SR
3758static __init int event_trace_memsetup(void)
3759{
3760 field_cachep = KMEM_CACHE(ftrace_event_field, SLAB_PANIC);
7f1d2f82 3761 file_cachep = KMEM_CACHE(trace_event_file, SLAB_PANIC);
d1a29143
SR
3762 return 0;
3763}
3764
c4846480
SRG
3765__init void
3766early_enable_events(struct trace_array *tr, char *buf, bool disable_first)
ce1039bd 3767{
ce1039bd
SRRH
3768 char *token;
3769 int ret;
3770
3771 while (true) {
3772 token = strsep(&buf, ",");
3773
3774 if (!token)
3775 break;
ce1039bd 3776
43ed3843
SRRH
3777 if (*token) {
3778 /* Restarting syscalls requires that we stop them first */
3779 if (disable_first)
3780 ftrace_set_clr_event(tr, token, 0);
ce1039bd 3781
43ed3843
SRRH
3782 ret = ftrace_set_clr_event(tr, token, 1);
3783 if (ret)
3784 pr_warn("Failed to enable trace event: %s\n", token);
3785 }
ce1039bd
SRRH
3786
3787 /* Put back the comma to allow this to be called again */
3788 if (buf)
3789 *(buf - 1) = ',';
3790 }
3791}
3792
8781915a
EG
3793static __init int event_trace_enable(void)
3794{
ae63b31e 3795 struct trace_array *tr = top_trace_array();
2425bcb9 3796 struct trace_event_call **iter, *call;
8781915a
EG
3797 int ret;
3798
dc81e5e3
YY
3799 if (!tr)
3800 return -ENODEV;
3801
8781915a
EG
3802 for_each_event(iter, __start_ftrace_events, __stop_ftrace_events) {
3803
3804 call = *iter;
3805 ret = event_init(call);
3806 if (!ret)
3807 list_add(&call->list, &ftrace_events);
3808 }
3809
a01fdc89
SRG
3810 register_trigger_cmds();
3811
77248221
SR
3812 /*
3813 * We need the top trace array to have a working set of trace
3814 * points at early init, before the debug files and directories
3815 * are created. Create the file entries now, and attach them
3816 * to the actual file dentries later.
3817 */
3818 __trace_early_add_events(tr);
3819
c4846480 3820 early_enable_events(tr, bootup_event_buf, false);
81698831
SR
3821
3822 trace_printk_start_comm();
3823
3cd715de
SRRH
3824 register_event_cmds();
3825
85f2b082 3826
8781915a
EG
3827 return 0;
3828}
3829
ce1039bd
SRRH
3830/*
3831 * event_trace_enable() is called from trace_event_init() first to
3832 * initialize events and perhaps start any events that are on the
3833 * command line. Unfortunately, there are some events that will not
3834 * start this early, like the system call tracepoints that need
524666cb
GKB
3835 * to set the %SYSCALL_WORK_SYSCALL_TRACEPOINT flag of pid 1. But
3836 * event_trace_enable() is called before pid 1 starts, and this flag
3837 * is never set, making the syscall tracepoint never get reached, but
3838 * the event is enabled regardless (and not doing anything).
ce1039bd
SRRH
3839 */
3840static __init int event_trace_enable_again(void)
3841{
3842 struct trace_array *tr;
3843
3844 tr = top_trace_array();
3845 if (!tr)
3846 return -ENODEV;
3847
c4846480 3848 early_enable_events(tr, bootup_event_buf, true);
ce1039bd
SRRH
3849
3850 return 0;
3851}
3852
3853early_initcall(event_trace_enable_again);
3854
ac343da7
MH
3855/* Init fields which doesn't related to the tracefs */
3856static __init int event_trace_init_fields(void)
3857{
3858 if (trace_define_generic_fields())
3859 pr_warn("tracing: Failed to allocated generic fields");
3860
3861 if (trace_define_common_fields())
3862 pr_warn("tracing: Failed to allocate common fields");
3863
3864 return 0;
3865}
3866
58b92547 3867__init int event_trace_init(void)
b77e38aa 3868{
ae63b31e 3869 struct trace_array *tr;
6d723736 3870 int ret;
b77e38aa 3871
ae63b31e 3872 tr = top_trace_array();
dc81e5e3
YY
3873 if (!tr)
3874 return -ENODEV;
ae63b31e 3875
e4931b82
YW
3876 trace_create_file("available_events", TRACE_MODE_READ,
3877 NULL, tr, &ftrace_avail_fops);
2314c4ae 3878
dc300d77 3879 ret = early_event_add_tracer(NULL, tr);
ae63b31e
SR
3880 if (ret)
3881 return ret;
020e5f85 3882
836d481e 3883#ifdef CONFIG_MODULES
6d723736 3884 ret = register_module_notifier(&trace_module_nb);
55379376 3885 if (ret)
3448bac3 3886 pr_warn("Failed to register trace events module notifier\n");
836d481e 3887#endif
a838deab
MH
3888
3889 eventdir_initialized = true;
3890
b77e38aa
SR
3891 return 0;
3892}
5f893b26
SRRH
3893
3894void __init trace_event_init(void)
3895{
3896 event_trace_memsetup();
3897 init_ftrace_syscalls();
3898 event_trace_enable();
ac343da7 3899 event_trace_init_fields();
5f893b26
SRRH
3900}
3901
b3015fe4 3902#ifdef CONFIG_EVENT_TRACE_STARTUP_TEST
e6187007
SR
3903
3904static DEFINE_SPINLOCK(test_spinlock);
3905static DEFINE_SPINLOCK(test_spinlock_irq);
3906static DEFINE_MUTEX(test_mutex);
3907
3908static __init void test_work(struct work_struct *dummy)
3909{
3910 spin_lock(&test_spinlock);
3911 spin_lock_irq(&test_spinlock_irq);
3912 udelay(1);
3913 spin_unlock_irq(&test_spinlock_irq);
3914 spin_unlock(&test_spinlock);
3915
3916 mutex_lock(&test_mutex);
3917 msleep(1);
3918 mutex_unlock(&test_mutex);
3919}
3920
3921static __init int event_test_thread(void *unused)
3922{
3923 void *test_malloc;
3924
3925 test_malloc = kmalloc(1234, GFP_KERNEL);
3926 if (!test_malloc)
3927 pr_info("failed to kmalloc\n");
3928
3929 schedule_on_each_cpu(test_work);
3930
3931 kfree(test_malloc);
3932
3933 set_current_state(TASK_INTERRUPTIBLE);
fe0e01c7 3934 while (!kthread_should_stop()) {
e6187007 3935 schedule();
fe0e01c7
PZ
3936 set_current_state(TASK_INTERRUPTIBLE);
3937 }
3938 __set_current_state(TASK_RUNNING);
e6187007
SR
3939
3940 return 0;
3941}
3942
3943/*
3944 * Do various things that may trigger events.
3945 */
3946static __init void event_test_stuff(void)
3947{
3948 struct task_struct *test_thread;
3949
3950 test_thread = kthread_run(event_test_thread, NULL, "test-events");
3951 msleep(1);
3952 kthread_stop(test_thread);
3953}
3954
3955/*
3956 * For every trace event defined, we will test each trace point separately,
3957 * and then by groups, and finally all trace points.
3958 */
9ea21c1e 3959static __init void event_trace_self_tests(void)
e6187007 3960{
7967b3e0 3961 struct trace_subsystem_dir *dir;
7f1d2f82 3962 struct trace_event_file *file;
2425bcb9 3963 struct trace_event_call *call;
e6187007 3964 struct event_subsystem *system;
ae63b31e 3965 struct trace_array *tr;
e6187007
SR
3966 int ret;
3967
ae63b31e 3968 tr = top_trace_array();
dc81e5e3
YY
3969 if (!tr)
3970 return;
ae63b31e 3971
e6187007
SR
3972 pr_info("Running tests on trace events:\n");
3973
ae63b31e
SR
3974 list_for_each_entry(file, &tr->events, list) {
3975
3976 call = file->event_call;
e6187007 3977
2239291a
SR
3978 /* Only test those that have a probe */
3979 if (!call->class || !call->class->probe)
e6187007
SR
3980 continue;
3981
1f5a6b45
SR
3982/*
3983 * Testing syscall events here is pretty useless, but
3984 * we still do it if configured. But this is time consuming.
3985 * What we really need is a user thread to perform the
3986 * syscalls as we test.
3987 */
3988#ifndef CONFIG_EVENT_TRACE_TEST_SYSCALLS
8f082018
SR
3989 if (call->class->system &&
3990 strcmp(call->class->system, "syscalls") == 0)
1f5a6b45
SR
3991 continue;
3992#endif
3993
687fcc4a 3994 pr_info("Testing event %s: ", trace_event_name(call));
e6187007
SR
3995
3996 /*
3997 * If an event is already enabled, someone is using
3998 * it and the self test should not be on.
3999 */
5d6ad960 4000 if (file->flags & EVENT_FILE_FL_ENABLED) {
3448bac3 4001 pr_warn("Enabled event during self test!\n");
e6187007
SR
4002 WARN_ON_ONCE(1);
4003 continue;
4004 }
4005
ae63b31e 4006 ftrace_event_enable_disable(file, 1);
e6187007 4007 event_test_stuff();
ae63b31e 4008 ftrace_event_enable_disable(file, 0);
e6187007
SR
4009
4010 pr_cont("OK\n");
4011 }
4012
4013 /* Now test at the sub system level */
4014
4015 pr_info("Running tests on trace event systems:\n");
4016
ae63b31e
SR
4017 list_for_each_entry(dir, &tr->systems, list) {
4018
4019 system = dir->subsystem;
e6187007
SR
4020
4021 /* the ftrace system is special, skip it */
4022 if (strcmp(system->name, "ftrace") == 0)
4023 continue;
4024
4025 pr_info("Testing event system %s: ", system->name);
4026
ae63b31e 4027 ret = __ftrace_set_clr_event(tr, NULL, system->name, NULL, 1);
e6187007 4028 if (WARN_ON_ONCE(ret)) {
3448bac3
FF
4029 pr_warn("error enabling system %s\n",
4030 system->name);
e6187007
SR
4031 continue;
4032 }
4033
4034 event_test_stuff();
4035
ae63b31e 4036 ret = __ftrace_set_clr_event(tr, NULL, system->name, NULL, 0);
76bab1b7 4037 if (WARN_ON_ONCE(ret)) {
3448bac3
FF
4038 pr_warn("error disabling system %s\n",
4039 system->name);
76bab1b7
YL
4040 continue;
4041 }
e6187007
SR
4042
4043 pr_cont("OK\n");
4044 }
4045
4046 /* Test with all events enabled */
4047
4048 pr_info("Running tests on all trace events:\n");
4049 pr_info("Testing all events: ");
4050
ae63b31e 4051 ret = __ftrace_set_clr_event(tr, NULL, NULL, NULL, 1);
e6187007 4052 if (WARN_ON_ONCE(ret)) {
3448bac3 4053 pr_warn("error enabling all events\n");
9ea21c1e 4054 return;
e6187007
SR
4055 }
4056
4057 event_test_stuff();
4058
4059 /* reset sysname */
ae63b31e 4060 ret = __ftrace_set_clr_event(tr, NULL, NULL, NULL, 0);
e6187007 4061 if (WARN_ON_ONCE(ret)) {
3448bac3 4062 pr_warn("error disabling all events\n");
9ea21c1e 4063 return;
e6187007
SR
4064 }
4065
4066 pr_cont("OK\n");
9ea21c1e
SR
4067}
4068
4069#ifdef CONFIG_FUNCTION_TRACER
4070
245b2e70 4071static DEFINE_PER_CPU(atomic_t, ftrace_test_event_disable);
9ea21c1e 4072
9b9db275 4073static struct trace_event_file event_trace_file __initdata;
b7f0c959
SRRH
4074
4075static void __init
2f5f6ad9 4076function_test_events_call(unsigned long ip, unsigned long parent_ip,
d19ad077 4077 struct ftrace_ops *op, struct ftrace_regs *regs)
9ea21c1e 4078{
13292494 4079 struct trace_buffer *buffer;
9ea21c1e
SR
4080 struct ring_buffer_event *event;
4081 struct ftrace_entry *entry;
36590c50 4082 unsigned int trace_ctx;
9ea21c1e 4083 long disabled;
9ea21c1e 4084 int cpu;
9ea21c1e 4085
36590c50 4086 trace_ctx = tracing_gen_ctx();
5168ae50 4087 preempt_disable_notrace();
9ea21c1e 4088 cpu = raw_smp_processor_id();
245b2e70 4089 disabled = atomic_inc_return(&per_cpu(ftrace_test_event_disable, cpu));
9ea21c1e
SR
4090
4091 if (disabled != 1)
4092 goto out;
4093
9b9db275
SRRH
4094 event = trace_event_buffer_lock_reserve(&buffer, &event_trace_file,
4095 TRACE_FN, sizeof(*entry),
36590c50 4096 trace_ctx);
9ea21c1e
SR
4097 if (!event)
4098 goto out;
4099 entry = ring_buffer_event_data(event);
4100 entry->ip = ip;
4101 entry->parent_ip = parent_ip;
4102
9b9db275 4103 event_trigger_unlock_commit(&event_trace_file, buffer, event,
36590c50 4104 entry, trace_ctx);
9ea21c1e 4105 out:
245b2e70 4106 atomic_dec(&per_cpu(ftrace_test_event_disable, cpu));
5168ae50 4107 preempt_enable_notrace();
9ea21c1e
SR
4108}
4109
4110static struct ftrace_ops trace_ops __initdata =
4111{
4112 .func = function_test_events_call,
4113};
4114
4115static __init void event_trace_self_test_with_function(void)
4116{
17bb615a 4117 int ret;
9b9db275
SRRH
4118
4119 event_trace_file.tr = top_trace_array();
4120 if (WARN_ON(!event_trace_file.tr))
2d34f489 4121 return;
9b9db275 4122
17bb615a
SR
4123 ret = register_ftrace_function(&trace_ops);
4124 if (WARN_ON(ret < 0)) {
4125 pr_info("Failed to enable function tracer for event tests\n");
4126 return;
4127 }
9ea21c1e
SR
4128 pr_info("Running tests again, along with the function tracer\n");
4129 event_trace_self_tests();
4130 unregister_ftrace_function(&trace_ops);
4131}
4132#else
4133static __init void event_trace_self_test_with_function(void)
4134{
4135}
4136#endif
4137
4138static __init int event_trace_self_tests_init(void)
4139{
020e5f85
LZ
4140 if (!tracing_selftest_disabled) {
4141 event_trace_self_tests();
4142 event_trace_self_test_with_function();
4143 }
e6187007
SR
4144
4145 return 0;
4146}
4147
28d20e2d 4148late_initcall(event_trace_self_tests_init);
e6187007
SR
4149
4150#endif