Blame SOURCES/gcc48-rh1491395.patch

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