Skip to content

Commit 5886e4d

Browse files
can: gs_usb: fix time stamp counter initialization
If the gs_usb device driver is unloaded (or unbound) before the interface is shut down, the USB stack first calls the struct usb_driver::disconnect and then the struct net_device_ops::ndo_stop callback. In gs_usb_disconnect() all pending bulk URBs are killed, i.e. no more RX'ed CAN frames are send from the USB device to the host. Later in gs_can_close() a reset control message is send to each CAN channel to remove the controller from the CAN bus. In this race window the USB device can still receive CAN frames from the bus and internally queue them to be send to the host. At least in the current version of the candlelight firmware, the queue of received CAN frames is not emptied during the reset command. After loading (or binding) the gs_usb driver, new URBs are submitted during the struct net_device_ops::ndo_open callback and the candlelight firmware starts sending its already queued CAN frames to the host. However, this scenario was not considered when implementing the hardware timestamp function. The cycle counter/time counter infrastructure is set up (gs_usb_timestamp_init()) after the USBs are submitted, resulting in a NULL pointer dereference if timecounter_cyc2time() (via the call chain: gs_usb_receive_bulk_callback() -> gs_usb_set_timestamp() -> gs_usb_skb_set_timestamp()) is called too early. Move the gs_usb_timestamp_init() function before the URBs are submitted to fix this problem. For a comprehensive solution, we need to consider gs_usb devices with more than 1 channel. The cycle counter/time counter infrastructure is setup per channel, but the RX URBs are per device. Once gs_can_open() of _a_ channel has been called, and URBs have been submitted, the gs_usb_receive_bulk_callback() can be called for _all_ available channels, even for channels that are not running, yet. As cycle counter/time counter has not set up, this will again lead to a NULL pointer dereference. Convert the cycle counter/time counter from a "per channel" to a "per device" functionality. Also set it up, before submitting any URBs to the device. Further in gs_usb_receive_bulk_callback(), don't process any URBs for not started CAN channels, only resubmit the URB. Fixes: 45dfa45 ("can: gs_usb: add RX and TX hardware timestamp support") Closes: candle-usb/candleLight_fw#137 (comment) Cc: [email protected] Cc: John Whittington <[email protected]> Link: https://lore.kernel.org/all/20230716-gs_usb-fix-time-stamp-counter-v1-2-9017cefcd9d5@pengutronix.de Signed-off-by: Marc Kleine-Budde <[email protected]>
1 parent 2603be9 commit 5886e4d

File tree

1 file changed

+53
-48
lines changed

1 file changed

+53
-48
lines changed

drivers/net/can/usb/gs_usb.c

Lines changed: 53 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -303,12 +303,6 @@ struct gs_can {
303303
struct can_bittiming_const bt_const, data_bt_const;
304304
unsigned int channel; /* channel number */
305305

306-
/* time counter for hardware timestamps */
307-
struct cyclecounter cc;
308-
struct timecounter tc;
309-
spinlock_t tc_lock; /* spinlock to guard access tc->cycle_last */
310-
struct delayed_work timestamp;
311-
312306
u32 feature;
313307
unsigned int hf_size_tx;
314308

@@ -325,6 +319,13 @@ struct gs_usb {
325319
struct gs_can *canch[GS_MAX_INTF];
326320
struct usb_anchor rx_submitted;
327321
struct usb_device *udev;
322+
323+
/* time counter for hardware timestamps */
324+
struct cyclecounter cc;
325+
struct timecounter tc;
326+
spinlock_t tc_lock; /* spinlock to guard access tc->cycle_last */
327+
struct delayed_work timestamp;
328+
328329
unsigned int hf_size_rx;
329330
u8 active_channels;
330331
};
@@ -388,15 +389,15 @@ static int gs_cmd_reset(struct gs_can *dev)
388389
GFP_KERNEL);
389390
}
390391

391-
static inline int gs_usb_get_timestamp(const struct gs_can *dev,
392+
static inline int gs_usb_get_timestamp(const struct gs_usb *parent,
392393
u32 *timestamp_p)
393394
{
394395
__le32 timestamp;
395396
int rc;
396397

397-
rc = usb_control_msg_recv(dev->udev, 0, GS_USB_BREQ_TIMESTAMP,
398+
rc = usb_control_msg_recv(parent->udev, 0, GS_USB_BREQ_TIMESTAMP,
398399
USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
399-
dev->channel, 0,
400+
0, 0,
400401
&timestamp, sizeof(timestamp),
401402
USB_CTRL_GET_TIMEOUT,
402403
GFP_KERNEL);
@@ -410,73 +411,74 @@ static inline int gs_usb_get_timestamp(const struct gs_can *dev,
410411

411412
static u64 gs_usb_timestamp_read(const struct cyclecounter *cc) __must_hold(&dev->tc_lock)
412413
{
413-
struct gs_can *dev = container_of(cc, struct gs_can, cc);
414+
struct gs_usb *parent = container_of(cc, struct gs_usb, cc);
414415
u32 timestamp = 0;
415416
int err;
416417

417-
lockdep_assert_held(&dev->tc_lock);
418+
lockdep_assert_held(&parent->tc_lock);
418419

419420
/* drop lock for synchronous USB transfer */
420-
spin_unlock_bh(&dev->tc_lock);
421-
err = gs_usb_get_timestamp(dev, &timestamp);
422-
spin_lock_bh(&dev->tc_lock);
421+
spin_unlock_bh(&parent->tc_lock);
422+
err = gs_usb_get_timestamp(parent, &timestamp);
423+
spin_lock_bh(&parent->tc_lock);
423424
if (err)
424-
netdev_err(dev->netdev,
425-
"Error %d while reading timestamp. HW timestamps may be inaccurate.",
426-
err);
425+
dev_err(&parent->udev->dev,
426+
"Error %d while reading timestamp. HW timestamps may be inaccurate.",
427+
err);
427428

428429
return timestamp;
429430
}
430431

431432
static void gs_usb_timestamp_work(struct work_struct *work)
432433
{
433434
struct delayed_work *delayed_work = to_delayed_work(work);
434-
struct gs_can *dev;
435+
struct gs_usb *parent;
435436

436-
dev = container_of(delayed_work, struct gs_can, timestamp);
437-
spin_lock_bh(&dev->tc_lock);
438-
timecounter_read(&dev->tc);
439-
spin_unlock_bh(&dev->tc_lock);
437+
parent = container_of(delayed_work, struct gs_usb, timestamp);
438+
spin_lock_bh(&parent->tc_lock);
439+
timecounter_read(&parent->tc);
440+
spin_unlock_bh(&parent->tc_lock);
440441

441-
schedule_delayed_work(&dev->timestamp,
442+
schedule_delayed_work(&parent->timestamp,
442443
GS_USB_TIMESTAMP_WORK_DELAY_SEC * HZ);
443444
}
444445

445446
static void gs_usb_skb_set_timestamp(struct gs_can *dev,
446447
struct sk_buff *skb, u32 timestamp)
447448
{
448449
struct skb_shared_hwtstamps *hwtstamps = skb_hwtstamps(skb);
450+
struct gs_usb *parent = dev->parent;
449451
u64 ns;
450452

451-
spin_lock_bh(&dev->tc_lock);
452-
ns = timecounter_cyc2time(&dev->tc, timestamp);
453-
spin_unlock_bh(&dev->tc_lock);
453+
spin_lock_bh(&parent->tc_lock);
454+
ns = timecounter_cyc2time(&parent->tc, timestamp);
455+
spin_unlock_bh(&parent->tc_lock);
454456

455457
hwtstamps->hwtstamp = ns_to_ktime(ns);
456458
}
457459

458-
static void gs_usb_timestamp_init(struct gs_can *dev)
460+
static void gs_usb_timestamp_init(struct gs_usb *parent)
459461
{
460-
struct cyclecounter *cc = &dev->cc;
462+
struct cyclecounter *cc = &parent->cc;
461463

462464
cc->read = gs_usb_timestamp_read;
463465
cc->mask = CYCLECOUNTER_MASK(32);
464466
cc->shift = 32 - bits_per(NSEC_PER_SEC / GS_USB_TIMESTAMP_TIMER_HZ);
465467
cc->mult = clocksource_hz2mult(GS_USB_TIMESTAMP_TIMER_HZ, cc->shift);
466468

467-
spin_lock_init(&dev->tc_lock);
468-
spin_lock_bh(&dev->tc_lock);
469-
timecounter_init(&dev->tc, &dev->cc, ktime_get_real_ns());
470-
spin_unlock_bh(&dev->tc_lock);
469+
spin_lock_init(&parent->tc_lock);
470+
spin_lock_bh(&parent->tc_lock);
471+
timecounter_init(&parent->tc, &parent->cc, ktime_get_real_ns());
472+
spin_unlock_bh(&parent->tc_lock);
471473

472-
INIT_DELAYED_WORK(&dev->timestamp, gs_usb_timestamp_work);
473-
schedule_delayed_work(&dev->timestamp,
474+
INIT_DELAYED_WORK(&parent->timestamp, gs_usb_timestamp_work);
475+
schedule_delayed_work(&parent->timestamp,
474476
GS_USB_TIMESTAMP_WORK_DELAY_SEC * HZ);
475477
}
476478

477-
static void gs_usb_timestamp_stop(struct gs_can *dev)
479+
static void gs_usb_timestamp_stop(struct gs_usb *parent)
478480
{
479-
cancel_delayed_work_sync(&dev->timestamp);
481+
cancel_delayed_work_sync(&parent->timestamp);
480482
}
481483

482484
static void gs_update_state(struct gs_can *dev, struct can_frame *cf)
@@ -560,6 +562,9 @@ static void gs_usb_receive_bulk_callback(struct urb *urb)
560562
if (!netif_device_present(netdev))
561563
return;
562564

565+
if (!netif_running(netdev))
566+
goto resubmit_urb;
567+
563568
if (hf->echo_id == -1) { /* normal rx */
564569
if (hf->flags & GS_CAN_FLAG_FD) {
565570
skb = alloc_canfd_skb(dev->netdev, &cfd);
@@ -856,6 +861,9 @@ static int gs_can_open(struct net_device *netdev)
856861
}
857862

858863
if (!parent->active_channels) {
864+
if (dev->feature & GS_CAN_FEATURE_HW_TIMESTAMP)
865+
gs_usb_timestamp_init(parent);
866+
859867
for (i = 0; i < GS_MAX_RX_URBS; i++) {
860868
u8 *buf;
861869

@@ -926,13 +934,9 @@ static int gs_can_open(struct net_device *netdev)
926934
flags |= GS_CAN_MODE_FD;
927935

928936
/* if hardware supports timestamps, enable it */
929-
if (dev->feature & GS_CAN_FEATURE_HW_TIMESTAMP) {
937+
if (dev->feature & GS_CAN_FEATURE_HW_TIMESTAMP)
930938
flags |= GS_CAN_MODE_HW_TIMESTAMP;
931939

932-
/* start polling timestamp */
933-
gs_usb_timestamp_init(dev);
934-
}
935-
936940
/* finally start device */
937941
dev->can.state = CAN_STATE_ERROR_ACTIVE;
938942
dm.flags = cpu_to_le32(flags);
@@ -942,8 +946,6 @@ static int gs_can_open(struct net_device *netdev)
942946
GFP_KERNEL);
943947
if (rc) {
944948
netdev_err(netdev, "Couldn't start device (err=%d)\n", rc);
945-
if (dev->feature & GS_CAN_FEATURE_HW_TIMESTAMP)
946-
gs_usb_timestamp_stop(dev);
947949
dev->can.state = CAN_STATE_STOPPED;
948950

949951
goto out_usb_kill_anchored_urbs;
@@ -960,9 +962,13 @@ static int gs_can_open(struct net_device *netdev)
960962
out_usb_free_urb:
961963
usb_free_urb(urb);
962964
out_usb_kill_anchored_urbs:
963-
if (!parent->active_channels)
965+
if (!parent->active_channels) {
964966
usb_kill_anchored_urbs(&dev->tx_submitted);
965967

968+
if (dev->feature & GS_CAN_FEATURE_HW_TIMESTAMP)
969+
gs_usb_timestamp_stop(parent);
970+
}
971+
966972
close_candev(netdev);
967973

968974
return rc;
@@ -1011,14 +1017,13 @@ static int gs_can_close(struct net_device *netdev)
10111017

10121018
netif_stop_queue(netdev);
10131019

1014-
/* stop polling timestamp */
1015-
if (dev->feature & GS_CAN_FEATURE_HW_TIMESTAMP)
1016-
gs_usb_timestamp_stop(dev);
1017-
10181020
/* Stop polling */
10191021
parent->active_channels--;
10201022
if (!parent->active_channels) {
10211023
usb_kill_anchored_urbs(&parent->rx_submitted);
1024+
1025+
if (dev->feature & GS_CAN_FEATURE_HW_TIMESTAMP)
1026+
gs_usb_timestamp_stop(parent);
10221027
}
10231028

10241029
/* Stop sending URBs */

0 commit comments

Comments
 (0)