50f89d
commit f21e8f8ca466320fed38bdb71526c574dae98026
50f89d
Author: Andreas Schwab <schwab@suse.de>
50f89d
Date:   Thu Nov 8 14:28:22 2018 +0100
50f89d
50f89d
    Fix rwlock stall with PREFER_WRITER_NONRECURSIVE_NP (bug 23861)
50f89d
    
50f89d
    In the read lock function (__pthread_rwlock_rdlock_full) there was a
50f89d
    code path which would fail to reload __readers while waiting for
50f89d
    PTHREAD_RWLOCK_RWAITING to change. This failure to reload __readers
50f89d
    into a local value meant that various conditionals used the old value
50f89d
    of __readers and with only two threads left it could result in an
50f89d
    indefinite stall of one of the readers (waiting for PTHREAD_RWLOCK_RWAITING
50f89d
    to go to zero, but it never would).
50f89d
50f89d
diff --git a/nptl/Makefile b/nptl/Makefile
50f89d
index ee720960d18f33d1..2d2db648f730db61 100644
50f89d
--- a/nptl/Makefile
50f89d
+++ b/nptl/Makefile
50f89d
@@ -318,7 +318,8 @@ tests = tst-attr1 tst-attr2 tst-attr3 tst-default-attr \
50f89d
 	tst-minstack-throw \
50f89d
 	tst-cnd-basic tst-mtx-trylock tst-cnd-broadcast \
50f89d
 	tst-cnd-timedwait tst-thrd-detach tst-mtx-basic tst-thrd-sleep \
50f89d
-	tst-mtx-recursive tst-tss-basic tst-call-once tst-mtx-timedlock
50f89d
+	tst-mtx-recursive tst-tss-basic tst-call-once tst-mtx-timedlock \
50f89d
+	tst-rwlock-pwn
50f89d
 
50f89d
 tests-internal := tst-rwlock19 tst-rwlock20 \
50f89d
 		  tst-sem11 tst-sem12 tst-sem13 \
50f89d
diff --git a/nptl/pthread_rwlock_common.c b/nptl/pthread_rwlock_common.c
50f89d
index 5dd534271aed6b41..85fc1bcfc7f5e60d 100644
50f89d
--- a/nptl/pthread_rwlock_common.c
50f89d
+++ b/nptl/pthread_rwlock_common.c
50f89d
@@ -314,7 +314,7 @@ __pthread_rwlock_rdlock_full (pthread_rwlock_t *rwlock,
50f89d
 		 harmless because the flag is just about the state of
50f89d
 		 __readers, and all threads set the flag under the same
50f89d
 		 conditions.  */
50f89d
-	      while ((atomic_load_relaxed (&rwlock->__data.__readers)
50f89d
+	      while (((r = atomic_load_relaxed (&rwlock->__data.__readers))
50f89d
 		      & PTHREAD_RWLOCK_RWAITING) != 0)
50f89d
 		{
50f89d
 		  int private = __pthread_rwlock_get_private (rwlock);
50f89d
diff --git a/nptl/tst-rwlock-pwn.c b/nptl/tst-rwlock-pwn.c
50f89d
new file mode 100644
50f89d
index 0000000000000000..c39dd70973f1a76e
50f89d
--- /dev/null
50f89d
+++ b/nptl/tst-rwlock-pwn.c
50f89d
@@ -0,0 +1,87 @@
50f89d
+/* Test rwlock with PREFER_WRITER_NONRECURSIVE_NP (bug 23861).
50f89d
+   Copyright (C) 2018 Free Software Foundation, Inc.
50f89d
+   This file is part of the GNU C Library.
50f89d
+
50f89d
+   The GNU C Library is free software; you can redistribute it and/or
50f89d
+   modify it under the terms of the GNU Lesser General Public
50f89d
+   License as published by the Free Software Foundation; either
50f89d
+   version 2.1 of the License, or (at your option) any later version.
50f89d
+
50f89d
+   The GNU C Library is distributed in the hope that it will be useful,
50f89d
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
50f89d
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
50f89d
+   Lesser General Public License for more details.
50f89d
+
50f89d
+   You should have received a copy of the GNU Lesser General Public
50f89d
+   License along with the GNU C Library; if not, see
50f89d
+   <http://www.gnu.org/licenses/>.  */
50f89d
+
50f89d
+#include <stdio.h>
50f89d
+#include <stdlib.h>
50f89d
+#include <unistd.h>
50f89d
+#include <pthread.h>
50f89d
+#include <support/xthread.h>
50f89d
+
50f89d
+/* We choose 10 iterations because this happens to be able to trigger the
50f89d
+   stall on contemporary hardware.  */
50f89d
+#define LOOPS 10
50f89d
+/* We need 3 threads to trigger bug 23861.  One thread as a writer, and
50f89d
+   two reader threads.  The test verifies that the second-to-last reader
50f89d
+   is able to notify the *last* reader that it should be done waiting.
50f89d
+   If the second-to-last reader fails to notify the last reader or does
50f89d
+   so incorrectly then the last reader may stall indefinitely.  */
50f89d
+#define NTHREADS 3
50f89d
+
50f89d
+_Atomic int do_exit;
50f89d
+pthread_rwlockattr_t mylock_attr;
50f89d
+pthread_rwlock_t mylock;
50f89d
+
50f89d
+void *
50f89d
+run_loop (void *a)
50f89d
+{
50f89d
+  while (!do_exit)
50f89d
+    {
50f89d
+      if (random () & 1)
50f89d
+	{
50f89d
+	  xpthread_rwlock_wrlock (&mylock);
50f89d
+	  xpthread_rwlock_unlock (&mylock);
50f89d
+	}
50f89d
+      else
50f89d
+	{
50f89d
+	  xpthread_rwlock_rdlock (&mylock);
50f89d
+	  xpthread_rwlock_unlock (&mylock);
50f89d
+	}
50f89d
+    }
50f89d
+  return NULL;
50f89d
+}
50f89d
+
50f89d
+int
50f89d
+do_test (void)
50f89d
+{
50f89d
+  xpthread_rwlockattr_init (&mylock_attr);
50f89d
+  xpthread_rwlockattr_setkind_np (&mylock_attr,
50f89d
+				  PTHREAD_RWLOCK_PREFER_WRITER_NONRECURSIVE_NP);
50f89d
+  xpthread_rwlock_init (&mylock, &mylock_attr);
50f89d
+
50f89d
+  for (int n = 0; n < LOOPS; n++)
50f89d
+    {
50f89d
+      pthread_t tids[NTHREADS];
50f89d
+      do_exit = 0;
50f89d
+      for (int i = 0; i < NTHREADS; i++)
50f89d
+	tids[i] = xpthread_create (NULL, run_loop, NULL);
50f89d
+      /* Let the threads run for some time.  */
50f89d
+      sleep (1);
50f89d
+      printf ("Exiting...");
50f89d
+      fflush (stdout);
50f89d
+      do_exit = 1;
50f89d
+      for (int i = 0; i < NTHREADS; i++)
50f89d
+	xpthread_join (tids[i]);
50f89d
+      printf ("done.\n");
50f89d
+    }
50f89d
+  pthread_rwlock_destroy (&mylock);
50f89d
+  pthread_rwlockattr_destroy (&mylock_attr);
50f89d
+  return 0;
50f89d
+}
50f89d
+
50f89d
+#define TIMEOUT (DEFAULT_TIMEOUT + 3 * LOOPS)
50f89d
+#include <support/test-driver.c>