From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 7487231CA4A for ; Mon, 15 Dec 2025 07:00:03 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1765782005; cv=none; b=C8z4IkL+pymSdRiGR0rMK6x1bbkYXNWMYTbJaOMtu9KrN/J9zIq9JYon0xjDHhSCyhXdoYXuqbSGqZgkSoNTgOkwP8OwB+2/svRzq42gKYL/4treZtCKFHbf7Xy1XKPGGKnj06tovVuDeYJDOFCBvY8ekpyvi4JHMnKva+7ZBO0= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1765782005; c=relaxed/simple; bh=e4404xLHPVU4gbq2WGDmEakngwUyg43t/93fVUfnYIY=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=gBIoGN2zkwJUdTLQ5blOsVwoTKaQO3Z4HiTbb5hdtj0WD6MqTcIVJgBLknH62uoXHOth2OLLf3PC0ou4ogPiG8rKZgCfBd44u6hwy7fGnyV1/PHJ/n3QMGrygPACP5hpX3GKmR80LTskxkzlcZLjtEJnu/9dFv2fRgt3ILkOcDA= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=KFYxUrA4; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="KFYxUrA4" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 39963C4CEF5; Mon, 15 Dec 2025 07:00:00 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1765782003; bh=e4404xLHPVU4gbq2WGDmEakngwUyg43t/93fVUfnYIY=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=KFYxUrA4yHt3t4B1Ll2Af5DP0UZ98ZXko9dF2zgaZX1faib2hszImJYn1hVik4aLW J6P7tpAVBpJ1/fPJzOLJUdi7u9vWmz5/mixCI8Z6q6SxUjy04N+HSEeQsShFgoWuQ+ hPlikWo0gMShNa0gfxAOMYXfgldoRf6hPM9hQLYwHRWa1AgTDFAr/oJw142vO81fGa 4xpQEu58bwYE0dA6hRvWTV/OGd83nX/SrZnEaHRRzUgC4rwczCqNbxO/hu4EtZN0cI 6vTzTiyAEm8b6IysjDGgAoZqL5sPQeu2A/V7VviLbOJtG6DDRgj2nRywA3+Wz8q4VA NrEKH9G7+Ioiw== Date: Mon, 15 Dec 2025 07:59:57 +0100 From: Ingo Molnar To: "Yury Norov (NVIDIA)" Cc: Ingo Molnar , Thomas Gleixner , Borislav Petkov , Dave Hansen , x86@kernel.org, "H. Peter Anvin" , Rasmus Villemoes , Andrew Morton , "Peter Zijlstra (Intel)" , Ilpo =?iso-8859-1?Q?J=E4rvinen?= , Tony Luck , "Xin Li (Intel)" , "Chang S. Bae" , linux-kernel@vger.kernel.org Subject: Re: [PATCH 1/2] bitmap: add bitmap_weight_from() Message-ID: References: <20251214235437.244125-1-yury.norov@gmail.com> <20251214235437.244125-2-yury.norov@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20251214235437.244125-2-yury.norov@gmail.com> * Yury Norov (NVIDIA) wrote: > The function calculates a Hamming weight of a bitmap starting from an > arbitrary bit. > > Signed-off-by: Yury Norov (NVIDIA) > --- > 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