summaryrefslogtreecommitdiff
path: root/gcc48-rh1491395.patch
blob: f0a8bb421b2dd789beef5ff87efd9b1a411909e8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
2016-01-16  Torvald Riegel  <triegel@redhat.com>

	* method-gl.cc (gl_wt_dispatch::trycommit): Ensure proxy privatization
	safety.
	* method-ml.cc (ml_wt_dispatch::trycommit): Likewise.
	* libitm/testsuite/libitm.c/priv-1.c: New.

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