Blame SOURCES/gcc48-rh1491395.patch

4dd737
2016-01-16  Torvald Riegel  <triegel@redhat.com>
4dd737
4dd737
	* method-gl.cc (gl_wt_dispatch::trycommit): Ensure proxy privatization
4dd737
	safety.
4dd737
	* method-ml.cc (ml_wt_dispatch::trycommit): Likewise.
4dd737
	* libitm/testsuite/libitm.c/priv-1.c: New.
4dd737
4dd737
--- libitm/method-gl.cc
4dd737
+++ libitm/method-gl.cc
4dd737
@@ -291,12 +291,18 @@ public:
4dd737
         // See begin_or_restart() for why we need release memory order here.
4dd737
 	v = gl_mg::clear_locked(v) + 1;
4dd737
 	o_gl_mg.orec.store(v, memory_order_release);
4dd737
-
4dd737
-	// Need to ensure privatization safety. Every other transaction must
4dd737
-	// have a snapshot time that is at least as high as our commit time
4dd737
-	// (i.e., our commit must be visible to them).
4dd737
-	priv_time = v;
4dd737
       }
4dd737
+
4dd737
+    // Need to ensure privatization safety. Every other transaction must have
4dd737
+    // a snapshot time that is at least as high as our commit time (i.e., our
4dd737
+    // commit must be visible to them).  Because of proxy privatization, we
4dd737
+    // must ensure that even if we are a read-only transaction.  See
4dd737
+    // ml_wt_dispatch::trycommit() for details: We can't get quite the same
4dd737
+    // set of problems because we just use one orec and thus, for example,
4dd737
+    // there cannot be concurrent writers -- but we can still get pending
4dd737
+    // loads to privatized data when not ensuring privatization safety, which
4dd737
+    // is problematic if the program unmaps the privatized memory.
4dd737
+    priv_time = v;
4dd737
     return true;
4dd737
   }
4dd737
 
4dd737
--- libitm/method-ml.cc
4dd737
+++ libitm/method-ml.cc
4dd737
@@ -513,6 +513,21 @@ public:
4dd737
     if (!tx->writelog.size())
4dd737
       {
4dd737
         tx->readlog.clear();
4dd737
+        // We still need to ensure privatization safety, unfortunately.  While
4dd737
+        // we cannot have privatized anything by ourselves (because we are not
4dd737
+        // an update transaction), we can have observed the commits of
4dd737
+        // another update transaction that privatized something.  Because any
4dd737
+        // commit happens before ensuring privatization, our snapshot and
4dd737
+        // commit can thus have happened before ensuring privatization safety
4dd737
+        // for this commit/snapshot time.  Therefore, before we can return to
4dd737
+        // nontransactional code that might use the privatized data, we must
4dd737
+        // ensure privatization safety for our snapshot time.
4dd737
+        // This still seems to be better than not allowing use of the
4dd737
+        // snapshot time before privatization safety has been ensured because
4dd737
+        // we at least can run transactions such as this one, and in the
4dd737
+        // meantime the transaction producing this commit time might have
4dd737
+        // finished ensuring privatization safety for it.
4dd737
+        priv_time = tx->shared_state.load(memory_order_relaxed);
4dd737
         return true;
4dd737
       }
4dd737
 
4dd737
--- /dev/null
4dd737
+++ libitm/testsuite/libitm.c/priv-1.c
4dd737
@@ -0,0 +1,117 @@
4dd737
+/* Quick stress test for proxy privatization.  */
4dd737
+
4dd737
+/* We need to use a TM method that has to enforce privatization safety
4dd737
+   explicitly.  */
4dd737
+/* { dg-set-target-env-var ITM_DEFAULT_METHOD "ml_wt" } */
4dd737
+/* { dg-options "-std=gnu11" } */
4dd737
+
4dd737
+#include <stdlib.h>
4dd737
+#include <stdio.h>
4dd737
+#include <pthread.h>
4dd737
+
4dd737
+/* Make them likely to be mapped to different orecs.  */
4dd737
+#define ALIGN __attribute__((aligned (256)))
4dd737
+/* Don't make these static to work around PR 68591.  */
4dd737
+int x ALIGN;
4dd737
+int *ptr ALIGN;
4dd737
+int *priv_ptr ALIGN;
4dd737
+int priv_value ALIGN;
4dd737
+int barrier ALIGN = 0;
4dd737
+const int iters = 100;
4dd737
+
4dd737
+static void arrive_and_wait (int expected_value)
4dd737
+{
4dd737
+  int now = __atomic_add_fetch (&barrier, 1, __ATOMIC_ACQ_REL);
4dd737
+  while (now < expected_value)
4dd737
+    __atomic_load (&barrier, &now, __ATOMIC_ACQUIRE);
4dd737
+}
4dd737
+
4dd737
+static void __attribute__((transaction_pure,noinline)) delay (int i)
4dd737
+{
4dd737
+  for (volatile int v = 0; v < i; v++);
4dd737
+}
4dd737
+
4dd737
+/* This tries to catch a case in which proxy privatization safety is not
4dd737
+   ensured by privatization_user.  Specifically, it's access to the value
4dd737
+   of it's transactional snapshot of ptr must read from an uncommitted write
4dd737
+   by writer; thus, writer must still be active but must have read ptr before
4dd737
+   proxy can privatize *ptr by assigning to ptr.
4dd737
+   We try to make this interleaving more likely by delaying the commit of
4dd737
+   writer and the start of proxy.  */
4dd737
+static void *writer (void *dummy __attribute__((unused)))
4dd737
+{
4dd737
+  for (int i = 0; i < iters; i++)
4dd737
+    {
4dd737
+      /* Initialize state in each round.  */
4dd737
+      x = 0;
4dd737
+      ptr = &x;
4dd737
+      priv_ptr = NULL;
4dd737
+      int wrote = 1;
4dd737
+      arrive_and_wait (i * 6 + 3);
4dd737
+      /* Interference by another writer.  Has a conflict with the proxy
4dd737
+	 privatizer.  */
4dd737
+      __transaction_atomic
4dd737
+	{
4dd737
+	  if (ptr != NULL)
4dd737
+	    *ptr = 1;
4dd737
+	  else
4dd737
+	    wrote = 0;
4dd737
+	  delay (2000000);
4dd737
+	}
4dd737
+      arrive_and_wait (i * 6 + 6);
4dd737
+      /* If the previous transaction committed first, wrote == 1 and x == 1;
4dd737
+	 otherwise, if the proxy came first, wrote == 0 and priv_value == 0.
4dd737
+       */
4dd737
+      if (wrote != priv_value)
4dd737
+	abort ();
4dd737
+    }
4dd737
+  return NULL;
4dd737
+}
4dd737
+
4dd737
+static void *proxy (void *dummy __attribute__((unused)))
4dd737
+{
4dd737
+  for (int i = 0; i < iters; i++)
4dd737
+    {
4dd737
+      arrive_and_wait (i * 6 + 3);
4dd737
+      delay(1000000);
4dd737
+      __transaction_atomic
4dd737
+	{
4dd737
+	  /* Hand-off to privatization-user and its read-only transaction and
4dd737
+	     subsequent use of privatization.  */
4dd737
+	  priv_ptr = ptr;
4dd737
+	  ptr = NULL;
4dd737
+	}
4dd737
+      arrive_and_wait (i * 6 + 6);
4dd737
+    }
4dd737
+  return NULL;
4dd737
+}
4dd737
+
4dd737
+static void *privatization_user (void *dummy __attribute__((unused)))
4dd737
+{
4dd737
+  for (int i = 0; i < iters; i++)
4dd737
+    {
4dd737
+      arrive_and_wait (i * 6 + 3);
4dd737
+      /* Spin until we have gotten a pointer from the proxy.  Then access
4dd737
+	 the value pointed to nontransactionally.  */
4dd737
+      int *p = NULL;
4dd737
+      while (p == NULL)
4dd737
+	__transaction_atomic { p = priv_ptr; }
4dd737
+      priv_value = *p;
4dd737
+      arrive_and_wait (i * 6 + 6);
4dd737
+    }
4dd737
+  return NULL;
4dd737
+}
4dd737
+
4dd737
+int main()
4dd737
+{
4dd737
+  pthread_t p[3];
4dd737
+
4dd737
+  pthread_create (p+0, NULL, writer, NULL);
4dd737
+  pthread_create (p+1, NULL, proxy, NULL);
4dd737
+  pthread_create (p+2, NULL, privatization_user, NULL);
4dd737
+
4dd737
+  for (int i = 0; i < 3; ++i)
4dd737
+    pthread_join  (p[i], NULL);
4dd737
+
4dd737
+  return 0;
4dd737
+}