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.
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>
35 #include <sys/kthread.h>
43 #include <machine/stdarg.h>
45 static uma_zone_t smrs_zone;
46 static smr_t smrs_smr;
49 static int smrs_writers;
50 static int smrs_started;
51 static int smrs_iterations = 10000000;
52 static int smrs_failures = 0;
53 static volatile int smrs_completed;
60 uintptr_t smrs_current;
63 smrs_error(struct smrs *smrs, const char *fmt, ...)
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);
72 (void)vprintf(fmt, ap);
82 /* Wait for the writer to exit. */
83 while (smrs_completed == 0) {
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);
90 cnt = atomic_fetchadd_int(&cur->count, -1);
91 if (cur->generation == -1)
92 smrs_error(cur, "read late: Use after free!\n");
94 smrs_error(cur, "Invalid ref\n");
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);
115 smrs_thread(void *arg)
117 int rw = (intptr_t)arg;
119 if (rw < smrs_writers)
123 atomic_add_int(&smrs_completed, 1);
133 smrs_cpus = mp_ncpus;
138 smrs_started = smrs_cpus;
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);
146 while (smrs_completed != smrs_started)
149 cur = (void *)smrs_current;
150 smrs_current = (uintptr_t)NULL;
151 uma_zfree_smr(smrs_zone, cur);
153 printf("Completed %d loops with %d failures\n",
154 smrs_iterations, smrs_failures);
158 smrs_ctor(void *mem, int size, void *arg, int flags)
160 static int smr_generation = 0;
161 struct smrs *smrs = mem;
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;
174 smrs_dtor(void *mem, int size, void *arg)
176 struct smrs *smrs = mem;
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");
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);
200 uma_zdestroy(smrs_zone);
204 smrs_modevent(module_t mod, int what, void *arg)
221 moduledata_t smrs_meta = {
226 DECLARE_MODULE(smrstress, smrs_meta, SI_SUB_DRIVERS, SI_ORDER_MIDDLE);
227 MODULE_VERSION(smrstress, 1);