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