Linux Audio

Check our new training course

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