From: Cunhao Lu <1579567540@qq.com> The bus-off IRQ cancels timestamp work asynchronously. If the worker is already running, it can requeue itself after cancellation and keep polling during bus-off, causing unnecessary wakeups and counter reads. Use disable_delayed_work() to block requeueing on bus-off. Initialize the work disabled and enable it after initializing the timecounter. Keep the active check and counter read under timestamp_lock so stop waits for an in-flight read before the controller is reset. Use the active flag to avoid disabling work twice when restart or close follows bus-off. Keep workqueue operations outside timestamp_lock; the IRQ and restart paths already serialize start and stop. Synchronous stop waits for the callback before reinitialization or clock shutdown. Fixes: 4e1a18bab124 ("can: rockchip_canfd: add hardware timestamping support") Cc: stable@vger.kernel.org Signed-off-by: Cunhao Lu <1579567540@qq.com> --- Changes in v9: - Use disable_delayed_work() to block self-requeueing, with balanced enable/disable calls across repeated stops. - Move worker requeueing and initial scheduling outside timestamp_lock. --- .../net/can/rockchip/rockchip_canfd-timestamp.c | 30 ++++++++++++++++++++-- drivers/net/can/rockchip/rockchip_canfd.h | 3 +++ 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/drivers/net/can/rockchip/rockchip_canfd-timestamp.c b/drivers/net/can/rockchip/rockchip_canfd-timestamp.c index ad582afbbd68..7c7b95343aa8 100644 --- a/drivers/net/can/rockchip/rockchip_canfd-timestamp.c +++ b/drivers/net/can/rockchip/rockchip_canfd-timestamp.c @@ -30,18 +30,26 @@ static void rkcanfd_timestamp_work(struct work_struct *work) { const struct delayed_work *delayed_work = to_delayed_work(work); struct rkcanfd_priv *priv; + unsigned long flags; priv = container_of(delayed_work, struct rkcanfd_priv, timestamp); - timecounter_read(&priv->tc); + spin_lock_irqsave(&priv->timestamp_lock, flags); + if (priv->timestamp_active) + timecounter_read(&priv->tc); + spin_unlock_irqrestore(&priv->timestamp_lock, flags); schedule_delayed_work(&priv->timestamp, priv->work_delay_jiffies); } void rkcanfd_timestamp_work_init(struct rkcanfd_priv *priv) { + spin_lock_init(&priv->timestamp_lock); + priv->timestamp_active = false; INIT_DELAYED_WORK(&priv->timestamp, rkcanfd_timestamp_work); + disable_delayed_work(&priv->timestamp); } +/* The caller must quiesce IRQs and timestamp work before reconfiguration. */ void rkcanfd_timestamp_init(struct rkcanfd_priv *priv) { const struct can_bittiming *dbt = &priv->can.fd.data_bittiming; @@ -93,17 +101,35 @@ void rkcanfd_timestamp_init(struct rkcanfd_priv *priv) void rkcanfd_timestamp_start(struct rkcanfd_priv *priv) { + unsigned long flags; + + spin_lock_irqsave(&priv->timestamp_lock, flags); timecounter_init(&priv->tc, &priv->cc, ktime_get_real_ns()); + priv->timestamp_active = true; + spin_unlock_irqrestore(&priv->timestamp_lock, flags); + enable_delayed_work(&priv->timestamp); schedule_delayed_work(&priv->timestamp, priv->work_delay_jiffies); } void rkcanfd_timestamp_stop(struct rkcanfd_priv *priv) { - cancel_delayed_work(&priv->timestamp); + unsigned long flags; + + /* Stop counter reads before the caller resets the controller. */ + spin_lock_irqsave(&priv->timestamp_lock, flags); + if (!priv->timestamp_active) { + spin_unlock_irqrestore(&priv->timestamp_lock, flags); + return; + } + priv->timestamp_active = false; + spin_unlock_irqrestore(&priv->timestamp_lock, flags); + + disable_delayed_work(&priv->timestamp); } void rkcanfd_timestamp_stop_sync(struct rkcanfd_priv *priv) { + rkcanfd_timestamp_stop(priv); cancel_delayed_work_sync(&priv->timestamp); } diff --git a/drivers/net/can/rockchip/rockchip_canfd.h b/drivers/net/can/rockchip/rockchip_canfd.h index 07fc5bc605df..9fa0327b3e74 100644 --- a/drivers/net/can/rockchip/rockchip_canfd.h +++ b/drivers/net/can/rockchip/rockchip_canfd.h @@ -477,6 +477,8 @@ struct rkcanfd_priv { /* Serialize TX submission/completion with bus-off and restart. */ spinlock_t tx_lock; + /* Serialize timestamp work with start/stop. */ + spinlock_t timestamp_lock; void __iomem *regs; unsigned int tx_head; unsigned int tx_tail; @@ -487,6 +489,7 @@ struct rkcanfd_priv { bool tx_enabled; bool irq_suspended; + bool timestamp_active; bool tx_retry_enabled; bool tx_retry_pending; -- 2.34.1