Skip to content

Commit 85e511d

Browse files
author
Peter Zijlstra
committed
sched/eevdf: Allow shorter slices to wakeup-preempt
Part of the reason to have shorter slices is to improve responsiveness. Allow shorter slices to preempt longer slices on wakeup. Task | Runtime ms | Switches | Avg delay ms | Max delay ms | Sum delay ms | 100ms massive_intr 500us cyclictest NO_PREEMPT_SHORT 1 massive_intr:(5) | 846018.956 ms | 779188 | avg: 0.273 ms | max: 58.337 ms | sum:212545.245 ms | 2 massive_intr:(5) | 853450.693 ms | 792269 | avg: 0.275 ms | max: 71.193 ms | sum:218263.588 ms | 3 massive_intr:(5) | 843888.920 ms | 771456 | avg: 0.277 ms | max: 92.405 ms | sum:213353.221 ms | 1 chromium-browse:(8) | 53015.889 ms | 131766 | avg: 0.463 ms | max: 36.341 ms | sum:60959.230 ms | 2 chromium-browse:(8) | 53864.088 ms | 136962 | avg: 0.480 ms | max: 27.091 ms | sum:65687.681 ms | 3 chromium-browse:(9) | 53637.904 ms | 132637 | avg: 0.481 ms | max: 24.756 ms | sum:63781.673 ms | 1 cyclictest:(5) | 12615.604 ms | 639689 | avg: 0.471 ms | max: 32.272 ms | sum:301351.094 ms | 2 cyclictest:(5) | 12511.583 ms | 642578 | avg: 0.448 ms | max: 44.243 ms | sum:287632.830 ms | 3 cyclictest:(5) | 12545.867 ms | 635953 | avg: 0.475 ms | max: 25.530 ms | sum:302374.658 ms | 100ms massive_intr 500us cyclictest PREEMPT_SHORT 1 massive_intr:(5) | 839843.919 ms | 837384 | avg: 0.264 ms | max: 74.366 ms | sum:221476.885 ms | 2 massive_intr:(5) | 852449.913 ms | 845086 | avg: 0.252 ms | max: 68.162 ms | sum:212595.968 ms | 3 massive_intr:(5) | 839180.725 ms | 836883 | avg: 0.266 ms | max: 69.742 ms | sum:222812.038 ms | 1 chromium-browse:(11) | 54591.481 ms | 138388 | avg: 0.458 ms | max: 35.427 ms | sum:63401.508 ms | 2 chromium-browse:(8) | 52034.541 ms | 132276 | avg: 0.436 ms | max: 31.826 ms | sum:57732.958 ms | 3 chromium-browse:(8) | 55231.771 ms | 141892 | avg: 0.469 ms | max: 27.607 ms | sum:66538.697 ms | 1 cyclictest:(5) | 13156.391 ms | 667412 | avg: 0.373 ms | max: 38.247 ms | sum:249174.502 ms | 2 cyclictest:(5) | 12688.939 ms | 665144 | avg: 0.374 ms | max: 33.548 ms | sum:248509.392 ms | 3 cyclictest:(5) | 13475.623 ms | 669110 | avg: 0.370 ms | max: 37.819 ms | sum:247673.390 ms | As per the numbers the, this makes cyclictest (short slice) it's max-delay more consistent and consistency drops the sum-delay. The trade-off is that the massive_intr (long slice) gets more context switches and a slight increase in sum-delay. Chunxin contributed did_preempt_short() where a task that lost slice protection from PREEMPT_SHORT gets rescheduled once it becomes in-eligible. [mike: numbers] Co-Developed-by: Chunxin Zang <zangchunxin@lixiang.com> Signed-off-by: Chunxin Zang <zangchunxin@lixiang.com> Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org> Tested-by: Valentin Schneider <vschneid@redhat.com> Tested-by: Mike Galbraith <umgwanakikbuti@gmail.com> Link: https://lkml.kernel.org/r/20240727105030.735459544@infradead.org
1 parent 82e9d04 commit 85e511d

File tree

2 files changed

+61
-8
lines changed

2 files changed

+61
-8
lines changed

kernel/sched/fair.c

Lines changed: 56 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -973,10 +973,10 @@ static void clear_buddies(struct cfs_rq *cfs_rq, struct sched_entity *se);
973973
* XXX: strictly: vd_i += N*r_i/w_i such that: vd_i > ve_i
974974
* this is probably good enough.
975975
*/
976-
static void update_deadline(struct cfs_rq *cfs_rq, struct sched_entity *se)
976+
static bool update_deadline(struct cfs_rq *cfs_rq, struct sched_entity *se)
977977
{
978978
if ((s64)(se->vruntime - se->deadline) < 0)
979-
return;
979+
return false;
980980

981981
/*
982982
* For EEVDF the virtual time slope is determined by w_i (iow.
@@ -993,10 +993,7 @@ static void update_deadline(struct cfs_rq *cfs_rq, struct sched_entity *se)
993993
/*
994994
* The task has consumed its request, reschedule.
995995
*/
996-
if (cfs_rq->nr_running > 1) {
997-
resched_curr(rq_of(cfs_rq));
998-
clear_buddies(cfs_rq, se);
999-
}
996+
return true;
1000997
}
1001998

