Commit 060eabe8 authored by SeongJae Park's avatar SeongJae Park Committed by Boris Ostrovsky

xenbus/backend: Protect xenbus callback with lock

A driver's 'reclaim_memory' callback can race with 'probe' or 'remove'
because it will be called whenever memory pressure is detected.  To
avoid such race, this commit embeds a spinlock in each 'xenbus_device'
and make 'xenbus' to hold the lock while the corresponded callbacks are
running.
Reviewed-by: default avatarJuergen Gross <jgross@suse.com>
Signed-off-by: default avatarSeongJae Park <sjpark@amazon.de>
Signed-off-by: default avatarBoris Ostrovsky <boris.ostrovsky@oracle.com>
parent 8a105678
...@@ -239,7 +239,9 @@ int xenbus_dev_probe(struct device *_dev) ...@@ -239,7 +239,9 @@ int xenbus_dev_probe(struct device *_dev)
goto fail; goto fail;
} }
spin_lock(&dev->reclaim_lock);
err = drv->probe(dev, id); err = drv->probe(dev, id);
spin_unlock(&dev->reclaim_lock);
if (err) if (err)
goto fail_put; goto fail_put;
...@@ -268,8 +270,11 @@ int xenbus_dev_remove(struct device *_dev) ...@@ -268,8 +270,11 @@ int xenbus_dev_remove(struct device *_dev)
free_otherend_watch(dev); free_otherend_watch(dev);
if (drv->remove) if (drv->remove) {
spin_lock(&dev->reclaim_lock);
drv->remove(dev); drv->remove(dev);
spin_unlock(&dev->reclaim_lock);
}
module_put(drv->driver.owner); module_put(drv->driver.owner);
...@@ -468,6 +473,7 @@ int xenbus_probe_node(struct xen_bus_type *bus, ...@@ -468,6 +473,7 @@ int xenbus_probe_node(struct xen_bus_type *bus,
goto fail; goto fail;
dev_set_name(&xendev->dev, "%s", devname); dev_set_name(&xendev->dev, "%s", devname);
spin_lock_init(&xendev->reclaim_lock);
/* Register with generic device framework. */ /* Register with generic device framework. */
err = device_register(&xendev->dev); err = device_register(&xendev->dev);
......
...@@ -250,12 +250,18 @@ static int backend_probe_and_watch(struct notifier_block *notifier, ...@@ -250,12 +250,18 @@ static int backend_probe_and_watch(struct notifier_block *notifier,
static int backend_reclaim_memory(struct device *dev, void *data) static int backend_reclaim_memory(struct device *dev, void *data)
{ {
const struct xenbus_driver *drv; const struct xenbus_driver *drv;
struct xenbus_device *xdev;
if (!dev->driver) if (!dev->driver)
return 0; return 0;
drv = to_xenbus_driver(dev->driver); drv = to_xenbus_driver(dev->driver);
if (drv && drv->reclaim_memory) if (drv && drv->reclaim_memory) {
drv->reclaim_memory(to_xenbus_device(dev)); xdev = to_xenbus_device(dev);
if (!spin_trylock(&xdev->reclaim_lock))
return 0;
drv->reclaim_memory(xdev);
spin_unlock(&xdev->reclaim_lock);
}
return 0; return 0;
} }
......
...@@ -76,6 +76,7 @@ struct xenbus_device { ...@@ -76,6 +76,7 @@ struct xenbus_device {
enum xenbus_state state; enum xenbus_state state;
struct completion down; struct completion down;
struct work_struct work; struct work_struct work;
spinlock_t reclaim_lock;
}; };
static inline struct xenbus_device *to_xenbus_device(struct device *dev) static inline struct xenbus_device *to_xenbus_device(struct device *dev)
......
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