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