Skip to contentSkip to conversation

xen/locks: fix preempt disabling in lock handling

PATCH Superseded Juergen Gross 3 participants 15 messages Mar 13, 2020

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover xen/locks: fix preempt disabling in lock handling

Xen's rwlocks don't disable preemption at all, while spinlocks are doing it only after obtaining the lock.

While not really critical, it is wrong.

This series fixes that.

Juergen Gross (2):
xen/rwlocks: call preempt_disable() when taking a rwlock xen/spinlocks: fix placement of preempt_[dis|en]able()

 xen/common/spinlock.c    |  9 ++++++---
 xen/include/xen/rwlock.h | 18 +++++++++++++++++-
 2 files changed, 23 insertions(+), 4 deletions(-)
Signature · 7 lines
2.16.4


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#1 lore raw

PATCH 1/2 xen/rwlocks: call preempt_disable() when taking a rwlock

Similar to spinlocks preemption should be disabled while holding a rwlock.

---
 xen/include/xen/rwlock.h | 18 +++++++++++++++++-
 1 file changed, 17 insertions(+), 1 deletion(-)
xen/include/xen/rwlock.h +17 -1
index 1c221dd0d9..4ee341a182 100644
@@ -2,6 +2,7 @@
 #define __RWLOCK_H__
 
 #include <xen/percpu.h>
+#include <xen/preempt.h>
 #include <xen/smp.h>
 #include <xen/spinlock.h>
 
@@ -57,10 +58,12 @@ static inline int _read_trylock(rwlock_t *lock)
     cnts = atomic_read(&lock->cnts);
     if ( likely(_can_read_lock(cnts)) )
     {
+        preempt_disable();
         cnts = (u32)atomic_add_return(_QR_BIAS, &lock->cnts);
         if ( likely(_can_read_lock(cnts)) )
             return 1;
         atomic_sub(_QR_BIAS, &lock->cnts);
+        preempt_enable();
     }
     return 0;
 }
@@ -73,6 +76,7 @@ static inline void _read_lock(rwlock_t *lock)
 {
     u32 cnts;
 
+    preempt_disable();
     cnts = atomic_add_return(_QR_BIAS, &lock->cnts);
     if ( likely(_can_read_lock(cnts)) )
         return;
@@ -106,6 +110,7 @@ static inline void _read_unlock(rwlock_t *lock)
      * Atomically decrement the reader count
      */
     atomic_sub(_QR_BIAS, &lock->cnts);
+    preempt_enable();
 }
 
 static inline void _read_unlock_irq(rwlock_t *lock)
@@ -137,6 +142,7 @@ static inline unsigned int _write_lock_val(void)
 static inline void _write_lock(rwlock_t *lock)
 {
     /* Optimize for the unfair lock case where the fair flag is 0. */
+    preempt_disable();
     if ( atomic_cmpxchg(&lock->cnts, 0, _write_lock_val()) == 0 )
         return;
 
@@ -172,13 +178,21 @@ static inline int _write_trylock(rwlock_t *lock)
     if ( unlikely(cnts) )
         return 0;
 
-    return likely(atomic_cmpxchg(&lock->cnts, 0, _write_lock_val()) == 0);
+    preempt_disable();
+    if ( unlikely(atomic_cmpxchg(&lock->cnts, 0, _write_lock_val()) != 0) )
+    {
+        preempt_enable();
+        return 0;
+    }
+
+    return 1;
 }
 
 static inline void _write_unlock(rwlock_t *lock)
 {
     ASSERT(_is_write_locked_by_me(atomic_read(&lock->cnts)));
     atomic_and(~(_QW_CPUMASK | _QW_WMASK), &lock->cnts);
+    preempt_enable();
 }
 
 static inline void _write_unlock_irq(rwlock_t *lock)
@@ -274,6 +288,7 @@ static inline void _percpu_read_lock(percpu_rwlock_t **per_cpudata,
     }
 
     /* Indicate this cpu is reading. */
+    preempt_disable();
     this_cpu_ptr(per_cpudata) = percpu_rwlock;
     smp_mb();
     /* Check if a writer is waiting. */
@@ -308,6 +323,7 @@ static inline void _percpu_read_unlock(percpu_rwlock_t **per_cpudata,
         return;
     }
     this_cpu_ptr(per_cpudata) = NULL;
