Commit b353b241 authored by Csókás Bence's avatar Csókás Bence Committed by Jakub Kicinski
Browse files

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



Mutexes cannot be taken in a non-preemptible context,
causing a panic in `fec_ptp_save_state()`. Replacing
`ptp_clk_mutex` by `tmreg_lock` fixes this.

Fixes: 6a4d7234 ("net: fec: ptp: avoid register access when ipg clock is disabled")
Fixes: f7995922 ("fec: Restart PPS after link state change")
Reported-by: default avatarMarc Kleine-Budde <mkl@pengutronix.de>
Link: https://lore.kernel.org/all/20220827160922.642zlcd5foopozru@pengutronix.de/


Signed-off-by: default avatarCsókás Bence <csokas.bence@prolan.hu>
Tested-by: Francesco Dolcini <francesco.dolcini@toradex.com> # Toradex Apalis iMX6
Link: https://lore.kernel.org/r/20220901140402.64804-1-csokas.bence@prolan.hu


Signed-off-by: default avatarJakub Kicinski <kuba@kernel.org>
parent 7d650df9
Loading
Loading
Loading
Loading
+0 −1
Original line number Diff line number Diff line
@@ -561,7 +561,6 @@ 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;

+9 −8
Original line number Diff line number Diff line
@@ -2029,6 +2029,7 @@ 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) {
@@ -2037,15 +2038,15 @@ static int fec_enet_clk_enable(struct net_device *ndev, bool enable)
			return ret;

		if (fep->clk_ptp) {
			mutex_lock(&fep->ptp_clk_mutex);
			spin_lock_irqsave(&fep->tmreg_lock, flags);
			ret = clk_prepare_enable(fep->clk_ptp);
			if (ret) {
				mutex_unlock(&fep->ptp_clk_mutex);
				spin_unlock_irqrestore(&fep->tmreg_lock, flags);
				goto failed_clk_ptp;
			} else {
				fep->ptp_clk_on = true;
			}
			mutex_unlock(&fep->ptp_clk_mutex);
			spin_unlock_irqrestore(&fep->tmreg_lock, flags);
		}

		ret = clk_prepare_enable(fep->clk_ref);
@@ -2060,10 +2061,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) {
			mutex_lock(&fep->ptp_clk_mutex);
			spin_lock_irqsave(&fep->tmreg_lock, flags);
			clk_disable_unprepare(fep->clk_ptp);
			fep->ptp_clk_on = false;
			mutex_unlock(&fep->ptp_clk_mutex);
			spin_unlock_irqrestore(&fep->tmreg_lock, flags);
		}
		clk_disable_unprepare(fep->clk_ref);
		clk_disable_unprepare(fep->clk_2x_txclk);
@@ -2076,10 +2077,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) {
		mutex_lock(&fep->ptp_clk_mutex);
		spin_lock_irqsave(&fep->tmreg_lock, flags);
		clk_disable_unprepare(fep->clk_ptp);
		fep->ptp_clk_on = false;
		mutex_unlock(&fep->ptp_clk_mutex);
		spin_unlock_irqrestore(&fep->tmreg_lock, flags);
	}
failed_clk_ptp:
	clk_disable_unprepare(fep->clk_enet_out);
@@ -3914,7 +3915,7 @@ fec_probe(struct platform_device *pdev)
	}

	fep->ptp_clk_on = false;
	mutex_init(&fep->ptp_clk_mutex);
	spin_lock_init(&fep->tmreg_lock);

	/* clk_ref is optional, depends on board */
	fep->clk_ref = devm_clk_get_optional(&pdev->dev, "enet_clk_ref");
+10 −18
Original line number Diff line number Diff line
@@ -365,21 +365,19 @@ 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 *adapter =
	struct fec_enet_private *fep =
	    container_of(ptp, struct fec_enet_private, ptp_caps);
	u64 ns;
	unsigned long flags;

	mutex_lock(&adapter->ptp_clk_mutex);
	spin_lock_irqsave(&fep->tmreg_lock, flags);
	/* Check the ptp clock */
	if (!adapter->ptp_clk_on) {
		mutex_unlock(&adapter->ptp_clk_mutex);
	if (!fep->ptp_clk_on) {
		spin_unlock_irqrestore(&fep->tmreg_lock, flags);
		return -EINVAL;
	}
	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);
	ns = timecounter_read(&fep->tc);
	spin_unlock_irqrestore(&fep->tmreg_lock, flags);

	*ts = ns_to_timespec64(ns);

@@ -404,10 +402,10 @@ static int fec_ptp_settime(struct ptp_clock_info *ptp,
	unsigned long flags;
	u32 counter;

	mutex_lock(&fep->ptp_clk_mutex);
	spin_lock_irqsave(&fep->tmreg_lock, flags);
	/* Check the ptp clock */
	if (!fep->ptp_clk_on) {
		mutex_unlock(&fep->ptp_clk_mutex);
		spin_unlock_irqrestore(&fep->tmreg_lock, flags);
		return -EINVAL;
	}

@@ -417,11 +415,9 @@ 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;
}

@@ -518,13 +514,11 @@ 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;

	mutex_lock(&fep->ptp_clk_mutex);
	if (fep->ptp_clk_on) {
	spin_lock_irqsave(&fep->tmreg_lock, flags);
	if (fep->ptp_clk_on) {
		timecounter_read(&fep->tc);
		spin_unlock_irqrestore(&fep->tmreg_lock, flags);
	}
	mutex_unlock(&fep->ptp_clk_mutex);
	spin_unlock_irqrestore(&fep->tmreg_lock, flags);

	schedule_delayed_work(&fep->time_keep, HZ);
}
@@ -599,8 +593,6 @@ 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);