1002999
#include "pelt.h"
@@ -1134,6 +1131,38 @@ static inline void update_curr_task(struct task_struct *p, s64 delta_exec)
11341131
dl_server_update(p->dl_server, delta_exec);
11351132
}
11361133

1134+
static inline bool did_preempt_short(struct cfs_rq *cfs_rq, struct sched_entity *curr)
1135+
{
1136+
if (!sched_feat(PREEMPT_SHORT))
1137+
return false;
1138+
1139+
if (curr->vlag == curr->deadline)
1140+
return false;
1141+
1142+
return !entity_eligible(cfs_rq, curr);
1143+
}
1144+
1145+
static inline bool do_preempt_short(struct cfs_rq *cfs_rq,
1146+
struct sched_entity *pse, struct sched_entity *se)
1147+
{
1148+
if (!sched_feat(PREEMPT_SHORT))
1149+
return false;
1150+
1151+
if (pse->slice >= se->slice)
1152+
return false;
1153+
1154+
if (!entity_eligible(cfs_rq, pse))
1155+
return false;
1156+
1157+
if (entity_before(pse, se))
1158+
return true;
1159+
1160+
if (!entity_eligible(cfs_rq, se))
1161+
return true;
1162+
1163+
return false;
1164+
}
1165+
11371166
/*
11381167
* Used by other classes to account runtime.
11391168
*/
@@ -1157,6 +1186,7 @@ static void update_curr(struct cfs_rq *cfs_rq)
11571186
struct sched_entity *curr = cfs_rq->curr;
11581187
struct rq *rq = rq_of(cfs_rq);
11591188
s64 delta_exec;
1189+
bool resched;
11601190

11611191
if (unlikely(!curr))
11621192
return;
@@ -1166,7 +1196,7 @@ static void update_curr(struct cfs_rq *cfs_rq)
11661196
return;
11671197

11681198
curr->vruntime += calc_delta_fair(delta_exec, curr);
1169-
update_deadline(cfs_rq, curr);
1199+
resched = update_deadline(cfs_rq, curr);
11701200
update_min_vruntime(cfs_rq);
11711201

11721202
if (entity_is_task(curr)) {
@@ -1184,6 +1214,14 @@ static void update_curr(struct cfs_rq *cfs_rq)
11841214
}
11851215

11861216
account_cfs_rq_runtime(cfs_rq, delta_exec);
1217+
1218+
if (rq->nr_running == 1)
1219+
return;
1220+
1221+
if (resched || did_preempt_short(cfs_rq, curr)) {
1222+
resched_curr(rq);
1223+
clear_buddies(cfs_rq, curr);
1224+
}
11871225
}
11881226

11891227
static void update_curr_fair(struct rq *rq)
@@ -8605,7 +8643,17 @@ static void check_preempt_wakeup_fair(struct rq *rq, struct task_struct *p, int
86058643
cfs_rq = cfs_rq_of(se);
86068644
update_curr(cfs_rq);
86078645
/*
8608-
* XXX pick_eevdf(cfs_rq) != se ?
8646+
* If @p has a shorter slice than current and @p is eligible, override
8647+
* current's slice protection in order to allow preemption.
8648+
*
8649+
* Note that even if @p does not turn out to be the most eligible
8650+
* task at this moment, current's slice protection will be lost.
8651+
*/
8652+
if (do_preempt_short(cfs_rq, pse, se) && se->vlag == se->deadline)
8653+
se->vlag = se->deadline + 1;
8654+
8655+
/*
8656+
* If @p has become the most eligible task, force preemption.
86098657
*/
86108658
if (pick_eevdf(cfs_rq) == pse)
86118659
goto preempt;

kernel/sched/features.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@ SCHED_FEAT(PLACE_REL_DEADLINE, true)
1818
* 0-lag point or until is has exhausted it's slice.
1919
*/
2020
SCHED_FEAT(RUN_TO_PARITY, true)
21+
/*
22+
* Allow wakeup of tasks with a shorter slice to cancel RESPECT_SLICE for
23+
* current.
24+
*/
25+
SCHED_FEAT(PREEMPT_SHORT, true)
2126

2227
/*
2328
* Prefer to schedule the task we woke last (assuming it failed

0 commit comments

Comments
 (0)