[PATCH v10 2/4] x86/asm, x86/boot: expose inline memcmp()

From: Mauricio Faria de Oliveira

Date: Mon Sep 21 2026 - 21:36:35 EST


Move the inline memcmp function currently only available in boot/string.c
into the shared string function header <asm/shared/string.h> to be reused.

This is not done through <asm/string.h> to avoid pulling unnecessary code
in boot/string.c that causes build errors in boot/compressed/string.c
and purgatory/purgatory.ro.

No functional changes.

Signed-off-by: Mauricio Faria de Oliveira <mfo@xxxxxxxxxx>

---

Thanks to David Laight for noticing the return value difference between
inline and regular memcmp().
---
arch/x86/boot/string.c | 17 ++---------------
arch/x86/include/asm/shared/string.h | 31 +++++++++++++++++++++++++++++++
2 files changed, 33 insertions(+), 15 deletions(-)

diff --git a/arch/x86/boot/string.c b/arch/x86/boot/string.c
index e10260ed58b69fc734a4e63f15785a643f414441..be454a6864225f3a972c3e81826b77ed4e8a57fe 100644
--- a/arch/x86/boot/string.c
+++ b/arch/x86/boot/string.c
@@ -15,6 +15,7 @@
#include <linux/errno.h>
#include <linux/limits.h>
#include <asm/asm.h>
+#include <asm/shared/string.h>
#include "ctype.h"
#include "string.h"

@@ -31,21 +32,7 @@

int memcmp(const void *s1, const void *s2, size_t len)
{
- bool diff;
-
- /*
- * Make sure ZF is properly set in the len==0 case because in it,
- * RCX==0 and the REPE; CMPSB won't get executed.
- *
- * The "cc" clobber has no meaning anymore, just source compatibility.
- * On x86 the flag status bits are automatically added to the clobber
- * set when there are no =@ccXY constraints. Keep it as documentation.
- */
- asm volatile("test %3, %3\n\t"
- "repe cmpsb"
- : "=@ccnz" (diff), "+D" (s1), "+S" (s2), "+c" (len)
- : : /* "cc", */ "memory");
- return diff;
+ return __inline_memcmp(s1, s2, len);
}

/*
diff --git a/arch/x86/include/asm/shared/string.h b/arch/x86/include/asm/shared/string.h
new file mode 100644
index 0000000000000000000000000000000000000000..6291653fe629babf15e29060b013d357d8eacf4c
--- /dev/null
+++ b/arch/x86/include/asm/shared/string.h
@@ -0,0 +1,31 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#ifndef _ASM_X86_SHARED_STRING_H
+#define _ASM_X86_SHARED_STRING_H
+
+/*
+ * Returns: 0 (equal)
+ * 1 (not equal)
+ *
+ * In contrast, the regular memcmp() follows glibc return value semantics.
+ */
+static __always_inline int __inline_memcmp(const void *s1, const void *s2, size_t len)
+{
+ bool diff;
+
+ /*
+ * Make sure ZF is properly set in the len==0 case because in it,
+ * RCX==0 and the REPE; CMPSB won't get executed.
+ *
+ * The "cc" clobber has no meaning anymore, just source compatibility.
+ * On x86 the flag status bits are automatically added to the clobber
+ * set when there are no =@ccXY constraints. Keep it as documentation.
+ */
+ asm volatile("test %3, %3\n\t"
+ "repe cmpsb"
+ : "=@ccnz" (diff), "+D" (s1), "+S" (s2), "+c" (len)
+ : : /* "cc", */ "memory");
+
+ return diff;
+}
+
+#endif /* _ASM_X86_SHARED_STRING_H */

--
2.47.3