Skip to content

Commit aba6f11

Browse files
linke ligregkh
linke li
authored andcommitted
sbitmap: use READ_ONCE to access map->word
[ Upstream commit 6ad0d7e ] 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]> Stable-dep-of: 72d04bd ("sbitmap: fix io hung due to race on sbitmap_word::cleared") Signed-off-by: Sasha Levin <[email protected]>
1 parent 0de2fb1 commit aba6f11

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
@@ -503,18 +503,18 @@ unsigned long __sbitmap_queue_get_batch(struct sbitmap_queue *sbq, int nr_tags,
503503
struct sbitmap_word *map = &sb->map[index];
504504
unsigned long get_mask;
505505
unsigned int map_depth = __map_depth(sb, index);
506+
unsigned long val;
506507

507508
sbitmap_deferred_clear(map);
508-
if (map->word == (1UL << (map_depth - 1)) - 1)
509+
val = READ_ONCE(map->word);
510+
if (val == (1UL << (map_depth - 1)) - 1)
509511
goto next;
510512

511-
nr = find_first_zero_bit(&map->word, map_depth);
513+
nr = find_first_zero_bit(&val, map_depth);
512514
if (nr + nr_tags <= map_depth) {
513515
atomic_long_t *ptr = (atomic_long_t *) &map->word;
514-
unsigned long val;
515516

516517
get_mask = ((1UL << nr_tags) - 1) << nr;
517-
val = READ_ONCE(map->word);
518518
while (!atomic_long_try_cmpxchg(ptr, &val,
519519
get_mask | val))
520520
;

0 commit comments

Comments
 (0)