Skip to contentSkip to conversation

arch: barrier cleanup + barriers for virt

PATCH Superseded Michael S. Tsirkin 15 participants 257 messages Dec 31, 2015 – Jan 11, 2016 Page 2 of 2

Subsystems (15)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
author maintainer
#189 lore raw

PATCH v2 28/32 asm-generic: implement virt_xxx memory barriers

In reply to Michael S. Tsirkin

Guests running within virtual machines might be affected by SMP effects even if the guest itself is compiled without SMP support. This is an artifact of interfacing with an SMP host while running an UP kernel. Using mandatory barriers for this use-case would be possible but is often suboptimal.

In particular, virtio uses a bunch of confusing ifdefs to work around this, while xen just uses the mandatory barriers.

To better handle this case, low-level virt_mb() etc macros are made available. These are implemented trivially using the low-level __smp_xxx macros, the purpose of these wrappers is to annotate those specific cases.

These have the same effect as smp_mb() etc when SMP is enabled, but generate identical code for SMP and non-SMP systems. For example, virtual machine guests should use virt_mb() rather than smp_mb() when synchronizing against a (possibly SMP) host.

---
 include/asm-generic/barrier.h     | 11 +++++++++++
 Documentation/memory-barriers.txt | 28 +++++++++++++++++++++++-----
 2 files changed, 34 insertions(+), 5 deletions(-)
include/asm-generic/barrier.h +11 -0
index 8752964..1cceca14 100644
@@ -196,5 +196,16 @@ do {									\
 
 #endif
 
+/* Barriers for virtual machine guests when talking to an SMP host */
+#define virt_mb() __smp_mb()
+#define virt_rmb() __smp_rmb()
+#define virt_wmb() __smp_wmb()
+#define virt_read_barrier_depends() __smp_read_barrier_depends()
+#define virt_store_mb(var, value) __smp_store_mb(var, value)
+#define virt_mb__before_atomic() __smp_mb__before_atomic()
+#define virt_mb__after_atomic()	__smp_mb__after_atomic()
+#define virt_store_release(p, v) __smp_store_release(p, v)
+#define virt_load_acquire(p) __smp_load_acquire(p)
+
 #endif /* !__ASSEMBLY__ */
 #endif /* __ASM_GENERIC_BARRIER_H */
Documentation/memory-barriers.txt +23 -5
index aef9487..8f4a93a 100644
@@ -1655,17 +1655,18 @@ macro is a good place to start looking.
 SMP memory barriers are reduced to compiler barriers on uniprocessor compiled
 systems because it is assumed that a CPU will appear to be self-consistent,
 and will order overlapping accesses correctly with respect to itself.
+However, see the subsection on "Virtual Machine Guests" below.
 
 [!] Note that SMP memory barriers _must_ be used to control the ordering of
 references to shared memory on SMP systems, though the use of locking instead
 is sufficient.
 
 Mandatory barriers should not be used to control SMP effects, since mandatory
-barriers unnecessarily impose overhead on UP systems. They may, however, be
-used to control MMIO effects on accesses through relaxed memory I/O windows.
-These are required even on non-SMP systems as they affect the order in which
-memory operations appear to a device by prohibiting both the compiler and the
-CPU from reordering them.
+barriers impose unnecessary overhead on both SMP and UP systems. They may,
+however, be used to control MMIO effects on accesses through relaxed memory I/O
+windows.  These barriers are required even on non-SMP systems as they affect
+the order in which memory operations appear to a device by prohibiting both the
+compiler and the CPU from reordering them.
 
 
 There are some more advanced barrier functions:
@@ -2948,6 +2949,23 @@ The Alpha defines the Linux kernel's memory barrier model.
 
 See the subsection on "Cache Coherency" above.
 
+VIRTUAL MACHINE GUESTS
+-------------------
+
+Guests running within virtual machines might be affected by SMP effects even if
+the guest itself is compiled without SMP support.  This is an artifact of
+interfacing with an SMP host while running an UP kernel.  Using mandatory
+barriers for this use-case would be possible but is often suboptimal.
+
+To handle this case optimally, low-level virt_mb() etc macros are available.
+These have the same effect as smp_mb() etc when SMP is enabled, but generate
+identical code for SMP and non-SMP systems. For example, virtual machine guests
+should use virt_mb() rather than smp_mb() when synchronizing against a
+(possibly SMP) host.
+
+These are equivalent to smp_mb() etc counterparts in all other respects,
+in particular, they do not control MMIO effects: to control
+MMIO effects, use mandatory barriers.
 
 ============
 EXAMPLE USES
Signature · 1 line
MST
author maintainer
#190 lore raw

PATCH v2 28/32 asm-generic: implement virt_xxx memory barriers

In reply to Michael S. Tsirkin

Guests running within virtual machines might be affected by SMP effects even if the guest itself is compiled without SMP support. This is an artifact of interfacing with an SMP host while running an UP kernel. Using mandatory barriers for this use-case would be possible but is often suboptimal.

In particular, virtio uses a bunch of confusing ifdefs to work around this, while xen just uses the mandatory barriers.

To better handle this case, low-level virt_mb() etc macros are made available. These are implemented trivially using the low-level __smp_xxx macros, the purpose of these wrappers is to annotate those specific cases.

These have the same effect as smp_mb() etc when SMP is enabled, but generate identical code for SMP and non-SMP systems. For example, virtual machine guests should use virt_mb() rather than smp_mb() when synchronizing against a (possibly SMP) host.

---
 include/asm-generic/barrier.h     | 11 +++++++++++
 Documentation/memory-barriers.txt | 28 +++++++++++++++++++++++-----
 2 files changed, 34 insertions(+), 5 deletions(-)
include/asm-generic/barrier.h +11 -0
index 8752964..1cceca14 100644
@@ -196,5 +196,16 @@ do {									\
 
 #endif
 
+/* Barriers for virtual machine guests when talking to an SMP host */
+#define virt_mb() __smp_mb()
+#define virt_rmb() __smp_rmb()
+#define virt_wmb() __smp_wmb()
+#define virt_read_barrier_depends() __smp_read_barrier_depends()
+#define virt_store_mb(var, value) __smp_store_mb(var, value)
+#define virt_mb__before_atomic() __smp_mb__before_atomic()
+#define virt_mb__after_atomic()	__smp_mb__after_atomic()
+#define virt_store_release(p, v) __smp_store_release(p, v)
+#define virt_load_acquire(p) __smp_load_acquire(p)
+
 #endif /* !__ASSEMBLY__ */
 #endif /* __ASM_GENERIC_BARRIER_H */
Documentation/memory-barriers.txt +23 -5
index aef9487..8f4a93a 100644
@@ -1655,17 +1655,18 @@ macro is a good place to start looking.
 SMP memory barriers are reduced to compiler barriers on uniprocessor compiled
 systems because it is assumed that a CPU will appear to be self-consistent,
 and will order overlapping accesses correctly with respect to itself.
+However, see the subsection on "Virtual Machine Guests" below.
 
 [!] Note that SMP memory barriers _must_ be used to control the ordering of
 references to shared memory on SMP systems, though the use of locking instead
 is sufficient.
 
 Mandatory barriers should not be used to control SMP effects, since mandatory
-barriers unnecessarily impose overhead on UP systems. They may, however, be
-used to control MMIO effects on accesses through relaxed memory I/O windows.
-These are required even on non-SMP systems as they affect the order in which
-memory operations appear to a device by prohibiting both the compiler and the
-CPU from reordering them.
+barriers impose unnecessary overhead on both SMP and UP systems. They may,
+however, be used to control MMIO effects on accesses through relaxed memory I/O
+windows.  These barriers are required even on non-SMP systems as they affect
+the order in which memory operations appear to a device by prohibiting both the
+compiler and the CPU from reordering them.
 
 
 There are some more advanced barrier functions:
@@ -2948,6 +2949,23 @@ The Alpha defines the Linux kernel's memory barrier model.
 
 See the subsection on "Cache Coherency" above.
 
+VIRTUAL MACHINE GUESTS
+-------------------
+
+Guests running within virtual machines might be affected by SMP effects even if
+the guest itself is compiled without SMP support.  This is an artifact of
+interfacing with an SMP host while running an UP kernel.  Using mandatory
+barriers for this use-case would be possible but is often suboptimal.
+
+To handle this case optimally, low-level virt_mb() etc macros are available.
+These have the same effect as smp_mb() etc when SMP is enabled, but generate
+identical code for SMP and non-SMP systems. For example, virtual machine guests
+should use virt_mb() rather than smp_mb() when synchronizing against a
+(possibly SMP) host.
+
+These are equivalent to smp_mb() etc counterparts in all other respects,
+in particular, they do not control MMIO effects: to control
+MMIO effects, use mandatory barriers.
 
 ============
 EXAMPLE USES
Signature · 1 line
MST
author maintainer
#191 lore raw

PATCH v2 28/32 asm-generic: implement virt_xxx memory barriers

In reply to Michael S. Tsirkin

Guests running within virtual machines might be affected by SMP effects even if the guest itself is compiled without SMP support. This is an artifact of interfacing with an SMP host while running an UP kernel. Using mandatory barriers for this use-case would be possible but is often suboptimal.

In particular, virtio uses a bunch of confusing ifdefs to work around this, while xen just uses the mandatory barriers.

To better handle this case, low-level virt_mb() etc macros are made available. These are implemented trivially using the low-level __smp_xxx macros, the purpose of these wrappers is to annotate those specific cases.

These have the same effect as smp_mb() etc when SMP is enabled, but generate identical code for SMP and non-SMP systems. For example, virtual machine guests should use virt_mb() rather than smp_mb() when synchronizing against a (possibly SMP) host.

---
 include/asm-generic/barrier.h     | 11 +++++++++++
 Documentation/memory-barriers.txt | 28 +++++++++++++++++++++++-----
 2 files changed, 34 insertions(+), 5 deletions(-)
include/asm-generic/barrier.h +11 -0
index 8752964..1cceca14 100644
@@ -196,5 +196,16 @@ do {									\
 
 #endif
 
+/* Barriers for virtual machine guests when talking to an SMP host */
+#define virt_mb() __smp_mb()
+#define virt_rmb() __smp_rmb()
+#define virt_wmb() __smp_wmb()
+#define virt_read_barrier_depends() __smp_read_barrier_depends()
+#define virt_store_mb(var, value) __smp_store_mb(var, value)
+#define virt_mb__before_atomic() __smp_mb__before_atomic()
+#define virt_mb__after_atomic()	__smp_mb__after_atomic()
+#define virt_store_release(p, v) __smp_store_release(p, v)
+#define virt_load_acquire(p) __smp_load_acquire(p)
+
 #endif /* !__ASSEMBLY__ */
 #endif /* __ASM_GENERIC_BARRIER_H */
Documentation/memory-barriers.txt +23 -5
index aef9487..8f4a93a 100644
@@ -1655,17 +1655,18 @@ macro is a good place to start looking.
 SMP memory barriers are reduced to compiler barriers on uniprocessor compiled
 systems because it is assumed that a CPU will appear to be self-consistent,
 and will order overlapping accesses correctly with respect to itself.
+However, see the subsection on "Virtual Machine Guests" below.
 
 [!] Note that SMP memory barriers _must_ be used to control the ordering of
 references to shared memory on SMP systems, though the use of locking instead
 is sufficient.
 
 Mandatory barriers should not be used to control SMP effects, since mandatory
-barriers unnecessarily impose overhead on UP systems. They may, however, be
-used to control MMIO effects on accesses through relaxed memory I/O windows.
-These are required even on non-SMP systems as they affect the order in which
-memory operations appear to a device by prohibiting both the compiler and the
-CPU from reordering them.
+barriers impose unnecessary overhead on both SMP and UP systems. They may,
+however, be used to control MMIO effects on accesses through relaxed memory I/O
+windows.  These barriers are required even on non-SMP systems as they affect
+the order in which memory operations appear to a device by prohibiting both the
+compiler and the CPU from reordering them.
 
 
 There are some more advanced barrier functions:
@@ -2948,6 +2949,23 @@ The Alpha defines the Linux kernel's memory barrier model.
 
 See the subsection on "Cache Coherency" above.
 
+VIRTUAL MACHINE GUESTS
+-------------------
+
+Guests running within virtual machines might be affected by SMP effects even if
+the guest itself is compiled without SMP support.  This is an artifact of
+interfacing with an SMP host while running an UP kernel.  Using mandatory
+barriers for this use-case would be possible but is often suboptimal.
+
+To handle this case optimally, low-level virt_mb() etc macros are available.
+These have the same effect as smp_mb() etc when SMP is enabled, but generate
+identical code for SMP and non-SMP systems. For example, virtual machine guests
+should use virt_mb() rather than smp_mb() when synchronizing against a
+(possibly SMP) host.
+
+These are equivalent to smp_mb() etc counterparts in all other respects,
+in particular, they do not control MMIO effects: to control
+MMIO effects, use mandatory barriers.
 
 ============
 EXAMPLE USES
Signature · 1 line
MST
author maintainer
#192 lore raw

PATCH v2 29/32 Revert "virtio_ring: Update weak barriers to use dma_wmb/rmb"

In reply to Michael S. Tsirkin

This reverts commit 9e1a27ea42691429e31f158cce6fc61bc79bb2e9.

While that commit optimizes !CONFIG_SMP, it mixes up DMA and SMP concepts, making the code hard to figure out.

A better way to optimize this is with the new __smp_XXX barriers.

As a first step, go back to full rmb/wmb barriers for !SMP.
We switch to __smp_XXX barriers in the next patch.

Cc Peter Zijlstra <[email protected]>
Cc Alexander Duyck <[email protected]>
---
 include/linux/virtio_ring.h | 23 +++++++++++++++++++----
 1 file changed, 19 insertions(+), 4 deletions(-)
include/linux/virtio_ring.h +19 -4
index 8e50888..67e06fe 100644
@@ -21,20 +21,19 @@
  * actually quite cheap.
  */
 
+#ifdef CONFIG_SMP
 static inline void virtio_mb(bool weak_barriers)
 {
-#ifdef CONFIG_SMP
 	if (weak_barriers)
 		smp_mb();
 	else
-#endif
 		mb();
 }
 
 static inline void virtio_rmb(bool weak_barriers)
 {
 	if (weak_barriers)
-		dma_rmb();
+		smp_rmb();
 	else
 		rmb();
 }
@@ -42,10 +41,26 @@ static inline void virtio_rmb(bool weak_barriers)
 static inline void virtio_wmb(bool weak_barriers)
 {
 	if (weak_barriers)
-		dma_wmb();
+		smp_wmb();
 	else
 		wmb();
 }
+#else
+static inline void virtio_mb(bool weak_barriers)
+{
+	mb();
+}
+
+static inline void virtio_rmb(bool weak_barriers)
+{
+	rmb();
+}
+
+static inline void virtio_wmb(bool weak_barriers)
+{
+	wmb();
+}
+#endif
 
 struct virtio_device;
 struct virtqueue;
Signature · 1 line
MST
author maintainer
#193 lore raw

PATCH v2 29/32 Revert "virtio_ring: Update weak barriers to use dma_wmb/rmb"

In reply to Michael S. Tsirkin

This reverts commit 9e1a27ea42691429e31f158cce6fc61bc79bb2e9.

While that commit optimizes !CONFIG_SMP, it mixes up DMA and SMP concepts, making the code hard to figure out.

A better way to optimize this is with the new __smp_XXX barriers.

As a first step, go back to full rmb/wmb barriers for !SMP.
We switch to __smp_XXX barriers in the next patch.

Cc Peter Zijlstra <[email protected]>
Cc Alexander Duyck <[email protected]>
---
 include/linux/virtio_ring.h | 23 +++++++++++++++++++----
 1 file changed, 19 insertions(+), 4 deletions(-)
include/linux/virtio_ring.h +19 -4
index 8e50888..67e06fe 100644
@@ -21,20 +21,19 @@
  * actually quite cheap.
  */
 
+#ifdef CONFIG_SMP
 static inline void virtio_mb(bool weak_barriers)
 {
-#ifdef CONFIG_SMP
 	if (weak_barriers)
 		smp_mb();
 	else
-#endif
 		mb();
 }
 
 static inline void virtio_rmb(bool weak_barriers)
 {
 	if (weak_barriers)
-		dma_rmb();
+		smp_rmb();
 	else
 		rmb();
 }
@@ -42,10 +41,26 @@ static inline void virtio_rmb(bool weak_barriers)
 static inline void virtio_wmb(bool weak_barriers)
 {
 	if (weak_barriers)
-		dma_wmb();
+		smp_wmb();
 	else
 		wmb();
 }
+#else
+static inline void virtio_mb(bool weak_barriers)
+{
+	mb();
+}
+
+static inline void virtio_rmb(bool weak_barriers)
+{
+	rmb();
+}
+
+static inline void virtio_wmb(bool weak_barriers)
+{
+	wmb();
+}
+#endif
 
 struct virtio_device;
 struct virtqueue;
Signature · 1 line
MST
author maintainer
#194 lore raw

PATCH v2 29/32 Revert "virtio_ring: Update weak barriers to use dma_wmb/rmb"

In reply to Michael S. Tsirkin

This reverts commit 9e1a27ea42691429e31f158cce6fc61bc79bb2e9.

While that commit optimizes !CONFIG_SMP, it mixes up DMA and SMP concepts, making the code hard to figure out.

A better way to optimize this is with the new __smp_XXX barriers.

As a first step, go back to full rmb/wmb barriers for !SMP.
We switch to __smp_XXX barriers in the next patch.

Cc Peter Zijlstra <[email protected]>
Cc Alexander Duyck <[email protected]>
---
 include/linux/virtio_ring.h | 23 +++++++++++++++++++----
 1 file changed, 19 insertions(+), 4 deletions(-)
include/linux/virtio_ring.h +19 -4
index 8e50888..67e06fe 100644
@@ -21,20 +21,19 @@
  * actually quite cheap.
  */
 
+#ifdef CONFIG_SMP
 static inline void virtio_mb(bool weak_barriers)
 {
-#ifdef CONFIG_SMP
 	if (weak_barriers)
 		smp_mb();
 	else
-#endif
 		mb();
 }
 
 static inline void virtio_rmb(bool weak_barriers)
 {
 	if (weak_barriers)
-		dma_rmb();
+		smp_rmb();
 	else
 		rmb();
 }
@@ -42,10 +41,26 @@ static inline void virtio_rmb(bool weak_barriers)
 static inline void virtio_wmb(bool weak_barriers)
 {
 	if (weak_barriers)
-		dma_wmb();
+		smp_wmb();
 	else
 		wmb();
 }
+#else
+static inline void virtio_mb(bool weak_barriers)
+{
+	mb();
+}
+
+static inline void virtio_rmb(bool weak_barriers)
+{
+	rmb();
+}
+
+static inline void virtio_wmb(bool weak_barriers)
+{
+	wmb();
+}
+#endif
 
 struct virtio_device;
 struct virtqueue;
Signature · 1 line
MST
author maintainer
#195 lore raw

PATCH v2 30/32 virtio_ring: update weak barriers to use __smp_xxx

In reply to Michael S. Tsirkin

virtio ring uses smp_wmb on SMP and wmb on !SMP, the reason for the later being that it might be talking to another kernel on the same SMP machine.

This is exactly what __smp_XXX barriers do, so switch to these instead of homegrown ifdef hacks.

Cc Peter Zijlstra <[email protected]>
Cc Alexander Duyck <[email protected]>
---
 include/linux/virtio_ring.h | 25 ++++---------------------
 1 file changed, 4 insertions(+), 21 deletions(-)
include/linux/virtio_ring.h +4 -21
index 67e06fe..f3fa55b 100644
@@ -12,7 +12,7 @@
  * anyone care?
  *
  * For virtio_pci on SMP, we don't need to order with respect to MMIO
- * accesses through relaxed memory I/O windows, so smp_mb() et al are
+ * accesses through relaxed memory I/O windows, so virt_mb() et al are
  * sufficient.
  *
  * For using virtio to talk to real devices (eg. other heterogeneous
@@ -21,11 +21,10 @@
  * actually quite cheap.
  */
 
-#ifdef CONFIG_SMP
 static inline void virtio_mb(bool weak_barriers)
 {
 	if (weak_barriers)
-		smp_mb();
+		virt_mb();
 	else
 		mb();
 }
@@ -33,7 +32,7 @@ static inline void virtio_mb(bool weak_barriers)
 static inline void virtio_rmb(bool weak_barriers)
 {
 	if (weak_barriers)
-		smp_rmb();
+		virt_rmb();
 	else
 		rmb();
 }
@@ -41,26 +40,10 @@ static inline void virtio_rmb(bool weak_barriers)
 static inline void virtio_wmb(bool weak_barriers)
 {
 	if (weak_barriers)
-		smp_wmb();
+		virt_wmb();
 	else
 		wmb();
 }
-#else
-static inline void virtio_mb(bool weak_barriers)
-{
-	mb();
-}
-
-static inline void virtio_rmb(bool weak_barriers)
-{
-	rmb();
-}
-
-static inline void virtio_wmb(bool weak_barriers)
-{
-	wmb();
-}
-#endif
 
 struct virtio_device;
 struct virtqueue;
Signature · 1 line
MST
author maintainer
#196 lore raw

Re: [PATCH v2 30/32] virtio_ring: update weak barriers to use __smp_xxx

Quoted 10 lines · Michael S. Tsirkin

virtio ring uses smp_wmb on SMP and wmb on !SMP, the reason for the later being that it might be talking to another kernel on the same SMP machine.

This is exactly what __smp_XXX barriers do, so switch to these instead of homegrown ifdef hacks.

Cc Peter Zijlstra <[email protected]>
Cc Alexander Duyck <[email protected]>

The subject and commit log should say
virt_xxx and not __smp_xxx - I fixed this up in
my tree.

Quoted 69 lines
---
 include/linux/virtio_ring.h | 25 ++++---------------------
 1 file changed, 4 insertions(+), 21 deletions(-)
include/linux/virtio_ring.h +4 -21
index 67e06fe..f3fa55b 100644
@@ -12,7 +12,7 @@
  * anyone care?
  *
  * For virtio_pci on SMP, we don't need to order with respect to MMIO
- * accesses through relaxed memory I/O windows, so smp_mb() et al are
+ * accesses through relaxed memory I/O windows, so virt_mb() et al are
  * sufficient.
  *
  * For using virtio to talk to real devices (eg. other heterogeneous
@@ -21,11 +21,10 @@
  * actually quite cheap.
  */
 
-#ifdef CONFIG_SMP
 static inline void virtio_mb(bool weak_barriers)
 {
 	if (weak_barriers)
-		smp_mb();
+		virt_mb();
 	else
 		mb();
 }
@@ -33,7 +32,7 @@ static inline void virtio_mb(bool weak_barriers)
 static inline void virtio_rmb(bool weak_barriers)
 {
 	if (weak_barriers)
-		smp_rmb();
+		virt_rmb();
 	else
 		rmb();
 }
@@ -41,26 +40,10 @@ static inline void virtio_rmb(bool weak_barriers)
 static inline void virtio_wmb(bool weak_barriers)
 {
 	if (weak_barriers)
-		smp_wmb();
+		virt_wmb();
 	else
 		wmb();
 }
-#else
-static inline void virtio_mb(bool weak_barriers)
-{
-	mb();
-}
-
-static inline void virtio_rmb(bool weak_barriers)
-{
-	rmb();
-}
-
-static inline void virtio_wmb(bool weak_barriers)
-{
-	wmb();
-}
-#endif
 
 struct virtio_device;
 struct virtqueue;
Signature · 1 line
MST
author maintainer
#197 lore raw

Re: [PATCH v2 30/32] virtio_ring: update weak barriers to use __smp_xxx

In reply to Michael S. Tsirkin
Quoted 10 lines · Michael S. Tsirkin

virtio ring uses smp_wmb on SMP and wmb on !SMP, the reason for the later being that it might be talking to another kernel on the same SMP machine.

This is exactly what __smp_XXX barriers do, so switch to these instead of homegrown ifdef hacks.

Cc Peter Zijlstra <[email protected]>
Cc Alexander Duyck <[email protected]>

The subject and commit log should say
virt_xxx and not __smp_xxx - I fixed this up in
my tree.

Quoted 69 lines
---
 include/linux/virtio_ring.h | 25 ++++---------------------
 1 file changed, 4 insertions(+), 21 deletions(-)
include/linux/virtio_ring.h +4 -21
index 67e06fe..f3fa55b 100644
@@ -12,7 +12,7 @@
  * anyone care?
  *
  * For virtio_pci on SMP, we don't need to order with respect to MMIO
- * accesses through relaxed memory I/O windows, so smp_mb() et al are
+ * accesses through relaxed memory I/O windows, so virt_mb() et al are
  * sufficient.
  *
  * For using virtio to talk to real devices (eg. other heterogeneous
@@ -21,11 +21,10 @@
  * actually quite cheap.
  */
 
-#ifdef CONFIG_SMP
 static inline void virtio_mb(bool weak_barriers)
 {
 	if (weak_barriers)
-		smp_mb();
+		virt_mb();
 	else
 		mb();
 }
@@ -33,7 +32,7 @@ static inline void virtio_mb(bool weak_barriers)
 static inline void virtio_rmb(bool weak_barriers)
 {
 	if (weak_barriers)
-		smp_rmb();
+		virt_rmb();
 	else
 		rmb();
 }
@@ -41,26 +40,10 @@ static inline void virtio_rmb(bool weak_barriers)
 static inline void virtio_wmb(bool weak_barriers)
 {
 	if (weak_barriers)
-		smp_wmb();
+		virt_wmb();
 	else
 		wmb();
 }
-#else
-static inline void virtio_mb(bool weak_barriers)
-{
-	mb();
-}
-
-static inline void virtio_rmb(bool weak_barriers)
-{
-	rmb();
-}
-
-static inline void virtio_wmb(bool weak_barriers)
-{
-	wmb();
-}
-#endif
 
 struct virtio_device;
 struct virtqueue;
Signature · 1 line
MST
author maintainer
#198 lore raw

Re: [PATCH v2 30/32] virtio_ring: update weak barriers to use __smp_xxx

In reply to Michael S. Tsirkin
Quoted 10 lines · Michael S. Tsirkin

virtio ring uses smp_wmb on SMP and wmb on !SMP, the reason for the later being that it might be talking to another kernel on the same SMP machine.

This is exactly what __smp_XXX barriers do, so switch to these instead of homegrown ifdef hacks.

Cc Peter Zijlstra <[email protected]>
Cc Alexander Duyck <[email protected]>

The subject and commit log should say
virt_xxx and not __smp_xxx - I fixed this up in
my tree.

Quoted 69 lines
---
 include/linux/virtio_ring.h | 25 ++++---------------------
 1 file changed, 4 insertions(+), 21 deletions(-)
include/linux/virtio_ring.h +4 -21
index 67e06fe..f3fa55b 100644
@@ -12,7 +12,7 @@
  * anyone care?
  *
  * For virtio_pci on SMP, we don't need to order with respect to MMIO
- * accesses through relaxed memory I/O windows, so smp_mb() et al are
+ * accesses through relaxed memory I/O windows, so virt_mb() et al are
  * sufficient.
  *
  * For using virtio to talk to real devices (eg. other heterogeneous
@@ -21,11 +21,10 @@
  * actually quite cheap.
  */
 
-#ifdef CONFIG_SMP
 static inline void virtio_mb(bool weak_barriers)
 {
 	if (weak_barriers)
-		smp_mb();
+		virt_mb();
 	else
 		mb();
 }
@@ -33,7 +32,7 @@ static inline void virtio_mb(bool weak_barriers)
 static inline void virtio_rmb(bool weak_barriers)
 {
 	if (weak_barriers)
-		smp_rmb();
+		virt_rmb();
 	else
 		rmb();
 }
@@ -41,26 +40,10 @@ static inline void virtio_rmb(bool weak_barriers)
 static inline void virtio_wmb(bool weak_barriers)
 {
 	if (weak_barriers)
-		smp_wmb();
+		virt_wmb();
 	else
 		wmb();
 }
-#else
-static inline void virtio_mb(bool weak_barriers)
-{
-	mb();
-}
-
-static inline void virtio_rmb(bool weak_barriers)
-{
-	rmb();
-}
-
-static inline void virtio_wmb(bool weak_barriers)
-{
-	wmb();
-}
-#endif
 
 struct virtio_device;
 struct virtqueue;
Signature · 1 line
MST
author maintainer
#199 lore raw

PATCH v2 30/32 virtio_ring: update weak barriers to use __smp_XXX

In reply to Michael S. Tsirkin

virtio ring uses smp_wmb on SMP and wmb on !SMP, the reason for the later being that it might be talking to another kernel on the same SMP machine.

This is exactly what __smp_XXX barriers do, so switch to these instead of homegrown ifdef hacks.

Cc Peter Zijlstra <[email protected]>
Cc Alexander Duyck <[email protected]>
---
 include/linux/virtio_ring.h | 25 ++++---------------------
 1 file changed, 4 insertions(+), 21 deletions(-)
include/linux/virtio_ring.h +4 -21
index 67e06fe..f3fa55b 100644
@@ -12,7 +12,7 @@
  * anyone care?
  *
  * For virtio_pci on SMP, we don't need to order with respect to MMIO
- * accesses through relaxed memory I/O windows, so smp_mb() et al are
+ * accesses through relaxed memory I/O windows, so virt_mb() et al are
  * sufficient.
  *
  * For using virtio to talk to real devices (eg. other heterogeneous
@@ -21,11 +21,10 @@
  * actually quite cheap.
  */
 
-#ifdef CONFIG_SMP
 static inline void virtio_mb(bool weak_barriers)
 {
 	if (weak_barriers)
-		smp_mb();
+		virt_mb();
 	else
 		mb();
 }
@@ -33,7 +32,7 @@ static inline void virtio_mb(bool weak_barriers)
 static inline void virtio_rmb(bool weak_barriers)
 {
 	if (weak_barriers)
-		smp_rmb();
+		virt_rmb();
 	else
 		rmb();
 }
@@ -41,26 +40,10 @@ static inline void virtio_rmb(bool weak_barriers)
 static inline void virtio_wmb(bool weak_barriers)
 {
 	if (weak_barriers)
-		smp_wmb();
+		virt_wmb();
 	else
 		wmb();
 }
-#else
-static inline void virtio_mb(bool weak_barriers)
-{
-	mb();
-}
-
-static inline void virtio_rmb(bool weak_barriers)
-{
-	rmb();
-}
-
-static inline void virtio_wmb(bool weak_barriers)
-{
-	wmb();
-}
-#endif
 
 struct virtio_device;
 struct virtqueue;
Signature · 1 line
MST
author maintainer
#200 lore raw

PATCH v2 30/32 virtio_ring: update weak barriers to use __smp_xxx

In reply to Michael S. Tsirkin

virtio ring uses smp_wmb on SMP and wmb on !SMP, the reason for the later being that it might be talking to another kernel on the same SMP machine.

This is exactly what __smp_XXX barriers do, so switch to these instead of homegrown ifdef hacks.

Cc Peter Zijlstra <[email protected]>
Cc Alexander Duyck <[email protected]>
---
 include/linux/virtio_ring.h | 25 ++++---------------------
 1 file changed, 4 insertions(+), 21 deletions(-)
include/linux/virtio_ring.h +4 -21
index 67e06fe..f3fa55b 100644
@@ -12,7 +12,7 @@
  * anyone care?
  *
  * For virtio_pci on SMP, we don't need to order with respect to MMIO
- * accesses through relaxed memory I/O windows, so smp_mb() et al are
+ * accesses through relaxed memory I/O windows, so virt_mb() et al are
  * sufficient.
  *
  * For using virtio to talk to real devices (eg. other heterogeneous
@@ -21,11 +21,10 @@
  * actually quite cheap.
  */
 
-#ifdef CONFIG_SMP
 static inline void virtio_mb(bool weak_barriers)
 {
 	if (weak_barriers)
-		smp_mb();
+		virt_mb();
 	else
 		mb();
 }
@@ -33,7 +32,7 @@ static inline void virtio_mb(bool weak_barriers)
 static inline void virtio_rmb(bool weak_barriers)
 {
 	if (weak_barriers)
-		smp_rmb();
+		virt_rmb();
 	else
 		rmb();
 }
@@ -41,26 +40,10 @@ static inline void virtio_rmb(bool weak_barriers)
 static inline void virtio_wmb(bool weak_barriers)
 {
 	if (weak_barriers)
-		smp_wmb();
+		virt_wmb();
 	else
 		wmb();
 }
-#else
-static inline void virtio_mb(bool weak_barriers)
-{
-	mb();
-}
-
-static inline void virtio_rmb(bool weak_barriers)
-{
-	rmb();
-}
-
-static inline void virtio_wmb(bool weak_barriers)
-{
-	wmb();
-}
-#endif
 
 struct virtio_device;
 struct virtqueue;
Signature · 1 line
MST
author maintainer
#201 lore raw

PATCH v2 31/32 sh: support a 2-byte smp_store_mb

In reply to Michael S. Tsirkin

At the moment, xchg on sh only supports 4 and 1 byte values, so using it from smp_store_mb means attempts to store a 2 byte value using this macro fail.

And happens to be exactly what virtio drivers want to do.

Check size and fall back to a slower, but safe, WRITE_ONCE+smp_mb.

---
 arch/sh/include/asm/barrier.h | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)
arch/sh/include/asm/barrier.h +9 -1
index f887c64..0cc5735 100644
@@ -32,7 +32,15 @@
 #define ctrl_barrier()	__asm__ __volatile__ ("nop;nop;nop;nop;nop;nop;nop;nop")
 #endif
 
-#define __smp_store_mb(var, value) do { (void)xchg(&var, value); } while (0)
+#define __smp_store_mb(var, value) do { \
+	if (sizeof(var) != 4 && sizeof(var) != 1) { \
+		 WRITE_ONCE(var, value); \
+		__smp_mb(); \
+	} else { \
+		(void)xchg(&var, value);  \
+	} \
+} while (0)
+
 #define smp_store_mb(var, value) __smp_store_mb(var, value)
 
 #include <asm-generic/barrier.h>
Signature · 1 line
MST
maintainer
#202 lore raw

Re: [PATCH v2 31/32] sh: support a 2-byte smp_store_mb

Quoted 20 lines · Michael S. Tsirkin

At the moment, xchg on sh only supports 4 and 1 byte values, so using it from smp_store_mb means attempts to store a 2 byte value using this macro fail.

And happens to be exactly what virtio drivers want to do.

Check size and fall back to a slower, but safe, WRITE_ONCE+smp_mb.

---
 arch/sh/include/asm/barrier.h | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)
arch/sh/include/asm/barrier.h +0 -0
index f887c64..0cc5735 100644
@@ -32,7 +32,15 @@
 #define ctrl_barrier()	__asm__ __volatile__ ("nop;nop;nop;nop;nop;nop;nop;nop")
 #endif

arch/sh/include/asm/barrier.h line 42

Quoted 10 lines
arch/sh/include/asm/barrier.h +8 -1
 
-#define __smp_store_mb(var, value) do { (void)xchg(&var, value); } while (0)
+#define __smp_store_mb(var, value) do { \
+	if (sizeof(var) != 4 && sizeof(var) != 1) { \
+		 WRITE_ONCE(var, value); \
+		__smp_mb(); \
+	} else { \
+		(void)xchg(&var, value);  \
+	} \
+} while (0)

So SH is an orphaned arch, which is also why I did not comment on using xchg() for the UP smp_store_mb() thing.

But I really think we should try fixing the xchg() implementation instead of this duct-tape.

maintainer
#203 lore raw

Re: [PATCH v2 31/32] sh: support a 2-byte smp_store_mb

In reply to Michael S. Tsirkin
Quoted 20 lines · Michael S. Tsirkin

At the moment, xchg on sh only supports 4 and 1 byte values, so using it from smp_store_mb means attempts to store a 2 byte value using this macro fail.

And happens to be exactly what virtio drivers want to do.

Check size and fall back to a slower, but safe, WRITE_ONCE+smp_mb.

---
 arch/sh/include/asm/barrier.h | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)
arch/sh/include/asm/barrier.h +0 -0
index f887c64..0cc5735 100644
@@ -32,7 +32,15 @@
 #define ctrl_barrier()	__asm__ __volatile__ ("nop;nop;nop;nop;nop;nop;nop;nop")
 #endif

arch/sh/include/asm/barrier.h line 42

Quoted 10 lines
arch/sh/include/asm/barrier.h +8 -1
 
-#define __smp_store_mb(var, value) do { (void)xchg(&var, value); } while (0)
+#define __smp_store_mb(var, value) do { \
+	if (sizeof(var) != 4 && sizeof(var) != 1) { \
+		 WRITE_ONCE(var, value); \
+		__smp_mb(); \
+	} else { \
+		(void)xchg(&var, value);  \
+	} \
+} while (0)

So SH is an orphaned arch, which is also why I did not comment on using xchg() for the UP smp_store_mb() thing.

But I really think we should try fixing the xchg() implementation instead of this duct-tape.

maintainer
#204 lore raw

Re: [PATCH v2 31/32] sh: support a 2-byte smp_store_mb

In reply to Michael S. Tsirkin
Quoted 20 lines · Michael S. Tsirkin

At the moment, xchg on sh only supports 4 and 1 byte values, so using it from smp_store_mb means attempts to store a 2 byte value using this macro fail.

And happens to be exactly what virtio drivers want to do.

Check size and fall back to a slower, but safe, WRITE_ONCE+smp_mb.

---
 arch/sh/include/asm/barrier.h | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)
arch/sh/include/asm/barrier.h +0 -0
index f887c64..0cc5735 100644
@@ -32,7 +32,15 @@
 #define ctrl_barrier()	__asm__ __volatile__ ("nop;nop;nop;nop;nop;nop;nop;nop")
 #endif

arch/sh/include/asm/barrier.h line 42

Quoted 10 lines
arch/sh/include/asm/barrier.h +8 -1
 
-#define __smp_store_mb(var, value) do { (void)xchg(&var, value); } while (0)
+#define __smp_store_mb(var, value) do { \
+	if (sizeof(var) != 4 && sizeof(var) != 1) { \
+		 WRITE_ONCE(var, value); \
+		__smp_mb(); \
+	} else { \
+		(void)xchg(&var, value);  \
+	} \
+} while (0)

So SH is an orphaned arch, which is also why I did not comment on using xchg() for the UP smp_store_mb() thing.

But I really think we should try fixing the xchg() implementation instead of this duct-tape.

maintainer
#205 lore raw

Re: [PATCH v2 31/32] sh: support a 2-byte smp_store_mb

In reply to Michael S. Tsirkin
Quoted 7 lines · Michael S. Tsirkin

At the moment, xchg on sh only supports 4 and 1 byte values, so using it from smp_store_mb means attempts to store a 2 byte value using this macro fail.

And happens to be exactly what virtio drivers want to do.

Check size and fall back to a slower, but safe, WRITE_ONCE+smp_mb.

Can you please do this for size 1 as well (i.e. all sizes != 4)? If you check the source, the code for size-1 xchg in sh cmpxchg-llsc.h is completely wrong and operates on a 32-bit object at the address passed to it. This code is presently unused anyway and I plan to submit a patch to remove the size 1 case.

Rich

author maintainer
#206 lore raw

Re: [PATCH v2 31/32] sh: support a 2-byte smp_store_mb

Quoted 9 lines · Rich Felker
Quoted 7 lines · Michael S. Tsirkin

At the moment, xchg on sh only supports 4 and 1 byte values, so using it from smp_store_mb means attempts to store a 2 byte value using this macro fail.

And happens to be exactly what virtio drivers want to do.

Check size and fall back to a slower, but safe, WRITE_ONCE+smp_mb.

Quoted 7 lines

Can you please do this for size 1 as well (i.e. all sizes != 4)? If you check the source, the code for size-1 xchg in sh cmpxchg-llsc.h is completely wrong and operates on a 32-bit object at the address passed to it. This code is presently unused anyway and I plan to submit a patch to remove the size 1 case.

Rich

Ouch. And PeterZ says I should write a 2-byte xchg in asm instead, and Fedora can't even build a full kernel for this arch at the moment :(

Peter, what do you think? How about I leave this patch as is for now?

Signature · 1 line
MST
maintainer
#207 lore raw

Re: [PATCH v2 31/32] sh: support a 2-byte smp_store_mb

Quoted 16 lines · Michael S. Tsirkin
Quoted 15 lines · Rich Felker
Quoted 7 lines · Michael S. Tsirkin

At the moment, xchg on sh only supports 4 and 1 byte values, so using it from smp_store_mb means attempts to store a 2 byte value using this macro fail.

And happens to be exactly what virtio drivers want to do.

Check size and fall back to a slower, but safe, WRITE_ONCE+smp_mb.

Can you please do this for size 1 as well (i.e. all sizes != 4)? If you check the source, the code for size-1 xchg in sh cmpxchg-llsc.h is completely wrong and operates on a 32-bit object at the address passed to it. This code is presently unused anyway and I plan to submit a patch to remove the size 1 case.

Quoted 4 lines
Quoted 1 line

Rich

Ouch. And PeterZ says I should write a 2-byte xchg in asm instead, and Fedora can't even build a full kernel for this arch at the moment :(

Does the kernel.org hosted cross compiler work?

Quoted 1 line

Peter, what do you think? How about I leave this patch as is for now?

No, and I object to removing the single byte implementation too. Either remove the full arch or fix xchg() to conform. xchg() should work on all native word sizes, for SH that would be 1,2 and 4 bytes.

author maintainer
#208 lore raw

Re: [PATCH v2 31/32] sh: support a 2-byte smp_store_mb

Quoted 17 lines · Peter Zijlstra
Quoted 16 lines · Michael S. Tsirkin
Quoted 15 lines · Rich Felker
Quoted 7 lines · Michael S. Tsirkin

At the moment, xchg on sh only supports 4 and 1 byte values, so using it from smp_store_mb means attempts to store a 2 byte value using this macro fail.

And happens to be exactly what virtio drivers want to do.

Check size and fall back to a slower, but safe, WRITE_ONCE+smp_mb.

Can you please do this for size 1 as well (i.e. all sizes != 4)? If you check the source, the code for size-1 xchg in sh cmpxchg-llsc.h is completely wrong and operates on a 32-bit object at the address passed to it. This code is presently unused anyway and I plan to submit a patch to remove the size 1 case.

Quoted 6 lines
Quoted 4 lines
Quoted 1 line

Rich

Ouch. And PeterZ says I should write a 2-byte xchg in asm instead, and Fedora can't even build a full kernel for this arch at the moment :(

Does the kernel.org hosted cross compiler work?

I'll test, thanks for the hint.

Quoted 5 lines
Quoted 1 line

Peter, what do you think? How about I leave this patch as is for now?

No, and I object to removing the single byte implementation too. Either remove the full arch or fix xchg() to conform. xchg() should work on all native word sizes, for SH that would be 1,2 and 4 bytes.

Rick, maybe you could explain how is current 1 byte xchg on llsc wrong?

It does use 4 byte accesses but IIUC that is all that exists on this architecture.

Signature · 1 line
MST
maintainer
#209 lore raw

Re: [PATCH v2 31/32] sh: support a 2-byte smp_store_mb

Quoted 7 lines · Michael S. Tsirkin
Quoted 5 lines
Quoted 1 line

Peter, what do you think? How about I leave this patch as is for now?

No, and I object to removing the single byte implementation too. Either remove the full arch or fix xchg() to conform. xchg() should work on all native word sizes, for SH that would be 1,2 and 4 bytes.

Rick, maybe you could explain how is current 1 byte xchg on llsc wrong?

It doesn't seem to preserve the 3 other bytes in the word.

Quoted 2 lines

It does use 4 byte accesses but IIUC that is all that exists on this architecture.

Right, that's not a problem, look at arch/alpha/include/asm/xchg.h for example. A store to another portion of the word should make the store-conditional fail and we'll retry the loop.

The short versions should however preserve the other bytes in the word.

SH's cmpxchg() is equally incomplete and does not provide 1 and 2 byte versions.

In any case, I'm all for rm -rf arch/sh/, one less arch to worry about is always good, but ISTR some people wanting to resurrect SH:

http://old.lwn.net/Articles/647636/

Rob, Jeff, Sato-san, might I suggest you send a MAINTAINERS patch and take up an active interest in SH lest someone 'accidentally' nukes it?

#210 lore raw

Re: [PATCH v2 31/32] sh: support a 2-byte smp_store_mb

Quoted 3 lines · Peter Zijlstra

On Wed, Jan 06, 2016 at 01:52:17PM +0200, Michael S. Tsirkin wrote:
SH's cmpxchg() is equally incomplete and does not provide 1 and 2 byte versions.

We added a new cmpxchg() in j-core (smp on sh2 was not previously a thing), but still need to support the old ones.

Quoted 4 lines

In any case, I'm all for rm -rf arch/sh/, one less arch to worry about is always good, but ISTR some people wanting to resurrect SH:

http://old.lwn.net/Articles/647636/

I note that old architectures in general become interesting again as their patents expire and they're available for reimplementation as open hardware. This tends to be about when the original manufacturer loses interest, and thus people try to delete existing (working) project support before the clones can get up to speed.

(I would have thought the presence of working QEMU support would tide us over providing an easy basic regression testing environment, but people keep insisting that's not real and doesn't count. But if we can keep it 99% working until the sh4 patents expire later this year, we can add mmu and have full sh4 in hardware again with BSD VHDL.)

Quoted 2 lines

Rob, Jeff, Sato-san, might I suggest you send a MAINTAINERS patch and take up an active interest in SH lest someone 'accidentally' nukes it?

We have an active interest, we just didn't think anybody would want a MAINTAINERS patch until we had skin in the game, and as you saw from our previous patch the kernel code wasn't remotely acceptable upstream yet. (Rich has been redoing it as device tree.)

We've been talking about this offline. (The superh list is cc'd on a bunch of Renesas arm drivers because Renesas, so it has a high noise to signal ratio.)

Sato-san agreed to co-maintain but needs somebody else to take point. (I similarly want to assist but don't have the expertise to be the main guy.) That leaves Rich, who is ok with doing it but was trying to finish the device tree port of the jcore board first.

That said, if you'd ack a submission, Rich already has my Acked-by line on a maintainers patch (AND one to remove the extra cc's from the sh kernel list, and I acked Chen Gang's syscall addition patch back in
https://lkml.org/lkml/2015/6/20/193 but nobody noticed...)

Rich? Maintain please.

Rob

maintainer
#211 lore raw

Re: [PATCH v2 31/32] sh: support a 2-byte smp_store_mb

Quoted 5 lines · Rob Landley

(I would have thought the presence of working QEMU support would tide us over providing an easy basic regression testing environment, but people keep insisting that's not real and doesn't count. But if we can keep it 99% working until the sh4 patents expire later this year, we can add mmu and have full sh4 in hardware again with BSD VHDL.)

I didn't know there was a 'working' qemu for SH.

My personal 'complaint' with SH is its lack of maintainer feedback. I do full arch sweeps on semi-regular basis, and while I know in very board terms how a fair number of archs work its impossible to know everything about all 25+ we support.

#212 lore raw

Re: [PATCH v2 31/32] sh: support a 2-byte smp_store_mb

Quoted 8 lines · Peter Zijlstra
Quoted 5 lines · Rob Landley

(I would have thought the presence of working QEMU support would tide us over providing an easy basic regression testing environment, but people keep insisting that's not real and doesn't count. But if we can keep it 99% working until the sh4 patents expire later this year, we can add mmu and have full sh4 in hardware again with BSD VHDL.)

I didn't know there was a 'working' qemu for SH.

Yes, for several years now?

https://lists.gnu.org/archive/html/qemu-devel/2010-03/msg00976.html

I try to build bootable images with each new kernel, although I'm a few versions behind at the moment (this is 4.1 I think?):

wget http://landley.net/aboriginal/bin/system-image-sh4.tar.gz
tar xvzf system-image-sh4.tar.gz
cd system-image-sh4
./run-emulator.sh

There's an sh2eb one in there too, but you need a $50 FPGA board to run it (Numato Mimas v2, setup walkthrough is at http://nommu.org/jcore).

I keep meaning to poke at qemu and get their r2d board emulation to give me more than 64 megs of memory so I can do native compiles. (I have a native toolchain but building much more than "hello world" requires setting up a swap file because the board emulation only gives me one virtual disk. None of the other architectures need that...)

I'd _also_ like to get proper sh2 support into qemu (sh2 code runs under sh4 but still), but the sh4 patents expire later this year and sometime after that we want to add an MMU to the VHDL, so...

(We still want the nommu version because hard realtime is actually easier to verify without page faults, and the big product needs nanosecond accurate timestamps on stuff...)

Quoted 4 lines

My personal 'complaint' with SH is its lack of maintainer feedback. I do full arch sweeps on semi-regular basis, and while I know in very board terms how a fair number of archs work its impossible to know everything about all 25+ we support.

We (the j-core guys) have wanted to take over arch/sh maintainership for a while, we've just been trying to get the board we're working on in position to be upstreamed first. The feedback on my craptacular first effort to chip off a chunk that other people could at least reproduce against a then-current kernel was "ew" and "redo it all as device tree". So we went away again to work on that...

Meanwhile all $DAYJOB's in-house resources (at se-instruments.com) have been tied up making SMP work for a product. (Yes, sh2 SMP. A NOMMU smp system. There were some teething troubles, but it's working now. Alas, not on the above $50 FPGA because that's only got an LX9 FPGA which one SOC instance uses like 2/3 of the gates in. We're doing the SMP stuff in LX45 boards, which are crazy expensive.)

I note I just sent a Numato board to the buildroot maintainer so I can walk him through adding jcore support to buildroot. And I got toybox working nommu, and Rich added sh2 support to musl-libc... There has been activity on this arch, just not on this list due to the noise from a bunch of different Renesas arm systems and whatever "arm/shmobile" is. ("Not superh or jcore compatible", that's all I know...)

Rob

#213 lore raw

Re: [PATCH v2 31/32] sh: support a 2-byte smp_store_mb

In reply to Rob Landley
Quoted 4 lines · Rob Landley

That said, if you'd ack a submission, Rich already has my Acked-by line on a maintainers patch (AND one to remove the extra cc's from the sh kernel list, and I acked Chen Gang's syscall addition patch back in
https://lkml.org/lkml/2015/6/20/193 but nobody noticed...)

If you want patches for orphan architectures to be picked up, you should CC Andrew Morton...

Gr{oetje,eeting}s,

Geert

--
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected]

In personal conversations with technical people, I call myself a hacker. But when I'm talking to journalists I just say "programmer" or something like that.
-- Linus Torvalds

maintainer
#214 lore raw

Re: [PATCH v2 31/32] sh: support a 2-byte smp_store_mb

In reply to Peter Zijlstra
Quoted 11 lines · Peter Zijlstra
Quoted 7 lines · Michael S. Tsirkin
Quoted 5 lines
Quoted 1 line

Peter, what do you think? How about I leave this patch as is for now?

No, and I object to removing the single byte implementation too. Either remove the full arch or fix xchg() to conform. xchg() should work on all native word sizes, for SH that would be 1,2 and 4 bytes.

Rick, maybe you could explain how is current 1 byte xchg on llsc wrong?

It doesn't seem to preserve the 3 other bytes in the word.

Quoted 8 lines
Quoted 2 lines

It does use 4 byte accesses but IIUC that is all that exists on this architecture.

Right, that's not a problem, look at arch/alpha/include/asm/xchg.h for example. A store to another portion of the word should make the store-conditional fail and we'll retry the loop.

The short versions should however preserve the other bytes in the word.

Indeed. Also, accesses must be aligned, so the asm needs to round down to an aligned address and perform a correct read-modify-write on it, placing the new byte in the correct offset in the word.

Alternatively (my preference) this logic can be impemented in C as a wrapper around the 32-bit cmpxchg. I think this is less error-prone and it can be shared between the multiple sh cmpxchg back-ends, including the new cas.l one we need for J2.

Quoted 10 lines

SH's cmpxchg() is equally incomplete and does not provide 1 and 2 byte versions.

In any case, I'm all for rm -rf arch/sh/, one less arch to worry about is always good, but ISTR some people wanting to resurrect SH:

http://old.lwn.net/Articles/647636/

Rob, Jeff, Sato-san, might I suggest you send a MAINTAINERS patch and take up an active interest in SH lest someone 'accidentally' nukes it?

We're in the process of preparing such a proposal right now. That current intent is that Sato-san and I will co-maintain arch/sh. We'll include more details about motivation, proposed development direction, existing work to be merged, etc. in that proposal.

Rich

author maintainer
#215 lore raw

Re: [PATCH v2 31/32] sh: support a 2-byte smp_store_mb

Quoted 41 lines · Rich Felker
Quoted 11 lines · Peter Zijlstra
Quoted 7 lines · Michael S. Tsirkin
Quoted 5 lines
Quoted 1 line

Peter, what do you think? How about I leave this patch as is for now?

No, and I object to removing the single byte implementation too. Either remove the full arch or fix xchg() to conform. xchg() should work on all native word sizes, for SH that would be 1,2 and 4 bytes.

Rick, maybe you could explain how is current 1 byte xchg on llsc wrong?

It doesn't seem to preserve the 3 other bytes in the word.

Quoted 8 lines
Quoted 2 lines

It does use 4 byte accesses but IIUC that is all that exists on this architecture.

Right, that's not a problem, look at arch/alpha/include/asm/xchg.h for example. A store to another portion of the word should make the store-conditional fail and we'll retry the loop.

The short versions should however preserve the other bytes in the word.

Indeed. Also, accesses must be aligned, so the asm needs to round down to an aligned address and perform a correct read-modify-write on it, placing the new byte in the correct offset in the word.

Alternatively (my preference) this logic can be impemented in C as a wrapper around the 32-bit cmpxchg. I think this is less error-prone and it can be shared between the multiple sh cmpxchg back-ends, including the new cas.l one we need for J2.

Quoted 10 lines

SH's cmpxchg() is equally incomplete and does not provide 1 and 2 byte versions.

In any case, I'm all for rm -rf arch/sh/, one less arch to worry about is always good, but ISTR some people wanting to resurrect SH:

http://old.lwn.net/Articles/647636/

Rob, Jeff, Sato-san, might I suggest you send a MAINTAINERS patch and take up an active interest in SH lest someone 'accidentally' nukes it?

