Re: [PATCH v2 2/3] locking/rwsem: disable preemption for spinning region

From: Peter Zijlstra
Date: Fri Oct 15 2021 - 06:14:22 EST


On Wed, Oct 13, 2021 at 09:41:53PM +0800, Yanfei Xu wrote:
> The spinning region rwsem_spin_on_owner() should not be preempted,
> however the rwsem_down_write_slowpath() invokes it and don't disable
> preemption. Fix it by adding a pair of preempt_disable/enable().

I'm thinking we should do this patch before #1, otherwise we have a
single patch window where we'll trigger the assertion, no?

>
> Signed-off-by: Yanfei Xu <yanfei.xu@xxxxxxxxxxxxx>
> ---
> kernel/locking/rwsem.c | 11 ++++++++---
> 1 file changed, 8 insertions(+), 3 deletions(-)
>
> diff --git a/kernel/locking/rwsem.c b/kernel/locking/rwsem.c
> index 7b5af452ace2..06925b43c3e7 100644
> --- a/kernel/locking/rwsem.c
> +++ b/kernel/locking/rwsem.c
> @@ -1024,6 +1024,7 @@ rwsem_down_write_slowpath(struct rw_semaphore *sem, int state)
> enum writer_wait_state wstate;
> struct rwsem_waiter waiter;
> struct rw_semaphore *ret = sem;
> + enum owner_state owner_state;
> DEFINE_WAKE_Q(wake_q);
>
> /* do optimistic spinning and steal lock if possible */
> @@ -1099,9 +1100,13 @@ rwsem_down_write_slowpath(struct rw_semaphore *sem, int state)
> * In this case, we attempt to acquire the lock again
> * without sleeping.
> */
> - if (wstate == WRITER_HANDOFF &&
> - rwsem_spin_on_owner(sem) == OWNER_NULL)
> - goto trylock_again;
> + if (wstate == WRITER_HANDOFF) {
> + preempt_disable();
> + owner_state = rwsem_spin_on_owner(sem);
> + preempt_enable();
> + if (owner_state == OWNER_NULL)
> + goto trylock_again;
> + }
>
> /* Block until there are no active lockers. */
> for (;;) {
> --
> 2.27.0
>