1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29#include "qemu/osdep.h"
30#include "qemu/rcu.h"
31#include "qemu/atomic.h"
32#include "qemu/thread.h"
33#include "qemu/main-loop.h"
34#if defined(CONFIG_MALLOC_TRIM)
35#include <malloc.h>
36#endif
37
38
39
40
41
42#define RCU_GP_LOCKED (1UL << 0)
43#define RCU_GP_CTR (1UL << 1)
44
45unsigned long rcu_gp_ctr = RCU_GP_LOCKED;
46
47QemuEvent rcu_gp_event;
48static QemuMutex rcu_registry_lock;
49static QemuMutex rcu_sync_lock;
50
51
52
53
54
55static inline int rcu_gp_ongoing(unsigned long *ctr)
56{
57 unsigned long v;
58
59 v = atomic_read(ctr);
60 return v && (v != rcu_gp_ctr);
61}
62
63
64
65
66__thread struct rcu_reader_data rcu_reader;
67
68
69typedef QLIST_HEAD(, rcu_reader_data) ThreadList;
70static ThreadList registry = QLIST_HEAD_INITIALIZER(registry);
71
72
73static void wait_for_readers(void)
74{
75 ThreadList qsreaders = QLIST_HEAD_INITIALIZER(qsreaders);
76 struct rcu_reader_data *index, *tmp;
77
78 for (;;) {
79
80
81
82 qemu_event_reset(&rcu_gp_event);
83
84
85
86
87
88
89
90 QLIST_FOREACH(index, ®istry, node) {
91 atomic_set(&index->waiting, true);
92 }
93
94
95
96
97
98 smp_mb_global();
99
100 QLIST_FOREACH_SAFE(index, ®istry, node, tmp) {
101 if (!rcu_gp_ongoing(&index->ctr)) {
102 QLIST_REMOVE(index, node);
103 QLIST_INSERT_HEAD(&qsreaders, index, node);
104
105
106
107
108 atomic_set(&index->waiting, false);
109 }
110 }
111
112 if (QLIST_EMPTY(®istry)) {
113 break;
114 }
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133 qemu_mutex_unlock(&rcu_registry_lock);
134 qemu_event_wait(&rcu_gp_event);
135 qemu_mutex_lock(&rcu_registry_lock);
136 }
137
138
139 QLIST_SWAP(®istry, &qsreaders, node);
140}
141
142void synchronize_rcu(void)
143{
144 qemu_mutex_lock(&rcu_sync_lock);
145
146
147
148
149 smp_mb_global();
150
151 qemu_mutex_lock(&rcu_registry_lock);
152 if (!QLIST_EMPTY(®istry)) {
153
154
155
156 if (sizeof(rcu_gp_ctr) < 8) {
157
158
159
160
161
162 atomic_mb_set(&rcu_gp_ctr, rcu_gp_ctr ^ RCU_GP_CTR);
163 wait_for_readers();
164 atomic_mb_set(&rcu_gp_ctr, rcu_gp_ctr ^ RCU_GP_CTR);
165 } else {
166
167 atomic_mb_set(&rcu_gp_ctr, rcu_gp_ctr + RCU_GP_CTR);
168 }
169
170 wait_for_readers();
171 }
172
173 qemu_mutex_unlock(&rcu_registry_lock);
174 qemu_mutex_unlock(&rcu_sync_lock);
175}
176
177
178#define RCU_CALL_MIN_SIZE 30
179
180
181
182
183static struct rcu_head dummy;
184static struct rcu_head *head = &dummy, **tail = &dummy.next;
185static int rcu_call_count;
186static QemuEvent rcu_call_ready_event;
187
188static void enqueue(struct rcu_head *node)
189{
190 struct rcu_head **old_tail;
191
192 node->next = NULL;
193 old_tail = atomic_xchg(&tail, &node->next);
194 atomic_mb_set(old_tail, node);
195}
196
197static struct rcu_head *try_dequeue(void)
198{
199 struct rcu_head *node, *next;
200
201retry:
202
203
204
205
206
207
208 if (head == &dummy && atomic_mb_read(&tail) == &dummy.next) {
209 abort();
210 }
211
212
213
214
215 node = head;
216 next = atomic_mb_read(&head->next);
217 if (!next) {
218 return NULL;
219 }
220
221
222
223
224
225
226 head = next;
227
228
229 if (node == &dummy) {
230 enqueue(node);
231 goto retry;
232 }
233
234 return node;
235}
236
237static void *call_rcu_thread(void *opaque)
238{
239 struct rcu_head *node;
240
241 rcu_register_thread();
242
243 for (;;) {
244 int tries = 0;
245 int n = atomic_read(&rcu_call_count);
246
247
248
249
250
251 while (n == 0 || (n < RCU_CALL_MIN_SIZE && ++tries <= 5)) {
252 g_usleep(10000);
253 if (n == 0) {
254 qemu_event_reset(&rcu_call_ready_event);
255 n = atomic_read(&rcu_call_count);
256 if (n == 0) {
257#if defined(CONFIG_MALLOC_TRIM)
258 malloc_trim(4 * 1024 * 1024);
259#endif
260 qemu_event_wait(&rcu_call_ready_event);
261 }
262 }
263 n = atomic_read(&rcu_call_count);
264 }
265
266 atomic_sub(&rcu_call_count, n);
267 synchronize_rcu();
268 qemu_mutex_lock_iothread();
269 while (n > 0) {
270 node = try_dequeue();
271 while (!node) {
272 qemu_mutex_unlock_iothread();
273 qemu_event_reset(&rcu_call_ready_event);
274 node = try_dequeue();
275 if (!node) {
276 qemu_event_wait(&rcu_call_ready_event);
277 node = try_dequeue();
278 }
279 qemu_mutex_lock_iothread();
280 }
281
282 n--;
283 node->func(node);
284 }
285 qemu_mutex_unlock_iothread();
286 }
287 abort();
288}
289
290void call_rcu1(struct rcu_head *node, void (*func)(struct rcu_head *node))
291{
292 node->func = func;
293 enqueue(node);
294 atomic_inc(&rcu_call_count);
295 qemu_event_set(&rcu_call_ready_event);
296}
297
298void rcu_register_thread(void)
299{
300 assert(rcu_reader.ctr == 0);
301 qemu_mutex_lock(&rcu_registry_lock);
302 QLIST_INSERT_HEAD(®istry, &rcu_reader, node);
303 qemu_mutex_unlock(&rcu_registry_lock);
304}
305
306void rcu_unregister_thread(void)
307{
308 qemu_mutex_lock(&rcu_registry_lock);
309 QLIST_REMOVE(&rcu_reader, node);
310 qemu_mutex_unlock(&rcu_registry_lock);
311}
312
313static void rcu_init_complete(void)
314{
315 QemuThread thread;
316
317 qemu_mutex_init(&rcu_registry_lock);
318 qemu_mutex_init(&rcu_sync_lock);
319 qemu_event_init(&rcu_gp_event, true);
320
321 qemu_event_init(&rcu_call_ready_event, false);
322
323
324
325
326 qemu_thread_create(&thread, "call_rcu", call_rcu_thread,
327 NULL, QEMU_THREAD_DETACHED);
328
329 rcu_register_thread();
330}
331
332static int atfork_depth = 1;
333
334void rcu_enable_atfork(void)
335{
336 atfork_depth++;
337}
338
339void rcu_disable_atfork(void)
340{
341 atfork_depth--;
342}
343
344#ifdef CONFIG_POSIX
345static void rcu_init_lock(void)
346{
347 if (atfork_depth < 1) {
348 return;
349 }
350
351 qemu_mutex_lock(&rcu_sync_lock);
352 qemu_mutex_lock(&rcu_registry_lock);
353}
354
355static void rcu_init_unlock(void)
356{
357 if (atfork_depth < 1) {
358 return;
359 }
360
361 qemu_mutex_unlock(&rcu_registry_lock);
362 qemu_mutex_unlock(&rcu_sync_lock);
363}
364
365static void rcu_init_child(void)
366{
367 if (atfork_depth < 1) {
368 return;
369 }
370
371 memset(®istry, 0, sizeof(registry));
372 rcu_init_complete();
373}
374#endif
375
376static void __attribute__((__constructor__)) rcu_init(void)
377{
378 smp_mb_global_init();
379#ifdef CONFIG_POSIX
380 pthread_atfork(rcu_init_lock, rcu_init_unlock, rcu_init_child);
381#endif
382 rcu_init_complete();
383}
384