2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2019, 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.
37 * Safe memory reclamation. See subr_smr.c for a description of the
40 * Readers synchronize with smr_enter()/exit() and writers may either
41 * free directly to a SMR UMA zone or use smr_synchronize or wait.
45 * Modular arithmetic for comparing sequence numbers that have
46 * potentially wrapped. Copied from tcp_seq.h.
48 #define SMR_SEQ_LT(a, b) ((int32_t)((a)-(b)) < 0)
49 #define SMR_SEQ_LEQ(a, b) ((int32_t)((a)-(b)) <= 0)
50 #define SMR_SEQ_GT(a, b) ((int32_t)((a)-(b)) > 0)
51 #define SMR_SEQ_GEQ(a, b) ((int32_t)((a)-(b)) >= 0)
52 #define SMR_SEQ_DELTA(a, b) ((int32_t)((a)-(b)))
54 #define SMR_SEQ_INVALID 0
56 /* Shared SMR state. */
58 const char *s_name; /* Name for debugging/reporting. */
59 smr_seq_t s_wr_seq; /* Current write sequence #. */
60 smr_seq_t s_rd_seq; /* Minimum observed read sequence. */
62 typedef struct smr_shared *smr_shared_t;
64 /* Per-cpu SMR state. */
66 smr_seq_t c_seq; /* Current observed sequence. */
67 smr_shared_t c_shared; /* Shared SMR state. */
68 int c_deferred; /* Deferred advance counter. */
71 #define SMR_ENTERED(smr) \
72 (curthread->td_critnest != 0 && zpcpu_get((smr))->c_seq != SMR_SEQ_INVALID)
74 #define SMR_ASSERT_ENTERED(smr) \
75 KASSERT(SMR_ENTERED(smr), ("Not in smr section"))
77 #define SMR_ASSERT_NOT_ENTERED(smr) \
78 KASSERT(!SMR_ENTERED(smr), ("In smr section."));
81 * Return the current write sequence number.
83 static inline smr_seq_t
84 smr_shared_current(smr_shared_t s)
87 return (atomic_load_int(&s->s_wr_seq));
90 static inline smr_seq_t
91 smr_current(smr_t smr)
94 return (smr_shared_current(zpcpu_get(smr)->c_shared));
98 * Enter a read section.
105 smr = zpcpu_get(smr);
106 KASSERT(smr->c_seq == 0,
107 ("smr_enter(%s) does not support recursion.",
108 smr->c_shared->s_name));
111 * Store the current observed write sequence number in our
112 * per-cpu state so that it can be queried via smr_poll().
113 * Frees that are newer than this stored value will be
114 * deferred until we call smr_exit().
116 * An acquire barrier is used to synchronize with smr_exit()
119 * It is possible that a long delay between loading the wr_seq
120 * and storing the c_seq could create a situation where the
121 * rd_seq advances beyond our stored c_seq. In this situation
122 * only the observed wr_seq is stale, the fence still orders
123 * the load. See smr_poll() for details on how this condition
124 * is detected and handled there.
126 /* This is an add because we do not have atomic_store_acq_int */
127 atomic_add_acq_int(&smr->c_seq, smr_shared_current(smr->c_shared));
131 * Exit a read section.
137 smr = zpcpu_get(smr);
138 CRITICAL_ASSERT(curthread);
139 KASSERT(smr->c_seq != SMR_SEQ_INVALID,
140 ("smr_exit(%s) not in a smr section.", smr->c_shared->s_name));
143 * Clear the recorded sequence number. This allows poll() to
144 * detect CPUs not in read sections.
146 * Use release semantics to retire any stores before the sequence
149 atomic_store_rel_int(&smr->c_seq, SMR_SEQ_INVALID);
154 * Advances the write sequence number. Returns the sequence number
155 * required to ensure that all modifications are visible to readers.
157 smr_seq_t smr_advance(smr_t smr);
160 * Advances the write sequence number only after N calls. Returns
161 * the correct goal for a wr_seq that has not yet occurred. Used to
162 * minimize shared cacheline invalidations for frequent writers.
164 smr_seq_t smr_advance_deferred(smr_t smr, int limit);
167 * Returns true if a goal sequence has been reached. If
168 * wait is true this will busy loop until success.
170 bool smr_poll(smr_t smr, smr_seq_t goal, bool wait);
172 /* Create a new SMR context. */
173 smr_t smr_create(const char *name);
174 void smr_destroy(smr_t smr);
177 * Blocking wait for all readers to observe 'goal'.
180 smr_wait(smr_t smr, smr_seq_t goal)
183 return (smr_poll(smr, goal, true));
187 * Synchronize advances the write sequence and returns when all
188 * readers have observed it.
190 * If your application can cache a sequence number returned from
191 * smr_advance() and poll or wait at a later time there will
192 * be less chance of busy looping while waiting for readers.
195 smr_synchronize(smr_t smr)
198 smr_wait(smr, smr_advance(smr));
201 /* Only at startup. */
204 #endif /* _SYS_SMR_H_ */