mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Jamie Lokier <jamie@shareable.org>
To: Timo Sirainen <tss@iki.fi>
Cc: David Schwartz <davids@webmaster.com>, linux-kernel@vger.kernel.org
Subject: Re: Lockless file reading
Date: Thu, 28 Aug 2003 13:01:35 +0100	[thread overview]
Message-ID: <20030828120135.GA6800@mail.jlokier.co.uk> (raw)
In-Reply-To: <1062066411.1451.319.camel@hurina>

Timo Sirainen wrote:
> Reorder on per-byte basis? Per-page/block would still be acceptable.

The _CPUs_ can reorder on a per-byte basis, on a multiprocessor.  It
has nothing to do with the kernel.

> Anyway, the alternative would be shared mmap()ed file. You can trust
> 32bit memory updates to be atomic, right?

Atomic yes (if aligned), weakly ordered though.

> Or what about: write("12"), fsync(), write("12")? Is it still possible
> for read() to return "1x1x"?

Yes it is possible, in principle.

This is what happens: the writing CPU writes "1", "2" in order.  The
reading CPU reads bytes 1, 3 before the writes are
observed and bytes 0, 2 after.  The CPU can do this.

The kernel doesn't prevent this, because it doesn't hold any exclusive
lock between the writer and reader during the data transfers.
Furthermore the kernel transfers a byte at a time, on some
architecture (including x86), if any buffer is not aligned.

It is very unlikely to return "1x1x", but you should know it is not
architecturally impossible.  Given your incomplete knowledge of every
architectural quirk, it is more likely to occur than an MD5 collision.

On 32-bit aligned atomicity: if the block of 4 bytes is aligned in
memory, then with shared mmap you will only see whole words
transferred because all (current) Linux SMP-capable architectures
offer 32 bit atomicity.  It is not a very nice assumption: it doesn't
hold for 16 bits or 64 bits, and may not hold for a future 64 bit
architecture.  Keep in mind the words stay whole, but multiple words
are read out of order.

With read() and write(), even aligned 32-bit words don't work.  On
some 64-bit architectures, a 32-bit word read() will be issued as 4
byte reads at the machine level, with weak memory ordering.  (I'm
reading Alpha and IA64 __copy_user right now, and they both do that).

Enjoy :)
-- Jamie

  parent reply	other threads:[~2003-08-28 12:01 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 [this message]
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
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=20030828120135.GA6800@mail.jlokier.co.uk \
    --to=jamie@shareable.org \
    --cc=davids@webmaster.com \
    --cc=linux-kernel@vger.kernel.org \
    --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®