From nobody Mon Feb 9 00:27:30 2026 Received: from mail-pj1-f45.google.com (mail-pj1-f45.google.com [209.85.216.45]) (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 7508131A55B for ; Thu, 18 Dec 2025 11:31:40 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.45 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1766057505; cv=none; b=WuJoXKhGuFosSrjv6/eMBbqJWe7PDA1eOOb9qPhNkjhM0zybsvvDJ0r1iQAfvVNbdkCpS8cvnmTsxCuxa8H1oXboE7GxVzJPIjvyszFyslysBovy5o9EdpfzVTCG57OslzGPrMJdJt0Gz9s3urXx474nDOCNTKQCxW4opzI4bWM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1766057505; c=relaxed/simple; bh=e4PdXHzqE2jWb1Mz7VBs2Ivn5mRVqnf4USlw0IL3LQk=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=k4Oac7su6XUriXwM6V9Vd6YHkyhe4dEcPO9wPfEB+CQ4wmyZFKb051AzAxS3cJHst3jRNhC6cqoZvvDxslo/LenYd5GGZts2KPXj8LO/VIa27wwMQl99N87v6nA3lqJ8eRrerrO4WsfEH/InOz87DsMGDlz9+AFBIN2kDuJGNGc= 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=mD2iNS3L; arc=none smtp.client-ip=209.85.216.45 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="mD2iNS3L" Received: by mail-pj1-f45.google.com with SMTP id 98e67ed59e1d1-34c868b197eso589414a91.2 for ; Thu, 18 Dec 2025 03:31:40 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1766057500; x=1766662300; 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=q4ua/Mfoorz93gkg4ZzzsoaKq2Zva6tuuEtR6k0T5nY=; b=mD2iNS3L6DSfQszNGOnft+OpQeMiBf8RjgEF6yLadDyH/X/nhRQnG5y97VtqoqaH5S i1dI9eN2LkySO1T3v09QHRyoshQKBGLFuoPUKr+7wgpo/949sUj04Zb7vxlE6Q3guqm9 +9oax0k8piB7thXUoOKZK53K94tkPXTB5IRs6BPZ4hYaGVrDVX9/QvHUMtFHbmqzGJlr 4YjF8WqrgN08ocYK15LY++hPLXEUPPYMdZEtP0Y/amrADs6rdeSpVSLBozDtfBRwGvXd YIPEy+insnFpxr6r5XXGjiFkPtE68lGLYWy2z+vf2tdevxopKwutbKPOr2oy1579KcwY 5EWw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1766057500; x=1766662300; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=q4ua/Mfoorz93gkg4ZzzsoaKq2Zva6tuuEtR6k0T5nY=; b=tHWGbOsBtHf+YpIA4tyGYFyGcIVphPq2HavEoQOhfosuHOexSwEx2lAIPGqAbrsEkp 09yCwKl6mBaMt9L9N4YMQML7wU0h5N/db938b2gdZqX6URxdYZ6zjSczuH8Cn1Fg+dCQ zoQVFJm4iowoXmsxP6OWQK7VyhOOI6Nfn8bQIMPfY5xtWymxTOF7Zl3HxH0R6YrZsehl 707SZabpXBDbsCpnXEubBZlkCo2gpvW5cZQ2O57QmUB0VoXdKsZGUTukeBBskbzWNXrg NBTLhUKo3q3BJTYo/eB7tqh/geoptIh6Svm1WuZ0MzgxjTY8kdxDgLee4/QvgE1JG8CY hAog== X-Forwarded-Encrypted: i=1; AJvYcCX5qLTHRkWV366IkUyBTTN1kFJDsU0Z8ibU0zbsHvj9TC/Aw9ddhLXgh64jxDSVM+ksxpfPNirqMuPo4Do=@vger.kernel.org X-Gm-Message-State: AOJu0YyIzaVMTkAM/Epq7XzOhd/su9C5BU6tP+z/wnHsf8eWWaolySTR AA0HQFa9AVoV0FczXhvZ7Cmxp9Hnvq5+N/3ccnIbyinq+IW7mtG2s67h X-Gm-Gg: AY/fxX4h6TzwWUMmPndL0yibTyN71ctbojnYoD/biSuFlsOKkFXuR6IMs5UoCTdDvg1 ioz3WZLAtuEORmYI55YDXMnw7ZNfLQm7OUROKzwNoPdF8nCseNtS0T/M+uxNuFtAs9m0Oh41Y9O oVizLPmLKaftCWwbmAtUkSonF7bsDaM9keXKQVsZggnB4utqfxf85JVNxtXaEUaH5NtZXEfJA/Y v22gyuQkk+q9Juhuk1kAAsCmzbIPUGGZF7HTHJNjSA3VoPepF4IX+lFkHk4won83COJk5TxlTUh ox9smjNssa0eYd0cETdCozFy/6K2YnS8OBQX32cTZS8UgNzU0WgwRPc6/8izpcmIT8Jhb0r9yeV t6Ob/iVJnw7oeMsC+R6wLij1479IdkpMLOanUGlFxkDaL8S6O6UWHJFMRF/e6d8TDw3LRVgp0jT YaptfJ78drMUDuvzDGQDgVCf7+gGU= X-Google-Smtp-Source: AGHT+IHwEc3GSxPWRabZgzS1RVEcFDoMz8zwnEifDJsenEmzvMA0MJNr47Yymxi+cbS6e1uISU046Q== X-Received: by 2002:a17:90b:1dc6:b0:32d:db5b:7636 with SMTP id 98e67ed59e1d1-34abd7853bcmr18909720a91.27.1766057499551; Thu, 18 Dec 2025 03:31:39 -0800 (PST) Received: from pengdl-pc.mioffice.cn ([43.224.245.249]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-34e70d4f887sm2328237a91.3.2025.12.18.03.31.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 18 Dec 2025 03:31:38 -0800 (PST) From: Donglin Peng To: ast@kernel.org, andrii.nakryiko@gmail.com, eddyz87@gmail.com Cc: zhangxiaoqin@xiaomi.com, ihor.solodrai@linux.dev, linux-kernel@vger.kernel.org, bpf@vger.kernel.org, pengdonglin , Alan Maguire Subject: [PATCH bpf-next v10 08/13] bpf: Skip anonymous types in type lookup for performance Date: Thu, 18 Dec 2025 19:30:46 +0800 Message-Id: <20251218113051.455293-9-dolinux.peng@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20251218113051.455293-1-dolinux.peng@gmail.com> References: <20251218113051.455293-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" From: pengdonglin Currently, vmlinux and kernel module BTFs are unconditionally sorted during the build phase, with named types placed at the end. Thus, anonymous types should be skipped when starting the search. In my vmlinux BTF, the number of anonymous types is 61,747, which means the loop count can be reduced by 61,747. Cc: Eduard Zingerman Cc: Alexei Starovoitov Cc: Andrii Nakryiko Cc: Alan Maguire Cc: Ihor Solodrai Cc: Xiaoqin Zhang Signed-off-by: pengdonglin Acked-by: Eduard Zingerman --- include/linux/btf.h | 1 + kernel/bpf/btf.c | 24 ++++++++++++++++++++---- kernel/bpf/verifier.c | 7 +------ 3 files changed, 22 insertions(+), 10 deletions(-) diff --git a/include/linux/btf.h b/include/linux/btf.h index f06976ffb63f..2d28f2b22ae5 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_sorted_start_id(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 a9e2345558c0..3aeb4f00cbfe 100644 --- a/kernel/bpf/btf.c +++ b/kernel/bpf/btf.c @@ -550,6 +550,11 @@ u32 btf_nr_types(const struct btf *btf) return total; } =20 +u32 btf_sorted_start_id(const struct btf *btf) +{ + return btf->sorted_start_id ?: (btf->start_id ?: 1); +} + /* * Assuming that types are sorted by name in ascending order. */ @@ -3540,9 +3545,14 @@ const char *btf_find_decl_tag_value(const struct btf= *btf, const struct btf_type { const char *value =3D NULL; const struct btf_type *t; + const struct btf *base_btf =3D btf; int len, id; =20 - id =3D btf_find_next_decl_tag(btf, pt, comp_idx, tag_key, 0); + while (base_btf->base_btf) + base_btf =3D base_btf->base_btf; + + id =3D btf_find_next_decl_tag(btf, pt, comp_idx, tag_key, + btf_sorted_start_id(base_btf) - 1); if (id < 0) return ERR_PTR(id); =20 @@ -7787,6 +7797,7 @@ int btf_prepare_func_args(struct bpf_verifier_env *en= v, int subprog) struct bpf_prog *prog =3D env->prog; enum bpf_prog_type prog_type =3D prog->type; struct btf *btf =3D prog->aux->btf; + struct btf *base_btf; const struct btf_param *args; const struct btf_type *t, *ref_t, *fn_t; u32 i, nargs, btf_id; @@ -7852,12 +7863,17 @@ int btf_prepare_func_args(struct bpf_verifier_env *= env, int subprog) tname); return -EINVAL; } + + base_btf =3D btf; + while (base_btf->base_btf) + base_btf =3D base_btf->base_btf; + /* Convert BTF function arguments into verifier types. * Only PTR_TO_CTX and SCALAR are supported atm. */ for (i =3D 0; i < nargs; i++) { u32 tags =3D 0; - int id =3D 0; + int id =3D btf_sorted_start_id(base_btf) - 1; =20 /* 'arg:' decl_tag takes precedence over derivation of * register type from BTF type itself @@ -9338,7 +9354,7 @@ bpf_core_find_cands(struct bpf_core_ctx *ctx, u32 loc= al_type_id) } =20 /* Attempt to find target candidates in vmlinux BTF first */ - cands =3D bpf_core_add_cands(cands, main_btf, 1); + cands =3D bpf_core_add_cands(cands, main_btf, btf_sorted_start_id(main_bt= f)); if (IS_ERR(cands)) return ERR_CAST(cands); =20 @@ -9370,7 +9386,7 @@ bpf_core_find_cands(struct bpf_core_ctx *ctx, u32 loc= al_type_id) */ btf_get(mod_btf); spin_unlock_bh(&btf_idr_lock); - cands =3D bpf_core_add_cands(cands, mod_btf, btf_nr_types(main_btf)); + cands =3D bpf_core_add_cands(cands, mod_btf, btf_sorted_start_id(mod_btf= )); btf_put(mod_btf); if (IS_ERR(cands)) return ERR_CAST(cands); diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c index d6b8a77fbe3b..1a9da59d8589 100644 --- a/kernel/bpf/verifier.c +++ b/kernel/bpf/verifier.c @@ -20651,12 +20651,7 @@ static int find_btf_percpu_datasec(struct btf *btf) * types to look at only module's own BTF types. */ n =3D btf_nr_types(btf); - if (btf_is_module(btf)) - i =3D btf_nr_types(btf_vmlinux); - else - i =3D 1; - - for(; i < n; i++) { + for (i =3D btf_sorted_start_id(btf); i < n; i++) { t =3D btf_type_by_id(btf, i); if (BTF_INFO_KIND(t->info) !=3D BTF_KIND_DATASEC) continue; --=20 2.34.1