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