Commit 25405d98 authored by Jason Gunthorpe's avatar Jason Gunthorpe

IB/ipoib: Do not remove child devices from within the ndo_uninit

Switching to priv_destructor and needs_free_netdev created a subtle
ordering problem in ipoib_remove_one.

Now that unregister_netdev frees the netdev and priv we must ensure that
the children are unregistered before trying to unregister the parent,
or child unregister will use after free.

The solution is to unregister the children, then parent, in the same batch
all while holding the rtnl_lock. This closes all the races where a new
child could have been added and ensures proper ordering.
Signed-off-by: default avatarJason Gunthorpe <jgg@mellanox.com>
Signed-off-by: default avatarLeon Romanovsky <leonro@mellanox.com>
parent ee190ab7
...@@ -330,6 +330,13 @@ struct ipoib_dev_priv { ...@@ -330,6 +330,13 @@ struct ipoib_dev_priv {
unsigned long flags; unsigned long flags;
/*
* This protects access to the child_intfs list.
* To READ from child_intfs the RTNL or vlan_rwsem read side must be
* held. To WRITE RTNL and the vlan_rwsem write side must be held (in
* that order) This lock exists because we have a few contexts where
* we need the child_intfs, but do not want to grab the RTNL.
*/
struct rw_semaphore vlan_rwsem; struct rw_semaphore vlan_rwsem;
struct mutex mcast_mutex; struct mutex mcast_mutex;
......
...@@ -1939,18 +1939,15 @@ static int ipoib_ndo_init(struct net_device *ndev) ...@@ -1939,18 +1939,15 @@ static int ipoib_ndo_init(struct net_device *ndev)
static void ipoib_ndo_uninit(struct net_device *dev) static void ipoib_ndo_uninit(struct net_device *dev)
{ {
struct ipoib_dev_priv *priv = ipoib_priv(dev), *cpriv, *tcpriv; struct ipoib_dev_priv *priv = ipoib_priv(dev);
LIST_HEAD(head);
ASSERT_RTNL(); ASSERT_RTNL();
/* Delete any child interfaces first */ /*
list_for_each_entry_safe(cpriv, tcpriv, &priv->child_intfs, list) { * ipoib_remove_one guarantees the children are removed before the
/* Stop GC on child */ * parent, and that is the only place where a parent can be removed.
cancel_delayed_work_sync(&cpriv->neigh_reap_task); */
unregister_netdevice_queue(cpriv->dev, &head); WARN_ON(!list_empty(&priv->child_intfs));
}
unregister_netdevice_many(&head);
ipoib_neigh_hash_uninit(dev); ipoib_neigh_hash_uninit(dev);
...@@ -2466,16 +2463,25 @@ static void ipoib_add_one(struct ib_device *device) ...@@ -2466,16 +2463,25 @@ static void ipoib_add_one(struct ib_device *device)
static void ipoib_remove_one(struct ib_device *device, void *client_data) static void ipoib_remove_one(struct ib_device *device, void *client_data)
{ {
struct ipoib_dev_priv *priv, *tmp; struct ipoib_dev_priv *priv, *tmp, *cpriv, *tcpriv;
struct list_head *dev_list = client_data; struct list_head *dev_list = client_data;
if (!dev_list) if (!dev_list)
return; return;
list_for_each_entry_safe(priv, tmp, dev_list, list) { list_for_each_entry_safe(priv, tmp, dev_list, list) {
LIST_HEAD(head);
ipoib_parent_unregister_pre(priv->dev); ipoib_parent_unregister_pre(priv->dev);
unregister_netdev(priv->dev); rtnl_lock();
list_for_each_entry_safe(cpriv, tcpriv, &priv->child_intfs,
list)
unregister_netdevice_queue(cpriv->dev, &head);
unregister_netdevice_queue(priv->dev, &head);
unregister_netdevice_many(&head);
rtnl_unlock();
} }
kfree(dev_list); kfree(dev_list);
......
...@@ -67,6 +67,12 @@ int __ipoib_vlan_add(struct ipoib_dev_priv *ppriv, struct ipoib_dev_priv *priv, ...@@ -67,6 +67,12 @@ int __ipoib_vlan_add(struct ipoib_dev_priv *ppriv, struct ipoib_dev_priv *priv,
ASSERT_RTNL(); ASSERT_RTNL();
/*
* Racing with unregister of the parent must be prevented by the
* caller.
*/
WARN_ON(ppriv->dev->reg_state != NETREG_REGISTERED);
priv->parent = ppriv->dev; priv->parent = ppriv->dev;
priv->pkey = pkey; priv->pkey = pkey;
priv->child_type = type; priv->child_type = type;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment