08c3a6
commit 290db09546b260a30137d03ce97a857e6f15b648
08c3a6
Author: Adhemerval Zanella <adhemerval.zanella@linaro.org>
08c3a6
Date:   Wed Apr 6 12:24:42 2022 -0300
08c3a6
08c3a6
    nptl: Handle spurious EINTR when thread cancellation is disabled (BZ#29029)
08c3a6
    
08c3a6
    Some Linux interfaces never restart after being interrupted by a signal
08c3a6
    handler, regardless of the use of SA_RESTART [1].  It means that for
08c3a6
    pthread cancellation, if the target thread disables cancellation with
08c3a6
    pthread_setcancelstate and calls such interfaces (like poll or select),
08c3a6
    it should not see spurious EINTR failures due the internal SIGCANCEL.
08c3a6
    
08c3a6
    However recent changes made pthread_cancel to always sent the internal
08c3a6
    signal, regardless of the target thread cancellation status or type.
08c3a6
    To fix it, the previous semantic is restored, where the cancel signal
08c3a6
    is only sent if the target thread has cancelation enabled in
08c3a6
    asynchronous mode.
08c3a6
    
08c3a6
    The cancel state and cancel type is moved back to cancelhandling
08c3a6
    and atomic operation are used to synchronize between threads.  The
08c3a6
    patch essentially revert the following commits:
08c3a6
    
08c3a6
      8c1c0aae20 nptl: Move cancel type out of cancelhandling
08c3a6
      2b51742531 nptl: Move cancel state out of cancelhandling
08c3a6
      26cfbb7162 nptl: Remove CANCELING_BITMASK
08c3a6
    
08c3a6
    However I changed the atomic operation to follow the internal C11
08c3a6
    semantic and removed the MACRO usage, it simplifies a bit the
08c3a6
    resulting code (and removes another usage of the old atomic macros).
08c3a6
    
08c3a6
    Checked on x86_64-linux-gnu, i686-linux-gnu, aarch64-linux-gnu,
08c3a6
    and powerpc64-linux-gnu.
08c3a6
    
08c3a6
    [1] https://man7.org/linux/man-pages/man7/signal.7.html
08c3a6
    
08c3a6
    Reviewed-by: Florian Weimer <fweimer@redhat.com>
08c3a6
    Tested-by: Aurelien Jarno <aurelien@aurel32.net>
08c3a6
    
08c3a6
    (cherry-picked from commit 404656009b459658138ed1bd18f3c6cf3863e6a6)
08c3a6
08c3a6
diff --git a/manual/process.texi b/manual/process.texi
08c3a6
index 28c9531f4294f56e..9307379194c6f666 100644
08c3a6
--- a/manual/process.texi
08c3a6
+++ b/manual/process.texi
08c3a6
@@ -68,8 +68,7 @@ until the subprogram terminates before you can do anything else.
08c3a6
 @c   CLEANUP_HANDLER @ascuplugin @ascuheap @acsmem
08c3a6
 @c    libc_cleanup_region_start @ascuplugin @ascuheap @acsmem
08c3a6
 @c     pthread_cleanup_push_defer @ascuplugin @ascuheap @acsmem
08c3a6
-@c      __pthread_testcancel @ascuplugin @ascuheap @acsmem
08c3a6
-@c       CANCEL_ENABLED_AND_CANCELED ok
08c3a6
+@c      cancel_enabled_and_canceled @ascuplugin @ascuheap @acsmem
08c3a6
 @c       do_cancel @ascuplugin @ascuheap @acsmem
08c3a6
 @c    cancel_handler ok
08c3a6
 @c     kill syscall ok
08c3a6
diff --git a/nptl/allocatestack.c b/nptl/allocatestack.c
08c3a6
index 554a721f814b53c4..96101753ec2f4323 100644
08c3a6
--- a/nptl/allocatestack.c
08c3a6
+++ b/nptl/allocatestack.c
08c3a6
@@ -120,8 +120,6 @@ get_cached_stack (size_t *sizep, void **memp)
08c3a6
 
08c3a6
   /* Cancellation handling is back to the default.  */
08c3a6
   result->cancelhandling = 0;
08c3a6
-  result->cancelstate = PTHREAD_CANCEL_ENABLE;
08c3a6
-  result->canceltype = PTHREAD_CANCEL_DEFERRED;
08c3a6
   result->cleanup = NULL;
08c3a6
   result->setup_failed = 0;
08c3a6
 
08c3a6
diff --git a/nptl/cancellation.c b/nptl/cancellation.c
08c3a6
index 05962784d51fb98b..e97d56f97d7a5698 100644
08c3a6
--- a/nptl/cancellation.c
08c3a6
+++ b/nptl/cancellation.c
08c3a6
@@ -31,19 +31,26 @@ int
08c3a6
 __pthread_enable_asynccancel (void)
08c3a6
 {
08c3a6
   struct pthread *self = THREAD_SELF;
08c3a6
+  int oldval = atomic_load_relaxed (&self->cancelhandling);
08c3a6
 
08c3a6
-  int oldval = THREAD_GETMEM (self, canceltype);
08c3a6
-  THREAD_SETMEM (self, canceltype, PTHREAD_CANCEL_ASYNCHRONOUS);
08c3a6
+  while (1)
08c3a6
+    {
08c3a6
+      int newval = oldval | CANCELTYPE_BITMASK;
08c3a6
 
08c3a6
-  int ch = THREAD_GETMEM (self, cancelhandling);
08c3a6
+      if (newval == oldval)
08c3a6
+	break;
08c3a6
 
08c3a6
-  if (self->cancelstate == PTHREAD_CANCEL_ENABLE
08c3a6
-      && (ch & CANCELED_BITMASK)
08c3a6
-      && !(ch & EXITING_BITMASK)
08c3a6
-      && !(ch & TERMINATED_BITMASK))
08c3a6
-    {
08c3a6
-      THREAD_SETMEM (self, result, PTHREAD_CANCELED);
08c3a6
-      __do_cancel ();
08c3a6
+      if (atomic_compare_exchange_weak_acquire (&self->cancelhandling,
08c3a6
+						&oldval, newval))
08c3a6
+	{
08c3a6
+	  if (cancel_enabled_and_canceled_and_async (newval))
08c3a6
+	    {
08c3a6
+	      self->result = PTHREAD_CANCELED;
08c3a6
+	      __do_cancel ();
08c3a6
+	    }
08c3a6
+
08c3a6
+	  break;
08c3a6
+	}
08c3a6
     }
08c3a6
 
08c3a6
   return oldval;
08c3a6
@@ -57,10 +64,29 @@ __pthread_disable_asynccancel (int oldtype)
08c3a6
 {
08c3a6
   /* If asynchronous cancellation was enabled before we do not have
08c3a6
      anything to do.  */
08c3a6
-  if (oldtype == PTHREAD_CANCEL_ASYNCHRONOUS)
08c3a6
+  if (oldtype & CANCELTYPE_BITMASK)
08c3a6
     return;
08c3a6
 
08c3a6
   struct pthread *self = THREAD_SELF;
08c3a6
-  self->canceltype = PTHREAD_CANCEL_DEFERRED;
08c3a6
+  int newval;
08c3a6
+  int oldval = atomic_load_relaxed (&self->cancelhandling);
08c3a6
+  do
08c3a6
+    {
08c3a6
+      newval = oldval & ~CANCELTYPE_BITMASK;
08c3a6
+    }
08c3a6
+  while (!atomic_compare_exchange_weak_acquire (&self->cancelhandling,
08c3a6
+						&oldval, newval));
08c3a6
+
08c3a6
+  /* We cannot return when we are being canceled.  Upon return the
08c3a6
+     thread might be things which would have to be undone.  The
08c3a6
+     following loop should loop until the cancellation signal is
08c3a6
+     delivered.  */
08c3a6
+  while (__glibc_unlikely ((newval & (CANCELING_BITMASK | CANCELED_BITMASK))
08c3a6
+			   == CANCELING_BITMASK))
08c3a6
+    {
08c3a6
+      futex_wait_simple ((unsigned int *) &self->cancelhandling, newval,
08c3a6
+			 FUTEX_PRIVATE);
08c3a6
+      newval = atomic_load_relaxed (&self->cancelhandling);
08c3a6
+    }
08c3a6
 }
08c3a6
 libc_hidden_def (__pthread_disable_asynccancel)
08c3a6
diff --git a/nptl/cleanup_defer.c b/nptl/cleanup_defer.c
08c3a6
index 7e858d0df068276b..35ba40fb0247c7cc 100644
08c3a6
--- a/nptl/cleanup_defer.c
08c3a6
+++ b/nptl/cleanup_defer.c
08c3a6
@@ -31,9 +31,22 @@ ___pthread_register_cancel_defer (__pthread_unwind_buf_t *buf)
08c3a6
   ibuf->priv.data.prev = THREAD_GETMEM (self, cleanup_jmp_buf);
08c3a6
   ibuf->priv.data.cleanup = THREAD_GETMEM (self, cleanup);
08c3a6
 
08c3a6
-  /* Disable asynchronous cancellation for now.  */
08c3a6
-  ibuf->priv.data.canceltype = THREAD_GETMEM (self, canceltype);
08c3a6
-  THREAD_SETMEM (self, canceltype, PTHREAD_CANCEL_DEFERRED);
08c3a6
+  int cancelhandling = atomic_load_relaxed (&self->cancelhandling);
08c3a6
+  if (__glibc_unlikely (cancelhandling & CANCELTYPE_BITMASK))
08c3a6
+    {
08c3a6
+      int newval;
08c3a6
+      do
08c3a6
+	{
08c3a6
+	  newval = cancelhandling & ~CANCELTYPE_BITMASK;
08c3a6
+	}
08c3a6
+      while (!atomic_compare_exchange_weak_acquire (&self->cancelhandling,
08c3a6
+						    &cancelhandling,
08c3a6
+						    newval));
08c3a6
+    }
08c3a6
+
08c3a6
+  ibuf->priv.data.canceltype = (cancelhandling & CANCELTYPE_BITMASK
08c3a6
+				? PTHREAD_CANCEL_ASYNCHRONOUS
08c3a6
+				: PTHREAD_CANCEL_DEFERRED);
08c3a6
 
08c3a6
   /* Store the new cleanup handler info.  */
08c3a6
   THREAD_SETMEM (self, cleanup_jmp_buf, (struct pthread_unwind_buf *) buf);
08c3a6
@@ -55,9 +68,26 @@ ___pthread_unregister_cancel_restore (__pthread_unwind_buf_t *buf)
08c3a6
 
08c3a6
   THREAD_SETMEM (self, cleanup_jmp_buf, ibuf->priv.data.prev);
08c3a6
 
08c3a6
-  THREAD_SETMEM (self, canceltype, ibuf->priv.data.canceltype);
08c3a6
-  if (ibuf->priv.data.canceltype == PTHREAD_CANCEL_ASYNCHRONOUS)
08c3a6
-    __pthread_testcancel ();
08c3a6
+  if (ibuf->priv.data.canceltype == PTHREAD_CANCEL_DEFERRED)
08c3a6
+    return;
08c3a6
+
08c3a6
+  int cancelhandling = atomic_load_relaxed (&self->cancelhandling);
08c3a6
+  if (cancelhandling & CANCELTYPE_BITMASK)
08c3a6
+    {
08c3a6
+      int newval;
08c3a6
+      do
08c3a6
+	{
08c3a6
+	  newval = cancelhandling | CANCELTYPE_BITMASK;
08c3a6
+	}
08c3a6
+      while (!atomic_compare_exchange_weak_acquire (&self->cancelhandling,
08c3a6
+						    &cancelhandling, newval));
08c3a6
+
08c3a6
+      if (cancel_enabled_and_canceled (cancelhandling))
08c3a6
+	{
08c3a6
+	  self->result = PTHREAD_CANCELED;
08c3a6
+	  __do_cancel ();
08c3a6
+	}
08c3a6
+    }
08c3a6
 }
08c3a6
 versioned_symbol (libc, ___pthread_unregister_cancel_restore,
08c3a6
 		  __pthread_unregister_cancel_restore, GLIBC_2_34);
08c3a6
diff --git a/nptl/descr.h b/nptl/descr.h
08c3a6
index dabf980e29615db3..dfef9c4bda075d13 100644
08c3a6
--- a/nptl/descr.h
08c3a6
+++ b/nptl/descr.h
08c3a6
@@ -280,18 +280,27 @@ struct pthread
08c3a6
 
08c3a6
   /* Flags determining processing of cancellation.  */
08c3a6
   int cancelhandling;
08c3a6
+  /* Bit set if cancellation is disabled.  */
08c3a6
+#define CANCELSTATE_BIT		0
08c3a6
+#define CANCELSTATE_BITMASK	(1 << CANCELSTATE_BIT)
08c3a6
+  /* Bit set if asynchronous cancellation mode is selected.  */
08c3a6
+#define CANCELTYPE_BIT		1
08c3a6
+#define CANCELTYPE_BITMASK	(1 << CANCELTYPE_BIT)
08c3a6
+  /* Bit set if canceling has been initiated.  */
08c3a6
+#define CANCELING_BIT		2
08c3a6
+#define CANCELING_BITMASK	(1 << CANCELING_BIT)
08c3a6
   /* Bit set if canceled.  */
08c3a6
 #define CANCELED_BIT		3
08c3a6
-#define CANCELED_BITMASK	(0x01 << CANCELED_BIT)
08c3a6
+#define CANCELED_BITMASK	(1 << CANCELED_BIT)
08c3a6
   /* Bit set if thread is exiting.  */
08c3a6
 #define EXITING_BIT		4
08c3a6
-#define EXITING_BITMASK		(0x01 << EXITING_BIT)
08c3a6
+#define EXITING_BITMASK		(1 << EXITING_BIT)
08c3a6
   /* Bit set if thread terminated and TCB is freed.  */
08c3a6
 #define TERMINATED_BIT		5
08c3a6
-#define TERMINATED_BITMASK	(0x01 << TERMINATED_BIT)
08c3a6
+#define TERMINATED_BITMASK	(1 << TERMINATED_BIT)
08c3a6
   /* Bit set if thread is supposed to change XID.  */
08c3a6
 #define SETXID_BIT		6
08c3a6
-#define SETXID_BITMASK		(0x01 << SETXID_BIT)
08c3a6
+#define SETXID_BITMASK		(1 << SETXID_BIT)
08c3a6
 
08c3a6
   /* Flags.  Including those copied from the thread attribute.  */
08c3a6
   int flags;
08c3a6
@@ -391,14 +400,6 @@ struct pthread
08c3a6
   /* Indicates whether is a C11 thread created by thrd_creat.  */
08c3a6
   bool c11;
08c3a6
 
08c3a6
-  /* Thread cancel state (PTHREAD_CANCEL_ENABLE or
08c3a6
-     PTHREAD_CANCEL_DISABLE).  */
08c3a6
-  unsigned char cancelstate;
08c3a6
-
08c3a6
-  /* Thread cancel type (PTHREAD_CANCEL_DEFERRED or
08c3a6
-     PTHREAD_CANCEL_ASYNCHRONOUS).  */
08c3a6
-  unsigned char canceltype;
08c3a6
-
08c3a6
   /* Used in __pthread_kill_internal to detected a thread that has
08c3a6
      exited or is about to exit.  exit_lock must only be acquired
08c3a6
      after blocking signals.  */
08c3a6
@@ -418,6 +419,22 @@ struct pthread
08c3a6
   (sizeof (struct pthread) - offsetof (struct pthread, end_padding))
08c3a6
 } __attribute ((aligned (TCB_ALIGNMENT)));
08c3a6
 
08c3a6
+static inline bool
08c3a6
+cancel_enabled_and_canceled (int value)
08c3a6
+{
08c3a6
+  return (value & (CANCELSTATE_BITMASK | CANCELED_BITMASK | EXITING_BITMASK
08c3a6
+		   | TERMINATED_BITMASK))
08c3a6
+    == CANCELED_BITMASK;
08c3a6
+}
08c3a6
+
08c3a6
+static inline bool
08c3a6
+cancel_enabled_and_canceled_and_async (int value)
08c3a6
+{
08c3a6
+  return ((value) & (CANCELSTATE_BITMASK | CANCELTYPE_BITMASK | CANCELED_BITMASK
08c3a6
+		     | EXITING_BITMASK | TERMINATED_BITMASK))
08c3a6
+    == (CANCELTYPE_BITMASK | CANCELED_BITMASK);
08c3a6
+}
08c3a6
+
08c3a6
 /* This yields the pointer that TLS support code calls the thread pointer.  */
08c3a6
 #if TLS_TCB_AT_TP
08c3a6
 # define TLS_TPADJ(pd) (pd)
08c3a6
diff --git a/nptl/libc-cleanup.c b/nptl/libc-cleanup.c
08c3a6
index 180d15bc9e9a8368..fccb1abe69aa693c 100644
08c3a6
--- a/nptl/libc-cleanup.c
08c3a6
+++ b/nptl/libc-cleanup.c
08c3a6
@@ -27,9 +27,24 @@ __libc_cleanup_push_defer (struct _pthread_cleanup_buffer *buffer)
08c3a6
 
08c3a6
   buffer->__prev = THREAD_GETMEM (self, cleanup);
08c3a6
 
08c3a6
+  int cancelhandling = atomic_load_relaxed (&self->cancelhandling);
08c3a6
+
08c3a6
   /* Disable asynchronous cancellation for now.  */
08c3a6
-  buffer->__canceltype = THREAD_GETMEM (self, canceltype);
08c3a6
-  THREAD_SETMEM (self, canceltype, PTHREAD_CANCEL_DEFERRED);
08c3a6
+  if (__glibc_unlikely (cancelhandling & CANCELTYPE_BITMASK))
08c3a6
+    {
08c3a6
+      int newval;
08c3a6
+      do
08c3a6
+	{
08c3a6
+	  newval = cancelhandling & ~CANCELTYPE_BITMASK;
08c3a6
+	}
08c3a6
+      while (!atomic_compare_exchange_weak_acquire (&self->cancelhandling,
08c3a6
+						    &cancelhandling,
08c3a6
+						    newval));
08c3a6
+    }
08c3a6
+
08c3a6
+  buffer->__canceltype = (cancelhandling & CANCELTYPE_BITMASK
08c3a6
+			  ? PTHREAD_CANCEL_ASYNCHRONOUS
08c3a6
+			  : PTHREAD_CANCEL_DEFERRED);
08c3a6
 
08c3a6
   THREAD_SETMEM (self, cleanup, buffer);
08c3a6
 }
08c3a6
@@ -42,8 +57,22 @@ __libc_cleanup_pop_restore (struct _pthread_cleanup_buffer *buffer)
08c3a6
 
08c3a6
   THREAD_SETMEM (self, cleanup, buffer->__prev);
08c3a6
 
08c3a6
-  THREAD_SETMEM (self, canceltype, buffer->__canceltype);
08c3a6
-  if (buffer->__canceltype == PTHREAD_CANCEL_ASYNCHRONOUS)
08c3a6
-      __pthread_testcancel ();
08c3a6
+  int cancelhandling = atomic_load_relaxed (&self->cancelhandling);
08c3a6
+  if (cancelhandling & CANCELTYPE_BITMASK)
08c3a6
+    {
08c3a6
+      int newval;
08c3a6
+      do
08c3a6
+	{
08c3a6
+	  newval = cancelhandling | CANCELTYPE_BITMASK;
08c3a6
+	}
08c3a6
+      while (!atomic_compare_exchange_weak_acquire (&self->cancelhandling,
08c3a6
+						    &cancelhandling, newval));
08c3a6
+
08c3a6
+      if (cancel_enabled_and_canceled (cancelhandling))
08c3a6
+	{
08c3a6
+	  self->result = PTHREAD_CANCELED;
08c3a6
+	  __do_cancel ();
08c3a6
+	}
08c3a6
+    }
08c3a6
 }
08c3a6
 libc_hidden_def (__libc_cleanup_pop_restore)
08c3a6
diff --git a/nptl/pthread_cancel.c b/nptl/pthread_cancel.c
08c3a6
index 9bac6e3b76a20312..2680b55586e035fe 100644
08c3a6
--- a/nptl/pthread_cancel.c
08c3a6
+++ b/nptl/pthread_cancel.c
08c3a6
@@ -43,18 +43,29 @@ sigcancel_handler (int sig, siginfo_t *si, void *ctx)
08c3a6
 
08c3a6
   struct pthread *self = THREAD_SELF;
08c3a6
 
08c3a6
-  int ch = atomic_load_relaxed (&self->cancelhandling);
08c3a6
-  /* Cancelation not enabled, not cancelled, or already exitting.  */
08c3a6
-  if (self->cancelstate == PTHREAD_CANCEL_DISABLE
08c3a6
-      || (ch & CANCELED_BITMASK) == 0
08c3a6
-      || (ch & EXITING_BITMASK) != 0)
08c3a6
-    return;
08c3a6
-
08c3a6
-  /* Set the return value.  */
08c3a6
-  THREAD_SETMEM (self, result, PTHREAD_CANCELED);
08c3a6
-  /* Make sure asynchronous cancellation is still enabled.  */
08c3a6
-  if (self->canceltype == PTHREAD_CANCEL_ASYNCHRONOUS)
08c3a6
-    __do_cancel ();
08c3a6
+  int oldval = atomic_load_relaxed (&self->cancelhandling);
08c3a6
+  while (1)
08c3a6
+    {
08c3a6
+      /* We are canceled now.  When canceled by another thread this flag
08c3a6
+	 is already set but if the signal is directly send (internally or
08c3a6
+	 from another process) is has to be done here.  */
08c3a6
+      int newval = oldval | CANCELING_BITMASK | CANCELED_BITMASK;
08c3a6
+
08c3a6
+      if (oldval == newval || (oldval & EXITING_BITMASK) != 0)
08c3a6
+	/* Already canceled or exiting.  */
08c3a6
+	break;
08c3a6
+
08c3a6
+      if (atomic_compare_exchange_weak_acquire (&self->cancelhandling,
08c3a6
+						&oldval, newval))
08c3a6
+	{
08c3a6
+	  self->result = PTHREAD_CANCELED;
08c3a6
+
08c3a6
+	  /* Make sure asynchronous cancellation is still enabled.  */
08c3a6
+	  if ((oldval & CANCELTYPE_BITMASK) != 0)
08c3a6
+	    /* Run the registered destructors and terminate the thread.  */
08c3a6
+	    __do_cancel ();
08c3a6
+	}
08c3a6
+    }
08c3a6
 }
08c3a6
 
08c3a6
 int
08c3a6
@@ -93,29 +104,70 @@ __pthread_cancel (pthread_t th)
08c3a6
   }
08c3a6
 #endif
08c3a6
 
08c3a6
-  int oldch = atomic_fetch_or_acquire (&pd->cancelhandling, CANCELED_BITMASK);
08c3a6
-  if ((oldch & CANCELED_BITMASK) != 0)
08c3a6
-    return 0;
08c3a6
-
08c3a6
-  if (pd == THREAD_SELF)
08c3a6
+  /* Some syscalls are never restarted after being interrupted by a signal
08c3a6
+     handler, regardless of the use of SA_RESTART (they always fail with
08c3a6
+     EINTR).  So pthread_cancel cannot send SIGCANCEL unless the cancellation
08c3a6
+     is enabled and set as asynchronous (in this case the cancellation will
08c3a6
+     be acted in the cancellation handler instead by the syscall wrapper).
08c3a6
+     Otherwise the target thread is set as 'cancelling' (CANCELING_BITMASK)
08c3a6
+     by atomically setting 'cancelhandling' and the cancelation will be acted
08c3a6
+     upon on next cancellation entrypoing in the target thread.
08c3a6
+
08c3a6
+     It also requires to atomically check if cancellation is enabled and
08c3a6
+     asynchronous, so both cancellation state and type are tracked on
08c3a6
+     'cancelhandling'.  */
08c3a6
+
08c3a6
+  int result = 0;
08c3a6
+  int oldval = atomic_load_relaxed (&pd->cancelhandling);
08c3a6
+  int newval;
08c3a6
+  do
08c3a6
     {
08c3a6
-      /* A single-threaded process should be able to kill itself, since there
08c3a6
-	 is nothing in the POSIX specification that says that it cannot.  So
08c3a6
-	 we set multiple_threads to true so that cancellation points get
08c3a6
-	 executed.  */
08c3a6
-      THREAD_SETMEM (THREAD_SELF, header.multiple_threads, 1);
08c3a6
+      newval = oldval | CANCELING_BITMASK | CANCELED_BITMASK;
08c3a6
+      if (oldval == newval)
08c3a6
+	break;
08c3a6
+
08c3a6
+      /* If the cancellation is handled asynchronously just send a
08c3a6
+	 signal.  We avoid this if possible since it's more
08c3a6
+	 expensive.  */
08c3a6
+      if (cancel_enabled_and_canceled_and_async (newval))
08c3a6
+	{
08c3a6
+	  /* Mark the cancellation as "in progress".  */
08c3a6
+	  int newval2 = oldval | CANCELING_BITMASK;
08c3a6
+	  if (!atomic_compare_exchange_weak_acquire (&pd->cancelhandling,
08c3a6
+						     &oldval, newval2))
08c3a6
+	    continue;
08c3a6
+
08c3a6
+	  if (pd == THREAD_SELF)
08c3a6
+	    /* This is not merely an optimization: An application may
08c3a6
+	       call pthread_cancel (pthread_self ()) without calling
08c3a6
+	       pthread_create, so the signal handler may not have been
08c3a6
+	       set up for a self-cancel.  */
08c3a6
+	    {
08c3a6
+	      pd->result = PTHREAD_CANCELED;
08c3a6
+	      if ((newval & CANCELTYPE_BITMASK) != 0)
08c3a6
+		__do_cancel ();
08c3a6
+	    }
08c3a6
+	  else
08c3a6
+	    /* The cancellation handler will take care of marking the
08c3a6
+	       thread as canceled.  */
08c3a6
+	    result = __pthread_kill_internal (th, SIGCANCEL);
08c3a6
+
08c3a6
+	  break;
08c3a6
+	}
08c3a6
+
08c3a6
+	/* A single-threaded process should be able to kill itself, since
08c3a6
+	   there is nothing in the POSIX specification that says that it
08c3a6
+	   cannot.  So we set multiple_threads to true so that cancellation
08c3a6
+	   points get executed.  */
08c3a6
+	THREAD_SETMEM (THREAD_SELF, header.multiple_threads, 1);
08c3a6
 #ifndef TLS_MULTIPLE_THREADS_IN_TCB
08c3a6
       __libc_multiple_threads = 1;
08c3a6
 #endif
08c3a6
-
08c3a6
-      THREAD_SETMEM (pd, result, PTHREAD_CANCELED);
08c3a6
-      if (pd->cancelstate == PTHREAD_CANCEL_ENABLE
08c3a6
-	  && pd->canceltype == PTHREAD_CANCEL_ASYNCHRONOUS)
08c3a6
-	__do_cancel ();
08c3a6
-      return 0;
08c3a6
     }
08c3a6
+  while (!atomic_compare_exchange_weak_acquire (&pd->cancelhandling, &oldval,
08c3a6
+						newval));
08c3a6
 
08c3a6
-  return __pthread_kill_internal (th, SIGCANCEL);
08c3a6
+  return result;
08c3a6
 }
08c3a6
 versioned_symbol (libc, __pthread_cancel, pthread_cancel, GLIBC_2_34);
08c3a6
 
08c3a6
diff --git a/nptl/pthread_join_common.c b/nptl/pthread_join_common.c
08c3a6
index 7303069316caef13..617056ef10671607 100644
08c3a6
--- a/nptl/pthread_join_common.c
08c3a6
+++ b/nptl/pthread_join_common.c
08c3a6
@@ -57,12 +57,9 @@ __pthread_clockjoin_ex (pthread_t threadid, void **thread_return,
08c3a6
   if ((pd == self
08c3a6
        || (self->joinid == pd
08c3a6
 	   && (pd->cancelhandling
08c3a6
-	       & (CANCELED_BITMASK | EXITING_BITMASK
08c3a6
+	       & (CANCELING_BITMASK | CANCELED_BITMASK | EXITING_BITMASK
08c3a6
 		  | TERMINATED_BITMASK)) == 0))
08c3a6
-      && !(self->cancelstate == PTHREAD_CANCEL_ENABLE
08c3a6
-	   && (pd->cancelhandling & (CANCELED_BITMASK | EXITING_BITMASK
08c3a6
-				     | TERMINATED_BITMASK))
08c3a6
-	       == CANCELED_BITMASK))
08c3a6
+      && !cancel_enabled_and_canceled (self->cancelhandling))
08c3a6
     /* This is a deadlock situation.  The threads are waiting for each
08c3a6
        other to finish.  Note that this is a "may" error.  To be 100%
08c3a6
        sure we catch this error we would have to lock the data
08c3a6
diff --git a/nptl/pthread_setcancelstate.c b/nptl/pthread_setcancelstate.c
08c3a6
index 7e2b6e4974bd58bd..cb567be5926816f1 100644
08c3a6
--- a/nptl/pthread_setcancelstate.c
08c3a6
+++ b/nptl/pthread_setcancelstate.c
08c3a6
@@ -31,9 +31,29 @@ __pthread_setcancelstate (int state, int *oldstate)
08c3a6
 
08c3a6
   self = THREAD_SELF;
08c3a6
 
08c3a6
-  if (oldstate != NULL)
08c3a6
-    *oldstate = self->cancelstate;
08c3a6
-  self->cancelstate = state;
08c3a6
+  int oldval = atomic_load_relaxed (&self->cancelhandling);
08c3a6
+  while (1)
08c3a6
+    {
08c3a6
+      int newval = (state == PTHREAD_CANCEL_DISABLE
08c3a6
+		    ? oldval | CANCELSTATE_BITMASK
08c3a6
+		    : oldval & ~CANCELSTATE_BITMASK);
08c3a6
+
08c3a6
+      if (oldstate != NULL)
08c3a6
+	*oldstate = ((oldval & CANCELSTATE_BITMASK)
08c3a6
+		     ? PTHREAD_CANCEL_DISABLE : PTHREAD_CANCEL_ENABLE);
08c3a6
+
08c3a6
+      if (oldval == newval)
08c3a6
+	break;
08c3a6
+
08c3a6
+      if (atomic_compare_exchange_weak_acquire (&self->cancelhandling,
08c3a6
+						&oldval, newval))
08c3a6
+	{
08c3a6
+	  if (cancel_enabled_and_canceled_and_async (newval))
08c3a6
+	    __do_cancel ();
08c3a6
+
08c3a6
+	  break;
08c3a6
+	}
08c3a6
+    }
08c3a6
 
08c3a6
   return 0;
08c3a6
 }
08c3a6
diff --git a/nptl/pthread_setcanceltype.c b/nptl/pthread_setcanceltype.c
08c3a6
index e7b24ae733dcc0f2..e08ff7b141f904f1 100644
08c3a6
--- a/nptl/pthread_setcanceltype.c
08c3a6
+++ b/nptl/pthread_setcanceltype.c
08c3a6
@@ -29,11 +29,32 @@ __pthread_setcanceltype (int type, int *oldtype)
08c3a6
 
08c3a6
   volatile struct pthread *self = THREAD_SELF;
08c3a6
 
08c3a6
-  if (oldtype != NULL)
08c3a6
-    *oldtype = self->canceltype;
08c3a6
-  self->canceltype = type;
08c3a6
-  if (type == PTHREAD_CANCEL_ASYNCHRONOUS)
08c3a6
-    __pthread_testcancel ();
08c3a6
+  int oldval = atomic_load_relaxed (&self->cancelhandling);
08c3a6
+  while (1)
08c3a6
+    {
08c3a6
+      int newval = (type == PTHREAD_CANCEL_ASYNCHRONOUS
08c3a6
+		    ? oldval | CANCELTYPE_BITMASK
08c3a6
+		    : oldval & ~CANCELTYPE_BITMASK);
08c3a6
+
08c3a6
+      if (oldtype != NULL)
08c3a6
+	*oldtype = ((oldval & CANCELTYPE_BITMASK)
08c3a6
+		    ? PTHREAD_CANCEL_ASYNCHRONOUS : PTHREAD_CANCEL_DEFERRED);
08c3a6
+
08c3a6
+      if (oldval == newval)
08c3a6
+	break;
08c3a6
+
08c3a6
+      if (atomic_compare_exchange_weak_acquire (&self->cancelhandling,
08c3a6
+						&oldval, newval))
08c3a6
+	{
08c3a6
+	  if (cancel_enabled_and_canceled_and_async (newval))
08c3a6
+	    {
08c3a6
+	      THREAD_SETMEM (self, result, PTHREAD_CANCELED);
08c3a6
+	      __do_cancel ();
08c3a6
+	    }
08c3a6
+
08c3a6
+	  break;
08c3a6
+	}
08c3a6
+    }
08c3a6
 
08c3a6
   return 0;
08c3a6
 }
08c3a6
diff --git a/nptl/pthread_testcancel.c b/nptl/pthread_testcancel.c
08c3a6
index 31185d89f2ab84c6..25230215fd607e8b 100644
08c3a6
--- a/nptl/pthread_testcancel.c
08c3a6
+++ b/nptl/pthread_testcancel.c
08c3a6
@@ -24,13 +24,10 @@ void
08c3a6
 ___pthread_testcancel (void)
08c3a6
 {
08c3a6
   struct pthread *self = THREAD_SELF;
08c3a6
-  int cancelhandling = THREAD_GETMEM (self, cancelhandling);
08c3a6
-  if (self->cancelstate == PTHREAD_CANCEL_ENABLE
08c3a6
-      && (cancelhandling & CANCELED_BITMASK)
08c3a6
-      && !(cancelhandling & EXITING_BITMASK)
08c3a6
-      && !(cancelhandling & TERMINATED_BITMASK))
08c3a6
+  int cancelhandling = atomic_load_relaxed (&self->cancelhandling);
08c3a6
+  if (cancel_enabled_and_canceled (cancelhandling))
08c3a6
     {
08c3a6
-      THREAD_SETMEM (self, result, PTHREAD_CANCELED);
08c3a6
+      self->result = PTHREAD_CANCELED;
08c3a6
       __do_cancel ();
08c3a6
     }
08c3a6
 }
08c3a6
diff --git a/sysdeps/nptl/dl-tls_init_tp.c b/sysdeps/nptl/dl-tls_init_tp.c
08c3a6
index b39dfbff2c6678d5..23aa4cfc0b784dfc 100644
08c3a6
--- a/sysdeps/nptl/dl-tls_init_tp.c
08c3a6
+++ b/sysdeps/nptl/dl-tls_init_tp.c
08c3a6
@@ -107,7 +107,4 @@ __tls_init_tp (void)
08c3a6
      It will be bigger than it actually is, but for unwind.c/pt-longjmp.c
08c3a6
      purposes this is good enough.  */
08c3a6
   THREAD_SETMEM (pd, stackblock_size, (size_t) __libc_stack_end);
08c3a6
-
08c3a6
-  THREAD_SETMEM (pd, cancelstate, PTHREAD_CANCEL_ENABLE);
08c3a6
-  THREAD_SETMEM (pd, canceltype, PTHREAD_CANCEL_DEFERRED);
08c3a6
 }
08c3a6
diff --git a/sysdeps/nptl/pthreadP.h b/sysdeps/nptl/pthreadP.h
08c3a6
index 374657a2fd0ee19a..b968afc4c6b61b92 100644
08c3a6
--- a/sysdeps/nptl/pthreadP.h
08c3a6
+++ b/sysdeps/nptl/pthreadP.h
08c3a6
@@ -276,7 +276,7 @@ __do_cancel (void)
08c3a6
   struct pthread *self = THREAD_SELF;
08c3a6
 
08c3a6
   /* Make sure we get no more cancellations.  */
08c3a6
-  THREAD_ATOMIC_BIT_SET (self, cancelhandling, EXITING_BIT);
08c3a6
+  atomic_bit_set (&self->cancelhandling, EXITING_BIT);
08c3a6
 
08c3a6
   __pthread_unwind ((__pthread_unwind_buf_t *)
08c3a6
 		    THREAD_GETMEM (self, cleanup_jmp_buf));
08c3a6
diff --git a/sysdeps/pthread/Makefile b/sysdeps/pthread/Makefile
08c3a6
index c65710169697ad95..00419c4d199df912 100644
08c3a6
--- a/sysdeps/pthread/Makefile
08c3a6
+++ b/sysdeps/pthread/Makefile
08c3a6
@@ -69,6 +69,7 @@ tests += tst-cnd-basic tst-mtx-trylock tst-cnd-broadcast \
08c3a6
 	 tst-cancel12 tst-cancel13 tst-cancel14 tst-cancel15 tst-cancel16 \
08c3a6
 	 tst-cancel18 tst-cancel19 tst-cancel20 tst-cancel21 \
08c3a6
 	 tst-cancel22 tst-cancel23 tst-cancel26 tst-cancel27 tst-cancel28 \
08c3a6
+	 tst-cancel29 \
08c3a6
 	 tst-cleanup0 tst-cleanup1 tst-cleanup2 tst-cleanup3 \
08c3a6
 	 tst-clock1 \
08c3a6
 	 tst-cond-except \
08c3a6
diff --git a/sysdeps/pthread/tst-cancel29.c b/sysdeps/pthread/tst-cancel29.c
08c3a6
new file mode 100644
08c3a6
index 0000000000000000..4f0d99e002883be4
08c3a6
--- /dev/null
08c3a6
+++ b/sysdeps/pthread/tst-cancel29.c
08c3a6
@@ -0,0 +1,207 @@
08c3a6
+/* Check if a thread that disables cancellation and which call functions
08c3a6
+   that might be interrupted by a signal do not see the internal SIGCANCEL.
08c3a6
+
08c3a6
+   Copyright (C) 2022 Free Software Foundation, Inc.
08c3a6
+   This file is part of the GNU C Library.
08c3a6
+
08c3a6
+   The GNU C Library is free software; you can redistribute it and/or
08c3a6
+   modify it under the terms of the GNU Lesser General Public
08c3a6
+   License as published by the Free Software Foundation; either
08c3a6
+   version 2.1 of the License, or (at your option) any later version.
08c3a6
+
08c3a6
+   The GNU C Library is distributed in the hope that it will be useful,
08c3a6
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
08c3a6
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
08c3a6
+   Lesser General Public License for more details.
08c3a6
+
08c3a6
+   You should have received a copy of the GNU Lesser General Public
08c3a6
+   License along with the GNU C Library; if not, see
08c3a6
+   <https://www.gnu.org/licenses/>.  */
08c3a6
+
08c3a6
+#include <array_length.h>
08c3a6
+#include <errno.h>
08c3a6
+#include <inttypes.h>
08c3a6
+#include <poll.h>
08c3a6
+#include <support/check.h>
08c3a6
+#include <support/support.h>
08c3a6
+#include <support/temp_file.h>
08c3a6
+#include <support/xthread.h>
08c3a6
+#include <sys/socket.h>
08c3a6
+#include <signal.h>
08c3a6
+#include <stdio.h>
08c3a6
+#include <unistd.h>
08c3a6
+
08c3a6
+/* On Linux some interfaces are never restarted after being interrupted by
08c3a6
+   a signal handler, regardless of the use of SA_RESTART.  It means that
08c3a6
+   if asynchronous cancellation is not enabled, the pthread_cancel can not
08c3a6
+   set the internal SIGCANCEL otherwise the interface might see a spurious
08c3a6
+   EINTR failure.  */
08c3a6
+
08c3a6
+static pthread_barrier_t b;
08c3a6
+
08c3a6
+/* Cleanup handling test.  */
08c3a6
+static int cl_called;
08c3a6
+static void
08c3a6
+cl (void *arg)
08c3a6
+{
08c3a6
+  ++cl_called;
08c3a6
+}
08c3a6
+
08c3a6
+static void *
08c3a6
+tf_sigtimedwait (void *arg)
08c3a6
+{
08c3a6
+  pthread_setcancelstate (PTHREAD_CANCEL_DISABLE, NULL);
08c3a6
+  xpthread_barrier_wait (&b);
08c3a6
+
08c3a6
+  int r;
08c3a6
+  pthread_cleanup_push (cl, NULL);
08c3a6
+
08c3a6
+  sigset_t mask;
08c3a6
+  sigemptyset (&mask);
08c3a6
+  r = sigtimedwait (&mask, NULL, &(struct timespec) { 0, 250000000 });
08c3a6
+  if (r != -1)
08c3a6
+    return (void*) -1;
08c3a6
+  if (errno != EAGAIN)
08c3a6
+    return (void*) -2;
08c3a6
+
08c3a6
+  pthread_cleanup_pop (0);
08c3a6
+  return NULL;
08c3a6
+}
08c3a6
+
08c3a6
+static void *
08c3a6
+tf_poll (void *arg)
08c3a6
+{
08c3a6
+  pthread_setcancelstate (PTHREAD_CANCEL_DISABLE, NULL);
08c3a6
+  xpthread_barrier_wait (&b);
08c3a6
+
08c3a6
+  int r;
08c3a6
+  pthread_cleanup_push (cl, NULL);
08c3a6
+
08c3a6
+  r = poll (NULL, 0, 250);
08c3a6
+  if (r != 0)
08c3a6
+    return (void*) -1;
08c3a6
+
08c3a6
+  pthread_cleanup_pop (0);
08c3a6
+  return NULL;
08c3a6
+}
08c3a6
+
08c3a6
+static void *
08c3a6
+tf_ppoll (void *arg)
08c3a6
+{
08c3a6
+  pthread_setcancelstate (PTHREAD_CANCEL_DISABLE, NULL);
08c3a6
+
08c3a6
+  xpthread_barrier_wait (&b);
08c3a6
+
08c3a6
+  int r;
08c3a6
+  pthread_cleanup_push (cl, NULL);
08c3a6
+
08c3a6
+  r = ppoll (NULL, 0, &(struct timespec) { 0, 250000000 }, NULL);
08c3a6
+  if (r != 0)
08c3a6
+    return (void*) -1;
08c3a6
+
08c3a6
+  pthread_cleanup_pop (0);
08c3a6
+  return NULL;
08c3a6
+}
08c3a6
+
08c3a6
+static void *
08c3a6
+tf_select (void *arg)
08c3a6
+{
08c3a6
+  pthread_setcancelstate (PTHREAD_CANCEL_DISABLE, NULL);
08c3a6
+  xpthread_barrier_wait (&b);
08c3a6
+
08c3a6
+  int r;
08c3a6
+  pthread_cleanup_push (cl, NULL);
08c3a6
+
08c3a6
+  r = select (0, NULL, NULL, NULL, &(struct timeval) { 0, 250000 });
08c3a6
+  if (r != 0)
08c3a6
+    return (void*) -1;
08c3a6
+
08c3a6
+  pthread_cleanup_pop (0);
08c3a6
+  return NULL;
08c3a6
+}
08c3a6
+
08c3a6
+static void *
08c3a6
+tf_pselect (void *arg)
08c3a6
+{
08c3a6
+  pthread_setcancelstate (PTHREAD_CANCEL_DISABLE, NULL);
08c3a6
+  xpthread_barrier_wait (&b);
08c3a6
+
08c3a6
+  int r;
08c3a6
+  pthread_cleanup_push (cl, NULL);
08c3a6
+
08c3a6
+  r = pselect (0, NULL, NULL, NULL, &(struct timespec) { 0, 250000000 }, NULL);
08c3a6
+  if (r != 0)
08c3a6
+    return (void*) -1;
08c3a6
+
08c3a6
+  pthread_cleanup_pop (0);
08c3a6
+  return NULL;
08c3a6
+}
08c3a6
+
08c3a6
+static void *
08c3a6
+tf_clock_nanosleep (void *arg)
08c3a6
+{
08c3a6
+  pthread_setcancelstate (PTHREAD_CANCEL_DISABLE, NULL);
08c3a6
+  xpthread_barrier_wait (&b);
08c3a6
+
08c3a6
+  int r;
08c3a6
+  pthread_cleanup_push (cl, NULL);
08c3a6
+
08c3a6
+  r = clock_nanosleep (CLOCK_REALTIME, 0, &(struct timespec) { 0, 250000000 },
08c3a6
+		       NULL);
08c3a6
+  if (r != 0)
08c3a6
+    return (void*) -1;
08c3a6
+
08c3a6
+  pthread_cleanup_pop (0);
08c3a6
+  return NULL;
08c3a6
+}
08c3a6
+
08c3a6
+struct cancel_test_t
08c3a6
+{
08c3a6
+  const char *name;
08c3a6
+  void * (*cf) (void *);
08c3a6
+} tests[] =
08c3a6
+{
08c3a6
+  { "sigtimedwait",    tf_sigtimedwait,    },
08c3a6
+  { "poll",            tf_poll,            },
08c3a6
+  { "ppoll",           tf_ppoll,           },
08c3a6
+  { "select",          tf_select,          },
08c3a6
+  { "pselect",         tf_pselect  ,       },
08c3a6
+  { "clock_nanosleep", tf_clock_nanosleep, },
08c3a6
+};
08c3a6
+
08c3a6
+static int
08c3a6
+do_test (void)
08c3a6
+{
08c3a6
+  for (int i = 0; i < array_length (tests); i++)
08c3a6
+    {
08c3a6
+      xpthread_barrier_init (&b, NULL, 2);
08c3a6
+
08c3a6
+      cl_called = 0;
08c3a6
+
08c3a6
+      pthread_t th = xpthread_create (NULL, tests[i].cf, NULL);
08c3a6
+
08c3a6
+      xpthread_barrier_wait (&b);
08c3a6
+
08c3a6
+      struct timespec ts = { .tv_sec = 0, .tv_nsec = 100000000 };
08c3a6
+      while (nanosleep (&ts, &ts) != 0)
08c3a6
+	continue;
08c3a6
+
08c3a6
+      xpthread_cancel (th);
08c3a6
+
08c3a6
+      void *status = xpthread_join (th);
08c3a6
+      if (status != NULL)
08c3a6
+	printf ("test '%s' failed: %" PRIdPTR "\n", tests[i].name,
08c3a6
+		(intptr_t) status);
08c3a6
+      TEST_VERIFY (status == NULL);
08c3a6
+
08c3a6
+      xpthread_barrier_destroy (&b);
08c3a6
+
08c3a6
+      TEST_COMPARE (cl_called, 0);
08c3a6
+
08c3a6
+      printf ("in-time cancel test of '%s' successful\n", tests[i].name);
08c3a6
+    }
08c3a6
+
08c3a6
+  return 0;
08c3a6
+}
08c3a6
+
08c3a6
+#include <support/test-driver.c>