* [PATCH can-next v10 00/14] gs_usb: fixes
@ 2026-11-22 18:48 Marc Kleine-Budde
2026-11-22 18:48 ` [PATCH can-next v10 01/14] can: gs_usb: gs_usb_probe(): init netdev completely before register_candev() Marc Kleine-Budde
` (13 more replies)
0 siblings, 14 replies; 24+ messages in thread
From: Marc Kleine-Budde @ 2026-11-22 18:48 UTC (permalink / raw)
To: Vincent Mailhol, Peter Fink, Eric Evenchick, Maximilian Schneider,
Wolfgang Grandegger
Cc: kernel, linux-can, Marc Kleine-Budde, stable, Shuangpeng Bai,
Vadim Fedorenko, Vincent Donnefort, Ciprian Marian Costea,
Sashiko
Hello,
this series contains fixes for issues found by sashiko. Also picked up the
patches by Ciprian Marian Costea and Shuangpeng Bai and integrated them
into the series.
regards,
Marc
Signed-off-by: Marc Kleine-Budde <mkl@pengutronix.de>
---
Changes in v10:
- patch #5/#6: fix single opening curly braces problem (sashiko)
- patch #8: remove net_ratelimit() (sashiko)
- patch #12: new - mask invalid parts of CAN-ID in TX-path
- patch #13/#14: apply host_frame checks only to RX'ed CAN frames, not echo
frames (sashiko)
- Link to v9: https://patch.msgid.link/20261121-gs_usb-fixes-v9-0-db121d488945@pengutronix.de
Changes in v9:
- reordered patches, so that patches to stable come first
- patch #4: new (sashiko)
- patch #5: update commit message (sashiko)
- patch #5: init struct gs_usb::cc under lock (sashiko)
- patch #5: if gs_usb_timestamp_start() fails with -ENODEV, detach devices
(sashiko)
- patch #6: gs_usb_get_timestamp() fix annotation
(sahiko)
- old patch #7: removed
- patch #8...#13: new
- Link to v8: https://patch.msgid.link/20261114-gs_usb-fixes-v8-0-f615502b005b@pengutronix.de
Changes in v8:
- patch #6: move function context annotation to patch #7
- patch #7: new: add context analysis support
- Link to v7: https://patch.msgid.link/20261114-gs_usb-fixes-v7-0-58b6341b1c08@pengutronix.de
Changes in v7:
- patch #1: update patch description (sashiko)
- patch #2: moved to linux-can
- patch #5: update function's locking annotation
- patch #5: drop active_timestamp_channels, use active_channels again
- patch #5: remove testing against IFF_UP (sashiko)
- patch #5: annotate that driver relied on RTNL for synchronization
(sashiko)
- patch #6: new: move use of new {enable,disable}_delayed_work() API here
- Link to v6: https://patch.msgid.link/20261113-gs_usb-fixes-v6-0-0c039e1bfd74@pengutronix.de
Changes in v6:
- patch #5: add missing initialization hw_ts_supported in
gs_usb_probe() (sashiko)
- patch #5: work around race window in gs_usb_timestamp_start() (sashiko)
- patch #5: start time stamping worker if the device supports HW time
stamping, if least one interface is brought up
- Link to v5: https://patch.msgid.link/20261108-gs_usb-fixes-v5-0-d52dd4940d46@pengutronix.de
Changes in v5:
- patch #1: add new patch
- patch #3: gs_usb_disconnect(): first unregister_candev() all CAN
interfaces, then call gs_destroy_candev()
- patch #4: update Fixes tag (sashiko)
- patch #4: use stats pointer directly (sashiko)
- patch #5: add new patch
- Link to v4: https://patch.msgid.link/20261026-gs_usb-fixes-v4-0-59e3b6360053@pengutronix.de
Changes in v4:
- patch #2: fix cleanup if register_candev() fails (sashiko)
- Link to v3: https://patch.msgid.link/20261026-gs_usb-fixes-v3-0-3a68f1f62813@pengutronix.de
Changes in v3:
- patch #2: fix cleanup if register_candev() fails (sashiko)
- Link to v2: https://patch.msgid.link/20261025-gs_usb-fixes-v2-0-535d45097f72@pengutronix.de
Changes in v2:
- patch #2: add missing cleanup if register_candev() fails (sashiko)
- patch #3: do not increase rx_errors during OOM (sashiko)
- Link to v1: https://patch.msgid.link/20261025-gs_usb-fixes-v1-0-f74ebbd4da36@pengutronix.de
---
Ciprian Marian Costea (1):
can: gs_usb: gs_make_candev(): check can_rx_offload_add_manual() return value
Marc Kleine-Budde (12):
can: gs_usb: gs_usb_probe(): init netdev completely before register_candev()
can: gs_usb: gs_usb_receive_bulk_callback(): check for overflow flag if SKB allocation fails
can: gs_usb: gs_usb_receive_bulk_callback(): add missing call to can_rx_offload_irq_finish
can: gs_usb: use spin_lock_irqsave() instead of spin_lock_bh() to protect struct gs_usb::tc
can: gs_usb: add support for context analysis
can: gs_usb: gs_usb_receive_bulk_callback(): use dev_info() to print re-submit info messages
can: gs_usb: gs_usb_get_gs_can(): introduce new function to consolidate validation of struct gs_can
can: gs_usb: gs_usb_get_gs_can(): report channel number out of range error
can: gs_usb: gs_usb_get_rx_data_length(): new function to consolidate validation of host frame
can: gs_usb: gs_can_start_xmit(): mask invalid parts of CAN ID before sending
can: gs_usb: gs_usb_host_frame_header_valid(): drop invalid CAN error frames
can: gs_usb: gs_usb_host_frame_header_valid(): drop CAN frames with invalid DLC
Shuangpeng Bai (1):
can: gs_usb: fix hardware timestamp state for mixed channels
drivers/net/can/usb/Makefile | 2 +
drivers/net/can/usb/gs_usb.c | 407 ++++++++++++++++++++++++++++++++-----------
2 files changed, 310 insertions(+), 99 deletions(-)
---
base-commit: eb6d6c80d35449c27471b1874f13066c09a8964d
base-tree: git://git.kernel.org/pub/scm/linux/kernel/git/netdev/net-next.git main
change-id: 20261025-gs_usb-fixes-250f38833107
Best regards,
--
Marc Kleine-Budde <mkl@pengutronix.de>
^ permalink raw reply [flat|nested] 24+ messages in thread
* [PATCH can-next v10 01/14] can: gs_usb: gs_usb_probe(): init netdev completely before register_candev()
2026-11-22 18:48 [PATCH can-next v10 00/14] gs_usb: fixes Marc Kleine-Budde
@ 2026-11-22 18:48 ` Marc Kleine-Budde
2026-11-22 18:48 ` [PATCH can-next v10 02/14] can: gs_usb: gs_usb_receive_bulk_callback(): check for overflow flag if SKB allocation fails Marc Kleine-Budde
` (12 subsequent siblings)
13 siblings, 0 replies; 24+ messages in thread
From: Marc Kleine-Budde @ 2026-11-22 18:48 UTC (permalink / raw)
To: Vincent Mailhol, Peter Fink, Eric Evenchick, Maximilian Schneider,
Wolfgang Grandegger
Cc: kernel, linux-can, Marc Kleine-Budde, stable
In gs_usb_probe(), gs_make_candev() is called to allocate and set up the
CAN channel, as well as to call register_candev(). However, gs_usb_probe()
defers the assignment of dev->parent and parent->hf_size_rx until after
gs_make_candev() returns. This opens a race window for the user space to
bring up the CAN interface, while it's not completely setup.
To close the race window, initialize all CAN interfaces in a first loop and
then register them in a second loop: Don't call register_candev() from
gs_make_candev() but explicitly from gs_usb_probe(). Adjust
gs_destroy_candev() likewise, move unregister_candev() to
gs_usb_disconnect() and the error paths in gs_usb_probe(), to keep it
inverse to symmetric to gs_make_candev().
Closes: https://sashiko.dev/#/bug/linux-be54de7d-0b75-408c-b3ee-dd90c9756857
Closes: https://sashiko.dev/#/bug/linux-71076d59-0843-4749-a5ab-fdd98a08582f
Fixes: d08e973a77d1 ("can: gs_usb: Added support for the GS_USB CAN devices")
Cc: stable@vger.kernel.org
Signed-off-by: Marc Kleine-Budde <mkl@pengutronix.de>
---
drivers/net/can/usb/gs_usb.c | 64 +++++++++++++++++++++++++++-----------------
1 file changed, 40 insertions(+), 24 deletions(-)
diff --git a/drivers/net/can/usb/gs_usb.c b/drivers/net/can/usb/gs_usb.c
index 3b9b2f104d86..d1124c49460e 100644
--- a/drivers/net/can/usb/gs_usb.c
+++ b/drivers/net/can/usb/gs_usb.c
@@ -1490,18 +1490,8 @@ static struct gs_can *gs_make_candev(unsigned int channel,
can_rx_offload_add_manual(netdev, &dev->offload, GS_NAPI_WEIGHT);
SET_NETDEV_DEV(netdev, &intf->dev);
- rc = register_candev(dev->netdev);
- if (rc) {
- dev_err(&intf->dev,
- "Couldn't register candev for channel %d (%pe)\n",
- channel, ERR_PTR(rc));
- goto out_can_rx_offload_del;
- }
-
return dev;
-out_can_rx_offload_del:
- can_rx_offload_del(&dev->offload);
out_free_candev:
free_candev(dev->netdev);
return ERR_PTR(rc);
@@ -1509,7 +1499,6 @@ static struct gs_can *gs_make_candev(unsigned int channel,
static void gs_destroy_candev(struct gs_can *dev)
{
- unregister_candev(dev->netdev);
can_rx_offload_del(&dev->offload);
free_candev(dev->netdev);
}
@@ -1587,21 +1576,16 @@ static int gs_usb_probe(struct usb_interface *intf,
for (i = 0; i < icount; i++) {
unsigned int hf_size_rx = 0;
+ struct gs_can *dev;
- parent->canch[i] = gs_make_candev(i, intf, &dconf);
- if (IS_ERR_OR_NULL(parent->canch[i])) {
+ dev = gs_make_candev(i, intf, &dconf);
+ if (IS_ERR_OR_NULL(dev)) {
/* save error code to return later */
- rc = PTR_ERR(parent->canch[i]);
+ rc = PTR_ERR(dev);
- /* on failure destroy previously created candevs */
- icount = i;
- for (i = 0; i < icount; i++)
- gs_destroy_candev(parent->canch[i]);
-
- usb_kill_anchored_urbs(&parent->rx_submitted);
- kfree(parent);
- return rc;
+ goto out_gs_destroy_candev;
}
+ parent->canch[i] = dev;
parent->canch[i]->parent = parent;
/* set RX packet size based on FD and if hardware
@@ -1621,7 +1605,37 @@ static int gs_usb_probe(struct usb_interface *intf,
parent->hf_size_rx = max(parent->hf_size_rx, hf_size_rx);
}
+ for (i = 0; i < icount; i++) {
+ struct gs_can *dev = parent->canch[i];
+
+ rc = register_candev(dev->netdev);
+ if (rc) {
+ const unsigned int cur = i;
+
+ dev_err(&intf->dev,
+ "Couldn't register candev for channel %d (%pe)\n",
+ i, ERR_PTR(rc));
+
+ for (i = 0; i < cur; i++)
+ unregister_candev(parent->canch[i]->netdev);
+
+ goto out_gs_destroy_candev;
+ }
+ }
+
return 0;
+
+out_gs_destroy_candev:
+ for (i = 0; i < parent->channel_cnt; i++) {
+ if (!parent->canch[i])
+ continue;
+
+ gs_destroy_candev(parent->canch[i]);
+ }
+
+ kfree(parent);
+
+ return rc;
}
static void gs_usb_disconnect(struct usb_interface *intf)
@@ -1637,8 +1651,10 @@ static void gs_usb_disconnect(struct usb_interface *intf)
}
for (i = 0; i < parent->channel_cnt; i++)
- if (parent->canch[i])
- gs_destroy_candev(parent->canch[i]);
+ unregister_candev(parent->canch[i]->netdev);
+
+ for (i = 0; i < parent->channel_cnt; i++)
+ gs_destroy_candev(parent->canch[i]);
kfree(parent);
}
--
2.53.0
^ permalink raw reply related [flat|nested] 24+ messages in thread
* [PATCH can-next v10 02/14] can: gs_usb: gs_usb_receive_bulk_callback(): check for overflow flag if SKB allocation fails
2026-11-22 18:48 [PATCH can-next v10 00/14] gs_usb: fixes Marc Kleine-Budde
2026-11-22 18:48 ` [PATCH can-next v10 01/14] can: gs_usb: gs_usb_probe(): init netdev completely before register_candev() Marc Kleine-Budde
@ 2026-11-22 18:48 ` Marc Kleine-Budde
2026-11-24 9:30 ` netdev-bot+sashiko
2026-11-22 18:48 ` [PATCH can-next v10 03/14] can: gs_usb: gs_usb_receive_bulk_callback(): add missing call to can_rx_offload_irq_finish Marc Kleine-Budde
` (11 subsequent siblings)
13 siblings, 1 reply; 24+ messages in thread
From: Marc Kleine-Budde @ 2026-11-22 18:48 UTC (permalink / raw)
To: Vincent Mailhol, Peter Fink, Eric Evenchick, Maximilian Schneider,
Wolfgang Grandegger
Cc: kernel, linux-can, Marc Kleine-Budde, stable
If the allocation of an SKB fails in gs_usb_receive_bulk_callback(), the
function neither updates the statistics nor checks whether an overflow flag
is set in the host frame.
Instead of directly resubmitting the URB, first update the dropped RX
statistics then check if an overflow flag is set.
Closes: https://sashiko.dev/#/bug/linux-7b3fe6a0-4628-4d0e-aec0-69f4bf8ad5d4
Fixes: d08e973a77d1 ("can: gs_usb: Added support for the GS_USB CAN devices")
Fixes: 26949ac935e3 ("can: gs_usb: add CAN-FD support")
Cc: stable@vger.kernel.org
Signed-off-by: Marc Kleine-Budde <mkl@pengutronix.de>
---
drivers/net/can/usb/gs_usb.c | 15 +++++++++++----
1 file changed, 11 insertions(+), 4 deletions(-)
diff --git a/drivers/net/can/usb/gs_usb.c b/drivers/net/can/usb/gs_usb.c
index d1124c49460e..9e8567544c88 100644
--- a/drivers/net/can/usb/gs_usb.c
+++ b/drivers/net/can/usb/gs_usb.c
@@ -673,8 +673,11 @@ static void gs_usb_receive_bulk_callback(struct urb *urb)
if (hf->echo_id == GS_HOST_FRAME_ECHO_ID_RX) { /* normal rx */
if (hf->flags & GS_CAN_FLAG_FD) {
skb = alloc_canfd_skb(netdev, &cfd);
- if (!skb)
- goto resubmit_urb;
+ if (!skb) {
+ stats->rx_dropped++;
+
+ goto check_overflow;
+ }
cfd->can_id = le32_to_cpu(hf->can_id);
cfd->len = data_length;
@@ -686,8 +689,11 @@ static void gs_usb_receive_bulk_callback(struct urb *urb)
memcpy(cfd->data, hf->canfd->data, data_length);
} else {
skb = alloc_can_skb(netdev, &cf);
- if (!skb)
- goto resubmit_urb;
+ if (!skb) {
+ stats->rx_dropped++;
+
+ goto check_overflow;
+ }
cf->can_id = le32_to_cpu(hf->can_id);
can_frame_set_cc_len(cf, hf->can_dlc, dev->can.ctrlmode);
@@ -728,6 +734,7 @@ static void gs_usb_receive_bulk_callback(struct urb *urb)
netif_wake_queue(netdev);
}
+check_overflow:
if (hf->flags & GS_CAN_FLAG_OVERFLOW) {
stats->rx_over_errors++;
stats->rx_errors++;
--
2.53.0
^ permalink raw reply related [flat|nested] 24+ messages in thread
* [PATCH can-next v10 03/14] can: gs_usb: gs_usb_receive_bulk_callback(): add missing call to can_rx_offload_irq_finish
2026-11-22 18:48 [PATCH can-next v10 00/14] gs_usb: fixes Marc Kleine-Budde
2026-11-22 18:48 ` [PATCH can-next v10 01/14] can: gs_usb: gs_usb_probe(): init netdev completely before register_candev() Marc Kleine-Budde
2026-11-22 18:48 ` [PATCH can-next v10 02/14] can: gs_usb: gs_usb_receive_bulk_callback(): check for overflow flag if SKB allocation fails Marc Kleine-Budde
@ 2026-11-22 18:48 ` Marc Kleine-Budde
2026-11-22 18:48 ` [PATCH can-next v10 04/14] can: gs_usb: use spin_lock_irqsave() instead of spin_lock_bh() to protect struct gs_usb::tc Marc Kleine-Budde
` (10 subsequent siblings)
13 siblings, 0 replies; 24+ messages in thread
From: Marc Kleine-Budde @ 2026-11-22 18:48 UTC (permalink / raw)
To: Vincent Mailhol, Peter Fink, Eric Evenchick, Maximilian Schneider,
Wolfgang Grandegger
Cc: kernel, linux-can, Marc Kleine-Budde, stable
If the overflow flag is set for a host frame and the allocation of the
error SKB fails, the URB should not be resubmitted immediately.
Instead, call can_rx_offload_irq_finish(), since an SKB may have been added
to rx-offload.
Closes: https://sashiko.dev/#/bug/linux-441137e2-cd75-411f-9ff5-dc4d473c5ddb
Fixes: 24bc41b45583 ("can: gs_usb: convert to NAPI/rx-offload to avoid OoO reception")
Cc: stable@vger.kernel.org
Signed-off-by: Marc Kleine-Budde <mkl@pengutronix.de>
---
drivers/net/can/usb/gs_usb.c | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)
diff --git a/drivers/net/can/usb/gs_usb.c b/drivers/net/can/usb/gs_usb.c
index 9e8567544c88..c9306e715552 100644
--- a/drivers/net/can/usb/gs_usb.c
+++ b/drivers/net/can/usb/gs_usb.c
@@ -741,7 +741,7 @@ static void gs_usb_receive_bulk_callback(struct urb *urb)
skb = alloc_can_err_skb(netdev, &cf);
if (!skb)
- goto resubmit_urb;
+ goto can_rx_offload_irq_finish;
cf->can_id |= CAN_ERR_CRTL;
cf->len = CAN_ERR_DLC;
@@ -750,6 +750,7 @@ static void gs_usb_receive_bulk_callback(struct urb *urb)
gs_usb_rx_offload(dev, skb, hf);
}
+can_rx_offload_irq_finish:
can_rx_offload_irq_finish(&dev->offload);
resubmit_urb:
--
2.53.0
^ permalink raw reply related [flat|nested] 24+ messages in thread
* [PATCH can-next v10 04/14] can: gs_usb: use spin_lock_irqsave() instead of spin_lock_bh() to protect struct gs_usb::tc
2026-11-22 18:48 [PATCH can-next v10 00/14] gs_usb: fixes Marc Kleine-Budde
` (2 preceding siblings ...)
2026-11-22 18:48 ` [PATCH can-next v10 03/14] can: gs_usb: gs_usb_receive_bulk_callback(): add missing call to can_rx_offload_irq_finish Marc Kleine-Budde
@ 2026-11-22 18:48 ` Marc Kleine-Budde
2026-11-24 9:30 ` netdev-bot+sashiko
2026-11-22 18:48 ` [PATCH can-next v10 05/14] can: gs_usb: fix hardware timestamp state for mixed channels Marc Kleine-Budde
` (9 subsequent siblings)
13 siblings, 1 reply; 24+ messages in thread
From: Marc Kleine-Budde @ 2026-11-22 18:48 UTC (permalink / raw)
To: Vincent Mailhol, Peter Fink, Eric Evenchick, Maximilian Schneider,
Wolfgang Grandegger
Cc: kernel, linux-can, Marc Kleine-Budde, stable
gs_usb_skb_set_timestamp() is called from gs_usb_receive_bulk_callback(),
the URB completion handle. usb_hcd_giveback_urb() calls the URB completion
handler directly when the HCD does not set HCD_BH.
Replace locking the bottom half only with a proper IRQ save spinlock.
This issue has not been observed on read hardware, but found by LLM review.
Closes: https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261114-gs_usb-fixes-v8-0-f615502b005b%40pengutronix.de?part=7
Fixes: 29a8c9ec9090 ("can: gs_usb: add missing lock to protect struct timecounter::cycle_last")
Cc: stable@vger.kernel.org
Signed-off-by: Marc Kleine-Budde <mkl@pengutronix.de>
---
drivers/net/can/usb/gs_usb.c | 18 ++++++++++--------
1 file changed, 10 insertions(+), 8 deletions(-)
diff --git a/drivers/net/can/usb/gs_usb.c b/drivers/net/can/usb/gs_usb.c
index c9306e715552..470dd52d97df 100644
--- a/drivers/net/can/usb/gs_usb.c
+++ b/drivers/net/can/usb/gs_usb.c
@@ -333,6 +333,7 @@ struct gs_usb {
struct cyclecounter cc;
struct timecounter tc;
spinlock_t tc_lock; /* spinlock to guard access tc->cycle_last */
+ unsigned long tc_lock_flags;
struct delayed_work timestamp;
unsigned int hf_size_rx;
@@ -432,9 +433,9 @@ static u64 gs_usb_timestamp_read(struct cyclecounter *cc) __must_hold(&dev->tc_l
lockdep_assert_held(&parent->tc_lock);
/* drop lock for synchronous USB transfer */
- spin_unlock_bh(&parent->tc_lock);
+ spin_unlock_irqrestore(&parent->tc_lock, parent->tc_lock_flags);
err = gs_usb_get_timestamp(parent, ×tamp);
- spin_lock_bh(&parent->tc_lock);
+ spin_lock_irqsave(&parent->tc_lock, parent->tc_lock_flags);
if (err)
dev_err(&parent->udev->dev,
"Error %d while reading timestamp. HW timestamps may be inaccurate.",
@@ -449,9 +450,9 @@ static void gs_usb_timestamp_work(struct work_struct *work)
struct gs_usb *parent;
parent = container_of(delayed_work, struct gs_usb, timestamp);
- spin_lock_bh(&parent->tc_lock);
+ spin_lock_irqsave(&parent->tc_lock, parent->tc_lock_flags);
timecounter_read(&parent->tc);
- spin_unlock_bh(&parent->tc_lock);
+ spin_unlock_irqrestore(&parent->tc_lock, parent->tc_lock_flags);
schedule_delayed_work(&parent->timestamp,
GS_USB_TIMESTAMP_WORK_DELAY_SEC * HZ);
@@ -462,11 +463,12 @@ static void gs_usb_skb_set_timestamp(struct gs_can *dev,
{
struct skb_shared_hwtstamps *hwtstamps = skb_hwtstamps(skb);
struct gs_usb *parent = dev->parent;
+ unsigned long flags;
u64 ns;
- spin_lock_bh(&parent->tc_lock);
+ spin_lock_irqsave(&parent->tc_lock, flags);
ns = timecounter_cyc2time(&parent->tc, timestamp);
- spin_unlock_bh(&parent->tc_lock);
+ spin_unlock_irqrestore(&parent->tc_lock, flags);
hwtstamps->hwtstamp = ns_to_ktime(ns);
}
@@ -481,9 +483,9 @@ static void gs_usb_timestamp_init(struct gs_usb *parent)
cc->mult = clocksource_hz2mult(GS_USB_TIMESTAMP_TIMER_HZ, cc->shift);
spin_lock_init(&parent->tc_lock);
- spin_lock_bh(&parent->tc_lock);
+ spin_lock_irqsave(&parent->tc_lock, parent->tc_lock_flags);
timecounter_init(&parent->tc, &parent->cc, ktime_get_real_ns());
- spin_unlock_bh(&parent->tc_lock);
+ spin_unlock_irqrestore(&parent->tc_lock, parent->tc_lock_flags);
INIT_DELAYED_WORK(&parent->timestamp, gs_usb_timestamp_work);
schedule_delayed_work(&parent->timestamp,
--
2.53.0
^ permalink raw reply related [flat|nested] 24+ messages in thread
* [PATCH can-next v10 05/14] can: gs_usb: fix hardware timestamp state for mixed channels
2026-11-22 18:48 [PATCH can-next v10 00/14] gs_usb: fixes Marc Kleine-Budde
` (3 preceding siblings ...)
2026-11-22 18:48 ` [PATCH can-next v10 04/14] can: gs_usb: use spin_lock_irqsave() instead of spin_lock_bh() to protect struct gs_usb::tc Marc Kleine-Budde
@ 2026-11-22 18:48 ` Marc Kleine-Budde
2026-11-24 9:30 ` netdev-bot+sashiko
2026-11-22 18:48 ` [PATCH can-next v10 06/14] can: gs_usb: add support for context analysis Marc Kleine-Budde
` (8 subsequent siblings)
13 siblings, 1 reply; 24+ messages in thread
From: Marc Kleine-Budde @ 2026-11-22 18:48 UTC (permalink / raw)
To: Vincent Mailhol, Peter Fink, Eric Evenchick, Maximilian Schneider,
Wolfgang Grandegger
Cc: kernel, linux-can, Marc Kleine-Budde, Shuangpeng Bai, stable,
Vadim Fedorenko
From: Shuangpeng Bai <shuangpeng.kernel@gmail.com>
For hardware timestamping, the driver manages the infrastructure
(cyclecounter/timecounter) for the entire USB interface, for now assuming
that all channels share a common time base, even though each channel
individually indicates support for hardware timestamping via the
GS_CAN_FEATURE_HW_TIMESTAMP flag.
Currently both the initialization and shutdown of the HW timestamping
infrastructure is tied to the flags of the first channel opened
respectively last channel closed. This leads to problems if the first
channel opened doesn't have the timestamping feature, but the second
channel has. In this case the timestamping is never properly initialized.
Keep the initialization of the HW timestamping infrastructure in
gs_usb_timestamp_init(), and move initial time init and the initial
scheduling of the delayed worker into gs_usb_timestamp_start().
It's worth noting that if reading of the initial time stamp fails the
interface cannot be opened.
Initialize the HW timestamping infrastructure in gs_usb_probe() using
gs_usb_timestamp_init() if at least one channel has the
GS_CAN_FEATURE_HW_TIMESTAMP flag set. Initialize struct gs_usb::cc while
holding the tc_lock.
Start and sync the timestamping infrastructure when opening the first
channel of a device that supports HW timestamps and stop it if the last
channel closes. Avoid the race window during timecounter_init()
(gs_usb_timestamp_read() drops the struct gs_usb::tc_lock) by reading the
initial time stamp prior taking the lock.
To simplify backporting, support for context analysis is added in a later
patch.
Reported-by: Shuangpeng Bai <shuangpeng.kernel@gmail.com>
Closes: https://sashiko.dev/#/bug/linux-b39360f9-6cd8-4f47-83bb-f0f78331f308
Closes: https://sashiko.dev/#/bug/linux-5f8ecd4e-344e-4ceb-9b46-886f4942a5f6
Fixes: 5886e4d5ecec ("can: gs_usb: fix time stamp counter initialization")
Cc: stable@vger.kernel.org
Cc: Shuangpeng Bai <shuangpeng.kernel@gmail.com>
Cc: Vadim Fedorenko <vadim.fedorenko@linux.dev>
Signed-off-by: Shuangpeng Bai <shuangpeng.kernel@gmail.com>
Co-developed-by: Marc Kleine-Budde <mkl@pengutronix.de>
Signed-off-by: Marc Kleine-Budde <mkl@pengutronix.de>
---
drivers/net/can/usb/gs_usb.c | 88 +++++++++++++++++++++++++++++++++++++-------
1 file changed, 74 insertions(+), 14 deletions(-)
diff --git a/drivers/net/can/usb/gs_usb.c b/drivers/net/can/usb/gs_usb.c
index 470dd52d97df..e1a10e39313d 100644
--- a/drivers/net/can/usb/gs_usb.c
+++ b/drivers/net/can/usb/gs_usb.c
@@ -324,6 +324,10 @@ struct gs_can {
atomic_t active_tx_urbs;
};
+enum gs_usb_parent_flags {
+ GS_USB_PARENT_FLAGS_HW_TIMESTAMP = BIT(0),
+};
+
/* usb interface struct */
struct gs_usb {
struct usb_anchor rx_submitted;
@@ -332,12 +336,14 @@ struct gs_usb {
/* time counter for hardware timestamps */
struct cyclecounter cc;
struct timecounter tc;
- spinlock_t tc_lock; /* spinlock to guard access tc->cycle_last */
+ u32 timestamp_initial;
+ spinlock_t tc_lock; /* spinlock to guard access cc, tc, timestamp_initial */
unsigned long tc_lock_flags;
struct delayed_work timestamp;
unsigned int hf_size_rx;
u8 active_channels;
+ u8 flags;
u8 channel_cnt;
unsigned int pipe_in;
@@ -424,6 +430,13 @@ static inline int gs_usb_get_timestamp(const struct gs_usb *parent,
return 0;
}
+static u64 gs_usb_timestamp_read_initial(struct cyclecounter *cc)
+{
+ const struct gs_usb *parent = container_of(cc, struct gs_usb, cc);
+
+ return parent->timestamp_initial;
+}
+
static u64 gs_usb_timestamp_read(struct cyclecounter *cc) __must_hold(&dev->tc_lock)
{
struct gs_usb *parent = container_of(cc, struct gs_usb, cc);
@@ -477,19 +490,46 @@ static void gs_usb_timestamp_init(struct gs_usb *parent)
{
struct cyclecounter *cc = &parent->cc;
- cc->read = gs_usb_timestamp_read;
- cc->mask = CYCLECOUNTER_MASK(32);
- cc->shift = 32 - bits_per(NSEC_PER_SEC / GS_USB_TIMESTAMP_TIMER_HZ);
- cc->mult = clocksource_hz2mult(GS_USB_TIMESTAMP_TIMER_HZ, cc->shift);
-
spin_lock_init(&parent->tc_lock);
- spin_lock_irqsave(&parent->tc_lock, parent->tc_lock_flags);
- timecounter_init(&parent->tc, &parent->cc, ktime_get_real_ns());
- spin_unlock_irqrestore(&parent->tc_lock, parent->tc_lock_flags);
+
+ scoped_guard(spinlock_irqsave, &parent->tc_lock) {
+ cc->mask = CYCLECOUNTER_MASK(32);
+ cc->shift = 32 - bits_per(NSEC_PER_SEC / GS_USB_TIMESTAMP_TIMER_HZ);
+ cc->mult = clocksource_hz2mult(GS_USB_TIMESTAMP_TIMER_HZ, cc->shift);
+ }
INIT_DELAYED_WORK(&parent->timestamp, gs_usb_timestamp_work);
+
+ parent->flags |= GS_USB_PARENT_FLAGS_HW_TIMESTAMP;
+}
+
+static int gs_usb_timestamp_start(struct gs_usb *parent)
+{
+ u32 timestamp = 0;
+ int rc;
+
+ /* read current timestamp, will be used for initial time stamp */
+ rc = gs_usb_get_timestamp(parent, ×tamp);
+ if (rc)
+ return rc;
+
+ scoped_guard(spinlock_irqsave, &parent->tc_lock) {
+ struct cyclecounter *cc = &parent->cc;
+
+ /* gs_usb_timestamp_read() will drop the struct gs_usb::tc_lock. Avoid the race
+ * window by using gs_usb_timestamp_read_initial() to read the initial timestamp in
+ * timecounter_init(), switch to gs_usb_timestamp_read() afterwards.
+ */
+ parent->timestamp_initial = timestamp;
+ cc->read = gs_usb_timestamp_read_initial;
+ timecounter_init(&parent->tc, &parent->cc, ktime_get_real_ns());
+ cc->read = gs_usb_timestamp_read;
+ }
+
schedule_delayed_work(&parent->timestamp,
GS_USB_TIMESTAMP_WORK_DELAY_SEC * HZ);
+
+ return 0;
}
static void gs_usb_timestamp_stop(struct gs_usb *parent)
@@ -990,9 +1030,19 @@ static int gs_can_open(struct net_device *netdev)
can_rx_offload_enable(&dev->offload);
+ /* rely on RTNL to serialize concurrent ifup/ifdown */
+ ASSERT_RTNL();
+
if (!parent->active_channels) {
- if (dev->feature & GS_CAN_FEATURE_HW_TIMESTAMP)
- gs_usb_timestamp_init(parent);
+ if (parent->flags & GS_USB_PARENT_FLAGS_HW_TIMESTAMP) {
+ rc = gs_usb_timestamp_start(parent);
+ if (rc) {
+ if (rc == -ENODEV)
+ netif_device_detach(netdev);
+
+ goto out_can_rx_offload_disable;
+ }
+ }
for (i = 0; i < GS_MAX_RX_URBS; i++) {
u8 *buf;
@@ -1107,10 +1157,10 @@ static int gs_can_open(struct net_device *netdev)
if (!parent->active_channels) {
usb_kill_anchored_urbs(&parent->rx_submitted);
- if (dev->feature & GS_CAN_FEATURE_HW_TIMESTAMP)
+ if (parent->flags & GS_USB_PARENT_FLAGS_HW_TIMESTAMP)
gs_usb_timestamp_stop(parent);
}
-
+out_can_rx_offload_disable:
can_rx_offload_disable(&dev->offload);
close_candev(netdev);
@@ -1160,12 +1210,15 @@ static int gs_can_close(struct net_device *netdev)
netif_stop_queue(netdev);
+ /* rely on RTNL to serialize concurrent ifup/ifdown */
+ ASSERT_RTNL();
+
/* Stop polling */
parent->active_channels--;
if (!parent->active_channels) {
usb_kill_anchored_urbs(&parent->rx_submitted);
- if (dev->feature & GS_CAN_FEATURE_HW_TIMESTAMP)
+ if (parent->flags & GS_USB_PARENT_FLAGS_HW_TIMESTAMP)
gs_usb_timestamp_stop(parent);
}
@@ -1524,6 +1577,7 @@ static int gs_usb_probe(struct usb_interface *intf,
.byte_order = cpu_to_le32(0x0000beef),
};
struct gs_device_config dconf;
+ bool hw_ts_supported = false;
unsigned int icount, i;
int rc;
@@ -1598,6 +1652,9 @@ static int gs_usb_probe(struct usb_interface *intf,
parent->canch[i] = dev;
parent->canch[i]->parent = parent;
+ if (parent->canch[i]->feature & GS_CAN_FEATURE_HW_TIMESTAMP)
+ hw_ts_supported = true;
+
/* set RX packet size based on FD and if hardware
* timestamps are supported.
*/
@@ -1615,6 +1672,9 @@ static int gs_usb_probe(struct usb_interface *intf,
parent->hf_size_rx = max(parent->hf_size_rx, hf_size_rx);
}
+ if (hw_ts_supported)
+ gs_usb_timestamp_init(parent);
+
for (i = 0; i < icount; i++) {
struct gs_can *dev = parent->canch[i];
--
2.53.0
^ permalink raw reply related [flat|nested] 24+ messages in thread
* [PATCH can-next v10 06/14] can: gs_usb: add support for context analysis
2026-11-22 18:48 [PATCH can-next v10 00/14] gs_usb: fixes Marc Kleine-Budde
` (4 preceding siblings ...)
2026-11-22 18:48 ` [PATCH can-next v10 05/14] can: gs_usb: fix hardware timestamp state for mixed channels Marc Kleine-Budde
@ 2026-11-22 18:48 ` Marc Kleine-Budde
2026-11-22 18:48 ` [PATCH can-next v10 07/14] can: gs_usb: gs_make_candev(): check can_rx_offload_add_manual() return value Marc Kleine-Budde
` (7 subsequent siblings)
13 siblings, 0 replies; 24+ messages in thread
From: Marc Kleine-Budde @ 2026-11-22 18:48 UTC (permalink / raw)
To: Vincent Mailhol, Peter Fink, Eric Evenchick, Maximilian Schneider,
Wolfgang Grandegger
Cc: kernel, linux-can, Marc Kleine-Budde, Vincent Donnefort
Enable context analysis for the gs_usb driver.
Tag the hardware timestamp counter infrastructure in struct gs_usb as
guarded by the tc_lock. Fix the existing annotation for
gs_usb_timestamp_read(), add context analysis annotation regarding the
tc_lock to the low level timestamp reading functions.
This has been tested on Debian testing with clang-23 and clang-24.
| Debian clang version 23.1.5 (2)
| Debian clang version 24.0.0 (++20261025103442+7e3c57157096-1~exp1)
Cc: Vincent Donnefort <vdonnefort@google.com>
Signed-off-by: Marc Kleine-Budde <mkl@pengutronix.de>
---
drivers/net/can/usb/Makefile | 2 ++
drivers/net/can/usb/gs_usb.c | 11 +++++++----
2 files changed, 9 insertions(+), 4 deletions(-)
diff --git a/drivers/net/can/usb/Makefile b/drivers/net/can/usb/Makefile
index fcafb1ac262e..b2e1539f1d52 100644
--- a/drivers/net/can/usb/Makefile
+++ b/drivers/net/can/usb/Makefile
@@ -3,6 +3,8 @@
# Makefile for the Linux Controller Area Network USB drivers.
#
+CONTEXT_ANALYSIS_gs_usb.o := y
+
obj-$(CONFIG_CAN_8DEV_USB) += usb_8dev.o
obj-$(CONFIG_CAN_EMS_USB) += ems_usb.o
obj-$(CONFIG_CAN_ESD_USB) += esd_usb.o
diff --git a/drivers/net/can/usb/gs_usb.c b/drivers/net/can/usb/gs_usb.c
index e1a10e39313d..ab82dc07f3f5 100644
--- a/drivers/net/can/usb/gs_usb.c
+++ b/drivers/net/can/usb/gs_usb.c
@@ -334,9 +334,9 @@ struct gs_usb {
struct usb_device *udev;
/* time counter for hardware timestamps */
- struct cyclecounter cc;
- struct timecounter tc;
- u32 timestamp_initial;
+ struct cyclecounter cc __guarded_by(&tc_lock);
+ struct timecounter tc __guarded_by(&tc_lock);
+ u32 timestamp_initial __guarded_by(&tc_lock);
spinlock_t tc_lock; /* spinlock to guard access cc, tc, timestamp_initial */
unsigned long tc_lock_flags;
struct delayed_work timestamp;
@@ -412,6 +412,7 @@ static int gs_cmd_reset(struct gs_can *dev)
static inline int gs_usb_get_timestamp(const struct gs_usb *parent,
u32 *timestamp_p)
+ __must_not_hold(&parent->tc_lock)
{
__le32 timestamp;
int rc;
@@ -431,13 +432,15 @@ static inline int gs_usb_get_timestamp(const struct gs_usb *parent,
}
static u64 gs_usb_timestamp_read_initial(struct cyclecounter *cc)
+ __must_hold(&container_of(cc, struct gs_usb, cc)->tc_lock)
{
const struct gs_usb *parent = container_of(cc, struct gs_usb, cc);
return parent->timestamp_initial;
}
-static u64 gs_usb_timestamp_read(struct cyclecounter *cc) __must_hold(&dev->tc_lock)
+static u64 gs_usb_timestamp_read(struct cyclecounter *cc)
+ __must_hold(&container_of(cc, struct gs_usb, cc)->tc_lock)
{
struct gs_usb *parent = container_of(cc, struct gs_usb, cc);
u32 timestamp = 0;
--
2.53.0
^ permalink raw reply related [flat|nested] 24+ messages in thread
* [PATCH can-next v10 07/14] can: gs_usb: gs_make_candev(): check can_rx_offload_add_manual() return value
2026-11-22 18:48 [PATCH can-next v10 00/14] gs_usb: fixes Marc Kleine-Budde
` (5 preceding siblings ...)
2026-11-22 18:48 ` [PATCH can-next v10 06/14] can: gs_usb: add support for context analysis Marc Kleine-Budde
@ 2026-11-22 18:48 ` Marc Kleine-Budde
2026-11-22 18:48 ` [PATCH can-next v10 08/14] can: gs_usb: gs_usb_receive_bulk_callback(): use dev_info() to print re-submit info messages Marc Kleine-Budde
` (6 subsequent siblings)
13 siblings, 0 replies; 24+ messages in thread
From: Marc Kleine-Budde @ 2026-11-22 18:48 UTC (permalink / raw)
To: Vincent Mailhol, Peter Fink, Eric Evenchick, Maximilian Schneider,
Wolfgang Grandegger
Cc: kernel, linux-can, Marc Kleine-Budde, 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().
In an upcoming series can_rx_offload will be improved and
can_rx_offload_add_manual() can fail.
Check the return value and exit via the existing free_candev() on error
path.
Signed-off-by: Ciprian Marian Costea <ciprianmarian.costea@oss.nxp.com>
Link: https://patch.msgid.link/20260907104942.17089-4-ciprianmarian.costea@oss.nxp.com
[mkl: update patch description]
Signed-off-by: Marc Kleine-Budde <mkl@pengutronix.de>
---
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 ab82dc07f3f5..308daa2645ad 100644
--- a/drivers/net/can/usb/gs_usb.c
+++ b/drivers/net/can/usb/gs_usb.c
@@ -1553,7 +1553,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);
return dev;
--
2.53.0
^ permalink raw reply related [flat|nested] 24+ messages in thread
* [PATCH can-next v10 08/14] can: gs_usb: gs_usb_receive_bulk_callback(): use dev_info() to print re-submit info messages
2026-11-22 18:48 [PATCH can-next v10 00/14] gs_usb: fixes Marc Kleine-Budde
` (6 preceding siblings ...)
2026-11-22 18:48 ` [PATCH can-next v10 07/14] can: gs_usb: gs_make_candev(): check can_rx_offload_add_manual() return value Marc Kleine-Budde
@ 2026-11-22 18:48 ` Marc Kleine-Budde
2026-11-24 9:30 ` netdev-bot+sashiko
2026-11-22 18:48 ` [PATCH can-next v10 09/14] can: gs_usb: gs_usb_get_gs_can(): introduce new function to consolidate validation of struct gs_can Marc Kleine-Budde
` (5 subsequent siblings)
13 siblings, 1 reply; 24+ messages in thread
From: Marc Kleine-Budde @ 2026-11-22 18:48 UTC (permalink / raw)
To: Vincent Mailhol, Peter Fink, Eric Evenchick, Maximilian Schneider,
Wolfgang Grandegger
Cc: kernel, linux-can, Marc Kleine-Budde, Sashiko
If an URB is retransmitted (due to an error) before the network device can
be identified, and if this retransmission fails, netdev_info() operates
with a NULL pointer for the network device. Although this does not cause a
system crash, it does not provide any information about which device is
affected.
Replace netdev_info() with dev_info_ratelimited() to output the affected
USB device, and also rate limit the number of messages to prevent log
flooding.
Reported-by: Sashiko <sashiko-bot@kernel.org>
Closes: https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20260729102802.505168-1-mkl@pengutronix.de
Signed-off-by: Marc Kleine-Budde <mkl@pengutronix.de>
---
drivers/net/can/usb/gs_usb.c | 9 +++++----
1 file changed, 5 insertions(+), 4 deletions(-)
diff --git a/drivers/net/can/usb/gs_usb.c b/drivers/net/can/usb/gs_usb.c
index 308daa2645ad..75adec495e65 100644
--- a/drivers/net/can/usb/gs_usb.c
+++ b/drivers/net/can/usb/gs_usb.c
@@ -655,11 +655,11 @@ static void gs_usb_receive_bulk_callback(struct urb *urb)
{
struct gs_usb *parent = urb->context;
struct gs_can *dev;
- struct net_device *netdev = NULL;
int rc;
struct net_device_stats *stats;
struct gs_host_frame *hf = urb->transfer_buffer;
unsigned int minimum_length, data_length;
+ struct net_device *netdev;
struct gs_tx_context *txc;
struct can_frame *cf;
struct canfd_frame *cfd;
@@ -819,9 +819,10 @@ static void gs_usb_receive_bulk_callback(struct urb *urb)
if (parent->canch[rc])
netif_device_detach(parent->canch[rc]->netdev);
}
- } else if (rc != -ESHUTDOWN && net_ratelimit()) {
- netdev_info(netdev, "failed to re-submit IN URB: %pe\n",
- ERR_PTR(rc));
+ } else if (rc != -ESHUTDOWN) {
+ dev_info_ratelimited(&parent->udev->dev,
+ "failed to re-submit IN URB: %pe\n",
+ ERR_PTR(rc));
}
}
--
2.53.0
^ permalink raw reply related [flat|nested] 24+ messages in thread
* [PATCH can-next v10 09/14] can: gs_usb: gs_usb_get_gs_can(): introduce new function to consolidate validation of struct gs_can
2026-11-22 18:48 [PATCH can-next v10 00/14] gs_usb: fixes Marc Kleine-Budde
` (7 preceding siblings ...)
2026-11-22 18:48 ` [PATCH can-next v10 08/14] can: gs_usb: gs_usb_receive_bulk_callback(): use dev_info() to print re-submit info messages Marc Kleine-Budde
@ 2026-11-22 18:48 ` Marc Kleine-Budde
2026-11-24 9:30 ` netdev-bot+sashiko
2026-11-22 18:48 ` [PATCH can-next v10 10/14] can: gs_usb: gs_usb_get_gs_can(): report channel number out of range error Marc Kleine-Budde
` (4 subsequent siblings)
13 siblings, 1 reply; 24+ messages in thread
From: Marc Kleine-Budde @ 2026-11-22 18:48 UTC (permalink / raw)
To: Vincent Mailhol, Peter Fink, Eric Evenchick, Maximilian Schneider,
Wolfgang Grandegger
Cc: kernel, linux-can, Marc Kleine-Budde
To improve code readability and structure consolidate the decoding and
validation of the struct gs_can from the host frame into a the new
function gs_usb_get_gs_can().
Let gs_usb_get_gs_can() only work on the actual header of the host frame,
return a struct gs_can pointer on success, or NULL on error. If fatal is
true, detach the device, otherwise re-submit the URB.
Signed-off-by: Marc Kleine-Budde <mkl@pengutronix.de>
---
drivers/net/can/usb/gs_usb.c | 61 +++++++++++++++++++++++++++++++-------------
1 file changed, 43 insertions(+), 18 deletions(-)
diff --git a/drivers/net/can/usb/gs_usb.c b/drivers/net/can/usb/gs_usb.c
index 75adec495e65..9078460aebca 100644
--- a/drivers/net/can/usb/gs_usb.c
+++ b/drivers/net/can/usb/gs_usb.c
@@ -267,7 +267,7 @@ struct canfd_quirk {
#define GS_HOST_FRAME_ECHO_ID_RX 0xffffffff
struct gs_host_frame {
- struct_group(header,
+ struct_group_tagged(gs_host_frame_header, header,
u32 echo_id;
__le32 can_id;
@@ -651,6 +651,43 @@ gs_usb_get_minimum_rx_length(const struct gs_can *dev, const struct gs_host_fram
return minimum_length;
}
+static struct gs_can *
+gs_usb_get_gs_can(const struct gs_usb *parent,
+ const struct gs_host_frame_header *header, u32 actual_length,
+ bool *fatal)
+{
+ const u32 minimum_length = sizeof(*header);
+
+ if (actual_length < minimum_length) {
+ dev_err_ratelimited(&parent->udev->dev,
+ "short read (actual_length=%u, minimum_length=%u)\n",
+ actual_length, minimum_length);
+
+ return NULL;
+ }
+
+ /* device reports out of range channel id */
+ if (header->channel >= parent->channel_cnt) {
+ *fatal = true;
+
+ return NULL;
+ }
+
+ struct gs_can *dev = parent->canch[header->channel];
+ const struct net_device *netdev = dev->netdev;
+
+ if (!netif_device_present(netdev)) {
+ *fatal = true;
+
+ return NULL;
+ }
+
+ if (!netif_running(netdev))
+ return NULL;
+
+ return dev;
+}
+
static void gs_usb_receive_bulk_callback(struct urb *urb)
{
struct gs_usb *parent = urb->context;
@@ -664,6 +701,7 @@ static void gs_usb_receive_bulk_callback(struct urb *urb)
struct can_frame *cf;
struct canfd_frame *cfd;
struct sk_buff *skb;
+ bool fatal = false;
BUG_ON(!parent);
@@ -678,30 +716,17 @@ static void gs_usb_receive_bulk_callback(struct urb *urb)
return;
}
- minimum_length = sizeof(hf->header);
- if (urb->actual_length < minimum_length) {
- dev_err_ratelimited(&parent->udev->dev,
- "short read (actual_length=%u, minimum_length=%u)\n",
- urb->actual_length, minimum_length);
+ dev = gs_usb_get_gs_can(parent, &hf->header, urb->actual_length, &fatal);
+ if (!dev) {
+ if (fatal)
+ goto device_detach;
goto resubmit_urb;
}
- /* device reports out of range channel id */
- if (hf->channel >= parent->channel_cnt)
- goto device_detach;
-
- dev = parent->canch[hf->channel];
-
netdev = dev->netdev;
stats = &netdev->stats;
- if (!netif_device_present(netdev))
- return;
-
- if (!netif_running(netdev))
- goto resubmit_urb;
-
minimum_length = gs_usb_get_minimum_rx_length(dev, hf, &data_length);
if (urb->actual_length < minimum_length) {
stats->rx_errors++;
--
2.53.0
^ permalink raw reply related [flat|nested] 24+ messages in thread
* [PATCH can-next v10 10/14] can: gs_usb: gs_usb_get_gs_can(): report channel number out of range error
2026-11-22 18:48 [PATCH can-next v10 00/14] gs_usb: fixes Marc Kleine-Budde
` (8 preceding siblings ...)
2026-11-22 18:48 ` [PATCH can-next v10 09/14] can: gs_usb: gs_usb_get_gs_can(): introduce new function to consolidate validation of struct gs_can Marc Kleine-Budde
@ 2026-11-22 18:48 ` Marc Kleine-Budde
2026-11-22 18:48 ` [PATCH can-next v10 11/14] can: gs_usb: gs_usb_get_rx_data_length(): new function to consolidate validation of host frame Marc Kleine-Budde
` (3 subsequent siblings)
13 siblings, 0 replies; 24+ messages in thread
From: Marc Kleine-Budde @ 2026-11-22 18:48 UTC (permalink / raw)
To: Vincent Mailhol, Peter Fink, Eric Evenchick, Maximilian Schneider,
Wolfgang Grandegger
Cc: kernel, linux-can, Marc Kleine-Budde
If the USB device sends an invalid channel, the device should not be
silently detached; instead, the display an error message before detaching.
Signed-off-by: Marc Kleine-Budde <mkl@pengutronix.de>
---
drivers/net/can/usb/gs_usb.c | 3 +++
1 file changed, 3 insertions(+)
diff --git a/drivers/net/can/usb/gs_usb.c b/drivers/net/can/usb/gs_usb.c
index 9078460aebca..df5ea6cb0cf2 100644
--- a/drivers/net/can/usb/gs_usb.c
+++ b/drivers/net/can/usb/gs_usb.c
@@ -668,6 +668,9 @@ gs_usb_get_gs_can(const struct gs_usb *parent,
/* device reports out of range channel id */
if (header->channel >= parent->channel_cnt) {
+ dev_err_ratelimited(&parent->udev->dev,
+ "channel number out of range (channel=%u, channel_cnt=%u)\n",
+ header->channel, parent->channel_cnt);
*fatal = true;
return NULL;
--
2.53.0
^ permalink raw reply related [flat|nested] 24+ messages in thread
* [PATCH can-next v10 11/14] can: gs_usb: gs_usb_get_rx_data_length(): new function to consolidate validation of host frame
2026-11-22 18:48 [PATCH can-next v10 00/14] gs_usb: fixes Marc Kleine-Budde
` (9 preceding siblings ...)
2026-11-22 18:48 ` [PATCH can-next v10 10/14] can: gs_usb: gs_usb_get_gs_can(): report channel number out of range error Marc Kleine-Budde
@ 2026-11-22 18:48 ` Marc Kleine-Budde
2026-11-24 9:31 ` netdev-bot+sashiko
2026-11-22 18:48 ` [PATCH can-next v10 12/14] can: gs_usb: gs_can_start_xmit(): mask invalid parts of CAN ID before sending Marc Kleine-Budde
` (2 subsequent siblings)
13 siblings, 1 reply; 24+ messages in thread
From: Marc Kleine-Budde @ 2026-11-22 18:48 UTC (permalink / raw)
To: Vincent Mailhol, Peter Fink, Eric Evenchick, Maximilian Schneider,
Wolfgang Grandegger
Cc: kernel, linux-can, Marc Kleine-Budde
Add a new function: gs_usb_get_rx_data_length() to consolidate the
validation of the struct gs_host_frame from the received URB.
Return the length of data in the host frame on success, or a negative errno
on error.
Signed-off-by: Marc Kleine-Budde <mkl@pengutronix.de>
---
drivers/net/can/usb/gs_usb.c | 79 +++++++++++++++++++++++++++++++-------------
1 file changed, 56 insertions(+), 23 deletions(-)
diff --git a/drivers/net/can/usb/gs_usb.c b/drivers/net/can/usb/gs_usb.c
index df5ea6cb0cf2..4aa674c743f6 100644
--- a/drivers/net/can/usb/gs_usb.c
+++ b/drivers/net/can/usb/gs_usb.c
@@ -620,9 +620,31 @@ gs_usb_get_echo_skb(struct gs_can *dev, struct sk_buff *skb,
return len;
}
-static unsigned int
+static bool gs_usb_host_frame_header_valid(const struct gs_can *dev,
+ const struct gs_host_frame_header *header)
+{
+ struct net_device *netdev = dev->netdev;
+ struct net_device_stats *stats = &netdev->stats;
+ bool valid = true;
+
+ if (header->echo_id >= GS_MAX_TX_URBS &&
+ header->echo_id < GS_HOST_FRAME_ECHO_ID_RX) {
+ stats->rx_errors++;
+
+ if (net_ratelimit())
+ netdev_err(netdev,
+ "out of range Echo ID (echo_id=0x%08x, max_echo_id=0x%08x)\n",
+ header->echo_id, GS_MAX_TX_URBS - 1);
+
+ valid = false;
+ }
+
+ return valid;
+}
+
+static int
gs_usb_get_minimum_rx_length(const struct gs_can *dev, const struct gs_host_frame *hf,
- unsigned int *data_length_p)
+ int *data_length_p)
{
unsigned int minimum_length, data_length = 0;
@@ -651,6 +673,33 @@ gs_usb_get_minimum_rx_length(const struct gs_can *dev, const struct gs_host_fram
return minimum_length;
}
+static int gs_usb_get_rx_data_length(const struct gs_can *dev,
+ const struct gs_host_frame *hf,
+ const u32 actual_length)
+{
+ struct net_device *netdev = dev->netdev;
+ struct net_device_stats *stats = &netdev->stats;
+ int minimum_length, data_length;
+
+ if (!gs_usb_host_frame_header_valid(dev, &hf->header))
+ return -EINVAL;
+
+ minimum_length = gs_usb_get_minimum_rx_length(dev, hf, &data_length);
+ if (actual_length < minimum_length) {
+ stats->rx_errors++;
+ stats->rx_length_errors++;
+
+ if (net_ratelimit())
+ netdev_err(netdev,
+ "short read (actual_length=%u, minimum_length=%u)\n",
+ actual_length, minimum_length);
+
+ return -EINVAL;
+ }
+
+ return data_length;
+}
+
static struct gs_can *
gs_usb_get_gs_can(const struct gs_usb *parent,
const struct gs_host_frame_header *header, u32 actual_length,
@@ -698,13 +747,13 @@ static void gs_usb_receive_bulk_callback(struct urb *urb)
int rc;
struct net_device_stats *stats;
struct gs_host_frame *hf = urb->transfer_buffer;
- unsigned int minimum_length, data_length;
struct net_device *netdev;
struct gs_tx_context *txc;
struct can_frame *cf;
struct canfd_frame *cfd;
struct sk_buff *skb;
bool fatal = false;
+ int data_length;
BUG_ON(!parent);
@@ -727,22 +776,13 @@ static void gs_usb_receive_bulk_callback(struct urb *urb)
goto resubmit_urb;
}
+ data_length = gs_usb_get_rx_data_length(dev, hf, urb->actual_length);
+ if (data_length < 0)
+ goto resubmit_urb;
+
netdev = dev->netdev;
stats = &netdev->stats;
- minimum_length = gs_usb_get_minimum_rx_length(dev, hf, &data_length);
- if (urb->actual_length < minimum_length) {
- stats->rx_errors++;
- stats->rx_length_errors++;
-
- if (net_ratelimit())
- netdev_err(netdev,
- "short read (actual_length=%u, minimum_length=%u)\n",
- urb->actual_length, minimum_length);
-
- goto resubmit_urb;
- }
-
if (hf->echo_id == GS_HOST_FRAME_ECHO_ID_RX) { /* normal rx */
if (hf->flags & GS_CAN_FLAG_FD) {
skb = alloc_canfd_skb(netdev, &cfd);
@@ -780,13 +820,6 @@ static void gs_usb_receive_bulk_callback(struct urb *urb)
gs_usb_rx_offload(dev, skb, hf);
} else { /* echo_id == hf->echo_id */
- if (hf->echo_id >= GS_MAX_TX_URBS) {
- netdev_err(netdev,
- "Unexpected out of range echo id %u\n",
- hf->echo_id);
- goto resubmit_urb;
- }
-
txc = gs_get_tx_context(dev, hf->echo_id);
/* bad devices send bad echo_ids. */
--
2.53.0
^ permalink raw reply related [flat|nested] 24+ messages in thread
* [PATCH can-next v10 12/14] can: gs_usb: gs_can_start_xmit(): mask invalid parts of CAN ID before sending
2026-11-22 18:48 [PATCH can-next v10 00/14] gs_usb: fixes Marc Kleine-Budde
` (10 preceding siblings ...)
2026-11-22 18:48 ` [PATCH can-next v10 11/14] can: gs_usb: gs_usb_get_rx_data_length(): new function to consolidate validation of host frame Marc Kleine-Budde
@ 2026-11-22 18:48 ` Marc Kleine-Budde
2026-11-24 9:31 ` netdev-bot+sashiko
2026-11-22 18:48 ` [PATCH can-next v10 13/14] can: gs_usb: gs_usb_host_frame_header_valid(): drop invalid CAN error frames Marc Kleine-Budde
2026-11-22 18:48 ` [PATCH can-next v10 14/14] can: gs_usb: gs_usb_host_frame_header_valid(): drop CAN frames with invalid DLC Marc Kleine-Budde
13 siblings, 1 reply; 24+ messages in thread
From: Marc Kleine-Budde @ 2026-11-22 18:48 UTC (permalink / raw)
To: Vincent Mailhol, Peter Fink, Eric Evenchick, Maximilian Schneider,
Wolfgang Grandegger
Cc: kernel, linux-can, Marc Kleine-Budde
Both struct can_frame::can_id and struct canfd_frame::can_id contain the
actual CAN ID as well as several flags. However, not all of them are valid
in all cases and/or in the TX path.
- CAN_EFF_FLAG indicates whether a CAN frame is sent in the standard or
extended frame format.
- A standard frame contains an 11-bit CAN ID; an extended frame contains a
29 bit CAN ID.
- CAN_RTR_FLAG indicates an RTR CAN frame; however, this applies only to
CAN-CC.
- CAN_ERR_MASK is used exclusively in the receive path between the
driver and user space to signal problems with the CAN controller.
Remove invalid flags and mask the actual CAN-ID to 11 or 29 bits, depending
on the EFF flag.
Signed-off-by: Marc Kleine-Budde <mkl@pengutronix.de>
---
drivers/net/can/usb/gs_usb.c | 16 ++++++++++++++--
1 file changed, 14 insertions(+), 2 deletions(-)
diff --git a/drivers/net/can/usb/gs_usb.c b/drivers/net/can/usb/gs_usb.c
index 4aa674c743f6..b345ab34a200 100644
--- a/drivers/net/can/usb/gs_usb.c
+++ b/drivers/net/can/usb/gs_usb.c
@@ -994,8 +994,14 @@ static netdev_tx_t gs_can_start_xmit(struct sk_buff *skb,
if (can_is_canfd_skb(skb)) {
cfd = (struct canfd_frame *)skb->data;
+ canid_t can_id = cfd->can_id;
- hf->can_id = cpu_to_le32(cfd->can_id);
+ if (can_id & CAN_EFF_FLAG)
+ can_id &= CAN_EFF_MASK | CAN_EFF_FLAG;
+ else
+ can_id &= CAN_SFF_MASK;
+
+ hf->can_id = cpu_to_le32(can_id);
hf->can_dlc = can_fd_len2dlc(cfd->len);
hf->flags |= GS_CAN_FLAG_FD;
if (cfd->flags & CANFD_BRS)
@@ -1006,8 +1012,14 @@ static netdev_tx_t gs_can_start_xmit(struct sk_buff *skb,
memcpy(hf->canfd->data, cfd->data, cfd->len);
} else {
cf = (struct can_frame *)skb->data;
+ canid_t can_id = cf->can_id;
- hf->can_id = cpu_to_le32(cf->can_id);
+ if (can_id & CAN_EFF_FLAG)
+ can_id &= CAN_EFF_MASK | CAN_RTR_FLAG | CAN_EFF_FLAG;
+ else
+ can_id &= CAN_SFF_MASK | CAN_RTR_FLAG;
+
+ hf->can_id = cpu_to_le32(can_id);
hf->can_dlc = can_get_cc_dlc(cf, dev->can.ctrlmode);
memcpy(hf->classic_can->data, cf->data, cf->len);
--
2.53.0
^ permalink raw reply related [flat|nested] 24+ messages in thread
* [PATCH can-next v10 13/14] can: gs_usb: gs_usb_host_frame_header_valid(): drop invalid CAN error frames
2026-11-22 18:48 [PATCH can-next v10 00/14] gs_usb: fixes Marc Kleine-Budde
` (11 preceding siblings ...)
2026-11-22 18:48 ` [PATCH can-next v10 12/14] can: gs_usb: gs_can_start_xmit(): mask invalid parts of CAN ID before sending Marc Kleine-Budde
@ 2026-11-22 18:48 ` Marc Kleine-Budde
2026-11-24 9:31 ` netdev-bot+sashiko
2026-11-22 18:48 ` [PATCH can-next v10 14/14] can: gs_usb: gs_usb_host_frame_header_valid(): drop CAN frames with invalid DLC Marc Kleine-Budde
13 siblings, 1 reply; 24+ messages in thread
From: Marc Kleine-Budde @ 2026-11-22 18:48 UTC (permalink / raw)
To: Vincent Mailhol, Peter Fink, Eric Evenchick, Maximilian Schneider,
Wolfgang Grandegger
Cc: kernel, linux-can, Marc Kleine-Budde, Sashiko
By definition, CAN error frames have a length of CAN_ERR_DLC (= 8) bytes
and the RTR flag is not set. The user space expects the frames to have this
length, and the function gs_update_state() accesses the data from the CAN
error frames.
In gs_usb_host_frame_header_valid(), ensure that CAN error frames have a
length of CAN_ERR_DLC bytes and ensure that the RTR flag is not sent. If
this is not the case, output an error message and resubmit the URB.
Reported-by: Sashiko <sashiko-bot@kernel.org>
Closes: https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261004-gs_usb-cleanups-and-fixes-v1-0-7b4b06244a77@pengutronix.de
Signed-off-by: Marc Kleine-Budde <mkl@pengutronix.de>
---
drivers/net/can/usb/gs_usb.c | 29 +++++++++++++++++++++++++++++
1 file changed, 29 insertions(+)
diff --git a/drivers/net/can/usb/gs_usb.c b/drivers/net/can/usb/gs_usb.c
index b345ab34a200..7229d002b938 100644
--- a/drivers/net/can/usb/gs_usb.c
+++ b/drivers/net/can/usb/gs_usb.c
@@ -639,6 +639,35 @@ static bool gs_usb_host_frame_header_valid(const struct gs_can *dev,
valid = false;
}
+ if (!(header->echo_id == GS_HOST_FRAME_ECHO_ID_RX))
+ return valid;
+
+ /* checks for RX'ed (non-echo) CAN frames */
+
+ if (header->can_id & cpu_to_le32(CAN_ERR_FLAG)) {
+ if (header->can_dlc != CAN_ERR_DLC) {
+ stats->rx_errors++;
+ stats->rx_length_errors++;
+
+ if (net_ratelimit())
+ netdev_err(netdev,
+ "invalid DLC in CAN error frame (can_dlc=%u, exptect_can_dlc=%u)\n",
+ header->can_dlc, CAN_ERR_DLC);
+
+ valid = false;
+ }
+
+ if (header->can_id & cpu_to_le32(CAN_RTR_FLAG)) {
+ stats->rx_errors++;
+
+ if (net_ratelimit())
+ netdev_err(netdev,
+ "invalid RTR flag in CAN error frame\n");
+
+ valid = false;
+ }
+ }
+
return valid;
}
--
2.53.0
^ permalink raw reply related [flat|nested] 24+ messages in thread
* [PATCH can-next v10 14/14] can: gs_usb: gs_usb_host_frame_header_valid(): drop CAN frames with invalid DLC
2026-11-22 18:48 [PATCH can-next v10 00/14] gs_usb: fixes Marc Kleine-Budde
` (12 preceding siblings ...)
2026-11-22 18:48 ` [PATCH can-next v10 13/14] can: gs_usb: gs_usb_host_frame_header_valid(): drop invalid CAN error frames Marc Kleine-Budde
@ 2026-11-22 18:48 ` Marc Kleine-Budde
2026-11-24 9:31 ` netdev-bot+sashiko
13 siblings, 1 reply; 24+ messages in thread
From: Marc Kleine-Budde @ 2026-11-22 18:48 UTC (permalink / raw)
To: Vincent Mailhol, Peter Fink, Eric Evenchick, Maximilian Schneider,
Wolfgang Grandegger
Cc: kernel, linux-can, Marc Kleine-Budde, Sashiko
According to the respective CAN standards, the maximum DLC length for
CAN-CC and CAN-FD frames is 0xf. The code in driver assumes that this limit
has been verified.
Drop CAN frames with DLC > 0xf, print a error message and resubmit the URB.
Reported-by: Sashiko <sashiko-bot@kernel.org>
Closes: https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261004-gs_usb-cleanups-and-fixes-v1-0-7b4b06244a77@pengutronix.de
Signed-off-by: Marc Kleine-Budde <mkl@pengutronix.de>
---
drivers/net/can/usb/gs_usb.c | 14 ++++++++++++++
1 file changed, 14 insertions(+)
diff --git a/drivers/net/can/usb/gs_usb.c b/drivers/net/can/usb/gs_usb.c
index 7229d002b938..5e8ea308fabf 100644
--- a/drivers/net/can/usb/gs_usb.c
+++ b/drivers/net/can/usb/gs_usb.c
@@ -668,6 +668,20 @@ static bool gs_usb_host_frame_header_valid(const struct gs_can *dev,
}
}
+ /* we can check for max CAN-CC and CAN-FD DLC at the same time, they are both 0xf */
+ BUILD_BUG_ON(CAN_MAX_RAW_DLC != CANFD_MAX_DLC);
+ if (header->can_dlc > CAN_MAX_RAW_DLC) {
+ stats->rx_errors++;
+ stats->rx_length_errors++;
+
+ if (net_ratelimit())
+ netdev_err(netdev,
+ "out of range DLC (can_dlc=%u, max_can_dlc=%u)\n",
+ header->can_dlc, CAN_MAX_RAW_DLC);
+
+ valid = false;
+ }
+
return valid;
}
--
2.53.0
^ permalink raw reply related [flat|nested] 24+ messages in thread
* Re: [PATCH can-next v10 02/14] can: gs_usb: gs_usb_receive_bulk_callback(): check for overflow flag if SKB allocation fails
2026-11-22 18:48 ` [PATCH can-next v10 02/14] can: gs_usb: gs_usb_receive_bulk_callback(): check for overflow flag if SKB allocation fails Marc Kleine-Budde
@ 2026-11-24 9:30 ` netdev-bot+sashiko
0 siblings, 0 replies; 24+ messages in thread
From: netdev-bot+sashiko @ 2026-11-24 9:30 UTC (permalink / raw)
To: mkl; +Cc: mailhol, pfink, eric, max, wg, kernel, linux-can, stable, kuba
Thank you for your contribution! Sashiko AI review found 1 potential
issue(s) to consider.
Critical: 0 · High: 0 · Medium: 1 · Low: 0
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] In gs_usb_receive_bulk_callback()
(drivers/net/can/usb/gs_usb.c), the CAN-CC RX branch now handles an…
--
can: gs_usb: gs_usb_receive_bulk_callback(): check for overflow flag if SKB allocation fails
When the RX SKB allocation fails in gs_usb_receive_bulk_callback(), this
commit increments rx_dropped. It then jumps to the overflow flag check
instead of resubmitting the URB right away. This applies to both the
CAN-FD and CAN-CC receive paths.
Closes: https://sashiko.dev/#/bug/linux-7b3fe6a0-4628-4d0e-aec0-69f4bf8ad5d4
> diff --git a/drivers/net/can/usb/gs_usb.c b/drivers/net/can/usb/gs_usb.c
> index d1124c49460e3..9e8567544c882 100644
> --- a/drivers/net/can/usb/gs_usb.c
> +++ b/drivers/net/can/usb/gs_usb.c
[ ... ]
> @@ -686,8 +689,11 @@ static void gs_usb_receive_bulk_callback(struct urb *urb)
> memcpy(cfd->data, hf->canfd->data, data_length);
> } else {
> skb = alloc_can_skb(netdev, &cf);
> - if (!skb)
> - goto resubmit_urb;
> + if (!skb) {
> + stats->rx_dropped++;
> +
> + goto check_overflow;
> + }
>
> cf->can_id = le32_to_cpu(hf->can_id);
> can_frame_set_cc_len(cf, hf->can_dlc, dev->can.ctrlmode);
[Severity: Medium]
Does this goto also skip the controller state update for error frames?
A few lines further down in the same CAN-CC branch of
gs_usb_receive_bulk_callback() there is:
/* ERROR frames tell us information about the controller */
if (le32_to_cpu(hf->can_id) & CAN_ERR_FLAG)
gs_update_state(dev, cf);
gs_update_state() is the only place in the RX path that sets
dev->can.state. It is also the only place that increments the can_stats
counters restarts, bus_off, error_warning and error_passive. It reads
these values from cf, so it cannot run when alloc_can_skb() fails:
gs_update_state() {
...
if (cf->can_id & CAN_ERR_RESTARTED) {
dev->can.state = CAN_STATE_ERROR_ACTIVE;
can_stats->restarts++;
} else if (cf->can_id & CAN_ERR_BUSOFF) {
dev->can.state = CAN_STATE_BUS_OFF;
can_stats->bus_off++;
...
}
Suppose the device sends a host frame with CAN_ERR_FLAG and
CAN_ERR_BUSOFF, CAN_ERR_RESTARTED or CAN_ERR_CRTL set, and the
GFP_ATOMIC allocation fails. Would dev->can.state then stay stale, for
example ERROR_ACTIVE while the controller is bus-off? Would the
can_device_stats counters reported to userspace under-count?
The old goto resubmit_urb skipped this too. However, the commit message
says the failure path "neither updates the statistics nor checks whether
an overflow flag is set", and after this change the can_stats and state
accounting are still skipped on that path.
Could the state be decoded from hf->can_id and hf->classic_can->data[1],
so it is applied even without an skb? Several other CAN drivers
(at91_can.c, mcp251xfd-core.c, rockchip_canfd-core.c, esdacc.c and
pcan_usb_fd.c) call can_change_state() with cf == NULL for this reason.
Some of them carry comments like "things must be done even in case of
OOM".
[ ... ]
--
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261122-gs_usb-fixes-v10-0-7c165c35d04a%40pengutronix.de
^ permalink raw reply [flat|nested] 24+ messages in thread
* Re: [PATCH can-next v10 04/14] can: gs_usb: use spin_lock_irqsave() instead of spin_lock_bh() to protect struct gs_usb::tc
2026-11-22 18:48 ` [PATCH can-next v10 04/14] can: gs_usb: use spin_lock_irqsave() instead of spin_lock_bh() to protect struct gs_usb::tc Marc Kleine-Budde
@ 2026-11-24 9:30 ` netdev-bot+sashiko
0 siblings, 0 replies; 24+ messages in thread
From: netdev-bot+sashiko @ 2026-11-24 9:30 UTC (permalink / raw)
To: mkl; +Cc: mailhol, pfink, eric, max, wg, kernel, linux-can, stable, kuba
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] This patch adds `unsigned long tc_lock_flags` to struct gs_usb
and creates a locking contract that is not written down anywhere.
- [Low] The commit message has typos and does not state the user-visible
impact.
--
can: gs_usb: use spin_lock_irqsave() instead of spin_lock_bh() to protect struct gs_usb::tc
This commit replaces spin_lock_bh() with spin_lock_irqsave() for
struct gs_usb::tc_lock. The reason is that gs_usb_skb_set_timestamp() can
be called from the URB completion handler in hardirq context when the HCD
does not set HCD_BH. A new tc_lock_flags field in struct gs_usb carries
the saved IRQ state into the cyclecounter read callback, which has to drop
and re-take the lock.
Closes: https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261114-gs_usb-fixes-v8-0-f615502b005b%40pengutronix.de?part=7
> gs_usb_skb_set_timestamp() is called from gs_usb_receive_bulk_callback(),
> the URB completion handle. usb_hcd_giveback_urb() calls the URB completion
> handler directly when the HCD does not set HCD_BH.
[ ... ]
> This issue has not been observed on read hardware, but found by LLM review.
[Severity: Low]
This isn't a bug, but there are two typos here. "completion handle" should
be "completion handler", and "read hardware" should be "real hardware".
The premise is correct. usb_hcd_giveback_urb() calls the handler directly:
drivers/usb/core/hcd.c:usb_hcd_giveback_urb() {
...
if (!hcd_giveback_urb_in_bh(hcd) && !is_root_hub(urb->dev)) {
__usb_hcd_giveback_urb(urb);
return;
}
...
}
So gs_usb_receive_bulk_callback()->gs_usb_set_timestamp()->
gs_usb_skb_set_timestamp() can run in hardirq context.
Could the commit message also say what goes wrong without this fix? With
spin_lock_bh(), gs_usb_timestamp_work() can hold tc_lock while hardirqs
are enabled. A URB completion on the same CPU would then spin on tc_lock
forever. Lockdep would also report an inconsistent {IN-HARDIRQ-W} vs
{HARDIRQ-ON-W} lock state.
The patch is tagged Cc: stable, and the stable team needs that impact
statement to evaluate it.
> diff --git a/drivers/net/can/usb/gs_usb.c b/drivers/net/can/usb/gs_usb.c
> index c9306e7155529..470dd52d97dfe 100644
> --- a/drivers/net/can/usb/gs_usb.c
> +++ b/drivers/net/can/usb/gs_usb.c
> @@ -333,6 +333,7 @@ struct gs_usb {
> struct cyclecounter cc;
> struct timecounter tc;
> spinlock_t tc_lock; /* spinlock to guard access tc->cycle_last */
> + unsigned long tc_lock_flags;
[Severity: Low]
This isn't a bug in the current callers, but should this new field have a
comment that describes the locking contract it creates?
gs_usb_timestamp_read() is the cyclecounter read callback. It drops and
re-takes tc_lock using the flags stored in the struct:
spin_unlock_irqrestore(&parent->tc_lock, parent->tc_lock_flags);
err = gs_usb_get_timestamp(parent, ×tamp);
spin_lock_irqsave(&parent->tc_lock, parent->tc_lock_flags);
The cc->read() signature cannot carry the caller's flags. Any path that
reaches cc->read therefore has to follow two rules that are not written
down. This covers timecounter_read() in gs_usb_timestamp_work() and
timecounter_init() in gs_usb_timestamp_init():
- the caller must save its IRQ state in parent->tc_lock_flags, not in a
local variable
- the caller must be in sleepable context, because of the GFP_KERNEL
control transfer in gs_usb_get_timestamp()
The patch also uses two conventions for the same lock.
gs_usb_skb_set_timestamp() keeps its flags in a local variable. That is
safe only because timecounter_cyc2time() never calls cc->read.
The commit message does not mention the new field either.
Two related documentation problems are fixed later in the series by "can:
gs_usb: add support for context analysis":
- the stale tc_lock comment ("guard access tc->cycle_last")
- the __must_hold(&dev->tc_lock) annotation on gs_usb_timestamp_read(),
which names a dev that does not exist in that function
tc_lock_flags still has no comment at the end of the series.
The contract is also harder to see later in the series.
gs_usb_timestamp_start() calls timecounter_init() under
scoped_guard(spinlock_irqsave) with guard-local flags. That is safe only
because cc->read is temporarily swapped to
gs_usb_timestamp_read_initial().
A future caller that takes tc_lock with local flags and then reaches
gs_usb_timestamp_read() would restore a stale IRQ state from
parent->tc_lock_flags, and nothing in the code warns against that.
--
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261122-gs_usb-fixes-v10-0-7c165c35d04a%40pengutronix.de
^ permalink raw reply [flat|nested] 24+ messages in thread
* Re: [PATCH can-next v10 05/14] can: gs_usb: fix hardware timestamp state for mixed channels
2026-11-22 18:48 ` [PATCH can-next v10 05/14] can: gs_usb: fix hardware timestamp state for mixed channels Marc Kleine-Budde
@ 2026-11-24 9:30 ` netdev-bot+sashiko
0 siblings, 0 replies; 24+ messages in thread
From: netdev-bot+sashiko @ 2026-11-24 9:30 UTC (permalink / raw)
To: mkl; +Cc: mailhol, pfink, eric, max, wg, kernel, linux-can, stable, kuba
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 does not say what actually goes wrong before
this fix.
--
can: gs_usb: fix hardware timestamp state for mixed channels
This patch moves the HW timestamp setup (cyclecounter and delayed work
init) into gs_usb_probe(). The setup now runs if any channel advertises
GS_CAN_FEATURE_HW_TIMESTAMP. Start and stop are keyed on a new
parent->flags bit rather than on the feature flags of the first channel
opened or the last channel closed.
Closes: https://sashiko.dev/#/bug/linux-b39360f9-6cd8-4f47-83bb-f0f78331f308
Closes: https://sashiko.dev/#/bug/linux-5f8ecd4e-344e-4ceb-9b46-886f4942a5f6
> Currently both the initialization and shutdown of the HW timestamping
> infrastructure is tied to the flags of the first channel opened
> respectively last channel closed. This leads to problems if the first
> channel opened doesn't have the timestamping feature, but the second
> channel has. In this case the timestamping is never properly initialized.
[Severity: Low]
This isn't a bug, but could the commit message say what actually goes
wrong before this fix? "Leads to problems" and "never properly
initialized" don't tell a stable reader how serious the fixed bugs are.
Both orderings look like memory-safety bugs in the old code.
In the first case, a channel without HW timestamps opens first and a
channel with HW timestamps opens second. gs_usb_timestamp_init() never
runs, so parent->tc.cc stays NULL and tc_lock is never initialized. An
RX or echo frame on the second channel still passes the
dev->feature & GS_CAN_FEATURE_HW_TIMESTAMP check and ends up in:
gs_usb_set_timestamp()
gs_usb_skb_set_timestamp()
timecounter_cyc2time()
timecounter_cyc2time() reads tc->cc->mask. Isn't that a NULL pointer
dereference in URB completion context?
In the second case, a channel with HW timestamps opens first, so the
timestamp state is initialized and the delayed work is scheduled. Then
a channel without HW timestamps opens, the first channel closes, and
the second channel closes last.
The old gs_can_close() checked only the last channel's dev->feature, so
gs_usb_timestamp_stop() was never called:
if (dev->feature & GS_CAN_FEATURE_HW_TIMESTAMP)
gs_usb_timestamp_stop(parent);
gs_usb_timestamp_work() re-arms itself unconditionally, and the old
gs_usb_disconnect() calls kfree(parent) without cancelling the work.
Wouldn't the delayed work embedded in parent then run after parent has
been freed?
In the same scenario, re-opening the first channel calls
spin_lock_init() and INIT_DELAYED_WORK() again while the work is still
pending.
The code changes appear to fix all of these. Would it be worth naming
the NULL pointer oops and the delayed work use-after-free in the commit
message?
--
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261122-gs_usb-fixes-v10-0-7c165c35d04a%40pengutronix.de
^ permalink raw reply [flat|nested] 24+ messages in thread
* Re: [PATCH can-next v10 08/14] can: gs_usb: gs_usb_receive_bulk_callback(): use dev_info() to print re-submit info messages
2026-11-22 18:48 ` [PATCH can-next v10 08/14] can: gs_usb: gs_usb_receive_bulk_callback(): use dev_info() to print re-submit info messages Marc Kleine-Budde
@ 2026-11-24 9:30 ` netdev-bot+sashiko
0 siblings, 0 replies; 24+ messages in thread
From: netdev-bot+sashiko @ 2026-11-24 9:30 UTC (permalink / raw)
To: mkl; +Cc: mailhol, pfink, eric, max, wg, kernel, linux-can, stable, kuba
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 fixes a defect that a specific earlier commit
introduced, but it has no Fixes: tag.
- [Low] The commit message does not match the code.
--
can: gs_usb: gs_usb_receive_bulk_callback(): use dev_info() to print re-submit info messages
This commit changes the IN URB re-submit failure path in
gs_usb_receive_bulk_callback() to use dev_info_ratelimited() on the
parent USB device instead of netdev_info(). With this change the message
names the affected device even when no network device has been
identified yet. It also drops the NULL initializer of the local netdev
variable.
Closes: https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20260729102802.505168-1-mkl@pengutronix.de
> diff --git a/drivers/net/can/usb/gs_usb.c b/drivers/net/can/usb/gs_usb.c
> index 308daa2645ada..75adec495e657 100644
> --- a/drivers/net/can/usb/gs_usb.c
> +++ b/drivers/net/can/usb/gs_usb.c
[ ... ]
> @@ -819,9 +819,10 @@ static void gs_usb_receive_bulk_callback(struct urb *urb)
> if (parent->canch[rc])
> netif_device_detach(parent->canch[rc]->netdev);
> }
> - } else if (rc != -ESHUTDOWN && net_ratelimit()) {
> - netdev_info(netdev, "failed to re-submit IN URB: %pe\n",
> - ERR_PTR(rc));
> + } else if (rc != -ESHUTDOWN) {
> + dev_info_ratelimited(&parent->udev->dev,
> + "failed to re-submit IN URB: %pe\n",
> + ERR_PTR(rc));
[Severity: Low]
The commit message says:
Replace netdev_info() with dev_info_ratelimited() to output the affected
USB device, and also rate limit the number of messages to prevent log
flooding.
Is the "also rate limit" part accurate? The old code was already rate
limited by the net_ratelimit() check in the else-if condition.
What actually changes is the rate limit state. The old code used the
global net_ratelimit_state, which all networking messages share. The new
code uses the per call site state that dev_info_ratelimited() defines.
There are two smaller mismatches. The subject says "use dev_info()", but
the code calls dev_info_ratelimited(). The commit message also doesn't
mention that the netdev = NULL initializer was removed.
The removal looks safe at this commit. The short read check jumps to
resubmit_urb before netdev = dev->netdev is assigned, but the code it
reaches no longer reads netdev.
Could the commit message be updated to describe these changes?
[Severity: Low]
This patch finishes a fix for a problem that an earlier commit
introduced, but it has no Fixes: tag.
Commit 79a6d1bfe114 ("can: gs_usb: gs_usb_receive_bulk_callback():
unanchor URL on usb_submit_urb() error") added the netdev_info(netdev, ...)
call on this path. That call can be reached from the short read check:
minimum_length = sizeof(hf->header);
if (urb->actual_length < minimum_length) {
dev_err_ratelimited(&parent->udev->dev, ...);
goto resubmit_urb;
}
That jump happens before netdev = dev->netdev is assigned. If
usb_submit_urb() then fails with something other than -ENODEV or
-ESHUTDOWN, the message is printed without a valid netdev.
Commit 494fc029f662 ("can: gs_usb: gs_usb_receive_bulk_callback(): fix
error message") carried Fixes: 79a6d1bfe114. It only set netdev to NULL,
so the message prints "(NULL net_device)" and does not name the device.
Should this patch carry Fixes: 494fc029f662 (or Fixes: 79a6d1bfe114)?
That would let trees that took those commits find this follow-up.
> }
> }
>
--
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261122-gs_usb-fixes-v10-0-7c165c35d04a%40pengutronix.de
^ permalink raw reply [flat|nested] 24+ messages in thread
* Re: [PATCH can-next v10 09/14] can: gs_usb: gs_usb_get_gs_can(): introduce new function to consolidate validation of struct gs_can
2026-11-22 18:48 ` [PATCH can-next v10 09/14] can: gs_usb: gs_usb_get_gs_can(): introduce new function to consolidate validation of struct gs_can Marc Kleine-Budde
@ 2026-11-24 9:30 ` netdev-bot+sashiko
0 siblings, 0 replies; 24+ messages in thread
From: netdev-bot+sashiko @ 2026-11-24 9:30 UTC (permalink / raw)
To: mkl; +Cc: mailhol, pfink, eric, max, wg, kernel, linux-can, stable, kuba
Thank you for your contribution! Sashiko AI review found 1 potential
issue(s) to consider.
Critical: 0 · High: 0 · Medium: 1 · Low: 0
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] This patch quietly changes how gs_usb_receive_bulk_callback()
handles a frame for a channel whose netdev is not present.
--
can: gs_usb: gs_usb_get_gs_can(): introduce new function to consolidate validation of struct gs_can
This commit moves the decoding and validation of struct gs_can from the
host frame header into a new helper, gs_usb_get_gs_can(). The helper
returns the gs_can pointer on success, or NULL on error. A fatal flag
tells the caller whether to detach the device or resubmit the URB.
> diff --git a/drivers/net/can/usb/gs_usb.c b/drivers/net/can/usb/gs_usb.c
> index 75adec495e657..9078460aebcaa 100644
> --- a/drivers/net/can/usb/gs_usb.c
> +++ b/drivers/net/can/usb/gs_usb.c
[ ... ]
> @@ -651,6 +651,43 @@ gs_usb_get_minimum_rx_length(const struct gs_can *dev, const struct gs_host_fram
> return minimum_length;
> }
>
> +static struct gs_can *
> +gs_usb_get_gs_can(const struct gs_usb *parent,
> + const struct gs_host_frame_header *header, u32 actual_length,
> + bool *fatal)
> +{
[ ... ]
> + struct gs_can *dev = parent->canch[header->channel];
> + const struct net_device *netdev = dev->netdev;
> +
> + if (!netif_device_present(netdev)) {
> + *fatal = true;
> +
> + return NULL;
> + }
[Severity: Medium]
Is this a deliberate behaviour change? Before this patch, the
!netif_device_present() case did a plain return in
gs_usb_receive_bulk_callback(). It dropped that one URB without
resubmitting it, and it left the other channels alone:
- if (!netif_device_present(netdev))
- return;
With *fatal = true, the caller now jumps to device_detach. That label
detaches every channel on the adapter:
device_detach:
for (rc = 0; rc < parent->channel_cnt; rc++) {
if (parent->canch[rc])
netif_device_detach(parent->canch[rc]->netdev);
}
A netdev can be not-present while the USB device is still attached.
gs_usb_probe() fills parent->canch[] for all channels first. It then
calls register_candev() one channel at a time:
for (i = 0; i < icount; i++) {
struct gs_can *dev = parent->canch[i];
rc = register_candev(dev->netdev);
So channel 0 can be opened before channel 1 is registered, and
gs_can_open() submits the shared bulk-IN URBs. If the device then sends
a frame tagged with the unregistered channel, would that now detach the
running channel 0 as well?
Once channel 0 is detached, its later RX frames also hit the fatal
path and the URBs are never resubmitted. TX queues stop, and reopening
fails with -ENODEV. gs_usb.c never calls netif_device_attach(), so the
channels appear to stay detached until the device is re-probed.
The other detach sites in this file only detach the affected channel's
netdev on -ENODEV, for example in gs_can_start_xmit():
if (rc == -ENODEV) {
netif_device_detach(netdev);
A single bool also can't express the old third outcome: drop the URB,
don't resubmit, don't detach.
The commit message says:
If fatal is
true, detach the device, otherwise re-submit the URB.
It doesn't mention that a case which used to just drop the URB now
detaches every channel. If this change is intended, could the commit
message say so? Otherwise, could the old drop-only behaviour be kept for
the not-present case?
This behaviour looks the same at the end of the series. The final
gs_usb_get_gs_can() still sets *fatal for !netif_device_present(), and
probe still registers the channels in sequence.
> +
> + if (!netif_running(netdev))
> + return NULL;
> +
> + return dev;
> +}
[ ... ]
> @@ -678,30 +716,17 @@ static void gs_usb_receive_bulk_callback(struct urb *urb)
> return;
> }
>
> - minimum_length = sizeof(hf->header);
> - if (urb->actual_length < minimum_length) {
> - dev_err_ratelimited(&parent->udev->dev,
> - "short read (actual_length=%u, minimum_length=%u)\n",
> - urb->actual_length, minimum_length);
> + dev = gs_usb_get_gs_can(parent, &hf->header, urb->actual_length, &fatal);
> + if (!dev) {
> + if (fatal)
> + goto device_detach;
>
> goto resubmit_urb;
> }
>
> - /* device reports out of range channel id */
> - if (hf->channel >= parent->channel_cnt)
> - goto device_detach;
> -
> - dev = parent->canch[hf->channel];
> -
> netdev = dev->netdev;
> stats = &netdev->stats;
>
> - if (!netif_device_present(netdev))
> - return;
> -
> - if (!netif_running(netdev))
> - goto resubmit_urb;
> -
[ ... ]
--
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261122-gs_usb-fixes-v10-0-7c165c35d04a%40pengutronix.de
^ permalink raw reply [flat|nested] 24+ messages in thread
* Re: [PATCH can-next v10 11/14] can: gs_usb: gs_usb_get_rx_data_length(): new function to consolidate validation of host frame
2026-11-22 18:48 ` [PATCH can-next v10 11/14] can: gs_usb: gs_usb_get_rx_data_length(): new function to consolidate validation of host frame Marc Kleine-Budde
@ 2026-11-24 9:31 ` netdev-bot+sashiko
0 siblings, 0 replies; 24+ messages in thread
From: netdev-bot+sashiko @ 2026-11-24 9:31 UTC (permalink / raw)
To: mkl; +Cc: mailhol, pfink, eric, max, wg, kernel, linux-can, stable, kuba
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 the patch only adds
gs_usb_get_rx_data_length() to consolidate host-frame validation, but…
- [Low] After this patch, the driver's two paths for rejecting a bad TX-
completion echo ID handle stats and logging differently.
- [Low] The patch makes gs_usb_get_minimum_rx_length() return `int` and
take an `int *data_length_p`, and gs_usb_get_rx_data_length() declares…
--
can: gs_usb: gs_usb_get_rx_data_length(): new function to consolidate validation of host frame
This patch adds gs_usb_get_rx_data_length() and
gs_usb_host_frame_header_valid(). They take the minimum length check and
the out of range echo ID check out of gs_usb_receive_bulk_callback(). The
new helper returns the data length of the host frame, or a negative errno.
> diff --git a/drivers/net/can/usb/gs_usb.c b/drivers/net/can/usb/gs_usb.c
> index df5ea6cb0cf2a..4aa674c743f67 100644
> --- a/drivers/net/can/usb/gs_usb.c
> +++ b/drivers/net/can/usb/gs_usb.c
> @@ -620,9 +620,31 @@ gs_usb_get_echo_skb(struct gs_can *dev, struct sk_buff *skb,
> return len;
> }
>
> -static unsigned int
> +static bool gs_usb_host_frame_header_valid(const struct gs_can *dev,
> + const struct gs_host_frame_header *header)
> +{
> + struct net_device *netdev = dev->netdev;
> + struct net_device_stats *stats = &netdev->stats;
> + bool valid = true;
> +
> + if (header->echo_id >= GS_MAX_TX_URBS &&
> + header->echo_id < GS_HOST_FRAME_ECHO_ID_RX) {
> + stats->rx_errors++;
> +
> + if (net_ratelimit())
> + netdev_err(netdev,
> + "out of range Echo ID (echo_id=0x%08x, max_echo_id=0x%08x)\n",
> + header->echo_id, GS_MAX_TX_URBS - 1);
> +
> + valid = false;
> + }
[Severity: Low]
The commit message only talks about adding gs_usb_get_rx_data_length() to
consolidate validation. This hunk also brings in some visible changes that
the message doesn't mention:
- a second helper, gs_usb_host_frame_header_valid(), is added
- an out of range TX echo ID now increments netdev->stats.rx_errors;
before, no counter changed
- the message is now rate limited with net_ratelimit(); before, it was
an unconditional netdev_err()
- the message text changes from "Unexpected out of range echo id %u"
(decimal) to "out of range Echo ID (echo_id=0x%08x,
max_echo_id=0x%08x)" (hex)
- in gs_usb_get_rx_data_length(), the echo ID range check now runs
before the minimum length check; before, it ran after it, in the else
branch of gs_usb_receive_bulk_callback()
None of these look harmful. Still, rx_errors in ip -s link and the log
text both change for the same device input.
Could the commit message describe these changes, so it doesn't read as a
purely mechanical consolidation?
> +
> + return valid;
> +}
> +
> +static int
> gs_usb_get_minimum_rx_length(const struct gs_can *dev, const struct gs_host_frame *hf,
> - unsigned int *data_length_p)
> + int *data_length_p)
> {
> unsigned int minimum_length, data_length = 0;
>
> @@ -651,6 +673,33 @@ gs_usb_get_minimum_rx_length(const struct gs_can *dev, const struct gs_host_fram
> return minimum_length;
> }
>
> +static int gs_usb_get_rx_data_length(const struct gs_can *dev,
> + const struct gs_host_frame *hf,
> + const u32 actual_length)
> +{
> + struct net_device *netdev = dev->netdev;
> + struct net_device_stats *stats = &netdev->stats;
> + int minimum_length, data_length;
> +
> + if (!gs_usb_host_frame_header_valid(dev, &hf->header))
> + return -EINVAL;
> +
> + minimum_length = gs_usb_get_minimum_rx_length(dev, hf, &data_length);
> + if (actual_length < minimum_length) {
> + stats->rx_errors++;
> + stats->rx_length_errors++;
> +
> + if (net_ratelimit())
> + netdev_err(netdev,
> + "short read (actual_length=%u, minimum_length=%u)\n",
> + actual_length, minimum_length);
[Severity: Low]
minimum_length is now a signed int. It is still compared against the u32
actual_length and printed with %u.
gs_usb_get_minimum_rx_length() above now returns int and fills an int
pointer. Internally it still computes unsigned int minimum_length and
data_length from sizeof() and struct_size().
The value is always a small positive number today, so nothing misbehaves.
But what happens if this helper ever returns a negative errno, as the int
return type suggests it could? The comparison actual_length <
minimum_length would turn it into a huge unsigned value, the check would
fail, and the error path would be taken for every frame.
Would it be cleaner to keep minimum_length as unsigned int or u32 here, or
to print it with %d?
> +
> + return -EINVAL;
> + }
[ ... ]
> @@ -780,13 +820,6 @@ static void gs_usb_receive_bulk_callback(struct urb *urb)
>
> gs_usb_rx_offload(dev, skb, hf);
> } else { /* echo_id == hf->echo_id */
> - if (hf->echo_id >= GS_MAX_TX_URBS) {
> - netdev_err(netdev,
> - "Unexpected out of range echo id %u\n",
> - hf->echo_id);
> - goto resubmit_urb;
> - }
> -
> txc = gs_get_tx_context(dev, hf->echo_id);
>
> /* bad devices send bad echo_ids. */
[Severity: Low]
With the out of range check moved into gs_usb_host_frame_header_valid(),
the two ways of rejecting a bad TX echo ID now behave differently.
An out of range echo ID increments rx_errors, is rate limited and is
printed in hex. An echo ID that is in range but unused still ends up here
in gs_usb_receive_bulk_callback():
if (!txc) {
netdev_err(netdev,
"Unexpected unused echo id %u\n",
hf->echo_id);
goto resubmit_urb;
}
This path changes no counter and isn't rate limited.
Before this patch the two paths behaved the same. Should the unused echo
ID case also bump rx_errors and use net_ratelimit()? Otherwise the
rx_errors value depends on which kind of bad echo ID the device sent.
Also, gs_get_tx_context() keeps its own range check:
if (id < GS_MAX_TX_URBS) {
This duplicates the check in gs_usb_host_frame_header_valid(). Is it meant
to stay, given that the goal is to keep host frame validation in one
place?
--
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261122-gs_usb-fixes-v10-0-7c165c35d04a%40pengutronix.de
^ permalink raw reply [flat|nested] 24+ messages in thread
* Re: [PATCH can-next v10 12/14] can: gs_usb: gs_can_start_xmit(): mask invalid parts of CAN ID before sending
2026-11-22 18:48 ` [PATCH can-next v10 12/14] can: gs_usb: gs_can_start_xmit(): mask invalid parts of CAN ID before sending Marc Kleine-Budde
@ 2026-11-24 9:31 ` netdev-bot+sashiko
0 siblings, 0 replies; 24+ messages in thread
From: netdev-bot+sashiko @ 2026-11-24 9:31 UTC (permalink / raw)
To: mkl; +Cc: mailhol, pfink, eric, max, wg, kernel, linux-can, stable, kuba
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 names the wrong macro.
Pre-existing issues:
- [High] This problem wasn't introduced by this patch, but
gs_can_start_xmit() sends uninitialized kernel heap memory to the USB…
--
can: gs_usb: gs_can_start_xmit(): mask invalid parts of CAN ID before sending
This commit masks the CAN ID in gs_can_start_xmit() before the frame is
sent to the device. Flags that are not valid in the TX path are removed,
and the ID is limited to 11 or 29 bits depending on CAN_EFF_FLAG.
CAN_RTR_FLAG is kept only for CAN-CC frames.
> - CAN_ERR_MASK is used exclusively in the receive path between the
> driver and user space to signal problems with the CAN controller.
[Severity: Low]
Should this bullet say CAN_ERR_FLAG instead of CAN_ERR_MASK?
include/uapi/linux/can.h has:
#define CAN_ERR_FLAG 0x20000000U /* error message frame */
#define CAN_ERR_MASK 0x1FFFFFFFU /* omit EFF, RTR, ERR flags */
CAN_ERR_MASK has the same value as CAN_EFF_MASK. In gs_can_start_xmit(),
the new EFF masks keep every CAN_ERR_MASK bit and drop only CAN_ERR_FLAG:
can_id &= CAN_EFF_MASK | CAN_EFF_FLAG;
...
can_id &= CAN_EFF_MASK | CAN_RTR_FLAG | CAN_EFF_FLAG;
> diff --git a/drivers/net/can/usb/gs_usb.c b/drivers/net/can/usb/gs_usb.c
> index 4aa674c743f67..b345ab34a200a 100644
> --- a/drivers/net/can/usb/gs_usb.c
> +++ b/drivers/net/can/usb/gs_usb.c
[ ... ]
> @@ -1006,8 +1012,14 @@ static netdev_tx_t gs_can_start_xmit(struct sk_buff *skb,
> memcpy(hf->canfd->data, cfd->data, cfd->len);
> } else {
> cf = (struct can_frame *)skb->data;
> + canid_t can_id = cf->can_id;
> +
> + if (can_id & CAN_EFF_FLAG)
> + can_id &= CAN_EFF_MASK | CAN_RTR_FLAG | CAN_EFF_FLAG;
> + else
> + can_id &= CAN_SFF_MASK | CAN_RTR_FLAG;
>
> - hf->can_id = cpu_to_le32(cf->can_id);
> + hf->can_id = cpu_to_le32(can_id);
> hf->can_dlc = can_get_cc_dlc(cf, dev->can.ctrlmode);
>
> memcpy(hf->classic_can->data, cf->data, cf->len);
[Severity: High]
This isn't a bug introduced by this patch, but does gs_can_start_xmit()
send uninitialized kernel heap memory to the USB device?
The TX buffer is not zeroed when it is allocated:
hf = kmalloc(dev->hf_size_tx, GFP_ATOMIC);
The function writes only the header fields plus cf->len (0..8) or
cfd->len (0..64) payload bytes. It then submits the whole buffer:
usb_fill_bulk_urb(urb, dev->udev,
dev->parent->pipe_out,
hf, dev->hf_size_tx,
gs_usb_xmit_callback, txc);
When CAN_CTRLMODE_FD is enabled, gs_can_open() sizes the buffer for a
64 byte payload, even when the frame is classic CAN:
dev->hf_size_tx = struct_size(hf, canfd, 1);
In FD mode, a classic frame leaves at least 56 data bytes unwritten. A
short or zero-length FD frame leaves up to 64. The quirk byte in
classic_can_quirk and canfd_quirk (LPC546XX devices) is never written
either.
That would expose leftover slab contents with every transmitted frame,
to the device or to anyone sniffing the bus. Would kzalloc(), or a
memset() of the area after the copied payload, fix this?
Later patches in this series do not seem to address it.
--
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261122-gs_usb-fixes-v10-0-7c165c35d04a%40pengutronix.de
^ permalink raw reply [flat|nested] 24+ messages in thread
* Re: [PATCH can-next v10 13/14] can: gs_usb: gs_usb_host_frame_header_valid(): drop invalid CAN error frames
2026-11-22 18:48 ` [PATCH can-next v10 13/14] can: gs_usb: gs_usb_host_frame_header_valid(): drop invalid CAN error frames Marc Kleine-Budde
@ 2026-11-24 9:31 ` netdev-bot+sashiko
0 siblings, 0 replies; 24+ messages in thread
From: netdev-bot+sashiko @ 2026-11-24 9:31 UTC (permalink / raw)
To: mkl; +Cc: mailhol, pfink, eric, max, wg, kernel, linux-can, stable, kuba
Thank you for your contribution! Sashiko AI review found 4 potential
issue(s) to consider.
Critical: 0 · High: 0 · Medium: 1 · Low: 3
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] gs_usb_host_frame_header_valid()
(drivers/net/can/usb/gs_usb.c) now rejects every received CAN error…
- [Low] The patch is titled "drop invalid CAN error frames" and checks
only DLC and RTR.
- [Low] In gs_usb_host_frame_header_valid(), the new DLC check (line 648)
and RTR check (line 660) are independent if-blocks.
- [Low] When the new checks reject an RX error frame,
gs_usb_receive_bulk_callback() goes from the data_length < 0 check…
--
can: gs_usb: gs_usb_host_frame_header_valid(): drop invalid CAN error frames
This commit adds checks to gs_usb_host_frame_header_valid() for received
CAN error frames. They must have a length of CAN_ERR_DLC and must not have
the RTR flag set. Frames that fail are reported with a rate limited error
message, and the URB is resubmitted without delivering the frame.
Closes: https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261004-gs_usb-cleanups-and-fixes-v1-0-7b4b06244a77@pengutronix.de
> diff --git a/drivers/net/can/usb/gs_usb.c b/drivers/net/can/usb/gs_usb.c
> index b345ab34a200a..7229d002b938a 100644
> --- a/drivers/net/can/usb/gs_usb.c
> +++ b/drivers/net/can/usb/gs_usb.c
> @@ -639,6 +639,35 @@ static bool gs_usb_host_frame_header_valid(const struct gs_can *dev,
> valid = false;
> }
>
> + if (!(header->echo_id == GS_HOST_FRAME_ECHO_ID_RX))
> + return valid;
> +
> + /* checks for RX'ed (non-echo) CAN frames */
> +
> + if (header->can_id & cpu_to_le32(CAN_ERR_FLAG)) {
> + if (header->can_dlc != CAN_ERR_DLC) {
> + stats->rx_errors++;
> + stats->rx_length_errors++;
> +
> + if (net_ratelimit())
> + netdev_err(netdev,
> + "invalid DLC in CAN error frame (can_dlc=%u, exptect_can_dlc=%u)\n",
> + header->can_dlc, CAN_ERR_DLC);
> +
> + valid = false;
> + }
[Severity: Medium]
Can dropping the whole frame here lose controller state transitions?
When this check fails, gs_usb_get_rx_data_length() returns -EINVAL, and
gs_usb_receive_bulk_callback() jumps straight to resubmit_urb. That also
skips the gs_update_state() call:
drivers/net/can/usb/gs_usb.c:gs_usb_receive_bulk_callback() {
...
data_length = gs_usb_get_rx_data_length(dev, hf, urb->actual_length);
if (data_length < 0)
goto resubmit_urb;
...
/* ERROR frames tell us information about the controller */
if (le32_to_cpu(hf->can_id) & CAN_ERR_FLAG)
gs_update_state(dev, cf);
...
}
gs_update_state() is the only runtime code that updates dev->can.state and
the bus_off, restarts, error_warning and error_passive counters. Its
bus-off and restart branches look only at cf->can_id:
drivers/net/can/usb/gs_usb.c:gs_update_state() {
if (cf->can_id & CAN_ERR_RESTARTED) {
dev->can.state = CAN_STATE_ERROR_ACTIVE;
can_stats->restarts++;
} else if (cf->can_id & CAN_ERR_BUSOFF) {
dev->can.state = CAN_STATE_BUS_OFF;
can_stats->bus_off++;
...
}
A bus-off or restart frame with a short DLC carried everything the driver
needed, but it is now discarded. If firmware sends such a frame, would
dev->can.state stay ERROR_ACTIVE or ERROR_WARNING while the controller is
actually bus-off? User space would also never see the event.
Nothing appears to correct the state later. gs_usb_get_state() is only
reached through do_get_berr_counter, the driver sets no do_get_state, and
gs_usb_can_get_berr_counter() ignores the polled state.
Short frames were not a memory safety problem before this patch.
alloc_can_skb() zeroes the can_frame and memcpy() copies only data_length
bytes, so data[1] reads as 0.
Would it be better to repair the frame instead? Setting len to CAN_ERR_DLC
and clearing CAN_RTR_FLAG, with a rate limited warning, would give user
space well formed frames without losing the state transition. The same
applies to the RTR check below.
It is not confirmed that any deployed gs_usb firmware (candleLight, CANtact
Pro, cannectivity, the original Geschwister Schneider firmware) sends error
frames with a DLC other than 8. The commit message also does not say that
state changes carried only in can_id are dropped as well.
> +
> + if (header->can_id & cpu_to_le32(CAN_RTR_FLAG)) {
> + stats->rx_errors++;
[Severity: Low]
Is a single frame counted twice in rx_errors here?
The DLC check above and this RTR check are independent, and neither
returns. An RX error frame with CAN_ERR_FLAG | CAN_RTR_FLAG and can_dlc != 8
therefore adds 2 to rx_errors. include/uapi/linux/if_link.h describes
rx_errors as the "Total number of bad packets received on this network
device".
The next patch in the series ("can: gs_usb:
gs_usb_host_frame_header_valid(): drop CAN frames with invalid DLC") adds a
third independent block for can_dlc > CAN_MAX_RAW_DLC. That block also
increments rx_errors and rx_length_errors. At the end of the series, an
error frame with can_dlc > 15 and RTR set would add 3 to rx_errors and 2 to
rx_length_errors.
Could the counter be incremented once per rejected frame instead? For
example, gs_usb_get_rx_data_length() could do it when
gs_usb_host_frame_header_valid() returns false.
> +
> + if (net_ratelimit())
> + netdev_err(netdev,
> + "invalid RTR flag in CAN error frame\n");
> +
> + valid = false;
> + }
> + }
[Severity: Low]
Should these checks also reject error frames that have GS_CAN_FLAG_FD set
in header->flags?
An RX error frame with GS_CAN_FLAG_FD (and optionally BRS or ESI) set and
can_dlc == 8 still passes here. gs_usb_receive_bulk_callback() then takes
the FD branch:
drivers/net/can/usb/gs_usb.c:gs_usb_receive_bulk_callback() {
...
if (hf->flags & GS_CAN_FLAG_FD) {
skb = alloc_canfd_skb(netdev, &cfd);
...
cfd->can_id = le32_to_cpu(hf->can_id);
cfd->len = data_length;
if (hf->flags & GS_CAN_FLAG_BRS)
cfd->flags |= CANFD_BRS;
if (hf->flags & GS_CAN_FLAG_ESI)
cfd->flags |= CANFD_ESI;
...
}
The resulting canfd_frame keeps CAN_ERR_FLAG. gs_update_state() is only
called in the classic branch, so the driver state ignores this frame.
can_rcv_filter() in net/can/af_can.c delivers any frame with CAN_ERR_FLAG
to RX_ERR subscribers. Sockets with CAN_RAW_FD_FRAMES enabled would
therefore receive the error frame as a struct canfd_frame.
SocketCAN error frames are always classic struct can_frame. This looks
like another kind of invalid error frame that, going by its subject, the
patch is meant to drop. The behaviour predates this patch, but it falls
within what the patch sets out to cover.
> +
> + return valid;
> }
[Severity: Low]
When the new checks reject an RX error frame,
gs_usb_receive_bulk_callback() goes straight to resubmit_urb and skips the
overflow handling:
drivers/net/can/usb/gs_usb.c:gs_usb_receive_bulk_callback() {
...
check_overflow:
if (hf->flags & GS_CAN_FLAG_OVERFLOW) {
stats->rx_over_errors++;
stats->rx_errors++;
...
}
Is it intended that a GS_CAN_FLAG_OVERFLOW bit in a rejected error frame
is lost? In that case rx_over_errors is not counted and no
CAN_ERR_CRTL_RX_OVERFLOW error frame is generated.
The flags byte does not depend on can_dlc or the RTR bit, and
gs_usb_get_gs_can() has already confirmed that the full header was
received.
Earlier validation failures (out of range echo_id, short read) already
skip overflow handling this way. This patch adds new cases that reach the
same path.
--
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261122-gs_usb-fixes-v10-0-7c165c35d04a%40pengutronix.de
^ permalink raw reply [flat|nested] 24+ messages in thread
* Re: [PATCH can-next v10 14/14] can: gs_usb: gs_usb_host_frame_header_valid(): drop CAN frames with invalid DLC
2026-11-22 18:48 ` [PATCH can-next v10 14/14] can: gs_usb: gs_usb_host_frame_header_valid(): drop CAN frames with invalid DLC Marc Kleine-Budde
@ 2026-11-24 9:31 ` netdev-bot+sashiko
0 siblings, 0 replies; 24+ messages in thread
From: netdev-bot+sashiko @ 2026-11-24 9:31 UTC (permalink / raw)
To: mkl; +Cc: mailhol, pfink, eric, max, wg, kernel, linux-can, stable, kuba
Thank you for your contribution! Sashiko AI review found 2 potential
issue(s) to consider.
Critical: 0 · High: 0 · Medium: 1 · Low: 1
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] In gs_usb_host_frame_header_valid()
(drivers/net/can/usb/gs_usb.c), an RX CAN error frame (CAN_ERR_FLAG…
- [Low] The commit "can: gs_usb: gs_usb_host_frame_header_valid(): drop
CAN frames with invalid DLC" fixes a user-visible bug that comes from…
--
can: gs_usb: gs_usb_host_frame_header_valid(): drop CAN frames with invalid DLC
This commit adds a check to gs_usb_host_frame_header_valid() that drops
received CAN frames with a DLC above 0xf. It updates the rx error
counters, logs a rate-limited error and resubmits the URB.
Closes: https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261004-gs_usb-cleanups-and-fixes-v1-0-7b4b06244a77@pengutronix.de
> According to the respective CAN standards, the maximum DLC length for
> CAN-CC and CAN-FD frames is 0xf. The code in driver assumes that this limit
> has been verified.
>
> Drop CAN frames with DLC > 0xf, print a error message and resubmit the URB.
[Severity: Low]
Should this patch have a Fixes: tag and Cc: stable? The commit message
also doesn't say what goes wrong without the check.
Before this patch, gs_usb_receive_bulk_callback() passed the raw device
DLC to the helper unchecked:
can_frame_set_cc_len(cf, hf->can_dlc, dev->can.ctrlmode);
The helper in include/linux/can/length.h assumes the range has already
been checked:
/* the caller already ensured that dlc is a value from 0 .. 15 */
if (ctrlmode & CAN_CTRLMODE_CC_LEN8_DLC && dlc > CAN_MAX_DLEN)
cf->len8_dlc = dlc;
With CAN_CTRLMODE_CC_LEN8_DLC enabled, a USB device sending can_dlc
0x10..0xff gives userspace a struct can_frame with len8_dlc above
CAN_MAX_RAW_DLC. For CAN-FD, can_fd_dlc2len() masks the DLC with 0x0F, so
a DLC of 0x1X is accepted with a different payload length.
gs_make_candev() has advertised CAN_CTRLMODE_CC_LEN8_DLC since commit
4c01fc87675e ("can: drivers: add len8_dlc support for various CAN
adapters"). That commit looks like a reasonable Fixes: target.
Other gs_usb fixes in the same series carry Fixes: and Cc: stable, for
example "can: gs_usb: gs_usb_receive_bulk_callback(): add missing call to
can_rx_offload_irq_finish". Could this one get the same tags, plus a
sentence on the user-visible effect?
> diff --git a/drivers/net/can/usb/gs_usb.c b/drivers/net/can/usb/gs_usb.c
> index 7229d002b938a..5e8ea308fabf5 100644
> --- a/drivers/net/can/usb/gs_usb.c
> +++ b/drivers/net/can/usb/gs_usb.c
> @@ -668,6 +668,20 @@ static bool gs_usb_host_frame_header_valid(const struct gs_can *dev,
> }
> }
>
> + /* we can check for max CAN-CC and CAN-FD DLC at the same time, they are both 0xf */
> + BUILD_BUG_ON(CAN_MAX_RAW_DLC != CANFD_MAX_DLC);
> + if (header->can_dlc > CAN_MAX_RAW_DLC) {
> + stats->rx_errors++;
> + stats->rx_length_errors++;
[Severity: Medium]
Can a single error frame be counted twice here?
Take an RX frame with CAN_ERR_FLAG set in can_id and can_dlc in 16..255.
The existing error-frame block earlier in
gs_usb_host_frame_header_valid() already counts it and keeps going
instead of returning:
if (header->can_id & cpu_to_le32(CAN_ERR_FLAG)) {
if (header->can_dlc != CAN_ERR_DLC) {
stats->rx_errors++;
stats->rx_length_errors++;
...
valid = false;
}
This new check then runs for the same frame and increments rx_errors and
rx_length_errors again. It also logs a second error message.
gs_usb_get_rx_data_length() drops exactly one frame here, but both
counters go up by 2. If CAN_RTR_FLAG is also set, rx_errors goes up
by 3.
include/uapi/linux/if_link.h defines rx_length_errors as "Number of
packets dropped due to invalid length", so this would over-count.
Would it work to make the new range check an else branch of the
error-frame DLC check? Another option is to update the counters once at
the end of the function when valid is false.
> +
> + if (net_ratelimit())
> + netdev_err(netdev,
> + "out of range DLC (can_dlc=%u, max_can_dlc=%u)\n",
> + header->can_dlc, CAN_MAX_RAW_DLC);
> +
> + valid = false;
> + }
> +
> return valid;
> }
--
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261122-gs_usb-fixes-v10-0-7c165c35d04a%40pengutronix.de
^ permalink raw reply [flat|nested] 24+ messages in thread
end of thread, other threads:[~2026-11-24 9:31 UTC | newest]
Thread overview: 24+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-22 18:48 [PATCH can-next v10 00/14] gs_usb: fixes Marc Kleine-Budde
2026-11-22 18:48 ` [PATCH can-next v10 01/14] can: gs_usb: gs_usb_probe(): init netdev completely before register_candev() Marc Kleine-Budde
2026-11-22 18:48 ` [PATCH can-next v10 02/14] can: gs_usb: gs_usb_receive_bulk_callback(): check for overflow flag if SKB allocation fails Marc Kleine-Budde
2026-11-24 9:30 ` netdev-bot+sashiko
2026-11-22 18:48 ` [PATCH can-next v10 03/14] can: gs_usb: gs_usb_receive_bulk_callback(): add missing call to can_rx_offload_irq_finish Marc Kleine-Budde
2026-11-22 18:48 ` [PATCH can-next v10 04/14] can: gs_usb: use spin_lock_irqsave() instead of spin_lock_bh() to protect struct gs_usb::tc Marc Kleine-Budde
2026-11-24 9:30 ` netdev-bot+sashiko
2026-11-22 18:48 ` [PATCH can-next v10 05/14] can: gs_usb: fix hardware timestamp state for mixed channels Marc Kleine-Budde
2026-11-24 9:30 ` netdev-bot+sashiko
2026-11-22 18:48 ` [PATCH can-next v10 06/14] can: gs_usb: add support for context analysis Marc Kleine-Budde
2026-11-22 18:48 ` [PATCH can-next v10 07/14] can: gs_usb: gs_make_candev(): check can_rx_offload_add_manual() return value Marc Kleine-Budde
2026-11-22 18:48 ` [PATCH can-next v10 08/14] can: gs_usb: gs_usb_receive_bulk_callback(): use dev_info() to print re-submit info messages Marc Kleine-Budde
2026-11-24 9:30 ` netdev-bot+sashiko
2026-11-22 18:48 ` [PATCH can-next v10 09/14] can: gs_usb: gs_usb_get_gs_can(): introduce new function to consolidate validation of struct gs_can Marc Kleine-Budde
2026-11-24 9:30 ` netdev-bot+sashiko
2026-11-22 18:48 ` [PATCH can-next v10 10/14] can: gs_usb: gs_usb_get_gs_can(): report channel number out of range error Marc Kleine-Budde
2026-11-22 18:48 ` [PATCH can-next v10 11/14] can: gs_usb: gs_usb_get_rx_data_length(): new function to consolidate validation of host frame Marc Kleine-Budde
2026-11-24 9:31 ` netdev-bot+sashiko
2026-11-22 18:48 ` [PATCH can-next v10 12/14] can: gs_usb: gs_can_start_xmit(): mask invalid parts of CAN ID before sending Marc Kleine-Budde
2026-11-24 9:31 ` netdev-bot+sashiko
2026-11-22 18:48 ` [PATCH can-next v10 13/14] can: gs_usb: gs_usb_host_frame_header_valid(): drop invalid CAN error frames Marc Kleine-Budde
2026-11-24 9:31 ` netdev-bot+sashiko
2026-11-22 18:48 ` [PATCH can-next v10 14/14] can: gs_usb: gs_usb_host_frame_header_valid(): drop CAN frames with invalid DLC Marc Kleine-Budde
2026-11-24 9:31 ` 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