net: phy: fix potential race in the phylib state machine
authorHeiner Kallweit <hkallweit1@gmail.com>
Wed, 13 Feb 2019 19:12:54 +0000 (20:12 +0100)
committerDavid S. Miller <davem@davemloft.net>
Thu, 14 Feb 2019 17:04:55 +0000 (12:04 -0500)
Russell reported the following race in the phylib state machine
(quoting from his mail):

if (phy_polling_mode(phydev) && phy_is_started(phydev))
phy_queue_state_machine(phydev, PHY_STATE_TIME);

state = PHY_UP
thread 0 thread 1
phy_disconnect()
+-phy_is_started()
phy_is_started()                |
`-phy_stop()
  +-phydev->state = PHY_HALTED
  `-phy_stop_machine()
    `-cancel_delayed_work_sync()
phy_queue_state_machine()
`-mod_delayed_work()

At this point, the phydev->state_queue() has been added back onto the
system workqueue despite phy_stop_machine() having been called and
cancel_delayed_work_sync() called on it.

Fix this by protecting the complete operation in thread 0.

Fixes: 2b3e88ea6528 ("net: phy: improve phy state checking")
Reported-by: Russell King - ARM Linux admin <linux@armlinux.org.uk>
Signed-off-by: Heiner Kallweit <hkallweit1@gmail.com>
Reviewed-by: Florian Fainelli <f.fainelli@gmail.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
drivers/net/phy/phy.c

index 602816d70281fd0c088b9826f3a8a48b675a0900..c5675df5fc6f8d4a377da8f08f6811bcffc36bf0 100644 (file)
@@ -985,8 +985,10 @@ void phy_state_machine(struct work_struct *work)
         * state machine would be pointless and possibly error prone when
         * called from phy_disconnect() synchronously.
         */
+       mutex_lock(&phydev->lock);
        if (phy_polling_mode(phydev) && phy_is_started(phydev))
                phy_queue_state_machine(phydev, PHY_STATE_TIME);
+       mutex_unlock(&phydev->lock);
 }
 
 /**