All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] btrfs: avoid pointless wake ups of drew lock readers
@ 2024-03-18 11:11 fdmanana
  2024-03-18 22:14 ` David Sterba
  0 siblings, 1 reply; 2+ messages in thread
From: fdmanana @ 2024-03-18 11:11 UTC (permalink / raw)
  To: linux-btrfs

From: Filipe Manana <fdmanana@suse.com>

When unlocking a write lock on a drew lock, at btrfs_drew_write_unlock(),
it's pointless to wake up tasks waiting to acquire a read lock if we
didn't decrement the 'writers' counter down to 0, since a read lock can
only be acquired when the counter reaches a value of 0. Doing so is
harmless from a functional point of view, but it's not efficient due to
unnecessarily waking up tasks just for them to sleep again on the
waitqueue.

So change this to wake up readers only if we decremented the 'writers'
counter to 0.

Signed-off-by: Filipe Manana <fdmanana@suse.com>
---
 fs/btrfs/locking.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)

diff --git a/fs/btrfs/locking.c b/fs/btrfs/locking.c
index 508a3fdfcd58..72992e74c479 100644
--- a/fs/btrfs/locking.c
+++ b/fs/btrfs/locking.c
@@ -364,8 +364,12 @@ void btrfs_drew_write_lock(struct btrfs_drew_lock *lock)
 
 void btrfs_drew_write_unlock(struct btrfs_drew_lock *lock)
 {
-	atomic_dec(&lock->writers);
-	cond_wake_up(&lock->pending_readers);
+	/*
+	 * atomic_dec_and_test() implies a full barrier, so woken up readers are
+	 * guaranteed to see the decrement.
+	 */
+	if (atomic_dec_and_test(&lock->writers))
+		wake_up(&lock->pending_readers);
 }
 
 void btrfs_drew_read_lock(struct btrfs_drew_lock *lock)
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH] btrfs: avoid pointless wake ups of drew lock readers
  2024-03-18 11:11 [PATCH] btrfs: avoid pointless wake ups of drew lock readers fdmanana
@ 2024-03-18 22:14 ` David Sterba
  0 siblings, 0 replies; 2+ messages in thread
From: David Sterba @ 2024-03-18 22:14 UTC (permalink / raw)
  To: fdmanana; +Cc: linux-btrfs

On Mon, Mar 18, 2024 at 11:11:56AM +0000, fdmanana@kernel.org wrote:
> From: Filipe Manana <fdmanana@suse.com>
> 
> When unlocking a write lock on a drew lock, at btrfs_drew_write_unlock(),
> it's pointless to wake up tasks waiting to acquire a read lock if we
> didn't decrement the 'writers' counter down to 0, since a read lock can
> only be acquired when the counter reaches a value of 0. Doing so is
> harmless from a functional point of view, but it's not efficient due to
> unnecessarily waking up tasks just for them to sleep again on the
> waitqueue.
> 
> So change this to wake up readers only if we decremented the 'writers'
> counter to 0.
> 
> Signed-off-by: Filipe Manana <fdmanana@suse.com>

Reviewed-by: David Sterba <dsterba@suse.com>

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2024-03-18 22:22 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-03-18 11:11 [PATCH] btrfs: avoid pointless wake ups of drew lock readers fdmanana
2024-03-18 22:14 ` David Sterba

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.