From patchwork Fri Oct 27 18:22:17 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 13438961 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 1BCA0C25B47 for ; Fri, 27 Oct 2023 18:23:57 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 70D758002A; Fri, 27 Oct 2023 14:23:38 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 6961D80018; Fri, 27 Oct 2023 14:23:38 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 44BC08002A; Fri, 27 Oct 2023 14:23:38 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 2D27C80018 for ; Fri, 27 Oct 2023 14:23:38 -0400 (EDT) Received: from smtpin02.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 0E56240779 for ; Fri, 27 Oct 2023 18:23:38 +0000 (UTC) X-FDA: 81392064516.02.522D8DF Received: from mail-pl1-f201.google.com (mail-pl1-f201.google.com [209.85.214.201]) by imf12.hostedemail.com (Postfix) with ESMTP id 293EB4001D for ; Fri, 27 Oct 2023 18:23:35 +0000 (UTC) Authentication-Results: imf12.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=wcxg8mMk; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf12.hostedemail.com: domain of 3JwA8ZQYKCEg2okxtmqyyqvo.mywvsx47-wwu5kmu.y1q@flex--seanjc.bounces.google.com designates 209.85.214.201 as permitted sender) smtp.mailfrom=3JwA8ZQYKCEg2okxtmqyyqvo.mywvsx47-wwu5kmu.y1q@flex--seanjc.bounces.google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1698431016; 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=srZUoBPWZiKpBpFDSV5l36Sp6ql4zJFMGAxv8lh5kbY=; b=F4j/CN7drUWzUPy3DItfuqq1xDY5w8fxUY0Og0zvvwruBKngluG0qyScMy/WeWFYI3i+1b uB8nj3x9Qf0u9G0P1zI07ZVJH5/tzJRNgMmYpGiFsRC/rcdaEisHNvxF7LRiDb3p6+yWeE lpDKFLtj/VAiHWUkTpyQkqMpwGfGRVE= ARC-Authentication-Results: i=1; imf12.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=wcxg8mMk; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf12.hostedemail.com: domain of 3JwA8ZQYKCEg2okxtmqyyqvo.mywvsx47-wwu5kmu.y1q@flex--seanjc.bounces.google.com designates 209.85.214.201 as permitted sender) smtp.mailfrom=3JwA8ZQYKCEg2okxtmqyyqvo.mywvsx47-wwu5kmu.y1q@flex--seanjc.bounces.google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1698431016; a=rsa-sha256; cv=none; b=Dfx4F8ldLCkP/mTcwL62aeeE76Jb/z9V8Bfn0HjK0TCitJ9QY8M/MORJlZi/0asmRUKVxz Avo3VzvLScbXgSPBD7r8CyLmhnHO6IMS8gEV7BKY2Hzh/lOrY/lQfylP/RFBuQ3UYWV+Yr T1QTj5Y8NOhdHKWZ0xE/6ozYD/l8aqI= Received: by mail-pl1-f201.google.com with SMTP id d9443c01a7336-1c9bc9e6a89so21621945ad.0 for ; Fri, 27 Oct 2023 11:23:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1698431015; x=1699035815; 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=srZUoBPWZiKpBpFDSV5l36Sp6ql4zJFMGAxv8lh5kbY=; b=wcxg8mMkahgGd5AEGJZUNJ8OwuJis/ASg1M5AGFlf2uzxLHItMYZDsZ7sz7LylT6Hb sg9F7IWKjZyMG62bPsIgFnFKoNFPomgCfPG1ZU9JpnLaps1sPBPBkljCECVajLNiaybq 0Q6V/tDenEgzZ6Vs/cMtc5DRFXozLIYM4UF0OByzhDJoZCfhjiKdCwKAxeu5l2TmGDZ4 J73EyjzYJLcSc8Er10ovVTVH+ih1e1hZbKPgQuTqfy6s8NhZ46/EXRvBtjHOApev3wNC 38eLz/qsY3e5BAMoGDzlMhwGncZfpsrrHdlbT7VUvhOsc/Skfi2aC8YuH4DmBgVohnA3 KsNw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698431015; x=1699035815; 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=srZUoBPWZiKpBpFDSV5l36Sp6ql4zJFMGAxv8lh5kbY=; b=xMWI1BWjsMhoGN3I1iEUv6cF9RoZXIV9tXu/Tl/ytxGuCAaW/XVOXGuO6X9sXFms0o 1CPTs/i8w2NFv6+xq8dv3WAMI8QAltDMkLf5S77ZKKvyI/5Uk5tKRKS7YBbZvF7UXqVv vjTDS78hMTtPBzu8U4ZS0jor9BFmED2Rnl31p76fO1R5vImTgYd847lvhZeJc4iT6whi V+mfqmR8G+q025Xq4NlsFFolc+vwHv8La9I7c+HFs1nu+baOMDiHKA4iIeaxN+RES0VW cpR19UnUFfjQAKkO++m8zoSaXKOArtDcCdR7i6A871kyQWSV9/F+z7lX2xicXbMza5Fv 8PDw== X-Gm-Message-State: AOJu0Yxd2JbJWJp+8lW0FIPrp/17RWA6qjslk1qcS5fuhBUVXoXi6sxd zDfDVxIHdHIj/a0XfIX9mN8j3ssfw9Y= X-Google-Smtp-Source: AGHT+IGsD3GBqEIwxpRuMZy6ZOrqAgR6Um5/oe2tLnBOXdqBPG31+kuimftnouCNE0Glk06NnY7NvSmeibY= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a17:903:442:b0:1cc:1e05:e0e7 with SMTP id iw2-20020a170903044200b001cc1e05e0e7mr49290plb.2.1698431015053; Fri, 27 Oct 2023 11:23:35 -0700 (PDT) Reply-To: Sean Christopherson Date: Fri, 27 Oct 2023 11:22:17 -0700 In-Reply-To: <20231027182217.3615211-1-seanjc@google.com> Mime-Version: 1.0 References: <20231027182217.3615211-1-seanjc@google.com> X-Mailer: git-send-email 2.42.0.820.g83a721a137-goog Message-ID: <20231027182217.3615211-36-seanjc@google.com> Subject: [PATCH v13 35/35] 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 , Alexander Viro , Christian Brauner , "Matthew Wilcox (Oracle)" , Andrew Morton 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-kernel@vger.kernel.org, Xiaoyao Li , Xu Yilun , Chao Peng , Fuad Tabba , Jarkko Sakkinen , Anish Moorthy , David Matlack , Yu Zhang , Isaku Yamahata , " =?utf-8?q?Micka=C3=ABl_Sala?= =?utf-8?q?=C3=BCn?= " , Vlastimil Babka , Vishal Annapurve , Ackerley Tng , Maciej Szmigiero , David Hildenbrand , Quentin Perret , Michael Roth , Wang , Liam Merwick , Isaku Yamahata , "Kirill A . Shutemov" X-Rspam-User: X-Stat-Signature: giqs8i3njbbqi68bw5cncma6tg311kmb X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: 293EB4001D X-HE-Tag: 1698431015-212508 X-HE-Meta: U2FsdGVkX1/Th+wX+tUwvf6h9kDjs7SAwbTBAqkISady2r7lw//Rp9mit4IhNf3iRfGstWNpV7Mk8MuOVKRcCL0CRmc+irjeoHkN9xZ6ueh2I+3PZAx+W8AgTd3+SoVZCBWTXGEvjsSfz88cuuci18gY3XuCx4lH0IMwyWfNu6MhYUe9SKjIqi4UpaILulePg9WT+tZ+Zmbn445xyVs3hFC7zV05JaYjrfy9VGE2LTHH/ge28hE9gKRbHiUjeKXa0qTXdM+k6ohI0tFWYnwFgQDDRRe/xSslCwJJjiVerA3OYzLM60o9WIGRaUy/Zj9+YRoXShi8fynIePysubD8S9dxJA2NKjJBQiAml/DY36SxT8OplqUVovBOz+djEKYBhj7k9vQz4+3crgPne3ybJH8joU7WzTRSOTRn9Y0tdCeZt2QM/KgHVlI8mjnaVoNsumXpMiIy39DHh8ptkFdLRHilpeQC9ZjBRcKwEhWf0B7LDBOCfIPr/8JOMk6r1KA5CM5zfB1SA8YClGEHgGF+9yBBPNB5Zd6zdPfSfSYHHN2cwRupF8/anGuiCLQYt4JWtx4i2i5GxLIB49wbYNVHZY34xKJz20frTB6PeAL8/1LTI9CVO0Quohpz/sNCAUPxJdfdFvXZocnzphfY4jvkMa89uOsrfAancq+SZFLpHln18zCqULhIhkLF4sPvg6KMUYTpRtjFeKmB5+DtKoPP2V5Etw/Y8Iml1f0acLnHPpN6UE5N1COweQnlMPGY3N/AnBYqQbfty+ZZApEwWdrNkYAznu4AthuVM0XUFm6PqTPyoZXM0DscAgPsLrVF/VVakx8PWjmwxmDYhQMtRQKzd7umNEydHmkZU21PD6ohWK95OSUo4QxxLWkd6KZFV9FFYU4Soxfu/Gj8zchnTVft2nn20vtMF5srAZ+k8ygqMBswa9czHUQSbbTUbI6YUXcVfHkoiPt08hMeuWYvKWi 0SpZBTLg bmPHKVIIqUtsnPqciQUj7H1XCcUSMsZ3h0DdioeqAWAR9gtcJ6VywQZ9NHTwIRyRntVojLIeT2ovmA7K0W/S4FGg1Ftby9d7IEcRCVz0MHbcHuNj54wD5V/902pjySRMbaSS5+SbLfS8ETMkELLUissaBc7iQHPrMG/Jk7tPpQIdnNPpMBRx1PBIcaAYowgSNt4b8H9797Xdctq/4Zg6jcprLnoiIusX01Xe9O51IONXHTxfq5hlMgE3lHbN+RGP2ntGzO6dpTkkwuGpSFgKYHTNcvHRHMymVzGIsEq0io/JUsK3fTq8JWdWq5LbFEy+O4WhbSW4H7UKJua8Q9o/FgIgTG2CR5ffjJeJLZl3a+OzQSHYd47ybpzvo0fEUWiI+QGiOGSEaDYr9GBgbfHcX5vcwLfYcY7yLJAaSVseykU+dl2THp9evfbfwb4UVBSm0UPSiPCz6eHY2SlZDBOSgYNabkXqgvBtLb7kkH9nLVX+7tTv6T3Zszj17GK2uK6+Hi3PwrJouphXv36pehbJZrKhWTPRhR/LIccc/b2Sh8a4D38nGf8YKV48tKtbOr5z8uPNu7xo62nDdfsJ1LMuygTC3R4UV5U/9qX1b1OIKxPMJ6zEIl0seOzmI0op8zs5gr1ap8Xr7WBAyI2y7t2d7baQUkop+9vcRT3lUzAlcvtydSRLayNuTw698iCg6RoyS5tXFScaX2nM3KqOFtRnZx+PCY+oS03Cih2t2K7OU836L0hD0sncJuKez03eqlOTMkE/Kz7kCu8Bc/b8= 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: List-Subscribe: List-Unsubscribe: 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. Intentionally don't take a requirement on KVM_CAP_GUEST_MEMFD, KVM_CAP_MEMORY_FAULT_INFO, KVM_MEMORY_ATTRIBUTE_PRIVATE, etc., as it's a KVM bug to advertise KVM_X86_SW_PROTECTED_VM without its prerequisites. Signed-off-by: Ackerley Tng [sean: call out the similarities with set_memory_region_test] Signed-off-by: Sean Christopherson --- tools/testing/selftests/kvm/Makefile | 1 + .../kvm/x86_64/private_mem_kvm_exits_test.c | 120 ++++++++++++++++++ 2 files changed, 121 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..7f7ca4475745 --- /dev/null +++ b/tools/testing/selftests/kvm/x86_64/private_mem_kvm_exits_test.c @@ -0,0 +1,120 @@ +// 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_check_cap(KVM_CAP_VM_TYPES) & BIT(KVM_X86_SW_PROTECTED_VM)); + + test_private_access_memslot_deleted(); + test_private_access_memslot_not_private(); +}