Re: [PATCH v2] sched/uclamp: Fix uclamp_tg_restrict()
From: Peter Zijlstra
Date: Tue Jun 22 2021 - 09:13:13 EST
On Thu, Jun 17, 2021 at 05:51:55PM +0100, Qais Yousef wrote:
> 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@xxxxxxxxxxxxxx/
>
> Reported-by: Xuewen Yan <xuewen.yan94@xxxxxxxxx>
> Fixes: 0c18f2ecfcc2 ("sched/uclamp: Fix wrong implementation of cpu.uclamp.min")
> Signed-off-by: Qais Yousef <qais.yousef@xxxxxxx>
Thanks!