Commit 6ad0d7e0 authored by linke li's avatar linke li Committed by Jens Axboe

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: default avatarlinke li <lilinke99@qq.com>
Link: https://lore.kernel.org/r/tencent_0B517C25E519D3D002194E8445E86C04AD0A@qq.comSigned-off-by: default avatarJens Axboe <axboe@kernel.dk>
parent 07d1b998
...@@ -494,18 +494,18 @@ unsigned long __sbitmap_queue_get_batch(struct sbitmap_queue *sbq, int nr_tags, ...@@ -494,18 +494,18 @@ unsigned long __sbitmap_queue_get_batch(struct sbitmap_queue *sbq, int nr_tags,
struct sbitmap_word *map = &sb->map[index]; struct sbitmap_word *map = &sb->map[index];
unsigned long get_mask; unsigned long get_mask;
unsigned int map_depth = __map_depth(sb, index); unsigned int map_depth = __map_depth(sb, index);
unsigned long val;
sbitmap_deferred_clear(map); sbitmap_deferred_clear(map);
if (map->word == (1UL << (map_depth - 1)) - 1) val = READ_ONCE(map->word);
if (val == (1UL << (map_depth - 1)) - 1)
goto next; goto next;
nr = find_first_zero_bit(&map->word, map_depth); nr = find_first_zero_bit(&val, map_depth);
if (nr + nr_tags <= map_depth) { if (nr + nr_tags <= map_depth) {
atomic_long_t *ptr = (atomic_long_t *) &map->word; atomic_long_t *ptr = (atomic_long_t *) &map->word;
unsigned long val;
get_mask = ((1UL << nr_tags) - 1) << nr; get_mask = ((1UL << nr_tags) - 1) << nr;
val = READ_ONCE(map->word);
while (!atomic_long_try_cmpxchg(ptr, &val, while (!atomic_long_try_cmpxchg(ptr, &val,
get_mask | val)) get_mask | val))
; ;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment