Skip to contentSkip to conversation

sched/cache: honor migrate_llc_task semantics in active load balance

PATCH Superseded Lu Wang 3 participants 20 messages Aug 1 – Aug 11 Scheduler

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

Lu Wang

author
#0 lore raw

PATCH 1/1 sched/cache: honor migrate_llc_task semantics in active load balance

A passive load-balance pass marks group_llc_balance as migrate_llc_task and queues active balance when it cannot move a task. The CPU stopper callback constructs a fresh lb_env, so preserve the migration type on the runqueue across the asynchronous boundary.

For CAS-directed active balance, reject a candidate whose preferred LLC does not match the destination LLC. This keeps the fallback from moving a task away from its preferred LLC.

Fixes e4c9a4cb244a ("sched/cache: Add migrate_llc_task migration type for cache-aware balancing")

---
v2: add Tim Chen and Chen Yu to Cc

 kernel/sched/fair.c  | 25 ++++++++++++++++++++++---
 kernel/sched/sched.h |  1 +
 2 files changed, 23 insertions(+), 3 deletions(-)
kernel/sched/fair.c +22 -3
index d78467ec6..cf036a8a0 100644
@@ -10645,6 +10645,18 @@ alb_break_llc(struct lb_env *env)
 	return false;
 }
 
+/*
+ * Returns true if p's preferred LLC does not match the destination CPU,
+ * meaning this task should not be migrated under migrate_llc_task semantics.
+ */
+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 &&
+	       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 +10685,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 +10708,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 +10813,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) {
@@ -13485,6 +13502,7 @@ static int sched_balance_rq(int this_cpu, struct rq *this_rq,
 			if (!busiest->active_balance) {
 				busiest->active_balance = 1;
 				busiest->push_cpu = this_cpu;
+				busiest->active_balance_type = env.migration_type;
 				active_balance = 1;
 			}
 
@@ -13654,6 +13672,7 @@ static int active_load_balance_cpu_stop(void *data)
 			.src_rq		= busiest_rq,
 			.idle		= CPU_IDLE,
 			.flags		= LBF_ACTIVE_LB,
+			.migration_type	= (enum migration_type)busiest_rq->active_balance_type,
 		};
 
 		schedstat_inc(sd->alb_count);
kernel/sched/sched.h +1 -0
index 56acf502b..82084d405 100644
@@ -1266,6 +1266,7 @@ struct rq {
 	/* For active balancing */
 	int			active_balance;
 	int			push_cpu;
+	int			active_balance_type; /* enum migration_type */
 	struct cpu_stop_work	active_balance_work;
 
 	/* CPU of this runqueue: */
Signature · 1 line
2.43.0
#1 lore raw

Hi Wang,

Thanks for taking a look at cas,

Quoted 9 lines · Lu Wang

A passive load-balance pass marks group_llc_balance as migrate_llc_task and queues active balance when it cannot move a task. The CPU stopper callback constructs a fresh lb_env, so preserve the migration type on the runqueue across the asynchronous boundary.

For CAS-directed active balance, reject a candidate whose preferred LLC does not match the destination LLC. This keeps the fallback from moving a task away from its preferred LLC.

[ ... ]

kernel/sched/fair.c int can_migrate_task(struct task_struct *p, struct lb_env *env) · line 10817

Quoted 10 lines
  static inline bool
  migrate_degrades_llc(struct task_struct *p, struct lb_env *env)
  {
kernel/sched/fair.c +1 -1
@@ -10796,7 +10813,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);
 

When checking whether active load balance is needed in need_active_balance(), alb_break_llc() is called to determine if the balance would break LLC locality
(i.e., prevent a task from being migrated away from its preferred
LLC). Later
in need_active_balance(), if LLC locality is not broken and the task is of migrate_llc_task type, we force an active load balance. This means that once active load balance reaches can_migrate_task(), we want it to always return true
- this helps the case where the task is the only running one on the src_cpu. Given this logic, I do not see the need for the duplicated LLC-locality check in can_migrate_task() for active load balance. Am I missing anything here?

thanks,
Chenyu

author
#2 lore raw

Thanks for the review, Chenyu.

I got interested in CAS because it strikes a good balance between generic CFS load balancing and strict LLC/CPU affinity.

My understanding is that migrate_llc_task encodes the target direction of the balance pass, not just "ALB was triggered by CAS". alb_break_llc() only vetoes ALB when every task on src_rq prefers staying put (nr_pref_llc_running == cfs.h_nr_runnable); once tasks have mixed preferences it lets ALB through without checking which one gets picked. active_load_balance_cpu_stop() then walks src_rq->cfs_tasks in reverse and takes the first task accepted by can_migrate_task() — with multiple tasks on src_rq, that's not necessarily the one whose preferred_llc matches the destination.

My patch threads migration_type through to the stopper and, only for migrate_llc_task, rejects a candidate whose preferred_llc doesn't match the destination LLC.

Regarding:

Quoted 2 lines

this helps the case where the task is the only running one on the src_cpu

If that single task already prefers the destination LLC, my check still returns true, so this case is unaffected. The disagreement is really about what happens when it does not prefer the destination. That comes down to how we read the semantics of migrate_llc_task:

(a) "migrate a task toward the destination LLC selected by calculate_imbalance()", or
(b) "this ALB was triggered by CAS's LLC-balance logic, so any generally-eligible task on src_rq may be pushed"

If (a), the per-task check is needed.

Happy to discuss further.

#3 lore raw
Quoted 28 lines · Lu Wang

Thanks for the review, Chenyu.

I got interested in CAS because it strikes a good balance between generic CFS load balancing and strict LLC/CPU affinity.

My understanding is that migrate_llc_task encodes the target direction of the balance pass, not just "ALB was triggered by CAS". alb_break_llc() only vetoes ALB when every task on src_rq prefers staying put (nr_pref_llc_running == cfs.h_nr_runnable); once tasks have mixed preferences it lets ALB through without checking which one gets picked. active_load_balance_cpu_stop() then walks src_rq->cfs_tasks in reverse and takes the first task accepted by can_migrate_task() — with multiple tasks on src_rq, that's not necessarily the one whose preferred_llc matches the destination.

My patch threads migration_type through to the stopper and, only for migrate_llc_task, rejects a candidate whose preferred_llc doesn't match the destination LLC.

Regarding:

Quoted 2 lines

this helps the case where the task is the only running one on the src_cpu

If that single task already prefers the destination LLC, my check still returns true, so this case is unaffected. The disagreement is really about what happens when it does not prefer the destination. That comes down to how we read the semantics of migrate_llc_task:

Quoted 4 lines

(a) "migrate a task toward the destination LLC selected by calculate_imbalance()", or
(b) "this ALB was triggered by CAS's LLC-balance logic, so any generally-eligible task on src_rq may be pushed"

The policy of when to break LLC preference locality whether it is in regular load balance or in active load balance are both encoded in can_migrate_llc(). Sometimes when an LLC is overloaded, you may want to move the task off its preferred LLC. Moving a task off its preferred LLC is not always wrong. Looks like you patch
stop that with migrate_llc_task_wrong_dst().
There is a comment section above can_migrate_llc() to
explain the policy details.

Tim

Quoted 4 lines

If (a), the per-task check is needed.

Happy to discuss further.

#4 lore raw
Quoted 34 lines · Tim Chen
Quoted 28 lines · Lu Wang

Thanks for the review, Chenyu.

I got interested in CAS because it strikes a good balance between generic CFS load balancing and strict LLC/CPU affinity.

My understanding is that migrate_llc_task encodes the target direction of the balance pass, not just "ALB was triggered by CAS". alb_break_llc() only vetoes ALB when every task on src_rq prefers staying put (nr_pref_llc_running == cfs.h_nr_runnable); once tasks have mixed preferences it lets ALB through without checking which one gets picked. active_load_balance_cpu_stop() then walks src_rq->cfs_tasks in reverse and takes the first task accepted by can_migrate_task() — with multiple tasks on src_rq, that's not necessarily the one whose preferred_llc matches the destination.

My patch threads migration_type through to the stopper and, only for migrate_llc_task, rejects a candidate whose preferred_llc doesn't match the destination LLC.

Regarding:

Quoted 2 lines

this helps the case where the task is the only running one on the src_cpu

If that single task already prefers the destination LLC, my check still returns true, so this case is unaffected. The disagreement is really about what happens when it does not prefer the destination. That comes down to how we read the semantics of migrate_llc_task:

Quoted 4 lines

(a) "migrate a task toward the destination LLC selected by calculate_imbalance()", or
(b) "this ALB was triggered by CAS's LLC-balance logic, so any generally-eligible task on src_rq may be pushed"

Quoted 9 lines

The policy of when to break LLC preference locality whether it is in regular load balance or in active load balance are both encoded in can_migrate_llc(). Sometimes when an LLC is overloaded, you may want to move the task off its preferred LLC. Moving a task off its preferred LLC is not always wrong. Looks like you patch
stop that with migrate_llc_task_wrong_dst().
There is a comment section above can_migrate_llc() to
explain the policy details.

Yes. Besides, if I understand correctly, I suppose Lu Wang was referring to the following scenario:

src_rq has 2 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() found p2 and returns true for p2 thus moves p2 out of its preferred LLC.

Here is the reason that it might not happen IMO:

Firstly, before ALB is triggered, the generic (passive) load balance is triggered. It iterates over p1 and p2 on src_rq to see if it can move any one of them to dst_rq, and in most cases it succeeds in moving p1 to dst_cpu. As a result, ALB will not be triggered. So the scenario Lu Wang was worried about will not happen.

On the other hand, even if there is only p2 running on src_cpu and it prefers src_cpu (src_llc), the passive load balance will fail, and ALB will be triggered. However, alb_break_llc() will return true because p2 wants to stay and it is the only running task, so ALB will not be triggered neither. So the scenario Lu Wang worried about will not happen.

thanks,
Chenyu

author
#5 lore raw

Thanks, Chenyu.

Quoted 8 lines · Yu C

Yes. Besides, if I understand correctly, I suppose Lu Wang was referring to the following scenario:

src_rq has 2 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() found p2 and returns true for p2 thus moves p2 out of its preferred LLC.

That's exactly the scenario I had in mind.

Quoted 4 lines

Firstly, before ALB is triggered, the generic (passive) load balance is triggered. It iterates over p1 and p2 on src_rq to see if it can move any one of them to dst_rq, and in most cases it succeeds in moving p1 to dst_cpu. As a result, ALB will not be triggered.

My question is whether p1 is guaranteed to be moved out in passive LB. can_migrate_task()/migrate_degrades_llc() can reject p1 for several independent reasons — p1 pinned by cpus_ptr, p1 cache-hot with nr_balance_failed still below cache_nice_tries, or can_migrate_llc_task() returning something other than mig_forbid due to capacity constraints on dst_llc at that instant. If passive LB rejects p1 for any of these, ALB is still triggered with p1 and p2 both present on src_rq.

Can we conclude that p1 and p2 never end up on src_rq together when ALB fires? Or would it help to set up a simple experiment and trace this path to see whether it actually occurs in practice?

Wang

#6 lore raw
Quoted 28 lines · Lu Wang

Thanks, Chenyu.

Quoted 8 lines · Yu C

Yes. Besides, if I understand correctly, I suppose Lu Wang was referring to the following scenario:

src_rq has 2 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() found p2 and returns true for p2 thus moves p2 out of its preferred LLC.

That's exactly the scenario I had in mind.

Quoted 4 lines

Firstly, before ALB is triggered, the generic (passive) load balance is triggered. It iterates over p1 and p2 on src_rq to see if it can move any one of them to dst_rq, and in most cases it succeeds in moving p1 to dst_cpu. As a result, ALB will not be triggered.

My question is whether p1 is guaranteed to be moved out in passive LB. can_migrate_task()/migrate_degrades_llc() can reject p1 for several independent reasons — p1 pinned by cpus_ptr, p1 cache-hot with nr_balance_failed still below cache_nice_tries, or can_migrate_llc_task() returning something other than mig_forbid due to capacity constraints on dst_llc at that instant. If passive LB rejects p1 for any of these, ALB is still triggered with p1 and p2 both present on src_rq.

Quoted 5 lines

Can we conclude that p1 and p2 never end up on src_rq together when ALB fires? Or would it help to set up a simple experiment and trace this path to see whether it actually occurs in practice?

With 2 tasks on rq with different preference, active load balance could pick the wrong task as can_migrate_task() checked in active load balance will not consult migrate_degrades_llc(). How about the following patch to fix this issue.

Tim

---
sched/cache: skip active load balance for LLC-motivated imbalance

For a migrate_llc_task imbalance, ALB runs detach_one_task() with LBF_ACTIVE_LB set, which makes can_migrate_task() return early before migrate_degrades_llc() is consulted. The victim is then the first eligible task at the tail of cfs_tasks, regardless of LLC preference, so ALB can pull a task that prefers the source LLC - the opposite of the intent.

Skip ALB for migrate_llc_task when more than one CFS task is runnable, and let a later balance pass pull a task that prefers the destination LLC. With a single runnable task ALB is retained: that task prefers the destination LLC and cannot migrate otherwise. Other ALB reasons (asym, misfit, imbalanced, capacity) arrive with a different migration_type and are unaffected.

---
 kernel/sched/fair.c | 13 +++++++++++++
 1 file changed, 13 insertions(+)
kernel/sched/fair.c +13 -0
index d78467ec6ee1..615c9aeab621 100644
@@ -10642,6 +10642,19 @@ alb_break_llc(struct lb_env *env)
 			return true;
 	}
 
+	/*
+	 * When the imbalance is for migrate_llc_task, the ALB victim is
+	 * chosen by can_migrate_task() under LBF_ACTIVE_LB, which ignores
+	 * LLC preference and may pull a task that prefers the source LLC.
+	 * Skip ALB when more than one CFS task is runnable, and let a
+	 * later balance pass pull a task that prefers the destination LLC
+	 * instead. With a single runnable task, ALB is still needed: that
+	 * task prefers the destination LLC and cannot migrate otherwise.
+	 */
+	if (env->migration_type == migrate_llc_task &&
+	    env->src_rq->cfs.h_nr_runnable > 1)
+		return true;
+
 	return false;
 }
 
author
#7 lore raw

From: Lu Wang <[email protected]>

Thanks, Tim.

Quoted 45 lines · Tim Chen
Quoted 28 lines · Lu Wang

Thanks, Chenyu.

Quoted 8 lines · Yu C

Yes. Besides, if I understand correctly, I suppose Lu Wang was referring to the following scenario:

src_rq has 2 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() found p2 and returns true for p2 thus moves p2 out of its preferred LLC.

That's exactly the scenario I had in mind.

Quoted 4 lines

Firstly, before ALB is triggered, the generic (passive) load balance is triggered. It iterates over p1 and p2 on src_rq to see if it can move any one of them to dst_rq, and in most cases it succeeds in moving p1 to dst_cpu. As a result, ALB will not be triggered.

My question is whether p1 is guaranteed to be moved out in passive LB. can_migrate_task()/migrate_degrades_llc() can reject p1 for several independent reasons — p1 pinned by cpus_ptr, p1 cache-hot with nr_balance_failed still below cache_nice_tries, or can_migrate_llc_task() returning something other than mig_forbid due to capacity constraints on dst_llc at that instant. If passive LB rejects p1 for any of these, ALB is still triggered with p1 and p2 both present on src_rq.

Quoted 3 lines

Can we conclude that p1 and p2 never end up on src_rq together when ALB fires? Or would it help to set up a simple experiment and trace this path to see whether it actually occurs in practice?

With 2 tasks on rq with different preference, active load balance could pick the wrong task as can_migrate_task() checked in active load balance will not consult migrate_degrades_llc(). How about the following patch to fix this issue.

[...]

+ if (env->migration_type == migrate_llc_task &&

kernel/sched/fair.c +3 -0
+     env->src_rq->cfs.h_nr_runnable > 1)
+         return true;
+
  return false;
Quoted 1 line

}

Your approach is simpler than mine — it avoids threading migration_type across the CPU stopper boundary and doesn't need any new rq field.

One thing I'd like to flag, IMO: this approach skips the ALB path entirely for migrate_llc_task whenever more than one task is runnable, deferring the fix to the next passive LB pass. So it trades "delay" for a simpler implementation.

Wang

#8 lore raw
Quoted 52 lines · On Wed, 2026-08-05 at 10:38 +0800, wanglu15 wrote:

From: Lu Wang <[email protected]>

Thanks, Tim.

Quoted 45 lines · Tim Chen
Quoted 28 lines · Lu Wang

Thanks, Chenyu.

Quoted 8 lines · Yu C

Yes. Besides, if I understand correctly, I suppose Lu Wang was referring to the following scenario:

src_rq has 2 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() found p2 and returns true for p2 thus moves p2 out of its preferred LLC.

