Skip to contentSkip to conversation

locking/mutex: remove rcu_read_lock/unlock as we already disabled preemption

PATCH Superseded Yanfei Xu 2 participants 3 messages Oct 8, 2021 – Oct 13, 2021 Locking primitives

lore
Share & tools
lore

Yanfei Xu

author
#0 lore raw

PATCH v2 1/1 locking/mutex: remove rcu_read_lock/unlock as we already disabled preemption

preempt_disable/enable() is equal to RCU read-side crital section, and the mutex lock slowpath disabled the preemption for the optimistic spinning code. Let's remove the rcu_read_lock/unlock for saving some cycles in hot codes.

---
v1->v2: fix the incorrect comment in code and commit message.
thanks for WaiMan's suggestion.

BTW, sorry for this late v2 due to a long vocation.

 kernel/locking/mutex.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)
kernel/locking/mutex.c +5 -5
index 2fede72b6af5..2f654cfb10d9 100644
@@ -351,13 +351,14 @@ bool mutex_spin_on_owner(struct mutex *lock, struct task_struct *owner,
 {
 	bool ret = true;
 
-	rcu_read_lock();
 	while (__mutex_owner(lock) == owner) {
 		/*
 		 * Ensure we emit the owner->on_cpu, dereference _after_
-		 * checking lock->owner still matches owner. If that fails,
-		 * owner might point to freed memory. If it still matches,
-		 * the rcu_read_lock() ensures the memory stays valid.
+		 * checking lock->owner still matches owner. And we already
+		 * disabled preemption which is equal to the RCU read-side
+		 * crital section in optimistic spinning code. Thus the
+		 * task_strcut structure won't go away during the spinning
+		 * period
 		 */
 		barrier();
 
@@ -377,7 +378,6 @@ bool mutex_spin_on_owner(struct mutex *lock, struct task_struct *owner,
 
 		cpu_relax();
 	}
-	rcu_read_unlock();
 
 	return ret;
 }
Signature · 1 line
2.27.0
maintainer
#1 lore raw
Quoted 20 lines · Yanfei Xu

preempt_disable/enable() is equal to RCU read-side crital section, and the mutex lock slowpath disabled the preemption for the optimistic spinning code. Let's remove the rcu_read_lock/unlock for saving some cycles in hot codes.

---
v1->v2: fix the incorrect comment in code and commit message.
thanks for WaiMan's suggestion.

BTW, sorry for this late v2 due to a long vocation.

 kernel/locking/mutex.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)
kernel/locking/mutex.c +0 -0
index 2fede72b6af5..2f654cfb10d9 100644

kernel/locking/mutex.c bool mutex_spin_on_owner(struct mutex *lock, struct task_struct *owner, · line 352

Quoted 2 lines
kernel/locking/mutex.c +0 -0
@@ -351,13 +351,14 @@ bool mutex_spin_on_owner(struct mutex *lock, struct task_struct *owner,
 {
 	bool ret = true;
	lockdep_assert_preemption_disabled();

kernel/locking/mutex.c bool mutex_spin_on_owner(struct mutex *lock, struct task_struct *owner, · line 356

Quoted 4 lines
kernel/locking/mutex.c +0 -1
-	rcu_read_lock();
 	while (__mutex_owner(lock) == owner) {
 		/*
 		 * Ensure we emit the owner->on_cpu, dereference _after_

And did you check the other code in locking/ for similar things?

author
#2 lore raw
Quoted 18 lines · Peter Zijlstra

[Please note: This e-mail is from an EXTERNAL e-mail address]

Quoted 15 lines · Yanfei Xu

preempt_disable/enable() is equal to RCU read-side crital section, and the mutex lock slowpath disabled the preemption for the optimistic spinning code. Let's remove the rcu_read_lock/unlock for saving some cycles in hot codes.

---
v1->v2: fix the incorrect comment in code and commit message.
thanks for WaiMan's suggestion.

BTW, sorry for this late v2 due to a long vocation.

  kernel/locking/mutex.c | 10 +++++-----
  1 file changed, 5 insertions(+), 5 deletions(-)
Quoted 9 lines

kernel/locking/mutex.c bool mutex_spin_on_owner(struct mutex *lock, struct task_struct *owner, · line 352

Quoted 7 lines
kernel/locking/mutex.c +0 -0
index 2fede72b6af5..2f654cfb10d9 100644
@@ -351,13 +351,14 @@ bool mutex_spin_on_owner(struct mutex *lock, struct task_struct *owner,
  {
       bool ret = true;
         lockdep_assert_preemption_disabled();

Agree.

Quoted 8 lines

kernel/locking/mutex.c bool mutex_spin_on_owner(struct mutex *lock, struct task_struct *owner, · line 356

Quoted 5 lines
kernel/locking/mutex.c +0 -1
-     rcu_read_lock();
       while (__mutex_owner(lock) == owner) {
               /*
                * Ensure we emit the owner->on_cpu, dereference _after_

And did you check the other code in locking/ for similar things?

I did a check, rwsem also has the similar things. Will do it for rwsem in v3.

Thanks,
Yanfei