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