Skip to contentSkip to conversation

perf trace: Fix wrong size to bpf_map__update_elem call

PATCH Applied Thomas Richter 4 participants 7 messages Mar 24, 2025 – Mar 25, 2025

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 perf trace: Fix wrong size to bpf_map__update_elem call

In linux-next
commit c760174401f6 ("perf cpumap: Reduce cpu size from int to int16_t") causes the perf tests 100 126 to fail on s390:

Output before:

 # ./perf test 100
 100: perf trace BTF general tests         : FAILED!
 #

The root cause is the change from int to int16_t for the cpu maps. The size of the CPU key value pair changes from four bytes to two bytes. However a two byte key size is not supported for bpf_map__update_elem().
Note: validate_map_op() in libbpf.c emits warning
libbpf: map '__augmented_syscalls__': \
unexpected key size 2 provided, expected 4
when key size is set to int16_t.

Therefore change to variable size back to 4 bytes for invocation of bpf_map__update_elem().

Output after:

 # ./perf test 100
 100: perf trace BTF general tests         : Ok
 #
Fixes c760174401f6 ("perf cpumap: Reduce cpu size from int to int16_t")
Cc Ian Rogers <[email protected]>
Cc James Clark <[email protected]>
---
 tools/perf/builtin-trace.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
tools/perf/builtin-trace.c +4 -2
index 092c5f6404ba..464c97a11852 100644
@@ -4375,10 +4375,12 @@ static int trace__run(struct trace *trace, int argc, const char **argv)
 		 * CPU the bpf-output event's file descriptor.
 		 */
 		perf_cpu_map__for_each_cpu(cpu, i, trace->syscalls.events.bpf_output->core.cpus) {
+			int mycpu = cpu.cpu;
+
 			bpf_map__update_elem(trace->skel->maps.__augmented_syscalls__,
-					&cpu.cpu, sizeof(int),
+					&mycpu, sizeof(int),
 					xyarray__entry(trace->syscalls.events.bpf_output->core.fd,
-						       cpu.cpu, 0),
+						       mycpu, 0),
 					sizeof(__u32), BPF_ANY);
 		}
 	}
Signature · 1 line
2.48.1
reviewer Reviewed-by on v1 1/1
#1 lore raw
Quoted 6 lines · Thomas Richter

In linux-next
commit c760174401f6 ("perf cpumap: Reduce cpu size from int to int16_t") causes the perf tests 100 126 to fail on s390:

Output before:

Quoted 8 lines
 # ./perf test 100
 100: perf trace BTF general tests         : FAILED!
 #

The root cause is the change from int to int16_t for the cpu maps. The size of the CPU key value pair changes from four bytes to two bytes. However a two byte key size is not supported for bpf_map__update_elem().

Nice catch!

Quoted 4 lines

Note: validate_map_op() in libbpf.c emits warning
libbpf: map '__augmented_syscalls__': \
unexpected key size 2 provided, expected 4
when key size is set to int16_t.

Wow, weird.

Quoted 21 lines

Therefore change to variable size back to 4 bytes for invocation of bpf_map__update_elem().

Output after:

 # ./perf test 100
 100: perf trace BTF general tests         : Ok
 #
Fixes c760174401f6 ("perf cpumap: Reduce cpu size from int to int16_t")
Cc Ian Rogers <[email protected]>
Cc James Clark <[email protected]>
---
 tools/perf/builtin-trace.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
tools/perf/builtin-trace.c +0 -0
index 092c5f6404ba..464c97a11852 100644

tools/perf/builtin-trace.c static int trace__run(struct trace *trace, int argc, const char **argv) · line 4381

