Skip to content

Commit 0943aac

Browse files
YongjiXiemstsirkin
authored andcommitted
vduse: Fix race condition between resetting and irq injecting
The interrupt might be triggered after a reset since there is no synchronization between resetting and irq injecting. And it might break something if the interrupt is delayed until a new round of device initialization. Fixes: c8a6153 ("vduse: Introduce VDUSE - vDPA Device in Userspace") Signed-off-by: Xie Yongji <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Michael S. Tsirkin <[email protected]>
1 parent 1394103 commit 0943aac

File tree

1 file changed

+25
-12
lines changed

1 file changed

+25
-12
lines changed

drivers/vdpa/vdpa_user/vduse_dev.c

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ struct vduse_dev {
8080
struct vdpa_callback config_cb;
8181
struct work_struct inject;
8282
spinlock_t irq_lock;
83+
struct rw_semaphore rwsem;
8384
int minor;
8485
bool broken;
8586
bool connected;
@@ -410,6 +411,8 @@ static void vduse_dev_reset(struct vduse_dev *dev)
410411
if (domain->bounce_map)
411412
vduse_domain_reset_bounce_map(domain);
412413

414+
down_write(&dev->rwsem);
415+
413416
dev->status = 0;
414417
dev->driver_features = 0;
415418
dev->generation++;
@@ -443,6 +446,8 @@ static void vduse_dev_reset(struct vduse_dev *dev)
443446
flush_work(&vq->inject);
444447
flush_work(&vq->kick);
445448
}
449+
450+
up_write(&dev->rwsem);
446451
}
447452

448453
static int vduse_vdpa_set_vq_address(struct vdpa_device *vdpa, u16 idx,
@@ -885,6 +890,23 @@ static void vduse_vq_irq_inject(struct work_struct *work)
885890
spin_unlock_irq(&vq->irq_lock);
886891
}
887892

893+
static int vduse_dev_queue_irq_work(struct vduse_dev *dev,
894+
struct work_struct *irq_work)
895+
{
896+
int ret = -EINVAL;
897+
898+
down_read(&dev->rwsem);
899+
if (!(dev->status & VIRTIO_CONFIG_S_DRIVER_OK))
900+
goto unlock;
901+
902+
ret = 0;
903+
queue_work(vduse_irq_wq, irq_work);
904+
unlock:
905+
up_read(&dev->rwsem);
906+
907+
return ret;
908+
}
909+
888910
static long vduse_dev_ioctl(struct file *file, unsigned int cmd,
889911
unsigned long arg)
890912
{
@@ -966,12 +988,7 @@ static long vduse_dev_ioctl(struct file *file, unsigned int cmd,
966988
break;
967989
}
968990
case VDUSE_DEV_INJECT_CONFIG_IRQ:
969-
ret = -EINVAL;
970-
if (!(dev->status & VIRTIO_CONFIG_S_DRIVER_OK))
971-
break;
972-
973-
ret = 0;
974-
queue_work(vduse_irq_wq, &dev->inject);
991+
ret = vduse_dev_queue_irq_work(dev, &dev->inject);
975992
break;
976993
case VDUSE_VQ_SETUP: {
977994
struct vduse_vq_config config;
@@ -1049,10 +1066,6 @@ static long vduse_dev_ioctl(struct file *file, unsigned int cmd,
10491066
case VDUSE_VQ_INJECT_IRQ: {
10501067
u32 index;
10511068

1052-
ret = -EINVAL;
1053-
if (!(dev->status & VIRTIO_CONFIG_S_DRIVER_OK))
1054-
break;
1055-
10561069
ret = -EFAULT;
10571070
if (get_user(index, (u32 __user *)argp))
10581071
break;
@@ -1061,9 +1074,8 @@ static long vduse_dev_ioctl(struct file *file, unsigned int cmd,
10611074
if (index >= dev->vq_num)
10621075
break;
10631076

1064-
ret = 0;
10651077
index = array_index_nospec(index, dev->vq_num);
1066-
queue_work(vduse_irq_wq, &dev->vqs[index].inject);
1078+
ret = vduse_dev_queue_irq_work(dev, &dev->vqs[index].inject);
10671079
break;
10681080
}
10691081
default:
@@ -1144,6 +1156,7 @@ static struct vduse_dev *vduse_dev_create(void)
11441156
INIT_LIST_HEAD(&dev->send_list);
11451157
INIT_LIST_HEAD(&dev->recv_list);
11461158
spin_lock_init(&dev->irq_lock);
1159+
init_rwsem(&dev->rwsem);
11471160

11481161
INIT_WORK(&dev->inject, vduse_dev_irq_inject);
11491162
init_waitqueue_head(&dev->waitq);

0 commit comments

Comments
 (0)