Re: [PATCH v3 1/2] locking/rwsem: Add a new RWSEM_ANONYMOUSLY_OWNED flag
From: Peter Zijlstra
Date: Tue May 15 2018 - 14:03:13 EST
On Tue, May 15, 2018 at 01:38:03PM -0400, Waiman Long wrote:
> diff --git a/kernel/locking/rwsem-xadd.c b/kernel/locking/rwsem-xadd.c
> index e795908..a27dbb4 100644
> --- a/kernel/locking/rwsem-xadd.c
> +++ b/kernel/locking/rwsem-xadd.c
> @@ -357,11 +357,8 @@ static inline bool rwsem_can_spin_on_owner(struct rw_semaphore *sem)
>
> rcu_read_lock();
> 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,8 +379,10 @@ 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 (!owner)
> + return true;
> + else if (!is_rwsem_owner_spinnable(owner))
> + return false;
s/else //
> rcu_read_lock();
> while (sem->owner == owner) {
> @@ -408,12 +407,12 @@ static noinline bool rwsem_spin_on_owner(struct rw_semaphore *sem)
> cpu_relax();
> }
> rcu_read_unlock();
> -out:
> +
> /*
> * 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));
> }
The above two cases have explicit owner tests, this one doesn't.
Now I know it works, but maybe:
return !owner || is_rwsem_owner_spinnable(owner);
is easier to read... dunno.