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