From: "H. Peter Anvin" <hpa@zytor.com>
To: Mauricio Faria de Oliveira <mfo@igalia.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: Fri, 24 Jul 2026 16:57:45 -0700 [thread overview]
Message-ID: <e52ff0fa-ee19-45e3-9b45-573ec3c5da24@zytor.com> (raw)
In-Reply-To: <348133f5-8a78-4805-bf0a-b1b08cdc65f2@zytor.com>
On 2026-07-24 16:35, H. Peter Anvin wrote:
> On 2026-07-24 14:53, H. Peter Anvin wrote:
>>
>> 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
>>
> 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;
> }
>
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
next prev parent reply other threads:[~2026-07-24 23:58 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
2026-07-24 21:53 ` H. Peter Anvin
2026-07-24 23:35 ` H. Peter Anvin
2026-07-24 23:57 ` H. Peter Anvin [this message]
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=e52ff0fa-ee19-45e3-9b45-573ec3c5da24@zytor.com \
--to=hpa@zytor.com \
--cc=adobriyan@gmail.com \
--cc=boris.ostrovsky@oracle.com \
--cc=bp@alien8.de \
--cc=dave.hansen@linux.intel.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
Powered by JetHome