* [PATCH can-next v9 01/13] can: gs_usb: gs_usb_probe(): init netdev completely before register_candev()
2026-11-21 22:35 [PATCH can-next v9 00/13] gs_usb: fixes Marc Kleine-Budde
@ 2026-11-21 22:35 ` Marc Kleine-Budde
2026-11-21 22:36 ` [PATCH can-next v9 02/13] can: gs_usb: gs_usb_receive_bulk_callback(): check for overflow flag if SKB allocation fails Marc Kleine-Budde
` (11 subsequent siblings)
12 siblings, 0 replies; 28+ messages in thread
From: Marc Kleine-Budde @ 2026-11-21 22:35 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] 28+ messages in thread* [PATCH can-next v9 02/13] can: gs_usb: gs_usb_receive_bulk_callback(): check for overflow flag if SKB allocation fails
2026-11-21 22:35 [PATCH can-next v9 00/13] gs_usb: fixes Marc Kleine-Budde
2026-11-21 22:35 ` [PATCH can-next v9 01/13] can: gs_usb: gs_usb_probe(): init netdev completely before register_candev() Marc Kleine-Budde
@ 2026-11-21 22:36 ` Marc Kleine-Budde
2026-11-21 22:36 ` [PATCH can-next v9 03/13] can: gs_usb: gs_usb_receive_bulk_callback(): add missing call to can_rx_offload_irq_finish Marc Kleine-Budde
` (10 subsequent siblings)
12 siblings, 0 replies; 28+ messages in thread
From: Marc Kleine-Budde @ 2026-11-21 22:36 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] 28+ messages in thread* [PATCH can-next v9 03/13] can: gs_usb: gs_usb_receive_bulk_callback(): add missing call to can_rx_offload_irq_finish
2026-11-21 22:35 [PATCH can-next v9 00/13] gs_usb: fixes Marc Kleine-Budde
2026-11-21 22:35 ` [PATCH can-next v9 01/13] can: gs_usb: gs_usb_probe(): init netdev completely before register_candev() Marc Kleine-Budde
2026-11-21 22:36 ` [PATCH can-next v9 02/13] can: gs_usb: gs_usb_receive_bulk_callback(): check for overflow flag if SKB allocation fails Marc Kleine-Budde
@ 2026-11-21 22:36 ` Marc Kleine-Budde
2026-11-21 22:36 ` [PATCH can-next v9 04/13] can: gs_usb: use spin_lock_irqsave() instead of spin_lock_bh() to protect struct gs_usb::tc Marc Kleine-Budde
` (9 subsequent siblings)
12 siblings, 0 replies; 28+ messages in thread
From: Marc Kleine-Budde @ 2026-11-21 22:36 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] 28+ messages in thread* [PATCH can-next v9 04/13] can: gs_usb: use spin_lock_irqsave() instead of spin_lock_bh() to protect struct gs_usb::tc
2026-11-21 22:35 [PATCH can-next v9 00/13] gs_usb: fixes Marc Kleine-Budde
` (2 preceding siblings ...)
2026-11-21 22:36 ` [PATCH can-next v9 03/13] can: gs_usb: gs_usb_receive_bulk_callback(): add missing call to can_rx_offload_irq_finish Marc Kleine-Budde
@ 2026-11-21 22:36 ` Marc Kleine-Budde
2026-11-21 22:36 ` [PATCH can-next v9 05/13] can: gs_usb: fix hardware timestamp state for mixed channels Marc Kleine-Budde
` (8 subsequent siblings)
12 siblings, 0 replies; 28+ messages in thread
From: Marc Kleine-Budde @ 2026-11-21 22:36 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] 28+ messages in thread* [PATCH can-next v9 05/13] can: gs_usb: fix hardware timestamp state for mixed channels
2026-11-21 22:35 [PATCH can-next v9 00/13] gs_usb: fixes Marc Kleine-Budde
` (3 preceding siblings ...)
2026-11-21 22:36 ` [PATCH can-next v9 04/13] can: gs_usb: use spin_lock_irqsave() instead of spin_lock_bh() to protect struct gs_usb::tc Marc Kleine-Budde
@ 2026-11-21 22:36 ` Marc Kleine-Budde
2026-11-21 22:43 ` sashiko-bot
2026-11-23 12:00 ` netdev-bot+sashiko
2026-11-21 22:36 ` [PATCH can-next v9 06/13] can: gs_usb: add support for context analysis Marc Kleine-Budde
` (7 subsequent siblings)
12 siblings, 2 replies; 28+ messages in thread
From: Marc Kleine-Budde @ 2026-11-21 22:36 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..30bcb734c5df 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] 28+ messages in thread* Re: [PATCH can-next v9 05/13] can: gs_usb: fix hardware timestamp state for mixed channels
2026-11-21 22:36 ` [PATCH can-next v9 05/13] can: gs_usb: fix hardware timestamp state for mixed channels Marc Kleine-Budde
@ 2026-11-21 22:43 ` sashiko-bot
2026-11-23 12:00 ` netdev-bot+sashiko
1 sibling, 0 replies; 28+ messages in thread
From: sashiko-bot @ 2026-11-21 22:43 UTC (permalink / raw)
To: Marc Kleine-Budde
Cc: linux-can, Oliver Hartkopp, Vincent Mailhol, Oleksij Rempel
Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [High] can: gs_usb: unmatched opening brace in gs_can_open() breaks
build
--
commit 5f884365b4de3f2bf3d74e69451cce317c14e008
Author: Shuangpeng Bai <shuangpeng.kernel@gmail.com>
can: gs_usb: fix hardware timestamp state for mixed channels
This commit moves hardware timestamp initialization to gs_usb_probe() when
any channel supports hardware timestamping, and starts or stops the
timecounter when the first channel opens or the last channel closes.
> diff --git a/drivers/net/can/usb/gs_usb.c b/drivers/net/can/usb/gs_usb.c
> index 470dd52d97dfe..30bcb734c5df8 100644
> --- a/drivers/net/can/usb/gs_usb.c
> +++ b/drivers/net/can/usb/gs_usb.c
[ ... ]
> @@ -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;
> + }
> + }
[Severity: High]
Does the unmatched opening brace on "if (rc == -ENODEV) {" break the
build? The stray brace is removed in the follow-up commit "can: gs_usb:
add support for context analysis", leaving this commit unbuildable for
bisection and stable backports.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261121-gs_usb-fixes-v9-0-db121d488945@pengutronix.de?part=5
^ permalink raw reply [flat|nested] 28+ messages in thread* Re: [PATCH can-next v9 05/13] can: gs_usb: fix hardware timestamp state for mixed channels
2026-11-21 22:36 ` [PATCH can-next v9 05/13] can: gs_usb: fix hardware timestamp state for mixed channels Marc Kleine-Budde
2026-11-21 22:43 ` sashiko-bot
@ 2026-11-23 12:00 ` netdev-bot+sashiko
1 sibling, 0 replies; 28+ messages in thread
From: netdev-bot+sashiko @ 2026-11-23 12:00 UTC (permalink / raw)
To: mkl
Cc: mailhol, pfink, eric, max, wg, kernel, linux-can,
shuangpeng.kernel, stable, vadim.fedorenko, 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 drivers/net/can/usb/gs_usb.c, gs_can_open() has an
unbalanced brace in the new gs_usb_timestamp_start() error handling,…
--
can: gs_usb: fix hardware timestamp state for mixed channels
The HW timestamp infrastructure is now set up in gs_usb_probe() when any
channel advertises GS_CAN_FEATURE_HW_TIMESTAMP. Initial time sync and the
scheduling of the delayed worker move into a new gs_usb_timestamp_start(),
which runs when the first channel is opened. Shutdown is keyed off a new
per-parent flag instead of the feature bits of the last channel closed.
> diff --git a/drivers/net/can/usb/gs_usb.c b/drivers/net/can/usb/gs_usb.c
> index 470dd52d97dfe..30bcb734c5df8 100644
> --- a/drivers/net/can/usb/gs_usb.c
> +++ b/drivers/net/can/usb/gs_usb.c
[ ... ]
> @@ -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) {
^
[Severity: Medium]
Is there a stray opening brace here? Nothing closes it, so the two closing
braces below end the -ENODEV block and the if (rc) block. The
if (parent->flags & GS_USB_PARENT_FLAGS_HW_TIMESTAMP) block is left open.
Every later closing brace in gs_can_open() is then off by one. The
function's final brace after the out_can_rx_offload_disable label no
longer ends gs_can_open(), so the next definition is parsed as a nested
function:
static int gs_usb_get_state(const struct net_device *netdev,
That doesn't compile, so drivers/net/can/usb/gs_usb.c can't be built at
this commit and bisection through it breaks.
The goto out_can_rx_offload_disable also ends up inside the -ENODEV block.
The commit message says a failed initial timestamp read should make the
open fail. The usb_submit_urb() error handling in the RX URB loop follows
that pattern, detaching only on -ENODEV but always taking the goto:
rc = usb_submit_urb(urb, GFP_KERNEL);
if (rc) {
if (rc == -ENODEV)
netif_device_detach(dev->netdev);
...
goto out_usb_unanchor_urb;
}
Should this be if (rc == -ENODEV) without the brace?
The next patch in the series, "can: gs_usb: add support for context
analysis", removes this brace, but that patch isn't tagged for stable.
This patch has Cc: stable@vger.kernel.org. Would backporting it on its
own give stable trees a driver that doesn't build?
> + 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);
>
[ ... ]
--
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261121-gs_usb-fixes-v9-0-db121d488945%40pengutronix.de
^ permalink raw reply [flat|nested] 28+ messages in thread
* [PATCH can-next v9 06/13] can: gs_usb: add support for context analysis
2026-11-21 22:35 [PATCH can-next v9 00/13] gs_usb: fixes Marc Kleine-Budde
` (4 preceding siblings ...)
2026-11-21 22:36 ` [PATCH can-next v9 05/13] can: gs_usb: fix hardware timestamp state for mixed channels Marc Kleine-Budde
@ 2026-11-21 22:36 ` Marc Kleine-Budde
2026-11-21 22:42 ` sashiko-bot
2026-11-23 12:00 ` netdev-bot+sashiko
2026-11-21 22:36 ` [PATCH can-next v9 07/13] can: gs_usb: gs_make_candev(): check can_rx_offload_add_manual() return value Marc Kleine-Budde
` (6 subsequent siblings)
12 siblings, 2 replies; 28+ messages in thread
From: Marc Kleine-Budde @ 2026-11-21 22:36 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 | 13 ++++++++-----
2 files changed, 10 insertions(+), 5 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 30bcb734c5df..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;
@@ -1037,7 +1040,7 @@ static int gs_can_open(struct net_device *netdev)
if (parent->flags & GS_USB_PARENT_FLAGS_HW_TIMESTAMP) {
rc = gs_usb_timestamp_start(parent);
if (rc) {
- if (rc == -ENODEV) {
+ if (rc == -ENODEV)
netif_device_detach(netdev);
goto out_can_rx_offload_disable;
--
2.53.0
^ permalink raw reply related [flat|nested] 28+ messages in thread* Re: [PATCH can-next v9 06/13] can: gs_usb: add support for context analysis
2026-11-21 22:36 ` [PATCH can-next v9 06/13] can: gs_usb: add support for context analysis Marc Kleine-Budde
@ 2026-11-21 22:42 ` sashiko-bot
2026-11-23 12:00 ` netdev-bot+sashiko
1 sibling, 0 replies; 28+ messages in thread
From: sashiko-bot @ 2026-11-21 22:42 UTC (permalink / raw)
To: Marc Kleine-Budde
Cc: linux-can, Oliver Hartkopp, Vincent Mailhol, Oleksij Rempel
Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Medium] can: gs_usb: misplaced brace fixup in gs_can_open() breaks
bisection
--
commit c9d422d27a7b8018d144a3f7d0351ca265fd88bd
Author: Marc Kleine-Budde <mkl@pengutronix.de>
can: gs_usb: add support for context analysis
This commit enables context analysis for the gs_usb driver and annotates
the hardware timestamp counter fields and helpers with tc_lock locking
requirements.
> diff --git a/drivers/net/can/usb/gs_usb.c b/drivers/net/can/usb/gs_usb.c
> index 30bcb734c5df8..ab82dc07f3f50 100644
> --- a/drivers/net/can/usb/gs_usb.c
> +++ b/drivers/net/can/usb/gs_usb.c
[ ... ]
> @@ -1037,7 +1040,7 @@ static int gs_can_open(struct net_device *netdev)
> if (parent->flags & GS_USB_PARENT_FLAGS_HW_TIMESTAMP) {
> rc = gs_usb_timestamp_start(parent);
> if (rc) {
> - if (rc == -ENODEV) {
> + if (rc == -ENODEV)
> netif_device_detach(netdev);
>
> goto out_can_rx_offload_disable;
[Severity: Medium]
Should this stray opening brace removal be squashed into the preceding
commit "can: gs_usb: fix hardware timestamp state for mixed channels"?
Leaving the unmatched brace in the earlier commit prevents gs_usb.c from
compiling there, which breaks bisection and standalone stable backports of
that fix.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261121-gs_usb-fixes-v9-0-db121d488945@pengutronix.de?part=6
^ permalink raw reply [flat|nested] 28+ messages in thread* Re: [PATCH can-next v9 06/13] can: gs_usb: add support for context analysis
2026-11-21 22:36 ` [PATCH can-next v9 06/13] can: gs_usb: add support for context analysis Marc Kleine-Budde
2026-11-21 22:42 ` sashiko-bot
@ 2026-11-23 12:00 ` netdev-bot+sashiko
1 sibling, 0 replies; 28+ messages in thread
From: netdev-bot+sashiko @ 2026-11-23 12:00 UTC (permalink / raw)
To: mkl
Cc: mailhol, pfink, eric, max, wg, kernel, linux-can,
shuangpeng.kernel, stable, vadim.fedorenko, 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] The commit message does not mention one of the changes.
--
can: gs_usb: add support for context analysis
This commit turns on context analysis for gs_usb.o. It marks the
cyclecounter, timecounter and timestamp_initial fields in struct gs_usb
as guarded by tc_lock. It also adds or fixes the __must_hold and
__must_not_hold annotations on the low level timestamp read helpers.
> diff --git a/drivers/net/can/usb/gs_usb.c b/drivers/net/can/usb/gs_usb.c
> index 30bcb734c5df8..ab82dc07f3f50 100644
> --- a/drivers/net/can/usb/gs_usb.c
> +++ b/drivers/net/can/usb/gs_usb.c
[ ... ]
> @@ -1037,7 +1040,7 @@ static int gs_can_open(struct net_device *netdev)
> if (parent->flags & GS_USB_PARENT_FLAGS_HW_TIMESTAMP) {
> rc = gs_usb_timestamp_start(parent);
> if (rc) {
> - if (rc == -ENODEV) {
> + if (rc == -ENODEV)
> netif_device_detach(netdev);
>
> goto out_can_rx_offload_disable;
[Severity: Medium]
This hunk in gs_can_open() is a build fix, but the commit message
doesn't mention it. Should it be part of the previous patch instead?
The message only describes annotation changes:
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.
At the parent commit, "can: gs_usb: fix hardware timestamp state for
mixed channels", gs_can_open() has an opening brace that is never
closed:
if (!parent->active_channels) {
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;
}
}
Because of that brace, gs_usb.c doesn't compile at that commit. A
bisect that lands there will hit the build error.
That parent commit has Fixes: 5886e4d5ecec and Cc: stable. Its message
also says:
To simplify backporting, support for context analysis is added in a later
patch.
This patch has no Fixes: or stable tag. If the parent is backported on
its own, as its message suggests, would the stable gs_usb build break?
Could the brace fix go into "can: gs_usb: fix hardware timestamp state
for mixed channels"? If it stays here, could the commit message at least
say that it fixes a build error from the previous patch?
--
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261121-gs_usb-fixes-v9-0-db121d488945%40pengutronix.de
^ permalink raw reply [flat|nested] 28+ messages in thread
* [PATCH can-next v9 07/13] can: gs_usb: gs_make_candev(): check can_rx_offload_add_manual() return value
2026-11-21 22:35 [PATCH can-next v9 00/13] gs_usb: fixes Marc Kleine-Budde
` (5 preceding siblings ...)
2026-11-21 22:36 ` [PATCH can-next v9 06/13] can: gs_usb: add support for context analysis Marc Kleine-Budde
@ 2026-11-21 22:36 ` Marc Kleine-Budde
2026-11-21 22:36 ` [PATCH can-next v9 08/13] can: gs_usb: gs_usb_receive_bulk_callback(): use dev_info() to print re-submit info messages Marc Kleine-Budde
` (5 subsequent siblings)
12 siblings, 0 replies; 28+ messages in thread
From: Marc Kleine-Budde @ 2026-11-21 22:36 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] 28+ messages in thread* [PATCH can-next v9 08/13] can: gs_usb: gs_usb_receive_bulk_callback(): use dev_info() to print re-submit info messages
2026-11-21 22:35 [PATCH can-next v9 00/13] gs_usb: fixes Marc Kleine-Budde
` (6 preceding siblings ...)
2026-11-21 22:36 ` [PATCH can-next v9 07/13] can: gs_usb: gs_make_candev(): check can_rx_offload_add_manual() return value Marc Kleine-Budde
@ 2026-11-21 22:36 ` Marc Kleine-Budde
2026-11-21 22:42 ` sashiko-bot
2026-11-23 12:00 ` netdev-bot+sashiko
2026-11-21 22:36 ` [PATCH can-next v9 09/13] can: gs_usb: gs_usb_get_gs_can(): introduce new function to consolidate validation of struct gs_can Marc Kleine-Budde
` (4 subsequent siblings)
12 siblings, 2 replies; 28+ messages in thread
From: Marc Kleine-Budde @ 2026-11-21 22:36 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 | 7 ++++---
1 file changed, 4 insertions(+), 3 deletions(-)
diff --git a/drivers/net/can/usb/gs_usb.c b/drivers/net/can/usb/gs_usb.c
index 308daa2645ad..f49179cbd787 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;
@@ -820,8 +820,9 @@ static void gs_usb_receive_bulk_callback(struct urb *urb)
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));
+ 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] 28+ messages in thread* Re: [PATCH can-next v9 08/13] can: gs_usb: gs_usb_receive_bulk_callback(): use dev_info() to print re-submit info messages
2026-11-21 22:36 ` [PATCH can-next v9 08/13] can: gs_usb: gs_usb_receive_bulk_callback(): use dev_info() to print re-submit info messages Marc Kleine-Budde
@ 2026-11-21 22:42 ` sashiko-bot
2026-11-23 12:00 ` netdev-bot+sashiko
1 sibling, 0 replies; 28+ messages in thread
From: sashiko-bot @ 2026-11-21 22:42 UTC (permalink / raw)
To: Marc Kleine-Budde
Cc: linux-can, Oliver Hartkopp, Vincent Mailhol, Oleksij Rempel
Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Low] can: gs_usb: redundant double rate limiting in
gs_usb_receive_bulk_callback()
--
commit 6963c253f83fff9fd057f286e4568b102a31d71f
Author: Marc Kleine-Budde <mkl@pengutronix.de>
can: gs_usb: gs_usb_receive_bulk_callback(): use dev_info() to print re-submit info messages
This commit replaces netdev_info() with dev_info_ratelimited() when
logging IN URB resubmission failures in gs_usb_receive_bulk_callback() so
that the affected USB device is reported even when netdev has not been
identified yet.
> diff --git a/drivers/net/can/usb/gs_usb.c b/drivers/net/can/usb/gs_usb.c
> index 308daa2645ada..f49179cbd7876 100644
> --- a/drivers/net/can/usb/gs_usb.c
> +++ b/drivers/net/can/usb/gs_usb.c
[ ... ]
> @@ -820,8 +820,9 @@ static void gs_usb_receive_bulk_callback(struct urb *urb)
> 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));
> + dev_info_ratelimited(&parent->udev->dev,
> + "failed to re-submit IN URB: %pe\n",
> + ERR_PTR(rc));
> }
> }
[Severity: Low]
Should net_ratelimit() be dropped from the condition now that
dev_info_ratelimited() is used? Keeping both applies two independent
rate limiters and consumes tokens from the global net_ratelimit_state.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261121-gs_usb-fixes-v9-0-db121d488945@pengutronix.de?part=8
^ permalink raw reply [flat|nested] 28+ messages in thread* Re: [PATCH can-next v9 08/13] can: gs_usb: gs_usb_receive_bulk_callback(): use dev_info() to print re-submit info messages
2026-11-21 22:36 ` [PATCH can-next v9 08/13] can: gs_usb: gs_usb_receive_bulk_callback(): use dev_info() to print re-submit info messages Marc Kleine-Budde
2026-11-21 22:42 ` sashiko-bot
@ 2026-11-23 12:00 ` netdev-bot+sashiko
1 sibling, 0 replies; 28+ messages in thread
From: netdev-bot+sashiko @ 2026-11-23 12:00 UTC (permalink / raw)
To: mkl
Cc: mailhol, pfink, eric, max, wg, kernel, linux-can,
shuangpeng.kernel, stable, vadim.fedorenko, 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] In gs_usb_receive_bulk_callback() (drivers/net/can/usb/gs_usb.c),
the patch replaces netdev_info() with dev_info_ratelimited() but…
--
can: gs_usb: gs_usb_receive_bulk_callback(): use dev_info() to print re-submit info messages
When an IN URB fails to re-submit before the network device is known,
gs_usb_receive_bulk_callback() used to call netdev_info() with a NULL
netdev. This patch logs against the USB device with
dev_info_ratelimited() instead. It also removes the NULL initialization
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..f49179cbd7876 100644
> --- a/drivers/net/can/usb/gs_usb.c
> +++ b/drivers/net/can/usb/gs_usb.c
[ ... ]
> @@ -820,8 +820,9 @@ static void gs_usb_receive_bulk_callback(struct urb *urb)
> 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));
> + dev_info_ratelimited(&parent->udev->dev,
> + "failed to re-submit IN URB: %pe\n",
> + ERR_PTR(rc));
[Severity: Low]
Is the net_ratelimit() check still needed here?
dev_info_ratelimited() already has its own per-callsite limiter through
dev_level_ratelimited() in include/linux/dev_printk.h:
static DEFINE_RATELIMIT_STATE(_rs, DEFAULT_RATELIMIT_INTERVAL,
DEFAULT_RATELIMIT_BURST);
if (__ratelimit(&_rs))
dev_level(dev, fmt, ##__VA_ARGS__);
That means this message now goes through two rate limiters. If
net_ratelimit() returns true but the local limiter suppresses the print,
a token from the global networking budget is used up and nothing gets
printed.
The short-read message earlier in the same function also logs against
the USB device, and it uses only the dev_*_ratelimited() helper:
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);
Should this site drop the "&& net_ratelimit()" part to match?
The commit message also doesn't quite match the code:
"and also rate limit the number of messages to prevent log flooding."
net_ratelimit() was already limiting this message before the patch, so
the patch doesn't add rate limiting here. The subject says "use
dev_info()", but the code uses dev_info_ratelimited().
The duplicate limiting is also still present at the end of the series.
> }
> }
>
--
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261121-gs_usb-fixes-v9-0-db121d488945%40pengutronix.de
^ permalink raw reply [flat|nested] 28+ messages in thread
* [PATCH can-next v9 09/13] can: gs_usb: gs_usb_get_gs_can(): introduce new function to consolidate validation of struct gs_can
2026-11-21 22:35 [PATCH can-next v9 00/13] gs_usb: fixes Marc Kleine-Budde
` (7 preceding siblings ...)
2026-11-21 22:36 ` [PATCH can-next v9 08/13] can: gs_usb: gs_usb_receive_bulk_callback(): use dev_info() to print re-submit info messages Marc Kleine-Budde
@ 2026-11-21 22:36 ` Marc Kleine-Budde
2026-11-23 12:00 ` netdev-bot+sashiko
2026-11-21 22:36 ` [PATCH can-next v9 10/13] can: gs_usb: gs_usb_get_gs_can(): report channel number out of range error Marc Kleine-Budde
` (3 subsequent siblings)
12 siblings, 1 reply; 28+ messages in thread
From: Marc Kleine-Budde @ 2026-11-21 22:36 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 f49179cbd787..c392cf65d8a6 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] 28+ messages in thread* Re: [PATCH can-next v9 09/13] can: gs_usb: gs_usb_get_gs_can(): introduce new function to consolidate validation of struct gs_can
2026-11-21 22:36 ` [PATCH can-next v9 09/13] can: gs_usb: gs_usb_get_gs_can(): introduce new function to consolidate validation of struct gs_can Marc Kleine-Budde
@ 2026-11-23 12:00 ` netdev-bot+sashiko
0 siblings, 0 replies; 28+ messages in thread
From: netdev-bot+sashiko @ 2026-11-23 12:00 UTC (permalink / raw)
To: mkl
Cc: mailhol, pfink, eric, max, wg, kernel, linux-can,
shuangpeng.kernel, stable, vadim.fedorenko, 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 presents this patch as a pure refactor ("To
improve code readability and structure consolidate the decoding and…
--
can: gs_usb: gs_usb_get_gs_can(): introduce new function to consolidate validation of struct gs_can
This patch adds a new helper, gs_usb_get_gs_can(). It takes over three
checks from gs_usb_receive_bulk_callback(): the short read check on the
RX header, the channel id range check, and the netdev present/running
checks.
The helper returns the struct gs_can pointer or NULL. It also sets a
fatal flag, which the caller uses to decide 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 f49179cbd7876..c392cf65d8a6d 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: Low]
Is this change in behavior intended? The commit message presents the
patch as a refactor:
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().
However, the not-present case now behaves differently. Before the patch,
gs_usb_receive_bulk_callback() returned without resubmitting the URB and
did not touch the other channels:
if (!netif_device_present(netdev))
return;
Now that *fatal is set here, the caller jumps to device_detach, which
detaches every channel of the parent:
device_detach:
for (rc = 0; rc < parent->channel_cnt; rc++) {
if (parent->canch[rc])
netif_device_detach(parent->canch[rc]->netdev);
}
One channel can be detached by itself. For example, gs_can_start_xmit()
and gs_can_open() call netif_device_detach() on only their own netdev
when they get -ENODEV. Before this patch the sibling channels stayed
attached in that case. With this patch, the next RX frame carrying the
detached channel's index also detaches all the siblings.
This is probably harmless in practice. -ENODEV means the shared USB
device is gone, so the siblings would fail anyway, and in both versions
the URB is not resubmitted.
The commit message does say "If fatal is true, detach the device", but
it doesn't mention that the not-present case used to be handled
differently. This behavior is still the same later in the series:
gs_usb_get_gs_can() still sets *fatal for !netif_device_present() in
"can: gs_usb: gs_usb_host_frame_header_valid(): drop CAN frames with
invalid DLC".
Could the commit message mention and justify this change? Otherwise,
should the old behavior (neither resubmit nor detach) be kept for this
case?
--
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261121-gs_usb-fixes-v9-0-db121d488945%40pengutronix.de
^ permalink raw reply [flat|nested] 28+ messages in thread
* [PATCH can-next v9 10/13] can: gs_usb: gs_usb_get_gs_can(): report channel number out of range error
2026-11-21 22:35 [PATCH can-next v9 00/13] gs_usb: fixes Marc Kleine-Budde
` (8 preceding siblings ...)
2026-11-21 22:36 ` [PATCH can-next v9 09/13] can: gs_usb: gs_usb_get_gs_can(): introduce new function to consolidate validation of struct gs_can Marc Kleine-Budde
@ 2026-11-21 22:36 ` Marc Kleine-Budde
2026-11-23 12:00 ` netdev-bot+sashiko
2026-11-21 22:36 ` [PATCH can-next v9 11/13] can: gs_usb: gs_usb_get_rx_data_length(): introduce new function to consolidate validation of host frame Marc Kleine-Budde
` (2 subsequent siblings)
12 siblings, 1 reply; 28+ messages in thread
From: Marc Kleine-Budde @ 2026-11-21 22:36 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 c392cf65d8a6..aca4f4fdc796 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] 28+ messages in thread* Re: [PATCH can-next v9 10/13] can: gs_usb: gs_usb_get_gs_can(): report channel number out of range error
2026-11-21 22:36 ` [PATCH can-next v9 10/13] can: gs_usb: gs_usb_get_gs_can(): report channel number out of range error Marc Kleine-Budde
@ 2026-11-23 12:00 ` netdev-bot+sashiko
0 siblings, 0 replies; 28+ messages in thread
From: netdev-bot+sashiko @ 2026-11-23 12:00 UTC (permalink / raw)
To: mkl
Cc: mailhol, pfink, eric, max, wg, kernel, linux-can,
shuangpeng.kernel, stable, vadim.fedorenko, 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] Grammar error in the commit message of "can: gs_usb:
gs_usb_get_gs_can(): report channel number out of range error".
--
can: gs_usb: gs_usb_get_gs_can(): report channel number out of range error
When the USB device reports a channel number that is out of range,
gs_usb_get_gs_can() now prints a rate-limited error message. It then
marks the condition as fatal and detaches, as before.
> If the USB device sends an invalid channel, the device should not be
> silently detached; instead, the display an error message before detaching.
[Severity: Low]
This isn't a bug, but there's an extra "the" in the commit message.
"instead, the display an error message" should probably read "instead,
display an error message".
The code change in gs_usb_get_gs_can() matches what the message
describes.
> diff --git a/drivers/net/can/usb/gs_usb.c b/drivers/net/can/usb/gs_usb.c
> index c392cf65d8a6d..aca4f4fdc796a 100644
> --- a/drivers/net/can/usb/gs_usb.c
> +++ b/drivers/net/can/usb/gs_usb.c
[ ... ]
--
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261121-gs_usb-fixes-v9-0-db121d488945%40pengutronix.de
^ permalink raw reply [flat|nested] 28+ messages in thread
* [PATCH can-next v9 11/13] can: gs_usb: gs_usb_get_rx_data_length(): introduce new function to consolidate validation of host frame
2026-11-21 22:35 [PATCH can-next v9 00/13] gs_usb: fixes Marc Kleine-Budde
` (9 preceding siblings ...)
2026-11-21 22:36 ` [PATCH can-next v9 10/13] can: gs_usb: gs_usb_get_gs_can(): report channel number out of range error Marc Kleine-Budde
@ 2026-11-21 22:36 ` Marc Kleine-Budde
2026-11-23 12:00 ` netdev-bot+sashiko
2026-11-21 22:36 ` [PATCH can-next v9 12/13] can: gs_usb: gs_usb_host_frame_header_valid(): drop invalid CAN error frames Marc Kleine-Budde
2026-11-21 22:36 ` [PATCH can-next v9 13/13] can: gs_usb: gs_usb_host_frame_header_valid(): drop CAN frames with invalid DLC Marc Kleine-Budde
12 siblings, 1 reply; 28+ messages in thread
From: Marc Kleine-Budde @ 2026-11-21 22:36 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 aca4f4fdc796..e4e177aee48b 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] 28+ messages in thread* Re: [PATCH can-next v9 11/13] can: gs_usb: gs_usb_get_rx_data_length(): introduce new function to consolidate validation of host frame
2026-11-21 22:36 ` [PATCH can-next v9 11/13] can: gs_usb: gs_usb_get_rx_data_length(): introduce new function to consolidate validation of host frame Marc Kleine-Budde
@ 2026-11-23 12:00 ` netdev-bot+sashiko
0 siblings, 0 replies; 28+ messages in thread
From: netdev-bot+sashiko @ 2026-11-23 12:00 UTC (permalink / raw)
To: mkl
Cc: mailhol, pfink, eric, max, wg, kernel, linux-can,
shuangpeng.kernel, stable, vadim.fedorenko, 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 understates what the patch does.
- [Low] In gs_usb_get_rx_data_length(), the patch declares minimum_length
as int but prints it with %u in "short read (actual_length=%u,…
--
can: gs_usb: gs_usb_get_rx_data_length(): introduce new function to consolidate validation of host frame
This commit adds gs_usb_get_rx_data_length(), which consolidates the
validation of the struct gs_host_frame received in the bulk IN URB. It
returns the data length on success or a negative errno on error.
gs_usb_receive_bulk_callback() is converted to use it.
> Add a new function: gs_usb_get_rx_data_length() to consolidate the
> validation of the struct gs_host_frame from the received URB.
[Severity: Low]
Is this description complete? The patch seems to do more than move the
existing checks into one function.
It also adds a second helper, gs_usb_host_frame_header_valid().
An out of range TX echo_id (GS_MAX_TX_URBS <= echo_id <
GS_HOST_FRAME_ECHO_ID_RX) now increments netdev->stats.rx_errors. The old
check in the else branch of gs_usb_receive_bulk_callback() did not update
any counter.
The error print is now behind net_ratelimit(). Its text changes from
"Unexpected out of range echo id %u" to "out of range Echo ID
(echo_id=0x%08x, max_echo_id=0x%08x)".
The echo_id check now runs before the short read check. Before the patch,
a frame that was both short and had a bad echo_id bumped rx_errors and
rx_length_errors and logged "short read". Now it bumps only rx_errors and
logs the echo ID message.
Whether a frame is accepted or dropped does not change.
Could the commit message mention these behavior changes, so the patch is
not read as a pure code move?
> diff --git a/drivers/net/can/usb/gs_usb.c b/drivers/net/can/usb/gs_usb.c
> index aca4f4fdc796a..e4e177aee48b0 100644
> --- a/drivers/net/can/usb/gs_usb.c
> +++ b/drivers/net/can/usb/gs_usb.c
[ ... ]
> +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 declared as int here, but it is still printed with
%u. The check actual_length < minimum_length also compares a u32 with an
int. The removed code in gs_usb_receive_bulk_callback() declared it as
unsigned int.
gs_usb_get_minimum_rx_length() now returns int, but its local
minimum_length is still unsigned int. The other "short read" print, in
gs_usb_get_gs_can(), uses a u32 with the same %u:
gs_usb_get_gs_can() {
const u32 minimum_length = sizeof(*header);
...
}
Today the values are small positive sizes, so the print and the
comparison both give correct results. If gs_usb_get_minimum_rx_length()
ever returned a negative errno, the value would print as a huge number.
The unsigned promotion in the comparison would then reject every frame.
This mismatch is still present at the end of the series. Should
minimum_length and the return type of gs_usb_get_minimum_rx_length()
stay unsigned?
> +
> + return -EINVAL;
> + }
> +
> + return data_length;
> +}
[ ... ]
--
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261121-gs_usb-fixes-v9-0-db121d488945%40pengutronix.de
^ permalink raw reply [flat|nested] 28+ messages in thread
* [PATCH can-next v9 12/13] can: gs_usb: gs_usb_host_frame_header_valid(): drop invalid CAN error frames
2026-11-21 22:35 [PATCH can-next v9 00/13] gs_usb: fixes Marc Kleine-Budde
` (10 preceding siblings ...)
2026-11-21 22:36 ` [PATCH can-next v9 11/13] can: gs_usb: gs_usb_get_rx_data_length(): introduce new function to consolidate validation of host frame Marc Kleine-Budde
@ 2026-11-21 22:36 ` Marc Kleine-Budde
2026-11-21 22:53 ` sashiko-bot
2026-11-23 12:00 ` netdev-bot+sashiko
2026-11-21 22:36 ` [PATCH can-next v9 13/13] can: gs_usb: gs_usb_host_frame_header_valid(): drop CAN frames with invalid DLC Marc Kleine-Budde
12 siblings, 2 replies; 28+ messages in thread
From: Marc Kleine-Budde @ 2026-11-21 22:36 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 | 24 ++++++++++++++++++++++++
1 file changed, 24 insertions(+)
diff --git a/drivers/net/can/usb/gs_usb.c b/drivers/net/can/usb/gs_usb.c
index e4e177aee48b..a1192867e49b 100644
--- a/drivers/net/can/usb/gs_usb.c
+++ b/drivers/net/can/usb/gs_usb.c
@@ -639,6 +639,30 @@ static bool gs_usb_host_frame_header_valid(const struct gs_can *dev,
valid = false;
}
+ 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] 28+ messages in thread* Re: [PATCH can-next v9 12/13] can: gs_usb: gs_usb_host_frame_header_valid(): drop invalid CAN error frames
2026-11-21 22:36 ` [PATCH can-next v9 12/13] can: gs_usb: gs_usb_host_frame_header_valid(): drop invalid CAN error frames Marc Kleine-Budde
@ 2026-11-21 22:53 ` sashiko-bot
2026-11-21 23:02 ` Marc Kleine-Budde
2026-11-23 7:41 ` Marc Kleine-Budde
2026-11-23 12:00 ` netdev-bot+sashiko
1 sibling, 2 replies; 28+ messages in thread
From: sashiko-bot @ 2026-11-21 22:53 UTC (permalink / raw)
To: Marc Kleine-Budde
Cc: linux-can, Oliver Hartkopp, Vincent Mailhol, Oleksij Rempel
Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Medium] can: gs_usb: TX context and echo skb leak on echoed
CAN_ERR_FLAG frames
--
commit b1d99b2dbb5c984106e59cf632bd8f01d76e1bcf
Author: Marc Kleine-Budde <mkl@pengutronix.de>
can: gs_usb: gs_usb_host_frame_header_valid(): drop invalid CAN error frames
This commit validates incoming CAN error frame headers in
gs_usb_host_frame_header_valid(), rejecting frames with CAN_ERR_FLAG set
if can_dlc is not CAN_ERR_DLC or if CAN_RTR_FLAG is set.
> diff --git a/drivers/net/can/usb/gs_usb.c b/drivers/net/can/usb/gs_usb.c
> index e4e177aee48b0..a1192867e49b8 100644
> --- a/drivers/net/can/usb/gs_usb.c
> +++ b/drivers/net/can/usb/gs_usb.c
> @@ -639,6 +639,30 @@ static bool gs_usb_host_frame_header_valid(const struct gs_can *dev,
> valid = false;
> }
>
> + 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;
> }
[Severity: Medium]
Can this leak txc and dev->can.echo_skb[hf->echo_id] when a transmitted
frame with CAN_ERR_FLAG set is echoed back by the device?
The TX path and gs_can_start_xmit() do not mask out or reject
CAN_ERR_FLAG in cf->can_id before allocating a TX context and storing the
echo skb. When the device echoes that TX frame back with
header->echo_id < GS_MAX_TX_URBS, gs_usb_receive_bulk_callback() calls
gs_usb_get_rx_data_length() -> gs_usb_host_frame_header_valid() before
branching on hf->echo_id == GS_HOST_FRAME_ECHO_ID_RX. If can_dlc !=
CAN_ERR_DLC or CAN_RTR_FLAG is set, gs_usb_receive_bulk_callback() jumps
to resubmit_urb without calling gs_free_tx_context(), consuming the echo
skb, or decrementing dev->active_tx_urbs, which permanently exhausts all
GS_MAX_TX_URBS slots and stalls the TX queue after 10 such frames.
Should this check be gated on header->echo_id == GS_HOST_FRAME_ECHO_ID_RX
(or should gs_can_start_xmit() reject or strip CAN_ERR_FLAG on TX)?
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261121-gs_usb-fixes-v9-0-db121d488945@pengutronix.de?part=12
^ permalink raw reply [flat|nested] 28+ messages in thread* Re: [PATCH can-next v9 12/13] can: gs_usb: gs_usb_host_frame_header_valid(): drop invalid CAN error frames
2026-11-21 22:53 ` sashiko-bot
@ 2026-11-21 23:02 ` Marc Kleine-Budde
2026-11-23 7:41 ` Marc Kleine-Budde
1 sibling, 0 replies; 28+ messages in thread
From: Marc Kleine-Budde @ 2026-11-21 23:02 UTC (permalink / raw)
To: sashiko-bot; +Cc: linux-can, Oliver Hartkopp, Vincent Mailhol, Oleksij Rempel
[-- Attachment #1: Type: text/plain, Size: 2147 bytes --]
On 21.11.2026 22:53:55, sashiko-bot@kernel.org wrote:
> Thank you for your contribution! Sashiko AI review found 1 potential
> issue to consider:
> - [Medium] can: gs_usb: TX context and echo skb leak on echoed
> CAN_ERR_FLAG frames
> --
>
> commit b1d99b2dbb5c984106e59cf632bd8f01d76e1bcf
> Author: Marc Kleine-Budde <mkl@pengutronix.de>
>
> can: gs_usb: gs_usb_host_frame_header_valid(): drop invalid CAN error frames
>
> This commit validates incoming CAN error frame headers in
> gs_usb_host_frame_header_valid(), rejecting frames with CAN_ERR_FLAG set
> if can_dlc is not CAN_ERR_DLC or if CAN_RTR_FLAG is set.
>
> > diff --git a/drivers/net/can/usb/gs_usb.c b/drivers/net/can/usb/gs_usb.c
> > index e4e177aee48b0..a1192867e49b8 100644
> > --- a/drivers/net/can/usb/gs_usb.c
> > +++ b/drivers/net/can/usb/gs_usb.c
> > @@ -639,6 +639,30 @@ static bool gs_usb_host_frame_header_valid(const struct gs_can *dev,
> > valid = false;
> > }
> >
> > + 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;
> > }
>
> [Severity: Medium]
> Can this leak txc and dev->can.echo_skb[hf->echo_id] when a transmitted
> frame with CAN_ERR_FLAG set is echoed back by the device?
valid finding.
pw-bot: cr
Marc
--
Pengutronix e.K. | Marc Kleine-Budde |
Embedded Linux | https://www.pengutronix.de |
Vertretung Nürnberg | Phone: +49-5121-206917-129 |
Amtsgericht Hildesheim, HRA 2686 | Fax: +49-5121-206917-9 |
[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 228 bytes --]
^ permalink raw reply [flat|nested] 28+ messages in thread* Re: [PATCH can-next v9 12/13] can: gs_usb: gs_usb_host_frame_header_valid(): drop invalid CAN error frames
2026-11-21 22:53 ` sashiko-bot
2026-11-21 23:02 ` Marc Kleine-Budde
@ 2026-11-23 7:41 ` Marc Kleine-Budde
1 sibling, 0 replies; 28+ messages in thread
From: Marc Kleine-Budde @ 2026-11-23 7:41 UTC (permalink / raw)
To: sashiko-bot; +Cc: linux-can, Oliver Hartkopp, Vincent Mailhol, Oleksij Rempel
[-- Attachment #1: Type: text/plain, Size: 2142 bytes --]
On 21.11.2026 22:53:55, sashiko-bot@kernel.org wrote:
> Thank you for your contribution! Sashiko AI review found 1 potential
> issue to consider:
> - [Medium] can: gs_usb: TX context and echo skb leak on echoed
> CAN_ERR_FLAG frames
> --
>
> commit b1d99b2dbb5c984106e59cf632bd8f01d76e1bcf
> Author: Marc Kleine-Budde <mkl@pengutronix.de>
>
> can: gs_usb: gs_usb_host_frame_header_valid(): drop invalid CAN error frames
>
> This commit validates incoming CAN error frame headers in
> gs_usb_host_frame_header_valid(), rejecting frames with CAN_ERR_FLAG set
> if can_dlc is not CAN_ERR_DLC or if CAN_RTR_FLAG is set.
>
> > diff --git a/drivers/net/can/usb/gs_usb.c b/drivers/net/can/usb/gs_usb.c
> > index e4e177aee48b0..a1192867e49b8 100644
> > --- a/drivers/net/can/usb/gs_usb.c
> > +++ b/drivers/net/can/usb/gs_usb.c
> > @@ -639,6 +639,30 @@ static bool gs_usb_host_frame_header_valid(const struct gs_can *dev,
> > valid = false;
> > }
> >
> > + 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;
> > }
>
> [Severity: Medium]
> Can this leak txc and dev->can.echo_skb[hf->echo_id] when a transmitted
> frame with CAN_ERR_FLAG set is echoed back by the device?
Will fix.
pw-bot: cr
Marc
--
Pengutronix e.K. | Marc Kleine-Budde |
Embedded Linux | https://www.pengutronix.de |
Vertretung Nürnberg | Phone: +49-5121-206917-129 |
Amtsgericht Hildesheim, HRA 2686 | Fax: +49-5121-206917-9 |
[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 228 bytes --]
^ permalink raw reply [flat|nested] 28+ messages in thread
* Re: [PATCH can-next v9 12/13] can: gs_usb: gs_usb_host_frame_header_valid(): drop invalid CAN error frames
2026-11-21 22:36 ` [PATCH can-next v9 12/13] can: gs_usb: gs_usb_host_frame_header_valid(): drop invalid CAN error frames Marc Kleine-Budde
2026-11-21 22:53 ` sashiko-bot
@ 2026-11-23 12:00 ` netdev-bot+sashiko
1 sibling, 0 replies; 28+ messages in thread
From: netdev-bot+sashiko @ 2026-11-23 12:00 UTC (permalink / raw)
To: mkl
Cc: mailhol, pfink, eric, max, wg, kernel, linux-can,
shuangpeng.kernel, stable, vadim.fedorenko, kuba
Thank you for your contribution! Sashiko AI review found 2 potential
issue(s) to consider.
Critical: 0 · High: 1 · Medium: 0 · 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).
- [High] This patch adds an unprivileged local DoS that permanently
wedges TX on the gs_usb CAN interface.
- [Low] The new netdev_err() format string in
gs_usb_host_frame_header_valid() has a typo: "exptect_can_dlc" (should…
--
can: gs_usb: gs_usb_host_frame_header_valid(): drop invalid CAN error frames
This commit adds two checks to gs_usb_host_frame_header_valid(). Frames
with CAN_ERR_FLAG set must have a DLC of CAN_ERR_DLC, and they must not
have CAN_RTR_FLAG set. If either check fails, the frame is dropped and
the URB is resubmitted.
> diff --git a/drivers/net/can/usb/gs_usb.c b/drivers/net/can/usb/gs_usb.c
> index e4e177aee48b0..a1192867e49b8 100644
> --- a/drivers/net/can/usb/gs_usb.c
> +++ b/drivers/net/can/usb/gs_usb.c
> @@ -639,6 +639,30 @@ static bool gs_usb_host_frame_header_valid(const struct gs_can *dev,
> valid = false;
> }
>
> + if (header->can_id & cpu_to_le32(CAN_ERR_FLAG)) {
> + if (header->can_dlc != CAN_ERR_DLC) {
[Severity: High]
This check only looks at CAN_ERR_FLAG and never at echo_id. Does it
also reject TX echo frames (echo_id < GS_MAX_TX_URBS) as well as real RX
frames?
On transmit, gs_can_start_xmit() copies the can_id and DLC from
userspace into the host frame unchanged:
hf->can_id = cpu_to_le32(cf->can_id);
hf->can_dlc = can_get_cc_dlc(cf, dev->can.ctrlmode);
The FD path does the same with can_fd_len2dlc(). The device sends this
header back as the echo.
A CAN_RAW socket needs no capability. Through one, a user can send a
frame with CAN_ERR_FLAG set and len != 8, or a frame with
CAN_ERR_FLAG | CAN_RTR_FLAG. Neither net/can nor can_dev_dropped_skb()
filters these on transmit.
When the echo arrives, gs_usb_receive_bulk_callback() does:
data_length = gs_usb_get_rx_data_length(dev, hf, urb->actual_length);
if (data_length < 0)
goto resubmit_urb;
That jump skips the echo branch:
gs_free_tx_context(txc);
atomic_dec(&dev->active_tx_urbs);
netif_wake_queue(netdev);
gs_usb_xmit_callback() returns early when urb->status == 0, so nothing
else releases the tx context or the echo skb.
After GS_MAX_TX_URBS such frames, every tx context is leaked, and
gs_can_start_xmit() has already stopped the queue:
if (atomic_read(&dev->active_tx_urbs) >= GS_MAX_TX_URBS)
netif_stop_queue(netdev);
Can anything wake the queue again before the interface is taken down and
brought back up? Until then no user can transmit, and the echo skbs stay
charged to the socket. Each rejected echo also bumps
rx_errors/rx_length_errors and logs an error. Before this patch, these
echoes completed normally.
The later patch in this series, "can: gs_usb:
gs_usb_host_frame_header_valid(): drop CAN frames with invalid DLC",
also leaves the CAN_ERR_FLAG block unconditional. The problem is
therefore still there at the end of the series.
Should these checks apply only when header->echo_id ==
GS_HOST_FRAME_ECHO_ID_RX, or should echo frames never be dropped by
payload validation?
> + 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",
[Severity: Low]
This isn't a bug, but there's a typo in the format string: "exptect_can_dlc"
should probably be "expected_can_dlc". It is still there at the end of the
series.
The commit message also says:
ensure that the RTR flag is not sent.
Should that say "set"? The code checks that the RTR flag is not set in
the received header.
> + header->can_dlc, CAN_ERR_DLC);
> +
> + valid = false;
> + }
> +
> + if (header->can_id & cpu_to_le32(CAN_RTR_FLAG)) {
[ ... ]
--
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261121-gs_usb-fixes-v9-0-db121d488945%40pengutronix.de
^ permalink raw reply [flat|nested] 28+ messages in thread
* [PATCH can-next v9 13/13] can: gs_usb: gs_usb_host_frame_header_valid(): drop CAN frames with invalid DLC
2026-11-21 22:35 [PATCH can-next v9 00/13] gs_usb: fixes Marc Kleine-Budde
` (11 preceding siblings ...)
2026-11-21 22:36 ` [PATCH can-next v9 12/13] can: gs_usb: gs_usb_host_frame_header_valid(): drop invalid CAN error frames Marc Kleine-Budde
@ 2026-11-21 22:36 ` Marc Kleine-Budde
2026-11-23 12:00 ` netdev-bot+sashiko
12 siblings, 1 reply; 28+ messages in thread
From: Marc Kleine-Budde @ 2026-11-21 22:36 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 a1192867e49b..2da2f8843e5d 100644
--- a/drivers/net/can/usb/gs_usb.c
+++ b/drivers/net/can/usb/gs_usb.c
@@ -663,6 +663,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] 28+ messages in thread* Re: [PATCH can-next v9 13/13] can: gs_usb: gs_usb_host_frame_header_valid(): drop CAN frames with invalid DLC
2026-11-21 22:36 ` [PATCH can-next v9 13/13] can: gs_usb: gs_usb_host_frame_header_valid(): drop CAN frames with invalid DLC Marc Kleine-Budde
@ 2026-11-23 12:00 ` netdev-bot+sashiko
0 siblings, 0 replies; 28+ messages in thread
From: netdev-bot+sashiko @ 2026-11-23 12:00 UTC (permalink / raw)
To: mkl
Cc: mailhol, pfink, eric, max, wg, kernel, linux-can,
shuangpeng.kernel, stable, vadim.fedorenko, kuba
Thank you for your contribution! Sashiko AI review found 3 potential
issue(s) to consider.
Critical: 0 · High: 0 · Medium: 1 · Low: 2
If you believe none of the issues are legitimate, please reply to each
issue of Medium severity or higher and explain why the AI is wrong.
If any of them are legitimate and you plan to respin, please let patchwork
know by including "pw-bot: cr" as a separate line at the end of your reply
(one such reply per series is enough).
- [Medium] The new DLC check in gs_usb_host_frame_header_valid()
(drivers/net/can/usb/gs_usb.c:666-678) ignores echo_id.
- [Low] The commit fixes a real contract violation that userspace can
see, but it has no Fixes: tag and the message does not state the…
- [Low] In gs_usb_host_frame_header_valid()
(drivers/net/can/usb/gs_usb.c), the new out-of-range DLC check (lines…
--
can: gs_usb: gs_usb_host_frame_header_valid(): drop CAN frames with invalid DLC
This patch adds a check to gs_usb_host_frame_header_valid() that rejects
any host frame with a DLC above 0xf. A rejected frame increments
rx_errors and rx_length_errors, logs a rate limited error and causes the
URB to be resubmitted.
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.
[Severity: Low]
Should this have a Fixes: tag, and could the message state what goes
wrong without the check?
Before this patch, gs_usb_receive_bulk_callback() passed the
device-supplied hf->can_dlc directly to can_frame_set_cc_len(). That
call came in with commit 26949ac935e3 ("can: gs_usb: add CAN-FD
support"):
can_frame_set_cc_len(cf, hf->can_dlc, dev->can.ctrlmode);
The helper expects the caller to have checked the value already:
include/linux/can/length.h:can_frame_set_cc_len() {
/* 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 len8_dlc above 15 could
therefore reach raw CAN socket users.
On the FD path, can_fd_dlc2len() does dlc2len[dlc & 0x0F]. A DLC such as
0x1f was silently turned into a valid 64 byte frame instead of being
rejected.
Other fixes in this series carry Fixes: and Cc: stable tags, for example
"can: gs_usb: gs_usb_receive_bulk_callback(): check for overflow flag if
SKB allocation fails". Would the same tags make sense here, so stable
backporters have something to go on?
> diff --git a/drivers/net/can/usb/gs_usb.c b/drivers/net/can/usb/gs_usb.c
> index a1192867e49b8..2da2f8843e5d5 100644
> --- a/drivers/net/can/usb/gs_usb.c
> +++ b/drivers/net/can/usb/gs_usb.c
> @@ -663,6 +663,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) {
[Severity: Medium]
This check runs whatever header->echo_id is, so TX echo frames
(echo_id < GS_MAX_TX_URBS) with can_dlc > 15 are now rejected as well.
What happens to the pending TX context in that case?
gs_usb_get_rx_data_length() returns -EINVAL, and
gs_usb_receive_bulk_callback() then skips the echo completion branch:
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;
...
} else { /* echo_id == hf->echo_id */
txc = gs_get_tx_context(dev, hf->echo_id);
...
gs_free_tx_context(txc);
atomic_dec(&dev->active_tx_urbs);
netif_wake_queue(netdev);
}
...
}
The TX context slot and the echo skb that gs_can_start_xmit() stored
with can_put_echo_skb() then stay held until gs_can_close(). The
active_tx_urbs count is never decremented either. The driver has no
ndo_tx_timeout or other recovery path.
After GS_MAX_TX_URBS such echoes, gs_alloc_tx_context() fails and
gs_can_start_xmit() returns NETDEV_TX_BUSY. Once the leaked
active_tx_urbs count reaches GS_MAX_TX_URBS, gs_can_start_xmit() stops
the queue and nothing wakes it. TX then stalls until the interface is
taken down and brought back up.
Echo frames never use can_dlc. gs_usb_get_minimum_rx_length() only
converts the DLC to a length for echo_id == GS_HOST_FRAME_ECHO_ID_RX,
and the echo path takes its data from dev->can.echo_skb[]. Before this
patch, an echo with a bogus can_dlc completed normally.
Could the DLC check be limited to echo_id == GS_HOST_FRAME_ECHO_ID_RX?
> + stats->rx_errors++;
> + stats->rx_length_errors++;
[Severity: Low]
Can error frames be counted twice here? Take a frame with CAN_ERR_FLAG
set and can_dlc > 15, for example 0x20. The existing check earlier in
gs_usb_host_frame_header_valid() already fires for it:
if (header->can_id & cpu_to_le32(CAN_ERR_FLAG)) {
if (header->can_dlc != CAN_ERR_DLC) {
stats->rx_errors++;
stats->rx_length_errors++;
Nothing skips the new check once valid is false, so both counters are
incremented again. That one dropped frame adds 2 to rx_length_errors
and at least 2 to rx_errors.
struct rtnl_link_stats64 in include/uapi/linux/if_link.h documents both
counters as counting packets, not individual validation failures.
> +
> + 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;
> + }
[ ... ]
--
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261121-gs_usb-fixes-v9-0-db121d488945%40pengutronix.de
^ permalink raw reply [flat|nested] 28+ messages in thread