From patchwork Tue Nov 22 19:29:33 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Valentin Schneider X-Patchwork-Id: 24525 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:f944:0:0:0:0:0 with SMTP id q4csp2404192wrr; Tue, 22 Nov 2022 11:38:14 -0800 (PST) X-Google-Smtp-Source: AA0mqf6nZkA6dFhKhkwYJJCGwg6wNnr50AH5pZdFE7vhYOZrsMUvelu7ZaehSeb+aGu727I5yix+ X-Received: by 2002:a17:906:f106:b0:7b2:9a6b:9ac6 with SMTP id gv6-20020a170906f10600b007b29a6b9ac6mr12906686ejb.215.1669145894015; Tue, 22 Nov 2022 11:38:14 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1669145894; cv=none; d=google.com; s=arc-20160816; b=jRfUck/LtA0W7/6A2eG3D6LaDeAMY8oICHd71uyMGwn2FeOpgsoF3blNreNB3A74L/ S1WFnmCYk9m9p7AG58v0eBqcBEX4n7uJsmlJpS7LB7I0ZxwIsyoDMN++s0pHlV0Z6HJc ihUvAl+eK1KGJVWxUA8wl0DaCK1xld14XHh0sHUMRfpc4TRePOLSLXvpqs/N2zQeA9Xv uSRlg5pL1H15NXmm++pFRrCCjjpDWpnitLXdVAYFxY1ZC/2V1tpKd07wci9o8XesAo2h rBJ9BAfmDOijOi2vsWe6gnoBpjDAIwW1FnwzEN5bH/xXlGs1fzg/NZ1XvPXcwNsusgFP QDpg== 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=TFC+S/qmWgcv9BlT3iCdwBE5S3zLHL2goSgWX3iJigFH7Q4FAbfCv/KSMDjZcS4QW7 V53n2pBvwVbg8xPfXq231VRxDGcNNqNNzLP/ZGTTSnA7WoOL0KcaWUeidU6hGUzHNK9y VGhPQqmAXrzUwOpWnfeB3Bq8gwXa1foShEpw9LekQcy4nmjY9rL3fE44pX54OXsY7AXc XKfV8IT2J2Wj26QpbOdV0cramSpnczDKUAETgK25yJKE/vT2MGaJaMYf+ajrrVU9FtC1 KRZmfX58sihax27XrVzB+7ZNXoLRRqtQOTfvm1ZnACL6BW1/3wccIyhQdvnKdN1kaZWL ZrLA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=VfnByIqV; 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 xd9-20020a170907078900b007addbdb9fbbsi9142986ejb.558.2022.11.22.11.37.50; Tue, 22 Nov 2022 11:38:13 -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=VfnByIqV; 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 S234781AbiKVTbG (ORCPT + 99 others); Tue, 22 Nov 2022 14:31:06 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53018 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234490AbiKVTbD (ORCPT ); Tue, 22 Nov 2022 14:31:03 -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 52F8191C20 for ; Tue, 22 Nov 2022 11:30:08 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1669145407; 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=VfnByIqVcE/WN2vy495rsvv8O5A02XsAsNPBGNlvXUESLieZqxOfFMQkFESdgP/Ed+qwh1 3+KWhxEzPxyAHB3Nxd2Pvrpr3RbsszoxBxM91jPkeR8PRUteFyjr/yumLkeQlTDvRhhYYi lzftUFFQoXVj+TMPTF1MQxHyZtv32IA= Received: from mail-wm1-f71.google.com (mail-wm1-f71.google.com [209.85.128.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-534-nIhQ87hwPV6XmiETeSv6uw-1; Tue, 22 Nov 2022 14:30:06 -0500 X-MC-Unique: nIhQ87hwPV6XmiETeSv6uw-1 Received: by mail-wm1-f71.google.com with SMTP id ay19-20020a05600c1e1300b003cf758f1617so11456094wmb.5 for ; Tue, 22 Nov 2022 11:30:05 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=3juG06G3YKqZVFnwZoo8pO4nzDVQkrUL6djcNIQJnew=; b=JO1kgryaAVE/7IDO0+lJUHZcA9KskOAYtkHgGUSbcjzw2FZ4SayaJpAKS617u6mjJz ZLnGbfN29qqYE/1XTgCExfNA7ead3dO4gUvkmHt8XlbcFGkBDFAM1Po6lsSzDukqQlzW YKDTCIhl7txqwH9MKPMKkHyQM7ob2aEivvW3dowLF3pIoQOVd2oh3KrYcK4Bb5Fjlt8a 8wAzjnZGIJC1MvUFVEwxQ2NFPf1NobRaJb1HHAfcsTvcDYU/TMPj6XUZHwgdVFvFO7ot vT1A3iIVhkFzGr5G/qoZ2PnpLt+g3ZtfyRFql77VLWW0PHkdrkpnBzF/uUJkctuQfshV yeIQ== X-Gm-Message-State: ANoB5plFeIots5jFwJ3/R2Zd/MRU0hPXGlsW/46JmtgykUHKucTR8OUn +WyLYCaedopyEEbFHans62pou8JYzkrHoyk4TK5s7+OMRdj9GegF7z507PQvT5tc2Vs1DmMaaY2 tzHtPOscTxTphSxKeh/afLRtIuawjnSzkirw6YN/RVWKK7P1yGtSvUvtN2wZjk+FZkUo/nr6ePy ww X-Received: by 2002:a05:600c:538b:b0:3b4:7e87:895f with SMTP id hg11-20020a05600c538b00b003b47e87895fmr17780388wmb.30.1669145404376; Tue, 22 Nov 2022 11:30:04 -0800 (PST) X-Received: by 2002:a05:600c:538b:b0:3b4:7e87:895f with SMTP id hg11-20020a05600c538b00b003b47e87895fmr17780350wmb.30.1669145404034; Tue, 22 Nov 2022 11:30:04 -0800 (PST) Received: from vschneid.remote.csb ([154.57.232.159]) by smtp.gmail.com with ESMTPSA id r14-20020adff10e000000b002366553eca7sm4173682wro.83.2022.11.22.11.30.01 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 22 Nov 2022 11:30:03 -0800 (PST) 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 v5 1/5] workqueue: Protects wq_unbound_cpumask with wq_pool_attach_mutex Date: Tue, 22 Nov 2022 19:29:33 +0000 Message-Id: <20221122192937.2386494-2-vschneid@redhat.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20221122192937.2386494-1-vschneid@redhat.com> References: <20221122192937.2386494-1-vschneid@redhat.com> MIME-Version: 1.0 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?1750226325304960579?= X-GMAIL-MSGID: =?utf-8?q?1750226325304960579?= 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 Tue Nov 22 19:29:34 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Valentin Schneider X-Patchwork-Id: 24529 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:f944:0:0:0:0:0 with SMTP id q4csp2404910wrr; Tue, 22 Nov 2022 11:40:01 -0800 (PST) X-Google-Smtp-Source: AA0mqf7k5bBRjHEtaym9KevuFO5eoqUX+F6/bLAudzSARLS0PSuoHQABftvvi01p2MMYk1STVRwQ X-Received: by 2002:aa7:c145:0:b0:469:400a:3f8e with SMTP id r5-20020aa7c145000000b00469400a3f8emr9047602edp.108.1669146001507; Tue, 22 Nov 2022 11:40:01 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1669146001; cv=none; d=google.com; s=arc-20160816; b=k4GlAK4S2yPoVAbzG0wF5IPHnfZ7O6lXX3u2wraQjPu/IdlzUtVTMTcRy5xnqctnxK s+1/k/qXzbA1EctZneYkqbqiu8De8w+jsKAY/hfi7dZcbopLnxQ8Md+KDkuVsfQy73SH H3EQu4TSwHABYLEXFiUydrt03sbPE3yMV5NjMZ2InVV912tCGk6PLfnX7cz+/L/WFRR4 BGq/B/hopIDLjziQIgpFuc7vkiE/5e9u7aqN/wEpwrI4Mkbx80pofUT5R9g+xcANaVgd hkuXFnlUrJhHPb3HPz/cvj8A3c8OvmqyA8Tb7aLSUX28IhzuBttOpiA4NJH51k6zs3UI kfdw== 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=0fA/SK8uF+4WddPpa/zAkhdVNVR8ka27hC6MhSQhWi/IvsSNlY0HrqG7FjL/Zt81A6 zGfgZCQi0C+4Nuq8CNSqTQgFoRCnl+26Q9QIjnf0XXAG/F+QOjoYH5oFxn0Bmfplid6q SCNJroBre5P9WShZNkBXbjJhLyvK4FbPcTWeVe3NzMg2ktbGoZIrzcGFLGh+Coi2wpnn nimlNBmEI/DzHxPCRTY8fCmk9C/nUDMzqbexPxmw1uW4UISgl7Xt/3EM8v9+D9uMPBhP xEEVFyJq2avhST0ZOl4u9lt8Q/7Gtxz0QFbeKtX7assrlHM3IgdkHvEBm0nzNT2hcB4E RhQQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=cHfYR5FI; 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 hz3-20020a1709072ce300b007b29a6b7bd2si12579200ejc.662.2022.11.22.11.39.34; Tue, 22 Nov 2022 11:40:01 -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=cHfYR5FI; 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 S234828AbiKVTbY (ORCPT + 99 others); Tue, 22 Nov 2022 14:31:24 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53066 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234792AbiKVTbK (ORCPT ); Tue, 22 Nov 2022 14:31:10 -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 471CC92B42 for ; Tue, 22 Nov 2022 11:30:10 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1669145409; 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=cHfYR5FI7gs5KBa6wABcFhvST4XH2TkGe23cXdLSkO9pwTnjUWPOaEF53H7CpTH6AeHVSe 0b7udjKznrEPwTz9cGSXiwUYH2BqO8eL5EEfj4W4TGKCqUAzM14iyavvetXGnAauNwzCQV puRlq1gEnBTpfFeLEqGMlGc54s8IxmQ= Received: from mail-wm1-f70.google.com (mail-wm1-f70.google.com [209.85.128.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-668-xi6gbFknMy63a4Nqet-pkw-1; Tue, 22 Nov 2022 14:30:08 -0500 X-MC-Unique: xi6gbFknMy63a4Nqet-pkw-1 Received: by mail-wm1-f70.google.com with SMTP id q12-20020a1ce90c000000b003d00f3fe1e7so3324197wmc.4 for ; Tue, 22 Nov 2022 11:30:08 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=eQtqTu7JRNjFnV6o8lHrVhZyrgiyx0mHVqwoCilAwG0=; b=SvHyq3eXfvSOdqegQmBFaCzDlYTYSNyKAVikb84E23LpwpoV1qdHXRGWYNWbHpmb8y DcTnkXlwasOfXGWGwBpASd2HHolMYsmcOkxmW5/j6pPpFFglycJ0KxlGCcpo2ke53R8N /OPcSvRdHhzkUjOfy+F4qFRgKqKGhtzoiq8CYDa5gOJmg0ebEslvF4KWuO1loAForNzG S8EzAVoxgGBNUi9JWoNVUsCTbTNjyi9M83+TR4GTq07RdPOUaS78Ly/ajE8ryzbWK1gS 8vZy9W3wTZwHmrpLNmhOo4nZkNFq1Bsp2F0Ui0UaZIWPmPaFmw6OU1zUC/3UvTlE1yWP SZhA== X-Gm-Message-State: ANoB5pljov2ALT0Jv5J1ZjRyPYi48TaGG0FYs8IpV7ftK1uSdQ8hnI4V CiP2Y/mjoJXL6qPDxEvoVAdkQva+e/05/+BZ6ztoTnmHCnNjgQxtuVpVz2w3CZ/jVowTmXDzHEy CXU9LobQGecrfT9TowMcZkAy9trp8Fp73O3v5Z8KVDG94aT0hXZZFxUl1CY9WEpyZIV/1pjRm6w WW X-Received: by 2002:a05:600c:3ba1:b0:3d0:47c:9ff5 with SMTP id n33-20020a05600c3ba100b003d0047c9ff5mr10845525wms.121.1669145406535; Tue, 22 Nov 2022 11:30:06 -0800 (PST) X-Received: by 2002:a05:600c:3ba1:b0:3d0:47c:9ff5 with SMTP id n33-20020a05600c3ba100b003d0047c9ff5mr10845499wms.121.1669145406296; Tue, 22 Nov 2022 11:30:06 -0800 (PST) Received: from vschneid.remote.csb ([154.57.232.159]) by smtp.gmail.com with ESMTPSA id r14-20020adff10e000000b002366553eca7sm4173682wro.83.2022.11.22.11.30.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 22 Nov 2022 11:30:05 -0800 (PST) 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 v5 2/5] workqueue: Factorize unbind/rebind_workers() logic Date: Tue, 22 Nov 2022 19:29:34 +0000 Message-Id: <20221122192937.2386494-3-vschneid@redhat.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20221122192937.2386494-1-vschneid@redhat.com> References: <20221122192937.2386494-1-vschneid@redhat.com> MIME-Version: 1.0 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?1750226437758922372?= X-GMAIL-MSGID: =?utf-8?q?1750226437758922372?= 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 Tue Nov 22 19:29:35 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Valentin Schneider X-Patchwork-Id: 24524 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:f944:0:0:0:0:0 with SMTP id q4csp2404173wrr; Tue, 22 Nov 2022 11:38:11 -0800 (PST) X-Google-Smtp-Source: AA0mqf5ODp/insdYZi8C1jhxXq47am49X55ohDHPIh2AqhZ2KPf2WpXJXNJI3mqGQxHd1HL6uXYu X-Received: by 2002:a17:902:c085:b0:186:ff42:704d with SMTP id j5-20020a170902c08500b00186ff42704dmr8153326pld.153.1669145891382; Tue, 22 Nov 2022 11:38:11 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1669145891; cv=none; d=google.com; s=arc-20160816; b=SHQIWcy06S5d0v/k3s44TDlPAfFLIGL3MRrNTzX008K+Lxb9iV/HwwRDkHQ91daJi0 vrxtg0QD5HS9spzeTSHG2V44tFLlG+jDwkHadCI3wxC+kIpY/DMQ6nyFg85C2QTxtroC tCLpiyYzlDIvsuav/UUj3Fvrf5VwyRFtBhhGw6rlhGoBliULTAtqqYPSpYFSSKdJRo+m 1vl18yX4kNnS0ZfnXO6ElvspCCQCMIK2Mlm7nhd05pkCXRZMrmQqWlXhS/+PJiBM+t5Y D77N+r0zdB+tVxIhvCR/FtwSVt57IF/q/dQI7FzqISqHHXQbcKd9dtT+d6LfUPSZoeuO oLAQ== 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=zSYjp+tSNrVSXI+gcTD2+vnKTaoJtRYmv4jLOiB9aTk=; b=OhcKKR5aEv91JfzHxL+o/kO6W3HcLm61RB2gHq2keKxjy7vgOLZ4AZC/7X5uSRVOqg lvjXwlorQ6wIb8c77fWY8arkPJkpVq1uyfoB8edNMPZFg6XOWenAjNm/cKfR8jYusFu6 m/Xo6WVRHI2xTGI4JQnIAo8bj9gi/LL6kWdCdD2GPdnej/OOGbFYyT5whNPcoTZ+R5b+ 51ovAM1zAsUZRPqnalFYtnWWseLsKN9Gzp0tXSI59QQ1W/fMyLcQYyZD007mouVoYLEu wFD04U+pZ+OSEK5CO6dzblJSnKRMbdjmwgw8GC1mhh+u6bP0e3V6H88pIKRVDrdiYzNK 6LJg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=Rj7zYd9s; 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 a11-20020a056a000c8b00b0056301324a24si15643334pfv.133.2022.11.22.11.37.57; Tue, 22 Nov 2022 11:38:11 -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=Rj7zYd9s; 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 S234795AbiKVTbN (ORCPT + 99 others); Tue, 22 Nov 2022 14:31:13 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53052 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234786AbiKVTbJ (ORCPT ); Tue, 22 Nov 2022 14:31:09 -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 4405690580 for ; Tue, 22 Nov 2022 11:30:12 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1669145411; 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=zSYjp+tSNrVSXI+gcTD2+vnKTaoJtRYmv4jLOiB9aTk=; b=Rj7zYd9sX37WDqapEFV0pXGQtt5+P+0ASTSLUcILy0kzdaLWZNSdd4u5pDLjIgMbERvh44 k1Oe0TryM+9RAK5jNbuz69gnWvvoeajybO0zb7WnJZ0I2UI0met9X+mk7nwZ6YSzwGVmit p7jQZEFbo5HNhLSoY5idXhRqa6hyphU= Received: from mail-wm1-f69.google.com (mail-wm1-f69.google.com [209.85.128.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-195-PEED_0QJOSOISy9CPCxUwQ-1; Tue, 22 Nov 2022 14:30:10 -0500 X-MC-Unique: PEED_0QJOSOISy9CPCxUwQ-1 Received: by mail-wm1-f69.google.com with SMTP id h9-20020a1c2109000000b003cfd37aec58so8516285wmh.1 for ; Tue, 22 Nov 2022 11:30:10 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=zSYjp+tSNrVSXI+gcTD2+vnKTaoJtRYmv4jLOiB9aTk=; b=RilhoFZ1OgHRw7pFXrl0yVmXTZKGdxyW3V3WxmEJrfaLmK4DNme8Jec07LtFsML9Bj LwsbkyjkM2isXPrhbbb5EnJFQX7cexHNb9M+QAykL+5HSFOmkutyDTi4p4Rq4QHhHS48 q6jl4q4+2Xp0UgAvJqWe3OWAxla5jldo0rT9iY28xvjrI/zkNPw+3ofdCPwNDlbm6Guw P7r5jUzxuiIep3iW6X42KE5R4y54rBATRhUVmkBJesEurMNeTrvK3xnGUI+x9VCyYXw0 whXABf/3BJw9oIroMOsRU7YqH29w/ef+bsiAxoYAXuXCe6tcNuZyEL4689HSAW5i+1rW PV1A== X-Gm-Message-State: ANoB5pkTEyQWVZmTCxIXY5uyk8nhZ03ywEnafzrnAf4/7i3rYelY1b38 jnr9aSjJdVbh/18mAaqHZsZBhreOEQYRBS1VkkW273B/HGK8HqseQVL1l1qaxtMGRG3yPuCwHsP uw2/GtQkWZjfZsN4J8CSajAdTnnHYjgfMOrNXmVb07DOXqVh35LUfr5/rm97KuWYMTHcjtfI1zO ok X-Received: by 2002:a05:600c:6545:b0:3cf:baef:e92a with SMTP id dn5-20020a05600c654500b003cfbaefe92amr5385800wmb.178.1669145408533; Tue, 22 Nov 2022 11:30:08 -0800 (PST) X-Received: by 2002:a05:600c:6545:b0:3cf:baef:e92a with SMTP id dn5-20020a05600c654500b003cfbaefe92amr5385782wmb.178.1669145408300; Tue, 22 Nov 2022 11:30:08 -0800 (PST) Received: from vschneid.remote.csb ([154.57.232.159]) by smtp.gmail.com with ESMTPSA id r14-20020adff10e000000b002366553eca7sm4173682wro.83.2022.11.22.11.30.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 22 Nov 2022 11:30:07 -0800 (PST) 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 v5 3/5] workqueue: Make too_many_workers() return the worker excess Date: Tue, 22 Nov 2022 19:29:35 +0000 Message-Id: <20221122192937.2386494-4-vschneid@redhat.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20221122192937.2386494-1-vschneid@redhat.com> References: <20221122192937.2386494-1-vschneid@redhat.com> MIME-Version: 1.0 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?1750226322292470211?= X-GMAIL-MSGID: =?utf-8?q?1750226322292470211?= Later patches will need the logic implemented within too_many_workers() to get the amount of workers to delete. Rather than duplicate the logic, rework too_many_workers() to return the count of workers to delete - its return value can be used as a boolean value, so no change in behaviour intended. The function currently returns true when (nr_idle - 2) * MAX_IDLE_WORKERS_RATIO >= nr_busy thus, the desired number of idle workers is expressed by (nr_idle - 2) * MAX_IDLE_WORKERS_RATIO == nr_busy - 1 IOW nr_idle == ((nr_busy - 1) / MAX_IDLE_WORKERS_RATIO) + 2 MAX_IDLE_WORKERS_RATIO being a compile-time power of 2, we can leave that as a division. While at it, rename too_many_workers() to worker_cull_count(). Signed-off-by: Valentin Schneider --- kernel/workqueue.c | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/kernel/workqueue.c b/kernel/workqueue.c index 8185a42848c50..4fc8085f3fe17 100644 --- a/kernel/workqueue.c +++ b/kernel/workqueue.c @@ -816,14 +816,20 @@ static bool need_to_create_worker(struct worker_pool *pool) return need_more_worker(pool) && !may_start_working(pool); } -/* Do we have too many workers and should some go away? */ -static bool too_many_workers(struct worker_pool *pool) +/* How many idle workers should we get rid of, if any? */ +static unsigned int worker_cull_count(struct worker_pool *pool) { bool managing = pool->flags & POOL_MANAGER_ACTIVE; int nr_idle = pool->nr_idle + managing; /* manager is considered idle */ int nr_busy = pool->nr_workers - nr_idle; - return nr_idle > 2 && (nr_idle - 2) * MAX_IDLE_WORKERS_RATIO >= nr_busy; + lockdep_assert_held(&pool->lock); + + /* + * We keep at least 2 spare idle workers, but overall aim to keep at + * most (1 / MAX_IDLE_WORKERS_RATIO) workers idle. + */ + return max(0, nr_idle - 2 - ((nr_busy - 1) / MAX_IDLE_WORKERS_RATIO)); } /* @@ -1806,7 +1812,7 @@ 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 (worker_cull_count(pool) && !timer_pending(&pool->idle_timer)) mod_timer(&pool->idle_timer, jiffies + IDLE_WORKER_TIMEOUT); /* Sanity check nr_running. */ @@ -2025,7 +2031,7 @@ static void idle_worker_timeout(struct timer_list *t) raw_spin_lock_irq(&pool->lock); - while (too_many_workers(pool)) { + while (worker_cull_count(pool)) { struct worker *worker; unsigned long expires; From patchwork Tue Nov 22 19:29:36 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Valentin Schneider X-Patchwork-Id: 24526 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:f944:0:0:0:0:0 with SMTP id q4csp2404628wrr; Tue, 22 Nov 2022 11:39:15 -0800 (PST) X-Google-Smtp-Source: AA0mqf6cAIxoi+2l8OHX9RxkgNO+2ALTAgHHWft5hvCUg1kcJAmVFDtx+nVCX0zQnN3FwQxVQMMp X-Received: by 2002:a17:90a:b382:b0:213:1fcb:3ce1 with SMTP id e2-20020a17090ab38200b002131fcb3ce1mr26732476pjr.58.1669145955198; Tue, 22 Nov 2022 11:39:15 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1669145955; cv=none; d=google.com; s=arc-20160816; b=kittNlWv2oTDJq3MgcSMXb3jNhpmgONl6z217bdo968HzPPBUMBntO6Z12BZVihVsm f7reej9o8iBo5UdDCSWyOfdHWsG3/d2SxbJQ6oxaVnKVysiZK1OrwBXp19NdabfH9OBb OOBvIS0XmIEpdThAY0bixWo392kYgEkHpKzRs/82nGj0b37rsTPENV5FjL2YSBmFxgM1 6VmLqMHrKQ4h4BYRcJEJNCsYnmWEGCwugOgWW5dJQ4Kw+i9BinSZqX+Cz5Q5mAM6FBy7 +LJRXHLhsbuI8wM/SU/8FpOFgm+m1YJ6tQKF2kaGGF79qzE3PQ/+fgCbW6svXLdx+WW6 WacA== 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=7U2dCc3qRrIxneK1dml4DuolDvzGGs0bA5uFiU/5J/w=; b=iY/2efwO4dvGuvlZO4GRI9Uz4yHQbM4lDV9H4N2z5HYPpTVE7KxG0JqO+y7D253JO9 PjrDkxpHFHgPHVs/yovEyMkENatlIbqlrX4o2WbT3JGJWkmTnrPq56ooHCww7SaOknRY +5UjvRjR81eZc3DX3QH5Z5phdiHFquzqjOrfCwVDszdeTO6vsVfwLBFkzkVjX9OUMohd OGwtn+MDJ/tHzzUv0RdtIjG1C818s540huI7m52gDZc9fp1+a+UwUnDly31DQSEqBDx3 G4wKTqv1RkWQiZspj+vYuhTi7KQB5xLPJTh2MCfTeGaoCtaWOiqg17kYfRTp1u1CXwMo Tf8w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=ZZ8Jrq+R; 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 l189-20020a6388c6000000b004561c1f132csi1864320pgd.346.2022.11.22.11.39.01; Tue, 22 Nov 2022 11:39:15 -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=ZZ8Jrq+R; 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 S234834AbiKVTb2 (ORCPT + 99 others); Tue, 22 Nov 2022 14:31:28 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53082 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234784AbiKVTbP (ORCPT ); Tue, 22 Nov 2022 14:31:15 -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 2231E91C39 for ; Tue, 22 Nov 2022 11:30:15 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1669145414; 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=7U2dCc3qRrIxneK1dml4DuolDvzGGs0bA5uFiU/5J/w=; b=ZZ8Jrq+RhtbUkyOCVmBG1UsELTl6gC9ZgzEsGY+LgVM2vtM/MFMYoZdY64fY+m7uy0FUEK D8NnsnyHmvMJ3F2o7uD5bqT6+KN4QyxX6mDKhqZpyWnDtuB/4SWwIeaVHToGOQt27tJmI6 J0DNi9vsXAEmVHgBdCQmQM3o6dTxxkw= Received: from mail-wm1-f71.google.com (mail-wm1-f71.google.com [209.85.128.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-473-fdOSotJyN0iDnSdxJ8NFog-1; Tue, 22 Nov 2022 14:30:13 -0500 X-MC-Unique: fdOSotJyN0iDnSdxJ8NFog-1 Received: by mail-wm1-f71.google.com with SMTP id h9-20020a1c2109000000b003cfd37aec58so8516355wmh.1 for ; Tue, 22 Nov 2022 11:30:12 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=7U2dCc3qRrIxneK1dml4DuolDvzGGs0bA5uFiU/5J/w=; b=b2l7l+6S7D9iLlBALUOicRmoqgyedzBuKma/rp80wDLWuEPxB2rZyDNNZJSlO2etxw k12Ux5Bnnh3CZrBa1lC2AnsMH5ILWoO1WgvyonbPR6RpBki6f6Uizp07Grwqc0SO4n98 joa9ZdskA9y6vc8uuo+jRtAWDU1xKuGnOpst056EctecCImjMvLZj3NNJ/jP7Zks42sg jmzgENDMN5yu7Paf2U7d1DLlkqrS1MPMz5gLenE1egoS3rSGr7CspUbEyJXEj/EJDJZf geqo8+rOsFXffGs+cb73XInXX8J+SYX4fmaynj8YKbcasB+ajLIOdImtXwPgaXeIuDJL SwYg== X-Gm-Message-State: ANoB5pkl1/aF6oEV7yhYosmWiGiEjwR7liRPGNloYn9XieCrAxC3Vaq2 DN/wbVkg6KnY7c4WbSt5bjXA/v5HbM3x6zXRlbAdDl5FN0tCoTfUPcDA3+8hCVFonHHD49//hW4 v/EIe9rEeaKqroiGSSS9leXWFTEeHCzSVXuWnZI5UEBGKvT+JtVsfgzixNOfSXAOz7I0NjtDU0g er X-Received: by 2002:a05:600c:a10:b0:3cf:75f4:794a with SMTP id z16-20020a05600c0a1000b003cf75f4794amr21090175wmp.16.1669145411271; Tue, 22 Nov 2022 11:30:11 -0800 (PST) X-Received: by 2002:a05:600c:a10:b0:3cf:75f4:794a with SMTP id z16-20020a05600c0a1000b003cf75f4794amr21090155wmp.16.1669145411045; Tue, 22 Nov 2022 11:30:11 -0800 (PST) Received: from vschneid.remote.csb ([154.57.232.159]) by smtp.gmail.com with ESMTPSA id r14-20020adff10e000000b002366553eca7sm4173682wro.83.2022.11.22.11.30.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 22 Nov 2022 11:30:10 -0800 (PST) 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 v5 4/5] workqueue: Convert the idle_timer to a timer + work_struct Date: Tue, 22 Nov 2022 19:29:36 +0000 Message-Id: <20221122192937.2386494-5-vschneid@redhat.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20221122192937.2386494-1-vschneid@redhat.com> References: <20221122192937.2386494-1-vschneid@redhat.com> MIME-Version: 1.0 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?1750226388896350677?= X-GMAIL-MSGID: =?utf-8?q?1750226388896350677?= 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. The new worker_pool.idle_cull_list is made ____cacheline_aligned to prevent it from sitting over two cachelines. Signed-off-by: Valentin Schneider --- kernel/workqueue.c | 68 +++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 64 insertions(+), 4 deletions(-) diff --git a/kernel/workqueue.c b/kernel/workqueue.c index 4fc8085f3fe17..b744288c58a4b 100644 --- a/kernel/workqueue.c +++ b/kernel/workqueue.c @@ -169,7 +169,12 @@ 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 list_head idle_cull_list /* L: list of idle workers to cull */ + ____cacheline_aligned; + 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); @@ -1812,7 +1817,9 @@ static void worker_enter_idle(struct worker *worker) /* idle_list is LIFO */ list_add(&worker->entry, &pool->idle_list); - if (worker_cull_count(pool) && !timer_pending(&pool->idle_timer)) + if (worker_cull_count(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. */ @@ -2025,13 +2032,27 @@ 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 worker_cull_count() 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); + unsigned int max_cull_cnt, cull_cnt; + + if (work_pending(&pool->idle_cull_work)) + return; raw_spin_lock_irq(&pool->lock); - while (worker_cull_count(pool)) { + max_cull_cnt = worker_cull_count(pool); + for (cull_cnt = 0; cull_cnt < max_cull_cnt; cull_cnt++) { struct worker *worker; unsigned long expires; @@ -2039,12 +2060,48 @@ static void idle_worker_timeout(struct timer_list *t) worker = list_entry(pool->idle_list.prev, struct worker, entry); expires = worker->last_active + IDLE_WORKER_TIMEOUT; + /* All remaining entries will be younger than this */ if (time_before(jiffies, expires)) { - mod_timer(&pool->idle_timer, expires); + if (!cull_cnt) + mod_timer(&pool->idle_timer, expires); break; } + /* + * Mark the idle worker ripe for culling. + * If a preempted idle worker gets to run before the idle cull + * handles it, it will just pop itself out of that list and + * continue as normal. + */ + list_move(&worker->entry, &pool->idle_cull_list); + } + raw_spin_unlock_irq(&pool->lock); + + if (cull_cnt) + 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); + struct worker *worker, *tmp; + + raw_spin_lock_irq(&pool->lock); + + list_for_each_entry_safe(worker, tmp, &pool->idle_cull_list, entry) destroy_worker(worker); + + /* Re-arm the idle timer if necessary */ + if (pool->nr_idle) { + unsigned long expires; + + worker = list_entry(pool->idle_list.prev, struct worker, entry); + expires = worker->last_active + IDLE_WORKER_TIMEOUT; + if (time_before(jiffies, expires)) + mod_timer(&pool->idle_timer, expires); } raw_spin_unlock_irq(&pool->lock); @@ -3482,9 +3539,11 @@ static int init_worker_pool(struct worker_pool *pool) pool->watchdog_ts = jiffies; INIT_LIST_HEAD(&pool->worklist); INIT_LIST_HEAD(&pool->idle_list); + INIT_LIST_HEAD(&pool->idle_cull_list); 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); @@ -3632,6 +3691,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 Tue Nov 22 19:29:37 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Valentin Schneider X-Patchwork-Id: 24527 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:f944:0:0:0:0:0 with SMTP id q4csp2404804wrr; Tue, 22 Nov 2022 11:39:48 -0800 (PST) X-Google-Smtp-Source: AA0mqf71afoGUPsyVC3u17bRbxp7ab8pl27UfVuZS3R+1wrRX5culNyX7SFctmNG+GBhB5bBR9oq X-Received: by 2002:a17:906:bc86:b0:78d:8580:8e07 with SMTP id lv6-20020a170906bc8600b0078d85808e07mr20649225ejb.131.1669145988045; Tue, 22 Nov 2022 11:39:48 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1669145988; cv=none; d=google.com; s=arc-20160816; b=UZ2WMDntGYCEUgPPu63cjKeUVpb5Dh/th4LPgaGEzpXPmPUMk4O0ZEH23BRxetqWeE fH2tmeocbhN2T3FdewjhzJ6n5QYshEC4absRPhqbCoEfv4lHh8AUPocvxdTL/rIFtQYC lii3nHkkcOd4dK2Cpb3n1lpiSzUrCl+OF3H5rno122VbfEU1hyh0w/Wdg1EIUN5w9Gp6 qBYAfV85FaCImGyDd79rtkBI/rTr7RrCi0zV1xZHegx3WC25XkHM2i1IRZaGjqSPLdTS 4yRkgoTAExXtX6f+gou2Z/44dCg7FAOJv3mtrpCV2yk0Bhsa/dGcmBzTpTda+YMab2x2 OBKA== 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=CqWXx4B31Pn7PRAztrkQHi3QWAEH1+l9z4s87lcuEGU=; b=RdUGNzeYzuJW48EpcJaZyOHfxir0d0sJOa/VE8XFgXYsY0p3nkO8dqe003lk+jJf84 /IYDNWMuSU0ujwxrLY58u0ncTTeuR1m3zYjNyu/vnxBoXe44ub0ZB5G59h9mSFtAsM53 077KsegZkF0Z/pu/j4U6cc6GOAVeI9gfwpahxfvIJkAAPSMiVz++Cy23PA5OUDwADit0 UO27E7yTkSKPtMb1UDvlwzHM09MY2ufWx/uvs1ndVB/7kp+9KiwgnZUTNHA2JYnwx9Rc VC7G0jl6q+2OZWJMiUpuNg9lFhTZeCtGtexokhQ0WNGcrEhzqZxYEe6CG2ACJOe36LeZ SPyw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=eXePOJfH; 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 m14-20020a056402430e00b00457f31c1a10si13480816edc.584.2022.11.22.11.39.24; Tue, 22 Nov 2022 11:39:48 -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=eXePOJfH; 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 S234840AbiKVTbd (ORCPT + 99 others); Tue, 22 Nov 2022 14:31:33 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53022 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234814AbiKVTbS (ORCPT ); Tue, 22 Nov 2022 14:31:18 -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 7085964A3B for ; Tue, 22 Nov 2022 11:30:17 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1669145416; 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=CqWXx4B31Pn7PRAztrkQHi3QWAEH1+l9z4s87lcuEGU=; b=eXePOJfHHrZcd/3iEtscmaRl3MuCUbaOdoVPiBz4tNnO4LD6G0N3kEor/m+kNImOdkeJDQ d8cgL0H4nMpbxukbssANVh3SeP3yhghyf/YqsUgRHYb7gJg2vMPe86ZMaFlj7XmaaZWtRY apHBO37iVVKo9IAe4jiK67ADPVt3NzA= Received: from mail-wm1-f71.google.com (mail-wm1-f71.google.com [209.85.128.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-299-g8mLuvuzMXesy8YiCCE_fw-1; Tue, 22 Nov 2022 14:30:15 -0500 X-MC-Unique: g8mLuvuzMXesy8YiCCE_fw-1 Received: by mail-wm1-f71.google.com with SMTP id m34-20020a05600c3b2200b003cf549cb32bso11467119wms.1 for ; Tue, 22 Nov 2022 11:30:14 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=CqWXx4B31Pn7PRAztrkQHi3QWAEH1+l9z4s87lcuEGU=; b=RGIcXdzoMxvCZZl4Xx1n0Gbm1Kiw52Y4jzYVzz2lDwdN/hsb2JP9wcJCXksOIC32xu ST8klSak6ktylcUUIhxnmTIBj3dQvQIgxKX2KVtqvI43WS92m6BNqF12gkmGES/Nb/zX kuC15t3yvaOESD2S1yUuEIszRxVnkBKlosO17ANmTkzPgcwjTwyhxcCzKo1yYEHH9QPw D/CcosJONHKwVjHgshDSDb/QFKsTnf2YUji+9A0IckJpaO04ewN3CPr73ZqYL44M/I3v XCvkastWIMcwsjRXCK8EbnWivUk1KUZJUNdVijsS/KKe4Nfqn6PFe7Kshc1rahnUkSiW +UXw== X-Gm-Message-State: ANoB5pm9YdVLVeTltpc1H01wYYllE5KVRq3xWQGi3AYw+R8pZb9lFJo9 ENqnZpQ/ZHG47wWU2XGwEyzQcJ9FTTnSy6UVfBh87+DczAwDSUE3PqlS5SlGxPnWUJ8Jq9updbP x83se9diXCQS5SvNegjmjGetumV8V9BhasmXwwiSrW5bY3AYC2cahYMvgiUR/RbG3AE6DC14+yA 4G X-Received: by 2002:a05:600c:3501:b0:3c7:4f0:77b3 with SMTP id h1-20020a05600c350100b003c704f077b3mr4308575wmq.93.1669145413312; Tue, 22 Nov 2022 11:30:13 -0800 (PST) X-Received: by 2002:a05:600c:3501:b0:3c7:4f0:77b3 with SMTP id h1-20020a05600c350100b003c704f077b3mr4308554wmq.93.1669145413058; Tue, 22 Nov 2022 11:30:13 -0800 (PST) Received: from vschneid.remote.csb ([154.57.232.159]) by smtp.gmail.com with ESMTPSA id r14-20020adff10e000000b002366553eca7sm4173682wro.83.2022.11.22.11.30.11 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 22 Nov 2022 11:30:12 -0800 (PST) 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 v5 5/5] workqueue: Unbind kworkers before sending them to exit() Date: Tue, 22 Nov 2022 19:29:37 +0000 Message-Id: <20221122192937.2386494-6-vschneid@redhat.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20221122192937.2386494-1-vschneid@redhat.com> References: <20221122192937.2386494-1-vschneid@redhat.com> MIME-Version: 1.0 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?1750226423153495834?= X-GMAIL-MSGID: =?utf-8?q?1750226423153495834?= 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 b744288c58a4b..8ce3b63851e95 100644 --- a/kernel/workqueue.c +++ b/kernel/workqueue.c @@ -182,6 +182,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 */ @@ -1913,7 +1914,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); @@ -2002,21 +2003,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) || @@ -2027,9 +2051,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); } /* @@ -2083,16 +2108,29 @@ 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 disturbed 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 worker *worker, *tmp; + 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); list_for_each_entry_safe(worker, tmp, &pool->idle_cull_list, entry) - destroy_worker(worker); + set_worker_dying(worker, &cull_list); /* Re-arm the idle timer if necessary */ if (pool->nr_idle) { @@ -2105,6 +2143,8 @@ static void idle_cull_fn(struct work_struct *work) } 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) @@ -2468,12 +2508,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; } @@ -3548,6 +3588,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); @@ -3648,8 +3689,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) @@ -3672,17 +3716,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);