mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] kthread: fall back when no preferred housekeeping CPU is online
@ 2026-10-20 12:55 Avi Weiss
  2026-10-20 15:08 ` Bradley Morgan
  0 siblings, 1 reply; 2+ messages in thread
From: Avi Weiss @ 2026-10-20 12:55 UTC (permalink / raw)
  To: Frederic Weisbecker; +Cc: Waiman Long, linux-kernel, Avi Weiss

kthread_fetch_affinity() falls back to the housekeeping mask
only when the intersection with the preferred mask is empty.

If the intersection contains only offline CPUs,
set_cpus_allowed_ptr() returns -EINVAL and the kthread keeps
its previous affinity, which may still include an isolated CPU.

Check whether the intersection contains an online CPU instead.
Fall back to the full housekeeping mask when none are online.

Fixes: e894f6339808 ("kthread: Honour kthreads preferred affinity after cpuset changes")
Signed-off-by: Avi Weiss <thnkslprpt@gmail.com>
---

Tested on x86_64 Linux 7.3.0 with vng/qemu and a kthread with 
preferred CPUs 1-2.

With CPU 2 offline and CPU 1 isolated, the unpatched kernel kept
affinity 1-2 and set_cpus_allowed_ptr() returned -EINVAL. With the
fix, affinity changed to 0,2. Bringing CPU 2 online restored 
affinity to 2, and removing isolation restored 1-2.

 kernel/kthread.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/kernel/kthread.c b/kernel/kthread.c
index cc8cb5d3eab7..fdb8fb07f40b 100644
--- a/kernel/kthread.c
+++ b/kernel/kthread.c
@@ -343,7 +343,7 @@ static void kthread_fetch_affinity(struct kthread *kthread, struct cpumask *cpum
 	}
 
 	cpumask_and(cpumask, pref, housekeeping_cpumask(HK_TYPE_DOMAIN));
-	if (cpumask_empty(cpumask))
+	if (!cpumask_intersects(cpumask, cpu_online_mask))
 		cpumask_copy(cpumask, housekeeping_cpumask(HK_TYPE_DOMAIN));
 }
 

base-commit: f47d223627f80cb7a1eb14c585fe6cb35de5e984
-- 
2.43.0


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [PATCH] kthread: fall back when no preferred housekeeping CPU is online
  2026-10-20 12:55 [PATCH] kthread: fall back when no preferred housekeeping CPU is online Avi Weiss
@ 2026-10-20 15:08 ` Bradley Morgan
  0 siblings, 0 replies; 2+ messages in thread
From: Bradley Morgan @ 2026-10-20 15:08 UTC (permalink / raw)
  To: thnkslprpt; +Cc: frederic, linux-kernel, longman

On 20 October 2026 13:55:18 BST, Avi Weiss <thnkslprpt@gmail.com> wrote:
>kthread_fetch_affinity() falls back to the housekeeping mask
>only when the intersection with the preferred mask is empty.
>
>If the intersection contains only offline CPUs,
>set_cpus_allowed_ptr() returns -EINVAL and the kthread keeps
>its previous affinity, which may still include an isolated CPU.
>
>Check whether the intersection contains an online CPU instead.
>Fall back to the full housekeeping mask when none are online.
>
>Fixes: e894f6339808 ("kthread: Honour kthreads preferred affinity after cpuset changes")
>Signed-off-by: Avi Weiss <thnkslprpt@gmail.com>
>---
>
>Tested on x86_64 Linux 7.3.0 with vng/qemu and a kthread with 
>preferred CPUs 1-2.
>
>With CPU 2 offline and CPU 1 isolated, the unpatched kernel kept
>affinity 1-2 and set_cpus_allowed_ptr() returned -EINVAL. With the
>fix, affinity changed to 0,2. Bringing CPU 2 online restored 
>affinity to 2, and removing isolation restored 1-2.
>
> kernel/kthread.c | 2 +-
> 1 file changed, 1 insertion(+), 1 deletion(-)
>
>diff --git a/kernel/kthread.c b/kernel/kthread.c
>index cc8cb5d3eab7..fdb8fb07f40b 100644
>--- a/kernel/kthread.c
>+++ b/kernel/kthread.c
>@@ -343,7 +343,7 @@ static void kthread_fetch_affinity(struct kthread *kthread, struct cpumask *cpum
> 	}
> 
> 	cpumask_and(cpumask, pref, housekeeping_cpumask(HK_TYPE_DOMAIN));
>-	if (cpumask_empty(cpumask))
>+	if (!cpumask_intersects(cpumask, cpu_online_mask))

Nice catch! 

Reviewed-by: Bradley Morgan <brads@mainlining.org>


> 		cpumask_copy(cpumask, housekeeping_cpumask(HK_TYPE_DOMAIN));
> }
> 
>
>base-commit: f47d223627f80cb7a1eb14c585fe6cb35de5e984
>

--- Thanks!
"I'm not a very positive person" - Linus torvalds

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-10-20 15:08 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-10-20 12:55 [PATCH] kthread: fall back when no preferred housekeeping CPU is online Avi Weiss
2026-10-20 15:08 ` Bradley Morgan

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®