[PATCH 1/4] locking/rwsem: Add missing ACQUIRE to read_slowpath exit when queue is empty

From: Peter Zijlstra
Date: Thu Jul 18 2019 - 09:53:41 EST

From: Jan Stancek <jstancek@xxxxxxxxxx>

LTP mtest06 has been observed to occasionally hit "still mapped when
deleted" and following BUG_ON on arm64.

The extra mapcount originated from pagefault handler, which handled
pagefault for vma that has already been detached. vma is detached
under mmap_sem write lock by detach_vmas_to_be_unmapped(), which
also invalidates vmacache.

When the pagefault handler (under mmap_sem read lock) calls
find_vma(), vmacache_valid() wrongly reports vmacache as valid.

After rwsem down_read() returns via 'queue empty' path (as of v5.2),
it does so without an ACQUIRE on sem->count:

if (list_empty(&sem->wait_list)) {
if (atomic_long_read(&sem->count) >= 0) {
return sem;

The problem can be reproduced by running LTP mtest06 in a loop and
building the kernel (-j $NCPUS) in parallel. It does reproduces since
v4.20 on arm64 HPE Apollo 70 (224 CPUs, 256GB RAM, 2 nodes). It
triggers reliably in about an hour.

The patched kernel ran fine for 10+ hours.

Cc: dbueso@xxxxxxx
Cc: mingo@xxxxxxxxxx
Fixes: 4b486b535c33 ("locking/rwsem: Exit read lock slowpath if queue empty & no writer")
Signed-off-by: Jan Stancek <jstancek@xxxxxxxxxx>
Reviewed-by: Will Deacon <will@xxxxxxxxxx>
Acked-by: Waiman Long <longman@xxxxxxxxxx>
Signed-off-by: Peter Zijlstra (Intel) <peterz@xxxxxxxxxxxxx>
Link: https://lkml.kernel.org/r/50b8914e20d1d62bb2dee42d342836c2c16ebee7.1563438048.git.jstancek@xxxxxxxxxx
kernel/locking/rwsem.c | 2 ++
1 file changed, 2 insertions(+)

--- a/kernel/locking/rwsem.c
+++ b/kernel/locking/rwsem.c
@@ -1032,6 +1032,8 @@ rwsem_down_read_slowpath(struct rw_semap
if (adjustment && !(atomic_long_read(&sem->count) &
+ /* Provide lock ACQUIRE */
+ smp_acquire__after_ctrl_dep();