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 09:50:40 -0700
From:   Alexei Starovoitov <alexei.starovoitov@...il.com>
To:     Yury Norov <yury.norov@...il.com>
Cc:     Andy Shevchenko <andriy.shevchenko@...ux.intel.com>,
        David Laight <David.Laight@...lab.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
Subject: Re: [PATCH 08/22] bitops: introduce MANY_BITS() macro

On Tue, May 10, 2022 at 8:48 AM Yury Norov <yury.norov@...il.com> wrote:
>
> arch/xtensa/kernel/traps.c and include/linux/log2.h define very similar
> functions with different behaviour. XTENSA defines IS_POW2(), and
> log2.h defines is_power_of_2(). The difference is that IS_POW2()
> considers 0 as power of 2, while is_power_of_2() - does not.
>
> This discrepancy may confuse reader. From mathematical point of view,
> 0 is not a power of 2. So let's introduce macro MANY_BITS(), which
> returns true if 2 or more bits are set in a number (which is what
> XTENSA actually needs), and use it in is_power_of_2().
>
> CC: Alexei Starovoitov <ast@...nel.org>
> CC: Andrew Morton <akpm@...ux-foundation.org>
> CC: Chris Zankel <chris@...kel.net>
> CC: Christophe Leroy <christophe.leroy@...roup.eu>
> CC: Eric W. Biederman <ebiederm@...ssion.com>
> CC: Kumar Kartikeya Dwivedi <memxor@...il.com>
> CC: Max Filippov <jcmvbkbc@...il.com>
> CC: Toke Høiland-Jørgensen <toke@...hat.com>
> CC: linux-xtensa@...ux-xtensa.org
> CC: linux-kernel@...r.kernel.org
> Signed-off-by: Yury Norov <yury.norov@...il.com>
> ---
>  arch/xtensa/kernel/traps.c | 5 +----
>  include/linux/bitops.h     | 3 +++
>  include/linux/log2.h       | 2 +-
>  3 files changed, 5 insertions(+), 5 deletions(-)
>
> diff --git a/arch/xtensa/kernel/traps.c b/arch/xtensa/kernel/traps.c
> index 138a86fbe9d7..040ec38bfce2 100644
> --- a/arch/xtensa/kernel/traps.c
> +++ b/arch/xtensa/kernel/traps.c
> @@ -203,10 +203,7 @@ static void do_multihit(struct pt_regs *regs)
>
>  #if XTENSA_FAKE_NMI
>
> -#define IS_POW2(v) (((v) & ((v) - 1)) == 0)
> -
> -#if !(PROFILING_INTLEVEL == XCHAL_EXCM_LEVEL && \
> -      IS_POW2(XTENSA_INTLEVEL_MASK(PROFILING_INTLEVEL)))
> +#if (MANY_BITS(XTENSA_INTLEVEL_MASK(PROFILING_INTLEVEL)) || PROFILING_INTLEVEL != XCHAL_EXCM_LEVEL)
>  #warning "Fake NMI is requested for PMM, but there are other IRQs at or above its level."
>  #warning "Fake NMI will be used, but there will be a bugcheck if one of those IRQs fire."
>
> diff --git a/include/linux/bitops.h b/include/linux/bitops.h
> index 7aaed501f768..96bc6a2552d6 100644
> --- a/include/linux/bitops.h
> +++ b/include/linux/bitops.h
> @@ -21,6 +21,9 @@
>  #define BITS_TO_U32(nr)                __KERNEL_DIV_ROUND_UP(nr, BITS_PER_TYPE(u32))
>  #define BITS_TO_BYTES(nr)      __KERNEL_DIV_ROUND_UP(nr, BITS_PER_TYPE(char))
>
> +/* Return: nonzero if 2 or more bits are set */
> +#define MANY_BITS(n)           ((n) & ((n) - 1))
> +
>  extern unsigned int __sw_hweight8(unsigned int w);
>  extern unsigned int __sw_hweight16(unsigned int w);
>  extern unsigned int __sw_hweight32(unsigned int w);
> diff --git a/include/linux/log2.h b/include/linux/log2.h
> index 9f30d087a128..335b9dbd302d 100644
> --- a/include/linux/log2.h
> +++ b/include/linux/log2.h
> @@ -44,7 +44,7 @@ int __ilog2_u64(u64 n)
>  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.

Powered by blists - more mailing lists