linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Donglin Peng <dolinux.peng@gmail.com>
To: ast@kernel.org
Cc: linux-kernel@vger.kernel.org, bpf@vger.kernel.org,
	Donglin Peng <dolinux.peng@gmail.com>,
	Eduard Zingerman <eddyz87@gmail.com>,
	Andrii Nakryiko <andrii.nakryiko@gmail.com>,
	Alan Maguire <alan.maguire@oracle.com>,
	Song Liu <song@kernel.org>, pengdonglin <pengdonglin@xiaomi.com>
Subject: [RFC PATCH v2 1/5] btf: search local BTF before base BTF
Date: Mon, 20 Oct 2025 17:39:37 +0800	[thread overview]
Message-ID: <20251020093941.548058-2-dolinux.peng@gmail.com> (raw)
In-Reply-To: <20251020093941.548058-1-dolinux.peng@gmail.com>

Change btf_find_by_name_kind() to search the local BTF first,
then fall back to the base BTF. This can skip traversing the large
vmlinux BTF when the target type resides in a kernel module's BTF,
thereby significantly improving lookup performance.

In a test searching for the btf_type of function ext2_new_inode
located in the ext2 kernel module:

Before: 408631 ns
After:     499 ns

Performance improvement: ~819x faster

Cc: Eduard Zingerman <eddyz87@gmail.com>
Cc: Alexei Starovoitov <ast@kernel.org>
Cc: Andrii Nakryiko <andrii.nakryiko@gmail.com>
Cc: Alan Maguire <alan.maguire@oracle.com>
Cc: Song Liu <song@kernel.org>
Signed-off-by: pengdonglin <pengdonglin@xiaomi.com>
Signed-off-by: Donglin Peng <dolinux.peng@gmail.com>
---
 include/linux/btf.h |  1 +
 kernel/bpf/btf.c    | 27 ++++++++++++++++++---------
 2 files changed, 19 insertions(+), 9 deletions(-)

diff --git a/include/linux/btf.h b/include/linux/btf.h
index f06976ffb63f..ddc53a7ac7cd 100644
--- a/include/linux/btf.h
+++ b/include/linux/btf.h
@@ -220,6 +220,7 @@ bool btf_is_module(const struct btf *btf);
 bool btf_is_vmlinux(const struct btf *btf);
 struct module *btf_try_get_module(const struct btf *btf);
 u32 btf_nr_types(const struct btf *btf);
+u32 btf_type_cnt(const struct btf *btf);
 struct btf *btf_base_btf(const struct btf *btf);
 bool btf_type_is_i32(const struct btf_type *t);
 bool btf_type_is_i64(const struct btf_type *t);
diff --git a/kernel/bpf/btf.c b/kernel/bpf/btf.c
index 0de8fc8a0e0b..c414cf37e1bd 100644
--- a/kernel/bpf/btf.c
+++ b/kernel/bpf/btf.c
@@ -544,22 +544,31 @@ u32 btf_nr_types(const struct btf *btf)
 	return total;
 }
 
+u32 btf_type_cnt(const struct btf *btf)
+{
+	return btf->start_id + btf->nr_types;
+}
+
 s32 btf_find_by_name_kind(const struct btf *btf, const char *name, u8 kind)
 {
 	const struct btf_type *t;
 	const char *tname;
 	u32 i, total;
 
-	total = btf_nr_types(btf);
-	for (i = 1; i < total; i++) {
-		t = btf_type_by_id(btf, i);
-		if (BTF_INFO_KIND(t->info) != kind)
-			continue;
+	do {
+		total = btf_type_cnt(btf);
+		for (i = btf->start_id; i < total; i++) {
+			t = btf_type_by_id(btf, i);
+			if (BTF_INFO_KIND(t->info) != kind)
+				continue;
 
-		tname = btf_name_by_offset(btf, t->name_off);
-		if (!strcmp(tname, name))
-			return i;
-	}
+			tname = btf_name_by_offset(btf, t->name_off);
+			if (!strcmp(tname, name))
+				return i;
+		}
+
+		btf = btf->base_btf;
+	} while (btf);
 
 	return -ENOENT;
 }
-- 
2.34.1


  reply	other threads:[~2025-10-20  9:39 UTC|newest]

Thread overview: 32+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-10-20  9:39 [RFC PATCH v2 0/5] Significantly Improve BTF Type Lookup Performance Donglin Peng
2025-10-20  9:39 ` Donglin Peng [this message]
2025-10-21  1:06   ` [RFC PATCH v2 1/5] btf: search local BTF before base BTF Eduard Zingerman
2025-10-21  8:31     ` Donglin Peng
2025-10-21 15:56       ` Eduard Zingerman
2025-10-22  3:08         ` Donglin Peng
2025-10-20  9:39 ` [RFC PATCH v2 2/5] btf: sort BTF types by kind and name to enable binary search Donglin Peng
2025-10-21 17:24   ` Alan Maguire
2025-10-22  4:47     ` Donglin Peng
2025-10-21 18:59   ` Eduard Zingerman
2025-10-22  3:02     ` Donglin Peng
2025-10-22 20:50       ` Eduard Zingerman
2025-10-23 10:35         ` Donglin Peng
2025-10-23 15:52           ` Alexei Starovoitov
2025-10-23 16:28             ` Andrii Nakryiko
2025-10-23 18:37               ` Alexei Starovoitov
2025-10-23 19:39                 ` Andrii Nakryiko
2025-10-24  1:59                   ` Donglin Peng
2025-10-24  2:23                     ` Donglin Peng
2025-10-24  2:32                       ` Eduard Zingerman
2025-10-24  3:04                         ` Donglin Peng
2025-10-24  3:15                           ` Eduard Zingerman
2025-10-24  3:19                             ` Donglin Peng
2025-10-20  9:39 ` [RFC PATCH v2 3/5] libbpf: check if BTF is sorted " Donglin Peng
2025-10-20  9:39 ` [RFC PATCH v2 4/5] selftests/bpf: add tests for BTF deduplication and sorting Donglin Peng
2025-10-21 19:07   ` Eduard Zingerman
2025-10-23 11:20     ` Donglin Peng
2025-10-20  9:39 ` [RFC PATCH v2 5/5] btf: add CONFIG_BPF_SORT_BTF_BY_KIND_NAME Donglin Peng
2025-10-21  0:50   ` Eduard Zingerman
2025-10-21  8:33     ` Donglin Peng
2025-10-21 17:27   ` Alan Maguire
2025-10-22  1:15     ` Donglin Peng

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=20251020093941.548058-2-dolinux.peng@gmail.com \
    --to=dolinux.peng@gmail.com \
    --cc=alan.maguire@oracle.com \
    --cc=andrii.nakryiko@gmail.com \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=eddyz87@gmail.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=pengdonglin@xiaomi.com \
    --cc=song@kernel.org \
    /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;
as well as URLs for NNTP newsgroup(s).