Skip to contentSkip to conversation

arm64: Fix kcsan test_barrier fail and panic

PATCH Superseded Kefeng Wang 2 participants 6 messages May 20, 2022

Subsystems (4)

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

v2 cover arm64: Fix kcsan test_barrier fail and panic

Fix selftest and kcsan_test() module fail when KCSAN_STRICT and KCSAN_WEAK_MEMORY enabled on ARM64.

v2:
- Add documents about dma_mb(), suggested Mike and Will.
- drop Fixes tag and update changlog, suggested Mike.

Kefeng Wang (2):
Documentation/barriers: Add memory barrier dma_mb()
arm64: kcsan: Support detecting more missing memory barriers

 Documentation/memory-barriers.txt |  5 ++++-
 arch/arm64/include/asm/barrier.h  | 12 ++++++------
 include/asm-generic/barrier.h     |  4 ++++
 3 files changed, 14 insertions(+), 7 deletions(-)
Signature · 1 line
2.35.3
#1 lore raw

PATCH v2 1/2 Documentation/barriers: Add memory barrier dma_mb()

The memory barrier dma_mb() is introduced by commit a76a37777f2c ("iommu/arm-smmu-v3: Ensure queue is read after updating prod pointer"), which is used to ensure that prior (both reads and writes) accesses to memory by a CPU are ordered w.r.t. a subsequent MMIO write.

---
 Documentation/memory-barriers.txt | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
Documentation/memory-barriers.txt +4 -1
index b12df9137e1c..1eabcc0e4eca 100644
@@ -1894,10 +1894,13 @@ There are some more advanced barrier functions:
 
  (*) dma_wmb();
  (*) dma_rmb();
+ (*) dma_mb();
 
      These are for use with consistent memory to guarantee the ordering
      of writes or reads of shared memory accessible to both the CPU and a
-     DMA capable device.
+     DMA capable device, in the case of ensure the prior (both reads and
+     writes) accesses to memory by a CPU are ordered w.r.t. a subsequent
+     MMIO write, dma_mb().
 
      For example, consider a device driver that shares memory with a device
      and uses a descriptor status value to indicate if the descriptor belongs
Signature · 1 line
2.35.3
#2 lore raw

Re: [PATCH v2 1/2] Documentation/barriers: Add memory barrier dma_mb()

Quoted 17 lines · Kefeng Wang

The memory barrier dma_mb() is introduced by commit a76a37777f2c ("iommu/arm-smmu-v3: Ensure queue is read after updating prod pointer"), which is used to ensure that prior (both reads and writes) accesses to memory by a CPU are ordered w.r.t. a subsequent MMIO write.

---
 Documentation/memory-barriers.txt | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
Documentation/memory-barriers.txt +0 -0
index b12df9137e1c..1eabcc0e4eca 100644
@@ -1894,10 +1894,13 @@ There are some more advanced barrier functions:
 
  (*) dma_wmb();

Documentation/memory-barriers.txt There are some more advanced barrier functions: · line 1904

Quoted 10 lines
Documentation/memory-barriers.txt +4 -1
  (*) dma_rmb();
+ (*) dma_mb();
 
      These are for use with consistent memory to guarantee the ordering
      of writes or reads of shared memory accessible to both the CPU and a
-     DMA capable device.
+     DMA capable device, in the case of ensure the prior (both reads and
+     writes) accesses to memory by a CPU are ordered w.r.t. a subsequent
+     MMIO write, dma_mb().
 

I think this is out of place; this explanation here is not yet elaborating on either. Elaboration on dma_mb() should go where dma_rmb() and dma_wmb() are explained.

Something like this:

------ >8 ------

Documentation/memory-barriers.txt +6 -5
index b12df9137e1c..fb322b6cce70 100644
@@ -1894,6 +1894,7 @@ There are some more advanced barrier functions:
 
  (*) dma_wmb();
  (*) dma_rmb();
+ (*) dma_mb();
 
      These are for use with consistent memory to guarantee the ordering
      of writes or reads of shared memory accessible to both the CPU and a
@@ -1925,11 +1926,11 @@ There are some more advanced barrier functions:
      The dma_rmb() allows us guarantee the device has released ownership
      before we read the data from the descriptor, and the dma_wmb() allows
      us to guarantee the data is written to the descriptor before the device
-     can see it now has ownership.  Note that, when using writel(), a prior
-     wmb() is not needed to guarantee that the cache coherent memory writes
-     have completed before writing to the MMIO region.  The cheaper
-     writel_relaxed() does not provide this guarantee and must not be used
-     here.
+     can see it now has ownership.  The dma_mb() implies both a dma_rmb() and a
+     dma_wmb().  Note that, when using writel(), a prior wmb() is not needed to
+     guarantee that the cache coherent memory writes have completed before
+     writing to the MMIO region.  The cheaper writel_relaxed() does not provide
+     this guarantee and must not be used here.
 
      See the subsection "Kernel I/O barrier effects" for more information on
      relaxed I/O accessors and the Documentation/core-api/dma-api.rst file for

------ >8 ------

Also, now that you're making dma_mb() part of the official API, it might need a generic definition in include/asm-generic/barrier.h, because as-is it's only available in arm64 builds.

Thoughts?

Thanks,
-- Marco

#3 lore raw

Re: [PATCH v2 1/2] Documentation/barriers: Add memory barrier dma_mb()

Quoted 57 lines · Marco Elver
Quoted 17 lines · Kefeng Wang

The memory barrier dma_mb() is introduced by commit a76a37777f2c ("iommu/arm-smmu-v3: Ensure queue is read after updating prod pointer"), which is used to ensure that prior (both reads and writes) accesses to memory by a CPU are ordered w.r.t. a subsequent MMIO write.

---
  Documentation/memory-barriers.txt | 5 ++++-
  1 file changed, 4 insertions(+), 1 deletion(-)
Documentation/memory-barriers.txt +0 -0
index b12df9137e1c..1eabcc0e4eca 100644
@@ -1894,10 +1894,13 @@ There are some more advanced barrier functions:
 
   (*) dma_wmb();

Documentation/memory-barriers.txt There are some more advanced barrier functions: · line 1904

Quoted 10 lines
Documentation/memory-barriers.txt +4 -1
   (*) dma_rmb();
+ (*) dma_mb();
 
       These are for use with consistent memory to guarantee the ordering
       of writes or reads of shared memory accessible to both the CPU and a
-     DMA capable device.
+     DMA capable device, in the case of ensure the prior (both reads and
+     writes) accesses to memory by a CPU are ordered w.r.t. a subsequent
+     MMIO write, dma_mb().
 

I think this is out of place; this explanation here is not yet elaborating on either. Elaboration on dma_mb() should go where dma_rmb() and dma_wmb() are explained.

Something like this:

------ >8 ------

Documentation/memory-barriers.txt +1 -5
index b12df9137e1c..fb322b6cce70 100644
@@ -1894,6 +1894,7 @@ There are some more advanced barrier functions:
 
   (*) dma_wmb();
   (*) dma_rmb();
+ (*) dma_mb();
 
       These are for use with consistent memory to guarantee the ordering
       of writes or reads of shared memory accessible to both the CPU and a
@@ -1925,11 +1926,11 @@ There are some more advanced barrier functions:
       The dma_rmb() allows us guarantee the device has released ownership
       before we read the data from the descriptor, and the dma_wmb() allows
       us to guarantee the data is written to the descriptor before the device
-     can see it now has ownership.  Note that, when using writel(), a prior
-     wmb() is not needed to guarantee that the cache coherent memory writes
-     have completed before writing to the MMIO region.  The cheaper
-     writel_relaxed() does not provide this guarantee and must not be used
-     here.

Documentation/memory-barriers.txt There are some more advanced barrier functions:

Quoted 10 lines
Documentation/memory-barriers.txt +5 -1
+     can see it now has ownership.  The dma_mb() implies both a dma_rmb() and a
+     dma_wmb().  Note that, when using writel(), a prior wmb() is not needed to
+     guarantee that the cache coherent memory writes have completed before
+     writing to the MMIO region.  The cheaper writel_relaxed() does not provide
+     this guarantee and must not be used here.
 
       See the subsection "Kernel I/O barrier effects" for more information on
       relaxed I/O accessors and the Documentation/core-api/dma-api.rst file for
 
------ >8 ------

Thanks, will use above explanation.

Quoted 3 lines

Also, now that you're making dma_mb() part of the official API, it might need a generic definition in include/asm-generic/barrier.h, because as-is it's only available in arm64 builds.

Ok, it's good to add the dma_mb() and __dma_mb  definition with a separate patch

into include/asm-generic/barrier.h.

Quoted 6 lines

Thoughts?

Thanks,
-- Marco
.

#4 lore raw

PATCH v2 2/2 arm64: kcsan: Support detecting more missing memory barriers

In reply to Kefeng Wang

As "kcsan: Support detecting a subset of missing memory barriers"[1] introduced KCSAN_STRICT/KCSAN_WEAK_MEMORY which make kcsan detects more missing memory barrier, but arm64 don't have KCSAN instrumentation for barriers, so the new selftest test_barrier() and test cases for memory barrier instrumentation in kcsan_test module will fail, even panic on selftest.

Let's prefix all barriers with __ on arm64, as asm-generic/barriers.h defined the final instrumented version of these barriers, which will fix the above issues.

Note, barrier instrumentation that can be disabled via __no_kcsan with appropriate compiler-support (and not just with objtool help), see commit bd3d5bd1a0ad ("kcsan: Support WEAK_MEMORY with Clang where no objtool support exists"), it adds disable_sanitizer_instrumentation to __no_kcsan attribute which will remove all sanitizer instrumentation fully (with Clang 14.0). Meanwhile, GCC does the same thing with no_sanitize.

[1] https://lore.kernel.org/linux-mm/[email protected]/

---
 arch/arm64/include/asm/barrier.h | 12 ++++++------
 include/asm-generic/barrier.h    |  4 ++++
 2 files changed, 10 insertions(+), 6 deletions(-)
arch/arm64/include/asm/barrier.h +6 -6
index 9f3e2c3d2ca0..2cfc4245d2e2 100644
@@ -50,13 +50,13 @@
 #define pmr_sync()	do {} while (0)
 #endif
 
-#define mb()		dsb(sy)
-#define rmb()		dsb(ld)
-#define wmb()		dsb(st)
+#define __mb()		dsb(sy)
+#define __rmb()		dsb(ld)
+#define __wmb()		dsb(st)
 
-#define dma_mb()	dmb(osh)
-#define dma_rmb()	dmb(oshld)
-#define dma_wmb()	dmb(oshst)
+#define __dma_mb()	dmb(osh)
+#define __dma_rmb()	dmb(oshld)
+#define __dma_wmb()	dmb(oshst)
 
 #define io_stop_wc()	dgh()
 
include/asm-generic/barrier.h +4 -0
index fd7e8fbaeef1..18863c50e9ce 100644
@@ -38,6 +38,10 @@
 #define wmb()	do { kcsan_wmb(); __wmb(); } while (0)
 #endif
 
+#ifdef __dma_mb
+#define dma_mb()	do { kcsan_mb(); __dma_mb(); } while (0)
+#endif
+
 #ifdef __dma_rmb
 #define dma_rmb()	do { kcsan_rmb(); __dma_rmb(); } while (0)
 #endif
Signature · 1 line
2.35.3
#5 lore raw

Re: [PATCH v2 2/2] arm64: kcsan: Support detecting more missing memory barriers

Quoted 54 lines · Kefeng Wang

As "kcsan: Support detecting a subset of missing memory barriers"[1] introduced KCSAN_STRICT/KCSAN_WEAK_MEMORY which make kcsan detects more missing memory barrier, but arm64 don't have KCSAN instrumentation for barriers, so the new selftest test_barrier() and test cases for memory barrier instrumentation in kcsan_test module will fail, even panic on selftest.

Let's prefix all barriers with __ on arm64, as asm-generic/barriers.h defined the final instrumented version of these barriers, which will fix the above issues.

Note, barrier instrumentation that can be disabled via __no_kcsan with appropriate compiler-support (and not just with objtool help), see commit bd3d5bd1a0ad ("kcsan: Support WEAK_MEMORY with Clang where no objtool support exists"), it adds disable_sanitizer_instrumentation to __no_kcsan attribute which will remove all sanitizer instrumentation fully (with Clang 14.0). Meanwhile, GCC does the same thing with no_sanitize.

[1] https://lore.kernel.org/linux-mm/[email protected]/

---
 arch/arm64/include/asm/barrier.h | 12 ++++++------
 include/asm-generic/barrier.h    |  4 ++++
 2 files changed, 10 insertions(+), 6 deletions(-)
arch/arm64/include/asm/barrier.h +6 -6
index 9f3e2c3d2ca0..2cfc4245d2e2 100644
@@ -50,13 +50,13 @@
 #define pmr_sync()	do {} while (0)
 #endif
 
-#define mb()		dsb(sy)
-#define rmb()		dsb(ld)
-#define wmb()		dsb(st)
+#define __mb()		dsb(sy)
+#define __rmb()		dsb(ld)
+#define __wmb()		dsb(st)
 
-#define dma_mb()	dmb(osh)
-#define dma_rmb()	dmb(oshld)
-#define dma_wmb()	dmb(oshst)
+#define __dma_mb()	dmb(osh)
+#define __dma_rmb()	dmb(oshld)
+#define __dma_wmb()	dmb(oshst)
 
 #define io_stop_wc()	dgh()
 
include/asm-generic/barrier.h +0 -0
index fd7e8fbaeef1..18863c50e9ce 100644

include/asm-generic/barrier.h line 43

Quoted 6 lines
include/asm-generic/barrier.h +3 -0
@@ -38,6 +38,10 @@
 #define wmb()	do { kcsan_wmb(); __wmb(); } while (0)
 #endif
 
+#ifdef __dma_mb
+#define dma_mb()	do { kcsan_mb(); __dma_mb(); } while (0)
+#endif

The asm-generic changes might want to go into a separate patch, along with the generic definition of dma_mb() if there's no __dma_mb, as mentioned in the other email.

Thanks,
-- Marco