From: Steven Rostedt <rostedt@goodmis.org>
To: Thomas Gleixner <tglx@linutronix.de>
Cc: LKML <linux-kernel@vger.kernel.org>,
Peter Zijlstra <peterz@infradead.org>,
Ingo Molnar <mingo@kernel.org>
Subject: Re: [patch V4 01/10] rtmutex: Plug slow unlock race
Date: Fri, 13 Jun 2014 11:41:55 -0400 [thread overview]
Message-ID: <20140613114155.41cf9ffa@gandalf.local.home> (raw)
In-Reply-To: <20140611183852.937945560@linutronix.de>
On Wed, 11 Jun 2014 18:44:04 -0000
Thomas Gleixner <tglx@linutronix.de> wrote:
> @@ -671,10 +724,26 @@ static void wakeup_next_waiter(struct rt
> */
> rt_mutex_dequeue_pi(current, waiter);
>
> - rt_mutex_set_owner(lock, NULL);
> + /*
> + * This is safe versus the fastpath acquire:
> + *
> + * We do not remove the waiter from the lock waiter list
> + * here. It stays the top waiter.
> + *
> + * We set the owner to NULL, but keep the RT_MUTEX_HAS_WAITERS
> + * bit set, which forces all potential new waiters into the
> + * slow path. So they are serialized along with all enqueued
> + * waiters on lock->wait_lock.
> + */
The above comment is a little more complex than it needs to be. We can
probably just consider this an optimization. If we are waking up the
next waiter, this lock obviously has waiters, and the deleted
rt_mutex_set_owner(lock, NULL) would do the below anyway.
/*
* As we are waking up the top waiter, and the waiter stays
* queued on the lock until it gets the lock, this lock
* obviously has waiters. Just set the bit here and this has
* the added benefit of forcing all new tasks into the
* slow path making sure no task of lower priority than
* the top waiter can steal this lock.
*/
The rest looks good.
Reviewed-by: Steven Rostedt <rostedt@goodmis.org>
-- Steve
> + lock->owner = (void *) RT_MUTEX_HAS_WAITERS;
>
> raw_spin_unlock_irqrestore(¤t->pi_lock, flags);
>
> + /*
> + * It's safe to dereference waiter as it cannot go away as
> + * long as we hold lock->wait_lock. The waiter task needs to
> + * acquire it in order to dequeue the waiter.
> + */
> wake_up_process(waiter->task);
> }
>
> @@ -928,12 +997,49 @@ rt_mutex_slowunlock(struct rt_mutex *loc
>
> rt_mutex_deadlock_account_unlock(current);
>
> - if (!rt_mutex_has_waiters(lock)) {
> - lock->owner = NULL;
> - raw_spin_unlock(&lock->wait_lock);
> - return;
> + /*
> + * We must be careful here if the fast path is enabled. If we
> + * have no waiters queued we cannot set owner to NULL here
> + * because of:
> + *
> + * foo->lock->owner = NULL;
> + * rtmutex_lock(foo->lock); <- fast path
> + * free = atomic_dec_and_test(foo->refcnt);
> + * rtmutex_unlock(foo->lock); <- fast path
> + * if (free)
> + * kfree(foo);
> + * raw_spin_unlock(foo->lock->wait_lock);
> + *
> + * So for the fastpath enabled kernel:
> + *
> + * Nothing can set the waiters bit as long as we hold
> + * lock->wait_lock. So we do the following sequence:
> + *
> + * owner = rt_mutex_owner(lock);
> + * clear_rt_mutex_waiters(lock);
> + * raw_spin_unlock(&lock->wait_lock);
> + * if (cmpxchg(&lock->owner, owner, 0) == owner)
> + * return;
> + * goto retry;
> + *
> + * The fastpath disabled variant is simple as all access to
> + * lock->owner is serialized by lock->wait_lock:
> + *
> + * lock->owner = NULL;
> + * raw_spin_unlock(&lock->wait_lock);
> + */
> + while (!rt_mutex_has_waiters(lock)) {
> + /* Drops lock->wait_lock ! */
> + if (unlock_rt_mutex_safe(lock) == true)
> + return;
> + /* Relock the rtmutex and try again */
> + raw_spin_lock(&lock->wait_lock);
> }
>
> + /*
> + * The wakeup next waiter path does not suffer from the above
> + * race. See the comments there.
> + */
> wakeup_next_waiter(lock);
>
> raw_spin_unlock(&lock->wait_lock);
>
next prev parent reply other threads:[~2014-06-13 15:41 UTC|newest]
Thread overview: 27+ messages / expand[flat|nested] mbox.gz Atom feed top
2014-06-11 18:44 [patch V4 00/10] rtmutex: Code clarification and optimization Thomas Gleixner
2014-06-11 18:44 ` [patch V4 01/10] rtmutex: Plug slow unlock race Thomas Gleixner
2014-06-13 15:41 ` Steven Rostedt [this message]
2014-06-16 8:06 ` [tip:locking/urgent] " tip-bot for Thomas Gleixner
2014-06-11 18:44 ` [patch V4 02/10] rtmutex: Simplify rtmutex_slowtrylock() Thomas Gleixner
2014-06-12 3:34 ` Lai Jiangshan
2014-06-13 15:58 ` Steven Rostedt
2014-06-11 18:44 ` [patch V4 03/10] rtmutex: Simplify and document try_to_take_rtmutex() Thomas Gleixner
2014-06-13 16:27 ` Steven Rostedt
2014-06-11 18:44 ` [patch V4 04/10] rtmutex: No need to keep task ref for lock owner check Thomas Gleixner
2014-06-13 16:30 ` Steven Rostedt
2014-06-11 18:44 ` [patch V4 06/10] rtmutex: Document pi chain walk Thomas Gleixner
2014-06-13 16:54 ` Steven Rostedt
2014-06-11 18:44 ` [patch V4 05/10] rtmutex: Clarify the boost/deboost part Thomas Gleixner
2014-06-13 16:35 ` Steven Rostedt
2014-06-11 18:44 ` [patch V4 07/10] rtmutex: Simplify remove_waiter() Thomas Gleixner
2014-06-13 16:58 ` Steven Rostedt
2014-06-11 18:44 ` [patch V4 09/10] rtmutex: Cleanup deadlock detector debug logic Thomas Gleixner
2014-06-13 17:19 ` Steven Rostedt
2014-06-13 19:43 ` Thomas Gleixner
2014-06-11 18:44 ` [patch V4 08/10] rtmutex: Confine deadlock logic to futex Thomas Gleixner
2014-06-13 17:10 ` Steven Rostedt
2014-06-11 18:44 ` [patch V4 10/10] rtmutex: Avoid pointless requeueing in the deadlock detection chain walk Thomas Gleixner
2014-06-13 17:28 ` Steven Rostedt
2014-06-13 19:46 ` Thomas Gleixner
2014-06-22 8:45 ` [patch V4 00/10] rtmutex: Code clarification and optimization Ingo Molnar
2014-06-22 9:20 ` Thomas Gleixner
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20140613114155.41cf9ffa@gandalf.local.home \
--to=rostedt@goodmis.org \
--cc=linux-kernel@vger.kernel.org \
--cc=mingo@kernel.org \
--cc=peterz@infradead.org \
--cc=tglx@linutronix.de \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®