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