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