BPF List
 help / color / mirror / Atom feed
From: Yonghong Song <yhs@fb.com>
To: <bpf@vger.kernel.org>
Cc: Alexei Starovoitov <ast@kernel.org>,
	Andrii Nakryiko <andrii@kernel.org>,
	Daniel Borkmann <daniel@iogearbox.net>, <kernel-team@fb.com>
Subject: [PATCH bpf-next v2 06/18] libbpf: Add enum64 deduplication support
Date: Fri, 13 May 2022 20:12:53 -0700	[thread overview]
Message-ID: <20220514031253.3242578-1-yhs@fb.com> (raw)
In-Reply-To: <20220514031221.3240268-1-yhs@fb.com>

Add enum64 deduplication support. BTF_KIND_ENUM64 handling
is very similar to BTF_KIND_ENUM.

Signed-off-by: Yonghong Song <yhs@fb.com>
---
 tools/lib/bpf/btf.c | 55 +++++++++++++++++++++++++++++++++------------
 tools/lib/bpf/btf.h |  5 +++++
 2 files changed, 46 insertions(+), 14 deletions(-)

diff --git a/tools/lib/bpf/btf.c b/tools/lib/bpf/btf.c
index 3bb6780f710d..016cec84e3dd 100644
--- a/tools/lib/bpf/btf.c
+++ b/tools/lib/bpf/btf.c
@@ -3568,7 +3568,7 @@ static bool btf_equal_int_tag(struct btf_type *t1, struct btf_type *t2)
 	return info1 == info2;
 }
 
-/* Calculate type signature hash of ENUM. */
+/* Calculate type signature hash of ENUM/ENUM64. */
 static long btf_hash_enum(struct btf_type *t)
 {
 	long h;
@@ -3580,17 +3580,12 @@ static long btf_hash_enum(struct btf_type *t)
 	return h;
 }
 
