All of lore.kernel.org
 help / color / mirror / Atom feed
From: "Jan Beulich" <JBeulich@suse.com>
To: Andrew Cooper <andrew.cooper3@citrix.com>
Cc: Julien Grall <julien.grall@arm.com>,
	Stefano Stabellini <sstabellini@kernel.org>,
	Wei Liu <wei.liu2@citrix.com>,
	Xen-devel <xen-devel@lists.xen.org>,
	Roger Pau Monne <roger.pau@citrix.com>
Subject: Re: [PATCH] xen/cmdline: Fix buggy strncmp(s, LITERAL, ss - s) construct
Date: Fri, 04 Jan 2019 08:32:29 -0700	[thread overview]
Message-ID: <5C2F7C8D020000780020A422@prv1-mh.provo.novell.com> (raw)
In-Reply-To: <1546277721-20234-1-git-send-email-andrew.cooper3@citrix.com>

>>> On 31.12.18 at 18:35, <andrew.cooper3@citrix.com> wrote:
> --- a/xen/arch/x86/cpu/vpmu.c
> +++ b/xen/arch/x86/cpu/vpmu.c
> @@ -61,42 +61,31 @@ static unsigned vpmu_count;
>  
>  static DEFINE_PER_CPU(struct vcpu *, last_vcpu);
>  
> -static int parse_vpmu_param(const char *s, unsigned int len)
> -{
> -    if ( !*s || !len )
> -        return 0;
> -    if ( !strncmp(s, "bts", len) )
> -        vpmu_features |= XENPMU_FEATURE_INTEL_BTS;
> -    else if ( !strncmp(s, "ipc", len) )
> -        vpmu_features |= XENPMU_FEATURE_IPC_ONLY;
> -    else if ( !strncmp(s, "arch", len) )
> -        vpmu_features |= XENPMU_FEATURE_ARCH_ONLY;
> -    else
> -        return 1;
> -    return 0;
> -}
> -
>  static int __init parse_vpmu_params(const char *s)
>  {
> -    const char *sep, *p = s;
> +    const char *ss;
>  
>      switch ( parse_bool(s, NULL) )
>      {
>      case 0:
>          break;
>      default:
> -        for ( ; ; )
> -        {
> -            sep = strchr(p, ',');
> -            if ( sep == NULL )
> -                sep = strchr(p, 0);
> -            if ( parse_vpmu_param(p, sep - p) )
> -                goto error;
> -            if ( !*sep )
> -                /* reached end of flags */
> -                break;
> -            p = sep + 1;
> -        }
> +        do {
> +            ss = strchr(s, ',');
> +            if ( !ss )
> +                ss = strchr(s, '\0');
> +
> +            if ( !cmdline_strcmp(s, "bts") )
> +                vpmu_features |= XENPMU_FEATURE_INTEL_BTS;
> +            else if ( !cmdline_strcmp(s, "ipc") )
> +                vpmu_features |= XENPMU_FEATURE_IPC_ONLY;
> +            else if ( !cmdline_strcmp(s, "arch") )
> +                vpmu_features |= XENPMU_FEATURE_ARCH_ONLY;
> +            else
> +                return -EINVAL;
> +
> +            s = ss + 1;
> +        } while ( *ss );

While presumably also applicable elsewhere, the issue is more
noticeable here because you introduce "ss" anew: It is now
unhelpful (in terms of generated code) to calculate ss before
the various cmdline_strcmp() calls, as the compiler can't know
(despite the const) that what s points to won't change across
those calls, and hence has to calculate ss early (and put it into
a callee saved register or on the stack), as written. If the
calculation was pulled down, only scratch registers would
suffice for the compiler to carry out the calculation.

That said - all of this is boot time only code, so not really
performance critical. It's just that this general structure will
then further proliferate, and the overall binary size is likely
going to be (slightly) larger this way.

> --- a/xen/common/kernel.c
> +++ b/xen/common/kernel.c
> @@ -227,19 +227,49 @@ int parse_bool(const char *s, const char *e)
>      if ( !len )
>          return -1;
>  
> -    if ( !strncmp("no", s, len) ||
> -         !strncmp("off", s, len) ||
> -         !strncmp("false", s, len) ||
> -         !strncmp("disable", s, len) ||
> -         !strncmp("0", s, len) )
> -        return 0;
> +    switch ( len )
> +    {
> +    case 1:
> +        if ( *s == '1' )
> +            return 1;
> +        else if ( *s == '0' )

The "else" here is pointless (also further down).

> @@ -271,6 +301,29 @@ int parse_boolean(const char *name, const char *s, const char *e)
>      return -1;
>  }
>  
> +int cmdline_strcmp(const char *frag, const char *name)
> +{
> +    while ( 1 )

Could I talk you into using "for ( ; ; )" instead (and then perhaps
moving the two increments up here)? I know gcc doesn't do this,
but in the general case a compiler warning about such constant
conditionals is not an entirely bad or wrong thing, so I prefer to
see such constructs avoided where we reasonably can.

> +    {
> +        int res = (*frag - *name);

With the result of this being implementation defined (due to plain
char's implementation defined - often command line controlled
with an implementation defined default - signedness) I wonder if
this function can really usefully return "int" rather than "bool".

> +        if ( res || *name == '\0' )
> +        {
> +            /*
> +             * NUL in 'name' matching a comma or colon in 'frag' implies
> +             * success.
> +             */
> +            if ( *name == '\0' && (*frag == ',' || *frag == ':') )

There's only a single (unrelated) use of ; as a separator right
now (afaics), but adding it here would seem quite desirable to
me.

Also, speaking of (the lack of) tokenization of the command line
in the caller, wouldn't it make sense to accept white space as
separators here too?

Jan


_______________________________________________
Xen-devel mailing list
Xen-devel@lists.xenproject.org
https://lists.xenproject.org/mailman/listinfo/xen-devel

  parent reply	other threads:[~2019-01-04 15:32 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-12-31 17:35 [PATCH] xen/cmdline: Fix buggy strncmp(s, LITERAL, ss - s) construct Andrew Cooper
2019-01-02 10:13 ` Roger Pau Monné
2019-01-02 12:18   ` Andrew Cooper
2019-01-02 12:25     ` Roger Pau Monné
2019-01-02 12:28       ` Andrew Cooper
2019-01-04 15:32 ` Jan Beulich [this message]
2019-01-04 15:55   ` Andrew Cooper
2019-01-04 16:25     ` Andrew Cooper
2019-01-04 16:47     ` Jan Beulich

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=5C2F7C8D020000780020A422@prv1-mh.provo.novell.com \
    --to=jbeulich@suse.com \
    --cc=andrew.cooper3@citrix.com \
    --cc=julien.grall@arm.com \
    --cc=roger.pau@citrix.com \
    --cc=sstabellini@kernel.org \
    --cc=wei.liu2@citrix.com \
    --cc=xen-devel@lists.xen.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.