Re: [PATCH] pstore: Revert pmsg_lock back to a normal mutex

From: Steven Rostedt
Date: Thu Mar 02 2023 - 16:56:26 EST


On Thu, 2 Mar 2023 16:36:03 -0500
Steven Rostedt <rostedt@xxxxxxxxxxx> wrote:

> I just noticed there's an rcu_read_lock() here around the loop.
> I'm guessing that's to keep this race from happening.
> Would have been nice to have a comment there stating such.

Knowing that rcu_read_lock() keeps the tasks safe, I made the optimization
to only grab the spinlock (and disable interrupts) once, or whenever the
top waiter changes.

-- Steve

diff --git a/kernel/locking/rtmutex.c b/kernel/locking/rtmutex.c
index 010cf4e6d0b8..37820331857b 100644
--- a/kernel/locking/rtmutex.c
+++ b/kernel/locking/rtmutex.c
@@ -1399,8 +1399,12 @@ static bool rtmutex_spin_on_owner(struct rt_mutex_base *lock,
struct rt_mutex_waiter *waiter,
struct task_struct *owner)
{
+ struct rt_mutex_waiter *top_waiter;
+ struct rt_mutex_waiter *last_waiter = NULL;
+ struct task_struct *top_task = NULL;
bool res = true;

+ /* rcu_read_lock keeps task_structs around */
rcu_read_lock();
for (;;) {
/* If owner changed, trylock again. */
@@ -1421,11 +1425,27 @@ static bool rtmutex_spin_on_owner(struct rt_mutex_base *lock,
* for CONFIG_PREEMPT_RCU=y)
* - the VCPU on which owner runs is preempted
*/
- if (!owner_on_cpu(owner) || need_resched() ||
- !rt_mutex_waiter_is_top_waiter(lock, waiter)) {
+ if (!owner_on_cpu(owner) || need_resched()) {
res = false;
break;
}
+ top_waiter = rt_mutex_top_waiter(lock);
+ if (top_waiter != waiter) {
+ if (top_waiter != last_waiter) {
+ raw_spin_lock_irq(&lock->wait_lock);
+ last_waiter = rt_mutex_top_waiter(lock);
+ top_task = last_waiter->task;
+ raw_spin_unlock_irq(&lock->wait_lock);
+ }
+ trace_printk("spin on owner! %s:%d\n",
+ top_task->comm,
+ top_task->pid);
+
+ if (!owner_on_cpu(top_task)) {
+ res = false;
+ break;
+ }
+ }
cpu_relax();
}
rcu_read_unlock();