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