cpumask: repair cpumask_check()
Subsystems (4)
Yury Norov
maintainercover cpumask: repair cpumask_check()
After switching cpumask to use nr_cpu_ids in [1], cpumask_check() started generating many false-positive warnings. There are some more issues with the cpumask_check() that brake it.
This series fixes cpumask_check() mess and addresses most of the false-positive warnings observed on boot of x86_64 and arm64.
[1] https://lore.kernel.org/lkml/[email protected]/T/
Yury Norov (7):
cpumask: fix checking valid cpu range
net: fix cpu_max_bits_warn() usage in netif_attrmask_next{,_and}
cpumask: switch for_each_cpu{,_not} to use for_each_bit() lib/find_bit: add find_next{,_and}_bit_wrap lib/bitmap: introduce for_each_set_bit_wrap() macro lib/find: optimize for_each() macros
lib/bitmap: add tests for for_each() iterators
include/linux/cpumask.h | 37 ++---- include/linux/find.h | 140 +++++++++++++++++----- include/linux/netdevice.h | 10 +- lib/cpumask.c | 12 +- lib/test_bitmap.c | 244 +++++++++++++++++++++++++++++++++++++- 5 files changed, 375 insertions(+), 68 deletions(-)
Signature · 1 line
2.34.1
Yury Norov
maintainerPATCH 1/7 cpumask: fix checking valid cpu range
The range of valid CPUs is [0, nr_cpu_ids). Some cpumask functions are passed with a shifted CPU index, and for them, the valid range is [-1, nr_cpu_ids-1). Currently for those functions, we check the index against [-1, nr_cpu_ids), which is wrong.
--- include/linux/cpumask.h | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-)
include/linux/cpumask.h +8 -11
@@ -174,9 +174,8 @@ static inline unsigned int cpumask_last(const struct cpumask *srcp)static inline unsigned int cpumask_next (int n,const struct cpumask *srcp) { -/* -1 is a legal arg here. */ -if (n !=-1 ) -cpumask_check (n); +/* n is a prior cpu */ +cpumask_check (n +1 );return find_next_bit (cpumask_bits (srcp), nr_cpumask_bits, n +1 ); }
@@ -189,9 +188,8 @@ unsigned int cpumask_next(int n, const struct cpumask *srcp)*/ static inline unsigned int cpumask_next_zero (int n,const struct cpumask *srcp) { -/* -1 is a legal arg here. */ -if (n !=-1 ) -cpumask_check (n); +/* n is a prior cpu */ +cpumask_check (n +1 );return find_next_zero_bit (cpumask_bits (srcp), nr_cpumask_bits, n+1 ); }
@@ -231,9 +229,8 @@ static inlineunsigned int cpumask_next_and (int n,const struct cpumask *src1p,const struct cpumask *src2p) { -/* -1 is a legal arg here. */ -if (n !=-1 ) -cpumask_check (n); +/* n is a prior cpu */ +cpumask_check (n +1 );return find_next_and_bit (cpumask_bits (src1p),cpumask_bits (src2p), nr_cpumask_bits, n +1 ); }
@@ -267,8 +264,8 @@ static inlineunsigned int cpumask_next_wrap (int n,const struct cpumask *mask,int start,bool wrap) {cpumask_check (start); -if (n !=-1 ) -cpumask_check (n); +/* n is a prior cpu */ +cpumask_check (n +1 );/* * Return the first available CPU when wrapping, or when starting before cpu0,
Signature · 1 line
2.34.1
Re: [PATCH 1/7] cpumask: fix checking valid cpu range
Quoted 15 lines · Yury Norov
The range of valid CPUs is [0,
nr_cpu_ids). Some cpumask functions are passed with a shifted CPU index, and for them, the valid range is [-1, nr_cpu_ids-1). Currently for those functions, we check the index against [-1,nr_cpu_ids), which is wrong.Signed-off-by Yury Norov <[email protected]>--- include/linux/cpumask.h | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-)include/linux/cpumask.h +0
-0
include/linux/cpumask.h static inline unsigned int cpumask_last(const struct cpumask *srcp) · line 179
Quoted 9 lines
include/linux/cpumask.h +2
-3@@ -174,9 +174,8 @@ static inline unsigned int cpumask_last(const struct cpumask *srcp)static inline unsigned int cpumask_next (int n,const struct cpumask *srcp) { -/* -1 is a legal arg here. */ -if (n !=-1 ) -cpumask_check (n); +/* n is a prior cpu */ +cpumask_check (n +1 );return find_next_bit (cpumask_bits (srcp), nr_cpumask_bits, n +1 );
I'm confused, this makes passing nr_cpu_ids-1 to cpumask_next*() trigger a warning. The documentation does states:
* @n: the cpu prior to the place to search (ie. return will be > @n)
So n is a valid CPU number (with -1 being the exception for scan initialization), this shouldn't exclude nr_cpu_ids-1.
IMO passing nr_cpu_ids-1 should be treated the same as passing the last set bit in a bitmap: no warning, and returns the bitmap size. Otherwise reaching nr_cpu_ids-1 has to be special-cased by the calling code which seems like unnecessary boiler plate
For instance, I trigger the cpumask_check() warning there:
3d2dcab932d0:block/blk-mq.c @l2047
if (--hctx->next_cpu_batch <= 0) {
select_cpu:
next_cpu = cpumask_next_and(next_cpu, hctx->cpumask, <-----
cpu_online_mask);
if (next_cpu >= nr_cpu_ids)
next_cpu = blk_mq_first_mapped_cpu(hctx);
hctx->next_cpu_batch = BLK_MQ_CPU_WORK_BATCH;
}next_cpu is a valid CPU number, shifting it doesn't seem to make sense, and we do want it to reach nr_cpu_ids-1.
Yury Norov
maintainerRe: [PATCH 1/7] cpumask: fix checking valid cpu range
Quoted 29 lines · Valentin Schneider
Quoted 15 lines · Yury Norov
The range of valid CPUs is [0,
nr_cpu_ids). Some cpumask functions are passed with a shifted CPU index, and for them, the valid range is [-1, nr_cpu_ids-1). Currently for those functions, we check the index against [-1,nr_cpu_ids), which is wrong.Signed-off-by Yury Norov <[email protected]>--- include/linux/cpumask.h | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-)include/linux/cpumask.h +0
-0include/linux/cpumask.h static inline unsigned int cpumask_last(const struct cpumask *srcp) · line 179
Quoted 9 lines
include/linux/cpumask.h +2
-3@@ -174,9 +174,8 @@ static inline unsigned int cpumask_last(const struct cpumask *srcp)static inline unsigned int cpumask_next (int n,const struct cpumask *srcp) { -/* -1 is a legal arg here. */ -if (n !=-1 ) -cpumask_check (n); +/* n is a prior cpu */ +cpumask_check (n +1 );return find_next_bit (cpumask_bits (srcp), nr_cpumask_bits, n +1 );I'm confused, this makes passing nr_cpu_ids-1 to cpumask_next*() trigger a warning. The documentation does states:
Quoted 4 lines
* @n: the cpu prior to the place to search (ie. return will be > @n)
So n is a valid CPU number (with -1 being the exception for scan initialization), this shouldn't exclude nr_cpu_ids-1.
For a regular cpumask function, like cpumask_any_but(), the valid range is [0, nr_cpu_ids).
'Special' functions shift by 1 when call underlying find API:
static inline
unsigned int cpumask_next(int n, const struct cpumask *srcp)
{
/* n is a prior cpu */
cpumask_check(n + 1);
return find_next_bit(cpumask_bits(srcp), nr_cpumask_bits, n + 1);
}So, for them the valid range [0, nr_cpu_ids) must be shifted in other
direction: [-1, nr_cpu_ids-1).
Quoted 3 lines
IMO passing nr_cpu_ids-1 should be treated the same as passing the last set bit in a bitmap: no warning, and returns the bitmap size.
This is how cpumask_check() works for normal functions. For cpumask_next() passing nr_cpu_ids-1 is the same as passing nr_cpu_ids for cpumask_any_but(), and it should trigger warning in both cases. (Or should not, but it's a different story.)
Quoted 14 lines
calling code which seems like unnecessary boiler plate
For instance, I trigger the
cpumask_check()warning there:3d2dcab932d0:block/blk-mq.c @l2047
if (--hctx->next_cpu_batch <= 0) { select_cpu: next_cpu = cpumask_next_and(next_cpu, hctx->cpumask, <----- cpu_online_mask); if (next_cpu >= nr_cpu_ids) next_cpu = blk_mq_first_mapped_cpu(hctx); hctx->next_cpu_batch = BLK_MQ_CPU_WORK_BATCH; }
Quoted 2 lines
next_cpuis a valid CPU number, shifting it doesn't seem to make sense, and we do want it to reach nr_cpu_ids-1.
next_cpu is a valid CPU number for all, but not for cpumask_next(). The warning is valid. If we are at the very last cpu, what for we look for next?
The snippet above should be fixed like this:
if (--hctx->next_cpu_batch <= 0) {
select_cpu:
if (next_cpu == nr_cpu_ids - 1)
next_cpu = nr_cpu_ids;
else
next_cpu = cpumask_next_and(next_cpu,
hctx->cpumask,
cpu_online_mask);
if (next_cpu >= nr_cpu_ids)
next_cpu = blk_mq_first_mapped_cpu(hctx);
hctx->next_cpu_batch = BLK_MQ_CPU_WORK_BATCH;
}The original motivation for this special shifted semantics was to avoid passing '+1' in cpumask_next() everywhere where it's used to iterate over cpumask. This is especially ugly because it brings negative semantics in such a simple thing like an index, and makes people confused. It was a bad decision, but now it's so broadly used that we have to live with it.
The strategy to mitigate this is to minimize using of that 'special' functions. They all are cpumask_next()-like. In this series I reworked for_each_cpu() to not use cpumask_next().
Often, cpumask_next() is a part of opencoded for_each_cpu(), and this is relatively easy to fix. In case of blk_mq_hctx_next_cpu() that you mentioned above, cpumask_next_and() usage looks unavoidable, and there's nothing to do with that, except that being careful.
It didn't trigger the warning in my test setup, so I didn't fix it. Feel free to submit a patch, if you observe the warning for yourself.
Maybe we should consider nr_cpu_ids as a special valid index for cpumask_check(), a sign of the end of an array. This would help to silence many warnings, like this one. For now I'm leaning towards that it's more a hack than a meaningful change.
Thanks,
Yury
Re: [PATCH 1/7] cpumask: fix checking valid cpu range
Quoted 34 lines · Yury Norov
Quoted 15 lines · Valentin Schneider
include/linux/cpumask.h static inline unsigned int cpumask_last(const struct cpumask *srcp) · line 179
Quoted 10 lines · Yury Norov
include/linux/cpumask.h +2
-3@@ -174,9 +174,8 @@ static inline unsigned int cpumask_last(const struct cpumask *srcp)static inline unsigned int cpumask_next (int n,const struct cpumask *srcp) { -/* -1 is a legal arg here. */ -if (n !=-1 ) -cpumask_check (n); +/* n is a prior cpu */ +cpumask_check (n +1 );return find_next_bit (cpumask_bits (srcp), nr_cpumask_bits, n +1 );I'm confused, this makes passing nr_cpu_ids-1 to cpumask_next*() trigger a warning. The documentation does states:
Quoted 4 lines
* @n: the cpu prior to the place to search (ie. return will be > @n)
So n is a valid CPU number (with -1 being the exception for scan initialization), this shouldn't exclude nr_cpu_ids-1.
For a regular cpumask function, like
cpumask_any_but(), the valid range is [0,nr_cpu_ids).'Special' functions shift by 1 when call underlying find API:
static inline unsigned int cpumask_next(int n, const struct cpumask *srcp) { /* n is a prior cpu */ cpumask_check(n + 1); return find_next_bit(cpumask_bits(srcp), nr_cpumask_bits, n + 1); }
Quoted 3 lines
So, for them the valid range [0,
nr_cpu_ids) must be shifted in other
direction: [-1, nr_cpu_ids-1).
The way I've been seeing this is that the [0, nr_cpu_ids) range is extended to [-1, nr_cpu_ids) to accommodate for iteration starts.
Quoted 23 lines
Quoted 3 lines
IMO passing nr_cpu_ids-1 should be treated the same as passing the last set bit in a bitmap: no warning, and returns the bitmap size.
This is how
cpumask_check()works for normal functions. Forcpumask_next()passing nr_cpu_ids-1 is the same as passingnr_cpu_idsforcpumask_any_but(), and it should trigger warning in both cases. (Or should not, but it's a different story.)Quoted 14 lines
calling code which seems like unnecessary boiler plate
For instance, I trigger the
cpumask_check()warning there:3d2dcab932d0:block/blk-mq.c @l2047
if (--hctx->next_cpu_batch <= 0) { select_cpu: next_cpu = cpumask_next_and(next_cpu, hctx->cpumask, <----- cpu_online_mask); if (next_cpu >= nr_cpu_ids) next_cpu = blk_mq_first_mapped_cpu(hctx); hctx->next_cpu_batch = BLK_MQ_CPU_WORK_BATCH; }
Quoted 7 lines
Quoted 2 lines
next_cpuis a valid CPU number, shifting it doesn't seem to make sense, and we do want it to reach nr_cpu_ids-1.
next_cpuis a valid CPU number for all, but not forcpumask_next(). The warning is valid. If we are at the very last cpu, what for we look for next?
Consider:
nr_cpu_ids=4
A)
cpumask: 0.1.1.0
CPU 0 1 2 3
n ^
result: nr_cpu_ids
B)
cpumask: 0.0.1.1
CPU 0 1 2 3
n ^
result: nr_cpu_ids + WARN
Both scenarios are identical from a user perspective: a valid CPU number was passed in (either from smp_processor_id() or from a previous call to cpumask_next*()), but there are no more bits set in the cpumask. There's no more CPUs to search for in both scenarios, but only one produces as WARN.
Quoted 31 lines
The snippet above should be fixed like this:
if (--hctx->next_cpu_batch <= 0) { select_cpu: if (next_cpu == nr_cpu_ids - 1) next_cpu = nr_cpu_ids; else next_cpu = cpumask_next_and(next_cpu, hctx->cpumask, cpu_online_mask); if (next_cpu >= nr_cpu_ids) next_cpu = blk_mq_first_mapped_cpu(hctx); hctx->next_cpu_batch = BLK_MQ_CPU_WORK_BATCH; }The original motivation for this special shifted semantics was to avoid passing '+1' in
cpumask_next()everywhere where it's used to iterate over cpumask. This is especially ugly because it brings negative semantics in such a simple thing like an index, and makes people confused. It was a bad decision, but now it's so broadly used that we have to live with it.The strategy to mitigate this is to minimize using of that 'special' functions. They all are cpumask_next()-like. In this series I reworked
for_each_cpu()to not usecpumask_next().Often,
cpumask_next()is a part of opencodedfor_each_cpu(), and this is relatively easy to fix. In case ofblk_mq_hctx_next_cpu()that you mentioned above,cpumask_next_and()usage looks unavoidable, and there's nothing to do with that, except that being careful.
Quoted 8 lines
It didn't trigger the warning in my test setup, so I didn't fix it. Feel free to submit a patch, if you observe the warning for yourself.
Maybe we should consider
nr_cpu_idsas a special valid index forcpumask_check(), a sign of the end of an array. This would help to silence many warnings, like this one. For now I'm leaning towards that it's more a hack than a meaningful change.
I agree, we definitely want to warn for e.g.
cpumask_set_cpu(nr_cpu_ids, ...);
Could we instead make cpumask_next*() immediately return nr_cpu_ids when passed n=nr_cpu_ids-1?
Also, what about cpumask_next_wrap()? That uses cpumask_next() under the hood and is bound to warn when wrapping after n=nr_cpu_ids-1, I think.
Quoted 2 lines
Thanks,
Yury
Yury Norov
maintainerRe: [PATCH 1/7] cpumask: fix checking valid cpu range
On Fri, Sep 30, 2022 at 06:04:08PM +0100, Valentin Schneider wrote:
[...]
Quoted 21 lines
Quoted 4 lines
next_cpuis a valid CPU number for all, but not forcpumask_next(). The warning is valid. If we are at the very last cpu, what for we look for next?Consider:
nr_cpu_ids=4
A)
cpumask: 0.1.1.0
CPU 0 1 2 3
n ^
result:nr_cpu_idsB)
cpumask: 0.0.1.1
CPU 0 1 2 3
n ^
result:nr_cpu_ids+ WARN
Quoted 4 lines
Both scenarios are identical from a user perspective: a valid CPU number was passed in (either from
smp_processor_id()or from a previous call to cpumask_next*()), but there are no more bits set in the cpumask. There's no more CPUs to search for in both scenarios, but only one produces as WARN.
It seems I have to repeat it for the 3rd time.
cpumask_next() takes shifted cpu index. That's why cpumask_check() must shift the index in the other direction to keep all that checking logic consistent.
This is a bad design, and all users of cpumask_next() must be aware of this pitfall.
[...]
Quoted 6 lines
Quoted 5 lines
Maybe we should consider
nr_cpu_idsas a special valid index forcpumask_check(), a sign of the end of an array. This would help to silence many warnings, like this one. For now I'm leaning towards that it's more a hack than a meaningful change.
Quoted 6 lines
I agree, we definitely want to warn for e.g.
cpumask_set_cpu(nr_cpu_ids, ...);Could we instead make cpumask_next*() immediately return
nr_cpu_idswhen passed n=nr_cpu_ids-1?
This is what FIND_NEXT_BIT() does. If you're suggesting to silence the warning - what for do we need it at all?
Quoted 2 lines
Also, what about
cpumask_next_wrap()? That usescpumask_next()under the hood and is bound to warn when wrapping after n=nr_cpu_ids-1, I think.
I'm working on a fix for it. Hopefully will merge it in next window.
Thanks,
Yury
Yury Norov
maintainerPATCH 2/7 net: fix cpu_max_bits_warn() usage in netif_attrmask_next{,_and}
In reply to Yury NorovThe functions require to be passed with a cpu index prior to one that is the first to start search, so the valid input range is [-1, nr_cpu_ids-1). However, the code checks against [-1, nr_cpu_ids).
--- include/linux/netdevice.h | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-)
include/linux/netdevice.h +4 -6
@@ -3643,9 +3643,8 @@ static inline bool netif_attr_test_online(unsigned long j,static inline unsigned int netif_attrmask_next (int n,const unsigned long *srcp,unsigned int nr_bits) { -/* -1 is a legal arg here. */ -if (n !=-1 ) -cpu_max_bits_warn (n, nr_bits); +/* n is a prior cpu */ +cpu_max_bits_warn (n +1 , nr_bits);if (srcp)return find_next_bit (srcp, nr_bits, n +1 );
@@ -3666,9 +3665,8 @@ static inline int netif_attrmask_next_and(int n, const unsigned long *src1p,const unsigned long *src2p, unsigned int nr_bits) { - /* -1 is a legal arg here. */ - if (n != -1) - cpu_max_bits_warn(n, nr_bits); +/* n is a prior cpu */ + cpu_max_bits_warn(n + 1, nr_bits);if (src1p && src2p)return find_next_and_bit (src1p, src2p, nr_bits, n +1 );
Signature · 1 line
2.34.1
Re: [PATCH 2/7] net: fix cpu_max_bits_warn() usage in netif_attrmask_next{,_and}
Quoted 3 lines · On Mon, 19 Sep 2022 14:05:54 -0700 Yury Norov wrote:
The functions require to be passed with a cpu index prior to one that is the first to start search, so the valid input range is [-1, nr_cpu_ids-1). However, the code checks against [-1,
nr_cpu_ids).
Yup, the analysis looks correct:
Yury Norov
maintainerRe: [PATCH 2/7] net: fix cpu_max_bits_warn() usage in netif_attrmask_next{,_and}
Quoted 8 lines · Jakub Kicinski
Quoted 3 lines · On Mon, 19 Sep 2022 14:05:54 -0700 Yury Norov wrote:
The functions require to be passed with a cpu index prior to one that is the first to start search, so the valid input range is [-1, nr_cpu_ids-1). However, the code checks against [-1,
nr_cpu_ids).Yup, the analysis looks correct:
Acked-by Jakub Kicinski <[email protected]>
Thanks. If no other comments from you and others, I'll take the series in bitmap-for-next.
Yury Norov
maintainerPATCH 3/7 cpumask: switch for_each_cpu{,_not} to use for_each_bit()
In reply to Yury NorovThe difference between for_each_cpu() and for_each_set_bit() is that the latter uses cpumask_next() instead of find_next_bit(), and so calls cpumask_check().
This check is useless because the iterator value is not provided by user. It generates false-positives for the very last iteration of for_each_cpu().
--- include/linux/cpumask.h | 12 +++--------- include/linux/find.h | 5 +++++ 2 files changed, 8 insertions(+), 9 deletions(-)
include/linux/cpumask.h +3 -9
@@ -243,9 +243,7 @@ unsigned int cpumask_next_and(int n, const struct cpumask *src1p,* After the loop, cpu is >= nr_cpu_ids. */ #define for_each_cpu (cpu, mask) \ - for ((cpu) = -1; \ - (cpu) = cpumask_next((cpu), (mask)), \ - (cpu) < nr_cpu_ids;) + for_each_set_bit(cpu, cpumask_bits(mask), nr_cpumask_bits)/** * for_each_cpu_not - iterate over every cpu in a complemented mask
@@ -255,9 +253,7 @@ unsigned int cpumask_next_and(int n, const struct cpumask *src1p,* After the loop, cpu is >= nr_cpu_ids. */ #define for_each_cpu_not (cpu, mask) \ - for ((cpu) = -1; \ - (cpu) = cpumask_next_zero((cpu), (mask)), \ - (cpu) < nr_cpu_ids;) + for_each_clear_bit(cpu, cpumask_bits(mask), nr_cpumask_bits)#if NR_CPUS ==1 static inline
@@ -310,9 +306,7 @@ unsigned int __pure cpumask_next_wrap(int n, const struct cpumask *mask, int sta* After the loop, cpu is >= nr_cpu_ids. */ #define for_each_cpu_and (cpu, mask1, mask2) \ - for ((cpu) = -1; \ - (cpu) = cpumask_next_and((cpu), (mask1), (mask2)), \ - (cpu) < nr_cpu_ids;) + for_each_and_bit(cpu, cpumask_bits(mask1), cpumask_bits(mask2), nr_cpumask_bits)/** * cpumask_any_but - return a "random" in a cpumask, but not this one.
include/linux/find.h +5 -0
@@ -390,6 +390,11 @@ unsigned long find_next_bit_le(const void *addr, unsigned (bit) < (size); \ (bit) = find_next_bit((addr), (size), (bit) + 1)) +#define for_each_and_bit (bit, addr1, addr2, size) \ + for ((bit) = find_next_and_bit((addr1), (addr2), (size), 0); \ + (bit) < (size); \ + (bit) = find_next_and_bit((addr1), (addr2), (size), (bit) + 1)) +/* same as for_each_set_bit() but use bit as value to start with */ #define for_each_set_bit_from (bit, addr, size) \ for ((bit) = find_next_bit((addr), (size), (bit)); \
Signature · 1 line
2.34.1
Yury Norov
maintainerPATCH 4/7 lib/find_bit: add find_next{,_and}_bit_wrap
In reply to Yury NorovThe helper is better optimized for the worst case: in case of empty cpumask, current code traverses 2 * size:
next = cpumask_next_and(prev, src1p, src2p); if (next >= nr_cpu_ids) next = cpumask_first_and(src1p, src2p);
At bitmap level we can stop earlier after checking 'size + offset' bits.
--- include/linux/find.h | 46 ++++++++++++++++++++++++++++++++++++++++++++ lib/cpumask.c | 12 +++--------- 2 files changed, 49 insertions(+), 9 deletions(-)
include/linux/find.h +46 -0
@@ -290,6 +290,52 @@ unsigned long find_last_bit(const unsigned long *addr, unsigned long size) }#endif +/** +* find_next_and_bit_wrap - find the next set bit in both memory regions +* @addr1: The first address to base the search on +* @addr2: The second address to base the search on +* @size: The bitmap size in bits +* @offset: The bitnumber to start searching at +* +* Returns the bit number for the next set bit, or first set bit up to @offset +* If no bits are set, returns @size. +*/ +static inline +unsigned long find_next_and_bit_wrap (const unsigned long *addr1, +const unsigned long *addr2, +unsigned long size,unsigned long offset) +{ +unsigned long bit =find_next_and_bit (addr1, addr2, size, offset); + +if (bit < size) +return bit; + + bit =find_first_and_bit (addr1, addr2, offset); +return bit < offset ? bit : size; +} + +/** +* find_next_bit_wrap - find the next set bit in both memory regions +* @addr: The first address to base the search on +* @size: The bitmap size in bits +* @offset: The bitnumber to start searching at +* +* Returns the bit number for the next set bit, or first set bit up to @offset +* If no bits are set, returns @size. +*/ +static inline +unsigned long find_next_bit_wrap (const unsigned long *addr, +unsigned long size,unsigned long offset) +{ +unsigned long bit =find_next_bit (addr, size, offset); + +if (bit < size) +return bit; + + bit =find_first_bit (addr, offset); +return bit < offset ? bit : size; +} +/** * find_next_clump8 - find next 8-bit clump with set bits in a memory region * @clump: location to store copy of found clump
lib/cpumask.c +3 -9
@@ -166,10 +166,8 @@ unsigned int cpumask_any_and_distribute(const struct cpumask *src1p,/* NOTE: our first selection will skip 0. */ prev =__this_cpu_read (distribute_cpu_mask_prev); - next =cpumask_next_and (prev, src1p, src2p); -if (next >= nr_cpu_ids) - next =cpumask_first_and (src1p, src2p); - + next =find_next_and_bit_wrap (cpumask_bits (src1p),cpumask_bits (src2p), + nr_cpumask_bits, prev +1 );if (next < nr_cpu_ids)__this_cpu_write (distribute_cpu_mask_prev, next);
@@ -183,11 +181,7 @@ unsigned int cpumask_any_distribute(const struct cpumask *srcp)/* NOTE: our first selection will skip 0. */ prev =__this_cpu_read (distribute_cpu_mask_prev); - - next =cpumask_next (prev, srcp); -if (next >= nr_cpu_ids) - next =cpumask_first (srcp); - + next =find_next_bit_wrap (cpumask_bits (srcp), nr_cpumask_bits, prev +1 );if (next < nr_cpu_ids)__this_cpu_write (distribute_cpu_mask_prev, next);
Signature · 1 line
2.34.1
Yury Norov
maintainerPATCH 5/7 lib/bitmap: introduce for_each_set_bit_wrap() macro
In reply to Yury NorovAdd for_each_set_bit_wrap() macro and use it in for_each_cpu_wrap(). The new macro is based on __for_each_wrap() iterator, which is simpler and smaller than cpumask_next_wrap().
--- include/linux/cpumask.h | 6 ++---- include/linux/find.h | 39 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 4 deletions(-)
include/linux/cpumask.h +2 -4
@@ -286,10 +286,8 @@ unsigned int __pure cpumask_next_wrap(int n, const struct cpumask *mask, int sta* * After the loop, cpu is >= nr_cpu_ids. */ -#define for_each_cpu_wrap (cpu, mask, start) \ - for ((cpu) = cpumask_next_wrap((start)-1, (mask), (start), false); \ - (cpu) < nr_cpumask_bits; \ - (cpu) = cpumask_next_wrap((cpu), (mask), (start), true)) +#define for_each_cpu_wrap (cpu, mask, start) \ + for_each_set_bit_wrap(cpu, cpumask_bits(mask), nr_cpumask_bits, start)/** * for_each_cpu_and - iterate over every cpu in both masks
include/linux/find.h +39 -0
@@ -336,6 +336,32 @@ unsigned long find_next_bit_wrap(const unsigned long *addr,return bit < offset ? bit : size; } +/* +* Helper for for_each_set_bit_wrap(). Make sure you're doing right thing +* before using it alone. +*/ +static inline +unsigned long __for_each_wrap (const unsigned long *bitmap,unsigned long size, +unsigned long start,unsigned long n) +{ +unsigned long bit; + +/* If not wrapped around */ +if (n > start) { +/* and have a bit, just return it. */ + bit =find_next_bit (bitmap, size, n); +if (bit < size) +return bit; + +/* Otherwise, wrap around and ... */ + n =0 ; + } + +/* Search the other part. */ + bit =find_next_bit (bitmap, start, n); +return bit < start ? bit : size; +} +/** * find_next_clump8 - find next 8-bit clump with set bits in a memory region * @clump: location to store copy of found clump
@@ -514,6 +540,19 @@ unsigned long find_next_bit_le(const void *addr, unsigned (b) = find_next_zero_bit((addr), (size), (e) + 1), \ (e) = find_next_bit((addr), (size), (b) + 1)) +/** +* for_each_set_bit_wrap - iterate over all set bits starting from @start, and +* wrapping around the end of bitmap. +* @bit: offset for current iteration +* @addr: bitmap address to base the search on +* @size: bitmap size in number of bits +* @start: Starting bit for bitmap traversing, wrapping around the bitmap end +*/ +#define for_each_set_bit_wrap (bit, addr, size, start) \ + for ((bit) = find_next_bit_wrap((addr), (size), (start)); \ + (bit) < (size); \ + (bit) = __for_each_wrap((addr), (size), (start), (bit) + 1)) +/** * for_each_set_clump8 - iterate over bitmap for each 8-bit clump with set bits * @start: bit offset to start search and to store the current iteration offset
Signature · 1 line
2.34.1
Yury Norov
maintainerPATCH 6/7 lib/find: optimize for_each() macros
In reply to Yury NorovMoving an iterator of the macros inside conditional part of for-loop helps to generate a better code. It had been first implemented in commit 7baac8b91f9871ba ("cpumask: make for_each_cpu_mask a bit smaller").
Now that cpumask for-loops are the aliases to bitmap loops, it's worth to optimize them the same way.
Bloat-o-meter says:
add/remove: 8/12 grow/shrink: 147/592 up/down: 4876/-24416 (-19540)
--- include/linux/find.h | 56 ++++++++++++++++++++------------------------ 1 file changed, 25 insertions(+), 31 deletions(-)
include/linux/find.h +25 -31
@@ -458,31 +458,25 @@ unsigned long find_next_bit_le(const void *addr, unsigned#endif #define for_each_set_bit (bit, addr, size) \ - for ((bit) = find_next_bit((addr), (size), 0); \ - (bit) < (size); \ - (bit) = find_next_bit((addr), (size), (bit) + 1)) + for ((bit) = 0; (bit) = find_next_bit((addr), (size), (bit)), (bit) < (size); (bit)++)#define for_each_and_bit (bit, addr1, addr2, size) \ - for ((bit) = find_next_and_bit((addr1), (addr2), (size), 0); \ - (bit) < (size); \ - (bit) = find_next_and_bit((addr1), (addr2), (size), (bit) + 1)) + for ((bit) = 0; \ + (bit) = find_next_and_bit((addr1), (addr2), (size), (bit)), (bit) < (size);\ + (bit)++)/* same as for_each_set_bit() but use bit as value to start with */ #define for_each_set_bit_from (bit, addr, size) \ - for ((bit) = find_next_bit((addr), (size), (bit)); \ - (bit) < (size); \ - (bit) = find_next_bit((addr), (size), (bit) + 1)) + for (; (bit) = find_next_bit((addr), (size), (bit)), (bit) < (size); (bit)++)#define for_each_clear_bit (bit, addr, size) \ - for ((bit) = find_next_zero_bit((addr), (size), 0); \ - (bit) < (size); \ - (bit) = find_next_zero_bit((addr), (size), (bit) + 1)) + for ((bit) = 0; \ + (bit) = find_next_zero_bit((addr), (size), (bit)), (bit) < (size); \ + (bit)++)/* same as for_each_clear_bit() but use bit as value to start with */ #define for_each_clear_bit_from (bit, addr, size) \ - for ((bit) = find_next_zero_bit((addr), (size), (bit)); \ - (bit) < (size); \ - (bit) = find_next_zero_bit((addr), (size), (bit) + 1)) + for (; (bit) = find_next_zero_bit((addr), (size), (bit)), (bit) < (size); (bit)++)/** * for_each_set_bitrange - iterate over all set bit ranges [b; e)
@@ -492,11 +486,11 @@ unsigned long find_next_bit_le(const void *addr, unsigned* @size: bitmap size in number of bits */ #define for_each_set_bitrange (b, e, addr, size) \ - for ((b) = find_next_bit((addr), (size), 0), \ - (e) = find_next_zero_bit((addr), (size), (b) + 1); \ + for ((b) = 0; \ + (b) = find_next_bit((addr), (size), b), \ + (e) = find_next_zero_bit((addr), (size), (b) + 1), \ (b) < (size); \ - (b) = find_next_bit((addr), (size), (e) + 1), \ - (e) = find_next_zero_bit((addr), (size), (b) + 1)) + (b) = (e) + 1)/** * for_each_set_bitrange_from - iterate over all set bit ranges [b; e)
@@ -506,11 +500,11 @@ unsigned long find_next_bit_le(const void *addr, unsigned* @size: bitmap size in number of bits */ #define for_each_set_bitrange_from (b, e, addr, size) \ - for ((b) = find_next_bit((addr), (size), (b)), \ - (e) = find_next_zero_bit((addr), (size), (b) + 1); \ + for (; \ + (b) = find_next_bit((addr), (size), (b)), \ + (e) = find_next_zero_bit((addr), (size), (b) + 1), \ (b) < (size); \ - (b) = find_next_bit((addr), (size), (e) + 1), \ - (e) = find_next_zero_bit((addr), (size), (b) + 1)) + (b) = (e) + 1)/** * for_each_clear_bitrange - iterate over all unset bit ranges [b; e)
@@ -520,11 +514,11 @@ unsigned long find_next_bit_le(const void *addr, unsigned* @size: bitmap size in number of bits */ #define for_each_clear_bitrange (b, e, addr, size) \ - for ((b) = find_next_zero_bit((addr), (size), 0), \ - (e) = find_next_bit((addr), (size), (b) + 1); \ + for ((b) = 0; \ + (b) = find_next_zero_bit((addr), (size), (b)), \ + (e) = find_next_bit((addr), (size), (b) + 1), \ (b) < (size); \ - (b) = find_next_zero_bit((addr), (size), (e) + 1), \ - (e) = find_next_bit((addr), (size), (b) + 1)) + (b) = (e) + 1)/** * for_each_clear_bitrange_from - iterate over all unset bit ranges [b; e)
@@ -534,11 +528,11 @@ unsigned long find_next_bit_le(const void *addr, unsigned* @size: bitmap size in number of bits */ #define for_each_clear_bitrange_from (b, e, addr, size) \ - for ((b) = find_next_zero_bit((addr), (size), (b)), \ - (e) = find_next_bit((addr), (size), (b) + 1); \ + for (; \ + (b) = find_next_zero_bit((addr), (size), (b)), \ + (e) = find_next_bit((addr), (size), (b) + 1), \ (b) < (size); \ - (b) = find_next_zero_bit((addr), (size), (e) + 1), \ - (e) = find_next_bit((addr), (size), (b) + 1)) + (b) = (e) + 1)/** * for_each_set_bit_wrap - iterate over all set bits starting from @start, and
Signature · 1 line
2.34.1
Yury Norov
maintainerPATCH 7/7 lib/bitmap: add tests for for_each() loops
In reply to Yury NorovWe have a test for test_for_each_set_clump8 only. Add basic tests for the others.
--- lib/test_bitmap.c | 244 +++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 243 insertions(+), 1 deletion(-)
lib/test_bitmap.c +243 -1
@@ -726,6 +726,239 @@ static void __init test_for_each_set_clump8(void)expect_eq_clump8 (start,CLUMP_EXP_NUMBITS , clump_exp, &clump); } +static void __init test_for_each_set_bit_wrap (void ) +{ +DECLARE_BITMAP (orig,500 ); +DECLARE_BITMAP (copy,500 ); +unsigned int wr, bit; + +bitmap_zero (orig,500 ); + +/* Set individual bits */ +for (bit =0 ; bit <500 ; bit +=10 ) +bitmap_set (orig, bit,1 ); + +/* Set range of bits */ +bitmap_set (orig,100 ,50 ); + +for (wr =0 ; wr <500 ; wr++) { +bitmap_zero (copy,500 ); + +for_each_set_bit_wrap (bit, orig,500 , wr) +bitmap_set (copy, bit,1 ); + +expect_eq_bitmap (orig, copy,500 ); + } +} + +static void __init test_for_each_set_bit (void ) +{ +DECLARE_BITMAP (orig,500 ); +DECLARE_BITMAP (copy,500 ); +unsigned int bit; + +bitmap_zero (orig,500 ); +bitmap_zero (copy,500 ); + +/* Set individual bits */ +for (bit =0 ; bit <500 ; bit +=10 ) +bitmap_set (orig, bit,1 ); + +/* Set range of bits */ +bitmap_set (orig,100 ,50 ); + +for_each_set_bit (bit, orig,500 ) +bitmap_set (copy, bit,1 ); + +expect_eq_bitmap (orig, copy,500 ); +} + +static void __init test_for_each_set_bit_from (void ) +{ +DECLARE_BITMAP (orig,500 ); +DECLARE_BITMAP (copy,500 ); +unsigned int wr, bit; + +bitmap_zero (orig,500 ); + +/* Set individual bits */ +for (bit =0 ; bit <500 ; bit +=10 ) +bitmap_set (orig, bit,1 ); + +/* Set range of bits */ +bitmap_set (orig,100 ,50 ); + +for (wr =0 ; wr <500 ; wr++) { +DECLARE_BITMAP (tmp,500 ); + +bitmap_zero (copy,500 ); + bit = wr; + +for_each_set_bit_from (bit, orig,500 ) +bitmap_set (copy, bit,1 ); + +bitmap_copy (tmp, orig,500 ); +bitmap_clear (tmp,0 , wr); +expect_eq_bitmap (tmp, copy,500 ); + } +} + +static void __init test_for_each_clear_bit (void ) +{ +DECLARE_BITMAP (orig,500 ); +DECLARE_BITMAP (copy,500 ); +unsigned int bit; + +bitmap_fill (orig,500 ); +bitmap_fill (copy,500 ); + +/* Set individual bits */ +for (bit =0 ; bit <500 ; bit +=10 ) +bitmap_clear (orig, bit,1 ); + +/* Set range of bits */ +bitmap_clear (orig,100 ,50 ); + +for_each_clear_bit (bit, orig,500 ) +bitmap_clear (copy, bit,1 ); + +expect_eq_bitmap (orig, copy,500 ); +} + +static void __init test_for_each_clear_bit_from (void ) +{ +DECLARE_BITMAP (orig,500 ); +DECLARE_BITMAP (copy,500 ); +unsigned int wr, bit; + +bitmap_fill (orig,500 ); + +/* Set individual bits */ +for (bit =0 ; bit <500 ; bit +=10 ) +bitmap_clear (orig, bit,1 ); + +/* Set range of bits */ +bitmap_clear (orig,100 ,50 ); + +for (wr =0 ; wr <500 ; wr++) { +DECLARE_BITMAP (tmp,500 ); + +bitmap_fill (copy,500 ); + bit = wr; + +for_each_clear_bit_from (bit, orig,500 ) +bitmap_clear (copy, bit,1 ); + +bitmap_copy (tmp, orig,500 ); +bitmap_set (tmp,0 , wr); +expect_eq_bitmap (tmp, copy,500 ); + } +} + +static void __init test_for_each_set_bitrange (void ) +{ +DECLARE_BITMAP (orig,500 ); +DECLARE_BITMAP (copy,500 ); +unsigned int s, e; + +bitmap_zero (orig,500 ); +bitmap_zero (copy,500 ); + +/* Set individual bits */ +for (s =0 ; s <500 ; s +=10 ) +bitmap_set (orig, s,1 ); + +/* Set range of bits */ +bitmap_set (orig,100 ,50 ); + +for_each_set_bitrange (s, e, orig,500 ) +bitmap_set (copy, s, e-s); + +expect_eq_bitmap (orig, copy,500 ); +} + +static void __init test_for_each_clear_bitrange (void ) +{ +DECLARE_BITMAP (orig,500 ); +DECLARE_BITMAP (copy,500 ); +unsigned int s, e; + +bitmap_fill (orig,500 ); +bitmap_fill (copy,500 ); + +/* Set individual bits */ +for (s =0 ; s <500 ; s +=10 ) +bitmap_clear (orig, s,1 ); + +/* Set range of bits */ +bitmap_clear (orig,100 ,50 ); + +for_each_clear_bitrange (s, e, orig,500 ) +bitmap_clear (copy, s, e-s); + +expect_eq_bitmap (orig, copy,500 ); +} + +static void __init test_for_each_set_bitrange_from (void ) +{ +DECLARE_BITMAP (orig,500 ); +DECLARE_BITMAP (copy,500 ); +unsigned int wr, s, e; + +bitmap_zero (orig,500 ); + +/* Set individual bits */ +for (s =0 ; s <500 ; s +=10 ) +bitmap_set (orig, s,1 ); + +/* Set range of bits */ +bitmap_set (orig,100 ,50 ); + +for (wr =0 ; wr <500 ; wr++) { +DECLARE_BITMAP (tmp,500 ); + +bitmap_zero (copy,500 ); + s = wr; + +for_each_set_bitrange_from (s, e, orig,500 ) +bitmap_set (copy, s, e - s); + +bitmap_copy (tmp, orig,500 ); +bitmap_clear (tmp,0 , wr); +expect_eq_bitmap (tmp, copy,500 ); + } +} + +static void __init test_for_each_clear_bitrange_from (void ) +{ +DECLARE_BITMAP (orig,500 ); +DECLARE_BITMAP (copy,500 ); +unsigned int wr, s, e; + +bitmap_fill (orig,500 ); + +/* Set individual bits */ +for (s =0 ; s <500 ; s +=10 ) +bitmap_clear (orig, s,1 ); + +/* Set range of bits */ +bitmap_set (orig,100 ,50 ); + +for (wr =0 ; wr <500 ; wr++) { +DECLARE_BITMAP (tmp,500 ); + +bitmap_fill (copy,500 ); + s = wr; + +for_each_clear_bitrange_from (s, e, orig,500 ) +bitmap_clear (copy, s, e - s); + +bitmap_copy (tmp, orig,500 ); +bitmap_set (tmp,0 , wr); +expect_eq_bitmap (tmp, copy,500 ); + } +} +struct test_bitmap_cut {unsigned int first;unsigned int cut;
@@ -989,12 +1222,21 @@ static void __init selftest(void)test_bitmap_parselist ();test_bitmap_printlist ();test_mem_optimisations (); - test_for_each_set_clump8();test_bitmap_cut ();test_bitmap_print_buf ();test_bitmap_const_eval ();test_find_nth_bit (); + test_for_each_set_bit(); + test_for_each_set_bit_from(); + test_for_each_clear_bit(); + test_for_each_clear_bit_from(); + test_for_each_set_bitrange(); + test_for_each_clear_bitrange(); + test_for_each_set_bitrange_from(); + test_for_each_clear_bitrange_from(); + test_for_each_set_clump8(); + test_for_each_set_bit_wrap(); }KSTM_MODULE_LOADERS (test_bitmap);
Signature · 1 line
2.34.1
Yury Norov
maintainerPing?
Quoted 29 lines · Yury Norov
After switching cpumask to use
nr_cpu_idsin [1],cpumask_check()started generating many false-positive warnings. There are some more issues with thecpumask_check()that brake it.This series fixes
cpumask_check()mess and addresses most of the false-positive warnings observed on boot ofx86_64and arm64.[1] https://lore.kernel.org/lkml/[email protected]/T/
Yury Norov (7):
cpumask: fix checking valid cpu range
net: fixcpu_max_bits_warn()usage in netif_attrmask_next{,_and}
cpumask: switch for_each_cpu{,_not} to usefor_each_bit()lib/find_bit: add find_next{,_and}_bit_wrap lib/bitmap: introducefor_each_set_bit_wrap()macro lib/find: optimizefor_each()macros
lib/bitmap: add tests forfor_each()iteratorsinclude/linux/cpumask.h | 37 ++---- include/linux/find.h | 140 +++++++++++++++++----- include/linux/netdevice.h | 10 +- lib/cpumask.c | 12 +- lib/test_bitmap.c | 244 +++++++++++++++++++++++++++++++++++++- 5 files changed, 375 insertions(+), 68 deletions(-)--
2.34.1
Jakub Kicinski
maintainerQuoted 1 line · On Sun, 25 Sep 2022 08:47:24 -0700 Yury Norov wrote:
Ping?
Sugar sweet, you really need to say more than ping. You put the entire recipient list in the To:, I have no idea what kind of feedback you expect and from whom.
Yury Norov
maintainerQuoted 6 lines · Jakub Kicinski
Quoted 1 line · On Sun, 25 Sep 2022 08:47:24 -0700 Yury Norov wrote:
Ping?
Sugar sweet, you really need to say more than ping. You put the entire recipient list in the To:, I have no idea what kind of feedback you expect and from whom.
From you I'd like to have a feedback on patch #2