Received: by 2002:a05:6a10:6d10:0:0:0:0 with SMTP id gq16csp2645974pxb; Mon, 18 Apr 2022 05:29:24 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyu5EZxBWdM01sQl32rs15/I7u/7WHtlmJR4dO3M87PnB0oUrzl1ixoF+X0NB8XK6hPn+aP X-Received: by 2002:a05:6402:14c2:b0:41d:816a:4eb0 with SMTP id f2-20020a05640214c200b0041d816a4eb0mr11795189edx.301.1650284963957; Mon, 18 Apr 2022 05:29:23 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1650284963; cv=none; d=google.com; s=arc-20160816; b=brUzs7ZOmE9t2Jx231JENPIuiFHnfSNAV0eGcYJez+09l1ifN4XiCJe2RgTqRjNsoa RZbif3oxho/5hpSQNqE317IE21m2bCg2BgFTZ9kpmGOIuhh0pQVP3ofyQyf66tsLT9u4 rSkCPJw1KF7XDb97jpWzHrb3LLxosJJ/tyamjtSU4PDdf7KW3r5EM6jcuRWrVdBUVEbf BKgHgmRRrB5OpQJU0G+TXNFXIKycOv61g3U+BaHldqRkWaR8gMFN7dp8P+G1AtJQuTdG n6qziVDpMs8FXY1baQRbzBLOzQgRMyPCo/lOqSIL0An4nMIaVs8OEGTd/XDc2JsphWf/ A26g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:mime-version:user-agent:references:in-reply-to :subject:cc:to:from:message-id:date:dkim-signature; bh=t8tsiX/3YCL8H5f0+l1anzukPM6SRJV2y+Njn/DgoTE=; b=Wlk1kpaF9eRw8JDm5o87tVUKEt1EekT+NduVQN/bUX/NsoxhQRLvh6nLeQioaIvyb4 2AgR3CCyHzvzMLniCFIfSlCMeFoGsJVgtULjD4s4ohEuHRGVTwDzuF6eiSehFDMVeTp8 MDc1QJjCHxVHwZo4RGbiF3d4iiQB4mBSRqd+5m25vB/eDLxp5rT131yJuXVo6HZL4b6b VfqqyIrNBkEQ9+HdU9Qr+kSxLLR7DGo9+ssjs7Q+WpWR/mb89QPrfcTE8hDncM6NeJ7r kla3SCWHju53h0wr3PxVqmoWNytaN35AVFcsS2cJDIeEVFfTw3/1Pww6Q9/VvJnNGXqd ahIw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=HfrYEnwC; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id x59-20020a50bac1000000b0041cd837afccsi6350220ede.617.2022.04.18.05.29.00; Mon, 18 Apr 2022 05:29:23 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=HfrYEnwC; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234307AbiDRKEF (ORCPT + 99 others); Mon, 18 Apr 2022 06:04:05 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48246 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229464AbiDRKED (ORCPT ); Mon, 18 Apr 2022 06:04:03 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [IPv6:2604:1380:4601:e00::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D13E217071 for ; Mon, 18 Apr 2022 03:01:24 -0700 (PDT) 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 8728EB80E61 for ; Mon, 18 Apr 2022 10:01:23 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 1D942C385A7; Mon, 18 Apr 2022 10:01:22 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1650276082; bh=58hSBSLrjx6v6M4mAOKijWKEjXw1WVtJOTUJiGLqYoc=; h=Date:From:To:Cc:Subject:In-Reply-To:References:From; b=HfrYEnwCeJhwwbhSTvoXJTwEp3Dg8c+SqBTmtNinSWEBp8cGMW48I80tbwqY9f9lM 7YPudmc/pTNsvltW3sKPN1UBkYtgphSMU5TeKcx6ggvhn9xOFMgvTIpcv5TQR6X397 DoYRylfBlOb8Y4/nwdMSwrD+hiU08Stttn6I88o2rKNFZJXlabBYG1Yrgqs43pczqH ish42gHLrxtkszBVSl+jwSOG06mNf3PCwnappv8HHiyBXXVqszrsPu+mVGQK+KOS1v 9aAE/HATd96G18T+3RyyCMQA92NQ1y4P70c6fU4HYptdEidxU6h63fBiBtMcPXn8Fe rztTQD654P5wA== 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 1ngOC7-004ycO-LZ; Mon, 18 Apr 2022 11:01:19 +0100 Date: Mon, 18 Apr 2022 11:01:19 +0100 Message-ID: <87wnfmaexc.wl-maz@kernel.org> From: Marc Zyngier To: Kalesh Singh Cc: will@kernel.org, qperret@google.com, tabba@google.com, surenb@google.com, kernel-team@android.com, James Morse , Alexandru Elisei , Suzuki K Poulose , Catalin Marinas , Mark Rutland , Andrew Jones , Nick Desaulniers , Masahiro Yamada , Changbin Du , linux-arm-kernel@lists.infradead.org, kvmarm@lists.cs.columbia.edu, linux-kernel@vger.kernel.org Subject: Re: [PATCH v7 3/6] KVM: arm64: Add guard pages for KVM nVHE hypervisor stack In-Reply-To: <20220408200349.1529080-4-kaleshsingh@google.com> References: <20220408200349.1529080-1-kaleshsingh@google.com> <20220408200349.1529080-4-kaleshsingh@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: kaleshsingh@google.com, will@kernel.org, qperret@google.com, tabba@google.com, surenb@google.com, kernel-team@android.com, james.morse@arm.com, alexandru.elisei@arm.com, suzuki.poulose@arm.com, catalin.marinas@arm.com, mark.rutland@arm.com, drjones@redhat.com, ndesaulniers@google.com, masahiroy@kernel.org, changbin.du@intel.com, linux-arm-kernel@lists.infradead.org, kvmarm@lists.cs.columbia.edu, linux-kernel@vger.kernel.org X-SA-Exim-Mail-From: maz@kernel.org X-SA-Exim-Scanned: No (on disco-boy.misterjones.org); SAEximRunCond expanded to false X-Spam-Status: No, score=-7.7 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Fri, 08 Apr 2022 21:03:26 +0100, Kalesh Singh wrote: > > Map the stack pages in the flexible private VA range and allocate > guard pages below the stack as unbacked VA space. The stack is aligned > so that any valid stack address has PAGE_SHIFT bit as 1 - this is used > for overflow detection (implemented in a subsequent patch in the series). > > Signed-off-by: Kalesh Singh > Tested-by: Fuad Tabba > Reviewed-by: Fuad Tabba > --- > > Changes in v7: > - Add Fuad's Reviewed-by and Tested-by tags. > > Changes in v6: > - Update call to hyp_alloc_private_va_range() (return val and params) > > Changes in v5: > - Use a single allocation for stack and guard pages to ensure they > are contiguous, per Marc > > Changes in v4: > - Replace IS_ERR_OR_NULL check with IS_ERR check now that > hyp_alloc_private_va_range() returns an error for null > pointer, per Fuad > - Format comments to < 80 cols, per Fuad > > Changes in v3: > - Handle null ptr in IS_ERR_OR_NULL checks, per Mark > > > arch/arm64/include/asm/kvm_asm.h | 1 + > arch/arm64/include/asm/kvm_mmu.h | 3 +++ > arch/arm64/kvm/arm.c | 39 +++++++++++++++++++++++++++++--- > arch/arm64/kvm/mmu.c | 4 ++-- > 4 files changed, 42 insertions(+), 5 deletions(-) > > diff --git a/arch/arm64/include/asm/kvm_asm.h b/arch/arm64/include/asm/kvm_asm.h > index d5b0386ef765..2e277f2ed671 100644 > --- a/arch/arm64/include/asm/kvm_asm.h > +++ b/arch/arm64/include/asm/kvm_asm.h > @@ -169,6 +169,7 @@ struct kvm_nvhe_init_params { > unsigned long tcr_el2; > unsigned long tpidr_el2; > unsigned long stack_hyp_va; > + unsigned long stack_pa; > phys_addr_t pgd_pa; > unsigned long hcr_el2; > unsigned long vttbr; > diff --git a/arch/arm64/include/asm/kvm_mmu.h b/arch/arm64/include/asm/kvm_mmu.h > index a50cbb5ba402..b805316c4866 100644 > --- a/arch/arm64/include/asm/kvm_mmu.h > +++ b/arch/arm64/include/asm/kvm_mmu.h > @@ -117,6 +117,9 @@ alternative_cb_end > #include > #include > > +extern struct kvm_pgtable *hyp_pgtable; > +extern struct mutex kvm_hyp_pgd_mutex; I'd rather you don't expose this at all. > + > void kvm_update_va_mask(struct alt_instr *alt, > __le32 *origptr, __le32 *updptr, int nr_inst); > void kvm_compute_layout(void); > diff --git a/arch/arm64/kvm/arm.c b/arch/arm64/kvm/arm.c > index 523bc934fe2f..5687c0175151 100644 > --- a/arch/arm64/kvm/arm.c > +++ b/arch/arm64/kvm/arm.c > @@ -1483,7 +1483,6 @@ static void cpu_prepare_hyp_mode(int cpu) > tcr |= (idmap_t0sz & GENMASK(TCR_TxSZ_WIDTH - 1, 0)) << TCR_T0SZ_OFFSET; > params->tcr_el2 = tcr; > > - params->stack_hyp_va = kern_hyp_va(per_cpu(kvm_arm_hyp_stack_page, cpu) + PAGE_SIZE); > params->pgd_pa = kvm_mmu_get_httbr(); > if (is_protected_kvm_enabled()) > params->hcr_el2 = HCR_HOST_NVHE_PROTECTED_FLAGS; > @@ -1933,14 +1932,48 @@ static int init_hyp_mode(void) > * Map the Hyp stack pages > */ > for_each_possible_cpu(cpu) { > + struct kvm_nvhe_init_params *params = per_cpu_ptr_nvhe_sym(kvm_init_params, cpu); > char *stack_page = (char *)per_cpu(kvm_arm_hyp_stack_page, cpu); > - err = create_hyp_mappings(stack_page, stack_page + PAGE_SIZE, > - PAGE_HYP); > + unsigned long hyp_addr; > > + /* > + * Allocate a contiguous HYP private VA range for the stack > + * and guard page. The allocation is also aligned based on > + * the order of its size. > + */ > + err = hyp_alloc_private_va_range(PAGE_SIZE * 2, &hyp_addr); > + if (err) { > + kvm_err("Cannot allocate hyp stack guard page\n"); > + goto out_err; > + } > + > + /* > + * Since the stack grows downwards, map the stack to the page > + * at the higher address and leave the lower guard page > + * unbacked. > + * > + * Any valid stack address now has the PAGE_SHIFT bit as 1 > + * and addresses corresponding to the guard page have the > + * PAGE_SHIFT bit as 0 - this is used for overflow detection. > + */ > + mutex_lock(&kvm_hyp_pgd_mutex); > + err = kvm_pgtable_hyp_map(hyp_pgtable, hyp_addr + PAGE_SIZE, > + PAGE_SIZE, __pa(stack_page), PAGE_HYP); > + mutex_unlock(&kvm_hyp_pgd_mutex); The mutex (and the HYP page table structure) really should stay private to the MMU code. Just add a new helper that will take the lock and use hyp_pgtable. > if (err) { > kvm_err("Cannot map hyp stack\n"); > goto out_err; > } > + > + /* > + * Save the stack PA in nvhe_init_params. This will be needed > + * to recreate the stack mapping in protected nVHE mode. > + * __hyp_pa() won't do the right thing there, since the stack > + * has been mapped in the flexible private VA space. > + */ > + params->stack_pa = __pa(stack_page); > + > + params->stack_hyp_va = hyp_addr + (2 * PAGE_SIZE); > } > > for_each_possible_cpu(cpu) { > diff --git a/arch/arm64/kvm/mmu.c b/arch/arm64/kvm/mmu.c > index 3d3efea4e991..a54f00bd06cc 100644 > --- a/arch/arm64/kvm/mmu.c > +++ b/arch/arm64/kvm/mmu.c > @@ -22,8 +22,8 @@ > > #include "trace.h" > > -static struct kvm_pgtable *hyp_pgtable; > -static DEFINE_MUTEX(kvm_hyp_pgd_mutex); > +struct kvm_pgtable *hyp_pgtable; > +DEFINE_MUTEX(kvm_hyp_pgd_mutex); > > static unsigned long hyp_idmap_start; > static unsigned long hyp_idmap_end; Thanks, M. -- Without deviation from the norm, progress is not possible.