sched: small changes
Subsystems (2)
cover sched: small changes
These improments are found in the implementation of BPF-NUMAB series[0]. They can be applied seperately.
[0]. https://lwn.net/Articles/1101442/
Yafang Shao (2):
sched: fix stale get_pref_llc() stub for !CONFIG_SCHED_CACHE
sched: use static_branch_likely() for the NUMA balancing tick check
kernel/sched/fair.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
Signature · 1 line
2.52.0
PATCH 1/2 sched: fix stale get_pref_llc() stub for !CONFIG_SCHED_CACHE
Commit b636fef85bda ("sched/cache: Introduce task_struct->sched_cache_grp to fix UAF") switched get_pref_llc() from taking a struct mm_struct * to taking the task's struct sched_cache_group *, but only updated the CONFIG_SCHED_CACHE definition and its caller: the stub in the !CONFIG_SCHED_CACHE branch kept the old mm_struct parameter.
--- kernel/sched/fair.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/sched/fair.c +1 -1
@@ -2194,7 +2194,7 @@ void init_sched_mm(struct task_struct *p) { }static void task_tick_cache (struct rq *rq,struct task_struct *p) { }static inline int get_pref_llc (struct task_struct *p, -struct mm_struct *mm) +struct sched_cache_group *grp) {return -1 ; }
Signature · 1 line
2.52.0
[tip: sched/urgent] sched: fix stale get_pref_llc() stub for !CONFIG_SCHED_CACHE
The following commit has been merged into the sched/urgent branch of tip:
The following commit has been merged into the sched/urgent branch of tip:
Commit-ID: cac037a85c1971ec75c4d3edf5a28af9ec2a089c Gitweb: https://git.kernel.org/tip/cac037a85c1971ec75c4d3edf5a28af9ec2a089c Author: Yafang Shao <[email protected]> AuthorDate: Fri, 23 Oct 2026 10:31:04 +08:00 Committer: Peter Zijlstra <[email protected]> CommitterDate: Fri, 23 Oct 2026 19:09:54 +02:00
sched: fix stale get_pref_llc() stub for !CONFIG_SCHED_CACHE
Commit b636fef85bda ("sched/cache: Introduce task_struct->sched_cache_grp to fix UAF") switched get_pref_llc() from taking a struct mm_struct * to taking the task's struct sched_cache_group *, but only updated the CONFIG_SCHED_CACHE definition and its caller: the stub in the !CONFIG_SCHED_CACHE branch kept the old mm_struct parameter.
--- kernel/sched/fair.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/sched/fair.c +1 -1
@@ -2194,7 +2194,7 @@ void init_sched_mm(struct task_struct *p) { }static void task_tick_cache (struct rq *rq,struct task_struct *p) { }static inline int get_pref_llc (struct task_struct *p, -struct mm_struct *mm) +struct sched_cache_group *grp) {return -1 ; }
PATCH 2/2 sched: use static_branch_likely() for the NUMA balancing tick check
In reply to Yafang Shaotask_tick_fair() checks sched_numa_balancing on every tick. The static_branch_unlikely() annotation reserves the nop for the disabled case, so the default configuration (NUMA_BALANCING_DEFAULT_ENABLED=y) pays a taken jump per tick. Flip the polarity to static_branch_likely() so that the fast path matches the default.
--- kernel/sched/fair.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/sched/fair.c +1 -1
@@ -15351,7 +15351,7 @@ static void task_tick_fair(struct rq *rq, struct task_struct *curr, int queued)if (queued)return ; -if (static_branch_unlikely (&sched_numa_balancing)) +if (static_branch_likely (&sched_numa_balancing))task_tick_numa (rq, curr);task_tick_cache (rq, curr);
Signature · 1 line
2.52.0
Vincent Guittot
maintainerRe: [PATCH 2/2] sched: use static_branch_likely() for the NUMA balancing tick check
Quoted 18 lines · Yafang Shao
task_tick_fair()checkssched_numa_balancingon every tick. Thestatic_branch_unlikely()annotation reserves the nop for the disabled case, so the default configuration (NUMA_BALANCING_DEFAULT_ENABLED=y) pays a taken jump per tick. Flip the polarity tostatic_branch_likely()so that the fast path matches the default.Signed-off-by Yafang Shao <[email protected]>--- kernel/sched/fair.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)kernel/sched/fair.c +0
-0
kernel/sched/fair.c static void task_tick_fair(struct rq *rq, struct task_struct *curr, int queued) · line 15354
Quoted 5 lines
kernel/sched/fair.c +1
-1@@ -15351,7 +15351,7 @@ static void task_tick_fair(struct rq *rq, struct task_struct *curr, int queued)if (queued)return ; -if (static_branch_unlikely (&sched_numa_balancing)) +if (static_branch_likely (&sched_numa_balancing))
static_branch_unlikely is used by default for other static keys too like sched_cluster_active, sched_cache_active, __sched_core_enabled sched_asym_cpucapacity and others ...
So systems which don't enable them don't pay a jump when not using it. I don't see why numa should be different
Quoted 6 lines
kernel/sched/fair.c +0
-1task_tick_numa (rq, curr);task_tick_cache (rq, curr); --2.52.0
Re: [PATCH 2/2] sched: use static_branch_likely() for the NUMA balancing tick check
Quoted 22 lines · Vincent Guittot
Quoted 21 lines · Yafang Shao
task_tick_fair()checkssched_numa_balancingon every tick. Thestatic_branch_unlikely()annotation reserves the nop for the disabled case, so the default configuration (NUMA_BALANCING_DEFAULT_ENABLED=y) pays a taken jump per tick. Flip the polarity tostatic_branch_likely()so that the fast path matches the default.Signed-off-by Yafang Shao <[email protected]>--- kernel/sched/fair.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)kernel/sched/fair.c +0
-0@@ -15351,7 +15351,7 @@ static void task_tick_fair(struct rq *rq, struct task_struct *curr, int queued)if (queued)return ;
Quoted 6 lines
kernel/sched/fair.c static void task_tick_fair(struct rq *rq, struct task_struct *curr, int queued) · line 15354
Quoted 2 lines
kernel/sched/fair.c +1
-1-if (static_branch_unlikely (&sched_numa_balancing)) +if (static_branch_likely (&sched_numa_balancing))
static_branch_unlikelyis used by default for other static keys too likesched_cluster_active,sched_cache_active,__sched_core_enabledsched_asym_cpucapacityand others ...
every other instance checking sched_numa_balancing checks likely(), this is the odd one out.
Quoted 3 lines
So systems which don't enable them don't pay a jump when not using it. I don't see why numa should be different
numa balancing ships enabled by default.
This just looks like cleanup to me.
Signature · 1 line
~Gregory
Vincent Guittot
maintainerRe: [PATCH 2/2] sched: use static_branch_likely() for the NUMA balancing tick check
Quoted 31 lines · Gregory Price (Meta)
Quoted 22 lines · Vincent Guittot
Quoted 21 lines · Yafang Shao
task_tick_fair()checkssched_numa_balancingon every tick. Thestatic_branch_unlikely()annotation reserves the nop for the disabled case, so the default configuration (NUMA_BALANCING_DEFAULT_ENABLED=y) pays a taken jump per tick. Flip the polarity tostatic_branch_likely()so that the fast path matches the default.Signed-off-by Yafang Shao <[email protected]>--- kernel/sched/fair.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)kernel/sched/fair.c +0
-0@@ -15351,7 +15351,7 @@ static void task_tick_fair(struct rq *rq, struct task_struct *curr, int queued)if (queued)return ;Quoted 6 lines
kernel/sched/fair.c static void task_tick_fair(struct rq *rq, struct task_struct *curr, int queued) · line 15354
Quoted 2 lines
kernel/sched/fair.c +1
-1-if (static_branch_unlikely (&sched_numa_balancing)) +if (static_branch_likely (&sched_numa_balancing))
static_branch_unlikelyis used by default for other static keys too likesched_cluster_active,sched_cache_active,__sched_core_enabledsched_asym_cpucapacityand others ...
Quoted 2 lines
every other instance checking
sched_numa_balancingcheckslikely(), this is the odd one out.
Some other places might be behind another key but otherwise it should be unlikely()
Quoted 6 lines
Quoted 4 lines
So systems which don't enable them don't pay a jump when not using it. I don't see why numa should be different
numa balancing ships enabled by default.
Others are also shipped enabled by default
Quoted 6 lines
This just looks like cleanup to me.
--
~Gregory
Re: [PATCH 2/2] sched: use static_branch_likely() for the NUMA balancing tick check
Quoted 32 lines · Vincent Guittot
Quoted 31 lines · Gregory Price (Meta)
Quoted 28 lines · Vincent Guittot
Quoted 18 lines · Yafang Shao
task_tick_fair()checkssched_numa_balancingon every tick. Thestatic_branch_unlikely()annotation reserves the nop for the disabled case, so the default configuration (NUMA_BALANCING_DEFAULT_ENABLED=y) pays a taken jump per tick. Flip the polarity tostatic_branch_likely()so that the fast path matches the default.Signed-off-by Yafang Shao <[email protected]>--- kernel/sched/fair.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)kernel/sched/fair.c +0
-0kernel/sched/fair.c static void task_tick_fair(struct rq *rq, struct task_struct *curr, int queued) · line 15354
Quoted 5 lines
kernel/sched/fair.c +1
-1@@ -15351,7 +15351,7 @@ static void task_tick_fair(struct rq *rq, struct task_struct *curr, int queued)if (queued)return ; -if (static_branch_unlikely (&sched_numa_balancing)) +if (static_branch_likely (&sched_numa_balancing))
static_branch_unlikelyis used by default for other static keys too likesched_cluster_active,sched_cache_active,__sched_core_enabledsched_asym_cpucapacityand others ...
Quoted 6 lines
Quoted 2 lines
every other instance checking
sched_numa_balancingcheckslikely(), this is the odd one out.Some other places might be behind another key but otherwise it should be
unlikely()
On second look, I made a poor assumption on it being enabled by default.
On single-node systems, the key is off by default, even if numa balancing is built in. I don't think this change makes a huge difference either way - but probably just drop this patch.
~Gregory
Signature · 1 line
~Gregory
Re: [PATCH 2/2] sched: use static_branch_likely() for the NUMA balancing tick check
In reply to Vincent GuittotQuoted 33 lines · Vincent Guittot
Quoted 31 lines · Gregory Price (Meta)
Quoted 28 lines · Vincent Guittot
Quoted 18 lines · Yafang Shao
task_tick_fair()checkssched_numa_balancingon every tick. Thestatic_branch_unlikely()annotation reserves the nop for the disabled case, so the default configuration (NUMA_BALANCING_DEFAULT_ENABLED=y) pays a taken jump per tick. Flip the polarity tostatic_branch_likely()so that the fast path matches the default.Signed-off-by Yafang Shao <[email protected]>--- kernel/sched/fair.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)kernel/sched/fair.c +0
-0kernel/sched/fair.c static void task_tick_fair(struct rq *rq, struct task_struct *curr, int queued) · line 15354
Quoted 5 lines
kernel/sched/fair.c +1
-1@@ -15351,7 +15351,7 @@ static void task_tick_fair(struct rq *rq, struct task_struct *curr, int queued)if (queued)return ; -if (static_branch_unlikely (&sched_numa_balancing)) +if (static_branch_likely (&sched_numa_balancing))
static_branch_unlikelyis used by default for other static keys too likesched_cluster_active,sched_cache_active,__sched_core_enabledsched_asym_cpucapacityand others ...
Quoted 5 lines
Quoted 2 lines
every other instance checking
sched_numa_balancingcheckslikely(), this is the odd one out.Some other places might be behind another key but otherwise it should be
unlikely()
Hello Vincent,
I checked the other places that use the sched_numa_balancing static key. None of them is behind another key, and all but one also run while the key is disabled:
fair.c: exceed_llc_capacity() fair.c: get_pref_llc() fair.c: get_scan_cpumasks() fair.c: update_scan_period() fair.c: migrate_degrades_locality()
The only exception is task_numa_fault(), which does not run when NUMA balancing is disabled.
Please correct me if I'm wrong.
Quoted 9 lines
Quoted 7 lines
Quoted 5 lines
So systems which don't enable them don't pay a jump when not using it. I don't see why numa should be different
numa balancing ships enabled by default.
Others are also shipped enabled by default
sched_cluster_active, sched_cache_active, __sched_core_enabled and sched_asym_cpucapacity are all DEFINE_STATIC_KEY_FALSE() as well, but none of them has a config that enables it by default. sched_numa_balancing is the only one with an explicit default policy:
NUMA_BALANCING_DEFAULT_ENABLED=y enables it on every multi-node system.
That said, Gregory is right in the other thread that I overlooked the single-node case: with this change, a single-node system pays one predicted jump per tick at this site, where it previously paid none.
--
Regards
Yafang