From nobody Mon Feb 9 03:45:40 2026 Received: from mail-pl1-f177.google.com (mail-pl1-f177.google.com [209.85.214.177]) (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 991F135CBD6 for ; Fri, 9 Jan 2026 13:00:29 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.177 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1767963631; cv=none; b=r3JvQhsdiqwGylI4fUF2Dd/ZbRU1X5BlvEyIOilxypU9Aj4TqESsUxQJleni7xH9soopetYIFnjWGuUEabDz5ExpSvlmgV5XqYD0N7Vi3DZiVka7meManHkwuffu7/Vf1wTKDkHTbZ2n4DczrQGUs991UALcHoQYY1N94b1GmRk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1767963631; c=relaxed/simple; bh=c745HJ4eBL9O6dcjNGxahlJgFWWLs7lhqs1RisTI0i8=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=k8fqCNPfmAjLXqnDVtPcBKh6pw/u2K9nQFyMmxXqaeR5BM/1Oyp6uwLQbo+2S7hLgenexrGJZP3ybkpPj+/v8jCBIxxTnRqa03bw1RFuivUtWklcjt3ezYv6+MLwsp3Ok5BOBV/Xju9EEiPoYxacPwwifePyFqxw1FmgUPndxoc= 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=OnFgMcuA; arc=none smtp.client-ip=209.85.214.177 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="OnFgMcuA" Received: by mail-pl1-f177.google.com with SMTP id d9443c01a7336-2a0bae9aca3so29662055ad.3 for ; Fri, 09 Jan 2026 05:00:29 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1767963629; x=1768568429; 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=QQrwHecK7iJFotsalH69RC9yNT/x39b0nH1K09oW1Js=; b=OnFgMcuAiDhCqKAnfRzu0ej65dFsrGOItfSUTbaRbxX9pZD+2SxqUGt1v9sYFUsUsQ BL/XEYMTEQcGhGTi9rZqQJxLC00bxUVGyGrUUFWsRjfKIZhs1VR37vG86xKiIJcpstHp Of7xxvZ+rQIRC+pfPEBUtk4EjiC07yhTEHL6MzQg+e4PuicBZ9xgtHKzdFxOk5geMgS6 r/XJfayqDZQNosJ9s6Ne1l6vhczQ+iKUUZzUVDxtfLwFznSip2zI2Ojwgw1+8EdASFoL sRcZG66J1QZujWLpSsm8tkNozmVwST3gHQ/AhioDkoe4vF2nO1nPGTaaXnOA1x8PMW/R W7pw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1767963629; x=1768568429; 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=QQrwHecK7iJFotsalH69RC9yNT/x39b0nH1K09oW1Js=; b=F5zIjIS2kcDMwG/QeVnsm9vdT+nF8sIxeJzCvVuTeejFQX6Jl2l5WnDeLAVyEvJNfT VmolklITo7SU/9KIkQX66SSt3y+J+Vcrfq9M/2MopX+L4PPf/Vc5u1azqACLz59PCr35 lqVFCretVvthLSekIBVTH0pT7DBeZjMsEug0renT3+yA8CxgKu8m9QnXLprS5Qh2RwsO 6eUl9jlQZNpwkEzbCUEaKV45pcdOxVAymOQsWhxhKyXBYKoWNiHG5Iy/6TxdmlWl3UCf dNbQ5YHh1PkCxA230zLXBsnIFvurajegQ8TQjtQ44LW0V4kgSbhXHIq0EmJ0N5d56/lG B/4g== X-Forwarded-Encrypted: i=1; AJvYcCWHancLxIMFGsP176PSgusLUDtJWUFwVhy0YlUsl6GT/2cFQDKPP3E99nFQQXVcRu/u720X8/kObfwP7dg=@vger.kernel.org X-Gm-Message-State: AOJu0YzsAaTIrtOBnjZYWkhk0+Aq9GiIW2rr9EEdcTddKgTavO77aIxM 7tzJKbHBwLGRFFeb9NmEXQEuRB/6K+OQMMwDwZ00PaP/L0a9ZpZDoCDU X-Gm-Gg: AY/fxX6pzWrXawkIP9JD0u2c2w3boZ3QWIAMTilriJHlALi53HFsFz/ZJ5UM0iDLN6k FG0tvob8Yw+7ID36B/GpYNQOqpLxyVVztAi9p8psBPsJ2rQJ+3KzuHcYvWpz+HmUg2ham5qK12/ x7hXtl+8ZiwMuhvezIzdx0q+NxbOrlxUwOfB5oHvbygiEXalscYXET2jTgA5CyBhFef+Cc/KRBy HP8hZAGuY2Cb3dqUgcf6YXkt1YAtawGIMH4T7RFAc5MVrA1xhUY5cufMIEH1ncmN+P9y1OMPpZl BsgxV/CaF6bXpcLv+z1iKNQRDaETFCIjbkqPExOHjOYtkdGLeh9Jd5zIBe0tQ3LE6QZ5YRQr2DB lJveOh8J7QhGAQOcTo9BzRf/WjcWT09RiJcMsqyxduxNBl58XiYEs2bSxEXfDrJ0UsUd8sS63If byl66QA+ZE2CNHEYc160m52NeqL6o= X-Google-Smtp-Source: AGHT+IH8r5Jl7OK/ko7OkHT0W07SkTpKr6K3Cb2AERKmBWRtYdKBRTbXr2RD8AFzYRrwDu33anvYvQ== X-Received: by 2002:a17:902:c402:b0:2a0:97d2:a265 with SMTP id d9443c01a7336-2a3ee452277mr89382755ad.14.1767963627294; Fri, 09 Jan 2026 05:00:27 -0800 (PST) Received: from pengdl-pc.mioffice.cn ([43.224.245.249]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-2a3e3c3a328sm104927325ad.4.2026.01.09.05.00.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 09 Jan 2026 05:00:26 -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, Donglin Peng , Alan Maguire Subject: [PATCH bpf-next v12 06/11] btf: Optimize type lookup with binary search Date: Fri, 9 Jan 2026 20:59:58 +0800 Message-Id: <20260109130003.3313716-7-dolinux.peng@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20260109130003.3313716-1-dolinux.peng@gmail.com> References: <20260109130003.3313716-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 Improve btf_find_by_name_kind() performance by adding binary search support for sorted types. Falls back to linear search for compatibility. Cc: Eduard Zingerman Cc: Alexei Starovoitov Cc: Andrii Nakryiko Cc: Alan Maguire Cc: Ihor Solodrai Cc: Xiaoqin Zhang Signed-off-by: Donglin Peng --- include/linux/btf.h | 1 + kernel/bpf/btf.c | 91 ++++++++++++++++++++++++++++++++++++++++----- 2 files changed, 83 insertions(+), 9 deletions(-) diff --git a/include/linux/btf.h b/include/linux/btf.h index 691f09784933..78dc79810c7d 100644 --- a/include/linux/btf.h +++ b/include/linux/btf.h @@ -219,6 +219,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_named_start_id(const struct btf *btf, bool own); 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 539c9fdea41d..d1f4b984100d 100644 --- a/kernel/bpf/btf.c +++ b/kernel/bpf/btf.c @@ -259,6 +259,7 @@ struct btf { void *nohdr_data; struct btf_header hdr; u32 nr_types; /* includes VOID for base BTF */ + u32 named_start_id; u32 types_size; u32 data_size; refcount_t refcnt; @@ -494,6 +495,11 @@ static bool btf_type_is_modifier(const struct btf_type= *t) return false; } =20 +static int btf_start_id(const struct btf *btf) +{ + return btf->start_id + (btf->base_btf ? 0 : 1); +} + bool btf_type_is_void(const struct btf_type *t) { return t =3D=3D &btf_void; @@ -544,21 +550,85 @@ u32 btf_nr_types(const struct btf *btf) return total; } =20 +/* btf_named_start_id - Get the named starting ID for the BTF + * @btf: Pointer to the target BTF object + * @own: Flag indicating whether to query only the current BTF (true =3D c= urrent BTF only, + * false =3D recursively traverse the base BTF chain) + * + * Return value rules: + * 1. For a sorted btf, return its named_start_id + * 2. Else for a split BTF, return its start_id + * 3. Else for a base BTF, return 1 + */ +u32 btf_named_start_id(const struct btf *btf, bool own) +{ + const struct btf *base_btf =3D btf; + + while (!own && base_btf->base_btf) + base_btf =3D base_btf->base_btf; + + return base_btf->named_start_id ?: (base_btf->start_id ?: 1); +} + +static s32 btf_find_by_name_kind_bsearch(const struct btf *btf, const char= *name) +{ + const struct btf_type *t; + const char *tname; + s32 l, r, m; + + l =3D btf_named_start_id(btf, true); + r =3D btf_nr_types(btf) - 1; + while (l <=3D r) { + m =3D l + (r - l) / 2; + t =3D btf_type_by_id(btf, m); + tname =3D btf_name_by_offset(btf, t->name_off); + if (strcmp(tname, name) >=3D 0) { + if (l =3D=3D r) + return r; + r =3D m; + } else { + l =3D m + 1; + } + } + + return btf_nr_types(btf); +} + s32 btf_find_by_name_kind(const struct btf *btf, const char *name, u8 kind) { + const struct btf *base_btf =3D btf_base_btf(btf); const struct btf_type *t; const char *tname; - u32 i, total; + s32 idx; =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; + if (base_btf) { + idx =3D btf_find_by_name_kind(base_btf, name, kind); + if (idx > 0) + return idx; + } =20 - tname =3D btf_name_by_offset(btf, t->name_off); - if (!strcmp(tname, name)) - return i; + if (btf->named_start_id > 0 && name[0]) { + idx =3D btf_find_by_name_kind_bsearch(btf, name); + for (; idx < btf_nr_types(btf); idx++) { + t =3D btf_type_by_id(btf, idx); + tname =3D btf_name_by_offset(btf, t->name_off); + if (strcmp(tname, name) !=3D 0) + return -ENOENT; + if (BTF_INFO_KIND(t->info) =3D=3D kind) + return idx; + } + } else { + u32 i, total; + + total =3D btf_nr_types(btf); + for (i =3D btf_start_id(btf); i < total; i++) { + t =3D btf_type_by_id(btf, i); + if (BTF_INFO_KIND(t->info) !=3D kind) + continue; + tname =3D btf_name_by_offset(btf, t->name_off); + if (strcmp(tname, name) =3D=3D 0) + return i; + } } =20 return -ENOENT; @@ -5791,6 +5861,7 @@ static struct btf *btf_parse(const union bpf_attr *at= tr, bpfptr_t uattr, u32 uat goto errout; } env->btf =3D btf; + btf->named_start_id =3D 0; =20 data =3D kvmalloc(attr->btf_size, GFP_KERNEL | __GFP_NOWARN); if (!data) { @@ -6210,6 +6281,7 @@ static struct btf *btf_parse_base(struct btf_verifier= _env *env, const char *name btf->data =3D data; btf->data_size =3D data_size; btf->kernel_btf =3D true; + btf->named_start_id =3D 0; snprintf(btf->name, sizeof(btf->name), "%s", name); =20 err =3D btf_parse_hdr(env); @@ -6327,6 +6399,7 @@ static struct btf *btf_parse_module(const char *modul= e_name, const void *data, btf->start_id =3D base_btf->nr_types; btf->start_str_off =3D base_btf->hdr.str_len; btf->kernel_btf =3D true; + btf->named_start_id =3D 0; snprintf(btf->name, sizeof(btf->name), "%s", module_name); =20 btf->data =3D kvmemdup(data, data_size, GFP_KERNEL | __GFP_NOWARN); --=20 2.34.1