Received: by 2002:a6b:fb09:0:0:0:0:0 with SMTP id h9csp449865iog; Mon, 13 Jun 2022 06:12:26 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzkoeIn7XS5/LGj2icS5H2L1eoG0dV7+0wk/hieEuvdKIK4mZPw6Z/F0waQNxrXG58QDqR/ X-Received: by 2002:a17:907:2d13:b0:711:b149:5272 with SMTP id gs19-20020a1709072d1300b00711b1495272mr39799195ejc.326.1655125946057; Mon, 13 Jun 2022 06:12:26 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1655125946; cv=none; d=google.com; s=arc-20160816; b=otO1wSbFFeuXh7L9zk+TgQV4ozjp8oAi13wCKX1zmqcjnq9ZG8hxM3kbAP6soG7W98 BalKRViCIyq399v8US8akyArE07q5SHM5fPgUoilkyXmQWiCg1s/dMa1pHFht8IysjDz KpdxzBX9uKdBvSJkVpghfIaL5dCtSCAdhyh1cKsNtoiE+Qjxy+2LIQ3T6a83y1PnjZEA jO3YkrPisoc8DCEI+qeZllxCtpERPq1J96O5GK5Ip1FZnfutrzimhx8v+LU66wAcrz6y 1TgE4oGRIn5G5o9c8nsEK6p8z+p9bMBIeRNl1Z8+PfQRDL01GoiHUdZ0wgTMy3N95c9S XgDw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=W1d+xiXPTCF4IKTqqkJTTiLNAZCnerv9lw/8ntk5k2w=; b=WvbjkLej+zDFggHHOu2wkoQqCaiahXm9gQIgYLjxrFv2JQ4iZE0izRMvWMIJ91lA9U p9pW5d0n0Da+W/Ozav60Hez3t+W6ifhbhzjMvlIthcam2TCcmWllbQw47RoBKWLNCAJq DA/W5iPUqKOUanodhr1PbU/Xu58d9Ukfqj4bQt/vXOSWxlL9hvV6XPZ6CKXG7BO7PXPS 59vTKJ6YY+mTGN/xRg0TnAmdZ13XXmSjplHIOxncpWOnV6RPOhusMgCJjsOm2MBDj1YB /LtXycW3rNW20/NQJVrgNZosJCK+k4MWu81Cqhj6+oHE1PRPM+uontkkOTe/4/Bt1oGG SupA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=U+FLOP9H; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id cy8-20020a0564021c8800b00431106c55e0si6093337edb.391.2022.06.13.06.11.58; Mon, 13 Jun 2022 06:12:26 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=U+FLOP9H; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1355850AbiFMLjo (ORCPT + 99 others); Mon, 13 Jun 2022 07:39:44 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56760 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1355154AbiFMLat (ORCPT ); Mon, 13 Jun 2022 07:30:49 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [IPv6:2604:1380:4601:e00::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8534E41325; Mon, 13 Jun 2022 03:46:33 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ams.source.kernel.org (Postfix) with ESMTPS id 3BBA1B80D19; Mon, 13 Jun 2022 10:46:32 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 92ABDC34114; Mon, 13 Jun 2022 10:46:30 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1655117190; bh=PtGvnZ+Ao/FM/HgYgj3VjZVGfbiJBvGiKkpIUT42WmE=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=U+FLOP9HQiG+e7e8A3N/5e8BoNugW3P/j41TORzkrn9mYeIX22anoJuyVyAZNBPw0 yVHy6WmLLoQeWF6QOLG4KiX7cNxMEsZgdNMP1xf+q7w9I3FZlLMJrDujUJ/mNezb3W H7lYQAG4dQfuT163n6qyeoGRnyRq4fMd4K6imZVg= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Zhang Wensheng , Sasha Levin Subject: [PATCH 5.4 318/411] driver core: fix deadlock in __device_attach Date: Mon, 13 Jun 2022 12:09:51 +0200 Message-Id: <20220613094938.273836573@linuxfoundation.org> X-Mailer: git-send-email 2.36.1 In-Reply-To: <20220613094928.482772422@linuxfoundation.org> References: <20220613094928.482772422@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-8.3 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Zhang Wensheng [ Upstream commit b232b02bf3c205b13a26dcec08e53baddd8e59ed ] 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 well, 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 shown above, when it is allowed to do async probes, because of out of memory or work limit, async work is not 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: 765230b5f084 ("driver-core: add asynchronous probing support for drivers") Signed-off-by: Zhang Wensheng Link: https://lore.kernel.org/r/20220518074516.1225580-1-zhangwensheng5@huawei.com Signed-off-by: Greg Kroah-Hartman Signed-off-by: Sasha Levin --- drivers/base/dd.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/drivers/base/dd.c b/drivers/base/dd.c index 26cd4ce3ac75..6f85280fef8d 100644 --- a/drivers/base/dd.c +++ b/drivers/base/dd.c @@ -873,6 +873,7 @@ static void __device_attach_async_helper(void *_dev, async_cookie_t cookie) static int __device_attach(struct device *dev, bool allow_async) { int ret = 0; + bool async = false; device_lock(dev); if (dev->p->dead) { @@ -911,7 +912,7 @@ static int __device_attach(struct device *dev, bool allow_async) */ dev_dbg(dev, "scheduling asynchronous probe\n"); get_device(dev); - async_schedule_dev(__device_attach_async_helper, dev); + async = true; } else { pm_request_idle(dev); } @@ -921,6 +922,8 @@ static int __device_attach(struct device *dev, bool allow_async) } out_unlock: device_unlock(dev); + if (async) + async_schedule_dev(__device_attach_async_helper, dev); return ret; } -- 2.35.1