From nobody Tue Dec 2 00:01:08 2025 Received: from mail-pf1-f172.google.com (mail-pf1-f172.google.com [209.85.210.172]) (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 71E533164A9 for ; Wed, 26 Nov 2025 08:50:36 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.172 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1764147038; cv=none; b=Erl/PbhCrsd7g4qqkLUy3igB6eaghRQSCj/oRcCSjynxtvlqPnmECgkJw/9QnZMsKvZ+tSQ90BMV5OkWhNhNiaUgbcU0PuRfNehSnIQ2CnPRKK7rT+lsEIRL/4dwdzU1Jqi+e4irXs1szAMb03S05cxgpbPqGdBDtm5e1okVt5s= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1764147038; c=relaxed/simple; bh=YJNPYvfz/GxoZXv79UmIYENyuKk2IR71wtQ8M88Szf8=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=tWYsaDrXuRtKDANIf0S6AwbwKNcb9rBbboRtJPlid+IjvzZiNfTvvnqqXBrnjsI7hXjtOX0ACF/TSgNXBqnFB0S55ASLg141Y7jE2f7QQxY4z5LtzG0C76v/zTLupAdwiFxaUKcA5wsNCdUO4LG3m+69HrKlc/QAnxYyUE5oVvk= 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=LMenimB+; arc=none smtp.client-ip=209.85.210.172 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="LMenimB+" Received: by mail-pf1-f172.google.com with SMTP id d2e1a72fcca58-7bb710d1d1dso9809709b3a.1 for ; Wed, 26 Nov 2025 00:50:36 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1764147036; x=1764751836; 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=a4S21SvMIVWAPa44TWVxWwB/nChiaz6KYgp24im87ZU=; b=LMenimB+wyVQQmyBG71lF/9Nv1Wuudu14lRXLsJ0OQh2Q7VeAMO3c9bPrRzcPhUPlJ pUwDVSyf4l96H50Asxl8MeLcn/GoCspflqPB62WIt8BvjaqdD2J227lrNw00wBM4XKB9 TzRvvuRNZVyuYtDC66B+BtZwrsTieqg684OL4pM5GW3YcGMe606WPNfHZ+/szs6uDN9R wVHuEg/N+qwZcscxw0RvPRWXfUhKNBGVsZI+/1BXGFxbcUYxtmkgx122lAkVX2589ScJ SB0UxIHmjKycruGvjo0Ax2+v7UeaLEWrPBeNOl3VKflxxEmbOnqOihjN0jkq3jBnyWZW KcuQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1764147036; x=1764751836; 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=a4S21SvMIVWAPa44TWVxWwB/nChiaz6KYgp24im87ZU=; b=FOdJiwxRUbn7cTlLjwL1339jlOeLvMlQVZpLdUDLI4FSPhKWqdlPZjRGqE6Nnsg8qj za75Me43zYffPXLujiJnl463XgS5dV5PR1d8HEc/OCHCXY7jh6+y7EIOayNHTjGu1CoV 1dMQa5sARPZXjZQ+cOzIKjZzofunRJkL9IMQyRj6LUkpOzg+Rj4rAmVTDpD1iFWkUDmm ZgKYx+BfMCoPTkPicESJrgd7Tv1BkUW/CS58H1MsP3JlJ6VAwW9fnLBgrqRGSE+29Qvz gJF4NGS1eooYDMMw55EtNR5qEMrH1ktI4X901pHFz3b80sZ1+3GVvimrEm+hYukqCjJW tFiA== X-Forwarded-Encrypted: i=1; AJvYcCU6uQGlxz7TSAhbeZ5KoaHbk6FQaUDm+FWZv372QZHdhvWE9sMAkZGAs+9A39nPyHf/NI3/12CEbZRsmq0=@vger.kernel.org X-Gm-Message-State: AOJu0YzUwJo6CbQu+XKD8K910monW+cwY/ujuszrcalk1uizsuT2A2p8 K8t3pMmSLwWMrOQtjZ7uNKsp+FzCeJbtjvZDkfwTB/lu7rICmr1BtDpz X-Gm-Gg: ASbGnctzq42AuyH1wf34DeO54QiXpcx/JJkLPnWDoQ8xtBDedlibEs1KQ0DmvM1O8vv H9ojn+YTNFlPTkgMvFz3JuZX0D0FU96KuvpARYbZ0K7skML1hVwjckCVAkJJZ+7Gx0rhAbWb9IY 1aII2HHlewyvxcqmd/HXuoi18O+CCkTpaeAP8Wz2owOo8e0fc+H8nddcxQlRXfi9VTmHa4/i1xO DeoikoXmZsrIR3M/zAKxrue2T0JLK+QSBtvqTd7aLFRJaim4ZtkLVk0itLKNKBfsXQxgHpnrd4B Y3Gfr/vsDlnaGuXxbKfM3gWnZyenZ9Fd9A/UT5eRbjiEThKDmjBm99XV6yyS3whqjLcQOMRRx8k N8elOJdOOAABEHIdgGX5lKRp/eA3/+laU+O7nTVQBTc3+dMTm2gl3lOuCSPewsYB4n4Kh1ihuXg nbPCiTzzb0yJ+LiwApvzdcWKWhNic= X-Google-Smtp-Source: AGHT+IHKWn9NWkYUvRb9WwqZogORsd/GfhCKitXLaO71Cj/o/bBaypLjOcJ6zUVdxpMAoqry3FCk4Q== X-Received: by 2002:a05:6a00:b53:b0:7ab:5e68:e204 with SMTP id d2e1a72fcca58-7ca8a043bb8mr6202036b3a.29.1764147035605; Wed, 26 Nov 2025 00:50:35 -0800 (PST) Received: from pengdl-pc.mioffice.cn ([43.224.245.249]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-7c3f023fd82sm20885721b3a.42.2025.11.26.00.50.32 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 26 Nov 2025 00:50:34 -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: [RFC bpf-next v8 1/9] libbpf: Add BTF permutation support for type reordering Date: Wed, 26 Nov 2025 16:50:17 +0800 Message-Id: <20251126085025.784288-2-dolinux.peng@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20251126085025.784288-1-dolinux.peng@gmail.com> References: <20251126085025.784288-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 Introduce btf__permute() API to allow in-place rearrangement of BTF types. This function reorganizes BTF type order according to a provided array of type IDs, updating all type references to maintain consistency. Cc: Eduard Zingerman Cc: Alexei Starovoitov Cc: Andrii Nakryiko Cc: Alan Maguire Cc: Ihor Solodrai Cc: Xiaoqin Zhang Signed-off-by: pengdonglin --- tools/lib/bpf/btf.c | 119 +++++++++++++++++++++++++++++++++++++++ tools/lib/bpf/btf.h | 36 ++++++++++++ tools/lib/bpf/libbpf.map | 1 + 3 files changed, 156 insertions(+) diff --git a/tools/lib/bpf/btf.c b/tools/lib/bpf/btf.c index 84a4b0abc8be..26ebc0234b9b 100644 --- a/tools/lib/bpf/btf.c +++ b/tools/lib/bpf/btf.c @@ -5868,3 +5868,122 @@ int btf__relocate(struct btf *btf, const struct btf= *base_btf) btf->owns_base =3D false; return libbpf_err(err); } + +struct btf_permute { + struct btf *btf; + __u32 *id_map; +}; + +/* Callback function to remap individual type ID references */ +static int btf_permute_remap_type_id(__u32 *type_id, void *ctx) +{ + struct btf_permute *p =3D ctx; + __u32 new_type_id =3D *type_id; + + /* refer to the base BTF or VOID type */ + if (new_type_id < p->btf->start_id) + return 0; + + if (new_type_id >=3D btf__type_cnt(p->btf)) + return -EINVAL; + + *type_id =3D p->id_map[new_type_id - p->btf->start_id]; + return 0; +} + +int btf__permute(struct btf *btf, __u32 *id_map, __u32 id_map_cnt, + const struct btf_permute_opts *opts) +{ + struct btf_permute p; + struct btf_ext *btf_ext; + void *nt, *new_types =3D NULL; + __u32 *order_map =3D NULL; + int err =3D 0, i; + __u32 id; + + if (!OPTS_VALID(opts, btf_permute_opts) || id_map_cnt !=3D btf->nr_types) + return libbpf_err(-EINVAL); + + /* record the sequence of types */ + order_map =3D calloc(id_map_cnt, sizeof(*id_map)); + if (!order_map) { + err =3D -ENOMEM; + goto done; + } + + new_types =3D calloc(btf->hdr->type_len, 1); + if (!new_types) { + err =3D -ENOMEM; + goto done; + } + + if (btf_ensure_modifiable(btf)) { + err =3D -ENOMEM; + goto done; + } + + for (i =3D 0; i < id_map_cnt; i++) { + id =3D id_map[i]; + if (id < btf->start_id || id >=3D btf__type_cnt(btf)) { + err =3D -EINVAL; + goto done; + } + id -=3D btf->start_id; + /* cannot be mapped to the same ID */ + if (order_map[id]) { + err =3D -EINVAL; + goto done; + } + order_map[id] =3D i + btf->start_id; + } + + p.btf =3D btf; + p.id_map =3D id_map; + nt =3D new_types; + for (i =3D 0; i < id_map_cnt; i++) { + struct btf_field_iter it; + const struct btf_type *t; + __u32 *type_id; + int type_size; + + id =3D order_map[i]; + t =3D btf__type_by_id(btf, id); + type_size =3D btf_type_size(t); + memcpy(nt, t, type_size); + + /* fix up referenced IDs for BTF */ + err =3D btf_field_iter_init(&it, nt, BTF_FIELD_ITER_IDS); + if (err) + goto done; + while ((type_id =3D btf_field_iter_next(&it))) { + err =3D btf_permute_remap_type_id(type_id, &p); + if (err) + goto done; + } + + nt +=3D type_size; + } + + /* fix up referenced IDs for btf_ext */ + btf_ext =3D OPTS_GET(opts, btf_ext, NULL); + if (btf_ext) { + err =3D btf_ext_visit_type_ids(btf_ext, btf_permute_remap_type_id, &p); + if (err) + goto done; + } + + for (nt =3D new_types, i =3D 0; i < id_map_cnt; i++) { + btf->type_offs[i] =3D nt - new_types; + nt +=3D btf_type_size(nt); + } + + free(order_map); + free(btf->types_data); + btf->types_data =3D new_types; + return 0; + +done: + free(order_map); + free(new_types); + return libbpf_err(err); +} diff --git a/tools/lib/bpf/btf.h b/tools/lib/bpf/btf.h index cc01494d6210..ba67e5457e3a 100644 --- a/tools/lib/bpf/btf.h +++ b/tools/lib/bpf/btf.h @@ -281,6 +281,42 @@ LIBBPF_API int btf__dedup(struct btf *btf, const struc= t btf_dedup_opts *opts); */ LIBBPF_API int btf__relocate(struct btf *btf, const struct btf *base_btf); =20 +struct btf_permute_opts { + size_t sz; + /* optional .BTF.ext info along the main BTF info */ + struct btf_ext *btf_ext; + size_t :0; +}; +#define btf_permute_opts__last_field btf_ext + +/** + * @brief **btf__permute()** performs in-place BTF type rearrangement + * @param btf BTF object to permute + * @param id_map Array mapping original type IDs to new IDs + * @param id_map_cnt Number of elements in @id_map + * @param opts Optional parameters for BTF extension updates + * @return 0 on success, negative error code on failure + * + * **btf__permute()** rearranges BTF types according to the specified ID m= apping. + * The @id_map array defines the new type ID for each original type ID. + * + * For **base BTF**: + * - @id_map must include all types from ID 1 to `btf__type_cnt(btf)-1` + * - @id_map_cnt should be `btf__type_cnt(btf) - 1` + * - Mapping uses `id_map[original_id - 1] =3D new_id` + * + * For **split BTF**: + * - @id_map should cover only split types + * - @id_map_cnt should be `btf__type_cnt(btf) - btf__type_cnt(btf__base_b= tf(btf))` + * - Mapping uses `id_map[original_id - btf__type_cnt(btf__base_btf(btf))]= =3D new_id` + * + * On error, returns negative error code and sets errno: + * - `-EINVAL`: Invalid parameters or ID mapping (duplicates, out-of-ran= ge) + * - `-ENOMEM`: Memory allocation failure + */ +LIBBPF_API int btf__permute(struct btf *btf, __u32 *id_map, __u32 id_map_c= nt, + const struct btf_permute_opts *opts); + struct btf_dump; =20 struct btf_dump_opts { diff --git a/tools/lib/bpf/libbpf.map b/tools/lib/bpf/libbpf.map index 8ed8749907d4..b778e5a5d0a8 100644 --- a/tools/lib/bpf/libbpf.map +++ b/tools/lib/bpf/libbpf.map @@ -451,4 +451,5 @@ LIBBPF_1.7.0 { global: bpf_map__set_exclusive_program; bpf_map__exclusive_program; + btf__permute; } LIBBPF_1.6.0; --=20 2.34.1