xref: /linux/kernel/trace/trace_eprobe.c (revision 0526b56cbc3c489642bd6a5fe4b718dea7ef0ee8)
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 trace_eprobe_tp_arg_update(struct trace_eprobe *ep, int i)
231 {
232 	struct probe_arg *parg = &ep->tp.args[i];
233 	struct ftrace_event_field *field;
234 	struct list_head *head;
235 	int ret = -ENOENT;
236 
237 	head = trace_get_fields(ep->event);
238 	list_for_each_entry(field, head, link) {
239 		if (!strcmp(parg->code->data, field->name)) {
240 			kfree(parg->code->data);
241 			parg->code->data = field;
242 			return 0;
243 		}
244 	}
245 
246 	/*
247 	 * Argument not found on event. But allow for comm and COMM
248 	 * to be used to get the current->comm.
249 	 */
250 	if (strcmp(parg->code->data, "COMM") == 0 ||
251 	    strcmp(parg->code->data, "comm") == 0) {
252 		parg->code->op = FETCH_OP_COMM;
253 		ret = 0;
254 	}
255 
256 	kfree(parg->code->data);
257 	parg->code->data = NULL;
258 	return ret;
259 }
260 
261 static int eprobe_event_define_fields(struct trace_event_call *event_call)
262 {
263 	struct eprobe_trace_entry_head field;
264 	struct trace_probe *tp;
265 
266 	tp = trace_probe_primary_from_call(event_call);
267 	if (WARN_ON_ONCE(!tp))
268 		return -ENOENT;
269 
270 	return traceprobe_define_arg_fields(event_call, sizeof(field), tp);
271 }
272 
273 static struct trace_event_fields eprobe_fields_array[] = {
274 	{ .type = TRACE_FUNCTION_TYPE,
275 	  .define_fields = eprobe_event_define_fields },
276 	{}
277 };
278 
279 /* Event entry printers */
280 static enum print_line_t
281 print_eprobe_event(struct trace_iterator *iter, int flags,
282 		   struct trace_event *event)
283 {
284 	struct eprobe_trace_entry_head *field;
285 	struct trace_event_call *pevent;
286 	struct trace_event *probed_event;
287 	struct trace_seq *s = &iter->seq;
288 	struct trace_eprobe *ep;
289 	struct trace_probe *tp;
290 	unsigned int type;
291 
292 	field = (struct eprobe_trace_entry_head *)iter->ent;
293 	tp = trace_probe_primary_from_call(
294 		container_of(event, struct trace_event_call, event));
295 	if (WARN_ON_ONCE(!tp))
296 		goto out;
297 
298 	ep = container_of(tp, struct trace_eprobe, tp);
299 	type = ep->event->event.type;
300 
301 	trace_seq_printf(s, "%s: (", trace_probe_name(tp));
302 
303 	probed_event = ftrace_find_event(type);
304 	if (probed_event) {
305 		pevent = container_of(probed_event, struct trace_event_call, event);
306 		trace_seq_printf(s, "%s.%s", pevent->class->system,
307 				 trace_event_name(pevent));
308 	} else {
309 		trace_seq_printf(s, "%u", type);
310 	}
311 
312 	trace_seq_putc(s, ')');
313 
314 	if (trace_probe_print_args(s, tp->args, tp->nr_args,
315 			     (u8 *)&field[1], field) < 0)
316 		goto out;
317 
318 	trace_seq_putc(s, '\n');
319  out:
320 	return trace_handle_return(s);
321 }
322 
323 static nokprobe_inline unsigned long
324 get_event_field(struct fetch_insn *code, void *rec)
325 {
326 	struct ftrace_event_field *field = code->data;
327 	unsigned long val;
328 	void *addr;
329 
330 	addr = rec + field->offset;
331 
332 	if (is_string_field(field)) {
333 		switch (field->filter_type) {
334 		case FILTER_DYN_STRING:
335 			val = (unsigned long)(rec + (*(unsigned int *)addr & 0xffff));
336 			break;
337 		case FILTER_RDYN_STRING:
338 			val = (unsigned long)(addr + (*(unsigned int *)addr & 0xffff));
339 			break;
340 		case FILTER_STATIC_STRING:
341 			val = (unsigned long)addr;
342 			break;
343 		case FILTER_PTR_STRING:
344 			val = (unsigned long)(*(char *)addr);
345 			break;
346 		default:
347 			WARN_ON_ONCE(1);
348 			return 0;
349 		}
350 		return val;
351 	}
352 
353 	switch (field->size) {
354 	case 1:
355 		if (field->is_signed)
356 			val = *(char *)addr;
357 		else
358 			val = *(unsigned char *)addr;
359 		break;
360 	case 2:
361 		if (field->is_signed)
362 			val = *(short *)addr;
363 		else
364 			val = *(unsigned short *)addr;
365 		break;
366 	case 4:
367 		if (field->is_signed)
368 			val = *(int *)addr;
369 		else
370 			val = *(unsigned int *)addr;
371 		break;
372 	default:
373 		if (field->is_signed)
374 			val = *(long *)addr;
375 		else
376 			val = *(unsigned long *)addr;
377 		break;
378 	}
379 	return val;
380 }
381 
382 static int get_eprobe_size(struct trace_probe *tp, void *rec)
383 {
384 	struct fetch_insn *code;
385 	struct probe_arg *arg;
386 	int i, len, ret = 0;
387 
388 	for (i = 0; i < tp->nr_args; i++) {
389 		arg = tp->args + i;
390 		if (arg->dynamic) {
391 			unsigned long val;
392 
393 			code = arg->code;
394  retry:
395 			switch (code->op) {
396 			case FETCH_OP_TP_ARG:
397 				val = get_event_field(code, rec);
398 				break;
399 			case FETCH_NOP_SYMBOL:	/* Ignore a place holder */
400 				code++;
401 				goto retry;
402 			default:
403 				if (process_common_fetch_insn(code, &val) < 0)
404 					continue;
405 			}
406 			code++;
407 			len = process_fetch_insn_bottom(code, val, NULL, NULL);
408 			if (len > 0)
409 				ret += len;
410 		}
411 	}
412 
413 	return ret;
414 }
415 
416 /* Kprobe specific fetch functions */
417 
418 /* Note that we don't verify it, since the code does not come from user space */
419 static int
420 process_fetch_insn(struct fetch_insn *code, void *rec, void *dest,
421 		   void *base)
422 {
423 	unsigned long val;
424 	int ret;
425 
426  retry:
427 	switch (code->op) {
428 	case FETCH_OP_TP_ARG:
429 		val = get_event_field(code, rec);
430 		break;
431 	case FETCH_NOP_SYMBOL:	/* Ignore a place holder */
432 		code++;
433 		goto retry;
434 	default:
435 		ret = process_common_fetch_insn(code, &val);
436 		if (ret < 0)
437 			return ret;
438 	}
439 	code++;
440 	return process_fetch_insn_bottom(code, val, dest, base);
441 }
442 NOKPROBE_SYMBOL(process_fetch_insn)
443 
444 /* eprobe handler */
445 static inline void
446 __eprobe_trace_func(struct eprobe_data *edata, void *rec)
447 {
448 	struct eprobe_trace_entry_head *entry;
449 	struct trace_event_call *call = trace_probe_event_call(&edata->ep->tp);
450 	struct trace_event_buffer fbuffer;
451 	int dsize;
452 
453 	if (WARN_ON_ONCE(call != edata->file->event_call))
454 		return;
455 
456 	if (trace_trigger_soft_disabled(edata->file))
457 		return;
458 
459 	dsize = get_eprobe_size(&edata->ep->tp, rec);
460 
461 	entry = trace_event_buffer_reserve(&fbuffer, edata->file,
462 					   sizeof(*entry) + edata->ep->tp.size + dsize);
463 
464 	if (!entry)
465 		return;
466 
467 	entry = fbuffer.entry = ring_buffer_event_data(fbuffer.event);
468 	store_trace_args(&entry[1], &edata->ep->tp, rec, sizeof(*entry), dsize);
469 
470 	trace_event_buffer_commit(&fbuffer);
471 }
472 
473 /*
474  * The event probe implementation uses event triggers to get access to
475  * the event it is attached to, but is not an actual trigger. The below
476  * functions are just stubs to fulfill what is needed to use the trigger
477  * infrastructure.
478  */
479 static int eprobe_trigger_init(struct event_trigger_data *data)
480 {
481 	return 0;
482 }
483 
484 static void eprobe_trigger_free(struct event_trigger_data *data)
485 {
486 
487 }
488 
489 static int eprobe_trigger_print(struct seq_file *m,
490 				struct event_trigger_data *data)
491 {
492 	/* Do not print eprobe event triggers */
493 	return 0;
494 }
495 
496 static void eprobe_trigger_func(struct event_trigger_data *data,
497 				struct trace_buffer *buffer, void *rec,
498 				struct ring_buffer_event *rbe)
499 {
500 	struct eprobe_data *edata = data->private_data;
501 
502 	if (unlikely(!rec))
503 		return;
504 
505 	__eprobe_trace_func(edata, rec);
506 }
507 
508 static struct event_trigger_ops eprobe_trigger_ops = {
509 	.trigger		= eprobe_trigger_func,
510 	.print			= eprobe_trigger_print,
511 	.init			= eprobe_trigger_init,
512 	.free			= eprobe_trigger_free,
513 };
514 
515 static int eprobe_trigger_cmd_parse(struct event_command *cmd_ops,
516 				    struct trace_event_file *file,
517 				    char *glob, char *cmd,
518 				    char *param_and_filter)
519 {
520 	return -1;
521 }
522 
523 static int eprobe_trigger_reg_func(char *glob,
524 				   struct event_trigger_data *data,
525 				   struct trace_event_file *file)
526 {
527 	return -1;
528 }
529 
530 static void eprobe_trigger_unreg_func(char *glob,
531 				      struct event_trigger_data *data,
532 				      struct trace_event_file *file)
533 {
534 
535 }
536 
537 static struct event_trigger_ops *eprobe_trigger_get_ops(char *cmd,
538 							char *param)
539 {
540 	return &eprobe_trigger_ops;
541 }
542 
543 static struct event_command event_trigger_cmd = {
544 	.name			= "eprobe",
545 	.trigger_type		= ETT_EVENT_EPROBE,
546 	.flags			= EVENT_CMD_FL_NEEDS_REC,
547 	.parse			= eprobe_trigger_cmd_parse,
548 	.reg			= eprobe_trigger_reg_func,
549 	.unreg			= eprobe_trigger_unreg_func,
550 	.unreg_all		= NULL,
551 	.get_trigger_ops	= eprobe_trigger_get_ops,
552 	.set_filter		= NULL,
553 };
554 
555 static struct event_trigger_data *
556 new_eprobe_trigger(struct trace_eprobe *ep, struct trace_event_file *file)
557 {
558 	struct event_trigger_data *trigger;
559 	struct event_filter *filter = NULL;
560 	struct eprobe_data *edata;
561 	int ret;
562 
563 	edata = kzalloc(sizeof(*edata), GFP_KERNEL);
564 	trigger = kzalloc(sizeof(*trigger), GFP_KERNEL);
565 	if (!trigger || !edata) {
566 		ret = -ENOMEM;
567 		goto error;
568 	}
569 
570 	trigger->flags = EVENT_TRIGGER_FL_PROBE;
571 	trigger->count = -1;
572 	trigger->ops = &eprobe_trigger_ops;
573 
574 	/*
575 	 * EVENT PROBE triggers are not registered as commands with
576 	 * register_event_command(), as they are not controlled by the user
577 	 * from the trigger file
578 	 */
579 	trigger->cmd_ops = &event_trigger_cmd;
580 
581 	INIT_LIST_HEAD(&trigger->list);
582 
583 	if (ep->filter_str) {
584 		ret = create_event_filter(file->tr, ep->event,
585 					ep->filter_str, false, &filter);
586 		if (ret)
587 			goto error;
588 	}
589 	RCU_INIT_POINTER(trigger->filter, filter);
590 
591 	edata->file = file;
592 	edata->ep = ep;
593 	trigger->private_data = edata;
594 
595 	return trigger;
596 error:
597 	free_event_filter(filter);
598 	kfree(edata);
599 	kfree(trigger);
600 	return ERR_PTR(ret);
601 }
602 
603 static int enable_eprobe(struct trace_eprobe *ep,
604 			 struct trace_event_file *eprobe_file)
605 {
606 	struct event_trigger_data *trigger;
607 	struct trace_event_file *file;
608 	struct trace_array *tr = eprobe_file->tr;
609 
610 	file = find_event_file(tr, ep->event_system, ep->event_name);
611 	if (!file)
612 		return -ENOENT;
613 	trigger = new_eprobe_trigger(ep, eprobe_file);
614 	if (IS_ERR(trigger))
615 		return PTR_ERR(trigger);
616 
617 	list_add_tail_rcu(&trigger->list, &file->triggers);
618 
619 	trace_event_trigger_enable_disable(file, 1);
620 	update_cond_flag(file);
621 
622 	return 0;
623 }
624 
625 static struct trace_event_functions eprobe_funcs = {
626 	.trace		= print_eprobe_event
627 };
628 
629 static int disable_eprobe(struct trace_eprobe *ep,
630 			  struct trace_array *tr)
631 {
632 	struct event_trigger_data *trigger = NULL, *iter;
633 	struct trace_event_file *file;
634 	struct event_filter *filter;
635 	struct eprobe_data *edata;
636 
637 	file = find_event_file(tr, ep->event_system, ep->event_name);
638 	if (!file)
639 		return -ENOENT;
640 
641 	list_for_each_entry(iter, &file->triggers, list) {
642 		if (!(iter->flags & EVENT_TRIGGER_FL_PROBE))
643 			continue;
644 		edata = iter->private_data;
645 		if (edata->ep == ep) {
646 			trigger = iter;
647 			break;
648 		}
649 	}
650 	if (!trigger)
651 		return -ENODEV;
652 
653 	list_del_rcu(&trigger->list);
654 
655 	trace_event_trigger_enable_disable(file, 0);
656 	update_cond_flag(file);
657 
658 	/* Make sure nothing is using the edata or trigger */
659 	tracepoint_synchronize_unregister();
660 
661 	filter = rcu_access_pointer(trigger->filter);
662 
663 	if (filter)
664 		free_event_filter(filter);
665 	kfree(edata);
666 	kfree(trigger);
667 
668 	return 0;
669 }
670 
671 static int enable_trace_eprobe(struct trace_event_call *call,
672 			       struct trace_event_file *file)
673 {
674 	struct trace_probe *pos, *tp;
675 	struct trace_eprobe *ep;
676 	bool enabled;
677 	int ret = 0;
678 
679 	tp = trace_probe_primary_from_call(call);
680 	if (WARN_ON_ONCE(!tp))
681 		return -ENODEV;
682 	enabled = trace_probe_is_enabled(tp);
683 
684 	/* This also changes "enabled" state */
685 	if (file) {
686 		ret = trace_probe_add_file(tp, file);
687 		if (ret)
688 			return ret;
689 	} else
690 		trace_probe_set_flag(tp, TP_FLAG_PROFILE);
691 
692 	if (enabled)
693 		return 0;
694 
695 	list_for_each_entry(pos, trace_probe_probe_list(tp), list) {
696 		ep = container_of(pos, struct trace_eprobe, tp);
697 		ret = enable_eprobe(ep, file);
698 		if (ret)
699 			break;
700 		enabled = true;
701 	}
702 
703 	if (ret) {
704 		/* Failed to enable one of them. Roll back all */
705 		if (enabled)
706 			disable_eprobe(ep, file->tr);
707 		if (file)
708 			trace_probe_remove_file(tp, file);
709 		else
710 			trace_probe_clear_flag(tp, TP_FLAG_PROFILE);
711 	}
712 
713 	return ret;
714 }
715 
716 static int disable_trace_eprobe(struct trace_event_call *call,
717 				struct trace_event_file *file)
718 {
719 	struct trace_probe *pos, *tp;
720 	struct trace_eprobe *ep;
721 
722 	tp = trace_probe_primary_from_call(call);
723 	if (WARN_ON_ONCE(!tp))
724 		return -ENODEV;
725 
726 	if (file) {
727 		if (!trace_probe_get_file_link(tp, file))
728 			return -ENOENT;
729 		if (!trace_probe_has_single_file(tp))
730 			goto out;
731 		trace_probe_clear_flag(tp, TP_FLAG_TRACE);
732 	} else
733 		trace_probe_clear_flag(tp, TP_FLAG_PROFILE);
734 
735 	if (!trace_probe_is_enabled(tp)) {
736 		list_for_each_entry(pos, trace_probe_probe_list(tp), list) {
737 			ep = container_of(pos, struct trace_eprobe, tp);
738 			disable_eprobe(ep, file->tr);
739 		}
740 	}
741 
742  out:
743 	if (file)
744 		/*
745 		 * Synchronization is done in below function. For perf event,
746 		 * file == NULL and perf_trace_event_unreg() calls
747 		 * tracepoint_synchronize_unregister() to ensure synchronize
748 		 * event. We don't need to care about it.
749 		 */
750 		trace_probe_remove_file(tp, file);
751 
752 	return 0;
753 }
754 
755 static int eprobe_register(struct trace_event_call *event,
756 			   enum trace_reg type, void *data)
757 {
758 	struct trace_event_file *file = data;
759 
760 	switch (type) {
761 	case TRACE_REG_REGISTER:
762 		return enable_trace_eprobe(event, file);
763 	case TRACE_REG_UNREGISTER:
764 		return disable_trace_eprobe(event, file);
765 #ifdef CONFIG_PERF_EVENTS
766 	case TRACE_REG_PERF_REGISTER:
767 	case TRACE_REG_PERF_UNREGISTER:
768 	case TRACE_REG_PERF_OPEN:
769 	case TRACE_REG_PERF_CLOSE:
770 	case TRACE_REG_PERF_ADD:
771 	case TRACE_REG_PERF_DEL:
772 		return 0;
773 #endif
774 	}
775 	return 0;
776 }
777 
778 static inline void init_trace_eprobe_call(struct trace_eprobe *ep)
779 {
780 	struct trace_event_call *call = trace_probe_event_call(&ep->tp);
781 
782 	call->flags = TRACE_EVENT_FL_EPROBE;
783 	call->event.funcs = &eprobe_funcs;
784 	call->class->fields_array = eprobe_fields_array;
785 	call->class->reg = eprobe_register;
786 }
787 
788 static struct trace_event_call *
789 find_and_get_event(const char *system, const char *event_name)
790 {
791 	struct trace_event_call *tp_event;
792 	const char *name;
793 
794 	list_for_each_entry(tp_event, &ftrace_events, list) {
795 		/* Skip other probes and ftrace events */
796 		if (tp_event->flags &
797 		    (TRACE_EVENT_FL_IGNORE_ENABLE |
798 		     TRACE_EVENT_FL_KPROBE |
799 		     TRACE_EVENT_FL_UPROBE |
800 		     TRACE_EVENT_FL_EPROBE))
801 			continue;
802 		if (!tp_event->class->system ||
803 		    strcmp(system, tp_event->class->system))
804 			continue;
805 		name = trace_event_name(tp_event);
806 		if (!name || strcmp(event_name, name))
807 			continue;
808 		if (!trace_event_try_get_ref(tp_event)) {
809 			return NULL;
810 			break;
811 		}
812 		return tp_event;
813 		break;
814 	}
815 	return NULL;
816 }
817 
818 static int trace_eprobe_tp_update_arg(struct trace_eprobe *ep, const char *argv[], int i)
819 {
820 	unsigned int flags = TPARG_FL_KERNEL | TPARG_FL_TPOINT;
821 	int ret;
822 
823 	ret = traceprobe_parse_probe_arg(&ep->tp, i, argv[i], flags);
824 	if (ret)
825 		return ret;
826 
827 	if (ep->tp.args[i].code->op == FETCH_OP_TP_ARG) {
828 		ret = trace_eprobe_tp_arg_update(ep, i);
829 		if (ret)
830 			trace_probe_log_err(0, BAD_ATTACH_ARG);
831 	}
832 
833 	/* Handle symbols "@" */
834 	if (!ret)
835 		ret = traceprobe_update_arg(&ep->tp.args[i]);
836 
837 	return ret;
838 }
839 
840 static int trace_eprobe_parse_filter(struct trace_eprobe *ep, int argc, const char *argv[])
841 {
842 	struct event_filter *dummy = NULL;
843 	int i, ret, len = 0;
844 	char *p;
845 
846 	if (argc == 0) {
847 		trace_probe_log_err(0, NO_EP_FILTER);
848 		return -EINVAL;
849 	}
850 
851 	/* Recover the filter string */
852 	for (i = 0; i < argc; i++)
853 		len += strlen(argv[i]) + 1;
854 
855 	ep->filter_str = kzalloc(len, GFP_KERNEL);
856 	if (!ep->filter_str)
857 		return -ENOMEM;
858 
859 	p = ep->filter_str;
860 	for (i = 0; i < argc; i++) {
861 		if (i)
862 			ret = snprintf(p, len, " %s", argv[i]);
863 		else
864 			ret = snprintf(p, len, "%s", argv[i]);
865 		p += ret;
866 		len -= ret;
867 	}
868 
869 	/*
870 	 * Ensure the filter string can be parsed correctly. Note, this
871 	 * filter string is for the original event, not for the eprobe.
872 	 */
873 	ret = create_event_filter(top_trace_array(), ep->event, ep->filter_str,
874 				  true, &dummy);
875 	free_event_filter(dummy);
876 	if (ret)
877 		goto error;
878 
879 	return 0;
880 error:
881 	kfree(ep->filter_str);
882 	ep->filter_str = NULL;
883 	return ret;
884 }
885 
886 static int __trace_eprobe_create(int argc, const char *argv[])
887 {
888 	/*
889 	 * Argument syntax:
890 	 *      e[:[GRP/][ENAME]] SYSTEM.EVENT [FETCHARGS] [if FILTER]
891 	 * Fetch args (no space):
892 	 *  <name>=$<field>[:TYPE]
893 	 */
894 	const char *event = NULL, *group = EPROBE_EVENT_SYSTEM;
895 	const char *sys_event = NULL, *sys_name = NULL;
896 	struct trace_event_call *event_call;
897 	struct trace_eprobe *ep = NULL;
898 	char buf1[MAX_EVENT_NAME_LEN];
899 	char buf2[MAX_EVENT_NAME_LEN];
900 	char gbuf[MAX_EVENT_NAME_LEN];
901 	int ret = 0, filter_idx = 0;
902 	int i, filter_cnt;
903 
904 	if (argc < 2 || argv[0][0] != 'e')
905 		return -ECANCELED;
906 
907 	trace_probe_log_init("event_probe", argc, argv);
908 
909 	event = strchr(&argv[0][1], ':');
910 	if (event) {
911 		event++;
912 		ret = traceprobe_parse_event_name(&event, &group, gbuf,
913 						  event - argv[0]);
914 		if (ret)
915 			goto parse_error;
916 	}
917 
918 	trace_probe_log_set_index(1);
919 	sys_event = argv[1];
920 	ret = traceprobe_parse_event_name(&sys_event, &sys_name, buf2, 0);
921 	if (ret || !sys_event || !sys_name) {
922 		trace_probe_log_err(0, NO_EVENT_INFO);
923 		goto parse_error;
924 	}
925 
926 	if (!event) {
927 		strscpy(buf1, sys_event, MAX_EVENT_NAME_LEN);
928 		event = buf1;
929 	}
930 
931 	for (i = 2; i < argc; i++) {
932 		if (!strcmp(argv[i], "if")) {
933 			filter_idx = i + 1;
934 			filter_cnt = argc - filter_idx;
935 			argc = i;
936 			break;
937 		}
938 	}
939 
940 	mutex_lock(&event_mutex);
941 	event_call = find_and_get_event(sys_name, sys_event);
942 	ep = alloc_event_probe(group, event, event_call, argc - 2);
943 	mutex_unlock(&event_mutex);
944 
945 	if (IS_ERR(ep)) {
946 		ret = PTR_ERR(ep);
947 		if (ret == -ENODEV)
948 			trace_probe_log_err(0, BAD_ATTACH_EVENT);
949 		/* This must return -ENOMEM or missing event, else there is a bug */
950 		WARN_ON_ONCE(ret != -ENOMEM && ret != -ENODEV);
951 		ep = NULL;
952 		goto error;
953 	}
954 
955 	if (filter_idx) {
956 		trace_probe_log_set_index(filter_idx);
957 		ret = trace_eprobe_parse_filter(ep, filter_cnt, argv + filter_idx);
958 		if (ret)
959 			goto parse_error;
960 	} else
961 		ep->filter_str = NULL;
962 
963 	argc -= 2; argv += 2;
964 	/* parse arguments */
965 	for (i = 0; i < argc && i < MAX_TRACE_ARGS; i++) {
966 		trace_probe_log_set_index(i + 2);
967 		ret = trace_eprobe_tp_update_arg(ep, argv, i);
968 		if (ret)
969 			goto error;
970 	}
971 	ret = traceprobe_set_print_fmt(&ep->tp, PROBE_PRINT_EVENT);
972 	if (ret < 0)
973 		goto error;
974 	init_trace_eprobe_call(ep);
975 	mutex_lock(&event_mutex);
976 	ret = trace_probe_register_event_call(&ep->tp);
977 	if (ret) {
978 		if (ret == -EEXIST) {
979 			trace_probe_log_set_index(0);
980 			trace_probe_log_err(0, EVENT_EXIST);
981 		}
982 		mutex_unlock(&event_mutex);
983 		goto error;
984 	}
985 	ret = dyn_event_add(&ep->devent, &ep->tp.event->call);
986 	mutex_unlock(&event_mutex);
987 	return ret;
988 parse_error:
989 	ret = -EINVAL;
990 error:
991 	trace_event_probe_cleanup(ep);
992 	return ret;
993 }
994 
995 /*
996  * Register dynevent at core_initcall. This allows kernel to setup eprobe
997  * events in postcore_initcall without tracefs.
998  */
999 static __init int trace_events_eprobe_init_early(void)
1000 {
1001 	int err = 0;
1002 
1003 	err = dyn_event_register(&eprobe_dyn_event_ops);
1004 	if (err)
1005 		pr_warn("Could not register eprobe_dyn_event_ops\n");
1006 
1007 	return err;
1008 }
1009 core_initcall(trace_events_eprobe_init_early);
1010