From a676643709115816d3ce7e50aa5b5a4af1ee6c45 Mon Sep 17 00:00:00 2001 From: Yury Norov Date: Wed, 25 Feb 2026 18:58:32 -0500 Subject: bitmap: drop __find_nth_andnot_bit() Remove find_nth_andnot_bit() leftovers. CC: Rasmus Villemoes CC: Andrew Morton CC: Shaopeng Tan Fixes: b0c85e99458a ("cpumask: Remove unnecessary cpumask_nth_andnot()") Signed-off-by: Yury Norov --- include/linux/find.h | 2 -- 1 file changed, 2 deletions(-) (limited to 'include/linux') diff --git a/include/linux/find.h b/include/linux/find.h index 9d720ad92bc1..6c2be8ca615d 100644 --- a/include/linux/find.h +++ b/include/linux/find.h @@ -22,8 +22,6 @@ extern unsigned long _find_first_bit(const unsigned long *addr, unsigned long si unsigned long __find_nth_bit(const unsigned long *addr, unsigned long size, unsigned long n); unsigned long __find_nth_and_bit(const unsigned long *addr1, const unsigned long *addr2, unsigned long size, unsigned long n); -unsigned long __find_nth_andnot_bit(const unsigned long *addr1, const unsigned long *addr2, - unsigned long size, unsigned long n); unsigned long __find_nth_and_andnot_bit(const unsigned long *addr1, const unsigned long *addr2, const unsigned long *addr3, unsigned long size, unsigned long n); -- cgit v1.2.3 From bf31ddc14f8c6bcd4987c31fe2bc9e93e433b41a Mon Sep 17 00:00:00 2001 From: Yury Norov Date: Mon, 22 Dec 2025 14:11:37 -0500 Subject: bitmap: add bitmap_weight_from() The function calculates a Hamming weight of a bitmap starting from an arbitrary bit. Signed-off-by: Yury Norov --- include/linux/bitmap.h | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) (limited to 'include/linux') diff --git a/include/linux/bitmap.h b/include/linux/bitmap.h index b0395e4ccf90..9c0d1de44350 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, end) 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 @@ -479,6 +480,38 @@ 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 + * @end: the bitmap size in bits + * + * Returns the number of set bits in the region. If @start >= @end, + * return >= end. + */ +static __always_inline +unsigned long bitmap_weight_from(const unsigned long *bitmap, + unsigned int start, unsigned int end) +{ + unsigned long w; + + if (unlikely(start >= end)) + return end; + + if (small_const_nbits(end)) + return hweight_long(*bitmap & GENMASK(end - 1, start)); + + bitmap += start / BITS_PER_LONG; + /* Opencode round_down() to not include math.h */ + end -= start & ~(BITS_PER_LONG - 1); + start %= BITS_PER_LONG; + w = bitmap_weight(bitmap, end); + if (start) + w -= hweight_long(*bitmap & BITMAP_LAST_WORD_MASK(start)); + + return w; +} + static __always_inline void bitmap_set(unsigned long *map, unsigned int start, unsigned int nbits) { -- cgit v1.2.3 From 18c48899653fa7a04120537c228031b5c7e4e9d6 Mon Sep 17 00:00:00 2001 From: Yury Norov Date: Tue, 10 Mar 2026 16:53:02 -0400 Subject: lib: crypto: fix comments for count_leading_zeros() count_leading_zeros() is based on fls(), which is defined for x == 0, contrary to __ffs() family. The comment in crypto/mpi erroneously states that the function may return undef in such case. Fix the comment together with the outdated function signature, and now that COUNT_LEADING_ZEROS_0 is not referenced in the codebase, get rid of it too. Reviewed-by: Andy Shevchenko Acked-by: Eric Biggers Signed-off-by: Yury Norov --- include/linux/count_zeros.h | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) (limited to 'include/linux') diff --git a/include/linux/count_zeros.h b/include/linux/count_zeros.h index 5b8ff5ac660d..4e5680327ece 100644 --- a/include/linux/count_zeros.h +++ b/include/linux/count_zeros.h @@ -18,7 +18,7 @@ * * If the MSB of @x is set, the result is 0. * If only the LSB of @x is set, then the result is BITS_PER_LONG-1. - * If @x is 0 then the result is COUNT_LEADING_ZEROS_0. + * If @x is 0 then the result is BITS_PER_LONG. */ static inline int count_leading_zeros(unsigned long x) { @@ -28,8 +28,6 @@ static inline int count_leading_zeros(unsigned long x) return BITS_PER_LONG - fls64(x); } -#define COUNT_LEADING_ZEROS_0 BITS_PER_LONG - /** * count_trailing_zeros - Count the number of zeros from the LSB forwards * @x: The value -- cgit v1.2.3 From 4e64c91b813f666dffc3962a815a8a50b7d6f468 Mon Sep 17 00:00:00 2001 From: Yury Norov Date: Thu, 12 Mar 2026 19:08:16 -0400 Subject: lib: count_zeros: fix 32/64-bit inconsistency in count_trailing_zeros() Based on 'sizeof(x) == 4' condition, in 32-bit case the function is wired to ffs(), while in 64-bit case to __ffs(). The difference is substantial: ffs(x) == __ffs(x) + 1. Also, ffs(0) == 0, while __ffs(0) is undefined. The 32-bit behaviour is inconsistent with the function description, so it needs to get fixed. There are 9 individual users for the function in 6 different subsystems. Some arches and drivers are 64-bit only: - arch/loongarch/kvm/intc/eiointc.c; - drivers/hv/mshv_vtl_main.c; - kernel/liveupdate/kexec_handover.c; The others are: - ib_umem_find_best_pgsz(): as per comment, __ffs() should be correct; - rzv2m_csi_reg_write_bit(): ARCH_RENESAS only, unclear; - lz77_match_len(): CIFS_COMPRESSION only, unclear, experimental; IB and CIFS are explicitly OK with the change. The attached patch gets rid of 32-bit explicit support, so that both 32- and 64-bit versions rely on __ffs(). CC: K. Y. Srinivasan CC: Haiyang Zhang CC: Mark Brown CC: Steve French CC: Alexander Graf CC: Mike Rapoport CC: Pasha Tatashin Acked-by: Leon Romanovsky Signed-off-by: Yury Norov --- include/linux/count_zeros.h | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) (limited to 'include/linux') diff --git a/include/linux/count_zeros.h b/include/linux/count_zeros.h index 4e5680327ece..5034a30b5c7c 100644 --- a/include/linux/count_zeros.h +++ b/include/linux/count_zeros.h @@ -10,6 +10,8 @@ #include +#define COUNT_TRAILING_ZEROS_0 (-1) + /** * count_leading_zeros - Count the number of zeros from the MSB back * @x: The value @@ -40,12 +42,7 @@ static inline int count_leading_zeros(unsigned long x) */ static inline int count_trailing_zeros(unsigned long x) { -#define COUNT_TRAILING_ZEROS_0 (-1) - - if (sizeof(x) == 4) - return ffs(x); - else - return (x != 0) ? __ffs(x) : COUNT_TRAILING_ZEROS_0; + return (x != 0) ? __ffs(x) : COUNT_TRAILING_ZEROS_0; } #endif /* _LINUX_BITOPS_COUNT_ZEROS_H_ */ -- cgit v1.2.3 From be56db15fcce8bb8bd85f236382276d52ce73d08 Mon Sep 17 00:00:00 2001 From: Yury Norov Date: Mon, 23 Mar 2026 12:17:12 -0400 Subject: lib: count_zeros: unify count_{leading,trailing}_zeros() The 'leading' helper returns BITS_PER_LONG if x == 0, while 'trailing' one returns COUNT_TRAILING_ZEROS_0, which turns to be -1. None of the current users explicitly check the returned value for COUNT_TRAILING_ZEROS_0, except the loongarch, which tests implicitly for the '>= 0'. So, align count_trailing_zeros() with the count_leading_zeros(), and simplify the loongarch handling. Reviewed-by: Andy Shevchenko Signed-off-by: Yury Norov --- include/linux/count_zeros.h | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) (limited to 'include/linux') diff --git a/include/linux/count_zeros.h b/include/linux/count_zeros.h index 5034a30b5c7c..b72ba3faa036 100644 --- a/include/linux/count_zeros.h +++ b/include/linux/count_zeros.h @@ -10,8 +10,6 @@ #include -#define COUNT_TRAILING_ZEROS_0 (-1) - /** * count_leading_zeros - Count the number of zeros from the MSB back * @x: The value @@ -38,11 +36,11 @@ static inline int count_leading_zeros(unsigned long x) * * If the LSB of @x is set, the result is 0. * If only the MSB of @x is set, then the result is BITS_PER_LONG-1. - * If @x is 0 then the result is COUNT_TRAILING_ZEROS_0. + * If @x is 0 then the result is BITS_PER_LONG. */ static inline int count_trailing_zeros(unsigned long x) { - return (x != 0) ? __ffs(x) : COUNT_TRAILING_ZEROS_0; + return x ? __ffs(x) : BITS_PER_LONG; } #endif /* _LINUX_BITOPS_COUNT_ZEROS_H_ */ -- cgit v1.2.3 From 7b52b262f8a8cd96dac33721389a884420c18365 Mon Sep 17 00:00:00 2001 From: Kit Dallege Date: Sun, 15 Mar 2026 16:34:14 +0100 Subject: bitops: fix kernel-doc parameter name for parity8() The kernel-doc comment for parity8() documents the parameter as @value but the actual parameter name is @val. Fix the mismatch. Assisted-by: Claude Signed-off-by: Kit Dallege Signed-off-by: Yury Norov --- include/linux/bitops.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'include/linux') diff --git a/include/linux/bitops.h b/include/linux/bitops.h index ea7898cc5903..1fe46703792f 100644 --- a/include/linux/bitops.h +++ b/include/linux/bitops.h @@ -230,7 +230,7 @@ static inline int get_count_order_long(unsigned long l) /** * parity8 - get the parity of an u8 value - * @value: the value to be examined + * @val: the value to be examined * * Determine the parity of the u8 argument. * -- cgit v1.2.3 From d57e74f10461b80c77d1678f646720f616fb8553 Mon Sep 17 00:00:00 2001 From: Yury Norov Date: Sun, 1 Mar 2026 20:11:55 -0500 Subject: bitmap: introduce bitmap_weighted_xor() The function helps to XOR bitmaps and calculate Hamming weight of the result in one pass. Reviewed-by: Aleksandr Loktionov Reviewed-by: Jacob Keller Signed-off-by: Yury Norov --- include/linux/bitmap.h | 15 +++++++++++++++ 1 file changed, 15 insertions(+) (limited to 'include/linux') diff --git a/include/linux/bitmap.h b/include/linux/bitmap.h index 9c0d1de44350..b007d54a9036 100644 --- a/include/linux/bitmap.h +++ b/include/linux/bitmap.h @@ -46,6 +46,7 @@ struct device; * bitmap_and(dst, src1, src2, nbits) *dst = *src1 & *src2 * bitmap_or(dst, src1, src2, nbits) *dst = *src1 | *src2 * bitmap_weighted_or(dst, src1, src2, nbits) *dst = *src1 | *src2. Returns Hamming Weight of dst + * bitmap_weighted_xor(dst, src1, src2, nbits) *dst = *src1 ^ *src2. Returns Hamming Weight of dst * bitmap_xor(dst, src1, src2, nbits) *dst = *src1 ^ *src2 * bitmap_andnot(dst, src1, src2, nbits) *dst = *src1 & ~(*src2) * bitmap_complement(dst, src, nbits) *dst = ~(*src) @@ -169,6 +170,8 @@ void __bitmap_or(unsigned long *dst, const unsigned long *bitmap1, const unsigned long *bitmap2, unsigned int nbits); unsigned int __bitmap_weighted_or(unsigned long *dst, const unsigned long *bitmap1, const unsigned long *bitmap2, unsigned int nbits); +unsigned int __bitmap_weighted_xor(unsigned long *dst, const unsigned long *bitmap1, + const unsigned long *bitmap2, unsigned int nbits); void __bitmap_xor(unsigned long *dst, const unsigned long *bitmap1, const unsigned long *bitmap2, unsigned int nbits); bool __bitmap_andnot(unsigned long *dst, const unsigned long *bitmap1, @@ -353,6 +356,18 @@ unsigned int bitmap_weighted_or(unsigned long *dst, const unsigned long *src1, } } +static __always_inline +unsigned int bitmap_weighted_xor(unsigned long *dst, const unsigned long *src1, + const unsigned long *src2, unsigned int nbits) +{ + if (small_const_nbits(nbits)) { + *dst = *src1 ^ *src2; + return hweight_long(*dst & BITMAP_LAST_WORD_MASK(nbits)); + } else { + return __bitmap_weighted_xor(dst, src1, src2, nbits); + } +} + static __always_inline void bitmap_xor(unsigned long *dst, const unsigned long *src1, const unsigned long *src2, unsigned int nbits) -- cgit v1.2.3 From 592a22338e5acfcd10983699cae8ea02ecd42935 Mon Sep 17 00:00:00 2001 From: Andy Shevchenko Date: Tue, 7 Apr 2026 17:14:31 +0200 Subject: bitops: Update kernel-doc for sign_extendXX() The sign_extendXX() lack of Return section and have other style issues. Address that by updating kernel-doc accordingly. Signed-off-by: Andy Shevchenko Signed-off-by: Yury Norov --- include/linux/bitops.h | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) (limited to 'include/linux') diff --git a/include/linux/bitops.h b/include/linux/bitops.h index 1fe46703792f..657eab2725ce 100644 --- a/include/linux/bitops.h +++ b/include/linux/bitops.h @@ -179,9 +179,11 @@ static inline __u8 ror8(__u8 word, unsigned int shift) /** * sign_extend32 - sign extend a 32-bit value using specified bit as sign-bit * @value: value to sign extend - * @index: 0 based bit index (0<=index<32) to sign bit + * @index: 0 based bit index (0 <= index < 32) to sign bit * * This is safe to use for 16- and 8-bit types as well. + * + * Return: 32-bit sign extended value */ static __always_inline __s32 sign_extend32(__u32 value, int index) { @@ -192,7 +194,11 @@ static __always_inline __s32 sign_extend32(__u32 value, int index) /** * sign_extend64 - sign extend a 64-bit value using specified bit as sign-bit * @value: value to sign extend - * @index: 0 based bit index (0<=index<64) to sign bit + * @index: 0 based bit index (0 <= index < 64) to sign bit + * + * This is safe to use for 32-, 16- and 8-bit types as well. + * + * Return: 64-bit sign extended value */ static __always_inline __s64 sign_extend64(__u64 value, int index) { -- cgit v1.2.3