mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: "H. Peter Anvin" <hpa@zytor.com>
To: Brian Gerst <brgerst@gmail.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>,
	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 16:13:02 -0700	[thread overview]
Message-ID: <2f22094d-b8c5-46ec-92b9-dd6752a8dc2e@zytor.com> (raw)
In-Reply-To: <CAMzpN2hyupAUk-XGRzM2wwQojPrsN66jWtT+saVf1NWdOzFXuw@mail.gmail.com>

On 2026-07-23 12:37, Brian Gerst wrote:
> On Wed, Jul 22, 2026 at 2:53 PM H. Peter Anvin <hpa@zytor.com> wrote:
>>
>> On July 22, 2026 10:03:34 AM PDT, Borislav Petkov <bp@alien8.de> wrote:
>>> hpa in To:.
>>>
>>> On Tue, Jul 21, 2026 at 12:56:43PM -0300, Mauricio Faria de Oliveira wrote:
>>>> 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.
>>>
>>>> the condition-code/zero flag, so the result is based on a stale flag.
>>>>
>>>> Those are pre-existing issues found by Sashiko.
>>>>
>>>> Link: https://sashiko.dev/#/patchset/20260701-pvh-kasan-inline-v6-0-ba99045dfa9f%40igalia.com
>>>> Signed-off-by: Mauricio Faria de Oliveira <mfo@igalia.com>
>>>> ---
>>>>  arch/x86/include/asm/shared/string.h | 8 ++++++--
>>>>  1 file changed, 6 insertions(+), 2 deletions(-)
>>>>
>>>> diff --git a/arch/x86/include/asm/shared/string.h b/arch/x86/include/asm/shared/string.h
>>>> index 02b92927553f7b8e1c87e6122bbaa70439e57ea7..166274e44f3cb49e3dccab3cdac281d67aef5d44 100644
>>>> --- a/arch/x86/include/asm/shared/string.h
>>>> +++ b/arch/x86/include/asm/shared/string.h
>>>> @@ -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;
>>>>  }
>>>
>>> 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.
>>>
>>
>> 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."
> 

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

	-hpa


  reply	other threads:[~2026-07-23 23:13 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
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 [this message]
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=2f22094d-b8c5-46ec-92b9-dd6752a8dc2e@zytor.com \
    --to=hpa@zytor.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=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®