Skip to contentSkip to conversation

spinlock: improve spin_is_locked() for recursive locks

PATCH In review Jan Beulich 6 participants 8 messages Mar 24, 2016 – Mar 29, 2016

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 spinlock: improve spin_is_locked() for recursive locks

Recursive locks know their current owner, and since we use the function solely to determine whether a particular lock is being held by the current CPU (which so far has been an imprecise check), make actually check the owner for recusrively acquired locks.

xen/common/spinlock.c +3 -1
@@ -188,7 +188,9 @@ void _spin_unlock_irqrestore(spinlock_t
 int _spin_is_locked(spinlock_t *lock)
 {
     check_lock(&lock->debug);
-    return lock->tickets.head != lock->tickets.tail;
+    return lock->recurse_cpu == SPINLOCK_NO_CPU
+           ? lock->tickets.head != lock->tickets.tail
+           : lock->recurse_cpu == smp_processor_id();
 }
 
 int _spin_trylock(spinlock_t *lock)

spinlock: improve spin_is_locked() for recursive locks

Recursive locks know their current owner, and since we use the function solely to determine whether a particular lock is being held by the current CPU (which so far has been an imprecise check), make actually check the owner for recusrively acquired locks.

xen/common/spinlock.c +3 -1
@@ -188,7 +188,9 @@ void _spin_unlock_irqrestore(spinlock_t
 int _spin_is_locked(spinlock_t *lock)
 {
     check_lock(&lock->debug);
-    return lock->tickets.head != lock->tickets.tail;
+    return lock->recurse_cpu == SPINLOCK_NO_CPU
+           ? lock->tickets.head != lock->tickets.tail
+           : lock->recurse_cpu == smp_processor_id();
 }
 
 int _spin_trylock(spinlock_t *lock)

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

Reviewed-by on v1 1/1
#1 lore raw
Quoted 8 lines · Jan Beulich

Recursive locks know their current owner, and since we use the function
solely to determine whether a particular lock is being held by the current CPU (which so far has been an imprecise check), make actually check the owner for recusrively acquired locks.

Regards,
Dario

Signature · 4 lines
<<This happens because I choose it to happen!>> (Raistlin Majere)
-----------------------------------------------------------------
Dario Faggioli, Ph.D, http://about.me/dario.faggioli
Senior Software Engineer, Citrix Systems R&D Ltd., Cambridge (UK)

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

Reviewed-by on v1 1/1
#2 lore raw
In reply to Jan Beulich
Quoted 6 lines · Jan Beulich

Recursive locks know their current owner, and since we use the function solely to determine whether a particular lock is being held by the current CPU (which so far has been an imprecise check), make actually check the owner for recusrively acquired locks.

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

#3 lore raw
In reply to Jan Beulich
Quoted 4 lines · Jan Beulich

Recursive locks know their current owner, and since we use the function solely to determine whether a particular lock is being held by the current CPU (which so far has been an imprecise check), make actually check the owner for recusrively acquired locks.

What's the expected behaviour of _spin_is_locked() if the lock is held by another CPU?

Before it may return true if it is held by another CPU, now it will always return false in this case.

David

Quoted 18 lines
xen/common/spinlock.c +3 -1
@@ -188,7 +188,9 @@ void _spin_unlock_irqrestore(spinlock_t
 int _spin_is_locked(spinlock_t *lock)
 {
     check_lock(&lock->debug);
-    return lock->tickets.head != lock->tickets.tail;
+    return lock->recurse_cpu == SPINLOCK_NO_CPU
+           ? lock->tickets.head != lock->tickets.tail
+           : lock->recurse_cpu == smp_processor_id();
 }
 
 int _spin_trylock(spinlock_t *lock)
Quoted 5 lines

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

#4 lore raw
Quoted 7 lines
Quoted 1 line
Quoted 1 line · On 24.03.16 at 16:55, <[email protected]> wrote:
Quoted 4 lines · Jan Beulich

Recursive locks know their current owner, and since we use the function solely to determine whether a particular lock is being held by the current CPU (which so far has been an imprecise check), make actually check the owner for recusrively acquired locks.

Quoted 5 lines

What's the expected behaviour of _spin_is_locked() if the lock is held by another CPU?

Before it may return true if it is held by another CPU, now it will always return false in this case.

Correct - hence the reference to this only being used for a limited set of cases (read: ASSERT()s and alike).

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

#5 lore raw
Quoted 7 lines · Jan Beulich
Quoted 7 lines
Quoted 1 line
Quoted 1 line · On 24.03.16 at 16:55, <[email protected]> wrote:
Quoted 4 lines · Jan Beulich

Recursive locks know their current owner, and since we use the function solely to determine whether a particular lock is being held by the current CPU (which so far has been an imprecise check), make actually check the owner for recusrively acquired locks.

Quoted 8 lines
Quoted 5 lines

What's the expected behaviour of _spin_is_locked() if the lock is held by another CPU?

Before it may return true if it is held by another CPU, now it will always return false in this case.

Correct - hence the reference to this only being used for a limited set of cases (read: ASSERT()s and alike).

A bunch of the mm locks add "_by_me" at the end of the function. Did spin_is_locked() used to have that as well?

In any case I suppose "spin_is_locked_by_someone()" is really pretty useless information.

-George

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

#6 lore raw
Quoted 12 lines
Quoted 1 line
Quoted 1 line · On 29.03.16 at 12:36, <[email protected]> wrote:
Quoted 10 lines · Jan Beulich
Quoted 10 lines
Quoted 1 line
Quoted 1 line · On 24.03.16 at 16:55, <[email protected]> wrote:
Quoted 4 lines · Jan Beulich

Recursive locks know their current owner, and since we use the function solely to determine whether a particular lock is being held by the current CPU (which so far has been an imprecise check), make actually check the owner for recusrively acquired locks.

What's the expected behaviour of _spin_is_locked() if the lock is held by another CPU?

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

Before it may return true if it is held by another CPU, now it will always return false in this case.

Correct - hence the reference to this only being used for a limited set of cases (read: ASSERT()s and alike).

A bunch of the mm locks add "_by_me" at the end of the function. Did spin_is_locked() used to have that as well?

I don't think it did. And I think the only other readily visible use case of spin_is_locked() (testing whether a spin lock could be acquired without delay) is bogus anyway, as it would better be performed by doing a try-lock.

Quoted 2 lines

In any case I suppose "spin_is_locked_by_someone()" is really pretty useless information.

Indeed, hence I didn't want to alter the functions' names.

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

Reviewed-by on v1 1/1
#7 lore raw
In reply to Jan Beulich
Quoted 12 lines · On March 24, 2016 7:31pm, <[email protected]> wrote:

Recursive locks know their current owner, and since we use the function solely to determine whether a particular lock is being held by the current CPU (which so far has been an imprecise check), make actually check the owner for recusrively acquired locks.

xen/common/spinlock.c void _spin_unlock_irqrestore(spinlock_t · line 196

Quoted 10 lines
xen/common/spinlock.c +3 -1
@@ -188,7 +188,9 @@ void _spin_unlock_irqrestore(spinlock_t
 int _spin_is_locked(spinlock_t *lock)
 {
     check_lock(&lock->debug);
-    return lock->tickets.head != lock->tickets.tail;
+    return lock->recurse_cpu == SPINLOCK_NO_CPU
+           ? lock->tickets.head != lock->tickets.tail
+           : lock->recurse_cpu == smp_processor_id();
 }
 
 int _spin_trylock(spinlock_t *lock)

Thanks for your enhancement. I am not aware of this case in my previous patch set. Quan

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel