From nobody Tue Dec 2 02:41:07 2025 Received: from mail-pl1-f173.google.com (mail-pl1-f173.google.com [209.85.214.173]) (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 20E532FDC29 for ; Wed, 19 Nov 2025 03:22:03 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.173 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1763522525; cv=none; b=HKqcP600dt9wdWJbgu8vZhxojwQKlaNypH/jWRylzO3ztnglmGK//mVjUQdLnUcvZCLzyZwaohpWkv71dG7zWgQT1fWeOFrdw8+iiq7cZ3i31w9W/EcLfFhJ4RBv5ADHI4R6ioDQW07hXJhRvVupRIyGbuW4lHvOuvFPDdowpyo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1763522525; c=relaxed/simple; bh=ZUKwweoi/8nzVzk7XZ/MU1Z+niGdx/B7murw68s/Bp4=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=KP17U07kp36j4qYqBe/XjzSHgIi4f5B8GwBXJD9Mwpl6b3jlyihiTHxa8ROsB2Ck65d+Ojw8GzkS895n/GuxRu4lMJbJe9I8cv3Kn9U/IAaSn9Mlzs60fyFd4FVp49atFsL75wv5e0CH1bth+bIzxxCsF5mq/3LUl8kgKdFcV7M= 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=NdGVl9Zs; arc=none smtp.client-ip=209.85.214.173 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="NdGVl9Zs" Received: by mail-pl1-f173.google.com with SMTP id d9443c01a7336-2955623e6faso57441025ad.1 for ; Tue, 18 Nov 2025 19:22:03 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1763522523; x=1764127323; 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=kUmHdLag/Zwi31gDv/2YF21TbKhBrlOl+Tx3LANhSGo=; b=NdGVl9ZsbbzDLLmFX26258QD7CmpTG/7tlEiiG5O/z8FeYhd3AlnkQknAlp+vPz5uB m5ouEedomubjMf6fslIsBRWESdnwiwld5G8pE/IlXeVQgRCArkEsafz+PIlrbYaq1faA gxj6koyT9AYzEItxchEwJ/5UgOGZXuTUF2mNcTXuVRGJgtr7y+rrWTv/EQ53VbP+EZna iysw0NnTwK0WmsTmdiMJ1zVCEUntWOtiXjj0lkGLrTQXlSy4ceKdJX+cTmWyCWySjY8n 9ysnWnXbFDSZZzJe8srS6LzQFsfTWCoTgVK1hAidPDI7TPOsAH7jn4PkXCetjUq5VgDJ zT9Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1763522523; x=1764127323; 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=kUmHdLag/Zwi31gDv/2YF21TbKhBrlOl+Tx3LANhSGo=; b=H0adMARW850ikcOERfZ+oCjYF18+kBzB1cmGDzr7QqYn9IyLwZLWZMZxjY9L56STMB 1biupn7wpz+CsYEAuK1QgUZEKozUv37qhncMEgHLuy9Rr+rtC68rDneJwDu/3qtko0VP l2I+yObtgTulrQafECDQInjwgr79JKYM96wdn6WRkBk6TQjlU9+tdQOFNV0PcaXVe6i5 6z6CrqmD/N7XL89ENEj90ze/D5ged2wIvfvXak6jZ2zsjZCTAUZ8UZ4zlCN/tlAVmtFi APj2FG7mxzyL6ho4uFExQ1FJmUccG8Xi0rleiY7GJ2evjK600UVjoFTmqxuO+Zv2nU/7 3nLA== X-Forwarded-Encrypted: i=1; AJvYcCWjeRsIWCx8zOTkIjYp9UTYd59hDJrPpn/tLBEkSk4MxqnrNLKcEICmbh0PNUUrrDpjeMHwIMQ4mIRSMTE=@vger.kernel.org X-Gm-Message-State: AOJu0YwBOC4Dn9f1XHFiL8ptHemKQO6IF7KfwsxOFGb/7vRST4ZluXg0 mVWiAMjlnW7z359beQ54NPK1zCYHT4Kj6TiXAvMGTZ/GHbnk8yp19vSp X-Gm-Gg: ASbGncsetLL39l8AfpoOBtaxMAuQ1MsXjVY3TW749wHsSPeQ7SQQ9EF9qjpNPjld/Et QBIolvF16DTgtPPY7cBdVxUgvpyOfFAuWXBoe5Y1ciLADApENflG/otE9MSe/bdaJa8af9czqcl 9j8nvpwSzgZkISYRqCyKRcYa+c57bVj605NzoNy0k2BoH89DFjPgq3aG74ON7bjl3SKCTXMPWsz f5vZetg8Wr50TL+alkgt+aSsAokVwE7mDrdVXUyTnVfBSqz6lU2sWYnUV3MBaaTme+/EtZhGy0q z8QfIykbPafNIlFoH5dTseTmB33ZpRlzVks69HSL0cFXvNVNhaIK4St7CmbK8OP1dAh6+7aJwIk p4sH/EJ9WyFlDDiPdE04dyNW5BK+/Uaw8a3IpYxoybKMH95bXjaFgrIXTXfHFgZinLIl9qKd/yt 3SFkg5L8LzcdVV4MuC6NxqOE9pIN8= X-Google-Smtp-Source: AGHT+IHpy1Aqpxs2EzkI3ifWZtaJJdDt7GcvJc8PdwkSqxfsWG9Ez6wqPI7Bjanrx6M+nXK35mM94Q== X-Received: by 2002:a17:903:2309:b0:298:34b:492c with SMTP id d9443c01a7336-2986a768210mr245315575ad.54.1763522523402; Tue, 18 Nov 2025 19:22:03 -0800 (PST) Received: from pengdl-pc.mioffice.cn ([43.224.245.249]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-2985c2bed5asm188352485ad.88.2025.11.18.19.22.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 18 Nov 2025 19:22:02 -0800 (PST) From: Donglin Peng To: ast@kernel.org, andrii.nakryiko@gmail.com Cc: eddyz87@gmail.com, zhangxiaoqin@xiaomi.com, linux-kernel@vger.kernel.org, bpf@vger.kernel.org, Donglin Peng , Alan Maguire , Song Liu Subject: [RFC PATCH v7 7/7] btf: Add sorting validation for binary search Date: Wed, 19 Nov 2025 11:15:31 +0800 Message-Id: <20251119031531.1817099-8-dolinux.peng@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20251119031531.1817099-1-dolinux.peng@gmail.com> References: <20251119031531.1817099-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: Donglin Peng Implement validation of BTF type ordering to enable efficient binary search for sorted BTF. Cc: Eduard Zingerman Cc: Alexei Starovoitov Cc: Andrii Nakryiko Cc: Alan Maguire Cc: Song Liu Cc: Xiaoqin Zhang Signed-off-by: Donglin Peng --- kernel/bpf/btf.c | 64 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) diff --git a/kernel/bpf/btf.c b/kernel/bpf/btf.c index 5dd2c40d4874..e9d102360292 100644 --- a/kernel/bpf/btf.c +++ b/kernel/bpf/btf.c @@ -550,6 +550,66 @@ u32 btf_nr_types(const struct btf *btf) return total; } =20 +/* Anonymous types (with empty names) are considered greater than named ty= pes + * and are sorted after them. Two anonymous types are considered equal. Na= med + * types are compared lexicographically. + */ +static int btf_compare_type_names(const void *a, const void *b, void *priv) +{ + struct btf *btf =3D (struct btf *)priv; + const struct btf_type *ta =3D btf_type_by_id(btf, *(__u32 *)a); + const struct btf_type *tb =3D btf_type_by_id(btf, *(__u32 *)b); + const char *na, *nb; + + if (!ta->name_off && tb->name_off) + return 1; + if (ta->name_off && !tb->name_off) + return -1; + if (!ta->name_off && !tb->name_off) + return 0; + + na =3D btf_name_by_offset(btf, ta->name_off); + nb =3D btf_name_by_offset(btf, tb->name_off); + return strcmp(na, nb); +} + +/* Verifies that BTF types are sorted in ascending order according to their + * names, with named types appearing before anonymous types. If the orderi= ng + * is correct, counts the number of named types and updates the BTF object= 's + * nr_sorted_types field. Note that vmlinux and kernel module BTFs are sor= ted + * during the building phase, so the validation logic only needs to count = the + * named types. + */ +static void btf_check_sorted(struct btf *btf) +{ + const struct btf_type *t; + int i, n, k =3D 0, nr_sorted_types; + bool skip_cmp =3D btf_is_kernel(btf); + + if (btf->nr_types < 2) + return; + + nr_sorted_types =3D 0; + n =3D btf_nr_types(btf) - 1; + for (i =3D btf_start_id(btf); i < n; i++) { + k =3D i + 1; + if (!skip_cmp && btf_compare_type_names(&i, &k, btf) > 0) + return; + + t =3D btf_type_by_id(btf, i); + if (t->name_off) + nr_sorted_types++; + else if (skip_cmp) + break; + } + + t =3D btf_type_by_id(btf, k); + if (t->name_off) + nr_sorted_types++; + if (nr_sorted_types) + btf->nr_sorted_types =3D nr_sorted_types; +} + static s32 btf_find_by_name_kind_bsearch(const struct btf *btf, const char= *name, s32 start_id, s32 end_id) { @@ -5885,6 +5945,8 @@ static struct btf *btf_parse(const union bpf_attr *at= tr, bpfptr_t uattr, u32 uat if (err) goto errout; =20 + btf_check_sorted(btf); + struct_meta_tab =3D btf_parse_struct_metas(&env->log, btf); if (IS_ERR(struct_meta_tab)) { err =3D PTR_ERR(struct_meta_tab); @@ -6292,6 +6354,7 @@ static struct btf *btf_parse_base(struct btf_verifier= _env *env, const char *name if (err) goto errout; =20 + btf_check_sorted(btf); refcount_set(&btf->refcnt, 1); =20 return btf; @@ -6426,6 +6489,7 @@ static struct btf *btf_parse_module(const char *modul= e_name, const void *data, } =20 btf_verifier_env_free(env); + btf_check_sorted(btf); refcount_set(&btf->refcnt, 1); return btf; =20 --=20 2.34.1