Skip to contentSkip to conversation

igc: fix lock order in igc_ptp_reset

Intel-wired-lan Jacob Keller 4 participants 4 messages Apr 22, 2025 – Apr 29, 2025

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

Commit 1a931c4f5e68 ("igc: add lock preventing multiple simultaneous PTM transactions") added a new mutex to protect concurrent PTM transactions. This lock is acquired in igc_ptp_reset() in order to ensure the PTM registers are properly disabled after a device reset.

The flow where the lock is acquired already holds a spinlock, so acquiring a mutex leads to a sleep-while-locking bug, reported both by smatch, and the kernel test robot.

The critical section in igc_ptp_reset() does correctly use the readx_poll_timeout_atomic variants, but the standard PTM flow uses regular sleeping variants. This makes converting the mutex to a spinlock a bit tricky.

Instead, re-order the locking in igc_ptp_reset. Acquire the mutex first, and then the tmreg_lock spinlock. This is safe because there is no other ordering dependency on these locks, as this is the only place where both locks were acquired simultaneously. Indeed, any other flow acquiring locks in that order would be wrong regardless.

---
 drivers/net/ethernet/intel/igc/igc_ptp.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
drivers/net/ethernet/intel/igc/igc_ptp.c +4 -2
index 612ed26a29c5..efc7b30e4211 100644
@@ -1290,6 +1290,8 @@ void igc_ptp_reset(struct igc_adapter *adapter)
 	/* reset the tstamp_config */
 	igc_ptp_set_timestamp_mode(adapter, &adapter->tstamp_config);
 
+	mutex_lock(&adapter->ptm_lock);
+
 	spin_lock_irqsave(&adapter->tmreg_lock, flags);
 
 	switch (adapter->hw.mac.type) {
@@ -1308,7 +1310,6 @@ void igc_ptp_reset(struct igc_adapter *adapter)
 		if (!igc_is_crosststamp_supported(adapter))
 			break;
 
-		mutex_lock(&adapter->ptm_lock);
 		wr32(IGC_PCIE_DIG_DELAY, IGC_PCIE_DIG_DELAY_DEFAULT);
 		wr32(IGC_PCIE_PHY_DELAY, IGC_PCIE_PHY_DELAY_DEFAULT);
 
@@ -1332,7 +1333,6 @@ void igc_ptp_reset(struct igc_adapter *adapter)
 			netdev_err(adapter->netdev, "Timeout reading IGC_PTM_STAT register\n");
 
 		igc_ptm_reset(hw);
-		mutex_unlock(&adapter->ptm_lock);
 		break;
 	default:
 		/* No work to do. */
@@ -1349,5 +1349,7 @@ void igc_ptp_reset(struct igc_adapter *adapter)
 out:
 	spin_unlock_irqrestore(&adapter->tmreg_lock, flags);
 
+	mutex_unlock(&adapter->ptm_lock);
+
 	wrfl();
 }
Signature · 1 line
2.48.1.397.gec9d649cc640
maintainer Reviewed-by
#1 lore raw
Quoted 14 lines · Jacob Keller

Commit 1a931c4f5e68 ("igc: add lock preventing multiple simultaneous PTM transactions") added a new mutex to protect concurrent PTM transactions. This lock is acquired in igc_ptp_reset() in order to ensure the PTM registers are properly disabled after a device reset.

The flow where the lock is acquired already holds a spinlock, so acquiring a mutex leads to a sleep-while-locking bug, reported both by smatch, and the kernel test robot.

The critical section in igc_ptp_reset() does correctly use the readx_poll_timeout_atomic variants, but the standard PTM flow uses regular sleeping variants. This makes converting the mutex to a spinlock a bit tricky.

Quoted 8 lines

Instead, re-order the locking in igc_ptp_reset. Acquire the mutex first, and then the tmreg_lock spinlock. This is safe because there is no other ordering dependency on these locks, as this is the only place where both locks were acquired simultaneously. Indeed, any other flow acquiring locks in that order would be wrong regardless.

Fixes 1a931c4f5e68 ("igc: add lock preventing multiple simultaneous PTM transactions")

Thank you!

Quoted 43 lines
---
  drivers/net/ethernet/intel/igc/igc_ptp.c | 6 ++++--
  1 file changed, 4 insertions(+), 2 deletions(-)
drivers/net/ethernet/intel/igc/igc_ptp.c +4 -2
index 612ed26a29c5..efc7b30e4211 100644
@@ -1290,6 +1290,8 @@ void igc_ptp_reset(struct igc_adapter *adapter)
  	/* reset the tstamp_config */
  	igc_ptp_set_timestamp_mode(adapter, &adapter->tstamp_config);
 
+	mutex_lock(&adapter->ptm_lock);
+
  	spin_lock_irqsave(&adapter->tmreg_lock, flags);
 
  	switch (adapter->hw.mac.type) {
@@ -1308,7 +1310,6 @@ void igc_ptp_reset(struct igc_adapter *adapter)
  		if (!igc_is_crosststamp_supported(adapter))
  			break;
 
-		mutex_lock(&adapter->ptm_lock);
  		wr32(IGC_PCIE_DIG_DELAY, IGC_PCIE_DIG_DELAY_DEFAULT);
  		wr32(IGC_PCIE_PHY_DELAY, IGC_PCIE_PHY_DELAY_DEFAULT);
 
@@ -1332,7 +1333,6 @@ void igc_ptp_reset(struct igc_adapter *adapter)
  			netdev_err(adapter->netdev, "Timeout reading IGC_PTM_STAT register\n");
 
  		igc_ptm_reset(hw);
-		mutex_unlock(&adapter->ptm_lock);
  		break;
  	default:
  		/* No work to do. */
@@ -1349,5 +1349,7 @@ void igc_ptp_reset(struct igc_adapter *adapter)
  out:
  	spin_unlock_irqrestore(&adapter->tmreg_lock, flags);
 
+	mutex_unlock(&adapter->ptm_lock);
+
  	wrfl();
  }
Reviewed-by
#2 lore raw
Quoted 21 lines · Przemek Kitszel
Quoted 20 lines · Jacob Keller

Commit 1a931c4f5e68 ("igc: add lock preventing multiple simultaneous PTM transactions") added a new mutex to protect concurrent PTM transactions. This lock is acquired in igc_ptp_reset() in order to ensure the PTM registers are properly disabled after a device reset.

The flow where the lock is acquired already holds a spinlock, so acquiring a mutex leads to a sleep-while-locking bug, reported both by smatch, and the kernel test robot.

The critical section in igc_ptp_reset() does correctly use the readx_poll_timeout_atomic variants, but the standard PTM flow uses regular sleeping variants. This makes converting the mutex to a spinlock a bit tricky.

Instead, re-order the locking in igc_ptp_reset. Acquire the mutex first, and then the tmreg_lock spinlock. This is safe because there is no other ordering dependency on these locks, as this is the only place where both locks were acquired simultaneously. Indeed, any other flow acquiring locks in that order would be wrong regardless.

Quoted 5 lines
Quoted 2 lines
Fixes 1a931c4f5e68 ("igc: add lock preventing multiple simultaneous PTM transactions")

Thank you!

Thanks!

Quoted 39 lines
Quoted 38 lines

---
  drivers/net/ethernet/intel/igc/igc_ptp.c | 6 ++++--

  1 file changed, 4 insertions(+), 2 deletions(-)
drivers/net/ethernet/intel/igc/igc_ptp.c +1 -0
index 612ed26a29c5..efc7b30e4211 100644
@@ -1290,6 +1290,8 @@ void igc_ptp_reset(struct igc_adapter *adapter)
       /* reset the tstamp_config */
       igc_ptp_set_timestamp_mode(adapter, &adapter->tstamp_config);
   +    mutex_lock(&adapter->ptm_lock);
+
       spin_lock_irqsave(&adapter->tmreg_lock, flags);
         switch (adapter->hw.mac.type) {
@@ -1308,7 +1310,6 @@ void igc_ptp_reset(struct igc_adapter *adapter)
           if (!igc_is_crosststamp_supported(adapter))
               break;
   -        mutex_lock(&adapter->ptm_lock);
           wr32(IGC_PCIE_DIG_DELAY, IGC_PCIE_DIG_DELAY_DEFAULT);
           wr32(IGC_PCIE_PHY_DELAY, IGC_PCIE_PHY_DELAY_DEFAULT);
   @@ -1332,7 +1333,6 @@ void igc_ptp_reset(struct igc_adapter *adapter)

              netdev_err(adapter->netdev, "Timeout reading IGC_PTM_STAT register\n"); igc_ptm_reset(hw);
-        mutex_unlock(&adapter->ptm_lock);
          break;
      default:
          /* No work to do. */

drivers/net/ethernet/intel/igc/igc_ptp.c +0 -0
@@ -1349,5 +1349,7 @@ void igc_ptp_reset(struct igc_adapter *adapter)

  out:
      spin_unlock_irqrestore(&adapter->tmreg_lock, flags); +    mutex_unlock(&adapter->ptm_lock);
+
      wrfl();
  }

Tested-by
#3 lore raw
In reply to Jacob Keller
Quoted 26 lines · Jacob Keller

Commit 1a931c4f5e68 ("igc: add lock preventing multiple simultaneous PTM transactions") added a new mutex to protect concurrent PTM transactions. This lock is acquired in igc_ptp_reset() in order to ensure the PTM registers are properly disabled after a device reset.

The flow where the lock is acquired already holds a spinlock, so acquiring a mutex leads to a sleep-while-locking bug, reported both by smatch, and the kernel test robot.

The critical section in igc_ptp_reset() does correctly use the readx_poll_timeout_atomic variants, but the standard PTM flow uses regular sleeping variants. This makes converting the mutex to a spinlock a bit tricky.

Instead, re-order the locking in igc_ptp_reset. Acquire the mutex first, and then the tmreg_lock spinlock. This is safe because there is no other ordering dependency on these locks, as this is the only place where both locks were acquired simultaneously. Indeed, any other flow acquiring locks in that order would be wrong regardless.

Quoted 4 lines
---
  drivers/net/ethernet/intel/igc/igc_ptp.c | 6 ++++--
  1 file changed, 4 insertions(+), 2 deletions(-)