From mboxrd@z Thu Jan 1 00:00:00 1970 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 4AA4FC433F5 for ; Mon, 18 Oct 2021 14:12:02 +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 08B1A60EFE for ; Mon, 18 Oct 2021 14:12:02 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org 08B1A60EFE 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:In-Reply-To:MIME-Version:References: Message-ID:Subject:Cc:To:From:Date:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=d3ZBmoJ8ZZSfMkhalYtuLSjwEsI/dUNvx7UV/yeJgfM=; b=WNW7flzvDS0peV 2t5E9oyYY7x1GFj4EFaKcGTqpaDgPeJCsONd1/0k9xUbPG0jND4tpztemv2tGwVktJL4EU6bRiX8h VMr6vwx6K2+/flL+QXUSXtoH+yR3qd5kPOg8ugOAv5ElNp3LMAhItkC87+0zteYL9/aiB3s6yQM96 qKd2Hg3k3G77e+/e9F0/uilzrZDJ/QePy2VYF8vcvgyIhnsX2d6wpbQJZn2BiDGuwhi/ckPuEwwDb 1fdWLlKn3WR2Lm03CBDtNoaXx+XgIUO3st+IVYuiXsQ9jRfPLNiG+uUOPaJrC/Yvx3+8ZMhMs0Foi uA5HIeIxCZI5rH6SiOxQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1mcTKj-00G0Wt-3Q; Mon, 18 Oct 2021 14:09:46 +0000 Received: from mail-wm1-x32b.google.com ([2a00:1450:4864:20::32b]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1mcTEN-00FyLz-AV for linux-arm-kernel@lists.infradead.org; Mon, 18 Oct 2021 14:03:13 +0000 Received: by mail-wm1-x32b.google.com with SMTP id m42so8851406wms.2 for ; Mon, 18 Oct 2021 07:03:09 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to; bh=ilVE4xJNEWJqyraR6L3VDA//LfSc9977wvuETuVPB8A=; b=iJIXL5k7UKvSaiTpCZ/NRbW8UXBAsbUL5bOR63EhPqb2ttLq0MzQ+FfWR28Ughkcbs 7oxUvk5dyNKyRQ9FcyE+4POjljVn8AUTg0UxQ/joj1V2cjBe6Mb/J+3yp9DUeTOChEzF hCbP6MccBTheeW2X1EAx4b6raO1wC/cuiHTbGljgxKMNmXFiLAeD0yLXrzZ+XBD5/Ssz 9Dg0rxygs5gZ7Hr/mxbdvF1cw40+Mdx6SSDe5DsAEeUGW+2gJ0hE6ZWPxLFMwf/XaGZJ 6dX+v1hEfS5yqCz0z84JwbkyGe05YN4VVNU5P88OPbnwPQx308pK5syC8OBhnhe9bTQy o9iw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=ilVE4xJNEWJqyraR6L3VDA//LfSc9977wvuETuVPB8A=; b=FWTy9eCl/qC/uHiwYY80j0XOuvXhTRZIMG2sWubUmScXRR/MgIEc3wwW46P/e2M1/j aSLE1E12fAS6DCipyg3b+JdETHYEVepOZ49wIkBrkS6ZlHlzGAO99QQAnABxkt2JpjU7 3+vFMFDJFxZ0b1iQZ7i3kbuQ/LvqeCcZbbOrxI+6VtNJdZQVGkKITgrcKdwYoC/XEQiL YwDin0qUyIPT1DseU+oNjxJBT57nKagpzT3Qiu1/he5XvWSgH/WCJb9xjK+vUNVKCGfK InHNVrE1wcB/YOTuNEhefc+R0jgkZhK0JKJSrzvwWjP0XNmxuKIBXnO5psBJWrFbDfwB Cgpg== X-Gm-Message-State: AOAM5312e/mW8sO4Wrigr39ib/o9TAjPCNFxmAYMjMFYi7tgTGGKd3/S /BndmULJyxj1mOzTp7Htfu5Jxg== X-Google-Smtp-Source: ABdhPJxJWiLYpJjB5ZQ0+cjl9xUBd/Al0nfVV9kESWuAisjQ7GXwYpBkwbby+DsCbh6H7gcw1jJXcg== X-Received: by 2002:a05:600c:1c1b:: with SMTP id j27mr29455483wms.1.1634565788135; Mon, 18 Oct 2021 07:03:08 -0700 (PDT) Received: from google.com ([2a00:79e0:d:210:ba81:6f1b:ab2e:f120]) by smtp.gmail.com with ESMTPSA id n9sm3982324wmq.6.2021.10.18.07.03.07 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 18 Oct 2021 07:03:07 -0700 (PDT) Date: Mon, 18 Oct 2021 15:03:05 +0100 From: Quentin Perret To: Marc Zyngier Cc: James Morse , Alexandru Elisei , Suzuki K Poulose , Catalin Marinas , Will Deacon , Fuad Tabba , David Brazdil , linux-arm-kernel@lists.infradead.org, kvmarm@lists.cs.columbia.edu, linux-kernel@vger.kernel.org, kernel-team@android.com Subject: Re: [PATCH 16/16] KVM: arm64: pkvm: Unshare guest structs during teardown Message-ID: References: <20211013155831.943476-1-qperret@google.com> <20211013155831.943476-17-qperret@google.com> <87h7dhupfa.wl-maz@kernel.org> MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20211018_070311_447616_03FC7E4A X-CRM114-Status: GOOD ( 20.20 ) 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: , Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org On Monday 18 Oct 2021 at 11:32:13 (+0100), Quentin Perret wrote: > Another option is to take a refcount on 'current' from > kvm_arch_vcpu_run_map_fp() before sharing thread-specific structs with > the hyp and release the refcount of the previous task after unsharing. > But that means we'll have to also drop the refcount when the vcpu > gets destroyed, as well as explicitly unshare at that point. Shouldn't > be too bad I think. Thoughts? Something like the below seems to work OK on my setup, including SIGKILL'ing the guest and such. How much do you hate it? diff --git a/arch/arm64/include/asm/kvm_host.h b/arch/arm64/include/asm/kvm_host.h index f8be56d5342b..50598d704c71 100644 --- a/arch/arm64/include/asm/kvm_host.h +++ b/arch/arm64/include/asm/kvm_host.h @@ -322,6 +322,7 @@ struct kvm_vcpu_arch { struct thread_info *host_thread_info; /* hyp VA */ struct user_fpsimd_state *host_fpsimd_state; /* hyp VA */ + struct task_struct *parent_task; struct { /* {Break,watch}point registers */ @@ -738,6 +739,7 @@ int kvm_arch_vcpu_run_map_fp(struct kvm_vcpu *vcpu); void kvm_arch_vcpu_load_fp(struct kvm_vcpu *vcpu); void kvm_arch_vcpu_ctxsync_fp(struct kvm_vcpu *vcpu); void kvm_arch_vcpu_put_fp(struct kvm_vcpu *vcpu); +void kvm_vcpu_unshare_task_fp(struct kvm_vcpu *vcpu); static inline bool kvm_pmu_counter_deferred(struct perf_event_attr *attr) { diff --git a/arch/arm64/kvm/fpsimd.c b/arch/arm64/kvm/fpsimd.c index 2fe1128d9f3d..27afeebbe1cb 100644 --- a/arch/arm64/kvm/fpsimd.c +++ b/arch/arm64/kvm/fpsimd.c @@ -15,6 +15,22 @@ #include #include +void kvm_vcpu_unshare_task_fp(struct kvm_vcpu *vcpu) +{ + struct task_struct *p = vcpu->arch.parent_task; + struct user_fpsimd_state *fpsimd; + struct thread_info *ti; + + if (!static_branch_likely(&kvm_protected_mode_initialized) || !p) + return; + + ti = &p->thread_info; + kvm_unshare_hyp(ti, ti + 1); + fpsimd = &p->thread.uw.fpsimd_state; + kvm_unshare_hyp(fpsimd, fpsimd + 1); + put_task_struct(p); +} + /* * Called on entry to KVM_RUN unless this vcpu previously ran at least * once and the most recent prior KVM_RUN for this vcpu was called from @@ -31,6 +47,8 @@ int kvm_arch_vcpu_run_map_fp(struct kvm_vcpu *vcpu) struct thread_info *ti = ¤t->thread_info; struct user_fpsimd_state *fpsimd = ¤t->thread.uw.fpsimd_state; + kvm_vcpu_unshare_task_fp(vcpu); + /* * Make sure the host task thread flags and fpsimd state are * visible to hyp: @@ -45,6 +63,17 @@ int kvm_arch_vcpu_run_map_fp(struct kvm_vcpu *vcpu) vcpu->arch.host_thread_info = kern_hyp_va(ti); vcpu->arch.host_fpsimd_state = kern_hyp_va(fpsimd); + + /* + * We need to keep current's task_struct pinned until its data has been + * unshared with the hypervisor to make sure it is not re-used by the + * kernel and donated to someone else while already shared -- see + * kvm_vcpu_unshare_task_fp() for the matching put_task_struct(). + */ + if (static_branch_likely(&kvm_protected_mode_initialized)) { + get_task_struct(current); + vcpu->arch.parent_task = current; + } error: return ret; } diff --git a/arch/arm64/kvm/reset.c b/arch/arm64/kvm/reset.c index 5ce36b0a3343..c2a2cd7d5748 100644 --- a/arch/arm64/kvm/reset.c +++ b/arch/arm64/kvm/reset.c @@ -141,7 +141,13 @@ bool kvm_arm_vcpu_is_finalized(struct kvm_vcpu *vcpu) void kvm_arm_vcpu_destroy(struct kvm_vcpu *vcpu) { - kfree(vcpu->arch.sve_state); + void *sve_state = vcpu->arch.sve_state; + + kvm_vcpu_unshare_task_fp(vcpu); + kvm_unshare_hyp(vcpu, vcpu + 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) _______________________________________________ linux-arm-kernel mailing list linux-arm-kernel@lists.infradead.org http://lists.infradead.org/mailman/listinfo/linux-arm-kernel