Skip to contentSkip to conversation

sched: Fix UCLAMP_FLAG_IDLE setting

PATCH Superseded Quentin Perret 2 participants 4 messages Jun 9, 2021 – Jun 10, 2021 Scheduler

v1 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 sched: Fix UCLAMP_FLAG_IDLE setting

The UCLAMP_FLAG_IDLE flag is set on a runqueue when dequeueing the last active task to maintain the last uclamp.max and prevent blocked util from suddenly becoming visible.

However, there is an asymmetry in how the flag is set and cleared which can lead to having the flag set whilst there are active task on the rq. Specifically, the flag is set in the uclamp_rq_inc() path, which is called at enqueue time, but cleared in the uclamp_rq_dec_id() which is called both when dequeueing and task _and_ during cgroup migrations.

Fix this by setting the flag in the uclamp_rq_inc_id() path to ensure things remain symmetrical.

---
 kernel/sched/core.c | 5 +----
 1 file changed, 1 insertion(+), 4 deletions(-)
kernel/sched/core.c +1 -4
index 5226cc26a095..3b213402798e 100644
@@ -980,6 +980,7 @@ static inline void uclamp_idle_reset(struct rq *rq, enum uclamp_id clamp_id,
 	if (!(rq->uclamp_flags & UCLAMP_FLAG_IDLE))
 		return;
 
+	rq->uclamp_flags &= ~UCLAMP_FLAG_IDLE;
 	WRITE_ONCE(rq->uclamp[clamp_id].value, clamp_value);
 }
 
@@ -1252,10 +1253,6 @@ static inline void uclamp_rq_inc(struct rq *rq, struct task_struct *p)
 
 	for_each_clamp_id(clamp_id)
 		uclamp_rq_inc_id(rq, p, clamp_id);
-
-	/* Reset clamp idle holding when there is one RUNNABLE task */
-	if (rq->uclamp_flags & UCLAMP_FLAG_IDLE)
-		rq->uclamp_flags &= ~UCLAMP_FLAG_IDLE;
 }
 
 static inline void uclamp_rq_dec(struct rq *rq, struct task_struct *p)
Signature · 1 line
2.32.0.272.g935e593368-goog
#1 lore raw
Quoted 10 lines · Quentin Perret

The UCLAMP_FLAG_IDLE flag is set on a runqueue when dequeueing the last active task to maintain the last uclamp.max and prevent blocked util from suddenly becoming visible.

However, there is an asymmetry in how the flag is set and cleared which can lead to having the flag set whilst there are active task on the rq. Specifically, the flag is set in the uclamp_rq_inc() path, which is called at enqueue time, but cleared in the uclamp_rq_dec_id() which is called both when dequeueing and task _and_ during cgroup migrations.

Quoted 5 lines

Fix this by setting the flag in the uclamp_rq_inc_id() path to ensure things remain symmetrical.

Argh, and I think this wants

Fixes e496187da710 ("sched/uclamp: Enforce last task's UCLAMP_MAX")
Reviewed-by on v1 1/1
#2 lore raw
In reply to Quentin Perret
Quoted 8 lines · Quentin Perret

The UCLAMP_FLAG_IDLE flag is set on a runqueue when dequeueing the last active task to maintain the last uclamp.max and prevent blocked util from suddenly becoming visible.

However, there is an asymmetry in how the flag is set and cleared which can lead to having the flag set whilst there are active task on the rq. Specifically, the flag is set in the uclamp_rq_inc() path, which is called at enqueue time, but cleared in the uclamp_rq_dec_id() which is

It is actually the other way around. It is cleared in uclamp_rq_inc() and set in uclamp_rq_dec_id().

Quoted 1 line

called both when dequeueing and task _and_ during cgroup migrations.

Is it cgroup migrations or when cgroup uclamp values are updated you mean?

It is worth being direct and mention that uclamp_update_active() will perform uclamp_rq_dec/inc_id() when the cgroup uclamp values are updated, which would end up with the flag set but not cleared in this path.

Quoted 7 lines

Fix this by setting the flag in the uclamp_rq_inc_id() path to ensure things remain symmetrical.

---

With the commit message fixed.

Thanks!

--
Qais Yousef

Quoted 29 lines
 kernel/sched/core.c | 5 +----
 1 file changed, 1 insertion(+), 4 deletions(-)
kernel/sched/core.c +1 -4
index 5226cc26a095..3b213402798e 100644
@@ -980,6 +980,7 @@ static inline void uclamp_idle_reset(struct rq *rq, enum uclamp_id clamp_id,
 	if (!(rq->uclamp_flags & UCLAMP_FLAG_IDLE))
 		return;
 
+	rq->uclamp_flags &= ~UCLAMP_FLAG_IDLE;
 	WRITE_ONCE(rq->uclamp[clamp_id].value, clamp_value);
 }
 
@@ -1252,10 +1253,6 @@ static inline void uclamp_rq_inc(struct rq *rq, struct task_struct *p)
 
 	for_each_clamp_id(clamp_id)
 		uclamp_rq_inc_id(rq, p, clamp_id);
-
-	/* Reset clamp idle holding when there is one RUNNABLE task */
-	if (rq->uclamp_flags & UCLAMP_FLAG_IDLE)
-		rq->uclamp_flags &= ~UCLAMP_FLAG_IDLE;
 }
 
 static inline void uclamp_rq_dec(struct rq *rq, struct task_struct *p)
Signature · 1 line
2.32.0.272.g935e593368-goog
#3 lore raw
Quoted 13 lines · Qais Yousef
Quoted 8 lines · Quentin Perret

The UCLAMP_FLAG_IDLE flag is set on a runqueue when dequeueing the last active task to maintain the last uclamp.max and prevent blocked util from suddenly becoming visible.

However, there is an asymmetry in how the flag is set and cleared which can lead to having the flag set whilst there are active task on the rq. Specifically, the flag is set in the uclamp_rq_inc() path, which is called at enqueue time, but cleared in the uclamp_rq_dec_id() which is

It is actually the other way around. It is cleared in uclamp_rq_inc() and set in uclamp_rq_dec_id().

Quoted 7 lines
Quoted 1 line

called both when dequeueing and task _and_ during cgroup migrations.

Is it cgroup migrations or when cgroup uclamp values are updated you mean?

It is worth being direct and mention that uclamp_update_active() will perform uclamp_rq_dec/inc_id() when the cgroup uclamp values are updated, which would end up with the flag set but not cleared in this path.

Yep, that's the problematic path.

Quoted 9 lines
Quoted 7 lines

Fix this by setting the flag in the uclamp_rq_inc_id() path to ensure things remain symmetrical.

---

With the commit message fixed.

Ack, this commit message is a mess, I'll rewrite.

Quoted 1 line

Thanks!
Quentin