Commit d7621c28 authored by Stefano Garzarella's avatar Stefano Garzarella Committed by Michael S. Tsirkin

vdpa_sim: replace the spinlock with a mutex to protect the state

The spinlock we use to protect the state of the simulator is sometimes
held for a long time (for example, when devices handle requests).

This also prevents us from calling functions that might sleep (such as
kthread_flush_work() in the next patch), and thus having to release
and retake the lock.

For these reasons, let's replace the spinlock with a mutex that gives
us more flexibility.
Suggested-by: default avatarJason Wang <jasowang@redhat.com>
Acked-by: default avatarJason Wang <jasowang@redhat.com>
Signed-off-by: default avatarStefano Garzarella <sgarzare@redhat.com>
Message-Id: <20230404131730.45920-1-sgarzare@redhat.com>
Signed-off-by: default avatarMichael S. Tsirkin <mst@redhat.com>
parent 76acfa7b
...@@ -178,7 +178,7 @@ struct vdpasim *vdpasim_create(struct vdpasim_dev_attr *dev_attr, ...@@ -178,7 +178,7 @@ struct vdpasim *vdpasim_create(struct vdpasim_dev_attr *dev_attr,
if (IS_ERR(vdpasim->worker)) if (IS_ERR(vdpasim->worker))
goto err_iommu; goto err_iommu;
spin_lock_init(&vdpasim->lock); mutex_init(&vdpasim->mutex);
spin_lock_init(&vdpasim->iommu_lock); spin_lock_init(&vdpasim->iommu_lock);
dev->dma_mask = &dev->coherent_dma_mask; dev->dma_mask = &dev->coherent_dma_mask;
...@@ -286,13 +286,13 @@ static void vdpasim_set_vq_ready(struct vdpa_device *vdpa, u16 idx, bool ready) ...@@ -286,13 +286,13 @@ static void vdpasim_set_vq_ready(struct vdpa_device *vdpa, u16 idx, bool ready)
struct vdpasim_virtqueue *vq = &vdpasim->vqs[idx]; struct vdpasim_virtqueue *vq = &vdpasim->vqs[idx];
bool old_ready; bool old_ready;
spin_lock(&vdpasim->lock); mutex_lock(&vdpasim->mutex);
old_ready = vq->ready; old_ready = vq->ready;
vq->ready = ready; vq->ready = ready;
if (vq->ready && !old_ready) { if (vq->ready && !old_ready) {
vdpasim_queue_ready(vdpasim, idx); vdpasim_queue_ready(vdpasim, idx);
} }
spin_unlock(&vdpasim->lock); mutex_unlock(&vdpasim->mutex);
} }
static bool vdpasim_get_vq_ready(struct vdpa_device *vdpa, u16 idx) static bool vdpasim_get_vq_ready(struct vdpa_device *vdpa, u16 idx)
...@@ -310,9 +310,9 @@ static int vdpasim_set_vq_state(struct vdpa_device *vdpa, u16 idx, ...@@ -310,9 +310,9 @@ static int vdpasim_set_vq_state(struct vdpa_device *vdpa, u16 idx,
struct vdpasim_virtqueue *vq = &vdpasim->vqs[idx]; struct vdpasim_virtqueue *vq = &vdpasim->vqs[idx];
struct vringh *vrh = &vq->vring; struct vringh *vrh = &vq->vring;
spin_lock(&vdpasim->lock); mutex_lock(&vdpasim->mutex);
vrh->last_avail_idx = state->split.avail_index; vrh->last_avail_idx = state->split.avail_index;
spin_unlock(&vdpasim->lock); mutex_unlock(&vdpasim->mutex);
return 0; return 0;
} }
...@@ -409,9 +409,9 @@ static u8 vdpasim_get_status(struct vdpa_device *vdpa) ...@@ -409,9 +409,9 @@ static u8 vdpasim_get_status(struct vdpa_device *vdpa)
struct vdpasim *vdpasim = vdpa_to_sim(vdpa); struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
u8 status; u8 status;
spin_lock(&vdpasim->lock); mutex_lock(&vdpasim->mutex);
status = vdpasim->status; status = vdpasim->status;
spin_unlock(&vdpasim->lock); mutex_unlock(&vdpasim->mutex);
return status; return status;
} }
...@@ -420,19 +420,19 @@ static void vdpasim_set_status(struct vdpa_device *vdpa, u8 status) ...@@ -420,19 +420,19 @@ static void vdpasim_set_status(struct vdpa_device *vdpa, u8 status)
{ {
struct vdpasim *vdpasim = vdpa_to_sim(vdpa); struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
spin_lock(&vdpasim->lock); mutex_lock(&vdpasim->mutex);
vdpasim->status = status; vdpasim->status = status;
spin_unlock(&vdpasim->lock); mutex_unlock(&vdpasim->mutex);
} }
static int vdpasim_reset(struct vdpa_device *vdpa) static int vdpasim_reset(struct vdpa_device *vdpa)
{ {
struct vdpasim *vdpasim = vdpa_to_sim(vdpa); struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
spin_lock(&vdpasim->lock); mutex_lock(&vdpasim->mutex);
vdpasim->status = 0; vdpasim->status = 0;
vdpasim_do_reset(vdpasim); vdpasim_do_reset(vdpasim);
spin_unlock(&vdpasim->lock); mutex_unlock(&vdpasim->mutex);
return 0; return 0;
} }
...@@ -441,9 +441,9 @@ static int vdpasim_suspend(struct vdpa_device *vdpa) ...@@ -441,9 +441,9 @@ static int vdpasim_suspend(struct vdpa_device *vdpa)
{ {
struct vdpasim *vdpasim = vdpa_to_sim(vdpa); struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
spin_lock(&vdpasim->lock); mutex_lock(&vdpasim->mutex);
vdpasim->running = false; vdpasim->running = false;
spin_unlock(&vdpasim->lock); mutex_unlock(&vdpasim->mutex);
return 0; return 0;
} }
...@@ -453,7 +453,7 @@ static int vdpasim_resume(struct vdpa_device *vdpa) ...@@ -453,7 +453,7 @@ static int vdpasim_resume(struct vdpa_device *vdpa)
struct vdpasim *vdpasim = vdpa_to_sim(vdpa); struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
int i; int i;
spin_lock(&vdpasim->lock); mutex_lock(&vdpasim->mutex);
vdpasim->running = true; vdpasim->running = true;
if (vdpasim->pending_kick) { if (vdpasim->pending_kick) {
...@@ -464,7 +464,7 @@ static int vdpasim_resume(struct vdpa_device *vdpa) ...@@ -464,7 +464,7 @@ static int vdpasim_resume(struct vdpa_device *vdpa)
vdpasim->pending_kick = false; vdpasim->pending_kick = false;
} }
spin_unlock(&vdpasim->lock); mutex_unlock(&vdpasim->mutex);
return 0; return 0;
} }
...@@ -536,14 +536,14 @@ static int vdpasim_set_group_asid(struct vdpa_device *vdpa, unsigned int group, ...@@ -536,14 +536,14 @@ static int vdpasim_set_group_asid(struct vdpa_device *vdpa, unsigned int group,
iommu = &vdpasim->iommu[asid]; iommu = &vdpasim->iommu[asid];
spin_lock(&vdpasim->lock); mutex_lock(&vdpasim->mutex);
for (i = 0; i < vdpasim->dev_attr.nvqs; i++) for (i = 0; i < vdpasim->dev_attr.nvqs; i++)
if (vdpasim_get_vq_group(vdpa, i) == group) if (vdpasim_get_vq_group(vdpa, i) == group)
vringh_set_iotlb(&vdpasim->vqs[i].vring, iommu, vringh_set_iotlb(&vdpasim->vqs[i].vring, iommu,
&vdpasim->iommu_lock); &vdpasim->iommu_lock);
spin_unlock(&vdpasim->lock); mutex_unlock(&vdpasim->mutex);
return 0; return 0;
} }
......
...@@ -60,8 +60,8 @@ struct vdpasim { ...@@ -60,8 +60,8 @@ struct vdpasim {
struct kthread_worker *worker; struct kthread_worker *worker;
struct kthread_work work; struct kthread_work work;
struct vdpasim_dev_attr dev_attr; struct vdpasim_dev_attr dev_attr;
/* spinlock to synchronize virtqueue state */ /* mutex to synchronize virtqueue state */
spinlock_t lock; struct mutex mutex;
/* virtio config according to device type */ /* virtio config according to device type */
void *config; void *config;
struct vhost_iotlb *iommu; struct vhost_iotlb *iommu;
......
...@@ -290,7 +290,7 @@ static void vdpasim_blk_work(struct vdpasim *vdpasim) ...@@ -290,7 +290,7 @@ static void vdpasim_blk_work(struct vdpasim *vdpasim)
bool reschedule = false; bool reschedule = false;
int i; int i;
spin_lock(&vdpasim->lock); mutex_lock(&vdpasim->mutex);
if (!(vdpasim->status & VIRTIO_CONFIG_S_DRIVER_OK)) if (!(vdpasim->status & VIRTIO_CONFIG_S_DRIVER_OK))
goto out; goto out;
...@@ -321,7 +321,7 @@ static void vdpasim_blk_work(struct vdpasim *vdpasim) ...@@ -321,7 +321,7 @@ static void vdpasim_blk_work(struct vdpasim *vdpasim)
} }
} }
out: out:
spin_unlock(&vdpasim->lock); mutex_unlock(&vdpasim->mutex);
if (reschedule) if (reschedule)
vdpasim_schedule_work(vdpasim); vdpasim_schedule_work(vdpasim);
......
...@@ -201,7 +201,7 @@ static void vdpasim_net_work(struct vdpasim *vdpasim) ...@@ -201,7 +201,7 @@ static void vdpasim_net_work(struct vdpasim *vdpasim)
u64 rx_drops = 0, rx_overruns = 0, rx_errors = 0, tx_errors = 0; u64 rx_drops = 0, rx_overruns = 0, rx_errors = 0, tx_errors = 0;
int err; int err;
spin_lock(&vdpasim->lock); mutex_lock(&vdpasim->mutex);
if (!vdpasim->running) if (!vdpasim->running)
goto out; goto out;
...@@ -264,7 +264,7 @@ static void vdpasim_net_work(struct vdpasim *vdpasim) ...@@ -264,7 +264,7 @@ static void vdpasim_net_work(struct vdpasim *vdpasim)
} }
out: out:
spin_unlock(&vdpasim->lock); mutex_unlock(&vdpasim->mutex);
u64_stats_update_begin(&net->tx_stats.syncp); u64_stats_update_begin(&net->tx_stats.syncp);
net->tx_stats.pkts += tx_pkts; net->tx_stats.pkts += tx_pkts;
......
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