linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [Q] Implementation of spin_lock on i386: why "rep;nop" ?
@ 2001-09-17 16:16 Jean-Marc Saffroy
  2001-09-17 16:22 ` Dave Jones
                   ` (2 more replies)
  0 siblings, 3 replies; 9+ messages in thread
From: Jean-Marc Saffroy @ 2001-09-17 16:16 UTC (permalink / raw)
  To: linux-kernel, linux-smp

Hi all,

One of my coworkers directed my attention to the implementation of
spinlocks on IA-32. In spin_lock_string, we can read:

	"cmpb $0,%0\n\t" \
	"rep;nop\n\t" \
	"jle 2b\n\t" \

The "rep;nop" line looks dubious, since the IA-32 programmer's manual from
Intel (year 2001) mentions that the behaviour of REP is undefined when it
is not used with string opcodes. BTW, according to the same manual, REP is
supposed to modify ecx, but it looks like is is not the case here... which
is fortunate, since ecx is never saved. :-)

What is the intent behind this "rep;nop" ? Does it really rely on an
undocumented behaviour ?


Regards,

-- 
Jean-Marc Saffroy - Research Engineer - Silicomp Research Institute
mailto:saffroy@ri.silicomp.fr


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [Q] Implementation of spin_lock on i386: why "rep;nop" ?
  2001-09-17 16:16 [Q] Implementation of spin_lock on i386: why "rep;nop" ? Jean-Marc Saffroy
@ 2001-09-17 16:22 ` Dave Jones
  2001-09-17 17:19   ` Jean-Marc Saffroy
  2001-09-17 16:42 ` Richard B. Johnson
  2001-09-17 17:27 ` Alan Cox
  2 siblings, 1 reply; 9+ messages in thread
From: Dave Jones @ 2001-09-17 16:22 UTC (permalink / raw)
  To: Jean-Marc Saffroy; +Cc: linux-kernel, linux-smp

On Mon, 17 Sep 2001, Jean-Marc Saffroy wrote:

> What is the intent behind this "rep;nop" ? Does it really rely on an
> undocumented behaviour ?

Its used to stop Pentium 4's from cooking themselves.
See the P4 manuals for more info.

regards,

Dave.

-- 
| Dave Jones.        http://www.suse.de/~davej
| SuSE Labs


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [Q] Implementation of spin_lock on i386: why "rep;nop" ?
  2001-09-17 16:16 [Q] Implementation of spin_lock on i386: why "rep;nop" ? Jean-Marc Saffroy
  2001-09-17 16:22 ` Dave Jones
@ 2001-09-17 16:42 ` Richard B. Johnson
  2001-09-17 17:06   ` Jonathan Lundell
  2001-09-17 17:27 ` Alan Cox
  2 siblings, 1 reply; 9+ messages in thread
From: Richard B. Johnson @ 2001-09-17 16:42 UTC (permalink / raw)
  To: Jean-Marc Saffroy; +Cc: linux-kernel, linux-smp

On Mon, 17 Sep 2001, Jean-Marc Saffroy wrote:

> Hi all,
> 
> One of my coworkers directed my attention to the implementation of
> spinlocks on IA-32. In spin_lock_string, we can read:
> 
> 	"cmpb $0,%0\n\t" \
> 	"rep;nop\n\t" \
> 	"jle 2b\n\t" \
> 
> The "rep;nop" line looks dubious, since the IA-32 programmer's manual from
> Intel (year 2001) mentions that the behaviour of REP is undefined when it
> is not used with string opcodes. BTW, according to the same manual, REP is
> supposed to modify ecx, but it looks like is is not the case here... which
> is fortunate, since ecx is never saved. :-)
> 
> What is the intent behind this "rep;nop" ? Does it really rely on an
> undocumented behaviour ?
> 
> 
> Regards,

Well it's now documented although you have to search a web-site to
find it. Basically, it runs the CPU at low clock-speed when it's
busy-waiting. Since most all spin-locks lock for mere microseconds
it's unlikely that it does anything useful, but it can't hurt.

Cheers,
Dick Johnson

Penguin : Linux version 2.4.1 on an i686 machine (799.53 BogoMips).

    I was going to compile a list of innovations that could be
    attributed to Microsoft. Once I realized that Ctrl-Alt-Del
    was handled in the BIOS, I found that there aren't any.



^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [Q] Implementation of spin_lock on i386: why "rep;nop" ?
  2001-09-17 16:42 ` Richard B. Johnson
@ 2001-09-17 17:06   ` Jonathan Lundell
  0 siblings, 0 replies; 9+ messages in thread
From: Jonathan Lundell @ 2001-09-17 17:06 UTC (permalink / raw)
  To: linux-kernel

At 12:42 PM -0400 2001-09-17, Richard B. Johnson wrote:
>  > What is the intent behind this "rep;nop" ? Does it really rely on an
>  > undocumented behaviour ?
>
>Well it's now documented although you have to search a web-site to
>find it. Basically, it runs the CPU at low clock-speed when it's
>busy-waiting. Since most all spin-locks lock for mere microseconds
>it's unlikely that it does anything useful, but it can't hurt.

Sounds like a good opportunity for a comment....
-- 
/Jonathan Lundell.

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [Q] Implementation of spin_lock on i386: why "rep;nop" ?
  2001-09-17 16:22 ` Dave Jones
@ 2001-09-17 17:19   ` Jean-Marc Saffroy
  0 siblings, 0 replies; 9+ messages in thread
From: Jean-Marc Saffroy @ 2001-09-17 17:19 UTC (permalink / raw)
  To: Dave Jones; +Cc: linux-kernel, linux-smp

On Mon, 17 Sep 2001, Dave Jones wrote:

> On Mon, 17 Sep 2001, Jean-Marc Saffroy wrote:
>
> > What is the intent behind this "rep;nop" ? Does it really rely on an
> > undocumented behaviour ?
>
> Its used to stop Pentium 4's from cooking themselves.
> See the P4 manuals for more info.

Ok, I found it: actually it is the PAUSE opcode in the P4 instruction set,
and the doc for PAUSE mentions that it is equivalent to a NOP on older
IA-32 processors.

So no black magic here, except that "rep;nop" is a bit misleading, since
the Intel docs for REP and NOP do not mention PAUSE...

Thanks all for you help.


Regards,

-- 
Jean-Marc Saffroy - Research Engineer - Silicomp Research Institute
mailto:saffroy@ri.silicomp.fr


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [Q] Implementation of spin_lock on i386: why "rep;nop" ?
  2001-09-17 16:16 [Q] Implementation of spin_lock on i386: why "rep;nop" ? Jean-Marc Saffroy
  2001-09-17 16:22 ` Dave Jones
  2001-09-17 16:42 ` Richard B. Johnson
@ 2001-09-17 17:27 ` Alan Cox
  2001-09-17 19:47   ` H. Peter Anvin
  2001-09-19  3:42   ` Jamie Lokier
  2 siblings, 2 replies; 9+ messages in thread
From: Alan Cox @ 2001-09-17 17:27 UTC (permalink / raw)
  To: Jean-Marc Saffroy; +Cc: linux-kernel, linux-smp

> The "rep;nop" line looks dubious, since the IA-32 programmer's manual from
> Intel (year 2001) mentions that the behaviour of REP is undefined when it
> is not used with string opcodes. BTW, according to the same manual, REP is
> supposed to modify ecx, but it looks like is is not the case here... which
> is fortunate, since ecx is never saved. :-)

rep nop is a pentium IV operation. Its retroactively after testing defined
to be portable and ok. 

Alan

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [Q] Implementation of spin_lock on i386: why "rep;nop" ?
  2001-09-17 17:27 ` Alan Cox
@ 2001-09-17 19:47   ` H. Peter Anvin
  2001-09-19  3:42   ` Jamie Lokier
  1 sibling, 0 replies; 9+ messages in thread
From: H. Peter Anvin @ 2001-09-17 19:47 UTC (permalink / raw)
  To: linux-kernel

Followup to:  <E15j2BM-0007WU-00@the-village.bc.nu>
By author:    Alan Cox <alan@lxorguk.ukuu.org.uk>
In newsgroup: linux.dev.kernel
>
> > The "rep;nop" line looks dubious, since the IA-32 programmer's manual from
> > Intel (year 2001) mentions that the behaviour of REP is undefined when it
> > is not used with string opcodes. BTW, according to the same manual, REP is
> > supposed to modify ecx, but it looks like is is not the case here... which
> > is fortunate, since ecx is never saved. :-)
> 
> rep nop is a pentium IV operation. Its retroactively after testing defined
> to be portable and ok. 
> 

Now, the example brought up was assembly, but in general I really
think we should have a processor-independent wait_loop(); inline.
Right now we have a rep_nop(); inline which only works on x86 (and
presumably x86-64).

	-hpa
-- 
<hpa@transmeta.com> at work, <hpa@zytor.com> in private!
"Unix gives you enough rope to shoot yourself in the foot."
http://www.zytor.com/~hpa/puzzle.txt	<amsp@zytor.com>

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [Q] Implementation of spin_lock on i386: why "rep;nop" ?
  2001-09-17 17:27 ` Alan Cox
  2001-09-17 19:47   ` H. Peter Anvin
@ 2001-09-19  3:42   ` Jamie Lokier
  2001-09-19  4:06     ` Brian Gerst
  1 sibling, 1 reply; 9+ messages in thread
From: Jamie Lokier @ 2001-09-19  3:42 UTC (permalink / raw)
  To: Alan Cox; +Cc: Jean-Marc Saffroy, linux-kernel, linux-smp

Alan Cox wrote:
> > The "rep;nop" line looks dubious, since the IA-32 programmer's manual from
> > Intel (year 2001) mentions that the behaviour of REP is undefined when it
> > is not used with string opcodes. BTW, according to the same manual, REP is
> > supposed to modify ecx, but it looks like is is not the case here... which
> > is fortunate, since ecx is never saved. :-)
> 
> rep nop is a pentium IV operation. Its retroactively after testing defined
> to be portable and ok. 

Are we sure that the value of ECX doesn't matter on a 386?  Or does it
count down doing nops ECX times on a 386?

-- Jamie

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [Q] Implementation of spin_lock on i386: why "rep;nop" ?
  2001-09-19  3:42   ` Jamie Lokier
@ 2001-09-19  4:06     ` Brian Gerst
  0 siblings, 0 replies; 9+ messages in thread
From: Brian Gerst @ 2001-09-19  4:06 UTC (permalink / raw)
  To: Jamie Lokier; +Cc: Alan Cox, Jean-Marc Saffroy, linux-kernel, linux-smp

Jamie Lokier wrote:
> 
> Alan Cox wrote:
> > > The "rep;nop" line looks dubious, since the IA-32 programmer's manual from
> > > Intel (year 2001) mentions that the behaviour of REP is undefined when it
> > > is not used with string opcodes. BTW, according to the same manual, REP is
> > > supposed to modify ecx, but it looks like is is not the case here... which
> > > is fortunate, since ecx is never saved. :-)
> >
> > rep nop is a pentium IV operation. Its retroactively after testing defined
> > to be portable and ok.
> 
> Are we sure that the value of ECX doesn't matter on a 386?  Or does it
> count down doing nops ECX times on a 386?

Older processors ignore the rep prefix when used with non-string
opcodes.  %ecx should not be affected.

--
						Brian Gerst

^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2001-09-19  4:01 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2001-09-17 16:16 [Q] Implementation of spin_lock on i386: why "rep;nop" ? Jean-Marc Saffroy
2001-09-17 16:22 ` Dave Jones
2001-09-17 17:19   ` Jean-Marc Saffroy
2001-09-17 16:42 ` Richard B. Johnson
2001-09-17 17:06   ` Jonathan Lundell
2001-09-17 17:27 ` Alan Cox
2001-09-17 19:47   ` H. Peter Anvin
2001-09-19  3:42   ` Jamie Lokier
2001-09-19  4:06     ` Brian Gerst

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).