[PATCH can-next v6 6/6] can: gs_usb: fix hardware timestamp state for mixed channels
From: Marc Kleine-Budde
Date: Fri Nov 13 2026 - 10:43:31 EST
From: Shuangpeng Bai <shuangpeng.kernel@xxxxxxxxx>
For hardware timestamping, the driver manages the
infrastructure (cyclecounter/timecounter) for the entire USB interface,
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 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.
Extend the functions gs_usb_timestamp_start()/gs_usb_timestamp_stop() to
track the number of active timestamp channels. Initialize the timestamping
infrastructure when opening the first channel. Avoid the race window during
timecounter_init() (because gs_usb_timestamp_read() drops the struct
gs_usb::tc_lock) by reading the initial time stamp prior taking the lock.
Enabling and disabling the delayed worker prevents concurrent rescheduling
while gs_usb_timestamp_stop() is running.
Call gs_usb_timestamp_start()/gs_usb_timestamp_stop() if at least one of
the channels support HW timestamping, rather than relying on the number of
active channels.
Reported-by: Shuangpeng Bai <shuangpeng.kernel@xxxxxxxxx>
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@xxxxxxxxxxxxxxx
Cc: Shuangpeng Bai <shuangpeng.kernel@xxxxxxxxx>
Cc: Vadim Fedorenko <vadim.fedorenko@xxxxxxxxx>
Signed-off-by: Shuangpeng Bai <shuangpeng.kernel@xxxxxxxxx>
Co-developed-by: Marc Kleine-Budde <mkl@xxxxxxxxxxxxxx>
Signed-off-by: Marc Kleine-Budde <mkl@xxxxxxxxxxxxxx>
---
drivers/net/can/usb/gs_usb.c | 98 +++++++++++++++++++++++++++++++++++---------
1 file changed, 78 insertions(+), 20 deletions(-)
diff --git a/drivers/net/can/usb/gs_usb.c b/drivers/net/can/usb/gs_usb.c
index 6327069f63c9..9eafdf63a4ef 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,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 */
+ spinlock_t tc_lock; /* spinlock to guard access tc and active_timestamp_channels */
struct delayed_work timestamp;
+ u32 initial_timestamp;
unsigned int hf_size_rx;
u8 active_channels;
+ u8 active_timestamp_channels;
+ u8 flags;
u8 channel_cnt;
unsigned int pipe_in;
@@ -423,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->initial_timestamp;
+}
+
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);
@@ -475,24 +489,61 @@ 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);
+ disable_delayed_work(&parent->timestamp);
+
+ 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;
+
+ parent->active_timestamp_channels++;
+ if (parent->active_timestamp_channels > 1)
+ return 0;
+
+ /* 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->initial_timestamp = timestamp;
+ cc->read = gs_usb_timestamp_read_initial;
+ timecounter_init(&parent->tc, &parent->cc, ktime_get_real_ns());
+ cc->read = gs_usb_timestamp_read;
+ }
+
+ enable_delayed_work(&parent->timestamp);
schedule_delayed_work(&parent->timestamp,
GS_USB_TIMESTAMP_WORK_DELAY_SEC * HZ);
+
+ return 0;
}
static void gs_usb_timestamp_stop(struct gs_usb *parent)
{
- cancel_delayed_work_sync(&parent->timestamp);
+ scoped_guard(spinlock_bh, &parent->tc_lock) {
+ parent->active_timestamp_channels--;
+ if (parent->active_timestamp_channels > 0)
+ return;
+ }
+
+ disable_delayed_work_sync(&parent->timestamp);
}
static void gs_update_state(struct gs_can *dev, struct can_frame *cf)
@@ -654,7 +705,7 @@ static void gs_usb_receive_bulk_callback(struct urb *urb)
if (!netif_device_present(netdev))
return;
- if (!netif_running(netdev))
+ if (!netif_running(netdev) || !(netdev->flags & IFF_UP))
goto resubmit_urb;
minimum_length = gs_usb_get_minimum_rx_length(dev, hf, &data_length);
@@ -988,10 +1039,13 @@ static int gs_can_open(struct net_device *netdev)
can_rx_offload_enable(&dev->offload);
- 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;
+ }
+ if (!parent->active_channels) {
for (i = 0; i < GS_MAX_RX_URBS; i++) {
u8 *buf;
@@ -1102,13 +1156,11 @@ static int gs_can_open(struct net_device *netdev)
out_usb_free_urb:
usb_free_urb(urb);
out_usb_kill_anchored_urbs:
- if (!parent->active_channels) {
+ if (!parent->active_channels)
usb_kill_anchored_urbs(&parent->rx_submitted);
-
- if (dev->feature & GS_CAN_FEATURE_HW_TIMESTAMP)
- gs_usb_timestamp_stop(parent);
- }
-
+ 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 +1212,11 @@ static int gs_can_close(struct net_device *netdev)
/* Stop polling */
parent->active_channels--;
- if (!parent->active_channels) {
+ if (!parent->active_channels)
usb_kill_anchored_urbs(&parent->rx_submitted);
- if (dev->feature & GS_CAN_FEATURE_HW_TIMESTAMP)
- gs_usb_timestamp_stop(parent);
- }
+ if (parent->flags & GS_USB_PARENT_FLAGS_HW_TIMESTAMP)
+ gs_usb_timestamp_stop(parent);
/* Stop sending URBs */
usb_kill_anchored_urbs(&dev->tx_submitted);
@@ -1525,6 +1576,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 +1651,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 +1671,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