Skip to content

Commit 100efd5

Browse files
ceggers-arrigregkh
authored andcommitted
net: dsa: microchip: fix race condition
[ Upstream commit 8098bd6 ] 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: 7c6ff47 ("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> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent e1a2e59 commit 100efd5

1 file changed

Lines changed: 9 additions & 7 deletions

File tree

drivers/net/dsa/microchip/ksz_common.c

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -103,14 +103,8 @@ void ksz_init_mib_timer(struct ksz_device *dev)
103103

104104
INIT_DELAYED_WORK(&dev->mib_read, ksz_mib_read_work);
105105

106-
/* Read MIB counters every 30 seconds to avoid overflow. */
107-
dev->mib_read_interval = msecs_to_jiffies(30000);
108-
109106
for (i = 0; i < dev->mib_port_cnt; i++)
110107
dev->dev_ops->port_init_cnt(dev, i);
111-
112-
/* Start the timer 2 seconds later. */
113-
schedule_delayed_work(&dev->mib_read, msecs_to_jiffies(2000));
114108
}
115109
EXPORT_SYMBOL_GPL(ksz_init_mib_timer);
116110

@@ -143,7 +137,9 @@ void ksz_mac_link_down(struct dsa_switch *ds, int port, unsigned int mode,
143137

144138
/* Read all MIB counters when the link is going down. */
145139
p->read = true;
146-
schedule_delayed_work(&dev->mib_read, 0);
140+
/* timer started */
141+
if (dev->mib_read_interval)
142+
schedule_delayed_work(&dev->mib_read, 0);
147143
}
148144
EXPORT_SYMBOL_GPL(ksz_mac_link_down);
149145

@@ -450,6 +446,12 @@ int ksz_switch_register(struct ksz_device *dev,
450446
return ret;
451447
}
452448

449+
/* Read MIB counters every 30 seconds to avoid overflow. */
450+
dev->mib_read_interval = msecs_to_jiffies(30000);
451+
452+
/* Start the MIB timer. */
453+
schedule_delayed_work(&dev->mib_read, 0);
454+
453455
return 0;
454456
}
455457
EXPORT_SYMBOL(ksz_switch_register);

0 commit comments

Comments
 (0)