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