aboutsummaryrefslogtreecommitdiff
path: root/tools/uma/smrstress/smrstress.c
blob: 7e7ee4ea065ca07456fc0e4f1eeeb20acb69d93c (plain) (blame)
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
/*-
 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
 *
 * Copyright (c) 2020 Jeffrey Roberson <jeff@FreeBSD.org>
 *
 * 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 unmodified, 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 AUTHOR ``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 AUTHOR 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.
 *
 * $FreeBSD$
 *
 */
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/kernel.h>
#include <sys/libkern.h>
#include <sys/module.h>
#include <sys/mutex.h>
#include <sys/proc.h>
#include <sys/kthread.h>
#include <sys/conf.h>
#include <sys/mbuf.h>
#include <sys/smp.h>
#include <sys/smr.h>

#include <vm/uma.h>

#include <machine/stdarg.h>

static uma_zone_t smrs_zone;
static smr_t smrs_smr;

static int smrs_cpus;
static int smrs_writers;
static int smrs_started;
static int smrs_iterations = 10000000;
static int smrs_failures = 0;
static volatile int smrs_completed;

struct smrs {
	int		generation;
	volatile u_int	count;
};

uintptr_t smrs_current;

static void
smrs_error(struct smrs *smrs, const char *fmt, ...)
{
	va_list ap;

	atomic_add_int(&smrs_failures, 1);
	printf("SMR ERROR: wr_seq %d, rd_seq %d, c_seq %d, generation %d, count %d ",
	    smrs_smr->c_shared->s_wr.seq, smrs_smr->c_shared->s_rd_seq,
	    zpcpu_get(smrs_smr)->c_seq, smrs->generation, smrs->count);
	va_start(ap, fmt);
	(void)vprintf(fmt, ap);
	va_end(ap);
}

static void
smrs_read(void)
{
	struct smrs *cur;
	int cnt;

	/* Wait for the writer to exit. */
	while (smrs_completed == 0) {
		smr_enter(smrs_smr);
		cur = (void *)atomic_load_acq_ptr(&smrs_current);
		if (cur->generation == -1)
			smrs_error(cur, "read early: Use after free!\n");
		atomic_add_int(&cur->count, 1);
		DELAY(100);
		cnt = atomic_fetchadd_int(&cur->count, -1);
		if (cur->generation == -1)
			smrs_error(cur, "read late: Use after free!\n");
		else if (cnt <= 0)
			smrs_error(cur, "Invalid ref\n");
		smr_exit(smrs_smr);
		maybe_yield();
	}
}

static void
smrs_write(void)
{
	struct smrs *cur;
	int i;

	for (i = 0; i < smrs_iterations; i++) {
		cur = uma_zalloc_smr(smrs_zone, M_WAITOK);
		atomic_thread_fence_rel();
		cur = (void *)atomic_swap_ptr(&smrs_current, (uintptr_t)cur);
		uma_zfree_smr(smrs_zone, cur);
	}
}

static void
smrs_thread(void *arg)
{
	int rw = (intptr_t)arg;

	if (rw < smrs_writers)
		smrs_write();
	else
		smrs_read();
	atomic_add_int(&smrs_completed, 1);
}

static void
smrs_start(void)
{
	struct smrs *cur;
	int i;

	smrs_cpus = mp_ncpus;
	if (mp_ncpus > 3)
		smrs_writers = 2;
	else
		smrs_writers = 1;
	smrs_started = smrs_cpus;
	smrs_completed = 0;
	atomic_store_rel_ptr(&smrs_current,
	    (uintptr_t)uma_zalloc_smr(smrs_zone, M_WAITOK));
	for (i = 0; i < smrs_started; i++)
		kthread_add((void (*)(void *))smrs_thread,
		    (void *)(intptr_t)i, curproc, NULL, 0, 0, "smrs-%d", i);

	while (smrs_completed != smrs_started)
		pause("prf", hz/2);

	cur = (void *)smrs_current;
	smrs_current = (uintptr_t)NULL;
	uma_zfree_smr(smrs_zone, cur);

	printf("Completed %d loops with %d failures\n",
	    smrs_iterations, smrs_failures);
}

static int
smrs_ctor(void *mem, int size, void *arg, int flags)
{
	static int smr_generation = 0;
	struct smrs *smrs = mem;

	if (smrs->generation != -1 && smrs->generation != 0)
		smrs_error(smrs, "ctor: Invalid smr generation on ctor\n");
	else if (smrs->count != 0)
		smrs_error(smrs, "ctor: Invalid count\n");
	smrs->generation = ++smr_generation;

	return (0);
}


static void
smrs_dtor(void *mem, int size, void *arg)
{
	struct smrs *smrs = mem;

	if (smrs->generation == -1)
		smrs_error(smrs, "dtor: Invalid generation");
	smrs->generation = -1;
	if (smrs->count != 0)
		smrs_error(smrs, "dtor: Invalid count\n");
}


static void
smrs_init(void)
{

	smrs_zone = uma_zcreate("smrs", sizeof(struct smrs),
	    smrs_ctor,  smrs_dtor, NULL, NULL, UMA_ALIGN_PTR,
	    UMA_ZONE_SMR | UMA_ZONE_ZINIT);
        smrs_smr = uma_zone_get_smr(smrs_zone);
}

static void
smrs_fini(void)
{

	uma_zdestroy(smrs_zone);
}

static int
smrs_modevent(module_t mod, int what, void *arg)
{

	switch (what) {
	case MOD_LOAD:
		smrs_init();
		smrs_start();
		break;
	case MOD_UNLOAD:
		smrs_fini();
		break;
	default:
		break;
	}
	return (0);
}

moduledata_t smrs_meta = {
	"smrstress",
	smrs_modevent,
	NULL
};
DECLARE_MODULE(smrstress, smrs_meta, SI_SUB_DRIVERS, SI_ORDER_MIDDLE);
MODULE_VERSION(smrstress, 1);