Skip to content

Commit 6ad0d7e

Browse files
linke liaxboe
linke li
authored andcommitted
sbitmap: use READ_ONCE to access map->word
In __sbitmap_queue_get_batch(), map->word is read several times, and update atomically using atomic_long_try_cmpxchg(). But the first two read of map->word is not protected. This patch moves the statement val = READ_ONCE(map->word) forward, eliminating unprotected accesses to map->word within the function. It is aimed at reducing the number of benign races reported by KCSAN in order to focus future debugging effort on harmful races. Signed-off-by: linke li <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Jens Axboe <[email protected]>
1 parent 07d1b99 commit 6ad0d7e

File tree

1 file changed

+4
-4
lines changed

1 file changed

+4
-4
lines changed

lib/sbitmap.c

+4-4
Original file line numberDiff line numberDiff line change
@@ -494,18 +494,18 @@ unsigned long __sbitmap_queue_get_batch(struct sbitmap_queue *sbq, int nr_tags,
494494
struct sbitmap_word *map = &sb->map[index];
495495
unsigned long get_mask;
496496
unsigned int map_depth = __map_depth(sb, index);
497+
unsigned long val;
497498

498499
sbitmap_deferred_clear(map);
499-
if (map->word == (1UL << (map_depth - 1)) - 1)
500+
val = READ_ONCE(map->word);
501+
if (val == (1UL << (map_depth - 1)) - 1)
500502
goto next;
501503

502-
nr = find_first_zero_bit(&map->word, map_depth);
504+
nr = find_first_zero_bit(&val, map_depth);
503505
if (nr + nr_tags <= map_depth) {
504506
atomic_long_t *ptr = (atomic_long_t *) &map->word;
505-
unsigned long val;
506507

507508
get_mask = ((1UL << nr_tags) - 1) << nr;
508-
val = READ_ONCE(map->word);
509509
while (!atomic_long_try_cmpxchg(ptr, &val,
510510
get_mask | val))
511511
;

0 commit comments

Comments
 (0)