lists.openwall.net   lists  /  announce  owl-users  owl-dev  john-users  john-dev  passwdqc-users  yescrypt  popa3d-users  /  oss-security  kernel-hardening  musl  sabotage  tlsify  passwords  /  crypt-dev  xvendor  /  Bugtraq  Full-Disclosure  linux-kernel  linux-netdev  linux-ext4  linux-hardening  linux-cve-announce  PHC 
Open Source and information security mailing list archives
 
Hash Suite: Windows password security audit tool. GUI, reports in PDF.
[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Date:   Tue, 10 May 2022 12:11:27 -0700
From:   Yury Norov <yury.norov@...il.com>
To:     David Laight <David.Laight@...lab.com>
Cc:     'Alexei Starovoitov' <alexei.starovoitov@...il.com>,
        Andy Shevchenko <andriy.shevchenko@...ux.intel.com>,
        Greg Kroah-Hartman <gregkh@...uxfoundation.org>,
        Joe Perches <joe@...ches.com>,
        Julia Lawall <Julia.Lawall@...ia.fr>,
        Michał Mirosław <mirq-linux@...e.qmqm.pl>,
        Nicholas Piggin <npiggin@...il.com>,
        Nicolas Palix <nicolas.palix@...g.fr>,
        Peter Zijlstra <peterz@...radead.org>,
        Rasmus Villemoes <linux@...musvillemoes.dk>,
        Matti Vaittinen <Matti.Vaittinen@...rohmeurope.com>,
        LKML <linux-kernel@...r.kernel.org>,
        Alexei Starovoitov <ast@...nel.org>,
        Andrew Morton <akpm@...ux-foundation.org>,
        Chris Zankel <chris@...kel.net>,
        Christophe Leroy <christophe.leroy@...roup.eu>,
        "Eric W . Biederman" <ebiederm@...ssion.com>,
        Kumar Kartikeya Dwivedi <memxor@...il.com>,
        Max Filippov <jcmvbkbc@...il.com>,
        Toke Høiland-Jørgensen <toke@...hat.com>,
        "linux-xtensa@...ux-xtensa.org" <linux-xtensa@...ux-xtensa.org>
Subject: Re: [PATCH 08/22] bitops: introduce MANY_BITS() macro

On Tue, May 10, 2022 at 05:54:13PM +0000, David Laight wrote:
> From: Alexei Starovoitov
> > Sent: 10 May 2022 17:51
> ...
> > +/* Return: nonzero if 2 or more bits are set */
> > +#define MANY_BITS(n)           ((n) & ((n) - 1))
> 
> You can't have a macro that expands its argument twice.

Yes, I'll fix it.
 
> ...
> > >  static inline __attribute__((const))
> > >  bool is_power_of_2(unsigned long n)
> > >  {
> > > -       return (n != 0 && ((n & (n - 1)) == 0));
> > > +       return n != 0 && !MANY_BITS(n);
> > >  }
> > 
> > Please don't. Open coded version is much easier to read.

To me the human-readable version is easier to read. Still, if you thing
that n & (n - 1) is simpler, what for this function is needed at all?

> Especially if you remove all the spare parenthesis.
> 	return n && !(n & (n - 1));
> 
> I bet a lot of callers know the value is non-zero.
> 
> I suspect you'll find at least one caller that uses
> is_power_of_2() assuming it is !(n & (n - 1)) and
> so is wrong for zero.
 
Another thing is that despite __attribute__(const), gcc sometimes doesn't
recognize it as constant expression, and people have to workaround it.
XTENSA is the example for 1st case, and for the 2nd:

arch/powerpc/mm/init-common.c:
        unsigned long minalign = max(MAX_PGTABLE_INDEX_SIZE + 1,
                                                     HUGEPD_SHIFT_MASK + 1);

        /* It would be nice if this was a BUILD_BUG_ON(), but at the
         * moment, gcc doesn't seem to recognize is_power_of_2
         * as a constant expression, so so much for that. */
        BUG_ON(!is_power_of_2(minalign));

This convinced me that we need a simple macro that is decoupled with
pow_2 semantics and can be used in another macros like BUILD_BUG_ON().

Thanks,
Yury

Powered by blists - more mailing lists