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 03F31C433EF for ; Thu, 9 Dec 2021 11:17:38 +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=eosx6RHtJL03OBkcLNgXaazk0WcIqI/scmae9Ut+xQY=; b=yf0RvKCRzXwPTi C3B78yciGQRGlHSaeSVKaKa+Jf6zkf0RRMe9hW77yBMRQG1G2DMnjxOnrYKasc34L1AnBtAjLMyNe aoBhNqvxPhwE7qeKuY9zr4hMIP+80smFeSNUvJp4HXfWi7hVHZj6YR1PNmGqbd/H2y+Dldin8wu0G lGzIRgqLCzBRwh2e3E7wTpVPZU93b5RZx2u6TcNyHZOafq7jhq/yLJsNlZDKYD448sAyFPlZj8JTP G0o4a02zq/KWEjVfhNcCx/ymgH95vde4JL7CXki++ox00oxqEMxiOBfEgxPUtjiNZpo6T1RfsybAh zqU20Z0k+7594PyS1tlQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1mvHPR-00Fkua-CO; Thu, 09 Dec 2021 11:16:21 +0000 Received: from ams.source.kernel.org ([145.40.68.75]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1mvHPN-00Fksr-EP for linux-arm-kernel@lists.infradead.org; Thu, 09 Dec 2021 11:16:19 +0000 Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ams.source.kernel.org (Postfix) with ESMTPS id 1A465B82436; Thu, 9 Dec 2021 11:16:16 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 12AFBC004DD; Thu, 9 Dec 2021 11:16:12 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1639048574; bh=kDyOftpkmsDk75GoXEVgUx9K1nNqGrcK4DZF+4URS5E=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=jQI0P+RkXeUhXDPkrrzoj0GrFMX0eSD1FfDHxzzxFQRopG2Py5EnZHqNLeCygCmlt w1TdvhgRi0nUtZ9TguyZMIAAUCZ0TDfoY403LndDgHvFry24vHaduocWwNXIVfiBvC EAETbyV52qtYoYXPvmDBxyYuQtFNTrV/kyXglI4xo8bcZ0Q/930S+O8QRY1PTmvh5o 8+e7JuaNuWMDBSnWME6y7c0y7iKZf58t3+cSsdGU8qRvDyEErXTWUzPMbiARc2s1wo E3ILJdQCqfxXka+VRXzLw+ZOyPXKyVirn1diPqca9ArQVFo4mvpFYxq267tCi0aIut 0BW/N+otnTuJw== Date: Thu, 9 Dec 2021 11:16:09 +0000 From: Will Deacon To: Quentin Perret 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 08/15] KVM: arm64: pkvm: Refcount the pages shared with EL2 Message-ID: <20211209111609.GC1912@willie-the-truck> References: <20211201170411.1561936-1-qperret@google.com> <20211201170411.1561936-9-qperret@google.com> MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: <20211201170411.1561936-9-qperret@google.com> User-Agent: Mutt/1.10.1 (2018-07-13) X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20211209_031617_798305_CAF746DA X-CRM114-Status: GOOD ( 23.13 ) 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 Wed, Dec 01, 2021 at 05:04:02PM +0000, Quentin Perret wrote: > In order to simplify the page tracking infrastructure at EL2 in nVHE > protected mode, move the responsibility of refcounting pages that are > shared multiple times on the host. In order to do so, let's create a > red-black tree tracking all the PFNs that have been shared, along with > a refcount. > > Signed-off-by: Quentin Perret > --- > arch/arm64/kvm/mmu.c | 78 ++++++++++++++++++++++++++++++++++++++------ > 1 file changed, 68 insertions(+), 10 deletions(-) > > diff --git a/arch/arm64/kvm/mmu.c b/arch/arm64/kvm/mmu.c > index fd868fb9d922..d72566896755 100644 > --- a/arch/arm64/kvm/mmu.c > +++ b/arch/arm64/kvm/mmu.c > @@ -284,23 +284,72 @@ static phys_addr_t kvm_kaddr_to_phys(void *kaddr) > } > } > > -static int pkvm_share_hyp(phys_addr_t start, phys_addr_t end) > +struct hyp_shared_pfn { > + u64 pfn; > + int count; > + struct rb_node node; > +}; > + > +static DEFINE_MUTEX(hyp_shared_pfns_lock); > +static struct rb_root hyp_shared_pfns = RB_ROOT; > + > +static struct hyp_shared_pfn *find_shared_pfn(u64 pfn, struct rb_node ***node, > + struct rb_node **parent) > { > - phys_addr_t addr; > - int ret; > + struct hyp_shared_pfn *this; > + > + *node = &hyp_shared_pfns.rb_node; > + *parent = NULL; > + while (**node) { > + this = container_of(**node, struct hyp_shared_pfn, node); > + *parent = **node; > + if (this->pfn < pfn) > + *node = &((**node)->rb_left); > + else if (this->pfn > pfn) > + *node = &((**node)->rb_right); > + else > + return this; > + } > > - for (addr = ALIGN_DOWN(start, PAGE_SIZE); addr < end; addr += PAGE_SIZE) { > - ret = kvm_call_hyp_nvhe(__pkvm_host_share_hyp, > - __phys_to_pfn(addr)); > - if (ret) > - return ret; > + return NULL; > +} > + > +static int share_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); I don't think this is a fast-path at the moment, but in the future we might consider using RCU to do the lookup outside of the mutex. But as-is: Acked-by: Will Deacon Will _______________________________________________ linux-arm-kernel mailing list linux-arm-kernel@lists.infradead.org http://lists.infradead.org/mailman/listinfo/linux-arm-kernel