Skip to contentSkip to conversation

libbpf: Fix build warning on ref_ctr_off

PATCH Superseded Khem Raj 2 participants 3 messages Dec 5, 2022 – Dec 19, 2022

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Khem Raj

author
#0 lore raw

PATCH 1/1 libbpf: Fix build warning on ref_ctr_off

Clang warns on 32-bit ARM on this comparision

libbpf.c:10497:18: error: result of comparison of constant 4294967296 with expression of type 'size_t' (aka 'unsigned int') is always false [-Werror,-Wtautological-constant-out-of-range-compare]

        if (ref_ctr_off >= (1ULL << PERF_UPROBE_REF_CTR_OFFSET_BITS))
            ~~~~~~~~~~~ ^  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Check for platform long int to be larger than 32-bits before enabling this check, it false on 32bit anyways.

Cc Alexei Starovoitov <[email protected]>
Cc Daniel Borkmann <[email protected]>
Cc Song Liu <[email protected]>
Cc Yonghong Song <[email protected]>
Cc Jiri Olsa <[email protected]>
Cc Paul Walmsley <[email protected]>
Cc Palmer Dabbelt <[email protected]>
Cc Nathan Chancellor <[email protected]>
Cc Nick Desaulniers <[email protected]>
---
 tools/lib/bpf/libbpf.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/lib/bpf/libbpf.c +1 -1
index 91b7106a4a73..65cb70cdc22b 100644
@@ -9837,7 +9837,7 @@ static int perf_event_open_probe(bool uprobe, bool retprobe, const char *name,
 	char errmsg[STRERR_BUFSIZE];
 	int type, pfd;
 
-	if (ref_ctr_off >= (1ULL << PERF_UPROBE_REF_CTR_OFFSET_BITS))
+	if (BITS_PER_LONG > 32 && ref_ctr_off >= (1ULL << PERF_UPROBE_REF_CTR_OFFSET_BITS))
 		return -EINVAL;
 
 	memset(&attr, 0, attr_sz);
Signature · 1 line
2.38.1
#1 lore raw
Quoted 30 lines · Khem Raj

Clang warns on 32-bit ARM on this comparision

libbpf.c:10497:18: error: result of comparison of constant 4294967296 with expression of type 'size_t' (aka 'unsigned int') is always false [-Werror,-Wtautological-constant-out-of-range-compare]

        if (ref_ctr_off >= (1ULL << PERF_UPROBE_REF_CTR_OFFSET_BITS))
            ~~~~~~~~~~~ ^  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Check for platform long int to be larger than 32-bits before enabling this check, it false on 32bit anyways.

Cc Alexei Starovoitov <[email protected]>
Cc Daniel Borkmann <[email protected]>
Cc Song Liu <[email protected]>
Cc Yonghong Song <[email protected]>
Cc Jiri Olsa <[email protected]>
Cc Paul Walmsley <[email protected]>
Cc Palmer Dabbelt <[email protected]>
Cc Nathan Chancellor <[email protected]>
Cc Nick Desaulniers <[email protected]>
---
 tools/lib/bpf/libbpf.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/lib/bpf/libbpf.c +0 -0
index 91b7106a4a73..65cb70cdc22b 100644

tools/lib/bpf/libbpf.c static int perf_event_open_probe(bool uprobe, bool retprobe, const char *name, · line 9840

Quoted 5 lines
tools/lib/bpf/libbpf.c +1 -1
@@ -9837,7 +9837,7 @@ static int perf_event_open_probe(bool uprobe, bool retprobe, const char *name,
        char errmsg[STRERR_BUFSIZE];
        int type, pfd;
 
-       if (ref_ctr_off >= (1ULL << PERF_UPROBE_REF_CTR_OFFSET_BITS))
+       if (BITS_PER_LONG > 32 && ref_ctr_off >= (1ULL << PERF_UPROBE_REF_CTR_OFFSET_BITS))

Where is BITS_PER_LONG defined? Is it a compiler-provided macro? A bit hesitant about taking dependency on such a macro. Also quick googling suggests user-space programs should use __BITS_PER_LONG?

Can you try if just casting ref_ctr_off to __u64 would make this warning go away?

Quoted 6 lines
tools/lib/bpf/libbpf.c +0 -1
                return -EINVAL;
 
        memset(&attr, 0, attr_sz);
--

2.38.1

author
#2 lore raw
Quoted 35 lines · Andrii Nakryiko
Quoted 33 lines · Khem Raj

Clang warns on 32-bit ARM on this comparision

libbpf.c:10497:18: error: result of comparison of constant 4294967296 with expression of type 'size_t' (aka 'unsigned int') is always false [-Werror,-Wtautological-constant-out-of-range-compare]

        if (ref_ctr_off >= (1ULL << PERF_UPROBE_REF_CTR_OFFSET_BITS))
            ~~~~~~~~~~~ ^  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Check for platform long int to be larger than 32-bits before enabling this check, it false on 32bit anyways.

Cc Alexei Starovoitov <[email protected]>
Cc Daniel Borkmann <[email protected]>
Cc Song Liu <[email protected]>
Cc Yonghong Song <[email protected]>
Cc Jiri Olsa <[email protected]>
Cc Paul Walmsley <[email protected]>
Cc Palmer Dabbelt <[email protected]>
Cc Nathan Chancellor <[email protected]>
Cc Nick Desaulniers <[email protected]>
---
 tools/lib/bpf/libbpf.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/lib/bpf/libbpf.c +0 -0
index 91b7106a4a73..65cb70cdc22b 100644
@@ -9837,7 +9837,7 @@ static int perf_event_open_probe(bool uprobe, bool retprobe, const char *name,
        char errmsg[STRERR_BUFSIZE];
        int type, pfd;
 
Quoted 6 lines

tools/lib/bpf/libbpf.c static int perf_event_open_probe(bool uprobe, bool retprobe, const char *name, · line 9840

Quoted 2 lines
tools/lib/bpf/libbpf.c +1 -1
-       if (ref_ctr_off >= (1ULL << PERF_UPROBE_REF_CTR_OFFSET_BITS))
+       if (BITS_PER_LONG > 32 && ref_ctr_off >= (1ULL << PERF_UPROBE_REF_CTR_OFFSET_BITS))

Where is BITS_PER_LONG defined? Is it a compiler-provided macro? A bit hesitant about taking dependency on such a macro. Also quick googling suggests user-space programs should use __BITS_PER_LONG?

BITS_PER_LONG is in include/asm-generic/bitsperlong.h, I have sent v2 which implements your suggestion

Quoted 10 lines

Can you try if just casting ref_ctr_off to __u64 would make this warning go away?

Quoted 6 lines
tools/lib/bpf/libbpf.c +0 -1
                return -EINVAL;
 
        memset(&attr, 0, attr_sz);
--

2.38.1