Skip to contentSkip to conversation

perf trace: Refactor augmented_raw_syscalls using bpf_loop

PATCH Superseded Viktor Malik 5 participants 18 messages Jun 23 – Jul 3

Subsystems (3)

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 perf trace: Refactor augmented_raw_syscalls using bpf_loop

The loop for processing syscall args in augment_raw_syscalls has a history of breaking with Clang updates, see e.g. commit 013eb043f37b ("perf trace: Fix BPF loading failure (-E2BIG)") from Clang 15 to 16.

Now, a similar thing happened between Clang 21 and 22. While the issue is mitigated on the main line by a recent verifier update, it remains broken on the 6.12 and 6.18 stable branches:

[linux-6.18.y]# sudo perf trace true
libbpf: prog 'sys_enter': BPF program load failed: -E2BIG
libbpf: prog 'sys_enter': -- BEGIN PROG LOAD LOG --
[...]
BPF program is too large. Processed 1000001 insn
processed 1000001 insns (limit 1000000) max_states_per_insn 40 total_states 37941 peak_states 232 mark_read 0 -- END PROG LOAD LOG --
libbpf: prog 'sys_enter': failed to load: -E2BIG
libbpf: failed to load object 'augmented_raw_syscalls_bpf'
libbpf: failed to load BPF skeleton 'augmented_raw_syscalls_bpf': -E2BIG
Error: failed to get syscall or beauty map fd
[...]

The reason is that the loop is quite complex and the BPF verifier often struggles to prove that it terminates.

Fix the issue by refactoring the loop body into a callback function and calling the bpf_loop helper. This should prevent future breakages of this kind since the callback function has no loops. It also allows to drop a few artificial checks to help the verifier, including the changes introduced by 013eb043f37b.

Fixes a68fd6a6cdd3 ("perf trace: Collect augmented data using BPF")
Fixes 013eb043f37b ("perf trace: Fix BPF loading failure (-E2BIG)")
---
 .../bpf_skel/augmented_raw_syscalls.bpf.c     | 157 +++++++++++-------
 1 file changed, 96 insertions(+), 61 deletions(-)
tools/perf/util/bpf_skel/augmented_raw_syscalls.bpf.c +96 -61
index 2a6e61864ee0..6d553ed3ac23 100644
@@ -429,15 +429,96 @@ static bool pid_filter__has(struct pids_filtered *pids, pid_t pid)
 	return bpf_map_lookup_elem(pids, &pid) != NULL;
 }
 
