1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * trace_events_synth - synthetic trace events
4  *
5  * Copyright (C) 2015, 2020 Tom Zanussi <tom.zanussi@linux.intel.com>
6  */
7 
8 #include <linux/module.h>
9 #include <linux/kallsyms.h>
10 #include <linux/security.h>
11 #include <linux/mutex.h>
12 #include <linux/slab.h>
13 #include <linux/stacktrace.h>
14 #include <linux/rculist.h>
15 #include <linux/tracefs.h>
16 
17 /* for gfp flag names */
18 #include <linux/trace_events.h>
19 #include <trace/events/mmflags.h>
20 
21 #include "trace_synth.h"
22 
23 #undef ERRORS
24 #define ERRORS	\
25 	C(BAD_NAME,		"Illegal name"),		\
26 	C(INVALID_CMD,		"Command must be of the form: <name> field[;field] ..."),\
27 	C(INVALID_DYN_CMD,	"Command must be of the form: s or -:[synthetic/]<name> field[;field] ..."),\
28 	C(EVENT_EXISTS,		"Event already exists"),	\
29 	C(TOO_MANY_FIELDS,	"Too many fields"),		\
30 	C(INCOMPLETE_TYPE,	"Incomplete type"),		\
31 	C(INVALID_TYPE,		"Invalid type"),		\
32 	C(INVALID_FIELD,        "Invalid field"),		\
33 	C(INVALID_ARRAY_SPEC,	"Invalid array specification"),
34 
35 #undef C
36 #define C(a, b)		SYNTH_ERR_##a
37 
38 enum { ERRORS };
39 
40 #undef C
41 #define C(a, b)		b
42 
43 static const char *err_text[] = { ERRORS };
44 
45 static char last_cmd[MAX_FILTER_STR_VAL];
46 
47 static int errpos(const char *str)
48 {
49 	return err_pos(last_cmd, str);
50 }
51 
52 static void last_cmd_set(const char *str)
53 {
54 	if (!str)
55 		return;
56 
57 	strncpy(last_cmd, str, MAX_FILTER_STR_VAL - 1);
58 }
59 
60 static void synth_err(u8 err_type, u8 err_pos)
61 {
62 	tracing_log_err(NULL, "synthetic_events", last_cmd, err_text,
63 			err_type, err_pos);
64 }
65 
66 static int create_synth_event(const char *raw_command);
67 static int synth_event_show(struct seq_file *m, struct dyn_event *ev);
68 static int synth_event_release(struct dyn_event *ev);
69 static bool synth_event_is_busy(struct dyn_event *ev);
70 static bool synth_event_match(const char *system, const char *event,
71 			int argc, const char **argv, struct dyn_event *ev);
72 
73 static struct dyn_event_operations synth_event_ops = {
74 	.create = create_synth_event,
75 	.show = synth_event_show,
76 	.is_busy = synth_event_is_busy,
77 	.free = synth_event_release,
78 	.match = synth_event_match,
79 };
80 
81 static bool is_synth_event(struct dyn_event *ev)
82 {
83 	return ev->ops == &synth_event_ops;
84 }
85 
86 static struct synth_event *to_synth_event(struct dyn_event *ev)
87 {
88 	return container_of(ev, struct synth_event, devent);
89 }
90 
91 static bool synth_event_is_busy(struct dyn_event *ev)
92 {
93 	struct synth_event *event = to_synth_event(ev);
94 
95 	return event->ref != 0;
96 }
97 
98 static bool synth_event_match(const char *system, const char *event,
99 			int argc, const char **argv, struct dyn_event *ev)
100 {
101 	struct synth_event *sev = to_synth_event(ev);
102 
103 	return strcmp(sev->name, event) == 0 &&
104 		(!system || strcmp(system, SYNTH_SYSTEM) == 0);
105 }
106 
107 struct synth_trace_event {
108 	struct trace_entry	ent;
109 	u64			fields[];
110 };
111 
112 static int synth_event_define_fields(struct trace_event_call *call)
113 {
114 	struct synth_trace_event trace;
115 	int offset = offsetof(typeof(trace), fields);
116 	struct synth_event *event = call->data;
117 	unsigned int i, size, n_u64;
118 	char *name, *type;
119 	bool is_signed;
120 	int ret = 0;
121 
122 	for (i = 0, n_u64 = 0; i < event->n_fields; i++) {
123 		size = event->fields[i]->size;
124 		is_signed = event->fields[i]->is_signed;
125 		type = event->fields[i]->type;
126 		name = event->fields[i]->name;
127 		ret = trace_define_field(call, type, name, offset, size,
128 					 is_signed, FILTER_OTHER);
129 		if (ret)
130 			break;
131 
132 		event->fields[i]->offset = n_u64;
133 
134 		if (event->fields[i]->is_string && !event->fields[i]->is_dynamic) {
135 			offset += STR_VAR_LEN_MAX;
136 			n_u64 += STR_VAR_LEN_MAX / sizeof(u64);
137 		} else {
138 			offset += sizeof(u64);
139 			n_u64++;
140 		}
141 	}
142 
143 	event->n_u64 = n_u64;
144 
145 	return ret;
146 }
147 
148 static bool synth_field_signed(char *type)
149 {
150 	if (str_has_prefix(type, "u"))
151 		return false;
152 	if (strcmp(type, "gfp_t") == 0)
153 		return false;
154 
155 	return true;
156 }
157 
158 static int synth_field_is_string(char *type)
159 {
160 	if (strstr(type, "char[") != NULL)
161 		return true;
162 
163 	return false;
164 }
165 
166 static int synth_field_string_size(char *type)
167 {
168 	char buf[4], *end, *start;
169 	unsigned int len;
170 	int size, err;
171 
172 	start = strstr(type, "char[");
173 	if (start == NULL)
174 		return -EINVAL;
175 	start += sizeof("char[") - 1;
176 
177 	end = strchr(type, ']');
178 	if (!end || end < start || type + strlen(type) > end + 1)
179 		return -EINVAL;
180 
181 	len = end - start;
182 	if (len > 3)
183 		return -EINVAL;
184 
185 	if (len == 0)
186 		return 0; /* variable-length string */
187 
188 	strncpy(buf, start, len);
189 	buf[len] = '\0';
190 
191 	err = kstrtouint(buf, 0, &size);
192 	if (err)
193 		return err;
194 
195 	if (size > STR_VAR_LEN_MAX)
196 		return -EINVAL;
197 
198 	return size;
199 }
200 
201 static int synth_field_size(char *type)
202 {
203 	int size = 0;
204 
205 	if (strcmp(type, "s64") == 0)
206 		size = sizeof(s64);
207 	else if (strcmp(type, "u64") == 0)
208 		size = sizeof(u64);
209 	else if (strcmp(type, "s32") == 0)
210 		size = sizeof(s32);
211 	else if (strcmp(type, "u32") == 0)
212 		size = sizeof(u32);
213 	else if (strcmp(type, "s16") == 0)
214 		size = sizeof(s16);
215 	else if (strcmp(type, "u16") == 0)
216 		size = sizeof(u16);
217 	else if (strcmp(type, "s8") == 0)
218 		size = sizeof(s8);
219 	else if (strcmp(type, "u8") == 0)
220 		size = sizeof(u8);
221 	else if (strcmp(type, "char") == 0)
222 		size = sizeof(char);
223 	else if (strcmp(type, "unsigned char") == 0)
224 		size = sizeof(unsigned char);
225 	else if (strcmp(type, "int") == 0)
226 		size = sizeof(int);
227 	else if (strcmp(type, "unsigned int") == 0)
228 		size = sizeof(unsigned int);
229 	else if (strcmp(type, "long") == 0)
230 		size = sizeof(long);
231 	else if (strcmp(type, "unsigned long") == 0)
232 		size = sizeof(unsigned long);
233 	else if (strcmp(type, "bool") == 0)
234 		size = sizeof(bool);
235 	else if (strcmp(type, "pid_t") == 0)
236 		size = sizeof(pid_t);
237 	else if (strcmp(type, "gfp_t") == 0)
238 		size = sizeof(gfp_t);
239 	else if (synth_field_is_string(type))
240 		size = synth_field_string_size(type);
241 
242 	return size;
243 }
244 
245 static const char *synth_field_fmt(char *type)
246 {
247 	const char *fmt = "%llu";
248 
249 	if (strcmp(type, "s64") == 0)
250 		fmt = "%lld";
251 	else if (strcmp(type, "u64") == 0)
252 		fmt = "%llu";
253 	else if (strcmp(type, "s32") == 0)
254 		fmt = "%d";
255 	else if (strcmp(type, "u32") == 0)
256 		fmt = "%u";
257 	else if (strcmp(type, "s16") == 0)
258 		fmt = "%d";
259 	else if (strcmp(type, "u16") == 0)
260 		fmt = "%u";
261 	else if (strcmp(type, "s8") == 0)
262 		fmt = "%d";
263 	else if (strcmp(type, "u8") == 0)
264 		fmt = "%u";
265 	else if (strcmp(type, "char") == 0)
266 		fmt = "%d";
267 	else if (strcmp(type, "unsigned char") == 0)
268 		fmt = "%u";
269 	else if (strcmp(type, "int") == 0)
270 		fmt = "%d";
271 	else if (strcmp(type, "unsigned int") == 0)
272 		fmt = "%u";
273 	else if (strcmp(type, "long") == 0)
274 		fmt = "%ld";
275 	else if (strcmp(type, "unsigned long") == 0)
276 		fmt = "%lu";
277 	else if (strcmp(type, "bool") == 0)
278 		fmt = "%d";
279 	else if (strcmp(type, "pid_t") == 0)
280 		fmt = "%d";
281 	else if (strcmp(type, "gfp_t") == 0)
282 		fmt = "%x";
283 	else if (synth_field_is_string(type))
284 		fmt = "%.*s";
285 
286 	return fmt;
287 }
288 
289 static void print_synth_event_num_val(struct trace_seq *s,
290 				      char *print_fmt, char *name,
291 				      int size, u64 val, char *space)
292 {
293 	switch (size) {
294 	case 1:
295 		trace_seq_printf(s, print_fmt, name, (u8)val, space);
296 		break;
297 
298 	case 2:
299 		trace_seq_printf(s, print_fmt, name, (u16)val, space);
300 		break;
301 
302 	case 4:
303 		trace_seq_printf(s, print_fmt, name, (u32)val, space);
304 		break;
305 
306 	default:
307 		trace_seq_printf(s, print_fmt, name, val, space);
308 		break;
309 	}
310 }
311 
312 static enum print_line_t print_synth_event(struct trace_iterator *iter,
313 					   int flags,
314 					   struct trace_event *event)
315 {
316 	struct trace_array *tr = iter->tr;
317 	struct trace_seq *s = &iter->seq;
318 	struct synth_trace_event *entry;
319 	struct synth_event *se;
320 	unsigned int i, n_u64;
321 	char print_fmt[32];
322 	const char *fmt;
323 
324 	entry = (struct synth_trace_event *)iter->ent;
325 	se = container_of(event, struct synth_event, call.event);
326 
327 	trace_seq_printf(s, "%s: ", se->name);
328 
329 	for (i = 0, n_u64 = 0; i < se->n_fields; i++) {
330 		if (trace_seq_has_overflowed(s))
331 			goto end;
332 
333 		fmt = synth_field_fmt(se->fields[i]->type);
334 
335 		/* parameter types */
336 		if (tr && tr->trace_flags & TRACE_ITER_VERBOSE)
337 			trace_seq_printf(s, "%s ", fmt);
338 
339 		snprintf(print_fmt, sizeof(print_fmt), "%%s=%s%%s", fmt);
340 
341 		/* parameter values */
342 		if (se->fields[i]->is_string) {
343 			if (se->fields[i]->is_dynamic) {
344 				u32 offset, data_offset;
345 				char *str_field;
346 
347 				offset = (u32)entry->fields[n_u64];
348 				data_offset = offset & 0xffff;
349 
350 				str_field = (char *)entry + data_offset;
351 
352 				trace_seq_printf(s, print_fmt, se->fields[i]->name,
353 						 STR_VAR_LEN_MAX,
354 						 str_field,
355 						 i == se->n_fields - 1 ? "" : " ");
356 				n_u64++;
357 			} else {
358 				trace_seq_printf(s, print_fmt, se->fields[i]->name,
359 						 STR_VAR_LEN_MAX,
360 						 (char *)&entry->fields[n_u64],
361 						 i == se->n_fields - 1 ? "" : " ");
362 				n_u64 += STR_VAR_LEN_MAX / sizeof(u64);
363 			}
364 		} else {
365 			struct trace_print_flags __flags[] = {
366 			    __def_gfpflag_names, {-1, NULL} };
367 			char *space = (i == se->n_fields - 1 ? "" : " ");
368 
369 			print_synth_event_num_val(s, print_fmt,
370 						  se->fields[i]->name,
371 						  se->fields[i]->size,
372 						  entry->fields[n_u64],
373 						  space);
374 
375 			if (strcmp(se->fields[i]->type, "gfp_t") == 0) {
376 				trace_seq_puts(s, " (");
377 				trace_print_flags_seq(s, "|",
378 						      entry->fields[n_u64],
379 						      __flags);
380 				trace_seq_putc(s, ')');
381 			}
382 			n_u64++;
383 		}
384 	}
385 end:
386 	trace_seq_putc(s, '\n');
387 
388 	return trace_handle_return(s);
389 }
390 
391 static struct trace_event_functions synth_event_funcs = {
392 	.trace		= print_synth_event
393 };
394 
395 static unsigned int trace_string(struct synth_trace_event *entry,
396 				 struct synth_event *event,
397 				 char *str_val,
398 				 bool is_dynamic,
399 				 unsigned int data_size,
400 				 unsigned int *n_u64)
401 {
402 	unsigned int len = 0;
403 	char *str_field;
404 
405 	if (is_dynamic) {
406 		u32 data_offset;
407 
408 		data_offset = offsetof(typeof(*entry), fields);
409 		data_offset += event->n_u64 * sizeof(u64);
410 		data_offset += data_size;
411 
412 		str_field = (char *)entry + data_offset;
413 
414 		len = strlen(str_val) + 1;
415 		strscpy(str_field, str_val, len);
416 
417 		data_offset |= len << 16;
418 		*(u32 *)&entry->fields[*n_u64] = data_offset;
419 
420 		(*n_u64)++;
421 	} else {
422 		str_field = (char *)&entry->fields[*n_u64];
423 
424 		strscpy(str_field, str_val, STR_VAR_LEN_MAX);
425 		(*n_u64) += STR_VAR_LEN_MAX / sizeof(u64);
426 	}
427 
428 	return len;
429 }
430 
431 static notrace void trace_event_raw_event_synth(void *__data,
432 						u64 *var_ref_vals,
433 						unsigned int *var_ref_idx)
434 {
435 	unsigned int i, n_u64, val_idx, len, data_size = 0;
436 	struct trace_event_file *trace_file = __data;
437 	struct synth_trace_event *entry;
438 	struct trace_event_buffer fbuffer;
439 	struct trace_buffer *buffer;
440 	struct synth_event *event;
441 	int fields_size = 0;
442 
443 	event = trace_file->event_call->data;
444 
445 	if (trace_trigger_soft_disabled(trace_file))
446 		return;
447 
448 	fields_size = event->n_u64 * sizeof(u64);
449 
450 	for (i = 0; i < event->n_dynamic_fields; i++) {
451 		unsigned int field_pos = event->dynamic_fields[i]->field_pos;
452 		char *str_val;
453 
454 		val_idx = var_ref_idx[field_pos];
455 		str_val = (char *)(long)var_ref_vals[val_idx];
456 
457 		len = strlen(str_val) + 1;
458 
459 		fields_size += len;
460 	}
461 
462 	/*
463 	 * Avoid ring buffer recursion detection, as this event
464 	 * is being performed within another event.
465 	 */
466 	buffer = trace_file->tr->array_buffer.buffer;
467 	ring_buffer_nest_start(buffer);
468 
469 	entry = trace_event_buffer_reserve(&fbuffer, trace_file,
470 					   sizeof(*entry) + fields_size);
471 	if (!entry)
472 		goto out;
473 
474 	for (i = 0, n_u64 = 0; i < event->n_fields; i++) {
475 		val_idx = var_ref_idx[i];
476 		if (event->fields[i]->is_string) {
477 			char *str_val = (char *)(long)var_ref_vals[val_idx];
478 
479 			len = trace_string(entry, event, str_val,
480 					   event->fields[i]->is_dynamic,
481 					   data_size, &n_u64);
482 			data_size += len; /* only dynamic string increments */
483 		} else {
484 			struct synth_field *field = event->fields[i];
485 			u64 val = var_ref_vals[val_idx];
486 
487 			switch (field->size) {
488 			case 1:
489 				*(u8 *)&entry->fields[n_u64] = (u8)val;
490 				break;
491 
492 			case 2:
493 				*(u16 *)&entry->fields[n_u64] = (u16)val;
494 				break;
495 
496 			case 4:
497 				*(u32 *)&entry->fields[n_u64] = (u32)val;
498 				break;
499 
500 			default:
501 				entry->fields[n_u64] = val;
502 				break;
503 			}
504 			n_u64++;
505 		}
506 	}
507 
508 	trace_event_buffer_commit(&fbuffer);
509 out:
510 	ring_buffer_nest_end(buffer);
511 }
512 
513 static void free_synth_event_print_fmt(struct trace_event_call *call)
514 {
515 	if (call) {
516 		kfree(call->print_fmt);
517 		call->print_fmt = NULL;
518 	}
519 }
520 
521 static int __set_synth_event_print_fmt(struct synth_event *event,
522 				       char *buf, int len)
523 {
524 	const char *fmt;
525 	int pos = 0;
526 	int i;
527 
528 	/* When len=0, we just calculate the needed length */
529 #define LEN_OR_ZERO (len ? len - pos : 0)
530 
531 	pos += snprintf(buf + pos, LEN_OR_ZERO, "\"");
532 	for (i = 0; i < event->n_fields; i++) {
533 		fmt = synth_field_fmt(event->fields[i]->type);
534 		pos += snprintf(buf + pos, LEN_OR_ZERO, "%s=%s%s",
535 				event->fields[i]->name, fmt,
536 				i == event->n_fields - 1 ? "" : ", ");
537 	}
538 	pos += snprintf(buf + pos, LEN_OR_ZERO, "\"");
539 
540 	for (i = 0; i < event->n_fields; i++) {
541 		if (event->fields[i]->is_string &&
542 		    event->fields[i]->is_dynamic)
543 			pos += snprintf(buf + pos, LEN_OR_ZERO,
544 				", __get_str(%s)", event->fields[i]->name);
545 		else
546 			pos += snprintf(buf + pos, LEN_OR_ZERO,
547 					", REC->%s", event->fields[i]->name);
548 	}
549 
550 #undef LEN_OR_ZERO
551 
552 	/* return the length of print_fmt */
553 	return pos;
554 }
555 
556 static int set_synth_event_print_fmt(struct trace_event_call *call)
557 {
558 	struct synth_event *event = call->data;
559 	char *print_fmt;
560 	int len;
561 
562 	/* First: called with 0 length to calculate the needed length */
563 	len = __set_synth_event_print_fmt(event, NULL, 0);
564 
565 	print_fmt = kmalloc(len + 1, GFP_KERNEL);
566 	if (!print_fmt)
567 		return -ENOMEM;
568 
569 	/* Second: actually write the @print_fmt */
570 	__set_synth_event_print_fmt(event, print_fmt, len + 1);
571 	call->print_fmt = print_fmt;
572 
573 	return 0;
574 }
575 
576 static void free_synth_field(struct synth_field *field)
577 {
578 	kfree(field->type);
579 	kfree(field->name);
580 	kfree(field);
581 }
582 
583 static int check_field_version(const char *prefix, const char *field_type,
584 			       const char *field_name)
585 {
586 	/*
587 	 * For backward compatibility, the old synthetic event command
588 	 * format did not require semicolons, and in order to not
589 	 * break user space, that old format must still work. If a new
590 	 * feature is added, then the format that uses the new feature
591 	 * will be required to have semicolons, as nothing that uses
592 	 * the old format would be using the new, yet to be created,
593 	 * feature. When a new feature is added, this will detect it,
594 	 * and return a number greater than 1, and require the format
595 	 * to use semicolons.
596 	 */
597 	return 1;
598 }
599 
600 static struct synth_field *parse_synth_field(int argc, char **argv,
601 					     int *consumed, int *field_version)
602 {
603 	const char *prefix = NULL, *field_type = argv[0], *field_name, *array;
604 	struct synth_field *field;
605 	int len, ret = -ENOMEM;
606 	struct seq_buf s;
607 	ssize_t size;
608 
609 	if (!strcmp(field_type, "unsigned")) {
610 		if (argc < 3) {
611 			synth_err(SYNTH_ERR_INCOMPLETE_TYPE, errpos(field_type));
612 			return ERR_PTR(-EINVAL);
613 		}
614 		prefix = "unsigned ";
615 		field_type = argv[1];
616 		field_name = argv[2];
617 		*consumed += 3;
618 	} else {
619 		field_name = argv[1];
620 		*consumed += 2;
621 	}
622 
623 	if (!field_name) {
624 		synth_err(SYNTH_ERR_INVALID_FIELD, errpos(field_type));
625 		return ERR_PTR(-EINVAL);
626 	}
627 
628 	*field_version = check_field_version(prefix, field_type, field_name);
629 
630 	field = kzalloc(sizeof(*field), GFP_KERNEL);
631 	if (!field)
632 		return ERR_PTR(-ENOMEM);
633 
634 	len = strlen(field_name);
635 	array = strchr(field_name, '[');
636 	if (array)
637 		len -= strlen(array);
638 
639 	field->name = kmemdup_nul(field_name, len, GFP_KERNEL);
640 	if (!field->name)
641 		goto free;
642 
643 	if (!is_good_name(field->name)) {
644 		synth_err(SYNTH_ERR_BAD_NAME, errpos(field_name));
645 		ret = -EINVAL;
646 		goto free;
647 	}
648 
649 	len = strlen(field_type) + 1;
650 
651 	if (array)
652 		len += strlen(array);
653 
654 	if (prefix)
655 		len += strlen(prefix);
656 
657 	field->type = kzalloc(len, GFP_KERNEL);
658 	if (!field->type)
659 		goto free;
660 
661 	seq_buf_init(&s, field->type, len);
662 	if (prefix)
663 		seq_buf_puts(&s, prefix);
664 	seq_buf_puts(&s, field_type);
665 	if (array)
666 		seq_buf_puts(&s, array);
667 	if (WARN_ON_ONCE(!seq_buf_buffer_left(&s)))
668 		goto free;
669 
670 	s.buffer[s.len] = '\0';
671 
672 	size = synth_field_size(field->type);
673 	if (size < 0) {
674 		if (array)
675 			synth_err(SYNTH_ERR_INVALID_ARRAY_SPEC, errpos(field_name));
676 		else
677 			synth_err(SYNTH_ERR_INVALID_TYPE, errpos(field_type));
678 		ret = -EINVAL;
679 		goto free;
680 	} else if (size == 0) {
681 		if (synth_field_is_string(field->type)) {
682 			char *type;
683 
684 			len = sizeof("__data_loc ") + strlen(field->type) + 1;
685 			type = kzalloc(len, GFP_KERNEL);
686 			if (!type)
687 				goto free;
688 
689 			seq_buf_init(&s, type, len);
690 			seq_buf_puts(&s, "__data_loc ");
691 			seq_buf_puts(&s, field->type);
692 
693 			if (WARN_ON_ONCE(!seq_buf_buffer_left(&s)))
694 				goto free;
695 			s.buffer[s.len] = '\0';
696 
697 			kfree(field->type);
698 			field->type = type;
699 
700 			field->is_dynamic = true;
701 			size = sizeof(u64);
702 		} else {
703 			synth_err(SYNTH_ERR_INVALID_TYPE, errpos(field_type));
704 			ret = -EINVAL;
705 			goto free;
706 		}
707 	}
708 	field->size = size;
709 
710 	if (synth_field_is_string(field->type))
711 		field->is_string = true;
712 
713 	field->is_signed = synth_field_signed(field->type);
714  out:
715 	return field;
716  free:
717 	free_synth_field(field);
718 	field = ERR_PTR(ret);
719 	goto out;
720 }
721 
722 static void free_synth_tracepoint(struct tracepoint *tp)
723 {
724 	if (!tp)
725 		return;
726 
727 	kfree(tp->name);
728 	kfree(tp);
729 }
730 
731 static struct tracepoint *alloc_synth_tracepoint(char *name)
732 {
733 	struct tracepoint *tp;
734 
735 	tp = kzalloc(sizeof(*tp), GFP_KERNEL);
736 	if (!tp)
737 		return ERR_PTR(-ENOMEM);
738 
739 	tp->name = kstrdup(name, GFP_KERNEL);
740 	if (!tp->name) {
741 		kfree(tp);
742 		return ERR_PTR(-ENOMEM);
743 	}
744 
745 	return tp;
746 }
747 
748 struct synth_event *find_synth_event(const char *name)
749 {
750 	struct dyn_event *pos;
751 	struct synth_event *event;
752 
753 	for_each_dyn_event(pos) {
754 		if (!is_synth_event(pos))
755 			continue;
756 		event = to_synth_event(pos);
757 		if (strcmp(event->name, name) == 0)
758 			return event;
759 	}
760 
761 	return NULL;
762 }
763 
764 static struct trace_event_fields synth_event_fields_array[] = {
765 	{ .type = TRACE_FUNCTION_TYPE,
766 	  .define_fields = synth_event_define_fields },
767 	{}
768 };
769 
770 static int register_synth_event(struct synth_event *event)
771 {
772 	struct trace_event_call *call = &event->call;
773 	int ret = 0;
774 
775 	event->call.class = &event->class;
776 	event->class.system = kstrdup(SYNTH_SYSTEM, GFP_KERNEL);
777 	if (!event->class.system) {
778 		ret = -ENOMEM;
779 		goto out;
780 	}
781 
782 	event->tp = alloc_synth_tracepoint(event->name);
783 	if (IS_ERR(event->tp)) {
784 		ret = PTR_ERR(event->tp);
785 		event->tp = NULL;
786 		goto out;
787 	}
788 
789 	INIT_LIST_HEAD(&call->class->fields);
790 	call->event.funcs = &synth_event_funcs;
791 	call->class->fields_array = synth_event_fields_array;
792 
793 	ret = register_trace_event(&call->event);
794 	if (!ret) {
795 		ret = -ENODEV;
796 		goto out;
797 	}
798 	call->flags = TRACE_EVENT_FL_TRACEPOINT;
799 	call->class->reg = trace_event_reg;
800 	call->class->probe = trace_event_raw_event_synth;
801 	call->data = event;
802 	call->tp = event->tp;
803 
804 	ret = trace_add_event_call(call);
805 	if (ret) {
806 		pr_warn("Failed to register synthetic event: %s\n",
807 			trace_event_name(call));
808 		goto err;
809 	}
810 
811 	ret = set_synth_event_print_fmt(call);
812 	if (ret < 0) {
813 		trace_remove_event_call(call);
814 		goto err;
815 	}
816  out:
817 	return ret;
818  err:
819 	unregister_trace_event(&call->event);
820 	goto out;
821 }
822 
823 static int unregister_synth_event(struct synth_event *event)
824 {
825 	struct trace_event_call *call = &event->call;
826 	int ret;
827 
828 	ret = trace_remove_event_call(call);
829 
830 	return ret;
831 }
832 
833 static void free_synth_event(struct synth_event *event)
834 {
835 	unsigned int i;
836 
837 	if (!event)
838 		return;
839 
840 	for (i = 0; i < event->n_fields; i++)
841 		free_synth_field(event->fields[i]);
842 
843 	kfree(event->fields);
844 	kfree(event->dynamic_fields);
845 	kfree(event->name);
846 	kfree(event->class.system);
847 	free_synth_tracepoint(event->tp);
848 	free_synth_event_print_fmt(&event->call);
849 	kfree(event);
850 }
851 
852 static struct synth_event *alloc_synth_event(const char *name, int n_fields,
853 					     struct synth_field **fields)
854 {
855 	unsigned int i, j, n_dynamic_fields = 0;
856 	struct synth_event *event;
857 
858 	event = kzalloc(sizeof(*event), GFP_KERNEL);
859 	if (!event) {
860 		event = ERR_PTR(-ENOMEM);
861 		goto out;
862 	}
863 
864 	event->name = kstrdup(name, GFP_KERNEL);
865 	if (!event->name) {
866 		kfree(event);
867 		event = ERR_PTR(-ENOMEM);
868 		goto out;
869 	}
870 
871 	event->fields = kcalloc(n_fields, sizeof(*event->fields), GFP_KERNEL);
872 	if (!event->fields) {
873 		free_synth_event(event);
874 		event = ERR_PTR(-ENOMEM);
875 		goto out;
876 	}
877 
878 	for (i = 0; i < n_fields; i++)
879 		if (fields[i]->is_dynamic)
880 			n_dynamic_fields++;
881 
882 	if (n_dynamic_fields) {
883 		event->dynamic_fields = kcalloc(n_dynamic_fields,
884 						sizeof(*event->dynamic_fields),
885 						GFP_KERNEL);
886 		if (!event->dynamic_fields) {
887 			free_synth_event(event);
888 			event = ERR_PTR(-ENOMEM);
889 			goto out;
890 		}
891 	}
892 
893 	dyn_event_init(&event->devent, &synth_event_ops);
894 
895 	for (i = 0, j = 0; i < n_fields; i++) {
896 		fields[i]->field_pos = i;
897 		event->fields[i] = fields[i];
898 
899 		if (fields[i]->is_dynamic)
900 			event->dynamic_fields[j++] = fields[i];
901 	}
902 	event->n_dynamic_fields = j;
903 	event->n_fields = n_fields;
904  out:
905 	return event;
906 }
907 
908 static int synth_event_check_arg_fn(void *data)
909 {
910 	struct dynevent_arg_pair *arg_pair = data;
911 	int size;
912 
913 	size = synth_field_size((char *)arg_pair->lhs);
914 	if (size == 0) {
915 		if (strstr((char *)arg_pair->lhs, "["))
916 			return 0;
917 	}
918 
919 	return size ? 0 : -EINVAL;
920 }
921 
922 /**
923  * synth_event_add_field - Add a new field to a synthetic event cmd
924  * @cmd: A pointer to the dynevent_cmd struct representing the new event
925  * @type: The type of the new field to add
926  * @name: The name of the new field to add
927  *
928  * Add a new field to a synthetic event cmd object.  Field ordering is in
929  * the same order the fields are added.
930  *
931  * See synth_field_size() for available types. If field_name contains
932  * [n] the field is considered to be an array.
933  *
934  * Return: 0 if successful, error otherwise.
935  */
936 int synth_event_add_field(struct dynevent_cmd *cmd, const char *type,
937 			  const char *name)
938 {
939 	struct dynevent_arg_pair arg_pair;
940 	int ret;
941 
942 	if (cmd->type != DYNEVENT_TYPE_SYNTH)
943 		return -EINVAL;
944 
945 	if (!type || !name)
946 		return -EINVAL;
947 
948 	dynevent_arg_pair_init(&arg_pair, 0, ';');
949 
950 	arg_pair.lhs = type;
951 	arg_pair.rhs = name;
952 
953 	ret = dynevent_arg_pair_add(cmd, &arg_pair, synth_event_check_arg_fn);
954 	if (ret)
955 		return ret;
956 
957 	if (++cmd->n_fields > SYNTH_FIELDS_MAX)
958 		ret = -EINVAL;
959 
960 	return ret;
961 }
962 EXPORT_SYMBOL_GPL(synth_event_add_field);
963 
964 /**
965  * synth_event_add_field_str - Add a new field to a synthetic event cmd
966  * @cmd: A pointer to the dynevent_cmd struct representing the new event
967  * @type_name: The type and name of the new field to add, as a single string
968  *
969  * Add a new field to a synthetic event cmd object, as a single
970  * string.  The @type_name string is expected to be of the form 'type
971  * name', which will be appended by ';'.  No sanity checking is done -
972  * what's passed in is assumed to already be well-formed.  Field
973  * ordering is in the same order the fields are added.
974  *
975  * See synth_field_size() for available types. If field_name contains
976  * [n] the field is considered to be an array.
977  *
978  * Return: 0 if successful, error otherwise.
979  */
980 int synth_event_add_field_str(struct dynevent_cmd *cmd, const char *type_name)
981 {
982 	struct dynevent_arg arg;
983 	int ret;
984 
985 	if (cmd->type != DYNEVENT_TYPE_SYNTH)
986 		return -EINVAL;
987 
988 	if (!type_name)
989 		return -EINVAL;
990 
991 	dynevent_arg_init(&arg, ';');
992 
993 	arg.str = type_name;
994 
995 	ret = dynevent_arg_add(cmd, &arg, NULL);
996 	if (ret)
997 		return ret;
998 
999 	if (++cmd->n_fields > SYNTH_FIELDS_MAX)
1000 		ret = -EINVAL;
1001 
1002 	return ret;
1003 }
1004 EXPORT_SYMBOL_GPL(synth_event_add_field_str);
1005 
1006 /**
1007  * synth_event_add_fields - Add multiple fields to a synthetic event cmd
1008  * @cmd: A pointer to the dynevent_cmd struct representing the new event
1009  * @fields: An array of type/name field descriptions
1010  * @n_fields: The number of field descriptions contained in the fields array
1011  *
1012  * Add a new set of fields to a synthetic event cmd object.  The event
1013  * fields that will be defined for the event should be passed in as an
1014  * array of struct synth_field_desc, and the number of elements in the
1015  * array passed in as n_fields.  Field ordering will retain the
1016  * ordering given in the fields array.
1017  *
1018  * See synth_field_size() for available types. If field_name contains
1019  * [n] the field is considered to be an array.
1020  *
1021  * Return: 0 if successful, error otherwise.
1022  */
1023 int synth_event_add_fields(struct dynevent_cmd *cmd,
1024 			   struct synth_field_desc *fields,
1025 			   unsigned int n_fields)
1026 {
1027 	unsigned int i;
1028 	int ret = 0;
1029 
1030 	for (i = 0; i < n_fields; i++) {
1031 		if (fields[i].type == NULL || fields[i].name == NULL) {
1032 			ret = -EINVAL;
1033 			break;
1034 		}
1035 
1036 		ret = synth_event_add_field(cmd, fields[i].type, fields[i].name);
1037 		if (ret)
1038 			break;
1039 	}
1040 
1041 	return ret;
1042 }
1043 EXPORT_SYMBOL_GPL(synth_event_add_fields);
1044 
1045 /**
1046  * __synth_event_gen_cmd_start - Start a synthetic event command from arg list
1047  * @cmd: A pointer to the dynevent_cmd struct representing the new event
1048  * @name: The name of the synthetic event
1049  * @mod: The module creating the event, NULL if not created from a module
1050  * @args: Variable number of arg (pairs), one pair for each field
1051  *
1052  * NOTE: Users normally won't want to call this function directly, but
1053  * rather use the synth_event_gen_cmd_start() wrapper, which
1054  * automatically adds a NULL to the end of the arg list.  If this
1055  * function is used directly, make sure the last arg in the variable
1056  * arg list is NULL.
1057  *
1058  * Generate a synthetic event command to be executed by
1059  * synth_event_gen_cmd_end().  This function can be used to generate
1060  * the complete command or only the first part of it; in the latter
1061  * case, synth_event_add_field(), synth_event_add_field_str(), or
1062  * synth_event_add_fields() can be used to add more fields following
1063  * this.
1064  *
1065  * There should be an even number variable args, each pair consisting
1066  * of a type followed by a field name.
1067  *
1068  * See synth_field_size() for available types. If field_name contains
1069  * [n] the field is considered to be an array.
1070  *
1071  * Return: 0 if successful, error otherwise.
1072  */
1073 int __synth_event_gen_cmd_start(struct dynevent_cmd *cmd, const char *name,
1074 				struct module *mod, ...)
1075 {
1076 	struct dynevent_arg arg;
1077 	va_list args;
1078 	int ret;
1079 
1080 	cmd->event_name = name;
1081 	cmd->private_data = mod;
1082 
1083 	if (cmd->type != DYNEVENT_TYPE_SYNTH)
1084 		return -EINVAL;
1085 
1086 	dynevent_arg_init(&arg, 0);
1087 	arg.str = name;
1088 	ret = dynevent_arg_add(cmd, &arg, NULL);
1089 	if (ret)
1090 		return ret;
1091 
1092 	va_start(args, mod);
1093 	for (;;) {
1094 		const char *type, *name;
1095 
1096 		type = va_arg(args, const char *);
1097 		if (!type)
1098 			break;
1099 		name = va_arg(args, const char *);
1100 		if (!name)
1101 			break;
1102 
1103 		if (++cmd->n_fields > SYNTH_FIELDS_MAX) {
1104 			ret = -EINVAL;
1105 			break;
1106 		}
1107 
1108 		ret = synth_event_add_field(cmd, type, name);
1109 		if (ret)
1110 			break;
1111 	}
1112 	va_end(args);
1113 
1114 	return ret;
1115 }
1116 EXPORT_SYMBOL_GPL(__synth_event_gen_cmd_start);
1117 
1118 /**
1119  * synth_event_gen_cmd_array_start - Start synthetic event command from an array
1120  * @cmd: A pointer to the dynevent_cmd struct representing the new event
1121  * @name: The name of the synthetic event
1122  * @fields: An array of type/name field descriptions
1123  * @n_fields: The number of field descriptions contained in the fields array
1124  *
1125  * Generate a synthetic event command to be executed by
1126  * synth_event_gen_cmd_end().  This function can be used to generate
1127  * the complete command or only the first part of it; in the latter
1128  * case, synth_event_add_field(), synth_event_add_field_str(), or
1129  * synth_event_add_fields() can be used to add more fields following
1130  * this.
1131  *
1132  * The event fields that will be defined for the event should be
1133  * passed in as an array of struct synth_field_desc, and the number of
1134  * elements in the array passed in as n_fields.  Field ordering will
1135  * retain the ordering given in the fields array.
1136  *
1137  * See synth_field_size() for available types. If field_name contains
1138  * [n] the field is considered to be an array.
1139  *
1140  * Return: 0 if successful, error otherwise.
1141  */
1142 int synth_event_gen_cmd_array_start(struct dynevent_cmd *cmd, const char *name,
1143 				    struct module *mod,
1144 				    struct synth_field_desc *fields,
1145 				    unsigned int n_fields)
1146 {
1147 	struct dynevent_arg arg;
1148 	unsigned int i;
1149 	int ret = 0;
1150 
1151 	cmd->event_name = name;
1152 	cmd->private_data = mod;
1153 
1154 	if (cmd->type != DYNEVENT_TYPE_SYNTH)
1155 		return -EINVAL;
1156 
1157 	if (n_fields > SYNTH_FIELDS_MAX)
1158 		return -EINVAL;
1159 
1160 	dynevent_arg_init(&arg, 0);
1161 	arg.str = name;
1162 	ret = dynevent_arg_add(cmd, &arg, NULL);
1163 	if (ret)
1164 		return ret;
1165 
1166 	for (i = 0; i < n_fields; i++) {
1167 		if (fields[i].type == NULL || fields[i].name == NULL)
1168 			return -EINVAL;
1169 
1170 		ret = synth_event_add_field(cmd, fields[i].type, fields[i].name);
1171 		if (ret)
1172 			break;
1173 	}
1174 
1175 	return ret;
1176 }
1177 EXPORT_SYMBOL_GPL(synth_event_gen_cmd_array_start);
1178 
1179 static int __create_synth_event(const char *name, const char *raw_fields)
1180 {
1181 	char **argv, *field_str, *tmp_fields, *saved_fields = NULL;
1182 	struct synth_field *field, *fields[SYNTH_FIELDS_MAX];
1183 	int consumed, cmd_version = 1, n_fields_this_loop;
1184 	int i, argc, n_fields = 0, ret = 0;
1185 	struct synth_event *event = NULL;
1186 
1187 	/*
1188 	 * Argument syntax:
1189 	 *  - Add synthetic event: <event_name> field[;field] ...
1190 	 *  - Remove synthetic event: !<event_name> field[;field] ...
1191 	 *      where 'field' = type field_name
1192 	 */
1193 
1194 	if (name[0] == '\0') {
1195 		synth_err(SYNTH_ERR_INVALID_CMD, 0);
1196 		return -EINVAL;
1197 	}
1198 
1199 	if (!is_good_name(name)) {
1200 		synth_err(SYNTH_ERR_BAD_NAME, errpos(name));
1201 		return -EINVAL;
1202 	}
1203 
1204 	mutex_lock(&event_mutex);
1205 
1206 	event = find_synth_event(name);
1207 	if (event) {
1208 		synth_err(SYNTH_ERR_EVENT_EXISTS, errpos(name));
1209 		ret = -EEXIST;
1210 		goto err;
1211 	}
1212 
1213 	tmp_fields = saved_fields = kstrdup(raw_fields, GFP_KERNEL);
1214 	if (!tmp_fields) {
1215 		ret = -ENOMEM;
1216 		goto err;
1217 	}
1218 
1219 	while ((field_str = strsep(&tmp_fields, ";")) != NULL) {
1220 		argv = argv_split(GFP_KERNEL, field_str, &argc);
1221 		if (!argv) {
1222 			ret = -ENOMEM;
1223 			goto err;
1224 		}
1225 
1226 		if (!argc) {
1227 			argv_free(argv);
1228 			continue;
1229 		}
1230 
1231 		n_fields_this_loop = 0;
1232 		consumed = 0;
1233 		while (argc > consumed) {
1234 			int field_version;
1235 
1236 			field = parse_synth_field(argc - consumed,
1237 						  argv + consumed, &consumed,
1238 						  &field_version);
1239 			if (IS_ERR(field)) {
1240 				ret = PTR_ERR(field);
1241 				goto err_free_arg;
1242 			}
1243 
1244 			/*
1245 			 * Track the highest version of any field we
1246 			 * found in the command.
1247 			 */
1248 			if (field_version > cmd_version)
1249 				cmd_version = field_version;
1250 
1251 			/*
1252 			 * Now sort out what is and isn't valid for
1253 			 * each supported version.
1254 			 *
1255 			 * If we see more than 1 field per loop, it
1256 			 * means we have multiple fields between
1257 			 * semicolons, and that's something we no
1258 			 * longer support in a version 2 or greater
1259 			 * command.
1260 			 */
1261 			if (cmd_version > 1 && n_fields_this_loop >= 1) {
1262 				synth_err(SYNTH_ERR_INVALID_CMD, errpos(field_str));
1263 				ret = -EINVAL;
1264 				goto err_free_arg;
1265 			}
1266 
1267 			fields[n_fields++] = field;
1268 			if (n_fields == SYNTH_FIELDS_MAX) {
1269 				synth_err(SYNTH_ERR_TOO_MANY_FIELDS, 0);
1270 				ret = -EINVAL;
1271 				goto err_free_arg;
1272 			}
1273 
1274 			n_fields_this_loop++;
1275 		}
1276 		argv_free(argv);
1277 
1278 		if (consumed < argc) {
1279 			synth_err(SYNTH_ERR_INVALID_CMD, 0);
1280 			ret = -EINVAL;
1281 			goto err;
1282 		}
1283 
1284 	}
1285 
1286 	if (n_fields == 0) {
1287 		synth_err(SYNTH_ERR_INVALID_CMD, 0);
1288 		ret = -EINVAL;
1289 		goto err;
1290 	}
1291 
1292 	event = alloc_synth_event(name, n_fields, fields);
1293 	if (IS_ERR(event)) {
1294 		ret = PTR_ERR(event);
1295 		event = NULL;
1296 		goto err;
1297 	}
1298 	ret = register_synth_event(event);
1299 	if (!ret)
1300 		dyn_event_add(&event->devent, &event->call);
1301 	else
1302 		free_synth_event(event);
1303  out:
1304 	mutex_unlock(&event_mutex);
1305 
1306 	kfree(saved_fields);
1307 
1308 	return ret;
1309  err_free_arg:
1310 	argv_free(argv);
1311  err:
1312 	for (i = 0; i < n_fields; i++)
1313 		free_synth_field(fields[i]);
1314 
1315 	goto out;
1316 }
1317 
1318 /**
1319  * synth_event_create - Create a new synthetic event
1320  * @name: The name of the new synthetic event
1321  * @fields: An array of type/name field descriptions
1322  * @n_fields: The number of field descriptions contained in the fields array
1323  * @mod: The module creating the event, NULL if not created from a module
1324  *
1325  * Create a new synthetic event with the given name under the
1326  * trace/events/synthetic/ directory.  The event fields that will be
1327  * defined for the event should be passed in as an array of struct
1328  * synth_field_desc, and the number elements in the array passed in as
1329  * n_fields. Field ordering will retain the ordering given in the
1330  * fields array.
1331  *
1332  * If the new synthetic event is being created from a module, the mod
1333  * param must be non-NULL.  This will ensure that the trace buffer
1334  * won't contain unreadable events.
1335  *
1336  * The new synth event should be deleted using synth_event_delete()
1337  * function.  The new synthetic event can be generated from modules or
1338  * other kernel code using trace_synth_event() and related functions.
1339  *
1340  * Return: 0 if successful, error otherwise.
1341  */
1342 int synth_event_create(const char *name, struct synth_field_desc *fields,
1343 		       unsigned int n_fields, struct module *mod)
1344 {
1345 	struct dynevent_cmd cmd;
1346 	char *buf;
1347 	int ret;
1348 
1349 	buf = kzalloc(MAX_DYNEVENT_CMD_LEN, GFP_KERNEL);
1350 	if (!buf)
1351 		return -ENOMEM;
1352 
1353 	synth_event_cmd_init(&cmd, buf, MAX_DYNEVENT_CMD_LEN);
1354 
1355 	ret = synth_event_gen_cmd_array_start(&cmd, name, mod,
1356 					      fields, n_fields);
1357 	if (ret)
1358 		goto out;
1359 
1360 	ret = synth_event_gen_cmd_end(&cmd);
1361  out:
1362 	kfree(buf);
1363 
1364 	return ret;
1365 }
1366 EXPORT_SYMBOL_GPL(synth_event_create);
1367 
1368 static int destroy_synth_event(struct synth_event *se)
1369 {
1370 	int ret;
1371 
1372 	if (se->ref)
1373 		return -EBUSY;
1374 
1375 	if (trace_event_dyn_busy(&se->call))
1376 		return -EBUSY;
1377 
1378 	ret = unregister_synth_event(se);
1379 	if (!ret) {
1380 		dyn_event_remove(&se->devent);
1381 		free_synth_event(se);
1382 	}
1383 
1384 	return ret;
1385 }
1386 
1387 /**
1388  * synth_event_delete - Delete a synthetic event
1389  * @event_name: The name of the new synthetic event
1390  *
1391  * Delete a synthetic event that was created with synth_event_create().
1392  *
1393  * Return: 0 if successful, error otherwise.
1394  */
1395 int synth_event_delete(const char *event_name)
1396 {
1397 	struct synth_event *se = NULL;
1398 	struct module *mod = NULL;
1399 	int ret = -ENOENT;
1400 
1401 	mutex_lock(&event_mutex);
1402 	se = find_synth_event(event_name);
1403 	if (se) {
1404 		mod = se->mod;
1405 		ret = destroy_synth_event(se);
1406 	}
1407 	mutex_unlock(&event_mutex);
1408 
1409 	if (mod) {
1410 		mutex_lock(&trace_types_lock);
1411 		/*
1412 		 * It is safest to reset the ring buffer if the module
1413 		 * being unloaded registered any events that were
1414 		 * used. The only worry is if a new module gets
1415 		 * loaded, and takes on the same id as the events of
1416 		 * this module. When printing out the buffer, traced
1417 		 * events left over from this module may be passed to
1418 		 * the new module events and unexpected results may
1419 		 * occur.
1420 		 */
1421 		tracing_reset_all_online_cpus();
1422 		mutex_unlock(&trace_types_lock);
1423 	}
1424 
1425 	return ret;
1426 }
1427 EXPORT_SYMBOL_GPL(synth_event_delete);
1428 
1429 static int check_command(const char *raw_command)
1430 {
1431 	char **argv = NULL, *cmd, *saved_cmd, *name_and_field;
1432 	int argc, ret = 0;
1433 
1434 	cmd = saved_cmd = kstrdup(raw_command, GFP_KERNEL);
1435 	if (!cmd)
1436 		return -ENOMEM;
1437 
1438 	name_and_field = strsep(&cmd, ";");
1439 	if (!name_and_field) {
1440 		ret = -EINVAL;
1441 		goto free;
1442 	}
1443 
1444 	if (name_and_field[0] == '!')
1445 		goto free;
1446 
1447 	argv = argv_split(GFP_KERNEL, name_and_field, &argc);
1448 	if (!argv) {
1449 		ret = -ENOMEM;
1450 		goto free;
1451 	}
1452 	argv_free(argv);
1453 
1454 	if (argc < 3)
1455 		ret = -EINVAL;
1456 free:
1457 	kfree(saved_cmd);
1458 
1459 	return ret;
1460 }
1461 
1462 static int create_or_delete_synth_event(const char *raw_command)
1463 {
1464 	char *name = NULL, *fields, *p;
1465 	int ret = 0;
1466 
1467 	raw_command = skip_spaces(raw_command);
1468 	if (raw_command[0] == '\0')
1469 		return ret;
1470 
1471 	last_cmd_set(raw_command);
1472 
1473 	ret = check_command(raw_command);
1474 	if (ret) {
1475 		synth_err(SYNTH_ERR_INVALID_CMD, 0);
1476 		return ret;
1477 	}
1478 
1479 	p = strpbrk(raw_command, " \t");
1480 	if (!p && raw_command[0] != '!') {
1481 		synth_err(SYNTH_ERR_INVALID_CMD, 0);
1482 		ret = -EINVAL;
1483 		goto free;
1484 	}
1485 
1486 	name = kmemdup_nul(raw_command, p ? p - raw_command : strlen(raw_command), GFP_KERNEL);
1487 	if (!name)
1488 		return -ENOMEM;
1489 
1490 	if (name[0] == '!') {
1491 		ret = synth_event_delete(name + 1);
1492 		goto free;
1493 	}
1494 
1495 	fields = skip_spaces(p);
1496 
1497 	ret = __create_synth_event(name, fields);
1498 free:
1499 	kfree(name);
1500 
1501 	return ret;
1502 }
1503 
1504 static int synth_event_run_command(struct dynevent_cmd *cmd)
1505 {
1506 	struct synth_event *se;
1507 	int ret;
1508 
1509 	ret = create_or_delete_synth_event(cmd->seq.buffer);
1510 	if (ret)
1511 		return ret;
1512 
1513 	se = find_synth_event(cmd->event_name);
1514 	if (WARN_ON(!se))
1515 		return -ENOENT;
1516 
1517 	se->mod = cmd->private_data;
1518 
1519 	return ret;
1520 }
1521 
1522 /**
1523  * synth_event_cmd_init - Initialize a synthetic event command object
1524  * @cmd: A pointer to the dynevent_cmd struct representing the new event
1525  * @buf: A pointer to the buffer used to build the command
1526  * @maxlen: The length of the buffer passed in @buf
1527  *
1528  * Initialize a synthetic event command object.  Use this before
1529  * calling any of the other dyenvent_cmd functions.
1530  */
1531 void synth_event_cmd_init(struct dynevent_cmd *cmd, char *buf, int maxlen)
1532 {
1533 	dynevent_cmd_init(cmd, buf, maxlen, DYNEVENT_TYPE_SYNTH,
1534 			  synth_event_run_command);
1535 }
1536 EXPORT_SYMBOL_GPL(synth_event_cmd_init);
1537 
1538 static inline int
1539 __synth_event_trace_init(struct trace_event_file *file,
1540 			 struct synth_event_trace_state *trace_state)
1541 {
1542 	int ret = 0;
1543 
1544 	memset(trace_state, '\0', sizeof(*trace_state));
1545 
1546 	/*
1547 	 * Normal event tracing doesn't get called at all unless the
1548 	 * ENABLED bit is set (which attaches the probe thus allowing
1549 	 * this code to be called, etc).  Because this is called
1550 	 * directly by the user, we don't have that but we still need
1551 	 * to honor not logging when disabled.  For the iterated
1552 	 * trace case, we save the enabled state upon start and just
1553 	 * ignore the following data calls.
1554 	 */
1555 	if (!(file->flags & EVENT_FILE_FL_ENABLED) ||
1556 	    trace_trigger_soft_disabled(file)) {
1557 		trace_state->disabled = true;
1558 		ret = -ENOENT;
1559 		goto out;
1560 	}
1561 
1562 	trace_state->event = file->event_call->data;
1563 out:
1564 	return ret;
1565 }
1566 
1567 static inline int
1568 __synth_event_trace_start(struct trace_event_file *file,
1569 			  struct synth_event_trace_state *trace_state,
1570 			  int dynamic_fields_size)
1571 {
1572 	int entry_size, fields_size = 0;
1573 	int ret = 0;
1574 
1575 	fields_size = trace_state->event->n_u64 * sizeof(u64);
1576 	fields_size += dynamic_fields_size;
1577 
1578 	/*
1579 	 * Avoid ring buffer recursion detection, as this event
1580 	 * is being performed within another event.
1581 	 */
1582 	trace_state->buffer = file->tr->array_buffer.buffer;
1583 	ring_buffer_nest_start(trace_state->buffer);
1584 
1585 	entry_size = sizeof(*trace_state->entry) + fields_size;
1586 	trace_state->entry = trace_event_buffer_reserve(&trace_state->fbuffer,
1587 							file,
1588 							entry_size);
1589 	if (!trace_state->entry) {
1590 		ring_buffer_nest_end(trace_state->buffer);
1591 		ret = -EINVAL;
1592 	}
1593 
1594 	return ret;
1595 }
1596 
1597 static inline void
1598 __synth_event_trace_end(struct synth_event_trace_state *trace_state)
1599 {
1600 	trace_event_buffer_commit(&trace_state->fbuffer);
1601 
1602 	ring_buffer_nest_end(trace_state->buffer);
1603 }
1604 
1605 /**
1606  * synth_event_trace - Trace a synthetic event
1607  * @file: The trace_event_file representing the synthetic event
1608  * @n_vals: The number of values in vals
1609  * @args: Variable number of args containing the event values
1610  *
1611  * Trace a synthetic event using the values passed in the variable
1612  * argument list.
1613  *
1614  * The argument list should be a list 'n_vals' u64 values.  The number
1615  * of vals must match the number of field in the synthetic event, and
1616  * must be in the same order as the synthetic event fields.
1617  *
1618  * All vals should be cast to u64, and string vals are just pointers
1619  * to strings, cast to u64.  Strings will be copied into space
1620  * reserved in the event for the string, using these pointers.
1621  *
1622  * Return: 0 on success, err otherwise.
1623  */
1624 int synth_event_trace(struct trace_event_file *file, unsigned int n_vals, ...)
1625 {
1626 	unsigned int i, n_u64, len, data_size = 0;
1627 	struct synth_event_trace_state state;
1628 	va_list args;
1629 	int ret;
1630 
1631 	ret = __synth_event_trace_init(file, &state);
1632 	if (ret) {
1633 		if (ret == -ENOENT)
1634 			ret = 0; /* just disabled, not really an error */
1635 		return ret;
1636 	}
1637 
1638 	if (state.event->n_dynamic_fields) {
1639 		va_start(args, n_vals);
1640 
1641 		for (i = 0; i < state.event->n_fields; i++) {
1642 			u64 val = va_arg(args, u64);
1643 
1644 			if (state.event->fields[i]->is_string &&
1645 			    state.event->fields[i]->is_dynamic) {
1646 				char *str_val = (char *)(long)val;
1647 
1648 				data_size += strlen(str_val) + 1;
1649 			}
1650 		}
1651 
1652 		va_end(args);
1653 	}
1654 
1655 	ret = __synth_event_trace_start(file, &state, data_size);
1656 	if (ret)
1657 		return ret;
1658 
1659 	if (n_vals != state.event->n_fields) {
1660 		ret = -EINVAL;
1661 		goto out;
1662 	}
1663 
1664 	data_size = 0;
1665 
1666 	va_start(args, n_vals);
1667 	for (i = 0, n_u64 = 0; i < state.event->n_fields; i++) {
1668 		u64 val;
1669 
1670 		val = va_arg(args, u64);
1671 
1672 		if (state.event->fields[i]->is_string) {
1673 			char *str_val = (char *)(long)val;
1674 
1675 			len = trace_string(state.entry, state.event, str_val,
1676 					   state.event->fields[i]->is_dynamic,
1677 					   data_size, &n_u64);
1678 			data_size += len; /* only dynamic string increments */
1679 		} else {
1680 			struct synth_field *field = state.event->fields[i];
1681 
1682 			switch (field->size) {
1683 			case 1:
1684 				*(u8 *)&state.entry->fields[n_u64] = (u8)val;
1685 				break;
1686 
1687 			case 2:
1688 				*(u16 *)&state.entry->fields[n_u64] = (u16)val;
1689 				break;
1690 
1691 			case 4:
1692 				*(u32 *)&state.entry->fields[n_u64] = (u32)val;
1693 				break;
1694 
1695 			default:
1696 				state.entry->fields[n_u64] = val;
1697 				break;
1698 			}
1699 			n_u64++;
1700 		}
1701 	}
1702 	va_end(args);
1703 out:
1704 	__synth_event_trace_end(&state);
1705 
1706 	return ret;
1707 }
1708 EXPORT_SYMBOL_GPL(synth_event_trace);
1709 
1710 /**
1711  * synth_event_trace_array - Trace a synthetic event from an array
1712  * @file: The trace_event_file representing the synthetic event
1713  * @vals: Array of values
1714  * @n_vals: The number of values in vals
1715  *
1716  * Trace a synthetic event using the values passed in as 'vals'.
1717  *
1718  * The 'vals' array is just an array of 'n_vals' u64.  The number of
1719  * vals must match the number of field in the synthetic event, and
1720  * must be in the same order as the synthetic event fields.
1721  *
1722  * All vals should be cast to u64, and string vals are just pointers
1723  * to strings, cast to u64.  Strings will be copied into space
1724  * reserved in the event for the string, using these pointers.
1725  *
1726  * Return: 0 on success, err otherwise.
1727  */
1728 int synth_event_trace_array(struct trace_event_file *file, u64 *vals,
1729 			    unsigned int n_vals)
1730 {
1731 	unsigned int i, n_u64, field_pos, len, data_size = 0;
1732 	struct synth_event_trace_state state;
1733 	char *str_val;
1734 	int ret;
1735 
1736 	ret = __synth_event_trace_init(file, &state);
1737 	if (ret) {
1738 		if (ret == -ENOENT)
1739 			ret = 0; /* just disabled, not really an error */
1740 		return ret;
1741 	}
1742 
1743 	if (state.event->n_dynamic_fields) {
1744 		for (i = 0; i < state.event->n_dynamic_fields; i++) {
1745 			field_pos = state.event->dynamic_fields[i]->field_pos;
1746 			str_val = (char *)(long)vals[field_pos];
1747 			len = strlen(str_val) + 1;
1748 			data_size += len;
1749 		}
1750 	}
1751 
1752 	ret = __synth_event_trace_start(file, &state, data_size);
1753 	if (ret)
1754 		return ret;
1755 
1756 	if (n_vals != state.event->n_fields) {
1757 		ret = -EINVAL;
1758 		goto out;
1759 	}
1760 
1761 	data_size = 0;
1762 
1763 	for (i = 0, n_u64 = 0; i < state.event->n_fields; i++) {
1764 		if (state.event->fields[i]->is_string) {
1765 			char *str_val = (char *)(long)vals[i];
1766 
1767 			len = trace_string(state.entry, state.event, str_val,
1768 					   state.event->fields[i]->is_dynamic,
1769 					   data_size, &n_u64);
1770 			data_size += len; /* only dynamic string increments */
1771 		} else {
1772 			struct synth_field *field = state.event->fields[i];
1773 			u64 val = vals[i];
1774 
1775 			switch (field->size) {
1776 			case 1:
1777 				*(u8 *)&state.entry->fields[n_u64] = (u8)val;
1778 				break;
1779 
1780 			case 2:
1781 				*(u16 *)&state.entry->fields[n_u64] = (u16)val;
1782 				break;
1783 
1784 			case 4:
1785 				*(u32 *)&state.entry->fields[n_u64] = (u32)val;
1786 				break;
1787 
1788 			default:
1789 				state.entry->fields[n_u64] = val;
1790 				break;
1791 			}
1792 			n_u64++;
1793 		}
1794 	}
1795 out:
1796 	__synth_event_trace_end(&state);
1797 
1798 	return ret;
1799 }
1800 EXPORT_SYMBOL_GPL(synth_event_trace_array);
1801 
1802 /**
1803  * synth_event_trace_start - Start piecewise synthetic event trace
1804  * @file: The trace_event_file representing the synthetic event
1805  * @trace_state: A pointer to object tracking the piecewise trace state
1806  *
1807  * Start the trace of a synthetic event field-by-field rather than all
1808  * at once.
1809  *
1810  * This function 'opens' an event trace, which means space is reserved
1811  * for the event in the trace buffer, after which the event's
1812  * individual field values can be set through either
1813  * synth_event_add_next_val() or synth_event_add_val().
1814  *
1815  * A pointer to a trace_state object is passed in, which will keep
1816  * track of the current event trace state until the event trace is
1817  * closed (and the event finally traced) using
1818  * synth_event_trace_end().
1819  *
1820  * Note that synth_event_trace_end() must be called after all values
1821  * have been added for each event trace, regardless of whether adding
1822  * all field values succeeded or not.
1823  *
1824  * Note also that for a given event trace, all fields must be added
1825  * using either synth_event_add_next_val() or synth_event_add_val()
1826  * but not both together or interleaved.
1827  *
1828  * Return: 0 on success, err otherwise.
1829  */
1830 int synth_event_trace_start(struct trace_event_file *file,
1831 			    struct synth_event_trace_state *trace_state)
1832 {
1833 	int ret;
1834 
1835 	if (!trace_state)
1836 		return -EINVAL;
1837 
1838 	ret = __synth_event_trace_init(file, trace_state);
1839 	if (ret) {
1840 		if (ret == -ENOENT)
1841 			ret = 0; /* just disabled, not really an error */
1842 		return ret;
1843 	}
1844 
1845 	if (trace_state->event->n_dynamic_fields)
1846 		return -ENOTSUPP;
1847 
1848 	ret = __synth_event_trace_start(file, trace_state, 0);
1849 
1850 	return ret;
1851 }
1852 EXPORT_SYMBOL_GPL(synth_event_trace_start);
1853 
1854 static int __synth_event_add_val(const char *field_name, u64 val,
1855 				 struct synth_event_trace_state *trace_state)
1856 {
1857 	struct synth_field *field = NULL;
1858 	struct synth_trace_event *entry;
1859 	struct synth_event *event;
1860 	int i, ret = 0;
1861 
1862 	if (!trace_state) {
1863 		ret = -EINVAL;
1864 		goto out;
1865 	}
1866 
1867 	/* can't mix add_next_synth_val() with add_synth_val() */
1868 	if (field_name) {
1869 		if (trace_state->add_next) {
1870 			ret = -EINVAL;
1871 			goto out;
1872 		}
1873 		trace_state->add_name = true;
1874 	} else {
1875 		if (trace_state->add_name) {
1876 			ret = -EINVAL;
1877 			goto out;
1878 		}
1879 		trace_state->add_next = true;
1880 	}
1881 
1882 	if (trace_state->disabled)
1883 		goto out;
1884 
1885 	event = trace_state->event;
1886 	if (trace_state->add_name) {
1887 		for (i = 0; i < event->n_fields; i++) {
1888 			field = event->fields[i];
1889 			if (strcmp(field->name, field_name) == 0)
1890 				break;
1891 		}
1892 		if (!field) {
1893 			ret = -EINVAL;
1894 			goto out;
1895 		}
1896 	} else {
1897 		if (trace_state->cur_field >= event->n_fields) {
1898 			ret = -EINVAL;
1899 			goto out;
1900 		}
1901 		field = event->fields[trace_state->cur_field++];
1902 	}
1903 
1904 	entry = trace_state->entry;
1905 	if (field->is_string) {
1906 		char *str_val = (char *)(long)val;
1907 		char *str_field;
1908 
1909 		if (field->is_dynamic) { /* add_val can't do dynamic strings */
1910 			ret = -EINVAL;
1911 			goto out;
1912 		}
1913 
1914 		if (!str_val) {
1915 			ret = -EINVAL;
1916 			goto out;
1917 		}
1918 
1919 		str_field = (char *)&entry->fields[field->offset];
1920 		strscpy(str_field, str_val, STR_VAR_LEN_MAX);
1921 	} else {
1922 		switch (field->size) {
1923 		case 1:
1924 			*(u8 *)&trace_state->entry->fields[field->offset] = (u8)val;
1925 			break;
1926 
1927 		case 2:
1928 			*(u16 *)&trace_state->entry->fields[field->offset] = (u16)val;
1929 			break;
1930 
1931 		case 4:
1932 			*(u32 *)&trace_state->entry->fields[field->offset] = (u32)val;
1933 			break;
1934 
1935 		default:
1936 			trace_state->entry->fields[field->offset] = val;
1937 			break;
1938 		}
1939 	}
1940  out:
1941 	return ret;
1942 }
1943 
1944 /**
1945  * synth_event_add_next_val - Add the next field's value to an open synth trace
1946  * @val: The value to set the next field to
1947  * @trace_state: A pointer to object tracking the piecewise trace state
1948  *
1949  * Set the value of the next field in an event that's been opened by
1950  * synth_event_trace_start().
1951  *
1952  * The val param should be the value cast to u64.  If the value points
1953  * to a string, the val param should be a char * cast to u64.
1954  *
1955  * This function assumes all the fields in an event are to be set one
1956  * after another - successive calls to this function are made, one for
1957  * each field, in the order of the fields in the event, until all
1958  * fields have been set.  If you'd rather set each field individually
1959  * without regard to ordering, synth_event_add_val() can be used
1960  * instead.
1961  *
1962  * Note however that synth_event_add_next_val() and
1963  * synth_event_add_val() can't be intermixed for a given event trace -
1964  * one or the other but not both can be used at the same time.
1965  *
1966  * Note also that synth_event_trace_end() must be called after all
1967  * values have been added for each event trace, regardless of whether
1968  * adding all field values succeeded or not.
1969  *
1970  * Return: 0 on success, err otherwise.
1971  */
1972 int synth_event_add_next_val(u64 val,
1973 			     struct synth_event_trace_state *trace_state)
1974 {
1975 	return __synth_event_add_val(NULL, val, trace_state);
1976 }
1977 EXPORT_SYMBOL_GPL(synth_event_add_next_val);
1978 
1979 /**
1980  * synth_event_add_val - Add a named field's value to an open synth trace
1981  * @field_name: The name of the synthetic event field value to set
1982  * @val: The value to set the named field to
1983  * @trace_state: A pointer to object tracking the piecewise trace state
1984  *
1985  * Set the value of the named field in an event that's been opened by
1986  * synth_event_trace_start().
1987  *
1988  * The val param should be the value cast to u64.  If the value points
1989  * to a string, the val param should be a char * cast to u64.
1990  *
1991  * This function looks up the field name, and if found, sets the field
1992  * to the specified value.  This lookup makes this function more
1993  * expensive than synth_event_add_next_val(), so use that or the
1994  * none-piecewise synth_event_trace() instead if efficiency is more
1995  * important.
1996  *
1997  * Note however that synth_event_add_next_val() and
1998  * synth_event_add_val() can't be intermixed for a given event trace -
1999  * one or the other but not both can be used at the same time.
2000  *
2001  * Note also that synth_event_trace_end() must be called after all
2002  * values have been added for each event trace, regardless of whether
2003  * adding all field values succeeded or not.
2004  *
2005  * Return: 0 on success, err otherwise.
2006  */
2007 int synth_event_add_val(const char *field_name, u64 val,
2008 			struct synth_event_trace_state *trace_state)
2009 {
2010 	return __synth_event_add_val(field_name, val, trace_state);
2011 }
2012 EXPORT_SYMBOL_GPL(synth_event_add_val);
2013 
2014 /**
2015  * synth_event_trace_end - End piecewise synthetic event trace
2016  * @trace_state: A pointer to object tracking the piecewise trace state
2017  *
2018  * End the trace of a synthetic event opened by
2019  * synth_event_trace__start().
2020  *
2021  * This function 'closes' an event trace, which basically means that
2022  * it commits the reserved event and cleans up other loose ends.
2023  *
2024  * A pointer to a trace_state object is passed in, which will keep
2025  * track of the current event trace state opened with
2026  * synth_event_trace_start().
2027  *
2028  * Note that this function must be called after all values have been
2029  * added for each event trace, regardless of whether adding all field
2030  * values succeeded or not.
2031  *
2032  * Return: 0 on success, err otherwise.
2033  */
2034 int synth_event_trace_end(struct synth_event_trace_state *trace_state)
2035 {
2036 	if (!trace_state)
2037 		return -EINVAL;
2038 
2039 	__synth_event_trace_end(trace_state);
2040 
2041 	return 0;
2042 }
2043 EXPORT_SYMBOL_GPL(synth_event_trace_end);
2044 
2045 static int create_synth_event(const char *raw_command)
2046 {
2047 	char *fields, *p;
2048 	const char *name;
2049 	int len, ret = 0;
2050 
2051 	raw_command = skip_spaces(raw_command);
2052 	if (raw_command[0] == '\0')
2053 		return ret;
2054 
2055 	last_cmd_set(raw_command);
2056 
2057 	name = raw_command;
2058 
2059 	/* Don't try to process if not our system */
2060 	if (name[0] != 's' || name[1] != ':')
2061 		return -ECANCELED;
2062 	name += 2;
2063 
2064 	p = strpbrk(raw_command, " \t");
2065 	if (!p) {
2066 		synth_err(SYNTH_ERR_INVALID_CMD, 0);
2067 		return -EINVAL;
2068 	}
2069 
2070 	fields = skip_spaces(p);
2071 
2072 	/* This interface accepts group name prefix */
2073 	if (strchr(name, '/')) {
2074 		len = str_has_prefix(name, SYNTH_SYSTEM "/");
2075 		if (len == 0) {
2076 			synth_err(SYNTH_ERR_INVALID_DYN_CMD, 0);
2077 			return -EINVAL;
2078 		}
2079 		name += len;
2080 	}
2081 
2082 	len = name - raw_command;
2083 
2084 	ret = check_command(raw_command + len);
2085 	if (ret) {
2086 		synth_err(SYNTH_ERR_INVALID_CMD, 0);
2087 		return ret;
2088 	}
2089 
2090 	name = kmemdup_nul(raw_command + len, p - raw_command - len, GFP_KERNEL);
2091 	if (!name)
2092 		return -ENOMEM;
2093 
2094 	ret = __create_synth_event(name, fields);
2095 
2096 	kfree(name);
2097 
2098 	return ret;
2099 }
2100 
2101 static int synth_event_release(struct dyn_event *ev)
2102 {
2103 	struct synth_event *event = to_synth_event(ev);
2104 	int ret;
2105 
2106 	if (event->ref)
2107 		return -EBUSY;
2108 
2109 	if (trace_event_dyn_busy(&event->call))
2110 		return -EBUSY;
2111 
2112 	ret = unregister_synth_event(event);
2113 	if (ret)
2114 		return ret;
2115 
2116 	dyn_event_remove(ev);
2117 	free_synth_event(event);
2118 	return 0;
2119 }
2120 
2121 static int __synth_event_show(struct seq_file *m, struct synth_event *event)
2122 {
2123 	struct synth_field *field;
2124 	unsigned int i;
2125 	char *type, *t;
2126 
2127 	seq_printf(m, "%s\t", event->name);
2128 
2129 	for (i = 0; i < event->n_fields; i++) {
2130 		field = event->fields[i];
2131 
2132 		type = field->type;
2133 		t = strstr(type, "__data_loc");
2134 		if (t) { /* __data_loc belongs in format but not event desc */
2135 			t += sizeof("__data_loc");
2136 			type = t;
2137 		}
2138 
2139 		/* parameter values */
2140 		seq_printf(m, "%s %s%s", type, field->name,
2141 			   i == event->n_fields - 1 ? "" : "; ");
2142 	}
2143 
2144 	seq_putc(m, '\n');
2145 
2146 	return 0;
2147 }
2148 
2149 static int synth_event_show(struct seq_file *m, struct dyn_event *ev)
2150 {
2151 	struct synth_event *event = to_synth_event(ev);
2152 
2153 	seq_printf(m, "s:%s/", event->class.system);
2154 
2155 	return __synth_event_show(m, event);
2156 }
2157 
2158 static int synth_events_seq_show(struct seq_file *m, void *v)
2159 {
2160 	struct dyn_event *ev = v;
2161 
2162 	if (!is_synth_event(ev))
2163 		return 0;
2164 
2165 	return __synth_event_show(m, to_synth_event(ev));
2166 }
2167 
2168 static const struct seq_operations synth_events_seq_op = {
2169 	.start	= dyn_event_seq_start,
2170 	.next	= dyn_event_seq_next,
2171 	.stop	= dyn_event_seq_stop,
2172 	.show	= synth_events_seq_show,
2173 };
2174 
2175 static int synth_events_open(struct inode *inode, struct file *file)
2176 {
2177 	int ret;
2178 
2179 	ret = security_locked_down(LOCKDOWN_TRACEFS);
2180 	if (ret)
2181 		return ret;
2182 
2183 	if ((file->f_mode & FMODE_WRITE) && (file->f_flags & O_TRUNC)) {
2184 		ret = dyn_events_release_all(&synth_event_ops);
2185 		if (ret < 0)
2186 			return ret;
2187 	}
2188 
2189 	return seq_open(file, &synth_events_seq_op);
2190 }
2191 
2192 static ssize_t synth_events_write(struct file *file,
2193 				  const char __user *buffer,
2194 				  size_t count, loff_t *ppos)
2195 {
2196 	return trace_parse_run_command(file, buffer, count, ppos,
2197 				       create_or_delete_synth_event);
2198 }
2199 
2200 static const struct file_operations synth_events_fops = {
2201 	.open           = synth_events_open,
2202 	.write		= synth_events_write,
2203 	.read           = seq_read,
2204 	.llseek         = seq_lseek,
2205 	.release        = seq_release,
2206 };
2207 
2208 /*
2209  * Register dynevent at core_initcall. This allows kernel to setup kprobe
2210  * events in postcore_initcall without tracefs.
2211  */
2212 static __init int trace_events_synth_init_early(void)
2213 {
2214 	int err = 0;
2215 
2216 	err = dyn_event_register(&synth_event_ops);
2217 	if (err)
2218 		pr_warn("Could not register synth_event_ops\n");
2219 
2220 	return err;
2221 }
2222 core_initcall(trace_events_synth_init_early);
2223 
2224 static __init int trace_events_synth_init(void)
2225 {
2226 	struct dentry *entry = NULL;
2227 	int err = 0;
2228 	err = tracing_init_dentry();
2229 	if (err)
2230 		goto err;
2231 
2232 	entry = tracefs_create_file("synthetic_events", TRACE_MODE_WRITE,
2233 				    NULL, NULL, &synth_events_fops);
2234 	if (!entry) {
2235 		err = -ENODEV;
2236 		goto err;
2237 	}
2238 
2239 	return err;
2240  err:
2241 	pr_warn("Could not create tracefs 'synthetic_events' entry\n");
2242 
2243 	return err;
2244 }
2245 
2246 fs_initcall(trace_events_synth_init);
2247