[PATCH v9 2/5] x86/asm, x86/boot: expose inline memcmp()

From: Mauricio Faria de Oliveira

Date: Sat Aug 22 2026 - 14:33:28 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 | 13 ++-----------
arch/x86/include/asm/shared/string.h | 26 ++++++++++++++++++++++++++
2 files changed, 28 insertions(+), 11 deletions(-)

diff --git a/arch/x86/boot/string.c b/arch/x86/boot/string.c
index 03278b4393887cb71cb063818d3378c4f52b06f8..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,17 +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.
- */
- asm volatile("test %3, %3\n\t"
- "repe cmpsb"
- : "=@ccnz" (diff), "+D" (s1), "+S" (s2), "+c" (len)
- : : "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..06c1d5e5013e4d59cfb49866d10e164362d2c4cc
--- /dev/null
+++ b/arch/x86/include/asm/shared/string.h
@@ -0,0 +1,26 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#ifndef _ASM_X86_SHARED_STRING_H
+#define _ASM_X86_SHARED_STRING_H
+
+/*
+ * This inline memcmp() returns 0 (equal) or 1 (not equal).
+ * The regular memcmp() returns <0 (less than), 0 (equal), or >0 (greater than)
+ * to indicate ordering as well.
+ */
+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.
+ */
+ asm volatile("test %3, %3\n\t"
+ "repe cmpsb"
+ : "=@ccnz" (diff), "+D" (s1), "+S" (s2), "+c" (len)
+ : : "memory");
+
+ return diff;
+}
+
+#endif /* _ASM_X86_SHARED_STRING_H */

--
2.47.3