mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Mateusz Guzik <mjguzik@gmail.com>
To: longman@redhat.com
Cc: peterz@infradead.org, mingo@redhat.com, will@kernel.org,
	boqun.feng@gmail.com, linux-kernel@vger.kernel.org,
	Mateusz Guzik <mjguzik@gmail.com>
Subject: [PATCH] locking/rwsem: cpu_relax before re-reading owner in rwsem_spin_on_owner
Date: Wed, 22 May 2024 20:54:21 +0200	[thread overview]
Message-ID: <20240522185421.424344-1-mjguzik@gmail.com> (raw)

The function starts with establishing whether there is an owner it can
spin waiting on and proceeds to immediately do it again when entering
the loop, adding another lock word access and possibly an avoidable
cacheline bounce. Subsequent iterations don't have this problem.

The sound thing to do is to cpu_relax() first.

Signed-off-by: Mateusz Guzik <mjguzik@gmail.com>
---

This is a borderline cosmetic patch I did not bother benchmarking.
If you don't like it that's fine with me, I'm not going to fight for it.

Cheers.

 kernel/locking/rwsem.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/kernel/locking/rwsem.c b/kernel/locking/rwsem.c
index c6d17aee4209..a6c5bb68920e 100644
--- a/kernel/locking/rwsem.c
+++ b/kernel/locking/rwsem.c
@@ -758,6 +758,8 @@ rwsem_spin_on_owner(struct rw_semaphore *sem)
 		return state;
 
 	for (;;) {
+		cpu_relax();
+
 		/*
 		 * When a waiting writer set the handoff flag, it may spin
 		 * on the owner as well. Once that writer acquires the lock,
@@ -784,8 +786,6 @@ rwsem_spin_on_owner(struct rw_semaphore *sem)
 			state = OWNER_NONSPINNABLE;
 			break;
 		}
-
-		cpu_relax();
 	}
 
 	return state;
-- 
2.39.2


                 reply	other threads:[~2024-05-22 18:54 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=20240522185421.424344-1-mjguzik@gmail.com \
    --to=mjguzik@gmail.com \
    --cc=boqun.feng@gmail.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=longman@redhat.com \
    --cc=mingo@redhat.com \
    --cc=peterz@infradead.org \
    --cc=will@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

all inboxes | Powered by JetHome®