From patchwork Wed Dec 7 21:48:07 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Oliver Upton X-Patchwork-Id: 31062 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:f944:0:0:0:0:0 with SMTP id q4csp419745wrr; Wed, 7 Dec 2022 13:49:51 -0800 (PST) X-Google-Smtp-Source: AA0mqf5nPZmYl5qnjXl0MeZ6FbzScUhYi7LZn8Wjg8kqJcStR1tg7UEMoSpIJg5v8w9/YdQs9DXN X-Received: by 2002:a65:458e:0:b0:477:e813:1795 with SMTP id o14-20020a65458e000000b00477e8131795mr53862537pgq.54.1670449791151; Wed, 07 Dec 2022 13:49:51 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1670449791; cv=none; d=google.com; s=arc-20160816; b=fO0lsNh9cDWLPUfa/3w1LoDd4AQYKDHGdVwxb8618a0tJNYxVTkfKRTn/LLFhmk4GD MZJowosMvu2z4h+RfoBxGIRljnCWKDYSLMkPoyxuosB/NBTXpTSTZzoBv21i1HYLaXXD NHkhvQMpxsKSJ0GMLCuRIc++DikPdMXX8V35GF8NtgzMtup3jn2B6Zd0CaxDdatw5Lhk +9TvXKLQsR5edcp4m7gqG6oQNVGFIBUYfA6W2dqX8Bmfs5HntXtIMuERPghqqoqE7ht+ LsrLQUrXXCqz3r/QwraM1PVBrEpPSLs/0lF4u4vkhhlzSr3+VT1sDM8rEXJxTU2yBS47 j/PA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=JjcLqG29a+AOOV8U9ftW4rJBJFHTcmpsMspP2V/5Cq4=; b=LNyTqN9hJQTvlwz+MBLYWbpVd6DXZTVKr+AEcrpk4+/FJEc2wQJa2hRveLY1LiseQW pScLJY9cUdyrn4Nmmxx5wnGIll6PSDVnAtFkO9pf0RGQYJ2451kFWNCgThK+du2FgVGU FZYmnWYr0fuDaaN7LIfPP87GbowPX7mbwCPGSbKDV/imD8AzZm5HlTB6pRsQvn5bW3uh 79LZVI3Ty16wxYYs3DlTAfCxD9v18Q3gx26urBkfHwuGPM8GNXw5C3jxqIGHyxLd0EIL N2j33U4PDx/jWMGwhJL/8vLY3Zfj+AcG7mAFhKxOm8HSiOmBA30H0ri+tvhG3dhOqWLV 3Ukg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linux.dev header.s=key1 header.b=LZ2KqSYJ; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linux.dev Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id y3-20020a631803000000b00476ddb08ed0si21382661pgl.446.2022.12.07.13.49.37; Wed, 07 Dec 2022 13:49:51 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@linux.dev header.s=key1 header.b=LZ2KqSYJ; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linux.dev Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229984AbiLGVsm (ORCPT + 99 others); Wed, 7 Dec 2022 16:48:42 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37080 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229486AbiLGVsg (ORCPT ); Wed, 7 Dec 2022 16:48:36 -0500 Received: from out2.migadu.com (out2.migadu.com [IPv6:2001:41d0:2:aacc::]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id EE9E084DC4; Wed, 7 Dec 2022 13:48:32 -0800 (PST) X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1670449711; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=JjcLqG29a+AOOV8U9ftW4rJBJFHTcmpsMspP2V/5Cq4=; b=LZ2KqSYJ3rzGKHTpVsL9UIqkvKv36BEDlqIYvZDdPVueuSXGNNtYFARu7DRvMcGEQeTTPm Ri9shjCfD4g/EIdtKic5HUphwv697OSWoj1wK85DQrabSHnkQ04NZnQdxOm2YczZ/8fm79 jG3qTkJ0rfmc8fLyhtF/6DxUvimTBDo= From: Oliver Upton To: Marc Zyngier , James Morse , Alexandru Elisei , Paolo Bonzini , Shuah Khan , Suzuki K Poulose , Oliver Upton 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 3/4] KVM: arm64: selftests: Align VA space allocator with TTBR0 Date: Wed, 7 Dec 2022 21:48:07 +0000 Message-Id: <20221207214809.489070-4-oliver.upton@linux.dev> In-Reply-To: <20221207214809.489070-1-oliver.upton@linux.dev> References: <20221207214809.489070-1-oliver.upton@linux.dev> MIME-Version: 1.0 X-Migadu-Flow: FLOW_OUT X-Spam-Status: No, score=-2.8 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_LOW,SPF_HELO_PASS, SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1751593560088821202?= X-GMAIL-MSGID: =?utf-8?q?1751593560088821202?= 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-1)). 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. Allow architectures to override the default VA space layout. Make use of the override to align vpages_valid with the behavior of TTBR0 on arm64. Signed-off-by: Oliver Upton --- .../testing/selftests/kvm/include/kvm_util_base.h | 1 + .../testing/selftests/kvm/lib/aarch64/processor.c | 10 ++++++++++ tools/testing/selftests/kvm/lib/kvm_util.c | 15 ++++++++++----- 3 files changed, 21 insertions(+), 5 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..fbc2a79369b8 100644 --- a/tools/testing/selftests/kvm/include/kvm_util_base.h +++ b/tools/testing/selftests/kvm/include/kvm_util_base.h @@ -420,6 +420,7 @@ void vm_mem_region_set_flags(struct kvm_vm *vm, uint32_t slot, uint32_t flags); void vm_mem_region_move(struct kvm_vm *vm, uint32_t slot, uint64_t new_gpa); void vm_mem_region_delete(struct kvm_vm *vm, uint32_t slot); struct kvm_vcpu *__vm_vcpu_add(struct kvm_vm *vm, uint32_t vcpu_id); +void vm_populate_vaddr_bitmap(struct kvm_vm *vm); vm_vaddr_t vm_vaddr_unused_gap(struct kvm_vm *vm, size_t sz, vm_vaddr_t vaddr_min); vm_vaddr_t vm_vaddr_alloc(struct kvm_vm *vm, size_t sz, vm_vaddr_t vaddr_min); vm_vaddr_t __vm_vaddr_alloc(struct kvm_vm *vm, size_t sz, vm_vaddr_t vaddr_min, diff --git a/tools/testing/selftests/kvm/lib/aarch64/processor.c b/tools/testing/selftests/kvm/lib/aarch64/processor.c index 316de70db91d..5972a23b2765 100644 --- a/tools/testing/selftests/kvm/lib/aarch64/processor.c +++ b/tools/testing/selftests/kvm/lib/aarch64/processor.c @@ -541,3 +541,13 @@ void kvm_selftest_arch_init(void) */ guest_modes_append_default(); } + +void vm_vaddr_populate_bitmap(struct kvm_vm *vm) +{ + /* + * arm64 selftests use only TTBR0_EL1, meaning that the valid VA space + * is [0, 2^(64 - TCR_EL1.T0SZ)). + */ + sparsebit_set_num(vm->vpages_valid, 0, + (1ULL << vm->va_bits) >> vm->page_shift); +} diff --git a/tools/testing/selftests/kvm/lib/kvm_util.c b/tools/testing/selftests/kvm/lib/kvm_util.c index e9607eb089be..c88c3ace16d2 100644 --- a/tools/testing/selftests/kvm/lib/kvm_util.c +++ b/tools/testing/selftests/kvm/lib/kvm_util.c @@ -186,6 +186,15 @@ 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?"); +__weak void vm_vaddr_populate_bitmap(struct kvm_vm *vm) +{ + 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); +} + struct kvm_vm *____vm_create(enum vm_guest_mode mode) { struct kvm_vm *vm; @@ -274,11 +283,7 @@ struct kvm_vm *____vm_create(enum vm_guest_mode mode) /* 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);