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 X-Spam-Level: X-Spam-Status: No, score=-14.0 required=3.0 tests=BAYES_00,INCLUDES_CR_TRAILER, INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id B6A4FC07E95 for ; Mon, 19 Jul 2021 15:04:06 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 9632261364 for ; Mon, 19 Jul 2021 15:04:06 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S243062AbhGSOXZ (ORCPT ); Mon, 19 Jul 2021 10:23:25 -0400 Received: from mail.kernel.org ([198.145.29.99]:55876 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S242880AbhGSOVQ (ORCPT ); Mon, 19 Jul 2021 10:21:16 -0400 Received: from disco-boy.misterjones.org (disco-boy.misterjones.org [51.254.78.96]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 6F9ED61181; Mon, 19 Jul 2021 15:01:43 +0000 (UTC) Received: from sofa.misterjones.org ([185.219.108.64] helo=why.misterjones.org) by disco-boy.misterjones.org with esmtpsa (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1m5Um5-00EEYM-CC; Mon, 19 Jul 2021 16:01:41 +0100 Date: Mon, 19 Jul 2021 16:01:40 +0100 Message-ID: <87eebujqjv.wl-maz@kernel.org> From: Marc Zyngier To: Quentin Perret Cc: james.morse@arm.com, alexandru.elisei@arm.com, suzuki.poulose@arm.com, catalin.marinas@arm.com, will@kernel.org, linux-arm-kernel@lists.infradead.org, kvmarm@lists.cs.columbia.edu, linux-kernel@vger.kernel.org, ardb@kernel.org, qwandor@google.com, tabba@google.com, dbrazdil@google.com, kernel-team@android.com Subject: Re: [PATCH 09/14] KVM: arm64: Mark host bss and rodata section as shared In-Reply-To: <20210719104735.3681732-10-qperret@google.com> References: <20210719104735.3681732-1-qperret@google.com> <20210719104735.3681732-10-qperret@google.com> User-Agent: Wanderlust/2.15.9 (Almost Unreal) SEMI-EPG/1.14.7 (Harue) FLIM-LB/1.14.9 (=?UTF-8?B?R29qxY0=?=) APEL-LB/10.8 EasyPG/1.0.0 Emacs/27.1 (x86_64-pc-linux-gnu) MULE/6.0 (HANACHIRUSATO) MIME-Version: 1.0 (generated by SEMI-EPG 1.14.7 - "Harue") Content-Type: text/plain; charset=US-ASCII X-SA-Exim-Connect-IP: 185.219.108.64 X-SA-Exim-Rcpt-To: qperret@google.com, james.morse@arm.com, alexandru.elisei@arm.com, suzuki.poulose@arm.com, catalin.marinas@arm.com, will@kernel.org, linux-arm-kernel@lists.infradead.org, kvmarm@lists.cs.columbia.edu, linux-kernel@vger.kernel.org, ardb@kernel.org, qwandor@google.com, tabba@google.com, dbrazdil@google.com, kernel-team@android.com X-SA-Exim-Mail-From: maz@kernel.org X-SA-Exim-Scanned: No (on disco-boy.misterjones.org); SAEximRunCond expanded to false Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Mon, 19 Jul 2021 11:47:30 +0100, Quentin Perret wrote: > > As the hypervisor maps the host's .bss and .rodata sections in its > stage-1, make sure to tag them as shared in hyp and host page-tables. > > But since the hypervisor relies on the presence of these mappings, we > cannot let the host in complete control of the memory regions -- it > must not unshare or donate them to another entity for example. To > prevent this, let's transfer the ownership of those ranges to the > hypervisor itself, and share the page back with the host. > > Signed-off-by: Quentin Perret > --- > arch/arm64/kvm/hyp/include/nvhe/mem_protect.h | 1 + > arch/arm64/kvm/hyp/nvhe/mem_protect.c | 7 ++- > arch/arm64/kvm/hyp/nvhe/setup.c | 52 ++++++++++++++++--- > 3 files changed, 51 insertions(+), 9 deletions(-) > > diff --git a/arch/arm64/kvm/hyp/include/nvhe/mem_protect.h b/arch/arm64/kvm/hyp/include/nvhe/mem_protect.h > index 9c227d87c36d..b39047463075 100644 > --- a/arch/arm64/kvm/hyp/include/nvhe/mem_protect.h > +++ b/arch/arm64/kvm/hyp/include/nvhe/mem_protect.h > @@ -23,6 +23,7 @@ extern struct host_kvm host_kvm; > int __pkvm_prot_finalize(void); > int __pkvm_mark_hyp(phys_addr_t start, phys_addr_t end); > > +int host_stage2_idmap_locked(u64 start, u64 end, enum kvm_pgtable_prot prot); > int kvm_host_prepare_stage2(void *pgt_pool_base); > void handle_host_mem_abort(struct kvm_cpu_context *host_ctxt); > > diff --git a/arch/arm64/kvm/hyp/nvhe/mem_protect.c b/arch/arm64/kvm/hyp/nvhe/mem_protect.c > index cdace80d3e28..6f28edf58407 100644 > --- a/arch/arm64/kvm/hyp/nvhe/mem_protect.c > +++ b/arch/arm64/kvm/hyp/nvhe/mem_protect.c > @@ -235,6 +235,11 @@ static bool host_stage2_want_pte_cb(u64 addr, u64 end, enum kvm_pgtable_prot pro > return prot != KVM_PGTABLE_PROT_RW; > } > > +int host_stage2_idmap_locked(u64 start, u64 end, enum kvm_pgtable_prot prot) > +{ > + return host_stage2_try(__host_stage2_idmap, start, end, prot); > +} > + > static int host_stage2_idmap(u64 addr) > { > enum kvm_pgtable_prot prot = KVM_PGTABLE_PROT_RW; > @@ -250,7 +255,7 @@ static int host_stage2_idmap(u64 addr) > if (ret) > goto unlock; > > - ret = host_stage2_try(__host_stage2_idmap, range.start, range.end, prot); > + ret = host_stage2_idmap_locked(range.start, range.end, prot); > unlock: > hyp_spin_unlock(&host_kvm.lock); > > diff --git a/arch/arm64/kvm/hyp/nvhe/setup.c b/arch/arm64/kvm/hyp/nvhe/setup.c > index 0b574d106519..74dce83a6fad 100644 > --- a/arch/arm64/kvm/hyp/nvhe/setup.c > +++ b/arch/arm64/kvm/hyp/nvhe/setup.c > @@ -83,10 +83,6 @@ static int recreate_hyp_mappings(phys_addr_t phys, unsigned long size, > if (ret) > return ret; > > - ret = pkvm_create_mappings(__start_rodata, __end_rodata, PAGE_HYP_RO); > - if (ret) > - return ret; > - > ret = pkvm_create_mappings(__hyp_rodata_start, __hyp_rodata_end, PAGE_HYP_RO); > if (ret) > return ret; > @@ -95,10 +91,6 @@ static int recreate_hyp_mappings(phys_addr_t phys, unsigned long size, > if (ret) > return ret; > > - ret = pkvm_create_mappings(__hyp_bss_end, __bss_stop, PAGE_HYP_RO); > - if (ret) > - return ret; > - > ret = pkvm_create_mappings(virt, virt + size, PAGE_HYP); > if (ret) > return ret; > @@ -117,6 +109,25 @@ static int recreate_hyp_mappings(phys_addr_t phys, unsigned long size, > return ret; > } > > + /* > + * Map the host's .bss and .rodata sections RO in the hypervisor, but > + * transfer the ownerhsip from the host to the hypervisor itself to > + * make sure it can't be donated or shared with another entity. > + * > + * The ownership transtion requires matching changes in the host > + * stage-2. This will done later (see finalize_mappings()) once the > + * hyp_vmemmap is addressable. > + */ > + ret = pkvm_create_mappings(__start_rodata, __end_rodata, > + PAGE_HYP_RO | KVM_PGTABLE_STATE_SHARED); > + if (ret) > + return ret; > + > + ret = pkvm_create_mappings(__hyp_bss_end, __bss_stop, > + PAGE_HYP_RO | KVM_PGTABLE_STATE_SHARED); > + if (ret) > + return ret; > + > return 0; > } > > @@ -148,6 +159,27 @@ static void hpool_put_page(void *addr) > hyp_put_page(&hpool, addr); > } > > +static int finalize_mappings(void) > +{ > + enum kvm_pgtable_prot prot = KVM_PGTABLE_PROT_RWX; > + int ret; > + > + /* > + * The host's .bss and .rodata sections are now conceptually owned by > + * the hypervisor, so mark them as 'borrowed' in the host stage-2. We > + * can safely use host_stage2_idmap_locked() at this point since the > + * host stage-2 has not been enabled yet. > + */ > + prot |= KVM_PGTABLE_STATE_SHARED | KVM_PGTABLE_STATE_BORROWED; > + ret = host_stage2_idmap_locked(__hyp_pa(__start_rodata), > + __hyp_pa(__end_rodata), prot); Do we really want to map the rodata section as RWX? > + if (ret) > + return ret; > + > + return host_stage2_idmap_locked(__hyp_pa(__hyp_bss_end), > + __hyp_pa(__bss_stop), prot); If the 'locked' state implies SHARED+BORROWED, maybe consider moving the ORRing of the prot into host_stage2_idmap_locked()? > +} > + > void __noreturn __pkvm_init_finalise(void) > { > struct kvm_host_data *host_data = this_cpu_ptr(&kvm_host_data); > @@ -167,6 +199,10 @@ void __noreturn __pkvm_init_finalise(void) > if (ret) > goto out; > > + ret = finalize_mappings(); > + if (ret) > + goto out; > + > pkvm_pgtable_mm_ops = (struct kvm_pgtable_mm_ops) { > .zalloc_page = hyp_zalloc_hyp_page, > .phys_to_virt = hyp_phys_to_virt, Thanks, M. -- Without deviation from the norm, progress is not possible. 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 X-Spam-Level: X-Spam-Status: No, score=-14.0 required=3.0 tests=BAYES_00,INCLUDES_CR_TRAILER, INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 36929C636C9 for ; Mon, 19 Jul 2021 15:01:53 +0000 (UTC) Received: from mm01.cs.columbia.edu (mm01.cs.columbia.edu [128.59.11.253]) by mail.kernel.org (Postfix) with ESMTP id B133D6120D for ; Mon, 19 Jul 2021 15:01:52 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org B133D6120D Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=kernel.org Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=kvmarm-bounces@lists.cs.columbia.edu Received: from localhost (localhost [127.0.0.1]) by mm01.cs.columbia.edu (Postfix) with ESMTP id 39B0949D50; Mon, 19 Jul 2021 11:01:52 -0400 (EDT) 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 sVzeJns+ZfOB; Mon, 19 Jul 2021 11:01:47 -0400 (EDT) Received: from mm01.cs.columbia.edu (localhost [127.0.0.1]) by mm01.cs.columbia.edu (Postfix) with ESMTP id 04B8F4A2E5; Mon, 19 Jul 2021 11:01:47 -0400 (EDT) Received: from localhost (localhost [127.0.0.1]) by mm01.cs.columbia.edu (Postfix) with ESMTP id BBCF040CF9 for ; Mon, 19 Jul 2021 11:01:45 -0400 (EDT) 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 SVAQTwezlXBm for ; Mon, 19 Jul 2021 11:01:44 -0400 (EDT) Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by mm01.cs.columbia.edu (Postfix) with ESMTPS id 69158407EC for ; Mon, 19 Jul 2021 11:01:44 -0400 (EDT) Received: from disco-boy.misterjones.org (disco-boy.misterjones.org [51.254.78.96]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 6F9ED61181; Mon, 19 Jul 2021 15:01:43 +0000 (UTC) Received: from sofa.misterjones.org ([185.219.108.64] helo=why.misterjones.org) by disco-boy.misterjones.org with esmtpsa (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1m5Um5-00EEYM-CC; Mon, 19 Jul 2021 16:01:41 +0100 Date: Mon, 19 Jul 2021 16:01:40 +0100 Message-ID: <87eebujqjv.wl-maz@kernel.org> From: Marc Zyngier To: Quentin Perret Subject: Re: [PATCH 09/14] KVM: arm64: Mark host bss and rodata section as shared In-Reply-To: <20210719104735.3681732-10-qperret@google.com> References: <20210719104735.3681732-1-qperret@google.com> <20210719104735.3681732-10-qperret@google.com> User-Agent: Wanderlust/2.15.9 (Almost Unreal) SEMI-EPG/1.14.7 (Harue) FLIM-LB/1.14.9 (=?UTF-8?B?R29qxY0=?=) APEL-LB/10.8 EasyPG/1.0.0 Emacs/27.1 (x86_64-pc-linux-gnu) MULE/6.0 (HANACHIRUSATO) MIME-Version: 1.0 (generated by SEMI-EPG 1.14.7 - "Harue") X-SA-Exim-Connect-IP: 185.219.108.64 X-SA-Exim-Rcpt-To: qperret@google.com, james.morse@arm.com, alexandru.elisei@arm.com, suzuki.poulose@arm.com, catalin.marinas@arm.com, will@kernel.org, linux-arm-kernel@lists.infradead.org, kvmarm@lists.cs.columbia.edu, linux-kernel@vger.kernel.org, ardb@kernel.org, qwandor@google.com, tabba@google.com, dbrazdil@google.com, kernel-team@android.com X-SA-Exim-Mail-From: maz@kernel.org X-SA-Exim-Scanned: No (on disco-boy.misterjones.org); SAEximRunCond expanded to false Cc: kernel-team@android.com, qwandor@google.com, will@kernel.org, catalin.marinas@arm.com, linux-kernel@vger.kernel.org, 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 Mon, 19 Jul 2021 11:47:30 +0100, Quentin Perret wrote: > > As the hypervisor maps the host's .bss and .rodata sections in its > stage-1, make sure to tag them as shared in hyp and host page-tables. > > But since the hypervisor relies on the presence of these mappings, we > cannot let the host in complete control of the memory regions -- it > must not unshare or donate them to another entity for example. To > prevent this, let's transfer the ownership of those ranges to the > hypervisor itself, and share the page back with the host. > > Signed-off-by: Quentin Perret > --- > arch/arm64/kvm/hyp/include/nvhe/mem_protect.h | 1 + > arch/arm64/kvm/hyp/nvhe/mem_protect.c | 7 ++- > arch/arm64/kvm/hyp/nvhe/setup.c | 52 ++++++++++++++++--- > 3 files changed, 51 insertions(+), 9 deletions(-) > > diff --git a/arch/arm64/kvm/hyp/include/nvhe/mem_protect.h b/arch/arm64/kvm/hyp/include/nvhe/mem_protect.h > index 9c227d87c36d..b39047463075 100644 > --- a/arch/arm64/kvm/hyp/include/nvhe/mem_protect.h > +++ b/arch/arm64/kvm/hyp/include/nvhe/mem_protect.h > @@ -23,6 +23,7 @@ extern struct host_kvm host_kvm; > int __pkvm_prot_finalize(void); > int __pkvm_mark_hyp(phys_addr_t start, phys_addr_t end); > > +int host_stage2_idmap_locked(u64 start, u64 end, enum kvm_pgtable_prot prot); > int kvm_host_prepare_stage2(void *pgt_pool_base); > void handle_host_mem_abort(struct kvm_cpu_context *host_ctxt); > > diff --git a/arch/arm64/kvm/hyp/nvhe/mem_protect.c b/arch/arm64/kvm/hyp/nvhe/mem_protect.c > index cdace80d3e28..6f28edf58407 100644 > --- a/arch/arm64/kvm/hyp/nvhe/mem_protect.c > +++ b/arch/arm64/kvm/hyp/nvhe/mem_protect.c > @@ -235,6 +235,11 @@ static bool host_stage2_want_pte_cb(u64 addr, u64 end, enum kvm_pgtable_prot pro > return prot != KVM_PGTABLE_PROT_RW; > } > > +int host_stage2_idmap_locked(u64 start, u64 end, enum kvm_pgtable_prot prot) > +{ > + return host_stage2_try(__host_stage2_idmap, start, end, prot); > +} > + > static int host_stage2_idmap(u64 addr) > { > enum kvm_pgtable_prot prot = KVM_PGTABLE_PROT_RW; > @@ -250,7 +255,7 @@ static int host_stage2_idmap(u64 addr) > if (ret) > goto unlock; > > - ret = host_stage2_try(__host_stage2_idmap, range.start, range.end, prot); > + ret = host_stage2_idmap_locked(range.start, range.end, prot); > unlock: > hyp_spin_unlock(&host_kvm.lock); > > diff --git a/arch/arm64/kvm/hyp/nvhe/setup.c b/arch/arm64/kvm/hyp/nvhe/setup.c > index 0b574d106519..74dce83a6fad 100644 > --- a/arch/arm64/kvm/hyp/nvhe/setup.c > +++ b/arch/arm64/kvm/hyp/nvhe/setup.c > @@ -83,10 +83,6 @@ static int recreate_hyp_mappings(phys_addr_t phys, unsigned long size, > if (ret) > return ret; > > - ret = pkvm_create_mappings(__start_rodata, __end_rodata, PAGE_HYP_RO); > - if (ret) > - return ret; > - > ret = pkvm_create_mappings(__hyp_rodata_start, __hyp_rodata_end, PAGE_HYP_RO); > if (ret) > return ret; > @@ -95,10 +91,6 @@ static int recreate_hyp_mappings(phys_addr_t phys, unsigned long size, > if (ret) > return ret; > > - ret = pkvm_create_mappings(__hyp_bss_end, __bss_stop, PAGE_HYP_RO); > - if (ret) > - return ret; > - > ret = pkvm_create_mappings(virt, virt + size, PAGE_HYP); > if (ret) > return ret; > @@ -117,6 +109,25 @@ static int recreate_hyp_mappings(phys_addr_t phys, unsigned long size, > return ret; > } > > + /* > + * Map the host's .bss and .rodata sections RO in the hypervisor, but > + * transfer the ownerhsip from the host to the hypervisor itself to > + * make sure it can't be donated or shared with another entity. > + * > + * The ownership transtion requires matching changes in the host > + * stage-2. This will done later (see finalize_mappings()) once the > + * hyp_vmemmap is addressable. > + */ > + ret = pkvm_create_mappings(__start_rodata, __end_rodata, > + PAGE_HYP_RO | KVM_PGTABLE_STATE_SHARED); > + if (ret) > + return ret; > + > + ret = pkvm_create_mappings(__hyp_bss_end, __bss_stop, > + PAGE_HYP_RO | KVM_PGTABLE_STATE_SHARED); > + if (ret) > + return ret; > + > return 0; > } > > @@ -148,6 +159,27 @@ static void hpool_put_page(void *addr) > hyp_put_page(&hpool, addr); > } > > +static int finalize_mappings(void) > +{ > + enum kvm_pgtable_prot prot = KVM_PGTABLE_PROT_RWX; > + int ret; > + > + /* > + * The host's .bss and .rodata sections are now conceptually owned by > + * the hypervisor, so mark them as 'borrowed' in the host stage-2. We > + * can safely use host_stage2_idmap_locked() at this point since the > + * host stage-2 has not been enabled yet. > + */ > + prot |= KVM_PGTABLE_STATE_SHARED | KVM_PGTABLE_STATE_BORROWED; > + ret = host_stage2_idmap_locked(__hyp_pa(__start_rodata), > + __hyp_pa(__end_rodata), prot); Do we really want to map the rodata section as RWX? > + if (ret) > + return ret; > + > + return host_stage2_idmap_locked(__hyp_pa(__hyp_bss_end), > + __hyp_pa(__bss_stop), prot); If the 'locked' state implies SHARED+BORROWED, maybe consider moving the ORRing of the prot into host_stage2_idmap_locked()? > +} > + > void __noreturn __pkvm_init_finalise(void) > { > struct kvm_host_data *host_data = this_cpu_ptr(&kvm_host_data); > @@ -167,6 +199,10 @@ void __noreturn __pkvm_init_finalise(void) > if (ret) > goto out; > > + ret = finalize_mappings(); > + if (ret) > + goto out; > + > pkvm_pgtable_mm_ops = (struct kvm_pgtable_mm_ops) { > .zalloc_page = hyp_zalloc_hyp_page, > .phys_to_virt = hyp_phys_to_virt, Thanks, M. -- Without deviation from the norm, progress is not possible. _______________________________________________ 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 X-Spam-Level: X-Spam-Status: No, score=-15.5 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id ABABFC6377C for ; Mon, 19 Jul 2021 15:03:09 +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 808E7611ED for ; Mon, 19 Jul 2021 15:03:09 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 808E7611ED Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=kernel.org Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@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:MIME-Version:References:In-Reply-To: Subject:Cc:To:From:Message-ID:Date:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=5O4sARRexRll/d5fb3P9QtO/kpcNgH+fApQbYWLBaIU=; b=3mN2StXq/WEySF /trYICRo+0lvY4ARBPhJ4uO9S/G5Y6n7sMOA2Jr2uPlLA0mcThkvdhfwNM8Aap+h9qtsw/1J0Pcf1 QsnJM3w715hfPUtKSZPwWsSeQ8W1vcu7P++iHCu1332/bPtnneOWKbIfGW3PIIuTNqBMYzBioI/FG EkVTyxoQTooZdSUAqjw9Z/mlJabL4nR0nbQWVPI1Nq08DINUULcMLQ1ABEl5gpppn9ttgknvvN9Q3 vSWEXt+wAIBJ2vXMGB3hiFyOGHiov4qc0AMGZcsjTusIfi4bX5Lp8rrSQneVdQRCKYdb0QTDv5N8O BrgOkMXHoFvtO1N3kqBQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1m5UmB-009zZ6-Df; Mon, 19 Jul 2021 15:01:47 +0000 Received: from mail.kernel.org ([198.145.29.99]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1m5Um7-009zXt-Lv for linux-arm-kernel@lists.infradead.org; Mon, 19 Jul 2021 15:01:45 +0000 Received: from disco-boy.misterjones.org (disco-boy.misterjones.org [51.254.78.96]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 6F9ED61181; Mon, 19 Jul 2021 15:01:43 +0000 (UTC) Received: from sofa.misterjones.org ([185.219.108.64] helo=why.misterjones.org) by disco-boy.misterjones.org with esmtpsa (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1m5Um5-00EEYM-CC; Mon, 19 Jul 2021 16:01:41 +0100 Date: Mon, 19 Jul 2021 16:01:40 +0100 Message-ID: <87eebujqjv.wl-maz@kernel.org> From: Marc Zyngier To: Quentin Perret Cc: james.morse@arm.com, alexandru.elisei@arm.com, suzuki.poulose@arm.com, catalin.marinas@arm.com, will@kernel.org, linux-arm-kernel@lists.infradead.org, kvmarm@lists.cs.columbia.edu, linux-kernel@vger.kernel.org, ardb@kernel.org, qwandor@google.com, tabba@google.com, dbrazdil@google.com, kernel-team@android.com Subject: Re: [PATCH 09/14] KVM: arm64: Mark host bss and rodata section as shared In-Reply-To: <20210719104735.3681732-10-qperret@google.com> References: <20210719104735.3681732-1-qperret@google.com> <20210719104735.3681732-10-qperret@google.com> User-Agent: Wanderlust/2.15.9 (Almost Unreal) SEMI-EPG/1.14.7 (Harue) FLIM-LB/1.14.9 (=?UTF-8?B?R29qxY0=?=) APEL-LB/10.8 EasyPG/1.0.0 Emacs/27.1 (x86_64-pc-linux-gnu) MULE/6.0 (HANACHIRUSATO) MIME-Version: 1.0 (generated by SEMI-EPG 1.14.7 - "Harue") X-SA-Exim-Connect-IP: 185.219.108.64 X-SA-Exim-Rcpt-To: qperret@google.com, james.morse@arm.com, alexandru.elisei@arm.com, suzuki.poulose@arm.com, catalin.marinas@arm.com, will@kernel.org, linux-arm-kernel@lists.infradead.org, kvmarm@lists.cs.columbia.edu, linux-kernel@vger.kernel.org, ardb@kernel.org, qwandor@google.com, tabba@google.com, dbrazdil@google.com, kernel-team@android.com X-SA-Exim-Mail-From: maz@kernel.org X-SA-Exim-Scanned: No (on disco-boy.misterjones.org); SAEximRunCond expanded to false X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20210719_080143_813594_07E0E962 X-CRM114-Status: GOOD ( 34.25 ) 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 Mon, 19 Jul 2021 11:47:30 +0100, Quentin Perret wrote: > > As the hypervisor maps the host's .bss and .rodata sections in its > stage-1, make sure to tag them as shared in hyp and host page-tables. > > But since the hypervisor relies on the presence of these mappings, we > cannot let the host in complete control of the memory regions -- it > must not unshare or donate them to another entity for example. To > prevent this, let's transfer the ownership of those ranges to the > hypervisor itself, and share the page back with the host. > > Signed-off-by: Quentin Perret > --- > arch/arm64/kvm/hyp/include/nvhe/mem_protect.h | 1 + > arch/arm64/kvm/hyp/nvhe/mem_protect.c | 7 ++- > arch/arm64/kvm/hyp/nvhe/setup.c | 52 ++++++++++++++++--- > 3 files changed, 51 insertions(+), 9 deletions(-) > > diff --git a/arch/arm64/kvm/hyp/include/nvhe/mem_protect.h b/arch/arm64/kvm/hyp/include/nvhe/mem_protect.h > index 9c227d87c36d..b39047463075 100644 > --- a/arch/arm64/kvm/hyp/include/nvhe/mem_protect.h > +++ b/arch/arm64/kvm/hyp/include/nvhe/mem_protect.h > @@ -23,6 +23,7 @@ extern struct host_kvm host_kvm; > int __pkvm_prot_finalize(void); > int __pkvm_mark_hyp(phys_addr_t start, phys_addr_t end); > > +int host_stage2_idmap_locked(u64 start, u64 end, enum kvm_pgtable_prot prot); > int kvm_host_prepare_stage2(void *pgt_pool_base); > void handle_host_mem_abort(struct kvm_cpu_context *host_ctxt); > > diff --git a/arch/arm64/kvm/hyp/nvhe/mem_protect.c b/arch/arm64/kvm/hyp/nvhe/mem_protect.c > index cdace80d3e28..6f28edf58407 100644 > --- a/arch/arm64/kvm/hyp/nvhe/mem_protect.c > +++ b/arch/arm64/kvm/hyp/nvhe/mem_protect.c > @@ -235,6 +235,11 @@ static bool host_stage2_want_pte_cb(u64 addr, u64 end, enum kvm_pgtable_prot pro > return prot != KVM_PGTABLE_PROT_RW; > } > > +int host_stage2_idmap_locked(u64 start, u64 end, enum kvm_pgtable_prot prot) > +{ > + return host_stage2_try(__host_stage2_idmap, start, end, prot); > +} > + > static int host_stage2_idmap(u64 addr) > { > enum kvm_pgtable_prot prot = KVM_PGTABLE_PROT_RW; > @@ -250,7 +255,7 @@ static int host_stage2_idmap(u64 addr) > if (ret) > goto unlock; > > - ret = host_stage2_try(__host_stage2_idmap, range.start, range.end, prot); > + ret = host_stage2_idmap_locked(range.start, range.end, prot); > unlock: > hyp_spin_unlock(&host_kvm.lock); > > diff --git a/arch/arm64/kvm/hyp/nvhe/setup.c b/arch/arm64/kvm/hyp/nvhe/setup.c > index 0b574d106519..74dce83a6fad 100644 > --- a/arch/arm64/kvm/hyp/nvhe/setup.c > +++ b/arch/arm64/kvm/hyp/nvhe/setup.c > @@ -83,10 +83,6 @@ static int recreate_hyp_mappings(phys_addr_t phys, unsigned long size, > if (ret) > return ret; > > - ret = pkvm_create_mappings(__start_rodata, __end_rodata, PAGE_HYP_RO); > - if (ret) > - return ret; > - > ret = pkvm_create_mappings(__hyp_rodata_start, __hyp_rodata_end, PAGE_HYP_RO); > if (ret) > return ret; > @@ -95,10 +91,6 @@ static int recreate_hyp_mappings(phys_addr_t phys, unsigned long size, > if (ret) > return ret; > > - ret = pkvm_create_mappings(__hyp_bss_end, __bss_stop, PAGE_HYP_RO); > - if (ret) > - return ret; > - > ret = pkvm_create_mappings(virt, virt + size, PAGE_HYP); > if (ret) > return ret; > @@ -117,6 +109,25 @@ static int recreate_hyp_mappings(phys_addr_t phys, unsigned long size, > return ret; > } > > + /* > + * Map the host's .bss and .rodata sections RO in the hypervisor, but > + * transfer the ownerhsip from the host to the hypervisor itself to > + * make sure it can't be donated or shared with another entity. > + * > + * The ownership transtion requires matching changes in the host > + * stage-2. This will done later (see finalize_mappings()) once the > + * hyp_vmemmap is addressable. > + */ > + ret = pkvm_create_mappings(__start_rodata, __end_rodata, > + PAGE_HYP_RO | KVM_PGTABLE_STATE_SHARED); > + if (ret) > + return ret; > + > + ret = pkvm_create_mappings(__hyp_bss_end, __bss_stop, > + PAGE_HYP_RO | KVM_PGTABLE_STATE_SHARED); > + if (ret) > + return ret; > + > return 0; > } > > @@ -148,6 +159,27 @@ static void hpool_put_page(void *addr) > hyp_put_page(&hpool, addr); > } > > +static int finalize_mappings(void) > +{ > + enum kvm_pgtable_prot prot = KVM_PGTABLE_PROT_RWX; > + int ret; > + > + /* > + * The host's .bss and .rodata sections are now conceptually owned by > + * the hypervisor, so mark them as 'borrowed' in the host stage-2. We > + * can safely use host_stage2_idmap_locked() at this point since the > + * host stage-2 has not been enabled yet. > + */ > + prot |= KVM_PGTABLE_STATE_SHARED | KVM_PGTABLE_STATE_BORROWED; > + ret = host_stage2_idmap_locked(__hyp_pa(__start_rodata), > + __hyp_pa(__end_rodata), prot); Do we really want to map the rodata section as RWX? > + if (ret) > + return ret; > + > + return host_stage2_idmap_locked(__hyp_pa(__hyp_bss_end), > + __hyp_pa(__bss_stop), prot); If the 'locked' state implies SHARED+BORROWED, maybe consider moving the ORRing of the prot into host_stage2_idmap_locked()? > +} > + > void __noreturn __pkvm_init_finalise(void) > { > struct kvm_host_data *host_data = this_cpu_ptr(&kvm_host_data); > @@ -167,6 +199,10 @@ void __noreturn __pkvm_init_finalise(void) > if (ret) > goto out; > > + ret = finalize_mappings(); > + if (ret) > + goto out; > + > pkvm_pgtable_mm_ops = (struct kvm_pgtable_mm_ops) { > .zalloc_page = hyp_zalloc_hyp_page, > .phys_to_virt = hyp_phys_to_virt, Thanks, M. -- Without deviation from the norm, progress is not possible. _______________________________________________ linux-arm-kernel mailing list linux-arm-kernel@lists.infradead.org http://lists.infradead.org/mailman/listinfo/linux-arm-kernel