From nobody Tue Dec 16 21:10:24 2025 Received: from mail-pl1-f181.google.com (mail-pl1-f181.google.com [209.85.214.181]) (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 535BA313E3A for ; Mon, 8 Dec 2025 06:24:28 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.181 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1765175069; cv=none; b=sk2bME5gqsDD0AhtI9WGM25iM4C5ebe50dIlRxZERpQicONrx6lteMqyhHqicczeR29iWz1Aa73criPyekuW9m8iar/fD1O76G7czM0Ps9AUWJ/R0eFBaeb/XHf5ySdVDRcv0eGEDJt4W3bOkSxnpD5aRMMktMHVXj5Vpe1MJQ0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1765175069; c=relaxed/simple; bh=i7QanyYSi/r+PgBdCryQh6I5m7xPTm9Iqix4Q3VKzZ4=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=qb/rUdz95Qhz1FSZpSn40sr+04x1CikmTTcUhEaXO+sIffVljyljXWyPYINVWzDv1oKkMAB6BbJufRy+M4O2B6vywSQIjBgSlEDpZo+TMswZjG4DA3ZeChbJ6WcNBp4+cNE0pq6dxRfeVKeP0ALrukIeXZ7nKHnGpuiym6JhsIM= 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=TnMuIIdP; arc=none smtp.client-ip=209.85.214.181 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="TnMuIIdP" Received: by mail-pl1-f181.google.com with SMTP id d9443c01a7336-29812589890so57424075ad.3 for ; Sun, 07 Dec 2025 22:24:28 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1765175068; x=1765779868; 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=s9Fz+H+Y9xd77iPc8boezvXQ6uLdCfh3xlXlYrH3URU=; b=TnMuIIdPju7kQMGEpbUgDAf/1Z0KOEki1Oxanpm//yDE4+UylorsFk53GZJXotAJ3T GKAufyO04eAOeuBHBFOO1LlmOu5Y92sSU/AZI/gMlYAMb0XyUwFx15wtSc9SpQgSsGnM HLyalj3qmcsWUPYFNhZ6aiWZcG+Q4jyo3i8YinJYlPJZcQs4or2JVg9TdrvO1P4zoRVf VAOvpHrvNtfLdQW/+57+DPbzrNr3jGv4FXB7Da/DmWAlKCc8kB/3FjHa/rCQXZpkr8II 3rPj/uuoLF/RVQj53zwZf4byQ7bOrif+hIv1wopg11bTkkFjlGLvDA1GcKADz1tlMAoE PzJw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1765175068; x=1765779868; 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=s9Fz+H+Y9xd77iPc8boezvXQ6uLdCfh3xlXlYrH3URU=; b=kUWLttxuNRISkZGm70XIkB9+AqSiugQLsq3Nx8InMiJxhgSSsLmCJmC1gvnl3CApiG 4sCM8lLzafPqRQ3Qh0kBAznSi/O3ifcotX+dIasi5bMcvhhZFgiceJXWGi5EhvYw4OUT 0M64EjAgzHvnjxFSJ3ykMhty3myMnS279M86dU5VKOqdM6rmudRVUoYnfqJKbcZuaFiI //THpT1CwKvFoogqfkPFjgr9h0Wsqd/X7M4aoiK30GgAooncD7sFWdSz0roara78Wuss hj7r+guAowCA3a5L/VTiTDldHDvblH897Sm3loK27gC+LVzTJANX6fqdCs7BXc+WoCJR SQJQ== X-Forwarded-Encrypted: i=1; AJvYcCWx7hIsRavX3JXiqlTEM25pJvl4bZ+iWvW+LEuh2BsO0pGRm+n23D8Cxg/XcMprQZH8Eg1zJ3QJ9rSOnkw=@vger.kernel.org X-Gm-Message-State: AOJu0YzOg/RQHnpD/e8udR0EjdAmNT2o3tmlV13IB90mPbSdx26uVR54 bYBWyWN5l6VPQ0e4c08oIx0jLN8jB3XRIqB8i8Xq3gjEOLdmgMe+Y8TF X-Gm-Gg: ASbGncvROhoiop+vC7MLpXrg4zk4ciTp91LiVL2GZanocQ+F08oh8MLMxVZ7+7OnzYz gllYPJ+N5lBYU8OS8xrDvc8AzFeuvNTCdSBLno8mtNjOYxUk01kW8o4CRP0hc6i365apqoehdzx Wrve1WVPPaC8aoYzHseqa1lZujQDwHfBO+xsoiDxjDagYfbH2L13h9/Lg4F2fCP8P/7iol8vCsa Uu/nb9TjUYa7q7hNGk4TayKj0RdhfnVYSsxY8O9AeQgLrVrJQq+B75ZKqV/U12LJoJRDqUj5ooO HItl4c2EelpePFJK/w6c916EC/UtS9pmov05cHZ30y+rlgAY/LdvGITwjckzctr0mV2xGPsuhBr lkKjUX5/gJLOPrUz9TD79giqKt8Wzo0+mS5/YiggBiqHxgvDCKRaK5Q2RjffLYgoL31uV13n/Co kGJ1vQAOl2pILvkxu0vQLwAHsLRWE= X-Google-Smtp-Source: AGHT+IEcsc2LtgH3QM15NWRvotNsGzkF1f7ECf0FET9aNq28ZN1vpLBBARPtfOIx4sM9uf1KOSiEow== X-Received: by 2002:a17:903:41c7:b0:298:ea9:5732 with SMTP id d9443c01a7336-29df5db8441mr55529665ad.41.1765175067626; Sun, 07 Dec 2025 22:24:27 -0800 (PST) Received: from pengdl-pc.mioffice.cn ([43.224.245.249]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-29dae49ca1esm112555855ad.2.2025.12.07.22.24.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 07 Dec 2025 22:24:26 -0800 (PST) From: Donglin Peng To: ast@kernel.org, andrii.nakryiko@gmail.com Cc: eddyz87@gmail.com, zhangxiaoqin@xiaomi.com, ihor.solodrai@linux.dev, linux-kernel@vger.kernel.org, bpf@vger.kernel.org, pengdonglin , Alan Maguire Subject: [PATCH bpf-next v9 08/10] bpf: Skip anonymous types in type lookup for performance Date: Mon, 8 Dec 2025 14:23:51 +0800 Message-Id: <20251208062353.1702672-9-dolinux.peng@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20251208062353.1702672-1-dolinux.peng@gmail.com> References: <20251208062353.1702672-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 --- include/linux/btf.h | 1 + kernel/bpf/btf.c | 15 ++++++++++----- kernel/bpf/verifier.c | 7 +------ 3 files changed, 12 insertions(+), 11 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 925cb524f3a8..5f4f51b0acf4 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,9 @@ 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; - int len, id; + int len, id =3D btf->sorted_start_id > 0 ? btf->sorted_start_id - 1 : 0; =20 - id =3D btf_find_next_decl_tag(btf, pt, comp_idx, tag_key, 0); + id =3D btf_find_next_decl_tag(btf, pt, comp_idx, tag_key, id); if (id < 0) return ERR_PTR(id); =20 @@ -7859,7 +7864,7 @@ int btf_prepare_func_args(struct bpf_verifier_env *en= v, int subprog) */ for (i =3D 0; i < nargs; i++) { u32 tags =3D 0; - int id =3D 0; + int id =3D btf->sorted_start_id > 0 ? btf->sorted_start_id - 1 : 0; =20 /* 'arg:' decl_tag takes precedence over derivation of * register type from BTF type itself @@ -9340,7 +9345,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, main_btf->sorted_start_id); if (IS_ERR(cands)) return ERR_CAST(cands); =20 @@ -9372,7 +9377,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, mod_btf->sorted_start_id); 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 f0ca69f888fa..2ae87075db6a 100644 --- a/kernel/bpf/verifier.c +++ b/kernel/bpf/verifier.c @@ -20655,12 +20655,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