From nobody Mon May 11 00:10:12 2026 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 57176C433F5 for ; Wed, 20 Apr 2022 21:44:01 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1382628AbiDTVqq (ORCPT ); Wed, 20 Apr 2022 17:46:46 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33212 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S241443AbiDTVqo (ORCPT ); Wed, 20 Apr 2022 17:46:44 -0400 Received: from mail-yb1-xb49.google.com (mail-yb1-xb49.google.com [IPv6:2607:f8b0:4864:20::b49]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 02DDC43ACE for ; Wed, 20 Apr 2022 14:43:57 -0700 (PDT) Received: by mail-yb1-xb49.google.com with SMTP id d129-20020a254f87000000b006411bf3f331so2675651ybb.4 for ; Wed, 20 Apr 2022 14:43:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=date:in-reply-to:message-id:mime-version:references:subject:from:cc; bh=H7kRCZxE86B6SWxgyz7zRi2sY2pDlR4nBd1oa6QSQkw=; b=TTCcYduxyz27rtHm5u8I3DjglQhoNrJAIrlsxPZQiM0yBCqC2X5vG08pfDUgqS5D0S Yf4klwHp1Cy05ZRUNct7mdNPz79qITMOuOjzm0ybp9e0/UBjYQ3bHarl6JZ/Ptu4oYtY 6Y5jEHrpZWlVlPdYJwDQaNeQ4HtOsTFcxhAe0wopXwYBpn1Y05w+YZjqLYYF46/VaXyz soQ9gF1dGxoFiuKfBj2BRiJn3wt+MB3i7grEPI+CfFL/jqj80d9HsC3ZpHP4a3w0JFQb 5f965Ek9CQ9Htes05RLyI06DePLAEwRayeGgzF1B1W24ekpfCAiGI+u1OxGp/l4WZcpT Zq2Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:cc; bh=H7kRCZxE86B6SWxgyz7zRi2sY2pDlR4nBd1oa6QSQkw=; b=anS517A345FxLyJfiil6hs4/I2gwIueo0c/u9wjCSe0aaDho/rc7+iUHgxJNXKaowl SsLmZfA8eYldKITek6vj3s0h2TKwHB2NmihL8ZEgTznsUo7SpMPGJXEqrMIGT/EMaBY3 EfZ7Z/R4mvNUfrYLkWmv2enim1jeGEbaMjxqAKXJYN8iWhdYf4FutZSfGf87WPMlSM1e USaEg+4G/GM/aBGanD867ern6H/p+5ydA/vo6lAByicx27AAJHEHiSfnDEzJ5E0k3v4D Y4xvk8UK/asYw+WRBpTIjLQEaf31F5Q8p4mbg6Dwq1RKZ3L2irFbyZiE9hGrXnyfllFn ghxA== X-Gm-Message-State: AOAM531ZM34GzbSUAd8waJThfL03vs5XkqMqyJjxR22HpaE1MevxnnC4 pCsJuVOiaXsNSjGrryx/pxt9za9DWe6OMfAbFQ== X-Google-Smtp-Source: ABdhPJwbrLaihFbnA+0JkLe58ng3Jvdp2cEtJqbHJL4+AURy/vsesCBbc478unv4e+ALPKEGCZgQvP8Tw7XdAJ7X2A== X-Received: from kaleshsingh.mtv.corp.google.com ([2620:15c:211:200:2a20:cec4:8f41:cd6f]) (user=kaleshsingh job=sendgmr) by 2002:a81:1557:0:b0:2f4:d3eb:6428 with SMTP id 84-20020a811557000000b002f4d3eb6428mr863893ywv.234.1650491036209; Wed, 20 Apr 2022 14:43:56 -0700 (PDT) Date: Wed, 20 Apr 2022 14:42:52 -0700 In-Reply-To: <20220420214317.3303360-1-kaleshsingh@google.com> Message-Id: <20220420214317.3303360-2-kaleshsingh@google.com> Mime-Version: 1.0 References: <20220420214317.3303360-1-kaleshsingh@google.com> X-Mailer: git-send-email 2.36.0.rc0.470.gd361397f0d-goog Subject: [PATCH v8 1/6] KVM: arm64: Introduce hyp_alloc_private_va_range() From: Kalesh Singh Cc: will@kernel.org, maz@kernel.org, qperret@google.com, tabba@google.com, surenb@google.com, kernel-team@android.com, Kalesh Singh , James Morse , Alexandru Elisei , Suzuki K Poulose , Catalin Marinas , Mark Rutland , Andrew Jones , Ard Biesheuvel , Changbin Du , Nick Desaulniers , Masahiro Yamada , linux-arm-kernel@lists.infradead.org, kvmarm@lists.cs.columbia.edu, linux-kernel@vger.kernel.org To: unlisted-recipients:; (no To-header on input) Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" hyp_alloc_private_va_range() can be used to reserve private VA ranges in the nVHE hypervisor. Allocations are aligned based on the order of the requested size. This will be used to implement stack guard pages for KVM nVHE hypervisor (nVHE Hyp mode / not pKVM), in a subsequent patch in the series. Signed-off-by: Kalesh Singh Tested-by: Fuad Tabba Reviewed-by: Fuad Tabba --- Changes in v8 - Remove !base check in hyp_alloc_private_va_range(), per Marc - PAGE_ALIGN the size in __create_hyp_private_mapping(), per Marc Changes in v7: - Add Fuad's Reviewed-by and Tested-by tags. Changes in v6: - Update kernel-doc for hyp_alloc_private_va_range() and add return description, per Stephen - Update hyp_alloc_private_va_range() to return an int error code, per Stephen - Replace IS_ERR() checks with IS_ERR_VALUE() check, per Stephen - Clean up goto, per Stephen Changes in v5: - Align private allocations based on the order of their size, per Marc Changes in v4: - Handle null ptr in hyp_alloc_private_va_range() and replace IS_ERR_OR_NULL checks in callers with IS_ERR checks, per Fuad - Fix kernel-doc comments format, per Fuad Changes in v3: - Handle null ptr in IS_ERR_OR_NULL checks, per Mark arch/arm64/include/asm/kvm_mmu.h | 1 + arch/arm64/kvm/mmu.c | 64 +++++++++++++++++++++----------- 2 files changed, 44 insertions(+), 21 deletions(-) diff --git a/arch/arm64/include/asm/kvm_mmu.h b/arch/arm64/include/asm/kvm_= mmu.h index 74735a864eee..a50cbb5ba402 100644 --- a/arch/arm64/include/asm/kvm_mmu.h +++ b/arch/arm64/include/asm/kvm_mmu.h @@ -154,6 +154,7 @@ static __always_inline unsigned long __kern_hyp_va(unsi= gned long v) int kvm_share_hyp(void *from, void *to); void kvm_unshare_hyp(void *from, void *to); int create_hyp_mappings(void *from, void *to, enum kvm_pgtable_prot prot); +int hyp_alloc_private_va_range(size_t size, unsigned long *haddr); int create_hyp_io_mappings(phys_addr_t phys_addr, size_t size, void __iomem **kaddr, void __iomem **haddr); diff --git a/arch/arm64/kvm/mmu.c b/arch/arm64/kvm/mmu.c index 53ae2c0640bc..7de1e02ebfd1 100644 --- a/arch/arm64/kvm/mmu.c +++ b/arch/arm64/kvm/mmu.c @@ -457,23 +457,22 @@ int create_hyp_mappings(void *from, void *to, enum kv= m_pgtable_prot prot) return 0; } =20 -static int __create_hyp_private_mapping(phys_addr_t phys_addr, size_t size, - unsigned long *haddr, - enum kvm_pgtable_prot prot) + +/** + * hyp_alloc_private_va_range - Allocates a private VA range. + * @size: The size of the VA range to reserve. + * @haddr: The hypervisor virtual start address of the allocation. + * + * The private virtual address (VA) range is allocated below io_map_base + * and aligned based on the order of @size. + * + * Return: 0 on success or negative error code on failure. + */ +int hyp_alloc_private_va_range(size_t size, unsigned long *haddr) { unsigned long base; int ret =3D 0; =20 - if (!kvm_host_owns_hyp_mappings()) { - base =3D kvm_call_hyp_nvhe(__pkvm_create_private_mapping, - phys_addr, size, prot); - if (IS_ERR_OR_NULL((void *)base)) - return PTR_ERR((void *)base); - *haddr =3D base; - - return 0; - } - mutex_lock(&kvm_hyp_pgd_mutex); =20 /* @@ -484,8 +483,10 @@ static int __create_hyp_private_mapping(phys_addr_t ph= ys_addr, size_t size, * * The allocated size is always a multiple of PAGE_SIZE. */ - size =3D PAGE_ALIGN(size + offset_in_page(phys_addr)); - base =3D io_map_base - size; + base =3D io_map_base - PAGE_ALIGN(size); + + /* Align the allocation based on the order of its size */ + base =3D ALIGN_DOWN(base, PAGE_SIZE << get_order(size)); =20 /* * Verify that BIT(VA_BITS - 1) hasn't been flipped by @@ -495,19 +496,40 @@ static int __create_hyp_private_mapping(phys_addr_t p= hys_addr, size_t size, if ((base ^ io_map_base) & BIT(VA_BITS - 1)) ret =3D -ENOMEM; else - io_map_base =3D base; + *haddr =3D io_map_base =3D base; =20 mutex_unlock(&kvm_hyp_pgd_mutex); =20 + return ret; +} + +static int __create_hyp_private_mapping(phys_addr_t phys_addr, size_t size, + unsigned long *haddr, + enum kvm_pgtable_prot prot) +{ + unsigned long addr; + int ret =3D 0; + + if (!kvm_host_owns_hyp_mappings()) { + addr =3D kvm_call_hyp_nvhe(__pkvm_create_private_mapping, + phys_addr, size, prot); + if (IS_ERR_VALUE(addr)) + return addr; + *haddr =3D addr; + + return 0; + } + + size =3D PAGE_ALIGN(size + offset_in_page(phys_addr)); + ret =3D hyp_alloc_private_va_range(size, &addr); if (ret) - goto out; + return ret; =20 - ret =3D __create_hyp_mappings(base, size, phys_addr, prot); + ret =3D __create_hyp_mappings(addr, size, phys_addr, prot); if (ret) - goto out; + return ret; =20 - *haddr =3D base + offset_in_page(phys_addr); -out: + *haddr =3D addr + offset_in_page(phys_addr); return ret; } =20 --=20 2.36.0.rc0.470.gd361397f0d-goog From nobody Mon May 11 00:10:12 2026 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 6D4C6C433FE for ; Wed, 20 Apr 2022 21:44:37 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1382641AbiDTVrW (ORCPT ); Wed, 20 Apr 2022 17:47:22 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33486 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S241443AbiDTVrT (ORCPT ); Wed, 20 Apr 2022 17:47:19 -0400 Received: from mail-yw1-x1149.google.com (mail-yw1-x1149.google.com [IPv6:2607:f8b0:4864:20::1149]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0F0B447ACD for ; Wed, 20 Apr 2022 14:44:32 -0700 (PDT) Received: by mail-yw1-x1149.google.com with SMTP id 00721157ae682-2eb2bc9018aso26937487b3.18 for ; Wed, 20 Apr 2022 14:44:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=date:in-reply-to:message-id:mime-version:references:subject:from:cc; bh=BogsBXABktFG0DHwrUtlQektCpF/08d5mM7wBisYbxw=; b=VWhDwyuJHJN0/FjtTViruyvGpY/7A0N/EpUNuOCFIlLGIggSNj8zqNGljB1QWydW9Z 74dPKsjOl6IJ863G/qtse8k4sCCJHI/+dy3Sa92WMmPhretB7CoU9Ovg+ZtEw8MuM4W8 fxg6nZC5P1l/vQYKF07jKASSb7YeD+AXXkT1iZaxi5mMSOZIx+36Sc2A9qtVWZY2uvfG LXVU2v06nDrw6kUizukdhKuf9vpgRqrL7ukjvaiU+TyJmPKZPnTMLr3G2sb54H/KIWeB v0CYkVOp2pSLrByfdd4MaJjVQ3ND+XiKtUqrRGEGXkjDWzC9GGwMQDEFgl/pJWFYQy+y iUlQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:cc; bh=BogsBXABktFG0DHwrUtlQektCpF/08d5mM7wBisYbxw=; b=elaEtQlJMw4l1jw+V/SqM12P8EgF4kNjL8noA7oVtLNxbYDt2GpSMJMFaIB7Zn2F8T /7saWMm6/Wiw3EoABr6x/ZPERumhfueeufrvSyCupeetpwdN47DX5/jQvDQGu/cnkLEV khcnaXrImn8ihLKFJJB63doFFiJaYFZG4tESlF+KUoVtPvhcyixj2zEKgBDx/vdas6UH b9Aqaje1Ld29cvSZMtfiVLKtDGJ84bZ3LYKykcWFDElWG7wV55mNSdYESs/EsGOjw688 GW4WavA+IT+jhOSFSErqsG0IC921Ct3qfYelzPXZq0VP2kxk1E9N5jq65/ScCy4yy5cV d3HQ== X-Gm-Message-State: AOAM532qLe963PHtIp5dwmB6olWMnHK5Uo0ZVGkJUcjbILkDsiGiSnO6 zA++O14T17hnrpVB5nCoS3a7L22C7B9YU8cIbA== X-Google-Smtp-Source: ABdhPJyof3Jv6llhTaTubLM1S8UoZuK++2AYOSCa5aeHhAWWq/dWly3CQhpjzyj/QsdPwQ/9jEoKCbzm+Ua/ft55wg== X-Received: from kaleshsingh.mtv.corp.google.com ([2620:15c:211:200:2a20:cec4:8f41:cd6f]) (user=kaleshsingh job=sendgmr) by 2002:a05:6902:70d:b0:644:b5e4:aa25 with SMTP id k13-20020a056902070d00b00644b5e4aa25mr22610620ybt.61.1650491071245; Wed, 20 Apr 2022 14:44:31 -0700 (PDT) Date: Wed, 20 Apr 2022 14:42:53 -0700 In-Reply-To: <20220420214317.3303360-1-kaleshsingh@google.com> Message-Id: <20220420214317.3303360-3-kaleshsingh@google.com> Mime-Version: 1.0 References: <20220420214317.3303360-1-kaleshsingh@google.com> X-Mailer: git-send-email 2.36.0.rc0.470.gd361397f0d-goog Subject: [PATCH v8 2/6] KVM: arm64: Introduce pkvm_alloc_private_va_range() From: Kalesh Singh Cc: will@kernel.org, maz@kernel.org, qperret@google.com, tabba@google.com, surenb@google.com, kernel-team@android.com, Kalesh Singh , James Morse , Alexandru Elisei , Suzuki K Poulose , Catalin Marinas , Andrew Walbran , Mark Rutland , Nick Desaulniers , Masahiro Yamada , Changbin Du , Nathan Chancellor , linux-arm-kernel@lists.infradead.org, kvmarm@lists.cs.columbia.edu, linux-kernel@vger.kernel.org To: unlisted-recipients:; (no To-header on input) Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" pkvm_hyp_alloc_private_va_range() can be used to reserve private VA ranges in the pKVM nVHE hypervisor. Allocations are aligned based on the order of the requested size. This will be used to implement stack guard pages for pKVM nVHE hypervisor (in a subsequent patch in the series). Credits to Quentin Perret for the idea of moving private VA allocation out of __pkvm_create_private_mapping() Signed-off-by: Kalesh Singh Tested-by: Fuad Tabba Reviewed-by: Fuad Tabba --- Changes in v8: - PAGE_ALIGN the size in __pkvm_create_private_mapping(), per Marc Changes in v7: - Add Fuad's Reviewed-by and Tested-by tags. Changes in v6: - Update kernel-doc for pkvm_alloc_private_va_range() and add return description, per Stephen - Update pkvm_alloc_private_va_range() to return an int error code, per Stephen - Update __pkvm_create_private_mapping to return an in error code, per Quentin - Update callers of __pkvm_create_private_mapping() to handle new return value and params. Changes in v5: - Align private allocations based on the order of their size, per Marc Changes in v4: - Handle null ptr in pkvm_alloc_private_va_range() and replace IS_ERR_OR_NULL checks in callers with IS_ERR checks, per Fuad - Fix kernel-doc comments format, per Fuad - Format __pkvm_create_private_mapping() prototype args (< 80 col), per F= uad Changes in v3: - Handle null ptr in IS_ERR_OR_NULL checks, per Mark Changes in v2: - Allow specifying an alignment for the private VA allocations, per Marc arch/arm64/kvm/hyp/include/nvhe/mm.h | 6 ++- arch/arm64/kvm/hyp/nvhe/hyp-main.c | 18 ++++++- arch/arm64/kvm/hyp/nvhe/mm.c | 78 ++++++++++++++++++---------- 3 files changed, 72 insertions(+), 30 deletions(-) diff --git a/arch/arm64/kvm/hyp/include/nvhe/mm.h b/arch/arm64/kvm/hyp/incl= ude/nvhe/mm.h index 2d08510c6cc1..42d8eb9bfe72 100644 --- a/arch/arm64/kvm/hyp/include/nvhe/mm.h +++ b/arch/arm64/kvm/hyp/include/nvhe/mm.h @@ -19,8 +19,10 @@ int hyp_back_vmemmap(phys_addr_t phys, unsigned long siz= e, phys_addr_t back); int pkvm_cpu_set_vector(enum arm64_hyp_spectre_vector slot); int pkvm_create_mappings(void *from, void *to, enum kvm_pgtable_prot prot); int pkvm_create_mappings_locked(void *from, void *to, enum kvm_pgtable_pro= t prot); -unsigned long __pkvm_create_private_mapping(phys_addr_t phys, size_t size, - enum kvm_pgtable_prot prot); +int __pkvm_create_private_mapping(phys_addr_t phys, size_t size, + enum kvm_pgtable_prot prot, + unsigned long *haddr); +int pkvm_alloc_private_va_range(size_t size, unsigned long *haddr); =20 static inline void hyp_vmemmap_range(phys_addr_t phys, unsigned long size, unsigned long *start, unsigned long *end) diff --git a/arch/arm64/kvm/hyp/nvhe/hyp-main.c b/arch/arm64/kvm/hyp/nvhe/h= yp-main.c index 5e2197db0d32..3cea4b6ac23e 100644 --- a/arch/arm64/kvm/hyp/nvhe/hyp-main.c +++ b/arch/arm64/kvm/hyp/nvhe/hyp-main.c @@ -160,7 +160,23 @@ static void handle___pkvm_create_private_mapping(struc= t kvm_cpu_context *host_ct DECLARE_REG(size_t, size, host_ctxt, 2); DECLARE_REG(enum kvm_pgtable_prot, prot, host_ctxt, 3); =20 - cpu_reg(host_ctxt, 1) =3D __pkvm_create_private_mapping(phys, size, prot); + /* + * __pkvm_create_private_mapping() populates a pointer with the + * hypervisor start address of the allocation. + * + * However, handle___pkvm_create_private_mapping() hypercall crosses the + * EL1/EL2 boundary so the pointer would not be valid in this context. + * + * Instead pass the allocation address as the return value (or return + * ERR_PTR() on failure). + */ + unsigned long haddr; + int err =3D __pkvm_create_private_mapping(phys, size, prot, &haddr); + + if (err) + haddr =3D (unsigned long)ERR_PTR(err); + + cpu_reg(host_ctxt, 1) =3D haddr; } =20 static void handle___pkvm_prot_finalize(struct kvm_cpu_context *host_ctxt) diff --git a/arch/arm64/kvm/hyp/nvhe/mm.c b/arch/arm64/kvm/hyp/nvhe/mm.c index cdbe8e246418..96193cb31a39 100644 --- a/arch/arm64/kvm/hyp/nvhe/mm.c +++ b/arch/arm64/kvm/hyp/nvhe/mm.c @@ -37,36 +37,60 @@ static int __pkvm_create_mappings(unsigned long start, = unsigned long size, return err; } =20 -unsigned long __pkvm_create_private_mapping(phys_addr_t phys, size_t size, - enum kvm_pgtable_prot prot) +/** + * pkvm_alloc_private_va_range - Allocates a private VA range. + * @size: The size of the VA range to reserve. + * @haddr: The hypervisor virtual start address of the allocation. + * + * The private virtual address (VA) range is allocated above __io_map_base + * and aligned based on the order of @size. + * + * Return: 0 on success or negative error code on failure. + */ +int pkvm_alloc_private_va_range(size_t size, unsigned long *haddr) { - unsigned long addr; - int err; + unsigned long base, addr; + int ret =3D 0; =20 hyp_spin_lock(&pkvm_pgd_lock); =20 - size =3D PAGE_ALIGN(size + offset_in_page(phys)); - addr =3D __io_map_base; - __io_map_base +=3D size; + /* Align the allocation based on the order of its size */ + addr =3D ALIGN(__io_map_base, PAGE_SIZE << get_order(size)); =20 - /* Are we overflowing on the vmemmap ? */ - if (__io_map_base > __hyp_vmemmap) { - __io_map_base -=3D size; - addr =3D (unsigned long)ERR_PTR(-ENOMEM); - goto out; - } + /* The allocated size is always a multiple of PAGE_SIZE */ + base =3D addr + PAGE_ALIGN(size); =20 - err =3D kvm_pgtable_hyp_map(&pkvm_pgtable, addr, size, phys, prot); - if (err) { - addr =3D (unsigned long)ERR_PTR(err); - goto out; + /* Are we overflowing on the vmemmap ? */ + if (!addr || base > __hyp_vmemmap) + ret =3D -ENOMEM; + else { + __io_map_base =3D base; + *haddr =3D addr; } =20 - addr =3D addr + offset_in_page(phys); -out: hyp_spin_unlock(&pkvm_pgd_lock); =20 - return addr; + return ret; +} + +int __pkvm_create_private_mapping(phys_addr_t phys, size_t size, + enum kvm_pgtable_prot prot, + unsigned long *haddr) +{ + unsigned long addr; + int err; + + size =3D PAGE_ALIGN(size + offset_in_page(phys)); + err =3D pkvm_alloc_private_va_range(size, &addr); + if (err) + return err; + + err =3D __pkvm_create_mappings(addr, size, phys, prot); + if (err) + return err; + + *haddr =3D addr + offset_in_page(phys); + return err; } =20 int pkvm_create_mappings_locked(void *from, void *to, enum kvm_pgtable_pro= t prot) @@ -146,7 +170,8 @@ int pkvm_cpu_set_vector(enum arm64_hyp_spectre_vector s= lot) int hyp_map_vectors(void) { phys_addr_t phys; - void *bp_base; + unsigned long bp_base; + int ret; =20 if (!kvm_system_needs_idmapped_vectors()) { __hyp_bp_vect_base =3D __bp_harden_hyp_vecs; @@ -154,13 +179,12 @@ int hyp_map_vectors(void) } =20 phys =3D __hyp_pa(__bp_harden_hyp_vecs); - bp_base =3D (void *)__pkvm_create_private_mapping(phys, - __BP_HARDEN_HYP_VECS_SZ, - PAGE_HYP_EXEC); - if (IS_ERR_OR_NULL(bp_base)) - return PTR_ERR(bp_base); + ret =3D __pkvm_create_private_mapping(phys, __BP_HARDEN_HYP_VECS_SZ, + PAGE_HYP_EXEC, &bp_base); + if (ret) + return ret; =20 - __hyp_bp_vect_base =3D bp_base; + __hyp_bp_vect_base =3D (void *)bp_base; =20 return 0; } --=20 2.36.0.rc0.470.gd361397f0d-goog From nobody Mon May 11 00:10:12 2026 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 C971AC433F5 for ; Wed, 20 Apr 2022 21:45:22 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1382645AbiDTVsH (ORCPT ); Wed, 20 Apr 2022 17:48:07 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34854 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238221AbiDTVsC (ORCPT ); Wed, 20 Apr 2022 17:48:02 -0400 Received: from mail-yb1-xb4a.google.com (mail-yb1-xb4a.google.com [IPv6:2607:f8b0:4864:20::b4a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9065B24F0C for ; Wed, 20 Apr 2022 14:45:15 -0700 (PDT) Received: by mail-yb1-xb4a.google.com with SMTP id b12-20020a056902030c00b0061d720e274aso2642684ybs.20 for ; Wed, 20 Apr 2022 14:45:15 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=date:in-reply-to:message-id:mime-version:references:subject:from:cc; bh=l+WC7bxPewzszL+k3JPl9SfRKuFFZeheKk4Z2YSYX0c=; b=kH6X+50CXU0B9J23OVjOUG6h5g3c6tr6fH8cfI3rJDJxnXKFPYs2g5h4P6do/Fz+J+ lFRfXCK96H3wdWyvB5ffVImXGDX+LYiIdPgRyvs1oggf1lBSu868EewZRXzK+5IjpvKu hxDQ5CRzyjjeNUZl7gZ2pkqXjk9my9hmFlBdCsUGlE+5nPduCtPa36HksUSIE6zzqdjR O5tYeuutq8xFL6Ruhcx56+rCZ3T8UwPLlVQNjC07B8+bqxlTZmgsVnZG989bWAkMua1S XWVJWefsf6Z3UjwM54OZKG+wYb7JDMvJk+DPq0QvDFr2oQy53fDzxqJzalqXRqVMu07t bBpA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:cc; bh=l+WC7bxPewzszL+k3JPl9SfRKuFFZeheKk4Z2YSYX0c=; b=b3VPP0/WSHzctTEY+OMXMoONiOi8kX7VtG7dhi5HIRAZVapEhfoJPDrVtdET0gPHK7 ViiZX2ngP0cYAK8N8nVZYp+swNUwoZlV8V8Rk/RXEoNjVtFxDCwA7ZpqSvbQm5WSY+Wm OvOvv7nlbGYQsIQPorY//hFeC76sF7iKWetOWNidl1W/0AycBhVqSzK6/qcM6LkbHabn KtIMWqwspJEZOItZABGsIaFRCPw3xQ+sbNeHUSPDs/r+pQrJSVMk061jA7Lh2f4jZk7H paTBp/PppXA06ehZodkPst0gY7+4Bw73OiAhC/I/THG5WH9V5CAGatSASqIJC6Ds4C9F Wr+w== X-Gm-Message-State: AOAM532GhHf5X0hXhm2GFvmzPQl4d0KEvYA4AfDEspp2yGVOEl0RPF9m l4ngk9Sx0jlCRfzIaVDHI6M8r4EfqeTwg6ld3Q== X-Google-Smtp-Source: ABdhPJwiYgead6QiW2HPaklZtWwWhmlUfQNFZc361RnnmGoFxH14G07w0bYAlUJmXEesM+yXWl6qV1WNWx8SNYvlfw== X-Received: from kaleshsingh.mtv.corp.google.com ([2620:15c:211:200:2a20:cec4:8f41:cd6f]) (user=kaleshsingh job=sendgmr) by 2002:a25:40b:0:b0:645:42f6:ad57 with SMTP id 11-20020a25040b000000b0064542f6ad57mr8816256ybe.649.1650491114835; Wed, 20 Apr 2022 14:45:14 -0700 (PDT) Date: Wed, 20 Apr 2022 14:42:54 -0700 In-Reply-To: <20220420214317.3303360-1-kaleshsingh@google.com> Message-Id: <20220420214317.3303360-4-kaleshsingh@google.com> Mime-Version: 1.0 References: <20220420214317.3303360-1-kaleshsingh@google.com> X-Mailer: git-send-email 2.36.0.rc0.470.gd361397f0d-goog Subject: [PATCH v8 3/6] KVM: arm64: Add guard pages for KVM nVHE hypervisor stack From: Kalesh Singh Cc: will@kernel.org, maz@kernel.org, qperret@google.com, tabba@google.com, surenb@google.com, kernel-team@android.com, Kalesh Singh , James Morse , Alexandru Elisei , Suzuki K Poulose , Catalin Marinas , Andrew Walbran , Mark Rutland , Zenghui Yu , Nick Desaulniers , Nathan Chancellor , Changbin Du , linux-arm-kernel@lists.infradead.org, kvmarm@lists.cs.columbia.edu, linux-kernel@vger.kernel.org To: unlisted-recipients:; (no To-header on input) Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" 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 v8: - Don't expose hyp_pgtable and kvm_hyp_pgd_mutex. Instead use __create_hyp_mappings() helper, per Marc 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 | 2 ++ arch/arm64/kvm/arm.c | 37 +++++++++++++++++++++++++++++--- arch/arm64/kvm/mmu.c | 4 ++-- 4 files changed, 39 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..b208da3bebec 100644 --- a/arch/arm64/include/asm/kvm_mmu.h +++ b/arch/arm64/include/asm/kvm_mmu.h @@ -154,6 +154,8 @@ static __always_inline unsigned long __kern_hyp_va(unsi= gned long v) int kvm_share_hyp(void *from, void *to); void kvm_unshare_hyp(void *from, void *to); int create_hyp_mappings(void *from, void *to, enum kvm_pgtable_prot prot); +int __create_hyp_mappings(unsigned long start, unsigned long size, + unsigned long phys, enum kvm_pgtable_prot prot); int hyp_alloc_private_va_range(size_t size, unsigned long *haddr); int create_hyp_io_mappings(phys_addr_t phys_addr, size_t size, void __iomem **kaddr, diff --git a/arch/arm64/kvm/arm.c b/arch/arm64/kvm/arm.c index 523bc934fe2f..dd257d9f21a2 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 |=3D (idmap_t0sz & GENMASK(TCR_TxSZ_WIDTH - 1, 0)) << TCR_T0SZ_OFFSET; params->tcr_el2 =3D tcr; =20 - params->stack_hyp_va =3D kern_hyp_va(per_cpu(kvm_arm_hyp_stack_page, cpu)= + PAGE_SIZE); params->pgd_pa =3D kvm_mmu_get_httbr(); if (is_protected_kvm_enabled()) params->hcr_el2 =3D HCR_HOST_NVHE_PROTECTED_FLAGS; @@ -1933,14 +1932,46 @@ static int init_hyp_mode(void) * Map the Hyp stack pages */ for_each_possible_cpu(cpu) { + struct kvm_nvhe_init_params *params =3D per_cpu_ptr_nvhe_sym(kvm_init_pa= rams, cpu); char *stack_page =3D (char *)per_cpu(kvm_arm_hyp_stack_page, cpu); - err =3D create_hyp_mappings(stack_page, stack_page + PAGE_SIZE, - PAGE_HYP); + unsigned long hyp_addr; =20 + /* + * 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 =3D 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. + */ + err =3D __create_hyp_mappings(hyp_addr + PAGE_SIZE, PAGE_SIZE, + __pa(stack_page), PAGE_HYP); 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 =3D __pa(stack_page); + + params->stack_hyp_va =3D hyp_addr + (2 * PAGE_SIZE); } =20 for_each_possible_cpu(cpu) { diff --git a/arch/arm64/kvm/mmu.c b/arch/arm64/kvm/mmu.c index 7de1e02ebfd1..088e14eae4cf 100644 --- a/arch/arm64/kvm/mmu.c +++ b/arch/arm64/kvm/mmu.c @@ -258,8 +258,8 @@ static bool kvm_host_owns_hyp_mappings(void) return true; } =20 -static int __create_hyp_mappings(unsigned long start, unsigned long size, - unsigned long phys, enum kvm_pgtable_prot prot) +int __create_hyp_mappings(unsigned long start, unsigned long size, + unsigned long phys, enum kvm_pgtable_prot prot) { int err; =20 --=20 2.36.0.rc0.470.gd361397f0d-goog From nobody Mon May 11 00:10:12 2026 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 2575BC433FE for ; Wed, 20 Apr 2022 21:46:04 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1382654AbiDTVst (ORCPT ); Wed, 20 Apr 2022 17:48:49 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35128 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1382646AbiDTVsq (ORCPT ); Wed, 20 Apr 2022 17:48:46 -0400 Received: from mail-yb1-xb49.google.com (mail-yb1-xb49.google.com [IPv6:2607:f8b0:4864:20::b49]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1AEDC2A73D for ; Wed, 20 Apr 2022 14:45:59 -0700 (PDT) Received: by mail-yb1-xb49.google.com with SMTP id i5-20020a258b05000000b006347131d40bso2642424ybl.17 for ; Wed, 20 Apr 2022 14:45:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=date:in-reply-to:message-id:mime-version:references:subject:from:cc; bh=lwg1kG8+1/wNbjGSZ2PbJ4ea1sTg4029wAFJD+g5Qi4=; b=MMPu74D6gv8xbJTQoLjt4UXwW+FjK1985Y1rOEoSiYg5gcI/zj6BJ7wbII6XHlcPvT 59BtqlU6lweX8m7XAawj/sGBvGhUoQEMvEoH66ZzG/M6VpOnudkU8Y4wq1Zazhe8YFeG VBU9mzPnHlb1w/OgY5JgQnc6KWd3K7yWmcD2S+RRlARxRVWS1VObFH9mAk/7S3g+3Qsz YXIIEl1zXt4sfgfpG3aW5xYWQK63b95pfOWsVjGxqQTDcSjvBn9I7rs+x5x2mcwC3zXD Pb2zPc5J59jODCdsDLIkvrDpuJGvOo368pyQKea6bgOY8ieTr7QPh3zxQ3jmVVvSVsku YaLw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:cc; bh=lwg1kG8+1/wNbjGSZ2PbJ4ea1sTg4029wAFJD+g5Qi4=; b=f7+PmG/YgK8vNWW82UPMx4uMZKOwXqaWoiwAhY9AJ1HwGwp5xJYb7gpsBuv4rQW4lC iitVdMKN+Wbo25kE1WQ8/GUA50+PF6M/Dz+0KOyp7pcY+Z1rZVetnIYYDiAminzPJ+yn PGgIw+wCo3KRgUkgo1pnGz5WWFRz4BCNF7ucjUrzPVGw/4bzZm2q90m33quNimYApu2q HNc9Ua8P205+26fi8noJhRBpH5W/daXaEh10rormsbYIFbsLRUecf0mEyEpkqXxdhA/R uCyv9hk6nPZ4aQdST9CLcbQF9pOmuqUU/SdQhhfYXuOxGR9OlpbPzA/UTTKq40m/RifT uxwg== X-Gm-Message-State: AOAM532ZANT0zIht6PcA0IgCEC4Bi9cE7uRNf/qoHiqqMBGKjllcmwoV Ofpkc+TNLBUfuCFczmd1Nr9VOg2Uce0UQLApLA== X-Google-Smtp-Source: ABdhPJwPvA9EBgeORhcNpGmqtT4iVpoccjQSHhBaKdbuYg64D0FqAMR8uclEjMkHcXhbagt529YZzXT1QLlZO+fSMw== X-Received: from kaleshsingh.mtv.corp.google.com ([2620:15c:211:200:2a20:cec4:8f41:cd6f]) (user=kaleshsingh job=sendgmr) by 2002:a81:ff06:0:b0:2e6:d7bc:c812 with SMTP id k6-20020a81ff06000000b002e6d7bcc812mr22513276ywn.122.1650491158313; Wed, 20 Apr 2022 14:45:58 -0700 (PDT) Date: Wed, 20 Apr 2022 14:42:55 -0700 In-Reply-To: <20220420214317.3303360-1-kaleshsingh@google.com> Message-Id: <20220420214317.3303360-5-kaleshsingh@google.com> Mime-Version: 1.0 References: <20220420214317.3303360-1-kaleshsingh@google.com> X-Mailer: git-send-email 2.36.0.rc0.470.gd361397f0d-goog Subject: [PATCH v8 4/6] KVM: arm64: Add guard pages for pKVM (protected nVHE) hypervisor stack From: Kalesh Singh Cc: will@kernel.org, maz@kernel.org, qperret@google.com, tabba@google.com, surenb@google.com, kernel-team@android.com, Kalesh Singh , James Morse , Alexandru Elisei , Suzuki K Poulose , Catalin Marinas , Andrew Walbran , Mark Rutland , Andrew Jones , Ard Biesheuvel , Nick Desaulniers , Masahiro Yamada , Nathan Chancellor , Changbin Du , linux-arm-kernel@lists.infradead.org, kvmarm@lists.cs.columbia.edu, linux-kernel@vger.kernel.org To: unlisted-recipients:; (no To-header on input) Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" 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 pkvm_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 pkvm_alloc_private_va_range() returns an error for null pointer, per Fuad Changes in v3: - Handle null ptr in IS_ERR_OR_NULL checks, per Mark arch/arm64/kvm/hyp/nvhe/setup.c | 31 ++++++++++++++++++++++++++++--- 1 file changed, 28 insertions(+), 3 deletions(-) diff --git a/arch/arm64/kvm/hyp/nvhe/setup.c b/arch/arm64/kvm/hyp/nvhe/setu= p.c index 27af337f9fea..e8d4ea2fcfa0 100644 --- a/arch/arm64/kvm/hyp/nvhe/setup.c +++ b/arch/arm64/kvm/hyp/nvhe/setup.c @@ -99,17 +99,42 @@ static int recreate_hyp_mappings(phys_addr_t phys, unsi= gned long size, return ret; =20 for (i =3D 0; i < hyp_nr_cpus; i++) { + struct kvm_nvhe_init_params *params =3D per_cpu_ptr(&kvm_init_params, i); + unsigned long hyp_addr; + start =3D (void *)kern_hyp_va(per_cpu_base[i]); end =3D start + PAGE_ALIGN(hyp_percpu_size); ret =3D pkvm_create_mappings(start, end, PAGE_HYP); if (ret) return ret; =20 - end =3D (void *)per_cpu_ptr(&kvm_init_params, i)->stack_hyp_va; - start =3D end - PAGE_SIZE; - ret =3D pkvm_create_mappings(start, end, PAGE_HYP); + /* + * 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. + */ + ret =3D pkvm_alloc_private_va_range(PAGE_SIZE * 2, &hyp_addr); + if (ret) + return ret; + + /* + * 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. + */ + hyp_spin_lock(&pkvm_pgd_lock); + ret =3D kvm_pgtable_hyp_map(&pkvm_pgtable, hyp_addr + PAGE_SIZE, + PAGE_SIZE, params->stack_pa, PAGE_HYP); + hyp_spin_unlock(&pkvm_pgd_lock); if (ret) return ret; + + /* Update stack_hyp_va to end of the stack's private VA range */ + params->stack_hyp_va =3D hyp_addr + (2 * PAGE_SIZE); } =20 /* --=20 2.36.0.rc0.470.gd361397f0d-goog From nobody Mon May 11 00:10:12 2026 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 E09E7C433EF for ; Wed, 20 Apr 2022 21:46:28 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1382658AbiDTVtO (ORCPT ); Wed, 20 Apr 2022 17:49:14 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35324 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234964AbiDTVtM (ORCPT ); Wed, 20 Apr 2022 17:49:12 -0400 Received: from mail-yb1-xb49.google.com (mail-yb1-xb49.google.com [IPv6:2607:f8b0:4864:20::b49]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C188E2E085 for ; Wed, 20 Apr 2022 14:46:25 -0700 (PDT) Received: by mail-yb1-xb49.google.com with SMTP id o188-20020a2541c5000000b0064334935847so2636300yba.16 for ; Wed, 20 Apr 2022 14:46:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=date:in-reply-to:message-id:mime-version:references:subject:from:cc; bh=GgvZp34BK6H0FUXR9EOtBK1V9jyG5177uTq39S22sfQ=; b=YLibaJIUGmaDvUVjiLNeo/1HlTfI3zS6LHvsGnOZ7nQlc63ry+7w9kNdZS98Nsg0dI UcuRAZtkNAF9gMWQ+7tEnCk9peOHo4HZMgSZOrFmUISzJr6HtkRl9Hc5VqsyLk/Gzkqg GaZHzBEkf/e0IzSd1EIp3E3UsHgYiiDnynGn0Vqr4Hzuz9/delgTV+agJj7ICCbs6/Dc LDf+9rDXMww0jVi9aLG5NMpwWfYcVTW6WVT8VQBUoyRDgiHtiqLO3fv6vWCtCxFJAyV1 xH57L111n1Gktk4VQNMV+qWaWEZaWoDAMmh5EdnCzNbAP96SgdXMwIvjMO87ui+g4cpy TuJg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:cc; bh=GgvZp34BK6H0FUXR9EOtBK1V9jyG5177uTq39S22sfQ=; b=mJWXIHXl+BshpSkKKAE3044OXUittZcY6i0Wxo3JJ4mcVXLuZ/Yrpjn4Hz8cEKHhwq 3HXApJOyPmaEfci59UlV8q3Z6qE/27Vj4oFJjwtMC72NDlR30o/OTEsqdjr/v7LbaC+a ZlxssfqRmEWVmww9A5VlE82xcpgx5vNpD3lGUsNl9JTaiUt7IUlR7DVteGGBx/9uNtD/ CYz1J8OyRda9PVq4ZnbRdgZnzKri2q2+68aAw6ZBfsNg5Zwv4SjdmlwFzujFedZ1Ji8L dnaReIt3n08CaXSaQkBymx22Ck1I5j73eNXI73oioWACekqCnk91w1RcOGfjMjdVkbGt kIlA== X-Gm-Message-State: AOAM531ZRrr5rgOt2SDBDBimmZTrcHW+v6esHe0wPo9GDMnt14Mgn1el cX1Y3HfCpu2vIDQnPkfWlIA9W8k3FbiyLn8iBA== X-Google-Smtp-Source: ABdhPJyppryoKr9oaIT1p1DVqRzVQjMH2bfnVLMwG49QsDwJMJtZjut11ZY+ct0kOHIySH/Adr7CF8CWrpG88gIeBg== X-Received: from kaleshsingh.mtv.corp.google.com ([2620:15c:211:200:2a20:cec4:8f41:cd6f]) (user=kaleshsingh job=sendgmr) by 2002:a05:6902:114c:b0:644:cada:4ee7 with SMTP id p12-20020a056902114c00b00644cada4ee7mr20521440ybu.217.1650491184989; Wed, 20 Apr 2022 14:46:24 -0700 (PDT) Date: Wed, 20 Apr 2022 14:42:56 -0700 In-Reply-To: <20220420214317.3303360-1-kaleshsingh@google.com> Message-Id: <20220420214317.3303360-6-kaleshsingh@google.com> Mime-Version: 1.0 References: <20220420214317.3303360-1-kaleshsingh@google.com> X-Mailer: git-send-email 2.36.0.rc0.470.gd361397f0d-goog Subject: [PATCH v8 5/6] KVM: arm64: Detect and handle hypervisor stack overflows From: Kalesh Singh Cc: will@kernel.org, maz@kernel.org, qperret@google.com, tabba@google.com, surenb@google.com, kernel-team@android.com, Kalesh Singh , James Morse , Alexandru Elisei , Suzuki K Poulose , Catalin Marinas , Andrew Walbran , Mark Rutland , Andrew Jones , Nathan Chancellor , Masahiro Yamada , Changbin Du , Nick Desaulniers , linux-arm-kernel@lists.infradead.org, kvmarm@lists.cs.columbia.edu, linux-kernel@vger.kernel.org To: unlisted-recipients:; (no To-header on input) Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" The hypervisor stacks (for both nVHE Hyp mode and nVHE protected mode) are aligned such that any valid stack address has PAGE_SHIFT bit as 1. This allows us to conveniently check for overflow in the exception entry without corrupting any GPRs. We won't recover from a stack overflow so panic the hypervisor. Signed-off-by: Kalesh Singh Tested-by: Fuad Tabba Reviewed-by: Fuad Tabba --- Changes in v8: - branch to hyp_panic_bad_stack(), instead of branch and link, per Marc Changes in v7: - Add Fuad's Reviewed-by and Tested-by tags. Changes in v5: - Valid stack addresses now have PAGE_SHIFT bit as 1 instead of 0 Changes in v3: - Remove test_sp_overflow macro, per Mark - Add asmlinkage attribute for hyp_panic, hyp_panic_bad_stack, per Ard arch/arm64/kvm/hyp/nvhe/host.S | 24 ++++++++++++++++++++++++ arch/arm64/kvm/hyp/nvhe/switch.c | 7 ++++++- 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/arch/arm64/kvm/hyp/nvhe/host.S b/arch/arm64/kvm/hyp/nvhe/host.S index 3d613e721a75..09b5254fb497 100644 --- a/arch/arm64/kvm/hyp/nvhe/host.S +++ b/arch/arm64/kvm/hyp/nvhe/host.S @@ -153,6 +153,18 @@ SYM_FUNC_END(__host_hvc) =20 .macro invalid_host_el2_vect .align 7 + + /* + * Test whether the SP has overflowed, without corrupting a GPR. + * nVHE hypervisor stacks are aligned so that the PAGE_SHIFT bit + * of SP should always be 1. + */ + add sp, sp, x0 // sp' =3D sp + x0 + sub x0, sp, x0 // x0' =3D sp' - x0 =3D (sp + x0) - x0 =3D sp + tbz x0, #PAGE_SHIFT, .L__hyp_sp_overflow\@ + sub x0, sp, x0 // x0'' =3D sp' - x0' =3D (sp + x0) - sp =3D x0 + sub sp, sp, x0 // sp'' =3D sp' - x0 =3D (sp + x0) - x0 =3D sp + /* If a guest is loaded, panic out of it. */ stp x0, x1, [sp, #-16]! get_loaded_vcpu x0, x1 @@ -165,6 +177,18 @@ SYM_FUNC_END(__host_hvc) * been partially clobbered by __host_enter. */ b hyp_panic + +.L__hyp_sp_overflow\@: + /* + * Reset SP to the top of the stack, to allow handling the hyp_panic. + * This corrupts the stack but is ok, since we won't be attempting + * any unwinding here. + */ + ldr_this_cpu x0, kvm_init_params + NVHE_INIT_STACK_HYP_VA, x1 + mov sp, x0 + + b hyp_panic_bad_stack + ASM_BUG() .endm =20 .macro invalid_host_el1_vect diff --git a/arch/arm64/kvm/hyp/nvhe/switch.c b/arch/arm64/kvm/hyp/nvhe/swi= tch.c index 6410d21d8695..703a5d3f611b 100644 --- a/arch/arm64/kvm/hyp/nvhe/switch.c +++ b/arch/arm64/kvm/hyp/nvhe/switch.c @@ -347,7 +347,7 @@ int __kvm_vcpu_run(struct kvm_vcpu *vcpu) return exit_code; } =20 -void __noreturn hyp_panic(void) +asmlinkage void __noreturn hyp_panic(void) { u64 spsr =3D read_sysreg_el2(SYS_SPSR); u64 elr =3D read_sysreg_el2(SYS_ELR); @@ -369,6 +369,11 @@ void __noreturn hyp_panic(void) unreachable(); } =20 +asmlinkage void __noreturn hyp_panic_bad_stack(void) +{ + hyp_panic(); +} + asmlinkage void kvm_unexpected_el2_exception(void) { return __kvm_unexpected_el2_exception(); --=20 2.36.0.rc0.470.gd361397f0d-goog From nobody Mon May 11 00:10:12 2026 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 05ABEC433EF for ; Wed, 20 Apr 2022 21:46:55 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1382661AbiDTVtk (ORCPT ); Wed, 20 Apr 2022 17:49:40 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35488 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234964AbiDTVti (ORCPT ); Wed, 20 Apr 2022 17:49:38 -0400 Received: from mail-yw1-x114a.google.com (mail-yw1-x114a.google.com [IPv6:2607:f8b0:4864:20::114a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 14AD73E5DE for ; Wed, 20 Apr 2022 14:46:51 -0700 (PDT) Received: by mail-yw1-x114a.google.com with SMTP id 00721157ae682-2f198b4e2d1so26926907b3.14 for ; Wed, 20 Apr 2022 14:46:51 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=date:in-reply-to:message-id:mime-version:references:subject:from:cc; bh=3TRcCUUaZ+b7tjFg88UNtO89FA4GsBVVD3ULOF8xuc0=; b=fNlEbCPYFY/2sKN0B+7mRjKWh9Vmc0mQaldUs5//VcDe8AKIWT3mEXRG2KmS10vL5m 55EK4a6Y6SvHoEHe583jl1ltI7lA3lfGImFy/DQ2SKUpe8Irwm8RZBCCkLKpl4jwU2hE cQjtaRRrYRRRRGCWFwdlb4iNu1ZFs+3MDX+TOnItxhcue3RRUprY5La0mnX8uvo+dqjt 2Mxx/8gIDsYenMuLbEfx/0MI476D/uSdF3vFghFSAOT/mzBSXqlZchMjcvwiSaJPI+yl 685YmTacJ70CuTt+LNso67g/Cybklwhm0T5nrSbQ3W8Y2OVqVugGT8KvrofMeWv2RMV8 OYOA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:cc; bh=3TRcCUUaZ+b7tjFg88UNtO89FA4GsBVVD3ULOF8xuc0=; b=w1xXIjJn5MqcOln49PANzSevP4i6hoiM1hXI20FVDkFUpNPLQ70q3fo9M2o9+Hz8Yg BB45tJMCIQEDHw7lYNx8MTVHkwMmxNaLmVPhj1/GUOatqDFZ6k+lEE6gLyDOh5cQ66bL jJcwxO9ekTxvP5ADgrA9932TyeCNB/Q13Kq6H5XFczgyX9g0WsXFDbSkdNddRdWZZi1U t7Ktoie3EqfouVqyB3z4YoYDNdXo4dlxgtNO9DJqwMJheaimv5FtuH/CiRMqWh2suH9s XHO/Bcr/YejxyuJqHmV2QYEXsH83fjCZnQXJv80iJyTZdWt1OTjN7N+DgUkLkRARDfuX RPuw== X-Gm-Message-State: AOAM532y7EbpAosJ7wBbxWGWmEM9s500Yw9Su22Y6wlORYKqCsXbl+Kp mxoQQVOm6JxjOcPfrkELyOQTS236GkBta8P/gw== X-Google-Smtp-Source: ABdhPJxyWCmldtrllRKLYN9yFrNAbR/NngkeHfsRFEFtVhkoioyyWhx2dJv29w6NEgLNecP0TQ8HxC5kcoFaJH7jLQ== X-Received: from kaleshsingh.mtv.corp.google.com ([2620:15c:211:200:2a20:cec4:8f41:cd6f]) (user=kaleshsingh job=sendgmr) by 2002:a05:6902:70c:b0:645:58e:a3ac with SMTP id k12-20020a056902070c00b00645058ea3acmr17151653ybt.380.1650491210272; Wed, 20 Apr 2022 14:46:50 -0700 (PDT) Date: Wed, 20 Apr 2022 14:42:57 -0700 In-Reply-To: <20220420214317.3303360-1-kaleshsingh@google.com> Message-Id: <20220420214317.3303360-7-kaleshsingh@google.com> Mime-Version: 1.0 References: <20220420214317.3303360-1-kaleshsingh@google.com> X-Mailer: git-send-email 2.36.0.rc0.470.gd361397f0d-goog Subject: [PATCH v8 6/6] KVM: arm64: Symbolize the nVHE HYP addresses From: Kalesh Singh Cc: will@kernel.org, maz@kernel.org, qperret@google.com, tabba@google.com, surenb@google.com, kernel-team@android.com, Kalesh Singh , James Morse , Alexandru Elisei , Suzuki K Poulose , Catalin Marinas , Andrew Walbran , Mark Rutland , Andrew Jones , Ard Biesheuvel , Masahiro Yamada , Nathan Chancellor , Changbin Du , linux-arm-kernel@lists.infradead.org, kvmarm@lists.cs.columbia.edu, linux-kernel@vger.kernel.org To: unlisted-recipients:; (no To-header on input) Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Reintroduce the __kvm_nvhe_ symbols in kallsyms, ignoring the local symbols in this namespace. The local symbols are not informative and can cause aliasing issues when symbolizing the addresses. With the necessary symbols now in kallsyms we can symbolize nVHE addresses using the %p print format specifier: [ 98.916444][ T426] kvm [426]: nVHE hyp panic at: [] _= _kvm_nvhe_overflow_stack+0x8/0x34! Signed-off-by: Kalesh Singh Tested-by: Fuad Tabba Reviewed-by: Fuad Tabba --- Changes in v8: - Also ignore local symbols prefixed by '.L' in KVM nvhe namespace, per Marc Changes in v6: - Add Fuad's Reviewed-by and Tested-by tags. =20 Changes in v2: - Fix printk warnings - %p expects (void *) arch/arm64/kvm/handle_exit.c | 13 +++++-------- scripts/kallsyms.c | 3 ++- 2 files changed, 7 insertions(+), 9 deletions(-) diff --git a/arch/arm64/kvm/handle_exit.c b/arch/arm64/kvm/handle_exit.c index 97fe14aab1a3..a377b871bf58 100644 --- a/arch/arm64/kvm/handle_exit.c +++ b/arch/arm64/kvm/handle_exit.c @@ -295,13 +295,8 @@ void __noreturn __cold nvhe_hyp_panic_handler(u64 esr,= u64 spsr, u64 elr_in_kimg =3D __phys_to_kimg(elr_phys); u64 hyp_offset =3D elr_in_kimg - kaslr_offset() - elr_virt; u64 mode =3D spsr & PSR_MODE_MASK; + u64 panic_addr =3D elr_virt + hyp_offset; =20 - /* - * The nVHE hyp symbols are not included by kallsyms to avoid issues - * with aliasing. That means that the symbols cannot be printed with the - * "%pS" format specifier, so fall back to the vmlinux address if - * there's no better option. - */ if (mode !=3D PSR_MODE_EL2t && mode !=3D PSR_MODE_EL2h) { kvm_err("Invalid host exception to nVHE hyp!\n"); } else if (ESR_ELx_EC(esr) =3D=3D ESR_ELx_EC_BRK64 && @@ -321,9 +316,11 @@ void __noreturn __cold nvhe_hyp_panic_handler(u64 esr,= u64 spsr, if (file) kvm_err("nVHE hyp BUG at: %s:%u!\n", file, line); else - kvm_err("nVHE hyp BUG at: %016llx!\n", elr_virt + hyp_offset); + kvm_err("nVHE hyp BUG at: [<%016llx>] %pB!\n", panic_addr, + (void *)panic_addr); } else { - kvm_err("nVHE hyp panic at: %016llx!\n", elr_virt + hyp_offset); + kvm_err("nVHE hyp panic at: [<%016llx>] %pB!\n", panic_addr, + (void *)panic_addr); } =20 /* diff --git a/scripts/kallsyms.c b/scripts/kallsyms.c index 8caabddf817c..e6906f79833d 100644 --- a/scripts/kallsyms.c +++ b/scripts/kallsyms.c @@ -111,7 +111,8 @@ static bool is_ignored_symbol(const char *name, char ty= pe) ".L", /* local labels, .LBB,.Ltmpxxx,.L__unnamed_xx,.LASANPC, etc. */ "__crc_", /* modversions */ "__efistub_", /* arm64 EFI stub namespace */ - "__kvm_nvhe_", /* arm64 non-VHE KVM namespace */ + "__kvm_nvhe_$", /* arm64 local symbols in non-VHE KVM namespace */ + "__kvm_nvhe_.L", /* arm64 local symbols in non-VHE KVM namespace */ "__AArch64ADRPThunk_", /* arm64 lld */ "__ARMV5PILongThunk_", /* arm lld */ "__ARMV7PILongThunk_", --=20 2.36.0.rc0.470.gd361397f0d-goog