Re: [PATCH] kthread: fall back when no preferred housekeeping CPU is online

From: Frederic Weisbecker

Date: Thu Oct 22 2026 - 09:02:52 EST


Le Tue, Oct 20, 2026 at 03:55:18PM +0300, Avi Weiss a écrit :
> 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))
> cpumask_copy(cpumask, housekeeping_cpumask(HK_TYPE_DOMAIN));
> }

Good catch thank you! Queueing it.



>
>
> base-commit: f47d223627f80cb7a1eb14c585fe6cb35de5e984
> --
> 2.43.0
>

--
Frederic Weisbecker
SUSE Labs