mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Jamie Lokier <jamie@shareable.org>
To: "Richard B. Johnson" <root@chaos.analogic.com>
Cc: Timo Sirainen <tss@iki.fi>,
	Martin Konold <martin.konold@erfrakon.de>,
	linux-kernel@vger.kernel.org
Subject: Re: Lockless file reading
Date: Thu, 28 Aug 2003 13:39:58 +0100	[thread overview]
Message-ID: <20030828123958.GC6800@mail.jlokier.co.uk> (raw)
In-Reply-To: <Pine.LNX.4.53.0308280746400.2211@chaos>

Richard B. Johnson wrote:
> > > Even in machines that do load/store operations where individual
> > > components of those stores happen in groups, access to/from
> > > a buffer of such data is controlled (by hardware) so a write
> > > will complete before a read occurs.
> >
> > I don't understand what you mean by this.
> >
> > Do you mean that the writes are forced to appear on a different CPU in
> > the same order that they were written?  That isn't true on x86, for
> > two reasons: 1. writes aren't always in processor order (see
> > CONFIG_X86_OOSTORE and CONFIG_X86_PPRO_FENCE); 2. reads on the other
> > processor are out of order anyway.
> 
> I never said, nor even implied any such thing.

That's why I said I don't understand what you meant.  What does your
first paragraph above mean?

> [...] The implimentation detail of trimming the start or finish of a
> copy procedure trims the ends. That's, in fact, why it is
> impossible, yes, __impossible__ for the byte-sequence 0xABC to have
> both 0xA and 0xC copied without the 0xB being copied also.  There
> are no combinations of byte/word/long writes that will allow this to
> happen.

Correct.

> Whether or not there's an interrupt between each byte means nothing
> also. In the byte copy described, an interruopt at any time will
> simply leave:
> 
> XXX
> XXC
> XBC
> ABC
> 
> Clearly, if both A and C were copied, B was copied also.
> You can screw around with endian and use words and longwords
> as well. It just isn't possible for, in any 3-byte sequence
> for the middle byte to be missing.

What you say is correct about the intermediate states from
the writer's point of view, however a parallel reader _can_ observe
the middle byte missing.

The writing proceeds: A, B then C.  In that order.  No problem.

The writing CPU or task will always see these memory states:

   XXX
   AXX
   ABX
   ABC

Two problems for the reader, though: multiprocessor out of order
reads, and uniprocessor preemption.

Out of order means the reader can read the second byte _before_ the
first byte.  Which means the reader sees this:

  ...
  .X.
  AX.
  AXB

Preemption means this sequence of events can occur:

  Writer's                              Reader's
  destination buffer                    destination buffer
  --------------------------------------------------------

  XXX
	writer writes A
  AXX
	context switch to reader
	reader reads 2 bytes
					-> AX.
	context switch to writer
	writer writes B, C
  ABX
  ABC
	context switch to reader
	reader reads final byte
					-> AXB

The preemption case is a classic race between a writer and reader
moving in the same direction, alternating who is ahead.

Parallel memory access is not as simple as you make it out to be.  If
you don't understand this, you will have a hard time understanding locks,
set_task_state vs. __set_task_state, the wmb/rmb/smp_rmb() macros, etc.

-- Jamie

  reply	other threads:[~2003-08-28 12:40 UTC|newest]

Thread overview: 56+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2003-08-27 12:37 Timo Sirainen
2003-08-27 12:42 ` Martin Konold
2003-08-27 12:52   ` Timo Sirainen
2003-08-27 13:40     ` Richard B. Johnson
2003-08-27 14:56       ` Timo Sirainen
2003-08-27 23:39         ` Jamie Lokier
2003-08-28  0:52           ` Timo Sirainen
2003-08-27 18:42             ` Nagendra Singh Tomar
2003-08-28  8:40               ` Timo Sirainen
2003-08-27 21:15                 ` Nagendra Singh Tomar
2003-08-28  9:35                   ` Timo Sirainen
2003-08-27 21:52                     ` Nagendra Singh Tomar
2003-08-28 13:26                       ` Matthias Andree
2003-08-28  9:17                 ` David Schwartz
2003-08-28  8:42               ` Valdis.Kletnieks
2003-08-28 20:13                 ` David B. Stevens
2003-08-28  1:50             ` Jamie Lokier
2003-08-28  3:17               ` Timo Sirainen
2003-08-28  6:01                 ` Valdis.Kletnieks
2003-08-28  6:13                 ` Jamie Lokier
2003-08-28  8:57                   ` Timo Sirainen
2003-08-28  9:56                     ` David Schwartz
2003-08-28 10:26                       ` Timo Sirainen
2003-08-27 22:58                         ` Nagendra Singh Tomar
2003-08-28 12:18                           ` Jamie Lokier
2003-08-28  0:39                             ` Nagendra Singh Tomar
2003-08-28 13:00                               ` Jamie Lokier
2003-08-28  1:06                                 ` Nagendra Singh Tomar
2003-08-28 21:49                             ` Bernd Eckenfels
2003-08-28 12:01                         ` Jamie Lokier
2003-08-28 13:28                           ` Timo Sirainen
2003-08-28 20:24                         ` David Schwartz
2003-08-28 12:44                       ` Ragnar Hojland Espinosa
2003-08-28 13:03                         ` Jamie Lokier
2003-08-28 17:26                           ` root
2003-08-28 17:35                             ` Jamie Lokier
2003-08-28 18:10                               ` root
2003-08-28 21:59                             ` Bernd Eckenfels
2003-08-28 23:02                               ` Jamie Lokier
2003-08-28 23:44                               ` Lockless file readingu root
2003-08-29 10:00                                 ` jlnance
2003-08-29 11:55                                   ` David Schwartz
2003-08-29 15:43                                   ` William Lee Irwin III
2003-08-28 20:37                         ` Lockless file reading David Schwartz
2003-08-28 22:11                           ` Bernd Eckenfels
2003-08-28 23:00                             ` Jamie Lokier
2003-08-29  0:47                             ` David Schwartz
2003-08-28  9:13                 ` Martin Konold
2003-08-28  9:27                   ` Timo Sirainen
2003-08-28  9:48                     ` Martin Konold
2003-08-28  0:03       ` Jamie Lokier
2003-08-28 12:08         ` Richard B. Johnson
2003-08-28 12:39           ` Jamie Lokier [this message]
2003-08-28 10:08   ` Matthias Andree
2003-08-28 10:54 ` Robin Rosenberg
2003-08-28 12:42   ` Jamie Lokier

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=20030828123958.GC6800@mail.jlokier.co.uk \
    --to=jamie@shareable.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=martin.konold@erfrakon.de \
    --cc=root@chaos.analogic.com \
    --cc=tss@iki.fi \
    /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®