From: Marc Kleine-Budde <mkl@pengutronix.de>
To: netdev@vger.kernel.org
Cc: davem@davemloft.net, kuba@kernel.org, linux-can@vger.kernel.org,
kernel@pengutronix.de,
Shuangpeng Bai <shuangpeng.kernel@gmail.com>,
stable@vger.kernel.org,
Vadim Fedorenko <vadim.fedorenko@linux.dev>,
Marc Kleine-Budde <mkl@pengutronix.de>
Subject: [PATCH net-next 5/9] can: gs_usb: fix hardware timestamp state for mixed channels
Date: Mon, 30 Nov 2026 10:15:31 +0100 [thread overview]
Message-ID: <20261130092003.1946901-6-mkl@pengutronix.de> (raw)
In-Reply-To: <20261130092003.1946901-1-mkl@pengutronix.de>
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 are tied to the flags of the first channel opened
respectively last channel closed.
If the first channel opened doesn't have the timestamping feature, but the
second does, gs_usb_timestamp_init() never runs. This leads to a NULL
pointer dereference in timecounter_cyc2time() in URB completion for the
second channel.
If the first channel opens has the timestamping feature, but the last one
closing doesn't, gs_usb_timestamp_stop() is not called, but parent is
freed. This leads to a use-after-free in the still scheduled worker.
This patch also reworks the locking around timecounter/cyclecounter. There
are competing requirements:
- the struct cyclecounter::read callback uses a sleeping USB control
transfer
- the updates during timecounter_read() must be atomic
- timecounter_cyc2time() is used from URB completion callback, which
run in BH or even IRQ context
To fix this issue, let timecounter_cyc2time() works on a snapshot of struct
cyclecounter/struct timecounter. Serialize the update of the snapshot with
a sequence counter. Update the snapshot after the timecounter_init() and in
the worker after timecounter_read().
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.
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>
Link: https://patch.msgid.link/20261130-gs_usb-fixes-v12-5-9ba1f84c179a@pengutronix.de
Signed-off-by: Marc Kleine-Budde <mkl@pengutronix.de>
---
drivers/net/can/usb/gs_usb.c | 87 +++++++++++++++++++++++++-----------
1 file changed, 61 insertions(+), 26 deletions(-)
diff --git a/drivers/net/can/usb/gs_usb.c b/drivers/net/can/usb/gs_usb.c
index 890389a1fa31..ccdcb264c7f5 100644
--- a/drivers/net/can/usb/gs_usb.c
+++ b/drivers/net/can/usb/gs_usb.c
@@ -324,19 +324,29 @@ struct gs_can {
atomic_t active_tx_urbs;
};
+enum gs_usb_parent_flags {
+ GS_USB_PARENT_FLAGS_HW_TIMESTAMP = BIT(0),
+};
+
+struct gs_usb_timer {
+ struct cyclecounter cc;
+ struct timecounter tc;
+};
+
/* usb interface struct */
struct gs_usb {
struct usb_anchor rx_submitted;
struct usb_device *udev;
/* time counter for hardware timestamps */
- struct cyclecounter cc;
- struct timecounter tc;
- spinlock_t tc_lock; /* spinlock to guard access tc->cycle_last */
+ struct gs_usb_timer tc_active;
+ struct gs_usb_timer tc_snapshot;
+ seqlock_t tc_lock; /* seqlock to guard access tc_snapshot */
struct delayed_work timestamp;
unsigned int hf_size_rx;
u8 active_channels;
+ u8 flags;
u8 channel_cnt;
unsigned int pipe_in;
@@ -423,18 +433,13 @@ 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(struct cyclecounter *cc)
{
- struct gs_usb *parent = container_of(cc, struct gs_usb, cc);
+ struct gs_usb *parent = container_of(cc, struct gs_usb, tc_active.cc);
u32 timestamp = 0;
int err;
- lockdep_assert_held(&parent->tc_lock);
-
- /* drop lock for synchronous USB transfer */
- spin_unlock_bh(&parent->tc_lock);
err = gs_usb_get_timestamp(parent, ×tamp);
- spin_lock_bh(&parent->tc_lock);
if (err)
dev_err(&parent->udev->dev,
"Error %d while reading timestamp. HW timestamps may be inaccurate.",
@@ -443,15 +448,24 @@ static u64 gs_usb_timestamp_read(struct cyclecounter *cc) __must_hold(&dev->tc_l
return timestamp;
}
+static void gs_usb_timestamp_do_snapshot(struct gs_usb *parent)
+{
+ unsigned long flags;
+
+ write_seqlock_irqsave(&parent->tc_lock, flags);
+ parent->tc_snapshot = parent->tc_active;
+ parent->tc_snapshot.tc.cc = &parent->tc_snapshot.cc;
+ write_sequnlock_irqrestore(&parent->tc_lock, flags);
+}
+
static void gs_usb_timestamp_work(struct work_struct *work)
{
struct delayed_work *delayed_work = to_delayed_work(work);
struct gs_usb *parent;
parent = container_of(delayed_work, struct gs_usb, timestamp);
- spin_lock_bh(&parent->tc_lock);
- timecounter_read(&parent->tc);
- spin_unlock_bh(&parent->tc_lock);
+ timecounter_read(&parent->tc_active.tc);
+ gs_usb_timestamp_do_snapshot(parent);
schedule_delayed_work(&parent->timestamp,
GS_USB_TIMESTAMP_WORK_DELAY_SEC * HZ);
@@ -461,31 +475,39 @@ static void gs_usb_skb_set_timestamp(struct gs_can *dev,
struct sk_buff *skb, u32 timestamp)
{
struct skb_shared_hwtstamps *hwtstamps = skb_hwtstamps(skb);
- struct gs_usb *parent = dev->parent;
+ const struct gs_usb *parent = dev->parent;
+ unsigned int seq;
u64 ns;
- spin_lock_bh(&parent->tc_lock);
- ns = timecounter_cyc2time(&parent->tc, timestamp);
- spin_unlock_bh(&parent->tc_lock);
+ do {
+ seq = read_seqbegin(&parent->tc_lock);
+ ns = timecounter_cyc2time(&parent->tc_snapshot.tc, timestamp);
+ } while (read_seqretry(&parent->tc_lock, seq));
hwtstamps->hwtstamp = ns_to_ktime(ns);
}
static void gs_usb_timestamp_init(struct gs_usb *parent)
{
- struct cyclecounter *cc = &parent->cc;
+ struct cyclecounter *cc = &parent->tc_active.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);
+ seqlock_init(&parent->tc_lock);
INIT_DELAYED_WORK(&parent->timestamp, gs_usb_timestamp_work);
+
+ parent->flags |= GS_USB_PARENT_FLAGS_HW_TIMESTAMP;
+}
+
+static void gs_usb_timestamp_start(struct gs_usb *parent)
+{
+ timecounter_init(&parent->tc_active.tc, &parent->tc_active.cc, ktime_get_real_ns());
+ gs_usb_timestamp_do_snapshot(parent);
+
schedule_delayed_work(&parent->timestamp,
GS_USB_TIMESTAMP_WORK_DELAY_SEC * HZ);
}
@@ -989,9 +1011,12 @@ 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)
+ gs_usb_timestamp_start(parent);
for (i = 0; i < GS_MAX_RX_URBS; i++) {
u8 *buf;
@@ -1106,7 +1131,7 @@ 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);
}
@@ -1159,12 +1184,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);
}
@@ -1523,6 +1551,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;
@@ -1597,6 +1626,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.
*/
@@ -1614,6 +1646,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
next prev parent reply other threads:[~2026-11-30 9:20 UTC|newest]
Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-30 9:15 [PATCH net-next 0/9] pull-request: can-next 2026-11-30 Marc Kleine-Budde
2026-11-30 9:15 ` [PATCH net-next 1/9] can: gs_usb: gs_usb_probe(): init netdev completely before register_candev() Marc Kleine-Budde
2026-11-30 9:23 ` netdev-bot+sinfo
2026-11-30 9:15 ` [PATCH net-next 2/9] can: gs_usb: gs_usb_receive_bulk_callback(): update state even if SKB allocations fails Marc Kleine-Budde
2026-11-30 9:15 ` [PATCH net-next 3/9] can: gs_usb: gs_usb_receive_bulk_callback(): check for overflow flag if SKB allocation fails Marc Kleine-Budde
2026-11-30 9:15 ` [PATCH net-next 4/9] can: gs_usb: gs_usb_receive_bulk_callback(): add missing call to can_rx_offload_irq_finish Marc Kleine-Budde
2026-11-30 9:15 ` Marc Kleine-Budde [this message]
2026-11-30 9:15 ` [PATCH net-next 6/9] can: gs_usb: add support for context analysis Marc Kleine-Budde
2026-11-30 9:15 ` [PATCH net-next 7/9] can: gs_usb: gs_make_candev(): check can_rx_offload_add_manual() return value Marc Kleine-Budde
2026-11-30 9:15 ` [PATCH net-next 8/9] can: gs_usb: gs_usb_receive_bulk_callback(): use dev_info_ratelimited() to print re-submit info messages Marc Kleine-Budde
2026-11-30 9:15 ` [PATCH net-next 9/9] can: gs_usb: gs_usb_receive_bulk_callback(): emit error message on invalid channel instead of disconnecting interfaces Marc Kleine-Budde
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261130092003.1946901-6-mkl@pengutronix.de \
--to=mkl@pengutronix.de \
--cc=davem@davemloft.net \
--cc=kernel@pengutronix.de \
--cc=kuba@kernel.org \
--cc=linux-can@vger.kernel.org \
--cc=netdev@vger.kernel.org \
--cc=shuangpeng.kernel@gmail.com \
--cc=stable@vger.kernel.org \
--cc=vadim.fedorenko@linux.dev \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox