1/*-
2 * SPDX-License-Identifier: BSD-2-Clause
3 *
4 * Copyright (c) 2020 Jeffrey Roberson <jeff@FreeBSD.org>
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 *    notice unmodified, this list of conditions, and the following
11 *    disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 *    notice, this list of conditions and the following disclaimer in the
14 *    documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 *
27 */
28#include <sys/param.h>
29#include <sys/systm.h>
30#include <sys/kernel.h>
31#include <sys/libkern.h>
32#include <sys/module.h>
33#include <sys/mutex.h>
34#include <sys/proc.h>
35#include <sys/kthread.h>
36#include <sys/conf.h>
37#include <sys/mbuf.h>
38#include <sys/smp.h>
39#include <sys/smr.h>
40
41#include <vm/uma.h>
42
43#include <machine/stdarg.h>
44
45static uma_zone_t smrs_zone;
46static smr_t smrs_smr;
47
48static int smrs_cpus;
49static int smrs_writers;
50static int smrs_started;
51static int smrs_iterations = 10000000;
52static int smrs_failures = 0;
53static volatile int smrs_completed;
54
55struct smrs {
56	int		generation;
57	volatile u_int	count;
58};
59
60uintptr_t smrs_current;
61
62static void
63smrs_error(struct smrs *smrs, const char *fmt, ...)
64{
65	va_list ap;
66
67	atomic_add_int(&smrs_failures, 1);
68	printf("SMR ERROR: wr_seq %d, rd_seq %d, c_seq %d, generation %d, count %d ",
69	    smrs_smr->c_shared->s_wr.seq, smrs_smr->c_shared->s_rd_seq,
70	    zpcpu_get(smrs_smr)->c_seq, smrs->generation, smrs->count);
71	va_start(ap, fmt);
72	(void)vprintf(fmt, ap);
73	va_end(ap);
74}
75
76static void
77smrs_read(void)
78{
79	struct smrs *cur;
80	int cnt;
81
82	/* Wait for the writer to exit. */
83	while (smrs_completed == 0) {
84		smr_enter(smrs_smr);
85		cur = (void *)atomic_load_acq_ptr(&smrs_current);
86		if (cur->generation == -1)
87			smrs_error(cur, "read early: Use after free!\n");
88		atomic_add_int(&cur->count, 1);
89		DELAY(100);
90		cnt = atomic_fetchadd_int(&cur->count, -1);
91		if (cur->generation == -1)
92			smrs_error(cur, "read late: Use after free!\n");
93		else if (cnt <= 0)
94			smrs_error(cur, "Invalid ref\n");
95		smr_exit(smrs_smr);
96		maybe_yield();
97	}
98}
99
100static void
101smrs_write(void)
102{
103	struct smrs *cur;
104	int i;
105
106	for (i = 0; i < smrs_iterations; i++) {
107		cur = uma_zalloc_smr(smrs_zone, M_WAITOK);
108		atomic_thread_fence_rel();
109		cur = (void *)atomic_swap_ptr(&smrs_current, (uintptr_t)cur);
110		uma_zfree_smr(smrs_zone, cur);
111	}
112}
113
114static void
115smrs_thread(void *arg)
116{
117	int rw = (intptr_t)arg;
118
119	if (rw < smrs_writers)
120		smrs_write();
121	else
122		smrs_read();
123	atomic_add_int(&smrs_completed, 1);
124	kthread_exit();
125}
126
127static void
128smrs_start(void)
129{
130	struct smrs *cur;
131	int i;
132
133	smrs_cpus = mp_ncpus;
134	if (mp_ncpus > 3)
135		smrs_writers = 2;
136	else
137		smrs_writers = 1;
138	smrs_started = smrs_cpus;
139	smrs_completed = 0;
140	atomic_store_rel_ptr(&smrs_current,
141	    (uintptr_t)uma_zalloc_smr(smrs_zone, M_WAITOK));
142	for (i = 0; i < smrs_started; i++)
143		kthread_add((void (*)(void *))smrs_thread,
144		    (void *)(intptr_t)i, curproc, NULL, 0, 0, "smrs-%d", i);
145
146	while (smrs_completed != smrs_started)
147		pause("prf", hz/2);
148
149	cur = (void *)smrs_current;
150	smrs_current = (uintptr_t)NULL;
151	uma_zfree_smr(smrs_zone, cur);
152
153	printf("Completed %d loops with %d failures\n",
154	    smrs_iterations, smrs_failures);
155}
156
157static int
158smrs_ctor(void *mem, int size, void *arg, int flags)
159{
160	static int smr_generation = 0;
161	struct smrs *smrs = mem;
162
163	if (smrs->generation != -1 && smrs->generation != 0)
164		smrs_error(smrs, "ctor: Invalid smr generation on ctor\n");
165	else if (smrs->count != 0)
166		smrs_error(smrs, "ctor: Invalid count\n");
167	smrs->generation = ++smr_generation;
168
169	return (0);
170}
171
172
173static void
174smrs_dtor(void *mem, int size, void *arg)
175{
176	struct smrs *smrs = mem;
177
178	if (smrs->generation == -1)
179		smrs_error(smrs, "dtor: Invalid generation");
180	smrs->generation = -1;
181	if (smrs->count != 0)
182		smrs_error(smrs, "dtor: Invalid count\n");
183}
184
185
186static void
187smrs_init(void)
188{
189
190	smrs_zone = uma_zcreate("smrs", sizeof(struct smrs),
191	    smrs_ctor,  smrs_dtor, NULL, NULL, UMA_ALIGN_PTR,
192	    UMA_ZONE_SMR | UMA_ZONE_ZINIT);
193        smrs_smr = uma_zone_get_smr(smrs_zone);
194}
195
196static void
197smrs_fini(void)
198{
199
200	uma_zdestroy(smrs_zone);
201}
202
203static int
204smrs_modevent(module_t mod, int what, void *arg)
205{
206
207	switch (what) {
208	case MOD_LOAD:
209		smrs_init();
210		smrs_start();
211		break;
212	case MOD_UNLOAD:
213		smrs_fini();
214		break;
215	default:
216		break;
217	}
218	return (0);
219}
220
221moduledata_t smrs_meta = {
222	"smrstress",
223	smrs_modevent,
224	NULL
225};
226DECLARE_MODULE(smrstress, smrs_meta, SI_SUB_DRIVERS, SI_ORDER_MIDDLE);
227MODULE_VERSION(smrstress, 1);
228