Message ID | 20230720094555.1397621-2-chengming.zhou@linux.dev (mailing list archive) |
---|---|
State | New, archived |
Headers | show |
Series | sbitmap: fix offset hint wrap and some optimizations | expand |
chengming.zhou@linux.dev writes: > From: Chengming Zhou <zhouchengming@bytedance.com> > > ``` > hint = nr + 1; > if (hint >= depth - 1) > hint = 0; > ``` > > Now we wrap the hint to 0 in the failure case, but: > 1. hint == depth - 1, is actually an available offset hint, which > we shouldn't wrap hint to 0. > 2. In the strict round_robin non-wrap case, we shouldn't wrap at all. > > ``` > wrap = wrap && hint; > ``` > > We only need to check wrap based on the original hint ( > 0), don't need > to recheck the new hint which maybe updated in the failure case. > Also delete the mismatched comments by the way. > > Signed-off-by: Chengming Zhou <zhouchengming@bytedance.com> > --- > lib/sbitmap.c | 16 ++++++++-------- > 1 file changed, 8 insertions(+), 8 deletions(-) > > diff --git a/lib/sbitmap.c b/lib/sbitmap.c > index eff4e42c425a..5ed6c2adf58e 100644 > --- a/lib/sbitmap.c > +++ b/lib/sbitmap.c > @@ -144,12 +144,7 @@ static int __sbitmap_get_word(unsigned long *word, unsigned long depth, > while (1) { > nr = find_next_zero_bit(word, depth, hint); > if (unlikely(nr >= depth)) { > - /* > - * We started with an offset, and we didn't reset the > - * offset to 0 in a failure case, so start from 0 to > - * exhaust the map. > - */ > - if (hint && wrap) { > + if (wrap) { > hint = 0; > continue; I think this is wrong. If you start with an offset in the wrap case and the bitmap is completely full this will become busy wait until a bit is available. The hint check is what make you break out of the loop early, after wrapping, re-walking the entire bitmap and failing to find any available space. > @@ -160,8 +155,13 @@ static int __sbitmap_get_word(unsigned long *word, unsigned long depth, > break; > > hint = nr + 1; > - if (hint >= depth - 1) > - hint = 0; > + if (hint >= depth) { > + if (wrap) { > + hint = 0; > + continue; > + } > + return -1; > + } > } > > return nr;
On 2023/7/21 03:06, Gabriel Krisman Bertazi wrote: > chengming.zhou@linux.dev writes: > >> From: Chengming Zhou <zhouchengming@bytedance.com> >> >> ``` >> hint = nr + 1; >> if (hint >= depth - 1) >> hint = 0; >> ``` >> >> Now we wrap the hint to 0 in the failure case, but: >> 1. hint == depth - 1, is actually an available offset hint, which >> we shouldn't wrap hint to 0. >> 2. In the strict round_robin non-wrap case, we shouldn't wrap at all. >> >> ``` >> wrap = wrap && hint; >> ``` >> >> We only need to check wrap based on the original hint ( > 0), don't need >> to recheck the new hint which maybe updated in the failure case. >> Also delete the mismatched comments by the way. >> >> Signed-off-by: Chengming Zhou <zhouchengming@bytedance.com> >> --- >> lib/sbitmap.c | 16 ++++++++-------- >> 1 file changed, 8 insertions(+), 8 deletions(-) >> >> diff --git a/lib/sbitmap.c b/lib/sbitmap.c >> index eff4e42c425a..5ed6c2adf58e 100644 >> --- a/lib/sbitmap.c >> +++ b/lib/sbitmap.c >> @@ -144,12 +144,7 @@ static int __sbitmap_get_word(unsigned long *word, unsigned long depth, >> while (1) { >> nr = find_next_zero_bit(word, depth, hint); >> if (unlikely(nr >= depth)) { >> - /* >> - * We started with an offset, and we didn't reset the >> - * offset to 0 in a failure case, so start from 0 to >> - * exhaust the map. >> - */ >> - if (hint && wrap) { >> + if (wrap) { >> hint = 0; >> continue; > > I think this is wrong. If you start with an offset in the wrap case and > the bitmap is completely full this will become busy wait until a bit is > available. The hint check is what make you break out of the loop early, > after wrapping, re-walking the entire bitmap and failing to find any > available space. Ah yes, you are right, thanks for your explanation. Here we need to check "hint && wrap" to avoid wrap repeatedly. Will drop this change in the next version. > >> @@ -160,8 +155,13 @@ static int __sbitmap_get_word(unsigned long *word, unsigned long depth, >> break; >> >> hint = nr + 1; Here we overwrite hint, may cause repeated wrap. So I think it's clearer that we set "wrap" to false after we wrap? ``` if (wrap) { wrap = false; hint = 0; continue; } ``` Thanks! >> - if (hint >= depth - 1) >> - hint = 0; >> + if (hint >= depth) { >> + if (wrap) { >> + hint = 0; >> + continue; >> + } >> + return -1; >> + } >> } >> >> return nr; >
diff --git a/lib/sbitmap.c b/lib/sbitmap.c index eff4e42c425a..5ed6c2adf58e 100644 --- a/lib/sbitmap.c +++ b/lib/sbitmap.c @@ -144,12 +144,7 @@ static int __sbitmap_get_word(unsigned long *word, unsigned long depth, while (1) { nr = find_next_zero_bit(word, depth, hint); if (unlikely(nr >= depth)) { - /* - * We started with an offset, and we didn't reset the - * offset to 0 in a failure case, so start from 0 to - * exhaust the map. - */ - if (hint && wrap) { + if (wrap) { hint = 0; continue; } @@ -160,8 +155,13 @@ static int __sbitmap_get_word(unsigned long *word, unsigned long depth, break; hint = nr + 1; - if (hint >= depth - 1) - hint = 0; + if (hint >= depth) { + if (wrap) { + hint = 0; + continue; + } + return -1; + } } return nr;