2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
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);
134 smrs_cpus = mp_ncpus;
139 smrs_started = smrs_cpus;
141 atomic_store_rel_ptr(&smrs_current,
142 (uintptr_t)uma_zalloc_smr(smrs_zone, M_WAITOK));
143 for (i = 0; i < smrs_started; i++)
144 kthread_add((void (*)(void *))smrs_thread,
145 (void *)(intptr_t)i, curproc, NULL, 0, 0, "smrs-%d", i);
147 while (smrs_completed != smrs_started)
150 cur = (void *)smrs_current;
151 smrs_current = (uintptr_t)NULL;
152 uma_zfree_smr(smrs_zone, cur);
154 printf("Completed %d loops with %d failures\n",
155 smrs_iterations, smrs_failures);
159 smrs_ctor(void *mem, int size, void *arg, int flags)
161 static int smr_generation = 0;
162 struct smrs *smrs = mem;
164 if (smrs->generation != -1 && smrs->generation != 0)
165 smrs_error(smrs, "ctor: Invalid smr generation on ctor\n");
166 else if (smrs->count != 0)
167 smrs_error(smrs, "ctor: Invalid count\n");
168 smrs->generation = ++smr_generation;
175 smrs_dtor(void *mem, int size, void *arg)
177 struct smrs *smrs = mem;
179 if (smrs->generation == -1)
180 smrs_error(smrs, "dtor: Invalid generation");
181 smrs->generation = -1;
182 if (smrs->count != 0)
183 smrs_error(smrs, "dtor: Invalid count\n");
191 smrs_zone = uma_zcreate("smrs", sizeof(struct smrs),
192 smrs_ctor, smrs_dtor, NULL, NULL, UMA_ALIGN_PTR,
193 UMA_ZONE_SMR | UMA_ZONE_ZINIT);
194 smrs_smr = uma_zone_get_smr(smrs_zone);
201 uma_zdestroy(smrs_zone);
205 smrs_modevent(module_t mod, int what, void *arg)
222 moduledata_t smrs_meta = {
227 DECLARE_MODULE(smrstress, smrs_meta, SI_SUB_DRIVERS, SI_ORDER_MIDDLE);
228 MODULE_VERSION(smrstress, 1);