[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [PATCH v7 2/5] x86/asm: add volatile, clobbers and zero-length check in inline memcmp


  • To: "H. Peter Anvin" <hpa@xxxxxxxxx>
  • From: Brian Gerst <brgerst@xxxxxxxxx>
  • Date: Thu, 23 Jul 2026 20:49:13 -0400
  • Arc-authentication-results: i=1; mx.google.com; arc=none
  • Arc-message-signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20260327; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:dkim-signature; bh=qHdU6G1F9MKnhrV0MttU1GATQHfTchrhi/+r4Nb6dJY=; fh=kXUsuW/8EmFLxOq4p73gFZOIEx4lTXvKzfkxyuUpyIU=; b=i+K7Snu5jo0AWs9JPr+XbhCugPWoNMxKskrtMemv5QfD/Iuy/pNl3qwe7eLNqACvzg AgzUP1kT8Q2+Fo/ZGH7td/nuYjkoNdoKVPY4U4pqNRDFG/zMYLTD5NZJfjDedP0Auixi M6bAD3Y8Qv1Q7+xrv3OATXJEuXHo+9RC3Hv9ITd1Tgu4tjDDqVnJQt4yAS782MsLMzSB z9fwL7Cz8j2UJ4OFc43aRaPFjWbmtbpWOUBKHz9iWy6eVQlYVP2B8tGTlQWrquOiyJ6o k+qJ2Wuv+b1YMzaWZllGygivU40EsC5NvtQLwhMdNoH4aDbaNecknoEEZ0kWMTfELNl3 BclA==; darn=lists.xenproject.org
  • Arc-seal: i=1; a=rsa-sha256; t=1784854168; cv=none; d=google.com; s=arc-20260327; b=HS2evN/+RqM8LCF53jIhCkC9D8Km42+sy/cTN2Qspgumes786BOOvq4/r5mdmEAxL+ W1JvUTi618lwvv0K4Je6zwUywrLs4/v2bU/85LB0RlpN6/KOS1f6sxiGapA4+/DQDbWE dibxCczivLlKf3cbHTE/FoUn/YbRMZ0jS43yVW42ny4uYzZPYd36XYRBPFukJB0hmq9u sqf50goIngkoC4qfCbDakNZZuDVYSE0hAcxQjoH/DXejvbjQXqKb/0+EvASkYxKwDC3j M+g9VZFZ81Z9oDv8tgzgLzYtQCXGTPe8YfmWAcmbHKJrJZBqpIu5vpHVmxHu/M8CluG0 lN6A==
  • Authentication-results: eu.smtp.expurgate.cloud; dkim=pass header.s=20251104 header.d=gmail.com header.i="@gmail.com" header.h="Content-Transfer-Encoding:Content-Type:Cc:To:Subject:Message-ID:Date:From:In-Reply-To:References:MIME-Version"
  • Cc: Borislav Petkov <bp@xxxxxxxxx>, Mauricio Faria de Oliveira <mfo@xxxxxxxxxx>, Thomas Gleixner <tglx@xxxxxxxxxx>, Ingo Molnar <mingo@xxxxxxxxxx>, Dave Hansen <dave.hansen@xxxxxxxxxxxxxxx>, x86@xxxxxxxxxx, Juergen Gross <jgross@xxxxxxxx>, Alexey Dobriyan <adobriyan@xxxxxxxxx>, Boris Ostrovsky <boris.ostrovsky@xxxxxxxxxx>, kernel-dev@xxxxxxxxxx, linux-kernel@xxxxxxxxxxxxxxx, xen-devel@xxxxxxxxxxxxxxxxxxxx
  • Delivery-date: Fri, 24 Jul 2026 00:49:37 +0000
  • List-id: Xen developer discussion <xen-devel.lists.xenproject.org>

On Thu, Jul 23, 2026 at 7:13 PM H. Peter Anvin <hpa@xxxxxxxxx> wrote:
>
> On 2026-07-23 12:37, Brian Gerst wrote:
> > On Wed, Jul 22, 2026 at 2:53 PM H. Peter Anvin <hpa@xxxxxxxxx> wrote:
> >>
> >> On July 22, 2026 10:03:34 AM PDT, Borislav Petkov <bp@xxxxxxxxx> 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@xxxxxxxxxx>
> >>>> ---
> >>>>  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.

I had originally thought that the test for RCX==0 was also at the end
of the loop, after the decrement.  But a closer look at the
pseudo-code in the SDM does show that the test is at the start of the
loop.  Starting with RCX==0 does indeed skip the comparison and does
not touch ZF.  Your suggestion is good, but should have a comment
explaining why it's there to someone reading the code in the future.



 


Rackspace

Lists.xenproject.org is hosted with RackSpace, monitoring our
servers 24x7x365 and backed by RackSpace's Fanatical Support®.