sched/fair: which tasks should nr_pref_llc_running be compared against?
Reading alb_break_llc() I cannot tell which set of tasks the equality is meant to cover:
/* kernel/sched/fair.c:10756 */
if (env->src_rq->nr_pref_llc_running &&
env->src_rq->nr_pref_llc_running == env->src_rq->cfs.h_nr_runnable) {The two counters track different sets. nr_pref_llc_running is maintained from account_entity_enqueue() and account_entity_dequeue() at fair.c:4522 and 4538, beside cfs_rq->nr_queued++/-- at 4525 and 4541, so it follows queued tasks. h_nr_runnable leaves out delay-dequeued entities:set_delayed() decrements it at 6398 while the entity stays queued and clear_delayed() restores it at 6418, neither going through account_entity_dequeue().
So with DELAY_DEQUEUE a task that has just gone to sleep holds nr_pref_llc_running above h_nr_runnable until it is dequeued for real, the equality cannot hold, and alb_break_llc() returns false, which stops active load balance from honouring the LLC preference. The counter is a superset of the other, so the error is one-sided: the check can fail to protect but never protects wrongly.
Comparing against cfs.h_nr_queued would make both sides agree and read as "every queued fair task prefers this LLC". Keeping runnable semantics would instead mean maintaining nr_pref_llc_running from set_delayed() and clear_delayed(), which may be the better fit, since only runnable tasks are candidates for active load balance and a task on its way to sleep is not one. Which did you have in mind?
This is from reading the code rather than an observed failure, and I could not exercise the path in a two-socket qemu guest either: with cache aware scheduling on and one process of 6 busy and 10 sleeping threads, p->preferred_llc was never assigned, so nr_pref_llc_running stayed 0 throughout while alb_break_llc() itself ran 21 times. Nearly every account_mm_sched() call returned before the mm->sc_stat check, which makes me suspect update_se() bails on delta_exec <= 0 under emulation before the accounting is reached. If a guest cannot exercise this, that is worth knowing on its own, since it is also how a mismatch like this stays hidden.
Thanks,
Zhan Xusheng
Quoted 15 lines · Zhan Xusheng
Reading
alb_break_llc()I cannot tell which set of tasks the equality is meant to cover:/* kernel/sched/fair.c:10756 */ if (env->src_rq->nr_pref_llc_running && env->src_rq->nr_pref_llc_running == env->src_rq->cfs.h_nr_runnable) {The two counters track different sets.
nr_pref_llc_runningis maintained fromaccount_entity_enqueue()andaccount_entity_dequeue()at fair.c:4522 and 4538, beside cfs_rq->nr_queued++/-- at 4525 and 4541, so it follows queued tasks.h_nr_runnableleaves out delay-dequeued entities:set_delayed()decrements it at 6398 while the entity stays queued andclear_delayed()restores it at 6418, neither going throughaccount_entity_dequeue().
Quoted 6 lines
So with
DELAY_DEQUEUEa task that has just gone to sleep holdsnr_pref_llc_runningaboveh_nr_runnableuntil it is dequeued for real, the equality cannot hold, andalb_break_llc()returns false, which stops active load balance from honouring the LLC preference. The counter is a superset of the other, so the error is one-sided: the check can fail to protect but never protects wrongly.
Thank you for your review of this code. You have a valid point that DELAY_DEQUEUE could cause problem with the check in question. The intention of the check is to take care of the situation where all running tasks prefer the source LLC, and we should try not to do active balance that will break LLC locality. And delayed dequeue of a task preferring source LLC could break the check unintentionally, even though the rest of running tasks still prefer source LLC.
Quoted 7 lines
Comparing against cfs.h_nr_queued would make both sides agree and read as "every queued fair task prefers this LLC". Keeping runnable semantics would instead mean maintaining
nr_pref_llc_runningfromset_delayed()andclear_delayed(), which may be the better fit, since only runnable tasks are candidates for active load balance and a task on its way to sleep is not one. Which did you have in mind?
I think the proper thing to do is to make sure the nr_pref_llc_running accounts correctly the number of running tasks that prefer the source LLC. And
exclude those that are delayed dequeued. How about the following fix:
Tim
---
From de4acf2ec43a57d84aa301b50c4ab51965ac1f23 Mon Sep 17 00:00:00 2001
From: Tim Chen <[email protected]>
Date: Thu, 27 Aug 2026 11:20:31 -0700
Subject: [PATCH] sched/cache: Keep nr_pref_llc_running in the runnable domain
alb_break_llc() decides whether to break LLC preference during active load balance. It does so by testing that every runnable fair task on the source rq prefers its LLC:
env->src_rq->nr_pref_llc_running == env->src_rq->cfs.h_nr_runnable
But the two counters cover different sets. nr_pref_llc_running is updated in account_llc_enqueue()/account_llc_dequeue(), next to cfs_rq->nr_queued, so it follows queued tasks. h_nr_runnable is updated in set_delayed()/ clear_delayed() and drops delay-dequeued tasks.
Say we start off with all running tasks preferring source LLC. So under DELAY_DEQUEUE, a preferring task that goes to sleep stays counted in nr_pref_llc_running while h_nr_runnable falls. The equality then breaks, alb_break_llc() returns false, and active balance is free to pull a task off its preferred LLC. Active balance only moves runnable tasks, and this is the only LLC check it consults: once the stopper runs, LBF_ACTIVE_LB skips the per-task test in can_migrate_task().
Fix it on the counter side. Adjust nr_pref_llc_running in set_delayed() and clear_delayed(), the same places that adjust h_nr_runnable, so both exclude delayed tasks.
This needs care to not count a task twice. set_delayed() removes the task from nr_pref_llc_running when it sleeps. Later, when the task is really dequeued, dequeue_entity() calls account_llc_dequeue() and then clear_delayed(). Left as is, account_llc_dequeue() would remove the task a second time and clear_delayed() would add it back. So account_llc_dequeue() skips its decrement while the task is still delayed, and clears pref_llc_queued so clear_delayed() also leaves the count alone. nr_llc_running and sd->llc_counts are not touched and stay on queued semantics.
--- kernel/sched/fair.c | 45 ++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 44 insertions(+), 1 deletion(-)
kernel/sched/fair.c +44 -1
@@ -1544,7 +1544,14 @@ static void account_llc_dequeue(struct rq *rq, struct task_struct *p) rq->nr_llc_running--;if (p->pref_llc_queued) { - rq->nr_pref_llc_running--; +/* +* If the task is being finally dequeued while still delayed, +* set_delayed() already removed it from nr_pref_llc_running; +* skip here to avoid underflow. Clearing pref_llc_queued also +* stops the subsequent clear_delayed() from re-adding it. +*/ +if (!p->se.sched_delayed) + rq->nr_pref_llc_running--;/* * Update the status in case * other logic might query
@@ -1572,6 +1579,24 @@ static void account_llc_dequeue(struct rq *rq, struct task_struct *p) } } +/* +* A task becoming delay-dequeued leaves the runnable set while staying +* queued. Keep nr_pref_llc_running in the runnable domain (like +* h_nr_runnable) so alb_break_llc() can compare the two directly. +*/ +static void account_llc_delayed (struct rq *rq,struct task_struct *p) +{ +if (p->pref_llc_queued) + rq->nr_pref_llc_running--; +} + +/* A delay-dequeued task becoming runnable again rejoins the count. */ +static void account_llc_requeue_delayed (struct rq *rq,struct task_struct *p) +{ +if (p->pref_llc_queued) + rq->nr_pref_llc_running++; +} +void mm_init_sched (struct mm_struct *mm,struct sched_cache_time __percpu *_pcpu_sched) {
@@ -1969,6 +1994,10 @@ static void account_llc_enqueue(struct rq *rq, struct task_struct *p) {}static void account_llc_dequeue (struct rq *rq,struct task_struct *p) {} +static void account_llc_delayed (struct rq *rq,struct task_struct *p) {} + +static void account_llc_requeue_delayed (struct rq *rq,struct task_struct *p) {} +#endif /* CONFIG_SCHED_CACHE */ /*
@@ -6203,6 +6232,13 @@ static void set_delayed(struct sched_entity *se)if (!entity_is_task (se))return ; +/* +* A delayed task is queued but no longer runnable. Drop it from +* nr_pref_llc_running so that counter keeps runnable semantics and +* stays comparable with h_nr_runnable in alb_break_llc(). +*/ +account_llc_delayed (rq_of (cfs_rq_of (se)),task_of (se)); +for_each_sched_entity (se) {struct cfs_rq *cfs_rq =cfs_rq_of (se);
@@ -6223,6 +6259,13 @@ static void clear_delayed(struct sched_entity *se)if (!entity_is_task (se))return ; +/* +* Re-add on wake (requeue_delayed_entity). On the final delayed +* dequeue, account_llc_dequeue() has already cleared pref_llc_queued, +* so this correctly does nothing. +*/ +account_llc_requeue_delayed (rq_of (cfs_rq_of (se)),task_of (se)); +for_each_sched_entity (se) {struct cfs_rq *cfs_rq =cfs_rq_of (se);
base-commit: a13c140cc289c0b7b3770bce5b3ad42ab35074aa
Signature · 1 line
2.53.0
Zhan Xusheng
Reviewed-byRe: [PATCH] sched/cache: Keep nr_pref_llc_running in the runnable domain
From: Zhan Xusheng <[email protected]>
The ordering the skip depends on is not visible in the diff, so for the
record: dequeue_hierarchy() at fair.c:8148 reaches account_llc_dequeue() through dequeue_entity(), and clear_delayed() runs only afterwards at 8164, so se->sched_delayed is still set when account_llc_dequeue() tests it.
The sequences balance:
enqueue, dequeue +1, -1
enqueue, sleep, wake, dequeue +1, -1 set_delayed,
+1 clear_delayed, -1
enqueue, sleep, real dequeue +1, -1, skip, no re-add
load balance moves a delayed task source unchanged, destination +1
with sched_delayed already clear
requeue_delayed_entity() does not reach account_entity_enqueue(), doing only __enqueue_entity() and clear_delayed(), so the wake path cannot double count. account_mm_sched() cannot see a delayed task either, since task_running_on_cpu() at fair.c:12245 requires task_on_rq_queued() on the current cpu and the caller is update_curr().
sd->llc_counts is right to stay on queued semantics: both readers compare it against another llc_counts value, at fair.c:11895 and 13228, so delayed tasks shift both sides alike.
Thanks,
Zhan Xusheng
Re: [PATCH] sched/cache: Keep nr_pref_llc_running in the runnable domain
Quoted 17 lines · Zhan Xusheng
From: Zhan Xusheng <[email protected]>
The ordering the skip depends on is not visible in the diff, so for the
record:dequeue_hierarchy()at fair.c:8148 reachesaccount_llc_dequeue()throughdequeue_entity(), andclear_delayed()runs only afterwards at 8164, sose->sched_delayedis still set whenaccount_llc_dequeue()tests it.The sequences balance:
enqueue, dequeue +1, -1
enqueue, sleep, wake, dequeue +1, -1set_delayed,+1 clear_delayed, -1enqueue, sleep, real dequeue +1, -1, skip, no re-add
load balance moves a delayed task source unchanged, destination +1
withsched_delayedalready clear
Quoted 5 lines
requeue_delayed_entity()does not reachaccount_entity_enqueue(), doing only__enqueue_entity()andclear_delayed(), so the wake path cannot double count.account_mm_sched()cannot see a delayed task either, sincetask_running_on_cpu()at fair.c:12245 requirestask_on_rq_queued()on the current cpu and the caller isupdate_curr().
Thanks for reviewing the patch proposed to fix the problem you pointed out, and validating that the nr_pref_llc_running accounting under the various scenarios are sane.
Quoted 5 lines
sd->llc_countsis right to stay on queued semantics: both readers compare it against anotherllc_countsvalue, at fair.c:11895 and 13228, so delayed tasks shift both sides alike.
Yes, sd->llc_counts do not need to change as you pointed out.
Quoted 2 lines
Reviewed-by Zhan Xusheng <[email protected]>
Tim
Quoted 13 lines · Tim Chen
On Thu, 2026-08-27 at 21:50 +0800, Zhan Xusheng wrote:
Signed-off-by Tim Chen <[email protected]>--- kernel/sched/fair.c | 45 ++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 44 insertions(+), 1 deletion(-)kernel/sched/fair.c +0
-0@@ -1544,7 +1544,14 @@ static void account_llc_dequeue(struct rq *rq, struct task_struct *p) rq->nr_llc_running--;
kernel/sched/fair.c static void account_llc_dequeue(struct rq *rq, struct task_struct *p) · line 1554
Quoted 10 lines
kernel/sched/fair.c +8
-1if (p->pref_llc_queued) { - rq->nr_pref_llc_running--; +/* +* If the task is being finally dequeued while still delayed, +* set_delayed() already removed it from nr_pref_llc_running; +* skip here to avoid underflow. Clearing pref_llc_queued also +* stops the subsequent clear_delayed() from re-adding it. +*/ +if (!p->se.sched_delayed) + rq->nr_pref_llc_running--;
Should we also do similar check in account_llc_enqueue()? It is possible during load balance migration, a migrate_load allows the task to be migrated across CPUs. And load balance leverages detach_tasks()/attach_tasks() to move tasks. During this stage the p->se.sched_delayed remained unchanged:
In enqueue_task_fair(), only when if (se->on_rq && se->sched_delayed) is true, p->se.sched_delayed will be cleared by requeue_delayed_entity() - during migration, se->on_rq is false.
As a result, attach_tasks -> enqueue_hierarchy -> enqueue_entity -> account_llc_enqueue(rq, p) incorrectly increase rq->nr_pref_llc_running - even that task is in delayed state, and not runnable.
kernel/sched/fair.c static void account_llc_dequeue(struct rq *rq, struct task_struct *p) · line 1587
Quoted 13 lines
kernel/sched/fair.c +5
-0/* * Update the status in case * other logic might query @@ -1572,6 +1579,24 @@ static void account_llc_dequeue(struct rq *rq, struct task_struct *p) } } +/* +* A task becoming delay-dequeued leaves the runnable set while staying +* queued. Keep nr_pref_llc_running in the runnable domain (like +* h_nr_runnable) so alb_break_llc() can compare the two directly. +*/
Regarding alb_break_llc(), since we have compared nr_pref_llc_running vs nr_runnable, I wonder if we should also change the code?
if (env->src_rq->nr_pref_llc_running &&
env->src_rq->nr_pref_llc_running == env->src_rq->cfs.h_nr_runnable)
unsigned long util = 0;
struct task_struct *cur;
if (env->src_rq->cfs.h_nr_runnable <= 1)
return true;thanks,
Chenyu
Quoted 25 lines · Chen Yu
Quoted 13 lines · Tim Chen
On Thu, 2026-08-27 at 21:50 +0800, Zhan Xusheng wrote:
Signed-off-by Tim Chen <[email protected]>--- kernel/sched/fair.c | 45 ++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 44 insertions(+), 1 deletion(-)kernel/sched/fair.c +0
-0@@ -1544,7 +1544,14 @@ static void account_llc_dequeue(struct rq *rq, struct task_struct *p) rq->nr_llc_running--;kernel/sched/fair.c static void account_llc_dequeue(struct rq *rq, struct task_struct *p) · line 1554
Quoted 10 lines
kernel/sched/fair.c +8
-1if (p->pref_llc_queued) { - rq->nr_pref_llc_running--; +/* +* If the task is being finally dequeued while still delayed, +* set_delayed() already removed it from nr_pref_llc_running; +* skip here to avoid underflow. Clearing pref_llc_queued also +* stops the subsequent clear_delayed() from re-adding it. +*/ +if (!p->se.sched_delayed) + rq->nr_pref_llc_running--;
Quoted 7 lines
Should we also do similar check in
account_llc_enqueue()? It is possible during load balance migration, amigrate_loadallows the task to be migrated across CPUs. And load balance leverages detach_tasks()/attach_tasks() to move tasks. During this stage thep->se.sched_delayedremained unchanged:
Inenqueue_task_fair(), only when if (se->on_rq&&se->sched_delayed) is true,p->se.sched_delayedwill be cleared byrequeue_delayed_entity()- during migration,se->on_rqis false.
Yes, you're right. We should extend the sched_delayed check also to enqueue.
Quoted 21 lines
As a result,
attach_tasks->enqueue_hierarchy->enqueue_entity->account_llc_enqueue(rq, p)incorrectly increaserq->nr_pref_llc_running- even that task is in delayed state, and not runnable.kernel/sched/fair.c static void account_llc_dequeue(struct rq *rq, struct task_struct *p) · line 1587
Quoted 13 lines
kernel/sched/fair.c +5
-0/* * Update the status in case * other logic might query @@ -1572,6 +1579,24 @@ static void account_llc_dequeue(struct rq *rq, struct task_struct *p) } } +/* +* A task becoming delay-dequeued leaves the runnable set while staying +* queued. Keep nr_pref_llc_running in the runnable domain (like +* h_nr_runnable) so alb_break_llc() can compare the two directly. +*/ Regarding
alb_break_llc(), since we have comparednr_pref_llc_runningvsnr_runnable, I wonder if we should also change the code?
Quoted 7 lines
if (env->src_rq->nr_pref_llc_running &&
env->src_rq->nr_pref_llc_running == env->src_rq->cfs.h_nr_runnable)
unsigned long util = 0;
struct task_struct *cur;
if (env->src_rq->cfs.h_nr_runnable <= 1)
return true;We already know that there is at least a cfs running task preferring src LLC.
And if there is only 1 running task, (i.e. env->src_rq->nr_running <= 1) then env->src_rq->>cfs.h_nr_runnable should be also 1 here. So switching the check to use cfs.h_nr_runnable should have the same effect.
So I'll leave that change out for now.
The patch is updated as below. Chen Yu and Xusheng, please add your reviewed by if the updated patch looks good.
Thanks.
Tim
---
From 05d33ae88ba1348ec53ede00c405e1100747f028 Mon Sep 17 00:00:00 2001
Message-Id: <05d33ae88ba1348ec53ede00c405e1100747f028.1788294281.git.tim.c.chen@linux.intel.com>
From: Tim Chen <[email protected]>
Date: Thu, 27 Aug 2026 11:20:31 -0700
Subject: [PATCH v2] sched/cache: Keep nr_pref_llc_running in the runnable domain
alb_break_llc() decides whether to break LLC preference during active load balance. It does so by testing that every runnable fair task on the source rq prefers its LLC:
env->src_rq->nr_pref_llc_running == env->src_rq->cfs.h_nr_runnable
But the two counters cover different sets. nr_pref_llc_running is updated in account_llc_enqueue()/account_llc_dequeue(), next to cfs_rq->nr_queued, so it follows queued tasks. h_nr_runnable is updated in set_delayed()/ clear_delayed() and drops delay-dequeued tasks.
So under DELAY_DEQUEUE, a preferring task that goes to sleep stays counted in nr_pref_llc_running while h_nr_runnable falls. The equality then breaks, alb_break_llc() returns false, and active balance is free to pull a task off its preferred LLC. Active balance only moves runnable tasks, and this is the only LLC check it consults: once the stopper runs, LBF_ACTIVE_LB skips the per-task test in can_migrate_task(). The runnable set is the one we want.
Fix it on the counter side. Adjust nr_pref_llc_running in set_delayed() and clear_delayed(), the same places that adjust h_nr_runnable, so both exclude delayed tasks.
This needs care to not count a task twice. set_delayed() removes the task from nr_pref_llc_running when it sleeps. Later, when the task is really dequeued, dequeue_entity() calls account_llc_dequeue() and then clear_delayed(). Left as is, account_llc_dequeue() would remove the task a second time and clear_delayed() would add it back. So account_llc_dequeue() skips its decrement while the task is still delayed, and clears pref_llc_queued so clear_delayed() also leaves the count alone. nr_llc_running and sd->llc_counts are not touched and stay on queued semantics.
---
Changes in v2:
- Prevent delay dequeued task from counted as running task in enqueue operation.
--- kernel/sched/fair.c | 53 +++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 51 insertions(+), 2 deletions(-)
kernel/sched/fair.c +51 -2
@@ -1549,7 +1549,13 @@ static void account_llc_enqueue(struct rq *rq, struct task_struct *p) pref_llc_queued = (pref_llc ==task_llc (p)); rq->nr_llc_running++; - rq->nr_pref_llc_running += pref_llc_queued; +/* +* If the task is being migrated while delay dequeued, keep it out +* of the runnable-domain nr_pref_llc_running; clear_delayed() -> +* account_llc_requeue_delayed() re-adds it when it wakes. +*/ +if (!p->se.sched_delayed) + rq->nr_pref_llc_running += pref_llc_queued;/* * Record whether p is enqueued on its preferred
@@ -1583,7 +1589,14 @@ static void account_llc_dequeue(struct rq *rq, struct task_struct *p) rq->nr_llc_running--;if (p->pref_llc_queued) { - rq->nr_pref_llc_running--; +/* +* If the task is being finally dequeued while still delayed, +* set_delayed() already removed it from nr_pref_llc_running; +* skip here to avoid underflow. Clearing pref_llc_queued also +* stops the subsequent clear_delayed() from re-adding it. +*/ +if (!p->se.sched_delayed) + rq->nr_pref_llc_running--;/* * Update the status in case * other logic might query
@@ -1611,6 +1624,24 @@ static void account_llc_dequeue(struct rq *rq, struct task_struct *p) } } +/* +* A task becoming delay-dequeued leaves the runnable set while staying +* queued. Keep nr_pref_llc_running in the runnable domain (like +* h_nr_runnable) so alb_break_llc() can compare the two directly. +*/ +static void account_llc_delayed (struct rq *rq,struct task_struct *p) +{ +if (p->pref_llc_queued) + rq->nr_pref_llc_running--; +} + +/* A delay-dequeued task becoming runnable again rejoins the count. */ +static void account_llc_requeue_delayed (struct rq *rq,struct task_struct *p) +{ +if (p->pref_llc_queued) + rq->nr_pref_llc_running++; +} +void mm_init_sched (struct mm_struct *mm,struct sched_cache_time __percpu *_pcpu_sched) {
@@ -2008,6 +2039,10 @@ static void account_llc_enqueue(struct rq *rq, struct task_struct *p) {}static void account_llc_dequeue (struct rq *rq,struct task_struct *p) {} +static void account_llc_delayed (struct rq *rq,struct task_struct *p) {} + +static void account_llc_requeue_delayed (struct rq *rq,struct task_struct *p) {} +#endif /* CONFIG_SCHED_CACHE */ /*
@@ -6392,6 +6427,13 @@ static void set_delayed(struct sched_entity *se)if (!entity_is_task (se))return ; +/* +* A delayed task is queued but no longer runnable. Drop it from +* nr_pref_llc_running so that counter keeps runnable semantics and +* stays comparable with h_nr_runnable in alb_break_llc(). +*/ +account_llc_delayed (rq_of (cfs_rq_of (se)),task_of (se)); +for_each_sched_entity (se) {struct cfs_rq *cfs_rq =cfs_rq_of (se);
@@ -6412,6 +6454,13 @@ static void clear_delayed(struct sched_entity *se)if (!entity_is_task (se))return ; +/* +* Re-add on wake (requeue_delayed_entity). On the final delayed +* dequeue, account_llc_dequeue() has already cleared pref_llc_queued, +* so this correctly does nothing. +*/ +account_llc_requeue_delayed (rq_of (cfs_rq_of (se)),task_of (se)); +for_each_sched_entity (se) {struct cfs_rq *cfs_rq =cfs_rq_of (se);
Signature · 1 line
2.32.0
kernel/sched/fair.c static void account_llc_enqueue(struct rq *rq, struct task_struct *p) · line 1552
Quoted 10 lines · Tim Chen
kernel/sched/fair.c +0
-1@@ -1549,7 +1549,13 @@ static void account_llc_enqueue(struct rq *rq, struct task_struct *p) pref_llc_queued = (pref_llc ==task_llc (p)); rq->nr_llc_running++; - rq->nr_pref_llc_running += pref_llc_queued;
Since the logic in
account_llc_enqueue()/account_llc_dequeue()/ account_llc_delayed()/account_llc_requeue_delayed() are very
similar, can we introduce one helper for them:
static void account_llc_pref_running(struct rq *rq, struct task_struct *p, int delta)
{
if (p->pref_llc_queued && !p->se.sched_delayed)
rq->nr_pref_llc_running += delta;
}kernel/sched/fair.c static void account_llc_enqueue(struct rq *rq, struct task_struct *p) · line 1559
Quoted 8 lines
kernel/sched/fair.c +7
-0+/* +* If the task is being migrated while delay dequeued, keep it out +* of the runnable-domain nr_pref_llc_running; clear_delayed() -> +* account_llc_requeue_delayed() re-adds it when it wakes. +*/ +if (!p->se.sched_delayed) + rq->nr_pref_llc_running += pref_llc_queued;
put after p->pref_llc_queued = pref_llc_queued;account_llc_pref_running(rq, p, 1)
Quoted 5 lines
kernel/sched/fair.c +0
-0/* * Record whether p is enqueued on its preferred @@ -1583,7 +1589,14 @@ static void account_llc_dequeue(struct rq *rq, struct task_struct *p) rq->nr_llc_running--;
kernel/sched/fair.c static void account_llc_dequeue(struct rq *rq, struct task_struct *p) · line 1599
Quoted 10 lines
kernel/sched/fair.c +8
-1if (p->pref_llc_queued) { - rq->nr_pref_llc_running--; +/* +* If the task is being finally dequeued while still delayed, +* set_delayed() already removed it from nr_pref_llc_running; +* skip here to avoid underflow. Clearing pref_llc_queued also +* stops the subsequent clear_delayed() from re-adding it. +*/ +if (!p->se.sched_delayed) + rq->nr_pref_llc_running--;
account_llc_pref_running(rq, p, -1)
kernel/sched/fair.c static void account_llc_dequeue(struct rq *rq, struct task_struct *p) · line 1644
Quoted 13 lines
kernel/sched/fair.c +13
-0+static void account_llc_delayed (struct rq *rq,struct task_struct *p) +{ +if (p->pref_llc_queued) + rq->nr_pref_llc_running--; +} + +/* A delay-dequeued task becoming runnable again rejoins the count. */ +static void account_llc_requeue_delayed (struct rq *rq,struct task_struct *p) +{ +if (p->pref_llc_queued) + rq->nr_pref_llc_running++; +} +
Do not need above anymore.
Quoted 9 lines
kernel/sched/fair.c +4
-0+static void account_llc_delayed (struct rq *rq,struct task_struct *p) {} + +static void account_llc_requeue_delayed (struct rq *rq,struct task_struct *p) {} +#endif /* CONFIG_SCHED_CACHE */ /*
kernel/sched/fair.c static void set_delayed(struct sched_entity *se) · line 6436
Quoted 10 lines
kernel/sched/fair.c +7
-0@@ -6392,6 +6427,13 @@ static void set_delayed(struct sched_entity *se)if (!entity_is_task (se))return ; +/* +* A delayed task is queued but no longer runnable. Drop it from +* nr_pref_llc_running so that counter keeps runnable semantics and +* stays comparable with h_nr_runnable in alb_break_llc(). +*/ +account_llc_delayed (rq_of (cfs_rq_of (se)),task_of (se)); +
Added before the se->sched_delayed is set:account_llc_pref_running(rq_of(cfs_rq_of(se)), task_of(se), -1); se->sched_delayed = 1;
Quoted 4 lines
kernel/sched/fair.c +0
-0for_each_sched_entity (se) {struct cfs_rq *cfs_rq =cfs_rq_of (se);
kernel/sched/fair.c static void clear_delayed(struct sched_entity *se) · line 6463
Quoted 10 lines
kernel/sched/fair.c +7
-0@@ -6412,6 +6454,13 @@ static void clear_delayed(struct sched_entity *se)if (!entity_is_task (se))return ; +/* +* Re-add on wake (requeue_delayed_entity). On the final delayed +* dequeue, account_llc_dequeue() has already cleared pref_llc_queued, +* so this correctly does nothing. +*/ +account_llc_requeue_delayed (rq_of (cfs_rq_of (se)),task_of (se)); +
account_llc_pref_running(rq_of(cfs_rq_of(se)), task_of(se), 1);
thanks,
Chenyu
Quoted 8 lines
kernel/sched/fair.c +0
-0for_each_sched_entity (se) {struct cfs_rq *cfs_rq =cfs_rq_of (se);Signature · 1 line
2.32.0
Quoted 17 lines · Chen Yu
kernel/sched/fair.c static void account_llc_enqueue(struct rq *rq, struct task_struct *p) · line 1552
Quoted 10 lines · Tim Chen
kernel/sched/fair.c +0
-1@@ -1549,7 +1549,13 @@ static void account_llc_enqueue(struct rq *rq, struct task_struct *p) pref_llc_queued = (pref_llc ==task_llc (p)); rq->nr_llc_running++; - rq->nr_pref_llc_running += pref_llc_queued;Since the logic in
account_llc_enqueue()/account_llc_dequeue()/ account_llc_delayed()/account_llc_requeue_delayed() are very
similar, can we introduce one helper for them:
static voidaccount_llc_pref_running(struct rq *rq, struct task_struct *p, int delta)
Quoted 5 lines
{
if (p->pref_llc_queued && !p->se.sched_delayed)
rq->nr_pref_llc_running += delta;
}Good idea - the four sites really are one operation ("if the task is queued on its preferred LLC and runnable, move the counter"), and folding the two conditions into one place is what keeps them from drifting apart later. I've adopted it in v3; account_llc_delayed() and account_llc_requeue_delayed() are gone.
I split it slightly differently: a membership predicate
static bool task_pref_llc_runnable(struct task_struct *p)
{
return p->pref_llc_queued && !p->se.sched_delayed;
}with pref_llc_running_inc()/pref_llc_running_dec() wrappers over it, so the call sites read as inc/dec rather than passing a +1/-1 delta.
Two things to note:
1) I kept the call site comments of pref_llc_running_inc/dec(). The helper name says *what* happens,
but not *why* it is safe across the delay-dequeue transition - that set_delayed() already did the decrement, so account_llc_dequeue() must skip it, and that clearing pref_llc_queued there is what neutralizes the following clear_delayed().
2) The pref_llc_running_dec() placement in set_delayed() is subtle. It has to be before se->sched_delayed = 1 or decrement would not happen. That deserves a comment so no one would move sched_delayed = 1 before the decrement.
Tim
---
From: Tim Chen <[email protected]>
Date: Wed, 3 Sep 2026 09:00:00 -0700
Subject: [PATCH v3] sched/cache: Keep nr_pref_llc_running in the runnable domain
To: Peter Zijlstra <[email protected]>, Ingo Molnar <[email protected]>
Cc: Zhan Xusheng <[email protected]>, [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected]
alb_break_llc() decides whether to break LLC preference during active load balance. It does so by testing that every runnable fair task on the source rq prefers its LLC:
env->src_rq->nr_pref_llc_running == env->src_rq->cfs.h_nr_runnable
But the two counters cover different sets. nr_pref_llc_running is updated in account_llc_enqueue()/account_llc_dequeue(), next to cfs_rq->nr_queued, so it follows queued tasks. h_nr_runnable is updated in set_delayed()/ clear_delayed() and drops delay-dequeued tasks.
So under DELAY_DEQUEUE, a preferring task that goes to sleep stays counted in nr_pref_llc_running while h_nr_runnable falls. The equality then breaks, alb_break_llc() returns false, and active balance is free to pull a task off its preferred LLC. Active balance only moves runnable tasks, and this is the only LLC check it consults: once the stopper runs, LBF_ACTIVE_LB skips the per-task test in can_migrate_task(). The runnable set is the one we want.
Fix it on the counter side. A task should be counted in nr_pref_llc_running exactly while it is both queued on its preferred LLC (pref_llc_queued) and runnable (!sched_delayed). Define that membership once in task_pref_llc_runnable(), and adjust the counter only through pref_llc_running_inc()/pref_llc_running_dec() from the four sites that change either input: account_llc_enqueue(), account_llc_dequeue(), set_delayed() and clear_delayed(). Gating every update on the same predicate keeps the delay, wake and dequeue paths from double-counting or underflowing; see the comments at those sites for the ordering.
nr_llc_running and sd->llc_counts are not touched and stay on queued semantics.
---
Based on v7.3-rc1.
Changes in v3:
- Route every nr_pref_llc_running adjustment through a single membership predicate task_pref_llc_runnable(), with pref_llc_running_inc()/ pref_llc_running_dec() wrappers, instead of four open-coded sites (Chen Yu). Keep the per-site comments that explain the delay-dequeue interaction, and note that set_delayed() must adjust the counter before setting se->sched_delayed.
Changes in v2:
- Prevent a delay-dequeued task from being counted as running in the enqueue path (Chen Yu).
kernel/sched/fair.c | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 50 insertions(+), 2 deletions(-)
kernel/sched/fair.c +50 -2
@@ -1538,6 +1538,28 @@ static bool invalid_llc_nr(struct mm_struct *mm, struct task_struct *p, (scale * per_cpu(sd_llc_size, cpu))); } +/* +* A task counts in nr_pref_llc_running while it is queued on its preferred +* LLC (pref_llc_queued) and runnable (!sched_delayed), keeping the counter in +* the runnable domain so alb_break_llc() can compare it with h_nr_runnable. +*/ +static bool task_pref_llc_runnable (struct task_struct *p) +{ +return p->pref_llc_queued && !p->se.sched_delayed; +} + +static void pref_llc_running_inc (struct rq *rq,struct task_struct *p) +{ +if (task_pref_llc_runnable (p)) + rq->nr_pref_llc_running++; +} + +static void pref_llc_running_dec (struct rq *rq,struct task_struct *p) +{ +if (task_pref_llc_runnable (p)) + rq->nr_pref_llc_running--; +} +static void account_llc_enqueue (struct rq *rq,struct task_struct *p) {int pref_llc, pref_llc_queued;
@@ -1549,7 +1571,6 @@ static void account_llc_enqueue(struct rq *rq, struct task_struct *p) pref_llc_queued = (pref_llc ==task_llc (p)); rq->nr_llc_running++; - rq->nr_pref_llc_running += pref_llc_queued;/* * Record whether p is enqueued on its preferred
@@ -1567,6 +1588,9 @@ static void account_llc_enqueue(struct rq *rq, struct task_struct *p)*/ p->pref_llc_queued = pref_llc_queued; +/* Skipped while delayed; clear_delayed() adds it back on wake. */ +pref_llc_running_inc (rq, p); + sd =rcu_dereference_all (rq->sd);if (sd && (unsigned int )pref_llc < sd->llc_max) sd->llc_counts[pref_llc]++;
@@ -1583,7 +1607,12 @@ static void account_llc_dequeue(struct rq *rq, struct task_struct *p) rq->nr_llc_running--;if (p->pref_llc_queued) { - rq->nr_pref_llc_running--; +/* +* Skipped if still delayed (set_delayed() already removed it); +* clearing pref_llc_queued below also stops clear_delayed() +* from re-adding it. +*/ +pref_llc_running_dec (rq, p);/* * Update the status in case * other logic might query
@@ -2008,6 +2037,10 @@ static void account_llc_enqueue(struct rq *rq, struct task_struct *p) {}static void account_llc_dequeue (struct rq *rq,struct task_struct *p) {} +static void pref_llc_running_inc (struct rq *rq,struct task_struct *p) {} + +static void pref_llc_running_dec (struct rq *rq,struct task_struct *p) {} +#endif /* CONFIG_SCHED_CACHE */ /*
@@ -6382,6 +6415,14 @@ static __always_inline void return_cfs_rq_runtime(struct cfs_rq *cfs_rq);static void set_delayed (struct sched_entity *se) { +/* +* Drop a task leaving the runnable set. Must run before sched_delayed +* is set, or task_pref_llc_runnable() would already exclude it; +* clear_delayed() mirrors this after clearing the flag. +*/ +if (entity_is_task (se)) +pref_llc_running_dec (rq_of (cfs_rq_of (se)),task_of (se)); + se->sched_delayed =1 ;/*
@@ -6412,6 +6453,13 @@ static void clear_delayed(struct sched_entity *se)if (!entity_is_task (se))return ; +/* +* Re-add on wake, after sched_delayed is cleared. On a final delayed +* dequeue account_llc_dequeue() already cleared pref_llc_queued, so +* this does nothing. +*/ +pref_llc_running_inc (rq_of (cfs_rq_of (se)),task_of (se)); +for_each_sched_entity (se) {struct cfs_rq *cfs_rq =cfs_rq_of (se);
Signature · 1 line
2.32.0
Quoted 195 lines · Tim Chen
Good idea - the four sites really are one operation ("if the task is queued on its preferred LLC and runnable, move the counter"), and folding the two conditions into one place is what keeps them from drifting apart later. I've adopted it in v3;
account_llc_delayed()andaccount_llc_requeue_delayed()are gone.I split it slightly differently: a membership predicate
static bool task_pref_llc_runnable(struct task_struct *p) { return p->pref_llc_queued && !p->se.sched_delayed; }with pref_llc_running_inc()/pref_llc_running_dec() wrappers over it, so the call sites read as inc/dec rather than passing a +1/-1 delta.
Two things to note:
1) I kept the call site comments of pref_llc_running_inc/dec(). The helper name says *what* happens,
but not *why* it is safe across the delay-dequeue transition - thatset_delayed()already did the decrement, soaccount_llc_dequeue()must skip it, and that clearingpref_llc_queuedthere is what neutralizes the followingclear_delayed().2) The
pref_llc_running_dec()placement inset_delayed()is subtle. It has to be beforese->sched_delayed= 1 or decrement would not happen. That deserves a comment so no one would movesched_delayed= 1 before the decrement.
alb_break_llc()decides whether to break LLC preference during active load balance. It does so by testing that every runnable fair task on the source rq prefers its LLC:env->src_rq->nr_pref_llc_running == env->src_rq->cfs.h_nr_runnableBut the two counters cover different sets.
nr_pref_llc_runningis updated in account_llc_enqueue()/account_llc_dequeue(), next tocfs_rq->nr_queued, so it follows queued tasks.h_nr_runnableis updated in set_delayed()/clear_delayed()and drops delay-dequeued tasks.So under
DELAY_DEQUEUE, a preferring task that goes to sleep stays counted innr_pref_llc_runningwhileh_nr_runnablefalls. The equality then breaks,alb_break_llc()returns false, and active balance is free to pull a task off its preferred LLC. Active balance only moves runnable tasks, and this is the only LLC check it consults: once the stopper runs,LBF_ACTIVE_LBskips the per-task test incan_migrate_task(). The runnable set is the one we want.Fix it on the counter side. A task should be counted in
nr_pref_llc_runningexactly while it is both queued on its preferred LLC (pref_llc_queued) and runnable (!sched_delayed). Define that membership once intask_pref_llc_runnable(), and adjust the counter only through pref_llc_running_inc()/pref_llc_running_dec() from the four sites that change either input:account_llc_enqueue(),account_llc_dequeue(),set_delayed()andclear_delayed(). Gating every update on the same predicate keeps the delay, wake and dequeue paths from double-counting or underflowing; see the comments at those sites for the ordering.
nr_llc_runningandsd->llc_countsare not touched and stay on queued semantics.Reported-by Zhan Xusheng <[email protected]>Suggested-by Chen Yu <[email protected]>Signed-off-by Tim Chen <[email protected]>---
Based on v7.3-rc1.Changes in v3:
- Route everynr_pref_llc_runningadjustment through a single membership predicatetask_pref_llc_runnable(), with pref_llc_running_inc()/pref_llc_running_dec()wrappers, instead of four open-coded sites (Chen Yu). Keep the per-site comments that explain the delay-dequeue interaction, and note thatset_delayed()must adjust the counter before settingse->sched_delayed.Changes in v2:
- Prevent a delay-dequeued task from being counted as running in the enqueue path (Chen Yu).kernel/sched/fair.c | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 50 insertions(+), 2 deletions(-)kernel/sched/fair.c +50
-2@@ -1538,6 +1538,28 @@ static bool invalid_llc_nr(struct mm_struct *mm, struct task_struct *p, (scale * per_cpu(sd_llc_size, cpu))); } +/* +* A task counts in nr_pref_llc_running while it is queued on its preferred +* LLC (pref_llc_queued) and runnable (!sched_delayed), keeping the counter in +* the runnable domain so alb_break_llc() can compare it with h_nr_runnable. +*/ +static bool task_pref_llc_runnable (struct task_struct *p) +{ +return p->pref_llc_queued && !p->se.sched_delayed; +} + +static void pref_llc_running_inc (struct rq *rq,struct task_struct *p) +{ +if (task_pref_llc_runnable (p)) + rq->nr_pref_llc_running++; +} + +static void pref_llc_running_dec (struct rq *rq,struct task_struct *p) +{ +if (task_pref_llc_runnable (p)) + rq->nr_pref_llc_running--; +} +static void account_llc_enqueue (struct rq *rq,struct task_struct *p) {int pref_llc, pref_llc_queued;@@ -1549,7 +1571,6 @@ static void account_llc_enqueue(struct rq *rq, struct task_struct *p) pref_llc_queued = (pref_llc ==task_llc (p)); rq->nr_llc_running++; - rq->nr_pref_llc_running += pref_llc_queued;/* * Record whether p is enqueued on its preferred @@ -1567,6 +1588,9 @@ static void account_llc_enqueue(struct rq *rq, struct task_struct *p)*/ p->pref_llc_queued = pref_llc_queued; +/* Skipped while delayed; clear_delayed() adds it back on wake. */ +pref_llc_running_inc (rq, p); + sd =rcu_dereference_all (rq->sd);if (sd && (unsigned int )pref_llc < sd->llc_max) sd->llc_counts[pref_llc]++;@@ -1583,7 +1607,12 @@ static void account_llc_dequeue(struct rq *rq, struct task_struct *p) rq->nr_llc_running--;if (p->pref_llc_queued) { - rq->nr_pref_llc_running--; +/* +* Skipped if still delayed (set_delayed() already removed it); +* clearing pref_llc_queued below also stops clear_delayed() +* from re-adding it. +*/ +pref_llc_running_dec (rq, p);/* * Update the status in case * other logic might query @@ -2008,6 +2037,10 @@ static void account_llc_enqueue(struct rq *rq, struct task_struct *p) {}static void account_llc_dequeue (struct rq *rq,struct task_struct *p) {} +static void pref_llc_running_inc (struct rq *rq,struct task_struct *p) {} + +static void pref_llc_running_dec (struct rq *rq,struct task_struct *p) {} +#endif /* CONFIG_SCHED_CACHE */ /* @@ -6382,6 +6415,14 @@ static __always_inline void return_cfs_rq_runtime(struct cfs_rq *cfs_rq);static void set_delayed (struct sched_entity *se) { +/* +* Drop a task leaving the runnable set. Must run before sched_delayed +* is set, or task_pref_llc_runnable() would already exclude it; +* clear_delayed() mirrors this after clearing the flag. +*/ +if (entity_is_task (se)) +pref_llc_running_dec (rq_of (cfs_rq_of (se)),task_of (se)); + se->sched_delayed =1 ;/* @@ -6412,6 +6453,13 @@ static void clear_delayed(struct sched_entity *se)if (!entity_is_task (se))return ; +/* +* Re-add on wake, after sched_delayed is cleared. On a final delayed +* dequeue account_llc_dequeue() already cleared pref_llc_queued, so +* this does nothing. +*/ +pref_llc_running_inc (rq_of (cfs_rq_of (se)),task_of (se)); +for_each_sched_entity (se) {struct cfs_rq *cfs_rq =cfs_rq_of (se);Signature · 1 line
2.32.0
Yes, I think this version looks good now. While looking back at Xusheng's proposal, I noticed there is another option:
if (env->src_rq->nr_pref_llc_running == env->src_rq->cfs.h_nr_queued) {
...
}May I know why we did not choose this approach, is it because of the following scenario?
Suppose there are 3 queued tasks: p1 and p2 prefer the src_rq, while p3 is a delayed task that also prefers src_rq. In the current implementation, nr_pref_llc_running is 3 and h_nr_runnable is 2, so alb_break_llc() might return false. As a result, active load balance would be triggered, and p1 or p2 might be migrated away, which is undesirable. However, would this still be a problem after Lu Wang's active load balance guard patch has been applied?
https://lore.kernel.org/lkml/[email protected]/
thanks,
Chenyu
Quoted 199 lines · Chen Yu
Quoted 195 lines · Tim Chen
Good idea - the four sites really are one operation ("if the task is queued on its preferred LLC and runnable, move the counter"), and folding the two conditions into one place is what keeps them from drifting apart later. I've adopted it in v3;
account_llc_delayed()andaccount_llc_requeue_delayed()are gone.I split it slightly differently: a membership predicate
static bool task_pref_llc_runnable(struct task_struct *p) { return p->pref_llc_queued && !p->se.sched_delayed; }with pref_llc_running_inc()/pref_llc_running_dec() wrappers over it, so the call sites read as inc/dec rather than passing a +1/-1 delta.
Two things to note:
1) I kept the call site comments of pref_llc_running_inc/dec(). The helper name says *what* happens,
but not *why* it is safe across the delay-dequeue transition - thatset_delayed()already did the decrement, soaccount_llc_dequeue()must skip it, and that clearingpref_llc_queuedthere is what neutralizes the followingclear_delayed().2) The
pref_llc_running_dec()placement inset_delayed()is subtle. It has to be beforese->sched_delayed= 1 or decrement would not happen. That deserves a comment so no one would movesched_delayed= 1 before the decrement.
alb_break_llc()decides whether to break LLC preference during active load balance. It does so by testing that every runnable fair task on the source rq prefers its LLC:env->src_rq->nr_pref_llc_running == env->src_rq->cfs.h_nr_runnableBut the two counters cover different sets.
nr_pref_llc_runningis updated in account_llc_enqueue()/account_llc_dequeue(), next tocfs_rq->nr_queued, so it follows queued tasks.h_nr_runnableis updated in set_delayed()/clear_delayed()and drops delay-dequeued tasks.So under
DELAY_DEQUEUE, a preferring task that goes to sleep stays counted innr_pref_llc_runningwhileh_nr_runnablefalls. The equality then breaks,alb_break_llc()returns false, and active balance is free to pull a task off its preferred LLC. Active balance only moves runnable tasks, and this is the only LLC check it consults: once the stopper runs,LBF_ACTIVE_LBskips the per-task test incan_migrate_task(). The runnable set is the one we want.Fix it on the counter side. A task should be counted in
nr_pref_llc_runningexactly while it is both queued on its preferred LLC (pref_llc_queued) and runnable (!sched_delayed). Define that membership once intask_pref_llc_runnable(), and adjust the counter only through pref_llc_running_inc()/pref_llc_running_dec() from the four sites that change either input:account_llc_enqueue(),account_llc_dequeue(),set_delayed()andclear_delayed(). Gating every update on the same predicate keeps the delay, wake and dequeue paths from double-counting or underflowing; see the comments at those sites for the ordering.
nr_llc_runningandsd->llc_countsare not touched and stay on queued semantics.Reported-by Zhan Xusheng <[email protected]>Suggested-by Chen Yu <[email protected]>Signed-off-by Tim Chen <[email protected]>---
Based on v7.3-rc1.Changes in v3:
- Route everynr_pref_llc_runningadjustment through a single membership predicatetask_pref_llc_runnable(), with pref_llc_running_inc()/pref_llc_running_dec()wrappers, instead of four open-coded sites (Chen Yu). Keep the per-site comments that explain the delay-dequeue interaction, and note thatset_delayed()must adjust the counter before settingse->sched_delayed.Changes in v2:
- Prevent a delay-dequeued task from being counted as running in the enqueue path (Chen Yu).kernel/sched/fair.c | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 50 insertions(+), 2 deletions(-)kernel/sched/fair.c +50
-2@@ -1538,6 +1538,28 @@ static bool invalid_llc_nr(struct mm_struct *mm, struct task_struct *p, (scale * per_cpu(sd_llc_size, cpu))); } +/* +* A task counts in nr_pref_llc_running while it is queued on its preferred +* LLC (pref_llc_queued) and runnable (!sched_delayed), keeping the counter in +* the runnable domain so alb_break_llc() can compare it with h_nr_runnable. +*/ +static bool task_pref_llc_runnable (struct task_struct *p) +{ +return p->pref_llc_queued && !p->se.sched_delayed; +} + +static void pref_llc_running_inc (struct rq *rq,struct task_struct *p) +{ +if (task_pref_llc_runnable (p)) + rq->nr_pref_llc_running++; +} + +static void pref_llc_running_dec (struct rq *rq,struct task_struct *p) +{ +if (task_pref_llc_runnable (p)) + rq->nr_pref_llc_running--; +} +static void account_llc_enqueue (struct rq *rq,struct task_struct *p) {int pref_llc, pref_llc_queued;@@ -1549,7 +1571,6 @@ static void account_llc_enqueue(struct rq *rq, struct task_struct *p) pref_llc_queued = (pref_llc ==task_llc (p)); rq->nr_llc_running++; - rq->nr_pref_llc_running += pref_llc_queued;/* * Record whether p is enqueued on its preferred @@ -1567,6 +1588,9 @@ static void account_llc_enqueue(struct rq *rq, struct task_struct *p)*/ p->pref_llc_queued = pref_llc_queued; +/* Skipped while delayed; clear_delayed() adds it back on wake. */ +pref_llc_running_inc (rq, p); + sd =rcu_dereference_all (rq->sd);if (sd && (unsigned int )pref_llc < sd->llc_max) sd->llc_counts[pref_llc]++;@@ -1583,7 +1607,12 @@ static void account_llc_dequeue(struct rq *rq, struct task_struct *p) rq->nr_llc_running--;if (p->pref_llc_queued) { - rq->nr_pref_llc_running--; +/* +* Skipped if still delayed (set_delayed() already removed it); +* clearing pref_llc_queued below also stops clear_delayed() +* from re-adding it. +*/ +pref_llc_running_dec (rq, p);/* * Update the status in case * other logic might query @@ -2008,6 +2037,10 @@ static void account_llc_enqueue(struct rq *rq, struct task_struct *p) {}static void account_llc_dequeue (struct rq *rq,struct task_struct *p) {} +static void pref_llc_running_inc (struct rq *rq,struct task_struct *p) {} + +static void pref_llc_running_dec (struct rq *rq,struct task_struct *p) {} +#endif /* CONFIG_SCHED_CACHE */ /* @@ -6382,6 +6415,14 @@ static __always_inline void return_cfs_rq_runtime(struct cfs_rq *cfs_rq);static void set_delayed (struct sched_entity *se) { +/* +* Drop a task leaving the runnable set. Must run before sched_delayed +* is set, or task_pref_llc_runnable() would already exclude it; +* clear_delayed() mirrors this after clearing the flag. +*/ +if (entity_is_task (se)) +pref_llc_running_dec (rq_of (cfs_rq_of (se)),task_of (se)); + se->sched_delayed =1 ;/* @@ -6412,6 +6453,13 @@ static void clear_delayed(struct sched_entity *se)if (!entity_is_task (se))return ; +/* +* Re-add on wake, after sched_delayed is cleared. On a final delayed +* dequeue account_llc_dequeue() already cleared pref_llc_queued, so +* this does nothing. +*/ +pref_llc_running_inc (rq_of (cfs_rq_of (se)),task_of (se)); +for_each_sched_entity (se) {struct cfs_rq *cfs_rq =cfs_rq_of (se);Signature · 1 line
2.32.0Yes, I think this version looks good now. While looking back at Xusheng's proposal, I noticed there is another option:
Quoted 5 lines
if (env->src_rq->nr_pref_llc_running == env->src_rq->cfs.h_nr_queued) { ... }May I know why we did not choose this approach, is it because of the following scenario?
The reason is that a common condition we are trying to avoid in alb_break_llc() is the following: We have one task T1 running on cpu preferring src LLC and another delay queued task T2 not preferring src LLC and delayed queued.
- runnable domain (current fix): h_nr_runnable == 1, nr_pref_llc_running == 1 → equal → alb_break_llc() true → suppress.
Correct: the only thing actually running here wants to be here; don't rip it away.
- h_nr_queued alternative: h_nr_queued == 2, nr_pref(queued) == 1 → not equal → alb_break_llc() false → proceed to active balance, which would then break T1's locality to relieve an "imbalance"
that is really just a sleeping T2.
Quoted 7 lines
Suppose there are 3 queued tasks: p1 and p2 prefer the
src_rq, while p3 is a delayed task that also preferssrc_rq. In the current implementation,nr_pref_llc_runningis 3 andh_nr_runnableis 2, soalb_break_llc()might return false. As a result, active load balance would be triggered, and p1 or p2 might be migrated away, which is undesirable. However, would this still be a problem after Lu Wang's active load balance guard patch has been applied?
https://lore.kernel.org/lkml/[email protected]/
Lu Wang's patch only mitigate the migrate_llc case but not other migration reasons. We shouldn't have done active balance in the example I gave if we are doing migration for other non migrate_llc reasons.
Tim
Quoted 3 lines
thanks,
Chenyu
Quoted 13 lines · Tim Chen
Quoted 7 lines · Chen Yu
Yes, I think this version looks good now. While looking back at Xusheng's proposal, I noticed there is another option:
if (env->src_rq->nr_pref_llc_running == env->src_rq->cfs.h_nr_queued) { ... }May I know why we did not choose this approach, is it because of the following scenario?
The reason is that a common condition we are trying to avoid in
alb_break_llc()is the following: We have one task T1 running on cpu preferring src LLC and another delay queued task T2 not preferring src LLC and delayed queued.
Quoted 7 lines
- runnable domain (current fix):
h_nr_runnable== 1,nr_pref_llc_running== 1 → equal →alb_break_llc()true → suppress.
Correct: the only thing actually running here wants to be here; don't rip it away.
-h_nr_queuedalternative:h_nr_queued== 2,nr_pref(queued)== 1 → not equal →alb_break_llc()false → proceed to active balance, which would then break T1's locality to relieve an "imbalance"
that is really just a sleeping T2.
Got it, I see.
Quoted 8 lines
Quoted 7 lines
Suppose there are 3 queued tasks: p1 and p2 prefer the
src_rq, while p3 is a delayed task that also preferssrc_rq. In the current implementation,nr_pref_llc_runningis 3 andh_nr_runnableis 2, soalb_break_llc()might return false. As a result, active load balance would be triggered, and p1 or p2 might be migrated away, which is undesirable. However, would this still be a problem after Lu Wang's active load balance guard patch has been applied?
https://lore.kernel.org/lkml/[email protected]/
Quoted 4 lines
Lu Wang's patch only mitigate the
migrate_llccase but not other migration reasons. We shouldn't have done active balance in the example I gave if we are doing migration for other nonmigrate_llcreasons.
Indeed. Got it. I've launched some santify tests, will report back later.
thanks,
Chenyu