From: Kent Overstreet <kent.overstreet@linux.dev>
To: unlisted-recipients:; (no To-header on input)
Cc: Kent Overstreet <kent.overstreet@linux.dev>,
Linus Torvalds <torvalds@linux-foundation.org>,
Boqun Feng <boqun.feng@gmail.com>,
linux-bcachefs@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: [PATCH] bcachefs: six locks: Fix missing barrier on wait->lock_acquired
Date: Sat, 12 Aug 2023 15:27:20 -0400 [thread overview]
Message-ID: <20230812192720.2703874-1-kent.overstreet@linux.dev> (raw)
Six locks do lock handoff via the wakeup path: the thread doing the
wakeup also takes the lock on behalf of the waiter, which means the
waiter only has to look at its waitlist entry, and doesn't have to touch
the lock cacheline while another thread is using it.
Linus noticed that this needs a real barrier, which this patch fixes.
Also add a comment for the should_sleep_fn() error path.
Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Boqun Feng <boqun.feng@gmail.com>
Cc: linux-bcachefs@vger.kernel.org
Cc: linux-kernel@vger.kernel.org
---
fs/bcachefs/six.c | 33 +++++++++++++++++++++++++--------
1 file changed, 25 insertions(+), 8 deletions(-)
diff --git a/fs/bcachefs/six.c b/fs/bcachefs/six.c
index 581aee565e..b6ca53c852 100644
--- a/fs/bcachefs/six.c
+++ b/fs/bcachefs/six.c
@@ -223,14 +223,16 @@ static void __six_lock_wakeup(struct six_lock *lock, enum six_lock_type lock_typ
if (ret <= 0)
goto unlock;
- __list_del(w->list.prev, w->list.next);
task = w->task;
+ __list_del(w->list.prev, w->list.next);
/*
- * Do no writes to @w besides setting lock_acquired - otherwise
- * we would need a memory barrier:
+ * The release barrier here ensures the ordering of the
+ * __list_del before setting w->lock_acquired; @w is on the
+ * stack of the thread doing the waiting and will be reused
+ * after it sees w->lock_acquired with no other locking:
+ * pairs with smp_load_acquire() in six_lock_slowpath()
*/
- barrier();
- w->lock_acquired = true;
+ smp_store_release(&w->lock_acquired, true);
wake_up_process(task);
}
@@ -502,17 +504,32 @@ static int six_lock_slowpath(struct six_lock *lock, enum six_lock_type type,
while (1) {
set_current_state(TASK_UNINTERRUPTIBLE);
- if (wait->lock_acquired)
+ /*
+ * Ensures that writes to the waitlist entry happen after we see
+ * wait->lock_acquired: pairs with the smp_store_release in
+ * __six_lock_wakeup
+ */
+ if (smp_load_acquire(&wait->lock_acquired))
break;
ret = should_sleep_fn ? should_sleep_fn(lock, p) : 0;
if (unlikely(ret)) {
+ bool acquired;
+
+ /*
+ * If should_sleep_fn() returns an error, we are
+ * required to return that error even if we already
+ * acquired the lock - should_sleep_fn() might have
+ * modified external state (e.g. when the deadlock cycle
+ * detector in bcachefs issued a transaction restart)
+ */
raw_spin_lock(&lock->wait_lock);
- if (!wait->lock_acquired)
+ acquired = wait->lock_acquired;
+ if (!acquired)
list_del(&wait->list);
raw_spin_unlock(&lock->wait_lock);
- if (unlikely(wait->lock_acquired))
+ if (unlikely(acquired))
do_six_unlock_type(lock, type);
break;
}
--
2.40.1
next reply other threads:[~2023-08-12 19:27 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-08-12 19:27 Kent Overstreet [this message]
2023-08-12 19:58 ` Boqun Feng
2023-08-12 20:58 ` Boqun Feng
2023-08-12 21:08 ` Kent Overstreet
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=20230812192720.2703874-1-kent.overstreet@linux.dev \
--to=kent.overstreet@linux.dev \
--cc=boqun.feng@gmail.com \
--cc=linux-bcachefs@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=torvalds@linux-foundation.org \
/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®