Skip to contentSkip to conversation

net: phy: fix phy_uses_state_machine()

PATCH In review Russell King (Oracle) 3 participants 12 messages Aug 31, 2025 – Sep 1, 2025

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 net: phy: fix phy_uses_state_machine()

phy_uses_state_machine() is called from the resume path (see mdio_bus_phy_resume()) which will be called for all devices whether they are connected to a network device or not.

phydev->phy_link_change is initialised by phy_attach_direct(), and overridden by phylink. This means that a never-connected PHY will have phydev->phy_link_change set to NULL, which causes phy_uses_state_machine() to return true. This is incorrect.

Fix the case where phydev->phy_link_change is NULL.

Fixes fc75ea20ffb4 ("net: phy: allow MDIO bus PM ops to start/stop state machine for phylink-controlled PHY")

---
The provided Link: rather than Closes: is because there were two issues identified in that thread, and this patch only addresses one of them. Therefore, it is not correct to mark that issue closed.

Xu Yang reported this fixed the problem for him, and it is an oversight in the phy_uses_state_machine() test.

 drivers/net/phy/phy_device.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/net/phy/phy_device.c +1 -1
index 7556aa3dd7ee..e6a673faabe6 100644
@@ -288,7 +288,7 @@ static bool phy_uses_state_machine(struct phy_device *phydev)
 		return phydev->attached_dev && phydev->adjust_link;
 
 	/* phydev->phy_link_change is implicitly phylink_phy_change() */
-	return true;
+	return !!phydev->phy_link_change;
 }
 
 static bool mdio_bus_phy_may_suspend(struct phy_device *phydev)
Signature · 1 line
2.47.2
Reviewed-by on v1 1/1
#1 lore raw
Quoted 31 lines · Russell King (Oracle)

phy_uses_state_machine() is called from the resume path (see mdio_bus_phy_resume()) which will be called for all devices whether they are connected to a network device or not.

phydev->phy_link_change is initialised by phy_attach_direct(), and overridden by phylink. This means that a never-connected PHY will have phydev->phy_link_change set to NULL, which causes phy_uses_state_machine() to return true. This is incorrect.

Fix the case where phydev->phy_link_change is NULL.

Fixes fc75ea20ffb4 ("net: phy: allow MDIO bus PM ops to start/stop state machine for phylink-controlled PHY")

---
The provided Link: rather than Closes: is because there were two issues identified in that thread, and this patch only addresses one of them. Therefore, it is not correct to mark that issue closed.

Xu Yang reported this fixed the problem for him, and it is an oversight in the phy_uses_state_machine() test.

 drivers/net/phy/phy_device.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/net/phy/phy_device.c +0 -0
index 7556aa3dd7ee..e6a673faabe6 100644

drivers/net/phy/phy_device.c static bool phy_uses_state_machine(struct phy_device *phydev) · line 290

Quoted 3 lines
drivers/net/phy/phy_device.c +0 -0
@@ -288,7 +288,7 @@ static bool phy_uses_state_machine(struct phy_device *phydev)
 		return phydev->attached_dev && phydev->adjust_link;
 
 	/* phydev->phy_link_change is implicitly phylink_phy_change() */

We should either update this comment to clarify that the function pointer is only set to phylink_phy_change() if set at all, or do away with the comment entirely. Otherwise, it doesn't make much sense.

With that:

Quoted 8 lines
drivers/net/phy/phy_device.c +1 -1
-	return true;
+	return !!phydev->phy_link_change;
 }
 
 static bool mdio_bus_phy_may_suspend(struct phy_device *phydev)
Signature · 1 line
2.47.2
#2 lore raw
In reply to Russell King (Oracle)
Quoted 4 lines · Russell King (Oracle)

phydev->phy_link_change is initialised by phy_attach_direct(), and overridden by phylink. This means that a never-connected PHY will have phydev->phy_link_change set to NULL, which causes phy_uses_state_machine() to return true. This is incorrect.

