public inbox for libc-alpha@sourceware.org
 help / color / mirror / Atom feed
From: Rich Felker <dalias@libc.org>
To: Szabolcs Nagy <szabolcs.nagy@arm.com>
Cc: Florian Weimer <fweimer@redhat.com>, libc-alpha@sourceware.org
Subject: Re: [PATCH 2/2] manual: Document __libc_single_threaded
Date: Fri, 22 May 2020 11:07:20 -0400	[thread overview]
Message-ID: <20200522150720.GR1079@brightrain.aerifal.cx> (raw)
In-Reply-To: <20200522105458.GB29518@arm.com>

On Fri, May 22, 2020 at 11:54:58AM +0100, Szabolcs Nagy wrote:
> The 05/22/2020 12:05, Florian Weimer wrote:
> > * Szabolcs Nagy:
> > 
> > > The 05/21/2020 15:44, Florian Weimer wrote:
> > >> * Szabolcs Nagy:
> > >> > what's wrong with pthread_join updating it?
> > >> 
> > >> It's tricky do it correctly if there are two remaining threads, one of
> > >> them the one being joined, the other one a detached thread.  A
> > >> straightforward implementation merely looking at __nptl_nthreads before
> > >> returning from pthread_join would not perform the required
> > >> synchronization on the detached thread exit.
> > >
> > > i'm trying to understand this, but don't see
> > > what's wrong if the last thread is detached.
> > 
> > Sorry, I meant three reamining threads in total, i.e., two more threads
> > in addition to the one thread that keeps going after the other two
> > exited, and may use __libc_single_threaded in the future.
> > 
> > Clearer now?
> 
> hm so a detached thread is concurrently exiting with
> a pthread_join which sees a decremented __nptl_nthreads
> but the detached thread has not actually exited yet.

In principle this is no big deal as long as the exiting thread cannot
make any further actions where its existence causes an observable
effect on users of __libc_single_threaded. (For this purpose, I think
you actually need to define what uses are valid, though; see setxid
remarks below.) If it makes a problem for pthread_join that's an
implementation detail that should be fixable. The bigger issue is
memory synchronization.

> i think glibc can issue a memory barrier syscall before
> decrementing __nptl_nthreads in a detached thread, this
> means if pthread_join observes __nptl_nthreads==1
> then user memory accesses in the detached thread are
> synchronized with non-atomic memory accesses after
> pthread_join returns. (i.e. __nptl_nthreads==1 should
> mean at all times that as far as user code is concerned
> the process is single threaded even if some detached
> thread is still hanging around)

This still has consequences for setxid safety which is why musl now
fully synchronizes the existing threads list. But if you're not using
the thread count for that, it's not an issue. Indeed I think
SYS_membarrier is a solution here, but if it's not supported or
blocked by seccomp then __libc_single_threaded must not be made true
again at this time.

> i think __libc_single_threaded should be possible to
> update in pthread_join with the above change, in
> which case we need not document that it stays false
> forever, so we can change this in the future.
> (unless somebody finds usecases where a false->true
> transition would cause problems)

pthread_join is not unique here. In principle __libc_single_threaded
can be made true again any time a non-AS-safe function observes a
thread count of 1 after performing a memory barrier. There may be
other internal-locking situations where it's beneficial to update it
so that future locks can be elided.

Rich

  parent reply	other threads:[~2020-05-22 15:07 UTC|newest]

Thread overview: 45+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-05-20 18:12 [PATCH 0/2] Add __libc_single_threaded Florian Weimer
2020-05-20 18:12 ` [PATCH 1/2] Add the __libc_single_threaded variable Florian Weimer
2020-05-21 13:07   ` Szabolcs Nagy
2020-05-21 13:16     ` Florian Weimer
2020-05-21 13:26       ` Szabolcs Nagy
2020-05-20 18:12 ` [PATCH 2/2] manual: Document __libc_single_threaded Florian Weimer
2020-05-21  7:52   ` Michael Kerrisk (man-pages)
2020-05-21 12:17     ` Florian Weimer
2020-05-21 11:18   ` Szabolcs Nagy
2020-05-21 12:16     ` Florian Weimer
2020-05-21 12:50   ` Adhemerval Zanella
2020-05-21 13:09     ` Szabolcs Nagy
2020-05-21 13:15       ` Adhemerval Zanella
2020-05-21 13:30         ` Szabolcs Nagy
2020-05-21 13:44           ` Florian Weimer
2020-05-21 13:58             ` Adhemerval Zanella
2020-05-21 14:03               ` Florian Weimer
2020-05-22 10:01             ` Szabolcs Nagy
2020-05-22 10:05               ` Florian Weimer
2020-05-22 10:54                 ` Szabolcs Nagy
2020-05-22 11:08                   ` Florian Weimer
2020-05-22 15:07                   ` Rich Felker [this message]
2020-05-22 16:14                     ` Rich Felker
2020-05-22 16:36                       ` Adhemerval Zanella
2020-05-22 17:02                       ` Florian Weimer
2020-05-22 17:18                         ` Florian Weimer
2020-05-22 17:28                         ` Rich Felker
2020-05-22 17:40                           ` Florian Weimer
2020-05-22 17:49                             ` Rich Felker
2020-05-22 19:22                               ` Szabolcs Nagy
2020-05-22 19:53                                 ` Rich Felker
2020-05-23  6:49                                   ` Szabolcs Nagy
2020-05-23 16:02                                     ` Rich Felker
2020-05-25  8:08                                       ` Florian Weimer
2020-05-25 17:21                                         ` Rich Felker
2020-05-27 11:54                                           ` Florian Weimer
2020-05-27 15:36                                             ` Rich Felker
2020-06-03 15:00                                               ` Florian Weimer
2020-06-03 17:11                                                 ` Rich Felker
2020-05-25  8:08                                       ` Florian Weimer
2020-05-21 13:56           ` Adhemerval Zanella
2020-05-21 13:14     ` Florian Weimer
2020-05-21 14:32       ` Adhemerval Zanella
2020-06-03 15:48         ` Florian Weimer
2020-06-03 17:52           ` Adhemerval Zanella

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=20200522150720.GR1079@brightrain.aerifal.cx \
    --to=dalias@libc.org \
    --cc=fweimer@redhat.com \
    --cc=libc-alpha@sourceware.org \
    --cc=szabolcs.nagy@arm.com \
    /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).