Skip to contentSkip to conversation

net: dlink: add synchronization for stats update

PATCH Superseded Moon Yeounsu 2 participants 4 messages Apr 25, 2025 – May 6, 2025

Subsystems (2)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 net: dlink: add synchronization for stats update

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

---
Changelog:
v1: https://lore.kernel.org/netdev/[email protected]/
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(-)
drivers/net/ethernet/dlink/dl2k.c +10 -1
index d88fbecdab4b..0a3ac9ba3729 100644
@@ -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;
 }
 
drivers/net/ethernet/dlink/dl2k.h +2 -0
index 195dc6cfd895..c24823e909ef 100644
@@ -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. */
Signature · 1 line
2.49.0
maintainer
#1 lore raw

drivers/net/ethernet/dlink/dl2k.c tx_error (struct net_device *dev, int tx_status) · line 911

Quoted 7 lines · On Sat, 26 Apr 2025 08:13:52 +0900 Moon Yeounsu wrote:
drivers/net/ethernet/dlink/dl2k.c +4 -0
+	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..

Signature · 1 line
pw-bot: cr
#2 lore raw

Thank you for reviewing my patch!

Quoted 3 lines · Jakub Kicinski

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

maintainer
#3 lore raw
Quoted 7 lines · 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().

Quoted 5 lines

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