* [PATCH can-next v7 1/6] can: gs_usb: gs_make_candev(): check can_rx_offload_add_manual() return value
2026-11-14 15:17 [PATCH can-next v7 0/6] gs_usb: fixes Marc Kleine-Budde
@ 2026-11-14 15:17 ` Marc Kleine-Budde
2026-11-14 15:17 ` [PATCH can-next v7 2/6] can: gs_usb: gs_usb_probe(): init netdev completely before register_candev() Marc Kleine-Budde
` (4 subsequent siblings)
5 siblings, 0 replies; 9+ messages in thread
From: Marc Kleine-Budde @ 2026-11-14 15:17 UTC (permalink / raw)
To: Vincent Mailhol, Peter Fink, Eric Evenchick,
Maximilian Schneider, Wolfgang Grandegger
Cc: kernel, linux-can, linux-kernel, 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 3b9b2f104d86..cac4ce323a1d 100644
--- a/drivers/net/can/usb/gs_usb.c
+++ b/drivers/net/can/usb/gs_usb.c
@@ -1487,7 +1487,10 @@ static struct gs_can *gs_make_candev(unsigned int channel,
dev->can.fd.data_bittiming_const = &dev->data_bt_const;
}
- can_rx_offload_add_manual(netdev, &dev->offload, GS_NAPI_WEIGHT);
+ rc = can_rx_offload_add_manual(netdev, &dev->offload, GS_NAPI_WEIGHT);
+ if (rc)
+ goto out_free_candev;
+
SET_NETDEV_DEV(netdev, &intf->dev);
rc = register_candev(dev->netdev);
--
2.53.0
^ permalink raw reply [flat|nested] 9+ messages in thread* [PATCH can-next v7 2/6] can: gs_usb: gs_usb_probe(): init netdev completely before register_candev()
2026-11-14 15:17 [PATCH can-next v7 0/6] gs_usb: fixes Marc Kleine-Budde
2026-11-14 15:17 ` [PATCH can-next v7 1/6] can: gs_usb: gs_make_candev(): check can_rx_offload_add_manual() return value Marc Kleine-Budde
@ 2026-11-14 15:17 ` Marc Kleine-Budde
2026-11-14 15:17 ` [PATCH can-next v7 3/6] can: gs_usb: gs_usb_receive_bulk_callback(): check for overflow flag if SKB allocation fails Marc Kleine-Budde
` (3 subsequent siblings)
5 siblings, 0 replies; 9+ messages in thread
From: Marc Kleine-Budde @ 2026-11-14 15:17 UTC (permalink / raw)
To: Vincent Mailhol, Peter Fink, Eric Evenchick,
Maximilian Schneider, Wolfgang Grandegger
Cc: kernel, linux-can, linux-kernel, 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 cac4ce323a1d..6997cc8a6b2d 100644
--- a/drivers/net/can/usb/gs_usb.c
+++ b/drivers/net/can/usb/gs_usb.c
@@ -1493,18 +1493,8 @@ static struct gs_can *gs_make_candev(unsigned int channel,
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);
@@ -1512,7 +1502,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);
}
@@ -1590,21 +1579,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
@@ -1624,7 +1608,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)
@@ -1640,8 +1654,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 [flat|nested] 9+ messages in thread* [PATCH can-next v7 3/6] can: gs_usb: gs_usb_receive_bulk_callback(): check for overflow flag if SKB allocation fails
2026-11-14 15:17 [PATCH can-next v7 0/6] gs_usb: fixes Marc Kleine-Budde
2026-11-14 15:17 ` [PATCH can-next v7 1/6] can: gs_usb: gs_make_candev(): check can_rx_offload_add_manual() return value Marc Kleine-Budde
2026-11-14 15:17 ` [PATCH can-next v7 2/6] can: gs_usb: gs_usb_probe(): init netdev completely before register_candev() Marc Kleine-Budde
@ 2026-11-14 15:17 ` Marc Kleine-Budde
2026-11-14 15:17 ` [PATCH can-next v7 4/6] can: gs_usb: gs_usb_receive_bulk_callback(): add missing call to can_rx_offload_irq_finish Marc Kleine-Budde
` (2 subsequent siblings)
5 siblings, 0 replies; 9+ messages in thread
From: Marc Kleine-Budde @ 2026-11-14 15:17 UTC (permalink / raw)
To: Vincent Mailhol, Peter Fink, Eric Evenchick,
Maximilian Schneider, Wolfgang Grandegger
Cc: kernel, linux-can, linux-kernel, 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 6997cc8a6b2d..1db2d660355d 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 [flat|nested] 9+ messages in thread* [PATCH can-next v7 4/6] can: gs_usb: gs_usb_receive_bulk_callback(): add missing call to can_rx_offload_irq_finish
2026-11-14 15:17 [PATCH can-next v7 0/6] gs_usb: fixes Marc Kleine-Budde
` (2 preceding siblings ...)
2026-11-14 15:17 ` [PATCH can-next v7 3/6] can: gs_usb: gs_usb_receive_bulk_callback(): check for overflow flag if SKB allocation fails Marc Kleine-Budde
@ 2026-11-14 15:17 ` Marc Kleine-Budde
2026-11-14 15:17 ` [PATCH can-next v7 5/6] can: gs_usb: fix hardware timestamp state for mixed channels Marc Kleine-Budde
2026-11-14 15:17 ` [PATCH can-next v7 6/6] can: gs_usb: properly shut down timestamp worker Marc Kleine-Budde
5 siblings, 0 replies; 9+ messages in thread
From: Marc Kleine-Budde @ 2026-11-14 15:17 UTC (permalink / raw)
To: Vincent Mailhol, Peter Fink, Eric Evenchick,
Maximilian Schneider, Wolfgang Grandegger
Cc: kernel, linux-can, linux-kernel, 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 1db2d660355d..980c4a748c94 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 [flat|nested] 9+ messages in thread* [PATCH can-next v7 5/6] can: gs_usb: fix hardware timestamp state for mixed channels
2026-11-14 15:17 [PATCH can-next v7 0/6] gs_usb: fixes Marc Kleine-Budde
` (3 preceding siblings ...)
2026-11-14 15:17 ` [PATCH can-next v7 4/6] can: gs_usb: gs_usb_receive_bulk_callback(): add missing call to can_rx_offload_irq_finish Marc Kleine-Budde
@ 2026-11-14 15:17 ` Marc Kleine-Budde
2026-11-16 4:18 ` netdev-bot+sashiko
2026-11-14 15:17 ` [PATCH can-next v7 6/6] can: gs_usb: properly shut down timestamp worker Marc Kleine-Budde
5 siblings, 1 reply; 9+ messages in thread
From: Marc Kleine-Budde @ 2026-11-14 15:17 UTC (permalink / raw)
To: Vincent Mailhol, Peter Fink, Eric Evenchick,
Maximilian Schneider, Wolfgang Grandegger
Cc: kernel, linux-can, linux-kernel, 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.
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().
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.
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, this change is not yet implemented in this patch,
but the next patch will switch to the new {enable,disable}_delayed_work()
API to avoid self-rescheduling the timestamp worker.
That API has been added with v6.10, commit 86898fa6b8cd ("workqueue:
Implement disable/enable for (delayed) work items").
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 | 79 ++++++++++++++++++++++++++++++++++++++------
1 file changed, 68 insertions(+), 11 deletions(-)
diff --git a/drivers/net/can/usb/gs_usb.c b/drivers/net/can/usb/gs_usb.c
index 980c4a748c94..b7780a23eff3 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,11 +336,13 @@ 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 */
struct delayed_work timestamp;
unsigned int hf_size_rx;
u8 active_channels;
+ u8 flags;
u8 channel_cnt;
unsigned int pipe_in;
@@ -405,6 +411,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;
@@ -423,7 +430,16 @@ static inline int gs_usb_get_timestamp(const struct gs_usb *parent,
return 0;
}
-static u64 gs_usb_timestamp_read(struct cyclecounter *cc) __must_hold(&dev->tc_lock)
+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(&container_of(cc, struct gs_usb, cc)->tc_lock)
{
struct gs_usb *parent = container_of(cc, struct gs_usb, cc);
u32 timestamp = 0;
@@ -475,19 +491,44 @@ 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_bh(&parent->tc_lock);
- timecounter_init(&parent->tc, &parent->cc, ktime_get_real_ns());
- spin_unlock_bh(&parent->tc_lock);
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 err;
+
+ /* read current timestamp, might be used later for initial time stamp */
+ err = gs_usb_get_timestamp(parent, ×tamp);
+ if (err)
+ return err;
+
+ scoped_guard(spinlock_bh, &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)
@@ -988,9 +1029,15 @@ 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)
+ goto out_can_rx_offload_disable;
+ }
for (i = 0; i < GS_MAX_RX_URBS; i++) {
u8 *buf;
@@ -1105,10 +1152,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);
@@ -1158,12 +1205,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);
}
@@ -1525,6 +1575,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;
@@ -1599,6 +1650,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.
*/
@@ -1616,6 +1670,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 [flat|nested] 9+ messages in thread* Re: [PATCH can-next v7 5/6] can: gs_usb: fix hardware timestamp state for mixed channels
2026-11-14 15:17 ` [PATCH can-next v7 5/6] can: gs_usb: fix hardware timestamp state for mixed channels Marc Kleine-Budde
@ 2026-11-16 4:18 ` netdev-bot+sashiko
0 siblings, 0 replies; 9+ messages in thread
From: netdev-bot+sashiko @ 2026-11-16 4:18 UTC (permalink / raw)
To: mkl
Cc: mailhol, pfink, eric, max, wg, kernel, linux-can, linux-kernel,
shuangpeng.kernel, stable, vadim.fedorenko, kuba
Thank you for your contribution! Sashiko AI review found 2 potential
issue(s) to consider.
Critical: 0 · High: 0 (1) · Medium: 1 · Low: 1
If you believe none of the issues are legitimate, please reply to each
issue of Medium severity or higher and explain why the AI is wrong.
If any of them are legitimate and you plan to respin, please let patchwork
know by including "pw-bot: cr" as a separate line at the end of your reply
(one such reply per series is enough).
- [Medium] gs_usb_timestamp_start() (drivers/net/can/usb/gs_usb.c) now
returns any error from the interface-wide GS_USB_BREQ_TIMESTAMP…
- [Low] The commit message only says the old gating "leads to problems".
Pre-existing issues:
- [High] This problem wasn't introduced by this patch, but the RX URB
completion handler gs_usb_receive_bulk_callback() reaches…
--
can: gs_usb: fix hardware timestamp state for mixed channels
This patch moves the HW timestamp cyclecounter setup into gs_usb_probe().
The setup runs when any channel advertises GS_CAN_FEATURE_HW_TIMESTAMP.
A new gs_usb_timestamp_start() initializes the timecounter and schedules
the worker when the first channel opens, and start/stop are now gated on
a parent-wide flag instead of the opened or closed channel's feature bits.
> 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.
[Severity: Low]
Could the commit message name the problems this fixes? Reading the
pre-patch code, there seem to be two.
First case: the first opened channel lacks GS_CAN_FEATURE_HW_TIMESTAMP
and a later one has it. Then gs_usb_timestamp_init() never runs and
parent->tc stays zeroed from kzalloc_flex(). RX on the timestamping
channel then takes this path:
gs_usb_receive_bulk_callback()
gs_usb_rx_offload() / gs_usb_get_echo_skb()
gs_usb_set_timestamp()
gs_usb_skb_set_timestamp()
timecounter_cyc2time()
timecounter_cyc2time() dereferences tc->cc->mask with tc->cc == NULL. It
does this under a tc_lock that was never initialized.
Second case: the first opened channel has the feature and the last
closed one does not. Then gs_can_close() skips gs_usb_timestamp_stop(),
gs_usb_timestamp_work() keeps rescheduling itself, and
gs_usb_disconnect() frees parent while the work is still queued.
The Closes: links point to an external tracker. Would it help to state
the NULL pointer dereference and the use-after-free in the changelog?
> diff --git a/drivers/net/can/usb/gs_usb.c b/drivers/net/can/usb/gs_usb.c
> index 980c4a748c946..b7780a23eff3b 100644
> --- a/drivers/net/can/usb/gs_usb.c
> +++ b/drivers/net/can/usb/gs_usb.c
[ ... ]
> @@ -332,11 +336,13 @@ 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 */
> struct delayed_work timestamp;
[Severity: High]
This is a pre-existing issue, not one introduced by this patch, but is
spin_lock_bh() the right primitive for tc_lock?
gs_usb_skb_set_timestamp() is reached from the RX URB completion
gs_usb_receive_bulk_callback():
static void gs_usb_skb_set_timestamp(struct gs_can *dev,
struct sk_buff *skb, u32 timestamp)
{
...
spin_lock_bh(&parent->tc_lock);
ns = timecounter_cyc2time(&parent->tc, timestamp);
spin_unlock_bh(&parent->tc_lock);
...
}
Some host controllers don't set HCD_BH. OHCI is one: ohci_hc_driver has
.flags = HCD_MEMORY | HCD_DMA | HCD_USB11. On those, usb_hcd_giveback_urb()
runs the completion directly from the HCD hardirq handler:
if (!hcd_giveback_urb_in_bh(hcd) && !is_root_hub(urb->dev)) {
__usb_hcd_giveback_urb(urb);
return;
}
In that context, spin_unlock_bh() would hit WARN_ON_ONCE(in_hardirq())
in __local_bh_enable_ip().
gs_usb_timestamp_work() holds the same lock with hard IRQs enabled:
spin_lock_bh(&parent->tc_lock);
timecounter_read(&parent->tc);
spin_unlock_bh(&parent->tc_lock);
gs_usb_timestamp_read() also re-takes the lock the same way after its
USB transfer. Suppose the HCD interrupt arrives on that CPU in this
window and completes an RX URB for a timestamping channel. Can
gs_usb_skb_set_timestamp() then spin forever on the lock held by the
worker it interrupted?
This patch keeps the scheme, and it is still there at the end of the
series. Would spin_lock_irqsave() on all tc_lock paths avoid this?
[ ... ]
> @@ -475,19 +491,44 @@ static void gs_usb_timestamp_init(struct gs_usb *parent)
[ ... ]
> +static int gs_usb_timestamp_start(struct gs_usb *parent)
> +{
> + u32 timestamp = 0;
> + int err;
> +
> + /* read current timestamp, might be used later for initial time stamp */
> + err = gs_usb_get_timestamp(parent, ×tamp);
> + if (err)
> + return err;
[ ... ]
> @@ -988,9 +1029,15 @@ 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)
> + goto out_can_rx_offload_disable;
> + }
[Severity: Medium]
Is it intended that a failed GS_USB_BREQ_TIMESTAMP read now makes ifup
fail?
gs_usb_timestamp_start() returns any error from gs_usb_get_timestamp(),
for example a STALL, a short read (-EREMOTEIO from
usb_control_msg_recv()) or a control timeout. gs_can_open() then aborts
through out_can_rx_offload_disable.
The check here uses the parent-wide GS_USB_PARENT_FLAGS_HW_TIMESTAMP,
not dev->feature. On a mixed device, a channel without
GS_CAN_FEATURE_HW_TIMESTAMP that is opened first now sends this request
and can fail to open because of it.
Before this patch, the initial read went through gs_usb_timestamp_read()
inside timecounter_init(). On error that path only logged "HW timestamps
may be inaccurate" and the open still succeeded. A channel without the
timestamp feature also never sent the request.
The commit message doesn't mention this change. It is also unchanged
later in the series: "can: gs_usb: properly shut down timestamp worker"
keeps the "if (err) return err;".
Should a timestamp read failure still be fatal when the channel being
opened doesn't use HW timestamps?
--
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261114-gs_usb-fixes-v7-0-58b6341b1c08%40pengutronix.de
^ permalink raw reply [flat|nested] 9+ messages in thread
* [PATCH can-next v7 6/6] can: gs_usb: properly shut down timestamp worker
2026-11-14 15:17 [PATCH can-next v7 0/6] gs_usb: fixes Marc Kleine-Budde
` (4 preceding siblings ...)
2026-11-14 15:17 ` [PATCH can-next v7 5/6] can: gs_usb: fix hardware timestamp state for mixed channels Marc Kleine-Budde
@ 2026-11-14 15:17 ` Marc Kleine-Budde
2026-11-16 4:18 ` netdev-bot+sashiko
5 siblings, 1 reply; 9+ messages in thread
From: Marc Kleine-Budde @ 2026-11-14 15:17 UTC (permalink / raw)
To: Vincent Mailhol, Peter Fink, Eric Evenchick,
Maximilian Schneider, Wolfgang Grandegger
Cc: kernel, linux-can, linux-kernel, Marc Kleine-Budde
To avoid self-rescheduling of the timestamp worker, shut it down using
disable_delayed_work_sync() instead of cancel_delayed_work_sync().
Signed-off-by: Marc Kleine-Budde <mkl@pengutronix.de>
---
drivers/net/can/usb/gs_usb.c | 4 +++-
1 file changed, 3 insertions(+), 1 deletion(-)
diff --git a/drivers/net/can/usb/gs_usb.c b/drivers/net/can/usb/gs_usb.c
index b7780a23eff3..b9ba3775eb60 100644
--- a/drivers/net/can/usb/gs_usb.c
+++ b/drivers/net/can/usb/gs_usb.c
@@ -498,6 +498,7 @@ static void gs_usb_timestamp_init(struct gs_usb *parent)
spin_lock_init(&parent->tc_lock);
INIT_DELAYED_WORK(&parent->timestamp, gs_usb_timestamp_work);
+ disable_delayed_work(&parent->timestamp);
parent->flags |= GS_USB_PARENT_FLAGS_HW_TIMESTAMP;
}
@@ -525,6 +526,7 @@ static int gs_usb_timestamp_start(struct gs_usb *parent)
cc->read = gs_usb_timestamp_read;
}
+ enable_delayed_work(&parent->timestamp);
schedule_delayed_work(&parent->timestamp,
GS_USB_TIMESTAMP_WORK_DELAY_SEC * HZ);
@@ -533,7 +535,7 @@ static int gs_usb_timestamp_start(struct gs_usb *parent)
static void gs_usb_timestamp_stop(struct gs_usb *parent)
{
- cancel_delayed_work_sync(&parent->timestamp);
+ disable_delayed_work_sync(&parent->timestamp);
}
static void gs_update_state(struct gs_can *dev, struct can_frame *cf)
--
2.53.0
^ permalink raw reply [flat|nested] 9+ messages in thread* Re: [PATCH can-next v7 6/6] can: gs_usb: properly shut down timestamp worker
2026-11-14 15:17 ` [PATCH can-next v7 6/6] can: gs_usb: properly shut down timestamp worker Marc Kleine-Budde
@ 2026-11-16 4:18 ` netdev-bot+sashiko
0 siblings, 0 replies; 9+ messages in thread
From: netdev-bot+sashiko @ 2026-11-16 4:18 UTC (permalink / raw)
To: mkl
Cc: mailhol, pfink, eric, max, wg, kernel, linux-can, linux-kernel,
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 gives the wrong reason for the change.
--
can: gs_usb: properly shut down timestamp worker
This commit disables the timestamp delayed work at init time, enables it
in gs_usb_timestamp_start(), and switches gs_usb_timestamp_stop() from
cancel_delayed_work_sync() to disable_delayed_work_sync().
> To avoid self-rescheduling of the timestamp worker, shut it down using
> disable_delayed_work_sync() instead of cancel_delayed_work_sync().
[Severity: Low]
Is this reason accurate? cancel_delayed_work_sync() already seems to
handle a self-requeueing work.
cancel_delayed_work_sync() goes through __cancel_work_sync(), and that
function always disables the work before it flushes. It only re-enables
the work after the callback has finished:
kernel/workqueue.c:__cancel_work_sync() {
...
ret = __cancel_work(work, cflags | WORK_CANCEL_DISABLE);
...
if (wq_online)
__flush_work(work, true);
if (!(cflags & WORK_CANCEL_DISABLE))
enable_work(work);
...
}
Suppose gs_usb_timestamp_work() requeues itself while the flush is
waiting:
drivers/net/can/usb/gs_usb.c:gs_usb_timestamp_work() {
...
schedule_delayed_work(&parent->timestamp,
GS_USB_TIMESTAMP_WORK_DELAY_SEC * HZ);
}
The work is still disabled at that point, so the requeue is rejected.
The kernel-doc for cancel_work_sync() also says it "can be used even if
the work re-queues itself".
The only other place that queues parent->timestamp is
gs_usb_timestamp_start(). It runs from gs_can_open() under RTNL, and
only when parent->active_channels == 0. Leaving the work enabled
between stop and start therefore does not appear to be reachable as a
bug.
The enable/disable count added by this patch looks balanced across the
init, start, stop, and open error paths.
The parent commit, "can: gs_usb: fix hardware timestamp state for mixed
channels" (Cc: stable), says this follow-up is needed to avoid
self-rescheduling the timestamp worker. It also says it was split out
to simplify backporting. Together, the two messages could suggest that
stable kernels without this patch still have a live self-rescheduling
bug. Older kernels also blocked the self-requeue during cancel, through
WORK_OFFQ_CANCELING.
Could the commit message describe this as cleanup or hardening, making
the stopped state explicit, rather than as a fix for self-rescheduling?
--
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261114-gs_usb-fixes-v7-0-58b6341b1c08%40pengutronix.de
^ permalink raw reply [flat|nested] 9+ messages in thread