From patchwork Fri Jul 12 23:44:04 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yonghong Song X-Patchwork-Id: 13732233 X-Patchwork-Delegate: bpf@iogearbox.net Received: from 66-220-155-179.mail-mxout.facebook.com (66-220-155-179.mail-mxout.facebook.com [66.220.155.179]) (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 F3D1573501 for ; Fri, 12 Jul 2024 23:44:15 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=66.220.155.179 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720827857; cv=none; b=HXipJWr2R03ijWrc21a/JA3wezp70SvrMh2g3Vx2nyAie5uRTMDNgAREA1fprDrVaf86Uqh45kZ4Riemdz9XpMRWbGVzKdhO4iKa6gkMS2qkmd0ydcNsvtDxNx0+pG02h22dc0rCsqZPxagloTtHtIf6aGZLrTEaY+xV53N5aLE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720827857; c=relaxed/simple; bh=2SD9UZGiJHpfsSe7bJx8VQwUpBh+9s0J/NOp74mTcJE=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=EJY19oJE4oQtnBd1nMfp6xo14HkcbXhnhMhTSJND6FVG3uXEPL4xkRnr52bDbdlg0fYAcDXVIaUIY4iU38A8t2rIt2n1gMSHygbXkQ8UVDvmmUfiuuBmq0gVNiQrghZRQdVGJ4jvS7lZYzQbVyc+RHaHqjHmxh91maRmP89k1Zo= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=fail (p=none dis=none) header.from=linux.dev; spf=fail smtp.mailfrom=linux.dev; arc=none smtp.client-ip=66.220.155.179 Authentication-Results: smtp.subspace.kernel.org; dmarc=fail (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=fail smtp.mailfrom=linux.dev Received: by devbig309.ftw3.facebook.com (Postfix, from userid 128203) id 33D1E6856305; Fri, 12 Jul 2024 16:44:04 -0700 (PDT) From: Yonghong Song To: bpf@vger.kernel.org Cc: Alexei Starovoitov , Andrii Nakryiko , Daniel Borkmann , kernel-team@fb.com, Martin KaFai Lau Subject: [PATCH bpf-next v3 2/2] selftests/bpf: Add ldsx selftests for ldsx and subreg compare Date: Fri, 12 Jul 2024 16:44:04 -0700 Message-ID: <20240712234404.288115-1-yonghong.song@linux.dev> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240712234359.287698-1-yonghong.song@linux.dev> References: <20240712234359.287698-1-yonghong.song@linux.dev> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: bpf@iogearbox.net Add a few selftests to test 32/16/8-bit ldsx followed by subreg comparison. Without the previous patch, all added tests will fail. Signed-off-by: Yonghong Song --- .../selftests/bpf/progs/verifier_ldsx.c | 106 ++++++++++++++++++ 1 file changed, 106 insertions(+) diff --git a/tools/testing/selftests/bpf/progs/verifier_ldsx.c b/tools/testing/selftests/bpf/progs/verifier_ldsx.c index d4427d8e1217..3b96a9436a0c 100644 --- a/tools/testing/selftests/bpf/progs/verifier_ldsx.c +++ b/tools/testing/selftests/bpf/progs/verifier_ldsx.c @@ -144,6 +144,112 @@ __naked void ldsx_s32_range(void) : __clobber_all); } +#define MAX_ENTRIES 12 + +struct test_val { + int foo[MAX_ENTRIES]; +}; + +struct { + __uint(type, BPF_MAP_TYPE_HASH); + __uint(max_entries, 1); + __type(key, long long); + __type(value, struct test_val); +} map_hash_48b SEC(".maps"); + +SEC("socket") +__description("LDSX, S8, subreg compare") +__success __success_unpriv +__naked void ldsx_s8_subreg_compare(void) +{ + asm volatile ( + "call %[bpf_get_prandom_u32];" + "*(u64 *)(r10 - 8) = r0;" + "w6 = w0;" + "if w6 > 0x1f goto l0_%=;" + "r7 = *(s8 *)(r10 - 8);" + "if w7 > w6 goto l0_%=;" + "r1 = 0;" + "*(u64 *)(r10 - 8) = r1;" + "r2 = r10;" + "r2 += -8;" + "r1 = %[map_hash_48b] ll;" + "call %[bpf_map_lookup_elem];" + "if r0 == 0 goto l0_%=;" + "r0 += r7;" + "*(u64 *)(r0 + 0) = 1;" +"l0_%=:" + "r0 = 0;" + "exit;" + : + : __imm(bpf_get_prandom_u32), + __imm_addr(map_hash_48b), + __imm(bpf_map_lookup_elem) + : __clobber_all); +} + +SEC("socket") +__description("LDSX, S16, subreg compare") +__success __success_unpriv +__naked void ldsx_s16_subreg_compare(void) +{ + asm volatile ( + "call %[bpf_get_prandom_u32];" + "*(u64 *)(r10 - 8) = r0;" + "w6 = w0;" + "if w6 > 0x1f goto l0_%=;" + "r7 = *(s16 *)(r10 - 8);" + "if w7 > w6 goto l0_%=;" + "r1 = 0;" + "*(u64 *)(r10 - 8) = r1;" + "r2 = r10;" + "r2 += -8;" + "r1 = %[map_hash_48b] ll;" + "call %[bpf_map_lookup_elem];" + "if r0 == 0 goto l0_%=;" + "r0 += r7;" + "*(u64 *)(r0 + 0) = 1;" +"l0_%=:" + "r0 = 0;" + "exit;" + : + : __imm(bpf_get_prandom_u32), + __imm_addr(map_hash_48b), + __imm(bpf_map_lookup_elem) + : __clobber_all); +} + +SEC("socket") +__description("LDSX, S32, subreg compare") +__success __success_unpriv +__naked void ldsx_s32_subreg_compare(void) +{ + asm volatile ( + "call %[bpf_get_prandom_u32];" + "*(u64 *)(r10 - 8) = r0;" + "w6 = w0;" + "if w6 > 0x1f goto l0_%=;" + "r7 = *(s32 *)(r10 - 8);" + "if w7 > w6 goto l0_%=;" + "r1 = 0;" + "*(u64 *)(r10 - 8) = r1;" + "r2 = r10;" + "r2 += -8;" + "r1 = %[map_hash_48b] ll;" + "call %[bpf_map_lookup_elem];" + "if r0 == 0 goto l0_%=;" + "r0 += r7;" + "*(u64 *)(r0 + 0) = 1;" +"l0_%=:" + "r0 = 0;" + "exit;" + : + : __imm(bpf_get_prandom_u32), + __imm_addr(map_hash_48b), + __imm(bpf_map_lookup_elem) + : __clobber_all); +} + #else SEC("socket")