Skip to content

Commit 5bc44dc

Browse files
Simon WunderlichAntonio Quartulli
authored andcommitted
batman-adv: postpone sysfs removal when unregistering
When processing the unregister notify for a hard interface, removing the sysfs files may lead to a circular deadlock (rtnl mutex <-> s_active). To overcome this problem, postpone the sysfs removal in a worker. Reported-by: Sasha Levin <sasha.levin@oracle.com> Reported-by: Sven Eckelmann <sven@narfation.org> Signed-off-by: Simon Wunderlich <siwu@hrz.tu-chemnitz.de> Signed-off-by: Marek Lindner <lindner_marek@yahoo.de> Signed-off-by: Antonio Quartulli <ordex@autistici.org>
1 parent e76e432 commit 5bc44dc

3 files changed

Lines changed: 57 additions & 5 deletions

File tree

‎net/batman-adv/hard-interface.c‎

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -457,6 +457,24 @@ void batadv_hardif_disable_interface(struct batadv_hard_iface *hard_iface)
457457
batadv_hardif_free_ref(primary_if);
458458
}
459459

460+
/**
461+
* batadv_hardif_remove_interface_finish - cleans up the remains of a hardif
462+
* @work: work queue item
463+
*
464+
* Free the parts of the hard interface which can not be removed under
465+
* rtnl lock (to prevent deadlock situations).
466+
*/
467+
static void batadv_hardif_remove_interface_finish(struct work_struct *work)
468+
{
469+
struct batadv_hard_iface *hard_iface;
470+
471+
hard_iface = container_of(work, struct batadv_hard_iface,
472+
cleanup_work);
473+
474+
batadv_sysfs_del_hardif(&hard_iface->hardif_obj);
475+
batadv_hardif_free_ref(hard_iface);
476+
}
477+
460478
static struct batadv_hard_iface *
461479
batadv_hardif_add_interface(struct net_device *net_dev)
462480
{
@@ -484,6 +502,9 @@ batadv_hardif_add_interface(struct net_device *net_dev)
484502
hard_iface->soft_iface = NULL;
485503
hard_iface->if_status = BATADV_IF_NOT_IN_USE;
486504
INIT_LIST_HEAD(&hard_iface->list);
505+
INIT_WORK(&hard_iface->cleanup_work,
506+
batadv_hardif_remove_interface_finish);
507+
487508
/* extra reference for return */
488509
atomic_set(&hard_iface->refcount, 2);
489510

@@ -518,8 +539,7 @@ static void batadv_hardif_remove_interface(struct batadv_hard_iface *hard_iface)
518539
return;
519540

520541
hard_iface->if_status = BATADV_IF_TO_BE_REMOVED;
521-
batadv_sysfs_del_hardif(&hard_iface->hardif_obj);
522-
batadv_hardif_free_ref(hard_iface);
542+
queue_work(batadv_event_workqueue, &hard_iface->cleanup_work);
523543
}
524544

525545
void batadv_hardif_remove_interfaces(void)

‎net/batman-adv/soft-interface.c‎

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -449,6 +449,30 @@ static void batadv_interface_setup(struct net_device *dev)
449449
memset(priv, 0, sizeof(*priv));
450450
}
451451

452+
/**
453+
* batadv_softif_destroy_finish - cleans up the remains of a softif
454+
* @work: work queue item
455+
*
456+
* Free the parts of the soft interface which can not be removed under
457+
* rtnl lock (to prevent deadlock situations).
458+
*/
459+
static void batadv_softif_destroy_finish(struct work_struct *work)
460+
{
461+
struct batadv_priv *bat_priv;
462+
struct net_device *soft_iface;
463+
464+
bat_priv = container_of(work, struct batadv_priv,
465+
cleanup_work);
466+
soft_iface = bat_priv->soft_iface;
467+
468+
batadv_debugfs_del_meshif(soft_iface);
469+
batadv_sysfs_del_meshif(soft_iface);
470+
471+
rtnl_lock();
472+
unregister_netdevice(soft_iface);
473+
rtnl_unlock();
474+
}
475+
452476
struct net_device *batadv_softif_create(const char *name)
453477
{
454478
struct net_device *soft_iface;
@@ -463,6 +487,8 @@ struct net_device *batadv_softif_create(const char *name)
463487
goto out;
464488

465489
bat_priv = netdev_priv(soft_iface);
490+
bat_priv->soft_iface = soft_iface;
491+
INIT_WORK(&bat_priv->cleanup_work, batadv_softif_destroy_finish);
466492

467493
/* batadv_interface_stats() needs to be available as soon as
468494
* register_netdevice() has been called
@@ -551,10 +577,10 @@ struct net_device *batadv_softif_create(const char *name)
551577

552578
void batadv_softif_destroy(struct net_device *soft_iface)
553579
{
554-
batadv_debugfs_del_meshif(soft_iface);
555-
batadv_sysfs_del_meshif(soft_iface);
580+
struct batadv_priv *bat_priv = netdev_priv(soft_iface);
581+
556582
batadv_mesh_free(soft_iface);
557-
unregister_netdevice(soft_iface);
583+
queue_work(batadv_event_workqueue, &bat_priv->cleanup_work);
558584
}
559585

560586
int batadv_softif_is_valid(const struct net_device *net_dev)

‎net/batman-adv/types.h‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ struct batadv_hard_iface_bat_iv {
6868
* @soft_iface: the batman-adv interface which uses this network interface
6969
* @rcu: struct used for freeing in an RCU-safe manner
7070
* @bat_iv: BATMAN IV specific per hard interface data
71+
* @cleanup_work: work queue callback item for hard interface deinit
7172
*/
7273
struct batadv_hard_iface {
7374
struct list_head list;
@@ -81,6 +82,7 @@ struct batadv_hard_iface {
8182
struct net_device *soft_iface;
8283
struct rcu_head rcu;
8384
struct batadv_hard_iface_bat_iv bat_iv;
85+
struct work_struct cleanup_work;
8486
};
8587

8688
/**
@@ -428,6 +430,7 @@ struct batadv_priv_dat {
428430
/**
429431
* struct batadv_priv - per mesh interface data
430432
* @mesh_state: current status of the mesh (inactive/active/deactivating)
433+
* @soft_iface: net device which holds this struct as private data
431434
* @stats: structure holding the data for the ndo_get_stats() call
432435
* @bat_counters: mesh internal traffic statistic counters (see batadv_counters)
433436
* @aggregated_ogms: bool indicating whether OGM aggregation is enabled
@@ -457,6 +460,7 @@ struct batadv_priv_dat {
457460
* @forw_bat_list_lock: lock protecting forw_bat_list
458461
* @forw_bcast_list_lock: lock protecting forw_bcast_list
459462
* @orig_work: work queue callback item for orig node purging
463+
* @cleanup_work: work queue callback item for soft interface deinit
460464
* @primary_if: one of the hard interfaces assigned to this mesh interface
461465
* becomes the primary interface
462466
* @bat_algo_ops: routing algorithm used by this mesh interface
@@ -469,6 +473,7 @@ struct batadv_priv_dat {
469473
*/
470474
struct batadv_priv {
471475
atomic_t mesh_state;
476+
struct net_device *soft_iface;
472477
struct net_device_stats stats;
473478
uint64_t __percpu *bat_counters; /* Per cpu counters */
474479
atomic_t aggregated_ogms;
@@ -502,6 +507,7 @@ struct batadv_priv {
502507
spinlock_t forw_bat_list_lock; /* protects forw_bat_list */
503508
spinlock_t forw_bcast_list_lock; /* protects forw_bcast_list */
504509
struct delayed_work orig_work;
510+
struct work_struct cleanup_work;
505511
struct batadv_hard_iface __rcu *primary_if; /* rcu protected pointer */
506512
struct batadv_algo_ops *bat_algo_ops;
507513
#ifdef CONFIG_BATMAN_ADV_BLA

0 commit comments

Comments
 (0)