That's exactly the scenario I had in mind.

Quoted 4 lines

Firstly, before ALB is triggered, the generic (passive) load balance is triggered. It iterates over p1 and p2 on src_rq to see if it can move any one of them to dst_rq, and in most cases it succeeds in moving p1 to dst_cpu. As a result, ALB will not be triggered.

My question is whether p1 is guaranteed to be moved out in passive LB. can_migrate_task()/migrate_degrades_llc() can reject p1 for several independent reasons — p1 pinned by cpus_ptr, p1 cache-hot with nr_balance_failed still below cache_nice_tries, or can_migrate_llc_task() returning something other than mig_forbid due to capacity constraints on dst_llc at that instant. If passive LB rejects p1 for any of these, ALB is still triggered with p1 and p2 both present on src_rq.

Quoted 3 lines

Can we conclude that p1 and p2 never end up on src_rq together when ALB fires? Or would it help to set up a simple experiment and trace this path to see whether it actually occurs in practice?

With 2 tasks on rq with different preference, active load balance could pick the wrong task as can_migrate_task() checked in active load balance will not consult migrate_degrades_llc(). How about the following patch to fix this issue.

[...]

+ if (env->migration_type == migrate_llc_task &&

kernel/sched/fair.c +3 -0
+     env->src_rq->cfs.h_nr_runnable > 1)
+         return true;
+
  return false;
Quoted 1 line

}

Quoted 9 lines

Your approach is simpler than mine — it avoids threading migration_type across the CPU stopper boundary and doesn't need any new rq field.

One thing I'd like to flag, IMO: this approach skips the ALB path entirely for migrate_llc_task whenever more than one task is runnable, deferring the fix to the next passive LB pass. So it trades "delay" for a simpler implementation.

If we cannot pull a task from this rq for a migrate_llc_task imbalance with more than one runnable task, can_migrate_task() has already rejected the candidates — either the task preferring the dst LLC is cache-hot or capacity-constrained, or the only movable task prefers the source LLC. Forcing ALB here would ignore that.

We keep ALB only for the single-task case, where that lone task prefers the dst LLC and has no other way to migrate.

I think this is the right thing to do because we shouldn't force the tasks to move when can_migrate_task() is already telling us not to.

Tim

#9 lore raw
Quoted 35 lines · Tim Chen
Quoted 34 lines · On Wed, 2026-08-05 at 10:38 +0800, wanglu15 wrote:

From: Lu Wang <[email protected]>

Thanks, Tim.

Quoted 29 lines · Tim Chen
Quoted 28 lines · Lu Wang

Thanks, Chenyu.

Quoted 8 lines · Yu C

Yes. Besides, if I understand correctly, I suppose Lu Wang was referring to the following scenario:

src_rq has 2 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() found p2 and returns true for p2 thus moves p2 out of its preferred LLC.

That's exactly the scenario I had in mind.

Quoted 4 lines

Firstly, before ALB is triggered, the generic (passive) load balance is triggered. It iterates over p1 and p2 on src_rq to see if it can move any one of them to dst_rq, and in most cases it succeeds in moving p1 to dst_cpu. As a result, ALB will not be triggered.

My question is whether p1 is guaranteed to be moved out in passive LB. can_migrate_task()/migrate_degrades_llc() can reject p1 for several independent reasons — p1 pinned by cpus_ptr, p1 cache-hot with nr_balance_failed still below cache_nice_tries, or can_migrate_llc_task() returning something other than mig_forbid due to capacity constraints on dst_llc at that instant. If passive LB rejects p1 for any of these, ALB is still triggered with p1 and p2 both present on src_rq.

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

Can we conclude that p1 and p2 never end up on src_rq together when ALB fires? Or would it help to set up a simple experiment and trace this path to see whether it actually occurs in practice?

I see. It is possible for p1 and p2 to coexist on src_rq. Previously, I thought that if task migration failed due to CPU affinity, then dst_cpu or src_cpu would be changed because of env.flags & LBF_DST_PINNED or env.flags & LBF_ALL_PINNED. So ALB would be skipped. But a more common scenario seems to be when p1 is the running task. Then can_migrate_task() will gate it anyway. That is a common migration failure.

Quoted 25 lines
Quoted 15 lines
Quoted 13 lines

With 2 tasks on rq with different preference, active load balance could pick the wrong task as can_migrate_task() checked in active load balance will not consult migrate_degrades_llc(). How about the following patch to fix this issue.

[...]

+ if (env->migration_type == migrate_llc_task &&

kernel/sched/fair.c +3 -0
+     env->src_rq->cfs.h_nr_runnable > 1)
+         return true;
+
   return false;
Quoted 1 line

}

Quoted 9 lines

Your approach is simpler than mine — it avoids threading migration_type across the CPU stopper boundary and doesn't need any new rq field.

One thing I'd like to flag, IMO: this approach skips the ALB path entirely for migrate_llc_task whenever more than one task is runnable, deferring the fix to the next passive LB pass. So it trades "delay" for a simpler implementation.

Quoted 5 lines

If we cannot pull a task from this rq for a migrate_llc_task imbalance with more than one runnable task, can_migrate_task() has already rejected the candidates — either the task preferring the dst LLC is cache-hot or capacity-constrained, or the only movable task prefers the source LLC. Forcing ALB here would ignore that.

In passive load balance, if there are 2 tasks, p1 prefers dst_llc, and if p1 is the running task, then p1 can not be migrated to dst_cpu in can_migrate_task():
if (task_on_cpu(env->src_rq, p))

	return 0;

and in this case we might have to rely on ALB to migrate p1(not ignore it)

thanks,
Chenyu

Quoted 8 lines

We keep ALB only for the single-task case, where that lone task prefers the dst LLC and has no other way to migrate.

I think this is the right thing to do because we shouldn't force the tasks to move when can_migrate_task() is already telling us not to.

#10 lore raw
Quoted 78 lines · Yu C
Quoted 35 lines · Tim Chen
Quoted 34 lines · On Wed, 2026-08-05 at 10:38 +0800, wanglu15 wrote:

From: Lu Wang <[email protected]>

Thanks, Tim.

Quoted 29 lines · Tim Chen
Quoted 28 lines · Lu Wang

Thanks, Chenyu.

Quoted 8 lines · Yu C

Yes. Besides, if I understand correctly, I suppose Lu Wang was referring to the following scenario:

src_rq has 2 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() found p2 and returns true for p2 thus moves p2 out of its preferred LLC.

That's exactly the scenario I had in mind.

Quoted 4 lines

Firstly, before ALB is triggered, the generic (passive) load balance is triggered. It iterates over p1 and p2 on src_rq to see if it can move any one of them to dst_rq, and in most cases it succeeds in moving p1 to dst_cpu. As a result, ALB will not be triggered.

My question is whether p1 is guaranteed to be moved out in passive LB. can_migrate_task()/migrate_degrades_llc() can reject p1 for several independent reasons — p1 pinned by cpus_ptr, p1 cache-hot with nr_balance_failed still below cache_nice_tries, or can_migrate_llc_task() returning something other than mig_forbid due to capacity constraints on dst_llc at that instant. If passive LB rejects p1 for any of these, ALB is still triggered with p1 and p2 both present on src_rq.

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

Can we conclude that p1 and p2 never end up on src_rq together when ALB fires? Or would it help to set up a simple experiment and trace this path to see whether it actually occurs in practice?

I see. It is possible for p1 and p2 to coexist on src_rq. Previously, I thought that if task migration failed due to CPU affinity, then dst_cpu or src_cpu would be changed because of env.flags & LBF_DST_PINNED or env.flags & LBF_ALL_PINNED. So ALB would be skipped. But a more common scenario seems to be when p1 is the running task. Then can_migrate_task() will gate it anyway. That is a common migration failure.

Quoted 25 lines
Quoted 15 lines
Quoted 13 lines

With 2 tasks on rq with different preference, active load balance could pick the wrong task as can_migrate_task() checked in active load balance will not consult migrate_degrades_llc(). How about the following patch to fix this issue.

[...]

+ if (env->migration_type == migrate_llc_task &&

kernel/sched/fair.c +3 -0
+     env->src_rq->cfs.h_nr_runnable > 1)
+         return true;
+
   return false;
Quoted 1 line

}

Quoted 9 lines

Your approach is simpler than mine — it avoids threading migration_type across the CPU stopper boundary and doesn't need any new rq field.

One thing I'd like to flag, IMO: this approach skips the ALB path entirely for migrate_llc_task whenever more than one task is runnable, deferring the fix to the next passive LB pass. So it trades "delay" for a simpler implementation.

Quoted 5 lines

If we cannot pull a task from this rq for a migrate_llc_task imbalance with more than one runnable task, can_migrate_task() has already rejected the candidates — either the task preferring the dst LLC is cache-hot or capacity-constrained, or the only movable task prefers the source LLC. Forcing ALB here would ignore that.

Quoted 7 lines

In passive load balance, if there are 2 tasks, p1 prefers dst_llc, and if p1 is the running task, then p1 can not be migrated to dst_cpu in can_migrate_task():
if (task_on_cpu(env->src_rq, p))

	return 0;

and in this case we might have to rely on ALB to migrate p1(not ignore it)

Considering the case above, then what Lu Wang proposed is probably the better option.

Tim

author
#11 lore raw
In reply to Tim Chen

Thanks, Tim.

Quoted 34 lines · Tim Chen
Quoted 28 lines · Lu Wang

Thanks for the review, Chenyu.

I got interested in CAS because it strikes a good balance between generic CFS load balancing and strict LLC/CPU affinity.

My understanding is that migrate_llc_task encodes the target direction of the balance pass, not just "ALB was triggered by CAS". alb_break_llc() only vetoes ALB when every task on src_rq prefers staying put (nr_pref_llc_running == cfs.h_nr_runnable); once tasks have mixed preferences it lets ALB through without checking which one gets picked. active_load_balance_cpu_stop() then walks src_rq->cfs_tasks in reverse and takes the first task accepted by can_migrate_task() — with multiple tasks on src_rq, that's not necessarily the one whose preferred_llc matches the destination.

My patch threads migration_type through to the stopper and, only for migrate_llc_task, rejects a candidate whose preferred_llc doesn't match the destination LLC.

Regarding:

Quoted 2 lines

this helps the case where the task is the only running one on the src_cpu

If that single task already prefers the destination LLC, my check still returns true, so this case is unaffected. The disagreement is really about what happens when it does not prefer the destination. That comes down to how we read the semantics of migrate_llc_task:

Quoted 4 lines

(a) "migrate a task toward the destination LLC selected by calculate_imbalance()", or
(b) "this ALB was triggered by CAS's LLC-balance logic, so any generally-eligible task on src_rq may be pushed"

Quoted 8 lines

The policy of when to break LLC preference locality whether it is in regular load balance or in active load balance are both encoded in can_migrate_llc(). Sometimes when an LLC is overloaded, you may want to move the task off its preferred LLC. Moving a task off its preferred LLC is not always wrong. Looks like you patch stop that with migrate_llc_task_wrong_dst().
There is a comment section above can_migrate_llc() to explain the policy details.

To clarify the scope: my patch only adds a stricter check for the migrate_llc_task type during migration.

llc_balance() triggers this classification as long as there are tasks whose preferred LLC is the destination, independent of load or utilization. So it sits at the end of the balancing priority chain. The comment above group_llc_balance's priority assignment, which you wrote, says:

"The priority of group_llc_balance is lower than that of [other types]... This is because group_llc_balance may exacerbate load imbalance."

This makes clear that group_llc_balance was not designed to correct load imbalance — it explicitly acknowledges it may worsen it.

For the other migration types that do address load imbalance (migrate_load, migrate_util, migrate_task, migrate_misfit), my patch doesn't change their logic at all — the check only fires when env->migration_type == migrate_llc_task.

Is my understanding incorrect here?

Wang

#12 lore raw
In reply to Lu Wang

Hi Lu Wang, Tim,

Quoted 9 lines · Lu Wang

A passive load-balance pass marks group_llc_balance as migrate_llc_task and queues active balance when it cannot move a task. The CPU stopper callback constructs a fresh lb_env, so preserve the migration type on the runqueue across the asynchronous boundary.

For CAS-directed active balance, reject a candidate whose preferred LLC does not match the destination LLC. This keeps the fallback from moving a task away from its preferred LLC.

It looks like this proposal provides fine-grain control on per-task base migration strategy is promising.

Quoted 8 lines
kernel/sched/fair.c +8 -0
+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 &&
+	       READ_ONCE(p->preferred_llc) != llc_id(env->dst_cpu);
+}
+

[ ... ]

kernel/sched/fair.c static int active_load_balance_cpu_stop(void *data) · line 13675

Quoted 5 lines
kernel/sched/fair.c +1 -0
@@ -13654,6 +13672,7 @@ static int active_load_balance_cpu_stop(void *data)
  			.src_rq		= busiest_rq,
  			.idle		= CPU_IDLE,
  			.flags		= LBF_ACTIVE_LB,
+			.migration_type	= (enum migration_type)busiest_rq->active_balance_type,

If we overwrite migration_type for ALB (default is 0, i.e. migrate_load), then in can_migrate_task() a delayed task might not be migrated in ALB:

if ((p->se.sched_delayed) && (env->migration_type != migrate_load))

	return 0;

So an enhanced approach I'm thinking of is to pass migrate_llc_task information via env->flags:

kernel/sched/sched.h struct rq { · line 1269

Quoted 12 lines
kernel/sched/fair.c +0 -0
  		};
 
  		schedstat_inc(sd->alb_count);
kernel/sched/sched.h +1 -0
index 56acf502b..82084d405 100644
@@ -1266,6 +1266,7 @@ struct rq {
  	/* For active balancing */
  	int			active_balance;
  	int			push_cpu;
+	int			active_balance_type; /* enum migration_type */

We can set env->flags by invoking different callbacks of stop_one_cpu_nowait(), thus avoiding the need to introduce active_balance_type into rq - which could cause false sharing if cache-line alignment is broken.

something like:

#define LBF_ACTIVE_LB_LLC 0x40

-static int active_load_balance_cpu_stop(void *data)
+static int __active_load_balance_cpu_stop(void *data, unsigned int lb_flags) -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);
@@ -13659,7 +13687,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,
                };

and in migrate_llc_task_wrong_dst(), we check env->flags & LBF_ACTIVE_LB_LLC instead.

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); 

<-- new flag
}

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;
}
if (active_balance) {
	stop_one_cpu_nowait(cpu_of(busiest),
		alb_stop_fn(&env), busiest,
		&busiest->active_balance_work);
}

thanks,
Chenyu

Quoted 3 lines
kernel/sched/sched.h +0 -0
  	struct cpu_stop_work	active_balance_work;
 
  	/* CPU of this runqueue: */
#13 lore raw
Quoted 36 lines · Yu C

Hi Lu Wang, Tim,

Quoted 9 lines · Lu Wang

A passive load-balance pass marks group_llc_balance as migrate_llc_task and queues active balance when it cannot move a task. The CPU stopper callback constructs a fresh lb_env, so preserve the migration type on the runqueue across the asynchronous boundary.

For CAS-directed active balance, reject a candidate whose preferred LLC does not match the destination LLC. This keeps the fallback from moving a task away from its preferred LLC.

It looks like this proposal provides fine-grain control on per-task base migration strategy is promising.

Quoted 8 lines
kernel/sched/fair.c +8 -0
+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 &&
+	       READ_ONCE(p->preferred_llc) != llc_id(env->dst_cpu);
+}
+

[ ... ]

