x86/lib: make clean_cache_range() zero-size safe
PATCH 1/1 x86/lib: make clean_cache_range() zero-size safe
clean_cache_range() writes back each cache line in the range [addr, addr + size). A zero-size range is empty and should not perform any cache maintenance operation.
As pointed out by Sashiko [1], clean_cache_range(addr, 0) currently can still execute one CLWB when addr is not cache-line aligned. With size 0, vend is equal to addr. However, the loop starts from the cacheline-aligned address containing addr. If addr is not cacheline aligned, that rounded-down start is below vend, so the loop can execute one CLWB even though the requested range is empty.
That gives zero-size callers observable side effects. For example, arch_wb_cache_pmem(addr, 0) should not write back any cache line, and memcpy_flushcache(dst, src, 0) should preserve the usual zero-length copy semantics. If the rounded-down line is not mapped, the stray CLWB can also fault.
Return immediately from clean_cache_range() for size 0.
[1] https://sashiko.dev/#/patchset/[email protected]
--- arch/x86/lib/usercopy_64.c | 3 +++ 1 file changed, 3 insertions(+)
arch/x86/lib/usercopy_64.c +3 -0
@@ -32,6 +32,9 @@ static void clean_cache_range(void *addr, size_t size)void *vend = addr + size;void *p; +if (!size) +return ; +for (p = (void *)((unsigned long )addr & ~clflush_mask); p < vend; p += x86_clflush_size)clwb (p);
Signature · 1 line
2.20.1
Quoted 11 lines · On Thu, 3 Sep 2026 15:11:25 +0800 "Li Zhe" <[email protected]> wrote:
clean_cache_range()writes back each cache line in the range [addr, addr + size). A zero-size range is empty and should not perform any cache maintenance operation.As pointed out by Sashiko [1],
clean_cache_range(addr, 0)currently can still execute one CLWB when addr is not cache-line aligned. With size 0, vend is equal to addr. However, the loop starts from the cacheline-aligned address containing addr. If addr is not cacheline aligned, that rounded-down start is below vend, so the loop can execute one CLWB even though the requested range is empty.
Quoted 7 lines
That gives zero-size callers observable side effects. For example,
arch_wb_cache_pmem(addr, 0)should not write back any cache line, andmemcpy_flushcache(dst, src, 0)should preserve the usual zero-length copy semantics. If the rounded-down line is not mapped, the stray CLWB can also fault.Return immediately from
clean_cache_range()for size 0.
Thanks. Sashiko might have found a few similar issues in there:
https://sashiko.dev/#/patchset/[email protected]
Quoted 19 lines · Andrew Morton
Quoted 17 lines · On Thu, 3 Sep 2026 15:11:25 +0800 "Li Zhe" <[email protected]> wrote:
clean_cache_range()writes back each cache line in the range [addr, addr + size). A zero-size range is empty and should not perform any cache maintenance operation.As pointed out by Sashiko [1],
clean_cache_range(addr, 0)currently can still execute one CLWB when addr is not cache-line aligned. With size 0, vend is equal to addr. However, the loop starts from the cacheline-aligned address containing addr. If addr is not cacheline aligned, that rounded-down start is below vend, so the loop can execute one CLWB even though the requested range is empty.That gives zero-size callers observable side effects. For example,
arch_wb_cache_pmem(addr, 0)should not write back any cache line, andmemcpy_flushcache(dst, src, 0)should preserve the usual zero-length copy semantics. If the rounded-down line is not mapped, the stray CLWB can also fault.
Quoted 3 lines
Quoted 1 line
Return immediately from
clean_cache_range()for size 0.Thanks. Sashiko might have found a few similar issues in there:
https://sashiko.dev/#/patchset/[email protected]
Thanks for pointing this out.
I had a quick look. These comments appear to point at related pre-existing corner cases in cache flush helpers, and one of them also touches the DRM cache flushing helper. They are independent from the specific clean_cache_range() issue fixed by this patch.
Would it make sense to keep this patch focused on clean_cache_range() for now, and handle any additional cases separately if maintainers think they should be fixed as well?
Thanks,
Zhe
Quoted 21 lines · On Fri, 4 Sep 2026 15:54:08 +0800 "Li Zhe" <[email protected]> wrote:
Quoted 20 lines · Andrew Morton
Quoted 18 lines · On Thu, 3 Sep 2026 15:11:25 +0800 "Li Zhe" <[email protected]> wrote:
clean_cache_range()writes back each cache line in the range [addr, addr + size). A zero-size range is empty and should not perform any cache maintenance operation.As pointed out by Sashiko [1],
clean_cache_range(addr, 0)currently can still execute one CLWB when addr is not cache-line aligned. With size 0, vend is equal to addr. However, the loop starts from the cacheline-aligned address containing addr. If addr is not cacheline aligned, that rounded-down start is below vend, so the loop can execute one CLWB even though the requested range is empty.That gives zero-size callers observable side effects. For example,
arch_wb_cache_pmem(addr, 0)should not write back any cache line, andmemcpy_flushcache(dst, src, 0)should preserve the usual zero-length copy semantics. If the rounded-down line is not mapped, the stray CLWB can also fault.Return immediately from
clean_cache_range()for size 0.
Quoted 8 lines
Quoted 2 lines
Thanks. Sashiko might have found a few similar issues in there:
https://sashiko.dev/#/patchset/[email protected]Thanks for pointing this out.
I had a quick look. These comments appear to point at related pre-existing corner cases in cache flush helpers, and one of them also touches the DRM cache flushing helper. They are independent from the specific
clean_cache_range()issue fixed by this patch.
Yup.
Quoted 2 lines
Would it make sense to keep this patch focused on
clean_cache_range()for now,
I think so. You're under no obligation to fix any Sashiko bugs! But I do like to point these things out because you might consider them relevant to the patch under discussion.
Quoted 2 lines
and handle any additional cases separately if maintainers think they should be fixed as well?
That's up to the x86 team.