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 X-Spam-Level: X-Spam-Status: No, score=-8.3 required=3.0 tests=HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH,MAILING_LIST_MULTI,SIGNED_OFF_BY,SPF_HELO_NONE,SPF_PASS, USER_AGENT_SANE_2 autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 40E55C5DF61 for ; Thu, 7 Nov 2019 13:55:43 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 1C29F214D8 for ; Thu, 7 Nov 2019 13:55:43 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2388907AbfKGNzm (ORCPT ); Thu, 7 Nov 2019 08:55:42 -0500 Received: from foss.arm.com ([217.140.110.172]:56714 "EHLO foss.arm.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1730980AbfKGNzl (ORCPT ); Thu, 7 Nov 2019 08:55:41 -0500 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 7AB9131B; Thu, 7 Nov 2019 05:55:41 -0800 (PST) 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 7699E3F71A; Thu, 7 Nov 2019 05:55:40 -0800 (PST) Date: Thu, 7 Nov 2019 13:55:37 +0000 From: Andre Przywara To: Alexandru Elisei Cc: kvm@vger.kernel.org, Will Deacon , Julien Thierry , Marc Zyngier , Suzuki Poulose , Julien Grall Subject: Re: [PATCH kvmtool 10/16] kvmtool: Allow standard size specifiers for memory Message-ID: <20191107135537.5786f77c@donnerap.cambridge.arm.com> In-Reply-To: <1569245722-23375-11-git-send-email-alexandru.elisei@arm.com> References: <1569245722-23375-1-git-send-email-alexandru.elisei@arm.com> <1569245722-23375-11-git-send-email-alexandru.elisei@arm.com> Organization: ARM X-Mailer: Claws Mail 3.17.3 (GTK+ 2.24.32; aarch64-unknown-linux-gnu) MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org On Mon, 23 Sep 2019 14:35:16 +0100 Alexandru Elisei wrote: Hi, > From: Suzuki K Poulose > > Allow standard suffixes, K, M, G, T & P suffixes (lowercase and uppercase) > for sizes and addresses for memory bank parameters. By default, the size is > specified in MB. > > Signed-off-by: Suzuki K Poulose > Signed-off-by: Alexandru Elisei > --- > builtin-run.c | 55 +++++++++++++++++++++++++++++++++++++++++++++++++------ > 1 file changed, 49 insertions(+), 6 deletions(-) > > diff --git a/builtin-run.c b/builtin-run.c > index df255cc44078..757ede4ac0d1 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,48 @@ void kvm_run_set_wrapper_sandbox(void) > kvm_run_wrapper = KVM_RUN_SANDBOX; > } > > +static int parse_unit(char **next) > +{ > + int shift = 0; > + > + switch(**next) { > + 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) > + (*next)++; > + > + return shift; > +} > + > +static u64 parse_size(char **next) > +{ > + int shift = parse_unit(next); > + > + /* By default the size is in MB, if none is specified. */ > + if (!shift) > + shift = 20; > + > + while (**next != '\0' && **next != '@') > + (*next)++; Mmh, doesn't that skip over invalid characters, which should be reported as an error? Like "12Three"? Why do we need to skip something here anyway? > + > + return ((u64)1) << shift; Is there any reason we don't just return the shift value back? Seems more efficient to me. > +} > + > +static u64 parse_addr(char **next) > +{ > + int shift = parse_unit(next); > + > + while (**next != '\0') > + (*next)++; Same here, why do we skip characters? Especially since we check for \0 in the caller below. Cheers, Andre > + > + return ((u64)1) << shift; > +} > + > static int mem_parser(const struct option *opt, const char *arg, int unset) > { > struct kvm_config *cfg = opt->value; > @@ -99,15 +143,12 @@ static int mem_parser(const struct option *opt, const char *arg, int unset) > if (next == p) > die("Invalid memory size"); > > - /* The user specifies the memory in MB, we use bytes. */ > - size <<= MB_SHIFT; > + size *= parse_size(&next); > > if (*next == '\0') > goto out; > - else if (*next == '@') > - p = next + 1; > else > - die("Unexpected character after memory size: %c", *next); > + p = next + 1; > > addr = strtoll(p, &next, 0); > if (next == p) > @@ -118,6 +159,8 @@ static int mem_parser(const struct option *opt, const char *arg, int unset) > die("Specifying the memory address not supported by the architecture"); > #endif > > + addr *= parse_addr(&next); > + > out: > cfg->ram_base = addr; > cfg->ram_size = size;