From nobody Tue Dec 2 00:01:06 2025 Received: from mail-pf1-f169.google.com (mail-pf1-f169.google.com [209.85.210.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 A2C6E31C584 for ; Wed, 26 Nov 2025 08:50:39 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.169 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1764147041; cv=none; b=NnKRDrho1Dh+KH75M1Y9u6Ee9pwpnFKlx0dBGiXpSNW2NzbEWD23LhkLSs1IFMaV63l5MGyJ84MGujqavHlcS5QQeAeHlWTt1YXEQ/+uTKEucWACoWbiQJH+bDQas6FUhsBU0xFbCjeHUw9uE+IZKVI5QQcU9xaMEAhXsNfQozM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1764147041; c=relaxed/simple; bh=7ngEULL678quRrsmJ39omINLf8wh8oMlVDKhnztkP/E=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=teknj2gcSYSp69ozWkHzqFjOa7iHTsMcnKD81Pd01rMUwTUzE5yZ+hyGVY0CkvP6ptVZ8CCEoasjiXlddSt2mIm4owRyriTcUrnYayegaCH78n/uIHOXGWa2VC6pSJwBUYdoUI9GIffe0/ChUaQn6I2Gg2ygM3KNYLyMzDcva3w= 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=cPvyhXsn; arc=none smtp.client-ip=209.85.210.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="cPvyhXsn" Received: by mail-pf1-f169.google.com with SMTP id d2e1a72fcca58-7b9c17dd591so5680951b3a.3 for ; Wed, 26 Nov 2025 00:50:39 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1764147039; x=1764751839; 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=9qbxRcU+iIJswxukru9rEL0qIQsiT5P658wY0LXDT54=; b=cPvyhXsnzE7RioeKynOfRhL8tOXWtpCqlRdEkdyslycqWPZCNN4pubWDC9xnJBFWW4 9bdKMU+5tmiWgRJJ5rAdfpWkR8kCVHnyx1EE3+P3zUpogqWFsnEcVU+Nc7uFmJQflq85 aqVBBnhO21CWZhDWO+f+jvW4XES8kxJwCbuhzxQrqP+4IqhSwjxBtNj8Qsfr9w+RW/uU qjsZA78pP1kzzLKiCqlRX0bCB6DgpLd29F6mg2w79boTMqqaVpA0gl6zmChEOloK/7yJ m1g58KONwQ+NXAsPQgT6vTGqwDmyOQQ0d3W+Hoo6TN2lJ45drW9V31WukXexVTYyk2c+ sf+w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1764147039; x=1764751839; 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=9qbxRcU+iIJswxukru9rEL0qIQsiT5P658wY0LXDT54=; b=t6rTx0ut9xP3n1/0TNtJd+5nr30m+5hLofYyMoncgej+b5Xx9k1fOy/AsPjCpUcBzE WOHLLErhRhxjepwwUkhxDcrOgIAag1J7GJg4oGMdRIWi7PnBc1Y4riv7J5xcQYMXmvA7 CSAPZ5xuNzE4N+drYeErH+Bc9nflUuQ3sCeY1KPNPoYIRF6fJROXqU29RR4pima9nNqk ptKuGDo19oVSiLoVNoUEbaXSPQ7xTPgo/I+n+CcvRVCMB+jKiQHN1y/4ux90xkLULIh/ m9/WOp9FdIAH3u1s3Y3H8sEu9OFE0E9DqYBwaM/AhW6rkFYiRZOuhLF1OhwASKqXhKfM EHSw== X-Forwarded-Encrypted: i=1; AJvYcCWYav5BieyGtwsp7vheOjvK5CrnQGRwvECWGGBR4c0tsqPirxLRHEGmLfJHs9m5DlIRMljaq32/qD1cbKU=@vger.kernel.org X-Gm-Message-State: AOJu0YzxhnfFp+BkY0xxtC7m7HtGTPI8udBiibkpFSQRIqD5Spw1XPvN Egw+sTj3oc44jaZ1eh7J+AYqjWlpMKen5UnqgGctVVqTfUDnKfxu4q0t X-Gm-Gg: ASbGnct26sraYQJXLqTeyP042XskoV1kijQPCrC/kcvX2tlB1dWUBTRa+Cm60KtkvcC /ukMD2ySYIokauWraXQJwqS/Lv/i4GaXOzxiBxdbOnUiy5DX+zoSyilfB+nDOI9RZvZIChOUbrz C0Q/5TzR1X1Pq5lqBniDZOrZgz9BDZwN7prB4H5PcsaOnN/cHGKKnOzGifmo24gGg9qs/UKQzez 7Z9RGDgCgBF1cj3C07hTZUndVMt2bUZwQBL4FlJeEO0ZXsyZ645nee1hW+gMdGXBAnLn9aAbskt CSwqJ2hdoVXgxN/Wh1y9SjHHeT228pQLNHCJNuxynAqV827jHDByWysW214LNq8F+78kjLhSnTj m084BPSMvrzopGQYWhMKdU9lMTcYwZMw9Yw82zLqPZt//6qREWYwI5C3nNnD6WPw4Wn2EfL47H9 sfA71+ZqIntmPLL4fQi7OaFirrdjQ= X-Google-Smtp-Source: AGHT+IHqvZLYppoBK2yVx1vDc9XKQk7gBLgQKYgen0ASnKXiIzwkQYbXBtvlVFuNZZBz+61W4qzx4w== X-Received: by 2002:a05:6a00:c83:b0:7b9:7f18:c716 with SMTP id d2e1a72fcca58-7ca8740ef7emr6830252b3a.1.1764147038864; Wed, 26 Nov 2025 00:50:38 -0800 (PST) Received: from pengdl-pc.mioffice.cn ([43.224.245.249]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-7c3f023fd82sm20885721b3a.42.2025.11.26.00.50.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 26 Nov 2025 00:50:37 -0800 (PST) From: Donglin Peng To: ast@kernel.org, andrii.nakryiko@gmail.com Cc: eddyz87@gmail.com, zhangxiaoqin@xiaomi.com, ihor.solodrai@linux.dev, linux-kernel@vger.kernel.org, bpf@vger.kernel.org, pengdonglin , Alan Maguire Subject: [RFC bpf-next v8 2/9] selftests/bpf: Add test cases for btf__permute functionality Date: Wed, 26 Nov 2025 16:50:18 +0800 Message-Id: <20251126085025.784288-3-dolinux.peng@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20251126085025.784288-1-dolinux.peng@gmail.com> References: <20251126085025.784288-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 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 Cc: Eduard Zingerman Cc: Alexei Starovoitov Cc: Andrii Nakryiko Cc: Alan Maguire Cc: Ihor Solodrai Cc: Xiaoqin Zhang Signed-off-by: pengdonglin --- .../selftests/bpf/prog_tests/btf_permute.c | 228 ++++++++++++++++++ 1 file changed, 228 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..9aa71cdf984a --- /dev/null +++ b/tools/testing/selftests/bpf/prog_tests/btf_permute.c @@ -0,0 +1,228 @@ +// SPDX-License-Identifier: GPL-2.0 +/* Copyright (c) 2025 Xiaomi */ + +#include +#include +#include "btf_helpers.h" + +static void permute_base_check(struct btf *btf) +{ + 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"); +} + +/* Ensure btf__permute work as expected with base BTF */ +static void test_permute_base(void) +{ + struct btf *btf; + __u32 permute_ids[6]; + int start_id =3D 1; + 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[1 - start_id] =3D 4; /* [1] -> [4] */ + permute_ids[2 - start_id] =3D 3; /* [2] -> [3] */ + permute_ids[3 - start_id] =3D 5; /* [3] -> [5] */ + permute_ids[4 - start_id] =3D 1; /* [4] -> [1] */ + permute_ids[5 - start_id] =3D 6; /* [5] -> [6] */ + permute_ids[6 - start_id] =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; + permute_base_check(btf); + + /* id_map_cnt is invalid */ + permute_ids[1 - start_id] =3D 4; /* [1] -> [4] */ + permute_ids[2 - start_id] =3D 3; /* [2] -> [3] */ + permute_ids[3 - start_id] =3D 5; /* [3] -> [5] */ + permute_ids[4 - start_id] =3D 1; /* [4] -> [1] */ + permute_ids[5 - start_id] =3D 6; /* [5] -> [6] */ + permute_ids[6 - start_id] =3D 2; /* [6] -> [2] */ + err =3D btf__permute(btf, permute_ids, ARRAY_SIZE(permute_ids) - 1, NULL); + if (!ASSERT_ERR(err, "btf__permute_base")) + goto done; + /* BTF is not modified */ + permute_base_check(btf); + + /* Multiple types can not be mapped to the same ID */ + permute_ids[1 - start_id] =3D 4; + permute_ids[2 - start_id] =3D 4; + permute_ids[3 - start_id] =3D 5; + permute_ids[4 - start_id] =3D 1; + permute_ids[5 - start_id] =3D 6; + permute_ids[6 - start_id] =3D 2; + err =3D btf__permute(btf, permute_ids, ARRAY_SIZE(permute_ids), NULL); + if (!ASSERT_ERR(err, "btf__permute_base")) + goto done; + /* BTF is not modified */ + permute_base_check(btf); + + /* Type ID must be valid */ + permute_ids[1 - start_id] =3D 4; + permute_ids[2 - start_id] =3D 3; + permute_ids[3 - start_id] =3D 5; + permute_ids[4 - start_id] =3D 1; + permute_ids[5 - start_id] =3D 7; + permute_ids[6 - start_id] =3D 2; + err =3D btf__permute(btf, permute_ids, ARRAY_SIZE(permute_ids), NULL); + if (!ASSERT_ERR(err, "btf__permute_base")) + goto done; + /* BTF is not modified */ + permute_base_check(btf); + +done: + btf__free(btf); +} + +static void permute_split_check(struct btf *btf) +{ + 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 '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"); +} + +/* 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; + int start_id; + + 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"); + + start_id =3D btf__type_cnt(base_btf); + permute_ids[3 - start_id] =3D 6; /* [3] -> [6] */ + permute_ids[4 - start_id] =3D 3; /* [4] -> [3] */ + permute_ids[5 - start_id] =3D 5; /* [5] -> [5] */ + permute_ids[6 - start_id] =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; + permute_split_check(split_btf); + + /* + * For split BTF, id_map_cnt must equal to the number of types + * added on top of base BTF + */ + permute_ids[3 - start_id] =3D 4; + permute_ids[4 - start_id] =3D 3; + permute_ids[5 - start_id] =3D 5; + permute_ids[6 - start_id] =3D 6; + err =3D btf__permute(split_btf, permute_ids, 3, NULL); + if (!ASSERT_ERR(err, "btf__permute_split")) + goto cleanup; + /* BTF is not modified */ + permute_split_check(split_btf); + + /* Multiple types can not be mapped to the same ID */ + permute_ids[3 - start_id] =3D 4; + permute_ids[4 - start_id] =3D 3; + permute_ids[5 - start_id] =3D 3; + permute_ids[6 - start_id] =3D 6; + err =3D btf__permute(split_btf, permute_ids, 4, NULL); + if (!ASSERT_ERR(err, "btf__permute_split")) + goto cleanup; + /* BTF is not modified */ + permute_split_check(split_btf); + + /* Can not map to base ID */ + permute_ids[3 - start_id] =3D 4; + permute_ids[4 - start_id] =3D 2; + permute_ids[5 - start_id] =3D 5; + permute_ids[6 - start_id] =3D 6; + err =3D btf__permute(split_btf, permute_ids, 4, NULL); + if (!ASSERT_ERR(err, "btf__permute_split")) + goto cleanup; + /* BTF is not modified */ + permute_split_check(split_btf); + +cleanup: + btf__free(split_btf); + btf__free(base_btf); +} + +void test_btf_permute(void) +{ + if (test__start_subtest("permute_base")) + test_permute_base(); + if (test__start_subtest("permute_split")) + test_permute_split(); +} --=20 2.34.1