From: Liu Bo <bo.li.liu@oracle.com>
To: linux-btrfs@vger.kernel.org
Subject: [PATCH] Btrfs: avoid wake_up if possible
Date: Fri, 1 Sep 2017 16:14:27 -0600 [thread overview]
Message-ID: <20170901221430.10131-2-bo.li.liu@oracle.com> (raw)
wake_up() will go to check whether someone is on the waiting list with
holding spin_lock().
Around some btrfs code, we don't check waitqueue_active() firstly, so
the spin_lock() pair in wake_up() is called even if no one is waiting
on the queue.
There are more wake_up()s without waitqueue_active(), but these two
are the hottest one I've run into so far.
Signed-off-by: Liu Bo <bo.li.liu@oracle.com>
---
fs/btrfs/extent_io.c | 9 ++++++++-
fs/btrfs/ordered-data.c | 8 +++++++-
2 files changed, 15 insertions(+), 2 deletions(-)
diff --git a/fs/btrfs/extent_io.c b/fs/btrfs/extent_io.c
index 825fad6..e2dc042 100644
--- a/fs/btrfs/extent_io.c
+++ b/fs/btrfs/extent_io.c
@@ -536,8 +536,15 @@ static struct extent_state *clear_state_bit(struct extent_io_tree *tree,
clear_state_cb(tree, state, bits);
add_extent_changeset(state, bits_to_clear, changeset, 0);
state->state &= ~bits_to_clear;
- if (wake)
+
+ assert_spin_locked(&tree->lock);
+ /*
+ * spin_lock is acquired by both waker and waiter, thus no
+ * need to restrict the order.
+ **/
+ if (wake && waitqueue_active(&state->wq))
wake_up(&state->wq);
+
if (state->state == 0) {
next = next_state(state);
if (extent_state_in_tree(state)) {
diff --git a/fs/btrfs/ordered-data.c b/fs/btrfs/ordered-data.c
index a3aca49..e439fb4 100644
--- a/fs/btrfs/ordered-data.c
+++ b/fs/btrfs/ordered-data.c
@@ -647,7 +647,13 @@ void btrfs_remove_ordered_extent(struct inode *inode,
spin_unlock(&fs_info->ordered_root_lock);
}
spin_unlock(&root->ordered_extent_lock);
- wake_up(&entry->wait);
+
+ /*
+ * setting flag is protected by spin_lock pair, which has a
+ * implicit memory barrier.
+ */
+ if (waitqueue_active(&entry->wait))
+ wake_up(&entry->wait);
}
static void btrfs_run_ordered_extent_work(struct btrfs_work *work)
--
2.9.4
next reply other threads:[~2017-09-01 23:16 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2017-09-01 22:14 Liu Bo [this message]
2017-09-06 14:19 ` [PATCH] Btrfs: avoid wake_up if possible David Sterba
2017-09-07 0:48 ` Liu Bo
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=20170901221430.10131-2-bo.li.liu@oracle.com \
--to=bo.li.liu@oracle.com \
--cc=linux-btrfs@vger.kernel.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;
as well as URLs for NNTP newsgroup(s).