Skip to content

Commit 0de2fb1

Browse files
Kemeng Shigregkh
Kemeng Shi
authored andcommitted
sbitmap: rewrite sbitmap_find_bit_in_index to reduce repeat code
[ Upstream commit 08470a9 ] Rewrite sbitmap_find_bit_in_index as following: 1. Rename sbitmap_find_bit_in_index to sbitmap_find_bit_in_word 2. Accept "struct sbitmap_word *" directly instead of accepting "struct sbitmap *" and "int index" to get "struct sbitmap_word *". 3. Accept depth/shallow_depth and wrap for __sbitmap_get_word from caller to support need of both __sbitmap_get_shallow and __sbitmap_get. With helper function sbitmap_find_bit_in_word, we can remove repeat code in __sbitmap_get_shallow to find bit considring deferred clear. Reviewed-by: Jan Kara <[email protected]> Signed-off-by: Kemeng Shi <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Jens Axboe <[email protected]> Stable-dep-of: 72d04bd ("sbitmap: fix io hung due to race on sbitmap_word::cleared") Signed-off-by: Sasha Levin <[email protected]>
1 parent fda0807 commit 0de2fb1

File tree

1 file changed

+15
-15
lines changed

1 file changed

+15
-15
lines changed

lib/sbitmap.c

+15-15
Original file line numberDiff line numberDiff line change
@@ -167,15 +167,16 @@ static int __sbitmap_get_word(unsigned long *word, unsigned long depth,
167167
return nr;
168168
}
169169

170-
static int sbitmap_find_bit_in_index(struct sbitmap *sb, int index,
171-
unsigned int alloc_hint)
170+
static int sbitmap_find_bit_in_word(struct sbitmap_word *map,
171+
unsigned int depth,
172+
unsigned int alloc_hint,
173+
bool wrap)
172174
{
173-
struct sbitmap_word *map = &sb->map[index];
174175
int nr;
175176

176177
do {
177-
nr = __sbitmap_get_word(&map->word, __map_depth(sb, index),
178-
alloc_hint, !sb->round_robin);
178+
nr = __sbitmap_get_word(&map->word, depth,
179+
alloc_hint, wrap);
179180
if (nr != -1)
180181
break;
181182
if (!sbitmap_deferred_clear(map))
@@ -203,7 +204,9 @@ static int __sbitmap_get(struct sbitmap *sb, unsigned int alloc_hint)
203204
alloc_hint = 0;
204205

205206
for (i = 0; i < sb->map_nr; i++) {
206-
nr = sbitmap_find_bit_in_index(sb, index, alloc_hint);
207+
nr = sbitmap_find_bit_in_word(&sb->map[index],
208+
__map_depth(sb, index),
209+
alloc_hint, !sb->round_robin);
207210
if (nr != -1) {
208211
nr += index << sb->shift;
209212
break;
@@ -246,20 +249,17 @@ static int __sbitmap_get_shallow(struct sbitmap *sb,
246249
alloc_hint = SB_NR_TO_BIT(sb, alloc_hint);
247250

248251
for (i = 0; i < sb->map_nr; i++) {
249-
again:
250-
nr = __sbitmap_get_word(&sb->map[index].word,
251-
min_t(unsigned int,
252-
__map_depth(sb, index),
253-
shallow_depth),
254-
alloc_hint, true);
252+
nr = sbitmap_find_bit_in_word(&sb->map[index],
253+
min_t(unsigned int,
254+
__map_depth(sb, index),
255+
shallow_depth),
256+
alloc_hint, true);
257+
255258
if (nr != -1) {
256259
nr += index << sb->shift;
257260
break;
258261
}
259262

260-
if (sbitmap_deferred_clear(&sb->map[index]))
261-
goto again;
262-
263263
/* Jump to next index. */
264264
alloc_hint = 0;
265265
if (++index >= sb->map_nr)

0 commit comments

Comments
 (0)