1
0
mirror of https://sourceware.org/git/glibc.git synced 2025-08-07 06:43:00 +03:00

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>
This commit is contained in:
Adhemerval Zanella
2022-04-06 12:24:42 -03:00
parent 2376944b9e
commit 404656009b
15 changed files with 481 additions and 90 deletions

View File

@@ -26,9 +26,24 @@ __libc_cleanup_push_defer (struct _pthread_cleanup_buffer *buffer)
buffer->__prev = THREAD_GETMEM (self, cleanup);
int cancelhandling = atomic_load_relaxed (&self->cancelhandling);
/* Disable asynchronous cancellation for now. */
buffer->__canceltype = THREAD_GETMEM (self, canceltype);
THREAD_SETMEM (self, canceltype, PTHREAD_CANCEL_DEFERRED);
if (__glibc_unlikely (cancelhandling & CANCELTYPE_BITMASK))
{
int newval;
do
{
newval = cancelhandling & ~CANCELTYPE_BITMASK;
}
while (!atomic_compare_exchange_weak_acquire (&self->cancelhandling,
&cancelhandling,
newval));
}
buffer->__canceltype = (cancelhandling & CANCELTYPE_BITMASK
? PTHREAD_CANCEL_ASYNCHRONOUS
: PTHREAD_CANCEL_DEFERRED);
THREAD_SETMEM (self, cleanup, buffer);
}
@@ -41,8 +56,22 @@ __libc_cleanup_pop_restore (struct _pthread_cleanup_buffer *buffer)
THREAD_SETMEM (self, cleanup, buffer->__prev);
THREAD_SETMEM (self, canceltype, buffer->__canceltype);
if (buffer->__canceltype == PTHREAD_CANCEL_ASYNCHRONOUS)
__pthread_testcancel ();
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 ();
}
}
}
libc_hidden_def (__libc_cleanup_pop_restore)