Re: [patch V3 50/64] locking/rtmutex: Extend the rtmutex core to support ww_mutex

From: Peter Zijlstra
Date: Fri Aug 06 2021 - 09:49:39 EST


On Fri, Aug 06, 2021 at 01:00:51PM +0200, Peter Zijlstra wrote:
> Also, I found a note that said I had to check what this code does for
> !RT tasks, lemme go do that now.

Do we want something like the below?

AFAICT the existing RB-tree actually does FIFO for equal prio right. Per
rb_add_cached() we only go left for less, therefore equal goes right,
giving a tail-add for equal prio.

Furthermore, rt_mutex_adjust_prio_chain() is careful not to requeue when
the priority doesn't change, preserving this FIFO order (as is mandated
by SCHED_FIFO semantics IIRC).

Therefore, all that seems to be missing is to squash all !RT tasks to
the same prio to make sure they're all FIFO ordered.

---
Subject: locking/rtmutex: Squash !RT tasks to DEFAULT_PRIO

Ensure all !RT tasks have the same prio such that they end up in FIFO
order and aren't split up according to nice level.

Signed-off-by: Peter Zijlstra (Intel) <peterz@xxxxxxxxxxxxx>
---
kernel/locking/rtmutex.c | 26 +++++++++++++++++++++-----
1 file changed, 21 insertions(+), 5 deletions(-)

diff --git a/kernel/locking/rtmutex.c b/kernel/locking/rtmutex.c
index b5d9bb5202c6..36e115c868a7 100644
--- a/kernel/locking/rtmutex.c
+++ b/kernel/locking/rtmutex.c
@@ -223,11 +223,29 @@ static __always_inline bool unlock_rt_mutex_safe(struct rt_mutex *lock,
}
#endif

+static __always_inline int
+__waiter_prio(struct task_struct *task)
+{
+ int prio = task->prio;
+
+ if (!rt_prio(prio))
+ prio = DEFAULT_PRIO;
+
+ return prio;
+}
+
+static __always_inline void
+waiter_update_prio(struct rt_mutex_waiter *waiter, struct task_struct *task)
+{
+ waiter->prio = __waiter_prio(task);
+ waiter->deadline = task->dl.deadline;
+}
+
/*
* Only use with rt_mutex_waiter_{less,equal}()
*/
#define task_to_waiter(p) \
- &(struct rt_mutex_waiter){ .prio = (p)->prio, .deadline = (p)->dl.deadline }
+ &(struct rt_mutex_waiter){ .prio = __waiter_prio(p), .deadline = (p)->dl.deadline }

static __always_inline int rt_mutex_waiter_less(struct rt_mutex_waiter *left,
struct rt_mutex_waiter *right)
@@ -653,8 +671,7 @@ static int __sched rt_mutex_adjust_prio_chain(struct task_struct *task,
* serializes all pi_waiters access and rb_erase() does not care about
* the values of the node being removed.
*/
- waiter->prio = task->prio;
- waiter->deadline = task->dl.deadline;
+ waiter_update_prio(waiter, task);

rt_mutex_enqueue(lock, waiter);

@@ -924,8 +941,7 @@ static int __sched task_blocks_on_rt_mutex(struct rt_mutex *lock,
raw_spin_lock(&task->pi_lock);
waiter->task = task;
waiter->lock = lock;
- waiter->prio = task->prio;
- waiter->deadline = task->dl.deadline;
+ waiter_update_prio(waiter, task);

/* Get the top priority waiter on the lock */
if (rt_mutex_has_waiters(lock))