Quoted 6 lines

We're in the process of preparing such a proposal right now. That current intent is that Sato-san and I will co-maintain arch/sh. We'll include more details about motivation, proposed development direction, existing work to be merged, etc. in that proposal.

Rich

Well I'd like to be able to make progress with generic arch cleanups meanwhile.

Could you quickly write a version of 1 and 2 byte xchg that works so I can include it?

Thanks!

Signature · 1 line
MST
maintainer
#216 lore raw

Re: [PATCH v2 31/32] sh: support a 2-byte smp_store_mb

Quoted 47 lines · Michael S. Tsirkin
Quoted 38 lines · Rich Felker
Quoted 11 lines · Peter Zijlstra
Quoted 7 lines · Michael S. Tsirkin
Quoted 5 lines
Quoted 1 line

Peter, what do you think? How about I leave this patch as is for now?

No, and I object to removing the single byte implementation too. Either remove the full arch or fix xchg() to conform. xchg() should work on all native word sizes, for SH that would be 1,2 and 4 bytes.

Rick, maybe you could explain how is current 1 byte xchg on llsc wrong?

It doesn't seem to preserve the 3 other bytes in the word.

Quoted 8 lines
Quoted 2 lines

It does use 4 byte accesses but IIUC that is all that exists on this architecture.

Right, that's not a problem, look at arch/alpha/include/asm/xchg.h for example. A store to another portion of the word should make the store-conditional fail and we'll retry the loop.

The short versions should however preserve the other bytes in the word.

Indeed. Also, accesses must be aligned, so the asm needs to round down to an aligned address and perform a correct read-modify-write on it, placing the new byte in the correct offset in the word.

Alternatively (my preference) this logic can be impemented in C as a wrapper around the 32-bit cmpxchg. I think this is less error-prone and it can be shared between the multiple sh cmpxchg back-ends, including the new cas.l one we need for J2.

Quoted 8 lines

SH's cmpxchg() is equally incomplete and does not provide 1 and 2 byte versions.

In any case, I'm all for rm -rf arch/sh/, one less arch to worry about is always good, but ISTR some people wanting to resurrect SH:

http://old.lwn.net/Articles/647636/

Quoted 7 lines
Quoted 2 lines

Rob, Jeff, Sato-san, might I suggest you send a MAINTAINERS patch and take up an active interest in SH lest someone 'accidentally' nukes it?

We're in the process of preparing such a proposal right now. That current intent is that Sato-san and I will co-maintain arch/sh. We'll include more details about motivation, proposed development direction, existing work to be merged, etc. in that proposal.

Quoted 5 lines

Well I'd like to be able to make progress with generic arch cleanups meanwhile.

Could you quickly write a version of 1 and 2 byte xchg that works so I can include it?

Here are quick, untested generic ones:

static inline unsigned long xchg_u8(volatile u8 *m, unsigned long val)

{
	u32 old;
	unsigned long offset = (unsigned long)m & 3;
	volatile u32 *w = (volatile u32 *)(m - offset);
	union { u32 w; u8 b[4]; } u;
	do {
		old = u.w = *w;
		result = w.b[offset];
		w.b[offset] = val;
	} while (cmpxchg(w, old, u.w) != old);
	return result;
}

static inline unsigned long xchg_u16(volatile u16 *m, unsigned long val)

{
	u32 old;
	unsigned long result;
	unsigned long offset = ((unsigned long)m & 3) >> 1;
	volatile u32 *w = (volatile u32 *)(m - offset);
	union { u32 w; u16 h[2]; } u;
	do {
		old = u.w = *w;
		result = w.h[offset];
		w.h[offset] = val;
	} while (cmpxchg(w, old, u.w) != old);
	return result;
}

It would be nice to have these in asm-generic for archs which don't define their own versions rather than having cruft like this repeated per-arch. Strictly speaking, the volatile u32 used to access the 32-bit word containing the u8 or u16 should be __attribute__((__may_alias__)) too. Is there an existing kernel type for a "may_alias u32" or should it perhaps be added?

Rich

maintainer
#217 lore raw

Re: [PATCH v2 31/32] sh: support a 2-byte smp_store_mb

Quoted 3 lines · Rich Felker

It would be nice to have these in asm-generic for archs which don't define their own versions rather than having cruft like this repeated per-arch.

Maybe, but I'm not sure how many archs would indeed suffer this problem, so far I'm only aware of Alpha and SH that do not have short atomic ops.

Quoted 4 lines

Strictly speaking, the volatile u32 used to access the 32-bit word containing the u8 or u16 should be __attribute__((__may_alias__)) too. Is there an existing kernel type for a "may_alias u32" or should it perhaps be added?

The kernel does -fno-strict-aliasing because the C aliasing rules are crap (TM) :-), so I suspect we do not need the alias attribute here.

maintainer
#218 lore raw

Re: [PATCH v2 31/32] sh: support a 2-byte smp_store_mb

Quoted 7 lines · Peter Zijlstra
Quoted 3 lines · Rich Felker

It would be nice to have these in asm-generic for archs which don't define their own versions rather than having cruft like this repeated per-arch.

Maybe, but I'm not sure how many archs would indeed suffer this problem, so far I'm only aware of Alpha and SH that do not have short atomic ops.

Apparently original armv6 (non-k) lacked u8 and u16 variants of ldrex/strex. I'm pretty sure or1k also lacks them, and mips, microblaze, and some powerpc versions might too. Not sure about risc-v.

Quoted 7 lines
Quoted 4 lines

Strictly speaking, the volatile u32 used to access the 32-bit word containing the u8 or u16 should be __attribute__((__may_alias__)) too. Is there an existing kernel type for a "may_alias u32" or should it perhaps be added?

The kernel does -fno-strict-aliasing because the C aliasing rules are crap (TM) :-), so I suspect we do not need the alias attribute here.

I suspect working on the kernel I'm going to have to get used to getting "corrected" for writing proper C... ;-)

Rich

author maintainer
#219 lore raw

Re: [PATCH v2 31/32] sh: support a 2-byte smp_store_mb

In reply to Rich Felker
Quoted 93 lines · Rich Felker
Quoted 47 lines · Michael S. Tsirkin
Quoted 38 lines · Rich Felker
Quoted 11 lines · Peter Zijlstra
Quoted 7 lines · Michael S. Tsirkin
Quoted 5 lines
Quoted 1 line

Peter, what do you think? How about I leave this patch as is for now?

No, and I object to removing the single byte implementation too. Either remove the full arch or fix xchg() to conform. xchg() should work on all native word sizes, for SH that would be 1,2 and 4 bytes.

Rick, maybe you could explain how is current 1 byte xchg on llsc wrong?

It doesn't seem to preserve the 3 other bytes in the word.

Quoted 8 lines
Quoted 2 lines

It does use 4 byte accesses but IIUC that is all that exists on this architecture.

Right, that's not a problem, look at arch/alpha/include/asm/xchg.h for example. A store to another portion of the word should make the store-conditional fail and we'll retry the loop.

The short versions should however preserve the other bytes in the word.

Indeed. Also, accesses must be aligned, so the asm needs to round down to an aligned address and perform a correct read-modify-write on it, placing the new byte in the correct offset in the word.

Alternatively (my preference) this logic can be impemented in C as a wrapper around the 32-bit cmpxchg. I think this is less error-prone and it can be shared between the multiple sh cmpxchg back-ends, including the new cas.l one we need for J2.

Quoted 8 lines

SH's cmpxchg() is equally incomplete and does not provide 1 and 2 byte versions.

In any case, I'm all for rm -rf arch/sh/, one less arch to worry about is always good, but ISTR some people wanting to resurrect SH:

http://old.lwn.net/Articles/647636/

Quoted 7 lines
Quoted 2 lines

Rob, Jeff, Sato-san, might I suggest you send a MAINTAINERS patch and take up an active interest in SH lest someone 'accidentally' nukes it?

We're in the process of preparing such a proposal right now. That current intent is that Sato-san and I will co-maintain arch/sh. We'll include more details about motivation, proposed development direction, existing work to be merged, etc. in that proposal.

Quoted 5 lines

Well I'd like to be able to make progress with generic arch cleanups meanwhile.

Could you quickly write a version of 1 and 2 byte xchg that works so I can include it?

Here are quick, untested generic ones:

static inline unsigned long xchg_u8(volatile u8 *m, unsigned long val)

{
	u32 old;
	unsigned long offset = (unsigned long)m & 3;
	volatile u32 *w = (volatile u32 *)(m - offset);
	union { u32 w; u8 b[4]; } u;
	do {
		old = u.w = *w;
		result = w.b[offset];
		w.b[offset] = val;
	} while (cmpxchg(w, old, u.w) != old);
	return result;
}

static inline unsigned long xchg_u16(volatile u16 *m, unsigned long val)

{
	u32 old;
	unsigned long result;
	unsigned long offset = ((unsigned long)m & 3) >> 1;
	volatile u32 *w = (volatile u32 *)(m - offset);
	union { u32 w; u16 h[2]; } u;
	do {
		old = u.w = *w;
		result = w.h[offset];
		w.h[offset] = val;
	} while (cmpxchg(w, old, u.w) != old);
	return result;
}

It would be nice to have these in asm-generic for archs which don't define their own versions rather than having cruft like this repeated per-arch. Strictly speaking, the volatile u32 used to access the 32-bit word containing the u8 or u16 should be __attribute__((__may_alias__)) too.
Is there an existing kernel type
for a "may_alias u32" or should it perhaps be added?

Quoted 1 line

Rich

I'm inclined to write this using shifts, this way there's no duplication between 1 and 2 byte variants. And READ_ONCE is better than volatile IMHO.

arch/sh/include/asm/cmpxchg.h +24 -1
index 85c97b18..a858879 100644
@@ -8,6 +8,7 @@
 
 #include <linux/compiler.h>
 #include <linux/types.h>
+#include <asm/byteorder.h>
 
 #if defined(CONFIG_GUSA_RB)
 #include <asm/cmpxchg-grb.h>
@@ -19,6 +20,26 @@
 
 extern void __xchg_called_with_bad_pointer(void);
 
