Blame nptl/tst-rwlock-tryrdlock-stall.c

Packit Service 0f798c
/* Bug 23844: Test for pthread_rwlock_tryrdlock stalls.
Packit Service 0f798c
   Copyright (C) 2019 Free Software Foundation, Inc.
Packit Service 0f798c
   This file is part of the GNU C Library.
Packit Service 0f798c
Packit Service 0f798c
   The GNU C Library is free software; you can redistribute it and/or
Packit Service 0f798c
   modify it under the terms of the GNU Lesser General Public
Packit Service 0f798c
   License as published by the Free Software Foundation; either
Packit Service 0f798c
   version 2.1 of the License, or (at your option) any later version.
Packit Service 0f798c
Packit Service 0f798c
   The GNU C Library is distributed in the hope that it will be useful,
Packit Service 0f798c
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit Service 0f798c
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit Service 0f798c
   Lesser General Public License for more details.
Packit Service 0f798c
Packit Service 0f798c
   You should have received a copy of the GNU Lesser General Public
Packit Service 0f798c
   License along with the GNU C Library; if not, see
Packit Service 0f798c
   <http://www.gnu.org/licenses/>.  */
Packit Service 0f798c
Packit Service 0f798c
/* For a full analysis see comment:
Packit Service 0f798c
   https://sourceware.org/bugzilla/show_bug.cgi?id=23844#c14
Packit Service 0f798c
Packit Service 0f798c
   Provided here for reference:
Packit Service 0f798c
Packit Service 0f798c
   --- Analysis of pthread_rwlock_tryrdlock() stall ---
Packit Service 0f798c
   A read lock begins to execute.
Packit Service 0f798c
Packit Service 0f798c
   In __pthread_rwlock_rdlock_full:
Packit Service 0f798c
Packit Service 0f798c
   We can attempt a read lock, but find that the lock is
Packit Service 0f798c
   in a write phase (PTHREAD_RWLOCK_WRPHASE, or WP-bit
Packit Service 0f798c
   is set), and the lock is held by a primary writer
Packit Service 0f798c
   (PTHREAD_RWLOCK_WRLOCKED is set). In this case we must
Packit Service 0f798c
   wait for explicit hand over from the writer to us or
Packit Service 0f798c
   one of the other waiters. The read lock threads are
Packit Service 0f798c
   about to execute:
Packit Service 0f798c
Packit Service 0f798c
   341   r = (atomic_fetch_add_acquire (&rwlock->__data.__readers,
Packit Service 0f798c
   342                                  (1 << PTHREAD_RWLOCK_READER_SHIFT))
Packit Service 0f798c
   343        + (1 << PTHREAD_RWLOCK_READER_SHIFT));
Packit Service 0f798c
Packit Service 0f798c
   An unlock beings to execute.
Packit Service 0f798c
Packit Service 0f798c
   Then in __pthread_rwlock_wrunlock:
Packit Service 0f798c
Packit Service 0f798c
   547   unsigned int r = atomic_load_relaxed (&rwlock->__data.__readers);
Packit Service 0f798c
   ...
Packit Service 0f798c
   549   while (!atomic_compare_exchange_weak_release
Packit Service 0f798c
   550          (&rwlock->__data.__readers, &r,
Packit Service 0f798c
   551           ((r ^ PTHREAD_RWLOCK_WRLOCKED)
Packit Service 0f798c
   552            ^ ((r >> PTHREAD_RWLOCK_READER_SHIFT) == 0 ? 0
Packit Service 0f798c
   553               : PTHREAD_RWLOCK_WRPHASE))))
Packit Service 0f798c
   554     {
Packit Service 0f798c
   ...
Packit Service 0f798c
   556     }
Packit Service 0f798c
Packit Service 0f798c
   We clear PTHREAD_RWLOCK_WRLOCKED, and if there are
Packit Service 0f798c
   no readers so we leave the lock in PTHRAD_RWLOCK_WRPHASE.
Packit Service 0f798c
Packit Service 0f798c
   Back in the read lock.
Packit Service 0f798c
Packit Service 0f798c
   The read lock adjusts __readres as above.
Packit Service 0f798c
Packit Service 0f798c
   383   while ((r & PTHREAD_RWLOCK_WRPHASE) != 0
Packit Service 0f798c
   384          && (r & PTHREAD_RWLOCK_WRLOCKED) == 0)
Packit Service 0f798c
   385     {
Packit Service 0f798c
   ...
Packit Service 0f798c
   390       if (atomic_compare_exchange_weak_acquire (&rwlock->__data.__readers, &r,
Packit Service 0f798c
   391                                                 r ^ PTHREAD_RWLOCK_WRPHASE))
Packit Service 0f798c
   392         {
Packit Service 0f798c
Packit Service 0f798c
   And then attemps to start the read phase.
Packit Service 0f798c
Packit Service 0f798c
   Assume there happens to be a tryrdlock at this point, noting
Packit Service 0f798c
   that PTHREAD_RWLOCK_WRLOCKED is clear, and PTHREAD_RWLOCK_WRPHASE
Packit Service 0f798c
   is 1. So the try lock attemps to start the read phase.
Packit Service 0f798c
Packit Service 0f798c
   In __pthread_rwlock_tryrdlock:
Packit Service 0f798c
Packit Service 0f798c
    44       if ((r & PTHREAD_RWLOCK_WRPHASE) == 0)
Packit Service 0f798c
    45         {
Packit Service 0f798c
   ...
Packit Service 0f798c
    49           if (((r & PTHREAD_RWLOCK_WRLOCKED) != 0)
Packit Service 0f798c
    50               && (rwlock->__data.__flags
Packit Service 0f798c
    51                   == PTHREAD_RWLOCK_PREFER_WRITER_NONRECURSIVE_NP))
Packit Service 0f798c
    52             return EBUSY;
Packit Service 0f798c
    53           rnew = r + (1 << PTHREAD_RWLOCK_READER_SHIFT);
Packit Service 0f798c
    54         }
Packit Service 0f798c
   ...
Packit Service 0f798c
    89   while (!atomic_compare_exchange_weak_acquire (&rwlock->__data.__readers,
Packit Service 0f798c
    90       &r, rnew));
Packit Service 0f798c
Packit Service 0f798c
   And succeeds.
Packit Service 0f798c
Packit Service 0f798c
   Back in the write unlock:
Packit Service 0f798c
Packit Service 0f798c
   557   if ((r >> PTHREAD_RWLOCK_READER_SHIFT) != 0)
Packit Service 0f798c
   558     {
Packit Service 0f798c
   ...
Packit Service 0f798c
   563       if ((atomic_exchange_relaxed (&rwlock->__data.__wrphase_futex, 0)
Packit Service 0f798c
   564            & PTHREAD_RWLOCK_FUTEX_USED) != 0)
Packit Service 0f798c
   565         futex_wake (&rwlock->__data.__wrphase_futex, INT_MAX, private);
Packit Service 0f798c
   566     }
Packit Service 0f798c
Packit Service 0f798c
   We note that PTHREAD_RWLOCK_FUTEX_USED is non-zero
Packit Service 0f798c
   and don't wake anyone. This is OK because we handed
Packit Service 0f798c
   over to the trylock. It will be the trylock's responsibility
Packit Service 0f798c
   to wake any waiters.
Packit Service 0f798c
Packit Service 0f798c
   Back in the read lock:
Packit Service 0f798c
Packit Service 0f798c
   The read lock fails to install PTHRAD_REWLOCK_WRPHASE as 0 because
Packit Service 0f798c
   the __readers value was adjusted by the trylock, and so it falls through
Packit Service 0f798c
   to waiting on the lock for explicit handover from either a new writer
Packit Service 0f798c
   or a new reader.
Packit Service 0f798c
Packit Service 0f798c
   448           int err = futex_abstimed_wait (&rwlock->__data.__wrphase_futex,
Packit Service 0f798c
   449                                          1 | PTHREAD_RWLOCK_FUTEX_USED,
Packit Service 0f798c
   450                                          abstime, private);
Packit Service 0f798c
Packit Service 0f798c
   We use PTHREAD_RWLOCK_FUTEX_USED to indicate the futex
Packit Service 0f798c
   is in use.
Packit Service 0f798c
Packit Service 0f798c
   At this point we have readers waiting on the read lock
Packit Service 0f798c
   to unlock. The wrlock is done. The trylock is finishing
Packit Service 0f798c
   the installation of the read phase.
Packit Service 0f798c
Packit Service 0f798c
    92   if ((r & PTHREAD_RWLOCK_WRPHASE) != 0)
Packit Service 0f798c
    93     {
Packit Service 0f798c
   ...
Packit Service 0f798c
   105       atomic_store_relaxed (&rwlock->__data.__wrphase_futex, 0);
Packit Service 0f798c
   106     }
Packit Service 0f798c
Packit Service 0f798c
   The trylock does note that we were the one that
Packit Service 0f798c
   installed the read phase, but the comments are not
Packit Service 0f798c
   correct, the execution ordering above shows that
Packit Service 0f798c
   readers might indeed be waiting, and they are.
Packit Service 0f798c
Packit Service 0f798c
   The atomic_store_relaxed throws away PTHREAD_RWLOCK_FUTEX_USED,
Packit Service 0f798c
   and the waiting reader is never worken becuase as noted
Packit Service 0f798c
   above it is conditional on the futex being used.
Packit Service 0f798c
Packit Service 0f798c
   The solution is for the trylock thread to inspect
Packit Service 0f798c
   PTHREAD_RWLOCK_FUTEX_USED and wake the waiting readers.
Packit Service 0f798c
Packit Service 0f798c
   --- Analysis of pthread_rwlock_trywrlock() stall ---
Packit Service 0f798c
Packit Service 0f798c
   A write lock begins to execute, takes the write lock,
Packit Service 0f798c
   and then releases the lock...
Packit Service 0f798c
Packit Service 0f798c
   In pthread_rwlock_wrunlock():
Packit Service 0f798c
Packit Service 0f798c
   547   unsigned int r = atomic_load_relaxed (&rwlock->__data.__readers);
Packit Service 0f798c
   ...
Packit Service 0f798c
   549   while (!atomic_compare_exchange_weak_release
Packit Service 0f798c
   550          (&rwlock->__data.__readers, &r,
Packit Service 0f798c
   551           ((r ^ PTHREAD_RWLOCK_WRLOCKED)
Packit Service 0f798c
   552            ^ ((r >> PTHREAD_RWLOCK_READER_SHIFT) == 0 ? 0
Packit Service 0f798c
   553               : PTHREAD_RWLOCK_WRPHASE))))
Packit Service 0f798c
   554     {
Packit Service 0f798c
   ...
Packit Service 0f798c
   556     }
Packit Service 0f798c
Packit Service 0f798c
   ... leaving it in the write phase with zero readers
Packit Service 0f798c
   (the case where we leave the write phase in place
Packit Service 0f798c
   during a write unlock).
Packit Service 0f798c
Packit Service 0f798c
   A write trylock begins to execute.
Packit Service 0f798c
Packit Service 0f798c
   In __pthread_rwlock_trywrlock:
Packit Service 0f798c
Packit Service 0f798c
    40   while (((r & PTHREAD_RWLOCK_WRLOCKED) == 0)
Packit Service 0f798c
    41       && (((r >> PTHREAD_RWLOCK_READER_SHIFT) == 0)
Packit Service 0f798c
    42           || (prefer_writer && ((r & PTHREAD_RWLOCK_WRPHASE) != 0))))
Packit Service 0f798c
    43     {
Packit Service 0f798c
Packit Service 0f798c
   The lock is not locked.
Packit Service 0f798c
Packit Service 0f798c
   There are no readers.
Packit Service 0f798c
Packit Service 0f798c
    45       if (atomic_compare_exchange_weak_acquire (
Packit Service 0f798c
    46           &rwlock->__data.__readers, &r,
Packit Service 0f798c
    47           r | PTHREAD_RWLOCK_WRPHASE | PTHREAD_RWLOCK_WRLOCKED))
Packit Service 0f798c
Packit Service 0f798c
   We atomically install the write phase and we take the
Packit Service 0f798c
   exclusive write lock.
Packit Service 0f798c
Packit Service 0f798c
    48         {
Packit Service 0f798c
    49           atomic_store_relaxed (&rwlock->__data.__writers_futex, 1);
Packit Service 0f798c
Packit Service 0f798c
   We get this far.
Packit Service 0f798c
Packit Service 0f798c
   A reader lock begins to execute.
Packit Service 0f798c
Packit Service 0f798c
   In pthread_rwlock_rdlock:
Packit Service 0f798c
Packit Service 0f798c
   437   for (;;)
Packit Service 0f798c
   438     {
Packit Service 0f798c
   439       while (((wpf = atomic_load_relaxed (&rwlock->__data.__wrphase_futex))
Packit Service 0f798c
   440               | PTHREAD_RWLOCK_FUTEX_USED) == (1 | PTHREAD_RWLOCK_FUTEX_USED))
Packit Service 0f798c
   441         {
Packit Service 0f798c
   442           int private = __pthread_rwlock_get_private (rwlock);
Packit Service 0f798c
   443           if (((wpf & PTHREAD_RWLOCK_FUTEX_USED) == 0)
Packit Service 0f798c
   444               && (!atomic_compare_exchange_weak_relaxed
Packit Service 0f798c
   445                   (&rwlock->__data.__wrphase_futex,
Packit Service 0f798c
   446                    &wpf, wpf | PTHREAD_RWLOCK_FUTEX_USED)))
Packit Service 0f798c
   447             continue;
Packit Service 0f798c
   448           int err = futex_abstimed_wait (&rwlock->__data.__wrphase_futex,
Packit Service 0f798c
   449                                          1 | PTHREAD_RWLOCK_FUTEX_USED,
Packit Service 0f798c
   450                                          abstime, private);
Packit Service 0f798c
Packit Service 0f798c
   We are in a write phase, so the while() on line 439 is true.
Packit Service 0f798c
Packit Service 0f798c
   The value of wpf does not have PTHREAD_RWLOCK_FUTEX_USED set
Packit Service 0f798c
   since this is the first reader to lock.
Packit Service 0f798c
Packit Service 0f798c
   The atomic operation sets wpf with PTHREAD_RELOCK_FUTEX_USED
Packit Service 0f798c
   on the expectation that this reader will be woken during
Packit Service 0f798c
   the handoff.
Packit Service 0f798c
Packit Service 0f798c
   Back in pthread_rwlock_trywrlock:
Packit Service 0f798c
Packit Service 0f798c
    50           atomic_store_relaxed (&rwlock->__data.__wrphase_futex, 1);
Packit Service 0f798c
    51           atomic_store_relaxed (&rwlock->__data.__cur_writer,
Packit Service 0f798c
    52               THREAD_GETMEM (THREAD_SELF, tid));
Packit Service 0f798c
    53           return 0;
Packit Service 0f798c
    54         }
Packit Service 0f798c
   ...
Packit Service 0f798c
    57     }
Packit Service 0f798c
Packit Service 0f798c
   We write 1 to __wrphase_futex discarding PTHREAD_RWLOCK_FUTEX_USED,
Packit Service 0f798c
   and so in the unlock we will not awaken the waiting reader.
Packit Service 0f798c
Packit Service 0f798c
   The solution to this is to realize that if we did not start the write
Packit Service 0f798c
   phase we need not write 1 or any other value to __wrphase_futex.
Packit Service 0f798c
   This ensures that any readers (which saw __wrphase_futex != 0) can
Packit Service 0f798c
   set PTHREAD_RWLOCK_FUTEX_USED and this can be used at unlock to
Packit Service 0f798c
   wake them.
Packit Service 0f798c
Packit Service 0f798c
   If we installed the write phase then all other readers are looping
Packit Service 0f798c
   here:
Packit Service 0f798c
Packit Service 0f798c
   In __pthread_rwlock_rdlock_full:
Packit Service 0f798c
Packit Service 0f798c
   437   for (;;)
Packit Service 0f798c
   438     {
Packit Service 0f798c
   439       while (((wpf = atomic_load_relaxed (&rwlock->__data.__wrphase_futex))
Packit Service 0f798c
   440               | PTHREAD_RWLOCK_FUTEX_USED) == (1 | PTHREAD_RWLOCK_FUTEX_USED))
Packit Service 0f798c
   441         {
Packit Service 0f798c
   ...
Packit Service 0f798c
   508     }
Packit Service 0f798c
Packit Service 0f798c
   waiting for the write phase to be installed or removed before they
Packit Service 0f798c
   can begin waiting on __wrphase_futex (part of the algorithm), or
Packit Service 0f798c
   taking a concurrent read lock, and thus we can safely write 1 to
Packit Service 0f798c
   __wrphase_futex.
Packit Service 0f798c
Packit Service 0f798c
   If we did not install the write phase then the readers may already
Packit Service 0f798c
   be waiting on the futex, the original writer wrote 1 to __wrphase_futex
Packit Service 0f798c
   as part of starting the write phase, and we cannot also write 1
Packit Service 0f798c
   without loosing the PTHREAD_RWLOCK_FUTEX_USED bit.
Packit Service 0f798c
Packit Service 0f798c
   ---
Packit Service 0f798c
Packit Service 0f798c
   Summary for the pthread_rwlock_tryrdlock() stall:
Packit Service 0f798c
Packit Service 0f798c
   The stall is caused by pthread_rwlock_tryrdlock failing to check
Packit Service 0f798c
   that PTHREAD_RWLOCK_FUTEX_USED is set in the __wrphase_futex futex
Packit Service 0f798c
   and then waking the futex.
Packit Service 0f798c
Packit Service 0f798c
   The fix for bug 23844 ensures that waiters on __wrphase_futex are
Packit Service 0f798c
   correctly woken.  Before the fix the test stalls as readers can
Packit Service 0f798c
   wait forever on __wrphase_futex.  */
