* [PATCH v5 0/3] can_rx_offload keeps a lockless irq_queue that the IRQ handlers fill and that is later spliced under skb_queue.lock into the NAPI-facing skb_queue. This works as long as a single context fills the irq_queue. flexcan with FLEXCAN_QUIRK_SECONDARY_MB_IRQ and mcf5441x use two mailbox IRQ lines. When those are affined to different CPUs the two handlers can enqueue into the same list at the same time and corrupt it.
@ 2026-09-07 10:49 Ciprian Costea
2026-09-07 10:49 ` [PATCH v5 1/3] can: rx-offload: make skb_irq_queue per-CPU Ciprian Costea
` (3 more replies)
0 siblings, 4 replies; 9+ messages in thread
From: Ciprian Costea @ 2026-09-07 10:49 UTC (permalink / raw)
To: Marc Kleine-Budde, Vincent Mailhol, Nicolas Ferre,
Alexandre Belloni, Claudiu Beznea, Kurt Van Dijck
Cc: linux-can, linux-arm-kernel, linux-kernel, imx,
NXP S32 Linux Team, Ciprian Marian Costea
From: Ciprian Marian Costea <ciprianmarian.costea@oss.nxp.com>
This series:
1. Makes the irq_queue per-CPU so the handlers no longer share a list.
2. Fixes at91_can rx-offload teardown.
3. Checks the can_rx_offload_add_manual() return value in gs_usb.
Changes since v4:
- rx-offload: expand the comment above the for_each_possible_cpu() loop
in can_rx_offload_threaded_irq_finish() to add the single-producer
assumption (IRQ requested with IRQF_ONESHOT / handler non-reentrant).
Suggested by Haibo Chen.
- rx-offload: add Reviewed-by: Haibo Chen <haibo.chen@nxp.com>
Changes since v3:
- In gs_usb driver, check the can_rx_offload_add_manual() return value,
the same NULL-deref the per-CPU change exposes.
Changes since v2:
- at91_can: also add can_rx_offload_del() on the register_candev() error
path and check the can_rx_offload_add_timestamp() return value.
Changes since v1:
- The enqueue helpers used this_cpu_ptr() without disabling preemption.
All four enqueue helpers now use get_cpu_ptr()/put_cpu_ptr().
- Guard can_rx_offload_del() against skb_irq_queue == NULL.
- Fix 'at91_can' memory leak by adding missing 'can_rx_offload_del'.
Ciprian Marian Costea (3):
can: rx-offload: make skb_irq_queue per-CPU
can: at91_can: fix rx-offload cleanup on unbind and probe errors
can: gs_usb: check can_rx_offload_add_manual() return value
drivers/net/can/at91_can.c | 10 +++-
drivers/net/can/dev/rx-offload.c | 88 +++++++++++++++++++++++++++-----
drivers/net/can/usb/gs_usb.c | 5 +-
include/linux/can/rx-offload.h | 2 +-
4 files changed, 87 insertions(+), 18 deletions(-)
--
2.43.0
^ permalink raw reply [flat|nested] 9+ messages in thread
* [PATCH v5 1/3] can: rx-offload: make skb_irq_queue per-CPU
2026-09-07 10:49 [PATCH v5 0/3] can_rx_offload keeps a lockless irq_queue that the IRQ handlers fill and that is later spliced under skb_queue.lock into the NAPI-facing skb_queue. This works as long as a single context fills the irq_queue. flexcan with FLEXCAN_QUIRK_SECONDARY_MB_IRQ and mcf5441x use two mailbox IRQ lines. When those are affined to different CPUs the two handlers can enqueue into the same list at the same time and corrupt it Ciprian Costea
@ 2026-09-07 10:49 ` Ciprian Costea
2026-09-07 11:12 ` sashiko-bot
2026-09-07 10:49 ` [PATCH v5 2/3] can: at91_can: fix rx-offload cleanup on unbind and probe errors Ciprian Costea
` (2 subsequent siblings)
3 siblings, 1 reply; 9+ messages in thread
From: Ciprian Costea @ 2026-09-07 10:49 UTC (permalink / raw)
To: Marc Kleine-Budde, Vincent Mailhol, Nicolas Ferre,
Alexandre Belloni, Claudiu Beznea, Kurt Van Dijck
Cc: linux-can, linux-arm-kernel, linux-kernel, imx,
NXP S32 Linux Team, Ciprian Marian Costea, Haibo Chen
From: Ciprian Marian Costea <ciprianmarian.costea@oss.nxp.com>
skb_irq_queue is filled by the IRQ handlers using the lockless
__skb_queue_add_sort() / __skb_queue_tail() helpers and later spliced
into skb_queue under skb_queue.lock by can_rx_offload_irq_finish() and
can_rx_offload_threaded_irq_finish().
This is only safe while a single context fills skb_irq_queue. FlexCAN
on NXP S32G2 (FLEXCAN_QUIRK_SECONDARY_MB_IRQ) uses two mailbox IRQ
lines, one for MB0-7 and one for MB8-63; MCF5441X similarly splits its
mailbox interrupt. When these lines are affined to different CPUs both
handlers can run at the same time and enqueue into the same sk_buff_head
concurrently, corrupting its list.
Allocate skb_irq_queue per-CPU so the handlers no longer share a list,
keeping the enqueue path lock-free. Access the per-CPU queue via
get_cpu_ptr()/put_cpu_ptr() in the enqueue helpers: this disables
preemption around the lockless __skb_queue_*() operation.
can_rx_offload_irq_finish() runs in the same context as its enqueues and
splices this_cpu_ptr(). can_rx_offload_threaded_irq_finish() may have
been migrated after its enqueues, so it splices every possible CPU's
queue; this is safe because each per-CPU queue has a single producer and
that producer runs with preemption disabled, so it cannot race the
splice.
Cross-line frames are now sorted by timestamp only within a CPU's queue
and appended across CPUs on splice; each skb keeps its own timestamp.
Fixes: c757096ea103 ("can: rx-offload: add skb queue for use during ISR")
Signed-off-by: Ciprian Marian Costea <ciprianmarian.costea@oss.nxp.com>
Reviewed-by: Haibo Chen <haibo.chen@nxp.com>
---
drivers/net/can/dev/rx-offload.c | 88 +++++++++++++++++++++++++++-----
include/linux/can/rx-offload.h | 2 +-
2 files changed, 75 insertions(+), 15 deletions(-)
diff --git a/drivers/net/can/dev/rx-offload.c b/drivers/net/can/dev/rx-offload.c
index 46e7b6db4a1e..7616a16d9049 100644
--- a/drivers/net/can/dev/rx-offload.c
+++ b/drivers/net/can/dev/rx-offload.c
@@ -7,6 +7,7 @@
#include <linux/can/dev.h>
#include <linux/can/rx-offload.h>
+#include <linux/percpu.h>
struct can_rx_offload_cb {
u32 timestamp;
@@ -175,9 +176,18 @@ can_rx_offload_offload_one(struct can_rx_offload *offload, unsigned int n)
int can_rx_offload_irq_offload_timestamp(struct can_rx_offload *offload,
u64 pending)
{
+ struct sk_buff_head *irq_queue;
unsigned int i;
int received = 0;
+ /*
+ * get_cpu_ptr() disables preemption so that the lockless
+ * __skb_queue_*() below operate on the current CPU's queue without
+ * racing a migration. This also keeps this_cpu_ptr() valid when a
+ * driver enqueues from a preemptible (threaded IRQ) context.
+ */
+ irq_queue = get_cpu_ptr(offload->skb_irq_queue);
+
for (i = offload->mb_first;
can_rx_offload_le(offload, i, offload->mb_last);
can_rx_offload_inc(offload, &i)) {
@@ -190,20 +200,25 @@ int can_rx_offload_irq_offload_timestamp(struct can_rx_offload *offload,
if (IS_ERR_OR_NULL(skb))
continue;
- __skb_queue_add_sort(&offload->skb_irq_queue, skb,
+ __skb_queue_add_sort(irq_queue, skb,
can_rx_offload_compare);
received++;
}
+ put_cpu_ptr(offload->skb_irq_queue);
+
return received;
}
EXPORT_SYMBOL_GPL(can_rx_offload_irq_offload_timestamp);
int can_rx_offload_irq_offload_fifo(struct can_rx_offload *offload)
{
+ struct sk_buff_head *irq_queue;
struct sk_buff *skb;
int received = 0;
+ irq_queue = get_cpu_ptr(offload->skb_irq_queue);
+
while (1) {
skb = can_rx_offload_offload_one(offload, 0);
if (IS_ERR(skb))
@@ -211,10 +226,12 @@ int can_rx_offload_irq_offload_fifo(struct can_rx_offload *offload)
if (!skb)
break;
- __skb_queue_tail(&offload->skb_irq_queue, skb);
+ __skb_queue_tail(irq_queue, skb);
received++;
}
+ put_cpu_ptr(offload->skb_irq_queue);
+
return received;
}
EXPORT_SYMBOL_GPL(can_rx_offload_irq_offload_fifo);
@@ -222,6 +239,7 @@ EXPORT_SYMBOL_GPL(can_rx_offload_irq_offload_fifo);
int can_rx_offload_queue_timestamp(struct can_rx_offload *offload,
struct sk_buff *skb, u32 timestamp)
{
+ struct sk_buff_head *irq_queue;
struct can_rx_offload_cb *cb;
if (skb_queue_len(&offload->skb_queue) >
@@ -233,8 +251,9 @@ int can_rx_offload_queue_timestamp(struct can_rx_offload *offload,
cb = can_rx_offload_get_cb(skb);
cb->timestamp = timestamp;
- __skb_queue_add_sort(&offload->skb_irq_queue, skb,
- can_rx_offload_compare);
+ irq_queue = get_cpu_ptr(offload->skb_irq_queue);
+ __skb_queue_add_sort(irq_queue, skb, can_rx_offload_compare);
+ put_cpu_ptr(offload->skb_irq_queue);
return 0;
}
@@ -268,13 +287,17 @@ EXPORT_SYMBOL_GPL(can_rx_offload_get_echo_skb_queue_timestamp);
int can_rx_offload_queue_tail(struct can_rx_offload *offload,
struct sk_buff *skb)
{
+ struct sk_buff_head *irq_queue;
+
if (skb_queue_len(&offload->skb_queue) >
offload->skb_queue_len_max) {
dev_kfree_skb_any(skb);
return -ENOBUFS;
}
- __skb_queue_tail(&offload->skb_irq_queue, skb);
+ irq_queue = get_cpu_ptr(offload->skb_irq_queue);
+ __skb_queue_tail(irq_queue, skb);
+ put_cpu_ptr(offload->skb_irq_queue);
return 0;
}
@@ -307,14 +330,15 @@ EXPORT_SYMBOL_GPL(can_rx_offload_get_echo_skb_queue_tail);
void can_rx_offload_irq_finish(struct can_rx_offload *offload)
{
+ struct sk_buff_head *irq_queue = this_cpu_ptr(offload->skb_irq_queue);
unsigned long flags;
int queue_len;
- if (skb_queue_empty_lockless(&offload->skb_irq_queue))
+ if (skb_queue_empty_lockless(irq_queue))
return;
spin_lock_irqsave(&offload->skb_queue.lock, flags);
- skb_queue_splice_tail_init(&offload->skb_irq_queue, &offload->skb_queue);
+ skb_queue_splice_tail_init(irq_queue, &offload->skb_queue);
spin_unlock_irqrestore(&offload->skb_queue.lock, flags);
queue_len = skb_queue_len(&offload->skb_queue);
@@ -330,15 +354,34 @@ void can_rx_offload_threaded_irq_finish(struct can_rx_offload *offload)
{
unsigned long flags;
int queue_len;
-
- if (skb_queue_empty_lockless(&offload->skb_irq_queue))
- return;
-
+ int cpu;
+
+ /*
+ * Splice every CPU's queue: unlike the non-threaded
+ * can_rx_offload_irq_finish(), a threaded handler may be migrated
+ * between the enqueue and this splice, so the frames may sit on a
+ * different CPU's queue. This is only safe because a given per-CPU
+ * queue has a single producer (the enqueue on that CPU is
+ * non-preemptible), so no producer can race this splice.
+ *
+ * This assumes a single threaded handler context per offload instance
+ * (IRQ requested with IRQF_ONESHOT / handler non-reentrant), so each
+ * per-CPU queue has exactly one producer. If that changes, this
+ * cross-CPU splice of lockless queues would need additional locking.
+ */
spin_lock_irqsave(&offload->skb_queue.lock, flags);
- skb_queue_splice_tail_init(&offload->skb_irq_queue, &offload->skb_queue);
+ for_each_possible_cpu(cpu) {
+ struct sk_buff_head *irq_queue;
+
+ irq_queue = per_cpu_ptr(offload->skb_irq_queue, cpu);
+ skb_queue_splice_tail_init(irq_queue, &offload->skb_queue);
+ }
spin_unlock_irqrestore(&offload->skb_queue.lock, flags);
queue_len = skb_queue_len(&offload->skb_queue);
+ if (!queue_len)
+ return;
+
if (queue_len > offload->skb_queue_len_max / 8)
netdev_dbg(offload->dev, "%s: queue_len=%d\n",
__func__, queue_len);
@@ -353,13 +396,21 @@ static int can_rx_offload_init_queue(struct net_device *dev,
struct can_rx_offload *offload,
unsigned int weight)
{
+ int cpu;
+
offload->dev = dev;
/* Limit queue len to 4x the weight (rounded to next power of two) */
offload->skb_queue_len_max = 2 << fls(weight);
offload->skb_queue_len_max *= 4;
skb_queue_head_init(&offload->skb_queue);
- __skb_queue_head_init(&offload->skb_irq_queue);
+
+ offload->skb_irq_queue = alloc_percpu(struct sk_buff_head);
+ if (!offload->skb_irq_queue)
+ return -ENOMEM;
+
+ for_each_possible_cpu(cpu)
+ __skb_queue_head_init(per_cpu_ptr(offload->skb_irq_queue, cpu));
netif_napi_add_weight(dev, &offload->napi, can_rx_offload_napi_poll,
weight);
@@ -420,8 +471,17 @@ EXPORT_SYMBOL_GPL(can_rx_offload_enable);
void can_rx_offload_del(struct can_rx_offload *offload)
{
+ int cpu;
+
netif_napi_del(&offload->napi);
skb_queue_purge(&offload->skb_queue);
- __skb_queue_purge(&offload->skb_irq_queue);
+
+ if (!offload->skb_irq_queue)
+ return;
+
+ for_each_possible_cpu(cpu)
+ __skb_queue_purge(per_cpu_ptr(offload->skb_irq_queue, cpu));
+
+ free_percpu(offload->skb_irq_queue);
}
EXPORT_SYMBOL_GPL(can_rx_offload_del);
diff --git a/include/linux/can/rx-offload.h b/include/linux/can/rx-offload.h
index d29bb4521947..1b9e2a8ab39a 100644
--- a/include/linux/can/rx-offload.h
+++ b/include/linux/can/rx-offload.h
@@ -20,7 +20,7 @@ struct can_rx_offload {
bool drop);
struct sk_buff_head skb_queue;
- struct sk_buff_head skb_irq_queue;
+ struct sk_buff_head __percpu *skb_irq_queue;
u32 skb_queue_len_max;
unsigned int mb_first;
--
2.43.0
^ permalink raw reply related [flat|nested] 9+ messages in thread
* [PATCH v5 2/3] can: at91_can: fix rx-offload cleanup on unbind and probe errors
2026-09-07 10:49 [PATCH v5 0/3] can_rx_offload keeps a lockless irq_queue that the IRQ handlers fill and that is later spliced under skb_queue.lock into the NAPI-facing skb_queue. This works as long as a single context fills the irq_queue. flexcan with FLEXCAN_QUIRK_SECONDARY_MB_IRQ and mcf5441x use two mailbox IRQ lines. When those are affined to different CPUs the two handlers can enqueue into the same list at the same time and corrupt it Ciprian Costea
2026-09-07 10:49 ` [PATCH v5 1/3] can: rx-offload: make skb_irq_queue per-CPU Ciprian Costea
@ 2026-09-07 10:49 ` Ciprian Costea
2026-09-07 10:49 ` [PATCH v5 3/3] can: gs_usb: check can_rx_offload_add_manual() return value Ciprian Costea
2026-09-07 13:49 ` [PATCH v5 0/3] can_rx_offload keeps a lockless irq_queue that the IRQ handlers fill and that is later spliced under skb_queue.lock into the NAPI-facing skb_queue. This works as long as a single context fills the irq_queue. flexcan with FLEXCAN_QUIRK_SECONDARY_MB_IRQ and mcf5441x use two mailbox IRQ lines. When those are affined to different CPUs the two handlers can enqueue into the same list at the same time and corrupt it Marc Kleine-Budde
3 siblings, 0 replies; 9+ messages in thread
From: Ciprian Costea @ 2026-09-07 10:49 UTC (permalink / raw)
To: Marc Kleine-Budde, Vincent Mailhol, Nicolas Ferre,
Alexandre Belloni, Claudiu Beznea, Kurt Van Dijck
Cc: linux-can, linux-arm-kernel, linux-kernel, imx,
NXP S32 Linux Team, Ciprian Marian Costea
From: Ciprian Marian Costea <ciprianmarian.costea@oss.nxp.com>
Making can_rx_offload's skb_irq_queue per-CPU (previous patch) adds an
alloc_percpu() to can_rx_offload_add_*(). That allocation has to be freed
on teardown and can fail with -ENOMEM, which exposes three problems in
at91_can:
- at91_can_remove() does not call can_rx_offload_del(), so the NAPI
instance and the per-CPU queues are leaked on unbind and module removal.
- The probe error path after a failed register_candev() jumps straight to
free_candev() without can_rx_offload_del() and leaks the same objects.
- The return value of can_rx_offload_add_timestamp() is ignored. It can now
return -ENOMEM with offload->skb_irq_queue == NULL, probe still succeeds,
and the first RX interrupt dereferences that NULL pointer in
can_rx_offload_irq_offload_timestamp() via get_cpu_ptr().
Check the return value and call can_rx_offload_del() from at91_can_remove()
and from a new error label taken when register_candev() fails.
Fixes: 137f59d5dab4 ("can: at91_can: switch to rx-offload implementation")
Signed-off-by: Ciprian Marian Costea <ciprianmarian.costea@oss.nxp.com>
---
drivers/net/can/at91_can.c | 10 ++++++++--
1 file changed, 8 insertions(+), 2 deletions(-)
diff --git a/drivers/net/can/at91_can.c b/drivers/net/can/at91_can.c
index 58da323f14d7..3f6c5bb373d3 100644
--- a/drivers/net/can/at91_can.c
+++ b/drivers/net/can/at91_can.c
@@ -1123,7 +1123,9 @@ static int at91_can_probe(struct platform_device *pdev)
priv->offload.mb_first = devtype_data->rx_first;
priv->offload.mb_last = devtype_data->rx_last;
- can_rx_offload_add_timestamp(dev, &priv->offload);
+ err = can_rx_offload_add_timestamp(dev, &priv->offload);
+ if (err)
+ goto exit_free;
if (transceiver)
priv->can.bitrate_max = transceiver->attrs.max_link_rate;
@@ -1137,7 +1139,7 @@ static int at91_can_probe(struct platform_device *pdev)
err = register_candev(dev);
if (err) {
dev_err(&pdev->dev, "registering netdev failed\n");
- goto exit_free;
+ goto exit_offload;
}
dev_info(&pdev->dev, "device registered (reg_base=%p, irq=%d)\n",
@@ -1145,6 +1147,8 @@ static int at91_can_probe(struct platform_device *pdev)
return 0;
+ exit_offload:
+ can_rx_offload_del(&priv->offload);
exit_free:
free_candev(dev);
exit_iounmap:
@@ -1165,6 +1169,8 @@ static void at91_can_remove(struct platform_device *pdev)
unregister_netdev(dev);
+ can_rx_offload_del(&priv->offload);
+
iounmap(priv->reg_base);
res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
--
2.43.0
^ permalink raw reply related [flat|nested] 9+ messages in thread
* [PATCH v5 3/3] can: gs_usb: check can_rx_offload_add_manual() return value
2026-09-07 10:49 [PATCH v5 0/3] can_rx_offload keeps a lockless irq_queue that the IRQ handlers fill and that is later spliced under skb_queue.lock into the NAPI-facing skb_queue. This works as long as a single context fills the irq_queue. flexcan with FLEXCAN_QUIRK_SECONDARY_MB_IRQ and mcf5441x use two mailbox IRQ lines. When those are affined to different CPUs the two handlers can enqueue into the same list at the same time and corrupt it Ciprian Costea
2026-09-07 10:49 ` [PATCH v5 1/3] can: rx-offload: make skb_irq_queue per-CPU Ciprian Costea
2026-09-07 10:49 ` [PATCH v5 2/3] can: at91_can: fix rx-offload cleanup on unbind and probe errors Ciprian Costea
@ 2026-09-07 10:49 ` Ciprian Costea
2026-09-07 13:49 ` [PATCH v5 0/3] can_rx_offload keeps a lockless irq_queue that the IRQ handlers fill and that is later spliced under skb_queue.lock into the NAPI-facing skb_queue. This works as long as a single context fills the irq_queue. flexcan with FLEXCAN_QUIRK_SECONDARY_MB_IRQ and mcf5441x use two mailbox IRQ lines. When those are affined to different CPUs the two handlers can enqueue into the same list at the same time and corrupt it Marc Kleine-Budde
3 siblings, 0 replies; 9+ messages in thread
From: Ciprian Costea @ 2026-09-07 10:49 UTC (permalink / raw)
To: Marc Kleine-Budde, Vincent Mailhol, Nicolas Ferre,
Alexandre Belloni, Claudiu Beznea, Kurt Van Dijck
Cc: linux-can, linux-arm-kernel, linux-kernel, imx,
NXP S32 Linux Team, Ciprian Marian Costea
From: Ciprian Marian Costea <ciprianmarian.costea@oss.nxp.com>
gs_make_candev() ignores the return value of can_rx_offload_add_manual().
Once can_rx_offload's skb_irq_queue is made per-CPU, that call allocates a
per-CPU queue and can fail with -ENOMEM, leaving offload->skb_irq_queue ==
NULL.
Check the return value and exit via the existing free_candev() on error
path.
Fixes: 24bc41b45583 ("can: gs_usb: convert to NAPI/rx-offload to avoid OoO reception")
Signed-off-by: Ciprian Marian Costea <ciprianmarian.costea@oss.nxp.com>
---
drivers/net/can/usb/gs_usb.c | 5 ++++-
1 file changed, 4 insertions(+), 1 deletion(-)
diff --git a/drivers/net/can/usb/gs_usb.c b/drivers/net/can/usb/gs_usb.c
index 3b9b2f104d86..cac4ce323a1d 100644
--- a/drivers/net/can/usb/gs_usb.c
+++ b/drivers/net/can/usb/gs_usb.c
@@ -1487,7 +1487,10 @@ static struct gs_can *gs_make_candev(unsigned int channel,
dev->can.fd.data_bittiming_const = &dev->data_bt_const;
}
- can_rx_offload_add_manual(netdev, &dev->offload, GS_NAPI_WEIGHT);
+ rc = can_rx_offload_add_manual(netdev, &dev->offload, GS_NAPI_WEIGHT);
+ if (rc)
+ goto out_free_candev;
+
SET_NETDEV_DEV(netdev, &intf->dev);
rc = register_candev(dev->netdev);
--
2.43.0
^ permalink raw reply related [flat|nested] 9+ messages in thread
* Re: [PATCH v5 1/3] can: rx-offload: make skb_irq_queue per-CPU
2026-09-07 10:49 ` [PATCH v5 1/3] can: rx-offload: make skb_irq_queue per-CPU Ciprian Costea
@ 2026-09-07 11:12 ` sashiko-bot
2026-09-07 13:32 ` Ciprian Marian Costea
0 siblings, 1 reply; 9+ messages in thread
From: sashiko-bot @ 2026-09-07 11:12 UTC (permalink / raw)
To: Ciprian Costea
Cc: Frank.Li, Vincent Mailhol, Oleksij Rempel, Oliver Hartkopp,
linux-can, Marc Kleine-Budde, imx
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [High] Scheduling while atomic BUG on PREEMPT_RT and high preemption latency due to wrapping hardware drain loop in get_cpu_ptr().
- [High] Lockless list corruption and smp_processor_id() BUG on PREEMPT_RT due to preemptible use of this_cpu_ptr() and splice in can_rx_offload_irq_finish().
--
commit c8905dbba299961e2728d05040d60499542251c0
Author: Ciprian Marian Costea <ciprianmarian.costea@oss.nxp.com>
can: rx-offload: make skb_irq_queue per-CPU
This commit makes skb_irq_queue per-CPU to prevent concurrent list
corruption when multiple IRQ lines are affined to different CPUs. It
uses get_cpu_ptr() during enqueue to disable preemption, and splices
the queues in the IRQ finish handlers.
> diff --git a/drivers/net/can/dev/rx-offload.c b/drivers/net/can/dev/rx-offload.c
> index 46e7b6db4a1ef..7616a16d90497 100644
> --- a/drivers/net/can/dev/rx-offload.c
> +++ b/drivers/net/can/dev/rx-offload.c
[ ... ]
> @@ -175,9 +176,18 @@ can_rx_offload_offload_one(struct can_rx_offload *offload, unsigned int n)
> int can_rx_offload_irq_offload_timestamp(struct can_rx_offload *offload,
> u64 pending)
> {
> + struct sk_buff_head *irq_queue;
> unsigned int i;
> int received = 0;
>
> + /*
> + * get_cpu_ptr() disables preemption so that the lockless
> + * __skb_queue_*() below operate on the current CPU's queue without
> + * racing a migration. This also keeps this_cpu_ptr() valid when a
> + * driver enqueues from a preemptible (threaded IRQ) context.
> + */
> + irq_queue = get_cpu_ptr(offload->skb_irq_queue);
> +
> for (i = offload->mb_first;
> can_rx_offload_le(offload, i, offload->mb_last);
> can_rx_offload_inc(offload, &i)) {
> @@ -190,20 +200,25 @@ int can_rx_offload_irq_offload_timestamp(struct can_rx_offload *offload,
> if (IS_ERR_OR_NULL(skb))
> continue;
>
[Severity: High]
Does wrapping this entire hardware drain loop in get_cpu_ptr() cause a
"scheduling while atomic" BUG on PREEMPT_RT?
Inside the loop in drivers/net/can/dev/rx-offload.c,
can_rx_offload_offload_one() allocates memory. On PREEMPT_RT, GFP_ATOMIC
allocations acquire a local_lock, which maps to an rt_mutex and can sleep.
Acquiring a sleeping lock while preemption is explicitly disabled by
get_cpu_ptr() triggers the BUG splat, and can also cause significant preemption
latency spikes on non-RT systems.
Could get_cpu_ptr() be pushed down into the loop to strictly guard only the
lockless __skb_queue_add_sort() operation, similar to how it was correctly
done in can_rx_offload_queue_timestamp()?
> - __skb_queue_add_sort(&offload->skb_irq_queue, skb,
> + __skb_queue_add_sort(irq_queue, skb,
> can_rx_offload_compare);
> received++;
> }
>
> + put_cpu_ptr(offload->skb_irq_queue);
> +
> return received;
> }
> EXPORT_SYMBOL_GPL(can_rx_offload_irq_offload_timestamp);
>
> int can_rx_offload_irq_offload_fifo(struct can_rx_offload *offload)
> {
> + struct sk_buff_head *irq_queue;
> struct sk_buff *skb;
> int received = 0;
>
> + irq_queue = get_cpu_ptr(offload->skb_irq_queue);
> +
> while (1) {
> skb = can_rx_offload_offload_one(offload, 0);
[Severity: High]
Does this unbounded read loop have the same "scheduling while atomic" issue
on PREEMPT_RT as in can_rx_offload_irq_offload_timestamp()?
By calling get_cpu_ptr() outside the while loop, preemption is disabled across
all memory allocations in can_rx_offload_offload_one() here as well.
[ ... ]
> @@ -307,14 +330,15 @@ EXPORT_SYMBOL_GPL(can_rx_offload_get_echo_skb_queue_tail);
>
> void can_rx_offload_irq_finish(struct can_rx_offload *offload)
> {
> + struct sk_buff_head *irq_queue = this_cpu_ptr(offload->skb_irq_queue);
[Severity: High]
Does calling this_cpu_ptr() here in a preemptible context trigger a
"using smp_processor_id() in preemptible" BUG on PREEMPT_RT?
On PREEMPT_RT, standard hardware IRQ handlers like flexcan_irq execute as
preemptible IRQ threads. Because preemption is not disabled here, calling
this_cpu_ptr() is unsafe.
> unsigned long flags;
> int queue_len;
>
> - if (skb_queue_empty_lockless(&offload->skb_irq_queue))
> + if (skb_queue_empty_lockless(irq_queue))
> return;
>
> spin_lock_irqsave(&offload->skb_queue.lock, flags);
> - skb_queue_splice_tail_init(&offload->skb_irq_queue, &offload->skb_queue);
> + skb_queue_splice_tail_init(irq_queue, &offload->skb_queue);
[Severity: High]
Can another IRQ thread preempt this list splice and corrupt the list?
Since preemption is not disabled in this function (and spin_lock_irqsave
only disables migration on RT, not preemption), a higher-priority IRQ
thread on a multi-IRQ controller (like FlexCAN on S32G2) could preempt
the lower-priority one during this skb_queue_splice_tail_init() splice.
If the preempting thread then calls can_rx_offload_irq_offload_timestamp(),
it will explicitly disable preemption and push to the exact same CPU's
irq_queue, concurrently modifying the lockless list.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20260907104942.17089-1-ciprianmarian.costea@oss.nxp.com?part=1
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: [PATCH v5 1/3] can: rx-offload: make skb_irq_queue per-CPU
2026-09-07 11:12 ` sashiko-bot
@ 2026-09-07 13:32 ` Ciprian Marian Costea
0 siblings, 0 replies; 9+ messages in thread
From: Ciprian Marian Costea @ 2026-09-07 13:32 UTC (permalink / raw)
To: sashiko-reviews
Cc: Frank.Li, Vincent Mailhol, Oleksij Rempel, Oliver Hartkopp,
linux-can, Marc Kleine-Budde, imx
On 9/7/2026 2:12 PM, sashiko-bot@kernel.org wrote:
> Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
> - [High] Scheduling while atomic BUG on PREEMPT_RT and high preemption latency due to wrapping hardware drain loop in get_cpu_ptr().
> - [High] Lockless list corruption and smp_processor_id() BUG on PREEMPT_RT due to preemptible use of this_cpu_ptr() and splice in can_rx_offload_irq_finish().
> --
>
> commit c8905dbba299961e2728d05040d60499542251c0
> Author: Ciprian Marian Costea <ciprianmarian.costea@oss.nxp.com>
>
> can: rx-offload: make skb_irq_queue per-CPU
>
> This commit makes skb_irq_queue per-CPU to prevent concurrent list
> corruption when multiple IRQ lines are affined to different CPUs. It
> uses get_cpu_ptr() during enqueue to disable preemption, and splices
> the queues in the IRQ finish handlers.
>
>> diff --git a/drivers/net/can/dev/rx-offload.c b/drivers/net/can/dev/rx-offload.c
>> index 46e7b6db4a1ef..7616a16d90497 100644
>> --- a/drivers/net/can/dev/rx-offload.c
>> +++ b/drivers/net/can/dev/rx-offload.c
> [ ... ]
>> @@ -175,9 +176,18 @@ can_rx_offload_offload_one(struct can_rx_offload *offload, unsigned int n)
>> int can_rx_offload_irq_offload_timestamp(struct can_rx_offload *offload,
>> u64 pending)
>> {
>> + struct sk_buff_head *irq_queue;
>> unsigned int i;
>> int received = 0;
>>
>> + /*
>> + * get_cpu_ptr() disables preemption so that the lockless
>> + * __skb_queue_*() below operate on the current CPU's queue without
>> + * racing a migration. This also keeps this_cpu_ptr() valid when a
>> + * driver enqueues from a preemptible (threaded IRQ) context.
>> + */
>> + irq_queue = get_cpu_ptr(offload->skb_irq_queue);
>> +
>> for (i = offload->mb_first;
>> can_rx_offload_le(offload, i, offload->mb_last);
>> can_rx_offload_inc(offload, &i)) {
>> @@ -190,20 +200,25 @@ int can_rx_offload_irq_offload_timestamp(struct can_rx_offload *offload,
>> if (IS_ERR_OR_NULL(skb))
>> continue;
>>
>
> [Severity: High]
> Does wrapping this entire hardware drain loop in get_cpu_ptr() cause a
> "scheduling while atomic" BUG on PREEMPT_RT?
>
> Inside the loop in drivers/net/can/dev/rx-offload.c,
> can_rx_offload_offload_one() allocates memory. On PREEMPT_RT, GFP_ATOMIC
> allocations acquire a local_lock, which maps to an rt_mutex and can sleep.
> Acquiring a sleeping lock while preemption is explicitly disabled by
> get_cpu_ptr() triggers the BUG splat, and can also cause significant preemption
> latency spikes on non-RT systems.
>
> Could get_cpu_ptr() be pushed down into the loop to strictly guard only the
> lockless __skb_queue_add_sort() operation, similar to how it was correctly
> done in can_rx_offload_queue_timestamp()?
>
>> - __skb_queue_add_sort(&offload->skb_irq_queue, skb,
>> + __skb_queue_add_sort(irq_queue, skb,
>> can_rx_offload_compare);
>> received++;
>> }
>>
>> + put_cpu_ptr(offload->skb_irq_queue);
>> +
>> return received;
>> }
>> EXPORT_SYMBOL_GPL(can_rx_offload_irq_offload_timestamp);
>>
>> int can_rx_offload_irq_offload_fifo(struct can_rx_offload *offload)
>> {
>> + struct sk_buff_head *irq_queue;
>> struct sk_buff *skb;
>> int received = 0;
>>
>> + irq_queue = get_cpu_ptr(offload->skb_irq_queue);
>> +
>> while (1) {
>> skb = can_rx_offload_offload_one(offload, 0);
>
> [Severity: High]
> Does this unbounded read loop have the same "scheduling while atomic" issue
> on PREEMPT_RT as in can_rx_offload_irq_offload_timestamp()?
>
> By calling get_cpu_ptr() outside the while loop, preemption is disabled across
> all memory allocations in can_rx_offload_offload_one() here as well.
>
> [ ... ]
>> @@ -307,14 +330,15 @@ EXPORT_SYMBOL_GPL(can_rx_offload_get_echo_skb_queue_tail);
>>
>> void can_rx_offload_irq_finish(struct can_rx_offload *offload)
>> {
>> + struct sk_buff_head *irq_queue = this_cpu_ptr(offload->skb_irq_queue);
>
> [Severity: High]
> Does calling this_cpu_ptr() here in a preemptible context trigger a
> "using smp_processor_id() in preemptible" BUG on PREEMPT_RT?
>
> On PREEMPT_RT, standard hardware IRQ handlers like flexcan_irq execute as
> preemptible IRQ threads. Because preemption is not disabled here, calling
> this_cpu_ptr() is unsafe.
>
>> unsigned long flags;
>> int queue_len;
>>
>> - if (skb_queue_empty_lockless(&offload->skb_irq_queue))
>> + if (skb_queue_empty_lockless(irq_queue))
>> return;
>>
>> spin_lock_irqsave(&offload->skb_queue.lock, flags);
>> - skb_queue_splice_tail_init(&offload->skb_irq_queue, &offload->skb_queue);
>> + skb_queue_splice_tail_init(irq_queue, &offload->skb_queue);
>
> [Severity: High]
> Can another IRQ thread preempt this list splice and corrupt the list?
>
> Since preemption is not disabled in this function (and spin_lock_irqsave
> only disables migration on RT, not preemption), a higher-priority IRQ
> thread on a multi-IRQ controller (like FlexCAN on S32G2) could preempt
> the lower-priority one during this skb_queue_splice_tail_init() splice.
>
> If the preempting thread then calls can_rx_offload_irq_offload_timestamp(),
> it will explicitly disable preemption and push to the exact same CPU's
> irq_queue, concurrently modifying the lockless list.
>
This review around PREEMPT_RT seems valid and I could switch the
'per_CPU' approach to a dedicated spinlock on the skb queue operations.
But I will await more review on this patchset.
Regards,
Ciprian
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: [PATCH v5 0/3] can_rx_offload keeps a lockless irq_queue that the IRQ handlers fill and that is later spliced under skb_queue.lock into the NAPI-facing skb_queue. This works as long as a single context fills the irq_queue. flexcan with FLEXCAN_QUIRK_SECONDARY_MB_IRQ and mcf5441x use two mailbox IRQ lines. When those are affined to different CPUs the two handlers can enqueue into the same list at the same time and corrupt it.
2026-09-07 10:49 [PATCH v5 0/3] can_rx_offload keeps a lockless irq_queue that the IRQ handlers fill and that is later spliced under skb_queue.lock into the NAPI-facing skb_queue. This works as long as a single context fills the irq_queue. flexcan with FLEXCAN_QUIRK_SECONDARY_MB_IRQ and mcf5441x use two mailbox IRQ lines. When those are affined to different CPUs the two handlers can enqueue into the same list at the same time and corrupt it Ciprian Costea
` (2 preceding siblings ...)
2026-09-07 10:49 ` [PATCH v5 3/3] can: gs_usb: check can_rx_offload_add_manual() return value Ciprian Costea
@ 2026-09-07 13:49 ` Marc Kleine-Budde
2026-09-07 15:07 ` Ciprian Marian Costea
3 siblings, 1 reply; 9+ messages in thread
From: Marc Kleine-Budde @ 2026-09-07 13:49 UTC (permalink / raw)
To: Ciprian Costea
Cc: Vincent Mailhol, Nicolas Ferre, Alexandre Belloni, Claudiu Beznea,
Kurt Van Dijck, linux-can, linux-arm-kernel, linux-kernel, imx,
NXP S32 Linux Team
[-- Attachment #1: Type: text/plain, Size: 1021 bytes --]
On 07.09.2026 12:49:39, Ciprian Costea wrote:
> From: Ciprian Marian Costea <ciprianmarian.costea@oss.nxp.com>
>
> This series:
> 1. Makes the irq_queue per-CPU so the handlers no longer share a list.
As sashiko pointed out, using per-CPU variables in a preemptible context
doesn't work. When proposing to use per-CPU variables I haven't thought
that far. So in hindsight this approach is not good.
What about following what NAPI does. Have a dedicated data structure per
IRQ. I think these ones are needed:
| struct sk_buff_head skb_irq_queue;
| u32 skb_queue_len_max;
|
| unsigned int mb_first;
| unsigned int mb_last;
And pass them to can_rx_offload_queue_timestamp() and
can_rx_offload_irq_finish().
regards,
Marc
--
Pengutronix e.K. | Marc Kleine-Budde |
Embedded Linux | https://www.pengutronix.de |
Vertretung Nürnberg | Phone: +49-5121-206917-129 |
Amtsgericht Hildesheim, HRA 2686 | Fax: +49-5121-206917-9 |
[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 228 bytes --]
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: [PATCH v5 0/3] can_rx_offload keeps a lockless irq_queue that the IRQ handlers fill and that is later spliced under skb_queue.lock into the NAPI-facing skb_queue. This works as long as a single context fills the irq_queue. flexcan with FLEXCAN_QUIRK_SECONDARY_MB_IRQ and mcf5441x use two mailbox IRQ lines. When those are affined to different CPUs the two handlers can enqueue into the same list at the same time and corrupt it.
2026-09-07 13:49 ` [PATCH v5 0/3] can_rx_offload keeps a lockless irq_queue that the IRQ handlers fill and that is later spliced under skb_queue.lock into the NAPI-facing skb_queue. This works as long as a single context fills the irq_queue. flexcan with FLEXCAN_QUIRK_SECONDARY_MB_IRQ and mcf5441x use two mailbox IRQ lines. When those are affined to different CPUs the two handlers can enqueue into the same list at the same time and corrupt it Marc Kleine-Budde
@ 2026-09-07 15:07 ` Ciprian Marian Costea
2026-09-08 8:58 ` Marc Kleine-Budde
0 siblings, 1 reply; 9+ messages in thread
From: Ciprian Marian Costea @ 2026-09-07 15:07 UTC (permalink / raw)
To: Marc Kleine-Budde
Cc: Vincent Mailhol, Nicolas Ferre, Alexandre Belloni, Claudiu Beznea,
Kurt Van Dijck, linux-can, linux-arm-kernel, linux-kernel, imx,
NXP S32 Linux Team
On 9/7/2026 4:49 PM, Marc Kleine-Budde wrote:
> On 07.09.2026 12:49:39, Ciprian Costea wrote:
>> From: Ciprian Marian Costea <ciprianmarian.costea@oss.nxp.com>
>>
>> This series:
>> 1. Makes the irq_queue per-CPU so the handlers no longer share a list.
>
> As sashiko pointed out, using per-CPU variables in a preemptible context
> doesn't work. When proposing to use per-CPU variables I haven't thought
> that far. So in hindsight this approach is not good.
>
> What about following what NAPI does. Have a dedicated data structure per
> IRQ. I think these ones are needed:
>
> | struct sk_buff_head skb_irq_queue;
> | u32 skb_queue_len_max;
> |
> | unsigned int mb_first;
> | unsigned int mb_last;
>
> And pass them to can_rx_offload_queue_timestamp() and
> can_rx_offload_irq_finish().
>
> regards,
> Marc
>
Hello Marc,
Thanks for replying.
Indeed the per-CPU approach should be dropped. I've also missed the
PREEMPT_RT case.
Your proposal seems better than even having a dedicated spinlock for
skb_irq_queue operations as I've originally thought.
One thing to confirm before I go ahead and implement this approach for
V6: Having the following connected patchset in mind [1] which separates
the IRQ handlers, both flexcan MB IRQs currently run flexcan_do_mb()
over the full iflag. Therefore, to actually separate the producers I'll
have each IRQ drain only its own mb_first..mb_last range into its own
queue - is this the right approach ?
[1]
https://lore.kernel.org/all/20260831143449.12828-1-ciprianmarian.costea@oss.nxp.com/T/#t
Regards,
Ciprian
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: [PATCH v5 0/3] can_rx_offload keeps a lockless irq_queue that the IRQ handlers fill and that is later spliced under skb_queue.lock into the NAPI-facing skb_queue. This works as long as a single context fills the irq_queue. flexcan with FLEXCAN_QUIRK_SECONDARY_MB_IRQ and mcf5441x use two mailbox IRQ lines. When those are affined to different CPUs the two handlers can enqueue into the same list at the same time and corrupt it.
2026-09-07 15:07 ` Ciprian Marian Costea
@ 2026-09-08 8:58 ` Marc Kleine-Budde
0 siblings, 0 replies; 9+ messages in thread
From: Marc Kleine-Budde @ 2026-09-08 8:58 UTC (permalink / raw)
To: Ciprian Marian Costea
Cc: Vincent Mailhol, Nicolas Ferre, Alexandre Belloni, Claudiu Beznea,
Kurt Van Dijck, linux-can, linux-arm-kernel, linux-kernel, imx,
NXP S32 Linux Team
[-- Attachment #1: Type: text/plain, Size: 2345 bytes --]
FYI: somehow the series description ended up in the subject
On 07.09.2026 18:07:42, Ciprian Marian Costea wrote:
> On 9/7/2026 4:49 PM, Marc Kleine-Budde wrote:
> > On 07.09.2026 12:49:39, Ciprian Costea wrote:
> > > From: Ciprian Marian Costea <ciprianmarian.costea@oss.nxp.com>
> > >
> > > This series:
> > > 1. Makes the irq_queue per-CPU so the handlers no longer share a list.
> >
> > As sashiko pointed out, using per-CPU variables in a preemptible context
> > doesn't work. When proposing to use per-CPU variables I haven't thought
> > that far. So in hindsight this approach is not good.
> >
> > What about following what NAPI does. Have a dedicated data structure per
> > IRQ. I think these ones are needed:
> >
> > | struct sk_buff_head skb_irq_queue;
> > | u32 skb_queue_len_max;
> > |
> > | unsigned int mb_first;
> > | unsigned int mb_last;
> >
> > And pass them to can_rx_offload_queue_timestamp() and
> > can_rx_offload_irq_finish().
If the RX-Offload knows the number of concurrent IRQs, you can optimize
for the single IRQ case and keep skb_queue_splice_tail_init(), while for
the other case you should sort the skbs into the offload->skb_queue
list.
> Thanks for replying.
> Indeed the per-CPU approach should be dropped. I've also missed the
> PREEMPT_RT case.
> Your proposal seems better than even having a dedicated spinlock for
> skb_irq_queue operations as I've originally thought.
>
> One thing to confirm before I go ahead and implement this approach for V6:
> Having the following connected patchset in mind [1] which separates the IRQ
> handlers, both flexcan MB IRQs currently run flexcan_do_mb() over the full
> iflag. Therefore, to actually separate the producers I'll have each IRQ
> drain only its own mb_first..mb_last range into its own queue - is this the
> right approach ?
Yes!
Please look out for shared ressources (read-modify-write registers,
etc..) when accessing the mailboxes concurrently from 2 (or more) IRQs.
After a quick look I haven't found any.
regards,
Marc
--
Pengutronix e.K. | Marc Kleine-Budde |
Embedded Linux | https://www.pengutronix.de |
Vertretung Nürnberg | Phone: +49-5121-206917-129 |
Amtsgericht Hildesheim, HRA 2686 | Fax: +49-5121-206917-9 |
[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 228 bytes --]
^ permalink raw reply [flat|nested] 9+ messages in thread
end of thread, other threads:[~2026-09-08 8:58 UTC | newest]
Thread overview: 9+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-09-07 10:49 [PATCH v5 0/3] can_rx_offload keeps a lockless irq_queue that the IRQ handlers fill and that is later spliced under skb_queue.lock into the NAPI-facing skb_queue. This works as long as a single context fills the irq_queue. flexcan with FLEXCAN_QUIRK_SECONDARY_MB_IRQ and mcf5441x use two mailbox IRQ lines. When those are affined to different CPUs the two handlers can enqueue into the same list at the same time and corrupt it Ciprian Costea
2026-09-07 10:49 ` [PATCH v5 1/3] can: rx-offload: make skb_irq_queue per-CPU Ciprian Costea
2026-09-07 11:12 ` sashiko-bot
2026-09-07 13:32 ` Ciprian Marian Costea
2026-09-07 10:49 ` [PATCH v5 2/3] can: at91_can: fix rx-offload cleanup on unbind and probe errors Ciprian Costea
2026-09-07 10:49 ` [PATCH v5 3/3] can: gs_usb: check can_rx_offload_add_manual() return value Ciprian Costea
2026-09-07 13:49 ` [PATCH v5 0/3] can_rx_offload keeps a lockless irq_queue that the IRQ handlers fill and that is later spliced under skb_queue.lock into the NAPI-facing skb_queue. This works as long as a single context fills the irq_queue. flexcan with FLEXCAN_QUIRK_SECONDARY_MB_IRQ and mcf5441x use two mailbox IRQ lines. When those are affined to different CPUs the two handlers can enqueue into the same list at the same time and corrupt it Marc Kleine-Budde
2026-09-07 15:07 ` Ciprian Marian Costea
2026-09-08 8:58 ` Marc Kleine-Budde
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox