Received: by 2002:a05:6a10:9afc:0:0:0:0 with SMTP id t28csp272387pxm; Wed, 2 Mar 2022 15:07:05 -0800 (PST) X-Google-Smtp-Source: ABdhPJxDHNpH2PlyspAb/TtPT1ByVw9A/MB9z75/+nldqQIxr5jCZUHIWH2TRToBmkRA4vqUlZ6J X-Received: by 2002:a05:6a00:1f9b:b0:4f4:1c0b:7102 with SMTP id bg27-20020a056a001f9b00b004f41c0b7102mr14986232pfb.62.1646262425246; Wed, 02 Mar 2022 15:07:05 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1646262425; cv=none; d=google.com; s=arc-20160816; b=oeH01M37iq8HowEauZjSispQ7fIX9uYe43/mS4oNBU21TCtG/xZdEtHDzJrYjEPPR8 bsZILeBQ5/1OEVui1V/jikJdAVVjBJXF4Sjg/ZuMWCBqtwRAdtrHyNoEMHdEY3ma48PN p9QG8AyJ4IzNOL5WUss/OWegeePWI/dWalDMMIkt87/4R/MKItTLoxNBmz1zjR+eZQGn eBqSPHLebwvBYwRjnKAU9YwPNu1kMEbtNbPXGxIHRYA6WiioTVwIG+JDW/MPidh9FVx2 xw1VRLI9DeSXZizMkG2crvihDLveQBseZu/YMjJiea7OQHEGT6LFuA8peI6BALRuU0rs uKqA== 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=O+jj/6AkxkumbtwT1XLrqFQPMUhDF+YRh8/UKktdPFg=; b=Ap5WGU6bHvLX8smle4Gu1iD9OQfgNjvw66UfeAn4pET35kN1E29jX2NPUfPUunSQBQ 4tU+A+ia9NADqeZvnyfORkUKtzb6pxT6s002fI8Mjer6zMJ69PxeUk1lwjnlJX4i4Hey 6OBJ/zOP2YJfjcMH18+z2CcJz9efSvENRz9oPYZSdEhx9K+cZHDljMVQbCKb9KWRg3G9 Kd3SPXXxoJ7HlY4yFQiebo90HrwV08RShOJgkNwMKSzHXUO/+GP2gRmrMDauUiMeMKmZ 0ddxjApB1KiFdBY6ETq11RDyP1+/DdYF7t8BU3RfUMiSd44hFK88raXpyGq3j5/HHf7C pvJA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=hov9VTF9; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 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 lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [2620:137:e000::1:18]) by mx.google.com with ESMTPS id p16-20020a170903249000b00151a6327ebfsi352453plw.90.2022.03.02.15.07.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 02 Mar 2022 15:07:05 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) client-ip=2620:137:e000::1:18; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=hov9VTF9; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id E773139697; Wed, 2 Mar 2022 14:48:45 -0800 (PST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239935AbiCBHyi (ORCPT + 99 others); Wed, 2 Mar 2022 02:54:38 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:32806 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233208AbiCBHyg (ORCPT ); Wed, 2 Mar 2022 02:54:36 -0500 Received: from ams.source.kernel.org (ams.source.kernel.org [IPv6:2604:1380:4601:e00::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id F2EA85F8FD for ; Tue, 1 Mar 2022 23:53:52 -0800 (PST) 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 C17D3B81F1F for ; Wed, 2 Mar 2022 07:53:50 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 7E911C004E1; Wed, 2 Mar 2022 07:53:49 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1646207629; bh=9NSP4DT8lrHgNHqaiQ/w0hWpRVSiCZAEBVQEdkPu8lI=; h=Date:From:To:Cc:Subject:In-Reply-To:References:From; b=hov9VTF9WXhCSj2BQLi5h9lU7A51DYWpLNtK0P8Y28T7E9oQFFCOfYjstpxILTIqN 14HUaLurhRfaeK8GpLPw7RYtdW7lgcQ+Mi7xv8b+WMPgGnqXYQ5diNfeFTkhfzcldy +S27YnzWtNsT//Lk4UWxGAYytrOK7EOmR9+sQsJqWzbRhBLb5LiBWlTVH3k/7SHEDD Nf0+lZXVS9n+UU7B5R+6tzBQhqI6CBClMLXNFg+Qh2BjFSXuqQrNwmtT7srYvhqbvy X2swTfAwhwwDzv7Y3yb+BWcLvr4Cjdv1q/I4qR8FZ+qLmOUHPrYdoV5t0Dx3+cYyCm +VYBQQG/zRCkg== Received: from ip-185-104-136-29.ptr.icomera.net ([185.104.136.29] helo=billy-the-mountain.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 1nPJnu-00BbmA-P4; Wed, 02 Mar 2022 07:53:47 +0000 Date: Wed, 02 Mar 2022 07:53:40 +0000 Message-ID: <87tucg6b97.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 , Mark Brown , Masami Hiramatsu , Peter Collingbourne , "Madhavan T. Venkataraman" , Andrew Walbran , Andrew Scull , linux-arm-kernel@lists.infradead.org, kvmarm@lists.cs.columbia.edu, linux-kernel@vger.kernel.org Subject: Re: [PATCH v4 3/8] KVM: arm64: Add guard pages for KVM nVHE hypervisor stack In-Reply-To: <20220225033548.1912117-4-kaleshsingh@google.com> References: <20220225033548.1912117-1-kaleshsingh@google.com> <20220225033548.1912117-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 (aarch64-unknown-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.104.136.29 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, broonie@kernel.org, mhiramat@kernel.org, pcc@google.com, madvenka@linux.microsoft.com, qwandor@google.com, ascull@google.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=-2.7 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,MAILING_LIST_MULTI, RDNS_NONE,SPF_HELO_NONE,T_SCC_BODY_TEXT_LINE autolearn=unavailable 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, 25 Feb 2022 03:34:48 +0000, Kalesh Singh wrote: > > Maps the stack pages in the flexible private VA range and allocates > guard pages below the stack as unbacked VA space. The stack is aligned > to twice its size to aid overflow detection (implemented in a subsequent > patch in the series). > > Signed-off-by: Kalesh Singh > --- > > 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/kvm/arm.c | 32 +++++++++++++++++++++++++++++--- > 2 files changed, 30 insertions(+), 3 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/kvm/arm.c b/arch/arm64/kvm/arm.c > index ecc5958e27fe..0a83c0e7f838 100644 > --- a/arch/arm64/kvm/arm.c > +++ b/arch/arm64/kvm/arm.c > @@ -1541,7 +1541,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; > @@ -1990,14 +1989,41 @@ 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 stack_hyp_va, guard_hyp_va; > > + /* > + * Private mappings are allocated downwards from io_map_base > + * so allocate the stack first then the guard page. > + * > + * The stack is aligned to twice its size to facilitate overflow > + * detection. > + */ > + err = __create_hyp_private_mapping(__pa(stack_page), PAGE_SIZE, > + PAGE_SIZE * 2, &stack_hyp_va, PAGE_HYP); Right, I guess that's where my earlier ask breaks, as you want an alignment that is *larger* than the allocation. > if (err) { > kvm_err("Cannot map hyp stack\n"); > goto out_err; > } > + > + /* Allocate unbacked private VA range for stack guard page */ > + guard_hyp_va = hyp_alloc_private_va_range(PAGE_SIZE, PAGE_SIZE); Huh. You are implicitly relying on the VA allocator handing you an address contiguous with the previous mapping. That's... brave. I'd rather you allocate the VA space upfront with the correct alignment and then map the single page where it should be in the VA region. That'd be a lot less fragile. > + if (IS_ERR((void *)guard_hyp_va)) { > + err = PTR_ERR((void *)guard_hyp_va); > + kvm_err("Cannot allocate hyp stack guard page\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) + PAGE_SIZE; > + > + params->stack_hyp_va = stack_hyp_va + PAGE_SIZE; > } > > for_each_possible_cpu(cpu) { Thanks, M. -- Without deviation from the norm, progress is not possible.