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