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]
Message-ID: <YXhOEEOSG+fgEy+t@yury-ThinkPad>
Date:   Tue, 26 Oct 2021 11:54:16 -0700
From:   Yury Norov <yury.norov@...il.com>
To:     Qian Cai <quic_qiancai@...cinc.com>
Cc:     Andy Shevchenko <andriy.shevchenko@...ux.intel.com>,
        Rasmus Villemoes <linux@...musvillemoes.dk>,
        linux-kernel@...r.kernel.org
Subject: Re: [PATCH] bitmap: simplify GENMASK(size - 1, 0) lines

On Tue, Oct 26, 2021 at 10:41:08AM -0400, Qian Cai wrote:
> Since "size" is an "unsigned int", the rvalue "size - 1" will still be
> "unsigned int" according to the C standard (3.2.1.5 Usual arithmetic
> conversions). Therefore, GENMASK(size - 1, 0) will always return 0UL. Those
> are also caught by GCC (W=2):
> 
> ./include/linux/find.h: In function 'find_first_bit':
> ./include/linux/bits.h:25:22: warning: comparison of unsigned expression in '< 0' is always false [-Wtype-limits]
>    25 |   __is_constexpr((l) > (h)), (l) > (h), 0)))
>       |                      ^
> ./include/linux/build_bug.h:16:62: note: in definition of macro 'BUILD_BUG_ON_ZERO'
>    16 | #define BUILD_BUG_ON_ZERO(e) ((int)(sizeof(struct { int:(-!!(e)); })))
>       |                                                              ^
> ./include/linux/bits.h:25:3: note: in expansion of macro '__is_constexpr'
>    25 |   __is_constexpr((l) > (h)), (l) > (h), 0)))
>       |   ^~~~~~~~~~~~~~
> ./include/linux/bits.h:38:3: note: in expansion of macro 'GENMASK_INPUT_CHECK'
>    38 |  (GENMASK_INPUT_CHECK(h, l) + __GENMASK(h, l))
>       |   ^~~~~~~~~~~~~~~~~~~
> ./include/linux/find.h:119:31: note: in expansion of macro 'GENMASK'
>   119 |   unsigned long val = *addr & GENMASK(size - 1, 0);
>       |                               ^~~~~~~
> ./include/linux/bits.h:25:34: warning: comparison of unsigned expression in '< 0' is always false [-Wtype-limits]
>    25 |   __is_constexpr((l) > (h)), (l) > (h), 0)))
>       |                                  ^
> ./include/linux/build_bug.h:16:62: note: in definition of macro 'BUILD_BUG_ON_ZERO'
>    16 | #define BUILD_BUG_ON_ZERO(e) ((int)(sizeof(struct { int:(-!!(e)); })))
>       |                                                              ^
> ./include/linux/bits.h:38:3: note: in expansion of macro 'GENMASK_INPUT_CHECK'
>    38 |  (GENMASK_INPUT_CHECK(h, l) + __GENMASK(h, l))
>       |   ^~~~~~~~~~~~~~~~~~~
> ./include/linux/find.h:119:31: note: in expansion of macro 'GENMASK'
>   119 |   unsigned long val = *addr & GENMASK(size - 1, 0);
>       |                               ^~~~~~~
> 
> Signed-off-by: Qian Cai <quic_qiancai@...cinc.com>
> ---
>  include/linux/find.h | 28 ++++++++--------------------
>  1 file changed, 8 insertions(+), 20 deletions(-)
> 
> diff --git a/include/linux/find.h b/include/linux/find.h
> index 5bb6db213bcb..5ce2b17aea42 100644
> --- a/include/linux/find.h
> +++ b/include/linux/find.h
> @@ -115,11 +115,8 @@ unsigned long find_next_zero_bit(const unsigned long *addr, unsigned long size,
>  static inline
>  unsigned long find_first_bit(const unsigned long *addr, unsigned long size)
>  {
> -	if (small_const_nbits(size)) {
> -		unsigned long val = *addr & GENMASK(size - 1, 0);
> -
> -		return val ? __ffs(val) : size;
> -	}
> +	if (small_const_nbits(size))
> +		return size;
>  
>  	return _find_first_bit(addr, size);
>  }

[...]

Nice catch! I'm a bit concerned that small_const_nbits() will never
allow GENMASK() to be passed with size == 0, but the patch looks
good to me overall.

It's too late to merge it in 5.15, so I will add it in a spring merge
window - most probably in a bitmap branch for 5.17 or 5.18:

https://github.com/norov/linux/tree/bitmap-2022-Apr-01

Thanks,
Yury

Powered by blists - more mailing lists

Powered by Openwall GNU/*/Linux Powered by OpenVZ