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

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