From patchwork Thu Jun 16 13:48:19 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexandru Elisei X-Patchwork-Id: 12883889 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 8F12BC43334 for ; Thu, 16 Jun 2022 13:49:49 +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:MIME-Version:References:In-Reply-To: Message-Id:Date:Subject:To:From:Reply-To:Cc:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=yXpUMqOmojI+sWnLm0Qdw49pUWKq41opKWuCldxROys=; b=C6xjfS7AHh57zV NAC2o+1EekYkx0VkYS4ZEvRtqYld1c64QCymQm2xrMj08/AVFEuTTz5Sk610JDwVH8sAGw71vg92c UDkd08YWyJvbIdFeadjM5Nej6nmt6wVJ545fP8ZhJCkCdFTukl24u9zFNNWK2keLLx40IyLgTGPdr IT77YLktHg+QznKXkkiOoxMD5yu+NTxBJPRSzBlOkmY3frRgGzzlbK3D8UGpaR3UN+3s9M8XBakc6 iV2zWLeIZ7DNvWDOEBllmLYzaOJevFePiayXEOxFMC/J1YUgZ/x9BIohbsPvgo0IYk9QfCl9A4NJH 6zEtJApPut3NzEg4SulA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1o1pre-002Zc8-OL; Thu, 16 Jun 2022 13:48:50 +0000 Received: from foss.arm.com ([217.140.110.172]) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1o1prK-002ZRn-Ij for linux-arm-kernel@lists.infradead.org; Thu, 16 Jun 2022 13:48:32 +0000 Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 800131474; Thu, 16 Jun 2022 06:48:29 -0700 (PDT) Received: from monolith.localdoman (unknown [172.31.20.19]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id D245A3F7F5; Thu, 16 Jun 2022 06:48:27 -0700 (PDT) From: Alexandru Elisei To: will@kernel.org, julien.thierry.kdev@gmail.com, maz@kernel.org, suzuki.poulose@arm.com, julien@xen.org, linux-arm-kernel@lists.infradead.org, kvmarm@lists.cs.columbia.edu, james.morse@arm.com, andre.przywara@arm.com Subject: [PATCH v4 kvmtool 03/12] builtin-run: Rework RAM size validation Date: Thu, 16 Jun 2022 14:48:19 +0100 Message-Id: <20220616134828.129006-4-alexandru.elisei@arm.com> X-Mailer: git-send-email 2.36.1 In-Reply-To: <20220616134828.129006-1-alexandru.elisei@arm.com> References: <20220616134828.129006-1-alexandru.elisei@arm.com> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20220616_064830_692890_10F83D9C X-CRM114-Status: GOOD ( 12.57 ) 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 host_ram_size() uses sysconf() to calculate the available ram, and sysconf() can fail. When that happens, host_ram_size() returns 0. kvmtool warns the user when the configured VM ram size exceeds the size of the host's memory, but doesn't take into account that host_ram_size() can return 0. If the function returns zero, skip the warning. Since this can only happen when the user sets the memory size (via the -m/--mem command line argument), skip the check entirely if the user hasn't set it. Move the check to kvm_run_validate_cfg(), as it checks for valid user configuration. Reviewed-by: Andre Przywara Signed-off-by: Alexandru Elisei --- builtin-run.c | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/builtin-run.c b/builtin-run.c index 2bf93fe13c92..e1770b3c9df2 100644 --- a/builtin-run.c +++ b/builtin-run.c @@ -509,6 +509,8 @@ static void kvm_run_set_real_cmdline(struct kvm *kvm) static void kvm_run_validate_cfg(struct kvm *kvm) { + u64 available_ram; + if (kvm->cfg.kernel_filename && kvm->cfg.firmware_filename) die("Only one of --kernel or --firmware can be specified"); @@ -518,6 +520,17 @@ static void kvm_run_validate_cfg(struct kvm *kvm) if (kvm->cfg.firmware_filename && kvm->cfg.initrd_filename) pr_warning("Ignoring initrd file when loading a firmware image"); + + if (kvm->cfg.ram_size) { + /* User specifies RAM size in megabytes. */ + kvm->cfg.ram_size <<= MB_SHIFT; + available_ram = host_ram_size(); + if (available_ram && kvm->cfg.ram_size > available_ram) { + pr_warning("Guest memory size %lluMB exceeds host physical RAM size %lluMB", + (unsigned long long)kvm->cfg.ram_size >> MB_SHIFT, + (unsigned long long)available_ram >> MB_SHIFT); + } + } } static struct kvm *kvm_cmd_run_init(int argc, const char **argv) @@ -596,13 +609,6 @@ static struct kvm *kvm_cmd_run_init(int argc, const char **argv) if (!kvm->cfg.ram_size) kvm->cfg.ram_size = get_ram_size(kvm->cfg.nrcpus); - else - kvm->cfg.ram_size <<= MB_SHIFT; - - if (kvm->cfg.ram_size > host_ram_size()) - pr_warning("Guest memory size %lluMB exceeds host physical RAM size %lluMB", - (unsigned long long)kvm->cfg.ram_size >> MB_SHIFT, - (unsigned long long)host_ram_size() >> MB_SHIFT); if (!kvm->cfg.dev) kvm->cfg.dev = DEFAULT_KVM_DEV;