From patchwork Wed Jul 10 04:29:20 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yonghong Song X-Patchwork-Id: 13728853 X-Patchwork-Delegate: bpf@iogearbox.net Received: from 69-171-232-180.mail-mxout.facebook.com (69-171-232-180.mail-mxout.facebook.com [69.171.232.180]) (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 799D1210FF for ; Wed, 10 Jul 2024 04:29:34 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=69.171.232.180 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720585776; cv=none; b=TQ/GUw5q3xAkRNpuZyARvohCe7iYmYZSYjrCBwRYwQPJsjUhgUdC01K/nF8XDNg3Ydbq5qughptR6R6Vp3aK+ZUc4y54L68dpZaNHspjw8HxxHBjOOlngHCaKOywSuej7Fl/RQuTBizpgWCjra0Zgb3wtypsfjBmZvyRfsGgRtA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720585776; c=relaxed/simple; bh=2SD9UZGiJHpfsSe7bJx8VQwUpBh+9s0J/NOp74mTcJE=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=T6vC7q5vfi+ilv1RhxK0DnuF8ceygifr9b0jt7Bi42T03AXMXru6AkS7qb6kamEaclZ+DQz7ywFbWg3jr0b9TvQvTbaHoQPht2kL99KfwHCH+epvXmU+ayYPTUVetOK/CDT5HI7V+WDxpIgV6VAQ4iWk48P9vXst7SuSXBHUNYI= 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=69.171.232.180 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 A914866A8CF9; Tue, 9 Jul 2024 21:29:20 -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 2/2] selftests/bpf: Add ldsx selftests for ldsx and subreg compare Date: Tue, 9 Jul 2024 21:29:20 -0700 Message-ID: <20240710042920.1212221-1-yonghong.song@linux.dev> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240710042915.1211933-1-yonghong.song@linux.dev> References: <20240710042915.1211933-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")