ANDROID: sched/pi: Reweight fair_policy() tasks when inheriting prio

For fair tasks inheriting the priority (nice) without reweighting is
a NOP as the task's share won't change.

This is visible when running with PTHREAD_PRIO_INHERIT where fair tasks
with low priority values are susceptible to starvation leading to PI
like impact on lock contention.

The logic in rt_mutex will reset these low priority fair tasks into nice
0, but without the additional reweight operation to actually update the
weights, it doesn't have the desired impact of boosting them to allow
them to run sooner/longer to release the lock.

Apply the reweight for fair_policy() tasks to achieve the desired boost
for those low nice values tasks. Note that boost here means resetting
their nice to 0; as this is what the current logic does for fair tasks.

We need to re-instate ordering fair tasks by their priority order on the
waiter tree to ensure we inherit the top_waiter properly.

Handling of idle_policy() requires more code refactoring and is not
handled yet. idle_policy() are treated specially and only run when the
CPU is idle and get a hardcoded low weight value. Changing weights won't
be enough without a promotion first to SCHED_OTHER.

Tested with a test program that creates three threads.

	1. main thread that spawns high prio and low prio task and busy
	   loops

	2. low priority thread that holds a pthread_mutex() with
	   PTHREAD_PRIO_INHERIT protocol. Runs at nice +10. Busy loops
	   after holding the lock.

	3. high priority thread that holds a pthread_mutex() with
	   PTHREADPTHREAD_PRIO_INHERIT, but made to start after the low
	   priority thread. Runs at nice 0. Should remain blocked by the
	   low priority thread.

All tasks are pinned to CPU0.

Without the patch I can see the low priority thread running only for
~10% of the time which is what expected without it being boosted.

With the patch the low priority thread runs for ~50% which is what
expected if it gets boosted to nice 0.

I modified the test program logic afterwards to ensure that after
releasing the lock the low priority thread goes back to running for 10%
of the time, and it does.

This is a temporary workaround until Proxy Execution is available and
connected to futex_pi. The inheritance logic won't work with SCHED_IDLE
nor with cgroup cpu.shares as inheriting these is complex and requires
Proxy Execution. But users of PTHREAD_PRIO_INHERIT will see that fair
tasks inherit nice value and will address a common class of existing
problems until Proxy Execution has fully landed.

Bug: 263876335
Link: https://lore.kernel.org/lkml/20240514160711.hpdg64grdwc43ux7@airbuntu/
Reported-by: Yabin Cui <yabinc@google.com>
Signed-off-by: Qais Yousef <qyousef@layalina.io>
[Fix trivial conflict with vendor hook]
Signed-off-by: Qais Yousef <qyousef@google.com>
Change-Id: Ia954ee528495b5cf5c3a2157c68b4a757cef1f83
(cherry picked from commit 23ac35ed8fc6220e4e498a21d22a9dbe67e7da9b)
[Fix conflict in __waiter_prio() where a vendor hook no longer present
use p->sched_class->reweight_task() as reweight_task() def has changed]
Signed-off-by: Qais Yousef <qyousef@google.com>
This commit is contained in:
Qais Yousef
2024-04-01 03:04:00 +01:00
committed by Treehugger Robot
parent b24c1517d9
commit b02669867b
2 changed files with 9 additions and 7 deletions
+1 -6
View File
@@ -348,12 +348,7 @@ static __always_inline bool unlock_rt_mutex_safe(struct rt_mutex_base *lock,
static __always_inline int __waiter_prio(struct task_struct *task)
{
int prio = task->prio;
if (!rt_or_dl_prio(prio))
return DEFAULT_PRIO;
return prio;
return task->prio;
}
/*
+8 -1
View File
@@ -8224,8 +8224,15 @@ void rt_mutex_setprio(struct task_struct *p, struct task_struct *pi_task)
} else {
if (dl_prio(oldprio))
p->dl.pi_se = &p->dl;
if (rt_prio(oldprio))
else if (rt_prio(oldprio))
p->rt.timeout = 0;
else if (!task_has_idle_policy(p)) {
struct load_weight lw;
lw.weight = scale_load(sched_prio_to_weight[prio - MAX_RT_PRIO]);
lw.inv_weight = sched_prio_to_wmult[prio - MAX_RT_PRIO];
p->sched_class->reweight_task(rq, p, &lw);
}
}
p->sched_class = next_class;