linux/drivers/net/dsa/microchip
Christian Eggers 8098bd69bc net: dsa: microchip: fix race condition
Between queuing the delayed work and finishing the setup of the dsa
ports, the process may sleep in request_module() (via
phy_device_create()) and the queued work may be executed prior to the
switch net devices being registered. In ksz_mib_read_work(), a NULL
dereference will happen within netof_carrier_ok(dp->slave).

Not queuing the delayed work in ksz_init_mib_timer() makes things even
worse because the work will now be queued for immediate execution
(instead of 2000 ms) in ksz_mac_link_down() via
dsa_port_link_register_of().

Call tree:
ksz9477_i2c_probe()
\--ksz9477_switch_register()
   \--ksz_switch_register()
      +--dsa_register_switch()
      |  \--dsa_switch_probe()
      |     \--dsa_tree_setup()
      |        \--dsa_tree_setup_switches()
      |           +--dsa_switch_setup()
      |           |  +--ksz9477_setup()
      |           |  |  \--ksz_init_mib_timer()
      |           |  |     |--/* Start the timer 2 seconds later. */
      |           |  |     \--schedule_delayed_work(&dev->mib_read, msecs_to_jiffies(2000));
      |           |  \--__mdiobus_register()
      |           |     \--mdiobus_scan()
      |           |        \--get_phy_device()
      |           |           +--get_phy_id()
      |           |           \--phy_device_create()
      |           |              |--/* sleeping, ksz_mib_read_work() can be called meanwhile */
      |           |              \--request_module()
      |           |
      |           \--dsa_port_setup()
      |              +--/* Called for non-CPU ports */
      |              +--dsa_slave_create()
      |              |  +--/* Too late, ksz_mib_read_work() may be called beforehand */
      |              |  \--port->slave = ...
      |             ...
      |              +--Called for CPU port */
      |              \--dsa_port_link_register_of()
      |                 \--ksz_mac_link_down()
      |                    +--/* mib_read must be initialized here */
      |                    +--/* work is already scheduled, so it will be executed after 2000 ms */
      |                    \--schedule_delayed_work(&dev->mib_read, 0);
      \-- /* here port->slave is setup properly, scheduling the delayed work should be safe */

Solution:
1. Do not queue (only initialize) delayed work in ksz_init_mib_timer().
2. Only queue delayed work in ksz_mac_link_down() if init is completed.
3. Queue work once in ksz_switch_register(), after dsa_register_switch()
has completed.

Fixes: 7c6ff470aa ("net: dsa: microchip: add MIB counter reading support")
Signed-off-by: Christian Eggers <ceggers@arri.de>
Reviewed-by: Florian Fainelli <f.fainelli@gmail.com>
Reviewed-by: Vladimir Oltean <olteanv@gmail.com>
Signed-off-by: Jakub Kicinski <kuba@kernel.org>
2020-10-12 10:00:24 -07:00
..
Kconfig net: dsa: ksz: Select KSZ protocol tag 2020-03-31 10:04:40 -07:00
Makefile net: dsa: microchip: add KSZ9477 I2C driver 2019-09-12 11:36:12 +01:00
ksz8795.c net: dsa: microchip: ksz8795: really set the correct number of ports 2020-09-16 17:46:09 -07:00
ksz8795_reg.h net: dsa: ksz: Add Microchip KSZ8795 DSA driver 2019-07-30 15:12:50 -07:00
ksz8795_spi.c net: dsa: microchip: Add shared regmap mutex 2019-10-17 15:38:46 -04:00
ksz9477.c net: dsa: microchip: look for phy-mode in port nodes 2020-09-10 12:32:37 -07:00
ksz9477_i2c.c net: dsa: microchip: enable ksz9893 via i2c in the ksz9477 driver 2020-07-01 17:48:47 -07:00
ksz9477_reg.h net: dsa: microchip: Use the correct style for SPDX License Identifier 2019-10-15 14:59:58 -07:00
ksz9477_spi.c net: dsa: microchip: enable module autoprobe 2020-02-07 18:45:12 +01:00
ksz_common.c net: dsa: microchip: fix race condition 2020-10-12 10:00:24 -07:00
ksz_common.h net: dsa: microchip: look for phy-mode in port nodes 2020-09-10 12:32:37 -07:00