Received: by 2002:a05:6358:1087:b0:cb:c9d3:cd90 with SMTP id j7csp77231rwi; Tue, 18 Oct 2022 14:22:37 -0700 (PDT) X-Google-Smtp-Source: AMsMyM7seryb8in7E17LppLeG0B6hBOnFx1yP6ujKtzVyTOzUUi60AVzE3dBufFiiwhKH2FxeaZz X-Received: by 2002:a17:907:7e95:b0:78d:e9cf:82c7 with SMTP id qb21-20020a1709077e9500b0078de9cf82c7mr4130883ejc.724.1666128157042; Tue, 18 Oct 2022 14:22:37 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1666128157; cv=none; d=google.com; s=arc-20160816; b=UzCNDNWKS37MgYBpw+rwf8i/4jlgmWXG0S8QgtqNvtV79OT/chvqUPEQStveZq8hgf fiPeIO1gfyuvostq6pQWlDevDgMYZV2pdYXQ9m3DSZ+90n6e4KX819mFBjyvnFVtPG6M hqbwGBKrLA1ukcVOXuO81tqCpM0ENK3MgvVr3NV6RI0Kc0gptFDHq8nFBMzh6H5MuEXf sKqBWlhVqJ+n2kP6d5IY93lX202a9d/09XXlAR2ZrUgbfT68gsg8y/xMnFWQ/hw+u9oS VtUGt7ufnd1cZznVqLqGd3/6qjWAdEcGr3qNsOhsrZ2gBL7xbK3pZ3D5BVP8UhgCaY+o MUCA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:from:subject:references:mime-version :message-id:in-reply-to:date:dkim-signature; bh=aZEM2Q9nzB7T1LpqHIaGf2vJIOKjOHavkTCUOr3ZRUg=; b=lm2DOBPcF5ieUjoH7jpnzwRfm/735aJBgtLbOWYFQztUxyR8SsiX4RmSkSo/0fHHyO t4uC3N35eUBgUP/VlmUCTtlFYqOSH2lcBRNJr7CEbEPRZ3Xce2W2/OSKIrQcNRRhKiVS WGNvBHBkv9DGT+3SVVsriRh/yfRw8XYqTk229ehtPu87lcooNMSvI8a8p99VVjx06VKw hlttDnEoVmGowZ3LGRrnRo9dapZdf93PAxiHztJjvCyf5UVmURx/sxxj/r1sywq6e3dy XsBBg28o4jj0Rg44yNHtiNIPgh8qS/gw4RWFnjB+RA4Qf7Ym8J9/8OzDIv/3eaVtx7AO gv4Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=L8xBPJIb; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id i4-20020a170906698400b00783d969f318si11418884ejr.253.2022.10.18.14.22.11; Tue, 18 Oct 2022 14:22:37 -0700 (PDT) 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=@google.com header.s=20210112 header.b=L8xBPJIb; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230220AbiJRU7L (ORCPT + 99 others); Tue, 18 Oct 2022 16:59:11 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33754 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230180AbiJRU7E (ORCPT ); Tue, 18 Oct 2022 16:59:04 -0400 Received: from mail-pl1-x64a.google.com (mail-pl1-x64a.google.com [IPv6:2607:f8b0:4864:20::64a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 82EB7C09B7 for ; Tue, 18 Oct 2022 13:59:02 -0700 (PDT) Received: by mail-pl1-x64a.google.com with SMTP id k16-20020a170902c41000b00184987e3d09so10553294plk.21 for ; Tue, 18 Oct 2022 13:59:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:references:mime-version:message-id:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=aZEM2Q9nzB7T1LpqHIaGf2vJIOKjOHavkTCUOr3ZRUg=; b=L8xBPJIbH2fFvWt3Nn0n5TviDwSCrB7kaYUmdmF7LrZ0bSGXPMRvjU/VXc//G0Nw9K MC83S93NdveyiStgpg8eMNHtClOJoKDefasKGeN5aIDu+gfB8i7Vh08g0BUPwb3W/aMI 1jQoKMxEquGWBcNf1zul641XBvckpW/PE9a4Kd9S+OVgsjvQlneCdvH2W54FAslGLSYy H6Ydqme6FfNJG0MJBM5GTCCm3i0GlKUrdwRpP8i443kd97ec9EJKWHLU58zTI4zkSn9e r4oG/B9JpvhkcC6rsWS0wEIpg9LSyQgtRrek6tgIA07D0n2ILqVuCP1yAegWB4zZqJFY 6VjA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:from:subject:references:mime-version:message-id:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=aZEM2Q9nzB7T1LpqHIaGf2vJIOKjOHavkTCUOr3ZRUg=; b=hTtZ0QbKL7qHKDPvkHycqqm9pxzcfQ2HfbOkJH06BoFPl6o1nnde8BJB7P+Rx65Mpi kDS/0BKuC/TrZk/CafUrAlhrIRJSfadGAPKZWfyOgcg/JcYhY9orIo9koUfmng+j8+KJ AZzONI/WipA8pBRFP/S/4FoiF7wxuiq+idNrXrhJMC5p+fh8SRE8i/vOfA5UNHqZ/efu PcrUZRtyTnddIVQVYjAS+Uk9dx3ZL7I5m/LGxDo0ETln0ckDxwCVNGSjJzWNVOUEillY HEBT0IIgHjJ8rqilPQh/fsobE42zD7PPPB8SxBnpcMNqkbD29LRzYW1lXoijs+XT61TP vd7w== X-Gm-Message-State: ACrzQf13zX3Et9ssC2IWXouprFPVpAnHUfd9/thHANPKVvowfJD5Qwcu 8ZBmTMUKjTgySQL3wd3vO6ZEVyjA9OA= X-Received: from pgonda1.kir.corp.google.com ([2620:15c:29:203:c89b:7f49:3437:9db8]) (user=pgonda job=sendgmr) by 2002:a17:902:cec8:b0:185:505b:95da with SMTP id d8-20020a170902cec800b00185505b95damr4875973plg.83.1666126741535; Tue, 18 Oct 2022 13:59:01 -0700 (PDT) Date: Tue, 18 Oct 2022 13:58:42 -0700 In-Reply-To: <20221018205845.770121-1-pgonda@google.com> Message-Id: <20221018205845.770121-5-pgonda@google.com> Mime-Version: 1.0 References: <20221018205845.770121-1-pgonda@google.com> X-Mailer: git-send-email 2.38.0.413.g74048e4d9e-goog Subject: [PATCH V5 4/7] KVM: selftests: add support for protected vm_vaddr_* allocations From: Peter Gonda To: kvm@vger.kernel.org Cc: linux-kernel@vger.kernel.org, marcorr@google.com, seanjc@google.com, michael.roth@amd.com, thomas.lendacky@amd.com, joro@8bytes.org, mizhang@google.com, pbonzini@redhat.com, andrew.jones@linux.dev, pgonda@google.com, vannapurve@google.com Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-9.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS,USER_IN_DEF_DKIM_WL autolearn=unavailable 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 From: Michael Roth Test programs may wish to allocate shared vaddrs for things like sharing memory with the guest. Since protected vms will have their memory encrypted by default an interface is needed to explicitly request shared pages. Implement this by splitting the common code out from vm_vaddr_alloc() and introducing a new vm_vaddr_alloc_shared(). Signed-off-by: Michael Roth Signed-off-by: Peter Gonda --- .../selftests/kvm/include/kvm_util_base.h | 1 + tools/testing/selftests/kvm/lib/kvm_util.c | 21 +++++++++++++++---- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/tools/testing/selftests/kvm/include/kvm_util_base.h b/tools/testing/selftests/kvm/include/kvm_util_base.h index 9aacc6110d09..4224026fbe25 100644 --- a/tools/testing/selftests/kvm/include/kvm_util_base.h +++ b/tools/testing/selftests/kvm/include/kvm_util_base.h @@ -396,6 +396,7 @@ 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); vm_vaddr_t vm_vaddr_alloc(struct kvm_vm *vm, size_t sz, vm_vaddr_t vaddr_min); +vm_vaddr_t vm_vaddr_alloc_shared(struct kvm_vm *vm, size_t sz, vm_vaddr_t vaddr_min); vm_vaddr_t vm_vaddr_alloc_pages(struct kvm_vm *vm, int nr_pages); vm_vaddr_t vm_vaddr_alloc_page(struct kvm_vm *vm); diff --git a/tools/testing/selftests/kvm/lib/kvm_util.c b/tools/testing/selftests/kvm/lib/kvm_util.c index f5f18a802434..d753345993d6 100644 --- a/tools/testing/selftests/kvm/lib/kvm_util.c +++ b/tools/testing/selftests/kvm/lib/kvm_util.c @@ -1231,12 +1231,13 @@ static vm_vaddr_t vm_vaddr_unused_gap(struct kvm_vm *vm, size_t sz, } /* - * VM Virtual Address Allocate + * VM Virtual Address Allocate Shared/Encrypted * * Input Args: * vm - Virtual Machine * sz - Size in bytes * vaddr_min - Minimum starting virtual address + * encrypt - Whether the region should be handled as encrypted * * Output Args: None * @@ -1249,13 +1250,15 @@ static vm_vaddr_t vm_vaddr_unused_gap(struct kvm_vm *vm, size_t sz, * a unique set of pages, with the minimum real allocation being at least * a page. */ -vm_vaddr_t vm_vaddr_alloc(struct kvm_vm *vm, size_t sz, vm_vaddr_t vaddr_min) +static vm_vaddr_t +_vm_vaddr_alloc(struct kvm_vm *vm, size_t sz, vm_vaddr_t vaddr_min, bool encrypt) { uint64_t pages = (sz >> vm->page_shift) + ((sz % vm->page_size) != 0); virt_pgd_alloc(vm); - vm_paddr_t paddr = vm_phy_pages_alloc(vm, pages, - KVM_UTIL_MIN_PFN * vm->page_size, 0); + vm_paddr_t paddr = _vm_phy_pages_alloc(vm, pages, + KVM_UTIL_MIN_PFN * vm->page_size, + 0, encrypt); /* * Find an unused range of virtual page addresses of at least @@ -1276,6 +1279,16 @@ vm_vaddr_t vm_vaddr_alloc(struct kvm_vm *vm, size_t sz, vm_vaddr_t vaddr_min) return vaddr_start; } +vm_vaddr_t vm_vaddr_alloc(struct kvm_vm *vm, size_t sz, vm_vaddr_t vaddr_min) +{ + return _vm_vaddr_alloc(vm, sz, vaddr_min, vm->protected); +} + +vm_vaddr_t vm_vaddr_alloc_shared(struct kvm_vm *vm, size_t sz, vm_vaddr_t vaddr_min) +{ + return _vm_vaddr_alloc(vm, sz, vaddr_min, false); +} + /* * VM Virtual Address Allocate Pages * -- 2.38.0.413.g74048e4d9e-goog