From mboxrd@z Thu Jan 1 00:00:00 1970 From: Cornelia Huck Subject: Re: [PATCH v2 2/5] vfio-ccw: concurrent I/O handling Date: Tue, 22 Jan 2019 12:53:22 +0100 Message-ID: <20190122125322.4bb04921.cohuck@redhat.com> References: <20190121110354.2247-1-cohuck@redhat.com> <20190121110354.2247-3-cohuck@redhat.com> <20190121212018.4e377e59@oc2783563651> <20190122112926.4ff54f9f.cohuck@redhat.com> <20190122121737.49c3f900@oc2783563651> Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit Return-path: In-Reply-To: <20190122121737.49c3f900@oc2783563651> List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+gceq-qemu-devel2=m.gmane.org@nongnu.org Sender: "Qemu-devel" List-Archive: List-Post: To: Halil Pasic Cc: linux-s390@vger.kernel.org, Eric Farman , Pierre Morel , kvm@vger.kernel.org, qemu-s390x@nongnu.org, Farhan Ali , qemu-devel@nongnu.org, Alex Williamson List-ID: On Tue, 22 Jan 2019 12:17:37 +0100 Halil Pasic wrote: > On Tue, 22 Jan 2019 11:29:26 +0100 > Cornelia Huck wrote: > > > On Mon, 21 Jan 2019 21:20:18 +0100 > > Halil Pasic wrote: > > > > > On Mon, 21 Jan 2019 12:03:51 +0100 > > > Cornelia Huck wrote: > > > > > > > Rework handling of multiple I/O requests to return -EAGAIN if > > > > we are already processing an I/O request. Introduce a mutex > > > > to disallow concurrent writes to the I/O region. > > > > > > > > The expectation is that userspace simply retries the operation > > > > if it gets -EAGAIN. > > > > > > > > We currently don't allow multiple ssch requests at the same > > > > time, as we don't have support for keeping channel programs > > > > around for more than one request. > > > > > > > > Signed-off-by: Cornelia Huck > > > > --- > > > > > > [..] > > > > > > > static ssize_t vfio_ccw_mdev_write(struct mdev_device *mdev, > > > > @@ -188,25 +192,30 @@ 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 (private->state != VFIO_CCW_STATE_IDLE) > > > > + if (private->state == VFIO_CCW_STATE_NOT_OPER || > > > > + private->state == VFIO_CCW_STATE_STANDBY) > > > > return -EACCES; > > > > + 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)) { > > > > > > This might race with vfio_ccw_sch_io_todo() on > > > private->io_region->irb_area, or? > > > > Ah yes, this should also take the mutex (should work because we're on a > > workqueue). > > > > I'm not sure that will do the trick (assumed I understood the > intention correctly). Let's say the things happen in this order: > 1) vfio_ccw_sch_io_todo() goes first, I guess updates > private->io_region->irb_area and releases the mutex. > 2) Then vfio_ccw_mdev_write() destroys the irb_area by zeriong it out, > and finally, > 3) userspace reads the destroyed irb_area using vfio_ccw_mdev_read(). > > Or am I misunderstanding something? You're not, but dealing with that race is outside the scope of this patch. If userspace submits a request and then tries to get the old data for a prior request, I suggest that userspace needs to fix their sequencing. From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([209.51.188.92]:58362) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gm1KK-00018X-Sl for qemu-devel@nongnu.org; Tue, 22 Jan 2019 14:03:17 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1gm1KG-0005hu-VS for qemu-devel@nongnu.org; Tue, 22 Jan 2019 14:03:12 -0500 Date: Tue, 22 Jan 2019 12:53:22 +0100 From: Cornelia Huck Message-ID: <20190122125322.4bb04921.cohuck@redhat.com> In-Reply-To: <20190122121737.49c3f900@oc2783563651> References: <20190121110354.2247-1-cohuck@redhat.com> <20190121110354.2247-3-cohuck@redhat.com> <20190121212018.4e377e59@oc2783563651> <20190122112926.4ff54f9f.cohuck@redhat.com> <20190122121737.49c3f900@oc2783563651> MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit Subject: Re: [Qemu-devel] [PATCH v2 2/5] vfio-ccw: concurrent I/O handling List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: Halil Pasic Cc: Eric Farman , Farhan Ali , Pierre Morel , linux-s390@vger.kernel.org, kvm@vger.kernel.org, Alex Williamson , qemu-devel@nongnu.org, qemu-s390x@nongnu.org On Tue, 22 Jan 2019 12:17:37 +0100 Halil Pasic wrote: > On Tue, 22 Jan 2019 11:29:26 +0100 > Cornelia Huck wrote: > > > On Mon, 21 Jan 2019 21:20:18 +0100 > > Halil Pasic wrote: > > > > > On Mon, 21 Jan 2019 12:03:51 +0100 > > > Cornelia Huck wrote: > > > > > > > Rework handling of multiple I/O requests to return -EAGAIN if > > > > we are already processing an I/O request. Introduce a mutex > > > > to disallow concurrent writes to the I/O region. > > > > > > > > The expectation is that userspace simply retries the operation > > > > if it gets -EAGAIN. > > > > > > > > We currently don't allow multiple ssch requests at the same > > > > time, as we don't have support for keeping channel programs > > > > around for more than one request. > > > > > > > > Signed-off-by: Cornelia Huck > > > > --- > > > > > > [..] > > > > > > > static ssize_t vfio_ccw_mdev_write(struct mdev_device *mdev, > > > > @@ -188,25 +192,30 @@ 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 (private->state != VFIO_CCW_STATE_IDLE) > > > > + if (private->state == VFIO_CCW_STATE_NOT_OPER || > > > > + private->state == VFIO_CCW_STATE_STANDBY) > > > > return -EACCES; > > > > + 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)) { > > > > > > This might race with vfio_ccw_sch_io_todo() on > > > private->io_region->irb_area, or? > > > > Ah yes, this should also take the mutex (should work because we're on a > > workqueue). > > > > I'm not sure that will do the trick (assumed I understood the > intention correctly). Let's say the things happen in this order: > 1) vfio_ccw_sch_io_todo() goes first, I guess updates > private->io_region->irb_area and releases the mutex. > 2) Then vfio_ccw_mdev_write() destroys the irb_area by zeriong it out, > and finally, > 3) userspace reads the destroyed irb_area using vfio_ccw_mdev_read(). > > Or am I misunderstanding something? You're not, but dealing with that race is outside the scope of this patch. If userspace submits a request and then tries to get the old data for a prior request, I suggest that userspace needs to fix their sequencing.