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/
Showing the changes to kernel (1 of 1 file). Show all
Unified · Split
| @@ -1403,8 +1403,10 @@ static void uclamp_sync_util_min_rt_default(void) · 1402 unmodified lines | |||
| 1403 | static inline struct uclamp_se | 1403 | static inline struct uclamp_se |
| 1404 | uclamp_tg_restrict(struct task_struct *p, enum uclamp_id clamp_id) | 1404 | uclamp_tg_restrict(struct task_struct *p, enum uclamp_id clamp_id) |
| 1405 | { | 1405 | { |
| 1406 | /* Copy by value as we could modify it */ | ||
| 1406 | struct uclamp_se uc_req = p->uclamp_req[clamp_id]; | 1407 | struct uclamp_se uc_req = p->uclamp_req[clamp_id]; |
| 1407 | #ifdef CONFIG_UCLAMP_TASK_GROUP | 1408 | #ifdef CONFIG_UCLAMP_TASK_GROUP |
| 1409 | unsigned int tg_min, tg_max, value; | ||
| 1408 | 1410 | ||
| 1409 | /* | 1411 | /* |
| 1410 | * Tasks in autogroups or root task group will be | 1412 | * 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) · 4 unmodified lines | |||
| 1415 | if (task_group(p) == &root_task_group) | 1417 | if (task_group(p) == &root_task_group) |
| 1416 | return uc_req; | 1418 | return uc_req; |
| 1417 | 1419 | ||
| 1418 | switch (clamp_id) { | 1420 | tg_min = task_group(p)->uclamp[UCLAMP_MIN].value; |
| 1419 | case UCLAMP_MIN: { | 1421 | tg_max = task_group(p)->uclamp[UCLAMP_MAX].value; |
| 1420 | struct uclamp_se uc_min = task_group(p)->uclamp[clamp_id]; | 1422 | value = uc_req.value; |
| 1421 | if (uc_req.value < uc_min.value) | 1423 | value = clamp(value, tg_min, tg_max); |
| 1422 | return uc_min; | 1424 | uclamp_se_set(&uc_req, value, false); |
| 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 | } | ||
| 1435 | #endif | 1425 | #endif |
| 1436 | 1426 | ||
| 1437 | return uc_req; | 1427 | return uc_req; |
| @@ -1630,8 +1620,9 @@ static inline void uclamp_rq_dec(struct rq *rq, struct task_struct *p) · 192 unmodified lines | |||
| 1630 | } | 1620 | } |
| 1631 | 1621 | ||
| 1632 | static inline void | 1622 | static inline void |
| 1633 | uclamp_update_active(struct task_struct *p, enum uclamp_id clamp_id) | 1623 | uclamp_update_active(struct task_struct *p) |
| 1634 | { | 1624 | { |
| 1625 | enum uclamp_id clamp_id; | ||
| 1635 | struct rq_flags rf; | 1626 | struct rq_flags rf; |
| 1636 | struct rq *rq; | 1627 | struct rq *rq; |
| 1637 | 1628 | ||
| @@ -1651,9 +1642,11 @@ uclamp_update_active(struct task_struct *p, enum uclamp_id clamp_id) · 13 unmodified lines | |||
| 1651 | * affecting a valid clamp bucket, the next time it's enqueued, | 1642 | * affecting a valid clamp bucket, the next time it's enqueued, |
| 1652 | * it will already see the updated clamp bucket value. | 1643 | * it will already see the updated clamp bucket value. |
| 1653 | */ | 1644 | */ |
| 1654 | if (p->uclamp[clamp_id].active) { | 1645 | for_each_clamp_id(clamp_id) { |
| 1655 | uclamp_rq_dec_id(rq, p, clamp_id); | 1646 | if (p->uclamp[clamp_id].active) { |
| 1656 | uclamp_rq_inc_id(rq, p, clamp_id); | 1647 | uclamp_rq_dec_id(rq, p, clamp_id); |
| 1648 | uclamp_rq_inc_id(rq, p, clamp_id); | ||
| 1649 | } | ||
| 1657 | } | 1650 | } |
| 1658 | 1651 | ||
| 1659 | task_rq_unlock(rq, p, &rf); | 1652 | task_rq_unlock(rq, p, &rf); |
| @@ -1661,20 +1654,14 @@ uclamp_update_active(struct task_struct *p, enum uclamp_id clamp_id) · 1 unmodified line | |||
| 1661 | 1654 | ||
| 1662 | #ifdef CONFIG_UCLAMP_TASK_GROUP | 1655 | #ifdef CONFIG_UCLAMP_TASK_GROUP |
| 1663 | static inline void | 1656 | static inline void |
| 1664 | uclamp_update_active_tasks(struct cgroup_subsys_state *css, | 1657 | uclamp_update_active_tasks(struct cgroup_subsys_state *css) |
| 1665 | unsigned int clamps) | ||
| 1666 | { | 1658 | { |
| 1667 | enum uclamp_id clamp_id; | ||
| 1668 | struct css_task_iter it; | 1659 | struct css_task_iter it; |
| 1669 | struct task_struct *p; | 1660 | struct task_struct *p; |
| 1670 | 1661 | ||
| 1671 | css_task_iter_start(css, 0, &it); | 1662 | css_task_iter_start(css, 0, &it); |
| 1672 | while ((p = css_task_iter_next(&it))) { | 1663 | while ((p = css_task_iter_next(&it))) |
| 1673 | for_each_clamp_id(clamp_id) { | 1664 | uclamp_update_active(p); |
| 1674 | if ((0x1 << clamp_id) & clamps) | ||
| 1675 | uclamp_update_active(p, clamp_id); | ||
| 1676 | } | ||
| 1677 | } | ||
| 1678 | css_task_iter_end(&it); | 1665 | css_task_iter_end(&it); |
| 1679 | } | 1666 | } |
| 1680 | 1667 | ||
| @@ -9634,7 +9621,7 @@ static void cpu_util_update_eff(struct cgroup_subsys_state *css) · 7953 unmodified lines | |||
| 9634 | } | 9621 | } |
| 9635 | 9622 | ||
| 9636 | /* Immediately update descendants RUNNABLE tasks */ | 9623 | /* Immediately update descendants RUNNABLE tasks */ |
| 9637 | uclamp_update_active_tasks(css, clamps); | 9624 | uclamp_update_active_tasks(css); |
| 9638 | } | 9625 | } |
| 9639 | } | 9626 | } |
| 9640 | 9627 | ||