public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Stefan Becker <Stefan.Becker@nokia.com>
To: ext Alan Stern <stern@rowland.harvard.edu>,
	David Brownell <david-b@pacbell.net>,
	linux-kernel@vger.kernel.org, linux-usb@vger.kernel.org
Cc: ext Linus Torvalds <torvalds@linux-foundation.org>
Subject: [PATCH] USB: fix interrupt disabling for HCDs with shared interrupt handlers
Date: Mon, 30 Jun 2008 21:53:16 +0300	[thread overview]
Message-ID: <48692B9C.7090608@nokia.com> (raw)
In-Reply-To: <Pine.LNX.4.44L0.0806301035510.2708-100000@iolanthe.rowland.org>

[-- Attachment #1: Type: text/plain, Size: 169 bytes --]

Hi,

here is the proposed patch to fix the problem. I hope I used git 
correctly to generate it...

Regards,

	Stefan

---
Stefan Becker
E-Mail: Stefan.Becker@nokia.com

[-- Attachment #2: usb-fix-shared-interrupts-disabling.patch --]
[-- Type: text/plain, Size: 3711 bytes --]

From 445575b29a449057294acf61c7d51c5f70490676 Mon Sep 17 00:00:00 2001
From: Stefan Becker <stefan.becker@nokia.com>
Date: Mon, 30 Jun 2008 21:18:29 +0300
Subject: [PATCH] USB: fix interrupt disabling for HCDs with shared interrupt handlers

As has been discussed several times on LKML, IRQF_SHARED | IRQF_DISABLED
doesn't work together reliably, i.e. a shared interrupt handler CAN'T be
certain to be called with interrupts disabled. Most USB HCD handlers use
IRQF_DISABLED and therefore havoc can break out if they share their
interrupt with a handler that doesn't use it.

On my test machine the yenta_socket interrupt handler (no IRQF_DISABLED)
was registered before ehci_hcd and one uhci_hcd instance. Therefore all
usb_hcd_irq() invocations for ehci_hcd and for one uhci_hcd instance
happened with interrupts enabled. That led to random lockups as USB core
HCD functions that acquire the same spinlock could be called twice
from interrupt handlers.

This patch doesn't modify the interface of usb_add_hcd(), i.e. HCD code
can still use IRQF_DISABLED. It will now silently convert this into an
internal HCD flag and clear IRQF_DISABLED before requesting the interrupt
from the Linux core. usb_hcd_irq() will check this flag to disable/re-enable
the interrupts on a case by case basis.

Signed-off-by: Stefan Becker <stefan.becker@nokia.com>

diff --git a/drivers/usb/core/hcd.c b/drivers/usb/core/hcd.c
index 09a53e7..76eb3fc 100644
--- a/drivers/usb/core/hcd.c
+++ b/drivers/usb/core/hcd.c
@@ -1683,20 +1683,30 @@ EXPORT_SYMBOL_GPL(usb_bus_start_enum);
  */
 irqreturn_t usb_hcd_irq (int irq, void *__hcd)
 {
-	struct usb_hcd		*hcd = __hcd;
-	int			start = hcd->state;
+	struct usb_hcd		*hcd  = __hcd;
+	unsigned int            flags = 0;
+	irqreturn_t             rc;
+
+	if (hcd->disable_irq)
+		local_irq_save(flags);
+
+	if (unlikely(hcd->state == HC_STATE_HALT ||
+		     !test_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags))) {
+		rc = IRQ_NONE;
+	} else if (hcd->driver->irq (hcd) == IRQ_NONE) {
+		rc = IRQ_NONE;
+	} else {
+		set_bit(HCD_FLAG_SAW_IRQ, &hcd->flags);
 
-	if (unlikely(start == HC_STATE_HALT ||
-	    !test_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags)))
-		return IRQ_NONE;
-	if (hcd->driver->irq (hcd) == IRQ_NONE)
-		return IRQ_NONE;
+		if (unlikely(hcd->state == HC_STATE_HALT))
+			usb_hc_died (hcd);
+		rc = IRQ_HANDLED;
+	}
 
-	set_bit(HCD_FLAG_SAW_IRQ, &hcd->flags);
+	if (hcd->disable_irq)
+		local_irq_restore(flags);
 
-	if (unlikely(hcd->state == HC_STATE_HALT))
-		usb_hc_died (hcd);
-	return IRQ_HANDLED;
+	return rc;
 }
 
 /*-------------------------------------------------------------------------*/
