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