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