netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH bpf-next] libbpf: Fix strict mode calculation
@ 2022-02-04 22:04 Mauricio Vásquez
  2022-02-04 22:24 ` Mauricio Vásquez Bernal
  2022-02-04 22:55 ` Andrii Nakryiko
  0 siblings, 2 replies; 4+ messages in thread
From: Mauricio Vásquez @ 2022-02-04 22:04 UTC (permalink / raw)
  To: netdev, bpf
  Cc: Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko,
	Quentin Monnet

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@kinvolk.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


^ permalink raw reply related	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2022-02-04 22:55 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2022-02-04 22:04 [PATCH bpf-next] libbpf: Fix strict mode calculation Mauricio Vásquez
2022-02-04 22:24 ` Mauricio Vásquez Bernal
2022-02-04 22:54   ` Andrii Nakryiko
2022-02-04 22:55 ` Andrii Nakryiko

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).