Re: sched/fair: which tasks should nr_pref_llc_running be compared against?
From: Chen Yu
Date: Thu Sep 03 2026 - 13:05:35 EST
On Tue, Sep 01, 2026 at 01:42:58PM -0700, Tim Chen wrote:
>
> diff --git a/kernel/sched/fair.c b/kernel/sched/fair.c
> index 8dff37059faf..84c068f1deec 100644
> --- a/kernel/sched/fair.c
> +++ b/kernel/sched/fair.c
> @@ -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;
}
> + /*
> + * 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)
> /*
> * 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--;
account_llc_pref_running(rq, p, -1)
> +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.
>
> +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));
> +
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;
> 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));
> +
account_llc_pref_running(rq_of(cfs_rq_of(se)), task_of(se), 1);
thanks,
Chenyu
> for_each_sched_entity(se) {
> struct cfs_rq *cfs_rq = cfs_rq_of(se);
>
> --
> 2.32.0
>
>
>