From mboxrd@z Thu Jan 1 00:00:00 1970 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 325CFC433EF for ; Wed, 1 Jun 2022 13:42:09 +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:Subject:Cc:To:From:Date:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=5xwbe2LEY4jSnH5FwdTMoV7uTQc+pPIO7yoTcZTTppE=; b=G3B6O8oilzEs3V NBMpEF8/q0a19Xu+lNVQD0J8VRxT4sGD7mV8Kv0tq+kxPPltNnbOqIq1edh/nfuRXqVN8WiEaitfb kWXIfFRNvpChRXqFeIJDIH13NAjVn7xtCAHHDC+EAHpVqNDwWov9sqwNrhNii2fkgKsgbs/rpCC/W 6RLVZSTsDPi5er3sefF2IzLPZdeFupmELtqoEWrVJWETucrJOhCnxMhH4aCvtLKemd/Wb/ZCK1bZ8 W/x24kIWgnURRRitzbK0/qZHfz4AgpaKuVJLK8GcuWnSUIw/kclowHV952MDCM6O2QGLc1Tg//AT/ LctNza8QrKzRpK6y9GUw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1nwOal-00GJyX-F8; Wed, 01 Jun 2022 13:40:55 +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 1nwOaP-00GJsM-Gk for linux-arm-kernel@lists.infradead.org; Wed, 01 Jun 2022 13:40:35 +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 956B51515; Wed, 1 Jun 2022 06:40:30 -0700 (PDT) Received: from donnerap.cambridge.arm.com (usa-sjc-imap-foss1.foss.arm.com [10.121.207.14]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 548463F73D; Wed, 1 Jun 2022 06:40:29 -0700 (PDT) Date: Wed, 1 Jun 2022 14:39:55 +0100 From: Andre Przywara To: Alexandru Elisei Cc: 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: Re: [PATCH v3 kvmtool 09/13] builtin_run: Allow standard size specifiers for memory Message-ID: <20220601143955.55e00cfd@donnerap.cambridge.arm.com> In-Reply-To: <20220525112345.121321-10-alexandru.elisei@arm.com> References: <20220525112345.121321-1-alexandru.elisei@arm.com> <20220525112345.121321-10-alexandru.elisei@arm.com> Organization: ARM X-Mailer: Claws Mail 3.18.0 (GTK+ 2.24.32; aarch64-unknown-linux-gnu) MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20220601_064033_695896_7FE8E639 X-CRM114-Status: GOOD ( 28.15 ) 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: , Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org On Wed, 25 May 2022 12:23:41 +0100 Alexandru Elisei wrote: Hi, > 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 2ef159cdb2a3..a49698d5b2fe 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) Can we lose the parentheses? > > __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; Doesn't that look better inside the switch/case? default: return 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); strtoull does not clear errno if it succeeds, so it retains the previous error value. So we would need to set errno to 0 just before calling strtoull. > + 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); Does 0 hold any significant value (anymore)? I think we die() if we encounter invalid values in parse_mem_option()? Cheers, Andre > + > + 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", _______________________________________________ linux-arm-kernel mailing list linux-arm-kernel@lists.infradead.org http://lists.infradead.org/mailman/listinfo/linux-arm-kernel