Skip to content

Commit 846661b

Browse files
committed
sbitmap: add sbitmap_find_bit to remove repeat code in __sbitmap_get/__sbitmap_get_shallow
jira LE-4066 Rebuild_History Non-Buildable kernel-4.18.0-553.72.1.el8_10 commit-author Kemeng Shi <shikemeng@huaweicloud.com> commit 678418c There are three differences between __sbitmap_get and __sbitmap_get_shallow when searching free bit: 1. __sbitmap_get_shallow limit number of bit to search per word. __sbitmap_get has no such limit. 2. __sbitmap_get_shallow always searches with wrap set. __sbitmap_get set wrap according to round_robin. 3. __sbitmap_get_shallow always searches from first bit in first word. __sbitmap_get searches from first bit when round_robin is not set otherwise searches from SB_NR_TO_BIT(sb, alloc_hint). Add helper function sbitmap_find_bit function to do common search while accept "limit depth per word", "wrap flag" and "first bit to search" from caller to support the need of both __sbitmap_get and __sbitmap_get_shallow. Reviewed-by: Jan Kara <jack@suse.cz> Signed-off-by: Kemeng Shi <shikemeng@huaweicloud.com> Link: https://lore.kernel.org/r/20230116205059.3821738-5-shikemeng@huaweicloud.com Signed-off-by: Jens Axboe <axboe@kernel.dk> (cherry picked from commit 678418c) Signed-off-by: Jonathan Maple <jmaple@ciq.com>
1 parent cadcdd7 commit 846661b

File tree

1 file changed

+33
-37
lines changed

1 file changed

+33
-37
lines changed

lib/sbitmap.c

Lines changed: 33 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -206,27 +206,22 @@ static int sbitmap_find_bit_in_word(struct sbitmap_word *map,
206206
return nr;
207207
}
208208

209-
static int __sbitmap_get(struct sbitmap *sb, unsigned int alloc_hint)
209+
static int sbitmap_find_bit(struct sbitmap *sb,
210+
unsigned int depth,
211+
unsigned int index,
212+
unsigned int alloc_hint,
213+
bool wrap)
210214
{
211-
unsigned int i, index;
215+
unsigned int i;
212216
int nr = -1;
213217

214-
index = SB_NR_TO_INDEX(sb, alloc_hint);
215-
216-
/*
217-
* Unless we're doing round robin tag allocation, just use the
218-
* alloc_hint to find the right word index. No point in looping
219-
* twice in find_next_zero_bit() for that case.
220-
*/
221-
if (sb->round_robin)
222-
alloc_hint = SB_NR_TO_BIT(sb, alloc_hint);
223-
else
224-
alloc_hint = 0;
225-
226218
for (i = 0; i < sb->map_nr; i++) {
227219
nr = sbitmap_find_bit_in_word(&sb->map[index],
228-
__map_depth(sb, index),
229-
alloc_hint, !sb->round_robin);
220+
min_t(unsigned int,
221+
__map_depth(sb, index),
222+
depth),
223+
alloc_hint, wrap);
224+
230225
if (nr != -1) {
231226
nr += index << sb->shift;
232227
break;
@@ -241,6 +236,26 @@ static int __sbitmap_get(struct sbitmap *sb, unsigned int alloc_hint)
241236
return nr;
242237
}
243238

239+
static int __sbitmap_get(struct sbitmap *sb, unsigned int alloc_hint)
240+
{
241+
unsigned int index;
242+
243+
index = SB_NR_TO_INDEX(sb, alloc_hint);
244+
245+
/*
246+
* Unless we're doing round robin tag allocation, just use the
247+
* alloc_hint to find the right word index. No point in looping
248+
* twice in find_next_zero_bit() for that case.
249+
*/
250+
if (sb->round_robin)
251+
alloc_hint = SB_NR_TO_BIT(sb, alloc_hint);
252+
else
253+
alloc_hint = 0;
254+
255+
return sbitmap_find_bit(sb, UINT_MAX, index, alloc_hint,
256+
!sb->round_robin);
257+
}
258+
244259
int sbitmap_get(struct sbitmap *sb)
245260
{
246261
unsigned int __percpu *alloc_hint = *SB_ALLOC_HINT_PTR(sb);
@@ -263,31 +278,12 @@ static int __sbitmap_get_shallow(struct sbitmap *sb,
263278
unsigned int alloc_hint,
264279
unsigned long shallow_depth)
265280
{
266-
unsigned int i, index;
267-
int nr = -1;
281+
unsigned int index;
268282

269283
index = SB_NR_TO_INDEX(sb, alloc_hint);
270284
alloc_hint = SB_NR_TO_BIT(sb, alloc_hint);
271285

272-
for (i = 0; i < sb->map_nr; i++) {
273-
nr = sbitmap_find_bit_in_word(&sb->map[index],
274-
min_t(unsigned int,
275-
__map_depth(sb, index),
276-
shallow_depth),
277-
alloc_hint, true);
278-
279-
if (nr != -1) {
280-
nr += index << sb->shift;
281-
break;
282-
}
283-
284-
/* Jump to next index. */
285-
alloc_hint = 0;
286-
if (++index >= sb->map_nr)
287-
index = 0;
288-
}
289-
290-
return nr;
286+
return sbitmap_find_bit(sb, shallow_depth, index, alloc_hint, true);
291287
}
292288

293289
int sbitmap_get_shallow(struct sbitmap *sb, unsigned long shallow_depth)

0 commit comments

Comments
 (0)