Received: by 2002:a05:6358:c692:b0:131:369:b2a3 with SMTP id fe18csp2498241rwb; Sat, 29 Jul 2023 07:59:34 -0700 (PDT) X-Google-Smtp-Source: APBJJlHyNDBbez3Syf79U1NFqQWyP91noizc8TLt1E5T2dowAMUGuiCBDBvR/M8h1qMU/jMsLK/g X-Received: by 2002:a17:90b:3b8a:b0:268:420:2e04 with SMTP id pc10-20020a17090b3b8a00b0026804202e04mr3577013pjb.49.1690642774280; Sat, 29 Jul 2023 07:59:34 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1690642774; cv=none; d=google.com; s=arc-20160816; b=Pqm9e+06r/samBGdDw7SLA721c9H+/z8QhXe5jXSI82pL+On6hAHILHp+JrlBeKWjg MJtFQDU+7LaOVcTbXIQWubnEVUdLPjxLGQUrHNb7xIdUZDP1Lm2rYQcScGh+fYzMl2oZ XF4DvYxEOGO3pI/c46fBfu8Ewp+tQ2wahyP5EHE6Lk9nBTXZW7HlWvH+vJsihoUi6g6G R2zDXECLmyf318OdPICM/XlftscOFBKpvEx70YlG19KGmo5eYEargagr553O1MTilrj+ xmNq9OxLxPFDAbT3mHR+yDmiJFhj+kEIkGBA8ZFkCwVVJklfjYF4VoQRZ0A1P1Ydi0MO 3s4Q== 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; bh=mE37UsZqSn9znd5Sx+VfCRH6FgOh5he8cYPTOBX4DUQ=; fh=iIrmuiooHb58lVSZg9OlpW7yJYF2kt3Km03sQ9AihSM=; b=XEcGbJ/0pYQ7n7Bqz01LZJHtcpfG2XBMzn/JDtXSMOLc8eGN6ODkxNutPgEBiTINcN aL8GP7H/EjpYEktxEbYeq1/jmtnAbw4jycfyD678N0kkkGvn4JCTDh8lZG32Ks0ZfvMU KPA9zMxqzFI9GKrnlcjxSWQepqLPUISwwqDdfHFe3upV2KLce93Ri5XK9j0DQriOMooO /HC/WCGG4aZFEahQOr8rr8UGzBmcHmziwTPnX8Gq9kOXu1vYkkdxT1RK4bU0/hrCB26O QuESm39Qr0+xF8of8UWx35KpgiIcGW9doUZETGfjRPscA8hRLzNL0jVZ/ec5/XiccQxt pVNg== ARC-Authentication-Results: i=1; mx.google.com; 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 Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id em9-20020a17090b014900b002680e08a877si6093369pjb.186.2023.07.29.07.59.21; Sat, 29 Jul 2023 07:59:34 -0700 (PDT) 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; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231586AbjG2OBJ (ORCPT + 99 others); Sat, 29 Jul 2023 10:01:09 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50858 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231379AbjG2OBI (ORCPT ); Sat, 29 Jul 2023 10:01:08 -0400 X-Greylist: delayed 305 seconds by postgrey-1.37 at lindbergh.monkeyblade.net; Sat, 29 Jul 2023 07:01:07 PDT Received: from sxb1plsmtpa01-09.prod.sxb1.secureserver.net (sxb1plsmtpa01-09.prod.sxb1.secureserver.net [188.121.53.95]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 019731707 for ; Sat, 29 Jul 2023 07:01:06 -0700 (PDT) Received: from localhost ([82.27.99.45]) by :SMTPAUTH: with ESMTPA id PkO0qJaTmd2niPkO3qO8D4; Sat, 29 Jul 2023 06:53:40 -0700 X-CMAE-Analysis: v=2.4 cv=Jax672GV c=1 sm=1 tr=0 ts=64c519e5 a=YwMIiW7BGddQzL8MrqPWMg==:117 a=YwMIiW7BGddQzL8MrqPWMg==:17 a=j-LfP5YGAAAA:8 a=Ni_nvw8SZd987JkjuKUA:9 a=pci6KG57UX3UzFLC8IW4:22 X-SECURESERVER-ACCT: atomlin@atomlin.com From: Aaron Tomlin To: linux-kernel@vger.kernel.org Cc: tj@kernel.org, jiangshanlai@gmail.com, peterz@infradead.org Subject: [RFC PATCH 1/2] workqueue: Introduce PF_WQ_RESCUE_WORKER Date: Sat, 29 Jul 2023 14:53:33 +0100 Message-Id: <20230729135334.566138-2-atomlin@atomlin.com> X-Mailer: git-send-email 2.39.1 In-Reply-To: <20230729135334.566138-1-atomlin@atomlin.com> References: <20230729135334.566138-1-atomlin@atomlin.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-CMAE-Envelope: MS4xfHMW2asXrhdKbVG7RHnaPmtF0hWKkilp+9ztc8Mc21IXu2C7qnb9mNoZzhAiIaHq+I0a4jm+Gu4RhsDruM/P97xNIRIaVoUpqQqqNT20KkGPxMlMB+wE wtnfQB9ADGcQvj2CyXC9hknYGChNVCW9tcAgPkeL3kEy5BoMiJNtd73LoaQiMvG5ECuphwm0uMCiQ+SiD05aQI0HO9l8qe2L4yzmfkQDxBgxobSN4W/W02NI d1Qq3FCnQLJIZyOBL4gMFraxZiVUJXrzE/tmAIyI2+I= X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,SPF_HELO_NONE, SPF_PASS,T_SCC_BODY_TEXT_LINE 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 The Linux kernel does not provide a way to differentiate between a kworker and a rescue kworker for user-mode. From user-mode, one can establish if a task is a kworker by testing for PF_WQ_WORKER in a specified task's flags bit mask (or bitmap) via /proc/[PID]/stat. Indeed, one can examine /proc/[PID]/stack and search for the function namely "rescuer_thread". This is only available to the root user. It can be useful to identify a rescue kworker since their CPU affinity cannot be modified and their initial CPU assignment can be safely ignored. Furthermore, a workqueue that was created with WQ_MEM_RECLAIM and WQ_SYSFS the cpumask file is not applicable to the rescue kworker. By design a rescue kworker should run anywhere. This patch introduces PF_WQ_RESCUE_WORKER and ensures it is set and cleared appropriately. Signed-off-by: Aaron Tomlin --- include/linux/sched.h | 2 +- kernel/workqueue.c | 19 ++++++++++++------- 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/include/linux/sched.h b/include/linux/sched.h index 609bde814cb0..039fcf8d9ed6 100644 --- a/include/linux/sched.h +++ b/include/linux/sched.h @@ -1734,7 +1734,7 @@ extern struct pid *cad_pid; #define PF_USED_MATH 0x00002000 /* If unset the fpu must be initialized before use */ #define PF_USER_WORKER 0x00004000 /* Kernel thread cloned from userspace thread */ #define PF_NOFREEZE 0x00008000 /* This thread should not be frozen */ -#define PF__HOLE__00010000 0x00010000 +#define PF_WQ_RESCUE_WORKER 0x00010000 /* I am a rescue workqueue worker */ #define PF_KSWAPD 0x00020000 /* I am kswapd */ #define PF_MEMALLOC_NOFS 0x00040000 /* All allocation requests will inherit GFP_NOFS */ #define PF_MEMALLOC_NOIO 0x00080000 /* All allocation requests will inherit GFP_NOIO */ diff --git a/kernel/workqueue.c b/kernel/workqueue.c index 02a8f402eeb5..6d38d714b72b 100644 --- a/kernel/workqueue.c +++ b/kernel/workqueue.c @@ -2665,13 +2665,18 @@ static void process_scheduled_works(struct worker *worker) } } -static void set_pf_worker(bool val) +static void set_pf_worker_and_rescuer(bool worker, bool rescue) { mutex_lock(&wq_pool_attach_mutex); - if (val) + if (worker) { current->flags |= PF_WQ_WORKER; - else + if (rescue) + current->flags |= PF_WQ_RESCUE_WORKER; + } else { current->flags &= ~PF_WQ_WORKER; + if (rescue) + current->flags &= ~PF_WQ_RESCUE_WORKER; + } mutex_unlock(&wq_pool_attach_mutex); } @@ -2693,14 +2698,14 @@ static int worker_thread(void *__worker) struct worker_pool *pool = worker->pool; /* tell the scheduler that this is a workqueue worker */ - set_pf_worker(true); + set_pf_worker_and_rescuer(true, false); woke_up: raw_spin_lock_irq(&pool->lock); /* am I supposed to die? */ if (unlikely(worker->flags & WORKER_DIE)) { raw_spin_unlock_irq(&pool->lock); - set_pf_worker(false); + set_pf_worker_and_rescuer(false, false); set_task_comm(worker->task, "kworker/dying"); ida_free(&pool->worker_ida, worker->id); @@ -2804,7 +2809,7 @@ static int rescuer_thread(void *__rescuer) * Mark rescuer as worker too. As WORKER_PREP is never cleared, it * doesn't participate in concurrency management. */ - set_pf_worker(true); + set_pf_worker_and_rescuer(true, true); repeat: set_current_state(TASK_IDLE); @@ -2903,7 +2908,7 @@ static int rescuer_thread(void *__rescuer) if (should_stop) { __set_current_state(TASK_RUNNING); - set_pf_worker(false); + set_pf_worker_and_rescuer(false, true); return 0; } -- 2.39.1