From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=pass header.oldest-pass=0 smtp.remote-ip=185.203.200.13 ARC-Seal: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=pass; t=1796030410; b=0oP3/S0wmo0OtGNxgZu6fdDFWMES/0RhAANfLtcj0nwr9lEw3xFXlKNOEgIFgfGIvJxi o7BU4CX6H3jwHnwSJ2A6ZklEZYgbjvIDnikJytfTCfc1dnKlnF/lHVyFKbW0HEy62CNjz Rnn4Wg/86tR45mFx+Gs04u9J3cQE4YJ3CkqEYsgQRVQbhbPxaOgbSFx/97jPwUehcL9tJ tZopGXrC/LwBB96hUbOn7YbIPvXm/t+KJ/J7WdTRRDLQyePBppr0gEqErBCjuTx0P9RL9 sdJ+E/+uS5un8/vEHSIO5mPJlwyfygTZbFyE1qyB4DLeouz/BeZUNM3C5NG48KU6h4A== ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796030410; h=DMARC-Filter:Received:DKIM-Signature:From:To:Cc:Subject:Date: Message-ID:X-Mailer:In-Reply-To:References:MIME-Version: Content-Transfer-Encoding; bh=iy2/YiKfKmRJKM6gyaNXT6Q5gQRWGYBB9xQNK6qn2DU=; b=C3/hBmfmLGfpMQpw9XIbBehO+/XdjN03dxRNqDeYWGm3MPaqVhlMP/d13U4DjBSpRWF9 29S/eN95hJyEYlru6qaTNDPIaopGW5QMxedSIqsoZhfMvdVlsBIoZ8uSclre7lPqR0wxW F0sWo0eUE2ZiQIzV1MvwNK6p7YfyD+SRSSiaWrjb5ya9s9wdFMPIH1ae2aNxu6VHUR9kv naUoLqRiADFTYuk0rcgn0kVt518CnHKnfQ419Wo1vN9QgEPN6I3BcVB5SbTyhWSYO7ToT zFUeuWLAAA4d+za/1OX6YnzSstiMQEESrxbztiS6TTKcF94DOV+qSVTYY63+kWyeGoQ== ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=none header.from=pengutronix.de; spf=pass smtp.mailfrom=pengutronix.de; dkim=pass header.d=pengutronix.de header.i=@pengutronix.de header.a=rsa-sha256 header.s=20260414 header.b=N71uAJUF; arc=pass header.oldest-pass=0 smtp.remote-ip=185.203.200.13 Received: from mx1.white.stw.pengutronix.de (mx1.white.stw.pengutronix.de [185.203.200.13]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id DBF8D606A68F; Mon, 30 Nov 2026 09:20:09 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org DBF8D606A68F Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=pengutronix.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=pengutronix.de Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=pengutronix.de header.i=@pengutronix.de header.a=rsa-sha256 header.s=20260414 header.b=N71uAJUF Received: from pengutronix.de (p20030102af17cb000b6919cc16900304.dip0.t-ipconnect.de [IPv6:2003:102:af17:cb00:b69:19cc:1690:304]) (Authenticated sender: mkl@pengutronix.de) by mx1.white.stw.pengutronix.de (Postfix) with ESMTPSA id 1D645205AFE; Mon, 30 Nov 2026 10:20:06 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=pengutronix.de; s=20260414; t=1796030406; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=iy2/YiKfKmRJKM6gyaNXT6Q5gQRWGYBB9xQNK6qn2DU=; b=N71uAJUFWTMQOPb164dx2M+wnC/6wiDoSdQbXznPTW2vXgiJXttNrA32PLowQXeSollUVy g/hbCrh/3ODb751Y+Is8Tode/I3OW+ywvxcd4gdJdOuV3iLeQXtzmaGA3PAVbKB18C7rI0 e3XhXQRbPZ1DSxtz8eIASw5p7E426t7ikRolopzQzss/YQTPNucnlf5T/tHOroeW2c+0iz q1hlYM30gsU7J/jJwKNdHSnueOZyRlyfWXjbKNQe4ai5CbqndyZ33K2jlHzzVxhm1OfGGv bEoCEKXffsXnQlYxSgVWSVqyUrZJU7KyxOW0WCZDQZtbx2mvJnc4bLM/FD0rAg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=pengutronix.de; s=20260414; t=1796030406; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=iy2/YiKfKmRJKM6gyaNXT6Q5gQRWGYBB9xQNK6qn2DU=; b=LugDeoBVYnvsIKtS9Bd5+k1T4/qcXQVOYH1LdBfjyTqmvQiKj+UDG4bqQaTpyAZ+QONLWA NRJjDcuG0urDhxOppD98CRSB+OuOkVrsmpi9rQioBYC3D5EIdLdhhJIL9FthSIXseBNMiv chC9dccFZ5e2brVFfix+A+uro3dsxW+ZIEXB52PQSE0jfDQLZzrjtjvEGP7ePEonWz4V8i 0ld/FcPhafcDf2cCCRuGO74nxEtfKWcmDS5+DYt0/3ACiEvJp8vue7i90tc9r/NPNJE/a5 seULWeSbxT3T2oF2EqrvjGQYFWJv5G24EGiIUo1GvmuCeHsikfEYVX82olBztA== ARC-Authentication-Results: i=1; ORIGINATING; auth=pass smtp.auth=mkl@pengutronix.de smtp.mailfrom=mkl@pengutronix.de ARC-Seal: i=1; s=20260414; d=pengutronix.de; t=1796030406; a=rsa-sha256; cv=none; b=GR1Wq8S092OrnDefoM7d0XMCD8IVsOufoGFM/jhKhJs8/ytT0JuinNSmwflj9YVYYlddSe prwsthdcZn+I9FOrxkJx5dp/S2upNk3L+mQaeCUssqF/CHD0IAwwYf4VrH5RCnENicvMAJ 2bLjXfMkMmKdLKZmleKOLjVOGyBcoOUYmnJez9bvEtWPRN0zVAcXwOWQ2dTcGbVMOpO4U6 gVhIDdZSRWTHh8fAC6L2FjG/WAPnJo/XgBVXt7Wc6QHHD40Kn5IeYezUMh2sTNptzNLcBO 8WDJEmnBSRDk0ACtGHOefgrLg0X6+7v6USPZA6Bsaajut86pkqe/1ET6VqBkwQ== From: Marc Kleine-Budde To: netdev@vger.kernel.org Cc: davem@davemloft.net, kuba@kernel.org, linux-can@vger.kernel.org, kernel@pengutronix.de, Shuangpeng Bai , stable@vger.kernel.org, Vadim Fedorenko , Marc Kleine-Budde 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 Message-ID: <20261130092003.1946901-6-mkl@pengutronix.de> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20261130092003.1946901-1-mkl@pengutronix.de> References: <20261130092003.1946901-1-mkl@pengutronix.de> X-Mailing-List: netdev@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit From: Shuangpeng Bai 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 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 Cc: Vadim Fedorenko Signed-off-by: Shuangpeng Bai Co-developed-by: Marc Kleine-Budde Link: https://patch.msgid.link/20261130-gs_usb-fixes-v12-5-9ba1f84c179a@pengutronix.de Signed-off-by: Marc Kleine-Budde --- 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