From patchwork Fri Dec 9 01:53:00 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Oliver Upton X-Patchwork-Id: 632749 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id C8828C10F1D for ; Fri, 9 Dec 2022 01:53:32 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229783AbiLIBxb (ORCPT ); Thu, 8 Dec 2022 20:53:31 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52982 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229568AbiLIBxa (ORCPT ); Thu, 8 Dec 2022 20:53:30 -0500 Received: from out-182.mta0.migadu.com (out-182.mta0.migadu.com [91.218.175.182]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 995CD941A4 for ; Thu, 8 Dec 2022 17:53:29 -0800 (PST) X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. From: Oliver Upton To: Marc Zyngier , James Morse , Alexandru Elisei , Suzuki K Poulose , Oliver Upton , Paolo Bonzini , Shuah Khan , Sean Christopherson , Andrew Jones , Ricardo Koller Cc: linux-arm-kernel@lists.infradead.org, kvmarm@lists.cs.columbia.edu, kvm@vger.kernel.org, kvmarm@lists.linux.dev, Mark Brown , linux-kselftest@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v2 1/7] KVM: selftests: Fix build due to ucall_uninit() removal Date: Fri, 9 Dec 2022 01:53:00 +0000 Message-Id: <20221209015307.1781352-2-oliver.upton@linux.dev> In-Reply-To: <20221209015307.1781352-1-oliver.upton@linux.dev> References: <20221209015307.1781352-1-oliver.upton@linux.dev> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-kselftest@vger.kernel.org From: Mark Brown Today's -next fails to build on arm64 due to: In file included from include/kvm_util.h:11, from aarch64/page_fault_test.c:15: include/ucall_common.h:36:47: note: expected ‘vm_paddr_t’ {aka ‘long unsigned int’} but argument is of type ‘void *’ 36 | void ucall_init(struct kvm_vm *vm, vm_paddr_t mmio_gpa); | ~~~~~~~~~~~^~~~~~~~ aarch64/page_fault_test.c:725:2: warning: implicit declaration of function ‘ucall_uninit’; did you mean ‘ucall_init’? [-Wimplicit-function-declaration] 725 | ucall_uninit(vm); | ^~~~~~~~~~~~ | ucall_init which is caused by commit interacting poorly with commit 28a65567acb5 ("KVM: selftests: Drop now-unnecessary ucall_uninit()") As is done for other ucall_uninit() users remove the call in the newly added page_fault_test.c. Fixes: 28a65567acb5 ("KVM: selftests: Drop now-unnecessary ucall_uninit()") Fixes: 35c581015712 ("KVM: selftests: aarch64: Add aarch64/page_fault_test") Signed-off-by: Mark Brown Cc: Sean Christopherson Cc: Ricardo Koller Cc: Marc Zyngier Signed-off-by: Oliver Upton --- tools/testing/selftests/kvm/aarch64/page_fault_test.c | 1 - 1 file changed, 1 deletion(-) diff --git a/tools/testing/selftests/kvm/aarch64/page_fault_test.c b/tools/testing/selftests/kvm/aarch64/page_fault_test.c index 0cda70bef5d5..92d3a91153b6 100644 --- a/tools/testing/selftests/kvm/aarch64/page_fault_test.c +++ b/tools/testing/selftests/kvm/aarch64/page_fault_test.c @@ -722,7 +722,6 @@ static void run_test(enum vm_guest_mode mode, void *arg) vcpu_run_loop(vm, vcpu, test); - ucall_uninit(vm); kvm_vm_free(vm); free_uffd(test, pt_uffd, data_uffd); From patchwork Fri Dec 9 01:53:01 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Oliver Upton X-Patchwork-Id: 633812 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 63133C4167B for ; Fri, 9 Dec 2022 01:53:37 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229973AbiLIBxf (ORCPT ); Thu, 8 Dec 2022 20:53:35 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53016 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229950AbiLIBxd (ORCPT ); Thu, 8 Dec 2022 20:53:33 -0500 Received: from out-156.mta0.migadu.com (out-156.mta0.migadu.com [IPv6:2001:41d0:1004:224b::9c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 91C0994191 for ; Thu, 8 Dec 2022 17:53:32 -0800 (PST) X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. From: Oliver Upton To: Marc Zyngier , James Morse , Alexandru Elisei , Suzuki K Poulose , Oliver Upton , Paolo Bonzini , Shuah Khan Cc: linux-arm-kernel@lists.infradead.org, kvmarm@lists.cs.columbia.edu, kvm@vger.kernel.org, kvmarm@lists.linux.dev, Ricardo Koller , Sean Christopherson , linux-kselftest@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v2 2/7] KVM: selftests: Setup ucall after loading program into guest memory Date: Fri, 9 Dec 2022 01:53:01 +0000 Message-Id: <20221209015307.1781352-3-oliver.upton@linux.dev> In-Reply-To: <20221209015307.1781352-1-oliver.upton@linux.dev> References: <20221209015307.1781352-1-oliver.upton@linux.dev> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-kselftest@vger.kernel.org The new ucall infrastructure needs to update a couple of guest globals to pass through the ucall MMIO addr and pool of ucall structs. A precondition of writing to the guest's program image is to have it already loaded into guest memory. Call ucall_init() after kvm_vm_elf_load(). Continue to park the ucall MMIO addr after MEM_REGION_TEST_DATA. Signed-off-by: Oliver Upton --- tools/testing/selftests/kvm/aarch64/page_fault_test.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/tools/testing/selftests/kvm/aarch64/page_fault_test.c b/tools/testing/selftests/kvm/aarch64/page_fault_test.c index 92d3a91153b6..95d22cfb7b41 100644 --- a/tools/testing/selftests/kvm/aarch64/page_fault_test.c +++ b/tools/testing/selftests/kvm/aarch64/page_fault_test.c @@ -609,8 +609,13 @@ static void setup_memslots(struct kvm_vm *vm, struct test_params *p) data_size / guest_page_size, p->test_desc->data_memslot_flags); vm->memslots[MEM_REGION_TEST_DATA] = TEST_DATA_MEMSLOT; +} + +static void setup_ucall(struct kvm_vm *vm) +{ + struct userspace_mem_region *region = vm_get_mem_region(vm, MEM_REGION_TEST_DATA); - ucall_init(vm, data_gpa + data_size); + ucall_init(vm, region->region.guest_phys_addr + region->region.memory_size); } static void setup_default_handlers(struct test_desc *test) @@ -702,6 +707,7 @@ static void run_test(enum vm_guest_mode mode, void *arg) vm = ____vm_create(mode); setup_memslots(vm, p); kvm_vm_elf_load(vm, program_invocation_name); + setup_ucall(vm); vcpu = vm_vcpu_add(vm, 0, guest_code); setup_gva_maps(vm); From patchwork Fri Dec 9 01:53:02 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Oliver Upton X-Patchwork-Id: 632748 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 2E537C10F1D for ; Fri, 9 Dec 2022 01:53:44 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230029AbiLIBxn (ORCPT ); Thu, 8 Dec 2022 20:53:43 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53064 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229990AbiLIBxk (ORCPT ); Thu, 8 Dec 2022 20:53:40 -0500 Received: from out-117.mta0.migadu.com (out-117.mta0.migadu.com [91.218.175.117]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 01A6798EB1 for ; Thu, 8 Dec 2022 17:53:35 -0800 (PST) X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. From: Oliver Upton To: Marc Zyngier , James Morse , Alexandru Elisei , Paolo Bonzini , Shuah Khan , Sean Christopherson , Vitaly Kuznetsov , Andrew Jones Cc: linux-arm-kernel@lists.infradead.org, kvmarm@lists.cs.columbia.edu, kvm@vger.kernel.org, kvmarm@lists.linux.dev, Ricardo Koller , Oliver Upton , linux-kselftest@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v2 3/7] KVM: selftests: Mark correct page as mapped in virt_map() Date: Fri, 9 Dec 2022 01:53:02 +0000 Message-Id: <20221209015307.1781352-4-oliver.upton@linux.dev> In-Reply-To: <20221209015307.1781352-1-oliver.upton@linux.dev> References: <20221209015307.1781352-1-oliver.upton@linux.dev> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-kselftest@vger.kernel.org The loop marks vaddr as mapped after incrementing it by page size, thereby marking the *next* page as mapped. Set the bit in vpages_mapped first instead. Fixes: 56fc7732031d ("KVM: selftests: Fill in vm->vpages_mapped bitmap in virt_map() too") Signed-off-by: Oliver Upton --- tools/testing/selftests/kvm/lib/kvm_util.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tools/testing/selftests/kvm/lib/kvm_util.c b/tools/testing/selftests/kvm/lib/kvm_util.c index e9607eb089be..a256ec67aff6 100644 --- a/tools/testing/selftests/kvm/lib/kvm_util.c +++ b/tools/testing/selftests/kvm/lib/kvm_util.c @@ -1411,10 +1411,10 @@ void virt_map(struct kvm_vm *vm, uint64_t vaddr, uint64_t paddr, while (npages--) { virt_pg_map(vm, vaddr, paddr); + sparsebit_set(vm->vpages_mapped, vaddr >> vm->page_shift); + vaddr += page_size; paddr += page_size; - - sparsebit_set(vm->vpages_mapped, vaddr >> vm->page_shift); } } From patchwork Fri Dec 9 01:53:03 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Oliver Upton X-Patchwork-Id: 633811 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 2257EC4332F for ; Fri, 9 Dec 2022 01:53:53 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229977AbiLIBxw (ORCPT ); Thu, 8 Dec 2022 20:53:52 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53218 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229948AbiLIBxl (ORCPT ); Thu, 8 Dec 2022 20:53:41 -0500 Received: from out-139.mta0.migadu.com (out-139.mta0.migadu.com [91.218.175.139]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 050F4A1984 for ; Thu, 8 Dec 2022 17:53:38 -0800 (PST) X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. From: Oliver Upton To: Marc Zyngier , James Morse , Alexandru Elisei , Paolo Bonzini , Shuah Khan Cc: linux-arm-kernel@lists.infradead.org, kvmarm@lists.cs.columbia.edu, kvm@vger.kernel.org, kvmarm@lists.linux.dev, Ricardo Koller , Sean Christopherson , Oliver Upton , linux-kselftest@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v2 4/7] KVM: selftests: Correctly initialize the VA space for TTBR0_EL1 Date: Fri, 9 Dec 2022 01:53:03 +0000 Message-Id: <20221209015307.1781352-5-oliver.upton@linux.dev> In-Reply-To: <20221209015307.1781352-1-oliver.upton@linux.dev> References: <20221209015307.1781352-1-oliver.upton@linux.dev> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-kselftest@vger.kernel.org An interesting feature of the Arm architecture is that the stage-1 MMU supports two distinct VA regions, controlled by TTBR{0,1}_EL1. As KVM selftests on arm64 only uses TTBR0_EL1, the VA space is constrained to [0, 2^(va_bits)). This is different from other architectures that allow for addressing low and high regions of the VA space from a single page table. KVM selftests' VA space allocator presumes the valid address range is split between low and high memory based the MSB, which of course is a poor match for arm64's TTBR0 region. Add a helper that correctly handles both addressing schemes with a comment describing each. Signed-off-by: Oliver Upton --- .../selftests/kvm/include/kvm_util_base.h | 1 + tools/testing/selftests/kvm/lib/kvm_util.c | 49 ++++++++++++++++--- 2 files changed, 44 insertions(+), 6 deletions(-) diff --git a/tools/testing/selftests/kvm/include/kvm_util_base.h b/tools/testing/selftests/kvm/include/kvm_util_base.h index 6cd86da698b3..b193863d754f 100644 --- a/tools/testing/selftests/kvm/include/kvm_util_base.h +++ b/tools/testing/selftests/kvm/include/kvm_util_base.h @@ -103,6 +103,7 @@ struct kvm_vm { struct sparsebit *vpages_mapped; bool has_irqchip; bool pgd_created; + bool has_split_va_space; vm_paddr_t ucall_mmio_addr; vm_paddr_t pgd; vm_vaddr_t gdt; diff --git a/tools/testing/selftests/kvm/lib/kvm_util.c b/tools/testing/selftests/kvm/lib/kvm_util.c index a256ec67aff6..53d15f32f220 100644 --- a/tools/testing/selftests/kvm/lib/kvm_util.c +++ b/tools/testing/selftests/kvm/lib/kvm_util.c @@ -186,6 +186,43 @@ const struct vm_guest_mode_params vm_guest_mode_params[] = { _Static_assert(sizeof(vm_guest_mode_params)/sizeof(struct vm_guest_mode_params) == NUM_VM_MODES, "Missing new mode params?"); +/* + * Initializes vm->vpages_valid to match the canonical VA space of the + * architecture. + * + * Most architectures split the range addressed by a single page table into a + * low and high region based on the MSB of the VA. On architectures with this + * behavior the VA region spans [0, 2^(va_bits - 1)), [-(2^(va_bits - 1), -1]. + * + * arm64 is a bit different from the rest of the crowd, as the low and high + * regions of the VA space are addressed by distinct paging structures + * (TTBR{0,1}_EL1). KVM selftests on arm64 only uses TTBR0_EL1, meaning that we + * only have a low VA region. As there is no VA split based on the MSB, the VA + * region spans [0, 2^va_bits). + */ +static void vm_vaddr_populate_bitmap(struct kvm_vm *vm) +{ + sparsebit_num_t contig_va_bits = vm->va_bits; + sparsebit_num_t nr_contig_pages; + + /* + * Depending on the architecture, the MSB of the VA could split between + * low and high regions. When that is the case each region has + * va_bits - 1 of address. + */ + if (vm->has_split_va_space) + contig_va_bits--; + + nr_contig_pages = (1ULL << contig_va_bits) >> vm->page_shift; + + sparsebit_set_num(vm->vpages_valid, 0, nr_contig_pages); + + if (vm->has_split_va_space) + sparsebit_set_num(vm->vpages_valid, + -(1ULL << contig_va_bits), + nr_contig_pages); +} + struct kvm_vm *____vm_create(enum vm_guest_mode mode) { struct kvm_vm *vm; @@ -268,17 +305,17 @@ struct kvm_vm *____vm_create(enum vm_guest_mode mode) #ifdef __aarch64__ if (vm->pa_bits != 40) vm->type = KVM_VM_TYPE_ARM_IPA_SIZE(vm->pa_bits); + + /* selftests use TTBR0 only, meaning there is a single VA region. */ + vm->has_split_va_space = false; +#else + vm->has_split_va_space = true; #endif vm_open(vm); - /* Limit to VA-bit canonical virtual addresses. */ vm->vpages_valid = sparsebit_alloc(); - sparsebit_set_num(vm->vpages_valid, - 0, (1ULL << (vm->va_bits - 1)) >> vm->page_shift); - sparsebit_set_num(vm->vpages_valid, - (~((1ULL << (vm->va_bits - 1)) - 1)) >> vm->page_shift, - (1ULL << (vm->va_bits - 1)) >> vm->page_shift); + vm_vaddr_populate_bitmap(vm); /* Limit physical addresses to PA-bits. */ vm->max_gfn = vm_compute_max_gfn(vm); From patchwork Fri Dec 9 01:53:04 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Oliver Upton X-Patchwork-Id: 632747 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 53764C4332F for ; Fri, 9 Dec 2022 01:54:13 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229610AbiLIByK (ORCPT ); Thu, 8 Dec 2022 20:54:10 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53312 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230064AbiLIBxv (ORCPT ); Thu, 8 Dec 2022 20:53:51 -0500 Received: from out-149.mta0.migadu.com (out-149.mta0.migadu.com [IPv6:2001:41d0:1004:224b::95]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id AF97F950FE for ; Thu, 8 Dec 2022 17:53:42 -0800 (PST) X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. From: Oliver Upton To: Marc Zyngier , James Morse , Alexandru Elisei , Suzuki K Poulose , Oliver Upton , Paolo Bonzini , Shuah Khan , Nathan Chancellor , Nick Desaulniers , Tom Rix Cc: linux-arm-kernel@lists.infradead.org, kvmarm@lists.cs.columbia.edu, kvm@vger.kernel.org, kvmarm@lists.linux.dev, Ricardo Koller , Sean Christopherson , linux-kselftest@vger.kernel.org, linux-kernel@vger.kernel.org, llvm@lists.linux.dev Subject: [PATCH v2 5/7] KVM: arm64: selftests: Don't identity map the ucall MMIO hole Date: Fri, 9 Dec 2022 01:53:04 +0000 Message-Id: <20221209015307.1781352-6-oliver.upton@linux.dev> In-Reply-To: <20221209015307.1781352-1-oliver.upton@linux.dev> References: <20221209015307.1781352-1-oliver.upton@linux.dev> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-kselftest@vger.kernel.org Currently the ucall MMIO hole is placed immediately after slot0, which is a relatively safe address in the PA space. However, it is possible that the same address has already been used for something else (like the guest program image) in the VA space. At least in my own testing, building the vgic_irq test with clang leads to the MMIO hole appearing underneath gicv3_ops. Stop identity mapping the MMIO hole and instead find an unused VA to map to it. Yet another subtle detail of the KVM selftests library is that virt_pg_map() does not update vm->vpages_mapped. Switch over to virt_map() instead to guarantee that the chosen VA isn't to something else. Signed-off-by: Oliver Upton --- tools/testing/selftests/kvm/lib/aarch64/ucall.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/tools/testing/selftests/kvm/lib/aarch64/ucall.c b/tools/testing/selftests/kvm/lib/aarch64/ucall.c index 562c16dfbb00..f212bd8ab93d 100644 --- a/tools/testing/selftests/kvm/lib/aarch64/ucall.c +++ b/tools/testing/selftests/kvm/lib/aarch64/ucall.c @@ -14,11 +14,13 @@ static vm_vaddr_t *ucall_exit_mmio_addr; void ucall_arch_init(struct kvm_vm *vm, vm_paddr_t mmio_gpa) { - virt_pg_map(vm, mmio_gpa, mmio_gpa); + vm_vaddr_t mmio_gva = vm_vaddr_unused_gap(vm, vm->page_size, KVM_UTIL_MIN_VADDR); + + virt_map(vm, mmio_gva, mmio_gpa, 1); vm->ucall_mmio_addr = mmio_gpa; - write_guest_global(vm, ucall_exit_mmio_addr, (vm_vaddr_t *)mmio_gpa); + write_guest_global(vm, ucall_exit_mmio_addr, (vm_vaddr_t *)mmio_gva); } void ucall_arch_do_ucall(vm_vaddr_t uc) From patchwork Fri Dec 9 01:53:05 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Oliver Upton X-Patchwork-Id: 633810 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id AD9C7C4332F for ; Fri, 9 Dec 2022 01:54:23 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230149AbiLIByW (ORCPT ); Thu, 8 Dec 2022 20:54:22 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53892 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230106AbiLIBx4 (ORCPT ); Thu, 8 Dec 2022 20:53:56 -0500 Received: from out-173.mta0.migadu.com (out-173.mta0.migadu.com [IPv6:2001:41d0:1004:224b::ad]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C5A27AFCEE for ; Thu, 8 Dec 2022 17:53:45 -0800 (PST) X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. From: Oliver Upton To: Marc Zyngier , James Morse , Alexandru Elisei , Paolo Bonzini , Shuah Khan Cc: linux-arm-kernel@lists.infradead.org, kvmarm@lists.cs.columbia.edu, kvm@vger.kernel.org, kvmarm@lists.linux.dev, Ricardo Koller , Sean Christopherson , Oliver Upton , linux-kselftest@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v2 6/7] KVM: selftests: Allocate ucall pool from MEM_REGION_DATA Date: Fri, 9 Dec 2022 01:53:05 +0000 Message-Id: <20221209015307.1781352-7-oliver.upton@linux.dev> In-Reply-To: <20221209015307.1781352-1-oliver.upton@linux.dev> References: <20221209015307.1781352-1-oliver.upton@linux.dev> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-kselftest@vger.kernel.org MEM_REGION_TEST_DATA is meant to hold data explicitly used by a selftest, not implicit allocations due to the selftests infrastructure. Allocate the ucall pool from MEM_REGION_DATA much like the rest of the selftests library allocations. Reviewed-by: Sean Christopherson Signed-off-by: Oliver Upton --- tools/testing/selftests/kvm/lib/ucall_common.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tools/testing/selftests/kvm/lib/ucall_common.c b/tools/testing/selftests/kvm/lib/ucall_common.c index 820ce6c82829..0cc0971ce60e 100644 --- a/tools/testing/selftests/kvm/lib/ucall_common.c +++ b/tools/testing/selftests/kvm/lib/ucall_common.c @@ -22,7 +22,7 @@ void ucall_init(struct kvm_vm *vm, vm_paddr_t mmio_gpa) vm_vaddr_t vaddr; int i; - vaddr = vm_vaddr_alloc(vm, sizeof(*hdr), KVM_UTIL_MIN_VADDR); + vaddr = __vm_vaddr_alloc(vm, sizeof(*hdr), KVM_UTIL_MIN_VADDR, MEM_REGION_DATA); hdr = (struct ucall_header *)addr_gva2hva(vm, vaddr); memset(hdr, 0, sizeof(*hdr)); From patchwork Fri Dec 9 01:53:06 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Oliver Upton X-Patchwork-Id: 632746 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 41307C4332F for ; Fri, 9 Dec 2022 01:54:35 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230114AbiLIBye (ORCPT ); Thu, 8 Dec 2022 20:54:34 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53584 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230045AbiLIByF (ORCPT ); Thu, 8 Dec 2022 20:54:05 -0500 Received: from out-149.mta0.migadu.com (out-149.mta0.migadu.com [91.218.175.149]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 54481B1051 for ; Thu, 8 Dec 2022 17:53:48 -0800 (PST) X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. From: Oliver Upton To: Marc Zyngier , James Morse , Alexandru Elisei , Paolo Bonzini , Shuah Khan , Andrew Jones , Peter Gonda , Sean Christopherson Cc: linux-arm-kernel@lists.infradead.org, kvmarm@lists.cs.columbia.edu, kvm@vger.kernel.org, kvmarm@lists.linux.dev, Ricardo Koller , Oliver Upton , linux-kselftest@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v2 7/7] KVM: selftests: Avoid infinite loop if ucall_alloc() fails Date: Fri, 9 Dec 2022 01:53:06 +0000 Message-Id: <20221209015307.1781352-8-oliver.upton@linux.dev> In-Reply-To: <20221209015307.1781352-1-oliver.upton@linux.dev> References: <20221209015307.1781352-1-oliver.upton@linux.dev> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-kselftest@vger.kernel.org Guest assertions depend on successfully allocating a ucall structure. As such, the use of guest assertions when ucall_alloc() fails simply leads to an infinite loop in guest code. Use GUEST_UCALL_NONE() to indicate failure instead. Though not technically necessary, use a goto to have a single callsite and an associated comment about why assertions don't work here. It isn't perfect, at least the poor developer gets some signal out of the guest... Fixes: 426729b2cf2e ("KVM: selftests: Add ucall pool based implementation") Signed-off-by: Oliver Upton Reported-by: Oliver Upton Signed-off-by: Sean Christopherson --- tools/testing/selftests/kvm/lib/ucall_common.c | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/tools/testing/selftests/kvm/lib/ucall_common.c b/tools/testing/selftests/kvm/lib/ucall_common.c index 0cc0971ce60e..e8370da3de24 100644 --- a/tools/testing/selftests/kvm/lib/ucall_common.c +++ b/tools/testing/selftests/kvm/lib/ucall_common.c @@ -41,7 +41,8 @@ static struct ucall *ucall_alloc(void) struct ucall *uc; int i; - GUEST_ASSERT(ucall_pool); + if (!ucall_pool) + goto out; for (i = 0; i < KVM_MAX_VCPUS; ++i) { if (!test_and_set_bit(i, ucall_pool->in_use)) { @@ -51,7 +52,14 @@ static struct ucall *ucall_alloc(void) } } - GUEST_ASSERT(0); +out: + /* + * If the guest cannot grab a ucall structure from the pool then the + * only option to get out to userspace is a bare ucall. This is probably + * a good time to mention that guest assertions depend on ucalls with + * arguments too. + */ + GUEST_UCALL_NONE(); return NULL; }