Skip to content

Commit 0213b70

Browse files
Qais YousefPeter Zijlstra
authored andcommitted
sched/uclamp: Fix uclamp_tg_restrict()
Now cpu.uclamp.min acts as a protection, we need to make sure that the uclamp request of the task is within the allowed range of the cgroup, that is it is clamp()'ed correctly by tg->uclamp[UCLAMP_MIN] and tg->uclamp[UCLAMP_MAX]. As reported by Xuewen [1] we can have some corner cases where there's inversion between uclamp requested by task (p) and the uclamp values of the taskgroup it's attached to (tg). Following table demonstrates 2 corner cases: | p | tg | effective -----------+-----+------+----------- CASE 1 -----------+-----+------+----------- uclamp_min | 60% | 0% | 60% -----------+-----+------+----------- uclamp_max | 80% | 50% | 50% -----------+-----+------+----------- CASE 2 -----------+-----+------+----------- uclamp_min | 0% | 30% | 30% -----------+-----+------+----------- uclamp_max | 20% | 50% | 20% -----------+-----+------+----------- With this fix we get: | p | tg | effective -----------+-----+------+----------- CASE 1 -----------+-----+------+----------- uclamp_min | 60% | 0% | 50% -----------+-----+------+----------- uclamp_max | 80% | 50% | 50% -----------+-----+------+----------- CASE 2 -----------+-----+------+----------- uclamp_min | 0% | 30% | 30% -----------+-----+------+----------- uclamp_max | 20% | 50% | 30% -----------+-----+------+----------- Additionally uclamp_update_active_tasks() must now unconditionally update both UCLAMP_MIN/MAX because changing the tg's UCLAMP_MAX for instance could have an impact on the effective UCLAMP_MIN of the tasks. | p | tg | effective -----------+-----+------+----------- old -----------+-----+------+----------- uclamp_min | 60% | 0% | 50% -----------+-----+------+----------- uclamp_max | 80% | 50% | 50% -----------+-----+------+----------- *new* -----------+-----+------+----------- uclamp_min | 60% | 0% | *60%* -----------+-----+------+----------- uclamp_max | 80% |*70%* | *70%* -----------+-----+------+----------- [1] https://lore.kernel.org/lkml/CAB8ipk_a6VFNjiEnHRHkUMBKbA+qzPQvhtNjJ_YNzQhqV_o8Zw@mail.gmail.com/ Fixes: 0c18f2e ("sched/uclamp: Fix wrong implementation of cpu.uclamp.min") Reported-by: Xuewen Yan <[email protected]> Signed-off-by: Qais Yousef <[email protected]> Signed-off-by: Peter Zijlstra (Intel) <[email protected]> Link: https://lkml.kernel.org/r/[email protected]
1 parent d7d6070 commit 0213b70

File tree

1 file changed

+18
-31
lines changed

1 file changed

+18
-31
lines changed

kernel/sched/core.c

Lines changed: 18 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1403,8 +1403,10 @@ static void uclamp_sync_util_min_rt_default(void)
14031403
static inline struct uclamp_se
14041404
uclamp_tg_restrict(struct task_struct *p, enum uclamp_id clamp_id)
14051405
{
1406+
/* Copy by value as we could modify it */
14061407
struct uclamp_se uc_req = p->uclamp_req[clamp_id];
14071408
#ifdef CONFIG_UCLAMP_TASK_GROUP
1409+
unsigned int tg_min, tg_max, value;
14081410

14091411
/*
14101412
* Tasks in autogroups or root task group will be
@@ -1415,23 +1417,11 @@ uclamp_tg_restrict(struct task_struct *p, enum uclamp_id clamp_id)
14151417
if (task_group(p) == &root_task_group)
14161418
return uc_req;
14171419

1418-
switch (clamp_id) {
1419-
case UCLAMP_MIN: {
1420-
struct uclamp_se uc_min = task_group(p)->uclamp[clamp_id];
1421-
if (uc_req.value < uc_min.value)
1422-
return uc_min;
1423-
break;
1424-
}
1425-
case UCLAMP_MAX: {
1426-
struct uclamp_se uc_max = task_group(p)->uclamp[clamp_id];
1427-
if (uc_req.value > uc_max.value)
1428-
return uc_max;
1429-
break;
1430-
}
1431-
default:
1432-
WARN_ON_ONCE(1);
1433-
break;
1434-
}
1420+
tg_min = task_group(p)->uclamp[UCLAMP_MIN].value;
1421+
tg_max = task_group(p)->uclamp[UCLAMP_MAX].value;
1422+
value = uc_req.value;
1423+
value = clamp(value, tg_min, tg_max);
1424+
uclamp_se_set(&uc_req, value, false);
14351425
#endif
14361426

14371427
return uc_req;
@@ -1630,8 +1620,9 @@ static inline void uclamp_rq_dec(struct rq *rq, struct task_struct *p)
16301620
}
16311621

16321622
static inline void
1633-
uclamp_update_active(struct task_struct *p, enum uclamp_id clamp_id)
1623+
uclamp_update_active(struct task_struct *p)
16341624
{
1625+
enum uclamp_id clamp_id;
16351626
struct rq_flags rf;
16361627
struct rq *rq;
16371628

@@ -1651,30 +1642,26 @@ uclamp_update_active(struct task_struct *p, enum uclamp_id clamp_id)
16511642
* affecting a valid clamp bucket, the next time it's enqueued,
16521643
* it will already see the updated clamp bucket value.
16531644
*/
1654-
if (p->uclamp[clamp_id].active) {
1655-
uclamp_rq_dec_id(rq, p, clamp_id);
1656-
uclamp_rq_inc_id(rq, p, clamp_id);
1645+
for_each_clamp_id(clamp_id) {
1646+
if (p->uclamp[clamp_id].active) {
1647+
uclamp_rq_dec_id(rq, p, clamp_id);
1648+
uclamp_rq_inc_id(rq, p, clamp_id);
1649+
}
16571650
}
16581651

16591652
task_rq_unlock(rq, p, &rf);
16601653
}
16611654

16621655
#ifdef CONFIG_UCLAMP_TASK_GROUP
16631656
static inline void
1664-
uclamp_update_active_tasks(struct cgroup_subsys_state *css,
1665-
unsigned int clamps)
1657+
uclamp_update_active_tasks(struct cgroup_subsys_state *css)
16661658
{
1667-
enum uclamp_id clamp_id;
16681659
struct css_task_iter it;
16691660
struct task_struct *p;
16701661

16711662
css_task_iter_start(css, 0, &it);
1672-
while ((p = css_task_iter_next(&it))) {
1673-
for_each_clamp_id(clamp_id) {
1674-
if ((0x1 << clamp_id) & clamps)
1675-
uclamp_update_active(p, clamp_id);
1676-
}
1677-
}
1663+
while ((p = css_task_iter_next(&it)))
1664+
uclamp_update_active(p);
16781665
css_task_iter_end(&it);
16791666
}
16801667

@@ -9634,7 +9621,7 @@ static void cpu_util_update_eff(struct cgroup_subsys_state *css)
96349621
}
96359622

96369623
/* Immediately update descendants RUNNABLE tasks */
9637-
uclamp_update_active_tasks(css, clamps);
9624+
uclamp_update_active_tasks(css);
96389625
}
96399626
}
96409627

0 commit comments

Comments
 (0)