Skip to contentSkip to conversation

locking/rwsem: fix comments about reader optimistic lock stealing conditions

PATCH New Yanfei Xu 1 participant 2 messages Sep 27, 2021 Locking primitives

lore
Share & tools
lore

Yanfei Xu

author
#0 lore raw

PATCH 1/2 locking/rwsem: fix comments about reader optimistic lock stealing conditions

After the commit 617f3ef95177 ("locking/rwsem: Remove reader optimistic spinning"), reader doesn't support optimistic spinning anymore, there is no need meet the condition which OSQ is empty.

BTW, add an unlikely() for the max reader wakeup check.

---
 kernel/locking/rwsem.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
kernel/locking/rwsem.c +1 -2
index 000e8d5a2884..2907f9d8e4a6 100644
@@ -56,7 +56,6 @@
  *
  * A fast path reader optimistic lock stealing is supported when the rwsem
  * is previously owned by a writer and the following conditions are met:
- *  - OSQ is empty
  *  - rwsem is not currently writer owned
  *  - the handoff isn't set.
  */
@@ -485,7 +484,7 @@ static void rwsem_mark_wake(struct rw_semaphore *sem,
 		/*
 		 * Limit # of readers that can be woken up per wakeup call.
 		 */
-		if (woken >= MAX_READERS_WAKEUP)
+		if (unlikely(woken >= MAX_READERS_WAKEUP))
 			break;
 	}
 
Signature · 1 line
2.27.0
author
#1 lore raw

PATCH 2/2 locking/rwsem: add preempt_disable/enable for spin region

The rwsem_optimistic_spin() invokes the rwsem_spin_on_owner() with preemption disabled, but rwsem_down_write_slowpath() doesn't. Because the spin region should not be preempted, add preempt_disable/enable() in rwsem_spin_on_owner().

---
 kernel/locking/rwsem.c | 2 ++
 1 file changed, 2 insertions(+)
kernel/locking/rwsem.c +2 -0
index 2907f9d8e4a6..db3a6a06a9c8 100644
@@ -674,6 +674,7 @@ rwsem_spin_on_owner(struct rw_semaphore *sem)
 	if (state != OWNER_WRITER)
 		return state;
 
+	preempt_disable();
 	rcu_read_lock();
 	for (;;) {
 		/*
@@ -704,6 +705,7 @@ rwsem_spin_on_owner(struct rw_semaphore *sem)
 		cpu_relax();
 	}
 	rcu_read_unlock();
+	preempt_enable();
 
 	return state;
 }
Signature · 1 line
2.27.0