xref: /linux/kernel/trace/trace_probe.c (revision 69efd863a78584b9416ed6be0e1e7349124b4a00)
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 	if (strcmp(arg, "comm") == 0 || strcmp(arg, "COMM") == 0) {
346 		code->op = FETCH_OP_COMM;
347 		return 0;
348 	}
349 	return -EINVAL;
350 }
351 
352 #ifdef CONFIG_PROBE_EVENTS_BTF_ARGS
353 
354 static u32 btf_type_int(const struct btf_type *t)
355 {
356 	return *(u32 *)(t + 1);
357 }
358 
359 static bool btf_type_is_char_ptr(struct btf *btf, const struct btf_type *type)
360 {
361 	const struct btf_type *real_type;
362 	u32 intdata;
363 	s32 tid;
364 
365 	real_type = btf_type_skip_modifiers(btf, type->type, &tid);
366 	if (!real_type)
367 		return false;
368 
369 	if (BTF_INFO_KIND(real_type->info) != BTF_KIND_INT)
370 		return false;
371 
372 	intdata = btf_type_int(real_type);
373 	return !(BTF_INT_ENCODING(intdata) & BTF_INT_SIGNED)
374 		&& BTF_INT_BITS(intdata) == 8;
375 }
376 
377 static bool btf_type_is_char_array(struct btf *btf, const struct btf_type *type)
378 {
379 	const struct btf_type *real_type;
380 	const struct btf_array *array;
381 	u32 intdata;
382 	s32 tid;
383 
384 	if (BTF_INFO_KIND(type->info) != BTF_KIND_ARRAY)
385 		return false;
386 
387 	array = (const struct btf_array *)(type + 1);
388 
389 	real_type = btf_type_skip_modifiers(btf, array->type, &tid);
390 
391 	intdata = btf_type_int(real_type);
392 	return !(BTF_INT_ENCODING(intdata) & BTF_INT_SIGNED)
393 		&& BTF_INT_BITS(intdata) == 8;
394 }
395 
396 static struct btf *ctx_btf(struct traceprobe_parse_context *ctx)
397 {
398 	return ctx->struct_btf ? : ctx->btf;
399 }
400 
401 static int check_prepare_btf_string_fetch(char *typename,
402 				struct fetch_insn **pcode,
403 				struct traceprobe_parse_context *ctx)
404 {
405 	struct btf *btf = ctx_btf(ctx);
406 
407 	if (!btf || !ctx->last_type)
408 		return 0;
409 
410 	/* char [] does not need any change. */
411 	if (btf_type_is_char_array(btf, ctx->last_type))
412 		return 0;
413 
414 	/* char * requires dereference the pointer. */
415 	if (btf_type_is_char_ptr(btf, ctx->last_type)) {
416 		struct fetch_insn *code = *pcode + 1;
417 
418 		if (code->op == FETCH_OP_END) {
419 			trace_probe_log_err(ctx->offset, TOO_MANY_OPS);
420 			return -E2BIG;
421 		}
422 		if (typename[0] == 'u')
423 			code->op = FETCH_OP_UDEREF;
424 		else
425 			code->op = FETCH_OP_DEREF;
426 		code->offset = 0;
427 		*pcode = code;
428 		return 0;
429 	}
430 	/* Other types are not available for string */
431 	trace_probe_log_err(ctx->offset, BAD_TYPE4STR);
432 	return -EINVAL;
433 }
434 
435 static const char *fetch_type_from_btf_type(struct btf *btf,
436 					const struct btf_type *type,
437 					struct traceprobe_parse_context *ctx)
438 {
439 	u32 intdata;
440 
441 	/* TODO: const char * could be converted as a string */
442 	switch (BTF_INFO_KIND(type->info)) {
443 	case BTF_KIND_ENUM:
444 		/* enum is "int", so convert to "s32" */
445 		return "s32";
446 	case BTF_KIND_ENUM64:
447 		return "s64";
448 	case BTF_KIND_PTR:
449 		/* pointer will be converted to "x??" */
450 		if (IS_ENABLED(CONFIG_64BIT))
451 			return "x64";
452 		else
453 			return "x32";
454 	case BTF_KIND_INT:
455 		intdata = btf_type_int(type);
456 		if (BTF_INT_ENCODING(intdata) & BTF_INT_SIGNED) {
457 			switch (BTF_INT_BITS(intdata)) {
458 			case 8:
459 				return "s8";
460 			case 16:
461 				return "s16";
462 			case 32:
463 				return "s32";
464 			case 64:
465 				return "s64";
466 			}
467 		} else {	/* unsigned */
468 			switch (BTF_INT_BITS(intdata)) {
469 			case 8:
470 				return "u8";
471 			case 16:
472 				return "u16";
473 			case 32:
474 				return "u32";
475 			case 64:
476 				return "u64";
477 			}
478 			/* bitfield, size is encoded in the type */
479 			ctx->last_bitsize = BTF_INT_BITS(intdata);
480 			ctx->last_bitoffs += BTF_INT_OFFSET(intdata);
481 			return "u64";
482 		}
483 	}
484 	/* TODO: support other types */
485 
486 	return NULL;
487 }
488 
489 static int query_btf_context(struct traceprobe_parse_context *ctx)
490 {
491 	const struct btf_param *param;
492 	const struct btf_type *type;
493 	struct btf *btf;
494 	s32 nr;
495 
496 	if (ctx->btf)
497 		return 0;
498 
499 	if (!ctx->funcname)
500 		return -EINVAL;
501 
502 	type = btf_find_func_proto(ctx->funcname, &btf);
503 	if (!type)
504 		return -ENOENT;
505 
506 	ctx->btf = btf;
507 	ctx->proto = type;
508 
509 	/* ctx->params is optional, since func(void) will not have params. */
510 	nr = 0;
511 	param = btf_get_func_param(type, &nr);
512 	if (!IS_ERR_OR_NULL(param)) {
513 		/* Hide the first 'data' argument of tracepoint */
514 		if (ctx->flags & TPARG_FL_TPOINT) {
515 			nr--;
516 			param++;
517 		}
518 	}
519 
520 	if (nr > 0) {
521 		ctx->nr_params = nr;
522 		ctx->params = param;
523 	} else {
524 		ctx->nr_params = 0;
525 		ctx->params = NULL;
526 	}
527 
528 	return 0;
529 }
530 
531 static void clear_struct_btf(struct traceprobe_parse_context *ctx)
532 {
533 	if (ctx->struct_btf) {
534 		btf_put(ctx->struct_btf);
535 		ctx->struct_btf = NULL;
536 		ctx->last_struct = NULL;
537 	}
538 }
539 
540 static void clear_btf_context(struct traceprobe_parse_context *ctx)
541 {
542 	if (ctx->btf) {
543 		btf_put(ctx->btf);
544 		ctx->btf = NULL;
545 		ctx->proto = NULL;
546 		ctx->params = NULL;
547 		ctx->nr_params = 0;
548 	}
549 }
550 
551 /* Return 1 if the field separator is arrow operator ('->') */
552 static int split_next_field(char *varname, char **next_field,
553 			    struct traceprobe_parse_context *ctx)
554 {
555 	char *field;
556 	int ret = 0;
557 
558 	field = strpbrk(varname, ".-");
559 	if (field) {
560 		if (field[0] == '-' && field[1] == '>') {
561 			field[0] = '\0';
562 			field += 2;
563 			ret = 1;
564 		} else if (field[0] == '.') {
565 			field[0] = '\0';
566 			field += 1;
567 		} else {
568 			trace_probe_log_err(ctx->offset + field - varname, BAD_HYPHEN);
569 			return -EINVAL;
570 		}
571 		*next_field = field;
572 	}
573 
574 	return ret;
575 }
576 
577 /*
578  * Parse the field of data structure. The @type must be a pointer type
579  * pointing the target data structure type.
580  */
581 static int parse_btf_field(char *fieldname, const struct btf_type *type,
582 			   struct fetch_insn **pcode, struct fetch_insn *end,
583 			   struct traceprobe_parse_context *ctx)
584 {
585 	struct fetch_insn *code = *pcode;
586 	const struct btf_member *field;
587 	u32 bitoffs, anon_offs;
588 	bool is_struct = ctx->struct_btf != NULL;
589 	struct btf *btf = ctx_btf(ctx);
590 	char *next;
591 	int is_ptr;
592 	s32 tid;
593 
594 	do {
595 		if (!is_struct) {
596 			/* Outer loop for solving arrow operator ('->') */
597 			if (BTF_INFO_KIND(type->info) != BTF_KIND_PTR) {
598 				trace_probe_log_err(ctx->offset, NO_PTR_STRCT);
599 				return -EINVAL;
600 			}
601 
602 			/* Convert a struct pointer type to a struct type */
603 			type = btf_type_skip_modifiers(btf, type->type, &tid);
604 			if (!type) {
605 				trace_probe_log_err(ctx->offset, BAD_BTF_TID);
606 				return -EINVAL;
607 			}
608 		}
609 		/* Only the first type can skip being a pointer */
610 		is_struct = false;
611 
612 		bitoffs = 0;
613 		do {
614 			/* Inner loop for solving dot operator ('.') */
615 			next = NULL;
616 			is_ptr = split_next_field(fieldname, &next, ctx);
617 			if (is_ptr < 0)
618 				return is_ptr;
619 
620 			anon_offs = 0;
621 			field = btf_find_struct_member(btf, type, fieldname,
622 						       &anon_offs);
623 			if (IS_ERR(field)) {
624 				trace_probe_log_err(ctx->offset, BAD_BTF_TID);
625 				return PTR_ERR(field);
626 			}
627 			if (!field) {
628 				trace_probe_log_err(ctx->offset, NO_BTF_FIELD);
629 				return -ENOENT;
630 			}
631 			/* Add anonymous structure/union offset */
632 			bitoffs += anon_offs;
633 
634 			/* Accumulate the bit-offsets of the dot-connected fields */
635 			if (btf_type_kflag(type)) {
636 				bitoffs += BTF_MEMBER_BIT_OFFSET(field->offset);
637 				ctx->last_bitsize = BTF_MEMBER_BITFIELD_SIZE(field->offset);
638 			} else {
639 				bitoffs += field->offset;
640 				ctx->last_bitsize = 0;
641 			}
642 
643 			type = btf_type_skip_modifiers(btf, field->type, &tid);
644 			if (!type) {
645 				trace_probe_log_err(ctx->offset, BAD_BTF_TID);
646 				return -EINVAL;
647 			}
648 
649 			ctx->offset += next - fieldname;
650 			fieldname = next;
651 		} while (!is_ptr && fieldname);
652 
653 		if (++code == end) {
654 			trace_probe_log_err(ctx->offset, TOO_MANY_OPS);
655 			return -EINVAL;
656 		}
657 		code->op = FETCH_OP_DEREF;	/* TODO: user deref support */
658 		code->offset = bitoffs / 8;
659 		*pcode = code;
660 
661 		ctx->last_bitoffs = bitoffs % 8;
662 		ctx->last_type = type;
663 	} while (fieldname);
664 
665 	return 0;
666 }
667 
668 static int __store_entry_arg(struct trace_probe *tp, int argnum);
669 
670 static int parse_btf_arg(char *varname,
671 			 struct fetch_insn **pcode, struct fetch_insn *end,
672 			 struct traceprobe_parse_context *ctx)
673 {
674 	struct fetch_insn *code = *pcode;
675 	const struct btf_param *params;
676 	const struct btf_type *type;
677 	char *field = NULL;
678 	int i, is_ptr, ret;
679 	u32 tid;
680 
681 	if (WARN_ON_ONCE(!ctx->funcname && !(ctx->flags & TPARG_FL_TEVENT)))
682 		return -EINVAL;
683 
684 	is_ptr = split_next_field(varname, &field, ctx);
685 	if (is_ptr < 0)
686 		return is_ptr;
687 	if (!is_ptr && field) {
688 		/* dot-connected field on an argument is not supported. */
689 		trace_probe_log_err(ctx->offset + field - varname,
690 				    NOSUP_DAT_ARG);
691 		return -EOPNOTSUPP;
692 	}
693 
694 	if (ctx->flags & TPARG_FL_TEVENT) {
695 		ret = parse_trace_event(varname, code, ctx);
696 		if (ret < 0) {
697 			trace_probe_log_err(ctx->offset, BAD_ATTACH_ARG);
698 			return ret;
699 		}
700 		/* TEVENT is only here via a typecast */
701 		if (WARN_ON_ONCE(ctx->struct_btf == NULL))
702 			return -EINVAL;
703 		type = ctx->last_struct;
704 		goto found_type;
705 	}
706 
707 	if (ctx->flags & TPARG_FL_RETURN && !strcmp(varname, "$retval")) {
708 		code->op = FETCH_OP_RETVAL;
709 		/* Check whether the function return type is not void */
710 		if (query_btf_context(ctx) == 0) {
711 			if (ctx->proto->type == 0) {
712 				trace_probe_log_err(ctx->offset, NO_RETVAL);
713 				return -ENOENT;
714 			}
715 			tid = ctx->proto->type;
716 			goto found;
717 		}
718 		if (field) {
719 			trace_probe_log_err(ctx->offset + field - varname,
720 					    NO_BTF_ENTRY);
721 			return -ENOENT;
722 		}
723 		return 0;
724 	}
725 
726 	if (!ctx->btf) {
727 		ret = query_btf_context(ctx);
728 		if (ret < 0 || ctx->nr_params == 0) {
729 			trace_probe_log_err(ctx->offset, NO_BTF_ENTRY);
730 			return -ENOENT;
731 		}
732 	}
733 	params = ctx->params;
734 
735 	for (i = 0; i < ctx->nr_params; i++) {
736 		const char *name = btf_name_by_offset(ctx->btf, params[i].name_off);
737 
738 		if (name && !strcmp(name, varname)) {
739 			if (tparg_is_function_entry(ctx->flags)) {
740 				code->op = FETCH_OP_ARG;
741 				if (ctx->flags & TPARG_FL_TPOINT)
742 					code->param = i + 1;
743 				else
744 					code->param = i;
745 			} else if (tparg_is_function_return(ctx->flags)) {
746 				code->op = FETCH_OP_EDATA;
747 				ret = __store_entry_arg(ctx->tp, i);
748 				if (ret < 0) {
749 					/* internal error */
750 					return ret;
751 				}
752 				code->offset = ret;
753 			}
754 			tid = params[i].type;
755 			goto found;
756 		}
757 	}
758 	trace_probe_log_err(ctx->offset, NO_BTFARG);
759 	return -ENOENT;
760 
761 found:
762 	type = btf_type_skip_modifiers(ctx->btf, tid, &tid);
763 found_type:
764 	if (!type) {
765 		trace_probe_log_err(ctx->offset, BAD_BTF_TID);
766 		return -EINVAL;
767 	}
768 	/* Initialize the last type information */
769 	ctx->last_type = type;
770 	ctx->last_bitoffs = 0;
771 	ctx->last_bitsize = 0;
772 	if (field) {
773 		ctx->offset += field - varname;
774 		return parse_btf_field(field, type, pcode, end, ctx);
775 	}
776 	return 0;
777 }
778 
779 static const struct fetch_type *find_fetch_type_from_btf_type(
780 					struct traceprobe_parse_context *ctx)
781 {
782 	struct btf *btf = ctx_btf(ctx);
783 	const char *typestr = NULL;
784 
785 	if (btf && ctx->last_type)
786 		typestr = fetch_type_from_btf_type(btf, ctx->last_type, ctx);
787 
788 	return find_fetch_type(typestr, ctx->flags);
789 }
790 
791 static int parse_btf_bitfield(struct fetch_insn **pcode,
792 			      struct traceprobe_parse_context *ctx)
793 {
794 	struct fetch_insn *code = *pcode;
795 
796 	if ((ctx->last_bitsize % 8 == 0) && ctx->last_bitoffs == 0)
797 		return 0;
798 
799 	code++;
800 	if (code->op != FETCH_OP_NOP) {
801 		trace_probe_log_err(ctx->offset, TOO_MANY_OPS);
802 		return -EINVAL;
803 	}
804 	*pcode = code;
805 
806 	code->op = FETCH_OP_MOD_BF;
807 	code->lshift = 64 - (ctx->last_bitsize + ctx->last_bitoffs);
808 	code->rshift = 64 - ctx->last_bitsize;
809 	code->basesize = 64 / 8;
810 	return 0;
811 }
812 
813 static int query_btf_struct(const char *sname, struct traceprobe_parse_context *ctx)
814 {
815 	struct btf *btf = NULL;
816 	int id;
817 
818 	/* A struct_btf should only be used by a single argument */
819 	if (WARN_ON_ONCE(ctx->struct_btf)) {
820 		btf_put(ctx->struct_btf);
821 		ctx->struct_btf = NULL;
822 	}
823 
824 	id = bpf_find_btf_id(sname, BTF_KIND_STRUCT, &btf);
825 	if (id < 0)
826 		return id;
827 	ctx->struct_btf = btf;
828 	ctx->last_struct = btf_type_by_id(ctx->struct_btf, id);
829 	return 0;
830 }
831 
832 static int handle_typecast(char *arg, struct fetch_insn **pcode,
833 			   struct fetch_insn *end,
834 			   struct traceprobe_parse_context *ctx)
835 {
836 	char *tmp;
837 	int ret;
838 
839 	/* Currently this only works for eprobes */
840 	if (!(ctx->flags & TPARG_FL_TEVENT)) {
841 		trace_probe_log_err(ctx->offset, TYPECAST_NOT_EVENT);
842 		return -EINVAL;
843 	}
844 
845 	tmp = strchr(arg, ')');
846 	if (!tmp) {
847 		trace_probe_log_err(ctx->offset + strlen(arg),
848 				    DEREF_OPEN_BRACE);
849 		return -EINVAL;
850 	}
851 	*tmp = '\0';
852 	ret = query_btf_struct(arg + 1, ctx);
853 	*tmp = ')';
854 
855 	if (ret < 0) {
856 		trace_probe_log_err(ctx->offset + 1, NO_PTR_STRCT);
857 		return -EINVAL;
858 	}
859 
860 	tmp++;
861 
862 	ctx->offset += tmp - arg;
863 	ret = parse_btf_arg(tmp, pcode, end, ctx);
864 	return ret;
865 }
866 
867 #else /* !CONFIG_PROBE_EVENTS_BTF_ARGS */
868 
869 static void clear_struct_btf(struct traceprobe_parse_context *ctx)
870 {
871 	ctx->struct_btf = NULL;
872 }
873 
874 static void clear_btf_context(struct traceprobe_parse_context *ctx)
875 {
876 	ctx->btf = NULL;
877 }
878 
879 static int query_btf_context(struct traceprobe_parse_context *ctx)
880 {
881 	return -EOPNOTSUPP;
882 }
883 
884 static int parse_btf_arg(char *varname,
885 			 struct fetch_insn **pcode, struct fetch_insn *end,
886 			 struct traceprobe_parse_context *ctx)
887 {
888 	trace_probe_log_err(ctx->offset, NOSUP_BTFARG);
889 	return -EOPNOTSUPP;
890 }
891 
892 static int parse_btf_bitfield(struct fetch_insn **pcode,
893 			      struct traceprobe_parse_context *ctx)
894 {
895 	trace_probe_log_err(ctx->offset, NOSUP_BTFARG);
896 	return -EOPNOTSUPP;
897 }
898 
899 #define find_fetch_type_from_btf_type(ctx)		\
900 	find_fetch_type(NULL, ctx->flags)
901 
902 static int check_prepare_btf_string_fetch(char *typename,
903 				struct fetch_insn **pcode,
904 				struct traceprobe_parse_context *ctx)
905 {
906 	return 0;
907 }
908 
909 static int handle_typecast(char *arg, struct fetch_insn **pcode,
910 			   struct fetch_insn *end,
911 			   struct traceprobe_parse_context *ctx)
912 {
913 	trace_probe_log_err(ctx->offset, NOSUP_BTFARG);
914 	return -EOPNOTSUPP;
915 }
916 
917 #endif /* CONFIG_PROBE_EVENTS_BTF_ARGS */
918 
919 #ifdef CONFIG_HAVE_FUNCTION_ARG_ACCESS_API
920 
921 static void store_entry_arg_at(struct fetch_insn *code, int argnum, int offset)
922 {
923 	code[0].op = FETCH_OP_ARG;
924 	code[0].param = argnum;
925 	code[1].op = FETCH_OP_ST_EDATA;
926 	code[1].offset = offset;
927 }
928 
929 static int get_entry_arg_max_offset(struct probe_entry_arg *earg)
930 {
931 	int i, max_offset = 0;
932 
933 	/*
934 	 * earg->code[] array has an operation sequence which is run in
935 	 * the entry handler.
936 	 * The sequence stopped by FETCH_OP_END and each data stored in
937 	 * the entry data buffer by FETCH_OP_ST_EDATA. The FETCH_OP_ST_EDATA
938 	 * stores the data at the data buffer + its offset, and all data are
939 	 * "unsigned long" size. The offset must be increased when a data is
940 	 * stored. Thus we need to find the last FETCH_OP_ST_EDATA in the
941 	 * code array.
942 	 */
943 	for (i = 0; i < earg->size - 1 && earg->code[i].op != FETCH_OP_END; i++) {
944 		if (earg->code[i].op == FETCH_OP_ST_EDATA)
945 			if (earg->code[i].offset > max_offset)
946 				max_offset = earg->code[i].offset;
947 	}
948 	return max_offset;
949 }
950 
951 /*
952  * Add the entry code to store the 'argnum'th parameter and return the offset
953  * in the entry data buffer where the data will be stored.
954  */
955 static int __store_entry_arg(struct trace_probe *tp, int argnum)
956 {
957 	struct probe_entry_arg *earg = tp->entry_arg;
958 	int i, offset, last_offset = 0;
959 
960 	if (!earg) {
961 		earg = kzalloc_flex(*earg, code, 2 * tp->nr_args + 1);
962 		if (!earg)
963 			return -ENOMEM;
964 		earg->size = 2 * tp->nr_args + 1;
965 		/* Fill the code buffer with 'end' to simplify it */
966 		for (i = 0; i < earg->size; i++)
967 			earg->code[i].op = FETCH_OP_END;
968 		tp->entry_arg = earg;
969 		store_entry_arg_at(earg->code, argnum, 0);
970 		return 0;
971 	}
972 
973 	/*
974 	 * NOTE: if anyone change the following rule, please rewrite this.
975 	 * The entry code array is filled with the pair of
976 	 *
977 	 * [FETCH_OP_ARG(argnum)]
978 	 * [FETCH_OP_ST_EDATA(offset of entry data buffer)]
979 	 *
980 	 * and the rest of entries are filled with [FETCH_OP_END].
981 	 * The offset should be incremented, thus the last pair should
982 	 * have the largest offset.
983 	 */
984 
985 	/* Search the offset for the sprcified argnum. */
986 	for (i = 0; i < earg->size - 1 && earg->code[i].op != FETCH_OP_END; i += 2) {
987 		if (WARN_ON_ONCE(earg->code[i].op != FETCH_OP_ARG))
988 			return -EINVAL;
989 
990 		if (earg->code[i].param != argnum)
991 			continue;
992 
993 		if (WARN_ON_ONCE(earg->code[i + 1].op != FETCH_OP_ST_EDATA))
994 			return -EINVAL;
995 
996 		return earg->code[i + 1].offset;
997 	}
998 	/* Not found, append new entry if possible. */
999 	if (i >= earg->size - 1)
1000 		return -ENOSPC;
1001 
1002 	/* The last entry must have the largest offset. */
1003 	if (i != 0) {
1004 		if (WARN_ON_ONCE(earg->code[i - 1].op != FETCH_OP_ST_EDATA))
1005 			return -EINVAL;
1006 		last_offset = earg->code[i - 1].offset;
1007 	}
1008 
1009 	offset = last_offset + sizeof(unsigned long);
1010 	store_entry_arg_at(&earg->code[i], argnum, offset);
1011 	return offset;
1012 }
1013 
1014 int traceprobe_get_entry_data_size(struct trace_probe *tp)
1015 {
1016 	struct probe_entry_arg *earg = tp->entry_arg;
1017 
1018 	if (!earg)
1019 		return 0;
1020 
1021 	return get_entry_arg_max_offset(earg) + sizeof(unsigned long);
1022 }
1023 
1024 void store_trace_entry_data(void *edata, struct trace_probe *tp, struct pt_regs *regs)
1025 {
1026 	struct probe_entry_arg *earg = tp->entry_arg;
1027 	unsigned long val = 0;
1028 	int i;
1029 
1030 	if (!earg)
1031 		return;
1032 
1033 	for (i = 0; i < earg->size; i++) {
1034 		struct fetch_insn *code = &earg->code[i];
1035 
1036 		switch (code->op) {
1037 		case FETCH_OP_ARG:
1038 			val = regs_get_kernel_argument(regs, code->param);
1039 			break;
1040 		case FETCH_OP_ST_EDATA:
1041 			*(unsigned long *)((unsigned long)edata + code->offset) = val;
1042 			break;
1043 		case FETCH_OP_END:
1044 			goto end;
1045 		default:
1046 			break;
1047 		}
1048 	}
1049 end:
1050 	return;
1051 }
1052 NOKPROBE_SYMBOL(store_trace_entry_data)
1053 #endif
1054 
1055 #define PARAM_MAX_STACK (THREAD_SIZE / sizeof(unsigned long))
1056 
1057 /* Parse $vars. @orig_arg points '$', which syncs to @ctx->offset */
1058 static int parse_probe_vars(char *orig_arg, const struct fetch_type *t,
1059 			    struct fetch_insn **pcode,
1060 			    struct fetch_insn *end,
1061 			    struct traceprobe_parse_context *ctx)
1062 {
1063 	struct fetch_insn *code = *pcode;
1064 	int err = TP_ERR_BAD_VAR;
1065 	char *arg = orig_arg + 1;
1066 	unsigned long param;
1067 	int ret = 0;
1068 	int len;
1069 
1070 	if (ctx->flags & TPARG_FL_TEVENT) {
1071 		if (parse_trace_event(arg, code, ctx) < 0)
1072 			goto inval;
1073 		return 0;
1074 	}
1075 
1076 	if (str_has_prefix(arg, "retval")) {
1077 		if (!(ctx->flags & TPARG_FL_RETURN)) {
1078 			err = TP_ERR_RETVAL_ON_PROBE;
1079 			goto inval;
1080 		}
1081 		if (!(ctx->flags & TPARG_FL_KERNEL) ||
1082 		    !IS_ENABLED(CONFIG_PROBE_EVENTS_BTF_ARGS)) {
1083 			code->op = FETCH_OP_RETVAL;
1084 			return 0;
1085 		}
1086 		return parse_btf_arg(orig_arg, pcode, end, ctx);
1087 	}
1088 
1089 	len = str_has_prefix(arg, "stack");
1090 	if (len) {
1091 
1092 		if (arg[len] == '\0') {
1093 			code->op = FETCH_OP_STACKP;
1094 			return 0;
1095 		}
1096 
1097 		if (isdigit(arg[len])) {
1098 			ret = kstrtoul(arg + len, 10, &param);
1099 			if (ret)
1100 				goto inval;
1101 
1102 			if ((ctx->flags & TPARG_FL_KERNEL) &&
1103 			    param > PARAM_MAX_STACK) {
1104 				err = TP_ERR_BAD_STACK_NUM;
1105 				goto inval;
1106 			}
1107 			code->op = FETCH_OP_STACK;
1108 			code->param = (unsigned int)param;
1109 			return 0;
1110 		}
1111 		goto inval;
1112 	}
1113 
1114 	if (strcmp(arg, "comm") == 0 || strcmp(arg, "COMM") == 0) {
1115 		code->op = FETCH_OP_COMM;
1116 		return 0;
1117 	}
1118 
1119 #ifdef CONFIG_HAVE_FUNCTION_ARG_ACCESS_API
1120 	len = str_has_prefix(arg, "arg");
1121 	if (len) {
1122 		ret = kstrtoul(arg + len, 10, &param);
1123 		if (ret)
1124 			goto inval;
1125 
1126 		if (!param || param > PARAM_MAX_STACK) {
1127 			err = TP_ERR_BAD_ARG_NUM;
1128 			goto inval;
1129 		}
1130 		param--; /* argN starts from 1, but internal arg[N] starts from 0 */
1131 
1132 		if (tparg_is_function_entry(ctx->flags)) {
1133 			code->op = FETCH_OP_ARG;
1134 			code->param = (unsigned int)param;
1135 			/*
1136 			 * The tracepoint probe will probe a stub function, and the
1137 			 * first parameter of the stub is a dummy and should be ignored.
1138 			 */
1139 			if (ctx->flags & TPARG_FL_TPOINT)
1140 				code->param++;
1141 		} else if (tparg_is_function_return(ctx->flags)) {
1142 			/* function entry argument access from return probe */
1143 			ret = __store_entry_arg(ctx->tp, param);
1144 			if (ret < 0)	/* This error should be an internal error */
1145 				return ret;
1146 
1147 			code->op = FETCH_OP_EDATA;
1148 			code->offset = ret;
1149 		} else {
1150 			err = TP_ERR_NOFENTRY_ARGS;
1151 			goto inval;
1152 		}
1153 		return 0;
1154 	}
1155 #endif
1156 
1157 inval:
1158 	__trace_probe_log_err(ctx->offset, err);
1159 	return -EINVAL;
1160 }
1161 
1162 static int str_to_immediate(char *str, unsigned long *imm)
1163 {
1164 	if (isdigit(str[0]))
1165 		return kstrtoul(str, 0, imm);
1166 	else if (str[0] == '-')
1167 		return kstrtol(str, 0, (long *)imm);
1168 	else if (str[0] == '+')
1169 		return kstrtol(str + 1, 0, (long *)imm);
1170 	return -EINVAL;
1171 }
1172 
1173 static int __parse_imm_string(char *str, char **pbuf, int offs)
1174 {
1175 	size_t len = strlen(str);
1176 
1177 	if (!len || str[len - 1] != '"') {
1178 		trace_probe_log_err(offs + len, IMMSTR_NO_CLOSE);
1179 		return -EINVAL;
1180 	}
1181 	*pbuf = kstrndup(str, len - 1, GFP_KERNEL);
1182 	if (!*pbuf)
1183 		return -ENOMEM;
1184 	return 0;
1185 }
1186 
1187 /* Recursive argument parser */
1188 static int
1189 parse_probe_arg(char *arg, const struct fetch_type *type,
1190 		struct fetch_insn **pcode, struct fetch_insn *end,
1191 		struct traceprobe_parse_context *ctx)
1192 {
1193 	struct fetch_insn *code = *pcode;
1194 	unsigned long param;
1195 	int deref = FETCH_OP_DEREF;
1196 	long offset = 0;
1197 	char *tmp;
1198 	int ret = 0;
1199 
1200 	switch (arg[0]) {
1201 	case '$':
1202 		ret = parse_probe_vars(arg, type, pcode, end, ctx);
1203 		break;
1204 
1205 	case '%':	/* named register */
1206 		if (ctx->flags & (TPARG_FL_TEVENT | TPARG_FL_FPROBE)) {
1207 			/* eprobe and fprobe do not handle registers */
1208 			trace_probe_log_err(ctx->offset, BAD_VAR);
1209 			break;
1210 		}
1211 		ret = regs_query_register_offset(arg + 1);
1212 		if (ret >= 0) {
1213 			code->op = FETCH_OP_REG;
1214 			code->param = (unsigned int)ret;
1215 			ret = 0;
1216 		} else
1217 			trace_probe_log_err(ctx->offset, BAD_REG_NAME);
1218 		break;
1219 
1220 	case '@':	/* memory, file-offset or symbol */
1221 		if (isdigit(arg[1])) {
1222 			ret = kstrtoul(arg + 1, 0, &param);
1223 			if (ret) {
1224 				trace_probe_log_err(ctx->offset, BAD_MEM_ADDR);
1225 				break;
1226 			}
1227 			/* load address */
1228 			code->op = FETCH_OP_IMM;
1229 			code->immediate = param;
1230 		} else if (arg[1] == '+') {
1231 			/* kprobes don't support file offsets */
1232 			if (ctx->flags & TPARG_FL_KERNEL) {
1233 				trace_probe_log_err(ctx->offset, FILE_ON_KPROBE);
1234 				return -EINVAL;
1235 			}
1236 			ret = kstrtol(arg + 2, 0, &offset);
1237 			if (ret) {
1238 				trace_probe_log_err(ctx->offset, BAD_FILE_OFFS);
1239 				break;
1240 			}
1241 
1242 			code->op = FETCH_OP_FOFFS;
1243 			code->immediate = (unsigned long)offset;  // imm64?
1244 		} else {
1245 			/* uprobes don't support symbols */
1246 			if (!(ctx->flags & TPARG_FL_KERNEL)) {
1247 				trace_probe_log_err(ctx->offset, SYM_ON_UPROBE);
1248 				return -EINVAL;
1249 			}
1250 			/* Preserve symbol for updating */
1251 			code->op = FETCH_NOP_SYMBOL;
1252 			code->data = kstrdup(arg + 1, GFP_KERNEL);
1253 			if (!code->data)
1254 				return -ENOMEM;
1255 			if (++code == end) {
1256 				trace_probe_log_err(ctx->offset, TOO_MANY_OPS);
1257 				return -EINVAL;
1258 			}
1259 			code->op = FETCH_OP_IMM;
1260 			code->immediate = 0;
1261 		}
1262 		/* These are fetching from memory */
1263 		if (++code == end) {
1264 			trace_probe_log_err(ctx->offset, TOO_MANY_OPS);
1265 			return -EINVAL;
1266 		}
1267 		*pcode = code;
1268 		code->op = FETCH_OP_DEREF;
1269 		code->offset = offset;
1270 		break;
1271 
1272 	case '+':	/* deref memory */
1273 	case '-':
1274 		if (arg[1] == 'u') {
1275 			deref = FETCH_OP_UDEREF;
1276 			arg[1] = arg[0];
1277 			arg++;
1278 		}
1279 		if (arg[0] == '+')
1280 			arg++;	/* Skip '+', because kstrtol() rejects it. */
1281 		tmp = strchr(arg, '(');
1282 		if (!tmp) {
1283 			trace_probe_log_err(ctx->offset, DEREF_NEED_BRACE);
1284 			return -EINVAL;
1285 		}
1286 		*tmp = '\0';
1287 		ret = kstrtol(arg, 0, &offset);
1288 		if (ret) {
1289 			trace_probe_log_err(ctx->offset, BAD_DEREF_OFFS);
1290 			break;
1291 		}
1292 		ctx->offset += (tmp + 1 - arg) + (arg[0] != '-' ? 1 : 0);
1293 		arg = tmp + 1;
1294 		tmp = strrchr(arg, ')');
1295 		if (!tmp) {
1296 			trace_probe_log_err(ctx->offset + strlen(arg),
1297 					    DEREF_OPEN_BRACE);
1298 			return -EINVAL;
1299 		} else {
1300 			const struct fetch_type *t2 = find_fetch_type(NULL, ctx->flags);
1301 			int cur_offs = ctx->offset;
1302 
1303 			*tmp = '\0';
1304 			ret = parse_probe_arg(arg, t2, &code, end, ctx);
1305 			if (ret)
1306 				break;
1307 			ctx->offset = cur_offs;
1308 			if (code->op == FETCH_OP_COMM ||
1309 			    code->op == FETCH_OP_DATA) {
1310 				trace_probe_log_err(ctx->offset, COMM_CANT_DEREF);
1311 				return -EINVAL;
1312 			}
1313 			if (++code == end) {
1314 				trace_probe_log_err(ctx->offset, TOO_MANY_OPS);
1315 				return -EINVAL;
1316 			}
1317 			*pcode = code;
1318 
1319 			code->op = deref;
1320 			code->offset = offset;
1321 			/* Reset the last type if used */
1322 			ctx->last_type = NULL;
1323 		}
1324 		break;
1325 	case '\\':	/* Immediate value */
1326 		if (arg[1] == '"') {	/* Immediate string */
1327 			ret = __parse_imm_string(arg + 2, &tmp, ctx->offset + 2);
1328 			if (ret)
1329 				break;
1330 			code->op = FETCH_OP_DATA;
1331 			code->data = tmp;
1332 		} else {
1333 			ret = str_to_immediate(arg + 1, &code->immediate);
1334 			if (ret)
1335 				trace_probe_log_err(ctx->offset + 1, BAD_IMM);
1336 			else
1337 				code->op = FETCH_OP_IMM;
1338 		}
1339 		break;
1340 	case '(':
1341 		ret = handle_typecast(arg, pcode, end, ctx);
1342 		break;
1343 	default:
1344 		if (isalpha(arg[0]) || arg[0] == '_') {	/* BTF variable */
1345 			if (!tparg_is_function_entry(ctx->flags) &&
1346 			    !tparg_is_function_return(ctx->flags)) {
1347 				trace_probe_log_err(ctx->offset, NOSUP_BTFARG);
1348 				return -EINVAL;
1349 			}
1350 			ret = parse_btf_arg(arg, pcode, end, ctx);
1351 			break;
1352 		}
1353 	}
1354 	if (!ret && code->op == FETCH_OP_NOP) {
1355 		/* Parsed, but do not find fetch method */
1356 		trace_probe_log_err(ctx->offset, BAD_FETCH_ARG);
1357 		ret = -EINVAL;
1358 	}
1359 	return ret;
1360 }
1361 
1362 /* Bitfield type needs to be parsed into a fetch function */
1363 static int __parse_bitfield_probe_arg(const char *bf,
1364 				      const struct fetch_type *t,
1365 				      struct fetch_insn **pcode)
1366 {
1367 	struct fetch_insn *code = *pcode;
1368 	unsigned long bw, bo;
1369 	char *tail;
1370 
1371 	if (*bf != 'b')
1372 		return 0;
1373 
1374 	bw = simple_strtoul(bf + 1, &tail, 0);	/* Use simple one */
1375 
1376 	if (bw == 0 || *tail != '@')
1377 		return -EINVAL;
1378 
1379 	bf = tail + 1;
1380 	bo = simple_strtoul(bf, &tail, 0);
1381 
1382 	if (tail == bf || *tail != '/')
1383 		return -EINVAL;
1384 	code++;
1385 	if (code->op != FETCH_OP_NOP)
1386 		return -EINVAL;
1387 	*pcode = code;
1388 
1389 	code->op = FETCH_OP_MOD_BF;
1390 	code->lshift = BYTES_TO_BITS(t->size) - (bw + bo);
1391 	code->rshift = BYTES_TO_BITS(t->size) - bw;
1392 	code->basesize = t->size;
1393 
1394 	return (BYTES_TO_BITS(t->size) < (bw + bo)) ? -EINVAL : 0;
1395 }
1396 
1397 /* Split type part from @arg and return it. */
1398 static char *parse_probe_arg_type(char *arg, struct probe_arg *parg,
1399 				  struct traceprobe_parse_context *ctx)
1400 {
1401 	char *t = NULL, *t2, *t3;
1402 	int offs;
1403 
1404 	t = strchr(arg, ':');
1405 	if (t) {
1406 		*t++ = '\0';
1407 		t2 = strchr(t, '[');
1408 		if (t2) {
1409 			*t2++ = '\0';
1410 			t3 = strchr(t2, ']');
1411 			if (!t3) {
1412 				offs = t2 + strlen(t2) - arg;
1413 
1414 				trace_probe_log_err(ctx->offset + offs,
1415 						    ARRAY_NO_CLOSE);
1416 				return ERR_PTR(-EINVAL);
1417 			} else if (t3[1] != '\0') {
1418 				trace_probe_log_err(ctx->offset + t3 + 1 - arg,
1419 						    BAD_ARRAY_SUFFIX);
1420 				return ERR_PTR(-EINVAL);
1421 			}
1422 			*t3 = '\0';
1423 			if (kstrtouint(t2, 0, &parg->count) || !parg->count) {
1424 				trace_probe_log_err(ctx->offset + t2 - arg,
1425 						    BAD_ARRAY_NUM);
1426 				return ERR_PTR(-EINVAL);
1427 			}
1428 			if (parg->count > MAX_ARRAY_LEN) {
1429 				trace_probe_log_err(ctx->offset + t2 - arg,
1430 						    ARRAY_TOO_BIG);
1431 				return ERR_PTR(-EINVAL);
1432 			}
1433 		}
1434 	}
1435 	offs = t ? t - arg : 0;
1436 
1437 	/*
1438 	 * Since $comm and immediate string can not be dereferenced,
1439 	 * we can find those by strcmp. But ignore for eprobes.
1440 	 */
1441 	if (!(ctx->flags & TPARG_FL_TEVENT) &&
1442 	    (strcmp(arg, "$comm") == 0 || strcmp(arg, "$COMM") == 0 ||
1443 	     strncmp(arg, "\\\"", 2) == 0)) {
1444 		/* The type of $comm must be "string", and not an array type. */
1445 		if (parg->count || (t && strcmp(t, "string"))) {
1446 			trace_probe_log_err(ctx->offset + offs, NEED_STRING_TYPE);
1447 			return ERR_PTR(-EINVAL);
1448 		}
1449 		parg->type = find_fetch_type("string", ctx->flags);
1450 	} else
1451 		parg->type = find_fetch_type(t, ctx->flags);
1452 
1453 	if (!parg->type) {
1454 		trace_probe_log_err(ctx->offset + offs, BAD_TYPE);
1455 		return ERR_PTR(-EINVAL);
1456 	}
1457 
1458 	return t;
1459 }
1460 
1461 /* After parsing, adjust the fetch_insn according to the probe_arg */
1462 static int finalize_fetch_insn(struct fetch_insn *code,
1463 			       struct probe_arg *parg,
1464 			       char *type,
1465 			       int type_offset,
1466 			       struct traceprobe_parse_context *ctx)
1467 {
1468 	struct fetch_insn *scode;
1469 	int ret;
1470 
1471 	/* Store operation */
1472 	if (parg->type->is_string) {
1473 		/* Check bad combination of the type and the last fetch_insn. */
1474 		if (!strcmp(parg->type->name, "symstr")) {
1475 			if (code->op != FETCH_OP_REG && code->op != FETCH_OP_STACK &&
1476 			    code->op != FETCH_OP_RETVAL && code->op != FETCH_OP_ARG &&
1477 			    code->op != FETCH_OP_DEREF && code->op != FETCH_OP_TP_ARG) {
1478 				trace_probe_log_err(ctx->offset + type_offset,
1479 						    BAD_SYMSTRING);
1480 				return -EINVAL;
1481 			}
1482 		} else {
1483 			if (code->op != FETCH_OP_DEREF && code->op != FETCH_OP_UDEREF &&
1484 			    code->op != FETCH_OP_IMM && code->op != FETCH_OP_COMM &&
1485 			    code->op != FETCH_OP_DATA && code->op != FETCH_OP_TP_ARG) {
1486 				trace_probe_log_err(ctx->offset + type_offset,
1487 						    BAD_STRING);
1488 				return -EINVAL;
1489 			}
1490 		}
1491 
1492 		if (!strcmp(parg->type->name, "symstr") ||
1493 		    (code->op == FETCH_OP_IMM || code->op == FETCH_OP_COMM ||
1494 		     code->op == FETCH_OP_DATA) || code->op == FETCH_OP_TP_ARG ||
1495 		     parg->count) {
1496 			/*
1497 			 * IMM, DATA and COMM is pointing actual address, those
1498 			 * must be kept, and if parg->count != 0, this is an
1499 			 * array of string pointers instead of string address
1500 			 * itself.
1501 			 * For the symstr, it doesn't need to dereference, thus
1502 			 * it just get the value.
1503 			 */
1504 			code++;
1505 			if (code->op != FETCH_OP_NOP) {
1506 				trace_probe_log_err(ctx->offset, TOO_MANY_OPS);
1507 				return -EINVAL;
1508 			}
1509 		}
1510 
1511 		/* If op == DEREF, replace it with STRING */
1512 		if (!strcmp(parg->type->name, "ustring") ||
1513 		    code->op == FETCH_OP_UDEREF)
1514 			code->op = FETCH_OP_ST_USTRING;
1515 		else if (!strcmp(parg->type->name, "symstr"))
1516 			code->op = FETCH_OP_ST_SYMSTR;
1517 		else
1518 			code->op = FETCH_OP_ST_STRING;
1519 		code->size = parg->type->size;
1520 		parg->dynamic = true;
1521 	} else if (code->op == FETCH_OP_DEREF) {
1522 		code->op = FETCH_OP_ST_MEM;
1523 		code->size = parg->type->size;
1524 	} else if (code->op == FETCH_OP_UDEREF) {
1525 		code->op = FETCH_OP_ST_UMEM;
1526 		code->size = parg->type->size;
1527 	} else {
1528 		code++;
1529 		if (code->op != FETCH_OP_NOP) {
1530 			trace_probe_log_err(ctx->offset, TOO_MANY_OPS);
1531 			return -E2BIG;
1532 		}
1533 		code->op = FETCH_OP_ST_RAW;
1534 		code->size = parg->type->size;
1535 	}
1536 
1537 	/* Save storing fetch_insn. */
1538 	scode = code;
1539 
1540 	/* Modify operation */
1541 	if (type != NULL) {
1542 		/* Bitfield needs a special fetch_insn. */
1543 		ret = __parse_bitfield_probe_arg(type, parg->type, &code);
1544 		if (ret) {
1545 			trace_probe_log_err(ctx->offset + type_offset, BAD_BITFIELD);
1546 			return ret;
1547 		}
1548 	} else if (IS_ENABLED(CONFIG_PROBE_EVENTS_BTF_ARGS) &&
1549 		   ctx->last_type) {
1550 		/* If user not specified the type, try parsing BTF bitfield. */
1551 		ret = parse_btf_bitfield(&code, ctx);
1552 		if (ret)
1553 			return ret;
1554 	}
1555 
1556 	/* Loop(Array) operation */
1557 	if (parg->count) {
1558 		if (scode->op != FETCH_OP_ST_MEM &&
1559 		    scode->op != FETCH_OP_ST_STRING &&
1560 		    scode->op != FETCH_OP_ST_USTRING) {
1561 			trace_probe_log_err(ctx->offset + type_offset, BAD_STRING);
1562 			return -EINVAL;
1563 		}
1564 		code++;
1565 		if (code->op != FETCH_OP_NOP) {
1566 			trace_probe_log_err(ctx->offset, TOO_MANY_OPS);
1567 			return -E2BIG;
1568 		}
1569 		code->op = FETCH_OP_LP_ARRAY;
1570 		code->param = parg->count;
1571 	}
1572 
1573 	/* Finalize the fetch_insn array. */
1574 	code++;
1575 	code->op = FETCH_OP_END;
1576 
1577 	return 0;
1578 }
1579 
1580 /* String length checking wrapper */
1581 static int traceprobe_parse_probe_arg_body(const char *argv, ssize_t *size,
1582 					   struct probe_arg *parg,
1583 					   struct traceprobe_parse_context *ctx)
1584 {
1585 	struct fetch_insn *code, *tmp = NULL;
1586 	char *type, *arg __free(kfree) = NULL;
1587 	int ret, len;
1588 
1589 	len = strlen(argv);
1590 	if (len > MAX_ARGSTR_LEN) {
1591 		trace_probe_log_err(ctx->offset, ARG_TOO_LONG);
1592 		return -E2BIG;
1593 	} else if (len == 0) {
1594 		trace_probe_log_err(ctx->offset, NO_ARG_BODY);
1595 		return -EINVAL;
1596 	}
1597 
1598 	arg = kstrdup(argv, GFP_KERNEL);
1599 	if (!arg)
1600 		return -ENOMEM;
1601 
1602 	parg->comm = kstrdup(arg, GFP_KERNEL);
1603 	if (!parg->comm)
1604 		return -ENOMEM;
1605 
1606 	type = parse_probe_arg_type(arg, parg, ctx);
1607 	if (IS_ERR(type))
1608 		return PTR_ERR(type);
1609 
1610 	code = tmp = kzalloc_objs(*code, FETCH_INSN_MAX);
1611 	if (!code)
1612 		return -ENOMEM;
1613 	code[FETCH_INSN_MAX - 1].op = FETCH_OP_END;
1614 
1615 	ctx->last_type = NULL;
1616 	ret = parse_probe_arg(arg, parg->type, &code, &code[FETCH_INSN_MAX - 1],
1617 			      ctx);
1618 	if (ret < 0)
1619 		goto fail;
1620 
1621 	/* Update storing type if BTF is available */
1622 	if (IS_ENABLED(CONFIG_PROBE_EVENTS_BTF_ARGS) &&
1623 	    ctx->last_type) {
1624 		if (!type) {
1625 			parg->type = find_fetch_type_from_btf_type(ctx);
1626 		} else if (strstr(type, "string")) {
1627 			ret = check_prepare_btf_string_fetch(type, &code, ctx);
1628 			if (ret)
1629 				goto fail;
1630 		}
1631 	}
1632 	parg->offset = *size;
1633 	*size += parg->type->size * (parg->count ?: 1);
1634 
1635 	if (*size > MAX_PROBE_EVENT_SIZE) {
1636 		ret = -E2BIG;
1637 		trace_probe_log_err(ctx->offset, EVENT_TOO_BIG);
1638 		goto fail;
1639 	}
1640 
1641 	if (parg->count) {
1642 		len = strlen(parg->type->fmttype) + 6;
1643 		parg->fmt = kmalloc(len, GFP_KERNEL);
1644 		if (!parg->fmt) {
1645 			ret = -ENOMEM;
1646 			goto fail;
1647 		}
1648 		snprintf(parg->fmt, len, "%s[%d]", parg->type->fmttype,
1649 			 parg->count);
1650 	}
1651 
1652 	ret = finalize_fetch_insn(code, parg, type, type ? type - arg : 0, ctx);
1653 	if (ret < 0)
1654 		goto fail;
1655 
1656 	for (; code < tmp + FETCH_INSN_MAX; code++)
1657 		if (code->op == FETCH_OP_END)
1658 			break;
1659 	/* Shrink down the code buffer */
1660 	parg->code = kzalloc_objs(*code, code - tmp + 1);
1661 	if (!parg->code)
1662 		ret = -ENOMEM;
1663 	else
1664 		memcpy(parg->code, tmp, sizeof(*code) * (code - tmp + 1));
1665 
1666 fail:
1667 	if (ret < 0) {
1668 		for (code = tmp; code < tmp + FETCH_INSN_MAX; code++)
1669 			if (code->op == FETCH_NOP_SYMBOL ||
1670 			    code->op == FETCH_OP_DATA)
1671 				kfree(code->data);
1672 	}
1673 	kfree(tmp);
1674 
1675 	/* struct_btf should not be passed to other arguments */
1676 	clear_struct_btf(ctx);
1677 
1678 	return ret;
1679 }
1680 
1681 /* Return 1 if name is reserved or already used by another argument */
1682 static int traceprobe_conflict_field_name(const char *name,
1683 					  struct probe_arg *args, int narg)
1684 {
1685 	int i;
1686 
1687 	for (i = 0; i < ARRAY_SIZE(reserved_field_names); i++)
1688 		if (strcmp(reserved_field_names[i], name) == 0)
1689 			return 1;
1690 
1691 	for (i = 0; i < narg; i++)
1692 		if (strcmp(args[i].name, name) == 0)
1693 			return 1;
1694 
1695 	return 0;
1696 }
1697 
1698 static char *generate_probe_arg_name(const char *arg, int idx)
1699 {
1700 	char *name = NULL;
1701 	const char *end;
1702 
1703 	/*
1704 	 * If argument name is omitted, try arg as a name (BTF variable)
1705 	 * or "argN".
1706 	 */
1707 	if (IS_ENABLED(CONFIG_PROBE_EVENTS_BTF_ARGS)) {
1708 		end = strchr(arg, ':');
1709 		if (!end)
1710 			end = arg + strlen(arg);
1711 
1712 		name = kmemdup_nul(arg, end - arg, GFP_KERNEL);
1713 		if (!name || !is_good_name(name)) {
1714 			kfree(name);
1715 			name = NULL;
1716 		}
1717 	}
1718 
1719 	if (!name)
1720 		name = kasprintf(GFP_KERNEL, "arg%d", idx + 1);
1721 
1722 	return name;
1723 }
1724 
1725 int traceprobe_parse_probe_arg(struct trace_probe *tp, int i, const char *arg,
1726 			       struct traceprobe_parse_context *ctx)
1727 {
1728 	struct probe_arg *parg = &tp->args[i];
1729 	const char *body;
1730 
1731 	ctx->tp = tp;
1732 	body = strchr(arg, '=');
1733 	if (body) {
1734 		if (body - arg > MAX_ARG_NAME_LEN) {
1735 			trace_probe_log_err(0, ARG_NAME_TOO_LONG);
1736 			return -EINVAL;
1737 		} else if (body == arg) {
1738 			trace_probe_log_err(0, NO_ARG_NAME);
1739 			return -EINVAL;
1740 		}
1741 		parg->name = kmemdup_nul(arg, body - arg, GFP_KERNEL);
1742 		body++;
1743 	} else {
1744 		parg->name = generate_probe_arg_name(arg, i);
1745 		body = arg;
1746 	}
1747 	if (!parg->name)
1748 		return -ENOMEM;
1749 
1750 	if (!is_good_name(parg->name)) {
1751 		trace_probe_log_err(0, BAD_ARG_NAME);
1752 		return -EINVAL;
1753 	}
1754 	if (traceprobe_conflict_field_name(parg->name, tp->args, i)) {
1755 		trace_probe_log_err(0, USED_ARG_NAME);
1756 		return -EINVAL;
1757 	}
1758 	ctx->offset = body - arg;
1759 	/* Parse fetch argument */
1760 	return traceprobe_parse_probe_arg_body(body, &tp->size, parg, ctx);
1761 }
1762 
1763 void traceprobe_free_probe_arg(struct probe_arg *arg)
1764 {
1765 	struct fetch_insn *code = arg->code;
1766 
1767 	while (code && code->op != FETCH_OP_END) {
1768 		if (code->op == FETCH_NOP_SYMBOL ||
1769 		    code->op == FETCH_OP_DATA)
1770 			kfree(code->data);
1771 		code++;
1772 	}
1773 	kfree(arg->code);
1774 	kfree(arg->name);
1775 	kfree(arg->comm);
1776 	kfree(arg->fmt);
1777 }
1778 
1779 static int argv_has_var_arg(int argc, const char *argv[], int *args_idx,
1780 			    struct traceprobe_parse_context *ctx)
1781 {
1782 	int i, found = 0;
1783 
1784 	for (i = 0; i < argc; i++)
1785 		if (str_has_prefix(argv[i], "$arg")) {
1786 			trace_probe_log_set_index(i + 2);
1787 
1788 			if (!tparg_is_function_entry(ctx->flags) &&
1789 			    !tparg_is_function_return(ctx->flags)) {
1790 				trace_probe_log_err(0, NOFENTRY_ARGS);
1791 				return -EINVAL;
1792 			}
1793 
1794 			if (isdigit(argv[i][4])) {
1795 				found = 1;
1796 				continue;
1797 			}
1798 
1799 			if (argv[i][4] != '*') {
1800 				trace_probe_log_err(0, BAD_VAR);
1801 				return -EINVAL;
1802 			}
1803 
1804 			if (*args_idx >= 0 && *args_idx < argc) {
1805 				trace_probe_log_err(0, DOUBLE_ARGS);
1806 				return -EINVAL;
1807 			}
1808 			found = 1;
1809 			*args_idx = i;
1810 		}
1811 
1812 	return found;
1813 }
1814 
1815 static int sprint_nth_btf_arg(int idx, const char *type,
1816 			      char *buf, int bufsize,
1817 			      struct traceprobe_parse_context *ctx)
1818 {
1819 	const char *name;
1820 	int ret;
1821 
1822 	if (idx >= ctx->nr_params) {
1823 		trace_probe_log_err(0, NO_BTFARG);
1824 		return -ENOENT;
1825 	}
1826 	name = btf_name_by_offset(ctx->btf, ctx->params[idx].name_off);
1827 	if (!name) {
1828 		trace_probe_log_err(0, NO_BTF_ENTRY);
1829 		return -ENOENT;
1830 	}
1831 	ret = snprintf(buf, bufsize, "%s%s", name, type);
1832 	if (ret >= bufsize) {
1833 		trace_probe_log_err(0, ARGS_2LONG);
1834 		return -E2BIG;
1835 	}
1836 	return ret;
1837 }
1838 
1839 /* Return new_argv which must be freed after use */
1840 const char **traceprobe_expand_meta_args(int argc, const char *argv[],
1841 					 int *new_argc, char *buf, int bufsize,
1842 					 struct traceprobe_parse_context *ctx)
1843 {
1844 	const struct btf_param *params = NULL;
1845 	int i, j, n, used, ret, args_idx = -1;
1846 	const char **new_argv __free(kfree) = NULL;
1847 
1848 	ret = argv_has_var_arg(argc, argv, &args_idx, ctx);
1849 	if (ret < 0)
1850 		return ERR_PTR(ret);
1851 
1852 	if (!ret) {
1853 		*new_argc = argc;
1854 		return NULL;
1855 	}
1856 
1857 	ret = query_btf_context(ctx);
1858 	if (ret < 0 || ctx->nr_params == 0) {
1859 		if (args_idx != -1) {
1860 			/* $arg* requires BTF info */
1861 			trace_probe_log_err(0, NOSUP_BTFARG);
1862 			return (const char **)params;
1863 		}
1864 		*new_argc = argc;
1865 		return NULL;
1866 	}
1867 
1868 	if (args_idx >= 0)
1869 		*new_argc = argc + ctx->nr_params - 1;
1870 	else
1871 		*new_argc = argc;
1872 
1873 	new_argv = kcalloc(*new_argc, sizeof(char *), GFP_KERNEL);
1874 	if (!new_argv)
1875 		return ERR_PTR(-ENOMEM);
1876 
1877 	used = 0;
1878 	for (i = 0, j = 0; i < argc; i++) {
1879 		trace_probe_log_set_index(i + 2);
1880 		if (i == args_idx) {
1881 			for (n = 0; n < ctx->nr_params; n++) {
1882 				ret = sprint_nth_btf_arg(n, "", buf + used,
1883 							 bufsize - used, ctx);
1884 				if (ret < 0)
1885 					return ERR_PTR(ret);
1886 
1887 				new_argv[j++] = buf + used;
1888 				used += ret + 1;
1889 			}
1890 			continue;
1891 		}
1892 
1893 		if (str_has_prefix(argv[i], "$arg")) {
1894 			char *type = NULL;
1895 
1896 			n = simple_strtoul(argv[i] + 4, &type, 10);
1897 			if (type && !(*type == ':' || *type == '\0')) {
1898 				trace_probe_log_err(0, BAD_VAR);
1899 				return ERR_PTR(-ENOENT);
1900 			}
1901 			/* Note: $argN starts from $arg1 */
1902 			ret = sprint_nth_btf_arg(n - 1, type, buf + used,
1903 						 bufsize - used, ctx);
1904 			if (ret < 0)
1905 				return ERR_PTR(ret);
1906 			new_argv[j++] = buf + used;
1907 			used += ret + 1;
1908 		} else
1909 			new_argv[j++] = argv[i];
1910 	}
1911 
1912 	return_ptr(new_argv);
1913 }
1914 
1915 /* @buf: *buf must be equal to NULL. Caller must to free *buf */
1916 int traceprobe_expand_dentry_args(int argc, const char *argv[], char **buf)
1917 {
1918 	int i, used, ret;
1919 	const int bufsize = MAX_DENTRY_ARGS_LEN;
1920 	char *tmpbuf __free(kfree) = NULL;
1921 
1922 	if (*buf)
1923 		return -EINVAL;
1924 
1925 	used = 0;
1926 	for (i = 0; i < argc; i++) {
1927 		char *tmp __free(kfree) = NULL;
1928 		char *equal;
1929 		size_t arg_len;
1930 
1931 		if (!glob_match("*:%p[dD]", argv[i]))
1932 			continue;
1933 
1934 		if (!tmpbuf) {
1935 			tmpbuf = kmalloc(bufsize, GFP_KERNEL);
1936 			if (!tmpbuf)
1937 				return -ENOMEM;
1938 		}
1939 
1940 		tmp = kstrdup(argv[i], GFP_KERNEL);
1941 		if (!tmp)
1942 			return -ENOMEM;
1943 
1944 		equal = strchr(tmp, '=');
1945 		if (equal)
1946 			*equal = '\0';
1947 		arg_len = strlen(argv[i]);
1948 		tmp[arg_len - 4] = '\0';
1949 		if (argv[i][arg_len - 1] == 'd')
1950 			ret = snprintf(tmpbuf + used, bufsize - used,
1951 				       "%s%s+0x0(+0x%zx(%s)):string",
1952 				       equal ? tmp : "", equal ? "=" : "",
1953 				       offsetof(struct dentry, d_name.name),
1954 				       equal ? equal + 1 : tmp);
1955 		else
1956 			ret = snprintf(tmpbuf + used, bufsize - used,
1957 				       "%s%s+0x0(+0x%zx(+0x%zx(%s))):string",
1958 				       equal ? tmp : "", equal ? "=" : "",
1959 				       offsetof(struct dentry, d_name.name),
1960 				       offsetof(struct file, f_path.dentry),
1961 				       equal ? equal + 1 : tmp);
1962 
1963 		if (ret >= bufsize - used)
1964 			return -ENOMEM;
1965 		argv[i] = tmpbuf + used;
1966 		used += ret + 1;
1967 	}
1968 
1969 	*buf = no_free_ptr(tmpbuf);
1970 	return 0;
1971 }
1972 
1973 void traceprobe_finish_parse(struct traceprobe_parse_context *ctx)
1974 {
1975 	clear_btf_context(ctx);
1976 }
1977 
1978 int traceprobe_update_arg(struct probe_arg *arg)
1979 {
1980 	struct fetch_insn *code = arg->code;
1981 	long offset;
1982 	char *tmp;
1983 	char c;
1984 	int ret = 0;
1985 
1986 	while (code && code->op != FETCH_OP_END) {
1987 		if (code->op == FETCH_NOP_SYMBOL) {
1988 			if (code[1].op != FETCH_OP_IMM)
1989 				return -EINVAL;
1990 
1991 			tmp = strpbrk(code->data, "+-");
1992 			if (tmp)
1993 				c = *tmp;
1994 			ret = traceprobe_split_symbol_offset(code->data,
1995 							     &offset);
1996 			if (ret)
1997 				return ret;
1998 
1999 			code[1].immediate =
2000 				(unsigned long)kallsyms_lookup_name(code->data);
2001 			if (tmp)
2002 				*tmp = c;
2003 			if (!code[1].immediate)
2004 				return -ENOENT;
2005 			code[1].immediate += offset;
2006 		}
2007 		code++;
2008 	}
2009 	return 0;
2010 }
2011 
2012 /* When len=0, we just calculate the needed length */
2013 #define LEN_OR_ZERO (len ? len - pos : 0)
2014 static int __set_print_fmt(struct trace_probe *tp, char *buf, int len,
2015 			   enum probe_print_type ptype)
2016 {
2017 	struct probe_arg *parg;
2018 	int i, j;
2019 	int pos = 0;
2020 	const char *fmt, *arg;
2021 
2022 	switch (ptype) {
2023 	case PROBE_PRINT_NORMAL:
2024 		fmt = "(%lx)";
2025 		arg = ", REC->" FIELD_STRING_IP;
2026 		break;
2027 	case PROBE_PRINT_RETURN:
2028 		fmt = "(%lx <- %lx)";
2029 		arg = ", REC->" FIELD_STRING_FUNC ", REC->" FIELD_STRING_RETIP;
2030 		break;
2031 	case PROBE_PRINT_EVENT:
2032 		fmt = "";
2033 		arg = "";
2034 		break;
2035 	default:
2036 		WARN_ON_ONCE(1);
2037 		return 0;
2038 	}
2039 
2040 	pos += snprintf(buf + pos, LEN_OR_ZERO, "\"%s", fmt);
2041 
2042 	for (i = 0; i < tp->nr_args; i++) {
2043 		parg = tp->args + i;
2044 		pos += snprintf(buf + pos, LEN_OR_ZERO, " %s=", parg->name);
2045 		if (parg->count) {
2046 			pos += snprintf(buf + pos, LEN_OR_ZERO, "{%s",
2047 					parg->type->fmt);
2048 			for (j = 1; j < parg->count; j++)
2049 				pos += snprintf(buf + pos, LEN_OR_ZERO, ",%s",
2050 						parg->type->fmt);
2051 			pos += snprintf(buf + pos, LEN_OR_ZERO, "}");
2052 		} else
2053 			pos += snprintf(buf + pos, LEN_OR_ZERO, "%s",
2054 					parg->type->fmt);
2055 	}
2056 
2057 	pos += snprintf(buf + pos, LEN_OR_ZERO, "\"%s", arg);
2058 
2059 	for (i = 0; i < tp->nr_args; i++) {
2060 		parg = tp->args + i;
2061 		if (parg->count) {
2062 			if (parg->type->is_string)
2063 				fmt = ", __get_str(%s[%d])";
2064 			else
2065 				fmt = ", REC->%s[%d]";
2066 			for (j = 0; j < parg->count; j++)
2067 				pos += snprintf(buf + pos, LEN_OR_ZERO,
2068 						fmt, parg->name, j);
2069 		} else {
2070 			if (parg->type->is_string)
2071 				fmt = ", __get_str(%s)";
2072 			else
2073 				fmt = ", REC->%s";
2074 			pos += snprintf(buf + pos, LEN_OR_ZERO,
2075 					fmt, parg->name);
2076 		}
2077 	}
2078 
2079 	/* return the length of print_fmt */
2080 	return pos;
2081 }
2082 #undef LEN_OR_ZERO
2083 
2084 int traceprobe_set_print_fmt(struct trace_probe *tp, enum probe_print_type ptype)
2085 {
2086 	struct trace_event_call *call = trace_probe_event_call(tp);
2087 	int len;
2088 	char *print_fmt;
2089 
2090 	/* First: called with 0 length to calculate the needed length */
2091 	len = __set_print_fmt(tp, NULL, 0, ptype);
2092 	print_fmt = kmalloc(len + 1, GFP_KERNEL);
2093 	if (!print_fmt)
2094 		return -ENOMEM;
2095 
2096 	/* Second: actually write the @print_fmt */
2097 	__set_print_fmt(tp, print_fmt, len + 1, ptype);
2098 	call->print_fmt = print_fmt;
2099 
2100 	return 0;
2101 }
2102 
2103 int traceprobe_define_arg_fields(struct trace_event_call *event_call,
2104 				 size_t offset, struct trace_probe *tp)
2105 {
2106 	int ret, i;
2107 
2108 	/* Set argument names as fields */
2109 	for (i = 0; i < tp->nr_args; i++) {
2110 		struct probe_arg *parg = &tp->args[i];
2111 		const char *fmt = parg->type->fmttype;
2112 		int size = parg->type->size;
2113 
2114 		if (parg->fmt)
2115 			fmt = parg->fmt;
2116 		if (parg->count)
2117 			size *= parg->count;
2118 		ret = trace_define_field(event_call, fmt, parg->name,
2119 					 offset + parg->offset, size,
2120 					 parg->type->is_signed,
2121 					 FILTER_OTHER);
2122 		if (ret)
2123 			return ret;
2124 	}
2125 	return 0;
2126 }
2127 
2128 static void trace_probe_event_free(struct trace_probe_event *tpe)
2129 {
2130 	kfree(tpe->class.system);
2131 	kfree(tpe->call.name);
2132 	kfree(tpe->call.print_fmt);
2133 	kfree(tpe);
2134 }
2135 
2136 int trace_probe_append(struct trace_probe *tp, struct trace_probe *to)
2137 {
2138 	if (trace_probe_has_sibling(tp))
2139 		return -EBUSY;
2140 
2141 	list_del_init(&tp->list);
2142 	trace_probe_event_free(tp->event);
2143 
2144 	tp->event = to->event;
2145 	list_add_tail(&tp->list, trace_probe_probe_list(to));
2146 
2147 	return 0;
2148 }
2149 
2150 void trace_probe_unlink(struct trace_probe *tp)
2151 {
2152 	list_del_init(&tp->list);
2153 	if (list_empty(trace_probe_probe_list(tp)))
2154 		trace_probe_event_free(tp->event);
2155 	tp->event = NULL;
2156 }
2157 
2158 void trace_probe_cleanup(struct trace_probe *tp)
2159 {
2160 	int i;
2161 
2162 	for (i = 0; i < tp->nr_args; i++)
2163 		traceprobe_free_probe_arg(&tp->args[i]);
2164 
2165 	if (tp->entry_arg) {
2166 		kfree(tp->entry_arg);
2167 		tp->entry_arg = NULL;
2168 	}
2169 
2170 	if (tp->event)
2171 		trace_probe_unlink(tp);
2172 }
2173 
2174 int trace_probe_init(struct trace_probe *tp, const char *event,
2175 		     const char *group, bool alloc_filter, int nargs)
2176 {
2177 	struct trace_event_call *call;
2178 	size_t size = sizeof(struct trace_probe_event);
2179 	int ret = 0;
2180 
2181 	if (!event || !group)
2182 		return -EINVAL;
2183 
2184 	if (alloc_filter)
2185 		size += sizeof(struct trace_uprobe_filter);
2186 
2187 	tp->event = kzalloc(size, GFP_KERNEL);
2188 	if (!tp->event)
2189 		return -ENOMEM;
2190 
2191 	INIT_LIST_HEAD(&tp->event->files);
2192 	INIT_LIST_HEAD(&tp->event->class.fields);
2193 	INIT_LIST_HEAD(&tp->event->probes);
2194 	INIT_LIST_HEAD(&tp->list);
2195 	list_add(&tp->list, &tp->event->probes);
2196 
2197 	call = trace_probe_event_call(tp);
2198 	call->class = &tp->event->class;
2199 	call->name = kstrdup(event, GFP_KERNEL);
2200 	if (!call->name) {
2201 		ret = -ENOMEM;
2202 		goto error;
2203 	}
2204 
2205 	tp->event->class.system = kstrdup(group, GFP_KERNEL);
2206 	if (!tp->event->class.system) {
2207 		ret = -ENOMEM;
2208 		goto error;
2209 	}
2210 
2211 	tp->nr_args = nargs;
2212 	/* Make sure pointers in args[] are NULL */
2213 	if (nargs)
2214 		memset(tp->args, 0, sizeof(tp->args[0]) * nargs);
2215 
2216 	return 0;
2217 
2218 error:
2219 	trace_probe_cleanup(tp);
2220 	return ret;
2221 }
2222 
2223 static struct trace_event_call *
2224 find_trace_event_call(const char *system, const char *event_name)
2225 {
2226 	struct trace_event_call *tp_event;
2227 	const char *name;
2228 
2229 	list_for_each_entry(tp_event, &ftrace_events, list) {
2230 		if (!tp_event->class->system ||
2231 		    strcmp(system, tp_event->class->system))
2232 			continue;
2233 		name = trace_event_name(tp_event);
2234 		if (!name || strcmp(event_name, name))
2235 			continue;
2236 		return tp_event;
2237 	}
2238 
2239 	return NULL;
2240 }
2241 
2242 int trace_probe_register_event_call(struct trace_probe *tp)
2243 {
2244 	struct trace_event_call *call = trace_probe_event_call(tp);
2245 	int ret;
2246 
2247 	lockdep_assert_held(&event_mutex);
2248 
2249 	if (find_trace_event_call(trace_probe_group_name(tp),
2250 				  trace_probe_name(tp)))
2251 		return -EEXIST;
2252 
2253 	ret = register_trace_event(&call->event);
2254 	if (!ret)
2255 		return -ENODEV;
2256 
2257 	ret = trace_add_event_call(call);
2258 	if (ret)
2259 		unregister_trace_event(&call->event);
2260 
2261 	return ret;
2262 }
2263 
2264 int trace_probe_add_file(struct trace_probe *tp, struct trace_event_file *file)
2265 {
2266 	struct event_file_link *link;
2267 
2268 	link = kmalloc_obj(*link);
2269 	if (!link)
2270 		return -ENOMEM;
2271 
2272 	link->file = file;
2273 	INIT_LIST_HEAD(&link->list);
2274 	list_add_tail_rcu(&link->list, &tp->event->files);
2275 	trace_probe_set_flag(tp, TP_FLAG_TRACE);
2276 	return 0;
2277 }
2278 
2279 struct event_file_link *trace_probe_get_file_link(struct trace_probe *tp,
2280 						  struct trace_event_file *file)
2281 {
2282 	struct event_file_link *link;
2283 
2284 	trace_probe_for_each_link(link, tp) {
2285 		if (link->file == file)
2286 			return link;
2287 	}
2288 
2289 	return NULL;
2290 }
2291 
2292 int trace_probe_remove_file(struct trace_probe *tp,
2293 			    struct trace_event_file *file)
2294 {
2295 	struct event_file_link *link;
2296 
2297 	link = trace_probe_get_file_link(tp, file);
2298 	if (!link)
2299 		return -ENOENT;
2300 
2301 	list_del_rcu(&link->list);
2302 	kvfree_rcu_mightsleep(link);
2303 
2304 	if (list_empty(&tp->event->files))
2305 		trace_probe_clear_flag(tp, TP_FLAG_TRACE);
2306 
2307 	return 0;
2308 }
2309 
2310 /*
2311  * Return the smallest index of different type argument (start from 1).
2312  * If all argument types and name are same, return 0.
2313  */
2314 int trace_probe_compare_arg_type(struct trace_probe *a, struct trace_probe *b)
2315 {
2316 	int i;
2317 
2318 	/* In case of more arguments */
2319 	if (a->nr_args < b->nr_args)
2320 		return a->nr_args + 1;
2321 	if (a->nr_args > b->nr_args)
2322 		return b->nr_args + 1;
2323 
2324 	for (i = 0; i < a->nr_args; i++) {
2325 		if ((b->nr_args <= i) ||
2326 		    ((a->args[i].type != b->args[i].type) ||
2327 		     (a->args[i].count != b->args[i].count) ||
2328 		     strcmp(a->args[i].name, b->args[i].name)))
2329 			return i + 1;
2330 	}
2331 
2332 	return 0;
2333 }
2334 
2335 bool trace_probe_match_command_args(struct trace_probe *tp,
2336 				    int argc, const char **argv)
2337 {
2338 	char buf[MAX_ARGSTR_LEN + 1];
2339 	int i;
2340 
2341 	if (tp->nr_args < argc)
2342 		return false;
2343 
2344 	for (i = 0; i < argc; i++) {
2345 		snprintf(buf, sizeof(buf), "%s=%s",
2346 			 tp->args[i].name, tp->args[i].comm);
2347 		if (strcmp(buf, argv[i]))
2348 			return false;
2349 	}
2350 	return true;
2351 }
2352 
2353 int trace_probe_create(const char *raw_command, int (*createfn)(int, const char **))
2354 {
2355 	int argc = 0, ret = 0;
2356 	char **argv;
2357 
2358 	argv = argv_split(GFP_KERNEL, raw_command, &argc);
2359 	if (!argv)
2360 		return -ENOMEM;
2361 
2362 	if (argc)
2363 		ret = createfn(argc, (const char **)argv);
2364 
2365 	argv_free(argv);
2366 
2367 	return ret;
2368 }
2369 
2370 int trace_probe_print_args(struct trace_seq *s, struct probe_arg *args, int nr_args,
2371 		 u8 *data, void *field)
2372 {
2373 	void *p;
2374 	int i, j;
2375 
2376 	for (i = 0; i < nr_args; i++) {
2377 		struct probe_arg *a = args + i;
2378 
2379 		trace_seq_printf(s, " %s=", a->name);
2380 		if (likely(!a->count)) {
2381 			if (!a->type->print(s, data + a->offset, field))
2382 				return -ENOMEM;
2383 			continue;
2384 		}
2385 		trace_seq_putc(s, '{');
2386 		p = data + a->offset;
2387 		for (j = 0; j < a->count; j++) {
2388 			if (!a->type->print(s, p, field))
2389 				return -ENOMEM;
2390 			trace_seq_putc(s, j == a->count - 1 ? '}' : ',');
2391 			p += a->type->size;
2392 		}
2393 	}
2394 	return 0;
2395 }
2396