From nobody Sat Feb 7 17:42:21 2026 Received: from mail-pl1-f181.google.com (mail-pl1-f181.google.com [209.85.214.181]) (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 A251030171B for ; Fri, 14 Nov 2025 09:03:57 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.181 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1763111039; cv=none; b=XxJNwbJ+/vfq1qImvVhJKshyPyDzHLQkR161cSzE+I2rBCsT/GR8oGlajHkm0FKe2LiOE9MLMKvYDTd+yu47fOzFIHSsHAccDO6LEMGtkjMav/UKgLVebuqiIvYJHHauBsNwXDN9wv6JerSYmtxOSdGD+pipnOtjsoRbe2zBBh8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1763111039; c=relaxed/simple; bh=mSwnWCwoL+MLuntCffdTS1GyT+kU9z+/dn8QhVSGrPk=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=sJ5Wj1/MOvu4Uv1PQqOrJ7w0cEHjA20ZSkfUeET+NLQE0AcvUrtAQudc3/wKaVDReJKZQnJna4AabrE56fAyLzbhreKqCfpmay4FB8e1JRr+8zWH+GcFGlhfCtA2CHzcwBe8fMYpuhRvefNGJatvtoCkXgxbRnKVo0Tdh3yqWdw= 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=cjiRKt5w; arc=none smtp.client-ip=209.85.214.181 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="cjiRKt5w" Received: by mail-pl1-f181.google.com with SMTP id d9443c01a7336-2981f9ce15cso21171315ad.1 for ; Fri, 14 Nov 2025 01:03:57 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1763111037; x=1763715837; 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=BqQyqGcdeA3UbShSzK/mrDf85bRWtniYX8zTmwsdvNI=; b=cjiRKt5w/qoJFPt0icJoQgP8GFCr+4NaOvQP76XH92wHIto4vcFQ12aZy8+LYrZhzG 2a6znCXh1PjMn8F4IuIFbTUOl38OLb/Yunav6tBuBYt/EjePgF2Qj4RnWAPalFJNJyBY Oy9dtUhrSDH0AW6t0I2CHOD8zAgCsnB97ZtvHvJckIxL2yvBHlWUKFHPXP6fwXFiBeQL qo6uFBVaEhMtj/hHiXJPv8CHvQCk0SOeDypRZEnsHsmDxcofHh6o9crNVK/6uqCSf2Gm dxoZDKq/EuzpP+8jeMzeUrhKZs+CD+s/inZP+5ATltQGl3j55JDky7QJZCf6Hzp++WnH mxFQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1763111037; x=1763715837; 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=BqQyqGcdeA3UbShSzK/mrDf85bRWtniYX8zTmwsdvNI=; b=mUBJ41qX1ufHA24R48fol+1xhoAFRFjVygeYI/DKLu/DtLLmo0TKWX7vYPsh+fXpAH ARddIlLkfPyhPxDOstP9W8LD+4Dkv1WuCdH2VILWavP5ejb1uHqrWF1CtyXUDvaJM7Oq 9ndGjXW6wd1Rk+Eun4f+1tnco5qQVVS42D6cUToAlNnszFanLVmGF5diBETN7mOhU3vj yYBbG/WFG61cHqD7TSVeXj91fYNYUOC55XH74jpS9gZQ80jokgiKUfQRYs8I6YPff4F2 NA2GZPOWnCNr+fjniYVcSahpYumHaQczQbg0twzGEo5RLJc5lIbZSJmC7DqP5FDTuS5h D0UA== X-Forwarded-Encrypted: i=1; AJvYcCUUg1ODEIiulVF8laMcR4HRBf1Ufh7UEGUPX787dsp6Ekqq1aLPdvMNpkhDguei0yjEAkqnUNI6/WkRZ2Q=@vger.kernel.org X-Gm-Message-State: AOJu0Yzxi72NtyeOtYzBFvdE7LXi1AaURW5j/hnE3+OAofFabSSvCiL3 0tFhcKCdh+Oju0nFTV+LffaJVObux1jhk4Wd/3Ou3a/ArsZLYano7yJu X-Gm-Gg: ASbGncsf+9FQsU6W6fljxyrN5EzEgyYmDBUPcJ3yGOWRMzItF3xQdZx+vYk3zVk3qpB PjVuNjqikQAHFsdDK7HFftjlxX5GMMoPiG71oJMlICK30h9ngZ8KIjYxE4gNT9vAQd4ytV88t+Y 68q7oHNQHho+MmzzSuQLxLTMlgwGeM1AQT5PgBpuadi8FcQ2Dm3ZRw8em3oTVGjEdUSFe2XMmZu kyn0TBMo0NSTWZ5YXX1Ri5W78nTjwggNIJALu5+6NIMLGEjrrWMC97TDSL5dHpEi6w45DCIqQn1 16D+EhAEjTgIePrVs9IfiEUuQizRn1p1GO3LKGjKBex/xrzHXTV9JTTlzqNrYuDwKz3viu4zOGa RAawjw8iiOhYd5olQkM2/QPcbDTibOi7gA8iII+lsdfRrfgFCAIT4Dz3IfSZTDy6bE3p9ajCclM l6hsxMGtvJOk33cC7H2S0YlG8stBs= X-Google-Smtp-Source: AGHT+IFqWVA/hDgLWbf8Cdc8Trj/t/PBbfDJxyG3yC7b0vYCwasCuQtfKOv+Suf59TOmAo+Cum62Zg== X-Received: by 2002:a17:902:d492:b0:297:d4a5:64e5 with SMTP id d9443c01a7336-2986a72df5cmr25852425ad.30.1763111036857; Fri, 14 Nov 2025 01:03:56 -0800 (PST) Received: from pengdl-pc.mioffice.cn ([43.224.245.249]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-2985c2cca66sm48362605ad.99.2025.11.14.01.03.53 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 14 Nov 2025 01:03:55 -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 1/2] libbpf: Add BTF permutation support for type reordering Date: Fri, 14 Nov 2025 17:02:30 +0800 Message-Id: <20251114090231.2786984-2-dolinux.peng@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20251114090231.2786984-1-dolinux.peng@gmail.com> References: <20251114090231.2786984-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 | 186 +++++++++++++++++++++++++++++++++++++++ tools/lib/bpf/btf.h | 43 +++++++++ tools/lib/bpf/libbpf.map | 1 + 3 files changed, 230 insertions(+) diff --git a/tools/lib/bpf/btf.c b/tools/lib/bpf/btf.c index 18907f0fcf9f..d22a2b0581b3 100644 --- a/tools/lib/bpf/btf.c +++ b/tools/lib/bpf/btf.c @@ -5829,3 +5829,189 @@ 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 (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 (!OPTS_VALID(opts, btf_permute_opts) || (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(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 From nobody Sat Feb 7 17:42:21 2026 Received: from mail-pl1-f169.google.com (mail-pl1-f169.google.com [209.85.214.169]) (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 7F679302746 for ; Fri, 14 Nov 2025 09:04:01 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.169 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1763111043; cv=none; b=aTcxINQyYplt8R182wVmVtDdLs9fnl5VM705C9y80+udOObPueP77C+p2MPD6Thiznm2radBOFFee4Yg4BLIFkXWAHJOzyeQh+TWuveI2BAvVvPa5UnA5RRSxaSxmWHnxJQDRlu9pfFMnvalupQQXkdsktiAtIsIAsEpbN0HySs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1763111043; c=relaxed/simple; bh=kaCcv/gxLali8+OQISBxtBb21YuXVVu0kdZi2au/Qu4=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=H6Tm/ITZb6nhdYzwiAmoY8HI9/9fjjwJYmlgkKyTB2bqX83/D+0HNK4DUzXO6VZ1VaAByRmK0xEIBkLZ/eudUiCXaDVkpRD44us6AUeMMUcGct26yCtxWH3AdkPupcYY62MCFBZ8fTe3oZPb5f84OBmYLuY6NqD+c4yreUAf3jI= 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=i9z+R2WV; arc=none smtp.client-ip=209.85.214.169 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="i9z+R2WV" Received: by mail-pl1-f169.google.com with SMTP id d9443c01a7336-29555b384acso19075145ad.1 for ; Fri, 14 Nov 2025 01:04:01 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1763111041; x=1763715841; 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=Dnz978baveRxYd8wc+wY2UrNBM5VV6QZrF0AyP76BvM=; b=i9z+R2WVs1y4BcVmzoKcVoiioWzKX6fFmrqInHBVnJbiAzDuTJOPd2xmTsWAKN+vbr 7QXn/FAuQdX0sXzIUXsVIh263sdvllkXyW/1M14LTG9tMvf5vO+EUjbdxEihUxP1RKlT +OK6Zk/mJj5jKGhLrMdoItFUlwIp5DF5KAelmqTWnC8bt0EuGOPBQp6hd8pLcs+C8JSz qJjc05n0NJdrvGjx2WDg2YDdCdI5pd7TYTejQ7L3Q+8SYU89pyW+d0Ca6sECFNJHcQiN KOp8o01sUVDTa7fHQSuZBp1y+kRkapc2lbGCrbCDOrbU+6Pl7HaCUGDUTHzEbumam5Ed 7zgQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1763111041; x=1763715841; 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=Dnz978baveRxYd8wc+wY2UrNBM5VV6QZrF0AyP76BvM=; b=JCr/qUMMFn9c5CM5fG7Vt56Jicsk8DDItMERLfltoDHif6VFq6/Ecoi7nBGGPUGFn7 8Gz/GyEq3gbI1QXHsxRtmiHvYQ72xisvjFvq2Qnd0fXGmp5JFuXchYAIFhXsyrTSNQPq 4frOGB+lgDWjgMXeSMbdq2FODGM+rnXfJEwLnSeMZdQb0HN+ketP+KKTMsGYk7+/w8/j qwXiww8lY59HuUzPVEDN+Hn8WAdHJSI0cO/u8RSVpz/3HdArBjbZ8NzlegqZFY3BYA2c RlVgyEkLVKf0CNfy6Pbm09SKBopLbiIDK/ioFXPeSs7LBOu9Q/bGqi3KzqWcMqv427dQ jTxw== X-Forwarded-Encrypted: i=1; AJvYcCW5lxF1pN5VYGDxUu37rM1xMYQCcgHq+5ebDQNI0ARAdD3c5wt/M+rEg8VjDr77QYqBURruJtO4L2/fAyg=@vger.kernel.org X-Gm-Message-State: AOJu0Yzc2sUZnXTbcHLavQ7r2KfBzesQa+LTdTP83TWBkeaQ3U5OcD/o urvnOxIF6Ukz21Nh4fgpHlkckmLZ74cAoRS6DFMjFtGom5wg7gQDeSXs X-Gm-Gg: ASbGncsjR/fY6HHXuHK8miBM9F4+ohTNXViXBiLSryYc/m591vPgH46zQuMFeL04RmR UH+89ORSSwu1jVyKxf0/FzkCHfLT75IJ47v6+EK3wNgo3czIh0iLV5IUiXLtZJMZekxPY3Qufkz IQqfdjhpXj0LMaxsZzb0pE1Pqfl3/UhdIv8swcHBUothkB1K5OF3hSeeLoJj3YH3P5BWbxGBP33 iZtDlF2K+kPLO1uUTZladTJQ6AAZyxogexLWSXkzFKz9zrI05VZJ20+Cpv1KXklzvLQF3nASLLP +EJ7/DW+XjFLc6vMEizkvOV2qPzc7kTAIbP0ukJJX3U9Y8uLHS14EFbO5/aVL2dZJKH8scTl0g0 QVou2+3C80E2/0puHk51rKusZVhjVbcttcUSSAqy+jQhCk0wScXQxWqWz5JrD7NEDDQ0y1dThY9 Kr+PccIdB26HFXDjgFaDmTBAcHRSs= X-Google-Smtp-Source: AGHT+IFDHmOsxp10mTDwD3+or7sTDeUuVW883O4cpyuSK8PUcKKcJ85WjZ1xOZEJCEREIhHJup+SdA== X-Received: by 2002:a17:902:ccc3:b0:24c:7b94:2f53 with SMTP id d9443c01a7336-2986a6b8528mr26627905ad.6.1763111040259; Fri, 14 Nov 2025 01:04:00 -0800 (PST) Received: from pengdl-pc.mioffice.cn ([43.224.245.249]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-2985c2cca66sm48362605ad.99.2025.11.14.01.03.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 14 Nov 2025 01:03:59 -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 2/2] selftests/bpf: Add test cases for btf__permute functionality Date: Fri, 14 Nov 2025 17:02:31 +0800 Message-Id: <20251114090231.2786984-3-dolinux.peng@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20251114090231.2786984-1-dolinux.peng@gmail.com> References: <20251114090231.2786984-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 This patch introduces test cases for the btf__permute function to ensure it works correctly with both base BTF and split BTF scenarios. The test suite includes: - test_permute_base: Validates permutation on base BTF - test_permute_split: Tests permutation on split BTF - test_permute_drop_base: Validates type dropping on base BTF - test_permute_drop_split: Tests type dropping on split BTF - test_permute_drop_dedup: Tests type dropping and deduping Cc: Eduard Zingerman Cc: Alexei Starovoitov Cc: Andrii Nakryiko Cc: Alan Maguire Cc: Song Liu Cc: Xiaoqin Zhang Signed-off-by: Donglin Peng --- .../selftests/bpf/prog_tests/btf_permute.c | 626 ++++++++++++++++++ 1 file changed, 626 insertions(+) create mode 100644 tools/testing/selftests/bpf/prog_tests/btf_permute.c diff --git a/tools/testing/selftests/bpf/prog_tests/btf_permute.c b/tools/t= esting/selftests/bpf/prog_tests/btf_permute.c new file mode 100644 index 000000000000..c1a47b8461ee --- /dev/null +++ b/tools/testing/selftests/bpf/prog_tests/btf_permute.c @@ -0,0 +1,626 @@ +// SPDX-License-Identifier: GPL-2.0 +/* Copyright (c) 2025 Xiaomi */ + +#include +#include +#include "btf_helpers.h" + +/* Ensure btf__permute work as expected with base BTF */ +static void test_permute_base(void) +{ + struct btf *btf; + __u32 permute_ids[7]; + int err; + + btf =3D btf__new_empty(); + if (!ASSERT_OK_PTR(btf, "empty_main_btf")) + return; + + btf__add_int(btf, "int", 4, BTF_INT_SIGNED); /* [1] int */ + btf__add_ptr(btf, 1); /* [2] ptr to int */ + btf__add_struct(btf, "s1", 4); /* [3] struct s1 { */ + btf__add_field(btf, "m", 1, 0, 0); /* int m; */ + /* } */ + btf__add_struct(btf, "s2", 4); /* [4] struct s2 { */ + btf__add_field(btf, "m", 1, 0, 0); /* int m; */ + /* } */ + btf__add_func_proto(btf, 1); /* [5] int (*)(int *p); */ + btf__add_func_param(btf, "p", 2); + btf__add_func(btf, "f", BTF_FUNC_STATIC, 5); /* [6] int f(int *p); */ + + VALIDATE_RAW_BTF( + btf, + "[1] INT 'int' size=3D4 bits_offset=3D0 nr_bits=3D32 encoding=3DSIGNED", + "[2] PTR '(anon)' type_id=3D1", + "[3] STRUCT 's1' size=3D4 vlen=3D1\n" + "\t'm' type_id=3D1 bits_offset=3D0", + "[4] STRUCT 's2' size=3D4 vlen=3D1\n" + "\t'm' type_id=3D1 bits_offset=3D0", + "[5] FUNC_PROTO '(anon)' ret_type_id=3D1 vlen=3D1\n" + "\t'p' type_id=3D2", + "[6] FUNC 'f' type_id=3D5 linkage=3Dstatic"); + + permute_ids[0] =3D 0; + permute_ids[1] =3D 4; /* [1] -> [4] */ + permute_ids[2] =3D 3; /* [2] -> [3] */ + permute_ids[3] =3D 5; /* [3] -> [5] */ + permute_ids[4] =3D 1; /* [4] -> [1] */ + permute_ids[5] =3D 6; /* [5] -> [6] */ + permute_ids[6] =3D 2; /* [6] -> [2] */ + err =3D btf__permute(btf, permute_ids, ARRAY_SIZE(permute_ids), NULL); + if (!ASSERT_OK(err, "btf__permute_base")) + goto done; + + VALIDATE_RAW_BTF( + btf, + "[1] STRUCT 's2' size=3D4 vlen=3D1\n" + "\t'm' type_id=3D4 bits_offset=3D0", + "[2] FUNC 'f' type_id=3D6 linkage=3Dstatic", + "[3] PTR '(anon)' type_id=3D4", + "[4] INT 'int' size=3D4 bits_offset=3D0 nr_bits=3D32 encoding=3DSIGNED", + "[5] STRUCT 's1' size=3D4 vlen=3D1\n" + "\t'm' type_id=3D4 bits_offset=3D0", + "[6] FUNC_PROTO '(anon)' ret_type_id=3D4 vlen=3D1\n" + "\t'p' type_id=3D3"); + + /* For base BTF, id_map[0] must be 0 */ + permute_ids[0] =3D 4; + permute_ids[1] =3D 0; + permute_ids[2] =3D 3; + permute_ids[3] =3D 5; + permute_ids[4] =3D 1; + permute_ids[5] =3D 6; + permute_ids[6] =3D 2; + err =3D btf__permute(btf, permute_ids, ARRAY_SIZE(permute_ids), NULL); + if (!ASSERT_ERR(err, "btf__permute_base")) + goto done; + + VALIDATE_RAW_BTF( + btf, + "[1] STRUCT 's2' size=3D4 vlen=3D1\n" + "\t'm' type_id=3D4 bits_offset=3D0", + "[2] FUNC 'f' type_id=3D6 linkage=3Dstatic", + "[3] PTR '(anon)' type_id=3D4", + "[4] INT 'int' size=3D4 bits_offset=3D0 nr_bits=3D32 encoding=3DSIGNED", + "[5] STRUCT 's1' size=3D4 vlen=3D1\n" + "\t'm' type_id=3D4 bits_offset=3D0", + "[6] FUNC_PROTO '(anon)' ret_type_id=3D4 vlen=3D1\n" + "\t'p' type_id=3D3"); + + /* + * For base BTF, id_map_cnt must equal to the number of types + * include VOID type + */ + permute_ids[0] =3D 4; + permute_ids[1] =3D 0; + permute_ids[2] =3D 3; + permute_ids[3] =3D 5; + permute_ids[4] =3D 1; + permute_ids[5] =3D 6; + err =3D btf__permute(btf, permute_ids, ARRAY_SIZE(permute_ids) - 1, NULL); + if (!ASSERT_ERR(err, "btf__permute_base")) + goto done; + + VALIDATE_RAW_BTF( + btf, + "[1] STRUCT 's2' size=3D4 vlen=3D1\n" + "\t'm' type_id=3D4 bits_offset=3D0", + "[2] FUNC 'f' type_id=3D6 linkage=3Dstatic", + "[3] PTR '(anon)' type_id=3D4", + "[4] INT 'int' size=3D4 bits_offset=3D0 nr_bits=3D32 encoding=3DSIGNED", + "[5] STRUCT 's1' size=3D4 vlen=3D1\n" + "\t'm' type_id=3D4 bits_offset=3D0", + "[6] FUNC_PROTO '(anon)' ret_type_id=3D4 vlen=3D1\n" + "\t'p' type_id=3D3"); + + /* Multiple types can not be mapped to the same ID */ + permute_ids[0] =3D 0; + permute_ids[1] =3D 4; + permute_ids[2] =3D 4; + permute_ids[3] =3D 5; + permute_ids[4] =3D 1; + permute_ids[5] =3D 6; + permute_ids[6] =3D 2; + err =3D btf__permute(btf, permute_ids, ARRAY_SIZE(permute_ids), NULL); + if (!ASSERT_ERR(err, "btf__permute_base")) + goto done; + + VALIDATE_RAW_BTF( + btf, + "[1] STRUCT 's2' size=3D4 vlen=3D1\n" + "\t'm' type_id=3D4 bits_offset=3D0", + "[2] FUNC 'f' type_id=3D6 linkage=3Dstatic", + "[3] PTR '(anon)' type_id=3D4", + "[4] INT 'int' size=3D4 bits_offset=3D0 nr_bits=3D32 encoding=3DSIGNED", + "[5] STRUCT 's1' size=3D4 vlen=3D1\n" + "\t'm' type_id=3D4 bits_offset=3D0", + "[6] FUNC_PROTO '(anon)' ret_type_id=3D4 vlen=3D1\n" + "\t'p' type_id=3D3"); + + /* Type ID must be valid */ + permute_ids[0] =3D 0; + permute_ids[1] =3D 4; + permute_ids[2] =3D 3; + permute_ids[3] =3D 5; + permute_ids[4] =3D 1; + permute_ids[5] =3D 7; + permute_ids[6] =3D 2; + err =3D btf__permute(btf, permute_ids, ARRAY_SIZE(permute_ids), NULL); + if (!ASSERT_ERR(err, "btf__permute_base")) + goto done; + + VALIDATE_RAW_BTF( + btf, + "[1] STRUCT 's2' size=3D4 vlen=3D1\n" + "\t'm' type_id=3D4 bits_offset=3D0", + "[2] FUNC 'f' type_id=3D6 linkage=3Dstatic", + "[3] PTR '(anon)' type_id=3D4", + "[4] INT 'int' size=3D4 bits_offset=3D0 nr_bits=3D32 encoding=3DSIGNED", + "[5] STRUCT 's1' size=3D4 vlen=3D1\n" + "\t'm' type_id=3D4 bits_offset=3D0", + "[6] FUNC_PROTO '(anon)' ret_type_id=3D4 vlen=3D1\n" + "\t'p' type_id=3D3"); + +done: + btf__free(btf); +} + +/* Ensure btf__permute work as expected with split BTF */ +static void test_permute_split(void) +{ + struct btf *split_btf =3D NULL, *base_btf =3D NULL; + __u32 permute_ids[4]; + int err; + + base_btf =3D btf__new_empty(); + if (!ASSERT_OK_PTR(base_btf, "empty_main_btf")) + return; + + btf__add_int(base_btf, "int", 4, BTF_INT_SIGNED); /* [1] int */ + btf__add_ptr(base_btf, 1); /* [2] ptr to int */ + VALIDATE_RAW_BTF( + base_btf, + "[1] INT 'int' size=3D4 bits_offset=3D0 nr_bits=3D32 encoding=3DSIGNED", + "[2] PTR '(anon)' type_id=3D1"); + split_btf =3D btf__new_empty_split(base_btf); + if (!ASSERT_OK_PTR(split_btf, "empty_split_btf")) + goto cleanup; + btf__add_struct(split_btf, "s1", 4); /* [3] struct s1 { */ + btf__add_field(split_btf, "m", 1, 0, 0); /* int m; */ + /* } */ + btf__add_struct(split_btf, "s2", 4); /* [4] struct s2 { */ + btf__add_field(split_btf, "m", 1, 0, 0); /* int m; */ + /* } */ + btf__add_func_proto(split_btf, 1); /* [5] int (*)(int p); */ + btf__add_func_param(split_btf, "p", 2); + btf__add_func(split_btf, "f", BTF_FUNC_STATIC, 5); /* [6] int f(int *p); = */ + + VALIDATE_RAW_BTF( + split_btf, + "[1] INT 'int' size=3D4 bits_offset=3D0 nr_bits=3D32 encoding=3DSIGNED", + "[2] PTR '(anon)' type_id=3D1", + "[3] STRUCT 's1' size=3D4 vlen=3D1\n" + "\t'm' type_id=3D1 bits_offset=3D0", + "[4] STRUCT 's2' size=3D4 vlen=3D1\n" + "\t'm' type_id=3D1 bits_offset=3D0", + "[5] FUNC_PROTO '(anon)' ret_type_id=3D1 vlen=3D1\n" + "\t'p' type_id=3D2", + "[6] FUNC 'f' type_id=3D5 linkage=3Dstatic"); + + permute_ids[0] =3D 6; /* [3] -> [6] */ + permute_ids[1] =3D 3; /* [4] -> [3] */ + permute_ids[2] =3D 5; /* [5] -> [5] */ + permute_ids[3] =3D 4; /* [6] -> [4] */ + err =3D btf__permute(split_btf, permute_ids, ARRAY_SIZE(permute_ids), NUL= L); + if (!ASSERT_OK(err, "btf__permute_split")) + goto cleanup; + + VALIDATE_RAW_BTF( + split_btf, + "[1] INT 'int' size=3D4 bits_offset=3D0 nr_bits=3D32 encoding=3DSIGNED", + "[2] PTR '(anon)' type_id=3D1", + "[3] STRUCT 's2' size=3D4 vlen=3D1\n" + "\t'm' type_id=3D1 bits_offset=3D0", + "[4] FUNC 'f' type_id=3D5 linkage=3Dstatic", + "[5] FUNC_PROTO '(anon)' ret_type_id=3D1 vlen=3D1\n" + "\t'p' type_id=3D2", + "[6] STRUCT 's1' size=3D4 vlen=3D1\n" + "\t'm' type_id=3D1 bits_offset=3D0"); + + /* + * For split BTF, id_map_cnt must equal to the number of types + * added on top of base BTF + */ + permute_ids[0] =3D 4; + permute_ids[1] =3D 3; + permute_ids[2] =3D 5; + permute_ids[3] =3D 6; + err =3D btf__permute(split_btf, permute_ids, 3, NULL); + if (!ASSERT_ERR(err, "btf__permute_split")) + goto cleanup; + + VALIDATE_RAW_BTF( + split_btf, + "[1] INT 'int' size=3D4 bits_offset=3D0 nr_bits=3D32 encoding=3DSIGNED", + "[2] PTR '(anon)' type_id=3D1", + "[3] STRUCT 's2' size=3D4 vlen=3D1\n" + "\t'm' type_id=3D1 bits_offset=3D0", + "[4] FUNC 'f' type_id=3D5 linkage=3Dstatic", + "[5] FUNC_PROTO '(anon)' ret_type_id=3D1 vlen=3D1\n" + "\t'p' type_id=3D2", + "[6] STRUCT 's1' size=3D4 vlen=3D1\n" + "\t'm' type_id=3D1 bits_offset=3D0"); + + /* Multiple types can not be mapped to the same ID */ + permute_ids[0] =3D 4; + permute_ids[1] =3D 3; + permute_ids[2] =3D 3; + permute_ids[3] =3D 6; + err =3D btf__permute(split_btf, permute_ids, 4, NULL); + if (!ASSERT_ERR(err, "btf__permute_split")) + goto cleanup; + + VALIDATE_RAW_BTF( + split_btf, + "[1] INT 'int' size=3D4 bits_offset=3D0 nr_bits=3D32 encoding=3DSIGNED", + "[2] PTR '(anon)' type_id=3D1", + "[3] STRUCT 's2' size=3D4 vlen=3D1\n" + "\t'm' type_id=3D1 bits_offset=3D0", + "[4] FUNC 'f' type_id=3D5 linkage=3Dstatic", + "[5] FUNC_PROTO '(anon)' ret_type_id=3D1 vlen=3D1\n" + "\t'p' type_id=3D2", + "[6] STRUCT 's1' size=3D4 vlen=3D1\n" + "\t'm' type_id=3D1 bits_offset=3D0"); + + /* Can not map to base ID */ + permute_ids[0] =3D 4; + permute_ids[1] =3D 2; + permute_ids[2] =3D 5; + permute_ids[3] =3D 6; + err =3D btf__permute(split_btf, permute_ids, 4, NULL); + if (!ASSERT_ERR(err, "btf__permute_split")) + goto cleanup; + + VALIDATE_RAW_BTF( + split_btf, + "[1] INT 'int' size=3D4 bits_offset=3D0 nr_bits=3D32 encoding=3DSIGNED", + "[2] PTR '(anon)' type_id=3D1", + "[3] STRUCT 's2' size=3D4 vlen=3D1\n" + "\t'm' type_id=3D1 bits_offset=3D0", + "[4] FUNC 'f' type_id=3D5 linkage=3Dstatic", + "[5] FUNC_PROTO '(anon)' ret_type_id=3D1 vlen=3D1\n" + "\t'p' type_id=3D2", + "[6] STRUCT 's1' size=3D4 vlen=3D1\n" + "\t'm' type_id=3D1 bits_offset=3D0"); + +cleanup: + btf__free(split_btf); + btf__free(base_btf); +} + +/* Verify btf__permute function drops types correctly with base_btf */ +static void test_permute_drop_base(void) +{ + struct btf *btf; + __u32 permute_ids[7]; + int err; + + btf =3D btf__new_empty(); + if (!ASSERT_OK_PTR(btf, "empty_main_btf")) + return; + + btf__add_int(btf, "int", 4, BTF_INT_SIGNED); /* [1] int */ + btf__add_ptr(btf, 1); /* [2] ptr to int */ + btf__add_struct(btf, "s1", 4); /* [3] struct s1 { */ + btf__add_field(btf, "m", 1, 0, 0); /* int m; */ + /* } */ + btf__add_struct(btf, "s2", 4); /* [4] struct s2 { */ + btf__add_field(btf, "m", 1, 0, 0); /* int m; */ + /* } */ + btf__add_func_proto(btf, 1); /* [5] int (*)(int *p); */ + btf__add_func_param(btf, "p", 2); + btf__add_func(btf, "f", BTF_FUNC_STATIC, 5); /* [6] int f(int *p); */ + + VALIDATE_RAW_BTF( + btf, + "[1] INT 'int' size=3D4 bits_offset=3D0 nr_bits=3D32 encoding=3DSIGNED", + "[2] PTR '(anon)' type_id=3D1", + "[3] STRUCT 's1' size=3D4 vlen=3D1\n" + "\t'm' type_id=3D1 bits_offset=3D0", + "[4] STRUCT 's2' size=3D4 vlen=3D1\n" + "\t'm' type_id=3D1 bits_offset=3D0", + "[5] FUNC_PROTO '(anon)' ret_type_id=3D1 vlen=3D1\n" + "\t'p' type_id=3D2", + "[6] FUNC 'f' type_id=3D5 linkage=3Dstatic"); + + /* Drop ID 4 */ + permute_ids[0] =3D 0; + permute_ids[1] =3D 5; /* [1] -> [5] */ + permute_ids[2] =3D 1; /* [2] -> [1] */ + permute_ids[3] =3D 2; /* [3] -> [2] */ + permute_ids[4] =3D 0; /* Drop [4] */ + permute_ids[5] =3D 3; /* [5] -> [3] */ + permute_ids[6] =3D 4; /* [6] -> [4] */ + err =3D btf__permute(btf, permute_ids, ARRAY_SIZE(permute_ids), NULL); + if (!ASSERT_OK(err, "btf__permute_drop_base")) + goto done; + + VALIDATE_RAW_BTF( + btf, + "[1] PTR '(anon)' type_id=3D5", + "[2] STRUCT 's1' size=3D4 vlen=3D1\n" + "\t'm' type_id=3D5 bits_offset=3D0", + "[3] FUNC_PROTO '(anon)' ret_type_id=3D5 vlen=3D1\n" + "\t'p' type_id=3D1", + "[4] FUNC 'f' type_id=3D3 linkage=3Dstatic", + "[5] INT 'int' size=3D4 bits_offset=3D0 nr_bits=3D32 encoding=3DSIGNED"); + + /* Continue dropping */ + permute_ids[0] =3D 0; + permute_ids[1] =3D 1; /* [1] -> [1] */ + permute_ids[2] =3D 2; /* [2] -> [2] */ + permute_ids[3] =3D 3; /* [3] -> [3] */ + permute_ids[4] =3D 0; /* Drop [4] */ + permute_ids[5] =3D 4; /* [5] -> [4] */ + err =3D btf__permute(btf, permute_ids, 6, NULL); + if (!ASSERT_OK(err, "btf__permute_drop_base_fail")) + goto done; + + VALIDATE_RAW_BTF( + btf, + "[1] PTR '(anon)' type_id=3D4", + "[2] STRUCT 's1' size=3D4 vlen=3D1\n" + "\t'm' type_id=3D4 bits_offset=3D0", + "[3] FUNC_PROTO '(anon)' ret_type_id=3D4 vlen=3D1\n" + "\t'p' type_id=3D1", + "[4] INT 'int' size=3D4 bits_offset=3D0 nr_bits=3D32 encoding=3DSIGNED"); + + /* Cannot drop the ID referenced by others */ + permute_ids[0] =3D 0; + permute_ids[1] =3D 2; + permute_ids[2] =3D 3; + permute_ids[3] =3D 1; + permute_ids[4] =3D 0; /* [4] is referenced by others */ + err =3D btf__permute(btf, permute_ids, 5, NULL); + if (!ASSERT_ERR(err, "btf__permute_drop_base_fail")) + goto done; + + VALIDATE_RAW_BTF( + btf, + "[1] PTR '(anon)' type_id=3D4", + "[2] STRUCT 's1' size=3D4 vlen=3D1\n" + "\t'm' type_id=3D4 bits_offset=3D0", + "[3] FUNC_PROTO '(anon)' ret_type_id=3D4 vlen=3D1\n" + "\t'p' type_id=3D1", + "[4] INT 'int' size=3D4 bits_offset=3D0 nr_bits=3D32 encoding=3DSIGNED"); + + /* Drop 2 IDs at once */ + permute_ids[0] =3D 0; + permute_ids[1] =3D 2; /* [1] -> [2] */ + permute_ids[2] =3D 0; /* Drop [2] */ + permute_ids[3] =3D 0; /* Drop [3] */ + permute_ids[4] =3D 1; /* [4] -> [1] */ + err =3D btf__permute(btf, permute_ids, 5, NULL); + if (!ASSERT_OK(err, "btf__permute_drop_base_fail")) + goto done; + + VALIDATE_RAW_BTF( + btf, + "[1] INT 'int' size=3D4 bits_offset=3D0 nr_bits=3D32 encoding=3DSIGNED", + "[2] PTR '(anon)' type_id=3D1"); + + /* Drop all IDs */ + permute_ids[0] =3D 0; + permute_ids[1] =3D 0; /* Drop [1] */ + permute_ids[2] =3D 0; /* Drop [2] */ + err =3D btf__permute(btf, permute_ids, 3, NULL); + if (!ASSERT_OK(err, "btf__permute_drop_base_fail")) + goto done; + if (!ASSERT_EQ(btf__type_cnt(btf), 0, "btf__permute_drop_split all")) + goto done; + +done: + btf__free(btf); +} + +/* Verify btf__permute function drops types correctly with split BTF */ +static void test_permute_drop_split(void) +{ + struct btf *split_btf =3D NULL, *base_btf =3D NULL; + __u32 permute_ids[4]; + int err; + + base_btf =3D btf__new_empty(); + if (!ASSERT_OK_PTR(base_btf, "empty_main_btf")) + return; + + btf__add_int(base_btf, "int", 4, BTF_INT_SIGNED); /* [1] int */ + btf__add_ptr(base_btf, 1); /* [2] ptr to int */ + VALIDATE_RAW_BTF( + base_btf, + "[1] INT 'int' size=3D4 bits_offset=3D0 nr_bits=3D32 encoding=3DSIGNED", + "[2] PTR '(anon)' type_id=3D1"); + split_btf =3D btf__new_empty_split(base_btf); + if (!ASSERT_OK_PTR(split_btf, "empty_split_btf")) + goto cleanup; + btf__add_struct(split_btf, "s1", 4); /* [3] struct s1 { */ + btf__add_field(split_btf, "m", 1, 0, 0); /* int m; */ + /* } */ + btf__add_struct(split_btf, "s2", 4); /* [4] struct s2 { */ + btf__add_field(split_btf, "m", 1, 0, 0); /* int m; */ + /* } */ + btf__add_func_proto(split_btf, 1); /* [5] int (*)(int p); */ + btf__add_func_param(split_btf, "p", 2); + btf__add_func(split_btf, "f", BTF_FUNC_STATIC, 5); /* [6] int f(int *p); = */ + + VALIDATE_RAW_BTF( + split_btf, + "[1] INT 'int' size=3D4 bits_offset=3D0 nr_bits=3D32 encoding=3DSIGNED", + "[2] PTR '(anon)' type_id=3D1", + "[3] STRUCT 's1' size=3D4 vlen=3D1\n" + "\t'm' type_id=3D1 bits_offset=3D0", + "[4] STRUCT 's2' size=3D4 vlen=3D1\n" + "\t'm' type_id=3D1 bits_offset=3D0", + "[5] FUNC_PROTO '(anon)' ret_type_id=3D1 vlen=3D1\n" + "\t'p' type_id=3D2", + "[6] FUNC 'f' type_id=3D5 linkage=3Dstatic"); + + /* Drop ID 4 */ + permute_ids[0] =3D 5; /* [3] -> [5] */ + permute_ids[1] =3D 0; /* Drop [4] */ + permute_ids[2] =3D 3; /* [5] -> [3] */ + permute_ids[3] =3D 4; /* [6] -> [4] */ + err =3D btf__permute(split_btf, permute_ids, ARRAY_SIZE(permute_ids), NUL= L); + if (!ASSERT_OK(err, "btf__permute_drop_split")) + goto cleanup; + + VALIDATE_RAW_BTF( + split_btf, + "[1] INT 'int' size=3D4 bits_offset=3D0 nr_bits=3D32 encoding=3DSIGNED", + "[2] PTR '(anon)' type_id=3D1", + "[3] FUNC_PROTO '(anon)' ret_type_id=3D1 vlen=3D1\n" + "\t'p' type_id=3D2", + "[4] FUNC 'f' type_id=3D3 linkage=3Dstatic", + "[5] STRUCT 's1' size=3D4 vlen=3D1\n" + "\t'm' type_id=3D1 bits_offset=3D0"); + + /* Can not drop the type referenced by others */ + permute_ids[0] =3D 0; /* [3] is referenced by [4] */ + permute_ids[1] =3D 4; + permute_ids[2] =3D 3; + err =3D btf__permute(split_btf, permute_ids, 3, NULL); + if (!ASSERT_ERR(err, "btf__permute_drop_split")) + goto cleanup; + + VALIDATE_RAW_BTF( + split_btf, + "[1] INT 'int' size=3D4 bits_offset=3D0 nr_bits=3D32 encoding=3DSIGNED", + "[2] PTR '(anon)' type_id=3D1", + "[3] FUNC_PROTO '(anon)' ret_type_id=3D1 vlen=3D1\n" + "\t'p' type_id=3D2", + "[4] FUNC 'f' type_id=3D3 linkage=3Dstatic", + "[5] STRUCT 's1' size=3D4 vlen=3D1\n" + "\t'm' type_id=3D1 bits_offset=3D0"); + + /* Continue dropping */ + permute_ids[0] =3D 0; /* Drop [3] */ + permute_ids[1] =3D 0; /* Drop [4] */ + permute_ids[2] =3D 3; /* [5] -> [3] */ + err =3D btf__permute(split_btf, permute_ids, 3, NULL); + if (!ASSERT_OK(err, "btf__permute_drop_split")) + goto cleanup; + + VALIDATE_RAW_BTF( + split_btf, + "[1] INT 'int' size=3D4 bits_offset=3D0 nr_bits=3D32 encoding=3DSIGNED", + "[2] PTR '(anon)' type_id=3D1", + "[3] STRUCT 's1' size=3D4 vlen=3D1\n" + "\t'm' type_id=3D1 bits_offset=3D0"); + + /* Continue dropping */ + permute_ids[0] =3D 0; /* Drop [3] */ + err =3D btf__permute(split_btf, permute_ids, 1, NULL); + if (!ASSERT_OK(err, "btf__permute_drop_split")) + goto cleanup; + + VALIDATE_RAW_BTF( + split_btf, + "[1] INT 'int' size=3D4 bits_offset=3D0 nr_bits=3D32 encoding=3DSIGNED", + "[2] PTR '(anon)' type_id=3D1"); + +cleanup: + btf__free(split_btf); + btf__free(base_btf); +} + +/* Verify btf__permute then btf__dedup work correctly */ +static void test_permute_drop_dedup(void) +{ + struct btf *btf, *new_btf; + const struct btf_header *hdr; + const void *btf_data; + char expect_strs[] =3D "\0int\0s1\0m\0tag1\0tag2\0tag3"; + char expect_strs_dedupped[] =3D "\0int\0s1\0m\0tag1"; + __u32 permute_ids[6], btf_size; + int err; + + btf =3D btf__new_empty(); + if (!ASSERT_OK_PTR(btf, "empty_main_btf")) + return; + + btf__add_int(btf, "int", 4, BTF_INT_SIGNED); /* [1] int */ + btf__add_struct(btf, "s1", 4); /* [2] struct s1 { */ + btf__add_field(btf, "m", 1, 0, 0); /* int m; */ + /* } */ + btf__add_decl_tag(btf, "tag1", 2, -1); /* [3] tag -> s1: tag1 */ + btf__add_decl_tag(btf, "tag2", 2, 1); /* [4] tag -> s1/m: tag2 */ + btf__add_decl_tag(btf, "tag3", 2, 1); /* [5] tag -> s1/m: tag3 */ + + VALIDATE_RAW_BTF( + btf, + "[1] INT 'int' size=3D4 bits_offset=3D0 nr_bits=3D32 encoding=3DSIGNED", + "[2] STRUCT 's1' size=3D4 vlen=3D1\n" + "\t'm' type_id=3D1 bits_offset=3D0", + "[3] DECL_TAG 'tag1' type_id=3D2 component_idx=3D-1", + "[4] DECL_TAG 'tag2' type_id=3D2 component_idx=3D1", + "[5] DECL_TAG 'tag3' type_id=3D2 component_idx=3D1"); + + btf_data =3D btf__raw_data(btf, &btf_size); + hdr =3D btf_data; + if (!ASSERT_EQ(hdr->str_len, ARRAY_SIZE(expect_strs), "expect_strs")) + goto done; + + new_btf =3D btf__new(btf_data, btf_size); + if (!ASSERT_OK_PTR(new_btf, "btf__new")) + goto done; + + /* Drop 2 IDs result in unreferenced strings */ + permute_ids[0] =3D 0; + permute_ids[1] =3D 3; /* [1] -> [3] */ + permute_ids[2] =3D 1; /* [2] -> [1] */ + permute_ids[3] =3D 2; /* [3] -> [2] */ + permute_ids[4] =3D 0; /* Drop result in unreferenced "tag2" */ + permute_ids[5] =3D 0; /* Drop result in unreferenced "tag3" */ + err =3D btf__permute(new_btf, permute_ids, ARRAY_SIZE(permute_ids), NULL); + if (!ASSERT_OK(err, "btf__permute")) + goto done; + + VALIDATE_RAW_BTF( + new_btf, + "[1] STRUCT 's1' size=3D4 vlen=3D1\n" + "\t'm' type_id=3D3 bits_offset=3D0", + "[2] DECL_TAG 'tag1' type_id=3D1 component_idx=3D-1", + "[3] INT 'int' size=3D4 bits_offset=3D0 nr_bits=3D32 encoding=3DSIGNED"); + + btf_data =3D btf__raw_data(new_btf, &btf_size); + hdr =3D btf_data; + if (!ASSERT_EQ(hdr->str_len, ARRAY_SIZE(expect_strs), "expect_strs")) + goto done; + + err =3D btf__dedup(new_btf, NULL); + if (!ASSERT_OK(err, "btf__dedup")) + goto done; + + btf_data =3D btf__raw_data(new_btf, &btf_size); + hdr =3D btf_data; + if (!ASSERT_EQ(hdr->str_len, ARRAY_SIZE(expect_strs_dedupped), "expect_st= rs_dedupped")) + goto done; + +done: + btf__free(btf); + btf__free(new_btf); +} + +void test_btf_permute(void) +{ + if (test__start_subtest("permute_base")) + test_permute_base(); + if (test__start_subtest("permute_split")) + test_permute_split(); + if (test__start_subtest("permute_drop_base")) + test_permute_drop_base(); + if (test__start_subtest("permute_drop_split")) + test_permute_drop_split(); + if (test__start_subtest("permute_drop_dedup")) + test_permute_drop_dedup(); +} --=20 2.34.1