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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
|
// Copyright 2020 The gVisor Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include <errno.h>
#include <signal.h>
#include <sys/syscall.h>
#include <sys/types.h>
#include <unistd.h>
#include <atomic>
#include "absl/time/clock.h"
#include "absl/time/time.h"
#include "test/util/cleanup.h"
#include "test/util/logging.h"
#include "test/util/memory_util.h"
#include "test/util/posix_error.h"
#include "test/util/test_util.h"
#include "test/util/thread_util.h"
namespace gvisor {
namespace testing {
namespace {
// This is the classic test case for memory fences on architectures with total
// store ordering; see e.g. Intel SDM Vol. 3A Sec. 8.2.3.4 "Loads May Be
// Reordered with Earlier Stores to Different Locations". In each iteration of
// the test, given two variables X and Y initially set to 0
// (MembarrierTestSharedState::local_var and remote_var in the code), two
// threads execute as follows:
//
// T1 T2
// -- --
//
// X = 1 Y = 1
// T1fence() T2fence()
// read Y read X
//
// On architectures where memory writes may be locally buffered by each CPU
// (essentially all architectures), if T1fence() and T2fence() are omitted or
// ineffective, it is possible for both T1 and T2 to read 0 because the memory
// write from the other CPU is not yet visible outside that CPU. T1fence() and
// T2fence() are expected to perform the necessary synchronization to restore
// sequential consistency: both threads agree on a order of memory accesses that
// is consistent with program order in each thread, such that at least one
// thread reads 1.
//
// In the NoMembarrier test, T1fence() and T2fence() are both ordinary memory
// fences establishing ordering between memory accesses before and after the
// fence (std::atomic_thread_fence). In all other test cases, T1fence() is not a
// memory fence at all, but only prevents compiler reordering of memory accesses
// (std::atomic_signal_fence); T2fence() is an invocation of the membarrier()
// syscall, which establishes ordering of memory accesses before and after the
// syscall on both threads.
template <typename F>
int DoMembarrierTestSide(std::atomic<int>* our_var,
std::atomic<int> const& their_var,
F const& test_fence) {
our_var->store(1, std::memory_order_relaxed);
test_fence();
return their_var.load(std::memory_order_relaxed);
}
struct MembarrierTestSharedState {
std::atomic<int64_t> remote_iter_cur;
std::atomic<int64_t> remote_iter_done;
std::atomic<int> local_var;
std::atomic<int> remote_var;
int remote_obs_of_local_var;
void Init() {
remote_iter_cur.store(-1, std::memory_order_relaxed);
remote_iter_done.store(-1, std::memory_order_relaxed);
}
};
// Special value for MembarrierTestSharedState::remote_iter_cur indicating that
// the remote thread should terminate.
constexpr int64_t kRemoteIterStop = -2;
// Must be async-signal-safe.
template <typename F>
void RunMembarrierTestRemoteSide(MembarrierTestSharedState* state,
F const& test_fence) {
int64_t i = 0;
int64_t cur;
while (true) {
while ((cur = state->remote_iter_cur.load(std::memory_order_acquire)) < i) {
if (cur == kRemoteIterStop) {
return;
}
// spin
}
state->remote_obs_of_local_var =
DoMembarrierTestSide(&state->remote_var, state->local_var, test_fence);
state->remote_iter_done.store(i, std::memory_order_release);
i++;
}
}
template <typename F>
void RunMembarrierTestLocalSide(MembarrierTestSharedState* state,
F const& test_fence) {
// On test completion, instruct the remote thread to terminate.
Cleanup cleanup_remote([&] {
state->remote_iter_cur.store(kRemoteIterStop, std::memory_order_relaxed);
});
int64_t i = 0;
absl::Time end = absl::Now() + absl::Seconds(5); // arbitrary test duration
while (absl::Now() < end) {
// Reset both vars to 0.
state->local_var.store(0, std::memory_order_relaxed);
state->remote_var.store(0, std::memory_order_relaxed);
// Instruct the remote thread to begin this iteration.
state->remote_iter_cur.store(i, std::memory_order_release);
// Perform our side of the test.
auto local_obs_of_remote_var =
DoMembarrierTestSide(&state->local_var, state->remote_var, test_fence);
// Wait for the remote thread to finish this iteration.
while (state->remote_iter_done.load(std::memory_order_acquire) < i) {
// spin
}
ASSERT_TRUE(local_obs_of_remote_var != 0 ||
state->remote_obs_of_local_var != 0);
i++;
}
}
TEST(MembarrierTest, NoMembarrier) {
MembarrierTestSharedState state;
state.Init();
ScopedThread remote_thread([&] {
RunMembarrierTestRemoteSide(
&state, [] { std::atomic_thread_fence(std::memory_order_seq_cst); });
});
RunMembarrierTestLocalSide(
&state, [] { std::atomic_thread_fence(std::memory_order_seq_cst); });
}
enum membarrier_cmd {
MEMBARRIER_CMD_QUERY = 0,
MEMBARRIER_CMD_GLOBAL = (1 << 0),
MEMBARRIER_CMD_GLOBAL_EXPEDITED = (1 << 1),
MEMBARRIER_CMD_REGISTER_GLOBAL_EXPEDITED = (1 << 2),
MEMBARRIER_CMD_PRIVATE_EXPEDITED = (1 << 3),
MEMBARRIER_CMD_REGISTER_PRIVATE_EXPEDITED = (1 << 4),
};
int membarrier(membarrier_cmd cmd, int flags) {
return syscall(SYS_membarrier, cmd, flags);
}
PosixErrorOr<int> SupportedMembarrierCommands() {
int cmds = membarrier(MEMBARRIER_CMD_QUERY, 0);
if (cmds < 0) {
if (errno == ENOSYS) {
// No commands are supported.
return 0;
}
return PosixError(errno, "membarrier(MEMBARRIER_CMD_QUERY) failed");
}
return cmds;
}
TEST(MembarrierTest, Global) {
SKIP_IF((ASSERT_NO_ERRNO_AND_VALUE(SupportedMembarrierCommands()) &
MEMBARRIER_CMD_GLOBAL) == 0);
Mapping m = ASSERT_NO_ERRNO_AND_VALUE(
MmapAnon(kPageSize, PROT_READ | PROT_WRITE, MAP_SHARED));
auto state = static_cast<MembarrierTestSharedState*>(m.ptr());
state->Init();
pid_t const child_pid = fork();
if (child_pid == 0) {
// In child process.
RunMembarrierTestRemoteSide(
state, [] { TEST_PCHECK(membarrier(MEMBARRIER_CMD_GLOBAL, 0) == 0); });
_exit(0);
}
// In parent process.
ASSERT_THAT(child_pid, SyscallSucceeds());
Cleanup cleanup_child([&] {
int status;
ASSERT_THAT(waitpid(child_pid, &status, 0),
SyscallSucceedsWithValue(child_pid));
EXPECT_TRUE(WIFEXITED(status) && WEXITSTATUS(status) == 0)
<< " status " << status;
});
RunMembarrierTestLocalSide(
state, [] { std::atomic_signal_fence(std::memory_order_seq_cst); });
}
TEST(MembarrierTest, GlobalExpedited) {
constexpr int kRequiredCommands = MEMBARRIER_CMD_GLOBAL_EXPEDITED |
MEMBARRIER_CMD_REGISTER_GLOBAL_EXPEDITED;
SKIP_IF((ASSERT_NO_ERRNO_AND_VALUE(SupportedMembarrierCommands()) &
kRequiredCommands) != kRequiredCommands);
ASSERT_THAT(membarrier(MEMBARRIER_CMD_REGISTER_GLOBAL_EXPEDITED, 0),
SyscallSucceeds());
Mapping m = ASSERT_NO_ERRNO_AND_VALUE(
MmapAnon(kPageSize, PROT_READ | PROT_WRITE, MAP_SHARED));
auto state = static_cast<MembarrierTestSharedState*>(m.ptr());
state->Init();
pid_t const child_pid = fork();
if (child_pid == 0) {
// In child process.
RunMembarrierTestRemoteSide(state, [] {
TEST_PCHECK(membarrier(MEMBARRIER_CMD_GLOBAL_EXPEDITED, 0) == 0);
});
_exit(0);
}
// In parent process.
ASSERT_THAT(child_pid, SyscallSucceeds());
Cleanup cleanup_child([&] {
int status;
ASSERT_THAT(waitpid(child_pid, &status, 0),
SyscallSucceedsWithValue(child_pid));
EXPECT_TRUE(WIFEXITED(status) && WEXITSTATUS(status) == 0)
<< " status " << status;
});
RunMembarrierTestLocalSide(
state, [] { std::atomic_signal_fence(std::memory_order_seq_cst); });
}
TEST(MembarrierTest, PrivateExpedited) {
constexpr int kRequiredCommands = MEMBARRIER_CMD_PRIVATE_EXPEDITED |
MEMBARRIER_CMD_REGISTER_PRIVATE_EXPEDITED;
SKIP_IF((ASSERT_NO_ERRNO_AND_VALUE(SupportedMembarrierCommands()) &
kRequiredCommands) != kRequiredCommands);
ASSERT_THAT(membarrier(MEMBARRIER_CMD_REGISTER_PRIVATE_EXPEDITED, 0),
SyscallSucceeds());
MembarrierTestSharedState state;
state.Init();
ScopedThread remote_thread([&] {
RunMembarrierTestRemoteSide(&state, [] {
TEST_PCHECK(membarrier(MEMBARRIER_CMD_PRIVATE_EXPEDITED, 0) == 0);
});
});
RunMembarrierTestLocalSide(
&state, [] { std::atomic_signal_fence(std::memory_order_seq_cst); });
}
} // namespace
} // namespace testing
} // namespace gvisor
|