kernel/sched/fair.c static int active_load_balance_cpu_stop(void *data) · line 13675

Quoted 5 lines
kernel/sched/fair.c +1 -0
@@ -13654,6 +13672,7 @@ static int active_load_balance_cpu_stop(void *data)
  			.src_rq		= busiest_rq,
  			.idle		= CPU_IDLE,
  			.flags		= LBF_ACTIVE_LB,
+			.migration_type	= (enum migration_type)busiest_rq->active_balance_type,

If we overwrite migration_type for ALB (default is 0, i.e. migrate_load), then in can_migrate_task() a delayed task might not be migrated in ALB:

Quoted 1 line

if ((p->se.sched_delayed) && (env->migration_type != migrate_load))

This is a good catch.

It may be easier to create a migrate_llc_task_alb type and pass that in migration type. Then modify the above as

if ((p->se.sched_delayed) && env->migration_type != migrate_load

		          && env->migration_type != migrate_llc_task_alb)
	return 0

That avoids creating two cpu stop functions.

Tim

Quoted 78 lines
	return 0;

So an enhanced approach I'm thinking of is to pass migrate_llc_task information via env->flags:

kernel/sched/sched.h struct rq { · line 1269

Quoted 12 lines
kernel/sched/fair.c +0 -0
  		};
 
  		schedstat_inc(sd->alb_count);
kernel/sched/sched.h +1 -0
index 56acf502b..82084d405 100644
@@ -1266,6 +1266,7 @@ struct rq {
  	/* For active balancing */
  	int			active_balance;
  	int			push_cpu;
+	int			active_balance_type; /* enum migration_type */

We can set env->flags by invoking different callbacks of stop_one_cpu_nowait(),
thus avoiding the need to introduce active_balance_type into rq - which could
cause false sharing if cache-line alignment is broken.

something like:

#define LBF_ACTIVE_LB_LLC 0x40

(quoted diff) +1 -1
-static int active_load_balance_cpu_stop(void *data)
+static int __active_load_balance_cpu_stop(void *data, unsigned int 

lb_flags)

(quoted diff) +1 -1
-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);
kernel/sched/fair.c +1 -1
@@ -13659,7 +13687,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,
                 };
 

and in migrate_llc_task_wrong_dst(), we check env->flags & LBF_ACTIVE_LB_LLC instead.

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); 

<-- new flag
}

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;
}
if (active_balance) {
	stop_one_cpu_nowait(cpu_of(busiest),
		alb_stop_fn(&env), busiest,
		&busiest->active_balance_work);
}

thanks,
Chenyu

Quoted 3 lines
kernel/sched/sched.h +0 -0
  	struct cpu_stop_work	active_balance_work;
 
  	/* CPU of this runqueue: */
#14 lore raw
Quoted 39 lines · Tim Chen
Quoted 36 lines · Yu C

Hi Lu Wang, Tim,

Quoted 9 lines · Lu Wang

A passive load-balance pass marks group_llc_balance as migrate_llc_task and queues active balance when it cannot move a task. The CPU stopper callback constructs a fresh lb_env, so preserve the migration type on the runqueue across the asynchronous boundary.

For CAS-directed active balance, reject a candidate whose preferred LLC does not match the destination LLC. This keeps the fallback from moving a task away from its preferred LLC.

It looks like this proposal provides fine-grain control on per-task base migration strategy is promising.

Quoted 8 lines
kernel/sched/fair.c +8 -0
+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 &&
+	       READ_ONCE(p->preferred_llc) != llc_id(env->dst_cpu);
+}
+

[ ... ]

kernel/sched/fair.c static int active_load_balance_cpu_stop(void *data) · line 13675

Quoted 5 lines
kernel/sched/fair.c +1 -0
@@ -13654,6 +13672,7 @@ static int active_load_balance_cpu_stop(void *data)
   			.src_rq		= busiest_rq,
   			.idle		= CPU_IDLE,
   			.flags		= LBF_ACTIVE_LB,
+			.migration_type	= (enum migration_type)busiest_rq->active_balance_type,

If we overwrite migration_type for ALB (default is 0, i.e. migrate_load), then in can_migrate_task() a delayed task might not be migrated in ALB:

Quoted 1 line

if ((p->se.sched_delayed) && (env->migration_type != migrate_load))

Quoted 9 lines

This is a good catch.

It may be easier to create a migrate_llc_task_alb type and pass that in migration type. Then modify the above as

if ((p->se.sched_delayed) && env->migration_type != migrate_load

		          && env->migration_type != migrate_llc_task_alb)
	return 0

We still need a channel to carry migrate_llc_task/migrate_llc_task_alb into the alb, since the stopper builds a fresh lb_env - hence rq->active_balance_type was introduced in Lu Wang's proposal. We can reuse the callback slot in active_balance_work instead, pick active_load_balance_llc_cpu_stop() at kick time, thus no new rq field is needed.

thanks,
Chenyu

author
#15 lore raw

Thanks, both.

Quoted 35 lines · Yu C
Quoted 25 lines · Tim Chen
Quoted 15 lines · Yu C

It looks like this proposal provides fine-grain control on per-task base migration strategy is promising.

If we overwrite migration_type for ALB (default is 0, i.e. migrate_load), then in can_migrate_task() a delayed task might not be migrated in ALB:

if ((p->se.sched_delayed) && (env->migration_type != migrate_load))

	return 0;

So an enhanced approach I'm thinking of is to pass migrate_llc_task information via env->flags:

#define LBF_ACTIVE_LB_LLC 0x40

