mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH v2] locking/mutex: remove rcu_read_lock/unlock as we already disabled preemption
@ 2021-10-08  3:25 Yanfei Xu
  2021-10-12 13:15 ` Peter Zijlstra
  0 siblings, 1 reply; 3+ messages in thread
From: Yanfei Xu @ 2021-10-08  3:25 UTC (permalink / raw)
  To: peterz, mingo, will, longman, boqun.feng; +Cc: linux-kernel

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.

Signed-off-by: Yanfei Xu <yanfei.xu@windriver.com>
---
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(-)

diff --git a/kernel/locking/mutex.c b/kernel/locking/mutex.c
index 2fede72b6af5..2f654cfb10d9 100644
--- a/kernel/locking/mutex.c
+++ b/kernel/locking/mutex.c
@@ -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;
 }
-- 
2.27.0


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH v2] locking/mutex: remove rcu_read_lock/unlock as we already disabled preemption
  2021-10-08  3:25 [PATCH v2] locking/mutex: remove rcu_read_lock/unlock as we already disabled preemption Yanfei Xu
@ 2021-10-12 13:15 ` Peter Zijlstra
  2021-10-13  7:54   ` Xu, Yanfei
  0 siblings, 1 reply; 3+ messages in thread
From: Peter Zijlstra @ 2021-10-12 13:15 UTC (permalink / raw)
  To: Yanfei Xu; +Cc: mingo, will, longman, boqun.feng, linux-kernel

On Fri, Oct 08, 2021 at 11:25:18AM +0800, Yanfei Xu wrote:
> 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.
> 
> Signed-off-by: Yanfei Xu <yanfei.xu@windriver.com>
> ---
> 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(-)
> 
> diff --git a/kernel/locking/mutex.c b/kernel/locking/mutex.c
> index 2fede72b6af5..2f654cfb10d9 100644
> --- a/kernel/locking/mutex.c
> +++ b/kernel/locking/mutex.c
> @@ -351,13 +351,14 @@ bool mutex_spin_on_owner(struct mutex *lock, struct task_struct *owner,
>  {
>  	bool ret = true;

	lockdep_assert_preemption_disabled();

> -	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?

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH v2] locking/mutex: remove rcu_read_lock/unlock as we already disabled preemption
  2021-10-12 13:15 ` Peter Zijlstra
@ 2021-10-13  7:54   ` Xu, Yanfei
  0 siblings, 0 replies; 3+ messages in thread
From: Xu, Yanfei @ 2021-10-13  7:54 UTC (permalink / raw)
  To: Peter Zijlstra; +Cc: mingo, will, longman, boqun.feng, linux-kernel



On 10/12/21 9:15 PM, Peter Zijlstra wrote:
> [Please note: This e-mail is from an EXTERNAL e-mail address]
> 
> On Fri, Oct 08, 2021 at 11:25:18AM +0800, Yanfei Xu wrote:
>> 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.
>>
>> Signed-off-by: Yanfei Xu <yanfei.xu@windriver.com>
>> ---
>> 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(-)
>>
>> diff --git a/kernel/locking/mutex.c b/kernel/locking/mutex.c
>> index 2fede72b6af5..2f654cfb10d9 100644
>> --- a/kernel/locking/mutex.c
>> +++ b/kernel/locking/mutex.c
>> @@ -351,13 +351,14 @@ bool mutex_spin_on_owner(struct mutex *lock, struct task_struct *owner,
>>   {
>>        bool ret = true;
> 
>          lockdep_assert_preemption_disabled();

Agree.

> 
>> -     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

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2021-10-13  7:54 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-10-08  3:25 [PATCH v2] locking/mutex: remove rcu_read_lock/unlock as we already disabled preemption Yanfei Xu
2021-10-12 13:15 ` Peter Zijlstra
2021-10-13  7:54   ` Xu, Yanfei

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®