Commit d7d760ef authored by Waiman Long's avatar Waiman Long Committed by Ingo Molnar

locking/rwsem: Add a new RWSEM_ANONYMOUSLY_OWNED flag

There are use cases where a rwsem can be acquired by one task, but
released by another task. In thess cases, optimistic spinning may need
to be disabled.  One example will be the filesystem freeze/thaw code
where the task that freezes the filesystem will acquire a write lock
on a rwsem and then un-owns it before returning to userspace. Later on,
another task will come along, acquire the ownership, thaw the filesystem
and release the rwsem.

Bit 0 of the owner field was used to designate that it is a reader
owned rwsem. It is now repurposed to mean that the owner of the rwsem
is not known. If only bit 0 is set, the rwsem is reader owned. If bit
0 and other bits are set, it is writer owned with an unknown owner.
One such value for the latter case is (-1L). So we can set owner to 1 for
reader-owned, -1 for writer-owned. The owner is unknown in both cases.

To handle transfer of rwsem ownership, the higher level code should
set the owner field to -1 to indicate a write-locked rwsem with unknown
owner.  Optimistic spinning will be disabled in this case.

Once the higher level code figures who the new owner is, it can then
set the owner field accordingly.
Tested-by: default avatarAmir Goldstein <>
Signed-off-by: default avatarWaiman Long <>
Acked-by: default avatarPeter Zijlstra <>
Cc: Andrew Morton <>
Cc: Davidlohr Bueso <>
Cc: Jan Kara <>
Cc: Linus Torvalds <>
Cc: Matthew Wilcox <>
Cc: Oleg Nesterov <>
Cc: Paul E. McKenney <>
Cc: Theodore Y. Ts'o <>
Cc: Thomas Gleixner <>
Cc: Will Deacon <>
Link: default avatarIngo Molnar <>
parent 67b8d5c7
......@@ -357,11 +357,8 @@ static inline bool rwsem_can_spin_on_owner(struct rw_semaphore *sem)
owner = READ_ONCE(sem->owner);
if (!rwsem_owner_is_writer(owner)) {
* Don't spin if the rwsem is readers owned.
ret = !rwsem_owner_is_reader(owner);
if (!owner || !is_rwsem_owner_spinnable(owner)) {
ret = !owner; /* !owner is spinnable */
goto done;
......@@ -382,11 +379,11 @@ static noinline bool rwsem_spin_on_owner(struct rw_semaphore *sem)
struct task_struct *owner = READ_ONCE(sem->owner);
if (!rwsem_owner_is_writer(owner))
goto out;
if (!is_rwsem_owner_spinnable(owner))
return false;
while (sem->owner == owner) {
while (owner && (READ_ONCE(sem->owner) == owner)) {
* Ensure we emit the owner->on_cpu, dereference _after_
* checking sem->owner still matches owner, if that fails,
......@@ -408,12 +405,12 @@ static noinline bool rwsem_spin_on_owner(struct rw_semaphore *sem)
* If there is a new owner or the owner is not set, we continue
* spinning.
return !rwsem_owner_is_reader(READ_ONCE(sem->owner));
return is_rwsem_owner_spinnable(READ_ONCE(sem->owner));
static bool rwsem_optimistic_spin(struct rw_semaphore *sem)
......@@ -221,5 +221,3 @@ void up_read_non_owner(struct rw_semaphore *sem)
/* SPDX-License-Identifier: GPL-2.0 */
* The owner field of the rw_semaphore structure will be set to
* RWSEM_READ_OWNED when a reader grabs the lock. A writer will clear
* RWSEM_READER_OWNED when a reader grabs the lock. A writer will clear
* the owner field when it unlocks. A reader, on the other hand, will
* not touch the owner field when it unlocks.
* In essence, the owner field now has the following 3 states:
* In essence, the owner field now has the following 4 states:
* 1) 0
* - lock is free or the owner hasn't set the field yet
* - lock is currently or previously owned by readers (lock is free
* or not set by owner yet)
* 3) Other non-zero value
* - a writer owns the lock
* 3) RWSEM_ANONYMOUSLY_OWNED bit set with some other bits set as well
* - lock is owned by an anonymous writer, so spinning on the lock
* owner should be disabled.
* 4) Other non-zero value
* - a writer owns the lock and other writers can spin on the lock owner.
#define RWSEM_READER_OWNED ((struct task_struct *)1UL)
#define RWSEM_READER_OWNED ((struct task_struct *)RWSEM_ANONYMOUSLY_OWNED)
......@@ -51,14 +55,22 @@ static inline void rwsem_set_reader_owned(struct rw_semaphore *sem)
static inline bool rwsem_owner_is_writer(struct task_struct *owner)
* Return true if the a rwsem waiter can spin on the rwsem's owner
* and steal the lock, i.e. the lock is not anonymously owned.
* N.B. !owner is considered spinnable.
static inline bool is_rwsem_owner_spinnable(struct task_struct *owner)
return owner && owner != RWSEM_READER_OWNED;
return !((unsigned long)owner & RWSEM_ANONYMOUSLY_OWNED);
static inline bool rwsem_owner_is_reader(struct task_struct *owner)
* Return true if rwsem is owned by an anonymous writer or readers.
static inline bool rwsem_has_anonymous_owner(struct task_struct *owner)
return owner == RWSEM_READER_OWNED;
return (unsigned long)owner & RWSEM_ANONYMOUSLY_OWNED;
static inline void rwsem_set_owner(struct rw_semaphore *sem)
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment