Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

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.

Link: https://lore.kernel.org/r/d7bb312e-2428-45f6-b9b3-59ba544e8b94@kili.mountain
Signed-off-by: Russell King (Oracle) <rmk+kernel@armlinux.org.uk>
Link: https://lore.kernel.org/r/E1q4kX6-00BNuM-Mx@rmk-PC.armlinux.org.uk
Signed-off-by: Jakub Kicinski <kuba@kernel.org>

authored by

Russell King (Oracle) and committed by
Jakub Kicinski
4ec73295 7fa217d4

+10 -6
+10 -6
include/linux/phy.h
··· 1206 1206 #define phy_read_poll_timeout(phydev, regnum, val, cond, sleep_us, \ 1207 1207 timeout_us, sleep_before_read) \ 1208 1208 ({ \ 1209 - int __ret = read_poll_timeout(phy_read, val, val < 0 || (cond), \ 1209 + int __ret, __val; \ 1210 + __ret = read_poll_timeout(__val = phy_read, val, \ 1211 + __val < 0 || (cond), \ 1210 1212 sleep_us, timeout_us, sleep_before_read, phydev, regnum); \ 1211 - if (val < 0) \ 1212 - __ret = val; \ 1213 + if (__val < 0) \ 1214 + __ret = __val; \ 1213 1215 if (__ret) \ 1214 1216 phydev_err(phydev, "%s failed: %d\n", __func__, __ret); \ 1215 1217 __ret; \ ··· 1304 1302 #define phy_read_mmd_poll_timeout(phydev, devaddr, regnum, val, cond, \ 1305 1303 sleep_us, timeout_us, sleep_before_read) \ 1306 1304 ({ \ 1307 - int __ret = read_poll_timeout(phy_read_mmd, val, (cond) || val < 0, \ 1305 + int __ret, __val; \ 1306 + __ret = read_poll_timeout(__val = phy_read_mmd, val, \ 1307 + __val < 0 || (cond), \ 1308 1308 sleep_us, timeout_us, sleep_before_read, \ 1309 1309 phydev, devaddr, regnum); \ 1310 - if (val < 0) \ 1311 - __ret = val; \ 1310 + if (__val < 0) \ 1311 + __ret = __val; \ 1312 1312 if (__ret) \ 1313 1313 phydev_err(phydev, "%s failed: %d\n", __func__, __ret); \ 1314 1314 __ret; \