+static inline u32 __xchg_cmpxchg(void *ptr, u32 x, int size)
+{
+	int off = (unsigned long)ptr % sizeof(u32);
+	u32 *p = ptr - off;
+	int bitoff = __BYTE_ORDER = __BIG_ENDIAN ?
+		((sizeof(u32) - 1 - off) * BITS_PER_BYTE) :
+		(off * BITS_PER_BYTE);
+	u32 bitmask = ((0x1 << size * BITS_PER_BYTE) - 1) << bitoff;
+	u32 oldv, newv;
+	u32 ret;
+
+	do {
+		oldv = READ_ONCE(*p);
+		ret = (oldv & bitmask) >> bitoff;
+		newv = (oldv & ~bitmask) | (x << bitoff);
+	} while(cmpxchg(p, oldv, newv) != oldv);
+
+	return ret;
+}
+
 #define __xchg(ptr, x, size)				\
 ({							\
 	unsigned long __xchg__res;			\
@@ -27,8 +48,10 @@ extern void __xchg_called_with_bad_pointer(void);
 	case 4:						\
 		__xchg__res = xchg_u32(__xchg_ptr, x);	\
 		break;					\
+	case 2:						\
 	case 1:						\
-		__xchg__res = xchg_u8(__xchg_ptr, x);	\
+		__xchg__res = __xchg_cmpxchg(__xchg_ptr,\
+					     x, size);	\
 		break;					\
 	default:					\
 		__xchg_called_with_bad_pointer();	\

Testing the above now.

Signature · 1 line
MST
author maintainer
#220 lore raw

Re: [PATCH v2 31/32] sh: support a 2-byte smp_store_mb

In reply to Rich Felker
Quoted 85 lines · Rich Felker
Quoted 47 lines · Michael S. Tsirkin
Quoted 38 lines · Rich Felker
Quoted 11 lines · Peter Zijlstra
Quoted 7 lines · Michael S. Tsirkin
Quoted 5 lines
Quoted 1 line

Peter, what do you think? How about I leave this patch as is for now?

No, and I object to removing the single byte implementation too. Either remove the full arch or fix xchg() to conform. xchg() should work on all native word sizes, for SH that would be 1,2 and 4 bytes.

Rick, maybe you could explain how is current 1 byte xchg on llsc wrong?

It doesn't seem to preserve the 3 other bytes in the word.

Quoted 8 lines
Quoted 2 lines

It does use 4 byte accesses but IIUC that is all that exists on this architecture.

Right, that's not a problem, look at arch/alpha/include/asm/xchg.h for example. A store to another portion of the word should make the store-conditional fail and we'll retry the loop.

The short versions should however preserve the other bytes in the word.

Indeed. Also, accesses must be aligned, so the asm needs to round down to an aligned address and perform a correct read-modify-write on it, placing the new byte in the correct offset in the word.

Alternatively (my preference) this logic can be impemented in C as a wrapper around the 32-bit cmpxchg. I think this is less error-prone and it can be shared between the multiple sh cmpxchg back-ends, including the new cas.l one we need for J2.

Quoted 8 lines

SH's cmpxchg() is equally incomplete and does not provide 1 and 2 byte versions.

In any case, I'm all for rm -rf arch/sh/, one less arch to worry about is always good, but ISTR some people wanting to resurrect SH:

http://old.lwn.net/Articles/647636/

Quoted 7 lines
Quoted 2 lines

Rob, Jeff, Sato-san, might I suggest you send a MAINTAINERS patch and take up an active interest in SH lest someone 'accidentally' nukes it?

We're in the process of preparing such a proposal right now. That current intent is that Sato-san and I will co-maintain arch/sh. We'll include more details about motivation, proposed development direction, existing work to be merged, etc. in that proposal.

Quoted 5 lines

Well I'd like to be able to make progress with generic arch cleanups meanwhile.

Could you quickly write a version of 1 and 2 byte xchg that works so I can include it?

Here are quick, untested generic ones:

static inline unsigned long xchg_u8(volatile u8 *m, unsigned long val)

{
	u32 old;
	unsigned long offset = (unsigned long)m & 3;
	volatile u32 *w = (volatile u32 *)(m - offset);
	union { u32 w; u8 b[4]; } u;
	do {
		old = u.w = *w;
		result = w.b[offset];
		w.b[offset] = val;
	} while (cmpxchg(w, old, u.w) != old);
	return result;
}

static inline unsigned long xchg_u16(volatile u16 *m, unsigned long val)

{
	u32 old;
	unsigned long result;
	unsigned long offset = ((unsigned long)m & 3) >> 1;
	volatile u32 *w = (volatile u32 *)(m - offset);
	union { u32 w; u16 h[2]; } u;
	do {
		old = u.w = *w;
		result = w.h[offset];
		w.h[offset] = val;
	} while (cmpxchg(w, old, u.w) != old);
	return result;
}
Quoted 8 lines

It would be nice to have these in asm-generic for archs which don't define their own versions rather than having cruft like this repeated per-arch. Strictly speaking, the volatile u32 used to access the 32-bit word containing the u8 or u16 should be __attribute__((__may_alias__)) too. Is there an existing kernel type for a "may_alias u32" or should it perhaps be added?

Rich

BTW this seems suboptimal for grb and irq variants which apparently can do things correctly.

maintainer
#221 lore raw

Re: [PATCH v2 31/32] sh: support a 2-byte smp_store_mb

Quoted 93 lines · Michael S. Tsirkin
Quoted 92 lines · Rich Felker
Quoted 47 lines · Michael S. Tsirkin
Quoted 38 lines · Rich Felker
Quoted 11 lines · Peter Zijlstra
Quoted 7 lines · Michael S. Tsirkin
Quoted 5 lines
Quoted 1 line

Peter, what do you think? How about I leave this patch as is for now?

No, and I object to removing the single byte implementation too. Either remove the full arch or fix xchg() to conform. xchg() should work on all native word sizes, for SH that would be 1,2 and 4 bytes.

Rick, maybe you could explain how is current 1 byte xchg on llsc wrong?

It doesn't seem to preserve the 3 other bytes in the word.

Quoted 8 lines
Quoted 2 lines

It does use 4 byte accesses but IIUC that is all that exists on this architecture.

Right, that's not a problem, look at arch/alpha/include/asm/xchg.h for example. A store to another portion of the word should make the store-conditional fail and we'll retry the loop.

The short versions should however preserve the other bytes in the word.

Indeed. Also, accesses must be aligned, so the asm needs to round down to an aligned address and perform a correct read-modify-write on it, placing the new byte in the correct offset in the word.

Alternatively (my preference) this logic can be impemented in C as a wrapper around the 32-bit cmpxchg. I think this is less error-prone and it can be shared between the multiple sh cmpxchg back-ends, including the new cas.l one we need for J2.

Quoted 8 lines

SH's cmpxchg() is equally incomplete and does not provide 1 and 2 byte versions.

In any case, I'm all for rm -rf arch/sh/, one less arch to worry about is always good, but ISTR some people wanting to resurrect SH:

http://old.lwn.net/Articles/647636/

Quoted 7 lines
Quoted 2 lines

Rob, Jeff, Sato-san, might I suggest you send a MAINTAINERS patch and take up an active interest in SH lest someone 'accidentally' nukes it?

We're in the process of preparing such a proposal right now. That current intent is that Sato-san and I will co-maintain arch/sh. We'll include more details about motivation, proposed development direction, existing work to be merged, etc. in that proposal.

Quoted 5 lines

Well I'd like to be able to make progress with generic arch cleanups meanwhile.

Could you quickly write a version of 1 and 2 byte xchg that works so I can include it?

Here are quick, untested generic ones:

static inline unsigned long xchg_u8(volatile u8 *m, unsigned long val)

{
	u32 old;
	unsigned long offset = (unsigned long)m & 3;
	volatile u32 *w = (volatile u32 *)(m - offset);
	union { u32 w; u8 b[4]; } u;
	do {
		old = u.w = *w;
		result = w.b[offset];
		w.b[offset] = val;
	} while (cmpxchg(w, old, u.w) != old);
	return result;
}

static inline unsigned long xchg_u16(volatile u16 *m, unsigned long val)

{
	u32 old;
	unsigned long result;
	unsigned long offset = ((unsigned long)m & 3) >> 1;
	volatile u32 *w = (volatile u32 *)(m - offset);
	union { u32 w; u16 h[2]; } u;
	do {
		old = u.w = *w;
		result = w.h[offset];
		w.h[offset] = val;
	} while (cmpxchg(w, old, u.w) != old);
	return result;
}

It would be nice to have these in asm-generic for archs which don't define their own versions rather than having cruft like this repeated per-arch. Strictly speaking, the volatile u32 used to access the 32-bit word containing the u8 or u16 should be __attribute__((__may_alias__)) too. Is there an existing kernel type for a "may_alias u32" or should it perhaps be added?

Quoted 4 lines
Quoted 1 line

Rich

BTW this seems suboptimal for grb and irq variants which apparently can do things correctly.

In principle I agree, but u8/u16 xchg is mostly unused (completely unused in my builds) and unlikely to matter to performance. Also, the irq variant is only for the original sh2 which is not even produced anymore afaik. Our reimplementation of the sh2 ISA, the J2, has a cas.l instruction that will be used instead because it supports SMP where interrupt masking is insufficient to achieve atomicity.

Rich

author maintainer
#222 lore raw

Re: [PATCH v2 31/32] sh: support a 2-byte smp_store_mb

Quoted 12 lines
Quoted 11 lines
Quoted 8 lines

It would be nice to have these in asm-generic for archs which don't define their own versions rather than having cruft like this repeated per-arch. Strictly speaking, the volatile u32 used to access the 32-bit word containing the u8 or u16 should be __attribute__((__may_alias__)) too. Is there an existing kernel type for a "may_alias u32" or should it perhaps be added?

Rich

BTW this seems suboptimal for grb and irq variants which apparently can do things correctly.

Quoted 8 lines

In principle I agree, but u8/u16 xchg is mostly unused (completely unused in my builds) and unlikely to matter to performance. Also, the irq variant is only for the original sh2 which is not even produced anymore afaik. Our reimplementation of the sh2 ISA, the J2, has a cas.l instruction that will be used instead because it supports SMP where interrupt masking is insufficient to achieve atomicity.

Rich

Since it looks like there will soon be active maintainers for this arch, I think it's best if I make the minimal possible changes and then you guys can rewrite it any way you like, drop irq variant or whatever.

The minimal change is probably the below code but
the grb variant is just copy paste from xchg_u8
with a minor tweak -
can you pls confirm it looks right?

I tested the llsc code on ppc and x86 and since it's portable I know the logic is correct there.

Will post v3 with this included but would appreciate your input first.

---->
sh: support 1 and 2 byte xchg

This completes the xchg implementation for sh architecture. Note: The llsc variant is tricky since this only supports 4 byte atomics, the existing implementation of 1 byte xchg is wrong: we need to do a 4 byte cmpxchg and retry if any bytes changed meanwhile.

Write this in C for clarity.

---->

arch/sh/include/asm/cmpxchg-grb.h +22 -0
index f848dec..2ed557b 100644
@@ -23,6 +23,28 @@ static inline unsigned long xchg_u32(volatile u32 *m, unsigned long val)
 	return retval;
 }
 
+static inline unsigned long xchg_u16(volatile u16 *m, unsigned long val)
+{
+	unsigned long retval;
+
+	__asm__ __volatile__ (
+		"   .align  2             \n\t"
+		"   mova    1f,   r0      \n\t" /* r0 = end point */
+		"   mov    r15,   r1      \n\t" /* r1 = saved sp */
+		"   mov    #-6,   r15     \n\t" /* LOGIN */
+		"   mov.w  @%1,   %0      \n\t" /* load  old value */
+		"   extu.w  %0,   %0      \n\t" /* extend as unsigned */
+		"   mov.w   %2,   @%1     \n\t" /* store new value */
+		"1: mov     r1,   r15     \n\t" /* LOGOUT */
+		: "=&r" (retval),
+		  "+r"  (m),
+		  "+r"  (val)		/* inhibit r15 overloading */
+		:
+		: "memory" , "r0", "r1");
+
+	return retval;
+}
+
 static inline unsigned long xchg_u8(volatile u8 *m, unsigned long val)
 {
 	unsigned long retval;
arch/sh/include/asm/cmpxchg-irq.h +11 -0
index bd11f63..f888772 100644
@@ -14,6 +14,17 @@ static inline unsigned long xchg_u32(volatile u32 *m, unsigned long val)
 	return retval;
 }
 
+static inline unsigned long xchg_u16(volatile u16 *m, unsigned long val)
+{
+	unsigned long flags, retval;
+
+	local_irq_save(flags);
+	retval = *m;
+	*m = val;
+	local_irq_restore(flags);
+	return retval;
+}
+
 static inline unsigned long xchg_u8(volatile u8 *m, unsigned long val)
 {
 	unsigned long flags, retval;
arch/sh/include/asm/cmpxchg-llsc.h +33 -22

72 lines not on this page · raw message

arch/sh/include/asm/cmpxchg.h +3 -0
index 85c97b18..5225916 100644
@@ -27,6 +27,9 @@ extern void __xchg_called_with_bad_pointer(void);
 	case 4:						\
 		__xchg__res = xchg_u32(__xchg_ptr, x);	\
 		break;					\
+	case 2:						\
+		__xchg__res = xchg_u16(__xchg_ptr, x);	\
+		break;					\
 	case 1:						\
 		__xchg__res = xchg_u8(__xchg_ptr, x);	\
 		break;					\
maintainer
#223 lore raw

Re: [PATCH v2 31/32] sh: support a 2-byte smp_store_mb

Quoted 26 lines · Michael S. Tsirkin
Quoted 12 lines
Quoted 11 lines
Quoted 8 lines

It would be nice to have these in asm-generic for archs which don't define their own versions rather than having cruft like this repeated per-arch. Strictly speaking, the volatile u32 used to access the 32-bit word containing the u8 or u16 should be __attribute__((__may_alias__)) too. Is there an existing kernel type for a "may_alias u32" or should it perhaps be added?

Rich

BTW this seems suboptimal for grb and irq variants which apparently can do things correctly.

Quoted 8 lines

In principle I agree, but u8/u16 xchg is mostly unused (completely unused in my builds) and unlikely to matter to performance. Also, the irq variant is only for the original sh2 which is not even produced anymore afaik. Our reimplementation of the sh2 ISA, the J2, has a cas.l instruction that will be used instead because it supports SMP where interrupt masking is insufficient to achieve atomicity.

Rich

Since it looks like there will soon be active maintainers for this arch, I think it's best if I make the minimal possible changes and then you guys can rewrite it any way you like, drop irq variant or whatever.

Quoted 4 lines

The minimal change is probably the below code but
the grb variant is just copy paste from xchg_u8
with a minor tweak -
can you pls confirm it looks right?

I haven't had a chance to test it, but I don't see anything obviously wrong with it.

Quoted 2 lines

I tested the llsc code on ppc and x86 and since it's portable I know the logic is correct there.

Sounds good. Since it will also be needed for the cas.l variant I'd rather have this in the main asm/cmpxchg.h where it can be shared if you see an easy way to do that now, but if not I can take care of it later when merging cmpxchg-cas.h. Perhaps just putting __xchg_cmpxchg in the main asm/cmpxchg.h would suffice so that only the thin wrappers need to be duplicated. Ideally it could even be moved outside of the arch asm headers, but then there might be annoying header ordering issues to deal with.

Quoted 2 lines

Will post v3 with this included but would appreciate your input first.

Go ahead. Thanks!

Rich

author maintainer
#224 lore raw

Re: [PATCH v2 31/32] sh: support a 2-byte smp_store_mb

Quoted 38 lines · Rich Felker
Quoted 26 lines · Michael S. Tsirkin
Quoted 12 lines
Quoted 11 lines
Quoted 8 lines

It would be nice to have these in asm-generic for archs which don't define their own versions rather than having cruft like this repeated per-arch. Strictly speaking, the volatile u32 used to access the 32-bit word containing the u8 or u16 should be __attribute__((__may_alias__)) too. Is there an existing kernel type for a "may_alias u32" or should it perhaps be added?

Rich

BTW this seems suboptimal for grb and irq variants which apparently can do things correctly.

Quoted 8 lines

In principle I agree, but u8/u16 xchg is mostly unused (completely unused in my builds) and unlikely to matter to performance. Also, the irq variant is only for the original sh2 which is not even produced anymore afaik. Our reimplementation of the sh2 ISA, the J2, has a cas.l instruction that will be used instead because it supports SMP where interrupt masking is insufficient to achieve atomicity.

Rich

Since it looks like there will soon be active maintainers for this arch, I think it's best if I make the minimal possible changes and then you guys can rewrite it any way you like, drop irq variant or whatever.

Quoted 4 lines

The minimal change is probably the below code but
the grb variant is just copy paste from xchg_u8
with a minor tweak -
can you pls confirm it looks right?

I haven't had a chance to test it, but I don't see anything obviously wrong with it.

Quoted 2 lines

I tested the llsc code on ppc and x86 and since it's portable I know the logic is correct there.

Quoted 9 lines

Sounds good. Since it will also be needed for the cas.l variant I'd rather have this in the main asm/cmpxchg.h where it can be shared if you see an easy way to do that now, but if not I can take care of it later when merging cmpxchg-cas.h. Perhaps just putting __xchg_cmpxchg in the main asm/cmpxchg.h would suffice so that only the thin wrappers need to be duplicated.
Ideally it could even be moved outside of the
arch asm headers, but then there might be annoying header ordering issues to deal with.

Well it isn't possible to put it in cmpxchg.h because you get into annoying ordering issues: __cmpxchg_u32 is needed
so it has to come after the headers, but the wrappers must come before the headers.

I put it in a header by itself. This way it's easy to reuse, and even the thin wrappers won't have to be duplicated.

Quoted 7 lines
Quoted 3 lines

Will post v3 with this included but would appreciate your input first.

Go ahead. Thanks!

Rich

author maintainer
#225 lore raw

Re: [PATCH v2 31/32] sh: support a 2-byte smp_store_mb

In reply to Rich Felker
Quoted 21 lines · Rich Felker
Quoted 11 lines · Peter Zijlstra
Quoted 7 lines · Michael S. Tsirkin
Quoted 5 lines
Quoted 1 line

Peter, what do you think? How about I leave this patch as is for now?

No, and I object to removing the single byte implementation too. Either remove the full arch or fix xchg() to conform. xchg() should work on all native word sizes, for SH that would be 1,2 and 4 bytes.

Rick, maybe you could explain how is current 1 byte xchg on llsc wrong?

It doesn't seem to preserve the 3 other bytes in the word.

Quoted 8 lines
Quoted 2 lines

It does use 4 byte accesses but IIUC that is all that exists on this architecture.

Right, that's not a problem, look at arch/alpha/include/asm/xchg.h for example. A store to another portion of the word should make the store-conditional fail and we'll retry the loop.

The short versions should however preserve the other bytes in the word.

Quoted 8 lines

Indeed. Also, accesses must be aligned, so the asm needs to round down to an aligned address and perform a correct read-modify-write on it, placing the new byte in the correct offset in the word.

Alternatively (my preference) this logic can be impemented in C as a wrapper around the 32-bit cmpxchg. I think this is less error-prone and it can be shared between the multiple sh cmpxchg back-ends, including the new cas.l one we need for J2.

Sounds much more reasonable.

Quoted 17 lines
Quoted 10 lines

SH's cmpxchg() is equally incomplete and does not provide 1 and 2 byte versions.

In any case, I'm all for rm -rf arch/sh/, one less arch to worry about is always good, but ISTR some people wanting to resurrect SH:

http://old.lwn.net/Articles/647636/

Rob, Jeff, Sato-san, might I suggest you send a MAINTAINERS patch and take up an active interest in SH lest someone 'accidentally' nukes it?

We're in the process of preparing such a proposal right now. That current intent is that Sato-san and I will co-maintain arch/sh. We'll include more details about motivation, proposed development direction, existing work to be merged, etc. in that proposal.

Rich

author maintainer
#226 lore raw

PATCH v2 31/32 sh: support a 2-byte smp_store_mb

In reply to Michael S. Tsirkin

At the moment, xchg on sh only supports 4 and 1 byte values, so using it from smp_store_mb means attempts to store a 2 byte value using this macro fail.

And happens to be exactly what virtio drivers want to do.

Check size and fall back to a slower, but safe, WRITE_ONCE+smp_mb.

---
 arch/sh/include/asm/barrier.h | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)
arch/sh/include/asm/barrier.h +9 -1
index f887c64..0cc5735 100644
@@ -32,7 +32,15 @@
 #define ctrl_barrier()	__asm__ __volatile__ ("nop;nop;nop;nop;nop;nop;nop;nop")
 #endif
 
-#define __smp_store_mb(var, value) do { (void)xchg(&var, value); } while (0)
+#define __smp_store_mb(var, value) do { \
+	if (sizeof(var) != 4 && sizeof(var) != 1) { \
+		 WRITE_ONCE(var, value); \
+		__smp_mb(); \
+	} else { \
+		(void)xchg(&var, value);  \
+	} \
+} while (0)
+
 #define smp_store_mb(var, value) __smp_store_mb(var, value)
 
 #include <asm-generic/barrier.h>
Signature · 1 line
MST
author maintainer
#227 lore raw

PATCH v2 31/32 sh: support a 2-byte smp_store_mb

In reply to Michael S. Tsirkin

At the moment, xchg on sh only supports 4 and 1 byte values, so using it from smp_store_mb means attempts to store a 2 byte value using this macro fail.

And happens to be exactly what virtio drivers want to do.

Check size and fall back to a slower, but safe, WRITE_ONCE+smp_mb.

---
 arch/sh/include/asm/barrier.h | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)
arch/sh/include/asm/barrier.h +9 -1
index f887c64..0cc5735 100644
@@ -32,7 +32,15 @@
 #define ctrl_barrier()	__asm__ __volatile__ ("nop;nop;nop;nop;nop;nop;nop;nop")
 #endif
 
-#define __smp_store_mb(var, value) do { (void)xchg(&var, value); } while (0)
+#define __smp_store_mb(var, value) do { \
+	if (sizeof(var) != 4 && sizeof(var) != 1) { \
+		 WRITE_ONCE(var, value); \
+		__smp_mb(); \
+	} else { \
+		(void)xchg(&var, value);  \
+	} \
+} while (0)
+
 #define smp_store_mb(var, value) __smp_store_mb(var, value)
 
 #include <asm-generic/barrier.h>
Signature · 1 line
MST
author maintainer
#228 lore raw

PATCH v2 32/32 virtio_ring: use virt_store_mb

In reply to Michael S. Tsirkin

We need a full barrier after writing out event index, using virt_store_mb there seems better than open-coding. As usual, we need a wrapper to account for strong barriers.

It's tempting to use this in vhost as well, for that, we'll need a variant of smp_store_mb that works on __user pointers.

---
 include/linux/virtio_ring.h  | 12 ++++++++++++
 drivers/virtio/virtio_ring.c | 15 +++++++++------
 2 files changed, 21 insertions(+), 6 deletions(-)
include/linux/virtio_ring.h +12 -0
index f3fa55b..3a74d91 100644
@@ -45,6 +45,18 @@ static inline void virtio_wmb(bool weak_barriers)
 		wmb();
 }
 
+static inline void virtio_store_mb(bool weak_barriers,
+				   __virtio16 *p, __virtio16 v)
+{
+	if (weak_barriers)
+		virt_store_mb(*p, v);
+	else
+	{
+		WRITE_ONCE(*p, v);
+		mb();
+	}
+}
+
 struct virtio_device;
 struct virtqueue;
 
drivers/virtio/virtio_ring.c +9 -6
index ee663c4..e12e385 100644
@@ -517,10 +517,10 @@ void *virtqueue_get_buf(struct virtqueue *_vq, unsigned int *len)
 	/* If we expect an interrupt for the next entry, tell host
 	 * by writing event index and flush out the write before
 	 * the read in the next get_buf call. */
-	if (!(vq->avail_flags_shadow & VRING_AVAIL_F_NO_INTERRUPT)) {
-		vring_used_event(&vq->vring) = cpu_to_virtio16(_vq->vdev, vq->last_used_idx);
-		virtio_mb(vq->weak_barriers);
-	}
+	if (!(vq->avail_flags_shadow & VRING_AVAIL_F_NO_INTERRUPT))
+		virtio_store_mb(vq->weak_barriers,
+				&vring_used_event(&vq->vring),
+				cpu_to_virtio16(_vq->vdev, vq->last_used_idx));
 
 #ifdef DEBUG
 	vq->last_add_time_valid = false;
@@ -653,8 +653,11 @@ bool virtqueue_enable_cb_delayed(struct virtqueue *_vq)
 	}
 	/* TODO: tune this threshold */
 	bufs = (u16)(vq->avail_idx_shadow - vq->last_used_idx) * 3 / 4;
-	vring_used_event(&vq->vring) = cpu_to_virtio16(_vq->vdev, vq->last_used_idx + bufs);
-	virtio_mb(vq->weak_barriers);
+
+	virtio_store_mb(vq->weak_barriers,
+			&vring_used_event(&vq->vring),
+			cpu_to_virtio16(_vq->vdev, vq->last_used_idx + bufs));
+
 	if (unlikely((u16)(virtio16_to_cpu(_vq->vdev, vq->vring.used->idx) - vq->last_used_idx) > bufs)) {
 		END_USE(vq);
 		return false;
Signature · 1 line
MST
#229 lore raw

Re: [PATCH v2 32/32] virtio_ring: use virt_store_mb

Hello.

Quoted 18 lines · Michael S. Tsirkin

We need a full barrier after writing out event index, using virt_store_mb there seems better than open-coding. As usual, we need a wrapper to account for strong barriers.

It's tempting to use this in vhost as well, for that, we'll need a variant of smp_store_mb that works on __user pointers.

---
  include/linux/virtio_ring.h  | 12 ++++++++++++
  drivers/virtio/virtio_ring.c | 15 +++++++++------
  2 files changed, 21 insertions(+), 6 deletions(-)
include/linux/virtio_ring.h +0 -0
index f3fa55b..3a74d91 100644

include/linux/virtio_ring.h static inline void virtio_wmb(bool weak_barriers) · line 54

Quoted 10 lines
include/linux/virtio_ring.h +7 -0
@@ -45,6 +45,18 @@ static inline void virtio_wmb(bool weak_barriers)
  		wmb();
  }
 
+static inline void virtio_store_mb(bool weak_barriers,
+				   __virtio16 *p, __virtio16 v)
+{
+	if (weak_barriers)
+		virt_store_mb(*p, v);
+	else
+	{

The kernel coding style dictates:

	if (weak_barriers) {
		virt_store_mb(*p, v);
	} else {

include/linux/virtio_ring.h static inline void virtio_wmb(bool weak_barriers) · line 59

Quoted 5 lines
include/linux/virtio_ring.h +5 -0
+		WRITE_ONCE(*p, v);
+		mb();
+	}
+}
+

[...]

MBR, Sergei

author maintainer
#230 lore raw

Re: [PATCH v2 32/32] virtio_ring: use virt_store_mb

Quoted 39 lines · Sergei Shtylyov

Hello.

Quoted 18 lines · Michael S. Tsirkin

We need a full barrier after writing out event index, using virt_store_mb there seems better than open-coding. As usual, we need a wrapper to account for strong barriers.

It's tempting to use this in vhost as well, for that, we'll need a variant of smp_store_mb that works on __user pointers.

---
 include/linux/virtio_ring.h  | 12 ++++++++++++
 drivers/virtio/virtio_ring.c | 15 +++++++++------
 2 files changed, 21 insertions(+), 6 deletions(-)
include/linux/virtio_ring.h +0 -0
index f3fa55b..3a74d91 100644

include/linux/virtio_ring.h static inline void virtio_wmb(bool weak_barriers) · line 54

Quoted 10 lines
include/linux/virtio_ring.h +7 -0
@@ -45,6 +45,18 @@ static inline void virtio_wmb(bool weak_barriers)
 		wmb();
 }
 
+static inline void virtio_store_mb(bool weak_barriers,
+				   __virtio16 *p, __virtio16 v)
+{
+	if (weak_barriers)
+		virt_store_mb(*p, v);
+	else
+	{
   The kernel coding style dictates:

	if (weak_barriers) {
		virt_store_mb(*p, v);
	} else {
Quoted 8 lines

include/linux/virtio_ring.h static inline void virtio_wmb(bool weak_barriers) · line 59

Quoted 5 lines
include/linux/virtio_ring.h +5 -0
+		WRITE_ONCE(*p, v);
+		mb();
+	}
+}
+

[...]

MBR, Sergei

Will fix, thanks!

author maintainer
#231 lore raw

Re: [PATCH v2 32/32] virtio_ring: use virt_store_mb

In reply to Sergei Shtylyov
Quoted 39 lines · Sergei Shtylyov

Hello.

Quoted 18 lines · Michael S. Tsirkin

We need a full barrier after writing out event index, using virt_store_mb there seems better than open-coding. As usual, we need a wrapper to account for strong barriers.

It's tempting to use this in vhost as well, for that, we'll need a variant of smp_store_mb that works on __user pointers.

---
 include/linux/virtio_ring.h  | 12 ++++++++++++
 drivers/virtio/virtio_ring.c | 15 +++++++++------
 2 files changed, 21 insertions(+), 6 deletions(-)
include/linux/virtio_ring.h +0 -0
index f3fa55b..3a74d91 100644

include/linux/virtio_ring.h static inline void virtio_wmb(bool weak_barriers) · line 54

Quoted 10 lines
include/linux/virtio_ring.h +7 -0
@@ -45,6 +45,18 @@ static inline void virtio_wmb(bool weak_barriers)
 		wmb();
 }
 
+static inline void virtio_store_mb(bool weak_barriers,
+				   __virtio16 *p, __virtio16 v)
+{
+	if (weak_barriers)
+		virt_store_mb(*p, v);
+	else
+	{
   The kernel coding style dictates:

	if (weak_barriers) {
		virt_store_mb(*p, v);
	} else {
Quoted 8 lines

include/linux/virtio_ring.h static inline void virtio_wmb(bool weak_barriers) · line 59

Quoted 5 lines
include/linux/virtio_ring.h +5 -0
+		WRITE_ONCE(*p, v);
+		mb();
+	}
+}
+

[...]

MBR, Sergei

Will fix, thanks!

author maintainer
#232 lore raw

Re: [PATCH v2 32/32] virtio_ring: use virt_store_mb

In reply to Sergei Shtylyov
Quoted 39 lines · Sergei Shtylyov

Hello.

Quoted 18 lines · Michael S. Tsirkin

We need a full barrier after writing out event index, using virt_store_mb there seems better than open-coding. As usual, we need a wrapper to account for strong barriers.

It's tempting to use this in vhost as well, for that, we'll need a variant of smp_store_mb that works on __user pointers.

---
 include/linux/virtio_ring.h  | 12 ++++++++++++
 drivers/virtio/virtio_ring.c | 15 +++++++++------
 2 files changed, 21 insertions(+), 6 deletions(-)
include/linux/virtio_ring.h +0 -0
index f3fa55b..3a74d91 100644

include/linux/virtio_ring.h static inline void virtio_wmb(bool weak_barriers) · line 54

Quoted 10 lines
include/linux/virtio_ring.h +7 -0
@@ -45,6 +45,18 @@ static inline void virtio_wmb(bool weak_barriers)
 		wmb();
 }
 
+static inline void virtio_store_mb(bool weak_barriers,
+				   __virtio16 *p, __virtio16 v)
+{
+	if (weak_barriers)
+		virt_store_mb(*p, v);
+	else
+	{
   The kernel coding style dictates:

	if (weak_barriers) {
		virt_store_mb(*p, v);
	} else {
Quoted 8 lines

include/linux/virtio_ring.h static inline void virtio_wmb(bool weak_barriers) · line 59

Quoted 5 lines
include/linux/virtio_ring.h +5 -0
+		WRITE_ONCE(*p, v);
+		mb();
+	}
+}
+

[...]

MBR, Sergei

Will fix, thanks!

#233 lore raw

Re: [PATCH v2 32/32] virtio_ring: use virt_store_mb

In reply to Michael S. Tsirkin

Hello.

Quoted 18 lines · Michael S. Tsirkin

We need a full barrier after writing out event index, using virt_store_mb there seems better than open-coding. As usual, we need a wrapper to account for strong barriers.

It's tempting to use this in vhost as well, for that, we'll need a variant of smp_store_mb that works on __user pointers.

---
  include/linux/virtio_ring.h  | 12 ++++++++++++
  drivers/virtio/virtio_ring.c | 15 +++++++++------
  2 files changed, 21 insertions(+), 6 deletions(-)
include/linux/virtio_ring.h +0 -0
index f3fa55b..3a74d91 100644

include/linux/virtio_ring.h static inline void virtio_wmb(bool weak_barriers) · line 54

Quoted 10 lines
include/linux/virtio_ring.h +7 -0
@@ -45,6 +45,18 @@ static inline void virtio_wmb(bool weak_barriers)
  		wmb();
  }
 
+static inline void virtio_store_mb(bool weak_barriers,
+				   __virtio16 *p, __virtio16 v)
+{
+	if (weak_barriers)
+		virt_store_mb(*p, v);
+	else
+	{

The kernel coding style dictates:

	if (weak_barriers) {
		virt_store_mb(*p, v);
	} else {

include/linux/virtio_ring.h static inline void virtio_wmb(bool weak_barriers) · line 59

Quoted 5 lines
include/linux/virtio_ring.h +5 -0
+		WRITE_ONCE(*p, v);
+		mb();
+	}
+}
+

[...]

MBR, Sergei

author maintainer
#234 lore raw

PATCH v2 33/34 xenbus: use virt_xxx barriers

In reply to Michael S. Tsirkin

drivers/xen/xenbus/xenbus_comms.c uses
full memory barriers to communicate with the other side.

For guests compiled with CONFIG_SMP, smp_wmb and smp_mb would be sufficient, so mb() and wmb() here are only needed if a non-SMP guest runs on an SMP host.

Switch to virt_xxx barriers which serve this exact purpose.

---
 drivers/xen/xenbus/xenbus_comms.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
drivers/xen/xenbus/xenbus_comms.c +4 -4
index fdb0f33..ecdecce 100644
@@ -123,14 +123,14 @@ int xb_write(const void *data, unsigned len)
 			avail = len;
 
 		/* Must write data /after/ reading the consumer index. */
-		mb();
+		virt_mb();
 
 		memcpy(dst, data, avail);
 		data += avail;
 		len -= avail;
 
 		/* Other side must not see new producer until data is there. */
-		wmb();
+		virt_wmb();
 		intf->req_prod += avail;
 
 		/* Implies mb(): other side will see the updated producer. */
@@ -180,14 +180,14 @@ int xb_read(void *data, unsigned len)
 			avail = len;
 
 		/* Must read data /after/ reading the producer index. */
-		rmb();
+		virt_rmb();
 
 		memcpy(data, src, avail);
 		data += avail;
 		len -= avail;
 
 		/* Other side must not see free space until we've copied out */
-		mb();
+		virt_mb();
 		intf->rsp_cons += avail;
 
 		pr_debug("Finished read of %i bytes (%i to go)\n", avail, len);
Signature · 1 line
MST
Acked-by on v2 33/34
#235 lore raw

Re: [Xen-devel] [PATCH v2 33/34] xenbus: use virt_xxx barriers

Quoted 8 lines · Michael S. Tsirkin

drivers/xen/xenbus/xenbus_comms.c uses
full memory barriers to communicate with the other side.

For guests compiled with CONFIG_SMP, smp_wmb and smp_mb would be sufficient, so mb() and wmb() here are only needed if a non-SMP guest runs on an SMP host.

Switch to virt_xxx barriers which serve this exact purpose.

If you're feeling particularly keen there's a rmb() consume_one_event() in drivers/xen/events/events_fifo.c that can be converted to virt_rmb() as well.

David

Acked-by on v2 33/34
#236 lore raw

Re: [Xen-devel] [PATCH v2 33/34] xenbus: use virt_xxx barriers

In reply to Michael S. Tsirkin
Quoted 8 lines · Michael S. Tsirkin

drivers/xen/xenbus/xenbus_comms.c uses
full memory barriers to communicate with the other side.

For guests compiled with CONFIG_SMP, smp_wmb and smp_mb would be sufficient, so mb() and wmb() here are only needed if a non-SMP guest runs on an SMP host.

Switch to virt_xxx barriers which serve this exact purpose.

If you're feeling particularly keen there's a rmb() consume_one_event() in drivers/xen/events/events_fifo.c that can be converted to virt_rmb() as well.

David

Acked-by on v2 33/34
#237 lore raw

Re: [PATCH v2 33/34] xenbus: use virt_xxx barriers

In reply to Michael S. Tsirkin
Quoted 8 lines · Michael S. Tsirkin

drivers/xen/xenbus/xenbus_comms.c uses
full memory barriers to communicate with the other side.

For guests compiled with CONFIG_SMP, smp_wmb and smp_mb would be sufficient, so mb() and wmb() here are only needed if a non-SMP guest runs on an SMP host.

Switch to virt_xxx barriers which serve this exact purpose.

If you're feeling particularly keen there's a rmb() consume_one_event() in drivers/xen/events/events_fifo.c that can be converted to virt_rmb() as well.

David

Reviewed-by on v2 33/34
#238 lore raw

Re: [PATCH v2 33/34] xenbus: use virt_xxx barriers

In reply to Michael S. Tsirkin
Quoted 10 lines · Michael S. Tsirkin

drivers/xen/xenbus/xenbus_comms.c uses
full memory barriers to communicate with the other side.

For guests compiled with CONFIG_SMP, smp_wmb and smp_mb would be sufficient, so mb() and wmb() here are only needed if a non-SMP guest runs on an SMP host.

Switch to virt_xxx barriers which serve this exact purpose.

Are you also going to take care of

drivers/xen/grant-table.c
drivers/xen/evtchn.c
drivers/xen/events/events_fifo.c
drivers/xen/xen-scsiback.c
drivers/xen/tmem.c
drivers/xen/xen-pciback/pci_stub.c
drivers/xen/xen-pciback/pciback_ops.c

?

Quoted 44 lines
 drivers/xen/xenbus/xenbus_comms.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
drivers/xen/xenbus/xenbus_comms.c +4 -4
index fdb0f33..ecdecce 100644
@@ -123,14 +123,14 @@ int xb_write(const void *data, unsigned len)
 			avail = len;
 
 		/* Must write data /after/ reading the consumer index. */
-		mb();
+		virt_mb();
 
 		memcpy(dst, data, avail);
 		data += avail;
 		len -= avail;
 
 		/* Other side must not see new producer until data is there. */
-		wmb();
+		virt_wmb();
 		intf->req_prod += avail;
 
 		/* Implies mb(): other side will see the updated producer. */
@@ -180,14 +180,14 @@ int xb_read(void *data, unsigned len)
 			avail = len;
 
 		/* Must read data /after/ reading the producer index. */
-		rmb();
+		virt_rmb();
 
 		memcpy(data, src, avail);
 		data += avail;
 		len -= avail;
 
 		/* Other side must not see free space until we've copied out */
-		mb();
+		virt_mb();
 		intf->rsp_cons += avail;
 
 		pr_debug("Finished read of %i bytes (%i to go)\n", avail, len);
Signature · 1 line
MST
Reviewed-by on v2 33/34
#239 lore raw

Re: [PATCH v2 33/34] xenbus: use virt_xxx barriers

In reply to Michael S. Tsirkin
Quoted 10 lines · Michael S. Tsirkin

drivers/xen/xenbus/xenbus_comms.c uses
full memory barriers to communicate with the other side.

For guests compiled with CONFIG_SMP, smp_wmb and smp_mb would be sufficient, so mb() and wmb() here are only needed if a non-SMP guest runs on an SMP host.

Switch to virt_xxx barriers which serve this exact purpose.

Are you also going to take care of

drivers/xen/grant-table.c
drivers/xen/evtchn.c
drivers/xen/events/events_fifo.c
drivers/xen/xen-scsiback.c
drivers/xen/tmem.c
drivers/xen/xen-pciback/pci_stub.c
drivers/xen/xen-pciback/pciback_ops.c

?

Quoted 44 lines
 drivers/xen/xenbus/xenbus_comms.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
drivers/xen/xenbus/xenbus_comms.c +4 -4
index fdb0f33..ecdecce 100644
@@ -123,14 +123,14 @@ int xb_write(const void *data, unsigned len)
 			avail = len;
 
 		/* Must write data /after/ reading the consumer index. */
-		mb();
+		virt_mb();
 
 		memcpy(dst, data, avail);
 		data += avail;
 		len -= avail;
 
 		/* Other side must not see new producer until data is there. */
-		wmb();
+		virt_wmb();
 		intf->req_prod += avail;
 
 		/* Implies mb(): other side will see the updated producer. */
@@ -180,14 +180,14 @@ int xb_read(void *data, unsigned len)
 			avail = len;
 
 		/* Must read data /after/ reading the producer index. */
-		rmb();
+		virt_rmb();
 
 		memcpy(data, src, avail);
 		data += avail;
 		len -= avail;
 
 		/* Other side must not see free space until we've copied out */
-		mb();
+		virt_mb();
 		intf->rsp_cons += avail;
 
 		pr_debug("Finished read of %i bytes (%i to go)\n", avail, len);
Signature · 1 line
MST
Reviewed-by on v2 33/34
#240 lore raw

Re: [PATCH v2 33/34] xenbus: use virt_xxx barriers

In reply to Michael S. Tsirkin
Quoted 10 lines · Michael S. Tsirkin

drivers/xen/xenbus/xenbus_comms.c uses
full memory barriers to communicate with the other side.

For guests compiled with CONFIG_SMP, smp_wmb and smp_mb would be sufficient, so mb() and wmb() here are only needed if a non-SMP guest runs on an SMP host.

Switch to virt_xxx barriers which serve this exact purpose.

Are you also going to take care of

drivers/xen/grant-table.c
drivers/xen/evtchn.c
drivers/xen/events/events_fifo.c
drivers/xen/xen-scsiback.c
drivers/xen/tmem.c
drivers/xen/xen-pciback/pci_stub.c
drivers/xen/xen-pciback/pciback_ops.c

?

Quoted 44 lines
 drivers/xen/xenbus/xenbus_comms.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
drivers/xen/xenbus/xenbus_comms.c +4 -4
index fdb0f33..ecdecce 100644
@@ -123,14 +123,14 @@ int xb_write(const void *data, unsigned len)
 			avail = len;
 
 		/* Must write data /after/ reading the consumer index. */
-		mb();
+		virt_mb();
 
 		memcpy(dst, data, avail);
 		data += avail;
 		len -= avail;
 
 		/* Other side must not see new producer until data is there. */
-		wmb();
+		virt_wmb();
 		intf->req_prod += avail;
 
 		/* Implies mb(): other side will see the updated producer. */
@@ -180,14 +180,14 @@ int xb_read(void *data, unsigned len)
 			avail = len;
 
 		/* Must read data /after/ reading the producer index. */
-		rmb();
+		virt_rmb();
 
 		memcpy(data, src, avail);
 		data += avail;
 		len -= avail;
 
 		/* Other side must not see free space until we've copied out */
-		mb();
+		virt_mb();
 		intf->rsp_cons += avail;
 
 		pr_debug("Finished read of %i bytes (%i to go)\n", avail, len);
Signature · 1 line
MST
maintainer
#241 lore raw

Re: [PATCH v2 33/34] xenbus: use virt_xxx barriers

In reply to Michael S. Tsirkin
Quoted 19 lines · Michael S. Tsirkin

drivers/xen/xenbus/xenbus_comms.c uses
full memory barriers to communicate with the other side.

For guests compiled with CONFIG_SMP, smp_wmb and smp_mb would be sufficient, so mb() and wmb() here are only needed if a non-SMP guest runs on an SMP host.

Switch to virt_xxx barriers which serve this exact purpose.

---
 drivers/xen/xenbus/xenbus_comms.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
drivers/xen/xenbus/xenbus_comms.c +0 -0
index fdb0f33..ecdecce 100644

drivers/xen/xenbus/xenbus_comms.c int xb_write(const void *data, unsigned len) · line 127

Quoted 6 lines
drivers/xen/xenbus/xenbus_comms.c +1 -1
@@ -123,14 +123,14 @@ int xb_write(const void *data, unsigned len)
 			avail = len;
 
 		/* Must write data /after/ reading the consumer index. */
-		mb();
+		virt_mb();
 

So its possible to remove this barrier entirely, see the "CONTROL DEPENDNCIES" chunk of memory-barrier.txt.

But do that in a separate patch series and only if you really really need the performance.

maintainer
#242 lore raw

Re: [PATCH v2 33/34] xenbus: use virt_xxx barriers

In reply to Michael S. Tsirkin
Quoted 19 lines · Michael S. Tsirkin

drivers/xen/xenbus/xenbus_comms.c uses
full memory barriers to communicate with the other side.

For guests compiled with CONFIG_SMP, smp_wmb and smp_mb would be sufficient, so mb() and wmb() here are only needed if a non-SMP guest runs on an SMP host.

Switch to virt_xxx barriers which serve this exact purpose.

---
 drivers/xen/xenbus/xenbus_comms.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
drivers/xen/xenbus/xenbus_comms.c +0 -0
index fdb0f33..ecdecce 100644

drivers/xen/xenbus/xenbus_comms.c int xb_write(const void *data, unsigned len) · line 127

Quoted 6 lines
drivers/xen/xenbus/xenbus_comms.c +1 -1
@@ -123,14 +123,14 @@ int xb_write(const void *data, unsigned len)
 			avail = len;
 
 		/* Must write data /after/ reading the consumer index. */
-		mb();
+		virt_mb();
 

So its possible to remove this barrier entirely, see the "CONTROL DEPENDNCIES" chunk of memory-barrier.txt.

But do that in a separate patch series and only if you really really need the performance.

maintainer
#243 lore raw

Re: [PATCH v2 33/34] xenbus: use virt_xxx barriers

In reply to Michael S. Tsirkin
Quoted 19 lines · Michael S. Tsirkin

drivers/xen/xenbus/xenbus_comms.c uses
full memory barriers to communicate with the other side.

For guests compiled with CONFIG_SMP, smp_wmb and smp_mb would be sufficient, so mb() and wmb() here are only needed if a non-SMP guest runs on an SMP host.

Switch to virt_xxx barriers which serve this exact purpose.

---
 drivers/xen/xenbus/xenbus_comms.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
drivers/xen/xenbus/xenbus_comms.c +0 -0
index fdb0f33..ecdecce 100644

drivers/xen/xenbus/xenbus_comms.c int xb_write(const void *data, unsigned len) · line 127

Quoted 6 lines
drivers/xen/xenbus/xenbus_comms.c +1 -1
@@ -123,14 +123,14 @@ int xb_write(const void *data, unsigned len)
 			avail = len;
 
 		/* Must write data /after/ reading the consumer index. */
-		mb();
+		virt_mb();
 

So its possible to remove this barrier entirely, see the "CONTROL DEPENDNCIES" chunk of memory-barrier.txt.

But do that in a separate patch series and only if you really really need the performance.

author maintainer
#244 lore raw

PATCH v2 33/34 xenbus: use virt_xxx barriers

In reply to Michael S. Tsirkin

drivers/xen/xenbus/xenbus_comms.c uses
full memory barriers to communicate with the other side.

For guests compiled with CONFIG_SMP, smp_wmb and smp_mb would be sufficient, so mb() and wmb() here are only needed if a non-SMP guest runs on an SMP host.

Switch to virt_xxx barriers which serve this exact purpose.

---
 drivers/xen/xenbus/xenbus_comms.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
drivers/xen/xenbus/xenbus_comms.c +4 -4
index fdb0f33..ecdecce 100644
@@ -123,14 +123,14 @@ int xb_write(const void *data, unsigned len)
 			avail = len;
 
 		/* Must write data /after/ reading the consumer index. */
-		mb();
+		virt_mb();
 
 		memcpy(dst, data, avail);
 		data += avail;
 		len -= avail;
 
 		/* Other side must not see new producer until data is there. */
-		wmb();
+		virt_wmb();
 		intf->req_prod += avail;
 
 		/* Implies mb(): other side will see the updated producer. */
@@ -180,14 +180,14 @@ int xb_read(void *data, unsigned len)
 			avail = len;
 
 		/* Must read data /after/ reading the producer index. */
-		rmb();
+		virt_rmb();
 
 		memcpy(data, src, avail);
 		data += avail;
 		len -= avail;
 
 		/* Other side must not see free space until we've copied out */
-		mb();
+		virt_mb();
 		intf->rsp_cons += avail;
 
 		pr_debug("Finished read of %i bytes (%i to go)\n", avail, len);
Signature · 1 line
MST
author maintainer
#245 lore raw

PATCH v2 32/32 virtio_ring: use virt_store_mb

In reply to Michael S. Tsirkin

We need a full barrier after writing out event index, using virt_store_mb there seems better than open-coding. As usual, we need a wrapper to account for strong barriers.

It's tempting to use this in vhost as well, for that, we'll need a variant of smp_store_mb that works on __user pointers.

---
 include/linux/virtio_ring.h  | 12 ++++++++++++
 drivers/virtio/virtio_ring.c | 15 +++++++++------
 2 files changed, 21 insertions(+), 6 deletions(-)
include/linux/virtio_ring.h +12 -0
index f3fa55b..3a74d91 100644
@@ -45,6 +45,18 @@ static inline void virtio_wmb(bool weak_barriers)
 		wmb();
 }
 
