net: phylib: fix phy_read*_poll_timeout()
Subsystems (3)
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
@@ -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
Jakub Kicinski
maintainerinclude/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?
Jakub Kicinski
maintainerQuoted 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
__valon purpose?
Yes it is :) All this to save the single line of assignment after the read_poll_timeout() "call" ?
Russell King (Oracle)
reviewerQuoted 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
__valon 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!
Jakub Kicinski
maintainerQuoted 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.
Russell King (Oracle)
reviewerQuoted 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?
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!
Russell King (Oracle)
reviewerQuoted 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?
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_loopbackis 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_LSTATUSis checking the value that was returned fromphy_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!
Jakub Kicinski
maintainerQuoted 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?
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.
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