Skip to contentSkip to conversation

sched/eevdf: Fix wakeup-preempt by checking cfs_rq->nr_running

PATCH Superseded Chen Yu 4 participants 5 messages Sep 23, 2024 – Sep 25, 2024 Scheduler

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Chen Yu

author
#0 lore raw

PATCH 1/1 sched/eevdf: Fix wakeup-preempt by checking cfs_rq->nr_running

Commit 85e511df3cec ("sched/eevdf: Allow shorter slices to wakeup-preempt") introduced a mechanism that a wakee with shorter slice could preempt the current running task. It also lower the bar for the current task to be preempted, by checking the rq->nr_running instead of cfs_rq->nr_running when the current task has ran out of time slice. Say, if there is 1 cfs task and 1 rt task, before 85e511df3cec, update_deadline() will not trigger a reschedule, and after 85e511df3cec, since rq->nr_running is 2 and resched is true, a resched_curr() would happen.

Some workloads (like the hackbench reported by lkp) do not like over-scheduling. We can see that the preemption rate has been increased by 2.2%:

1.654e+08 +2.2% 1.69e+08 hackbench.time.involuntary_context_switches

Restore its previous check criterion.

Fixes 85e511df3cec ("sched/eevdf: Allow shorter slices to wakeup-preempt")
---
 kernel/sched/fair.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/sched/fair.c +1 -1
index 225b31aaee55..2859fc7e2da2 100644
@@ -1025,7 +1025,7 @@ static bool update_deadline(struct cfs_rq *cfs_rq, struct sched_entity *se)
 	/*
 	 * The task has consumed its request, reschedule.
 	 */
-	return true;
+	return (cfs_rq->nr_running > 1);
 }
 
 #include "pelt.h"
Signature · 1 line
2.25.1
Tested-by on v1 1/1
#1 lore raw

hi, Chenyu,

Quoted 13 lines · Chen Yu

Commit 85e511df3cec ("sched/eevdf: Allow shorter slices to wakeup-preempt") introduced a mechanism that a wakee with shorter slice could preempt the current running task. It also lower the bar for the current task to be preempted, by checking the rq->nr_running instead of cfs_rq->nr_running when the current task has ran out of time slice. Say, if there is 1 cfs task and 1 rt task, before 85e511df3cec, update_deadline() will not trigger a reschedule, and after 85e511df3cec, since rq->nr_running is 2 and resched is true, a resched_curr() would happen.

Some workloads (like the hackbench reported by lkp) do not like over-scheduling. We can see that the preemption rate has been increased by 2.2%:

Quoted 5 lines

1.654e+08 +2.2% 1.69e+08 hackbench.time.involuntary_context_switches

Restore its previous check criterion.

Fixes 85e511df3cec ("sched/eevdf: Allow shorter slices to wakeup-preempt")

we applied this patch upon 85e511df3cec, confirmed the regression is fixed.

=========================================================================================
compiler/cpufreq_governor/ipc/iterations/kconfig/mode/nr_threads/rootfs/tbox_group/testcase:
gcc-12/performance/socket/4/x86_64-rhel-8.3/process/50%/debian-12-x86_64-20240206.cgz/lkp-spr-r02/hackbench

commit:
82e9d0456e06 ("sched/fair: Avoid re-setting virtual deadline on 'migrations'") 85e511df3cec ("sched/eevdf: Allow shorter slices to wakeup-preempt") 1c3386188ad9 ("sched/eevdf: Fix wakeup-preempt by checking cfs_rq->nr_running")

82e9d0456e06cebe 85e511df3cec46021024176672a 1c3386188ad9007c9975470c817 ---------------- --------------------------- ---------------------------

         %stddev     %change         %stddev     %change         %stddev
             \          |                \          |                \
    623219           -13.1%     541887            +0.3%     625154        hackbench.throughput
Quoted 23 lines
---
 kernel/sched/fair.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/sched/fair.c +1 -1
index 225b31aaee55..2859fc7e2da2 100644
@@ -1025,7 +1025,7 @@ static bool update_deadline(struct cfs_rq *cfs_rq, struct sched_entity *se)
 	/*
 	 * The task has consumed its request, reschedule.
 	 */
-	return true;
+	return (cfs_rq->nr_running > 1);
 }
 
 #include "pelt.h"
Signature · 1 line
2.25.1
reviewer Tested-by on v1 1/1
#2 lore raw
In reply to Chen Yu

Hello Chenyu,

Quoted 13 lines · Chen Yu

Commit 85e511df3cec ("sched/eevdf: Allow shorter slices to wakeup-preempt") introduced a mechanism that a wakee with shorter slice could preempt the current running task. It also lower the bar for the current task to be preempted, by checking the rq->nr_running instead of cfs_rq->nr_running when the current task has ran out of time slice. Say, if there is 1 cfs task and 1 rt task, before 85e511df3cec, update_deadline() will not trigger a reschedule, and after 85e511df3cec, since rq->nr_running is 2 and resched is true, a resched_curr() would happen.

Some workloads (like the hackbench reported by lkp) do not like over-scheduling. We can see that the preemption rate has been increased by 2.2%:

Quoted 8 lines

1.654e+08 +2.2% 1.69e+08 hackbench.time.involuntary_context_switches

Restore its previous check criterion.

Fixes 85e511df3cec ("sched/eevdf: Allow shorter slices to wakeup-preempt")

Gave it a spin on my dual socket 3rd Generation EPYC System and I do not as big a jump in hackbench numbers as Oliver reported, most likely because I couldn't emulate the exact scenario where a fair task is running in presence of an RT task queued. Following are numbers from my testing:

==================================================================
Test : hackbench
Units : Normalized time in seconds
Interpretation: Lower is better
Statistic : AMean
==================================================================
Case: tip[pct imp](CV) preempt-fix[pct imp](CV)

 1-groups     1.00 [ -0.00]( 2.60)     1.00 [  0.17]( 2.12)
 2-groups     1.00 [ -0.00]( 1.21)     0.98 [  2.05]( 0.95)
 4-groups     1.00 [ -0.00]( 1.63)     0.97 [  2.65]( 1.53)
 8-groups     1.00 [ -0.00]( 1.34)     0.99 [  0.81]( 1.33)
16-groups     1.00 [ -0.00]( 2.07)     0.98 [  2.31]( 1.09)

--

Feel free to include:

Quoted 9 lines
---
  kernel/sched/fair.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
kernel/sched/fair.c +0 -0
index 225b31aaee55..2859fc7e2da2 100644

kernel/sched/fair.c static bool update_deadline(struct cfs_rq *cfs_rq, struct sched_entity *se) · line 1028

Quoted 5 lines
kernel/sched/fair.c +1 -1
@@ -1025,7 +1025,7 @@ static bool update_deadline(struct cfs_rq *cfs_rq, struct sched_entity *se)
  	/*
  	 * The task has consumed its request, reschedule.
  	 */
-	return true;
+	return (cfs_rq->nr_running > 1);

Was there a strong reason why Peter decided to use "rq->nr_running" instead of "cfs_rq->nr_running" with PREEMPT_SHORT in update_curr()?

I wonder if it was to force a pick_next_task() cycle to dequeue a possibly delayed entity but AFAICT, "cfs_rq->nr_running" should account for the delayed entity still on the cfs_rq and perhaps the early return in update_curr() can just be changed to use "cfs_rq->nr_running". Not sure if I'm missing something trivial.

Quoted 3 lines
kernel/sched/fair.c +0 -0
  }
 
  #include "pelt.h"
Signature · 2 lines
Thanks and Regards,
Prateek
#3 lore raw

Hello Prateek,

Quoted 26 lines · K Prateek Nayak

Hello Chenyu,

Quoted 13 lines · Chen Yu

Commit 85e511df3cec ("sched/eevdf: Allow shorter slices to wakeup-preempt") introduced a mechanism that a wakee with shorter slice could preempt the current running task. It also lower the bar for the current task to be preempted, by checking the rq->nr_running instead of cfs_rq->nr_running when the current task has ran out of time slice. Say, if there is 1 cfs task and 1 rt task, before 85e511df3cec, update_deadline() will not trigger a reschedule, and after 85e511df3cec, since rq->nr_running is 2 and resched is true, a resched_curr() would happen.

Some workloads (like the hackbench reported by lkp) do not like over-scheduling. We can see that the preemption rate has been increased by 2.2%:

Quoted 8 lines

1.654e+08 +2.2% 1.69e+08 hackbench.time.involuntary_context_switches

Restore its previous check criterion.

Fixes 85e511df3cec ("sched/eevdf: Allow shorter slices to wakeup-preempt")
Quoted 4 lines

Gave it a spin on my dual socket 3rd Generation EPYC System and I do not as big a jump in hackbench numbers as Oliver reported, most likely because I couldn't emulate the exact scenario where a fair task is running in presence of an RT task queued.

Actually I did not dig into Oliver's test scenario. While looking at the commit I thought that there could be scenario where a rt and a cfs task together might make a difference in preemption which caused this problem.

Quoted 14 lines

Following are numbers from my
testing:

==================================================================
Test : hackbench
Units : Normalized time in seconds
Interpretation: Lower is better
Statistic : AMean
==================================================================
Case: tip[pct imp](CV) preempt-fix[pct imp](CV)

  1-groups     1.00 [ -0.00]( 2.60)     1.00 [  0.17]( 2.12)
  2-groups     1.00 [ -0.00]( 1.21)     0.98 [  2.05]( 0.95)
  4-groups     1.00 [ -0.00]( 1.63)     0.97 [  2.65]( 1.53)
  8-groups     1.00 [ -0.00]( 1.34)     0.99 [  0.81]( 1.33)
Quoted 7 lines

16-groups 1.00 [ -0.00]( 2.07) 0.98 [ 2.31]( 1.09)
--

Feel free to include:

Thanks for the test!

Quoted 18 lines
Quoted 9 lines
---
  kernel/sched/fair.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
kernel/sched/fair.c +0 -0
index 225b31aaee55..2859fc7e2da2 100644

kernel/sched/fair.c static bool update_deadline(struct cfs_rq *cfs_rq, struct sched_entity *se) · line 1028

Quoted 5 lines
kernel/sched/fair.c +1 -1
@@ -1025,7 +1025,7 @@ static bool update_deadline(struct cfs_rq *cfs_rq, struct sched_entity *se)
      /*
       * The task has consumed its request, reschedule.
       */
-     return true;
+     return (cfs_rq->nr_running > 1);

Was there a strong reason why Peter decided to use "rq->nr_running" instead of "cfs_rq->nr_running" with PREEMPT_SHORT in update_curr()?

Quoted 7 lines

I wonder if it was to force a pick_next_task() cycle to dequeue a possibly delayed entity
but AFAICT, "cfs_rq->nr_running" should
account for the delayed entity still on the cfs_rq and perhaps the early return in update_curr() can just be changed to use "cfs_rq->nr_running". Not sure if I'm missing something trivial.

85e511df3cec changes
if (cfs_rq->nr_running > 1) resched
to
if (rq->nr_running == 1) not_resched
which does lower the bar to trigger resched

Yes, I think your proposal make sense, the resched should only be triggered between 2 cfs tasks,
and the restore to update_deadline() is not needed, something like below in update_curr() could also work:

if (cfs_rq->nr_running == 1)

      return;

thanks,
Chenyu

#4 lore raw

Hello Chenyu,

Quoted 28 lines · Chen Yu

[..snip..]

Quoted 14 lines

kernel/sched/fair.c static bool update_deadline(struct cfs_rq *cfs_rq, struct sched_entity *se) · line 1028

Quoted 10 lines
kernel/sched/fair.c +1 -1
index 225b31aaee55..2859fc7e2da2 100644
@@ -1025,7 +1025,7 @@ static bool update_deadline(struct cfs_rq *cfs_rq, struct sched_entity *se)
       /*
        * The task has consumed its request, reschedule.
        */
-     return true;
+     return (cfs_rq->nr_running > 1);

Was there a strong reason why Peter decided to use "rq->nr_running" instead of "cfs_rq->nr_running" with PREEMPT_SHORT in update_curr()?

Quoted 7 lines

I wonder if it was to force a pick_next_task() cycle to dequeue a possibly delayed entity
but AFAICT, "cfs_rq->nr_running" should
account for the delayed entity still on the cfs_rq and perhaps the early return in update_curr() can just be changed to use "cfs_rq->nr_running". Not sure if I'm missing something trivial.

85e511df3cec changes
if (cfs_rq->nr_running > 1) resched
to
if (rq->nr_running == 1) not_resched
which does lower the bar to trigger resched

Quoted 7 lines

Yes, I think your proposal make sense, the resched should only be triggered between 2 cfs tasks,
and the restore to update_deadline() is not needed, something like below in update_curr() could also work:

if (cfs_rq->nr_running == 1)

       return;

That seems better IMO unless there was a strong reason for the original change to use rq->nr_running :)

Quoted 3 lines

thanks,
Chenyu

Signature · 2 lines
Thanks and Regards,
Prateek