Skip to contentSkip to conversation

Revert "net: fec: Use a spinlock to guard `fep->ptp_clk_on`"

PATCH In review Bence Csókás 3 participants 8 messages Sep 12, 2022 – Sep 14, 2022

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

PATCH 1/2 Revert "net: fec: Use a spinlock to guard `fep->ptp_clk_on`"

From: Csókás Bence <[email protected]>

clk_prepare_enable() gets called in atomic context (holding a spinlock), which may sleep, causing a BUG on certain platforms.

This reverts commit b353b241f1eb9b6265358ffbe2632fdcb563354f.

---
 drivers/net/ethernet/freescale/fec.h      |  1 +
 drivers/net/ethernet/freescale/fec_main.c | 17 ++++++++---------
 drivers/net/ethernet/freescale/fec_ptp.c  | 20 ++++++++++++++------
 3 files changed, 23 insertions(+), 15 deletions(-)
drivers/net/ethernet/freescale/fec.h +1 -0
index 99cfe6ab41fc..b7b8c4ddac5b 100644
@@ -563,6 +563,7 @@ struct fec_enet_private {
 	struct clk *clk_2x_txclk;
 
 	bool ptp_clk_on;
+	struct mutex ptp_clk_mutex;
 	unsigned int num_tx_queues;
 	unsigned int num_rx_queues;
 
drivers/net/ethernet/freescale/fec_main.c +8 -9
index ad01db156972..f5ba133468f5 100644
@@ -2059,7 +2059,6 @@ static void fec_enet_phy_reset_after_clk_enable(struct net_device *ndev)
 static int fec_enet_clk_enable(struct net_device *ndev, bool enable)
 {
 	struct fec_enet_private *fep = netdev_priv(ndev);
-	unsigned long flags;
 	int ret;
 
 	if (enable) {
@@ -2068,15 +2067,15 @@ static int fec_enet_clk_enable(struct net_device *ndev, bool enable)
 			return ret;
 
 		if (fep->clk_ptp) {
-			spin_lock_irqsave(&fep->tmreg_lock, flags);
+			mutex_lock(&fep->ptp_clk_mutex);
 			ret = clk_prepare_enable(fep->clk_ptp);
 			if (ret) {
-				spin_unlock_irqrestore(&fep->tmreg_lock, flags);
+				mutex_unlock(&fep->ptp_clk_mutex);
 				goto failed_clk_ptp;
 			} else {
 				fep->ptp_clk_on = true;
 			}
-			spin_unlock_irqrestore(&fep->tmreg_lock, flags);
+			mutex_unlock(&fep->ptp_clk_mutex);
 		}
 
 		ret = clk_prepare_enable(fep->clk_ref);
@@ -2091,10 +2090,10 @@ static int fec_enet_clk_enable(struct net_device *ndev, bool enable)
 	} else {
 		clk_disable_unprepare(fep->clk_enet_out);
 		if (fep->clk_ptp) {
-			spin_lock_irqsave(&fep->tmreg_lock, flags);
+			mutex_lock(&fep->ptp_clk_mutex);
 			clk_disable_unprepare(fep->clk_ptp);
 			fep->ptp_clk_on = false;
-			spin_unlock_irqrestore(&fep->tmreg_lock, flags);
+			mutex_unlock(&fep->ptp_clk_mutex);
 		}
 		clk_disable_unprepare(fep->clk_ref);
 		clk_disable_unprepare(fep->clk_2x_txclk);
@@ -2107,10 +2106,10 @@ static int fec_enet_clk_enable(struct net_device *ndev, bool enable)
 		clk_disable_unprepare(fep->clk_ref);
 failed_clk_ref:
 	if (fep->clk_ptp) {
-		spin_lock_irqsave(&fep->tmreg_lock, flags);
+		mutex_lock(&fep->ptp_clk_mutex);
 		clk_disable_unprepare(fep->clk_ptp);
 		fep->ptp_clk_on = false;
-		spin_unlock_irqrestore(&fep->tmreg_lock, flags);
+		mutex_unlock(&fep->ptp_clk_mutex);
 	}
 failed_clk_ptp:
 	clk_disable_unprepare(fep->clk_enet_out);
@@ -3949,7 +3948,7 @@ fec_probe(struct platform_device *pdev)
 	}
 
 	fep->ptp_clk_on = false;
-	spin_lock_init(&fep->tmreg_lock);
+	mutex_init(&fep->ptp_clk_mutex);
 
 	/* clk_ref is optional, depends on board */
 	fep->clk_ref = devm_clk_get_optional(&pdev->dev, "enet_clk_ref");
drivers/net/ethernet/freescale/fec_ptp.c +14 -6
index 7be97ab84e50..ae2c786dc130 100644
@@ -370,14 +370,16 @@ static int fec_ptp_gettime(struct ptp_clock_info *ptp, struct timespec64 *ts)
 	u64 ns;
 	unsigned long flags;
 
-	spin_lock_irqsave(&fep->tmreg_lock, flags);
+	mutex_lock(&fep->ptp_clk_mutex);
 	/* Check the ptp clock */
 	if (!fep->ptp_clk_on) {
-		spin_unlock_irqrestore(&fep->tmreg_lock, flags);
+		mutex_unlock(&fep->ptp_clk_mutex);
 		return -EINVAL;
 	}
+	spin_lock_irqsave(&fep->tmreg_lock, flags);
 	ns = timecounter_read(&fep->tc);
 	spin_unlock_irqrestore(&fep->tmreg_lock, flags);
+	mutex_unlock(&fep->ptp_clk_mutex);
 
 	*ts = ns_to_timespec64(ns);
 
@@ -402,10 +404,10 @@ static int fec_ptp_settime(struct ptp_clock_info *ptp,
 	unsigned long flags;
 	u32 counter;
 
-	spin_lock_irqsave(&fep->tmreg_lock, flags);
+	mutex_lock(&fep->ptp_clk_mutex);
 	/* Check the ptp clock */
 	if (!fep->ptp_clk_on) {
-		spin_unlock_irqrestore(&fep->tmreg_lock, flags);
+		mutex_unlock(&fep->ptp_clk_mutex);
 		return -EINVAL;
 	}
 
@@ -415,9 +417,11 @@ static int fec_ptp_settime(struct ptp_clock_info *ptp,
 	 */
 	counter = ns & fep->cc.mask;
 
+	spin_lock_irqsave(&fep->tmreg_lock, flags);
 	writel(counter, fep->hwp + FEC_ATIME);
 	timecounter_init(&fep->tc, &fep->cc, ns);
 	spin_unlock_irqrestore(&fep->tmreg_lock, flags);
+	mutex_unlock(&fep->ptp_clk_mutex);
 	return 0;
 }
 
@@ -514,11 +518,13 @@ static void fec_time_keep(struct work_struct *work)
 	struct fec_enet_private *fep = container_of(dwork, struct fec_enet_private, time_keep);
 	unsigned long flags;
 
-	spin_lock_irqsave(&fep->tmreg_lock, flags);
+	mutex_lock(&fep->ptp_clk_mutex);
 	if (fep->ptp_clk_on) {
+		spin_lock_irqsave(&fep->tmreg_lock, flags);
 		timecounter_read(&fep->tc);
+		spin_unlock_irqrestore(&fep->tmreg_lock, flags);
 	}
-	spin_unlock_irqrestore(&fep->tmreg_lock, flags);
+	mutex_unlock(&fep->ptp_clk_mutex);
 
 	schedule_delayed_work(&fep->time_keep, HZ);
 }
@@ -593,6 +599,8 @@ void fec_ptp_init(struct platform_device *pdev, int irq_idx)
 	}
 	fep->ptp_inc = NSEC_PER_SEC / fep->cycle_speed;
 
+	spin_lock_init(&fep->tmreg_lock);
+
 	fec_ptp_start_cyclecounter(ndev);
 
 	INIT_DELAYED_WORK(&fep->time_keep, fec_time_keep);
Signature · 1 line
2.37.3
#1 lore raw

PATCH 2/2 net: fec: Use unlocked timecounter reads for saving state

From: Csókás Bence <[email protected]>

fec_ptp_save_state() may be called from an atomic context, which makes fec_ptp_gettime() unable to acquire a mutex. Using the lower-level timecounter ops remedies the problem.

---
 drivers/net/ethernet/freescale/fec.h     |  4 ++--
 drivers/net/ethernet/freescale/fec_ptp.c | 16 ++++++++++------
 2 files changed, 12 insertions(+), 8 deletions(-)
drivers/net/ethernet/freescale/fec.h +2 -2
index b7b8c4ddac5b..3797988432e0 100644
@@ -642,7 +642,7 @@ struct fec_enet_private {
 	unsigned int next_counter;
 
 	struct {
-		struct timespec64 ts_phc;
+		u64 ns_phc;
 		u64 ns_sys;
 		u32 at_corr;
 		u8 at_inc_corr;
@@ -661,7 +661,7 @@ int fec_ptp_set(struct net_device *ndev, struct ifreq *ifr);
 int fec_ptp_get(struct net_device *ndev, struct ifreq *ifr);
 
 void fec_ptp_save_state(struct fec_enet_private *fep);
-int fec_ptp_restore_state(struct fec_enet_private *fep);
+void fec_ptp_restore_state(struct fec_enet_private *fep);
 
 /****************************************************************************/
 #endif /* FEC_H */
drivers/net/ethernet/freescale/fec_ptp.c +10 -6
index ae2c786dc130..f1a8eb8c888b 100644
@@ -643,26 +643,30 @@ void fec_ptp_stop(struct platform_device *pdev)
 
 void fec_ptp_save_state(struct fec_enet_private *fep)
 {
+	unsigned long flags;
 	u32 atime_inc_corr;
 
-	fec_ptp_gettime(&fep->ptp_caps, &fep->ptp_saved_state.ts_phc);
+	spin_lock_irqsave(&fep->tmreg_lock, flags);
+	fep->ptp_saved_state.ns_phc = timecounter_read(&fep->tc);
 	fep->ptp_saved_state.ns_sys = ktime_get_ns();
+	spin_unlock_irqrestore(&fep->tmreg_lock, flags);
 
 	fep->ptp_saved_state.at_corr = readl(fep->hwp + FEC_ATIME_CORR);
 	atime_inc_corr = readl(fep->hwp + FEC_ATIME_INC) & FEC_T_INC_CORR_MASK;
 	fep->ptp_saved_state.at_inc_corr = (u8)(atime_inc_corr >> FEC_T_INC_CORR_OFFSET);
 }
 
-int fec_ptp_restore_state(struct fec_enet_private *fep)
+void fec_ptp_restore_state(struct fec_enet_private *fep)
 {
 	u32 atime_inc = readl(fep->hwp + FEC_ATIME_INC) & FEC_T_INC_MASK;
-	u64 ns_sys;
+	u64 ns;
 
 	writel(fep->ptp_saved_state.at_corr, fep->hwp + FEC_ATIME_CORR);
 	atime_inc |= ((u32)fep->ptp_saved_state.at_inc_corr) << FEC_T_INC_CORR_OFFSET;
 	writel(atime_inc, fep->hwp + FEC_ATIME_INC);
 
-	ns_sys = ktime_get_ns() - fep->ptp_saved_state.ns_sys;
-	timespec64_add_ns(&fep->ptp_saved_state.ts_phc, ns_sys);
-	return fec_ptp_settime(&fep->ptp_caps, &fep->ptp_saved_state.ts_phc);
+	ns = ktime_get_ns() - fep->ptp_saved_state.ns_sys + fep->ptp_saved_state.ns_phc;
+
+	writel(ns & fep->cc.mask, fep->hwp + FEC_ATIME);
+	timecounter_init(&fep->tc, &fep->cc, ns);
 }
Signature · 1 line
2.37.3
#3 lore raw

Re: [PATCH v3 2/2] net: fec: Use unlocked timecounter reads for saving state

From: Bence Csókás <[email protected]>

Quoted 4 lines

From: Csókás Bence <[email protected]>

Please thoroughly test this, as I am still
out-of-office, and cannot test on hardware.

(Sorry for the confusion, I am unable to access the company mail server with git send-email remotely, so I had to send it via another serer. But it is still me.)

#4 lore raw
In reply to Bence Csókás
Quoted 10 lines · Bence Csókás

From: Csókás Bence <[email protected]>

clk_prepare_enable() gets called in atomic context (holding a spinlock), which may sleep, causing a BUG on certain platforms.

This reverts commit b353b241f1eb9b6265358ffbe2632fdcb563354f.

This is not a 100% revert.

In b353b241f1eb ("net: fec: Use a spinlock to guard fep->ptp_clk_on") the "struct fec_enet_private *fep" was renamed to "struct fec_enet_private *adapter" for no apparent reason. The driver uses "fep" everywhere else. This revert doesn't restore the original state.

This leads to the following diff against a 100% revert.

drivers/net/ethernet/freescale/fec_ptp.c +8 -8
index c99dff3c3422..c74d04f4b2fd 100644
@@ -365,21 +365,21 @@ static int fec_ptp_adjtime(struct ptp_clock_info *ptp, s64 delta)
  */
 static int fec_ptp_gettime(struct ptp_clock_info *ptp, struct timespec64 *ts)
 {
-       struct fec_enet_private *fep =
+       struct fec_enet_private *adapter =
            container_of(ptp, struct fec_enet_private, ptp_caps);
        u64 ns;
        unsigned long flags;
 
-       mutex_lock(&fep->ptp_clk_mutex);
+       mutex_lock(&adapter->ptp_clk_mutex);
        /* Check the ptp clock */
-       if (!fep->ptp_clk_on) {
-               mutex_unlock(&fep->ptp_clk_mutex);
+       if (!adapter->ptp_clk_on) {
+               mutex_unlock(&adapter->ptp_clk_mutex);
                return -EINVAL;
        }
-       spin_lock_irqsave(&fep->tmreg_lock, flags);
-       ns = timecounter_read(&fep->tc);
-       spin_unlock_irqrestore(&fep->tmreg_lock, flags);
-       mutex_unlock(&fep->ptp_clk_mutex);
+       spin_lock_irqsave(&adapter->tmreg_lock, flags);
+       ns = timecounter_read(&adapter->tc);
+       spin_unlock_irqrestore(&adapter->tmreg_lock, flags);
+       mutex_unlock(&adapter->ptp_clk_mutex);
 
        *ts = ns_to_timespec64(ns);
 

regards,
Marc

Signature · 4 lines
Pengutronix e.K.                 | Marc Kleine-Budde           |
Embedded Linux                   | https://www.pengutronix.de  |
Vertretung West/Dortmund         | Phone: +49-231-2826-924     |
Amtsgericht Hildesheim, HRA 2686 | Fax:   +49-5121-206917-5555 |
#5 lore raw
Quoted 6 lines · Marc Kleine-Budde
Quoted 5 lines · Bence Csókás

From: Csókás Bence <[email protected]>

clk_prepare_enable() gets called in atomic context (holding a spinlock), which may sleep, causing a BUG on certain platforms.

Quoted 7 lines
Quoted 5 lines

This reverts commit b353b241f1eb9b6265358ffbe2632fdcb563354f.

This is not a 100% revert.

Better use the Francesco Dolcini's revert series

| https://lore.kernel.org/all/[email protected]/

instead.

regards,
Marc

Signature · 4 lines
Pengutronix e.K.                 | Marc Kleine-Budde           |
Embedded Linux                   | https://www.pengutronix.de  |
Vertretung West/Dortmund         | Phone: +49-231-2826-924     |
Amtsgericht Hildesheim, HRA 2686 | Fax:   +49-5121-206917-5555 |
#6 lore raw
In reply to Marc Kleine-Budde
Quoted 6 lines

This is not a 100% revert.

In b353b241f1eb ("net: fec: Use a spinlock to guard fep->ptp_clk_on") the "struct fec_enet_private *fep" was renamed to "struct fec_enet_private *adapter" for no apparent reason. The driver uses "fep" everywhere else. This revert doesn't restore the original state.

You got that backwards. b353b241f1eb renamed adapter to fep to align it with the rest of the driver. I decided to amend the revert to keep this renaming. adapter was introduced in 6605b73 when fec_ptp.c was created.

drivers/net/ethernet/freescale/fec_ptp.c static int fec_ptp_adjtime(struct ptp_clock_info *ptp, s64 delta) · line 368

Quoted 12 lines

This leads to the following diff against a 100% revert.

drivers/net/ethernet/freescale/fec_ptp.c +1 -1
index c99dff3c3422..c74d04f4b2fd 100644
@@ -365,21 +365,21 @@ static int fec_ptp_adjtime(struct ptp_clock_info *ptp, s64 delta)
   */
  static int fec_ptp_gettime(struct ptp_clock_info *ptp, struct timespec64 *ts)
  {
-       struct fec_enet_private *fep =
+       struct fec_enet_private *adapter =

Here you can clearly see the nature of my amend. I thought I added this to the commit message, but it seems I have forgot.

Bence

#7 lore raw
Quoted 9 lines · Csókás Bence
Quoted 6 lines

This is not a 100% revert.

In b353b241f1eb ("net: fec: Use a spinlock to guard fep->ptp_clk_on") the "struct fec_enet_private *fep" was renamed to "struct fec_enet_private *adapter" for no apparent reason. The driver uses "fep" everywhere else. This revert doesn't restore the original state.

You got that backwards. b353b241f1eb renamed adapter to fep to align it with the rest of the driver.

Doh! You're right - sorry about that.

Quoted 5 lines

I decided to amend the revert to keep this renaming. adapter was introduced in 6605b73 when fec_ptp.c was created.

Quoted 2 lines

This leads to the following diff against a 100% revert.

Quoted 12 lines

drivers/net/ethernet/freescale/fec_ptp.c static int fec_ptp_adjtime(struct ptp_clock_info *ptp, s64 delta) · line 368

Quoted 10 lines
drivers/net/ethernet/freescale/fec_ptp.c +1 -1
index c99dff3c3422..c74d04f4b2fd 100644
@@ -365,21 +365,21 @@ static int fec_ptp_adjtime(struct ptp_clock_info *ptp, s64 delta)
   */
  static int fec_ptp_gettime(struct ptp_clock_info *ptp, struct timespec64 *ts)
  {
-       struct fec_enet_private *fep =
+       struct fec_enet_private *adapter =

Here you can clearly see the nature of my amend.

Please don't mix fixes and especially reverts with cleanups.

Quoted 2 lines

I thought I added this to the commit message,
but it seems I have forgot.

regards,
Marc

Signature · 4 lines
Pengutronix e.K.                 | Marc Kleine-Budde           |
Embedded Linux                   | https://www.pengutronix.de  |
Vertretung West/Dortmund         | Phone: +49-231-2826-924     |
Amtsgericht Hildesheim, HRA 2686 | Fax:   +49-5121-206917-5555 |