Another nitpick regarding phrasing here: the never-connected PHY doesn't _cause_ phy_uses_state_machine() to return true. It returns true _in spite_ of the PHY never being connected: the non-NULL quality of phydev->phy_link_change is not something that phy_uses_state_machine() tests for.

#3 lore raw
Quoted 11 lines · Vladimir Oltean
Quoted 4 lines · Russell King (Oracle)

phydev->phy_link_change is initialised by phy_attach_direct(), and overridden by phylink. This means that a never-connected PHY will have phydev->phy_link_change set to NULL, which causes phy_uses_state_machine() to return true. This is incorrect.

Another nitpick regarding phrasing here: the never-connected PHY doesn't _cause_ phy_uses_state_machine() to return true. It returns true _in spite_ of the PHY never being connected: the non-NULL quality of phydev->phy_link_change is not something that phy_uses_state_machine() tests for.

No. What I'm saying is that if phydev->phy_link_change is set to NULL, _this_ causes phy_uses_state_machine() to return true and that behaviour incorrect.

The first part is describing _when_ phydev->phy_link_change is set to NULL.

It is not saying that a never-connected PHY directly causes phy_uses_state_machine() to return true.

I think my phrasing of this is totally fine, even re-reading it now.

Signature · 2 lines
RMK's Patch system: https://www.armlinux.org.uk/developer/patches/
FTTP is here! 80Mbps down 10Mbps up. Decent connectivity at last!
#4 lore raw
Quoted 17 lines · Russell King (Oracle)
Quoted 11 lines · Vladimir Oltean
Quoted 4 lines · Russell King (Oracle)

phydev->phy_link_change is initialised by phy_attach_direct(), and overridden by phylink. This means that a never-connected PHY will have phydev->phy_link_change set to NULL, which causes phy_uses_state_machine() to return true. This is incorrect.

Another nitpick regarding phrasing here: the never-connected PHY doesn't _cause_ phy_uses_state_machine() to return true. It returns true _in spite_ of the PHY never being connected: the non-NULL quality of phydev->phy_link_change is not something that phy_uses_state_machine() tests for.

No. What I'm saying is that if phydev->phy_link_change is set to NULL, _this_ causes phy_uses_state_machine() to return true and that behaviour incorrect.

Quoted 11 lines

The first part is describing _when_ phydev->phy_link_change is set to NULL.

It is not saying that a never-connected PHY directly causes phy_uses_state_machine() to return true.

I think my phrasing of this is totally fine, even re-reading it now.

Signature · 2 lines
RMK's Patch system: https://www.armlinux.org.uk/developer/patches/
FTTP is here! 80Mbps down 10Mbps up. Decent connectivity at last!

I think the language is sufficiently flexible for us to find a phrasing that avoids all ambiguity. What about:

"It was assumed the only two valid values for phydev->phy_link_change are phy_link_change() and phylink_phy_change(), thus phy_uses_state_machine() was oversimplified to only compare with one of these values. There is a third possible value (NULL), meaning that the PHY is unconnected, and does not use the state machine. This logic misinterprets this case as phy_uses_state_machine() == true, but in reality it should return false."

#5 lore raw
Quoted 33 lines · Vladimir Oltean
Quoted 17 lines · Russell King (Oracle)
Quoted 11 lines · Vladimir Oltean
Quoted 4 lines · Russell King (Oracle)

phydev->phy_link_change is initialised by phy_attach_direct(), and overridden by phylink. This means that a never-connected PHY will have phydev->phy_link_change set to NULL, which causes phy_uses_state_machine() to return true. This is incorrect.

Another nitpick regarding phrasing here: the never-connected PHY doesn't _cause_ phy_uses_state_machine() to return true. It returns true _in spite_ of the PHY never being connected: the non-NULL quality of phydev->phy_link_change is not something that phy_uses_state_machine() tests for.

No. What I'm saying is that if phydev->phy_link_change is set to NULL, _this_ causes phy_uses_state_machine() to return true and that behaviour incorrect.

