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