Packit Service 0f798c
Packit Service 0f798c
#include <stdio.h>
Packit Service 0f798c
#include <stdlib.h>
Packit Service 0f798c
#include <unistd.h>
Packit Service 0f798c
#include <pthread.h>
Packit Service 0f798c
#include <support/xthread.h>
Packit Service 0f798c
#include <errno.h>
Packit Service 0f798c
Packit Service 0f798c
/* We need only one lock to reproduce the issue. We will need multiple
Packit Service 0f798c
   threads to get the exact case where we have a read, try, and unlock
Packit Service 0f798c
   all interleaving to produce the case where the readers are waiting
Packit Service 0f798c
   and the try fails to wake them.  */
Packit Service 0f798c
pthread_rwlock_t onelock;
Packit Service 0f798c
Packit Service 0f798c
/* The number of threads is arbitrary but empirically chosen to have
Packit Service 0f798c
   enough threads that we see the condition where waiting readers are
Packit Service 0f798c
   not woken by a successful tryrdlock.  */
Packit Service 0f798c
#define NTHREADS 32
Packit Service 0f798c
Packit Service 0f798c
_Atomic int do_exit;
Packit Service 0f798c
Packit Service 0f798c
void *
Packit Service 0f798c
run_loop (void *arg)
Packit Service 0f798c
{
Packit Service 0f798c
  int i = 0, ret;
Packit Service 0f798c
  while (!do_exit)
Packit Service 0f798c
    {
Packit Service 0f798c
      /* Arbitrarily choose if we are the writer or reader.  Choose a
Packit Service 0f798c
	 high enough ratio of readers to writers to make it likely
Packit Service 0f798c
	 that readers block (and eventually are susceptable to
Packit Service 0f798c
	 stalling).
Packit Service 0f798c
Packit Service 0f798c
         If we are a writer, take the write lock, and then unlock.
Packit Service 0f798c
	 If we are a reader, try the lock, then lock, then unlock.  */
Packit Service 0f798c
      if ((i % 8) != 0)
Packit Service 0f798c
	xpthread_rwlock_wrlock (&onelock);
Packit Service 0f798c
      else
Packit Service 0f798c
	{
Packit Service 0f798c
	  if ((ret = pthread_rwlock_tryrdlock (&onelock)) != 0)
Packit Service 0f798c
	    {
Packit Service 0f798c
	      if (ret == EBUSY)
Packit Service 0f798c
		xpthread_rwlock_rdlock (&onelock);
Packit Service 0f798c
	      else
Packit Service 0f798c
		exit (EXIT_FAILURE);
Packit Service 0f798c
	    }
Packit Service 0f798c
	}
Packit Service 0f798c
      /* Thread does some work and then unlocks.  */
Packit Service 0f798c
      xpthread_rwlock_unlock (&onelock);
Packit Service 0f798c
      i++;
Packit Service 0f798c
    }
Packit Service 0f798c
  return NULL;
Packit Service 0f798c
}
Packit Service 0f798c
Packit Service 0f798c
int
Packit Service 0f798c
do_test (void)
Packit Service 0f798c
{
Packit Service 0f798c
  int i;
Packit Service 0f798c
  pthread_t tids[NTHREADS];
Packit Service 0f798c
  xpthread_rwlock_init (&onelock, NULL);
Packit Service 0f798c
  for (i = 0; i < NTHREADS; i++)
Packit Service 0f798c
    tids[i] = xpthread_create (NULL, run_loop, NULL);
Packit Service 0f798c
  /* Run for some amount of time.  Empirically speaking exercising
Packit Service 0f798c
     the stall via pthread_rwlock_tryrdlock is much harder, and on
Packit Service 0f798c
     a 3.5GHz 4 core x86_64 VM system it takes somewhere around
Packit Service 0f798c
     20-200s to stall, approaching 100% stall past 200s.  We can't
Packit Service 0f798c
     wait that long for a regression test so we just test for 20s,
Packit Service 0f798c
     and expect the stall to happen with a 5-10% chance (enough for
Packit Service 0f798c
     developers to see).  */
Packit Service 0f798c
  sleep (20);
Packit Service 0f798c
  /* Then exit.  */
Packit Service 0f798c
  printf ("INFO: Exiting...\n");
Packit Service 0f798c
  do_exit = 1;
Packit Service 0f798c
  /* If any readers stalled then we will timeout waiting for them.  */
Packit Service 0f798c
  for (i = 0; i < NTHREADS; i++)
Packit Service 0f798c
    xpthread_join (tids[i]);
Packit Service 0f798c
  printf ("INFO: Done.\n");
Packit Service 0f798c
  xpthread_rwlock_destroy (&onelock);
Packit Service 0f798c
  printf ("PASS: No pthread_rwlock_tryrdlock stalls detected.\n");
Packit Service 0f798c
  return 0;
Packit Service 0f798c
}
Packit Service 0f798c
Packit Service 0f798c
#define TIMEOUT 30
Packit Service 0f798c
#include <support/test-driver.c>