Skip to contentSkip to conversation

perf: Fix btf_vlen() signedness errors

PATCH In review Alan Maguire 4 participants 6 messages Jun 22

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

PATCH 1/1 perf: Fix btf_vlen() signedness errors

Commit f7a6b9eaff3e6 ("bpf: Extend BTF UAPI vlen, kinds to use unused bits")

extended the return value for btf_vlen() to __u32, triggering errors for cases where an int was used to store return value. Fix these cases for perf as was done for other libbpf consumers.

---
 tools/perf/builtin-trace.c | 6 +++---
 tools/perf/util/btf.c      | 2 +-
 2 files changed, 4 insertions(+), 4 deletions(-)
tools/perf/builtin-trace.c +3 -3
index 48615ddccd93..9903d79c4b77 100644
@@ -964,7 +964,7 @@ static bool syscall_arg__strtoul_btf_enum(char *bf, size_t size, struct syscall_
 	struct btf *btf = arg->trace->btf;
 	struct btf_enum *be = btf_enum(bt);
 
-	for (int i = 0; i < btf_vlen(bt); ++i, ++be) {
+	for (unsigned int i = 0; i < btf_vlen(bt); ++i, ++be) {
 		const char *name = btf__name_by_offset(btf, be->name_off);
 		int max_len = max(size, strlen(name));
 
@@ -1009,9 +1009,9 @@ static bool syscall_arg__strtoul_btf_type(char *bf, size_t size, struct syscall_
 static size_t btf_enum_scnprintf(const struct btf_type *type, struct btf *btf, char *bf, size_t size, int val)
 {
 	struct btf_enum *be = btf_enum(type);
-	const int nr_entries = btf_vlen(type);
+	const unsigned int nr_entries = btf_vlen(type);
 
-	for (int i = 0; i < nr_entries; ++i, ++be) {
+	for (unsigned int i = 0; i < nr_entries; ++i, ++be) {
 		if (be->val == val) {
 			return scnprintf(bf, size, "%s",
 					 btf__name_by_offset(btf, be->name_off));
tools/perf/util/btf.c +1 -1
index bb163fe87767..1716b49d3568 100644
@@ -14,7 +14,7 @@ const struct btf_member *__btf_type__find_member_by_name(struct btf *btf,
 {
 	const struct btf_type *t = btf__type_by_id(btf, type_id);
 	const struct btf_member *m;
-	int i;
+	unsigned int i;
 
 	for (i = 0, m = btf_members(t); i < btf_vlen(t); i++, m++) {
 		const char *current_member_name = btf__name_by_offset(btf, m->name_off);
Signature · 1 line
2.47.3
#1 lore raw
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:

Pre-existing issues:
- [Medium] Silent truncation of BTF_INFO_VLEN in libbpf's btf_vlen() --

commit c28608c8a13cb7af8bf42f5aaadf102bb452fc40
Author: Alan Maguire <[email protected]>

perf: Fix btf_vlen() signedness errors

This commit updates perf to use unsigned int loop counters when iterating up to btf_vlen(). This avoids signedness errors after commit f7a6b9eaff3e6 extended the BTF UAPI vlen to use previously unused bits.

tools/perf/builtin-trace.c static bool syscall_arg__strtoul_btf_enum(char *bf, size_t size, struct syscall_ · line 967

Quoted 10 lines
tools/perf/builtin-trace.c +1 -1
index 48615ddccd93a..9903d79c4b77a 100644
@@ -964,7 +964,7 @@ static bool syscall_arg__strtoul_btf_enum(char *bf, size_t size, struct syscall_
 	struct btf *btf = arg->trace->btf;
 	struct btf_enum *be = btf_enum(bt);
 
-	for (int i = 0; i < btf_vlen(bt); ++i, ++be) {
+	for (unsigned int i = 0; i < btf_vlen(bt); ++i, ++be) {

[Severity: Medium]
This is a pre-existing issue, but does the btf_vlen function still silently truncate the newly extended length?

Looking at tools/lib/bpf/btf.h, it appears to be defined as:

static inline __u16 btf_vlen(const struct btf_type *t)

{
        return BTF_INFO_VLEN(t->info);
}

Since commit f7a6b9eaff3e6 extended the internal BTF_INFO_VLEN from 16 to 24 bits to support more than 65535 elements, won't returning __u16 here truncate the upper 8 bits?

While updating the loop variables to unsigned int fixes the signedness warnings in consumers, does the underlying library still need an update to return __u32 and prevent truncation?

Quoted 2 lines
tools/perf/builtin-trace.c +0 -0
 		const char *name = btf__name_by_offset(btf, be->name_off);
 		int max_len = max(size, strlen(name));
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
#2 lore raw
In reply to Alan Maguire
Quoted 5 lines · Alan Maguire

Commit f7a6b9eaff3e6 ("bpf: Extend BTF UAPI vlen, kinds to use unused bits")

extended the return value for btf_vlen() to __u32, triggering errors for cases where an int was used to store return value. Fix these cases for perf as was done for other libbpf consumers.

I already sent:

https://lore.kernel.org/r/[email protected]

which I have been using as a fixup since I originally reported the issue since Linus' tree is broken, Arnaldo said he'd applied it to the perf-tools-next branch but it has not yet appeared in Linus' tree (and it looks like he forgot to push it so his copy isn't getting picked up in the merge either, the last update was on the 15th).

#3 lore raw
Quoted 9 lines · Mark Brown
Quoted 5 lines · Alan Maguire

Commit f7a6b9eaff3e6 ("bpf: Extend BTF UAPI vlen, kinds to use unused bits")

extended the return value for btf_vlen() to __u32, triggering errors for cases where an int was used to store return value. Fix these cases for perf as was done for other libbpf consumers.

I already sent:

Quoted 7 lines

https://lore.kernel.org/r/[email protected]

which I have been using as a fixup since I originally reported the issue since Linus' tree is broken, Arnaldo said he'd applied it to the perf-tools-next branch but it has not yet appeared in Linus' tree (and it looks like he forgot to push it so his copy isn't getting picked up in the merge either, the last update was on the 15th).

Its here:

<https://git.kernel.org/pub/scm/linux/kernel/git/perf/perf-tools-next.git/commit/?h=perf-tools-next&id=776bd9ac834556ab6ff6b7ef74b71ac53d37855e>

I think Alan's latest patch has one more hunk, I'll get that one in and then send the pull request to Linus today,

- Arnaldo

#4 lore raw
Quoted 11 lines · Arnaldo Melo
Quoted 5 lines · Mark Brown

which I have been using as a fixup since I originally reported the issue since Linus' tree is broken, Arnaldo said he'd applied it to the perf-tools-next branch but it has not yet appeared in Linus' tree (and it looks like he forgot to push it so his copy isn't getting picked up in the merge either, the last update was on the 15th).

Its here:

<https://git.kernel.org/pub/scm/linux/kernel/git/perf/perf-tools-next.git/commit/?h=perf-tools-next&id=776bd9ac834556ab6ff6b7ef74b71ac53d37855e>

Ah, so it is - I missed it because Ian's series was applied on top of it and doesn't look at all like fixes so I stopped reading the log (should it be in -next given the merge window?).

#5 lore raw
Quoted 9 lines · Mark Brown
Quoted 7 lines · Arnaldo Melo
Quoted 5 lines · Mark Brown

which I have been using as a fixup since I originally reported the issue since Linus' tree is broken, Arnaldo said he'd applied it to the perf-tools-next branch but it has not yet appeared in Linus' tree (and it looks like he forgot to push it so his copy isn't getting picked up in the merge either, the last update was on the 15th).

Quoted 7 lines
Quoted 3 lines

Its here:

<https://git.kernel.org/pub/scm/linux/kernel/git/perf/perf-tools-next.git/commit/?h=perf-tools-next&id=776bd9ac834556ab6ff6b7ef74b71ac53d37855e>

Ah, so it is - I missed it because Ian's series was applied on top of it and doesn't look at all like fixes so I stopped reading the log (should it be in -next given the merge window?).

Ian's series? Yeah, I was unsure about having it in this merge window, it went thru many versions, but your question is a fair one, I'll move it to the next merge window and send only up to your fix (+ Alan's missing hunk),

- Arnaldo