Skip to content

Commit f6fb166

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 9ceb524 commit f6fb166

File tree

1 file changed

+4
-4
lines changed

1 file changed

+4
-4
lines changed

lib/sbitmap.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -499,18 +499,18 @@ unsigned long __sbitmap_queue_get_batch(struct sbitmap_queue *sbq, int nr_tags,
499499
struct sbitmap_word *map = &sb->map[index];
500500
unsigned long get_mask;
501501
unsigned int map_depth = __map_depth(sb, index);
502+
unsigned long val;
502503

503504
sbitmap_deferred_clear(map);
504-
if (map->word == (1UL << (map_depth - 1)) - 1)
505+
val = READ_ONCE(map->word);
506+
if (val == (1UL << (map_depth - 1)) - 1)
505507
goto next;
506508

507-
nr = find_first_zero_bit(&map->word, map_depth);
509+
nr = find_first_zero_bit(&val, map_depth);
508510
if (nr + nr_tags <= map_depth) {
509511
atomic_long_t *ptr = (atomic_long_t *) &map->word;
510-
unsigned long val;
511512

512513
get_mask = ((1UL << nr_tags) - 1) << nr;
513-
val = READ_ONCE(map->word);
514514
while (!atomic_long_try_cmpxchg(ptr, &val,
515515
get_mask | val))
516516
;

0 commit comments

Comments
 (0)