netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [patch 3/3] hci_usb.h: fix hard-to-trigger race
@ 2008-04-18 20:46 akpm
  2008-04-18 21:10 ` Marcel Holtmann
  0 siblings, 1 reply; 8+ messages in thread
From: akpm @ 2008-04-18 20:46 UTC (permalink / raw)
  To: marcel; +Cc: hidave.darkstar, linux-bluetooth, netdev, akpm, pavel, pavel

From: Pavel Machek <pavel@ucw.cz>

If someone tries to _urb_unlink while _urb_queue_head is running, he'll see
_urb->queue == NULL and fail to do any locking.  Prevent that from happening
by strategically placed barriers.

Signed-off-by: Pavel Machek <pavel@suse.cz>
Cc: Marcel Holtmann <marcel@holtmann.org>
Cc: Dave Young <hidave.darkstar@gmail.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
---

 drivers/bluetooth/hci_usb.h |   21 +++++++++++++--------
 1 file changed, 13 insertions(+), 8 deletions(-)

diff -puN drivers/bluetooth/hci_usb.h~hci_usbh-fix-hard-to-trigger-race drivers/bluetooth/hci_usb.h
--- a/drivers/bluetooth/hci_usb.h~hci_usbh-fix-hard-to-trigger-race
+++ a/drivers/bluetooth/hci_usb.h
@@ -70,7 +70,8 @@ static inline void _urb_queue_head(struc
 {
 	unsigned long flags;
 	spin_lock_irqsave(&q->lock, flags);
-	list_add(&_urb->list, &q->head); _urb->queue = q;
+	/* _urb_unlink needs to know which spinlock to use, thus mb(). */
+	_urb->queue = q; mb(); list_add(&_urb->list, &q->head);
 	spin_unlock_irqrestore(&q->lock, flags);
 }
 
@@ -78,19 +79,23 @@ static inline void _urb_queue_tail(struc
 {
 	unsigned long flags;
 	spin_lock_irqsave(&q->lock, flags);
-	list_add_tail(&_urb->list, &q->head); _urb->queue = q;
+	/* _urb_unlink needs to know which spinlock to use, thus mb(). */
+	_urb->queue = q; mb(); list_add_tail(&_urb->list, &q->head);
 	spin_unlock_irqrestore(&q->lock, flags);
 }
 
 static inline void _urb_unlink(struct _urb *_urb)
 {
-	struct _urb_queue *q = _urb->queue;
+	struct _urb_queue *q;
 	unsigned long flags;
-	if (q) {
-		spin_lock_irqsave(&q->lock, flags);
-		list_del(&_urb->list); _urb->queue = NULL;
-		spin_unlock_irqrestore(&q->lock, flags);
-	}
+
+	mb();
+	q = _urb->queue;
+	/* If q is NULL, it will die at easy-to-debug NULL pointer dereference.
+	   No need to BUG(). */
+	spin_lock_irqsave(&q->lock, flags);
+	list_del(&_urb->list); _urb->queue = NULL;
+	spin_unlock_irqrestore(&q->lock, flags);
 }
 
 struct hci_usb {
_

^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2008-04-19 17:56 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2008-04-18 20:46 [patch 3/3] hci_usb.h: fix hard-to-trigger race akpm
2008-04-18 21:10 ` Marcel Holtmann
     [not found]   ` <55DEC459-2D49-4366-88CC-21E9CA6783DC-kz+m5ild9QBg9hUCZPvPmw@public.gmane.org>
2008-04-18 21:21     ` Andrew Morton
     [not found]       ` <20080418142153.4e0eb83b.akpm-de/tnXTf+JLsfHDXvbKv3WD2FQJk+8+b@public.gmane.org>
2008-04-18 22:41         ` David Miller
2008-04-19  1:25       ` Marcel Holtmann
2008-04-19 16:49         ` [patch] document hci_usb as broken Pavel Machek
2008-04-19 17:40           ` Marcel Holtmann
2008-04-19 17:57             ` Pavel Machek

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).