Re: [PATCH] kthread: fall back when no preferred housekeeping CPU is online
From: Bradley Morgan
Date: Tue Oct 20 2026 - 11:24:15 EST
On 20 October 2026 13:55:18 BST, Avi Weiss <thnkslprpt@xxxxxxxxx> 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@xxxxxxxxx>
>---
>
>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@xxxxxxxxxxxxxx>
> cpumask_copy(cpumask, housekeeping_cpumask(HK_TYPE_DOMAIN));
> }
>
>
>base-commit: f47d223627f80cb7a1eb14c585fe6cb35de5e984
>
--- Thanks!
"I'm not a very positive person" - Linus torvalds