The Linux Kernel Mailing List
 help / color / mirror / Atom feed
From: Matthew Wilcox <willy@infradead.org>
To: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Peter Zijlstra <peterz@infradead.org>,
	Ingo Molnar <mingo@redhat.com>, Will Deacon <will@kernel.org>,
	Boqun Feng <boqun.feng@gmail.com>,
	Waiman Long <longman@redhat.com>,
	linux-kernel@vger.kernel.org,
	Christoph Hellwig <hch@infradead.org>,
	linux-fsdevel@vger.kernel.org
Subject: Re: [RFC 1/1] rwsem: Shrink rwsem by one pointer
Date: Wed, 4 Mar 2026 19:51:38 +0000	[thread overview]
Message-ID: <aaiNSqx5by3J5Cij@casper.infradead.org> (raw)
In-Reply-To: <CAHk-=wggNowW32UcNWHQ4Ak5J-0mZT_EO+PAEw2DLe7tr8-Dtg@mail.gmail.com>

On Wed, Feb 18, 2026 at 02:52:29PM -0800, Linus Torvalds wrote:
> On Wed, 18 Feb 2026 at 14:45, Linus Torvalds
> <torvalds@linux-foundation.org> wrote:
> >
> > Anyway, this is all from just looking at the patch, so maybe I missed
> > something, but it does look very wrong.
> 
> Bah. And immediately after sending, I went "maybe I should look at the
> code" more closely.
> 
> I think my suggestion to just remove the check was right, but the
> return value of rwsem_del_waiter() needs to be fixed to be the "I used
> to be the first waiter, but there are other waiters and I updated the
> first waiter pointer".

I tried to make this work, but it's a bit ugly.  rwsem_del_waiter()
needs to know whether there are remaining waiters, and
rwsem_del_wake_waiter() needs to know whether we deleted the first
waiter _and_ there are remaining ones.  So we end up returning a
tristate from __rwsem_del_waiter() and I find it less clear.

 static inline
-bool __rwsem_del_waiter(struct rw_semaphore *sem, struct rwsem_waiter *waiter)
+int __rwsem_del_waiter(struct rw_semaphore *sem, struct rwsem_waiter *waiter)
 {
+       int ret = 1;
+
        if (list_empty(&waiter->list)) {
                sem->first_waiter = NULL;
-               return true;
+               return 0;
        }

-       if (sem->first_waiter == waiter)
+       if (sem->first_waiter == waiter) {
                sem->first_waiter = list_first_entry(&waiter->list,
                                struct rwsem_waiter, list);
+               ret = 2;
+       }
        list_del(&waiter->list);

-       return false;
+       return ret;
 }
[...]
-static inline bool
-rwsem_del_waiter(struct rw_semaphore *sem, struct rwsem_waiter *waiter)
+static inline
+bool rwsem_del_waiter(struct rw_semaphore *sem, struct rwsem_waiter *waiter)
 {
+       int del_case;
+
        lockdep_assert_held(&sem->wait_lock);
-       if (__rwsem_del_waiter(sem, waiter))
-               return true;
-       atomic_long_andnot(RWSEM_FLAG_HANDOFF | RWSEM_FLAG_WAITERS, &sem->count);
-       return false;
+       del_case = __rwsem_del_waiter(sem, waiter);
+       if (del_case > 0)
+               atomic_long_andnot(RWSEM_FLAG_HANDOFF | RWSEM_FLAG_WAITERS,
+                               &sem->count);
+       return del_case == 2;
 }
[...]
 {
-       bool first = sem->first_waiter == waiter;
-
        wake_q_init(wake_q);

        /*
-        * If the wait_list isn't empty and the waiter to be deleted is
-        * the first waiter, we wake up the remaining waiters as they may
-        * be eligible to acquire or spin on the lock.
+        * If the deleted waiter was the first one and there are other
+        * waiters, we wake them up as they may be eligible to acquire
+        * or spin on the lock.
         */
-       if (rwsem_del_waiter(sem, waiter) && first)
+       if (rwsem_del_waiter(sem, waiter))
                rwsem_mark_wake(sem, RWSEM_WAKE_ANY, wake_q);


Even if we use a nice enum instead of 0/1/2 for the return value, I
don't think this is an improvement.  I played around with a couple of
other ways to refactor this and didn't come up with anything pretty.

  reply	other threads:[~2026-03-04 19:51 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-02-17 19:08 [RFC 0/1] Shrinking rwsem Matthew Wilcox (Oracle)
2026-02-17 19:08 ` [RFC 1/1] rwsem: Shrink rwsem by one pointer Matthew Wilcox (Oracle)
2026-02-17 20:27   ` Linus Torvalds
2026-02-18 21:00     ` Matthew Wilcox
2026-02-18 21:37       ` Linus Torvalds
2026-02-18 22:26       ` Peter Zijlstra
2026-02-18 22:45       ` Linus Torvalds
2026-02-18 22:52         ` Linus Torvalds
2026-03-04 19:51           ` Matthew Wilcox [this message]
2026-02-18 22:47   ` Waiman Long
2026-02-18 23:06     ` Matthew Wilcox

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=aaiNSqx5by3J5Cij@casper.infradead.org \
    --to=willy@infradead.org \
    --cc=boqun.feng@gmail.com \
    --cc=hch@infradead.org \
    --cc=linux-fsdevel@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=longman@redhat.com \
    --cc=mingo@redhat.com \
    --cc=peterz@infradead.org \
    --cc=torvalds@linux-foundation.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