All of lore.kernel.org
 help / color / mirror / Atom feed
From: wangbiao <biao.wang@intel.com>
To: oneukum@suse.de, netdev@vger.kernel.org,
	linux-usb@vger.kernel.org, linux-kernel@vger.kernel.org,
	Ingo Molnar <mingo.kernel.org@gmail.com>
Cc: akpm@linux-foundation.org, mingo@elte.hu, a.p.zijlstra@chello.nl,
	rusty@rustcorp.com.au, william.douglas@intel.com,
	di.zhang@intel.com, biao.wang@intel.com
Subject: [PATCH V3] usbnet: fix race condition caused spinlock bad magic issue
Date: Thu, 14 Nov 2013 10:03:15 +0800	[thread overview]
Message-ID: <1384394595.4229.24.camel@wangbiao> (raw)

From: wang, biao <biao.wang@intel.com>
Date: Mon, 11 Nov 2013 10:23:40 +0800
Subject: [PATCH] usbnet: fix race condition caused spinlock bad magic issue

there is race between usbnet_terminate_urbs and usbnet_bh.
for example:
    cpu 0                                     cpu 1
                                          usbnet_suspend
usbnet_bh {                               ->usbnet_terminate_urbs {
                                              dev->wait = &unlink_wakeup;
                                              while(....){..}//break
    if(dev->wait) is true
                       		              dev->wait=NULL
                                          }//unlink_wakeup is invalid
    __wake_up(dev->wait)//garbage value
}

the race is due to unprotection of dev->wait, so this patch involves a
spinlock to avoid it.

Signed-off-by: wang, biao <biao.wang@intel.com>
Signed-off-by: Zhang, Di <di.zhang@intel.com>
---
 drivers/net/usb/usbnet.c |   16 ++++++++++++++--
 1 files changed, 14 insertions(+), 2 deletions(-)

diff --git a/drivers/net/usb/usbnet.c b/drivers/net/usb/usbnet.c
index 90a429b..f7df1af 100644
--- a/drivers/net/usb/usbnet.c
+++ b/drivers/net/usb/usbnet.c
@@ -86,6 +86,7 @@ static const char driver_name [] = "usbnet";
 
 /* use ethtool to change the level for any given device */
 static int msg_level = -1;
+static spinlock_t dev_wait_lock = __SPIN_LOCK_UNLOCKED(dev_wait_lock);
 module_param (msg_level, int, 0);
 MODULE_PARM_DESC (msg_level, "Override default message level");
 
@@ -764,11 +765,14 @@ static void usbnet_terminate_urbs(struct usbnet *dev)
 	DECLARE_WAIT_QUEUE_HEAD_ONSTACK(unlink_wakeup);
 	DECLARE_WAITQUEUE(wait, current);
 	int temp;
+	unsigned long flags;
 
 	/* ensure there are no more active urbs */
 	add_wait_queue(&unlink_wakeup, &wait);
 	set_current_state(TASK_UNINTERRUPTIBLE);
+	spin_lock_irqsave(&dev_wait_lock, flags);
 	dev->wait = &unlink_wakeup;
+	spin_unlock_irqrestore(&dev_wait_lock, flags);
 	temp = unlink_urbs(dev, &dev->txq) +
 		unlink_urbs(dev, &dev->rxq);
 
@@ -782,7 +786,9 @@ static void usbnet_terminate_urbs(struct usbnet *dev)
 				  "waited for %d urb completions\n", temp);
 	}
 	set_current_state(TASK_RUNNING);
+	spin_lock_irqsave(&dev_wait_lock, flags);
 	dev->wait = NULL;
+	spin_unlock_irqrestore(&dev_wait_lock, flags);
 	remove_wait_queue(&unlink_wakeup, &wait);
 }
 
@@ -1424,6 +1430,7 @@ static void usbnet_bh (unsigned long param)
 	struct usbnet		*dev = (struct usbnet *) param;
 	struct sk_buff		*skb;
 	struct skb_data		*entry;
