diff options
author | Adhemerval Zanella <adhemerval.zanella@linaro.org> | 2022-04-06 12:24:42 -0300 |
---|---|---|
committer | Adhemerval Zanella <adhemerval.zanella@linaro.org> | 2022-04-14 12:48:31 -0300 |
commit | 404656009b459658138ed1bd18f3c6cf3863e6a6 (patch) | |
tree | 3cbfb1da2b0f8e657f72a53abf1c5bf85b8d43c3 /nptl/cleanup_defer.c | |
parent | 2376944b9e5c0364b9fb473e4d8dabca31b57167 (diff) | |
download | glibc-404656009b459658138ed1bd18f3c6cf3863e6a6.zip glibc-404656009b459658138ed1bd18f3c6cf3863e6a6.tar.gz glibc-404656009b459658138ed1bd18f3c6cf3863e6a6.tar.bz2 |
nptl: Handle spurious EINTR when thread cancellation is disabled (BZ#29029)
Some Linux interfaces never restart after being interrupted by a signal
handler, regardless of the use of SA_RESTART [1]. It means that for
pthread cancellation, if the target thread disables cancellation with
pthread_setcancelstate and calls such interfaces (like poll or select),
it should not see spurious EINTR failures due the internal SIGCANCEL.
However recent changes made pthread_cancel to always sent the internal
signal, regardless of the target thread cancellation status or type.
To fix it, the previous semantic is restored, where the cancel signal
is only sent if the target thread has cancelation enabled in
asynchronous mode.
The cancel state and cancel type is moved back to cancelhandling
and atomic operation are used to synchronize between threads. The
patch essentially revert the following commits:
8c1c0aae20 nptl: Move cancel type out of cancelhandling
2b51742531 nptl: Move cancel state out of cancelhandling
26cfbb7162 nptl: Remove CANCELING_BITMASK
However I changed the atomic operation to follow the internal C11
semantic and removed the MACRO usage, it simplifies a bit the
resulting code (and removes another usage of the old atomic macros).
Checked on x86_64-linux-gnu, i686-linux-gnu, aarch64-linux-gnu,
and powerpc64-linux-gnu.
[1] https://man7.org/linux/man-pages/man7/signal.7.html
Reviewed-by: Florian Weimer <fweimer@redhat.com>
Tested-by: Aurelien Jarno <aurelien@aurel32.net>
Diffstat (limited to 'nptl/cleanup_defer.c')
-rw-r--r-- | nptl/cleanup_defer.c | 42 |
1 files changed, 36 insertions, 6 deletions
diff --git a/nptl/cleanup_defer.c b/nptl/cleanup_defer.c index f8181a4..eb0bc77 100644 --- a/nptl/cleanup_defer.c +++ b/nptl/cleanup_defer.c @@ -30,9 +30,22 @@ ___pthread_register_cancel_defer (__pthread_unwind_buf_t *buf) ibuf->priv.data.prev = THREAD_GETMEM (self, cleanup_jmp_buf); ibuf->priv.data.cleanup = THREAD_GETMEM (self, cleanup); - /* Disable asynchronous cancellation for now. */ - ibuf->priv.data.canceltype = THREAD_GETMEM (self, canceltype); - THREAD_SETMEM (self, canceltype, PTHREAD_CANCEL_DEFERRED); + int cancelhandling = atomic_load_relaxed (&self->cancelhandling); + if (__glibc_unlikely (cancelhandling & CANCELTYPE_BITMASK)) + { + int newval; + do + { + newval = cancelhandling & ~CANCELTYPE_BITMASK; + } + while (!atomic_compare_exchange_weak_acquire (&self->cancelhandling, + &cancelhandling, + newval)); + } + + ibuf->priv.data.canceltype = (cancelhandling & CANCELTYPE_BITMASK + ? PTHREAD_CANCEL_ASYNCHRONOUS + : PTHREAD_CANCEL_DEFERRED); /* Store the new cleanup handler info. */ THREAD_SETMEM (self, cleanup_jmp_buf, (struct pthread_unwind_buf *) buf); @@ -54,9 +67,26 @@ ___pthread_unregister_cancel_restore (__pthread_unwind_buf_t *buf) THREAD_SETMEM (self, cleanup_jmp_buf, ibuf->priv.data.prev); - THREAD_SETMEM (self, canceltype, ibuf->priv.data.canceltype); - if (ibuf->priv.data.canceltype == PTHREAD_CANCEL_ASYNCHRONOUS) - __pthread_testcancel (); + if (ibuf->priv.data.canceltype == PTHREAD_CANCEL_DEFERRED) + return; + + int cancelhandling = atomic_load_relaxed (&self->cancelhandling); + if (cancelhandling & CANCELTYPE_BITMASK) + { + int newval; + do + { + newval = cancelhandling | CANCELTYPE_BITMASK; + } + while (!atomic_compare_exchange_weak_acquire (&self->cancelhandling, + &cancelhandling, newval)); + + if (cancel_enabled_and_canceled (cancelhandling)) + { + self->result = PTHREAD_CANCELED; + __do_cancel (); + } + } } versioned_symbol (libc, ___pthread_unregister_cancel_restore, __pthread_unregister_cancel_restore, GLIBC_2_34); |