mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Davide Libenzi <davidel@xmailserver.org>
To: "Peter T. Breuer" <ptb@it.uc3m.es>
Cc: Linux Kernel Mailing List <linux-kernel@vger.kernel.org>
Subject: Re: recursive spinlocks. Shoot.
Date: Mon, 19 May 2003 10:15:54 -0700 (PDT)	[thread overview]
Message-ID: <Pine.LNX.4.55.0305190957520.4379@bigblue.dev.mcafeelabs.com> (raw)
In-Reply-To: <200305191248.h4JCmti06492@oboe.it.uc3m.es>

On Mon, 19 May 2003, Peter T. Breuer wrote:

> "A month of sundays ago Davide Libenzi wrote:"
> > On Mon, 19 May 2003, Peter T. Breuer wrote:
> >
> > > No. This is not true. Imagine two threads, timed as follows ...
> > >
> > >     .
> > >     .
> > >     .
> > >     .
> > > if ((snl)->uniq == current) {
> > > atomic_inc(&(snl)->count); 		.
> > > } else { 				.
> > > spin_lock(&(snl)->lock);		.
> > > atomic_inc(&(snl)->count);		.
> > > (snl)->uniq = current; 	  <->	if ((snl)->uniq == current) {
> > > 				atomic_inc(&(snl)->count);
> > > 				} else {
> > > 				spin_lock(&(snl)->lock);
> > > 				atomic_inc(&(snl)->count);
> > > 				(snl)->uniq = current;
> > >
> > >
> > > There you are. One hits the read exactly at the time the other does a
> > > write. Bang.
> >
> > So, what's bang for you ? The second task (the one that reads "uniq")
> > will either see "uniq" as NULL or as (task1)->current. And it'll go
> > acquiring the lock, as expected. Check it again ...
>
> Perhaps I should expand on my earlier answer ...
>
> (1) while, with some luck, writing may be atomic on ia32 (and I'm not
> sure it is, I'm only prepared to guarantee it for the lower bits, and I
> really don't know about zeroing the carry and so on), I actually doubt
> that reading is atomic, or we wouldn't need the atomic_read
> construction!

Look at atomic read :

$ emacs `find /usr/src/linux/include -name atomic.h | xargs`


> (3) even if one gets either one or the other answer, one of them would
> be the wrong answer, and you clearly intend the atomic_inc of the
> counter to be done in the same atomic region as the setting to current,
> which would be a programming hypothesis that is broken when the wrong
> answer comes up.

Atomic inc/dec/add/sub are different to read/write an aligned sizeof(int)
memory location. An aligned sizeof(int) read/write must be atomic for the
bare bone CPU memory coherency. While add/sub/add/inc are (or at least can
be) split in MEMOP(load)->ALUOP(?)->MEMOP(store) whose full cycle is not
guaranteed to be atomic, load/store of sizeof(int) aligned memory location
are not split is every CPU whose designer was not drunk during the
architectural phase. Or better, if they are split due some sort of HW
limitation, the HW itself has to guarentee the atomicity of the operation.
Intel tries also to guarantee the atomicity of non aligned load/store by
doing split locks on the memory bus. If you have :

int a;

thread1:
	for (;;)
		a = 1;

thread2:
	for (;;)
		a = -1;

being "a" an aligned memory location, a third thread reading "a" reads
either 1 or -1. Going back to the (doubtfully useful) code, you still have
to point out were it does bang ...



- Davide


  reply	other threads:[~2003-05-19 17:03 UTC|newest]

Thread overview: 58+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <20030518202013$5297@gated-at.bofh.it>
2003-05-18 23:15 ` Peter T. Breuer
2003-05-18 23:26   ` Davide Libenzi
2003-05-19 12:48     ` Peter T. Breuer
2003-05-19 17:15       ` Davide Libenzi [this message]
2003-05-19 17:27         ` Peter T. Breuer
2003-05-19 17:57           ` Alan Cox
2003-05-19 19:51         ` Peter T. Breuer
2003-05-19 20:22   ` Robert White
     [not found] <20030520231013$3d77@gated-at.bofh.it>
2003-05-21 14:16 ` Peter T. Breuer
     [not found] <20030518182010$0541@gated-at.bofh.it>
2003-05-18 19:09 ` Peter T. Breuer
2003-05-18 19:31   ` Davide Libenzi
2003-05-18 19:49     ` Peter T. Breuer
2003-05-18 20:13       ` Davide Libenzi
2003-05-19 20:47   ` Jan Hudec
2003-05-18  9:21 Peter T. Breuer
2003-05-18 16:30 ` Martin J. Bligh
2003-05-18 16:35   ` William Lee Irwin III
2003-05-18 16:49     ` Arjan van de Ven
2003-05-18 16:54       ` William Lee Irwin III
2003-05-18 17:14         ` Martin J. Bligh
2003-05-18 17:24     ` Peter T. Breuer
2003-05-18 22:34       ` David Woodhouse
2003-05-19 13:37         ` Peter T. Breuer
2003-05-19 13:45           ` Jens Axboe
2003-05-19 13:47           ` Arjan van de Ven
     [not found]           ` <mailman.1053352200.24653.linux-kernel2news@redhat.com>
2003-05-19 23:54             ` Pete Zaitcev
2003-05-20  0:03               ` viro
2003-05-20  0:03               ` Johannes Erdfelt
2003-05-20  3:12         ` Robert White
2003-05-20 11:59           ` Helge Hafting
2003-05-20 12:23             ` Richard B. Johnson
2003-05-20 21:05               ` Robert White
2003-05-20 21:42                 ` Richard B. Johnson
2003-05-20 23:06                   ` Robert White
2003-05-21 14:01                     ` Richard B. Johnson
2003-05-21 21:56                       ` Robert White
2003-05-22  0:13                         ` viro
2003-05-22  0:32                           ` Robert White
2003-05-22  0:46                         ` Carl-Daniel Hailfinger
2003-05-21  5:48                   ` Nikita Danilov
2003-05-22  1:00           ` Rik van Riel
2003-05-22  3:11             ` Robert White
2003-05-22  4:04               ` Nick Piggin
2003-05-22  4:42                 ` Peter T. Breuer
2003-05-22  5:09                   ` Nick Piggin
2003-05-23  0:19                 ` Robert White
2003-05-23  7:22                   ` Nikita Danilov
2003-05-23  9:07                     ` Helge Hafting
2003-05-23 12:18                     ` William Lee Irwin III
2003-05-24  2:39                       ` Robert White
2003-05-28 16:50                         ` Timothy Miller
2003-05-19  2:05       ` Kevin O'Connor
2003-05-19  6:19       ` Jan Hudec
2003-05-19 10:29       ` Helge Hafting
2003-05-19 11:37         ` Nikita Danilov
2003-05-22  1:21           ` Daniel Phillips
2003-05-19 14:28       ` Martin J. Bligh
2003-05-18 18:13 ` Davide Libenzi

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=Pine.LNX.4.55.0305190957520.4379@bigblue.dev.mcafeelabs.com \
    --to=davidel@xmailserver.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=ptb@it.uc3m.es \
    /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®