From patchwork Thu Jun 16 13:48:18 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexandru Elisei X-Patchwork-Id: 12883890 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 846ACCCA47A for ; Thu, 16 Jun 2022 13:50:01 +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=B+8DDIx6xfPFZwBxwIGUQsy5Zl5iw3+cG04JXL3ILhA=; b=vjVx1gCe484Ujm mJ8HP55UbdEvEH381d36qWmWlxMRusAQHwgSGdXhIi8hfIep6LqXnSJWNUFE2jfvnGYrLqKAPoR7C H4PN8eCaLtURdQz8oluHyrSCowmdnl1eZdHnmXmDJgkKWSZcPQgAjPifUABAVUDJMZLpbf/+yZ4RR jbrOvZ9FqD4NTBi5wMxx9ieKP+AKDZvv2MXTdeOxGbUZSCV/PKSP0xCXKoSUydbnF7e+GCSF5ApeX wkORlTyTSu7nw6XI1ncQ0hoxo70H4yMRSEYFtY3s8ONAaXzHbyIs1cudGn/bewBQPdXwtisyRQKzR GflEE9/w/KLwCrtPjkfQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1o1prp-002Zgj-9g; Thu, 16 Jun 2022 13:49:01 +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 1o1prJ-002ZQw-BX 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 A95E513D5; Thu, 16 Jun 2022 06:48:27 -0700 (PDT) Received: from monolith.localdoman (unknown [172.31.20.19]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 08CB43F7F5; Thu, 16 Jun 2022 06:48:25 -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 02/12] builtin-run: Always use RAM size in bytes Date: Thu, 16 Jun 2022 14:48:18 +0100 Message-Id: <20220616134828.129006-3-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_064829_515786_845B807E X-CRM114-Status: GOOD ( 14.98 ) 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 The user can specify the virtual machine memory size in MB, which is saved in cfg->ram_size. kvmtool validates it against the host memory size, converted from bytes to MB. ram_size is then converted to bytes, and this is how it is used throughout the rest of kvmtool. To avoid any confusion about the unit of measurement, especially once the user is allowed to specify the unit of measurement, always use ram_size in bytes. Signed-off-by: Alexandru Elisei Reviewed-by: Andre Przywara --- builtin-run.c | 19 ++++++++++--------- include/kvm/kvm-config.h | 7 ++++--- include/kvm/kvm.h | 2 +- 3 files changed, 15 insertions(+), 13 deletions(-) diff --git a/builtin-run.c b/builtin-run.c index 0126c9fbcba6..2bf93fe13c92 100644 --- a/builtin-run.c +++ b/builtin-run.c @@ -36,6 +36,7 @@ #include #include +#include #include #include @@ -264,7 +265,7 @@ static u64 host_ram_size(void) return 0; } - return (nr_pages * page_size) >> MB_SHIFT; + return (u64)nr_pages * page_size; } /* @@ -278,11 +279,11 @@ static u64 get_ram_size(int nr_cpus) u64 available; u64 ram_size; - ram_size = 64 * (nr_cpus + 3); + ram_size = (u64)SZ_64M * (nr_cpus + 3); available = host_ram_size() * RAM_SIZE_RATIO; if (!available) - available = MIN_RAM_SIZE_MB; + available = MIN_RAM_SIZE; if (ram_size > available) ram_size = available; @@ -595,13 +596,13 @@ 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, - (unsigned long long)host_ram_size()); - - kvm->cfg.ram_size <<= MB_SHIFT; + (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; @@ -676,12 +677,12 @@ static struct kvm *kvm_cmd_run_init(int argc, const char **argv) if (kvm->cfg.kernel_filename) { printf(" # %s run -k %s -m %Lu -c %d --name %s\n", KVM_BINARY_NAME, kvm->cfg.kernel_filename, - (unsigned long long)kvm->cfg.ram_size / 1024 / 1024, + (unsigned long long)kvm->cfg.ram_size >> MB_SHIFT, kvm->cfg.nrcpus, kvm->cfg.guest_name); } else if (kvm->cfg.firmware_filename) { printf(" # %s run --firmware %s -m %Lu -c %d --name %s\n", KVM_BINARY_NAME, kvm->cfg.firmware_filename, - (unsigned long long)kvm->cfg.ram_size / 1024 / 1024, + (unsigned long long)kvm->cfg.ram_size >> MB_SHIFT, kvm->cfg.nrcpus, kvm->cfg.guest_name); } diff --git a/include/kvm/kvm-config.h b/include/kvm/kvm-config.h index 6a5720c4c7d4..31bc89520d52 100644 --- a/include/kvm/kvm-config.h +++ b/include/kvm/kvm-config.h @@ -5,6 +5,8 @@ #include "kvm/vfio.h" #include "kvm/kvm-config-arch.h" +#include + #define DEFAULT_KVM_DEV "/dev/kvm" #define DEFAULT_CONSOLE "serial" #define DEFAULT_NETWORK "user" @@ -15,14 +17,13 @@ #define DEFAULT_SCRIPT "none" #define DEFAULT_SANDBOX_FILENAME "guest/sandbox.sh" -#define MIN_RAM_SIZE_MB (64ULL) -#define MIN_RAM_SIZE_BYTE (MIN_RAM_SIZE_MB << MB_SHIFT) +#define MIN_RAM_SIZE SZ_64M struct kvm_config { struct kvm_config_arch arch; struct disk_image_params disk_image[MAX_DISK_IMAGES]; struct vfio_device_params *vfio_devices; - u64 ram_size; + u64 ram_size; /* Guest memory size, in bytes */ u8 num_net_devices; u8 num_vfio_devices; u64 vsock_cid; diff --git a/include/kvm/kvm.h b/include/kvm/kvm.h index ad732e56f5ed..7b14b33b50ca 100644 --- a/include/kvm/kvm.h +++ b/include/kvm/kvm.h @@ -87,7 +87,7 @@ struct kvm { struct kvm_cpu **cpus; u32 mem_slots; /* for KVM_SET_USER_MEMORY_REGION */ - u64 ram_size; + u64 ram_size; /* Guest memory size, in bytes */ void *ram_start; u64 ram_pagesize; struct mutex mem_banks_lock;