Skip to contentSkip to conversation

sched/uclamp: Fix getting unreasonable ucalmp_max when rq is idle

PATCH Superseded Xuewen Yan 3 participants 7 messages Jun 18, 2021 – Jun 30, 2021 Scheduler

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Xuewen Yan

author
#0 lore raw

PATCH 1/1 sched/uclamp: Fix getting unreasonable ucalmp_max when rq is idle

From: Xuewen Yan <[email protected]>

Now in uclamp_rq_util_with(), when the task != NULL, the uclamp_max as following:
uc_rq_max = rq->uclamp[UCLAMP_MAX].value;
uc_eff_max = uclamp_eff_value(p, UCLAMP_MAX);
uclamp_max = max{uc_rq_max, uc_eff_max};

Consider the following scenario:
(1)the rq is idle, the uc_rq_max is last task's UCLAMP_MAX; (2)the p's uc_eff_max < uc_rq_max.

The result is the uclamp_max = uc_rq_max instead of uc_eff_max, it is unreasonable.

The scenario often happens in find_energy_efficient_cpu(), when the task has smaller UCLAMP_MAX.

Inserts whether the rq is idle in the uclamp_rq_util_with().

---
 kernel/sched/sched.h | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
kernel/sched/sched.h +4 -1
index a189bec13729..0feef6af89f2 100644
@@ -2550,7 +2550,10 @@ unsigned long uclamp_rq_util_with(struct rq *rq, unsigned long util,
 
 	if (p) {
 		min_util = max(min_util, uclamp_eff_value(p, UCLAMP_MIN));
-		max_util = max(max_util, uclamp_eff_value(p, UCLAMP_MAX));
+		if (rq->uclamp_flags & UCLAMP_FLAG_IDLE)
+			max_util = uclamp_eff_value(p, UCLAMP_MAX);
+		else
+			max_util = max(max_util, uclamp_eff_value(p, UCLAMP_MAX));
 	}
 
 	/*
Signature · 1 line
2.25.1
author
#1 lore raw

+cc Qais, Quentin

Hi Dietmar,

The patch has been around for a long time, and I think it makes sense for EAS. Do you have any suggestions or comments?

Thanks!
xuewen

Quoted 42 lines · Xuewen Yan

From: Xuewen Yan <[email protected]>

Now in uclamp_rq_util_with(), when the task != NULL, the uclamp_max as following:
uc_rq_max = rq->uclamp[UCLAMP_MAX].value;
uc_eff_max = uclamp_eff_value(p, UCLAMP_MAX);
uclamp_max = max{uc_rq_max, uc_eff_max};

Consider the following scenario:
(1)the rq is idle, the uc_rq_max is last task's UCLAMP_MAX; (2)the p's uc_eff_max < uc_rq_max.

The result is the uclamp_max = uc_rq_max instead of uc_eff_max, it is unreasonable.

The scenario often happens in find_energy_efficient_cpu(), when the task has smaller UCLAMP_MAX.

Inserts whether the rq is idle in the uclamp_rq_util_with().

---
 kernel/sched/sched.h | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
kernel/sched/sched.h +4 -2
index a189bec13729..0feef6af89f2 100644
@@ -2550,7 +2550,10 @@ unsigned long uclamp_rq_util_with(struct rq *rq, unsigned long util,
 
        if (p) {
                min_util = max(min_util, uclamp_eff_value(p, UCLAMP_MIN));
-               max_util = max(max_util, uclamp_eff_value(p, UCLAMP_MAX));
+               if (rq->uclamp_flags & UCLAMP_FLAG_IDLE)
+                       max_util = uclamp_eff_value(p, UCLAMP_MAX);
+               else
+                       max_util = max(max_util, uclamp_eff_value(p, UCLAMP_MAX));
        }
 
        /*
--

2.25.1

#2 lore raw
In reply to Xuewen Yan

+Cc Patrick's current address

Quoted 27 lines · Xuewen Yan

From: Xuewen Yan <[email protected]>

Now in uclamp_rq_util_with(), when the task != NULL, the uclamp_max as following:
uc_rq_max = rq->uclamp[UCLAMP_MAX].value;
uc_eff_max = uclamp_eff_value(p, UCLAMP_MAX);
uclamp_max = max{uc_rq_max, uc_eff_max};

Consider the following scenario:
(1)the rq is idle, the uc_rq_max is last task's UCLAMP_MAX; (2)the p's uc_eff_max < uc_rq_max.

The result is the uclamp_max = uc_rq_max instead of uc_eff_max, it is unreasonable.

The scenario often happens in find_energy_efficient_cpu(), when the task has smaller UCLAMP_MAX.

Inserts whether the rq is idle in the uclamp_rq_util_with().

---
 kernel/sched/sched.h | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
kernel/sched/sched.h +0 -0
index a189bec13729..0feef6af89f2 100644

kernel/sched/sched.h unsigned long uclamp_rq_util_with(struct rq *rq, unsigned long util, · line 2556

Quoted 8 lines
kernel/sched/sched.h +4 -1
@@ -2550,7 +2550,10 @@ unsigned long uclamp_rq_util_with(struct rq *rq, unsigned long util,
 
      if (p) {
              min_util = max(min_util, uclamp_eff_value(p, UCLAMP_MIN));
-		max_util = max(max_util, uclamp_eff_value(p, UCLAMP_MAX));
+		if (rq->uclamp_flags & UCLAMP_FLAG_IDLE)
+			max_util = uclamp_eff_value(p, UCLAMP_MAX);
+		else
+			max_util = max(max_util, uclamp_eff_value(p, UCLAMP_MAX));

That makes sense to me - enqueuing the task will lift UCLAMP_FLAG_IDLE and set the rq clamp as the task's via uclamp_idle_reset().

Does this want a

Fixes 9d20ad7dfc9a ("sched/uclamp: Add uclamp_util_with()")

?

Also, when we have UCLAMP_FLAG_IDLE, we don't even need to read the rq max
- and I'm pretty sure the same applies to the rq min. What about something like:

---

kernel/sched/sched.h +13 -9
index 6510f0a46789..a2c6f6ae6392 100644
@@ -2833,23 +2833,27 @@ static __always_inline
 unsigned long uclamp_rq_util_with(struct rq *rq, unsigned long util,
 				  struct task_struct *p)
 {
-	unsigned long min_util;
-	unsigned long max_util;
+	unsigned long min_util = 0;
+	unsigned long max_util = 0;
 
 	if (!static_branch_likely(&sched_uclamp_used))
 		return util;
 
-	min_util = READ_ONCE(rq->uclamp[UCLAMP_MIN].value);
-	max_util = READ_ONCE(rq->uclamp[UCLAMP_MAX].value);
-
 	if (p) {
-		min_util = max(min_util, uclamp_eff_value(p, UCLAMP_MIN));
+		min_util = uclamp_eff_value(p, UCLAMP_MIN);
+		max_util = uclamp_eff_value(p, UCLAMP_MAX);
+
+		/*
+		 * Ignore last runnable task's max clamp, as this task will
+		 * reset it. Similarly, no need to read the rq's min clamp.
+		 */
 		if (rq->uclamp_flags & UCLAMP_FLAG_IDLE)
-			max_util = uclamp_eff_value(p, UCLAMP_MAX);
-		else
-			max_util = max(max_util, uclamp_eff_value(p, UCLAMP_MAX));
+			goto out;
 	}
 
