From nobody Mon Feb 9 07:22:11 2026 Received: from mail-pf1-f173.google.com (mail-pf1-f173.google.com [209.85.210.173]) (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 9CA1B35B139 for ; Fri, 9 Jan 2026 13:00:13 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.173 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1767963615; cv=none; b=TWk0DxH47hnQ/2wVcNYcx7W20sJWpHH0K6mFLgb9sYXLZEyjsc1flgqWfdwkg9VcqK6UlzXlyUe1lqdwIbG4YlsmVnY3V37aEs/2iTBjfvR8m3NAtUgnGxvN3S0w5liYQxSQaH/ODM9FiCJjPjPn3Yj2qqKuPU1mmoCoSHcvK0Q= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1767963615; c=relaxed/simple; bh=ksqELPO0cXKauiYwTecfnPaKtxldFgV/kANERx4D1IY=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=TaBoAfHRHM32SdkzsTrAFoSwCHs+rHr4kcFLbzX6DcykiV/27OeLac900VqH9Fc5yoxRtxkxWnjebregvGK7N48LTEUedQtXzh5ViCnx5FW3mwSVaiWg8I9FpxomiZh8mkcIlBgco2LKtKPfBG6nUh3v2smcl3BAiANM78wZGns= 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=flJTaxQd; arc=none smtp.client-ip=209.85.210.173 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="flJTaxQd" Received: by mail-pf1-f173.google.com with SMTP id d2e1a72fcca58-81ef4b87291so51874b3a.0 for ; Fri, 09 Jan 2026 05:00:13 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1767963613; x=1768568413; 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=flJTaxQdrv5Lzpa2CUYap4fjKfT7miz/fibkohjp8eEiKMY6euidWA/nDpiZJ3R6DW ZCujTnQqjvY2+YdyAVPqp0+0GPc2V1oSLqIfajgwE4EVQg6AI4VE55GuBKsXgwTIZE3y 8Ihj4NM1cLuIS3rN+QvWtdswxYdwWWEyGY4F71KIKtL1sFrUg/GukxmRstF6aX+gnmjQ jP1TcWNPzNzRNmCWrRo0IuhDwb5x/+X6c+07olsh9BftaHoMVbHli02LCfaIV58FWoXA iquTczMhuuJWj2bCw9Xp+N9kBDUCI51XEaKUfSef0HtorK2zD+Skjxmu05gu2Zz1GewY 4RAA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1767963613; x=1768568413; 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=IrxRTTgdpEZpUncx1PmBty4mqhcKEt51aTMfkDUdB3ERjBkdkq3On4UkTUIzfCWLb6 SQwuP7POUKFLtQlBpERXiB+RKIV44suB2ED7dSLobGmx9FuHhOPqrPmqcSblNUbTi4oZ 3hs6nWQ3lki4fQXq3Rm7RDlDKFQhzqAWLPMGVsZH6XtY99Up78G6PeBguUqodAp+ABGn 9GR0uPAyQs0MCLFcpDPnlc4iMc/8wDqV0ROS60UT3Galr9ZrHy6YC1/KOiPmLFKL7dWz NpAqER8PFB2icwIEiElL8c/pvD1+20xA3FE9qVFqmQLBQRaS4/pHBVJmjZCfGMYRg34U +Csw== X-Forwarded-Encrypted: i=1; AJvYcCWhB0bnedoMHYjqCvxMLnHfByvTHNkJSO3Zmv3L6fp5MJZScUEdHtgyaZyOprNsGRCCY73azCm+Ttyy9HY=@vger.kernel.org X-Gm-Message-State: AOJu0YxNqPVT6o0pzgzMvKEVjqSd/NFJC4zXHZqdGKN4kQQQTytMHHo+ 2xBtILy5W3qz7hUYopo5nwIFwRAEWG5yB/zCsQzRBEVAF8SbQv+v71m6 X-Gm-Gg: AY/fxX4Ri9Fj/LZcs6bLv/x6ZHfQtwji/1sGCX63MZ3YfJHjolvufJuY5bXzVoCAHxA tlwX9VdDLPE0HkOZ19Zgnui5dVoEZI0cAINFBsL5EAxmDieb57AdnZaDaeBFkcAVXOjm6LtnH6X 7WAZh/tdwjbnchC6uclGHBifBuOy6U02cwH3krnLN9420dgx0DhC1h+SyLSKlnmXkAcqtTRXj+2 Od21t6nNsbBm1784MDBkHVslRW4/CCWITtqN1G5tL368zkuIm5Kq5fI4Ai3G42IPWhW6qyFeW02 K8C5eK7mcda5ISypv0/8AIvn4QDrG93O8gMSbVU3L5VKMYnpuvEqPLOAhLsxmByFUW+ZcNfHLC/ 44QFT1It+mhpyXIid5iBNRosugEV8RfTTUBnDWlHvh1x5O7Kg9V08p6kFJSREmmOUIi/ukaR8Yl VBjD9Y7iUQuTJw8TpAt5jibSnr9SY= X-Google-Smtp-Source: AGHT+IF51Igam8v/LIPmHw9lFij0EDTO1sN2sr7KpHZqEplaw8HFWRTzlRk8sdAEKOZQNo95zqw+LQ== X-Received: by 2002:a05:6a20:94c9:b0:366:5bda:1e87 with SMTP id adf61e73a8af0-3898fa40b18mr9183375637.80.1767963612265; Fri, 09 Jan 2026 05:00:12 -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.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 09 Jan 2026 05:00:11 -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 01/11] libbpf: Add BTF permutation support for type reordering Date: Fri, 9 Jan 2026 20:59:53 +0800 Message-Id: <20260109130003.3313716-2-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 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