virtualization.lists.linux-foundation.org archive mirror
 help / color / mirror / Atom feed
From: Benjamin Herrenschmidt <benh@kernel.crashing.org>
To: "Michael S. Tsirkin" <mst@redhat.com>
Cc: Pawel Moll <pawel.moll@arm.com>,
	virtualization <virtualization@lists.linux-foundation.org>,
	Christian Borntraeger <borntraeger@de.ibm.com>,
	Sasha Levin <levinsasha928@gmail.com>,
	Anthony Liguori <anthony@codemonkey.ws>
Subject: Re: [RFC 7/11] virtio_pci: new, capability-aware driver.
Date: Fri, 13 Jan 2012 13:32:42 +1100	[thread overview]
Message-ID: <1326421962.23910.228.camel@pasglop> (raw)
In-Reply-To: <20120113021930.GA15379@redhat.com>

On Fri, 2012-01-13 at 04:19 +0200, Michael S. Tsirkin wrote:
> On Thu, Jan 12, 2012 at 12:12:17PM +1030, Rusty Russell wrote:
> > On Thu, 12 Jan 2012 00:02:33 +0200, "Michael S. Tsirkin" <mst@redhat.com> wrote:
> > > Look, we have a race currently. Let us not tie a bug fix to a huge
> > > rewrite with unclear performance benefits, please.
> > 
> > In theory, yes.  In practice, we bandaid it.
> > 
> > I think in the short term we change ->get to get the entire sequence
> > twice, and check it's the same.  Theoretically, still racy, but it does
> > cut the window.  And we haven't seen the bug yet, either.
> 
> I thought about this some more. Since we always get
> an interrupt on config changes, it seems that a rather
> robust method would be to just synchronize against that.
> Something like the below (warning - completely untested).
> Still need to think about memory barriers, overflow etc.
> What do you think?

Your interrupt may take an unpredictable amount of time to arrive, I
don't see how you can use that as a guarantee.

Cheers,
Ben.

