From patchwork Thu Apr 28 15:55:53 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexandru Elisei X-Patchwork-Id: 12830977 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 39122C433EF for ; Thu, 28 Apr 2022 15:58:39 +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=zZp+FUABxKDE6qfEpuWD+8N27eHFd3YVQn/B6K1RD+Q=; b=vxtlaqSoh1Nz9K G/0g2ULgcGmYKVkOze3rVHqjSSsCF20zM3PY+BfHVFQ66mfXF+QiZDijKzrAkAJxMwP5BJg48zx4w jLtY4D2JzpBmj2ITXof6MLcxALejodi+rudjyQceDG6dVDR41aD/D2UxmhUdAyrRwSQalwv07/yNN UVSkjSlwJoYzE9E4ooZsfdet980+423KebxgKKGU+B2CX7CDHW0zkZs5XtF0+xvYNLSGVctjT7VfB KFJ4NKPSavooFGl8Tg2opL7EdvFRs7DH411/Je1gUmY194UcBjvDDy0iVEqOo18ObE1gzzuaue0cE 6df328NhtS7YOMcWipPQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1nk6WL-007knt-Vx; Thu, 28 Apr 2022 15:57:34 +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 1nk6V7-007kKT-7E for linux-arm-kernel@lists.infradead.org; Thu, 28 Apr 2022 15:56:19 +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 BABD91474; Thu, 28 Apr 2022 08:56:16 -0700 (PDT) Received: from monolith.localdoman (unknown [172.31.20.19]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 583EC3F774; Thu, 28 Apr 2022 08:56:15 -0700 (PDT) From: Alexandru Elisei To: julien.thierry.kdev@gmail.com, will@kernel.org, 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 kvmtool 06/15] builtin_run: Allow standard size specifiers for memory Date: Thu, 28 Apr 2022 16:55:53 +0100 Message-Id: <20220428155602.29445-7-alexandru.elisei@arm.com> X-Mailer: git-send-email 2.36.0 In-Reply-To: <20220428155602.29445-1-alexandru.elisei@arm.com> References: <20220428155602.29445-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-20220428_085617_400701_BC513FA8 X-CRM114-Status: GOOD ( 14.40 ) 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 | 66 +++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 61 insertions(+), 5 deletions(-) diff --git a/builtin-run.c b/builtin-run.c index 2c37b91b6574..0632a0c393a0 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,10 @@ 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. The default unit" \ + " of measurement is 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 +580,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",