From patchwork Fri Feb 25 03:34:46 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kalesh Singh X-Patchwork-Id: 12759607 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 6E14AC433F5 for ; Fri, 25 Feb 2022 03:40:40 +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=ghSiXRz0c5MImtX+3CRvcJ+Bq0BJ60gMCtZqFHlcFQI=; b=RTFTFwqYguESPW fR9aFgipOMo+XqGl7rtKCnk6J8eHw0uZKbc+MIsdcb17x+d94ndwVS+LrTd8r1zE/8cbnYM5mlr57 glX+HVVKlr67JF3yrQMctN2QGAA0wX5YSJKAfD2W5ptOC+hVaZmRE30aeucytGA7EdsGzjkMERJrv n6iGEnDJzVsb93CMTA9ml4XenGR1piFgxMVVkvqdtdgG0n+PDLhZ8WdWaTZppQ12D6Sb3+iYavQRR LSKdkQfBLJrgae1jVeNpomS9L2bj51jOlCRY+6IP9Obt1nMk7oXdn+WAw8HpK+AUGXT+vZYk0Uz8z 3mWCtK/poC6WjJ6Nj9+w==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1nNRS7-00375S-RP; Fri, 25 Feb 2022 03:39:32 +0000 Received: from mail-yb1-xb49.google.com ([2607:f8b0:4864:20::b49]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1nNRS3-00373s-HN for linux-arm-kernel@lists.infradead.org; Fri, 25 Feb 2022 03:39:29 +0000 Received: by mail-yb1-xb49.google.com with SMTP id x1-20020a25a001000000b0061c64ee0196so2021487ybh.9 for ; Thu, 24 Feb 2022 19:39:26 -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=3z7umWzjiaBi0H0VxEKou4UGWFcI8zJgOh8cZtYt5r8=; b=cSis1zFbDzg8HujAEb1ym/V6ME+YJ6BjUBqNYot/d81BO3h6D3Zk4WnTOTGc+6rl/a A1zpsu/lBasqxVBWEud7ld822jEooEBgrJsZMHuRMEVpfetkVIKN1s3ZYYpUx9+5/prc mBkto3DjqaO5JCwqXh801epJZthVVkZyzdjfKtgf6mptDewViPTKTLabmUOSYyPXrDQv cRZxkoyy97EAVUdmXTOT7ZsGdSwEwW2Kj8uqeQJUPxPMBB2z1yhVoKxwAqNUN6Inq610 wbd175TAvAMgG+YXzuUlp1+rLhkDzzcYQgJ93AXxefT4SUK0W9EQew3+ipLub9HkmUPL XFxg== 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=3z7umWzjiaBi0H0VxEKou4UGWFcI8zJgOh8cZtYt5r8=; b=khgXnWERIRnnxViD9hwNdTr2HChjSPOEiFc1L7CzkCzghk/MYjmnsDQMpYm5NJM0C2 2KbmapQ0dEMiDkS7jV7hNQCPYuTIOj0E4WcIg6xFI7eGJkdQivKjnoinjIAfr2zgTbMX gde+fb7hqsBqMokU1DqUn5QFOzwjl6TbxO6PvcrVYEIXxmBYztmJ/4BgGZBaEPQLDmgz uqV+27VzbdF4VrEYfwSD/SJ+JoGws2Fz99uGxDBC/h7+ptvJAUZxsaMxKT0Kl6eQhItY h7j5Stpanur0dOiV2IxbXHPHJCxIKKkj2xmL1uqHyBUbfXGgzfYktu/BcT9QKFNNx3kX OIYw== X-Gm-Message-State: AOAM531p1rSQSnfRjHKHP5AqL8QAciAeKpAL1yOiWGpcz6pfAld0RdV7 VzEBAEaJLM9/SnpTETjVwxC1/AxM0/bXjgoc0w== X-Google-Smtp-Source: ABdhPJzpCeaHUbNSr0tCnAVkQAGHMc2xtowI3g9fyEbYLjuQ5ZyqNkyEST6Fzl5lL9PO8MaOgXM5S0SNbU3r/dG5Dg== X-Received: from kaleshsingh.mtv.corp.google.com ([2620:15c:211:200:3a50:90b2:e6a2:9060]) (user=kaleshsingh job=sendgmr) by 2002:a25:13c6:0:b0:61e:105e:d291 with SMTP id 189-20020a2513c6000000b0061e105ed291mr5499552ybt.104.1645760365539; Thu, 24 Feb 2022 19:39:25 -0800 (PST) Date: Thu, 24 Feb 2022 19:34:46 -0800 In-Reply-To: <20220225033548.1912117-1-kaleshsingh@google.com> Message-Id: <20220225033548.1912117-2-kaleshsingh@google.com> Mime-Version: 1.0 References: <20220225033548.1912117-1-kaleshsingh@google.com> X-Mailer: git-send-email 2.35.1.574.g5d30c73bfb-goog Subject: [PATCH v4 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 Walbran , Andrew Scull , Zenghui Yu , 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-20220224_193927_610325_F995F564 X-CRM114-Status: GOOD ( 17.37 ) 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 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 | 4 ++ arch/arm64/kvm/mmu.c | 65 ++++++++++++++++++++------------ 2 files changed, 45 insertions(+), 24 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..a4868a6fa1c3 100644 --- a/arch/arm64/kvm/mmu.c +++ b/arch/arm64/kvm/mmu.c @@ -457,22 +457,17 @@ 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. + * @align: The required alignment for the allocation. + * + * The private VA range is allocated below io_map_base. + */ +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,29 +479,51 @@ 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 * allocating the new area, as it would indicate we've * overflowed the idmap/IO address range. */ - if ((base ^ io_map_base) & BIT(VA_BITS - 1)) - ret = -ENOMEM; + if (!base || (base ^ io_map_base) & BIT(VA_BITS - 1)) + 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((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((void *)addr)) + return PTR_ERR((void *)addr); - 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 +554,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 +581,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;