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
|
/* $NetBSD: t_seqlock.c,v 1.2 2022/04/10 11:36:32 riastradh Exp $ */
/*-
* Copyright (c) 2022 The NetBSD Foundation, Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#include <sys/cdefs.h>
__RCSID("$NetBSD: t_seqlock.c,v 1.2 2022/04/10 11:36:32 riastradh Exp $");
#include <sys/atomic.h>
#include <sys/param.h>
#include <sys/sysctl.h>
#include <assert.h>
#include <atf-c.h>
#include <err.h>
#include <errno.h>
#include <inttypes.h>
#include <pthread.h>
#include <stdint.h>
#include <stdio.h>
#include <unistd.h>
#ifdef BROKEN_PRODUCER
#undef membar_producer
#define membar_producer() asm volatile("" ::: "memory")
#endif /* BROKEN_PRODUCER */
#ifdef BROKEN_CONSUMER
#undef membar_consumer
#define membar_consumer() asm volatile("" ::: "memory")
#endif /* BROKEN_CONSUMER */
volatile sig_atomic_t times_up;
volatile unsigned version;
volatile struct {
uint64_t s;
} __aligned(COHERENCY_UNIT) stats[16];
uint64_t results[2];
static void *
writer(void *cookie)
{
uint64_t s;
unsigned i;
for (s = 0; !times_up; s++) {
version |= 1;
membar_producer();
for (i = __arraycount(stats); i --> 0;)
stats[i].s = s;
membar_producer();
version |= 1;
version += 1;
/*
* Not needed for correctness, but this helps on Cavium
* Octeon cnMIPS CPUs which require issuing a sync
* plunger to unclog store buffers which can otherwise
* stay clogged for hundreds of thousands of cycles,
* giving very little concurrency to this test.
* Without this, the reader spends most of its time
* thinking an update is in progress.
*/
membar_producer();
}
return NULL;
}
static void *
reader(void *cookie)
{
uint64_t s;
unsigned v, result, i;
volatile unsigned *vp = &version;
volatile uint64_t t;
while (!times_up) {
/*
* Prime the cache with possibly stale garbage.
*/
t = stats[0].s;
/*
* Normally we would do
*
* while ((v = version) & 1)
* SPINLOCK_BACKOFF_HOOK;
*
* to avoid copying out a version that we know is in
* flux, but it's not wrong to copy out a version in
* flux -- just wasteful.
*
* Reading the version unconditionally, and then
* copying out the record, better exercises plausible
* bugs in PowerPC membars based on `isync' that
* provide the desired ordering only if separated from
* the load by a conditional branch based on the load.
*/
v = *vp;
membar_consumer();
s = stats[0].s;
for (result = 0, i = 1; i < __arraycount(stats); i++)
result |= (s != stats[i].s);
membar_consumer();
if ((v & ~1u) != *vp)
continue;
results[result]++;
}
(void)t;
return NULL;
}
ATF_TC(seqlock);
ATF_TC_HEAD(seqlock, tc)
{
atf_tc_set_md_var(tc, "descr",
"Verify membar_producer/consumer work for seqlocks");
}
ATF_TC_BODY(seqlock, tc)
{
pthread_t t[2];
void *(*start[2])(void *) = { &reader, &writer };
unsigned i;
int ncpu;
size_t ncpulen = sizeof(ncpu);
int error;
alarm(10);
if (sysctlbyname("hw.ncpu", &ncpu, &ncpulen, NULL, 0) == -1)
atf_tc_fail("hw.ncpu: (%d) %s", errno, strerror(errno));
assert(ncpulen == sizeof(ncpu));
if (ncpu == 1)
atf_tc_skip("membar tests are only for multicore systems");
for (i = 0; i < 2; i++) {
error = pthread_create(&t[i], NULL, start[i],
(void *)(uintptr_t)i);
if (error)
errc(1, error, "pthread_create");
}
sleep(5);
times_up = 1;
for (i = 0; i < 2; i++) {
error = pthread_join(t[i], NULL);
if (error)
errc(1, error, "pthread_join");
}
ATF_REQUIRE(results[0] != 0);
ATF_REQUIRE_MSG(results[1] == 0,
"%"PRIu64" good snapshots, %"PRIu64" bad snapshots",
results[0], results[1]);
}
ATF_TP_ADD_TCS(tp)
{
ATF_TP_ADD_TC(tp, seqlock);
return atf_no_error();
}
|