+struct args_loop_ctx {
+	struct syscall_enter_args *args;
+	unsigned int *beauty_map;
+	void *payload_offset;
+	int value_size;
+	u64 *output;
+	bool *do_output;
+};
+
+static long process_arg_cb(u64 i, void *ctx)
+{
+	/*
+	 * Determine what type of argument and how many bytes to read from user space, using the
+	 * value in the beauty_map. This is the relation of parameter type and its corresponding
+	 * value in the beauty map, and how many bytes we read eventually:
+	 *
+	 * string: 1			      -> size of string
+	 * struct: size of struct	      -> size of struct
+	 * buffer: -1 * (index of paired len) -> value of paired len (maximum: TRACE_AUG_MAX_BUF)
+	 */
+	struct augmented_arg *augmented_arg;
+	struct args_loop_ctx *loop_ctx;
+	int aug_size, size, index;
+	bool augmented;
+	void *arg;
+
+	/* Bounds check for the below map access to help the verifier */
+	if (i < 0 || i >= 6)
+		return 1;
+
+	loop_ctx = (struct args_loop_ctx *)ctx;
+	arg = (void *)loop_ctx->args->args[i];
+	augmented = false;
+	size = loop_ctx->beauty_map[i];
+	aug_size = size; /* size of the augmented data read from user space */
+	augmented_arg = (struct augmented_arg *)loop_ctx->payload_offset;
+
+	if (size == 0 || arg == NULL)
+		return 0; /* continue */
+
+	if (size == 1) { /* string */
+		aug_size = bpf_probe_read_user_str(augmented_arg->value, loop_ctx->value_size, arg);
+		augmented = true;
+	} else if (size > 0 && size <= loop_ctx->value_size) { /* struct */
+		if (!bpf_probe_read_user(augmented_arg->value, size, arg))
+			augmented = true;
+	} else if (size < 0 && size >= -6) { /* buffer */
+		index = -(size + 1);
+		barrier_var(index); // Prevent clang (noticed with v18) from removing the &= 7 trick.
+		index &= 7;	    // Satisfy the bounds checking with the verifier in some kernels.
+		aug_size = loop_ctx->args->args[index];
+
+		if (aug_size > TRACE_AUG_MAX_BUF)
+			aug_size = TRACE_AUG_MAX_BUF;
+
+		if (aug_size > 0) {
+			if (!bpf_probe_read_user(augmented_arg->value, aug_size, arg))
+				augmented = true;
+		}
+	}
+
+	/* Augmented data size is limited to sizeof(augmented_arg->unnamed union with value field) */
+	if (aug_size > loop_ctx->value_size)
+		aug_size = loop_ctx->value_size;
+
+	/* write data to payload */
+	if (augmented) {
+		int written = offsetof(struct augmented_arg, value) + aug_size;
+
+		if (written < 0 || written > sizeof(struct augmented_arg))
+			return 1; /* break */
+
+		augmented_arg->size = aug_size;
+		*loop_ctx->output += written;
+		loop_ctx->payload_offset += written;
+		*loop_ctx->do_output = true;
+	}
+
+	return 0;
+}
+
 static int augment_sys_enter(void *ctx, struct syscall_enter_args *args)
 {
-	bool augmented, do_output = false;
-	int zero = 0, index, value_size = sizeof(struct augmented_arg) - offsetof(struct augmented_arg, value);
+	bool do_output = false;
+	int zero = 0, value_size = sizeof(struct augmented_arg) - offsetof(struct augmented_arg, value);
 	u64 output = 0; /* has to be u64, otherwise it won't pass the verifier */
-	s64 aug_size, size;
 	unsigned int nr, *beauty_map;
 	struct beauty_payload_enter *payload;
-	void *arg, *payload_offset;
+	void *payload_offset;
+	long iters;
 
 	/* fall back to do predefined tail call */
 	if (args == NULL)
@@ -457,63 +538,17 @@ static int augment_sys_enter(void *ctx, struct syscall_enter_args *args)
 	/* copy the sys_enter header, which has the syscall_nr */
 	__builtin_memcpy(&payload->args, args, sizeof(struct syscall_enter_args));
 
-	/*
-	 * Determine what type of argument and how many bytes to read from user space, using the
-	 * value in the beauty_map. This is the relation of parameter type and its corresponding
-	 * value in the beauty map, and how many bytes we read eventually:
-	 *
-	 * string: 1			      -> size of string
-	 * struct: size of struct	      -> size of struct
-	 * buffer: -1 * (index of paired len) -> value of paired len (maximum: TRACE_AUG_MAX_BUF)
-	 */
-	for (int i = 0; i < 6; i++) {
-		arg = (void *)args->args[i];
-		augmented = false;
-		size = beauty_map[i];
-		aug_size = size; /* size of the augmented data read from user space */
-
-		if (size == 0 || arg == NULL)
-			continue;
-
-		if (size == 1) { /* string */
-			aug_size = bpf_probe_read_user_str(((struct augmented_arg *)payload_offset)->value, value_size, arg);
-			/* minimum of 0 to pass the verifier */
-			if (aug_size < 0)
-				aug_size = 0;
-
-			augmented = true;
-		} else if (size > 0 && size <= value_size) { /* struct */
-			if (!bpf_probe_read_user(((struct augmented_arg *)payload_offset)->value, size, arg))
-				augmented = true;
-		} else if ((int)size < 0 && size >= -6) { /* buffer */
-			index = -(size + 1);
-			barrier_var(index); // Prevent clang (noticed with v18) from removing the &= 7 trick.
-			index &= 7;	    // Satisfy the bounds checking with the verifier in some kernels.
-			aug_size = args->args[index] > TRACE_AUG_MAX_BUF ? TRACE_AUG_MAX_BUF : args->args[index];
-
-			if (aug_size > 0) {
-				if (!bpf_probe_read_user(((struct augmented_arg *)payload_offset)->value, aug_size, arg))
-					augmented = true;
-			}
-		}
-
-		/* Augmented data size is limited to sizeof(augmented_arg->unnamed union with value field) */
-		if (aug_size > value_size)
-			aug_size = value_size;
-
-		/* write data to payload */
-		if (augmented) {
-			int written = offsetof(struct augmented_arg, value) + aug_size;
-
-			if (written < 0 || written > sizeof(struct augmented_arg))
-				return 1;
-
-			((struct augmented_arg *)payload_offset)->size = aug_size;
-			output += written;
-			payload_offset += written;
-			do_output = true;
-		}
-	}
+	struct args_loop_ctx loop_ctx = {
+		.args = args,
+		.beauty_map = beauty_map,
+		.payload_offset = payload_offset,
+		.value_size = value_size,
+		.output = &output,
+		.do_output = &do_output
+	};
+	iters = bpf_loop(6, process_arg_cb, &loop_ctx, 0);
+	if (iters != 6)
+		return 1;
 
 	if (!do_output || (sizeof(struct syscall_enter_args) + output) > sizeof(struct beauty_payload_enter))
 		return 1;
Signature · 1 line
2.54.0
#1 lore raw
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [Medium] Removal of the `aug_size < 0` check causes negative BPF helper error codes to corrupt the perf…

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [Medium] Removal of the aug_size < 0 check causes negative BPF helper error codes to corrupt the perf event payload and abort tracing.
- [Medium] Truncation of 64-bit syscall buffer lengths to a 32-bit signed integer silently drops buffer augmentations or bypasses bounds checks. --

commit 9622318b331a56255744b4baafdc95c2083ba210
Author: Viktor Malik <[email protected]>
perf trace: Refactor augmented_raw_syscalls using bpf_loop

This patch refactors the loop body in augment_raw_syscalls into a callback function to be used with the bpf_loop helper. This helps the BPF verifier prove termination and avoids -E2BIG load failures.

Quoted 3 lines
tools/perf/util/bpf_skel/augmented_raw_syscalls.bpf.c +0 -0

[ ... ]

Quoted 2 lines
tools/perf/util/bpf_skel/augmented_raw_syscalls.bpf.c +2 -0
+static long process_arg_cb(u64 i, void *ctx)
+{

[ ... ]

Quoted 5 lines
tools/perf/util/bpf_skel/augmented_raw_syscalls.bpf.c +5 -0
+	struct augmented_arg *augmented_arg;
+	struct args_loop_ctx *loop_ctx;
+	int aug_size, size, index;
+	bool augmented;
+	void *arg;

[ ... ]

tools/perf/util/bpf_skel/augmented_raw_syscalls.bpf.c static bool pid_filter__has(struct pids_filtered *pids, pid_t pid) · line 473

Quoted 2 lines
tools/perf/util/bpf_skel/augmented_raw_syscalls.bpf.c +2 -0
+	if (size == 1) { /* string */
+		aug_size = bpf_probe_read_user_str(augmented_arg->value, loop_ctx->value_size, arg);

[Severity: Medium]
Since bpf_probe_read_user_str() can return a negative error code like -EFAULT, does the removal of the previous clamping check cause an issue here?

If a negative error code is assigned to aug_size, it might result in a negative size being added to offsetof() later, which could abort the loop prematurely or misalign payload offsets:

    int written = offsetof(struct augmented_arg, value) + aug_size;

    if (written < 0 || written > sizeof(struct augmented_arg))
        return 1; /* break */

tools/perf/util/bpf_skel/augmented_raw_syscalls.bpf.c static bool pid_filter__has(struct pids_filtered *pids, pid_t pid) · line 482

Quoted 9 lines
tools/perf/util/bpf_skel/augmented_raw_syscalls.bpf.c +9 -0
+		augmented = true;
+	} else if (size > 0 && size <= loop_ctx->value_size) { /* struct */
+		if (!bpf_probe_read_user(augmented_arg->value, size, arg))
+			augmented = true;
+	} else if (size < 0 && size >= -6) { /* buffer */
+		index = -(size + 1);
+		barrier_var(index); // Prevent clang (noticed with v18) from removing the &= 7 trick.
+		index &= 7;	    // Satisfy the bounds checking with the verifier in some kernels.
+		aug_size = loop_ctx->args->args[index];

[Severity: Medium]
Does assigning the 64-bit user-provided buffer length to a 32-bit signed integer (aug_size) cause truncation before bounds checking is performed?

If a length over 2GB is provided, it might wrap to a negative number and fail the subsequent aug_size > 0 check. Values over 4GB could wrap to small positive numbers, bypass the max buffer size clamp, and result in capturing fewer bytes than expected.

Quoted 9 lines
tools/perf/util/bpf_skel/augmented_raw_syscalls.bpf.c +9 -0
+
+		if (aug_size > TRACE_AUG_MAX_BUF)
+			aug_size = TRACE_AUG_MAX_BUF;
+
+		if (aug_size > 0) {
+			if (!bpf_probe_read_user(augmented_arg->value, aug_size, arg))
+				augmented = true;
+		}
+	}

[ ... ]

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
#2 lore raw
In reply to Viktor Malik
Quoted 203 lines · Viktor Malik

The loop for processing syscall args in augment_raw_syscalls has a history of breaking with Clang updates, see e.g. commit 013eb043f37b ("perf trace: Fix BPF loading failure (-E2BIG)") from Clang 15 to 16.

Now, a similar thing happened between Clang 21 and 22. While the issue is mitigated on the main line by a recent verifier update, it remains broken on the 6.12 and 6.18 stable branches:

[linux-6.18.y]# sudo perf trace true
libbpf: prog 'sys_enter': BPF program load failed: -E2BIG
libbpf: prog 'sys_enter': -- BEGIN PROG LOAD LOG --
[...]
BPF program is too large. Processed 1000001 insn
processed 1000001 insns (limit 1000000) max_states_per_insn 40 total_states 37941 peak_states 232 mark_read 0 -- END PROG LOAD LOG --
libbpf: prog 'sys_enter': failed to load: -E2BIG
libbpf: failed to load object 'augmented_raw_syscalls_bpf'
libbpf: failed to load BPF skeleton 'augmented_raw_syscalls_bpf': -E2BIG
Error: failed to get syscall or beauty map fd
[...]

The reason is that the loop is quite complex and the BPF verifier often struggles to prove that it terminates.

Fix the issue by refactoring the loop body into a callback function and calling the bpf_loop helper. This should prevent future breakages of this kind since the callback function has no loops. It also allows to drop a few artificial checks to help the verifier, including the changes introduced by 013eb043f37b.

Fixes a68fd6a6cdd3 ("perf trace: Collect augmented data using BPF")
Fixes 013eb043f37b ("perf trace: Fix BPF loading failure (-E2BIG)")
---
 .../bpf_skel/augmented_raw_syscalls.bpf.c     | 157 +++++++++++-------
 1 file changed, 96 insertions(+), 61 deletions(-)
tools/perf/util/bpf_skel/augmented_raw_syscalls.bpf.c +85 -60
index 2a6e61864ee0..6d553ed3ac23 100644
@@ -429,15 +429,96 @@ static bool pid_filter__has(struct pids_filtered *pids, pid_t pid)
 	return bpf_map_lookup_elem(pids, &pid) != NULL;
 }
 
+struct args_loop_ctx {
+	struct syscall_enter_args *args;
+	unsigned int *beauty_map;
+	void *payload_offset;
+	int value_size;
+	u64 *output;
+	bool *do_output;
+};
+
+static long process_arg_cb(u64 i, void *ctx)
+{
+	/*
+	 * Determine what type of argument and how many bytes to read from user space, using the
+	 * value in the beauty_map. This is the relation of parameter type and its corresponding
+	 * value in the beauty map, and how many bytes we read eventually:
+	 *
+	 * string: 1			      -> size of string
+	 * struct: size of struct	      -> size of struct
+	 * buffer: -1 * (index of paired len) -> value of paired len (maximum: TRACE_AUG_MAX_BUF)
+	 */
+	struct augmented_arg *augmented_arg;
+	struct args_loop_ctx *loop_ctx;
+	int aug_size, size, index;
+	bool augmented;
+	void *arg;
+
+	/* Bounds check for the below map access to help the verifier */
+	if (i < 0 || i >= 6)
+		return 1;
+
+	loop_ctx = (struct args_loop_ctx *)ctx;
+	arg = (void *)loop_ctx->args->args[i];
+	augmented = false;
+	size = loop_ctx->beauty_map[i];
+	aug_size = size; /* size of the augmented data read from user space */
+	augmented_arg = (struct augmented_arg *)loop_ctx->payload_offset;
+
+	if (size == 0 || arg == NULL)
+		return 0; /* continue */
+
+	if (size == 1) { /* string */
+		aug_size = bpf_probe_read_user_str(augmented_arg->value, loop_ctx->value_size, arg);
+		augmented = true;
+	} else if (size > 0 && size <= loop_ctx->value_size) { /* struct */
+		if (!bpf_probe_read_user(augmented_arg->value, size, arg))
+			augmented = true;
+	} else if (size < 0 && size >= -6) { /* buffer */
+		index = -(size + 1);
+		barrier_var(index); // Prevent clang (noticed with v18) from removing the &= 7 trick.
+		index &= 7;	    // Satisfy the bounds checking with the verifier in some kernels.
+		aug_size = loop_ctx->args->args[index];
+
+		if (aug_size > TRACE_AUG_MAX_BUF)
+			aug_size = TRACE_AUG_MAX_BUF;
+
+		if (aug_size > 0) {
+			if (!bpf_probe_read_user(augmented_arg->value, aug_size, arg))
+				augmented = true;
+		}
+	}
+
+	/* Augmented data size is limited to sizeof(augmented_arg->unnamed union with value field) */
+	if (aug_size > loop_ctx->value_size)
+		aug_size = loop_ctx->value_size;
+
+	/* write data to payload */
+	if (augmented) {
+		int written = offsetof(struct augmented_arg, value) + aug_size;
+
+		if (written < 0 || written > sizeof(struct augmented_arg))
+			return 1; /* break */
+
+		augmented_arg->size = aug_size;
+		*loop_ctx->output += written;
+		loop_ctx->payload_offset += written;
+		*loop_ctx->do_output = true;
+	}
+
+	return 0;
+}
+
 static int augment_sys_enter(void *ctx, struct syscall_enter_args *args)
 {
-	bool augmented, do_output = false;
-	int zero = 0, index, value_size = sizeof(struct augmented_arg) - offsetof(struct augmented_arg, value);
+	bool do_output = false;
+	int zero = 0, value_size = sizeof(struct augmented_arg) - offsetof(struct augmented_arg, value);
 	u64 output = 0; /* has to be u64, otherwise it won't pass the verifier */
-	s64 aug_size, size;
 	unsigned int nr, *beauty_map;
 	struct beauty_payload_enter *payload;
-	void *arg, *payload_offset;
+	void *payload_offset;
+	long iters;
 
 	/* fall back to do predefined tail call */
 	if (args == NULL)
@@ -457,63 +538,17 @@ static int augment_sys_enter(void *ctx, struct syscall_enter_args *args)
 	/* copy the sys_enter header, which has the syscall_nr */
 	__builtin_memcpy(&payload->args, args, sizeof(struct syscall_enter_args));
 
-	/*
-	 * Determine what type of argument and how many bytes to read from user space, using the
-	 * value in the beauty_map. This is the relation of parameter type and its corresponding
-	 * value in the beauty map, and how many bytes we read eventually:
-	 *
-	 * string: 1			      -> size of string
-	 * struct: size of struct	      -> size of struct
-	 * buffer: -1 * (index of paired len) -> value of paired len (maximum: TRACE_AUG_MAX_BUF)
-	 */
-	for (int i = 0; i < 6; i++) {
-		arg = (void *)args->args[i];
-		augmented = false;
-		size = beauty_map[i];
-		aug_size = size; /* size of the augmented data read from user space */
-
-		if (size == 0 || arg == NULL)
-			continue;
-
-		if (size == 1) { /* string */
-			aug_size = bpf_probe_read_user_str(((struct augmented_arg *)payload_offset)->value, value_size, arg);
-			/* minimum of 0 to pass the verifier */
-			if (aug_size < 0)
-				aug_size = 0;
-
-			augmented = true;
-		} else if (size > 0 && size <= value_size) { /* struct */
-			if (!bpf_probe_read_user(((struct augmented_arg *)payload_offset)->value, size, arg))
-				augmented = true;
-		} else if ((int)size < 0 && size >= -6) { /* buffer */
-			index = -(size + 1);
-			barrier_var(index); // Prevent clang (noticed with v18) from removing the &= 7 trick.
-			index &= 7;	    // Satisfy the bounds checking with the verifier in some kernels.
-			aug_size = args->args[index] > TRACE_AUG_MAX_BUF ? TRACE_AUG_MAX_BUF : args->args[index];
-
-			if (aug_size > 0) {
-				if (!bpf_probe_read_user(((struct augmented_arg *)payload_offset)->value, aug_size, arg))
-					augmented = true;
-			}
-		}
-
-		/* Augmented data size is limited to sizeof(augmented_arg->unnamed union with value field) */
-		if (aug_size > value_size)
-			aug_size = value_size;
-
-		/* write data to payload */
-		if (augmented) {
-			int written = offsetof(struct augmented_arg, value) + aug_size;
-
-			if (written < 0 || written > sizeof(struct augmented_arg))
-				return 1;
-
-			((struct augmented_arg *)payload_offset)->size = aug_size;
-			output += written;
-			payload_offset += written;
-			do_output = true;
-		}

tools/perf/util/bpf_skel/augmented_raw_syscalls.bpf.c static int augment_sys_enter(void *ctx, struct syscall_enter_args *args) · line 549

Quoted 10 lines
tools/perf/util/bpf_skel/augmented_raw_syscalls.bpf.c +9 -1
-	}
+	struct args_loop_ctx loop_ctx = {
+		.args = args,
+		.beauty_map = beauty_map,
+		.payload_offset = payload_offset,
+		.value_size = value_size,
+		.output = &output,
+		.do_output = &do_output
+	};
+	iters = bpf_loop(6, process_arg_cb, &loop_ctx, 0);

bpf_loop() is old and generally not recommended. Please use bpf_for() then the diff will be one line change and can scale to any number of args. Not just 6.

maintainer
#3 lore raw

Hello,

Quoted 22 lines · Alexei Starovoitov
Quoted 21 lines · Viktor Malik

The loop for processing syscall args in augment_raw_syscalls has a history of breaking with Clang updates, see e.g. commit 013eb043f37b ("perf trace: Fix BPF loading failure (-E2BIG)") from Clang 15 to 16.

Now, a similar thing happened between Clang 21 and 22. While the issue is mitigated on the main line by a recent verifier update, it remains broken on the 6.12 and 6.18 stable branches:

[linux-6.18.y]# sudo perf trace true
libbpf: prog 'sys_enter': BPF program load failed: -E2BIG
libbpf: prog 'sys_enter': -- BEGIN PROG LOAD LOG --
[...]
BPF program is too large. Processed 1000001 insn
processed 1000001 insns (limit 1000000) max_states_per_insn 40 total_states 37941 peak_states 232 mark_read 0 -- END PROG LOAD LOG --
libbpf: prog 'sys_enter': failed to load: -E2BIG
libbpf: failed to load object 'augmented_raw_syscalls_bpf'
libbpf: failed to load BPF skeleton 'augmented_raw_syscalls_bpf': -E2BIG
Error: failed to get syscall or beauty map fd
[...]

Quoted 8 lines
Quoted 8 lines

The reason is that the loop is quite complex and the BPF verifier often struggles to prove that it terminates.

Fix the issue by refactoring the loop body into a callback function and calling the bpf_loop helper. This should prevent future breakages of this kind since the callback function has no loops. It also allows to drop a few artificial checks to help the verifier, including the changes introduced by 013eb043f37b.

Thanks for working on this. I encountered this issue before and never found time to take a deeper look yet.

Quoted 180 lines
Quoted 180 lines
Fixes a68fd6a6cdd3 ("perf trace: Collect augmented data using BPF")
Fixes 013eb043f37b ("perf trace: Fix BPF loading failure (-E2BIG)")
---
 .../bpf_skel/augmented_raw_syscalls.bpf.c     | 157 +++++++++++-------
 1 file changed, 96 insertions(+), 61 deletions(-)
tools/perf/util/bpf_skel/augmented_raw_syscalls.bpf.c +90 -61
index 2a6e61864ee0..6d553ed3ac23 100644
@@ -429,15 +429,96 @@ static bool pid_filter__has(struct pids_filtered *pids, pid_t pid)
 	return bpf_map_lookup_elem(pids, &pid) != NULL;
 }
 
+struct args_loop_ctx {
+	struct syscall_enter_args *args;
+	unsigned int *beauty_map;
+	void *payload_offset;
+	int value_size;
+	u64 *output;
+	bool *do_output;
+};
+
+static long process_arg_cb(u64 i, void *ctx)
+{
+	/*
+	 * Determine what type of argument and how many bytes to read from user space, using the
+	 * value in the beauty_map. This is the relation of parameter type and its corresponding
+	 * value in the beauty map, and how many bytes we read eventually:
+	 *
+	 * string: 1			      -> size of string
+	 * struct: size of struct	      -> size of struct
+	 * buffer: -1 * (index of paired len) -> value of paired len (maximum: TRACE_AUG_MAX_BUF)
+	 */
+	struct augmented_arg *augmented_arg;
+	struct args_loop_ctx *loop_ctx;
+	int aug_size, size, index;
+	bool augmented;
+	void *arg;
+
+	/* Bounds check for the below map access to help the verifier */
+	if (i < 0 || i >= 6)
+		return 1;
+
+	loop_ctx = (struct args_loop_ctx *)ctx;
+	arg = (void *)loop_ctx->args->args[i];
+	augmented = false;
+	size = loop_ctx->beauty_map[i];
+	aug_size = size; /* size of the augmented data read from user space */
+	augmented_arg = (struct augmented_arg *)loop_ctx->payload_offset;
+
+	if (size == 0 || arg == NULL)
+		return 0; /* continue */
+
+	if (size == 1) { /* string */
+		aug_size = bpf_probe_read_user_str(augmented_arg->value, loop_ctx->value_size, arg);
+		augmented = true;
+	} else if (size > 0 && size <= loop_ctx->value_size) { /* struct */
+		if (!bpf_probe_read_user(augmented_arg->value, size, arg))
+			augmented = true;
+	} else if (size < 0 && size >= -6) { /* buffer */
+		index = -(size + 1);
+		barrier_var(index); // Prevent clang (noticed with v18) from removing the &= 7 trick.
+		index &= 7;	    // Satisfy the bounds checking with the verifier in some kernels.
+		aug_size = loop_ctx->args->args[index];
+
+		if (aug_size > TRACE_AUG_MAX_BUF)
+			aug_size = TRACE_AUG_MAX_BUF;
+
+		if (aug_size > 0) {
+			if (!bpf_probe_read_user(augmented_arg->value, aug_size, arg))
+				augmented = true;
+		}
+	}
+
+	/* Augmented data size is limited to sizeof(augmented_arg->unnamed union with value field) */
+	if (aug_size > loop_ctx->value_size)
+		aug_size = loop_ctx->value_size;
+
+	/* write data to payload */
+	if (augmented) {
+		int written = offsetof(struct augmented_arg, value) + aug_size;
+
+		if (written < 0 || written > sizeof(struct augmented_arg))
+			return 1; /* break */
+
+		augmented_arg->size = aug_size;
+		*loop_ctx->output += written;
+		loop_ctx->payload_offset += written;
+		*loop_ctx->do_output = true;
+	}
+
+	return 0;
+}
+
 static int augment_sys_enter(void *ctx, struct syscall_enter_args *args)
 {
-	bool augmented, do_output = false;
-	int zero = 0, index, value_size = sizeof(struct augmented_arg) - offsetof(struct augmented_arg, value);
+	bool do_output = false;
+	int zero = 0, value_size = sizeof(struct augmented_arg) - offsetof(struct augmented_arg, value);
 	u64 output = 0; /* has to be u64, otherwise it won't pass the verifier */
-	s64 aug_size, size;
 	unsigned int nr, *beauty_map;
 	struct beauty_payload_enter *payload;
-	void *arg, *payload_offset;
+	void *payload_offset;
+	long iters;
 
 	/* fall back to do predefined tail call */
 	if (args == NULL)
@@ -457,63 +538,17 @@ static int augment_sys_enter(void *ctx, struct syscall_enter_args *args)
 	/* copy the sys_enter header, which has the syscall_nr */
 	__builtin_memcpy(&payload->args, args, sizeof(struct syscall_enter_args));
 
-	/*
-	 * Determine what type of argument and how many bytes to read from user space, using the
-	 * value in the beauty_map. This is the relation of parameter type and its corresponding
-	 * value in the beauty map, and how many bytes we read eventually:
-	 *
-	 * string: 1			      -> size of string
-	 * struct: size of struct	      -> size of struct
-	 * buffer: -1 * (index of paired len) -> value of paired len (maximum: TRACE_AUG_MAX_BUF)
-	 */
-	for (int i = 0; i < 6; i++) {
-		arg = (void *)args->args[i];
-		augmented = false;
-		size = beauty_map[i];
-		aug_size = size; /* size of the augmented data read from user space */
-
-		if (size == 0 || arg == NULL)
-			continue;
-
-		if (size == 1) { /* string */
-			aug_size = bpf_probe_read_user_str(((struct augmented_arg *)payload_offset)->value, value_size, arg);
-			/* minimum of 0 to pass the verifier */
-			if (aug_size < 0)
-				aug_size = 0;
-
-			augmented = true;
-		} else if (size > 0 && size <= value_size) { /* struct */
-			if (!bpf_probe_read_user(((struct augmented_arg *)payload_offset)->value, size, arg))
-				augmented = true;
-		} else if ((int)size < 0 && size >= -6) { /* buffer */
-			index = -(size + 1);
-			barrier_var(index); // Prevent clang (noticed with v18) from removing the &= 7 trick.
-			index &= 7;	    // Satisfy the bounds checking with the verifier in some kernels.
-			aug_size = args->args[index] > TRACE_AUG_MAX_BUF ? TRACE_AUG_MAX_BUF : args->args[index];
-
-			if (aug_size > 0) {
-				if (!bpf_probe_read_user(((struct augmented_arg *)payload_offset)->value, aug_size, arg))
-					augmented = true;
-			}
-		}
-
-		/* Augmented data size is limited to sizeof(augmented_arg->unnamed union with value field) */
-		if (aug_size > value_size)
-			aug_size = value_size;
-
-		/* write data to payload */
-		if (augmented) {
-			int written = offsetof(struct augmented_arg, value) + aug_size;
-
-			if (written < 0 || written > sizeof(struct augmented_arg))
-				return 1;
-
-			((struct augmented_arg *)payload_offset)->size = aug_size;
-			output += written;
-			payload_offset += written;
-			do_output = true;
-		}
-	}
+	struct args_loop_ctx loop_ctx = {
+		.args = args,
+		.beauty_map = beauty_map,
+		.payload_offset = payload_offset,
+		.value_size = value_size,
Quoted 8 lines

tools/perf/util/bpf_skel/augmented_raw_syscalls.bpf.c static int augment_sys_enter(void *ctx, struct syscall_enter_args *args) · line 549

Quoted 4 lines
tools/perf/util/bpf_skel/augmented_raw_syscalls.bpf.c +4 -0
+		.output = &output,
+		.do_output = &do_output
+	};
+	iters = bpf_loop(6, process_arg_cb, &loop_ctx, 0);

bpf_loop() is old and generally not recommended. Please use bpf_for() then the diff will be one line change and can scale to any number of args. Not just 6.

One thing we should take care is to support old kernels. The oldest LTS kernel in the kernel.org is 5.10 and bpf_loop() was introduced in 5.17 and bpf_for (bpf_iter_num) was 6.4.

Maybe we can factor out the loop body and call it from different mechanisms like open-coded loop, bpf_loop or bpf_for depending on the kernel version. But not sure it'd fix the verifier issue though.

Thanks,
Namhyung

#4 lore raw
Quoted 28 lines · Namhyung Kim

Hello,

Quoted 25 lines · Alexei Starovoitov
Quoted 24 lines · Viktor Malik

The loop for processing syscall args in augment_raw_syscalls has a history of breaking with Clang updates, see e.g. commit 013eb043f37b ("perf trace: Fix BPF loading failure (-E2BIG)") from Clang 15 to 16.

Now, a similar thing happened between Clang 21 and 22. While the issue is mitigated on the main line by a recent verifier update, it remains broken on the 6.12 and 6.18 stable branches:

[linux-6.18.y]# sudo perf trace true
libbpf: prog 'sys_enter': BPF program load failed: -E2BIG
libbpf: prog 'sys_enter': -- BEGIN PROG LOAD LOG --
[...]
BPF program is too large. Processed 1000001 insn
processed 1000001 insns (limit 1000000) max_states_per_insn 40 total_states 37941 peak_states 232 mark_read 0 -- END PROG LOAD LOG --
libbpf: prog 'sys_enter': failed to load: -E2BIG
libbpf: failed to load object 'augmented_raw_syscalls_bpf'
libbpf: failed to load BPF skeleton 'augmented_raw_syscalls_bpf': -E2BIG
Error: failed to get syscall or beauty map fd
[...]

The reason is that the loop is quite complex and the BPF verifier often struggles to prove that it terminates.

Quoted 197 lines
Quoted 5 lines
Quoted 5 lines

Fix the issue by refactoring the loop body into a callback function and calling the bpf_loop helper. This should prevent future breakages of this kind since the callback function has no loops. It also allows to drop a few artificial checks to help the verifier, including the changes introduced by 013eb043f37b.

Thanks for working on this. I encountered this issue before and never found time to take a deeper look yet.

Quoted 188 lines
Quoted 174 lines
Fixes a68fd6a6cdd3 ("perf trace: Collect augmented data using BPF")
Fixes 013eb043f37b ("perf trace: Fix BPF loading failure (-E2BIG)")
---
 .../bpf_skel/augmented_raw_syscalls.bpf.c     | 157 +++++++++++-------
 1 file changed, 96 insertions(+), 61 deletions(-)
tools/perf/util/bpf_skel/augmented_raw_syscalls.bpf.c +85 -60
index 2a6e61864ee0..6d553ed3ac23 100644
@@ -429,15 +429,96 @@ static bool pid_filter__has(struct pids_filtered *pids, pid_t pid)
 	return bpf_map_lookup_elem(pids, &pid) != NULL;
 }
 
+struct args_loop_ctx {
+	struct syscall_enter_args *args;
+	unsigned int *beauty_map;
+	void *payload_offset;
+	int value_size;
+	u64 *output;
+	bool *do_output;
+};
+
+static long process_arg_cb(u64 i, void *ctx)
+{
+	/*
+	 * Determine what type of argument and how many bytes to read from user space, using the
+	 * value in the beauty_map. This is the relation of parameter type and its corresponding
+	 * value in the beauty map, and how many bytes we read eventually:
+	 *
+	 * string: 1			      -> size of string
+	 * struct: size of struct	      -> size of struct
+	 * buffer: -1 * (index of paired len) -> value of paired len (maximum: TRACE_AUG_MAX_BUF)
+	 */
+	struct augmented_arg *augmented_arg;
+	struct args_loop_ctx *loop_ctx;
+	int aug_size, size, index;
+	bool augmented;
+	void *arg;
+
+	/* Bounds check for the below map access to help the verifier */
+	if (i < 0 || i >= 6)
+		return 1;
+
+	loop_ctx = (struct args_loop_ctx *)ctx;
+	arg = (void *)loop_ctx->args->args[i];
+	augmented = false;
+	size = loop_ctx->beauty_map[i];
+	aug_size = size; /* size of the augmented data read from user space */
+	augmented_arg = (struct augmented_arg *)loop_ctx->payload_offset;
+
+	if (size == 0 || arg == NULL)
+		return 0; /* continue */
+
+	if (size == 1) { /* string */
+		aug_size = bpf_probe_read_user_str(augmented_arg->value, loop_ctx->value_size, arg);
+		augmented = true;
+	} else if (size > 0 && size <= loop_ctx->value_size) { /* struct */
+		if (!bpf_probe_read_user(augmented_arg->value, size, arg))
+			augmented = true;
+	} else if (size < 0 && size >= -6) { /* buffer */
+		index = -(size + 1);
+		barrier_var(index); // Prevent clang (noticed with v18) from removing the &= 7 trick.
+		index &= 7;	    // Satisfy the bounds checking with the verifier in some kernels.
+		aug_size = loop_ctx->args->args[index];
+
+		if (aug_size > TRACE_AUG_MAX_BUF)
+			aug_size = TRACE_AUG_MAX_BUF;
+
+		if (aug_size > 0) {
+			if (!bpf_probe_read_user(augmented_arg->value, aug_size, arg))
+				augmented = true;
+		}
+	}
+
+	/* Augmented data size is limited to sizeof(augmented_arg->unnamed union with value field) */
+	if (aug_size > loop_ctx->value_size)
+		aug_size = loop_ctx->value_size;
+
+	/* write data to payload */
+	if (augmented) {
+		int written = offsetof(struct augmented_arg, value) + aug_size;
+
+		if (written < 0 || written > sizeof(struct augmented_arg))
+			return 1; /* break */
+
+		augmented_arg->size = aug_size;
+		*loop_ctx->output += written;
+		loop_ctx->payload_offset += written;
+		*loop_ctx->do_output = true;
+	}
+
+	return 0;
+}
+
 static int augment_sys_enter(void *ctx, struct syscall_enter_args *args)
 {
-	bool augmented, do_output = false;
-	int zero = 0, index, value_size = sizeof(struct augmented_arg) - offsetof(struct augmented_arg, value);
+	bool do_output = false;
+	int zero = 0, value_size = sizeof(struct augmented_arg) - offsetof(struct augmented_arg, value);
 	u64 output = 0; /* has to be u64, otherwise it won't pass the verifier */
-	s64 aug_size, size;
 	unsigned int nr, *beauty_map;
 	struct beauty_payload_enter *payload;
-	void *arg, *payload_offset;
+	void *payload_offset;
+	long iters;
 
 	/* fall back to do predefined tail call */
 	if (args == NULL)
@@ -457,63 +538,17 @@ static int augment_sys_enter(void *ctx, struct syscall_enter_args *args)
 	/* copy the sys_enter header, which has the syscall_nr */
 	__builtin_memcpy(&payload->args, args, sizeof(struct syscall_enter_args));
 
-	/*
-	 * Determine what type of argument and how many bytes to read from user space, using the
-	 * value in the beauty_map. This is the relation of parameter type and its corresponding
-	 * value in the beauty map, and how many bytes we read eventually:
-	 *
-	 * string: 1			      -> size of string
-	 * struct: size of struct	      -> size of struct
-	 * buffer: -1 * (index of paired len) -> value of paired len (maximum: TRACE_AUG_MAX_BUF)
-	 */
-	for (int i = 0; i < 6; i++) {
-		arg = (void *)args->args[i];
-		augmented = false;
-		size = beauty_map[i];
-		aug_size = size; /* size of the augmented data read from user space */
-
-		if (size == 0 || arg == NULL)
-			continue;
-
-		if (size == 1) { /* string */
-			aug_size = bpf_probe_read_user_str(((struct augmented_arg *)payload_offset)->value, value_size, arg);
-			/* minimum of 0 to pass the verifier */
-			if (aug_size < 0)
-				aug_size = 0;
-
-			augmented = true;
-		} else if (size > 0 && size <= value_size) { /* struct */
-			if (!bpf_probe_read_user(((struct augmented_arg *)payload_offset)->value, size, arg))
-				augmented = true;
-		} else if ((int)size < 0 && size >= -6) { /* buffer */
-			index = -(size + 1);
-			barrier_var(index); // Prevent clang (noticed with v18) from removing the &= 7 trick.
-			index &= 7;	    // Satisfy the bounds checking with the verifier in some kernels.
-			aug_size = args->args[index] > TRACE_AUG_MAX_BUF ? TRACE_AUG_MAX_BUF : args->args[index];
-
-			if (aug_size > 0) {
-				if (!bpf_probe_read_user(((struct augmented_arg *)payload_offset)->value, aug_size, arg))
-					augmented = true;
-			}
-		}
-
-		/* Augmented data size is limited to sizeof(augmented_arg->unnamed union with value field) */
-		if (aug_size > value_size)
-			aug_size = value_size;
-
-		/* write data to payload */
-		if (augmented) {
-			int written = offsetof(struct augmented_arg, value) + aug_size;
-
-			if (written < 0 || written > sizeof(struct augmented_arg))
-				return 1;
-
-			((struct augmented_arg *)payload_offset)->size = aug_size;
-			output += written;
-			payload_offset += written;
-			do_output = true;
-		}

tools/perf/util/bpf_skel/augmented_raw_syscalls.bpf.c static int augment_sys_enter(void *ctx, struct syscall_enter_args *args) · line 549

Quoted 10 lines
tools/perf/util/bpf_skel/augmented_raw_syscalls.bpf.c +9 -1
-	}
+	struct args_loop_ctx loop_ctx = {
+		.args = args,
+		.beauty_map = beauty_map,
+		.payload_offset = payload_offset,
+		.value_size = value_size,
+		.output = &output,
+		.do_output = &do_output
+	};
+	iters = bpf_loop(6, process_arg_cb, &loop_ctx, 0);

bpf_loop() is old and generally not recommended. Please use bpf_for() then the diff will be one line change and can scale to any number of args. Not just 6.

Thanks Alexei, I didn't know about this preference.

Quoted 3 lines

One thing we should take care is to support old kernels. The oldest LTS kernel in the kernel.org is 5.10 and bpf_loop() was introduced in 5.17 and bpf_for (bpf_iter_num) was 6.4.

The problematic loop was introduced in 6.12 by a68fd6a6cdd3 ("perf
trace: Collect augmented data using BPF") so we should be good using bpf_for. Or is perf from 7.2 supposed to work on 5.10 LTS kernels?

I'll refactor with bpf_for and will send v2. It should be then backported to stable kernels down to 6.12 LTS.

Viktor

Quoted 8 lines

Maybe we can factor out the loop body and call it from different mechanisms like open-coded loop, bpf_loop or bpf_for depending on the kernel version. But not sure it'd fix the verifier issue though.

Thanks,
Namhyung

#5 lore raw
Quoted 233 lines · Viktor Malik
Quoted 28 lines · Namhyung Kim

Hello,

Quoted 25 lines · Alexei Starovoitov
Quoted 24 lines · Viktor Malik

The loop for processing syscall args in augment_raw_syscalls has a history of breaking with Clang updates, see e.g. commit 013eb043f37b ("perf trace: Fix BPF loading failure (-E2BIG)") from Clang 15 to 16.

Now, a similar thing happened between Clang 21 and 22. While the issue is mitigated on the main line by a recent verifier update, it remains broken on the 6.12 and 6.18 stable branches:

[linux-6.18.y]# sudo perf trace true
libbpf: prog 'sys_enter': BPF program load failed: -E2BIG
libbpf: prog 'sys_enter': -- BEGIN PROG LOAD LOG --
[...]
BPF program is too large. Processed 1000001 insn
processed 1000001 insns (limit 1000000) max_states_per_insn 40 total_states 37941 peak_states 232 mark_read 0 -- END PROG LOAD LOG --
libbpf: prog 'sys_enter': failed to load: -E2BIG
libbpf: failed to load object 'augmented_raw_syscalls_bpf'
libbpf: failed to load BPF skeleton 'augmented_raw_syscalls_bpf': -E2BIG
Error: failed to get syscall or beauty map fd
[...]

The reason is that the loop is quite complex and the BPF verifier often struggles to prove that it terminates.

Quoted 197 lines
Quoted 5 lines
Quoted 5 lines

Fix the issue by refactoring the loop body into a callback function and calling the bpf_loop helper. This should prevent future breakages of this kind since the callback function has no loops. It also allows to drop a few artificial checks to help the verifier, including the changes introduced by 013eb043f37b.

Thanks for working on this. I encountered this issue before and never found time to take a deeper look yet.

Quoted 188 lines
Quoted 174 lines
Fixes a68fd6a6cdd3 ("perf trace: Collect augmented data using BPF")
Fixes 013eb043f37b ("perf trace: Fix BPF loading failure (-E2BIG)")
---
 .../bpf_skel/augmented_raw_syscalls.bpf.c     | 157 +++++++++++-------
 1 file changed, 96 insertions(+), 61 deletions(-)
tools/perf/util/bpf_skel/augmented_raw_syscalls.bpf.c +85 -60
index 2a6e61864ee0..6d553ed3ac23 100644
@@ -429,15 +429,96 @@ static bool pid_filter__has(struct pids_filtered *pids, pid_t pid)
 	return bpf_map_lookup_elem(pids, &pid) != NULL;
 }
 
+struct args_loop_ctx {
+	struct syscall_enter_args *args;
+	unsigned int *beauty_map;
+	void *payload_offset;
+	int value_size;
+	u64 *output;
+	bool *do_output;
+};
+
+static long process_arg_cb(u64 i, void *ctx)
+{
+	/*
+	 * Determine what type of argument and how many bytes to read from user space, using the
+	 * value in the beauty_map. This is the relation of parameter type and its corresponding
+	 * value in the beauty map, and how many bytes we read eventually:
+	 *
+	 * string: 1			      -> size of string
+	 * struct: size of struct	      -> size of struct
+	 * buffer: -1 * (index of paired len) -> value of paired len (maximum: TRACE_AUG_MAX_BUF)
+	 */
+	struct augmented_arg *augmented_arg;
+	struct args_loop_ctx *loop_ctx;
+	int aug_size, size, index;
+	bool augmented;
+	void *arg;
+
+	/* Bounds check for the below map access to help the verifier */
+	if (i < 0 || i >= 6)
+		return 1;
+
+	loop_ctx = (struct args_loop_ctx *)ctx;
+	arg = (void *)loop_ctx->args->args[i];
+	augmented = false;
+	size = loop_ctx->beauty_map[i];
+	aug_size = size; /* size of the augmented data read from user space */
+	augmented_arg = (struct augmented_arg *)loop_ctx->payload_offset;
+
+	if (size == 0 || arg == NULL)
+		return 0; /* continue */
+
+	if (size == 1) { /* string */
+		aug_size = bpf_probe_read_user_str(augmented_arg->value, loop_ctx->value_size, arg);
+		augmented = true;
+	} else if (size > 0 && size <= loop_ctx->value_size) { /* struct */
+		if (!bpf_probe_read_user(augmented_arg->value, size, arg))
+			augmented = true;
+	} else if (size < 0 && size >= -6) { /* buffer */
+		index = -(size + 1);
+		barrier_var(index); // Prevent clang (noticed with v18) from removing the &= 7 trick.
+		index &= 7;	    // Satisfy the bounds checking with the verifier in some kernels.
+		aug_size = loop_ctx->args->args[index];
+
+		if (aug_size > TRACE_AUG_MAX_BUF)
+			aug_size = TRACE_AUG_MAX_BUF;
+
+		if (aug_size > 0) {
+			if (!bpf_probe_read_user(augmented_arg->value, aug_size, arg))
+				augmented = true;
+		}
+	}
+
+	/* Augmented data size is limited to sizeof(augmented_arg->unnamed union with value field) */
+	if (aug_size > loop_ctx->value_size)
+		aug_size = loop_ctx->value_size;
+
+	/* write data to payload */
+	if (augmented) {
+		int written = offsetof(struct augmented_arg, value) + aug_size;
+
+		if (written < 0 || written > sizeof(struct augmented_arg))
+			return 1; /* break */
+
+		augmented_arg->size = aug_size;
+		*loop_ctx->output += written;
+		loop_ctx->payload_offset += written;
+		*loop_ctx->do_output = true;
+	}
+
+	return 0;
+}
+
 static int augment_sys_enter(void *ctx, struct syscall_enter_args *args)
 {
-	bool augmented, do_output = false;
-	int zero = 0, index, value_size = sizeof(struct augmented_arg) - offsetof(struct augmented_arg, value);
+	bool do_output = false;
+	int zero = 0, value_size = sizeof(struct augmented_arg) - offsetof(struct augmented_arg, value);
 	u64 output = 0; /* has to be u64, otherwise it won't pass the verifier */
-	s64 aug_size, size;
 	unsigned int nr, *beauty_map;
 	struct beauty_payload_enter *payload;
-	void *arg, *payload_offset;
+	void *payload_offset;
+	long iters;
 
 	/* fall back to do predefined tail call */
 	if (args == NULL)
@@ -457,63 +538,17 @@ static int augment_sys_enter(void *ctx, struct syscall_enter_args *args)
 	/* copy the sys_enter header, which has the syscall_nr */
 	__builtin_memcpy(&payload->args, args, sizeof(struct syscall_enter_args));
 
-	/*
-	 * Determine what type of argument and how many bytes to read from user space, using the
-	 * value in the beauty_map. This is the relation of parameter type and its corresponding
-	 * value in the beauty map, and how many bytes we read eventually:
-	 *
-	 * string: 1			      -> size of string
-	 * struct: size of struct	      -> size of struct
-	 * buffer: -1 * (index of paired len) -> value of paired len (maximum: TRACE_AUG_MAX_BUF)
-	 */
-	for (int i = 0; i < 6; i++) {
-		arg = (void *)args->args[i];
-		augmented = false;
-		size = beauty_map[i];
-		aug_size = size; /* size of the augmented data read from user space */
-
-		if (size == 0 || arg == NULL)
-			continue;
-
-		if (size == 1) { /* string */
-			aug_size = bpf_probe_read_user_str(((struct augmented_arg *)payload_offset)->value, value_size, arg);
-			/* minimum of 0 to pass the verifier */
-			if (aug_size < 0)
-				aug_size = 0;
-
-			augmented = true;
-		} else if (size > 0 && size <= value_size) { /* struct */
-			if (!bpf_probe_read_user(((struct augmented_arg *)payload_offset)->value, size, arg))
-				augmented = true;
-		} else if ((int)size < 0 && size >= -6) { /* buffer */
-			index = -(size + 1);
-			barrier_var(index); // Prevent clang (noticed with v18) from removing the &= 7 trick.
-			index &= 7;	    // Satisfy the bounds checking with the verifier in some kernels.
-			aug_size = args->args[index] > TRACE_AUG_MAX_BUF ? TRACE_AUG_MAX_BUF : args->args[index];
-
-			if (aug_size > 0) {
-				if (!bpf_probe_read_user(((struct augmented_arg *)payload_offset)->value, aug_size, arg))
-					augmented = true;
-			}
-		}
-
-		/* Augmented data size is limited to sizeof(augmented_arg->unnamed union with value field) */
-		if (aug_size > value_size)
-			aug_size = value_size;
-
-		/* write data to payload */
-		if (augmented) {
-			int written = offsetof(struct augmented_arg, value) + aug_size;
-
-			if (written < 0 || written > sizeof(struct augmented_arg))
-				return 1;
-
-			((struct augmented_arg *)payload_offset)->size = aug_size;
-			output += written;
-			payload_offset += written;
-			do_output = true;
-		}

tools/perf/util/bpf_skel/augmented_raw_syscalls.bpf.c static int augment_sys_enter(void *ctx, struct syscall_enter_args *args) · line 549

Quoted 10 lines
tools/perf/util/bpf_skel/augmented_raw_syscalls.bpf.c +9 -1
-	}
+	struct args_loop_ctx loop_ctx = {
+		.args = args,
+		.beauty_map = beauty_map,
+		.payload_offset = payload_offset,
+		.value_size = value_size,
+		.output = &output,
+		.do_output = &do_output
+	};
+	iters = bpf_loop(6, process_arg_cb, &loop_ctx, 0);

bpf_loop() is old and generally not recommended. Please use bpf_for() then the diff will be one line change and can scale to any number of args. Not just 6.

Thanks Alexei, I didn't know about this preference.

Quoted 3 lines

One thing we should take care is to support old kernels. The oldest LTS kernel in the kernel.org is 5.10 and bpf_loop() was introduced in 5.17 and bpf_for (bpf_iter_num) was 6.4.

Quoted 5 lines

The problematic loop was introduced in 6.12 by a68fd6a6cdd3 ("perf
trace: Collect augmented data using BPF") so we should be good using bpf_for. Or is perf from 7.2 supposed to work on 5.10 LTS kernels?

I'll refactor with bpf_for and will send v2.

Or I won't. It turns out that just swapping the for loop for bpf_for leads to -E2BIG from the verifier again. Looking at the verifier log, it fails to find equivalence between states at the loop head:

    [...]
    78: (85) call bpf_iter_num_next#84922 [...]
fp-56=map_value(map=beauty_payload_,ks=4,vs=24688,imm=112)
    [...]
    78: (85) call bpf_iter_num_next#84922 [...]
fp-56=map_value(map=beauty_payload_,ks=4,vs=24688,imm=120)
    [...]

IMHO, the reason is that payload_offset, which points to the beauty_payload_enter_map entry, gets updated in every iteration.

This could be probably fixed on the perf side by reworking how augmented args are stored but at this point, bpf_loop sounds like an easier and more reliable approach.

Let me know if anyone has objections, otherwise I'll send v2 of the bpf_loop approach, with suggestions from Sashiko incorporated.

Thanks,
Viktor

Quoted 13 lines

It should be then
backported to stable kernels down to 6.12 LTS.

Viktor

Quoted 8 lines

Maybe we can factor out the loop body and call it from different mechanisms like open-coded loop, bpf_loop or bpf_for depending on the kernel version. But not sure it'd fix the verifier issue though.

Thanks,
Namhyung

#6 lore raw
Quoted 256 lines · Viktor Malik
Quoted 233 lines · Viktor Malik
Quoted 28 lines · Namhyung Kim

Hello,

Quoted 25 lines · Alexei Starovoitov
Quoted 24 lines · Viktor Malik

The loop for processing syscall args in augment_raw_syscalls has a history of breaking with Clang updates, see e.g. commit 013eb043f37b ("perf trace: Fix BPF loading failure (-E2BIG)") from Clang 15 to 16.

Now, a similar thing happened between Clang 21 and 22. While the issue is mitigated on the main line by a recent verifier update, it remains broken on the 6.12 and 6.18 stable branches:

[linux-6.18.y]# sudo perf trace true
libbpf: prog 'sys_enter': BPF program load failed: -E2BIG
libbpf: prog 'sys_enter': -- BEGIN PROG LOAD LOG --
[...]
BPF program is too large. Processed 1000001 insn
processed 1000001 insns (limit 1000000) max_states_per_insn 40 total_states 37941 peak_states 232 mark_read 0 -- END PROG LOAD LOG --
libbpf: prog 'sys_enter': failed to load: -E2BIG
libbpf: failed to load object 'augmented_raw_syscalls_bpf'
libbpf: failed to load BPF skeleton 'augmented_raw_syscalls_bpf': -E2BIG
Error: failed to get syscall or beauty map fd
[...]

The reason is that the loop is quite complex and the BPF verifier often struggles to prove that it terminates.

Quoted 197 lines
Quoted 5 lines
Quoted 5 lines

Fix the issue by refactoring the loop body into a callback function and calling the bpf_loop helper. This should prevent future breakages of this kind since the callback function has no loops. It also allows to drop a few artificial checks to help the verifier, including the changes introduced by 013eb043f37b.

Thanks for working on this. I encountered this issue before and never found time to take a deeper look yet.

Quoted 188 lines
Quoted 174 lines
Fixes a68fd6a6cdd3 ("perf trace: Collect augmented data using BPF")
Fixes 013eb043f37b ("perf trace: Fix BPF loading failure (-E2BIG)")
---
 .../bpf_skel/augmented_raw_syscalls.bpf.c     | 157 +++++++++++-------
 1 file changed, 96 insertions(+), 61 deletions(-)
tools/perf/util/bpf_skel/augmented_raw_syscalls.bpf.c +85 -60
index 2a6e61864ee0..6d553ed3ac23 100644
@@ -429,15 +429,96 @@ static bool pid_filter__has(struct pids_filtered *pids, pid_t pid)
   return bpf_map_lookup_elem(pids, &pid) != NULL;
 }
 
+struct args_loop_ctx {
+  struct syscall_enter_args *args;
+  unsigned int *beauty_map;
+  void *payload_offset;
+  int value_size;
+  u64 *output;
+  bool *do_output;
+};
+
+static long process_arg_cb(u64 i, void *ctx)
+{
+  /*
+   * Determine what type of argument and how many bytes to read from user space, using the
+   * value in the beauty_map. This is the relation of parameter type and its corresponding
+   * value in the beauty map, and how many bytes we read eventually:
+   *
+   * string: 1                          -> size of string
+   * struct: size of struct             -> size of struct
+   * buffer: -1 * (index of paired len) -> value of paired len (maximum: TRACE_AUG_MAX_BUF)
+   */
+  struct augmented_arg *augmented_arg;
+  struct args_loop_ctx *loop_ctx;
+  int aug_size, size, index;
+  bool augmented;
+  void *arg;
+
+  /* Bounds check for the below map access to help the verifier */
+  if (i < 0 || i >= 6)
+          return 1;
+
+  loop_ctx = (struct args_loop_ctx *)ctx;
+  arg = (void *)loop_ctx->args->args[i];
+  augmented = false;
+  size = loop_ctx->beauty_map[i];
+  aug_size = size; /* size of the augmented data read from user space */
+  augmented_arg = (struct augmented_arg *)loop_ctx->payload_offset;
+
+  if (size == 0 || arg == NULL)
+          return 0; /* continue */
+
+  if (size == 1) { /* string */
+          aug_size = bpf_probe_read_user_str(augmented_arg->value, loop_ctx->value_size, arg);
+          augmented = true;
+  } else if (size > 0 && size <= loop_ctx->value_size) { /* struct */
+          if (!bpf_probe_read_user(augmented_arg->value, size, arg))
+                  augmented = true;
+  } else if (size < 0 && size >= -6) { /* buffer */
+          index = -(size + 1);
+          barrier_var(index); // Prevent clang (noticed with v18) from removing the &= 7 trick.
+          index &= 7;         // Satisfy the bounds checking with the verifier in some kernels.
+          aug_size = loop_ctx->args->args[index];
+
+          if (aug_size > TRACE_AUG_MAX_BUF)
+                  aug_size = TRACE_AUG_MAX_BUF;
+
+          if (aug_size > 0) {
+                  if (!bpf_probe_read_user(augmented_arg->value, aug_size, arg))
+                          augmented = true;
+          }
+  }
+
+  /* Augmented data size is limited to sizeof(augmented_arg->unnamed union with value field) */
+  if (aug_size > loop_ctx->value_size)
+          aug_size = loop_ctx->value_size;
+
+  /* write data to payload */
+  if (augmented) {
+          int written = offsetof(struct augmented_arg, value) + aug_size;
+
+          if (written < 0 || written > sizeof(struct augmented_arg))
+                  return 1; /* break */
+
+          augmented_arg->size = aug_size;
+          *loop_ctx->output += written;
+          loop_ctx->payload_offset += written;
+          *loop_ctx->do_output = true;
+  }
+
+  return 0;
+}
+
 static int augment_sys_enter(void *ctx, struct syscall_enter_args *args)
 {
-  bool augmented, do_output = false;
-  int zero = 0, index, value_size = sizeof(struct augmented_arg) - offsetof(struct augmented_arg, value);
+  bool do_output = false;
+  int zero = 0, value_size = sizeof(struct augmented_arg) - offsetof(struct augmented_arg, value);
   u64 output = 0; /* has to be u64, otherwise it won't pass the verifier */
-  s64 aug_size, size;
   unsigned int nr, *beauty_map;
   struct beauty_payload_enter *payload;
-  void *arg, *payload_offset;
+  void *payload_offset;
+  long iters;
 
   /* fall back to do predefined tail call */
   if (args == NULL)
@@ -457,63 +538,17 @@ static int augment_sys_enter(void *ctx, struct syscall_enter_args *args)
   /* copy the sys_enter header, which has the syscall_nr */
   __builtin_memcpy(&payload->args, args, sizeof(struct syscall_enter_args));
 
-  /*
-   * Determine what type of argument and how many bytes to read from user space, using the
-   * value in the beauty_map. This is the relation of parameter type and its corresponding
-   * value in the beauty map, and how many bytes we read eventually:
-   *
-   * string: 1                          -> size of string
-   * struct: size of struct             -> size of struct
-   * buffer: -1 * (index of paired len) -> value of paired len (maximum: TRACE_AUG_MAX_BUF)
-   */
-  for (int i = 0; i < 6; i++) {
-          arg = (void *)args->args[i];
-          augmented = false;
-          size = beauty_map[i];
-          aug_size = size; /* size of the augmented data read from user space */
-
-          if (size == 0 || arg == NULL)
-                  continue;
-
-          if (size == 1) { /* string */
-                  aug_size = bpf_probe_read_user_str(((struct augmented_arg *)payload_offset)->value, value_size, arg);
-                  /* minimum of 0 to pass the verifier */
-                  if (aug_size < 0)
-                          aug_size = 0;
-
-                  augmented = true;
-          } else if (size > 0 && size <= value_size) { /* struct */
-                  if (!bpf_probe_read_user(((struct augmented_arg *)payload_offset)->value, size, arg))
-                          augmented = true;
-          } else if ((int)size < 0 && size >= -6) { /* buffer */
-                  index = -(size + 1);
-                  barrier_var(index); // Prevent clang (noticed with v18) from removing the &= 7 trick.
-                  index &= 7;         // Satisfy the bounds checking with the verifier in some kernels.
-                  aug_size = args->args[index] > TRACE_AUG_MAX_BUF ? TRACE_AUG_MAX_BUF : args->args[index];
-
-                  if (aug_size > 0) {
-                          if (!bpf_probe_read_user(((struct augmented_arg *)payload_offset)->value, aug_size, arg))
-                                  augmented = true;
-                  }
-          }
-
-          /* Augmented data size is limited to sizeof(augmented_arg->unnamed union with value field) */
-          if (aug_size > value_size)
-                  aug_size = value_size;
-
-          /* write data to payload */
-          if (augmented) {
-                  int written = offsetof(struct augmented_arg, value) + aug_size;
-
-                  if (written < 0 || written > sizeof(struct augmented_arg))
-                          return 1;
-
-                  ((struct augmented_arg *)payload_offset)->size = aug_size;
-                  output += written;
-                  payload_offset += written;
-                  do_output = true;
-          }

tools/perf/util/bpf_skel/augmented_raw_syscalls.bpf.c static int augment_sys_enter(void *ctx, struct syscall_enter_args *args) · line 549

Quoted 10 lines
tools/perf/util/bpf_skel/augmented_raw_syscalls.bpf.c +9 -1
-  }
+  struct args_loop_ctx loop_ctx = {
+          .args = args,
+          .beauty_map = beauty_map,
+          .payload_offset = payload_offset,
+          .value_size = value_size,
+          .output = &output,
+          .do_output = &do_output
+  };
+  iters = bpf_loop(6, process_arg_cb, &loop_ctx, 0);

bpf_loop() is old and generally not recommended. Please use bpf_for() then the diff will be one line change and can scale to any number of args. Not just 6.

Thanks Alexei, I didn't know about this preference.

Quoted 3 lines

One thing we should take care is to support old kernels. The oldest LTS kernel in the kernel.org is 5.10 and bpf_loop() was introduced in 5.17 and bpf_for (bpf_iter_num) was 6.4.

Quoted 5 lines

The problematic loop was introduced in 6.12 by a68fd6a6cdd3 ("perf
trace: Collect augmented data using BPF") so we should be good using bpf_for. Or is perf from 7.2 supposed to work on 5.10 LTS kernels?

I'll refactor with bpf_for and will send v2.

Or I won't. It turns out that just swapping the for loop for bpf_for leads to -E2BIG from the verifier again. Looking at the verifier log, it fails to find equivalence between states at the loop head:

    [...]
    78: (85) call bpf_iter_num_next#84922 [...]
fp-56=map_value(map=beauty_payload_,ks=4,vs=24688,imm=112)
    [...]
    78: (85) call bpf_iter_num_next#84922 [...]
fp-56=map_value(map=beauty_payload_,ks=4,vs=24688,imm=120)
    [...]

IMHO, the reason is that payload_offset, which points to the beauty_payload_enter_map entry, gets updated in every iteration.

Quoted 7 lines

This could be probably fixed on the perf side by reworking how augmented args are stored but at this point, bpf_loop sounds like an easier and more reliable approach.

Let me know if anyone has objections, otherwise I'll send v2 of the bpf_loop approach, with suggestions from Sashiko incorporated.

I'd still try to adapt bpf_for(), it's a much better code structure. You probably need to add a bounding checking/confirming if () condition validating that offset at which you access map_value is always correct. And/or you might need barrier_var() before using i, because bpf_for() macro does bounds checking (check the macro itself), but compiler often will reorder instructions leading to verifier complaints.

Quoted 18 lines

Thanks,
Viktor

Quoted 13 lines

It should be then
backported to stable kernels down to 6.12 LTS.

Viktor

Quoted 8 lines

Maybe we can factor out the loop body and call it from different mechanisms like open-coded loop, bpf_loop or bpf_for depending on the kernel version. But not sure it'd fix the verifier issue though.

Thanks,
Namhyung

maintainer
#12 lore raw
In reply to Viktor Malik
Quoted 5 lines · Viktor Malik
Quoted 4 lines · Namhyung Kim

Hello,

Quoted 1 line · Alexei Starovoitov

On Tue Jun 23, 2026 at 4:25 AM PDT, Viktor Malik wrote:

[SNIP]

Quoted 16 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
tools/perf/util/bpf_skel/augmented_raw_syscalls.bpf.c +5 -0
+	struct args_loop_ctx loop_ctx = {
+		.args = args,
+		.beauty_map = beauty_map,
+		.payload_offset = payload_offset,
+		.value_size = value_size,
Quoted 8 lines
Quoted 8 lines

tools/perf/util/bpf_skel/augmented_raw_syscalls.bpf.c static int augment_sys_enter(void *ctx, struct syscall_enter_args *args) · line 549

Quoted 4 lines
tools/perf/util/bpf_skel/augmented_raw_syscalls.bpf.c +4 -0
+		.output = &output,
+		.do_output = &do_output
+	};
+	iters = bpf_loop(6, process_arg_cb, &loop_ctx, 0);

bpf_loop() is old and generally not recommended. Please use bpf_for() then the diff will be one line change and can scale to any number of args. Not just 6.

Thanks Alexei, I didn't know about this preference.

Quoted 7 lines
Quoted 3 lines

One thing we should take care is to support old kernels. The oldest LTS kernel in the kernel.org is 5.10 and bpf_loop() was introduced in 5.17 and bpf_for (bpf_iter_num) was 6.4.

The problematic loop was introduced in 6.12 by a68fd6a6cdd3 ("perf
trace: Collect augmented data using BPF") so we should be good using bpf_for. Or is perf from 7.2 supposed to work on 5.10 LTS kernels?

Yep, we'd like to support old kernels.

Thanks,
Namhyung

#7 lore raw
In reply to Andrii Nakryiko
Quoted 265 lines · Andrii Nakryiko
Quoted 256 lines · Viktor Malik
Quoted 233 lines · Viktor Malik
Quoted 28 lines · Namhyung Kim

Hello,

Quoted 25 lines · Alexei Starovoitov
Quoted 24 lines · Viktor Malik

The loop for processing syscall args in augment_raw_syscalls has a history of breaking with Clang updates, see e.g. commit 013eb043f37b ("perf trace: Fix BPF loading failure (-E2BIG)") from Clang 15 to 16.

Now, a similar thing happened between Clang 21 and 22. While the issue is mitigated on the main line by a recent verifier update, it remains broken on the 6.12 and 6.18 stable branches:

[linux-6.18.y]# sudo perf trace true
libbpf: prog 'sys_enter': BPF program load failed: -E2BIG
libbpf: prog 'sys_enter': -- BEGIN PROG LOAD LOG --
[...]
BPF program is too large. Processed 1000001 insn
processed 1000001 insns (limit 1000000) max_states_per_insn 40 total_states 37941 peak_states 232 mark_read 0 -- END PROG LOAD LOG --
libbpf: prog 'sys_enter': failed to load: -E2BIG
libbpf: failed to load object 'augmented_raw_syscalls_bpf'
libbpf: failed to load BPF skeleton 'augmented_raw_syscalls_bpf': -E2BIG
Error: failed to get syscall or beauty map fd
[...]

The reason is that the loop is quite complex and the BPF verifier often struggles to prove that it terminates.

Quoted 197 lines
Quoted 5 lines
Quoted 5 lines

Fix the issue by refactoring the loop body into a callback function and calling the bpf_loop helper. This should prevent future breakages of this kind since the callback function has no loops. It also allows to drop a few artificial checks to help the verifier, including the changes introduced by 013eb043f37b.

Thanks for working on this. I encountered this issue before and never found time to take a deeper look yet.

Quoted 188 lines
Quoted 174 lines
Fixes a68fd6a6cdd3 ("perf trace: Collect augmented data using BPF")
Fixes 013eb043f37b ("perf trace: Fix BPF loading failure (-E2BIG)")
---
 .../bpf_skel/augmented_raw_syscalls.bpf.c     | 157 +++++++++++-------
 1 file changed, 96 insertions(+), 61 deletions(-)
tools/perf/util/bpf_skel/augmented_raw_syscalls.bpf.c +85 -60
index 2a6e61864ee0..6d553ed3ac23 100644
@@ -429,15 +429,96 @@ static bool pid_filter__has(struct pids_filtered *pids, pid_t pid)
   return bpf_map_lookup_elem(pids, &pid) != NULL;
 }
 
+struct args_loop_ctx {
+  struct syscall_enter_args *args;
+  unsigned int *beauty_map;
+  void *payload_offset;
+  int value_size;
+  u64 *output;
+  bool *do_output;
+};
+
+static long process_arg_cb(u64 i, void *ctx)
+{
+  /*
+   * Determine what type of argument and how many bytes to read from user space, using the
+   * value in the beauty_map. This is the relation of parameter type and its corresponding
+   * value in the beauty map, and how many bytes we read eventually:
+   *
+   * string: 1                          -> size of string
+   * struct: size of struct             -> size of struct
+   * buffer: -1 * (index of paired len) -> value of paired len (maximum: TRACE_AUG_MAX_BUF)
+   */
+  struct augmented_arg *augmented_arg;
+  struct args_loop_ctx *loop_ctx;
+  int aug_size, size, index;
+  bool augmented;
+  void *arg;
+
+  /* Bounds check for the below map access to help the verifier */
+  if (i < 0 || i >= 6)
+          return 1;
+
+  loop_ctx = (struct args_loop_ctx *)ctx;
+  arg = (void *)loop_ctx->args->args[i];
+  augmented = false;
+  size = loop_ctx->beauty_map[i];
+  aug_size = size; /* size of the augmented data read from user space */
+  augmented_arg = (struct augmented_arg *)loop_ctx->payload_offset;
+
+  if (size == 0 || arg == NULL)
+          return 0; /* continue */
+
+  if (size == 1) { /* string */
+          aug_size = bpf_probe_read_user_str(augmented_arg->value, loop_ctx->value_size, arg);
+          augmented = true;
+  } else if (size > 0 && size <= loop_ctx->value_size) { /* struct */
+          if (!bpf_probe_read_user(augmented_arg->value, size, arg))
+                  augmented = true;
+  } else if (size < 0 && size >= -6) { /* buffer */
+          index = -(size + 1);
+          barrier_var(index); // Prevent clang (noticed with v18) from removing the &= 7 trick.
+          index &= 7;         // Satisfy the bounds checking with the verifier in some kernels.
+          aug_size = loop_ctx->args->args[index];
+
+          if (aug_size > TRACE_AUG_MAX_BUF)
+                  aug_size = TRACE_AUG_MAX_BUF;
+
+          if (aug_size > 0) {
+                  if (!bpf_probe_read_user(augmented_arg->value, aug_size, arg))
+                          augmented = true;
+          }
+  }
+
+  /* Augmented data size is limited to sizeof(augmented_arg->unnamed union with value field) */
+  if (aug_size > loop_ctx->value_size)
+          aug_size = loop_ctx->value_size;
+
+  /* write data to payload */
+  if (augmented) {
+          int written = offsetof(struct augmented_arg, value) + aug_size;
+
+          if (written < 0 || written > sizeof(struct augmented_arg))
+                  return 1; /* break */
+
+          augmented_arg->size = aug_size;
+          *loop_ctx->output += written;
+          loop_ctx->payload_offset += written;
+          *loop_ctx->do_output = true;
+  }
+
+  return 0;
+}
+
 static int augment_sys_enter(void *ctx, struct syscall_enter_args *args)
 {
-  bool augmented, do_output = false;
-  int zero = 0, index, value_size = sizeof(struct augmented_arg) - offsetof(struct augmented_arg, value);
+  bool do_output = false;
+  int zero = 0, value_size = sizeof(struct augmented_arg) - offsetof(struct augmented_arg, value);
   u64 output = 0; /* has to be u64, otherwise it won't pass the verifier */
-  s64 aug_size, size;
   unsigned int nr, *beauty_map;
   struct beauty_payload_enter *payload;
-  void *arg, *payload_offset;
+  void *payload_offset;
+  long iters;
 
   /* fall back to do predefined tail call */
   if (args == NULL)
@@ -457,63 +538,17 @@ static int augment_sys_enter(void *ctx, struct syscall_enter_args *args)
   /* copy the sys_enter header, which has the syscall_nr */
   __builtin_memcpy(&payload->args, args, sizeof(struct syscall_enter_args));
 
-  /*
-   * Determine what type of argument and how many bytes to read from user space, using the
-   * value in the beauty_map. This is the relation of parameter type and its corresponding
-   * value in the beauty map, and how many bytes we read eventually:
-   *
-   * string: 1                          -> size of string
-   * struct: size of struct             -> size of struct
-   * buffer: -1 * (index of paired len) -> value of paired len (maximum: TRACE_AUG_MAX_BUF)
-   */
-  for (int i = 0; i < 6; i++) {
-          arg = (void *)args->args[i];
-          augmented = false;
-          size = beauty_map[i];
-          aug_size = size; /* size of the augmented data read from user space */
-
-          if (size == 0 || arg == NULL)
-                  continue;
-
-          if (size == 1) { /* string */
-                  aug_size = bpf_probe_read_user_str(((struct augmented_arg *)payload_offset)->value, value_size, arg);
-                  /* minimum of 0 to pass the verifier */
-                  if (aug_size < 0)
-                          aug_size = 0;
-
-                  augmented = true;
-          } else if (size > 0 && size <= value_size) { /* struct */
-                  if (!bpf_probe_read_user(((struct augmented_arg *)payload_offset)->value, size, arg))
-                          augmented = true;
-          } else if ((int)size < 0 && size >= -6) { /* buffer */
-                  index = -(size + 1);
-                  barrier_var(index); // Prevent clang (noticed with v18) from removing the &= 7 trick.
-                  index &= 7;         // Satisfy the bounds checking with the verifier in some kernels.
-                  aug_size = args->args[index] > TRACE_AUG_MAX_BUF ? TRACE_AUG_MAX_BUF : args->args[index];
-
-                  if (aug_size > 0) {
-                          if (!bpf_probe_read_user(((struct augmented_arg *)payload_offset)->value, aug_size, arg))
-                                  augmented = true;
-                  }
-          }
-
-          /* Augmented data size is limited to sizeof(augmented_arg->unnamed union with value field) */
-          if (aug_size > value_size)
-                  aug_size = value_size;
-
-          /* write data to payload */
-          if (augmented) {
-                  int written = offsetof(struct augmented_arg, value) + aug_size;
-
-                  if (written < 0 || written > sizeof(struct augmented_arg))
-                          return 1;
-
-                  ((struct augmented_arg *)payload_offset)->size = aug_size;
-                  output += written;
-                  payload_offset += written;
-                  do_output = true;
-          }

tools/perf/util/bpf_skel/augmented_raw_syscalls.bpf.c static int augment_sys_enter(void *ctx, struct syscall_enter_args *args) · line 549

Quoted 10 lines
tools/perf/util/bpf_skel/augmented_raw_syscalls.bpf.c +9 -1
-  }
+  struct args_loop_ctx loop_ctx = {
+          .args = args,
+          .beauty_map = beauty_map,
+          .payload_offset = payload_offset,
+          .value_size = value_size,
+          .output = &output,
+          .do_output = &do_output
+  };
+  iters = bpf_loop(6, process_arg_cb, &loop_ctx, 0);

bpf_loop() is old and generally not recommended. Please use bpf_for() then the diff will be one line change and can scale to any number of args. Not just 6.

Thanks Alexei, I didn't know about this preference.

Quoted 3 lines

One thing we should take care is to support old kernels. The oldest LTS kernel in the kernel.org is 5.10 and bpf_loop() was introduced in 5.17 and bpf_for (bpf_iter_num) was 6.4.

Quoted 5 lines

The problematic loop was introduced in 6.12 by a68fd6a6cdd3 ("perf
trace: Collect augmented data using BPF") so we should be good using bpf_for. Or is perf from 7.2 supposed to work on 5.10 LTS kernels?

I'll refactor with bpf_for and will send v2.

Or I won't. It turns out that just swapping the for loop for bpf_for leads to -E2BIG from the verifier again. Looking at the verifier log, it fails to find equivalence between states at the loop head:

    [...]
    78: (85) call bpf_iter_num_next#84922 [...]
fp-56=map_value(map=beauty_payload_,ks=4,vs=24688,imm=112)
    [...]
    78: (85) call bpf_iter_num_next#84922 [...]
fp-56=map_value(map=beauty_payload_,ks=4,vs=24688,imm=120)
    [...]

IMHO, the reason is that payload_offset, which points to the beauty_payload_enter_map entry, gets updated in every iteration.

Quoted 7 lines

This could be probably fixed on the perf side by reworking how augmented args are stored but at this point, bpf_loop sounds like an easier and more reliable approach.

Let me know if anyone has objections, otherwise I'll send v2 of the bpf_loop approach, with suggestions from Sashiko incorporated.

Quoted 7 lines

I'd still try to adapt bpf_for(), it's a much better code structure. You probably need to add a bounding checking/confirming if () condition validating that offset at which you access map_value is always correct. And/or you might need barrier_var() before using i, because bpf_for() macro does bounds checking (check the macro itself), but compiler often will reorder instructions leading to verifier complaints.

I gave it a try but wasn't successful so far. I think that the problem is that while it would be possible to add an upper bound condition for payload_offset, the verifier tracks the value of payload_offset too precisely (as map_value(..., imm=X) with a concrete offset) and never merges states with different offsets. And since there are multiple branches inside the loop, each incrementing payload_offset by a different value, the verifier seems to fork its state on each branch, effectively leading to the amount of states growing exponentially and hitting the jump limit.

To me, bpf_loop sounds like a more reliable choice in this situation. It's also older, which is good in this case, since compatibility with older kernels seems to be important for perf (see other messages in the thread).

I'm also wondering if the verifier could be improved to handle these cases but that's a different discussion.

Quoted 20 lines
Quoted 19 lines

Thanks,
Viktor

Quoted 13 lines

It should be then
backported to stable kernels down to 6.12 LTS.

Viktor

Quoted 8 lines

Maybe we can factor out the loop body and call it from different mechanisms like open-coded loop, bpf_loop or bpf_for depending on the kernel version. But not sure it'd fix the verifier issue though.

Thanks,
Namhyung

#13 lore raw
In reply to Namhyung Kim
Quoted 27 lines · Namhyung Kim
Quoted 5 lines · Viktor Malik
Quoted 4 lines · Namhyung Kim

Hello,

Quoted 1 line · Alexei Starovoitov

On Tue Jun 23, 2026 at 4:25 AM PDT, Viktor Malik wrote:

[SNIP]

Quoted 20 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
tools/perf/util/bpf_skel/augmented_raw_syscalls.bpf.c +5 -0
+	struct args_loop_ctx loop_ctx = {
+		.args = args,
+		.beauty_map = beauty_map,
+		.payload_offset = payload_offset,
+		.value_size = value_size,
Quoted 8 lines
Quoted 8 lines

tools/perf/util/bpf_skel/augmented_raw_syscalls.bpf.c static int augment_sys_enter(void *ctx, struct syscall_enter_args *args) · line 549

Quoted 4 lines
tools/perf/util/bpf_skel/augmented_raw_syscalls.bpf.c +4 -0
+		.output = &output,
+		.do_output = &do_output
+	};
+	iters = bpf_loop(6, process_arg_cb, &loop_ctx, 0);

bpf_loop() is old and generally not recommended. Please use bpf_for() then the diff will be one line change and can scale to any number of args. Not just 6.

Thanks Alexei, I didn't know about this preference.

Quoted 3 lines

One thing we should take care is to support old kernels. The oldest LTS kernel in the kernel.org is 5.10 and bpf_loop() was introduced in 5.17 and bpf_for (bpf_iter_num) was 6.4.

Quoted 5 lines
Quoted 3 lines

The problematic loop was introduced in 6.12 by a68fd6a6cdd3 ("perf
trace: Collect augmented data using BPF") so we should be good using bpf_for. Or is perf from 7.2 supposed to work on 5.10 LTS kernels?

Yep, we'd like to support old kernels.

How much strict are you on this requirement? IMHO, the very least we need to fix the verifier issue is bpf_loop, so that would still not work on 5.10 and 5.15 LTS kernels.

We could probably keep the open-coded loop in case bpf_loop is not available but perf trace would still fail on kernels without bpf_loop for new perf built with Clang>=22. Also, the code would be a bit ugly and I'm not sure how well the feature check for helpers (bpf_loop) works on old kernels.

Viktor

#8 lore raw
In reply to Viktor Malik
Quoted 285 lines · Viktor Malik
Quoted 265 lines · Andrii Nakryiko
Quoted 256 lines · Viktor Malik
Quoted 233 lines · Viktor Malik
Quoted 28 lines · Namhyung Kim

Hello,

Quoted 25 lines · Alexei Starovoitov
Quoted 24 lines · Viktor Malik

The loop for processing syscall args in augment_raw_syscalls has a history of breaking with Clang updates, see e.g. commit 013eb043f37b ("perf trace: Fix BPF loading failure (-E2BIG)") from Clang 15 to 16.

Now, a similar thing happened between Clang 21 and 22. While the issue is mitigated on the main line by a recent verifier update, it remains broken on the 6.12 and 6.18 stable branches:

[linux-6.18.y]# sudo perf trace true
libbpf: prog 'sys_enter': BPF program load failed: -E2BIG
libbpf: prog 'sys_enter': -- BEGIN PROG LOAD LOG --
[...]
BPF program is too large. Processed 1000001 insn
processed 1000001 insns (limit 1000000) max_states_per_insn 40 total_states 37941 peak_states 232 mark_read 0 -- END PROG LOAD LOG --
libbpf: prog 'sys_enter': failed to load: -E2BIG
libbpf: failed to load object 'augmented_raw_syscalls_bpf'
libbpf: failed to load BPF skeleton 'augmented_raw_syscalls_bpf': -E2BIG
Error: failed to get syscall or beauty map fd
[...]

The reason is that the loop is quite complex and the BPF verifier often struggles to prove that it terminates.

Quoted 197 lines
Quoted 5 lines
Quoted 5 lines

Fix the issue by refactoring the loop body into a callback function and calling the bpf_loop helper. This should prevent future breakages of this kind since the callback function has no loops. It also allows to drop a few artificial checks to help the verifier, including the changes introduced by 013eb043f37b.

Thanks for working on this. I encountered this issue before and never found time to take a deeper look yet.

Quoted 188 lines
Quoted 174 lines
Fixes a68fd6a6cdd3 ("perf trace: Collect augmented data using BPF")
Fixes 013eb043f37b ("perf trace: Fix BPF loading failure (-E2BIG)")
---
 .../bpf_skel/augmented_raw_syscalls.bpf.c     | 157 +++++++++++-------
 1 file changed, 96 insertions(+), 61 deletions(-)
tools/perf/util/bpf_skel/augmented_raw_syscalls.bpf.c +85 -60
index 2a6e61864ee0..6d553ed3ac23 100644
@@ -429,15 +429,96 @@ static bool pid_filter__has(struct pids_filtered *pids, pid_t pid)
   return bpf_map_lookup_elem(pids, &pid) != NULL;
 }
 
+struct args_loop_ctx {
+  struct syscall_enter_args *args;
+  unsigned int *beauty_map;
+  void *payload_offset;
+  int value_size;
+  u64 *output;
+  bool *do_output;
+};
+
+static long process_arg_cb(u64 i, void *ctx)
+{
+  /*
+   * Determine what type of argument and how many bytes to read from user space, using the
+   * value in the beauty_map. This is the relation of parameter type and its corresponding
+   * value in the beauty map, and how many bytes we read eventually:
+   *
+   * string: 1                          -> size of string
+   * struct: size of struct             -> size of struct
+   * buffer: -1 * (index of paired len) -> value of paired len (maximum: TRACE_AUG_MAX_BUF)
+   */
+  struct augmented_arg *augmented_arg;
+  struct args_loop_ctx *loop_ctx;
+  int aug_size, size, index;
+  bool augmented;
+  void *arg;
+
+  /* Bounds check for the below map access to help the verifier */
+  if (i < 0 || i >= 6)
+          return 1;
+
+  loop_ctx = (struct args_loop_ctx *)ctx;
+  arg = (void *)loop_ctx->args->args[i];
+  augmented = false;
+  size = loop_ctx->beauty_map[i];
+  aug_size = size; /* size of the augmented data read from user space */
+  augmented_arg = (struct augmented_arg *)loop_ctx->payload_offset;
+
+  if (size == 0 || arg == NULL)
+          return 0; /* continue */
+
+  if (size == 1) { /* string */
+          aug_size = bpf_probe_read_user_str(augmented_arg->value, loop_ctx->value_size, arg);
+          augmented = true;
+  } else if (size > 0 && size <= loop_ctx->value_size) { /* struct */
+          if (!bpf_probe_read_user(augmented_arg->value, size, arg))
+                  augmented = true;
+  } else if (size < 0 && size >= -6) { /* buffer */
+          index = -(size + 1);
+          barrier_var(index); // Prevent clang (noticed with v18) from removing the &= 7 trick.
+          index &= 7;         // Satisfy the bounds checking with the verifier in some kernels.
+          aug_size = loop_ctx->args->args[index];
+
+          if (aug_size > TRACE_AUG_MAX_BUF)
+                  aug_size = TRACE_AUG_MAX_BUF;
+
+          if (aug_size > 0) {
+                  if (!bpf_probe_read_user(augmented_arg->value, aug_size, arg))
+                          augmented = true;
+          }
+  }
+
+  /* Augmented data size is limited to sizeof(augmented_arg->unnamed union with value field) */
+  if (aug_size > loop_ctx->value_size)
+          aug_size = loop_ctx->value_size;
+
+  /* write data to payload */
+  if (augmented) {
+          int written = offsetof(struct augmented_arg, value) + aug_size;
+
+          if (written < 0 || written > sizeof(struct augmented_arg))
+                  return 1; /* break */
+
+          augmented_arg->size = aug_size;
+          *loop_ctx->output += written;
+          loop_ctx->payload_offset += written;
+          *loop_ctx->do_output = true;
+  }
+
+  return 0;
+}
+
 static int augment_sys_enter(void *ctx, struct syscall_enter_args *args)
 {
-  bool augmented, do_output = false;
-  int zero = 0, index, value_size = sizeof(struct augmented_arg) - offsetof(struct augmented_arg, value);
+  bool do_output = false;
+  int zero = 0, value_size = sizeof(struct augmented_arg) - offsetof(struct augmented_arg, value);
   u64 output = 0; /* has to be u64, otherwise it won't pass the verifier */
-  s64 aug_size, size;
   unsigned int nr, *beauty_map;
   struct beauty_payload_enter *payload;
-  void *arg, *payload_offset;
+  void *payload_offset;
+  long iters;
 
   /* fall back to do predefined tail call */
   if (args == NULL)
@@ -457,63 +538,17 @@ static int augment_sys_enter(void *ctx, struct syscall_enter_args *args)
   /* copy the sys_enter header, which has the syscall_nr */
   __builtin_memcpy(&payload->args, args, sizeof(struct syscall_enter_args));
 
-  /*
-   * Determine what type of argument and how many bytes to read from user space, using the
-   * value in the beauty_map. This is the relation of parameter type and its corresponding
-   * value in the beauty map, and how many bytes we read eventually:
-   *
-   * string: 1                          -> size of string
-   * struct: size of struct             -> size of struct
-   * buffer: -1 * (index of paired len) -> value of paired len (maximum: TRACE_AUG_MAX_BUF)
-   */
-  for (int i = 0; i < 6; i++) {
-          arg = (void *)args->args[i];
-          augmented = false;
-          size = beauty_map[i];
-          aug_size = size; /* size of the augmented data read from user space */
-
-          if (size == 0 || arg == NULL)
-                  continue;
-
-          if (size == 1) { /* string */
-                  aug_size = bpf_probe_read_user_str(((struct augmented_arg *)payload_offset)->value, value_size, arg);
-                  /* minimum of 0 to pass the verifier */
-                  if (aug_size < 0)
-                          aug_size = 0;
-
-                  augmented = true;
-          } else if (size > 0 && size <= value_size) { /* struct */
-                  if (!bpf_probe_read_user(((struct augmented_arg *)payload_offset)->value, size, arg))
-                          augmented = true;
-          } else if ((int)size < 0 && size >= -6) { /* buffer */
-                  index = -(size + 1);
-                  barrier_var(index); // Prevent clang (noticed with v18) from removing the &= 7 trick.
-                  index &= 7;         // Satisfy the bounds checking with the verifier in some kernels.
-                  aug_size = args->args[index] > TRACE_AUG_MAX_BUF ? TRACE_AUG_MAX_BUF : args->args[index];
-
-                  if (aug_size > 0) {
-                          if (!bpf_probe_read_user(((struct augmented_arg *)payload_offset)->value, aug_size, arg))
-                                  augmented = true;
-                  }
-          }
-
-          /* Augmented data size is limited to sizeof(augmented_arg->unnamed union with value field) */
-          if (aug_size > value_size)
-                  aug_size = value_size;
-
-          /* write data to payload */
-          if (augmented) {
-                  int written = offsetof(struct augmented_arg, value) + aug_size;
-
-                  if (written < 0 || written > sizeof(struct augmented_arg))
-                          return 1;
-
-                  ((struct augmented_arg *)payload_offset)->size = aug_size;
-                  output += written;
-                  payload_offset += written;
-                  do_output = true;
-          }

tools/perf/util/bpf_skel/augmented_raw_syscalls.bpf.c static int augment_sys_enter(void *ctx, struct syscall_enter_args *args) · line 549

Quoted 10 lines
tools/perf/util/bpf_skel/augmented_raw_syscalls.bpf.c +9 -1
-  }
+  struct args_loop_ctx loop_ctx = {
+          .args = args,
+          .beauty_map = beauty_map,
+          .payload_offset = payload_offset,
+          .value_size = value_size,
+          .output = &output,
+          .do_output = &do_output
+  };
+  iters = bpf_loop(6, process_arg_cb, &loop_ctx, 0);

bpf_loop() is old and generally not recommended. Please use bpf_for() then the diff will be one line change and can scale to any number of args. Not just 6.

Thanks Alexei, I didn't know about this preference.

Quoted 3 lines

One thing we should take care is to support old kernels. The oldest LTS kernel in the kernel.org is 5.10 and bpf_loop() was introduced in 5.17 and bpf_for (bpf_iter_num) was 6.4.

Quoted 5 lines

The problematic loop was introduced in 6.12 by a68fd6a6cdd3 ("perf
trace: Collect augmented data using BPF") so we should be good using bpf_for. Or is perf from 7.2 supposed to work on 5.10 LTS kernels?

I'll refactor with bpf_for and will send v2.

Or I won't. It turns out that just swapping the for loop for bpf_for leads to -E2BIG from the verifier again. Looking at the verifier log, it fails to find equivalence between states at the loop head:

    [...]
    78: (85) call bpf_iter_num_next#84922 [...]
fp-56=map_value(map=beauty_payload_,ks=4,vs=24688,imm=112)
    [...]
    78: (85) call bpf_iter_num_next#84922 [...]
fp-56=map_value(map=beauty_payload_,ks=4,vs=24688,imm=120)
    [...]

IMHO, the reason is that payload_offset, which points to the beauty_payload_enter_map entry, gets updated in every iteration.

Quoted 7 lines

This could be probably fixed on the perf side by reworking how augmented args are stored but at this point, bpf_loop sounds like an easier and more reliable approach.

Let me know if anyone has objections, otherwise I'll send v2 of the bpf_loop approach, with suggestions from Sashiko incorporated.

Quoted 7 lines

I'd still try to adapt bpf_for(), it's a much better code structure. You probably need to add a bounding checking/confirming if () condition validating that offset at which you access map_value is always correct. And/or you might need barrier_var() before using i, because bpf_for() macro does bounds checking (check the macro itself), but compiler often will reorder instructions leading to verifier complaints.

I gave it a try but wasn't successful so far. I think that the problem is that while it would be possible to add an upper bound condition for payload_offset, the verifier tracks the value of payload_offset too precisely (as map_value(..., imm=X) with a concrete offset) and never merges states with different offsets. And since there are multiple branches inside the loop, each incrementing payload_offset by a different value, the verifier seems to fork its state on each branch, effectively leading to the amount of states growing exponentially and hitting the jump limit.

Quoted 1 line

To me, bpf_loop sounds like a more reliable choice in this situation.

correctly verified bpf_loop would basically have to follow the same logic, so if it works with bpf_loop, it should work with bpf_for. Is it possible to share your bpf_for-based code in some branch to try locally? I'm sure it can be done one way or another.

Quoted 28 lines

It's also older, which is good in this case, since compatibility with older kernels seems to be important for perf (see other messages in the thread).

I'm also wondering if the verifier could be improved to handle these cases but that's a different discussion.

Quoted 20 lines
Quoted 19 lines

Thanks,
Viktor

Quoted 13 lines

It should be then
backported to stable kernels down to 6.12 LTS.

Viktor

Quoted 8 lines

Maybe we can factor out the loop body and call it from different mechanisms like open-coded loop, bpf_loop or bpf_for depending on the kernel version. But not sure it'd fix the verifier issue though.

Thanks,
Namhyung

#9 lore raw
Quoted 286 lines · Andrii Nakryiko
Quoted 285 lines · Viktor Malik
Quoted 265 lines · Andrii Nakryiko
Quoted 256 lines · Viktor Malik
Quoted 233 lines · Viktor Malik
Quoted 28 lines · Namhyung Kim

Hello,

Quoted 25 lines · Alexei Starovoitov
Quoted 24 lines · Viktor Malik

The loop for processing syscall args in augment_raw_syscalls has a history of breaking with Clang updates, see e.g. commit 013eb043f37b ("perf trace: Fix BPF loading failure (-E2BIG)") from Clang 15 to 16.

Now, a similar thing happened between Clang 21 and 22. While the issue is mitigated on the main line by a recent verifier update, it remains broken on the 6.12 and 6.18 stable branches:

[linux-6.18.y]# sudo perf trace true
libbpf: prog 'sys_enter': BPF program load failed: -E2BIG
libbpf: prog 'sys_enter': -- BEGIN PROG LOAD LOG --
[...]
BPF program is too large. Processed 1000001 insn
processed 1000001 insns (limit 1000000) max_states_per_insn 40 total_states 37941 peak_states 232 mark_read 0 -- END PROG LOAD LOG --
libbpf: prog 'sys_enter': failed to load: -E2BIG
libbpf: failed to load object 'augmented_raw_syscalls_bpf'
libbpf: failed to load BPF skeleton 'augmented_raw_syscalls_bpf': -E2BIG
Error: failed to get syscall or beauty map fd
[...]

The reason is that the loop is quite complex and the BPF verifier often struggles to prove that it terminates.

Quoted 197 lines
Quoted 5 lines
Quoted 5 lines

Fix the issue by refactoring the loop body into a callback function and calling the bpf_loop helper. This should prevent future breakages of this kind since the callback function has no loops. It also allows to drop a few artificial checks to help the verifier, including the changes introduced by 013eb043f37b.

Thanks for working on this. I encountered this issue before and never found time to take a deeper look yet.

Quoted 188 lines
Quoted 174 lines
Fixes a68fd6a6cdd3 ("perf trace: Collect augmented data using BPF")
Fixes 013eb043f37b ("perf trace: Fix BPF loading failure (-E2BIG)")
---
 .../bpf_skel/augmented_raw_syscalls.bpf.c     | 157 +++++++++++-------
 1 file changed, 96 insertions(+), 61 deletions(-)
tools/perf/util/bpf_skel/augmented_raw_syscalls.bpf.c +85 -60
index 2a6e61864ee0..6d553ed3ac23 100644
@@ -429,15 +429,96 @@ static bool pid_filter__has(struct pids_filtered *pids, pid_t pid)
   return bpf_map_lookup_elem(pids, &pid) != NULL;
 }
 
+struct args_loop_ctx {
+  struct syscall_enter_args *args;
+  unsigned int *beauty_map;
+  void *payload_offset;
+  int value_size;
+  u64 *output;
+  bool *do_output;
+};
+
+static long process_arg_cb(u64 i, void *ctx)
+{
+  /*
+   * Determine what type of argument and how many bytes to read from user space, using the
+   * value in the beauty_map. This is the relation of parameter type and its corresponding
+   * value in the beauty map, and how many bytes we read eventually:
+   *
+   * string: 1                          -> size of string
+   * struct: size of struct             -> size of struct
+   * buffer: -1 * (index of paired len) -> value of paired len (maximum: TRACE_AUG_MAX_BUF)
+   */
+  struct augmented_arg *augmented_arg;
+  struct args_loop_ctx *loop_ctx;
+  int aug_size, size, index;
+  bool augmented;
+  void *arg;
+
+  /* Bounds check for the below map access to help the verifier */
+  if (i < 0 || i >= 6)
+          return 1;
+
+  loop_ctx = (struct args_loop_ctx *)ctx;
+  arg = (void *)loop_ctx->args->args[i];
+  augmented = false;
+  size = loop_ctx->beauty_map[i];
+  aug_size = size; /* size of the augmented data read from user space */
+  augmented_arg = (struct augmented_arg *)loop_ctx->payload_offset;
+
+  if (size == 0 || arg == NULL)
+          return 0; /* continue */
+
+  if (size == 1) { /* string */
+          aug_size = bpf_probe_read_user_str(augmented_arg->value, loop_ctx->value_size, arg);
+          augmented = true;
+  } else if (size > 0 && size <= loop_ctx->value_size) { /* struct */
+          if (!bpf_probe_read_user(augmented_arg->value, size, arg))
+                  augmented = true;
+  } else if (size < 0 && size >= -6) { /* buffer */
+          index = -(size + 1);
+          barrier_var(index); // Prevent clang (noticed with v18) from removing the &= 7 trick.
+          index &= 7;         // Satisfy the bounds checking with the verifier in some kernels.
+          aug_size = loop_ctx->args->args[index];
+
+          if (aug_size > TRACE_AUG_MAX_BUF)
+                  aug_size = TRACE_AUG_MAX_BUF;
+
+          if (aug_size > 0) {
+                  if (!bpf_probe_read_user(augmented_arg->value, aug_size, arg))
+                          augmented = true;
+          }
+  }
+
+  /* Augmented data size is limited to sizeof(augmented_arg->unnamed union with value field) */
+  if (aug_size > loop_ctx->value_size)
+          aug_size = loop_ctx->value_size;
+
+  /* write data to payload */
+  if (augmented) {
+          int written = offsetof(struct augmented_arg, value) + aug_size;
+
+          if (written < 0 || written > sizeof(struct augmented_arg))
+                  return 1; /* break */
+
+          augmented_arg->size = aug_size;
+          *loop_ctx->output += written;
+          loop_ctx->payload_offset += written;
+          *loop_ctx->do_output = true;
+  }
+
+  return 0;
+}
+
 static int augment_sys_enter(void *ctx, struct syscall_enter_args *args)
 {
-  bool augmented, do_output = false;
-  int zero = 0, index, value_size = sizeof(struct augmented_arg) - offsetof(struct augmented_arg, value);
+  bool do_output = false;
+  int zero = 0, value_size = sizeof(struct augmented_arg) - offsetof(struct augmented_arg, value);
   u64 output = 0; /* has to be u64, otherwise it won't pass the verifier */
-  s64 aug_size, size;
   unsigned int nr, *beauty_map;
   struct beauty_payload_enter *payload;
-  void *arg, *payload_offset;
+  void *payload_offset;
+  long iters;
 
   /* fall back to do predefined tail call */
   if (args == NULL)
@@ -457,63 +538,17 @@ static int augment_sys_enter(void *ctx, struct syscall_enter_args *args)
   /* copy the sys_enter header, which has the syscall_nr */
   __builtin_memcpy(&payload->args, args, sizeof(struct syscall_enter_args));
 
-  /*
-   * Determine what type of argument and how many bytes to read from user space, using the
-   * value in the beauty_map. This is the relation of parameter type and its corresponding
-   * value in the beauty map, and how many bytes we read eventually:
-   *
-   * string: 1                          -> size of string
-   * struct: size of struct             -> size of struct
-   * buffer: -1 * (index of paired len) -> value of paired len (maximum: TRACE_AUG_MAX_BUF)
-   */
-  for (int i = 0; i < 6; i++) {
-          arg = (void *)args->args[i];
-          augmented = false;
-          size = beauty_map[i];
-          aug_size = size; /* size of the augmented data read from user space */
-
-          if (size == 0 || arg == NULL)
-                  continue;
-
-          if (size == 1) { /* string */
-                  aug_size = bpf_probe_read_user_str(((struct augmented_arg *)payload_offset)->value, value_size, arg);
-                  /* minimum of 0 to pass the verifier */
-                  if (aug_size < 0)
-                          aug_size = 0;
-
-                  augmented = true;
-          } else if (size > 0 && size <= value_size) { /* struct */
-                  if (!bpf_probe_read_user(((struct augmented_arg *)payload_offset)->value, size, arg))
-                          augmented = true;
-          } else if ((int)size < 0 && size >= -6) { /* buffer */
-                  index = -(size + 1);
-                  barrier_var(index); // Prevent clang (noticed with v18) from removing the &= 7 trick.
-                  index &= 7;         // Satisfy the bounds checking with the verifier in some kernels.
-                  aug_size = args->args[index] > TRACE_AUG_MAX_BUF ? TRACE_AUG_MAX_BUF : args->args[index];
-
-                  if (aug_size > 0) {
-                          if (!bpf_probe_read_user(((struct augmented_arg *)payload_offset)->value, aug_size, arg))
-                                  augmented = true;
-                  }
-          }
-
-          /* Augmented data size is limited to sizeof(augmented_arg->unnamed union with value field) */
-          if (aug_size > value_size)
-                  aug_size = value_size;
-
-          /* write data to payload */
-          if (augmented) {
-                  int written = offsetof(struct augmented_arg, value) + aug_size;
-
-                  if (written < 0 || written > sizeof(struct augmented_arg))
-                          return 1;
-
-                  ((struct augmented_arg *)payload_offset)->size = aug_size;
-                  output += written;
-                  payload_offset += written;
-                  do_output = true;
-          }

tools/perf/util/bpf_skel/augmented_raw_syscalls.bpf.c static int augment_sys_enter(void *ctx, struct syscall_enter_args *args) · line 549

Quoted 10 lines
tools/perf/util/bpf_skel/augmented_raw_syscalls.bpf.c +9 -1
-  }
+  struct args_loop_ctx loop_ctx = {
+          .args = args,
+          .beauty_map = beauty_map,
+          .payload_offset = payload_offset,
+          .value_size = value_size,
+          .output = &output,
+          .do_output = &do_output
+  };
+  iters = bpf_loop(6, process_arg_cb, &loop_ctx, 0);

bpf_loop() is old and generally not recommended. Please use bpf_for() then the diff will be one line change and can scale to any number of args. Not just 6.

Thanks Alexei, I didn't know about this preference.

Quoted 3 lines

One thing we should take care is to support old kernels. The oldest LTS kernel in the kernel.org is 5.10 and bpf_loop() was introduced in 5.17 and bpf_for (bpf_iter_num) was 6.4.

Quoted 5 lines

The problematic loop was introduced in 6.12 by a68fd6a6cdd3 ("perf
trace: Collect augmented data using BPF") so we should be good using bpf_for. Or is perf from 7.2 supposed to work on 5.10 LTS kernels?

I'll refactor with bpf_for and will send v2.

Or I won't. It turns out that just swapping the for loop for bpf_for leads to -E2BIG from the verifier again. Looking at the verifier log, it fails to find equivalence between states at the loop head:

    [...]
    78: (85) call bpf_iter_num_next#84922 [...]
fp-56=map_value(map=beauty_payload_,ks=4,vs=24688,imm=112)
    [...]
    78: (85) call bpf_iter_num_next#84922 [...]
fp-56=map_value(map=beauty_payload_,ks=4,vs=24688,imm=120)
    [...]

IMHO, the reason is that payload_offset, which points to the beauty_payload_enter_map entry, gets updated in every iteration.

Quoted 7 lines

This could be probably fixed on the perf side by reworking how augmented args are stored but at this point, bpf_loop sounds like an easier and more reliable approach.

Let me know if anyone has objections, otherwise I'll send v2 of the bpf_loop approach, with suggestions from Sashiko incorporated.

Quoted 7 lines

I'd still try to adapt bpf_for(), it's a much better code structure. You probably need to add a bounding checking/confirming if () condition validating that offset at which you access map_value is always correct. And/or you might need barrier_var() before using i, because bpf_for() macro does bounds checking (check the macro itself), but compiler often will reorder instructions leading to verifier complaints.

I gave it a try but wasn't successful so far. I think that the problem is that while it would be possible to add an upper bound condition for payload_offset, the verifier tracks the value of payload_offset too precisely (as map_value(..., imm=X) with a concrete offset) and never merges states with different offsets. And since there are multiple branches inside the loop, each incrementing payload_offset by a different value, the verifier seems to fork its state on each branch, effectively leading to the amount of states growing exponentially and hitting the jump limit.

Quoted 4 lines
Quoted 1 line

To me, bpf_loop sounds like a more reliable choice in this situation.

correctly verified bpf_loop would basically have to follow the same logic, so if it works with bpf_loop, it should work with bpf_for.

Are you sure about that? My perception is that the bpf_loop callback is only verified once in a single pass. On the contrary, bpf_for is a normal loop, for which the verifier needs to prove that after some iteration, we get to the state seen in a previous iteration (to prune the state). Which never happens here because the offset to beauty_payload_enter_map (the payload_offset var) is tracked precisely and causes state forks on every condition inside the loop.

Quoted 3 lines

Is
it possible to share your bpf_for-based code in some branch to try locally? I'm sure it can be done one way or another.

The change is super-simple, I can as well share it here. It's just the matter of using bpf_for with two additional suggested mechanisms, barrier_var and a bounds check for payload_offset:

tools/perf/util/bpf_skel/augmented_raw_syscalls.bpf.c +6 -2
index 2a6e61864ee0..341d77a78949 100644
@@ -432,7 +432,7 @@ static bool pid_filter__has(struct pids_filtered *pids, pid_t pid)
 static int augment_sys_enter(void *ctx, struct syscall_enter_args *args)
 {
        bool augmented, do_output = false;
-       int zero = 0, index, value_size = sizeof(struct augmented_arg) - offsetof(struct augmented_arg, value);
+       int zero = 0, i, index, value_size = sizeof(struct augmented_arg) - offsetof(struct augmented_arg, value);
        u64 output = 0; /* has to be u64, otherwise it won't pass the verifier */
        s64 aug_size, size;
        unsigned int nr, *beauty_map;
@@ -466,12 +466,16 @@ static int augment_sys_enter(void *ctx, struct syscall_enter_args *args)
         * struct: size of struct             -> size of struct
         * buffer: -1 * (index of paired len) -> value of paired len (maximum: TRACE_AUG_MAX_BUF)
         */
-       for (int i = 0; i < 6; i++) {
+       bpf_for(i, 0, 6) {
+               barrier_var(i);
                arg = (void *)args->args[i];
                augmented = false;
                size = beauty_map[i];
                aug_size = size; /* size of the augmented data read from user space */
 
+               if (payload_offset + sizeof(struct augmented_arg) > (void *)payload + sizeof(struct beauty_payload_enter))
+                       break;
+
                if (size == 0 || arg == NULL)
                        continue;
 

Thanks a lot for the help!
Viktor

maintainer
#14 lore raw
In reply to Viktor Malik
Quoted 32 lines · Viktor Malik
Quoted 31 lines · Namhyung Kim
Quoted 5 lines · Viktor Malik
Quoted 4 lines · Namhyung Kim

Hello,

Quoted 1 line · Alexei Starovoitov

On Tue Jun 23, 2026 at 4:25 AM PDT, Viktor Malik wrote:

[SNIP]

Quoted 23 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
tools/perf/util/bpf_skel/augmented_raw_syscalls.bpf.c +5 -0
+	struct args_loop_ctx loop_ctx = {
+		.args = args,
+		.beauty_map = beauty_map,
+		.payload_offset = payload_offset,
+		.value_size = value_size,
Quoted 8 lines
Quoted 8 lines

tools/perf/util/bpf_skel/augmented_raw_syscalls.bpf.c static int augment_sys_enter(void *ctx, struct syscall_enter_args *args) · line 549

Quoted 4 lines
tools/perf/util/bpf_skel/augmented_raw_syscalls.bpf.c +4 -0
+		.output = &output,
+		.do_output = &do_output
+	};
+	iters = bpf_loop(6, process_arg_cb, &loop_ctx, 0);

bpf_loop() is old and generally not recommended. Please use bpf_for() then the diff will be one line change and can scale to any number of args. Not just 6.

Thanks Alexei, I didn't know about this preference.

Quoted 3 lines

One thing we should take care is to support old kernels. The oldest LTS kernel in the kernel.org is 5.10 and bpf_loop() was introduced in 5.17 and bpf_for (bpf_iter_num) was 6.4.

The problematic loop was introduced in 6.12 by a68fd6a6cdd3 ("perf
trace: Collect augmented data using BPF") so we should be good using bpf_for. Or is perf from 7.2 supposed to work on 5.10 LTS kernels?

Quoted 5 lines
Quoted 1 line

Yep, we'd like to support old kernels.

How much strict are you on this requirement? IMHO, the very least we need to fix the verifier issue is bpf_loop, so that would still not work on 5.10 and 5.15 LTS kernels.

I don't think it's an absolute requirement, but I think we don't want to break any existing working setup (old kernel + old compiler).

Quoted 6 lines

We could probably keep the open-coded loop in case bpf_loop is not available but perf trace would still fail on kernels without bpf_loop for new perf built with Clang>=22. Also, the code would be a bit ugly and I'm not sure how well the feature check for helpers (bpf_loop) works on old kernels.

Any chance process_arg_cb() can be called directly in the regular for loop on old kernels?

Thanks,
Namhyung

#15 lore raw
Quoted 43 lines · Namhyung Kim
Quoted 32 lines · Viktor Malik
Quoted 31 lines · Namhyung Kim
Quoted 5 lines · Viktor Malik
Quoted 4 lines · Namhyung Kim

Hello,

Quoted 1 line · Alexei Starovoitov

On Tue Jun 23, 2026 at 4:25 AM PDT, Viktor Malik wrote:

[SNIP]

Quoted 23 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
(quoted diff) +5 -0
+	struct args_loop_ctx loop_ctx = {
+		.args = args,
+		.beauty_map = beauty_map,
+		.payload_offset = payload_offset,
+		.value_size = value_size,
Quoted 8 lines
Quoted 8 lines

Review

Quoted 4 lines
(quoted diff) +4 -0
+		.output = &output,
+		.do_output = &do_output
+	};
+	iters = bpf_loop(6, process_arg_cb, &loop_ctx, 0);

bpf_loop() is old and generally not recommended. Please use bpf_for() then the diff will be one line change and can scale to any number of args. Not just 6.

Thanks Alexei, I didn't know about this preference.

Quoted 3 lines

One thing we should take care is to support old kernels. The oldest LTS kernel in the kernel.org is 5.10 and bpf_loop() was introduced in 5.17 and bpf_for (bpf_iter_num) was 6.4.

The problematic loop was introduced in 6.12 by a68fd6a6cdd3 ("perf
trace: Collect augmented data using BPF") so we should be good using bpf_for. Or is perf from 7.2 supposed to work on 5.10 LTS kernels?

Quoted 5 lines
Quoted 1 line

Yep, we'd like to support old kernels.

How much strict are you on this requirement? IMHO, the very least we need to fix the verifier issue is bpf_loop, so that would still not work on 5.10 and 5.15 LTS kernels.

I don't think it's an absolute requirement, but I think we don't want to break any existing working setup (old kernel + old compiler).

Quoted 1 line
Quoted 8 lines
Quoted 5 lines

We could probably keep the open-coded loop in case bpf_loop is not available but perf trace would still fail on kernels without bpf_loop for new perf built with Clang>=22. Also, the code would be a bit ugly and I'm not sure how well the feature check for helpers (bpf_loop) works on old kernels.

Any chance process_arg_cb() can be called directly in the regular for loop on old kernels?

That's my thinking, too. Should be pretty straightforward, I'm going to give it a try in v2.

Viktor

#16 lore raw
Quoted 50 lines · Viktor Malik
Quoted 49 lines · Namhyung Kim
Quoted 32 lines · Viktor Malik
Quoted 31 lines · Namhyung Kim
Quoted 5 lines · Viktor Malik
Quoted 4 lines · Namhyung Kim

Hello,

Quoted 1 line · Alexei Starovoitov

On Tue Jun 23, 2026 at 4:25 AM PDT, Viktor Malik wrote:

[SNIP]

Quoted 23 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
(quoted diff) +5 -0
+	struct args_loop_ctx loop_ctx = {
+		.args = args,
+		.beauty_map = beauty_map,
+		.payload_offset = payload_offset,
+		.value_size = value_size,
Quoted 8 lines
Quoted 8 lines

Review

Quoted 4 lines
(quoted diff) +4 -0
+		.output = &output,
+		.do_output = &do_output
+	};
+	iters = bpf_loop(6, process_arg_cb, &loop_ctx, 0);

bpf_loop() is old and generally not recommended. Please use bpf_for() then the diff will be one line change and can scale to any number of args. Not just 6.

Thanks Alexei, I didn't know about this preference.

Quoted 3 lines

One thing we should take care is to support old kernels. The oldest LTS kernel in the kernel.org is 5.10 and bpf_loop() was introduced in 5.17 and bpf_for (bpf_iter_num) was 6.4.

The problematic loop was introduced in 6.12 by a68fd6a6cdd3 ("perf
trace: Collect augmented data using BPF") so we should be good using bpf_for. Or is perf from 7.2 supposed to work on 5.10 LTS kernels?

Quoted 5 lines
Quoted 1 line

Yep, we'd like to support old kernels.

How much strict are you on this requirement? IMHO, the very least we need to fix the verifier issue is bpf_loop, so that would still not work on 5.10 and 5.15 LTS kernels.

I don't think it's an absolute requirement, but I think we don't want to break any existing working setup (old kernel + old compiler).

Quoted 6 lines

We could probably keep the open-coded loop in case bpf_loop is not available but perf trace would still fail on kernels without bpf_loop for new perf built with Clang>=22. Also, the code would be a bit ugly and I'm not sure how well the feature check for helpers (bpf_loop) works on old kernels.

Quoted 5 lines
Quoted 2 lines

Any chance process_arg_cb() can be called directly in the regular for loop on old kernels?

That's my thinking, too. Should be pretty straightforward, I'm going to give it a try in v2.

Btw, I just noticed that util/bpf_skel/lock_contention.bpf.c already uses bpf_loop without any fallback so newer perf (at least perf lock) won't be usable on kernels without bpf_loop anyways.

#10 lore raw
In reply to Viktor Malik
Quoted 54 lines · Viktor Malik
Quoted 52 lines · Andrii Nakryiko
Quoted 51 lines · Viktor Malik
Quoted 49 lines · Andrii Nakryiko
Quoted 48 lines · Viktor Malik
Quoted 46 lines · Viktor Malik
Quoted 45 lines · Namhyung Kim

Hello,

Quoted 22 lines · Alexei Starovoitov
Quoted 21 lines · Viktor Malik

The loop for processing syscall args in augment_raw_syscalls has a history of breaking with Clang updates, see e.g. commit 013eb043f37b ("perf trace: Fix BPF loading failure (-E2BIG)") from Clang 15 to 16.

Now, a similar thing happened between Clang 21 and 22. While the issue is mitigated on the main line by a recent verifier update, it remains broken on the 6.12 and 6.18 stable branches:

[linux-6.18.y]# sudo perf trace true
libbpf: prog 'sys_enter': BPF program load failed: -E2BIG
libbpf: prog 'sys_enter': -- BEGIN PROG LOAD LOG --
[...]
BPF program is too large. Processed 1000001 insn
processed 1000001 insns (limit 1000000) max_states_per_insn 40 total_states 37941 peak_states 232 mark_read 0 -- END PROG LOAD LOG --
libbpf: prog 'sys_enter': failed to load: -E2BIG
libbpf: failed to load object 'augmented_raw_syscalls_bpf'
libbpf: failed to load BPF skeleton 'augmented_raw_syscalls_bpf': -E2BIG
Error: failed to get syscall or beauty map fd
[...]

Quoted 8 lines
Quoted 8 lines

The reason is that the loop is quite complex and the BPF verifier often struggles to prove that it terminates.

Fix the issue by refactoring the loop body into a callback function and calling the bpf_loop helper. This should prevent future breakages of this kind since the callback function has no loops. It also allows to drop a few artificial checks to help the verifier, including the changes introduced by 013eb043f37b.

Thanks for working on this. I encountered this issue before and never found time to take a deeper look yet.

Quoted 8 lines
Quoted 8 lines
Fixes a68fd6a6cdd3 ("perf trace: Collect augmented data using BPF")
Fixes 013eb043f37b ("perf trace: Fix BPF loading failure (-E2BIG)")
---
 .../bpf_skel/augmented_raw_syscalls.bpf.c     | 157 +++++++++++-------
 1 file changed, 96 insertions(+), 61 deletions(-)

[...]

Quoted 72 lines
Quoted 67 lines
Quoted 67 lines
Quoted 49 lines
Quoted 41 lines
Quoted 20 lines
Quoted 10 lines
Quoted 10 lines
Quoted 9 lines
(quoted diff) +9 -0
+  struct args_loop_ctx loop_ctx = {
+          .args = args,
+          .beauty_map = beauty_map,
+          .payload_offset = payload_offset,
+          .value_size = value_size,
+          .output = &output,
+          .do_output = &do_output
+  };
+  iters = bpf_loop(6, process_arg_cb, &loop_ctx, 0);
Quoted 3 lines
Quoted 3 lines

bpf_loop() is old and generally not recommended. Please use bpf_for() then the diff will be one line change and can scale to any number of args. Not just 6.

Thanks Alexei, I didn't know about this preference.

Quoted 3 lines

One thing we should take care is to support old kernels. The oldest LTS kernel in the kernel.org is 5.10 and bpf_loop() was introduced in 5.17 and bpf_for (bpf_iter_num) was 6.4.

Quoted 5 lines

The problematic loop was introduced in 6.12 by a68fd6a6cdd3 ("perf
trace: Collect augmented data using BPF") so we should be good using bpf_for. Or is perf from 7.2 supposed to work on 5.10 LTS kernels?

I'll refactor with bpf_for and will send v2.

Or I won't. It turns out that just swapping the for loop for bpf_for leads to -E2BIG from the verifier again. Looking at the verifier log, it fails to find equivalence between states at the loop head:

    [...]
    78: (85) call bpf_iter_num_next#84922 [...]
fp-56=map_value(map=beauty_payload_,ks=4,vs=24688,imm=112)
    [...]
    78: (85) call bpf_iter_num_next#84922 [...]
fp-56=map_value(map=beauty_payload_,ks=4,vs=24688,imm=120)
    [...]

IMHO, the reason is that payload_offset, which points to the beauty_payload_enter_map entry, gets updated in every iteration.

Quoted 7 lines

This could be probably fixed on the perf side by reworking how augmented args are stored but at this point, bpf_loop sounds like an easier and more reliable approach.

Let me know if anyone has objections, otherwise I'll send v2 of the bpf_loop approach, with suggestions from Sashiko incorporated.

Quoted 7 lines

I'd still try to adapt bpf_for(), it's a much better code structure. You probably need to add a bounding checking/confirming if () condition validating that offset at which you access map_value is always correct. And/or you might need barrier_var() before using i, because bpf_for() macro does bounds checking (check the macro itself), but compiler often will reorder instructions leading to verifier complaints.

I gave it a try but wasn't successful so far. I think that the problem is that while it would be possible to add an upper bound condition for payload_offset, the verifier tracks the value of payload_offset too precisely (as map_value(..., imm=X) with a concrete offset) and never merges states with different offsets. And since there are multiple branches inside the loop, each incrementing payload_offset by a different value, the verifier seems to fork its state on each branch, effectively leading to the amount of states growing exponentially and hitting the jump limit.

Quoted 4 lines
Quoted 1 line

To me, bpf_loop sounds like a more reliable choice in this situation.

correctly verified bpf_loop would basically have to follow the same logic, so if it works with bpf_loop, it should work with bpf_for.

Quoted 7 lines

Are you sure about that? My perception is that the bpf_loop callback is only verified once in a single pass. On the contrary, bpf_for is a normal loop, for which the verifier needs to prove that after some iteration, we get to the state seen in a previous iteration (to prune the state). Which never happens here because the offset to beauty_payload_enter_map (the payload_offset var) is tracked precisely and causes state forks on every condition inside the loop.

Hey Viktor,

Sorry for taking so long to get back.

Answering your question about bpf_loop() vs bpf_for() they are conceptually the same from verifier POV, so they are verified similarly. Earlier (buggier) versions of verifier did have a loophole where we verifier bpf_loop() in more laxed single-shot way, but that's not correct. We have since fixed that and it (bpf_loop) now has to "prove" convergence just like bpf_for().

Anyways, the biggest issue with "normal" unrolled BPF loop is that people tend to write it such that there is some carry-over state between each iteration (like output variable which tracks advancing but bounded offset) which, with fixed number of iterations allows verifier to prove everything is bounded.

This model is really-really bad for bpf_for() because it doesn't allow convergence. The trick is to structure each iteration as independent piece of calculation where the state outside of bpf_for() loop stays as unspecific/imprecise as possible, which at the beginning of the loop you revalidate invariants, if necessary (e.g., reestablish map_value offset boundaries).

Anyways, it needed a bit of persuasion, but here's the verification result and gmail-butchered diff below. The trick is in making output imprecise (force verifier to forget its tracked range), so it doesn't differ between iterations from verifier POV. That's what the global ZERO allows to do. (We've discussed w/ Alexei and Eduard adding special instruction to force scalar register into imprecise, it would be a cleaner solution here, alas we never got anywhere with this, unfortunately).

Processing 'augmented_raw_syscalls.bpf.o'...
PROCESSING ./util/bpf_skel/.tmp/augmented_raw_syscalls.bpf.o/sys_enter, DURATION US: 1129, VERDICT: success, VERIFIER LOG:
verification time 1129 usec
stack depth 64
processed 547 insns (limit 1000000) max_states_per_insn 4 total_states 38 peak_states 67 mark_read 0

File Program Verdict Duration (us) Insns
States Program size Jited size
---------------------------- --------- ------- ------------- -----
------ ------------ ----------

augmented_raw_syscalls.bpf.o  sys_enter  success           1129    547
     38           172         917
----------------------------  ---------  -------  -------------  -----

------ ------------ ----------

The diff:

(quoted diff) +0 -0

b/tools/perf/util/bpf_skel/augmented_raw_syscalls.bpf.c index 2a6e61864ee0..8436368ba203 100644

tools/perf/util/bpf_skel/augmented_raw_syscalls.bpf.c +0 -0
@@ -429,15 +429,17 @@ static bool pid_filter__has(struct pids_filtered

*pids, pid_t pid)

        return bpf_map_lookup_elem(pids, &pid) != NULL;

}

+u64 ZERO = 0;
+
static int augment_sys_enter(void *ctx, struct syscall_enter_args *args)

 {
        bool augmented, do_output = false;

- int zero = 0, index, value_size = sizeof(struct augmented_arg)
- offsetof(struct augmented_arg, value);
+ int i, zero = 0, index, value_size = sizeof(struct
augmented_arg) - offsetof(struct augmented_arg, value);
u64 output = 0; /* has to be u64, otherwise it won't pass the
verifier */

        s64 aug_size, size;
        unsigned int nr, *beauty_map;
        struct beauty_payload_enter *payload;

- void *arg, *payload_offset;
+ void *arg;

        /* fall back to do predefined tail call */
        if (args == NULL)

@@ -449,7 +451,6 @@ static int augment_sys_enter(void *ctx, struct syscall_enter_args *args)

        /* set up payload for output */
        payload        = bpf_map_lookup_elem(&beauty_payload_enter_map, &zero);

- payload_offset = (void *)&payload->aug_args;

        if (beauty_map == NULL || payload == NULL)
                return 1;

@@ -466,7 +467,7 @@ static int augment_sys_enter(void *ctx, struct syscall_enter_args *args)
* struct: size of struct -> size of struct
* buffer: -1 * (index of paired len) -> value of paired len
(maximum: TRACE_AUG_MAX_BUF)

         */
-       for (int i = 0; i < 6; i++) {
+       bpf_for(i, 0, 6) {
                arg = (void *)args->args[i];
                augmented = false;
                size = beauty_map[i];
@@ -475,6 +476,11 @@ static int augment_sys_enter(void *ctx, struct
syscall_enter_args *args)
                if (size == 0 || arg == NULL)
                        continue;

+ if (output > sizeof(payload->aug_args) -
sizeof(payload->aug_args[0]))
+ break; /* can't/shouldn't happen */
+ barrier_var(output);
+ void *payload_offset = (void *)&payload->aug_args + output;
+
if (size == 1) { /* string */
aug_size = bpf_probe_read_user_str(((struct

augmented_arg *)payload_offset)->value, value_size, arg);
                        /* minimum of 0 to pass the verifier */

@@ -510,7 +516,7 @@ static int augment_sys_enter(void *ctx, struct syscall_enter_args *args)

                        ((struct augmented_arg *)payload_offset)->size
= aug_size;
                        output += written;
-                       payload_offset += written;
+                       output += ZERO; /* forget range */
                        do_output = true;
                }
        }
Quoted 44 lines
Quoted 4 lines

Is
it possible to share your bpf_for-based code in some branch to try locally? I'm sure it can be done one way or another.

The change is super-simple, I can as well share it here. It's just the matter of using bpf_for with two additional suggested mechanisms, barrier_var and a bounds check for payload_offset:

tools/perf/util/bpf_skel/augmented_raw_syscalls.bpf.c +6 -2
index 2a6e61864ee0..341d77a78949 100644
@@ -432,7 +432,7 @@ static bool pid_filter__has(struct pids_filtered *pids, pid_t pid)
 static int augment_sys_enter(void *ctx, struct syscall_enter_args *args)
 {
        bool augmented, do_output = false;
-       int zero = 0, index, value_size = sizeof(struct augmented_arg) - offsetof(struct augmented_arg, value);
+       int zero = 0, i, index, value_size = sizeof(struct augmented_arg) - offsetof(struct augmented_arg, value);
        u64 output = 0; /* has to be u64, otherwise it won't pass the verifier */
        s64 aug_size, size;
        unsigned int nr, *beauty_map;
@@ -466,12 +466,16 @@ static int augment_sys_enter(void *ctx, struct syscall_enter_args *args)
         * struct: size of struct             -> size of struct
         * buffer: -1 * (index of paired len) -> value of paired len (maximum: TRACE_AUG_MAX_BUF)
         */
-       for (int i = 0; i < 6; i++) {
+       bpf_for(i, 0, 6) {
+               barrier_var(i);
                arg = (void *)args->args[i];
                augmented = false;
                size = beauty_map[i];
                aug_size = size; /* size of the augmented data read from user space */
 
+               if (payload_offset + sizeof(struct augmented_arg) > (void *)payload + sizeof(struct beauty_payload_enter))
+                       break;
+
                if (size == 0 || arg == NULL)
                        continue;
 

Thanks a lot for the help!
Viktor

maintainer
#17 lore raw
In reply to Viktor Malik
Quoted 54 lines · Viktor Malik
Quoted 53 lines · Viktor Malik
Quoted 51 lines · Namhyung Kim
Quoted 32 lines · Viktor Malik
Quoted 31 lines · Namhyung Kim
Quoted 5 lines · Viktor Malik
Quoted 4 lines · Namhyung Kim

Hello,

Quoted 1 line · Alexei Starovoitov

On Tue Jun 23, 2026 at 4:25 AM PDT, Viktor Malik wrote:

[SNIP]

Quoted 23 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
(quoted diff) +5 -0
+	struct args_loop_ctx loop_ctx = {
+		.args = args,
+		.beauty_map = beauty_map,
+		.payload_offset = payload_offset,
+		.value_size = value_size,
Quoted 8 lines
Quoted 8 lines

Review

Quoted 4 lines
(quoted diff) +4 -0
+		.output = &output,
+		.do_output = &do_output
+	};
+	iters = bpf_loop(6, process_arg_cb, &loop_ctx, 0);

bpf_loop() is old and generally not recommended. Please use bpf_for() then the diff will be one line change and can scale to any number of args. Not just 6.

Thanks Alexei, I didn't know about this preference.

Quoted 3 lines

One thing we should take care is to support old kernels. The oldest LTS kernel in the kernel.org is 5.10 and bpf_loop() was introduced in 5.17 and bpf_for (bpf_iter_num) was 6.4.

The problematic loop was introduced in 6.12 by a68fd6a6cdd3 ("perf
trace: Collect augmented data using BPF") so we should be good using bpf_for. Or is perf from 7.2 supposed to work on 5.10 LTS kernels?

Quoted 5 lines
Quoted 1 line

Yep, we'd like to support old kernels.

How much strict are you on this requirement? IMHO, the very least we need to fix the verifier issue is bpf_loop, so that would still not work on 5.10 and 5.15 LTS kernels.

I don't think it's an absolute requirement, but I think we don't want to break any existing working setup (old kernel + old compiler).

Quoted 6 lines

We could probably keep the open-coded loop in case bpf_loop is not available but perf trace would still fail on kernels without bpf_loop for new perf built with Clang>=22. Also, the code would be a bit ugly and I'm not sure how well the feature check for helpers (bpf_loop) works on old kernels.

Any chance process_arg_cb() can be called directly in the regular for loop on old kernels?

Quoted 6 lines
Quoted 2 lines

That's my thinking, too. Should be pretty straightforward, I'm going to give it a try in v2.

Btw, I just noticed that util/bpf_skel/lock_contention.bpf.c already uses bpf_loop without any fallback so newer perf (at least perf lock) won't be usable on kernels without bpf_loop anyways.

IIUC the lock contention tracepoints were added to v5.19 so it won't work on old kernels anyway. :)

Thanks,
Namhyung

#11 lore raw
In reply to Andrii Nakryiko
Quoted 138 lines · Andrii Nakryiko
Quoted 54 lines · Viktor Malik
Quoted 52 lines · Andrii Nakryiko
Quoted 51 lines · Viktor Malik
Quoted 49 lines · Andrii Nakryiko
Quoted 48 lines · Viktor Malik
Quoted 46 lines · Viktor Malik
Quoted 45 lines · Namhyung Kim

Hello,

Quoted 22 lines · Alexei Starovoitov
Quoted 21 lines · Viktor Malik

The loop for processing syscall args in augment_raw_syscalls has a history of breaking with Clang updates, see e.g. commit 013eb043f37b ("perf trace: Fix BPF loading failure (-E2BIG)") from Clang 15 to 16.

Now, a similar thing happened between Clang 21 and 22. While the issue is mitigated on the main line by a recent verifier update, it remains broken on the 6.12 and 6.18 stable branches:

[linux-6.18.y]# sudo perf trace true
libbpf: prog 'sys_enter': BPF program load failed: -E2BIG
libbpf: prog 'sys_enter': -- BEGIN PROG LOAD LOG --
[...]
BPF program is too large. Processed 1000001 insn
processed 1000001 insns (limit 1000000) max_states_per_insn 40 total_states 37941 peak_states 232 mark_read 0 -- END PROG LOAD LOG --
libbpf: prog 'sys_enter': failed to load: -E2BIG
libbpf: failed to load object 'augmented_raw_syscalls_bpf'
libbpf: failed to load BPF skeleton 'augmented_raw_syscalls_bpf': -E2BIG
Error: failed to get syscall or beauty map fd
[...]

Quoted 8 lines
Quoted 8 lines

The reason is that the loop is quite complex and the BPF verifier often struggles to prove that it terminates.

Fix the issue by refactoring the loop body into a callback function and calling the bpf_loop helper. This should prevent future breakages of this kind since the callback function has no loops. It also allows to drop a few artificial checks to help the verifier, including the changes introduced by 013eb043f37b.

Thanks for working on this. I encountered this issue before and never found time to take a deeper look yet.

Quoted 8 lines
Quoted 8 lines
Fixes a68fd6a6cdd3 ("perf trace: Collect augmented data using BPF")
Fixes 013eb043f37b ("perf trace: Fix BPF loading failure (-E2BIG)")
---
 .../bpf_skel/augmented_raw_syscalls.bpf.c     | 157 +++++++++++-------
 1 file changed, 96 insertions(+), 61 deletions(-)

[...]

Quoted 72 lines
Quoted 67 lines
Quoted 67 lines
Quoted 49 lines
Quoted 41 lines
Quoted 20 lines
Quoted 10 lines
Quoted 10 lines
Quoted 9 lines
(quoted diff) +9 -0
+  struct args_loop_ctx loop_ctx = {
+          .args = args,
+          .beauty_map = beauty_map,
+          .payload_offset = payload_offset,
+          .value_size = value_size,
+          .output = &output,
+          .do_output = &do_output
+  };
+  iters = bpf_loop(6, process_arg_cb, &loop_ctx, 0);
Quoted 3 lines
Quoted 3 lines

bpf_loop() is old and generally not recommended. Please use bpf_for() then the diff will be one line change and can scale to any number of args. Not just 6.

Thanks Alexei, I didn't know about this preference.

Quoted 3 lines

One thing we should take care is to support old kernels. The oldest LTS kernel in the kernel.org is 5.10 and bpf_loop() was introduced in 5.17 and bpf_for (bpf_iter_num) was 6.4.

Quoted 5 lines

The problematic loop was introduced in 6.12 by a68fd6a6cdd3 ("perf
trace: Collect augmented data using BPF") so we should be good using bpf_for. Or is perf from 7.2 supposed to work on 5.10 LTS kernels?

I'll refactor with bpf_for and will send v2.

Or I won't. It turns out that just swapping the for loop for bpf_for leads to -E2BIG from the verifier again. Looking at the verifier log, it fails to find equivalence between states at the loop head:

    [...]
    78: (85) call bpf_iter_num_next#84922 [...]
fp-56=map_value(map=beauty_payload_,ks=4,vs=24688,imm=112)
    [...]
    78: (85) call bpf_iter_num_next#84922 [...]
fp-56=map_value(map=beauty_payload_,ks=4,vs=24688,imm=120)
    [...]

IMHO, the reason is that payload_offset, which points to the beauty_payload_enter_map entry, gets updated in every iteration.

Quoted 7 lines

This could be probably fixed on the perf side by reworking how augmented args are stored but at this point, bpf_loop sounds like an easier and more reliable approach.

Let me know if anyone has objections, otherwise I'll send v2 of the bpf_loop approach, with suggestions from Sashiko incorporated.

Quoted 7 lines

I'd still try to adapt bpf_for(), it's a much better code structure. You probably need to add a bounding checking/confirming if () condition validating that offset at which you access map_value is always correct. And/or you might need barrier_var() before using i, because bpf_for() macro does bounds checking (check the macro itself), but compiler often will reorder instructions leading to verifier complaints.

I gave it a try but wasn't successful so far. I think that the problem is that while it would be possible to add an upper bound condition for payload_offset, the verifier tracks the value of payload_offset too precisely (as map_value(..., imm=X) with a concrete offset) and never merges states with different offsets. And since there are multiple branches inside the loop, each incrementing payload_offset by a different value, the verifier seems to fork its state on each branch, effectively leading to the amount of states growing exponentially and hitting the jump limit.

Quoted 4 lines
Quoted 1 line

To me, bpf_loop sounds like a more reliable choice in this situation.

correctly verified bpf_loop would basically have to follow the same logic, so if it works with bpf_loop, it should work with bpf_for.

Quoted 7 lines

Are you sure about that? My perception is that the bpf_loop callback is only verified once in a single pass. On the contrary, bpf_for is a normal loop, for which the verifier needs to prove that after some iteration, we get to the state seen in a previous iteration (to prune the state). Which never happens here because the offset to beauty_payload_enter_map (the payload_offset var) is tracked precisely and causes state forks on every condition inside the loop.

Quoted 3 lines

Hey Viktor,

Sorry for taking so long to get back.

Hey Andrii,

np, thanks for taking a look!

Quoted 6 lines

Answering your question about bpf_loop() vs bpf_for() they are conceptually the same from verifier POV, so they are verified similarly. Earlier (buggier) versions of verifier did have a loophole where we verifier bpf_loop() in more laxed single-shot way, but that's not correct. We have since fixed that and it (bpf_loop) now has to "prove" convergence just like bpf_for().

Right, this is the piece of the information that I was missing. It now makes much more sense.

Quoted 13 lines

Anyways, the biggest issue with "normal" unrolled BPF loop is that people tend to write it such that there is some carry-over state between each iteration (like output variable which tracks advancing but bounded offset) which, with fixed number of iterations allows verifier to prove everything is bounded.

This model is really-really bad for bpf_for() because it doesn't allow convergence. The trick is to structure each iteration as independent piece of calculation where the state outside of bpf_for() loop stays as unspecific/imprecise as possible, which at the beginning of the loop you revalidate invariants, if necessary (e.g., reestablish map_value offset boundaries).

Quoted 8 lines

Anyways, it needed a bit of persuasion, but here's the verification result and gmail-butchered diff below. The trick is in making output imprecise (force verifier to forget its tracked range), so it doesn't differ between iterations from verifier POV. That's what the global ZERO allows to do. (We've discussed w/ Alexei and Eduard adding special instruction to force scalar register into imprecise, it would be a cleaner solution here, alas we never got anywhere with this, unfortunately).

Many thanks for the patch! Looking at it, I got pretty close during my attempts, I only missed the ZERO trick, which is obviously crucial. I was worried I'll have to rewrite the logic much more to get rid of the concrete carry-over state but this is really neat.

I'm wondering if we could teach the verifier to figure out that it's tracking a value too precisely in an iterator-based loop and convert it to a range (sort of a "widening" operation). But I guess that this part is going to be changed quite a bit with the upcoming verifier change that Alexei is working on.

I'll take your change and send v2 of the patch (with a fall back to standard for loop to keep backwards compatibility).

Thanks again!
Viktor

Quoted 141 lines

Processing 'augmented_raw_syscalls.bpf.o'...
PROCESSING ./util/bpf_skel/.tmp/augmented_raw_syscalls.bpf.o/sys_enter, DURATION US: 1129, VERDICT: success, VERIFIER LOG:
verification time 1129 usec
stack depth 64
processed 547 insns (limit 1000000) max_states_per_insn 4 total_states 38 peak_states 67 mark_read 0

File Program Verdict Duration (us) Insns
States Program size Jited size
---------------------------- --------- ------- ------------- -----
------ ------------ ----------

augmented_raw_syscalls.bpf.o  sys_enter  success           1129    547
     38           172         917
----------------------------  ---------  -------  -------------  -----

------ ------------ ----------

The diff:

tools/perf/util/bpf_skel/augmented_raw_syscalls.bpf.c +3 -2
index 2a6e61864ee0..8436368ba203 100644
@@ -429,15 +429,17 @@ static bool pid_filter__has(struct pids_filtered
 *pids, pid_t pid)
        return bpf_map_lookup_elem(pids, &pid) != NULL;
 }
 
+u64 ZERO = 0;
+
 static int augment_sys_enter(void *ctx, struct syscall_enter_args *args)
 {
        bool augmented, do_output = false;
-       int zero = 0, index, value_size = sizeof(struct augmented_arg)
- offsetof(struct augmented_arg, value);
+       int i, zero = 0, index, value_size = sizeof(struct
 augmented_arg) - offsetof(struct augmented_arg, value);
        u64 output = 0; /* has to be u64, otherwise it won't pass the

verifier */

        s64 aug_size, size;
        unsigned int nr, *beauty_map;
        struct beauty_payload_enter *payload;
tools/perf/util/bpf_skel/augmented_raw_syscalls.bpf.c +1 -2
-       void *arg, *payload_offset;
+       void *arg;
 
        /* fall back to do predefined tail call */
        if (args == NULL)
@@ -449,7 +451,6 @@ static int augment_sys_enter(void *ctx, struct
 syscall_enter_args *args)
 
        /* set up payload for output */
        payload        = bpf_map_lookup_elem(&beauty_payload_enter_map, &zero);
-       payload_offset = (void *)&payload->aug_args;
 
        if (beauty_map == NULL || payload == NULL)
                return 1;
tools/perf/util/bpf_skel/augmented_raw_syscalls.bpf.c +1 -1
@@ -466,7 +467,7 @@ static int augment_sys_enter(void *ctx, struct
 syscall_enter_args *args)
         * struct: size of struct             -> size of struct
         * buffer: -1 * (index of paired len) -> value of paired len
 (maximum: TRACE_AUG_MAX_BUF)
         */
-       for (int i = 0; i < 6; i++) {
+       bpf_for(i, 0, 6) {
                arg = (void *)args->args[i];
                augmented = false;
                size = beauty_map[i];
tools/perf/util/bpf_skel/augmented_raw_syscalls.bpf.c +5 -0
@@ -475,6 +476,11 @@ static int augment_sys_enter(void *ctx, struct
 syscall_enter_args *args)
                if (size == 0 || arg == NULL)
                        continue;
 
+               if (output > sizeof(payload->aug_args) -
 sizeof(payload->aug_args[0]))
+                       break; /* can't/shouldn't happen */
+               barrier_var(output);
+               void *payload_offset = (void *)&payload->aug_args + output;
+
                if (size == 1) { /* string */

aug_size = bpf_probe_read_user_str(((struct

augmented_arg *)payload_offset)->value, value_size, arg);
                        /* minimum of 0 to pass the verifier */
tools/perf/util/bpf_skel/augmented_raw_syscalls.bpf.c +1 -1
@@ -510,7 +516,7 @@ static int augment_sys_enter(void *ctx, struct
 syscall_enter_args *args)
 
                        ((struct augmented_arg *)payload_offset)->size
 = aug_size;
                        output += written;
-                       payload_offset += written;
+                       output += ZERO; /* forget range */
                        do_output = true;
                }
        }
Quoted 44 lines
Quoted 4 lines

Is
it possible to share your bpf_for-based code in some branch to try locally? I'm sure it can be done one way or another.

The change is super-simple, I can as well share it here. It's just the matter of using bpf_for with two additional suggested mechanisms, barrier_var and a bounds check for payload_offset:

tools/perf/util/bpf_skel/augmented_raw_syscalls.bpf.c +6 -2
index 2a6e61864ee0..341d77a78949 100644
@@ -432,7 +432,7 @@ static bool pid_filter__has(struct pids_filtered *pids, pid_t pid)
 static int augment_sys_enter(void *ctx, struct syscall_enter_args *args)
 {
        bool augmented, do_output = false;
-       int zero = 0, index, value_size = sizeof(struct augmented_arg) - offsetof(struct augmented_arg, value);
+       int zero = 0, i, index, value_size = sizeof(struct augmented_arg) - offsetof(struct augmented_arg, value);
        u64 output = 0; /* has to be u64, otherwise it won't pass the verifier */
        s64 aug_size, size;
        unsigned int nr, *beauty_map;
@@ -466,12 +466,16 @@ static int augment_sys_enter(void *ctx, struct syscall_enter_args *args)
         * struct: size of struct             -> size of struct
         * buffer: -1 * (index of paired len) -> value of paired len (maximum: TRACE_AUG_MAX_BUF)
         */
-       for (int i = 0; i < 6; i++) {
+       bpf_for(i, 0, 6) {
+               barrier_var(i);
                arg = (void *)args->args[i];
                augmented = false;
                size = beauty_map[i];
                aug_size = size; /* size of the augmented data read from user space */
 
+               if (payload_offset + sizeof(struct augmented_arg) > (void *)payload + sizeof(struct beauty_payload_enter))
+                       break;
+
                if (size == 0 || arg == NULL)
                        continue;
 

Thanks a lot for the help!
Viktor