+    preempt_enable();
     smp_wmb();
 }
 
Signature · 7 lines
2.16.4


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#2 lore raw

Re: [Xen-devel] [PATCH 1/2] xen/rwlocks: call preempt_disable() when taking a rwlock

Quoted 4 lines · Juergen Gross

Similar to spinlocks preemption should be disabled while holding a rwlock.

Just one note/question:

xen/include/xen/rwlock.h static inline void _percpu_read_unlock(percpu_rwlock_t **per_cpudata, · line 328

Quoted 7 lines
xen/include/xen/rwlock.h +1 -0
@@ -308,6 +323,7 @@ static inline void _percpu_read_unlock(percpu_rwlock_t **per_cpudata,
         return;
     }
     this_cpu_ptr(per_cpudata) = NULL;
+    preempt_enable();
     smp_wmb();
 }

It would seem more logical to me to insert this after the smp_wmb(). Thoughts? I'll be happy to give my R-b once we've settled on this.

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#3 lore raw

Re: [Xen-devel] [PATCH 1/2] xen/rwlocks: call preempt_disable() when taking a rwlock

Quoted 8 lines · Jan Beulich
Quoted 4 lines · Juergen Gross

Similar to spinlocks preemption should be disabled while holding a rwlock.

Just one note/question:

Quoted 9 lines

xen/include/xen/rwlock.h static inline void _percpu_read_unlock(percpu_rwlock_t **per_cpudata, · line 328

Quoted 7 lines
xen/include/xen/rwlock.h +1 -0
@@ -308,6 +323,7 @@ static inline void _percpu_read_unlock(percpu_rwlock_t **per_cpudata,
          return;
      }
      this_cpu_ptr(per_cpudata) = NULL;
+    preempt_enable();
      smp_wmb();
  }

It would seem more logical to me to insert this after the smp_wmb().

+1

Quoted 4 lines

Thoughts? I'll be happy to give my R-b once we've settled on this.

Jan

Signature · 6 lines
Julien Grall

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#4 lore raw

Re: [Xen-devel] [PATCH 1/2] xen/rwlocks: call preempt_disable() when taking a rwlock

In reply to Juergen Gross

Hi Juergen,

Quoted 21 lines · Juergen Gross

Similar to spinlocks preemption should be disabled while holding a rwlock.

---
  xen/include/xen/rwlock.h | 18 +++++++++++++++++-
  1 file changed, 17 insertions(+), 1 deletion(-)
xen/include/xen/rwlock.h +1 -0
index 1c221dd0d9..4ee341a182 100644
@@ -2,6 +2,7 @@
  #define __RWLOCK_H__
 
  #include <xen/percpu.h>
+#include <xen/preempt.h>
  #include <xen/smp.h>
  #include <xen/spinlock.h>
 

xen/include/xen/rwlock.h static inline int _read_trylock(rwlock_t *lock) · line 60

Quoted 3 lines
xen/include/xen/rwlock.h +0 -0
@@ -57,10 +58,12 @@ static inline int _read_trylock(rwlock_t *lock)
      cnts = atomic_read(&lock->cnts);
      if ( likely(_can_read_lock(cnts)) )
      {

If you get preempted here, then it means the check below is likely going to fail. So I think it would be best to disable preemption before, to give more chance to succeed.

Quoted 34 lines
xen/include/xen/rwlock.h +5 -0
+        preempt_disable();
          cnts = (u32)atomic_add_return(_QR_BIAS, &lock->cnts);
          if ( likely(_can_read_lock(cnts)) )
              return 1;
          atomic_sub(_QR_BIAS, &lock->cnts);
+        preempt_enable();
      }
      return 0;
  }
@@ -73,6 +76,7 @@ static inline void _read_lock(rwlock_t *lock)
  {
      u32 cnts;
 
+    preempt_disable();
      cnts = atomic_add_return(_QR_BIAS, &lock->cnts);
      if ( likely(_can_read_lock(cnts)) )
          return;
@@ -106,6 +110,7 @@ static inline void _read_unlock(rwlock_t *lock)
       * Atomically decrement the reader count
       */
      atomic_sub(_QR_BIAS, &lock->cnts);
+    preempt_enable();
  }
 
  static inline void _read_unlock_irq(rwlock_t *lock)
