qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Cornelia Huck <cohuck@redhat.com>
To: Halil Pasic <pasic@linux.ibm.com>,
	Eric Farman <farman@linux.ibm.com>,
	Farhan Ali <alifm@linux.ibm.com>,
	Pierre Morel <pmorel@linux.ibm.com>
Cc: linux-s390@vger.kernel.org, kvm@vger.kernel.org,
	qemu-devel@nongnu.org, qemu-s390x@nongnu.org,
	Alex Williamson <alex.williamson@redhat.com>,
	Cornelia Huck <cohuck@redhat.com>
Subject: [Qemu-devel] [PATCH v4 3/6] vfio-ccw: protect the I/O region
Date: Fri,  1 Mar 2019 10:38:59 +0100	[thread overview]
Message-ID: <20190301093902.27799-4-cohuck@redhat.com> (raw)
In-Reply-To: <20190301093902.27799-1-cohuck@redhat.com>

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: Eric Farman <farman@linux.ibm.com>
Signed-off-by: Cornelia Huck <cohuck@redhat.com>
---
 drivers/s390/cio/vfio_ccw_drv.c     |  3 +++
 drivers/s390/cio/vfio_ccw_ops.c     | 28 +++++++++++++++++++---------
 drivers/s390/cio/vfio_ccw_private.h |  2 ++
 3 files changed, 24 insertions(+), 9 deletions(-)

diff --git a/drivers/s390/cio/vfio_ccw_drv.c b/drivers/s390/cio/vfio_ccw_drv.c
index 0b3b9de45c60..5ea0da1dd954 100644
--- a/drivers/s390/cio/vfio_ccw_drv.c
+++ b/drivers/s390/cio/vfio_ccw_drv.c
@@ -84,7 +84,9 @@ static void vfio_ccw_sch_io_todo(struct work_struct *work)
 		if (is_final)
 			cp_free(&private->cp);
 	}
+	mutex_lock(&private->io_mutex);
 	memcpy(private->io_region->irb_area, irb, sizeof(*irb));
+	mutex_unlock(&private->io_mutex);
 
 	if (private->io_trigger)
 		eventfd_signal(private->io_trigger, 1);
@@ -129,6 +131,7 @@ static int vfio_ccw_sch_probe(struct subchannel *sch)
 
 	private->sch = sch;
 	dev_set_drvdata(&sch->dev, private);
+	mutex_init(&private->io_mutex);
 
 	spin_lock_irq(sch->lock);
 	private->state = VFIO_CCW_STATE_NOT_OPER;
diff --git a/drivers/s390/cio/vfio_ccw_ops.c b/drivers/s390/cio/vfio_ccw_ops.c
index 3fdcc6dfe0bf..025c8a832bc8 100644
--- a/drivers/s390/cio/vfio_ccw_ops.c
+++ b/drivers/s390/cio/vfio_ccw_ops.c
@@ -169,16 +169,20 @@ static ssize_t vfio_ccw_mdev_read(struct mdev_device *mdev,
 {
 	struct vfio_ccw_private *private;
 	struct ccw_io_region *region;
+	int ret;
 
 	if (*ppos + count > sizeof(*region))
 		return -EINVAL;
 
 	private = dev_get_drvdata(mdev_parent_dev(mdev));
+	mutex_lock(&private->io_mutex);
 	region = private->io_region;
 	if (copy_to_user(buf, (void *)region + *ppos, count))
-		return -EFAULT;
-
-	return count;
+		ret = -EFAULT;
+	else
+		ret = count;
+	mutex_unlock(&private->io_mutex);
+	return ret;
 }
 
 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 ccw_io_region *region;
+	int ret;
 
 	if (*ppos + count > sizeof(*region))
 		return -EINVAL;
 
 	private = dev_get_drvdata(mdev_parent_dev(mdev));
+	if (!mutex_trylock(&private->io_mutex))
+		return -EAGAIN;
 
 	region = private->io_region;
-	if (copy_from_user((void *)region + *ppos, buf, count))
-		return -EFAULT;
+	if (copy_from_user((void *)region + *ppos, buf, count)) {
+		ret = -EFAULT;
+		goto out_unlock;
+	}
 
 	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;
-		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)
diff --git a/drivers/s390/cio/vfio_ccw_private.h b/drivers/s390/cio/vfio_ccw_private.h
index 50c52efb4fcb..32173cbd838d 100644
--- a/drivers/s390/cio/vfio_ccw_private.h
+++ b/drivers/s390/cio/vfio_ccw_private.h
@@ -28,6 +28,7 @@
  * @mdev: pointer to the mediated device
  * @nb: notifier for vfio events
  * @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
  * @irb: irb info received from interrupt
  * @scsw: scsw info
@@ -42,6 +43,7 @@ struct vfio_ccw_private {
 	struct mdev_device	*mdev;
 	struct notifier_block	nb;
 	struct ccw_io_region	*io_region;
+	struct mutex		io_mutex;
 
 	struct channel_program	cp;
 	struct irb		irb;
-- 
2.17.2

  parent reply	other threads:[~2019-03-01  9:39 UTC|newest]

Thread overview: 46+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-03-01  9:38 [Qemu-devel] [PATCH v4 0/6] vfio-ccw: support hsch/csch (kernel part) Cornelia Huck
2019-03-01  9:38 ` [Qemu-devel] [PATCH v4 1/6] vfio-ccw: make it safe to access channel programs Cornelia Huck
2019-04-08 17:02   ` Farhan Ali
2019-04-08 17:02     ` Farhan Ali
2019-04-08 17:07     ` Cornelia Huck
2019-04-08 17:07       ` Cornelia Huck
2019-04-08 17:19       ` Farhan Ali
2019-04-08 17:19         ` Farhan Ali
2019-04-08 20:25       ` Eric Farman
2019-04-08 20:25         ` Eric Farman
2019-04-09 23:34       ` Halil Pasic
2019-04-09 23:34         ` Halil Pasic
2019-04-11  2:59         ` Eric Farman
2019-04-11  2:59           ` Eric Farman
2019-04-11 15:58           ` [Qemu-devel] [qemu-s390x] " Halil Pasic
2019-04-11 15:58             ` Halil Pasic
2019-04-11 16:25             ` Eric Farman
2019-04-11 16:25               ` Eric Farman
2019-04-11 16:36               ` Cornelia Huck
2019-04-11 16:36                 ` Cornelia Huck
2019-04-11 18:07                 ` Halil Pasic
2019-04-11 18:07                   ` Halil Pasic
2019-04-11 21:27           ` [Qemu-devel] " Eric Farman
2019-04-11 21:27             ` Eric Farman
2019-04-12  8:14             ` Cornelia Huck
2019-04-12  8:14               ` Cornelia Huck
2019-03-01  9:38 ` [Qemu-devel] [PATCH v4 2/6] vfio-ccw: rework ssch state handling Cornelia Huck
2019-03-08 22:18   ` Eric Farman
2019-03-11  9:47     ` Cornelia Huck
2019-03-01  9:38 ` Cornelia Huck [this message]
2019-03-01  9:39 ` [Qemu-devel] [PATCH v4 4/6] vfio-ccw: add capabilities chain Cornelia Huck
2019-04-15 14:40   ` Eric Farman
2019-04-15 14:40     ` Eric Farman
2019-04-15 15:24   ` Farhan Ali
2019-04-15 15:24     ` Farhan Ali
2019-03-01  9:39 ` [Qemu-devel] [PATCH v4 5/6] s390/cio: export hsch to modules Cornelia Huck
2019-03-01  9:39 ` [Qemu-devel] [PATCH v4 6/6] vfio-ccw: add handling for async channel instructions Cornelia Huck
2019-04-15 14:56   ` Eric Farman
2019-04-15 14:56     ` Eric Farman
2019-04-15 15:25   ` Farhan Ali
2019-04-15 15:25     ` Farhan Ali
2019-03-07 21:28 ` [Qemu-devel] [PATCH v4 0/6] vfio-ccw: support hsch/csch (kernel part) Eric Farman
2019-04-15 11:51 ` Cornelia Huck
2019-04-15 11:51   ` Cornelia Huck
2019-04-15 16:43 ` Cornelia Huck
2019-04-15 16:43   ` Cornelia Huck

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20190301093902.27799-4-cohuck@redhat.com \
    --to=cohuck@redhat.com \
    --cc=alex.williamson@redhat.com \
    --cc=alifm@linux.ibm.com \
    --cc=farman@linux.ibm.com \
    --cc=kvm@vger.kernel.org \
    --cc=linux-s390@vger.kernel.org \
    --cc=pasic@linux.ibm.com \
    --cc=pmorel@linux.ibm.com \
    --cc=qemu-devel@nongnu.org \
    --cc=qemu-s390x@nongnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).