virtio-blk: add lock to protect s->rq
s->rq is accessed from IO_CODE and GLOBAL_STATE_CODE. Introduce a lock to protect s->rq and eliminate reliance on the AioContext lock. Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com> Message-ID: <20230914140101.1065008-3-stefanha@redhat.com> Reviewed-by: Kevin Wolf <kwolf@redhat.com> Reviewed-by: Eric Blake <eblake@redhat.com> Reviewed-by: Michael S. Tsirkin <mst@redhat.com> Signed-off-by: Kevin Wolf <kwolf@redhat.com>
This commit is contained in:
parent
cd0c0db0aa
commit
9c67f33fca
@ -82,8 +82,11 @@ static int virtio_blk_handle_rw_error(VirtIOBlockReq *req, int error,
|
|||||||
/* Break the link as the next request is going to be parsed from the
|
/* Break the link as the next request is going to be parsed from the
|
||||||
* ring again. Otherwise we may end up doing a double completion! */
|
* ring again. Otherwise we may end up doing a double completion! */
|
||||||
req->mr_next = NULL;
|
req->mr_next = NULL;
|
||||||
|
|
||||||
|
WITH_QEMU_LOCK_GUARD(&s->rq_lock) {
|
||||||
req->next = s->rq;
|
req->next = s->rq;
|
||||||
s->rq = req;
|
s->rq = req;
|
||||||
|
}
|
||||||
} else if (action == BLOCK_ERROR_ACTION_REPORT) {
|
} else if (action == BLOCK_ERROR_ACTION_REPORT) {
|
||||||
virtio_blk_req_complete(req, VIRTIO_BLK_S_IOERR);
|
virtio_blk_req_complete(req, VIRTIO_BLK_S_IOERR);
|
||||||
if (acct_failed) {
|
if (acct_failed) {
|
||||||
@ -1183,10 +1186,13 @@ static void virtio_blk_dma_restart_bh(void *opaque)
|
|||||||
{
|
{
|
||||||
VirtIOBlock *s = opaque;
|
VirtIOBlock *s = opaque;
|
||||||
|
|
||||||
VirtIOBlockReq *req = s->rq;
|
VirtIOBlockReq *req;
|
||||||
MultiReqBuffer mrb = {};
|
MultiReqBuffer mrb = {};
|
||||||
|
|
||||||
|
WITH_QEMU_LOCK_GUARD(&s->rq_lock) {
|
||||||
|
req = s->rq;
|
||||||
s->rq = NULL;
|
s->rq = NULL;
|
||||||
|
}
|
||||||
|
|
||||||
aio_context_acquire(blk_get_aio_context(s->conf.conf.blk));
|
aio_context_acquire(blk_get_aio_context(s->conf.conf.blk));
|
||||||
while (req) {
|
while (req) {
|
||||||
@ -1238,22 +1244,29 @@ static void virtio_blk_reset(VirtIODevice *vdev)
|
|||||||
AioContext *ctx;
|
AioContext *ctx;
|
||||||
VirtIOBlockReq *req;
|
VirtIOBlockReq *req;
|
||||||
|
|
||||||
|
/* Dataplane has stopped... */
|
||||||
|
assert(!s->dataplane_started);
|
||||||
|
|
||||||
|
/* ...but requests may still be in flight. */
|
||||||
ctx = blk_get_aio_context(s->blk);
|
ctx = blk_get_aio_context(s->blk);
|
||||||
aio_context_acquire(ctx);
|
aio_context_acquire(ctx);
|
||||||
blk_drain(s->blk);
|
blk_drain(s->blk);
|
||||||
|
aio_context_release(ctx);
|
||||||
|
|
||||||
/* We drop queued requests after blk_drain() because blk_drain() itself can
|
/* We drop queued requests after blk_drain() because blk_drain() itself can
|
||||||
* produce them. */
|
* produce them. */
|
||||||
|
WITH_QEMU_LOCK_GUARD(&s->rq_lock) {
|
||||||
while (s->rq) {
|
while (s->rq) {
|
||||||
req = s->rq;
|
req = s->rq;
|
||||||
s->rq = req->next;
|
s->rq = req->next;
|
||||||
|
|
||||||
|
/* No other threads can access req->vq here */
|
||||||
virtqueue_detach_element(req->vq, &req->elem, 0);
|
virtqueue_detach_element(req->vq, &req->elem, 0);
|
||||||
|
|
||||||
virtio_blk_free_request(req);
|
virtio_blk_free_request(req);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
aio_context_release(ctx);
|
|
||||||
|
|
||||||
assert(!s->dataplane_started);
|
|
||||||
blk_set_enable_write_cache(s->blk, s->original_wce);
|
blk_set_enable_write_cache(s->blk, s->original_wce);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1443,6 +1456,8 @@ static void virtio_blk_set_status(VirtIODevice *vdev, uint8_t status)
|
|||||||
static void virtio_blk_save_device(VirtIODevice *vdev, QEMUFile *f)
|
static void virtio_blk_save_device(VirtIODevice *vdev, QEMUFile *f)
|
||||||
{
|
{
|
||||||
VirtIOBlock *s = VIRTIO_BLK(vdev);
|
VirtIOBlock *s = VIRTIO_BLK(vdev);
|
||||||
|
|
||||||
|
WITH_QEMU_LOCK_GUARD(&s->rq_lock) {
|
||||||
VirtIOBlockReq *req = s->rq;
|
VirtIOBlockReq *req = s->rq;
|
||||||
|
|
||||||
while (req) {
|
while (req) {
|
||||||
@ -1455,6 +1470,8 @@ static void virtio_blk_save_device(VirtIODevice *vdev, QEMUFile *f)
|
|||||||
qemu_put_virtqueue_element(vdev, f, &req->elem);
|
qemu_put_virtqueue_element(vdev, f, &req->elem);
|
||||||
req = req->next;
|
req = req->next;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
qemu_put_sbyte(f, 0);
|
qemu_put_sbyte(f, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1480,9 +1497,12 @@ static int virtio_blk_load_device(VirtIODevice *vdev, QEMUFile *f,
|
|||||||
|
|
||||||
req = qemu_get_virtqueue_element(vdev, f, sizeof(VirtIOBlockReq));
|
req = qemu_get_virtqueue_element(vdev, f, sizeof(VirtIOBlockReq));
|
||||||
virtio_blk_init_request(s, virtio_get_queue(vdev, vq_idx), req);
|
virtio_blk_init_request(s, virtio_get_queue(vdev, vq_idx), req);
|
||||||
|
|
||||||
|
WITH_QEMU_LOCK_GUARD(&s->rq_lock) {
|
||||||
req->next = s->rq;
|
req->next = s->rq;
|
||||||
s->rq = req;
|
s->rq = req;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
@ -1628,6 +1648,8 @@ static void virtio_blk_device_realize(DeviceState *dev, Error **errp)
|
|||||||
s->host_features);
|
s->host_features);
|
||||||
virtio_init(vdev, VIRTIO_ID_BLOCK, s->config_size);
|
virtio_init(vdev, VIRTIO_ID_BLOCK, s->config_size);
|
||||||
|
|
||||||
|
qemu_mutex_init(&s->rq_lock);
|
||||||
|
|
||||||
s->blk = conf->conf.blk;
|
s->blk = conf->conf.blk;
|
||||||
s->rq = NULL;
|
s->rq = NULL;
|
||||||
s->sector_mask = (s->conf.conf.logical_block_size / BDRV_SECTOR_SIZE) - 1;
|
s->sector_mask = (s->conf.conf.logical_block_size / BDRV_SECTOR_SIZE) - 1;
|
||||||
@ -1679,6 +1701,7 @@ static void virtio_blk_device_unrealize(DeviceState *dev)
|
|||||||
virtio_del_queue(vdev, i);
|
virtio_del_queue(vdev, i);
|
||||||
}
|
}
|
||||||
qemu_coroutine_dec_pool_size(conf->num_queues * conf->queue_size / 2);
|
qemu_coroutine_dec_pool_size(conf->num_queues * conf->queue_size / 2);
|
||||||
|
qemu_mutex_destroy(&s->rq_lock);
|
||||||
blk_ram_registrar_destroy(&s->blk_ram_registrar);
|
blk_ram_registrar_destroy(&s->blk_ram_registrar);
|
||||||
qemu_del_vm_change_state_handler(s->change);
|
qemu_del_vm_change_state_handler(s->change);
|
||||||
blockdev_mark_auto_del(s->blk);
|
blockdev_mark_auto_del(s->blk);
|
||||||
|
@ -54,7 +54,8 @@ struct VirtIOBlockReq;
|
|||||||
struct VirtIOBlock {
|
struct VirtIOBlock {
|
||||||
VirtIODevice parent_obj;
|
VirtIODevice parent_obj;
|
||||||
BlockBackend *blk;
|
BlockBackend *blk;
|
||||||
void *rq;
|
QemuMutex rq_lock;
|
||||||
|
void *rq; /* protected by rq_lock */
|
||||||
VirtIOBlkConf conf;
|
VirtIOBlkConf conf;
|
||||||
unsigned short sector_mask;
|
unsigned short sector_mask;
|
||||||
bool original_wce;
|
bool original_wce;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user