xref: /linux/kernel/trace/trace_probe.c (revision 7d8d6ad659c02ed5d2387777194c22e8e81dbb2b)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Common code for probe-based Dynamic events.
4  *
5  * This code was copied from kernel/trace/trace_kprobe.c written by
6  * Masami Hiramatsu <masami.hiramatsu.pt@hitachi.com>
7  *
8  * Updates to make this generic:
9  * Copyright (C) IBM Corporation, 2010-2011
10  * Author:     Srikar Dronamraju
11  */
12 #define pr_fmt(fmt)	"trace_probe: " fmt
13 
14 #include <linux/bpf.h>
15 #include <linux/fs.h>
16 
17 #include "trace_btf.h"
18 #include "trace_probe.h"
19 
20 #undef C
21 #define C(a, b)		b
22 
23 static const char *trace_probe_err_text[] = { ERRORS };
24 
25 static const char *reserved_field_names[] = {
26 	"common_type",
27 	"common_flags",
28 	"common_preempt_count",
29 	"common_pid",
30 	"common_tgid",
31 	FIELD_STRING_IP,
32 	FIELD_STRING_RETIP,
33 	FIELD_STRING_FUNC,
34 };
35 
36 /* Printing  in basic type function template */
37 #define DEFINE_BASIC_PRINT_TYPE_FUNC(tname, type, fmt)			\
38 int PRINT_TYPE_FUNC_NAME(tname)(struct trace_seq *s, void *data, void *ent)\
39 {									\
40 	trace_seq_printf(s, fmt, *(type *)data);			\
41 	return !trace_seq_has_overflowed(s);				\
42 }									\
43 const char PRINT_TYPE_FMT_NAME(tname)[] = fmt;
44 
45 DEFINE_BASIC_PRINT_TYPE_FUNC(u8,  u8,  "%u")
46 DEFINE_BASIC_PRINT_TYPE_FUNC(u16, u16, "%u")
47 DEFINE_BASIC_PRINT_TYPE_FUNC(u32, u32, "%u")
48 DEFINE_BASIC_PRINT_TYPE_FUNC(u64, u64, "%Lu")
49 DEFINE_BASIC_PRINT_TYPE_FUNC(s8,  s8,  "%d")
50 DEFINE_BASIC_PRINT_TYPE_FUNC(s16, s16, "%d")
51 DEFINE_BASIC_PRINT_TYPE_FUNC(s32, s32, "%d")
52 DEFINE_BASIC_PRINT_TYPE_FUNC(s64, s64, "%Ld")
53 DEFINE_BASIC_PRINT_TYPE_FUNC(x8,  u8,  "0x%x")
54 DEFINE_BASIC_PRINT_TYPE_FUNC(x16, u16, "0x%x")
55 DEFINE_BASIC_PRINT_TYPE_FUNC(x32, u32, "0x%x")
56 DEFINE_BASIC_PRINT_TYPE_FUNC(x64, u64, "0x%Lx")
57 DEFINE_BASIC_PRINT_TYPE_FUNC(char, u8, "'%c'")
58 
59 int PRINT_TYPE_FUNC_NAME(symbol)(struct trace_seq *s, void *data, void *ent)
60 {
61 	trace_seq_printf(s, "%pS", (void *)*(unsigned long *)data);
62 	return !trace_seq_has_overflowed(s);
63 }
64 const char PRINT_TYPE_FMT_NAME(symbol)[] = "%pS";
65 
66 /* Print type function for string type */
67 int PRINT_TYPE_FUNC_NAME(string)(struct trace_seq *s, void *data, void *ent)
68 {
69 	int len = *(u32 *)data >> 16;
70 
71 	if (!len)
72 		trace_seq_puts(s, FAULT_STRING);
73 	else
74 		trace_seq_printf(s, "\"%s\"",
75 				 (const char *)get_loc_data(data, ent));
76 	return !trace_seq_has_overflowed(s);
77 }
78 
79 const char PRINT_TYPE_FMT_NAME(string)[] = "\\\"%s\\\"";
80 
81 /* Fetch type information table */
82 static const struct fetch_type probe_fetch_types[] = {
83 	/* Special types */
84 	__ASSIGN_FETCH_TYPE("string", string, string, sizeof(u32), 1, 1,
85 			    "__data_loc char[]"),
86 	__ASSIGN_FETCH_TYPE("ustring", string, string, sizeof(u32), 1, 1,
87 			    "__data_loc char[]"),
88 	__ASSIGN_FETCH_TYPE("symstr", string, string, sizeof(u32), 1, 1,
89 			    "__data_loc char[]"),
90 	/* Basic types */
91 	ASSIGN_FETCH_TYPE(u8,  u8,  0),
92 	ASSIGN_FETCH_TYPE(u16, u16, 0),
93 	ASSIGN_FETCH_TYPE(u32, u32, 0),
94 	ASSIGN_FETCH_TYPE(u64, u64, 0),
95 	ASSIGN_FETCH_TYPE(s8,  u8,  1),
96 	ASSIGN_FETCH_TYPE(s16, u16, 1),
97 	ASSIGN_FETCH_TYPE(s32, u32, 1),
98 	ASSIGN_FETCH_TYPE(s64, u64, 1),
99 	ASSIGN_FETCH_TYPE_ALIAS(x8,  u8,  u8,  0),
100 	ASSIGN_FETCH_TYPE_ALIAS(x16, u16, u16, 0),
101 	ASSIGN_FETCH_TYPE_ALIAS(x32, u32, u32, 0),
102 	ASSIGN_FETCH_TYPE_ALIAS(x64, u64, u64, 0),
103 	ASSIGN_FETCH_TYPE_ALIAS(char, u8, u8,  0),
104 	ASSIGN_FETCH_TYPE_ALIAS(symbol, ADDR_FETCH_TYPE, ADDR_FETCH_TYPE, 0),
105 
106 	ASSIGN_FETCH_TYPE_END
107 };
108 
109 static const struct fetch_type *find_fetch_type(const char *type, unsigned long flags)
110 {
111 	int i;
112 
113 	/* Reject the symbol/symstr for uprobes */
114 	if (type && (flags & TPARG_FL_USER) &&
115 	    (!strcmp(type, "symbol") || !strcmp(type, "symstr")))
116 		return NULL;
117 
118 	if (!type)
119 		type = DEFAULT_FETCH_TYPE_STR;
120 
121 	/* Special case: bitfield */
122 	if (*type == 'b') {
123 		unsigned long bs;
124 
125 		type = strchr(type, '/');
126 		if (!type)
127 			goto fail;
128 
129 		type++;
130 		if (kstrtoul(type, 0, &bs))
131 			goto fail;
132 
133 		switch (bs) {
134 		case 8:
135 			return find_fetch_type("u8", flags);
136 		case 16:
137 			return find_fetch_type("u16", flags);
138 		case 32:
139 			return find_fetch_type("u32", flags);
140 		case 64:
141 			return find_fetch_type("u64", flags);
142 		default:
143 			goto fail;
144 		}
145 	}
146 
147 	for (i = 0; probe_fetch_types[i].name; i++) {
148 		if (strcmp(type, probe_fetch_types[i].name) == 0)
149 			return &probe_fetch_types[i];
150 	}
151 
152 fail:
153 	return NULL;
154 }
155 
156 static struct trace_probe_log trace_probe_log;
157 extern struct mutex dyn_event_ops_mutex;
158 
159 const char *trace_probe_log_init(const char *subsystem, int argc, const char **argv)
160 {
161 	lockdep_assert_held(&dyn_event_ops_mutex);
162 
163 	trace_probe_log.subsystem = subsystem;
164 	trace_probe_log.argc = argc;
165 	trace_probe_log.argv = argv;
166 	trace_probe_log.index = 0;
167 	return subsystem;
168 }
169 
170 void trace_probe_log_clear(void)
171 {
172 	lockdep_assert_held(&dyn_event_ops_mutex);
173 
174 	memset(&trace_probe_log, 0, sizeof(trace_probe_log));
175 }
176 
177 void trace_probe_log_set_index(int index)
178 {
179 	lockdep_assert_held(&dyn_event_ops_mutex);
180 
181 	trace_probe_log.index = index;
182 }
183 
184 void __trace_probe_log_err(int offset, int err_type)
185 {
186 	char *command, *p;
187 	int i, len = 0, pos = 0;
188 
189 	lockdep_assert_held(&dyn_event_ops_mutex);
190 
191 	if (!trace_probe_log.argv)
192 		return;
193 
194 	/* Recalculate the length and allocate buffer */
195 	for (i = 0; i < trace_probe_log.argc; i++) {
196 		if (i == trace_probe_log.index)
197 			pos = len;
198 		len += strlen(trace_probe_log.argv[i]) + 1;
199 	}
200 	command = kzalloc(len, GFP_KERNEL);
201 	if (!command)
202 		return;
203 
204 	if (trace_probe_log.index >= trace_probe_log.argc) {
205 		/**
206 		 * Set the error position is next to the last arg + space.
207 		 * Note that len includes the terminal null and the cursor
208 		 * appears at pos + 1.
209 		 */
210 		pos = len;
211 		offset = 0;
212 	}
213 
214 	/* And make a command string from argv array */
215 	p = command;
216 	for (i = 0; i < trace_probe_log.argc; i++) {
217 		len = strlen(trace_probe_log.argv[i]);
218 		memcpy(p, trace_probe_log.argv[i], len);
219 		p[len] = ' ';
220 		p += len + 1;
221 	}
222 	*(p - 1) = '\0';
223 
224 	tracing_log_err(NULL, trace_probe_log.subsystem, command,
225 			trace_probe_err_text, err_type, pos + offset);
226 
227 	kfree(command);
228 }
229 
230 /* Split symbol and offset. */
231 int traceprobe_split_symbol_offset(char *symbol, long *offset)
232 {
233 	char *tmp;
234 	int ret;
235 
236 	if (!offset)
237 		return -EINVAL;
238 
239 	tmp = strpbrk(symbol, "+-");
240 	if (tmp) {
241 		ret = kstrtol(tmp, 0, offset);
242 		if (ret)
243 			return ret;
244 		*tmp = '\0';
245 	} else
246 		*offset = 0;
247 
248 	return 0;
249 }
250 
251 /**
252  * traceprobe_parse_event_name() - Parse a string into group and event names
253  * @pevent: A pointer to the string to be parsed.
254  * @pgroup: A pointer to the group name.
255  * @buf:    A buffer to store the parsed group name.
256  * @offset: The offset of the string in the original user command, for logging.
257  *
258  * This parses a string with the format `[GROUP/][EVENT]` or `[GROUP.][EVENT]`
259  * (either GROUP or EVENT or both must be specified).
260  * Since the parsed group name is stored in @buf, the caller must ensure @buf
261  * is at least MAX_EVENT_NAME_LEN bytes.
262  *
263  * Return: 0 on success, or -EINVAL on failure.
264  *
265  * If success, *@pevent is updated to point to the event name part of the
266  * original string, or NULL if there is no event name.
267  * Also, *@pgroup is updated to point to the parsed group which is stored
268  * in @buf, or NULL if there is no group name.
269  */
270 int traceprobe_parse_event_name(const char **pevent, const char **pgroup,
271 				char *buf, int offset)
272 {
273 	const char *slash, *event = *pevent;
274 	int len;
275 
276 	slash = strchr(event, '/');
277 	if (!slash)
278 		slash = strchr(event, '.');
279 
280 	if (slash) {
281 		if (slash == event) {
282 			trace_probe_log_err(offset, NO_GROUP_NAME);
283 			return -EINVAL;
284 		}
285 		if (slash - event + 1 > MAX_EVENT_NAME_LEN) {
286 			trace_probe_log_err(offset, GROUP_TOO_LONG);
287 			return -EINVAL;
288 		}
289 		strscpy(buf, event, slash - event + 1);
290 		if (!is_good_system_name(buf)) {
291 			trace_probe_log_err(offset, BAD_GROUP_NAME);
292 			return -EINVAL;
293 		}
294 		*pgroup = buf;
295 		*pevent = slash + 1;
296 		offset += slash - event + 1;
297 		event = *pevent;
298 	}
299 	len = strlen(event);
300 	if (len == 0) {
301 		if (slash) {
302 			*pevent = NULL;
303 			return 0;
304 		}
305 		trace_probe_log_err(offset, NO_EVENT_NAME);
306 		return -EINVAL;
307 	} else if (len >= MAX_EVENT_NAME_LEN) {
308 		trace_probe_log_err(offset, EVENT_TOO_LONG);
309 		return -EINVAL;
310 	}
311 	if (!is_good_name(event)) {
312 		trace_probe_log_err(offset, BAD_EVENT_NAME);
313 		return -EINVAL;
314 	}
315 	return 0;
316 }
317 
318 static int parse_trace_event_arg(char *arg, struct fetch_insn *code,
319 				 struct traceprobe_parse_context *ctx)
320 {
321 	struct ftrace_event_field *field;
322 	struct list_head *head;
323 
324 	head = trace_get_fields(ctx->event);
325 	list_for_each_entry(field, head, link) {
326 		if (!strcmp(arg, field->name)) {
327 			code->op = FETCH_OP_TP_ARG;
328 			code->data = field;
329 			return 0;
330 		}
331 	}
332 	return -ENOENT;
333 }
334 
335 static int parse_trace_event(char *arg, struct fetch_insn *code,
336 			     struct traceprobe_parse_context *ctx)
337 {
338 	int ret;
339 
340 	if (code->data)
341 		return -EFAULT;
342 	ret = parse_trace_event_arg(arg, code, ctx);
343 	if (!ret)
344 		return 0;
345 	return -EINVAL;
346 }
347 
348 /* this_cpu_* parser */
349 #define THIS_CPU_PTR_PREFIX "this_cpu_ptr("
350 #define THIS_CPU_READ_PREFIX "this_cpu_read("
351 #define THIS_CPU_PTR_LEN (sizeof(THIS_CPU_PTR_PREFIX) - 1)
352 #define THIS_CPU_READ_LEN (sizeof(THIS_CPU_READ_PREFIX) - 1)
353 
354 static int
355 parse_probe_arg(char *arg, const struct fetch_type *type,
356 		struct fetch_insn **pcode, struct fetch_insn *end,
357 		struct traceprobe_parse_context *ctx);
358 
359 /* handle dereference nested call */
360 static inline int handle_dereference(char *arg, struct fetch_insn **pcode,
361 	struct fetch_insn *end, struct traceprobe_parse_context *ctx,
362 	int deref, long offset)
363 {
364 	const struct fetch_type *type = find_fetch_type(NULL, ctx->flags);
365 	struct fetch_insn *code = *pcode;
366 	int cur_offs = ctx->offset;
367 	char *tmp;
368 	int ret;
369 
370 	tmp = strrchr(arg, ')');
371 	if (!tmp) {
372 		trace_probe_log_err(ctx->offset + strlen(arg),
373 					DEREF_OPEN_BRACE);
374 		return -EINVAL;
375 	}
376 
377 	*tmp = '\0';
378 	ret = parse_probe_arg(arg, type, &code, end, ctx);
379 	if (ret)
380 		return ret;
381 	ctx->offset = cur_offs;
382 	if (code->op == FETCH_OP_COMM || code->op == FETCH_OP_IMMSTR) {
383 		trace_probe_log_err(ctx->offset, COMM_CANT_DEREF);
384 		return -EINVAL;
385 	}
386 
387 	/*
388 	 * this_cpu_ptr(@SYM) does not use SYM value, but use SYM address.
389 	 * So we overwrite the last FETCH_OP_DEREF with FETCH_OP_CPU_PTR.
390 	 */
391 	if (!(deref == FETCH_OP_CPU_PTR && *arg == '@')) {
392 		code++;
393 		if (code == end) {
394 			trace_probe_log_err(ctx->offset, TOO_MANY_OPS);
395 			return -EINVAL;
396 		}
397 	}
398 	*pcode = code;
399 
400 	code->op = deref;
401 	code->offset = offset;
402 	/* Reset the last type if used */
403 	ctx->last_type = NULL;
404 	return 0;
405 }
406 
407 static int parse_this_cpu(char *arg, struct fetch_insn **pcode,
408 			  struct fetch_insn *end,
409 			  struct traceprobe_parse_context *ctx)
410 {
411 	struct fetch_insn *code;
412 	bool is_ptr = false;
413 	int ret;
414 
415 	/*
416 	 * This is only for kernel probes, excluding eprobe, because per-cpu
417 	 * pointer should not be recorded by events.
418 	 */
419 	if (!(ctx->flags & TPARG_FL_KERNEL) ||
420 	    (ctx->flags & TPARG_FL_TEVENT)) {
421 		trace_probe_log_err(ctx->offset, NOSUP_PERCPU);
422 		return -EINVAL;
423 	}
424 	if (str_has_prefix(arg, THIS_CPU_PTR_PREFIX)) {
425 		arg += THIS_CPU_PTR_LEN;
426 		ctx->offset += THIS_CPU_PTR_LEN;
427 		is_ptr = true;
428 	} else if (str_has_prefix(arg, THIS_CPU_READ_PREFIX)) {
429 		arg += THIS_CPU_READ_LEN;
430 		ctx->offset += THIS_CPU_READ_LEN;
431 	} else
432 		return -EINVAL;
433 
434 	ret = handle_dereference(arg, pcode, end, ctx, FETCH_OP_CPU_PTR, 0);
435 	if (ret || is_ptr)
436 		return ret;
437 
438 	/* this_cpu_read(VAR) -> +0(this_cpu_ptr(VAR)) */
439 	code = *pcode;
440 	code++;
441 	if (code == end) {
442 		trace_probe_log_err(ctx->offset, TOO_MANY_OPS);
443 		return -EINVAL;
444 	}
445 	code->op = FETCH_OP_DEREF;
446 	code->offset = 0;
447 	*pcode = code;
448 	return 0;
449 }
450 
451 #ifdef CONFIG_PROBE_EVENTS_BTF_ARGS
452 
453 static u32 btf_type_int(const struct btf_type *t)
454 {
455 	return *(u32 *)(t + 1);
456 }
457 
458 static bool btf_type_is_char_ptr(struct btf *btf, const struct btf_type *type)
459 {
460 	const struct btf_type *real_type;
461 	u32 intdata;
462 
463 	real_type = btf_type_skip_modifiers(btf, type->type, NULL);
464 	if (!real_type)
465 		return false;
466 
467 	if (BTF_INFO_KIND(real_type->info) != BTF_KIND_INT)
468 		return false;
469 
470 	intdata = btf_type_int(real_type);
471 	return !(BTF_INT_ENCODING(intdata) & BTF_INT_SIGNED)
472 		&& BTF_INT_BITS(intdata) == 8;
473 }
474 
475 static bool btf_type_is_char_array(struct btf *btf, const struct btf_type *type)
476 {
477 	const struct btf_type *real_type;
478 	const struct btf_array *array;
479 	u32 intdata;
480 
481 	if (BTF_INFO_KIND(type->info) != BTF_KIND_ARRAY)
482 		return false;
483 
484 	array = (const struct btf_array *)(type + 1);
485 
486 	real_type = btf_type_skip_modifiers(btf, array->type, NULL);
487 
488 	intdata = btf_type_int(real_type);
489 	return !(BTF_INT_ENCODING(intdata) & BTF_INT_SIGNED)
490 		&& BTF_INT_BITS(intdata) == 8;
491 }
492 
493 static struct btf *ctx_btf(struct traceprobe_parse_context *ctx)
494 {
495 	return ctx->struct_btf ? : ctx->btf;
496 }
497 
498 static int check_prepare_btf_string_fetch(char *typename,
499 				struct fetch_insn **pcode,
500 				struct traceprobe_parse_context *ctx)
501 {
502 	struct btf *btf = ctx_btf(ctx);
503 
504 	if (!btf || !ctx->last_type)
505 		return 0;
506 
507 	/* char [] does not need any change. */
508 	if (btf_type_is_char_array(btf, ctx->last_type))
509 		return 0;
510 
511 	/* char * requires dereference the pointer. */
512 	if (btf_type_is_char_ptr(btf, ctx->last_type)) {
513 		struct fetch_insn *code = *pcode + 1;
514 
515 		if (code->op == FETCH_OP_END) {
516 			trace_probe_log_err(ctx->offset, TOO_MANY_OPS);
517 			return -E2BIG;
518 		}
519 		if (typename[0] == 'u')
520 			code->op = FETCH_OP_UDEREF;
521 		else
522 			code->op = FETCH_OP_DEREF;
523 		code->offset = 0;
524 		*pcode = code;
525 		return 0;
526 	}
527 	/* Other types are not available for string */
528 	trace_probe_log_err(ctx->offset, BAD_TYPE4STR);
529 	return -EINVAL;
530 }
531 
532 static const char *fetch_type_from_btf_type(struct btf *btf,
533 					const struct btf_type *type,
534 					struct traceprobe_parse_context *ctx)
535 {
536 	u32 intdata;
537 
538 	/* TODO: const char * could be converted as a string */
539 	switch (BTF_INFO_KIND(type->info)) {
540 	case BTF_KIND_ENUM:
541 		/* enum is "int", so convert to "s32" */
542 		return "s32";
543 	case BTF_KIND_ENUM64:
544 		return "s64";
545 	case BTF_KIND_PTR:
546 		/* pointer will be converted to "x??" */
547 		if (IS_ENABLED(CONFIG_64BIT))
548 			return "x64";
549 		else
550 			return "x32";
551 	case BTF_KIND_INT:
552 		intdata = btf_type_int(type);
553 		if (BTF_INT_ENCODING(intdata) & BTF_INT_SIGNED) {
554 			switch (BTF_INT_BITS(intdata)) {
555 			case 8:
556 				return "s8";
557 			case 16:
558 				return "s16";
559 			case 32:
560 				return "s32";
561 			case 64:
562 				return "s64";
563 			}
564 		} else {	/* unsigned */
565 			switch (BTF_INT_BITS(intdata)) {
566 			case 8:
567 				return "u8";
568 			case 16:
569 				return "u16";
570 			case 32:
571 				return "u32";
572 			case 64:
573 				return "u64";
574 			}
575 			/* bitfield, size is encoded in the type */
576 			ctx->last_bitsize = BTF_INT_BITS(intdata);
577 			ctx->last_bitoffs += BTF_INT_OFFSET(intdata);
578 			return "u64";
579 		}
580 	}
581 	/* TODO: support other types */
582 
583 	return NULL;
584 }
585 
586 static int query_btf_context(struct traceprobe_parse_context *ctx)
587 {
588 	const struct btf_param *param;
589 	const struct btf_type *type;
590 	struct btf *btf;
591 	s32 nr;
592 
593 	if (ctx->btf)
594 		return 0;
595 
596 	if (!ctx->funcname)
597 		return -EINVAL;
598 
599 	type = btf_find_func_proto(ctx->funcname, &btf);
600 	if (!type)
601 		return -ENOENT;
602 
603 	ctx->btf = btf;
604 	ctx->proto = type;
605 
606 	/* ctx->params is optional, since func(void) will not have params. */
607 	nr = 0;
608 	param = btf_get_func_param(type, &nr);
609 	if (!IS_ERR_OR_NULL(param)) {
610 		/* Hide the first 'data' argument of tracepoint */
611 		if (ctx->flags & TPARG_FL_TPOINT) {
612 			nr--;
613 			param++;
614 		}
615 	}
616 
617 	if (nr > 0) {
618 		ctx->nr_params = nr;
619 		ctx->params = param;
620 	} else {
621 		ctx->nr_params = 0;
622 		ctx->params = NULL;
623 	}
624 
625 	return 0;
626 }
627 
628 static void clear_struct_btf(struct traceprobe_parse_context *ctx)
629 {
630 	if (ctx->struct_btf) {
631 		btf_put(ctx->struct_btf);
632 		ctx->struct_btf = NULL;
633 		ctx->last_struct = NULL;
634 	}
635 }
636 
637 static void clear_btf_context(struct traceprobe_parse_context *ctx)
638 {
639 	if (ctx->btf) {
640 		btf_put(ctx->btf);
641 		ctx->btf = NULL;
642 		ctx->proto = NULL;
643 		ctx->params = NULL;
644 		ctx->nr_params = 0;
645 	}
646 }
647 
648 /* Return 1 if the field separator is arrow operator ('->') */
649 static int split_next_field(char *varname, char **next_field,
650 			    struct traceprobe_parse_context *ctx)
651 {
652 	char *field;
653 	int ret = 0;
654 
655 	field = strpbrk(varname, ".-");
656 	if (field) {
657 		if (field[0] == '-' && field[1] == '>') {
658 			field[0] = '\0';
659 			field += 2;
660 			ret = 1;
661 		} else if (field[0] == '.') {
662 			field[0] = '\0';
663 			field += 1;
664 		} else {
665 			trace_probe_log_err(ctx->offset + field - varname, BAD_HYPHEN);
666 			return -EINVAL;
667 		}
668 		*next_field = field;
669 	}
670 
671 	return ret;
672 }
673 
674 /* Inner loop for solving dot operator ('.'). Return bit-offset of the given field */
675 static int get_bitoffset_of_field(char **pfieldname, const struct btf_type **ptype,
676 				  struct traceprobe_parse_context *ctx)
677 {
678 	const struct btf_type *type = *ptype;
679 	const struct btf_member *field;
680 	struct btf *btf = ctx_btf(ctx);
681 	char *fieldname = *pfieldname;
682 	int bitoffs = 0;
683 	u32 anon_offs;
684 	char *next;
685 	int is_ptr;
686 
687 	do {
688 		next = NULL;
689 		is_ptr = split_next_field(fieldname, &next, ctx);
690 		if (is_ptr < 0)
691 			return is_ptr;
692 
693 		anon_offs = 0;
694 		field = btf_find_struct_member(btf, type, fieldname,
695 						&anon_offs);
696 		if (IS_ERR(field)) {
697 			trace_probe_log_err(ctx->offset, BAD_BTF_TID);
698 			return PTR_ERR(field);
699 		}
700 		if (!field) {
701 			trace_probe_log_err(ctx->offset, NO_BTF_FIELD);
702 			return -ENOENT;
703 		}
704 		/* Add anonymous structure/union offset */
705 		bitoffs += anon_offs;
706 
707 		/* Accumulate the bit-offsets of the dot-connected fields */
708 		if (btf_type_kflag(type)) {
709 			bitoffs += BTF_MEMBER_BIT_OFFSET(field->offset);
710 			ctx->last_bitsize = BTF_MEMBER_BITFIELD_SIZE(field->offset);
711 		} else {
712 			bitoffs += field->offset;
713 			ctx->last_bitsize = 0;
714 		}
715 
716 			type = btf_type_skip_modifiers(btf, field->type, NULL);
717 			if (!type) {
718 				trace_probe_log_err(ctx->offset, BAD_BTF_TID);
719 				return -EINVAL;
720 			}
721 
722 		if (next)
723 			ctx->offset += next - fieldname;
724 		fieldname = next;
725 	} while (!is_ptr && fieldname);
726 
727 	*pfieldname = fieldname;
728 	*ptype = type;
729 
730 	return bitoffs;
731 }
732 /*
733  * Parse the field of data structure. The @type must be a pointer type
734  * pointing the target data structure type.
735  */
736 static int parse_btf_field(char *fieldname, const struct btf_type *type,
737 			   struct fetch_insn **pcode, struct fetch_insn *end,
738 			   struct traceprobe_parse_context *ctx)
739 {
740 	struct fetch_insn *code = *pcode;
741 	struct btf *btf = ctx_btf(ctx);
742 	bool is_first_field = true;
743 	int bitoffs;
744 
745 	do {
746 		/* For the first field of typecast, @type will be the target structure type. */
747 		if (!(is_first_field && ctx->struct_btf)) {
748 			/* Outer loop for solving arrow operator ('->') */
749 			if (BTF_INFO_KIND(type->info) != BTF_KIND_PTR) {
750 				trace_probe_log_err(ctx->offset, NO_PTR_STRCT);
751 				return -EINVAL;
752 			}
753 
754 			/* Convert a struct pointer type to a struct type */
755 			type = btf_type_skip_modifiers(btf, type->type, NULL);
756 			if (!type) {
757 				trace_probe_log_err(ctx->offset, BAD_BTF_TID);
758 				return -EINVAL;
759 			}
760 		}
761 
762 		bitoffs = get_bitoffset_of_field(&fieldname, &type, ctx);
763 		if (bitoffs < 0)
764 			return bitoffs;
765 		if (++code == end) {
766 			trace_probe_log_err(ctx->offset, TOO_MANY_OPS);
767 			return -EINVAL;
768 		}
769 		code->op = FETCH_OP_DEREF;	/* TODO: user deref support */
770 		code->offset = bitoffs / 8;
771 		if (is_first_field && ctx->struct_btf) {
772 			/* The first field can be typecasted with field option. */
773 			code->offset -= ctx->prefix_byteoffs;
774 		}
775 		*pcode = code;
776 
777 		ctx->last_bitoffs = bitoffs % 8;
778 		ctx->last_type = type;
779 		is_first_field = false;
780 	} while (fieldname);
781 
782 	return 0;
783 }
784 
785 static int __store_entry_arg(struct trace_probe *tp, int argnum);
786 
787 static int parse_btf_arg(char *varname,
788 			 struct fetch_insn **pcode, struct fetch_insn *end,
789 			 struct traceprobe_parse_context *ctx)
790 {
791 	struct fetch_insn *code = *pcode;
792 	const struct btf_param *params;
793 	const struct btf_type *type;
794 	char *field = NULL;
795 	int i, is_ptr, ret;
796 	u32 tid;
797 
798 	/* Note: field is not separated at this point, so check prefix. */
799 	if (!str_has_prefix(varname, "$current") &&
800 	    !ctx->funcname && !(ctx->flags & TPARG_FL_TEVENT))
801 		return -EINVAL;
802 
803 	is_ptr = split_next_field(varname, &field, ctx);
804 	if (is_ptr < 0)
805 		return is_ptr;
806 	if (!is_ptr && field) {
807 		/* dot-connected field on an argument is not supported. */
808 		trace_probe_log_err(ctx->offset + field - varname,
809 				    NOSUP_DAT_ARG);
810 		return -EOPNOTSUPP;
811 	}
812 
813 	if (!strcmp(varname, "$current")) {
814 		code->op = FETCH_OP_CURRENT;
815 		/* If no typecast is specified for $current, use task_struct by default */
816 		ret = bpf_find_btf_id("task_struct", BTF_KIND_STRUCT, &ctx->struct_btf);
817 		if (ret < 0) {
818 			trace_probe_log_err(ctx->offset, NO_BTF_ENTRY);
819 			return -ENOENT;
820 		}
821 		tid = (u32)ret;
822 		type = ctx->last_struct =
823 			btf_type_skip_modifiers(ctx->struct_btf, tid, NULL);
824 		goto found_type;
825 	}
826 
827 	if (ctx->flags & TPARG_FL_RETURN && !strcmp(varname, "$retval")) {
828 		code->op = FETCH_OP_RETVAL;
829 		/* Check whether the function return type is not void, even with typecast. */
830 		if (query_btf_context(ctx) == 0) {
831 			if (ctx->proto->type == 0) {
832 				trace_probe_log_err(ctx->offset, NO_RETVAL);
833 				return -ENOENT;
834 			}
835 			tid = ctx->proto->type;
836 			goto found;
837 		}
838 		if (field) {
839 			trace_probe_log_err(ctx->offset + field - varname,
840 					    NO_BTF_ENTRY);
841 			return -ENOENT;
842 		}
843 		return 0;
844 	}
845 
846 	if (!ctx->btf) {
847 		ret = query_btf_context(ctx);
848 		if (ret < 0 || ctx->nr_params == 0) {
849 			trace_probe_log_err(ctx->offset, NO_BTF_ENTRY);
850 			return -ENOENT;
851 		}
852 	}
853 	params = ctx->params;
854 
855 	for (i = 0; i < ctx->nr_params; i++) {
856 		const char *name = btf_name_by_offset(ctx->btf, params[i].name_off);
857 
858 		if (name && !strcmp(name, varname)) {
859 			if (tparg_is_function_entry(ctx->flags)) {
860 				code->op = FETCH_OP_ARG;
861 				if (ctx->flags & TPARG_FL_TPOINT)
862 					code->param = i + 1;
863 				else
864 					code->param = i;
865 			} else if (tparg_is_function_return(ctx->flags)) {
866 				code->op = FETCH_OP_EDATA;
867 				ret = __store_entry_arg(ctx->tp, i);
868 				if (ret < 0) {
869 					/* internal error */
870 					return ret;
871 				}
872 				code->offset = ret;
873 			}
874 			tid = params[i].type;
875 			goto found;
876 		}
877 	}
878 	trace_probe_log_err(ctx->offset, NO_BTFARG);
879 	return -ENOENT;
880 
881 found:
882 	type = btf_type_skip_modifiers(ctx->btf, tid, NULL);
883 found_type:
884 	if (!type) {
885 		trace_probe_log_err(ctx->offset, BAD_BTF_TID);
886 		return -EINVAL;
887 	}
888 	/* Initialize the last type information */
889 	ctx->last_type = type;
890 	ctx->last_bitoffs = 0;
891 	ctx->last_bitsize = 0;
892 	if (field) {
893 		ctx->offset += field - varname;
894 		return parse_btf_field(field, type, pcode, end, ctx);
895 	}
896 	return 0;
897 }
898 
899 static const struct fetch_type *find_fetch_type_from_btf_type(
900 					struct traceprobe_parse_context *ctx)
901 {
902 	struct btf *btf = ctx_btf(ctx);
903 	const char *typestr = NULL;
904 
905 	if (btf && ctx->last_type)
906 		typestr = fetch_type_from_btf_type(btf, ctx->last_type, ctx);
907 
908 	return find_fetch_type(typestr, ctx->flags);
909 }
910 
911 static int parse_btf_bitfield(struct fetch_insn **pcode,
912 			      struct traceprobe_parse_context *ctx)
913 {
914 	struct fetch_insn *code = *pcode;
915 
916 	if ((ctx->last_bitsize % 8 == 0) && ctx->last_bitoffs == 0)
917 		return 0;
918 
919 	code++;
920 	if (code->op != FETCH_OP_NOP) {
921 		trace_probe_log_err(ctx->offset, TOO_MANY_OPS);
922 		return -EINVAL;
923 	}
924 	*pcode = code;
925 
926 	code->op = FETCH_OP_MOD_BF;
927 	code->lshift = 64 - (ctx->last_bitsize + ctx->last_bitoffs);
928 	code->rshift = 64 - ctx->last_bitsize;
929 	code->basesize = 64 / 8;
930 	return 0;
931 }
932 
933 static int query_btf_struct(const char *sname, struct traceprobe_parse_context *ctx)
934 {
935 	struct btf *btf = NULL;
936 	int id;
937 
938 	/* A struct_btf should only be used by a single argument */
939 	if (WARN_ON_ONCE(ctx->struct_btf)) {
940 		btf_put(ctx->struct_btf);
941 		ctx->struct_btf = NULL;
942 	}
943 
944 	id = bpf_find_btf_id(sname, BTF_KIND_STRUCT, &btf);
945 	if (id < 0)
946 		return id;
947 	ctx->struct_btf = btf;
948 	ctx->last_struct = btf_type_by_id(ctx->struct_btf, id);
949 	return 0;
950 }
951 
952 static int parse_btf_casttype(char *casttype, struct traceprobe_parse_context *ctx)
953 {
954 	char *field;
955 	int ret;
956 
957 	/* Field option - evaluated later. */
958 	field = strchr(casttype, ',');
959 	if (field)
960 		*field++ = '\0';
961 
962 	ret = query_btf_struct(casttype, ctx);
963 	if (ret < 0) {
964 		trace_probe_log_err(ctx->offset, NO_PTR_STRCT);
965 		return -EINVAL;
966 	}
967 
968 	if (field) {
969 		struct btf_type *type = (struct btf_type *)ctx->last_struct;
970 
971 		ctx->offset += field - casttype;
972 		ret = get_bitoffset_of_field(&field, &ctx->last_struct, ctx);
973 		if (ret < 0)
974 			return ret;
975 		if (ret % 8) {
976 			trace_probe_log_err(ctx->offset, TYPECAST_NOT_ALIGNED);
977 			return -EINVAL;
978 		}
979 		if (field != NULL) {
980 			/* this means @field skips an arrow operator ("->"). */
981 			trace_probe_log_err(ctx->offset - 2, TYPECAST_BAD_ARROW);
982 			return -EINVAL;
983 		}
984 		ctx->prefix_byteoffs = ret / 8;
985 		/* Restore the original struct type (overwritten by get_bitoffset_of_field) */
986 		ctx->last_struct = type;
987 	}
988 
989 	return ret;
990 }
991 
992 /* Find the matching closing parenthesis for a given opening parenthesis. */
993 static char *find_matched_close_paren(char *s)
994 {
995 	char *p = s;
996 	int count = 0;
997 
998 	while (*p) {
999 		if (*p == '(')
1000 			count++;
1001 		else if (*p == ')') {
1002 			if (--count == 0)
1003 				return p;
1004 		}
1005 		p++;
1006 	}
1007 	return NULL;
1008 }
1009 
1010 static int handle_typecast(char *arg, struct fetch_insn **pcode,
1011 			   struct fetch_insn *end,
1012 			   struct traceprobe_parse_context *ctx)
1013 {
1014 	int orig_offset = ctx->offset;
1015 	char *close;
1016 	char *tmp;
1017 	int ret;
1018 
1019 	if (!(tparg_is_event_probe(ctx->flags) ||
1020 	      tparg_is_function_entry(ctx->flags) ||
1021 	      tparg_is_function_return(ctx->flags))) {
1022 		trace_probe_log_err(ctx->offset, NOSUP_BTFARG);
1023 		return -EOPNOTSUPP;
1024 	}
1025 
1026 	/*
1027 	 * Always consider the token after typecast as a nested call
1028 	 * For example: (STRUCT)VAR->FIELD and (STRUCT)(VAR)->FIELD are same.
1029 	 * VAR is solved in the nested call.
1030 	 */
1031 	ctx->nested_level++;
1032 	if (ctx->nested_level > TRACEPROBE_MAX_NESTED_LEVEL) {
1033 		trace_probe_log_err(ctx->offset, TOO_MANY_NESTED);
1034 		return -E2BIG;
1035 	}
1036 
1037 	tmp = strchr(arg, ')');
1038 	if (!tmp) {
1039 		trace_probe_log_err(ctx->offset + strlen(arg),
1040 				    DEREF_OPEN_BRACE);
1041 		return -EINVAL;
1042 	}
1043 	*tmp++ = '\0';
1044 
1045 	ctx->offset += tmp - arg;
1046 	if (*tmp == '(') {
1047 		close = find_matched_close_paren(tmp);
1048 
1049 		if (!close) {
1050 			trace_probe_log_err(ctx->offset, DEREF_OPEN_BRACE);
1051 			return -EINVAL;
1052 		}
1053 		/* We expect a field access for typecast */
1054 		if (close[1] != '-' || close[2] != '>') {
1055 			trace_probe_log_err(ctx->offset + close - tmp + 1,
1056 					    TYPECAST_REQ_FIELD);
1057 			return -EINVAL;
1058 		}
1059 		/* Skip '(' */
1060 		ctx->offset += 1;
1061 		tmp++;
1062 	} else if (*tmp == '+' || *tmp == '-' ||
1063 		   str_has_prefix(tmp, THIS_CPU_PTR_PREFIX) ||
1064 		   str_has_prefix(tmp, THIS_CPU_READ_PREFIX)) {
1065 		/* Dereference can have another field access inside it. */
1066 		char *open = strchr(tmp + 1, '(');
1067 
1068 		if (!open) {
1069 			trace_probe_log_err(ctx->offset,
1070 					    DEREF_NEED_BRACE);
1071 			return -EINVAL;
1072 		}
1073 		close = find_matched_close_paren(open);
1074 		if (!close) {
1075 			trace_probe_log_err(ctx->offset + strlen(tmp),
1076 					    DEREF_OPEN_BRACE);
1077 			return -EINVAL;
1078 		}
1079 		close++;
1080 		/* We expect a field access for typecast */
1081 		if (close[0] != '-' || close[1] != '>') {
1082 			trace_probe_log_err(ctx->offset + close - tmp,
1083 					    TYPECAST_REQ_FIELD);
1084 			return -EINVAL;
1085 		}
1086 	} else {
1087 		if (tmp[0] == '@') {
1088 			/* @sym+offset is not allowed without parenthesized */
1089 			close = strpbrk(tmp, "+-");
1090 			if (close && isdigit(close[1])) {
1091 				trace_probe_log_err(ctx->offset,
1092 						    TYPECAST_SYM_OFFSET);
1093 				return -EINVAL;
1094 			}
1095 		}
1096 		/* Inner variable name */
1097 		close = strchr(tmp, '-');
1098 		if (!close || close[1] != '>') {
1099 			trace_probe_log_err(ctx->offset + strlen(tmp),
1100 					    TYPECAST_REQ_FIELD);
1101 			return -EINVAL;
1102 		}
1103 	}
1104 	*close = '\0';
1105 
1106 	/* We need to parse the nested one */
1107 	ret = parse_probe_arg(tmp, find_fetch_type(NULL, ctx->flags),
1108 			      pcode, end, ctx);
1109 	if (ret < 0)
1110 		return ret;
1111 	ctx->nested_level--;
1112 	clear_struct_btf(ctx);
1113 
1114 	/* Let tmp point the field name. */
1115 	if (close[1] == '-')
1116 		tmp = close + 3; /* Skip "->" after closing parenthesis */
1117 	else
1118 		tmp = close + 2; /* Skip ">" after inner variable name */
1119 
1120 	/* resolve the typecast struct name */
1121 	ctx->offset = orig_offset + 1; /* for the '(' */
1122 	ret = parse_btf_casttype(arg + 1, ctx);
1123 	if (ret < 0)
1124 		return ret;
1125 
1126 	ctx->offset = orig_offset + tmp - arg;
1127 	ret = parse_btf_field(tmp, ctx->last_struct, pcode, end, ctx);
1128 	ctx->prefix_byteoffs = 0;
1129 	return ret;
1130 }
1131 
1132 #else /* !CONFIG_PROBE_EVENTS_BTF_ARGS */
1133 
1134 static void clear_struct_btf(struct traceprobe_parse_context *ctx)
1135 {
1136 	ctx->struct_btf = NULL;
1137 }
1138 
1139 static void clear_btf_context(struct traceprobe_parse_context *ctx)
1140 {
1141 	ctx->btf = NULL;
1142 }
1143 
1144 static int query_btf_context(struct traceprobe_parse_context *ctx)
1145 {
1146 	return -EOPNOTSUPP;
1147 }
1148 
1149 static int parse_btf_arg(char *varname,
1150 			 struct fetch_insn **pcode, struct fetch_insn *end,
1151 			 struct traceprobe_parse_context *ctx)
1152 {
1153 	trace_probe_log_err(ctx->offset, NOSUP_BTFARG);
1154 	return -EOPNOTSUPP;
1155 }
1156 
1157 static int parse_btf_bitfield(struct fetch_insn **pcode,
1158 			      struct traceprobe_parse_context *ctx)
1159 {
1160 	trace_probe_log_err(ctx->offset, NOSUP_BTFARG);
1161 	return -EOPNOTSUPP;
1162 }
1163 
1164 #define find_fetch_type_from_btf_type(ctx)		\
1165 	find_fetch_type(NULL, ctx->flags)
1166 
1167 static int check_prepare_btf_string_fetch(char *typename,
1168 				struct fetch_insn **pcode,
1169 				struct traceprobe_parse_context *ctx)
1170 {
1171 	return 0;
1172 }
1173 
1174 static int handle_typecast(char *arg, struct fetch_insn **pcode,
1175 			   struct fetch_insn *end,
1176 			   struct traceprobe_parse_context *ctx)
1177 {
1178 	trace_probe_log_err(ctx->offset, NOSUP_BTFARG);
1179 	return -EOPNOTSUPP;
1180 }
1181 
1182 #endif /* CONFIG_PROBE_EVENTS_BTF_ARGS */
1183 
1184 #ifdef CONFIG_HAVE_FUNCTION_ARG_ACCESS_API
1185 
1186 static void store_entry_arg_at(struct fetch_insn *code, int argnum, int offset)
1187 {
1188 	code[0].op = FETCH_OP_ARG;
1189 	code[0].param = argnum;
1190 	code[1].op = FETCH_OP_ST_EDATA;
1191 	code[1].offset = offset;
1192 }
1193 
1194 static int get_entry_arg_max_offset(struct probe_entry_arg *earg)
1195 {
1196 	int i, max_offset = 0;
1197 
1198 	/*
1199 	 * earg->code[] array has an operation sequence which is run in
1200 	 * the entry handler.
1201 	 * The sequence stopped by FETCH_OP_END and each data stored in
1202 	 * the entry data buffer by FETCH_OP_ST_EDATA. The FETCH_OP_ST_EDATA
1203 	 * stores the data at the data buffer + its offset, and all data are
1204 	 * "unsigned long" size. The offset must be increased when a data is
1205 	 * stored. Thus we need to find the last FETCH_OP_ST_EDATA in the
1206 	 * code array.
1207 	 */
1208 	for (i = 0; i < earg->size - 1 && earg->code[i].op != FETCH_OP_END; i++) {
1209 		if (earg->code[i].op == FETCH_OP_ST_EDATA)
1210 			if (earg->code[i].offset > max_offset)
1211 				max_offset = earg->code[i].offset;
1212 	}
1213 	return max_offset;
1214 }
1215 
1216 /*
1217  * Add the entry code to store the 'argnum'th parameter and return the offset
1218  * in the entry data buffer where the data will be stored.
1219  */
1220 static int __store_entry_arg(struct trace_probe *tp, int argnum)
1221 {
1222 	struct probe_entry_arg *earg = tp->entry_arg;
1223 	int i, offset, last_offset = 0;
1224 
1225 	if (!earg) {
1226 		earg = kzalloc_flex(*earg, code, 2 * tp->nr_args + 1);
1227 		if (!earg)
1228 			return -ENOMEM;
1229 		earg->size = 2 * tp->nr_args + 1;
1230 		/* Fill the code buffer with 'end' to simplify it */
1231 		for (i = 0; i < earg->size; i++)
1232 			earg->code[i].op = FETCH_OP_END;
1233 		tp->entry_arg = earg;
1234 		store_entry_arg_at(earg->code, argnum, 0);
1235 		return 0;
1236 	}
1237 
1238 	/*
1239 	 * NOTE: if anyone change the following rule, please rewrite this.
1240 	 * The entry code array is filled with the pair of
1241 	 *
1242 	 * [FETCH_OP_ARG(argnum)]
1243 	 * [FETCH_OP_ST_EDATA(offset of entry data buffer)]
1244 	 *
1245 	 * and the rest of entries are filled with [FETCH_OP_END].
1246 	 * The offset should be incremented, thus the last pair should
1247 	 * have the largest offset.
1248 	 */
1249 
1250 	/* Search the offset for the sprcified argnum. */
1251 	for (i = 0; i < earg->size - 1 && earg->code[i].op != FETCH_OP_END; i += 2) {
1252 		if (WARN_ON_ONCE(earg->code[i].op != FETCH_OP_ARG))
1253 			return -EINVAL;
1254 
1255 		if (earg->code[i].param != argnum)
1256 			continue;
1257 
1258 		if (WARN_ON_ONCE(earg->code[i + 1].op != FETCH_OP_ST_EDATA))
1259 			return -EINVAL;
1260 
1261 		return earg->code[i + 1].offset;
1262 	}
1263 	/* Not found, append new entry if possible. */
1264 	if (i >= earg->size - 1)
1265 		return -ENOSPC;
1266 
1267 	/* The last entry must have the largest offset. */
1268 	if (i != 0) {
1269 		if (WARN_ON_ONCE(earg->code[i - 1].op != FETCH_OP_ST_EDATA))
1270 			return -EINVAL;
1271 		last_offset = earg->code[i - 1].offset;
1272 	}
1273 
1274 	offset = last_offset + sizeof(unsigned long);
1275 	store_entry_arg_at(&earg->code[i], argnum, offset);
1276 	return offset;
1277 }
1278 
1279 int traceprobe_get_entry_data_size(struct trace_probe *tp)
1280 {
1281 	struct probe_entry_arg *earg = tp->entry_arg;
1282 
1283 	if (!earg)
1284 		return 0;
1285 
1286 	return get_entry_arg_max_offset(earg) + sizeof(unsigned long);
1287 }
1288 
1289 void store_trace_entry_data(void *edata, struct trace_probe *tp, struct pt_regs *regs)
1290 {
1291 	struct probe_entry_arg *earg = tp->entry_arg;
1292 	unsigned long val = 0;
1293 	int i;
1294 
1295 	if (!earg)
1296 		return;
1297 
1298 	for (i = 0; i < earg->size; i++) {
1299 		struct fetch_insn *code = &earg->code[i];
1300 
1301 		switch (code->op) {
1302 		case FETCH_OP_ARG:
1303 			val = regs_get_kernel_argument(regs, code->param);
1304 			break;
1305 		case FETCH_OP_ST_EDATA:
1306 			*(unsigned long *)((unsigned long)edata + code->offset) = val;
1307 			break;
1308 		case FETCH_OP_END:
1309 			goto end;
1310 		default:
1311 			break;
1312 		}
1313 	}
1314 end:
1315 	return;
1316 }
1317 NOKPROBE_SYMBOL(store_trace_entry_data)
1318 #endif
1319 
1320 #define PARAM_MAX_STACK (THREAD_SIZE / sizeof(unsigned long))
1321 
1322 /* Parse $vars. @orig_arg points '$', which syncs to @ctx->offset */
1323 static int parse_probe_vars(char *orig_arg, const struct fetch_type *t,
1324 			    struct fetch_insn **pcode,
1325 			    struct fetch_insn *end,
1326 			    struct traceprobe_parse_context *ctx)
1327 {
1328 	struct fetch_insn *code = *pcode;
1329 	int err = TP_ERR_BAD_VAR;
1330 	char *arg = orig_arg + 1;
1331 	unsigned long param;
1332 	int ret = 0;
1333 	int len;
1334 
1335 	if (ctx->flags & TPARG_FL_TEVENT) {
1336 		if (parse_trace_event(arg, code, ctx) < 0) {
1337 			/* 'comm' should be checked after field parsing. */
1338 			if (strcmp(arg, "comm") == 0 || strcmp(arg, "COMM") == 0) {
1339 				code->op = FETCH_OP_COMM;
1340 				return 0;
1341 			}
1342 			goto inval;
1343 		}
1344 		return 0;
1345 	}
1346 
1347 	if (str_has_prefix(arg, "retval")) {
1348 		if (!(ctx->flags & TPARG_FL_RETURN)) {
1349 			err = TP_ERR_RETVAL_ON_PROBE;
1350 			goto inval;
1351 		}
1352 		if (!(ctx->flags & TPARG_FL_KERNEL) ||
1353 		    !IS_ENABLED(CONFIG_PROBE_EVENTS_BTF_ARGS)) {
1354 			code->op = FETCH_OP_RETVAL;
1355 			return 0;
1356 		}
1357 		return parse_btf_arg(orig_arg, pcode, end, ctx);
1358 	}
1359 
1360 	len = str_has_prefix(arg, "stack");
1361 	if (len) {
1362 
1363 		if (arg[len] == '\0') {
1364 			code->op = FETCH_OP_STACKP;
1365 			return 0;
1366 		}
1367 
1368 		if (isdigit(arg[len])) {
1369 			ret = kstrtoul(arg + len, 10, &param);
1370 			if (ret)
1371 				goto inval;
1372 
1373 			if ((ctx->flags & TPARG_FL_KERNEL) &&
1374 			    param > PARAM_MAX_STACK) {
1375 				err = TP_ERR_BAD_STACK_NUM;
1376 				goto inval;
1377 			}
1378 			code->op = FETCH_OP_STACK;
1379 			code->param = (unsigned int)param;
1380 			return 0;
1381 		}
1382 		goto inval;
1383 	}
1384 
1385 	if (strcmp(arg, "comm") == 0 || strcmp(arg, "COMM") == 0) {
1386 		code->op = FETCH_OP_COMM;
1387 		return 0;
1388 	}
1389 
1390 	/* $current returns the address of the current task_struct. */
1391 	if (str_has_prefix(arg, "current")) {
1392 		/* $current is only supported by kernel probe. */
1393 		if (!(ctx->flags & TPARG_FL_KERNEL)) {
1394 			err = TP_ERR_BAD_VAR;
1395 			goto inval;
1396 		}
1397 		arg += strlen("current");
1398 		if (*arg == '-' && IS_ENABLED(CONFIG_PROBE_EVENTS_BTF_ARGS))
1399 			return parse_btf_arg(orig_arg, pcode, end, ctx);
1400 
1401 		if (*arg != '\0')
1402 			goto inval;
1403 
1404 		code->op = FETCH_OP_CURRENT;
1405 		return 0;
1406 	}
1407 
1408 #ifdef CONFIG_HAVE_FUNCTION_ARG_ACCESS_API
1409 	len = str_has_prefix(arg, "arg");
1410 	if (len) {
1411 		ret = kstrtoul(arg + len, 10, &param);
1412 		if (ret)
1413 			goto inval;
1414 
1415 		if (!param || param > PARAM_MAX_STACK) {
1416 			err = TP_ERR_BAD_ARG_NUM;
1417 			goto inval;
1418 		}
1419 		param--; /* argN starts from 1, but internal arg[N] starts from 0 */
1420 
1421 		if (tparg_is_function_entry(ctx->flags)) {
1422 			code->op = FETCH_OP_ARG;
1423 			code->param = (unsigned int)param;
1424 			/*
1425 			 * The tracepoint probe will probe a stub function, and the
1426 			 * first parameter of the stub is a dummy and should be ignored.
1427 			 */
1428 			if (ctx->flags & TPARG_FL_TPOINT)
1429 				code->param++;
1430 		} else if (tparg_is_function_return(ctx->flags)) {
1431 			/* function entry argument access from return probe */
1432 			ret = __store_entry_arg(ctx->tp, param);
1433 			if (ret < 0)	/* This error should be an internal error */
1434 				return ret;
1435 
1436 			code->op = FETCH_OP_EDATA;
1437 			code->offset = ret;
1438 		} else {
1439 			err = TP_ERR_NOFENTRY_ARGS;
1440 			goto inval;
1441 		}
1442 		return 0;
1443 	}
1444 #endif
1445 
1446 inval:
1447 	__trace_probe_log_err(ctx->offset, err);
1448 	return -EINVAL;
1449 }
1450 
1451 static int str_to_immediate(char *str, unsigned long *imm)
1452 {
1453 	if (isdigit(str[0]))
1454 		return kstrtoul(str, 0, imm);
1455 	else if (str[0] == '-')
1456 		return kstrtol(str, 0, (long *)imm);
1457 	else if (str[0] == '+')
1458 		return kstrtol(str + 1, 0, (long *)imm);
1459 	return -EINVAL;
1460 }
1461 
1462 static int __parse_imm_string(char *str, char **pbuf, int offs)
1463 {
1464 	size_t len = strlen(str);
1465 
1466 	if (!len || str[len - 1] != '"') {
1467 		trace_probe_log_err(offs + len, IMMSTR_NO_CLOSE);
1468 		return -EINVAL;
1469 	}
1470 	*pbuf = kstrndup(str, len - 1, GFP_KERNEL);
1471 	if (!*pbuf)
1472 		return -ENOMEM;
1473 	return 0;
1474 }
1475 
1476 /* Recursive argument parser */
1477 static int
1478 parse_probe_arg(char *arg, const struct fetch_type *type,
1479 		struct fetch_insn **pcode, struct fetch_insn *end,
1480 		struct traceprobe_parse_context *ctx)
1481 {
1482 	struct fetch_insn *code = *pcode;
1483 	unsigned long param;
1484 	int deref = FETCH_OP_DEREF;
1485 	long offset = 0;
1486 	char *tmp;
1487 	int ret = 0;
1488 
1489 	switch (arg[0]) {
1490 	case '$':
1491 		ret = parse_probe_vars(arg, type, pcode, end, ctx);
1492 		break;
1493 
1494 	case '%':	/* named register */
1495 		if (ctx->flags & (TPARG_FL_TEVENT | TPARG_FL_FPROBE)) {
1496 			/* eprobe and fprobe do not handle registers */
1497 			trace_probe_log_err(ctx->offset, BAD_VAR);
1498 			break;
1499 		}
1500 		ret = regs_query_register_offset(arg + 1);
1501 		if (ret >= 0) {
1502 			code->op = FETCH_OP_REG;
1503 			code->param = (unsigned int)ret;
1504 			ret = 0;
1505 		} else
1506 			trace_probe_log_err(ctx->offset, BAD_REG_NAME);
1507 		break;
1508 
1509 	case '@':	/* memory, file-offset or symbol */
1510 		if (isdigit(arg[1])) {
1511 			ret = kstrtoul(arg + 1, 0, &param);
1512 			if (ret) {
1513 				trace_probe_log_err(ctx->offset, BAD_MEM_ADDR);
1514 				break;
1515 			}
1516 			/* load address */
1517 			code->op = FETCH_OP_IMM;
1518 			code->immediate = param;
1519 		} else if (arg[1] == '+') {
1520 			/* Kernel probes do not support file offsets */
1521 			if (ctx->flags & TPARG_FL_KERNEL) {
1522 				trace_probe_log_err(ctx->offset, FILE_ON_KPROBE);
1523 				return -EINVAL;
1524 			}
1525 			ret = kstrtol(arg + 2, 0, &offset);
1526 			if (ret) {
1527 				trace_probe_log_err(ctx->offset, BAD_FILE_OFFS);
1528 				break;
1529 			}
1530 
1531 			code->op = FETCH_OP_FOFFS;
1532 			code->immediate = (unsigned long)offset;  // imm64?
1533 			offset = 0;
1534 		} else {
1535 			/* uprobes don't support symbols */
1536 			if (!(ctx->flags & TPARG_FL_KERNEL)) {
1537 				trace_probe_log_err(ctx->offset, SYM_ON_UPROBE);
1538 				return -EINVAL;
1539 			}
1540 			/* Preserve symbol for updating */
1541 			code->op = FETCH_NOP_SYMBOL;
1542 			code->data = kstrdup(arg + 1, GFP_KERNEL);
1543 			if (!code->data)
1544 				return -ENOMEM;
1545 			if (++code == end) {
1546 				trace_probe_log_err(ctx->offset, TOO_MANY_OPS);
1547 				return -EINVAL;
1548 			}
1549 			code->op = FETCH_OP_IMM;
1550 			code->immediate = 0;
1551 		}
1552 		/* These are fetching from memory */
1553 		if (++code == end) {
1554 			trace_probe_log_err(ctx->offset, TOO_MANY_OPS);
1555 			return -EINVAL;
1556 		}
1557 		*pcode = code;
1558 		code->op = FETCH_OP_DEREF;
1559 		code->offset = offset;
1560 		break;
1561 
1562 	case '+':	/* deref memory */
1563 	case '-':
1564 		if (arg[1] == 'u') {
1565 			deref = FETCH_OP_UDEREF;
1566 			arg[1] = arg[0];
1567 			arg++;
1568 		}
1569 		if (arg[0] == '+')
1570 			arg++;	/* Skip '+', because kstrtol() rejects it. */
1571 		tmp = strchr(arg, '(');
1572 		if (!tmp) {
1573 			trace_probe_log_err(ctx->offset, DEREF_NEED_BRACE);
1574 			return -EINVAL;
1575 		}
1576 		*tmp = '\0';
1577 		ret = kstrtol(arg, 0, &offset);
1578 		if (ret) {
1579 			trace_probe_log_err(ctx->offset, BAD_DEREF_OFFS);
1580 			break;
1581 		}
1582 		ctx->offset += (tmp + 1 - arg) + (arg[0] != '-' ? 1 : 0);
1583 		arg = tmp + 1;
1584 		ret = handle_dereference(arg, pcode, end, ctx, deref, offset);
1585 		if (ret < 0)
1586 			return ret;
1587 		break;
1588 	case '\\':	/* Immediate value */
1589 		if (arg[1] == '"') {	/* Immediate string */
1590 			ret = __parse_imm_string(arg + 2, &tmp, ctx->offset + 2);
1591 			if (ret)
1592 				break;
1593 			code->op = FETCH_OP_IMMSTR;
1594 			code->data = tmp;
1595 		} else {
1596 			ret = str_to_immediate(arg + 1, &code->immediate);
1597 			if (ret)
1598 				trace_probe_log_err(ctx->offset + 1, BAD_IMM);
1599 			else
1600 				code->op = FETCH_OP_IMM;
1601 		}
1602 		break;
1603 	case '(':
1604 		ret = handle_typecast(arg, pcode, end, ctx);
1605 		break;
1606 	default:
1607 		if (str_has_prefix(arg, THIS_CPU_PTR_PREFIX) ||
1608 		    str_has_prefix(arg, THIS_CPU_READ_PREFIX)) {
1609 			ret = parse_this_cpu(arg, pcode, end, ctx);
1610 		} else if (isalpha(arg[0]) || arg[0] == '_') {
1611 			/* BTF variable or event field*/
1612 			if (ctx->flags & TPARG_FL_TEVENT) {
1613 				ret = parse_trace_event(arg, *pcode, ctx);
1614 				if (ret < 0) {
1615 					trace_probe_log_err(ctx->offset,
1616 							    NO_EVENT_FIELD);
1617 					return -EINVAL;
1618 				}
1619 				break;
1620 			}
1621 			if (!tparg_is_function_entry(ctx->flags) &&
1622 			    !tparg_is_function_return(ctx->flags)) {
1623 				trace_probe_log_err(ctx->offset, NOSUP_BTFARG);
1624 				return -EINVAL;
1625 			}
1626 			ret = parse_btf_arg(arg, pcode, end, ctx);
1627 		}
1628 		break;
1629 	}
1630 	if (!ret && code->op == FETCH_OP_NOP) {
1631 		/* Parsed, but do not find fetch method */
1632 		trace_probe_log_err(ctx->offset, BAD_FETCH_ARG);
1633 		ret = -EINVAL;
1634 	}
1635 	return ret;
1636 }
1637 
1638 /* Bitfield type needs to be parsed into a fetch function */
1639 static int __parse_bitfield_probe_arg(const char *bf,
1640 				      const struct fetch_type *t,
1641 				      struct fetch_insn **pcode)
1642 {
1643 	struct fetch_insn *code = *pcode;
1644 	unsigned long bw, bo;
1645 	char *tail;
1646 
1647 	if (*bf != 'b')
1648 		return 0;
1649 
1650 	bw = simple_strtoul(bf + 1, &tail, 0);	/* Use simple one */
1651 
1652 	if (bw == 0 || *tail != '@')
1653 		return -EINVAL;
1654 
1655 	bf = tail + 1;
1656 	bo = simple_strtoul(bf, &tail, 0);
1657 
1658 	if (tail == bf || *tail != '/')
1659 		return -EINVAL;
1660 	code++;
1661 	if (code->op != FETCH_OP_NOP)
1662 		return -EINVAL;
1663 	*pcode = code;
1664 
1665 	code->op = FETCH_OP_MOD_BF;
1666 	code->lshift = BYTES_TO_BITS(t->size) - (bw + bo);
1667 	code->rshift = BYTES_TO_BITS(t->size) - bw;
1668 	code->basesize = t->size;
1669 
1670 	return (BYTES_TO_BITS(t->size) < (bw + bo)) ? -EINVAL : 0;
1671 }
1672 
1673 /* Split type part from @arg and return it. */
1674 static char *parse_probe_arg_type(char *arg, struct probe_arg *parg,
1675 				  struct traceprobe_parse_context *ctx)
1676 {
1677 	char *t = NULL, *t2, *t3;
1678 	int offs;
1679 
1680 	t = strchr(arg, ':');
1681 	if (t) {
1682 		*t++ = '\0';
1683 		t2 = strchr(t, '[');
1684 		if (t2) {
1685 			*t2++ = '\0';
1686 			t3 = strchr(t2, ']');
1687 			if (!t3) {
1688 				offs = t2 + strlen(t2) - arg;
1689 
1690 				trace_probe_log_err(ctx->offset + offs,
1691 						    ARRAY_NO_CLOSE);
1692 				return ERR_PTR(-EINVAL);
1693 			} else if (t3[1] != '\0') {
1694 				trace_probe_log_err(ctx->offset + t3 + 1 - arg,
1695 						    BAD_ARRAY_SUFFIX);
1696 				return ERR_PTR(-EINVAL);
1697 			}
1698 			*t3 = '\0';
1699 			if (kstrtouint(t2, 0, &parg->count) || !parg->count) {
1700 				trace_probe_log_err(ctx->offset + t2 - arg,
1701 						    BAD_ARRAY_NUM);
1702 				return ERR_PTR(-EINVAL);
1703 			}
1704 			if (parg->count > MAX_ARRAY_LEN) {
1705 				trace_probe_log_err(ctx->offset + t2 - arg,
1706 						    ARRAY_TOO_BIG);
1707 				return ERR_PTR(-EINVAL);
1708 			}
1709 		}
1710 	}
1711 	offs = t ? t - arg : 0;
1712 
1713 	/*
1714 	 * Since $comm and immediate string can not be dereferenced,
1715 	 * we can find those by strcmp. But ignore for eprobes.
1716 	 */
1717 	if (!(ctx->flags & TPARG_FL_TEVENT) &&
1718 	    (strcmp(arg, "$comm") == 0 || strcmp(arg, "$COMM") == 0 ||
1719 	     strncmp(arg, "\\\"", 2) == 0)) {
1720 		/* The type of $comm must be "string", and not an array type. */
1721 		if (parg->count || (t && strcmp(t, "string"))) {
1722 			trace_probe_log_err(ctx->offset + offs, NEED_STRING_TYPE);
1723 			return ERR_PTR(-EINVAL);
1724 		}
1725 		parg->type = find_fetch_type("string", ctx->flags);
1726 	} else
1727 		parg->type = find_fetch_type(t, ctx->flags);
1728 
1729 	if (!parg->type) {
1730 		trace_probe_log_err(ctx->offset + offs, BAD_TYPE);
1731 		return ERR_PTR(-EINVAL);
1732 	}
1733 
1734 	return t;
1735 }
1736 
1737 /* After parsing, adjust the fetch_insn according to the probe_arg */
1738 static int finalize_fetch_insn(struct fetch_insn *code,
1739 			       struct probe_arg *parg,
1740 			       char *type,
1741 			       int type_offset,
1742 			       struct traceprobe_parse_context *ctx)
1743 {
1744 	struct fetch_insn *scode;
1745 	int ret;
1746 
1747 	/* Store operation */
1748 	if (parg->type->is_string) {
1749 		/* Check bad combination of the type and the last fetch_insn. */
1750 		if (!strcmp(parg->type->name, "symstr")) {
1751 			if (code->op != FETCH_OP_REG && code->op != FETCH_OP_STACK &&
1752 			    code->op != FETCH_OP_RETVAL && code->op != FETCH_OP_ARG &&
1753 			    code->op != FETCH_OP_DEREF && code->op != FETCH_OP_TP_ARG) {
1754 				trace_probe_log_err(ctx->offset + type_offset,
1755 						    BAD_SYMSTRING);
1756 				return -EINVAL;
1757 			}
1758 		} else {
1759 			if (code->op != FETCH_OP_DEREF && code->op != FETCH_OP_UDEREF &&
1760 			    code->op != FETCH_OP_IMM && code->op != FETCH_OP_COMM &&
1761 			    code->op != FETCH_OP_IMMSTR && code->op != FETCH_OP_TP_ARG) {
1762 				trace_probe_log_err(ctx->offset + type_offset,
1763 						    BAD_STRING);
1764 				return -EINVAL;
1765 			}
1766 		}
1767 
1768 		if (!strcmp(parg->type->name, "symstr") ||
1769 		    (code->op == FETCH_OP_IMM || code->op == FETCH_OP_COMM ||
1770 		     code->op == FETCH_OP_IMMSTR) || code->op == FETCH_OP_TP_ARG ||
1771 		     parg->count) {
1772 			/*
1773 			 * IMM, DATA and COMM is pointing actual address, those
1774 			 * must be kept, and if parg->count != 0, this is an
1775 			 * array of string pointers instead of string address
1776 			 * itself.
1777 			 * For the symstr, it doesn't need to dereference, thus
1778 			 * it just get the value.
1779 			 */
1780 			code++;
1781 			if (code->op != FETCH_OP_NOP) {
1782 				trace_probe_log_err(ctx->offset, TOO_MANY_OPS);
1783 				return -EINVAL;
1784 			}
1785 		}
1786 
1787 		/* If op == DEREF, replace it with STRING */
1788 		if (!strcmp(parg->type->name, "ustring") ||
1789 		    code->op == FETCH_OP_UDEREF)
1790 			code->op = FETCH_OP_ST_USTRING;
1791 		else if (!strcmp(parg->type->name, "symstr"))
1792 			code->op = FETCH_OP_ST_SYMSTR;
1793 		else
1794 			code->op = FETCH_OP_ST_STRING;
1795 		code->size = parg->type->size;
1796 		parg->dynamic = true;
1797 	} else if (code->op == FETCH_OP_DEREF) {
1798 		code->op = FETCH_OP_ST_MEM;
1799 		code->size = parg->type->size;
1800 	} else if (code->op == FETCH_OP_UDEREF) {
1801 		code->op = FETCH_OP_ST_UMEM;
1802 		code->size = parg->type->size;
1803 	} else {
1804 		code++;
1805 		if (code->op != FETCH_OP_NOP) {
1806 			trace_probe_log_err(ctx->offset, TOO_MANY_OPS);
1807 			return -E2BIG;
1808 		}
1809 		code->op = FETCH_OP_ST_RAW;
1810 		code->size = parg->type->size;
1811 	}
1812 
1813 	/* Save storing fetch_insn. */
1814 	scode = code;
1815 
1816 	/* Modify operation */
1817 	if (type != NULL) {
1818 		/* Bitfield needs a special fetch_insn. */
1819 		ret = __parse_bitfield_probe_arg(type, parg->type, &code);
1820 		if (ret) {
1821 			trace_probe_log_err(ctx->offset + type_offset, BAD_BITFIELD);
1822 			return ret;
1823 		}
1824 	} else if (IS_ENABLED(CONFIG_PROBE_EVENTS_BTF_ARGS) &&
1825 		   ctx->last_type) {
1826 		/* If user not specified the type, try parsing BTF bitfield. */
1827 		ret = parse_btf_bitfield(&code, ctx);
1828 		if (ret)
1829 			return ret;
1830 	}
1831 
1832 	/* Loop(Array) operation */
1833 	if (parg->count) {
1834 		if (scode->op != FETCH_OP_ST_MEM &&
1835 		    scode->op != FETCH_OP_ST_STRING &&
1836 		    scode->op != FETCH_OP_ST_USTRING) {
1837 			trace_probe_log_err(ctx->offset + type_offset, BAD_STRING);
1838 			return -EINVAL;
1839 		}
1840 		code++;
1841 		if (code->op != FETCH_OP_NOP) {
1842 			trace_probe_log_err(ctx->offset, TOO_MANY_OPS);
1843 			return -E2BIG;
1844 		}
1845 		code->op = FETCH_OP_LP_ARRAY;
1846 		code->param = parg->count;
1847 	}
1848 
1849 	/* Finalize the fetch_insn array. */
1850 	code++;
1851 	code->op = FETCH_OP_END;
1852 
1853 	return 0;
1854 }
1855 
1856 /* String length checking wrapper */
1857 static int traceprobe_parse_probe_arg_body(const char *argv, ssize_t *size,
1858 					   struct probe_arg *parg,
1859 					   struct traceprobe_parse_context *ctx)
1860 {
1861 	struct fetch_insn *code, *tmp = NULL;
1862 	char *type, *arg __free(kfree) = NULL;
1863 	int ret, len;
1864 
1865 	len = strlen(argv);
1866 	if (len > MAX_ARGSTR_LEN) {
1867 		trace_probe_log_err(ctx->offset, ARG_TOO_LONG);
1868 		return -E2BIG;
1869 	} else if (len == 0) {
1870 		trace_probe_log_err(ctx->offset, NO_ARG_BODY);
1871 		return -EINVAL;
1872 	}
1873 
1874 	arg = kstrdup(argv, GFP_KERNEL);
1875 	if (!arg)
1876 		return -ENOMEM;
1877 
1878 	parg->comm = kstrdup(arg, GFP_KERNEL);
1879 	if (!parg->comm)
1880 		return -ENOMEM;
1881 
1882 	type = parse_probe_arg_type(arg, parg, ctx);
1883 	if (IS_ERR(type))
1884 		return PTR_ERR(type);
1885 
1886 	code = tmp = kzalloc_objs(*code, FETCH_INSN_MAX);
1887 	if (!code)
1888 		return -ENOMEM;
1889 	code[FETCH_INSN_MAX - 1].op = FETCH_OP_END;
1890 
1891 	ctx->last_type = NULL;
1892 	ret = parse_probe_arg(arg, parg->type, &code, &code[FETCH_INSN_MAX - 1],
1893 			      ctx);
1894 	if (ret < 0)
1895 		goto fail;
1896 	/* nested_level must be 0 here, otherwise there is a bug. */
1897 	if (WARN_ON_ONCE(ctx->nested_level)) {
1898 		ret = -EINVAL;
1899 		goto fail;
1900 	}
1901 
1902 	/* Update storing type if BTF is available */
1903 	if (IS_ENABLED(CONFIG_PROBE_EVENTS_BTF_ARGS) &&
1904 	    ctx->last_type) {
1905 		if (!type) {
1906 			parg->type = find_fetch_type_from_btf_type(ctx);
1907 		} else if (strstr(type, "string")) {
1908 			ret = check_prepare_btf_string_fetch(type, &code, ctx);
1909 			if (ret)
1910 				goto fail;
1911 		}
1912 	}
1913 	parg->offset = *size;
1914 	*size += parg->type->size * (parg->count ?: 1);
1915 
1916 	if (*size > MAX_PROBE_EVENT_SIZE) {
1917 		ret = -E2BIG;
1918 		trace_probe_log_err(ctx->offset, EVENT_TOO_BIG);
1919 		goto fail;
1920 	}
1921 
1922 	if (parg->count) {
1923 		len = strlen(parg->type->fmttype) + 6;
1924 		parg->fmt = kmalloc(len, GFP_KERNEL);
1925 		if (!parg->fmt) {
1926 			ret = -ENOMEM;
1927 			goto fail;
1928 		}
1929 		snprintf(parg->fmt, len, "%s[%d]", parg->type->fmttype,
1930 			 parg->count);
1931 	}
1932 
1933 	ret = finalize_fetch_insn(code, parg, type, type ? type - arg : 0, ctx);
1934 	if (ret < 0)
1935 		goto fail;
1936 
1937 	for (; code < tmp + FETCH_INSN_MAX; code++)
1938 		if (code->op == FETCH_OP_END)
1939 			break;
1940 	/* Shrink down the code buffer */
1941 	parg->code = kzalloc_objs(*code, code - tmp + 1);
1942 	if (!parg->code)
1943 		ret = -ENOMEM;
1944 	else
1945 		memcpy(parg->code, tmp, sizeof(*code) * (code - tmp + 1));
1946 
1947 fail:
1948 	if (ret < 0) {
1949 		for (code = tmp; code < tmp + FETCH_INSN_MAX; code++)
1950 			if (code->op == FETCH_NOP_SYMBOL ||
1951 			    code->op == FETCH_OP_IMMSTR)
1952 				kfree(code->data);
1953 	}
1954 	kfree(tmp);
1955 
1956 	/* struct_btf should not be passed to other arguments */
1957 	clear_struct_btf(ctx);
1958 
1959 	return ret;
1960 }
1961 
1962 /* Return 1 if name is reserved or already used by another argument */
1963 static int traceprobe_conflict_field_name(const char *name,
1964 					  struct probe_arg *args, int narg)
1965 {
1966 	int i;
1967 
1968 	for (i = 0; i < ARRAY_SIZE(reserved_field_names); i++)
1969 		if (strcmp(reserved_field_names[i], name) == 0)
1970 			return 1;
1971 
1972 	for (i = 0; i < narg; i++)
1973 		if (strcmp(args[i].name, name) == 0)
1974 			return 1;
1975 
1976 	return 0;
1977 }
1978 
1979 static char *generate_probe_arg_name(const char *arg, int idx)
1980 {
1981 	char *name = NULL;
1982 	const char *end;
1983 
1984 	/*
1985 	 * If argument name is omitted, try arg as a name (BTF variable)
1986 	 * or "argN".
1987 	 */
1988 	if (IS_ENABLED(CONFIG_PROBE_EVENTS_BTF_ARGS)) {
1989 		end = strchr(arg, ':');
1990 		if (!end)
1991 			end = arg + strlen(arg);
1992 
1993 		name = kmemdup_nul(arg, end - arg, GFP_KERNEL);
1994 		if (!name || !is_good_name(name)) {
1995 			kfree(name);
1996 			name = NULL;
1997 		}
1998 	}
1999 
2000 	if (!name)
2001 		name = kasprintf(GFP_KERNEL, "arg%d", idx + 1);
2002 
2003 	return name;
2004 }
2005 
2006 int traceprobe_parse_probe_arg(struct trace_probe *tp, int i, const char *arg,
2007 			       struct traceprobe_parse_context *ctx)
2008 {
2009 	struct probe_arg *parg = &tp->args[i];
2010 	const char *body;
2011 
2012 	ctx->tp = tp;
2013 	body = strchr(arg, '=');
2014 	if (body) {
2015 		if (body - arg > MAX_ARG_NAME_LEN) {
2016 			trace_probe_log_err(0, ARG_NAME_TOO_LONG);
2017 			return -EINVAL;
2018 		} else if (body == arg) {
2019 			trace_probe_log_err(0, NO_ARG_NAME);
2020 			return -EINVAL;
2021 		}
2022 		parg->name = kmemdup_nul(arg, body - arg, GFP_KERNEL);
2023 		body++;
2024 	} else {
2025 		parg->name = generate_probe_arg_name(arg, i);
2026 		body = arg;
2027 	}
2028 	if (!parg->name)
2029 		return -ENOMEM;
2030 
2031 	if (!is_good_name(parg->name)) {
2032 		trace_probe_log_err(0, BAD_ARG_NAME);
2033 		return -EINVAL;
2034 	}
2035 	if (traceprobe_conflict_field_name(parg->name, tp->args, i)) {
2036 		trace_probe_log_err(0, USED_ARG_NAME);
2037 		return -EINVAL;
2038 	}
2039 	ctx->offset = body - arg;
2040 	/* Parse fetch argument */
2041 	return traceprobe_parse_probe_arg_body(body, &tp->size, parg, ctx);
2042 }
2043 
2044 void traceprobe_free_probe_arg(struct probe_arg *arg)
2045 {
2046 	struct fetch_insn *code = arg->code;
2047 
2048 	while (code && code->op != FETCH_OP_END) {
2049 		if (code->op == FETCH_NOP_SYMBOL ||
2050 		    code->op == FETCH_OP_IMMSTR)
2051 			kfree(code->data);
2052 		code++;
2053 	}
2054 	kfree(arg->code);
2055 	kfree(arg->name);
2056 	kfree(arg->comm);
2057 	kfree(arg->fmt);
2058 }
2059 
2060 static int argv_has_var_arg(int argc, const char *argv[], int *args_idx,
2061 			    struct traceprobe_parse_context *ctx)
2062 {
2063 	int i, found = 0;
2064 
2065 	for (i = 0; i < argc; i++)
2066 		if (str_has_prefix(argv[i], "$arg")) {
2067 			trace_probe_log_set_index(i + 2);
2068 
2069 			if (!tparg_is_function_entry(ctx->flags) &&
2070 			    !tparg_is_function_return(ctx->flags)) {
2071 				trace_probe_log_err(0, NOFENTRY_ARGS);
2072 				return -EINVAL;
2073 			}
2074 
2075 			if (isdigit(argv[i][4])) {
2076 				found = 1;
2077 				continue;
2078 			}
2079 
2080 			if (argv[i][4] != '*') {
2081 				trace_probe_log_err(0, BAD_VAR);
2082 				return -EINVAL;
2083 			}
2084 
2085 			if (*args_idx >= 0 && *args_idx < argc) {
2086 				trace_probe_log_err(0, DOUBLE_ARGS);
2087 				return -EINVAL;
2088 			}
2089 			found = 1;
2090 			*args_idx = i;
2091 		}
2092 
2093 	return found;
2094 }
2095 
2096 static int sprint_nth_btf_arg(int idx, const char *type,
2097 			      char *buf, int bufsize,
2098 			      struct traceprobe_parse_context *ctx)
2099 {
2100 	const char *name;
2101 	int ret;
2102 
2103 	if (idx >= ctx->nr_params) {
2104 		trace_probe_log_err(0, NO_BTFARG);
2105 		return -ENOENT;
2106 	}
2107 	name = btf_name_by_offset(ctx->btf, ctx->params[idx].name_off);
2108 	if (!name) {
2109 		trace_probe_log_err(0, NO_BTF_ENTRY);
2110 		return -ENOENT;
2111 	}
2112 	ret = snprintf(buf, bufsize, "%s%s", name, type);
2113 	if (ret >= bufsize) {
2114 		trace_probe_log_err(0, ARGS_2LONG);
2115 		return -E2BIG;
2116 	}
2117 	return ret;
2118 }
2119 
2120 /* Return new_argv which must be freed after use */
2121 const char **traceprobe_expand_meta_args(int argc, const char *argv[],
2122 					 int *new_argc, char *buf, int bufsize,
2123 					 struct traceprobe_parse_context *ctx)
2124 {
2125 	const struct btf_param *params = NULL;
2126 	int i, j, n, used, ret, args_idx = -1;
2127 	const char **new_argv __free(kfree) = NULL;
2128 
2129 	ret = argv_has_var_arg(argc, argv, &args_idx, ctx);
2130 	if (ret < 0)
2131 		return ERR_PTR(ret);
2132 
2133 	if (!ret) {
2134 		*new_argc = argc;
2135 		return NULL;
2136 	}
2137 
2138 	ret = query_btf_context(ctx);
2139 	if (ret < 0 || ctx->nr_params == 0) {
2140 		if (args_idx != -1) {
2141 			/* $arg* requires BTF info */
2142 			trace_probe_log_err(0, NOSUP_BTFARG);
2143 			return (const char **)params;
2144 		}
2145 		*new_argc = argc;
2146 		return NULL;
2147 	}
2148 
2149 	if (args_idx >= 0)
2150 		*new_argc = argc + ctx->nr_params - 1;
2151 	else
2152 		*new_argc = argc;
2153 
2154 	new_argv = kcalloc(*new_argc, sizeof(char *), GFP_KERNEL);
2155 	if (!new_argv)
2156 		return ERR_PTR(-ENOMEM);
2157 
2158 	used = 0;
2159 	for (i = 0, j = 0; i < argc; i++) {
2160 		trace_probe_log_set_index(i + 2);
2161 		if (i == args_idx) {
2162 			for (n = 0; n < ctx->nr_params; n++) {
2163 				ret = sprint_nth_btf_arg(n, "", buf + used,
2164 							 bufsize - used, ctx);
2165 				if (ret < 0)
2166 					return ERR_PTR(ret);
2167 
2168 				new_argv[j++] = buf + used;
2169 				used += ret + 1;
2170 			}
2171 			continue;
2172 		}
2173 
2174 		if (str_has_prefix(argv[i], "$arg")) {
2175 			char *type = NULL;
2176 
2177 			n = simple_strtoul(argv[i] + 4, &type, 10);
2178 			if (type && !(*type == ':' || *type == '\0')) {
2179 				trace_probe_log_err(0, BAD_VAR);
2180 				return ERR_PTR(-ENOENT);
2181 			}
2182 			/* Note: $argN starts from $arg1 */
2183 			ret = sprint_nth_btf_arg(n - 1, type, buf + used,
2184 						 bufsize - used, ctx);
2185 			if (ret < 0)
2186 				return ERR_PTR(ret);
2187 			new_argv[j++] = buf + used;
2188 			used += ret + 1;
2189 		} else
2190 			new_argv[j++] = argv[i];
2191 	}
2192 
2193 	return_ptr(new_argv);
2194 }
2195 
2196 /* @buf: *buf must be equal to NULL. Caller must to free *buf */
2197 int traceprobe_expand_dentry_args(int argc, const char *argv[], char **buf)
2198 {
2199 	int i, used, ret;
2200 	const int bufsize = MAX_DENTRY_ARGS_LEN;
2201 	char *tmpbuf __free(kfree) = NULL;
2202 
2203 	if (*buf)
2204 		return -EINVAL;
2205 
2206 	used = 0;
2207 	for (i = 0; i < argc; i++) {
2208 		char *tmp __free(kfree) = NULL;
2209 		char *equal;
2210 		size_t arg_len;
2211 
2212 		if (!glob_match("*:%p[dD]", argv[i]))
2213 			continue;
2214 
2215 		if (!tmpbuf) {
2216 			tmpbuf = kmalloc(bufsize, GFP_KERNEL);
2217 			if (!tmpbuf)
2218 				return -ENOMEM;
2219 		}
2220 
2221 		tmp = kstrdup(argv[i], GFP_KERNEL);
2222 		if (!tmp)
2223 			return -ENOMEM;
2224 
2225 		equal = strchr(tmp, '=');
2226 		if (equal)
2227 			*equal = '\0';
2228 		arg_len = strlen(argv[i]);
2229 		tmp[arg_len - 4] = '\0';
2230 		if (argv[i][arg_len - 1] == 'd')
2231 			ret = snprintf(tmpbuf + used, bufsize - used,
2232 				       "%s%s+0x0(+0x%zx(%s)):string",
2233 				       equal ? tmp : "", equal ? "=" : "",
2234 				       offsetof(struct dentry, d_name.name),
2235 				       equal ? equal + 1 : tmp);
2236 		else
2237 			ret = snprintf(tmpbuf + used, bufsize - used,
2238 				       "%s%s+0x0(+0x%zx(+0x%zx(%s))):string",
2239 				       equal ? tmp : "", equal ? "=" : "",
2240 				       offsetof(struct dentry, d_name.name),
2241 				       offsetof(struct file, f_path.dentry),
2242 				       equal ? equal + 1 : tmp);
2243 
2244 		if (ret >= bufsize - used)
2245 			return -ENOMEM;
2246 		argv[i] = tmpbuf + used;
2247 		used += ret + 1;
2248 	}
2249 
2250 	*buf = no_free_ptr(tmpbuf);
2251 	return 0;
2252 }
2253 
2254 void traceprobe_finish_parse(struct traceprobe_parse_context *ctx)
2255 {
2256 	clear_btf_context(ctx);
2257 }
2258 
2259 int traceprobe_update_arg(struct probe_arg *arg)
2260 {
2261 	struct fetch_insn *code = arg->code;
2262 	long offset;
2263 	char *tmp;
2264 	char c;
2265 	int ret = 0;
2266 
2267 	while (code && code->op != FETCH_OP_END) {
2268 		if (code->op == FETCH_NOP_SYMBOL) {
2269 			if (code[1].op != FETCH_OP_IMM)
2270 				return -EINVAL;
2271 
2272 			tmp = strpbrk(code->data, "+-");
2273 			if (tmp)
2274 				c = *tmp;
2275 			ret = traceprobe_split_symbol_offset(code->data,
2276 							     &offset);
2277 			if (ret)
2278 				return ret;
2279 
2280 			code[1].immediate =
2281 				(unsigned long)kallsyms_lookup_name(code->data);
2282 			if (tmp)
2283 				*tmp = c;
2284 			if (!code[1].immediate)
2285 				return -ENOENT;
2286 			code[1].immediate += offset;
2287 		}
2288 		code++;
2289 	}
2290 	return 0;
2291 }
2292 
2293 /* When len=0, we just calculate the needed length */
2294 #define LEN_OR_ZERO (len ? len - pos : 0)
2295 static int __set_print_fmt(struct trace_probe *tp, char *buf, int len,
2296 			   enum probe_print_type ptype)
2297 {
2298 	struct probe_arg *parg;
2299 	int i, j;
2300 	int pos = 0;
2301 	const char *fmt, *arg;
2302 
2303 	switch (ptype) {
2304 	case PROBE_PRINT_NORMAL:
2305 		fmt = "(%lx)";
2306 		arg = ", REC->" FIELD_STRING_IP;
2307 		break;
2308 	case PROBE_PRINT_RETURN:
2309 		fmt = "(%lx <- %lx)";
2310 		arg = ", REC->" FIELD_STRING_FUNC ", REC->" FIELD_STRING_RETIP;
2311 		break;
2312 	case PROBE_PRINT_EVENT:
2313 		fmt = "";
2314 		arg = "";
2315 		break;
2316 	default:
2317 		WARN_ON_ONCE(1);
2318 		return 0;
2319 	}
2320 
2321 	pos += snprintf(buf + pos, LEN_OR_ZERO, "\"%s", fmt);
2322 
2323 	for (i = 0; i < tp->nr_args; i++) {
2324 		parg = tp->args + i;
2325 		pos += snprintf(buf + pos, LEN_OR_ZERO, " %s=", parg->name);
2326 		if (parg->count) {
2327 			pos += snprintf(buf + pos, LEN_OR_ZERO, "{%s",
2328 					parg->type->fmt);
2329 			for (j = 1; j < parg->count; j++)
2330 				pos += snprintf(buf + pos, LEN_OR_ZERO, ",%s",
2331 						parg->type->fmt);
2332 			pos += snprintf(buf + pos, LEN_OR_ZERO, "}");
2333 		} else
2334 			pos += snprintf(buf + pos, LEN_OR_ZERO, "%s",
2335 					parg->type->fmt);
2336 	}
2337 
2338 	pos += snprintf(buf + pos, LEN_OR_ZERO, "\"%s", arg);
2339 
2340 	for (i = 0; i < tp->nr_args; i++) {
2341 		parg = tp->args + i;
2342 		if (parg->count) {
2343 			if (parg->type->is_string)
2344 				fmt = ", __get_str(%s[%d])";
2345 			else
2346 				fmt = ", REC->%s[%d]";
2347 			for (j = 0; j < parg->count; j++)
2348 				pos += snprintf(buf + pos, LEN_OR_ZERO,
2349 						fmt, parg->name, j);
2350 		} else {
2351 			if (parg->type->is_string)
2352 				fmt = ", __get_str(%s)";
2353 			else
2354 				fmt = ", REC->%s";
2355 			pos += snprintf(buf + pos, LEN_OR_ZERO,
2356 					fmt, parg->name);
2357 		}
2358 	}
2359 
2360 	/* return the length of print_fmt */
2361 	return pos;
2362 }
2363 #undef LEN_OR_ZERO
2364 
2365 int traceprobe_set_print_fmt(struct trace_probe *tp, enum probe_print_type ptype)
2366 {
2367 	struct trace_event_call *call = trace_probe_event_call(tp);
2368 	int len;
2369 	char *print_fmt;
2370 
2371 	/* First: called with 0 length to calculate the needed length */
2372 	len = __set_print_fmt(tp, NULL, 0, ptype);
2373 	print_fmt = kmalloc(len + 1, GFP_KERNEL);
2374 	if (!print_fmt)
2375 		return -ENOMEM;
2376 
2377 	/* Second: actually write the @print_fmt */
2378 	__set_print_fmt(tp, print_fmt, len + 1, ptype);
2379 	call->print_fmt = print_fmt;
2380 
2381 	return 0;
2382 }
2383 
2384 int traceprobe_define_arg_fields(struct trace_event_call *event_call,
2385 				 size_t offset, struct trace_probe *tp)
2386 {
2387 	int ret, i;
2388 
2389 	/* Set argument names as fields */
2390 	for (i = 0; i < tp->nr_args; i++) {
2391 		struct probe_arg *parg = &tp->args[i];
2392 		const char *fmt = parg->type->fmttype;
2393 		int size = parg->type->size;
2394 
2395 		if (parg->fmt)
2396 			fmt = parg->fmt;
2397 		if (parg->count)
2398 			size *= parg->count;
2399 		ret = trace_define_field(event_call, fmt, parg->name,
2400 					 offset + parg->offset, size,
2401 					 parg->type->is_signed,
2402 					 FILTER_OTHER);
2403 		if (ret)
2404 			return ret;
2405 	}
2406 	return 0;
2407 }
2408 
2409 static void trace_probe_event_free(struct trace_probe_event *tpe)
2410 {
2411 	kfree(tpe->class.system);
2412 	kfree(tpe->call.name);
2413 	kfree(tpe->call.print_fmt);
2414 	kfree(tpe);
2415 }
2416 
2417 int trace_probe_append(struct trace_probe *tp, struct trace_probe *to)
2418 {
2419 	if (trace_probe_has_sibling(tp))
2420 		return -EBUSY;
2421 
2422 	list_del_init(&tp->list);
2423 	trace_probe_event_free(tp->event);
2424 
2425 	tp->event = to->event;
2426 	list_add_tail(&tp->list, trace_probe_probe_list(to));
2427 
2428 	return 0;
2429 }
2430 
2431 void trace_probe_unlink(struct trace_probe *tp)
2432 {
2433 	list_del_init(&tp->list);
2434 	if (list_empty(trace_probe_probe_list(tp)))
2435 		trace_probe_event_free(tp->event);
2436 	tp->event = NULL;
2437 }
2438 
2439 void trace_probe_cleanup(struct trace_probe *tp)
2440 {
2441 	int i;
2442 
2443 	for (i = 0; i < tp->nr_args; i++)
2444 		traceprobe_free_probe_arg(&tp->args[i]);
2445 
2446 	if (tp->entry_arg) {
2447 		kfree(tp->entry_arg);
2448 		tp->entry_arg = NULL;
2449 	}
2450 
2451 	if (tp->event)
2452 		trace_probe_unlink(tp);
2453 }
2454 
2455 int trace_probe_init(struct trace_probe *tp, const char *event,
2456 		     const char *group, bool alloc_filter, int nargs)
2457 {
2458 	struct trace_event_call *call;
2459 	size_t size = sizeof(struct trace_probe_event);
2460 	int ret = 0;
2461 
2462 	if (!event || !group)
2463 		return -EINVAL;
2464 
2465 	if (alloc_filter)
2466 		size += sizeof(struct trace_uprobe_filter);
2467 
2468 	tp->event = kzalloc(size, GFP_KERNEL);
2469 	if (!tp->event)
2470 		return -ENOMEM;
2471 
2472 	INIT_LIST_HEAD(&tp->event->files);
2473 	INIT_LIST_HEAD(&tp->event->class.fields);
2474 	INIT_LIST_HEAD(&tp->event->probes);
2475 	INIT_LIST_HEAD(&tp->list);
2476 	list_add(&tp->list, &tp->event->probes);
2477 
2478 	call = trace_probe_event_call(tp);
2479 	call->class = &tp->event->class;
2480 	call->name = kstrdup(event, GFP_KERNEL);
2481 	if (!call->name) {
2482 		ret = -ENOMEM;
2483 		goto error;
2484 	}
2485 
2486 	tp->event->class.system = kstrdup(group, GFP_KERNEL);
2487 	if (!tp->event->class.system) {
2488 		ret = -ENOMEM;
2489 		goto error;
2490 	}
2491 
2492 	tp->nr_args = nargs;
2493 	/* Make sure pointers in args[] are NULL */
2494 	if (nargs)
2495 		memset(tp->args, 0, sizeof(tp->args[0]) * nargs);
2496 
2497 	return 0;
2498 
2499 error:
2500 	trace_probe_cleanup(tp);
2501 	return ret;
2502 }
2503 
2504 static struct trace_event_call *
2505 find_trace_event_call(const char *system, const char *event_name)
2506 {
2507 	struct trace_event_call *tp_event;
2508 	const char *name;
2509 
2510 	list_for_each_entry(tp_event, &ftrace_events, list) {
2511 		if (!tp_event->class->system ||
2512 		    strcmp(system, tp_event->class->system))
2513 			continue;
2514 		name = trace_event_name(tp_event);
2515 		if (!name || strcmp(event_name, name))
2516 			continue;
2517 		return tp_event;
2518 	}
2519 
2520 	return NULL;
2521 }
2522 
2523 int trace_probe_register_event_call(struct trace_probe *tp)
2524 {
2525 	struct trace_event_call *call = trace_probe_event_call(tp);
2526 	int ret;
2527 
2528 	lockdep_assert_held(&event_mutex);
2529 
2530 	if (find_trace_event_call(trace_probe_group_name(tp),
2531 				  trace_probe_name(tp)))
2532 		return -EEXIST;
2533 
2534 	ret = register_trace_event(&call->event);
2535 	if (!ret)
2536 		return -ENODEV;
2537 
2538 	ret = trace_add_event_call(call);
2539 	if (ret)
2540 		unregister_trace_event(&call->event);
2541 
2542 	return ret;
2543 }
2544 
2545 int trace_probe_add_file(struct trace_probe *tp, struct trace_event_file *file)
2546 {
2547 	struct event_file_link *link;
2548 
2549 	link = kmalloc_obj(*link);
2550 	if (!link)
2551 		return -ENOMEM;
2552 
2553 	link->file = file;
2554 	INIT_LIST_HEAD(&link->list);
2555 	list_add_tail_rcu(&link->list, &tp->event->files);
2556 	trace_probe_set_flag(tp, TP_FLAG_TRACE);
2557 	return 0;
2558 }
2559 
2560 struct event_file_link *trace_probe_get_file_link(struct trace_probe *tp,
2561 						  struct trace_event_file *file)
2562 {
2563 	struct event_file_link *link;
2564 
2565 	trace_probe_for_each_link(link, tp) {
2566 		if (link->file == file)
2567 			return link;
2568 	}
2569 
2570 	return NULL;
2571 }
2572 
2573 int trace_probe_remove_file(struct trace_probe *tp,
2574 			    struct trace_event_file *file)
2575 {
2576 	struct event_file_link *link;
2577 
2578 	link = trace_probe_get_file_link(tp, file);
2579 	if (!link)
2580 		return -ENOENT;
2581 
2582 	list_del_rcu(&link->list);
2583 	kvfree_rcu_mightsleep(link);
2584 
2585 	if (list_empty(&tp->event->files))
2586 		trace_probe_clear_flag(tp, TP_FLAG_TRACE);
2587 
2588 	return 0;
2589 }
2590 
2591 /*
2592  * Return the smallest index of different type argument (start from 1).
2593  * If all argument types and name are same, return 0.
2594  */
2595 int trace_probe_compare_arg_type(struct trace_probe *a, struct trace_probe *b)
2596 {
2597 	int i;
2598 
2599 	/* In case of more arguments */
2600 	if (a->nr_args < b->nr_args)
2601 		return a->nr_args + 1;
2602 	if (a->nr_args > b->nr_args)
2603 		return b->nr_args + 1;
2604 
2605 	for (i = 0; i < a->nr_args; i++) {
2606 		if ((b->nr_args <= i) ||
2607 		    ((a->args[i].type != b->args[i].type) ||
2608 		     (a->args[i].count != b->args[i].count) ||
2609 		     strcmp(a->args[i].name, b->args[i].name)))
2610 			return i + 1;
2611 	}
2612 
2613 	return 0;
2614 }
2615 
2616 bool trace_probe_match_command_args(struct trace_probe *tp,
2617 				    int argc, const char **argv)
2618 {
2619 	char buf[MAX_ARGSTR_LEN + 1];
2620 	int i;
2621 
2622 	if (tp->nr_args < argc)
2623 		return false;
2624 
2625 	for (i = 0; i < argc; i++) {
2626 		snprintf(buf, sizeof(buf), "%s=%s",
2627 			 tp->args[i].name, tp->args[i].comm);
2628 		if (strcmp(buf, argv[i]))
2629 			return false;
2630 	}
2631 	return true;
2632 }
2633 
2634 int trace_probe_create(const char *raw_command, int (*createfn)(int, const char **))
2635 {
2636 	int argc = 0, ret = 0;
2637 	char **argv;
2638 
2639 	argv = argv_split(GFP_KERNEL, raw_command, &argc);
2640 	if (!argv)
2641 		return -ENOMEM;
2642 
2643 	if (argc)
2644 		ret = createfn(argc, (const char **)argv);
2645 
2646 	argv_free(argv);
2647 
2648 	return ret;
2649 }
2650 
2651 int trace_probe_print_args(struct trace_seq *s, struct probe_arg *args, int nr_args,
2652 		 u8 *data, void *field)
2653 {
2654 	void *p;
2655 	int i, j;
2656 
2657 	for (i = 0; i < nr_args; i++) {
2658 		struct probe_arg *a = args + i;
2659 
2660 		trace_seq_printf(s, " %s=", a->name);
2661 		if (likely(!a->count)) {
2662 			if (!a->type->print(s, data + a->offset, field))
2663 				return -ENOMEM;
2664 			continue;
2665 		}
2666 		trace_seq_putc(s, '{');
2667 		p = data + a->offset;
2668 		for (j = 0; j < a->count; j++) {
2669 			if (!a->type->print(s, p, field))
2670 				return -ENOMEM;
2671 			trace_seq_putc(s, j == a->count - 1 ? '}' : ',');
2672 			p += a->type->size;
2673 		}
2674 	}
2675 	return 0;
2676 }
2677 
2678 #ifdef CONFIG_PROBE_EVENTS_DUMP_FETCHARG
2679 
2680 struct fetch_op_decode {
2681 	const char *name;
2682 	void (*decode)(struct seq_file *m, struct fetch_insn *insn);
2683 };
2684 
2685 static const struct fetch_op_decode fetch_op_decode[];
2686 
2687 static void fetcharg_decode_none(struct seq_file *m, struct fetch_insn *insn)
2688 {
2689 	seq_puts(m, fetch_op_decode[insn->op].name);
2690 }
2691 
2692 static void fetcharg_decode_param(struct seq_file *m, struct fetch_insn *insn)
2693 {
2694 	seq_printf(m, "%s(%u)", fetch_op_decode[insn->op].name, insn->param);
2695 }
2696 
2697 static void fetcharg_decode_imm(struct seq_file *m, struct fetch_insn *insn)
2698 {
2699 	seq_printf(m, "%s(0x%lx)", fetch_op_decode[insn->op].name, insn->immediate);
2700 }
2701 
2702 static void fetcharg_decode_string(struct seq_file *m, struct fetch_insn *insn)
2703 {
2704 	seq_printf(m, "%s(%s)", fetch_op_decode[insn->op].name, (char *)insn->data);
2705 }
2706 
2707 static void fetcharg_decode_symbol(struct seq_file *m, struct fetch_insn *insn)
2708 {
2709 	seq_printf(m, "%s(%s)", fetch_op_decode[insn->op].name, (char *)insn->data);
2710 }
2711 
2712 static void fetcharg_decode_offset(struct seq_file *m, struct fetch_insn *insn)
2713 {
2714 	seq_printf(m, "%s(offset=%d)", fetch_op_decode[insn->op].name, insn->offset);
2715 }
2716 
2717 static void fetcharg_decode_store(struct seq_file *m, struct fetch_insn *insn)
2718 {
2719 	if (insn->op == FETCH_OP_ST_RAW)
2720 		seq_printf(m, "%s(size=%u)", fetch_op_decode[insn->op].name, insn->size);
2721 	else
2722 		seq_printf(m, "%s(offset=%d,size=%u)", fetch_op_decode[insn->op].name,
2723 			  insn->offset, insn->size);
2724 }
2725 
2726 static void fetcharg_decode_bf(struct seq_file *m, struct fetch_insn *insn)
2727 {
2728 	seq_printf(m, "%s(basesize=%u,lshift=%u,rshift=%u)",
2729 		   fetch_op_decode[insn->op].name, insn->basesize, insn->lshift, insn->rshift);
2730 }
2731 
2732 static void fetcharg_decode_tp_arg(struct seq_file *m, struct fetch_insn *insn)
2733 {
2734 	struct ftrace_event_field *field = insn->data;
2735 
2736 	seq_printf(m, "%s(%s)", fetch_op_decode[insn->op].name, field->name);
2737 }
2738 
2739 #define FETCH_OP(opname, decode_fn) \
2740 	[FETCH_OP_##opname] = { .name = #opname, .decode = fetcharg_decode_##decode_fn }
2741 
2742 static const struct fetch_op_decode fetch_op_decode[] = FETCH_OP_LIST;
2743 #undef FETCH_OP
2744 
2745 static void trace_probe_dump_arg(struct seq_file *m, struct probe_arg *parg)
2746 {
2747 	int i;
2748 
2749 	seq_printf(m, "#  %s: ", parg->name);
2750 	for (i = 0; i < FETCH_INSN_MAX; i++) {
2751 		struct fetch_insn *insn = parg->code + i;
2752 
2753 		if (insn->op >= ARRAY_SIZE(fetch_op_decode) || !fetch_op_decode[insn->op].decode)
2754 			seq_printf(m, "unknown(%d)", insn->op);
2755 		else
2756 			fetch_op_decode[insn->op].decode(m, insn);
2757 
2758 		if (insn->op == FETCH_OP_END)
2759 			break;
2760 		seq_puts(m, " -> ");
2761 	}
2762 	seq_putc(m, '\n');
2763 }
2764 
2765 void trace_probe_dump_args(struct seq_file *m, struct trace_probe *tp)
2766 {
2767 	int i;
2768 
2769 	for (i = 0; i < tp->nr_args; i++)
2770 		trace_probe_dump_arg(m, &tp->args[i]);
2771 }
2772 #endif /* CONFIG_PROBE_EVENTS_DUMP_FETCHARG */
2773