Skip to contentSkip to conversation

XSA-114 follow-ups

PATCH In review Jan Beulich 4 participants 10 messages Jan 8, 2015 – Jan 9, 2015

lore
Share & tools
lore
#0 lore raw

cover XSA-114 follow-ups

1: spinlock: use local_irq_disable() instead of local_irq_save() where possible 2: rwlock: allow arch to override read_unlock() atomic
3: rwlock: allow arch to override write_unlock() atomic

#1 lore raw

PATCH 1/3 spinlock: use local_irq_disable() instead of local_irq_save() where possible

... as generally being a cheaper operation.

xen/common/spinlock.c +3 -3
@@ -162,7 +162,7 @@ unsigned long _spin_lock_irqsave(spinloc
         local_irq_restore(flags);
         while ( likely(_raw_spin_is_locked(&lock->raw)) )
             cpu_relax();
-        local_irq_save(flags);
+        local_irq_disable();
     }
     LOCK_PROFILE_GOT;
     preempt_disable();
@@ -313,7 +313,7 @@ unsigned long _read_lock_irqsave(rwlock_
             local_irq_restore(flags);
             while ( (x = lock->lock) & RW_WRITE_FLAG )
                 cpu_relax();
-            local_irq_save(flags);
+            local_irq_disable();
         }
     } while ( cmpxchg(&lock->lock, x, x+1) != x );
     preempt_disable();
@@ -409,7 +409,7 @@ unsigned long _write_lock_irqsave(rwlock
             local_irq_restore(flags);
             while ( (x = lock->lock) & RW_WRITE_FLAG )
                 cpu_relax();
-            local_irq_save(flags);
+            local_irq_disable();
         }
     } while ( cmpxchg(&lock->lock, x, x|RW_WRITE_FLAG) != x );
     while ( x != 0 )

spinlock: use local_irq_disable() instead of local_irq_save() where possible

... as generally being a cheaper operation.

xen/common/spinlock.c +3 -3
@@ -162,7 +162,7 @@ unsigned long _spin_lock_irqsave(spinloc
         local_irq_restore(flags);
         while ( likely(_raw_spin_is_locked(&lock->raw)) )
             cpu_relax();
-        local_irq_save(flags);
+        local_irq_disable();
     }
     LOCK_PROFILE_GOT;
     preempt_disable();
@@ -313,7 +313,7 @@ unsigned long _read_lock_irqsave(rwlock_
             local_irq_restore(flags);
             while ( (x = lock->lock) & RW_WRITE_FLAG )
                 cpu_relax();
-            local_irq_save(flags);
+            local_irq_disable();
         }
     } while ( cmpxchg(&lock->lock, x, x+1) != x );
     preempt_disable();
@@ -409,7 +409,7 @@ unsigned long _write_lock_irqsave(rwlock
             local_irq_restore(flags);
             while ( (x = lock->lock) & RW_WRITE_FLAG )
                 cpu_relax();
-            local_irq_save(flags);
+            local_irq_disable();
         }
     } while ( cmpxchg(&lock->lock, x, x|RW_WRITE_FLAG) != x );
     while ( x != 0 )

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

#2 lore raw

Re: [PATCH 1/3] spinlock: use local_irq_disable() instead of local_irq_save() where possible

Quoted 1 line · Jan Beulich

... as generally being a cheaper operation.

I was wondering if it would be possible to change some of the EFLAGS after when we go in the 'cpu_relax' - and an interrupt happens, we process it, alter the EFLAGS, then when we are done, the EFLAGS are different - which the original code would save when it was done sitting on the cpu_relax() loop.

Actually that sounds bad - we only want to restore the flags that we had when going in this spin lock. Would make sense to add an ASSERT to check for flags being different from the EFLAGS?

Quoted 76 lines
xen/common/spinlock.c +3 -3
@@ -162,7 +162,7 @@ unsigned long _spin_lock_irqsave(spinloc
         local_irq_restore(flags);
         while ( likely(_raw_spin_is_locked(&lock->raw)) )
             cpu_relax();
-        local_irq_save(flags);
+        local_irq_disable();
     }
     LOCK_PROFILE_GOT;
     preempt_disable();
@@ -313,7 +313,7 @@ unsigned long _read_lock_irqsave(rwlock_
             local_irq_restore(flags);
             while ( (x = lock->lock) & RW_WRITE_FLAG )
                 cpu_relax();
-            local_irq_save(flags);
+            local_irq_disable();
         }
     } while ( cmpxchg(&lock->lock, x, x+1) != x );
     preempt_disable();
@@ -409,7 +409,7 @@ unsigned long _write_lock_irqsave(rwlock
             local_irq_restore(flags);
             while ( (x = lock->lock) & RW_WRITE_FLAG )
                 cpu_relax();
-            local_irq_save(flags);
+            local_irq_disable();
         }
     } while ( cmpxchg(&lock->lock, x, x|RW_WRITE_FLAG) != x );
     while ( x != 0 )

spinlock: use local_irq_disable() instead of local_irq_save() where possible

... as generally being a cheaper operation.

xen/common/spinlock.c +3 -3
@@ -162,7 +162,7 @@ unsigned long _spin_lock_irqsave(spinloc
         local_irq_restore(flags);
         while ( likely(_raw_spin_is_locked(&lock->raw)) )
             cpu_relax();
-        local_irq_save(flags);
+        local_irq_disable();
     }
     LOCK_PROFILE_GOT;
     preempt_disable();
@@ -313,7 +313,7 @@ unsigned long _read_lock_irqsave(rwlock_
             local_irq_restore(flags);
             while ( (x = lock->lock) & RW_WRITE_FLAG )
                 cpu_relax();
-            local_irq_save(flags);
+            local_irq_disable();
         }
     } while ( cmpxchg(&lock->lock, x, x+1) != x );
     preempt_disable();
@@ -409,7 +409,7 @@ unsigned long _write_lock_irqsave(rwlock
             local_irq_restore(flags);
             while ( (x = lock->lock) & RW_WRITE_FLAG )
                 cpu_relax();
-            local_irq_save(flags);
+            local_irq_disable();
         }
     } while ( cmpxchg(&lock->lock, x, x|RW_WRITE_FLAG) != x );
     while ( x != 0 )

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

#3 lore raw

Re: [PATCH 1/3] spinlock: use local_irq_disable() instead of local_irq_save() where possible

Quoted 10 lines
Quoted 1 line
Quoted 1 line · On 09.01.15 at 17:02, <[email protected]> wrote:
Quoted 1 line · Jan Beulich

... as generally being a cheaper operation.

I was wondering if it would be possible to change some of the EFLAGS after when we go in the 'cpu_relax' - and an interrupt happens, we process it, alter the EFLAGS, then when we are done, the EFLAGS are different - which the original code would save when it was done sitting on the cpu_relax() loop.

Quoted 4 lines

Actually that sounds bad - we only want to restore the flags that we had when going in this spin lock. Would make sense to add an ASSERT to check for flags being different from the EFLAGS?

I'm not sure I'm following what you try to tell me. For one, interrupts don't change EFLAGS - the IRET restores them. And second, local_irq_restore() is only guaranteed to restore EFLAGS.IF - whether it touches other flags if largely undefined.

Jan

Quoted 29 lines
Quoted 29 lines
xen/common/spinlock.c +3 -3
@@ -162,7 +162,7 @@ unsigned long _spin_lock_irqsave(spinloc
         local_irq_restore(flags);
         while ( likely(_raw_spin_is_locked(&lock->raw)) )
             cpu_relax();
-        local_irq_save(flags);
+        local_irq_disable();
     }
     LOCK_PROFILE_GOT;
     preempt_disable();
@@ -313,7 +313,7 @@ unsigned long _read_lock_irqsave(rwlock_
             local_irq_restore(flags);
             while ( (x = lock->lock) & RW_WRITE_FLAG )
                 cpu_relax();
-            local_irq_save(flags);
+            local_irq_disable();
         }
     } while ( cmpxchg(&lock->lock, x, x+1) != x );
     preempt_disable();
@@ -409,7 +409,7 @@ unsigned long _write_lock_irqsave(rwlock
             local_irq_restore(flags);
             while ( (x = lock->lock) & RW_WRITE_FLAG )
                 cpu_relax();
-            local_irq_save(flags);
+            local_irq_disable();
         }
     } while ( cmpxchg(&lock->lock, x, x|RW_WRITE_FLAG) != x );
     while ( x != 0 )
#4 lore raw

Re: [PATCH 1/3] spinlock: use local_irq_disable() instead of local_irq_save() where possible

In reply to Konrad Rzeszutek Wilk
Quoted 8 lines · Konrad Rzeszutek Wilk
Quoted 1 line · Jan Beulich

... as generally being a cheaper operation.

I was wondering if it would be possible to change some of the EFLAGS after when we go in the 'cpu_relax' - and an interrupt happens, we process it, alter the EFLAGS, then when we are done, the EFLAGS are different - which the original code would save when it was done sitting on the cpu_relax() loop.

Quoted 4 lines

Actually that sounds bad - we only want to restore the flags that we had when going in this spin lock. Would make sense to add an ASSERT to check for flags being different from the EFLAGS?

local_irq_restore() only restores the interrupt flag from flags. All other bits in EFLAGS are unmodified.

~Andrew

#5 lore raw

Re: [PATCH 1/3] spinlock: use local_irq_disable() instead of local_irq_save() where possible

Quoted 9 lines · Andrew Cooper
Quoted 8 lines · Konrad Rzeszutek Wilk
Quoted 1 line · Jan Beulich

... as generally being a cheaper operation.

I was wondering if it would be possible to change some of the EFLAGS after when we go in the 'cpu_relax' - and an interrupt happens, we process it, alter the EFLAGS, then when we are done, the EFLAGS are different - which the original code would save when it was done sitting on the cpu_relax() loop.

Quoted 7 lines
Quoted 4 lines

Actually that sounds bad - we only want to restore the flags that we had when going in this spin lock. Would make sense to add an ASSERT to check for flags being different from the EFLAGS?

local_irq_restore() only restores the interrupt flag from flags. All other bits in EFLAGS are unmodified.

which I would have found out if I read the code from local_irq_restore().

Sorry about the noise - should have looked at the code before asking questions!

Quoted 2 lines

~Andrew

#6 lore raw

PATCH 2/3 rwlock: allow arch to override read_unlock() atomic

In reply to Jan Beulich

On x86, LOCK DEC is cheaper than LOCK CMPXCHG and doesn't require a retry loop around it.

xen/common/spinlock.c +10 -6
@@ -333,14 +333,18 @@ int _read_trylock(rwlock_t *lock)
     return 1;
 }
 
-void _read_unlock(rwlock_t *lock)
-{
-    uint32_t x, y;
+#ifndef _raw_read_unlock
+# define _raw_read_unlock(l) do {                      \
+    uint32_t x = (l)->lock, y;                         \
+    while ( (y = cmpxchg(&(l)->lock, x, x - 1)) != x ) \
+        x = y;                                         \
+} while (0)
+#endif
 
+inline void _read_unlock(rwlock_t *lock)
+{
     preempt_enable();
-    x = lock->lock;
-    while ( (y = cmpxchg(&lock->lock, x, x-1)) != x )
-        x = y;
+    _raw_read_unlock(lock);
 }
 
 void _read_unlock_irq(rwlock_t *lock)
xen/include/asm-x86/spinlock.h +3 -0
@@ -31,4 +31,7 @@ static always_inline int _raw_spin_trylo
     return (oldval > 0);
 }
 
+#define _raw_read_unlock(l) \
+    asm volatile ( "lock; dec%z0 %0" : "+m" ((l)->lock) :: "memory" )
+
 #endif /* __ASM_SPINLOCK_H */

rwlock: allow arch to override read_unlock() atomic

On x86, LOCK DEC is cheaper than LOCK CMPXCHG and doesn't require a retry loop around it.

xen/common/spinlock.c +10 -6
@@ -333,14 +333,18 @@ int _read_trylock(rwlock_t *lock)
     return 1;
 }
 
-void _read_unlock(rwlock_t *lock)
-{
-    uint32_t x, y;
+#ifndef _raw_read_unlock
+# define _raw_read_unlock(l) do {                      \
+    uint32_t x = (l)->lock, y;                         \
+    while ( (y = cmpxchg(&(l)->lock, x, x - 1)) != x ) \
+        x = y;                                         \
+} while (0)
+#endif
 
+inline void _read_unlock(rwlock_t *lock)
+{
     preempt_enable();
-    x = lock->lock;
-    while ( (y = cmpxchg(&lock->lock, x, x-1)) != x )
-        x = y;
+    _raw_read_unlock(lock);
 }
 
 void _read_unlock_irq(rwlock_t *lock)
xen/include/asm-x86/spinlock.h +3 -0
@@ -31,4 +31,7 @@ static always_inline int _raw_spin_trylo
     return (oldval > 0);
 }
 
+#define _raw_read_unlock(l) \
+    asm volatile ( "lock; dec%z0 %0" : "+m" ((l)->lock) :: "memory" )
+
 #endif /* __ASM_SPINLOCK_H */

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

#7 lore raw

PATCH 3/3 rwlock: allow arch to override write_unlock() atomic

In reply to Jan Beulich

... (for consistency with read_unlock()), and default it to xchg(), being generally cheaper than cmpxchg().

xen/common/spinlock.c +6 -2
@@ -438,10 +438,14 @@ int _write_trylock(rwlock_t *lock)
     return 1;
 }
 
-void _write_unlock(rwlock_t *lock)
+#ifndef _raw_write_unlock
+# define _raw_write_unlock(l) xchg(&(l)->lock, 0)
+#endif
+
+inline void _write_unlock(rwlock_t *lock)
 {
     preempt_enable();
-    if ( cmpxchg(&lock->lock, RW_WRITE_FLAG, 0) != RW_WRITE_FLAG )
+    if ( _raw_write_unlock(lock) != RW_WRITE_FLAG )
         BUG();
 }
 

rwlock: allow arch to override write_unlock() atomic

... (for consistency with read_unlock()), and default it to xchg(), being generally cheaper than cmpxchg().

xen/common/spinlock.c +6 -2
@@ -438,10 +438,14 @@ int _write_trylock(rwlock_t *lock)
     return 1;
 }
 
-void _write_unlock(rwlock_t *lock)
+#ifndef _raw_write_unlock
+# define _raw_write_unlock(l) xchg(&(l)->lock, 0)
+#endif
+
+inline void _write_unlock(rwlock_t *lock)
 {
     preempt_enable();
-    if ( cmpxchg(&lock->lock, RW_WRITE_FLAG, 0) != RW_WRITE_FLAG )
+    if ( _raw_write_unlock(lock) != RW_WRITE_FLAG )
         BUG();
 }
 

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