diff --git a/sys/compat/linuxkpi/common/include/linux/device.h b/sys/compat/linuxkpi/common/include/linux/device.h --- a/sys/compat/linuxkpi/common/include/linux/device.h +++ b/sys/compat/linuxkpi/common/include/linux/device.h @@ -451,9 +451,9 @@ dev->bsddev = NULL; if (bsddev != NULL && dev->bsddev_attached_here) { - mtx_lock(&Giant); + bus_topo_lock(); device_delete_child(device_get_parent(bsddev), bsddev); - mtx_unlock(&Giant); + bus_topo_unlock(); } put_device(dev); } @@ -467,9 +467,9 @@ dev->bsddev = NULL; if (bsddev != NULL && dev->bsddev_attached_here) { - mtx_lock(&Giant); + bus_topo_lock(); device_delete_child(device_get_parent(bsddev), bsddev); - mtx_unlock(&Giant); + bus_topo_unlock(); } } diff --git a/sys/compat/linuxkpi/common/src/linux_pci.c b/sys/compat/linuxkpi/common/src/linux_pci.c --- a/sys/compat/linuxkpi/common/src/linux_pci.c +++ b/sys/compat/linuxkpi/common/src/linux_pci.c @@ -710,10 +710,10 @@ pdrv->bsddriver.methods = pci_methods; pdrv->bsddriver.size = sizeof(struct pci_dev); - mtx_lock(&Giant); + bus_topo_lock(); error = devclass_add_driver(dc, &pdrv->bsddriver, BUS_PASS_DEFAULT, &pdrv->bsdclass); - mtx_unlock(&Giant); + bus_topo_unlock(); return (-error); } @@ -882,10 +882,10 @@ spin_lock(&pci_lock); list_del(&pdrv->node); spin_unlock(&pci_lock); - mtx_lock(&Giant); + bus_topo_lock(); if (bus != NULL) devclass_delete_driver(bus, &pdrv->bsddriver); - mtx_unlock(&Giant); + bus_topo_unlock(); } void @@ -898,10 +898,10 @@ spin_lock(&pci_lock); list_del(&pdrv->node); spin_unlock(&pci_lock); - mtx_lock(&Giant); + bus_topo_lock(); if (bus != NULL) devclass_delete_driver(bus, &pdrv->bsddriver); - mtx_unlock(&Giant); + bus_topo_unlock(); } int diff --git a/sys/compat/linuxkpi/common/src/linux_usb.c b/sys/compat/linuxkpi/common/src/linux_usb.c --- a/sys/compat/linuxkpi/common/src/linux_usb.c +++ b/sys/compat/linuxkpi/common/src/linux_usb.c @@ -1166,7 +1166,9 @@ LIST_FOREACH(sc, &usb_linux_attached_list, sc_attached_list) { if (sc->sc_udrv == drv) { mtx_unlock(&Giant); + bus_topo_lock(); device_detach(sc->sc_fbsd_dev); + bus_topo_unlock(); goto repeat; } } diff --git a/sys/dev/aac/aac.c b/sys/dev/aac/aac.c --- a/sys/dev/aac/aac.c +++ b/sys/dev/aac/aac.c @@ -3305,10 +3305,10 @@ while (co != NULL) { if (co->co_found == 0) { mtx_unlock(&sc->aac_io_lock); - mtx_lock(&Giant); + bus_topo_lock(); device_delete_child(sc->aac_dev, co->co_disk); - mtx_unlock(&Giant); + bus_topo_unlock(); mtx_lock(&sc->aac_io_lock); co_next = TAILQ_NEXT(co, co_link); mtx_lock(&sc->aac_container_lock); @@ -3326,9 +3326,9 @@ /* Attach the newly created containers */ if (added) { mtx_unlock(&sc->aac_io_lock); - mtx_lock(&Giant); + bus_topo_lock(); bus_generic_attach(sc->aac_dev); - mtx_unlock(&Giant); + bus_topo_unlock(); mtx_lock(&sc->aac_io_lock); } diff --git a/sys/dev/acpica/acpi.c b/sys/dev/acpica/acpi.c --- a/sys/dev/acpica/acpi.c +++ b/sys/dev/acpica/acpi.c @@ -3258,10 +3258,9 @@ #endif /* - * Be sure to hold Giant across DEVICE_SUSPEND/RESUME since non-MPSAFE - * drivers need this. + * Be sure to hold Giant across DEVICE_SUSPEND/RESUME */ - mtx_lock(&Giant); + bus_topo_lock(); slp_state = ACPI_SS_NONE; @@ -3387,7 +3386,7 @@ } sc->acpi_next_sstate = 0; - mtx_unlock(&Giant); + bus_topo_unlock(); #ifdef EARLY_AP_STARTUP thread_lock(curthread); diff --git a/sys/dev/acpica/acpi_dock.c b/sys/dev/acpica/acpi_dock.c --- a/sys/dev/acpica/acpi_dock.c +++ b/sys/dev/acpica/acpi_dock.c @@ -193,9 +193,9 @@ if (!device_is_enabled(dev)) device_enable(dev); - mtx_lock(&Giant); + bus_topo_lock(); device_probe_and_attach(dev); - mtx_unlock(&Giant); + bus_topo_unlock(); } static ACPI_STATUS @@ -306,9 +306,9 @@ dev = acpi_get_device(handle); if (dev != NULL && device_is_attached(dev)) { - mtx_lock(&Giant); + bus_topo_lock(); device_detach(dev); - mtx_unlock(&Giant); + bus_topo_unlock(); } acpi_SetInteger(handle, "_EJ0", 0); diff --git a/sys/dev/acpica/acpi_pci.c b/sys/dev/acpica/acpi_pci.c --- a/sys/dev/acpica/acpi_pci.c +++ b/sys/dev/acpica/acpi_pci.c @@ -344,9 +344,9 @@ switch (notify) { case ACPI_NOTIFY_BUS_CHECK: - mtx_lock(&Giant); + bus_topo_lock(); BUS_RESCAN(dev); - mtx_unlock(&Giant); + bus_topo_unlock(); break; default: device_printf(dev, "unknown notify %#x\n", notify); @@ -365,9 +365,9 @@ switch (notify) { case ACPI_NOTIFY_DEVICE_CHECK: - mtx_lock(&Giant); + bus_topo_lock(); BUS_RESCAN(dev); - mtx_unlock(&Giant); + bus_topo_unlock(); break; case ACPI_NOTIFY_EJECT_REQUEST: child = acpi_get_device(h); @@ -376,23 +376,23 @@ acpi_name(h)); return; } - mtx_lock(&Giant); + bus_topo_lock(); error = device_detach(child); if (error) { - mtx_unlock(&Giant); + bus_topo_unlock(); device_printf(dev, "failed to detach %s: %d\n", device_get_nameunit(child), error); return; } status = acpi_SetInteger(h, "_EJ0", 1); if (ACPI_FAILURE(status)) { - mtx_unlock(&Giant); + bus_topo_unlock(); device_printf(dev, "failed to eject %s: %s\n", acpi_name(h), AcpiFormatException(status)); return; } BUS_RESCAN(dev); - mtx_unlock(&Giant); + bus_topo_unlock(); break; default: device_printf(dev, "unknown notify %#x for %s\n", notify, diff --git a/sys/dev/bhnd/cores/chipc/chipc.c b/sys/dev/bhnd/cores/chipc/chipc.c --- a/sys/dev/bhnd/cores/chipc/chipc.c +++ b/sys/dev/bhnd/cores/chipc/chipc.c @@ -1164,13 +1164,13 @@ if (!CHIPC_QUIRK(sc, MUX_SPROM)) return (true); - mtx_lock(&Giant); /* for newbus */ + bus_topo_lock(); parent = device_get_parent(sc->dev); hostb = bhnd_bus_find_hostb_device(parent); if ((error = device_get_children(parent, &devs, &devcount))) { - mtx_unlock(&Giant); + bus_topo_unlock(); return (false); } @@ -1193,7 +1193,7 @@ } free(devs, M_TEMP); - mtx_unlock(&Giant); + bus_topo_unlock(); return (result); } diff --git a/sys/dev/cardbus/cardbus.c b/sys/dev/cardbus/cardbus.c --- a/sys/dev/cardbus/cardbus.c +++ b/sys/dev/cardbus/cardbus.c @@ -199,7 +199,7 @@ domain = pcib_get_domain(cbdev); bus = pcib_get_bus(cbdev); slot = 0; - mtx_lock(&Giant); + bus_topo_lock(); /* For each function, set it up and try to attach a driver to it */ for (func = 0; func <= cardbusfunchigh; func++) { struct cardbus_devinfo *dinfo; @@ -233,7 +233,7 @@ else pci_cfg_save(dinfo->pci.cfg.dev, &dinfo->pci, 1); } - mtx_unlock(&Giant); + bus_topo_unlock(); if (cardattached > 0) return (0); /* POWER_DISABLE_SOCKET(brdev, cbdev); */ @@ -256,11 +256,11 @@ { int err = 0; - mtx_lock(&Giant); + bus_topo_lock(); err = bus_generic_detach(cbdev); if (err == 0) err = device_delete_children(cbdev); - mtx_unlock(&Giant); + bus_topo_unlock(); if (err) return (err); diff --git a/sys/dev/drm2/drm_dp_iic_helper.c b/sys/dev/drm2/drm_dp_iic_helper.c --- a/sys/dev/drm2/drm_dp_iic_helper.c +++ b/sys/dev/drm2/drm_dp_iic_helper.c @@ -228,12 +228,12 @@ int idx, error; static int dp_bus_counter; - mtx_lock(&Giant); + bus_topo_lock(); idx = atomic_fetchadd_int(&dp_bus_counter, 1); ibus = device_add_child(dev, "drm_iic_dp_aux", idx); if (ibus == NULL) { - mtx_unlock(&Giant); + bus_topo_unlock(); DRM_ERROR("drm_iic_dp_aux bus %d creation error\n", idx); return (-ENXIO); } @@ -241,7 +241,7 @@ error = device_probe_and_attach(ibus); if (error != 0) { device_delete_child(dev, ibus); - mtx_unlock(&Giant); + bus_topo_unlock(); DRM_ERROR("drm_iic_dp_aux bus %d attach failed, %d\n", idx, error); return (-error); @@ -256,7 +256,7 @@ *bus = ibus; *adapter = data->port; } - mtx_unlock(&Giant); + bus_topo_unlock(); return (-error); } diff --git a/sys/dev/hyperv/pcib/vmbus_pcib.c b/sys/dev/hyperv/pcib/vmbus_pcib.c --- a/sys/dev/hyperv/pcib/vmbus_pcib.c +++ b/sys/dev/hyperv/pcib/vmbus_pcib.c @@ -560,14 +560,14 @@ devfn = wslot_to_devfn(hpdev->desc.wslot.val); - mtx_lock(&Giant); + bus_topo_lock(); pci_dev = pci_find_dbsf(hbus->pci_domain, 0, PCI_SLOT(devfn), PCI_FUNC(devfn)); if (pci_dev) device_delete_child(hbus->pci_bus, pci_dev); - mtx_unlock(&Giant); + bus_topo_unlock(); mtx_lock(&hbus->device_list_lock); TAILQ_REMOVE(&hbus->children, hpdev, link); diff --git a/sys/dev/ida/ida.c b/sys/dev/ida/ida.c --- a/sys/dev/ida/ida.c +++ b/sys/dev/ida/ida.c @@ -334,9 +334,9 @@ config_intrhook_disestablish(&ida->ich); - mtx_lock(&Giant); + bus_topo_lock(); bus_generic_attach(ida->dev); - mtx_unlock(&Giant); + bus_topo_unlock(); } int diff --git a/sys/dev/mfi/mfi.c b/sys/dev/mfi/mfi.c --- a/sys/dev/mfi/mfi.c +++ b/sys/dev/mfi/mfi.c @@ -1425,9 +1425,9 @@ if (found == 0) { printf("DELETE\n"); mtx_unlock(&sc->mfi_io_lock); - mtx_lock(&Giant); + bus_topo_lock(); device_delete_child(sc->mfi_dev, syspd->pd_dev); - mtx_unlock(&Giant); + bus_topo_unlock(); mtx_lock(&sc->mfi_io_lock); } } @@ -1585,9 +1585,9 @@ KASSERT(ld != NULL, ("volume dissappeared")); */ if (ld != NULL) { - mtx_lock(&Giant); + bus_topo_lock(); device_delete_child(sc->mfi_dev, ld->ld_dev); - mtx_unlock(&Giant); + bus_topo_unlock(); } } break; @@ -1602,11 +1602,11 @@ pd_link) { if (syspd->pd_id == detail->args.pd.device_id) { - mtx_lock(&Giant); + bus_topo_lock(); device_delete_child( sc->mfi_dev, syspd->pd_dev); - mtx_unlock(&Giant); + bus_topo_unlock(); break; } } @@ -1923,11 +1923,11 @@ mfi_release_command(cm); mtx_unlock(&sc->mfi_io_lock); - mtx_lock(&Giant); + bus_topo_lock(); if ((child = device_add_child(sc->mfi_dev, "mfid", -1)) == NULL) { device_printf(sc->mfi_dev, "Failed to add logical disk\n"); free(ld_info, M_MFIBUF); - mtx_unlock(&Giant); + bus_topo_unlock(); mtx_lock(&sc->mfi_io_lock); return; } @@ -1935,7 +1935,7 @@ device_set_ivars(child, ld_info); device_set_desc(child, "MFI Logical Disk"); bus_generic_attach(sc->mfi_dev); - mtx_unlock(&Giant); + bus_topo_unlock(); mtx_lock(&sc->mfi_io_lock); } @@ -2011,11 +2011,11 @@ mfi_release_command(cm); mtx_unlock(&sc->mfi_io_lock); - mtx_lock(&Giant); + bus_topo_lock(); if ((child = device_add_child(sc->mfi_dev, "mfisyspd", -1)) == NULL) { device_printf(sc->mfi_dev, "Failed to add system pd\n"); free(pd_info, M_MFIBUF); - mtx_unlock(&Giant); + bus_topo_unlock(); mtx_lock(&sc->mfi_io_lock); return; } @@ -2023,7 +2023,7 @@ device_set_ivars(child, pd_info); device_set_desc(child, "MFI System PD"); bus_generic_attach(sc->mfi_dev); - mtx_unlock(&Giant); + bus_topo_unlock(); mtx_lock(&sc->mfi_io_lock); } @@ -2832,9 +2832,9 @@ KASSERT(ld != NULL, ("volume dissappeared")); if (cm->cm_frame->header.cmd_status == MFI_STAT_OK) { mtx_unlock(&sc->mfi_io_lock); - mtx_lock(&Giant); + bus_topo_lock(); device_delete_child(sc->mfi_dev, ld->ld_dev); - mtx_unlock(&Giant); + bus_topo_unlock(); mtx_lock(&sc->mfi_io_lock); } else mfi_disk_enable(ld); @@ -2842,11 +2842,11 @@ case MFI_DCMD_CFG_CLEAR: if (cm->cm_frame->header.cmd_status == MFI_STAT_OK) { mtx_unlock(&sc->mfi_io_lock); - mtx_lock(&Giant); + bus_topo_lock(); TAILQ_FOREACH_SAFE(ld, &sc->mfi_ld_tqh, ld_link, ldn) { device_delete_child(sc->mfi_dev, ld->ld_dev); } - mtx_unlock(&Giant); + bus_topo_unlock(); mtx_lock(&sc->mfi_io_lock); } else { TAILQ_FOREACH(ld, &sc->mfi_ld_tqh, ld_link) diff --git a/sys/dev/mfi/mfi_cam.c b/sys/dev/mfi/mfi_cam.c --- a/sys/dev/mfi/mfi_cam.c +++ b/sys/dev/mfi/mfi_cam.c @@ -298,9 +298,9 @@ struct cam_sim *sim; device_t mfip_dev; - mtx_lock(&Giant); + bus_topo_lock(); mfip_dev = device_find_child(sc->mfi_dev, "mfip", -1); - mtx_unlock(&Giant); + bus_topo_unlock(); if (mfip_dev == NULL) { device_printf(sc->mfi_dev, "Couldn't find mfip child device!\n"); return; diff --git a/sys/dev/mlx/mlx.c b/sys/dev/mlx/mlx.c --- a/sys/dev/mlx/mlx.c +++ b/sys/dev/mlx/mlx.c @@ -830,9 +830,9 @@ * Scan the controller to see whether new drives have appeared. */ case MLX_RESCAN_DRIVES: - mtx_lock(&Giant); + bus_topo_lock(); mlx_startup(sc); - mtx_unlock(&Giant); + bus_topo_unlock(); return(0); /* @@ -979,9 +979,9 @@ case MLX_GET_SYSDRIVE: error = ENOENT; MLX_CONFIG_LOCK(sc); - mtx_lock(&Giant); + bus_topo_lock(); mlxd = (struct mlxd_softc *)devclass_get_softc(mlxd_devclass, *arg); - mtx_unlock(&Giant); + bus_topo_unlock(); if ((mlxd != NULL) && (mlxd->mlxd_drive >= sc->mlx_sysdrive) && (mlxd->mlxd_drive < (sc->mlx_sysdrive + MLX_MAXDRIVES))) { error = 0; diff --git a/sys/dev/mlx5/mlx5_core/mlx5_fwdump.c b/sys/dev/mlx5/mlx5_core/mlx5_fwdump.c --- a/sys/dev/mlx5/mlx5_core/mlx5_fwdump.c +++ b/sys/dev/mlx5/mlx5_core/mlx5_fwdump.c @@ -343,11 +343,11 @@ error = -mlx5_set_mfrl_reg(mdev, MLX5_FRL_LEVEL3); if (error == 0) { dev = mdev->pdev->dev.bsddev; - mtx_lock(&Giant); + bus_topo_lock(); bus = device_get_parent(dev); error = BUS_RESET_CHILD(device_get_parent(bus), bus, DEVF_RESET_DETACH); - mtx_unlock(&Giant); + bus_topo_unlock(); } return (error); } diff --git a/sys/dev/mlx5/mlx5_core/mlx5_health.c b/sys/dev/mlx5/mlx5_core/mlx5_health.c --- a/sys/dev/mlx5/mlx5_core/mlx5_health.c +++ b/sys/dev/mlx5/mlx5_core/mlx5_health.c @@ -377,7 +377,8 @@ priv = container_of(health, struct mlx5_priv, health); dev = container_of(priv, struct mlx5_core_dev, priv); - mtx_lock(&Giant); /* XXX newbus needs this */ + /* This might likely be wrong, cut and paste from elsewhere? */ + bus_topo_lock(); if (sensor_pci_no_comm(dev)) { mlx5_core_err(dev, @@ -404,7 +405,7 @@ mlx5_recover_device(dev); } - mtx_unlock(&Giant); + bus_topo_unlock(); } /* How much time to wait until health resetting the driver (in msecs) */ diff --git a/sys/dev/pccard/pccard.c b/sys/dev/pccard/pccard.c --- a/sys/dev/pccard/pccard.c +++ b/sys/dev/pccard/pccard.c @@ -238,7 +238,7 @@ DEVPRINTF((dev, "Card has %d functions. pccard_mfc is %d\n", i + 1, pccard_mfc(sc))); - mtx_lock(&Giant); + bus_topo_lock(); STAILQ_FOREACH(pf, &sc->card.pf_head, pf_list) { if (STAILQ_EMPTY(&pf->cfe_head)) continue; @@ -251,7 +251,7 @@ pf->dev = child; pccard_probe_and_attach_child(dev, child, pf); } - mtx_unlock(&Giant); + bus_topo_unlock(); return (0); } diff --git a/sys/dev/pci/pci_pci.c b/sys/dev/pci/pci_pci.c --- a/sys/dev/pci/pci_pci.c +++ b/sys/dev/pci/pci_pci.c @@ -1397,7 +1397,7 @@ pcib_pcie_cc_timeout, sc); TIMEOUT_TASK_INIT(taskqueue_pci_hp, &sc->pcie_dll_task, 0, pcib_pcie_dll_timeout, sc); - sc->pcie_hp_lock = &Giant; + sc->pcie_hp_lock = bus_topo_mtx(); /* Allocate IRQ. */ if (pcib_alloc_pcie_irq(sc) != 0) diff --git a/sys/dev/pci/pci_user.c b/sys/dev/pci/pci_user.c --- a/sys/dev/pci/pci_user.c +++ b/sys/dev/pci/pci_user.c @@ -1056,8 +1056,11 @@ } } - /* Giant because newbus is Giant locked revisit with newbus locking */ - mtx_lock(&Giant); + /* + * Use bus topology lock to ensure that the pci list of devies doesn't + * change while we're traversing the list, in some cases multiple times. + */ + bus_topo_lock(); switch (cmd) { case PCIOCGETCONF: @@ -1409,7 +1412,7 @@ break; } - mtx_unlock(&Giant); + bus_topo_unlock(); return (error); } diff --git a/sys/dev/sdio/sdiob.c b/sys/dev/sdio/sdiob.c --- a/sys/dev/sdio/sdiob.c +++ b/sys/dev/sdio/sdiob.c @@ -947,10 +947,10 @@ return (ENXIO); } - mtx_lock(&Giant); + bus_topo_lock(); error = devclass_add_driver(bus_devclass, &sdiob_driver, BUS_PASS_DEFAULT, &sdiob_devclass); - mtx_unlock(&Giant); + bus_topo_unlock(); if (error != 0) { printf("%s: Failed to add driver to devclass: %d.\n", __func__, error); diff --git a/sys/dev/twe/twe_freebsd.c b/sys/dev/twe/twe_freebsd.c --- a/sys/dev/twe/twe_freebsd.c +++ b/sys/dev/twe/twe_freebsd.c @@ -583,10 +583,10 @@ char buf[80]; int error; - mtx_lock(&Giant); + bus_topo_lock(); dr->td_disk = device_add_child(sc->twe_dev, NULL, -1); if (dr->td_disk == NULL) { - mtx_unlock(&Giant); + bus_topo_unlock(); twe_printf(sc, "Cannot add unit\n"); return (EIO); } @@ -603,7 +603,7 @@ device_set_desc_copy(dr->td_disk, buf); error = device_probe_and_attach(dr->td_disk); - mtx_unlock(&Giant); + bus_topo_unlock(); if (error != 0) { twe_printf(sc, "Cannot attach unit to controller. error = %d\n", error); return (EIO); @@ -622,9 +622,9 @@ int error = 0; TWE_CONFIG_ASSERT_LOCKED(sc); - mtx_lock(&Giant); + bus_topo_lock(); error = device_delete_child(sc->twe_dev, sc->twe_drive[unit].td_disk); - mtx_unlock(&Giant); + bus_topo_unlock(); if (error != 0) { twe_printf(sc, "failed to delete unit %d\n", unit); return(error); diff --git a/sys/dev/usb/controller/usb_controller.c b/sys/dev/usb/controller/usb_controller.c --- a/sys/dev/usb/controller/usb_controller.c +++ b/sys/dev/usb/controller/usb_controller.c @@ -438,9 +438,9 @@ USB_BUS_UNLOCK(bus); /* detach children first */ - mtx_lock(&Giant); + bus_topo_lock(); bus_generic_detach(dev); - mtx_unlock(&Giant); + bus_topo_unlock(); /* * Free USB device and all subdevices, if any. @@ -803,10 +803,10 @@ static void usb_attach_sub(device_t dev, struct usb_bus *bus) { - mtx_lock(&Giant); + bus_topo_lock(); if (usb_devclass_ptr == NULL) usb_devclass_ptr = devclass_find("usbus"); - mtx_unlock(&Giant); + bus_topo_unlock(); #if USB_HAVE_PF usbpf_attach(bus); diff --git a/sys/dev/usb/net/if_axe.c b/sys/dev/usb/net/if_axe.c --- a/sys/dev/usb/net/if_axe.c +++ b/sys/dev/usb/net/if_axe.c @@ -901,11 +901,11 @@ adv_pause = MIIF_DOPAUSE; else adv_pause = 0; - mtx_lock(&Giant); + bus_topo_lock(); error = mii_attach(ue->ue_dev, &ue->ue_miibus, ifp, uether_ifmedia_upd, ue->ue_methods->ue_mii_sts, BMSR_DEFCAPMASK, sc->sc_phyno, MII_OFFSET_ANY, adv_pause); - mtx_unlock(&Giant); + bus_topo_unlock(); return (error); } diff --git a/sys/dev/usb/net/if_axge.c b/sys/dev/usb/net/if_axge.c --- a/sys/dev/usb/net/if_axge.c +++ b/sys/dev/usb/net/if_axge.c @@ -468,11 +468,11 @@ ifp->if_hwassist = AXGE_CSUM_FEATURES; ifp->if_capenable = ifp->if_capabilities; - mtx_lock(&Giant); + bus_topo_lock(); error = mii_attach(ue->ue_dev, &ue->ue_miibus, ifp, uether_ifmedia_upd, ue->ue_methods->ue_mii_sts, BMSR_DEFCAPMASK, AXGE_PHY_ADDR, MII_OFFSET_ANY, MIIF_DOPAUSE); - mtx_unlock(&Giant); + bus_topo_unlock(); return (error); } diff --git a/sys/dev/usb/net/if_muge.c b/sys/dev/usb/net/if_muge.c --- a/sys/dev/usb/net/if_muge.c +++ b/sys/dev/usb/net/if_muge.c @@ -1638,11 +1638,11 @@ ifp->if_capenable = ifp->if_capabilities; - mtx_lock(&Giant); + bus_topo_lock(); error = mii_attach(ue->ue_dev, &ue->ue_miibus, ifp, uether_ifmedia_upd, ue->ue_methods->ue_mii_sts, BMSR_DEFCAPMASK, sc->sc_phyno, MII_OFFSET_ANY, 0); - mtx_unlock(&Giant); + bus_topo_unlock(); return (0); } diff --git a/sys/dev/usb/net/if_smsc.c b/sys/dev/usb/net/if_smsc.c --- a/sys/dev/usb/net/if_smsc.c +++ b/sys/dev/usb/net/if_smsc.c @@ -1641,11 +1641,11 @@ ifp->if_capenable = ifp->if_capabilities; - mtx_lock(&Giant); + bus_topo_lock(); error = mii_attach(ue->ue_dev, &ue->ue_miibus, ifp, uether_ifmedia_upd, ue->ue_methods->ue_mii_sts, BMSR_DEFCAPMASK, sc->sc_phyno, MII_OFFSET_ANY, 0); - mtx_unlock(&Giant); + bus_topo_unlock(); return (error); } diff --git a/sys/dev/usb/net/if_ure.c b/sys/dev/usb/net/if_ure.c --- a/sys/dev/usb/net/if_ure.c +++ b/sys/dev/usb/net/if_ure.c @@ -1014,7 +1014,6 @@ #endif if_setcapenable(ifp, if_getcapabilities(ifp)); - mtx_lock(&Giant); if (sc->sc_flags & (URE_FLAG_8156 | URE_FLAG_8156B)) { ifmedia_init(&sc->sc_ifmedia, IFM_IMASK, ure_ifmedia_upd, ure_ifmedia_sts); @@ -1024,11 +1023,12 @@ sc->sc_ifmedia.ifm_media = IFM_ETHER | IFM_AUTO; error = 0; } else { + bus_topo_lock(); error = mii_attach(ue->ue_dev, &ue->ue_miibus, ifp, uether_ifmedia_upd, ue->ue_methods->ue_mii_sts, BMSR_DEFCAPMASK, sc->sc_phyno, MII_OFFSET_ANY, 0); + bus_topo_unlock(); } - mtx_unlock(&Giant); sctx = device_get_sysctl_ctx(sc->sc_ue.ue_dev); soid = device_get_sysctl_tree(sc->sc_ue.ue_dev); diff --git a/sys/dev/usb/net/usb_ethernet.c b/sys/dev/usb/net/usb_ethernet.c --- a/sys/dev/usb/net/usb_ethernet.c +++ b/sys/dev/usb/net/usb_ethernet.c @@ -249,12 +249,11 @@ if (ue->ue_methods->ue_mii_upd != NULL && ue->ue_methods->ue_mii_sts != NULL) { - /* device_xxx() depends on this */ - mtx_lock(&Giant); + bus_topo_lock(); error = mii_attach(ue->ue_dev, &ue->ue_miibus, ifp, ue_ifmedia_upd, ue->ue_methods->ue_mii_sts, BMSR_DEFCAPMASK, MII_PHY_ANY, MII_OFFSET_ANY, 0); - mtx_unlock(&Giant); + bus_topo_unlock(); } } @@ -327,9 +326,9 @@ /* detach miibus */ if (ue->ue_miibus != NULL) { - mtx_lock(&Giant); /* device_xxx() depends on this */ + bus_topo_lock(); device_delete_child(ue->ue_dev, ue->ue_miibus); - mtx_unlock(&Giant); + bus_topo_unlock(); } /* free interface instance */ diff --git a/sys/dev/usb/usb_device.c b/sys/dev/usb/usb_device.c --- a/sys/dev/usb/usb_device.c +++ b/sys/dev/usb/usb_device.c @@ -2905,7 +2905,7 @@ * are locked before locking Giant. Else the lock can be * locked multiple times. */ - mtx_lock(&Giant); + bus_topo_lock(); return (1); } @@ -2925,7 +2925,7 @@ sx_xunlock(&udev->enum_sx); return (255); } - mtx_lock(&Giant); + bus_topo_lock(); return (1); } #endif @@ -2935,7 +2935,7 @@ void usbd_enum_unlock(struct usb_device *udev) { - mtx_unlock(&Giant); + bus_topo_unlock(); sx_xunlock(&udev->enum_sx); sx_xunlock(&udev->sr_sx); } @@ -2951,7 +2951,7 @@ * are locked before locking Giant. Else the lock can be * locked multiple times. */ - mtx_lock(&Giant); + bus_topo_lock(); } /* The following function unlocks suspend and resume. */ @@ -2959,7 +2959,7 @@ void usbd_sr_unlock(struct usb_device *udev) { - mtx_unlock(&Giant); + bus_topo_unlock(); sx_xunlock(&udev->sr_sx); } diff --git a/sys/dev/xen/control/control.c b/sys/dev/xen/control/control.c --- a/sys/dev/xen/control/control.c +++ b/sys/dev/xen/control/control.c @@ -223,12 +223,11 @@ KASSERT((PCPU_GET(cpuid) == 0), ("Not running on CPU#0")); /* - * Be sure to hold Giant across DEVICE_SUSPEND/RESUME since non-MPSAFE - * drivers need this. + * Be sure to hold Giant across DEVICE_SUSPEND/RESUME. */ - mtx_lock(&Giant); + bus_topo_lock(); if (DEVICE_SUSPEND(root_bus) != 0) { - mtx_unlock(&Giant); + bus_topo_unlock(); printf("%s: device_suspend failed\n", __func__); return; } @@ -299,7 +298,7 @@ * similar. */ DEVICE_RESUME(root_bus); - mtx_unlock(&Giant); + bus_topo_unlock(); /* * Warm up timecounter again and reset system clock. diff --git a/sys/kern/subr_bus.c b/sys/kern/subr_bus.c --- a/sys/kern/subr_bus.c +++ b/sys/kern/subr_bus.c @@ -865,6 +865,27 @@ DEFINE_CLASS(null, null_methods, 0); +struct mtx * +bus_topo_mtx(void) +{ + + return (&Giant); +} + +void +bus_topo_lock(void) +{ + + mtx_lock(bus_topo_mtx()); +} + +void +bus_topo_unlock(void) +{ + + mtx_unlock(bus_topo_mtx()); +} + /* * Bus pass implementation */ @@ -5725,7 +5746,7 @@ int error, old; /* Locate the device to control. */ - mtx_lock(&Giant); + bus_topo_lock(); req = (struct devreq *)data; switch (cmd) { case DEV_ATTACH: @@ -5752,7 +5773,7 @@ break; } if (error) { - mtx_unlock(&Giant); + bus_topo_unlock(); return (error); } @@ -5973,7 +5994,7 @@ req->dr_flags); break; } - mtx_unlock(&Giant); + bus_topo_unlock(); return (error); } diff --git a/sys/net/iflib_clone.c b/sys/net/iflib_clone.c --- a/sys/net/iflib_clone.c +++ b/sys/net/iflib_clone.c @@ -181,9 +181,9 @@ if (__predict_false(iflib_pseudodev == NULL)) { /* SYSINIT initialization would panic !?! */ - mtx_lock(&Giant); + bus_topo_lock(); iflib_pseudodev = device_add_child(root_bus, "ifpseudo", 0); - mtx_unlock(&Giant); + bus_topo_unlock(); MPASS(iflib_pseudodev != NULL); } ip = iflib_ip_lookup(name); @@ -207,9 +207,9 @@ MPASS(devclass_get_device(ip->ip_dc, unit) == dev); rc = iflib_pseudo_register(dev, ip->ip_sctx, &ctx, &clctx); if (rc) { - mtx_lock(&Giant); + bus_topo_lock(); device_delete_child(iflib_pseudodev, dev); - mtx_unlock(&Giant); + bus_topo_unlock(); } else device_set_softc(dev, ctx); @@ -235,9 +235,9 @@ iflib_stop(ctx); sx_xunlock(ctx_lock); - mtx_lock(&Giant); + bus_topo_lock(); rc = device_delete_child(iflib_pseudodev, dev); - mtx_unlock(&Giant); + bus_topo_unlock(); if (rc == 0) iflib_pseudo_deregister(ctx); } diff --git a/sys/sys/bus.h b/sys/sys/bus.h --- a/sys/sys/bus.h +++ b/sys/sys/bus.h @@ -751,6 +751,14 @@ void bus_set_pass(int pass); +/** + * Routines to lock / unlock the newbus lock. + * Must be taken out to interact with newbus. + */ +void bus_topo_lock(void); +void bus_topo_unlock(void); +struct mtx * bus_topo_mtx(void); + /** * Shorthands for constructing method tables. */ @@ -784,7 +792,7 @@ #define _EARLY_DRIVER_MODULE_ORDERED(name, busname, driver, devclass, \ evh, arg, order, pass) \ - \ + \ static struct driver_module_data name##_##busname##_driver_mod = { \ evh, arg, \ #busname, \ diff --git a/sys/xen/xenbus/xenbusb.c b/sys/xen/xenbus/xenbusb.c --- a/sys/xen/xenbus/xenbusb.c +++ b/sys/xen/xenbus/xenbusb.c @@ -533,12 +533,9 @@ { device_t dev = (device_t)arg; - /* - * Hold Giant until the Giant free newbus changes are committed. - */ - mtx_lock(&Giant); + bus_topo_lock(); xenbusb_probe_children(dev); - mtx_unlock(&Giant); + bus_topo_unlock(); } /**