Skip to contentSkip to conversation

util/cacheflush: Avoid flushing dcache twice when not necessary

PATCH In review Philippe Mathieu-Daudé 3 participants 5 messages Jun 5, 2023

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 util/cacheflush: Avoid flushing dcache twice when not necessary

<libkern/OSCacheControl.h> describes sys_icache_invalidate() as "equivalent to sys_cache_control(kCacheFunctionPrepareForExecution)", having kCacheFunctionPrepareForExecution defined as:

/* Prepare memory for execution. This should be called
* after writing machine instructions to memory, before
* executing them. It syncs the dcache and icache. [...] */

Since the dcache is also sync'd, we can avoid the sys_dcache_flush() call when both rx/rw pointers are equal.

---
Based-on: <[email protected]>

---
 util/cacheflush.c | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)
util/cacheflush.c +8 -1
index de35616718..a08906155a 100644
@@ -241,7 +241,14 @@ static void __attribute__((constructor)) init_cache_info(void)
 
 void flush_idcache_range(uintptr_t rx, uintptr_t rw, size_t len)
 {
-    sys_dcache_flush((void *)rw, len);
+    if (rx == rw) {
+        /*
+         * sys_icache_invalidate() syncs the dcache and icache,
+         * so no need to call sys_dcache_flush().
+         */
+    } else {
+        sys_dcache_flush((void *)rw, len);
+    }
     sys_icache_invalidate((void *)rx, len);
 }
 #else
Signature · 1 line
2.38.1
#1 lore raw

Subject: "Avoid flushing dcache twice [on Darwin] when not necessary"

Quoted 40 lines · Philippe Mathieu-Daudé

<libkern/OSCacheControl.h> describes sys_icache_invalidate() as "equivalent to sys_cache_control(kCacheFunctionPrepareForExecution)", having kCacheFunctionPrepareForExecution defined as:

/* Prepare memory for execution. This should be called
* after writing machine instructions to memory, before
* executing them. It syncs the dcache and icache. [...] */

Since the dcache is also sync'd, we can avoid the sys_dcache_flush() call when both rx/rw pointers are equal.

---
Based-on: <[email protected]>

---
  util/cacheflush.c | 9 ++++++++-
  1 file changed, 8 insertions(+), 1 deletion(-)
util/cacheflush.c +8 -1
index de35616718..a08906155a 100644
@@ -241,7 +241,14 @@ static void __attribute__((constructor)) init_cache_info(void)
 
  void flush_idcache_range(uintptr_t rx, uintptr_t rw, size_t len)
  {
-    sys_dcache_flush((void *)rw, len);
+    if (rx == rw) {
+        /*
+         * sys_icache_invalidate() syncs the dcache and icache,
+         * so no need to call sys_dcache_flush().
+         */
+    } else {
+        sys_dcache_flush((void *)rw, len);
+    }
      sys_icache_invalidate((void *)rx, len);
  }
  #else
Reviewed-by on v1 1/1
#2 lore raw
In reply to Philippe Mathieu-Daudé
Quoted 12 lines · Philippe Mathieu-Daudé

<libkern/OSCacheControl.h> describes sys_icache_invalidate() as "equivalent to sys_cache_control(kCacheFunctionPrepareForExecution)", having kCacheFunctionPrepareForExecution defined as:

/* Prepare memory for execution. This should be called
* after writing machine instructions to memory, before
* executing them. It syncs the dcache and icache. [...] */

Since the dcache is also sync'd, we can avoid the sys_dcache_flush() call when both rx/rw pointers are equal.

Quoted 7 lines

---
Based-on:<[email protected]>

---
  util/cacheflush.c | 9 ++++++++-
  1 file changed, 8 insertions(+), 1 deletion(-)

r~

#3 lore raw
In reply to Philippe Mathieu-Daudé
Quoted 25 lines · Philippe Mathieu-Daudé

<libkern/OSCacheControl.h> describes sys_icache_invalidate() as "equivalent to sys_cache_control(kCacheFunctionPrepareForExecution)", having kCacheFunctionPrepareForExecution defined as:

/* Prepare memory for execution. This should be called
* after writing machine instructions to memory, before
* executing them. It syncs the dcache and icache. [...] */

Since the dcache is also sync'd, we can avoid the sys_dcache_flush() call when both rx/rw pointers are equal.

---
Based-on: <[email protected]>
---
util/cacheflush.c | 9 ++++++++-

1 file changed, 8 insertions(+), 1 deletion(-)
util/cacheflush.c +0 -0
index de35616718..a08906155a 100644

util/cacheflush.c static void __attribute__((constructor)) init_cache_info(void) · line 244

Quoted 5 lines
util/cacheflush.c +1 -1
@@ -241,7 +241,14 @@ static void __attribute__((constructor)) init_cache_info(void)
 
 void flush_idcache_range(uintptr_t rx, uintptr_t rw, size_t len)
 {
-    sys_dcache_flush((void *)rw, len);
+    if (rx == rw) {

Isn't it more straight forward to use rx != rw and drop the else branch than having an empty if branch? You can still keep the comment above the if to explain it if needed.

Regards,
BALATON Zoltan

Quoted 11 lines
util/cacheflush.c +7 -0
+        /*
+         * sys_icache_invalidate() syncs the dcache and icache,
+         * so no need to call sys_dcache_flush().
+         */
+    } else {
+        sys_dcache_flush((void *)rw, len);
+    }
    sys_icache_invalidate((void *)rx, len);
 }
 #else
#4 lore raw
Quoted 28 lines · BALATON Zoltan
Quoted 27 lines · Philippe Mathieu-Daudé

<libkern/OSCacheControl.h> describes sys_icache_invalidate() as "equivalent to sys_cache_control(kCacheFunctionPrepareForExecution)", having kCacheFunctionPrepareForExecution defined as:

 /* Prepare memory for execution.  This should be called
  * after writing machine instructions to memory, before
  * executing them.  It syncs the dcache and icache. [...] */

Since the dcache is also sync'd, we can avoid the sys_dcache_flush() call when both rx/rw pointers are equal.

---
Based-on: <[email protected]>
---
util/cacheflush.c | 9 ++++++++-

1 file changed, 8 insertions(+), 1 deletion(-)
util/cacheflush.c +0 -0
index de35616718..a08906155a 100644
@@ -241,7 +241,14 @@ static void __attribute__((constructor)) 
 init_cache_info(void)
 
Quoted 6 lines

util/cacheflush.c static void __attribute__((constructor)) init_cache_info(void) · line 244

Quoted 4 lines
util/cacheflush.c +1 -1
 void flush_idcache_range(uintptr_t rx, uintptr_t rw, size_t len)
 {
-    sys_dcache_flush((void *)rw, len);
+    if (rx == rw) {

Isn't it more straight forward to use rx != rw and drop the else branch than having an empty if branch? You can still keep the comment above the if to explain it if needed.

I tried that first but found it was not obvious, so chose this form because it seemed clearer to me.