All of lore.kernel.org
 help / color / mirror / Atom feed
From: Konrad Rzeszutek Wilk <konrad.wilk@oracle.com>
To: Boris Ostrovsky <boris.ostrovsky@oracle.com>
Cc: konrad@kernel.org, xen-devel@lists.xenproject.org,
	david.vrabel@citrix.com, linux-kernel@vger.kernel.org
Subject: Re: [PATCH v4 2/5] xen/pciback: Don't deadlock when unbinding.
Date: Mon, 14 Jul 2014 10:13:28 -0400	[thread overview]
Message-ID: <20140714141328.GA5112@laptop.dumpdata.com> (raw)
In-Reply-To: <20140711210201.GC25233@laptop.dumpdata.com>

On Fri, Jul 11, 2014 at 05:02:01PM -0400, Konrad Rzeszutek Wilk wrote:
> > >--- a/drivers/xen/xen-pciback/pci_stub.c
> > >+++ b/drivers/xen/xen-pciback/pci_stub.c
> > >@@ -250,6 +250,8 @@ struct pci_dev *pcistub_get_pci_dev(struct xen_pcibk_device *pdev,
> > >   *  - 'echo BDF > unbind' with a guest still using it. See pcistub_remove
> > >   *
> > >   *  As such we have to be careful.
> > >+ *
> > >+ *  To make this easier, the caller has to hold the device lock.
> > 
> > Should we assert that the lock is being held?
> 
> Yes of course we should. Thank you!

How about this:

>From 388a03c598218dac8bfeb6c5bf3992e0d1e37d1e Mon Sep 17 00:00:00 2001
From: Konrad Rzeszutek Wilk <konrad.wilk@oracle.com>
Date: Tue, 8 Jul 2014 11:12:02 -0400
Subject: [PATCH] xen/pciback: Don't deadlock when unbinding.

As commit 0a9fd0152929db372ff61b0d6c280fdd34ae8bdb
'xen/pciback: Document the entry points for 'pcistub_put_pci_dev''
explained there are four entry points in this function.
Two of them are when the user fiddles in the SysFS to
unbind a device which might be in use by a guest or not.

Both 'unbind' states will cause a deadlock as the the PCI lock has
already been taken, which then pci_device_reset tries to take.

We can simplify this by requiring that all callers of
pcistub_put_pci_dev MUST hold the device lock. And then
we can just call the lockless version of pci_device_reset.

To make it even simpler we will modify xen_pcibk_release_pci_dev
to quality whether it should take a lock or not - as it ends
up calling xen_pcibk_release_pci_dev and needs to hold the lock.

Signed-off-by: Konrad Rzeszutek Wilk <konrad.wilk@oracle.com>
---
[v2: Per David Vrabel's suggestion - use lockless version of reset]
[v3: Per Boris suggestion add assertion mechanism]
---
 drivers/xen/xen-pciback/passthrough.c |  9 +++++++--
 drivers/xen/xen-pciback/pci_stub.c    | 12 ++++++------
 drivers/xen/xen-pciback/pciback.h     |  7 ++++---
 drivers/xen/xen-pciback/vpci.c        |  9 +++++++--
 drivers/xen/xen-pciback/xenbus.c      |  2 +-
 5 files changed, 25 insertions(+), 14 deletions(-)

diff --git a/drivers/xen/xen-pciback/passthrough.c b/drivers/xen/xen-pciback/passthrough.c
index 828dddc..d0c3fb4 100644
--- a/drivers/xen/xen-pciback/passthrough.c
+++ b/drivers/xen/xen-pciback/passthrough.c
@@ -69,7 +69,7 @@ static int __xen_pcibk_add_pci_dev(struct xen_pcibk_device *pdev,
 }
 
 static void __xen_pcibk_release_pci_dev(struct xen_pcibk_device *pdev,
-					struct pci_dev *dev)
+					struct pci_dev *dev, bool lock)
 {
 	struct passthrough_dev_data *dev_data = pdev->pci_dev_data;
 	struct pci_dev_entry *dev_entry, *t;
@@ -87,8 +87,13 @@ static void __xen_pcibk_release_pci_dev(struct xen_pcibk_device *pdev,
 
 	mutex_unlock(&dev_data->lock);
 
-	if (found_dev)
+	if (found_dev) {
+		if (lock)
+			device_lock(&found_dev->dev);
 		pcistub_put_pci_dev(found_dev);
+		if (lock)
+			device_unlock(&found_dev->dev);
+	}
 }
 
 static int __xen_pcibk_init_devices(struct xen_pcibk_device *pdev)
diff --git a/drivers/xen/xen-pciback/pci_stub.c b/drivers/xen/xen-pciback/pci_stub.c
index d57a173..8293fbb 100644
--- a/drivers/xen/xen-pciback/pci_stub.c
+++ b/drivers/xen/xen-pciback/pci_stub.c
@@ -250,6 +250,8 @@ struct pci_dev *pcistub_get_pci_dev(struct xen_pcibk_device *pdev,
  *  - 'echo BDF > unbind' with a guest still using it. See pcistub_remove
  *
  *  As such we have to be careful.
+ *
+ *  To make this easier, the caller has to hold the device lock.
  */
 void pcistub_put_pci_dev(struct pci_dev *dev)
 {
@@ -276,11 +278,8 @@ void pcistub_put_pci_dev(struct pci_dev *dev)
 	/* Cleanup our device
 	 * (so it's ready for the next domain)
 	 */
-
-	/* This is OK - we are running from workqueue context
-	 * and want to inhibit the user from fiddling with 'reset'
-	 */
-	pci_reset_function(dev);
+	lockdep_assert_held(&dev->dev.mutex);
+	__pci_reset_function_locked(dev);
 	pci_restore_state(dev);
 
 	/* This disables the device. */
@@ -567,7 +566,8 @@ static void pcistub_remove(struct pci_dev *dev)
 			/* N.B. This ends up calling pcistub_put_pci_dev which ends up
 			 * doing the FLR. */
 			xen_pcibk_release_pci_dev(found_psdev->pdev,
-						found_psdev->dev);
+						found_psdev->dev,
+						false /* caller holds  the lock. */);
 		}
 
 		spin_lock_irqsave(&pcistub_devices_lock, flags);
diff --git a/drivers/xen/xen-pciback/pciback.h b/drivers/xen/xen-pciback/pciback.h
index f72af87..58e38d5 100644
--- a/drivers/xen/xen-pciback/pciback.h
+++ b/drivers/xen/xen-pciback/pciback.h
@@ -99,7 +99,8 @@ struct xen_pcibk_backend {
 		    unsigned int *domain, unsigned int *bus,
 		    unsigned int *devfn);
 	int (*publish)(struct xen_pcibk_device *pdev, publish_pci_root_cb cb);
-	void (*release)(struct xen_pcibk_device *pdev, struct pci_dev *dev);
+	void (*release)(struct xen_pcibk_device *pdev, struct pci_dev *dev,
+                        bool lock);
 	int (*add)(struct xen_pcibk_device *pdev, struct pci_dev *dev,
 		   int devid, publish_pci_dev_cb publish_cb);
 	struct pci_dev *(*get)(struct xen_pcibk_device *pdev,
@@ -122,10 +123,10 @@ static inline int xen_pcibk_add_pci_dev(struct xen_pcibk_device *pdev,
 }
 
 static inline void xen_pcibk_release_pci_dev(struct xen_pcibk_device *pdev,
-					     struct pci_dev *dev)
+					     struct pci_dev *dev, bool lock)
 {
 	if (xen_pcibk_backend && xen_pcibk_backend->release)
-		return xen_pcibk_backend->release(pdev, dev);
+		return xen_pcibk_backend->release(pdev, dev, lock);
 }
 
 static inline struct pci_dev *
diff --git a/drivers/xen/xen-pciback/vpci.c b/drivers/xen/xen-pciback/vpci.c
index 51afff9..2fdfcb9 100644
--- a/drivers/xen/xen-pciback/vpci.c
+++ b/drivers/xen/xen-pciback/vpci.c
@@ -145,7 +145,7 @@ out:
 }
 
 static void __xen_pcibk_release_pci_dev(struct xen_pcibk_device *pdev,
-					struct pci_dev *dev)
+					struct pci_dev *dev, bool lock)
 {
 	int slot;
 	struct vpci_dev_data *vpci_dev = pdev->pci_dev_data;
@@ -169,8 +169,13 @@ static void __xen_pcibk_release_pci_dev(struct xen_pcibk_device *pdev,
 out:
 	mutex_unlock(&vpci_dev->lock);
 
-	if (found_dev)
+	if (found_dev) {
+		if (lock)
+			device_lock(&found_dev->dev);
 		pcistub_put_pci_dev(found_dev);
+		if (lock)
+			device_unlock(&found_dev->dev);
+	}
 }
 
 static int __xen_pcibk_init_devices(struct xen_pcibk_device *pdev)
diff --git a/drivers/xen/xen-pciback/xenbus.c b/drivers/xen/xen-pciback/xenbus.c
index 4a7e6e0..b3318fd 100644
--- a/drivers/xen/xen-pciback/xenbus.c
+++ b/drivers/xen/xen-pciback/xenbus.c
@@ -290,7 +290,7 @@ static int xen_pcibk_remove_device(struct xen_pcibk_device *pdev,
 
 	/* N.B. This ends up calling pcistub_put_pci_dev which ends up
 	 * doing the FLR. */
-	xen_pcibk_release_pci_dev(pdev, dev);
+	xen_pcibk_release_pci_dev(pdev, dev, true /* use the lock. */);
 
 out:
 	return err;
-- 
1.9.3


  parent reply	other threads:[~2014-07-14 14:13 UTC|newest]

Thread overview: 57+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-07-11 20:08 [PATCH v4] PCI back fixes for 3.17 konrad
2014-07-11 20:08 ` [PATCH v4 1/5] xen-pciback: Document the various parameters and attributes in SysFS konrad
2014-07-11 20:08   ` konrad
2014-07-11 20:46   ` Boris Ostrovsky
2014-07-11 20:46   ` Boris Ostrovsky
2014-07-14 16:28     ` Konrad Rzeszutek Wilk
2014-07-14 16:28     ` Konrad Rzeszutek Wilk
2014-07-11 20:08 ` [PATCH v4 2/5] xen/pciback: Don't deadlock when unbinding konrad
2014-07-11 20:48   ` Boris Ostrovsky
2014-07-11 20:48   ` Boris Ostrovsky
2014-07-11 21:02     ` Konrad Rzeszutek Wilk
2014-07-14 14:13       ` Konrad Rzeszutek Wilk
2014-07-14 14:13       ` Konrad Rzeszutek Wilk [this message]
2014-07-14 14:30         ` Boris Ostrovsky
2014-07-14 14:30         ` Boris Ostrovsky
2014-07-14 15:42           ` Konrad Rzeszutek Wilk
2014-07-14 15:42           ` Konrad Rzeszutek Wilk
2014-07-11 21:02     ` Konrad Rzeszutek Wilk
2014-07-11 20:08 ` konrad
2014-07-11 20:08 ` [PATCH v4 3/5] xen/pciback: Include the domain id if removing the device whilst still in use konrad
2014-07-11 20:08   ` konrad
2014-07-11 20:08 ` [PATCH v4 4/5] xen/pciback: Print out the domain owning the device konrad
2014-07-11 20:08 ` konrad
2014-07-11 20:08 ` [PATCH v4 5/5] xen/pciback: Remove tons of dereferences konrad
2014-07-11 20:54   ` Boris Ostrovsky
2014-07-11 20:54   ` Boris Ostrovsky
2014-07-11 20:08 ` konrad
2014-07-14 16:37 ` [PATCH v4] PCI back fixes for 3.17 Sander Eikelenboom
2014-07-14 16:37 ` [Xen-devel] " Sander Eikelenboom
2014-07-14 17:22   ` Konrad Rzeszutek Wilk
2014-07-14 17:29     ` Sander Eikelenboom
2014-07-14 17:37       ` Konrad Rzeszutek Wilk
2014-07-14 17:43         ` Sander Eikelenboom
2014-07-14 17:45           ` Konrad Rzeszutek Wilk
2014-07-14 18:24             ` Sander Eikelenboom
2014-07-14 18:45               ` Konrad Rzeszutek Wilk
2014-07-14 18:45               ` [Xen-devel] " Konrad Rzeszutek Wilk
2014-07-14 19:01                 ` Sander Eikelenboom
2014-07-14 19:50                   ` Sander Eikelenboom
2014-07-14 19:50                   ` [Xen-devel] " Sander Eikelenboom
2014-07-14 19:54                   ` Konrad Rzeszutek Wilk
2014-07-14 20:16                     ` Sander Eikelenboom
2014-07-14 20:16                     ` [Xen-devel] " Sander Eikelenboom
2014-07-14 20:18                       ` Konrad Rzeszutek Wilk
2014-07-14 20:21                         ` Sander Eikelenboom
2014-07-14 20:25                           ` Konrad Rzeszutek Wilk
2014-07-14 20:25                           ` Konrad Rzeszutek Wilk
2014-07-14 20:21                         ` Sander Eikelenboom
2014-07-14 20:18                       ` Konrad Rzeszutek Wilk
2014-07-14 19:54                   ` Konrad Rzeszutek Wilk
2014-07-14 19:01                 ` Sander Eikelenboom
2014-07-14 18:24             ` Sander Eikelenboom
2014-07-14 17:45           ` Konrad Rzeszutek Wilk
2014-07-14 17:43         ` Sander Eikelenboom
2014-07-14 17:37       ` Konrad Rzeszutek Wilk
2014-07-14 17:29     ` Sander Eikelenboom
2014-07-14 17:22   ` Konrad Rzeszutek Wilk

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=20140714141328.GA5112@laptop.dumpdata.com \
    --to=konrad.wilk@oracle.com \
    --cc=boris.ostrovsky@oracle.com \
    --cc=david.vrabel@citrix.com \
    --cc=konrad@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=xen-devel@lists.xenproject.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.