mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Mauricio Faria de Oliveira <mfo@igalia.com>
To: "H. Peter Anvin" <hpa@zytor.com>, Borislav Petkov <bp@alien8.de>
Cc: Jan Beulich <jbeulich@suse.com>,
	Thomas Gleixner <tglx@kernel.org>, Ingo Molnar <mingo@redhat.com>,
	Dave Hansen <dave.hansen@linux.intel.com>,
	x86@kernel.org, Juergen Gross <jgross@suse.com>,
	Alexey Dobriyan <adobriyan@gmail.com>,
	Boris Ostrovsky <boris.ostrovsky@oracle.com>,
	kernel-dev@igalia.com, linux-kernel@vger.kernel.org,
	xen-devel@lists.xenproject.org
Subject: Re: [PATCH v7 2/5] x86/asm: add volatile, clobbers and zero-length check in inline memcmp
Date: Thu, 23 Jul 2026 21:35:02 -0300	[thread overview]
Message-ID: <8df0340f0dc7f6c303c6a7da78fad7b8@igalia.com> (raw)
In-Reply-To: <5e19b195-0ca2-4510-81cb-497b40e4aaf5@zytor.com>

On 2026-07-23 20:12, H. Peter Anvin wrote:
> On 2026-07-22 23:59, Jan Beulich wrote:
>>> 
>>> 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:

> 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

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

> 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/324ef97b16f52e0ccc72f6381d1b5dd2@igalia.com/
[1]
https://gcc.gnu.org/onlinedocs/gcc/Extended-Asm.html#Clobbers-and-Scratch-Registers-1

cheers,

> 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;
> }


-- 
Mauricio

  reply	other threads:[~2026-07-24  0:35 UTC|newest]

Thread overview: 29+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-07-21 15:56 [PATCH v7 0/5] x86/pvh: fix unbootable VMs again (PVH + KASAN) Mauricio Faria de Oliveira
2026-07-21 15:56 ` [PATCH v7 1/5] x86/asm, x86/boot: expose inline memcmp Mauricio Faria de Oliveira
2026-07-21 15:56 ` [PATCH v7 2/5] x86/asm: add volatile, clobbers and zero-length check in " Mauricio Faria de Oliveira
2026-07-22 17:03   ` Borislav Petkov
2026-07-22 18:45     ` H. Peter Anvin
2026-07-23  6:59       ` Jan Beulich
2026-07-23 23:12         ` H. Peter Anvin
2026-07-24  0:35           ` Mauricio Faria de Oliveira [this message]
2026-07-24 21:53             ` H. Peter Anvin
2026-07-24 23:35               ` H. Peter Anvin
2026-07-24 23:57                 ` H. Peter Anvin
2026-07-25  0:25                   ` H. Peter Anvin
2026-07-25 10:27                     ` David Laight
2026-07-23 19:37       ` Brian Gerst
2026-07-23 23:13         ` H. Peter Anvin
2026-07-24  0:49           ` Brian Gerst
2026-07-24 21:40             ` H. Peter Anvin
2026-07-25 10:34             ` David Laight
2026-07-26  2:57               ` H. Peter Anvin
2026-07-28 23:33             ` Borislav Petkov
2026-07-23 23:20       ` Mauricio Faria de Oliveira
2026-07-22 18:48     ` H. Peter Anvin
2026-07-23 23:26       ` Mauricio Faria de Oliveira
2026-07-23 23:17     ` Mauricio Faria de Oliveira
2026-07-21 15:56 ` [PATCH v7 3/5] x86/asm: group inline string functions Mauricio Faria de Oliveira
2026-07-23 21:59   ` Borislav Petkov
2026-07-24  0:51     ` Mauricio Faria de Oliveira
2026-07-21 15:56 ` [PATCH v7 4/5] x86/cpuid: fix unbootable VMs by really inlining memcmp() in hypervisor_cpuid_base() Mauricio Faria de Oliveira
2026-07-21 15:56 ` [PATCH v7 5/5] x86/pvh: fix unbootable VMs by really inlining memset() in xen_prepare_pvh() Mauricio Faria de Oliveira

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=8df0340f0dc7f6c303c6a7da78fad7b8@igalia.com \
    --to=mfo@igalia.com \
    --cc=adobriyan@gmail.com \
    --cc=boris.ostrovsky@oracle.com \
    --cc=bp@alien8.de \
    --cc=dave.hansen@linux.intel.com \
    --cc=hpa@zytor.com \
    --cc=jbeulich@suse.com \
    --cc=jgross@suse.com \
    --cc=kernel-dev@igalia.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mingo@redhat.com \
    --cc=tglx@kernel.org \
    --cc=x86@kernel.org \
    --cc=xen-devel@lists.xenproject.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®