Skip to contentSkip to conversation

tracing/eprobes: Allow use of BTF names to dereference pointers

PATCH Superseded Steven Rostedt 5 participants 11 messages May 19 – May 21

Subsystems (4)

v5 of 8 Superseded Latest is v8 →

lore
Share & tools
lore

Steven Rostedt

author maintainer
#0 lore raw

PATCH v5 1/1 tracing/eprobes: Allow use of BTF names to dereference pointers

From: Steven Rostedt <[email protected]>

Add syntax to the parsing of eprobes to be able to typecast a trace event field that is a pointer to a structure.

Currently, a dereference must be a number, where the user has to figure out manually the offset of a member of a structure that they want to dereference.

But for event probes that records a field that happens to be a pointer to a structure, it cannot dereference these values with BTF naming, but must use numerical offsets.

For example, to find out what device a sk_buff is pointing to in the net_dev_xmit trace event, one must first use gdb to find the offsets of the members of the structures:

(gdb) p &((struct sk_buff *)0)->dev
$1 = (struct net_device **) 0x10
(gdb) p &((struct net_device *)0)->name
$2 = (char (*)[16]) 0x118

And then use the raw numbers to dereference:

  # echo 'e:xmit net.net_dev_xmit +0x118(+0x10($skbaddr)):string' >> dynamic_events

If BTF is in the kernel, then instead, the skbaddr can be typecast to sk_buff and use the normal dereference logic.

  # echo 'e:xmit net.net_dev_xmit (sk_buff)skbaddr->dev->name:string' >> dynamic_events
  # echo 1 > events/eprobes/xmit/enable
  # cat trace
[..]
    sshd-session-1022    [000] b..2.   860.249343: xmit: (net.net_dev_xmit) arg1="enp7s0"
    sshd-session-1022    [000] b..2.   860.250061: xmit: (net.net_dev_xmit) arg1="enp7s0"
    sshd-session-1022    [000] b..2.   860.250142: xmit: (net.net_dev_xmit) arg1="enp7s0"
    sshd-session-1022    [000] b..2.   860.263553: xmit: (net.net_dev_xmit) arg1="enp7s0"
    sshd-session-1022    [000] b..2.   860.283820: xmit: (net.net_dev_xmit) arg1="enp7s0"
    sshd-session-1022    [000] b..2.   860.302716: xmit: (net.net_dev_xmit) arg1="enp7s0"
    sshd-session-1022    [000] b..2.   860.322905: xmit: (net.net_dev_xmit) arg1="enp7s0"
    sshd-session-1022    [000] b..2.   860.342828: xmit: (net.net_dev_xmit) arg1="enp7s0"
    sshd-session-1022    [000] b..2.   860.362268: xmit: (net.net_dev_xmit) arg1="enp7s0"
    sshd-session-1022    [000] b..2.   860.382335: xmit: (net.net_dev_xmit) arg1="enp7s0"
    sshd-session-1022    [000] b..2.   860.400856: xmit: (net.net_dev_xmit) arg1="enp7s0"
    sshd-session-1022    [000] b..2.   860.419893: xmit: (net.net_dev_xmit) arg1="enp7s0"

The syntax is simply: (STRUCT)(FIELD)->MEMBER[->MEMBER..]

---
Changes from v4: https://patch.msgid.link/[email protected]

- Only describe this for eprobes, as that is currently the only user of this. Kprobes registers can come later. (Sashiko)

- Update the documentation to only be in the eprobetrace.rst.

- Make the btf value separate for structures and functions. As a function may use a BTF from a module, it can point to a structure that is in vmlinux. Make them separate and add a ctx_btf() helper function to figure out which one to use. (Masami Hiramatsu)

- Remove updating query_btf_context() by having it check funcname first. The above ctx_btf() helper fixes this.

- Rename TPARG_FL_STRUCT to TPARG_FL_TYPECAST (Masami Hiramatsu)

- Have the typecast code call parse_btf_arg() directly, instead of going through parse_probe_vars(). This fixes issues with updating the pcode value, and simplifies the formatting and code (Sashiko).

Added a parse_trace_event() helper to duplicate the handling of TPARG_FL_TEVENT.

- Use "goto found_type;" instead of relying on type being NULL in parse_btf_arg(). (Masami Hiramatsu)

- Add query_btf_struct() stub function when CONFIG_PROBE_EVENTS_BTF_ARGS is not defined. (kernel test robot)

- Remove need for '*' in typecast (Masami Hiramatsu)

- Have the event fields not use '$' when the typecast is used. (Masami Hiramatsu)

 Documentation/trace/eprobetrace.rst |   4 +
 kernel/trace/trace_probe.c          | 158 ++++++++++++++++++++++------
 kernel/trace/trace_probe.h          |   4 +
 3 files changed, 135 insertions(+), 31 deletions(-)
Documentation/trace/eprobetrace.rst +4 -0
index 89b5157cfab8..fe3602540569 100644
@@ -46,6 +46,10 @@ Synopsis of eprobe_events
 		  (x8/x16/x32/x64), VFS layer common type(%pd/%pD), "char",
                   "string", "ustring", "symbol", "symstr" and "bitfield" are
                   supported.
+  (STRUCT)FIELD->MEMBER[->MEMBER] : If BTF is supported, typecast FIELD to
+                  a pointer to STRUCT and then derference the pointer defined by
+                  ->MEMBER. Note that when this is used, the FIELD name does not
+                  need to be prefixed with a '$'.
 
 Types
 -----
kernel/trace/trace_probe.c +127 -31

294 lines not on this page · raw message

