Skip to contentSkip to conversation

sched/fair: Fixup wake_up_sync vs DELAYED_DEQUEUE

RFC PATCH Superseded Xuewen Yan 3 participants 3 messages Feb 26, 2025 – Mar 3, 2025 Scheduler

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Xuewen Yan

author
#0 lore raw

RFC PATCH 1/1 sched/fair: Fixup wake_up_sync vs DELAYED_DEQUEUE

Delayed dequeued feature keeps a sleeping task enqueued until its lag has elapsed. As a result, it stays also visible in rq->nr_running. So when in ake_affine_idle(), we should use the real running-tasks in rq to check whether we should place the wake-up task to current cpu.

Fixes 152e11f6df29 ("sched/fair: Implement delayed dequeue")
---
 kernel/sched/fair.c | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)
kernel/sched/fair.c +9 -2
index 1c0ef435a7aa..2d6d5582c3e9 100644
@@ -7342,8 +7342,15 @@ wake_affine_idle(int this_cpu, int prev_cpu, int sync)
 	if (available_idle_cpu(this_cpu) && cpus_share_cache(this_cpu, prev_cpu))
 		return available_idle_cpu(prev_cpu) ? prev_cpu : this_cpu;
 
-	if (sync && cpu_rq(this_cpu)->nr_running == 1)
-		return this_cpu;
+	if (sync) {
+		struct rq *rq = cpu_rq(this_cpu);
+		int nr_delayed;
+
+		nr_delayed = rq->cfs.h_nr_queued - rq->cfs.h_nr_runnable;
+
+		if ((rq->nr_running - nr_delayed) == 1)
+			return this_cpu;
+	}
 
 	if (available_idle_cpu(prev_cpu))
 		return prev_cpu;
Signature · 1 line
2.25.1
maintainer
#1 lore raw
Quoted 4 lines · Xuewen Yan

Delayed dequeued feature keeps a sleeping task enqueued until its lag has elapsed. As a result, it stays also visible in rq->nr_running. So when in ake_affine_idle(), we should use the real running-tasks

typo: wake_affine_idle

Quoted 2 lines

in rq to check whether we should place the wake-up task to current cpu.

fair enough

Quoted 12 lines
Fixes 152e11f6df29 ("sched/fair: Implement delayed dequeue")
---
 kernel/sched/fair.c | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)
kernel/sched/fair.c +0 -0
index 1c0ef435a7aa..2d6d5582c3e9 100644

kernel/sched/fair.c wake_affine_idle(int this_cpu, int prev_cpu, int sync) · line 7349

Quoted 10 lines
kernel/sched/fair.c +5 -2
@@ -7342,8 +7342,15 @@ wake_affine_idle(int this_cpu, int prev_cpu, int sync)
        if (available_idle_cpu(this_cpu) && cpus_share_cache(this_cpu, prev_cpu))
                return available_idle_cpu(prev_cpu) ? prev_cpu : this_cpu;
 
-       if (sync && cpu_rq(this_cpu)->nr_running == 1)
-               return this_cpu;
+       if (sync) {
+               struct rq *rq = cpu_rq(this_cpu);
+               int nr_delayed;
+
+               nr_delayed = rq->cfs.h_nr_queued - rq->cfs.h_nr_runnable;

Could you encapsulate this in a helper function ? something like below

static inline unsigned int cfs_h_nr_delayed(struct rq *rq)

{
        struct rq *rq = cpu_rq(this_cpu);

        return (rq->cfs.h_nr_queued - rq->cfs.h_nr_runnable);
}
Quoted 11 lines
kernel/sched/fair.c +4 -1
+
+               if ((rq->nr_running - nr_delayed) == 1)
+                       return this_cpu;
+       }
 
        if (available_idle_cpu(prev_cpu))
                return prev_cpu;
--

2.25.1

#2 lore raw
Quoted 39 lines · Vincent Guittot
Quoted 4 lines · Xuewen Yan

Delayed dequeued feature keeps a sleeping task enqueued until its lag has elapsed. As a result, it stays also visible in rq->nr_running. So when in ake_affine_idle(), we should use the real running-tasks

typo: wake_affine_idle

Quoted 2 lines

in rq to check whether we should place the wake-up task to current cpu.

fair enough

Quoted 12 lines
Fixes 152e11f6df29 ("sched/fair: Implement delayed dequeue")
---
 kernel/sched/fair.c | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)
kernel/sched/fair.c +0 -0
index 1c0ef435a7aa..2d6d5582c3e9 100644

kernel/sched/fair.c wake_affine_idle(int this_cpu, int prev_cpu, int sync) · line 7349

Quoted 10 lines
kernel/sched/fair.c +5 -2
@@ -7342,8 +7342,15 @@ wake_affine_idle(int this_cpu, int prev_cpu, int sync)
        if (available_idle_cpu(this_cpu) && cpus_share_cache(this_cpu, prev_cpu))
                return available_idle_cpu(prev_cpu) ? prev_cpu : this_cpu;
 
-       if (sync && cpu_rq(this_cpu)->nr_running == 1)
-               return this_cpu;
+       if (sync) {
+               struct rq *rq = cpu_rq(this_cpu);
+               int nr_delayed;
+
+               nr_delayed = rq->cfs.h_nr_queued - rq->cfs.h_nr_runnable;

Could you encapsulate this in a helper function ? something like below

Quoted 7 lines

static inline unsigned int cfs_h_nr_delayed(struct rq *rq)

{
        struct rq *rq = cpu_rq(this_cpu);

        return (rq->cfs.h_nr_queued - rq->cfs.h_nr_runnable);
}

Okay,or we could add cfs_h_nr_delayed(struct cfs_rq *cfs_rq)? Others can call it with cfs_h_nr_delayed(cfs_rq) or cfs_h_nr_delayed(rq->cfs_q). This way, the helper function will be more versatile.

On the other hand, I found that it's not just the nr-running here that needs to be fixed; other instances of nr-running also need correction, such as in yield_task_fair and sched_fair_update_stop_tick. Perhaps we need to modify a series of patches to address these issues.

BR