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