@@ -1860,6 +1870,14 @@ int usb_add_hcd(struct usb_hcd *hcd,
 
 	/* enable irqs just before we start the controller */
 	if (hcd->driver->irq) {
+
+		/* IRQF_DISABLED doesn't work as advertised when used together
+		 * with IRQF_SHARED. Therefore we handle interrupt disabling
+		 * ourselves in usb_hcd_irq() when the controller requests it.
+		 */
+		hcd->disable_irq = (irqflags & IRQF_DISABLED) ? 1 : 0;
+		irqflags &= ~IRQF_DISABLED;
+
 		snprintf(hcd->irq_descr, sizeof(hcd->irq_descr), "%s:usb%d",
 				hcd->driver->description, hcd->self.busnum);
 		if ((retval = request_irq(irqnum, &usb_hcd_irq, irqflags,
diff --git a/drivers/usb/core/hcd.h b/drivers/usb/core/hcd.h
index a0bf5df..df404ad 100644
--- a/drivers/usb/core/hcd.h
+++ b/drivers/usb/core/hcd.h
@@ -100,6 +100,7 @@ struct usb_hcd {
 	unsigned		wireless:1;	/* Wireless USB HCD */
 	unsigned		authorized_default:1;
 	unsigned		has_tt:1;	/* Integrated TT in root hub */
+	unsigned		disable_irq:1;	/* Disable IRQs for handler */
 
 	int			irq;		/* irq allocated */
 	void __iomem		*regs;		/* device memory/io */

  reply	other threads:[~2008-06-30 18:54 UTC|newest]

Thread overview: 45+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-06-22 16:55 [REGRESSION] 2.6.24/25: random lockups when accessing external USB harddrive Stefan Becker
2008-06-22 17:42 ` Rene Herman
2008-06-22 19:31   ` Alan Stern
2008-06-23 15:52     ` Stefan Becker
2008-06-23 18:10       ` Alan Stern
2008-06-24 18:41         ` Stefan Becker
2008-06-24 21:15           ` Alan Stern
2008-06-25 15:52             ` Stefan Becker
2008-06-25 18:38               ` Alan Stern
2008-06-26  6:31                 ` Stefan Becker
2008-06-26 14:25                   ` Alan Stern
2008-06-26 22:07                     ` Stefan Becker
2008-06-27 16:07                       ` David Brownell
2008-06-28 14:31                         ` Stefan Becker
2008-06-27 16:10                       ` Alan Stern
2008-06-28 14:36                         ` Stefan Becker
2008-06-28 15:39                         ` Stefan Becker
2008-06-28 16:53                           ` Alan Stern
2008-06-28 19:34                             ` BUG in 2.6.26-rc8 interrupt handling Becker Stefan (Nokia-D/Salo)
2008-06-28 19:51                               ` David Brownell
2008-06-29 14:57                                 ` PATCH: 2.6.26-rc8: Fix IRQF_DISABLED for shared interrupts Stefan Becker
2008-06-30  3:09                                   ` David Brownell
2008-06-30  5:22                                     ` Stefan Becker
2008-06-30 14:28                                       ` Henrique de Moraes Holschuh
2008-06-30 14:26                                         ` Alan Cox
2008-06-30  9:34                                     ` Stefan Becker
2008-06-30 11:15                                       ` David Brownell
2008-06-30 14:37                                         ` Alan Stern
2008-06-30 18:53                                           ` Stefan Becker [this message]
2008-06-30 19:35                                             ` [PATCH] USB: fix interrupt disabling for HCDs with shared interrupt handlers Alan Stern
2008-06-30 20:31                                               ` David Brownell
2008-06-30 21:26                                                 ` Stefan Becker
2008-07-01 14:11                                                   ` Alan Stern
2008-07-01 14:19                                                     ` Leonardo Chiquitto
2008-07-01 16:19                                                     ` Stefan Becker
2008-07-01 18:25                                                       ` Greg KH
2008-07-01 18:59                                                         ` Alan Stern
2008-07-01 19:13                                                           ` Greg KH
2008-07-01 19:21                                                           ` David Brownell
2008-07-01 19:15                                                         ` Stefan Becker
2008-07-01 19:51                                                           ` Greg KH
2008-07-01 16:22                                                     ` David Brownell
2008-06-30 21:29                                                 ` Alan Stern
2008-06-30 21:48                                                   ` David Brownell
2008-06-30 19:57                                         ` PATCH: 2.6.26-rc8: Fix IRQF_DISABLED for shared interrupts David Brownell

Reply instructions:

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

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

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

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

  git send-email \
    --in-reply-to=48692B9C.7090608@nokia.com \
    --to=stefan.becker@nokia.com \
    --cc=david-b@pacbell.net \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-usb@vger.kernel.org \
    --cc=stern@rowland.harvard.edu \
    --cc=torvalds@linux-foundation.org \
    /path/to/YOUR_REPLY

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

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