Skip to content

Commit 8850d77

Browse files
Peter Zijlstraingomolnar
authored andcommitted
locking/ww_mutex: Add RT priority to W/W order
RT mutex based ww_mutexes cannot order based on timestamps. They have to order based on priority. Add the necessary decision logic. Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org> Signed-off-by: Thomas Gleixner <tglx@linutronix.de> Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org> Signed-off-by: Ingo Molnar <mingo@kernel.org> Link: https://lore.kernel.org/r/20210815211304.847536630@linutronix.de
1 parent dc4564f commit 8850d77

1 file changed

Lines changed: 49 additions & 15 deletions

File tree

‎kernel/locking/ww_mutex.h‎

Lines changed: 49 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -219,19 +219,54 @@ ww_mutex_lock_acquired(struct ww_mutex *ww, struct ww_acquire_ctx *ww_ctx)
219219
}
220220

221221
/*
222-
* Determine if context @a is 'after' context @b. IOW, @a is a younger
223-
* transaction than @b and depending on algorithm either needs to wait for
224-
* @b or die.
222+
* Determine if @a is 'less' than @b. IOW, either @a is a lower priority task
223+
* or, when of equal priority, a younger transaction than @b.
224+
*
225+
* Depending on the algorithm, @a will either need to wait for @b, or die.
225226
*/
226227
static inline bool
227-
__ww_ctx_stamp_after(struct ww_acquire_ctx *a, struct ww_acquire_ctx *b)
228+
__ww_ctx_less(struct ww_acquire_ctx *a, struct ww_acquire_ctx *b)
228229
{
230+
/*
231+
* Can only do the RT prio for WW_RT, because task->prio isn't stable due to PI,
232+
* so the wait_list ordering will go wobbly. rt_mutex re-queues the waiter and
233+
* isn't affected by this.
234+
*/
235+
#ifdef WW_RT
236+
/* kernel prio; less is more */
237+
int a_prio = a->task->prio;
238+
int b_prio = b->task->prio;
239+
240+
if (rt_prio(a_prio) || rt_prio(b_prio)) {
241+
242+
if (a_prio > b_prio)
243+
return true;
244+
245+
if (a_prio < b_prio)
246+
return false;
247+
248+
/* equal static prio */
249+
250+
if (dl_prio(a_prio)) {
251+
if (dl_time_before(b->task->dl.deadline,
252+
a->task->dl.deadline))
253+
return true;
254+
255+
if (dl_time_before(a->task->dl.deadline,
256+
b->task->dl.deadline))
257+
return false;
258+
}
259+
260+
/* equal prio */
261+
}
262+
#endif
229263

264+
/* FIFO order tie break -- bigger is younger */
230265
return (signed long)(a->stamp - b->stamp) > 0;
231266
}
232267

233268
/*
234-
* Wait-Die; wake a younger waiter context (when locks held) such that it can
269+
* Wait-Die; wake a lesser waiter context (when locks held) such that it can
235270
* die.
236271
*
237272
* Among waiters with context, only the first one can have other locks acquired
@@ -245,8 +280,7 @@ __ww_mutex_die(struct MUTEX *lock, struct MUTEX_WAITER *waiter,
245280
if (!ww_ctx->is_wait_die)
246281
return false;
247282

248-
if (waiter->ww_ctx->acquired > 0 &&
249-
__ww_ctx_stamp_after(waiter->ww_ctx, ww_ctx)) {
283+
if (waiter->ww_ctx->acquired > 0 && __ww_ctx_less(waiter->ww_ctx, ww_ctx)) {
250284
#ifndef WW_RT
251285
debug_mutex_wake_waiter(lock, waiter);
252286
#endif
@@ -257,10 +291,10 @@ __ww_mutex_die(struct MUTEX *lock, struct MUTEX_WAITER *waiter,
257291
}
258292

259293
/*
260-
* Wound-Wait; wound a younger @hold_ctx if it holds the lock.
294+
* Wound-Wait; wound a lesser @hold_ctx if it holds the lock.
261295
*
262-
* Wound the lock holder if there are waiters with older transactions than
263-
* the lock holders. Even if multiple waiters may wound the lock holder,
296+
* Wound the lock holder if there are waiters with more important transactions
297+
* than the lock holders. Even if multiple waiters may wound the lock holder,
264298
* it's sufficient that only one does.
265299
*/
266300
static bool __ww_mutex_wound(struct MUTEX *lock,
@@ -287,7 +321,7 @@ static bool __ww_mutex_wound(struct MUTEX *lock,
287321
if (!owner)
288322
return false;
289323

290-
if (ww_ctx->acquired > 0 && __ww_ctx_stamp_after(hold_ctx, ww_ctx)) {
324+
if (ww_ctx->acquired > 0 && __ww_ctx_less(hold_ctx, ww_ctx)) {
291325
hold_ctx->wounded = 1;
292326

293327
/*
@@ -306,8 +340,8 @@ static bool __ww_mutex_wound(struct MUTEX *lock,
306340
}
307341

308342
/*
309-
* We just acquired @lock under @ww_ctx, if there are later contexts waiting
310-
* behind us on the wait-list, check if they need to die, or wound us.
343+
* We just acquired @lock under @ww_ctx, if there are more important contexts
344+
* waiting behind us on the wait-list, check if they need to die, or wound us.
311345
*
312346
* See __ww_mutex_add_waiter() for the list-order construction; basically the
313347
* list is ordered by stamp, smallest (oldest) first.
@@ -421,7 +455,7 @@ __ww_mutex_check_kill(struct MUTEX *lock, struct MUTEX_WAITER *waiter,
421455
return 0;
422456
}
423457

424-
if (hold_ctx && __ww_ctx_stamp_after(ctx, hold_ctx))
458+
if (hold_ctx && __ww_ctx_less(ctx, hold_ctx))
425459
return __ww_mutex_kill(lock, ctx);
426460

427461
/*
@@ -479,7 +513,7 @@ __ww_mutex_add_waiter(struct MUTEX_WAITER *waiter,
479513
if (!cur->ww_ctx)
480514
continue;
481515

482-
if (__ww_ctx_stamp_after(ww_ctx, cur->ww_ctx)) {
516+
if (__ww_ctx_less(ww_ctx, cur->ww_ctx)) {
483517
/*
484518
* Wait-Die: if we find an older context waiting, there
485519
* is no point in queueing behind it, as we'd have to

0 commit comments

Comments
 (0)