Skip to contentSkip to conversation

net: ocelot: fix wrong time_after usage

PATCH Applied Pavel Skripkin 3 participants 3 messages Jul 6, 2022 – Jul 8, 2022

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

PATCH v4 1/1 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")

---

Changes since v3:
- Aligned the arguments to the open bracket

Changes since v2:
- Use _atomic variant of readx_poll_timeout

Changes since v1:
- Fixed typos in title and commit message
- Remove while loop and use readx_poll_timeout as suggested by Andrew

---
 drivers/net/ethernet/mscc/ocelot_fdma.c | 17 ++++++++---------
 1 file changed, 8 insertions(+), 9 deletions(-)
drivers/net/ethernet/mscc/ocelot_fdma.c +8 -9
index 083fddd263ec..8e3894cf5f7c 100644
@@ -94,19 +94,18 @@ static void ocelot_fdma_activate_chan(struct ocelot *ocelot, dma_addr_t dma,
 	ocelot_fdma_writel(ocelot, MSCC_FDMA_CH_ACTIVATE, BIT(chan));
 }
 
+static u32 ocelot_fdma_read_ch_safe(struct ocelot *ocelot)
+{
+	return ocelot_fdma_readl(ocelot, MSCC_FDMA_CH_SAFE);
+}
+
 static int ocelot_fdma_wait_chan_safe(struct ocelot *ocelot, int chan)
 {
-	unsigned long timeout;
 	u32 safe;
 
-	timeout = jiffies + usecs_to_jiffies(OCELOT_FDMA_CH_SAFE_TIMEOUT_US);
-	do {
-		safe = ocelot_fdma_readl(ocelot, MSCC_FDMA_CH_SAFE);
-		if (safe & BIT(chan))
-			return 0;
-	} while (time_after(jiffies, timeout));
-
-	return -ETIMEDOUT;
+	return readx_poll_timeout_atomic(ocelot_fdma_read_ch_safe, ocelot, safe,
+					 safe & BIT(chan), 0,
+					 OCELOT_FDMA_CH_SAFE_TIMEOUT_US);
 }
 
 static void ocelot_fdma_dcb_set_data(struct ocelot_fdma_dcb *dcb,
Signature · 1 line
2.36.1
maintainer Reviewed-by on v4 1/1
#1 lore raw
Quoted 12 lines · Pavel Skripkin

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.

Quoted 6 lines

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

Fixes 753a026cfec1 ("net: ocelot: add FDMA support")

---

This patch should target the "net" tree.

#2 lore raw
In reply to Pavel Skripkin
This patch was applied to netdev/net.git (master) by Jakub Kicinski <[email protected]>:

Hello:

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

Quoted 8 lines · On Wed, 6 Jul 2022 16:28:45 +0300 you wrote:

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.

[...]

Here is the summary with links:
- [v4] net: ocelot: fix wrong time_after usage

    https://git.kernel.org/netdev/net/c/f46fd3d7c3bd

You are awesome, thank you!

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