From nobody Sun Feb 8 23:25:47 2026 Received: from mail-pf1-f179.google.com (mail-pf1-f179.google.com [209.85.210.179]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 2D95B2F7453 for ; Mon, 20 Oct 2025 09:39:52 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.179 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1760953194; cv=none; b=O2wQGVEVD3tndjCmLVNoQoyq3+plxxd4KHRwDmoXNJfDUA/+VITDmoqLgSTqvZ2qSC/AGS+UNHOu6/eFCfoz3SinVgIBQdauqtWWqGFkbKrqqPsMYaW6MTVUQ+h4s08rPdwuGthnbvncmQj+4dEmS1UlaDBRFwAi4nSnn7uN9js= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1760953194; c=relaxed/simple; bh=ugDn8c1lW1orwqdqhvNXZZRri40gpMmMmMcEemnXh6c=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=pgjeYT/7wRC60hXhoYkTWxGi0WfzNb301Daut0HkqAIwHMAaRgENjoOVih4Xco1oiDYvzfB/ufj4viU6GjOUIztgcOVB8NwLIM0vprJXh4Q9qVudIXhavdHeSqcEi9yXuX15sh14cuT0Y0Z6wKIVdzMyExKaYE5OegfzPbceGGs= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=j4aL8M+I; arc=none smtp.client-ip=209.85.210.179 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="j4aL8M+I" Received: by mail-pf1-f179.google.com with SMTP id d2e1a72fcca58-781db5068b8so3164485b3a.0 for ; Mon, 20 Oct 2025 02:39:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1760953192; x=1761557992; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=52FJ4sV7PeUmrKe+3MVtvr5aRK62gcaubO05HKMB1Gs=; b=j4aL8M+I7seDFaslC8lfK/X/ytzafCZqx9oyZh55DmhKKDex5Px24Hq+tBP9nVdG1w BcdlRMuQJ/EyQaY9jcJBkDahPjP7wIKxPDgeYjQ/0RleJh8KUqJ5Uw5h2F8iwbO42Urj qQcyaUVY6n/5NYlSjvvK3M2C7R0pdt2kz1o90JWLPZe+vbZpcqtgNSb+v5DvZgrUW7Yy oyWkN1Zn/54FhKNpxNMAzO9QjYQqjccW9+P5ELQLaMMg2qI1gnprkW5BYE2ed4woWIa3 LEmlyIZcvuPO+M25ZefidjWaBT/390sP0A70UqUzJk9Q/QsCSpz7eeThJ0LQbFaxuyLc 4Q7g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1760953192; x=1761557992; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=52FJ4sV7PeUmrKe+3MVtvr5aRK62gcaubO05HKMB1Gs=; b=IEh54pLrk7OzaQptD+0WbggAFHsgA2QpLdo3kmwcbkrn6zNwP4HSIlHrHbx/3TRLLN xODsw7upNaO3BZHHhlSqgpKHmi/6zZf6k95uxtt9JzfYdUfWjd4pCjrLFpFM2HUxaZZw 7jnwOjMk+qXniDkIQ2PT/DBLwYEo96safo+Bu8r/vJ6ZgG2nP2MyXvH5lwQZjQEwWMJD wPDPLV6dCVIw6PdKbwkM8CDke14xi2RWM+BVVBvRfePmvAMMjIl2vhdYi0/4qCUxx6zx qvFS1j7UR2cN1FYvpt9Zt6UaZwDkVCpTOZg50O1lW1+3OgxlYZAx2BzmpXQKHzNF0kul RceA== X-Gm-Message-State: AOJu0Yyf7PGLjer/zyNypMBEQyMMlykojqcT0/uqphg0Mhdac9FFFCr2 7FNEl+Bklzlh8ycktkQ64e54hu6ME3SB5lT4yMc8r5m05JNS4IM25QQL X-Gm-Gg: ASbGncvY8we4S19bUCdNw+v6Wq/FdOBfR7nwk+3huPhLlRIySNdIdyW9Oi80/BYEENQ 6iiGAtEQZM6pWG0oswsN9t5pK+ca8kK47VLFPrtswPZehiUYeYKRoXP+RphoJ8Ccq+Vam0YPqVG rcd3XQDQKah7AraQ63ekb2FEGRo74HFHNHWaxeYRq1azvX/r/prhGiu6LlwcMmAaVi+EgBUA/aU H4EEUk8WWBDY8WaOXgzsBTKua4ZqzYRlPC8y7Ssnv+6WLL1R3RR0H9/TCy4/c/m7W4H2JXEdil2 GMyhHxSkwAeeX5JMJw8EOEV0scyWAWfDnb2MJ1zz2Qzd/kFVET4uZ2yffMJpfJkgSP1ZGNQG9eQ ZL12sAvKw4LrYmUbxefZZQWXYM0BUy+8auqArPe8Jv1EkDJlThr0K4zVoZuEX8lBguf/H++QaRi wtLcncvvqKMPSVwTVGexQNOo6x1Zo= X-Google-Smtp-Source: AGHT+IEchgGNOZqdezdEtqnT/rI36dWl4KIQU1k/DQ/xqsCVOOSCdlbhYXFAwti9fT8qv9KnNzaQvQ== X-Received: by 2002:a17:90b:3dcb:b0:32e:b87e:a961 with SMTP id 98e67ed59e1d1-33bcf85b526mr14610819a91.5.1760953192310; Mon, 20 Oct 2025 02:39:52 -0700 (PDT) Received: from pengdl-pc.mioffice.cn ([43.224.245.249]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-33d5de8091fsm7617200a91.19.2025.10.20.02.39.49 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 20 Oct 2025 02:39:51 -0700 (PDT) From: Donglin Peng To: ast@kernel.org Cc: linux-kernel@vger.kernel.org, bpf@vger.kernel.org, Donglin Peng , Eduard Zingerman , Andrii Nakryiko , Alan Maguire , Song Liu , pengdonglin Subject: [RFC PATCH v2 1/5] btf: search local BTF before base BTF Date: Mon, 20 Oct 2025 17:39:37 +0800 Message-Id: <20251020093941.548058-2-dolinux.peng@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20251020093941.548058-1-dolinux.peng@gmail.com> References: <20251020093941.548058-1-dolinux.peng@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" 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 Cc: Alexei Starovoitov Cc: Andrii Nakryiko Cc: Alan Maguire Cc: Song Liu Signed-off-by: pengdonglin Signed-off-by: Donglin Peng --- 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; } =20 +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; =20 - total =3D btf_nr_types(btf); - for (i =3D 1; i < total; i++) { - t =3D btf_type_by_id(btf, i); - if (BTF_INFO_KIND(t->info) !=3D kind) - continue; + do { + total =3D btf_type_cnt(btf); + for (i =3D btf->start_id; i < total; i++) { + t =3D btf_type_by_id(btf, i); + if (BTF_INFO_KIND(t->info) !=3D kind) + continue; =20 - tname =3D btf_name_by_offset(btf, t->name_off); - if (!strcmp(tname, name)) - return i; - } + tname =3D btf_name_by_offset(btf, t->name_off); + if (!strcmp(tname, name)) + return i; + } + + btf =3D btf->base_btf; + } while (btf); =20 return -ENOENT; } --=20 2.34.1