Re: [PATCH v6 05/23] sched/core: Try to use a preferred CPU in is_cpu_allowed
From: Shrikanth Hegde
Date: Thu Jul 02 2026 - 02:31:32 EST
On 7/1/26 10:19 PM, Shrikanth Hegde wrote:
Hi Yury,
[...]
The is_cpu_allowed() is ~20 lines now, and your patch doubles that count.
Can you keep this type of thoughts in commit message? 90% of setups
will disable preferred CPUs, and I guess 99% of developers don't care.
This is the code, not a scientific paper, after all.
Ok. I will update the comments and share updated one soon
as reply to this.
Made it as below. This looks much cleaner and retains the same
functionality. Reduces cache window for kthread too. No double
cost due to earlier returns.
Does this look sane?
/*
* Per-CPU kthreads are allowed to run on !active && online CPUs, see
* __set_cpus_allowed_ptr() and select_fallback_rq().
*/
static inline bool is_cpu_allowed(struct task_struct *p, int cpu)
{
/* When not in the task's cpumask, no point in looking further. */
if (!task_allowed_on_cpu(p, cpu))
return false;
/* migrate_disabled() must be allowed to finish. */
if (is_migration_disabled(p))
return cpu_online(cpu);
/* Non kernel threads are not allowed during either online or offline. */
if (!(p->flags & PF_KTHREAD)) {
/* Try to use preferred CPU if task's affinity allows */
if (task_can_sched_on_preferred(cpu, p))
return false;
return cpu_active(cpu);
}
/* KTHREAD_IS_PER_CPU is always allowed. */
if (kthread_is_per_cpu(p))
return cpu_online(cpu);
/* Regular kernel threads don't get to stay during offline. */
if (cpu_dying(cpu))
return false;
/* Try to keep unbound kthreads on a preferred CPU if possible. */
if (task_can_sched_on_preferred(cpu, p))
return false;
/* Otherwise, they are allowed to run on online CPU. */
return cpu_online(cpu);
}
and
static inline bool task_can_sched_on_preferred(int cpu, struct task_struct *p)
{
if (cpu_preferred(cpu))
return false;
/* Only FAIR tasks honor preferred CPU state */
if (unlikely(p->sched_class != &fair_sched_class))
return false;
return cpumask_intersects(p->cpus_ptr, cpu_preferred_mask);
}