Skip to content

Commit 70c37bf

Browse files
bmorkdavem330
authored andcommitted
net: usbnet: prevent buggy devices from killing us
A device sending 0 length frames as fast as it can has been observed killing the host system due to the resulting memory pressure. Temporarily disable RX skb allocation and URB submission when the current error ratio is high, preventing us from trying to allocate an infinite number of skbs. Reenable as soon as we are finished processing the done queue, allowing the device to continue working after short error bursts. Signed-off-by: Bjørn Mork <bjorn@mork.no> Acked-by: Oliver Neukum <oneukum@suse.de> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent 6cdd20c commit 70c37bf

2 files changed

Lines changed: 27 additions & 0 deletions

File tree

‎drivers/net/usb/usbnet.c‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,12 @@ static int rx_submit (struct usbnet *dev, struct urb *urb, gfp_t flags)
380380
unsigned long lockflags;
381381
size_t size = dev->rx_urb_size;
382382

383+
/* prevent rx skb allocation when error ratio is high */
384+
if (test_bit(EVENT_RX_KILL, &dev->flags)) {
385+
usb_free_urb(urb);
386+
return -ENOLINK;
387+
}
388+
383389
skb = __netdev_alloc_skb_ip_align(dev->net, size, flags);
384390
if (!skb) {
385391
netif_dbg(dev, rx_err, dev->net, "no rx skb\n");
@@ -539,6 +545,17 @@ static void rx_complete (struct urb *urb)
539545
break;
540546
}
541547

548+
/* stop rx if packet error rate is high */
549+
if (++dev->pkt_cnt > 30) {
550+
dev->pkt_cnt = 0;
551+
dev->pkt_err = 0;
552+
} else {
553+
if (state == rx_cleanup)
554+
dev->pkt_err++;
555+
if (dev->pkt_err > 20)
556+
set_bit(EVENT_RX_KILL, &dev->flags);
557+
}
558+
542559
state = defer_bh(dev, skb, &dev->rxq, state);
543560

544561
if (urb) {
@@ -791,6 +808,11 @@ int usbnet_open (struct net_device *net)
791808
(dev->driver_info->flags & FLAG_FRAMING_AX) ? "ASIX" :
792809
"simple");
793810

811+
/* reset rx error state */
812+
dev->pkt_cnt = 0;
813+
dev->pkt_err = 0;
814+
clear_bit(EVENT_RX_KILL, &dev->flags);
815+
794816
// delay posting reads until we're fully open
795817
tasklet_schedule (&dev->bh);
796818
if (info->manage_power) {
@@ -1254,6 +1276,9 @@ static void usbnet_bh (unsigned long param)
12541276
}
12551277
}
12561278

1279+
/* restart RX again after disabling due to high error rate */
1280+
clear_bit(EVENT_RX_KILL, &dev->flags);
1281+
12571282
// waiting for all pending urbs to complete?
12581283
if (dev->wait) {
12591284
if ((dev->txq.qlen + dev->rxq.qlen + dev->done.qlen) == 0) {

‎include/linux/usb/usbnet.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ struct usbnet {
3333
wait_queue_head_t *wait;
3434
struct mutex phy_mutex;
3535
unsigned char suspend_count;
36+
unsigned char pkt_cnt, pkt_err;
3637

3738
/* i/o info: pipes etc */
3839
unsigned in, out;
@@ -70,6 +71,7 @@ struct usbnet {
7071
# define EVENT_DEV_OPEN 7
7172
# define EVENT_DEVICE_REPORT_IDLE 8
7273
# define EVENT_NO_RUNTIME_PM 9
74+
# define EVENT_RX_KILL 10
7375
};
7476

7577
static inline struct usb_driver *driver_of(struct usb_interface *intf)

0 commit comments

Comments
 (0)