From patchwork Thu Jun 16 13:48:24 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexandru Elisei X-Patchwork-Id: 12883896 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 72392C43334 for ; Thu, 16 Jun 2022 13:52:25 +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=3r64hbXkqG3L10SXtqRFxMl+tnPvPH2SU2ImXnHfVa0=; b=PhrXLxBE7mh8aq jooDbaoe2L3Bp8XOjMKRdxgq1tzKAwswoXpuvCHW+tskA79c11StJKDrCzCdkClhf3TadEi0ZtX3A wWnh0d1UXQxtHZb4XZIpm1RL4BQeUL+bIKpQBbYdaMui0Q9MUZBVmV4r+ns0c7mQPMiEt0yg6xo38 OxRE9/ba97iEZZKA/gyb4//yeK9MoeXcAI7bU004Tnjc/ySYqy38FfPeeS49yfjCj0pT4xlJ4+A04 X70+ZkVIk3IqJ+1YpctE6wTEmwR1o/KZoOl4Upw85ynDhFmlZ6PH5iIJuX/khQdwW11hk4wdFbOc5 ML32G527s92QTVOESwfg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1o1puC-002b0L-2m; Thu, 16 Jun 2022 13:51:28 +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 1o1prT-002ZX0-Mx for linux-arm-kernel@lists.infradead.org; Thu, 16 Jun 2022 13:48:42 +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 88FF212FC; Thu, 16 Jun 2022 06:48:38 -0700 (PDT) Received: from monolith.localdoman (unknown [172.31.20.19]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id E03E33F7F5; Thu, 16 Jun 2022 06:48:36 -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 08/12] builtin_run: Allow standard size specifiers for memory Date: Thu, 16 Jun 2022 14:48:24 +0100 Message-Id: <20220616134828.129006-9-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_064839_927044_25B0E235 X-CRM114-Status: GOOD ( 13.56 ) 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 Reviewed-by: Andre Przywara --- builtin-run.c | 59 ++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 54 insertions(+), 5 deletions(-) diff --git a/builtin-run.c b/builtin-run.c index dcd08f739469..8b4e865f0a0e 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,54 @@ void kvm_run_set_wrapper_sandbox(void) kvm_run_wrapper = KVM_RUN_SANDBOX; } +static int parse_mem_unit(char **next) +{ + switch (**next) { + case 'B': case 'b': (*next)++; return 0; + case 'K': case 'k': (*next)++; return KB_SHIFT; + case 'M': case 'm': (*next)++; return MB_SHIFT; + case 'G': case 'g': (*next)++; return GB_SHIFT; + case 'T': case 't': (*next)++; return TB_SHIFT; + case 'P': case 'p': (*next)++; return PB_SHIFT; + } + + return MB_SHIFT; +} + +static u64 parse_mem_option(const char *nptr, char **next) +{ + u64 shift; + u64 val; + + errno = 0; + val = strtoull(nptr, next, 10); + if (errno == ERANGE) + die("Memory too large: %s", nptr); + if (*next == nptr) + die("Invalid memory specifier: %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 +147,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 +573,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",