Quoted 8 lines
tools/perf/builtin-trace.c +3 -1
@@ -4375,10 +4375,12 @@ static int trace__run(struct trace *trace, int argc, const char **argv)
                 * CPU the bpf-output event's file descriptor.
                 */
                perf_cpu_map__for_each_cpu(cpu, i, trace->syscalls.events.bpf_output->core.cpus) {
+                       int mycpu = cpu.cpu;
+
                        bpf_map__update_elem(trace->skel->maps.__augmented_syscalls__,
-                                       &cpu.cpu, sizeof(int),
+                                       &mycpu, sizeof(int),

nit: It is usually preferred to do "sizeof(mycpu)" to avoid the problems like the one this fix is fixing. And I'm blamed for the bad code in:
5e6da6be3082 perf trace: Migrate BPF augmentation to use a skeleton

Thanks for the fixes!
Ian

Quoted 9 lines
tools/perf/builtin-trace.c +1 -2
                                        xyarray__entry(trace->syscalls.events.bpf_output->core.fd,
-                                                      cpu.cpu, 0),
+                                                      mycpu, 0),
                                        sizeof(__u32), BPF_ANY);
                }
        }
--

2.48.1

reviewer
#2 lore raw
Quoted 17 lines · Ian Rogers
Quoted 6 lines · Thomas Richter

In linux-next
commit c760174401f6 ("perf cpumap: Reduce cpu size from int to int16_t") causes the perf tests 100 126 to fail on s390:

Output before:

Quoted 8 lines
 # ./perf test 100
 100: perf trace BTF general tests         : FAILED!
 #

The root cause is the change from int to int16_t for the cpu maps. The size of the CPU key value pair changes from four bytes to two bytes. However a two byte key size is not supported for bpf_map__update_elem().

Quoted 8 lines

Nice catch!

Quoted 4 lines

Note: validate_map_op() in libbpf.c emits warning
libbpf: map '__augmented_syscalls__': \
unexpected key size 2 provided, expected 4
when key size is set to int16_t.

Wow, weird.

Ah, I guess it is a mismatch with the declaration in tools/perf/util/bpf_skel/augmented_raw_syscalls.bpf.c:
https://web.git.kernel.org/pub/scm/linux/kernel/git/perf/perf-tools-next.git/tree/tools/perf/util/bpf_skel/augmented_raw_syscalls.bpf.c?h=perf-tools-next#n31

```
/* bpf-output associated map */
struct __augmented_syscalls__ {
       __uint(type, BPF_MAP_TYPE_PERF_EVENT_ARRAY);
       __type(key, int);
       __type(value, __u32);
       __uint(max_entries, MAX_CPUS);
} __augmented_syscalls__ SEC(".maps");
```
but this looks wrong. The values are file descriptors, so should be
ints. I think it should be:
```
struct __augmented_syscalls__ {
       __uint(type, BPF_MAP_TYPE_PERF_EVENT_ARRAY);
       __type(key, int16_t);
       __type(value, int);
       __uint(max_entries, MAX_CPUS);
} __augmented_syscalls__ SEC(".maps");
```

I'm not sure if max_entries can be a uint16_t too. I suspect this may well interfere with BPF_MAP_TYPE_PERF_EVENT_ARRAY and its use by bpf_perf_event_output. Probably best to keep things in the BPF code as they are and do your builtin-trace.c fix. Perhaps someone else can clean this up.

Thanks,
Ian

Quoted 49 lines
Quoted 21 lines

Therefore change to variable size back to 4 bytes for invocation of bpf_map__update_elem().

Output after:

 # ./perf test 100
 100: perf trace BTF general tests         : Ok
 #
Fixes c760174401f6 ("perf cpumap: Reduce cpu size from int to int16_t")
Cc Ian Rogers <[email protected]>
Cc James Clark <[email protected]>
---
 tools/perf/builtin-trace.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
tools/perf/builtin-trace.c +0 -0
index 092c5f6404ba..464c97a11852 100644

tools/perf/builtin-trace.c static int trace__run(struct trace *trace, int argc, const char **argv) · line 4381

Quoted 8 lines
tools/perf/builtin-trace.c +3 -1
@@ -4375,10 +4375,12 @@ static int trace__run(struct trace *trace, int argc, const char **argv)
                 * CPU the bpf-output event's file descriptor.
                 */
                perf_cpu_map__for_each_cpu(cpu, i, trace->syscalls.events.bpf_output->core.cpus) {
+                       int mycpu = cpu.cpu;
+
                        bpf_map__update_elem(trace->skel->maps.__augmented_syscalls__,
-                                       &cpu.cpu, sizeof(int),
+                                       &mycpu, sizeof(int),

nit: It is usually preferred to do "sizeof(mycpu)" to avoid the problems like the one this fix is fixing. And I'm blamed for the bad code in:
5e6da6be3082 perf trace: Migrate BPF augmentation to use a skeleton

Thanks for the fixes!
Ian

Quoted 9 lines
tools/perf/builtin-trace.c +1 -2
                                        xyarray__entry(trace->syscalls.events.bpf_output->core.fd,
-                                                      cpu.cpu, 0),
+                                                      mycpu, 0),
                                        sizeof(__u32), BPF_ANY);
                }
        }
--

2.48.1

maintainer
#3 lore raw

Hello,

Quoted 49 lines · Ian Rogers
Quoted 17 lines · Ian Rogers
Quoted 6 lines · Thomas Richter

In linux-next
commit c760174401f6 ("perf cpumap: Reduce cpu size from int to int16_t") causes the perf tests 100 126 to fail on s390:

Output before:

Quoted 8 lines
 # ./perf test 100
 100: perf trace BTF general tests         : FAILED!
 #

The root cause is the change from int to int16_t for the cpu maps. The size of the CPU key value pair changes from four bytes to two bytes. However a two byte key size is not supported for bpf_map__update_elem().

Quoted 8 lines

Nice catch!

Quoted 4 lines

Note: validate_map_op() in libbpf.c emits warning
libbpf: map '__augmented_syscalls__': \
unexpected key size 2 provided, expected 4
when key size is set to int16_t.

Wow, weird.

Ah, I guess it is a mismatch with the declaration in tools/perf/util/bpf_skel/augmented_raw_syscalls.bpf.c:
https://web.git.kernel.org/pub/scm/linux/kernel/git/perf/perf-tools-next.git/tree/tools/perf/util/bpf_skel/augmented_raw_syscalls.bpf.c?h=perf-tools-next#n31

```
/* bpf-output associated map */
struct __augmented_syscalls__ {
       __uint(type, BPF_MAP_TYPE_PERF_EVENT_ARRAY);
       __type(key, int);
       __type(value, __u32);
       __uint(max_entries, MAX_CPUS);
} __augmented_syscalls__ SEC(".maps");
```
but this looks wrong. The values are file descriptors, so should be
ints. I think it should be:
```
struct __augmented_syscalls__ {
       __uint(type, BPF_MAP_TYPE_PERF_EVENT_ARRAY);
       __type(key, int16_t);
       __type(value, int);
       __uint(max_entries, MAX_CPUS);
} __augmented_syscalls__ SEC(".maps");
```
Quoted 5 lines

I'm not sure if max_entries can be a uint16_t too. I suspect this may well interfere with BPF_MAP_TYPE_PERF_EVENT_ARRAY and its use by bpf_perf_event_output. Probably best to keep things in the BPF code as they are and do your builtin-trace.c fix. Perhaps someone else can clean this up.

I don't think max_entries can be uint16_t. Also I'm skeptical to change the key type to int16_t as you said. I'll pick Thomas's patch up.

Thanks,
Namhyung

Quoted 50 lines
Quoted 50 lines
Quoted 22 lines

Therefore change to variable size back to 4 bytes for invocation of bpf_map__update_elem().

Output after:

 # ./perf test 100
 100: perf trace BTF general tests         : Ok
 #
Fixes c760174401f6 ("perf cpumap: Reduce cpu size from int to int16_t")
Cc Ian Rogers <[email protected]>
Cc James Clark <[email protected]>
---
 tools/perf/builtin-trace.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
tools/perf/builtin-trace.c +0 -0
index 092c5f6404ba..464c97a11852 100644

tools/perf/builtin-trace.c static int trace__run(struct trace *trace, int argc, const char **argv) · line 4381

Quoted 8 lines
tools/perf/builtin-trace.c +3 -1
@@ -4375,10 +4375,12 @@ static int trace__run(struct trace *trace, int argc, const char **argv)
                 * CPU the bpf-output event's file descriptor.
                 */
                perf_cpu_map__for_each_cpu(cpu, i, trace->syscalls.events.bpf_output->core.cpus) {
+                       int mycpu = cpu.cpu;
+
                        bpf_map__update_elem(trace->skel->maps.__augmented_syscalls__,
-                                       &cpu.cpu, sizeof(int),
+                                       &mycpu, sizeof(int),

nit: It is usually preferred to do "sizeof(mycpu)" to avoid the problems like the one this fix is fixing. And I'm blamed for the bad code in:
5e6da6be3082 perf trace: Migrate BPF augmentation to use a skeleton

Thanks for the fixes!
Ian

Quoted 9 lines
tools/perf/builtin-trace.c +1 -2
                                        xyarray__entry(trace->syscalls.events.bpf_output->core.fd,
-                                                      cpu.cpu, 0),
+                                                      mycpu, 0),
                                        sizeof(__u32), BPF_ANY);
                }
        }
--

2.48.1

#4 lore raw
In reply to Ian Rogers

Hello Ian,

Quoted 41 lines · Ian Rogers
Quoted 17 lines · Ian Rogers
Quoted 6 lines · Thomas Richter

In linux-next
commit c760174401f6 ("perf cpumap: Reduce cpu size from int to int16_t") causes the perf tests 100 126 to fail on s390:

Output before:

Quoted 8 lines
 # ./perf test 100
 100: perf trace BTF general tests         : FAILED!
 #

The root cause is the change from int to int16_t for the cpu maps. The size of the CPU key value pair changes from four bytes to two bytes. However a two byte key size is not supported for bpf_map__update_elem().

Quoted 8 lines

Nice catch!

Quoted 4 lines

Note: validate_map_op() in libbpf.c emits warning
libbpf: map '__augmented_syscalls__': \
unexpected key size 2 provided, expected 4
when key size is set to int16_t.

Wow, weird.

Ah, I guess it is a mismatch with the declaration in tools/perf/util/bpf_skel/augmented_raw_syscalls.bpf.c:
https://web.git.kernel.org/pub/scm/linux/kernel/git/perf/perf-tools-next.git/tree/tools/perf/util/bpf_skel/augmented_raw_syscalls.bpf.c?h=perf-tools-next#n31

```
/* bpf-output associated map */
struct __augmented_syscalls__ {
       __uint(type, BPF_MAP_TYPE_PERF_EVENT_ARRAY);
       __type(key, int);
       __type(value, __u32);
       __uint(max_entries, MAX_CPUS);
} __augmented_syscalls__ SEC(".maps");
```

but this looks wrong. The values are file descriptors, so should be ints. I think it should be:

Quoted 8 lines
```
struct __augmented_syscalls__ {
       __uint(type, BPF_MAP_TYPE_PERF_EVENT_ARRAY);
       __type(key, int16_t);
       __type(value, int);
       __uint(max_entries, MAX_CPUS);
} __augmented_syscalls__ SEC(".maps");
```

I agree, regardless of the key, value of fd should be int.

Quoted 5 lines

I'm not sure if max_entries can be a uint16_t too. I suspect this may well interfere with BPF_MAP_TYPE_PERF_EVENT_ARRAY and its use by bpf_perf_event_output. Probably best to keep things in the BPF code as they are and do your builtin-trace.c fix. Perhaps someone else can clean this up.

I'll do that since I'm responsible, and I'll based it on Thomas' changes.

Thanks
Howard

Quoted 54 lines

Thanks,
Ian

Quoted 49 lines
Quoted 21 lines

Therefore change to variable size back to 4 bytes for invocation of bpf_map__update_elem().

Output after:

 # ./perf test 100
 100: perf trace BTF general tests         : Ok
 #
Fixes c760174401f6 ("perf cpumap: Reduce cpu size from int to int16_t")
Cc Ian Rogers <[email protected]>
Cc James Clark <[email protected]>
---
 tools/perf/builtin-trace.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
tools/perf/builtin-trace.c +0 -0
index 092c5f6404ba..464c97a11852 100644

