From nobody Tue Dec 2 03:00:28 2025 Received: from mail-pf1-f182.google.com (mail-pf1-f182.google.com [209.85.210.182]) (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 333343328EC for ; Mon, 17 Nov 2025 13:26:34 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.182 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1763385995; cv=none; b=M12qJTDD3FFRF4RWzrbGx3U25DM3rAgSeGYvtBZp08aionRIp+AxK8HYQS7S4xpyotdmRyAcjm8ZeOLqcJEJBBGx3VVmwhny2Otzh+g1OGd4JvMzh6aLnOgtBoyBmkgiXqXRxLakq0bKgeYmzj32yFMU+CgW0EsiEezuW6Vr/ko= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1763385995; c=relaxed/simple; bh=6VBbAusWLjZvaGOC/+K+V+im/qf4tqKKK59Uc/T8Ph0=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=VLDpnNC30Kr0GxTmweVvsdiWm3iyA8P/jNpArz9ACH1jmgHgtrC8m+ouutIzRFDjpQM3IzqYc2PdEVuyu2y7nx8WitwBGXjRmMlWmRmzQvnxz4DiUF2SIpt7gswJt/O2KJ7H9Zoh3JqCAfAErJjtKnmhXNBjpgZTYqqtZIAhAOM= 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=Hff5GoYR; arc=none smtp.client-ip=209.85.210.182 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="Hff5GoYR" Received: by mail-pf1-f182.google.com with SMTP id d2e1a72fcca58-7aad4823079so3841614b3a.0 for ; Mon, 17 Nov 2025 05:26:34 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1763385993; x=1763990793; 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=DBbxUx6nVTODhi42u5LXpziwCIk3CPqNxfSqUe5bn+c=; b=Hff5GoYRLNJS2BQp8bczKrl6ga8Z1VxA67Zyl9XibD6T74RYge340yv+LKxOlnjK4m RxRYjEYfiGf9BcDcbxdMPEPckM3C8jAt46ljcZ3zZsPCnmy6Qw9u7eDpUClGcodW62pA Abosi7oOSH9rIGVvhjYVUvmolmmxh/ky/rXvjLbvfHaL6laFtnCLr5kdwApvTrNBSn2j ngsG9PA1rTCtyYNntu4Jk1f2NlAGUmDxRN/2IiwtRNCgvkhjVYZaqiHJ5JDK6n48Zihw KYHgI5l1GweU3yNJhaEB9DpwVRZ+zTavsxX712DhHI1+u58gxLm5vBcaDGbwoV6eaLyV zylw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1763385993; x=1763990793; 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=DBbxUx6nVTODhi42u5LXpziwCIk3CPqNxfSqUe5bn+c=; b=HTf6Iau/Z2FYwnbOG04swSzK6d/IHxCW5aGUICCGnHwyLUt2VcuLGRQZJTqKT2NajW Rxj76fHqurKPQcRtAEEsCv5Q/Lhsy9LBC8pkJ4P6vgheqhhDluqMmlgsC9UGy+uFuBmn VuDWzbN2WDHind15CtFUTwY5v7CuQfDSPvFtZ3y1B6EajEvg7He4eCIxiiUAuOjIjyzd EnKnrx5v2XWN2BbH2a6EODO+Dhs6toXpqM7KpisnfscbJ5lyLY0JC8KJHIKX1FFaEg1D Bit2J2PUIM36W3Omo5DJih58bjcJczGFyPgYvkzRUZB0KFd5fkzxtE9jr0zIx+C1yx4t a+Cg== X-Forwarded-Encrypted: i=1; AJvYcCVa21TukH1JNMEEzPm2G9QQNdF/aUrRO2Hf08MTaoWiwfqN1KEmvLV+oFR3j3zr67feSZIEj/N8OtlkO+k=@vger.kernel.org X-Gm-Message-State: AOJu0YxVGlAptOHuQnCgulpZcybq5ohnqt+7xZYQDYK7Npx/tWwt1ObD EATC4Y5GwUjsuNHa9xbxThV5vhWK9QWjqUPOfHcnCjTStmaYKH28GMRd X-Gm-Gg: ASbGncvEq9lPWuGd7Bey+XErtYvGCyPU8QHzCaiHeqCfSHDTkm8MoFx9x4bBUKZjYvN chjV4B2vqvuj0mpYy3gLNHQWhx9EuXnmEXfU2pthSohMe2iRXZRV6wII9+zzM2wYp2Pfr162j3/ Fs6wKllQjjKVPZhH+jt1gJUP9h63NSUJ2xCno5gqh+H0eLK5MfIofefQ8mJUhBOZu/QCtrag7hC ZSzPX0auPzXfuPkD6/K4sWbxe2atyPm8bh/5IYW63MvZVm0t/H7WCbeLyds5uILNolhIMBO1a7f Wqxifb9dCIBqrRAu5qpCsSUKne25yprBUpbNUqbqt1ND9r49JOU2eUGeQ/gjhF/1JSNmbnv/9lx yIkBuzu0A3zCHFo55AwAAfXluSGIgYph+ErXGQbYmc4x536NqawqPLIkzquQEHnrG1RO0z5K+2X F19k0F5w108wzLWEumCPtGMSCyybM= X-Google-Smtp-Source: AGHT+IGbXFMdLTSTTZa1RPQl3rBb9sfz0EW9VgsJegiZI/gWTT/GjQE/NwLRF8MyNLpZwsf2nDRPyQ== X-Received: by 2002:a05:6a20:729d:b0:360:1b2e:5257 with SMTP id adf61e73a8af0-3601b2e54e1mr2570118637.2.1763385993351; Mon, 17 Nov 2025 05:26:33 -0800 (PST) Received: from pengdl-pc.mioffice.cn ([43.224.245.249]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-7b924cd89bcsm13220953b3a.15.2025.11.17.05.26.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 17 Nov 2025 05:26:32 -0800 (PST) From: Donglin Peng To: ast@kernel.org Cc: eddyz87@gmail.com, andrii.nakryiko@gmail.com, zhangxiaoqin@xiaomi.com, linux-kernel@vger.kernel.org, bpf@vger.kernel.org, Donglin Peng , Alan Maguire , Song Liu Subject: [RFC PATCH v6 1/7] libbpf: Add BTF permutation support for type reordering Date: Mon, 17 Nov 2025 21:26:17 +0800 Message-Id: <20251117132623.3807094-2-dolinux.peng@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20251117132623.3807094-1-dolinux.peng@gmail.com> References: <20251117132623.3807094-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: Song Liu Cc: Xiaoqin Zhang Signed-off-by: Donglin Peng --- tools/lib/bpf/btf.c | 191 +++++++++++++++++++++++++++++++++++++++ tools/lib/bpf/btf.h | 43 +++++++++ tools/lib/bpf/libbpf.map | 1 + 3 files changed, 235 insertions(+) diff --git a/tools/lib/bpf/btf.c b/tools/lib/bpf/btf.c index 18907f0fcf9f..e3aa31c735c8 100644 --- a/tools/lib/bpf/btf.c +++ b/tools/lib/bpf/btf.c @@ -5829,3 +5829,194 @@ 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 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_type_id =3D *type_id; + + /* skip references that point into the base BTF */ + if (new_type_id < p->btf->start_id) + return 0; + + /* invalid reference id */ + if (new_type_id >=3D btf__type_cnt(p->btf)) + return -EINVAL; + + new_type_id =3D p->id_map[new_type_id - p->offs]; + /* reference a dropped type is not allowed */ + if (new_type_id =3D=3D 0) + return -EINVAL; + + *type_id =3D new_type_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 *next_type, *end_type; + void *nt, *new_types =3D NULL; + int err =3D 0, n, i, new_type_len; + __u32 *order_map =3D NULL; + __u32 offs, id, new_nr_types =3D 0; + + if (!OPTS_VALID(opts, btf_permute_opts)) + return libbpf_err(-EINVAL); + + if (btf__base_btf(btf)) { + /* + * For split BTF, the number of types added on the + * top of base BTF + */ + n =3D btf->nr_types; + offs =3D btf->start_id; + } else if (id_map[0] !=3D 0) { + /* id_map[0] must be 0 for base BTF */ + err =3D -EINVAL; + goto done; + } else { + /* include VOID type 0 for base BTF */ + n =3D btf__type_cnt(btf); + offs =3D 0; + } + + if (id_map_cnt !=3D n) + return libbpf_err(-EINVAL); + + /* used to record the storage sequence of types */ + order_map =3D calloc(n, 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]; + /* + * 0: Drop the specified type (exclude base BTF type 0). + * For base BTF, type 0 is always preserved. + */ + if (id =3D=3D 0) + continue; + /* Invalid id */ + if (id < btf->start_id || id >=3D btf__type_cnt(btf)) { + err =3D -EINVAL; + goto done; + } + id -=3D offs; + /* Multiple types cannot be mapped to the same ID */ + if (order_map[id]) { + err =3D -EINVAL; + goto done; + } + order_map[id] =3D i + offs; + new_nr_types =3D max(id + 1, new_nr_types); + } + + /* Check for missing IDs */ + for (i =3D offs ? 0 : 1; i < new_nr_types; i++) { + if (order_map[i] =3D=3D 0) { + err =3D -EINVAL; + goto done; + } + } + + p.btf =3D btf; + p.id_map =3D id_map; + p.offs =3D offs; + nt =3D new_types; + for (i =3D offs ? 0 : 1; i < new_nr_types; i++) { + struct btf_field_iter it; + const struct btf_type *t; + __u32 *type_id; + int type_size; + + id =3D order_map[i]; + /* must be a valid type ID */ + t =3D btf__type_by_id(btf, id); + if (!t) { + err =3D -EINVAL; + goto done; + } + 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; + } + + new_type_len =3D nt - new_types; + next_type =3D new_types; + end_type =3D next_type + new_type_len; + i =3D 0; + while (next_type + sizeof(struct btf_type) <=3D end_type) { + btf->type_offs[i++] =3D next_type - new_types; + next_type +=3D btf_type_size(next_type); + } + + /* Resize */ + if (new_type_len < btf->hdr->type_len) { + void *tmp_types; + + tmp_types =3D realloc(new_types, new_type_len); + if (new_type_len && !tmp_types) { + err =3D -ENOMEM; + goto done; + } + new_types =3D tmp_types; + btf->nr_types =3D new_nr_types - (offs ? 0 : 1); + btf->type_offs_cap =3D btf->nr_types; + btf->types_data_cap =3D new_type_len; + btf->hdr->type_len =3D new_type_len; + btf->hdr->str_off =3D new_type_len; + btf->raw_size =3D btf->hdr->hdr_len + btf->hdr->type_len + btf->hdr->str= _len; + } + + 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 ccfd905f03df..ec4e31e918c3 100644 --- a/tools/lib/bpf/btf.h +++ b/tools/lib/bpf/btf.h @@ -273,6 +273,49 @@ 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 0 to `btf__type_cnt(btf)-1` + * - @id_map_cnt should be `btf__type_cnt(btf)` + * - Mapping uses `id_map[original_id] =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` + * + * Setting @id_map element to 0 (except `id_map[0]` for base BTF) drops th= e corresponding type. + * Dropped types must not be referenced by any retained types. After permu= tation, + * type references in BTF data and optional extension are updated automati= cally. + * + * Note: Dropping types may orphan some strings, requiring subsequent **bt= f__dedup()** + * to clean up unreferenced strings. + * + * 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