]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/sys/smr.h
Merge ^/head r357270 through r357349.
[FreeBSD/FreeBSD.git] / sys / sys / smr.h
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2019, 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  * $FreeBSD$
28  *
29  */
30
31 #ifndef _SYS_SMR_H_
32 #define _SYS_SMR_H_
33
34 #include <sys/_smr.h>
35
36 /*
37  * Safe memory reclamation.  See subr_smr.c for a description of the
38  * algorithm.
39  *
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.
42  */
43
44 /*
45  * Modular arithmetic for comparing sequence numbers that have
46  * potentially wrapped.  Copied from tcp_seq.h.
47  */
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
53 #define SMR_SEQ_INVALID         0
54
55 /* Shared SMR state. */
56 struct smr_shared {
57         const char      *s_name;        /* Name for debugging/reporting. */
58         smr_seq_t       s_wr_seq;       /* Current write sequence #. */
59         smr_seq_t       s_rd_seq;       /* Minimum observed read sequence. */
60 };
61 typedef struct smr_shared *smr_shared_t;
62
63 /* Per-cpu SMR state. */
64 struct smr {
65         smr_seq_t       c_seq;          /* Current observed sequence. */
66         smr_shared_t    c_shared;       /* Shared SMR state. */
67 };
68
69 /*
70  * Return the current write sequence number.
71  */
72 static inline smr_seq_t
73 smr_current(smr_t smr)
74 {
75
76         return (atomic_load_int(&smr->c_shared->s_wr_seq));
77 }
78
79 /*
80  * Enter a read section.
81  */
82 static inline void
83 smr_enter(smr_t smr)
84 {
85
86         critical_enter();
87         smr = zpcpu_get(smr);
88         KASSERT(smr->c_seq == 0,
89             ("smr_enter(%s) does not support recursion.",
90             smr->c_shared->s_name));
91
92         /*
93          * Store the current observed write sequence number in our
94          * per-cpu state so that it can be queried via smr_poll().
95          * Frees that are newer than this stored value will be
96          * deferred until we call smr_exit().
97          *
98          * An acquire barrier is used to synchronize with smr_exit()
99          * and smr_poll().
100          *
101          * It is possible that a long delay between loading the wr_seq
102          * and storing the c_seq could create a situation where the
103          * rd_seq advances beyond our stored c_seq.  In this situation
104          * only the observed wr_seq is stale, the fence still orders
105          * the load.  See smr_poll() for details on how this condition
106          * is detected and handled there.
107          */
108         /* This is an add because we do not have atomic_store_acq_int */
109         atomic_add_acq_int(&smr->c_seq, smr_current(smr));
110 }
111
112 /*
113  * Exit a read section.
114  */
115 static inline void
116 smr_exit(smr_t smr)
117 {
118
119         smr = zpcpu_get(smr);
120         CRITICAL_ASSERT(curthread);
121         KASSERT(smr->c_seq != SMR_SEQ_INVALID,
122             ("smr_exit(%s) not in a smr section.", smr->c_shared->s_name));
123
124         /*
125          * Clear the recorded sequence number.  This allows poll() to
126          * detect CPUs not in read sections.
127          *
128          * Use release semantics to retire any stores before the sequence
129          * number is cleared.
130          */
131         atomic_store_rel_int(&smr->c_seq, SMR_SEQ_INVALID);
132         critical_exit();
133 }
134
135 /*
136  * Advances the write sequence number.  Returns the sequence number
137  * required to ensure that all modifications are visible to readers.
138  */
139 smr_seq_t smr_advance(smr_t smr);
140
141 /*
142  * Returns true if a goal sequence has been reached.  If
143  * wait is true this will busy loop until success.
144  */
145 bool smr_poll(smr_t smr, smr_seq_t goal, bool wait);
146
147 /* Create a new SMR context. */
148 smr_t smr_create(const char *name);
149 void smr_destroy(smr_t smr);
150
151 /*
152  * Blocking wait for all readers to observe 'goal'.
153  */
154 static inline bool
155 smr_wait(smr_t smr, smr_seq_t goal)
156 {
157
158         return (smr_poll(smr, goal, true));
159 }
160
161 /*
162  * Synchronize advances the write sequence and returns when all
163  * readers have observed it. 
164  *
165  * If your application can cache a sequence number returned from
166  * smr_advance() and poll or wait at a later time there will
167  * be less chance of busy looping while waiting for readers.
168  */
169 static inline void
170 smr_synchronize(smr_t smr)
171 {
172
173         smr_wait(smr, smr_advance(smr));
174 }
175
176 /* Only at startup. */
177 void smr_init(void);
178
179 #endif  /* _SYS_SMR_H_ */