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

driver core: fix deadlock in __device_attach

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

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

In __device_attach function, The lock holding logic is as follows:
...
__device_attach
device_lock(dev)      // get lock dev
  async_schedule_dev(__device_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
	   __device_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
__device_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: 765230b5 ("driver-core: add asynchronous probing support for drivers")
Signed-off-by: NZhang Wensheng <zhangwensheng5@huawei.com>
Reviewed-by: NHou Tao <houtao1@huawei.com>
Signed-off-by: NZheng Zengkai <zhengzengkai@huawei.com>
上级 5f0c3540
...@@ -897,6 +897,7 @@ static void __device_attach_async_helper(void *_dev, async_cookie_t cookie) ...@@ -897,6 +897,7 @@ static void __device_attach_async_helper(void *_dev, async_cookie_t cookie)
static int __device_attach(struct device *dev, bool allow_async) static int __device_attach(struct device *dev, bool allow_async)
{ {
int ret = 0; int ret = 0;
bool async = false;
device_lock(dev); device_lock(dev);
if (dev->p->dead) { if (dev->p->dead) {
...@@ -935,7 +936,7 @@ static int __device_attach(struct device *dev, bool allow_async) ...@@ -935,7 +936,7 @@ static int __device_attach(struct device *dev, bool allow_async)
*/ */
dev_dbg(dev, "scheduling asynchronous probe\n"); dev_dbg(dev, "scheduling asynchronous probe\n");
get_device(dev); get_device(dev);
async_schedule_dev(__device_attach_async_helper, dev); async = true;
} else { } else {
pm_request_idle(dev); pm_request_idle(dev);
} }
...@@ -945,6 +946,8 @@ static int __device_attach(struct device *dev, bool allow_async) ...@@ -945,6 +946,8 @@ static int __device_attach(struct device *dev, bool allow_async)
} }
out_unlock: out_unlock:
device_unlock(dev); device_unlock(dev);
if (async)
async_schedule_dev(__device_attach_async_helper, dev);
return ret; return ret;
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册