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