From patchwork Fri Jul 26 21:56:48 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Frederic Weisbecker X-Patchwork-Id: 13743192 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 kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 2AA25C3DA7F for ; Fri, 26 Jul 2024 21:57:47 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id DEB366B0092; Fri, 26 Jul 2024 17:57:45 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id D7A666B0096; Fri, 26 Jul 2024 17:57:45 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id ADE326B0093; Fri, 26 Jul 2024 17:57:45 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id 8C5D26B0092 for ; Fri, 26 Jul 2024 17:57:45 -0400 (EDT) Received: from smtpin11.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 3C519120175 for ; Fri, 26 Jul 2024 21:57:45 +0000 (UTC) X-FDA: 82383266490.11.2C2ECB3 Received: from sin.source.kernel.org (sin.source.kernel.org [145.40.73.55]) by imf24.hostedemail.com (Postfix) with ESMTP id EB31718000F for ; Fri, 26 Jul 2024 21:57:41 +0000 (UTC) Authentication-Results: imf24.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=heNYtWqP; spf=pass (imf24.hostedemail.com: domain of frederic@kernel.org designates 145.40.73.55 as permitted sender) smtp.mailfrom=frederic@kernel.org; dmarc=pass (policy=none) header.from=kernel.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1722031012; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=CW1A0sP7ya8kf/pccF0TH8tFOgIiSQElkEzym5WR3+0=; b=ku8gFg8LzfIaHzin2GYyiZtI0dzjV3mftgQ5u0No5+cusEtPQkdt4culnnhwE4Ij/nC2zt rVYZJJ9VmbwY5haia4Ab0Q6AsZD5GgO+BMakyFF3oVoarxrVX7RNIpaCxutrofFTCZogdH Dpc9QOdpne5+OrTTwIlh8N+g90mAFNM= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1722031012; a=rsa-sha256; cv=none; b=o6fWIirBTSGCX+VkLRLNyaQj9DCjXC8x3o2Tsp9x1btVULwSrfCNj4aE5gwPShlfreODB/ Zz2IQMTdg0XijyIBz11UCTF/w8CxGILAgue8WBA2k3d+4YL5e7o1mi0wIjvzETD5yIX6fR JD3uTydApWPPFko61NLq3YDoHAn2DLc= ARC-Authentication-Results: i=1; imf24.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=heNYtWqP; spf=pass (imf24.hostedemail.com: domain of frederic@kernel.org designates 145.40.73.55 as permitted sender) smtp.mailfrom=frederic@kernel.org; dmarc=pass (policy=none) header.from=kernel.org Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by sin.source.kernel.org (Postfix) with ESMTP id 3985ECE1952; Fri, 26 Jul 2024 21:57:38 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 4DCB0C4AF0B; Fri, 26 Jul 2024 21:57:34 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1722031057; bh=ti4mfUgveAuqTAPPwXGYkPNNFuo7Vc9aOZIdnCtmsYQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=heNYtWqPzI279b23ynorkZtq9s+XtrGZuDGs5HGiedBJIbhqIYfZlNLZbVnJRlGWb OoIJj85ZxFgF5Pf0IoPAk1ly5fkb/U6UyuT5cW0GnVsadONHBpy7j2gDi7LQybe50+ I4fHIxGYUhytu7KJ87eKYGc87z8Mzg0xc3xy96B3iDivajwqofkWVWVJujy2Gx3XVF a2bFKPz4bsNe5kK7YlfbJU6R/0i7zEH59l/Er15+P8rBvDbO2OVJ0sx5U7dcnX1D2n wWZXB/LQV7ncN0jrEUJxS/KDWlj6z3hX0I+H0asvSQRZgwj5QmDkOnsHlYVNJPc5yU wB+175sFPcwyQ== From: Frederic Weisbecker To: LKML Cc: Frederic Weisbecker , Andrew Morton , Kees Cook , Peter Zijlstra , Thomas Gleixner , Michal Hocko , Vlastimil Babka , linux-mm@kvack.org, "Paul E. McKenney" , Neeraj Upadhyay , Joel Fernandes , Boqun Feng , Zqiang , rcu@vger.kernel.org Subject: [RFC PATCH 12/20] kthread: Implement preferred affinity Date: Fri, 26 Jul 2024 23:56:48 +0200 Message-ID: <20240726215701.19459-13-frederic@kernel.org> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20240726215701.19459-1-frederic@kernel.org> References: <20240726215701.19459-1-frederic@kernel.org> MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: EB31718000F X-Stat-Signature: echhoykdxgnq7buaaa88nkipjshdpf1n X-HE-Tag: 1722031061-716740 X-HE-Meta: U2FsdGVkX1/+CWIOCpzsE+/Itx88PDOg3fMZL+ePrgLX1waPyL4NpXS/6t+0NGreWAghYHMi9c3NlxPdA1/poRrqEn09qwTb2J1Yg4BNMczDLpXoYCbimT9JFaUPZQfzLYJ1R1pKej4NPzGG3q+W86j94HgHMDSuLK46reN/UzdxIIfhgorX50LZSnUSYe1hMfmYPhKTvmsJED4Bh+DnNDLL39GKGa73PaXmJ1Gb64DpMvJeGFb1tsmXJFcAG7Z7h2zEStREA+2IARchSSR5kzNT18xt2D9amkMBX6f28/wdWFHDi/gL/Ai+tzKrlndnnd0Ulrwt1EAO29G84TVSaHzr12uHukmttI47qO+vbKZRHZQUDQvSW1KB6nKGlyeTLE0TcHy6If7rh/wmNXXfUFQQINbMh/R2ona9FGjCf7fS6YqIUr9txT865PqSxb6GUutNEwinz590HM4ooswkIB1pBmiOxiQvIRPSWKj0/2IdNUAKPEXeRyBu4fwyDvugy3S/KEL6pZpdVb+idty6F3o0Bv2pxJZfAreZ/IEoJBg1Jz4UToqzRJUgo33U+hjUfUFLlcmkmDoQJ72UOeTNlwaVlvKc0+/2SRnPXKC0BKu2B9x9FrLtlnw8qq9568VOGxPwYXAcUNyLetVWFTR2b2fpUv7j0GIhAHp7zuv3A+6v4mbL9rgIF2vXIpRdJcOhodx/rjXO1CnDQlLfF2vnrSf/YfHIMHqsj80VLEeauv8RXUrbGNv3GIGWxtPQ1IPm9V2mmRD53x4sIa4FEZDGsk+hqZ6Z7RSqM3mPx+L/PAub/wv9+wOX4e2EutJ20vJvo2MRoxutUhUV2xYA32GNIJ74XWPpsfrAFfnE+v1ztMlHyAGoUwbGdnMINVUFV/rP1wRJllTtIL3qTzHqk5jD9TBGGbBJsky1H/7oRnqKxfM6sv5kXyzQxWe9GlrouFHiq+DXTPjRfc+HFQrxFEe yI7iNYPx T4fnyk96majneFGLDalOCpCdWBFVgZutFNLhZYKO6+t2LQmoSzIwpMFTBygOTV0dg01gWwxhChqMHvy9S++E2bKXJU6WVK+gXebSnmbuTpJ32KlqdHIj2sFzXtKy1ji/zPHoLQJaQBK/f53zypuzwk5z4xCv2/VaNmEQqtpZ69+Xk79WZ65lqg4Esy74c3ykAOXUJiAbH9uxJiEdZKCVlDx9ZymSWXT0jYI+M1XKhmRh2L/S2QpVRZtKfGLNMTIhUaugqGZwD2/LyZYUbQP97V6QVSyBwiJO5fYyEAY6Ss3St/zD8RhsShqjAhg== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: Affining kthreads follow either of three existing different patterns: 1) Per-CPU kthreads must stay affine to a single CPU and never execute relevant code on any other CPU. This is currently handled by smpboot code which takes care of CPU-hotplug operations. 2) Kthreads that _have_ to be affine to a specific set of CPUs and can't run anywhere else. The affinity is set through kthread_bind_mask() and the subsystem takes care by itself to handle CPU-hotplug operations. 3) Kthreads that have a _preferred_ affinity but that can run anywhere without breaking correctness. Userspace can overwrite the affinity. It is set manually like any other task and CPU-hotplug is supposed to be handled by the relevant subsystem so that the task is properly reaffined whenever a given CPU from the preferred affinity comes up or down. Also care must be taken so that the preferred affinity doesn't cross housekeeping cpumask boundaries. Currently the preferred affinity pattern has at least 4 identified users, with more or less success when it comes to handle CPU-hotplug operations and housekeeping cpumask. Provide an infrastructure to handle this usecase patter. A new kthread_affine_preferred() API is introduced, to be used just like kthread_bind_mask(), right after kthread creation and before the first wake up. The kthread is then affine right away to the cpumask passed through the API if it has online housekeeping CPUs. Otherwise it will be affine to all online housekeeping CPUs as a last resort. It is aware of CPU hotplug events such that: * When a housekeeping CPU goes up and is part of the preferred affinity of a given kthread, it is added to its applied affinity set (and possibly the default last resort online housekeeping set is removed from the set). * When a housekeeping CPU goes down while it was part of the preferred affinity of a kthread, it is removed from the kthread's applied affinity. The last resort is to affine the kthread to all online housekeeping CPUs. Signed-off-by: Frederic Weisbecker Acked-by: Vlastimil Babka --- include/linux/cpuhotplug.h | 1 + include/linux/kthread.h | 1 + kernel/kthread.c | 121 +++++++++++++++++++++++++++++++++++++ 3 files changed, 123 insertions(+) diff --git a/include/linux/cpuhotplug.h b/include/linux/cpuhotplug.h index 7a5785f405b6..5c204bd0fed6 100644 --- a/include/linux/cpuhotplug.h +++ b/include/linux/cpuhotplug.h @@ -238,6 +238,7 @@ enum cpuhp_state { CPUHP_AP_WORKQUEUE_ONLINE, CPUHP_AP_RANDOM_ONLINE, CPUHP_AP_RCUTREE_ONLINE, + CPUHP_AP_KTHREADS_ONLINE, CPUHP_AP_BASE_CACHEINFO_ONLINE, CPUHP_AP_ONLINE_DYN, CPUHP_AP_ONLINE_DYN_END = CPUHP_AP_ONLINE_DYN + 40, diff --git a/include/linux/kthread.h b/include/linux/kthread.h index b11f53c1ba2e..30209bdf83a2 100644 --- a/include/linux/kthread.h +++ b/include/linux/kthread.h @@ -85,6 +85,7 @@ kthread_run_on_cpu(int (*threadfn)(void *data), void *data, void free_kthread_struct(struct task_struct *k); void kthread_bind(struct task_struct *k, unsigned int cpu); void kthread_bind_mask(struct task_struct *k, const struct cpumask *mask); +int kthread_affine_preferred(struct task_struct *p, const struct cpumask *mask); int kthread_stop(struct task_struct *k); int kthread_stop_put(struct task_struct *k); bool kthread_should_stop(void); diff --git a/kernel/kthread.c b/kernel/kthread.c index ecb719f54f7a..cfa6e1b8d933 100644 --- a/kernel/kthread.c +++ b/kernel/kthread.c @@ -35,6 +35,10 @@ static DEFINE_SPINLOCK(kthread_create_lock); static LIST_HEAD(kthread_create_list); struct task_struct *kthreadd_task; +static struct cpumask kthread_online_mask; +static LIST_HEAD(kthreads_hotplug); +static DEFINE_MUTEX(kthreads_hotplug_lock); + struct kthread_create_info { /* Information passed to kthread() from kthreadd. */ @@ -64,6 +68,9 @@ struct kthread { #endif /* To store the full name if task comm is truncated. */ char *full_name; + struct task_struct *task; + struct list_head hotplug_node; + struct cpumask *preferred_affinity; }; enum KTHREAD_BITS { @@ -124,6 +131,7 @@ bool set_kthread_struct(struct task_struct *p) init_completion(&kthread->parked); p->vfork_done = &kthread->exited; + kthread->task = p; p->worker_private = kthread; return true; } @@ -314,6 +322,16 @@ void __noreturn kthread_exit(long result) { struct kthread *kthread = to_kthread(current); kthread->result = result; + if (kthread->preferred_affinity) { + mutex_lock(&kthreads_hotplug_lock); + list_del(&kthread->hotplug_node); + /* Make sure the kthread never gets re-affined globally */ + set_cpus_allowed_ptr(current, housekeeping_cpumask(HK_TYPE_KTHREAD)); + mutex_unlock(&kthreads_hotplug_lock); + + kfree(kthread->preferred_affinity); + kthread->preferred_affinity = NULL; + } do_exit(0); } EXPORT_SYMBOL(kthread_exit); @@ -779,6 +797,109 @@ int kthreadd(void *unused) return 0; } +static void kthread_fetch_affinity(struct kthread *k, struct cpumask *mask) +{ + cpumask_and(mask, k->preferred_affinity, &kthread_online_mask); + cpumask_and(mask, mask, housekeeping_cpumask(HK_TYPE_KTHREAD)); + if (cpumask_empty(mask)) + cpumask_copy(mask, housekeeping_cpumask(HK_TYPE_KTHREAD)); +} + +int kthread_affine_preferred(struct task_struct *p, const struct cpumask *mask) +{ + struct kthread *kthread = to_kthread(p); + cpumask_var_t affinity; + unsigned long flags; + int ret; + + if (!wait_task_inactive(p, TASK_UNINTERRUPTIBLE) || kthread->started) { + WARN_ON(1); + return -EINVAL; + } + + if (!zalloc_cpumask_var(&affinity, GFP_KERNEL)) + return -ENOMEM; + + kthread->preferred_affinity = kzalloc(sizeof(struct cpumask), GFP_KERNEL); + if (!kthread->preferred_affinity) { + ret = -ENOMEM; + goto out; + } + + mutex_lock(&kthreads_hotplug_lock); + cpumask_copy(kthread->preferred_affinity, mask); + list_add_tail(&kthread->hotplug_node, &kthreads_hotplug); + kthread_fetch_affinity(kthread, affinity); + + /* It's safe because the task is inactive. */ + raw_spin_lock_irqsave(&p->pi_lock, flags); + do_set_cpus_allowed(p, mask); + raw_spin_unlock_irqrestore(&p->pi_lock, flags); + + mutex_unlock(&kthreads_hotplug_lock); +out: + free_cpumask_var(affinity); + + return 0; +} + +static int kthreads_hotplug_update(void) +{ + cpumask_var_t affinity; + struct kthread *k; + int err = 0; + + if (list_empty(&kthreads_hotplug)) + return 0; + + if (!zalloc_cpumask_var(&affinity, GFP_KERNEL)) + return -ENOMEM; + + list_for_each_entry(k, &kthreads_hotplug, hotplug_node) { + if (WARN_ON_ONCE(!k->preferred_affinity)) { + err = -EINVAL; + break; + } + kthread_fetch_affinity(k, affinity); + set_cpus_allowed_ptr(k->task, affinity); + } + + free_cpumask_var(affinity); + + return err; +} + +static int kthreads_offline_cpu(unsigned int cpu) +{ + int ret = 0; + + mutex_lock(&kthreads_hotplug_lock); + cpumask_clear_cpu(cpu, &kthread_online_mask); + ret = kthreads_hotplug_update(); + mutex_unlock(&kthreads_hotplug_lock); + + return ret; +} + +static int kthreads_online_cpu(unsigned int cpu) +{ + int ret = 0; + + mutex_lock(&kthreads_hotplug_lock); + cpumask_set_cpu(cpu, &kthread_online_mask); + ret = kthreads_hotplug_update(); + mutex_unlock(&kthreads_hotplug_lock); + + return ret; +} + +static int kthreads_init(void) +{ + return cpuhp_setup_state(CPUHP_AP_KTHREADS_ONLINE, "kthreads:online", + kthreads_online_cpu, kthreads_offline_cpu); +} +early_initcall(kthreads_init); + void __kthread_init_worker(struct kthread_worker *worker, const char *name, struct lock_class_key *key)