xref: /linux/kernel/trace/trace_eprobe.c (revision f16d85cd3de9d0c761b74155b4cbe2f20d3dcf80)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * event probes
4  *
5  * Part of this code was copied from kernel/trace/trace_kprobe.c written by
6  * Masami Hiramatsu <mhiramat@kernel.org>
7  *
8  * Copyright (C) 2021, VMware Inc, Steven Rostedt <rostedt@goodmis.org>
9  * Copyright (C) 2021, VMware Inc, Tzvetomir Stoyanov tz.stoyanov@gmail.com>
10  *
11  */
12 #include <linux/cleanup.h>
13 #include <linux/ftrace.h>
14 #include <linux/module.h>
15 #include <linux/mutex.h>
16 
17 #include "trace_dynevent.h"
18 #include "trace_probe.h"
19 #include "trace_probe_kernel.h"
20 #include "trace_probe_tmpl.h"
21 
22 #define EPROBE_EVENT_SYSTEM "eprobes"
23 
24 struct trace_eprobe {
25 	/* tracepoint system */
26 	const char *event_system;
27 
28 	/* tracepoint event */
29 	const char *event_name;
30 
31 	/* filter string for the tracepoint */
32 	char *filter_str;
33 
34 	struct trace_event_call *event;
35 
36 	struct dyn_event	devent;
37 	struct trace_probe	tp;
38 };
39 
40 struct eprobe_data {
41 	struct trace_event_file	*file;
42 	struct trace_eprobe	*ep;
43 };
44 
45 
46 #define for_each_trace_eprobe_tp(ep, _tp) \
47 	list_for_each_entry(ep, trace_probe_probe_list(_tp), tp.list)
48 
49 static int __trace_eprobe_create(int argc, const char *argv[]);
50 
51 static void trace_event_probe_cleanup(struct trace_eprobe *ep)
52 {
53 	if (!ep)
54 		return;
55 	trace_probe_cleanup(&ep->tp);
56 	kfree(ep->event_name);
57 	kfree(ep->event_system);
58 	if (ep->event)
59 		trace_event_put_ref(ep->event);
60 	kfree(ep->filter_str);
61 	kfree(ep);
62 }
63 
64 DEFINE_FREE(trace_event_probe_cleanup, struct trace_eprobe *,
65 		if (!IS_ERR_OR_NULL(_T)) trace_event_probe_cleanup(_T))
66 
67 static struct trace_eprobe *to_trace_eprobe(struct dyn_event *ev)
68 {
69 	return container_of(ev, struct trace_eprobe, devent);
70 }
71 
72 static int eprobe_dyn_event_create(const char *raw_command)
73 {
74 	return trace_probe_create(raw_command, __trace_eprobe_create);
75 }
76 
77 static int eprobe_dyn_event_show(struct seq_file *m, struct dyn_event *ev)
78 {
79 	struct trace_eprobe *ep = to_trace_eprobe(ev);
80 	int i;
81 
82 	seq_printf(m, "e:%s/%s", trace_probe_group_name(&ep->tp),
83 				trace_probe_name(&ep->tp));
84 	seq_printf(m, " %s.%s", ep->event_system, ep->event_name);
85 
86 	for (i = 0; i < ep->tp.nr_args; i++)
87 		seq_printf(m, " %s=%s", ep->tp.args[i].name, ep->tp.args[i].comm);
88 	seq_putc(m, '\n');
89 
90 	trace_probe_dump_args(m, &ep->tp);
91 
92 	return 0;
93 }
94 
95 static int unregister_trace_eprobe(struct trace_eprobe *ep)
96 {
97 	/* If other probes are on the event, just unregister eprobe */
98 	if (trace_probe_has_sibling(&ep->tp))
99 		goto unreg;
100 
101 	/* Enabled event can not be unregistered */
102 	if (trace_probe_is_enabled(&ep->tp))
103 		return -EBUSY;
104 
105 	/* Will fail if probe is being used by ftrace or perf */
106 	if (trace_probe_unregister_event_call(&ep->tp))
107 		return -EBUSY;
108 
109 unreg:
110 	dyn_event_remove(&ep->devent);
111 	trace_probe_unlink(&ep->tp);
112 
113 	return 0;
114 }
115 
116 static int eprobe_dyn_event_release(struct dyn_event *ev)
117 {
118 	struct trace_eprobe *ep = to_trace_eprobe(ev);
119 	int ret = unregister_trace_eprobe(ep);
120 
121 	if (!ret)
122 		trace_event_probe_cleanup(ep);
123 	return ret;
124 }
125 
126 static bool eprobe_dyn_event_is_busy(struct dyn_event *ev)
127 {
128 	struct trace_eprobe *ep = to_trace_eprobe(ev);
129 
130 	return trace_probe_is_enabled(&ep->tp);
131 }
132 
133 static bool eprobe_dyn_event_match(const char *system, const char *event,
134 			int argc, const char **argv, struct dyn_event *ev)
135 {
136 	struct trace_eprobe *ep = to_trace_eprobe(ev);
137 	const char *slash;
138 
139 	/*
140 	 * We match the following:
141 	 *  event only			- match all eprobes with event name
142 	 *  system and event only	- match all system/event probes
143 	 *  system only			- match all system probes
144 	 *
145 	 * The below has the above satisfied with more arguments:
146 	 *
147 	 *  attached system/event	- If the arg has the system and event
148 	 *				  the probe is attached to, match
149 	 *				  probes with the attachment.
150 	 *
151 	 *  If any more args are given, then it requires a full match.
152 	 */
153 
154 	/*
155 	 * If system exists, but this probe is not part of that system
156 	 * do not match.
157 	 */
158 	if (system && strcmp(trace_probe_group_name(&ep->tp), system) != 0)
159 		return false;
160 
161 	/* Must match the event name */
162 	if (event[0] != '\0' && strcmp(trace_probe_name(&ep->tp), event) != 0)
163 		return false;
164 
165 	/* No arguments match all */
166 	if (argc < 1)
167 		return true;
168 
169 	/* First argument is the system/event the probe is attached to */
170 
171 	slash = strchr(argv[0], '/');
172 	if (!slash)
173 		slash = strchr(argv[0], '.');
174 	if (!slash)
175 		return false;
176 
177 	if (strncmp(ep->event_system, argv[0], slash - argv[0]) ||
178 	    ep->event_system[slash - argv[0]] != '\0')
179 		return false;
180 	if (strcmp(ep->event_name, slash + 1))
181 		return false;
182 
183 	argc--;
184 	argv++;
185 
186 	/* If there are no other args, then match */
187 	if (argc < 1)
188 		return true;
189 
190 	return trace_probe_match_command_args(&ep->tp, argc, argv);
191 }
192 
193 static struct dyn_event_operations eprobe_dyn_event_ops = {
194 	.create = eprobe_dyn_event_create,
195 	.show = eprobe_dyn_event_show,
196 	.is_busy = eprobe_dyn_event_is_busy,
197 	.free = eprobe_dyn_event_release,
198 	.match = eprobe_dyn_event_match,
199 };
200 
201 static struct trace_eprobe *alloc_event_probe(const char *group,
202 					      const char *this_event,
203 					      struct trace_event_call *event,
204 					      int nargs)
205 {
206 	struct trace_eprobe *ep __free(trace_event_probe_cleanup) = NULL;
207 	const char *event_name;
208 	const char *sys_name;
209 	int ret;
210 
211 	if (!event)
212 		return ERR_PTR(-ENODEV);
213 
214 	sys_name = event->class->system;
215 	event_name = trace_event_name(event);
216 
217 	ep = kzalloc_flex(*ep, tp.args, nargs);
218 	if (!ep) {
219 		trace_event_put_ref(event);
220 		return ERR_PTR(-ENOMEM);
221 	}
222 	ep->event = event;
223 	ep->event_name = kstrdup(event_name, GFP_KERNEL);
224 	if (!ep->event_name)
225 		return ERR_PTR(-ENOMEM);
226 	ep->event_system = kstrdup(sys_name, GFP_KERNEL);
227 	if (!ep->event_system)
228 		return ERR_PTR(-ENOMEM);
229 
230 	ret = trace_probe_init(&ep->tp, this_event, group, false, nargs);
231 	if (ret < 0)
232 		return ERR_PTR(ret);
233 
234 	dyn_event_init(&ep->devent, &eprobe_dyn_event_ops);
235 	return_ptr(ep);
236 }
237 
238 static int eprobe_event_define_fields(struct trace_event_call *event_call)
239 {
240 	struct eprobe_trace_entry_head field;
241 	struct trace_probe *tp;
242 
243 	tp = trace_probe_primary_from_call(event_call);
244 	if (WARN_ON_ONCE(!tp))
245 		return -ENOENT;
246 
247 	return traceprobe_define_arg_fields(event_call, sizeof(field), tp);
248 }
249 
250 static struct trace_event_fields eprobe_fields_array[] = {
251 	{ .type = TRACE_FUNCTION_TYPE,
252 	  .define_fields = eprobe_event_define_fields },
253 	{}
254 };
255 
256 /* Event entry printers */
257 static enum print_line_t
258 print_eprobe_event(struct trace_iterator *iter, int flags,
259 		   struct trace_event *event)
260 {
261 	struct eprobe_trace_entry_head *field;
262 	struct trace_event_call *pevent;
263 	struct trace_event *probed_event;
264 	struct trace_seq *s = &iter->seq;
265 	struct trace_eprobe *ep;
266 	struct trace_probe *tp;
267 	unsigned int type;
268 
269 	field = (struct eprobe_trace_entry_head *)iter->ent;
270 	tp = trace_probe_primary_from_call(
271 		container_of(event, struct trace_event_call, event));
272 	if (WARN_ON_ONCE(!tp))
273 		goto out;
274 
275 	ep = container_of(tp, struct trace_eprobe, tp);
276 	type = ep->event->event.type;
277 
278 	trace_seq_printf(s, "%s: (", trace_probe_name(tp));
279 
280 	probed_event = ftrace_find_event(type);
281 	if (probed_event) {
282 		pevent = container_of(probed_event, struct trace_event_call, event);
283 		trace_seq_printf(s, "%s.%s", pevent->class->system,
284 				 trace_event_name(pevent));
285 	} else {
286 		trace_seq_printf(s, "%u", type);
287 	}
288 
289 	trace_seq_putc(s, ')');
290 
291 	if (trace_probe_print_args(s, tp->args, tp->nr_args,
292 			     (u8 *)&field[1], field) < 0)
293 		goto out;
294 
295 	trace_seq_putc(s, '\n');
296  out:
297 	return trace_handle_return(s);
298 }
299 
300 static nokprobe_inline unsigned long
301 get_event_field(struct fetch_insn *code, void *rec)
302 {
303 	struct ftrace_event_field *field = code->data;
304 	unsigned long val;
305 	void *addr;
306 
307 	addr = rec + field->offset;
308 
309 	if (is_string_field(field)) {
310 		switch (field->filter_type) {
311 		case FILTER_DYN_STRING:
312 			val = (unsigned long)(rec + (*(unsigned int *)addr & 0xffff));
313 			break;
314 		case FILTER_RDYN_STRING:
315 			val = (unsigned long)(addr + (*(unsigned int *)addr & 0xffff));
316 			break;
317 		case FILTER_STATIC_STRING:
318 			val = (unsigned long)addr;
319 			break;
320 		case FILTER_PTR_STRING:
321 			val = *(unsigned long *)addr;
322 			break;
323 		default:
324 			WARN_ON_ONCE(1);
325 			return 0;
326 		}
327 		return val;
328 	}
329 
330 	switch (field->size) {
331 	case 1:
332 		if (field->is_signed)
333 			val = *(char *)addr;
334 		else
335 			val = *(unsigned char *)addr;
336 		break;
337 	case 2:
338 		if (field->is_signed)
339 			val = *(short *)addr;
340 		else
341 			val = *(unsigned short *)addr;
342 		break;
343 	case 4:
344 		if (field->is_signed)
345 			val = *(int *)addr;
346 		else
347 			val = *(unsigned int *)addr;
348 		break;
349 	default:
350 		if (field->size == sizeof(long)) {
351 			if (field->is_signed)
352 				val = *(long *)addr;
353 			else
354 				val = *(unsigned long *)addr;
355 			break;
356 		}
357 		/* This is an array, point to the addr itself */
358 		val = (unsigned long)addr;
359 		break;
360 	}
361 	return val;
362 }
363 
364 static int get_eprobe_size(struct trace_probe *tp, void *rec)
365 {
366 	struct fetch_insn *code;
367 	struct probe_arg *arg;
368 	int i, len, ret = 0;
369 
370 	for (i = 0; i < tp->nr_args; i++) {
371 		arg = tp->args + i;
372 		if (arg->dynamic) {
373 			unsigned long val;
374 
375 			code = arg->code;
376  retry:
377 			switch (code->op) {
378 			case FETCH_OP_TP_ARG:
379 				val = get_event_field(code, rec);
380 				break;
381 			case FETCH_NOP_SYMBOL:	/* Ignore a place holder */
382 				code++;
383 				goto retry;
384 			default:
385 				if (process_common_fetch_insn(code, &val) < 0)
386 					continue;
387 			}
388 			code++;
389 			len = process_fetch_insn_bottom(code, val, NULL, NULL);
390 			if (len > 0)
391 				ret += len;
392 		}
393 	}
394 
395 	return ret;
396 }
397 
398 /* Kprobe specific fetch functions */
399 
400 /* Note that we don't verify it, since the code does not come from user space */
401 static int
402 process_fetch_insn(struct fetch_insn *code, void *rec, void *edata,
403 		   void *dest, void *base)
404 {
405 	unsigned long val;
406 	int ret;
407 
408  retry:
409 	switch (code->op) {
410 	case FETCH_OP_TP_ARG:
411 		val = get_event_field(code, rec);
412 		break;
413 	case FETCH_NOP_SYMBOL:	/* Ignore a place holder */
414 		code++;
415 		goto retry;
416 	default:
417 		ret = process_common_fetch_insn(code, &val);
418 		if (ret < 0)
419 			return ret;
420 	}
421 	code++;
422 	return process_fetch_insn_bottom(code, val, dest, base);
423 }
424 NOKPROBE_SYMBOL(process_fetch_insn)
425 
426 /* eprobe handler */
427 static inline void
428 __eprobe_trace_func(struct eprobe_data *edata, void *rec)
429 {
430 	struct eprobe_trace_entry_head *entry;
431 	struct trace_event_call *call = trace_probe_event_call(&edata->ep->tp);
432 	struct trace_event_buffer fbuffer;
433 	int dsize;
434 
435 	if (WARN_ON_ONCE(call != edata->file->event_call))
436 		return;
437 
438 	if (trace_trigger_soft_disabled(edata->file))
439 		return;
440 
441 	dsize = get_eprobe_size(&edata->ep->tp, rec);
442 
443 	entry = trace_event_buffer_reserve(&fbuffer, edata->file,
444 					   sizeof(*entry) + edata->ep->tp.size + dsize);
445 
446 	if (!entry)
447 		return;
448 
449 	entry = fbuffer.entry = ring_buffer_event_data(fbuffer.event);
450 	store_trace_args(&entry[1], &edata->ep->tp, rec, NULL, sizeof(*entry), dsize);
451 
452 	trace_event_buffer_commit(&fbuffer);
453 }
454 
455 /*
456  * The event probe implementation uses event triggers to get access to
457  * the event it is attached to, but is not an actual trigger. The below
458  * functions are just stubs to fulfill what is needed to use the trigger
459  * infrastructure.
460  */
461 static int eprobe_trigger_init(struct event_trigger_data *data)
462 {
463 	return 0;
464 }
465 
466 static void eprobe_trigger_free(struct event_trigger_data *data)
467 {
468 
469 }
470 
471 static int eprobe_trigger_print(struct seq_file *m,
472 				struct event_trigger_data *data)
473 {
474 	/* Do not print eprobe event triggers */
475 	return 0;
476 }
477 
478 static void eprobe_trigger_func(struct event_trigger_data *data,
479 				struct trace_buffer *buffer, void *rec,
480 				struct ring_buffer_event *rbe)
481 {
482 	struct eprobe_data *edata = data->private_data;
483 
484 	if (unlikely(!rec))
485 		return;
486 
487 	__eprobe_trace_func(edata, rec);
488 }
489 
490 static int eprobe_trigger_cmd_parse(struct event_command *cmd_ops,
491 				    struct trace_event_file *file,
492 				    char *glob, char *cmd,
493 				    char *param_and_filter)
494 {
495 	return -1;
496 }
497 
498 static int eprobe_trigger_reg_func(char *glob,
499 				   struct event_trigger_data *data,
500 				   struct trace_event_file *file)
501 {
502 	return -1;
503 }
504 
505 static void eprobe_trigger_unreg_func(char *glob,
506 				      struct event_trigger_data *data,
507 				      struct trace_event_file *file)
508 {
509 
510 }
511 
512 static struct event_command event_trigger_cmd = {
513 	.name			= "eprobe",
514 	.trigger_type		= ETT_EVENT_EPROBE,
515 	.flags			= EVENT_CMD_FL_NEEDS_REC,
516 	.parse			= eprobe_trigger_cmd_parse,
517 	.reg			= eprobe_trigger_reg_func,
518 	.unreg			= eprobe_trigger_unreg_func,
519 	.unreg_all		= NULL,
520 	.set_filter		= NULL,
521 	.trigger		= eprobe_trigger_func,
522 	.print			= eprobe_trigger_print,
523 	.init			= eprobe_trigger_init,
524 	.free			= eprobe_trigger_free,
525 };
526 
527 static struct event_trigger_data *
528 new_eprobe_trigger(struct trace_eprobe *ep, struct trace_event_file *file)
529 {
530 	struct event_trigger_data *trigger;
531 	struct event_filter *filter = NULL;
532 	struct eprobe_data *edata;
533 	int ret;
534 
535 	edata = kzalloc_obj(*edata);
536 	trigger = kzalloc_obj(*trigger);
537 	if (!trigger || !edata) {
538 		ret = -ENOMEM;
539 		goto error;
540 	}
541 
542 	trigger->flags = EVENT_TRIGGER_FL_PROBE;
543 	trigger->count = -1;
544 
545 	/*
546 	 * EVENT PROBE triggers are not registered as commands with
547 	 * register_event_command(), as they are not controlled by the user
548 	 * from the trigger file
549 	 */
550 	trigger->cmd_ops = &event_trigger_cmd;
551 
552 	INIT_LIST_HEAD(&trigger->list);
553 
554 	if (ep->filter_str) {
555 		ret = create_event_filter(file->tr, ep->event,
556 					ep->filter_str, false, &filter);
557 		if (ret)
558 			goto error;
559 	}
560 	RCU_INIT_POINTER(trigger->filter, filter);
561 
562 	edata->file = file;
563 	edata->ep = ep;
564 	trigger->private_data = edata;
565 
566 	return trigger;
567 error:
568 	free_event_filter(filter);
569 	kfree(edata);
570 	kfree(trigger);
571 	return ERR_PTR(ret);
572 }
573 
574 static int enable_eprobe(struct trace_eprobe *ep,
575 			 struct trace_event_file *eprobe_file)
576 {
577 	struct event_trigger_data *trigger;
578 	struct trace_event_file *file;
579 	struct trace_array *tr = eprobe_file->tr;
580 
581 	file = find_event_file(tr, ep->event_system, ep->event_name);
582 	if (!file)
583 		return -ENOENT;
584 	trigger = new_eprobe_trigger(ep, eprobe_file);
585 	if (IS_ERR(trigger))
586 		return PTR_ERR(trigger);
587 
588 	list_add_tail_rcu(&trigger->list, &file->triggers);
589 
590 	trace_event_trigger_enable_disable(file, 1);
591 	update_cond_flag(file);
592 
593 	return 0;
594 }
595 
596 static struct trace_event_functions eprobe_funcs = {
597 	.trace		= print_eprobe_event
598 };
599 
600 static int disable_eprobe(struct trace_eprobe *ep,
601 			  struct trace_array *tr)
602 {
603 	struct event_trigger_data *trigger = NULL, *iter;
604 	struct trace_event_file *file;
605 	struct event_filter *filter;
606 	struct eprobe_data *edata;
607 
608 	file = find_event_file(tr, ep->event_system, ep->event_name);
609 	if (!file)
610 		return -ENOENT;
611 
612 	list_for_each_entry(iter, &file->triggers, list) {
613 		if (!(iter->flags & EVENT_TRIGGER_FL_PROBE))
614 			continue;
615 		edata = iter->private_data;
616 		if (edata->ep == ep) {
617 			trigger = iter;
618 			break;
619 		}
620 	}
621 	if (!trigger)
622 		return -ENODEV;
623 
624 	list_del_rcu(&trigger->list);
625 
626 	trace_event_trigger_enable_disable(file, 0);
627 	update_cond_flag(file);
628 
629 	/* Make sure nothing is using the edata or trigger */
630 	tracepoint_synchronize_unregister();
631 
632 	filter = rcu_access_pointer(trigger->filter);
633 
634 	if (filter)
635 		free_event_filter(filter);
636 	kfree(edata);
637 	kfree(trigger);
638 
639 	return 0;
640 }
641 
642 static int enable_trace_eprobe(struct trace_event_call *call,
643 			       struct trace_event_file *file)
644 {
645 	struct trace_probe *tp;
646 	struct trace_eprobe *ep;
647 	int ret = 0;
648 	int cnt = 0;
649 
650 	if (WARN_ON_ONCE(!file))
651 		return -EINVAL;
652 
653 	tp = trace_probe_primary_from_call(call);
654 	if (WARN_ON_ONCE(!tp))
655 		return -ENODEV;
656 
657 	/* This also changes "enabled" state */
658 	ret = trace_probe_add_file(tp, file);
659 	if (ret)
660 		return ret;
661 
662 	/* The trigger lives in the instance of the file, add it for each file */
663 	for_each_trace_eprobe_tp(ep, tp) {
664 		ret = enable_eprobe(ep, file);
665 		if (ret)
666 			break;
667 		cnt++;
668 	}
669 
670 	if (ret) {
671 		/* Failed to enable one of them. Roll back all */
672 		if (cnt) {
673 			/*
674 			 * It's a bug if one failed for something other than memory
675 			 * not being available but another eprobe succeeded.
676 			 */
677 			WARN_ON_ONCE(ret != -ENOMEM);
678 
679 			for_each_trace_eprobe_tp(ep, tp) {
680 				disable_eprobe(ep, file->tr);
681 				if (!--cnt)
682 					break;
683 			}
684 		}
685 		trace_probe_remove_file(tp, file);
686 	}
687 
688 	return ret;
689 }
690 
691 static int disable_trace_eprobe(struct trace_event_call *call,
692 				struct trace_event_file *file)
693 {
694 	struct trace_probe *tp;
695 	struct trace_eprobe *ep;
696 
697 	if (WARN_ON_ONCE(!file))
698 		return -EINVAL;
699 
700 	tp = trace_probe_primary_from_call(call);
701 	if (WARN_ON_ONCE(!tp))
702 		return -ENODEV;
703 
704 	if (!trace_probe_get_file_link(tp, file))
705 		return -ENOENT;
706 
707 	for_each_trace_eprobe_tp(ep, tp)
708 		disable_eprobe(ep, file->tr);
709 
710 	/* Synchronization is done in below function. */
711 	trace_probe_remove_file(tp, file);
712 
713 	return 0;
714 }
715 
716 static int eprobe_register(struct trace_event_call *event,
717 			   enum trace_reg type, void *data)
718 {
719 	struct trace_event_file *file = data;
720 
721 	switch (type) {
722 	case TRACE_REG_REGISTER:
723 		return enable_trace_eprobe(event, file);
724 	case TRACE_REG_UNREGISTER:
725 		return disable_trace_eprobe(event, file);
726 #ifdef CONFIG_PERF_EVENTS
727 	case TRACE_REG_PERF_REGISTER:
728 	case TRACE_REG_PERF_UNREGISTER:
729 	case TRACE_REG_PERF_OPEN:
730 	case TRACE_REG_PERF_CLOSE:
731 	case TRACE_REG_PERF_ADD:
732 	case TRACE_REG_PERF_DEL:
733 		return 0;
734 #endif
735 	}
736 	return 0;
737 }
738 
739 static inline void init_trace_eprobe_call(struct trace_eprobe *ep)
740 {
741 	struct trace_event_call *call = trace_probe_event_call(&ep->tp);
742 
743 	call->flags = TRACE_EVENT_FL_EPROBE;
744 	call->event.funcs = &eprobe_funcs;
745 	call->class->fields_array = eprobe_fields_array;
746 	call->class->reg = eprobe_register;
747 }
748 
749 static struct trace_event_call *
750 find_and_get_event(const char *system, const char *event_name)
751 {
752 	struct trace_event_call *tp_event;
753 	const char *name;
754 
755 	list_for_each_entry(tp_event, &ftrace_events, list) {
756 		/* Skip other probes and ftrace events */
757 		if (tp_event->flags &
758 		    (TRACE_EVENT_FL_IGNORE_ENABLE |
759 		     TRACE_EVENT_FL_KPROBE |
760 		     TRACE_EVENT_FL_UPROBE |
761 		     TRACE_EVENT_FL_EPROBE))
762 			continue;
763 		if (!tp_event->class->system ||
764 		    strcmp(system, tp_event->class->system))
765 			continue;
766 		name = trace_event_name(tp_event);
767 		if (!name || strcmp(event_name, name))
768 			continue;
769 		if (!trace_event_try_get_ref(tp_event))
770 			return NULL;
771 		return tp_event;
772 	}
773 	return NULL;
774 }
775 
776 static int trace_eprobe_parse_filter(struct trace_eprobe *ep, int argc, const char *argv[])
777 {
778 	struct event_filter *dummy = NULL;
779 	int i, ret, len = 0;
780 	char *p;
781 
782 	if (argc == 0) {
783 		trace_probe_log_err(0, NO_EP_FILTER);
784 		return -EINVAL;
785 	}
786 
787 	/* Recover the filter string */
788 	for (i = 0; i < argc; i++)
789 		len += strlen(argv[i]) + 1;
790 
791 	ep->filter_str = kzalloc(len, GFP_KERNEL);
792 	if (!ep->filter_str)
793 		return -ENOMEM;
794 
795 	p = ep->filter_str;
796 	for (i = 0; i < argc; i++) {
797 		if (i)
798 			ret = snprintf(p, len, " %s", argv[i]);
799 		else
800 			ret = snprintf(p, len, "%s", argv[i]);
801 		p += ret;
802 		len -= ret;
803 	}
804 
805 	/*
806 	 * Ensure the filter string can be parsed correctly. Note, this
807 	 * filter string is for the original event, not for the eprobe.
808 	 */
809 	ret = create_event_filter(top_trace_array(), ep->event, ep->filter_str,
810 				  true, &dummy);
811 	free_event_filter(dummy);
812 	if (ret) {
813 		kfree(ep->filter_str);
814 		ep->filter_str = NULL;
815 	}
816 	return ret;
817 }
818 
819 static int __trace_eprobe_create(int argc, const char *argv[])
820 {
821 	/*
822 	 * Argument syntax:
823 	 *      e[:[GRP/][ENAME]] SYSTEM.EVENT [FETCHARGS] [if FILTER]
824 	 * Fetch args (no space):
825 	 *  <name>=$<field>[:TYPE]
826 	 */
827 	struct traceprobe_parse_context *ctx __free(traceprobe_parse_context) = NULL;
828 	struct trace_eprobe *ep __free(trace_event_probe_cleanup) = NULL;
829 	const char *trlog __free(trace_probe_log_clear) = NULL;
830 	const char *event = NULL, *group = EPROBE_EVENT_SYSTEM;
831 	const char *sys_event = NULL, *sys_name = NULL;
832 	struct trace_event_call *event_call;
833 	char *buf1 __free(kfree) = NULL;
834 	char *buf2 __free(kfree) = NULL;
835 	char *gbuf __free(kfree) = NULL;
836 	int ret = 0, filter_idx = 0;
837 	int i, filter_cnt;
838 
839 	if (argc < 2 || argv[0][0] != 'e')
840 		return -ECANCELED;
841 
842 	trlog = trace_probe_log_init("event_probe", argc, argv);
843 
844 	event = strchr(&argv[0][1], ':');
845 	if (event) {
846 		gbuf = kmalloc(MAX_EVENT_NAME_LEN, GFP_KERNEL);
847 		if (!gbuf)
848 			return -ENOMEM;
849 		event++;
850 		ret = traceprobe_parse_event_name(&event, &group, gbuf,
851 						  event - argv[0]);
852 		if (ret)
853 			return -EINVAL;
854 	}
855 
856 	trace_probe_log_set_index(1);
857 	sys_event = argv[1];
858 
859 	buf2 = kmalloc(MAX_EVENT_NAME_LEN, GFP_KERNEL);
860 	if (!buf2)
861 		return -ENOMEM;
862 
863 	ret = traceprobe_parse_event_name(&sys_event, &sys_name, buf2, 0);
864 	if (ret || !sys_event || !sys_name) {
865 		trace_probe_log_err(0, NO_EVENT_INFO);
866 		return -EINVAL;
867 	}
868 
869 	if (!event) {
870 		buf1 = kstrdup(sys_event, GFP_KERNEL);
871 		if (!buf1)
872 			return -ENOMEM;
873 		event = buf1;
874 	}
875 
876 	for (i = 2; i < argc; i++) {
877 		if (!strcmp(argv[i], "if")) {
878 			filter_idx = i + 1;
879 			filter_cnt = argc - filter_idx;
880 			argc = i;
881 			break;
882 		}
883 	}
884 
885 	if (argc - 2 > MAX_TRACE_ARGS) {
886 		trace_probe_log_set_index(2);
887 		trace_probe_log_err(0, TOO_MANY_ARGS);
888 		return -E2BIG;
889 	}
890 
891 	scoped_guard(mutex, &event_mutex) {
892 		event_call = find_and_get_event(sys_name, sys_event);
893 		ep = alloc_event_probe(group, event, event_call, argc - 2);
894 	}
895 
896 	if (IS_ERR(ep)) {
897 		ret = PTR_ERR(ep);
898 		if (ret == -ENODEV)
899 			trace_probe_log_err(0, BAD_ATTACH_EVENT);
900 		/* This must return -ENOMEM or missing event, else there is a bug */
901 		WARN_ON_ONCE(ret != -ENOMEM && ret != -ENODEV);
902 		return ret;
903 	}
904 
905 	if (filter_idx) {
906 		trace_probe_log_set_index(filter_idx);
907 		ret = trace_eprobe_parse_filter(ep, filter_cnt, argv + filter_idx);
908 		if (ret)
909 			return -EINVAL;
910 	} else
911 		ep->filter_str = NULL;
912 
913 	ctx = kzalloc_obj(*ctx);
914 	if (!ctx)
915 		return -ENOMEM;
916 	ctx->event = ep->event;
917 	ctx->flags = TPARG_FL_KERNEL | TPARG_FL_TEVENT;
918 
919 	argc -= 2; argv += 2;
920 	/* parse arguments */
921 	for (i = 0; i < argc; i++) {
922 		trace_probe_log_set_index(i + 2);
923 
924 		ret = traceprobe_parse_probe_arg(&ep->tp, i, argv[i], ctx);
925 		/* Handle symbols "@" */
926 		if (!ret)
927 			ret = traceprobe_update_arg(&ep->tp.args[i]);
928 		if (ret)
929 			return ret;
930 	}
931 	ret = traceprobe_set_print_fmt(&ep->tp, PROBE_PRINT_EVENT);
932 	if (ret < 0)
933 		return ret;
934 
935 	init_trace_eprobe_call(ep);
936 	scoped_guard(mutex, &event_mutex) {
937 		ret = trace_probe_register_event_call(&ep->tp);
938 		if (ret) {
939 			if (ret == -EEXIST) {
940 				trace_probe_log_set_index(0);
941 				trace_probe_log_err(0, EVENT_EXIST);
942 			}
943 			return ret;
944 		}
945 		ret = dyn_event_add(&ep->devent, &ep->tp.event->call);
946 		if (ret < 0) {
947 			trace_probe_unregister_event_call(&ep->tp);
948 			return ret;
949 		}
950 		/* To avoid freeing registered eprobe event, clear ep. */
951 		ep = NULL;
952 	}
953 	return ret;
954 }
955 
956 /*
957  * Register dynevent at core_initcall. This allows kernel to setup eprobe
958  * events in postcore_initcall without tracefs.
959  */
960 static __init int trace_events_eprobe_init_early(void)
961 {
962 	int err = 0;
963 
964 	err = dyn_event_register(&eprobe_dyn_event_ops);
965 	if (err)
966 		pr_warn("Could not register eprobe_dyn_event_ops\n");
967 
968 	return err;
969 }
970 core_initcall(trace_events_eprobe_init_early);
971