Quoted 11 lines

The first part is describing _when_ phydev->phy_link_change is set to NULL.

It is not saying that a never-connected PHY directly causes phy_uses_state_machine() to return true.

I think my phrasing of this is totally fine, even re-reading it now.

Signature · 2 lines
RMK's Patch system: https://www.armlinux.org.uk/developer/patches/
FTTP is here! 80Mbps down 10Mbps up. Decent connectivity at last!

I think the language is sufficiently flexible for us to find a phrasing that avoids all ambiguity. What about:

Quoted 6 lines

"It was assumed the only two valid values for phydev->phy_link_change are phy_link_change() and phylink_phy_change(), thus phy_uses_state_machine() was oversimplified to only compare with one of these values. There is a third possible value (NULL), meaning that the PHY is unconnected, and does not use the state machine. This logic misinterprets this case as phy_uses_state_machine() == true, but in reality it should return false."

Well, having spent considerable time writing and rewriting the damn commit message, this is what I'm now using, which I think covers the problem in sufficient detail.

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

net: phy: fix phy_uses_state_machine()

The blamed commit changed the conditions which phylib uses to stop and start the state machine in the suspend and resume paths, and while improving it, has caused two issues.

The original code used this test:

        phydev->attached_dev && phydev->adjust_link

and if true, the paths would handle the PHY state machine. This test evaluates true for normal drivers that are using phylib directly while the PHY is attached to the network device, but false in all other cases, which include the following cases:

- when the PHY has never been attached to a network device.
- when the PHY has been detached from a network device (as phy_detach() sets phydev->attached_dev to NULL, phy_disconnect() calls phy_detach() and additionally sets phydev->adjust_link NULL.)
- when phylink is using the driver (as phydev->adjust_link is NULL.)

Only the third case was incorrect, and the blamed commit attempted to fix this by changing this test to (simplified for brevity, see phy_uses_state_machine()):

        phydev->phy_link_change == phy_link_change ?
                phydev->attached_dev && phydev->adjust_link : true

However, this also incorrectly evaluates true in the first two cases.

Fix the first case by ensuring that phy_uses_state_machine() returns false when phydev->phy_link_change is NULL.

Fix the second case by ensuring that phydev->phy_link_change is set to NULL when phy_detach() is called.
<<<<<<

Signature · 2 lines
RMK's Patch system: https://www.armlinux.org.uk/developer/patches/
FTTP is here! 80Mbps down 10Mbps up. Decent connectivity at last!
#6 lore raw
Quoted 33 lines · Russell King (Oracle)

Well, having spent considerable time writing and rewriting the damn commit message, this is what I'm now using, which I think covers the problem in sufficient detail.

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

net: phy: fix phy_uses_state_machine()

The blamed commit changed the conditions which phylib uses to stop and start the state machine in the suspend and resume paths, and while improving it, has caused two issues.

The original code used this test:

        phydev->attached_dev && phydev->adjust_link

and if true, the paths would handle the PHY state machine. This test evaluates true for normal drivers that are using phylib directly while the PHY is attached to the network device, but false in all other cases, which include the following cases:

- when the PHY has never been attached to a network device.
- when the PHY has been detached from a network device (as phy_detach() sets phydev->attached_dev to NULL, phy_disconnect() calls phy_detach() and additionally sets phydev->adjust_link NULL.)
- when phylink is using the driver (as phydev->adjust_link is NULL.)

Only the third case was incorrect, and the blamed commit attempted to fix this by changing this test to (simplified for brevity, see phy_uses_state_machine()):

        phydev->phy_link_change == phy_link_change ?
                phydev->attached_dev && phydev->adjust_link : true
Quoted 8 lines

However, this also incorrectly evaluates true in the first two cases.

Fix the first case by ensuring that phy_uses_state_machine() returns false when phydev->phy_link_change is NULL.

Fix the second case by ensuring that phydev->phy_link_change is set to NULL when phy_detach() is called.
<<<<<<

The new explanation and the placement of the function pointer clearing make sense, thanks. Maybe add one last sentence at the end: "This covers both the phylink_bringup_phy() error path, as well as the normal phylink_disconnect_phy() path."

#7 lore raw
Quoted 39 lines · Vladimir Oltean
Quoted 38 lines · Russell King (Oracle)

Well, having spent considerable time writing and rewriting the damn commit message, this is what I'm now using, which I think covers the problem in sufficient detail.

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

net: phy: fix phy_uses_state_machine()

The blamed commit changed the conditions which phylib uses to stop and start the state machine in the suspend and resume paths, and while improving it, has caused two issues.

The original code used this test:

        phydev->attached_dev && phydev->adjust_link

and if true, the paths would handle the PHY state machine. This test evaluates true for normal drivers that are using phylib directly while the PHY is attached to the network device, but false in all other cases, which include the following cases:

- when the PHY has never been attached to a network device.
- when the PHY has been detached from a network device (as phy_detach() sets phydev->attached_dev to NULL, phy_disconnect() calls phy_detach() and additionally sets phydev->adjust_link NULL.)
- when phylink is using the driver (as phydev->adjust_link is NULL.)

Only the third case was incorrect, and the blamed commit attempted to fix this by changing this test to (simplified for brevity, see phy_uses_state_machine()):

        phydev->phy_link_change == phy_link_change ?
                phydev->attached_dev && phydev->adjust_link : true

However, this also incorrectly evaluates true in the first two cases.

Fix the first case by ensuring that phy_uses_state_machine() returns false when phydev->phy_link_change is NULL.

Quoted 8 lines
Quoted 3 lines

Fix the second case by ensuring that phydev->phy_link_change is set to NULL when phy_detach() is called.
<<<<<<

The new explanation and the placement of the function pointer clearing make sense, thanks. Maybe add one last sentence at the end: "This covers both the phylink_bringup_phy() error path, as well as the normal phylink_disconnect_phy() path."

Is it really necessary to add that level of detail? phy_attach() sets phydev->phy_link_change, so it's natural that phy_detach() should undo that action, especially as phy_detach() is phy_attach()'s complement.

Signature · 2 lines
RMK's Patch system: https://www.armlinux.org.uk/developer/patches/
FTTP is here! 80Mbps down 10Mbps up. Decent connectivity at last!
#8 lore raw
Quoted 9 lines · Russell King (Oracle)
Quoted 4 lines · Vladimir Oltean

The new explanation and the placement of the function pointer clearing make sense, thanks. Maybe add one last sentence at the end: "This covers both the phylink_bringup_phy() error path, as well as the normal phylink_disconnect_phy() path."

Is it really necessary to add that level of detail? phy_attach() sets phydev->phy_link_change, so it's natural that phy_detach() should undo that action, especially as phy_detach() is phy_attach()'s complement.

Well, it shows for future reference the code paths you've considered when establishing the placement of the code. Your call whether you want to include this sentence or not - and it looks like you don't want to.

#9 lore raw
In reply to Russell King (Oracle)
Quoted 15 lines · Russell King (Oracle)

phy_uses_state_machine() is called from the resume path (see mdio_bus_phy_resume()) which will be called for all devices whether they are connected to a network device or not.

phydev->phy_link_change is initialised by phy_attach_direct(), and overridden by phylink. This means that a never-connected PHY will have phydev->phy_link_change set to NULL, which causes phy_uses_state_machine() to return true. This is incorrect.

Fix the case where phydev->phy_link_change is NULL.

Fixes fc75ea20ffb4 ("net: phy: allow MDIO bus PM ops to start/stop state machine for phylink-controlled PHY")
Quoted 7 lines

---
The provided Link: rather than Closes: is because there were two issues identified in that thread, and this patch only addresses one of them. Therefore, it is not correct to mark that issue closed.

Xu Yang reported this fixed the problem for him, and it is an oversight in the phy_uses_state_machine() test.

