linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Joe Perches <joe@perches.com>
To: Ard Biesheuvel <ard.biesheuvel@linaro.org>
Cc: Laura Abbott <labbott@redhat.com>,
	Will Deacon <will.deacon@arm.com>,
	Linus Torvalds <torvalds@linux-foundation.org>,
	Markus Trippelsdorf <markus@trippelsdorf.de>,
	"linux-arm-kernel@lists.infradead.org" 
	<linux-arm-kernel@lists.infradead.org>,
	Linux Kernel Mailing List <linux-kernel@vger.kernel.org>,
	Peter Zijlstra <peterz@infradead.org>,
	Ingo Molnar <mingo@kernel.org>,
	james.greenhalgh@arm.com,
	Gregory Clement <gregory.clement@free-electrons.com>,
	Stephen Boyd <sboyd@codeaurora.org>
Subject: Re: Build failure with v4.9-rc1 and GCC trunk -- compiler weirdness
Date: Wed, 01 Feb 2017 11:49:47 -0800	[thread overview]
Message-ID: <1485978587.2560.15.camel@perches.com> (raw)
In-Reply-To: <CAKv+Gu-Vc-+BDGAWOLJ79KqjL7ZW5vxUk=VW8MqEJyjO2fM6NQ@mail.gmail.com>

On Wed, 2017-02-01 at 19:31 +0000, Ard Biesheuvel wrote:
> On 1 February 2017 at 19:04, Joe Perches <joe@perches.com> wrote:
> > On Wed, 2017-02-01 at 18:19 +0000, Ard Biesheuvel wrote:
> > > On 1 February 2017 at 17:36, Ard Biesheuvel <ard.biesheuvel@linaro.org> wrote:
> > > > I still think order_base_2() is broken, since it may invoke
> > > > roundup_pow_of_two() with an input value that is documented as
> > > > producing undefined output. I would argue that the below is the
> > > > correct fix.
> > > > 
> > > > diff --git a/include/linux/log2.h b/include/linux/log2.h
> > > > index fd7ff3d91e6a..46523731bec0 100644
> > > > --- a/include/linux/log2.h
> > > > +++ b/include/linux/log2.h
> > > > @@ -203,6 +203,18 @@ unsigned long __rounddown_pow_of_two(unsigned long n)
> > > >   *  ... and so on.
> > > >   */
> > > > 
> > > > -#define order_base_2(n) ilog2(roundup_pow_of_two(n))
> > > > +static inline __attribute__((__const__))
> > > > +unsigned long __order_base_2(unsigned long n)
> > > > +{
> > > > +       return n ? 1UL << fls_long(n - 1) : 1;
> > > > +}
> > > > +
> > > > +#define order_base_2(n)                                \
> > > > +(                                              \
> > > > +       __builtin_constant_p(n) ? (             \
> > > > +               ((n) < 2) ? (n) :               \
> > > > +               ilog2((n) - 1) + 1) :           \
> > > > +       ilog2(__order_base_2(n))                \
> > > > + )
> > > > 
> > > >  #endif /* _LINUX_LOG2_H */
> > > 
> > > Actually, there is a still a redundant shift/fls() in there, this is
> > > even simpler:
> > > 
> > > diff --git a/include/linux/log2.h b/include/linux/log2.h
> > > index fd7ff3d91e6a..4741534bd7af 100644
> > > --- a/include/linux/log2.h
> > > +++ b/include/linux/log2.h
> > > @@ -203,6 +203,18 @@ unsigned long __rounddown_pow_of_two(unsigned long n)
> > >   *  ... and so on.
> > >   */
> > > 
> > > -#define order_base_2(n) ilog2(roundup_pow_of_two(n))
> > > +static inline __attribute__((__const__))
> > 
> > commonly __attribute_const__
> > 
> 
> ... except in <linux/ilog2.h>, which probably predates that.
> 
> > > +unsigned long __order_base_2(unsigned long n)
> > > +{
> > > +       return n > 1 ? ilog2(n - 1) + 1 : 0;
> > > +}
> > > +
> > > +#define order_base_2(n)                                \
> > > +(                                              \
> > > +       __builtin_constant_p(n) ? (             \
> > > +               ((n) < 2) ? (n) :               \
> > > +               ilog2((n) - 1) + 1) :           \
> > > +       __order_base_2(n)                       \
> > > + )
> > 
> > Does this work properly when n is a signed negative value?
> > 
> 
> No, but order_base_2() is explicitly only defined for inputs [0, ->),

where?

> so its behavior for negative inputs is best left undefined.

Or maybe add a BUILD_BUG_ON something like:

#define order_base_2(n)							\
({									\
	typeof(n) _n = n;						\
	BUILD_BUG_ON(__builtin_constant_p(_n) && _n < 0);		\
	__builtin_constant_p(_n) ? (_n < 2 ? _n : ilog2((_n) - 1) + 1))	\
				 : __order_base_2(_n);			\
})

  reply	other threads:[~2017-02-01 19:49 UTC|newest]

Thread overview: 30+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-10-17 18:38 Build failure with v4.9-rc1 and GCC trunk -- compiler weirdness Will Deacon
2016-10-17 19:43 ` Ard Biesheuvel
2016-10-19 13:35   ` Will Deacon
2016-10-19 14:59     ` Ard Biesheuvel
2016-10-19 15:01       ` Ard Biesheuvel
2016-10-19 15:11         ` Arnd Bergmann
2016-10-19 15:27           ` Ard Biesheuvel
2016-10-19 15:44             ` Arnd Bergmann
2016-10-19 15:32           ` Gregory CLEMENT
2016-10-19 15:58     ` Russell King - ARM Linux
2016-10-19 15:37 ` Markus Trippelsdorf
2016-10-19 15:55   ` Linus Torvalds
2016-10-19 15:56     ` Markus Trippelsdorf
2016-10-19 16:00       ` Ard Biesheuvel
2016-10-19 16:01       ` Linus Torvalds
2016-10-19 16:22         ` Will Deacon
2017-02-01 16:58           ` Laura Abbott
2017-02-01 17:36             ` Ard Biesheuvel
2017-02-01 18:19               ` Ard Biesheuvel
2017-02-01 19:04                 ` Joe Perches
2017-02-01 19:31                   ` Ard Biesheuvel
2017-02-01 19:49                     ` Joe Perches [this message]
2017-02-01 19:53                       ` Ard Biesheuvel
2017-02-01 20:34                         ` Joe Perches
2017-02-01 21:11                           ` Ard Biesheuvel
2017-02-02  9:02                   ` Peter Zijlstra
2017-02-01 21:50               ` Laura Abbott
2017-02-02  9:17                 ` Ard Biesheuvel
2017-02-02 15:43                   ` Laura Abbott
2017-02-02 15:45                     ` Ard Biesheuvel

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=1485978587.2560.15.camel@perches.com \
    --to=joe@perches.com \
    --cc=ard.biesheuvel@linaro.org \
    --cc=gregory.clement@free-electrons.com \
    --cc=james.greenhalgh@arm.com \
    --cc=labbott@redhat.com \
    --cc=linux-arm-kernel@lists.infradead.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=markus@trippelsdorf.de \
    --cc=mingo@kernel.org \
    --cc=peterz@infradead.org \
    --cc=sboyd@codeaurora.org \
    --cc=torvalds@linux-foundation.org \
    --cc=will.deacon@arm.com \
    /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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).