static int active_load_balance_cpu_stop(void *data)

Quoted 8 lines
{
	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);
}
Quoted 8 lines

This is a good catch.

It may be easier to create a migrate_llc_task_alb type and pass that in migration type. Then modify the above as

if ((p->se.sched_delayed) && env->migration_type != migrate_load

	          && env->migration_type != migrate_llc_task_alb)
	return 0
Quoted 6 lines

We still need a channel to carry migrate_llc_task/migrate_llc_task_alb into the alb, since the stopper builds a fresh lb_env - hence rq->active_balance_type was introduced in Lu Wang's proposal. We can reuse the callback slot in active_balance_work instead, pick active_load_balance_llc_cpu_stop() at kick time, thus no new rq field is needed.

Good catch. I agree that overwriting migration_type in the stopper changes the existing delayed-dequeue behavior.

Picking the callback at kick time and dropping the rq field looks like the cleanest version of this so far -- it fixes the original issue I raised without affecting delayed-dequeue behavior. I agree with refactoring the patch along this idea and send patchV3.

Lu Wang

author
#16 lore raw

PATCH v2 1/1 sched/cache: honor migrate_llc_task semantics in active load balance

In reply to Lu Wang

A passive load-balance pass marks group_llc_balance as migrate_llc_task and queues active balance when it cannot move a task. The CPU stopper callback constructs a fresh lb_env, so select the stopper callback when queueing active balance to preserve the migration semantics across the asynchronous boundary.

For CAS-directed active balance, reject a candidate whose preferred LLC does not match the destination LLC. This keeps the fallback from moving a task away from its preferred LLC.

Fixes e4c9a4cb244a (\"sched/cache: Add migrate_llc_task migration type for cache-aware balancing\")

---
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 | 56 ++++++++++++++++++++++++++++++++++++++++-----
 1 file changed, 50 insertions(+), 6 deletions(-)
kernel/sched/fair.c +50 -6
index d78467ec6..37a470c41 100644
@@ -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	0x40
 
 struct lb_env {
 	struct sched_domain	*sd;
@@ -10645,6 +10646,20 @@ 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 migration_type, while active LB carries it in LBF_ACTIVE_LB_LLC.
+ */
+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 +10688,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 +10711,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 +10816,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 +13176,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 +13526,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 +13637,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 +13687,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 +13715,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
Reviewed-by on v2 1/1
#17 lore raw
Quoted 9 lines · Lu Wang

A passive load-balance pass marks group_llc_balance as migrate_llc_task and queues active balance when it cannot move a task. The CPU stopper callback constructs a fresh lb_env, so select the stopper callback when queueing active balance to preserve the migration semantics across the asynchronous boundary.

For CAS-directed active balance, reject a candidate whose preferred LLC does not match the destination LLC. This keeps the fallback from moving a task away from its preferred LLC.

Thanks. This version looks good to me.

Tim

Quoted 5 lines
Fixes e4c9a4cb244a (\"sched/cache: Add migrate_llc_task migration type for cache-aware balancing\")

---

Reviewed-by on v2 1/1
#18 lore raw
In reply to Lu Wang
Quoted 6 lines · Lu Wang

A passive load-balance pass marks group_llc_balance as migrate_llc_task and queues active balance when it cannot move a task. The CPU stopper callback constructs a fresh lb_env, so select the stopper callback when queueing active balance to preserve the migration semantics across the asynchronous boundary.

Worthy adding a [Problem Statement] section to describe what issue this patch tries to fix (with polish):
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.

Quoted 4 lines

For CAS-directed active balance, reject a candidate whose preferred LLC does not match the destination LLC. This keeps the fallback from moving a task away from its preferred LLC.

Quoted 8 lines
Fixes e4c9a4cb244a (\"sched/cache: Add migrate_llc_task migration type for cache-aware balancing\")

---
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.

Maybe move the above reasoning about why the new stopper function is needed into the commit log for future reference.

kernel/sched/fair.c alb_break_llc(struct lb_env *env) · line 10652

Quoted 8 lines
kernel/sched/fair.c +4 -0
@@ -10645,6 +10646,20 @@ 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 migration_type, while active LB carries it in LBF_ACTIVE_LB_LLC.

"while active LB carries LBF_ACTIVE_LB_LLC in env->flags to avoid overwriting env->migrate_type."

Per my understanding,

thanks,
Chenyu

author
#19 lore raw

Hi Chenyu,

Thanks for the review and the suggestions.

Quoted 16 lines · Yu C
Quoted 6 lines · Lu Wang

A passive load-balance pass marks group_llc_balance as migrate_llc_task and queues active balance when it cannot move a task. The CPU stopper callback constructs a fresh lb_env, so select the stopper callback when queueing active balance to preserve the migration semantics across the asynchronous boundary.

Worthy adding a [Problem Statement] section to describe what issue this patch tries to fix
(with polish):
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.

Quoted 6 lines
Quoted 4 lines

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.

Maybe move the above reasoning about why the new stopper function is needed into the commit log for future reference.

Thanks. I will add the p1/p2 problem statement and move the rationale for selecting the stopper callback at kick time into the commit log.

Quoted 6 lines
Quoted 4 lines

+/*
+ * 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 migration_type, while active LB carries it in LBF_ACTIVE_LB_LLC.

"while active LB carries LBF_ACTIVE_LB_LLC in env->flags to avoid overwriting env->migrate_type."

Thanks. I will update the helper comment accordingly.

I will include these commit-log and comment-only updates in v3.

Thanks,
Lu Wang