From nobody Sun Apr 12 02:48:18 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 E0592C00140 for ; Tue, 2 Aug 2022 08:42:32 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236440AbiHBIma (ORCPT ); Tue, 2 Aug 2022 04:42:30 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43586 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231859AbiHBImZ (ORCPT ); Tue, 2 Aug 2022 04:42:25 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id CAB7C1BEB0 for ; Tue, 2 Aug 2022 01:42:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1659429744; 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=4gzLDvQ7aWaj23CMbC0REd/onn9CKJJFu3RV6mIazrs=; b=Xp1UPP5ii4iyXaAydigSfRar46EdUQPO7gmNfsjGS+rfXTQAVXIj7cjBdvxIrwkhg/Ilxc j1UFdoXDG9HLU0N0BPz7uk423JkcJZOziBaMdbt5Z7Fc6c+gl61w0VCTq6aS54cAGlT4Il 9uuuQMXnnNLlwIevTRhGrdHkTIBdc5g= Received: from mail-wr1-f72.google.com (mail-wr1-f72.google.com [209.85.221.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-457-J1vX1i_rNjGZGR7kmqvMsQ-1; Tue, 02 Aug 2022 04:42:23 -0400 X-MC-Unique: J1vX1i_rNjGZGR7kmqvMsQ-1 Received: by mail-wr1-f72.google.com with SMTP id s24-20020adf9798000000b0021ed3f3dd75so3254033wrb.15 for ; Tue, 02 Aug 2022 01:42:22 -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; bh=4gzLDvQ7aWaj23CMbC0REd/onn9CKJJFu3RV6mIazrs=; b=eUbS/yaAgNCSbC1+YVLDR7BQrvxhQ++ZZn4dk4O17z1ZtA2c5c9szIGWnMRDJ4XhbJ qUKMc1EuchElX5IvP9YLNp5Qcev8bW0i5Ub8ywL3jHPRGqOWojM+2rnmWb4sdk67q3DU 7VIDashNHo6X0rv7s+q5UHPvRrnfUw6TpMkgddkSTnW1+jHjAgKLAUBLexF+yXP9q+jC mBwErAFXHQ6VhsT5+bHR4tT9eOp8gjku8TUVZ2YBBvuQQs84qEdvmyIf0JGAOMY/ZQqJ r2WljJAU0sbtn6CdbXOo5RB92VZzWPg97NpVSSm/kMF+IkDHl0QPyeIY1Xk1uvI2Zz5w l1MA== X-Gm-Message-State: AJIora88xsILnoRFGxinErcMdIYc5yCj1yC/C7daqbU1dle7k9EPxsMl 63DhFdiqTtkowQa8ChUkLjbbpvoZyYQlx4wz4EnNMI1ka2xEtHxtSokt3o5GODpDDO6P8ZAMJqk SeLGtm3uo9qOpuX5B20DQoIOlG2L7v1817THXiMBCZZ2rzJFOQ7BLHcFnN7cRoSvPPrzuVbXDax Bj X-Received: by 2002:a05:600c:a146:b0:3a3:1e79:4446 with SMTP id ib6-20020a05600ca14600b003a31e794446mr13768018wmb.158.1659429741610; Tue, 02 Aug 2022 01:42:21 -0700 (PDT) X-Google-Smtp-Source: AGRyM1te439EFrpVNkzNpR1rWgVUPi8JRsLzt6WMfD4BOYnGxBzLvyGLBPnePuCREG7Vbdo+SsE81w== X-Received: by 2002:a05:600c:a146:b0:3a3:1e79:4446 with SMTP id ib6-20020a05600ca14600b003a31e794446mr13768000wmb.158.1659429741412; Tue, 02 Aug 2022 01:42:21 -0700 (PDT) Received: from vschneid.remote.csb ([185.11.37.247]) by smtp.gmail.com with ESMTPSA id f12-20020a05600c154c00b003a31169a7f4sm24825800wmg.12.2022.08.02.01.42.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 02 Aug 2022 01:42:20 -0700 (PDT) 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: [RFC PATCH v3 1/3] workqueue: Hold wq_pool_mutex while affining tasks to wq_unbound_cpumask Date: Tue, 2 Aug 2022 09:41:44 +0100 Message-Id: <20220802084146.3922640-2-vschneid@redhat.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20220802084146.3922640-1-vschneid@redhat.com> References: <20220802084146.3922640-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" 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. This is made more obvious as of recent commit 46a4d679ef88 ("workqueue: Avoid a false warning in unbind_workers()") e.g. unbind_workers() workqueue_set_unbound_cpumask() kthread_set_per_cpu(p, -1); if (cpumask_intersects(wq_unbound_cpumask, cpu_active_mask)) cpumask_copy(wq_unbound_cpumask, cpumask); WARN_ON_ONCE(set_cpus_allowed_ptr(worker->task, wq_unbound_cpumask) < 0= ); Make workqueue_offline_cpu() invoke unbind_workers() with wq_pool_mutex held. Fixes: 10a5a651e3af ("workqueue: Restrict kworker in the offline CPU pool r= unning on housekeeping CPUs") Signed-off-by: Valentin Schneider Reported-by: Valentin Schneider --- kernel/workqueue.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/kernel/workqueue.c b/kernel/workqueue.c index aa8a82bc6738..97cc41430a76 100644 --- a/kernel/workqueue.c +++ b/kernel/workqueue.c @@ -5143,14 +5143,15 @@ int workqueue_offline_cpu(unsigned int cpu) if (WARN_ON(cpu !=3D smp_processor_id())) return -1; =20 + mutex_lock(&wq_pool_mutex); + unbind_workers(cpu); =20 /* update NUMA affinity of unbound workqueues */ - mutex_lock(&wq_pool_mutex); list_for_each_entry(wq, &workqueues, list) wq_update_unbound_numa(wq, cpu, false); - mutex_unlock(&wq_pool_mutex); =20 + mutex_unlock(&wq_pool_mutex); return 0; } =20 --=20 2.31.1 From nobody Sun Apr 12 02:48:18 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 095ADC00140 for ; Tue, 2 Aug 2022 08:42:37 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235493AbiHBImf (ORCPT ); Tue, 2 Aug 2022 04:42:35 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43644 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235746AbiHBIm1 (ORCPT ); Tue, 2 Aug 2022 04:42:27 -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 ESMTP id 806611834B for ; Tue, 2 Aug 2022 01:42:26 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1659429745; 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=aT7p3QkgEShXnfExV1EoIhnpwMw5uLCIByiCReTweps=; b=Uz0Gu+Nu8OyIyPKqfoE4sagP+HfkdWMtN9HUeK9azieXAbAJy/1Hmy0OGj3Cs/J8aGZRtj RW3eEUa1c/XnN9zKo6gAj4bNPEKEwDx1q6Y4TtoaOBis2wTgTCwHI02YgqRomnp9HYM0F5 vFuAa6h72ttcj9eH/xgge2C2takEH1E= Received: from mail-wr1-f71.google.com (mail-wr1-f71.google.com [209.85.221.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-656-CedOCPNKMZK9aCdQ0qLD7Q-1; Tue, 02 Aug 2022 04:42:24 -0400 X-MC-Unique: CedOCPNKMZK9aCdQ0qLD7Q-1 Received: by mail-wr1-f71.google.com with SMTP id m7-20020adfa3c7000000b0021ef71807e3so3256596wrb.9 for ; Tue, 02 Aug 2022 01:42:24 -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; bh=aT7p3QkgEShXnfExV1EoIhnpwMw5uLCIByiCReTweps=; b=nSn/vYXF/tl5Dp7WPKGqGuq3h8ZA11IaWfdGW95iBca/f7dDQDQuoLD9wV14hG6SYe YMcxg+IXWfr5V+x6UOxY4PyuV4c4G8mqA4e/1MYfFjcwGYAhMV8MR15VQszte3Oew9tE Z8GJLbIHG1jn4Dk2QdCiuVsPBUkFGhEnEhwwN4PXMPqej7jUI1OAHBjhuiLVezm3sOuR q8PHKwY6Fnb3XXwV7sXMbifSK6AyCVD9FpQUp8TnPvqk1e/MK7OJBB5NJgwajNsEh3vW K31a0x1vQgbuRmSp3lvdivtPQ4jjYgHWFdlfvthMZ1f46oCHGlBv34cNdP9Dzt4aM6Y+ rapQ== X-Gm-Message-State: ACgBeo1bl8iSMSz5xHJ++exGStKJNbrhtCTPC4yArMUJeUb2CO9uKP1Z ifD+0I5D59xpdxPoZniRw8OHJYJnw9rRoxaSS7ZeJELxk+4nJ8LEcsTBC40g/rPhug6O0iEW9w+ RaBUg0yGzgwgm8RIUfIpq8OcXVgsHGP71NvtiGLfhekqbXdgRRhhQ74x/Fn7F7/ngHGPoyxitg4 FK X-Received: by 2002:a5d:64ac:0:b0:21d:7832:ecf9 with SMTP id m12-20020a5d64ac000000b0021d7832ecf9mr12430345wrp.86.1659429743139; Tue, 02 Aug 2022 01:42:23 -0700 (PDT) X-Google-Smtp-Source: AA6agR74cK2el17Wbd1YXJXEm2xi29pldGRwzrQiQ90NPJxxuSk4yZ7/hsHX7hk76lXVIbbV8g2m9g== X-Received: by 2002:a5d:64ac:0:b0:21d:7832:ecf9 with SMTP id m12-20020a5d64ac000000b0021d7832ecf9mr12430312wrp.86.1659429742675; Tue, 02 Aug 2022 01:42:22 -0700 (PDT) Received: from vschneid.remote.csb ([185.11.37.247]) by smtp.gmail.com with ESMTPSA id f12-20020a05600c154c00b003a31169a7f4sm24825800wmg.12.2022.08.02.01.42.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 02 Aug 2022 01:42:22 -0700 (PDT) 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: [RFC PATCH v3 2/3] workqueue: Unbind workers before sending them to exit() Date: Tue, 2 Aug 2022 09:41:45 +0100 Message-Id: <20220802084146.3922640-3-vschneid@redhat.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20220802084146.3922640-1-vschneid@redhat.com> References: <20220802084146.3922640-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" 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, so get rid of the pool->idle_timer and use a delayed_work instead. Ensure kworkers do not free their resources before the new kworker reaper has handled them by introducing a new struct worker.reaper field - this new field fills in a 4 byte hole in the second cacheline of struct worker. Signed-off-by: Valentin Schneider --- kernel/workqueue.c | 155 +++++++++++++++++++++++++++++------- kernel/workqueue_internal.h | 1 + 2 files changed, 126 insertions(+), 30 deletions(-) diff --git a/kernel/workqueue.c b/kernel/workqueue.c index 97cc41430a76..28cd58c684ee 100644 --- a/kernel/workqueue.c +++ b/kernel/workqueue.c @@ -167,9 +167,9 @@ struct worker_pool { int nr_workers; /* L: total number of workers */ int nr_idle; /* L: currently idle workers */ =20 - 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_list; /* L: list of idle workers */ + struct delayed_work idle_reaper_work; /* L: worker idle timeout */ + struct timer_list mayday_timer; /* L: SOS timer for workers */ =20 /* a workers is either on busy_hash or idle_list, or the manager */ DECLARE_HASHTABLE(busy_hash, BUSY_WORKER_HASH_ORDER); @@ -1806,8 +1806,10 @@ static void worker_enter_idle(struct worker *worker) /* idle_list is LIFO */ list_add(&worker->entry, &pool->idle_list); =20 - if (too_many_workers(pool) && !timer_pending(&pool->idle_timer)) - mod_timer(&pool->idle_timer, jiffies + IDLE_WORKER_TIMEOUT); + if (too_many_workers(pool) && !delayed_work_pending(&pool->idle_reaper_wo= rk)) + mod_delayed_work(system_unbound_wq, + &pool->idle_reaper_work, + IDLE_WORKER_TIMEOUT); =20 /* Sanity check nr_running. */ WARN_ON_ONCE(pool->nr_workers =3D=3D pool->nr_idle && pool->nr_running); @@ -1972,9 +1974,72 @@ static struct worker *create_worker(struct worker_po= ol *pool) return NULL; } =20 +static void unbind_worker(struct worker *worker) +{ + lockdep_assert_held(&wq_pool_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= ); +} + +static void reap_workers(struct list_head *reaplist) +{ + struct worker *worker, *tmp; + + list_for_each_entry_safe(worker, tmp, reaplist, 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 destroy_worker() 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. + */ + WRITE_ONCE(worker->reaped, true); + wake_up_process(worker->task); + } +} + +/* + * Unlikely as it may be, a worker could wake after destroy_worker() has + * happened but before reap_workers(). WORKER_DIE would be set in worker->= flags, + * so it would be able to kfree(worker) and head out to do_exit(). + * + * Rather than make the reaper wait for each to-be-reaped kworker to exit = and + * kfree(worker) itself, make the kworkers (which have nothing to do but go + * do_exit() anyway) wait for the reaper to be done with them. + */ +static void worker_wait_reaped(struct worker *worker) +{ + WARN_ON_ONCE(current !=3D worker->task); + + for (;;) { + set_current_state(TASK_INTERRUPTIBLE); + if (READ_ONCE(worker->reaped)) + break; + schedule(); + } + __set_current_state(TASK_RUNNING); +} + /** * destroy_worker - destroy a workqueue worker * @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. @@ -1982,7 +2047,7 @@ static struct worker *create_worker(struct worker_poo= l *pool) * CONTEXT: * raw_spin_lock_irq(pool->lock). */ -static void destroy_worker(struct worker *worker) +static void destroy_worker(struct worker *worker, struct list_head *list) { struct worker_pool *pool =3D worker->pool; =20 @@ -1997,34 +2062,64 @@ static void destroy_worker(struct worker *worker) pool->nr_workers--; pool->nr_idle--; =20 - list_del_init(&worker->entry); worker->flags |=3D WORKER_DIE; - wake_up_process(worker->task); + + list_move(&worker->entry, list); } =20 -static void idle_worker_timeout(struct timer_list *t) +/** + * idle_reaper_fn - reap workers that have been idle for too long. + * + * Unbinding marked-for-destruction workers requires a sleepable context, = as + * changing a task's affinity is not an atomic operation, and we don't want + * to disturb isolated CPUs IDLE_WORKER_TIMEOUT in the future just for a k= worker + * to do_exit(). + * + * Percpu kworkers should meet the conditions for the affinity change to n= ot + * block (not migration-disabled and not running), but there is no *hard* + * guarantee that they are not running when we get here. + * + * The delayed_work is only ever modified under raw_spin_lock_irq(pool->lo= ck). + */ +static void idle_reaper_fn(struct work_struct *work) { - struct worker_pool *pool =3D from_timer(pool, t, idle_timer); + struct delayed_work *dwork =3D to_delayed_work(work); + struct worker_pool *pool =3D container_of(dwork, struct worker_pool, idle= _reaper_work); + struct list_head reaplist; + struct worker *worker; + + INIT_LIST_HEAD(&reaplist); =20 raw_spin_lock_irq(&pool->lock); =20 while (too_many_workers(pool)) { - struct worker *worker; unsigned long expires; + unsigned long now =3D jiffies; =20 /* idle_list is kept in LIFO order, check the last one */ worker =3D list_entry(pool->idle_list.prev, struct worker, entry); expires =3D worker->last_active + IDLE_WORKER_TIMEOUT; =20 - if (time_before(jiffies, expires)) { - mod_timer(&pool->idle_timer, expires); + /* + * Careful: queueing a work item from here can and will cause a + * self-deadlock when dealing with an unbound pool. However, + * here the delay *cannot* be zero and *has* to be in the + * future, which works. + */ + if (time_before(now, expires)) { + mod_delayed_work(system_unbound_wq, + &pool->idle_reaper_work, + expires - now); break; } =20 - destroy_worker(worker); + destroy_worker(worker, &reaplist); } - raw_spin_unlock_irq(&pool->lock); + + mutex_lock(&wq_pool_mutex); + reap_workers(&reaplist); + mutex_unlock(&wq_pool_mutex); } =20 static void send_mayday(struct work_struct *work) @@ -2388,6 +2483,9 @@ static int worker_thread(void *__worker) /* am I supposed to die? */ if (unlikely(worker->flags & WORKER_DIE)) { raw_spin_unlock_irq(&pool->lock); + + worker_wait_reaped(worker); + WARN_ON_ONCE(!list_empty(&worker->entry)); set_pf_worker(false); =20 @@ -3454,7 +3552,7 @@ static int init_worker_pool(struct worker_pool *pool) INIT_LIST_HEAD(&pool->idle_list); hash_init(pool->busy_hash); =20 - timer_setup(&pool->idle_timer, idle_worker_timeout, TIMER_DEFERRABLE); + INIT_DEFERRABLE_WORK(&pool->idle_reaper_work, idle_reaper_fn); =20 timer_setup(&pool->mayday_timer, pool_mayday_timeout, 0); =20 @@ -3559,8 +3657,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 reaplist; struct worker *worker; =20 + INIT_LIST_HEAD(&reaplist); + lockdep_assert_held(&wq_pool_mutex); =20 if (--pool->refcnt) @@ -3588,10 +3689,12 @@ static void put_unbound_pool(struct worker_pool *po= ol) pool->flags |=3D POOL_MANAGER_ACTIVE; =20 while ((worker =3D first_idle_worker(pool))) - destroy_worker(worker); + destroy_worker(worker, &reaplist); WARN_ON(pool->nr_workers || pool->nr_idle); raw_spin_unlock_irq(&pool->lock); =20 + reap_workers(&reaplist); + mutex_lock(&wq_pool_attach_mutex); if (!list_empty(&pool->workers)) pool->detach_completion =3D &detach_completion; @@ -3601,7 +3704,7 @@ static void put_unbound_pool(struct worker_pool *pool) wait_for_completion(pool->detach_completion); =20 /* shut down the timers */ - del_timer_sync(&pool->idle_timer); + cancel_delayed_work_sync(&pool->idle_reaper_work); del_timer_sync(&pool->mayday_timer); =20 /* RCU protected to allow dereferences from get_work_pool() */ @@ -4999,13 +5102,8 @@ static void unbind_workers(int cpu) =20 raw_spin_unlock_irq(&pool->lock); =20 - 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); =20 mutex_unlock(&wq_pool_attach_mutex); } @@ -5030,11 +5128,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); =20 raw_spin_lock_irq(&pool->lock); =20 diff --git a/kernel/workqueue_internal.h b/kernel/workqueue_internal.h index e00b1204a8e9..a3d60e10a76f 100644 --- a/kernel/workqueue_internal.h +++ b/kernel/workqueue_internal.h @@ -46,6 +46,7 @@ struct worker { unsigned int flags; /* X: flags */ int id; /* I: worker id */ int sleeping; /* None */ + int reaped; /* None */ =20 /* * Opaque string set with work_set_desc(). Printed out with task --=20 2.31.1 From nobody Sun Apr 12 02:48:18 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 6E5C6C00140 for ; Tue, 2 Aug 2022 08:42:40 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236449AbiHBImh (ORCPT ); Tue, 2 Aug 2022 04:42:37 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43666 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235891AbiHBIm2 (ORCPT ); Tue, 2 Aug 2022 04:42:28 -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 ESMTP id 9A49D1ADB1 for ; Tue, 2 Aug 2022 01:42:27 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1659429746; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=H8eF92se8Ck9gTMF8XB0hpV/HpagXM8oD2FIuevox34=; b=eFACHF/gzM0gdqegtcbzuwxtKECzM9ogTwSrZrZJ5RIMKvCePgSCcS+AhF9089SrVEMEi/ 5xXYsQR1zchJqt7C0FgOssK/EqLx7N9BZ9puXiFeozhrDwnfzDok05nfkjnaX7UpV4dI3y N3raYVH2bby9COdSDjl4IIhqeHpasJY= 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.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-136-UbyiuJ87MWewiWR5h0-WHQ-1; Tue, 02 Aug 2022 04:42:25 -0400 X-MC-Unique: UbyiuJ87MWewiWR5h0-WHQ-1 Received: by mail-wm1-f70.google.com with SMTP id n19-20020a05600c3b9300b003a314062cf4so461698wms.0 for ; Tue, 02 Aug 2022 01:42:25 -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; bh=H8eF92se8Ck9gTMF8XB0hpV/HpagXM8oD2FIuevox34=; b=erYpPyPXEf+f1ZZ5HuhN7AvTfKsrWFMC3gghLvYd3W8bqc/jTYmp5L6NY8F7ifoOCk piDoWyIjQB2vyB1+/ErIVhRUZ2vpXAKNbgSH83d6fBUFAwI+RaEE2WP9LZNmTJvH9sot Zko5zKxJeKBJDJ97S/AaSWcJMWlqvcs03+Iz8yzh6nH1u/yJjZ3HqpuQZqhzEPy/DsiQ UpipWz+XxMCgHHHovhjnYV27Cbjz3vh7Z+0ZDsITJ9h0BN2+3wEYNJhM9oCK5jiZkPzO fCBeUdRPiWX+aGQSaat4ImV+DgCSgSJ3FLOFuOfAyC6Zm4ai9BEKuN2EeBHMDuYoEAzQ IClA== X-Gm-Message-State: ACgBeo1cy5XeI78ocp+Oo8bWxKPQ+g7U6wGRr2yJvGdxVY8xemDMUTlE j/lNbkpxWm7Tig6/tYOF19y1tgFvb0m0i4X2+DA5uhbtd7hIabl1Rn03Gwsj7d/veTdbPLOC30u bV3r7ZHlkxSbt8Eq1F9I/AUThwNiGLm3NyUEN431uPjQe8Ef7kGxQpt35NTuFLuAwaH6Dn/Am9/ J/ X-Received: by 2002:adf:e30c:0:b0:21e:51b9:113c with SMTP id b12-20020adfe30c000000b0021e51b9113cmr12594435wrj.247.1659429743822; Tue, 02 Aug 2022 01:42:23 -0700 (PDT) X-Google-Smtp-Source: AA6agR68Br9lvaisAnOSvsrL/jy0yqyzJQxAgckkxeOgYyGYsx7EHgZcGonLMjxfQhNaIRqQ0BwU5g== X-Received: by 2002:adf:e30c:0:b0:21e:51b9:113c with SMTP id b12-20020adfe30c000000b0021e51b9113cmr12594416wrj.247.1659429743552; Tue, 02 Aug 2022 01:42:23 -0700 (PDT) Received: from vschneid.remote.csb ([185.11.37.247]) by smtp.gmail.com with ESMTPSA id f12-20020a05600c154c00b003a31169a7f4sm24825800wmg.12.2022.08.02.01.42.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 02 Aug 2022 01:42:23 -0700 (PDT) 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: [RFC PATCH v3 3/3] DEBUG-DO-NOT-MERGE: workqueue: kworker spawner Date: Tue, 2 Aug 2022 09:41:46 +0100 Message-Id: <20220802084146.3922640-4-vschneid@redhat.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20220802084146.3922640-1-vschneid@redhat.com> References: <20220802084146.3922640-1-vschneid@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org --- kernel/Makefile | 2 +- kernel/workqueue.c | 9 +++++- kernel/wqstress.c | 69 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 78 insertions(+), 2 deletions(-) create mode 100644 kernel/wqstress.c diff --git a/kernel/Makefile b/kernel/Makefile index a7e1f49ab2b3..860133f7bca5 100644 --- a/kernel/Makefile +++ b/kernel/Makefile @@ -10,7 +10,7 @@ obj-y =3D fork.o exec_domain.o panic.o \ extable.o params.o platform-feature.o \ kthread.o sys_ni.o nsproxy.o \ notifier.o ksysfs.o cred.o reboot.o \ - async.o range.o smpboot.o ucount.o regset.o + async.o range.o smpboot.o ucount.o regset.o wqstress.o =20 obj-$(CONFIG_USERMODE_DRIVER) +=3D usermode_driver.o obj-$(CONFIG_MODULES) +=3D kmod.o diff --git a/kernel/workqueue.c b/kernel/workqueue.c index 28cd58c684ee..4ffd50a3db46 100644 --- a/kernel/workqueue.c +++ b/kernel/workqueue.c @@ -91,7 +91,7 @@ enum { BUSY_WORKER_HASH_ORDER =3D 6, /* 64 pointers */ =20 MAX_IDLE_WORKERS_RATIO =3D 4, /* 1/4 of busy can be idle */ - IDLE_WORKER_TIMEOUT =3D 300 * HZ, /* keep idle ones for 5 mins */ + IDLE_WORKER_TIMEOUT =3D 3 * HZ, /* keep idle ones for 5 mins */ =20 MAYDAY_INITIAL_TIMEOUT =3D HZ / 100 >=3D 2 ? HZ / 100 : 2, /* call for help after 10ms @@ -1996,6 +1996,10 @@ static void reap_workers(struct list_head *reaplist) struct worker *worker, *tmp; =20 list_for_each_entry_safe(worker, tmp, reaplist, entry) { + pr_info("WORKER_REAP: task=3D%s cpu=3D%d this_task=3D%s this_cpu=3D%d\n", + worker->task->comm, task_cpu(worker->task), + current->comm, raw_smp_processor_id()); + list_del_init(&worker->entry); unbind_worker(worker); =20 @@ -2489,6 +2493,9 @@ static int worker_thread(void *__worker) WARN_ON_ONCE(!list_empty(&worker->entry)); set_pf_worker(false); =20 + pr_info("WORKER_DIE: task=3D%s this_cpu=3D%d\n", + current->comm, raw_smp_processor_id()); + set_task_comm(worker->task, "kworker/dying"); ida_free(&pool->worker_ida, worker->id); worker_detach_from_pool(worker); diff --git a/kernel/wqstress.c b/kernel/wqstress.c new file mode 100644 index 000000000000..16a3771027cd --- /dev/null +++ b/kernel/wqstress.c @@ -0,0 +1,69 @@ +// SPDX-License-Identifier: GPL-2.0 +#include +#include +#include + +MODULE_AUTHOR("Valentin Schneider "); +MODULE_LICENSE("GPL"); + +#define TARGET_CPU 3 + +static void wqstress_workfn(struct work_struct *work) +{ + schedule_timeout_interruptible(10 * HZ); +} + +#define DECL_WORK(n) static DECLARE_WORK(wqstress_work_##n, wqstress_workf= n) +#define KICK_WORK(n) do { \ + schedule_work_on(TARGET_CPU, &wqstress_work_##n); \ + } while (0); +#define FLUSH_WORK(n) do { \ + flush_work(&wqstress_work_##n); \ + } while (0); + +DECL_WORK(0); +DECL_WORK(1); +DECL_WORK(2); +DECL_WORK(3); +DECL_WORK(4); +DECL_WORK(5); +DECL_WORK(6); +DECL_WORK(7); +DECL_WORK(8); +DECL_WORK(9); + +/* + * This should create =E2=89=88(N-1) extra kworkers for N kicked work + */ +static int __init wqstress_init(void) +{ + pr_info("WQSTRESS START\n"); + + sched_set_fifo_low(current); + + KICK_WORK(0); + KICK_WORK(1); + KICK_WORK(2); + KICK_WORK(3); + KICK_WORK(4); + KICK_WORK(5); + KICK_WORK(6); + KICK_WORK(7); + KICK_WORK(8); + KICK_WORK(9); + + FLUSH_WORK(0); + FLUSH_WORK(1); + FLUSH_WORK(2); + FLUSH_WORK(3); + FLUSH_WORK(4); + FLUSH_WORK(5); + FLUSH_WORK(6); + FLUSH_WORK(7); + FLUSH_WORK(8); + FLUSH_WORK(9); + + return 0; +} + +late_initcall_sync(wqstress_init); --=20 2.31.1