From patchwork Sat Jul 29 13:53:33 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Aaron Tomlin X-Patchwork-Id: 128142 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:918b:0:b0:3e4:2afc:c1 with SMTP id s11csp1064371vqg; Sat, 29 Jul 2023 07:37:03 -0700 (PDT) X-Google-Smtp-Source: APBJJlHVI/MByu81U2Ai3evUULyqtyXWElNsBqVNlKT4gmvtjC/YAzEPD9S9upHWeNDnLuuDRXp4 X-Received: by 2002:a17:902:c404:b0:1bb:35b6:448f with SMTP id k4-20020a170902c40400b001bb35b6448fmr5722918plk.15.1690641423122; Sat, 29 Jul 2023 07:37:03 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1690641423; cv=none; d=google.com; s=arc-20160816; b=gZacaYt+Aj1OEGi5ppkxBSxblne2NoZU/hjMcdHAd7svKkes2onEFGpfv//9yJQ/V2 VhEk9ZWU+tXhm4RYV9a0aVk+eZJmAiFMyt/Qgpl0bnnWM3feg4dTk4pkasQtx7hHoFfC zzynot90VWIw4UuI0SqZNWH8SBCISqSlggyNIZJgbREKsECfvCKA8LQ1w9w8ADYQO8Hk gqdaIherew4BEYwjDdpcNfCrYYMKemuSQJndKsmBL2y+PQygLEVr1z3aM70kCsLAoPAH bqzL9BK1gregVWVepJBleDVJVRsrxnbftNW+4fmMqfzzgiSFv89rrs+9oZ2rPDtutSr+ uMZw== 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 Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id kg14-20020a170903060e00b001b87ecb0e5csi4516863plb.126.2023.07.29.07.36.48; Sat, 29 Jul 2023 07:37:03 -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 S231598AbjG2OBK (ORCPT + 99 others); Sat, 29 Jul 2023 10:01:10 -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 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 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1772765517508955464 X-GMAIL-MSGID: 1772766020644240107 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; }