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