OSDN Git Service

sched/uclamp: Fix uclamp_tg_restrict()
authorQais Yousef <qais.yousef@arm.com>
Thu, 17 Jun 2021 16:51:55 +0000 (17:51 +0100)
committerPeter Zijlstra <peterz@infradead.org>
Tue, 22 Jun 2021 14:41:59 +0000 (16:41 +0200)
commit0213b7083e81f4acd69db32cb72eb4e5f220329a
tree3d659e4507e2eb5ae674ff152908beacd468bc1e
parentd7d607096ae6d378b4e92d49946d22739c047d4c
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: 0c18f2ecfcc2 ("sched/uclamp: Fix wrong implementation of cpu.uclamp.min")
Reported-by: Xuewen Yan <xuewen.yan94@gmail.com>
Signed-off-by: Qais Yousef <qais.yousef@arm.com>
Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
Link: https://lkml.kernel.org/r/20210617165155.3774110-1-qais.yousef@arm.com
kernel/sched/core.c