sched/uclamp: Fix rq->uclamp_max not set on first enqueue
PATCH 1/1 sched/uclamp: Fix rq->uclamp_max not set on first enqueue
Commit d81ae8aac85c ("sched/uclamp: Fix initialization of struct uclamp_rq") introduced a bug where uclamp_max of the rq is not reset to match the woken up task's uclamp_max when the rq is idle. This only impacts the first wake up after enabling the static key. And it only matters if the uclamp_max of this task is < 1024 since only then its uclamp_max will be effectively ignored.
Fix it by properly initializing rq->uclamp_flags = UCLAMP_FLAG_IDLE to ensure we reset rq uclamp_max when waking up from idle.
--- kernel/sched/core.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/sched/core.c +1 -1
@@ -1929,7 +1929,7 @@ static void __init init_uclamp_rq(struct rq *rq) }; } - rq->uclamp_flags =0 ; + rq->uclamp_flags =UCLAMP_FLAG_IDLE ; }static void __init init_uclamp (void )
Signature · 1 line
2.25.1
Quoted 4 lines · Qais Yousef
Commit
d81ae8aac85c("sched/uclamp: Fix initialization of structuclamp_rq") introduced a bug whereuclamp_maxof the rq is not reset to match the woken up task'suclamp_maxwhen the rq is idle. This only impacts the first wake up after enabling the static key. And it only
Wouldn't that rather be all wakeups after enabling the static key, until the rq goes idle and gains UCLAMP_FLAG_IDLE? e.g. if you enqueue N uclamp_max=512 tasks, the first enqueue flips the static key and the rq max-aggregate will stay at 1024 after the remaining enqueues.
Quoted 7 lines
matters if the
uclamp_maxof this task is < 1024 since only then itsuclamp_maxwill be effectively ignored.Fix it by properly initializing
rq->uclamp_flags=UCLAMP_FLAG_IDLEto ensure we reset rquclamp_maxwhen waking up from idle.Fixes d81ae8aac85c ("sched/uclamp: Fix initialization of struct uclamp_rq")
Looking at this again, I'm starting to think this actually stems from the introduction of the flag:
e496187da710 ("sched/uclamp: Enforce last task's UCLAMP_MAX")
Before the commit you point at, we would still initialize ->uclamp_flags to
0. This was probably hidden by all the activity at boot-time (e.g. just unparking smpboot threads) which yielded an nr_running>0 -> nr_running==0 transition, IOW we'd most likely get UCLAMP_FLAG_IDLE set on a rq before any userspace task could get on there.
The static key would have only made this problem more visible.
Quoted 1 line
Signed-off-by Qais Yousef <[email protected]>
Changelog nitpicking aside:
Quoted 19 lines
--- kernel/sched/core.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)kernel/sched/core.c +1
-2@@ -1929,7 +1929,7 @@ static void __init init_uclamp_rq(struct rq *rq) }; } - rq->uclamp_flags =0 ; + rq->uclamp_flags =UCLAMP_FLAG_IDLE ; }static void __init init_uclamp (void ) --2.25.1
Hi Valentin
Quoted 10 lines · Valentin Schneider
Quoted 4 lines · Qais Yousef
Commit
d81ae8aac85c("sched/uclamp: Fix initialization of structuclamp_rq") introduced a bug whereuclamp_maxof the rq is not reset to match the woken up task'suclamp_maxwhen the rq is idle. This only impacts the first wake up after enabling the static key. And it onlyWouldn't that rather be all wakeups after enabling the static key, until the rq goes idle and gains
UCLAMP_FLAG_IDLE? e.g. if you enqueue N uclamp_max=512 tasks, the first enqueue flips the static key and the rq max-aggregate will stay at 1024 after the remaining enqueues.
Yep. Bad phrasing from my side. How about:
"This is visible from first wake up(s) until the first dequeue to idle after enabling the static key"?
Quoted 14 lines
Quoted 8 lines
matters if the
uclamp_maxof this task is < 1024 since only then itsuclamp_maxwill be effectively ignored.Fix it by properly initializing
rq->uclamp_flags=UCLAMP_FLAG_IDLEto ensure we reset rquclamp_maxwhen waking up from idle.Fixes d81ae8aac85c ("sched/uclamp: Fix initialization of struct uclamp_rq")Looking at this again, I'm starting to think this actually stems from the introduction of the flag:
e496187da710("sched/uclamp: Enforce last task'sUCLAMP_MAX")
Quoted 7 lines
Before the commit you point at, we would still initialize ->uclamp_flags to
0. This was probably hidden by all the activity at boot-time (e.g. just unparking smpboot threads) which yielded an nr_running>0 -> nr_running==0 transition, IOW we'd most likely getUCLAMP_FLAG_IDLEset on a rq before any userspace task could get on there.The static key would have only made this problem more visible.
Hmm. I can't see the sequence of events. I guess you could argue in theory that this commit should have initialized the ->uclamp_flags to UCLAMP_FLAG_IDLE but I think it used to work because uc_rq->value = 0 by default
static inline void uclamp_rq_inc_id(struct rq *rq, struct task_struct *p,
enum uclamp_id clamp_id)
{
...
if (uc_se->value > READ_ONCE(uc_rq->value))
WRITE_ONCE(uc_rq->value, uc_se->value);
}The commit I point to changed makes uc_rq->value = 1024 by default, hence we miss the first update.
I don't mind updating the FIXES tag here, though AFAICT there's no visible side effect from it.
Quoted 5 lines
Quoted 2 lines
Signed-off-by Qais Yousef <[email protected]>Changelog nitpicking aside:
Reviewed-by Valentin Schneider <[email protected]>
Thanks!
--
Qais Yousef
Quoted 14 lines · Qais Yousef
Hi Valentin
Quoted 10 lines · Valentin Schneider
Quoted 4 lines · Qais Yousef
Commit
d81ae8aac85c("sched/uclamp: Fix initialization of structuclamp_rq") introduced a bug whereuclamp_maxof the rq is not reset to match the woken up task'suclamp_maxwhen the rq is idle. This only impacts the first wake up after enabling the static key. And it onlyWouldn't that rather be all wakeups after enabling the static key, until the rq goes idle and gains
UCLAMP_FLAG_IDLE? e.g. if you enqueue N uclamp_max=512 tasks, the first enqueue flips the static key and the rq max-aggregate will stay at 1024 after the remaining enqueues.
Quoted 5 lines
Yep. Bad phrasing from my side. How about:
"This is visible from first wake up(s) until the first dequeue to idle after enabling the static key"?
Sounds good.
Quoted 35 lines
Quoted 14 lines
Quoted 8 lines
matters if the
uclamp_maxof this task is < 1024 since only then itsuclamp_maxwill be effectively ignored.Fix it by properly initializing
rq->uclamp_flags=UCLAMP_FLAG_IDLEto ensure we reset rquclamp_maxwhen waking up from idle.Fixes d81ae8aac85c ("sched/uclamp: Fix initialization of struct uclamp_rq")Looking at this again, I'm starting to think this actually stems from the introduction of the flag:
e496187da710("sched/uclamp: Enforce last task'sUCLAMP_MAX")Quoted 7 lines
Before the commit you point at, we would still initialize ->uclamp_flags to
0. This was probably hidden by all the activity at boot-time (e.g. just unparking smpboot threads) which yielded an nr_running>0 -> nr_running==0 transition, IOW we'd most likely getUCLAMP_FLAG_IDLEset on a rq before any userspace task could get on there.The static key would have only made this problem more visible.
Hmm. I can't see the sequence of events. I guess you could argue in theory that this commit should have initialized the ->uclamp_flags to
UCLAMP_FLAG_IDLEbut I think it used to work becauseuc_rq->value= 0 by defaultstatic inline void uclamp_rq_inc_id(struct rq *rq, struct task_struct *p, enum uclamp_id clamp_id) { ... if (uc_se->value > READ_ONCE(uc_rq->value)) WRITE_ONCE(uc_rq->value, uc_se->value); }
Quoted 6 lines
The commit I point to changed makes
uc_rq->value= 1024 by default, hence we miss the first update.I don't mind updating the FIXES tag here, though AFAICT there's no visible side effect from it.
Oh, you're right, that initial uc_rq->value ends up being equivalent to having the flag. Sorry for the confusion!
Patching up that original commit would only really be a "code correctness" thing, it wouldn't fix any visible problem, so I think it's better to keep your current Fixes:.
Quoted 10 lines
Quoted 5 lines
Quoted 2 lines
Signed-off-by Qais Yousef <[email protected]>Changelog nitpicking aside:
Reviewed-by Valentin Schneider <[email protected]>Thanks!
--
Qais Yousef
Quoted 15 lines · Valentin Schneider
Quoted 14 lines · Qais Yousef
Hi Valentin
Quoted 10 lines · Valentin Schneider
Quoted 4 lines · Qais Yousef
Commit
d81ae8aac85c("sched/uclamp: Fix initialization of structuclamp_rq") introduced a bug whereuclamp_maxof the rq is not reset to match the woken up task'suclamp_maxwhen the rq is idle. This only impacts the first wake up after enabling the static key. And it onlyWouldn't that rather be all wakeups after enabling the static key, until the rq goes idle and gains
UCLAMP_FLAG_IDLE? e.g. if you enqueue N uclamp_max=512 tasks, the first enqueue flips the static key and the rq max-aggregate will stay at 1024 after the remaining enqueues.
Quoted 7 lines
Quoted 5 lines
Yep. Bad phrasing from my side. How about:
"This is visible from first wake up(s) until the first dequeue to idle after enabling the static key"?
Sounds good.
+1
Quoted 39 lines
Quoted 39 lines
Quoted 15 lines
Quoted 9 lines
matters if the
uclamp_maxof this task is < 1024 since only then itsuclamp_maxwill be effectively ignored.Fix it by properly initializing
rq->uclamp_flags=UCLAMP_FLAG_IDLEto ensure we reset rquclamp_maxwhen waking up from idle.Fixes d81ae8aac85c ("sched/uclamp: Fix initialization of struct uclamp_rq")Looking at this again, I'm starting to think this actually stems from the introduction of the flag:
e496187da710("sched/uclamp: Enforce last task'sUCLAMP_MAX")Quoted 7 lines
Before the commit you point at, we would still initialize ->uclamp_flags to
0. This was probably hidden by all the activity at boot-time (e.g. just unparking smpboot threads) which yielded an nr_running>0 -> nr_running==0 transition, IOW we'd most likely getUCLAMP_FLAG_IDLEset on a rq before any userspace task could get on there.The static key would have only made this problem more visible.
Hmm. I can't see the sequence of events. I guess you could argue in theory that this commit should have initialized the ->uclamp_flags to
UCLAMP_FLAG_IDLEbut I think it used to work becauseuc_rq->value= 0 by defaultstatic inline void uclamp_rq_inc_id(struct rq *rq, struct task_struct *p, enum uclamp_id clamp_id) { ... if (uc_se->value > READ_ONCE(uc_rq->value)) WRITE_ONCE(uc_rq->value, uc_se->value); }The commit I point to changed makes
uc_rq->value= 1024 by default, hence we miss the first update.
Quoted 6 lines
Quoted 3 lines
I don't mind updating the FIXES tag here, though AFAICT there's no visible side effect from it.
Oh, you're right, that initial
uc_rq->valueends up being equivalent to having the flag. Sorry for the confusion!
No worries! I probably need to mention this in the commit message too..
Quoted 4 lines
Patching up that original commit would only really be a "code correctness" thing, it wouldn't fix any visible problem, so I think it's better to keep your current Fixes:.
Cool. I'll let this brew a bit more and send v2 with the updated commit message.
Thanks!
--
Qais Yousef
Quoted 10 lines · Qais Yousef
Quoted 9 lines · Valentin Schneider
Quoted 8 lines · Qais Yousef
Hi Valentin
Quoted 5 lines · Valentin Schneider
Quoted 4 lines · Qais Yousef
Commit
d81ae8aac85c("sched/uclamp: Fix initialization of structuclamp_rq") introduced a bug whereuclamp_maxof the rq is not reset to match the woken up task'suclamp_maxwhen the rq is idle. This only impacts the first wake up after enabling the static key. And it only
LGTM.
Tested with rt-app:
"tasks": {
"task_n1": {
"util_min": 0,
"util_max": 369,
"loop": 1,
"phases": {
"p000001": {
"loop": 5,
"run": 800,
"timer": {
"period": 16000,
"ref": "task_n1"
}
}
},
"policy": "SCHED_OTHER"
}
}w/o patch:
/* missing (1) since rq->uclamp_flags = UCLAMP_FLAG_IDLE is not set initially */
[75.002086] (3) uclamp_rq_inc_id() CPU5 p=[task_n1-0 1693] uc_se->value=369 uc_rq->value=1024 /* first dequeue to _uclamp_ idle set UCLAMP_FLAG_IDLE */ [75.013851] (2) uclamp_idle_value() CPU5 p=[task_n1-0 1693] clamp_id=1 value=369 [75.017972] (1) uclamp_idle_reset() CPU5 p=[task_n1-0 1693] clamp_id=0 value=0 /* UCLAMP_FLAG_IDLE is set -> set rq->uclamp[UCLAMP_MAX].value to *369* */ [75.017984] (1) uclamp_idle_reset() CPU5 p=[task_n1-0 1693] clamp_id=1 value=369 [75.017995] (3) uclamp_rq_inc_id() CPU5 p=[task_n1-0 1693] uc_se->value=369 uc_rq->value=*369*
w/ patch:
[63.393974] (1) uclamp_idle_reset() CPU5 p=[task_n1-0 1700] clamp_id=0 value=0 /* UCLAMP_FLAG_IDLE is set -> set rq->uclamp[UCLAMP_MAX].value to *369* */ [63.401269] (1) uclamp_idle_reset() CPU5 p=[task_n1-0 1700] clamp_id=1 value=369 [63.415513] (3) uclamp_rq_inc_id() CPU5 p=[task_n1-0 1700] uc_se->value=369 uc_rq->value=*369* /* first dequeue to _uclamp_ idle set UCLAMP_FLAG_IDLE (again) */ [63.434781] (2) uclamp_idle_value() CPU5 p=[task_n1-0 1700] clamp_id=1 value=369 [63.449681] (1) uclamp_idle_reset() CPU5 p=[task_n1-0 1700] clamp_id=0 value=0 [63.449691] (1) uclamp_idle_reset() CPU5 p=[task_n1-0 1700] clamp_id=1 value=369 [63.449699] (3) uclamp_rq_inc_id() CPU5 p=[task_n1-0 1700] uc_se->value=369 uc_rq->value=369
[...]
Quoted 7 lines · Dietmar Eggemann
Quoted 6 lines · Qais Yousef
Quoted 5 lines · Valentin Schneider
Quoted 4 lines · Qais Yousef
Hi Valentin
Quoted 1 line · Valentin Schneider
On 25/11/21 16:52, Qais Yousef wrote:
Quoted 8 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Commit
d81ae8aac85c("sched/uclamp: Fix initialization of structuclamp_rq") introduced a bug whereuclamp_maxof the rq is not reset to match the woken up task'suclamp_maxwhen the rq is idle. This only impacts the first wake up after enabling the static key. And it onlyLGTM.
Tested-by Dietmar Eggemann <[email protected]>
Thanks!
--
Qais Yousef