* [PATCH net-next v2] net: dlink: add synchronization for stats update
@ 2025-04-25 23:13 Moon Yeounsu
2025-04-29 21:35 ` Jakub Kicinski
0 siblings, 1 reply; 4+ messages in thread
From: Moon Yeounsu @ 2025-04-25 23:13 UTC (permalink / raw)
To: Andrew Lunn, David S. Miller, Eric Dumazet, Jakub Kicinski,
Paolo Abeni
Cc: Moon Yeounsu, netdev, linux-kernel
There are two paths that call `get_stats()`:
1. From user space via the `ip` command
2. From interrupt context via `rio_interrupt()`
Case 1 is synchronized by `rtnl_lock()`, so it is safe.
However, the two cases above are not synchronized with each other.
Therefore, `spin_lock` is needed to protect `get_stats()` as it can be
preempted by an interrupt. In this context, `spin_lock_irq()` is required
(using `spin_lock_bh()` may result in a deadlock).
`dev->stats.tx_errors` and `dev->stats.collisions` may be
concurrently modified by the interrupt handler and user space, so
they are also protected by `spin_lock`.
Tested-on: D-Link DGE-550T Rev-A3
Signed-off-by: Moon Yeounsu <yyyynoom@gmail.com>
---
Changelog:
v1: https://lore.kernel.org/netdev/20250421191645.43526-2-yyyynoom@gmail.com/
v2:
- fix incorrect method of updating `dev->stats.tx_errors` and
`dev->stats.collisions`
---
drivers/net/ethernet/dlink/dl2k.c | 11 ++++++++++-
drivers/net/ethernet/dlink/dl2k.h | 2 ++
2 files changed, 12 insertions(+), 1 deletion(-)
diff --git a/drivers/net/ethernet/dlink/dl2k.c b/drivers/net/ethernet/dlink/dl2k.c
index d88fbecdab4b..0a3ac9ba3729 100644
--- a/drivers/net/ethernet/dlink/dl2k.c
+++ b/drivers/net/ethernet/dlink/dl2k.c
@@ -865,7 +865,6 @@ tx_error (struct net_device *dev, int tx_status)
frame_id = (tx_status & 0xffff0000);
printk (KERN_ERR "%s: Transmit error, TxStatus %4.4x, FrameId %d.\n",
dev->name, tx_status, frame_id);
- dev->stats.tx_errors++;
/* Ttransmit Underrun */
if (tx_status & 0x10) {
dev->stats.tx_fifo_errors++;
@@ -902,9 +901,15 @@ tx_error (struct net_device *dev, int tx_status)
rio_set_led_mode(dev);
/* Let TxStartThresh stay default value */
}
+
+ spin_lock_irq(&np->stats_lock);
/* Maximum Collisions */
if (tx_status & 0x08)
dev->stats.collisions++;
+
+ dev->stats.tx_errors++;
+ spin_unlock_irq(&np->stats_lock);
+
/* Restart the Tx */
dw32(MACCtrl, dr16(MACCtrl) | TxEnable);
}
@@ -1074,6 +1079,7 @@ get_stats (struct net_device *dev)
#endif
unsigned int stat_reg;
+ spin_lock_irq(&np->stats_lock);
/* All statistics registers need to be acknowledged,
else statistic overflow could cause problems */
@@ -1123,6 +1129,9 @@ get_stats (struct net_device *dev)
dr16(TCPCheckSumErrors);
dr16(UDPCheckSumErrors);
dr16(IPCheckSumErrors);
+
+ spin_unlock_irq(&np->stats_lock);
+
return &dev->stats;
}
diff --git a/drivers/net/ethernet/dlink/dl2k.h b/drivers/net/ethernet/dlink/dl2k.h
index 195dc6cfd895..c24823e909ef 100644
--- a/drivers/net/ethernet/dlink/dl2k.h
+++ b/drivers/net/ethernet/dlink/dl2k.h
@@ -372,6 +372,8 @@ struct netdev_private {
struct pci_dev *pdev;
void __iomem *ioaddr;
void __iomem *eeprom_addr;
+ // To ensure synchronization when stats are updated.
+ spinlock_t stats_lock;
spinlock_t tx_lock;
spinlock_t rx_lock;
unsigned int rx_buf_sz; /* Based on MTU+slack. */
--
2.49.0
^ permalink raw reply related [flat|nested] 4+ messages in thread* Re: [PATCH net-next v2] net: dlink: add synchronization for stats update
2025-04-25 23:13 [PATCH net-next v2] net: dlink: add synchronization for stats update Moon Yeounsu
@ 2025-04-29 21:35 ` Jakub Kicinski
2025-05-02 23:50 ` Moon Yeounsu
0 siblings, 1 reply; 4+ messages in thread
From: Jakub Kicinski @ 2025-04-29 21:35 UTC (permalink / raw)
To: Moon Yeounsu
Cc: Andrew Lunn, David S. Miller, Eric Dumazet, Paolo Abeni, netdev,
linux-kernel
On Sat, 26 Apr 2025 08:13:52 +0900 Moon Yeounsu wrote:
> + spin_lock_irq(&np->stats_lock);
> /* Maximum Collisions */
> if (tx_status & 0x08)
> dev->stats.collisions++;
> +
> + dev->stats.tx_errors++;
> + spin_unlock_irq(&np->stats_lock);
I could be wrong but since this code runs in IRQ context I believe
you need to use spin_lock_irqsave() here. Or just spin_lock() but
not sure, and spin_lock_irqsave() always works..
--
pw-bot: cr
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH net-next v2] net: dlink: add synchronization for stats update
2025-04-29 21:35 ` Jakub Kicinski
@ 2025-05-02 23:50 ` Moon Yeounsu
2025-05-06 1:49 ` Jakub Kicinski
0 siblings, 1 reply; 4+ messages in thread
From: Moon Yeounsu @ 2025-05-02 23:50 UTC (permalink / raw)
To: Jakub Kicinski
Cc: Andrew Lunn, David S. Miller, Eric Dumazet, Paolo Abeni, netdev,
linux-kernel
Thank you for reviewing my patch!
On Wed, Apr 30, 2025 at 6:35 AM Jakub Kicinski <kuba@kernel.org> wrote:
> I could be wrong but since this code runs in IRQ context I believe
> you need to use spin_lock_irqsave() here. Or just spin_lock() but
> not sure, and spin_lock_irqsave() always works..
If my understanding is correct, then it seems I was mistaken and you were right.
I misunderstood that the same form of locking function should always
be used for a given lock. However, as you pointed out,
`spin_[un]lock()` seems to be the correct choice in this case.
This is because the `tx_error()` function is only called from
`rio_interrupt()`, which runs in IRQ context. (There are no other call
paths.) In this context, there's no need to enable or disable IRQs at
all.
Also, I believe that `spin_lock_irq()` in `get_stats()` should be
changed to `spin_lock_irqsave()` since `get_stats()` can be called
from IRQ context (via `rio_interrupt()` -> `rio_error()` ->
`get_stats()`). In my view, calling `spin_unlock_irq()` in this
context could be risky, as it would re-enable local IRQs via
local_irq_enable().
There are two ways to lock the `get_stats()` function: either add a
new parameter to check whether it's in IRQ context, or simply use
`spin_lock_irqsave()`. I found that `rio_free_tx()` behaves like the
first case. I'd appreciate your opinion on which approach would be
preferable here.
Thanks again for your feedback.
Best regards,
Moon Yeounsu
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH net-next v2] net: dlink: add synchronization for stats update
2025-05-02 23:50 ` Moon Yeounsu
@ 2025-05-06 1:49 ` Jakub Kicinski
0 siblings, 0 replies; 4+ messages in thread
From: Jakub Kicinski @ 2025-05-06 1:49 UTC (permalink / raw)
To: Moon Yeounsu
Cc: Andrew Lunn, David S. Miller, Eric Dumazet, Paolo Abeni, netdev,
linux-kernel
On Sat, 3 May 2025 08:50:49 +0900 Moon Yeounsu wrote:
> Also, I believe that `spin_lock_irq()` in `get_stats()` should be
> changed to `spin_lock_irqsave()` since `get_stats()` can be called
> from IRQ context (via `rio_interrupt()` -> `rio_error()` ->
> `get_stats()`). In my view, calling `spin_unlock_irq()` in this
> context could be risky, as it would re-enable local IRQs via
> local_irq_enable().
>
> There are two ways to lock the `get_stats()` function: either add a
> new parameter to check whether it's in IRQ context, or simply use
> `spin_lock_irqsave()`. I found that `rio_free_tx()` behaves like the
> first case. I'd appreciate your opinion on which approach would be
> preferable here.
If there's a call path from the IRQ I'd go with spin_lock_irqsave()
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2025-05-06 1:49 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-04-25 23:13 [PATCH net-next v2] net: dlink: add synchronization for stats update Moon Yeounsu
2025-04-29 21:35 ` Jakub Kicinski
2025-05-02 23:50 ` Moon Yeounsu
2025-05-06 1:49 ` Jakub Kicinski
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).