@@ -137,6 +142,7 @@ static inline unsigned int _write_lock_val(void)
  static inline void _write_lock(rwlock_t *lock)
  {
      /* Optimize for the unfair lock case where the fair flag is 0. */
+    preempt_disable();
      if ( atomic_cmpxchg(&lock->cnts, 0, _write_lock_val()) == 0 )
          return;
 

xen/include/xen/rwlock.h static inline int _write_trylock(rwlock_t *lock) · line 181

Quoted 5 lines
xen/include/xen/rwlock.h +1 -1
@@ -172,13 +178,21 @@ static inline int _write_trylock(rwlock_t *lock)
      if ( unlikely(cnts) )
          return 0;
 
-    return likely(atomic_cmpxchg(&lock->cnts, 0, _write_lock_val()) == 0);
+    preempt_disable();

Similar remark as the read_trylock().

Quoted 27 lines
xen/include/xen/rwlock.h +9 -0
+    if ( unlikely(atomic_cmpxchg(&lock->cnts, 0, _write_lock_val()) != 0) )
+    {
+        preempt_enable();
+        return 0;
+    }
+
+    return 1;
  }
 
  static inline void _write_unlock(rwlock_t *lock)
  {
      ASSERT(_is_write_locked_by_me(atomic_read(&lock->cnts)));
      atomic_and(~(_QW_CPUMASK | _QW_WMASK), &lock->cnts);
+    preempt_enable();
  }
 
  static inline void _write_unlock_irq(rwlock_t *lock)
@@ -274,6 +288,7 @@ static inline void _percpu_read_lock(percpu_rwlock_t **per_cpudata,
      }
 
      /* Indicate this cpu is reading. */
+    preempt_disable();
      this_cpu_ptr(per_cpudata) = percpu_rwlock;
      smp_mb();
      /* Check if a writer is waiting. */

xen/include/xen/rwlock.h static inline void _percpu_read_unlock(percpu_rwlock_t **per_cpudata, · line 329

Quoted 7 lines
xen/include/xen/rwlock.h +1 -0
@@ -308,6 +323,7 @@ static inline void _percpu_read_unlock(percpu_rwlock_t **per_cpudata,
          return;
      }
      this_cpu_ptr(per_cpudata) = NULL;
+    preempt_enable();
      smp_wmb();
  }
 

Cheers,

Signature · 6 lines
Julien Grall

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#5 lore raw

Re: [Xen-devel] [PATCH 1/2] xen/rwlocks: call preempt_disable() when taking a rwlock

Quoted 11 lines · Julien Grall

Hi Juergen,

Quoted 8 lines · Juergen Gross

Similar to spinlocks preemption should be disabled while holding a rwlock.

---
  xen/include/xen/rwlock.h | 18 +++++++++++++++++-

  1 file changed, 17 insertions(+), 1 deletion(-)
Quoted 16 lines
Quoted 11 lines
xen/include/xen/rwlock.h +1 -0
index 1c221dd0d9..4ee341a182 100644
@@ -2,6 +2,7 @@
   #define __RWLOCK_H__
   #include <xen/percpu.h>
+#include <xen/preempt.h>
   #include <xen/smp.h>
   #include <xen/spinlock.h>

xen/include/xen/rwlock.h static inline int _read_trylock(rwlock_t *lock) · line 60

