From patchwork Mon Nov 28 18:31:06 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Valentin Schneider X-Patchwork-Id: 26918 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:f944:0:0:0:0:0 with SMTP id q4csp5874007wrr; Mon, 28 Nov 2022 10:45:16 -0800 (PST) X-Google-Smtp-Source: AA0mqf6r4vSYy32AHGOOwx8s3RHQsK6gXkgEBhGIcqhfd6gp2Sz7G+pqhPP2n6dmU7FlsnI2SjAw X-Received: by 2002:a17:906:cd10:b0:7ad:c0e7:7a99 with SMTP id oz16-20020a170906cd1000b007adc0e77a99mr42880150ejb.689.1669661116221; Mon, 28 Nov 2022 10:45:16 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1669661116; cv=none; d=google.com; s=arc-20160816; b=SqBEgRhM/Ee+INHyruSrnlJQra5ySvbAmY/5icep0X8dLW/5gvcYD9/mT06xkQVRVL VsfaZoXWBVFSM5UbxP61/ZdkAYO6bQ8evPj+llyGO9RX9ooRZv9v1DeKvosMq7p/aHZU /4LHenaO5qN7EpnM2DuzWFQ+hWWqxO9QRHTCXS8JDWQI4JRC2psV7H1fVtKJX18Xb7ho xaWeg+CEwK4VGUK+lTGXcT2oQ1pPFUHg/EH3M25R7xIn0cyt3e9+bTccMdZgwcrYmGPT HJIXb4LzLW6kyqzURXpUowJo4wgHRviyRZfQIK+S4wNoK/w2B97vipbTFCmjwEJw3QvP iJRg== 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 :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=3juG06G3YKqZVFnwZoo8pO4nzDVQkrUL6djcNIQJnew=; b=TxJ6O/XrJeWK7icqjBzwY9lKPC6/pJwJQVhg/NFluL1ciqWuPHZDOBtmrul8FckaqL QPbk9lxxGFBYFsFnmw5wYTjosqK1PZMc7I6tTN8ur6jx5+rRm9Y7MwGtIQs4jIDjwsfI xBfXbwAp0pV6mUs/dOmgxHxyuGxX9yVVn2xoaNkqNqBYiPq4aGmDR619POhBuvz14mrj 27qC5j5jM6ztvWSsU52AuXifCorJ85l0mH+RDiMcV3Jx8BE/8go2KxfHbVJiRHhn6JnZ QBv1/CGG82zYKDgQuZfcNSEb0Uem8ma23qvllcFxLFa+YYxlr/hFD91sRNkK/dQMBQDK igig== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=D2ZR0YY2; 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=redhat.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id z4-20020aa7d404000000b0046270f01409si8967084edq.40.2022.11.28.10.44.52; Mon, 28 Nov 2022 10:45:16 -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=@redhat.com header.s=mimecast20190719 header.b=D2ZR0YY2; 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=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231442AbiK1ShO (ORCPT + 99 others); Mon, 28 Nov 2022 13:37:14 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41096 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233023AbiK1Sgs (ORCPT ); Mon, 28 Nov 2022 13:36:48 -0500 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1A6F8F41 for ; Mon, 28 Nov 2022 10:35:26 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1669660479; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=3juG06G3YKqZVFnwZoo8pO4nzDVQkrUL6djcNIQJnew=; b=D2ZR0YY2WOjOgxP/9QjRx6fqDykope7TvhOhciUgSawz5vPch5/CEHCR1LKvq7jEUzUfab 7CQOst9CQb1jn4N3rDq+KM0E9dAyjx2LHwnUBnGz/L2th0dmgKzcWz81o93U83dvvGo9RG /lrQYK4H5VDQiH9a0vDq0hb0MOAQtWo= Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-102-DixXqDWaP6uq6Sg8SFAy9A-1; Mon, 28 Nov 2022 13:32:30 -0500 X-MC-Unique: DixXqDWaP6uq6Sg8SFAy9A-1 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.rdu2.redhat.com [10.11.54.6]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 2837D2999B34; Mon, 28 Nov 2022 18:31:19 +0000 (UTC) Received: from vschneid.remote.csb (unknown [10.33.37.8]) by smtp.corp.redhat.com (Postfix) with ESMTP id BD1E22166B2E; Mon, 28 Nov 2022 18:31:17 +0000 (UTC) From: Valentin Schneider To: linux-kernel@vger.kernel.org Cc: Lai Jiangshan , Tejun Heo , Lai Jiangshan , Peter Zijlstra , Frederic Weisbecker , Juri Lelli , Phil Auld , Marcelo Tosatti Subject: [PATCH v6 1/4] workqueue: Protects wq_unbound_cpumask with wq_pool_attach_mutex Date: Mon, 28 Nov 2022 18:31:06 +0000 Message-Id: <20221128183109.446754-2-vschneid@redhat.com> In-Reply-To: <20221128183109.446754-1-vschneid@redhat.com> References: <20221128183109.446754-1-vschneid@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.6 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_NONE 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1750766574713785205?= X-GMAIL-MSGID: =?utf-8?q?1750766574713785205?= From: Lai Jiangshan When unbind_workers() reads wq_unbound_cpumask to set the affinity of freshly-unbound kworkers, it only holds wq_pool_attach_mutex. This isn't sufficient as wq_unbound_cpumask is only protected by wq_pool_mutex. Make wq_unbound_cpumask protected with wq_pool_attach_mutex and also remove the need of temporary saved_cpumask. Fixes: 10a5a651e3af ("workqueue: Restrict kworker in the offline CPU pool running on housekeeping CPUs") Reported-by: Valentin Schneider Signed-off-by: Lai Jiangshan --- kernel/workqueue.c | 41 ++++++++++++++++------------------------- 1 file changed, 16 insertions(+), 25 deletions(-) diff --git a/kernel/workqueue.c b/kernel/workqueue.c index 7cd5f5e7e0a1b..8e21c352c1558 100644 --- a/kernel/workqueue.c +++ b/kernel/workqueue.c @@ -326,7 +326,7 @@ static struct rcuwait manager_wait = __RCUWAIT_INITIALIZER(manager_wait); static LIST_HEAD(workqueues); /* PR: list of all workqueues */ static bool workqueue_freezing; /* PL: have wqs started freezing? */ -/* PL: allowable cpus for unbound wqs and work items */ +/* PL&A: allowable cpus for unbound wqs and work items */ static cpumask_var_t wq_unbound_cpumask; /* CPU where unbound work was last round robin scheduled from this CPU */ @@ -3952,7 +3952,8 @@ static void apply_wqattrs_cleanup(struct apply_wqattrs_ctx *ctx) /* allocate the attrs and pwqs for later installation */ static struct apply_wqattrs_ctx * apply_wqattrs_prepare(struct workqueue_struct *wq, - const struct workqueue_attrs *attrs) + const struct workqueue_attrs *attrs, + const cpumask_var_t unbound_cpumask) { struct apply_wqattrs_ctx *ctx; struct workqueue_attrs *new_attrs, *tmp_attrs; @@ -3968,14 +3969,15 @@ apply_wqattrs_prepare(struct workqueue_struct *wq, goto out_free; /* - * Calculate the attrs of the default pwq. + * Calculate the attrs of the default pwq with unbound_cpumask + * which is wq_unbound_cpumask or to set to wq_unbound_cpumask. * If the user configured cpumask doesn't overlap with the * wq_unbound_cpumask, we fallback to the wq_unbound_cpumask. */ copy_workqueue_attrs(new_attrs, attrs); - cpumask_and(new_attrs->cpumask, new_attrs->cpumask, wq_unbound_cpumask); + cpumask_and(new_attrs->cpumask, new_attrs->cpumask, unbound_cpumask); if (unlikely(cpumask_empty(new_attrs->cpumask))) - cpumask_copy(new_attrs->cpumask, wq_unbound_cpumask); + cpumask_copy(new_attrs->cpumask, unbound_cpumask); /* * We may create multiple pwqs with differing cpumasks. Make a @@ -4072,7 +4074,7 @@ static int apply_workqueue_attrs_locked(struct workqueue_struct *wq, wq->flags &= ~__WQ_ORDERED; } - ctx = apply_wqattrs_prepare(wq, attrs); + ctx = apply_wqattrs_prepare(wq, attrs, wq_unbound_cpumask); if (!ctx) return -ENOMEM; @@ -5334,7 +5336,7 @@ void thaw_workqueues(void) } #endif /* CONFIG_FREEZER */ -static int workqueue_apply_unbound_cpumask(void) +static int workqueue_apply_unbound_cpumask(const cpumask_var_t unbound_cpumask) { LIST_HEAD(ctxs); int ret = 0; @@ -5350,7 +5352,7 @@ static int workqueue_apply_unbound_cpumask(void) if (wq->flags & __WQ_ORDERED) continue; - ctx = apply_wqattrs_prepare(wq, wq->unbound_attrs); + ctx = apply_wqattrs_prepare(wq, wq->unbound_attrs, unbound_cpumask); if (!ctx) { ret = -ENOMEM; break; @@ -5365,6 +5367,11 @@ static int workqueue_apply_unbound_cpumask(void) apply_wqattrs_cleanup(ctx); } + if (!ret) { + mutex_lock(&wq_pool_attach_mutex); + cpumask_copy(wq_unbound_cpumask, unbound_cpumask); + mutex_unlock(&wq_pool_attach_mutex); + } return ret; } @@ -5383,7 +5390,6 @@ static int workqueue_apply_unbound_cpumask(void) int workqueue_set_unbound_cpumask(cpumask_var_t cpumask) { int ret = -EINVAL; - cpumask_var_t saved_cpumask; /* * Not excluding isolated cpus on purpose. @@ -5397,23 +5403,8 @@ int workqueue_set_unbound_cpumask(cpumask_var_t cpumask) goto out_unlock; } - if (!zalloc_cpumask_var(&saved_cpumask, GFP_KERNEL)) { - ret = -ENOMEM; - goto out_unlock; - } - - /* save the old wq_unbound_cpumask. */ - cpumask_copy(saved_cpumask, wq_unbound_cpumask); - - /* update wq_unbound_cpumask at first and apply it to wqs. */ - cpumask_copy(wq_unbound_cpumask, cpumask); - ret = workqueue_apply_unbound_cpumask(); - - /* restore the wq_unbound_cpumask when failed. */ - if (ret < 0) - cpumask_copy(wq_unbound_cpumask, saved_cpumask); + ret = workqueue_apply_unbound_cpumask(cpumask); - free_cpumask_var(saved_cpumask); out_unlock: apply_wqattrs_unlock(); } From patchwork Mon Nov 28 18:31:07 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Valentin Schneider X-Patchwork-Id: 26917 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:f944:0:0:0:0:0 with SMTP id q4csp5873649wrr; Mon, 28 Nov 2022 10:44:30 -0800 (PST) X-Google-Smtp-Source: AA0mqf56grwQCZSO8+so8XclPribBn3OXhQzW8fEFFlx2Qz37ou8/OUOAq7f8iFMA6/MuLveC/LY X-Received: by 2002:a17:906:2c5b:b0:7ae:180f:e6e with SMTP id f27-20020a1709062c5b00b007ae180f0e6emr27759542ejh.498.1669661070027; Mon, 28 Nov 2022 10:44:30 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1669661070; cv=none; d=google.com; s=arc-20160816; b=KLvvAofF7o+oLPyNPU0MfiCvaa6XnJNKZUB72PiLA01g4I5DKBjkOuzIK8Skw0TbcS DvO8egoDVrR4IiQL/FDM9DyHf/5Py1Wkf03Ko0R6cDDGFIm9/UZG0pQdKYI76JbI/czL xglxKf6Mfa77k90uym5XtRXuWDc2Pn/7RiLWjVnse/mHAJO6Q+chf9G7qxl1UUzaD4Zg xS9FbdOT0EEz8r1r6m7FQGFXYd/9V9CYkL8EZZhf40iTJLIgWWZziyfGUk9rYNMjInHG 5C42cQXN8URfoTGwYPfu8lDeAo6tEVQfK8LM1HGXVzy7pGNio+9pE37jMEaPSTY4xyBY dFZw== 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 :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=eQtqTu7JRNjFnV6o8lHrVhZyrgiyx0mHVqwoCilAwG0=; b=x7PrwIbephFHwqEggFcsacqm2RtHRnvmk92NJMOeOz6Fmx8GvPzO45eFY8E3JRWGpc h7ePuKeqf28L7KlGl+61Kdf910eZJjKF1HEo7F2nxBbJlrTQF0aKTl4hYJ0tDVvajHtq r4Ow/BvClwJm9n0DqGZpCWJaAXouRlC9nkanuXz/k1aDd9o8wHS+checTMQRRroAtAm1 CY/LUTdgrSZ0Zs0cdrvWk0J1XkFf5JovueVamCa4TjV+65UeolN3BC71atkDYt+1Kp7P O7pvhLdJWuQhUT/ip2Dw5xlCVRtZxyfKs2Q0jZPvQ3wQwGMDuJHvlII7/rG4rurXoKne kL2A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=G+9mBmb5; 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=redhat.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id b7-20020a056402278700b00463f8aad371si11963535ede.239.2022.11.28.10.44.00; Mon, 28 Nov 2022 10:44:30 -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=@redhat.com header.s=mimecast20190719 header.b=G+9mBmb5; 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=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233142AbiK1Sfr (ORCPT + 99 others); Mon, 28 Nov 2022 13:35:47 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41028 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233210AbiK1Sf0 (ORCPT ); Mon, 28 Nov 2022 13:35:26 -0500 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C36E91A05F for ; Mon, 28 Nov 2022 10:32:42 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1669660361; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=eQtqTu7JRNjFnV6o8lHrVhZyrgiyx0mHVqwoCilAwG0=; b=G+9mBmb5pfor3M4bss4cZGDh0i/yqjTKww4jtI/P6ox4fazIr3Zf4Alg1bMFQv4OPEQGPi vV2l3iUjsxfkNL2cyg9xqRVPk/B0QEsP4ktMXrp1tZNXePVozcC1KiTswa+ebcJghWLFwf qrgaFvUkPqUSMAoCV7XliHvQJizBULQ= Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-241--wTU94U4M8KlY8lonqT2Pw-1; Mon, 28 Nov 2022 13:32:19 -0500 X-MC-Unique: -wTU94U4M8KlY8lonqT2Pw-1 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.rdu2.redhat.com [10.11.54.6]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 9160A2999B36; Mon, 28 Nov 2022 18:31:20 +0000 (UTC) Received: from vschneid.remote.csb (unknown [10.33.37.8]) by smtp.corp.redhat.com (Postfix) with ESMTP id 836C02166B2E; Mon, 28 Nov 2022 18:31:19 +0000 (UTC) From: Valentin Schneider To: linux-kernel@vger.kernel.org Cc: Tejun Heo , Lai Jiangshan , Peter Zijlstra , Frederic Weisbecker , Juri Lelli , Phil Auld , Marcelo Tosatti Subject: [PATCH v6 2/4] workqueue: Factorize unbind/rebind_workers() logic Date: Mon, 28 Nov 2022 18:31:07 +0000 Message-Id: <20221128183109.446754-3-vschneid@redhat.com> In-Reply-To: <20221128183109.446754-1-vschneid@redhat.com> References: <20221128183109.446754-1-vschneid@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.6 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_NONE 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1750766526340562513?= X-GMAIL-MSGID: =?utf-8?q?1750766526340562513?= Later patches will reuse this code, move it into reusable functions. Signed-off-by: Valentin Schneider --- kernel/workqueue.c | 33 +++++++++++++++++++++------------ 1 file changed, 21 insertions(+), 12 deletions(-) diff --git a/kernel/workqueue.c b/kernel/workqueue.c index 8e21c352c1558..8185a42848c50 100644 --- a/kernel/workqueue.c +++ b/kernel/workqueue.c @@ -1972,6 +1972,23 @@ static struct worker *create_worker(struct worker_pool *pool) return NULL; } +static void unbind_worker(struct worker *worker) +{ + lockdep_assert_held(&wq_pool_attach_mutex); + + kthread_set_per_cpu(worker->task, -1); + if (cpumask_intersects(wq_unbound_cpumask, cpu_active_mask)) + WARN_ON_ONCE(set_cpus_allowed_ptr(worker->task, wq_unbound_cpumask) < 0); + else + WARN_ON_ONCE(set_cpus_allowed_ptr(worker->task, cpu_possible_mask) < 0); +} + +static void rebind_worker(struct worker *worker, struct worker_pool *pool) +{ + kthread_set_per_cpu(worker->task, pool->cpu); + WARN_ON_ONCE(set_cpus_allowed_ptr(worker->task, pool->attrs->cpumask) < 0); +} + /** * destroy_worker - destroy a workqueue worker * @worker: worker to be destroyed @@ -5008,13 +5025,8 @@ static void unbind_workers(int cpu) raw_spin_unlock_irq(&pool->lock); - for_each_pool_worker(worker, pool) { - kthread_set_per_cpu(worker->task, -1); - if (cpumask_intersects(wq_unbound_cpumask, cpu_active_mask)) - WARN_ON_ONCE(set_cpus_allowed_ptr(worker->task, wq_unbound_cpumask) < 0); - else - WARN_ON_ONCE(set_cpus_allowed_ptr(worker->task, cpu_possible_mask) < 0); - } + for_each_pool_worker(worker, pool) + unbind_worker(worker); mutex_unlock(&wq_pool_attach_mutex); } @@ -5039,11 +5051,8 @@ static void rebind_workers(struct worker_pool *pool) * of all workers first and then clear UNBOUND. As we're called * from CPU_ONLINE, the following shouldn't fail. */ - for_each_pool_worker(worker, pool) { - kthread_set_per_cpu(worker->task, pool->cpu); - WARN_ON_ONCE(set_cpus_allowed_ptr(worker->task, - pool->attrs->cpumask) < 0); - } + for_each_pool_worker(worker, pool) + rebind_worker(worker, pool); raw_spin_lock_irq(&pool->lock); From patchwork Mon Nov 28 18:31:08 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Valentin Schneider X-Patchwork-Id: 26916 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:f944:0:0:0:0:0 with SMTP id q4csp5872823wrr; Mon, 28 Nov 2022 10:42:40 -0800 (PST) X-Google-Smtp-Source: AA0mqf5M6Bit06Uuj+yJv9nxfh2YTaWqAGFW4kEmeiOdjK+H4KAd+DPhxlgc+rKBwUO0P95TOBjv X-Received: by 2002:a17:902:ead4:b0:189:68ed:6c0d with SMTP id p20-20020a170902ead400b0018968ed6c0dmr16796686pld.140.1669660959747; Mon, 28 Nov 2022 10:42:39 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1669660959; cv=none; d=google.com; s=arc-20160816; b=PUc2AK7OAmPrSTQ+4c2Sj96kf35Ro+H1aR86g5IJL8IZjOh1x2br6HDniXbG+fWo25 pLKrUyb13LCdzDyLN7dBT824oYr5U4TbvlUbsrYf3nzATReaM8wZFG//dhS62p0vlssP M2YiXVv4AHG4L6wDyCXj8btqvc1ZnWx1iYT3Ej4nLyl3uDIrWc/1ww3eQVpDBebvhmHA iZvbOJn0b0ozY59DUyknvvTEycDPqPEvtVruLUf1LT1NEDb9lsR18u8qTKmgWZIgTTzI vK4KwQj97NMvj2rrB7hy9W3C9l+K0FnOl+cJV3ImvFOvsJ86delXVmEEkz6v9N/TJzs0 fO/w== 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 :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=iBeA7STGfam5IOKxD+wB1RjgRxQP91bo13x/HX7w5mQ=; b=yLUBgV0z9nmVEPOjyaWVosQ20+Qiqz9ZlOaa8gZ7SBR9AoQ9r6QGvKqlxeevQlVQqQ iTL3RkdpC50pEqnAuJRWAfYpEUAsnVLUJdWKCXs3SH8DRoFai6GJsxJpkG4GweD9ndY2 6NAkNxfnlA33ZotFv6kxN/obzAa74xqtBePEvfnEFOC/BjOySEaqowDcRVI7Y/G752lv FIPU4sUnL9LNvxHcbN2kWKfb8wbI31UFCWOdLsTnzJo+QNujE07UWYtulwSO67NvaIug f/d6/o8uoCmS7ka2pEFvseUxgkuqnQenhWUr6AKHq7Si80niXaLU/jTwuqcOxmJkRF/N 5nuA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=ecnWwGyo; 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=redhat.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id ay16-20020a056a00301000b005730a1bbac0si11900961pfb.354.2022.11.28.10.42.26; Mon, 28 Nov 2022 10:42:39 -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=@redhat.com header.s=mimecast20190719 header.b=ecnWwGyo; 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=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232802AbiK1Se0 (ORCPT + 99 others); Mon, 28 Nov 2022 13:34:26 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41096 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233410AbiK1Sd7 (ORCPT ); Mon, 28 Nov 2022 13:33:59 -0500 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 61168274 for ; Mon, 28 Nov 2022 10:31:27 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1669660286; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=iBeA7STGfam5IOKxD+wB1RjgRxQP91bo13x/HX7w5mQ=; b=ecnWwGyomCgz+xuQUlohCqXMHG7Ex9vSOD/QgQDYPdCkHOxXLU59P5aVgqprI8/bT1dOr4 g3MsTHvg1+OAxDwaiKxX0G6GOumOFp39uYH598sowPKkeGyzly+Ayoz7P/8wHXg1lHV6j7 dgEAIt94vXNWxG09J/9+a8B4pdEg8IM= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-351-UVzXAoVYOQ6S6tCp3ERLqA-1; Mon, 28 Nov 2022 13:31:22 -0500 X-MC-Unique: UVzXAoVYOQ6S6tCp3ERLqA-1 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.rdu2.redhat.com [10.11.54.6]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id C2E35800186; Mon, 28 Nov 2022 18:31:21 +0000 (UTC) Received: from vschneid.remote.csb (unknown [10.33.37.8]) by smtp.corp.redhat.com (Postfix) with ESMTP id C436E2166B2E; Mon, 28 Nov 2022 18:31:20 +0000 (UTC) From: Valentin Schneider To: linux-kernel@vger.kernel.org Cc: Tejun Heo , Lai Jiangshan , Peter Zijlstra , Frederic Weisbecker , Juri Lelli , Phil Auld , Marcelo Tosatti Subject: [PATCH v6 3/4] workqueue: Convert the idle_timer to a timer + work_struct Date: Mon, 28 Nov 2022 18:31:08 +0000 Message-Id: <20221128183109.446754-4-vschneid@redhat.com> In-Reply-To: <20221128183109.446754-1-vschneid@redhat.com> References: <20221128183109.446754-1-vschneid@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.6 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_NONE 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1750766410233623995?= X-GMAIL-MSGID: =?utf-8?q?1750766410233623995?= A later patch will require a sleepable context in the idle worker timeout function. Converting worker_pool.idle_timer to a delayed_work gives us just that, however this would imply turning all idle_timer expiries into scheduler events (waking up a worker to handle the dwork). Instead, implement a "custom dwork" where the timer callback does some extra checks before queuing the associated work. No change in functionality intended. Signed-off-by: Valentin Schneider --- kernel/workqueue.c | 51 +++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 48 insertions(+), 3 deletions(-) diff --git a/kernel/workqueue.c b/kernel/workqueue.c index 8185a42848c50..c8b1466a9c070 100644 --- a/kernel/workqueue.c +++ b/kernel/workqueue.c @@ -169,7 +169,9 @@ struct worker_pool { struct list_head idle_list; /* L: list of idle workers */ struct timer_list idle_timer; /* L: worker idle timeout */ - struct timer_list mayday_timer; /* L: SOS timer for workers */ + struct work_struct idle_cull_work; /* L: worker idle cleanup */ + + struct timer_list mayday_timer; /* L: SOS timer for workers */ /* a workers is either on busy_hash or idle_list, or the manager */ DECLARE_HASHTABLE(busy_hash, BUSY_WORKER_HASH_ORDER); @@ -1806,7 +1808,9 @@ static void worker_enter_idle(struct worker *worker) /* idle_list is LIFO */ list_add(&worker->entry, &pool->idle_list); - if (too_many_workers(pool) && !timer_pending(&pool->idle_timer)) + if (too_many_workers(pool) && + !timer_pending(&pool->idle_timer) && + !work_pending(&pool->idle_cull_work)) mod_timer(&pool->idle_timer, jiffies + IDLE_WORKER_TIMEOUT); /* Sanity check nr_running. */ @@ -2019,17 +2023,56 @@ static void destroy_worker(struct worker *worker) wake_up_process(worker->task); } +/* + * idle_worker_timeout - check if some idle workers can now be deleted. + * + * The timer is armed in worker_enter_idle(). Note that it isn't disarmed in + * worker_leave_idle(), as a worker flicking between idle and active while its + * pool is at the too_many_workers() tipping point would cause too much timer + * housekeeping overhead. Since IDLE_WORKER_TIMEOUT is long enough, we just let + * it expire and re-evaluate things from there. + */ static void idle_worker_timeout(struct timer_list *t) { struct worker_pool *pool = from_timer(pool, t, idle_timer); + bool do_cull = false; + + if (work_pending(&pool->idle_cull_work)) + return; raw_spin_lock_irq(&pool->lock); - while (too_many_workers(pool)) { + if (too_many_workers(pool)) { struct worker *worker; unsigned long expires; /* idle_list is kept in LIFO order, check the last one */ + worker = list_entry(pool->idle_list.prev, struct worker, entry); + expires = worker->last_active + IDLE_WORKER_TIMEOUT; + do_cull = !time_before(jiffies, expires); + + if (!do_cull) + mod_timer(&pool->idle_timer, expires); + } + raw_spin_unlock_irq(&pool->lock); + + if (do_cull) + queue_work(system_unbound_wq, &pool->idle_cull_work); +} + +/* + * idle_cull_fn - cull workers that have been idle for too long. + */ +static void idle_cull_fn(struct work_struct *work) +{ + struct worker_pool *pool = container_of(work, struct worker_pool, idle_cull_work); + + raw_spin_lock_irq(&pool->lock); + + while (too_many_workers(pool)) { + struct worker *worker; + unsigned long expires; + worker = list_entry(pool->idle_list.prev, struct worker, entry); expires = worker->last_active + IDLE_WORKER_TIMEOUT; @@ -3479,6 +3522,7 @@ static int init_worker_pool(struct worker_pool *pool) hash_init(pool->busy_hash); timer_setup(&pool->idle_timer, idle_worker_timeout, TIMER_DEFERRABLE); + INIT_WORK(&pool->idle_cull_work, idle_cull_fn); timer_setup(&pool->mayday_timer, pool_mayday_timeout, 0); @@ -3626,6 +3670,7 @@ static void put_unbound_pool(struct worker_pool *pool) /* shut down the timers */ del_timer_sync(&pool->idle_timer); + cancel_work_sync(&pool->idle_cull_work); del_timer_sync(&pool->mayday_timer); /* RCU protected to allow dereferences from get_work_pool() */ From patchwork Mon Nov 28 18:31:09 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Valentin Schneider X-Patchwork-Id: 26915 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:f944:0:0:0:0:0 with SMTP id q4csp5872683wrr; Mon, 28 Nov 2022 10:42:27 -0800 (PST) X-Google-Smtp-Source: AA0mqf6Ua3g+UQFJ6u9uzAT77rVZ34yT8mQsNdoDbhBTcd5YUd5pUtqc/8weIu4s4aqrAazBsH31 X-Received: by 2002:aa7:9388:0:b0:56d:4c7e:777a with SMTP id t8-20020aa79388000000b0056d4c7e777amr55447913pfe.0.1669660946882; Mon, 28 Nov 2022 10:42:26 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1669660946; cv=none; d=google.com; s=arc-20160816; b=eG35ullh1he6pIlszjK1ID2K/IfJaZN1Yxq5/quKVu3DFj7oplMTsXMU6gLDF2Wi90 MX0/ulbkz5JmKXCnPAVXKGVbmlhjim6kWiLF6+HMsfMOIoebUmknbUNSR9xS70L0a1v4 ma78OMbBkQXq86z7FT3WFpDf3v9hORHK+YApMEP8M+GTjpTCUpUAQeZU/5fLsO+DcD2s Ha7v7MDTX92aY5FHLWAI5HncokviWs7RHsQANhK9H/iecyskySU2pgGgsqoISL1Ixaqy BF7pS58UUX2uX8F35xDxfCj96OmoZ8ZvHn3grOYNYcM/WvVSp3jHnwjMAzNzgekS3A0H ehKA== 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 :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=BGM2PS6waVZLkSGP61Ah74Ke9n+AqtE5b7AKkQ/DYGQ=; b=Y1aiUevj/FzNqeFXgGmB3j1MME/rKeu/11W5xq0iR+YMt51u4WKiQRCTFUh1X9luiz uQvjEZxNrQQQ/TzJ9rx0rWflihSOACpt8FhSKQx4kRf9p/Pg0pU2vQ5qLJzJhXiqRIzm 8h06IRwXnad0yG9ceLsV5dbpt0cc3BNN3uHgDxc4dx/sIqSE5Dv9GzbdiMAbxR11o8fp Zzt7PoDX8u00k5bBtcelJgMCOCZMMtlTlr486LrtyynvBaFcc47U+NeEXP5KI4KCTHmI u8pSyipTzGHFOhMJ+F/HuSLzr7a8Zr0DXOhr7P2zsgbbccbnTh32y7ycQtAcy73u0vry q2qQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=SpK5DcxF; 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=redhat.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id ja11-20020a170902efcb00b001892abd19easi11383416plb.157.2022.11.28.10.42.13; Mon, 28 Nov 2022 10:42:26 -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=@redhat.com header.s=mimecast20190719 header.b=SpK5DcxF; 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=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233041AbiK1See (ORCPT + 99 others); Mon, 28 Nov 2022 13:34:34 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41088 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233419AbiK1Sd7 (ORCPT ); Mon, 28 Nov 2022 13:33:59 -0500 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 25A99AB for ; Mon, 28 Nov 2022 10:31:26 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1669660285; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=BGM2PS6waVZLkSGP61Ah74Ke9n+AqtE5b7AKkQ/DYGQ=; b=SpK5DcxFyx4vHbID1TahhQ3WfAX9TIbJxTWxOVKrW/UDTXNLhDv6Oe7nIdiUuk72YS1SD7 eaQ8bM2VWVVQ4B8jA1ZLXwPxu34ml5Y3jH0YmwSDMY+5O4cPgMNSX/4GTM6BsVMBQWlLeM 50K3d7oKsXcYt0hkiqWYclsX5T6lkcs= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-473-nJGr7m6COBGpYIm-6jUaKA-1; Mon, 28 Nov 2022 13:31:23 -0500 X-MC-Unique: nJGr7m6COBGpYIm-6jUaKA-1 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.rdu2.redhat.com [10.11.54.6]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id F2795855438; Mon, 28 Nov 2022 18:31:22 +0000 (UTC) Received: from vschneid.remote.csb (unknown [10.33.37.8]) by smtp.corp.redhat.com (Postfix) with ESMTP id 004C12166B2E; Mon, 28 Nov 2022 18:31:21 +0000 (UTC) From: Valentin Schneider To: linux-kernel@vger.kernel.org Cc: Tejun Heo , Lai Jiangshan , Peter Zijlstra , Frederic Weisbecker , Juri Lelli , Phil Auld , Marcelo Tosatti Subject: [PATCH v6 4/4] workqueue: Unbind kworkers before sending them to exit() Date: Mon, 28 Nov 2022 18:31:09 +0000 Message-Id: <20221128183109.446754-5-vschneid@redhat.com> In-Reply-To: <20221128183109.446754-1-vschneid@redhat.com> References: <20221128183109.446754-1-vschneid@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.6 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_NONE 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1750766396755805235?= X-GMAIL-MSGID: =?utf-8?q?1750766396755805235?= It has been reported that isolated CPUs can suffer from interference due to per-CPU kworkers waking up just to die. A surge of workqueue activity during initial setup of a latency-sensitive application (refresh_vm_stats() being one of the culprits) can cause extra per-CPU kworkers to be spawned. Then, said latency-sensitive task can be running merrily on an isolated CPU only to be interrupted sometime later by a kworker marked for death (cf. IDLE_WORKER_TIMEOUT, 5 minutes after last kworker activity). Prevent this by affining kworkers to the wq_unbound_cpumask (which doesn't contain isolated CPUs, cf. HK_TYPE_WQ) before waking them up after marking them with WORKER_DIE. Changing the affinity does require a sleepable context, leverage the newly introduced pool->idle_cull_work to get that. Remove dying workers from pool->workers and keep track of them in a separate list. This intentionally prevents for_each_loop_worker() from iterating over workers that are marked for death. Rename destroy_worker() to set_working_dying() to better reflect its effects and relationship with wake_dying_workers(). Signed-off-by: Valentin Schneider --- kernel/workqueue.c | 70 ++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 58 insertions(+), 12 deletions(-) diff --git a/kernel/workqueue.c b/kernel/workqueue.c index c8b1466a9c070..da6396f513102 100644 --- a/kernel/workqueue.c +++ b/kernel/workqueue.c @@ -179,6 +179,7 @@ struct worker_pool { struct worker *manager; /* L: purely informational */ struct list_head workers; /* A: attached workers */ + struct list_head dying_workers; /* A: workers about to die */ struct completion *detach_completion; /* all workers detached */ struct ida worker_ida; /* worker IDs for task name */ @@ -1904,7 +1905,7 @@ static void worker_detach_from_pool(struct worker *worker) list_del(&worker->node); worker->pool = NULL; - if (list_empty(&pool->workers)) + if (list_empty(&pool->workers) && list_empty(&pool->dying_workers)) detach_completion = pool->detach_completion; mutex_unlock(&wq_pool_attach_mutex); @@ -1993,21 +1994,44 @@ static void rebind_worker(struct worker *worker, struct worker_pool *pool) WARN_ON_ONCE(set_cpus_allowed_ptr(worker->task, pool->attrs->cpumask) < 0); } +static void wake_dying_workers(struct list_head *cull_list) +{ + struct worker *worker, *tmp; + + list_for_each_entry_safe(worker, tmp, cull_list, entry) { + list_del_init(&worker->entry); + unbind_worker(worker); + /* + * If the worker was somehow already running, then it had to be + * in pool->idle_list when set_worker_dying() happened or we + * wouldn't have gotten here. + * + * Thus, the worker must either have observed the WORKER_DIE + * flag, or have set its state to TASK_IDLE. Either way, the + * below will be observed by the worker and is safe to do + * outside of pool->lock. + */ + wake_up_process(worker->task); + } +} + /** - * destroy_worker - destroy a workqueue worker + * set_worker_dying - Tag a worker for destruction * @worker: worker to be destroyed + * @list: transfer worker away from its pool->idle_list and into list * - * Destroy @worker and adjust @pool stats accordingly. The worker should - * be idle. + * Tag @worker for destruction and adjust @pool stats accordingly. The worker + * should be idle. * * CONTEXT: * raw_spin_lock_irq(pool->lock). */ -static void destroy_worker(struct worker *worker) +static void set_worker_dying(struct worker *worker, struct list_head *list) { struct worker_pool *pool = worker->pool; lockdep_assert_held(&pool->lock); + lockdep_assert_held(&wq_pool_attach_mutex); /* sanity check frenzy */ if (WARN_ON(worker->current_work) || @@ -2018,9 +2042,10 @@ static void destroy_worker(struct worker *worker) pool->nr_workers--; pool->nr_idle--; - list_del_init(&worker->entry); worker->flags |= WORKER_DIE; - wake_up_process(worker->task); + + list_move(&worker->entry, list); + list_move(&worker->node, &pool->dying_workers); } /* @@ -2062,11 +2087,24 @@ static void idle_worker_timeout(struct timer_list *t) /* * idle_cull_fn - cull workers that have been idle for too long. + * + * We don't want to disturb isolated CPUs because of a pcpu kworker being + * culled, so this also resets worker affinity. This requires a sleepable + * context, hence the split between timer callback and work item. */ static void idle_cull_fn(struct work_struct *work) { struct worker_pool *pool = container_of(work, struct worker_pool, idle_cull_work); + struct list_head cull_list; + INIT_LIST_HEAD(&cull_list); + /* + * Grabbing wq_pool_attach_mutex here ensures an already-running worker + * cannot proceed beyong worker_detach_from_pool() in its self-destruct + * path. This is required as a previously-preempted worker could run after + * set_worker_dying() has happened but before wake_dying_workers() did. + */ + mutex_lock(&wq_pool_attach_mutex); raw_spin_lock_irq(&pool->lock); while (too_many_workers(pool)) { @@ -2081,10 +2119,12 @@ static void idle_cull_fn(struct work_struct *work) break; } - destroy_worker(worker); + set_worker_dying(worker, &cull_list); } raw_spin_unlock_irq(&pool->lock); + wake_dying_workers(&cull_list); + mutex_unlock(&wq_pool_attach_mutex); } static void send_mayday(struct work_struct *work) @@ -2448,12 +2488,12 @@ static int worker_thread(void *__worker) /* am I supposed to die? */ if (unlikely(worker->flags & WORKER_DIE)) { raw_spin_unlock_irq(&pool->lock); - WARN_ON_ONCE(!list_empty(&worker->entry)); set_pf_worker(false); set_task_comm(worker->task, "kworker/dying"); ida_free(&pool->worker_ida, worker->id); worker_detach_from_pool(worker); + WARN_ON_ONCE(!list_empty(&worker->entry)); kfree(worker); return 0; } @@ -3527,6 +3567,7 @@ static int init_worker_pool(struct worker_pool *pool) timer_setup(&pool->mayday_timer, pool_mayday_timeout, 0); INIT_LIST_HEAD(&pool->workers); + INIT_LIST_HEAD(&pool->dying_workers); ida_init(&pool->worker_ida); INIT_HLIST_NODE(&pool->hash_node); @@ -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; 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);