提交 afd76f44 编写于 作者: Z Zhang Wensheng 提交者: Zheng Zengkai

driver core: fix deadlock in __driver_attach

hulk inclusion
category: bugfix
bugzilla: https://gitee.com/openeuler/kernel/issues/I58CRT
CVE: NA

--------------------------------

In __driver_attach function, The lock holding logic is as follows:
...
__driver_attach
if (driver_allows_async_probing(drv))
  device_lock(dev)      // get lock dev
    async_schedule_dev(__driver_attach_async_helper, dev); // func
      async_schedule_node
        async_schedule_node_domain(func)
          entry = kzalloc(sizeof(struct async_entry), GFP_ATOMIC);
	  /* when fail or work limit, sync to execute func, but
	     __driver_attach_async_helper will get lock dev as
	     will, which will lead to A-A deadlock.  */
	  if (!entry || atomic_read(&entry_count) > MAX_WORK) {
	    func;
	  else
	    queue_work_node(node, system_unbound_wq, &entry->work)
  device_unlock(dev)

As above show, when it is allowed to do async probes, because of
out of memory or work limit, async work is not be allowed, to do
sync execute instead. it will lead to A-A deadlock because of
__driver_attach_async_helper getting lock dev.

To fix the deadlock, move the async_schedule_dev outside device_lock,
as we can see, in async_schedule_node_domain, the parameter of
queue_work_node is system_unbound_wq, so it can accept concurrent
operations. which will also not change the code logic, and will
not lead to deadlock.

Fixes: ef0ff683 ("driver core: Probe devices asynchronously instead of the driver")
Signed-off-by: NZhang Wensheng <zhangwensheng5@huawei.com>
Reviewed-by: NHou Tao <houtao1@huawei.com>
Signed-off-by: NZheng Zengkai <zhengzengkai@huawei.com>
上级 780c35b3
......@@ -1062,6 +1062,7 @@ static int __driver_attach(struct device *dev, void *data)
{
struct device_driver *drv = data;
int ret;
bool async = false;
/*
* Lock device and try to bind to it. We drop the error
......@@ -1098,9 +1099,11 @@ static int __driver_attach(struct device *dev, void *data)
if (!dev->driver) {
get_device(dev);
dev->p->async_driver = drv;
async_schedule_dev(__driver_attach_async_helper, dev);
async = true;
}
device_unlock(dev);
if (async)
async_schedule_dev(__driver_attach_async_helper, dev);
return 0;
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册