From: Waiman Long <longman@redhat.com>
To: "Lorenzo Stoakes (ARM)" <ljs@kernel.org>,
Peter Zijlstra <peterz@infradead.org>,
Ingo Molnar <mingo@redhat.com>, Will Deacon <will@kernel.org>,
Boqun Feng <boqun@kernel.org>, Nick Terrell <terrelln@fb.com>,
David Sterba <dsterba@suse.com>,
Nathan Chancellor <nathan@kernel.org>,
Nick Desaulniers <ndesaulniers@google.com>,
Bill Wendling <morbo@google.com>,
Justin Stitt <justinstitt@google.com>
Cc: linux-kernel@vger.kernel.org,
"David Hildenbrand (Arm)" <david@kernel.org>,
llvm@lists.linux.dev
Subject: Re: [PATCH v2] locking/rwsem: don't let readers retain the lock past a queued writer
Date: Tue, 1 Dec 2026 11:27:59 -0500 [thread overview]
Message-ID: <bfc87291-52d0-47ae-8cf8-9a28b636a2ad@redhat.com> (raw)
In-Reply-To: <20261126-rwsem-dont-steal-from-queued-writer-v2-1-9772914de49e@kernel.org>
On 11/26/26 5:14 PM, Lorenzo Stoakes (ARM) wrote:
> Since commit 1a728dff855a ("locking/rwsem: Enable reader optimistic lock
> stealing"), a racing read lock can steal an rwsem from under a queued
> writer whose handoff timeout hasn't expired yet.
>
> This is most problematic when an rwsem read lock is released then
> reacquired in order to relieve lock contention, as instead it will simply
> have the lock handed right back to it ahead of the intended beneficiary.
>
> Naturally, this behaviour rather defeats the object of doing this kind of
> thing, so this is less than ideal.
>
> Fix this by updating the optimistic read lock stealing code in
> rwsem_down_read_slowpath() to explicitly check whether the first queued
> lock is a waiting writer and the rwsem was last held by a reader, and to
> not allow the theft if so.
>
> This still permits reader stealing if a writer last held the lock, as
> failing to do so can starve readers in favour of writers. This maintains
> fairness.
Yes, I like this idea of maintaining some fairness.
>
> When a steal is disallowed, keep the existing sem->wait_lock held when
> jumping to the queue code to avoid useless lock acquisition/release.
>
> Also update the comment describing optimistic read lock acquisition
> conditions to include this newly introduced behaviour.
>
> Care is taken to ensure correct ordering of lock acquisition, lock waking
> and marking the lock reader-owned.
>
> Signed-off-by: Lorenzo Stoakes (ARM) <ljs@kernel.org>
> ---
> Testing:
>
> All on an AMD threadripper 9980X with 64 cores, one thread per core (SMT off),
> bare metal testing.
>
> Correctness
> -----------
> locktorture: RWSEM01-08 all pass, 30 minutes each, 0 read/write failures,
> no splats.
> Bare metal: no dmesg splats on any boot.
>
> Writer latency (microbenchmark, bare metal, p99)
> ------------------------------------------------
> readers base patched
> 2 3813 us 17 us
> 4 3868 us 29 us
> 16 5369 us 4008 us (within expected stddev)
> 62 4612 us 3835 us (ditto)
> 2, one CCD 3865 us 17 us
> 7, one CCD 3951 us 35 us
>
> Writer throughput +35-54% at 2 and 4 readers and in both one-CCD cases.
> Reader steals in this test: 4.8M -> 8.
Just to confirm if the reader steals had dropped from 4.8M to just 8.
That is really low. What is the test duration?
>
> stress-ng (bare metal, 64 instances, 2 boots each, alternating)
> ---------------------------------------------------------------
> vma 59.9k -> 67.1k bogo-ops/s +12.0%
> msg +9.5%
> madvise +5.0%
>
> vma in guests: 64.2k-66.7k vs 58.8k-61.0k base.
>
> will-it-scale (t=8 and t=64)
> ----------------------------
> mmap1, mmap2, brk1, open1, page_fault1/2/3, unlink1: within noise.
>
> Real workloads
> --------------
> base patched
> kernel build, LLVM (guest) 28.33 s 28.51 s +0.6%
> kernel build (bare metal) 26.86 s 27.03 s +0.6%
> relink with lld 6.136 s 6.154 s +0.3%
> zstd 0.617 s 0.610 s -1.1%
> ripgrep 0.154 s 0.153 s flat
> Go std build 3.718 s 3.707 s flat
>
> I.e. all within noise levels.
>
> v2:
> * Update to allow reader stealing if a writer last held the lock to avoid a
> spinning writer from starving readers.
> * Simplified the code.
> * Ran a full suite of tests and benchmarks on bare metal to ensure
> correctness and avoid performance regressions.
>
> v1:
> https://lore.kernel.org/r/20261105-rwsem-dont-steal-from-queued-writer-v1-1-275e070f426f@kernel.org
> ---
> kernel/locking/rwsem.c | 44 +++++++++++++++++++++++++++-----------------
> 1 file changed, 27 insertions(+), 17 deletions(-)
>
> diff --git a/kernel/locking/rwsem.c b/kernel/locking/rwsem.c
> index b9c180ac1eee..c49e518f1b2c 100644
> --- a/kernel/locking/rwsem.c
> +++ b/kernel/locking/rwsem.c
> @@ -59,7 +59,9 @@
> * A fast path reader optimistic lock stealing is supported when the rwsem
> * is previously owned by a writer and the following conditions are met:
> * - rwsem is not currently writer owned
> - * - the handoff isn't set.
> + * - the handoff isn't set
> + * - a writer isn't at the head of the wait queue while the rwsem was last
> + * held by a reader.
> */
> #define RWSEM_READER_OWNED (1UL << 0)
> #define RWSEM_NONSPINNABLE (1UL << 1)
> @@ -1018,47 +1020,55 @@ rwsem_down_read_slowpath(struct rw_semaphore *sem, long count, unsigned int stat
> {
> long adjustment = -RWSEM_READER_BIAS;
> long rcnt = (count >> RWSEM_READER_SHIFT);
> + bool reader_owned = atomic_long_read(&sem->owner) & RWSEM_READER_OWNED;
> struct rwsem_waiter waiter, *first;
> DEFINE_WAKE_Q(wake_q);
>
> - /*
> - * To prevent a constant stream of readers from starving a sleeping
> - * writer, don't attempt optimistic lock stealing if the lock is
> - * very likely owned by readers.
> - */
> - if ((atomic_long_read(&sem->owner) & RWSEM_READER_OWNED) &&
> - (rcnt > 1) && !(count & RWSEM_WRITER_LOCKED))
> - goto queue;
> -
> /*
> * Reader optimistic lock stealing.
> */
> if (!(count & (RWSEM_WRITER_LOCKED | RWSEM_FLAG_HANDOFF))) {
> - rwsem_set_reader_owned(sem);
> - lockevent_inc(rwsem_rlock_steal);
> -
> /*
> - * Wake up other readers in the wait queue if it is
> - * the first reader.
> + * To prevent a constant stream of readers from starving a
> + * sleeping writer, don't attempt optimistic lock stealing if
> + * the lock is very likely owned by readers.
> */
> + if (reader_owned && rcnt > 1)
> + goto queue;
> +
> if ((rcnt == 1) && (count & RWSEM_FLAG_WAITERS)) {
> raw_spin_lock_irq(&sem->wait_lock);
> - if (sem->first_waiter)
> + first = sem->first_waiter;
> + /*
> + * If a reader last held this lock, let a queued writer
> + * take precedence to avoid writer starvation.
> + */
> + if (reader_owned && first &&
> + first->type == RWSEM_WAITING_FOR_WRITE)
> + goto queue_locked;
> + rwsem_set_reader_owned(sem);
> + /* If first reader, wake other readers in queue. */
> + if (first)
> rwsem_mark_wake(sem, RWSEM_WAKE_READ_OWNED,
> &wake_q);
> raw_spin_unlock_irq(&sem->wait_lock);
> wake_up_q(&wake_q);
> + } else {
> + rwsem_set_reader_owned(sem);
> }
> +
> + lockevent_inc(rwsem_rlock_steal);
> return sem;
> }
>
> queue:
> + raw_spin_lock_irq(&sem->wait_lock);
> +queue_locked:
> waiter.task = current;
> waiter.type = RWSEM_WAITING_FOR_READ;
> waiter.timeout = jiffies + RWSEM_WAIT_TIMEOUT;
> waiter.handoff_set = false;
>
> - raw_spin_lock_irq(&sem->wait_lock);
> first = sem->first_waiter;
> if (!first) {
> /*
>
> ---
> base-commit: 05441d697f7f9a927264f4058cbbc6a03dfcac9d
> change-id: 20261105-rwsem-dont-steal-from-queued-writer-04b11d9d406b
Other than a question about the drop in reader stealing count, the patch
looks reasonable to me and the benchmark results look good too.
Reviewed-by: Waiman Long <longman@redhat.com>
prev parent reply other threads:[~2026-12-01 16:28 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-26 22:14 Lorenzo Stoakes (ARM)
2026-12-01 16:27 ` Waiman Long [this message]
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=bfc87291-52d0-47ae-8cf8-9a28b636a2ad@redhat.com \
--to=longman@redhat.com \
--cc=boqun@kernel.org \
--cc=david@kernel.org \
--cc=dsterba@suse.com \
--cc=justinstitt@google.com \
--cc=linux-kernel@vger.kernel.org \
--cc=ljs@kernel.org \
--cc=llvm@lists.linux.dev \
--cc=mingo@redhat.com \
--cc=morbo@google.com \
--cc=nathan@kernel.org \
--cc=ndesaulniers@google.com \
--cc=peterz@infradead.org \
--cc=terrelln@fb.com \
--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®