From nobody Mon Apr 6 19:38:37 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 5DA70C433FE for ; Tue, 4 Oct 2022 15:06:16 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229908AbiJDPGN (ORCPT ); Tue, 4 Oct 2022 11:06:13 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55450 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229461AbiJDPGH (ORCPT ); Tue, 4 Oct 2022 11:06:07 -0400 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 E59C05A8A9 for ; Tue, 4 Oct 2022 08:06:06 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1664895966; 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=Q9IkfNrJjAEpG1neD9g4eIbNDeR16z8tF/Uf9UeR0cE=; b=KwtxrRwgamitWOPCviUpDM9luXcxXdapHeLgh+ucX6vPny6mrbxSILo2DfrhdPOyfz/vyr uyeSWMh4fYJzdoEhmSQ0ShSOSXjEEhHlI4sre4MWwk/0GALi+vymCosgZok0kI2kO97FnV qAlJE4Hd8JXuBmpY5bY9WmcVluqEr34= Received: from mail-wr1-f69.google.com (mail-wr1-f69.google.com [209.85.221.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-614-PdAdjIp3Nl-KwfyxDsiaug-1; Tue, 04 Oct 2022 11:06:04 -0400 X-MC-Unique: PdAdjIp3Nl-KwfyxDsiaug-1 Received: by mail-wr1-f69.google.com with SMTP id k30-20020adfb35e000000b0022e04708c18so2954440wrd.22 for ; Tue, 04 Oct 2022 08:06:03 -0700 (PDT) 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; bh=Q9IkfNrJjAEpG1neD9g4eIbNDeR16z8tF/Uf9UeR0cE=; b=QRsDsQzE2FwZt/my1IGuOBH6TA8f6JtKH+GlPT3TBiT5TWWJnvWIfsUi58NI7L76Nc ix9Fml2NjdKnzJR4m1yMMunUEn/eLspjI5K7J0rX2zOblOqRZVziwx2+gpy5N+m2+YBq 7GXR1EMXSoJpMRv6LsU2cOHOXBnxicpvoSX6dIvr22JlucjwMB9Ykc++4iSjR6I17VCD gjt9KgDAnXg2m9XjS6D0e1i4OCa2ECvOz8VGXG2BvqYsjVarTUDa4YqzCPzggF592f92 YrQvrVJrbOODBAYZtxhjtPsFougf/wrV0AAVmBv1ZH8exJx4bn8/QhGeBYOVGdHGEQCv OFPA== X-Gm-Message-State: ACrzQf2/rL7C4x+L0YmC914SmqzpsKKmBsQeIzRcek+BVghD9XRhNGzj JzByjiMZFh8yuyAMw9FuTaA6x3pOL1rbJnWDqFN0Nd0+SmoZJkJL4SzQj8G67WxFkTSguMPSPsO QptSCpd0kSGfzhBpQYRJ///0tbVboTvBpZIqLqdgbHTWpEat10dpPxuBmWb4eB79CqiOjusBHsT GT X-Received: by 2002:a05:6000:168c:b0:226:f4c2:d6db with SMTP id y12-20020a056000168c00b00226f4c2d6dbmr16186561wrd.659.1664895960911; Tue, 04 Oct 2022 08:06:00 -0700 (PDT) X-Google-Smtp-Source: AMsMyM6Y8f4VWUmIwlPgyvFhw+BUHewZq0tOElVMmS6BG7uBGH81oWmBHQSRDSUA5Smo5rVNwc1B6A== X-Received: by 2002:a05:6000:168c:b0:226:f4c2:d6db with SMTP id y12-20020a056000168c00b00226f4c2d6dbmr16186528wrd.659.1664895960594; Tue, 04 Oct 2022 08:06:00 -0700 (PDT) Received: from vschneid.remote.csb ([149.71.65.94]) by smtp.gmail.com with ESMTPSA id w10-20020a05600c474a00b003b4ac05a8a4sm25777717wmo.27.2022.10.04.08.05.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 04 Oct 2022 08:05:59 -0700 (PDT) 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 v4 1/4] workqueue: Protects wq_unbound_cpumask with wq_pool_attach_mutex Date: Tue, 4 Oct 2022 16:05:18 +0100 Message-Id: <20221004150521.822266-2-vschneid@redhat.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20221004150521.822266-1-vschneid@redhat.com> References: <20221004150521.822266-1-vschneid@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" 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 r= unning 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 7cd5f5e7e0a1..8e21c352c155 100644 --- a/kernel/workqueue.c +++ b/kernel/workqueue.c @@ -326,7 +326,7 @@ static struct rcuwait manager_wait =3D __RCUWAIT_INITIA= LIZER(manager_wait); static LIST_HEAD(workqueues); /* PR: list of all workqueues */ static bool workqueue_freezing; /* PL: have wqs started freezing? */ =20 -/* 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; =20 /* CPU where unbound work was last round robin scheduled from this CPU */ @@ -3952,7 +3952,8 @@ static void apply_wqattrs_cleanup(struct apply_wqattr= s_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; =20 /* - * 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); =20 /* * We may create multiple pwqs with differing cpumasks. Make a @@ -4072,7 +4074,7 @@ static int apply_workqueue_attrs_locked(struct workqu= eue_struct *wq, wq->flags &=3D ~__WQ_ORDERED; } =20 - ctx =3D apply_wqattrs_prepare(wq, attrs); + ctx =3D apply_wqattrs_prepare(wq, attrs, wq_unbound_cpumask); if (!ctx) return -ENOMEM; =20 @@ -5334,7 +5336,7 @@ void thaw_workqueues(void) } #endif /* CONFIG_FREEZER */ =20 -static int workqueue_apply_unbound_cpumask(void) +static int workqueue_apply_unbound_cpumask(const cpumask_var_t unbound_cpu= mask) { LIST_HEAD(ctxs); int ret =3D 0; @@ -5350,7 +5352,7 @@ static int workqueue_apply_unbound_cpumask(void) if (wq->flags & __WQ_ORDERED) continue; =20 - ctx =3D apply_wqattrs_prepare(wq, wq->unbound_attrs); + ctx =3D apply_wqattrs_prepare(wq, wq->unbound_attrs, unbound_cpumask); if (!ctx) { ret =3D -ENOMEM; break; @@ -5365,6 +5367,11 @@ static int workqueue_apply_unbound_cpumask(void) apply_wqattrs_cleanup(ctx); } =20 + if (!ret) { + mutex_lock(&wq_pool_attach_mutex); + cpumask_copy(wq_unbound_cpumask, unbound_cpumask); + mutex_unlock(&wq_pool_attach_mutex); + } return ret; } =20 @@ -5383,7 +5390,6 @@ static int workqueue_apply_unbound_cpumask(void) int workqueue_set_unbound_cpumask(cpumask_var_t cpumask) { int ret =3D -EINVAL; - cpumask_var_t saved_cpumask; =20 /* * Not excluding isolated cpus on purpose. @@ -5397,23 +5403,8 @@ int workqueue_set_unbound_cpumask(cpumask_var_t cpum= ask) goto out_unlock; } =20 - if (!zalloc_cpumask_var(&saved_cpumask, GFP_KERNEL)) { - ret =3D -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 =3D workqueue_apply_unbound_cpumask(); - - /* restore the wq_unbound_cpumask when failed. */ - if (ret < 0) - cpumask_copy(wq_unbound_cpumask, saved_cpumask); + ret =3D workqueue_apply_unbound_cpumask(cpumask); =20 - free_cpumask_var(saved_cpumask); out_unlock: apply_wqattrs_unlock(); } --=20 2.31.1