From nobody Fri Dec 19 13:24:04 2025 Received: from mail-pj1-f48.google.com (mail-pj1-f48.google.com [209.85.216.48]) (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 EAAE731A815 for ; Thu, 18 Dec 2025 11:31:14 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.48 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1766057476; cv=none; b=eAAKTL5P5iXXY0eGKRISotil7DioCCcOB7CElj1VQFJoyiWEQ83i6SSEW62GOJ0HfGvMJ1ayFhb2xGdZ8uE0hlfadS08byBlHLnuv2BwMevYYADKRbLv5SzZsa4ZucesE4qKmkLID/w6OoQJGdtQ4QzdXeyBxN4PzaYCVyaiJ8E= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1766057476; c=relaxed/simple; bh=YhgJ9w2mpWwT1l163tIuzQE2TeqsSQofdQK+jrLBJY4=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=IW2GAXa/JUHMwIs7QdkCR/rnQV+xj38DMWJCqvBkpi7/jGAOSrbmYKPxKhx4Et65PJdG3ZSI8RZ8DDKI37YDyfapjkIhOm2Kc41/Rnh05B2Ftf+tKhrkBWwFxxNxXKjtn1tSIXAc2TW63LXNgWzc5rZofHNMzsUEUuyAzSvubzA= 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=DhTZH7Tc; arc=none smtp.client-ip=209.85.216.48 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="DhTZH7Tc" Received: by mail-pj1-f48.google.com with SMTP id 98e67ed59e1d1-34c363eb612so538053a91.0 for ; Thu, 18 Dec 2025 03:31:14 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1766057474; x=1766662274; 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=rwtrPyApu5OKLhu1m0R33rOrz3b0Od/pu+2hCHtjT5A=; b=DhTZH7TcsYXusbjuMLRU21DzRW6BfdD6IK3StTYGMgceGGzem+cwt8I6vl6JUpG4gS f8FWs19Ifhyn9bZlwTVs4DB6T20LLPyv41jC24E7HPBVtau8cY5zrRsTljwZBYpxeONf 8CRGgOmzc+KaczUdR6MT8L2DSZuAHJt7cvj26S3kaNjJAvdBPZP+gAu/tfUsZAPRpdYx MMCZkQju9ffNrAKDWszP20rZh7zuEHmbuWwaiPWQy3DGydNv60VMAZy9TRzX686J7VPr tZhKr7L1kPOJz519xp4MGatBqeG+frJLVmbgxUCsGpTYjYVO+UPt1WEELCyAxvk2DjrK pY/Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1766057474; x=1766662274; 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=rwtrPyApu5OKLhu1m0R33rOrz3b0Od/pu+2hCHtjT5A=; b=mX3N5sdhqO7+UwLzTL5QWUp6XtrvPA25ud0kvrriDbxq9uGIK0Sl3sOraUbzeKbT+b EpQvcjyc82KBAG0AEGNI7x2ksCDmFeH6yQIbx6uYAFOvvtRsSe9rpgUXIjsSasvBYexN If2Zn5XND5Nn0L0L/NB5F+ZcROacOiJ2GhVpSzvzD7X9s/kM7yyJme9sGBQZEzO1TvSr NqsylVoKo/hjMmWBMF7kBW9dzJFzKlWGfoVMgKiyokcl7FhhE31XL0uODCIDd9WC3Zoj L+GwGhVFUWWlfkR4QocKeEEegJ3GQHe+2FJloSfC+D5S+k6YSwwYxFTbGNd9xIr2YpH3 NJJA== X-Forwarded-Encrypted: i=1; AJvYcCWb6sOW/2Z1t7yHMLc0fGZgOjypFgIyP7vBjcMrqejpA3RhBTTqnqrZWUZrTK4oFs6xBCmAMDcPnXS0Vz4=@vger.kernel.org X-Gm-Message-State: AOJu0Yyu1NVRPbOYT326Wrm/6oSoSLVurwJDhDw4lWHucDN9u86KC0M5 gU+eiTJinOuqF2xpSwCp3BdRIv7i9kIxZ/tn1LKdEVAb8D8STn4bcJnl X-Gm-Gg: AY/fxX7WAs9YXpEvo9qViO2jdqCKtepXRla6JVbTYxt1MFsUvUWJyfAzz5x/casTOTg dQMmWTC5FgLxIGkyjADN0RvPqpkjvMzrWDYVOd+/BBfkpgKn/1UTAmImztje31U8rKG/DMLCRaz fMku1oPXa0JBzGLI7jK9DF5RG8Qh8JfzRk1KKqSyy/Z6EN6vW6N4kEl7oA92SWvof7A9iMSwJhF z7gUWbDZmkeChG0QGYLd07kVTzjcLHZqjOU2aNULyHA2NGGmWjbyFfVvvrVu9w9B54ZQa8B67/0 lePZZuRKVXPwSAFki3SNTqYScpcB/Eqeqgc2HK3zpDV3AhzeGxf2naooGo5RX8HZnHqnu4FPQHw iJvLhJar0fGeivx/tTAdekP7HsZgqUkw1IzL+KmEBSSIXYSbSx00gq6jQ862j42py8bNRd7VvSk sYYn9502jMVvPVih6sM1tMf54WWtg= X-Google-Smtp-Source: AGHT+IFVQfIxDi/VYL5j2krPuLS3c9hBg9Z2+cMwNmaB0ZeP+5lhhD1BnA4xectZ75VE1Cja4zJDGA== X-Received: by 2002:a17:90b:270a:b0:340:b912:536 with SMTP id 98e67ed59e1d1-34abd77f7cbmr16495764a91.31.1766057474127; Thu, 18 Dec 2025 03:31:14 -0800 (PST) Received: from pengdl-pc.mioffice.cn ([43.224.245.249]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-34e70d4f887sm2328237a91.3.2025.12.18.03.31.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 18 Dec 2025 03:31:12 -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, pengdonglin , Alan Maguire Subject: [PATCH bpf-next v10 01/13] libbpf: Add BTF permutation support for type reordering Date: Thu, 18 Dec 2025 19:30:39 +0800 Message-Id: <20251218113051.455293-2-dolinux.peng@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20251218113051.455293-1-dolinux.peng@gmail.com> References: <20251218113051.455293-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 Acked-by: Eduard Zingerman --- 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 b136572e889a..ab204ca403dc 100644 --- a/tools/lib/bpf/btf.c +++ b/tools/lib/bpf/btf.c @@ -5887,3 +5887,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..5d560571b1b5 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. + * + * @id_map must include all types from ID `start_id` to `btf__type_cnt(btf= ) - 1`. + * @id_map_cnt should be `btf__type_cnt(btf) - start_id` + * The mapping is defined as: `id_map[original_id - start_id] =3D new_id` + * + * For base BTF, its `start_id` is fixed to 1, i.e. the VOID type can + * not be redefined or remapped and its ID is fixed to 0. + * + * For split BTF, its `start_id` can be retrieved by calling + * `btf__type_cnt(btf__base_btf(btf))`. + * + * 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 84fb90a016c9..d18fbcea7578 100644 --- a/tools/lib/bpf/libbpf.map +++ b/tools/lib/bpf/libbpf.map @@ -453,4 +453,5 @@ LIBBPF_1.7.0 { bpf_map__exclusive_program; bpf_prog_assoc_struct_ops; bpf_program__assoc_struct_ops; + btf__permute; } LIBBPF_1.6.0; --=20 2.34.1