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

net: ocelot: fix wrong time_after usage

Accidentally noticed, that this driver is the only user of
while (time_after(jiffies...)).

It looks like typo, because likely this while loop will finish after 1st
iteration, because time_after() returns true when 1st argument _is after_
2nd one.

There is one possible problem with this poll loop: the scheduler could put
the thread to sleep, and it does not get woken up for
OCELOT_FDMA_CH_SAFE_TIMEOUT_US. During that time, the hardware has done
its thing, but you exit the while loop and return -ETIMEDOUT.

Fix it by using sane poll API that avoids all problems described above

Fixes: 753a026cfec1 ("net: ocelot: add FDMA support")
Suggested-by: Andrew Lunn <andrew@lunn.ch>
Signed-off-by: Pavel Skripkin <paskripkin@gmail.com>
Reviewed-by: Vladimir Oltean <vladimir.oltean@nxp.com>
Link: https://lore.kernel.org/r/20220706132845.27968-1-paskripkin@gmail.com
Signed-off-by: Jakub Kicinski <kuba@kernel.org>

authored by

Pavel Skripkin and committed by
Jakub Kicinski
f46fd3d7 fe5235ae

+8 -9
+8 -9
drivers/net/ethernet/mscc/ocelot_fdma.c
··· 94 94 ocelot_fdma_writel(ocelot, MSCC_FDMA_CH_ACTIVATE, BIT(chan)); 95 95 } 96 96 97 + static u32 ocelot_fdma_read_ch_safe(struct ocelot *ocelot) 98 + { 99 + return ocelot_fdma_readl(ocelot, MSCC_FDMA_CH_SAFE); 100 + } 101 + 97 102 static int ocelot_fdma_wait_chan_safe(struct ocelot *ocelot, int chan) 98 103 { 99 - unsigned long timeout; 100 104 u32 safe; 101 105 102 - timeout = jiffies + usecs_to_jiffies(OCELOT_FDMA_CH_SAFE_TIMEOUT_US); 103 - do { 104 - safe = ocelot_fdma_readl(ocelot, MSCC_FDMA_CH_SAFE); 105 - if (safe & BIT(chan)) 106 - return 0; 107 - } while (time_after(jiffies, timeout)); 108 - 109 - return -ETIMEDOUT; 106 + return readx_poll_timeout_atomic(ocelot_fdma_read_ch_safe, ocelot, safe, 107 + safe & BIT(chan), 0, 108 + OCELOT_FDMA_CH_SAFE_TIMEOUT_US); 110 109 } 111 110 112 111 static void ocelot_fdma_dcb_set_data(struct ocelot_fdma_dcb *dcb,