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 mm01.cs.columbia.edu (mm01.cs.columbia.edu [128.59.11.253]) by smtp.lore.kernel.org (Postfix) with ESMTP id 9BFDFC433F5 for ; Fri, 10 Dec 2021 14:49:02 +0000 (UTC) Received: from localhost (localhost [127.0.0.1]) by mm01.cs.columbia.edu (Postfix) with ESMTP id 1E69A4B1D1; Fri, 10 Dec 2021 09:49:02 -0500 (EST) X-Virus-Scanned: at lists.cs.columbia.edu Authentication-Results: mm01.cs.columbia.edu (amavisd-new); dkim=softfail (fail, message has been altered) header.i=@google.com Received: from mm01.cs.columbia.edu ([127.0.0.1]) by localhost (mm01.cs.columbia.edu [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id Ufj1H33wR1UP; Fri, 10 Dec 2021 09:49:01 -0500 (EST) Received: from mm01.cs.columbia.edu (localhost [127.0.0.1]) by mm01.cs.columbia.edu (Postfix) with ESMTP id F18AF4B1E2; Fri, 10 Dec 2021 09:49:00 -0500 (EST) Received: from localhost (localhost [127.0.0.1]) by mm01.cs.columbia.edu (Postfix) with ESMTP id 76D284B1D1 for ; Fri, 10 Dec 2021 09:48:59 -0500 (EST) X-Virus-Scanned: at lists.cs.columbia.edu Received: from mm01.cs.columbia.edu ([127.0.0.1]) by localhost (mm01.cs.columbia.edu [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id H+a2bb97KWHT for ; Fri, 10 Dec 2021 09:48:58 -0500 (EST) Received: from mail-wr1-f53.google.com (mail-wr1-f53.google.com [209.85.221.53]) by mm01.cs.columbia.edu (Postfix) with ESMTPS id 104BE4B1C1 for ; Fri, 10 Dec 2021 09:48:58 -0500 (EST) Received: by mail-wr1-f53.google.com with SMTP id t18so15287636wrg.11 for ; Fri, 10 Dec 2021 06:48:58 -0800 (PST) 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=XSGjwIt9RpPIjkr6liBZIwGzdNBMMcqErsUs7NKqs3g=; b=eFa+/5A+zfMKqzOSVMBePCnsVCvDiruKqK0HXpczVW9xdUh2bI5XBpZykeV00eoyn0 Ps0U7SIG3EJbM2kI0skWEcTnH6lkua7grTJTjDqX4T8jIuSkNqfqNddprnKjZOHHCBOr lAxx8avwGZkdKqJIhrNTAB+UHYQ2NTf1l05If80e5qPJhuhh8x/UU89YQ2EJiE8cor6K O1TOpAxYAh0Omdk6QzNXX51mJBiVH5OGAK5U+h85oIkduoyeNxLMk7CK7/LlxQAb6DOz 9IZqTT8YSfKXYrk4aMHLmYOXa1wCvpRvUVnfICn8/QIi6iSE6JjmwvPvYentz7mJzyTz 084g== 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=XSGjwIt9RpPIjkr6liBZIwGzdNBMMcqErsUs7NKqs3g=; b=SgdWIy+glwF71brWO1zwy2FLanVarmsFWkCNozOZZq2mFlQsJJSgaAVI9gO7pjkuxF LjV8BHzfUk32BDpCRiBTSEkm+ozOoGgMYCFSmOdSxIzmUPVnltDRO2KgyDoJIsAoa2TF qBB5F0eegKO6sLwRA8VWQnno3nLzFMq5YWcTkAmKXXwCN5O3/CfQKREB81sRtdQXq5/q anB23AjCOZobDVFfPJPDxpVJHfNbknoMeEFdK7bN8Tvyvm8nGLR66dU4gyzSL7LdNcPx X8uya7iZgYMallN6jjL8GWl1vdrs+cjb6BsimMnxIOKVCW9IzbY8AICaqiW6q6TOBY7j ew/g== X-Gm-Message-State: AOAM532eeeaMjqqWHyXBTbV3v51zjHdtiHIoBSv8fFDLHYWJKkV/nXks Fnsj2iflwqa6FCyOgoXPfKcjPg== X-Google-Smtp-Source: ABdhPJwvEUvtytgXDIUTqN1i7hPX5BX/gnJ9OxL+n5KP+mAtXq7F0bdSrTa2ut2djOds2sajpum1GA== X-Received: by 2002:adf:a412:: with SMTP id d18mr14714578wra.529.1639147736805; Fri, 10 Dec 2021 06:48:56 -0800 (PST) Received: from google.com ([2a00:79e0:d:210:76c1:fb36:1e22:8677]) by smtp.gmail.com with ESMTPSA id c4sm2669899wrr.37.2021.12.10.06.48.56 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 10 Dec 2021 06:48:56 -0800 (PST) Date: Fri, 10 Dec 2021 14:48:53 +0000 From: Quentin Perret To: Will Deacon Subject: Re: [PATCH v3 15/15] KVM: arm64: pkvm: Unshare guest structs during teardown Message-ID: References: <20211201170411.1561936-1-qperret@google.com> <20211201170411.1561936-16-qperret@google.com> <20211209112233.GD1912@willie-the-truck> MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: <20211209112233.GD1912@willie-the-truck> Cc: kernel-team@android.com, Marc Zyngier , linux-kernel@vger.kernel.org, Catalin Marinas , kvmarm@lists.cs.columbia.edu, linux-arm-kernel@lists.infradead.org X-BeenThere: kvmarm@lists.cs.columbia.edu X-Mailman-Version: 2.1.14 Precedence: list List-Id: Where KVM/ARM decisions are made List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Errors-To: kvmarm-bounces@lists.cs.columbia.edu Sender: kvmarm-bounces@lists.cs.columbia.edu On Thursday 09 Dec 2021 at 11:22:33 (+0000), Will Deacon wrote: > On Wed, Dec 01, 2021 at 05:04:09PM +0000, Quentin Perret wrote: > > 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 | 34 ++++++++++++++++++++++--- > > arch/arm64/kvm/mmu.c | 42 +++++++++++++++++++++++++++++++ > > arch/arm64/kvm/reset.c | 8 +++++- > > 6 files changed, 85 insertions(+), 4 deletions(-) > > [...] > > > diff --git a/arch/arm64/kvm/mmu.c b/arch/arm64/kvm/mmu.c > > index d72566896755..8e506ba8988e 100644 > > --- a/arch/arm64/kvm/mmu.c > > +++ b/arch/arm64/kvm/mmu.c > > @@ -344,6 +344,32 @@ static int share_pfn_hyp(u64 pfn) > > return ret; > > } > > > > +static int unshare_pfn_hyp(u64 pfn) > > +{ > > + struct rb_node **node, *parent; > > + struct hyp_shared_pfn *this; > > + int ret = 0; > > + > > + mutex_lock(&hyp_shared_pfns_lock); > > + this = find_shared_pfn(pfn, &node, &parent); > > + if (WARN_ON(!this)) { > > + ret = -EINVAL; > > -ENOENT? Sure. > > + goto unlock; > > + } > > + > > + this->count--; > > + if (this->count) > > + goto unlock; > > Again, if we did an RCU lookup then this could be converted to a refcount_t > to take the mutex only when it hits zero. But for now I think it's fine. No objection to do this in the future, but yeah I think we might as well start simple :) > > + > > + rb_erase(&this->node, &hyp_shared_pfns); > > + kfree(this); > > + ret = kvm_call_hyp_nvhe(__pkvm_host_unshare_hyp, pfn, 1); > > +unlock: > > + mutex_unlock(&hyp_shared_pfns_lock); > > + > > + return ret; > > +} > > + > > int kvm_share_hyp(void *from, void *to) > > { > > phys_addr_t start, end, cur; > > @@ -376,6 +402,22 @@ int kvm_share_hyp(void *from, void *to) > > return 0; > > } > > > > +void kvm_unshare_hyp(void *from, void *to) > > +{ > > + phys_addr_t start, end, cur; > > + u64 pfn; > > + > > + if (is_kernel_in_hyp_mode() || kvm_host_owns_hyp_mappings() || !from) > > I don't think you need the is_kernel_in_hyp_mode() check any more not that > you've moved that into kvm_host_owns_hyp_mappings(). The logic is a little odd, but I think I still do as kvm_host_owns_hyp_mappings() will return false if is_kernel_in_hyp_mode() is true. _______________________________________________ kvmarm mailing list kvmarm@lists.cs.columbia.edu https://lists.cs.columbia.edu/mailman/listinfo/kvmarm 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 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 smtp.lore.kernel.org (Postfix) with ESMTPS id 98721C433EF for ; Fri, 10 Dec 2021 14:50:43 +0000 (UTC) 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=3x0IWEB86nw5RW88Z9FS3YDft+lReSSGIMBI//GMbTc=; b=uCxFuO1InRsESp 7vvkLyPLK2gEDPvybmQKeAbTBDLGAbYcsxzUPPKCisCGr3dUQHlxv//rwWA4v5xdAQv86pK9dHrYt ICXeyz3Rz6sSfXRq21DHGe79izX2aE70vyGLYiRPQao7BDYRmHbW+bd3A9WDmUQkBWEj13K8SFn/S 0AiRx435rtGXVnd8BcJPu3PiHHpmgQYmz21QLM4Z98OxC+HvcDcCpaitoZut5HBV9a/3W6o/reTym lLvFPFazmEZwve2wNcRuQf54shCMCOYkonHRSyvRBqaNmH0VOXtykhC6iu9jAOst+0R7Y0YP+PIAV t88WW6d68rSIkWwtll6g==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1mvhCp-002FVN-Cy; Fri, 10 Dec 2021 14:49:03 +0000 Received: from mail-wr1-x431.google.com ([2a00:1450:4864:20::431]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1mvhCl-002FUA-M4 for linux-arm-kernel@lists.infradead.org; Fri, 10 Dec 2021 14:49:01 +0000 Received: by mail-wr1-x431.google.com with SMTP id d24so15457258wra.0 for ; Fri, 10 Dec 2021 06:48:58 -0800 (PST) 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=XSGjwIt9RpPIjkr6liBZIwGzdNBMMcqErsUs7NKqs3g=; b=eFa+/5A+zfMKqzOSVMBePCnsVCvDiruKqK0HXpczVW9xdUh2bI5XBpZykeV00eoyn0 Ps0U7SIG3EJbM2kI0skWEcTnH6lkua7grTJTjDqX4T8jIuSkNqfqNddprnKjZOHHCBOr lAxx8avwGZkdKqJIhrNTAB+UHYQ2NTf1l05If80e5qPJhuhh8x/UU89YQ2EJiE8cor6K O1TOpAxYAh0Omdk6QzNXX51mJBiVH5OGAK5U+h85oIkduoyeNxLMk7CK7/LlxQAb6DOz 9IZqTT8YSfKXYrk4aMHLmYOXa1wCvpRvUVnfICn8/QIi6iSE6JjmwvPvYentz7mJzyTz 084g== 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=XSGjwIt9RpPIjkr6liBZIwGzdNBMMcqErsUs7NKqs3g=; b=ABBq9uHBQcxelBv8gTz46wgXIqXKYdu/0yE1YaI56D8d83xCglRIM74JCICeAz8akg xYlGhfcSEqh/EU7h7NpCA8OKQSCLavnwVGpH+O4x2mRiIs1M5BAsf0edOA7mA6xNQDBD 9+fV5OOpge1iOLUDohZqD1eUy6BWB7yDu7p20IjFLhKnkW0EYzXni/lzMsnzISirhzS8 MAh2CF2KMXu+Y6fJ4SI3KOIQleD3sQMmxdYUIQAzb6Q7fypGITb5auumcp2+LrDxMBtl s2Mhokjdi0D4xDB6MUBZDMhVPfGgUrAJJq9btg26qZDhx79XwvsL2iryYl5qxdGlAG3s CJyQ== X-Gm-Message-State: AOAM532z3U94K3UXwo7YN0HXOpLIkjohG4DsDXDlSzJ1lI8OSRV0bBIE 6C6mwoVoNu2Y483JjwT+QVXRQw== X-Google-Smtp-Source: ABdhPJwvEUvtytgXDIUTqN1i7hPX5BX/gnJ9OxL+n5KP+mAtXq7F0bdSrTa2ut2djOds2sajpum1GA== X-Received: by 2002:adf:a412:: with SMTP id d18mr14714578wra.529.1639147736805; Fri, 10 Dec 2021 06:48:56 -0800 (PST) Received: from google.com ([2a00:79e0:d:210:76c1:fb36:1e22:8677]) by smtp.gmail.com with ESMTPSA id c4sm2669899wrr.37.2021.12.10.06.48.56 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 10 Dec 2021 06:48:56 -0800 (PST) Date: Fri, 10 Dec 2021 14:48:53 +0000 From: Quentin Perret To: Will Deacon Cc: Marc Zyngier , James Morse , Alexandru Elisei , Suzuki K Poulose , Catalin Marinas , linux-arm-kernel@lists.infradead.org, kvmarm@lists.cs.columbia.edu, linux-kernel@vger.kernel.org, kernel-team@android.com Subject: Re: [PATCH v3 15/15] KVM: arm64: pkvm: Unshare guest structs during teardown Message-ID: References: <20211201170411.1561936-1-qperret@google.com> <20211201170411.1561936-16-qperret@google.com> <20211209112233.GD1912@willie-the-truck> MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: <20211209112233.GD1912@willie-the-truck> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20211210_064859_754714_E0A42102 X-CRM114-Status: GOOD ( 25.46 ) 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 Thursday 09 Dec 2021 at 11:22:33 (+0000), Will Deacon wrote: > On Wed, Dec 01, 2021 at 05:04:09PM +0000, Quentin Perret wrote: > > 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 | 34 ++++++++++++++++++++++--- > > arch/arm64/kvm/mmu.c | 42 +++++++++++++++++++++++++++++++ > > arch/arm64/kvm/reset.c | 8 +++++- > > 6 files changed, 85 insertions(+), 4 deletions(-) > > [...] > > > diff --git a/arch/arm64/kvm/mmu.c b/arch/arm64/kvm/mmu.c > > index d72566896755..8e506ba8988e 100644 > > --- a/arch/arm64/kvm/mmu.c > > +++ b/arch/arm64/kvm/mmu.c > > @@ -344,6 +344,32 @@ static int share_pfn_hyp(u64 pfn) > > return ret; > > } > > > > +static int unshare_pfn_hyp(u64 pfn) > > +{ > > + struct rb_node **node, *parent; > > + struct hyp_shared_pfn *this; > > + int ret = 0; > > + > > + mutex_lock(&hyp_shared_pfns_lock); > > + this = find_shared_pfn(pfn, &node, &parent); > > + if (WARN_ON(!this)) { > > + ret = -EINVAL; > > -ENOENT? Sure. > > + goto unlock; > > + } > > + > > + this->count--; > > + if (this->count) > > + goto unlock; > > Again, if we did an RCU lookup then this could be converted to a refcount_t > to take the mutex only when it hits zero. But for now I think it's fine. No objection to do this in the future, but yeah I think we might as well start simple :) > > + > > + rb_erase(&this->node, &hyp_shared_pfns); > > + kfree(this); > > + ret = kvm_call_hyp_nvhe(__pkvm_host_unshare_hyp, pfn, 1); > > +unlock: > > + mutex_unlock(&hyp_shared_pfns_lock); > > + > > + return ret; > > +} > > + > > int kvm_share_hyp(void *from, void *to) > > { > > phys_addr_t start, end, cur; > > @@ -376,6 +402,22 @@ int kvm_share_hyp(void *from, void *to) > > return 0; > > } > > > > +void kvm_unshare_hyp(void *from, void *to) > > +{ > > + phys_addr_t start, end, cur; > > + u64 pfn; > > + > > + if (is_kernel_in_hyp_mode() || kvm_host_owns_hyp_mappings() || !from) > > I don't think you need the is_kernel_in_hyp_mode() check any more not that > you've moved that into kvm_host_owns_hyp_mappings(). The logic is a little odd, but I think I still do as kvm_host_owns_hyp_mappings() will return false if is_kernel_in_hyp_mode() is true. _______________________________________________ linux-arm-kernel mailing list linux-arm-kernel@lists.infradead.org http://lists.infradead.org/mailman/listinfo/linux-arm-kernel 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 3785BC433EF for ; Fri, 10 Dec 2021 14:49:04 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S242370AbhLJOwh (ORCPT ); Fri, 10 Dec 2021 09:52:37 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60208 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S242332AbhLJOwe (ORCPT ); Fri, 10 Dec 2021 09:52:34 -0500 Received: from mail-wr1-x434.google.com (mail-wr1-x434.google.com [IPv6:2a00:1450:4864:20::434]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6AA24C061746 for ; Fri, 10 Dec 2021 06:48:58 -0800 (PST) Received: by mail-wr1-x434.google.com with SMTP id t18so15287634wrg.11 for ; Fri, 10 Dec 2021 06:48:58 -0800 (PST) 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=XSGjwIt9RpPIjkr6liBZIwGzdNBMMcqErsUs7NKqs3g=; b=eFa+/5A+zfMKqzOSVMBePCnsVCvDiruKqK0HXpczVW9xdUh2bI5XBpZykeV00eoyn0 Ps0U7SIG3EJbM2kI0skWEcTnH6lkua7grTJTjDqX4T8jIuSkNqfqNddprnKjZOHHCBOr lAxx8avwGZkdKqJIhrNTAB+UHYQ2NTf1l05If80e5qPJhuhh8x/UU89YQ2EJiE8cor6K O1TOpAxYAh0Omdk6QzNXX51mJBiVH5OGAK5U+h85oIkduoyeNxLMk7CK7/LlxQAb6DOz 9IZqTT8YSfKXYrk4aMHLmYOXa1wCvpRvUVnfICn8/QIi6iSE6JjmwvPvYentz7mJzyTz 084g== 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=XSGjwIt9RpPIjkr6liBZIwGzdNBMMcqErsUs7NKqs3g=; b=NG37Bi404MJU1UW8Aex4fR3YFSCeRqkfbCb5n+mL0L6A3foqCFADQPW8f8OG9cdYRH vvYU6ZLttWQOqIEP6yV3Cv1I04LmzVgbfTLvTYySKklAGIND7F4rnuJuOgKIFTlhNvBj zrRm/1wufJHIDlzUcnmnoGgrsIWd/4ZehdmI8qCyUJaMk6WcH9wEWVVw0Spt59WEb09Z 52m0woGAohGzcncZZ3q22IPchriwctKHYVLQ39lY+w9t+KaHnRMUc1zdP6/XzNsdh+qQ PIfjLPPdVbRTMvurylZhCzuMlpsS+xtZp+FLkfxK7yxUQHxS1utI8Y5xkrprF9oPHtcx t2vQ== X-Gm-Message-State: AOAM530hynT5+Le1qxQS3MkScT3dtSwLtEJ8JPrdDBIlKV5Ak/pTDa83 3NUappg9BQODXkKgP0feD0NjsA== X-Google-Smtp-Source: ABdhPJwvEUvtytgXDIUTqN1i7hPX5BX/gnJ9OxL+n5KP+mAtXq7F0bdSrTa2ut2djOds2sajpum1GA== X-Received: by 2002:adf:a412:: with SMTP id d18mr14714578wra.529.1639147736805; Fri, 10 Dec 2021 06:48:56 -0800 (PST) Received: from google.com ([2a00:79e0:d:210:76c1:fb36:1e22:8677]) by smtp.gmail.com with ESMTPSA id c4sm2669899wrr.37.2021.12.10.06.48.56 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 10 Dec 2021 06:48:56 -0800 (PST) Date: Fri, 10 Dec 2021 14:48:53 +0000 From: Quentin Perret To: Will Deacon Cc: Marc Zyngier , James Morse , Alexandru Elisei , Suzuki K Poulose , Catalin Marinas , linux-arm-kernel@lists.infradead.org, kvmarm@lists.cs.columbia.edu, linux-kernel@vger.kernel.org, kernel-team@android.com Subject: Re: [PATCH v3 15/15] KVM: arm64: pkvm: Unshare guest structs during teardown Message-ID: References: <20211201170411.1561936-1-qperret@google.com> <20211201170411.1561936-16-qperret@google.com> <20211209112233.GD1912@willie-the-truck> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20211209112233.GD1912@willie-the-truck> Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Thursday 09 Dec 2021 at 11:22:33 (+0000), Will Deacon wrote: > On Wed, Dec 01, 2021 at 05:04:09PM +0000, Quentin Perret wrote: > > 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 | 34 ++++++++++++++++++++++--- > > arch/arm64/kvm/mmu.c | 42 +++++++++++++++++++++++++++++++ > > arch/arm64/kvm/reset.c | 8 +++++- > > 6 files changed, 85 insertions(+), 4 deletions(-) > > [...] > > > diff --git a/arch/arm64/kvm/mmu.c b/arch/arm64/kvm/mmu.c > > index d72566896755..8e506ba8988e 100644 > > --- a/arch/arm64/kvm/mmu.c > > +++ b/arch/arm64/kvm/mmu.c > > @@ -344,6 +344,32 @@ static int share_pfn_hyp(u64 pfn) > > return ret; > > } > > > > +static int unshare_pfn_hyp(u64 pfn) > > +{ > > + struct rb_node **node, *parent; > > + struct hyp_shared_pfn *this; > > + int ret = 0; > > + > > + mutex_lock(&hyp_shared_pfns_lock); > > + this = find_shared_pfn(pfn, &node, &parent); > > + if (WARN_ON(!this)) { > > + ret = -EINVAL; > > -ENOENT? Sure. > > + goto unlock; > > + } > > + > > + this->count--; > > + if (this->count) > > + goto unlock; > > Again, if we did an RCU lookup then this could be converted to a refcount_t > to take the mutex only when it hits zero. But for now I think it's fine. No objection to do this in the future, but yeah I think we might as well start simple :) > > + > > + rb_erase(&this->node, &hyp_shared_pfns); > > + kfree(this); > > + ret = kvm_call_hyp_nvhe(__pkvm_host_unshare_hyp, pfn, 1); > > +unlock: > > + mutex_unlock(&hyp_shared_pfns_lock); > > + > > + return ret; > > +} > > + > > int kvm_share_hyp(void *from, void *to) > > { > > phys_addr_t start, end, cur; > > @@ -376,6 +402,22 @@ int kvm_share_hyp(void *from, void *to) > > return 0; > > } > > > > +void kvm_unshare_hyp(void *from, void *to) > > +{ > > + phys_addr_t start, end, cur; > > + u64 pfn; > > + > > + if (is_kernel_in_hyp_mode() || kvm_host_owns_hyp_mappings() || !from) > > I don't think you need the is_kernel_in_hyp_mode() check any more not that > you've moved that into kvm_host_owns_hyp_mappings(). The logic is a little odd, but I think I still do as kvm_host_owns_hyp_mappings() will return false if is_kernel_in_hyp_mode() is true.