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