Skip to contentSkip to conversation

USB host: Fix lockdep warning in AMD PLL quirk

PATCH In review Joerg Roedel 6 participants 33 messages Apr 6, 2011 – Apr 13, 2011

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 USB host: Fix lockdep warning in AMD PLL quirk

Booting 2.6.38 on my test machine produces a lockdep warning from the usb_amd_find_chipset_info() function:

 WARNING: at /data/lemmy/linux.trees.git/kernel/lockdep.c:2465 lockdep_trace_alloc+0x95/0xc2()
 Hardware name: Snook
 Modules linked in:
 Pid: 959, comm: work_for_cpu Not tainted 2.6.39-rc2+ #22
 Call Trace:
  [<ffffffff8103c0d4>] warn_slowpath_common+0x80/0x98
  [<ffffffff812387e6>] ? T.492+0x24/0x26
  [<ffffffff8103c101>] warn_slowpath_null+0x15/0x17
  [<ffffffff81068667>] lockdep_trace_alloc+0x95/0xc2
  [<ffffffff810ed9ac>] slab_pre_alloc_hook+0x18/0x3b
  [<ffffffff810ef227>] kmem_cache_alloc_trace+0x25/0xba
  [<ffffffff812387e6>] T.492+0x24/0x26
  [<ffffffff81238816>] pci_get_subsys+0x2e/0x73
 sr0: scsi3-mmc drive: 48x/48x writer dvd-ram cd/rw xa/form2 cdda tray
  [<ffffffff8123886c>] pci_get_device+0x11/0x13
  [<ffffffff814082a9>] usb_amd_find_chipset_info+0x3f/0x18a

...

It turns out that this function calls pci_get_device under a spin_lock with irqs disabled, but the pci_get_device function is only allowed in preemptible context.

This patch fixes this by using temporary variables in the quirk algorithm and commiting them later to the struct under the lock. This moves all pci_get_device() invocations out of the spin_lock and fixes the lockdep warning for me.

---
 drivers/usb/host/pci-quirks.c |   67 +++++++++++++++++++++++-----------------
 1 files changed, 38 insertions(+), 29 deletions(-)
drivers/usb/host/pci-quirks.c +38 -29
index 1d586d4..d3e5cf3 100644
@@ -84,64 +84,73 @@ int usb_amd_find_chipset_info(void)
 {
 	u8 rev = 0;
 	unsigned long flags;
+	struct pci_dev *nb_dev, *smbus_dev;
+	int nb_type, sb_type;
 
 	spin_lock_irqsave(&amd_lock, flags);
-
 	amd_chipset.probe_count++;
 	/* probe only once */
 	if (amd_chipset.probe_count > 1) {
 		spin_unlock_irqrestore(&amd_lock, flags);
 		return amd_chipset.probe_result;
 	}
+	spin_unlock_irqrestore(&amd_lock, flags);
 
-	amd_chipset.smbus_dev = pci_get_device(PCI_VENDOR_ID_ATI, 0x4385, NULL);
-	if (amd_chipset.smbus_dev) {
-		rev = amd_chipset.smbus_dev->revision;
+	sb_type   = 0;
+	smbus_dev = pci_get_device(PCI_VENDOR_ID_ATI, 0x4385, NULL);
+	if (smbus_dev) {
+		rev = smbus_dev->revision;
 		if (rev >= 0x40)
-			amd_chipset.sb_type = 1;
+			sb_type = 1;
 		else if (rev >= 0x30 && rev <= 0x3b)
-			amd_chipset.sb_type = 3;
+			sb_type = 3;
 	} else {
-		amd_chipset.smbus_dev = pci_get_device(PCI_VENDOR_ID_AMD,
-							0x780b, NULL);
-		if (!amd_chipset.smbus_dev) {
+		smbus_dev = pci_get_device(PCI_VENDOR_ID_AMD, 0x780b, NULL);
+		if (smbus_dev) {
+			spin_lock_irqsave(&amd_lock, flags);
+			amd_chipset.smbus_dev = smbus_dev;
 			spin_unlock_irqrestore(&amd_lock, flags);
 			return 0;
 		}
-		rev = amd_chipset.smbus_dev->revision;
+		rev = smbus_dev->revision;
 		if (rev >= 0x11 && rev <= 0x18)
-			amd_chipset.sb_type = 2;
+			sb_type = 2;
 	}
 
-	if (amd_chipset.sb_type == 0) {
-		if (amd_chipset.smbus_dev) {
-			pci_dev_put(amd_chipset.smbus_dev);
-			amd_chipset.smbus_dev = NULL;
+	if (sb_type == 0) {
+		if (smbus_dev) {
+			pci_dev_put(smbus_dev);
+			smbus_dev = NULL;
 		}
-		spin_unlock_irqrestore(&amd_lock, flags);
 		return 0;
 	}
 
-	amd_chipset.nb_dev = pci_get_device(PCI_VENDOR_ID_AMD, 0x9601, NULL);
-	if (amd_chipset.nb_dev) {
-		amd_chipset.nb_type = 1;
+	nb_type = 0;
+	nb_dev  = pci_get_device(PCI_VENDOR_ID_AMD, 0x9601, NULL);
+	if (nb_dev) {
+		nb_type = 1;
 	} else {
-		amd_chipset.nb_dev = pci_get_device(PCI_VENDOR_ID_AMD,
-							0x1510, NULL);
-		if (amd_chipset.nb_dev) {
-			amd_chipset.nb_type = 2;
-		} else  {
-			amd_chipset.nb_dev = pci_get_device(PCI_VENDOR_ID_AMD,
-								0x9600, NULL);
-			if (amd_chipset.nb_dev)
-				amd_chipset.nb_type = 3;
+		nb_dev = pci_get_device(PCI_VENDOR_ID_AMD, 0x1510, NULL);
+		if (nb_dev) {
+			nb_type = 2;
+		} else {
+			nb_dev = pci_get_device(PCI_VENDOR_ID_AMD,
+						0x9600, NULL);
+			if (nb_dev)
+				nb_type = 3;
 		}
 	}
 
+	spin_lock_irqsave(&amd_lock, flags);
+	amd_chipset.smbus_dev    = smbus_dev;
+	amd_chipset.sb_type      = sb_type;
+	amd_chipset.nb_dev       = nb_dev;
+	amd_chipset.nb_type      = nb_type;
 	amd_chipset.probe_result = 1;
+	spin_unlock_irqrestore(&amd_lock, flags);
+
 	printk(KERN_DEBUG "QUIRK: Enable AMD PLL fix\n");
 
-	spin_unlock_irqrestore(&amd_lock, flags);
 	return amd_chipset.probe_result;
 }
 EXPORT_SYMBOL_GPL(usb_amd_find_chipset_info);
Signature · 1 line
1.7.1
#1 lore raw
Quoted 45 lines · Joerg Roedel

Booting 2.6.38 on my test machine produces a lockdep warning from the usb_amd_find_chipset_info() function:

 WARNING: at /data/lemmy/linux.trees.git/kernel/lockdep.c:2465 lockdep_trace_alloc+0x95/0xc2()
 Hardware name: Snook
 Modules linked in:
 Pid: 959, comm: work_for_cpu Not tainted 2.6.39-rc2+ #22
 Call Trace:
  [<ffffffff8103c0d4>] warn_slowpath_common+0x80/0x98
  [<ffffffff812387e6>] ? T.492+0x24/0x26
  [<ffffffff8103c101>] warn_slowpath_null+0x15/0x17
  [<ffffffff81068667>] lockdep_trace_alloc+0x95/0xc2
  [<ffffffff810ed9ac>] slab_pre_alloc_hook+0x18/0x3b
  [<ffffffff810ef227>] kmem_cache_alloc_trace+0x25/0xba
  [<ffffffff812387e6>] T.492+0x24/0x26
  [<ffffffff81238816>] pci_get_subsys+0x2e/0x73
 sr0: scsi3-mmc drive: 48x/48x writer dvd-ram cd/rw xa/form2 cdda tray
  [<ffffffff8123886c>] pci_get_device+0x11/0x13
  [<ffffffff814082a9>] usb_amd_find_chipset_info+0x3f/0x18a

...

It turns out that this function calls pci_get_device under a spin_lock with irqs disabled, but the pci_get_device function is only allowed in preemptible context.

This patch fixes this by using temporary variables in the quirk algorithm and commiting them later to the struct under the lock. This moves all pci_get_device() invocations out of the spin_lock and fixes the lockdep warning for me.

---
 drivers/usb/host/pci-quirks.c |   67 +++++++++++++++++++++++-----------------
 1 files changed, 38 insertions(+), 29 deletions(-)
drivers/usb/host/pci-quirks.c +1 -0
index 1d586d4..d3e5cf3 100644
@@ -84,64 +84,73 @@ int usb_amd_find_chipset_info(void)
 {
 	u8 rev = 0;
 	unsigned long flags;
+	struct pci_dev *nb_dev, *smbus_dev;

drivers/usb/host/pci-quirks.c int usb_amd_find_chipset_info(void) · line 96

Quoted 10 lines
drivers/usb/host/pci-quirks.c +1 -1
+	int nb_type, sb_type;
 
 	spin_lock_irqsave(&amd_lock, flags);
-
 	amd_chipset.probe_count++;
 	/* probe only once */
 	if (amd_chipset.probe_count > 1) {
 		spin_unlock_irqrestore(&amd_lock, flags);
 		return amd_chipset.probe_result;
 	}

The counter really should be a bool: Has the chipset already been probed or not? After all, nobody cares how many times this routine was called.

drivers/usb/host/pci-quirks.c int usb_amd_find_chipset_info(void) · line 97

Quoted 1 line
drivers/usb/host/pci-quirks.c +1 -0
+	spin_unlock_irqrestore(&amd_lock, flags);

This code now contains a bug: You incremented the probe_count _before_ doing the probe. If another thread calls this routine right now, it will get an incorrect result.

Fixing this up should be fairly easy.

Alan Stern

#2 lore raw
Quoted 11 lines · Alan Stern

drivers/usb/host/pci-quirks.c int usb_amd_find_chipset_info(void) · line 96

Quoted 6 lines · Joerg Roedel
drivers/usb/host/pci-quirks.c +0 -0
 	amd_chipset.probe_count++;
 	/* probe only once */
 	if (amd_chipset.probe_count > 1) {
 		spin_unlock_irqrestore(&amd_lock, flags);
 		return amd_chipset.probe_result;
 	}

The counter really should be a bool: Has the chipset already been probed or not? After all, nobody cares how many times this routine was called.

The quirk function needs a re-structuring anyway. The code-path is not very straight-forward and readable. But for this fix I tried to keep the changes small.

Quoted 8 lines

Review

Quoted 2 lines
(quoted diff) +1 -0
+	spin_unlock_irqrestore(&amd_lock, flags);

This code now contains a bug: You incremented the probe_count _before_ doing the probe. If another thread calls this routine right now, it will get an incorrect result.

Fixing this up should be fairly easy.

Okay, I will do. Thanks for the feedback.

Joerg

Signature · 5 lines
AMD Operating System Research Center

Advanced Micro Devices GmbH Einsteinring 24 85609 Dornach
General Managers: Alberto Bozzo, Andrew Bowd
Registration: Dornach, Landkr. Muenchen; Registerger. Muenchen, HRB Nr. 43632
#3 lore raw
In reply to Alan Stern
Quoted 5 lines

-----Original Message-----
From: Alan Stern [mailto:[email protected]]
Sent: Wednesday, April 06, 2011 11:16 PM
To: Roedel, Joerg
Cc: Greg Kroah-Hartman; Sarah Sharp; Xu, Andiry;

[email protected];

Quoted 23 lines

[email protected]; [email protected]
Subject: Re: [PATCH] USB host: Fix lockdep warning in AMD PLL quirk

Quoted 4 lines · Joerg Roedel

Booting 2.6.38 on my test machine produces a lockdep warning from the usb_amd_find_chipset_info() function:

 WARNING: at /data/lemmy/linux.trees.git/kernel/lockdep.c:2465

lockdep_trace_alloc+0x95/0xc2()

Quoted 13 lines
 Hardware name: Snook
 Modules linked in:
 Pid: 959, comm: work_for_cpu Not tainted 2.6.39-rc2+ #22
 Call Trace:
  [<ffffffff8103c0d4>] warn_slowpath_common+0x80/0x98
  [<ffffffff812387e6>] ? T.492+0x24/0x26
  [<ffffffff8103c101>] warn_slowpath_null+0x15/0x17
  [<ffffffff81068667>] lockdep_trace_alloc+0x95/0xc2
  [<ffffffff810ed9ac>] slab_pre_alloc_hook+0x18/0x3b
  [<ffffffff810ef227>] kmem_cache_alloc_trace+0x25/0xba
  [<ffffffff812387e6>] T.492+0x24/0x26
  [<ffffffff81238816>] pci_get_subsys+0x2e/0x73
 sr0: scsi3-mmc drive: 48x/48x writer dvd-ram cd/rw xa/form2 cdda

tray

Quoted 5 lines
Quoted 5 lines
  [<ffffffff8123886c>] pci_get_device+0x11/0x13
  [<ffffffff814082a9>] usb_amd_find_chipset_info+0x3f/0x18a

...

It turns out that this function calls pci_get_device under a

spin_lock

Quoted 1 line
Quoted 1 line

with irqs disabled, but the pci_get_device function is only allowed

in

Quoted 4 lines
Quoted 4 lines

preemptible context.

This patch fixes this by using temporary variables in the quirk algorithm and commiting them later to the struct under the lock.

This

Quoted 1 line
Quoted 1 line

moves all pci_get_device() invocations out of the spin_lock and

fixes

Quoted 6 lines
Quoted 6 lines

the lockdep warning for me.

---
 drivers/usb/host/pci-quirks.c |   67

+++++++++++++++++++++++-----------

Quoted 15 lines

------

drivers/usb/host/pci-

Quoted 3 lines
 1 files changed, 38 insertions(+), 29 deletions(-)
drivers/usb/host/pci- renamed from drivers/usb/host/pci-quirks.c +0 -0

quirks.c

Quoted 10 lines

index 1d586d4..d3e5cf3 100644

drivers/usb/host/pci-quirks.c +2 -0
@@ -84,64 +84,73 @@ int usb_amd_find_chipset_info(void)
 {
 	u8 rev = 0;
 	unsigned long flags;
+	struct pci_dev *nb_dev, *smbus_dev;
+	int nb_type, sb_type;
 
Quoted 11 lines

drivers/usb/host/pci-quirks.c int usb_amd_find_chipset_info(void) · line 96

Quoted 8 lines
drivers/usb/host/pci-quirks.c +0 -1
 	spin_lock_irqsave(&amd_lock, flags);
-
 	amd_chipset.probe_count++;
 	/* probe only once */
 	if (amd_chipset.probe_count > 1) {
 		spin_unlock_irqrestore(&amd_lock, flags);
 		return amd_chipset.probe_result;
 	}

The counter really should be a bool: Has the chipset already been probed or not? After all, nobody cares how many times this routine

was

Quoted 2 lines

called.

The probe routine increases probe_count counter to remember how many hosts calls it. Every host that calls probe routine in initialization will call usb_amd_dev_put() in *hci_stop() and decrease probe_count, so the last caller can safely free the resources.

Thanks,
Andiry

#4 lore raw
In reply to Alan Stern
Quoted 6 lines · Alan Stern

On Wed, 6 Apr 2011, Joerg Roedel wrote:

The counter really should be a bool: Has the chipset already been probed or not? After all, nobody cares how many times this routine was called.

Quoted 7 lines

Review

Quoted 1 line
(quoted diff) +1 -0
+	spin_unlock_irqrestore(&amd_lock, flags);

This code now contains a bug: You incremented the probe_count _before_ doing the probe. If another thread calls this routine right now, it will get an incorrect result.

Fixing this up should be fairly easy.

Hmm, we can get rid of the amd_lock completly if every thread uses the following call-order:

	usb_amd_find_chipset_info();
	usb_amd_quirk_pll_enable();
	usb_amd_quirk_pll_disable();
	usb_amd_dev_put();

In that case we can just change the probe_count and isoc_reqs into atomic_t with some care and should be fine, no?
Problem is that I don't know if the above call-order is guaranteed.

Joerg

Signature · 5 lines
AMD Operating System Research Center

Advanced Micro Devices GmbH Einsteinring 24 85609 Dornach
General Managers: Alberto Bozzo, Andrew Bowd
Registration: Dornach, Landkr. Muenchen; Registerger. Muenchen, HRB Nr. 43632
#13 lore raw

PATCH 1/1 USB host: Fix lockdep warning in AMD PLL quirk

In reply to Alan Stern

Booting 2.6.38 on my test machine produces a lockdep warning from the usb_amd_find_chipset_info() function:

 WARNING: at /data/lemmy/linux.trees.git/kernel/lockdep.c:2465 lockdep_trace_alloc+0x95/0xc2()
 Hardware name: Snook
 Modules linked in:
 Pid: 959, comm: work_for_cpu Not tainted 2.6.39-rc2+ #22
 Call Trace:
  [<ffffffff8103c0d4>] warn_slowpath_common+0x80/0x98
  [<ffffffff812387e6>] ? T.492+0x24/0x26
  [<ffffffff8103c101>] warn_slowpath_null+0x15/0x17
  [<ffffffff81068667>] lockdep_trace_alloc+0x95/0xc2
  [<ffffffff810ed9ac>] slab_pre_alloc_hook+0x18/0x3b
  [<ffffffff810ef227>] kmem_cache_alloc_trace+0x25/0xba
  [<ffffffff812387e6>] T.492+0x24/0x26
  [<ffffffff81238816>] pci_get_subsys+0x2e/0x73
 sr0: scsi3-mmc drive: 48x/48x writer dvd-ram cd/rw xa/form2 cdda tray
  [<ffffffff8123886c>] pci_get_device+0x11/0x13
  [<ffffffff814082a9>] usb_amd_find_chipset_info+0x3f/0x18a

...

It turns out that this function calls pci_get_device under a spin_lock with irqs disabled, but the pci_get_device function is only allowed in preemptible context.

This patch fixes this by using temporary variables in the quirk algorithm and commiting them later to the struct under the lock. This moves all pci_get_device() invocations out of the spin_lock and fixes the lockdep warning.

---
 drivers/usb/host/pci-quirks.c |   86 +++++++++++++++++++++++++----------------
 1 files changed, 52 insertions(+), 34 deletions(-)
drivers/usb/host/pci-quirks.c +52 -34
index 1d586d4..b5802f6 100644
@@ -84,65 +84,83 @@ int usb_amd_find_chipset_info(void)
 {
 	u8 rev = 0;
 	unsigned long flags;
+	struct amd_chipset_info info;
+	int ret;
 
 	spin_lock_irqsave(&amd_lock, flags);
 
-	amd_chipset.probe_count++;
 	/* probe only once */
-	if (amd_chipset.probe_count > 1) {
+	if (amd_chipset.probe_count > 0) {
 		spin_unlock_irqrestore(&amd_lock, flags);
 		return amd_chipset.probe_result;
 	}
+	info = amd_chipset;
+	spin_unlock_irqrestore(&amd_lock, flags);
 
-	amd_chipset.smbus_dev = pci_get_device(PCI_VENDOR_ID_ATI, 0x4385, NULL);
-	if (amd_chipset.smbus_dev) {
-		rev = amd_chipset.smbus_dev->revision;
+	info.smbus_dev = pci_get_device(PCI_VENDOR_ID_ATI, 0x4385, NULL);
+	if (info.smbus_dev) {
+		rev = info.smbus_dev->revision;
 		if (rev >= 0x40)
-			amd_chipset.sb_type = 1;
+			info.sb_type = 1;
 		else if (rev >= 0x30 && rev <= 0x3b)
-			amd_chipset.sb_type = 3;
+			info.sb_type = 3;
 	} else {
-		amd_chipset.smbus_dev = pci_get_device(PCI_VENDOR_ID_AMD,
-							0x780b, NULL);
-		if (!amd_chipset.smbus_dev) {
-			spin_unlock_irqrestore(&amd_lock, flags);
-			return 0;
+		info.smbus_dev = pci_get_device(PCI_VENDOR_ID_AMD,
+						0x780b, NULL);
+		if (!info.smbus_dev) {
+			ret = 0;
+			goto commit;
 		}
-		rev = amd_chipset.smbus_dev->revision;
+
+		rev = info.smbus_dev->revision;
 		if (rev >= 0x11 && rev <= 0x18)
-			amd_chipset.sb_type = 2;
+			info.sb_type = 2;
 	}
 
-	if (amd_chipset.sb_type == 0) {
-		if (amd_chipset.smbus_dev) {
-			pci_dev_put(amd_chipset.smbus_dev);
-			amd_chipset.smbus_dev = NULL;
+	if (info.sb_type == 0) {
+		if (info.smbus_dev) {
+			pci_dev_put(info.smbus_dev);
+			info.smbus_dev = NULL;
 		}
-		spin_unlock_irqrestore(&amd_lock, flags);
-		return 0;
+		ret = 0;
+		goto commit;
 	}
 
-	amd_chipset.nb_dev = pci_get_device(PCI_VENDOR_ID_AMD, 0x9601, NULL);
-	if (amd_chipset.nb_dev) {
-		amd_chipset.nb_type = 1;
+	info.nb_dev = pci_get_device(PCI_VENDOR_ID_AMD, 0x9601, NULL);
+	if (info.nb_dev) {
+		info.nb_type = 1;
 	} else {
-		amd_chipset.nb_dev = pci_get_device(PCI_VENDOR_ID_AMD,
-							0x1510, NULL);
-		if (amd_chipset.nb_dev) {
-			amd_chipset.nb_type = 2;
-		} else  {
-			amd_chipset.nb_dev = pci_get_device(PCI_VENDOR_ID_AMD,
-								0x9600, NULL);
-			if (amd_chipset.nb_dev)
-				amd_chipset.nb_type = 3;
+		info.nb_dev = pci_get_device(PCI_VENDOR_ID_AMD, 0x1510, NULL);
+		if (info.nb_dev) {
+			info.nb_type = 2;
+		} else {
+			info.nb_dev = pci_get_device(PCI_VENDOR_ID_AMD,
+						     0x9600, NULL);
+			if (info.nb_dev)
+				info.nb_type = 3;
 		}
 	}
 
-	amd_chipset.probe_result = 1;
+	ret = info.probe_result = 1;
 	printk(KERN_DEBUG "QUIRK: Enable AMD PLL fix\n");
 
+commit:
+
+	spin_lock_irqsave(&amd_lock, flags);
+	if (amd_chipset.probe_count > 0) {
+		/* race - someone else was faster - drop devices */
+		if (info.nb_dev)
+			pci_dev_put(info.nb_dev);
+		if (info.smbus_dev)
+			pci_dev_put(info.smbus_dev);
+	} else {
+		/* no race - commit the result */
+		info.probe_count++;
+		amd_chipset = info;
+	}
 	spin_unlock_irqrestore(&amd_lock, flags);
-	return amd_chipset.probe_result;
+
+	return ret;
 }
 EXPORT_SYMBOL_GPL(usb_amd_find_chipset_info);
 
Signature · 1 line
1.7.1
#5 lore raw
In reply to Roedel, Joerg
Quoted 5 lines

-----Original Message-----
From: Roedel, Joerg [mailto:[email protected]]
Sent: Thursday, April 07, 2011 3:51 PM
To: Alan Stern
Cc: Greg Kroah-Hartman; Sarah Sharp; Xu, Andiry;

[email protected];

Quoted 8 lines

[email protected]; [email protected]
Subject: Re: [PATCH] USB host: Fix lockdep warning in AMD PLL quirk

Quoted 4 lines · Alan Stern

On Wed, 6 Apr 2011, Joerg Roedel wrote:

The counter really should be a bool: Has the chipset already been probed or not? After all, nobody cares how many times this routine

was

Quoted 5 lines
Quoted 5 lines

called.

Review

Quoted 1 line
(quoted diff) +1 -0
+	spin_unlock_irqrestore(&amd_lock, flags);

This code now contains a bug: You incremented the probe_count

_before_

Quoted 5 lines
Quoted 4 lines

doing the probe. If another thread calls this routine right now, it will get an incorrect result.

Fixing this up should be fairly easy.

Quoted 8 lines

Hmm, we can get rid of the amd_lock completly if every thread uses the following call-order:

	usb_amd_find_chipset_info();
	usb_amd_quirk_pll_enable();
	usb_amd_quirk_pll_disable();
	usb_amd_dev_put();

The correct order is:

usb_amd_find_chipset_info();
usb_amd_quirk_pll_disable();
usb_amd_quirk_pll_enable();
usb_amd_dev_put();

The pair of pll disable and enable may be called for multiple times.

Thanks,
Andiry

#14 lore raw
In reply to Joerg Roedel
Quoted 5 lines

-----Original Message-----
From: Joerg Roedel [mailto:[email protected]]
Sent: Thursday, April 07, 2011 4:26 PM
To: Greg Kroah-Hartman; Sarah Sharp; Xu, Andiry
Cc: [email protected]; [email protected]; Roedel,

Joerg;

Quoted 34 lines

[email protected]
Subject: [PATCH] USB host: Fix lockdep warning in AMD PLL quirk

Booting 2.6.38 on my test machine produces a lockdep warning from the usb_amd_find_chipset_info() function:

 WARNING: at /data/lemmy/linux.trees.git/kernel/lockdep.c:2465
lockdep_trace_alloc+0x95/0xc2()
 Hardware name: Snook
 Modules linked in:
 Pid: 959, comm: work_for_cpu Not tainted 2.6.39-rc2+ #22
 Call Trace:
  [<ffffffff8103c0d4>] warn_slowpath_common+0x80/0x98
  [<ffffffff812387e6>] ? T.492+0x24/0x26
  [<ffffffff8103c101>] warn_slowpath_null+0x15/0x17
  [<ffffffff81068667>] lockdep_trace_alloc+0x95/0xc2
  [<ffffffff810ed9ac>] slab_pre_alloc_hook+0x18/0x3b
  [<ffffffff810ef227>] kmem_cache_alloc_trace+0x25/0xba
  [<ffffffff812387e6>] T.492+0x24/0x26
  [<ffffffff81238816>] pci_get_subsys+0x2e/0x73
 sr0: scsi3-mmc drive: 48x/48x writer dvd-ram cd/rw xa/form2 cdda tray
  [<ffffffff8123886c>] pci_get_device+0x11/0x13
  [<ffffffff814082a9>] usb_amd_find_chipset_info+0x3f/0x18a

...

It turns out that this function calls pci_get_device under a spin_lock with irqs disabled, but the pci_get_device function is only allowed in preemptible context.

This patch fixes this by using temporary variables in the quirk algorithm and commiting them later to the struct under the lock. This moves all pci_get_device() invocations out of the spin_lock and fixes the lockdep warning.

Quoted 4 lines
---
 drivers/usb/host/pci-quirks.c |   86

+++++++++++++++++++++++++-----------

Review

Quoted 4 lines

-----

 1 files changed, 52 insertions(+), 34 deletions(-)
(quoted diff) +0 -0

b/drivers/usb/host/pci-quirks.c

Quoted 13 lines

index 1d586d4..b5802f6 100644

drivers/usb/host/pci-quirks.c +2 -1
@@ -84,65 +84,83 @@ int usb_amd_find_chipset_info(void)
 {
 	u8 rev = 0;
 	unsigned long flags;
+	struct amd_chipset_info info;
+	int ret;
 
 	spin_lock_irqsave(&amd_lock, flags);
 
-	amd_chipset.probe_count++;

drivers/usb/host/pci-quirks.c int usb_amd_find_chipset_info(void) · line 97

Quoted 10 lines
drivers/usb/host/pci-quirks.c +3 -2
 	/* probe only once */
-	if (amd_chipset.probe_count > 1) {
+	if (amd_chipset.probe_count > 0) {
 		spin_unlock_irqrestore(&amd_lock, flags);
 		return amd_chipset.probe_result;
 	}
+	info = amd_chipset;
+	spin_unlock_irqrestore(&amd_lock, flags);
 
-	amd_chipset.smbus_dev = pci_get_device(PCI_VENDOR_ID_ATI,

0x4385,

drivers/usb/host/pci-quirks.c int usb_amd_find_chipset_info(void)

Quoted 4 lines
drivers/usb/host/pci-quirks.c +1 -2
 NULL);
-	if (amd_chipset.smbus_dev) {
-		rev = amd_chipset.smbus_dev->revision;
+	info.smbus_dev = pci_get_device(PCI_VENDOR_ID_ATI, 0x4385,

NULL);

drivers/usb/host/pci-quirks.c int usb_amd_find_chipset_info(void)

Quoted 10 lines
drivers/usb/host/pci-quirks.c +4 -3
+	if (info.smbus_dev) {
+		rev = info.smbus_dev->revision;
 		if (rev >= 0x40)
-			amd_chipset.sb_type = 1;
+			info.sb_type = 1;
 		else if (rev >= 0x30 && rev <= 0x3b)
-			amd_chipset.sb_type = 3;
+			info.sb_type = 3;
 	} else {
-		amd_chipset.smbus_dev =

pci_get_device(PCI_VENDOR_ID_AMD,

Quoted 24 lines
drivers/usb/host/pci-quirks.c +10 -10
-							0x780b, NULL);
-		if (!amd_chipset.smbus_dev) {
-			spin_unlock_irqrestore(&amd_lock, flags);
-			return 0;
+		info.smbus_dev = pci_get_device(PCI_VENDOR_ID_AMD,
+						0x780b, NULL);
+		if (!info.smbus_dev) {
+			ret = 0;
+			goto commit;
 		}
-		rev = amd_chipset.smbus_dev->revision;
+
+		rev = info.smbus_dev->revision;
 		if (rev >= 0x11 && rev <= 0x18)
-			amd_chipset.sb_type = 2;
+			info.sb_type = 2;
 	}
 
-	if (amd_chipset.sb_type == 0) {
-		if (amd_chipset.smbus_dev) {
-			pci_dev_put(amd_chipset.smbus_dev);
-			amd_chipset.smbus_dev = NULL;
+	if (info.sb_type == 0) {
+		if (info.smbus_dev) {

drivers/usb/host/pci-quirks.c int usb_amd_find_chipset_info(void)

Quoted 10 lines
drivers/usb/host/pci-quirks.c +4 -3
+			pci_dev_put(info.smbus_dev);
+			info.smbus_dev = NULL;
 		}
-		spin_unlock_irqrestore(&amd_lock, flags);
-		return 0;
+		ret = 0;
+		goto commit;
 	}
 
-	amd_chipset.nb_dev = pci_get_device(PCI_VENDOR_ID_AMD, 0x9601,

NULL);

drivers/usb/host/pci-quirks.c int usb_amd_find_chipset_info(void)

Quoted 12 lines
drivers/usb/host/pci-quirks.c +3 -8
-	if (amd_chipset.nb_dev) {
-		amd_chipset.nb_type = 1;
+	info.nb_dev = pci_get_device(PCI_VENDOR_ID_AMD, 0x9601, NULL);
+	if (info.nb_dev) {
+		info.nb_type = 1;
 	} else {
-		amd_chipset.nb_dev = pci_get_device(PCI_VENDOR_ID_AMD,
-							0x1510, NULL);
-		if (amd_chipset.nb_dev) {
-			amd_chipset.nb_type = 2;
-		} else  {
-			amd_chipset.nb_dev =

pci_get_device(PCI_VENDOR_ID_AMD,

drivers/usb/host/pci-quirks.c int usb_amd_find_chipset_info(void) · line 136

Quoted 1 line
drivers/usb/host/pci-quirks.c +0 -1
-								0x9600,

NULL);

drivers/usb/host/pci-quirks.c int usb_amd_find_chipset_info(void)

Quoted 3 lines
drivers/usb/host/pci-quirks.c +1 -2
-			if (amd_chipset.nb_dev)
-				amd_chipset.nb_type = 3;
+		info.nb_dev = pci_get_device(PCI_VENDOR_ID_AMD, 0x1510,

NULL);

Quoted 13 lines
drivers/usb/host/pci-quirks.c +8 -1
+		if (info.nb_dev) {
+			info.nb_type = 2;
+		} else {
+			info.nb_dev = pci_get_device(PCI_VENDOR_ID_AMD,
+						     0x9600, NULL);
+			if (info.nb_dev)
+				info.nb_type = 3;
 		}
 	}
 
-	amd_chipset.probe_result = 1;
+	ret = info.probe_result = 1;
 	printk(KERN_DEBUG "QUIRK: Enable AMD PLL fix\n");

drivers/usb/host/pci-quirks.c int usb_amd_find_chipset_info(void) · line 155

Quoted 10 lines
drivers/usb/host/pci-quirks.c +9 -0
 
+commit:
+
+	spin_lock_irqsave(&amd_lock, flags);
+	if (amd_chipset.probe_count > 0) {
+		/* race - someone else was faster - drop devices */
+		if (info.nb_dev)
+			pci_dev_put(info.nb_dev);
+		if (info.smbus_dev)
+			pci_dev_put(info.smbus_dev);

You need to increase info.probe_count here and set amd_chipset.probe_count with it, because it's decreased during every host controller stop routine.

Quoted 13 lines
drivers/usb/host/pci-quirks.c +7 -2
+	} else {
+		/* no race - commit the result */
+		info.probe_count++;
+		amd_chipset = info;
+	}
 	spin_unlock_irqrestore(&amd_lock, flags);
-	return amd_chipset.probe_result;
+
+	return ret;
 }
 EXPORT_SYMBOL_GPL(usb_amd_find_chipset_info);
 
--
Quoted 1 line

1.7.1

The original design target is to probe the chipset only once, so other host controllers will not probe the chipset again. This patch allows multiple threads to probe the chipset at the same time, and only one thread is doing the valid work. It may add unnecessary cost.

Thanks,
Andiry

#15 lore raw
Quoted 11 lines · Andiry

drivers/usb/host/pci-quirks.c int usb_amd_find_chipset_info(void) · line 155

Quoted 7 lines
drivers/usb/host/pci-quirks.c +7 -0
+     spin_lock_irqsave(&amd_lock, flags);
+     if (amd_chipset.probe_count > 0) {
+             /* race - someone else was faster - drop devices */
+             if (info.nb_dev)
+                     pci_dev_put(info.nb_dev);
+             if (info.smbus_dev)
+                     pci_dev_put(info.smbus_dev);

You need to increase info.probe_count here and set amd_chipset.probe_count with it, because it's decreased during every host controller stop routine.

Right, the correct return value needs to be set too. I'll fix it.

Quoted 12 lines
Quoted 12 lines
drivers/usb/host/pci-quirks.c +7 -1
+     } else {
+             /* no race - commit the result */
+             info.probe_count++;
+             amd_chipset = info;
+     }
      spin_unlock_irqrestore(&amd_lock, flags);
-     return amd_chipset.probe_result;
+
+     return ret;
 }
 EXPORT_SYMBOL_GPL(usb_amd_find_chipset_info);
Quoted 7 lines
Quoted 2 lines

--
1.7.1

The original design target is to probe the chipset only once, so other host controllers will not probe the chipset again. This patch allows multiple threads to probe the chipset at the same time, and only one thread is doing the valid work. It may add unnecessary cost.

Not necessarily. With this patch more than one thread could do the valid work at the same time. With the old code the other threads would have spent this time spinning on the lock. So I don't think there is much additional cost introduced with this patch. And as a plus, it fixes a bug :-)
I'll send a fixed version soon.

Joerg

Signature · 5 lines
AMD Operating System Research Center

Advanced Micro Devices GmbH Einsteinring 24 85609 Dornach
General Managers: Alberto Bozzo, Andrew Bowd
Registration: Dornach, Landkr. Muenchen; Registerger. Muenchen, HRB Nr. 43632
#6 lore raw
In reply to Xu, Andiry
Quoted 32 lines · Andiry
Quoted 23 lines

-----Original Message-----
From: Roedel, Joerg [mailto:[email protected]]
Sent: Thursday, April 07, 2011 3:51 PM
To: Alan Stern
Cc: Greg Kroah-Hartman; Sarah Sharp; Xu, Andiry; [email protected]; [email protected]; [email protected]
Subject: Re: [PATCH] USB host: Fix lockdep warning in AMD PLL quirk

Quoted 6 lines · Alan Stern

On Wed, 6 Apr 2011, Joerg Roedel wrote:

The counter really should be a bool: Has the chipset already been probed or not? After all, nobody cares how many times this routine was called.

Quoted 7 lines
Quoted 1 line

+ spin_unlock_irqrestore(&amd_lock, flags);

This code now contains a bug: You incremented the probe_count _before_ doing the probe. If another thread calls this routine right now, it will get an incorrect result.

Fixing this up should be fairly easy.

Quoted 8 lines

Hmm, we can get rid of the amd_lock completly if every thread uses the following call-order:

      usb_amd_find_chipset_info();
      usb_amd_quirk_pll_enable();
      usb_amd_quirk_pll_disable();
      usb_amd_dev_put();
Quoted 8 lines

The correct order is:

usb_amd_find_chipset_info();
usb_amd_quirk_pll_disable();
usb_amd_quirk_pll_enable();
usb_amd_dev_put();

The pair of pll disable and enable may be called for multiple times.

So we could access the data structure without any locks if we want using atomic_t for the probe_count and isoc_reqs members. But as I've seen meanwhile the lock still needs to protect the access to the hardware in the usb_amd_quirk_pll() function.
So its probably not worth the work, what do you think?

Regards,

Joerg

Signature · 5 lines
AMD Operating System Research Center

Advanced Micro Devices GmbH Einsteinring 24 85609 Dornach
General Managers: Alberto Bozzo, Andrew Bowd
Registration: Dornach, Landkr. Muenchen; Registerger. Muenchen, HRB Nr. 43632
#16 lore raw

PATCH 1/1 USB host: Fix lockdep warning in AMD PLL quirk

In reply to Xu, Andiry

Booting 2.6.38 on my test machine produces a lockdep warning from the usb_amd_find_chipset_info() function:

 WARNING: at /data/lemmy/linux.trees.git/kernel/lockdep.c:2465 lockdep_trace_alloc+0x95/0xc2()
 Hardware name: Snook
 Modules linked in:
 Pid: 959, comm: work_for_cpu Not tainted 2.6.39-rc2+ #22
 Call Trace:
  [<ffffffff8103c0d4>] warn_slowpath_common+0x80/0x98
  [<ffffffff812387e6>] ? T.492+0x24/0x26
  [<ffffffff8103c101>] warn_slowpath_null+0x15/0x17
  [<ffffffff81068667>] lockdep_trace_alloc+0x95/0xc2
  [<ffffffff810ed9ac>] slab_pre_alloc_hook+0x18/0x3b
  [<ffffffff810ef227>] kmem_cache_alloc_trace+0x25/0xba
  [<ffffffff812387e6>] T.492+0x24/0x26
  [<ffffffff81238816>] pci_get_subsys+0x2e/0x73
 sr0: scsi3-mmc drive: 48x/48x writer dvd-ram cd/rw xa/form2 cdda tray
  [<ffffffff8123886c>] pci_get_device+0x11/0x13
  [<ffffffff814082a9>] usb_amd_find_chipset_info+0x3f/0x18a

...

It turns out that this function calls pci_get_device under a spin_lock with irqs disabled, but the pci_get_device function is only allowed in preemptible context.

This patch fixes this by using temporary variables in the quirk algorithm and commiting them later to the struct under the lock. This moves all pci_get_device() invocations out of the spin_lock and fixes the lockdep warning.

---
 drivers/usb/host/pci-quirks.c |   90 +++++++++++++++++++++++++---------------
 1 files changed, 56 insertions(+), 34 deletions(-)
drivers/usb/host/pci-quirks.c +56 -34
index 1d586d4..c21b073 100644
@@ -84,65 +84,87 @@ int usb_amd_find_chipset_info(void)
 {
 	u8 rev = 0;
 	unsigned long flags;
+	struct amd_chipset_info info;
+	int ret;
 
 	spin_lock_irqsave(&amd_lock, flags);
 
-	amd_chipset.probe_count++;
 	/* probe only once */
-	if (amd_chipset.probe_count > 1) {
+	if (amd_chipset.probe_count > 0) {
 		spin_unlock_irqrestore(&amd_lock, flags);
 		return amd_chipset.probe_result;
 	}
+	info = amd_chipset;
+	spin_unlock_irqrestore(&amd_lock, flags);
 
-	amd_chipset.smbus_dev = pci_get_device(PCI_VENDOR_ID_ATI, 0x4385, NULL);
-	if (amd_chipset.smbus_dev) {
-		rev = amd_chipset.smbus_dev->revision;
+	info.smbus_dev = pci_get_device(PCI_VENDOR_ID_ATI, 0x4385, NULL);
+	if (info.smbus_dev) {
+		rev = info.smbus_dev->revision;
 		if (rev >= 0x40)
-			amd_chipset.sb_type = 1;
+			info.sb_type = 1;
 		else if (rev >= 0x30 && rev <= 0x3b)
-			amd_chipset.sb_type = 3;
+			info.sb_type = 3;
 	} else {
-		amd_chipset.smbus_dev = pci_get_device(PCI_VENDOR_ID_AMD,
-							0x780b, NULL);
-		if (!amd_chipset.smbus_dev) {
-			spin_unlock_irqrestore(&amd_lock, flags);
-			return 0;
+		info.smbus_dev = pci_get_device(PCI_VENDOR_ID_AMD,
+						0x780b, NULL);
+		if (!info.smbus_dev) {
+			ret = 0;
+			goto commit;
 		}
-		rev = amd_chipset.smbus_dev->revision;
+
+		rev = info.smbus_dev->revision;
 		if (rev >= 0x11 && rev <= 0x18)
-			amd_chipset.sb_type = 2;
+			info.sb_type = 2;
 	}
 
-	if (amd_chipset.sb_type == 0) {
-		if (amd_chipset.smbus_dev) {
-			pci_dev_put(amd_chipset.smbus_dev);
-			amd_chipset.smbus_dev = NULL;
+	if (info.sb_type == 0) {
+		if (info.smbus_dev) {
+			pci_dev_put(info.smbus_dev);
+			info.smbus_dev = NULL;
 		}
-		spin_unlock_irqrestore(&amd_lock, flags);
-		return 0;
+		ret = 0;
+		goto commit;
 	}
 
-	amd_chipset.nb_dev = pci_get_device(PCI_VENDOR_ID_AMD, 0x9601, NULL);
-	if (amd_chipset.nb_dev) {
-		amd_chipset.nb_type = 1;
+	info.nb_dev = pci_get_device(PCI_VENDOR_ID_AMD, 0x9601, NULL);
+	if (info.nb_dev) {
+		info.nb_type = 1;
 	} else {
-		amd_chipset.nb_dev = pci_get_device(PCI_VENDOR_ID_AMD,
-							0x1510, NULL);
-		if (amd_chipset.nb_dev) {
-			amd_chipset.nb_type = 2;
-		} else  {
-			amd_chipset.nb_dev = pci_get_device(PCI_VENDOR_ID_AMD,
-								0x9600, NULL);
-			if (amd_chipset.nb_dev)
-				amd_chipset.nb_type = 3;
+		info.nb_dev = pci_get_device(PCI_VENDOR_ID_AMD, 0x1510, NULL);
+		if (info.nb_dev) {
+			info.nb_type = 2;
+		} else {
+			info.nb_dev = pci_get_device(PCI_VENDOR_ID_AMD,
+						     0x9600, NULL);
+			if (info.nb_dev)
+				info.nb_type = 3;
 		}
 	}
 
-	amd_chipset.probe_result = 1;
+	ret = info.probe_result = 1;
 	printk(KERN_DEBUG "QUIRK: Enable AMD PLL fix\n");
 
+commit:
+
+	spin_lock_irqsave(&amd_lock, flags);
+	if (amd_chipset.probe_count > 0) {
+		/* race - someone else was faster - drop devices */
+		if (info.nb_dev)
+			pci_dev_put(info.nb_dev);
+		if (info.smbus_dev)
+			pci_dev_put(info.smbus_dev);
+
+		/* Mark that we where here */
+		amd_chipset.probe_count++;
+		ret = amd_chipset.probe_result;
+	} else {
+		/* no race - commit the result */
+		info.probe_count++;
+		amd_chipset = info;
+	}
 	spin_unlock_irqrestore(&amd_lock, flags);
-	return amd_chipset.probe_result;
+
+	return ret;
 }
 EXPORT_SYMBOL_GPL(usb_amd_find_chipset_info);
 
Signature · 1 line
1.7.1
#31 lore raw
In reply to Joerg Roedel
Quoted 2 lines · Joerg Roedel

Booting 2.6.38 on my test machine produces a lockdep warning from the usb_amd_find_chipset_info() function:

When posting revised patches, you need to indicate this fact in the email Subject: line. For example:

[PATCH v.2] USB host: Fix lockdep warning in AMD PLL quirk

Otherwise people reading the email won't know which patch it refers to.

Alan Stern

#7 lore raw
In reply to Roedel, Joerg
Quoted 5 lines · Joerg

So we could access the data structure without any locks if we want using atomic_t for the probe_count and isoc_reqs members. But as I've seen meanwhile the lock still needs to protect the access to the hardware in the usb_amd_quirk_pll() function.
So its probably not worth the work, what do you think?

You might as well use the spinlock.

However, is there a good reason to zero out the amd_chipset members in usb_amd_dev_put()? Can these things be added and removed dynamically? If they can't then the data should remain valid indefinitely once it has been probed, and you could call pci_dev_put() at the end of usb_amd_find_chipset_info().

And if they can, is it valid to call pci_dev_put() in usb_amd_dev_put() while holding a spinlock? You might want to move those calls to the end of the function.

Alan Stern

#32 lore raw
In reply to Alan Stern
Quoted 11 lines · Alan Stern
Quoted 2 lines · Joerg Roedel

Booting 2.6.38 on my test machine produces a lockdep warning from the usb_amd_find_chipset_info() function:

When posting revised patches, you need to indicate this fact in the email Subject: line. For example:

[PATCH v.2] USB host: Fix lockdep warning in AMD PLL quirk

Otherwise people reading the email won't know which patch it refers to.

Yes, I usually do this when I post patch series. For single patches I just send what 'git format-patch' gives me. I should probably edit that output in the future on single patches.
(This is btw. actually v3, sorry that I forgot to Cc you on v2)

Joerg

Signature · 5 lines
AMD Operating System Research Center

Advanced Micro Devices GmbH Einsteinring 24 85609 Dornach
General Managers: Alberto Bozzo, Andrew Bowd
Registration: Dornach, Landkr. Muenchen; Registerger. Muenchen, HRB Nr. 43632
#8 lore raw
In reply to Alan Stern
Quoted 9 lines · Alan Stern
Quoted 5 lines · Joerg

So we could access the data structure without any locks if we want using atomic_t for the probe_count and isoc_reqs members. But as I've seen meanwhile the lock still needs to protect the access to the hardware in the usb_amd_quirk_pll() function.
So its probably not worth the work, what do you think?

You might as well use the spinlock.

Yes, since we need it anyway for protecting the hardware-access we can leave everything as is (with the fix).

Quoted 5 lines

However, is there a good reason to zero out the amd_chipset members in usb_amd_dev_put()? Can these things be added and removed dynamically? If they can't then the data should remain valid indefinitely once it has been probed, and you could call pci_dev_put() at the end of usb_amd_find_chipset_info().

Well, in a real system it is indeed very unlikely that the chipset is hotplugged. But for formal correctness it is right to hold a reference to the pci_dev struct as long as we rely on a pointer to it.

Quoted 3 lines

And if they can, is it valid to call pci_dev_put() in usb_amd_dev_put() while holding a spinlock? You might want to move those calls to the end of the function.

I just had a look, pci_dev_put seems to be invalid in atomic context too. If the reference count drops to 0 (which is very unlikely for the chipset devices) the device and its kobject are released. This causes a uevent to be sent to userspace which does GFP_KERNEL allocations and all the stuff.
So for formal correctness the pci_dev_put calls need to be moved out of the spinlock too.

Regards,

Joerg

#9 lore raw

PATCH v4 1/1 USB host: Fix lockdep warning in AMD PLL quirk

Booting 2.6.38 on my test machine produces a lockdep warning from the usb_amd_find_chipset_info() function:

 WARNING: at /data/lemmy/linux.trees.git/kernel/lockdep.c:2465 lockdep_trace_alloc+0x95/0xc2()
 Hardware name: Snook
 Modules linked in:
 Pid: 959, comm: work_for_cpu Not tainted 2.6.39-rc2+ #22
 Call Trace:
  [<ffffffff8103c0d4>] warn_slowpath_common+0x80/0x98
  [<ffffffff812387e6>] ? T.492+0x24/0x26
  [<ffffffff8103c101>] warn_slowpath_null+0x15/0x17
  [<ffffffff81068667>] lockdep_trace_alloc+0x95/0xc2
  [<ffffffff810ed9ac>] slab_pre_alloc_hook+0x18/0x3b
  [<ffffffff810ef227>] kmem_cache_alloc_trace+0x25/0xba
  [<ffffffff812387e6>] T.492+0x24/0x26
  [<ffffffff81238816>] pci_get_subsys+0x2e/0x73
  [<ffffffff8123886c>] pci_get_device+0x11/0x13
  [<ffffffff814082a9>] usb_amd_find_chipset_info+0x3f/0x18a

...

It turns out that this function calls pci_get_device under a spin_lock with irqs disabled, but the pci_get_device function is only allowed in preemptible context.

This patch fixes the warning by making all data-structure modifications on temporal storage and commiting this back into the visible structure at the end. While at it, this patch also moves the pci_dev_put calls out of the spinlocks because this function might sleep too.

---
 drivers/usb/host/pci-quirks.c |  117 ++++++++++++++++++++++++++---------------
 1 files changed, 74 insertions(+), 43 deletions(-)
drivers/usb/host/pci-quirks.c +74 -43
index 1d586d4..dfc639a 100644
@@ -84,65 +84,91 @@ int usb_amd_find_chipset_info(void)
 {
 	u8 rev = 0;
 	unsigned long flags;
+	struct amd_chipset_info info;
+	int ret;
 
 	spin_lock_irqsave(&amd_lock, flags);
 
-	amd_chipset.probe_count++;
 	/* probe only once */
-	if (amd_chipset.probe_count > 1) {
+	if (amd_chipset.probe_count > 0) {
 		spin_unlock_irqrestore(&amd_lock, flags);
 		return amd_chipset.probe_result;
 	}
+	info = amd_chipset;
+	spin_unlock_irqrestore(&amd_lock, flags);
 
-	amd_chipset.smbus_dev = pci_get_device(PCI_VENDOR_ID_ATI, 0x4385, NULL);
-	if (amd_chipset.smbus_dev) {
-		rev = amd_chipset.smbus_dev->revision;
+	info.smbus_dev = pci_get_device(PCI_VENDOR_ID_ATI, 0x4385, NULL);
+	if (info.smbus_dev) {
+		rev = info.smbus_dev->revision;
 		if (rev >= 0x40)
-			amd_chipset.sb_type = 1;
+			info.sb_type = 1;
 		else if (rev >= 0x30 && rev <= 0x3b)
-			amd_chipset.sb_type = 3;
+			info.sb_type = 3;
 	} else {
-		amd_chipset.smbus_dev = pci_get_device(PCI_VENDOR_ID_AMD,
-							0x780b, NULL);
-		if (!amd_chipset.smbus_dev) {
-			spin_unlock_irqrestore(&amd_lock, flags);
-			return 0;
+		info.smbus_dev = pci_get_device(PCI_VENDOR_ID_AMD,
+						0x780b, NULL);
+		if (!info.smbus_dev) {
+			ret = 0;
+			goto commit;
 		}
-		rev = amd_chipset.smbus_dev->revision;
+
+		rev = info.smbus_dev->revision;
 		if (rev >= 0x11 && rev <= 0x18)
-			amd_chipset.sb_type = 2;
+			info.sb_type = 2;
 	}
 
-	if (amd_chipset.sb_type == 0) {
-		if (amd_chipset.smbus_dev) {
-			pci_dev_put(amd_chipset.smbus_dev);
-			amd_chipset.smbus_dev = NULL;
+	if (info.sb_type == 0) {
+		if (info.smbus_dev) {
+			pci_dev_put(info.smbus_dev);
+			info.smbus_dev = NULL;
 		}
-		spin_unlock_irqrestore(&amd_lock, flags);
-		return 0;
+		ret = 0;
+		goto commit;
 	}
 
-	amd_chipset.nb_dev = pci_get_device(PCI_VENDOR_ID_AMD, 0x9601, NULL);
-	if (amd_chipset.nb_dev) {
-		amd_chipset.nb_type = 1;
+	info.nb_dev = pci_get_device(PCI_VENDOR_ID_AMD, 0x9601, NULL);
+	if (info.nb_dev) {
+		info.nb_type = 1;
 	} else {
-		amd_chipset.nb_dev = pci_get_device(PCI_VENDOR_ID_AMD,
-							0x1510, NULL);
-		if (amd_chipset.nb_dev) {
-			amd_chipset.nb_type = 2;
-		} else  {
-			amd_chipset.nb_dev = pci_get_device(PCI_VENDOR_ID_AMD,
-								0x9600, NULL);
-			if (amd_chipset.nb_dev)
-				amd_chipset.nb_type = 3;
+		info.nb_dev = pci_get_device(PCI_VENDOR_ID_AMD, 0x1510, NULL);
+		if (info.nb_dev) {
+			info.nb_type = 2;
+		} else {
+			info.nb_dev = pci_get_device(PCI_VENDOR_ID_AMD,
+						     0x9600, NULL);
+			if (info.nb_dev)
+				info.nb_type = 3;
 		}
 	}
 
-	amd_chipset.probe_result = 1;
+	ret = info.probe_result = 1;
 	printk(KERN_DEBUG "QUIRK: Enable AMD PLL fix\n");
 
-	spin_unlock_irqrestore(&amd_lock, flags);
-	return amd_chipset.probe_result;
+commit:
+
+	spin_lock_irqsave(&amd_lock, flags);
+	if (amd_chipset.probe_count > 0) {
+		/* race - someone else was faster - drop devices */
+
+		/* Mark that we where here */
+		amd_chipset.probe_count++;
+		ret = amd_chipset.probe_result;
+
+		spin_unlock_irqrestore(&amd_lock, flags);
+
+		if (info.nb_dev)
+			pci_dev_put(info.nb_dev);
+		if (info.smbus_dev)
+			pci_dev_put(info.smbus_dev);
+
+	} else {
+		/* no race - commit the result */
+		info.probe_count++;
+		amd_chipset = info;
+		spin_unlock_irqrestore(&amd_lock, flags);
+	}
+
+	return ret;
 }
 EXPORT_SYMBOL_GPL(usb_amd_find_chipset_info);
 
@@ -284,8 +310,10 @@ EXPORT_SYMBOL_GPL(usb_amd_quirk_pll_enable);
 
 void usb_amd_dev_put(void)
 {
+	struct pci_dev *nb, *smbus;
 	unsigned long flags;
 
+
 	spin_lock_irqsave(&amd_lock, flags);
 
 	amd_chipset.probe_count--;
@@ -294,20 +322,23 @@ void usb_amd_dev_put(void)
 		return;
 	}
 
-	if (amd_chipset.nb_dev) {
-		pci_dev_put(amd_chipset.nb_dev);
-		amd_chipset.nb_dev = NULL;
-	}
-	if (amd_chipset.smbus_dev) {
-		pci_dev_put(amd_chipset.smbus_dev);
-		amd_chipset.smbus_dev = NULL;
-	}
+	/* save them to pci_dev_put outside of spinlock */
+	nb    = amd_chipset.nb_dev;
+	smbus = amd_chipset.smbus_dev;
+
+	amd_chipset.nb_dev = NULL;
+	amd_chipset.smbus_dev = NULL;
 	amd_chipset.nb_type = 0;
 	amd_chipset.sb_type = 0;
 	amd_chipset.isoc_reqs = 0;
 	amd_chipset.probe_result = 0;
 
 	spin_unlock_irqrestore(&amd_lock, flags);
+
+	if (nb)
+		pci_dev_put(nb);
+	if (smbus)
+		pci_dev_put(amd_chipset.smbus_dev);
 }
 EXPORT_SYMBOL_GPL(usb_amd_dev_put);
 
Signature · 1 line
1.7.1
#10 lore raw
Quoted 24 lines · Joerg Roedel

Booting 2.6.38 on my test machine produces a lockdep warning from the usb_amd_find_chipset_info() function:

 WARNING: at /data/lemmy/linux.trees.git/kernel/lockdep.c:2465 lockdep_trace_alloc+0x95/0xc2()
 Hardware name: Snook
 Modules linked in:
 Pid: 959, comm: work_for_cpu Not tainted 2.6.39-rc2+ #22
 Call Trace:
  [<ffffffff8103c0d4>] warn_slowpath_common+0x80/0x98
  [<ffffffff812387e6>] ? T.492+0x24/0x26
  [<ffffffff8103c101>] warn_slowpath_null+0x15/0x17
  [<ffffffff81068667>] lockdep_trace_alloc+0x95/0xc2
  [<ffffffff810ed9ac>] slab_pre_alloc_hook+0x18/0x3b
  [<ffffffff810ef227>] kmem_cache_alloc_trace+0x25/0xba
  [<ffffffff812387e6>] T.492+0x24/0x26
  [<ffffffff81238816>] pci_get_subsys+0x2e/0x73
  [<ffffffff8123886c>] pci_get_device+0x11/0x13
  [<ffffffff814082a9>] usb_amd_find_chipset_info+0x3f/0x18a

...

It turns out that this function calls pci_get_device under a spin_lock with irqs disabled, but the pci_get_device function is only allowed in preemptible context.

Quoted 5 lines

This patch fixes the warning by making all data-structure modifications on temporal storage and commiting this back into the visible structure at the end. While at it, this patch also moves the pci_dev_put calls out of the spinlocks because this function might sleep too.

I see only a couple of small flaws...

Quoted 7 lines
drivers/usb/host/pci-quirks.c +0 -0
index 1d586d4..dfc639a 100644
@@ -84,65 +84,91 @@ int usb_amd_find_chipset_info(void)
 {
 	u8 rev = 0;

drivers/usb/host/pci-quirks.c int usb_amd_find_chipset_info(void) · line 93

Quoted 10 lines
drivers/usb/host/pci-quirks.c +3 -2
 	unsigned long flags;
+	struct amd_chipset_info info;
+	int ret;
 
 	spin_lock_irqsave(&amd_lock, flags);
 
-	amd_chipset.probe_count++;
 	/* probe only once */
-	if (amd_chipset.probe_count > 1) {
+	if (amd_chipset.probe_count > 0) {

You need to increment probe_count here.

drivers/usb/host/pci-quirks.c int usb_amd_find_chipset_info(void) · line 97

Quoted 4 lines
drivers/usb/host/pci-quirks.c +1 -0
 		spin_unlock_irqrestore(&amd_lock, flags);
 		return amd_chipset.probe_result;
 	}
+	info = amd_chipset;

What's the point of this line? You're just going to write over all the data in info anyway, so it doesn't matter what amd_chipset contains. A memset would work just as well.

drivers/usb/host/pci-quirks.c EXPORT_SYMBOL_GPL(usb_amd_quirk_pll_enable); · line 316

Quoted 8 lines
drivers/usb/host/pci-quirks.c +2 -0
@@ -284,8 +310,10 @@ EXPORT_SYMBOL_GPL(usb_amd_quirk_pll_enable);
 
 void usb_amd_dev_put(void)
 {
+	struct pci_dev *nb, *smbus;
 	unsigned long flags;
 
+

Why add an extra blank line?

Quoted 15 lines
drivers/usb/host/pci-quirks.c +0 -8
 	spin_lock_irqsave(&amd_lock, flags);
 
 	amd_chipset.probe_count--;
@@ -294,20 +322,23 @@ void usb_amd_dev_put(void)
 		return;
 	}
 
-	if (amd_chipset.nb_dev) {
-		pci_dev_put(amd_chipset.nb_dev);
-		amd_chipset.nb_dev = NULL;
-	}
-	if (amd_chipset.smbus_dev) {
-		pci_dev_put(amd_chipset.smbus_dev);
-		amd_chipset.smbus_dev = NULL;
-	}

drivers/usb/host/pci-quirks.c void usb_amd_dev_put(void) · line 334

Quoted 10 lines
drivers/usb/host/pci-quirks.c +6 -0
+	/* save them to pci_dev_put outside of spinlock */
+	nb    = amd_chipset.nb_dev;
+	smbus = amd_chipset.smbus_dev;
+
+	amd_chipset.nb_dev = NULL;
+	amd_chipset.smbus_dev = NULL;
 	amd_chipset.nb_type = 0;
 	amd_chipset.sb_type = 0;
 	amd_chipset.isoc_reqs = 0;
 	amd_chipset.probe_result = 0;

You could use memset instead. However, in reality it shouldn't be necessary to set any of these things to 0.

drivers/usb/host/pci-quirks.c void usb_amd_dev_put(void) · line 343

Quoted 9 lines
drivers/usb/host/pci-quirks.c +5 -0
 	spin_unlock_irqrestore(&amd_lock, flags);
+
+	if (nb)
+		pci_dev_put(nb);
+	if (smbus)
+		pci_dev_put(amd_chipset.smbus_dev);
 }
 EXPORT_SYMBOL_GPL(usb_amd_dev_put);

Alan Stern

#11 lore raw
Quoted 7 lines · Alan Stern

drivers/usb/host/pci-quirks.c int usb_amd_find_chipset_info(void) · line 93

Quoted 4 lines · Joerg Roedel
drivers/usb/host/pci-quirks.c +1 -2
-	amd_chipset.probe_count++;
 	/* probe only once */
-	if (amd_chipset.probe_count > 1) {
+	if (amd_chipset.probe_count > 0) {

You need to increment probe_count here.

Argh, right.

Quoted 9 lines

drivers/usb/host/pci-quirks.c int usb_amd_find_chipset_info(void) · line 97

Quoted 5 lines
drivers/usb/host/pci-quirks.c +1 -0
 		spin_unlock_irqrestore(&amd_lock, flags);
 		return amd_chipset.probe_result;
 	}
+	info = amd_chipset;

What's the point of this line? You're just going to write over all the data in info anyway, so it doesn't matter what amd_chipset contains. A memset would work just as well.

info is on-stack and needs to be initialized somehow because the following code does not touch the whole data structure.

Quoted 11 lines

drivers/usb/host/pci-quirks.c EXPORT_SYMBOL_GPL(usb_amd_quirk_pll_enable); · line 316

Quoted 9 lines
drivers/usb/host/pci-quirks.c +2 -0
@@ -284,8 +310,10 @@ EXPORT_SYMBOL_GPL(usb_amd_quirk_pll_enable);
 
 void usb_amd_dev_put(void)
 {
+	struct pci_dev *nb, *smbus;
 	unsigned long flags;
 
+

Why add an extra blank line?

Left-over from editing.

Quoted 21 lines
Quoted 21 lines
drivers/usb/host/pci-quirks.c +5 -8
 	spin_lock_irqsave(&amd_lock, flags);
 
 	amd_chipset.probe_count--;
@@ -294,20 +322,23 @@ void usb_amd_dev_put(void)
 		return;
 	}
 
-	if (amd_chipset.nb_dev) {
-		pci_dev_put(amd_chipset.nb_dev);
-		amd_chipset.nb_dev = NULL;
-	}
-	if (amd_chipset.smbus_dev) {
-		pci_dev_put(amd_chipset.smbus_dev);
-		amd_chipset.smbus_dev = NULL;
-	}
+	/* save them to pci_dev_put outside of spinlock */
+	nb    = amd_chipset.nb_dev;
+	smbus = amd_chipset.smbus_dev;
+
+	amd_chipset.nb_dev = NULL;
Quoted 8 lines

drivers/usb/host/pci-quirks.c void usb_amd_dev_put(void) · line 334

Quoted 5 lines
drivers/usb/host/pci-quirks.c +1 -0
+	amd_chipset.smbus_dev = NULL;
 	amd_chipset.nb_type = 0;
 	amd_chipset.sb_type = 0;
 	amd_chipset.isoc_reqs = 0;
 	amd_chipset.probe_result = 0;

You could use memset instead. However, in reality it shouldn't be necessary to set any of these things to 0.

Well, I left this code as it was before because this is meant as a fix and I tried to keep it small. For a fix it is already really big... But that can certainly be changed in a follow-on patch.

Joerg

Signature · 5 lines
AMD Operating System Research Center

Advanced Micro Devices GmbH Einsteinring 24 85609 Dornach
General Managers: Alberto Bozzo, Andrew Bowd
Registration: Dornach, Landkr. Muenchen; Registerger. Muenchen, HRB Nr. 43632
#12 lore raw
Quoted 8 lines · Joerg
Quoted 5 lines

drivers/usb/host/pci-quirks.c int usb_amd_find_chipset_info(void) · line 97

Quoted 1 line
drivers/usb/host/pci-quirks.c +1 -0
+	info = amd_chipset;

What's the point of this line? You're just going to write over all the data in info anyway, so it doesn't matter what amd_chipset contains. A memset would work just as well.

info is on-stack and needs to be initialized somehow because the following code does not touch the whole data structure.

Then just use memset.

Alan Stern

#17 lore raw
In reply to Joerg Roedel
Quoted 1 line · Joerg Roedel

Booting 2.6.38 on my test machine produces a lockdep warning

You mean 2.6.39-rc2 right? I'm asking because I hit the same warning with 39-rc2 now too but it didn't appear with .38 - so it has to have snuck in after the merge window. If so, you don't need the stable tag.

Also, is this the final version wrt code design so that I can test too?

Thanks.

Quoted 170 lines

from the usb_amd_find_chipset_info() function:

 WARNING: at /data/lemmy/linux.trees.git/kernel/lockdep.c:2465 lockdep_trace_alloc+0x95/0xc2()
 Hardware name: Snook
 Modules linked in:
 Pid: 959, comm: work_for_cpu Not tainted 2.6.39-rc2+ #22
 Call Trace:
  [<ffffffff8103c0d4>] warn_slowpath_common+0x80/0x98
  [<ffffffff812387e6>] ? T.492+0x24/0x26
  [<ffffffff8103c101>] warn_slowpath_null+0x15/0x17
  [<ffffffff81068667>] lockdep_trace_alloc+0x95/0xc2
  [<ffffffff810ed9ac>] slab_pre_alloc_hook+0x18/0x3b
  [<ffffffff810ef227>] kmem_cache_alloc_trace+0x25/0xba
  [<ffffffff812387e6>] T.492+0x24/0x26
  [<ffffffff81238816>] pci_get_subsys+0x2e/0x73
 sr0: scsi3-mmc drive: 48x/48x writer dvd-ram cd/rw xa/form2 cdda tray
  [<ffffffff8123886c>] pci_get_device+0x11/0x13
  [<ffffffff814082a9>] usb_amd_find_chipset_info+0x3f/0x18a

...

It turns out that this function calls pci_get_device under a spin_lock with irqs disabled, but the pci_get_device function is only allowed in preemptible context.

This patch fixes this by using temporary variables in the quirk algorithm and commiting them later to the struct under the lock. This moves all pci_get_device() invocations out of the spin_lock and fixes the lockdep warning.

---
 drivers/usb/host/pci-quirks.c |   90 +++++++++++++++++++++++++---------------
 1 files changed, 56 insertions(+), 34 deletions(-)
drivers/usb/host/pci-quirks.c +56 -34
index 1d586d4..c21b073 100644
@@ -84,65 +84,87 @@ int usb_amd_find_chipset_info(void)
 {
 	u8 rev = 0;
 	unsigned long flags;
+	struct amd_chipset_info info;
+	int ret;
 
 	spin_lock_irqsave(&amd_lock, flags);
 
-	amd_chipset.probe_count++;
 	/* probe only once */
-	if (amd_chipset.probe_count > 1) {
+	if (amd_chipset.probe_count > 0) {
 		spin_unlock_irqrestore(&amd_lock, flags);
 		return amd_chipset.probe_result;
 	}
+	info = amd_chipset;
+	spin_unlock_irqrestore(&amd_lock, flags);
 
-	amd_chipset.smbus_dev = pci_get_device(PCI_VENDOR_ID_ATI, 0x4385, NULL);
-	if (amd_chipset.smbus_dev) {
-		rev = amd_chipset.smbus_dev->revision;
+	info.smbus_dev = pci_get_device(PCI_VENDOR_ID_ATI, 0x4385, NULL);
+	if (info.smbus_dev) {
+		rev = info.smbus_dev->revision;
 		if (rev >= 0x40)
-			amd_chipset.sb_type = 1;
+			info.sb_type = 1;
 		else if (rev >= 0x30 && rev <= 0x3b)
-			amd_chipset.sb_type = 3;
+			info.sb_type = 3;
 	} else {
-		amd_chipset.smbus_dev = pci_get_device(PCI_VENDOR_ID_AMD,
-							0x780b, NULL);
-		if (!amd_chipset.smbus_dev) {
-			spin_unlock_irqrestore(&amd_lock, flags);
-			return 0;
+		info.smbus_dev = pci_get_device(PCI_VENDOR_ID_AMD,
+						0x780b, NULL);
+		if (!info.smbus_dev) {
+			ret = 0;
+			goto commit;
 		}
-		rev = amd_chipset.smbus_dev->revision;
+
+		rev = info.smbus_dev->revision;
 		if (rev >= 0x11 && rev <= 0x18)
-			amd_chipset.sb_type = 2;
+			info.sb_type = 2;
 	}
 
-	if (amd_chipset.sb_type == 0) {
-		if (amd_chipset.smbus_dev) {
-			pci_dev_put(amd_chipset.smbus_dev);
-			amd_chipset.smbus_dev = NULL;
+	if (info.sb_type == 0) {
+		if (info.smbus_dev) {
+			pci_dev_put(info.smbus_dev);
+			info.smbus_dev = NULL;
 		}
-		spin_unlock_irqrestore(&amd_lock, flags);
-		return 0;
+		ret = 0;
+		goto commit;
 	}
 
-	amd_chipset.nb_dev = pci_get_device(PCI_VENDOR_ID_AMD, 0x9601, NULL);
-	if (amd_chipset.nb_dev) {
-		amd_chipset.nb_type = 1;
+	info.nb_dev = pci_get_device(PCI_VENDOR_ID_AMD, 0x9601, NULL);
+	if (info.nb_dev) {
+		info.nb_type = 1;
 	} else {
-		amd_chipset.nb_dev = pci_get_device(PCI_VENDOR_ID_AMD,
-							0x1510, NULL);
-		if (amd_chipset.nb_dev) {
-			amd_chipset.nb_type = 2;
-		} else  {
-			amd_chipset.nb_dev = pci_get_device(PCI_VENDOR_ID_AMD,
-								0x9600, NULL);
-			if (amd_chipset.nb_dev)
-				amd_chipset.nb_type = 3;
+		info.nb_dev = pci_get_device(PCI_VENDOR_ID_AMD, 0x1510, NULL);
+		if (info.nb_dev) {
+			info.nb_type = 2;
+		} else {
+			info.nb_dev = pci_get_device(PCI_VENDOR_ID_AMD,
+						     0x9600, NULL);
+			if (info.nb_dev)
+				info.nb_type = 3;
 		}
 	}
 
-	amd_chipset.probe_result = 1;
+	ret = info.probe_result = 1;
 	printk(KERN_DEBUG "QUIRK: Enable AMD PLL fix\n");
 
+commit:
+
+	spin_lock_irqsave(&amd_lock, flags);
+	if (amd_chipset.probe_count > 0) {
+		/* race - someone else was faster - drop devices */
+		if (info.nb_dev)
+			pci_dev_put(info.nb_dev);
+		if (info.smbus_dev)
+			pci_dev_put(info.smbus_dev);
+
+		/* Mark that we where here */
+		amd_chipset.probe_count++;
+		ret = amd_chipset.probe_result;
+	} else {
+		/* no race - commit the result */
+		info.probe_count++;
+		amd_chipset = info;
+	}
 	spin_unlock_irqrestore(&amd_lock, flags);
-	return amd_chipset.probe_result;
+
+	return ret;
 }
 EXPORT_SYMBOL_GPL(usb_amd_find_chipset_info);
 
Signature · 8 lines
1.7.1


--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/
Signature · 2 lines
Regards/Gruss,
    Boris.
#18 lore raw
Quoted 8 lines · Borislav Petkov
Quoted 1 line · Joerg Roedel

Booting 2.6.38 on my test machine produces a lockdep warning

You mean 2.6.39-rc2 right? I'm asking because I hit the same warning with 39-rc2 now too but it didn't appear with .38 - so it has to have snuck in after the merge window. If so, you don't need the stable tag.

Also, is this the final version wrt code design so that I can test too?

I hit this in the KVM branch and git-describe told me it is 2.6.38 based. So I guess this happens on 2.6.38 too, but I havn't tested plain 2.6.38. I do this and sent out a new version shortly which you can use then. The new version addresses Alan Stern's latest comments.

Joerg

#19 lore raw

PATCH v5 1/1 USB host: Fix lockdep warning in AMD PLL quirk

In reply to Borislav Petkov
Quoted 3 lines · Borislav Petkov

You mean 2.6.39-rc2 right? I'm asking because I hit the same warning with 39-rc2 now too but it didn't appear with .38 - so it has to have snuck in after the merge window. If so, you don't need the stable tag.

You were right, just tested plain 2.6.38 and it doesn't happen. So I removed the stable tag. Here is the updated patch.

Quoted 1 line

From 6b9a9018cf0b847dcd906c269d663668e67a097d Mon Sep 17 00:00:00 2001

From: Joerg Roedel <[email protected]>
Date: Wed, 6 Apr 2011 13:07:53 +0200
Subject: [PATCH] USB host: Fix lockdep warning in AMD PLL quirk

Booting 2.6.38 on my test machine produces a lockdep warning from the usb_amd_find_chipset_info() function:

 WARNING: at /data/lemmy/linux.trees.git/kernel/lockdep.c:2465 lockdep_trace_alloc+0x95/0xc2()
 Hardware name: Snook
 Modules linked in:
 Pid: 959, comm: work_for_cpu Not tainted 2.6.39-rc2+ #22
 Call Trace:
  [<ffffffff8103c0d4>] warn_slowpath_common+0x80/0x98
  [<ffffffff812387e6>] ? T.492+0x24/0x26
  [<ffffffff8103c101>] warn_slowpath_null+0x15/0x17
  [<ffffffff81068667>] lockdep_trace_alloc+0x95/0xc2
  [<ffffffff810ed9ac>] slab_pre_alloc_hook+0x18/0x3b
  [<ffffffff810ef227>] kmem_cache_alloc_trace+0x25/0xba
  [<ffffffff812387e6>] T.492+0x24/0x26
  [<ffffffff81238816>] pci_get_subsys+0x2e/0x73
  [<ffffffff8123886c>] pci_get_device+0x11/0x13
  [<ffffffff814082a9>] usb_amd_find_chipset_info+0x3f/0x18a

...

It turns out that this function calls pci_get_device under a spin_lock with irqs disabled, but the pci_get_device function is only allowed in preemptible context.

This patch fixes the warning by making all data-structure modifications on temporal storage and commiting this back into the visible structure at the end. While at it, this patch also moves the pci_dev_put calls out of the spinlocks because this function might sleep too.

---
 drivers/usb/host/pci-quirks.c |  117 ++++++++++++++++++++++++++---------------
 1 files changed, 74 insertions(+), 43 deletions(-)
drivers/usb/host/pci-quirks.c +74 -43
index 1d586d4..c6eb69c 100644
@@ -84,65 +84,92 @@ int usb_amd_find_chipset_info(void)
 {
 	u8 rev = 0;
 	unsigned long flags;
+	struct amd_chipset_info info;
+	int ret;
 
 	spin_lock_irqsave(&amd_lock, flags);
 
-	amd_chipset.probe_count++;
 	/* probe only once */
-	if (amd_chipset.probe_count > 1) {
+	if (amd_chipset.probe_count > 0) {
+		amd_chipset.probe_count++;
 		spin_unlock_irqrestore(&amd_lock, flags);
 		return amd_chipset.probe_result;
 	}
+	memset(&info, 0, sizeof(info));
+	spin_unlock_irqrestore(&amd_lock, flags);
 
-	amd_chipset.smbus_dev = pci_get_device(PCI_VENDOR_ID_ATI, 0x4385, NULL);
-	if (amd_chipset.smbus_dev) {
-		rev = amd_chipset.smbus_dev->revision;
+	info.smbus_dev = pci_get_device(PCI_VENDOR_ID_ATI, 0x4385, NULL);
+	if (info.smbus_dev) {
+		rev = info.smbus_dev->revision;
 		if (rev >= 0x40)
-			amd_chipset.sb_type = 1;
+			info.sb_type = 1;
 		else if (rev >= 0x30 && rev <= 0x3b)
-			amd_chipset.sb_type = 3;
+			info.sb_type = 3;
 	} else {
-		amd_chipset.smbus_dev = pci_get_device(PCI_VENDOR_ID_AMD,
-							0x780b, NULL);
-		if (!amd_chipset.smbus_dev) {
-			spin_unlock_irqrestore(&amd_lock, flags);
-			return 0;
+		info.smbus_dev = pci_get_device(PCI_VENDOR_ID_AMD,
+						0x780b, NULL);
+		if (!info.smbus_dev) {
+			ret = 0;
+			goto commit;
 		}
-		rev = amd_chipset.smbus_dev->revision;
+
+		rev = info.smbus_dev->revision;
 		if (rev >= 0x11 && rev <= 0x18)
-			amd_chipset.sb_type = 2;
+			info.sb_type = 2;
 	}
 
-	if (amd_chipset.sb_type == 0) {
-		if (amd_chipset.smbus_dev) {
-			pci_dev_put(amd_chipset.smbus_dev);
-			amd_chipset.smbus_dev = NULL;
+	if (info.sb_type == 0) {
+		if (info.smbus_dev) {
+			pci_dev_put(info.smbus_dev);
+			info.smbus_dev = NULL;
 		}
-		spin_unlock_irqrestore(&amd_lock, flags);
-		return 0;
+		ret = 0;
+		goto commit;
 	}
 
-	amd_chipset.nb_dev = pci_get_device(PCI_VENDOR_ID_AMD, 0x9601, NULL);
-	if (amd_chipset.nb_dev) {
-		amd_chipset.nb_type = 1;
+	info.nb_dev = pci_get_device(PCI_VENDOR_ID_AMD, 0x9601, NULL);
+	if (info.nb_dev) {
+		info.nb_type = 1;
 	} else {
-		amd_chipset.nb_dev = pci_get_device(PCI_VENDOR_ID_AMD,
-							0x1510, NULL);
-		if (amd_chipset.nb_dev) {
-			amd_chipset.nb_type = 2;
-		} else  {
-			amd_chipset.nb_dev = pci_get_device(PCI_VENDOR_ID_AMD,
-								0x9600, NULL);
-			if (amd_chipset.nb_dev)
-				amd_chipset.nb_type = 3;
+		info.nb_dev = pci_get_device(PCI_VENDOR_ID_AMD, 0x1510, NULL);
+		if (info.nb_dev) {
+			info.nb_type = 2;
+		} else {
+			info.nb_dev = pci_get_device(PCI_VENDOR_ID_AMD,
+						     0x9600, NULL);
+			if (info.nb_dev)
+				info.nb_type = 3;
 		}
 	}
 
-	amd_chipset.probe_result = 1;
+	ret = info.probe_result = 1;
 	printk(KERN_DEBUG "QUIRK: Enable AMD PLL fix\n");
 
-	spin_unlock_irqrestore(&amd_lock, flags);
-	return amd_chipset.probe_result;
+commit:
+
+	spin_lock_irqsave(&amd_lock, flags);
+	if (amd_chipset.probe_count > 0) {
+		/* race - someone else was faster - drop devices */
+
+		/* Mark that we where here */
+		amd_chipset.probe_count++;
+		ret = amd_chipset.probe_result;
+
+		spin_unlock_irqrestore(&amd_lock, flags);
+
+		if (info.nb_dev)
+			pci_dev_put(info.nb_dev);
+		if (info.smbus_dev)
+			pci_dev_put(info.smbus_dev);
+
+	} else {
+		/* no race - commit the result */
+		info.probe_count++;
+		amd_chipset = info;
+		spin_unlock_irqrestore(&amd_lock, flags);
+	}
+
+	return ret;
 }
 EXPORT_SYMBOL_GPL(usb_amd_find_chipset_info);
 
@@ -284,6 +311,7 @@ EXPORT_SYMBOL_GPL(usb_amd_quirk_pll_enable);
 
 void usb_amd_dev_put(void)
 {
+	struct pci_dev *nb, *smbus;
 	unsigned long flags;
 
 	spin_lock_irqsave(&amd_lock, flags);
@@ -294,20 +322,23 @@ void usb_amd_dev_put(void)
 		return;
 	}
 
-	if (amd_chipset.nb_dev) {
-		pci_dev_put(amd_chipset.nb_dev);
-		amd_chipset.nb_dev = NULL;
-	}
-	if (amd_chipset.smbus_dev) {
-		pci_dev_put(amd_chipset.smbus_dev);
-		amd_chipset.smbus_dev = NULL;
-	}
+	/* save them to pci_dev_put outside of spinlock */
+	nb    = amd_chipset.nb_dev;
+	smbus = amd_chipset.smbus_dev;
+
+	amd_chipset.nb_dev = NULL;
+	amd_chipset.smbus_dev = NULL;
 	amd_chipset.nb_type = 0;
 	amd_chipset.sb_type = 0;
 	amd_chipset.isoc_reqs = 0;
 	amd_chipset.probe_result = 0;
 
 	spin_unlock_irqrestore(&amd_lock, flags);
+
+	if (nb)
+		pci_dev_put(nb);
+	if (smbus)
+		pci_dev_put(amd_chipset.smbus_dev);
 }
 EXPORT_SYMBOL_GPL(usb_amd_dev_put);
 
Signature · 9 lines
1.7.1


-- 
AMD Operating System Research Center

Advanced Micro Devices GmbH Einsteinring 24 85609 Dornach
General Managers: Alberto Bozzo, Andrew Bowd
Registration: Dornach, Landkr. Muenchen; Registerger. Muenchen, HRB Nr. 43632
#20 lore raw

Hello.

Quoted 7 lines · Joerg
Quoted 3 lines

You mean 2.6.39-rc2 right? I'm asking because I hit the same warning with 39-rc2 now too but it didn't appear with .38 - so it has to have snuck in after the merge window. If so, you don't need the stable tag.

You were right, just tested plain 2.6.38 and it doesn't happen. So I removed the stable tag. Here is the updated patch.

Quoted 6 lines

From 6b9a9018cf0b847dcd906c269d663668e67a097d Mon Sep 17 00:00:00 2001
From: Joerg Roedel<[email protected]>
Date: Wed, 6 Apr 2011 13:07:53 +0200
Subject: [PATCH] USB host: Fix lockdep warning in AMD PLL quirk

Booting 2.6.38 on my test machine produces a lockdep warning

You've just said that this doesn't happen with plain 2.6.38...

Quoted 38 lines

from the usb_amd_find_chipset_info() function:

  WARNING: at /data/lemmy/linux.trees.git/kernel/lockdep.c:2465 lockdep_trace_alloc+0x95/0xc2()
  Hardware name: Snook
  Modules linked in:
  Pid: 959, comm: work_for_cpu Not tainted 2.6.39-rc2+ #22
  Call Trace:
   [<ffffffff8103c0d4>] warn_slowpath_common+0x80/0x98
   [<ffffffff812387e6>] ? T.492+0x24/0x26
   [<ffffffff8103c101>] warn_slowpath_null+0x15/0x17
   [<ffffffff81068667>] lockdep_trace_alloc+0x95/0xc2
   [<ffffffff810ed9ac>] slab_pre_alloc_hook+0x18/0x3b
   [<ffffffff810ef227>] kmem_cache_alloc_trace+0x25/0xba
   [<ffffffff812387e6>] T.492+0x24/0x26
   [<ffffffff81238816>] pci_get_subsys+0x2e/0x73
   [<ffffffff8123886c>] pci_get_device+0x11/0x13
   [<ffffffff814082a9>] usb_amd_find_chipset_info+0x3f/0x18a

...

It turns out that this function calls pci_get_device under a spin_lock with irqs disabled, but the pci_get_device function is only allowed in preemptible context.

This patch fixes the warning by making all data-structure modifications on temporal storage and commiting this back into the visible structure at the end. While at it, this patch also moves the pci_dev_put calls out of the spinlocks because this function might sleep too.

---
  drivers/usb/host/pci-quirks.c |  117 ++++++++++++++++++++++++++---------------
  1 files changed, 74 insertions(+), 43 deletions(-)
drivers/usb/host/pci-quirks.c +0 -0
index 1d586d4..c6eb69c 100644

[...]

Quoted 19 lines
drivers/usb/host/pci-quirks.c +6 -8
@@ -294,20 +322,23 @@ void usb_amd_dev_put(void)
  		return;
  	}
 
-	if (amd_chipset.nb_dev) {
-		pci_dev_put(amd_chipset.nb_dev);
-		amd_chipset.nb_dev = NULL;
-	}
-	if (amd_chipset.smbus_dev) {
-		pci_dev_put(amd_chipset.smbus_dev);
-		amd_chipset.smbus_dev = NULL;
-	}
+	/* save them to pci_dev_put outside of spinlock */
+	nb    = amd_chipset.nb_dev;
+	smbus = amd_chipset.smbus_dev;
+
+	amd_chipset.nb_dev = NULL;
+	amd_chipset.smbus_dev = NULL;
  	amd_chipset.nb_type = 0;

drivers/usb/host/pci-quirks.c void usb_amd_dev_put(void) · line 341

Quoted 10 lines
drivers/usb/host/pci-quirks.c +5 -0
  	amd_chipset.sb_type = 0;
  	amd_chipset.isoc_reqs = 0;
  	amd_chipset.probe_result = 0;
 
  	spin_unlock_irqrestore(&amd_lock, flags);
+
+	if (nb)
+		pci_dev_put(nb);
+	if (smbus)
+		pci_dev_put(amd_chipset.smbus_dev);

Haven't you just set this to NULL?

WBR, Sergei

#21 lore raw
In reply to Roedel, Joerg
Quoted 7 lines · Joerg
Quoted 3 lines · Borislav Petkov

You mean 2.6.39-rc2 right? I'm asking because I hit the same warning with 39-rc2 now too but it didn't appear with .38 - so it has to have snuck in after the merge window. If so, you don't need the stable tag.

You were right, just tested plain 2.6.38 and it doesn't happen. So I removed the stable tag. Here is the updated patch.

...

Quoted 4 lines
drivers/usb/host/pci-quirks.c +0 -0
@@ -84,65 +84,92 @@ int usb_amd_find_chipset_info(void)
 {

...

drivers/usb/host/pci-quirks.c int usb_amd_find_chipset_info(void) · line 155

Quoted 8 lines
drivers/usb/host/pci-quirks.c +8 -0
+commit:
+
+	spin_lock_irqsave(&amd_lock, flags);
+	if (amd_chipset.probe_count > 0) {
+		/* race - someone else was faster - drop devices */
+
+		/* Mark that we where here */
+		amd_chipset.probe_count++;

This line should be moved above the "if" statement, since you always want to increment the count.

drivers/usb/host/pci-quirks.c int usb_amd_find_chipset_info(void) · line 167

Quoted 12 lines
drivers/usb/host/pci-quirks.c +12 -0
+		ret = amd_chipset.probe_result;
+
+		spin_unlock_irqrestore(&amd_lock, flags);
+
+		if (info.nb_dev)
+			pci_dev_put(info.nb_dev);
+		if (info.smbus_dev)
+			pci_dev_put(info.smbus_dev);
+
+	} else {
+		/* no race - commit the result */
+		info.probe_count++;

This isn't right, because info.probe_count was initialized to 0. Maybe amd_chipset.probe_count should be made into a separate variable, not a part of the structure, like amd_lock.

drivers/usb/host/pci-quirks.c int usb_amd_find_chipset_info(void) · line 174

Quoted 7 lines
drivers/usb/host/pci-quirks.c +5 -0
+		amd_chipset = info;
+		spin_unlock_irqrestore(&amd_lock, flags);
+	}
+
+	return ret;
 }
 EXPORT_SYMBOL_GPL(usb_amd_find_chipset_info);

Alan Stern

#22 lore raw
Quoted 19 lines · Alan Stern
Quoted 7 lines · Joerg
Quoted 3 lines · Borislav Petkov

You mean 2.6.39-rc2 right? I'm asking because I hit the same warning with 39-rc2 now too but it didn't appear with .38 - so it has to have snuck in after the merge window. If so, you don't need the stable tag.

You were right, just tested plain 2.6.38 and it doesn't happen. So I removed the stable tag. Here is the updated patch.

...

Quoted 4 lines
drivers/usb/host/pci-quirks.c +0 -0
@@ -84,65 +84,92 @@ int usb_amd_find_chipset_info(void)
 {

...

Quoted 11 lines

drivers/usb/host/pci-quirks.c int usb_amd_find_chipset_info(void) · line 155

Quoted 8 lines
drivers/usb/host/pci-quirks.c +8 -0
+commit:
+
+	spin_lock_irqsave(&amd_lock, flags);
+	if (amd_chipset.probe_count > 0) {
+		/* race - someone else was faster - drop devices */
+
+		/* Mark that we where here */
+		amd_chipset.probe_count++;

This line should be moved above the "if" statement, since you always want to increment the count.

No, probe_count can't be incremented here because the probe is not finished yet. If another thread jumps in after the lock is released and detects probe_count > 0 while the probe hasn't happened the quirk will fail. So we need to make sure that amd_chipset.probe_count does not become > 0 before the probe is finished.

Quoted 17 lines

drivers/usb/host/pci-quirks.c int usb_amd_find_chipset_info(void) · line 167

Quoted 13 lines
drivers/usb/host/pci-quirks.c +12 -0
+		ret = amd_chipset.probe_result;
+
+		spin_unlock_irqrestore(&amd_lock, flags);
+
+		if (info.nb_dev)
+			pci_dev_put(info.nb_dev);
+		if (info.smbus_dev)
+			pci_dev_put(info.smbus_dev);
+
+	} else {
+		/* no race - commit the result */
+		info.probe_count++;

This isn't right, because info.probe_count was initialized to 0. Maybe amd_chipset.probe_count should be made into a separate variable, not a part of the structure, like amd_lock.

The purpose of the struct is structuring of data. In theory all of its members could be turned into global variables. The amd_lock is different because it does not only protect the struct but also access to the hardware while the quirk is applied/unapplied.

Joerg

Signature · 5 lines
AMD Operating System Research Center

Advanced Micro Devices GmbH Einsteinring 24 85609 Dornach
General Managers: Alberto Bozzo, Andrew Bowd
Registration: Dornach, Landkr. Muenchen; Registerger. Muenchen, HRB Nr. 43632
#23 lore raw
Quoted 12 lines · Joerg
Quoted 11 lines

drivers/usb/host/pci-quirks.c int usb_amd_find_chipset_info(void) · line 155

Quoted 8 lines
drivers/usb/host/pci-quirks.c +8 -0
+commit:
+
+	spin_lock_irqsave(&amd_lock, flags);
+	if (amd_chipset.probe_count > 0) {
+		/* race - someone else was faster - drop devices */
+
+		/* Mark that we where here */
+		amd_chipset.probe_count++;

This line should be moved above the "if" statement, since you always want to increment the count.

Quoted 2 lines

No, probe_count can't be incremented here because the probe is not finished yet.

I don't follow you. Sure it is finished; this is the "commit" part of the probe.

Quoted 4 lines

If another thread jumps in after the lock is released and
detects probe_count > 0 while the probe hasn't happened the quirk will fail. So we need to make sure that amd_chipset.probe_count does not become > 0 before the probe is finished.

I meant the increment should be done before the "if" statement but after the spin_lock_irqsave(). That way nobody else can jump in at the wrong time.

Quoted 17 lines
Quoted 8 lines
Quoted 8 lines
drivers/usb/host/pci-quirks.c +8 -0
+		ret = amd_chipset.probe_result;
+
+		spin_unlock_irqrestore(&amd_lock, flags);
+
+		if (info.nb_dev)
+			pci_dev_put(info.nb_dev);
+		if (info.smbus_dev)
+			pci_dev_put(info.smbus_dev);
Quoted 8 lines

drivers/usb/host/pci-quirks.c int usb_amd_find_chipset_info(void) · line 167

Quoted 4 lines
drivers/usb/host/pci-quirks.c +4 -0
+
+	} else {
+		/* no race - commit the result */
+		info.probe_count++;

This isn't right, because info.probe_count was initialized to 0. Maybe amd_chipset.probe_count should be made into a separate variable, not a part of the structure, like amd_lock.

Quoted 4 lines

The purpose of the struct is structuring of data. In theory all of its members could be turned into global variables. The amd_lock is different because it does not only protect the struct but also access to the hardware while the quirk is applied/unapplied.

Do it however you prefer. But as it stands now, the patch is wrong.

Alan Stern

#24 lore raw
Quoted 14 lines · Alan Stern
Quoted 12 lines · Joerg
Quoted 11 lines

drivers/usb/host/pci-quirks.c int usb_amd_find_chipset_info(void) · line 155

Quoted 8 lines
drivers/usb/host/pci-quirks.c +8 -0
+commit:
+
+	spin_lock_irqsave(&amd_lock, flags);
+	if (amd_chipset.probe_count > 0) {
+		/* race - someone else was faster - drop devices */
+
+		/* Mark that we where here */
+		amd_chipset.probe_count++;

This line should be moved above the "if" statement, since you always want to increment the count.

Quoted 5 lines
Quoted 2 lines

No, probe_count can't be incremented here because the probe is not finished yet.

I don't follow you. Sure it is finished; this is the "commit" part of the probe.

Nevermind, I thought you were refering to the spin-locked part at the beginning of the function.

Quoted 8 lines
Quoted 4 lines

If another thread jumps in after the lock is released and
detects probe_count > 0 while the probe hasn't happened the quirk will fail. So we need to make sure that amd_chipset.probe_count does not become > 0 before the probe is finished.

I meant the increment should be done before the "if" statement but after the spin_lock_irqsave(). That way nobody else can jump in at the wrong time.

In the real commit case the

	amd_chipset = info;

line will overwrite the increment if the probe is done before the if-statement. So incrementing amd_chipset.probe_count directly only matters for the case where we detected a race.

Quoted 17 lines
Quoted 17 lines
Quoted 16 lines

drivers/usb/host/pci-quirks.c int usb_amd_find_chipset_info(void) · line 167

Quoted 12 lines
drivers/usb/host/pci-quirks.c +12 -0
+		ret = amd_chipset.probe_result;
+
+		spin_unlock_irqrestore(&amd_lock, flags);
+
+		if (info.nb_dev)
+			pci_dev_put(info.nb_dev);
+		if (info.smbus_dev)
+			pci_dev_put(info.smbus_dev);
+
+	} else {
+		/* no race - commit the result */
+		info.probe_count++;

This isn't right, because info.probe_count was initialized to 0. Maybe amd_chipset.probe_count should be made into a separate variable, not a part of the structure, like amd_lock.

Quoted 6 lines
Quoted 4 lines

The purpose of the struct is structuring of data. In theory all of its members could be turned into global variables. The amd_lock is different because it does not only protect the struct but also access to the hardware while the quirk is applied/unapplied.

Do it however you prefer. But as it stands now, the patch is wrong.

Hmm, I see how it can be done differently, but no real bug.

Joerg

Signature · 5 lines
AMD Operating System Research Center

Advanced Micro Devices GmbH Einsteinring 24 85609 Dornach
General Managers: Alberto Bozzo, Andrew Bowd
Registration: Dornach, Landkr. Muenchen; Registerger. Muenchen, HRB Nr. 43632
#25 lore raw
Quoted 17 lines · Joerg
Quoted 17 lines
Quoted 17 lines
Quoted 16 lines

drivers/usb/host/pci-quirks.c int usb_amd_find_chipset_info(void) · line 167

Quoted 12 lines
drivers/usb/host/pci-quirks.c +12 -0
+		ret = amd_chipset.probe_result;
+
+		spin_unlock_irqrestore(&amd_lock, flags);
+
+		if (info.nb_dev)
+			pci_dev_put(info.nb_dev);
+		if (info.smbus_dev)
+			pci_dev_put(info.smbus_dev);
+
+	} else {
+		/* no race - commit the result */
+		info.probe_count++;

This isn't right, because info.probe_count was initialized to 0. Maybe amd_chipset.probe_count should be made into a separate variable, not a part of the structure, like amd_lock.

Quoted 8 lines
Quoted 6 lines
Quoted 4 lines

The purpose of the struct is structuring of data. In theory all of its members could be turned into global variables. The amd_lock is different because it does not only protect the struct but also access to the hardware while the quirk is applied/unapplied.

Do it however you prefer. But as it stands now, the patch is wrong.

Hmm, I see how it can be done differently, but no real bug.

Never mind, you're right. In the no-race case, the initial count is always going to be 0, and so incrementing info.probe_count is the right thing to do.

Alan Stern

#26 lore raw

PATCH v6 1/1 USB host: Fix lockdep warning in AMD PLL quirk

Quoted 7 lines · Alan Stern
Quoted 1 line · Joerg

Hmm, I see how it can be done differently, but no real bug.

Never mind, you're right. In the no-race case, the initial count is always going to be 0, and so incrementing info.probe_count is the right thing to do.

Okay, cool. So now that we have settled on this I removed the last occurence of '2.6.38' from the changelog and here is the result. No code changes since v5.

Quoted 1 line

From a6e00d237f20e940afdbcaafb27117fe8730466f Mon Sep 17 00:00:00 2001

From: Joerg Roedel <[email protected]>
Date: Wed, 6 Apr 2011 13:07:53 +0200
Subject: [PATCH] USB host: Fix lockdep warning in AMD PLL quirk

Booting latest kernel on my test machine produces a lockdep warning from the usb_amd_find_chipset_info() function:

 WARNING: at /data/lemmy/linux.trees.git/kernel/lockdep.c:2465 lockdep_trace_alloc+0x95/0xc2()
 Hardware name: Snook
 Modules linked in:
 Pid: 959, comm: work_for_cpu Not tainted 2.6.39-rc2+ #22
 Call Trace:
  [<ffffffff8103c0d4>] warn_slowpath_common+0x80/0x98
  [<ffffffff812387e6>] ? T.492+0x24/0x26
  [<ffffffff8103c101>] warn_slowpath_null+0x15/0x17
  [<ffffffff81068667>] lockdep_trace_alloc+0x95/0xc2
  [<ffffffff810ed9ac>] slab_pre_alloc_hook+0x18/0x3b
  [<ffffffff810ef227>] kmem_cache_alloc_trace+0x25/0xba
  [<ffffffff812387e6>] T.492+0x24/0x26
  [<ffffffff81238816>] pci_get_subsys+0x2e/0x73
  [<ffffffff8123886c>] pci_get_device+0x11/0x13
  [<ffffffff814082a9>] usb_amd_find_chipset_info+0x3f/0x18a

...

It turns out that this function calls pci_get_device under a spin_lock with irqs disabled, but the pci_get_device function is only allowed in preemptible context.

This patch fixes the warning by making all data-structure modifications on temporal storage and commiting this back into the visible structure at the end. While at it, this patch also moves the pci_dev_put calls out of the spinlocks because this function might sleep too.

---
 drivers/usb/host/pci-quirks.c |  117 ++++++++++++++++++++++++++---------------
 1 files changed, 74 insertions(+), 43 deletions(-)
drivers/usb/host/pci-quirks.c +74 -43
index 1d586d4..c6eb69c 100644
@@ -84,65 +84,92 @@ int usb_amd_find_chipset_info(void)
 {
 	u8 rev = 0;
 	unsigned long flags;
+	struct amd_chipset_info info;
+	int ret;
 
 	spin_lock_irqsave(&amd_lock, flags);
 
-	amd_chipset.probe_count++;
 	/* probe only once */
-	if (amd_chipset.probe_count > 1) {
+	if (amd_chipset.probe_count > 0) {
+		amd_chipset.probe_count++;
 		spin_unlock_irqrestore(&amd_lock, flags);
 		return amd_chipset.probe_result;
 	}
+	memset(&info, 0, sizeof(info));
+	spin_unlock_irqrestore(&amd_lock, flags);
 
-	amd_chipset.smbus_dev = pci_get_device(PCI_VENDOR_ID_ATI, 0x4385, NULL);
-	if (amd_chipset.smbus_dev) {
-		rev = amd_chipset.smbus_dev->revision;
+	info.smbus_dev = pci_get_device(PCI_VENDOR_ID_ATI, 0x4385, NULL);
+	if (info.smbus_dev) {
+		rev = info.smbus_dev->revision;
 		if (rev >= 0x40)
-			amd_chipset.sb_type = 1;
+			info.sb_type = 1;
 		else if (rev >= 0x30 && rev <= 0x3b)
-			amd_chipset.sb_type = 3;
+			info.sb_type = 3;
 	} else {
-		amd_chipset.smbus_dev = pci_get_device(PCI_VENDOR_ID_AMD,
-							0x780b, NULL);
-		if (!amd_chipset.smbus_dev) {
-			spin_unlock_irqrestore(&amd_lock, flags);
-			return 0;
+		info.smbus_dev = pci_get_device(PCI_VENDOR_ID_AMD,
+						0x780b, NULL);
+		if (!info.smbus_dev) {
+			ret = 0;
+			goto commit;
 		}
-		rev = amd_chipset.smbus_dev->revision;
+
+		rev = info.smbus_dev->revision;
 		if (rev >= 0x11 && rev <= 0x18)
-			amd_chipset.sb_type = 2;
+			info.sb_type = 2;
 	}
 
-	if (amd_chipset.sb_type == 0) {
-		if (amd_chipset.smbus_dev) {
-			pci_dev_put(amd_chipset.smbus_dev);
-			amd_chipset.smbus_dev = NULL;
+	if (info.sb_type == 0) {
+		if (info.smbus_dev) {
+			pci_dev_put(info.smbus_dev);
+			info.smbus_dev = NULL;
 		}
-		spin_unlock_irqrestore(&amd_lock, flags);
-		return 0;
+		ret = 0;
+		goto commit;
 	}
 
-	amd_chipset.nb_dev = pci_get_device(PCI_VENDOR_ID_AMD, 0x9601, NULL);
-	if (amd_chipset.nb_dev) {
-		amd_chipset.nb_type = 1;
+	info.nb_dev = pci_get_device(PCI_VENDOR_ID_AMD, 0x9601, NULL);
+	if (info.nb_dev) {
+		info.nb_type = 1;
 	} else {
-		amd_chipset.nb_dev = pci_get_device(PCI_VENDOR_ID_AMD,
-							0x1510, NULL);
-		if (amd_chipset.nb_dev) {
-			amd_chipset.nb_type = 2;
-		} else  {
-			amd_chipset.nb_dev = pci_get_device(PCI_VENDOR_ID_AMD,
-								0x9600, NULL);
-			if (amd_chipset.nb_dev)
-				amd_chipset.nb_type = 3;
+		info.nb_dev = pci_get_device(PCI_VENDOR_ID_AMD, 0x1510, NULL);
+		if (info.nb_dev) {
+			info.nb_type = 2;
+		} else {
+			info.nb_dev = pci_get_device(PCI_VENDOR_ID_AMD,
+						     0x9600, NULL);
+			if (info.nb_dev)
+				info.nb_type = 3;
 		}
 	}
 
-	amd_chipset.probe_result = 1;
+	ret = info.probe_result = 1;
 	printk(KERN_DEBUG "QUIRK: Enable AMD PLL fix\n");
 
-	spin_unlock_irqrestore(&amd_lock, flags);
-	return amd_chipset.probe_result;
+commit:
+
+	spin_lock_irqsave(&amd_lock, flags);
+	if (amd_chipset.probe_count > 0) {
+		/* race - someone else was faster - drop devices */
+
+		/* Mark that we where here */
+		amd_chipset.probe_count++;
+		ret = amd_chipset.probe_result;
+
+		spin_unlock_irqrestore(&amd_lock, flags);
+
+		if (info.nb_dev)
+			pci_dev_put(info.nb_dev);
+		if (info.smbus_dev)
+			pci_dev_put(info.smbus_dev);
+
+	} else {
+		/* no race - commit the result */
+		info.probe_count++;
+		amd_chipset = info;
+		spin_unlock_irqrestore(&amd_lock, flags);
+	}
+
+	return ret;
 }
 EXPORT_SYMBOL_GPL(usb_amd_find_chipset_info);
 
@@ -284,6 +311,7 @@ EXPORT_SYMBOL_GPL(usb_amd_quirk_pll_enable);
 
 void usb_amd_dev_put(void)
 {
+	struct pci_dev *nb, *smbus;
 	unsigned long flags;
 
 	spin_lock_irqsave(&amd_lock, flags);
@@ -294,20 +322,23 @@ void usb_amd_dev_put(void)
 		return;
 	}
 
-	if (amd_chipset.nb_dev) {
-		pci_dev_put(amd_chipset.nb_dev);
-		amd_chipset.nb_dev = NULL;
-	}
-	if (amd_chipset.smbus_dev) {
-		pci_dev_put(amd_chipset.smbus_dev);
-		amd_chipset.smbus_dev = NULL;
-	}
+	/* save them to pci_dev_put outside of spinlock */
+	nb    = amd_chipset.nb_dev;
+	smbus = amd_chipset.smbus_dev;
+
+	amd_chipset.nb_dev = NULL;
+	amd_chipset.smbus_dev = NULL;
 	amd_chipset.nb_type = 0;
 	amd_chipset.sb_type = 0;
 	amd_chipset.isoc_reqs = 0;
 	amd_chipset.probe_result = 0;
 
 	spin_unlock_irqrestore(&amd_lock, flags);
+
+	if (nb)
+		pci_dev_put(nb);
+	if (smbus)
+		pci_dev_put(amd_chipset.smbus_dev);
 }
 EXPORT_SYMBOL_GPL(usb_amd_dev_put);
 
Signature · 9 lines
1.7.1


-- 
AMD Operating System Research Center

Advanced Micro Devices GmbH Einsteinring 24 85609 Dornach
General Managers: Alberto Bozzo, Andrew Bowd
Registration: Dornach, Landkr. Muenchen; Registerger. Muenchen, HRB Nr. 43632
#27 lore raw

Hello.

Quoted 9 lines · Joerg
Quoted 5 lines
Quoted 1 line

Hmm, I see how it can be done differently, but no real bug.

Never mind, you're right. In the no-race case, the initial count is always going to be 0, and so incrementing info.probe_count is the right thing to do.

Okay, cool. So now that we have settled on this I removed the last occurence of '2.6.38' from the changelog and here is the result. No code changes since v5.

I've pointed out the mistake in the code that you didn't fix... :-(

Quoted 44 lines

From a6e00d237f20e940afdbcaafb27117fe8730466f Mon Sep 17 00:00:00 2001
From: Joerg Roedel<[email protected]>
Date: Wed, 6 Apr 2011 13:07:53 +0200
Subject: [PATCH] USB host: Fix lockdep warning in AMD PLL quirk

Booting latest kernel on my test machine produces a lockdep warning from the usb_amd_find_chipset_info() function:

  WARNING: at /data/lemmy/linux.trees.git/kernel/lockdep.c:2465 lockdep_trace_alloc+0x95/0xc2()
  Hardware name: Snook
  Modules linked in:
  Pid: 959, comm: work_for_cpu Not tainted 2.6.39-rc2+ #22
  Call Trace:
   [<ffffffff8103c0d4>] warn_slowpath_common+0x80/0x98
   [<ffffffff812387e6>] ? T.492+0x24/0x26
   [<ffffffff8103c101>] warn_slowpath_null+0x15/0x17
   [<ffffffff81068667>] lockdep_trace_alloc+0x95/0xc2
   [<ffffffff810ed9ac>] slab_pre_alloc_hook+0x18/0x3b
   [<ffffffff810ef227>] kmem_cache_alloc_trace+0x25/0xba
   [<ffffffff812387e6>] T.492+0x24/0x26
   [<ffffffff81238816>] pci_get_subsys+0x2e/0x73
   [<ffffffff8123886c>] pci_get_device+0x11/0x13
   [<ffffffff814082a9>] usb_amd_find_chipset_info+0x3f/0x18a

...

It turns out that this function calls pci_get_device under a spin_lock with irqs disabled, but the pci_get_device function is only allowed in preemptible context.

This patch fixes the warning by making all data-structure modifications on temporal storage and commiting this back into the visible structure at the end. While at it, this patch also moves the pci_dev_put calls out of the spinlocks because this function might sleep too.

---
  drivers/usb/host/pci-quirks.c |  117 ++++++++++++++++++++++++++---------------
  1 files changed, 74 insertions(+), 43 deletions(-)
drivers/usb/host/pci-quirks.c +0 -0
index 1d586d4..c6eb69c 100644

[...]

Quoted 19 lines
drivers/usb/host/pci-quirks.c +6 -8
@@ -294,20 +322,23 @@ void usb_amd_dev_put(void)
  		return;
  	}
 
-	if (amd_chipset.nb_dev) {
-		pci_dev_put(amd_chipset.nb_dev);
-		amd_chipset.nb_dev = NULL;
-	}
-	if (amd_chipset.smbus_dev) {
-		pci_dev_put(amd_chipset.smbus_dev);
-		amd_chipset.smbus_dev = NULL;
-	}
+	/* save them to pci_dev_put outside of spinlock */
+	nb    = amd_chipset.nb_dev;
+	smbus = amd_chipset.smbus_dev;
+
+	amd_chipset.nb_dev = NULL;
+	amd_chipset.smbus_dev = NULL;
  	amd_chipset.nb_type = 0;

drivers/usb/host/pci-quirks.c void usb_amd_dev_put(void) · line 341

Quoted 10 lines
drivers/usb/host/pci-quirks.c +5 -0
  	amd_chipset.sb_type = 0;
  	amd_chipset.isoc_reqs = 0;
  	amd_chipset.probe_result = 0;
 
  	spin_unlock_irqrestore(&amd_lock, flags);
+
+	if (nb)
+		pci_dev_put(nb);
+	if (smbus)
+		pci_dev_put(amd_chipset.smbus_dev);

Here it is. This pointer is NULL, it should be 'smbus' instead.

WBR, Sergei

#28 lore raw
Quoted 10 lines · Sergei Shtylyov

Hello.

Quoted 5 lines · Joerg
Quoted 5 lines
Quoted 1 line

Hmm, I see how it can be done differently, but no real bug.

Never mind, you're right. In the no-race case, the initial count is always going to be 0, and so incrementing info.probe_count is the right thing to do.

Quoted 5 lines
Quoted 3 lines

Okay, cool. So now that we have settled on this I removed the last occurence of '2.6.38' from the changelog and here is the result. No code changes since v5.

    I've pointed out the mistake in the code that you didn't fix... :-(

Argh, sorry, I missed this comment from you. I'll fix this.

Joerg

Signature · 5 lines
AMD Operating System Research Center

Advanced Micro Devices GmbH Einsteinring 24 85609 Dornach
General Managers: Alberto Bozzo, Andrew Bowd
Registration: Dornach, Landkr. Muenchen; Registerger. Muenchen, HRB Nr. 43632
#29 lore raw

PATCH v7 1/1 USB host: Fix lockdep warning in AMD PLL quirk

In reply to Sergei Shtylyov
Quoted 8 lines · Sergei Shtylyov

Review

Quoted 4 lines · Joerg
(quoted diff) +4 -0
+	if (nb)
+		pci_dev_put(nb);
+	if (smbus)
+		pci_dev_put(amd_chipset.smbus_dev);

Here it is. This pointer is NULL, it should be 'smbus' instead.

Ok, this is fixed now too.

Quoted 1 line

From 7ee4b6274f5e4c1f238e9ff4faa0b9a43f5203e2 Mon Sep 17 00:00:00 2001

From: Joerg Roedel <[email protected]>
Date: Wed, 6 Apr 2011 13:07:53 +0200
Subject: [PATCH] USB host: Fix lockdep warning in AMD PLL quirk

Booting latest kernel on my test machine produces a lockdep warning from the usb_amd_find_chipset_info() function:

 WARNING: at /data/lemmy/linux.trees.git/kernel/lockdep.c:2465 lockdep_trace_alloc+0x95/0xc2()
 Hardware name: Snook
 Modules linked in:
 Pid: 959, comm: work_for_cpu Not tainted 2.6.39-rc2+ #22
 Call Trace:
  [<ffffffff8103c0d4>] warn_slowpath_common+0x80/0x98
  [<ffffffff812387e6>] ? T.492+0x24/0x26
  [<ffffffff8103c101>] warn_slowpath_null+0x15/0x17
  [<ffffffff81068667>] lockdep_trace_alloc+0x95/0xc2
  [<ffffffff810ed9ac>] slab_pre_alloc_hook+0x18/0x3b
  [<ffffffff810ef227>] kmem_cache_alloc_trace+0x25/0xba
  [<ffffffff812387e6>] T.492+0x24/0x26
  [<ffffffff81238816>] pci_get_subsys+0x2e/0x73
  [<ffffffff8123886c>] pci_get_device+0x11/0x13
  [<ffffffff814082a9>] usb_amd_find_chipset_info+0x3f/0x18a

...

It turns out that this function calls pci_get_device under a spin_lock with irqs disabled, but the pci_get_device function is only allowed in preemptible context.

This patch fixes the warning by making all data-structure modifications on temporal storage and commiting this back into the visible structure at the end. While at it, this patch also moves the pci_dev_put calls out of the spinlocks because this function might sleep too.

---
 drivers/usb/host/pci-quirks.c |  117 ++++++++++++++++++++++++++---------------
 1 files changed, 74 insertions(+), 43 deletions(-)
drivers/usb/host/pci-quirks.c +74 -43
index 1d586d4..9b166d7 100644
@@ -84,65 +84,92 @@ int usb_amd_find_chipset_info(void)
 {
 	u8 rev = 0;
 	unsigned long flags;
+	struct amd_chipset_info info;
+	int ret;
 
 	spin_lock_irqsave(&amd_lock, flags);
 
-	amd_chipset.probe_count++;
 	/* probe only once */
-	if (amd_chipset.probe_count > 1) {
+	if (amd_chipset.probe_count > 0) {
+		amd_chipset.probe_count++;
 		spin_unlock_irqrestore(&amd_lock, flags);
 		return amd_chipset.probe_result;
 	}
+	memset(&info, 0, sizeof(info));
+	spin_unlock_irqrestore(&amd_lock, flags);
 
-	amd_chipset.smbus_dev = pci_get_device(PCI_VENDOR_ID_ATI, 0x4385, NULL);
-	if (amd_chipset.smbus_dev) {
-		rev = amd_chipset.smbus_dev->revision;
+	info.smbus_dev = pci_get_device(PCI_VENDOR_ID_ATI, 0x4385, NULL);
+	if (info.smbus_dev) {
+		rev = info.smbus_dev->revision;
 		if (rev >= 0x40)
-			amd_chipset.sb_type = 1;
+			info.sb_type = 1;
 		else if (rev >= 0x30 && rev <= 0x3b)
-			amd_chipset.sb_type = 3;
+			info.sb_type = 3;
 	} else {
-		amd_chipset.smbus_dev = pci_get_device(PCI_VENDOR_ID_AMD,
-							0x780b, NULL);
-		if (!amd_chipset.smbus_dev) {
-			spin_unlock_irqrestore(&amd_lock, flags);
-			return 0;
+		info.smbus_dev = pci_get_device(PCI_VENDOR_ID_AMD,
+						0x780b, NULL);
+		if (!info.smbus_dev) {
+			ret = 0;
+			goto commit;
 		}
-		rev = amd_chipset.smbus_dev->revision;
+
+		rev = info.smbus_dev->revision;
 		if (rev >= 0x11 && rev <= 0x18)
-			amd_chipset.sb_type = 2;
+			info.sb_type = 2;
 	}
 
-	if (amd_chipset.sb_type == 0) {
-		if (amd_chipset.smbus_dev) {
-			pci_dev_put(amd_chipset.smbus_dev);
-			amd_chipset.smbus_dev = NULL;
+	if (info.sb_type == 0) {
+		if (info.smbus_dev) {
+			pci_dev_put(info.smbus_dev);
+			info.smbus_dev = NULL;
 		}
-		spin_unlock_irqrestore(&amd_lock, flags);
-		return 0;
+		ret = 0;
+		goto commit;
 	}
 
-	amd_chipset.nb_dev = pci_get_device(PCI_VENDOR_ID_AMD, 0x9601, NULL);
-	if (amd_chipset.nb_dev) {
-		amd_chipset.nb_type = 1;
+	info.nb_dev = pci_get_device(PCI_VENDOR_ID_AMD, 0x9601, NULL);
+	if (info.nb_dev) {
+		info.nb_type = 1;
 	} else {
-		amd_chipset.nb_dev = pci_get_device(PCI_VENDOR_ID_AMD,
-							0x1510, NULL);
-		if (amd_chipset.nb_dev) {
-			amd_chipset.nb_type = 2;
-		} else  {
-			amd_chipset.nb_dev = pci_get_device(PCI_VENDOR_ID_AMD,
-								0x9600, NULL);
-			if (amd_chipset.nb_dev)
-				amd_chipset.nb_type = 3;
+		info.nb_dev = pci_get_device(PCI_VENDOR_ID_AMD, 0x1510, NULL);
+		if (info.nb_dev) {
+			info.nb_type = 2;
+		} else {
+			info.nb_dev = pci_get_device(PCI_VENDOR_ID_AMD,
+						     0x9600, NULL);
+			if (info.nb_dev)
+				info.nb_type = 3;
 		}
 	}
 
-	amd_chipset.probe_result = 1;
+	ret = info.probe_result = 1;
 	printk(KERN_DEBUG "QUIRK: Enable AMD PLL fix\n");
 
-	spin_unlock_irqrestore(&amd_lock, flags);
-	return amd_chipset.probe_result;
+commit:
+
+	spin_lock_irqsave(&amd_lock, flags);
+	if (amd_chipset.probe_count > 0) {
+		/* race - someone else was faster - drop devices */
+
+		/* Mark that we where here */
+		amd_chipset.probe_count++;
+		ret = amd_chipset.probe_result;
+
+		spin_unlock_irqrestore(&amd_lock, flags);
+
+		if (info.nb_dev)
+			pci_dev_put(info.nb_dev);
+		if (info.smbus_dev)
+			pci_dev_put(info.smbus_dev);
+
+	} else {
+		/* no race - commit the result */
+		info.probe_count++;
+		amd_chipset = info;
+		spin_unlock_irqrestore(&amd_lock, flags);
+	}
+
+	return ret;
 }
 EXPORT_SYMBOL_GPL(usb_amd_find_chipset_info);
 
@@ -284,6 +311,7 @@ EXPORT_SYMBOL_GPL(usb_amd_quirk_pll_enable);
 
 void usb_amd_dev_put(void)
 {
+	struct pci_dev *nb, *smbus;
 	unsigned long flags;
 
 	spin_lock_irqsave(&amd_lock, flags);
@@ -294,20 +322,23 @@ void usb_amd_dev_put(void)
 		return;
 	}
 
-	if (amd_chipset.nb_dev) {
-		pci_dev_put(amd_chipset.nb_dev);
-		amd_chipset.nb_dev = NULL;
-	}
-	if (amd_chipset.smbus_dev) {
-		pci_dev_put(amd_chipset.smbus_dev);
-		amd_chipset.smbus_dev = NULL;
-	}
+	/* save them to pci_dev_put outside of spinlock */
+	nb    = amd_chipset.nb_dev;
+	smbus = amd_chipset.smbus_dev;
+
+	amd_chipset.nb_dev = NULL;
+	amd_chipset.smbus_dev = NULL;
 	amd_chipset.nb_type = 0;
 	amd_chipset.sb_type = 0;
 	amd_chipset.isoc_reqs = 0;
 	amd_chipset.probe_result = 0;
 
 	spin_unlock_irqrestore(&amd_lock, flags);
+
+	if (nb)
+		pci_dev_put(nb);
+	if (smbus)
+		pci_dev_put(smbus);
 }
 EXPORT_SYMBOL_GPL(usb_amd_dev_put);
 
Signature · 9 lines
1.7.1


-- 
AMD Operating System Research Center

Advanced Micro Devices GmbH Einsteinring 24 85609 Dornach
General Managers: Alberto Bozzo, Andrew Bowd
Registration: Dornach, Landkr. Muenchen; Registerger. Muenchen, HRB Nr. 43632
Acked-by on v7 1/1
#30 lore raw
Quoted 41 lines · Joerg
Quoted 8 lines · Sergei Shtylyov

drivers/usb/host/pci-quirks.c void usb_amd_dev_put(void)

Quoted 4 lines · Joerg
drivers/usb/host/pci-quirks.c +4 -0
+	if (nb)
+		pci_dev_put(nb);
+	if (smbus)
+		pci_dev_put(amd_chipset.smbus_dev);

Here it is. This pointer is NULL, it should be 'smbus' instead.

Ok, this is fixed now too.

From 7ee4b6274f5e4c1f238e9ff4faa0b9a43f5203e2 Mon Sep 17 00:00:00 2001
From: Joerg Roedel <[email protected]>
Date: Wed, 6 Apr 2011 13:07:53 +0200
Subject: [PATCH] USB host: Fix lockdep warning in AMD PLL quirk

Booting latest kernel on my test machine produces a lockdep warning from the usb_amd_find_chipset_info() function:

 WARNING: at /data/lemmy/linux.trees.git/kernel/lockdep.c:2465 lockdep_trace_alloc+0x95/0xc2()
 Hardware name: Snook
 Modules linked in:
 Pid: 959, comm: work_for_cpu Not tainted 2.6.39-rc2+ #22
 Call Trace:
  [<ffffffff8103c0d4>] warn_slowpath_common+0x80/0x98
  [<ffffffff812387e6>] ? T.492+0x24/0x26
  [<ffffffff8103c101>] warn_slowpath_null+0x15/0x17
  [<ffffffff81068667>] lockdep_trace_alloc+0x95/0xc2
  [<ffffffff810ed9ac>] slab_pre_alloc_hook+0x18/0x3b
  [<ffffffff810ef227>] kmem_cache_alloc_trace+0x25/0xba
  [<ffffffff812387e6>] T.492+0x24/0x26
  [<ffffffff81238816>] pci_get_subsys+0x2e/0x73
  [<ffffffff8123886c>] pci_get_device+0x11/0x13
  [<ffffffff814082a9>] usb_amd_find_chipset_info+0x3f/0x18a

...

It turns out that this function calls pci_get_device under a spin_lock with irqs disabled, but the pci_get_device function is only allowed in preemptible context.

Quoted 7 lines

This patch fixes the warning by making all data-structure modifications on temporal storage and commiting this back into the visible structure at the end. While at it, this patch also moves the pci_dev_put calls out of the spinlocks because this function might sleep too.