Blame SOURCES/gcc48-rh1491395.patch

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