+	min_util = max_t(unsigned long, min_util, READ_ONCE(rq->uclamp[UCLAMP_MIN].value));
+	max_util = max_t(unsigned long, max_util, READ_ONCE(rq->uclamp[UCLAMP_MAX].value));
+out:
 	/*
 	 * Since CPU's {min,max}_util clamps are MAX aggregated considering
 	 * RUNNABLE tasks with _different_ clamps, we can end up with an
author
#3 lore raw
Quoted 44 lines · Valentin Schneider

+Cc Patrick's current address

Quoted 27 lines · Xuewen Yan

From: Xuewen Yan <[email protected]>

Now in uclamp_rq_util_with(), when the task != NULL, the uclamp_max as following:
uc_rq_max = rq->uclamp[UCLAMP_MAX].value;
uc_eff_max = uclamp_eff_value(p, UCLAMP_MAX);
uclamp_max = max{uc_rq_max, uc_eff_max};

Consider the following scenario:
(1)the rq is idle, the uc_rq_max is last task's UCLAMP_MAX; (2)the p's uc_eff_max < uc_rq_max.

The result is the uclamp_max = uc_rq_max instead of uc_eff_max, it is unreasonable.

The scenario often happens in find_energy_efficient_cpu(), when the task has smaller UCLAMP_MAX.

Inserts whether the rq is idle in the uclamp_rq_util_with().

---
 kernel/sched/sched.h | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
kernel/sched/sched.h +0 -0
index a189bec13729..0feef6af89f2 100644

kernel/sched/sched.h unsigned long uclamp_rq_util_with(struct rq *rq, unsigned long util, · line 2556

Quoted 8 lines
kernel/sched/sched.h +4 -1
@@ -2550,7 +2550,10 @@ unsigned long uclamp_rq_util_with(struct rq *rq, unsigned long util,
 
      if (p) {
              min_util = max(min_util, uclamp_eff_value(p, UCLAMP_MIN));
-             max_util = max(max_util, uclamp_eff_value(p, UCLAMP_MAX));
+             if (rq->uclamp_flags & UCLAMP_FLAG_IDLE)
+                     max_util = uclamp_eff_value(p, UCLAMP_MAX);
+             else
+                     max_util = max(max_util, uclamp_eff_value(p, UCLAMP_MAX));

That makes sense to me - enqueuing the task will lift UCLAMP_FLAG_IDLE and set the rq clamp as the task's via uclamp_idle_reset().

Quoted 5 lines

Does this want a

Fixes 9d20ad7dfc9a ("sched/uclamp: Add uclamp_util_with()")

?

Yes,add it.

Quoted 3 lines

Also, when we have UCLAMP_FLAG_IDLE, we don't even need to read the rq max
- and I'm pretty sure the same applies to the rq min. What about something like:

Good idea, I'll try it in V2.

Quoted 29 lines

---

kernel/sched/sched.h +8 -6
index 6510f0a46789..a2c6f6ae6392 100644
@@ -2833,23 +2833,27 @@ static __always_inline
 unsigned long uclamp_rq_util_with(struct rq *rq, unsigned long util,
                                  struct task_struct *p)
 {
-       unsigned long min_util;
-       unsigned long max_util;
+       unsigned long min_util = 0;
+       unsigned long max_util = 0;
 
        if (!static_branch_likely(&sched_uclamp_used))
                return util;
 
-       min_util = READ_ONCE(rq->uclamp[UCLAMP_MIN].value);
-       max_util = READ_ONCE(rq->uclamp[UCLAMP_MAX].value);
-
        if (p) {
-               min_util = max(min_util, uclamp_eff_value(p, UCLAMP_MIN));
+               min_util = uclamp_eff_value(p, UCLAMP_MIN);
+               max_util = uclamp_eff_value(p, UCLAMP_MAX);
+
+               /*
+                * Ignore last runnable task's max clamp, as this task will
+                * reset it. Similarly, no need to read the rq's min clamp.

kernel/sched/sched.h static __always_inline · line 2855

Quoted 10 lines
kernel/sched/sched.h +4 -3
+                */
                if (rq->uclamp_flags & UCLAMP_FLAG_IDLE)
