Skip to contentSkip to conversation

net: phylib: fix phy_read*_poll_timeout()

PATCH Applied Russell King (Oracle) 4 participants 9 messages Jun 1, 2023 – Jun 3, 2023

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

PATCH 1/1 net: phylib: fix phy_read*_poll_timeout()

Dan Carpenter reported a signedness bug in genphy_loopback(). Andrew reports that:

"It is common to get this wrong in general with PHY drivers. Dan regularly posts fixes like this soon after a PHY driver patch it merged. I really wish we could somehow get the compiler to warn when the result from phy_read() is stored into a unsigned type. It would save Dan a lot of work."

Let's make phy_read*_poll_timeout() immune to further issues when "val" is an unsigned type by storing the read function's result in a signed int as well as "val", and using the signed variable both to check for an error and for propagating that error to the caller.

The advantage of this method is we don't change where the cast from the signed return code to the user's variable occurs - so users will see no change.

Previously Heiner changed phy_read_poll_timeout() to check for an error before evaluating the user supplied condition, but didn't update phy_read_mmd_poll_timeout(). Make that change there too.

---
 include/linux/phy.h | 16 ++++++++++------
 1 file changed, 10 insertions(+), 6 deletions(-)
include/linux/phy.h +10 -6
index 7addde5d14c0..11c1e91563d4 100644
@@ -1206,10 +1206,12 @@ static inline int phy_read(struct phy_device *phydev, u32 regnum)
 #define phy_read_poll_timeout(phydev, regnum, val, cond, sleep_us, \
 				timeout_us, sleep_before_read) \
 ({ \
-	int __ret = read_poll_timeout(phy_read, val, val < 0 || (cond), \
+	int __ret, __val; \
+	__ret = read_poll_timeout(__val = phy_read, val, \
+				  __val < 0 || (cond), \
 		sleep_us, timeout_us, sleep_before_read, phydev, regnum); \
-	if (val < 0) \
-		__ret = val; \
+	if (__val < 0) \
+		__ret = __val; \
 	if (__ret) \
 		phydev_err(phydev, "%s failed: %d\n", __func__, __ret); \
 	__ret; \
@@ -1302,11 +1304,13 @@ int phy_read_mmd(struct phy_device *phydev, int devad, u32 regnum);
 #define phy_read_mmd_poll_timeout(phydev, devaddr, regnum, val, cond, \
 				  sleep_us, timeout_us, sleep_before_read) \
 ({ \
-	int __ret = read_poll_timeout(phy_read_mmd, val, (cond) || val < 0, \
+	int __ret, __val; \
+	__ret = read_poll_timeout(__val = phy_read_mmd, val, \
+				  __val < 0 || (cond), \
 				  sleep_us, timeout_us, sleep_before_read, \
 				  phydev, devaddr, regnum); \
-	if (val <  0) \
-		__ret = val; \
+	if (__val < 0) \
+		__ret = __val; \
 	if (__ret) \
 		phydev_err(phydev, "%s failed: %d\n", __func__, __ret); \
 	__ret; \
Signature · 1 line
2.30.2
maintainer
#1 lore raw

include/linux/phy.h static inline int phy_read(struct phy_device *phydev, u32 regnum) · line 1210

Quoted 1 line · On Thu, 01 Jun 2023 16:48:12 +0100 Russell King (Oracle) wrote:
include/linux/phy.h +1 -0
+	__ret = read_poll_timeout(__val = phy_read, val, \
                                                    ^^^

Is this not __val on purpose?

maintainer
#2 lore raw
Quoted 4 lines · On Thu, 1 Jun 2023 21:33:45 -0700 Jakub Kicinski wrote:

include/linux/phy.h static inline int phy_read(struct phy_device *phydev, u32 regnum) · line 1210

Quoted 1 line · On Thu, 01 Jun 2023 16:48:12 +0100 Russell King (Oracle) wrote:
include/linux/phy.h +1 -0
+	__ret = read_poll_timeout(__val = phy_read, val, \  
                                                    ^^^

Is this not __val on purpose?

Yes it is :) All this to save the single line of assignment after the read_poll_timeout() "call" ?

#3 lore raw
Quoted 8 lines · Jakub Kicinski
Quoted 4 lines · On Thu, 1 Jun 2023 21:33:45 -0700 Jakub Kicinski wrote:

include/linux/phy.h static inline int phy_read(struct phy_device *phydev, u32 regnum) · line 1210

Quoted 1 line · On Thu, 01 Jun 2023 16:48:12 +0100 Russell King (Oracle) wrote:
include/linux/phy.h +1 -0
+	__ret = read_poll_timeout(__val = phy_read, val, \  
                                                    ^^^

Is this not __val on purpose?

Yes it is :) All this to save the single line of assignment after the read_poll_timeout() "call" ?

Okay, so it seems you don't like it. We can't fix it then, and we'll have to go with the BUILD_BUG_ON() forcing all users to use a signed varable (which better be larger than a s8 so negative errnos can fit) or we just rely on Dan to report the problems.

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!
maintainer
#4 lore raw
Quoted 7 lines · On Fri, 2 Jun 2023 09:53:09 +0100 Russell King (Oracle) wrote:
Quoted 2 lines

Yes it is :) All this to save the single line of assignment after the read_poll_timeout() "call" ?

Okay, so it seems you don't like it. We can't fix it then, and we'll have to go with the BUILD_BUG_ON() forcing all users to use a signed varable (which better be larger than a s8 so negative errnos can fit) or we just rely on Dan to report the problems.

Wait, did the version I proposed not work?

https://lore.kernel.org/all/[email protected]/

I just think the assignment inside the first argument is unnecessarily unreadable. Maybe it's just me.

#5 lore raw
Quoted 4 lines · Jakub Kicinski
Quoted 3 lines · On Fri, 2 Jun 2023 09:53:09 +0100 Russell King (Oracle) wrote:
Quoted 2 lines

Yes it is :) All this to save the single line of assignment after the read_poll_timeout() "call" ?

Quoted 8 lines
Quoted 4 lines

Okay, so it seems you don't like it. We can't fix it then, and we'll have to go with the BUILD_BUG_ON() forcing all users to use a signed varable (which better be larger than a s8 so negative errnos can fit) or we just rely on Dan to report the problems.

Wait, did the version I proposed not work?

https://lore.kernel.org/all/[email protected]/

If we're into the business of throwing web URLs at each other for messages we've already read, here's my one for you which contains the explanation why your one is broken, and proposing my solution.

https://lore.kernel.org/all/[email protected]/

To see exactly why yours is broken, see the paragraph starting "The elephant in the room..."

If it needs yet more explanation, which clearly it does, then let's look at what genphy_loopback is doing:

ret = phy_read_poll_timeout(phydev, MII_BMSR, val,
val & BMSR_LSTATUS,
5000, 500000, true);

Now, with your supposed "fix" of:

+ int __ret, __val; \
+ \
+ __ret = read_poll_timeout(phy_read, __val, __val < 0 || (cond), \
sleep_us, timeout_us, sleep_before_read, phydev, regnum); \

This ends up being:

	int __ret, __val;

	__ret = read_poll_timeout(phy_read, __val, __val < 0 || (val & BMSR_LSTATUS),

sleep_us, timeout_us, sleep_before_read, phydev, regnum);

and that expands to something that does this:

	__val = phy_read(phydev, regnum);
	if (__val < 0 || (val & BMSR_LSTATUS))
		break;

Can you spot the bug yet? Where does "val" for the test "val & BMSR_LSTATUS" come from?

A bigger hint. With the existing code, this would have been:

	val = phy_read(phydev, regnum);
	if (val < 0 || (val & BMSR_LSTATUS))
		break;

See the difference? val & BMSR_LSTATUS is checking the value that was returned from phy_read() here, but in yours, it's checking an uninitialised variable.

With my proposal, this becomes:

	val = __val = phy_read(phydev, regnum);
	if (__val < 0 || (val & BMSR_LSTATUS))
		break;

where "val" is whatever type the user chose, which has absolutely _no_ bearing what so ever on whether the test for __val < 0 can be correctly evaluated, and makes that test totally independent of whatever type the user chose.

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 65 lines · Russell King (Oracle)
Quoted 4 lines · Jakub Kicinski
Quoted 3 lines · On Fri, 2 Jun 2023 09:53:09 +0100 Russell King (Oracle) wrote:
Quoted 2 lines

Yes it is :) All this to save the single line of assignment after the read_poll_timeout() "call" ?

Quoted 8 lines
Quoted 4 lines

Okay, so it seems you don't like it. We can't fix it then, and we'll have to go with the BUILD_BUG_ON() forcing all users to use a signed varable (which better be larger than a s8 so negative errnos can fit) or we just rely on Dan to report the problems.

Wait, did the version I proposed not work?

https://lore.kernel.org/all/[email protected]/

If we're into the business of throwing web URLs at each other for messages we've already read, here's my one for you which contains the explanation why your one is broken, and proposing my solution.

https://lore.kernel.org/all/[email protected]/

To see exactly why yours is broken, see the paragraph starting "The elephant in the room..."

If it needs yet more explanation, which clearly it does, then let's look at what genphy_loopback is doing:

ret = phy_read_poll_timeout(phydev, MII_BMSR, val,
val & BMSR_LSTATUS,
5000, 500000, true);

Now, with your supposed "fix" of:

include/linux/phy.h +3 -0
+	int __ret, __val;						\
+									\
+	__ret = read_poll_timeout(phy_read, __val, __val < 0 || (cond),	\
 		sleep_us, timeout_us, sleep_before_read, phydev, regnum); \

This ends up being:

	int __ret, __val;

	__ret = read_poll_timeout(phy_read, __val, __val < 0 || (val & BMSR_LSTATUS),

sleep_us, timeout_us, sleep_before_read, phydev, regnum);

and that expands to something that does this:

	__val = phy_read(phydev, regnum);
	if (__val < 0 || (val & BMSR_LSTATUS))
		break;

Can you spot the bug yet? Where does "val" for the test "val & BMSR_LSTATUS" come from?

A bigger hint. With the existing code, this would have been:

	val = phy_read(phydev, regnum);
	if (val < 0 || (val & BMSR_LSTATUS))
		break;

See the difference? val & BMSR_LSTATUS is checking the value that was returned from phy_read() here, but in yours, it's checking an uninitialised variable.

With my proposal, this becomes:

Quoted 8 lines
	val = __val = phy_read(phydev, regnum);
	if (__val < 0 || (val & BMSR_LSTATUS))
		break;

where "val" is whatever type the user chose, which has absolutely _no_ bearing what so ever on whether the test for __val < 0 can be correctly evaluated, and makes that test totally independent of whatever type the user chose.

If you don't like my solution, then I suppose another possibility would be:

#define __phy_poll_read(phydev, regnum, val) \
	({ \
		int __err; \
		__err = phy_read(phydev, regnum); \
		if (__err >= 0) \
			val = __err; \
		__err; \
	})
#define phy_read_poll_timeout(phydev, regnum, val, cond, sleep_us, \
                                timeout_us, sleep_before_read) \
({ \
	int __ret, __err; \
	__ret = read_poll_timeout(__phy_poll_read, __err, \
				  __err < 0 || (cond), \
		sleep_us, timeout_us, sleep_before_read, phydev, regnum, val); \
	if (__err < 0) \
		__ret = __err; \

...

but that brings with it the possibility of using an uninitialised "val" (e.g. if phy_read() returns an error on the first iteration.) and is way more horrid and even less easy to understand.

Remember that we default to *not* warning about uninitialised variables when building the kernel, so this won't produce a warning - which I guess is probably why you didn't notice that your suggestion left "val" uninitialised.

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!
maintainer
#7 lore raw
Quoted 6 lines · On Fri, 2 Jun 2023 17:34:31 +0100 Russell King (Oracle) wrote:
Quoted 5 lines · Russell King (Oracle)
Quoted 3 lines · Jakub Kicinski

Wait, did the version I proposed not work?

https://lore.kernel.org/all/[email protected]/

Quoted 8 lines
Quoted 8 lines

If we're into the business of throwing web URLs at each other for messages we've already read, here's my one for you which contains the explanation why your one is broken, and proposing my solution.

https://lore.kernel.org/all/[email protected]/

To see exactly why yours is broken, see the paragraph starting "The elephant in the room..."

Ah, yes, sorry, I'll admit I didn't get what you mean by the elephant paragraph when I read that.

Quoted 23 lines

If you don't like my solution, then I suppose another possibility would be:

#define __phy_poll_read(phydev, regnum, val) \
	({ \
		int __err; \
		__err = phy_read(phydev, regnum); \
		if (__err >= 0) \
			val = __err; \
		__err; \
	})
#define phy_read_poll_timeout(phydev, regnum, val, cond, sleep_us, \
                                timeout_us, sleep_before_read) \
({ \
	int __ret, __err; \
	__ret = read_poll_timeout(__phy_poll_read, __err, \
				  __err < 0 || (cond), \
		sleep_us, timeout_us, sleep_before_read, phydev, regnum, val); \
	if (__err < 0) \
		__ret = __err; \

...

Quoted 8 lines

but that brings with it the possibility of using an uninitialised "val" (e.g. if phy_read() returns an error on the first iteration.) and is way more horrid and even less easy to understand.

Remember that we default to *not* warning about uninitialised variables when building the kernel, so this won't produce a warning - which I guess is probably why you didn't notice that your suggestion left "val" uninitialised.

Right :( Let's keep the patch as is.

#8 lore raw
In reply to Russell King (Oracle)
This patch was applied to netdev/net-next.git (main) by Jakub Kicinski <[email protected]>:

Hello:

This patch was applied to netdev/net-next.git (main) by Jakub Kicinski <[email protected]>:

Quoted 10 lines · On Thu, 01 Jun 2023 16:48:12 +0100 you wrote:

Dan Carpenter reported a signedness bug in genphy_loopback(). Andrew reports that:

"It is common to get this wrong in general with PHY drivers. Dan regularly posts fixes like this soon after a PHY driver patch it merged. I really wish we could somehow get the compiler to warn when the result from phy_read() is stored into a unsigned type. It would save Dan a lot of work."

[...]

Here is the summary with links:
- [net-next] net: phylib: fix phy_read*_poll_timeout()

    https://git.kernel.org/netdev/net-next/c/4ec732951702

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html