Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

net: dsa: be compatible with masters which unregister on shutdown

Lino reports that on his system with bcmgenet as DSA master and KSZ9897
as a switch, rebooting or shutting down never works properly.

What does the bcmgenet driver have special to trigger this, that other
DSA masters do not? It has an implementation of ->shutdown which simply
calls its ->remove implementation. Otherwise said, it unregisters its
network interface on shutdown.

This message can be seen in a loop, and it hangs the reboot process there:

unregister_netdevice: waiting for eth0 to become free. Usage count = 3

So why 3?

A usage count of 1 is normal for a registered network interface, and any
virtual interface which links itself as an upper of that will increment
it via dev_hold. In the case of DSA, this is the call path:

dsa_slave_create
-> netdev_upper_dev_link
-> __netdev_upper_dev_link
-> __netdev_adjacent_dev_insert
-> dev_hold

So a DSA switch with 3 interfaces will result in a usage count elevated
by two, and netdev_wait_allrefs will wait until they have gone away.

Other stacked interfaces, like VLAN, watch NETDEV_UNREGISTER events and
delete themselves, but DSA cannot just vanish and go poof, at most it
can unbind itself from the switch devices, but that must happen strictly
earlier compared to when the DSA master unregisters its net_device, so
reacting on the NETDEV_UNREGISTER event is way too late.

It seems that it is a pretty established pattern to have a driver's
->shutdown hook redirect to its ->remove hook, so the same code is
executed regardless of whether the driver is unbound from the device, or
the system is just shutting down. As Florian puts it, it is quite a big
hammer for bcmgenet to unregister its net_device during shutdown, but
having a common code path with the driver unbind helps ensure it is well
tested.

So DSA, for better or for worse, has to live with that and engage in an
arms race of implementing the ->shutdown hook too, from all individual
drivers, and do something sane when paired with masters that unregister
their net_device there. The only sane thing to do, of course, is to
unlink from the master.

However, complications arise really quickly.

The pattern of redirecting ->shutdown to ->remove is not unique to
bcmgenet or even to net_device drivers. In fact, SPI controllers do it
too (see dspi_shutdown -> dspi_remove), and presumably, I2C controllers
and MDIO controllers do it too (this is something I have not researched
too deeply, but even if this is not the case today, it is certainly
plausible to happen in the future, and must be taken into consideration).

Since DSA switches might be SPI devices, I2C devices, MDIO devices, the
insane implication is that for the exact same DSA switch device, we
might have both ->shutdown and ->remove getting called.

So we need to do something with that insane environment. The pattern
I've come up with is "if this, then not that", so if either ->shutdown
or ->remove gets called, we set the device's drvdata to NULL, and in the
other hook, we check whether the drvdata is NULL and just do nothing.
This is probably not necessary for platform devices, just for devices on
buses, but I would really insist for consistency among drivers, because
when code is copy-pasted, it is not always copy-pasted from the best
sources.

So depending on whether the DSA switch's ->remove or ->shutdown will get
called first, we cannot really guarantee even for the same driver if
rebooting will result in the same code path on all platforms. But
nonetheless, we need to do something minimally reasonable on ->shutdown
too to fix the bug. Of course, the ->remove will do more (a full
teardown of the tree, with all data structures freed, and this is why
the bug was not caught for so long). The new ->shutdown method is kept
separate from dsa_unregister_switch not because we couldn't have
unregistered the switch, but simply in the interest of doing something
quick and to the point.

The big question is: does the DSA switch's ->shutdown get called earlier
than the DSA master's ->shutdown? If not, there is still a risk that we
might still trigger the WARN_ON in unregister_netdevice that says we are
attempting to unregister a net_device which has uppers. That's no good.
Although the reference to the master net_device won't physically go away
even if DSA's ->shutdown comes afterwards, remember we have a dev_hold
on it.

The answer to that question lies in this comment above device_link_add:

* A side effect of the link creation is re-ordering of dpm_list and the
* devices_kset list by moving the consumer device and all devices depending
* on it to the ends of these lists (that does not happen to devices that have
* not been registered when this function is called).

so the fact that DSA uses device_link_add towards its master is not
exactly for nothing. device_shutdown() walks devices_kset from the back,
so this is our guarantee that DSA's shutdown happens before the master's
shutdown.

Fixes: 2f1e8ea726e9 ("net: dsa: link interfaces with the DSA master to get rid of lockdep warnings")
Link: https://lore.kernel.org/netdev/20210909095324.12978-1-LinoSanfilippo@gmx.de/
Reported-by: Lino Sanfilippo <LinoSanfilippo@gmx.de>
Signed-off-by: Vladimir Oltean <vladimir.oltean@nxp.com>
Tested-by: Andrew Lunn <andrew@lunn.ch>
Signed-off-by: David S. Miller <davem@davemloft.net>

authored by

Vladimir Oltean and committed by
David S. Miller
0650bf52 cf957997

