Linux
kernel/trace/trace_events.c
Source file coverage
Path:
kernel/trace/trace_events.c
Lines:
5125
Non-empty lines:
4264
Non-empty lines covered with requirements:
478 / 4264 (11.2%)
Functions:
176
Functions covered by requirements:
6 / 176 (3.4%)
1
// SPDX-License-Identifier: GPL-2.0
2
/*
3
 * event tracer
4
 *
5
 * Copyright (C) 2008 Red Hat Inc, Steven Rostedt <srostedt@redhat.com>
6
 *
7
 *  - Added format output of fields of the trace point.
8
 *    This was based off of work by Tom Zanussi <tzanussi@gmail.com>.
9
 *
10
 */
11
 
12
#define pr_fmt(fmt) fmt
13
 
14
#include <linux/workqueue.h>
15
#include <linux/security.h>
16
#include <linux/spinlock.h>
17
#include <linux/kthread.h>
18
#include <linux/tracefs.h>
19
#include <linux/uaccess.h>
20
#include <linux/module.h>
21
#include <linux/ctype.h>
22
#include <linux/sort.h>
23
#include <linux/slab.h>
24
#include <linux/delay.h>
25
 
26
#include <trace/events/sched.h>
27
#include <trace/syscall.h>
28
 
29
#include <asm/setup.h>
30
 
31
#include "trace_output.h"
32
 
33
#undef TRACE_SYSTEM
34
#define TRACE_SYSTEM "TRACE_SYSTEM"
35
 
36
DEFINE_MUTEX(event_mutex);
37
 
38
LIST_HEAD(ftrace_events);
39
static LIST_HEAD(ftrace_generic_fields);
40
static LIST_HEAD(ftrace_common_fields);
41
static bool eventdir_initialized;
42
 
43
static LIST_HEAD(module_strings);
44
 
45
struct module_string {
46
	struct list_head	next;
47
	struct module		*module;
48
	char			*str;
49
};
50
 
51
#define GFP_TRACE (GFP_KERNEL | __GFP_ZERO)
52
 
53
static struct kmem_cache *field_cachep;
54
static struct kmem_cache *file_cachep;
55
 
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 last
234
 * accessible offset of trace fields excluding __dynamic_array bytes
235
 */
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 is
313
		 * OK to dereference. There's ways to fool this, but
314
		 * this is to catch common mistakes, not malicious code.
315
		 */
316
		a = strchr(fmt, '&');
317
		if ((a && (a < r)) || test_field(r, call))
318
			return true;
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 strings
370
	 * that are safe to use.
371
	 */
372
	r = strstr(s, "[");
373
	if (r && r < e) {
374
		r = strstr(r, "REC->");
375
		if (r && r < e)
376
			return true;
377
	}
378
 
379
	/*
380
	 * If there's any strings in the argument consider this arg OK as it
381
	 * could be: REC->field ? "foo" : "bar" and we don't want to get into
382
	 * verifying that logic here.
383
	 */
384
	if (find_print_string(fmt, "\"", e))
385
		return true;
386
 
387
	/* Dereferenced strings are also valid like any other pointer */
388
	if (process_pointer(fmt, len, call))
389
		return true;
390
 
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 dereference
419
 * pointers using %p* that could be recorded in the trace event and
420
 * much later referenced after the pointer was freed. Dereferencing
421
 * pointers are OK, if it is dereferenced into the event itself.
422
 */
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 that
451
			 * is processed first to find %p* usage,
452
			 * then after the first string, the print fmt
453
			 * contains arguments that are used to check
454
			 * if the dereferenced %p* usage is safe.
455
			 */
456
			if (first) {
457
				if (fmt[i] == '\'')
458
					continue;
459
				if (in_quote) {
460
					arg = 0;
461
					first = false;
462
					/*
463
					 * If there was no %p* uses
464
					 * the fmt is OK.
465
					 */
466
					if (!dereference_flags)
467
						return;
468
				}
469
			}
470
			if (in_quote) {
471
				if (in_quote == fmt[i])
472
					in_quote = 0;
473
			} else {
474
				in_quote = fmt[i];
475
			}
476
			continue;
477
		case '%':
478
			if (!first || !in_quote)
479
				continue;
480
			i++;
481
			if (!fmt[i])
482
				return;
483
			switch (fmt[i]) {
484
			case '%':
485
				continue;
486
			case 'p':
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 the
568
			 * first argument. The processing of the argument happens
569
			 * when the end of the argument is found, as it needs to
570
			 * handle paranthesis and such.
571
			 */
572
			if (!start_arg) {
573
				start_arg = i;
574
				/* Balance out the i++ in the for loop */
575
				i--;
576
				continue;
577
			}
578
 
579
			if (dereference_flags & (1ULL << arg)) {
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 reported
600
	 * uses an unsafe dereference pointer %p*. As the data stored
601
	 * at the trace event time may no longer exist when the trace
602
	 * event is printed, dereferencing to the original source is
603
	 * unsafe. The source of the dereference must be copied into the
604
	 * event itself, and the dereference must access the copy instead.
605
	 */
606
	if (WARN_ON_ONCE(dereference_flags)) {
607
		arg = 1;
608
		while (!(dereference_flags & 1)) {
609
			dereference_flags >>= 1;
610
			arg++;
611
		}
612
		pr_warn("event %s has unsafe dereference of argument %d\n",
613
			trace_event_name(call), arg);
614
		pr_warn("print_fmt: %s\n", fmt);
615
	}
616
}
617
 
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 disables
664
	 * preemption (adding one to the preempt_count). Since we are
665
	 * interested in the preempt_count at the time the tracepoint was
666
	 * hit, we need to subtract one to offset the increment.
667
	 */
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_EVENTS
702
	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
#endif
717
	}
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
 
766
/**
767
 * SPDX-Req-ID: 77958d2a51762caa727e5751d8dfec127c07cb5385f542d7b2fdf26b2a07c8b3
768
 * SPDX-Req-Text:
769
 * __ftrace_event_enable_disable - enable or disable a trace event
770
 * @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 or
773
 * IS NOT a soft enable/disable.
774
 *
775
 * Function's expectations:
776
 * - If soft_disable is 1 a soft mode reference counter associated with the
777
 *   trace event shall be increased or decreased according to the enable
778
 *   parameter being 1 (enable) or 0 (disable) respectively.
779
 *   If the soft mode reference counter is > 0 before the increase or after
780
 *   the decrease, no other actions shall be taken.
781
 *
782
 * - if soft_disable is 1 and the soft mode reference counter is 0 before
783
 *   the increase or after the decrease, an enable value set to 0 or 1 shall
784
 *   result in disabling or enabling the use of trace_buffered_event
785
 *   respectively.
786
 *
787
 * - If soft_disable is 1 and enable is 0 and the soft mode reference counter
788
 *   reaches zero and if the soft disabled flag is set (i.e. if the event was
789
 *   previously enabled with soft_disable = 1), tracing for the trace point
790
 *   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 event
793
 *   shall be disabled only if the soft mode reference counter is 0.
794
 *   Additionally the soft disabled flag shall be set or cleared according to
795
 *   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 (if
798
 *   previously disabled); in addition, if soft_disable is 1 and the soft mode
799
 *   reference counter is 0 before the increase, the soft disabled flag shall
800
 *   be set.
801
 *
802
 * - When enabling or disabling tracing for the trace point event
803
 *   the flags associated with comms and tgids shall be checked and, if set,
804
 *   respectively tracing of comms and tgdis at sched_switch shall be
805
 *   enabled/disabled.
806
 *
807
 * Assumptions of Use:
808
 * - for thread-safe execution, event_mutex shall be locked before calling
809
 *   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 0
812
 *   or 1.
813
 *
814
 * Context: process context.
815
 *
816
 * Return:
817
 * * 0 on success
818
 * * any error returned by the event register or unregister callbacks
819
 *
820
 * SPDX-Req-End
821
 */
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_ref
835
		 * reference counter is decremented. If it reaches 0, we want
836
		 * to clear the SOFT_DISABLED flag but leave the event in the
837
		 * state that it was. That is, if the event was enabled and
838
		 * SOFT_DISABLED isn't set, then do nothing. But if SOFT_DISABLED
839
		 * is set we do not want the event to be enabled before we
840
		 * 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, otherwise
844
		 * "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 to
881
		 * register the tracepoint for the event, but leave the event
882
		 * as is. That means, if the event was already enabled, we do
883
		 * nothing (but set soft_mode). If the event is disabled, we
884
		 * set SOFT_DISABLED before enabling the event tracepoint, so
885
		 * 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_MODULES
950
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
#endif
1062
 
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 it
1134
	 * 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
 
1355
/**
1356
 * SPDX-Req-ID: 428a5db6e481de87fc424119c30738d83e378b34bb42e12295ddfcba9839e5b3
1357
 * 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 '-' with
1368
 *   '_' 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 enabled
1371
 *   or disabled shall be selected according to the input match, sub, event and
1372
 *   mod parameters. Each of these parameters, if set, shall restrict the events
1373
 *   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 the
1377
 *   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 calling
1381
 *   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 0
1384
 *   or 1.
1385
 *
1386
 * NOTE: __ftrace_set_clr_event_nolock(NULL, NULL, NULL, set, NULL) will
1387
 * set/unset all events.
1388
 *
1389
 * Context: process context.
1390
 *
1391
 * Return:
1392
 * * 0 on success
1393
 * * %-EINVAL - the input parameters do not match any registered event
1394
 * * %-ENOMEM -  memory allocation fails for the module pointer
1395
 * * any value returned by the first call to ftrace_event_enable_disable that
1396
 * returned an error
1397
 *
1398
 * SPDX-Req-End
1399
 */
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 events
1455
		 * may still have been enabled, but let the user
1456
		 * 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 subsystem
1509
	 *  <subsystem>: means the same.
1510
	 *
1511
	 *  <name> (no ':') means all events in a subsystem with
1512
	 *  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: 080fa9a6d27aa94dfaf8cbceb9715cbc146b0671bbe53c10dccf173f911b1a5e
1542
 * 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 disable
1549
 * event recording.
1550
 *
1551
 * Function's expectations:
1552
 * - This function shall retrieve the pointer of the global trace array (global
1553
 *   tracer) and pass it, along the rest of input parameters, to
1554
 *   __ftrace_set_clr_event_nolock.
1555
 *
1556
 * - This function shall properly lock/unlock the global event_mutex
1557
 *   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 0
1561
 *   or 1.
1562
 *
1563
 * Context: process context, locks and unlocks event_mutex.
1564
 *
1565
 * Return:
1566
 * * 0 on success
1567
 * * %-ENODEV - the global tracer cannot be retrieved
1568
 * * any other error condition returned by __ftrace_set_clr_event_nolock
1569
 *
1570
 * SPDX-Req-End
1571
 */
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
 
1583
/**
1584
 * SPDX-Req-ID: 9c8df0f97d3dc3e6e0a2df6ade7f7d99a983559f03b64d3ed16ec6b1314e88bd
1585
 * SPDX-Req-Text:
1586
 * trace_array_set_clr_event - enable or disable an event within a system for
1587
 * 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 event
1594
 * recording.
1595
 *
1596
 * Function's expectations:
1597
 * - This function shall properly lock/unlock the global event_mutex
1598
 *   before/after invoking ftrace_set_clr_event_nolock passing along the same
1599
 *   input parameters.
1600
 *
1601
 * Context: process context, locks and unlocks event_mutex.
1602
 *
1603
 * Return:
1604
 * * 0 on success
1605
 * * %-ENOENT - the input tr is NULL
1606
 * * any other error condition returned by __ftrace_set_clr_event_nolock
1607
 *
1608
 * SPDX-Req-End
1609
 */
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		127
1625
 
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_MODULES
1737
		iter->type = SET_EVENT_MOD;
1738
		iter->event_mod = list_entry(&tr->mod_events, struct event_mod_load, list);
1739
#endif
1740
	}
1741
 
1742
#ifdef CONFIG_MODULES
1743
	list_for_each_entry_continue(iter->event_mod, &tr->mod_events, list)
1744
		return iter;
1745
#endif
1746
 
1747
	/*
1748
	 * The iter is allocated in s_start() and passed via the 'v'
1749
	 * parameter. To stop the iterator, NULL must be returned. But
1750
	 * the return value is what the 'v' parameter in s_stop() receives
1751
	 * 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_MODULES
1797
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
#endif
1828
 
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 same
1869
	 * tr->filtered_pids as p_start() has.
1870
	 * If we just passed the tr->filtered_pids around, then RCU would
1871
	 * 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: dfa044fb2e2570c8691dc83f65ab96142120f1dd61a5d746947f9d36d10c0223
1908
 * 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 a
1916
 * specific event
1917
 *
1918
 * Function's expectations:
1919
 * - The global event_mutex shall be taken before performing any operation
1920
 *   on the target event;
1921
 *
1922
 * - The string copied to user space shall be formatted according to the
1923
 *   status flags from the target event file:
1924
 *   - If the enable flag is set AND the soft_disable flag is not set then
1925
 *     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 the
1928
 *     second character shall be set to "*" ELSE it is skipped;
1929
 *
1930
 *   - The string shall be terminated by a newline ("\n") and any remaining
1931
 *     character shall be set to "0";
1932
 *
1933
 * - This function shall invoke simple_read_from_buffer() to perform the copy
1934
 *   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 string
1938
 *   to be copied to user space;
1939
 *
1940
 * - Any read operation on a file descriptor, unless it is the first operation
1941
 *   following a trace event file open, shall be preceded by an lseek
1942
 *   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 success
1948
 * * %-ENODEV - the event file cannot be retrieved from the input filp
1949
 * * any error returned by simple_read_from_buffer
1950
 *
1951
 * SPDX-Req-End
1952
 */
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: 4e996e6ac0d952336cac1f8497fb9fdb73407c3942008b2853ae2bc417db4f93
1984
 * 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 event
1992
 * recording.
1993
 *
1994
 * Function's expectations:
1995
 * - This function shall copy cnt bytes from the input ubuf buffer to a kernel
1996
 *   space buffer and shall convert the string within the kernel space buffer
1997
 *   into a decimal base format number;
1998
 *
1999
 * - The global event_mutex shall be taken before performing any operation
2000
 *   on the target event;
2001
 *
2002
 * - This function shall check the size of the per-cpu ring-buffers used for
2003
 *   the event trace data and, if smaller than TRACE_BUF_SIZE_DEFAULT, expand
2004
 *   them to TRACE_BUF_SIZE_DEFAULT bytes (sizes larger than
2005
 *   TRACE_BUF_SIZE_DEFAULT are not allowed);
2006
 *
2007
 * - This function shall invoke ftrace_event_enable_disable to enable or
2008
 *   disable the target trace event according to the value read from user space
2009
 *   (0 - disable, 1 - enable);
2010
 *
2011
 * - This function shall increase the file position pointed by ppos by the
2012
 *   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 success
2018
 * * any error returned by kstrtoul_from_user
2019
 * * %-ENODEV - the event file cannot be retrieved from the input filp
2020
 * * any error returned by tracing_update_buffers
2021
 * * any error returned by ftrace_event_enable_disable
2022
 * * %-EINVAL - the value copied from the user space ubuf is different
2023
 * from 0 or 1
2024
 *
2025
 * SPDX-Req-End
2026
 */
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
 * Returns:
2066
 *   0 : no events exist?
2067
 *   1 : all events are disabled
2068
 *   2 : all events are enabled
2069
 *   3 : some events are enabled and some are enabled
2070
 */
2071
int trace_events_enabled(struct trace_array *tr, const char *system)
2072
{
2073
	struct trace_event_call *call;
2074
	struct trace_event_file *file;
2075
	int set = 0;
2076
 
2077
	guard(mutex)(&event_mutex);
2078
 
2079
	list_for_each_entry(file, &tr->events, list) {
2080
		call = file->event_call;
2081
		if ((call->flags & TRACE_EVENT_FL_IGNORE_ENABLE) ||
2082
		    !trace_event_name(call) || !call->class || !call->class->reg)
2083
			continue;
2084
 
2085
		if (system && strcmp(call->class->system, system) != 0)
2086
			continue;
2087
 
2088
		/*
2089
		 * We need to find out if all the events are set
2090
		 * or if all events or cleared, or if we have
2091
		 * a mixture.
2092
		 */
2093
		set |= (1 << !!(file->flags & EVENT_FILE_FL_ENABLED));
2094
 
2095
		/*
2096
		 * If we have a mixture, no need to look further.
2097
		 */
2098
		if (set == 3)
2099
			break;
2100
	}
2101
 
2102
	return set;
2103
}
2104
 
2105
static ssize_t
2106
system_enable_read(struct file *filp, char __user *ubuf, size_t cnt,
2107
		   loff_t *ppos)
2108
{
2109
	const char set_to_char[4] = { '?', '0', '1', 'X' };
2110
	struct trace_subsystem_dir *dir = filp->private_data;
2111
	struct event_subsystem *system = dir->subsystem;
2112
	struct trace_array *tr = dir->tr;
2113
	char buf[2];
2114
	int set;
2115
	int ret;
2116
 
2117
	set = trace_events_enabled(tr, system ? system->name : NULL);
2118
 
2119
	buf[0] = set_to_char[set];
2120
	buf[1] = '\n';
2121
 
2122
	ret = simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
2123
 
2124
	return ret;
2125
}
2126
 
2127
static ssize_t
2128
system_enable_write(struct file *filp, const char __user *ubuf, size_t cnt,
2129
		    loff_t *ppos)
2130
{
2131
	struct trace_subsystem_dir *dir = filp->private_data;
2132
	struct event_subsystem *system = dir->subsystem;
2133
	const char *name = NULL;
2134
	unsigned long val;
2135
	ssize_t ret;
2136
 
2137
	ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
2138
	if (ret)
2139
		return ret;
2140
 
2141
	ret = tracing_update_buffers(dir->tr);
2142
	if (ret < 0)
2143
		return ret;
2144
 
2145
	if (val != 0 && val != 1)
2146
		return -EINVAL;
2147
 
2148
	/*
2149
	 * Opening of "enable" adds a ref count to system,
2150
	 * so the name is safe to use.
2151
	 */
2152
	if (system)
2153
		name = system->name;
2154
 
2155
	ret = __ftrace_set_clr_event(dir->tr, NULL, name, NULL, val, NULL);
2156
	if (ret)
2157
		goto out;
2158
 
2159
	ret = cnt;
2160
 
2161
out:
2162
	*ppos += cnt;
2163
 
2164
	return ret;
2165
}
2166
 
2167
enum {
2168
	FORMAT_HEADER		= 1,
2169
	FORMAT_FIELD_SEPERATOR	= 2,
2170
	FORMAT_PRINTFMT		= 3,
2171
};
2172
 
2173
static void *f_next(struct seq_file *m, void *v, loff_t *pos)
2174
{
2175
	struct trace_event_file *file = event_file_data(m->private);
2176
	struct trace_event_call *call = file->event_call;
2177
	struct list_head *common_head = &ftrace_common_fields;
2178
	struct list_head *head = trace_get_fields(call);
2179
	struct list_head *node = v;
2180
 
2181
	(*pos)++;
2182
 
2183
	switch ((unsigned long)v) {
2184
	case FORMAT_HEADER:
2185
		node = common_head;
2186
		break;
2187
 
2188
	case FORMAT_FIELD_SEPERATOR:
2189
		node = head;
2190
		break;
2191
 
2192
	case FORMAT_PRINTFMT:
2193
		/* all done */
2194
		return NULL;
2195
	}
2196
 
2197
	node = node->prev;
2198
	if (node == common_head)
2199
		return (void *)FORMAT_FIELD_SEPERATOR;
2200
	else if (node == head)
2201
		return (void *)FORMAT_PRINTFMT;
2202
	else
2203
		return node;
2204
}
2205
 
2206
static int f_show(struct seq_file *m, void *v)
2207
{
2208
	struct trace_event_file *file = event_file_data(m->private);
2209
	struct trace_event_call *call = file->event_call;
2210
	struct ftrace_event_field *field;
2211
	const char *array_descriptor;
2212
 
2213
	switch ((unsigned long)v) {
2214
	case FORMAT_HEADER:
2215
		seq_printf(m, "name: %s\n", trace_event_name(call));
2216
		seq_printf(m, "ID: %d\n", call->event.type);
2217
		seq_puts(m, "format:\n");
2218
		return 0;
2219
 
2220
	case FORMAT_FIELD_SEPERATOR:
2221
		seq_putc(m, '\n');
2222
		return 0;
2223
 
2224
	case FORMAT_PRINTFMT:
2225
		seq_printf(m, "\nprint fmt: %s\n",
2226
			   call->print_fmt);
2227
		return 0;
2228
	}
2229
 
2230
	field = list_entry(v, struct ftrace_event_field, link);
2231
	/*
2232
	 * Smartly shows the array type(except dynamic array).
2233
	 * Normal:
2234
	 *	field:TYPE VAR
2235
	 * If TYPE := TYPE[LEN], it is shown:
2236
	 *	field:TYPE VAR[LEN]
2237
	 */
2238
	array_descriptor = strchr(field->type, '[');
2239
 
2240
	if (str_has_prefix(field->type, "__data_loc"))
2241
		array_descriptor = NULL;
2242
 
2243
	if (!array_descriptor)
2244
		seq_printf(m, "\tfield:%s %s;\toffset:%u;\tsize:%u;\tsigned:%d;\n",
2245
			   field->type, field->name, field->offset,
2246
			   field->size, !!field->is_signed);
2247
	else if (field->len)
2248
		seq_printf(m, "\tfield:%.*s %s[%d];\toffset:%u;\tsize:%u;\tsigned:%d;\n",
2249
			   (int)(array_descriptor - field->type),
2250
			   field->type, field->name,
2251
			   field->len, field->offset,
2252
			   field->size, !!field->is_signed);
2253
	else
2254
		seq_printf(m, "\tfield:%.*s %s[];\toffset:%u;\tsize:%u;\tsigned:%d;\n",
2255
				(int)(array_descriptor - field->type),
2256
				field->type, field->name,
2257
				field->offset, field->size, !!field->is_signed);
2258
 
2259
	return 0;
2260
}
2261
 
2262
static void *f_start(struct seq_file *m, loff_t *pos)
2263
{
2264
	struct trace_event_file *file;
2265
	void *p = (void *)FORMAT_HEADER;
2266
	loff_t l = 0;
2267
 
2268
	/* ->stop() is called even if ->start() fails */
2269
	mutex_lock(&event_mutex);
2270
	file = event_file_file(m->private);
2271
	if (!file)
2272
		return ERR_PTR(-ENODEV);
2273
 
2274
	while (l < *pos && p)
2275
		p = f_next(m, p, &l);
2276
 
2277
	return p;
2278
}
2279
 
2280
static void f_stop(struct seq_file *m, void *p)
2281
{
2282
	mutex_unlock(&event_mutex);
2283
}
2284
 
2285
static const struct seq_operations trace_format_seq_ops = {
2286
	.start		= f_start,
2287
	.next		= f_next,
2288
	.stop		= f_stop,
2289
	.show		= f_show,
2290
};
2291
 
2292
static int trace_format_open(struct inode *inode, struct file *file)
2293
{
2294
	struct seq_file *m;
2295
	int ret;
2296
 
2297
	/* Do we want to hide event format files on tracefs lockdown? */
2298
 
2299
	ret = seq_open(file, &trace_format_seq_ops);
2300
	if (ret < 0)
2301
		return ret;
2302
 
2303
	m = file->private_data;
2304
	m->private = file;
2305
 
2306
	return 0;
2307
}
2308
 
2309
#ifdef CONFIG_PERF_EVENTS
2310
static ssize_t
2311
event_id_read(struct file *filp, char __user *ubuf, size_t cnt, loff_t *ppos)
2312
{
2313
	int id = (long)event_file_data(filp);
2314
	char buf[32];
2315
	int len;
2316
 
2317
	if (unlikely(!id))
2318
		return -ENODEV;
2319
 
2320
	len = sprintf(buf, "%d\n", id);
2321
 
2322
	return simple_read_from_buffer(ubuf, cnt, ppos, buf, len);
2323
}
2324
#endif
2325
 
2326
static ssize_t
2327
event_filter_read(struct file *filp, char __user *ubuf, size_t cnt,
2328
		  loff_t *ppos)
2329
{
2330
	struct trace_event_file *file;
2331
	struct trace_seq *s;
2332
	int r = -ENODEV;
2333
 
2334
	if (*ppos)
2335
		return 0;
2336
 
2337
	s = kmalloc(sizeof(*s), GFP_KERNEL);
2338
 
2339
	if (!s)
2340
		return -ENOMEM;
2341
 
2342
	trace_seq_init(s);
2343
 
2344
	mutex_lock(&event_mutex);
2345
	file = event_file_file(filp);
2346
	if (file)
2347
		print_event_filter(file, s);
2348
	mutex_unlock(&event_mutex);
2349
 
2350
	if (file)
2351
		r = simple_read_from_buffer(ubuf, cnt, ppos,
2352
					    s->buffer, trace_seq_used(s));
2353
 
2354
	kfree(s);
2355
 
2356
	return r;
2357
}
2358
 
2359
static ssize_t
2360
event_filter_write(struct file *filp, const char __user *ubuf, size_t cnt,
2361
		   loff_t *ppos)
2362
{
2363
	struct trace_event_file *file;
2364
	char *buf;
2365
	int err = -ENODEV;
2366
 
2367
	if (cnt >= PAGE_SIZE)
2368
		return -EINVAL;
2369
 
2370
	buf = memdup_user_nul(ubuf, cnt);
2371
	if (IS_ERR(buf))
2372
		return PTR_ERR(buf);
2373
 
2374
	mutex_lock(&event_mutex);
2375
	file = event_file_file(filp);
2376
	if (file) {
2377
		if (file->flags & EVENT_FILE_FL_FREED)
2378
			err = -ENODEV;
2379
		else
2380
			err = apply_event_filter(file, buf);
2381
	}
2382
	mutex_unlock(&event_mutex);
2383
 
2384
	kfree(buf);
2385
	if (err < 0)
2386
		return err;
2387
 
2388
	*ppos += cnt;
2389
 
2390
	return cnt;
2391
}
2392
 
2393
static LIST_HEAD(event_subsystems);
2394
 
2395
static int subsystem_open(struct inode *inode, struct file *filp)
2396
{
2397
	struct trace_subsystem_dir *dir = NULL, *iter_dir;
2398
	struct trace_array *tr = NULL, *iter_tr;
2399
	struct event_subsystem *system = NULL;
2400
	int ret;
2401
 
2402
	if (tracing_is_disabled())
2403
		return -ENODEV;
2404
 
2405
	/* Make sure the system still exists */
2406
	mutex_lock(&event_mutex);
2407
	mutex_lock(&trace_types_lock);
2408
	list_for_each_entry(iter_tr, &ftrace_trace_arrays, list) {
2409
		list_for_each_entry(iter_dir, &iter_tr->systems, list) {
2410
			if (iter_dir == inode->i_private) {
2411
				/* Don't open systems with no events */
2412
				tr = iter_tr;
2413
				dir = iter_dir;
2414
				if (dir->nr_events) {
2415
					__get_system_dir(dir);
2416
					system = dir->subsystem;
2417
				}
2418
				goto exit_loop;
2419
			}
2420
		}
2421
	}
2422
 exit_loop:
2423
	mutex_unlock(&trace_types_lock);
2424
	mutex_unlock(&event_mutex);
2425
 
2426
	if (!system)
2427
		return -ENODEV;
2428
 
2429
	/* Still need to increment the ref count of the system */
2430
	if (trace_array_get(tr) < 0) {
2431
		put_system(dir);
2432
		return -ENODEV;
2433
	}
2434
 
2435
	ret = tracing_open_generic(inode, filp);
2436
	if (ret < 0) {
2437
		trace_array_put(tr);
2438
		put_system(dir);
2439
	}
2440
 
2441
	return ret;
2442
}
2443
 
2444
static int system_tr_open(struct inode *inode, struct file *filp)
2445
{
2446
	struct trace_subsystem_dir *dir;
2447
	struct trace_array *tr = inode->i_private;
2448
	int ret;
2449
 
2450
	/* Make a temporary dir that has no system but points to tr */
2451
	dir = kzalloc(sizeof(*dir), GFP_KERNEL);
2452
	if (!dir)
2453
		return -ENOMEM;
2454
 
2455
	ret = tracing_open_generic_tr(inode, filp);
2456
	if (ret < 0) {
2457
		kfree(dir);
2458
		return ret;
2459
	}
2460
	dir->tr = tr;
2461
	filp->private_data = dir;
2462
 
2463
	return 0;
2464
}
2465
 
2466
static int subsystem_release(struct inode *inode, struct file *file)
2467
{
2468
	struct trace_subsystem_dir *dir = file->private_data;
2469
 
2470
	trace_array_put(dir->tr);
2471
 
2472
	/*
2473
	 * If dir->subsystem is NULL, then this is a temporary
2474
	 * descriptor that was made for a trace_array to enable
2475
	 * all subsystems.
2476
	 */
2477
	if (dir->subsystem)
2478
		put_system(dir);
2479
	else
2480
		kfree(dir);
2481
 
2482
	return 0;
2483
}
2484
 
2485
static ssize_t
2486
subsystem_filter_read(struct file *filp, char __user *ubuf, size_t cnt,
2487
		      loff_t *ppos)
2488
{
2489
	struct trace_subsystem_dir *dir = filp->private_data;
2490
	struct event_subsystem *system = dir->subsystem;
2491
	struct trace_seq *s;
2492
	int r;
2493
 
2494
	if (*ppos)
2495
		return 0;
2496
 
2497
	s = kmalloc(sizeof(*s), GFP_KERNEL);
2498
	if (!s)
2499
		return -ENOMEM;
2500
 
2501
	trace_seq_init(s);
2502
 
2503
	print_subsystem_event_filter(system, s);
2504
	r = simple_read_from_buffer(ubuf, cnt, ppos,
2505
				    s->buffer, trace_seq_used(s));
2506
 
2507
	kfree(s);
2508
 
2509
	return r;
2510
}
2511
 
2512
static ssize_t
2513
subsystem_filter_write(struct file *filp, const char __user *ubuf, size_t cnt,
2514
		       loff_t *ppos)
2515
{
2516
	struct trace_subsystem_dir *dir = filp->private_data;
2517
	char *buf;
2518
	int err;
2519
 
2520
	if (cnt >= PAGE_SIZE)
2521
		return -EINVAL;
2522
 
2523
	buf = memdup_user_nul(ubuf, cnt);
2524
	if (IS_ERR(buf))
2525
		return PTR_ERR(buf);
2526
 
2527
	err = apply_subsystem_event_filter(dir, buf);
2528
	kfree(buf);
2529
	if (err < 0)
2530
		return err;
2531
 
2532
	*ppos += cnt;
2533
 
2534
	return cnt;
2535
}
2536
 
2537
static ssize_t
2538
show_header_page_file(struct file *filp, char __user *ubuf, size_t cnt, loff_t *ppos)
2539
{
2540
	struct trace_array *tr = filp->private_data;
2541
	struct trace_seq *s;
2542
	int r;
2543
 
2544
	if (*ppos)
2545
		return 0;
2546
 
2547
	s = kmalloc(sizeof(*s), GFP_KERNEL);
2548
	if (!s)
2549
		return -ENOMEM;
2550
 
2551
	trace_seq_init(s);
2552
 
2553
	ring_buffer_print_page_header(tr->array_buffer.buffer, s);
2554
	r = simple_read_from_buffer(ubuf, cnt, ppos,
2555
				    s->buffer, trace_seq_used(s));
2556
 
2557
	kfree(s);
2558
 
2559
	return r;
2560
}
2561
 
2562
static ssize_t
2563
show_header_event_file(struct file *filp, char __user *ubuf, size_t cnt, loff_t *ppos)
2564
{
2565
	struct trace_seq *s;
2566
	int r;
2567
 
2568
	if (*ppos)
2569
		return 0;
2570
 
2571
	s = kmalloc(sizeof(*s), GFP_KERNEL);
2572
	if (!s)
2573
		return -ENOMEM;
2574
 
2575
	trace_seq_init(s);
2576
 
2577
	ring_buffer_print_entry_header(s);
2578
	r = simple_read_from_buffer(ubuf, cnt, ppos,
2579
				    s->buffer, trace_seq_used(s));
2580
 
2581
	kfree(s);
2582
 
2583
	return r;
2584
}
2585
 
2586
static void ignore_task_cpu(void *data)
2587
{
2588
	struct trace_array *tr = data;
2589
	struct trace_pid_list *pid_list;
2590
	struct trace_pid_list *no_pid_list;
2591
 
2592
	/*
2593
	 * This function is called by on_each_cpu() while the
2594
	 * event_mutex is held.
2595
	 */
2596
	pid_list = rcu_dereference_protected(tr->filtered_pids,
2597
					     mutex_is_locked(&event_mutex));
2598
	no_pid_list = rcu_dereference_protected(tr->filtered_no_pids,
2599
					     mutex_is_locked(&event_mutex));
2600
 
2601
	this_cpu_write(tr->array_buffer.data->ignore_pid,
2602
		       trace_ignore_this_task(pid_list, no_pid_list, current));
2603
}
2604
 
2605
static void register_pid_events(struct trace_array *tr)
2606
{
2607
	/*
2608
	 * Register a probe that is called before all other probes
2609
	 * to set ignore_pid if next or prev do not match.
2610
	 * Register a probe this is called after all other probes
2611
	 * to only keep ignore_pid set if next pid matches.
2612
	 */
2613
	register_trace_prio_sched_switch(event_filter_pid_sched_switch_probe_pre,
2614
					 tr, INT_MAX);
2615
	register_trace_prio_sched_switch(event_filter_pid_sched_switch_probe_post,
2616
					 tr, 0);
2617
 
2618
	register_trace_prio_sched_wakeup(event_filter_pid_sched_wakeup_probe_pre,
2619
					 tr, INT_MAX);
2620
	register_trace_prio_sched_wakeup(event_filter_pid_sched_wakeup_probe_post,
2621
					 tr, 0);
2622
 
2623
	register_trace_prio_sched_wakeup_new(event_filter_pid_sched_wakeup_probe_pre,
2624
					     tr, INT_MAX);
2625
	register_trace_prio_sched_wakeup_new(event_filter_pid_sched_wakeup_probe_post,
2626
					     tr, 0);
2627
 
2628
	register_trace_prio_sched_waking(event_filter_pid_sched_wakeup_probe_pre,
2629
					 tr, INT_MAX);
2630
	register_trace_prio_sched_waking(event_filter_pid_sched_wakeup_probe_post,
2631
					 tr, 0);
2632
}
2633
 
2634
static ssize_t
2635
event_pid_write(struct file *filp, const char __user *ubuf,
2636
		size_t cnt, loff_t *ppos, int type)
2637
{
2638
	struct seq_file *m = filp->private_data;
2639
	struct trace_array *tr = m->private;
2640
	struct trace_pid_list *filtered_pids = NULL;
2641
	struct trace_pid_list *other_pids = NULL;
2642
	struct trace_pid_list *pid_list;
2643
	struct trace_event_file *file;
2644
	ssize_t ret;
2645
 
2646
	if (!cnt)
2647
		return 0;
2648
 
2649
	ret = tracing_update_buffers(tr);
2650
	if (ret < 0)
2651
		return ret;
2652
 
2653
	guard(mutex)(&event_mutex);
2654
 
2655
	if (type == TRACE_PIDS) {
2656
		filtered_pids = rcu_dereference_protected(tr->filtered_pids,
2657
							  lockdep_is_held(&event_mutex));
2658
		other_pids = rcu_dereference_protected(tr->filtered_no_pids,
2659
							  lockdep_is_held(&event_mutex));
2660
	} else {
2661
		filtered_pids = rcu_dereference_protected(tr->filtered_no_pids,
2662
							  lockdep_is_held(&event_mutex));
2663
		other_pids = rcu_dereference_protected(tr->filtered_pids,
2664
							  lockdep_is_held(&event_mutex));
2665
	}
2666
 
2667
	ret = trace_pid_write(filtered_pids, &pid_list, ubuf, cnt);
2668
	if (ret < 0)
2669
		return ret;
2670
 
2671
	if (type == TRACE_PIDS)
2672
		rcu_assign_pointer(tr->filtered_pids, pid_list);
2673
	else
2674
		rcu_assign_pointer(tr->filtered_no_pids, pid_list);
2675
 
2676
	list_for_each_entry(file, &tr->events, list) {
2677
		set_bit(EVENT_FILE_FL_PID_FILTER_BIT, &file->flags);
2678
	}
2679
 
2680
	if (filtered_pids) {
2681
		tracepoint_synchronize_unregister();
2682
		trace_pid_list_free(filtered_pids);
2683
	} else if (pid_list && !other_pids) {
2684
		register_pid_events(tr);
2685
	}
2686
 
2687
	/*
2688
	 * Ignoring of pids is done at task switch. But we have to
2689
	 * check for those tasks that are currently running.
2690
	 * Always do this in case a pid was appended or removed.
2691
	 */
2692
	on_each_cpu(ignore_task_cpu, tr, 1);
2693
 
2694
	*ppos += ret;
2695
 
2696
	return ret;
2697
}
2698
 
2699
static ssize_t
2700
ftrace_event_pid_write(struct file *filp, const char __user *ubuf,
2701
		       size_t cnt, loff_t *ppos)
2702
{
2703
	return event_pid_write(filp, ubuf, cnt, ppos, TRACE_PIDS);
2704
}
2705
 
2706
static ssize_t
2707
ftrace_event_npid_write(struct file *filp, const char __user *ubuf,
2708
			size_t cnt, loff_t *ppos)
2709
{
2710
	return event_pid_write(filp, ubuf, cnt, ppos, TRACE_NO_PIDS);
2711
}
2712
 
2713
static int ftrace_event_avail_open(struct inode *inode, struct file *file);
2714
static int ftrace_event_set_open(struct inode *inode, struct file *file);
2715
static int ftrace_event_set_pid_open(struct inode *inode, struct file *file);
2716
static int ftrace_event_set_npid_open(struct inode *inode, struct file *file);
2717
static int ftrace_event_release(struct inode *inode, struct file *file);
2718
 
2719
static const struct seq_operations show_event_seq_ops = {
2720
	.start = t_start,
2721
	.next = t_next,
2722
	.show = t_show,
2723
	.stop = t_stop,
2724
};
2725
 
2726
static const struct seq_operations show_set_event_seq_ops = {
2727
	.start = s_start,
2728
	.next = s_next,
2729
	.show = s_show,
2730
	.stop = s_stop,
2731
};
2732
 
2733
static const struct seq_operations show_set_pid_seq_ops = {
2734
	.start = p_start,
2735
	.next = p_next,
2736
	.show = trace_pid_show,
2737
	.stop = p_stop,
2738
};
2739
 
2740
static const struct seq_operations show_set_no_pid_seq_ops = {
2741
	.start = np_start,
2742
	.next = np_next,
2743
	.show = trace_pid_show,
2744
	.stop = p_stop,
2745
};
2746
 
2747
static const struct file_operations ftrace_avail_fops = {
2748
	.open = ftrace_event_avail_open,
2749
	.read = seq_read,
2750
	.llseek = seq_lseek,
2751
	.release = seq_release,
2752
};
2753
 
2754
static const struct file_operations ftrace_set_event_fops = {
2755
	.open = ftrace_event_set_open,
2756
	.read = seq_read,
2757
	.write = ftrace_event_write,
2758
	.llseek = seq_lseek,
2759
	.release = ftrace_event_release,
2760
};
2761
 
2762
static const struct file_operations ftrace_set_event_pid_fops = {
2763
	.open = ftrace_event_set_pid_open,
2764
	.read = seq_read,
2765
	.write = ftrace_event_pid_write,
2766
	.llseek = seq_lseek,
2767
	.release = ftrace_event_release,
2768
};
2769
 
2770
static const struct file_operations ftrace_set_event_notrace_pid_fops = {
2771
	.open = ftrace_event_set_npid_open,
2772
	.read = seq_read,
2773
	.write = ftrace_event_npid_write,
2774
	.llseek = seq_lseek,
2775
	.release = ftrace_event_release,
2776
};
2777
 
2778
static const struct file_operations ftrace_enable_fops = {
2779
	.open = tracing_open_file_tr,
2780
	.read = event_enable_read,
2781
	.write = event_enable_write,
2782
	.release = tracing_release_file_tr,
2783
	.llseek = default_llseek,
2784
};
2785
 
2786
static const struct file_operations ftrace_event_format_fops = {
2787
	.open = trace_format_open,
2788
	.read = seq_read,
2789
	.llseek = seq_lseek,
2790
	.release = seq_release,
2791
};
2792
 
2793
#ifdef CONFIG_PERF_EVENTS
2794
static const struct file_operations ftrace_event_id_fops = {
2795
	.read = event_id_read,
2796
	.llseek = default_llseek,
2797
};
2798
#endif
2799
 
2800
static const struct file_operations ftrace_event_filter_fops = {
2801
	.open = tracing_open_file_tr,
2802
	.read = event_filter_read,
2803
	.write = event_filter_write,
2804
	.release = tracing_release_file_tr,
2805
	.llseek = default_llseek,
2806
};
2807
 
2808
static const struct file_operations ftrace_subsystem_filter_fops = {
2809
	.open = subsystem_open,
2810
	.read = subsystem_filter_read,
2811
	.write = subsystem_filter_write,
2812
	.llseek = default_llseek,
2813
	.release = subsystem_release,
2814
};
2815
 
2816
static const struct file_operations ftrace_system_enable_fops = {
2817
	.open = subsystem_open,
2818
	.read = system_enable_read,
2819
	.write = system_enable_write,
2820
	.llseek = default_llseek,
2821
	.release = subsystem_release,
2822
};
2823
 
2824
static const struct file_operations ftrace_tr_enable_fops = {
2825
	.open = system_tr_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_show_header_page_fops = {
2833
	.open = tracing_open_generic_tr,
2834
	.read = show_header_page_file,
2835
	.llseek = default_llseek,
2836
	.release = tracing_release_generic_tr,
2837
};
2838
 
2839
static const struct file_operations ftrace_show_header_event_fops = {
2840
	.open = tracing_open_generic_tr,
2841
	.read = show_header_event_file,
2842
	.llseek = default_llseek,
2843
	.release = tracing_release_generic_tr,
2844
};
2845
 
2846
static int
2847
ftrace_event_open(struct inode *inode, struct file *file,
2848
		  const struct seq_operations *seq_ops)
2849
{
2850
	struct seq_file *m;
2851
	int ret;
2852
 
2853
	ret = security_locked_down(LOCKDOWN_TRACEFS);
2854
	if (ret)
2855
		return ret;
2856
 
2857
	ret = seq_open(file, seq_ops);
2858
	if (ret < 0)
2859
		return ret;
2860
	m = file->private_data;
2861
	/* copy tr over to seq ops */
2862
	m->private = inode->i_private;
2863
 
2864
	return ret;
2865
}
2866
 
2867
static int ftrace_event_release(struct inode *inode, struct file *file)
2868
{
2869
	struct trace_array *tr = inode->i_private;
2870
 
2871
	trace_array_put(tr);
2872
 
2873
	return seq_release(inode, file);
2874
}
2875
 
2876
static int
2877
ftrace_event_avail_open(struct inode *inode, struct file *file)
2878
{
2879
	const struct seq_operations *seq_ops = &show_event_seq_ops;
2880
 
2881
	/* Checks for tracefs lockdown */
2882
	return ftrace_event_open(inode, file, seq_ops);
2883
}
2884
 
2885
static int
2886
ftrace_event_set_open(struct inode *inode, struct file *file)
2887
{
2888
	const struct seq_operations *seq_ops = &show_set_event_seq_ops;
2889
	struct trace_array *tr = inode->i_private;
2890
	int ret;
2891
 
2892
	ret = tracing_check_open_get_tr(tr);
2893
	if (ret)
2894
		return ret;
2895
 
2896
	if ((file->f_mode & FMODE_WRITE) &&
2897
	    (file->f_flags & O_TRUNC))
2898
		ftrace_clear_events(tr);
2899
 
2900
	ret = ftrace_event_open(inode, file, seq_ops);
2901
	if (ret < 0)
2902
		trace_array_put(tr);
2903
	return ret;
2904
}
2905
 
2906
static int
2907
ftrace_event_set_pid_open(struct inode *inode, struct file *file)
2908
{
2909
	const struct seq_operations *seq_ops = &show_set_pid_seq_ops;
2910
	struct trace_array *tr = inode->i_private;
2911
	int ret;
2912
 
2913
	ret = tracing_check_open_get_tr(tr);
2914
	if (ret)
2915
		return ret;
2916
 
2917
	if ((file->f_mode & FMODE_WRITE) &&
2918
	    (file->f_flags & O_TRUNC))
2919
		ftrace_clear_event_pids(tr, TRACE_PIDS);
2920
 
2921
	ret = ftrace_event_open(inode, file, seq_ops);
2922
	if (ret < 0)
2923
		trace_array_put(tr);
2924
	return ret;
2925
}
2926
 
2927
static int
2928
ftrace_event_set_npid_open(struct inode *inode, struct file *file)
2929
{
2930
	const struct seq_operations *seq_ops = &show_set_no_pid_seq_ops;
2931
	struct trace_array *tr = inode->i_private;
2932
	int ret;
2933
 
2934
	ret = tracing_check_open_get_tr(tr);
2935
	if (ret)
2936
		return ret;
2937
 
2938
	if ((file->f_mode & FMODE_WRITE) &&
2939
	    (file->f_flags & O_TRUNC))
2940
		ftrace_clear_event_pids(tr, TRACE_NO_PIDS);
2941
 
2942
	ret = ftrace_event_open(inode, file, seq_ops);
2943
	if (ret < 0)
2944
		trace_array_put(tr);
2945
	return ret;
2946
}
2947
 
2948
static struct event_subsystem *
2949
create_new_subsystem(const char *name)
2950
{
2951
	struct event_subsystem *system;
2952
 
2953
	/* need to create new entry */
2954
	system = kmalloc(sizeof(*system), GFP_KERNEL);
2955
	if (!system)
2956
		return NULL;
2957
 
2958
	system->ref_count = 1;
2959
 
2960
	/* Only allocate if dynamic (kprobes and modules) */
2961
	system->name = kstrdup_const(name, GFP_KERNEL);
2962
	if (!system->name)
2963
		goto out_free;
2964
 
2965
	system->filter = kzalloc(sizeof(struct event_filter), GFP_KERNEL);
2966
	if (!system->filter)
2967
		goto out_free;
2968
 
2969
	list_add(&system->list, &event_subsystems);
2970
 
2971
	return system;
2972
 
2973
 out_free:
2974
	kfree_const(system->name);
2975
	kfree(system);
2976
	return NULL;
2977
}
2978
 
2979
static int system_callback(const char *name, umode_t *mode, void **data,
2980
		    const struct file_operations **fops)
2981
{
2982
	if (strcmp(name, "filter") == 0)
2983
		*fops = &ftrace_subsystem_filter_fops;
2984
 
2985
	else if (strcmp(name, "enable") == 0)
2986
		*fops = &ftrace_system_enable_fops;
2987
 
2988
	else
2989
		return 0;
2990
 
2991
	*mode = TRACE_MODE_WRITE;
2992
	return 1;
2993
}
2994
 
2995
static struct eventfs_inode *
2996
event_subsystem_dir(struct trace_array *tr, const char *name,
2997
		    struct trace_event_file *file, struct eventfs_inode *parent)
2998
{
2999
	struct event_subsystem *system, *iter;
3000
	struct trace_subsystem_dir *dir;
3001
	struct eventfs_inode *ei;
3002
	int nr_entries;
3003
	static struct eventfs_entry system_entries[] = {
3004
		{
3005
			.name		= "filter",
3006
			.callback	= system_callback,
3007
		},
3008
		{
3009
			.name		= "enable",
3010
			.callback	= system_callback,
3011
		}
3012
	};
3013
 
3014
	/* First see if we did not already create this dir */
3015
	list_for_each_entry(dir, &tr->systems, list) {
3016
		system = dir->subsystem;
3017
		if (strcmp(system->name, name) == 0) {
3018
			dir->nr_events++;
3019
			file->system = dir;
3020
			return dir->ei;
3021
		}
3022
	}
3023
 
3024
	/* Now see if the system itself exists. */
3025
	system = NULL;
3026
	list_for_each_entry(iter, &event_subsystems, list) {
3027
		if (strcmp(iter->name, name) == 0) {
3028
			system = iter;
3029
			break;
3030
		}
3031
	}
3032
 
3033
	dir = kmalloc(sizeof(*dir), GFP_KERNEL);
3034
	if (!dir)
3035
		goto out_fail;
3036
 
3037
	if (!system) {
3038
		system = create_new_subsystem(name);
3039
		if (!system)
3040
			goto out_free;
3041
	} else
3042
		__get_system(system);
3043
 
3044
	/* ftrace only has directories no files */
3045
	if (strcmp(name, "ftrace") == 0)
3046
		nr_entries = 0;
3047
	else
3048
		nr_entries = ARRAY_SIZE(system_entries);
3049
 
3050
	ei = eventfs_create_dir(name, parent, system_entries, nr_entries, dir);
3051
	if (IS_ERR(ei)) {
3052
		pr_warn("Failed to create system directory %s\n", name);
3053
		__put_system(system);
3054
		goto out_free;
3055
	}
3056
 
3057
	dir->ei = ei;
3058
	dir->tr = tr;
3059
	dir->ref_count = 1;
3060
	dir->nr_events = 1;
3061
	dir->subsystem = system;
3062
	file->system = dir;
3063
 
3064
	list_add(&dir->list, &tr->systems);
3065
 
3066
	return dir->ei;
3067
 
3068
 out_free:
3069
	kfree(dir);
3070
 out_fail:
3071
	/* Only print this message if failed on memory allocation */
3072
	if (!dir || !system)
3073
		pr_warn("No memory to create event subsystem %s\n", name);
3074
	return NULL;
3075
}
3076
 
3077
static int
3078
event_define_fields(struct trace_event_call *call)
3079
{
3080
	struct list_head *head;
3081
	int ret = 0;
3082
 
3083
	/*
3084
	 * Other events may have the same class. Only update
3085
	 * the fields if they are not already defined.
3086
	 */
3087
	head = trace_get_fields(call);
3088
	if (list_empty(head)) {
3089
		struct trace_event_fields *field = call->class->fields_array;
3090
		unsigned int offset = sizeof(struct trace_entry);
3091
 
3092
		for (; field->type; field++) {
3093
			if (field->type == TRACE_FUNCTION_TYPE) {
3094
				field->define_fields(call);
3095
				break;
3096
			}
3097
 
3098
			offset = ALIGN(offset, field->align);
3099
			ret = trace_define_field_ext(call, field->type, field->name,
3100
						 offset, field->size,
3101
						 field->is_signed, field->filter_type,
3102
						 field->len, field->needs_test);
3103
			if (WARN_ON_ONCE(ret)) {
3104
				pr_err("error code is %d\n", ret);
3105
				break;
3106
			}
3107
 
3108
			offset += field->size;
3109
		}
3110
	}
3111
 
3112
	return ret;
3113
}
3114
 
3115
static int event_callback(const char *name, umode_t *mode, void **data,
3116
			  const struct file_operations **fops)
3117
{
3118
	struct trace_event_file *file = *data;
3119
	struct trace_event_call *call = file->event_call;
3120
 
3121
	if (strcmp(name, "format") == 0) {
3122
		*mode = TRACE_MODE_READ;
3123
		*fops = &ftrace_event_format_fops;
3124
		return 1;
3125
	}
3126
 
3127
	/*
3128
	 * Only event directories that can be enabled should have
3129
	 * triggers or filters, with the exception of the "print"
3130
	 * event that can have a "trigger" file.
3131
	 */
3132
	if (!(call->flags & TRACE_EVENT_FL_IGNORE_ENABLE)) {
3133
		if (call->class->reg && strcmp(name, "enable") == 0) {
3134
			*mode = TRACE_MODE_WRITE;
3135
			*fops = &ftrace_enable_fops;
3136
			return 1;
3137
		}
3138
 
3139
		if (strcmp(name, "filter") == 0) {
3140
			*mode = TRACE_MODE_WRITE;
3141
			*fops = &ftrace_event_filter_fops;
3142
			return 1;
3143
		}
3144
	}
3145
 
3146
	if (!(call->flags & TRACE_EVENT_FL_IGNORE_ENABLE) ||
3147
	    strcmp(trace_event_name(call), "print") == 0) {
3148
		if (strcmp(name, "trigger") == 0) {
3149
			*mode = TRACE_MODE_WRITE;
3150
			*fops = &event_trigger_fops;
3151
			return 1;
3152
		}
3153
	}
3154
 
3155
#ifdef CONFIG_PERF_EVENTS
3156
	if (call->event.type && call->class->reg &&
3157
	    strcmp(name, "id") == 0) {
3158
		*mode = TRACE_MODE_READ;
3159
		*data = (void *)(long)call->event.type;
3160
		*fops = &ftrace_event_id_fops;
3161
		return 1;
3162
	}
3163
#endif
3164
 
3165
#ifdef CONFIG_HIST_TRIGGERS
3166
	if (strcmp(name, "hist") == 0) {
3167
		*mode = TRACE_MODE_READ;
3168
		*fops = &event_hist_fops;
3169
		return 1;
3170
	}
3171
#endif
3172
#ifdef CONFIG_HIST_TRIGGERS_DEBUG
3173
	if (strcmp(name, "hist_debug") == 0) {
3174
		*mode = TRACE_MODE_READ;
3175
		*fops = &event_hist_debug_fops;
3176
		return 1;
3177
	}
3178
#endif
3179
#ifdef CONFIG_TRACE_EVENT_INJECT
3180
	if (call->event.type && call->class->reg &&
3181
	    strcmp(name, "inject") == 0) {
3182
		*mode = 0200;
3183
		*fops = &event_inject_fops;
3184
		return 1;
3185
	}
3186
#endif
3187
	return 0;
3188
}
3189
 
3190
/* The file is incremented on creation and freeing the enable file decrements it */
3191
static void event_release(const char *name, void *data)
3192
{
3193
	struct trace_event_file *file = data;
3194
 
3195
	event_file_put(file);
3196
}
3197
 
3198
static int
3199
event_create_dir(struct eventfs_inode *parent, struct trace_event_file *file)
3200
{
3201
	struct trace_event_call *call = file->event_call;
3202
	struct trace_array *tr = file->tr;
3203
	struct eventfs_inode *e_events;
3204
	struct eventfs_inode *ei;
3205
	const char *name;
3206
	int nr_entries;
3207
	int ret;
3208
	static struct eventfs_entry event_entries[] = {
3209
		{
3210
			.name		= "enable",
3211
			.callback	= event_callback,
3212
			.release	= event_release,
3213
		},
3214
		{
3215
			.name		= "filter",
3216
			.callback	= event_callback,
3217
		},
3218
		{
3219
			.name		= "trigger",
3220
			.callback	= event_callback,
3221
		},
3222
		{
3223
			.name		= "format",
3224
			.callback	= event_callback,
3225
		},
3226
#ifdef CONFIG_PERF_EVENTS
3227
		{
3228
			.name		= "id",
3229
			.callback	= event_callback,
3230
		},
3231
#endif
3232
#ifdef CONFIG_HIST_TRIGGERS
3233
		{
3234
			.name		= "hist",
3235
			.callback	= event_callback,
3236
		},
3237
#endif
3238
#ifdef CONFIG_HIST_TRIGGERS_DEBUG
3239
		{
3240
			.name		= "hist_debug",
3241
			.callback	= event_callback,
3242
		},
3243
#endif
3244
#ifdef CONFIG_TRACE_EVENT_INJECT
3245
		{
3246
			.name		= "inject",
3247
			.callback	= event_callback,
3248
		},
3249
#endif
3250
	};
3251
 
3252
	/*
3253
	 * If the trace point header did not define TRACE_SYSTEM
3254
	 * then the system would be called "TRACE_SYSTEM". This should
3255
	 * never happen.
3256
	 */
3257
	if (WARN_ON_ONCE(strcmp(call->class->system, TRACE_SYSTEM) == 0))
3258
		return -ENODEV;
3259
 
3260
	e_events = event_subsystem_dir(tr, call->class->system, file, parent);
3261
	if (!e_events)
3262
		return -ENOMEM;
3263
 
3264
	nr_entries = ARRAY_SIZE(event_entries);
3265
 
3266
	name = trace_event_name(call);
3267
	ei = eventfs_create_dir(name, e_events, event_entries, nr_entries, file);
3268
	if (IS_ERR(ei)) {
3269
		pr_warn("Could not create tracefs '%s' directory\n", name);
3270
		return -1;
3271
	}
3272
 
3273
	file->ei = ei;
3274
 
3275
	ret = event_define_fields(call);
3276
	if (ret < 0) {
3277
		pr_warn("Could not initialize trace point events/%s\n", name);
3278
		return ret;
3279
	}
3280
 
3281
	/* Gets decremented on freeing of the "enable" file */
3282
	event_file_get(file);
3283
 
3284
	return 0;
3285
}
3286
 
3287
static void remove_event_from_tracers(struct trace_event_call *call)
3288
{
3289
	struct trace_event_file *file;
3290
	struct trace_array *tr;
3291
 
3292
	do_for_each_event_file_safe(tr, file) {
3293
		if (file->event_call != call)
3294
			continue;
3295
 
3296
		remove_event_file_dir(file);
3297
		/*
3298
		 * The do_for_each_event_file_safe() is
3299
		 * a double loop. After finding the call for this
3300
		 * trace_array, we use break to jump to the next
3301
		 * trace_array.
3302
		 */
3303
		break;
3304
	} while_for_each_event_file();
3305
}
3306
 
3307
static void event_remove(struct trace_event_call *call)
3308
{
3309
	struct trace_array *tr;
3310
	struct trace_event_file *file;
3311
 
3312
	do_for_each_event_file(tr, file) {
3313
		if (file->event_call != call)
3314
			continue;
3315
 
3316
		if (file->flags & EVENT_FILE_FL_WAS_ENABLED)
3317
			tr->clear_trace = true;
3318
 
3319
		ftrace_event_enable_disable(file, 0);
3320
		/*
3321
		 * The do_for_each_event_file() is
3322
		 * a double loop. After finding the call for this
3323
		 * trace_array, we use break to jump to the next
3324
		 * trace_array.
3325
		 */
3326
		break;
3327
	} while_for_each_event_file();
3328
 
3329
	if (call->event.funcs)
3330
		__unregister_trace_event(&call->event);
3331
	remove_event_from_tracers(call);
3332
	list_del(&call->list);
3333
}
3334
 
3335
static int event_init(struct trace_event_call *call)
3336
{
3337
	int ret = 0;
3338
	const char *name;
3339
 
3340
	name = trace_event_name(call);
3341
	if (WARN_ON(!name))
3342
		return -EINVAL;
3343
 
3344
	if (call->class->raw_init) {
3345
		ret = call->class->raw_init(call);
3346
		if (ret < 0 && ret != -ENOSYS)
3347
			pr_warn("Could not initialize trace events/%s\n", name);
3348
	}
3349
 
3350
	return ret;
3351
}
3352
 
3353
static int
3354
__register_event(struct trace_event_call *call, struct module *mod)
3355
{
3356
	int ret;
3357
 
3358
	ret = event_init(call);
3359
	if (ret < 0)
3360
		return ret;
3361
 
3362
	down_write(&trace_event_sem);
3363
	list_add(&call->list, &ftrace_events);
3364
	up_write(&trace_event_sem);
3365
 
3366
	if (call->flags & TRACE_EVENT_FL_DYNAMIC)
3367
		atomic_set(&call->refcnt, 0);
3368
	else
3369
		call->module = mod;
3370
 
3371
	return 0;
3372
}
3373
 
3374
static char *eval_replace(char *ptr, struct trace_eval_map *map, int len)
3375
{
3376
	int rlen;
3377
	int elen;
3378
 
3379
	/* Find the length of the eval value as a string */
3380
	elen = snprintf(ptr, 0, "%ld", map->eval_value);
3381
	/* Make sure there's enough room to replace the string with the value */
3382
	if (len < elen)
3383
		return NULL;
3384
 
3385
	snprintf(ptr, elen + 1, "%ld", map->eval_value);
3386
 
3387
	/* Get the rest of the string of ptr */
3388
	rlen = strlen(ptr + len);
3389
	memmove(ptr + elen, ptr + len, rlen);
3390
	/* Make sure we end the new string */
3391
	ptr[elen + rlen] = 0;
3392
 
3393
	return ptr + elen;
3394
}
3395
 
3396
static void update_event_printk(struct trace_event_call *call,
3397
				struct trace_eval_map *map)
3398
{
3399
	char *ptr;
3400
	int quote = 0;
3401
	int len = strlen(map->eval_string);
3402
 
3403
	for (ptr = call->print_fmt; *ptr; ptr++) {
3404
		if (*ptr == '\\') {
3405
			ptr++;
3406
			/* paranoid */
3407
			if (!*ptr)
3408
				break;
3409
			continue;
3410
		}
3411
		if (*ptr == '"') {
3412
			quote ^= 1;
3413
			continue;
3414
		}
3415
		if (quote)
3416
			continue;
3417
		if (isdigit(*ptr)) {
3418
			/* skip numbers */
3419
			do {
3420
				ptr++;
3421
				/* Check for alpha chars like ULL */
3422
			} while (isalnum(*ptr));
3423
			if (!*ptr)
3424
				break;
3425
			/*
3426
			 * A number must have some kind of delimiter after
3427
			 * it, and we can ignore that too.
3428
			 */
3429
			continue;
3430
		}
3431
		if (isalpha(*ptr) || *ptr == '_') {
3432
			if (strncmp(map->eval_string, ptr, len) == 0 &&
3433
			    !isalnum(ptr[len]) && ptr[len] != '_') {
3434
				ptr = eval_replace(ptr, map, len);
3435
				/* enum/sizeof string smaller than value */
3436
				if (WARN_ON_ONCE(!ptr))
3437
					return;
3438
				/*
3439
				 * No need to decrement here, as eval_replace()
3440
				 * returns the pointer to the character passed
3441
				 * the eval, and two evals can not be placed
3442
				 * back to back without something in between.
3443
				 * We can skip that something in between.
3444
				 */
3445
				continue;
3446
			}
3447
		skip_more:
3448
			do {
3449
				ptr++;
3450
			} while (isalnum(*ptr) || *ptr == '_');
3451
			if (!*ptr)
3452
				break;
3453
			/*
3454
			 * If what comes after this variable is a '.' or
3455
			 * '->' then we can continue to ignore that string.
3456
			 */
3457
			if (*ptr == '.' || (ptr[0] == '-' && ptr[1] == '>')) {
3458
				ptr += *ptr == '.' ? 1 : 2;
3459
				if (!*ptr)
3460
					break;
3461
				goto skip_more;
3462
			}
3463
			/*
3464
			 * Once again, we can skip the delimiter that came
3465
			 * after the string.
3466
			 */
3467
			continue;
3468
		}
3469
	}
3470
}
3471
 
3472
static void add_str_to_module(struct module *module, char *str)
3473
{
3474
	struct module_string *modstr;
3475
 
3476
	modstr = kmalloc(sizeof(*modstr), GFP_KERNEL);
3477
 
3478
	/*
3479
	 * If we failed to allocate memory here, then we'll just
3480
	 * let the str memory leak when the module is removed.
3481
	 * If this fails to allocate, there's worse problems than
3482
	 * a leaked string on module removal.
3483
	 */
3484
	if (WARN_ON_ONCE(!modstr))
3485
		return;
3486
 
3487
	modstr->module = module;
3488
	modstr->str = str;
3489
 
3490
	list_add(&modstr->next, &module_strings);
3491
}
3492
 
3493
#define ATTRIBUTE_STR "__attribute__("
3494
#define ATTRIBUTE_STR_LEN (sizeof(ATTRIBUTE_STR) - 1)
3495
 
3496
/* Remove all __attribute__() from @type. Return allocated string or @type. */
3497
static char *sanitize_field_type(const char *type)
3498
{
3499
	char *attr, *tmp, *next, *ret = (char *)type;
3500
	int depth;
3501
 
3502
	next = (char *)type;
3503
	while ((attr = strstr(next, ATTRIBUTE_STR))) {
3504
		/* Retry if "__attribute__(" is a part of another word. */
3505
		if (attr != next && !isspace(attr[-1])) {
3506
			next = attr + ATTRIBUTE_STR_LEN;
3507
			continue;
3508
		}
3509
 
3510
		if (ret == type) {
3511
			ret = kstrdup(type, GFP_KERNEL);
3512
			if (WARN_ON_ONCE(!ret))
3513
				return NULL;
3514
			attr = ret + (attr - type);
3515
		}
3516
 
3517
		/* the ATTRIBUTE_STR already has the first '(' */
3518
		depth = 1;
3519
		next = attr + ATTRIBUTE_STR_LEN;
3520
		do {
3521
			tmp = strpbrk(next, "()");
3522
			/* There is unbalanced parentheses */
3523
			if (WARN_ON_ONCE(!tmp)) {
3524
				kfree(ret);
3525
				return (char *)type;
3526
			}
3527
 
3528
			if (*tmp == '(')
3529
				depth++;
3530
			else
3531
				depth--;
3532
			next = tmp + 1;
3533
		} while (depth > 0);
3534
		next = skip_spaces(next);
3535
		strcpy(attr, next);
3536
		next = attr;
3537
	}
3538
	return ret;
3539
}
3540
 
3541
static char *find_replacable_eval(const char *type, const char *eval_string,
3542
				  int len)
3543
{
3544
	char *ptr;
3545
 
3546
	if (!eval_string)
3547
		return NULL;
3548
 
3549
	ptr = strchr(type, '[');
3550
	if (!ptr)
3551
		return NULL;
3552
	ptr++;
3553
 
3554
	if (!isalpha(*ptr) && *ptr != '_')
3555
		return NULL;
3556
 
3557
	if (strncmp(eval_string, ptr, len) != 0)
3558
		return NULL;
3559
 
3560
	return ptr;
3561
}
3562
 
3563
static void update_event_fields(struct trace_event_call *call,
3564
				struct trace_eval_map *map)
3565
{
3566
	struct ftrace_event_field *field;
3567
	const char *eval_string = NULL;
3568
	struct list_head *head;
3569
	int len = 0;
3570
	char *ptr;
3571
	char *str;
3572
 
3573
	/* Dynamic events should never have field maps */
3574
	if (call->flags & TRACE_EVENT_FL_DYNAMIC)
3575
		return;
3576
 
3577
	if (map) {
3578
		eval_string = map->eval_string;
3579
		len = strlen(map->eval_string);
3580
	}
3581
 
3582
	head = trace_get_fields(call);
3583
	list_for_each_entry(field, head, link) {
3584
		str = sanitize_field_type(field->type);
3585
		if (!str)
3586
			return;
3587
 
3588
		ptr = find_replacable_eval(str, eval_string, len);
3589
		if (ptr) {
3590
			if (str == field->type) {
3591
				str = kstrdup(field->type, GFP_KERNEL);
3592
				if (WARN_ON_ONCE(!str))
3593
					return;
3594
				ptr = str + (ptr - field->type);
3595
			}
3596
 
3597
			ptr = eval_replace(ptr, map, len);
3598
			/* enum/sizeof string smaller than value */
3599
			if (WARN_ON_ONCE(!ptr)) {
3600
				kfree(str);
3601
				continue;
3602
			}
3603
		}
3604
 
3605
		if (str == field->type)
3606
			continue;
3607
		/*
3608
		 * If the event is part of a module, then we need to free the string
3609
		 * when the module is removed. Otherwise, it will stay allocated
3610
		 * until a reboot.
3611
		 */
3612
		if (call->module)
3613
			add_str_to_module(call->module, str);
3614
 
3615
		field->type = str;
3616
		if (field->filter_type == FILTER_OTHER)
3617
			field->filter_type = filter_assign_type(field->type);
3618
	}
3619
}
3620
 
3621
/* Update all events for replacing eval and sanitizing */
3622
void trace_event_update_all(struct trace_eval_map **map, int len)
3623
{
3624
	struct trace_event_call *call, *p;
3625
	const char *last_system = NULL;
3626
	bool first = false;
3627
	bool updated;
3628
	int last_i;
3629
	int i;
3630
 
3631
	down_write(&trace_event_sem);
3632
	list_for_each_entry_safe(call, p, &ftrace_events, list) {
3633
		/* events are usually grouped together with systems */
3634
		if (!last_system || call->class->system != last_system) {
3635
			first = true;
3636
			last_i = 0;
3637
			last_system = call->class->system;
3638
		}
3639
 
3640
		updated = false;
3641
		/*
3642
		 * Since calls are grouped by systems, the likelihood that the
3643
		 * next call in the iteration belongs to the same system as the
3644
		 * previous call is high. As an optimization, we skip searching
3645
		 * for a map[] that matches the call's system if the last call
3646
		 * was from the same system. That's what last_i is for. If the
3647
		 * call has the same system as the previous call, then last_i
3648
		 * will be the index of the first map[] that has a matching
3649
		 * system.
3650
		 */
3651
		for (i = last_i; i < len; i++) {
3652
			if (call->class->system == map[i]->system) {
3653
				/* Save the first system if need be */
3654
				if (first) {
3655
					last_i = i;
3656
					first = false;
3657
				}
3658
				update_event_printk(call, map[i]);
3659
				update_event_fields(call, map[i]);
3660
				updated = true;
3661
			}
3662
		}
3663
		/* If not updated yet, update field for sanitizing. */
3664
		if (!updated)
3665
			update_event_fields(call, NULL);
3666
		cond_resched();
3667
	}
3668
	up_write(&trace_event_sem);
3669
}
3670
 
3671
static bool event_in_systems(struct trace_event_call *call,
3672
			     const char *systems)
3673
{
3674
	const char *system;
3675
	const char *p;
3676
 
3677
	if (!systems)
3678
		return true;
3679
 
3680
	system = call->class->system;
3681
	p = strstr(systems, system);
3682
	if (!p)
3683
		return false;
3684
 
3685
	if (p != systems && !isspace(*(p - 1)) && *(p - 1) != ',')
3686
		return false;
3687
 
3688
	p += strlen(system);
3689
	return !*p || isspace(*p) || *p == ',';
3690
}
3691
 
3692
#ifdef CONFIG_HIST_TRIGGERS
3693
/*
3694
 * Wake up waiter on the hist_poll_wq from irq_work because the hist trigger
3695
 * may happen in any context.
3696
 */
3697
static void hist_poll_event_irq_work(struct irq_work *work)
3698
{
3699
	wake_up_all(&hist_poll_wq);
3700
}
3701
 
3702
DEFINE_IRQ_WORK(hist_poll_work, hist_poll_event_irq_work);
3703
DECLARE_WAIT_QUEUE_HEAD(hist_poll_wq);
3704
#endif
3705
 
3706
static struct trace_event_file *
3707
trace_create_new_event(struct trace_event_call *call,
3708
		       struct trace_array *tr)
3709
{
3710
	struct trace_pid_list *no_pid_list;
3711
	struct trace_pid_list *pid_list;
3712
	struct trace_event_file *file;
3713
	unsigned int first;
3714
 
3715
	if (!event_in_systems(call, tr->system_names))
3716
		return NULL;
3717
 
3718
	file = kmem_cache_alloc(file_cachep, GFP_TRACE);
3719
	if (!file)
3720
		return ERR_PTR(-ENOMEM);
3721
 
3722
	pid_list = rcu_dereference_protected(tr->filtered_pids,
3723
					     lockdep_is_held(&event_mutex));
3724
	no_pid_list = rcu_dereference_protected(tr->filtered_no_pids,
3725
					     lockdep_is_held(&event_mutex));
3726
 
3727
	if (!trace_pid_list_first(pid_list, &first) ||
3728
	    !trace_pid_list_first(no_pid_list, &first))
3729
		file->flags |= EVENT_FILE_FL_PID_FILTER;
3730
 
3731
	file->event_call = call;
3732
	file->tr = tr;
3733
	atomic_set(&file->sm_ref, 0);
3734
	atomic_set(&file->tm_ref, 0);
3735
	INIT_LIST_HEAD(&file->triggers);
3736
	list_add(&file->list, &tr->events);
3737
	refcount_set(&file->ref, 1);
3738
 
3739
	return file;
3740
}
3741
 
3742
#define MAX_BOOT_TRIGGERS 32
3743
 
3744
static struct boot_triggers {
3745
	const char		*event;
3746
	char			*trigger;
3747
} bootup_triggers[MAX_BOOT_TRIGGERS];
3748
 
3749
static char bootup_trigger_buf[COMMAND_LINE_SIZE];
3750
static int nr_boot_triggers;
3751
 
3752
static __init int setup_trace_triggers(char *str)
3753
{
3754
	char *trigger;
3755
	char *buf;
3756
	int i;
3757
 
3758
	strscpy(bootup_trigger_buf, str, COMMAND_LINE_SIZE);
3759
	trace_set_ring_buffer_expanded(NULL);
3760
	disable_tracing_selftest("running event triggers");
3761
 
3762
	buf = bootup_trigger_buf;
3763
	for (i = 0; i < MAX_BOOT_TRIGGERS; i++) {
3764
		trigger = strsep(&buf, ",");
3765
		if (!trigger)
3766
			break;
3767
		bootup_triggers[i].event = strsep(&trigger, ".");
3768
		bootup_triggers[i].trigger = trigger;
3769
		if (!bootup_triggers[i].trigger)
3770
			break;
3771
	}
3772
 
3773
	nr_boot_triggers = i;
3774
	return 1;
3775
}
3776
__setup("trace_trigger=", setup_trace_triggers);
3777
 
3778
/* Add an event to a trace directory */
3779
static int
3780
__trace_add_new_event(struct trace_event_call *call, struct trace_array *tr)
3781
{
3782
	struct trace_event_file *file;
3783
 
3784
	file = trace_create_new_event(call, tr);
3785
	/*
3786
	 * trace_create_new_event() returns ERR_PTR(-ENOMEM) if failed
3787
	 * allocation, or NULL if the event is not part of the tr->system_names.
3788
	 * When the event is not part of the tr->system_names, return zero, not
3789
	 * an error.
3790
	 */
3791
	if (!file)
3792
		return 0;
3793
 
3794
	if (IS_ERR(file))
3795
		return PTR_ERR(file);
3796
 
3797
	if (eventdir_initialized)
3798
		return event_create_dir(tr->event_dir, file);
3799
	else
3800
		return event_define_fields(call);
3801
}
3802
 
3803
static void trace_early_triggers(struct trace_event_file *file, const char *name)
3804
{
3805
	int ret;
3806
	int i;
3807
 
3808
	for (i = 0; i < nr_boot_triggers; i++) {
3809
		if (strcmp(name, bootup_triggers[i].event))
3810
			continue;
3811
		mutex_lock(&event_mutex);
3812
		ret = trigger_process_regex(file, bootup_triggers[i].trigger);
3813
		mutex_unlock(&event_mutex);
3814
		if (ret)
3815
			pr_err("Failed to register trigger '%s' on event %s\n",
3816
			       bootup_triggers[i].trigger,
3817
			       bootup_triggers[i].event);
3818
	}
3819
}
3820
 
3821
/*
3822
 * Just create a descriptor for early init. A descriptor is required
3823
 * for enabling events at boot. We want to enable events before
3824
 * the filesystem is initialized.
3825
 */
3826
static int
3827
__trace_early_add_new_event(struct trace_event_call *call,
3828
			    struct trace_array *tr)
3829
{
3830
	struct trace_event_file *file;
3831
	int ret;
3832
 
3833
	file = trace_create_new_event(call, tr);
3834
	/*
3835
	 * trace_create_new_event() returns ERR_PTR(-ENOMEM) if failed
3836
	 * allocation, or NULL if the event is not part of the tr->system_names.
3837
	 * When the event is not part of the tr->system_names, return zero, not
3838
	 * an error.
3839
	 */
3840
	if (!file)
3841
		return 0;
3842
 
3843
	if (IS_ERR(file))
3844
		return PTR_ERR(file);
3845
 
3846
	ret = event_define_fields(call);
3847
	if (ret)
3848
		return ret;
3849
 
3850
	trace_early_triggers(file, trace_event_name(call));
3851
 
3852
	return 0;
3853
}
3854
 
3855
struct ftrace_module_file_ops;
3856
static void __add_event_to_tracers(struct trace_event_call *call);
3857
 
3858
/* Add an additional event_call dynamically */
3859
int trace_add_event_call(struct trace_event_call *call)
3860
{
3861
	int ret;
3862
	lockdep_assert_held(&event_mutex);
3863
 
3864
	guard(mutex)(&trace_types_lock);
3865
 
3866
	ret = __register_event(call, NULL);
3867
	if (ret < 0)
3868
		return ret;
3869
 
3870
	__add_event_to_tracers(call);
3871
	return ret;
3872
}
3873
EXPORT_SYMBOL_GPL(trace_add_event_call);
3874
 
3875
/*
3876
 * Must be called under locking of trace_types_lock, event_mutex and
3877
 * trace_event_sem.
3878
 */
3879
static void __trace_remove_event_call(struct trace_event_call *call)
3880
{
3881
	event_remove(call);
3882
	trace_destroy_fields(call);
3883
}
3884
 
3885
static int probe_remove_event_call(struct trace_event_call *call)
3886
{
3887
	struct trace_array *tr;
3888
	struct trace_event_file *file;
3889
 
3890
#ifdef CONFIG_PERF_EVENTS
3891
	if (call->perf_refcount)
3892
		return -EBUSY;
3893
#endif
3894
	do_for_each_event_file(tr, file) {
3895
		if (file->event_call != call)
3896
			continue;
3897
		/*
3898
		 * We can't rely on ftrace_event_enable_disable(enable => 0)
3899
		 * we are going to do, soft mode can suppress
3900
		 * TRACE_REG_UNREGISTER.
3901
		 */
3902
		if (file->flags & EVENT_FILE_FL_ENABLED)
3903
			goto busy;
3904
 
3905
		if (file->flags & EVENT_FILE_FL_WAS_ENABLED)
3906
			tr->clear_trace = true;
3907
		/*
3908
		 * The do_for_each_event_file_safe() is
3909
		 * a double loop. After finding the call for this
3910
		 * trace_array, we use break to jump to the next
3911
		 * trace_array.
3912
		 */
3913
		break;
3914
	} while_for_each_event_file();
3915
 
3916
	__trace_remove_event_call(call);
3917
 
3918
	return 0;
3919
 busy:
3920
	/* No need to clear the trace now */
3921
	list_for_each_entry(tr, &ftrace_trace_arrays, list) {
3922
		tr->clear_trace = false;
3923
	}
3924
	return -EBUSY;
3925
}
3926
 
3927
/* Remove an event_call */
3928
int trace_remove_event_call(struct trace_event_call *call)
3929
{
3930
	int ret;
3931
 
3932
	lockdep_assert_held(&event_mutex);
3933
 
3934
	mutex_lock(&trace_types_lock);
3935
	down_write(&trace_event_sem);
3936
	ret = probe_remove_event_call(call);
3937
	up_write(&trace_event_sem);
3938
	mutex_unlock(&trace_types_lock);
3939
 
3940
	return ret;
3941
}
3942
EXPORT_SYMBOL_GPL(trace_remove_event_call);
3943
 
3944
#define for_each_event(event, start, end)			\
3945
	for (event = start;					\
3946
	     (unsigned long)event < (unsigned long)end;		\
3947
	     event++)
3948
 
3949
#ifdef CONFIG_MODULES
3950
static void update_mod_cache(struct trace_array *tr, struct module *mod)
3951
{
3952
	struct event_mod_load *event_mod, *n;
3953
 
3954
	list_for_each_entry_safe(event_mod, n, &tr->mod_events, list) {
3955
		if (strcmp(event_mod->module, mod->name) != 0)
3956
			continue;
3957
 
3958
		__ftrace_set_clr_event_nolock(tr, event_mod->match,
3959
					      event_mod->system,
3960
					      event_mod->event, 1, mod->name);
3961
		free_event_mod(event_mod);
3962
	}
3963
}
3964
 
3965
static void update_cache_events(struct module *mod)
3966
{
3967
	struct trace_array *tr;
3968
 
3969
	list_for_each_entry(tr, &ftrace_trace_arrays, list)
3970
		update_mod_cache(tr, mod);
3971
}
3972
 
3973
static void trace_module_add_events(struct module *mod)
3974
{
3975
	struct trace_event_call **call, **start, **end;
3976
 
3977
	if (!mod->num_trace_events)
3978
		return;
3979
 
3980
	/* Don't add infrastructure for mods without tracepoints */
3981
	if (trace_module_has_bad_taint(mod)) {
3982
		pr_err("%s: module has bad taint, not creating trace events\n",
3983
		       mod->name);
3984
		return;
3985
	}
3986
 
3987
	start = mod->trace_events;
3988
	end = mod->trace_events + mod->num_trace_events;
3989
 
3990
	for_each_event(call, start, end) {
3991
		__register_event(*call, mod);
3992
		__add_event_to_tracers(*call);
3993
	}
3994
 
3995
	update_cache_events(mod);
3996
}
3997
 
3998
static void trace_module_remove_events(struct module *mod)
3999
{
4000
	struct trace_event_call *call, *p;
4001
	struct module_string *modstr, *m;
4002
 
4003
	down_write(&trace_event_sem);
4004
	list_for_each_entry_safe(call, p, &ftrace_events, list) {
4005
		if ((call->flags & TRACE_EVENT_FL_DYNAMIC) || !call->module)
4006
			continue;
4007
		if (call->module == mod)
4008
			__trace_remove_event_call(call);
4009
	}
4010
	/* Check for any strings allocated for this module */
4011
	list_for_each_entry_safe(modstr, m, &module_strings, next) {
4012
		if (modstr->module != mod)
4013
			continue;
4014
		list_del(&modstr->next);
4015
		kfree(modstr->str);
4016
		kfree(modstr);
4017
	}
4018
	up_write(&trace_event_sem);
4019
 
4020
	/*
4021
	 * It is safest to reset the ring buffer if the module being unloaded
4022
	 * registered any events that were used. The only worry is if
4023
	 * a new module gets loaded, and takes on the same id as the events
4024
	 * of this module. When printing out the buffer, traced events left
4025
	 * over from this module may be passed to the new module events and
4026
	 * unexpected results may occur.
4027
	 */
4028
	tracing_reset_all_online_cpus_unlocked();
4029
}
4030
 
4031
static int trace_module_notify(struct notifier_block *self,
4032
			       unsigned long val, void *data)
4033
{
4034
	struct module *mod = data;
4035
 
4036
	mutex_lock(&event_mutex);
4037
	mutex_lock(&trace_types_lock);
4038
	switch (val) {
4039
	case MODULE_STATE_COMING:
4040
		trace_module_add_events(mod);
4041
		break;
4042
	case MODULE_STATE_GOING:
4043
		trace_module_remove_events(mod);
4044
		break;
4045
	}
4046
	mutex_unlock(&trace_types_lock);
4047
	mutex_unlock(&event_mutex);
4048
 
4049
	return NOTIFY_OK;
4050
}
4051
 
4052
static struct notifier_block trace_module_nb = {
4053
	.notifier_call = trace_module_notify,
4054
	.priority = 1, /* higher than trace.c module notify */
4055
};
4056
#endif /* CONFIG_MODULES */
4057
 
4058
/* Create a new event directory structure for a trace directory. */
4059
static void
4060
__trace_add_event_dirs(struct trace_array *tr)
4061
{
4062
	struct trace_event_call *call;
4063
	int ret;
4064
 
4065
	lockdep_assert_held(&trace_event_sem);
4066
 
4067
	list_for_each_entry(call, &ftrace_events, list) {
4068
		ret = __trace_add_new_event(call, tr);
4069
		if (ret < 0)
4070
			pr_warn("Could not create directory for event %s\n",
4071
				trace_event_name(call));
4072
	}
4073
}
4074
 
4075
/* Returns any file that matches the system and event */
4076
struct trace_event_file *
4077
__find_event_file(struct trace_array *tr, const char *system, const char *event)
4078
{
4079
	struct trace_event_file *file;
4080
	struct trace_event_call *call;
4081
	const char *name;
4082
 
4083
	list_for_each_entry(file, &tr->events, list) {
4084
 
4085
		call = file->event_call;
4086
		name = trace_event_name(call);
4087
 
4088
		if (!name || !call->class)
4089
			continue;
4090
 
4091
		if (strcmp(event, name) == 0 &&
4092
		    strcmp(system, call->class->system) == 0)
4093
			return file;
4094
	}
4095
	return NULL;
4096
}
4097
 
4098
/* Returns valid trace event files that match system and event */
4099
struct trace_event_file *
4100
find_event_file(struct trace_array *tr, const char *system, const char *event)
4101
{
4102
	struct trace_event_file *file;
4103
 
4104
	file = __find_event_file(tr, system, event);
4105
	if (!file || !file->event_call->class->reg ||
4106
	    file->event_call->flags & TRACE_EVENT_FL_IGNORE_ENABLE)
4107
		return NULL;
4108
 
4109
	return file;
4110
}
4111
 
4112
/**
4113
 * trace_get_event_file - Find and return a trace event file
4114
 * @instance: The name of the trace instance containing the event
4115
 * @system: The name of the system containing the event
4116
 * @event: The name of the event
4117
 *
4118
 * Return a trace event file given the trace instance name, trace
4119
 * system, and trace event name.  If the instance name is NULL, it
4120
 * refers to the top-level trace array.
4121
 *
4122
 * This function will look it up and return it if found, after calling
4123
 * trace_array_get() to prevent the instance from going away, and
4124
 * increment the event's module refcount to prevent it from being
4125
 * removed.
4126
 *
4127
 * To release the file, call trace_put_event_file(), which will call
4128
 * trace_array_put() and decrement the event's module refcount.
4129
 *
4130
 * Return: The trace event on success, ERR_PTR otherwise.
4131
 */
4132
struct trace_event_file *trace_get_event_file(const char *instance,
4133
					      const char *system,
4134
					      const char *event)
4135
{
4136
	struct trace_array *tr = top_trace_array();
4137
	struct trace_event_file *file = NULL;
4138
	int ret = -EINVAL;
4139
 
4140
	if (instance) {
4141
		tr = trace_array_find_get(instance);
4142
		if (!tr)
4143
			return ERR_PTR(-ENOENT);
4144
	} else {
4145
		ret = trace_array_get(tr);
4146
		if (ret)
4147
			return ERR_PTR(ret);
4148
	}
4149
 
4150
	guard(mutex)(&event_mutex);
4151
 
4152
	file = find_event_file(tr, system, event);
4153
	if (!file) {
4154
		trace_array_put(tr);
4155
		return ERR_PTR(-EINVAL);
4156
	}
4157
 
4158
	/* Don't let event modules unload while in use */
4159
	ret = trace_event_try_get_ref(file->event_call);
4160
	if (!ret) {
4161
		trace_array_put(tr);
4162
		return ERR_PTR(-EBUSY);
4163
	}
4164
 
4165
	return file;
4166
}
4167
EXPORT_SYMBOL_GPL(trace_get_event_file);
4168
 
4169
/**
4170
 * trace_put_event_file - Release a file from trace_get_event_file()
4171
 * @file: The trace event file
4172
 *
4173
 * If a file was retrieved using trace_get_event_file(), this should
4174
 * be called when it's no longer needed.  It will cancel the previous
4175
 * trace_array_get() called by that function, and decrement the
4176
 * event's module refcount.
4177
 */
4178
void trace_put_event_file(struct trace_event_file *file)
4179
{
4180
	mutex_lock(&event_mutex);
4181
	trace_event_put_ref(file->event_call);
4182
	mutex_unlock(&event_mutex);
4183
 
4184
	trace_array_put(file->tr);
4185
}
4186
EXPORT_SYMBOL_GPL(trace_put_event_file);
4187
 
4188
#ifdef CONFIG_DYNAMIC_FTRACE
4189
 
4190
/* Avoid typos */
4191
#define ENABLE_EVENT_STR	"enable_event"
4192
#define DISABLE_EVENT_STR	"disable_event"
4193
 
4194
struct event_probe_data {
4195
	struct trace_event_file	*file;
4196
	unsigned long			count;
4197
	int				ref;
4198
	bool				enable;
4199
};
4200
 
4201
static void update_event_probe(struct event_probe_data *data)
4202
{
4203
	if (data->enable)
4204
		clear_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &data->file->flags);
4205
	else
4206
		set_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &data->file->flags);
4207
}
4208
 
4209
static void
4210
event_enable_probe(unsigned long ip, unsigned long parent_ip,
4211
		   struct trace_array *tr, struct ftrace_probe_ops *ops,
4212
		   void *data)
4213
{
4214
	struct ftrace_func_mapper *mapper = data;
4215
	struct event_probe_data *edata;
4216
	void **pdata;
4217
 
4218
	pdata = ftrace_func_mapper_find_ip(mapper, ip);
4219
	if (!pdata || !*pdata)
4220
		return;
4221
 
4222
	edata = *pdata;
4223
	update_event_probe(edata);
4224
}
4225
 
4226
static void
4227
event_enable_count_probe(unsigned long ip, unsigned long parent_ip,
4228
			 struct trace_array *tr, struct ftrace_probe_ops *ops,
4229
			 void *data)
4230
{
4231
	struct ftrace_func_mapper *mapper = data;
4232
	struct event_probe_data *edata;
4233
	void **pdata;
4234
 
4235
	pdata = ftrace_func_mapper_find_ip(mapper, ip);
4236
	if (!pdata || !*pdata)
4237
		return;
4238
 
4239
	edata = *pdata;
4240
 
4241
	if (!edata->count)
4242
		return;
4243
 
4244
	/* Skip if the event is in a state we want to switch to */
4245
	if (edata->enable == !(edata->file->flags & EVENT_FILE_FL_SOFT_DISABLED))
4246
		return;
4247
 
4248
	if (edata->count != -1)
4249
		(edata->count)--;
4250
 
4251
	update_event_probe(edata);
4252
}
4253
 
4254
static int
4255
event_enable_print(struct seq_file *m, unsigned long ip,
4256
		   struct ftrace_probe_ops *ops, void *data)
4257
{
4258
	struct ftrace_func_mapper *mapper = data;
4259
	struct event_probe_data *edata;
4260
	void **pdata;
4261
 
4262
	pdata = ftrace_func_mapper_find_ip(mapper, ip);
4263
 
4264
	if (WARN_ON_ONCE(!pdata || !*pdata))
4265
		return 0;
4266
 
4267
	edata = *pdata;
4268
 
4269
	seq_printf(m, "%ps:", (void *)ip);
4270
 
4271
	seq_printf(m, "%s:%s:%s",
4272
		   edata->enable ? ENABLE_EVENT_STR : DISABLE_EVENT_STR,
4273
		   edata->file->event_call->class->system,
4274
		   trace_event_name(edata->file->event_call));
4275
 
4276
	if (edata->count == -1)
4277
		seq_puts(m, ":unlimited\n");
4278
	else
4279
		seq_printf(m, ":count=%ld\n", edata->count);
4280
 
4281
	return 0;
4282
}
4283
 
4284
static int
4285
event_enable_init(struct ftrace_probe_ops *ops, struct trace_array *tr,
4286
		  unsigned long ip, void *init_data, void **data)
4287
{
4288
	struct ftrace_func_mapper *mapper = *data;
4289
	struct event_probe_data *edata = init_data;
4290
	int ret;
4291
 
4292
	if (!mapper) {
4293
		mapper = allocate_ftrace_func_mapper();
4294
		if (!mapper)
4295
			return -ENODEV;
4296
		*data = mapper;
4297
	}
4298
 
4299
	ret = ftrace_func_mapper_add_ip(mapper, ip, edata);
4300
	if (ret < 0)
4301
		return ret;
4302
 
4303
	edata->ref++;
4304
 
4305
	return 0;
4306
}
4307
 
4308
static int free_probe_data(void *data)
4309
{
4310
	struct event_probe_data *edata = data;
4311
 
4312
	edata->ref--;
4313
	if (!edata->ref) {
4314
		/* Remove soft mode */
4315
		__ftrace_event_enable_disable(edata->file, 0, 1);
4316
		trace_event_put_ref(edata->file->event_call);
4317
		kfree(edata);
4318
	}
4319
	return 0;
4320
}
4321
 
4322
static void
4323
event_enable_free(struct ftrace_probe_ops *ops, struct trace_array *tr,
4324
		  unsigned long ip, void *data)
4325
{
4326
	struct ftrace_func_mapper *mapper = data;
4327
	struct event_probe_data *edata;
4328
 
4329
	if (!ip) {
4330
		if (!mapper)
4331
			return;
4332
		free_ftrace_func_mapper(mapper, free_probe_data);
4333
		return;
4334
	}
4335
 
4336
	edata = ftrace_func_mapper_remove_ip(mapper, ip);
4337
 
4338
	if (WARN_ON_ONCE(!edata))
4339
		return;
4340
 
4341
	if (WARN_ON_ONCE(edata->ref <= 0))
4342
		return;
4343
 
4344
	free_probe_data(edata);
4345
}
4346
 
4347
static struct ftrace_probe_ops event_enable_probe_ops = {
4348
	.func			= event_enable_probe,
4349
	.print			= event_enable_print,
4350
	.init			= event_enable_init,
4351
	.free			= event_enable_free,
4352
};
4353
 
4354
static struct ftrace_probe_ops event_enable_count_probe_ops = {
4355
	.func			= event_enable_count_probe,
4356
	.print			= event_enable_print,
4357
	.init			= event_enable_init,
4358
	.free			= event_enable_free,
4359
};
4360
 
4361
static struct ftrace_probe_ops event_disable_probe_ops = {
4362
	.func			= event_enable_probe,
4363
	.print			= event_enable_print,
4364
	.init			= event_enable_init,
4365
	.free			= event_enable_free,
4366
};
4367
 
4368
static struct ftrace_probe_ops event_disable_count_probe_ops = {
4369
	.func			= event_enable_count_probe,
4370
	.print			= event_enable_print,
4371
	.init			= event_enable_init,
4372
	.free			= event_enable_free,
4373
};
4374
 
4375
static int
4376
event_enable_func(struct trace_array *tr, struct ftrace_hash *hash,
4377
		  char *glob, char *cmd, char *param, int enabled)
4378
{
4379
	struct trace_event_file *file;
4380
	struct ftrace_probe_ops *ops;
4381
	struct event_probe_data *data;
4382
	unsigned long count = -1;
4383
	const char *system;
4384
	const char *event;
4385
	char *number;
4386
	bool enable;
4387
	int ret;
4388
 
4389
	if (!tr)
4390
		return -ENODEV;
4391
 
4392
	/* hash funcs only work with set_ftrace_filter */
4393
	if (!enabled || !param)
4394
		return -EINVAL;
4395
 
4396
	system = strsep(&param, ":");
4397
	if (!param)
4398
		return -EINVAL;
4399
 
4400
	event = strsep(&param, ":");
4401
 
4402
	guard(mutex)(&event_mutex);
4403
 
4404
	file = find_event_file(tr, system, event);
4405
	if (!file)
4406
		return -EINVAL;
4407
 
4408
	enable = strcmp(cmd, ENABLE_EVENT_STR) == 0;
4409
 
4410
	if (enable)
4411
		ops = param ? &event_enable_count_probe_ops : &event_enable_probe_ops;
4412
	else
4413
		ops = param ? &event_disable_count_probe_ops : &event_disable_probe_ops;
4414
 
4415
	if (glob[0] == '!')
4416
		return unregister_ftrace_function_probe_func(glob+1, tr, ops);
4417
 
4418
	if (param) {
4419
		number = strsep(&param, ":");
4420
 
4421
		if (!strlen(number))
4422
			return -EINVAL;
4423
 
4424
		/*
4425
		 * We use the callback data field (which is a pointer)
4426
		 * as our counter.
4427
		 */
4428
		ret = kstrtoul(number, 0, &count);
4429
		if (ret)
4430
			return ret;
4431
	}
4432
 
4433
	/* Don't let event modules unload while probe registered */
4434
	ret = trace_event_try_get_ref(file->event_call);
4435
	if (!ret)
4436
		return -EBUSY;
4437
 
4438
	ret = __ftrace_event_enable_disable(file, 1, 1);
4439
	if (ret < 0)
4440
		goto out_put;
4441
 
4442
	ret = -ENOMEM;
4443
	data = kzalloc(sizeof(*data), GFP_KERNEL);
4444
	if (!data)
4445
		goto out_put;
4446
 
4447
	data->enable = enable;
4448
	data->count = count;
4449
	data->file = file;
4450
 
4451
	ret = register_ftrace_function_probe(glob, tr, ops, data);
4452
	/*
4453
	 * The above returns on success the # of functions enabled,
4454
	 * but if it didn't find any functions it returns zero.
4455
	 * Consider no functions a failure too.
4456
	 */
4457
 
4458
	/* Just return zero, not the number of enabled functions */
4459
	if (ret > 0)
4460
		return 0;
4461
 
4462
	kfree(data);
4463
 
4464
	if (!ret)
4465
		ret = -ENOENT;
4466
 
4467
	__ftrace_event_enable_disable(file, 0, 1);
4468
 out_put:
4469
	trace_event_put_ref(file->event_call);
4470
	return ret;
4471
}
4472
 
4473
static struct ftrace_func_command event_enable_cmd = {
4474
	.name			= ENABLE_EVENT_STR,
4475
	.func			= event_enable_func,
4476
};
4477
 
4478
static struct ftrace_func_command event_disable_cmd = {
4479
	.name			= DISABLE_EVENT_STR,
4480
	.func			= event_enable_func,
4481
};
4482
 
4483
static __init int register_event_cmds(void)
4484
{
4485
	int ret;
4486
 
4487
	ret = register_ftrace_command(&event_enable_cmd);
4488
	if (WARN_ON(ret < 0))
4489
		return ret;
4490
	ret = register_ftrace_command(&event_disable_cmd);
4491
	if (WARN_ON(ret < 0))
4492
		unregister_ftrace_command(&event_enable_cmd);
4493
	return ret;
4494
}
4495
#else
4496
static inline int register_event_cmds(void) { return 0; }
4497
#endif /* CONFIG_DYNAMIC_FTRACE */
4498
 
4499
/*
4500
 * The top level array and trace arrays created by boot-time tracing
4501
 * have already had its trace_event_file descriptors created in order
4502
 * to allow for early events to be recorded.
4503
 * This function is called after the tracefs has been initialized,
4504
 * and we now have to create the files associated to the events.
4505
 */
4506
static void __trace_early_add_event_dirs(struct trace_array *tr)
4507
{
4508
	struct trace_event_file *file;
4509
	int ret;
4510
 
4511
 
4512
	list_for_each_entry(file, &tr->events, list) {
4513
		ret = event_create_dir(tr->event_dir, file);
4514
		if (ret < 0)
4515
			pr_warn("Could not create directory for event %s\n",
4516
				trace_event_name(file->event_call));
4517
	}
4518
}
4519
 
4520
/*
4521
 * For early boot up, the top trace array and the trace arrays created
4522
 * by boot-time tracing require to have a list of events that can be
4523
 * enabled. This must be done before the filesystem is set up in order
4524
 * to allow events to be traced early.
4525
 */
4526
void __trace_early_add_events(struct trace_array *tr)
4527
{
4528
	struct trace_event_call *call;
4529
	int ret;
4530
 
4531
	list_for_each_entry(call, &ftrace_events, list) {
4532
		/* Early boot up should not have any modules loaded */
4533
		if (!(call->flags & TRACE_EVENT_FL_DYNAMIC) &&
4534
		    WARN_ON_ONCE(call->module))
4535
			continue;
4536
 
4537
		ret = __trace_early_add_new_event(call, tr);
4538
		if (ret < 0)
4539
			pr_warn("Could not create early event %s\n",
4540
				trace_event_name(call));
4541
	}
4542
}
4543
 
4544
/* Remove the event directory structure for a trace directory. */
4545
static void
4546
__trace_remove_event_dirs(struct trace_array *tr)
4547
{
4548
	struct trace_event_file *file, *next;
4549
 
4550
	list_for_each_entry_safe(file, next, &tr->events, list)
4551
		remove_event_file_dir(file);
4552
}
4553
 
4554
static void __add_event_to_tracers(struct trace_event_call *call)
4555
{
4556
	struct trace_array *tr;
4557
 
4558
	list_for_each_entry(tr, &ftrace_trace_arrays, list)
4559
		__trace_add_new_event(call, tr);
4560
}
4561
 
4562
extern struct trace_event_call *__start_ftrace_events[];
4563
extern struct trace_event_call *__stop_ftrace_events[];
4564
 
4565
static char bootup_event_buf[COMMAND_LINE_SIZE] __initdata;
4566
 
4567
static __init int setup_trace_event(char *str)
4568
{
4569
	strscpy(bootup_event_buf, str, COMMAND_LINE_SIZE);
4570
	trace_set_ring_buffer_expanded(NULL);
4571
	disable_tracing_selftest("running event tracing");
4572
 
4573
	return 1;
4574
}
4575
__setup("trace_event=", setup_trace_event);
4576
 
4577
static int events_callback(const char *name, umode_t *mode, void **data,
4578
			   const struct file_operations **fops)
4579
{
4580
	if (strcmp(name, "enable") == 0) {
4581
		*mode = TRACE_MODE_WRITE;
4582
		*fops = &ftrace_tr_enable_fops;
4583
		return 1;
4584
	}
4585
 
4586
	if (strcmp(name, "header_page") == 0) {
4587
		*mode = TRACE_MODE_READ;
4588
		*fops = &ftrace_show_header_page_fops;
4589
 
4590
	} else if (strcmp(name, "header_event") == 0) {
4591
		*mode = TRACE_MODE_READ;
4592
		*fops = &ftrace_show_header_event_fops;
4593
	} else
4594
		return 0;
4595
 
4596
	return 1;
4597
}
4598
 
4599
/* Expects to have event_mutex held when called */
4600
static int
4601
create_event_toplevel_files(struct dentry *parent, struct trace_array *tr)
4602
{
4603
	struct eventfs_inode *e_events;
4604
	struct dentry *entry;
4605
	int nr_entries;
4606
	static struct eventfs_entry events_entries[] = {
4607
		{
4608
			.name		= "enable",
4609
			.callback	= events_callback,
4610
		},
4611
		{
4612
			.name		= "header_page",
4613
			.callback	= events_callback,
4614
		},
4615
		{
4616
			.name		= "header_event",
4617
			.callback	= events_callback,
4618
		},
4619
	};
4620
 
4621
	entry = trace_create_file("set_event", TRACE_MODE_WRITE, parent,
4622
				  tr, &ftrace_set_event_fops);
4623
	if (!entry)
4624
		return -ENOMEM;
4625
 
4626
	nr_entries = ARRAY_SIZE(events_entries);
4627
 
4628
	e_events = eventfs_create_events_dir("events", parent, events_entries,
4629
					     nr_entries, tr);
4630
	if (IS_ERR(e_events)) {
4631
		pr_warn("Could not create tracefs 'events' directory\n");
4632
		return -ENOMEM;
4633
	}
4634
 
4635
	/* There are not as crucial, just warn if they are not created */
4636
 
4637
	trace_create_file("set_event_pid", TRACE_MODE_WRITE, parent,
4638
			  tr, &ftrace_set_event_pid_fops);
4639
 
4640
	trace_create_file("set_event_notrace_pid",
4641
			  TRACE_MODE_WRITE, parent, tr,
4642
			  &ftrace_set_event_notrace_pid_fops);
4643
 
4644
	tr->event_dir = e_events;
4645
 
4646
	return 0;
4647
}
4648
 
4649
/**
4650
 * event_trace_add_tracer - add a instance of a trace_array to events
4651
 * @parent: The parent dentry to place the files/directories for events in
4652
 * @tr: The trace array associated with these events
4653
 *
4654
 * When a new instance is created, it needs to set up its events
4655
 * directory, as well as other files associated with events. It also
4656
 * creates the event hierarchy in the @parent/events directory.
4657
 *
4658
 * Returns 0 on success.
4659
 *
4660
 * Must be called with event_mutex held.
4661
 */
4662
int event_trace_add_tracer(struct dentry *parent, struct trace_array *tr)
4663
{
4664
	int ret;
4665
 
4666
	lockdep_assert_held(&event_mutex);
4667
 
4668
	ret = create_event_toplevel_files(parent, tr);
4669
	if (ret)
4670
		goto out;
4671
 
4672
	down_write(&trace_event_sem);
4673
	/* If tr already has the event list, it is initialized in early boot. */
4674
	if (unlikely(!list_empty(&tr->events)))
4675
		__trace_early_add_event_dirs(tr);
4676
	else
4677
		__trace_add_event_dirs(tr);
4678
	up_write(&trace_event_sem);
4679
 
4680
 out:
4681
	return ret;
4682
}
4683
 
