Skip to content

Commit 9abefcb

Browse files
Sergei Miroshnichenkomarckleinebudde
authored andcommitted
can: dev: fix deadlock reported after bus-off
A timer was used to restart after the bus-off state, leading to a relatively large can_restart() executed in an interrupt context, which in turn sets up pinctrl. When this happens during system boot, there is a high probability of grabbing the pinctrl_list_mutex, which is locked already by the probe() of other device, making the kernel suspect a deadlock condition [1]. To resolve this issue, the restart_timer is replaced by a delayed work. [1] victronenergy/venus#24 Signed-off-by: Sergei Miroshnichenko <sergeimir@emcraft.com> Cc: linux-stable <stable@vger.kernel.org> Signed-off-by: Marc Kleine-Budde <mkl@pengutronix.de>
1 parent 7e32b44 commit 9abefcb

2 files changed

Lines changed: 19 additions & 11 deletions

File tree

‎drivers/net/can/dev.c‎

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include <linux/slab.h>
2222
#include <linux/netdevice.h>
2323
#include <linux/if_arp.h>
24+
#include <linux/workqueue.h>
2425
#include <linux/can.h>
2526
#include <linux/can/dev.h>
2627
#include <linux/can/skb.h>
@@ -501,9 +502,8 @@ EXPORT_SYMBOL_GPL(can_free_echo_skb);
501502
/*
502503
* CAN device restart for bus-off recovery
503504
*/
504-
static void can_restart(unsigned long data)
505+
static void can_restart(struct net_device *dev)
505506
{
506-
struct net_device *dev = (struct net_device *)data;
507507
struct can_priv *priv = netdev_priv(dev);
508508
struct net_device_stats *stats = &dev->stats;
509509
struct sk_buff *skb;
@@ -543,6 +543,14 @@ static void can_restart(unsigned long data)
543543
netdev_err(dev, "Error %d during restart", err);
544544
}
545545

546+
static void can_restart_work(struct work_struct *work)
547+
{
548+
struct delayed_work *dwork = to_delayed_work(work);
549+
struct can_priv *priv = container_of(dwork, struct can_priv, restart_work);
550+
551+
can_restart(priv->dev);
552+
}
553+
546554
int can_restart_now(struct net_device *dev)
547555
{
548556
struct can_priv *priv = netdev_priv(dev);
@@ -556,8 +564,8 @@ int can_restart_now(struct net_device *dev)
556564
if (priv->state != CAN_STATE_BUS_OFF)
557565
return -EBUSY;
558566

559-
/* Runs as soon as possible in the timer context */
560-
mod_timer(&priv->restart_timer, jiffies);
567+
cancel_delayed_work_sync(&priv->restart_work);
568+
can_restart(dev);
561569

562570
return 0;
563571
}
@@ -578,8 +586,8 @@ void can_bus_off(struct net_device *dev)
578586
netif_carrier_off(dev);
579587

580588
if (priv->restart_ms)
581-
mod_timer(&priv->restart_timer,
582-
jiffies + (priv->restart_ms * HZ) / 1000);
589+
schedule_delayed_work(&priv->restart_work,
590+
msecs_to_jiffies(priv->restart_ms));
583591
}
584592
EXPORT_SYMBOL_GPL(can_bus_off);
585593

@@ -688,6 +696,7 @@ struct net_device *alloc_candev(int sizeof_priv, unsigned int echo_skb_max)
688696
return NULL;
689697

690698
priv = netdev_priv(dev);
699+
priv->dev = dev;
691700

692701
if (echo_skb_max) {
693702
priv->echo_skb_max = echo_skb_max;
@@ -697,7 +706,7 @@ struct net_device *alloc_candev(int sizeof_priv, unsigned int echo_skb_max)
697706

698707
priv->state = CAN_STATE_STOPPED;
699708

700-
init_timer(&priv->restart_timer);
709+
INIT_DELAYED_WORK(&priv->restart_work, can_restart_work);
701710

702711
return dev;
703712
}
@@ -778,8 +787,6 @@ int open_candev(struct net_device *dev)
778787
if (!netif_carrier_ok(dev))
779788
netif_carrier_on(dev);
780789

781-
setup_timer(&priv->restart_timer, can_restart, (unsigned long)dev);
782-
783790
return 0;
784791
}
785792
EXPORT_SYMBOL_GPL(open_candev);
@@ -794,7 +801,7 @@ void close_candev(struct net_device *dev)
794801
{
795802
struct can_priv *priv = netdev_priv(dev);
796803

797-
del_timer_sync(&priv->restart_timer);
804+
cancel_delayed_work_sync(&priv->restart_work);
798805
can_flush_echo_skb(dev);
799806
}
800807
EXPORT_SYMBOL_GPL(close_candev);

‎include/linux/can/dev.h‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ enum can_mode {
3232
* CAN common private data
3333
*/
3434
struct can_priv {
35+
struct net_device *dev;
3536
struct can_device_stats can_stats;
3637

3738
struct can_bittiming bittiming, data_bittiming;
@@ -47,7 +48,7 @@ struct can_priv {
4748
u32 ctrlmode_static; /* static enabled options for driver/hardware */
4849

4950
int restart_ms;
50-
struct timer_list restart_timer;
51+
struct delayed_work restart_work;
5152

5253
int (*do_set_bittiming)(struct net_device *dev);
5354
int (*do_set_data_bittiming)(struct net_device *dev);

0 commit comments

Comments
 (0)