From patchwork Wed Oct 13 15:58:31 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Quentin Perret X-Patchwork-Id: 12556285 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 mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id CC73CC433F5 for ; Wed, 13 Oct 2021 16:10:55 +0000 (UTC) Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 8B1EA6109F for ; Wed, 13 Oct 2021 16:10:55 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org 8B1EA6109F Authentication-Results: mail.kernel.org; dmarc=fail (p=reject dis=none) header.from=google.com Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=lists.infradead.org DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:Cc:To:From:Subject:References: Mime-Version:Message-Id:In-Reply-To:Date:Reply-To:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Owner; bh=PUxJCRJ9fIMWhSn0iLY9oZkmWjf/olQLiABfCSJIbtk=; b=cUKZlfxu237DB2cX2nQfDDEOmI pUtkBKk/3D4DNuVZ4wOInSTYsitXibGEY0VNh9q6mFAuP2Sj4EIjF2S/jbCbktkdgMdaDrk6iaiRM hEkAuVBxjbYfwl+xMQVLe3fiAMw86CcDhbTYrGlaiR/KhLvqRFFFdfWHmC7cxhDAe2rERuR6trYHQ 92WAWvBulAQZ1MTGJBYpGpHP7WvP38lT+p7ulo2CW8Ush7g/H8xpsjs/4Q3tSOe8yq6uleyvPF429 A9ILNOhrJWTtnCvIcIDcg6CAD7W3ogiLxMMgCPUad38KGFxLP/z4G53nkbtpaqkYER8SK+MC3Iwot tFYf3vFg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1magnO-00HXAb-Bw; Wed, 13 Oct 2021 16:08:00 +0000 Received: from mail-qk1-x749.google.com ([2607:f8b0:4864:20::749]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1mageu-00HTU7-K2 for linux-arm-kernel@lists.infradead.org; Wed, 13 Oct 2021 15:59:14 +0000 Received: by mail-qk1-x749.google.com with SMTP id c16-20020a05620a0cf000b0045f1d55407aso2194900qkj.22 for ; Wed, 13 Oct 2021 08:59:11 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=NDD02US5GbETqBBkcvp2ltSMD53RfkXvi4Mxnfbxyc8=; b=T1qbVsJ5h8QTAp1/7WrxvMrgG72eD1aSQQj1BSMPYB1Kenh6Hklh6K8yCZTmndDK42 u4Y6NVa014WhwUgqqD9CmbygSYVNBaZkM0MN8g7UYT0kUTHH54kjhCEurh3xoN5qYwBo ddSTtlaIarhctUtcmQkgKfG1Z9B5fnVxEdlhSrzQjhoOayqJ5st7VQycchZ+5+1bRDiN kqcD6cdKLI0G2Cohd+EKqWaBlTYO6NH7oR9jYBU6u0mp5nvRhRsQqIvt2zveBAGm2gW3 sz7jZ5lZd0cOcFiIBkGj8S5gYYRVvaB97+qO8VnlH+n3idlF4gkw+ygzF2gJ+kyVQItQ 0ceQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=NDD02US5GbETqBBkcvp2ltSMD53RfkXvi4Mxnfbxyc8=; b=NcEkExzafEyenM+4CKaT6IKoHstzdhNjPSh4txRXPWfWO6l0YmezG46wlOB/mgB/V7 QqNZBaIPCQ0AzsEC0ibsh2lXqQXDQyuR3/OgqUOtQktAB0PxPqwAS0Ll1XafyPLayjlG yFtg5McRvtqHUpuQCSMuBPTXd0MlQG2QoE6nEyliJjc73BhmVKhd4ObGW5hTtIYWCNqh 94erNaurkgA3v6R/zWMG6V1dF2AHwjCrzshoDRsm0qpTOW5Wl7hFsb2fvxrl763TKWYy 2hBJ5slxc9IGYYJL4ZrorssaDJDU3kZQqFjegd3IaXpBTJaZZVaDvFnlz8GZIhg5uZa1 7PZA== X-Gm-Message-State: AOAM532x6y4C+sw9I+hiY/ZX92tQ7KUmik/XPsEuVLH/w0J3z8XgcQur 9Emdxz6t/UgBTnEwaMbCaGpL7fwWOt3L X-Google-Smtp-Source: ABdhPJy+Hh/3VJIHIRnHcU5zrl6PnoHx6rKK0wdWdMKIvuGmVj2OaF9KH+F7ChEdi48qABlrMpoZp8hwR7HW X-Received: from luke.lon.corp.google.com ([2a00:79e0:d:210:65b5:73d3:1558:b9ae]) (user=qperret job=sendgmr) by 2002:a05:622a:118b:: with SMTP id m11mr81305qtk.67.1634140750968; Wed, 13 Oct 2021 08:59:10 -0700 (PDT) Date: Wed, 13 Oct 2021 16:58:31 +0100 In-Reply-To: <20211013155831.943476-1-qperret@google.com> Message-Id: <20211013155831.943476-17-qperret@google.com> Mime-Version: 1.0 References: <20211013155831.943476-1-qperret@google.com> X-Mailer: git-send-email 2.33.0.882.g93a45727a2-goog Subject: [PATCH 16/16] KVM: arm64: pkvm: Unshare guest structs during teardown From: Quentin Perret To: Marc Zyngier , James Morse , Alexandru Elisei , Suzuki K Poulose , Catalin Marinas , Will Deacon , Fuad Tabba , David Brazdil Cc: linux-arm-kernel@lists.infradead.org, kvmarm@lists.cs.columbia.edu, linux-kernel@vger.kernel.org, kernel-team@android.com X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20211013_085912_714735_E7020771 X-CRM114-Status: GOOD ( 16.98 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org Make use of the newly introduced unshare hypercall during guest teardown to unmap guest-related data structures from the hyp stage-1. Signed-off-by: Quentin Perret --- arch/arm64/include/asm/kvm_host.h | 2 ++ arch/arm64/include/asm/kvm_mmu.h | 1 + arch/arm64/kvm/arm.c | 2 ++ arch/arm64/kvm/fpsimd.c | 10 ++++++++-- arch/arm64/kvm/mmu.c | 16 ++++++++++++++++ arch/arm64/kvm/reset.c | 13 ++++++++++++- 6 files changed, 41 insertions(+), 3 deletions(-) diff --git a/arch/arm64/include/asm/kvm_host.h b/arch/arm64/include/asm/kvm_host.h index f8be56d5342b..8b61cdcd1b29 100644 --- a/arch/arm64/include/asm/kvm_host.h +++ b/arch/arm64/include/asm/kvm_host.h @@ -322,6 +322,8 @@ struct kvm_vcpu_arch { struct thread_info *host_thread_info; /* hyp VA */ struct user_fpsimd_state *host_fpsimd_state; /* hyp VA */ + struct thread_info *kern_thread_info; + struct user_fpsimd_state *kern_fpsimd_state; struct { /* {Break,watch}point registers */ diff --git a/arch/arm64/include/asm/kvm_mmu.h b/arch/arm64/include/asm/kvm_mmu.h index 185d0f62b724..81839e9a8a24 100644 --- a/arch/arm64/include/asm/kvm_mmu.h +++ b/arch/arm64/include/asm/kvm_mmu.h @@ -151,6 +151,7 @@ static __always_inline unsigned long __kern_hyp_va(unsigned long v) #include int kvm_share_hyp(void *from, void *to); +void kvm_unshare_hyp(void *from, void *to); int create_hyp_mappings(void *from, void *to, enum kvm_pgtable_prot prot); int create_hyp_io_mappings(phys_addr_t phys_addr, size_t size, void __iomem **kaddr, diff --git a/arch/arm64/kvm/arm.c b/arch/arm64/kvm/arm.c index f2e74635332b..f11c51db6fe6 100644 --- a/arch/arm64/kvm/arm.c +++ b/arch/arm64/kvm/arm.c @@ -188,6 +188,8 @@ void kvm_arch_destroy_vm(struct kvm *kvm) } } atomic_set(&kvm->online_vcpus, 0); + + kvm_unshare_hyp(kvm, kvm + 1); } int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext) diff --git a/arch/arm64/kvm/fpsimd.c b/arch/arm64/kvm/fpsimd.c index 2fe1128d9f3d..67059daf4d26 100644 --- a/arch/arm64/kvm/fpsimd.c +++ b/arch/arm64/kvm/fpsimd.c @@ -28,23 +28,29 @@ int kvm_arch_vcpu_run_map_fp(struct kvm_vcpu *vcpu) { int ret; - struct thread_info *ti = ¤t->thread_info; - struct user_fpsimd_state *fpsimd = ¤t->thread.uw.fpsimd_state; + struct thread_info *ti = vcpu->arch.kern_thread_info; + struct user_fpsimd_state *fpsimd = vcpu->arch.kern_fpsimd_state; /* * Make sure the host task thread flags and fpsimd state are * visible to hyp: */ + kvm_unshare_hyp(ti, ti + 1); + ti = ¤t->thread_info; ret = kvm_share_hyp(ti, ti + 1); if (ret) goto error; + kvm_unshare_hyp(fpsimd, fpsimd + 1); + fpsimd = ¤t->thread.uw.fpsimd_state; ret = kvm_share_hyp(fpsimd, fpsimd + 1); if (ret) goto error; vcpu->arch.host_thread_info = kern_hyp_va(ti); vcpu->arch.host_fpsimd_state = kern_hyp_va(fpsimd); + vcpu->arch.kern_thread_info = ti; + vcpu->arch.kern_fpsimd_state = fpsimd; error: return ret; } diff --git a/arch/arm64/kvm/mmu.c b/arch/arm64/kvm/mmu.c index bc9865a8c988..f01b0e49e262 100644 --- a/arch/arm64/kvm/mmu.c +++ b/arch/arm64/kvm/mmu.c @@ -300,6 +300,22 @@ int kvm_share_hyp(void *from, void *to) nr_pages); } +void kvm_unshare_hyp(void *from, void *to) +{ + phys_addr_t start, end; + u64 nr_pages; + + if (is_kernel_in_hyp_mode() || kvm_host_owns_hyp_mappings() || !from) + return; + + start = ALIGN_DOWN(kvm_kaddr_to_phys(from), PAGE_SIZE); + end = PAGE_ALIGN(kvm_kaddr_to_phys(to)); + nr_pages = (end - start) >> PAGE_SHIFT; + + WARN_ON(kvm_call_hyp_nvhe(__pkvm_host_unshare_hyp, __phys_to_pfn(start), + nr_pages)); +} + /** * create_hyp_mappings - duplicate a kernel virtual address range in Hyp mode * @from: The virtual kernel start address of the range diff --git a/arch/arm64/kvm/reset.c b/arch/arm64/kvm/reset.c index 5ce36b0a3343..e3e9c9e1f1c8 100644 --- a/arch/arm64/kvm/reset.c +++ b/arch/arm64/kvm/reset.c @@ -141,7 +141,18 @@ bool kvm_arm_vcpu_is_finalized(struct kvm_vcpu *vcpu) void kvm_arm_vcpu_destroy(struct kvm_vcpu *vcpu) { - kfree(vcpu->arch.sve_state); + struct user_fpsimd_state *fpsimd = vcpu->arch.kern_fpsimd_state; + struct thread_info *ti = vcpu->arch.kern_thread_info; + void *sve_state = vcpu->arch.sve_state; + + kvm_unshare_hyp(vcpu, vcpu + 1); + if (ti) + kvm_unshare_hyp(ti, ti + 1); + if (fpsimd) + kvm_unshare_hyp(fpsimd, fpsimd + 1); + if (sve_state && vcpu->arch.has_run_once) + kvm_unshare_hyp(sve_state, sve_state + vcpu_sve_state_size(vcpu)); + kfree(sve_state); } static void kvm_vcpu_reset_sve(struct kvm_vcpu *vcpu)