+	unsigned long           flags;
 
 	while ((skb = skb_dequeue (&dev->done))) {
 		entry = (struct skb_data *) skb->cb;
@@ -1447,13 +1454,18 @@ static void usbnet_bh (unsigned long param)
 	clear_bit(EVENT_RX_KILL, &dev->flags);
 
 	// waiting for all pending urbs to complete?
+	spin_lock_irqsave(&dev_wait_lock, flags);
 	if (dev->wait) {
 		if ((dev->txq.qlen + dev->rxq.qlen + dev->done.qlen) == 0) {
-			wake_up (dev->wait);
+			wake_up(dev->wait);
 		}
+		spin_unlock_irqrestore(&dev_wait_lock, flags);
+		return;
+	}
+	spin_unlock_irqrestore(&dev_wait_lock, flags);
 
 	// or are we maybe short a few urbs?
-	} else if (netif_running (dev->net) &&
+	if (netif_running(dev->net) &&
 		   netif_device_present (dev->net) &&
 		   netif_carrier_ok(dev->net) &&
 		   !timer_pending (&dev->delay) &&
-- 
1.7.0.4




WARNING: multiple messages have this Message-ID (diff)
From: wangbiao <biao.wang-ral2JQCrhuEAvxtiuMwx3w@public.gmane.org>
To: oneukum-l3A5Bk7waGM@public.gmane.org,
	netdev-u79uwXL29TY76Z2rM5mHXA@public.gmane.org,
	linux-usb-u79uwXL29TY76Z2rM5mHXA@public.gmane.org,
	linux-kernel-u79uwXL29TY76Z2rM5mHXA@public.gmane.org,
	Ingo Molnar
	<mingo.kernel.org-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
Cc: akpm-de/tnXTf+JLsfHDXvbKv3WD2FQJk+8+b@public.gmane.org,
	mingo-X9Un+BFzKDI@public.gmane.org,
	a.p.zijlstra-/NLkJaSkS4VmR6Xm/wNWPw@public.gmane.org,
	rusty-8n+1lVoiYb80n/F98K4Iww@public.gmane.org,
	william.douglas-ral2JQCrhuEAvxtiuMwx3w@public.gmane.org,
	di.zhang-ral2JQCrhuEAvxtiuMwx3w@public.gmane.org,
	biao.wang-ral2JQCrhuEAvxtiuMwx3w@public.gmane.org
Subject: [PATCH V3] usbnet: fix race condition caused spinlock bad magic issue
Date: Thu, 14 Nov 2013 10:03:15 +0800	[thread overview]
Message-ID: <1384394595.4229.24.camel@wangbiao> (raw)

From: wang, biao <biao.wang-ral2JQCrhuEAvxtiuMwx3w@public.gmane.org>
Date: Mon, 11 Nov 2013 10:23:40 +0800
Subject: [PATCH] usbnet: fix race condition caused spinlock bad magic issue

there is race between usbnet_terminate_urbs and usbnet_bh.
for example:
    cpu 0                                     cpu 1
                                          usbnet_suspend
usbnet_bh {                               ->usbnet_terminate_urbs {
                                              dev->wait = &unlink_wakeup;
                                              while(....){..}//break
    if(dev->wait) is true
                       		              dev->wait=NULL
                                          }//unlink_wakeup is invalid
    __wake_up(dev->wait)//garbage value
}

the race is due to unprotection of dev->wait, so this patch involves a
spinlock to avoid it.

Signed-off-by: wang, biao <biao.wang-ral2JQCrhuEAvxtiuMwx3w@public.gmane.org>
Signed-off-by: Zhang, Di <di.zhang-ral2JQCrhuEAvxtiuMwx3w@public.gmane.org>
---
 drivers/net/usb/usbnet.c |   16 ++++++++++++++--
 1 files changed, 14 insertions(+), 2 deletions(-)

diff --git a/drivers/net/usb/usbnet.c b/drivers/net/usb/usbnet.c
index 90a429b..f7df1af 100644
--- a/drivers/net/usb/usbnet.c
+++ b/drivers/net/usb/usbnet.c
@@ -86,6 +86,7 @@ static const char driver_name [] = "usbnet";
 
 /* use ethtool to change the level for any given device */
 static int msg_level = -1;
+static spinlock_t dev_wait_lock = __SPIN_LOCK_UNLOCKED(dev_wait_lock);
 module_param (msg_level, int, 0);
 MODULE_PARM_DESC (msg_level, "Override default message level");
 
@@ -764,11 +765,14 @@ static void usbnet_terminate_urbs(struct usbnet *dev)
 	DECLARE_WAIT_QUEUE_HEAD_ONSTACK(unlink_wakeup);
 	DECLARE_WAITQUEUE(wait, current);
 	int temp;
+	unsigned long flags;
 
 	/* ensure there are no more active urbs */
 	add_wait_queue(&unlink_wakeup, &wait);
 	set_current_state(TASK_UNINTERRUPTIBLE);
+	spin_lock_irqsave(&dev_wait_lock, flags);
 	dev->wait = &unlink_wakeup;
+	spin_unlock_irqrestore(&dev_wait_lock, flags);
 	temp = unlink_urbs(dev, &dev->txq) +
 		unlink_urbs(dev, &dev->rxq);
 
@@ -782,7 +786,9 @@ static void usbnet_terminate_urbs(struct usbnet *dev)
 				  "waited for %d urb completions\n", temp);
 	}
 	set_current_state(TASK_RUNNING);
+	spin_lock_irqsave(&dev_wait_lock, flags);
 	dev->wait = NULL;
+	spin_unlock_irqrestore(&dev_wait_lock, flags);
 	remove_wait_queue(&unlink_wakeup, &wait);
 }
 
@@ -1424,6 +1430,7 @@ static void usbnet_bh (unsigned long param)
 	struct usbnet		*dev = (struct usbnet *) param;
 	struct sk_buff		*skb;
 	struct skb_data		*entry;
+	unsigned long           flags;
 
 	while ((skb = skb_dequeue (&dev->done))) {
 		entry = (struct skb_data *) skb->cb;
@@ -1447,13 +1454,18 @@ static void usbnet_bh (unsigned long param)
 	clear_bit(EVENT_RX_KILL, &dev->flags);
 
 	// waiting for all pending urbs to complete?
+	spin_lock_irqsave(&dev_wait_lock, flags);
 	if (dev->wait) {
 		if ((dev->txq.qlen + dev->rxq.qlen + dev->done.qlen) == 0) {
-			wake_up (dev->wait);
+			wake_up(dev->wait);
 		}
+		spin_unlock_irqrestore(&dev_wait_lock, flags);
+		return;
+	}
+	spin_unlock_irqrestore(&dev_wait_lock, flags);
 
 	// or are we maybe short a few urbs?
-	} else if (netif_running (dev->net) &&
+	if (netif_running(dev->net) &&
 		   netif_device_present (dev->net) &&
 		   netif_carrier_ok(dev->net) &&
 		   !timer_pending (&dev->delay) &&
-- 
1.7.0.4



--
To unsubscribe from this list: send the line "unsubscribe linux-usb" in
the body of a message to majordomo-u79uwXL29TY76Z2rM5mHXA@public.gmane.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

             reply	other threads:[~2013-11-14  2:21 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-11-14  2:03 wangbiao [this message]
2013-11-14  2:03 ` [PATCH V3] usbnet: fix race condition caused spinlock bad magic issue wangbiao
2013-11-14  7:44 ` Oliver Neukum
2013-11-14  7:44   ` Oliver Neukum
2013-11-14  9:18   ` Bjørn Mork
2013-11-14  9:18     ` Bjørn Mork
2013-11-14  7:58 ` Ingo Molnar

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=1384394595.4229.24.camel@wangbiao \
    --to=biao.wang@intel.com \
    --cc=a.p.zijlstra@chello.nl \
    --cc=akpm@linux-foundation.org \
    --cc=di.zhang@intel.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-usb@vger.kernel.org \
    --cc=mingo.kernel.org@gmail.com \
    --cc=mingo@elte.hu \
    --cc=netdev@vger.kernel.org \
    --cc=oneukum@suse.de \
    --cc=rusty@rustcorp.com.au \
    --cc=william.douglas@intel.com \
    /path/to/YOUR_REPLY

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

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