tools/perf/builtin-trace.c static int trace__run(struct trace *trace, int argc, const char **argv) · line 4381

Quoted 8 lines
tools/perf/builtin-trace.c +3 -1
@@ -4375,10 +4375,12 @@ static int trace__run(struct trace *trace, int argc, const char **argv)
                 * CPU the bpf-output event's file descriptor.
                 */
                perf_cpu_map__for_each_cpu(cpu, i, trace->syscalls.events.bpf_output->core.cpus) {
+                       int mycpu = cpu.cpu;
+
                        bpf_map__update_elem(trace->skel->maps.__augmented_syscalls__,
-                                       &cpu.cpu, sizeof(int),
+                                       &mycpu, sizeof(int),

nit: It is usually preferred to do "sizeof(mycpu)" to avoid the problems like the one this fix is fixing. And I'm blamed for the bad code in:
5e6da6be3082 perf trace: Migrate BPF augmentation to use a skeleton

Thanks for the fixes!
Ian

Quoted 9 lines
tools/perf/builtin-trace.c +1 -2
                                        xyarray__entry(trace->syscalls.events.bpf_output->core.fd,
-                                                      cpu.cpu, 0),
+                                                      mycpu, 0),
                                        sizeof(__u32), BPF_ANY);
                }
        }
--

2.48.1

Acked-by on v1 1/1
#5 lore raw
In reply to Thomas Richter
Quoted 7 lines · Thomas Richter

In linux-next
commit c760174401f6 ("perf cpumap: Reduce cpu size from int to int16_t") causes the perf tests 100 126 to fail on s390:

Output before:

 # ./perf test 100
 100: perf trace BTF general tests         : FAILED!

Arnaldo encountered similar failure, but I couldn't reproduce it, so the fix was stalled.

Quoted 11 lines

 #

The root cause is the change from int to int16_t for the cpu maps. The size of the CPU key value pair changes from four bytes to two bytes. However a two byte key size is not supported for bpf_map__update_elem().
Note: validate_map_op() in libbpf.c emits warning
 libbpf: map '__augmented_syscalls__': \
       unexpected key size 2 provided, expected 4 when key size is set to int16_t.

Quoted 2 lines

Therefore change to variable size back to 4 bytes for invocation of bpf_map__update_elem().

Makes sense, thank you so much for digging into this and fixing it.

Quoted 34 lines

Output after:

 # ./perf test 100
 100: perf trace BTF general tests         : Ok
 #
Fixes c760174401f6 ("perf cpumap: Reduce cpu size from int to int16_t")
Cc Ian Rogers <[email protected]>
Cc James Clark <[email protected]>

---
 tools/perf/builtin-trace.c | 6 ++++--

 1 file changed, 4 insertions(+), 2 deletions(-)
tools/perf/builtin-trace.c +4 -3
index 092c5f6404ba..464c97a11852 100644
@@ -4375,10 +4375,12 @@ static int trace__run(struct trace *trace, int argc, const char **argv)
                * CPU the bpf-output event's file descriptor.
                */
               perf_cpu_map__for_each_cpu(cpu, i, trace->syscalls.events.bpf_output->core.cpus) {
+                     int mycpu = cpu.cpu;
+
                       bpf_map__update_elem(trace->skel->maps.__augmented_syscalls__,
-                                     &cpu.cpu, sizeof(int),
+                                     &mycpu, sizeof(int),
                                       xyarray__entry(trace->syscalls.events.bpf_output->core.fd,
-                                                    cpu.cpu, 0),
+                                                    mycpu, 0),
                                       sizeof(__u32), BPF_ANY);
               }
       }
--
Quoted 3 lines

2.48.1

Thanks,
Howard

maintainer Applied
#6 lore raw
In reply to Thomas Richter
Quoted 10 lines · Thomas Richter

In linux-next
commit c760174401f6 ("perf cpumap: Reduce cpu size from int to int16_t") causes the perf tests 100 126 to fail on s390:

Output before:

 # ./perf test 100
 100: perf trace BTF general tests         : FAILED!
 #

[...]

Applied to perf-tools-next, thanks!

Best regards,
Namhyung