util/cacheflush: Avoid flushing dcache twice when not necessary
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
@@ -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
Subject: "Avoid flushing dcache twice [on Darwin] when not necessary"
Quoted 40 lines · Philippe Mathieu-Daudé
<
libkern/OSCacheControl.h> describessys_icache_invalidate()as "equivalent tosys_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.Suggested-by Richard Henderson <[email protected]>Signed-off-by Philippe Mathieu-Daudé <[email protected]>---
Based-on: <[email protected]>--- util/cacheflush.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-)util/cacheflush.c +8
-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) { +/* +* 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
Quoted 12 lines · Philippe Mathieu-Daudé
<
libkern/OSCacheControl.h> describessys_icache_invalidate()as "equivalent tosys_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
Suggested-by Richard Henderson<[email protected]>Signed-off-by Philippe Mathieu-Daudé<[email protected]>---
Based-on:<[email protected]>--- util/cacheflush.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-)
r~
Quoted 25 lines · Philippe Mathieu-Daudé
<
libkern/OSCacheControl.h> describessys_icache_invalidate()as "equivalent tosys_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.Suggested-by Richard Henderson <[email protected]>Signed-off-by Philippe Mathieu-Daudé <[email protected]>---
Based-on: <[email protected]>
---util/cacheflush.c| 9 ++++++++-1 file changed, 8 insertions(+), 1 deletion(-)util/cacheflush.c +0
-0
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
Quoted 28 lines · BALATON Zoltan
Quoted 27 lines · Philippe Mathieu-Daudé
<
libkern/OSCacheControl.h> describessys_icache_invalidate()as "equivalent tosys_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.Suggested-by Richard Henderson <[email protected]>Signed-off-by Philippe Mathieu-Daudé <[email protected]>---
Based-on: <[email protected]>
---util/cacheflush.c| 9 ++++++++-1 file changed, 8 insertions(+), 1 deletion(-)util/cacheflush.c +0
-0@@ -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
-1void 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.