From: Shrikanth Hegde <sshegde@linux.ibm.com>
To: Yury Norov <yury.norov@gmail.com>, kprateek.nayak@amd.com
Cc: linux-kernel@vger.kernel.org, mingo@kernel.org,
peterz@infradead.org, juri.lelli@redhat.com,
vincent.guittot@linaro.org, iii@linux.ibm.com, corbet@lwn.net,
tglx@kernel.org, gregkh@linuxfoundation.org, pbonzini@redhat.com,
seanjc@google.com, vschneid@redhat.com, huschle@linux.ibm.com,
rostedt@goodmis.org, dietmar.eggemann@arm.com,
maddy@linux.ibm.com, srikar@linux.ibm.com, hdanton@sina.com,
chleroy@kernel.org, vineeth@bitbyteword.org, frederic@kernel.org,
arighi@nvidia.com, pauld@redhat.com, christian.loehle@arm.com,
tj@kernel.org, tommaso.cucinotta@gmail.com, maz@kernel.org,
rafael@kernel.org, rdunlap@infradead.org, kernellwp@gmail.com,
linux-doc@vger.kernel.org
Subject: Re: [PATCH v5 06/24] sched/core: allow only preferred CPUs in is_cpu_allowed
Date: Tue, 30 Jun 2026 11:55:59 +0530 [thread overview]
Message-ID: <b91782dc-cde0-4291-949d-0cc804c1f477@linux.ibm.com> (raw)
In-Reply-To: <e6570eac-e675-4b9b-8d1b-516965cd2207@linux.ibm.com>
Hi Yury, Prateek,
On 6/29/26 9:44 AM, Shrikanth Hegde wrote:
> Hi Yury.
>
>> Just as said on previous round. Please order your series such that the
>> core logic goes first, and all sorts of complications, like this
>> optimization, are appended at the end.
>
> Ok. I will split it up into two patches.
>
> One without any optimization but with comment explaining the rare case
> of N**2.
> Second one at the end of the series, with a patch do the optimization.
>
>>
>>> ---
>>>
>>> diff --git a/kernel/sched/core.c b/kernel/sched/core.c
>>> index 9e16946c9d62..fafedd52611f 100644
>>> --- a/kernel/sched/core.c
>>> +++ b/kernel/sched/core.c
>>> @@ -2498,8 +2498,10 @@ static inline bool rq_has_pinned_tasks(struct
>>> rq *rq)
>>> * 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)
>>> +static inline bool is_cpu_allowed(struct task_struct *p, int cpu,
>>> int cached)
>>> {
>>> + bool task_check_preferred_cpu;
>>> +
>>> /* When not in the task's cpumask, no point in looking
>>> further. */
>>> if (!task_allowed_on_cpu(p, cpu))
>>> return false;
>>> @@ -2508,9 +2510,24 @@ static inline bool is_cpu_allowed(struct
>>> task_struct *p, int cpu)
>>> if (is_migration_disabled(p))
>>> return cpu_online(cpu);
>>> + /*
>>> + * 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.
>>> + *
>>> + * If CPU is non-preferred and task can run on other CPUs
>>> which are
>>> + * currently preferred, then choose those other CPUs instead.
>>> + * Overhead is minimal when CPU is preferred.
>>> + */
>>> + task_check_preferred_cpu = !cpu_preferred(cpu) &&
>>> + task_has_preferred_cpus(p, cached);
>>> +
>>> /* Non kernel threads are not allowed during either online
>>> or offline. */
>>> - if (!(p->flags & PF_KTHREAD))
>>> + if (!(p->flags & PF_KTHREAD)) {
>>> + if (task_check_preferred_cpu)
>>> + return false;
>>> return cpu_active(cpu);
>>> + }
>>> /* KTHREAD_IS_PER_CPU is always allowed. */
>>> if (kthread_is_per_cpu(p))
>>> @@ -2520,6 +2537,10 @@ static inline bool is_cpu_allowed(struct
>>> task_struct *p, int cpu)
>>> if (cpu_dying(cpu))
>>> return false;
>>> + /* Try on preferred CPU first if possible*/
>>> + if (task_check_preferred_cpu)
>>> + return false;
>>> +
>>> /* But are allowed during online. */
>>> return cpu_online(cpu);
>>> }
>>> @@ -2595,7 +2616,7 @@ static struct rq *__migrate_task(struct rq *rq,
>>> struct rq_flags *rf,
>>> __must_hold(__rq_lockp(rq))
>>> {
>>> /* Affinity changed (again). */
>>> - if (!is_cpu_allowed(p, dest_cpu))
>>> + if (!is_cpu_allowed(p, dest_cpu, NO_CACHED_VAL))
>>> return rq;
>>
>> This thing I really dislike. The unrelated code should not be
>> affected. You can make it less visually invasive with:
>> #define is_cpu_allowed(p, cpu) __is_cpu_allowed(p, cpu,
>> NO_CACHED_VAL)
>>
>> Please reconsider your code to have the changes better localized.
>>
>> Thanks,
>> Yury
>>
>
> That was typed out too fast. I did refactor something like that later.
> But i will split this into twp patches as said above.
>
>
> diff --git a/kernel/sched/core.c b/kernel/sched/core.c
> index 9e16946c9d62..a1b21c21aa9c 100644
> --- a/kernel/sched/core.c
> +++ b/kernel/sched/core.c
> @@ -2498,8 +2498,11 @@ static inline bool rq_has_pinned_tasks(struct rq
> *rq)
> * 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)
> +static inline bool __is_cpu_allowed(struct task_struct *p, int cpu,
> + int pref_state)
> {
> + bool task_check_preferred_cpu;
> +
> /* When not in the task's cpumask, no point in looking further. */
> if (!task_allowed_on_cpu(p, cpu))
> return false;
> @@ -2508,9 +2511,24 @@ static inline bool is_cpu_allowed(struct
> task_struct *p, int cpu)
> if (is_migration_disabled(p))
> return cpu_online(cpu);
>
> + /*
> + * 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.
> + *
> + * If CPU is non-preferred and task can run on other CPUs which are
> + * currently preferred, then choose those other CPUs instead.
> + * Overhead is minimal when CPU is preferred.
> + */
> + task_check_preferred_cpu = !cpu_preferred(cpu) &&
> + task_has_preferred_cpus(p, pref_state);
> +
> /* Non kernel threads are not allowed during either online or
> offline. */
> - if (!(p->flags & PF_KTHREAD))
> + if (!(p->flags & PF_KTHREAD)) {
> + if (task_check_preferred_cpu)
> + return false;
> return cpu_active(cpu);
> + }
>
> /* KTHREAD_IS_PER_CPU is always allowed. */
> if (kthread_is_per_cpu(p))
> @@ -2520,10 +2538,19 @@ static inline bool is_cpu_allowed(struct
> task_struct *p, int cpu)
> if (cpu_dying(cpu))
> return false;
>
> + /* Try on preferred CPU first if possible*/
> + if (task_check_preferred_cpu)
> + return false;
> +
> /* But are allowed during online. */
> return cpu_online(cpu);
> }
>
> +static inline bool is_cpu_allowed(struct task_struct *p, int cpu)
> +{
> + return __is_cpu_allowed(p, cpu, PREFERRED_CPU_UNKNOWN);
> +}
> +
> /*
> * This is how migration works:
> *
> @@ -3547,7 +3574,15 @@ static int select_fallback_rq(int cpu, struct
> task_struct *p)
> int nid = cpu_to_node(cpu);
> const struct cpumask *nodemask = NULL;
> enum { cpuset, possible, fail } state = cpuset;
> - int dest_cpu;
> + int dest_cpu, pref_state;
> +
> + /*
> + * Cache the value whether task's affinity spans preferred CPUs.
> + * This helps to avoid repeating the same for each CPU
> + * later in the loop.
> + */
> + pref_state = task_has_preferred_cpus(p, PREFERRED_CPU_UNKNOWN) ?
> + PREFERRED_CPU_EXISTS : PREFERRED_CPU_NONE;
>
> /*
> * If the node that the CPU is on has been offlined, cpu_to_node()
> @@ -3559,7 +3594,7 @@ static int select_fallback_rq(int cpu, struct
> task_struct *p)
>
> /* Look for allowed, online CPU in same node. */
> for_each_cpu(dest_cpu, nodemask) {
> - if (is_cpu_allowed(p, dest_cpu))
> + if (__is_cpu_allowed(p, dest_cpu, pref_state))
> return dest_cpu;
> }
> }
> @@ -3567,7 +3602,7 @@ static int select_fallback_rq(int cpu, struct
> task_struct *p)
> for (;;) {
> /* Any allowed, online CPU? */
> for_each_cpu(dest_cpu, p->cpus_ptr) {
> - if (!is_cpu_allowed(p, dest_cpu))
> + if (!__is_cpu_allowed(p, dest_cpu, pref_state))
> continue;
>
> goto out;
> diff --git a/kernel/sched/sched.h b/kernel/sched/sched.h
> index c7c2dea65edd..6a352d235503 100644
> --- a/kernel/sched/sched.h
> +++ b/kernel/sched/sched.h
> @@ -4213,4 +4213,33 @@ DEFINE_CLASS_IS_UNCONDITIONAL(sched_change)
>
> #include "ext.h"
>
> +/*
> + * PREFERRED_CPU_UNKNOWN: Unknown and need to evaluate.
> + * PREFERRED_CPU_NONE : Known and No preferred CPUs exists in task's
> affinity.
> + * PREFERRED_CPU_EXISTS: Known and preferred CPU exists in task's
> affinity.
> + */
> +
> +enum task_preferred_cached {
> + PREFERRED_CPU_UNKNOWN,
> + PREFERRED_CPU_NONE,
> + PREFERRED_CPU_EXISTS,
> +};
> +
> +/*
> + * Value is known when called via select_fallback_rq(). This helps to
> + * avoid calling cpumask_intersects repeatedly in the loop.
> + *
> + * Only affects FAIR task.
> + */
> +static inline bool task_has_preferred_cpus(struct task_struct *p, int
> pref_state)
> +{
> + /* Only FAIR tasks honor preferred CPU state */
> + if (unlikely(p->sched_class != &fair_sched_class))
> + return false;
> +
> + if (pref_state != PREFERRED_CPU_UNKNOWN)
> + return pref_state == PREFERRED_CPU_EXISTS;
> +
> + return cpumask_intersects(p->cpus_ptr, cpu_preferred_mask);
> +}
> #endif /* _KERNEL_SCHED_SCHED_H */
>
I was thinking caching the value can cause affinity to be reset,
because there is no protection of mask change within the loop of
select_fallback_rq. So i did some testing, and
I hit a case in practice with 10ms as interval with explicit
affinities on overlapping preferred and non-preferred CPUs and hotplugging
specific CPUs. caching exposes this race
Lets say task affined to 464-479 and preferred mask is 0-471 (472-479 is non preferred)
CPU468 CPU0
select_fallback_rq
- pref_state = PREFERRED_CPU_EXISTS
(Now preferred_mask becomes 0-463, but (Changes preferred to 0-463)
before any further call to is_cpu_allowed
is made)
- is_cpu_allowed tries to find a preferred
CPU since cached state says one exists.
- is_cpu_allowed is called twice (once on nodemask,
and once on p->cpus_ptr) but cached state remains the
same.
- no CPU found, fallback to reset to possible CPUs.
Without cached state, there is evaluation on each !preferred CPU
and such race isn;t possible between two calls. So chance of race
is extremely rare if non-existent.
I couldn't hit the same race in any permutations I tried.
Even if we take task was affined to only one CPU and mask changed between &&.
!cpu_preferred(cpu) && task_has_preferred_cpus(p);
Two cases.
Case 1: cpu was marked as preferred and after && it got removed from preferred_mask.
In that case task may end up on non-preferred CPU and it gets pushed out if possible.
No reset of its affinity.
Case 2: cpu was non-preferred and now it became preferred after && Now cpumask_intersects
will be true true and task_has_preferred_cpus is true as well, but this CPU will be skipped.
But second call in select_fallback_rq will ensure it returns the since cpu_preferred check
will succeed. Since select_fallback_rq is called on task_cpu(p) where it previously ran,
first check may fail due to race, but second one can't since evaluation between the two can't
be more than 1ms.
* So I will drop this optimization of caching the state for now *
next prev parent reply other threads:[~2026-06-30 6:26 UTC|newest]
Thread overview: 49+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-06-25 12:46 [PATCH v5 00/24] sched: Introduce cpu_preferred_mask and steal-driven vCPU backoff Shrikanth Hegde
2026-06-25 12:46 ` [PATCH v5 01/24] sched/debug: Remove unused schedstats Shrikanth Hegde
2026-06-25 12:46 ` [PATCH v5 02/24] sched/docs: Document cpu_preferred_mask and Preferred CPU concept Shrikanth Hegde
2026-06-25 12:46 ` [PATCH v5 03/24] kconfig: Provide PREFERRED_CPU option Shrikanth Hegde
2026-06-25 12:46 ` [PATCH v5 04/24] cpumask: Introduce cpu_preferred_mask Shrikanth Hegde
2026-06-26 9:34 ` Peter Zijlstra
2026-06-26 13:37 ` Shrikanth Hegde
2026-06-26 9:39 ` Peter Zijlstra
2026-06-26 9:41 ` Peter Zijlstra
2026-06-26 13:09 ` Shrikanth Hegde
2026-06-26 13:18 ` Yury Norov
2026-06-26 13:27 ` Shrikanth Hegde
2026-06-26 12:40 ` Yury Norov
2026-06-26 13:18 ` Shrikanth Hegde
2026-06-26 18:51 ` Shrikanth Hegde
2026-06-25 12:46 ` [PATCH v5 05/24] sysfs: Add preferred CPU file Shrikanth Hegde
2026-06-25 12:46 ` [PATCH v5 06/24] sched/core: allow only preferred CPUs in is_cpu_allowed Shrikanth Hegde
2026-06-26 13:06 ` Yury Norov
2026-06-26 13:25 ` Shrikanth Hegde
2026-06-26 18:43 ` Shrikanth Hegde
2026-06-29 4:09 ` Yury Norov
2026-06-29 4:14 ` Shrikanth Hegde
2026-06-30 6:25 ` Shrikanth Hegde [this message]
2026-06-25 12:46 ` [PATCH v5 07/24] sched/fair: Select preferred CPU at wakeup when possible Shrikanth Hegde
2026-06-26 9:59 ` Peter Zijlstra
2026-06-26 13:17 ` Shrikanth Hegde
2026-06-25 12:46 ` [PATCH v5 08/24] sched/fair: load balance only among preferred CPUs Shrikanth Hegde
2026-06-25 12:46 ` [PATCH v5 09/24] sched/fair: Pull the load on preferred CPU Shrikanth Hegde
2026-06-26 10:00 ` Peter Zijlstra
2026-06-26 13:35 ` Shrikanth Hegde
2026-06-25 12:46 ` [PATCH v5 10/24] sched/core: Keep tick on non-preferred CPUs until tasks are out Shrikanth Hegde
2026-06-25 12:46 ` [PATCH v5 11/24] sched/core: Push current task from non preferred CPU Shrikanth Hegde
2026-06-25 12:46 ` [PATCH v5 12/24] sched/debug: Add migration stats due to non preferred CPUs Shrikanth Hegde
2026-06-25 12:46 ` [PATCH v5 13/24] virt/steal_monitor: Add documentation Shrikanth Hegde
2026-06-25 17:00 ` Randy Dunlap
2026-06-26 4:30 ` Shrikanth Hegde
2026-06-26 9:28 ` Peter Zijlstra
2026-06-26 14:05 ` Shrikanth Hegde
2026-06-25 12:46 ` [PATCH v5 14/24] virt: Introduce steal monitor driver Shrikanth Hegde
2026-06-25 12:46 ` [PATCH v5 15/24] virt/steal_monitor: Restore to active on module disable Shrikanth Hegde
2026-06-25 12:46 ` [PATCH v5 16/24] virt/steal_monitor: Define steal_monitor structure Shrikanth Hegde
2026-06-25 12:46 ` [PATCH v5 17/24] virt/steal_monitor: Add control knobs for handling steal values Shrikanth Hegde
2026-06-25 12:46 ` [PATCH v5 18/24] virt/steal_monitor: Compute work at regular intervals Shrikanth Hegde
2026-06-25 12:46 ` [PATCH v5 19/24] virt/steal_monitor: Provide default method to get systemwide steal time Shrikanth Hegde
2026-06-25 12:46 ` [PATCH v5 20/24] virt/steal_monitor: Provide default method to inc/dec preferred CPUs Shrikanth Hegde
2026-06-25 12:46 ` [PATCH v5 21/24] virt/steal_monitor: Provide default method to get num of CPUs for steal ratio Shrikanth Hegde
2026-06-25 12:46 ` [PATCH v5 22/24] virt/steal_monitor: Act on steal values at regular intervals Shrikanth Hegde
2026-06-25 12:46 ` [PATCH v5 23/24] virt/steal_monitor: Add direction control Shrikanth Hegde
2026-06-25 12:46 ` [PATCH v5 24/24] virt/steal_monitor: Add design check of preferred subset of active Shrikanth Hegde
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=b91782dc-cde0-4291-949d-0cc804c1f477@linux.ibm.com \
--to=sshegde@linux.ibm.com \
--cc=arighi@nvidia.com \
--cc=chleroy@kernel.org \
--cc=christian.loehle@arm.com \
--cc=corbet@lwn.net \
--cc=dietmar.eggemann@arm.com \
--cc=frederic@kernel.org \
--cc=gregkh@linuxfoundation.org \
--cc=hdanton@sina.com \
--cc=huschle@linux.ibm.com \
--cc=iii@linux.ibm.com \
--cc=juri.lelli@redhat.com \
--cc=kernellwp@gmail.com \
--cc=kprateek.nayak@amd.com \
--cc=linux-doc@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=maddy@linux.ibm.com \
--cc=maz@kernel.org \
--cc=mingo@kernel.org \
--cc=pauld@redhat.com \
--cc=pbonzini@redhat.com \
--cc=peterz@infradead.org \
--cc=rafael@kernel.org \
--cc=rdunlap@infradead.org \
--cc=rostedt@goodmis.org \
--cc=seanjc@google.com \
--cc=srikar@linux.ibm.com \
--cc=tglx@kernel.org \
--cc=tj@kernel.org \
--cc=tommaso.cucinotta@gmail.com \
--cc=vincent.guittot@linaro.org \
--cc=vineeth@bitbyteword.org \
--cc=vschneid@redhat.com \
--cc=yury.norov@gmail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox