public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Willy Tarreau <willy@w.ods.org>
To: multisyncfe991@hotmail.com
Cc: linux-kernel@vger.kernel.org
Subject: Re: About a change to the implementation of spin lock in 2.6.12 kernel.
Date: Thu, 14 Jul 2005 07:16:54 +0200	[thread overview]
Message-ID: <20050714051653.GP8907@alpha.home.local> (raw)
In-Reply-To: <BAY108-DAV14071EF16A4482FB4B691593D10@phx.gbl>

Hi,

On Wed, Jul 13, 2005 at 07:20:06PM -0700, multisyncfe991@hotmail.com wrote:
> Hi,
> 
> I found _spin_lock used a LOCK instruction to make the following 
> operation "decb %0" atomic. As you know, LOCK instruction alone takes 
> almost 70 clock cycles to finish and this add lots of cost to the 
> _spin_lock. However _spin_unlock does not use this LOCK instruction and 
> it uses "movb $1,%0" instead since 4-byte writes on 4-byte aligned 
> addresses are atomic.

_spin_unlock does not need locked operations because when it is run, the
code is already known to be the only one to hold the lock, so it can
release it without checking what others do.

> So I want rewrite the _spin_lock defined spinlock.h 
> (/linux/include/asm-i386) as follows to reduce the overhead of _spin_lock 
> and make it more efficient.

It does not work. You cannot write an inter-cpu atomic test-and-set with
several unlocked instructions.

> #define spin_lock_string \
>        "\n1:\t" \
>        "cmpb $0,%0\n\t" \
>        "jle 2f\n\t" \

==> here, another thread or CPU can get the lock simultaneously.

>        "movb $0, %0\n\t" \
>        "jmp 3f\n" \
>        "2:\t" \
>        "rep;nop\n\t" \
>        "cmpb $0, %0\n\t" \
>        "jle 2b\n\t" \
>        "jmp 1b\n" \
>        "3:\n\t"
> 
> Compared with the original version as follows, LOCK instruction is 
> removed. I rebuilt the Intel e1000 Gigabit driver with this _spin_lock. 
> There is about 2% throughput improvement.
> #define spin_lock_string \
>            "\n1:\t" \
>            "lock ; decb %0\n\t" \
>            "jns 3f\n" \
>            "2:\t" \
>            "rep;nop\n\t" \
>            "cmpb $0,%0\n\t" \
>            "jle 2b\n\t" \
>            "jmp 1b\n" \
>            "3:\n\t"
> 
> Do you think I can get a better performance if I dig further?
> 
> Any ideas will be greatly appreciated,

well, of course with those methods you can improve performance, but you
lose the warranty that you're alone to get a lock, and that's bad.

another similar method to get a lock in some very controlled environment
is as follows :

  1:  cmp $0, %0
      jne 1b
      mov $CPUID, %0
      membar
      cmp $CPUID, %0
      jne 1b

This only works with same speed CPUs and interrupts disabled. But in todays
environments, this is very risky (hyperthreaded CPUs, etc...). However, this
is often OK for more deterministic CPUs such as microcontrollers.

Regards,
Willy


  reply	other threads:[~2005-07-14  5:22 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2005-07-14  2:20 About a change to the implementation of spin lock in 2.6.12 kernel multisyncfe991
2005-07-14  5:16 ` Willy Tarreau [this message]
2005-07-14 16:21   ` multisyncfe991
2005-07-14 16:26     ` Brandon Niemczyk
2005-11-09 17:57       ` Does Printk() block another CPU in dual cpu platforms? John Smith
2005-11-10  3:31         ` Fawad Lateef
2005-07-15 19:22     ` Volatile vs Non-Volatile Spin Locks on SMP multisyncfe991
2005-07-17 12:51       ` Joe Seigh
2005-07-18 13:40         ` Joe Seigh

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=20050714051653.GP8907@alpha.home.local \
    --to=willy@w.ods.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=multisyncfe991@hotmail.com \
    /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