linux/net/dsa
Vladimir Oltean 6c24a03a61 net: dsa: improve shutdown sequence
Alexander Sverdlin presents 2 problems during shutdown with the
lan9303 driver. One is specific to lan9303 and the other just happens
to reproduce there.

The first problem is that lan9303 is unique among DSA drivers in that it
calls dev_get_drvdata() at "arbitrary runtime" (not probe, not shutdown,
not remove):

phy_state_machine()
-> ...
   -> dsa_user_phy_read()
      -> ds->ops->phy_read()
         -> lan9303_phy_read()
            -> chip->ops->phy_read()
               -> lan9303_mdio_phy_read()
                  -> dev_get_drvdata()

But we never stop the phy_state_machine(), so it may continue to run
after dsa_switch_shutdown(). Our common pattern in all DSA drivers is
to set drvdata to NULL to suppress the remove() method that may come
afterwards. But in this case it will result in an NPD.

The second problem is that the way in which we set
dp->conduit->dsa_ptr = NULL; is concurrent with receive packet
processing. dsa_switch_rcv() checks once whether dev->dsa_ptr is NULL,
but afterwards, rather than continuing to use that non-NULL value,
dev->dsa_ptr is dereferenced again and again without NULL checks:
dsa_conduit_find_user() and many other places. In between dereferences,
there is no locking to ensure that what was valid once continues to be
valid.

Both problems have the common aspect that closing the conduit interface
solves them.

In the first case, dev_close(conduit) triggers the NETDEV_GOING_DOWN
event in dsa_user_netdevice_event() which closes user ports as well.
dsa_port_disable_rt() calls phylink_stop(), which synchronously stops
the phylink state machine, and ds->ops->phy_read() will thus no longer
call into the driver after this point.

In the second case, dev_close(conduit) should do this, as per
Documentation/networking/driver.rst:

| Quiescence
| ----------
|
| After the ndo_stop routine has been called, the hardware must
| not receive or transmit any data.  All in flight packets must
| be aborted. If necessary, poll or wait for completion of
| any reset commands.

So it should be sufficient to ensure that later, when we zeroize
conduit->dsa_ptr, there will be no concurrent dsa_switch_rcv() call
on this conduit.

The addition of the netif_device_detach() function is to ensure that
ioctls, rtnetlinks and ethtool requests on the user ports no longer
propagate down to the driver - we're no longer prepared to handle them.

The race condition actually did not exist when commit 0650bf52b3
("net: dsa: be compatible with masters which unregister on shutdown")
first introduced dsa_switch_shutdown(). It was created later, when we
stopped unregistering the user interfaces from a bad spot, and we just
replaced that sequence with a racy zeroization of conduit->dsa_ptr
(one which doesn't ensure that the interfaces aren't up).

