lists.openwall.net   lists  /  announce  owl-users  owl-dev  john-users  john-dev  passwdqc-users  yescrypt  popa3d-users  /  oss-security  kernel-hardening  musl  sabotage  tlsify  passwords  /  crypt-dev  xvendor  /  Bugtraq  Full-Disclosure  linux-kernel  linux-netdev  linux-ext4  linux-hardening  linux-cve-announce  PHC 
Open Source and information security mailing list archives
 
Hash Suite: Windows password security audit tool. GUI, reports in PDF.
[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <20250609220901.5059217c@pumpkin>
Date: Mon, 9 Jun 2025 22:09:01 +0100
From: David Laight <david.laight.linux@...il.com>
To: Peter Zijlstra <peterz@...radead.org>
Cc: Uros Bizjak <ubizjak@...il.com>, Mateusz Guzik <mjguzik@...il.com>,
 torvalds@...ux-foundation.org, mingo@...hat.com, x86@...nel.org,
 linux-kernel@...r.kernel.org
Subject: Re: [PATCH v2] x86: prevent gcc from emitting rep movsq/stosq for
 inlined ops

On Fri, 6 Jun 2025 09:20:29 +0200
Peter Zijlstra <peterz@...radead.org> wrote:

> On Fri, Jun 06, 2025 at 08:27:39AM +0200, Uros Bizjak wrote:
> > On Fri, Jun 6, 2025 at 8:13 AM Uros Bizjak <ubizjak@...il.com> wrote:  
> > >
> > > On Thu, Jun 5, 2025 at 9:00 PM Peter Zijlstra <peterz@...radead.org> wrote:  
> > > >
> > > > On Thu, Jun 05, 2025 at 06:47:33PM +0200, Mateusz Guzik wrote:  
> > > > > gcc is over eager to use rep movsq/stosq (starts above 40 bytes), which
> > > > > comes with a significant penalty on CPUs without the respective fast
> > > > > short ops bits (FSRM/FSRS).  
> > > >
> > > > I don't suppose there's a magic compiler toggle to make it emit prefix
> > > > padded 'rep movs'/'rep stos' variants such that they are 5 bytes each,
> > > > right?
> > > >
> > > > Something like:
> > > >
> > > >    2e 2e 2e f3 a4          cs cs rep movsb %ds:(%rsi),%es:(%rdi)  
> > >
> > > This won't fly, because gas complains:
> > >
> > > z.s: Assembler messages:
> > > z.s:1: Error: same type of prefix used twice  
> > 
> > However, it is possible to use " cs ; cs ; cs ; rep movsb".   
> 
> Heh, the way I encoded it was:
> 
> 	.byte 0x2e, 0x2e, 0x2e
> 	rep movsb %ds:(%rsi), %es:(%rdi)
> 
> GCC compiled it, and then objdump grokked it (although it outputs one CS
> too few). Your variant is much nicer though.
> 
> > We can add a compile flag to the compiler, and it will be able to emit
> > the desired sequence.

You want the compiler to use 'rep movsw', 'rep movsl' or 'rep movsq' if it knows
the size is a multiple of 2, 4 or 8.
Then you can substitute a suitable function.
But replacing the 'rep movsb' with a call is much more flexible.

You do need to allow for the REX prefix (rep movsq is f3 48 a5).

I also suspect it would be neater to repeat the 'rep' prefix instead of
using the 'cs' prefix.

> 
> Thanks; Linus, this would be acceptable?

Any compiler change isn't going to be useful in the short term.
(Unless the kernel sources start including gcc.)

	David


Powered by blists - more mailing lists

Powered by Openwall GNU/*/Linux Powered by OpenVZ