Re: [PATCH RESEND 1/3] locking: add rwsem_is_write_locked(), update non-lockdep asserts
From: Lorenzo Stoakes
Date: Fri Jan 16 2026 - 11:31:04 EST
On Fri, Jan 16, 2026 at 10:08:00AM -0500, Zi Yan wrote:
> On 16 Jan 2026, at 8:36, Lorenzo Stoakes wrote:
>
> > As part of adding some additional lock asserts in mm, we wish to be able to
> > determine if a read/write semaphore is write-locked, so add
> > rwsem_is_write_locked() to do the write-lock equivalent of
> > rwsem_is_locked().
> >
> > While we're here, update rwsem_assert_[write_]held_nolockdep() to utilise
> > the rwsem_is_[write_]locked() helpers directly to reduce code duplication,
> > and also update rwsem_is_locked() to take a const rwsem and return a
> > boolean.
> >
> > This patch also updates the CONFIG_PREEMPT_RT helpers to do the same thing
> > there.
> >
> > Signed-off-by: Lorenzo Stoakes <lorenzo.stoakes@xxxxxxxxxx>
> > ---
> > include/linux/rwsem.h | 20 +++++++++++++++-----
> > 1 file changed, 15 insertions(+), 5 deletions(-)
> >
> > diff --git a/include/linux/rwsem.h b/include/linux/rwsem.h
> > index f1aaf676a874..b25b7944ad99 100644
> > --- a/include/linux/rwsem.h
> > +++ b/include/linux/rwsem.h
> > @@ -70,19 +70,24 @@ struct rw_semaphore {
> > #define RWSEM_WRITER_LOCKED (1UL << 0)
> > #define __RWSEM_COUNT_INIT(name) .count = ATOMIC_LONG_INIT(RWSEM_UNLOCKED_VALUE)
> >
> > -static inline int rwsem_is_locked(struct rw_semaphore *sem)
> > +static inline bool rwsem_is_locked(const struct rw_semaphore *sem)
> > {
> > return atomic_long_read(&sem->count) != RWSEM_UNLOCKED_VALUE;
> > }
> >
> > +static inline bool rwsem_is_write_locked(const struct rw_semaphore *sem)
> > +{
> > + return atomic_long_read(&sem->count) & RWSEM_WRITER_LOCKED;
> > +}
> > +
> > static inline void rwsem_assert_held_nolockdep(const struct rw_semaphore *sem)
> > {
> > - WARN_ON(atomic_long_read(&sem->count) == RWSEM_UNLOCKED_VALUE);
> > + WARN_ON(!rwsem_is_locked(sem));
> > }
> >
> > static inline void rwsem_assert_held_write_nolockdep(const struct rw_semaphore *sem)
> > {
> > - WARN_ON(!(atomic_long_read(&sem->count) & RWSEM_WRITER_LOCKED));
> > + WARN_ON(!rwsem_is_write_locked(sem));
> > }
> >
> > /* Common initializer macros and functions */
> > @@ -174,11 +179,16 @@ do { \
> > __init_rwsem((sem), #sem, &__key); \
> > } while (0)
> >
> > -static __always_inline int rwsem_is_locked(const struct rw_semaphore *sem)
> > +static __always_inline bool rwsem_is_locked(const struct rw_semaphore *sem)
> > {
> > return rw_base_is_locked(&sem->rwbase);
> > }
> >
> > +static __always_inline bool rwsem_is_write_locked(const struct rw_semaphore *sem)
> > +{
> > + return rw_base_is_write_locked(&sem->rwbase);
> > +}
> > +
> > static __always_inline void rwsem_assert_held_nolockdep(const struct rw_semaphore *sem)
> > {
> > WARN_ON(!rwsem_is_locked(sem));
> > @@ -186,7 +196,7 @@ static __always_inline void rwsem_assert_held_nolockdep(const struct rw_semaphor
> >
> > static __always_inline void rwsem_assert_held_write_nolockdep(const struct rw_semaphore *sem)
> > {
> > - WARN_ON(!rw_base_is_write_locked(&sem->rwbase));
> > + WARN_ON(!rwsem_is_write_locked(sem));
>
> I thought it was wrong since rwsem_is_write_locked() at the top reads ->count
> instead of ->rwbase until I see there is another rwsem_is_write_locked() above.
:)
>
>
> > }
> >
> > static __always_inline int rwsem_is_contended(struct rw_semaphore *sem)
> > --
>
> Reviewed-by: Zi Yan <ziy@xxxxxxxxxx>
Thanks!
>
>
> --
> Best Regards,
> Yan, Zi