public inbox for libc-alpha@sourceware.org
 help / color / mirror / Atom feed
From: Rich Felker <dalias@libc.org>
To: Florian Weimer <fweimer@redhat.com>
Cc: libc-alpha@sourceware.org
Subject: Re: [PATCH 2/2] manual: Document __libc_single_threaded
Date: Wed, 27 May 2020 11:36:10 -0400	[thread overview]
Message-ID: <20200527153609.GT1079@brightrain.aerifal.cx> (raw)
In-Reply-To: <87mu5tk2zq.fsf@oldenburg2.str.redhat.com>

On Wed, May 27, 2020 at 01:54:01PM +0200, Florian Weimer wrote:
> * Rich Felker:
> 
> > On Mon, May 25, 2020 at 10:08:37AM +0200, Florian Weimer via Libc-alpha wrote:
> >> * Rich Felker:
> >> 
> >> >> this can allow earlier single threaded detection than only
> >> >> considering pthread_join: e.g. stdio, malloc etc may do a
> >> >> check and update the global after an acquire barrier, however
> >> >> the compiler must not cache globals across libc calls for this
> >> >> to work.
> >> >
> >> > It can't cache globals across non-pure functions whose definitions it
> >> > cant't see (and if it saw the definition it would know the global is
> >> > modified).
> >> 
> >> Sorry about that, hit C-c C-c while I thought I was in a terminal. 8-/
> >> 
> >> For most standard C functions, it is well-known to which global
> >> variables (if any) they write.  Of course, compilers exploit this fact.
> >> 
> >> > malloc is something of a special case where clang treats it
> >> > not as a function but having "pure malloc semantics", but even then I
> >> > don't think it matters if it caches it;
> >> 
> >> And of course malloc is the most common example of a standard function
> >> that has observable side effects beyond those specified in the standard:
> >> most implementations have a statistics interface.
> >> 
> >> > at worst you see the old value of __libc_single_threaded (false)
> >> > rather than the new one (true) and that direction is safe.
> >> 
> >> It's still a data race.  The compiler can easily generate invalid code
> >> if it incorrectly assumes that __libc_single_threaded remains stable.  I
> >> don't know if Clang will do this.  But I think the C library
> >> implementation should be rather conservative here.
> >
> > If this is an issue, and even regardless of whether it is, I think the
> > type of __libc_single_threaded should be volatile qualified. This
> > ensures that it cannot be cached in any way that might be invalid.
> > That's not just a hack; it's the correct way to model that the value
> > is able to change asynchronously (such as by an operation that the
> > compiler would otherwise assume can't have side effects).
> 
> I think it makes more sense not to declare the object as volatile and
> make sure that only libc functions which imply the required barrier
> write to __libc_single_threaded.  For instance, I expect that this will
> allow compilers to generate tighter code around multiple (implied)
> reference count updates.

It really should be volatile just because whatever you make it is ABI,
and there might be good reasons to want to make it updated
"asynchronously" with respect to the compiler's model in the future.
If you don't make it volatile now you can't do that in the future.

The cost of volatile is rather trivial; compiler can still cache
address of it, and loading from a cache line you recently loaded from,
and to which no stores are happening, is virtually free.

Rich

  reply	other threads:[~2020-05-27 15:36 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
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 [this message]
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=20200527153609.GT1079@brightrain.aerifal.cx \
    --to=dalias@libc.org \
    --cc=fweimer@redhat.com \
    --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).