[PATCH v7 2/5] x86/asm: add volatile, clobbers and zero-length check in inline memcmp

Mauricio Faria de Oliveira <[email protected]>
Newsgroups org.xenproject.lists.xen-devel,org.kernel.vger.linux-kernel
Message-ID <[email protected]>
Add the volatile qualifier and clobbers parameter to prevent bugs with
instruction reordering and optimization.

Also check the zero-length case, as the 'repe' prefix does not run the
'cmpsb' instruction if the 'count' register is zero, which doesn't set
the condition-code/zero flag, so the result is based on a stale flag.

Those are pre-existing issues found by Sashiko.

Link: https://sashiko.dev/#/patchset/20260701-pvh-kasan-inline-v6-0-ba99045dfa9f%40igalia.com
Signed-off-by: Mauricio Faria de Oliveira <[email protected]>
---
 arch/x86/include/asm/shared/string.h | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)

diff --git a/arch/x86/include/asm/shared/string.h b/arch/x86/include/asm/shared/string.h
index 02b92927553f7b8e1c87e6122bbaa70439e57ea7..166274e44f3cb49e3dccab3cdac281d67aef5d44 100644
--- a/arch/x86/include/asm/shared/string.h
+++ b/arch/x86/include/asm/shared/string.h
@@ -11,8 +11,12 @@ static __always_inline int __inline_memcmp(const void *s1, const void *s2, size_
 {
 	bool diff;
 
-	asm("repe cmpsb"
-	    : "=@ccnz" (diff), "+D" (s1), "+S" (s2), "+c" (len));
+	if (len == 0)
+		return 0;
+
+	asm volatile("repe cmpsb"
+		     : "=@ccnz" (diff), "+D" (s1), "+S" (s2), "+c" (len)
+		     : : "cc", "memory");
 
 	return diff;
 }

-- 
2.47.3
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.