Skip to content

Commit 4b2b031

Browse files
Yongpeng Yangaxboe
authored andcommitted
zloop: use READ_ONCE() to read lo->lo_state in queue_rq path
In the queue_rq path, zlo->state is accessed without locking, and direct access may read stale data. This patch uses READ_ONCE() to read zlo->state and data_race() to silence code checkers, and changes all assignments to use WRITE_ONCE(). Reviewed-by: Damien Le Moal <dlemoal@kernel.org> Reviewed-by: Christoph Hellwig <hch@lst.de> Signed-off-by: Yongpeng Yang <yangyongpeng@xiaomi.com> Signed-off-by: Jens Axboe <axboe@kernel.dk>
1 parent 54891a9 commit 4b2b031

1 file changed

Lines changed: 4 additions & 4 deletions

File tree

drivers/block/zloop.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -697,7 +697,7 @@ static blk_status_t zloop_queue_rq(struct blk_mq_hw_ctx *hctx,
697697
struct zloop_cmd *cmd = blk_mq_rq_to_pdu(rq);
698698
struct zloop_device *zlo = rq->q->queuedata;
699699

700-
if (zlo->state == Zlo_deleting)
700+
if (data_race(READ_ONCE(zlo->state)) == Zlo_deleting)
701701
return BLK_STS_IOERR;
702702

703703
/*
@@ -1002,7 +1002,7 @@ static int zloop_ctl_add(struct zloop_options *opts)
10021002
ret = -ENOMEM;
10031003
goto out;
10041004
}
1005-
zlo->state = Zlo_creating;
1005+
WRITE_ONCE(zlo->state, Zlo_creating);
10061006

10071007
ret = mutex_lock_killable(&zloop_ctl_mutex);
10081008
if (ret)
@@ -1113,7 +1113,7 @@ static int zloop_ctl_add(struct zloop_options *opts)
11131113
}
11141114

11151115
mutex_lock(&zloop_ctl_mutex);
1116-
zlo->state = Zlo_live;
1116+
WRITE_ONCE(zlo->state, Zlo_live);
11171117
mutex_unlock(&zloop_ctl_mutex);
11181118

11191119
pr_info("zloop: device %d, %u zones of %llu MiB, %u B block size\n",
@@ -1177,7 +1177,7 @@ static int zloop_ctl_remove(struct zloop_options *opts)
11771177
ret = -EINVAL;
11781178
} else {
11791179
idr_remove(&zloop_index_idr, zlo->id);
1180-
zlo->state = Zlo_deleting;
1180+
WRITE_ONCE(zlo->state, Zlo_deleting);
11811181
}
11821182

11831183
mutex_unlock(&zloop_ctl_mutex);

0 commit comments

Comments
 (0)