From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 457C1C433EF for ; Tue, 22 Feb 2022 18:53:26 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235090AbiBVSxu (ORCPT ); Tue, 22 Feb 2022 13:53:50 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54070 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229747AbiBVSxr (ORCPT ); Tue, 22 Feb 2022 13:53:47 -0500 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 43C0A113D9C for ; Tue, 22 Feb 2022 10:53:21 -0800 (PST) Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 0CE1F106F; Tue, 22 Feb 2022 10:53:21 -0800 (PST) Received: from lakrids (usa-sjc-imap-foss1.foss.arm.com [10.121.207.14]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 98D7E3F66F; Tue, 22 Feb 2022 10:53:18 -0800 (PST) Date: Tue, 22 Feb 2022 18:53:16 +0000 From: Mark Rutland To: Kalesh Singh Cc: will@kernel.org, maz@kernel.org, qperret@google.com, tabba@google.com, surenb@google.com, kernel-team@android.com, Catalin Marinas , James Morse , Alexandru Elisei , Suzuki K Poulose , Ard Biesheuvel , Pasha Tatashin , Joey Gouly , Peter Collingbourne , Andrew Walbran , Andrew Scull , linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, kvmarm@lists.cs.columbia.edu Subject: Re: [PATCH v2 1/9] KVM: arm64: Introduce hyp_alloc_private_va_range() Message-ID: References: <20220222165212.2005066-1-kaleshsingh@google.com> <20220222165212.2005066-2-kaleshsingh@google.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20220222165212.2005066-2-kaleshsingh@google.com> Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Tue, Feb 22, 2022 at 08:51:02AM -0800, Kalesh Singh wrote: > hyp_alloc_private_va_range() can be used to reserve private VA ranges > in the nVHE hypervisor. Also update __create_hyp_private_mapping() > to allow specifying an alignment for the private VA mapping. > > These 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 > --- > arch/arm64/include/asm/kvm_mmu.h | 4 +++ > arch/arm64/kvm/mmu.c | 61 +++++++++++++++++++++----------- > 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 81839e9a8a24..0b0c71302b92 100644 > --- a/arch/arm64/include/asm/kvm_mmu.h > +++ b/arch/arm64/include/asm/kvm_mmu.h > @@ -153,6 +153,10 @@ static __always_inline unsigned long __kern_hyp_va(unsigned 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); > +unsigned long hyp_alloc_private_va_range(size_t size, size_t align); > +int __create_hyp_private_mapping(phys_addr_t phys_addr, size_t size, > + size_t align, unsigned long *haddr, > + enum kvm_pgtable_prot prot); > 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 bc2aba953299..e5abcce44ad0 100644 > --- a/arch/arm64/kvm/mmu.c > +++ b/arch/arm64/kvm/mmu.c > @@ -457,22 +457,16 @@ int create_hyp_mappings(void *from, void *to, enum kvm_pgtable_prot prot) > return 0; > } > > -static int __create_hyp_private_mapping(phys_addr_t phys_addr, size_t size, > - unsigned long *haddr, > - enum kvm_pgtable_prot prot) > + > +/* > + * Allocates a private VA range below io_map_base. > + * > + * @size: The size of the VA range to reserve. > + * @align: The required alignment for the allocation. > + */ > +unsigned long hyp_alloc_private_va_range(size_t size, size_t align) > { > unsigned long base; > - int ret = 0; > - > - if (!kvm_host_owns_hyp_mappings()) { > - base = kvm_call_hyp_nvhe(__pkvm_create_private_mapping, > - phys_addr, size, prot); > - if (IS_ERR_OR_NULL((void *)base)) > - return PTR_ERR((void *)base); There is a latent bug here; PTR_ERR() is not valid for NULL. Today on arm64 that will happen to return 0, which may or may not be what you want, but it's a bad pattern regardless. That applies to the two copies below that this has been transformed into. Thanks, Mark > - *haddr = base; > - > - return 0; > - } > > mutex_lock(&kvm_hyp_pgd_mutex); > > @@ -484,8 +478,8 @@ static int __create_hyp_private_mapping(phys_addr_t phys_addr, size_t size, > * > * The allocated size is always a multiple of PAGE_SIZE. > */ > - size = PAGE_ALIGN(size + offset_in_page(phys_addr)); > - base = io_map_base - size; > + base = io_map_base - PAGE_ALIGN(size); > + base = ALIGN_DOWN(base, align); > > /* > * Verify that BIT(VA_BITS - 1) hasn't been flipped by > @@ -493,20 +487,45 @@ static int __create_hyp_private_mapping(phys_addr_t phys_addr, size_t size, > * overflowed the idmap/IO address range. > */ > if ((base ^ io_map_base) & BIT(VA_BITS - 1)) > - ret = -ENOMEM; > + base = (unsigned long)ERR_PTR(-ENOMEM); > else > io_map_base = base; > > mutex_unlock(&kvm_hyp_pgd_mutex); > > + return base; > +} > + > +int __create_hyp_private_mapping(phys_addr_t phys_addr, size_t size, > + size_t align, unsigned long *haddr, > + enum kvm_pgtable_prot prot) > +{ > + unsigned long addr; > + int ret = 0; > + > + if (!kvm_host_owns_hyp_mappings()) { > + addr = kvm_call_hyp_nvhe(__pkvm_create_private_mapping, > + phys_addr, size, prot); > + if (IS_ERR_OR_NULL((void *)addr)) > + return PTR_ERR((void *)addr); > + *haddr = addr; > + > + return 0; > + } > + > + size += offset_in_page(phys_addr); > + addr = hyp_alloc_private_va_range(size, align); > + if (IS_ERR_OR_NULL((void *)addr)) > + return PTR_ERR((void *)addr); > + > if (ret) > goto out; > > - ret = __create_hyp_mappings(base, size, phys_addr, prot); > + ret = __create_hyp_mappings(addr, size, phys_addr, prot); > if (ret) > goto out; > > - *haddr = base + offset_in_page(phys_addr); > + *haddr = addr + offset_in_page(phys_addr); > out: > return ret; > } > @@ -537,7 +556,7 @@ int create_hyp_io_mappings(phys_addr_t phys_addr, size_t size, > return 0; > } > > - ret = __create_hyp_private_mapping(phys_addr, size, > + ret = __create_hyp_private_mapping(phys_addr, size, PAGE_SIZE, > &addr, PAGE_HYP_DEVICE); > if (ret) { > iounmap(*kaddr); > @@ -564,7 +583,7 @@ int create_hyp_exec_mappings(phys_addr_t phys_addr, size_t size, > > BUG_ON(is_kernel_in_hyp_mode()); > > - ret = __create_hyp_private_mapping(phys_addr, size, > + ret = __create_hyp_private_mapping(phys_addr, size, PAGE_SIZE, > &addr, PAGE_HYP_EXEC); > if (ret) { > *haddr = NULL; > -- > 2.35.1.473.g83b2b277ed-goog > From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from mm01.cs.columbia.edu (mm01.cs.columbia.edu [128.59.11.253]) by smtp.lore.kernel.org (Postfix) with ESMTP id A2F8AC433FE for ; Tue, 22 Feb 2022 18:53:26 +0000 (UTC) Received: from localhost (localhost [127.0.0.1]) by mm01.cs.columbia.edu (Postfix) with ESMTP id 0581F49EE4; Tue, 22 Feb 2022 13:53:26 -0500 (EST) 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 vSIuL1dl8O1E; Tue, 22 Feb 2022 13:53:24 -0500 (EST) Received: from mm01.cs.columbia.edu (localhost [127.0.0.1]) by mm01.cs.columbia.edu (Postfix) with ESMTP id 6045649EEB; Tue, 22 Feb 2022 13:53:24 -0500 (EST) Received: from localhost (localhost [127.0.0.1]) by mm01.cs.columbia.edu (Postfix) with ESMTP id 5248949EB1 for ; Tue, 22 Feb 2022 13:53:23 -0500 (EST) 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 dYh3Jcl0AyfQ for ; Tue, 22 Feb 2022 13:53:21 -0500 (EST) Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by mm01.cs.columbia.edu (Postfix) with ESMTP id C324949E08 for ; Tue, 22 Feb 2022 13:53:21 -0500 (EST) Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 0CE1F106F; Tue, 22 Feb 2022 10:53:21 -0800 (PST) Received: from lakrids (usa-sjc-imap-foss1.foss.arm.com [10.121.207.14]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 98D7E3F66F; Tue, 22 Feb 2022 10:53:18 -0800 (PST) Date: Tue, 22 Feb 2022 18:53:16 +0000 From: Mark Rutland To: Kalesh Singh Subject: Re: [PATCH v2 1/9] KVM: arm64: Introduce hyp_alloc_private_va_range() Message-ID: References: <20220222165212.2005066-1-kaleshsingh@google.com> <20220222165212.2005066-2-kaleshsingh@google.com> MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: <20220222165212.2005066-2-kaleshsingh@google.com> Cc: linux-arm-kernel@lists.infradead.org, kernel-team@android.com, Pasha Tatashin , will@kernel.org, Peter Collingbourne , maz@kernel.org, linux-kernel@vger.kernel.org, Joey Gouly , kvmarm@lists.cs.columbia.edu, Andrew Walbran , Catalin Marinas , surenb@google.com 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 Tue, Feb 22, 2022 at 08:51:02AM -0800, Kalesh Singh wrote: > hyp_alloc_private_va_range() can be used to reserve private VA ranges > in the nVHE hypervisor. Also update __create_hyp_private_mapping() > to allow specifying an alignment for the private VA mapping. > > These 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 > --- > arch/arm64/include/asm/kvm_mmu.h | 4 +++ > arch/arm64/kvm/mmu.c | 61 +++++++++++++++++++++----------- > 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 81839e9a8a24..0b0c71302b92 100644 > --- a/arch/arm64/include/asm/kvm_mmu.h > +++ b/arch/arm64/include/asm/kvm_mmu.h > @@ -153,6 +153,10 @@ static __always_inline unsigned long __kern_hyp_va(unsigned 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); > +unsigned long hyp_alloc_private_va_range(size_t size, size_t align); > +int __create_hyp_private_mapping(phys_addr_t phys_addr, size_t size, > + size_t align, unsigned long *haddr, > + enum kvm_pgtable_prot prot); > 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 bc2aba953299..e5abcce44ad0 100644 > --- a/arch/arm64/kvm/mmu.c > +++ b/arch/arm64/kvm/mmu.c > @@ -457,22 +457,16 @@ int create_hyp_mappings(void *from, void *to, enum kvm_pgtable_prot prot) > return 0; > } > > -static int __create_hyp_private_mapping(phys_addr_t phys_addr, size_t size, > - unsigned long *haddr, > - enum kvm_pgtable_prot prot) > + > +/* > + * Allocates a private VA range below io_map_base. > + * > + * @size: The size of the VA range to reserve. > + * @align: The required alignment for the allocation. > + */ > +unsigned long hyp_alloc_private_va_range(size_t size, size_t align) > { > unsigned long base; > - int ret = 0; > - > - if (!kvm_host_owns_hyp_mappings()) { > - base = kvm_call_hyp_nvhe(__pkvm_create_private_mapping, > - phys_addr, size, prot); > - if (IS_ERR_OR_NULL((void *)base)) > - return PTR_ERR((void *)base); There is a latent bug here; PTR_ERR() is not valid for NULL. Today on arm64 that will happen to return 0, which may or may not be what you want, but it's a bad pattern regardless. That applies to the two copies below that this has been transformed into. Thanks, Mark > - *haddr = base; > - > - return 0; > - } > > mutex_lock(&kvm_hyp_pgd_mutex); > > @@ -484,8 +478,8 @@ static int __create_hyp_private_mapping(phys_addr_t phys_addr, size_t size, > * > * The allocated size is always a multiple of PAGE_SIZE. > */ > - size = PAGE_ALIGN(size + offset_in_page(phys_addr)); > - base = io_map_base - size; > + base = io_map_base - PAGE_ALIGN(size); > + base = ALIGN_DOWN(base, align); > > /* > * Verify that BIT(VA_BITS - 1) hasn't been flipped by > @@ -493,20 +487,45 @@ static int __create_hyp_private_mapping(phys_addr_t phys_addr, size_t size, > * overflowed the idmap/IO address range. > */ > if ((base ^ io_map_base) & BIT(VA_BITS - 1)) > - ret = -ENOMEM; > + base = (unsigned long)ERR_PTR(-ENOMEM); > else > io_map_base = base; > > mutex_unlock(&kvm_hyp_pgd_mutex); > > + return base; > +} > + > +int __create_hyp_private_mapping(phys_addr_t phys_addr, size_t size, > + size_t align, unsigned long *haddr, > + enum kvm_pgtable_prot prot) > +{ > + unsigned long addr; > + int ret = 0; > + > + if (!kvm_host_owns_hyp_mappings()) { > + addr = kvm_call_hyp_nvhe(__pkvm_create_private_mapping, > + phys_addr, size, prot); > + if (IS_ERR_OR_NULL((void *)addr)) > + return PTR_ERR((void *)addr); > + *haddr = addr; > + > + return 0; > + } > + > + size += offset_in_page(phys_addr); > + addr = hyp_alloc_private_va_range(size, align); > + if (IS_ERR_OR_NULL((void *)addr)) > + return PTR_ERR((void *)addr); > + > if (ret) > goto out; > > - ret = __create_hyp_mappings(base, size, phys_addr, prot); > + ret = __create_hyp_mappings(addr, size, phys_addr, prot); > if (ret) > goto out; > > - *haddr = base + offset_in_page(phys_addr); > + *haddr = addr + offset_in_page(phys_addr); > out: > return ret; > } > @@ -537,7 +556,7 @@ int create_hyp_io_mappings(phys_addr_t phys_addr, size_t size, > return 0; > } > > - ret = __create_hyp_private_mapping(phys_addr, size, > + ret = __create_hyp_private_mapping(phys_addr, size, PAGE_SIZE, > &addr, PAGE_HYP_DEVICE); > if (ret) { > iounmap(*kaddr); > @@ -564,7 +583,7 @@ int create_hyp_exec_mappings(phys_addr_t phys_addr, size_t size, > > BUG_ON(is_kernel_in_hyp_mode()); > > - ret = __create_hyp_private_mapping(phys_addr, size, > + ret = __create_hyp_private_mapping(phys_addr, size, PAGE_SIZE, > &addr, PAGE_HYP_EXEC); > if (ret) { > *haddr = NULL; > -- > 2.35.1.473.g83b2b277ed-goog > _______________________________________________ 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 Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 6BC6DC433F5 for ; Tue, 22 Feb 2022 18:54:46 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:In-Reply-To:MIME-Version:References: Message-ID:Subject:Cc:To:From:Date:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=aciR2zgivBFqPTH5TJ13MFLOcf8DHXIkJOHhWc6W6WU=; b=5GCYsKQoh9bL8N +aDewYeFah+I444E3zjliar3XG69fr7JiYtLu2U7sb3lyzx5FVbSQK3ns16oZqVfrtifXyNdXUzqN KNf7fQbqiPiFBgeB4J8v7jyHlBtgxllBJCkfKDxRkuygL8C1stbMqnz0d5mve6/G8SclwnuFcclQ/ 30zecMWwqcBrD3Lx10gwkOBmSeCPvRMW8/RThZnW/RQAqpqeKFCT1XRAeCFSpyYL06fqmLWBzyzD4 Zfpn39ziiPGlPNIxcEiAtCr+aHGYFC09hBcGOQgqZdsA1RG5lOivePTJIXwKomXXXDYlu5H49q1XN FD0kqJTz9SRuFGtGHESg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1nMaHu-00BGI1-TN; Tue, 22 Feb 2022 18:53:27 +0000 Received: from foss.arm.com ([217.140.110.172]) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1nMaHq-00BGHS-7B for linux-arm-kernel@lists.infradead.org; Tue, 22 Feb 2022 18:53:23 +0000 Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 0CE1F106F; Tue, 22 Feb 2022 10:53:21 -0800 (PST) Received: from lakrids (usa-sjc-imap-foss1.foss.arm.com [10.121.207.14]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 98D7E3F66F; Tue, 22 Feb 2022 10:53:18 -0800 (PST) Date: Tue, 22 Feb 2022 18:53:16 +0000 From: Mark Rutland To: Kalesh Singh Cc: will@kernel.org, maz@kernel.org, qperret@google.com, tabba@google.com, surenb@google.com, kernel-team@android.com, Catalin Marinas , James Morse , Alexandru Elisei , Suzuki K Poulose , Ard Biesheuvel , Pasha Tatashin , Joey Gouly , Peter Collingbourne , Andrew Walbran , Andrew Scull , linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, kvmarm@lists.cs.columbia.edu Subject: Re: [PATCH v2 1/9] KVM: arm64: Introduce hyp_alloc_private_va_range() Message-ID: References: <20220222165212.2005066-1-kaleshsingh@google.com> <20220222165212.2005066-2-kaleshsingh@google.com> MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: <20220222165212.2005066-2-kaleshsingh@google.com> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20220222_105322_385231_AC8D31DC X-CRM114-Status: GOOD ( 25.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 Tue, Feb 22, 2022 at 08:51:02AM -0800, Kalesh Singh wrote: > hyp_alloc_private_va_range() can be used to reserve private VA ranges > in the nVHE hypervisor. Also update __create_hyp_private_mapping() > to allow specifying an alignment for the private VA mapping. > > These 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 > --- > arch/arm64/include/asm/kvm_mmu.h | 4 +++ > arch/arm64/kvm/mmu.c | 61 +++++++++++++++++++++----------- > 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 81839e9a8a24..0b0c71302b92 100644 > --- a/arch/arm64/include/asm/kvm_mmu.h > +++ b/arch/arm64/include/asm/kvm_mmu.h > @@ -153,6 +153,10 @@ static __always_inline unsigned long __kern_hyp_va(unsigned 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); > +unsigned long hyp_alloc_private_va_range(size_t size, size_t align); > +int __create_hyp_private_mapping(phys_addr_t phys_addr, size_t size, > + size_t align, unsigned long *haddr, > + enum kvm_pgtable_prot prot); > 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 bc2aba953299..e5abcce44ad0 100644 > --- a/arch/arm64/kvm/mmu.c > +++ b/arch/arm64/kvm/mmu.c > @@ -457,22 +457,16 @@ int create_hyp_mappings(void *from, void *to, enum kvm_pgtable_prot prot) > return 0; > } > > -static int __create_hyp_private_mapping(phys_addr_t phys_addr, size_t size, > - unsigned long *haddr, > - enum kvm_pgtable_prot prot) > + > +/* > + * Allocates a private VA range below io_map_base. > + * > + * @size: The size of the VA range to reserve. > + * @align: The required alignment for the allocation. > + */ > +unsigned long hyp_alloc_private_va_range(size_t size, size_t align) > { > unsigned long base; > - int ret = 0; > - > - if (!kvm_host_owns_hyp_mappings()) { > - base = kvm_call_hyp_nvhe(__pkvm_create_private_mapping, > - phys_addr, size, prot); > - if (IS_ERR_OR_NULL((void *)base)) > - return PTR_ERR((void *)base); There is a latent bug here; PTR_ERR() is not valid for NULL. Today on arm64 that will happen to return 0, which may or may not be what you want, but it's a bad pattern regardless. That applies to the two copies below that this has been transformed into. Thanks, Mark > - *haddr = base; > - > - return 0; > - } > > mutex_lock(&kvm_hyp_pgd_mutex); > > @@ -484,8 +478,8 @@ static int __create_hyp_private_mapping(phys_addr_t phys_addr, size_t size, > * > * The allocated size is always a multiple of PAGE_SIZE. > */ > - size = PAGE_ALIGN(size + offset_in_page(phys_addr)); > - base = io_map_base - size; > + base = io_map_base - PAGE_ALIGN(size); > + base = ALIGN_DOWN(base, align); > > /* > * Verify that BIT(VA_BITS - 1) hasn't been flipped by > @@ -493,20 +487,45 @@ static int __create_hyp_private_mapping(phys_addr_t phys_addr, size_t size, > * overflowed the idmap/IO address range. > */ > if ((base ^ io_map_base) & BIT(VA_BITS - 1)) > - ret = -ENOMEM; > + base = (unsigned long)ERR_PTR(-ENOMEM); > else > io_map_base = base; > > mutex_unlock(&kvm_hyp_pgd_mutex); > > + return base; > +} > + > +int __create_hyp_private_mapping(phys_addr_t phys_addr, size_t size, > + size_t align, unsigned long *haddr, > + enum kvm_pgtable_prot prot) > +{ > + unsigned long addr; > + int ret = 0; > + > + if (!kvm_host_owns_hyp_mappings()) { > + addr = kvm_call_hyp_nvhe(__pkvm_create_private_mapping, > + phys_addr, size, prot); > + if (IS_ERR_OR_NULL((void *)addr)) > + return PTR_ERR((void *)addr); > + *haddr = addr; > + > + return 0; > + } > + > + size += offset_in_page(phys_addr); > + addr = hyp_alloc_private_va_range(size, align); > + if (IS_ERR_OR_NULL((void *)addr)) > + return PTR_ERR((void *)addr); > + > if (ret) > goto out; > > - ret = __create_hyp_mappings(base, size, phys_addr, prot); > + ret = __create_hyp_mappings(addr, size, phys_addr, prot); > if (ret) > goto out; > > - *haddr = base + offset_in_page(phys_addr); > + *haddr = addr + offset_in_page(phys_addr); > out: > return ret; > } > @@ -537,7 +556,7 @@ int create_hyp_io_mappings(phys_addr_t phys_addr, size_t size, > return 0; > } > > - ret = __create_hyp_private_mapping(phys_addr, size, > + ret = __create_hyp_private_mapping(phys_addr, size, PAGE_SIZE, > &addr, PAGE_HYP_DEVICE); > if (ret) { > iounmap(*kaddr); > @@ -564,7 +583,7 @@ int create_hyp_exec_mappings(phys_addr_t phys_addr, size_t size, > > BUG_ON(is_kernel_in_hyp_mode()); > > - ret = __create_hyp_private_mapping(phys_addr, size, > + ret = __create_hyp_private_mapping(phys_addr, size, PAGE_SIZE, > &addr, PAGE_HYP_EXEC); > if (ret) { > *haddr = NULL; > -- > 2.35.1.473.g83b2b277ed-goog > _______________________________________________ linux-arm-kernel mailing list linux-arm-kernel@lists.infradead.org http://lists.infradead.org/mailman/listinfo/linux-arm-kernel