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