From: David Laight <david.laight.linux@gmail.com>
To: "H. Peter Anvin" <hpa@zytor.com>
Cc: Borislav Petkov <bp@alien8.de>,
Mauricio Faria de Oliveira <mfo@igalia.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>,
Jan Beulich <jbeulich@suse.com>, Brian Gerst <brgerst@gmail.com>,
kernel-dev@igalia.com, linux-kernel@vger.kernel.org,
xen-devel@lists.xenproject.org
Subject: Re: [PATCH v9 2/5] x86/asm, x86/boot: expose inline memcmp()
Date: Wed, 9 Sep 2026 22:39:51 +0100 [thread overview]
Message-ID: <20260909223951.1fe5fe25@pumpkin> (raw)
In-Reply-To: <95D5AACE-1D91-402A-9B6B-8D03C3D90705@zytor.com>
On Wed, 09 Sep 2026 12:33:40 -0700
"H. Peter Anvin" <hpa@zytor.com> wrote:
> On September 9, 2026 12:28:38 PM PDT, David Laight <david.laight.linux@gmail.com> wrote:
> >On Wed, 9 Sep 2026 06:43:01 -0700
> >"H. Peter Anvin" <hpa@zytor.com> wrote:
> >
> >> On 2026-09-09 01:38, David Laight wrote:
> >> >>
> >> >> Here is an out-of-line compact memcmp() which works for both 16/32 and 64 bits:
> >> >>
> >> <broken code removed>
> >>
> >> >>
> >> >> On 64 bits it compiles to:
> >> >>
> >> >> 0000000000000000 <memcmp>:
> >> >> 0: 48 89 d1 mov %rdx,%rcx
> >> >> 3: 31 d2 xor %edx,%edx
> >> >> 5: 31 c0 xor %eax,%eax
> >> >> 7: f3 a6 repz cmpsb (%rdi),(%rsi)
> >> >> 9: 0f 97 c2 seta %dl
> >> >> c: 0f 92 c0 setb %al
> >> >> f: 29 d0 sub %edx,%eax
> >> >
> >> > That isn't the object code from the source ...
> >> >
> >> And that's the ultimate hint that a cut and paste error had happened.
> >>
> >> This was the actual source code.
> >>
> >> int memcmp(const void *s1, const void *s2, size_t len)
> >> {
> >> int lt, gt;
> >>
> >> /*
> >> * Note: for the benefit of 64-bit code, xDI and xSI are reversed
> >> * compared with what CMPSB uses; hence SETA and SETB are also reversed.
> >> *
> >> * The XOR statements set ZF = 1, CF = 0, which is required to handle
> >> * the case len == 0 correctly.
> >> */
> >> asm volatile("xor %[lt],%[lt] ; "
> >> "xor %[gt],%[gt] ; "
> >> "repe cmpsb ; "
> >> "seta %b[lt] ; "
> >> "setb %b[gt]"
> >> : "+D" (s1), "+S" (s2), "+c" (len),
> >> [lt] "=&q" (lt), [gt] "=&q" (gt)
> >> : : "cc", "memory");
> >> return gt - lt;
> >> }
> >>
> >
> >Try:
> >
> >int memcmp_2(const void *s1, const void *s2, unsigned long len)
> >{
> > signed char lt, gt;
> >
> > asm volatile("repe cmpsb ; "
> > "seta %[lt] ; "
> > "setb %[gt]"
> > : "+D" (s1), "+S" (s2), "+c" (len),
> > [lt] "=&q" (lt), [gt] "=&q" (gt)
> > : : "cc", "memory");
> > return (signed char)(gt - lt);
> >}
> >
> >https://www.godbolt.org/z/6hrxGxb18
> >
> >Saves the XORs - go away completely in the usual case of 'if (memcpy(....))'.
> >The 'mess' on the return statement moves the sign extend after the
> >subtract.
> >
> >David
>
> The xors are explicitly in the asm to deal with the len = 0 case (this is for the out of line version!)
>
> We need to enter with ZF = 1 CF = 0.
And, of course, I knew that.
They also zero the high 24bits of the registers.
Given the setup cost of 'repe cmpsb' I suspect the xor just add code
bytes.
David
next prev parent reply other threads:[~2026-09-09 21:39 UTC|newest]
Thread overview: 31+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-08-22 18:33 [PATCH v9 0/5] x86/pvh: fix unbootable VMs again (PVH + KASAN) Mauricio Faria de Oliveira
2026-08-22 18:33 ` [PATCH v9 1/5] x86/boot: Remove "cc" clobber from memcmp() Mauricio Faria de Oliveira
2026-09-01 13:16 ` Jan Beulich
2026-09-02 8:33 ` David Laight
2026-09-02 14:18 ` H. Peter Anvin
2026-09-02 2:31 ` Borislav Petkov
2026-09-02 13:29 ` Michael Matz
2026-09-02 13:48 ` Mauricio Faria de Oliveira
2026-09-03 0:01 ` Borislav Petkov
2026-09-03 0:07 ` Mauricio Faria de Oliveira
2026-09-03 0:39 ` Borislav Petkov
2026-09-03 8:40 ` David Laight
2026-09-02 23:58 ` Borislav Petkov
2026-08-22 18:33 ` [PATCH v9 2/5] x86/asm, x86/boot: expose inline memcmp() Mauricio Faria de Oliveira
2026-09-06 17:01 ` Borislav Petkov
2026-09-06 18:15 ` H. Peter Anvin
2026-09-08 18:04 ` Mauricio Faria de Oliveira
2026-09-08 19:32 ` Borislav Petkov
2026-09-08 21:06 ` Mauricio Faria de Oliveira
2026-09-10 3:38 ` Borislav Petkov
2026-09-08 23:16 ` H. Peter Anvin
2026-09-09 8:38 ` David Laight
2026-09-09 13:43 ` H. Peter Anvin
2026-09-09 19:28 ` David Laight
2026-09-09 19:33 ` H. Peter Anvin
2026-09-09 21:39 ` David Laight [this message]
2026-09-09 23:12 ` H. Peter Anvin
2026-09-08 17:57 ` Mauricio Faria de Oliveira
2026-08-22 18:33 ` [PATCH v9 3/5] x86/asm: group inline string functions Mauricio Faria de Oliveira
2026-08-22 18:33 ` [PATCH v9 4/5] x86/cpuid: fix unbootable VMs by really inlining memcmp() in hypervisor_cpuid_base() Mauricio Faria de Oliveira
2026-08-22 18:33 ` [PATCH v9 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=20260909223951.1fe5fe25@pumpkin \
--to=david.laight.linux@gmail.com \
--cc=adobriyan@gmail.com \
--cc=boris.ostrovsky@oracle.com \
--cc=bp@alien8.de \
--cc=brgerst@gmail.com \
--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=mfo@igalia.com \
--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®