* [PATCH net v10 00/10] can: fix Rockchip TX stalls and bus-off restart/close races
@ 2026-11-19 8:39 Cunhao Lu via B4 Relay
2026-11-19 8:39 ` [PATCH net v10 01/10] can: rockchip_canfd: prevent TX stall on echo skb failure Cunhao Lu via B4 Relay
` (9 more replies)
0 siblings, 10 replies; 15+ messages in thread
From: Cunhao Lu via B4 Relay @ 2026-11-19 8:39 UTC (permalink / raw)
To: Marc Kleine-Budde, kernel, Vincent Mailhol, Heiko Stuebner,
Oliver Hartkopp, David S. Miller, Eric Dumazet, Jakub Kicinski,
Paolo Abeni, Simon Horman, Jonathan Corbet, Shuah Khan,
Randy Dunlap, Wolfgang Grandegger
Cc: linux-can, linux-arm-kernel, linux-rockchip, linux-kernel,
linux-doc, Cunhao Lu, stable
On a production robot using a LubanCat-5IO with an RK3588, the CAN TX
queue remained stopped while RX continued, and subsequent sends returned
ENOBUFS. The interface still reported ERROR-ACTIVE, with neither a
hardware bus-off condition nor a pending TX request.
During CAN traffic, the driver also repeatedly reported an inconsistent
TX echo state:
rockchip_canfd fea60000.can can0: rkcanfd_tx_tail_is_eff:
echo_skb[0]=NULL tx_head=0x00060f7d tx_tail=0x00060f7c
The Rockchip CAN-FD driver completes transmissions by matching
self-received frames with entries in the echo skb ring. A race between its
transmit and completion paths can expose a partially updated echo entry,
dereference an skb queued for NAPI, or stop the TX queue without a later
completion to wake it. If the driver cannot install an echo skb, it now
drops the frame before issuing a TX request. Retries use the outstanding
frame's slot and block new TX until both request bits clear, even if the
original echo arrives first.
While testing the echo-loss fix with fault injection, I discovered a
separate race between bus-off restart and device close.
Rockchip currently cancels restart_work in close_candev(), after freeing
the IRQ, disabling RX offload and stopping the controller. A concurrent
restart can re-enable the controller and interrupts after IRQ teardown,
and requeue timestamp work that accesses registers after runtime suspend
has disabled the controller clocks.
The CAN core skips scheduling or starting recovery when its
netif_running() check observes the interface down. These checks apply to
CAN core software recovery and do not serialize it with device close.
Rockchip waits for active core restart work before freeing the IRQ and
stopping the controller. The final close_candev() call still cancels
restart work and flushes the echo ring.
Rockchip restart masks the device's interrupts and drains active IRQ
handlers without disabling the shared interrupt line. It also drains
timestamp work before reinitializing the controller and timecounter.
The reset path restores and flushes the interrupt mask immediately after
reset deassertion. The Rockchip IRQ handler stays suspended until
initialization is complete.
Disabling timestamp work prevents workers from rearming after bus-off.
Timestamp locking keeps timestamp conversions consistent with timecounter
updates.
The bus-off path drains TX submission and RX-side echo users before
publishing BUS_OFF. Consumed RX FIFO entries are dropped while TX is
disabled to prevent self-transmitted frames from reaching ordinary RX.
Startup serializes carrier restoration, IRQ resumption and queue wake
with a new bus-off. The series also fixes RX offload cleanup when
request_irq() fails during open.
Hardware testing used a LubanCat-5IO running Linux 6.18 PREEMPT and
PREEMPT_RT kernels with v7 and the applicable prerequisites backported.
Tests on each kernel covered 600 seconds of bidirectional classic CAN
traffic, plus CAN FD and CAN FD+BRS. Local echo and both software and
hardware TX timestamps were also verified.
All 1,000 socket-close iterations passed. Instrumentation captured
1,150 cases where the last TX skb of a closed socket was destroyed,
all after releasing tx_lock and with IRQs enabled.
All 200 injected echo clone failures dropped the frame without
submitting TX, advancing tx_head or adding BQL bytes; subsequent sends
succeeded after removing the injection. BUSY, occupied slots, missing
echo skbs and invalid indices were also tested, along with automatic and
manual recovery from hardware bus-off triggered by bitrate mismatches.
All passed.
Test-only instrumentation captured 20 restart/close and 20 close/FIFO
interleavings. No timestamp worker ran during about 300 seconds after
close, and no WARN/Oops, deadlocks or persistent TX stalls were observed
during CAN testing.
Signed-off-by: Cunhao Lu <1579567540@qq.com>
---
Changes in v10:
- Move the retry fix (v9 03) after TX/IRQ synchronization and block new
submissions after a retry until both TX request bits clear.
- Limit the recovery documentation to CAN core software recovery and
clarify that running checks do not serialize recovery with shutdown.
- Link to v9:
https://patch.msgid.link/20261111-rockchip-canfd-restart-close-v9-0-6643f3825ba3@qq.com
Changes in v9:
- Use workqueue disable/enable operations to prevent timestamp work from
rearming after bus-off, balancing repeated stop calls.
- Move worker requeueing and initial scheduling outside timestamp_lock.
- Restore and flush device interrupt masking immediately after hardware
reset, before the remaining controller configuration.
- Count consumed RX FIFO entries discarded while TX is disabled.
- Link to v8:
https://patch.msgid.link/20261108-rockchip-canfd-restart-close-v8-0-3e7a23fd9eae@qq.com
Changes in v8:
- Merge the previously submitted Rockchip TX fixes, with TX serialization
before the retry fix.
- Move retained-skb handling and the missing-echo guard into the echo
failure fix, using the original skb as the frame-data source.
- Establish the TX/RX critical sections and caller-held helper locking in
the TX serialization patch, covering queue updates, TX diagnostics and
RX pending checks, with assertions for command writes.
- Reuse those critical sections in the bus-off/restart lifecycle patch.
- Rebase onto the latest testing branch in the mkl/linux-can-next repository.
- Link to the Rockchip TX fixes:
https://lore.kernel.org/linux-can/tencent_E25C9BE6689264E4138A464803B810FDFD09@qq.com
- Link to v7:
https://patch.msgid.link/20261106-rockchip-canfd-restart-close-v7-0-accdb11bc845@qq.com
Changes in v7:
- Clarify that running checks do not serialize recovery with close, and
keep the restart-ms example tied to configuration while down.
- Introduce Rockchip restart/IRQ synchronization and TX protection
together, keeping close cancellation and open failure cleanup in
separate patches.
- Build on the CAN skb and Rockchip TX fixes, reusing the TX lock and
adapting failure cleanup to the updated skb ownership contract.
- Link to v6:
https://patch.msgid.link/20261017-rockchip-canfd-restart-close-v6-0-ed605feb4b6e@qq.com
Changes in v6:
- Update the can_bus_off() comment and document the running-interface
requirement for automatic recovery.
- Explain Rockchip's use of the cancellation helper.
- Release the original TX skb after unlocking.
- Abort TX submission if echo preparation fails.
- Link to v5:
https://patch.msgid.link/20261015-rockchip-canfd-restart-close-v5-0-ae77327037c9@qq.com
Changes in v5:
- Drop consumed RX FIFO entries under tx_lock when TX is disabled
instead of only skipping echo filtering.
- Link to v4:
https://patch.msgid.link/20261015-rockchip-canfd-restart-close-v4-0-ad149ff79450@qq.com
Changes in v4:
- Serialize timecounter_cyc2time() with the worker's timecounter_read()
and startup's timecounter_init() using timestamp_lock.
- Update rkcanfd_skb_set_timestamp() to take a non-const private pointer
and document the expanded scope of timestamp_lock.
- Link to v3:
https://patch.msgid.link/20261014-rockchip-canfd-restart-close-v3-0-f0eb41090242@qq.com
Changes in v3:
- Replace disable_irq()/enable_irq() in Rockchip restart with device
interrupt masking, mask readback, irq_suspended and synchronize_irq().
- Use release/acquire ordering when resuming the IRQ handler.
- Initialize timestamp delayed work once at probe and synchronously
cancel it before restart reinitializes the timecounter.
- Balance RX offload enablement when request_irq() fails during open.
- Prevent timestamp work from reading the counter or rearming after
deactivation, with cancellation outside the lock.
- Drain TX submission and RX-side echo users before publishing BUS_OFF,
protecting the core's pre-restart echo flush.
- Serialize carrier restoration, IRQ enablement and queue wake with a new
bus-off; suspend shared IRQ handling during open until startup finishes.
- Link to v2:
https://lore.kernel.org/linux-can/tencent_82C93561D200C84C24608C80049C23EEF80A@qq.com/
Changes in v2:
- Split the series into the common checks, the cancellation helper, and
its use in Rockchip.
- Check netif_running() after the do_set_mode capability check and before
the restart notification, echo cleanup, and carrier update.
- Only queue automatic recovery for a running interface; keep reporting
bus-off and make the log message match the scheduling decision.
- Add close_candev_cancel_restart_sync() in linux/can/dev.h and use it in
close_candev() without changing the existing cleanup order.
- Replace the Rockchip mutex and closing flag with an early call to the
helper. Drop the extra close-path IRQ synchronization and mask readback.
- Retain IRQ exclusion around hardware reinitialization in the restart
callback.
- Link to v1:
https://lore.kernel.org/linux-can/tencent_5802EE88434010229EDF7A53084D994C6106@qq.com/
---
Cunhao Lu (10):
can: rockchip_canfd: prevent TX stall on echo skb failure
can: rockchip_canfd: serialize TX state and command writes
can: dev: check netif_running() before bus-off recovery
can: dev: add close_candev_cancel_restart_sync()
can: rockchip_canfd: cancel bus-off restart before closing the device
can: rockchip_canfd: disable RX offload on open failure
can: rockchip_canfd: serialize TX with bus-off and restart
can: rockchip_canfd: retry the outstanding TX buffer
can: rockchip_canfd: prevent timestamp work from rearming after bus-off
can: rockchip_canfd: serialize timecounter timestamp conversion
Documentation/networking/can.rst | 9 +-
drivers/net/can/dev/dev.c | 17 ++-
drivers/net/can/rockchip/rockchip_canfd-core.c | 79 ++++++++++++-
drivers/net/can/rockchip/rockchip_canfd-rx.c | 30 +++--
.../net/can/rockchip/rockchip_canfd-timestamp.c | 41 ++++++-
drivers/net/can/rockchip/rockchip_canfd-tx.c | 124 +++++++++++++++++++--
drivers/net/can/rockchip/rockchip_canfd.h | 19 +++-
include/linux/can/dev.h | 17 +++
8 files changed, 303 insertions(+), 33 deletions(-)
---
base-commit: 48bcad36a131fa67a29a5ac430aceedf2d6f3d0f
change-id: 20261013-rockchip-canfd-restart-close-ebde5954f1bf
Best regards,
--
Cunhao Lu <1579567540@qq.com>
^ permalink raw reply [flat|nested] 15+ messages in thread
* [PATCH net v10 01/10] can: rockchip_canfd: prevent TX stall on echo skb failure
2026-11-19 8:39 [PATCH net v10 00/10] can: fix Rockchip TX stalls and bus-off restart/close races Cunhao Lu via B4 Relay
@ 2026-11-19 8:39 ` Cunhao Lu via B4 Relay
2026-11-19 8:39 ` [PATCH net v10 02/10] can: rockchip_canfd: serialize TX state and command writes Cunhao Lu via B4 Relay
` (8 subsequent siblings)
9 siblings, 0 replies; 15+ messages in thread
From: Cunhao Lu via B4 Relay @ 2026-11-19 8:39 UTC (permalink / raw)
To: Marc Kleine-Budde, kernel, Vincent Mailhol, Heiko Stuebner,
Oliver Hartkopp, David S. Miller, Eric Dumazet, Jakub Kicinski,
Paolo Abeni, Simon Horman, Jonathan Corbet, Shuah Khan,
Randy Dunlap, Wolfgang Grandegger
Cc: linux-can, linux-arm-kernel, linux-rockchip, linux-kernel,
linux-doc, Cunhao Lu, stable
From: Cunhao Lu <1579567540@qq.com>
rkcanfd_start_xmit() advances tx_head and requests transmission even when
can_put_echo_skb() fails. Completion matches self-received frames against
the echo ring, so an entry without an echo skb cannot be completed and
can leave the TX queue stopped. While that empty entry blocks tx_tail,
frames already read from the RX FIFO fail echo filtering with -ENOMSG.
They are discarded before reaching sockets, including ordinary frames
from other nodes, and the current FIFO drain ends early.
Install the echo skb first. On error or an empty echo slot, drop the
frame before writing TX data or commands, advancing tx_head or adding
BQL bytes.
Pass an extra reference to can_put_echo_skb() and retain the original skb
as the frame-data source until submission finishes. This keeps the data
alive even if echo cleanup removes the installed clone. Release the
retained reference on both success and failure.
Fixes: b6661d73290c ("can: rockchip_canfd: add TX PATH")
Cc: stable@vger.kernel.org
Signed-off-by: Cunhao Lu <1579567540@qq.com>
---
Changes in v8:
- Retain the original skb as the frame-data source and release it after
submission or failure.
- Reject successful echo preparation if no echo skb was installed.
---
drivers/net/can/rockchip/rockchip_canfd-tx.c | 21 +++++++++++++++++----
1 file changed, 17 insertions(+), 4 deletions(-)
diff --git a/drivers/net/can/rockchip/rockchip_canfd-tx.c b/drivers/net/can/rockchip/rockchip_canfd-tx.c
index 12200dcfd3389..420e59b41059c 100644
--- a/drivers/net/can/rockchip/rockchip_canfd-tx.c
+++ b/drivers/net/can/rockchip/rockchip_canfd-tx.c
@@ -117,16 +117,21 @@ netdev_tx_t rkcanfd_start_xmit(struct sk_buff *skb, struct net_device *ndev)
tx_head = rkcanfd_get_tx_head(priv);
reg_cmd = RKCANFD_REG_CMD_TX_REQ(tx_head);
+ /* Retain the original skb as the frame-data source even if the
+ * echo skb is removed before submission finishes.
+ */
+ frame_len = can_skb_get_frame_len(skb);
+ err = can_put_echo_skb(skb_get(skb), ndev, tx_head, frame_len);
+ if (unlikely(err || !priv->can.echo_skb[tx_head]))
+ goto out_drop;
+
rkcanfd_write(priv, RKCANFD_REG_FD_TXFRAMEINFO, reg_frameinfo);
rkcanfd_write(priv, RKCANFD_REG_FD_TXID, reg_id);
for (i = 0; i < cfd->len; i += 4)
rkcanfd_write(priv, RKCANFD_REG_FD_TXDATA0 + i,
*(u32 *)(cfd->data + i));
- frame_len = can_skb_get_frame_len(skb);
- err = can_put_echo_skb(skb, ndev, tx_head, frame_len);
- if (!err)
- netdev_sent_queue(priv->ndev, frame_len);
+ netdev_sent_queue(priv->ndev, frame_len);
WRITE_ONCE(priv->tx_head, priv->tx_head + 1);
@@ -137,6 +142,14 @@ netdev_tx_t rkcanfd_start_xmit(struct sk_buff *skb, struct net_device *ndev)
RKCANFD_TX_STOP_THRESHOLD,
RKCANFD_TX_START_THRESHOLD);
+ dev_consume_skb_any(skb);
+
+ return NETDEV_TX_OK;
+
+out_drop:
+ ndev->stats.tx_dropped++;
+ dev_kfree_skb_any(skb);
+
return NETDEV_TX_OK;
}
--
2.34.1
^ permalink raw reply related [flat|nested] 15+ messages in thread
* [PATCH net v10 02/10] can: rockchip_canfd: serialize TX state and command writes
2026-11-19 8:39 [PATCH net v10 00/10] can: fix Rockchip TX stalls and bus-off restart/close races Cunhao Lu via B4 Relay
2026-11-19 8:39 ` [PATCH net v10 01/10] can: rockchip_canfd: prevent TX stall on echo skb failure Cunhao Lu via B4 Relay
@ 2026-11-19 8:39 ` Cunhao Lu via B4 Relay
2026-11-19 8:39 ` [PATCH net v10 03/10] can: dev: check netif_running() before bus-off recovery Cunhao Lu via B4 Relay
` (7 subsequent siblings)
9 siblings, 0 replies; 15+ messages in thread
From: Cunhao Lu via B4 Relay @ 2026-11-19 8:39 UTC (permalink / raw)
To: Marc Kleine-Budde, kernel, Vincent Mailhol, Heiko Stuebner,
Oliver Hartkopp, David S. Miller, Eric Dumazet, Jakub Kicinski,
Paolo Abeni, Simon Horman, Jonathan Corbet, Shuah Khan,
Randy Dunlap, Wolfgang Grandegger
Cc: linux-can, linux-arm-kernel, linux-rockchip, linux-kernel,
linux-doc, Cunhao Lu, stable
From: Cunhao Lu <1579567540@qq.com>
rkcanfd_get_effective_tx_free() inspects the tail echo entry while the RX
completion path removes it and advances tx_tail. Concurrent TX submission
can dereference an skb already queued for NAPI, where another CPU can
free it, or miscount free slots and stop the TX queue with no completion
left to wake it.
TX submission and RX-side retry also share the MODE/CMD/MODE sequence
used for erratum 12. Interleaving these sequences can restore the default
mode before the other caller writes CMD, bypassing the workaround.
Serialize TX submission with RX completion and retry using tx_lock. Hold
it across the TX space and RX pending checks, echo handling, head/tail
updates, BQL accounting, queue stop/wake and command writes. Require
caller-held locking in the shared helpers and keep TX diagnostics in
the same critical section.
Release the retained original skb after unlocking and restoring IRQs, so
its socket destructor cannot run under tx_lock.
Fixes: ae002cc32ec4 ("can: rockchip_canfd: prepare to use full TX-FIFO depth")
Fixes: 83f9bd6bf39d ("can: rockchip_canfd: implement workaround for erratum 12")
Cc: stable@vger.kernel.org
Signed-off-by: Cunhao Lu <1579567540@qq.com>
---
Changes in v8:
- Extend the TX/RX critical sections to cover space and pending checks,
queue updates and diagnostics; require caller-held helper locking and
assert it for command writes.
- Release the retained original skb after unlocking.
---
drivers/net/can/rockchip/rockchip_canfd-core.c | 1 +
drivers/net/can/rockchip/rockchip_canfd-rx.c | 23 ++++++++++++++---------
drivers/net/can/rockchip/rockchip_canfd-tx.c | 26 +++++++++++++++++++++++---
drivers/net/can/rockchip/rockchip_canfd.h | 3 +++
4 files changed, 41 insertions(+), 12 deletions(-)
diff --git a/drivers/net/can/rockchip/rockchip_canfd-core.c b/drivers/net/can/rockchip/rockchip_canfd-core.c
index 37c1c22c40c9..1cae86973da5 100644
--- a/drivers/net/can/rockchip/rockchip_canfd-core.c
+++ b/drivers/net/can/rockchip/rockchip_canfd-core.c
@@ -921,6 +921,7 @@ static int rkcanfd_probe(struct platform_device *pdev)
priv->can.do_set_mode = rkcanfd_set_mode;
priv->can.do_get_berr_counter = rkcanfd_get_berr_counter;
priv->ndev = ndev;
+ spin_lock_init(&priv->tx_lock);
match = device_get_match_data(&pdev->dev);
if (match) {
diff --git a/drivers/net/can/rockchip/rockchip_canfd-rx.c b/drivers/net/can/rockchip/rockchip_canfd-rx.c
index 24e87daa1df0..23476a334d6c 100644
--- a/drivers/net/can/rockchip/rockchip_canfd-rx.c
+++ b/drivers/net/can/rockchip/rockchip_canfd-rx.c
@@ -91,6 +91,7 @@ rkcanfd_fifo_header_to_cfd_header(const struct rkcanfd_priv *priv,
return len + cfd->len;
}
+/* Called with tx_lock held. */
static int rkcanfd_rxstx_filter(struct rkcanfd_priv *priv,
const struct canfd_frame *cfd_rx, const u32 ts,
bool *tx_done)
@@ -101,6 +102,8 @@ static int rkcanfd_rxstx_filter(struct rkcanfd_priv *priv,
const struct sk_buff *skb;
unsigned int tx_tail;
+ lockdep_assert_held(&priv->tx_lock);
+
tx_tail = rkcanfd_get_tx_tail(priv);
skb = priv->can.echo_skb[tx_tail];
if (!skb) {
@@ -201,9 +204,11 @@ rkcanfd_fifo_header_empty(const struct rkcanfd_fifo_header *header)
static int rkcanfd_handle_rx_int_one(struct rkcanfd_priv *priv)
{
struct net_device_stats *stats = &priv->ndev->stats;
- struct canfd_frame cfd[1] = { }, *skb_cfd;
struct rkcanfd_fifo_header header[1] = { };
+ struct canfd_frame cfd[1] = { }, *skb_cfd;
+ bool tx_done = false;
struct sk_buff *skb;
+ unsigned long flags;
unsigned int len;
int err;
@@ -237,15 +242,15 @@ static int rkcanfd_handle_rx_int_one(struct rkcanfd_priv *priv)
return 0;
}
- if (rkcanfd_get_tx_pending(priv)) {
- bool tx_done = false;
-
+ spin_lock_irqsave(&priv->tx_lock, flags);
+ err = 0;
+ if (rkcanfd_get_tx_pending(priv))
err = rkcanfd_rxstx_filter(priv, cfd, header->ts, &tx_done);
- if (err)
- return err;
- if (tx_done && !(priv->can.ctrlmode & CAN_CTRLMODE_LOOPBACK))
- return 0;
- }
+ spin_unlock_irqrestore(&priv->tx_lock, flags);
+ if (err)
+ return err;
+ if (tx_done && !(priv->can.ctrlmode & CAN_CTRLMODE_LOOPBACK))
+ return 0;
/* Manual handling of CAN Bus Error counters. See
* rkcanfd_get_corrected_berr_counter() for detailed
diff --git a/drivers/net/can/rockchip/rockchip_canfd-tx.c b/drivers/net/can/rockchip/rockchip_canfd-tx.c
index 420e59b41059..caa71cf8e39a 100644
--- a/drivers/net/can/rockchip/rockchip_canfd-tx.c
+++ b/drivers/net/can/rockchip/rockchip_canfd-tx.c
@@ -14,6 +14,8 @@ static bool rkcanfd_tx_tail_is_eff(const struct rkcanfd_priv *priv)
const struct sk_buff *skb;
unsigned int tx_tail;
+ lockdep_assert_held(&priv->tx_lock);
+
if (!rkcanfd_get_tx_pending(priv))
return false;
@@ -35,6 +37,8 @@ static bool rkcanfd_tx_tail_is_eff(const struct rkcanfd_priv *priv)
unsigned int rkcanfd_get_effective_tx_free(const struct rkcanfd_priv *priv)
{
+ lockdep_assert_held(&priv->tx_lock);
+
if (priv->devtype_data.quirks & RKCANFD_QUIRK_RK3568_ERRATUM_6 &&
rkcanfd_tx_tail_is_eff(priv))
return 0;
@@ -45,6 +49,8 @@ unsigned int rkcanfd_get_effective_tx_free(const struct rkcanfd_priv *priv)
static void rkcanfd_start_xmit_write_cmd(const struct rkcanfd_priv *priv,
const u32 reg_cmd)
{
+ lockdep_assert_held(&priv->tx_lock);
+
if (priv->devtype_data.quirks & RKCANFD_QUIRK_RK3568_ERRATUM_12)
rkcanfd_write(priv, RKCANFD_REG_MODE, priv->reg_mode_default |
RKCANFD_REG_MODE_SPACE_RX_MODE);
@@ -60,6 +66,8 @@ void rkcanfd_xmit_retry(struct rkcanfd_priv *priv)
const unsigned int tx_head = rkcanfd_get_tx_head(priv);
const u32 reg_cmd = RKCANFD_REG_CMD_TX_REQ(tx_head);
+ lockdep_assert_held(&priv->tx_lock);
+
rkcanfd_start_xmit_write_cmd(priv, reg_cmd);
}
@@ -69,12 +77,15 @@ netdev_tx_t rkcanfd_start_xmit(struct sk_buff *skb, struct net_device *ndev)
u32 reg_frameinfo, reg_id, reg_cmd;
unsigned int tx_head, frame_len;
const struct canfd_frame *cfd;
+ unsigned long flags;
int err;
u8 i;
if (can_dev_dropped_skb(ndev, skb))
return NETDEV_TX_OK;
+ spin_lock_irqsave(&priv->tx_lock, flags);
+
if (!netif_subqueue_maybe_stop(priv->ndev, 0,
rkcanfd_get_effective_tx_free(priv),
RKCANFD_TX_STOP_THRESHOLD,
@@ -85,7 +96,7 @@ netdev_tx_t rkcanfd_start_xmit(struct sk_buff *skb, struct net_device *ndev)
priv->tx_head, priv->tx_tail,
rkcanfd_get_tx_pending(priv));
- return NETDEV_TX_BUSY;
+ goto out_busy;
}
cfd = (struct canfd_frame *)skb->data;
@@ -117,8 +128,8 @@ netdev_tx_t rkcanfd_start_xmit(struct sk_buff *skb, struct net_device *ndev)
tx_head = rkcanfd_get_tx_head(priv);
reg_cmd = RKCANFD_REG_CMD_TX_REQ(tx_head);
- /* Retain the original skb as the frame-data source even if the
- * echo skb is removed before submission finishes.
+ /* Keep the original skb alive until IRQs are restored so echo
+ * preparation cannot invoke its socket destructor under tx_lock.
*/
frame_len = can_skb_get_frame_len(skb);
err = can_put_echo_skb(skb_get(skb), ndev, tx_head, frame_len);
@@ -142,15 +153,22 @@ netdev_tx_t rkcanfd_start_xmit(struct sk_buff *skb, struct net_device *ndev)
RKCANFD_TX_STOP_THRESHOLD,
RKCANFD_TX_START_THRESHOLD);
+ spin_unlock_irqrestore(&priv->tx_lock, flags);
dev_consume_skb_any(skb);
return NETDEV_TX_OK;
out_drop:
ndev->stats.tx_dropped++;
+ spin_unlock_irqrestore(&priv->tx_lock, flags);
dev_kfree_skb_any(skb);
return NETDEV_TX_OK;
+
+out_busy:
+ spin_unlock_irqrestore(&priv->tx_lock, flags);
+
+ return NETDEV_TX_BUSY;
}
void rkcanfd_handle_tx_done_one(struct rkcanfd_priv *priv, const u32 ts,
@@ -160,6 +178,8 @@ void rkcanfd_handle_tx_done_one(struct rkcanfd_priv *priv, const u32 ts,
unsigned int tx_tail;
struct sk_buff *skb;
+ lockdep_assert_held(&priv->tx_lock);
+
tx_tail = rkcanfd_get_tx_tail(priv);
skb = priv->can.echo_skb[tx_tail];
diff --git a/drivers/net/can/rockchip/rockchip_canfd.h b/drivers/net/can/rockchip/rockchip_canfd.h
index 95bea9bfd8a2..077dd57d42de 100644
--- a/drivers/net/can/rockchip/rockchip_canfd.h
+++ b/drivers/net/can/rockchip/rockchip_canfd.h
@@ -15,6 +15,7 @@
#include <linux/netdevice.h>
#include <linux/reset.h>
#include <linux/skbuff.h>
+#include <linux/spinlock.h>
#include <linux/timecounter.h>
#include <linux/types.h>
#include <linux/u64_stats_sync.h>
@@ -474,6 +475,8 @@ struct rkcanfd_priv {
struct can_rx_offload offload;
struct net_device *ndev;
+ /* Protect running TX/RX echo state, queue updates and TX commands. */
+ spinlock_t tx_lock;
void __iomem *regs;
unsigned int tx_head;
unsigned int tx_tail;
--
2.34.1
^ permalink raw reply related [flat|nested] 15+ messages in thread
* [PATCH net v10 03/10] can: dev: check netif_running() before bus-off recovery
2026-11-19 8:39 [PATCH net v10 00/10] can: fix Rockchip TX stalls and bus-off restart/close races Cunhao Lu via B4 Relay
2026-11-19 8:39 ` [PATCH net v10 01/10] can: rockchip_canfd: prevent TX stall on echo skb failure Cunhao Lu via B4 Relay
2026-11-19 8:39 ` [PATCH net v10 02/10] can: rockchip_canfd: serialize TX state and command writes Cunhao Lu via B4 Relay
@ 2026-11-19 8:39 ` Cunhao Lu via B4 Relay
2026-11-19 8:39 ` [PATCH net v10 04/10] can: dev: add close_candev_cancel_restart_sync() Cunhao Lu via B4 Relay
` (6 subsequent siblings)
9 siblings, 0 replies; 15+ messages in thread
From: Cunhao Lu via B4 Relay @ 2026-11-19 8:39 UTC (permalink / raw)
To: Marc Kleine-Budde, kernel, Vincent Mailhol, Heiko Stuebner,
Oliver Hartkopp, David S. Miller, Eric Dumazet, Jakub Kicinski,
Paolo Abeni, Simon Horman, Jonathan Corbet, Shuah Khan,
Randy Dunlap, Wolfgang Grandegger
Cc: linux-can, linux-arm-kernel, linux-rockchip, linux-kernel,
linux-doc, Cunhao Lu, stable
From: Cunhao Lu <1579567540@qq.com>
The networking core clears __LINK_STATE_START before calling ndo_stop(),
but the CAN core can still schedule or start bus-off recovery after the
interface is down.
Check netif_running() in can_restart() before flushing echo skbs, sending
the restart notification or enabling the carrier. Keep the existing
unsupported-operation check first. In can_bus_off(), schedule recovery
only when the check observes the interface running, and make the log
message reflect that decision. Keep reporting bus-off and disabling the
carrier.
These checks do not wait for recovery that has already passed the check,
nor serialize the check with work submission. Drivers still need to
cancel restart work before tearing down resources and quiesce its
producers before their final cleanup.
Fixes: 39549eef3587 ("can: CAN Network device driver and Netlink interface")
Cc: stable@vger.kernel.org
Signed-off-by: Cunhao Lu <1579567540@qq.com>
---
Changes in v10:
- Limit the recovery documentation to CAN core software recovery and
checks that observe the interface down; retain driver shutdown
synchronization requirements.
Changes in v7:
- Clarify the running checks' limits and configure restart-ms while down.
Changes in v6:
- Update the can_bus_off() comment and document the running-interface
requirement for automatic recovery.
---
Q: Do the running checks serialize recovery with device close?
A: No. A worker can pass the check before close clears the running bit.
FlexCAN cancels restart work after stopping hardware and removing RX
offload, so that worker can restart after shutdown. This race predates
the patch, and the running checks do not widen it. FlexCAN must drain
active restart work before tearing down those resources. That driver fix
is not part of this patch.
Q: Can HI3110 queue restart work after close and outlive the device?
A: Yes. With restart-ms enabled, its IRQ thread can decide to schedule
before close, then enqueue after close_candev() returns. free_irq() drains
the producer but leaves that core work queued. A quick reopen permits
stale recovery and echo cleanup; removal can leave work referring to
freed private data. The driver's private workqueue does not own the core
work. These races predate the patch, and the running checks do not widen
them. HI3110 must stop producers before final cancellation and drain
both core recovery and work queued by its callback before teardown.
That driver fix is not part of this patch.
Q: Why not replace cancellation with disable/enable_delayed_work() here?
A: Disabling work can reject late submissions, but doing it at the end
of ndo_stop() still lets an already running callback overlap teardown.
A core-wide conversion would need to balance disable and enable across
initialization, open, failed open and close. Rockchip instead cancels
restart_work before teardown and again after free_irq(). The restart
races in FlexCAN and HI3110 predate this patch, and the netif_running()
checks do not widen them. Fixes for those drivers are outside this series.
Link: https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261017-rockchip-canfd-restart-close-v6-0-ed605feb4b6e%40qq.com
Link: https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261111-rockchip-canfd-restart-close-v9-0-6643f3825ba3%40qq.com
---
Documentation/networking/can.rst | 9 ++++++++-
drivers/net/can/dev/dev.c | 13 ++++++++++---
2 files changed, 18 insertions(+), 4 deletions(-)
diff --git a/Documentation/networking/can.rst b/Documentation/networking/can.rst
index 51bc70748f753..f44b9c96848de 100644
--- a/Documentation/networking/can.rst
+++ b/Documentation/networking/can.rst
@@ -1376,9 +1376,16 @@ before you can start it to avoid error-prone default settings::
A device may enter the "bus-off" state if too many errors occurred on
the CAN bus. Then no more messages are received or sent. An automatic
bus-off recovery can be enabled by setting the "restart-ms" to a
-non-zero value, e.g.::
+non-zero value while the interface is down, e.g.::
+ $ ip link set canX down
$ ip link set canX type can restart-ms 100
+ $ ip link set canX up
+
+The CAN core checks the interface before queuing a restart and again
+before starting it. If it finds the interface down, it skips that step.
+Drivers must still synchronize recovery with shutdown. Controller-managed
+recovery bypasses these checks.
Alternatively, the application may realize the "bus-off" condition
by monitoring CAN error message frames and do a restart when
diff --git a/drivers/net/can/dev/dev.c b/drivers/net/can/dev/dev.c
index 769745e22a3c7..a893a49a33813 100644
--- a/drivers/net/can/dev/dev.c
+++ b/drivers/net/can/dev/dev.c
@@ -198,6 +198,9 @@ static int can_restart(struct net_device *dev)
if (!priv->do_set_mode)
return -EOPNOTSUPP;
+ if (!netif_running(dev))
+ return -ENETDOWN;
+
if (netif_carrier_ok(dev))
netdev_err(dev, "Attempt to restart for bus-off recovery, but carrier is OK?\n");
@@ -259,13 +262,17 @@ int can_restart_now(struct net_device *dev)
*
* This functions should be called when the device goes bus-off to
* tell the netif layer that no more packets can be sent or received.
- * If enabled, a timer is started to trigger bus-off recovery.
+ * If automatic recovery is enabled, schedule delayed work unless the
+ * running check observes the interface down. Drivers must still synchronize
+ * recovery with device close.
*/
void can_bus_off(struct net_device *dev)
{
struct can_priv *priv = netdev_priv(dev);
+ bool restart;
- if (priv->restart_ms)
+ restart = priv->restart_ms && netif_running(dev);
+ if (restart)
netdev_info(dev, "bus-off, scheduling restart in %d ms\n",
priv->restart_ms);
else
@@ -273,7 +280,7 @@ void can_bus_off(struct net_device *dev)
netif_carrier_off(dev);
- if (priv->restart_ms)
+ if (restart)
schedule_delayed_work(&priv->restart_work,
msecs_to_jiffies(priv->restart_ms));
}
--
2.34.1
^ permalink raw reply related [flat|nested] 15+ messages in thread
* [PATCH net v10 04/10] can: dev: add close_candev_cancel_restart_sync()
2026-11-19 8:39 [PATCH net v10 00/10] can: fix Rockchip TX stalls and bus-off restart/close races Cunhao Lu via B4 Relay
` (2 preceding siblings ...)
2026-11-19 8:39 ` [PATCH net v10 03/10] can: dev: check netif_running() before bus-off recovery Cunhao Lu via B4 Relay
@ 2026-11-19 8:39 ` Cunhao Lu via B4 Relay
2026-11-19 8:39 ` [PATCH net v10 05/10] can: rockchip_canfd: cancel bus-off restart before closing the device Cunhao Lu via B4 Relay
` (5 subsequent siblings)
9 siblings, 0 replies; 15+ messages in thread
From: Cunhao Lu via B4 Relay @ 2026-11-19 8:39 UTC (permalink / raw)
To: Marc Kleine-Budde, kernel, Vincent Mailhol, Heiko Stuebner,
Oliver Hartkopp, David S. Miller, Eric Dumazet, Jakub Kicinski,
Paolo Abeni, Simon Horman, Jonathan Corbet, Shuah Khan,
Randy Dunlap, Wolfgang Grandegger
Cc: linux-can, linux-arm-kernel, linux-rockchip, linux-kernel,
linux-doc, Cunhao Lu, stable
From: Cunhao Lu <1579567540@qq.com>
Drivers may need to wait for bus-off recovery before tearing down the
IRQ and hardware resources used by their restart callback. Calling
close_candev() at that point also flushes echo skbs, which belongs after
the driver has quiesced the users of those buffers.
Extract the synchronous cancellation from close_candev() into
close_candev_cancel_restart_sync(). Drivers can then cancel restart
work early and call close_candev() after stopping its producers. The
final close still cancels restart work before flushing echo skbs.
Cc: stable@vger.kernel.org # prerequisite for the Rockchip restart/close fix
Signed-off-by: Cunhao Lu <1579567540@qq.com>
---
Changes in v6:
- Describe the helper's use by the following Rockchip fix.
---
drivers/net/can/dev/dev.c | 4 +---
include/linux/can/dev.h | 17 +++++++++++++++++
2 files changed, 18 insertions(+), 3 deletions(-)
diff --git a/drivers/net/can/dev/dev.c b/drivers/net/can/dev/dev.c
index a893a49a3381..39271a3ef0fa 100644
--- a/drivers/net/can/dev/dev.c
+++ b/drivers/net/can/dev/dev.c
@@ -537,9 +537,7 @@ EXPORT_SYMBOL_GPL(of_can_transceiver);
*/
void close_candev(struct net_device *dev)
{
- struct can_priv *priv = netdev_priv(dev);
-
- cancel_delayed_work_sync(&priv->restart_work);
+ close_candev_cancel_restart_sync(dev);
can_flush_echo_skb(dev);
}
EXPORT_SYMBOL_GPL(close_candev);
diff --git a/include/linux/can/dev.h b/include/linux/can/dev.h
index 346faddee02e..2a286f906394 100644
--- a/include/linux/can/dev.h
+++ b/include/linux/can/dev.h
@@ -22,6 +22,7 @@
#include <linux/can/skb.h>
#include <linux/ethtool.h>
#include <linux/netdevice.h>
+#include <linux/workqueue.h>
/*
* CAN mode
@@ -115,6 +116,22 @@ struct can_priv *safe_candev_priv(struct net_device *dev);
int open_candev(struct net_device *dev);
void close_candev(struct net_device *dev);
+
+/**
+ * close_candev_cancel_restart_sync() - cancel bus-off restart synchronously
+ * @dev: CAN network device
+ *
+ * Call before tearing down resources used by the restart callback.
+ * The driver must quiesce restart work producers before calling
+ * close_candev() for the final cleanup.
+ */
+static inline void close_candev_cancel_restart_sync(struct net_device *dev)
+{
+ struct can_priv *priv = netdev_priv(dev);
+
+ cancel_delayed_work_sync(&priv->restart_work);
+}
+
void can_set_default_mtu(struct net_device *dev);
void can_set_cap_info(struct net_device *dev);
int __must_check can_set_static_ctrlmode(struct net_device *dev,
--
2.34.1
^ permalink raw reply related [flat|nested] 15+ messages in thread
* [PATCH net v10 05/10] can: rockchip_canfd: cancel bus-off restart before closing the device
2026-11-19 8:39 [PATCH net v10 00/10] can: fix Rockchip TX stalls and bus-off restart/close races Cunhao Lu via B4 Relay
` (3 preceding siblings ...)
2026-11-19 8:39 ` [PATCH net v10 04/10] can: dev: add close_candev_cancel_restart_sync() Cunhao Lu via B4 Relay
@ 2026-11-19 8:39 ` Cunhao Lu via B4 Relay
2026-11-21 0:02 ` netdev-bot+sashiko
2026-11-19 8:39 ` [PATCH net v10 06/10] can: rockchip_canfd: disable RX offload on open failure Cunhao Lu via B4 Relay
` (4 subsequent siblings)
9 siblings, 1 reply; 15+ messages in thread
From: Cunhao Lu via B4 Relay @ 2026-11-19 8:39 UTC (permalink / raw)
To: Marc Kleine-Budde, kernel, Vincent Mailhol, Heiko Stuebner,
Oliver Hartkopp, David S. Miller, Eric Dumazet, Jakub Kicinski,
Paolo Abeni, Simon Horman, Jonathan Corbet, Shuah Khan,
Randy Dunlap, Wolfgang Grandegger
Cc: linux-can, linux-arm-kernel, linux-rockchip, linux-kernel,
linux-doc, Cunhao Lu, stable
From: Cunhao Lu <1579567540@qq.com>
The close path cancels restart_work in close_candev() only after freeing
the IRQ, disabling RX offload and stopping the controller. A concurrent
restart can re-enable the controller and interrupts after IRQ teardown,
or requeue timestamp work that accesses registers after runtime suspend
has disabled the controller clocks.
Call close_candev_cancel_restart_sync() at the start of ndo_stop(), while
the resources needed by a running restart are still available. Leave
echo cleanup in close_candev().
A bus-off IRQ can pass the scheduling check before close, then queue
restart work after the early cancellation. The final cancellation after
free_irq() drains that late work so it cannot survive close.
Fixes: ff60bfbaf67f ("can: rockchip_canfd: add driver for Rockchip CAN-FD controller")
Cc: stable@vger.kernel.org
Signed-off-by: Cunhao Lu <1579567540@qq.com>
---
Changes in v7:
- Keep close cancellation separate from restart and open error handling.
---
drivers/net/can/rockchip/rockchip_canfd-core.c | 1 +
1 file changed, 1 insertion(+)
diff --git a/drivers/net/can/rockchip/rockchip_canfd-core.c b/drivers/net/can/rockchip/rockchip_canfd-core.c
index 1cae86973da5..59a79fe057e6 100644
--- a/drivers/net/can/rockchip/rockchip_canfd-core.c
+++ b/drivers/net/can/rockchip/rockchip_canfd-core.c
@@ -758,6 +758,7 @@ static int rkcanfd_stop(struct net_device *ndev)
{
struct rkcanfd_priv *priv = netdev_priv(ndev);
+ close_candev_cancel_restart_sync(ndev);
netif_stop_queue(ndev);
rkcanfd_chip_interrupts_disable(priv);
--
2.34.1
^ permalink raw reply related [flat|nested] 15+ messages in thread
* [PATCH net v10 06/10] can: rockchip_canfd: disable RX offload on open failure
2026-11-19 8:39 [PATCH net v10 00/10] can: fix Rockchip TX stalls and bus-off restart/close races Cunhao Lu via B4 Relay
` (4 preceding siblings ...)
2026-11-19 8:39 ` [PATCH net v10 05/10] can: rockchip_canfd: cancel bus-off restart before closing the device Cunhao Lu via B4 Relay
@ 2026-11-19 8:39 ` Cunhao Lu via B4 Relay
2026-11-19 8:39 ` [PATCH net v10 07/10] can: rockchip_canfd: serialize TX with bus-off and restart Cunhao Lu via B4 Relay
` (3 subsequent siblings)
9 siblings, 0 replies; 15+ messages in thread
From: Cunhao Lu via B4 Relay @ 2026-11-19 8:39 UTC (permalink / raw)
To: Marc Kleine-Budde, kernel, Vincent Mailhol, Heiko Stuebner,
Oliver Hartkopp, David S. Miller, Eric Dumazet, Jakub Kicinski,
Paolo Abeni, Simon Horman, Jonathan Corbet, Shuah Khan,
Randy Dunlap, Wolfgang Grandegger
Cc: linux-can, linux-arm-kernel, linux-rockchip, linux-kernel,
linux-doc, Cunhao Lu, stable
From: Cunhao Lu <1579567540@qq.com>
rkcanfd_open() enables RX offload before requesting the IRQ. If
request_irq() fails, the error path leaves the offload enabled. A later
open calls napi_enable() on an already enabled NAPI instance.
Disable RX offload on that error path before stopping the controller and
releasing the runtime PM reference.
Fixes: ff60bfbaf67f ("can: rockchip_canfd: add driver for Rockchip CAN-FD controller")
Cc: stable@vger.kernel.org
Signed-off-by: Cunhao Lu <1579567540@qq.com>
---
Changes in v7:
- Split the open error cleanup into a separate patch.
Changes in v3:
- Disable RX offload when request_irq() fails during open.
---
drivers/net/can/rockchip/rockchip_canfd-core.c | 1 +
1 file changed, 1 insertion(+)
diff --git a/drivers/net/can/rockchip/rockchip_canfd-core.c b/drivers/net/can/rockchip/rockchip_canfd-core.c
index 59a79fe057e6..d05264605efc 100644
--- a/drivers/net/can/rockchip/rockchip_canfd-core.c
+++ b/drivers/net/can/rockchip/rockchip_canfd-core.c
@@ -747,6 +747,7 @@ static int rkcanfd_open(struct net_device *ndev)
return 0;
out_rkcanfd_chip_stop:
+ can_rx_offload_disable(&priv->offload);
rkcanfd_chip_stop_sync(priv, CAN_STATE_STOPPED);
pm_runtime_put(ndev->dev.parent);
out_close_candev:
--
2.34.1
^ permalink raw reply related [flat|nested] 15+ messages in thread
* [PATCH net v10 07/10] can: rockchip_canfd: serialize TX with bus-off and restart
2026-11-19 8:39 [PATCH net v10 00/10] can: fix Rockchip TX stalls and bus-off restart/close races Cunhao Lu via B4 Relay
` (5 preceding siblings ...)
2026-11-19 8:39 ` [PATCH net v10 06/10] can: rockchip_canfd: disable RX offload on open failure Cunhao Lu via B4 Relay
@ 2026-11-19 8:39 ` Cunhao Lu via B4 Relay
2026-11-21 0:02 ` netdev-bot+sashiko
2026-11-19 8:39 ` [PATCH net v10 08/10] can: rockchip_canfd: retry the outstanding TX buffer Cunhao Lu via B4 Relay
` (2 subsequent siblings)
9 siblings, 1 reply; 15+ messages in thread
From: Cunhao Lu via B4 Relay @ 2026-11-19 8:39 UTC (permalink / raw)
To: Marc Kleine-Budde, kernel, Vincent Mailhol, Heiko Stuebner,
Oliver Hartkopp, David S. Miller, Eric Dumazet, Jakub Kicinski,
Paolo Abeni, Simon Horman, Jonathan Corbet, Shuah Khan,
Randy Dunlap, Wolfgang Grandegger
Cc: linux-can, linux-arm-kernel, linux-rockchip, linux-kernel,
linux-doc, Cunhao Lu, stable
From: Cunhao Lu <1579567540@qq.com>
The bus-off path can reset the controller while TX submission or RX-side
echo handling is active. CAN restart flushes echo skbs before set_mode(),
so disabling TX in that callback is too late to protect the echo ring.
Use tx_lock and tx_enabled to drain TX and RX-side echo users before
publishing CAN_STATE_BUS_OFF. Return NETDEV_TX_BUSY while TX is disabled
and drop consumed RX entries so self-transmitted frames cannot escape
echo filtering as ordinary RX.
Before restart, mask device interrupts and flush the mask write, suspend
shared IRQ access, and drain active handlers and timestamp work. After
reinitialization, restore carrier, TX, IRQ handling and the queue under
tx_lock, so the old bus-off IRQ cannot leave carrier off and a new bus-off
takes precedence. Keep synchronous waits outside the lock and other
shared IRQ users enabled.
Reset clears INT_MASK. Restore and flush the full mask immediately after
deassertion so the remaining controller configuration stays masked.
Keep IRQ handling suspended until open completes and TX disabled on
close or failed open. Initialize timestamp work once at probe.
Fixes: ff60bfbaf67f ("can: rockchip_canfd: add driver for Rockchip CAN-FD controller")
Fixes: b6661d73290c ("can: rockchip_canfd: add TX PATH")
Cc: stable@vger.kernel.org
Signed-off-by: Cunhao Lu <1579567540@qq.com>
---
Changes in v9:
- Restore and flush the full interrupt mask immediately after reset
deassertion, before the remaining controller configuration.
- Count consumed RX FIFO entries discarded while TX is disabled.
- Include asm/barrier.h directly for release/acquire operations.
Changes in v8:
- Move skb ownership handling and running TX/RX critical sections to
the preceding TX patches.
Changes in v7:
- Introduce restart/IRQ synchronization and TX protection together.
- Reuse prerequisite TX locking and adapt to the updated skb ownership.
Changes in v6:
- Defer the original skb's release until after unlocking.
- Abort TX submission if echo preparation fails.
Changes in v5:
- Drop consumed RX FIFO entries when tx_enabled is false under tx_lock,
rather than skipping only echo filtering and delivering them as RX.
Changes in v3:
- Replace disable_irq()/enable_irq() with device interrupt masking,
mask readback, an irq_suspended flag and synchronize_irq().
- Pair release/acquire accesses to publish reinitialized state before
the IRQ handler resumes, without disabling other shared IRQ users.
- Initialize timestamp delayed work once at probe and synchronously
cancel it before reinitializing the timestamp state during restart.
---
Q: Does irq_suspended replace a lock around controller reinitialization?
A: No. Restart masks and flushes device interrupts, sets irq_suspended
and drains active handlers with synchronize_irq() before reinitializing
the controller. The release/acquire pair publishes the initialized state
when IRQ handling resumes. Open sets the gate before registering the
handler, so there is no old handler to drain. This closes the window in
which the IRQ handler could access the controller during restart.
Q: Does this also protect the bus error counter getter?
A: No. The getter can race with IRQ cache updates, RX/TX decrements and
restart's cache reset. Its runtime PM reference protects power, not the
cache. This race predates the patch. The IRQ gate adds no cache accesses
and does not widen the race, so this patch does not fix it.
Q: Can the first INT_MASK write after reset deassertion be lost?
A: No lost write was observed on RK3588 rev2.2. Across open, close and
idle restart callbacks, all 600 first reads of INT_MASK returned 0x3fff
after a write of 0x7fff. A readback flushes the write but does not
establish when reset completes. Reset already cleared INT_MASK before
this patch; restoring the mask earlier shortens the unmasked window.
These results do not rule out a lost write on other hardware, but give
no basis for adding a delay here.
Link: https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261111-rockchip-canfd-restart-close-v9-0-6643f3825ba3%40qq.com
---
drivers/net/can/rockchip/rockchip_canfd-core.c | 66 ++++++++++++++++++++--
drivers/net/can/rockchip/rockchip_canfd-rx.c | 11 +++-
.../net/can/rockchip/rockchip_canfd-timestamp.c | 6 +-
drivers/net/can/rockchip/rockchip_canfd-tx.c | 4 ++
drivers/net/can/rockchip/rockchip_canfd.h | 6 +-
5 files changed, 84 insertions(+), 9 deletions(-)
diff --git a/drivers/net/can/rockchip/rockchip_canfd-core.c b/drivers/net/can/rockchip/rockchip_canfd-core.c
index d05264605efc2..a5c6331191d17 100644
--- a/drivers/net/can/rockchip/rockchip_canfd-core.c
+++ b/drivers/net/can/rockchip/rockchip_canfd-core.c
@@ -22,6 +22,8 @@
#include <linux/pm_runtime.h>
#include <linux/string.h>
+#include <asm/barrier.h>
+
#include "rockchip_canfd.h"
static const struct rkcanfd_devtype_data rkcanfd_devtype_data_rk3568v2 = {
@@ -116,6 +118,10 @@ static void rkcanfd_chip_set_reset_mode(const struct rkcanfd_priv *priv)
udelay(2);
reset_control_deassert(priv->reset);
+ /* Reset clears INT_MASK; restore it before configuring the controller. */
+ rkcanfd_write(priv, RKCANFD_REG_INT_MASK, RKCANFD_REG_INT_ALL);
+ rkcanfd_read(priv, RKCANFD_REG_INT_MASK);
+
rkcanfd_write(priv, RKCANFD_REG_MODE, 0x0);
}
@@ -325,6 +331,34 @@ static void rkcanfd_chip_start(struct rkcanfd_priv *priv)
rkcanfd_read(priv, RKCANFD_REG_MODE));
}
+static void rkcanfd_tx_disable(struct rkcanfd_priv *priv)
+{
+ unsigned long flags;
+
+ spin_lock_irqsave(&priv->tx_lock, flags);
+ priv->tx_enabled = false;
+ netif_stop_queue(priv->ndev);
+ spin_unlock_irqrestore(&priv->tx_lock, flags);
+}
+
+static void rkcanfd_chip_start_finish(struct rkcanfd_priv *priv)
+{
+ unsigned long flags;
+
+ /* Restore carrier after draining the old bus-off IRQ. Hold tx_lock
+ * through IRQ resumption and queue wake so a new bus-off takes
+ * precedence.
+ */
+ spin_lock_irqsave(&priv->tx_lock, flags);
+ netif_carrier_on(priv->ndev);
+ priv->tx_enabled = true;
+ /* Publish initialized state to the IRQ handler's acquire. */
+ smp_store_release(&priv->irq_suspended, false);
+ rkcanfd_chip_interrupts_enable(priv);
+ netif_wake_queue(priv->ndev);
+ spin_unlock_irqrestore(&priv->tx_lock, flags);
+}
+
static void __rkcanfd_chip_stop(struct rkcanfd_priv *priv, const enum can_state state)
{
priv->can.state = state;
@@ -356,9 +390,17 @@ static int rkcanfd_set_mode(struct net_device *ndev,
switch (mode) {
case CAN_MODE_START:
+ rkcanfd_tx_disable(priv);
+ rkcanfd_chip_interrupts_disable(priv);
+ /* Flush the interrupt mask write before suspending the handler. */
+ rkcanfd_read(priv, RKCANFD_REG_INT_MASK);
+ WRITE_ONCE(priv->irq_suspended, true);
+ /* Wait for handlers that already passed the suspended check. */
+ synchronize_irq(ndev->irq);
+ rkcanfd_timestamp_stop_sync(priv);
+
rkcanfd_chip_start(priv);
- rkcanfd_chip_interrupts_enable(priv);
- netif_wake_queue(ndev);
+ rkcanfd_chip_start_finish(priv);
break;
default:
@@ -592,6 +634,11 @@ static int rkcanfd_handle_state_error_int(struct rkcanfd_priv *priv)
* handles cf == NULL.
*/
skb = rkcanfd_alloc_can_err_skb(priv, &cf, ×tamp);
+ /* Drain TX before publishing BUS_OFF, which permits a manual
+ * restart to flush echo SKBs before calling our set_mode().
+ */
+ if (new_state == CAN_STATE_BUS_OFF)
+ rkcanfd_tx_disable(priv);
can_change_state(ndev, cf, tx_state, rx_state);
if (new_state == CAN_STATE_BUS_OFF) {
@@ -666,6 +713,10 @@ static irqreturn_t rkcanfd_irq(int irq, void *dev_id)
struct rkcanfd_priv *priv = dev_id;
u32 reg_int_unmasked, reg_int;
+ /* Pairs with smp_store_release() in rkcanfd_chip_start_finish(). */
+ if (smp_load_acquire(&priv->irq_suspended))
+ return IRQ_NONE;
+
reg_int_unmasked = rkcanfd_read(priv, RKCANFD_REG_INT);
reg_int = reg_int_unmasked & ~priv->reg_int_mask_default;
@@ -733,6 +784,8 @@ static int rkcanfd_open(struct net_device *ndev)
if (err)
goto out_close_candev;
+ /* The shared IRQ handler may run before request_irq() returns. */
+ WRITE_ONCE(priv->irq_suspended, true);
rkcanfd_chip_start(priv);
can_rx_offload_enable(&priv->offload);
@@ -740,13 +793,12 @@ static int rkcanfd_open(struct net_device *ndev)
if (err)
goto out_rkcanfd_chip_stop;
- rkcanfd_chip_interrupts_enable(priv);
-
- netif_start_queue(ndev);
+ rkcanfd_chip_start_finish(priv);
return 0;
out_rkcanfd_chip_stop:
+ rkcanfd_tx_disable(priv);
can_rx_offload_disable(&priv->offload);
rkcanfd_chip_stop_sync(priv, CAN_STATE_STOPPED);
pm_runtime_put(ndev->dev.parent);
@@ -760,7 +812,7 @@ static int rkcanfd_stop(struct net_device *ndev)
struct rkcanfd_priv *priv = netdev_priv(ndev);
close_candev_cancel_restart_sync(ndev);
- netif_stop_queue(ndev);
+ rkcanfd_tx_disable(priv);
rkcanfd_chip_interrupts_disable(priv);
free_irq(ndev->irq, priv);
@@ -937,6 +989,8 @@ static int rkcanfd_probe(struct platform_device *pdev)
if (err)
goto out_free_candev;
+ rkcanfd_timestamp_work_init(priv);
+
err = rkcanfd_register(priv);
if (err)
goto out_can_rx_offload_del;
diff --git a/drivers/net/can/rockchip/rockchip_canfd-rx.c b/drivers/net/can/rockchip/rockchip_canfd-rx.c
index 23476a334d6c3..0eab00623e037 100644
--- a/drivers/net/can/rockchip/rockchip_canfd-rx.c
+++ b/drivers/net/can/rockchip/rockchip_canfd-rx.c
@@ -91,7 +91,7 @@ rkcanfd_fifo_header_to_cfd_header(const struct rkcanfd_priv *priv,
return len + cfd->len;
}
-/* Called with tx_lock held. */
+/* Called with tx_lock held and TX enabled. */
static int rkcanfd_rxstx_filter(struct rkcanfd_priv *priv,
const struct canfd_frame *cfd_rx, const u32 ts,
bool *tx_done)
@@ -242,7 +242,16 @@ static int rkcanfd_handle_rx_int_one(struct rkcanfd_priv *priv)
return 0;
}
+ /* Drop consumed FIFO entries while TX is disabled to avoid delivering
+ * self-received frames as ordinary RX without echo filtering.
+ */
spin_lock_irqsave(&priv->tx_lock, flags);
+ if (unlikely(!priv->tx_enabled)) {
+ spin_unlock_irqrestore(&priv->tx_lock, flags);
+ stats->rx_dropped++;
+ return 0;
+ }
+
err = 0;
if (rkcanfd_get_tx_pending(priv))
err = rkcanfd_rxstx_filter(priv, cfd, header->ts, &tx_done);
diff --git a/drivers/net/can/rockchip/rockchip_canfd-timestamp.c b/drivers/net/can/rockchip/rockchip_canfd-timestamp.c
index 72774cd2f94b7..ad582afbbd682 100644
--- a/drivers/net/can/rockchip/rockchip_canfd-timestamp.c
+++ b/drivers/net/can/rockchip/rockchip_canfd-timestamp.c
@@ -37,6 +37,11 @@ static void rkcanfd_timestamp_work(struct work_struct *work)
schedule_delayed_work(&priv->timestamp, priv->work_delay_jiffies);
}
+void rkcanfd_timestamp_work_init(struct rkcanfd_priv *priv)
+{
+ INIT_DELAYED_WORK(&priv->timestamp, rkcanfd_timestamp_work);
+}
+
void rkcanfd_timestamp_init(struct rkcanfd_priv *priv)
{
const struct can_bittiming *dbt = &priv->can.fd.data_bittiming;
@@ -73,7 +78,6 @@ void rkcanfd_timestamp_init(struct rkcanfd_priv *priv)
max_cycles = min(max_cycles, cc->mask);
work_delay_ns = clocksource_cyc2ns(max_cycles, cc->mult, cc->shift);
priv->work_delay_jiffies = div_u64(work_delay_ns, 3u * NSEC_PER_SEC / HZ);
- INIT_DELAYED_WORK(&priv->timestamp, rkcanfd_timestamp_work);
netdev_dbg(priv->ndev, "clock=%lu.%02luMHz bitrate=%lu.%02luMBit/s div=%u rate=%lu.%02luMHz mult=%u shift=%u delay=%lus\n",
priv->can.clock.freq / MEGA,
diff --git a/drivers/net/can/rockchip/rockchip_canfd-tx.c b/drivers/net/can/rockchip/rockchip_canfd-tx.c
index caa71cf8e39a9..7e3f478422e0e 100644
--- a/drivers/net/can/rockchip/rockchip_canfd-tx.c
+++ b/drivers/net/can/rockchip/rockchip_canfd-tx.c
@@ -85,6 +85,10 @@ netdev_tx_t rkcanfd_start_xmit(struct sk_buff *skb, struct net_device *ndev)
return NETDEV_TX_OK;
spin_lock_irqsave(&priv->tx_lock, flags);
+ if (unlikely(!priv->tx_enabled)) {
+ netif_stop_queue(ndev);
+ goto out_busy;
+ }
if (!netif_subqueue_maybe_stop(priv->ndev, 0,
rkcanfd_get_effective_tx_free(priv),
diff --git a/drivers/net/can/rockchip/rockchip_canfd.h b/drivers/net/can/rockchip/rockchip_canfd.h
index 077dd57d42de9..8fdb59025bfaf 100644
--- a/drivers/net/can/rockchip/rockchip_canfd.h
+++ b/drivers/net/can/rockchip/rockchip_canfd.h
@@ -475,7 +475,7 @@ struct rkcanfd_priv {
struct can_rx_offload offload;
struct net_device *ndev;
- /* Protect running TX/RX echo state, queue updates and TX commands. */
+ /* Serialize TX submission/completion with bus-off and restart. */
spinlock_t tx_lock;
void __iomem *regs;
unsigned int tx_head;
@@ -485,6 +485,9 @@ struct rkcanfd_priv {
u32 reg_int_mask_default;
struct rkcanfd_devtype_data devtype_data;
+ bool tx_enabled;
+ bool irq_suspended;
+
struct cyclecounter cc;
struct timecounter tc;
struct delayed_work timestamp;
@@ -554,6 +557,7 @@ int rkcanfd_handle_rx_int(struct rkcanfd_priv *priv);
void rkcanfd_skb_set_timestamp(const struct rkcanfd_priv *priv,
struct sk_buff *skb, const u32 timestamp);
+void rkcanfd_timestamp_work_init(struct rkcanfd_priv *priv);
void rkcanfd_timestamp_init(struct rkcanfd_priv *priv);
void rkcanfd_timestamp_start(struct rkcanfd_priv *priv);
void rkcanfd_timestamp_stop(struct rkcanfd_priv *priv);
--
2.34.1
^ permalink raw reply related [flat|nested] 15+ messages in thread
* [PATCH net v10 08/10] can: rockchip_canfd: retry the outstanding TX buffer
2026-11-19 8:39 [PATCH net v10 00/10] can: fix Rockchip TX stalls and bus-off restart/close races Cunhao Lu via B4 Relay
` (6 preceding siblings ...)
2026-11-19 8:39 ` [PATCH net v10 07/10] can: rockchip_canfd: serialize TX with bus-off and restart Cunhao Lu via B4 Relay
@ 2026-11-19 8:39 ` Cunhao Lu via B4 Relay
2026-11-21 0:02 ` netdev-bot+sashiko
2026-11-19 8:39 ` [PATCH net v10 09/10] can: rockchip_canfd: prevent timestamp work from rearming after bus-off Cunhao Lu via B4 Relay
2026-11-19 8:39 ` [PATCH net v10 10/10] can: rockchip_canfd: serialize timecounter timestamp conversion Cunhao Lu via B4 Relay
9 siblings, 1 reply; 15+ messages in thread
From: Cunhao Lu via B4 Relay @ 2026-11-19 8:39 UTC (permalink / raw)
To: Marc Kleine-Budde, kernel, Vincent Mailhol, Heiko Stuebner,
Oliver Hartkopp, David S. Miller, Eric Dumazet, Jakub Kicinski,
Paolo Abeni, Simon Horman, Jonathan Corbet, Shuah Khan,
Randy Dunlap, Wolfgang Grandegger
Cc: linux-can, linux-arm-kernel, linux-rockchip, linux-kernel,
linux-doc, Cunhao Lu, stable
From: Cunhao Lu <1579567540@qq.com>
rkcanfd_xmit_retry() uses tx_head to select the buffer for an erratum 6
retransmission. With two buffers and one outstanding frame, this selects
the next submission slot instead of the slot holding that frame.
A standard frame from another node can match the erratum 6 check while
the original extended frame is still pending: its ID matches the low
11 ID bits, and both have the same RTR flag and length. Data frames must
also have matching payloads. The retry loads the extended frame from the
shared TX registers into the other buffer. In RK3588 tests, the peer
received the current extended frame twice and local sockets received the
extra copy as ordinary RX. When a queued successor reused the buffer
while the retry was active, it was lost, leaving its echo and BQL
accounting pending and the TX queue stopped.
Use tx_tail to retry the original slot. The original request may have
completed by the time the driver processes the matching standard frame,
with its echo still in the RX FIFO. Processing that echo can wake the
queue while the retry is active, allowing a successor to overwrite the
shared TX registers.
Stop the queue and mark the retry pending under tx_lock before issuing
it. Complete the original echo and BQL accounting normally, but return
NETDEV_TX_BUSY for new submissions without consuming their skbs. A worker
polls both TX request bits with a one-jiffy delay, so progress does not
depend on another RX interrupt. Once both bits clear, it releases the
guard and wakes the queue if space is available. Repeated matches do not
postpone a pending check, but worker scheduling can delay the next TX.
Disable retries before publishing BUS_OFF and at close entry so the
retry worker cannot read TX request bits, requeue itself or wake the
queue. Close frees the IRQ before draining the worker. Restart waits
for in-flight IRQ handlers, then drains the worker before resetting the
controller. Both paths wait for the worker outside tx_lock.
On RK3588 with PREEMPT_RT, a higher-priority userspace task delayed the
CAN IRQ thread. The peer had already received the original extended frame
when the driver processed the matching standard frame. With only the
tx_tail change, the peer received a frame with the original CAN ID and
a payload containing bytes from a queued successor. The TX queue stalled.
With new submissions blocked until both TX request bits cleared, the peer
received all queued successors and no mixed frames in the same test.
The guard prevents later submissions from overwriting the shared TX
registers during a retry, but the driver can still filter out a matching
standard frame from another node and send a second complete copy of the
original extended frame.
Fixes: a5605d61c7dd ("can: rockchip_canfd: enable full TX-FIFO depth of 2")
Cc: stable@vger.kernel.org
Signed-off-by: Cunhao Lu <1579567540@qq.com>
---
Changes in v10:
- Block new submissions until both TX request bits clear after a retry.
- Move this patch after the TX/IRQ lifecycle synchronization.
Changes in v8:
- Apply the retry-slot correction after TX serialization.
---
drivers/net/can/rockchip/rockchip_canfd-core.c | 10 ++++
drivers/net/can/rockchip/rockchip_canfd-tx.c | 77 +++++++++++++++++++++++++-
drivers/net/can/rockchip/rockchip_canfd.h | 7 +++
3 files changed, 92 insertions(+), 2 deletions(-)
diff --git a/drivers/net/can/rockchip/rockchip_canfd-core.c b/drivers/net/can/rockchip/rockchip_canfd-core.c
index a5c6331191d1..38ffd06768a3 100644
--- a/drivers/net/can/rockchip/rockchip_canfd-core.c
+++ b/drivers/net/can/rockchip/rockchip_canfd-core.c
@@ -270,6 +270,8 @@ static void rkcanfd_chip_start(struct rkcanfd_priv *priv)
{
u32 reg;
+ rkcanfd_tx_retry_stop_sync(priv);
+
rkcanfd_chip_set_reset_mode(priv);
/* Receiving Filter: accept all */
@@ -329,12 +331,15 @@ static void rkcanfd_chip_start(struct rkcanfd_priv *priv)
netdev_dbg(priv->ndev, "%s: reg_mode=0x%08x\n", __func__,
rkcanfd_read(priv, RKCANFD_REG_MODE));
+ rkcanfd_tx_retry_start(priv);
}
static void rkcanfd_tx_disable(struct rkcanfd_priv *priv)
{
unsigned long flags;
+ rkcanfd_tx_retry_stop(priv);
+
spin_lock_irqsave(&priv->tx_lock, flags);
priv->tx_enabled = false;
netif_stop_queue(priv->ndev);
@@ -377,6 +382,8 @@ static void rkcanfd_chip_stop(struct rkcanfd_priv *priv, const enum can_state st
static void rkcanfd_chip_stop_sync(struct rkcanfd_priv *priv, const enum can_state state)
{
+ rkcanfd_tx_retry_stop_sync(priv);
+
priv->can.state = state;
rkcanfd_timestamp_stop_sync(priv);
@@ -811,6 +818,8 @@ static int rkcanfd_stop(struct net_device *ndev)
{
struct rkcanfd_priv *priv = netdev_priv(ndev);
+ rkcanfd_tx_retry_stop(priv);
+
close_candev_cancel_restart_sync(ndev);
rkcanfd_tx_disable(priv);
@@ -976,6 +985,7 @@ static int rkcanfd_probe(struct platform_device *pdev)
priv->can.do_get_berr_counter = rkcanfd_get_berr_counter;
priv->ndev = ndev;
spin_lock_init(&priv->tx_lock);
+ rkcanfd_tx_retry_init(priv);
match = device_get_match_data(&pdev->dev);
if (match) {
diff --git a/drivers/net/can/rockchip/rockchip_canfd-tx.c b/drivers/net/can/rockchip/rockchip_canfd-tx.c
index 7e3f478422e0..745029c23645 100644
--- a/drivers/net/can/rockchip/rockchip_canfd-tx.c
+++ b/drivers/net/can/rockchip/rockchip_canfd-tx.c
@@ -39,6 +39,10 @@ unsigned int rkcanfd_get_effective_tx_free(const struct rkcanfd_priv *priv)
{
lockdep_assert_held(&priv->tx_lock);
+ /* The original echo can precede completion of a retry. */
+ if (!priv->tx_retry_enabled || priv->tx_retry_pending)
+ return 0;
+
if (priv->devtype_data.quirks & RKCANFD_QUIRK_RK3568_ERRATUM_6 &&
rkcanfd_tx_tail_is_eff(priv))
return 0;
@@ -46,6 +50,68 @@ unsigned int rkcanfd_get_effective_tx_free(const struct rkcanfd_priv *priv)
return rkcanfd_get_tx_free(priv);
}
+static void rkcanfd_tx_retry_work(struct work_struct *work)
+{
+ struct rkcanfd_priv *priv = container_of(to_delayed_work(work),
+ struct rkcanfd_priv, tx_retry_work);
+ const u32 tx_req = RKCANFD_REG_CMD_TX0_REQ | RKCANFD_REG_CMD_TX1_REQ;
+ unsigned long flags;
+ u32 reg_cmd;
+
+ spin_lock_irqsave(&priv->tx_lock, flags);
+ if (!priv->tx_retry_enabled || !priv->tx_retry_pending)
+ goto out_unlock;
+
+ reg_cmd = rkcanfd_read(priv, RKCANFD_REG_CMD);
+
+ if (reg_cmd & tx_req) {
+ schedule_delayed_work(&priv->tx_retry_work, 1);
+ goto out_unlock;
+ }
+
+ priv->tx_retry_pending = false;
+
+ if (rkcanfd_get_effective_tx_free(priv) >= RKCANFD_TX_START_THRESHOLD)
+ netif_wake_queue(priv->ndev);
+
+out_unlock:
+ spin_unlock_irqrestore(&priv->tx_lock, flags);
+}
+
+void rkcanfd_tx_retry_init(struct rkcanfd_priv *priv)
+{
+ INIT_DELAYED_WORK(&priv->tx_retry_work, rkcanfd_tx_retry_work);
+}
+
+void rkcanfd_tx_retry_start(struct rkcanfd_priv *priv)
+{
+ unsigned long flags;
+
+ spin_lock_irqsave(&priv->tx_lock, flags);
+ priv->tx_retry_enabled = netif_running(priv->ndev);
+ priv->tx_retry_pending = false;
+ spin_unlock_irqrestore(&priv->tx_lock, flags);
+}
+
+void rkcanfd_tx_retry_stop(struct rkcanfd_priv *priv)
+{
+ unsigned long flags;
+
+ spin_lock_irqsave(&priv->tx_lock, flags);
+ priv->tx_retry_enabled = false;
+ priv->tx_retry_pending = false;
+ netif_stop_queue(priv->ndev);
+ spin_unlock_irqrestore(&priv->tx_lock, flags);
+
+ cancel_delayed_work(&priv->tx_retry_work);
+}
+
+void rkcanfd_tx_retry_stop_sync(struct rkcanfd_priv *priv)
+{
+ rkcanfd_tx_retry_stop(priv);
+ cancel_delayed_work_sync(&priv->tx_retry_work);
+}
+
static void rkcanfd_start_xmit_write_cmd(const struct rkcanfd_priv *priv,
const u32 reg_cmd)
{
@@ -63,12 +129,19 @@ static void rkcanfd_start_xmit_write_cmd(const struct rkcanfd_priv *priv,
void rkcanfd_xmit_retry(struct rkcanfd_priv *priv)
{
- const unsigned int tx_head = rkcanfd_get_tx_head(priv);
- const u32 reg_cmd = RKCANFD_REG_CMD_TX_REQ(tx_head);
+ const unsigned int tx_tail = rkcanfd_get_tx_tail(priv);
+ const u32 reg_cmd = RKCANFD_REG_CMD_TX_REQ(tx_tail);
lockdep_assert_held(&priv->tx_lock);
+ if (!priv->tx_retry_enabled)
+ return;
+
+ priv->tx_retry_pending = true;
+ netif_stop_queue(priv->ndev);
+
rkcanfd_start_xmit_write_cmd(priv, reg_cmd);
+ schedule_delayed_work(&priv->tx_retry_work, 1);
}
netdev_tx_t rkcanfd_start_xmit(struct sk_buff *skb, struct net_device *ndev)
diff --git a/drivers/net/can/rockchip/rockchip_canfd.h b/drivers/net/can/rockchip/rockchip_canfd.h
index 8fdb59025bfa..07fc5bc605df 100644
--- a/drivers/net/can/rockchip/rockchip_canfd.h
+++ b/drivers/net/can/rockchip/rockchip_canfd.h
@@ -487,10 +487,13 @@ struct rkcanfd_priv {
bool tx_enabled;
bool irq_suspended;
+ bool tx_retry_enabled;
+ bool tx_retry_pending;
struct cyclecounter cc;
struct timecounter tc;
struct delayed_work timestamp;
+ struct delayed_work tx_retry_work;
unsigned long work_delay_jiffies;
struct can_berr_counter bec;
@@ -564,6 +567,10 @@ void rkcanfd_timestamp_stop(struct rkcanfd_priv *priv);
void rkcanfd_timestamp_stop_sync(struct rkcanfd_priv *priv);
unsigned int rkcanfd_get_effective_tx_free(const struct rkcanfd_priv *priv);
+void rkcanfd_tx_retry_init(struct rkcanfd_priv *priv);
+void rkcanfd_tx_retry_start(struct rkcanfd_priv *priv);
+void rkcanfd_tx_retry_stop(struct rkcanfd_priv *priv);
+void rkcanfd_tx_retry_stop_sync(struct rkcanfd_priv *priv);
void rkcanfd_xmit_retry(struct rkcanfd_priv *priv);
netdev_tx_t rkcanfd_start_xmit(struct sk_buff *skb, struct net_device *ndev);
void rkcanfd_handle_tx_done_one(struct rkcanfd_priv *priv, const u32 ts,
--
2.34.1
^ permalink raw reply related [flat|nested] 15+ messages in thread
* [PATCH net v10 09/10] can: rockchip_canfd: prevent timestamp work from rearming after bus-off
2026-11-19 8:39 [PATCH net v10 00/10] can: fix Rockchip TX stalls and bus-off restart/close races Cunhao Lu via B4 Relay
` (7 preceding siblings ...)
2026-11-19 8:39 ` [PATCH net v10 08/10] can: rockchip_canfd: retry the outstanding TX buffer Cunhao Lu via B4 Relay
@ 2026-11-19 8:39 ` Cunhao Lu via B4 Relay
2026-11-19 8:39 ` [PATCH net v10 10/10] can: rockchip_canfd: serialize timecounter timestamp conversion Cunhao Lu via B4 Relay
9 siblings, 0 replies; 15+ messages in thread
From: Cunhao Lu via B4 Relay @ 2026-11-19 8:39 UTC (permalink / raw)
To: Marc Kleine-Budde, kernel, Vincent Mailhol, Heiko Stuebner,
Oliver Hartkopp, David S. Miller, Eric Dumazet, Jakub Kicinski,
Paolo Abeni, Simon Horman, Jonathan Corbet, Shuah Khan,
Randy Dunlap, Wolfgang Grandegger
Cc: linux-can, linux-arm-kernel, linux-rockchip, linux-kernel,
linux-doc, Cunhao Lu, stable
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
^ permalink raw reply related [flat|nested] 15+ messages in thread
* [PATCH net v10 10/10] can: rockchip_canfd: serialize timecounter timestamp conversion
2026-11-19 8:39 [PATCH net v10 00/10] can: fix Rockchip TX stalls and bus-off restart/close races Cunhao Lu via B4 Relay
` (8 preceding siblings ...)
2026-11-19 8:39 ` [PATCH net v10 09/10] can: rockchip_canfd: prevent timestamp work from rearming after bus-off Cunhao Lu via B4 Relay
@ 2026-11-19 8:39 ` Cunhao Lu via B4 Relay
2026-11-21 0:02 ` netdev-bot+sashiko
9 siblings, 1 reply; 15+ messages in thread
From: Cunhao Lu via B4 Relay @ 2026-11-19 8:39 UTC (permalink / raw)
To: Marc Kleine-Budde, kernel, Vincent Mailhol, Heiko Stuebner,
Oliver Hartkopp, David S. Miller, Eric Dumazet, Jakub Kicinski,
Paolo Abeni, Simon Horman, Jonathan Corbet, Shuah Khan,
Randy Dunlap, Wolfgang Grandegger
Cc: linux-can, linux-arm-kernel, linux-rockchip, linux-kernel,
linux-doc, Cunhao Lu, stable
From: Cunhao Lu <1579567540@qq.com>
The timestamp worker advances the timecounter with timecounter_read()
while RX, TX completion and error-frame paths use timecounter_cyc2time()
to convert hardware timestamps without holding timestamp_lock.
timecounter_read() updates frac, cycle_last and nsec in separate steps.
A concurrent conversion can observe the new cycle_last with the old
nsec and produce an incorrect timestamp.
Hold timestamp_lock across timecounter_cyc2time(), using the same lock
as timecounter_read() and timecounter_init(). Keep the SKB timestamp
assignment outside the critical section.
TX completion nests timestamp_lock inside tx_lock; no timestamp-lock
holder acquires tx_lock.
Fixes: 4e1a18bab124 ("can: rockchip_canfd: add hardware timestamping support")
Cc: stable@vger.kernel.org
Signed-off-by: Cunhao Lu <1579567540@qq.com>
---
drivers/net/can/rockchip/rockchip_canfd-timestamp.c | 5 ++++-
drivers/net/can/rockchip/rockchip_canfd.h | 4 ++--
2 files changed, 6 insertions(+), 3 deletions(-)
diff --git a/drivers/net/can/rockchip/rockchip_canfd-timestamp.c b/drivers/net/can/rockchip/rockchip_canfd-timestamp.c
index 7c7b95343aa8..76d522ee972e 100644
--- a/drivers/net/can/rockchip/rockchip_canfd-timestamp.c
+++ b/drivers/net/can/rockchip/rockchip_canfd-timestamp.c
@@ -15,13 +15,16 @@ static u64 rkcanfd_timestamp_read(struct cyclecounter *cc)
return rkcanfd_get_timestamp(priv);
}
-void rkcanfd_skb_set_timestamp(const struct rkcanfd_priv *priv,
+void rkcanfd_skb_set_timestamp(struct rkcanfd_priv *priv,
struct sk_buff *skb, const u32 timestamp)
{
struct skb_shared_hwtstamps *hwtstamps = skb_hwtstamps(skb);
+ unsigned long flags;
u64 ns;
+ spin_lock_irqsave(&priv->timestamp_lock, flags);
ns = timecounter_cyc2time(&priv->tc, timestamp);
+ spin_unlock_irqrestore(&priv->timestamp_lock, flags);
hwtstamps->hwtstamp = ns_to_ktime(ns);
}
diff --git a/drivers/net/can/rockchip/rockchip_canfd.h b/drivers/net/can/rockchip/rockchip_canfd.h
index 9fa0327b3e74..b84ab01eaab4 100644
--- a/drivers/net/can/rockchip/rockchip_canfd.h
+++ b/drivers/net/can/rockchip/rockchip_canfd.h
@@ -477,7 +477,7 @@ struct rkcanfd_priv {
/* Serialize TX submission/completion with bus-off and restart. */
spinlock_t tx_lock;
- /* Serialize timestamp work with start/stop. */
+ /* Protects tc and timestamp_active. */
spinlock_t timestamp_lock;
void __iomem *regs;
unsigned int tx_head;
@@ -561,7 +561,7 @@ void rkcanfd_ethtool_init(struct rkcanfd_priv *priv);
int rkcanfd_handle_rx_int(struct rkcanfd_priv *priv);
-void rkcanfd_skb_set_timestamp(const struct rkcanfd_priv *priv,
+void rkcanfd_skb_set_timestamp(struct rkcanfd_priv *priv,
struct sk_buff *skb, const u32 timestamp);
void rkcanfd_timestamp_work_init(struct rkcanfd_priv *priv);
void rkcanfd_timestamp_init(struct rkcanfd_priv *priv);
--
2.34.1
^ permalink raw reply related [flat|nested] 15+ messages in thread
* Re: [PATCH net v10 05/10] can: rockchip_canfd: cancel bus-off restart before closing the device
2026-11-19 8:39 ` [PATCH net v10 05/10] can: rockchip_canfd: cancel bus-off restart before closing the device Cunhao Lu via B4 Relay
@ 2026-11-21 0:02 ` netdev-bot+sashiko
0 siblings, 0 replies; 15+ messages in thread
From: netdev-bot+sashiko @ 2026-11-21 0:02 UTC (permalink / raw)
To: 1579567540
Cc: mkl, kernel, mailhol, heiko, socketcan, davem, edumazet, kuba,
pabeni, horms, corbet, skhan, rdunlap, wg, linux-can,
linux-arm-kernel, linux-rockchip, linux-kernel, linux-doc, stable
Thank you for your contribution! Sashiko AI review found more severe
issue(s) in other patches of this series. This mail is only an FYI, in case
you want to address the nit picks below when you respin.
Low priority / nit picks for your consideration:
- [Low] The commit message says late restart work (queued by a bus-off
IRQ after the early close_candev_cancel_restart_sync() in…
--
can: rockchip_canfd: cancel bus-off restart before closing the device
This patch adds a call to close_candev_cancel_restart_sync() at the start
of rkcanfd_stop(), so that a running bus-off restart is cancelled while
the IRQ, RX offload and controller are still available. Echo cleanup and
the final restart cancellation stay in close_candev().
> A bus-off IRQ can pass the scheduling check before close, then queue
> restart work after the early cancellation. The final cancellation after
> free_irq() drains that late work so it cannot survive close.
[Severity: Low]
Is this explanation complete? Late-queued restart work does not have to
wait for the final cancel. With a short restart_ms it can run anywhere
between the early cancel and close_candev().
That window covers rkcanfd_chip_interrupts_disable(), free_irq(),
can_rx_offload_disable() and rkcanfd_chip_stop_sync(). The final
cancel_delayed_work_sync() in close_candev() can't undo a restart that
has already run there.
In that window, only this check in can_restart() stops the restart:
drivers/net/can/dev/dev.c:can_restart() {
...
if (!netif_running(dev))
return -ENETDOWN;
...
}
Without that check, the work would reach rkcanfd_set_mode():
case CAN_MODE_START:
rkcanfd_chip_start(priv);
rkcanfd_chip_interrupts_enable(priv);
netif_wake_queue(ndev);
That check, and the "scheduling check" in can_bus_off():
restart = priv->restart_ms && netif_running(dev);
both come from the earlier patch in this series, "can: dev: check
netif_running() before bus-off recovery".
Neither the commit message nor the Cc: stable tag names that dependency.
The patch only needs "can: dev: add close_candev_cancel_restart_sync()"
to build. So a backport that takes this patch and the helper, but not
the netif_running() patch, would build cleanly and still have the
restart-after-teardown race.
Could the commit message say that late work which runs before the final
cancel is stopped by the netif_running() check in can_restart()? Could
the netif_running() patch also be listed as a stable prerequisite?
> diff --git a/drivers/net/can/rockchip/rockchip_canfd-core.c b/drivers/net/can/rockchip/rockchip_canfd-core.c
> index 1cae86973da5a..59a79fe057e66 100644
> --- a/drivers/net/can/rockchip/rockchip_canfd-core.c
> +++ b/drivers/net/can/rockchip/rockchip_canfd-core.c
> @@ -758,6 +758,7 @@ static int rkcanfd_stop(struct net_device *ndev)
> {
> struct rkcanfd_priv *priv = netdev_priv(ndev);
>
> + close_candev_cancel_restart_sync(ndev);
> netif_stop_queue(ndev);
>
> rkcanfd_chip_interrupts_disable(priv);
--
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261119-rockchip-canfd-restart-close-v10-0-df23913aa33a%40qq.com
^ permalink raw reply [flat|nested] 15+ messages in thread
* Re: [PATCH net v10 07/10] can: rockchip_canfd: serialize TX with bus-off and restart
2026-11-19 8:39 ` [PATCH net v10 07/10] can: rockchip_canfd: serialize TX with bus-off and restart Cunhao Lu via B4 Relay
@ 2026-11-21 0:02 ` netdev-bot+sashiko
0 siblings, 0 replies; 15+ messages in thread
From: netdev-bot+sashiko @ 2026-11-21 0:02 UTC (permalink / raw)
To: 1579567540
Cc: mkl, kernel, mailhol, heiko, socketcan, davem, edumazet, kuba,
pabeni, horms, corbet, skhan, rdunlap, wg, linux-can,
linux-arm-kernel, linux-rockchip, linux-kernel, linux-doc, stable
Thank you for your contribution! Sashiko AI review found more severe
issue(s) in other patches of this series. This mail is only an FYI, in case
you want to address the nit picks below when you respin.
Low priority / nit picks for your consideration:
- [Low] In struct rkcanfd_priv
(drivers/net/can/rockchip/rockchip_canfd.h), the patch replaces the…
Pre-existing issues:
- [Low] At this commit, priv->tc is read with no lock from the hardirq
handler, via rkcanfd_skb_set_timestamp() -> timecounter_cyc2time(), on…
--
can: rockchip_canfd: serialize TX with bus-off and restart
This patch uses tx_lock and a new tx_enabled flag to drain TX and RX-side
echo users before CAN_STATE_BUS_OFF is published, and it drops consumed RX
FIFO entries while TX is disabled. Before reinitializing the controller,
restart masks device interrupts, suspends the shared IRQ handler through
irq_suspended and synchronize_irq(), and drains the timestamp work.
Link: https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261111-rockchip-canfd-restart-close-v9-0-6643f3825ba3%40qq.com
> diff --git a/drivers/net/can/rockchip/rockchip_canfd-timestamp.c b/drivers/net/can/rockchip/rockchip_canfd-timestamp.c
> index 72774cd2f94b7..ad582afbbd682 100644
> --- a/drivers/net/can/rockchip/rockchip_canfd-timestamp.c
> +++ b/drivers/net/can/rockchip/rockchip_canfd-timestamp.c
> @@ -37,6 +37,11 @@ static void rkcanfd_timestamp_work(struct work_struct *work)
> schedule_delayed_work(&priv->timestamp, priv->work_delay_jiffies);
> }
>
> +void rkcanfd_timestamp_work_init(struct rkcanfd_priv *priv)
> +{
> + INIT_DELAYED_WORK(&priv->timestamp, rkcanfd_timestamp_work);
> +}
> +
[Severity: Low]
This isn't a bug introduced by this patch, but can priv->tc be updated
here while the IRQ handler is reading it?
rkcanfd_timestamp_work() updates the timecounter without taking a lock:
rkcanfd_timestamp_work() {
priv = container_of(delayed_work, struct rkcanfd_priv, timestamp);
timecounter_read(&priv->tc);
schedule_delayed_work(&priv->timestamp, priv->work_delay_jiffies);
}
The hardirq handler reads the same timecounter, also without a lock. It
does this on the RX, TX-done and error-skb paths through
rkcanfd_skb_set_timestamp():
ns = timecounter_cyc2time(&priv->tc, timestamp);
timecounter_read() writes cycle_last, frac and nsec as separate stores.
Suppose the worker runs on one CPU while the CAN IRQ converts a timestamp
on another. Can the conversion then combine a new cycle_last with an old
nsec and produce a wrong hardware timestamp?
Separately, the bus-off path in rkcanfd_chip_stop() only does a non-sync
cancel:
rkcanfd_timestamp_stop(priv);
__rkcanfd_chip_stop(priv, state);
Here rkcanfd_timestamp_stop() is cancel_delayed_work(). If the worker is
already running, it can re-arm itself and keep reading the counter while
the controller is in reset.
This patch adds rkcanfd_timestamp_stop_sync() only on the restart path.
That covers timecounter_init(), but not these two cases.
Both appear to be fixed later in this series:
"can: rockchip_canfd: serialize timecounter timestamp conversion" holds
timestamp_lock around timecounter_cyc2time(), timecounter_read() and
timecounter_init().
"can: rockchip_canfd: prevent timestamp work from rearming after
bus-off" uses disable_delayed_work() and a timestamp_active flag.
> diff --git a/drivers/net/can/rockchip/rockchip_canfd.h b/drivers/net/can/rockchip/rockchip_canfd.h
> index 077dd57d42de9..8fdb59025bfaf 100644
> --- a/drivers/net/can/rockchip/rockchip_canfd.h
> +++ b/drivers/net/can/rockchip/rockchip_canfd.h
> @@ -475,7 +475,7 @@ struct rkcanfd_priv {
> struct can_rx_offload offload;
> struct net_device *ndev;
>
> - /* Protect running TX/RX echo state, queue updates and TX commands. */
> + /* Serialize TX submission/completion with bus-off and restart. */
> spinlock_t tx_lock;
[ ... ]
> @@ -485,6 +485,9 @@ struct rkcanfd_priv {
> u32 reg_int_mask_default;
> struct rkcanfd_devtype_data devtype_data;
>
> + bool tx_enabled;
> + bool irq_suspended;
> +
[Severity: Low]
This isn't a bug, but the new tx_lock comment covers less than the lock
actually protects.
tx_lock still serializes TX command writes. rkcanfd_start_xmit_write_cmd()
asserts it because rkcanfd_start_xmit() and the RX-side
rkcanfd_xmit_retry() share the erratum-12 MODE/CMD/MODE sequence:
rkcanfd_start_xmit_write_cmd() {
lockdep_assert_held(&priv->tx_lock);
if (priv->devtype_data.quirks & RKCANFD_QUIRK_RK3568_ERRATUM_12)
rkcanfd_write(priv, RKCANFD_REG_MODE, priv->reg_mode_default |
...
}
The lock also still covers the echo ring and the queue/BQL updates in
rkcanfd_tx_tail_is_eff(), rkcanfd_get_effective_tx_free(),
rkcanfd_rxstx_filter() and rkcanfd_handle_tx_done_one().
Could the comment keep listing the TX command and echo ring duties?
Also, nothing says how the two new fields are synchronized. tx_enabled is
only accessed under tx_lock. irq_suspended takes no lock and relies on the
WRITE_ONCE()/smp_store_release() and smp_load_acquire() pairing plus
synchronize_irq().
Would a short note on each field help? At the end of the series the
tx_lock comment is still narrow, and the later tx_retry_enabled and
tx_retry_pending fields have no comment either.
--
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261119-rockchip-canfd-restart-close-v10-0-df23913aa33a%40qq.com
^ permalink raw reply [flat|nested] 15+ messages in thread
* Re: [PATCH net v10 08/10] can: rockchip_canfd: retry the outstanding TX buffer
2026-11-19 8:39 ` [PATCH net v10 08/10] can: rockchip_canfd: retry the outstanding TX buffer Cunhao Lu via B4 Relay
@ 2026-11-21 0:02 ` netdev-bot+sashiko
0 siblings, 0 replies; 15+ messages in thread
From: netdev-bot+sashiko @ 2026-11-21 0:02 UTC (permalink / raw)
To: 1579567540
Cc: mkl, kernel, mailhol, heiko, socketcan, davem, edumazet, kuba,
pabeni, horms, corbet, skhan, rdunlap, wg, linux-can,
linux-arm-kernel, linux-rockchip, linux-kernel, linux-doc, stable
Thank you for your contribution! Sashiko AI review found 3 potential
issue(s) to consider.
Critical: 0 · High: 0 · Medium: 1 · Low: 2
If you believe none of the issues are legitimate, please reply to each
issue of Medium severity or higher and explain why the AI is wrong.
If any of them are legitimate and you plan to respin, please let patchwork
know by including "pw-bot: cr" as a separate line at the end of your reply
(one such reply per series is enough).
- [Medium] When the matching standard frame is processed after the
original EFF frame has already completed, the erratum 6 retry puts a…
- [Low] rkcanfd_get_effective_tx_free()
(drivers/net/can/rockchip/rockchip_canfd-tx.c) now returns 0 whenever…
- [Low] rkcanfd_tx_retry_work() (drivers/net/can/rockchip/rockchip_canfd-
tx.c) re-arms itself every jiffy, with no limit and no timeout, for as…
--
can: rockchip_canfd: retry the outstanding TX buffer
This commit makes the erratum 6 retransmission use tx_tail instead of
tx_head, so the outstanding buffer is the one retried. It also adds a
tx_retry_pending guard that blocks new submissions until both TX request
bits clear, polled by a delayed worker, and disables retries on bus-off,
restart and close.
> diff --git a/drivers/net/can/rockchip/rockchip_canfd-tx.c b/drivers/net/can/rockchip/rockchip_canfd-tx.c
> index 7e3f478422e0e..745029c23645e 100644
> --- a/drivers/net/can/rockchip/rockchip_canfd-tx.c
> +++ b/drivers/net/can/rockchip/rockchip_canfd-tx.c
> @@ -39,6 +39,10 @@ unsigned int rkcanfd_get_effective_tx_free(const struct rkcanfd_priv *priv)
> {
> lockdep_assert_held(&priv->tx_lock);
>
> + /* The original echo can precede completion of a retry. */
> + if (!priv->tx_retry_enabled || priv->tx_retry_pending)
> + return 0;
> +
[Severity: Low]
This isn't a bug, but the comment only explains the tx_retry_pending part
of the condition.
The !priv->tx_retry_enabled term sets TX capacity to zero on every model.
That includes rkcanfd_devtype_data_rk3568v3, which has no
RKCANFD_QUIRK_RK3568_ERRATUM_6 and never reaches rkcanfd_xmit_retry().
On those models tx_retry_enabled is a second lifecycle flag that copies
tx_enabled. It is set at the end of rkcanfd_chip_start() rather than in
rkcanfd_chip_start_finish().
The current callers keep both flags in sync, so nothing stalls today.
Could the comment also cover this coupling with tx_enabled?
> if (priv->devtype_data.quirks & RKCANFD_QUIRK_RK3568_ERRATUM_6 &&
> rkcanfd_tx_tail_is_eff(priv))
> return 0;
> @@ -46,6 +50,68 @@ unsigned int rkcanfd_get_effective_tx_free(const struct rkcanfd_priv *priv)
> return rkcanfd_get_tx_free(priv);
> }
>
> +static void rkcanfd_tx_retry_work(struct work_struct *work)
> +{
> + struct rkcanfd_priv *priv = container_of(to_delayed_work(work),
> + struct rkcanfd_priv, tx_retry_work);
> + const u32 tx_req = RKCANFD_REG_CMD_TX0_REQ | RKCANFD_REG_CMD_TX1_REQ;
> + unsigned long flags;
> + u32 reg_cmd;
> +
> + spin_lock_irqsave(&priv->tx_lock, flags);
> + if (!priv->tx_retry_enabled || !priv->tx_retry_pending)
> + goto out_unlock;
> +
> + reg_cmd = rkcanfd_read(priv, RKCANFD_REG_CMD);
> +
> + if (reg_cmd & tx_req) {
> + schedule_delayed_work(&priv->tx_retry_work, 1);
> + goto out_unlock;
> + }
[Severity: Low]
Is there a limit on how many times this re-arms?
rkcanfd_chip_start() enables RKCANFD_REG_MODE_AUTO_RETX_MODE. If the
retried frame never gets an ACK (for example the bus is unplugged or the
only other node goes silent), the controller keeps retransmitting and
TX_REQ stays set.
Under the ISO 11898-1 fault confinement exception, an error-passive
transmitter that sees only ACK errors does not increase TEC. Bus-off is
never reached, so rkcanfd_tx_disable()->rkcanfd_tx_retry_stop() never
runs.
Wouldn't this worker then take tx_lock and read RKCANFD_REG_CMD once per
jiffy until the interface is closed or restarted? An un-ACKed frame also
stalled TX before this patch, but the per-jiffy polling with no limit is
new.
In the spurious match case the stuck request is the untracked duplicate.
The queue then stays stopped even though tx_pending is 0.
> +
> + priv->tx_retry_pending = false;
> +
> + if (rkcanfd_get_effective_tx_free(priv) >= RKCANFD_TX_START_THRESHOLD)
> + netif_wake_queue(priv->ndev);
[Severity: Medium]
Is it safe to release the guard as soon as both TX_REQ bits read clear?
rkcanfd_xmit_retry() starts a second hardware transmission of the frame at
tx_tail, but it creates no echo_skb slot or pending count for it. Clear
TX_REQ bits only mean the hardware finished sending. The RXSTX
self-reception of the retried copy may still be in the RX FIFO.
Take the ordering the commit message describes, where the original request
has already completed when the matching standard frame is processed:
1. Standard frame S matches pending EFF frame B at tx_tail.
rkcanfd_rxstx_filter() calls rkcanfd_xmit_retry(), and B is sent again.
2. B's original echo, still in the RX FIFO, matches echo_skb[tx_tail].
tx_tail advances and tx_pending becomes 0.
3. The retry finishes. This worker sees TX_REQ clear,
rkcanfd_get_effective_tx_free() returns 2, and the queue is woken.
When the echo of the retry is processed with nothing pending,
rkcanfd_handle_rx_int_one() skips the filter:
drivers/net/can/rockchip/rockchip_canfd-rx.c:rkcanfd_handle_rx_int_one() {
...
if (rkcanfd_get_tx_pending(priv))
err = rkcanfd_rxstx_filter(priv, cfd, header->ts, &tx_done);
...
}
Wouldn't the duplicate B then reach local sockets as ordinary RX? The
commit message lists this as an observed symptom, but it seems to remain
for this ordering.
Now suppose the IRQ thread is delayed, as in the PREEMPT_RT scenario from
the commit message, and a successor C has been queued at tx_tail. The
retry echo is then compared against echo_skb[tx_tail] = C in
rkcanfd_rxstx_filter():
rkcanfd_handle_tx_done_one(priv, ts, &frame_len);
WRITE_ONCE(priv->tx_tail, priv->tx_tail + 1);
netif_subqueue_completed_wake(priv->ndev, 0, 1, frame_len,
rkcanfd_get_effective_tx_free(priv),
RKCANFD_TX_START_THRESHOLD);
Periodic CAN traffic often repeats the same ID, flags, length and payload.
If C matches B in all of these, can C's echo and BQL completion fire while
C is still pending in hardware?
The queue would then be woken. The next submission could rewrite the
shared TX registers while C's request is still outstanding, which is the
hazard this guard was added to prevent. C's real echo would later show up
as spurious RX, or be matched against the wrong slot.
The closing paragraph of the commit message only mentions the second copy
on the bus:
"but the driver can still filter out a matching standard frame from
another node and send a second complete copy of the original extended
frame."
Should the echo of the retry be accounted for? If not, could the commit
message also mention the local phantom RX and the misattributed
completion?
> +
> +out_unlock:
> + spin_unlock_irqrestore(&priv->tx_lock, flags);
> +}
[ ... ]
--
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261119-rockchip-canfd-restart-close-v10-0-df23913aa33a%40qq.com
^ permalink raw reply [flat|nested] 15+ messages in thread
* Re: [PATCH net v10 10/10] can: rockchip_canfd: serialize timecounter timestamp conversion
2026-11-19 8:39 ` [PATCH net v10 10/10] can: rockchip_canfd: serialize timecounter timestamp conversion Cunhao Lu via B4 Relay
@ 2026-11-21 0:02 ` netdev-bot+sashiko
0 siblings, 0 replies; 15+ messages in thread
From: netdev-bot+sashiko @ 2026-11-21 0:02 UTC (permalink / raw)
To: 1579567540
Cc: mkl, kernel, mailhol, heiko, socketcan, davem, edumazet, kuba,
pabeni, horms, corbet, skhan, rdunlap, wg, linux-can,
linux-arm-kernel, linux-rockchip, linux-kernel, linux-doc, stable
Thank you for your contribution! Sashiko AI review found more severe
issue(s) in other patches of this series. This mail is only an FYI, in case
you want to address the nit picks below when you respin.
Low priority / nit picks for your consideration:
- [Low] The patch is tagged `Cc: stable@vger.kernel.org` with `Fixes:
4e1a18bab124 ("can: rockchip_canfd: add hardware timestamping…
--
can: rockchip_canfd: serialize timecounter timestamp conversion
This commit holds priv->timestamp_lock around timecounter_cyc2time() in
rkcanfd_skb_set_timestamp(). That serializes the RX, TX completion and
error-frame timestamp conversion against timecounter_read() in the
timestamp worker. The comment on timestamp_lock in struct rkcanfd_priv is
also changed to say it protects tc and timestamp_active.
> Hold timestamp_lock across timecounter_cyc2time(), using the same lock
> as timecounter_read() and timecounter_init(). Keep the SKB timestamp
> assignment outside the critical section.
[ ... ]
> Fixes: 4e1a18bab124 ("can: rockchip_canfd: add hardware timestamping support")
> Cc: stable@vger.kernel.org
[Severity: Low]
Should the stable tag mention that this depends on the earlier patch in
the series, "can: rockchip_canfd: prevent timestamp work from rearming
after bus-off"?
The timestamp_lock and timestamp_active members of struct rkcanfd_priv are
not in 4e1a18bab124. The earlier patch adds them. It also puts
timecounter_read() in rkcanfd_timestamp_work() and timecounter_init() in
rkcanfd_timestamp_start() under the lock:
spin_lock_irqsave(&priv->timestamp_lock, flags);
if (priv->timestamp_active)
timecounter_read(&priv->tc);
spin_unlock_irqrestore(&priv->timestamp_lock, flags);
If this patch is picked on its own for a stable kernel that only has
4e1a18bab124, it will not build. A backporter could add just the lock to
get it building. timecounter_read() and timecounter_init() would still
not take that lock, so the race would stay open.
Other patches in the series list their stable prerequisites, for example
"# prerequisite for the Rockchip restart/close fix". Would something like
this work here?
Cc: stable@vger.kernel.org # depends on "can: rockchip_canfd: prevent timestamp work from rearming after bus-off"
--
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261119-rockchip-canfd-restart-close-v10-0-df23913aa33a%40qq.com
^ permalink raw reply [flat|nested] 15+ messages in thread
end of thread, other threads:[~2026-11-21 0:03 UTC | newest]
Thread overview: 15+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-19 8:39 [PATCH net v10 00/10] can: fix Rockchip TX stalls and bus-off restart/close races Cunhao Lu via B4 Relay
2026-11-19 8:39 ` [PATCH net v10 01/10] can: rockchip_canfd: prevent TX stall on echo skb failure Cunhao Lu via B4 Relay
2026-11-19 8:39 ` [PATCH net v10 02/10] can: rockchip_canfd: serialize TX state and command writes Cunhao Lu via B4 Relay
2026-11-19 8:39 ` [PATCH net v10 03/10] can: dev: check netif_running() before bus-off recovery Cunhao Lu via B4 Relay
2026-11-19 8:39 ` [PATCH net v10 04/10] can: dev: add close_candev_cancel_restart_sync() Cunhao Lu via B4 Relay
2026-11-19 8:39 ` [PATCH net v10 05/10] can: rockchip_canfd: cancel bus-off restart before closing the device Cunhao Lu via B4 Relay
2026-11-21 0:02 ` netdev-bot+sashiko
2026-11-19 8:39 ` [PATCH net v10 06/10] can: rockchip_canfd: disable RX offload on open failure Cunhao Lu via B4 Relay
2026-11-19 8:39 ` [PATCH net v10 07/10] can: rockchip_canfd: serialize TX with bus-off and restart Cunhao Lu via B4 Relay
2026-11-21 0:02 ` netdev-bot+sashiko
2026-11-19 8:39 ` [PATCH net v10 08/10] can: rockchip_canfd: retry the outstanding TX buffer Cunhao Lu via B4 Relay
2026-11-21 0:02 ` netdev-bot+sashiko
2026-11-19 8:39 ` [PATCH net v10 09/10] can: rockchip_canfd: prevent timestamp work from rearming after bus-off Cunhao Lu via B4 Relay
2026-11-19 8:39 ` [PATCH net v10 10/10] can: rockchip_canfd: serialize timecounter timestamp conversion Cunhao Lu via B4 Relay
2026-11-21 0:02 ` netdev-bot+sashiko
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox