Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

sbitmap: add sbitmap_find_bit to remove repeat code in __sbitmap_get/__sbitmap_get_shallow

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>

authored by

Kemeng Shi and committed by
Jens Axboe
678418c6 08470a98

+35 -39
+35 -39
lib/sbitmap.c
··· 186 186 return nr; 187 187 } 188 188 189 + static int sbitmap_find_bit(struct sbitmap *sb, 190 + unsigned int depth, 191 + unsigned int index, 192 + unsigned int alloc_hint, 193 + bool wrap) 194 + { 195 + unsigned int i; 196 + int nr = -1; 197 + 198 + for (i = 0; i < sb->map_nr; i++) { 199 + nr = sbitmap_find_bit_in_word(&sb->map[index], 200 + min_t(unsigned int, 201 + __map_depth(sb, index), 202 + depth), 203 + alloc_hint, wrap); 204 + 205 + if (nr != -1) { 206 + nr += index << sb->shift; 207 + break; 208 + } 209 + 210 + /* Jump to next index. */ 211 + alloc_hint = 0; 212 + if (++index >= sb->map_nr) 213 + index = 0; 214 + } 215 + 216 + return nr; 217 + } 218 + 189 219 static int __sbitmap_get(struct sbitmap *sb, unsigned int alloc_hint) 190 220 { 191 - unsigned int i, index; 192 - int nr = -1; 221 + unsigned int index; 193 222 194 223 index = SB_NR_TO_INDEX(sb, alloc_hint); 195 224 ··· 232 203 else 233 204 alloc_hint = 0; 234 205 235 - for (i = 0; i < sb->map_nr; i++) { 236 - nr = sbitmap_find_bit_in_word(&sb->map[index], 237 - __map_depth(sb, index), 238 - alloc_hint, !sb->round_robin); 239 - if (nr != -1) { 240 - nr += index << sb->shift; 241 - break; 242 - } 243 - 244 - /* Jump to next index. */ 245 - alloc_hint = 0; 246 - if (++index >= sb->map_nr) 247 - index = 0; 248 - } 249 - 250 - return nr; 206 + return sbitmap_find_bit(sb, UINT_MAX, index, alloc_hint, 207 + !sb->round_robin); 251 208 } 252 209 253 210 int sbitmap_get(struct sbitmap *sb) ··· 257 242 unsigned int alloc_hint, 258 243 unsigned long shallow_depth) 259 244 { 260 - unsigned int i, index; 261 - int nr = -1; 245 + unsigned int index; 262 246 263 247 index = SB_NR_TO_INDEX(sb, alloc_hint); 264 248 alloc_hint = SB_NR_TO_BIT(sb, alloc_hint); 265 249 266 - for (i = 0; i < sb->map_nr; i++) { 267 - nr = sbitmap_find_bit_in_word(&sb->map[index], 268 - min_t(unsigned int, 269 - __map_depth(sb, index), 270 - shallow_depth), 271 - alloc_hint, true); 272 - 273 - if (nr != -1) { 274 - nr += index << sb->shift; 275 - break; 276 - } 277 - 278 - /* Jump to next index. */ 279 - alloc_hint = 0; 280 - if (++index >= sb->map_nr) 281 - index = 0; 282 - } 283 - 284 - return nr; 250 + return sbitmap_find_bit(sb, shallow_depth, index, alloc_hint, true); 285 251 } 286 252 287 253 int sbitmap_get_shallow(struct sbitmap *sb, unsigned long shallow_depth)