From patchwork Thu Dec 20 15:21:22 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Julien Grall X-Patchwork-Id: 10739059 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 0C8FC13B5 for ; Thu, 20 Dec 2018 15:21:49 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id F277828D00 for ; Thu, 20 Dec 2018 15:21:48 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id EE22728D27; Thu, 20 Dec 2018 15:21:48 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-7.9 required=2.0 tests=BAYES_00,MAILING_LIST_MULTI, RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 9A13A28D2A for ; Thu, 20 Dec 2018 15:21:48 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2387797AbeLTPVr (ORCPT ); Thu, 20 Dec 2018 10:21:47 -0500 Received: from foss.arm.com ([217.140.101.70]:57860 "EHLO foss.arm.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2387822AbeLTPVp (ORCPT ); Thu, 20 Dec 2018 10:21:45 -0500 Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.72.51.249]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 6C7461596; Thu, 20 Dec 2018 07:21:45 -0800 (PST) Received: from e108454-lin.cambridge.arm.com (e108454-lin.cambridge.arm.com [10.1.196.50]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 5AC993F5C0; Thu, 20 Dec 2018 07:21:44 -0800 (PST) From: Julien Grall To: kvm@vger.kernel.org Cc: suzuki.poulose@arm.com, marc.zyngier@arm.com, will.deacon@arm.com, Julien Grall Subject: [PATCH kvmtool v3 5/9] arm: Add an helper to sanitize KVM configuration Date: Thu, 20 Dec 2018 15:21:22 +0000 Message-Id: <20181220152126.18741-6-julien.grall@arm.com> X-Mailer: git-send-email 2.11.0 In-Reply-To: <20181220152126.18741-1-julien.grall@arm.com> References: <20181220152126.18741-1-julien.grall@arm.com> Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP At the moment only the RAM size is checked. A follow-up patch will add more sanity check. Signed-off-by: Julien Grall --- Changes in v2: - Add missing commit message --- arm/aarch64/include/kvm/kvm-arch.h | 2 +- arm/kvm.c | 13 ++++++++++++- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/arm/aarch64/include/kvm/kvm-arch.h b/arm/aarch64/include/kvm/kvm-arch.h index 9de623a..1b3d0a5 100644 --- a/arm/aarch64/include/kvm/kvm-arch.h +++ b/arm/aarch64/include/kvm/kvm-arch.h @@ -5,7 +5,7 @@ 0x8000 : \ 0x80000) -#define ARM_MAX_MEMORY(kvm) ((kvm)->cfg.arch.aarch32_guest ? \ +#define ARM_MAX_MEMORY(cfg) ((cfg)->arch.aarch32_guest ? \ ARM_LOMAP_MAX_MEMORY : \ ARM_HIMAP_MAX_MEMORY) diff --git a/arm/kvm.c b/arm/kvm.c index b6ff2e2..45022aa 100644 --- a/arm/kvm.c +++ b/arm/kvm.c @@ -57,6 +57,15 @@ void kvm__arch_set_cmdline(char *cmdline, bool video) { } +static void kvm__arch_sanitize_cfg(struct kvm_config *cfg) +{ + if (cfg->ram_size > ARM_MAX_MEMORY(cfg)) { + cfg->ram_size = ARM_MAX_MEMORY(cfg); + pr_warning("sanitize: Capping memory to %lluMB", + cfg->ram_size >> 20); + } +} + void kvm__arch_init(struct kvm *kvm) { unsigned long alignment; @@ -64,6 +73,8 @@ void kvm__arch_init(struct kvm *kvm) /* Convenience aliases */ const char *hugetlbfs_path = kvm->cfg.hugetlbfs_path; + kvm__arch_sanitize_cfg(&kvm->cfg); + /* * Allocate guest memory. We must align our buffer to 64K to * correlate with the maximum guest page size for virtio-mmio. @@ -76,7 +87,7 @@ void kvm__arch_init(struct kvm *kvm) alignment = SZ_32M; else alignment = SZ_2M; - kvm->ram_size = min(kvm->cfg.ram_size, (u64)ARM_MAX_MEMORY(kvm)); + kvm->ram_size = kvm->cfg.ram_size; kvm->arch.ram_alloc_size = kvm->ram_size + alignment; kvm->arch.ram_alloc_start = mmap_anon_or_hugetlbfs(kvm, hugetlbfs_path, kvm->arch.ram_alloc_size);