Skip to contentSkip to conversation

x86/pvh: fix unbootable VMs again (PVH + KASAN)

PATCH Superseded Mauricio Faria de Oliveira 6 participants 29 messages Jul 21 – Jul 28

Subsystems (2)

v7 of 10 Superseded Latest is v10 →

lore
Share & tools
lore
#0 lore raw

v7 cover x86/pvh: fix unbootable VMs again (PVH + KASAN)

The issue of unbootable VMs with CONFIG_PVH due to CONFIG_KASAN is back.

Booting directly from vmlinux (instead of bzImage) now fails with gcc-14/15 (but works with gcc-12/13) if CONFIG_KASAN_GENERIC is set, on Ubuntu 25.10.

The PVH code is required/supposed not to use the KASAN memory access check in the kernel entry point as KASAN has not yet been setup, or an exception is hit and the boot fails.

This was previously described and addressed with __builtin_mem{cmp,set}():
- commit 661362e3dcab ("xen, pvh: fix unbootable VMs (PVH + KASAN - AMD_MEM_ENCRYPT)")
- commit 416a33c9afce ("x86/cpu: fix unbootable VMs by inlining memcmp() in hypervisor_cpuid_base()")
- commit fbe5a6dfe492 ("xen, pvh: fix unbootable VMs by inlining memset() in xen_prepare_pvh()")

However, even with __builtin the compiler may decide to use the out of line function instead of the inline implementation. So, that does not really fix the issue unconditionally; see details below.

In order to address this, it's required to switch to inline implementations that do not depend on the compiler.

There's such a memset() in <asm/string.h> and memcmp() in 'boot/string.c'. Use them instead of builtins in PVH entry.

Testing:

- Booting from vmlinux (fixed) and bzImage (still works) using allnoconfig + CONFIG_PVH + CONFIG_KASAN with gcc-12/13/14/15.

- Building with CONFIG_KEXEC_FILE, CONFIG_CFI and !CONFIG_KASAN with LLVM 20 (check for a build error not caught previously).

Details/Debugging:

- Only CONFIG_PVH (works):

  make allnoconfig
  ./scripts/config \
    -e 64BIT -e HYPERVISOR_GUEST -e PVH \
    -e SERIAL_8250 -e SERIAL_8250_CONSOLE
  make olddefconfig
  make -j$(nproc) vmlinux

  qemu-system-x86_64 \
    -accel kvm -nodefaults -nographic -serial stdio \
    -kernel vmlinux -append 'console=ttyS0'
  ...
  SeaBIOS (version ...)
  Booting from ROM...
  Linux version ...
  ...
  <Ctrl-C>

- With CONFIG_KASAN (fails)

  ./scripts/config -e KASAN
  make olddefconfig
  make -j$(nproc) vmlinux

  qemu-system-x86_64 \
    -accel kvm -nodefaults -nographic -serial stdio \
    -kernel vmlinux -append 'console=ttyS0'
  ...
  SeaBIOS (version ...)
  Booting from ROM...
  <QEMU reboot loop, flashing the text above>

- Debugging:

Enable debug info and rebuild.

QEMU: enable and wait for GDB, stop rebooting, remain running.

  qemu-system-x86_64 \
    -s -S -no-reboot -no-shutdown \
    <other options>

gdb vmlinux
(gdb) target remote localhost:1234
...
(gdb) c
...
Thread 2 received signal SIGQUIT, Quit.
...
(gdb) info threads

    Id   Target Id                    Frame
    1    Thread 1.1 (CPU#0 [running]) bytes_is_nonzero (
      start=0xfffffbfff031eebe <error: Cannot access memory at address 0xfffffbfff031eebe>, size=1)
      at .../linux/mm/kasan/generic.c:98
  * 2    Thread 1.2 (CPU#1 [halted ]) 0x00000000000fd0a9 in ?? ()
  ...
  (gdb) thr 1
  ...
  (gdb) bt
  #0  bytes_is_nonzero (start=0xfffffbfff031eebe <error: Cannot access memory at address 0xfffffbfff031eebe>, size=1)
      at .../linux/mm/kasan/generic.c:98
  #1  memory_is_nonzero (start=0xfffffbfff031eebe, end=0xfffffbfff031eebf) at .../linux/mm/kasan/generic.c:115
  #2  memory_is_poisoned_n (addr=0xffffffff818f75f0, size=8) at .../linux/mm/kasan/generic.c:140
  #3  memory_is_poisoned (addr=0xffffffff818f75f0, size=8) at .../linux/mm/kasan/generic.c:172
  #4  check_region_inline (addr=0xffffffff818f75f0, size=8, write=false, ret_ip=18446744071585002062)
      at .../linux/mm/kasan/generic.c:191
  #5  kasan_check_range (addr=addr@entry=0xffffffff818f75f0, size=size@entry=8, write=write@entry=false,
      ret_ip=18446744071585002062) at .../linux/mm/kasan/generic.c:200
  #6  0xffffffff813eb283 in __asan_loadN (addr=addr@entry=0xffffffff818f75f0, size=size@entry=8)
      at .../linux/mm/kasan/generic.c:278
  #7  0xffffffff815df24e in memcmp (cs=cs@entry=0xffffffff818f75f0, ct=ct@entry=0x1be2fe4, count=<optimized out>,
      count@entry=12) at .../linux/lib/string.c:683
  #8  0xffffffff81ba2323 in cpuid_base_hypervisor (sig=0xffffffff818f75f0 "XenVMMXenVMM", leaves=2)
      at .../linux/arch/x86/include/asm/cpuid/api.h:206
  #9  xen_cpuid_base () at .../linux/arch/x86/include/asm/xen/hypervisor.h:46
  #10 xen_prepare_pvh () at .../linux/arch/x86/platform/pvh/enlighten.c:119
  #11 0x0000000001ba2588 in ?? ()
  #12 0x0000000000000000 in ?? ()
  (gdb)

Frames #7-#8 show the non-builtin memcmp() (lib/string.c) was called even with __builtin_memcmp() being used in cpuid_base_hypervisor().

---
Changes in v7:
- Patch 2 (added):
- Address pre-existing issues in 'asm' (Borislav Petkov, Sashiko).
- Link to v6: https://lore.kernel.org/r/[email protected]

Changes in v6:
- Patch 1:
- Explain the return value difference between __inline_memcmp() and memcmp().
- Patch 2 (added):
- Group __inline string functions in <asm/shared/string.h>.
- Link to v5: https://lore.kernel.org/r/[email protected]

Changes in v5:
- Create a minimal separate header in <asm/shared/string.h> instead,
to be used by 'boot/setup.c' and <asm/string.h> (Borislav Petkov).
- Patch 1 (in v4/v3) is no longer needed; removed.
- Patch 1 (in v5):
- Briefly mention there are issues with <asm/string.h>.
- Remove 'Reviewed-by: Jurgen Gross' to be conservative
(same code change and result, but the means changed).
- Link to v4: https://lore.kernel.org/r/[email protected]

Changes in v4:
- Patch 1: address Juergen's feedback:
- s/In next patch/In a future patch/.
- Move footnote (Reasons not to include...) after "---".
- Add 'Reviewed-by: Juergen Gross' in patches 1 and 2 as well.
- Link to v3: https://lore.kernel.org/r/[email protected]

Changes in v3:
- Create and use a separate header for inline string functions
to fix a build error reported by kernel test robot (patch 1).
- That also removes '#ifndef _SETUP/#endif' in <asm/string.h>.
- Link to v2: https://lore.kernel.org/r/[email protected]

Changes in v2:
- Add comment about the return value of __inline_memcmp() in patch 1. (v3: now 2)
- Add 'Reviewed-by: Juergen Gross' in patches 2 and 3 (v3: now 3 and 4).
- Link to v1: https://lore.kernel.org/r/[email protected]

---
Mauricio Faria de Oliveira (5):
x86/asm, x86/boot: expose inline memcmp
x86/asm: add volatile, clobbers and zero-length check in inline memcmp x86/asm: group inline string functions
x86/cpuid: fix unbootable VMs by really inlining memcmp() in hypervisor_cpuid_base() x86/pvh: fix unbootable VMs by really inlining memset() in xen_prepare_pvh()

 arch/x86/boot/string.c               |  6 ++---
 arch/x86/include/asm/cpuid/api.h     |  2 +-
 arch/x86/include/asm/shared/string.h | 50 ++++++++++++++++++++++++++++++++++++
 arch/x86/include/asm/string.h        | 21 +--------------
 arch/x86/platform/pvh/enlighten.c    |  3 ++-
 5 files changed, 56 insertions(+), 26 deletions(-)

---
base-commit: 3fe08b9796f36ef437ab9328e7dd1e5ff2d66603
change-id: 20260422-pvh-kasan-inline-6efac77f1b27

Best regards,

Signature · 1 line
Mauricio Faria de Oliveira <[email protected]>
#1 lore raw

PATCH v7 1/5 x86/asm, x86/boot: expose inline memcmp

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'.

---

Thanks to David Laight for noticing the return value difference between inline and regular memcmp().

---
 arch/x86/boot/string.c               |  6 ++----
 arch/x86/include/asm/shared/string.h | 20 ++++++++++++++++++++
 2 files changed, 22 insertions(+), 4 deletions(-)
arch/x86/boot/string.c +2 -4
index ac0f900ebc47efa81c92e1bb2010ea41677899c4..be454a6864225f3a972c3e81826b77ed4e8a57fe 100644
@@ -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,10 +32,7 @@
 
 int memcmp(const void *s1, const void *s2, size_t len)
 {
-	bool diff;
-	asm("repe cmpsb"
-	    : "=@ccnz" (diff), "+D" (s1), "+S" (s2), "+c" (len));
-	return diff;
+	return __inline_memcmp(s1, s2, len);
 }
 
 /*
arch/x86/include/asm/shared/string.h new file +20 -0
new file mode 100644
index 0000000000000000000000000000000000000000..02b92927553f7b8e1c87e6122bbaa70439e57ea7
@@ -0,0 +1,20 @@
+/* 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;
+
+	asm("repe cmpsb"
+	    : "=@ccnz" (diff), "+D" (s1), "+S" (s2), "+c" (len));
+
+	return diff;
+}
+
+#endif /* _ASM_X86_SHARED_STRING_H */
Signature · 1 line
2.47.3
#2 lore raw

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

In reply to Mauricio Faria de Oliveira

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.

---
 arch/x86/include/asm/shared/string.h | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
arch/x86/include/asm/shared/string.h +6 -2
index 02b92927553f7b8e1c87e6122bbaa70439e57ea7..166274e44f3cb49e3dccab3cdac281d67aef5d44 100644
@@ -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;
 }
Signature · 1 line
2.47.3
maintainer
#3 lore raw

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

hpa in To:.

Quoted 5 lines · Mauricio Faria de Oliveira

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

Please use capital letters for insns: REPE, CMPSB and you don't need to put words in '' - it reads fine without them.

Quoted 19 lines

the condition-code/zero flag, so the result is based on a stale flag.

Those are pre-existing issues found by Sashiko.

---
 arch/x86/include/asm/shared/string.h | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
arch/x86/include/asm/shared/string.h +0 -1
index 02b92927553f7b8e1c87e6122bbaa70439e57ea7..166274e44f3cb49e3dccab3cdac281d67aef5d44 100644
@@ -11,8 +11,12 @@ static __always_inline int __inline_memcmp(const void *s1, const void *s2, size_
 {
 	bool diff;
 
-	asm("repe cmpsb"

arch/x86/include/asm/shared/string.h static __always_inline int __inline_memcmp(const void *s1, const void *s2, size_ · line 22

Quoted 10 lines
arch/x86/include/asm/shared/string.h +6 -1
-	    : "=@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;
 }

So this is a fix which should probably go to stable, I think. Going back into git history, it points to

62bd0337d0c4 ("Top header file for new x86 setup code")

from 2007. And we have carried it this way through the years and who knows what hit this or not. So please make this the first patch in your set - you can even send it separately so that I can get route it through stable.

Then you can base the rest ontop.

Thx.

Signature · 4 lines
Regards/Gruss,
    Boris.

https://people.kernel.org/tglx/notes-about-netiquette
#4 lore raw

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

Quoted 47 lines · Borislav Petkov

hpa in To:.

Quoted 5 lines · Mauricio Faria de Oliveira

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

Please use capital letters for insns: REPE, CMPSB and you don't need to put words in '' - it reads fine without them.

Quoted 19 lines

the condition-code/zero flag, so the result is based on a stale flag.

Those are pre-existing issues found by Sashiko.

---
 arch/x86/include/asm/shared/string.h | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
arch/x86/include/asm/shared/string.h +0 -1
index 02b92927553f7b8e1c87e6122bbaa70439e57ea7..166274e44f3cb49e3dccab3cdac281d67aef5d44 100644
@@ -11,8 +11,12 @@ static __always_inline int __inline_memcmp(const void *s1, const void *s2, size_
 {
 	bool diff;
 
-	asm("repe cmpsb"

arch/x86/include/asm/shared/string.h static __always_inline int __inline_memcmp(const void *s1, const void *s2, size_ · line 22

Quoted 10 lines
arch/x86/include/asm/shared/string.h +6 -1
-	    : "=@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;
 }

So this is a fix which should probably go to stable, I think. Going back into git history, it points to

62bd0337d0c4 ("Top header file for new x86 setup code")

Quoted 8 lines

from 2007. And we have carried it this way through the years and who knows what hit this or not. So please make this the first patch in your set - you can even send it separately so that I can get route it through stable.

Then you can base the rest ontop.

Thx.

Also, this is silly. Instead of adding a whole separate test, just do "test %3,%3" before the repe to set ZF and let the REPE skip.

#5 lore raw

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

Quoted 52 lines · H. Peter Anvin
Quoted 51 lines · Borislav Petkov

hpa in To:.

Quoted 5 lines · Mauricio Faria de Oliveira

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

Please use capital letters for insns: REPE, CMPSB and you don't need to put words in '' - it reads fine without them.

Quoted 19 lines

the condition-code/zero flag, so the result is based on a stale flag.

Those are pre-existing issues found by Sashiko.

---
 arch/x86/include/asm/shared/string.h | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
arch/x86/include/asm/shared/string.h +0 -1
index 02b92927553f7b8e1c87e6122bbaa70439e57ea7..166274e44f3cb49e3dccab3cdac281d67aef5d44 100644
@@ -11,8 +11,12 @@ static __always_inline int __inline_memcmp(const void *s1, const void *s2, size_
 {
 	bool diff;
 
-	asm("repe cmpsb"

arch/x86/include/asm/shared/string.h static __always_inline int __inline_memcmp(const void *s1, const void *s2, size_ · line 22

Quoted 10 lines
arch/x86/include/asm/shared/string.h +6 -1
-	    : "=@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;
 }

So this is a fix which should probably go to stable, I think. Going back into git history, it points to

62bd0337d0c4 ("Top header file for new x86 setup code")

from 2007. And we have carried it this way through the years and who knows what hit this or not. So please make this the first patch in your set - you can even send it separately so that I can get route it through stable.

Quoted 6 lines
Quoted 4 lines

Then you can base the rest ontop.

Thx.

Also, this is silly. Instead of adding a whole separate test, just do "test %3,%3" before the repe to set ZF and let the REPE skip.

Besides this, isn't the function effectively returning bool wrong anyway? This way you can use it for equal / not-equal comparisons, but not for sorting and alike.

Jan

#6 lore raw

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

Quoted 7 lines · Jan Beulich
Quoted 2 lines

Also, this is silly. Instead of adding a whole separate test, just do "test %3,%3" before the repe to set ZF and let the REPE skip.

Besides this, isn't the function effectively returning bool wrong anyway? This way you can use it for equal / not-equal comparisons, but not for sorting and alike.

There is no use case in the early code for sorting, and it seems rather broken to burden the code with that.

That being said it probably should return bool explicitly (it makes no sense for the prototype to be different than the internal variable.)

We could call it memneq() if someone really, really cares, I guess.

The early code is very size-sensitive, so I'm really not fond of the idea of burdening it further. Perhaps something like:

A memory clobber is ugly here since no memory is actually modified, although it probably doesn't affect code; "cc" is completely redundant with condition code output operand.

static __always_inline bool
__inline_memcmp(const void *s1, const void *s2, size_t len)

{
	bool diff;

	if (__builtin_constant_p(len == 0)) {
		if (!len)
			return false;
		asm volatile("repe cmpsb"
				: "=@ccnz" (diff),
				  "+D" (s1), "+S" (s2), "+c" (len)
				: : "memory");
	} else {
		/* Clear ZF beforehand in case len == 0 */
		asm volatile("test %3,%3; repe cmpsb"
				: "=@ccnz" (diff),
				  "+D" (s1), "+S" (s2), "+c" (len)
				: : "memory");
	}
	return diff;
}
#7 lore raw

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

Quoted 10 lines · H. Peter Anvin
Quoted 7 lines · Jan Beulich
Quoted 2 lines

Also, this is silly. Instead of adding a whole separate test, just do "test %3,%3" before the repe to set ZF and let the REPE skip.

Besides this, isn't the function effectively returning bool wrong anyway? This way you can use it for equal / not-equal comparisons, but not for sorting and alike.

There is no use case in the early code for sorting, and it seems rather broken to burden the code with that.

If this is merged, other users not in early code might eventually appear, which might introduce the use case for sorting. Even though that is unlikely, as such users could probably use regular memcmp() instead, consider that just in case, so I can ask for your input/advice on this:

Quoted 7 lines

That being said it probably should return bool explicitly (it makes no sense for the prototype to be different than the internal variable.)

We could call it memneq() if someone really, really cares, I guess.

The early code is very size-sensitive, so I'm really not fond of the idea of burdening it further. Perhaps something like:

Do you think this implementation (returns -1/0/+1) is reasonable, size-wise, for early code?

I considered submitting it eventually, once the return value difference to regular memcmp() was called out [0], as an improvement, if there's agreement this would be a good idea considering the scenario above.

static __always_inline int __inline_memcmp(const void *s1, const void *s2, size_t len)

{
        int above, below;

        asm volatile("test %2, %2\n\t"
                     "repe cmpsb"
                     : "+S" (s1), "+D" (s2), "+c" (len),
                       "=@cca" (above), "=@ccb" (below)
                     : : "memory");

        return above - below;
}

@ arch/x86/boot/string.o
00000028 <memcmp>:

  28:   66 57                   push   %di
  2a:   66 56                   push   %si
  2c:   66 89 c6                mov    %ax,%si
  2f:   66 89 d7                mov    %dx,%di
  32:   66 85 c9                test   %cx,%cx
  35:   f3 a6                   repz cmpsb %es:(%edi),%ds:(%esi)
  37:   0f 97 c0                seta   %al
  3a:   66 0f b6 c0             movzbw %al,%ax
  3e:   0f 92 c2                setb   %dl
  41:   66 0f b6 d2             movzbw %dl,%dx
  45:   66 29 d0                sub    %dx,%ax
  48:   66 5e                   pop    %si
  4a:   66 5f                   pop    %di
  4c:   66 c3                   retw
Quoted 1 line

A memory clobber is ugly here since no memory is actually modified, although

I'm definitely not an expert, but IIUIC, this memory clobber is for reads, not writes? Say, a caller/optimized code that writes to the buffer(s) prior to __inline_memcmp() and data might still reside in registers; even if theoretical/unlikely.

As in [1]:

The "memory" clobber tells the compiler that the assembly code
performs memory reads or writes [...] (for example, accessing the memory pointed to [...]). To ensure memory contains correct values, GCC may need to flush specific register values to memory before executing the asm.

Quoted 2 lines

it probably doesn't affect code; "cc" is completely redundant with condition code output operand.

Thanks for explaining. I'll remove that in the next version.

[0]
https://lore.kernel.org/all/[email protected]/
[1]
https://gcc.gnu.org/onlinedocs/gcc/Extended-Asm.html#Clobbers-and-Scratch-Registers-1

cheers,

Quoted 21 lines

static __always_inline bool
__inline_memcmp(const void *s1, const void *s2, size_t len)

{
	bool diff;

	if (__builtin_constant_p(len == 0)) {
		if (!len)
			return false;
		asm volatile("repe cmpsb"
				: "=@ccnz" (diff),
				  "+D" (s1), "+S" (s2), "+c" (len)
				: : "memory");
	} else {
		/* Clear ZF beforehand in case len == 0 */
		asm volatile("test %3,%3; repe cmpsb"
				: "=@ccnz" (diff),
				  "+D" (s1), "+S" (s2), "+c" (len)
				: : "memory");
	}
	return diff;
}
Signature · 1 line
Mauricio
#8 lore raw

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

Quoted 83 lines · Mauricio Faria de Oliveira
Quoted 10 lines · H. Peter Anvin
Quoted 7 lines · Jan Beulich
Quoted 2 lines

Also, this is silly. Instead of adding a whole separate test, just do "test %3,%3" before the repe to set ZF and let the REPE skip.

Besides this, isn't the function effectively returning bool wrong anyway? This way you can use it for equal / not-equal comparisons, but not for sorting and alike.

There is no use case in the early code for sorting, and it seems rather broken to burden the code with that.

If this is merged, other users not in early code might eventually appear, which might introduce the use case for sorting. Even though that is unlikely, as such users could probably use regular memcmp() instead, consider that just in case, so I can ask for your input/advice on this:

Quoted 7 lines

That being said it probably should return bool explicitly (it makes no sense for the prototype to be different than the internal variable.)

We could call it memneq() if someone really, really cares, I guess.

The early code is very size-sensitive, so I'm really not fond of the idea of burdening it further. Perhaps something like:

Do you think this implementation (returns -1/0/+1) is reasonable, size-wise, for early code?

I considered submitting it eventually, once the return value difference to regular memcmp() was called out [0], as an improvement, if there's agreement this would be a good idea considering the scenario above.

static __always_inline int __inline_memcmp(const void *s1, const void *s2, size_t len)

{
       int above, below;

       asm volatile("test %2, %2\n\t"
                    "repe cmpsb"
                    : "+S" (s1), "+D" (s2), "+c" (len),
                      "=@cca" (above), "=@ccb" (below)
                    : : "memory");

       return above - below;
}

@ arch/x86/boot/string.o
00000028 <memcmp>:

 28:   66 57                   push   %di
 2a:   66 56                   push   %si
 2c:   66 89 c6                mov    %ax,%si
 2f:   66 89 d7                mov    %dx,%di
 32:   66 85 c9                test   %cx,%cx
 35:   f3 a6                   repz cmpsb %es:(%edi),%ds:(%esi)
 37:   0f 97 c0                seta   %al
 3a:   66 0f b6 c0             movzbw %al,%ax
 3e:   0f 92 c2                setb   %dl
 41:   66 0f b6 d2             movzbw %dl,%dx
 45:   66 29 d0                sub    %dx,%ax
 48:   66 5e                   pop    %si
 4a:   66 5f                   pop    %di
 4c:   66 c3                   retw
Quoted 1 line

A memory clobber is ugly here since no memory is actually modified, although

I'm definitely not an expert, but IIUIC, this memory clobber is for reads, not writes? Say, a caller/optimized code that writes to the buffer(s) prior to __inline_memcmp() and data might still reside in registers; even if theoretical/unlikely.

As in [1]:

The "memory" clobber tells the compiler that the assembly code
performs memory reads or writes [...] (for example, accessing the memory pointed to [...]). To ensure memory contains correct values, GCC may need to flush specific register values to memory before executing the asm.

Quoted 2 lines

it probably doesn't affect code; "cc" is completely redundant with condition code output operand.

Quoted 32 lines

Thanks for explaining. I'll remove that in the next version.

[0]
https://lore.kernel.org/all/[email protected]/
[1]
https://gcc.gnu.org/onlinedocs/gcc/Extended-Asm.html#Clobbers-and-Scratch-Registers-1

cheers,

Quoted 21 lines

static __always_inline bool
__inline_memcmp(const void *s1, const void *s2, size_t len)

{
	bool diff;

	if (__builtin_constant_p(len == 0)) {
		if (!len)
			return false;
		asm volatile("repe cmpsb"
				: "=@ccnz" (diff),
				  "+D" (s1), "+S" (s2), "+c" (len)
				: : "memory");
	} else {
		/* Clear ZF beforehand in case len == 0 */
		asm volatile("test %3,%3; repe cmpsb"
				: "=@ccnz" (diff),
				  "+D" (s1), "+S" (s2), "+c" (len)
				: : "memory");
	}
	return diff;
}

It's pretty bloaty; I don't know exactly how much the rest of the code bloats.

I would be fine with having mem[n]eq() though as an explicit API. For the out-of-line version used for compiler-generated memcmp() calls it doesn't matter.

Here is a very compact way the out-of-line version can be written (showing the 64-bit version; formatting apologies, on a phone):

xor %eax,%eax /* Sets ZF */
repe cmpsb
jr z,1f
movzbl -1(%rdi),%edi
mov -1(%rsi),%al
sub %edi,%eax
1: ret

#9 lore raw

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

Quoted 7 lines · H. Peter Anvin

It's pretty bloaty; I don't know exactly how much the rest of the code bloats.

I would be fine with having mem[n]eq() though as an explicit API. For the out-of-line version used for compiler-generated memcmp() calls it doesn't matter.

Here is a very compact way the out-of-line version can be written (showing the 64-bit version; formatting apologies, on a phone):

Quoted 8 lines

xor %eax,%eax /* Sets ZF */
repe cmpsb
jr z,1f
movzbl -1(%rdi),%edi
mov -1(%rsi),%al
sub %edi,%eax
1: ret

With a C wrapper to work in any mode:

int memcmp(const void *a, const void *b, size_t size)

{
        int diff;

        asm volatile("xor %0,%0 ;"      /* Sets ZF for the size = 0 case */
                     "repe cmpsb ;"
                     "jz 1f ;"          /* When size = 0 loading is unsafe */
                     "movb -1(%2),%b0 ;"
                     "movzbl -1(%1),%k1 ;"
                     "sub %k1,%0 ;"
                     "1:"
                     : "=&q" (diff), "+D" (a), "+S" (b), "+c" (size)
                     : : "memory");
        return diff;
}
#10 lore raw

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

Quoted 19 lines · H. Peter Anvin
Quoted 7 lines · H. Peter Anvin

It's pretty bloaty; I don't know exactly how much the rest of the code bloats.

I would be fine with having mem[n]eq() though as an explicit API. For the out-of-line version used for compiler-generated memcmp() calls it doesn't matter.

Here is a very compact way the out-of-line version can be written (showing the 64-bit version; formatting apologies, on a phone):

Quoted 8 lines

xor %eax,%eax /* Sets ZF */
repe cmpsb
jr z,1f
movzbl -1(%rdi),%edi
mov -1(%rsi),%al
sub %edi,%eax
1: ret

With a C wrapper to work in any mode:

int memcmp(const void *a, const void *b, size_t size)

Quoted 15 lines
{
        int diff;

        asm volatile("xor %0,%0 ;"      /* Sets ZF for the size = 0 case */
                     "repe cmpsb ;"
                     "jz 1f ;"          /* When size = 0 loading is unsafe */
                     "movb -1(%2),%b0 ;"
                     "movzbl -1(%1),%k1 ;"
                     "sub %k1,%0 ;"
                     "1:"
                     : "=&q" (diff), "+D" (a), "+S" (b), "+c" (size)
                     : : "memory");
        return diff;
}

And of course I got the comparison backwards (negative means a < b, thus we want a - b not b - a). Here is a fixed version.

int memcmp(const void *a, const void *b, size_t size)

{
        int diff;

        asm volatile("xor %0,%0 ;"      /* Sets ZF for the size = 0 case */
                     "repe cmpsb ;"
                     "jz 1f ;"          /* When size = 0 loading is unsafe */
                     "movb -1(%1),%b0 ;"
                     "movzbl -1(%2),%k2 ;"
                     "sub %k2,%0 ;"
                     "1:"
                     : "=&q" (diff), "+D" (a), "+S" (b), "+c" (size)
                     : : "memory");
        return diff;
}

-hpa

#11 lore raw

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

Quoted 5 lines · H. Peter Anvin

And of course I got the comparison backwards (negative means a < b, thus we want a - b not b - a). Here is a fixed version.

int memcmp(const void *a, const void *b, size_t size)

Quoted 15 lines
{
        int diff;

        asm volatile("xor %0,%0 ;"      /* Sets ZF for the size = 0 case */
                     "repe cmpsb ;"
                     "jz 1f ;"          /* When size = 0 loading is unsafe */
                     "movb -1(%1),%b0 ;"
                     "movzbl -1(%2),%k2 ;"
                     "sub %k2,%0 ;"
                     "1:"
                     : "=&q" (diff), "+D" (a), "+S" (b), "+c" (size)
                     : : "memory");
        return diff;
}

For extra credit, this version is even smaller in 16- and 32-bit mode, but larger in 64-bit mode (because it depends on the order of the CMPSB operands, which is the inverse of what the x86-64 ABI expects; swapping the order of "a" and "b" and adding a cmc instruction improves the x86-64 size, but 64 bits is not where the really tight code is...)

int memcmp(const void *a, const void *b, size_t size)

{
        int diff;

        asm volatile("xor %0,%0 ;"
                     "repe cmpsb ;"
                     "jz 1f ;"
                     "sbb %0,%0 ;"
                     "or $1,%0 ;"
                     "1:"
                     : "=&r" (diff), "+S" (a), "+D" (b), "+c" (size)
                     : : "memory");
        return diff;
}
#12 lore raw

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

Quoted 21 lines · On Fri, 24 Jul 2026 17:25:21 -0700 "H. Peter Anvin" <[email protected]> wrote:
Quoted 5 lines · H. Peter Anvin

And of course I got the comparison backwards (negative means a < b, thus we want a - b not b - a). Here is a fixed version.

int memcmp(const void *a, const void *b, size_t size)

Quoted 15 lines
{
        int diff;

        asm volatile("xor %0,%0 ;"      /* Sets ZF for the size = 0 case */
                     "repe cmpsb ;"
                     "jz 1f ;"          /* When size = 0 loading is unsafe */
                     "movb -1(%1),%b0 ;"
                     "movzbl -1(%2),%k2 ;"
                     "sub %k2,%0 ;"
                     "1:"
                     : "=&q" (diff), "+D" (a), "+S" (b), "+c" (size)
                     : : "memory");
        return diff;
}
Quoted 2 lines

For extra credit, this version is even smaller in 16- and 32-bit mode, but larger in 64-bit mode (because it depends on the order of the CMPSB operands,

The function is inlined, so it will make no difference.

David

Quoted 20 lines

which is the inverse of what the x86-64 ABI expects; swapping the order of "a" and "b" and adding a cmc instruction improves the x86-64 size, but 64 bits is not where the really tight code is...)

int memcmp(const void *a, const void *b, size_t size)

{
        int diff;

        asm volatile("xor %0,%0 ;"
                     "repe cmpsb ;"
                     "jz 1f ;"
                     "sbb %0,%0 ;"
                     "or $1,%0 ;"
                     "1:"
                     : "=&r" (diff), "+S" (a), "+D" (b), "+c" (size)
                     : : "memory");
        return diff;
}
#13 lore raw

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

In reply to H. Peter Anvin
Quoted 53 lines · H. Peter Anvin
Quoted 51 lines · Borislav Petkov

hpa in To:.

Quoted 5 lines · Mauricio Faria de Oliveira

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

Please use capital letters for insns: REPE, CMPSB and you don't need to put words in '' - it reads fine without them.

Quoted 19 lines

the condition-code/zero flag, so the result is based on a stale flag.

Those are pre-existing issues found by Sashiko.

---
 arch/x86/include/asm/shared/string.h | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
arch/x86/include/asm/shared/string.h +0 -1
index 02b92927553f7b8e1c87e6122bbaa70439e57ea7..166274e44f3cb49e3dccab3cdac281d67aef5d44 100644
@@ -11,8 +11,12 @@ static __always_inline int __inline_memcmp(const void *s1, const void *s2, size_
 {
     bool diff;
 
-    asm("repe cmpsb"

arch/x86/include/asm/shared/string.h static __always_inline int __inline_memcmp(const void *s1, const void *s2, size_ · line 22

Quoted 10 lines
arch/x86/include/asm/shared/string.h +6 -1
-        : "=@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;
 }

So this is a fix which should probably go to stable, I think. Going back into git history, it points to

62bd0337d0c4 ("Top header file for new x86 setup code")

from 2007. And we have carried it this way through the years and who knows what hit this or not. So please make this the first patch in your set - you can even send it separately so that I can get route it through stable.

Quoted 6 lines
Quoted 4 lines

Then you can base the rest ontop.

Thx.

Also, this is silly. Instead of adding a whole separate test, just do "test %3,%3" before the repe to set ZF and let the REPE skip.

REPE does not check ZF before the first iteration.

From the Intel SDM:
"When the REPE/REPZ and REPNE/REPNZ prefixes are used, the ZF flag does not require initialization because both
the CMPS and SCAS instructions affect the ZF flag according to the results of the comparisons they make."

#14 lore raw

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

Quoted 61 lines · Brian Gerst
Quoted 53 lines · H. Peter Anvin
Quoted 51 lines · Borislav Petkov

hpa in To:.

Quoted 5 lines · Mauricio Faria de Oliveira

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

Please use capital letters for insns: REPE, CMPSB and you don't need to put words in '' - it reads fine without them.

Quoted 19 lines

the condition-code/zero flag, so the result is based on a stale flag.

Those are pre-existing issues found by Sashiko.

---
 arch/x86/include/asm/shared/string.h | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
arch/x86/include/asm/shared/string.h +0 -1
index 02b92927553f7b8e1c87e6122bbaa70439e57ea7..166274e44f3cb49e3dccab3cdac281d67aef5d44 100644
@@ -11,8 +11,12 @@ static __always_inline int __inline_memcmp(const void *s1, const void *s2, size_
 {
     bool diff;
 
-    asm("repe cmpsb"

arch/x86/include/asm/shared/string.h static __always_inline int __inline_memcmp(const void *s1, const void *s2, size_ · line 22

Quoted 10 lines
arch/x86/include/asm/shared/string.h +6 -1
-        : "=@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;
 }

So this is a fix which should probably go to stable, I think. Going back into git history, it points to

62bd0337d0c4 ("Top header file for new x86 setup code")

from 2007. And we have carried it this way through the years and who knows what hit this or not. So please make this the first patch in your set - you can even send it separately so that I can get route it through stable.

Quoted 6 lines
Quoted 4 lines

Then you can base the rest ontop.

Thx.

Also, this is silly. Instead of adding a whole separate test, just do "test %3,%3" before the repe to set ZF and let the REPE skip.

Quoted 8 lines

REPE does not check ZF before the first iteration.

From the Intel SDM:
"When the REPE/REPZ and REPNE/REPNZ prefixes are used, the ZF flag does not require initialization because both
the CMPS and SCAS instructions affect the ZF flag according to the results of the comparisons they make."

The point was to ensure we have a valid result in ZF (which should be ZF=1) even if len = 0.

-hpa

#15 lore raw

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

Quoted 72 lines · H. Peter Anvin
Quoted 61 lines · Brian Gerst
Quoted 53 lines · H. Peter Anvin
Quoted 51 lines · Borislav Petkov

hpa in To:.

Quoted 5 lines · Mauricio Faria de Oliveira

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

Please use capital letters for insns: REPE, CMPSB and you don't need to put words in '' - it reads fine without them.

Quoted 19 lines

the condition-code/zero flag, so the result is based on a stale flag.

Those are pre-existing issues found by Sashiko.

---
 arch/x86/include/asm/shared/string.h | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
arch/x86/include/asm/shared/string.h +0 -1
index 02b92927553f7b8e1c87e6122bbaa70439e57ea7..166274e44f3cb49e3dccab3cdac281d67aef5d44 100644
@@ -11,8 +11,12 @@ static __always_inline int __inline_memcmp(const void *s1, const void *s2, size_
 {
     bool diff;
 
-    asm("repe cmpsb"

arch/x86/include/asm/shared/string.h static __always_inline int __inline_memcmp(const void *s1, const void *s2, size_ · line 22

Quoted 10 lines
arch/x86/include/asm/shared/string.h +6 -1
-        : "=@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;
 }

So this is a fix which should probably go to stable, I think. Going back into git history, it points to

62bd0337d0c4 ("Top header file for new x86 setup code")

from 2007. And we have carried it this way through the years and who knows what hit this or not. So please make this the first patch in your set - you can even send it separately so that I can get route it through stable.

Quoted 6 lines
Quoted 4 lines

Then you can base the rest ontop.

Thx.

Also, this is silly. Instead of adding a whole separate test, just do "test %3,%3" before the repe to set ZF and let the REPE skip.

Quoted 8 lines

REPE does not check ZF before the first iteration.

From the Intel SDM:
"When the REPE/REPZ and REPNE/REPNZ prefixes are used, the ZF flag does not require initialization because both
the CMPS and SCAS instructions affect the ZF flag according to the results of the comparisons they make."

Quoted 2 lines

The point was to ensure we have a valid result in ZF (which should be ZF=1) even if len = 0.

I had originally thought that the test for RCX==0 was also at the end of the loop, after the decrement. But a closer look at the pseudo-code in the SDM does show that the test is at the start of the loop. Starting with RCX==0 does indeed skip the comparison and does not touch ZF. Your suggestion is good, but should have a comment explaining why it's there to someone reading the code in the future.

#16 lore raw

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

Quoted 76 lines · Brian Gerst
Quoted 72 lines · H. Peter Anvin
Quoted 61 lines · Brian Gerst
Quoted 53 lines · H. Peter Anvin
Quoted 51 lines · Borislav Petkov

hpa in To:.

Quoted 5 lines · Mauricio Faria de Oliveira

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

Please use capital letters for insns: REPE, CMPSB and you don't need to put words in '' - it reads fine without them.

Quoted 19 lines

the condition-code/zero flag, so the result is based on a stale flag.

Those are pre-existing issues found by Sashiko.

---
 arch/x86/include/asm/shared/string.h | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
arch/x86/include/asm/shared/string.h +0 -1
index 02b92927553f7b8e1c87e6122bbaa70439e57ea7..166274e44f3cb49e3dccab3cdac281d67aef5d44 100644
@@ -11,8 +11,12 @@ static __always_inline int __inline_memcmp(const void *s1, const void *s2, size_
 {
     bool diff;
 
-    asm("repe cmpsb"

arch/x86/include/asm/shared/string.h static __always_inline int __inline_memcmp(const void *s1, const void *s2, size_ · line 22

Quoted 10 lines
arch/x86/include/asm/shared/string.h +6 -1
-        : "=@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;
 }

So this is a fix which should probably go to stable, I think. Going back into git history, it points to

62bd0337d0c4 ("Top header file for new x86 setup code")

from 2007. And we have carried it this way through the years and who knows what hit this or not. So please make this the first patch in your set - you can even send it separately so that I can get route it through stable.

Quoted 6 lines
Quoted 4 lines

Then you can base the rest ontop.

Thx.

Also, this is silly. Instead of adding a whole separate test, just do "test %3,%3" before the repe to set ZF and let the REPE skip.

Quoted 8 lines

REPE does not check ZF before the first iteration.

From the Intel SDM:
"When the REPE/REPZ and REPNE/REPNZ prefixes are used, the ZF flag does not require initialization because both
the CMPS and SCAS instructions affect the ZF flag according to the results of the comparisons they make."

Quoted 2 lines

The point was to ensure we have a valid result in ZF (which should be ZF=1) even if len = 0.

Quoted 7 lines

I had originally thought that the test for RCX==0 was also at the end of the loop, after the decrement. But a closer look at the pseudo-code in the SDM does show that the test is at the start of the loop. Starting with RCX==0 does indeed skip the comparison and does not touch ZF. Your suggestion is good, but should have a comment explaining why it's there to someone reading the code in the future.

See other post.

#17 lore raw

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

In reply to Brian Gerst

On Thu, 23 Jul 2026 20:49:13 -0400
Brian Gerst <[email protected]> wrote:

...

Quoted 6 lines

I had originally thought that the test for RCX==0 was also at the end of the loop, after the decrement. But a closer look at the pseudo-code in the SDM does show that the test is at the start of the loop. Starting with RCX==0 does indeed skip the comparison and does not touch ZF. Your suggestion is good, but should have a comment explaining why it's there to someone reading the code in the future.

I have a feeling that was a change between the 8086 and 80286. I don't have an 8086 book (any more) but I'm sure I remember fixing bugs where %cx of zero caused 65536 iterations.
(Fun to find because the vector table is 'first in the line to be overwritten.)

David

#18 lore raw

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

Quoted 11 lines · David Laight

On Thu, 23 Jul 2026 20:49:13 -0400
Brian Gerst <[email protected]> wrote:

...

Quoted 6 lines

I had originally thought that the test for RCX==0 was also at the end of the loop, after the decrement. But a closer look at the pseudo-code in the SDM does show that the test is at the start of the loop. Starting with RCX==0 does indeed skip the comparison and does not touch ZF. Your suggestion is good, but should have a comment explaining why it's there to someone reading the code in the future.

Quoted 7 lines

I have a feeling that was a change between the 8086 and 80286. I don't have an 8086 book (any more) but I'm sure I remember fixing bugs where %cx of zero caused 65536 iterations.
(Fun to find because the vector table is 'first in the line to be overwritten.)

David

No, but on the Z80 0 meant 64K.

maintainer
#19 lore raw

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

In reply to Brian Gerst
Quoted 2 lines · Brian Gerst

Your suggestion is good, but should have a comment explaining why it's there to someone reading the code in the future.

I have this there now:

arch/x86/boot/string.c +5 -0
index 6827551720dd..7bc34f9272fb 100644
@@ -32,6 +32,11 @@
 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)
Signature · 4 lines
Regards/Gruss,
    Boris.

https://people.kernel.org/tglx/notes-about-netiquette
#20 lore raw

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

In reply to H. Peter Anvin
Quoted 52 lines · H. Peter Anvin
Quoted 51 lines · Borislav Petkov

hpa in To:.

Quoted 5 lines · Mauricio Faria de Oliveira

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

Please use capital letters for insns: REPE, CMPSB and you don't need to put words in '' - it reads fine without them.

Quoted 19 lines

the condition-code/zero flag, so the result is based on a stale flag.

Those are pre-existing issues found by Sashiko.

---
 arch/x86/include/asm/shared/string.h | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
arch/x86/include/asm/shared/string.h +0 -1
index 02b92927553f7b8e1c87e6122bbaa70439e57ea7..166274e44f3cb49e3dccab3cdac281d67aef5d44 100644
@@ -11,8 +11,12 @@ static __always_inline int __inline_memcmp(const void *s1, const void *s2, size_
 {
 	bool diff;
 
-	asm("repe cmpsb"

arch/x86/include/asm/shared/string.h static __always_inline int __inline_memcmp(const void *s1, const void *s2, size_ · line 22

Quoted 10 lines
arch/x86/include/asm/shared/string.h +6 -1
-	    : "=@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;
 }

So this is a fix which should probably go to stable, I think. Going back into git history, it points to

62bd0337d0c4 ("Top header file for new x86 setup code")

from 2007. And we have carried it this way through the years and who knows what hit this or not. So please make this the first patch in your set - you can even send it separately so that I can get route it through stable.

Quoted 6 lines
Quoted 4 lines

Then you can base the rest ontop.

Thx.

Also, this is silly. Instead of adding a whole separate test, just do "test %3,%3" before the repe to set ZF and let the REPE skip.

This is elegant; done. Thanks!

Signature · 1 line
Mauricio
#21 lore raw

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

In reply to Borislav Petkov
Quoted 47 lines · Borislav Petkov

hpa in To:.

Quoted 5 lines · Mauricio Faria de Oliveira

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

Please use capital letters for insns: REPE, CMPSB and you don't need to put words in '' - it reads fine without them.

Quoted 19 lines

the condition-code/zero flag, so the result is based on a stale flag.

Those are pre-existing issues found by Sashiko.

---
 arch/x86/include/asm/shared/string.h | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
arch/x86/include/asm/shared/string.h +0 -1
index 02b92927553f7b8e1c87e6122bbaa70439e57ea7..166274e44f3cb49e3dccab3cdac281d67aef5d44 100644
@@ -11,8 +11,12 @@ static __always_inline int __inline_memcmp(const void *s1, const void *s2, size_
 {
 	bool diff;
 
-	asm("repe cmpsb"

arch/x86/include/asm/shared/string.h static __always_inline int __inline_memcmp(const void *s1, const void *s2, size_ · line 22

Quoted 10 lines
arch/x86/include/asm/shared/string.h +6 -1
-	    : "=@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;
 }

So this is a fix which should probably go to stable, I think. Going back into git history, it points to

62bd0337d0c4 ("Top header file for new x86 setup code")

Quoted 8 lines

from 2007. And we have carried it this way through the years and who knows what hit this or not. So please make this the first patch in your set - you can even send it separately so that I can get route it through stable.

Then you can base the rest ontop.

Thx.

I suspect that the zero-length case simply can't happen in the real code, but who knows; the use of the code has expanded over the years.

#22 lore raw

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

Quoted 52 lines · H. Peter Anvin
Quoted 51 lines · Borislav Petkov

hpa in To:.

Quoted 5 lines · Mauricio Faria de Oliveira

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

Please use capital letters for insns: REPE, CMPSB and you don't need to put words in '' - it reads fine without them.

Quoted 19 lines

the condition-code/zero flag, so the result is based on a stale flag.

Those are pre-existing issues found by Sashiko.

---
 arch/x86/include/asm/shared/string.h | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
arch/x86/include/asm/shared/string.h +0 -1
index 02b92927553f7b8e1c87e6122bbaa70439e57ea7..166274e44f3cb49e3dccab3cdac281d67aef5d44 100644
@@ -11,8 +11,12 @@ static __always_inline int __inline_memcmp(const void *s1, const void *s2, size_
 {
 	bool diff;
 
-	asm("repe cmpsb"

arch/x86/include/asm/shared/string.h static __always_inline int __inline_memcmp(const void *s1, const void *s2, size_ · line 22

Quoted 10 lines
arch/x86/include/asm/shared/string.h +6 -1
-	    : "=@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;
 }

So this is a fix which should probably go to stable, I think. Going back into git history, it points to

62bd0337d0c4 ("Top header file for new x86 setup code")

from 2007. And we have carried it this way through the years and who knows what hit this or not. So please make this the first patch in your set - you can even send it separately so that I can get route it through stable.

Quoted 6 lines
Quoted 4 lines

Then you can base the rest ontop.

Thx.

I suspect that the zero-length case simply can't happen in the real code, but who knows; the use of the code has expanded over the years.

Agreed, and its use may expand more in the future, being in a header (e.g., this very series :)

Thanks,

Signature · 1 line
Mauricio
#23 lore raw

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

In reply to Borislav Petkov
Quoted 11 lines · Borislav Petkov

hpa in To:.

Quoted 5 lines · Mauricio Faria de Oliveira

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

Please use capital letters for insns: REPE, CMPSB and you don't need to put words in '' - it reads fine without them.

Ok.

Quoted 34 lines
Quoted 20 lines

the condition-code/zero flag, so the result is based on a stale flag.

Those are pre-existing issues found by Sashiko.

---
 arch/x86/include/asm/shared/string.h | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
arch/x86/include/asm/shared/string.h +0 -1
index 02b92927553f7b8e1c87e6122bbaa70439e57ea7..166274e44f3cb49e3dccab3cdac281d67aef5d44 100644
@@ -11,8 +11,12 @@ static __always_inline int __inline_memcmp(const void *s1, const void *s2, size_
 {
 	bool diff;
 
-	asm("repe cmpsb"

arch/x86/include/asm/shared/string.h static __always_inline int __inline_memcmp(const void *s1, const void *s2, size_ · line 22

Quoted 10 lines
arch/x86/include/asm/shared/string.h +6 -1
-	    : "=@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;
 }

So this is a fix which should probably go to stable, I think. Going back into git history, it points to

Quoted 5 lines

62bd0337d0c4 ("Top header file for new x86 setup code")

from 2007. And we have carried it this way through the years and who knows what hit this or not. So please make this the first patch in your set - you can even send it separately so that I can get route it through stable.

Sure:
https://lore.kernel.org/all/[email protected]/

Quoted 1 line

Then you can base the rest ontop.

Done:
https://lore.kernel.org/all/[email protected]/

Thanks,

Quoted 2 lines

Thx.

Signature · 1 line
Mauricio
#24 lore raw

PATCH v7 3/5 x86/asm: group inline string functions

In reply to Mauricio Faria de Oliveira

Group the __inline string functions in the same header.

Use <asm/shared/string.h> since __inline_memcmp() must remain there for use by arch/x86/boot/string.c.

---
 arch/x86/include/asm/shared/string.h | 26 ++++++++++++++++++++++++++
 arch/x86/include/asm/string.h        | 21 +--------------------
 2 files changed, 27 insertions(+), 20 deletions(-)
arch/x86/include/asm/shared/string.h +26 -0
index 166274e44f3cb49e3dccab3cdac281d67aef5d44..9714fbda8b734e20686bfb88a6ca857bfee59e1a 100644
@@ -2,6 +2,32 @@
 #ifndef _ASM_X86_SHARED_STRING_H
 #define _ASM_X86_SHARED_STRING_H
 
+/*
+ * The __inline string functions are grouped in this file for consistency and
+ * for use by arch/x86/boot code due to limitations on including asm/string.h.
+ */
+
+static __always_inline void *__inline_memcpy(void *to, const void *from, size_t len)
+{
+	void *ret = to;
+
+	asm volatile("rep movsb"
+		     : "+D" (to), "+S" (from), "+c" (len)
+		     : : "memory");
+	return ret;
+}
+
+static __always_inline void *__inline_memset(void *s, int v, size_t n)
+{
+	void *ret = s;
+
+	asm volatile("rep stosb"
+		     : "+D" (s), "+c" (n)
+		     : "a" ((uint8_t)v)
+		     : "memory");
+	return ret;
+}
+
 /*
  * This inline memcmp() returns 0 (equal) or 1 (not equal).
  * The regular memcmp() returns <0 (less than), 0 (equal), or >0 (greater than)
arch/x86/include/asm/string.h +1 -20
index 9cb5aae7fba9ffcf0f5af8f939d30467750ccaa9..dbf59f0d4cca71e2ddce0d8764aeec8782236669 100644
@@ -8,25 +8,6 @@
 # include <asm/string_64.h>
 #endif
 
-static __always_inline void *__inline_memcpy(void *to, const void *from, size_t len)
-{
-	void *ret = to;
-
-	asm volatile("rep movsb"
-		     : "+D" (to), "+S" (from), "+c" (len)
-		     : : "memory");
-	return ret;
-}
-
-static __always_inline void *__inline_memset(void *s, int v, size_t n)
-{
-	void *ret = s;
-
-	asm volatile("rep stosb"
-		     : "+D" (s), "+c" (n)
-		     : "a" ((uint8_t)v)
-		     : "memory");
-	return ret;
-}
+#include <asm/shared/string.h>
 
 #endif /* _ASM_X86_STRING_H */
Signature · 1 line
2.47.3
maintainer
#25 lore raw

Re: [PATCH v7 3/5] x86/asm: group inline string functions

Quoted 4 lines · Mauricio Faria de Oliveira

Group the __inline string functions in the same header.

Use <asm/shared/string.h> since __inline_memcmp() must remain there for use by arch/x86/boot/string.c.

"No functional changes."

Quoted 11 lines
---
 arch/x86/include/asm/shared/string.h | 26 ++++++++++++++++++++++++++
 arch/x86/include/asm/string.h        | 21 +--------------------
 2 files changed, 27 insertions(+), 20 deletions(-)
arch/x86/include/asm/shared/string.h +0 -0
index 166274e44f3cb49e3dccab3cdac281d67aef5d44..9714fbda8b734e20686bfb88a6ca857bfee59e1a 100644

arch/x86/include/asm/shared/string.h line 7

Quoted 6 lines
arch/x86/include/asm/shared/string.h +3 -0
@@ -2,6 +2,32 @@
 #ifndef _ASM_X86_SHARED_STRING_H
 #define _ASM_X86_SHARED_STRING_H
 
+/*
+ * The __inline string functions are grouped in this file for consistency and
+ * for use by arch/x86/boot code due to limitations on including asm/string.h.

What limitations?

Either explain them or don't bother adding a comment. asm/shared/ is generally an understood concept if you grep the tree and you don't really need a comment here.

Signature · 4 lines
Regards/Gruss,
    Boris.

https://people.kernel.org/tglx/notes-about-netiquette
#26 lore raw

Re: [PATCH v7 3/5] x86/asm: group inline string functions

Quoted 7 lines · Borislav Petkov
Quoted 4 lines · Mauricio Faria de Oliveira

Group the __inline string functions in the same header.

Use <asm/shared/string.h> since __inline_memcmp() must remain there for use by arch/x86/boot/string.c.

"No functional changes."

Ok, added.

Quoted 21 lines
Quoted 12 lines
---
 arch/x86/include/asm/shared/string.h | 26 ++++++++++++++++++++++++++
 arch/x86/include/asm/string.h        | 21 +--------------------
 2 files changed, 27 insertions(+), 20 deletions(-)
arch/x86/include/asm/shared/string.h +0 -0
index 166274e44f3cb49e3dccab3cdac281d67aef5d44..9714fbda8b734e20686bfb88a6ca857bfee59e1a 100644

arch/x86/include/asm/shared/string.h line 7

Quoted 6 lines
arch/x86/include/asm/shared/string.h +3 -0
@@ -2,6 +2,32 @@
 #ifndef _ASM_X86_SHARED_STRING_H
 #define _ASM_X86_SHARED_STRING_H
 
+/*
+ * The __inline string functions are grouped in this file for consistency and
+ * for use by arch/x86/boot code due to limitations on including asm/string.h.

What limitations?

Quoted 3 lines

Either explain them or don't bother adding a comment. asm/shared/ is generally an understood concept if you grep the tree and you don't really need a comment here.

Ok, removed.

Thanks,

Signature · 1 line
Mauricio
#27 lore raw

PATCH v7 4/5 x86/cpuid: fix unbootable VMs by really inlining memcmp() in hypervisor_cpuid_base()

In reply to Mauricio Faria de Oliveira

Even with __builtin the compiler may decide to use the out of line function instead of the inline implementation.

The existing code is broken with gcc-14/15 but not gcc-12/13 (Ubuntu 25.10) and vmlinux no longer boots with CONFIG_PVH if CONFIG_KASAN_GENERIC is set.

For testing purposes, if the size argument is reduced from 12 to 8 then the compiler decides to use the inline implementation; that shows results vary.

Switch the builtin to the inline implementation to address it.

Fixes 416a33c9afce ("x86/cpu: fix unbootable VMs by inlining memcmp() in hypervisor_cpuid_base()")
---
 arch/x86/include/asm/cpuid/api.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/include/asm/cpuid/api.h +1 -1
index 82eddfa2347b32b76c2ea9b85f005ca5416ac71f..2d9f3d4d63de6e721f275d9e80d372edbdfedf30 100644
@@ -204,7 +204,7 @@ static inline u32 cpuid_base_hypervisor(const char *sig, u32 leaves)
 		 * from PVH early boot code before instrumentation is set up
 		 * and memcmp() itself may be instrumented.
 		 */
-		if (!__builtin_memcmp(sig, signature, 12) &&
+		if (!__inline_memcmp(sig, signature, 12) &&
 		    (leaves == 0 || ((eax - base) >= leaves)))
 			return base;
 	}
Signature · 1 line
2.47.3
#28 lore raw

PATCH v7 5/5 x86/pvh: fix unbootable VMs by really inlining memset() in xen_prepare_pvh()

In reply to Mauricio Faria de Oliveira

Even with __builtin the compiler may decide to use the out of line function instead of the inline implementation.

This particular one (still) generated the inline implementation as expected (at least in these compiler versions) but this is not guaranteed to remain.

Switch the builtin to the inline implementation to address it.

Fixes fbe5a6dfe492 ("xen, pvh: fix unbootable VMs by inlining memset() in xen_prepare_pvh()")
---
 arch/x86/platform/pvh/enlighten.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
arch/x86/platform/pvh/enlighten.c +2 -1
index f2053cbe9b0ce3d2178938269607c652ae8f528e..cb442cbd9d828619421babb281bfe9759edbca8a 100644
@@ -8,6 +8,7 @@
 #include <asm/hypervisor.h>
 #include <asm/e820/api.h>
 #include <asm/x86_init.h>
+#include <asm/string.h>
 
 #include <asm/xen/interface.h>
 
@@ -129,7 +130,7 @@ void __init xen_prepare_pvh(void)
 	 * This must not compile to "call memset" because memset() may be
 	 * instrumented.
 	 */
-	__builtin_memset(&pvh_bootparams, 0, sizeof(pvh_bootparams));
+	__inline_memset(&pvh_bootparams, 0, sizeof(pvh_bootparams));
 
 	hypervisor_specific_init(xen_guest);
 
Signature · 1 line
2.47.3