Linux-RISC-V Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: Marek Szyprowski <m.szyprowski@samsung.com>
To: Vivian Wang <wangruikang@iscas.ac.cn>,
	Andrew Lunn <andrew+netdev@lunn.ch>,
	"David S. Miller" <davem@davemloft.net>,
	Eric Dumazet <edumazet@google.com>,
	Jakub Kicinski <kuba@kernel.org>, Paolo Abeni <pabeni@redhat.com>,
	Yixun Lan <dlan@gentoo.org>,
	Maxime Chevallier <maxime.chevallier@bootlin.com>,
	Troy Mitchell <troy.mitchell@linux.spacemit.com>,
	Vadim Fedorenko <vadim.fedorenko@linux.dev>
Cc: netdev@vger.kernel.org, linux-riscv@lists.infradead.org,
	spacemit@lists.linux.dev, linux-kernel@vger.kernel.org,
	Vivian Wang <uwu@dram.page>
Subject: Re: [PATCH net-next] net: spacemit: Make stats_lock softirq-safe
Date: Fri, 19 Sep 2025 14:25:45 +0200	[thread overview]
Message-ID: <7605453a-ac62-497b-b77a-76d73e9a6741@samsung.com> (raw)
In-Reply-To: <20250919-k1-ethernet-fix-lock-v1-1-c8b700aa4954@iscas.ac.cn>

On 19.09.2025 14:04, Vivian Wang wrote:
> While most of the statistics functions (emac_get_stats64() and such) are
> called with softirqs enabled, emac_stats_timer() is, as its name
> suggests, also called from a timer, i.e. called in softirq context.
>
> All of these take stats_lock. Therefore, make stats_lock softirq-safe by
> changing spin_lock() into spin_lock_bh() for the functions that get
> statistics.
>
> Also, instead of directly calling emac_stats_timer() in emac_up() and
> emac_resume(), set the timer to trigger instead, so that
> emac_stats_timer() is only called from the timer. It will keep using
> spin_lock().
>
> This fixes a lockdep warning, and potential deadlock when stats_timer is
> triggered in the middle of getting statistics.
>
> Fixes: bfec6d7f2001 ("net: spacemit: Add K1 Ethernet MAC")
> Reported-by: Marek Szyprowski <m.szyprowski@samsung.com>
> Closes: https://lore.kernel.org/all/a52c0cf5-0444-41aa-b061-a0a1d72b02fe@samsung.com/
> Signed-off-by: Vivian Wang <wangruikang@iscas.ac.cn>
Tested-by: Marek Szyprowski <m.szyprowski@samsung.com>
> ---
> Thanks a lot for catching this, Marek!
> ---
>   drivers/net/ethernet/spacemit/k1_emac.c | 30 +++++++++++++++---------------
>   1 file changed, 15 insertions(+), 15 deletions(-)
>
> diff --git a/drivers/net/ethernet/spacemit/k1_emac.c b/drivers/net/ethernet/spacemit/k1_emac.c
> index 928fea02198c3754f63a7b33fc25c5dd8c2b59f9..e1c5faff3b71c7d4ceba2ea194d9c888f0e71b70 100644
> --- a/drivers/net/ethernet/spacemit/k1_emac.c
> +++ b/drivers/net/ethernet/spacemit/k1_emac.c
> @@ -135,7 +135,7 @@ struct emac_priv {
>   	bool flow_control_autoneg;
>   	u8 flow_control;
>   
> -	/* Hold while touching hardware statistics */
> +	/* Softirq-safe, hold while touching hardware statistics */
>   	spinlock_t stats_lock;
>   };
>   
> @@ -1239,7 +1239,7 @@ static void emac_get_stats64(struct net_device *dev,
>   	/* This is the only software counter */
>   	storage->tx_dropped = emac_get_stat_tx_drops(priv);
>   
> -	spin_lock(&priv->stats_lock);
> +	spin_lock_bh(&priv->stats_lock);
>   
>   	emac_stats_update(priv);
>   
> @@ -1261,7 +1261,7 @@ static void emac_get_stats64(struct net_device *dev,
>   	storage->rx_missed_errors = rx_stats->stats.rx_drp_fifo_full_pkts;
>   	storage->rx_missed_errors += rx_stats->stats.rx_truncate_fifo_full_pkts;
>   
> -	spin_unlock(&priv->stats_lock);
> +	spin_unlock_bh(&priv->stats_lock);
>   }
>   
>   static void emac_get_rmon_stats(struct net_device *dev,
> @@ -1275,7 +1275,7 @@ static void emac_get_rmon_stats(struct net_device *dev,
>   
>   	*ranges = emac_rmon_hist_ranges;
>   
> -	spin_lock(&priv->stats_lock);
> +	spin_lock_bh(&priv->stats_lock);
>   
>   	emac_stats_update(priv);
>   
> @@ -1294,7 +1294,7 @@ static void emac_get_rmon_stats(struct net_device *dev,
>   	rmon_stats->hist[5] = rx_stats->stats.rx_1024_1518_pkts;
>   	rmon_stats->hist[6] = rx_stats->stats.rx_1519_plus_pkts;
>   
> -	spin_unlock(&priv->stats_lock);
> +	spin_unlock_bh(&priv->stats_lock);
>   }
>   
>   static void emac_get_eth_mac_stats(struct net_device *dev,
> @@ -1307,7 +1307,7 @@ static void emac_get_eth_mac_stats(struct net_device *dev,
>   	tx_stats = &priv->tx_stats;
>   	rx_stats = &priv->rx_stats;
>   
> -	spin_lock(&priv->stats_lock);
> +	spin_lock_bh(&priv->stats_lock);
>   
>   	emac_stats_update(priv);
>   
> @@ -1325,7 +1325,7 @@ static void emac_get_eth_mac_stats(struct net_device *dev,
>   	mac_stats->FramesAbortedDueToXSColls =
>   		tx_stats->stats.tx_excessclsn_pkts;
>   
> -	spin_unlock(&priv->stats_lock);
> +	spin_unlock_bh(&priv->stats_lock);
>   }
>   
>   static void emac_get_pause_stats(struct net_device *dev,
> @@ -1338,14 +1338,14 @@ static void emac_get_pause_stats(struct net_device *dev,
>   	tx_stats = &priv->tx_stats;
>   	rx_stats = &priv->rx_stats;
>   
> -	spin_lock(&priv->stats_lock);
> +	spin_lock_bh(&priv->stats_lock);
>   
>   	emac_stats_update(priv);
>   
>   	pause_stats->tx_pause_frames = tx_stats->stats.tx_pause_pkts;
>   	pause_stats->rx_pause_frames = rx_stats->stats.rx_pause_pkts;
>   
> -	spin_unlock(&priv->stats_lock);
> +	spin_unlock_bh(&priv->stats_lock);
>   }
>   
>   /* Other statistics that are not derivable from standard statistics */
> @@ -1393,14 +1393,14 @@ static void emac_get_ethtool_stats(struct net_device *dev,
>   	u64 *rx_stats = (u64 *)&priv->rx_stats;
>   	int i;
>   
> -	spin_lock(&priv->stats_lock);
> +	spin_lock_bh(&priv->stats_lock);
>   
>   	emac_stats_update(priv);
>   
>   	for (i = 0; i < ARRAY_SIZE(emac_ethtool_rx_stats); i++)
>   		data[i] = rx_stats[emac_ethtool_rx_stats[i].offset];
>   
> -	spin_unlock(&priv->stats_lock);
> +	spin_unlock_bh(&priv->stats_lock);
>   }
>   
>   static int emac_ethtool_get_regs_len(struct net_device *dev)
> @@ -1769,7 +1769,7 @@ static int emac_up(struct emac_priv *priv)
>   
>   	netif_start_queue(ndev);
>   
> -	emac_stats_timer(&priv->stats_timer);
> +	mod_timer(&priv->stats_timer, jiffies);
>   
>   	return 0;
>   
> @@ -1807,14 +1807,14 @@ static int emac_down(struct emac_priv *priv)
>   
>   	/* Update and save current stats, see emac_stats_update() for usage */
>   
> -	spin_lock(&priv->stats_lock);
> +	spin_lock_bh(&priv->stats_lock);
>   
>   	emac_stats_update(priv);
>   
>   	priv->tx_stats_off = priv->tx_stats;
>   	priv->rx_stats_off = priv->rx_stats;
>   
> -	spin_unlock(&priv->stats_lock);
> +	spin_unlock_bh(&priv->stats_lock);
>   
>   	pm_runtime_put_sync(&pdev->dev);
>   	return 0;
> @@ -2111,7 +2111,7 @@ static int emac_resume(struct device *dev)
>   
>   	netif_device_attach(ndev);
>   
> -	emac_stats_timer(&priv->stats_timer);
> +	mod_timer(&priv->stats_timer, jiffies);
>   
>   	return 0;
>   }
>
> ---
> base-commit: 315f423be0d1ebe720d8fd4fa6bed68586b13d34
> change-id: 20250919-k1-ethernet-fix-lock-c99681a9aa5d
>
> Best regards,

Best regards
-- 
Marek Szyprowski, PhD
Samsung R&D Institute Poland


_______________________________________________
linux-riscv mailing list
linux-riscv@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-riscv

  reply	other threads:[~2025-09-19 12:26 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <CGME20250919120522eucas1p224c42d0cc2a59903d85e6c80dfdfa727@eucas1p2.samsung.com>
2025-09-19 12:04 ` [PATCH net-next] net: spacemit: Make stats_lock softirq-safe Vivian Wang
2025-09-19 12:25   ` Marek Szyprowski [this message]
2025-09-22 19:00   ` patchwork-bot+netdevbpf

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=7605453a-ac62-497b-b77a-76d73e9a6741@samsung.com \
    --to=m.szyprowski@samsung.com \
    --cc=andrew+netdev@lunn.ch \
    --cc=davem@davemloft.net \
    --cc=dlan@gentoo.org \
    --cc=edumazet@google.com \
    --cc=kuba@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-riscv@lists.infradead.org \
    --cc=maxime.chevallier@bootlin.com \
    --cc=netdev@vger.kernel.org \
    --cc=pabeni@redhat.com \
    --cc=spacemit@lists.linux.dev \
    --cc=troy.mitchell@linux.spacemit.com \
    --cc=uwu@dram.page \
    --cc=vadim.fedorenko@linux.dev \
    --cc=wangruikang@iscas.ac.cn \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox