Commit c2c63310 authored by Andrea Righi's avatar Andrea Righi Committed by David S. Miller

xen-netfront: fix potential deadlock in xennet_remove()

There's a potential race in xennet_remove(); this is what the driver is
doing upon unregistering a network device:

  1. state = read bus state
  2. if state is not "Closed":
  3.    request to set state to "Closing"
  4.    wait for state to be set to "Closing"
  5.    request to set state to "Closed"
  6.    wait for state to be set to "Closed"

If the state changes to "Closed" immediately after step 1 we are stuck
forever in step 4, because the state will never go back from "Closed" to
"Closing".

Make sure to check also for state == "Closed" in step 4 to prevent the
deadlock.

Also add a 5 sec timeout any time we wait for the bus state to change,
to avoid getting stuck forever in wait_event().
Signed-off-by: default avatarAndrea Righi <andrea.righi@canonical.com>
Signed-off-by: default avatarDavid S. Miller <davem@davemloft.net>
parent c2b69f24
...@@ -63,6 +63,8 @@ module_param_named(max_queues, xennet_max_queues, uint, 0644); ...@@ -63,6 +63,8 @@ module_param_named(max_queues, xennet_max_queues, uint, 0644);
MODULE_PARM_DESC(max_queues, MODULE_PARM_DESC(max_queues,
"Maximum number of queues per virtual interface"); "Maximum number of queues per virtual interface");
#define XENNET_TIMEOUT (5 * HZ)
static const struct ethtool_ops xennet_ethtool_ops; static const struct ethtool_ops xennet_ethtool_ops;
struct netfront_cb { struct netfront_cb {
...@@ -1334,12 +1336,15 @@ static struct net_device *xennet_create_dev(struct xenbus_device *dev) ...@@ -1334,12 +1336,15 @@ static struct net_device *xennet_create_dev(struct xenbus_device *dev)
netif_carrier_off(netdev); netif_carrier_off(netdev);
xenbus_switch_state(dev, XenbusStateInitialising); do {
wait_event(module_wq, xenbus_switch_state(dev, XenbusStateInitialising);
xenbus_read_driver_state(dev->otherend) != err = wait_event_timeout(module_wq,
XenbusStateClosed && xenbus_read_driver_state(dev->otherend) !=
xenbus_read_driver_state(dev->otherend) != XenbusStateClosed &&
XenbusStateUnknown); xenbus_read_driver_state(dev->otherend) !=
XenbusStateUnknown, XENNET_TIMEOUT);
} while (!err);
return netdev; return netdev;
exit: exit:
...@@ -2139,28 +2144,43 @@ static const struct attribute_group xennet_dev_group = { ...@@ -2139,28 +2144,43 @@ static const struct attribute_group xennet_dev_group = {
}; };
#endif /* CONFIG_SYSFS */ #endif /* CONFIG_SYSFS */
static int xennet_remove(struct xenbus_device *dev) static void xennet_bus_close(struct xenbus_device *dev)
{ {
struct netfront_info *info = dev_get_drvdata(&dev->dev); int ret;
dev_dbg(&dev->dev, "%s\n", dev->nodename);
if (xenbus_read_driver_state(dev->otherend) != XenbusStateClosed) { if (xenbus_read_driver_state(dev->otherend) == XenbusStateClosed)
return;
do {
xenbus_switch_state(dev, XenbusStateClosing); xenbus_switch_state(dev, XenbusStateClosing);
wait_event(module_wq, ret = wait_event_timeout(module_wq,
xenbus_read_driver_state(dev->otherend) == xenbus_read_driver_state(dev->otherend) ==
XenbusStateClosing || XenbusStateClosing ||
xenbus_read_driver_state(dev->otherend) == xenbus_read_driver_state(dev->otherend) ==
XenbusStateUnknown); XenbusStateClosed ||
xenbus_read_driver_state(dev->otherend) ==
XenbusStateUnknown,
XENNET_TIMEOUT);
} while (!ret);
if (xenbus_read_driver_state(dev->otherend) == XenbusStateClosed)
return;
do {
xenbus_switch_state(dev, XenbusStateClosed); xenbus_switch_state(dev, XenbusStateClosed);
wait_event(module_wq, ret = wait_event_timeout(module_wq,
xenbus_read_driver_state(dev->otherend) == xenbus_read_driver_state(dev->otherend) ==
XenbusStateClosed || XenbusStateClosed ||
xenbus_read_driver_state(dev->otherend) == xenbus_read_driver_state(dev->otherend) ==
XenbusStateUnknown); XenbusStateUnknown,
} XENNET_TIMEOUT);
} while (!ret);
}
static int xennet_remove(struct xenbus_device *dev)
{
struct netfront_info *info = dev_get_drvdata(&dev->dev);
xennet_bus_close(dev);
xennet_disconnect_backend(info); xennet_disconnect_backend(info);
if (info->netdev->reg_state == NETREG_REGISTERED) if (info->netdev->reg_state == NETREG_REGISTERED)
......
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