sched/cache: honor migrate_llc_task semantics in active load balance
PATCH v3 1/1 sched/cache: honor migrate_llc_task semantics in active load balance
CAS introduced the migrate_llc_task migration type to direct tasks toward their preferred LLC, but its semantics can be lost when passive load balance falls back to active load balance. This may allow ALB to select a candidate whose preferred LLC does not match the destination, moving it away from its preferred LLC.
Example scenario:
src_rq has two runnable tasks, p1 and p2. p1 prefers dst_rq (dst_llc), while p2 prefers src_rq (src_llc). In this case, migrate_llc_task is set because src_rq has at least one task, p1, that wants to migrate to dst_rq. In ALB, can_migrate_task() finds p2 and returns true for it, thus moving p2 out of its preferred LLC.
Solution:
The CPU stopper in ALB constructs a fresh lb_env that does not inherit migration_type from the passive load-balance pass. Two approaches are possible:
(a) Add a new member to struct rq so ALB can inherit migrate_llc_task from the passive LB that triggered it.
(b) Define a new flag LBF_ACTIVE_LB_LLC and select the stopper callback at kick time to preserve the migration semantics across the asynchronous boundary.
We choose (b) because it avoids passing migration_type through the stopper, which would affect the meaning of migration_type for delayed-dequeue tasks.
---
Changes in v3:
- Updated commit message and helper comment description, no code changes
- Link to V2: https://lore.kernel.org/all/[email protected]/
Changes in v2:
- Select the stopper callback at kick time to preserve
migrate_llc_task semantics in active load balance without passing migration_type across the stopper, which affects delayed-dequeue tasks.
- Link to V1: https://lore.kernel.org/all/[email protected]/
kernel/sched/fair.c | 57 ++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 51 insertions(+), 6 deletions(-)
kernel/sched/fair.c +51 -6
@@ -10254,6 +10254,7 @@ enum migration_type {#define LBF_SOME_PINNED 0x08#define LBF_ACTIVE_LB 0x10#define LBF_LLC_PINNED 0x20 +#define LBF_ACTIVE_LB_LLC 0x40struct lb_env {struct sched_domain *sd;
@@ -10645,6 +10646,21 @@ alb_break_llc(struct lb_env *env)return false; } +/* +* Returns true if p's preferred LLC does not match the destination CPU +* under migrate_llc_task semantics. Passive LB passes migrate_llc_task +* in env->migration_type, while active LB carries LBF_ACTIVE_LB_LLC in +* env->flags to avoid overwriting env->migration_type. +*/ +static inline bool +migrate_llc_task_wrong_dst (struct task_struct *p,struct lb_env *env) +{ +return sched_cache_enabled () && + (env->migration_type == migrate_llc_task || + env->flags &LBF_ACTIVE_LB_LLC ) && +READ_ONCE (p->preferred_llc) !=llc_id (env->dst_cpu); +} +/* * Check if migrating task p from env->src_cpu to * env->dst_cpu breaks LLC localiy.
@@ -10673,8 +10689,7 @@ static bool migrate_degrades_llc(struct task_struct *p, struct lb_env *env)* run on env->dst_cpu, skip the tasks do not prefer * env->dst_cpu, and find the one that prefers. */ -if (env->migration_type == migrate_llc_task && -READ_ONCE (p->preferred_llc) !=llc_id (env->dst_cpu)) +if (migrate_llc_task_wrong_dst (p, env))return true; if (can_migrate_llc_task(env->src_cpu,
@@ -10697,6 +10712,12 @@ alb_break_llc(struct lb_env *env)return false; } +static inline bool +migrate_llc_task_wrong_dst (struct task_struct *p,struct lb_env *env) +{ +return false; +} +static inline bool migrate_degrades_llc (struct task_struct *p,struct lb_env *env) {
@@ -10796,7 +10817,7 @@ int can_migrate_task(struct task_struct *p, struct lb_env *env)* 4) too many balance attempts have failed. */ if (env->flags &LBF_ACTIVE_LB ) -return 1 ; +return !migrate_llc_task_wrong_dst (p, env); degrades =migrate_degrades_locality (p, env);if (!degrades) {
@@ -13156,6 +13177,20 @@ static int need_active_balance(struct lb_env *env) }static int active_load_balance_cpu_stop (void *data); +static int active_load_balance_llc_cpu_stop (void *data); + +/* +* migration_type is checked elsewhere to decide migration policy, so +* it shouldn't be repurposed just to flag an LLC-directed active +* balance across the stopper. Pick the callback here instead. +*/ +static inline cpu_stop_fn_t alb_stop_fn (struct lb_env *env) +{ +if (env->migration_type == migrate_llc_task) +return active_load_balance_llc_cpu_stop; + +return active_load_balance_cpu_stop; +}static int should_we_balance (struct lb_env *env) {
@@ -13492,7 +13527,7 @@ static int sched_balance_rq(int this_cpu, struct rq *this_rq,raw_spin_rq_unlock_irqrestore (busiest, flags);if (active_balance) {stop_one_cpu_nowait (cpu_of (busiest), - active_load_balance_cpu_stop, busiest, +alb_stop_fn (&env), busiest, &busiest->active_balance_work); }preempt_enable ();
@@ -13603,7 +13638,7 @@ update_next_balance(struct sched_domain *sd, unsigned long *next_balance)* least 1 task to be running on each physical CPU where possible, and * avoids physical / logical imbalances. */ -static int active_load_balance_cpu_stop (void *data) +static int __active_load_balance_cpu_stop (void *data,unsigned int lb_flags) {struct rq *busiest_rq = data;int busiest_cpu =cpu_of (busiest_rq);
@@ -13653,7 +13688,7 @@ static int active_load_balance_cpu_stop(void *data) .src_cpu = busiest_rq->cpu, .src_rq = busiest_rq, .idle = CPU_IDLE, - .flags = LBF_ACTIVE_LB, + .flags = LBF_ACTIVE_LB | lb_flags, };schedstat_inc (sd->alb_count);
@@ -13681,6 +13716,16 @@ static int active_load_balance_cpu_stop(void *data)return 0 ; } +static int active_load_balance_cpu_stop (void *data) +{ +return __active_load_balance_cpu_stop (data,0 ); +} + +static int active_load_balance_llc_cpu_stop (void *data) +{ +return __active_load_balance_cpu_stop (data,LBF_ACTIVE_LB_LLC ); +} +/* * Scale the max sched_balance_rq interval with the number of CPUs in the system. * This trades load-balance latency on larger machines for less cross talk.
Signature · 1 line
2.43.0
Quoted 23 lines
CAS introduced the
migrate_llc_taskmigration type to direct tasks toward their preferred LLC, but its semantics can be lost when passive load balance falls back to active load balance. This may allow ALB to select a candidate whose preferred LLC does not match the destination, moving it away from its preferred LLC.Example scenario:
src_rqhas two runnable tasks, p1 and p2. p1 prefersdst_rq(dst_llc), while p2 preferssrc_rq(src_llc). In this case,migrate_llc_taskis set becausesrc_rqhas at least one task, p1, that wants to migrate todst_rq. In ALB,can_migrate_task()finds p2 and returns true for it, thus moving p2 out of its preferred LLC.Solution:
The CPU stopper in ALB constructs a fresh
lb_envthat does not inheritmigration_typefrom the passive load-balance pass. Two approaches are possible:(a) Add a new member to struct rq so ALB can inherit
migrate_llc_taskfrom the passive LB that triggered it.
Quoted 7 lines
(b) Define a new flag
LBF_ACTIVE_LB_LLCand select the stopper callback at kick time to preserve the migration semantics across the asynchronous boundary.We choose (b) because it avoids passing
migration_typethrough the stopper, which would affect the meaning ofmigration_typefor delayed-dequeue tasks.
Hi Peter,
Just a gentle ping on this patch.
The v3 received Reviewed-by tags from Tim Chen and Chen Yu. Would you be happy to take this patch, or are any further changes needed?
Thanks,
Lu
PATCH v4 1/1 sched/cache: Honor migrate_llc_task semantics in active load balance
In reply to Lu WangCAS introduced the migrate_llc_task migration type to direct tasks toward their preferred LLC, but its semantics can be lost when passive load balance falls back to active load balance. This may allow ALB to select a candidate whose preferred LLC does not match the destination, moving it away from its preferred LLC.
Example scenario:
src_rq has two runnable tasks, p1 and p2. p1 prefers dst_rq (dst_llc), while p2 prefers src_rq (src_llc). In this case, migrate_llc_task is set because src_rq has at least one task, p1, that wants to migrate to dst_rq. In ALB, can_migrate_task() finds p2 and returns true for it, thus moving p2 out of its preferred LLC.
Solution:
The CPU stopper in ALB constructs a fresh lb_env that does not inherit migration_type from the passive load-balance pass. Two approaches are possible:
(a) Add a new member to struct rq so ALB can inherit migrate_llc_task from the passive LB that triggered it.
(b) Define a new flag LBF_ACTIVE_LB_LLC and select the stopper callback at kick time to preserve the migration semantics across the asynchronous boundary.
We choose (b) because it avoids passing migration_type through the stopper, which would affect the meaning of migration_type for delayed-dequeue tasks.
---
Changes in v4:
- Rebase onto current tip sched/core (ef9293b3b7, 2026-09-03),
which includes Tim Chen's commit f0d243a96f26 ("sched/fair: Avoid creating misfits during cache-aware balancing").
No functional changes vs v3.
- Link to V3: https://lore.kernel.org/all/[email protected]/
Changes in v3:
- Updated commit message and helper comment description, no code changes
- Link to V2: https://lore.kernel.org/all/[email protected]/
Changes in v2:
- Select the stopper callback at kick time to preserve
migrate_llc_task semantics in active load balance without passing migration_type across the stopper, which affects delayed-dequeue tasks.
- Link to V1: https://lore.kernel.org/all/[email protected]/
kernel/sched/fair.c | 57 ++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 51 insertions(+), 6 deletions(-)
kernel/sched/fair.c +51 -6
@@ -10387,6 +10387,7 @@ enum migration_type {#define LBF_SOME_PINNED 0x08#define LBF_ACTIVE_LB 0x10#define LBF_LLC_PINNED 0x20 +#define LBF_ACTIVE_LB_LLC 0x40struct lb_env {struct sched_domain *sd;
@@ -10805,6 +10806,21 @@ alb_break_llc(struct lb_env *env)return false; } +/* +* Returns true if p's preferred LLC does not match the destination CPU +* under migrate_llc_task semantics. Passive LB passes migrate_llc_task +* in env->migration_type, while active LB carries LBF_ACTIVE_LB_LLC in +* env->flags to avoid overwriting env->migration_type. +*/ +static inline bool +migrate_llc_task_wrong_dst (struct task_struct *p,struct lb_env *env) +{ +return sched_cache_enabled () && + (env->migration_type == migrate_llc_task || + env->flags &LBF_ACTIVE_LB_LLC ) && +READ_ONCE (p->preferred_llc) !=llc_id (env->dst_cpu); +} +/* * Check if migrating task p from env->src_cpu to * env->dst_cpu breaks LLC localiy.
@@ -10833,8 +10849,7 @@ static bool migrate_degrades_llc(struct task_struct *p, struct lb_env *env)* run on env->dst_cpu, skip the tasks do not prefer * env->dst_cpu, and find the one that prefers. */ -if (env->migration_type == migrate_llc_task && -READ_ONCE (p->preferred_llc) !=llc_id (env->dst_cpu)) +if (migrate_llc_task_wrong_dst (p, env))return true;if (can_migrate_llc_task (env, p) != mig_forbid)
@@ -10856,6 +10871,12 @@ alb_break_llc(struct lb_env *env)return false; } +static inline bool +migrate_llc_task_wrong_dst (struct task_struct *p,struct lb_env *env) +{ +return false; +} +static inline bool migrate_degrades_llc (struct task_struct *p,struct lb_env *env) {
@@ -10955,7 +10976,7 @@ int can_migrate_task(struct task_struct *p, struct lb_env *env)* 4) too many balance attempts have failed. */ if (env->flags &LBF_ACTIVE_LB ) -return 1 ; +return !migrate_llc_task_wrong_dst (p, env); degrades =migrate_degrades_locality (p, env);if (!degrades) {
@@ -13340,6 +13361,20 @@ static int need_active_balance(struct lb_env *env) }static int active_load_balance_cpu_stop (void *data); +static int active_load_balance_llc_cpu_stop (void *data); + +/* +* migration_type is checked elsewhere to decide migration policy, so +* it shouldn't be repurposed just to flag an LLC-directed active +* balance across the stopper. Pick the callback here instead. +*/ +static inline cpu_stop_fn_t alb_stop_fn (struct lb_env *env) +{ +if (env->migration_type == migrate_llc_task) +return active_load_balance_llc_cpu_stop; + +return active_load_balance_cpu_stop; +}static int should_we_balance (struct lb_env *env) {
@@ -13685,7 +13720,7 @@ static int sched_balance_rq(int this_cpu, struct rq *this_rq, }if (active_balance) {stop_one_cpu_nowait (cpu_of (busiest), - active_load_balance_cpu_stop, busiest, +alb_stop_fn (&env), busiest, &busiest->active_balance_work); }preempt_enable ();
@@ -13790,7 +13825,7 @@ update_next_balance(struct sched_domain *sd, unsigned long *next_balance)* least 1 task to be running on each physical CPU where possible, and * avoids physical / logical imbalances. */ -static int active_load_balance_cpu_stop (void *data) +static int __active_load_balance_cpu_stop (void *data,unsigned int lb_flags) {struct rq *busiest_rq = data;int busiest_cpu =cpu_of (busiest_rq);
@@ -13840,7 +13875,7 @@ static int active_load_balance_cpu_stop(void *data) .src_cpu = busiest_rq->cpu, .src_rq = busiest_rq, .idle = CPU_IDLE, - .flags = LBF_ACTIVE_LB, + .flags = LBF_ACTIVE_LB | lb_flags, };schedstat_inc (sd->alb_count);
@@ -13868,6 +13903,16 @@ static int active_load_balance_cpu_stop(void *data)return 0 ; } +static int active_load_balance_cpu_stop (void *data) +{ +return __active_load_balance_cpu_stop (data,0 ); +} + +static int active_load_balance_llc_cpu_stop (void *data) +{ +return __active_load_balance_cpu_stop (data,LBF_ACTIVE_LB_LLC ); +} +/* * Scale the max sched_balance_rq interval with the number of CPUs in the system. * This trades load-balance latency on larger machines for less cross talk.
Signature · 1 line
2.43.0