Thu Mar 3 05:48:37 2022 UTC ()
usbnet: Refuse to bring interfaces back up once dying.

Make this happen uniformly across all usbnet drivers, not on a
per-driver basis.

This ensures new activity on the interface can't happen by the time
we have stopped existing activity and waited for it to complete.


(riastradh)
diff -r1.57 -r1.58 src/sys/dev/usb/usbnet.c

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

--- src/sys/dev/usb/usbnet.c 2022/03/03 05:48:30 1.57
+++ src/sys/dev/usb/usbnet.c 2022/03/03 05:48:37 1.58
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1/* $NetBSD: usbnet.c,v 1.57 2022/03/03 05:48:30 riastradh Exp $ */ 1/* $NetBSD: usbnet.c,v 1.58 2022/03/03 05:48:37 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.57 2022/03/03 05:48:30 riastradh Exp $"); 34__KERNEL_RCSID(0, "$NetBSD: usbnet.c,v 1.58 2022/03/03 05:48:37 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;
@@ -1279,29 +1279,40 @@ usbnet_tick_task(void *arg) @@ -1279,29 +1279,40 @@ usbnet_tick_task(void *arg)
1279 1279
1280 mutex_enter(&unp->unp_core_lock); 1280 mutex_enter(&unp->unp_core_lock);
1281 usbnet_unbusy(un); 1281 usbnet_unbusy(un);
1282 if (!unp->unp_stopping && !unp->unp_dying) 1282 if (!unp->unp_stopping && !unp->unp_dying)
1283 callout_schedule(&unp->unp_stat_ch, hz); 1283 callout_schedule(&unp->unp_stat_ch, hz);
1284 mutex_exit(&unp->unp_core_lock); 1284 mutex_exit(&unp->unp_core_lock);
1285} 1285}
1286 1286
1287static int 1287static int
1288usbnet_if_init(struct ifnet *ifp) 1288usbnet_if_init(struct ifnet *ifp)
1289{ 1289{
1290 USBNETHIST_FUNC(); USBNETHIST_CALLED(); 1290 USBNETHIST_FUNC(); USBNETHIST_CALLED();
1291 struct usbnet * const un = ifp->if_softc; 1291 struct usbnet * const un = ifp->if_softc;
 1292 bool dying;
1292 1293
1293 KASSERTMSG(IFNET_LOCKED(ifp), "%s", ifp->if_xname); 1294 KASSERTMSG(IFNET_LOCKED(ifp), "%s", ifp->if_xname);
1294 1295
 1296 /*
 1297 * Prevent anyone from bringing the interface back up once
 1298 * we're detaching.
 1299 */
 1300 mutex_enter(&un->un_pri->unp_core_lock);
 1301 dying = un->un_pri->unp_dying;
 1302 mutex_exit(&un->un_pri->unp_core_lock);
 1303 if (dying)
 1304 return EIO;
 1305
1295 return uno_init(un, ifp); 1306 return uno_init(un, ifp);
1296} 1307}
1297 1308
1298 1309
1299/* Various accessors. */ 1310/* Various accessors. */
1300 1311
1301void 1312void
1302usbnet_set_link(struct usbnet *un, bool link) 1313usbnet_set_link(struct usbnet *un, bool link)
1303{ 1314{
1304 un->un_pri->unp_link = link; 1315 un->un_pri->unp_link = link;
1305} 1316}
1306 1317
1307struct ifnet * 1318struct ifnet *
@@ -1562,30 +1573,38 @@ int @@ -1562,30 +1573,38 @@ int
1562usbnet_detach(device_t self, int flags) 1573usbnet_detach(device_t self, int flags)
1563{ 1574{
1564 USBNETHIST_FUNC(); USBNETHIST_CALLED(); 1575 USBNETHIST_FUNC(); USBNETHIST_CALLED();
1565 struct usbnet * const un = device_private(self); 1576 struct usbnet * const un = device_private(self);
1566 struct usbnet_private * const unp = un->un_pri; 1577 struct usbnet_private * const unp = un->un_pri;
1567 1578
1568 /* Detached before attached finished, so just bail out. */ 1579 /* Detached before attached finished, so just bail out. */
1569 if (unp == NULL || !unp->unp_attached) 1580 if (unp == NULL || !unp->unp_attached)
1570 return 0; 1581 return 0;
1571 1582
1572 struct ifnet * const ifp = usbnet_ifp(un); 1583 struct ifnet * const ifp = usbnet_ifp(un);
1573 struct mii_data * const mii = usbnet_mii(un); 1584 struct mii_data * const mii = usbnet_mii(un);
1574 1585
 1586 /*
 1587 * Prevent new activity. After we stop the interface, it
 1588 * cannot be brought back up.
 1589 */
1575 mutex_enter(&unp->unp_core_lock); 1590 mutex_enter(&unp->unp_core_lock);
1576 unp->unp_dying = true; 1591 unp->unp_dying = true;
1577 mutex_exit(&unp->unp_core_lock); 1592 mutex_exit(&unp->unp_core_lock);
1578 1593
 1594 /*
 1595 * If we're still running on the network, stop and wait for all
 1596 * asynchronous activity to finish.
 1597 */
1579 IFNET_LOCK(ifp); 1598 IFNET_LOCK(ifp);
1580 if (ifp->if_flags & IFF_RUNNING) { 1599 if (ifp->if_flags & IFF_RUNNING) {
1581 usbnet_if_stop(ifp, 1); 1600 usbnet_if_stop(ifp, 1);
1582 } 1601 }
1583 IFNET_UNLOCK(ifp); 1602 IFNET_UNLOCK(ifp);
1584 1603
1585 callout_halt(&unp->unp_stat_ch, NULL); 1604 callout_halt(&unp->unp_stat_ch, NULL);
1586 usb_rem_task_wait(un->un_udev, &unp->unp_ticktask, USB_TASKQ_DRIVER, 1605 usb_rem_task_wait(un->un_udev, &unp->unp_ticktask, USB_TASKQ_DRIVER,
1587 NULL); 1606 NULL);
1588 usb_rem_task_wait(un->un_udev, &unp->unp_mcasttask, USB_TASKQ_DRIVER, 1607 usb_rem_task_wait(un->un_udev, &unp->unp_mcasttask, USB_TASKQ_DRIVER,
1589 NULL); 1608 NULL);
1590 1609
1591 mutex_enter(&unp->unp_core_lock); 1610 mutex_enter(&unp->unp_core_lock);