Skip to content

Commit 328e765

Browse files
committed
Merge tag 'linux-can-fixes-for-5.17-20220225' of git://git.kernel.org/pub/scm/linux/kernel/git/mkl/linux-can
Marc Kleine-Budde says: ==================== pull-request: can 2022-02-25 The first 2 patches are by Vincent Mailhol and fix the error handling of the ndo_open callbacks of the etas_es58x and the gs_usb CAN USB drivers. The last patch is by Lad Prabhakar and fixes a small race condition in the rcar_canfd's rcar_canfd_channel_probe() function. * tag 'linux-can-fixes-for-5.17-20220225' of git://git.kernel.org/pub/scm/linux/kernel/git/mkl/linux-can: can: rcar_canfd: rcar_canfd_channel_probe(): register the CAN device when fully ready can: gs_usb: change active_channels's type from atomic_t to u8 can: etas_es58x: change opened_channel_cnt's type from atomic_t to u8 ==================== Link: https://lore.kernel.org/r/20220225165622.3231809-1-mkl@pengutronix.de Signed-off-by: Jakub Kicinski <kuba@kernel.org>
2 parents e01b042 + c5048a7 commit 328e765

4 files changed

Lines changed: 18 additions & 15 deletions

File tree

drivers/net/can/rcar/rcar_canfd.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1715,15 +1715,15 @@ static int rcar_canfd_channel_probe(struct rcar_canfd_global *gpriv, u32 ch,
17151715

17161716
netif_napi_add(ndev, &priv->napi, rcar_canfd_rx_poll,
17171717
RCANFD_NAPI_WEIGHT);
1718+
spin_lock_init(&priv->tx_lock);
1719+
devm_can_led_init(ndev);
1720+
gpriv->ch[priv->channel] = priv;
17181721
err = register_candev(ndev);
17191722
if (err) {
17201723
dev_err(&pdev->dev,
17211724
"register_candev() failed, error %d\n", err);
17221725
goto fail_candev;
17231726
}
1724-
spin_lock_init(&priv->tx_lock);
1725-
devm_can_led_init(ndev);
1726-
gpriv->ch[priv->channel] = priv;
17271727
dev_info(&pdev->dev, "device registered (channel %u)\n", priv->channel);
17281728
return 0;
17291729

drivers/net/can/usb/etas_es58x/es58x_core.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1787,7 +1787,7 @@ static int es58x_open(struct net_device *netdev)
17871787
struct es58x_device *es58x_dev = es58x_priv(netdev)->es58x_dev;
17881788
int ret;
17891789

1790-
if (atomic_inc_return(&es58x_dev->opened_channel_cnt) == 1) {
1790+
if (!es58x_dev->opened_channel_cnt) {
17911791
ret = es58x_alloc_rx_urbs(es58x_dev);
17921792
if (ret)
17931793
return ret;
@@ -1805,12 +1805,13 @@ static int es58x_open(struct net_device *netdev)
18051805
if (ret)
18061806
goto free_urbs;
18071807

1808+
es58x_dev->opened_channel_cnt++;
18081809
netif_start_queue(netdev);
18091810

18101811
return ret;
18111812

18121813
free_urbs:
1813-
if (atomic_dec_and_test(&es58x_dev->opened_channel_cnt))
1814+
if (!es58x_dev->opened_channel_cnt)
18141815
es58x_free_urbs(es58x_dev);
18151816
netdev_err(netdev, "%s: Could not open the network device: %pe\n",
18161817
__func__, ERR_PTR(ret));
@@ -1845,7 +1846,8 @@ static int es58x_stop(struct net_device *netdev)
18451846

18461847
es58x_flush_pending_tx_msg(netdev);
18471848

1848-
if (atomic_dec_and_test(&es58x_dev->opened_channel_cnt))
1849+
es58x_dev->opened_channel_cnt--;
1850+
if (!es58x_dev->opened_channel_cnt)
18491851
es58x_free_urbs(es58x_dev);
18501852

18511853
return 0;
@@ -2215,7 +2217,6 @@ static struct es58x_device *es58x_init_es58x_dev(struct usb_interface *intf,
22152217
init_usb_anchor(&es58x_dev->tx_urbs_idle);
22162218
init_usb_anchor(&es58x_dev->tx_urbs_busy);
22172219
atomic_set(&es58x_dev->tx_urbs_idle_cnt, 0);
2218-
atomic_set(&es58x_dev->opened_channel_cnt, 0);
22192220
usb_set_intfdata(intf, es58x_dev);
22202221

22212222
es58x_dev->rx_pipe = usb_rcvbulkpipe(es58x_dev->udev,

drivers/net/can/usb/etas_es58x/es58x_core.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -373,8 +373,6 @@ struct es58x_operators {
373373
* queue wake/stop logic should prevent this URB from getting
374374
* empty. Please refer to es58x_get_tx_urb() for more details.
375375
* @tx_urbs_idle_cnt: number of urbs in @tx_urbs_idle.
376-
* @opened_channel_cnt: number of channels opened (c.f. es58x_open()
377-
* and es58x_stop()).
378376
* @ktime_req_ns: kernel timestamp when es58x_set_realtime_diff_ns()
379377
* was called.
380378
* @realtime_diff_ns: difference in nanoseconds between the clocks of
@@ -384,6 +382,10 @@ struct es58x_operators {
384382
* in RX branches.
385383
* @rx_max_packet_size: Maximum length of bulk-in URB.
386384
* @num_can_ch: Number of CAN channel (i.e. number of elements of @netdev).
385+
* @opened_channel_cnt: number of channels opened. Free of race
386+
* conditions because its two users (net_device_ops:ndo_open()
387+
* and net_device_ops:ndo_close()) guarantee that the network
388+
* stack big kernel lock (a.k.a. rtnl_mutex) is being hold.
387389
* @rx_cmd_buf_len: Length of @rx_cmd_buf.
388390
* @rx_cmd_buf: The device might split the URB commands in an
389391
* arbitrary amount of pieces. This buffer is used to concatenate
@@ -406,7 +408,6 @@ struct es58x_device {
406408
struct usb_anchor tx_urbs_busy;
407409
struct usb_anchor tx_urbs_idle;
408410
atomic_t tx_urbs_idle_cnt;
409-
atomic_t opened_channel_cnt;
410411

411412
u64 ktime_req_ns;
412413
s64 realtime_diff_ns;
@@ -415,6 +416,7 @@ struct es58x_device {
415416

416417
u16 rx_max_packet_size;
417418
u8 num_can_ch;
419+
u8 opened_channel_cnt;
418420

419421
u16 rx_cmd_buf_len;
420422
union es58x_urb_cmd rx_cmd_buf;

drivers/net/can/usb/gs_usb.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -191,8 +191,8 @@ struct gs_can {
191191
struct gs_usb {
192192
struct gs_can *canch[GS_MAX_INTF];
193193
struct usb_anchor rx_submitted;
194-
atomic_t active_channels;
195194
struct usb_device *udev;
195+
u8 active_channels;
196196
};
197197

198198
/* 'allocate' a tx context.
@@ -589,7 +589,7 @@ static int gs_can_open(struct net_device *netdev)
589589
if (rc)
590590
return rc;
591591

592-
if (atomic_add_return(1, &parent->active_channels) == 1) {
592+
if (!parent->active_channels) {
593593
for (i = 0; i < GS_MAX_RX_URBS; i++) {
594594
struct urb *urb;
595595
u8 *buf;
@@ -690,6 +690,7 @@ static int gs_can_open(struct net_device *netdev)
690690

691691
dev->can.state = CAN_STATE_ERROR_ACTIVE;
692692

693+
parent->active_channels++;
693694
if (!(dev->can.ctrlmode & CAN_CTRLMODE_LISTENONLY))
694695
netif_start_queue(netdev);
695696

@@ -705,7 +706,8 @@ static int gs_can_close(struct net_device *netdev)
705706
netif_stop_queue(netdev);
706707

707708
/* Stop polling */
708-
if (atomic_dec_and_test(&parent->active_channels))
709+
parent->active_channels--;
710+
if (!parent->active_channels)
709711
usb_kill_anchored_urbs(&parent->rx_submitted);
710712

711713
/* Stop sending URBs */
@@ -984,8 +986,6 @@ static int gs_usb_probe(struct usb_interface *intf,
984986

985987
init_usb_anchor(&dev->rx_submitted);
986988

987-
atomic_set(&dev->active_channels, 0);
988-
989989
usb_set_intfdata(intf, dev);
990990
dev->udev = interface_to_usbdev(intf);
991991

0 commit comments

Comments
 (0)