From: Peter Zijlstra <peterz@infradead.org>
To: Vincent Guittot <vincent.guittot@linaro.org>
Cc: Dietmar Eggemann <dietmar.eggemann@arm.com>,
Zhang Qiao <zhangqiao22@huawei.com>,
linux-kernel@vger.kernel.org, mingo@redhat.com,
juri.lelli@redhat.com, rostedt@goodmis.org, bsegall@google.com,
mgorman@suse.de, bristot@redhat.com, vschneid@redhat.com,
rkagan@amazon.de
Subject: Re: [PATCH v2] sched/fair: sanitize vruntime of entity being migrated
Date: Tue, 14 Mar 2023 13:07:26 +0100 [thread overview]
Message-ID: <20230314120726.GG1845660@hirez.programming.kicks-ass.net> (raw)
In-Reply-To: <CAKfTPtBw9SJxVBcN1qff7jRzE81kXSjbc-rXD6goEBFiXEwbyg@mail.gmail.com>
On Tue, Mar 14, 2023 at 08:41:30AM +0100, Vincent Guittot wrote:
> I'm going to use something a bit different from your proposal below by
> merging initial and flag
> static void place_entity(struct cfs_rq *cfs_rq, struct sched_entity
> *se, int flags)
>
> with flags:
> 0 for initial placement
> ENQUEUE_WAKEUP for wakeup
> ENQUEUE_MIGRATED for migrated task
So when a task is not running for a long time (our case at hand), then
there's two cases:
- it wakes up locally and place_entity() gets to reset vruntime;
- it wakes up remotely and migrate_task_rq_fair() can reset vruntime.
So if we can rely on ENQUEUE_MIGRATED to differentiate between these
cases, when wouldn't something like this work?
---
diff --git a/kernel/sched/fair.c b/kernel/sched/fair.c
index 7a1b1f855b96..a0d00b6a8bc6 100644
--- a/kernel/sched/fair.c
+++ b/kernel/sched/fair.c
@@ -4648,11 +4648,31 @@ static void check_spread(struct cfs_rq *cfs_rq, struct sched_entity *se)
#endif
}
+static bool reset_vruntime(struct cfs_rq *cfs_rq, struct sched_entity *se)
+{
+ const s64 limit = 60LL * NSEC_PER_SEC;
+ s64 sleep_time;
+
+ /*
+ * Pull vruntime of the entity being placed to the base level of
+ * cfs_rq, to prevent boosting it if placed backwards. If the entity
+ * slept for a long time, don't even try to compare its vruntime with
+ * the base as it may be too far off and the comparison may get
+ * inversed due to s64 overflow.
+ */
+ sleep_time = rq_clock_task(rq_of(cfs_rq)) - se->exec_start;
+ if (unlikely(sleep_time > limit)) {
+ se->vruntime = cfs_rq->min_vruntime - calc_delta_fair(limit, se);
+ return true;
+ }
+
+ return false;
+}
+
static void
place_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, int initial)
{
u64 vruntime = cfs_rq->min_vruntime;
- u64 sleep_time;
/*
* The 'current' period is already promised to the current tasks,
@@ -4682,18 +4702,8 @@ place_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, int initial)
vruntime -= thresh;
}
- /*
- * Pull vruntime of the entity being placed to the base level of
- * cfs_rq, to prevent boosting it if placed backwards. If the entity
- * slept for a long time, don't even try to compare its vruntime with
- * the base as it may be too far off and the comparison may get
- * inversed due to s64 overflow.
- */
- sleep_time = rq_clock_task(rq_of(cfs_rq)) - se->exec_start;
- if ((s64)sleep_time > 60LL * NSEC_PER_SEC)
- se->vruntime = vruntime;
- else
- se->vruntime = max_vruntime(se->vruntime, vruntime);
+ /* ensure we don't gain time by being placed backwards */
+ se->vruntime = max_vruntime(se->vruntime, vruntime);
}
static void check_enqueue_throttle(struct cfs_rq *cfs_rq);
@@ -4768,8 +4778,11 @@ enqueue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, int flags)
update_cfs_group(se);
account_entity_enqueue(cfs_rq, se);
- if (flags & ENQUEUE_WAKEUP)
+ if (flags & ENQUEUE_WAKEUP) {
+ if (!(flags & ENQUEUE_MIGRATED))
+ reset_vruntime(cfs_rq, se);
place_entity(cfs_rq, se, 0);
+ }
check_schedstat_required();
update_stats_enqueue_fair(cfs_rq, se, flags);
@@ -7625,6 +7638,7 @@ select_task_rq_fair(struct task_struct *p, int prev_cpu, int wake_flags)
static void migrate_task_rq_fair(struct task_struct *p, int new_cpu)
{
struct sched_entity *se = &p->se;
+ struct cfs_rq *cfs_rq = cfs_rq_of(se);
/*
* As blocked tasks retain absolute vruntime the migration needs to
@@ -7632,11 +7646,8 @@ static void migrate_task_rq_fair(struct task_struct *p, int new_cpu)
* min_vruntime -- the latter is done by enqueue_entity() when placing
* the task on the new runqueue.
*/
- if (READ_ONCE(p->__state) == TASK_WAKING) {
- struct cfs_rq *cfs_rq = cfs_rq_of(se);
-
+ if (READ_ONCE(p->__state) == TASK_WAKING || reset_vruntime(cfs_rq, se))
se->vruntime -= u64_u32_load(cfs_rq->min_vruntime);
- }
if (!task_on_rq_migrating(p)) {
remove_entity_load_avg(se);
next prev parent reply other threads:[~2023-03-14 12:09 UTC|newest]
Thread overview: 59+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-03-06 13:24 Zhang Qiao
2023-03-06 13:53 ` Vincent Guittot
2023-03-07 10:26 ` Vincent Guittot
2023-03-07 11:05 ` Zhang Qiao
2023-03-07 13:41 ` Zhang Qiao
2023-03-08 8:01 ` Vincent Guittot
2023-03-08 12:55 ` Vincent Guittot
2023-03-09 8:37 ` Zhang Qiao
2023-03-09 9:09 ` Dietmar Eggemann
2023-03-09 9:30 ` Zhang Qiao
2023-03-09 10:48 ` Vincent Guittot
2023-03-09 14:23 ` Zhang Qiao
2023-03-07 2:16 ` kernel test robot
2023-03-07 12:45 ` Dietmar Eggemann
2023-03-07 14:06 ` Zhang Qiao
2023-03-09 9:43 ` Zhang Qiao
2023-03-08 14:33 ` Chen Yu
2023-03-09 13:05 ` Peter Zijlstra
2023-03-09 13:34 ` Vincent Guittot
2023-03-09 14:28 ` Peter Zijlstra
2023-03-09 14:36 ` Peter Zijlstra
2023-03-09 15:14 ` Vincent Guittot
2023-03-10 14:29 ` Vincent Guittot
2023-03-11 9:57 ` Zhang Qiao
2023-03-13 14:23 ` Vincent Guittot
2023-03-14 11:03 ` Zhang Qiao
2023-03-14 13:26 ` Vincent Guittot
2023-03-14 13:38 ` Zhang Qiao
2023-03-14 13:39 ` Vincent Guittot
2023-03-14 15:32 ` Vincent Guittot
2023-03-15 9:16 ` Zhang Qiao
2023-03-15 15:30 ` Vincent Guittot
2023-03-13 9:06 ` Dietmar Eggemann
2023-03-13 18:17 ` Dietmar Eggemann
2023-03-14 7:41 ` Vincent Guittot
2023-03-14 12:07 ` Peter Zijlstra [this message]
2023-03-14 13:24 ` Vincent Guittot
2023-03-14 17:16 ` Peter Zijlstra
2023-03-15 7:18 ` Vincent Guittot
2023-03-15 8:42 ` Vincent Guittot
2023-03-15 10:15 ` Dietmar Eggemann
2023-03-15 10:21 ` Vincent Guittot
2023-03-15 13:35 ` Dietmar Eggemann
2023-03-15 15:32 ` Vincent Guittot
2023-03-14 13:29 ` Dietmar Eggemann
2023-03-14 13:37 ` Dietmar Eggemann
2023-03-17 16:08 Vincent Guittot
2023-03-18 7:45 ` Zhang Qiao
2023-03-20 12:29 ` Dietmar Eggemann
2023-03-20 13:26 ` Vincent Guittot
2023-03-21 10:02 ` Peter Zijlstra
2023-03-21 10:29 ` Dietmar Eggemann
2023-03-21 10:49 ` Peter Zijlstra
2023-03-21 11:12 ` Vincent Guittot
2023-03-21 11:13 ` Dietmar Eggemann
2023-03-21 12:26 ` Peter Zijlstra
2023-03-21 12:28 ` Peter Zijlstra
2023-03-21 12:38 ` Vincent Guittot
2023-03-24 4:05 ` Chen Yu
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20230314120726.GG1845660@hirez.programming.kicks-ass.net \
--to=peterz@infradead.org \
--cc=bristot@redhat.com \
--cc=bsegall@google.com \
--cc=dietmar.eggemann@arm.com \
--cc=juri.lelli@redhat.com \
--cc=linux-kernel@vger.kernel.org \
--cc=mgorman@suse.de \
--cc=mingo@redhat.com \
--cc=rkagan@amazon.de \
--cc=rostedt@goodmis.org \
--cc=vincent.guittot@linaro.org \
--cc=vschneid@redhat.com \
--cc=zhangqiao22@huawei.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®