From nobody Sun Feb 8 16:50:28 2026 Received: from mail-pf1-f178.google.com (mail-pf1-f178.google.com [209.85.210.178]) (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 B357E1FF5E3 for ; Thu, 8 Jan 2026 03:16:56 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.178 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1767842218; cv=none; b=FlgUsj7ZYrDV8l/cGp7t8Ii4220aI9z6mFbRHkRrw0OogWyanFdAtBCjKXmwUTEiBhVI1dxoZWoQwBOR2sekERpoKmqPZH5mtm7pEbl73AVY55cHV75OFV4drbUo1Ep4PEErj9iHveIyuRUAY2bAygsYpi2FJtThW2R+pnfkY7o= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1767842218; c=relaxed/simple; bh=ksqELPO0cXKauiYwTecfnPaKtxldFgV/kANERx4D1IY=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=Asn8NhQQ6TYU0T/TQCDNDKFpzM6at0EKUd5RriLnpTROsljZJEKTcvLf0oLTRVpmcYeyTZfBJNy/RJVTTgRdAeRq9lvf3c2veFJMWv6uU8e3KaahbVl0j6iqnUGAAXOC95n9619rP+VsIIOOszLoOIui78021/YjvHVDF2nuVMA= 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=KGBBOouk; arc=none smtp.client-ip=209.85.210.178 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="KGBBOouk" Received: by mail-pf1-f178.google.com with SMTP id d2e1a72fcca58-7ba55660769so1637793b3a.1 for ; Wed, 07 Jan 2026 19:16:56 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1767842216; x=1768447016; 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=PQF9Y2EREf+dgAVABdiiMRWyZAZapN+hexHBR/LqK4M=; b=KGBBOoukJlESMM5DeiT9zqoi9PHV8qR8FU9VSHoScQVtsdqoLxMyBjn8uPLU5r1Z8+ 89uhIf2gKHJSoVkQJLeLYrVZhQnDhKGDXyWfNagRXyg2g6On6g8Nlqj2Xndf8HE+C27P Sqewzf7eGsqCrTWIP8WgU5ZMaOieh2PdoJFywof1xbxxAo+t6ZIYUZUerijqF99vaEBO IkUJf9Pr48iNSYYUKkWVc9Sgrn8trHjw2ZsCz2dRlq2gDIEmx4Y5TuKa2uRgosF+q0I3 Oc0lt7VrMUaXzhc30rw/LAHvzn+5XEnaQO4tas1bdlCI8AADXT4pqv1vuruo7669OL3B 9hEg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1767842216; x=1768447016; 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=PQF9Y2EREf+dgAVABdiiMRWyZAZapN+hexHBR/LqK4M=; b=LSKD8vA6WoIeXuW9ROVnxLm0RfVVpO/QLNmUq3m5lVNK3xU/V6Brl+TFd34fMMeZwu QwhIHkR5FYs5z/inpoXYIsWnCC31gjt24BLmPaFHNV1RY0swjAZMoTw9n/NyWm4pJGuY i+C0CKKhaLDjGDt9D7Cp2N4PFWqW9aCCYQm0RjE5TO9KH9HBifsQ3TfEOHDrystPpfTQ F7jQVCnFGixajeYDRomje8tOoOTtT6/haWqZ4aORL3W6JVb+rMOFau/Uwnc1YqgwykyP 7GoHAMB4aok3EhvTOJcKjILSaqxhNaeR/wpzRpIu8Losp2SR0+N8rGCqaM/BbTI/0vNX dhnA== X-Forwarded-Encrypted: i=1; AJvYcCWuP9g5bq3cc0qywAdF9qTasQJuPKV2iRpHxIgcQdgqLgA5wmh2tmizltbarAM0jAQ3tz7hW1e5YqAA/GU=@vger.kernel.org X-Gm-Message-State: AOJu0YwprMsYe4V1VCOUTB0pNdNOfuYLOTzzecFZi4TkdUE5ledZGrAk qgXbm0nWiVWWlUluzobUu9VKRwqkXW+O9dwkKOAi70PMRnJUA4t6F7Dx X-Gm-Gg: AY/fxX5F8QXB9Nbc7yb8LtlYQ3zaA2hWgwKjcG+6gPjO3gvSoQoolKBOYtbAntSDUSZ ms0Ifex5B5Sf2E5IprL8fGzzG49q2Tx95BIpcUcFWqvxUxSq/rpXB7i4NRXsKfw67eM4EdiA618 IexgLfffJhGkHa/bIbWqlar6TKjsM2ADUwjBFQh+OUuUyGl8lZEfOeCDg0SGX7+MP2uJpowuDHY 1uCQEcd7lN0KdZFXmA1JNMsdm78KYvcW6c/3pYNo/Q9h80JQ4suGCT/Gjw4pJELEmxJnfCS87cl hz2+oGWi5E14b/D2w7uuhnIdZI/IDdvxDutgiPwiv2Zj0ZvwMr/0plJH4Yw2fpPU0zcxFfm3sCB Ka8zXn8X23fsL86yllZkMftXX2UFlUXISrS3+EMvytFpPOmCL7EvYX9InDIqYsQLNRGyZdd1hYN g4AzKRcUVcN232AlZOGymEXea0Qqs= X-Google-Smtp-Source: AGHT+IHxJx1chwNx2IMHnP6vHObc0mE/s4CGIeE8/R0IOP6ROUhqtKdhAwZnrXNr1L4jq8gp1O7kbw== X-Received: by 2002:a05:6a00:3019:b0:819:6dbc:caf6 with SMTP id d2e1a72fcca58-81b7f101badmr3643122b3a.44.1767842215894; Wed, 07 Jan 2026 19:16:55 -0800 (PST) Received: from pengdl-pc.mioffice.cn ([43.224.245.249]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-819c5de655bsm6134860b3a.60.2026.01.07.19.16.53 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 07 Jan 2026 19:16:55 -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 v11 01/11] libbpf: Add BTF permutation support for type reordering Date: Thu, 8 Jan 2026 11:16:35 +0800 Message-Id: <20260108031645.1350069-2-dolinux.peng@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20260108031645.1350069-1-dolinux.peng@gmail.com> References: <20260108031645.1350069-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 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: Donglin Peng Acked-by: Eduard Zingerman --- tools/lib/bpf/btf.c | 133 +++++++++++++++++++++++++++++++++++++++ tools/lib/bpf/btf.h | 42 +++++++++++++ tools/lib/bpf/libbpf.map | 1 + 3 files changed, 176 insertions(+) diff --git a/tools/lib/bpf/btf.c b/tools/lib/bpf/btf.c index b136572e889a..bf75f770d29a 100644 --- a/tools/lib/bpf/btf.c +++ b/tools/lib/bpf/btf.c @@ -5887,3 +5887,136 @@ 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; + __u32 start_offs; +}; + +/* 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_id =3D *type_id; + + /* refer to the base BTF or VOID type */ + if (new_id < p->btf->start_id) + return 0; + + if (new_id >=3D btf__type_cnt(p->btf)) + return -EINVAL; + + *type_id =3D p->id_map[new_id - p->btf->start_id + p->start_offs]; + 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 n, id, start_offs =3D 0; + + if (!OPTS_VALID(opts, btf_permute_opts)) + return libbpf_err(-EINVAL); + + if (btf__base_btf(btf)) { + n =3D btf->nr_types; + } else { + if (id_map[0] !=3D 0) + return libbpf_err(-EINVAL); + n =3D btf__type_cnt(btf); + start_offs =3D 1; + } + + if (id_map_cnt !=3D n) + 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 start_offs; 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 - start_offs; + /* cannot be mapped to the same ID */ + if (order_map[id]) { + err =3D -EINVAL; + goto done; + } + order_map[id] =3D i + btf->start_id - start_offs; + } + + p.btf =3D btf; + p.id_map =3D id_map; + p.start_offs =3D start_offs; + nt =3D new_types; + for (i =3D start_offs; 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 - start_offs; 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..b30008c267c0 100644 --- a/tools/lib/bpf/btf.h +++ b/tools/lib/bpf/btf.h @@ -281,6 +281,48 @@ 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()** rearranges BTF types in-place according to a = specified ID mapping + * @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, including BTF extension data for refer= ence updates + * @return 0 on success, negative error code on failure + * + * **btf__permute()** reorders BTF types based on the provided @id_map arr= ay, + * updating all internal type references to maintain consistency. The func= tion + * operates in-place, modifying the BTF object directly. + * + * For **base BTF**: + * - @id_map must include all types from ID 0 to `btf__type_cnt(btf) - 1` + * - @id_map_cnt must be `btf__type_cnt(btf)` + * - Mapping is defined as `id_map[original_id] =3D new_id` + * - `id_map[0]` must be 0 (void type cannot be moved) + * + * For **split BTF**: + * - @id_map must include only split types (types added on top of the base= BTF) + * - @id_map_cnt must be `btf__type_cnt(btf) - btf__type_cnt(btf__base_btf= (btf))` + * - Mapping is defined as `id_map[original_id - start_id] =3D new_id` + * - `start_id` equals `btf__type_cnt(btf__base_btf(btf))` + * + * After permutation, all type references within the BTF data and optional + * BTF extension (if provided via @opts) are updated automatically. + * + * On error, returns a negative error code and sets errno: + * - `-EINVAL`: Invalid parameters or invalid ID mapping + * - `-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