mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Dan Bonachea <bonachead@comcast.net>
To: Alan Cox <alan@lxorguk.ukuu.org.uk>, Linus Torvalds <torvalds@osdl.org>
Cc: Nick Piggin <nickpiggin@yahoo.com.au>,
	Andrew Morton <akpm@osdl.org>,
	linux-kernel@vger.kernel.org
Subject: Re: PROBLEM: pthread-safety bug in write(2) on Linux 2.6.x
Date: Thu, 13 Apr 2006 15:06:03 -0700	[thread overview]
Message-ID: <6.2.5.6.2.20060413145913.03436f38@comcast.net> (raw)
In-Reply-To: <1144965022.12387.23.camel@localhost.localdomain>

At 02:50 PM 4/13/2006, Alan Cox wrote:
>The only serious case historically has been O_APPEND which does have
>pretty precise semantics. Nowdays we also have pread/pwrite which have
>pretty clear semantics and deal with threading. The O_APPEND case is
>very important to get correct and 2.4 certainly did so.
...
>As such I belive that the O_APPEND case must be kept locked properly and
>the non O_APPEND cases are already correctly handled by the kernel. That
>seems to argue for f_pos serialization on O_APPEND only.

I agree that O_APPEND is important to get correct. However, would that also 
handle the specific case of stdout redirection to a file? (ie "a.out > 
result", not just "a.out >> result" - the latter incidentally does not 
currently seem to fail, at least with my tests)

>Looking at the spec it says the following
>
>"If the O_APPEND flag of the file status flags is set, the file offset
>shall be set to the end of the file prior to each write and no
>intervening file modification operation shall occur between changing the
>file offset and the write operation."
>
>This is what 2.4 took great paints to guarantee for file writes. Now in
>actual fact no OS I know of implements this to the extreme (mmap) but
>does for the other cases.
>
>Outside of O_APPEND the specification says only that
>- The write starts at the file position
>- The file position is updated before the syscall returns
>
>It makes no other guarantee I can see.

The POSIX 1003.1-2001 spec seems to provide a very clear guarantee of 
thread-safety behavior wrt threads:

2.9.1 Thread-Safety
All functions defined by this volume of IEEE Std 1003.1-2001 shall be 
thread-safe, except that the following functions need not be thread-safe.
    <omitted list of functions, which does not include write>
Implementations shall provide internal synchronization as necessary in order 
to satisfy this
requirement.
...
2.9.7 Thread Interactions with Regular File Operations
All of the functions chmod( ), close( ), fchmod( ), fcntl( ), fstat( ), 
ftruncate( ), lseek( ), open( ), read( ), readlink( ), stat( ), symlink( ), 
and write( ) shall be atomic with respect to each other in the effects 
specified in IEEE Std 1003.1-2001 when they operate on regular files. If two 
threads each call one of these functions, each call shall either see all of 
the specified effects of the other call, or none of them.

Unless I'm missing something, that doesn't leave much ambiguity regarding 
what's required for POSIX compliance on this issue (although I'm not sure 
POSIX compliance is the right metric).

Dan


  reply	other threads:[~2006-04-13 22:47 UTC|newest]

Thread overview: 24+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2006-04-13  1:45 Dan Bonachea
2006-04-13  2:10 ` Alistair John Strachan
2006-04-13  4:46 ` Andrew Morton
2006-04-13  5:33   ` Nick Piggin
2006-04-13 15:01     ` Linus Torvalds
2006-04-13 15:28       ` Linus Torvalds
2006-04-14 10:20         ` Nikita Danilov
2006-04-13 21:50       ` Alan Cox
2006-04-13 22:06         ` Dan Bonachea [this message]
2006-04-13 23:03           ` Linus Torvalds
2006-04-13 23:11           ` Alan Cox
2006-04-13 23:19             ` Linus Torvalds
2006-04-13 22:40         ` Linus Torvalds
2006-04-13 23:05           ` Alan Cox
2006-04-13 23:06             ` Linus Torvalds
2006-04-13 23:11               ` Linus Torvalds
2006-04-13  9:18   ` Dan Bonachea
2006-04-13  9:56     ` Andrew Morton
2006-04-13 10:28       ` Kyle Moffett
2006-04-13 14:14     ` Jan Engelhardt
2010-12-19 22:45 ` Jens Moser
     [not found] <60Z8f-4QA-25@gated-at.bofh.it>
     [not found] ` <611Wl-u5-1@gated-at.bofh.it>
     [not found]   ` <616k2-6Xz-27@gated-at.bofh.it>
2006-04-14  9:14     ` Kai Henningsen
2006-04-27  9:06 Samuel Thibault
2006-04-27 15:23 ` Linus Torvalds

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=6.2.5.6.2.20060413145913.03436f38@comcast.net \
    --to=bonachead@comcast.net \
    --cc=akpm@osdl.org \
    --cc=alan@lxorguk.ukuu.org.uk \
    --cc=linux-kernel@vger.kernel.org \
    --cc=nickpiggin@yahoo.com.au \
    --cc=torvalds@osdl.org \
    /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®