kernel/trace/trace_probe.h +4 -0
index 262d8707a3df..6735b8b2432b 100644
@@ -394,6 +394,7 @@ static inline int traceprobe_get_entry_data_size(struct trace_probe *tp)
  * TPARG_FL_KERNEL and TPARG_FL_USER are also mutually exclusive.
  * TPARG_FL_FPROBE and TPARG_FL_TPOINT are optional but it should be with
  * TPARG_FL_KERNEL.
+ * TPARG_FL_TYPECAST is set if an argument was typecast to a structure.
  */
 #define TPARG_FL_RETURN BIT(0)
 #define TPARG_FL_KERNEL BIT(1)
@@ -402,6 +403,7 @@ static inline int traceprobe_get_entry_data_size(struct trace_probe *tp)
 #define TPARG_FL_USER   BIT(4)
 #define TPARG_FL_FPROBE BIT(5)
 #define TPARG_FL_TPOINT BIT(6)
+#define TPARG_FL_TYPECAST BIT(7)
 #define TPARG_FL_LOC_MASK	GENMASK(4, 0)
 
 static inline bool tparg_is_function_entry(unsigned int flags)
@@ -422,7 +424,9 @@ struct traceprobe_parse_context {
 	const struct btf_param *params;	/* Parameter of the function */
 	s32 nr_params;			/* The number of the parameters */
 	struct btf *btf;		/* The BTF to be used */
+	struct btf *struct_btf;		/* The BTF to be used for structs */
 	const struct btf_type *last_type;	/* Saved type */
+	const struct btf_type *last_struct;	/* Saved structure */
 	u32 last_bitoffs;		/* Saved bitoffs */
 	u32 last_bitsize;		/* Saved bitsize */
 	struct trace_probe *tp;
Signature · 1 line
2.53.0
author maintainer
#1 lore raw
Quoted 30 lines · On Tue, 19 May 2026 13:01:44 -0400 Steven Rostedt <[email protected]> wrote:
kernel/trace/trace_probe.c +10 -1
@@ -636,11 +694,12 @@ static int parse_btf_arg(char *varname,
 	struct fetch_insn *code = *pcode;
 	const struct btf_param *params;
 	const struct btf_type *type;
+	struct btf *btf = ctx_btf(ctx);
 	char *field = NULL;
 	int i, is_ptr, ret;
 	u32 tid;
 
-	if (WARN_ON_ONCE(!ctx->funcname))
+	if (WARN_ON_ONCE(!ctx->funcname && !(ctx->flags & TPARG_FL_TYPECAST)))
 		return -EINVAL;
 
 	is_ptr = split_next_field(varname, &field, ctx);
@@ -653,6 +712,14 @@ static int parse_btf_arg(char *varname,
 		return -EOPNOTSUPP;
 	}
 
+	if (ctx->flags & TPARG_FL_TEVENT) {
+		int ret;
+
+		ret = parse_trace_event(varname, code, ctx);
+		if (ret < 0)
+			return ret;
+	}
+
 	if (ctx->flags & TPARG_FL_RETURN && !strcmp(varname, "$retval")) {
 		code->op = FETCH_OP_RETVAL;
 		/* Check whether the function return type is not void */

kernel/trace/trace_probe.c static int parse_btf_arg(char *varname, · line 743

Quoted 6 lines
kernel/trace/trace_probe.c +1 -1
@@ -672,7 +739,7 @@ static int parse_btf_arg(char *varname,
 		return 0;
 	}
 
-	if (!ctx->btf) {
+	if (!btf) {
 		ret = query_btf_context(ctx);

Oops, need a:

		btf = ctx->btf;

here!

-- Steve

Quoted 2 lines
kernel/trace/trace_probe.c +0 -0
 		if (ret < 0 || ctx->nr_params == 0) {
 			trace_probe_log_err(ctx->offset, NO_BTF_ENTRY);
#2 lore raw
Quoted 21 lines · On Tue, 19 May 2026 13:28:30 -0400 Steven Rostedt <[email protected]> wrote:
Quoted 18 lines · On Tue, 19 May 2026 13:01:44 -0400 Steven Rostedt <[email protected]> wrote:
kernel/trace/trace_probe.c +2 -1
@@ -636,11 +694,12 @@ static int parse_btf_arg(char *varname,
 	struct fetch_insn *code = *pcode;
 	const struct btf_param *params;
 	const struct btf_type *type;
+	struct btf *btf = ctx_btf(ctx);
 	char *field = NULL;
 	int i, is_ptr, ret;
 	u32 tid;
 
-	if (WARN_ON_ONCE(!ctx->funcname))
+	if (WARN_ON_ONCE(!ctx->funcname && !(ctx->flags & TPARG_FL_TYPECAST)))
 		return -EINVAL;
 
 	is_ptr = split_next_field(varname, &field, ctx);
@@ -653,6 +712,14 @@ static int parse_btf_arg(char *varname,
 		return -EOPNOTSUPP;
 	}
 
Quoted 6 lines

kernel/trace/trace_probe.c static int parse_btf_arg(char *varname, · line 720

Quoted 6 lines
kernel/trace/trace_probe.c +6 -0
+	if (ctx->flags & TPARG_FL_TEVENT) {
+		int ret;
+
+		ret = parse_trace_event(varname, code, ctx);
+		if (ret < 0)
+			return ret;

Actually, since typecasting is currently only for eprobes, I'm going to shuffle the code around a bit to only have the TYPECAST affect TEVENT, and do the jump to found_type from here.

-- Steve

Quoted 23 lines

kernel/trace/trace_probe.c static int parse_btf_arg(char *varname, · line 743

Quoted 12 lines
kernel/trace/trace_probe.c +3 -1
+	}
+
 	if (ctx->flags & TPARG_FL_RETURN && !strcmp(varname, "$retval")) {
 		code->op = FETCH_OP_RETVAL;
 		/* Check whether the function return type is not void */
@@ -672,7 +739,7 @@ static int parse_btf_arg(char *varname,
 		return 0;
 	}
 
-	if (!ctx->btf) {
+	if (!btf) {
 		ret = query_btf_context(ctx);  

Oops, need a:

		btf = ctx->btf;

here!

-- Steve

Quoted 2 lines
kernel/trace/trace_probe.c +0 -0
 		if (ret < 0 || ctx->nr_params == 0) {
 			trace_probe_log_err(ctx->offset, NO_BTF_ENTRY);  
#3 lore raw
In reply to Steven Rostedt
Thank you for your contribution! Sashiko AI review found 5 potential issue(s) to consider: - [High] Using `(STRUCT)` typecasts for non-eprobes leaves the base fetch instruction uninitialized…

Thank you for your contribution! Sashiko AI review found 5 potential issue(s) to consider:
- [High] Using (STRUCT) typecasts for non-eprobes leaves the base fetch instruction uninitialized (FETCH_OP_NOP), silently breaking the probe.
- [High] Prematurely clearing TPARG_FL_TYPECAST causes cross-BTF type confusion and breaks string fetches.
- [Medium] Erroneous caching of struct_btf restricts a single trace probe to using typecasts from only one BTF object, preventing cross-module lookups.
- [Medium] Cross-BTF string table access in parse_btf_arg() causes invalid string comparisons.
- [Low] clear_btf_context() fails to clear ctx->struct_btf after dropping its reference.
--

commit de4b6287a7069fde24b0e0f679a545a06fb9ae6c
Author: Steven Rostedt <[email protected]>

tracing/eprobes: Allow use of BTF names to dereference pointers

Add syntax to the parsing of eprobes to be able to typecast a trace event field that is a pointer to a structure.

Quoted 11 lines
kernel/trace/trace_probe.c +3 -0
index e1c73065dae51..2bc9f4ca8aa7e 100644
@@ -464,6 +470,27 @@ static const char *fetch_type_from_btf_type(struct btf *btf,
 	return NULL;
 }
 
+static int query_btf_struct(const char *sname, struct traceprobe_parse_context *ctx)
+{
+	int id;

kernel/trace/trace_probe.c static const char *fetch_type_from_btf_type(struct btf *btf, · line 485

Quoted 10 lines
kernel/trace/trace_probe.c +10 -0
+
+	if (!ctx->struct_btf) {
+		struct btf *btf;
+
+		id = bpf_find_btf_id(sname, BTF_KIND_STRUCT, &btf);
+		if (id < 0)
+			return id;
+		ctx->struct_btf = btf;
+	} else {
+		id = btf_find_by_name_kind(ctx->struct_btf, sname, BTF_KIND_STRUCT);

[Severity: Medium]
Does caching struct_btf here restrict a single trace probe event to using typecasts from only one BTF object?

If a probe attempts to typecast structures from two different kernel modules, wouldn't the lookup for the second module's struct fail because it only searches the cached BTF object of the first module (or vmlinux)?

Quoted 7 lines
kernel/trace/trace_probe.c +7 -0
+		if (id < 0)
+			return id;
+	}
+
+	ctx->last_struct = btf_type_by_id(ctx->struct_btf, id);
+	return 0;
+}

[ ... ]

kernel/trace/trace_probe.c static void clear_btf_context(struct traceprobe_parse_context *ctx) · line 547

Quoted 7 lines
kernel/trace/trace_probe.c +3 -0
@@ -515,6 +542,10 @@ static void clear_btf_context(struct traceprobe_parse_context *ctx)
 		ctx->params = NULL;
 		ctx->nr_params = 0;
 	}
+	if (ctx->struct_btf) {
+		btf_put(ctx->struct_btf);
+		ctx->last_struct = NULL;

[Severity: Low]
Should ctx->struct_btf be explicitly set to NULL after btf_put() drops the reference?

While the traceprobe_parse_context might be destroyed shortly after parsing completes, leaving a dangling pointer could potentially lead to a use-after-free and double-free if any future refactoring reuses the parsing context across multiple trace events.

Quoted 2 lines
kernel/trace/trace_probe.c +1 -0
+	}
 }

[ ... ]

Quoted 10 lines
kernel/trace/trace_probe.c +1 -1
@@ -672,7 +739,7 @@ static int parse_btf_arg(char *varname,
 		return 0;
 	}
 
-	if (!ctx->btf) {
+	if (!btf) {
 		ret = query_btf_context(ctx);
 		if (ret < 0 || ctx->nr_params == 0) {
 			trace_probe_log_err(ctx->offset, NO_BTF_ENTRY);

kernel/trace/trace_probe.c static int parse_btf_arg(char *varname, · line 752

Quoted 5 lines
kernel/trace/trace_probe.c +1 -1
@@ -682,7 +749,7 @@ static int parse_btf_arg(char *varname,
 	params = ctx->params;
 
 	for (i = 0; i < ctx->nr_params; i++) {
-		const char *name = btf_name_by_offset(ctx->btf, params[i].name_off);
+		const char *name = btf_name_by_offset(btf, params[i].name_off);

[Severity: Medium]
When parsing a typecast argument, btf evaluates to ctx->struct_btf. The parameter matching loop then calls btf_name_by_offset() using that btf.

Since params[i].name_off originates from the function's BTF (ctx->btf, populated by previous non-typecast arguments), could this lead to reading from the wrong string table if ctx->struct_btf is a different kernel module?

This might result in invalid string comparisons or returning (invalid-name-offset) if it reads out of bounds.

Quoted 3 lines
kernel/trace/trace_probe.c +0 -0
 		if (name && !strcmp(name, varname)) {
 			if (tparg_is_function_entry(ctx->flags)) {

[ ... ]

Quoted 15 lines
kernel/trace/trace_probe.c +11 -0
@@ -1231,6 +1301,30 @@ parse_probe_arg(char *arg, const struct fetch_type *type,
 				code->op = FETCH_OP_IMM;
 		}
 		break;
+	case '(':
+		tmp = strchr(arg, ')');
+		if (!tmp) {
+			trace_probe_log_err(ctx->offset + strlen(arg),
+					    DEREF_OPEN_BRACE);
+			return -EINVAL;
+		}
+		*tmp = '\0';
+		ret = query_btf_struct(arg + 1, ctx);
+		*tmp = ')';
+

kernel/trace/trace_probe.c parse_probe_arg(char *arg, const struct fetch_type *type, · line 1324

Quoted 10 lines
kernel/trace/trace_probe.c +10 -0
+		if (ret < 0) {
+			trace_probe_log_err(ctx->offset + 1, NO_PTR_STRCT);
+			return -EINVAL;
+		}
+
+		ctx->flags |= TPARG_FL_TYPECAST;
+		tmp++;
+
+		ctx->offset += tmp - arg;
+		ret = parse_btf_arg(tmp, pcode, end, ctx);

[Severity: High]
When parse_probe_arg() evaluates the (STRUCT) syntax for kprobes or fprobes, it sets TPARG_FL_TYPECAST and calls parse_btf_arg(). Inside parse_btf_arg(), ctx_btf() evaluates to ctx->struct_btf, so it bypasses query_btf_context() and jumps to found_type.

However, it doesn't seem to initialize code->op with a valid base fetch instruction like FETCH_OP_ARG or FETCH_OP_REG.

Could this leave the base instruction as FETCH_OP_NOP (0)? If so, it seems the base address would be 0, causing subsequent field dereferences to safely fail on 0 + offset due to copy_from_kernel_nofault(). This might allow unsupported syntax like (sk_buff)%di->dev to parse but silently fetch nothing.

kernel/trace/trace_probe.c parse_probe_arg(char *arg, const struct fetch_type *type, · line 1325

Quoted 1 line
kernel/trace/trace_probe.c +1 -0
+		ctx->flags &= ~TPARG_FL_TYPECAST;

[Severity: High]
The TPARG_FL_TYPECAST flag is cleared here, but ctx->last_type still points to a type originating from ctx->struct_btf.

Later in traceprobe_parse_probe_arg_body(), check_prepare_btf_string_fetch() is called. Since the flag is cleared, ctx_btf() will incorrectly return ctx->btf (vmlinux for kprobes, or NULL for eprobes).

Does this cause check_prepare_btf_string_fetch() to return 0 for eprobes and skip adding the necessary dummy FETCH_OP_DEREF for string fetches? Without this dummy dereference, string fetches on character pointers in eprobes might fail by reading the raw pointer value itself as a string.

Also, for kprobes, could this cause type confusion if btf_type_skip_modifiers() looks up a module's type ID inside the vmlinux BTF?

Quoted 2 lines
kernel/trace/trace_probe.c +2 -0
+		ctx->last_struct = NULL;
+		break;
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/20260519130144.40e71a00@fedora?part=1
author maintainer
#4 lore raw
Quoted 42 lines · On Tue, 19 May 2026 17:48:47 +0000 [email protected] wrote:

Thank you for your contribution! Sashiko AI review found 5 potential issue(s) to consider:
- [High] Using (STRUCT) typecasts for non-eprobes leaves the base fetch instruction uninitialized (FETCH_OP_NOP), silently breaking the probe.
- [High] Prematurely clearing TPARG_FL_TYPECAST causes cross-BTF type confusion and breaks string fetches.
- [Medium] Erroneous caching of struct_btf restricts a single trace probe to using typecasts from only one BTF object, preventing cross-module lookups.
- [Medium] Cross-BTF string table access in parse_btf_arg() causes invalid string comparisons.
- [Low] clear_btf_context() fails to clear ctx->struct_btf after dropping its reference.
--

commit de4b6287a7069fde24b0e0f679a545a06fb9ae6c
Author: Steven Rostedt <[email protected]>

tracing/eprobes: Allow use of BTF names to dereference pointers

Add syntax to the parsing of eprobes to be able to typecast a trace event field that is a pointer to a structure.

Quoted 11 lines
kernel/trace/trace_probe.c +3 -0
index e1c73065dae51..2bc9f4ca8aa7e 100644
@@ -464,6 +470,27 @@ static const char *fetch_type_from_btf_type(struct btf *btf,
 	return NULL;
 }
 
+static int query_btf_struct(const char *sname, struct traceprobe_parse_context *ctx)
+{
+	int id;

kernel/trace/trace_probe.c static const char *fetch_type_from_btf_type(struct btf *btf, · line 485

Quoted 10 lines
kernel/trace/trace_probe.c +10 -0
+
+	if (!ctx->struct_btf) {
+		struct btf *btf;
+
+		id = bpf_find_btf_id(sname, BTF_KIND_STRUCT, &btf);
+		if (id < 0)
+			return id;
+		ctx->struct_btf = btf;
+	} else {
+		id = btf_find_by_name_kind(ctx->struct_btf, sname, BTF_KIND_STRUCT);  

[Severity: Medium]
Does caching struct_btf here restrict a single trace probe event to using typecasts from only one BTF object?

Quoted 3 lines

If a probe attempts to typecast structures from two different kernel modules, wouldn't the lookup for the second module's struct fail because it only searches the cached BTF object of the first module (or vmlinux)?

Hmm, good point. I guess I should clear the struct_btf at the end of the '(' switch case.

Quoted 11 lines
Quoted 8 lines
kernel/trace/trace_probe.c +7 -0
+		if (id < 0)
+			return id;
+	}
+
+	ctx->last_struct = btf_type_by_id(ctx->struct_btf, id);
+	return 0;
+}  

[ ... ]

Quoted 11 lines

kernel/trace/trace_probe.c static void clear_btf_context(struct traceprobe_parse_context *ctx) · line 547

Quoted 7 lines
kernel/trace/trace_probe.c +3 -0
@@ -515,6 +542,10 @@ static void clear_btf_context(struct traceprobe_parse_context *ctx)
 		ctx->params = NULL;
 		ctx->nr_params = 0;
 	}
+	if (ctx->struct_btf) {
+		btf_put(ctx->struct_btf);
+		ctx->last_struct = NULL;  

[Severity: Low]
Should ctx->struct_btf be explicitly set to NULL after btf_put() drops the reference?

I'm thinking of dropping it in the '(' switch case.

Quoted 31 lines

While the traceprobe_parse_context might be destroyed shortly after parsing completes, leaving a dangling pointer could potentially lead to a use-after-free and double-free if any future refactoring reuses the parsing context across multiple trace events.

Quoted 2 lines
kernel/trace/trace_probe.c +1 -0
+	}
 }  

[ ... ]

Quoted 10 lines
kernel/trace/trace_probe.c +1 -1
@@ -672,7 +739,7 @@ static int parse_btf_arg(char *varname,
 		return 0;
 	}
 
-	if (!ctx->btf) {
+	if (!btf) {
 		ret = query_btf_context(ctx);
 		if (ret < 0 || ctx->nr_params == 0) {
 			trace_probe_log_err(ctx->offset, NO_BTF_ENTRY);

kernel/trace/trace_probe.c static int parse_btf_arg(char *varname, · line 752

Quoted 5 lines
kernel/trace/trace_probe.c +1 -1
@@ -682,7 +749,7 @@ static int parse_btf_arg(char *varname,
 	params = ctx->params;
 
 	for (i = 0; i < ctx->nr_params; i++) {
-		const char *name = btf_name_by_offset(ctx->btf, params[i].name_off);
+		const char *name = btf_name_by_offset(btf, params[i].name_off);  

[Severity: Medium]
When parsing a typecast argument, btf evaluates to ctx->struct_btf. The parameter matching loop then calls btf_name_by_offset() using that btf.

Quoted 4 lines

Since params[i].name_off originates from the function's BTF (ctx->btf, populated by previous non-typecast arguments), could this lead to reading from the wrong string table if ctx->struct_btf is a different kernel module?

The new patch will skip this for TYPECAST arguments. Well, at least for trace events.

Quoted 42 lines

This might result in invalid string comparisons or returning (invalid-name-offset) if it reads out of bounds.

Quoted 3 lines
kernel/trace/trace_probe.c +0 -0
 		if (name && !strcmp(name, varname)) {
 			if (tparg_is_function_entry(ctx->flags)) {  

[ ... ]

Quoted 15 lines
kernel/trace/trace_probe.c +11 -0
@@ -1231,6 +1301,30 @@ parse_probe_arg(char *arg, const struct fetch_type *type,
 				code->op = FETCH_OP_IMM;
 		}
 		break;
+	case '(':
+		tmp = strchr(arg, ')');
+		if (!tmp) {
+			trace_probe_log_err(ctx->offset + strlen(arg),
+					    DEREF_OPEN_BRACE);
+			return -EINVAL;
+		}
+		*tmp = '\0';
+		ret = query_btf_struct(arg + 1, ctx);
+		*tmp = ')';
+

kernel/trace/trace_probe.c parse_probe_arg(char *arg, const struct fetch_type *type, · line 1324

Quoted 10 lines
kernel/trace/trace_probe.c +10 -0
+		if (ret < 0) {
+			trace_probe_log_err(ctx->offset + 1, NO_PTR_STRCT);
+			return -EINVAL;
+		}
+
+		ctx->flags |= TPARG_FL_TYPECAST;
+		tmp++;
+
+		ctx->offset += tmp - arg;
+		ret = parse_btf_arg(tmp, pcode, end, ctx);  

[Severity: High]
When parse_probe_arg() evaluates the (STRUCT) syntax for kprobes or fprobes, it sets TPARG_FL_TYPECAST and calls parse_btf_arg(). Inside parse_btf_arg(), ctx_btf() evaluates to ctx->struct_btf, so it bypasses query_btf_context() and jumps to found_type.

Quoted 8 lines

However, it doesn't seem to initialize code->op with a valid base fetch instruction like FETCH_OP_ARG or FETCH_OP_REG.

Could this leave the base instruction as FETCH_OP_NOP (0)? If so, it seems the base address would be 0, causing subsequent field dereferences to safely fail on 0 + offset due to copy_from_kernel_nofault(). This might allow unsupported syntax like (sk_buff)%di->dev to parse but silently fetch nothing.

Yeah, I may have to force this not to work with function types.

Quoted 12 lines

kernel/trace/trace_probe.c parse_probe_arg(char *arg, const struct fetch_type *type, · line 1325

Quoted 2 lines
kernel/trace/trace_probe.c +1 -0
+		ctx->flags &= ~TPARG_FL_TYPECAST;  

[Severity: High]
The TPARG_FL_TYPECAST flag is cleared here, but ctx->last_type still points to a type originating from ctx->struct_btf.

Later in traceprobe_parse_probe_arg_body(), check_prepare_btf_string_fetch() is called. Since the flag is cleared, ctx_btf() will incorrectly return ctx->btf (vmlinux for kprobes, or NULL for eprobes).

Quoted 8 lines

Does this cause check_prepare_btf_string_fetch() to return 0 for eprobes and skip adding the necessary dummy FETCH_OP_DEREF for string fetches? Without this dummy dereference, string fetches on character pointers in eprobes might fail by reading the raw pointer value itself as a string.

Also, for kprobes, could this cause type confusion if btf_type_skip_modifiers() looks up a module's type ID inside the vmlinux BTF?

As mentioned above, I need to put and clear the last_struct here.

Thanks,

-- Steve

Quoted 4 lines
Quoted 3 lines
kernel/trace/trace_probe.c +2 -0
+		ctx->last_struct = NULL;
+		break;  
#5 lore raw
Quoted 45 lines · On Tue, 19 May 2026 14:17:26 -0400 Steven Rostedt <[email protected]> wrote:
Quoted 42 lines · On Tue, 19 May 2026 17:48:47 +0000 [email protected] wrote:

Thank you for your contribution! Sashiko AI review found 5 potential issue(s) to consider:
- [High] Using (STRUCT) typecasts for non-eprobes leaves the base fetch instruction uninitialized (FETCH_OP_NOP), silently breaking the probe.
- [High] Prematurely clearing TPARG_FL_TYPECAST causes cross-BTF type confusion and breaks string fetches.
- [Medium] Erroneous caching of struct_btf restricts a single trace probe to using typecasts from only one BTF object, preventing cross-module lookups.
- [Medium] Cross-BTF string table access in parse_btf_arg() causes invalid string comparisons.
- [Low] clear_btf_context() fails to clear ctx->struct_btf after dropping its reference.
--

commit de4b6287a7069fde24b0e0f679a545a06fb9ae6c
Author: Steven Rostedt <[email protected]>

tracing/eprobes: Allow use of BTF names to dereference pointers

Add syntax to the parsing of eprobes to be able to typecast a trace event field that is a pointer to a structure.

Quoted 11 lines
kernel/trace/trace_probe.c +3 -0
index e1c73065dae51..2bc9f4ca8aa7e 100644
@@ -464,6 +470,27 @@ static const char *fetch_type_from_btf_type(struct btf *btf,
 	return NULL;
 }
 
+static int query_btf_struct(const char *sname, struct traceprobe_parse_context *ctx)
+{
+	int id;

kernel/trace/trace_probe.c static const char *fetch_type_from_btf_type(struct btf *btf, · line 485

Quoted 10 lines
kernel/trace/trace_probe.c +10 -0
+
+	if (!ctx->struct_btf) {
+		struct btf *btf;
+
+		id = bpf_find_btf_id(sname, BTF_KIND_STRUCT, &btf);
+		if (id < 0)
+			return id;
+		ctx->struct_btf = btf;
+	} else {
+		id = btf_find_by_name_kind(ctx->struct_btf, sname, BTF_KIND_STRUCT);  

[Severity: Medium]
Does caching struct_btf here restrict a single trace probe event to using typecasts from only one BTF object?

Quoted 6 lines
Quoted 3 lines

If a probe attempts to typecast structures from two different kernel modules, wouldn't the lookup for the second module's struct fail because it only searches the cached BTF object of the first module (or vmlinux)?

Hmm, good point. I guess I should clear the struct_btf at the end of the '(' switch case.

Yes, it should be reset before parsing next argument.

Quoted 24 lines
Quoted 12 lines
Quoted 9 lines
kernel/trace/trace_probe.c +7 -0
+		if (id < 0)
+			return id;
+	}
+
+	ctx->last_struct = btf_type_by_id(ctx->struct_btf, id);
+	return 0;
+}  

[ ... ]

Quoted 11 lines

kernel/trace/trace_probe.c static void clear_btf_context(struct traceprobe_parse_context *ctx) · line 547

Quoted 7 lines
kernel/trace/trace_probe.c +3 -0
@@ -515,6 +542,10 @@ static void clear_btf_context(struct traceprobe_parse_context *ctx)
 		ctx->params = NULL;
 		ctx->nr_params = 0;
 	}
+	if (ctx->struct_btf) {
+		btf_put(ctx->struct_btf);
+		ctx->last_struct = NULL;  

[Severity: Low]
Should ctx->struct_btf be explicitly set to NULL after btf_put() drops the reference?

Quoted 1 line

I'm thinking of dropping it in the '(' switch case.

Can you consider making the '(' switch case part as a helper function because it depends on CONFIG_DEBUG_INFO_BTF?

Thanks,

Signature · 1 line
Masami Hiramatsu (Google) <[email protected]>
author maintainer
#6 lore raw
Quoted 12 lines · On Wed, 20 May 2026 15:20:21 +0900 Masami Hiramatsu (Google)…
Quoted 12 lines
Quoted 11 lines

kernel/trace/trace_probe.c static void clear_btf_context(struct traceprobe_parse_context *ctx) · line 547

Quoted 7 lines
kernel/trace/trace_probe.c +3 -0
@@ -515,6 +542,10 @@ static void clear_btf_context(struct traceprobe_parse_context *ctx)
 		ctx->params = NULL;
 		ctx->nr_params = 0;
 	}
+	if (ctx->struct_btf) {
+		btf_put(ctx->struct_btf);
+		ctx->last_struct = NULL;    

[Severity: Low]
Should ctx->struct_btf be explicitly set to NULL after btf_put() drops the reference?

Quoted 4 lines
Quoted 1 line

I'm thinking of dropping it in the '(' switch case.

Can you consider making the '(' switch case part as a helper function because it depends on CONFIG_DEBUG_INFO_BTF?

Should we just encapsulate that entire case statement with:

#ifdef CONFIG_DEBUG_INFO_BTF
[..]
#endif

?

-- Steve

#7 lore raw
Quoted 17 lines · On Wed, 20 May 2026 12:48:32 -0400 Steven Rostedt <[email protected]> wrote:
Quoted 14 lines · On Wed, 20 May 2026 15:20:21 +0900 Masami Hiramatsu (Google)…
Quoted 13 lines
Quoted 11 lines

kernel/trace/trace_probe.c static void clear_btf_context(struct traceprobe_parse_context *ctx) · line 547

Quoted 7 lines
kernel/trace/trace_probe.c +3 -0
@@ -515,6 +542,10 @@ static void clear_btf_context(struct traceprobe_parse_context *ctx)
 		ctx->params = NULL;
 		ctx->nr_params = 0;
 	}
+	if (ctx->struct_btf) {
+		btf_put(ctx->struct_btf);
+		ctx->last_struct = NULL;    

[Severity: Low]
Should ctx->struct_btf be explicitly set to NULL after btf_put() drops the reference?

I'm thinking of dropping it in the '(' switch case.

Quoted 8 lines
Quoted 2 lines

Can you consider making the '(' switch case part as a helper function because it depends on CONFIG_DEBUG_INFO_BTF?

Should we just encapsulate that entire case statement with:

#ifdef CONFIG_DEBUG_INFO_BTF
[..]
#endif

Yeah that is possible, and I rather like to make it a separate function for simplifying switch-case block for readability.

Thank you,

Quoted 6 lines

?

-- Steve

Signature · 1 line
Masami Hiramatsu (Google) <[email protected]>
author maintainer
#8 lore raw
Quoted 23 lines · On Thu, 21 May 2026 10:58:04 +0900 Masami Hiramatsu (Google)…
Quoted 20 lines · On Wed, 20 May 2026 12:48:32 -0400 Steven Rostedt <[email protected]> wrote:
Quoted 16 lines · On Wed, 20 May 2026 15:20:21 +0900 Masami Hiramatsu (Google)…
Quoted 12 lines
Quoted 11 lines

kernel/trace/trace_probe.c static void clear_btf_context(struct traceprobe_parse_context *ctx) · line 547

Quoted 7 lines
kernel/trace/trace_probe.c +3 -0
@@ -515,6 +542,10 @@ static void clear_btf_context(struct traceprobe_parse_context *ctx)
 		ctx->params = NULL;
 		ctx->nr_params = 0;
 	}
+	if (ctx->struct_btf) {
+		btf_put(ctx->struct_btf);
+		ctx->last_struct = NULL;      

[Severity: Low]
Should ctx->struct_btf be explicitly set to NULL after btf_put() drops the reference?

Quoted 1 line

I'm thinking of dropping it in the '(' switch case.

Can you consider making the '(' switch case part as a helper function because it depends on CONFIG_DEBUG_INFO_BTF?

Quoted 9 lines
Quoted 5 lines

Should we just encapsulate that entire case statement with:

#ifdef CONFIG_DEBUG_INFO_BTF
[..]
#endif

Yeah that is possible, and I rather like to make it a separate function for simplifying switch-case block for readability.

Hmm, but as a separate function, you mean something like this?

	case '(':
		ret = handle_typecast(...);
		break;

And have;

#ifdef CONFIG_DEBUG_INFO_BTF
static int handle_typecast(...)
{
[ do the real work here ]
}
#else
static int handle_typecast(...)

{
	trace_probe_log_err(ctx->offset, NOSUP_BTFARG);
	return -EOPNOTSUPP;
}

#endif

?

-- Steve

#9 lore raw
In reply to Steven Rostedt
kernel test robot noticed the following build errors:

Hi Steven,

kernel test robot noticed the following build errors:

[auto build test ERROR on trace/for-next]
[also build test ERROR on linus/master v7.1-rc4 next-20260519] [If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Steven-Rostedt/tracing-eprobes-Allow-use-of-BTF-names-to-dereference-pointers/20260520-011353
base:   https://git.kernel.org/pub/scm/linux/kernel/git/trace/linux-trace for-next
patch link:    https://lore.kernel.org/r/20260519130144.40e71a00%40fedora
patch subject: [PATCH v5] tracing/eprobes: Allow use of BTF names to dereference pointers
config: s390-randconfig-r071-20260520 (https://download.01.org/0day-ci/archive/20260520/[email protected]/config)
compiler: s390-linux-gcc (GCC) 8.5.0
smatch: v0.5.0-9185-gbcc58b9c
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20260520/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All error/warnings (new ones prefixed by >>):

kernel/trace/trace_probe.c: In function 'parse_probe_vars':

>> kernel/trace/trace_probe.c:1035:7: error: implicit declaration of function 'parse_trace_event'; did you mean 'parse_trace_event_arg'? [-Werror=implicit-function-declaration]
      if (parse_trace_event(arg, code, ctx) < 0)
          ^~~~~~~~~~~~~~~~~
          parse_trace_event_arg
   kernel/trace/trace_probe.c: In function 'sprint_nth_btf_arg':
>> kernel/trace/trace_probe.c:1803:20: error: implicit declaration of function 'ctx_btf' [-Werror=implicit-function-declaration]
     struct btf *btf = ctx_btf(ctx);
                       ^~~~~~~
>> kernel/trace/trace_probe.c:1803:20: warning: initialization of 'struct btf *' from 'int' makes pointer from integer without a cast [-Wint-conversion]
   At top level:
>> kernel/trace/trace_probe.c:318:12: warning: 'parse_trace_event_arg' defined but not used [-Wunused-function]

static int parse_trace_event_arg(char *arg, struct fetch_insn *code,
^~~~~~~~~~~~~~~~~~~~~
cc1: some warnings being treated as errors

vim +1035 kernel/trace/trace_probe.c

1020
1021 /* Parse $vars. @orig_arg points '$', which syncs to @ctx->offset */
1022 static int parse_probe_vars(char *orig_arg, const struct fetch_type *t,

  1023				    struct fetch_insn **pcode,
  1024				    struct fetch_insn *end,
  1025				    struct traceprobe_parse_context *ctx)
  1026	{
  1027		struct fetch_insn *code = *pcode;
  1028		int err = TP_ERR_BAD_VAR;
  1029		char *arg = orig_arg + 1;
  1030		unsigned long param;
  1031		int ret = 0;
  1032		int len;
  1033	
  1034		if (ctx->flags & TPARG_FL_TEVENT) {
> 1035			if (parse_trace_event(arg, code, ctx) < 0)

1036 goto inval;
1037 return 0;
1038 }
1039
1040 if (str_has_prefix(arg, "retval")) {
1041 if (!(ctx->flags & TPARG_FL_RETURN)) {
1042 err = TP_ERR_RETVAL_ON_PROBE;
1043 goto inval;
1044 }
1045 if (!(ctx->flags & TPARG_FL_KERNEL) ||
1046 !IS_ENABLED(CONFIG_PROBE_EVENTS_BTF_ARGS)) {
1047 code->op = FETCH_OP_RETVAL;
1048 return 0;
1049 }
1050 return parse_btf_arg(orig_arg, pcode, end, ctx);
1051 }
1052
1053 len = str_has_prefix(arg, "stack");
1054 if (len) {
1055
1056 if (arg[len] == '\0') {
1057 code->op = FETCH_OP_STACKP;
1058 return 0;
1059 }
1060
1061 if (isdigit(arg[len])) {
1062 ret = kstrtoul(arg + len, 10, &param);
1063 if (ret)
1064 goto inval;
1065
1066 if ((ctx->flags & TPARG_FL_KERNEL) &&
1067 param > PARAM_MAX_STACK) {
1068 err = TP_ERR_BAD_STACK_NUM;
1069 goto inval;
1070 }
1071 code->op = FETCH_OP_STACK;
1072 code->param = (unsigned int)param;
1073 return 0;
1074 }
1075 goto inval;
1076 }
1077
1078 if (strcmp(arg, "comm") == 0 || strcmp(arg, "COMM") == 0) {
1079 code->op = FETCH_OP_COMM;
1080 return 0;
1081 }
1082

--
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki

#10 lore raw
In reply to Steven Rostedt
kernel test robot noticed the following build errors:

Hi Steven,

kernel test robot noticed the following build errors:

[auto build test ERROR on trace/for-next]
[also build test ERROR on linus/master v7.1-rc4 next-20260519] [If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Steven-Rostedt/tracing-eprobes-Allow-use-of-BTF-names-to-dereference-pointers/20260520-011353
base:   https://git.kernel.org/pub/scm/linux/kernel/git/trace/linux-trace for-next
patch link:    https://lore.kernel.org/r/20260519130144.40e71a00%40fedora
patch subject: [PATCH v5] tracing/eprobes: Allow use of BTF names to dereference pointers
config: arc-defconfig (https://download.01.org/0day-ci/archive/20260520/[email protected]/config)
compiler: arc-linux-gcc (GCC) 15.2.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20260520/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All errors (new ones prefixed by >>):

kernel/trace/trace_probe.c: In function 'parse_probe_vars':
kernel/trace/trace_probe.c:1035:21: error: implicit declaration of function 'parse_trace_event'; did you mean 'parse_trace_event_arg'? [-Wimplicit-function-declaration]

    1035 |                 if (parse_trace_event(arg, code, ctx) < 0)
         |                     ^~~~~~~~~~~~~~~~~
         |                     parse_trace_event_arg
   kernel/trace/trace_probe.c: In function 'sprint_nth_btf_arg':
   kernel/trace/trace_probe.c:1803:27: error: implicit declaration of function 'ctx_btf' [-Wimplicit-function-declaration]
    1803 |         struct btf *btf = ctx_btf(ctx);
         |                           ^~~~~~~
>> kernel/trace/trace_probe.c:1803:27: error: initialization of 'struct btf *' from 'int' makes pointer from integer without a cast [-Wint-conversion]

kernel/trace/trace_probe.c: At top level:
kernel/trace/trace_probe.c:318:12: warning: 'parse_trace_event_arg' defined but not used [-Wunused-function]
318 | static int parse_trace_event_arg(char *arg, struct fetch_insn *code,
| ^~~~~~~~~~~~~~~~~~~~~

vim +1803 kernel/trace/trace_probe.c

1797
1798 static int sprint_nth_btf_arg(int idx, const char *type,
1799 char *buf, int bufsize,
1800 struct traceprobe_parse_context *ctx)
1801 {
1802 const char *name;

> 1803		struct btf *btf = ctx_btf(ctx);

1804 int ret;
1805
1806 if (idx >= ctx->nr_params) {
1807 trace_probe_log_err(0, NO_BTFARG);
1808 return -ENOENT;
1809 }
1810 name = btf_name_by_offset(btf, ctx->params[idx].name_off);
1811 if (!name) {
1812 trace_probe_log_err(0, NO_BTF_ENTRY);
1813 return -ENOENT;
1814 }
1815 ret = snprintf(buf, bufsize, "%s%s", name, type);
1816 if (ret >= bufsize) {
1817 trace_probe_log_err(0, ARGS_2LONG);
1818 return -E2BIG;
1819 }
1820 return ret;
1821 }
1822

--
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki