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