Commit 4f766173 authored by Cornelia Huck's avatar Cornelia Huck

vfio-ccw: protect the I/O region

Introduce a mutex to disallow concurrent reads or writes to the
I/O region. This makes sure that the data the kernel or user
space see is always consistent.

The same mutex will be used to protect the async region as well.
Reviewed-by: default avatarEric Farman <farman@linux.ibm.com>
Signed-off-by: default avatarCornelia Huck <cohuck@redhat.com>
parent 690f6a15
...@@ -84,7 +84,9 @@ static void vfio_ccw_sch_io_todo(struct work_struct *work) ...@@ -84,7 +84,9 @@ static void vfio_ccw_sch_io_todo(struct work_struct *work)
if (is_final) if (is_final)
cp_free(&private->cp); cp_free(&private->cp);
} }
mutex_lock(&private->io_mutex);
memcpy(private->io_region->irb_area, irb, sizeof(*irb)); memcpy(private->io_region->irb_area, irb, sizeof(*irb));
mutex_unlock(&private->io_mutex);
if (private->io_trigger) if (private->io_trigger)
eventfd_signal(private->io_trigger, 1); eventfd_signal(private->io_trigger, 1);
...@@ -129,6 +131,7 @@ static int vfio_ccw_sch_probe(struct subchannel *sch) ...@@ -129,6 +131,7 @@ static int vfio_ccw_sch_probe(struct subchannel *sch)
private->sch = sch; private->sch = sch;
dev_set_drvdata(&sch->dev, private); dev_set_drvdata(&sch->dev, private);
mutex_init(&private->io_mutex);
spin_lock_irq(sch->lock); spin_lock_irq(sch->lock);
private->state = VFIO_CCW_STATE_NOT_OPER; private->state = VFIO_CCW_STATE_NOT_OPER;
......
...@@ -169,16 +169,20 @@ static ssize_t vfio_ccw_mdev_read(struct mdev_device *mdev, ...@@ -169,16 +169,20 @@ static ssize_t vfio_ccw_mdev_read(struct mdev_device *mdev,
{ {
struct vfio_ccw_private *private; struct vfio_ccw_private *private;
struct ccw_io_region *region; struct ccw_io_region *region;
int ret;
if (*ppos + count > sizeof(*region)) if (*ppos + count > sizeof(*region))
return -EINVAL; return -EINVAL;
private = dev_get_drvdata(mdev_parent_dev(mdev)); private = dev_get_drvdata(mdev_parent_dev(mdev));
mutex_lock(&private->io_mutex);
region = private->io_region; region = private->io_region;
if (copy_to_user(buf, (void *)region + *ppos, count)) if (copy_to_user(buf, (void *)region + *ppos, count))
return -EFAULT; ret = -EFAULT;
else
return count; ret = count;
mutex_unlock(&private->io_mutex);
return ret;
} }
static ssize_t vfio_ccw_mdev_write(struct mdev_device *mdev, static ssize_t vfio_ccw_mdev_write(struct mdev_device *mdev,
...@@ -188,23 +192,29 @@ static ssize_t vfio_ccw_mdev_write(struct mdev_device *mdev, ...@@ -188,23 +192,29 @@ static ssize_t vfio_ccw_mdev_write(struct mdev_device *mdev,
{ {
struct vfio_ccw_private *private; struct vfio_ccw_private *private;
struct ccw_io_region *region; struct ccw_io_region *region;
int ret;
if (*ppos + count > sizeof(*region)) if (*ppos + count > sizeof(*region))
return -EINVAL; return -EINVAL;
private = dev_get_drvdata(mdev_parent_dev(mdev)); private = dev_get_drvdata(mdev_parent_dev(mdev));
if (!mutex_trylock(&private->io_mutex))
return -EAGAIN;
region = private->io_region; region = private->io_region;
if (copy_from_user((void *)region + *ppos, buf, count)) if (copy_from_user((void *)region + *ppos, buf, count)) {
return -EFAULT; ret = -EFAULT;
goto out_unlock;
}
vfio_ccw_fsm_event(private, VFIO_CCW_EVENT_IO_REQ); vfio_ccw_fsm_event(private, VFIO_CCW_EVENT_IO_REQ);
if (region->ret_code != 0) { if (region->ret_code != 0)
private->state = VFIO_CCW_STATE_IDLE; private->state = VFIO_CCW_STATE_IDLE;
return region->ret_code; ret = (region->ret_code != 0) ? region->ret_code : count;
}
return count; out_unlock:
mutex_unlock(&private->io_mutex);
return ret;
} }
static int vfio_ccw_mdev_get_device_info(struct vfio_device_info *info) static int vfio_ccw_mdev_get_device_info(struct vfio_device_info *info)
......
...@@ -28,6 +28,7 @@ ...@@ -28,6 +28,7 @@
* @mdev: pointer to the mediated device * @mdev: pointer to the mediated device
* @nb: notifier for vfio events * @nb: notifier for vfio events
* @io_region: MMIO region to input/output I/O arguments/results * @io_region: MMIO region to input/output I/O arguments/results
* @io_mutex: protect against concurrent update of I/O regions
* @cp: channel program for the current I/O operation * @cp: channel program for the current I/O operation
* @irb: irb info received from interrupt * @irb: irb info received from interrupt
* @scsw: scsw info * @scsw: scsw info
...@@ -42,6 +43,7 @@ struct vfio_ccw_private { ...@@ -42,6 +43,7 @@ struct vfio_ccw_private {
struct mdev_device *mdev; struct mdev_device *mdev;
struct notifier_block nb; struct notifier_block nb;
struct ccw_io_region *io_region; struct ccw_io_region *io_region;
struct mutex io_mutex;
struct channel_program cp; struct channel_program cp;
struct irb irb; struct irb irb;
......
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