From nobody Sun Feb 8 17:36:47 2026 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 7D9F75F557 for ; Sat, 3 Feb 2024 15:44:04 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706975046; cv=none; b=VRgUPQIJG5Mzfn7V1l8PpdfzTdPMrXtRgtcdu5H06zRSPnb+7N3PTVg7mk6TroMQUPlLo29ZBqiychHb3En/EbVi2EoDwGM6vaoctPnpD0xJOqbbovZjoqy40rao/Qw+ITdwUnO7NVmCiJicW6LpzovOi/230Lwo+knqu/6Sbq0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706975046; c=relaxed/simple; bh=N0NFN9G6d8VckDtQzeRTKSXMqsIlgHOkxLBCqDdAB+Q=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=il4Sa6mfIUTG/EW+ZGY8hJ+QA+5a5gYJJpwLOSE6kqdVYK5lu80p2GQXt43zQKXu6AEKbRXLsXEudfssY/9ClEYGPwEbWwxGxxFAOMtCDDIMzWjJHCmJ9fPdzuhri14wPVHkFALMiOCl1lpxmCQ61jg7sZK7OPNvDVutskLSq8o= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=UN3lK1be; arc=none smtp.client-ip=170.10.133.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="UN3lK1be" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1706975043; 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=Qd3lbhuW8fX+DsS5188RrMwd7iN3OLF290p+wTUe/dc=; b=UN3lK1beGPMzr4dsr0b9cKs1uKH4G8ZD4wM/6pq5boXAdvfSSB9ecrL9+gYDoiyWojaTF3 iibRtWmVBWp1TxO/Dy+8o6zt8nXsVt86g4EY9QNtODUIcx8W//QiaB+R2bU0OflAioNEkH EnxD8VlM8ve11EB+dtVP3xgDatRq5oI= Received: from mimecast-mx02.redhat.com (mx-ext.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-350-XtEwpxLtMrWxLbKcxe-wAw-1; Sat, 03 Feb 2024 10:43:59 -0500 X-MC-Unique: XtEwpxLtMrWxLbKcxe-wAw-1 Received: from smtp.corp.redhat.com (int-mx09.intmail.prod.int.rdu2.redhat.com [10.11.54.9]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 39BB73C0C136; Sat, 3 Feb 2024 15:43:59 +0000 (UTC) Received: from llong.com (unknown [10.22.32.36]) by smtp.corp.redhat.com (Postfix) with ESMTP id B8BA5492BC6; Sat, 3 Feb 2024 15:43:58 +0000 (UTC) From: Waiman Long To: Tejun Heo , Lai Jiangshan Cc: linux-kernel@vger.kernel.org, Juri Lelli , Cestmir Kalina , Alex Gladkov , Phil Auld , Costa Shulyupin , Waiman Long Subject: [PATCH-wq v2 1/5] workqueue: Skip __WQ_DESTROYING workqueues when updating global unbound cpumask Date: Sat, 3 Feb 2024 10:43:30 -0500 Message-Id: <20240203154334.791910-2-longman@redhat.com> In-Reply-To: <20240203154334.791910-1-longman@redhat.com> References: <20240203154334.791910-1-longman@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.9 Content-Type: text/plain; charset="utf-8" Skip updating workqueues with __WQ_DESTROYING bit set when updating global unbound cpumask to avoid unnecessary work and other complications. Signed-off-by: Waiman Long Tested-by: Juri Lelli --- kernel/workqueue.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/kernel/workqueue.c b/kernel/workqueue.c index ffb625db9771..7ef393f4012e 100644 --- a/kernel/workqueue.c +++ b/kernel/workqueue.c @@ -6313,7 +6313,7 @@ static int workqueue_apply_unbound_cpumask(const cpum= ask_var_t unbound_cpumask) lockdep_assert_held(&wq_pool_mutex); =20 list_for_each_entry(wq, &workqueues, list) { - if (!(wq->flags & WQ_UNBOUND)) + if (!(wq->flags & WQ_UNBOUND) || (wq->flags & __WQ_DESTROYING)) continue; =20 /* creating multiple pwqs breaks ordering guarantee */ --=20 2.39.3 From nobody Sun Feb 8 17:36:47 2026 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B002A5F549 for ; Sat, 3 Feb 2024 15:44:04 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706975046; cv=none; b=tCWziBbzkfYvwIuTht4sJxbzDeD+Y6g0OERN8uHty7oq5T0y8iTo25xM3OBxpm9k0cMSAnuQJhzb6o210p97eFJGDRVZ2yBriDRmDppR1qtUkhucz4LfUGGtq4Y5btDkA4uQysjyqn2mYrDWkx9ITKhQhuAc66y1UJjfYuBThJc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706975046; c=relaxed/simple; bh=P0fDczSqjm+4VoEPMcjEIqr0fOERQ7HRV0xJuzSw7Sw=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=apeEqKu66IDlxeMuio3AsxscXrv+LZGsrQohiP5PfUpzFcOTLAg4JOcyVmcIB3iVj3KJ7whMtlrQx8bv8wgpSsrt+0hUE66CW0xlyNYGS91EYTqHZ+YK4tB0AD/9K3990lZ2V7Sa5UYN03aM7tTPHmQ0CD5KxfxV9setd2rvwtk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=TQUXn5fm; arc=none smtp.client-ip=170.10.133.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="TQUXn5fm" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1706975043; 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=xqI95iFKnrWkwe8wbz452OkEw/9Fkjl9mFJelsPsD1I=; b=TQUXn5fmTUy20KWzZKT077IM+kL4649wFqBTa1kFro1Ns0i7bAUyeuE+75x5lMHcMYBLFD rpkC2icXiFk6yF4/CePTZcJuKhWModAKQo2RgvUOFuzcbWA2f5H5GZa6um7LtB1NaeSEHG FOsWi23RKpuuddD4cg5Ci/QnI2s3cDE= Received: from mimecast-mx02.redhat.com (mx-ext.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-134-yHwX9ymmOlquW_3aGoaEYQ-1; Sat, 03 Feb 2024 10:44:00 -0500 X-MC-Unique: yHwX9ymmOlquW_3aGoaEYQ-1 Received: from smtp.corp.redhat.com (int-mx09.intmail.prod.int.rdu2.redhat.com [10.11.54.9]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id C2467383DBE0; Sat, 3 Feb 2024 15:43:59 +0000 (UTC) Received: from llong.com (unknown [10.22.32.36]) by smtp.corp.redhat.com (Postfix) with ESMTP id 496A6492BC6; Sat, 3 Feb 2024 15:43:59 +0000 (UTC) From: Waiman Long To: Tejun Heo , Lai Jiangshan Cc: linux-kernel@vger.kernel.org, Juri Lelli , Cestmir Kalina , Alex Gladkov , Phil Auld , Costa Shulyupin , Waiman Long Subject: [PATCH-wq v2 2/5] workqueue: Enable unbound cpumask update on ordered workqueues Date: Sat, 3 Feb 2024 10:43:31 -0500 Message-Id: <20240203154334.791910-3-longman@redhat.com> In-Reply-To: <20240203154334.791910-1-longman@redhat.com> References: <20240203154334.791910-1-longman@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.9 Content-Type: text/plain; charset="utf-8" Ordered workqueues does not currently follow changes made to the global unbound cpumask because per-pool workqueue changes may break the ordering guarantee. IOW, a work function in an ordered workqueue may run on an isolated CPU. This patch enables ordered workqueues to follow changes made to the global unbound cpumask by temporaily freeze the newly allocated pool_workqueue by using the new frozen flag to freeze execution of newly queued work items until the old pwq has been properly flushed. This enables ordered workqueues to follow the unbound cpumask changes like other unbound workqueues at the expense of some delay in execution of work functions during the transition period. Signed-off-by: Waiman Long Tested-by: Juri Lelli --- kernel/workqueue.c | 93 +++++++++++++++++++++++++++++++++++++++------- 1 file changed, 80 insertions(+), 13 deletions(-) diff --git a/kernel/workqueue.c b/kernel/workqueue.c index 7ef393f4012e..f089e532758a 100644 --- a/kernel/workqueue.c +++ b/kernel/workqueue.c @@ -242,6 +242,7 @@ struct pool_workqueue { int refcnt; /* L: reference count */ int nr_in_flight[WORK_NR_COLORS]; /* L: nr of in_flight works */ + int frozen; /* L: temporarily frozen */ =20 /* * nr_active management and WORK_STRUCT_INACTIVE: @@ -1667,6 +1668,9 @@ static bool pwq_tryinc_nr_active(struct pool_workqueu= e *pwq, bool fill) =20 lockdep_assert_held(&pool->lock); =20 + if (pwq->frozen) + return false; + if (!nna) { /* per-cpu workqueue, pwq->nr_active is sufficient */ obtained =3D pwq->nr_active < READ_ONCE(wq->max_active); @@ -1747,6 +1751,21 @@ static bool pwq_activate_first_inactive(struct pool_= workqueue *pwq, bool fill) } } =20 +/** + * thaw_pwq - thaw a frozen pool_workqueue + * @pwq: pool_workqueue to be thawed + */ +static void thaw_pwq(struct pool_workqueue *pwq) +{ + unsigned long flags; + + raw_spin_lock_irqsave(&pwq->pool->lock, flags); + pwq->frozen =3D false; + if (pwq_activate_first_inactive(pwq, true)) + kick_pool(pwq->pool); + raw_spin_unlock_irqrestore(&pwq->pool->lock, flags); +} + /** * node_activate_pending_pwq - Activate a pending pwq on a wq_node_nr_acti= ve * @nna: wq_node_nr_active to activate a pending pwq for @@ -4595,6 +4614,14 @@ static void pwq_release_workfn(struct kthread_work *= work) mutex_lock(&wq->mutex); list_del_rcu(&pwq->pwqs_node); is_last =3D list_empty(&wq->pwqs); + + /* + * For ordered workqueue with a frozen dfl_pwq, thaw it now. + */ + if (!is_last && (wq->flags & __WQ_ORDERED_EXPLICIT) && + wq->dfl_pwq->frozen) + thaw_pwq(wq->dfl_pwq); + mutex_unlock(&wq->mutex); } =20 @@ -4758,10 +4785,30 @@ static void apply_wqattrs_cleanup(struct apply_wqat= trs_ctx *ctx) { if (ctx) { int cpu; + bool refcheck =3D false; =20 for_each_possible_cpu(cpu) put_pwq_unlocked(ctx->pwq_tbl[cpu]); + + /* + * For ordered workqueue with a frozen dfl_pwq and a reference + * count of 1 in ctx->dfl_pwq, it is highly likely that the + * refcnt will become 0 after the final put_pwq(). Acquire + * wq->mutex to ensure that the pwq won't be freed by + * pwq_release_workfn() when we check pwq later. + */ + if ((ctx->wq->flags & __WQ_ORDERED_EXPLICIT) && + ctx->wq->dfl_pwq->frozen && + (ctx->dfl_pwq->refcnt =3D=3D 1)) { + mutex_lock(&ctx->wq->mutex); + refcheck =3D true; + } put_pwq_unlocked(ctx->dfl_pwq); + if (refcheck) { + if (!ctx->dfl_pwq->refcnt) + thaw_pwq(ctx->wq->dfl_pwq); + mutex_unlock(&ctx->wq->mutex); + } =20 free_workqueue_attrs(ctx->attrs); =20 @@ -4821,6 +4868,15 @@ apply_wqattrs_prepare(struct workqueue_struct *wq, cpumask_copy(new_attrs->__pod_cpumask, new_attrs->cpumask); ctx->attrs =3D new_attrs; =20 + /* + * For initialized ordered workqueues, there is only one pwq (dfl_pwq). + * Temporarily the frozen flag of ctx->dfl_pwq to freeze the execution + * of newly queued work items until execution of older work items in + * the old pwq has completed. + */ + if (!list_empty(&wq->pwqs) && (wq->flags & __WQ_ORDERED_EXPLICIT)) + ctx->dfl_pwq->frozen =3D true; + ctx->wq =3D wq; return ctx; =20 @@ -4861,13 +4917,8 @@ static int apply_workqueue_attrs_locked(struct workq= ueue_struct *wq, if (WARN_ON(!(wq->flags & WQ_UNBOUND))) return -EINVAL; =20 - /* creating multiple pwqs breaks ordering guarantee */ - if (!list_empty(&wq->pwqs)) { - if (WARN_ON(wq->flags & __WQ_ORDERED_EXPLICIT)) - return -EINVAL; - + if (!list_empty(&wq->pwqs) && !(wq->flags & __WQ_ORDERED_EXPLICIT)) wq->flags &=3D ~__WQ_ORDERED; - } =20 ctx =3D apply_wqattrs_prepare(wq, attrs, wq_unbound_cpumask); if (IS_ERR(ctx)) @@ -6316,11 +6367,28 @@ static int workqueue_apply_unbound_cpumask(const cp= umask_var_t unbound_cpumask) if (!(wq->flags & WQ_UNBOUND) || (wq->flags & __WQ_DESTROYING)) continue; =20 - /* creating multiple pwqs breaks ordering guarantee */ + /* + * We does not support changing cpumask of an ordered workqueue + * again before the previous cpumask change is completed. + * Sleep up to 100ms in 10ms interval to allow previous + * operation to complete and skip it if not done by then. + */ if (!list_empty(&wq->pwqs)) { - if (wq->flags & __WQ_ORDERED_EXPLICIT) - continue; - wq->flags &=3D ~__WQ_ORDERED; + struct pool_workqueue *pwq =3D wq->dfl_pwq; + + if (!(wq->flags & __WQ_ORDERED_EXPLICIT)) { + wq->flags &=3D ~__WQ_ORDERED; + } else if (pwq && pwq->frozen) { + int i; + + for (i =3D 0; i < 10; i++) { + msleep(10); + if (!pwq->frozen) + break; + } + if (WARN_ON_ONCE(pwq->frozen)) + continue; + } } =20 ctx =3D apply_wqattrs_prepare(wq, wq->unbound_attrs, unbound_cpumask); @@ -6836,9 +6904,8 @@ int workqueue_sysfs_register(struct workqueue_struct = *wq) int ret; =20 /* - * Adjusting max_active or creating new pwqs by applying - * attributes breaks ordering guarantee. Disallow exposing ordered - * workqueues. + * Adjusting max_active breaks ordering guarantee. Disallow exposing + * ordered workqueues. */ if (WARN_ON(wq->flags & __WQ_ORDERED_EXPLICIT)) return -EINVAL; --=20 2.39.3 From nobody Sun Feb 8 17:36:47 2026 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 6D7E85F579 for ; Sat, 3 Feb 2024 15:44:06 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706975048; cv=none; b=HJGCmgRph4acVTXAwgtwYsvYVTiZLofZmg+iQTC0sCVCtDTlFOL7dfPTssuRz0HJYgcrvYL3VF8zth7PXlXRYUB50XY0YnZpvUrc6kaorUW5UDUL1/0EhN9CGMXrOsiQcPW61f8EUf9ivM2w3WJUujUHjmePeVsk6VOgXTAynfA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706975048; c=relaxed/simple; bh=1jnXP20A8FTVhBfK5XMoOFBXtkTqsoRl3cBJhPND+Tg=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=bR/2f25UTt7qPHKoL/J+aq2Wx/rQhbrjtbXmvELpV2tBLE4F24V4hgynW8IeoapKS41ss1TVujBP+RJsmj5lRD7VBD6WeRQ/pdVW58iA8qPGLKmWhmRxqFTN9R0m5xk7b8kuaW5xel2NoKHrG6qU9cyG3TQNih8thhwKd1KIOpE= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=aBhH4ipD; arc=none smtp.client-ip=170.10.129.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="aBhH4ipD" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1706975045; 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=z37S38Ac+FZG32K3ttyGhIlDZkrUn25fgqCMfEdj2WY=; b=aBhH4ipDEgoo8UXOSW7WmbWhPq9zVUBr1RB1JReM0I2qHV25bosFShNDkM++oHn+LNqiLO b3egrFqb1OMWR4oeWq18EgYNa42rGuuURj5vddWafBBPgCEwIU8smI9Y7AA7AKF88HdkyK X758m2z5hPhBi+/wM5dmaGOf4vjkvBc= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-571-KRPLBrgKMu6TrTKsDj8DEA-1; Sat, 03 Feb 2024 10:44:00 -0500 X-MC-Unique: KRPLBrgKMu6TrTKsDj8DEA-1 Received: from smtp.corp.redhat.com (int-mx09.intmail.prod.int.rdu2.redhat.com [10.11.54.9]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 537488432A0; Sat, 3 Feb 2024 15:44:00 +0000 (UTC) Received: from llong.com (unknown [10.22.32.36]) by smtp.corp.redhat.com (Postfix) with ESMTP id D413D492BC6; Sat, 3 Feb 2024 15:43:59 +0000 (UTC) From: Waiman Long To: Tejun Heo , Lai Jiangshan Cc: linux-kernel@vger.kernel.org, Juri Lelli , Cestmir Kalina , Alex Gladkov , Phil Auld , Costa Shulyupin , Waiman Long Subject: [PATCH-wq v2 3/5] workqueue: Thaw frozen pwq in workqueue_apply_unbound_cpumask() Date: Sat, 3 Feb 2024 10:43:32 -0500 Message-Id: <20240203154334.791910-4-longman@redhat.com> In-Reply-To: <20240203154334.791910-1-longman@redhat.com> References: <20240203154334.791910-1-longman@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.9 Content-Type: text/plain; charset="utf-8" workqueue_apply_unbound_cpumask() cannot proceed with an ordered workqueue if its dfl_pwq is still frozen. Just do a sleep wait for it to be thawed may not work in some cases if pwq_release_workfn() is somehow prevented from being called due to resources (e.g. wq_pool_mutex) that are held by its caller. To break the logjam, we have to actively check if the frozen dfl_pwq is ready to be thawed and call thaw_pwq() directly if so. Signed-off-by: Waiman Long Tested-by: Juri Lelli --- kernel/workqueue.c | 30 ++++++++++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/kernel/workqueue.c b/kernel/workqueue.c index f089e532758a..ee934c2c6ea8 100644 --- a/kernel/workqueue.c +++ b/kernel/workqueue.c @@ -6354,6 +6354,32 @@ void thaw_workqueues(void) } #endif /* CONFIG_FREEZER */ =20 +/* + * Check the given ordered workqueue to see if its non-default pwq's have + * zero reference count and if so thaw the frozen default pwq. + * + * Return: + * %true if dfl_pwq has been thawed or %false otherwise. + */ +static bool ordered_workqueue_ref_check(struct workqueue_struct *wq) +{ + int refs =3D 0; + struct pool_workqueue *pwq; + + if (!READ_ONCE(wq->dfl_pwq->frozen)) + return true; + mutex_lock(&wq->mutex); + for_each_pwq(pwq, wq) { + if (pwq =3D=3D wq->dfl_pwq) + continue; + refs +=3D pwq->refcnt; + } + if (!refs) + thaw_pwq(wq->dfl_pwq); + mutex_unlock(&wq->mutex); + return !refs; +} + static int workqueue_apply_unbound_cpumask(const cpumask_var_t unbound_cpu= mask) { LIST_HEAD(ctxs); @@ -6378,12 +6404,12 @@ static int workqueue_apply_unbound_cpumask(const cp= umask_var_t unbound_cpumask) =20 if (!(wq->flags & __WQ_ORDERED_EXPLICIT)) { wq->flags &=3D ~__WQ_ORDERED; - } else if (pwq && pwq->frozen) { + } else if (pwq && !ordered_workqueue_ref_check(wq)) { int i; =20 for (i =3D 0; i < 10; i++) { msleep(10); - if (!pwq->frozen) + if (ordered_workqueue_ref_check(wq)) break; } if (WARN_ON_ONCE(pwq->frozen)) --=20 2.39.3 From nobody Sun Feb 8 17:36:47 2026 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 6D7BD5F55E for ; Sat, 3 Feb 2024 15:44:06 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706975048; cv=none; b=dIWxxy10rmkNKE6BnZhuXTsoaXcrK5IXvfe3YHkD1PLH2DnJl3vxoLLjUBF0V2KRSDC7VOVBU99UpFcK8qGpukEPlGfm4pZzPgqqsMybot9UpESJpm8IKuChDNaU1W8lDGybBNqti4rM+e79Nmddl/EfxPjlJmxwdSJShp9Ffmk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706975048; c=relaxed/simple; bh=h5QmlYtfNPJkR43xmQK8MmClp8+0FgNrfjWDooIF2m0=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=hI6Z0gZLxAieT32sIs6vOW6QMeYZKBAf/2/822iMShMCtTuvv1sJTNaC8PwTOkPL9S0YYbbIZBLaO83wmglGgzEQb0DJbW295MJHL6Icq6gVLaA4IyDINRcLunF38W93jlDCbhuilykEAxKnRHnP1KufvBaQQGnuXWs7jzITRG4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=WjzxMAgk; arc=none smtp.client-ip=170.10.129.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="WjzxMAgk" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1706975045; 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=X2395hdNLA+e2lfzBFwx2gFoDSFHPhjOE9LGsAepU2M=; b=WjzxMAgkj36pZ35fZ6xBRi93t2HvO53jHTZVM6VOjM+EAtUwjRCKhiyB6IVNpn3pIpgZ5w pQs1pVx8nJoU3rfC/fwWD8U4q6DifCDJ//hrVme7ZEeT3ps4KWTcBpnsmrI4WQd6Auevly 6b+aSadqgxXqOcnIAzwT5t3ka5bvgBo= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-573-LSgab-vdNNS5dJajugvDTA-1; Sat, 03 Feb 2024 10:44:01 -0500 X-MC-Unique: LSgab-vdNNS5dJajugvDTA-1 Received: from smtp.corp.redhat.com (int-mx09.intmail.prod.int.rdu2.redhat.com [10.11.54.9]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id DC44E10651AF; Sat, 3 Feb 2024 15:44:00 +0000 (UTC) Received: from llong.com (unknown [10.22.32.36]) by smtp.corp.redhat.com (Postfix) with ESMTP id 64ECB492BC6; Sat, 3 Feb 2024 15:44:00 +0000 (UTC) From: Waiman Long To: Tejun Heo , Lai Jiangshan Cc: linux-kernel@vger.kernel.org, Juri Lelli , Cestmir Kalina , Alex Gladkov , Phil Auld , Costa Shulyupin , Waiman Long Subject: [PATCH-wq v2 4/5] kernel/workqueue: Let rescuers follow unbound wq cpumask changes Date: Sat, 3 Feb 2024 10:43:33 -0500 Message-Id: <20240203154334.791910-5-longman@redhat.com> In-Reply-To: <20240203154334.791910-1-longman@redhat.com> References: <20240203154334.791910-1-longman@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.9 Content-Type: text/plain; charset="utf-8" From: Juri Lelli When workqueue cpumask changes are committed the associated rescuer (if one exists) affinity is not touched and this might be a problem down the line for isolated setups. Make sure rescuers affinity is updated every time a workqueue cpumask changes, so that rescuers can't break isolation. [longman: set_cpus_allowed_ptr() will block until the designated task is enqueued on an allowed CPU, no wake_up_process() needed. Also use the unbound_effective_cpumask() helper as suggested by Tejun.] Signed-off-by: Juri Lelli Signed-off-by: Waiman Long Tested-by: Juri Lelli --- kernel/workqueue.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/kernel/workqueue.c b/kernel/workqueue.c index ee934c2c6ea8..172f7299aa71 100644 --- a/kernel/workqueue.c +++ b/kernel/workqueue.c @@ -4905,6 +4905,11 @@ static void apply_wqattrs_commit(struct apply_wqattr= s_ctx *ctx) /* update node_nr_active->max */ wq_update_node_max_active(ctx->wq, -1); =20 + /* rescuer needs to respect wq cpumask changes */ + if (ctx->wq->rescuer) + set_cpus_allowed_ptr(ctx->wq->rescuer->task, + unbound_effective_cpumask(ctx->wq)); + mutex_unlock(&ctx->wq->mutex); } =20 --=20 2.39.3 From nobody Sun Feb 8 17:36:47 2026 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C8D4B5F844 for ; Sat, 3 Feb 2024 15:44:06 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706975048; cv=none; b=Ai7Tqx5SSkJ2WN03IoaYN9XSzLpk3658wE3WYNyxXjxh064O/OExTS+04IZQpBSBPOVwyhEYEDS4e+2HIVNTVuIITMcD0HtKHMj52S6ysDddI9hifg2CI8+U/H5D+kzKYDuM/Tx1H1iX3QE1mMr0/oSPlrgLKC+5ybjbBKIaxFg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706975048; c=relaxed/simple; bh=i6TPdcvT1TQK+CKu+xNdsdflEhMuYXP1v6ppPXqzfUY=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=NCoSI1LTkJ9Ij7HiX8XBlnghvFl47onquJbOSgcDmNh6DoN4/MwINVE9C7s/+pp2ysx2P6x+X2vY6uhg2UE9rrUUsKyJHBDpR5cS9OJDPwXFknea3igj+ku67sTN6K3YXPIFxWmqWaFUrsiWYpuu0pPEAGhPHsuzCLlDE+kB7N8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=X7LUy/uz; arc=none smtp.client-ip=170.10.129.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="X7LUy/uz" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1706975045; 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=ZE5SuTiC1eU6caXAnwVyMAj7DO/jK06d00aD2gGGC9Y=; b=X7LUy/uzVH5EXH2n/t6My/QPxHBhhzeFjPesyb1BeDWyZYucG8QvuZbXhkanu9NxnO2ITA R6Js1uhp6Cw+e8iw4hTp3SDnZ+cexYHPcDXJvsi7bjUcqGwJeK9AjvX9FmIgwGb6J/obev VTJRJg1DBcXNS5Gmk0goxFKqbe0UuKw= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-583-kpxT-kJSPYyD4ihjCqYHlA-1; Sat, 03 Feb 2024 10:44:01 -0500 X-MC-Unique: kpxT-kJSPYyD4ihjCqYHlA-1 Received: from smtp.corp.redhat.com (int-mx09.intmail.prod.int.rdu2.redhat.com [10.11.54.9]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 69864185A781; Sat, 3 Feb 2024 15:44:01 +0000 (UTC) Received: from llong.com (unknown [10.22.32.36]) by smtp.corp.redhat.com (Postfix) with ESMTP id EB934492BC6; Sat, 3 Feb 2024 15:44:00 +0000 (UTC) From: Waiman Long To: Tejun Heo , Lai Jiangshan Cc: linux-kernel@vger.kernel.org, Juri Lelli , Cestmir Kalina , Alex Gladkov , Phil Auld , Costa Shulyupin , Waiman Long Subject: [PATCH-wq v2 5/5] workqueue: Bind unbound workqueue rescuer to wq_unbound_cpumask Date: Sat, 3 Feb 2024 10:43:34 -0500 Message-Id: <20240203154334.791910-6-longman@redhat.com> In-Reply-To: <20240203154334.791910-1-longman@redhat.com> References: <20240203154334.791910-1-longman@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.9 Content-Type: text/plain; charset="utf-8" Commit 85f0ab43f9de ("kernel/workqueue: Bind rescuer to unbound cpumask for WQ_UNBOUND") modified init_rescuer() to bind rescuer of an unbound workqueue to the cpumask in wq->unbound_attrs. However unbound_attrs->cpumask's of all workqueues are initialized to cpu_possible_mask and will only be changed if it has the WQ_SYSFS flag to expose a cpumask sysfs file to be written by users. So this patch doesn't achieve what it is intended to do. If an unbound workqueue is created after wq_unbound_cpumask is modified and there is no more unbound cpumask update after that, the unbound rescuer will be bound to all CPUs unless the workqueue is created with the WQ_SYSFS flag and an user explicitly modified its cpumask sysfs file. Fix this problem by binding directly to wq_unbound_cpumask in init_rescuer(). Fixes: 85f0ab43f9de ("kernel/workqueue: Bind rescuer to unbound cpumask for= WQ_UNBOUND") Signed-off-by: Waiman Long Tested-by: Juri Lelli --- kernel/workqueue.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/kernel/workqueue.c b/kernel/workqueue.c index 172f7299aa71..1e022899c13c 100644 --- a/kernel/workqueue.c +++ b/kernel/workqueue.c @@ -5149,7 +5149,7 @@ static int init_rescuer(struct workqueue_struct *wq) =20 wq->rescuer =3D rescuer; if (wq->flags & WQ_UNBOUND) - kthread_bind_mask(rescuer->task, wq->unbound_attrs->cpumask); + kthread_bind_mask(rescuer->task, wq_unbound_cpumask); else kthread_bind_mask(rescuer->task, cpu_possible_mask); wake_up_process(rescuer->task); --=20 2.39.3