Skip to contentSkip to conversation

sched/fair: Fix overutilized update in enqueue_task_fair()

PATCH Applied Quentin Perret 5 participants 7 messages Nov 12, 2020 – Nov 19, 2020 Scheduler

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 sched/fair: Fix overutilized update in enqueue_task_fair()

enqueue_task_fair() attempts to skip the overutilized update for new tasks as their util_avg is not accurate yet. However, the flag we check to do so is overwritten earlier on in the function, which makes the condition pretty much a nop.

Fix this by saving the flag early on.

Fixes 2802bf3cd936 ("sched/fair: Add over-utilization/tipping point

indicator")

---
 kernel/sched/fair.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
kernel/sched/fair.c +2 -1
index 290f9e38378c..f3ee60b92718 100644
@@ -5477,6 +5477,7 @@ enqueue_task_fair(struct rq *rq, struct task_struct *p, int flags)
 	struct cfs_rq *cfs_rq;
 	struct sched_entity *se = &p->se;
 	int idle_h_nr_running = task_has_idle_policy(p);
+	int task_new = !(flags & ENQUEUE_WAKEUP);
 
 	/*
 	 * The code below (indirectly) updates schedutil which looks at
@@ -5549,7 +5550,7 @@ enqueue_task_fair(struct rq *rq, struct task_struct *p, int flags)
 	 * into account, but that is not straightforward to implement,
 	 * and the following generally works well enough in practice.
 	 */
-	if (flags & ENQUEUE_WAKEUP)
+	if (!task_new)
 		update_overutilized_status(rq);
 
 enqueue_throttle:
Signature · 1 line
2.29.2.222.g5d2a92d10f8-goog
maintainer Reviewed-by on v1 1/1
#1 lore raw
Quoted 6 lines · Quentin Perret

enqueue_task_fair() attempts to skip the overutilized update for new tasks as their util_avg is not accurate yet. However, the flag we check to do so is overwritten earlier on in the function, which makes the condition pretty much a nop.

Quoted 6 lines

Fix this by saving the flag early on.

Fixes 2802bf3cd936 ("sched/fair: Add over-utilization/tipping point

indicator")

Quoted 28 lines
---
 kernel/sched/fair.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
kernel/sched/fair.c +2 -2
index 290f9e38378c..f3ee60b92718 100644
@@ -5477,6 +5477,7 @@ enqueue_task_fair(struct rq *rq, struct task_struct *p, int flags)
        struct cfs_rq *cfs_rq;
        struct sched_entity *se = &p->se;
        int idle_h_nr_running = task_has_idle_policy(p);
+       int task_new = !(flags & ENQUEUE_WAKEUP);
 
        /*
         * The code below (indirectly) updates schedutil which looks at
@@ -5549,7 +5550,7 @@ enqueue_task_fair(struct rq *rq, struct task_struct *p, int flags)
         * into account, but that is not straightforward to implement,
         * and the following generally works well enough in practice.
         */
-       if (flags & ENQUEUE_WAKEUP)
+       if (!task_new)
                update_overutilized_status(rq);
 
 enqueue_throttle:
--

2.29.2.222.g5d2a92d10f8-goog

Reviewed-by on v1 1/1
#2 lore raw
In reply to Quentin Perret

Hi Quentin,

Quoted 11 lines · Quentin Perret

enqueue_task_fair() attempts to skip the overutilized update for new tasks as their util_avg is not accurate yet. However, the flag we check to do so is overwritten earlier on in the function, which makes the condition pretty much a nop.

Fix this by saving the flag early on.

Fixes 2802bf3cd936 ("sched/fair: Add over-utilization/tipping point

indicator")

Alternatively: how much does not updating the overutilized status here help us? The next tick will unconditionally update it, which for arm64 is anywhere in the next ]0, 4]ms. That "fake" fork-time util_avg should already be accounted in the rq util_avg, and even if the new task was running the entire time, 4ms doesn't buy us much decay.

Quoted 25 lines
---
 kernel/sched/fair.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
kernel/sched/fair.c +2 -1
index 290f9e38378c..f3ee60b92718 100644
@@ -5477,6 +5477,7 @@ enqueue_task_fair(struct rq *rq, struct task_struct *p, int flags)
      struct cfs_rq *cfs_rq;
      struct sched_entity *se = &p->se;
      int idle_h_nr_running = task_has_idle_policy(p);
+	int task_new = !(flags & ENQUEUE_WAKEUP);
 
      /*
       * The code below (indirectly) updates schedutil which looks at
@@ -5549,7 +5550,7 @@ enqueue_task_fair(struct rq *rq, struct task_struct *p, int flags)
       * into account, but that is not straightforward to implement,
       * and the following generally works well enough in practice.
       */
-	if (flags & ENQUEUE_WAKEUP)
+	if (!task_new)
              update_overutilized_status(rq);
 
 enqueue_throttle:
#3 lore raw
Quoted 5 lines · Valentin Schneider

Alternatively: how much does not updating the overutilized status here help us? The next tick will unconditionally update it, which for arm64 is anywhere in the next ]0, 4]ms. That "fake" fork-time util_avg should already be accounted in the rq util_avg, and even if the new task was running the entire time, 4ms doesn't buy us much decay.

Yes, this is arguably a dodgy hack, which will not save us in a number of cases. The only situation where this helps is for short-lived tasks that will run only once. And this is a sadly common programming pattern.

So yeah, this is not the prettiest thing in the world, but it doesn't cost us much and helps some real-world workloads, so ...

Thanks,
Quentin

#4 lore raw
Quoted 7 lines · Quentin Perret
Quoted 5 lines · Valentin Schneider

Alternatively: how much does not updating the overutilized status here help us? The next tick will unconditionally update it, which for arm64 is anywhere in the next ]0, 4]ms. That "fake" fork-time util_avg should already be accounted in the rq util_avg, and even if the new task was running the entire time, 4ms doesn't buy us much decay.

Quoted 7 lines

Yes, this is arguably a dodgy hack, which will not save us in a number of cases. The only situation where this helps is for short-lived tasks that will run only once. And this is a sadly common programming pattern.

So yeah, this is not the prettiest thing in the world, but it doesn't cost us much and helps some real-world workloads, so ...

Aye aye

Quoted 2 lines

Thanks,
Quentin

maintainer
#5 lore raw
In reply to Quentin Perret
Quoted 9 lines · Quentin Perret

enqueue_task_fair() attempts to skip the overutilized update for new tasks as their util_avg is not accurate yet. However, the flag we check to do so is overwritten earlier on in the function, which makes the condition pretty much a nop.

Fix this by saving the flag early on.

Fixes 2802bf3cd936 ("sched/fair: Add over-utilization/tipping point

indicator")

Fix that wrapping for you :-)

Quoted 2 lines

Thanks!

merge bot Applied
#6 lore raw
In reply to Quentin Perret
The following commit has been merged into the sched/urgent branch of tip:

The following commit has been merged into the sched/urgent branch of tip:

Commit-ID:     8e1ac4299a6e8726de42310d9c1379f188140c71
Gitweb:        https://git.kernel.org/tip/8e1ac4299a6e8726de42310d9c1379f188140c71
Author:        Quentin Perret <[email protected]>
AuthorDate:    Thu, 12 Nov 2020 11:12:01 
Committer:     Peter Zijlstra <[email protected]>
CommitterDate: Tue, 17 Nov 2020 13:15:27 +01:00

sched/fair: Fix overutilized update in enqueue_task_fair()

enqueue_task_fair() attempts to skip the overutilized update for new tasks as their util_avg is not accurate yet. However, the flag we check to do so is overwritten earlier on in the function, which makes the condition pretty much a nop.

Fix this by saving the flag early on.

---
 kernel/sched/fair.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
kernel/sched/fair.c +2 -1
index 8e563cf..56a8ca9 100644
@@ -5477,6 +5477,7 @@ enqueue_task_fair(struct rq *rq, struct task_struct *p, int flags)
 	struct cfs_rq *cfs_rq;
 	struct sched_entity *se = &p->se;
 	int idle_h_nr_running = task_has_idle_policy(p);
+	int task_new = !(flags & ENQUEUE_WAKEUP);
 
 	/*
 	 * The code below (indirectly) updates schedutil which looks at
@@ -5549,7 +5550,7 @@ enqueue_task_fair(struct rq *rq, struct task_struct *p, int flags)
 	 * into account, but that is not straightforward to implement,
 	 * and the following generally works well enough in practice.
 	 */
-	if (flags & ENQUEUE_WAKEUP)
+	if (!task_new)
 		update_overutilized_status(rq);
 
 enqueue_throttle: