From patchwork Thu Sep 14 01:55:31 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 13384040 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 7A0E9EE0212 for ; Thu, 14 Sep 2023 01:57:13 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 04C5C8D0010; Wed, 13 Sep 2023 21:56:45 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 008288D0001; Wed, 13 Sep 2023 21:56:44 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id D6B138D0010; Wed, 13 Sep 2023 21:56:44 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id C00D58D0001 for ; Wed, 13 Sep 2023 21:56:44 -0400 (EDT) Received: from smtpin12.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 96328140EBC for ; Thu, 14 Sep 2023 01:56:44 +0000 (UTC) X-FDA: 81233539128.12.5E5DBD1 Received: from mail-yb1-f202.google.com (mail-yb1-f202.google.com [209.85.219.202]) by imf19.hostedemail.com (Postfix) with ESMTP id CF4161A0003 for ; Thu, 14 Sep 2023 01:56:42 +0000 (UTC) Authentication-Results: imf19.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=r6dzU3bZ; spf=pass (imf19.hostedemail.com: domain of 3WmgCZQYKCGMTFBOKDHPPHMF.DPNMJOVY-NNLWBDL.PSH@flex--seanjc.bounces.google.com designates 209.85.219.202 as permitted sender) smtp.mailfrom=3WmgCZQYKCGMTFBOKDHPPHMF.DPNMJOVY-NNLWBDL.PSH@flex--seanjc.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1694656602; h=from:from:sender:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=XfRZ+8VXY/OEYZxaTO5L0PdI0q7LErIUsWDHkpQzw6o=; b=bkEeaq2BQNGgtU+SveEznnmI7Yix2sSrwzOmnEBG1yMTLSfRgvkxsBJVZD5HvitZMYA+vW 3OG90mNt28VSAkKJh6E+S2A5ZnS5/JXDTHsDKEFhJinQuqP1Ts3rFUq20bfFBh7G1qgV4Q Lf43R5TNt5hT8DjsGrlq9FfLBCBql2U= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1694656602; a=rsa-sha256; cv=none; b=JOAlhltBsTawpWgzGNOU/R0eyIsm34Oy5C9xF14GwUSzy5NM8a9A2XC7N/YcfMmcywQTuk 8t/s7vp+piJOaFqyBlYt4Vsj9ljZBMivqO6XIqt8qLIyTHrS1sEBUlGZCfBBk4SObB96TK UHyCvqO6vdQfeqegXC1LFHhMuSlyx6o= ARC-Authentication-Results: i=1; imf19.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=r6dzU3bZ; spf=pass (imf19.hostedemail.com: domain of 3WmgCZQYKCGMTFBOKDHPPHMF.DPNMJOVY-NNLWBDL.PSH@flex--seanjc.bounces.google.com designates 209.85.219.202 as permitted sender) smtp.mailfrom=3WmgCZQYKCGMTFBOKDHPPHMF.DPNMJOVY-NNLWBDL.PSH@flex--seanjc.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com Received: by mail-yb1-f202.google.com with SMTP id 3f1490d57ef6-d814a1f7378so2791632276.1 for ; Wed, 13 Sep 2023 18:56:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1694656602; x=1695261402; darn=kvack.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=XfRZ+8VXY/OEYZxaTO5L0PdI0q7LErIUsWDHkpQzw6o=; b=r6dzU3bZpAXmV5oOwdBhy4KnPH98+HqnVYcWpE/d0LKl5bdtt6+Db/7NmhFQQCT2c8 QmnobPuWP7/j58Ab1ZiHekQQ4ZvVRWUwY2kPLm+PQR8jF9eBXc9QZYMKe7yUiNNv79PF t4qYCwU1gGLb5tYq/o8D7anvh9qT1jpEPoE4wInDvKPeGOGcfy8hjmHbTmnSXIHh6Cx3 lgt+wuS2ERC7voOWx4CxMmuT26boYbU7skSIQdDZHmy78zy75vEEIZ5KQS8HcU0dy0mL sP863sdT/TRfcZ9K3mLHVefPGFiGsGVBVy2xbg49qqbDP5Infim3idqb4q5T7DUVzPIY ih7g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1694656602; x=1695261402; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=XfRZ+8VXY/OEYZxaTO5L0PdI0q7LErIUsWDHkpQzw6o=; b=oORVcd7KGS98k8wIgdNfQx6wdzpmS1nRdw+zbPHsl4OD5U0BImfodO06xIXv5oJzN4 TsZZ0bP+/ZCbL70BTyMecmcpHjazlyN2MimFF7l1ysUNt3iLu105vqWV0vD9ooHYybd4 AAU8qT62SLQb1Ui/OjhlqP0lJLgZrrrTS3TF/CHFaflvONxasjaeWCks0mmXBdr3fyAK 81CH6+aWPJ7FTYc8P2V9wbX+LEP6iUknWortYGx8W2tc9st/HP4w0oLmZrpGHQ+/+kYP PhMYa/XQsZjh5Hmbw07eKJl4d9okC9wvfJKv3MxXOBW5I7nIGXPTElDszLOqTb7MNvWV KT9g== X-Gm-Message-State: AOJu0YxIPA5U54NYlYw4GG9w+h1/iKD410GqrvrBOjfjuiM3eacoXAyB Fn3qrE9PhAoWC0kOlBNYaWcQ73D7dwk= X-Google-Smtp-Source: AGHT+IGRS3O+T9+VVevze+Os2k+DiGR8L+K7fuRVrk/vVySDtGkT7cNTInKtJxs4EW7npy+OCmJw4oxYHnE= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a25:d141:0:b0:d77:fb00:b246 with SMTP id i62-20020a25d141000000b00d77fb00b246mr14108ybg.1.1694656602040; Wed, 13 Sep 2023 18:56:42 -0700 (PDT) Reply-To: Sean Christopherson Date: Wed, 13 Sep 2023 18:55:31 -0700 In-Reply-To: <20230914015531.1419405-1-seanjc@google.com> Mime-Version: 1.0 References: <20230914015531.1419405-1-seanjc@google.com> X-Mailer: git-send-email 2.42.0.283.g2d96d420d3-goog Message-ID: <20230914015531.1419405-34-seanjc@google.com> Subject: [RFC PATCH v12 33/33] KVM: selftests: Test KVM exit behavior for private memory/access From: Sean Christopherson To: Paolo Bonzini , Marc Zyngier , Oliver Upton , Huacai Chen , Michael Ellerman , Anup Patel , Paul Walmsley , Palmer Dabbelt , Albert Ou , Sean Christopherson , "Matthew Wilcox (Oracle)" , Andrew Morton , Paul Moore , James Morris , "Serge E. Hallyn" Cc: kvm@vger.kernel.org, linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, linux-mips@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, kvm-riscv@lists.infradead.org, linux-riscv@lists.infradead.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-security-module@vger.kernel.org, linux-kernel@vger.kernel.org, Chao Peng , Fuad Tabba , Jarkko Sakkinen , Anish Moorthy , Yu Zhang , Isaku Yamahata , Xu Yilun , Vlastimil Babka , Vishal Annapurve , Ackerley Tng , Maciej Szmigiero , David Hildenbrand , Quentin Perret , Michael Roth , Wang , Liam Merwick , Isaku Yamahata , "Kirill A . Shutemov" X-Stat-Signature: z6hpysqffy7s8b9ugq83f81b3twqyjbo X-Rspamd-Server: rspam10 X-Rspamd-Queue-Id: CF4161A0003 X-Rspam-User: X-HE-Tag: 1694656602-536103 X-HE-Meta: U2FsdGVkX1+wos18vnygIABHFfG/5AS0O9OWqWemClSgeGxzlePJD2P8jM6zovpYRPvXCzs5zW0sT0IGXzwM54SnYp1AHvGPNHiuPAUgeevflWJjA8DMEsQsq+0ip7xBjbWuz6FDSugZi196D6PiXJ7l47jRO7Qf2fjPfCJEk4sVwxtM0IdJ0xtYf+RTjnS0bmye6MzaiDSWuYMcgoMy+tsJ6BH5WMflaCdurp5xx9FkTgjYOvBdbu0KQNkZvYBHPfFnTGKnvtGCT8nhZqqtzFiR3P4gPHMrjpmxgkCHrnqU03/2qfkrUk2So0I6vhUC5m286RVgPIwPPeSQAnbxqRpIKdesDKsbNe4CZGTXoI+xhSbxzciO+0Vw9hXn39x11boKx3oF6ocnCYHRyxs2ulq8yVOO7oqr2sValSprXBm8NqXH4m6upHyhiPGJnCP0fL/Pg9t8UUZh67nTYW31MytOZdyDau3B0jrY4RH5O0zjyYbPFj39GKC0WapsaiUIGurMko0CCr44RkgjSNO69agQf4C/+Kwx02NetN/HiQrPNv8/ycpPZ6XnzRJeRsMsR/Jjw8hwdbywGD/XOiz8qIEphpsOp71bmha8kchADumZgEADdY9GXW1brSdeVJkheVDHxQDflDvAAgM4439tkV//L4XDowzrqErnQmPG8MOZiZCxSD9gByhWKakR1XY6gy/s+xMa/UBQlht53qoEKaa9R6Wuzf3eNfdC9585SGC4zQAsrAEOzK1w+f3hv8M+OLZPnNC7alTw/7a7Vifm5IKpVcexryat8Q1uCpCucdKjnR5XEiBJVfRvQ+c5ccWfpUGtbUIr4+IB83XrBCxfuLj73bbUNy8GfgLIDSaLAWYeX1X/2WNtaAAVi0I2ZwwQLXdWLhkAbeY+XoeWjC7FrULqgGg0SWu74yGbV8YLLkaY5xsGGag4solDIiwVxQyK0bZIRJ5tvHa4m1xqxbM ytGdu3x3 f7lBcJafH754Kt7bwRwJVCTmg3VHzDFneGxbQ4nlFNPmE8WFhyzD3hBPjLg8G9p/jjSyzK/5gge2xCgrUYQICzy4mbfsbZUPFTxsDFsQRZ0kQ8L5ZhdKWPf9kTjlAIKQkCPawmjNHeFygZZJlkEmhhaG/lmiSBdo2tJ7XemDrzmnyc0LrUmMefNiR8nBib0BO+50WvSZy0puMEisDQlxMOoXb8ICLbmhNYvYsRWSgrMJvVM1rDKwMnSLsaRBGV7cQp82swNbjKPzhb2NkrZVcqNOUL7JVlf6OeJ3A/glGeO5H9OSmPl056DHMYPj0VfGPkpZANJIthkKxoxEQTEoo9NmBCa44c42MrauNRVvQioRPFZ4JPY0xcY3pUA4MjgH5IcSTBCyQp8/XiFahAgzGND0KUAjbwqfbNgsyDeG9aTb1muEiR3JE1gpm6Z74fcfvr9oLkCjz+1PekMlsk2PAwYj4NOftbemTqVW6JkUlUBUU2PPc8EAsMMwJSPYRKaoWozfRliVmvK4tom4CEHQd8ZDSTL+MhSFk84GW8OnFIxw2XJOn3Zwnfxap9dbFCaN6tsZirqZ26pOW830dHC0GA/geoH1KCVbhF4M7qL0pIlgytm3dgUa3tdveivNzAht4m/4FB91+1TEwC6WZJICWW2ebPcsPHOa4ofyXJenpWmP3QSErSyJ3BuZvwB4yS4Nz7AgAMOLN++kfULhIoulS+usPcXOJnTk8EsSSRLWIei+6keeI2OldiNW4ynz7/fsie616vHFD1l5GaAU= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: From: Ackerley Tng "Testing private access when memslot gets deleted" tests the behavior of KVM when a private memslot gets deleted while the VM is using the private memslot. When KVM looks up the deleted (slot = NULL) memslot, KVM should exit to userspace with KVM_EXIT_MEMORY_FAULT. In the second test, upon a private access to non-private memslot, KVM should also exit to userspace with KVM_EXIT_MEMORY_FAULT. sean: These testcases belong in set_memory_region_test.c, they're private variants on existing testscases and aren't as robust, e.g. don't ensure the vCPU is actually running and accessing memory when converting and deleting. Signed-off-by: Ackerley Tng Signed-off-by: Sean Christopherson --- tools/testing/selftests/kvm/Makefile | 1 + .../kvm/x86_64/private_mem_kvm_exits_test.c | 121 ++++++++++++++++++ 2 files changed, 122 insertions(+) create mode 100644 tools/testing/selftests/kvm/x86_64/private_mem_kvm_exits_test.c diff --git a/tools/testing/selftests/kvm/Makefile b/tools/testing/selftests/kvm/Makefile index 2b1ef809d73a..f7fdd8244547 100644 --- a/tools/testing/selftests/kvm/Makefile +++ b/tools/testing/selftests/kvm/Makefile @@ -82,6 +82,7 @@ TEST_GEN_PROGS_x86_64 += x86_64/nested_exceptions_test TEST_GEN_PROGS_x86_64 += x86_64/platform_info_test TEST_GEN_PROGS_x86_64 += x86_64/pmu_event_filter_test TEST_GEN_PROGS_x86_64 += x86_64/private_mem_conversions_test +TEST_GEN_PROGS_x86_64 += x86_64/private_mem_kvm_exits_test TEST_GEN_PROGS_x86_64 += x86_64/set_boot_cpu_id TEST_GEN_PROGS_x86_64 += x86_64/set_sregs_test TEST_GEN_PROGS_x86_64 += x86_64/smaller_maxphyaddr_emulation_test diff --git a/tools/testing/selftests/kvm/x86_64/private_mem_kvm_exits_test.c b/tools/testing/selftests/kvm/x86_64/private_mem_kvm_exits_test.c new file mode 100644 index 000000000000..1a61c51c2390 --- /dev/null +++ b/tools/testing/selftests/kvm/x86_64/private_mem_kvm_exits_test.c @@ -0,0 +1,121 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * Copyright (C) 2022, Google LLC. + */ +#include +#include +#include + +#include "kvm_util.h" +#include "processor.h" +#include "test_util.h" + +/* Arbitrarily selected to avoid overlaps with anything else */ +#define EXITS_TEST_GVA 0xc0000000 +#define EXITS_TEST_GPA EXITS_TEST_GVA +#define EXITS_TEST_NPAGES 1 +#define EXITS_TEST_SIZE (EXITS_TEST_NPAGES * PAGE_SIZE) +#define EXITS_TEST_SLOT 10 + +static uint64_t guest_repeatedly_read(void) +{ + volatile uint64_t value; + + while (true) + value = *((uint64_t *) EXITS_TEST_GVA); + + return value; +} + +static uint32_t run_vcpu_get_exit_reason(struct kvm_vcpu *vcpu) +{ + int r; + + r = _vcpu_run(vcpu); + if (r) { + TEST_ASSERT(errno == EFAULT, KVM_IOCTL_ERROR(KVM_RUN, r)); + TEST_ASSERT_EQ(vcpu->run->exit_reason, KVM_EXIT_MEMORY_FAULT); + } + return vcpu->run->exit_reason; +} + +const struct vm_shape protected_vm_shape = { + .mode = VM_MODE_DEFAULT, + .type = KVM_X86_SW_PROTECTED_VM, +}; + +static void test_private_access_memslot_deleted(void) +{ + struct kvm_vm *vm; + struct kvm_vcpu *vcpu; + pthread_t vm_thread; + void *thread_return; + uint32_t exit_reason; + + vm = vm_create_shape_with_one_vcpu(protected_vm_shape, &vcpu, + guest_repeatedly_read); + + vm_userspace_mem_region_add(vm, VM_MEM_SRC_ANONYMOUS, + EXITS_TEST_GPA, EXITS_TEST_SLOT, + EXITS_TEST_NPAGES, + KVM_MEM_PRIVATE); + + virt_map(vm, EXITS_TEST_GVA, EXITS_TEST_GPA, EXITS_TEST_NPAGES); + + /* Request to access page privately */ + vm_mem_set_private(vm, EXITS_TEST_GPA, EXITS_TEST_SIZE); + + pthread_create(&vm_thread, NULL, + (void *(*)(void *))run_vcpu_get_exit_reason, + (void *)vcpu); + + vm_mem_region_delete(vm, EXITS_TEST_SLOT); + + pthread_join(vm_thread, &thread_return); + exit_reason = (uint32_t)(uint64_t)thread_return; + + TEST_ASSERT_EQ(exit_reason, KVM_EXIT_MEMORY_FAULT); + TEST_ASSERT_EQ(vcpu->run->memory_fault.flags, KVM_MEMORY_EXIT_FLAG_PRIVATE); + TEST_ASSERT_EQ(vcpu->run->memory_fault.gpa, EXITS_TEST_GPA); + TEST_ASSERT_EQ(vcpu->run->memory_fault.size, EXITS_TEST_SIZE); + + kvm_vm_free(vm); +} + +static void test_private_access_memslot_not_private(void) +{ + struct kvm_vm *vm; + struct kvm_vcpu *vcpu; + uint32_t exit_reason; + + vm = vm_create_shape_with_one_vcpu(protected_vm_shape, &vcpu, + guest_repeatedly_read); + + /* Add a non-private memslot (flags = 0) */ + vm_userspace_mem_region_add(vm, VM_MEM_SRC_ANONYMOUS, + EXITS_TEST_GPA, EXITS_TEST_SLOT, + EXITS_TEST_NPAGES, 0); + + virt_map(vm, EXITS_TEST_GVA, EXITS_TEST_GPA, EXITS_TEST_NPAGES); + + /* Request to access page privately */ + vm_mem_set_private(vm, EXITS_TEST_GPA, EXITS_TEST_SIZE); + + exit_reason = run_vcpu_get_exit_reason(vcpu); + + TEST_ASSERT_EQ(exit_reason, KVM_EXIT_MEMORY_FAULT); + TEST_ASSERT_EQ(vcpu->run->memory_fault.flags, KVM_MEMORY_EXIT_FLAG_PRIVATE); + TEST_ASSERT_EQ(vcpu->run->memory_fault.gpa, EXITS_TEST_GPA); + TEST_ASSERT_EQ(vcpu->run->memory_fault.size, EXITS_TEST_SIZE); + + kvm_vm_free(vm); +} + +int main(int argc, char *argv[]) +{ + TEST_REQUIRE(kvm_has_cap(KVM_CAP_GUEST_MEMFD)); + TEST_REQUIRE(kvm_check_cap(KVM_CAP_VM_TYPES) & BIT(KVM_X86_SW_PROTECTED_VM)); + + test_private_access_memslot_deleted(); + test_private_access_memslot_not_private(); +}