From patchwork Thu Feb 24 05:13:36 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kalesh Singh X-Patchwork-Id: 12757841 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 3EADFC433EF for ; Thu, 24 Feb 2022 05:17:30 +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=YCXrDHWIYDCLc0K4Uwvblwidrz7dY/vkYshSjfiboq0=; b=j/sqJI0WmXP2Vb iL29+Lkzr3R85F/k0eNoC3ZtEyAUwlBy3H/djGrNtIHNvjII4vN3r4fktiYGGb+QdC9EHXGhNqX/Y aq7uZ2JMgjXWjgwjVSnW88iB2dl7BoGMt6lO+bHTcZQeWeyuHOCyChy0Pb8e+qF4xKQVjhGrQxV2h zLADy9NuJnTCExmUIBOjKzGoXwXfEpBDCDgHUToC5Ece6iv3TvQ0o4IK5Lg1QA5uRPX8s0xRQP52y o2/nmBt39MvYIeTPVYxMBddUsD1PH8mNDY0Wx8h9GhFnH2ckHIUEN66xK+nb3enNXUsIMpSHXuRtK 8/2LQVOY54yyOdjVZUTA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1nN6UI-00Gpjj-5E; Thu, 24 Feb 2022 05:16:22 +0000 Received: from mail-yw1-x1149.google.com ([2607:f8b0:4864:20::1149]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1nN6UD-00Gpiu-PM for linux-arm-kernel@lists.infradead.org; Thu, 24 Feb 2022 05:16:19 +0000 Received: by mail-yw1-x1149.google.com with SMTP id 00721157ae682-2d6180e0ab4so11886007b3.2 for ; Wed, 23 Feb 2022 21:16:16 -0800 (PST) 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=mMupdIrbmOm80/sZrASEhygALQnDQAhlGvHAY7r7H5w=; b=X6XkcRUhmtAVIiQWY//3Gn68yYt9JlaMc47Vxfs+9cbIeqKDJtChfU2PP1IOBRmR1x ti8C4IVVDHdguNDBsD2+Zu2z/+0wf1X0jjy8HgGHG5uy+eXYp4j1Mytn6RWE7zYG3Pjh qcHknwrb+7+95GUvIDQnxRanAZSZWd6SqEFWEJJ9Xn2MnMnc1Ul9PgDZjLJvsIsLXGX7 AsxVI1BhGqVIogXF3czePdz1Ax326NINXaOl1FH+v9tqYAT57vqsKVzzNH50KPCClXnd VHJRzl7HiQYfXLY0uKpfCjgyCDYQj9/O8D4qmTHjNwlFd2AqpCH4gs/GGlghND4EjnTW b0nw== 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=mMupdIrbmOm80/sZrASEhygALQnDQAhlGvHAY7r7H5w=; b=q2mntYW0wdv64LX7LO2rHXO7T1W94xYuKH1hObOnUwcks2uZV3+3y3bJfc5aOoRGga D79WECGq9sC4UKGVfDBpTYcbJnQZBS7ySKeaeA4czaltnOLzAN1MGVOvdCqHUaa5eUmg 6irxi+EcsNBhcsTX2hIeIa6d+uLj2yszSn0JZCp6/KsIkmCDOx2ha6xagj3tB0J+KGlX Po8ESi9ut4fbENH0lAxJ1z7J8INaowCLF94m/S1f6rkgpY+D4NM+VTZZgbXm3mUM1ljs OhCFtT6sQ9UuB3MwDrO+fIJTQTg+IjGlE+SRJyPfD/ofgkLHAGpuhWnOMKitGOTZZ8SR brHg== X-Gm-Message-State: AOAM53246cqkUqac0gAnMe2ob9jujHuJSS3LjxWV4YL7c12XcTjFGrkQ oaysGc0gNL2UIcnDSVXPOxPDes5XgVcSk+xjsQ== X-Google-Smtp-Source: ABdhPJwXwzSytvMwsjPP9WzSxgb1djKy65Lz6QLBpdS+tGwETD/J0V3ZhDRlutQHDIbQP4uTSJSAD6d/fMA2MaVCFQ== X-Received: from kaleshsingh.mtv.corp.google.com ([2620:15c:211:200:415c:9be0:c461:440a]) (user=kaleshsingh job=sendgmr) by 2002:a81:1cd8:0:b0:2d6:b74b:5b55 with SMTP id c207-20020a811cd8000000b002d6b74b5b55mr908040ywc.149.1645679776175; Wed, 23 Feb 2022 21:16:16 -0800 (PST) Date: Wed, 23 Feb 2022 21:13:36 -0800 In-Reply-To: <20220224051439.640768-1-kaleshsingh@google.com> Message-Id: <20220224051439.640768-2-kaleshsingh@google.com> Mime-Version: 1.0 References: <20220224051439.640768-1-kaleshsingh@google.com> X-Mailer: git-send-email 2.35.1.473.g83b2b277ed-goog Subject: [PATCH v3 1/8] 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 , Mark Brown , Masami Hiramatsu , Peter Collingbourne , "Madhavan T. Venkataraman" , Andrew Scull , Paolo Bonzini , Ard Biesheuvel , 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-20220223_211617_852940_99B569CA X-CRM114-Status: GOOD ( 17.49 ) 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. 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 --- Changes in v3: - Handle null ptr in IS_ERR_OR_NULL checks, per Mark arch/arm64/include/asm/kvm_mmu.h | 4 +++ arch/arm64/kvm/mmu.c | 62 ++++++++++++++++++++------------ 2 files changed, 43 insertions(+), 23 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..fc09536c8197 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); - *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,42 @@ 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); - if (ret) - goto out; + 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 addr ? PTR_ERR((void *)addr) : -ENOMEM; + *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 addr ? PTR_ERR((void *)addr) : -ENOMEM; - 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 +553,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 +580,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;