From: Florian Weimer <fweimer@redhat.com>
To: Adhemerval Zanella <adhemerval.zanella@linaro.org>
Cc: libc-alpha@sourceware.org
Subject: Re: [PATCH 1/3] linux: Move flockfile/_IO_flockfile into libc
Date: Thu, 06 May 2021 22:34:41 +0200 [thread overview]
Message-ID: <87k0obd2ji.fsf@oldenburg.str.redhat.com> (raw)
In-Reply-To: <20210506200614.2928007-1-adhemerval.zanella@linaro.org> (Adhemerval Zanella's message of "Thu, 6 May 2021 17:06:12 -0300")
* Adhemerval Zanella:
> diff --git a/stdio-common/flockfile.c b/stdio-common/flockfile.c
> index 0bc856a465..c838dcd538 100644
> --- a/stdio-common/flockfile.c
> +++ b/stdio-common/flockfile.c
> @@ -17,13 +17,18 @@
> <https://www.gnu.org/licenses/>. */
>
> #include <stdio.h>
> +#include <stdio-lock.h>
> +#include <sys/single_threaded.h>
>
> void
> __flockfile (FILE *stream)
> {
> + bool multithread = __libc_single_threaded == 0;
> + if (multithread)
> + {
> + stream->_flags2 |= _IO_FLAGS2_NEED_LOCK;
> + _IO_lock_lock (*stream->_lock);
> + }
> }
I'm afraid this is not correct because a thread can become multithreaded
between the flockfile/funlockfile calls. As a result, the lock state
would be wrong at funlockfile time.
In order to optimize locks in such cases, it is necessary to perform the
same actions as the locking/unlocking operations, but without atomic
instructions.
The stream->_flags2 update has a data race with other _flags2 updates
(e.g., in __malloc_stats), but that's a preexisting bug that should be
fixed separately (there might be other affected places).
Thanks,
Florian
next prev parent reply other threads:[~2021-05-06 20:34 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-05-06 20:06 Adhemerval Zanella
2021-05-06 20:06 ` [PATCH 2/3] linux: Move ftrylockfile/_IO_ftrylockfile " Adhemerval Zanella
2021-05-06 20:06 ` [PATCH 3/3] linux: Move funlockfile/_IO_funlockfile " Adhemerval Zanella
2021-05-06 20:34 ` Florian Weimer [this message]
2021-05-06 21:01 ` [PATCH 1/3] linux: Move flockfile/_IO_flockfile " Adhemerval Zanella
2021-05-07 6:37 ` Florian Weimer
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=87k0obd2ji.fsf@oldenburg.str.redhat.com \
--to=fweimer@redhat.com \
--cc=adhemerval.zanella@linaro.org \
--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).