Received: by 2002:a05:6a10:9848:0:0:0:0 with SMTP id x8csp1257464pxf; Fri, 19 Mar 2021 03:05:02 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxldpPAybM0i3x+muQ4c2VgNRnyCa7Eeic2/k9dvP6vHsKfcClgAl4HUc4v4vFcGyo/t+5A X-Received: by 2002:a17:907:110c:: with SMTP id qu12mr3434174ejb.442.1616148302344; Fri, 19 Mar 2021 03:05:02 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1616148302; cv=none; d=google.com; s=arc-20160816; b=VDg55Ai8RlgTc49cbVxH7lCmayQhU5jwSM+CSOyHiOpMxIL8a4GGrw/Ji6kw6jEG/0 yBXcLeJpX4Qh1E2O99xtvWBdJDmwIIO52Io8cZYU9RqSuJ78zQKcGtH5vbQ0Ed3HTvrl ASucAQ4CPuWQEF0w6vgQdihdPDdI4QfbL32IDI/8EhoZnGMoYoMgMAvHf1lbS4tJwTxW R6PisxmcqQzsMM6EHQpV4IhySxcX7/G8/mSbal2as+CLeF1vKnLnr7/ysBT/AVDylwaY 1fDRqjI/QlU5xOLDpUPpIZdjoxTmBgcfeIsmGOm5vimRtx5OlEfIRrT6SJXu++T1ZHos DBvw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:from:subject:references:mime-version :message-id:in-reply-to:date:dkim-signature; bh=XmlXvszH/b/asNjtzAQpiqetpKnqM2IpsKRJDC1QZ6Q=; b=YPq9RgNvi5JFpQuJzbDu0FfL+XZWy9RmXRsQAoRHtQUJq1dO/TiIbs6dDAlpU1d80n QAbDc8zx4YfdqQTRQVDA9aARJ8E11BXYQz6HrUFm4L7seRru1WAkrJyiVtWMU8jGJd52 KPk3KOfklA7tqfH1fExMX8gNALpz0dJh40P3L/eNVwygsZvu0uNW9Ongj1l1T+uCwYf6 QhwZxlzCy2GaLIlWbm396f4dNVKNZmWqHEtXHow5inAhjN90ZYTaJ2jvpTMq9cvDpSdd 6ZY9GbVv+/Gur9yNh+UV7hccM1NxadubDNkYfpPF/iO9GehvsBRR+gXuD5asHrGBZcfL VOVg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=n4Dk7ghO; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id n21si3759966edv.569.2021.03.19.03.04.39; Fri, 19 Mar 2021 03:05:02 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=n4Dk7ghO; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230367AbhCSKDY (ORCPT + 99 others); Fri, 19 Mar 2021 06:03:24 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54250 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230097AbhCSKCr (ORCPT ); Fri, 19 Mar 2021 06:02:47 -0400 Received: from mail-wm1-x349.google.com (mail-wm1-x349.google.com [IPv6:2a00:1450:4864:20::349]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 86E2BC06175F for ; Fri, 19 Mar 2021 03:02:46 -0700 (PDT) Received: by mail-wm1-x349.google.com with SMTP id c9so4391647wme.5 for ; Fri, 19 Mar 2021 03:02:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=XmlXvszH/b/asNjtzAQpiqetpKnqM2IpsKRJDC1QZ6Q=; b=n4Dk7ghOSzuv6hVrIpa0xZkXJiJp/2GO82+nTiQ8hFfxhtk7tfUubAu5G6oAsbQ3B0 J/XUIjaZfAGthgBmJZnzVE7NvdMLH7gOUo3BNzXhIqUHXTczuV3LrPo2veo6MqS0Vr18 BZ2PaFQo3EVEHLmVITwSYsopCbG6T5mhJlHfW78S6B9IAiknDWxS7ZcEpBpZf4kDtvDG 1TQVQ/lMArkzLkuu5rRrjpTVy0B/G+/vRlL+P0QaA9wAPeyOX0fPLGqIKOy7t7G5nQWF Fs1v54qAANB1TYF5F8ggD8dCP72Rfo6Z0PA9awAi+CXrXvkOt3vI34oofG/rj38cYwLT G17Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=XmlXvszH/b/asNjtzAQpiqetpKnqM2IpsKRJDC1QZ6Q=; b=lt1Zle0fCfGLzqg8AOCyVnBr1wwcWggYlmWO9pgbcC+bhuPhnoeDs4/3ge954bZf3p 4F7DHaQ58rG6QwmBlyvraAgwGntT/Q5PwBv++XqMxVtENXMo9FiNWLmBT0kxtDRqYV7x 4aBVVK8nwzB33mGs6qIAw5kJa8kZ9O2uVFu1/gP1S74DoesFbuoBUlNpQjbt1UCLIyQm gvECkwvX1+KsOpC1zH0aV17BNSgEwgrMsAi+uIh0xsfY7TeUU/F8yUpZppSl0yMpDzuE 4vsn00ORn1rL6ZiVBsziE43aBC+2ZNPB6Z71L3nKMIwHvhIlgwNjxOGwu6qh4wWYmT+H +wPg== X-Gm-Message-State: AOAM5331L+21ZUUYZpmZlL7ENcD5CvhN9t/pDnCBstJ1PkBtZ+Qs86Fc eckxWpg3ISMi6Qw+8LvMT0l52t2fARYJ X-Received: from r2d2-qp.c.googlers.com ([fda3:e722:ac3:10:28:9cb1:c0a8:1652]) (user=qperret job=sendgmr) by 2002:a05:600c:290b:: with SMTP id i11mr3046295wmd.129.1616148165171; Fri, 19 Mar 2021 03:02:45 -0700 (PDT) Date: Fri, 19 Mar 2021 10:01:34 +0000 In-Reply-To: <20210319100146.1149909-1-qperret@google.com> Message-Id: <20210319100146.1149909-27-qperret@google.com> Mime-Version: 1.0 References: <20210319100146.1149909-1-qperret@google.com> X-Mailer: git-send-email 2.31.0.rc2.261.g7f71774620-goog Subject: [PATCH v6 26/38] KVM: arm64: Reserve memory for host stage 2 From: Quentin Perret To: catalin.marinas@arm.com, will@kernel.org, maz@kernel.org, james.morse@arm.com, julien.thierry.kdev@gmail.com, suzuki.poulose@arm.com Cc: android-kvm@google.com, seanjc@google.com, mate.toth-pal@arm.com, linux-kernel@vger.kernel.org, robh+dt@kernel.org, linux-arm-kernel@lists.infradead.org, kernel-team@android.com, kvmarm@lists.cs.columbia.edu, tabba@google.com, ardb@kernel.org, mark.rutland@arm.com, dbrazdil@google.com, qperret@google.com Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Extend the memory pool allocated for the hypervisor to include enough pages to map all of memory at page granularity for the host stage 2. While at it, also reserve some memory for device mappings. Acked-by: Will Deacon Signed-off-by: Quentin Perret --- arch/arm64/kvm/hyp/include/nvhe/mm.h | 27 ++++++++++++++++++++++++++- arch/arm64/kvm/hyp/nvhe/setup.c | 12 ++++++++++++ arch/arm64/kvm/hyp/reserved_mem.c | 2 ++ 3 files changed, 40 insertions(+), 1 deletion(-) diff --git a/arch/arm64/kvm/hyp/include/nvhe/mm.h b/arch/arm64/kvm/hyp/include/nvhe/mm.h index ac0f7fcffd08..0095f6289742 100644 --- a/arch/arm64/kvm/hyp/include/nvhe/mm.h +++ b/arch/arm64/kvm/hyp/include/nvhe/mm.h @@ -53,7 +53,7 @@ static inline unsigned long __hyp_pgtable_max_pages(unsigned long nr_pages) return total; } -static inline unsigned long hyp_s1_pgtable_pages(void) +static inline unsigned long __hyp_pgtable_total_pages(void) { unsigned long res = 0, i; @@ -63,9 +63,34 @@ static inline unsigned long hyp_s1_pgtable_pages(void) res += __hyp_pgtable_max_pages(reg->size >> PAGE_SHIFT); } + return res; +} + +static inline unsigned long hyp_s1_pgtable_pages(void) +{ + unsigned long res; + + res = __hyp_pgtable_total_pages(); + /* Allow 1 GiB for private mappings */ res += __hyp_pgtable_max_pages(SZ_1G >> PAGE_SHIFT); return res; } + +static inline unsigned long host_s2_mem_pgtable_pages(void) +{ + /* + * Include an extra 16 pages to safely upper-bound the worst case of + * concatenated pgds. + */ + return __hyp_pgtable_total_pages() + 16; +} + +static inline unsigned long host_s2_dev_pgtable_pages(void) +{ + /* Allow 1 GiB for MMIO mappings */ + return __hyp_pgtable_max_pages(SZ_1G >> PAGE_SHIFT); +} + #endif /* __KVM_HYP_MM_H */ diff --git a/arch/arm64/kvm/hyp/nvhe/setup.c b/arch/arm64/kvm/hyp/nvhe/setup.c index 1e8bcd8b0299..c1a3e7e0ebbc 100644 --- a/arch/arm64/kvm/hyp/nvhe/setup.c +++ b/arch/arm64/kvm/hyp/nvhe/setup.c @@ -24,6 +24,8 @@ unsigned long hyp_nr_cpus; static void *vmemmap_base; static void *hyp_pgt_base; +static void *host_s2_mem_pgt_base; +static void *host_s2_dev_pgt_base; static int divide_memory_pool(void *virt, unsigned long size) { @@ -42,6 +44,16 @@ static int divide_memory_pool(void *virt, unsigned long size) if (!hyp_pgt_base) return -ENOMEM; + nr_pages = host_s2_mem_pgtable_pages(); + host_s2_mem_pgt_base = hyp_early_alloc_contig(nr_pages); + if (!host_s2_mem_pgt_base) + return -ENOMEM; + + nr_pages = host_s2_dev_pgtable_pages(); + host_s2_dev_pgt_base = hyp_early_alloc_contig(nr_pages); + if (!host_s2_dev_pgt_base) + return -ENOMEM; + return 0; } diff --git a/arch/arm64/kvm/hyp/reserved_mem.c b/arch/arm64/kvm/hyp/reserved_mem.c index 9bc6a6d27904..fd42705a3c26 100644 --- a/arch/arm64/kvm/hyp/reserved_mem.c +++ b/arch/arm64/kvm/hyp/reserved_mem.c @@ -52,6 +52,8 @@ void __init kvm_hyp_reserve(void) } hyp_mem_pages += hyp_s1_pgtable_pages(); + hyp_mem_pages += host_s2_mem_pgtable_pages(); + hyp_mem_pages += host_s2_dev_pgtable_pages(); /* * The hyp_vmemmap needs to be backed by pages, but these pages -- 2.31.0.rc2.261.g7f71774620-goog