From patchwork Wed Apr 20 21:42:52 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kalesh Singh X-Patchwork-Id: 12820835 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 6F3B7C433EF for ; Wed, 20 Apr 2022 21:45:03 +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:Cc:From:Subject:References:Mime-Version :Message-Id:In-Reply-To:Date:Reply-To:To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=hE15ujnK0wuvjISPjP7YuKWRhpFKIckq99vHUoXQhxw=; b=TXFM/8+QJEE3pu R/f7zzLqNeKn58wYrA42r8jmUIDQJDn9u7CU4jXraMAIx33q5KHYkrx4Pk8dUMptWJL0nAByP2K9J aPmrxAhrBpTzpDjx5xPsiMhRZDN7FXGN1OT8m5XOWmPQJUAvPV7pminD/rdD5ldiTp5Nyfn8G+RNL aicr07/d8KYQi6foflHXQHZh06B6JMr6eyq6khHTVWQnXunZKmusVJct7D36hvAZoWThQ18bYF1gG 8fVpcnY5IBOsI7RczUzGqO1/2tE1O72CiZuNBanRCLObKQptWFcfEz7lPH0qo/PrPf6+0zh2axsZK VfBLRKkbRpZ/CeE+DHmQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1nhI7F-00AU2h-Mr; Wed, 20 Apr 2022 21:44:01 +0000 Received: from mail-yb1-xb4a.google.com ([2607:f8b0:4864:20::b4a]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1nhI7C-00AU0y-Hc for linux-arm-kernel@lists.infradead.org; Wed, 20 Apr 2022 21:44:00 +0000 Received: by mail-yb1-xb4a.google.com with SMTP id l16-20020a25cc10000000b006456cb3d1deso1318102ybf.14 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=TxuqlSQwgNwR8fuxVsySf7ubg9WdyNEMFxBoMvcqgNZ+YVuYAVEkvsjT9pU1ZlcPhV 9vww6ekuNoQrZxYEQQZ7CtY2aTz1xgg52rzIH68xAfB2/4rAvmGHW+bNaJ0jD/2RH7+2 cbQvQoFlbfaCGkGizFLD1B7jQtheP6pzTi2a0nHyT/tOwbpUub9iFuUUR2QQ+YQ5cYBY wS96aBPqHgPSN47OldgXB9/fRq0CDaKx1iGzf12vjW/g1jyZkAjNaH7DdQiaSM7QLxHt /Pm/3UL9mHcOLXqk2bgCUYJL5cSczclkOiGItl52B1wMcmNUUVp9qNRvnnVwPSJbTSIJ +Qiw== X-Gm-Message-State: AOAM5331VR6Phayn+6dR1vb3DcGJV8Uv01/DbRI/bjlrLXDafXAVrMJy w0GDnEUm0elL7+iNQBECah9TugR1FXDzkTDfeQ== 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 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20220420_144358_644481_250B56A8 X-CRM114-Status: GOOD ( 19.77 ) 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: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org 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(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); +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 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) + +/** + * 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 = 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); - *haddr = base; - - return 0; - } - mutex_lock(&kvm_hyp_pgd_mutex); /* @@ -484,8 +483,10 @@ 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); + + /* Align the allocation based on the order of its size */ + base = ALIGN_DOWN(base, PAGE_SIZE << get_order(size)); /* * Verify that BIT(VA_BITS - 1) hasn't been flipped by @@ -495,19 +496,40 @@ static int __create_hyp_private_mapping(phys_addr_t phys_addr, size_t size, if ((base ^ io_map_base) & BIT(VA_BITS - 1)) ret = -ENOMEM; else - io_map_base = base; + *haddr = io_map_base = base; mutex_unlock(&kvm_hyp_pgd_mutex); + 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 = 0; + + if (!kvm_host_owns_hyp_mappings()) { + addr = kvm_call_hyp_nvhe(__pkvm_create_private_mapping, + phys_addr, size, prot); + if (IS_ERR_VALUE(addr)) + return addr; + *haddr = addr; + + return 0; + } + + size = PAGE_ALIGN(size + offset_in_page(phys_addr)); + ret = hyp_alloc_private_va_range(size, &addr); if (ret) - goto out; + return ret; - ret = __create_hyp_mappings(base, size, phys_addr, prot); + ret = __create_hyp_mappings(addr, size, phys_addr, prot); if (ret) - goto out; + return ret; - *haddr = base + offset_in_page(phys_addr); -out: + *haddr = addr + offset_in_page(phys_addr); return ret; }