linux-renesas-soc.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH net v4] smsc911x: only update stats when interface is up
@ 2023-03-29  6:40 Wolfram Sang
  2023-03-29  7:41 ` Steen.Hegelund
  2023-03-29 19:39 ` Jakub Kicinski
  0 siblings, 2 replies; 9+ messages in thread
From: Wolfram Sang @ 2023-03-29  6:40 UTC (permalink / raw)
  To: netdev
  Cc: linux-renesas-soc, Wolfram Sang, Steve Glendinning,
	David S. Miller, Eric Dumazet, Jakub Kicinski, Paolo Abeni,
	Geert Uytterhoeven, linux-kernel

Otherwise the clocks are not enabled and reading registers will OOPS.
Copy the behaviour from Renesas SH_ETH and use a custom flag because
using netif_running() is racy. A generic solution still needs to be
implemented. Tested on a Renesas APE6-EK.

Fixes: 1e30b8d755b8 ("net: smsc911x: Make Runtime PM handling more fine-grained")
Signed-off-by: Wolfram Sang <wsa+renesas@sang-engineering.com>
---

Changes since v3:
* broken out of a patch series
* don't use netif_running() but a custom flag

 drivers/net/ethernet/smsc/smsc911x.c | 14 ++++++++++++--
 1 file changed, 12 insertions(+), 2 deletions(-)

diff --git a/drivers/net/ethernet/smsc/smsc911x.c b/drivers/net/ethernet/smsc/smsc911x.c
index a690d139e177..af96986cbc88 100644
--- a/drivers/net/ethernet/smsc/smsc911x.c
+++ b/drivers/net/ethernet/smsc/smsc911x.c
@@ -140,6 +140,8 @@ struct smsc911x_data {
 
 	/* clock */
 	struct clk *clk;
+
+	bool is_open;
 };
 
 /* Easy access to information */
@@ -1738,6 +1740,8 @@ static int smsc911x_open(struct net_device *dev)
 	smsc911x_reg_write(pdata, TX_CFG, TX_CFG_TX_ON_);
 
 	netif_start_queue(dev);
+	pdata->is_open = true;
+
 	return 0;
 
 irq_stop_out:
@@ -1778,6 +1782,8 @@ static int smsc911x_stop(struct net_device *dev)
 		dev->phydev = NULL;
 	}
 	netif_carrier_off(dev);
+	pdata->is_open = false;
+
 	pm_runtime_put(dev->dev.parent);
 
 	SMSC_TRACE(pdata, ifdown, "Interface stopped");
@@ -1841,8 +1847,12 @@ smsc911x_hard_start_xmit(struct sk_buff *skb, struct net_device *dev)
 static struct net_device_stats *smsc911x_get_stats(struct net_device *dev)
 {
 	struct smsc911x_data *pdata = netdev_priv(dev);
-	smsc911x_tx_update_txcounters(dev);
-	dev->stats.rx_dropped += smsc911x_reg_read(pdata, RX_DROP);
+
+	if (pdata->is_open) {
+		smsc911x_tx_update_txcounters(dev);
+		dev->stats.rx_dropped += smsc911x_reg_read(pdata, RX_DROP);
+	}
+
 	return &dev->stats;
 }
 
-- 
2.30.2


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

* Re: [PATCH net v4] smsc911x: only update stats when interface is up
  2023-03-29  6:40 [PATCH net v4] smsc911x: only update stats when interface is up Wolfram Sang
@ 2023-03-29  7:41 ` Steen.Hegelund
  2023-03-31  6:02   ` Wolfram Sang
  2023-03-29 19:39 ` Jakub Kicinski
  1 sibling, 1 reply; 9+ messages in thread
From: Steen.Hegelund @ 2023-03-29  7:41 UTC (permalink / raw)
  To: wsa+renesas, netdev
  Cc: linux-renesas-soc, steve.glendinning, davem, edumazet, kuba,
	pabeni, geert+renesas, linux-kernel

Hi Wolfram,

On Wed Mar 29, 2023 at 8:40 AM CEST, Wolfram Sang wrote:
> EXTERNAL EMAIL: Do not click links or open attachments unless you know the content is safe
>
> Otherwise the clocks are not enabled and reading registers will OOPS.
> Copy the behaviour from Renesas SH_ETH and use a custom flag because
> using netif_running() is racy. A generic solution still needs to be
> implemented. Tested on a Renesas APE6-EK.
>
> Fixes: 1e30b8d755b8 ("net: smsc911x: Make Runtime PM handling more fine-grained")
> Signed-off-by: Wolfram Sang <wsa+renesas@sang-engineering.com>
> ---
>
> Changes since v3:
> * broken out of a patch series
> * don't use netif_running() but a custom flag
>
>  drivers/net/ethernet/smsc/smsc911x.c | 14 ++++++++++++--
>  1 file changed, 12 insertions(+), 2 deletions(-)
>
> diff --git a/drivers/net/ethernet/smsc/smsc911x.c b/drivers/net/ethernet/smsc/smsc911x.c
> index a690d139e177..af96986cbc88 100644
> --- a/drivers/net/ethernet/smsc/smsc911x.c
> +++ b/drivers/net/ethernet/smsc/smsc911x.c
> @@ -140,6 +140,8 @@ struct smsc911x_data {
>
>         /* clock */
>         struct clk *clk;
> +
> +       bool is_open;
>  };
>
>  /* Easy access to information */
> @@ -1738,6 +1740,8 @@ static int smsc911x_open(struct net_device *dev)
>         smsc911x_reg_write(pdata, TX_CFG, TX_CFG_TX_ON_);
>
>         netif_start_queue(dev);
> +       pdata->is_open = true;
> +
>         return 0;
>
>  irq_stop_out:
> @@ -1778,6 +1782,8 @@ static int smsc911x_stop(struct net_device *dev)
>                 dev->phydev = NULL;
>         }
>         netif_carrier_off(dev);
> +       pdata->is_open = false;
> +
>         pm_runtime_put(dev->dev.parent);
>
>         SMSC_TRACE(pdata, ifdown, "Interface stopped");
> @@ -1841,8 +1847,12 @@ smsc911x_hard_start_xmit(struct sk_buff *skb, struct net_device *dev)
>  static struct net_device_stats *smsc911x_get_stats(struct net_device *dev)
>  {
>         struct smsc911x_data *pdata = netdev_priv(dev);
> -       smsc911x_tx_update_txcounters(dev);
> -       dev->stats.rx_dropped += smsc911x_reg_read(pdata, RX_DROP);
> +
> +       if (pdata->is_open) {

Couldn't you just use netif_carrier_ok() here and drop the is_open
variable?

> +               smsc911x_tx_update_txcounters(dev);
> +               dev->stats.rx_dropped += smsc911x_reg_read(pdata, RX_DROP);
> +       }
> +
>         return &dev->stats;
>  }
>
> --
> 2.30.2

BR
Steen

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

* Re: [PATCH net v4] smsc911x: only update stats when interface is up
  2023-03-29  6:40 [PATCH net v4] smsc911x: only update stats when interface is up Wolfram Sang
  2023-03-29  7:41 ` Steen.Hegelund
@ 2023-03-29 19:39 ` Jakub Kicinski
  2023-03-29 19:48   ` Wolfram Sang
  1 sibling, 1 reply; 9+ messages in thread
From: Jakub Kicinski @ 2023-03-29 19:39 UTC (permalink / raw)
  To: Wolfram Sang
  Cc: netdev, linux-renesas-soc, Steve Glendinning, David S. Miller,
	Eric Dumazet, Paolo Abeni, Geert Uytterhoeven, linux-kernel

On Wed, 29 Mar 2023 08:40:10 +0200 Wolfram Sang wrote:
> Otherwise the clocks are not enabled and reading registers will OOPS.
> Copy the behaviour from Renesas SH_ETH and use a custom flag because
> using netif_running() is racy. A generic solution still needs to be
> implemented. Tested on a Renesas APE6-EK.

Hm, so you opted to not add the flag in the core?
To keep the backport small? I think we should just add it..
Clearly multiple drivers would benefit and it's not a huge change.

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

* Re: [PATCH net v4] smsc911x: only update stats when interface is up
  2023-03-29 19:39 ` Jakub Kicinski
@ 2023-03-29 19:48   ` Wolfram Sang
  2023-03-29 20:17     ` Jakub Kicinski
  0 siblings, 1 reply; 9+ messages in thread
From: Wolfram Sang @ 2023-03-29 19:48 UTC (permalink / raw)
  To: Jakub Kicinski
  Cc: netdev, linux-renesas-soc, Steve Glendinning, David S. Miller,
	Eric Dumazet, Paolo Abeni, Geert Uytterhoeven, linux-kernel

[-- Attachment #1: Type: text/plain, Size: 1158 bytes --]

On Wed, Mar 29, 2023 at 12:39:58PM -0700, Jakub Kicinski wrote:
> On Wed, 29 Mar 2023 08:40:10 +0200 Wolfram Sang wrote:
> > Otherwise the clocks are not enabled and reading registers will OOPS.
> > Copy the behaviour from Renesas SH_ETH and use a custom flag because
> > using netif_running() is racy. A generic solution still needs to be
> > implemented. Tested on a Renesas APE6-EK.
> 
> Hm, so you opted to not add the flag in the core?
> To keep the backport small? I think we should just add it..
> Clearly multiple drivers would benefit and it's not a huge change.

I did it this way for two reasons. First, yes, this is a minimal patch
for backporting. No dependency on core changes, very easy. Second, this
is a solution I could develop quickly. I am interested in finding
another solution, but I guess it needs more time, especially as it
probably touches 15 drivers. I created an action item for it. I hope
I'll be able to work on it somewhen. But for now, I just need the SMSC
bug fixed and need to move to the next issue. If we later have the
generic solution, converting this driver also won't make a lot of a
difference.


[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 833 bytes --]

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

* Re: [PATCH net v4] smsc911x: only update stats when interface is up
  2023-03-29 19:48   ` Wolfram Sang
@ 2023-03-29 20:17     ` Jakub Kicinski
  2023-03-31  6:33       ` Wolfram Sang
  0 siblings, 1 reply; 9+ messages in thread
From: Jakub Kicinski @ 2023-03-29 20:17 UTC (permalink / raw)
  To: Wolfram Sang
  Cc: netdev, linux-renesas-soc, Steve Glendinning, David S. Miller,
	Eric Dumazet, Paolo Abeni, Geert Uytterhoeven, linux-kernel

On Wed, 29 Mar 2023 21:48:55 +0200 Wolfram Sang wrote:
> > Hm, so you opted to not add the flag in the core?
> > To keep the backport small? I think we should just add it..
> > Clearly multiple drivers would benefit and it's not a huge change.  
> 
> I did it this way for two reasons. First, yes, this is a minimal patch
> for backporting. No dependency on core changes, very easy. Second, this
> is a solution I could develop quickly. I am interested in finding
> another solution, but I guess it needs more time, especially as it
> probably touches 15 drivers. I created an action item for it. I hope
> I'll be able to work on it somewhen. But for now, I just need the SMSC
> bug fixed and need to move to the next issue. If we later have the
> generic solution, converting this driver also won't make a lot of a
> difference.

Okay, core changes aside - does pm_runtime_put() imply an RCU sync?
Otherwise your check in get_stats is racy...

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

* Re: [PATCH net v4] smsc911x: only update stats when interface is up
  2023-03-29  7:41 ` Steen.Hegelund
@ 2023-03-31  6:02   ` Wolfram Sang
  0 siblings, 0 replies; 9+ messages in thread
From: Wolfram Sang @ 2023-03-31  6:02 UTC (permalink / raw)
  To: Steen.Hegelund
  Cc: netdev, linux-renesas-soc, steve.glendinning, davem, edumazet,
	kuba, pabeni, geert+renesas, linux-kernel

[-- Attachment #1: Type: text/plain, Size: 806 bytes --]


> > +       if (pdata->is_open) {
> 
> Couldn't you just use netif_carrier_ok() here and drop the is_open
> variable?

From my research, I can't:

1) netif_carrier_ok() uses __LINK_STATE_NOCARRIER
2) __LINK_STATE_NOCARRIER gets cleared in netif_carrier_on()
3) netif_carrier_on() is this code:

	if (test_and_clear_bit(__LINK_STATE_NOCARRIER, &dev->state)) {
		if (dev->reg_state == NETREG_UNINITIALIZED)
			return;
		atomic_inc(&dev->carrier_up_count);
		linkwatch_fire_event(dev);
		if (netif_running(dev))
			__netdev_watchdog_up(dev);
	}

4) Notice the last if. It checks netif_running(). So, it is possible to
have the carrier on and the device not opened yet.
5) Sadly, no cigar. If I didn't miss something...