While looking at this after Vladimir's comments, I've realised that phy_uses_state_machine() will also return true when a PHY has been attached and detached by phylink - phydev->phy_link_change remains set to phylink_phy_change after it has been detached. So, there will definitely be a v2 for this.

Signature · 2 lines
RMK's Patch system: https://www.armlinux.org.uk/developer/patches/
FTTP is here! 80Mbps down 10Mbps up. Decent connectivity at last!
#10 lore raw
Quoted 21 lines · Russell King (Oracle)
Quoted 20 lines · Russell King (Oracle)

phy_uses_state_machine() is called from the resume path (see mdio_bus_phy_resume()) which will be called for all devices whether they are connected to a network device or not.

phydev->phy_link_change is initialised by phy_attach_direct(), and overridden by phylink. This means that a never-connected PHY will have phydev->phy_link_change set to NULL, which causes phy_uses_state_machine() to return true. This is incorrect.

Fix the case where phydev->phy_link_change is NULL.

Fixes fc75ea20ffb4 ("net: phy: allow MDIO bus PM ops to start/stop state machine for phylink-controlled PHY")

---
The provided Link: rather than Closes: is because there were two issues identified in that thread, and this patch only addresses one of them. Therefore, it is not correct to mark that issue closed.

Quoted 12 lines
Quoted 2 lines

Xu Yang reported this fixed the problem for him, and it is an oversight in the phy_uses_state_machine() test.

While looking at this after Vladimir's comments, I've realised that phy_uses_state_machine() will also return true when a PHY has been attached and detached by phylink - phydev->phy_link_change remains set to phylink_phy_change after it has been detached. So, there will definitely be a v2 for this.

Signature · 2 lines
RMK's Patch system: https://www.armlinux.org.uk/developer/patches/
FTTP is here! 80Mbps down 10Mbps up. Decent connectivity at last!

Good point. Do you plan to modify phy_disconnect() to set phydev->phy_link_change = NULL?

#11 lore raw
Quoted 31 lines · Vladimir Oltean
Quoted 30 lines · Russell King (Oracle)
Quoted 15 lines · Russell King (Oracle)

phy_uses_state_machine() is called from the resume path (see mdio_bus_phy_resume()) which will be called for all devices whether they are connected to a network device or not.

phydev->phy_link_change is initialised by phy_attach_direct(), and overridden by phylink. This means that a never-connected PHY will have phydev->phy_link_change set to NULL, which causes phy_uses_state_machine() to return true. This is incorrect.

Fix the case where phydev->phy_link_change is NULL.

Fixes fc75ea20ffb4 ("net: phy: allow MDIO bus PM ops to start/stop state machine for phylink-controlled PHY")
Quoted 7 lines

---
The provided Link: rather than Closes: is because there were two issues identified in that thread, and this patch only addresses one of them. Therefore, it is not correct to mark that issue closed.

Xu Yang reported this fixed the problem for him, and it is an oversight in the phy_uses_state_machine() test.

While looking at this after Vladimir's comments, I've realised that phy_uses_state_machine() will also return true when a PHY has been attached and detached by phylink - phydev->phy_link_change remains set to phylink_phy_change after it has been detached. So, there will definitely be a v2 for this.

Quoted 6 lines
Quoted 3 lines
Signature · 2 lines
RMK's Patch system: https://www.armlinux.org.uk/developer/patches/
FTTP is here! 80Mbps down 10Mbps up. Decent connectivity at last!

Good point. Do you plan to modify phy_disconnect() to set phydev->phy_link_change = NULL?

No, I've put it in phy_detach() because this sequence:

	phy_attach_direct()
	phylink_bringup_phy()
		phydev->phy_link_change set
		error occurs
	phy_detach()

would result in phydev->phy_link_change remaining set if it was only cleared in phy_disconnect().

Signature · 2 lines
RMK's Patch system: https://www.armlinux.org.uk/developer/patches/
FTTP is here! 80Mbps down 10Mbps up. Decent connectivity at last!