Improve the performance of bitmap_find_next_zero_area_off()
Subsystems (2)
v5 cover Improve the performance of bitmap_find_next_zero_area_off()
Use lib/find_bit_benchmark.c for testing. Test results showed a 40% improvement in dense cases, and almost no change in sparse cases.
Performance test results on my hardware:
before(ns) after(ns) change p-value
dense 1211 688 -43.2% 8.3e-11
sparse 13.3 13.4 0.8% 0.27
The following modifications were made based on v4:
- Improve test code and testing methods to make test results more stable and accurate.
- Change the "goto again" code structure to a for loop.
---
v4: https://lore.kernel.org/all/[email protected]
- Test code has been added to PATCH v2.
v3: https://lore.kernel.org/all/[email protected]
- Code optimization was performed on PATCH v1.
v2: https://lore.kernel.org/all/[email protected]
- Do not introduce find_last_bit_from().
v1: https://lore.kernel.org/all/[email protected]
Yi Sun (2):
lib: bitmap: add tests for bitmap_find_next_zero_area_off()
lib: bitmap: optimize bitmap_find_next_zero_area_off()
lib/bitmap.c | 35 +++++++++++++++++++++-------------- lib/find_bit_benchmark.c | 17 +++++++++++++++++ lib/test_bitmap.c | 38 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 76 insertions(+), 14 deletions(-)
Signature · 1 line
2.34.1
PATCH v5 1/2 lib: bitmap: add tests for bitmap_find_next_zero_area_off()
Add functional and performance tests
for bitmap_find_next_zero_area_off().
To maintain consistent output format,
use "find_next_0_area" instead of
"bitmap_find_next_zero_area_off".
performance tests partial output:
Start testing find_bit() with random-filled bitmap
[ 0.191166] find_next_0_area: 1521308 ns, 1326 iterations [ 0.192552] find_next_bit: 1382385 ns, 163724 iterations Start testing find_bit() with sparse bitmap [ 0.196835] find_next_0_area: 4250307 ns, 322497 iterations [ 0.196853] find_next_bit: 15077 ns, 656 iterations
--- lib/find_bit_benchmark.c | 17 +++++++++++++++++ lib/test_bitmap.c | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+)
lib/find_bit_benchmark.c +17 -0
@@ -149,6 +149,21 @@ static int __init test_find_next_and_bit(const void *bitmap,return 0 ; } +static int __init +test_bitmap_find_next_zero_area_off (unsigned long *bitmap,unsigned long len) +{ +unsigned long i, cnt; +ktime_t time; + + time =ktime_get (); +for (cnt = i =0 ; i <BITMAP_LEN ; cnt++) + i =bitmap_find_next_zero_area_off (bitmap,BITMAP_LEN , i,8 ,0 ,0 ) +1 ; + time =ktime_get () - time; +pr_err ("find_next_0_area: %18llu ns, %6ld iterations\n" , time, cnt); + +return 0 ; +} +static int __init find_bit_test (void ) {unsigned long nbits =BITMAP_LEN /SPARSE ;
@@ -158,6 +173,7 @@ static int __init find_bit_test(void)get_random_bytes (bitmap,sizeof (bitmap));get_random_bytes (bitmap2,sizeof (bitmap2)); +test_bitmap_find_next_zero_area_off (bitmap,BITMAP_LEN );test_find_next_bit (bitmap,BITMAP_LEN );test_find_next_zero_bit (bitmap,BITMAP_LEN );test_find_last_bit (bitmap,BITMAP_LEN );
@@ -181,6 +197,7 @@ static int __init find_bit_test(void)__set_bit (get_random_u32_below (BITMAP_LEN ), bitmap2); } +test_bitmap_find_next_zero_area_off (bitmap,BITMAP_LEN );test_find_next_bit (bitmap,BITMAP_LEN );test_find_next_zero_bit (bitmap,BITMAP_LEN );test_find_last_bit (bitmap,BITMAP_LEN );
lib/test_bitmap.c +38 -0
@@ -234,6 +234,43 @@ static void __init test_find_nth_bit(void) } } +static void __init +test_bitmap_find_next_zero_area_off (void ) +{ +DECLARE_BITMAP (bmap,192 ); + +bitmap_set (bmap,0 ,192 ); + +bitmap_clear (bmap,0 ,8 ); +__clear_bit (50 , bmap); +bitmap_clear (bmap,60 ,18 ); +__set_bit (69 , bmap); +__clear_bit (80 , bmap); +bitmap_clear (bmap,100 ,10 ); +__clear_bit (120 , bmap); +bitmap_clear (bmap,145 ,8 ); +bitmap_clear (bmap,160 ,32 ); + +expect_eq_uint (0 , +bitmap_find_next_zero_area_off (bmap,192 ,0 ,8 ,0 ,0 )); +expect_eq_uint (0 , +bitmap_find_next_zero_area_off (bmap,192 ,0 ,8 ,3 ,0 )); +expect_eq_uint (163 , +bitmap_find_next_zero_area_off (bmap,192 ,0 ,8 ,3 ,1 )); +expect_eq_uint (60 , +bitmap_find_next_zero_area_off (bmap,192 ,1 ,8 ,0 ,0 )); +expect_eq_uint (160 , +bitmap_find_next_zero_area_off (bmap,192 ,1 ,8 ,7 ,0 )); +expect_eq_uint (60 , +bitmap_find_next_zero_area_off (bmap,192 ,1 ,8 ,7 ,4 )); +expect_eq_uint (100 , +bitmap_find_next_zero_area_off (bmap,192 ,0 ,10 ,0 ,0 )); +expect_eq_uint (160 , +bitmap_find_next_zero_area_off (bmap,192 ,0 ,32 ,0 ,0 )); +expect_eq_uint (1 , + !!(bitmap_find_next_zero_area_off (bmap,192 ,0 ,33 ,0 ,0 ) >192 )); +} +static void __init test_fill_set (void ) {DECLARE_BITMAP (bmap,1024 );
@@ -1559,6 +1596,7 @@ static void __init selftest(void) test_for_each_clear_bitrange_from(); test_for_each_set_clump8(); test_for_each_set_bit_wrap(); +test_bitmap_find_next_zero_area_off (); }KSTM_MODULE_LOADERS (test_bitmap);
Signature · 1 line
2.34.1
Yury Norov
maintainerRe: [PATCH v5 1/2] lib: bitmap: add tests for bitmap_find_next_zero_area_off()
Quoted 30 lines · Yi Sun
Add functional and performance tests
forbitmap_find_next_zero_area_off().To maintain consistent output format,
use "find_next_0_area" instead of
"bitmap_find_next_zero_area_off".performance tests partial output:
Start testingfind_bit()with random-filled bitmap[ 0.191166] find_next_0_area: 1521308 ns, 1326 iterations [ 0.192552] find_next_bit: 1382385 ns, 163724 iterations Start testing find_bit() with sparse bitmap [ 0.196835] find_next_0_area: 4250307 ns, 322497 iterations [ 0.196853] find_next_bit: 15077 ns, 656 iterationsSigned-off-by Yi Sun <[email protected]>--- lib/find_bit_benchmark.c | 17 +++++++++++++++++ lib/test_bitmap.c | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+)lib/find_bit_benchmark.c +1
-0@@ -149,6 +149,21 @@ static int __init test_find_next_and_bit(const void *bitmap,return 0 ; } +static int __init
lib/find_bit_benchmark.c static int __init test_find_next_and_bit(const void *bitmap, · line 162
Quoted 10 lines
lib/find_bit_benchmark.c +10
-0+test_bitmap_find_next_zero_area_off(unsigned long *bitmap, unsigned long len) +{ +unsigned long i, cnt; +ktime_t time; + + time =ktime_get (); +for (cnt = i =0 ; i <BITMAP_LEN ; cnt++) + i =bitmap_find_next_zero_area_off (bitmap,BITMAP_LEN , i,8 ,0 ,0 ) +1 ; + time =ktime_get () - time; +pr_err ("find_next_0_area: %18llu ns, %6ld iterations\n" , time, cnt);
Change %18llu to %14llu (or whatever), and keep the name as it should.
Quoted 56 lines
lib/find_bit_benchmark.c +6
-0+ +return 0 ; +} +static int __init find_bit_test (void ) {unsigned long nbits =BITMAP_LEN /SPARSE ;@@ -158,6 +173,7 @@ static int __init find_bit_test(void)get_random_bytes (bitmap,sizeof (bitmap));get_random_bytes (bitmap2,sizeof (bitmap2)); +test_bitmap_find_next_zero_area_off (bitmap,BITMAP_LEN );test_find_next_bit (bitmap,BITMAP_LEN );test_find_next_zero_bit (bitmap,BITMAP_LEN );test_find_last_bit (bitmap,BITMAP_LEN );@@ -181,6 +197,7 @@ static int __init find_bit_test(void)__set_bit (get_random_u32_below (BITMAP_LEN ), bitmap2); } +test_bitmap_find_next_zero_area_off (bitmap,BITMAP_LEN );test_find_next_bit (bitmap,BITMAP_LEN );test_find_next_zero_bit (bitmap,BITMAP_LEN );test_find_last_bit (bitmap,BITMAP_LEN );lib/test_bitmap.c +25
-0@@ -234,6 +234,43 @@ static void __init test_find_nth_bit(void) } } +static void __init +test_bitmap_find_next_zero_area_off (void ) +{ +DECLARE_BITMAP (bmap,192 ); + +bitmap_set (bmap,0 ,192 ); + +bitmap_clear (bmap,0 ,8 ); +__clear_bit (50 , bmap); +bitmap_clear (bmap,60 ,18 ); +__set_bit (69 , bmap); +__clear_bit (80 , bmap); +bitmap_clear (bmap,100 ,10 ); +__clear_bit (120 , bmap); +bitmap_clear (bmap,145 ,8 ); +bitmap_clear (bmap,160 ,32 ); + +expect_eq_uint (0 , +bitmap_find_next_zero_area_off (bmap,192 ,0 ,8 ,0 ,0 )); +expect_eq_uint (0 , +bitmap_find_next_zero_area_off (bmap,192 ,0 ,8 ,3 ,0 )); +expect_eq_uint (163 , +bitmap_find_next_zero_area_off (bmap,192 ,0 ,8 ,3 ,1 )); +expect_eq_uint (60 , +bitmap_find_next_zero_area_off (bmap,192 ,1 ,8 ,0 ,0 ));
lib/test_bitmap.c static void __init test_find_nth_bit(void) · line 271
Quoted 10 lines
lib/test_bitmap.c +10
-0+expect_eq_uint (160 , +bitmap_find_next_zero_area_off (bmap,192 ,1 ,8 ,7 ,0 )); +expect_eq_uint (60 , +bitmap_find_next_zero_area_off (bmap,192 ,1 ,8 ,7 ,4 )); +expect_eq_uint (100 , +bitmap_find_next_zero_area_off (bmap,192 ,0 ,10 ,0 ,0 )); +expect_eq_uint (160 , +bitmap_find_next_zero_area_off (bmap,192 ,0 ,32 ,0 ,0 )); +expect_eq_uint (1 , + !!(bitmap_find_next_zero_area_off (bmap,192 ,0 ,33 ,0 ,0 ) >192 ));
It should be >= here.
Quoted 15 lines
lib/test_bitmap.c +3
-0+} +static void __init test_fill_set (void ) {DECLARE_BITMAP (bmap,1024 );@@ -1559,6 +1596,7 @@ static void __init selftest(void) test_for_each_clear_bitrange_from(); test_for_each_set_clump8(); test_for_each_set_bit_wrap(); +test_bitmap_find_next_zero_area_off (); }KSTM_MODULE_LOADERS (test_bitmap);Signature · 1 line
2.34.1
答复: [PATCH v5 1/2] lib: bitmap: add tests for bitmap_find_next_zero_area_off()
Quoted 59 lines
-----邮件原件-----
发件人: Yury Norov <[email protected]>
发送时间: 2026年6月18日 13:48
收件人: 孙毅 (Yi Sun) <[email protected]>
抄送: [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]
主题: Re: [PATCH v5 1/2] lib: bitmap: add tests forbitmap_find_next_zero_area_off()注意: 这封邮件来自于外部。除非你确定邮件内容安全,否则不要点击任何链
接和附件。
CAUTION: This email originated from outside of the organization. Do not click links or open attachments unless you recognize the sender and know the content is safe.lib/find_bit_benchmark.c static int __init test_find_next_and_bit(const void
Quoted 26 lines · Yi Sun
Add functional and performance tests
forbitmap_find_next_zero_area_off().To maintain consistent output format,
use "find_next_0_area" instead of
"bitmap_find_next_zero_area_off".performance tests partial output:
Start testingfind_bit()with random-filled bitmap[ 0.191166] find_next_0_area: 1521308 ns, 1326 iterations [ 0.192552] find_next_bit: 1382385 ns, 163724 iterations Start testing find_bit() with sparse bitmap [ 0.196835] find_next_0_area: 4250307 ns, 322497 iterations [ 0.196853] find_next_bit: 15077 ns, 656 iterationsSigned-off-by Yi Sun <[email protected]>--- lib/find_bit_benchmark.c | 17 +++++++++++++++++ lib/test_bitmap.c | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+)lib/find_bit_benchmark.c +0
-0@@ -149,6 +149,21 @@ static int __init test_find_next_and_bit(const void*bitmap,
lib/find_bit_benchmark.c static int __init test_find_next_and_bit(const void *bitmap,
Quoted 5 lines
lib/find_bit_benchmark.c +2
-0return 0 ; } +static int __init +test_bitmap_find_next_zero_area_off(unsigned long *bitmap, unsigned longlen)
Quoted 11 lines
lib/find_bit_benchmark.c static int __init test_find_next_and_bit(const void *bitmap, · line 162
Quoted 9 lines
lib/find_bit_benchmark.c +9
-0+{ +unsigned long i, cnt; +ktime_t time; + + time =ktime_get (); +for (cnt = i =0 ; i <BITMAP_LEN ; cnt++) + i =bitmap_find_next_zero_area_off (bitmap,BITMAP_LEN , i,8 ,0 ,0 ) +1 ; + time =ktime_get () - time; +pr_err ("find_next_0_area: %18llu ns, %6ld iterations\n" , time, cnt);Change %18llu to %14llu (or whatever), and keep the name as it should.
ok, I will make changes in v6.
Quoted 61 lines
Quoted 61 lines
lib/find_bit_benchmark.c +6
-0+ +return 0 ; +} +static int __init find_bit_test (void ) {unsigned long nbits =BITMAP_LEN /SPARSE ;@@ -158,6 +173,7 @@ static int __init find_bit_test(void)get_random_bytes (bitmap,sizeof (bitmap));get_random_bytes (bitmap2,sizeof (bitmap2)); +test_bitmap_find_next_zero_area_off (bitmap,BITMAP_LEN );test_find_next_bit (bitmap,BITMAP_LEN );test_find_next_zero_bit (bitmap,BITMAP_LEN );test_find_last_bit (bitmap,BITMAP_LEN );@@ -181,6 +197,7 @@ static int __init find_bit_test(void)__set_bit (get_random_u32_below (BITMAP_LEN ), bitmap2); } +test_bitmap_find_next_zero_area_off (bitmap,BITMAP_LEN );test_find_next_bit (bitmap,BITMAP_LEN );test_find_next_zero_bit (bitmap,BITMAP_LEN );test_find_last_bit (bitmap,BITMAP_LEN );lib/test_bitmap.c +29
-0@@ -234,6 +234,43 @@ static void __init test_find_nth_bit(void) } } +static void __init +test_bitmap_find_next_zero_area_off (void ) +{ +DECLARE_BITMAP (bmap,192 ); + +bitmap_set (bmap,0 ,192 ); + +bitmap_clear (bmap,0 ,8 ); +__clear_bit (50 , bmap); +bitmap_clear (bmap,60 ,18 ); +__set_bit (69 , bmap); +__clear_bit (80 , bmap); +bitmap_clear (bmap,100 ,10 ); +__clear_bit (120 , bmap); +bitmap_clear (bmap,145 ,8 ); +bitmap_clear (bmap,160 ,32 ); + +expect_eq_uint (0 , +bitmap_find_next_zero_area_off (bmap,192 ,0 ,8 ,0 ,0 )); +expect_eq_uint (0 , +bitmap_find_next_zero_area_off (bmap,192 ,0 ,8 ,3 ,0 )); +expect_eq_uint (163 , +bitmap_find_next_zero_area_off (bmap,192 ,0 ,8 ,3 ,1 )); +expect_eq_uint (60 , +bitmap_find_next_zero_area_off (bmap,192 ,1 ,8 ,0 ,0 )); +expect_eq_uint (160 , +bitmap_find_next_zero_area_off (bmap,192 ,1 ,8 ,7 ,0 )); +expect_eq_uint (60 , +bitmap_find_next_zero_area_off (bmap,192 ,1 ,8 ,7 ,4 ));
Quoted 8 lines
lib/test_bitmap.c static void __init test_find_nth_bit(void) · line 271
Quoted 6 lines
lib/test_bitmap.c +6
-0+expect_eq_uint (100 , +bitmap_find_next_zero_area_off (bmap,192 ,0 ,10 ,0 ,0 )); +expect_eq_uint (160 , +bitmap_find_next_zero_area_off (bmap,192 ,0 ,32 ,0 ,0 )); +expect_eq_uint (1 , + !!(bitmap_find_next_zero_area_off (bmap,192 ,0 ,33 ,0 ,0 ) >192 ));It should be >= here.
There would be a problem if the return value equals 192. size = 192;
if (end > size)
return end;
So the return value must be greater than 192.
Quoted 16 lines
Quoted 16 lines
lib/test_bitmap.c +3
-1+} +static void __init test_fill_set (void ) {DECLARE_BITMAP (bmap,1024 );@@ -1559,6 +1596,7 @@ static void __init selftest(void) test_for_each_clear_bitrange_from(); test_for_each_set_clump8(); test_for_each_set_bit_wrap(); +test_bitmap_find_next_zero_area_off (); }KSTM_MODULE_LOADERS (test_bitmap); --2.34.1
答复: [PATCH v5 1/2] lib: bitmap: add tests for bitmap_find_next_zero_area_off()
In reply to Yury NorovQuoted 166 lines
-----邮件原件-----
发件人: 孙毅 (Yi Sun)
发送时间: 2026年6月18日 17:43
收件人: 'Yury Norov' <[email protected]>
抄送: '[email protected]' <[email protected]>; '[email protected]' <[email protected]>; '[email protected]' <[email protected]>; '[email protected]' <[email protected]>; '[email protected]' <[email protected]>; 'linux- [email protected]' <[email protected]>; '[email protected]' <[email protected]>; '[email protected]' <[email protected]>; '[email protected]' <[email protected]>; '[email protected]' <[email protected]>; '[email protected]' <[email protected]>; '[email protected]' <[email protected]>; '[email protected]' <[email protected]>; '[email protected]' <[email protected]>; 王科 (Ke Wang) <[email protected]>; 'John Stultz' <[email protected]> 主题: 答复: [PATCH v5 1/2] lib: bitmap: add tests forbitmap_find_next_zero_area_off()Quoted 15 lines
-----邮件原件-----
发件人: Yury Norov <[email protected]>
发送时间: 2026年6月18日 13:48
收件人: 孙毅 (Yi Sun) <[email protected]>
抄送: [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]
主题: Re: [PATCH v5 1/2] lib: bitmap: add tests forbitmap_find_next_zero_area_off()Quoted 1 line
注意: 这封邮件来自于外部。除非你确定邮件内容安全,否则不要点击任何
链
Quoted 2 lines
接和附件。
CAUTION: This email originated from outside of the organization. Do not clicklinks
Quoted 39 lines
or open attachments unless you recognize the sender and know the content is safe.
lib/find_bit_benchmark.c static int __init test_find_next_and_bit(const void
Quoted 26 lines · Yi Sun
Add functional and performance tests
forbitmap_find_next_zero_area_off().To maintain consistent output format,
use "find_next_0_area" instead of
"bitmap_find_next_zero_area_off".performance tests partial output:
Start testingfind_bit()with random-filled bitmap[ 0.191166] find_next_0_area: 1521308 ns, 1326 iterations [ 0.192552] find_next_bit: 1382385 ns, 163724 iterations Start testing find_bit() with sparse bitmap [ 0.196835] find_next_0_area: 4250307 ns, 322497 iterations [ 0.196853] find_next_bit: 15077 ns, 656 iterationsSigned-off-by Yi Sun <[email protected]>--- lib/find_bit_benchmark.c | 17 +++++++++++++++++ lib/test_bitmap.c | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+)lib/find_bit_benchmark.c +0
-0@@ -149,6 +149,21 @@ static int __init test_find_next_and_bit(const void*bitmap,
lib/find_bit_benchmark.c static int __init test_find_next_and_bit(const void *bitmap,
Quoted 5 lines
lib/find_bit_benchmark.c +2
-0return 0 ; } +static int __init +test_bitmap_find_next_zero_area_off(unsigned long *bitmap, unsigned longlen)
Quoted 7 lines
lib/find_bit_benchmark.c static int __init test_find_next_and_bit(const void *bitmap,
Quoted 7 lines
lib/find_bit_benchmark.c +7
-0+{ +unsigned long i, cnt; +ktime_t time; + + time =ktime_get (); +for (cnt = i =0 ; i <BITMAP_LEN ; cnt++) + i = bitmap_find_next_zero_area_off(bitmap, BITMAP_LEN, i, 8, 0, 0) +1;
Quoted 4 lines
lib/find_bit_benchmark.c static int __init test_find_next_and_bit(const void *bitmap, · line 162
Quoted 2 lines
lib/find_bit_benchmark.c +2
-0+ time =ktime_get () - time; +pr_err ("find_next_0_area: %18llu ns, %6ld iterations\n" , time, cnt);Change %18llu to %14llu (or whatever), and keep the name as it should.
ok, I will make changes in v6.
Quoted 61 lines
Quoted 61 lines
lib/find_bit_benchmark.c +6
-0+ +return 0 ; +} +static int __init find_bit_test (void ) {unsigned long nbits =BITMAP_LEN /SPARSE ;@@ -158,6 +173,7 @@ static int __init find_bit_test(void)get_random_bytes (bitmap,sizeof (bitmap));get_random_bytes (bitmap2,sizeof (bitmap2)); +test_bitmap_find_next_zero_area_off (bitmap,BITMAP_LEN );test_find_next_bit (bitmap,BITMAP_LEN );test_find_next_zero_bit (bitmap,BITMAP_LEN );test_find_last_bit (bitmap,BITMAP_LEN );@@ -181,6 +197,7 @@ static int __init find_bit_test(void)__set_bit (get_random_u32_below (BITMAP_LEN ), bitmap2); } +test_bitmap_find_next_zero_area_off (bitmap,BITMAP_LEN );test_find_next_bit (bitmap,BITMAP_LEN );test_find_next_zero_bit (bitmap,BITMAP_LEN );test_find_last_bit (bitmap,BITMAP_LEN );lib/test_bitmap.c +29
-0@@ -234,6 +234,43 @@ static void __init test_find_nth_bit(void) } } +static void __init +test_bitmap_find_next_zero_area_off (void ) +{ +DECLARE_BITMAP (bmap,192 ); + +bitmap_set (bmap,0 ,192 ); + +bitmap_clear (bmap,0 ,8 ); +__clear_bit (50 , bmap); +bitmap_clear (bmap,60 ,18 ); +__set_bit (69 , bmap); +__clear_bit (80 , bmap); +bitmap_clear (bmap,100 ,10 ); +__clear_bit (120 , bmap); +bitmap_clear (bmap,145 ,8 ); +bitmap_clear (bmap,160 ,32 ); + +expect_eq_uint (0 , +bitmap_find_next_zero_area_off (bmap,192 ,0 ,8 ,0 ,0 )); +expect_eq_uint (0 , +bitmap_find_next_zero_area_off (bmap,192 ,0 ,8 ,3 ,0 )); +expect_eq_uint (163 , +bitmap_find_next_zero_area_off (bmap,192 ,0 ,8 ,3 ,1 )); +expect_eq_uint (60 , +bitmap_find_next_zero_area_off (bmap,192 ,1 ,8 ,0 ,0 )); +expect_eq_uint (160 , +bitmap_find_next_zero_area_off (bmap,192 ,1 ,8 ,7 ,0 )); +expect_eq_uint (60 , +bitmap_find_next_zero_area_off (bmap,192 ,1 ,8 ,7 ,4 ));Quoted 8 lines
lib/test_bitmap.c static void __init test_find_nth_bit(void) · line 271
Quoted 6 lines
lib/test_bitmap.c +6
-0+expect_eq_uint (100 , +bitmap_find_next_zero_area_off (bmap,192 ,0 ,10 ,0 ,0 )); +expect_eq_uint (160 , +bitmap_find_next_zero_area_off (bmap,192 ,0 ,32 ,0 ,0 )); +expect_eq_uint (1 , + !!(bitmap_find_next_zero_area_off (bmap,192 ,0 ,33 ,0 ,0 ) >192 ));It should be >= here.
Quoted 5 lines
There would be a problem if the return value equals 192. size = 192;
if (end > size)return end;So the return value must be greater than 192.
I agree with the patch you suggested that returns the size, so it needs to be changed to >= here.
Quoted 17 lines
Quoted 17 lines
Quoted 17 lines
lib/test_bitmap.c +3
-1+} +static void __init test_fill_set (void ) {DECLARE_BITMAP (bmap,1024 );@@ -1559,6 +1596,7 @@ static void __init selftest(void) test_for_each_clear_bitrange_from(); test_for_each_set_clump8(); test_for_each_set_bit_wrap(); +test_bitmap_find_next_zero_area_off (); }KSTM_MODULE_LOADERS (test_bitmap); --2.34.1
Yury Norov
maintainerRe: 答复: [PATCH v5 1/2] lib: bitmap: add tests for bitmap_find_next_zero_area_off()
Quoted 6 lines · 孙毅 (Yi Sun)
Quoted 6 lines
Quoted 5 lines
lib/test_bitmap.c static void __init test_find_nth_bit(void) · line 271
Quoted 3 lines
lib/test_bitmap.c +2
-0+expect_eq_uint (1 , + !!(bitmap_find_next_zero_area_off (bmap,192 ,0 ,33 ,0 ,0 ) >192 ));It should be >= here.
Quoted 7 lines
Quoted 5 lines
There would be a problem if the return value equals 192. size = 192;
if (end > size)return end;So the return value must be greater than 192.
I agree with the patch you suggested that returns the size, so it needs to be changed to >= here.
The valid index range is 0...(size-1), i.e. for 10-bits bitmap the highest valid index is 9. If your implementation never returns 'size', it doesn't mean that the test should consider 'size' as a valid index.
(I'm not even sure that your implementation never returns 'size').
Quoted 15 lines
Quoted 15 lines
Quoted 15 lines
Quoted 15 lines
lib/test_bitmap.c +3
-1+} +static void __init test_fill_set (void ) {DECLARE_BITMAP (bmap,1024 );@@ -1559,6 +1596,7 @@ static void __init selftest(void) test_for_each_clear_bitrange_from(); test_for_each_set_clump8(); test_for_each_set_bit_wrap(); +test_bitmap_find_next_zero_area_off (); }KSTM_MODULE_LOADERS (test_bitmap); --2.34.1
PATCH v5 2/2 lib: bitmap: optimize bitmap_find_next_zero_area_off()
In reply to Yi SunFinding a contiguous free region in a highly fragmented bitmap is not easy and may require many repeated attempts. Therefore, find_next_bit(map, end, index) is not the optimal choice. This is because there may be multiple scattered free regions within the range [index, end) and none of them will meet the length requirement of @nr.
Instead, it's sufficient to directly find the last bit within the range [index, end), thus reducing unnecessary repeated calls.
An example of a bitmap:
Bits 0-3: cleared(4 bits) Bits 4-5: set (2 bits) Bits 6-8: cleared(4 bits) Bits 9-10: set (2 bits) Bits 11-20: cleared(10 bits)
The goal is to find a 10-bit free region.
The old code logic is as follows:find_next_zero_bit(start = 0, find bit 0) -> find_next_bit(find bit 4) -> goto again ->find_next_zero_bit(start = 5, find bit 6) -> find_next_bit(find bit 9) -> goto again ->find_next_zero_bit(start = 10, find bit 11) -> success
The new code logic is as follows:find_next_zero_bit(start = 0, find bit 0) -> find_last_bit(find bit 9) -> goto again ->find_next_zero_bit(start = 10, find bit 11) -> success
Performance test results on my hardware(use lib/find_bit_benchmark.c):
before after change p-value
dense 1211 688 -43.2% 8.3e-11
sparse 13.3 13.4 0.8% 0.27
--- lib/bitmap.c | 35 +++++++++++++++++++++-------------- 1 file changed, 21 insertions(+), 14 deletions(-)
lib/bitmap.c +21 -14
@@ -432,22 +432,29 @@ unsigned long bitmap_find_next_zero_area_off(unsigned long *map,unsigned long align_mask, unsigned long align_offset) { - unsigned long index, end, i; -again: - index =find_next_zero_bit (map, size, start); - -/* Align allocation */ - index =__ALIGN_MASK (index + align_offset, align_mask) - align_offset; - - end = index + nr; -if (end > size) -return end; - i =find_next_bit (map, end, index); -if (i < end) { + unsigned long find_idx, end, i, find_word_idx, find_word_off; + +for (;;) { + find_idx =find_next_zero_bit (map, size, start); + +/* Align allocation */ + find_idx =__ALIGN_MASK (find_idx + align_offset, + align_mask) - align_offset; + + end = find_idx + nr; +if (end > size) +return end; + + find_word_idx = find_idx /BITS_PER_LONG ; + find_word_off = find_word_idx *BITS_PER_LONG ; + + i =find_last_bit (map + find_word_idx, + end - find_word_off) + find_word_off; +if (i >= end || i < find_idx) +return find_idx; + start = i +1 ; - goto again; } -return index; }EXPORT_SYMBOL (bitmap_find_next_zero_area_off);
Signature · 1 line
2.34.1
Yury Norov
maintainerRe: [PATCH v5 2/2] lib: bitmap: optimize bitmap_find_next_zero_area_off()
Quoted 25 lines · Yi Sun
Finding a contiguous free region in a highly fragmented bitmap is not easy and may require many repeated attempts. Therefore,
find_next_bit(map, end, index)is not the optimal choice. This is because there may be multiple scattered free regions within the range [index, end) and none of them will meet the length requirement of @nr.
Instead, it's sufficient to directly find the last bit within the range [index, end), thus reducing unnecessary repeated calls.An example of a bitmap:
Bits 0-3: cleared(4 bits) Bits 4-5: set (2 bits) Bits 6-8: cleared(4 bits) Bits 9-10: set (2 bits) Bits 11-20: cleared(10 bits)The goal is to find a 10-bit free region.
The old code logic is as follows:
find_next_zero_bit(start = 0, find bit 0)->find_next_bit(find bit 4)-> goto again ->find_next_zero_bit(start = 5, find bit 6)->find_next_bit(find bit 9)-> goto again ->find_next_zero_bit(start = 10, find bit 11)-> success
Quoted 4 lines
The new code logic is as follows:
find_next_zero_bit(start = 0, find bit 0)->find_last_bit(find bit 9)-> goto again ->find_next_zero_bit(start = 10, find bit 11)-> success
In new logic there's no goto, right?
Quoted 24 lines
Performance test results on my
hardware(use lib/find_bit_benchmark.c):before after change p-value
dense 1211 688 -43.2% 8.3e-11
sparse 13.3 13.4 0.8% 0.27Signed-off-by Yi Sun <[email protected]>--- lib/bitmap.c | 35 +++++++++++++++++++++-------------- 1 file changed, 21 insertions(+), 14 deletions(-)lib/bitmap.c +0
-5@@ -432,22 +432,29 @@ unsigned long bitmap_find_next_zero_area_off(unsigned long *map,unsigned long align_mask, unsigned long align_offset) { - unsigned long index, end, i; -again: - index =find_next_zero_bit (map, size, start); - -/* Align allocation */
lib/bitmap.c unsigned long bitmap_find_next_zero_area_off(unsigned long *map, · line 437
Quoted 10 lines
lib/bitmap.c +3
-7- index =__ALIGN_MASK (index + align_offset, align_mask) - align_offset; - - end = index + nr; -if (end > size) -return end; - i =find_next_bit (map, end, index); -if (i < end) { +unsigned long find_idx, end, i, find_word_idx, find_word_off; + +for (;;) {
"Infinite" loop is nothing better than the backward goto. Can you try this:
unsigned long end, i, off;
for_each_clear_bit_from(start, map, size) {
start = __ALIGN_MASK(start + align_offset, align_mask) - align_offset;
end = start + nr;
if (end > size)
break;
off = round_down(start, BITS_PER_LONG);
i = find_last_bit(map + start / BITS_PER_LONG, end - off) + off;
if (i >= end || i < start)
return start;
start = i;
}
return size;If it works for you, let's move with the for_each() version. Can you test and add my Co-developed-by please?
Thanks,
Yury
Quoted 27 lines
lib/bitmap.c +18
-2+ find_idx =find_next_zero_bit (map, size, start); + +/* Align allocation */ + find_idx =__ALIGN_MASK (find_idx + align_offset, + align_mask) - align_offset; + + end = find_idx + nr; +if (end > size) +return end; + + find_word_idx = find_idx /BITS_PER_LONG ; + find_word_off = find_word_idx *BITS_PER_LONG ; + + i =find_last_bit (map + find_word_idx, + end - find_word_off) + find_word_off; +if (i >= end || i < find_idx) +return find_idx; + start = i +1 ; - goto again; } -return index; }EXPORT_SYMBOL (bitmap_find_next_zero_area_off);Signature · 1 line
2.34.1
答复: [PATCH v5 2/2] lib: bitmap: optimize bitmap_find_next_zero_area_off()
Quoted 50 lines
-----邮件原件-----
发件人: Yury Norov <[email protected]>
发送时间: 2026年6月18日 14:15
收件人: 孙毅 (Yi Sun) <[email protected]>
抄送: [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]
主题: Re: [PATCH v5 2/2] lib: bitmap: optimizebitmap_find_next_zero_area_off()注意: 这封邮件来自于外部。除非你确定邮件内容安全,否则不要点击任何链
接和附件。
CAUTION: This email originated from outside of the organization. Do not click links or open attachments unless you recognize the sender and know the content is safe.Quoted 25 lines · Yi Sun
Finding a contiguous free region in a highly fragmented bitmap is not easy and may require many repeated attempts. Therefore,
find_next_bit(map, end, index)is not the optimal choice. This is because there may be multiple scattered free regions within the range [index, end) and none of them will meet the length requirement of @nr.
Instead, it's sufficient to directly find the last bit within the range [index, end), thus reducing unnecessary repeated calls.An example of a bitmap:
Bits 0-3: cleared(4 bits) Bits 4-5: set (2 bits) Bits 6-8: cleared(4 bits) Bits 9-10: set (2 bits) Bits 11-20: cleared(10 bits)The goal is to find a 10-bit free region.
The old code logic is as follows:
find_next_zero_bit(start = 0, find bit 0)->find_next_bit(find bit 4)-> goto again ->find_next_zero_bit(start = 5, find bit 6)->find_next_bit(find bit 9)-> goto again ->find_next_zero_bit(start = 10, find bit 11)-> success
Quoted 6 lines
Quoted 4 lines
The new code logic is as follows:
find_next_zero_bit(start = 0, find bit 0)->find_last_bit(find bit 9)-> goto again ->find_next_zero_bit(start = 10, find bit 11)-> successIn new logic there's no goto, right?
ok, I will make changes in v6.
Quoted 58 lines
lib/bitmap.c unsigned long
Quoted 17 lines
Performance test results on my
hardware(use lib/find_bit_benchmark.c):before after change p-value dense 1211 688 -43.2% 8.3e-11 sparse 13.3 13.4 0.8% 0.27Signed-off-by Yi Sun <[email protected]>--- lib/bitmap.c | 35 +++++++++++++++++++++-------------- 1 file changed, 21 insertions(+), 14 deletions(-)lib/bitmap.c +0
-0@@ -432,22 +432,29 @@ unsigned longbitmap_find_next_zero_area_off(unsigned long *map,
Quoted 8 lines
lib/bitmap.c +0
-5unsigned long align_mask, unsigned long align_offset) { - unsigned long index, end, i; -again: - index =find_next_zero_bit (map, size, start); - -/* Align allocation */ lib/bitmap.c unsigned long bitmap_find_next_zero_area_off(unsigned long *map, · line 437
Quoted 10 lines
lib/bitmap.c +3
-7- index =__ALIGN_MASK (index + align_offset, align_mask) - align_offset; - - end = index + nr; -if (end > size) -return end; - i =find_next_bit (map, end, index); -if (i < end) { +unsigned long find_idx, end, i, find_word_idx, find_word_off; + +for (;;) {"Infinite" loop is nothing better than the backward goto. Can you try this:
unsigned long end, i, off; for_each_clear_bit_from(start, map, size) { start = __ALIGN_MASK(start + align_offset, align_mask) - align_offset; end = start + nr; if (end > size) break; off = round_down(start, BITS_PER_LONG); i = find_last_bit(map + start / BITS_PER_LONG, end - off) + off; if (i >= end || i < start) return start; start = i; } return size;
Quoted 2 lines
If it works for you, let's move with the
for_each()version. Can you test and add my Co-developed-by please?
ok, I will test and make changes in v6.
Quoted 31 lines
Thanks,
YuryQuoted 27 lines
lib/bitmap.c +18
-3+ find_idx =find_next_zero_bit (map, size, start); + +/* Align allocation */ + find_idx =__ALIGN_MASK (find_idx + align_offset, + align_mask) - align_offset; + + end = find_idx + nr; +if (end > size) +return end; + + find_word_idx = find_idx /BITS_PER_LONG ; + find_word_off = find_word_idx *BITS_PER_LONG ; + + i =find_last_bit (map + find_word_idx, + end - find_word_off) + find_word_off; +if (i >= end || i < find_idx) +return find_idx; + start = i +1 ; - goto again; } -return index; }EXPORT_SYMBOL (bitmap_find_next_zero_area_off); --2.34.1