From nobody Mon Feb 9 03:45:36 2026 Received: from mail-pl1-f172.google.com (mail-pl1-f172.google.com [209.85.214.172]) (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 90FA033C53A for ; Fri, 9 Jan 2026 13:00:16 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.172 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1767963619; cv=none; b=t0/6bbSjD2wQg7VLqiveP4cVNiPPTR68HEyo6Kx98zopEbgtfdbMdE8hR0EZJxre5fcL+YNfxv/WdmlQv0Y5v/USMbhfrA9XWmwhP16gEf0cXZ05K6ObEROXTrWZj0Nb7koPunnhw9mOhp1tEFtH+brpG4rEjE+/EVyUBETQae8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1767963619; c=relaxed/simple; bh=uMiIaTuHoGp8t2XP9PDz6WtnuLCS6v4VMQ6JPWkBmY4=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=sCjhegu9aWzKc5ToNzjiinradFevKln08hMcFVeM6WlPuOrFgtOQco6u6kGjYPJMZtXFV5Ht3UU3HIjV206uJWZgPIRf2wK/eoiB6YOZetvCrmIyVX2YDorG1yr41oa3ySpeH//yXpAHQJeD6ieGlmgRsmPwykjNdij5XNT39pU= 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=ecM4lOQD; arc=none smtp.client-ip=209.85.214.172 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="ecM4lOQD" Received: by mail-pl1-f172.google.com with SMTP id d9443c01a7336-2a081c163b0so30772915ad.0 for ; Fri, 09 Jan 2026 05:00:16 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1767963616; x=1768568416; 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=CLf4MD/8vWtFsjd7VfVNcjK4gNe3ynQJ9Qc+27+cU9U=; b=ecM4lOQD1dlVHQAwW9Kjrizm73duzuA7G9IWNr4S24nmYrj4pDSrral0v1oYsjCea3 Mft+xWCKu6ZxjRrlYZHF3PsTCLMBTmitdvJLJrX7AbvJLJcA7riWkkibxd1C0IAqYr8d 7dwLyDcOu++iow1KadTChv/aSKRwha8sMf6ccnnkEGd8wB9jQyeEy/+3rsGdR0xnxTVc I5saWhbPRAuMaS9R9enc5brbqfcg6h5fQPh8iHAhcLTZBEmr1CwJksN6zsHDzKZM9+pv kIMcdV8OsEf+a/TjSQi/dy/Uk4qf56OG9oPcG4xmgdWhfrYXuIHUR+FC1c7kwmoUP05j Lfpw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1767963616; x=1768568416; 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=CLf4MD/8vWtFsjd7VfVNcjK4gNe3ynQJ9Qc+27+cU9U=; b=AawKfb0+g00tXX27C59JeadBvfY+nPrTaN3kxfQsZyvKMaLW1Q9m/SN6XHCvaJ02vR BXuxTG6AB1/GEViZ+nxCitmnCCFHqi+bOEPwHSnwKgCudwUKQ3hVAYSmTYi2dBMa0yLi FlVOu37klx9L/umLmXdEYKLjtfMi3+A6DUuMPq84TxINZcx8dHdNBm6ekfWHh0QFWZxP 7ZVQUiCNtQTOGdMUt+r570kYjsZUkhLb6PUMr3vlRs3OoLCHdjZhP+TRCVkEFUdko04a IQCGQ5lJLTvcgcWsmFgqIE4IJqgNyDjt91q8Tqgtr0RdYzbw1V2/Y62arSXzf2cgjWGy O60w== X-Forwarded-Encrypted: i=1; AJvYcCV2O/dJ75qyGdiQ7hcq0YIk30VnSYIzkHJdCYlb5ijXgEzFPR6tIjep7vTzuZNB4rXyZf6ibBG5kv/20eM=@vger.kernel.org X-Gm-Message-State: AOJu0YyN6pkMCDNtNeEZU94L+P2B6Ms5O50FZI5zcLnnppVNX+O0epD/ H93TR+BOr8P3Z1EGdFF5x3vIIMrxfeftObobQy4adTWBP29MEwI/Zv51 X-Gm-Gg: AY/fxX7vVMUr3cJ2qAuy2FMuKaTVa4c8Bjw5eK4/6DNloAan1Ya6R4Gh1JfZ9nC9lzH M+GU5WCP0UIVK2AjTzlk9jQRbdWtrgWRxpBO8nGjLjGRi0hZW6QFOyQz6bUf+HfS1W/wh2eAL7r zgAZw//TDwg73DRLgyXYvD7GoyoJoJmT1LC5kReN72ceQClGk1BwmG5O4cTVzwt4yYR4y6ChJF2 M3PfaF2Q40CmnEqBByuTNXsGMstMoQOhZFZJhXgA5Q3m/e+unp3XDvsfbSC2Dtn+9EMQVOuPshU +8HtIN0CeDkiAkKXAnhUy2kbC1Y812hjwl7Ggi2UYR3PswunfV4oXR6JexjqkXBsznQVkyjEmeB CU/W8p6kyMlC04UwqDFZ0YVo7mgtAroZATDdJZWOC+H9N4bSp7ZTr/du/LojDfh/AyKQyEQ8kl+ oFx7+M3n5nWWNPaEkH8wTxG7Pg7JE= X-Google-Smtp-Source: AGHT+IHUbv9WRCZStjAecPrTyLJ6XpZmmO3OJTH9Yv044JkqHu4Qb0zoqrFjdl0rbPYyVoyrUN7Tow== X-Received: by 2002:a17:903:3d0f:b0:2a0:81c1:6194 with SMTP id d9443c01a7336-2a3ee49c2a5mr99381295ad.47.1767963615302; Fri, 09 Jan 2026 05:00:15 -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.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 09 Jan 2026 05:00:14 -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 02/11] selftests/bpf: Add test cases for btf__permute functionality Date: Fri, 9 Jan 2026 20:59:54 +0800 Message-Id: <20260109130003.3313716-3-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 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: Donglin Peng Acked-by: Eduard Zingerman --- .../selftests/bpf/prog_tests/btf_permute.c | 244 ++++++++++++++++++ 1 file changed, 244 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..04ade5ad77ac --- /dev/null +++ b/tools/testing/selftests/bpf/prog_tests/btf_permute.c @@ -0,0 +1,244 @@ +// SPDX-License-Identifier: GPL-2.0 +/* Copyright (c) 2026 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 works as expected in the base-BTF scenario */ +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; /* [0] -> [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; + permute_base_check(btf); + + /* ids[0] must be 0 for base BTF */ + permute_ids[0] =3D 4; /* [0] -> [0] */ + permute_ids[1] =3D 0; /* [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_ERR(err, "btf__permute_base")) + goto done; + /* BTF is not modified */ + permute_base_check(btf); + + /* id_map_cnt is invalid */ + permute_ids[0] =3D 0; /* [0] -> [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) - 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[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; + /* BTF is not modified */ + permute_base_check(btf); + + /* 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; + /* 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 works as expected in the split-BTF scenario */ +static void test_permute_split(void) +{ + struct btf *split_btf =3D NULL, *base_btf =3D NULL; + __u32 permute_ids[4]; + int err, 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, ARRAY_SIZE(permute_ids) - 1,= 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, ARRAY_SIZE(permute_ids), NUL= L); + 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, ARRAY_SIZE(permute_ids), NUL= L); + 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