+static inline void virtio_store_mb(bool weak_barriers,
+				   __virtio16 *p, __virtio16 v)
+{
+	if (weak_barriers)
+		virt_store_mb(*p, v);
+	else
+	{
+		WRITE_ONCE(*p, v);
+		mb();
+	}
+}
+
 struct virtio_device;
 struct virtqueue;
 
drivers/virtio/virtio_ring.c +9 -6
index ee663c4..e12e385 100644
@@ -517,10 +517,10 @@ void *virtqueue_get_buf(struct virtqueue *_vq, unsigned int *len)
 	/* If we expect an interrupt for the next entry, tell host
 	 * by writing event index and flush out the write before
 	 * the read in the next get_buf call. */
-	if (!(vq->avail_flags_shadow & VRING_AVAIL_F_NO_INTERRUPT)) {
-		vring_used_event(&vq->vring) = cpu_to_virtio16(_vq->vdev, vq->last_used_idx);
-		virtio_mb(vq->weak_barriers);
-	}
+	if (!(vq->avail_flags_shadow & VRING_AVAIL_F_NO_INTERRUPT))
+		virtio_store_mb(vq->weak_barriers,
+				&vring_used_event(&vq->vring),
+				cpu_to_virtio16(_vq->vdev, vq->last_used_idx));
 
 #ifdef DEBUG
 	vq->last_add_time_valid = false;
@@ -653,8 +653,11 @@ bool virtqueue_enable_cb_delayed(struct virtqueue *_vq)
 	}
 	/* TODO: tune this threshold */
 	bufs = (u16)(vq->avail_idx_shadow - vq->last_used_idx) * 3 / 4;
-	vring_used_event(&vq->vring) = cpu_to_virtio16(_vq->vdev, vq->last_used_idx + bufs);
-	virtio_mb(vq->weak_barriers);
+
+	virtio_store_mb(vq->weak_barriers,
+			&vring_used_event(&vq->vring),
+			cpu_to_virtio16(_vq->vdev, vq->last_used_idx + bufs));
+
 	if (unlikely((u16)(virtio16_to_cpu(_vq->vdev, vq->vring.used->idx) - vq->last_used_idx) > bufs)) {
 		END_USE(vq);
 		return false;
Signature · 1 line
MST
author maintainer
#246 lore raw

PATCH v2 33/34 xenbus: use virt_xxx barriers

In reply to Michael S. Tsirkin

drivers/xen/xenbus/xenbus_comms.c uses
full memory barriers to communicate with the other side.

For guests compiled with CONFIG_SMP, smp_wmb and smp_mb would be sufficient, so mb() and wmb() here are only needed if a non-SMP guest runs on an SMP host.

Switch to virt_xxx barriers which serve this exact purpose.

---
 drivers/xen/xenbus/xenbus_comms.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
drivers/xen/xenbus/xenbus_comms.c +4 -4
index fdb0f33..ecdecce 100644
@@ -123,14 +123,14 @@ int xb_write(const void *data, unsigned len)
 			avail = len;
 
 		/* Must write data /after/ reading the consumer index. */
-		mb();
+		virt_mb();
 
 		memcpy(dst, data, avail);
 		data += avail;
 		len -= avail;
 
 		/* Other side must not see new producer until data is there. */
-		wmb();
+		virt_wmb();
 		intf->req_prod += avail;
 
 		/* Implies mb(): other side will see the updated producer. */
@@ -180,14 +180,14 @@ int xb_read(void *data, unsigned len)
 			avail = len;
 
 		/* Must read data /after/ reading the producer index. */
-		rmb();
+		virt_rmb();
 
 		memcpy(data, src, avail);
 		data += avail;
 		len -= avail;
 
 		/* Other side must not see free space until we've copied out */
-		mb();
+		virt_mb();
 		intf->rsp_cons += avail;
 
 		pr_debug("Finished read of %i bytes (%i to go)\n", avail, len);
Signature · 1 line
MST
author maintainer
#247 lore raw

PATCH v2 34/34 xen/io: use virt_xxx barriers

In reply to Michael S. Tsirkin

include/xen/interface/io/ring.h uses
full memory barriers to communicate with the other side.

For guests compiled with CONFIG_SMP, smp_wmb and smp_mb would be sufficient, so mb() and wmb() here are only needed if a non-SMP guest runs on an SMP host.

Switch to virt_xxx barriers which serve this exact purpose.

---
 include/xen/interface/io/ring.h | 16 ++++++++--------
 1 file changed, 8 insertions(+), 8 deletions(-)
include/xen/interface/io/ring.h +8 -8
index 7dc685b..21f4fbd 100644
@@ -208,12 +208,12 @@ struct __name##_back_ring {						\
 
 
 #define RING_PUSH_REQUESTS(_r) do {					\
-    wmb(); /* back sees requests /before/ updated producer index */	\
+    virt_wmb(); /* back sees requests /before/ updated producer index */	\
     (_r)->sring->req_prod = (_r)->req_prod_pvt;				\
 } while (0)
 
 #define RING_PUSH_RESPONSES(_r) do {					\
-    wmb(); /* front sees responses /before/ updated producer index */	\
+    virt_wmb(); /* front sees responses /before/ updated producer index */	\
     (_r)->sring->rsp_prod = (_r)->rsp_prod_pvt;				\
 } while (0)
 
@@ -250,9 +250,9 @@ struct __name##_back_ring {						\
 #define RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(_r, _notify) do {		\
     RING_IDX __old = (_r)->sring->req_prod;				\
     RING_IDX __new = (_r)->req_prod_pvt;				\
-    wmb(); /* back sees requests /before/ updated producer index */	\
+    virt_wmb(); /* back sees requests /before/ updated producer index */	\
     (_r)->sring->req_prod = __new;					\
-    mb(); /* back sees new requests /before/ we check req_event */	\
+    virt_mb(); /* back sees new requests /before/ we check req_event */	\
     (_notify) = ((RING_IDX)(__new - (_r)->sring->req_event) <		\
 		 (RING_IDX)(__new - __old));				\
 } while (0)
@@ -260,9 +260,9 @@ struct __name##_back_ring {						\
 #define RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(_r, _notify) do {		\
     RING_IDX __old = (_r)->sring->rsp_prod;				\
     RING_IDX __new = (_r)->rsp_prod_pvt;				\
-    wmb(); /* front sees responses /before/ updated producer index */	\
+    virt_wmb(); /* front sees responses /before/ updated producer index */	\
     (_r)->sring->rsp_prod = __new;					\
-    mb(); /* front sees new responses /before/ we check rsp_event */	\
+    virt_mb(); /* front sees new responses /before/ we check rsp_event */	\
     (_notify) = ((RING_IDX)(__new - (_r)->sring->rsp_event) <		\
 		 (RING_IDX)(__new - __old));				\
 } while (0)
@@ -271,7 +271,7 @@ struct __name##_back_ring {						\
     (_work_to_do) = RING_HAS_UNCONSUMED_REQUESTS(_r);			\
     if (_work_to_do) break;						\
     (_r)->sring->req_event = (_r)->req_cons + 1;			\
-    mb();								\
+    virt_mb();								\
     (_work_to_do) = RING_HAS_UNCONSUMED_REQUESTS(_r);			\
 } while (0)
 
@@ -279,7 +279,7 @@ struct __name##_back_ring {						\
     (_work_to_do) = RING_HAS_UNCONSUMED_RESPONSES(_r);			\
     if (_work_to_do) break;						\
     (_r)->sring->rsp_event = (_r)->rsp_cons + 1;			\
-    mb();								\
+    virt_mb();								\
     (_work_to_do) = RING_HAS_UNCONSUMED_RESPONSES(_r);			\
 } while (0)
 
Signature · 1 line
MST
Acked-by on v2 34/34
#248 lore raw

Re: [Xen-devel] [PATCH v2 34/34] xen/io: use virt_xxx barriers

Quoted 8 lines · Michael S. Tsirkin

include/xen/interface/io/ring.h uses
full memory barriers to communicate with the other side.

For guests compiled with CONFIG_SMP, smp_wmb and smp_mb would be sufficient, so mb() and wmb() here are only needed if a non-SMP guest runs on an SMP host.

Switch to virt_xxx barriers which serve this exact purpose.

David

Acked-by on v2 34/34
#249 lore raw

Re: [Xen-devel] [PATCH v2 34/34] xen/io: use virt_xxx barriers

In reply to Michael S. Tsirkin
Quoted 8 lines · Michael S. Tsirkin

include/xen/interface/io/ring.h uses
full memory barriers to communicate with the other side.

For guests compiled with CONFIG_SMP, smp_wmb and smp_mb would be sufficient, so mb() and wmb() here are only needed if a non-SMP guest runs on an SMP host.

Switch to virt_xxx barriers which serve this exact purpose.

David

Acked-by on v2 34/34
#250 lore raw

Re: [PATCH v2 34/34] xen/io: use virt_xxx barriers

In reply to Michael S. Tsirkin
Quoted 8 lines · Michael S. Tsirkin

include/xen/interface/io/ring.h uses
full memory barriers to communicate with the other side.

For guests compiled with CONFIG_SMP, smp_wmb and smp_mb would be sufficient, so mb() and wmb() here are only needed if a non-SMP guest runs on an SMP host.

Switch to virt_xxx barriers which serve this exact purpose.

David

Reviewed-by on v2 34/34
#251 lore raw

Re: [PATCH v2 34/34] xen/io: use virt_xxx barriers

In reply to Michael S. Tsirkin
Quoted 10 lines · Michael S. Tsirkin

include/xen/interface/io/ring.h uses
full memory barriers to communicate with the other side.

For guests compiled with CONFIG_SMP, smp_wmb and smp_mb would be sufficient, so mb() and wmb() here are only needed if a non-SMP guest runs on an SMP host.

Switch to virt_xxx barriers which serve this exact purpose.

Quoted 67 lines
 include/xen/interface/io/ring.h | 16 ++++++++--------
 1 file changed, 8 insertions(+), 8 deletions(-)
include/xen/interface/io/ring.h +8 -8
index 7dc685b..21f4fbd 100644
@@ -208,12 +208,12 @@ struct __name##_back_ring {						\
 
 
 #define RING_PUSH_REQUESTS(_r) do {					\
-    wmb(); /* back sees requests /before/ updated producer index */	\
+    virt_wmb(); /* back sees requests /before/ updated producer index */	\
     (_r)->sring->req_prod = (_r)->req_prod_pvt;				\
 } while (0)
 
 #define RING_PUSH_RESPONSES(_r) do {					\
-    wmb(); /* front sees responses /before/ updated producer index */	\
+    virt_wmb(); /* front sees responses /before/ updated producer index */	\
     (_r)->sring->rsp_prod = (_r)->rsp_prod_pvt;				\
 } while (0)
 
@@ -250,9 +250,9 @@ struct __name##_back_ring {						\
 #define RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(_r, _notify) do {		\
     RING_IDX __old = (_r)->sring->req_prod;				\
     RING_IDX __new = (_r)->req_prod_pvt;				\
-    wmb(); /* back sees requests /before/ updated producer index */	\
+    virt_wmb(); /* back sees requests /before/ updated producer index */	\
     (_r)->sring->req_prod = __new;					\
-    mb(); /* back sees new requests /before/ we check req_event */	\
+    virt_mb(); /* back sees new requests /before/ we check req_event */	\
     (_notify) = ((RING_IDX)(__new - (_r)->sring->req_event) <		\
 		 (RING_IDX)(__new - __old));				\
 } while (0)
@@ -260,9 +260,9 @@ struct __name##_back_ring {						\
 #define RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(_r, _notify) do {		\
     RING_IDX __old = (_r)->sring->rsp_prod;				\
     RING_IDX __new = (_r)->rsp_prod_pvt;				\
-    wmb(); /* front sees responses /before/ updated producer index */	\
+    virt_wmb(); /* front sees responses /before/ updated producer index */	\
     (_r)->sring->rsp_prod = __new;					\
-    mb(); /* front sees new responses /before/ we check rsp_event */	\
+    virt_mb(); /* front sees new responses /before/ we check rsp_event */	\
     (_notify) = ((RING_IDX)(__new - (_r)->sring->rsp_event) <		\
 		 (RING_IDX)(__new - __old));				\
 } while (0)
@@ -271,7 +271,7 @@ struct __name##_back_ring {						\
     (_work_to_do) = RING_HAS_UNCONSUMED_REQUESTS(_r);			\
     if (_work_to_do) break;						\
     (_r)->sring->req_event = (_r)->req_cons + 1;			\
-    mb();								\
+    virt_mb();								\
     (_work_to_do) = RING_HAS_UNCONSUMED_REQUESTS(_r);			\
 } while (0)
 
@@ -279,7 +279,7 @@ struct __name##_back_ring {						\
     (_work_to_do) = RING_HAS_UNCONSUMED_RESPONSES(_r);			\
     if (_work_to_do) break;						\
     (_r)->sring->rsp_event = (_r)->rsp_cons + 1;			\
-    mb();								\
+    virt_mb();								\
     (_work_to_do) = RING_HAS_UNCONSUMED_RESPONSES(_r);			\
 } while (0)
 
Signature · 1 line
MST
Reviewed-by on v2 34/34
#252 lore raw

Re: [PATCH v2 34/34] xen/io: use virt_xxx barriers

In reply to Michael S. Tsirkin
Quoted 10 lines · Michael S. Tsirkin

include/xen/interface/io/ring.h uses
full memory barriers to communicate with the other side.

For guests compiled with CONFIG_SMP, smp_wmb and smp_mb would be sufficient, so mb() and wmb() here are only needed if a non-SMP guest runs on an SMP host.

Switch to virt_xxx barriers which serve this exact purpose.

Quoted 67 lines
 include/xen/interface/io/ring.h | 16 ++++++++--------
 1 file changed, 8 insertions(+), 8 deletions(-)
include/xen/interface/io/ring.h +8 -8
index 7dc685b..21f4fbd 100644
@@ -208,12 +208,12 @@ struct __name##_back_ring {						\
 
 
 #define RING_PUSH_REQUESTS(_r) do {					\
-    wmb(); /* back sees requests /before/ updated producer index */	\
+    virt_wmb(); /* back sees requests /before/ updated producer index */	\
     (_r)->sring->req_prod = (_r)->req_prod_pvt;				\
 } while (0)
 
 #define RING_PUSH_RESPONSES(_r) do {					\
-    wmb(); /* front sees responses /before/ updated producer index */	\
+    virt_wmb(); /* front sees responses /before/ updated producer index */	\
     (_r)->sring->rsp_prod = (_r)->rsp_prod_pvt;				\
 } while (0)
 
@@ -250,9 +250,9 @@ struct __name##_back_ring {						\
 #define RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(_r, _notify) do {		\
     RING_IDX __old = (_r)->sring->req_prod;				\
     RING_IDX __new = (_r)->req_prod_pvt;				\
-    wmb(); /* back sees requests /before/ updated producer index */	\
+    virt_wmb(); /* back sees requests /before/ updated producer index */	\
     (_r)->sring->req_prod = __new;					\
-    mb(); /* back sees new requests /before/ we check req_event */	\
+    virt_mb(); /* back sees new requests /before/ we check req_event */	\
     (_notify) = ((RING_IDX)(__new - (_r)->sring->req_event) <		\
 		 (RING_IDX)(__new - __old));				\
 } while (0)
@@ -260,9 +260,9 @@ struct __name##_back_ring {						\
 #define RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(_r, _notify) do {		\
     RING_IDX __old = (_r)->sring->rsp_prod;				\
     RING_IDX __new = (_r)->rsp_prod_pvt;				\
-    wmb(); /* front sees responses /before/ updated producer index */	\
+    virt_wmb(); /* front sees responses /before/ updated producer index */	\
     (_r)->sring->rsp_prod = __new;					\
-    mb(); /* front sees new responses /before/ we check rsp_event */	\
+    virt_mb(); /* front sees new responses /before/ we check rsp_event */	\
     (_notify) = ((RING_IDX)(__new - (_r)->sring->rsp_event) <		\
 		 (RING_IDX)(__new - __old));				\
 } while (0)
@@ -271,7 +271,7 @@ struct __name##_back_ring {						\
     (_work_to_do) = RING_HAS_UNCONSUMED_REQUESTS(_r);			\
     if (_work_to_do) break;						\
     (_r)->sring->req_event = (_r)->req_cons + 1;			\
-    mb();								\
+    virt_mb();								\
     (_work_to_do) = RING_HAS_UNCONSUMED_REQUESTS(_r);			\
 } while (0)
 
@@ -279,7 +279,7 @@ struct __name##_back_ring {						\
     (_work_to_do) = RING_HAS_UNCONSUMED_RESPONSES(_r);			\
     if (_work_to_do) break;						\
     (_r)->sring->rsp_event = (_r)->rsp_cons + 1;			\
-    mb();								\
+    virt_mb();								\
     (_work_to_do) = RING_HAS_UNCONSUMED_RESPONSES(_r);			\
 } while (0)
 
Signature · 1 line
MST
Reviewed-by on v2 34/34
#253 lore raw

Re: [PATCH v2 34/34] xen/io: use virt_xxx barriers

In reply to Michael S. Tsirkin
Quoted 10 lines · Michael S. Tsirkin

include/xen/interface/io/ring.h uses
full memory barriers to communicate with the other side.

For guests compiled with CONFIG_SMP, smp_wmb and smp_mb would be sufficient, so mb() and wmb() here are only needed if a non-SMP guest runs on an SMP host.

Switch to virt_xxx barriers which serve this exact purpose.

Quoted 67 lines
 include/xen/interface/io/ring.h | 16 ++++++++--------
 1 file changed, 8 insertions(+), 8 deletions(-)
include/xen/interface/io/ring.h +8 -8
index 7dc685b..21f4fbd 100644
@@ -208,12 +208,12 @@ struct __name##_back_ring {						\
 
 
 #define RING_PUSH_REQUESTS(_r) do {					\
-    wmb(); /* back sees requests /before/ updated producer index */	\
+    virt_wmb(); /* back sees requests /before/ updated producer index */	\
     (_r)->sring->req_prod = (_r)->req_prod_pvt;				\
 } while (0)
 
 #define RING_PUSH_RESPONSES(_r) do {					\
-    wmb(); /* front sees responses /before/ updated producer index */	\
+    virt_wmb(); /* front sees responses /before/ updated producer index */	\
     (_r)->sring->rsp_prod = (_r)->rsp_prod_pvt;				\
 } while (0)
 
@@ -250,9 +250,9 @@ struct __name##_back_ring {						\
 #define RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(_r, _notify) do {		\
     RING_IDX __old = (_r)->sring->req_prod;				\
     RING_IDX __new = (_r)->req_prod_pvt;				\
-    wmb(); /* back sees requests /before/ updated producer index */	\
+    virt_wmb(); /* back sees requests /before/ updated producer index */	\
     (_r)->sring->req_prod = __new;					\
-    mb(); /* back sees new requests /before/ we check req_event */	\
+    virt_mb(); /* back sees new requests /before/ we check req_event */	\
     (_notify) = ((RING_IDX)(__new - (_r)->sring->req_event) <		\
 		 (RING_IDX)(__new - __old));				\
 } while (0)
@@ -260,9 +260,9 @@ struct __name##_back_ring {						\
 #define RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(_r, _notify) do {		\
     RING_IDX __old = (_r)->sring->rsp_prod;				\
     RING_IDX __new = (_r)->rsp_prod_pvt;				\
-    wmb(); /* front sees responses /before/ updated producer index */	\
+    virt_wmb(); /* front sees responses /before/ updated producer index */	\
     (_r)->sring->rsp_prod = __new;					\
-    mb(); /* front sees new responses /before/ we check rsp_event */	\
+    virt_mb(); /* front sees new responses /before/ we check rsp_event */	\
     (_notify) = ((RING_IDX)(__new - (_r)->sring->rsp_event) <		\
 		 (RING_IDX)(__new - __old));				\
 } while (0)
@@ -271,7 +271,7 @@ struct __name##_back_ring {						\
     (_work_to_do) = RING_HAS_UNCONSUMED_REQUESTS(_r);			\
     if (_work_to_do) break;						\
     (_r)->sring->req_event = (_r)->req_cons + 1;			\
-    mb();								\
+    virt_mb();								\
     (_work_to_do) = RING_HAS_UNCONSUMED_REQUESTS(_r);			\
 } while (0)
 
@@ -279,7 +279,7 @@ struct __name##_back_ring {						\
     (_work_to_do) = RING_HAS_UNCONSUMED_RESPONSES(_r);			\
     if (_work_to_do) break;						\
     (_r)->sring->rsp_event = (_r)->rsp_cons + 1;			\
-    mb();								\
+    virt_mb();								\
     (_work_to_do) = RING_HAS_UNCONSUMED_RESPONSES(_r);			\
 } while (0)
 
Signature · 1 line
MST
author maintainer
#254 lore raw

PATCH v2 34/34 xen/io: use virt_xxx barriers

In reply to Michael S. Tsirkin

include/xen/interface/io/ring.h uses
full memory barriers to communicate with the other side.

For guests compiled with CONFIG_SMP, smp_wmb and smp_mb would be sufficient, so mb() and wmb() here are only needed if a non-SMP guest runs on an SMP host.

Switch to virt_xxx barriers which serve this exact purpose.

---
 include/xen/interface/io/ring.h | 16 ++++++++--------
 1 file changed, 8 insertions(+), 8 deletions(-)
include/xen/interface/io/ring.h +8 -8
index 7dc685b..21f4fbd 100644
@@ -208,12 +208,12 @@ struct __name##_back_ring {						\
 
 
 #define RING_PUSH_REQUESTS(_r) do {					\
-    wmb(); /* back sees requests /before/ updated producer index */	\
+    virt_wmb(); /* back sees requests /before/ updated producer index */	\
     (_r)->sring->req_prod = (_r)->req_prod_pvt;				\
 } while (0)
 
 #define RING_PUSH_RESPONSES(_r) do {					\
-    wmb(); /* front sees responses /before/ updated producer index */	\
+    virt_wmb(); /* front sees responses /before/ updated producer index */	\
     (_r)->sring->rsp_prod = (_r)->rsp_prod_pvt;				\
 } while (0)
 
@@ -250,9 +250,9 @@ struct __name##_back_ring {						\
 #define RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(_r, _notify) do {		\
     RING_IDX __old = (_r)->sring->req_prod;				\
     RING_IDX __new = (_r)->req_prod_pvt;				\
-    wmb(); /* back sees requests /before/ updated producer index */	\
+    virt_wmb(); /* back sees requests /before/ updated producer index */	\
     (_r)->sring->req_prod = __new;					\
-    mb(); /* back sees new requests /before/ we check req_event */	\
+    virt_mb(); /* back sees new requests /before/ we check req_event */	\
     (_notify) = ((RING_IDX)(__new - (_r)->sring->req_event) <		\
 		 (RING_IDX)(__new - __old));				\
 } while (0)
@@ -260,9 +260,9 @@ struct __name##_back_ring {						\
 #define RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(_r, _notify) do {		\
     RING_IDX __old = (_r)->sring->rsp_prod;				\
     RING_IDX __new = (_r)->rsp_prod_pvt;				\
-    wmb(); /* front sees responses /before/ updated producer index */	\
+    virt_wmb(); /* front sees responses /before/ updated producer index */	\
     (_r)->sring->rsp_prod = __new;					\
-    mb(); /* front sees new responses /before/ we check rsp_event */	\
+    virt_mb(); /* front sees new responses /before/ we check rsp_event */	\
     (_notify) = ((RING_IDX)(__new - (_r)->sring->rsp_event) <		\
 		 (RING_IDX)(__new - __old));				\
 } while (0)
@@ -271,7 +271,7 @@ struct __name##_back_ring {						\
     (_work_to_do) = RING_HAS_UNCONSUMED_REQUESTS(_r);			\
     if (_work_to_do) break;						\
     (_r)->sring->req_event = (_r)->req_cons + 1;			\
-    mb();								\
+    virt_mb();								\
     (_work_to_do) = RING_HAS_UNCONSUMED_REQUESTS(_r);			\
 } while (0)
 
@@ -279,7 +279,7 @@ struct __name##_back_ring {						\
     (_work_to_do) = RING_HAS_UNCONSUMED_RESPONSES(_r);			\
     if (_work_to_do) break;						\
     (_r)->sring->rsp_event = (_r)->rsp_cons + 1;			\
-    mb();								\
+    virt_mb();								\
     (_work_to_do) = RING_HAS_UNCONSUMED_RESPONSES(_r);			\
 } while (0)
 
Signature · 1 line
MST
author maintainer
#255 lore raw

PATCH v2 34/34 xen/io: use virt_xxx barriers

In reply to Michael S. Tsirkin

include/xen/interface/io/ring.h uses
full memory barriers to communicate with the other side.

For guests compiled with CONFIG_SMP, smp_wmb and smp_mb would be sufficient, so mb() and wmb() here are only needed if a non-SMP guest runs on an SMP host.

Switch to virt_xxx barriers which serve this exact purpose.

---
 include/xen/interface/io/ring.h | 16 ++++++++--------
 1 file changed, 8 insertions(+), 8 deletions(-)
include/xen/interface/io/ring.h +8 -8
index 7dc685b..21f4fbd 100644
@@ -208,12 +208,12 @@ struct __name##_back_ring {						\
 
 
 #define RING_PUSH_REQUESTS(_r) do {					\
-    wmb(); /* back sees requests /before/ updated producer index */	\
+    virt_wmb(); /* back sees requests /before/ updated producer index */	\
     (_r)->sring->req_prod = (_r)->req_prod_pvt;				\
 } while (0)
 
 #define RING_PUSH_RESPONSES(_r) do {					\
-    wmb(); /* front sees responses /before/ updated producer index */	\
+    virt_wmb(); /* front sees responses /before/ updated producer index */	\
     (_r)->sring->rsp_prod = (_r)->rsp_prod_pvt;				\
 } while (0)
 
@@ -250,9 +250,9 @@ struct __name##_back_ring {						\
 #define RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(_r, _notify) do {		\
     RING_IDX __old = (_r)->sring->req_prod;				\
     RING_IDX __new = (_r)->req_prod_pvt;				\
-    wmb(); /* back sees requests /before/ updated producer index */	\
+    virt_wmb(); /* back sees requests /before/ updated producer index */	\
     (_r)->sring->req_prod = __new;					\
-    mb(); /* back sees new requests /before/ we check req_event */	\
+    virt_mb(); /* back sees new requests /before/ we check req_event */	\
     (_notify) = ((RING_IDX)(__new - (_r)->sring->req_event) <		\
 		 (RING_IDX)(__new - __old));				\
 } while (0)
@@ -260,9 +260,9 @@ struct __name##_back_ring {						\
 #define RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(_r, _notify) do {		\
     RING_IDX __old = (_r)->sring->rsp_prod;				\
     RING_IDX __new = (_r)->rsp_prod_pvt;				\
-    wmb(); /* front sees responses /before/ updated producer index */	\
+    virt_wmb(); /* front sees responses /before/ updated producer index */	\
     (_r)->sring->rsp_prod = __new;					\
-    mb(); /* front sees new responses /before/ we check rsp_event */	\
+    virt_mb(); /* front sees new responses /before/ we check rsp_event */	\
     (_notify) = ((RING_IDX)(__new - (_r)->sring->rsp_event) <		\
 		 (RING_IDX)(__new - __old));				\
 } while (0)
@@ -271,7 +271,7 @@ struct __name##_back_ring {						\
     (_work_to_do) = RING_HAS_UNCONSUMED_REQUESTS(_r);			\
     if (_work_to_do) break;						\
     (_r)->sring->req_event = (_r)->req_cons + 1;			\
-    mb();								\
+    virt_mb();								\
     (_work_to_do) = RING_HAS_UNCONSUMED_REQUESTS(_r);			\
 } while (0)
 
@@ -279,7 +279,7 @@ struct __name##_back_ring {						\
     (_work_to_do) = RING_HAS_UNCONSUMED_RESPONSES(_r);			\
     if (_work_to_do) break;						\
     (_r)->sring->rsp_event = (_r)->rsp_cons + 1;			\
-    mb();								\
+    virt_mb();								\
     (_work_to_do) = RING_HAS_UNCONSUMED_RESPONSES(_r);			\
 } while (0)
 
Signature · 1 line
MST
author maintainer
#256 lore raw

PATCH v2 32/32 virtio_ring: use virt_store_mb

In reply to Michael S. Tsirkin

We need a full barrier after writing out event index, using virt_store_mb there seems better than open-coding. As usual, we need a wrapper to account for strong barriers.

It's tempting to use this in vhost as well, for that, we'll need a variant of smp_store_mb that works on __user pointers.

---
 include/linux/virtio_ring.h  | 12 ++++++++++++
 drivers/virtio/virtio_ring.c | 15 +++++++++------
 2 files changed, 21 insertions(+), 6 deletions(-)
include/linux/virtio_ring.h +12 -0
index f3fa55b..3a74d91 100644
@@ -45,6 +45,18 @@ static inline void virtio_wmb(bool weak_barriers)
 		wmb();
 }
 
+static inline void virtio_store_mb(bool weak_barriers,
+				   __virtio16 *p, __virtio16 v)
+{
+	if (weak_barriers)
+		virt_store_mb(*p, v);
+	else
+	{
+		WRITE_ONCE(*p, v);
+		mb();
+	}
+}
+
 struct virtio_device;
 struct virtqueue;
 
drivers/virtio/virtio_ring.c +9 -6
index ee663c4..e12e385 100644
@@ -517,10 +517,10 @@ void *virtqueue_get_buf(struct virtqueue *_vq, unsigned int *len)
 	/* If we expect an interrupt for the next entry, tell host
 	 * by writing event index and flush out the write before
 	 * the read in the next get_buf call. */
-	if (!(vq->avail_flags_shadow & VRING_AVAIL_F_NO_INTERRUPT)) {
-		vring_used_event(&vq->vring) = cpu_to_virtio16(_vq->vdev, vq->last_used_idx);
-		virtio_mb(vq->weak_barriers);
-	}
+	if (!(vq->avail_flags_shadow & VRING_AVAIL_F_NO_INTERRUPT))
+		virtio_store_mb(vq->weak_barriers,
+				&vring_used_event(&vq->vring),
+				cpu_to_virtio16(_vq->vdev, vq->last_used_idx));
 
 #ifdef DEBUG
 	vq->last_add_time_valid = false;
@@ -653,8 +653,11 @@ bool virtqueue_enable_cb_delayed(struct virtqueue *_vq)
 	}
 	/* TODO: tune this threshold */
 	bufs = (u16)(vq->avail_idx_shadow - vq->last_used_idx) * 3 / 4;
-	vring_used_event(&vq->vring) = cpu_to_virtio16(_vq->vdev, vq->last_used_idx + bufs);
-	virtio_mb(vq->weak_barriers);
+
+	virtio_store_mb(vq->weak_barriers,
+			&vring_used_event(&vq->vring),
+			cpu_to_virtio16(_vq->vdev, vq->last_used_idx + bufs));
+
 	if (unlikely((u16)(virtio16_to_cpu(_vq->vdev, vq->vring.used->idx) - vq->last_used_idx) > bufs)) {
 		END_USE(vq);
 		return false;
Signature · 1 line
MST