Skip to content

Commit 2d4cf3d

Browse files
Oliver Neukumgregkh
authored andcommitted
usbnet: include wait queue head in device structure
[ Upstream commit 14a0d63 ] This fixes a race which happens by freeing an object on the stack. Quoting Julius: > The issue is > that it calls usbnet_terminate_urbs() before that, which temporarily > installs a waitqueue in dev->wait in order to be able to wait on the > tasklet to run and finish up some queues. The waiting itself looks > okay, but the access to 'dev->wait' is totally unprotected and can > race arbitrarily. I think in this case usbnet_bh() managed to succeed > it's dev->wait check just before usbnet_terminate_urbs() sets it back > to NULL. The latter then finishes and the waitqueue_t structure on its > stack gets overwritten by other functions halfway through the > wake_up() call in usbnet_bh(). The fix is to just not allocate the data structure on the stack. As dev->wait is abused as a flag it also takes a runtime PM change to fix this bug. Signed-off-by: Oliver Neukum <oneukum@suse.de> Reported-by: Grant Grundler <grundler@google.com> Tested-by: Grant Grundler <grundler@google.com> Signed-off-by: David S. Miller <davem@davemloft.net> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 8d45088 commit 2d4cf3d

2 files changed

Lines changed: 20 additions & 15 deletions

File tree

‎drivers/net/usb/usbnet.c‎

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -727,14 +727,12 @@ EXPORT_SYMBOL_GPL(usbnet_unlink_rx_urbs);
727727
// precondition: never called in_interrupt
728728
static void usbnet_terminate_urbs(struct usbnet *dev)
729729
{
730-
DECLARE_WAIT_QUEUE_HEAD_ONSTACK(unlink_wakeup);
731730
DECLARE_WAITQUEUE(wait, current);
732731
int temp;
733732

734733
/* ensure there are no more active urbs */
735-
add_wait_queue(&unlink_wakeup, &wait);
734+
add_wait_queue(&dev->wait, &wait);
736735
set_current_state(TASK_UNINTERRUPTIBLE);
737-
dev->wait = &unlink_wakeup;
738736
temp = unlink_urbs(dev, &dev->txq) +
739737
unlink_urbs(dev, &dev->rxq);
740738

@@ -748,15 +746,14 @@ static void usbnet_terminate_urbs(struct usbnet *dev)
748746
"waited for %d urb completions\n", temp);
749747
}
750748
set_current_state(TASK_RUNNING);
751-
dev->wait = NULL;
752-
remove_wait_queue(&unlink_wakeup, &wait);
749+
remove_wait_queue(&dev->wait, &wait);
753750
}
754751

755752
int usbnet_stop (struct net_device *net)
756753
{
757754
struct usbnet *dev = netdev_priv(net);
758755
struct driver_info *info = dev->driver_info;
759-
int retval;
756+
int retval, pm;
760757

761758
clear_bit(EVENT_DEV_OPEN, &dev->flags);
762759
netif_stop_queue (net);
@@ -766,6 +763,8 @@ int usbnet_stop (struct net_device *net)
766763
net->stats.rx_packets, net->stats.tx_packets,
767764
net->stats.rx_errors, net->stats.tx_errors);
768765

766+
/* to not race resume */
767+
pm = usb_autopm_get_interface(dev->intf);
769768
/* allow minidriver to stop correctly (wireless devices to turn off
770769
* radio etc) */
771770
if (info->stop) {
@@ -792,6 +791,9 @@ int usbnet_stop (struct net_device *net)
792791
dev->flags = 0;
793792
del_timer_sync (&dev->delay);
794793
tasklet_kill (&dev->bh);
794+
if (!pm)
795+
usb_autopm_put_interface(dev->intf);
796+
795797
if (info->manage_power &&
796798
!test_and_clear_bit(EVENT_NO_RUNTIME_PM, &dev->flags))
797799
info->manage_power(dev, 0);
@@ -1360,11 +1362,12 @@ static void usbnet_bh (unsigned long param)
13601362
/* restart RX again after disabling due to high error rate */
13611363
clear_bit(EVENT_RX_KILL, &dev->flags);
13621364

1363-
// waiting for all pending urbs to complete?
1364-
if (dev->wait) {
1365-
if ((dev->txq.qlen + dev->rxq.qlen + dev->done.qlen) == 0) {
1366-
wake_up (dev->wait);
1367-
}
1365+
/* waiting for all pending urbs to complete?
1366+
* only then can we forgo submitting anew
1367+
*/
1368+
if (waitqueue_active(&dev->wait)) {
1369+
if (dev->txq.qlen + dev->rxq.qlen + dev->done.qlen == 0)
1370+
wake_up_all(&dev->wait);
13681371

13691372
// or are we maybe short a few urbs?
13701373
} else if (netif_running (dev->net) &&
@@ -1502,6 +1505,7 @@ usbnet_probe (struct usb_interface *udev, const struct usb_device_id *prod)
15021505
dev->driver_name = name;
15031506
dev->msg_enable = netif_msg_init (msg_level, NETIF_MSG_DRV
15041507
| NETIF_MSG_PROBE | NETIF_MSG_LINK);
1508+
init_waitqueue_head(&dev->wait);
15051509
skb_queue_head_init (&dev->rxq);
15061510
skb_queue_head_init (&dev->txq);
15071511
skb_queue_head_init (&dev->done);
@@ -1694,9 +1698,10 @@ int usbnet_resume (struct usb_interface *intf)
16941698
spin_unlock_irq(&dev->txq.lock);
16951699

16961700
if (test_bit(EVENT_DEV_OPEN, &dev->flags)) {
1697-
/* handle remote wakeup ASAP */
1698-
if (!dev->wait &&
1699-
netif_device_present(dev->net) &&
1701+
/* handle remote wakeup ASAP
1702+
* we cannot race against stop
1703+
*/
1704+
if (netif_device_present(dev->net) &&
17001705
!timer_pending(&dev->delay) &&
17011706
!test_bit(EVENT_RX_HALT, &dev->flags))
17021707
rx_alloc_submit(dev, GFP_NOIO);

‎include/linux/usb/usbnet.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ struct usbnet {
3030
struct driver_info *driver_info;
3131
const char *driver_name;
3232
void *driver_priv;
33-
wait_queue_head_t *wait;
33+
wait_queue_head_t wait;
3434
struct mutex phy_mutex;
3535
unsigned char suspend_count;
3636
unsigned char pkt_cnt, pkt_err;

0 commit comments

Comments
 (0)