locking/mutex: Refactor mutex_spin_on_owner()
PATCH 1/1 locking/mutex: Refactor mutex_spin_on_owner()
This patch applies on top of tip.
-------------------------------------------------------------------
Similar to what Linus suggested for rwsem_spin_on_owner(), in mutex_spin_on_owner(), instead of having while (true) and breaking out of the spin loop on lock->owner != owner, we can have the loop directly check for while (lock->owner == owner). This improves the readability of the code.
--- kernel/locking/mutex.c | 17 +++++------------ 1 files changed, 5 insertions(+), 12 deletions(-)
kernel/locking/mutex.c +5 -12
@@ -224,16 +224,8 @@ ww_mutex_set_context_slowpath(struct ww_mutex *lock,static noinline bool mutex_spin_on_owner(struct mutex *lock, struct task_struct *owner) { -bool ret; -rcu_read_lock (); -while (true) { -/* Return success when the lock owner changed */ -if (lock->owner != owner) { - ret = true; -break ; - } - +while (lock->owner == owner) {/* * Ensure we emit the owner->on_cpu, dereference _after_ * checking lock->owner still matches owner, if that fails,
@@ -242,16 +234,17 @@ bool mutex_spin_on_owner(struct mutex *lock, struct task_struct *owner)*/ barrier (); +/* Stop spinning when need_resched or owner is not running. */ if (!owner->on_cpu ||need_resched ()) { - ret = false; -break ; +rcu_read_unlock (); +return false; }cpu_relax_lowlatency (); }rcu_read_unlock (); -return ret; +return true; }/*
Signature · 1 line
1.7.2.5
Quoted 44 lines · Jason Low
This patch applies on top of tip.
-------------------------------------------------------------------
Similar to what Linus suggested forrwsem_spin_on_owner(), inmutex_spin_on_owner(), instead of having while (true) and breaking out of the spin loop onlock->owner!= owner, we can have the loop directly check for while (lock->owner== owner). This improves the readability of the code.Signed-off-by Jason Low <[email protected]>--- kernel/locking/mutex.c | 17 +++++------------ 1 files changed, 5 insertions(+), 12 deletions(-)kernel/locking/mutex.c +2
-11@@ -224,16 +224,8 @@ ww_mutex_set_context_slowpath(struct ww_mutex *lock,static noinline bool mutex_spin_on_owner(struct mutex *lock, struct task_struct *owner) { -bool ret; -rcu_read_lock (); -while (true) { -/* Return success when the lock owner changed */ -if (lock->owner != owner) { - ret = true; -break ; - } - +while (lock->owner == owner) {/* * Ensure we emit the owner->on_cpu, dereference _after_ * checking lock->owner still matches owner, if that fails, @@ -242,16 +234,17 @@ bool mutex_spin_on_owner(struct mutex *lock, struct task_struct *owner)*/ barrier (); +/* Stop spinning when need_resched or owner is not running. */ if (!owner->on_cpu ||need_resched ()) { - ret = false; -break ;
kernel/locking/mutex.c bool mutex_spin_on_owner(struct mutex *lock, struct task_struct *owner) · line 247
Quoted 10 lines
kernel/locking/mutex.c +3
-1+rcu_read_unlock (); +return false; }cpu_relax_lowlatency (); }rcu_read_unlock (); -return ret; +return true;
A nit: having multiple return statements in a function is not the cleanest approach, especially when we are holding locks.
It's better to add an 'out_unlock' label to before the rcu_read_unlock() and use that plus 'ret'.
Thanks,
Ingo
Quoted 54 lines · Ingo Molnar
Quoted 52 lines · Jason Low
This patch applies on top of tip.
-------------------------------------------------------------------
Similar to what Linus suggested forrwsem_spin_on_owner(), inmutex_spin_on_owner(), instead of having while (true) and breaking out of the spin loop onlock->owner!= owner, we can have the loop directly check for while (lock->owner== owner). This improves the readability of the code.Signed-off-by Jason Low <[email protected]>--- kernel/locking/mutex.c | 17 +++++------------ 1 files changed, 5 insertions(+), 12 deletions(-)kernel/locking/mutex.c +4
-11@@ -224,16 +224,8 @@ ww_mutex_set_context_slowpath(struct ww_mutex *lock,static noinline bool mutex_spin_on_owner(struct mutex *lock, struct task_struct *owner) { -bool ret; -rcu_read_lock (); -while (true) { -/* Return success when the lock owner changed */ -if (lock->owner != owner) { - ret = true; -break ; - } - +while (lock->owner == owner) {/* * Ensure we emit the owner->on_cpu, dereference _after_ * checking lock->owner still matches owner, if that fails, @@ -242,16 +234,17 @@ bool mutex_spin_on_owner(struct mutex *lock, struct task_struct *owner)*/ barrier (); +/* Stop spinning when need_resched or owner is not running. */ if (!owner->on_cpu ||need_resched ()) { - ret = false; -break ; +rcu_read_unlock (); +return false; }cpu_relax_lowlatency (); }rcu_read_unlock ();
Quoted 8 lines
kernel/locking/mutex.c bool mutex_spin_on_owner(struct mutex *lock, struct task_struct *owner) · line 247
Quoted 2 lines
kernel/locking/mutex.c +1
-1-return ret; +return true;A nit: having multiple return statements in a function is not the cleanest approach, especially when we are holding locks.
It's better to add an '
out_unlock' label to before thercu_read_unlock()and use that plus 'ret'.
Okay, I can update this patch. Should we make another similar update for the rwsem then?
Quoted 61 lines · Jason Low
Quoted 60 lines · Ingo Molnar
Quoted 44 lines · Jason Low
This patch applies on top of tip.
-------------------------------------------------------------------
Similar to what Linus suggested forrwsem_spin_on_owner(), inmutex_spin_on_owner(), instead of having while (true) and breaking out of the spin loop onlock->owner!= owner, we can have the loop directly check for while (lock->owner== owner). This improves the readability of the code.Signed-off-by Jason Low <[email protected]>--- kernel/locking/mutex.c | 17 +++++------------ 1 files changed, 5 insertions(+), 12 deletions(-)kernel/locking/mutex.c +2
-11@@ -224,16 +224,8 @@ ww_mutex_set_context_slowpath(struct ww_mutex *lock,static noinline bool mutex_spin_on_owner(struct mutex *lock, struct task_struct *owner) { -bool ret; -rcu_read_lock (); -while (true) { -/* Return success when the lock owner changed */ -if (lock->owner != owner) { - ret = true; -break ; - } - +while (lock->owner == owner) {/* * Ensure we emit the owner->on_cpu, dereference _after_ * checking lock->owner still matches owner, if that fails, @@ -242,16 +234,17 @@ bool mutex_spin_on_owner(struct mutex *lock, struct task_struct *owner)*/ barrier (); +/* Stop spinning when need_resched or owner is not running. */ if (!owner->on_cpu ||need_resched ()) { - ret = false; -break ;kernel/locking/mutex.c bool mutex_spin_on_owner(struct mutex *lock, struct task_struct *owner) · line 247
Quoted 10 lines
kernel/locking/mutex.c +3
-1+rcu_read_unlock (); +return false; }cpu_relax_lowlatency (); }rcu_read_unlock (); -return ret; +return true;A nit: having multiple return statements in a function is not the cleanest approach, especially when we are holding locks.
Quoted 5 lines
Quoted 2 lines
It's better to add an '
out_unlock' label to before thercu_read_unlock()and use that plus 'ret'.Okay, I can update this patch. Should we make another similar update for the rwsem then?
Yeah, I suppose so.
Thanks,
Ingo