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