2 * SPDX-License-Identifier: BSD-2-Clause
4 * Copyright (c) 2020 Jeffrey Roberson <jeff@FreeBSD.org>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice unmodified, this list of conditions, and the following
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.
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.
30 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/kernel.h>
33 #include <sys/libkern.h>
34 #include <sys/module.h>
35 #include <sys/mutex.h>
37 #include <sys/kthread.h>
45 #include <machine/stdarg.h>
47 static uma_zone_t smrs_zone;
48 static smr_t smrs_smr;
51 static int smrs_writers;
52 static int smrs_started;
53 static int smrs_iterations = 10000000;
54 static int smrs_failures = 0;
55 static volatile int smrs_completed;
62 uintptr_t smrs_current;
65 smrs_error(struct smrs *smrs, const char *fmt, ...)
69 atomic_add_int(&smrs_failures, 1);
70 printf("SMR ERROR: wr_seq %d, rd_seq %d, c_seq %d, generation %d, count %d ",
71 smrs_smr->c_shared->s_wr.seq, smrs_smr->c_shared->s_rd_seq,
72 zpcpu_get(smrs_smr)->c_seq, smrs->generation, smrs->count);
74 (void)vprintf(fmt, ap);
84 /* Wait for the writer to exit. */
85 while (smrs_completed == 0) {
87 cur = (void *)atomic_load_acq_ptr(&smrs_current);
88 if (cur->generation == -1)
89 smrs_error(cur, "read early: Use after free!\n");
90 atomic_add_int(&cur->count, 1);
92 cnt = atomic_fetchadd_int(&cur->count, -1);
93 if (cur->generation == -1)
94 smrs_error(cur, "read late: Use after free!\n");
96 smrs_error(cur, "Invalid ref\n");
108 for (i = 0; i < smrs_iterations; i++) {
109 cur = uma_zalloc_smr(smrs_zone, M_WAITOK);
110 atomic_thread_fence_rel();
111 cur = (void *)atomic_swap_ptr(&smrs_current, (uintptr_t)cur);
112 uma_zfree_smr(smrs_zone, cur);
117 smrs_thread(void *arg)
119 int rw = (intptr_t)arg;
121 if (rw < smrs_writers)
125 atomic_add_int(&smrs_completed, 1);
135 smrs_cpus = mp_ncpus;
140 smrs_started = smrs_cpus;
142 atomic_store_rel_ptr(&smrs_current,
143 (uintptr_t)uma_zalloc_smr(smrs_zone, M_WAITOK));
144 for (i = 0; i < smrs_started; i++)
145 kthread_add((void (*)(void *))smrs_thread,
146 (void *)(intptr_t)i, curproc, NULL, 0, 0, "smrs-%d", i);
148 while (smrs_completed != smrs_started)
151 cur = (void *)smrs_current;
152 smrs_current = (uintptr_t)NULL;
153 uma_zfree_smr(smrs_zone, cur);
155 printf("Completed %d loops with %d failures\n",
156 smrs_iterations, smrs_failures);
160 smrs_ctor(void *mem, int size, void *arg, int flags)
162 static int smr_generation = 0;
163 struct smrs *smrs = mem;
165 if (smrs->generation != -1 && smrs->generation != 0)
166 smrs_error(smrs, "ctor: Invalid smr generation on ctor\n");
167 else if (smrs->count != 0)
168 smrs_error(smrs, "ctor: Invalid count\n");
169 smrs->generation = ++smr_generation;
176 smrs_dtor(void *mem, int size, void *arg)
178 struct smrs *smrs = mem;
180 if (smrs->generation == -1)
181 smrs_error(smrs, "dtor: Invalid generation");
182 smrs->generation = -1;
183 if (smrs->count != 0)
184 smrs_error(smrs, "dtor: Invalid count\n");
192 smrs_zone = uma_zcreate("smrs", sizeof(struct smrs),
193 smrs_ctor, smrs_dtor, NULL, NULL, UMA_ALIGN_PTR,
194 UMA_ZONE_SMR | UMA_ZONE_ZINIT);
195 smrs_smr = uma_zone_get_smr(smrs_zone);
202 uma_zdestroy(smrs_zone);
206 smrs_modevent(module_t mod, int what, void *arg)
223 moduledata_t smrs_meta = {
228 DECLARE_MODULE(smrstress, smrs_meta, SI_SUB_DRIVERS, SI_ORDER_MIDDLE);
229 MODULE_VERSION(smrstress, 1);