But thanks for the suggestion! Happy hacking,

   Wolfram


[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 833 bytes --]

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

* Re: [PATCH net v4] smsc911x: only update stats when interface is up
  2023-03-29 20:17     ` Jakub Kicinski
@ 2023-03-31  6:33       ` Wolfram Sang
  2023-03-31  6:50         ` Jakub Kicinski
  0 siblings, 1 reply; 9+ messages in thread
From: Wolfram Sang @ 2023-03-31  6:33 UTC (permalink / raw)
  To: Jakub Kicinski
  Cc: netdev, linux-renesas-soc, Steve Glendinning, David S. Miller,
	Eric Dumazet, Paolo Abeni, Geert Uytterhoeven, linux-kernel

[-- Attachment #1: Type: text/plain, Size: 328 bytes --]


> Okay, core changes aside - does pm_runtime_put() imply an RCU sync?
> Otherwise your check in get_stats is racy...

From some light research, I can't find a trace of RCU sync. Pity, I'll
need to move furhter investigation to later. I'll report back if I have
something, but it may take a while. Thanks for the help!


[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 833 bytes --]

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

* Re: [PATCH net v4] smsc911x: only update stats when interface is up
  2023-03-31  6:33       ` Wolfram Sang
@ 2023-03-31  6:50         ` Jakub Kicinski
  2023-03-31  9:53           ` Wolfram Sang
  0 siblings, 1 reply; 9+ messages in thread
From: Jakub Kicinski @ 2023-03-31  6:50 UTC (permalink / raw)
  To: Wolfram Sang
  Cc: netdev, linux-renesas-soc, Steve Glendinning, David S. Miller,
	Eric Dumazet, Paolo Abeni, Geert Uytterhoeven, linux-kernel

On Fri, 31 Mar 2023 08:33:24 +0200 Wolfram Sang wrote:
> > Okay, core changes aside - does pm_runtime_put() imply an RCU sync?
> > Otherwise your check in get_stats is racy...  
> 
> From some light research, I can't find a trace of RCU sync. Pity, I'll
> need to move furhter investigation to later. I'll report back if I have
> something, but it may take a while. Thanks for the help!

If you don't want to spend too much time you can just call it yourself
right? :) Put a synchronize_rcu() with a comment about concurrent stat
reading after pdata->is_open = false; and that's it?

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

* Re: [PATCH net v4] smsc911x: only update stats when interface is up
  2023-03-31  6:50         ` Jakub Kicinski
@ 2023-03-31  9:53           ` Wolfram Sang
  0 siblings, 0 replies; 9+ messages in thread
From: Wolfram Sang @ 2023-03-31  9:53 UTC (permalink / raw)
  To: Jakub Kicinski
  Cc: netdev, linux-renesas-soc, Steve Glendinning, David S. Miller,
	Eric Dumazet, Paolo Abeni, Geert Uytterhoeven, linux-kernel

[-- Attachment #1: Type: text/plain, Size: 870 bytes --]


> > From some light research, I can't find a trace of RCU sync. Pity, I'll
> > need to move furhter investigation to later. I'll report back if I have
> > something, but it may take a while. Thanks for the help!
> 
> If you don't want to spend too much time you can just call it yourself
> right? :) Put a synchronize_rcu() with a comment about concurrent stat
> reading after pdata->is_open = false; and that's it?

Probably. Although I trust you, I would still add code which I a) don't
fully understand myself yet and b) feels somehow wrong because something
like synchronize_rcu() should likely not be in drivers but somewhere
more generic. Given that the bug in this driver went unnoticed for years
(same with the race in other drivers), I do prefer to come back later
with the good feeling that I understood the problem and fixed it
thoroughly.


[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 833 bytes --]

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

end of thread, other threads:[~2023-03-31  9:55 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-03-29  6:40 [PATCH net v4] smsc911x: only update stats when interface is up Wolfram Sang
2023-03-29  7:41 ` Steen.Hegelund
2023-03-31  6:02   ` Wolfram Sang
2023-03-29 19:39 ` Jakub Kicinski
2023-03-29 19:48   ` Wolfram Sang
2023-03-29 20:17     ` Jakub Kicinski
2023-03-31  6:33       ` Wolfram Sang
2023-03-31  6:50         ` Jakub Kicinski
2023-03-31  9:53           ` Wolfram Sang

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).