Received: by 2002:a05:6358:d09b:b0:dc:cd0c:909e with SMTP id jc27csp3308497rwb; Wed, 30 Nov 2022 19:07:38 -0800 (PST) X-Google-Smtp-Source: AA0mqf4mC1Gz8X6cNmt76PBPbRoE/Xc82IkySEyo+rvOQdA6B9iUmPNMG2r/NZabMn02EhhJiZ/u X-Received: by 2002:a17:90a:5317:b0:213:95f:d37b with SMTP id x23-20020a17090a531700b00213095fd37bmr73127634pjh.60.1669864057856; Wed, 30 Nov 2022 19:07:37 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1669864057; cv=none; d=google.com; s=arc-20160816; b=KXrmlX1aJ7qQO4P+dQXJXxbBDUSq86pn18QfziaFq3fbEPuKJiOQbZ5m/WW/jQIx+H C5JFL2a5+5mIwQPwudWvYZBKhHGxmVImI1taVDCuWfFiIvgf/mFM1I66PRpQJtJ5ZGtV 5n6CJXFQm5anlMzlnytbH6pKb07/OoGdDl2G6PhkIcEGTrhAm7t3+gc/tjp0abOoYdO2 fEv4Rw4Lyd5dQ70fZ2pS2Qir7HsoNb8jO4LUR5XEZz5vitNL0cUfZ0z0eQHU8nebowfJ g9KZMZjWCzXkzUB+IyTpElBDzoYRnuAge0iGGjD8j7c96X8qoh8rmsg2ewYdk3pe3I7p OMxA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:dkim-signature; bh=A1q0DUW0UHb1nD0XGbTyZTTfZ/Tzl76R+aLJImRrqQE=; b=rYtxfouT3U4pcyAuprmcCjxp5Az8fLre3bFK276MtEiR8FoxS8TUTapUPBW6HneF18 ryqi3WqSQPR54dkQXRojRhzRx/niPYhDJyFyOjvx80jLZODPCCh4yWmW8uzILwqcwo7M Jc/m8PumXaJcUUqL/bJ8RDV2uWirZLN2CmUtnWLDmQhmRhsrd2PvithE4KjR4YQA/c/Y d2FZPNnUDE46do2qaQ6xD/8EaLd1xyBqLwFtFw9KUg+CCujBi6yR13ISXYX7osCe2SIh re3+8ZbjPI0wbfKutxiDmY3yKr8a9gj7rSrcjUVxtph3tVXOlsVxR/LimBUP4jE0MdnK EECw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=nxB3GTyr; 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=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id t11-20020a170902e84b00b001892277e107si3090002plg.64.2022.11.30.19.07.27; Wed, 30 Nov 2022 19:07:37 -0800 (PST) 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=@gmail.com header.s=20210112 header.b=nxB3GTyr; 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=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229777AbiLADCC (ORCPT + 82 others); Wed, 30 Nov 2022 22:02:02 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56082 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229541AbiLADCA (ORCPT ); Wed, 30 Nov 2022 22:02:00 -0500 Received: from mail-io1-xd35.google.com (mail-io1-xd35.google.com [IPv6:2607:f8b0:4864:20::d35]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7AC0B83256 for ; Wed, 30 Nov 2022 19:01:59 -0800 (PST) Received: by mail-io1-xd35.google.com with SMTP id n188so260031iof.8 for ; Wed, 30 Nov 2022 19:01:59 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=A1q0DUW0UHb1nD0XGbTyZTTfZ/Tzl76R+aLJImRrqQE=; b=nxB3GTyrTZMoG76NG8T3un04JYOU7mS4NbBynHGZYyxJk+1KTl0xgrZam7ICoBV5sk 46cG5ZeZpbvxqm6BO/02KAKO/TSHz9oaSufUZ6hx69OMHM+lFmiAWHCuMik9cnEDuia6 Jc9CKI/9nqBA9B6GHEINQFKHU54u9hFGrrZibG3c375ixo5BdkeKYNhZN7ZjY4/La/sI bFpqBz8/VjNqPeH4ngj03RbXzN2MIvQ542DHSLY4EDQP79sHrQOPZ7WphtHRzD/qho+C OryCEbTT6k6S7X77hQC5knHdsoj8tkvuycRNQZzwg2Sxd5eKRuacG2CMz5VE2Bjk22Ku Ikog== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=A1q0DUW0UHb1nD0XGbTyZTTfZ/Tzl76R+aLJImRrqQE=; b=THj8Yz8cT5Pvagjn9wO6iizMQ4ZVQP68T+bbWjDd32uJs1YzByUNLGDBuaccYpPHTg wzTMYSnMHFSmpwwdm/aGEzwfgcXzwQm5ZTZeaTXqg1UTsyO1fAZPH9SxII52nwsb2QC0 uhwaj02ZwAlJlvF7hEWVyrrajYczzOlJ2yCtdKaWd8CLRHTMGHLiTVXizgnYZzvA/ph5 2i7Hs/D3VDfeHA4Dl7Gh9efWXzZN2t2p6EMNiiBUwOZXNQ7LcDL3KuHJfdffA8a5DbPD OaWXz351bPyswMiQrRUYRA7UCz6l/JRV0Jr1B2SjrhqSnNObzGQc+0Errh0cUVMi7Bzi yfWA== X-Gm-Message-State: ANoB5pkaukySy/EboH9xveD2dkNOnStKBYXa72LfD7ke3JEDoCQcECHL mme+kRNk64j54O8o9kuBko2Wj6uVTJPHiB4wfGs= X-Received: by 2002:a02:cbde:0:b0:387:7704:abc3 with SMTP id u30-20020a02cbde000000b003877704abc3mr18317041jaq.72.1669863718925; Wed, 30 Nov 2022 19:01:58 -0800 (PST) MIME-Version: 1.0 References: <20221128183109.446754-1-vschneid@redhat.com> <20221128183109.446754-5-vschneid@redhat.com> In-Reply-To: <20221128183109.446754-5-vschneid@redhat.com> From: Lai Jiangshan Date: Thu, 1 Dec 2022 11:01:47 +0800 Message-ID: Subject: Re: [PATCH v6 4/4] workqueue: Unbind kworkers before sending them to exit() To: Valentin Schneider Cc: linux-kernel@vger.kernel.org, Tejun Heo , Peter Zijlstra , Frederic Weisbecker , Juri Lelli , Phil Auld , Marcelo Tosatti Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS 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 On Tue, Nov 29, 2022 at 2:31 AM Valentin Schneider wrote: > @@ -3627,8 +3668,11 @@ static bool wq_manager_inactive(struct worker_pool *pool) > static void put_unbound_pool(struct worker_pool *pool) > { > DECLARE_COMPLETION_ONSTACK(detach_completion); > + struct list_head cull_list; > struct worker *worker; > > + INIT_LIST_HEAD(&cull_list); > + > lockdep_assert_held(&wq_pool_mutex); > > if (--pool->refcnt) > @@ -3651,17 +3695,19 @@ static void put_unbound_pool(struct worker_pool *pool) > * Because of how wq_manager_inactive() works, we will hold the > * spinlock after a successful wait. > */ > + mutex_lock(&wq_pool_attach_mutex); > rcuwait_wait_event(&manager_wait, wq_manager_inactive(pool), > TASK_UNINTERRUPTIBLE); > pool->flags |= POOL_MANAGER_ACTIVE; Hello, Valentin I'm afraid it might deadlock here. If put_unbound_pool() is called while manage_workers() is sleeping on allocating memory, put_unbound_pool() will get the wq_pool_attach_mutex earlier than the manager which prevents the manager from getting the lock to attach the newly created worker and deadlock. I think mutex_lock(&wq_pool_attach_mutex) can be moved into wq_manager_inactive(), and handle it in the same way as pool->lock. > > while ((worker = first_idle_worker(pool))) > - destroy_worker(worker); > + set_worker_dying(worker, &cull_list); > WARN_ON(pool->nr_workers || pool->nr_idle); > raw_spin_unlock_irq(&pool->lock); > > - mutex_lock(&wq_pool_attach_mutex); > - if (!list_empty(&pool->workers)) > + wake_dying_workers(&cull_list); > + > + if (!list_empty(&pool->workers) || !list_empty(&pool->dying_workers)) > pool->detach_completion = &detach_completion; > mutex_unlock(&wq_pool_attach_mutex); > > -- > 2.31.1 >