Skip to contentSkip to conversation

xen/arm: fix affected memory range by dcache clean functions

PATCH In review Stefano Stabellini 3 participants 3 messages Mar 3, 2017 – Mar 4, 2017

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 xen/arm: fix affected memory range by dcache clean functions

clean_dcache_va_range and clean_and_invalidate_dcache_va_range don't calculate the range correctly when "end" is not cacheline aligned. As a result, the last cacheline is not skipped. Fix the issue by aligning the start address to the cacheline size.

In addition, make the code simpler and faster in invalidate_dcache_va_range, by removing the module operation and using bitmasks instead.

---
 xen/include/asm-arm/page.h | 24 +++++++++++-------------
 1 file changed, 11 insertions(+), 13 deletions(-)
xen/include/asm-arm/page.h +11 -13
index 86de0b6..4b46e88 100644
@@ -291,24 +291,20 @@ extern size_t cacheline_bytes;
 
 static inline int invalidate_dcache_va_range(const void *p, unsigned long size)
 {
-    size_t off;
     const void *end = p + size;
+    size_t cacheline_mask = cacheline_bytes - 1;
 
     dsb(sy);           /* So the CPU issues all writes to the range */
 
-    off = (unsigned long)p % cacheline_bytes;
-    if ( off )
+    if ( (uintptr_t)p & cacheline_mask )
     {
-        p -= off;
+        p = (void *)((uintptr_t)p & ~cacheline_mask);
         asm volatile (__clean_and_invalidate_dcache_one(0) : : "r" (p));
         p += cacheline_bytes;
-        size -= cacheline_bytes - off;
     }
-    off = (unsigned long)end % cacheline_bytes;
-    if ( off )
+    if ( (uintptr_t)end & cacheline_mask )
     {
-        end -= off;
-        size -= off;
+        end = (void *)((uintptr_t)end & ~cacheline_mask);
         asm volatile (__clean_and_invalidate_dcache_one(0) : : "r" (end));
     }
 
@@ -322,9 +318,10 @@ static inline int invalidate_dcache_va_range(const void *p, unsigned long size)
 
 static inline int clean_dcache_va_range(const void *p, unsigned long size)
 {
-    const void *end;
+    const void *end = p + size;
     dsb(sy);           /* So the CPU issues all writes to the range */
-    for ( end = p + size; p < end; p += cacheline_bytes )
+    p = (void *)((uintptr_t)p & ~(cacheline_bytes - 1));
+    for ( ; p < end; p += cacheline_bytes )
         asm volatile (__clean_dcache_one(0) : : "r" (p));
     dsb(sy);           /* So we know the flushes happen before continuing */
     /* ARM callers assume that dcache_* functions cannot fail. */
@@ -334,9 +331,10 @@ static inline int clean_dcache_va_range(const void *p, unsigned long size)
 static inline int clean_and_invalidate_dcache_va_range
     (const void *p, unsigned long size)
 {
-    const void *end;
+    const void *end = p + size;
     dsb(sy);         /* So the CPU issues all writes to the range */
-    for ( end = p + size; p < end; p += cacheline_bytes )
+    p = (void *)((uintptr_t)p & ~(cacheline_bytes - 1));
+    for ( ; p < end; p += cacheline_bytes )
         asm volatile (__clean_and_invalidate_dcache_one(0) : : "r" (p));
     dsb(sy);         /* So we know the flushes happen before continuing */
     /* ARM callers assume that dcache_* functions cannot fail. */
Signature · 7 lines
1.9.1


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
Reviewed-by Tested-by on v1 1/1
#1 lore raw
Quoted 5 lines · Stefano Stabellini

clean_dcache_va_range and clean_and_invalidate_dcache_va_range don't calculate the range correctly when "end" is not cacheline aligned. As a result, the last cacheline is not skipped. Fix the issue by aligning the start address to the cacheline size.

Quoted 7 lines

In addition, make the code simpler and faster in invalidate_dcache_va_range, by removing the module operation and using bitmasks instead.

This looks good to me and it works on my side.
In the future, perhaps we could convert invalidate_dcache_va_range to use a smaller implementation like clean_dcache_va_range().

Anyway:

And super thanks for helping me resolve this issue!

Cheers,
Edgar

Quoted 67 lines
---
 xen/include/asm-arm/page.h | 24 +++++++++++-------------
 1 file changed, 11 insertions(+), 13 deletions(-)
xen/include/asm-arm/page.h +11 -13
index 86de0b6..4b46e88 100644
@@ -291,24 +291,20 @@ extern size_t cacheline_bytes;
 
 static inline int invalidate_dcache_va_range(const void *p, unsigned long size)
 {
-    size_t off;
     const void *end = p + size;
+    size_t cacheline_mask = cacheline_bytes - 1;
 
     dsb(sy);           /* So the CPU issues all writes to the range */
 
-    off = (unsigned long)p % cacheline_bytes;
-    if ( off )
+    if ( (uintptr_t)p & cacheline_mask )
     {
-        p -= off;
+        p = (void *)((uintptr_t)p & ~cacheline_mask);
         asm volatile (__clean_and_invalidate_dcache_one(0) : : "r" (p));
         p += cacheline_bytes;
-        size -= cacheline_bytes - off;
     }
-    off = (unsigned long)end % cacheline_bytes;
-    if ( off )
+    if ( (uintptr_t)end & cacheline_mask )
     {
-        end -= off;
-        size -= off;
+        end = (void *)((uintptr_t)end & ~cacheline_mask);
         asm volatile (__clean_and_invalidate_dcache_one(0) : : "r" (end));
     }
 
@@ -322,9 +318,10 @@ static inline int invalidate_dcache_va_range(const void *p, unsigned long size)
 
 static inline int clean_dcache_va_range(const void *p, unsigned long size)
 {
-    const void *end;
+    const void *end = p + size;
     dsb(sy);           /* So the CPU issues all writes to the range */
-    for ( end = p + size; p < end; p += cacheline_bytes )
+    p = (void *)((uintptr_t)p & ~(cacheline_bytes - 1));
+    for ( ; p < end; p += cacheline_bytes )
         asm volatile (__clean_dcache_one(0) : : "r" (p));
     dsb(sy);           /* So we know the flushes happen before continuing */
     /* ARM callers assume that dcache_* functions cannot fail. */
@@ -334,9 +331,10 @@ static inline int clean_dcache_va_range(const void *p, unsigned long size)
 static inline int clean_and_invalidate_dcache_va_range
     (const void *p, unsigned long size)
 {
-    const void *end;
+    const void *end = p + size;
     dsb(sy);         /* So the CPU issues all writes to the range */
-    for ( end = p + size; p < end; p += cacheline_bytes )
+    p = (void *)((uintptr_t)p & ~(cacheline_bytes - 1));
+    for ( ; p < end; p += cacheline_bytes )
         asm volatile (__clean_and_invalidate_dcache_one(0) : : "r" (p));
     dsb(sy);         /* So we know the flushes happen before continuing */
     /* ARM callers assume that dcache_* functions cannot fail. */
Signature · 1 line
1.9.1

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

Reviewed-by on v1 1/1
#2 lore raw
In reply to Stefano Stabellini

Hi Stefano,

Quoted 29 lines · Stefano Stabellini

clean_dcache_va_range and clean_and_invalidate_dcache_va_range don't calculate the range correctly when "end" is not cacheline aligned. As a result, the last cacheline is not skipped. Fix the issue by aligning the start address to the cacheline size.

In addition, make the code simpler and faster in invalidate_dcache_va_range, by removing the module operation and using bitmasks instead.

---
 xen/include/asm-arm/page.h | 24 +++++++++++-------------
 1 file changed, 11 insertions(+), 13 deletions(-)
xen/include/asm-arm/page.h +1 -1
index 86de0b6..4b46e88 100644
@@ -291,24 +291,20 @@ extern size_t cacheline_bytes;
 
 static inline int invalidate_dcache_va_range(const void *p, unsigned long size)
 {
-    size_t off;
     const void *end = p + size;
+    size_t cacheline_mask = cacheline_bytes - 1;
 
     dsb(sy);           /* So the CPU issues all writes to the range */

xen/include/asm-arm/page.h extern size_t cacheline_bytes; · line 305

Quoted 10 lines
xen/include/asm-arm/page.h +2 -4
 
-    off = (unsigned long)p % cacheline_bytes;
-    if ( off )
+    if ( (uintptr_t)p & cacheline_mask )
     {
-        p -= off;
+        p = (void *)((uintptr_t)p & ~cacheline_mask);
         asm volatile (__clean_and_invalidate_dcache_one(0) : : "r" (p));
         p += cacheline_bytes;
-        size -= cacheline_bytes - off;

It would have been nice to explain in the commit message that you removed the adjustment of the size because the variable is not used later on.

With that:

Cheers,

Signature · 6 lines
Julien Grall

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