Thu Mar 3 05:47:06 2022 UTC ()
usbnet: Take IFNET_LOCK around access to if_flags in usbnet_detach.

This is not stable without IFNET_LOCK.  Extraneous calls to
usbnet_stop arising from this race might be harmless, but let's
render it unnecessary to even think about that.


(riastradh)
diff -r1.46 -r1.47 src/sys/dev/usb/usbnet.c

cvs diff -r1.46 -r1.47 src/sys/dev/usb/usbnet.c (expand / switch to unified diff)

--- src/sys/dev/usb/usbnet.c 2022/03/03 05:46:58 1.46
+++ src/sys/dev/usb/usbnet.c 2022/03/03 05:47:06 1.47
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1/* $NetBSD: usbnet.c,v 1.46 2022/03/03 05:46:58 riastradh Exp $ */ 1/* $NetBSD: usbnet.c,v 1.47 2022/03/03 05:47:06 riastradh Exp $ */
2 2
3/* 3/*
4 * Copyright (c) 2019 Matthew R. Green 4 * Copyright (c) 2019 Matthew R. Green
5 * All rights reserved. 5 * All rights reserved.
6 * 6 *
7 * Redistribution and use in source and binary forms, with or without 7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions 8 * modification, are permitted provided that the following conditions
9 * are met: 9 * are met:
10 * 1. Redistributions of source code must retain the above copyright 10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer. 11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright 12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the 13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution. 14 * documentation and/or other materials provided with the distribution.
@@ -21,27 +21,27 @@ @@ -21,27 +21,27 @@
21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 22 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE. 26 * SUCH DAMAGE.
27 */ 27 */
28 28
29/* 29/*
30 * Common code shared between USB network drivers. 30 * Common code shared between USB network drivers.
31 */ 31 */
32 32
33#include <sys/cdefs.h> 33#include <sys/cdefs.h>
34__KERNEL_RCSID(0, "$NetBSD: usbnet.c,v 1.46 2022/03/03 05:46:58 riastradh Exp $"); 34__KERNEL_RCSID(0, "$NetBSD: usbnet.c,v 1.47 2022/03/03 05:47:06 riastradh Exp $");
35 35
36#include <sys/param.h> 36#include <sys/param.h>
37#include <sys/kernel.h> 37#include <sys/kernel.h>
38#include <sys/kmem.h> 38#include <sys/kmem.h>
39#include <sys/module.h> 39#include <sys/module.h>
40#include <sys/atomic.h> 40#include <sys/atomic.h>
41 41
42#include <dev/usb/usbnet.h> 42#include <dev/usb/usbnet.h>
43#include <dev/usb/usbhist.h> 43#include <dev/usb/usbhist.h>
44 44
45struct usbnet_cdata { 45struct usbnet_cdata {
46 struct usbnet_chain *uncd_tx_chain; 46 struct usbnet_chain *uncd_tx_chain;
47 struct usbnet_chain *uncd_rx_chain; 47 struct usbnet_chain *uncd_rx_chain;
@@ -1544,31 +1544,31 @@ usbnet_detach(device_t self, int flags) @@ -1544,31 +1544,31 @@ usbnet_detach(device_t self, int flags)
1544 struct usbnet_private * const unp = un->un_pri; 1544 struct usbnet_private * const unp = un->un_pri;
1545 1545
1546 /* Detached before attached finished, so just bail out. */ 1546 /* Detached before attached finished, so just bail out. */
1547 if (unp == NULL || !unp->unp_attached) 1547 if (unp == NULL || !unp->unp_attached)
1548 return 0; 1548 return 0;
1549 1549
1550 struct ifnet * const ifp = usbnet_ifp(un); 1550 struct ifnet * const ifp = usbnet_ifp(un);
1551 struct mii_data * const mii = usbnet_mii(un); 1551 struct mii_data * const mii = usbnet_mii(un);
1552 1552
1553 mutex_enter(&unp->unp_core_lock); 1553 mutex_enter(&unp->unp_core_lock);
1554 unp->unp_dying = true; 1554 unp->unp_dying = true;
1555 mutex_exit(&unp->unp_core_lock); 1555 mutex_exit(&unp->unp_core_lock);
1556 1556
 1557 IFNET_LOCK(ifp);
1557 if (ifp->if_flags & IFF_RUNNING) { 1558 if (ifp->if_flags & IFF_RUNNING) {
1558 IFNET_LOCK(ifp); 
1559 usbnet_if_stop(ifp, 1); 1559 usbnet_if_stop(ifp, 1);
1560 IFNET_UNLOCK(ifp); 
1561 } 1560 }
 1561 IFNET_UNLOCK(ifp);
1562 1562
1563 callout_halt(&unp->unp_stat_ch, NULL); 1563 callout_halt(&unp->unp_stat_ch, NULL);
1564 usb_rem_task_wait(un->un_udev, &unp->unp_ticktask, USB_TASKQ_DRIVER, 1564 usb_rem_task_wait(un->un_udev, &unp->unp_ticktask, USB_TASKQ_DRIVER,
1565 NULL); 1565 NULL);
1566 usb_rem_task_wait(un->un_udev, &unp->unp_mcasttask, USB_TASKQ_DRIVER, 1566 usb_rem_task_wait(un->un_udev, &unp->unp_mcasttask, USB_TASKQ_DRIVER,
1567 NULL); 1567 NULL);
1568 1568
1569 mutex_enter(&unp->unp_core_lock); 1569 mutex_enter(&unp->unp_core_lock);
1570 unp->unp_refcnt--; 1570 unp->unp_refcnt--;
1571 while (unp->unp_refcnt >= 0) { 1571 while (unp->unp_refcnt >= 0) {
1572 /* Wait for processes to go away */ 1572 /* Wait for processes to go away */
1573 cv_wait(&unp->unp_detachcv, &unp->unp_core_lock); 1573 cv_wait(&unp->unp_detachcv, &unp->unp_core_lock);
1574 } 1574 }