All of lore.kernel.org
 help / color / mirror / Atom feed
From: Andrew Morton <akpm@osdl.org>
To: Dan Bonachea <bonachead@comcast.net>
Cc: linux-kernel@vger.kernel.org
Subject: Re: PROBLEM: pthread-safety bug in write(2) on Linux 2.6.x
Date: Wed, 12 Apr 2006 21:46:13 -0700	[thread overview]
Message-ID: <20060412214613.404cf49f.akpm@osdl.org> (raw)
In-Reply-To: <6.2.5.6.2.20060412173852.033dbb90@cs.berkeley.edu>

Dan Bonachea <bonachead@comcast.net> wrote:
>
> Hi - I believe I've discovered a thread-safety bug in the Linux 2.6.x kernel 
>  implementation of write(2).
> 
>  The small C program below the problem - in a nutshell, if multiple threads 
>  write() to STDOUT_FILENO, and stdout has been redirected to a file, then some 
>  of the output lines get lost. The actual result is non-deterministic (even in 
>  a "correct" run) - however the expected correct behavior is 10 lines of output 
>  (in some non-deterministic order). However, the test program reproducibly 
>  generates some lost output (less than 10 lines of total output) on every run 
>  where the output is redirected to a new file. This appears to be a violation 
>  of the POSIX spec (POSIX 1003.1-2001:2.9.1 requires thread-safety of write()). 
> 
> 
>  The problem does not appear to occur if output goes to the console, or is 
>  redirected to append to an existing file, only when stdout is redirected to a 
>  new file.

This comes up occasionally.  Is very simple:

asmlinkage ssize_t sys_write(unsigned int fd, const char __user * buf, size_t count)
{
	struct file *file;
	ssize_t ret = -EBADF;
	int fput_needed;

	file = fget_light(fd, &fput_needed);
	if (file) {
		loff_t pos = file_pos_read(file);
		ret = vfs_write(file, buf, count, &pos);
		file_pos_write(file, pos);
		fput_light(file, fput_needed);
	}

	return ret;
}

we can have multiple threads in vfs_write() using the same `pos'.

Locking for file.f_pos is generally file->f_dentry->d_inode->i_mutex.  We
could use that if we were to restructure the code a lot.  Or we could add a
new lock to `struct file'.

Or we could do nothing, because a) the application is going to produce
inderterminate output anyway and b) because it only affects silly testcases
and not real-world apps.

OK, there _might_ be a real-world case: threads appending logging
information to a flat file.  Trivially workable-around with a userspace
lock, or by switching to stdio (same thing).

Yes, really we should fix it.  But it's not worth adding more overhead to
do so.  So the fix would involve widespread (but simple) change, to draw
that f_pos update inside i_mutex.

(We could pseudo-fix it by updating f_pos _before_ entering vfs_write(), too).

  parent reply	other threads:[~2006-04-13  4:46 UTC|newest]

Thread overview: 24+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2006-04-13  1:45 PROBLEM: pthread-safety bug in write(2) on Linux 2.6.x Dan Bonachea
2006-04-13  2:10 ` Alistair John Strachan
2006-04-13  4:46 ` Andrew Morton [this message]
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
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
  -- strict thread matches above, loose matches on Subject: below --
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=20060412214613.404cf49f.akpm@osdl.org \
    --to=akpm@osdl.org \
    --cc=bonachead@comcast.net \
    --cc=linux-kernel@vger.kernel.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.