Re: [PATCH v3 06/20] sched/core: allow only preferred CPUs in is_cpu_allowed

From: Shrikanth Hegde

Date: Thu Jun 04 2026 - 02:00:10 EST




On 6/4/26 10:36 AM, K Prateek Nayak wrote:
Hello Shrikanth,

On 5/14/2026 8:51 PM, Shrikanth Hegde wrote:
+ /*
+ * This is essential to maintain user affinities when preferred
+ * CPUs change. A task pinned on non-preferred CPU should continue
+ * to run there, since this is non-user triggered.
+ *
+ * For majority of the cases this would still keep select_fallback_rq
+ * as O(N). task_has_preferred_cpus which is O(N) is called only if
+ * !cpu_preferred. Then task running there is expected to move out.
+ * So subsequent it should run on preferred CPU. This becomes O(N**2)
+ * only for tasks pinned only non preferred CPUs. That is rare case.
+ */
+ task_has_preferred_cpu = !cpu_preferred(cpu) && task_has_preferred_cpus(p);

For this O(N**2) bit, can't we cache the result of cpumask_intersect()
in the task_struct similar to "p->sched_task_hot" when entering
select_fallback_rq() and clear it at the end?


Ok. Let me try.

+
/* Non kernel threads are not allowed during either online or offline. */
- if (!(p->flags & PF_KTHREAD))
- return cpu_active(cpu);
+ if (!(p->flags & PF_KTHREAD)) {
+ if (!cpu_active(cpu))
+ return false;
+ if (task_has_preferred_cpu)
+ return false;

Shouldn't this return true at the end here for
cpu_active() && !task_has_preferred_cpu?

Essentially, this:

diff --git a/kernel/sched/core.c b/kernel/sched/core.c
index 77a172f31862..3bf7e3c4d518 100644
--- a/kernel/sched/core.c
+++ b/kernel/sched/core.c
@@ -2489,6 +2489,7 @@ static inline bool is_cpu_allowed(struct task_struct *p, int cpu)
return false;
if (task_has_preferred_cpu)
return false;
+ return true;
}

Yes. Good catch. I missed it. it would have returned cpu_online()

/* KTHREAD_IS_PER_CPU is always allowed. */
---

Otherwise, it starts evaluating kthread bits for user tasks.

+ }
/* KTHREAD_IS_PER_CPU is always allowed. */
if (kthread_is_per_cpu(p))