> Signed-off-by: Michael S. Tsirkin <mst@redhat.com>
> 
> diff --git a/drivers/virtio/virtio_pci.c b/drivers/virtio/virtio_pci.c
> index 03d1984..b5df385 100644
> --- a/drivers/virtio/virtio_pci.c
> +++ b/drivers/virtio/virtio_pci.c
> @@ -57,6 +57,7 @@ struct virtio_pci_device
>  	unsigned msix_used_vectors;
>  	/* Whether we have vector per vq */
>  	bool per_vq_vectors;
> +	atomic_t config_changes;
>  };
>  
>  /* Constants for MSI-X */
> @@ -125,6 +126,19 @@ static void vp_finalize_features(struct virtio_device *vdev)
>  	iowrite32(vdev->features[0], vp_dev->ioaddr+VIRTIO_PCI_GUEST_FEATURES);
>  }
>  
> +/* wait for pending irq handlers */
> +static void vp_synchronize_vectors(struct virtio_device *vdev)
> +{
> +	struct virtio_pci_device *vp_dev = to_vp_device(vdev);
> +	int i;
> +
> +	if (vp_dev->intx_enabled)
> +		synchronize_irq(vp_dev->pci_dev->irq);
> +
> +	for (i = 0; i < vp_dev->msix_vectors; ++i)
> +		synchronize_irq(vp_dev->msix_entries[i].vector);
> +}
> +
>  /* virtio config->get() implementation */
>  static void vp_get(struct virtio_device *vdev, unsigned offset,
>  		   void *buf, unsigned len)
> @@ -134,9 +148,20 @@ static void vp_get(struct virtio_device *vdev, unsigned offset,
>  				VIRTIO_PCI_CONFIG(vp_dev) + offset;
>  	u8 *ptr = buf;
>  	int i;
> -
> -	for (i = 0; i < len; i++)
> -		ptr[i] = ioread8(ioaddr + i);
> +	int uninitialized_var(c);
> +	c = atomic_read(&vp_dev->config_changes);
> +	/* Make sure read is done before we get the first config byte */
> +	rmb();
> +	do {
> +		for (i = 0; i < len; i++)
> +			ptr[i] = ioread8(ioaddr + i);
> +		/* Synchronize with config interrupt */
> +		vp_synchronize_vectors(vdev);
> +		/*
> +		 * For multi-byte fields, we might get a config change interrupt
> +		 * between byte reads. If this happens, retry the read.
> +		 */
> +	} while (c != atomic_read(&vp_dev->config_changes))
>  }
>  
>  /* the config->set() implementation.  it's symmetric to the config->get()
> @@ -169,19 +194,6 @@ static void vp_set_status(struct virtio_device *vdev, u8 status)
>  	iowrite8(status, vp_dev->ioaddr + VIRTIO_PCI_STATUS);
>  }
>  
> -/* wait for pending irq handlers */
> -static void vp_synchronize_vectors(struct virtio_device *vdev)
> -{
> -	struct virtio_pci_device *vp_dev = to_vp_device(vdev);
> -	int i;
> -
> -	if (vp_dev->intx_enabled)
> -		synchronize_irq(vp_dev->pci_dev->irq);
> -
> -	for (i = 0; i < vp_dev->msix_vectors; ++i)
> -		synchronize_irq(vp_dev->msix_entries[i].vector);
> -}
> -
>  static void vp_reset(struct virtio_device *vdev)
>  {
>  	struct virtio_pci_device *vp_dev = to_vp_device(vdev);
> @@ -213,6 +225,8 @@ static irqreturn_t vp_config_changed(int irq, void *opaque)
>  	drv = container_of(vp_dev->vdev.dev.driver,
>  			   struct virtio_driver, driver);
>  
> +	atomic_inc(&vp_dev->config_changes);
> +
>  	if (drv && drv->config_changed)
>  		drv->config_changed(&vp_dev->vdev);
>  	return IRQ_HANDLED;
> @@ -646,6 +660,7 @@ static int __devinit virtio_pci_probe(struct pci_dev *pci_dev,
>  	vp_dev->vdev.config = &virtio_pci_config_ops;
>  	vp_dev->pci_dev = pci_dev;
>  	INIT_LIST_HEAD(&vp_dev->virtqueues);
> +	atomic_set(&vp_dev->config_changes, 0);
>  	spin_lock_init(&vp_dev->lock);
>  
>  	/* Disable MSI/MSIX to bring device to a known good state. */

  reply	other threads:[~2012-01-13  2:32 UTC|newest]

Thread overview: 101+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-12-08 10:22 [PATCH 0/11] RFC: PCI using capabilitities Rusty Russell
2011-12-08 10:30 ` [RFC 1/11] virtio: use u32, not bitmap for struct virtio_device's features Rusty Russell
2011-12-08 10:31 ` [RFC 2/11] virtio: add support for 64 bit features Rusty Russell
2011-12-08 10:32 ` [PATCH 0/11] RFC: PCI using capabilitities Sasha Levin
2011-12-08 10:32 ` [RFC 3/11] pci: add pci_iomap_range Rusty Russell
2011-12-15  8:30   ` Michael S. Tsirkin
2011-12-16  1:56     ` Rusty Russell
2011-12-08 10:34 ` [RFC 4/11] virtio-pci: define layout for virtio vendor-specific capabilities Rusty Russell
2011-12-10 21:14   ` Sasha Levin
2011-12-08 10:35 ` [RFC 6/11] virtio_pci: move old defines to legacy, introduce new structure Rusty Russell
2011-12-08 10:38 ` [RFC 6/11] virtio_pci: don't use the legacy driver if we find the new PCI capabilities Rusty Russell
2011-12-10 21:18   ` Sasha Levin
2011-12-11  5:15     ` Rusty Russell
2011-12-11  9:37     ` Michael S. Tsirkin
2011-12-08 10:39 ` [RFC 7/11] virtio_pci: new, capability-aware driver Rusty Russell
2011-12-11  9:42   ` Michael S. Tsirkin
2011-12-11 22:45     ` Rusty Russell
2011-12-12 11:49       ` Michael S. Tsirkin
2011-12-12 18:10         ` Don Dutile
2011-12-16  1:58           ` Rusty Russell
2013-05-28  7:56         ` Michael S. Tsirkin
2013-05-29  1:17           ` Rusty Russell
2013-05-29 10:21             ` Michael S. Tsirkin
2013-05-30  2:17               ` Rusty Russell
2011-12-12 18:25       ` Michael S. Tsirkin
2011-12-13  2:21         ` Rusty Russell
2011-12-15  8:27           ` Michael S. Tsirkin
2011-12-16  1:50             ` Rusty Russell
2011-12-18 10:18               ` Michael S. Tsirkin
2011-12-19  6:06                 ` Rusty Russell
2011-12-19  9:13                   ` Michael S. Tsirkin
2011-12-19 11:08                     ` Rusty Russell
2011-12-20 11:37                       ` Michael S. Tsirkin
2011-12-21  0:33                         ` Rusty Russell
2011-12-21  9:19                           ` Michael S. Tsirkin
2012-01-10 17:03                           ` Michael S. Tsirkin
2012-01-11  0:25                             ` Rusty Russell
2012-01-11  1:48                               ` Benjamin Herrenschmidt
2012-01-11  8:47                               ` Stefan Hajnoczi
2012-01-11  9:10                                 ` Benjamin Herrenschmidt
2012-01-11 14:28                                   ` Stefan Hajnoczi
2012-01-11 15:39                                     ` Michael S. Tsirkin
2012-01-11 17:21                                       ` Stefan Hajnoczi
2012-01-11 18:34                                         ` Michael S. Tsirkin
2012-01-11 20:56                                         ` Benjamin Herrenschmidt
2012-01-11 20:46                                     ` Benjamin Herrenschmidt
2012-01-12 10:37                                       ` Stefan Hajnoczi
2012-01-11 10:21                               ` Michael S. Tsirkin
2012-01-11 21:13                                 ` Benjamin Herrenschmidt
2012-01-11 22:13                                   ` Michael S. Tsirkin
2012-01-11 22:19                                     ` Benjamin Herrenschmidt
2012-01-11 22:56                                     ` Benjamin Herrenschmidt
2012-01-12  5:29                                       ` Michael S. Tsirkin
2012-01-12  6:13                                         ` Benjamin Herrenschmidt
2012-01-12  6:37                                           ` Michael S. Tsirkin
2012-01-12  2:01                                 ` Rusty Russell
2012-01-12  4:31                                   ` Benjamin Herrenschmidt
2012-01-12  6:09                                     ` Michael S. Tsirkin
2012-01-12  6:28                                       ` Benjamin Herrenschmidt
2012-01-12  6:51                                         ` Michael S. Tsirkin
2012-01-12  7:03                                           ` Benjamin Herrenschmidt
2012-01-12  6:00                                   ` Michael S. Tsirkin
2012-01-13  3:22                                     ` Rusty Russell
2012-01-11 13:30                               ` Anthony Liguori
2012-01-11 15:12                                 ` Michael S. Tsirkin
2012-01-11 15:15                                   ` Anthony Liguori
2012-01-11 15:21                                     ` Michael S. Tsirkin
2012-01-11 15:28                                       ` Anthony Liguori
2012-01-11 15:45                                         ` Michael S. Tsirkin
2012-01-11 16:02                                           ` Anthony Liguori
2012-01-11 17:08                                             ` Michael S. Tsirkin
2012-01-11 19:42                                               ` Anthony Liguori
2012-01-11 20:14                                                 ` Michael S. Tsirkin
2012-01-11 20:26                                                   ` Anthony Liguori
2012-01-11 21:02                                                     ` Benjamin Herrenschmidt
2012-01-11 22:02                                                       ` Michael S. Tsirkin
2012-01-11 22:16                                                         ` Benjamin Herrenschmidt
2012-01-12  1:42                                                         ` Rusty Russell
2012-01-13  2:19                                                           ` Michael S. Tsirkin
2012-01-13  2:32                                                             ` Benjamin Herrenschmidt [this message]
2012-01-18 15:29                                                               ` Michael S. Tsirkin
2012-01-22 22:53                                                                 ` Rusty Russell
2012-01-18  4:52                                                             ` Rusty Russell
2012-01-11 21:58                                                     ` Michael S. Tsirkin
2012-01-11 20:59                                                 ` Benjamin Herrenschmidt
2012-01-11 20:51                                       ` Benjamin Herrenschmidt
2012-01-11 20:50                                   ` Benjamin Herrenschmidt
2012-01-12  1:35                                 ` Rusty Russell
2011-12-08 10:40 ` [RFC 8/11] virtio_pci: share structure between legacy and modern Rusty Russell
2011-12-08 10:41 ` [RFC 9/11] virtio_pci: share interrupt/notify handlers " Rusty Russell
2011-12-08 10:42 ` [RFC 10/11] virtio_pci: share virtqueue setup/teardown between modern and legacy driver Rusty Russell
2011-12-08 10:44 ` [RFC 11/11] virtio_pci: simplify common helpers Rusty Russell
2011-12-08 15:37 ` [PATCH 0/11] RFC: PCI using capabilitities Sasha Levin
     [not found] ` <1323358657.32487.9.camel@lappy>
2011-12-09  6:17   ` Rusty Russell
2011-12-10 21:32     ` Sasha Levin
2011-12-11  9:05   ` Avi Kivity
2011-12-11 10:03     ` Sasha Levin
2011-12-11 12:30       ` Michael S. Tsirkin
2011-12-11 12:48         ` Sasha Levin
2011-12-11 12:47   ` Michael S. Tsirkin
2011-12-11 12:53     ` Sasha Levin

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=1326421962.23910.228.camel@pasglop \
    --to=benh@kernel.crashing.org \
    --cc=anthony@codemonkey.ws \
    --cc=borntraeger@de.ibm.com \
    --cc=levinsasha928@gmail.com \
    --cc=mst@redhat.com \
    --cc=pawel.moll@arm.com \
    --cc=virtualization@lists.linux-foundation.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).