4684
/*
4685
 * The top trace array already had its file descriptors created.
4686
 * Now the files themselves need to be created.
4687
 */
4688
static __init int
4689
early_event_add_tracer(struct dentry *parent, struct trace_array *tr)
4690
{
4691
	int ret;
4692
 
4693
	guard(mutex)(&event_mutex);
4694
 
4695
	ret = create_event_toplevel_files(parent, tr);
4696
	if (ret)
4697
		return ret;
4698
 
4699
	down_write(&trace_event_sem);
4700
	__trace_early_add_event_dirs(tr);
4701
	up_write(&trace_event_sem);
4702
 
4703
	return 0;
4704
}
4705
 
4706
/* Must be called with event_mutex held */
4707
int event_trace_del_tracer(struct trace_array *tr)
4708
{
4709
	lockdep_assert_held(&event_mutex);
4710
 
4711
	/* Disable any event triggers and associated soft-disabled events */
4712
	clear_event_triggers(tr);
4713
 
4714
	/* Clear the pid list */
4715
	__ftrace_clear_event_pids(tr, TRACE_PIDS | TRACE_NO_PIDS);
4716
 
4717
	/* Disable any running events */
4718
	__ftrace_set_clr_event_nolock(tr, NULL, NULL, NULL, 0, NULL);
4719
 
4720
	/* Make sure no more events are being executed */
4721
	tracepoint_synchronize_unregister();
4722
 
4723
	down_write(&trace_event_sem);
4724
	__trace_remove_event_dirs(tr);
4725
	eventfs_remove_events_dir(tr->event_dir);
4726
	up_write(&trace_event_sem);
4727
 
4728
	tr->event_dir = NULL;
4729
 
4730
	return 0;
4731
}
4732
 
4733
static __init int event_trace_memsetup(void)
4734
{
4735
	field_cachep = KMEM_CACHE(ftrace_event_field, SLAB_PANIC);
4736
	file_cachep = KMEM_CACHE(trace_event_file, SLAB_PANIC);
4737
	return 0;
4738
}
4739
 
4740
__init void
4741
early_enable_events(struct trace_array *tr, char *buf, bool disable_first)
4742
{
4743
	char *token;
4744
	int ret;
4745
 
4746
	while (true) {
4747
		token = strsep(&buf, ",");
4748
 
4749
		if (!token)
4750
			break;
4751
 
4752
		if (*token) {
4753
			/* Restarting syscalls requires that we stop them first */
4754
			if (disable_first)
4755
				ftrace_set_clr_event(tr, token, 0);
4756
 
4757
			ret = ftrace_set_clr_event(tr, token, 1);
4758
			if (ret)
4759
				pr_warn("Failed to enable trace event: %s\n", token);
4760
		}
4761
 
4762
		/* Put back the comma to allow this to be called again */
4763
		if (buf)
4764
			*(buf - 1) = ',';
4765
	}
4766
}
4767
 
4768
static __init int event_trace_enable(void)
4769
{
4770
	struct trace_array *tr = top_trace_array();
4771
	struct trace_event_call **iter, *call;
4772
	int ret;
4773
 
4774
	if (!tr)
4775
		return -ENODEV;
4776
 
4777
	for_each_event(iter, __start_ftrace_events, __stop_ftrace_events) {
4778
 
4779
		call = *iter;
4780
		ret = event_init(call);
4781
		if (!ret)
4782
			list_add(&call->list, &ftrace_events);
4783
	}
4784
 
4785
	register_trigger_cmds();
4786
 
4787
	/*
4788
	 * We need the top trace array to have a working set of trace
4789
	 * points at early init, before the debug files and directories
4790
	 * are created. Create the file entries now, and attach them
4791
	 * to the actual file dentries later.
4792
	 */
4793
	__trace_early_add_events(tr);
4794
 
4795
	early_enable_events(tr, bootup_event_buf, false);
4796
 
4797
	trace_printk_start_comm();
4798
 
4799
	register_event_cmds();
4800
 
4801
 
4802
	return 0;
4803
}
4804
 
4805
/*
4806
 * event_trace_enable() is called from trace_event_init() first to
4807
 * initialize events and perhaps start any events that are on the
4808
 * command line. Unfortunately, there are some events that will not
4809
 * start this early, like the system call tracepoints that need
4810
 * to set the %SYSCALL_WORK_SYSCALL_TRACEPOINT flag of pid 1. But
4811
 * event_trace_enable() is called before pid 1 starts, and this flag
4812
 * is never set, making the syscall tracepoint never get reached, but
4813
 * the event is enabled regardless (and not doing anything).
4814
 */
4815
static __init int event_trace_enable_again(void)
4816
{
4817
	struct trace_array *tr;
4818
 
4819
	tr = top_trace_array();
4820
	if (!tr)
4821
		return -ENODEV;
4822
 
4823
	early_enable_events(tr, bootup_event_buf, true);
4824
 
4825
	return 0;
4826
}
4827
 
4828
early_initcall(event_trace_enable_again);
4829
 
4830
/* Init fields which doesn't related to the tracefs */
4831
static __init int event_trace_init_fields(void)
4832
{
4833
	if (trace_define_generic_fields())
4834
		pr_warn("tracing: Failed to allocated generic fields");
4835
 
4836
	if (trace_define_common_fields())
4837
		pr_warn("tracing: Failed to allocate common fields");
4838
 
4839
	return 0;
4840
}
4841
 
4842
__init int event_trace_init(void)
4843
{
4844
	struct trace_array *tr;
4845
	int ret;
4846
 
4847
	tr = top_trace_array();
4848
	if (!tr)
4849
		return -ENODEV;
4850
 
4851
	trace_create_file("available_events", TRACE_MODE_READ,
4852
			  NULL, tr, &ftrace_avail_fops);
4853
 
4854
	ret = early_event_add_tracer(NULL, tr);
4855
	if (ret)
4856
		return ret;
4857
 
4858
#ifdef CONFIG_MODULES
4859
	ret = register_module_notifier(&trace_module_nb);
4860
	if (ret)
4861
		pr_warn("Failed to register trace events module notifier\n");
4862
#endif
4863
 
4864
	eventdir_initialized = true;
4865
 
4866
	return 0;
4867
}
4868
 
4869
void __init trace_event_init(void)
4870
{
4871
	event_trace_memsetup();
4872
	init_ftrace_syscalls();
4873
	event_trace_enable();
4874
	event_trace_init_fields();
4875
}
4876
 
4877
#ifdef CONFIG_EVENT_TRACE_STARTUP_TEST
4878
 
4879
static DEFINE_SPINLOCK(test_spinlock);
4880
static DEFINE_SPINLOCK(test_spinlock_irq);
4881
static DEFINE_MUTEX(test_mutex);
4882
 
4883
static __init void test_work(struct work_struct *dummy)
4884
{
4885
	spin_lock(&test_spinlock);
4886
	spin_lock_irq(&test_spinlock_irq);
4887
	udelay(1);
4888
	spin_unlock_irq(&test_spinlock_irq);
4889
	spin_unlock(&test_spinlock);
4890
 
4891
	mutex_lock(&test_mutex);
4892
	msleep(1);
4893
	mutex_unlock(&test_mutex);
4894
}
4895
 
4896
static __init int event_test_thread(void *unused)
4897
{
4898
	void *test_malloc;
4899
 
4900
	test_malloc = kmalloc(1234, GFP_KERNEL);
4901
	if (!test_malloc)
4902
		pr_info("failed to kmalloc\n");
4903
 
4904
	schedule_on_each_cpu(test_work);
4905
 
4906
	kfree(test_malloc);
4907
 
4908
	set_current_state(TASK_INTERRUPTIBLE);
4909
	while (!kthread_should_stop()) {
4910
		schedule();
4911
		set_current_state(TASK_INTERRUPTIBLE);
4912
	}
4913
	__set_current_state(TASK_RUNNING);
4914
 
4915
	return 0;
4916
}
4917
 
4918
/*
4919
 * Do various things that may trigger events.
4920
 */
4921
static __init void event_test_stuff(void)
4922
{
4923
	struct task_struct *test_thread;
4924
 
4925
	test_thread = kthread_run(event_test_thread, NULL, "test-events");
4926
	msleep(1);
4927
	kthread_stop(test_thread);
4928
}
4929
 
4930
/*
4931
 * For every trace event defined, we will test each trace point separately,
4932
 * and then by groups, and finally all trace points.
4933
 */
4934
static __init void event_trace_self_tests(void)
4935
{
4936
	struct trace_subsystem_dir *dir;
4937
	struct trace_event_file *file;
4938
	struct trace_event_call *call;
4939
	struct event_subsystem *system;
4940
	struct trace_array *tr;
4941
	int ret;
4942
 
4943
	tr = top_trace_array();
4944
	if (!tr)
4945
		return;
4946
 
4947
	pr_info("Running tests on trace events:\n");
4948
 
4949
	list_for_each_entry(file, &tr->events, list) {
4950
 
4951
		call = file->event_call;
4952
 
4953
		/* Only test those that have a probe */
4954
		if (!call->class || !call->class->probe)
4955
			continue;
4956
 
4957
/*
4958
 * Testing syscall events here is pretty useless, but
4959
 * we still do it if configured. But this is time consuming.
4960
 * What we really need is a user thread to perform the
4961
 * syscalls as we test.
4962
 */
4963
#ifndef CONFIG_EVENT_TRACE_TEST_SYSCALLS
4964
		if (call->class->system &&
4965
		    strcmp(call->class->system, "syscalls") == 0)
4966
			continue;
4967
#endif
4968
 
4969
		pr_info("Testing event %s: ", trace_event_name(call));
4970
 
4971
		/*
4972
		 * If an event is already enabled, someone is using
4973
		 * it and the self test should not be on.
4974
		 */
4975
		if (file->flags & EVENT_FILE_FL_ENABLED) {
4976
			pr_warn("Enabled event during self test!\n");
4977
			WARN_ON_ONCE(1);
4978
			continue;
4979
		}
4980
 
4981
		ftrace_event_enable_disable(file, 1);
4982
		event_test_stuff();
4983
		ftrace_event_enable_disable(file, 0);
4984
 
4985
		pr_cont("OK\n");
4986
	}
4987
 
4988
	/* Now test at the sub system level */
4989
 
4990
	pr_info("Running tests on trace event systems:\n");
4991
 
4992
	list_for_each_entry(dir, &tr->systems, list) {
4993
 
4994
		system = dir->subsystem;
4995
 
4996
		/* the ftrace system is special, skip it */
4997
		if (strcmp(system->name, "ftrace") == 0)
4998
			continue;
4999
 
5000
		pr_info("Testing event system %s: ", system->name);
5001
 
5002
		ret = __ftrace_set_clr_event(tr, NULL, system->name, NULL, 1, NULL);
5003
		if (WARN_ON_ONCE(ret)) {
5004
			pr_warn("error enabling system %s\n",
5005
				system->name);
5006
			continue;
5007
		}
5008
 
5009
		event_test_stuff();
5010
 
5011
		ret = __ftrace_set_clr_event(tr, NULL, system->name, NULL, 0, NULL);
5012
		if (WARN_ON_ONCE(ret)) {
5013
			pr_warn("error disabling system %s\n",
5014
				system->name);
5015
			continue;
5016
		}
5017
 
5018
		pr_cont("OK\n");
5019
	}
5020
 
5021
	/* Test with all events enabled */
5022
 
5023
	pr_info("Running tests on all trace events:\n");
5024
	pr_info("Testing all events: ");
5025
 
5026
	ret = __ftrace_set_clr_event(tr, NULL, NULL, NULL, 1, NULL);
5027
	if (WARN_ON_ONCE(ret)) {
5028
		pr_warn("error enabling all events\n");
5029
		return;
5030
	}
5031
 
5032
	event_test_stuff();
5033
 
5034
	/* reset sysname */
5035
	ret = __ftrace_set_clr_event(tr, NULL, NULL, NULL, 0, NULL);
5036
	if (WARN_ON_ONCE(ret)) {
5037
		pr_warn("error disabling all events\n");
5038
		return;
5039
	}
5040
 
5041
	pr_cont("OK\n");
5042
}
5043
 
5044
#ifdef CONFIG_FUNCTION_TRACER
5045
 
5046
static DEFINE_PER_CPU(atomic_t, ftrace_test_event_disable);
5047
 
5048
static struct trace_event_file event_trace_file __initdata;
5049
 
5050
static void __init
5051
function_test_events_call(unsigned long ip, unsigned long parent_ip,
5052
			  struct ftrace_ops *op, struct ftrace_regs *regs)
5053
{
5054
	struct trace_buffer *buffer;
5055
	struct ring_buffer_event *event;
5056
	struct ftrace_entry *entry;
5057
	unsigned int trace_ctx;
5058
	long disabled;
5059
	int cpu;
5060
 
5061
	trace_ctx = tracing_gen_ctx();
5062
	preempt_disable_notrace();
5063
	cpu = raw_smp_processor_id();
5064
	disabled = atomic_inc_return(&per_cpu(ftrace_test_event_disable, cpu));
5065
 
5066
	if (disabled != 1)
5067
		goto out;
5068
 
5069
	event = trace_event_buffer_lock_reserve(&buffer, &event_trace_file,
5070
						TRACE_FN, sizeof(*entry),
5071
						trace_ctx);
5072
	if (!event)
5073
		goto out;
5074
	entry	= ring_buffer_event_data(event);
5075
	entry->ip			= ip;
5076
	entry->parent_ip		= parent_ip;
5077
 
5078
	event_trigger_unlock_commit(&event_trace_file, buffer, event,
5079
				    entry, trace_ctx);
5080
 out:
5081
	atomic_dec(&per_cpu(ftrace_test_event_disable, cpu));
5082
	preempt_enable_notrace();
5083
}
5084
 
5085
static struct ftrace_ops trace_ops __initdata  =
5086
{
5087
	.func = function_test_events_call,
5088
};
5089
 
5090
static __init void event_trace_self_test_with_function(void)
5091
{
5092
	int ret;
5093
 
5094
	event_trace_file.tr = top_trace_array();
5095
	if (WARN_ON(!event_trace_file.tr))
5096
		return;
5097
 
5098
	ret = register_ftrace_function(&trace_ops);
5099
	if (WARN_ON(ret < 0)) {
5100
		pr_info("Failed to enable function tracer for event tests\n");
5101
		return;
5102
	}
5103
	pr_info("Running tests again, along with the function tracer\n");
5104
	event_trace_self_tests();
5105
	unregister_ftrace_function(&trace_ops);
5106
}
5107
#else
5108
static __init void event_trace_self_test_with_function(void)
5109
{
5110
}
5111
#endif
5112
 
5113
static __init int event_trace_self_tests_init(void)
5114
{
5115
	if (!tracing_selftest_disabled) {
5116
		event_trace_self_tests();
5117
		event_trace_self_test_with_function();
5118
	}
5119
 
5120
	return 0;
5121
}
5122
 
5123
late_initcall(event_trace_self_tests_init);
5124
 
5125
#endif