[PATCH] Fix unbalanced nwaiters counting in sem_wait (bug 32436)

Andreas Schwab schwab@suse.de
Mon Dec 9 13:03:13 GMT 2024


When __new_sem_wait_slow64 is entered while a token is available on the
semaphore it decrements the nwaiters field by one too much (in the
__HAVE_64B_ATOMICS case).  The initial increment remembers the value
before the increment, so the final decrement will set the counter to one
smaller than before entry.
---
 nptl/sem_waitcommon.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/nptl/sem_waitcommon.c b/nptl/sem_waitcommon.c
index 75fb26b6f1..22f6ab22c1 100644
--- a/nptl/sem_waitcommon.c
+++ b/nptl/sem_waitcommon.c
@@ -170,7 +170,8 @@ __new_sem_wait_slow64 (struct new_sem *sem, clockid_t clockid,
   /* Add a waiter.  Relaxed MO is sufficient because we can rely on the
      ordering provided by the RMW operations we use.  */
   uint64_t d = atomic_fetch_add_relaxed (&sem->data,
-      (uint64_t) 1 << SEM_NWAITERS_SHIFT);
+      (uint64_t) 1 << SEM_NWAITERS_SHIFT)
+    + ((uint64_t) 1 << SEM_NWAITERS_SHIFT);
 
   pthread_cleanup_push (__sem_wait_cleanup, sem);
 
-- 
2.47.1


-- 
Andreas Schwab, SUSE Labs, schwab@suse.de
GPG Key fingerprint = 0196 BAD8 1CE9 1970 F4BE  1748 E4D4 88E3 0EEA B9D7
"And now for something completely different."


More information about the Libc-alpha mailing list