Quoted 3 lines
xen/include/xen/rwlock.h +0 -0
@@ -57,10 +58,12 @@ static inline int _read_trylock(rwlock_t *lock)
       cnts = atomic_read(&lock->cnts);
       if ( likely(_can_read_lock(cnts)) )
       {

If you get preempted here, then it means the check below is likely going to fail. So I think it would be best to disable preemption before, to give more chance to succeed.

As preemption probability at this very point should be much lower than that of held locks I think that is optimizing the wrong path. I'm not opposed doing the modification you are requesting, but would like to hear a second opinion on that topic, especially as I'd need to add another preempt_enable() call when following your advice.

Juergen

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#6 lore raw

Re: [Xen-devel] [PATCH 1/2] xen/rwlocks: call preempt_disable() when taking a rwlock

Quoted 31 lines · Jürgen Groß
Quoted 21 lines · Julien Grall

Hi Juergen,

Quoted 18 lines · Juergen Gross

Similar to spinlocks preemption should be disabled while holding a rwlock.

---
  xen/include/xen/rwlock.h | 18 +++++++++++++++++-

  1 file changed, 17 insertions(+), 1 deletion(-)
xen/include/xen/rwlock.h +1 -0
index 1c221dd0d9..4ee341a182 100644
@@ -2,6 +2,7 @@
   #define __RWLOCK_H__
   #include <xen/percpu.h>
+#include <xen/preempt.h>
   #include <xen/smp.h>
   #include <xen/spinlock.h>
Quoted 8 lines

xen/include/xen/rwlock.h static inline int _read_trylock(rwlock_t *lock) · line 60

Quoted 4 lines
xen/include/xen/rwlock.h +0 -0
@@ -57,10 +58,12 @@ static inline int _read_trylock(rwlock_t *lock)
       cnts = atomic_read(&lock->cnts);
       if ( likely(_can_read_lock(cnts)) )
       {

If you get preempted here, then it means the check below is likely going to fail. So I think it would be best to disable preemption before, to give more chance to succeed.

Quoted 5 lines

As preemption probability at this very point should be much lower than that of held locks I think that is optimizing the wrong path. I'm not opposed doing the modification you are requesting, but would like to hear a second opinion on that topic, especially as I'd need to add another preempt_enable() call when following your advice.

I can see arguments for both placements, and hence I'm fine either way.

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#7 lore raw

Re: [Xen-devel] [PATCH 1/2] xen/rwlocks: call preempt_disable() when taking a rwlock

In reply to Jürgen Groß

Hi Juergen,

Quoted 29 lines · Jürgen Groß
Quoted 11 lines · Julien Grall

Hi Juergen,

Quoted 8 lines · Juergen Gross

Similar to spinlocks preemption should be disabled while holding a rwlock.

---
  xen/include/xen/rwlock.h | 18 +++++++++++++++++-

  1 file changed, 17 insertions(+), 1 deletion(-)
Quoted 16 lines
Quoted 11 lines
xen/include/xen/rwlock.h +1 -0
index 1c221dd0d9..4ee341a182 100644
@@ -2,6 +2,7 @@
   #define __RWLOCK_H__
   #include <xen/percpu.h>
+#include <xen/preempt.h>
   #include <xen/smp.h>
   #include <xen/spinlock.h>

xen/include/xen/rwlock.h static inline int _read_trylock(rwlock_t *lock) · line 60

Quoted 3 lines
xen/include/xen/rwlock.h +0 -0
@@ -57,10 +58,12 @@ static inline int _read_trylock(rwlock_t *lock)
       cnts = atomic_read(&lock->cnts);
       if ( likely(_can_read_lock(cnts)) )
       {

If you get preempted here, then it means the check below is likely going to fail. So I think it would be best to disable preemption before, to give more chance to succeed.

Quoted 2 lines

As preemption probability at this very point should be much lower than that of held locks I think that is optimizing the wrong path.

Why so? Lock contention should be fairly limited or you already have a problem on your system. So preemption is more likely.

Quoted 4 lines

I'm not
opposed doing the modification you are requesting, but would like to hear a second opinion on that topic, especially as I'd need to add another preempt_enable() call when following your advice.

I don't really see the problem with adding a new preemption_enable() call. But the code can also be reworked to have only one call...

Cheers,

Signature · 6 lines
Julien Grall

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#8 lore raw

Re: [Xen-devel] [PATCH 1/2] xen/rwlocks: call preempt_disable() when taking a rwlock

Quoted 32 lines · Julien Grall

Hi Juergen,

Quoted 29 lines · Jürgen Groß
Quoted 27 lines · Julien Grall

Hi Juergen,

Quoted 19 lines · Juergen Gross

Similar to spinlocks preemption should be disabled while holding a rwlock.

---
  xen/include/xen/rwlock.h | 18 +++++++++++++++++-

  1 file changed, 17 insertions(+), 1 deletion(-)
xen/include/xen/rwlock.h +1 -0
index 1c221dd0d9..4ee341a182 100644
@@ -2,6 +2,7 @@
   #define __RWLOCK_H__
   #include <xen/percpu.h>
+#include <xen/preempt.h>
   #include <xen/smp.h>
   #include <xen/spinlock.h>

xen/include/xen/rwlock.h static inline int _read_trylock(rwlock_t *lock) · line 60

Quoted 3 lines
xen/include/xen/rwlock.h +0 -0
@@ -57,10 +58,12 @@ static inline int _read_trylock(rwlock_t *lock)
       cnts = atomic_read(&lock->cnts);
       if ( likely(_can_read_lock(cnts)) )
       {

If you get preempted here, then it means the check below is likely going to fail. So I think it would be best to disable preemption before, to give more chance to succeed.

Quoted 4 lines
Quoted 2 lines

As preemption probability at this very point should be much lower than that of held locks I think that is optimizing the wrong path.

Why so? Lock contention should be fairly limited or you already have a problem on your system. So preemption is more likely.

Today probability of preemption is 0.

Even with preemption added the probability to be preempted in a sequence of about a dozen instructions is _very_ low.

Quoted 7 lines
Quoted 5 lines

I'm not
opposed doing the modification you are requesting, but would like to hear a second opinion on that topic, especially as I'd need to add another preempt_enable() call when following your advice.

I don't really see the problem with adding a new preemption_enable() call. But the code can also be reworked to have only one call...

Its the dynamical path I'm speaking of. Accessing a local cpu variable is not that cheap, and in case we add preemption in the future preempt_enable() will become even more expensive.

Juergen

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#9 lore raw

Re: [Xen-devel] [PATCH 1/2] xen/rwlocks: call preempt_disable() when taking a rwlock

Hi Juergen,

Quoted 33 lines · Jürgen Groß
Quoted 32 lines · Julien Grall

Hi Juergen,

Quoted 29 lines · Jürgen Groß
Quoted 27 lines · Julien Grall

Hi Juergen,

Quoted 19 lines · Juergen Gross

Similar to spinlocks preemption should be disabled while holding a rwlock.

---
  xen/include/xen/rwlock.h | 18 +++++++++++++++++-

  1 file changed, 17 insertions(+), 1 deletion(-)
xen/include/xen/rwlock.h +1 -0
index 1c221dd0d9..4ee341a182 100644
@@ -2,6 +2,7 @@
   #define __RWLOCK_H__
   #include <xen/percpu.h>
+#include <xen/preempt.h>
   #include <xen/smp.h>
   #include <xen/spinlock.h>

xen/include/xen/rwlock.h static inline int _read_trylock(rwlock_t *lock) · line 60

Quoted 3 lines
xen/include/xen/rwlock.h +0 -0
@@ -57,10 +58,12 @@ static inline int _read_trylock(rwlock_t *lock)
       cnts = atomic_read(&lock->cnts);
       if ( likely(_can_read_lock(cnts)) )
       {

If you get preempted here, then it means the check below is likely going to fail. So I think it would be best to disable preemption before, to give more chance to succeed.

Quoted 6 lines
Quoted 4 lines
Quoted 2 lines

As preemption probability at this very point should be much lower than that of held locks I think that is optimizing the wrong path.

Why so? Lock contention should be fairly limited or you already have a problem on your system. So preemption is more likely.

Today probability of preemption is 0.

I am aware of that...

Quoted 3 lines

Even with preemption added the probability to be preempted in a sequence of about a dozen instructions is _very_ low.

... but I am not convinced of the low probability here.

Quoted 7 lines
Quoted 7 lines
Quoted 6 lines

I'm not
opposed doing the modification you are requesting, but would like to hear a second opinion on that topic, especially as I'd need to add another preempt_enable() call when following your advice.

Quoted 5 lines
Quoted 1 line

I don't really see the problem with adding a new preemption_enable() call. But the code can also be reworked to have only one call...

Its the dynamical path I'm speaking of. Accessing a local cpu variable is not that cheap, and in case we add preemption in the future preempt_enable() will become even more expensive.

Do you realize that the lock is most likely be uncontented? And if it were, the caller would likely not spin in a tight loop, otherwise it would have used read_lock().

So until you proved me otherwise (with numbers), this is micro-optimization that is not going to be seen in a workload.

Cheers,

Signature · 6 lines
Julien Grall

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#10 lore raw

Re: [Xen-devel] [PATCH 1/2] xen/rwlocks: call preempt_disable() when taking a rwlock

Quoted 60 lines · Julien Grall

Hi Juergen,

Quoted 33 lines · Jürgen Groß
Quoted 32 lines · Julien Grall

Hi Juergen,

Quoted 29 lines · Jürgen Groß
Quoted 27 lines · Julien Grall

Hi Juergen,

Quoted 19 lines · Juergen Gross

Similar to spinlocks preemption should be disabled while holding a rwlock.

---
  xen/include/xen/rwlock.h | 18 +++++++++++++++++-

  1 file changed, 17 insertions(+), 1 deletion(-)
xen/include/xen/rwlock.h +1 -0
index 1c221dd0d9..4ee341a182 100644
@@ -2,6 +2,7 @@
   #define __RWLOCK_H__
   #include <xen/percpu.h>
+#include <xen/preempt.h>
   #include <xen/smp.h>
   #include <xen/spinlock.h>

xen/include/xen/rwlock.h static inline int _read_trylock(rwlock_t *lock) · line 60

Quoted 3 lines
xen/include/xen/rwlock.h +0 -0
@@ -57,10 +58,12 @@ static inline int _read_trylock(rwlock_t *lock)
       cnts = atomic_read(&lock->cnts);
       if ( likely(_can_read_lock(cnts)) )
       {

If you get preempted here, then it means the check below is likely going to fail. So I think it would be best to disable preemption before, to give more chance to succeed.

Quoted 6 lines
Quoted 4 lines
Quoted 2 lines

As preemption probability at this very point should be much lower than that of held locks I think that is optimizing the wrong path.

Why so? Lock contention should be fairly limited or you already have a problem on your system. So preemption is more likely.

Today probability of preemption is 0.

I am aware of that...

Quoted 3 lines

Even with preemption added the probability to be preempted in a sequence of about a dozen instructions is _very_ low.

... but I am not convinced of the low probability here.

Quoted 9 lines
Quoted 8 lines
Quoted 6 lines

I'm not
opposed doing the modification you are requesting, but would like to hear a second opinion on that topic, especially as I'd need to add another preempt_enable() call when following your advice.

I don't really see the problem with adding a new preemption_enable() call. But the code can also be reworked to have only one call...

Quoted 7 lines
Quoted 3 lines

Its the dynamical path I'm speaking of. Accessing a local cpu variable is not that cheap, and in case we add preemption in the future preempt_enable() will become even more expensive.

Do you realize that the lock is most likely be uncontented? And if it were, the caller would likely not spin in a tight loop, otherwise it would have used read_lock().

So until you proved me otherwise (with numbers), this is micro-optimization that is not going to be seen in a workload.

Fine, in case you feeling so strong about that, I'll change it.

Juergen

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#11 lore raw

PATCH 2/2 xen/spinlocks: fix placement of preempt_[dis|en]able()

In reply to Juergen Gross

In case Xen ever gains preemption support the spinlock coding's placement of preempt_disable() and preempt_enable() should be outside of the locked section.

---
 xen/common/spinlock.c | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)
xen/common/spinlock.c +6 -3
index 344981c54a..f05fb068cd 100644
@@ -160,6 +160,7 @@ void inline _spin_lock_cb(spinlock_t *lock, void (*cb)(void *), void *data)
     LOCK_PROFILE_VAR;
 
     check_lock(&lock->debug);
+    preempt_disable();
     tickets.head_tail = arch_fetch_and_add(&lock->tickets.head_tail,
                                            tickets.head_tail);
     while ( tickets.tail != observe_head(&lock->tickets) )
@@ -171,7 +172,6 @@ void inline _spin_lock_cb(spinlock_t *lock, void (*cb)(void *), void *data)
     }
     got_lock(&lock->debug);
     LOCK_PROFILE_GOT;
-    preempt_disable();
     arch_lock_acquire_barrier();
 }
 
@@ -199,10 +199,10 @@ unsigned long _spin_lock_irqsave(spinlock_t *lock)
 void _spin_unlock(spinlock_t *lock)
 {
     arch_lock_release_barrier();
-    preempt_enable();
     LOCK_PROFILE_REL;
     rel_lock(&lock->debug);
     add_sized(&lock->tickets.head, 1);
+    preempt_enable();
     arch_lock_signal();
 }
 
@@ -242,15 +242,18 @@ int _spin_trylock(spinlock_t *lock)
         return 0;
     new = old;
     new.tail++;
+    preempt_disable();
     if ( cmpxchg(&lock->tickets.head_tail,
                  old.head_tail, new.head_tail) != old.head_tail )
+    {
+        preempt_enable();
         return 0;
+    }
     got_lock(&lock->debug);
 #ifdef CONFIG_DEBUG_LOCK_PROFILE
     if (lock->profile)
         lock->profile->time_locked = NOW();
 #endif
-    preempt_disable();
     /*
      * cmpxchg() is a full barrier so no need for an
      * arch_lock_acquire_barrier().
Signature · 7 lines
2.16.4


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#12 lore raw

Re: [Xen-devel] [PATCH 2/2] xen/spinlocks: fix placement of preempt_[dis|en]able()

xen/common/spinlock.c unsigned long _spin_lock_irqsave(spinlock_t *lock) · line 207

Quoted 11 lines · Juergen Gross
xen/common/spinlock.c +1 -1
@@ -199,10 +199,10 @@ unsigned long _spin_lock_irqsave(spinlock_t *lock)
 void _spin_unlock(spinlock_t *lock)
 {
     arch_lock_release_barrier();
-    preempt_enable();
     LOCK_PROFILE_REL;
     rel_lock(&lock->debug);
     add_sized(&lock->tickets.head, 1);
+    preempt_enable();
     arch_lock_signal();
 }

arch_lock_signal() is a barrier on Arm, hence just like for patch 1 I wonder whether the insertion wouldn't better come after it.

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#13 lore raw

Re: [Xen-devel] [PATCH 2/2] xen/spinlocks: fix placement of preempt_[dis|en]able()

Quoted 15 lines · Jan Beulich

xen/common/spinlock.c unsigned long _spin_lock_irqsave(spinlock_t *lock) · line 207

Quoted 11 lines · Juergen Gross
xen/common/spinlock.c +1 -1
@@ -199,10 +199,10 @@ unsigned long _spin_lock_irqsave(spinlock_t *lock)
  void _spin_unlock(spinlock_t *lock)
  {
      arch_lock_release_barrier();
-    preempt_enable();
      LOCK_PROFILE_REL;
      rel_lock(&lock->debug);
      add_sized(&lock->tickets.head, 1);
+    preempt_enable();
      arch_lock_signal();
  }

arch_lock_signal() is a barrier on Arm, hence just like for patch 1 I wonder whether the insertion wouldn't better come after it.

Either way is fine for me. It should be noted that preemption is only relevant on the local cpu. So this is about trading lock state visibility against preemption disabled time, and I agree the visible time of the lock held should be minimized at higher priority than the preemption disabled time.

I'll modify my patches accordingly, adding a note in this regard to the commit message.

Juergen

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#14 lore raw

Re: [Xen-devel] [PATCH 2/2] xen/spinlocks: fix placement of preempt_[dis|en]able()

Hi,

Quoted 16 lines · Jürgen Groß
Quoted 15 lines · Jan Beulich

xen/common/spinlock.c unsigned long _spin_lock_irqsave(spinlock_t *lock) · line 207

Quoted 11 lines · Juergen Gross
xen/common/spinlock.c +1 -1
@@ -199,10 +199,10 @@ unsigned long _spin_lock_irqsave(spinlock_t *lock)
   void _spin_unlock(spinlock_t *lock)
   {
       arch_lock_release_barrier();
-    preempt_enable();
       LOCK_PROFILE_REL;
       rel_lock(&lock->debug);
       add_sized(&lock->tickets.head, 1);
+    preempt_enable();
       arch_lock_signal();
   }

arch_lock_signal() is a barrier on Arm, hence just like for patch 1 I wonder whether the insertion wouldn't better come after it.

The important barrier in spin_unlock() is arch_lock_release_barrier().

The one in arch_lock_signal() is just to ensure that waking up the other CPUs will not happen before the unlock is seen. The barrier would not have been necessary if the we didn't use 'sev'.

Quoted 6 lines

Either way is fine for me. It should be noted that preemption is only relevant on the local cpu. So this is about trading lock state visibility against preemption disabled time, and I agree the visible time of the lock held should be minimized at higher priority than the preemption disabled time.

I don't think the rationale is about "performance" here. The rationale is you don't know the implementation of arch_lock_signal(). If you get preempted by a thread trying to acquire the same lock, then it may not do the right thing.

Linux will also re-enable preemption only after the unlock has been completed. So it would be best to follow the same pattern.

Cheers,

Signature · 6 lines
Julien Grall

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel