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