From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pf1-f174.google.com (mail-pf1-f174.google.com [209.85.210.174]) (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 786932836B1 for ; Thu, 18 Dec 2025 21:43:08 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.174 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1766094193; cv=none; b=ZTs2JJWky3kfC3LWiBWI0NQk0PugyFUKLn/dp0pfSKZ4ubyOskVcdOWjEYeqlcqnRKqwXUQPN/2GCT4y5Jcl4+28YNGXqfvD7q60vYQe6a1K2baut4fKwD+AILnHpI7SIXrDRV2DCMA6MrQKJdmz/rKqt8ls4OOG1cP/t7K7+Xc= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1766094193; c=relaxed/simple; bh=5xoJ+FAwmvBBOlQ1HBxPlbDwjy7kb9lj2INfUmDlOss=; h=Message-ID:Subject:From:To:Cc:Date:In-Reply-To:References: Content-Type:MIME-Version; b=NgBJQSVFm+YQNMKI1qnJ5P2RWm3nP6TtKiFKtd53Yi9ET7vtFFmkXZvjlp2y+SQdg/pkR6MsYW+mnH8GzXYlqI3Acq89+WV17rVdqdnjCxu8YasRgwfmVtF96y+B8+0d1iaKY6vjPIGIEFkMfiFUb7xRTOcI1neY9F0Y7tiWjbk= 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=hqpiXNRJ; arc=none smtp.client-ip=209.85.210.174 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="hqpiXNRJ" Received: by mail-pf1-f174.google.com with SMTP id d2e1a72fcca58-7aab061e7cbso1625262b3a.1 for ; Thu, 18 Dec 2025 13:43:07 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1766094184; x=1766698984; darn=vger.kernel.org; h=mime-version:user-agent:content-transfer-encoding:references :in-reply-to:date:cc:to:from:subject:message-id:from:to:cc:subject :date:message-id:reply-to; bh=zIi7zvTPASA4DYIZ2FET2r2wmNcVdbNKGGGML9Mr4dM=; b=hqpiXNRJIQBUeJ3ShR1B0owwEv/o2ed7Bg/LOfIPJkliDCxEqDLU+Ahf7QcHkIteRI ghzzFPvAyHjnbDODCT2rmY3Z8cVf/JIH5mgyzAYrX/+AoM+trCG2BnNyiNyIck5yaE2K T9jsf1SlqaFwWwf0BS5/cDTiuma89UASTVgQeRlXQfg0qXpunOMffuNZHyt1/X1N/DMW 5dKH6lLKIheF1IroRRfrccU5w0S8pcooc8es1P3sXyCY69Fp6ndpwScj/GSGeP18vK+Y 9NiCThgNwH7N5umlPFw7Mr0l6cNCxkq2WUxtUiBoKyhzuYqn0OUwRrrGIKoQAZ3rEsJ0 7fwQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1766094184; x=1766698984; h=mime-version:user-agent:content-transfer-encoding:references :in-reply-to:date:cc:to:from:subject:message-id:x-gm-gg :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=zIi7zvTPASA4DYIZ2FET2r2wmNcVdbNKGGGML9Mr4dM=; b=US7BUUFToYzfPOQifJpDIp+9zMMddPyByo0P3KFzGCUBtAIVds1bdstxrqpvSIAqsx 4jZ/B0v9oJMz5aI6zMNYjmX8vPcrV5jlP6XLLj39/qMr2DmftXWsm3xjYAFQZuDlTEWx vxMp9MZOGs/qaMX3JwfeG2FftB7EXb2TiozL34cYyNWN4+5oylvET1wuITK36uEOQK+a 19gZRJgZGR1fSPrvLC5FX/i13OJtAL/2X74+s1YPFtD4UT2U+ayem/JpcGNL25pkK4w9 D7U9iIYeilkSEC7PeWU1DMLwMKOyYexk9o4iu7EE2+62LuQQCFb38OtNFzrnzK8jWO0l uoXQ== X-Forwarded-Encrypted: i=1; AJvYcCWfznj9eeObZWyscZ0htCuos2fHseBwBMHPqBCDlhD+c2VmOf97ZwuVVG2vrFl2QMs7i5Y/LWJq+j7CNeg=@vger.kernel.org X-Gm-Message-State: AOJu0YxBNPZkZvanzE5TUYtIJJedE106SLdH94wktXU0GY+XP/eOBdbD 7zxdhoU/UiDm6yAOhEsq/Aj7saUfh4MGbzTU6xZdjzZQnZlQqgI6Olspo+d7EpMcqcg= X-Gm-Gg: AY/fxX6qZ36We6+LXueWWT0nda83ZHy4zwmIAtNqBYLyB+eHOcTwn3khDLXKBad9Cqa XHqyp1hwMp6hm/sAS9CcgWGndRrQs9aJNEIyJDidiee1ycATqgTPkv2nevlcB3Y8gpCglbsXIQx tmG8EFKb/D1srUS0J8PAQ/QIr8R2jmRqh2v5wNpG3BUtANwR5EG6NaBQ9IcMrk7wTFNcoQ0Jd8y WqPaPHRqoGzE37s/FKk73Aop+6+kmJu1hRmOkwV6Q32I9R2rFPw1gMqAxNTaVQb8Dbdolf6FQhA EgLSbomRo2B0uCrRrpRoh0+vNnJn+c3awplSKs0On3E3q5ERQhdaI8COKy7oRWgFtb8NlQiFQ15 6VAuBKu3lbAStPfHSML0wtjB1L8JUK0t5Aynct7q7TCH3/f3sXDByGnK+CjwC/Sa7in+y3KYSvL K6UXbRIrQol2MpDCSHuyTl/DMkkSCeW+jdJEO/ X-Google-Smtp-Source: AGHT+IF6PosctMUD6swP6NPgKPI4DdVv2r80djgQpA1bMjDo9jebIdYS3EJsb2g0nGmwkAoHidG8nw== X-Received: by 2002:a05:7022:985:b0:11b:a8e3:8468 with SMTP id a92af1059eb24-121722fd206mr785123c88.33.1766094184436; Thu, 18 Dec 2025 13:43:04 -0800 (PST) Received: from ?IPv6:2a03:83e0:115c:1:4779:aa2b:e8ff:52c4? ([2620:10d:c090:500::5:3eff]) by smtp.gmail.com with ESMTPSA id a92af1059eb24-1217243bbe3sm1708148c88.0.2025.12.18.13.43.03 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 18 Dec 2025 13:43:04 -0800 (PST) Message-ID: <1a9c0b5c22b2ecd8a8df531b1ed9441ab926415b.camel@gmail.com> Subject: Re: [PATCH bpf-next v10 07/13] btf: Verify BTF Sorting From: Eduard Zingerman To: Donglin Peng , ast@kernel.org, andrii.nakryiko@gmail.com Cc: zhangxiaoqin@xiaomi.com, ihor.solodrai@linux.dev, linux-kernel@vger.kernel.org, bpf@vger.kernel.org, pengdonglin , Alan Maguire Date: Thu, 18 Dec 2025 13:43:02 -0800 In-Reply-To: <20251218113051.455293-8-dolinux.peng@gmail.com> References: <20251218113051.455293-1-dolinux.peng@gmail.com> <20251218113051.455293-8-dolinux.peng@gmail.com> Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable User-Agent: Evolution 3.56.2 (3.56.2-2.fc42) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 On Thu, 2025-12-18 at 19:30 +0800, Donglin Peng wrote: > From: pengdonglin >=20 > This patch checks whether the BTF is sorted by name in ascending order. > If sorted, binary search will be used when looking up types. >=20 > Specifically, vmlinux and kernel module BTFs are always sorted during > the build phase with anonymous types placed before named types, so we > only need to identify the starting ID of named types. >=20 > Cc: Eduard Zingerman > Cc: Alexei Starovoitov > Cc: Andrii Nakryiko > Cc: Alan Maguire > Cc: Ihor Solodrai > Cc: Xiaoqin Zhang > Signed-off-by: pengdonglin > --- Lgtm, but please take a look at a few nits. > kernel/bpf/btf.c | 56 ++++++++++++++++++++++++++++++++++++++++++++++++ > 1 file changed, 56 insertions(+) >=20 > diff --git a/kernel/bpf/btf.c b/kernel/bpf/btf.c > index 0394f0c8ef74..a9e2345558c0 100644 > --- a/kernel/bpf/btf.c > +++ b/kernel/bpf/btf.c > @@ -550,6 +550,60 @@ u32 btf_nr_types(const struct btf *btf) > return total; > } > =20 > +/* > + * Assuming that types are sorted by name in ascending order. > + */ > +static int btf_compare_type_names(u32 *a, u32 *b, const struct btf *btf) Nit: no need for 'a' and 'b' to be pointers. > +{ > + const struct btf_type *ta =3D btf_type_by_id(btf, *a); > + const struct btf_type *tb =3D btf_type_by_id(btf, *b); > + const char *na, *nb; > + > + na =3D btf_name_by_offset(btf, ta->name_off); > + nb =3D btf_name_by_offset(btf, tb->name_off); > + return strcmp(na, nb); > +} > + > +/* Note that vmlinux and kernel module BTFs are always sorted > + * during the building phase. > + */ > +static void btf_check_sorted(struct btf *btf) > +{ > + const struct btf_type *t; > + u32 sorted_start_id; > + u32 i, n, k; > + > + if (btf_is_kernel(btf) && !btf_is_module(btf)) { ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Nit: there is a btf_is_vmlinux() helper, which does the same thing. > + for (i =3D btf_start_id(btf); i < n; i++) { > + t =3D btf_type_by_id(btf, i); > + if (t->name_off) { > + btf->sorted_start_id =3D i; > + return; > + } > + } Nit: return here? > + } > + > + if (btf->nr_types < 2) > + return; > + > + sorted_start_id =3D 0; > + n =3D btf_nr_types(btf); > + for (i =3D btf_start_id(btf); i < n; i++) { > + k =3D i + 1; > + if (k < n && btf_compare_type_names(&i, &k, btf) > 0) > + return; > + > + if (sorted_start_id =3D=3D 0) { > + t =3D btf_type_by_id(btf, i); > + if (t->name_off) > + sorted_start_id =3D i; > + } > + } > + > + if (sorted_start_id) > + btf->sorted_start_id =3D sorted_start_id; > +} > + > static s32 btf_find_by_name_bsearch(const struct btf *btf, const char *n= ame, > s32 start_id, s32 end_id) > { > @@ -6296,6 +6350,7 @@ static struct btf *btf_parse_base(struct btf_verifi= er_env *env, const char *name > if (err) > goto errout; > =20 > + btf_check_sorted(btf); > refcount_set(&btf->refcnt, 1); > =20 > return btf; > @@ -6430,6 +6485,7 @@ static struct btf *btf_parse_module(const char *mod= ule_name, const void *data, > } > =20 > btf_verifier_env_free(env); > + btf_check_sorted(btf); > refcount_set(&btf->refcnt, 1); > return btf; > =20