+457 -24
+19 -2
drivers/net/dsa/b53/b53_mdio.c
··· 351 351 static void b53_mdio_remove(struct mdio_device *mdiodev) 352 352 { 353 353 struct b53_device *dev = dev_get_drvdata(&mdiodev->dev); 354 - struct dsa_switch *ds = dev->ds; 355 354 356 - dsa_unregister_switch(ds); 355 + if (!dev) 356 + return; 357 + 358 + b53_switch_remove(dev); 359 + 360 + dev_set_drvdata(&mdiodev->dev, NULL); 361 + } 362 + 363 + static void b53_mdio_shutdown(struct mdio_device *mdiodev) 364 + { 365 + struct b53_device *dev = dev_get_drvdata(&mdiodev->dev); 366 + 367 + if (!dev) 368 + return; 369 + 370 + b53_switch_shutdown(dev); 371 + 372 + dev_set_drvdata(&mdiodev->dev, NULL); 357 373 } 358 374 359 375 static const struct of_device_id b53_of_match[] = { ··· 389 373 static struct mdio_driver b53_mdio_driver = { 390 374 .probe = b53_mdio_probe, 391 375 .remove = b53_mdio_remove, 376 + .shutdown = b53_mdio_shutdown, 392 377 .mdiodrv.driver = { 393 378 .name = "bcm53xx", 394 379 .of_match_table = b53_of_match,
+13
drivers/net/dsa/b53/b53_mmap.c
··· 316 316 if (dev) 317 317 b53_switch_remove(dev); 318 318 319 + platform_set_drvdata(pdev, NULL); 320 + 319 321 return 0; 322 + } 323 + 324 + static void b53_mmap_shutdown(struct platform_device *pdev) 325 + { 326 + struct b53_device *dev = platform_get_drvdata(pdev); 327 + 328 + if (dev) 329 + b53_switch_shutdown(dev); 330 + 331 + platform_set_drvdata(pdev, NULL); 320 332 } 321 333 322 334 static const struct of_device_id b53_mmap_of_table[] = { ··· 343 331 static struct platform_driver b53_mmap_driver = { 344 332 .probe = b53_mmap_probe, 345 333 .remove = b53_mmap_remove, 334 + .shutdown = b53_mmap_shutdown, 346 335 .driver = { 347 336 .name = "b53-switch", 348 337 .of_match_table = b53_mmap_of_table,
+5
drivers/net/dsa/b53/b53_priv.h
··· 228 228 dsa_unregister_switch(dev->ds); 229 229 } 230 230 231 + static inline void b53_switch_shutdown(struct b53_device *dev) 232 + { 233 + dsa_switch_shutdown(dev->ds); 234 + } 235 + 231 236 #define b53_build_op(type_op_size, val_type) \ 232 237 static inline int b53_##type_op_size(struct b53_device *dev, u8 page, \ 233 238 u8 reg, val_type val) \
+13
drivers/net/dsa/b53/b53_spi.c
··· 321 321 if (dev) 322 322 b53_switch_remove(dev); 323 323 324 + spi_set_drvdata(spi, NULL); 325 + 324 326 return 0; 327 + } 328 + 329 + static void b53_spi_shutdown(struct spi_device *spi) 330 + { 331 + struct b53_device *dev = spi_get_drvdata(spi); 332 + 333 + if (dev) 334 + b53_switch_shutdown(dev); 335 + 336 + spi_set_drvdata(spi, NULL); 325 337 } 326 338 327 339 static const struct of_device_id b53_spi_of_match[] = { ··· 356 344 }, 357 345 .probe = b53_spi_probe, 358 346 .remove = b53_spi_remove, 347 + .shutdown = b53_spi_shutdown, 359 348 }; 360 349 361 350 module_spi_driver(b53_spi_driver);
+19 -2
drivers/net/dsa/b53/b53_srab.c
··· 629 629 static int b53_srab_remove(struct platform_device *pdev) 630 630 { 631 631 struct b53_device *dev = platform_get_drvdata(pdev); 632 - struct b53_srab_priv *priv = dev->priv; 633 632 634 - b53_srab_intr_set(priv, false); 633 + if (!dev) 634 + return 0; 635 + 636 + b53_srab_intr_set(dev->priv, false); 635 637 b53_switch_remove(dev); 636 638 639 + platform_set_drvdata(pdev, NULL); 640 + 637 641 return 0; 642 + } 643 + 644 + static void b53_srab_shutdown(struct platform_device *pdev) 645 + { 646 + struct b53_device *dev = platform_get_drvdata(pdev); 647 + 648 + if (!dev) 649 + return; 650 + 651 + b53_switch_shutdown(dev); 652 + 653 + platform_set_drvdata(pdev, NULL); 638 654 } 639 655 640 656 static struct platform_driver b53_srab_driver = { 641 657 .probe = b53_srab_probe, 642 658 .remove = b53_srab_remove, 659 + .shutdown = b53_srab_shutdown, 643 660 .driver = { 644 661 .name = "b53-srab-switch", 645 662 .of_match_table = b53_srab_of_match,
+12
drivers/net/dsa/bcm_sf2.c
··· 1512 1512 { 1513 1513 struct bcm_sf2_priv *priv = platform_get_drvdata(pdev); 1514 1514 1515 + if (!priv) 1516 + return 0; 1517 + 1515 1518 priv->wol_ports_mask = 0; 1516 1519 /* Disable interrupts */ 1517 1520 bcm_sf2_intr_disable(priv); ··· 1526 1523 if (priv->type == BCM7278_DEVICE_ID) 1527 1524 reset_control_assert(priv->rcdev); 1528 1525 1526 + platform_set_drvdata(pdev, NULL); 1527 + 1529 1528 return 0; 1530 1529 } 1531 1530 1532 1531 static void bcm_sf2_sw_shutdown(struct platform_device *pdev) 1533 1532 { 1534 1533 struct bcm_sf2_priv *priv = platform_get_drvdata(pdev); 1534 + 1535 + if (!priv) 1536 + return; 1535 1537 1536 1538 /* For a kernel about to be kexec'd we want to keep the GPHY on for a 1537 1539 * successful MDIO bus scan to occur. If we did turn off the GPHY ··· 1546 1538 */ 1547 1539 if (priv->hw_params.num_gphy == 1) 1548 1540 bcm_sf2_gphy_enable_set(priv->dev->ds, true); 1541 + 1542 + dsa_switch_shutdown(priv->dev->ds); 1543 + 1544 + platform_set_drvdata(pdev, NULL); 1549 1545 } 1550 1546 1551 1547 #ifdef CONFIG_PM_SLEEP
+21 -1
drivers/net/dsa/dsa_loop.c
··· 340 340 static void dsa_loop_drv_remove(struct mdio_device *mdiodev) 341 341 { 342 342 struct dsa_switch *ds = dev_get_drvdata(&mdiodev->dev); 343 - struct dsa_loop_priv *ps = ds->priv; 343 + struct dsa_loop_priv *ps; 344 + 345 + if (!ds) 346 + return; 347 + 348 + ps = ds->priv; 344 349 345 350 dsa_unregister_switch(ds); 346 351 dev_put(ps->netdev); 352 + 353 + dev_set_drvdata(&mdiodev->dev, NULL); 354 + } 355 + 356 + static void dsa_loop_drv_shutdown(struct mdio_device *mdiodev) 357 + { 358 + struct dsa_switch *ds = dev_get_drvdata(&mdiodev->dev); 359 + 360 + if (!ds) 361 + return; 362 + 363 + dsa_switch_shutdown(ds); 364 + 365 + dev_set_drvdata(&mdiodev->dev, NULL); 347 366 } 348 367 349 368 static struct mdio_driver dsa_loop_drv = { ··· 371 352 }, 372 353 .probe = dsa_loop_drv_probe, 373 354 .remove = dsa_loop_drv_remove, 355 + .shutdown = dsa_loop_drv_shutdown, 374 356 }; 375 357 376 358 #define NUM_FIXED_PHYS (DSA_LOOP_NUM_PORTS - 2)
+6
drivers/net/dsa/lan9303-core.c
··· 1379 1379 } 1380 1380 EXPORT_SYMBOL(lan9303_remove); 1381 1381 1382 + void lan9303_shutdown(struct lan9303 *chip) 1383 + { 1384 + dsa_switch_shutdown(chip->ds); 1385 + } 1386 + EXPORT_SYMBOL(lan9303_shutdown); 1387 + 1382 1388 MODULE_AUTHOR("Juergen Borleis <kernel@pengutronix.de>"); 1383 1389 MODULE_DESCRIPTION("Core driver for SMSC/Microchip LAN9303 three port ethernet switch"); 1384 1390 MODULE_LICENSE("GPL v2");
+1
drivers/net/dsa/lan9303.h
··· 10 10 11 11 int lan9303_probe(struct lan9303 *chip, struct device_node *np); 12 12 int lan9303_remove(struct lan9303 *chip); 13 + void lan9303_shutdown(struct lan9303 *chip);
+20 -4
drivers/net/dsa/lan9303_i2c.c
··· 67 67 68 68 static int lan9303_i2c_remove(struct i2c_client *client) 69 69 { 70 - struct lan9303_i2c *sw_dev; 70 + struct lan9303_i2c *sw_dev = i2c_get_clientdata(client); 71 71 72 - sw_dev = i2c_get_clientdata(client); 73 72 if (!sw_dev) 74 - return -ENODEV; 73 + return 0; 75 74 76 - return lan9303_remove(&sw_dev->chip); 75 + lan9303_remove(&sw_dev->chip); 76 + 77 + i2c_set_clientdata(client, NULL); 78 + 79 + return 0; 80 + } 81 + 82 + static void lan9303_i2c_shutdown(struct i2c_client *client) 83 + { 84 + struct lan9303_i2c *sw_dev = i2c_get_clientdata(client); 85 + 86 + if (!sw_dev) 87 + return; 88 + 89 + lan9303_shutdown(&sw_dev->chip); 90 + 91 + i2c_set_clientdata(client, NULL); 77 92 } 78 93 79 94 /*-------------------------------------------------------------------------*/ ··· 112 97 }, 113 98 .probe = lan9303_i2c_probe, 114 99 .remove = lan9303_i2c_remove, 100 + .shutdown = lan9303_i2c_shutdown, 115 101 .id_table = lan9303_i2c_id, 116 102 }; 117 103 module_i2c_driver(lan9303_i2c_driver);
+15
drivers/net/dsa/lan9303_mdio.c
··· 138 138 return; 139 139 140 140 lan9303_remove(&sw_dev->chip); 141 + 142 + dev_set_drvdata(&mdiodev->dev, NULL); 143 + } 144 + 145 + static void lan9303_mdio_shutdown(struct mdio_device *mdiodev) 146 + { 147 + struct lan9303_mdio *sw_dev = dev_get_drvdata(&mdiodev->dev); 148 + 149 + if (!sw_dev) 150 + return; 151 + 152 + lan9303_shutdown(&sw_dev->chip); 153 + 154 + dev_set_drvdata(&mdiodev->dev, NULL); 141 155 } 142 156 143 157 /*-------------------------------------------------------------------------*/ ··· 169 155 }, 170 156 .probe = lan9303_mdio_probe, 171 157 .remove = lan9303_mdio_remove, 158 + .shutdown = lan9303_mdio_shutdown, 172 159 }; 173 160 mdio_module_driver(lan9303_mdio_driver); 174 161
+18
drivers/net/dsa/lantiq_gswip.c
··· 2184 2184 struct gswip_priv *priv = platform_get_drvdata(pdev); 2185 2185 int i; 2186 2186 2187 + if (!priv) 2188 + return 0; 2189 + 2187 2190 /* disable the switch */ 2188 2191 gswip_mdio_mask(priv, GSWIP_MDIO_GLOB_ENABLE, 0, GSWIP_MDIO_GLOB); 2189 2192 ··· 2200 2197 for (i = 0; i < priv->num_gphy_fw; i++) 2201 2198 gswip_gphy_fw_remove(priv, &priv->gphy_fw[i]); 2202 2199 2200 + platform_set_drvdata(pdev, NULL); 2201 + 2203 2202 return 0; 2203 + } 2204 + 2205 + static void gswip_shutdown(struct platform_device *pdev) 2206 + { 2207 + struct gswip_priv *priv = platform_get_drvdata(pdev); 2208 + 2209 + if (!priv) 2210 + return; 2211 + 2212 + dsa_switch_shutdown(priv->ds); 2213 + 2214 + platform_set_drvdata(pdev, NULL); 2204 2215 } 2205 2216 2206 2217 static const struct gswip_hw_info gswip_xrx200 = { ··· 2240 2223 static struct platform_driver gswip_driver = { 2241 2224 .probe = gswip_probe, 2242 2225 .remove = gswip_remove, 2226 + .shutdown = gswip_shutdown, 2243 2227 .driver = { 2244 2228 .name = "gswip", 2245 2229 .of_match_table = gswip_of_match,
+10 -1
drivers/net/dsa/microchip/ksz8795_spi.c
··· 94 94 if (dev) 95 95 ksz_switch_remove(dev); 96 96 97 + spi_set_drvdata(spi, NULL); 98 + 97 99 return 0; 98 100 } 99 101 ··· 103 101 { 104 102 struct ksz_device *dev = spi_get_drvdata(spi); 105 103 106 - if (dev && dev->dev_ops->shutdown) 104 + if (!dev) 105 + return; 106 + 107 + if (dev->dev_ops->shutdown) 107 108 dev->dev_ops->shutdown(dev); 109 + 110 + dsa_switch_shutdown(dev->ds); 111 + 112 + spi_set_drvdata(spi, NULL); 108 113 } 109 114 110 115 static const struct of_device_id ksz8795_dt_ids[] = {
+12 -2
drivers/net/dsa/microchip/ksz9477_i2c.c
··· 56 56 { 57 57 struct ksz_device *dev = i2c_get_clientdata(i2c); 58 58 59 - ksz_switch_remove(dev); 59 + if (dev) 60 + ksz_switch_remove(dev); 61 + 62 + i2c_set_clientdata(i2c, NULL); 60 63 61 64 return 0; 62 65 } ··· 68 65 { 69 66 struct ksz_device *dev = i2c_get_clientdata(i2c); 70 67 71 - if (dev && dev->dev_ops->shutdown) 68 + if (!dev) 69 + return; 70 + 71 + if (dev->dev_ops->shutdown) 72 72 dev->dev_ops->shutdown(dev); 73 + 74 + dsa_switch_shutdown(dev->ds); 75 + 76 + i2c_set_clientdata(i2c, NULL); 73 77 } 74 78 75 79 static const struct i2c_device_id ksz9477_i2c_id[] = {
+6 -2
drivers/net/dsa/microchip/ksz9477_spi.c
··· 72 72 if (dev) 73 73 ksz_switch_remove(dev); 74 74 75 + spi_set_drvdata(spi, NULL); 76 + 75 77 return 0; 76 78 } 77 79 ··· 81 79 { 82 80 struct ksz_device *dev = spi_get_drvdata(spi); 83 81 84 - if (dev && dev->dev_ops->shutdown) 85 - dev->dev_ops->shutdown(dev); 82 + if (dev) 83 + dsa_switch_shutdown(dev->ds); 84 + 85 + spi_set_drvdata(spi, NULL); 86 86 } 87 87 88 88 static const struct of_device_id ksz9477_dt_ids[] = {
+18
drivers/net/dsa/mt7530.c
··· 3286 3286 struct mt7530_priv *priv = dev_get_drvdata(&mdiodev->dev); 3287 3287 int ret = 0; 3288 3288 3289 + if (!priv) 3290 + return; 3291 + 3289 3292 ret = regulator_disable(priv->core_pwr); 3290 3293 if (ret < 0) 3291 3294 dev_err(priv->dev, ··· 3304 3301 3305 3302 dsa_unregister_switch(priv->ds); 3306 3303 mutex_destroy(&priv->reg_mutex); 3304 + 3305 + dev_set_drvdata(&mdiodev->dev, NULL); 3306 + } 3307 + 3308 + static void mt7530_shutdown(struct mdio_device *mdiodev) 3309 + { 3310 + struct mt7530_priv *priv = dev_get_drvdata(&mdiodev->dev); 3311 + 3312 + if (!priv) 3313 + return; 3314 + 3315 + dsa_switch_shutdown(priv->ds); 3316 + 3317 + dev_set_drvdata(&mdiodev->dev, NULL); 3307 3318 } 3308 3319 3309 3320 static struct mdio_driver mt7530_mdio_driver = { 3310 3321 .probe = mt7530_probe, 3311 3322 .remove = mt7530_remove, 3323 + .shutdown = mt7530_shutdown, 3312 3324 .mdiodrv.driver = { 3313 3325 .name = "mt7530", 3314 3326 .of_match_table = mt7530_of_match,
+18
drivers/net/dsa/mv88e6060.c
··· 290 290 { 291 291 struct dsa_switch *ds = dev_get_drvdata(&mdiodev->dev); 292 292 293 + if (!ds) 294 + return; 295 + 293 296 dsa_unregister_switch(ds); 297 + 298 + dev_set_drvdata(&mdiodev->dev, NULL); 299 + } 300 + 301 + static void mv88e6060_shutdown(struct mdio_device *mdiodev) 302 + { 303 + struct dsa_switch *ds = dev_get_drvdata(&mdiodev->dev); 304 + 305 + if (!ds) 306 + return; 307 + 308 + dsa_switch_shutdown(ds); 309 + 310 + dev_set_drvdata(&mdiodev->dev, NULL); 294 311 } 295 312 296 313 static const struct of_device_id mv88e6060_of_match[] = { ··· 320 303 static struct mdio_driver mv88e6060_driver = { 321 304 .probe = mv88e6060_probe, 322 305 .remove = mv88e6060_remove, 306 + .shutdown = mv88e6060_shutdown, 323 307 .mdiodrv.driver = { 324 308 .name = "mv88e6060", 325 309 .of_match_table = mv88e6060_of_match,
+21 -1
drivers/net/dsa/mv88e6xxx/chip.c
··· 6389 6389 static void mv88e6xxx_remove(struct mdio_device *mdiodev) 6390 6390 { 6391 6391 struct dsa_switch *ds = dev_get_drvdata(&mdiodev->dev); 6392 - struct mv88e6xxx_chip *chip = ds->priv; 6392 + struct mv88e6xxx_chip *chip; 6393 + 6394 + if (!ds) 6395 + return; 6396 + 6397 + chip = ds->priv; 6393 6398 6394 6399 if (chip->info->ptp_support) { 6395 6400 mv88e6xxx_hwtstamp_free(chip); ··· 6415 6410 mv88e6xxx_g1_irq_free(chip); 6416 6411 else 6417 6412 mv88e6xxx_irq_poll_free(chip); 6413 + 6414 + dev_set_drvdata(&mdiodev->dev, NULL); 6415 + } 6416 + 6417 + static void mv88e6xxx_shutdown(struct mdio_device *mdiodev) 6418 + { 6419 + struct dsa_switch *ds = dev_get_drvdata(&mdiodev->dev); 6420 + 6421 + if (!ds) 6422 + return; 6423 + 6424 + dsa_switch_shutdown(ds); 6425 + 6426 + dev_set_drvdata(&mdiodev->dev, NULL); 6418 6427 } 6419 6428 6420 6429 static const struct of_device_id mv88e6xxx_of_match[] = { ··· 6452 6433 static struct mdio_driver mv88e6xxx_driver = { 6453 6434 .probe = mv88e6xxx_probe, 6454 6435 .remove = mv88e6xxx_remove, 6436 + .shutdown = mv88e6xxx_shutdown, 6455 6437 .mdiodrv.driver = { 6456 6438 .name = "mv88e6085", 6457 6439 .of_match_table = mv88e6xxx_of_match,
+18 -2
drivers/net/dsa/ocelot/felix_vsc9959.c
··· 1472 1472 1473 1473 static void felix_pci_remove(struct pci_dev *pdev) 1474 1474 { 1475 - struct felix *felix; 1475 + struct felix *felix = pci_get_drvdata(pdev); 1476 1476 1477 - felix = pci_get_drvdata(pdev); 1477 + if (!felix) 1478 + return; 1478 1479 1479 1480 dsa_unregister_switch(felix->ds); 1480 1481 ··· 1483 1482 kfree(felix); 1484 1483 1485 1484 pci_disable_device(pdev); 1485 + 1486 + pci_set_drvdata(pdev, NULL); 1487 + } 1488 + 1489 + static void felix_pci_shutdown(struct pci_dev *pdev) 1490 + { 1491 + struct felix *felix = pci_get_drvdata(pdev); 1492 + 1493 + if (!felix) 1494 + return; 1495 + 1496 + dsa_switch_shutdown(felix->ds); 1497 + 1498 + pci_set_drvdata(pdev, NULL); 1486 1499 } 1487 1500 1488 1501 static struct pci_device_id felix_ids[] = { ··· 1513 1498 .id_table = felix_ids, 1514 1499 .probe = felix_pci_probe, 1515 1500 .remove = felix_pci_remove, 1501 + .shutdown = felix_pci_shutdown, 1516 1502 }; 1517 1503 module_pci_driver(felix_vsc9959_pci_driver); 1518 1504
+18 -2
drivers/net/dsa/ocelot/seville_vsc9953.c
··· 1245 1245 1246 1246 static int seville_remove(struct platform_device *pdev) 1247 1247 { 1248 - struct felix *felix; 1248 + struct felix *felix = platform_get_drvdata(pdev); 1249 1249 1250 - felix = platform_get_drvdata(pdev); 1250 + if (!felix) 1251 + return 0; 1251 1252 1252 1253 dsa_unregister_switch(felix->ds); 1253 1254 1254 1255 kfree(felix->ds); 1255 1256 kfree(felix); 1256 1257 1258 + platform_set_drvdata(pdev, NULL); 1259 + 1257 1260 return 0; 1261 + } 1262 + 1263 + static void seville_shutdown(struct platform_device *pdev) 1264 + { 1265 + struct felix *felix = platform_get_drvdata(pdev); 1266 + 1267 + if (!felix) 1268 + return; 1269 + 1270 + dsa_switch_shutdown(felix->ds); 1271 + 1272 + platform_set_drvdata(pdev, NULL); 1258 1273 } 1259 1274 1260 1275 static const struct of_device_id seville_of_match[] = { ··· 1281 1266 static struct platform_driver seville_vsc9953_driver = { 1282 1267 .probe = seville_probe, 1283 1268 .remove = seville_remove, 1269 + .shutdown = seville_shutdown, 1284 1270 .driver = { 1285 1271 .name = "mscc_seville", 1286 1272 .of_match_table = of_match_ptr(seville_of_match),
+18
drivers/net/dsa/qca/ar9331.c
··· 1083 1083 struct ar9331_sw_priv *priv = dev_get_drvdata(&mdiodev->dev); 1084 1084 unsigned int i; 1085 1085 1086 + if (!priv) 1087 + return; 1088 + 1086 1089 for (i = 0; i < ARRAY_SIZE(priv->port); i++) { 1087 1090 struct ar9331_sw_port *port = &priv->port[i]; 1088 1091 ··· 1097 1094 dsa_unregister_switch(&priv->ds); 1098 1095 1099 1096 reset_control_assert(priv->sw_reset); 1097 + 1098 + dev_set_drvdata(&mdiodev->dev, NULL); 1099 + } 1100 + 1101 + static void ar9331_sw_shutdown(struct mdio_device *mdiodev) 1102 + { 1103 + struct ar9331_sw_priv *priv = dev_get_drvdata(&mdiodev->dev); 1104 + 1105 + if (!priv) 1106 + return; 1107 + 1108 + dsa_switch_shutdown(&priv->ds); 1109 + 1110 + dev_set_drvdata(&mdiodev->dev, NULL); 1100 1111 } 1101 1112 1102 1113 static const struct of_device_id ar9331_sw_of_match[] = { ··· 1121 1104 static struct mdio_driver ar9331_sw_mdio_driver = { 1122 1105 .probe = ar9331_sw_probe, 1123 1106 .remove = ar9331_sw_remove, 1107 + .shutdown = ar9331_sw_shutdown, 1124 1108 .mdiodrv.driver = { 1125 1109 .name = AR9331_SW_NAME, 1126 1110 .of_match_table = ar9331_sw_of_match,
+18
drivers/net/dsa/qca8k.c
··· 1880 1880 struct qca8k_priv *priv = dev_get_drvdata(&mdiodev->dev); 1881 1881 int i; 1882 1882 1883 + if (!priv) 1884 + return; 1885 + 1883 1886 for (i = 0; i < QCA8K_NUM_PORTS; i++) 1884 1887 qca8k_port_set_status(priv, i, 0); 1885 1888 1886 1889 dsa_unregister_switch(priv->ds); 1890 + 1891 + dev_set_drvdata(&mdiodev->dev, NULL); 1892 + } 1893 + 1894 + static void qca8k_sw_shutdown(struct mdio_device *mdiodev) 1895 + { 1896 + struct qca8k_priv *priv = dev_get_drvdata(&mdiodev->dev); 1897 + 1898 + if (!priv) 1899 + return; 1900 + 1901 + dsa_switch_shutdown(priv->ds); 1902 + 1903 + dev_set_drvdata(&mdiodev->dev, NULL); 1887 1904 } 1888 1905 1889 1906 #ifdef CONFIG_PM_SLEEP ··· 1957 1940 static struct mdio_driver qca8kmdio_driver = { 1958 1941 .probe = qca8k_sw_probe, 1959 1942 .remove = qca8k_sw_remove, 1943 + .shutdown = qca8k_sw_shutdown, 1960 1944 .mdiodrv.driver = { 1961 1945 .name = "qca8k", 1962 1946 .of_match_table = qca8k_of_match,
+19 -1
drivers/net/dsa/realtek-smi-core.c
··· 464 464 465 465 static int realtek_smi_remove(struct platform_device *pdev) 466 466 { 467 - struct realtek_smi *smi = dev_get_drvdata(&pdev->dev); 467 + struct realtek_smi *smi = platform_get_drvdata(pdev); 468 + 469 + if (!smi) 470 + return 0; 468 471 469 472 dsa_unregister_switch(smi->ds); 470 473 if (smi->slave_mii_bus) 471 474 of_node_put(smi->slave_mii_bus->dev.of_node); 472 475 gpiod_set_value(smi->reset, 1); 473 476 477 + platform_set_drvdata(pdev, NULL); 478 + 474 479 return 0; 480 + } 481 + 482 + static void realtek_smi_shutdown(struct platform_device *pdev) 483 + { 484 + struct realtek_smi *smi = platform_get_drvdata(pdev); 485 + 486 + if (!smi) 487 + return; 488 + 489 + dsa_switch_shutdown(smi->ds); 490 + 491 + platform_set_drvdata(pdev, NULL); 475 492 } 476 493 477 494 static const struct of_device_id realtek_smi_of_match[] = { ··· 512 495 }, 513 496 .probe = realtek_smi_probe, 514 497 .remove = realtek_smi_remove, 498 + .shutdown = realtek_smi_shutdown, 515 499 }; 516 500 module_platform_driver(realtek_smi_driver); 517 501
+19 -2
drivers/net/dsa/sja1105/sja1105_main.c
··· 3335 3335 static int sja1105_remove(struct spi_device *spi) 3336 3336 { 3337 3337 struct sja1105_private *priv = spi_get_drvdata(spi); 3338 - struct dsa_switch *ds = priv->ds; 3339 3338 3340 - dsa_unregister_switch(ds); 3339 + if (!priv) 3340 + return 0; 3341 + 3342 + dsa_unregister_switch(priv->ds); 3343 + 3344 + spi_set_drvdata(spi, NULL); 3341 3345 3342 3346 return 0; 3347 + } 3348 + 3349 + static void sja1105_shutdown(struct spi_device *spi) 3350 + { 3351 + struct sja1105_private *priv = spi_get_drvdata(spi); 3352 + 3353 + if (!priv) 3354 + return; 3355 + 3356 + dsa_switch_shutdown(priv->ds); 3357 + 3358 + spi_set_drvdata(spi, NULL); 3343 3359 } 3344 3360 3345 3361 static const struct of_device_id sja1105_dt_ids[] = { ··· 3381 3365 }, 3382 3366 .probe = sja1105_probe, 3383 3367 .remove = sja1105_remove, 3368 + .shutdown = sja1105_shutdown, 3384 3369 }; 3385 3370 3386 3371 module_spi_driver(sja1105_driver);
+6
drivers/net/dsa/vitesse-vsc73xx-core.c
··· 1225 1225 } 1226 1226 EXPORT_SYMBOL(vsc73xx_remove); 1227 1227 1228 + void vsc73xx_shutdown(struct vsc73xx *vsc) 1229 + { 1230 + dsa_switch_shutdown(vsc->ds); 1231 + } 1232 + EXPORT_SYMBOL(vsc73xx_shutdown); 1233 + 1228 1234 MODULE_AUTHOR("Linus Walleij <linus.walleij@linaro.org>"); 1229 1235 MODULE_DESCRIPTION("Vitesse VSC7385/7388/7395/7398 driver"); 1230 1236 MODULE_LICENSE("GPL v2");
+21 -1
drivers/net/dsa/vitesse-vsc73xx-platform.c
··· 116 116 { 117 117 struct vsc73xx_platform *vsc_platform = platform_get_drvdata(pdev); 118 118 119 - return vsc73xx_remove(&vsc_platform->vsc); 119 + if (!vsc_platform) 120 + return 0; 121 + 122 + vsc73xx_remove(&vsc_platform->vsc); 123 + 124 + platform_set_drvdata(pdev, NULL); 125 + 126 + return 0; 127 + } 128 + 129 + static void vsc73xx_platform_shutdown(struct platform_device *pdev) 130 + { 131 + struct vsc73xx_platform *vsc_platform = platform_get_drvdata(pdev); 132 + 133 + if (!vsc_platform) 134 + return; 135 + 136 + vsc73xx_shutdown(&vsc_platform->vsc); 137 + 138 + platform_set_drvdata(pdev, NULL); 120 139 } 121 140 122 141 static const struct vsc73xx_ops vsc73xx_platform_ops = { ··· 163 144 static struct platform_driver vsc73xx_platform_driver = { 164 145 .probe = vsc73xx_platform_probe, 165 146 .remove = vsc73xx_platform_remove, 147 + .shutdown = vsc73xx_platform_shutdown, 166 148 .driver = { 167 149 .name = "vsc73xx-platform", 168 150 .of_match_table = vsc73xx_of_match,
+21 -1
drivers/net/dsa/vitesse-vsc73xx-spi.c
··· 163 163 { 164 164 struct vsc73xx_spi *vsc_spi = spi_get_drvdata(spi); 165 165 166 - return vsc73xx_remove(&vsc_spi->vsc); 166 + if (!vsc_spi) 167 + return 0; 168 + 169 + vsc73xx_remove(&vsc_spi->vsc); 170 + 171 + spi_set_drvdata(spi, NULL); 172 + 173 + return 0; 174 + } 175 + 176 + static void vsc73xx_spi_shutdown(struct spi_device *spi) 177 + { 178 + struct vsc73xx_spi *vsc_spi = spi_get_drvdata(spi); 179 + 180 + if (!vsc_spi) 181 + return; 182 + 183 + vsc73xx_shutdown(&vsc_spi->vsc); 184 + 185 + spi_set_drvdata(spi, NULL); 167 186 } 168 187 169 188 static const struct vsc73xx_ops vsc73xx_spi_ops = { ··· 210 191 static struct spi_driver vsc73xx_spi_driver = { 211 192 .probe = vsc73xx_spi_probe, 212 193 .remove = vsc73xx_spi_remove, 194 + .shutdown = vsc73xx_spi_shutdown, 213 195 .driver = { 214 196 .name = "vsc73xx-spi", 215 197 .of_match_table = vsc73xx_of_match,
+1
drivers/net/dsa/vitesse-vsc73xx.h
··· 27 27 int vsc73xx_is_addr_valid(u8 block, u8 subblock); 28 28 int vsc73xx_probe(struct vsc73xx *vsc); 29 29 int vsc73xx_remove(struct vsc73xx *vsc); 30 + void vsc73xx_shutdown(struct vsc73xx *vsc);
+1
include/net/dsa.h
··· 1046 1046 1047 1047 void dsa_unregister_switch(struct dsa_switch *ds); 1048 1048 int dsa_register_switch(struct dsa_switch *ds); 1049 + void dsa_switch_shutdown(struct dsa_switch *ds); 1049 1050 struct dsa_switch *dsa_switch_find(int tree_index, int sw_index); 1050 1051 #ifdef CONFIG_PM_SLEEP 1051 1052 int dsa_switch_suspend(struct dsa_switch *ds);
+50
net/dsa/dsa2.c
··· 1562 1562 mutex_unlock(&dsa2_mutex); 1563 1563 } 1564 1564 EXPORT_SYMBOL_GPL(dsa_unregister_switch); 1565 + 1566 + /* If the DSA master chooses to unregister its net_device on .shutdown, DSA is 1567 + * blocking that operation from completion, due to the dev_hold taken inside 1568 + * netdev_upper_dev_link. Unlink the DSA slave interfaces from being uppers of 1569 + * the DSA master, so that the system can reboot successfully. 1570 + */ 1571 + void dsa_switch_shutdown(struct dsa_switch *ds) 1572 + { 1573 + struct net_device *master, *slave_dev; 1574 + LIST_HEAD(unregister_list); 1575 + struct dsa_port *dp; 1576 + 1577 + mutex_lock(&dsa2_mutex); 1578 + rtnl_lock(); 1579 + 1580 + list_for_each_entry(dp, &ds->dst->ports, list) { 1581 + if (dp->ds != ds) 1582 + continue; 1583 + 1584 + if (!dsa_port_is_user(dp)) 1585 + continue; 1586 + 1587 + master = dp->cpu_dp->master; 1588 + slave_dev = dp->slave; 1589 + 1590 + netdev_upper_dev_unlink(master, slave_dev); 1591 + /* Just unlinking ourselves as uppers of the master is not 1592 + * sufficient. When the master net device unregisters, that will 1593 + * also call dev_close, which we will catch as NETDEV_GOING_DOWN 1594 + * and trigger a dev_close on our own devices (dsa_slave_close). 1595 + * In turn, that will call dev_mc_unsync on the master's net 1596 + * device. If the master is also a DSA switch port, this will 1597 + * trigger dsa_slave_set_rx_mode which will call dev_mc_sync on 1598 + * its own master. Lockdep will complain about the fact that 1599 + * all cascaded masters have the same dsa_master_addr_list_lock_key, 1600 + * which it normally would not do if the cascaded masters would 1601 + * be in a proper upper/lower relationship, which we've just 1602 + * destroyed. 1603 + * To suppress the lockdep warnings, let's actually unregister 1604 + * the DSA slave interfaces too, to avoid the nonsensical 1605 + * multicast address list synchronization on shutdown. 1606 + */ 1607 + unregister_netdevice_queue(slave_dev, &unregister_list); 1608 + } 1609 + unregister_netdevice_many(&unregister_list); 1610 + 1611 + rtnl_unlock(); 1612 + mutex_unlock(&dsa2_mutex); 1613 + } 1614 + EXPORT_SYMBOL_GPL(dsa_switch_shutdown);