Re: [PATCH v9 1/5] x86/boot: Remove "cc" clobber from memcmp()
From: Borislav Petkov
Date: Tue Sep 01 2026 - 22:32:27 EST
On Sat, Aug 22, 2026 at 03:33:17PM -0300, Mauricio Faria de Oliveira wrote:
> According to the GCC documentation, conditions in the flags register
> (e.g., "=@ccnz") are output operands [1] and the compiler is aware [2].
>
> Also, clobbers (e.g., "cc") may not overlap with an output operand [2].
>
> Thus, remove the "cc" clobber as it is redudant, and overlaps with, the
> "=@ccnz" output operand.
>
> """
> 6.11.2.4 Flag Output Operands
>
> On some targets, a special form of output operand exists by which
> conditions in the flags register may be outputs of the asm. [...]
>
> 6.11.2.6 Clobbers and Scratch Registers
>
> While the compiler is aware of changes to entries listed in the
> output operands, [...]
>
> Clobber descriptions may not in any way overlap with an input or
> output operand. [...]
> """
>
> Reported-by: "H. Peter Anvin" <hpa@xxxxxxxxx>
> Link: https://lore.kernel.org/all/5e19b195-0ca2-4510-81cb-497b40e4aaf5@xxxxxxxxx/
> Fixes: a8c171c107c0 ("x86/boot: Add volatile, clobbers and zero-length test in memcmp()")
> Signed-off-by: Mauricio Faria de Oliveira <mfo@xxxxxxxxxx>
> Link: https://gcc.gnu.org/onlinedocs/gcc/Extended-Asm.html#Flag-Output-Operands [1]
> Link: https://gcc.gnu.org/onlinedocs/gcc/Extended-Asm.html#Clobbers-and-Scratch-Registers-1 [2]
> ---
> arch/x86/boot/string.c | 2 +-
> 1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/arch/x86/boot/string.c b/arch/x86/boot/string.c
> index 1632d40e1f545ae0665597069b568ea6b6c263e5..03278b4393887cb71cb063818d3378c4f52b06f8 100644
> --- a/arch/x86/boot/string.c
> +++ b/arch/x86/boot/string.c
> @@ -40,7 +40,7 @@ int memcmp(const void *s1, const void *s2, size_t len)
> asm volatile("test %3, %3\n\t"
> "repe cmpsb"
> : "=@ccnz" (diff), "+D" (s1), "+S" (s2), "+c" (len)
> - : : "cc", "memory");
> + : : "memory");
> return diff;
> }
So far, so good.
But then I'd expect that gcc would enforce that. I know it can't have it when
the clobbers contain input or output regs:
In function ‘__memcmp’,
inlined from ‘main’ at memcmp.c:25:6:
memcmp.c:11:9: error: ‘asm’ operand has impossible constraints or there are not enough registers
11 | asm volatile("test %3, %3\n\t"
| ^~~
but with "cc" clobbers it works.
That's gcc-16 btw.
Micha, Richi?
--
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquette