From: Torvald Riegel <triegel@redhat.com>
To: Rich Felker <dalias@aerifal.cx>
Cc: Adhemerval Zanella <azanella@linux.vnet.ibm.com>,
"GNU C. Library" <libc-alpha@sourceware.org>
Subject: Re: [RFC] Propose fix for race conditions in pthread cancellation (bz#12683)
Date: Fri, 12 Sep 2014 16:11:00 -0000 [thread overview]
Message-ID: <1410538290.4967.111.camel@triegel.csb> (raw)
In-Reply-To: <20140912153235.GP23797@brightrain.aerifal.cx>
On Fri, 2014-09-12 at 11:32 -0400, Rich Felker wrote:
> On Fri, Sep 12, 2014 at 04:44:26PM +0200, Torvald Riegel wrote:
> > On Wed, 2014-09-10 at 18:47 -0300, Adhemerval Zanella wrote:
> > "8. Adjust 'lowlevellock.h' arch-specific implementations to provide
> > cancelable futex calls (used in libpthread code)."
> >
> > Only FUTEX_WAIT should be cancellable, I suppose.
> >
> > I've been thinking about whether it would be easier for the
> > implementation of cancellable pthreads functions to just call a
> > noncancellable FUTEX_WAIT and handle EINTR properly (by not retrying the
> > wait but calling pthread_testcancel instead so that any pending
> > cancellation is acted upon). However, this seems to save less
> > complexity than just doing for FUTEX_WAIT what we do for all the other
> > cancellable syscalls, so it's probably not worth it.
>
> This is not viable; it has race conditions, of the exact type which
> the futex syscall and the new cancellation system are designed to
> avoid.
The new scheme would be used, but would always set the cancellation as
pending. So what I thought about would primarily change the way we act
on cancellation, not when it's considered to have happened. There's no
real side effect in waiting, so futex_wait is a special case.
> AFAIK it also only works if the cancellation signal is an interrupting
> signal (no SA_RESTART) which is a big problem -- it would introduce
> illegal EINTR results into programs not expecting to receive them and
> not prepared to handle them.
That sounds right.
> > I'd add another thing to the list of steps, though: For future-proofing,
> > it would be good pthread_setcancelstate and pthread_setcanceltype would
> > have an explicit compiler barrier in them. If at some point we have LTO
> > including those functions, we use C11 atomics, and compilers start
> > optimizing those more aggressively, then I believe we're at risk that
> > the compiler reorders code across the atomics in pthread_setcancelstate;
> > this could lead to cancellation handlers seeing unexpected state when
> > they are actually run. C11 has tighter rules for what's allowed in
> > signal handlers (IIRC, these are still under discussion, at least in the
> > C++ committee) than what POSIX requires, yet the cancellation handler is
> > similar to a signal handler. A compiler barrier should hopefully
> > prevent any issues due to that mismatch. Thoughts?
>
> Since you're only concerned about signal handlers, not access from
> other threads, volatile is probably sufficient here.
Yes, we don't need HW synchronization between the interrupted code and
the cancellation handler. Requiring programmers to use volatile
whenever they need to communicate from interrupted code to cancellation
handlers (on both sides!) could work in practice, but only if the
pthread_setcancelstate etc. look to the compiler like they could call
code that uses volatiles -- which isn't the case for these functions
(assuming the compiler understands and optimizes atomics).
next prev parent reply other threads:[~2014-09-12 16:11 UTC|newest]
Thread overview: 16+ messages / expand[flat|nested] mbox.gz Atom feed top
2014-09-10 21:48 Adhemerval Zanella
2014-09-10 22:00 ` Rich Felker
2014-09-10 22:11 ` Adhemerval Zanella
2014-09-12 14:44 ` Torvald Riegel
2014-09-12 15:32 ` Rich Felker
2014-09-12 16:11 ` Torvald Riegel [this message]
2014-09-12 17:17 ` Rich Felker
2014-09-12 22:44 ` Torvald Riegel
2014-09-13 1:58 ` Rich Felker
2014-09-14 18:00 ` Torvald Riegel
2014-09-15 0:46 ` Rich Felker
2014-09-12 17:33 ` Joseph S. Myers
2014-09-14 18:04 ` Torvald Riegel
2014-09-17 22:13 ` Adhemerval Zanella
2014-09-15 12:49 ` Florian Weimer
2014-09-15 14:39 ` Torvald Riegel
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=1410538290.4967.111.camel@triegel.csb \
--to=triegel@redhat.com \
--cc=azanella@linux.vnet.ibm.com \
--cc=dalias@aerifal.cx \
--cc=libc-alpha@sourceware.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;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).