Skip to contentSkip to conversation

net: phy: fix locking issue

PATCH In review Heiner Kallweit 3 participants 6 messages Feb 13, 2019 – Feb 14, 2019

Subsystems (3)
lore
Share & tools
lore

Heiner Kallweit

author maintainer
#0 lore raw

cover net: phy: fix locking issue

Russell pointed out that the locking used in phy_is_started() isn't needed and misleading. This locking also contributes to a race fixed with patch 2.

Heiner Kallweit (2):
net: phy: don't use locking in phy_is_started
net: phy: fix potential race in the phylib state machine

 drivers/net/phy/phy.c | 13 +++++++------
 include/linux/phy.h   | 15 +--------------
 2 files changed, 8 insertions(+), 20 deletions(-)
Signature · 1 line
2.20.1
author maintainer
#1 lore raw

PATCH 1/2 net: phy: don't use locking in phy_is_started

Russell suggested to remove the locking from phy_is_started() because the read is atomic anyway and actually the locking may be more misleading.

Fixes 2b3e88ea6528 ("net: phy: improve phy state checking")
---
 drivers/net/phy/phy.c | 11 +++++------
 include/linux/phy.h   | 15 +--------------
 2 files changed, 6 insertions(+), 20 deletions(-)
drivers/net/phy/phy.c +5 -6
index ca5e0c0f018c..602816d70281 100644
@@ -553,7 +553,7 @@ int phy_start_aneg(struct phy_device *phydev)
 	if (err < 0)
 		goto out_unlock;
 
-	if (__phy_is_started(phydev)) {
+	if (phy_is_started(phydev)) {
 		if (phydev->autoneg == AUTONEG_ENABLE) {
 			err = phy_check_link_status(phydev);
 		} else {
@@ -709,7 +709,7 @@ void phy_stop_machine(struct phy_device *phydev)
 	cancel_delayed_work_sync(&phydev->state_queue);
 
 	mutex_lock(&phydev->lock);
-	if (__phy_is_started(phydev))
+	if (phy_is_started(phydev))
 		phydev->state = PHY_UP;
 	mutex_unlock(&phydev->lock);
 }
@@ -839,15 +839,14 @@ EXPORT_SYMBOL(phy_stop_interrupts);
  */
 void phy_stop(struct phy_device *phydev)
 {
-	mutex_lock(&phydev->lock);
-
-	if (!__phy_is_started(phydev)) {
+	if (!phy_is_started(phydev)) {
 		WARN(1, "called from state %s\n",
 		     phy_state_to_str(phydev->state));
-		mutex_unlock(&phydev->lock);
 		return;
 	}
 
+	mutex_lock(&phydev->lock);
+
 	if (phy_interrupt_is_valid(phydev))
 		phy_disable_interrupts(phydev);
 
include/linux/phy.h +1 -14
index ef20aeea10cc..127fcc9c3778 100644
@@ -674,26 +674,13 @@ phy_lookup_setting(int speed, int duplex, const unsigned long *mask,
 size_t phy_speeds(unsigned int *speeds, size_t size,
 		  unsigned long *mask);
 
-static inline bool __phy_is_started(struct phy_device *phydev)
-{
-	WARN_ON(!mutex_is_locked(&phydev->lock));
-
-	return phydev->state >= PHY_UP;
-}
-
 /**
  * phy_is_started - Convenience function to check whether PHY is started
  * @phydev: The phy_device struct
  */
 static inline bool phy_is_started(struct phy_device *phydev)
 {
-	bool started;
-
-	mutex_lock(&phydev->lock);
-	started = __phy_is_started(phydev);
-	mutex_unlock(&phydev->lock);
-
-	return started;
+	return phydev->state >= PHY_UP;
 }
 
 void phy_resolve_aneg_linkmode(struct phy_device *phydev);
Signature · 1 line
2.20.1
Reviewed-by on v1 1/2
#2 lore raw

Re: [PATCH net 1/2] net: phy: don't use locking in phy_is_started

Quoted 7 lines · Heiner Kallweit

Russell suggested to remove the locking from phy_is_started() because the read is atomic anyway and actually the locking may be more misleading.

Fixes 2b3e88ea6528 ("net: phy: improve phy state checking")
Signature · 1 line
Florian
author maintainer
#3 lore raw

PATCH 2/2 net: phy: fix potential race in the phylib state machine

In reply to Heiner Kallweit

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")
---
 drivers/net/phy/phy.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/net/phy/phy.c +2 -0
index 602816d70281..c5675df5fc6f 100644
@@ -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);
 }
 
 /**
Signature · 1 line
2.20.1
Reviewed-by on v1 2/2
#4 lore raw

Re: [PATCH net 2/2] net: phy: fix potential race in the phylib state machine

Quoted 22 lines · Heiner Kallweit

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.

Quoted 5 lines

Fix this by protecting the complete operation in thread 0.

Fixes 2b3e88ea6528 ("net: phy: improve phy state checking")
Signature · 1 line
Florian
maintainer
#5 lore raw
In reply to Heiner Kallweit

From: Heiner Kallweit <[email protected]>
Date: Wed, 13 Feb 2019 20:10:36 +0100

Quoted 3 lines

Russell pointed out that the locking used in phy_is_started() isn't needed and misleading. This locking also contributes to a race fixed with patch 2.

Series applied and queued up for -stable, thanks.