Skip to content

Commit

Permalink
locking/rtmutex: Implement equal priority lock stealing
Browse files Browse the repository at this point in the history
The current logic only allows lock stealing to occur if the current task is
of higher priority than the pending owner.

Significant throughput improvements can be gained by allowing the lock
stealing to include tasks of equal priority when the contended lock is a
spin_lock or a rw_lock and the tasks are not in a RT scheduling task.

The assumption was that the system will make faster progress by allowing
the task already on the CPU to take the lock rather than waiting for the
system to wake up a different task.

This does add a degree of unfairness, but in reality no negative side
effects have been observed in the many years that this has been used in the
RT kernel.

[ tglx: Refactored and rewritten several times by Steve Rostedt, Sebastian
  	Siewior and myself ]

Signed-off-by: Gregory Haskins <[email protected]>
Signed-off-by: Thomas Gleixner <[email protected]>
Signed-off-by: Peter Zijlstra (Intel) <[email protected]>
Signed-off-by: Ingo Molnar <[email protected]>
Link: https://lore.kernel.org/r/[email protected]
  • Loading branch information
Gregory Haskins authored and Ingo Molnar committed Aug 17, 2021
1 parent 015680a commit 48eb3f4
Showing 1 changed file with 35 additions and 17 deletions.
52 changes: 35 additions & 17 deletions kernel/locking/rtmutex.c
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,26 @@ static __always_inline int rt_mutex_waiter_equal(struct rt_mutex_waiter *left,
return 1;
}

static inline bool rt_mutex_steal(struct rt_mutex_waiter *waiter,
struct rt_mutex_waiter *top_waiter)
{
if (rt_mutex_waiter_less(waiter, top_waiter))
return true;

#ifdef RT_MUTEX_BUILD_SPINLOCKS
/*
* Note that RT tasks are excluded from same priority (lateral)
* steals to prevent the introduction of an unbounded latency.
*/
if (rt_prio(waiter->prio) || dl_prio(waiter->prio))
return false;

return rt_mutex_waiter_equal(waiter, top_waiter);
#else
return false;
#endif
}

#define __node_2_waiter(node) \
rb_entry((node), struct rt_mutex_waiter, tree_entry)

Expand Down Expand Up @@ -932,19 +952,21 @@ try_to_take_rt_mutex(struct rt_mutex_base *lock, struct task_struct *task,
* trylock attempt.
*/
if (waiter) {
/*
* If waiter is not the highest priority waiter of
* @lock, give up.
*/
if (waiter != rt_mutex_top_waiter(lock))
return 0;
struct rt_mutex_waiter *top_waiter = rt_mutex_top_waiter(lock);

/*
* We can acquire the lock. Remove the waiter from the
* lock waiters tree.
* If waiter is the highest priority waiter of @lock,
* or allowed to steal it, take it over.
*/
rt_mutex_dequeue(lock, waiter);

if (waiter == top_waiter || rt_mutex_steal(waiter, top_waiter)) {
/*
* We can acquire the lock. Remove the waiter from the
* lock waiters tree.
*/
rt_mutex_dequeue(lock, waiter);
} else {
return 0;
}
} else {
/*
* If the lock has waiters already we check whether @task is
Expand All @@ -955,13 +977,9 @@ try_to_take_rt_mutex(struct rt_mutex_base *lock, struct task_struct *task,
* not need to be dequeued.
*/
if (rt_mutex_has_waiters(lock)) {
/*
* If @task->prio is greater than or equal to
* the top waiter priority (kernel view),
* @task lost.
*/
if (!rt_mutex_waiter_less(task_to_waiter(task),
rt_mutex_top_waiter(lock)))
/* Check whether the trylock can steal it. */
if (!rt_mutex_steal(task_to_waiter(task),
rt_mutex_top_waiter(lock)))
return 0;

/*
Expand Down

0 comments on commit 48eb3f4

Please sign in to comment.