[<prev] [next>] [thread-next>] [day] [month] [year] [list]
Message-Id: <20220204220435.301896-1-mauricio@kinvolk.io>
Date: Fri, 4 Feb 2022 17:04:35 -0500
From: Mauricio Vásquez <mauricio@...volk.io>
To: netdev@...r.kernel.org, bpf@...r.kernel.org
Cc: Alexei Starovoitov <ast@...nel.org>,
Daniel Borkmann <daniel@...earbox.net>,
Andrii Nakryiko <andrii@...nel.org>,
Quentin Monnet <quentin@...valent.com>
Subject: [PATCH bpf-next] libbpf: Fix strict mode calculation
The correct formula to get all possible values is
((__LIBBPF_STRICT_LAST - 1) * 2 - 1) as stated in
libbpf_set_strict_mode().
Fixes: 93b8952d223a ("libbpf: deprecate legacy BPF map definitions")
Signed-off-by: Mauricio Vásquez <mauricio@...volk.io>
---
tools/bpf/bpftool/main.c | 6 +++++-
tools/testing/selftests/bpf/prog_tests/btf.c | 10 ++++++++--
2 files changed, 13 insertions(+), 3 deletions(-)
diff --git a/tools/bpf/bpftool/main.c b/tools/bpf/bpftool/main.c
index 9d01fa9de033..c5b27e41d1e9 100644
--- a/tools/bpf/bpftool/main.c
+++ b/tools/bpf/bpftool/main.c
@@ -483,8 +483,12 @@ int main(int argc, char **argv)
/* Allow legacy map definitions for skeleton generation.
* It will still be rejected if users use LIBBPF_STRICT_ALL
* mode for loading generated skeleton.
+ *
+ * __LIBBPF_STRICT_LAST is the last power-of-2 value used + 1, so to
+ * get all possible values we compensate last +1, and then (2*x - 1)
+ * to get the bit mask
*/
- mode = (__LIBBPF_STRICT_LAST - 1) & ~LIBBPF_STRICT_MAP_DEFINITIONS;
+ mode = ((__LIBBPF_STRICT_LAST - 1) * 2 - 1) & ~LIBBPF_STRICT_MAP_DEFINITIONS;
ret = libbpf_set_strict_mode(mode);
if (ret)
p_err("failed to enable libbpf strict mode: %d", ret);
diff --git a/tools/testing/selftests/bpf/prog_tests/btf.c b/tools/testing/selftests/bpf/prog_tests/btf.c
index 14f9b6136794..90d5cd4f504c 100644
--- a/tools/testing/selftests/bpf/prog_tests/btf.c
+++ b/tools/testing/selftests/bpf/prog_tests/btf.c
@@ -4533,6 +4533,7 @@ static void do_test_file(unsigned int test_num)
struct btf_ext *btf_ext = NULL;
struct bpf_prog_info info = {};
struct bpf_object *obj = NULL;
+ enum libbpf_strict_mode mode;
struct bpf_func_info *finfo;
struct bpf_program *prog;
__u32 info_len, rec_size;
@@ -4560,8 +4561,13 @@ static void do_test_file(unsigned int test_num)
has_btf_ext = btf_ext != NULL;
btf_ext__free(btf_ext);
- /* temporary disable LIBBPF_STRICT_MAP_DEFINITIONS to test legacy maps */
- libbpf_set_strict_mode((__LIBBPF_STRICT_LAST - 1) & ~LIBBPF_STRICT_MAP_DEFINITIONS);
+ /* temporary disable LIBBPF_STRICT_MAP_DEFINITIONS to test legacy maps
+ * __LIBBPF_STRICT_LAST is the last power-of-2 value used + 1, so to
+ * get all possible values we compensate last +1, and then (2*x - 1)
+ * to get the bit mask
+ */
+ mode = ((__LIBBPF_STRICT_LAST - 1) * 2 - 1) & ~LIBBPF_STRICT_MAP_DEFINITIONS
+ libbpf_set_strict_mode(mode);
obj = bpf_object__open(test->file);
err = libbpf_get_error(obj);
if (CHECK(err, "obj: %d", err))
--
2.25.1
Powered by blists - more mailing lists