sched/fair: Skip sched-domain traversal when wake affine is disabled
PATCH 1/1 sched/fair: Skip sched-domain traversal when wake affine is disabled
In select_task_rq_fair(), TTWU wakeups for which wake_wide() returns true, or where the waking CPU is not in the task's allowed CPU mask, want_affine is set to false.
For these wakeups, the scheduler does not perform wake-affine placement. The existing code already treats this as a fast-path by exiting the sched-domain walk before locating an SD_BALANCE_WAKE domain, ultimately falling through to select_idle_sibling().
However, select_task_rq_fair() still enters the sched-domain traversal only to break out almost immediately. Avoid this unnecessary work by jumping directly to the existing fast path when want_affine is false.
As part of this cleanup:
- Move want_affine into the WF_TTWU block where it is used.
- Skip the sched-domain traversal entirely when want_affine is false.
- Remove the now redundant want_affine test from the SD_WAKE_AFFINE check.
- Remove the corresponding !want_affine loop termination logic.
This preserves existing wakeup placement behavior while avoiding unnecessary sched-domain iteration for TTWU wakeups that cannot make use of wake-affine placement.
--- kernel/sched/fair.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-)
kernel/sched/fair.c +6 -4
@@ -8832,7 +8832,6 @@ select_task_rq_fair(struct task_struct *p, int prev_cpu, int wake_flags)struct sched_domain *tmp, *sd =NULL ;int cpu =smp_processor_id ();int new_cpu = prev_cpu; -int want_affine =0 ;/* SD_flags and WF_flags share the first nibble */ int sd_flag = wake_flags &0xF ;
@@ -8841,6 +8840,8 @@ select_task_rq_fair(struct task_struct *p, int prev_cpu, int wake_flags)*/ lockdep_assert_held (&p->pi_lock);if (wake_flags &WF_TTWU ) { +int want_affine; +record_wakee (p); if ((wake_flags & WF_CURRENT_CPU) &&
@@ -8855,6 +8856,8 @@ select_task_rq_fair(struct task_struct *p, int prev_cpu, int wake_flags) } want_affine = !wake_wide (p) &&cpumask_test_cpu (cpu, p->cpus_ptr); +if (!want_affine) + goto fast_path; }for_each_domain (cpu, tmp) {
@@ -8862,7 +8865,7 @@ select_task_rq_fair(struct task_struct *p, int prev_cpu, int wake_flags)* If both 'cpu' and 'prev_cpu' are part of this domain, * cpu is a valid SD_WAKE_AFFINE target. */ -if (want_affine && (tmp->flags &SD_WAKE_AFFINE ) && +if ((tmp->flags &SD_WAKE_AFFINE ) &&cpumask_test_cpu (prev_cpu,sched_domain_span (tmp))) {if (cpu != prev_cpu) new_cpu =wake_affine (tmp, p, cpu, prev_cpu, sync);
@@ -8878,8 +8881,6 @@ select_task_rq_fair(struct task_struct *p, int prev_cpu, int wake_flags)*/ if (tmp->flags & sd_flag) sd = tmp; -else if (!want_affine) -break ; }/* Slow path */
@@ -8887,6 +8888,7 @@ select_task_rq_fair(struct task_struct *p, int prev_cpu, int wake_flags)return sched_balance_find_dst_cpu (sd, p, cpu, prev_cpu, sd_flag);/* Fast path */ +fast_path:if (wake_flags &WF_TTWU )return select_idle_sibling (p, prev_cpu, new_cpu);
Signature · 1 line
2.43.0
Hi!
Quoted 8 lines · Srikar Dronamraju
In
select_task_rq_fair(), TTWU wakeups for whichwake_wide()returns true, or where the waking CPU is not in the task's allowed CPU mask,want_affineis set to false.For these wakeups, the scheduler does not perform wake-affine placement. The existing code already treats this as a fast-path by exiting the sched-domain walk before locating an
SD_BALANCE_WAKEdomain, ultimately falling through toselect_idle_sibling().
Not before locating. If it doesn't locates SD_BALANCE_WAKE in the first check it just leaves.
AFAICT SD_BALANCE_WAKE is not set anywhere tho.
So if you wanna do this, maybe you can do something like:
kernel/sched/fair.c +3 -0
@@ -10016,6 +10016,9 @@ select_task_rq_fair(struct task_struct *p, int prev_cpu, int wake_flags) } want_affine = !wake_wide (p) &&cpumask_test_cpu (cpu, p->cpus_ptr); + +if (!want_affine) +return select_idle_sibling (p, prev_cpu, new_cpu); }for_each_domain (cpu, tmp) {
Quoted 62 lines
However,
select_task_rq_fair()still enters the sched-domain traversal only to break out almost immediately. Avoid this unnecessary work by jumping directly to the existing fast path whenwant_affineis false.As part of this cleanup:
- Move
want_affineinto theWF_TTWUblock where it is used.
- Skip the sched-domain traversal entirely whenwant_affineis false.
- Remove the now redundantwant_affinetest from theSD_WAKE_AFFINEcheck.
- Remove the corresponding !want_affine loop termination logic.This preserves existing wakeup placement behavior while avoiding unnecessary sched-domain iteration for TTWU wakeups that cannot make use of wake-affine placement.
Signed-off-by Srikar Dronamraju <[email protected]>--- kernel/sched/fair.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-)kernel/sched/fair.c +5
-2@@ -8832,7 +8832,6 @@ select_task_rq_fair(struct task_struct *p, int prev_cpu, int wake_flags)struct sched_domain *tmp, *sd =NULL ;int cpu =smp_processor_id ();int new_cpu = prev_cpu; -int want_affine =0 ;/* SD_flags and WF_flags share the first nibble */ int sd_flag = wake_flags &0xF ;@@ -8841,6 +8840,8 @@ select_task_rq_fair(struct task_struct *p, int prev_cpu, int wake_flags)*/ lockdep_assert_held (&p->pi_lock);if (wake_flags &WF_TTWU ) { +int want_affine; +record_wakee (p); if ((wake_flags & WF_CURRENT_CPU) &&@@ -8855,6 +8856,8 @@ select_task_rq_fair(struct task_struct *p, int prev_cpu, int wake_flags) } want_affine = !wake_wide (p) &&cpumask_test_cpu (cpu, p->cpus_ptr); +if (!want_affine) + goto fast_path; }for_each_domain (cpu, tmp) {@@ -8862,7 +8865,7 @@ select_task_rq_fair(struct task_struct *p, int prev_cpu, int wake_flags)* If both 'cpu' and 'prev_cpu' are part of this domain, * cpu is a valid SD_WAKE_AFFINE target. */ -if (want_affine && (tmp->flags &SD_WAKE_AFFINE ) && +if ((tmp->flags &SD_WAKE_AFFINE ) &&cpumask_test_cpu (prev_cpu,sched_domain_span (tmp))) {if (cpu != prev_cpu) new_cpu =wake_affine (tmp, p, cpu, prev_cpu, sync);
kernel/sched/fair.c select_task_rq_fair(struct task_struct *p, int prev_cpu, int wake_flags) · line 8884
Quoted 6 lines
kernel/sched/fair.c +0
-2@@ -8878,8 +8881,6 @@ select_task_rq_fair(struct task_struct *p, int prev_cpu, int wake_flags)*/ if (tmp->flags & sd_flag) sd = tmp; -else if (!want_affine) -break ; }
This just changes the behavior.
Old Code WF_FORK 1, WF_TTWU 0:
want_affine = 0, so we skip the first if block in the loop then leave once we find a domain without our flag. And go with slow path.
New Code:
want_affine is moved away and early exit is removed. So we enter the if block and try to find a domain with prev_cpu. Because WF_TTWU is 0, we just return new_cpu. Or if we can't fimd the domain with prev_cpu, slow path.
Cheers,
Kayra
* Kayra Cizmeci <[email protected]> [2026-11-09 18:09:56]:
Hi Kayra,
Quoted 11 lines · Srikar Dronamraju
Quoted 8 lines
In
select_task_rq_fair(), TTWU wakeups for whichwake_wide()returns true, or where the waking CPU is not in the task's allowed CPU mask,want_affineis set to false.For these wakeups, the scheduler does not perform wake-affine placement. The existing code already treats this as a fast-path by exiting the sched-domain walk before locating an
SD_BALANCE_WAKEdomain, ultimately falling through toselect_idle_sibling().
Quoted 4 lines
Not before locating. If it doesn't locates
SD_BALANCE_WAKEin the first check it just leaves.AFAICT
SD_BALANCE_WAKEis not set anywhere tho.
from kernel/sched/sched.h
#define WF_TTWU 0x08 /* Wakeup; maps to SD_BALANCE_WAKE *
The code searches for WF_TTWU in lieu of SD_BALANCE_WAKE.
Quoted 8 lines
So if you wanna do this, maybe you can do something like:
kernel/sched/fair.c +0
-0
kernel/sched/fair.c select_task_rq_fair(struct task_struct *p, int prev_cpu, int wake_flags) · line 10022
Quoted 7 lines
kernel/sched/fair.c +3
-0@@ -10016,6 +10016,9 @@ select_task_rq_fair(struct task_struct *p, int prev_cpu, int wake_flags) } want_affine = !wake_wide (p) &&cpumask_test_cpu (cpu, p->cpus_ptr); + +if (!want_affine) +return select_idle_sibling (p, prev_cpu, new_cpu); }
Yes, this also has the same effect.
Quoted 81 lines
kernel/sched/fair.c +0
-0for_each_domain (cpu, tmp) {Quoted 62 lines
However,
select_task_rq_fair()still enters the sched-domain traversal only to break out almost immediately. Avoid this unnecessary work by jumping directly to the existing fast path whenwant_affineis false.As part of this cleanup:
- Move
want_affineinto theWF_TTWUblock where it is used.
- Skip the sched-domain traversal entirely whenwant_affineis false.
- Remove the now redundantwant_affinetest from theSD_WAKE_AFFINEcheck.
- Remove the corresponding !want_affine loop termination logic.This preserves existing wakeup placement behavior while avoiding unnecessary sched-domain iteration for TTWU wakeups that cannot make use of wake-affine placement.
Signed-off-by Srikar Dronamraju <[email protected]>--- kernel/sched/fair.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-)kernel/sched/fair.c +5
-2@@ -8832,7 +8832,6 @@ select_task_rq_fair(struct task_struct *p, int prev_cpu, int wake_flags)struct sched_domain *tmp, *sd =NULL ;int cpu =smp_processor_id ();int new_cpu = prev_cpu; -int want_affine =0 ;/* SD_flags and WF_flags share the first nibble */ int sd_flag = wake_flags &0xF ;@@ -8841,6 +8840,8 @@ select_task_rq_fair(struct task_struct *p, int prev_cpu, int wake_flags)*/ lockdep_assert_held (&p->pi_lock);if (wake_flags &WF_TTWU ) { +int want_affine; +record_wakee (p); if ((wake_flags & WF_CURRENT_CPU) &&@@ -8855,6 +8856,8 @@ select_task_rq_fair(struct task_struct *p, int prev_cpu, int wake_flags) } want_affine = !wake_wide (p) &&cpumask_test_cpu (cpu, p->cpus_ptr); +if (!want_affine) + goto fast_path; }for_each_domain (cpu, tmp) {@@ -8862,7 +8865,7 @@ select_task_rq_fair(struct task_struct *p, int prev_cpu, int wake_flags)* If both 'cpu' and 'prev_cpu' are part of this domain, * cpu is a valid SD_WAKE_AFFINE target. */ -if (want_affine && (tmp->flags &SD_WAKE_AFFINE ) && +if ((tmp->flags &SD_WAKE_AFFINE ) &&cpumask_test_cpu (prev_cpu,sched_domain_span (tmp))) {if (cpu != prev_cpu) new_cpu =wake_affine (tmp, p, cpu, prev_cpu, sync);kernel/sched/fair.c select_task_rq_fair(struct task_struct *p, int prev_cpu, int wake_flags) · line 8884
Quoted 6 lines
kernel/sched/fair.c +0
-2@@ -8878,8 +8881,6 @@ select_task_rq_fair(struct task_struct *p, int prev_cpu, int wake_flags)*/ if (tmp->flags & sd_flag) sd = tmp; -else if (!want_affine) -break ; }This just changes the behavior.
Old Code
WF_FORK1,WF_TTWU0:
want_affine= 0, so we skip the first if block in the loop then leave once we find a domain without our flag. And go with slow path.
Quoted 7 lines
New Code:
want_affineis moved away and early exit is removed. So we enter the if block and try to find a domain withprev_cpu. BecauseWF_TTWUis 0, we just returnnew_cpu. Or if we can't fimd the domain withprev_cpu, slow path.
When WF_TTWU is set, we are always suppose take the fast path. slowpath is for WF_FORK and WF_EXEC.
Quoted 4 lines
Cheers,
Kayra
Signature · 2 lines
Thanks and Regards Srikar Dronamraju
Quoted 15 lines · Srikar Dronamraju
* Kayra Cizmeci <[email protected]> [2026-11-09 18:09:56]:
Hi Kayra,
Quoted 11 lines · Srikar Dronamraju
Quoted 8 lines
In
select_task_rq_fair(), TTWU wakeups for whichwake_wide()returns true, or where the waking CPU is not in the task's allowed CPU mask,want_affineis set to false.For these wakeups, the scheduler does not perform wake-affine placement. The existing code already treats this as a fast-path by exiting the sched-domain walk before locating an
SD_BALANCE_WAKEdomain, ultimately falling through toselect_idle_sibling().
Quoted 8 lines
Quoted 4 lines
Not before locating. If it doesn't locates
SD_BALANCE_WAKEin the first check it just leaves.AFAICT
SD_BALANCE_WAKEis not set anywhere tho.from
kernel/sched/sched.h
#defineWF_TTWU0x08/* Wakeup; maps toSD_BALANCE_WAKE*
The code searches forWF_TTWUin lieu ofSD_BALANCE_WAKE.
Yeah I know. But I couldn't find a place where SD_BALANCE_WAKE gets set in the domain.
Quoted 102 lines
Quoted 9 lines
So if you wanna do this, maybe you can do something like:
kernel/sched/fair.c +0
-0kernel/sched/fair.c select_task_rq_fair(struct task_struct *p, int prev_cpu, int wake_flags) · line 10022
Quoted 7 lines
kernel/sched/fair.c +3
-0@@ -10016,6 +10016,9 @@ select_task_rq_fair(struct task_struct *p, int prev_cpu, int wake_flags) } want_affine = !wake_wide (p) &&cpumask_test_cpu (cpu, p->cpus_ptr); + +if (!want_affine) +return select_idle_sibling (p, prev_cpu, new_cpu); }Yes, this also has the same effect.
Quoted 83 lines
kernel/sched/fair.c +0
-0for_each_domain (cpu, tmp) {Quoted 62 lines
However,
select_task_rq_fair()still enters the sched-domain traversal only to break out almost immediately. Avoid this unnecessary work by jumping directly to the existing fast path whenwant_affineis false.As part of this cleanup:
- Move
want_affineinto theWF_TTWUblock where it is used.
- Skip the sched-domain traversal entirely whenwant_affineis false.
- Remove the now redundantwant_affinetest from theSD_WAKE_AFFINEcheck.
- Remove the corresponding !want_affine loop termination logic.This preserves existing wakeup placement behavior while avoiding unnecessary sched-domain iteration for TTWU wakeups that cannot make use of wake-affine placement.
Signed-off-by Srikar Dronamraju <[email protected]>--- kernel/sched/fair.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-)kernel/sched/fair.c +5
-2@@ -8832,7 +8832,6 @@ select_task_rq_fair(struct task_struct *p, int prev_cpu, int wake_flags)struct sched_domain *tmp, *sd =NULL ;int cpu =smp_processor_id ();int new_cpu = prev_cpu; -int want_affine =0 ;/* SD_flags and WF_flags share the first nibble */ int sd_flag = wake_flags &0xF ;@@ -8841,6 +8840,8 @@ select_task_rq_fair(struct task_struct *p, int prev_cpu, int wake_flags)*/ lockdep_assert_held (&p->pi_lock);if (wake_flags &WF_TTWU ) { +int want_affine; +record_wakee (p); if ((wake_flags & WF_CURRENT_CPU) &&@@ -8855,6 +8856,8 @@ select_task_rq_fair(struct task_struct *p, int prev_cpu, int wake_flags) } want_affine = !wake_wide (p) &&cpumask_test_cpu (cpu, p->cpus_ptr); +if (!want_affine) + goto fast_path; }for_each_domain (cpu, tmp) {@@ -8862,7 +8865,7 @@ select_task_rq_fair(struct task_struct *p, int prev_cpu, int wake_flags)* If both 'cpu' and 'prev_cpu' are part of this domain, * cpu is a valid SD_WAKE_AFFINE target. */ -if (want_affine && (tmp->flags &SD_WAKE_AFFINE ) && +if ((tmp->flags &SD_WAKE_AFFINE ) &&cpumask_test_cpu (prev_cpu,sched_domain_span (tmp))) {if (cpu != prev_cpu) new_cpu =wake_affine (tmp, p, cpu, prev_cpu, sync);kernel/sched/fair.c select_task_rq_fair(struct task_struct *p, int prev_cpu, int wake_flags) · line 8884
Quoted 6 lines
kernel/sched/fair.c +0
-2@@ -8878,8 +8881,6 @@ select_task_rq_fair(struct task_struct *p, int prev_cpu, int wake_flags)*/ if (tmp->flags & sd_flag) sd = tmp; -else if (!want_affine) -break ; }This just changes the behavior.
Old Code
WF_FORK1,WF_TTWU0:
want_affine= 0, so we skip the first if block in the loop then leave once we find a domain without our flag. And go with slow path.New Code:
Quoted 8 lines
Quoted 5 lines
want_affineis moved away and early exit is removed. So we enter the if block and try to find a domain withprev_cpu. BecauseWF_TTWUis 0, we just returnnew_cpu. Or if we can't fimd the domain withprev_cpu, slow path.When
WF_TTWUis set, we are always suppose take the fast path. slowpath is forWF_FORKandWF_EXEC.
I know. And that is the exact problem with the above diff :-).
Like let's say prev_cpu is in the first domain we are looking, the domain has SD_WAKE_AFFINE and SD_BALANCE_FORK set and next domain has only SD_WAKE_AFFINE set:
Old Code:
sd = first domain and we enter the slow path with the first domain.
With your patch:
sd = NULL and we return new_cpu
Or am I missing something?
Cheers,
Kayra
Quoted 38 lines · Kayra Cizmeci
Quoted 22 lines · Srikar Dronamraju
* Kayra Cizmeci <[email protected]> [2026-11-09 18:09:56]:
Hi Kayra,
Quoted 18 lines · Srikar Dronamraju
Quoted 4 lines
In
select_task_rq_fair(), TTWU wakeups for whichwake_wide()returns true, or where the waking CPU is not in the task's allowed CPU mask,want_affineis set to false.This just changes the behavior.
Old Code
WF_FORK1,WF_TTWU0:
want_affine= 0, so we skip the first if block in the loop then leave once we find a domain without our flag. And go with slow path.New Code:
Quoted 8 lines
Quoted 5 lines
want_affineis moved away and early exit is removed. So we enter the if block and try to find a domain withprev_cpu. BecauseWF_TTWUis 0, we just returnnew_cpu. Or if we can't fimd the domain withprev_cpu, slow path.When
WF_TTWUis set, we are always suppose take the fast path. slowpath is forWF_FORKandWF_EXEC.I know. And that is the exact problem with the above diff :-).
Like let's say
prev_cpuis in the first domain we are looking, the domain hasSD_WAKE_AFFINEandSD_BALANCE_FORKset and next domain has onlySD_WAKE_AFFINEset:
Quoted 8 lines
Old Code:
sd = first domain and we enter the slow path with the first domain.
With your patch:
sd = NULL and we return
new_cpu
Oh thats a fair point, thanks for the reviewing it.
Quoted 6 lines
Or am I missing something?
Cheers,
Kayra
Signature · 2 lines
Thanks and Regards Srikar Dronamraju
kernel test robot
build botkernel test robot noticed a 23.8% improvement of autonuma-benchmark.numa02.seconds on:
Hello,
kernel test robot noticed a 23.8% improvement of autonuma-benchmark.numa02.seconds on:
commit: d9f685e0573ed835e37d8053690c646cff017954 ("[PATCH] sched/fair: Skip sched-domain traversal when wake affine is disabled")
url: https://github.com/intel-lab-lkp/linux/commits/Srikar-Dronamraju/sched-fair-Skip-sched-domain-traversal-when-wake-affine-is-disabled/20261109-173522
base: v7.3
patch link: https://lore.kernel.org/all/[email protected]/
patch subject: [PATCH] sched/fair: Skip sched-domain traversal when wake affine is disabled
testcase: autonuma-benchmark
config: x86_64-rhel-9.4
compiler: gcc-14
test machine: 224 threads 2 sockets Intel(R) Xeon(R) Platinum 8480CTDX (Sapphire Rapids) with 256G memory parameters:
iterations: 1x
test: numa01_THREAD_ALLOC
cpufreq_governor: performance
Details are as below:
-------------------------------------------------------------------------------------------------->
The kernel config and materials to reproduce are available at:
https://download.01.org/0day-ci/archive/20261123/[email protected]
=========================================================================================
compiler/cpufreq_governor/iterations/kconfig/rootfs/tbox_group/test/testcase:
gcc-14/performance/1x/x86_64-rhel-9.4/debian-13-x86_64-20250902.cgz/lkp-spr-r02/numa01_THREAD_ALLOC/autonuma-benchmark
commit:
v7.3
d9f685e057 ("[PATCH] sched/fair: Skip sched-domain traversal when wake affine is disabled")
v7.3 d9f685e0573ed835e37d8053690
---------------- ---------------------------
%stddev %change %stddev
\ | \
8.24 -23.8% 6.28 ± 17% autonuma-benchmark.numa02.seconds
1115596 ± 4% -17.1% 924791 ± 6% autonuma-benchmark.time.minor_page_faults
20753 -1.8% 20388 autonuma-benchmark.time.percent_of_cpu_this_job_got
1387 ± 7% -42.8% 794.05 ± 33% autonuma-benchmark.time.system_timeDisclaimer:
Results have been estimated based on internal Intel analysis and are provided for informational purposes only. Any difference in system hardware or software design or configuration may affect actual performance.
Signature · 2 lines
0-DAY CI Kernel Test Service https://github.com/intel/lkp-tests/wiki