All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v3] net: ocelot: fix wrong time_after usage
@ 2022-07-06 10:50 Pavel Skripkin
  2022-07-06 13:13 ` Vladimir Oltean
  0 siblings, 1 reply; 4+ messages in thread
From: Pavel Skripkin @ 2022-07-06 10:50 UTC (permalink / raw)
  To: vladimir.oltean, claudiu.manoil, alexandre.belloni, davem,
	edumazet, kuba, pabeni
  Cc: clement.leger, netdev, linux-kernel, Pavel Skripkin, Andrew Lunn

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>
---

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(-)

diff --git a/drivers/net/ethernet/mscc/ocelot_fdma.c b/drivers/net/ethernet/mscc/ocelot_fdma.c
index 083fddd263ec..c93fba0a2a7d 100644
--- a/drivers/net/ethernet/mscc/ocelot_fdma.c
+++ b/drivers/net/ethernet/mscc/ocelot_fdma.c
@@ -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,
-- 
2.35.1


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH v3] net: ocelot: fix wrong time_after usage
  2022-07-06 10:50 [PATCH v3] net: ocelot: fix wrong time_after usage Pavel Skripkin
@ 2022-07-06 13:13 ` Vladimir Oltean
  2022-07-06 13:29   ` Pavel Skripkin
  0 siblings, 1 reply; 4+ messages in thread
From: Vladimir Oltean @ 2022-07-06 13:13 UTC (permalink / raw)
  To: Pavel Skripkin
  Cc: Claudiu Manoil, alexandre.belloni, davem, edumazet, kuba, pabeni,
	clement.leger, netdev, linux-kernel, Andrew Lunn

Hi Pavel,

On Wed, Jul 06, 2022 at 01:50:44PM +0300, Pavel Skripkin 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.
> 
> 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>
> ---
> 
> 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(-)
> 
> diff --git a/drivers/net/ethernet/mscc/ocelot_fdma.c b/drivers/net/ethernet/mscc/ocelot_fdma.c
> index 083fddd263ec..c93fba0a2a7d 100644
> --- a/drivers/net/ethernet/mscc/ocelot_fdma.c
> +++ b/drivers/net/ethernet/mscc/ocelot_fdma.c
> @@ -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);

Can you please indent the arguments to the open bracket?

>  }
>  
>  static void ocelot_fdma_dcb_set_data(struct ocelot_fdma_dcb *dcb,
> -- 
> 2.35.1
>

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH v3] net: ocelot: fix wrong time_after usage
  2022-07-06 13:13 ` Vladimir Oltean
@ 2022-07-06 13:29   ` Pavel Skripkin
  2022-07-06 13:30     ` Vladimir Oltean
  0 siblings, 1 reply; 4+ messages in thread
From: Pavel Skripkin @ 2022-07-06 13:29 UTC (permalink / raw)
  To: Vladimir Oltean
  Cc: Claudiu Manoil, alexandre.belloni, davem, edumazet, kuba, pabeni,
	clement.leger, netdev, linux-kernel, Andrew Lunn

Hi Vladimir,

Vladimir Oltean <vladimir.oltean@nxp.com> says:
> Can you please indent the arguments to the open bracket?
> 

Sure thing! I've just sent a v4.

Thank you for review



Thanks,
--Pavel Skripkin

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH v3] net: ocelot: fix wrong time_after usage
  2022-07-06 13:29   ` Pavel Skripkin
@ 2022-07-06 13:30     ` Vladimir Oltean
  0 siblings, 0 replies; 4+ messages in thread
From: Vladimir Oltean @ 2022-07-06 13:30 UTC (permalink / raw)
  To: Pavel Skripkin
  Cc: Claudiu Manoil, alexandre.belloni, davem, edumazet, kuba, pabeni,
	clement.leger, netdev, linux-kernel, Andrew Lunn

On Wed, Jul 06, 2022 at 04:29:45PM +0300, Pavel Skripkin wrote:
> Hi Vladimir,
> 
> Vladimir Oltean <vladimir.oltean@nxp.com> says:
> > Can you please indent the arguments to the open bracket?
> > 
> 
> Sure thing! I've just sent a v4.
> 
> Thank you for review

And thank you for the patch!

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2022-07-06 13:31 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-07-06 10:50 [PATCH v3] net: ocelot: fix wrong time_after usage Pavel Skripkin
2022-07-06 13:13 ` Vladimir Oltean
2022-07-06 13:29   ` Pavel Skripkin
2022-07-06 13:30     ` Vladimir Oltean

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.