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] [day] [month] [year] [list]
Message-ID: <aT-x7f4EFFEgd1T4@gmail.com>
Date: Mon, 15 Dec 2025 07:59:57 +0100
From: Ingo Molnar <mingo@...nel.org>
To: "Yury Norov (NVIDIA)" <yury.norov@...il.com>
Cc: Ingo Molnar <mingo@...hat.com>, Thomas Gleixner <tglx@...utronix.de>,
	Borislav Petkov <bp@...en8.de>,
	Dave Hansen <dave.hansen@...ux.intel.com>, x86@...nel.org,
	"H. Peter Anvin" <hpa@...or.com>,
	Rasmus Villemoes <linux@...musvillemoes.dk>,
	Andrew Morton <akpm@...ux-foundation.org>,
	"Peter Zijlstra (Intel)" <peterz@...radead.org>,
	Ilpo Järvinen <ilpo.jarvinen@...ux.intel.com>,
	Tony Luck <tony.luck@...el.com>, "Xin Li (Intel)" <xin@...or.com>,
	"Chang S. Bae" <chang.seok.bae@...el.com>,
	linux-kernel@...r.kernel.org
Subject: Re: [PATCH 1/2] bitmap: add bitmap_weight_from()


* Yury Norov (NVIDIA) <yury.norov@...il.com> wrote:

> The function calculates a Hamming weight of a bitmap starting from an
> arbitrary bit.
> 
> Signed-off-by: Yury Norov (NVIDIA) <yury.norov@...il.com>
> ---
>  include/linux/bitmap.h | 25 +++++++++++++++++++++++++
>  lib/bitmap.c           | 28 ++++++++++++++++++++++++++++
>  lib/test_bitmap.c      | 29 +++++++++++++++++++++++++++++
>  3 files changed, 82 insertions(+)
> 
> diff --git a/include/linux/bitmap.h b/include/linux/bitmap.h
> index b0395e4ccf90..0f4789e1f7cb 100644
> --- a/include/linux/bitmap.h
> +++ b/include/linux/bitmap.h
> @@ -57,6 +57,7 @@ struct device;
>   *  bitmap_weight(src, nbits)                   Hamming Weight: number set bits
>   *  bitmap_weight_and(src1, src2, nbits)        Hamming Weight of and'ed bitmap
>   *  bitmap_weight_andnot(src1, src2, nbits)     Hamming Weight of andnot'ed bitmap
> + *  bitmap_weight_from(src, start, nbits)       Hamming Weight starting from @start
>   *  bitmap_set(dst, pos, nbits)                 Set specified bit area
>   *  bitmap_clear(dst, pos, nbits)               Clear specified bit area
>   *  bitmap_find_next_zero_area(buf, len, pos, n, mask)  Find bit free area
> @@ -184,6 +185,8 @@ unsigned int __bitmap_weight_and(const unsigned long *bitmap1,
>  				 const unsigned long *bitmap2, unsigned int nbits);
>  unsigned int __bitmap_weight_andnot(const unsigned long *bitmap1,
>  				    const unsigned long *bitmap2, unsigned int nbits);
> +unsigned long __bitmap_weight_from(const unsigned long *bitmap,
> +				   unsigned int start, unsigned int nbits);
>  void __bitmap_set(unsigned long *map, unsigned int start, int len);
>  void __bitmap_clear(unsigned long *map, unsigned int start, int len);
>  
> @@ -479,6 +482,28 @@ unsigned long bitmap_weight_andnot(const unsigned long *src1,
>  	return __bitmap_weight_andnot(src1, src2, nbits);
>  }
>  
> +/**
> + * bitmap_weight_from - Hamming weight for a memory region
> + * @bitmap: The base address
> + * @start: The bitnumber to starts weighting
> + * @nbits: the bitmap size in bits
> + *
> + * Returns the number of set bits in the region, or > @nbits in case of error.
> + */
> +static __always_inline
> +unsigned long bitmap_weight_from(const unsigned long *bitmap,
> +				   unsigned int start, unsigned int nbits)
> +{
> +	if (small_const_nbits(nbits)) {
> +		if (unlikely(start >= nbits))
> +			return nbits + 1;
> +
> +		return hweight_long(*bitmap & GENMASK(nbits - 1, start));
> +	}
> +
> +	return __bitmap_weight_from(bitmap, start, nbits);

The 'nbits' name and description is actively misleading: it suggests
the number of bits searched, like bitmap_write() has nbits for
the number of bits written, but in reality it's the *end* index,
not the size of the area to search.

Note how it contrasts with how bitmap_write(..,start,nbits)
works.

So please rename it to something more suitable, like 'end', or so.

Thanks,

	Ingo 

Powered by blists - more mailing lists

Powered by Openwall GNU/*/Linux Powered by OpenVZ