-/* Check structural equality of two ENUMs. */
-static bool btf_equal_enum(struct btf_type *t1, struct btf_type *t2)
+static bool btf_equal_enum32_val(struct btf_type *t1, struct btf_type *t2)
 {
 	const struct btf_enum *m1, *m2;
-	__u16 vlen;
+	__u16 vlen = btf_vlen(t1);
 	int i;
 
-	if (!btf_equal_common(t1, t2))
-		return false;
-
-	vlen = btf_vlen(t1);
 	m1 = btf_enum(t1);
 	m2 = btf_enum(t2);
 	for (i = 0; i < vlen; i++) {
@@ -3602,15 +3597,44 @@ static bool btf_equal_enum(struct btf_type *t1, struct btf_type *t2)
 	return true;
 }
 
+static bool btf_equal_enum64_val(struct btf_type *t1, struct btf_type *t2)
+{
+	const struct btf_enum64 *m1, *m2;
+	__u16 vlen = btf_vlen(t1);
+	int i;
+
+	m1 = btf_enum64(t1);
+	m2 = btf_enum64(t2);
+	for (i = 0; i < vlen; i++) {
+		if (m1->name_off != m2->name_off || m1->val_lo32 != m2->val_lo32 ||
+		    m1->val_hi32 != m2->val_hi32)
+			return false;
+		m1++;
+		m2++;
+	}
+	return true;
+}
+
+/* Check structural equality of two ENUMs. */
+static bool btf_equal_enum_or_enum64(struct btf_type *t1, struct btf_type *t2)
+{
+	if (!btf_equal_common(t1, t2))
+		return false;
+
+	if (btf_is_enum(t1))
+		return btf_equal_enum32_val(t1, t2);
+	return btf_equal_enum64_val(t1, t2);
+}
+
 static inline bool btf_is_enum_fwd(struct btf_type *t)
 {
-	return btf_is_enum(t) && btf_vlen(t) == 0;
+	return btf_type_is_any_enum(t) && btf_vlen(t) == 0;
 }
 
-static bool btf_compat_enum(struct btf_type *t1, struct btf_type *t2)
+static bool btf_compat_enum_or_enum64(struct btf_type *t1, struct btf_type *t2)
 {
 	if (!btf_is_enum_fwd(t1) && !btf_is_enum_fwd(t2))
-		return btf_equal_enum(t1, t2);
+		return btf_equal_enum_or_enum64(t1, t2);
 	/* ignore vlen when comparing */
 	return t1->name_off == t2->name_off &&
 	       (t1->info & ~0xffff) == (t2->info & ~0xffff) &&
@@ -3829,6 +3853,7 @@ static int btf_dedup_prep(struct btf_dedup *d)
 			h = btf_hash_int_decl_tag(t);
 			break;
 		case BTF_KIND_ENUM:
+		case BTF_KIND_ENUM64:
 			h = btf_hash_enum(t);
 			break;
 		case BTF_KIND_STRUCT:
@@ -3898,15 +3923,16 @@ static int btf_dedup_prim_type(struct btf_dedup *d, __u32 type_id)
 		break;
 
 	case BTF_KIND_ENUM:
+	case BTF_KIND_ENUM64:
 		h = btf_hash_enum(t);
 		for_each_dedup_cand(d, hash_entry, h) {
 			cand_id = (__u32)(long)hash_entry->value;
 			cand = btf_type_by_id(d->btf, cand_id);
-			if (btf_equal_enum(t, cand)) {
+			if (btf_equal_enum_or_enum64(t, cand)) {
 				new_id = cand_id;
 				break;
 			}
-			if (btf_compat_enum(t, cand)) {
+			if (btf_compat_enum_or_enum64(t, cand)) {
 				if (btf_is_enum_fwd(t)) {
 					/* resolve fwd to full enum */
 					new_id = cand_id;
@@ -4211,7 +4237,8 @@ static int btf_dedup_is_equiv(struct btf_dedup *d, __u32 cand_id,
 		return btf_equal_int_tag(cand_type, canon_type);
 
 	case BTF_KIND_ENUM:
-		return btf_compat_enum(cand_type, canon_type);
+	case BTF_KIND_ENUM64:
+		return btf_compat_enum_or_enum64(cand_type, canon_type);
 
 	case BTF_KIND_FWD:
 	case BTF_KIND_FLOAT:
diff --git a/tools/lib/bpf/btf.h b/tools/lib/bpf/btf.h
index a41463bf9060..b22c648c69ff 100644
--- a/tools/lib/bpf/btf.h
+++ b/tools/lib/bpf/btf.h
@@ -531,6 +531,11 @@ static inline bool btf_is_type_tag(const struct btf_type *t)
 	return btf_kind(t) == BTF_KIND_TYPE_TAG;
 }
 
+static inline bool btf_type_is_any_enum(const struct btf_type *t)
+{
+	return btf_is_enum(t) || btf_is_enum64(t);
+}
+
 static inline __u8 btf_int_encoding(const struct btf_type *t)
 {
 	return BTF_INT_ENCODING(*(__u32 *)(t + 1));
-- 
2.30.2


  parent reply	other threads:[~2022-05-14  3:13 UTC|newest]

Thread overview: 45+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-05-14  3:12 [PATCH bpf-next v2 00/18] bpf: Add 64bit enum value support Yonghong Song
2022-05-14  3:12 ` [PATCH bpf-next v2 01/18] bpf: Add btf enum64 support Yonghong Song
2022-05-17  0:06   ` Andrii Nakryiko
2022-05-14  3:12 ` [PATCH bpf-next v2 02/18] libbpf: Permit 64bit relocation value Yonghong Song
2022-05-17  0:08   ` Andrii Nakryiko
2022-05-14  3:12 ` [PATCH bpf-next v2 03/18] libbpf: Fix an error in 64bit relocation value computation Yonghong Song
2022-05-17  0:10   ` Andrii Nakryiko
2022-05-14  3:12 ` [PATCH bpf-next v2 04/18] libbpf: Refactor btf__add_enum() for future code sharing Yonghong Song
2022-05-17  0:15   ` Andrii Nakryiko
2022-05-14  3:12 ` [PATCH bpf-next v2 05/18] libbpf: Add enum64 parsing and new enum64 public API Yonghong Song
2022-05-17  0:15   ` Andrii Nakryiko
2022-05-14  3:12 ` Yonghong Song [this message]
2022-05-17  0:28   ` [PATCH bpf-next v2 06/18] libbpf: Add enum64 deduplication support Andrii Nakryiko
2022-05-17 17:11     ` Yonghong Song
2022-05-17 22:22       ` Andrii Nakryiko
2022-05-14  3:12 ` [PATCH bpf-next v2 07/18] libbpf: Add enum64 support for btf_dump Yonghong Song
2022-05-17  0:37   ` Andrii Nakryiko
2022-05-17 17:15     ` Yonghong Song
2022-05-14  3:13 ` [PATCH bpf-next v2 08/18] libbpf: Add enum64 sanitization Yonghong Song
2022-05-17 23:25   ` Andrii Nakryiko
2022-05-18 21:08     ` Yonghong Song
2022-05-14  3:13 ` [PATCH bpf-next v2 09/18] libbpf: Add enum64 support for bpf linking Yonghong Song
2022-05-17 23:25   ` Andrii Nakryiko
2022-05-14  3:13 ` [PATCH bpf-next v2 10/18] libbpf: Add enum64 relocation support Yonghong Song
2022-05-17 23:32   ` Andrii Nakryiko
2022-05-18 21:09     ` Yonghong Song
2022-05-14  3:13 ` [PATCH bpf-next v2 11/18] bpftool: Add btf enum64 support Yonghong Song
2022-05-17 23:38   ` Andrii Nakryiko
2022-05-18 21:10     ` Yonghong Song
2022-05-14  3:13 ` [PATCH bpf-next v2 12/18] selftests/bpf: Fix selftests failure Yonghong Song
2022-05-17 23:33   ` Andrii Nakryiko
2022-05-14  3:13 ` [PATCH bpf-next v2 13/18] selftests/bpf: Test new enum kflag and enum64 API functions Yonghong Song
2022-05-17 23:40   ` Andrii Nakryiko
2022-05-18 21:12     ` Yonghong Song
2022-05-14  3:13 ` [PATCH bpf-next v2 14/18] selftests/bpf: Add BTF_KIND_ENUM64 unit tests Yonghong Song
2022-05-17 23:41   ` Andrii Nakryiko
2022-05-14  3:13 ` [PATCH bpf-next v2 15/18] selftests/bpf: Test BTF_KIND_ENUM64 for deduplication Yonghong Song
2022-05-17 23:43   ` Andrii Nakryiko
2022-05-14  3:13 ` [PATCH bpf-next v2 16/18] selftests/bpf: Add a test for enum64 value relocations Yonghong Song
2022-05-17 23:45   ` Andrii Nakryiko
2022-05-14  3:13 ` [PATCH bpf-next v2 17/18] selftests/bpf: Clarify llvm dependency with possible selftest failures Yonghong Song
2022-05-17 23:45   ` Andrii Nakryiko
2022-05-14  3:13 ` [PATCH bpf-next v2 18/18] docs/bpf: Update documentation for BTF_KIND_ENUM64 support Yonghong Song
2022-05-17 23:47   ` Andrii Nakryiko
2022-05-18 21:21     ` Yonghong Song

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220514031253.3242578-1-yhs@fb.com \
    --to=yhs@fb.com \
    --cc=andrii@kernel.org \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=kernel-team@fb.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox