From patchwork Mon May 16 15:55:22 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexandru Elisei X-Patchwork-Id: 12851020 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 9FB7BC433EF for ; Mon, 16 May 2022 15:58:03 +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=aVQdfh+8nXjviZhLqw7ZKFhcdYE3mWibJ4yCOSS6HKo=; b=LCljMvtu9i0H0j 9+kxLrwrk2wdae41Dk9vg67lyESI0AULKOM/aOUNiX2kNvYnHrSd60oWuhaIw24PVHjHQGy31/9NQ bsVtGRBvseuvMTdpSVC0ANac7LqJVt9grIqb3vx8gEQqvgESWT0/FURN5rWNauLNXB+0eR9o6ybm1 SZ1rMoCm++3Hr5EHw4DmpmgkKOvowsef3KDBCE3sBbW4lmuaqp0VKIA/GqZd2Qt1508BMqAvsTgW3 jKxikZOeT/9J018YWGgffuOOocI9B+vrrs/RVAC7jH/rqv8GlUzsVlse6JuwJV5oNJ1UCn0UAsVP3 Gitwrp44VoAGS9a7p3Eg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1nqd5h-008lQf-Ie; Mon, 16 May 2022 15:57: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 1nqd4K-008koq-Vk for linux-arm-kernel@lists.infradead.org; Mon, 16 May 2022 15:55:38 +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 69B171063; Mon, 16 May 2022 08:55:36 -0700 (PDT) Received: from monolith.localdoman (unknown [172.31.20.19]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 0515B3F73D; Mon, 16 May 2022 08:55:34 -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 Subject: [PATCH v2 kvmtool 08/12] builtin_run: Allow standard size specifiers for memory Date: Mon, 16 May 2022 16:55:22 +0100 Message-Id: <20220516155526.181694-9-alexandru.elisei@arm.com> X-Mailer: git-send-email 2.36.1 In-Reply-To: <20220516155526.181694-1-alexandru.elisei@arm.com> References: <20220516155526.181694-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-20220516_085537_143352_35374305 X-CRM114-Status: GOOD ( 14.70 ) 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 From: Suzuki K Poulose Allow the user to use the standard B (bytes), K (kilobytes), M (megabytes), G (gigabytes), T (terabytes) and P (petabytes) suffixes for memory size. When none are specified, the default is megabytes. Also raise an error if the guest specifies 0 as the memory size, instead of treating it as uninitialized, as kvmtool has done so far. Signed-off-by: Suzuki K Poulose Signed-off-by: Alexandru Elisei --- builtin-run.c | 65 +++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 60 insertions(+), 5 deletions(-) diff --git a/builtin-run.c b/builtin-run.c index 2c37b91b6574..57c58be55159 100644 --- a/builtin-run.c +++ b/builtin-run.c @@ -49,9 +49,11 @@ #include #include -#define MB_SHIFT (20) #define KB_SHIFT (10) +#define MB_SHIFT (20) #define GB_SHIFT (30) +#define TB_SHIFT (40) +#define PB_SHIFT (50) __thread struct kvm_cpu *current_kvm_cpu; @@ -87,6 +89,60 @@ void kvm_run_set_wrapper_sandbox(void) kvm_run_wrapper = KVM_RUN_SANDBOX; } +static int parse_mem_unit(char **next) +{ + int shift = -1; + + switch (**next) { + case 'B': case 'b': shift = 0; break; + case 'K': case 'k': shift = KB_SHIFT; break; + case 'M': case 'm': shift = MB_SHIFT; break; + case 'G': case 'g': shift = GB_SHIFT; break; + case 'T': case 't': shift = TB_SHIFT; break; + case 'P': case 'p': shift = PB_SHIFT; break; + } + + if (shift == -1) { + /* The default is megabytes. */ + shift = MB_SHIFT; + } else { + (*next)++; + } + + return shift; +} + +static u64 parse_mem_option(const char *nptr, char **next) +{ + u64 shift; + u64 val; + + val = strtoull(nptr, next, 10); + if (errno == ERANGE) + die("Memory too large: %s", nptr); + shift = parse_mem_unit(next); + + if ((val << shift) < val) + die("Memory too large: %s", nptr); + + return val << shift; +} + +static int mem_parser(const struct option *opt, const char *arg, int unset) +{ + struct kvm *kvm = opt->ptr; + char *next; + + kvm->cfg.ram_size = parse_mem_option(arg, &next); + if (kvm->cfg.ram_size == 0) + die("Invalid RAM size: %s", arg); + + if (*next != '\0') + die("Invalid memory specifier: %s", arg); + + return 0; +} + #ifndef OPT_ARCH_RUN #define OPT_ARCH_RUN(...) #endif @@ -97,8 +153,9 @@ void kvm_run_set_wrapper_sandbox(void) OPT_STRING('\0', "name", &(cfg)->guest_name, "guest name", \ "A name for the guest"), \ OPT_INTEGER('c', "cpus", &(cfg)->nrcpus, "Number of CPUs"), \ - OPT_U64('m', "mem", &(cfg)->ram_size, "Virtual machine memory" \ - " size in MB."), \ + OPT_CALLBACK('m', "mem", NULL, "size[BKMGTP]", \ + "Virtual machine memory size, by default measured" \ + " in megabytes (M)", mem_parser, kvm), \ OPT_CALLBACK('d', "disk", kvm, "image or rootfs_dir", "Disk " \ " image or rootfs directory", img_name_parser, \ kvm), \ @@ -522,8 +579,6 @@ static void kvm_run_validate_cfg(struct kvm *kvm) 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",