Reported-by: Alexander Sverdlin <alexander.sverdlin@siemens.com>
Closes: https://lore.kernel.org/netdev/2d2e3bba17203c14a5ffdabc174e3b6bbb9ad438.camel@siemens.com/
Closes: https://lore.kernel.org/netdev/c1bf4de54e829111e0e4a70e7bd1cf523c9550ff.camel@siemens.com/
Fixes: ee534378f0 ("net: dsa: fix panic when DSA master device unbinds on shutdown")
Reviewed-by: Alexander Sverdlin <alexander.sverdlin@siemens.com>
Tested-by: Alexander Sverdlin <alexander.sverdlin@siemens.com>
Signed-off-by: Vladimir Oltean <vladimir.oltean@nxp.com>
Link: https://patch.msgid.link/20240913203549.3081071-1-vladimir.oltean@nxp.com
Signed-off-by: Paolo Abeni <pabeni@redhat.com>
2024-10-01 09:56:36 +02:00
..
conduit.c net: dsa: Use conduit and user terms 2023-10-24 13:08:14 -07:00
conduit.h net: dsa: Use conduit and user terms 2023-10-24 13:08:14 -07:00
devlink.c devlink: extend devlink_param *set pointer 2024-04-22 13:05:19 -07:00
devlink.h net: dsa: move rest of devlink setup/teardown to devlink.c 2022-11-22 20:41:47 -08:00
dsa.c net: dsa: improve shutdown sequence 2024-10-01 09:56:36 +02:00
dsa.h net: dsa: Use conduit and user terms 2023-10-24 13:08:14 -07:00
Kconfig net: dsa: vsc73xx: introduce tag 8021q for vsc73xx 2024-07-15 06:55:15 -07:00
Makefile net: dsa: vsc73xx: introduce tag 8021q for vsc73xx 2024-07-15 06:55:15 -07:00
netlink.c net: dsa: Rename IFLA_DSA_MASTER to IFLA_DSA_CONDUIT 2023-10-24 13:08:14 -07:00
netlink.h net: dsa: kill off dsa_priv.h 2022-11-22 20:41:54 -08:00
port.c net: dsa: update the unicast MAC address when changing conduit 2024-06-10 13:48:06 +01:00
port.h net: dsa: Use conduit and user terms 2023-10-24 13:08:14 -07:00
stubs.c net: dsa: replace NETDEV_PRE_CHANGE_HWTSTAMP notifier with a stub 2023-04-09 15:35:49 +01:00
switch.c net: dsa: Use conduit and user terms 2023-10-24 13:08:14 -07:00
switch.h net: dsa: Use conduit and user terms 2023-10-24 13:08:14 -07:00
tag_8021q.c net: dsa: prepare 'dsa_tag_8021q_bridge_join' for standalone use 2024-07-15 06:55:16 -07:00
tag_8021q.h net: dsa: tag_sja1105: refactor skb->dev assignment to dsa_tag_8021q_find_user() 2024-07-15 06:55:15 -07:00
tag_ar9331.c net: fill in MODULE_DESCRIPTION()s for DSA tags 2024-01-05 08:06:19 -08:00
tag_brcm.c net: fill in MODULE_DESCRIPTION()s for DSA tags 2024-01-05 08:06:19 -08:00
tag_dsa.c net: fill in MODULE_DESCRIPTION()s for DSA tags 2024-01-05 08:06:19 -08:00
tag_gswip.c net: fill in MODULE_DESCRIPTION()s for DSA tags 2024-01-05 08:06:19 -08:00
tag_hellcreek.c net: fill in MODULE_DESCRIPTION()s for DSA tags 2024-01-05 08:06:19 -08:00
tag_ksz.c net: dsa: microchip: update tag_ksz masks for KSZ9477 family 2024-09-10 17:27:56 -07:00
tag_lan9303.c net: fill in MODULE_DESCRIPTION()s for DSA tags 2024-01-05 08:06:19 -08:00
tag_mtk.c net: fill in MODULE_DESCRIPTION()s for DSA tags 2024-01-05 08:06:19 -08:00
tag_none.c net: fill in MODULE_DESCRIPTION()s for DSA tags 2024-01-05 08:06:19 -08:00
tag_ocelot_8021q.c net: dsa: tag_sja1105: absorb entire sja1105_vlan_rcv() into dsa_8021q_rcv() 2024-07-15 06:55:15 -07:00
tag_ocelot.c net: mscc: ocelot: use ocelot_xmit_get_vlan_info() also for FDMA and register injection 2024-08-16 09:59:32 +01:00
tag_qca.c net: fill in MODULE_DESCRIPTION()s for DSA tags 2024-01-05 08:06:19 -08:00
tag_rtl4_a.c net: fill in MODULE_DESCRIPTION()s for DSA tags 2024-01-05 08:06:19 -08:00
tag_rtl8_4.c net: fill in MODULE_DESCRIPTION()s for DSA tags 2024-01-05 08:06:19 -08:00
tag_rzn1_a5psw.c net: fill in MODULE_DESCRIPTION()s for DSA tags 2024-01-05 08:06:19 -08:00
tag_sja1105.c net: dsa: tag_sja1105: refactor skb->dev assignment to dsa_tag_8021q_find_user() 2024-07-15 06:55:15 -07:00
tag_trailer.c net: fill in MODULE_DESCRIPTION()s for DSA tags 2024-01-05 08:06:19 -08:00
tag_vsc73xx_8021q.c net: dsa: vsc73xx: introduce tag 8021q for vsc73xx 2024-07-15 06:55:15 -07:00
tag_xrs700x.c net: fill in MODULE_DESCRIPTION()s for DSA tags 2024-01-05 08:06:19 -08:00
tag.c net: dsa: provide a software untagging function on RX for VLAN-aware bridges 2024-08-16 09:59:32 +01:00
tag.h net: dsa: provide a software untagging function on RX for VLAN-aware bridges 2024-08-16 09:59:32 +01:00
trace.c net: dsa: add trace points for FDB/MDB operations 2023-04-12 08:36:07 +01:00
trace.h tracing/treewide: Remove second parameter of __assign_str() 2024-05-22 20:14:47 -04:00
user.c netdev_features: convert NETIF_F_LLTX to dev->lltx 2024-09-03 11:36:43 +02:00
user.h net: dsa: update the unicast MAC address when changing conduit 2024-06-10 13:48:06 +01:00