-                       max_util = uclamp_eff_value(p, UCLAMP_MAX);
-               else
-                       max_util = max(max_util, uclamp_eff_value(p, UCLAMP_MAX));
+                       goto out;
        }
 
+       min_util = max_t(unsigned long, min_util, READ_ONCE(rq->uclamp[UCLAMP_MIN].value));
+       max_util = max_t(unsigned long, max_util, READ_ONCE(rq->uclamp[UCLAMP_MAX].value));

Is it necessary to use max_t here? although it is not the main problem...

kernel/sched/sched.h static __always_inline · line 2859

Quoted 4 lines
kernel/sched/sched.h +1 -0
+out:
        /*
         * Since CPU's {min,max}_util clamps are MAX aggregated considering
         * RUNNABLE tasks with _different_ clamps, we can end up with an

Thanks!
xuewen

#4 lore raw
Quoted 7 lines · Xuewen Yan

kernel/sched/sched.h static __always_inline · line 2855

Quoted 2 lines · Valentin Schneider
kernel/sched/sched.h +2 -0
+       min_util = max_t(unsigned long, min_util, READ_ONCE(rq->uclamp[UCLAMP_MIN].value));
+       max_util = max_t(unsigned long, max_util, READ_ONCE(rq->uclamp[UCLAMP_MAX].value));

Is it necessary to use max_t here? although it is not the main problem...

I got comparison warnings when using a regular max() - the RQ clamp values are unsigned int, whereas the local variable is unsigned long.

Quoted 7 lines
Quoted 4 lines

+out:
/*
* Since CPU's {min,max}_util clamps are MAX aggregated considering
* RUNNABLE tasks with _different_ clamps, we can end up with an

Thanks!
xuewen

author
#5 lore raw
Quoted 4 lines · Valentin Schneider
Quoted 2 lines · Xuewen Yan

On Tue, Jun 29, 2021 at 9:50 PM Valentin Schneider <[email protected]> wrote:

Quoted 8 lines
Quoted 5 lines

kernel/sched/sched.h static __always_inline · line 2855

Quoted 2 lines
kernel/sched/sched.h +2 -0
+       min_util = max_t(unsigned long, min_util, READ_ONCE(rq->uclamp[UCLAMP_MIN].value));
+       max_util = max_t(unsigned long, max_util, READ_ONCE(rq->uclamp[UCLAMP_MAX].value));

Is it necessary to use max_t here? although it is not the main problem...

I got comparison warnings when using a regular max() - the RQ clamp values are unsigned int, whereas the local variable is unsigned long.

Yes,I miss the rq clamp value's type.

Thanks!
xuewen

#6 lore raw
In reply to Xuewen Yan

Thanks for the CC.

Quoted 46 lines · Xuewen Yan
Quoted 44 lines · Valentin Schneider

+Cc Patrick's current address

Quoted 27 lines · Xuewen Yan

From: Xuewen Yan <[email protected]>

Now in uclamp_rq_util_with(), when the task != NULL, the uclamp_max as following:
uc_rq_max = rq->uclamp[UCLAMP_MAX].value;
uc_eff_max = uclamp_eff_value(p, UCLAMP_MAX);
uclamp_max = max{uc_rq_max, uc_eff_max};

Consider the following scenario:
(1)the rq is idle, the uc_rq_max is last task's UCLAMP_MAX; (2)the p's uc_eff_max < uc_rq_max.

The result is the uclamp_max = uc_rq_max instead of uc_eff_max, it is unreasonable.

The scenario often happens in find_energy_efficient_cpu(), when the task has smaller UCLAMP_MAX.

Inserts whether the rq is idle in the uclamp_rq_util_with().

---
 kernel/sched/sched.h | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
kernel/sched/sched.h +0 -0
index a189bec13729..0feef6af89f2 100644

kernel/sched/sched.h unsigned long uclamp_rq_util_with(struct rq *rq, unsigned long util, · line 2556

Quoted 8 lines
kernel/sched/sched.h +4 -1
@@ -2550,7 +2550,10 @@ unsigned long uclamp_rq_util_with(struct rq *rq, unsigned long util,
 
      if (p) {
              min_util = max(min_util, uclamp_eff_value(p, UCLAMP_MIN));
-             max_util = max(max_util, uclamp_eff_value(p, UCLAMP_MAX));
+             if (rq->uclamp_flags & UCLAMP_FLAG_IDLE)
+                     max_util = uclamp_eff_value(p, UCLAMP_MAX);
+             else
+                     max_util = max(max_util, uclamp_eff_value(p, UCLAMP_MAX));

That makes sense to me - enqueuing the task will lift UCLAMP_FLAG_IDLE and set the rq clamp as the task's via uclamp_idle_reset().

Quoted 7 lines
Quoted 5 lines

Does this want a

Fixes 9d20ad7dfc9a ("sched/uclamp: Add uclamp_util_with()")

?

Yes,add it.

+1

Quoted 4 lines
Quoted 4 lines

Also, when we have UCLAMP_FLAG_IDLE, we don't even need to read the rq max
- and I'm pretty sure the same applies to the rq min. What about something like:

uclamp_min is fine since it defaults to 0. But the suggested improvement looks good to me.

Thanks

--
Qais Yousef