source: mainline/uspace/lib/c/generic/thread/rcu.c@ f787c8e

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since f787c8e was f787c8e, checked in by Jiří Zárevúcky <jiri.zarevucky@…>, 7 years ago

Move some internal interfaces to private headers.

  • Property mode set to 100644
File size: 12.6 KB
Line 
1/*
2 * Copyright (c) 2012 Adam Hraska
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29/** @addtogroup liburcu
30 * @{
31 */
32/**
33 * @file
34 *
35 * User space RCU is based on URCU utilizing signals [1]. This
36 * implementation does not however signal each thread of the process
37 * to issue a memory barrier. Instead, we introduced a syscall that
38 * issues memory barriers (via IPIs) on cpus that are running threads
39 * of the current process. First, it does not require us to schedule
40 * and run every thread of the process. Second, IPIs are less intrusive
41 * than switching contexts and entering user space.
42 *
43 * This algorithm is further modified to require a single instead of
44 * two reader group changes per grace period. Signal-URCU flips
45 * the reader group and waits for readers of the previous group
46 * twice in succession in order to wait for new readers that were
47 * delayed and mistakenly associated with the previous reader group.
48 * The modified algorithm ensures that the new reader group is
49 * always empty (by explicitly waiting for it to become empty).
50 * Only then does it flip the reader group and wait for preexisting
51 * readers of the old reader group (invariant of SRCU [2, 3]).
52 *
53 *
54 * [1] User-level implementations of read-copy update,
55 * 2012, appendix
56 * http://www.rdrop.com/users/paulmck/RCU/urcu-supp-accepted.2011.08.30a.pdf
57 *
58 * [2] linux/kernel/srcu.c in Linux 3.5-rc2,
59 * 2012
60 * http://tomoyo.sourceforge.jp/cgi-bin/lxr/source/kernel/srcu.c?v=linux-3.5-rc2-ccs-1.8.3
61 *
62 * [3] [RFC PATCH 5/5 single-thread-version] implement
63 * per-domain single-thread state machine,
64 * 2012, Lai
65 * https://lkml.org/lkml/2012/3/6/586
66 */
67
68#include "rcu.h"
69#include <fibril_synch.h>
70#include <fibril.h>
71#include <stdio.h>
72#include <stddef.h>
73#include <compiler/barrier.h>
74#include <libarch/barrier.h>
75#include <macros.h>
76#include <async.h>
77#include <adt/list.h>
78#include <smp_memory_barrier.h>
79#include <assert.h>
80#include <time.h>
81
82#include "../private/fibril.h"
83
84
85/** RCU sleeps for RCU_SLEEP_MS before polling an active RCU reader again. */
86#define RCU_SLEEP_MS 10
87
88#define RCU_NESTING_SHIFT 1
89#define RCU_NESTING_INC (1 << RCU_NESTING_SHIFT)
90#define RCU_GROUP_BIT_MASK (size_t)(RCU_NESTING_INC - 1)
91#define RCU_GROUP_A (size_t)(0 | RCU_NESTING_INC)
92#define RCU_GROUP_B (size_t)(1 | RCU_NESTING_INC)
93
94
95/** Fibril local RCU data. */
96typedef struct fibril_rcu_data {
97 size_t nesting_cnt;
98 link_t link;
99 bool registered;
100} fibril_rcu_data_t;
101
102/** Process global RCU data. */
103typedef struct rcu_data {
104 size_t cur_gp;
105 size_t reader_group;
106 fibril_rmutex_t list_mutex;
107 list_t fibrils_list;
108 struct {
109 fibril_rmutex_t mutex;
110 bool locked;
111 list_t blocked_fibrils;
112 } sync_lock;
113} rcu_data_t;
114
115typedef struct blocked_fibril {
116 fibril_event_t unblock;
117 link_t link;
118 bool is_ready;
119} blocked_fibril_t;
120
121
122/** Fibril local RCU data. */
123static fibril_local fibril_rcu_data_t fibril_rcu = {
124 .nesting_cnt = 0,
125 .link = {
126 .next = NULL,
127 .prev = NULL
128 },
129 .registered = false
130};
131
132/** Process global RCU data. */
133static rcu_data_t rcu = {
134 .cur_gp = 0,
135 .reader_group = RCU_GROUP_A,
136 .list_mutex = FIBRIL_RMUTEX_INITIALIZER(rcu.list_mutex),
137 .fibrils_list = LIST_INITIALIZER(rcu.fibrils_list),
138 .sync_lock = {
139 .mutex = FIBRIL_RMUTEX_INITIALIZER(rcu.sync_lock.mutex),
140 .locked = false,
141 .blocked_fibrils = LIST_INITIALIZER(rcu.sync_lock.blocked_fibrils),
142 },
143};
144
145
146static void wait_for_readers(size_t reader_group);
147static void force_mb_in_all_threads(void);
148static bool is_preexisting_reader(const fibril_rcu_data_t *fib, size_t group);
149
150static void lock_sync(void);
151static void unlock_sync(void);
152static void sync_sleep(void);
153
154static bool is_in_group(size_t nesting_cnt, size_t group);
155static bool is_in_reader_section(size_t nesting_cnt);
156static size_t get_other_group(size_t group);
157
158
159/** Registers a fibril so it may start using RCU read sections.
160 *
161 * A fibril must be registered with rcu before it can enter RCU critical
162 * sections delineated by rcu_read_lock() and rcu_read_unlock().
163 */
164void rcu_register_fibril(void)
165{
166 assert(!fibril_rcu.registered);
167
168 fibril_rmutex_lock(&rcu.list_mutex);
169 list_append(&fibril_rcu.link, &rcu.fibrils_list);
170 fibril_rmutex_unlock(&rcu.list_mutex);
171
172 fibril_rcu.registered = true;
173}
174
175/** Deregisters a fibril that had been using RCU read sections.
176 *
177 * A fibril must be deregistered before it exits if it had
178 * been registered with rcu via rcu_register_fibril().
179 */
180void rcu_deregister_fibril(void)
181{
182 assert(fibril_rcu.registered);
183
184 /*
185 * Forcefully unlock any reader sections. The fibril is exiting
186 * so it is not holding any references to data protected by the
187 * rcu section. Therefore, it is safe to unlock. Otherwise,
188 * rcu_synchronize() would wait indefinitely.
189 */
190 memory_barrier();
191 fibril_rcu.nesting_cnt = 0;
192
193 fibril_rmutex_lock(&rcu.list_mutex);
194 list_remove(&fibril_rcu.link);
195 fibril_rmutex_unlock(&rcu.list_mutex);
196
197 fibril_rcu.registered = false;
198}
199
200/** Delimits the start of an RCU reader critical section.
201 *
202 * RCU reader sections may be nested.
203 */
204void rcu_read_lock(void)
205{
206 assert(fibril_rcu.registered);
207
208 size_t nesting_cnt = ACCESS_ONCE(fibril_rcu.nesting_cnt);
209
210 if (0 == (nesting_cnt >> RCU_NESTING_SHIFT)) {
211 ACCESS_ONCE(fibril_rcu.nesting_cnt) = ACCESS_ONCE(rcu.reader_group);
212 /* Required by MB_FORCE_L */
213 compiler_barrier(); /* CC_BAR_L */
214 } else {
215 ACCESS_ONCE(fibril_rcu.nesting_cnt) = nesting_cnt + RCU_NESTING_INC;
216 }
217}
218
219/** Delimits the end of an RCU reader critical section. */
220void rcu_read_unlock(void)
221{
222 assert(fibril_rcu.registered);
223 assert(rcu_read_locked());
224
225 /* Required by MB_FORCE_U */
226 compiler_barrier(); /* CC_BAR_U */
227 /* todo: ACCESS_ONCE(nesting_cnt) ? */
228 fibril_rcu.nesting_cnt -= RCU_NESTING_INC;
229}
230
231/** Returns true if the current fibril is in an RCU reader section. */
232bool rcu_read_locked(void)
233{
234 return 0 != (fibril_rcu.nesting_cnt >> RCU_NESTING_SHIFT);
235}
236
237/** Blocks until all preexisting readers exit their critical sections. */
238void rcu_synchronize(void)
239{
240 assert(!rcu_read_locked());
241
242 /* Contain load of rcu.cur_gp. */
243 memory_barrier();
244
245 /* Approximately the number of the GP in progress. */
246 size_t gp_in_progress = ACCESS_ONCE(rcu.cur_gp);
247
248 lock_sync();
249
250 /*
251 * Exit early if we were stuck waiting for the mutex for a full grace
252 * period. Started waiting during gp_in_progress (or gp_in_progress + 1
253 * if the value propagated to this cpu too late) so wait for the next
254 * full GP, gp_in_progress + 1, to finish. Ie don't wait if the GP
255 * after that, gp_in_progress + 2, already started.
256 */
257 /* rcu.cur_gp >= gp_in_progress + 2, but tolerates overflows. */
258 if (rcu.cur_gp != gp_in_progress && rcu.cur_gp + 1 != gp_in_progress) {
259 unlock_sync();
260 return;
261 }
262
263 ++ACCESS_ONCE(rcu.cur_gp);
264
265 /*
266 * Pairs up with MB_FORCE_L (ie CC_BAR_L). Makes changes prior
267 * to rcu_synchronize() visible to new readers.
268 */
269 memory_barrier(); /* MB_A */
270
271 /*
272 * Pairs up with MB_A.
273 *
274 * If the memory barrier is issued before CC_BAR_L in the target
275 * thread, it pairs up with MB_A and the thread sees all changes
276 * prior to rcu_synchronize(). Ie any reader sections are new
277 * rcu readers.
278 *
279 * If the memory barrier is issued after CC_BAR_L, it pairs up
280 * with MB_B and it will make the most recent nesting_cnt visible
281 * in this thread. Since the reader may have already accessed
282 * memory protected by RCU (it ran instructions passed CC_BAR_L),
283 * it is a preexisting reader. Seeing the most recent nesting_cnt
284 * ensures the thread will be identified as a preexisting reader
285 * and we will wait for it in wait_for_readers(old_reader_group).
286 */
287 force_mb_in_all_threads(); /* MB_FORCE_L */
288
289 /*
290 * Pairs with MB_FORCE_L (ie CC_BAR_L, CC_BAR_U) and makes the most
291 * current fibril.nesting_cnt visible to this cpu.
292 */
293 read_barrier(); /* MB_B */
294
295 size_t new_reader_group = get_other_group(rcu.reader_group);
296 wait_for_readers(new_reader_group);
297
298 /* Separates waiting for readers in new_reader_group from group flip. */
299 memory_barrier();
300
301 /* Flip the group new readers should associate with. */
302 size_t old_reader_group = rcu.reader_group;
303 rcu.reader_group = new_reader_group;
304
305 /* Flip the group before waiting for preexisting readers in the old group.*/
306 memory_barrier();
307
308 wait_for_readers(old_reader_group);
309
310 /* MB_FORCE_U */
311 force_mb_in_all_threads(); /* MB_FORCE_U */
312
313 unlock_sync();
314}
315
316/** Issues a memory barrier in each thread of this process. */
317static void force_mb_in_all_threads(void)
318{
319 /*
320 * Only issue barriers in running threads. The scheduler will
321 * execute additional memory barriers when switching to threads
322 * of the process that are currently not running.
323 */
324 smp_memory_barrier();
325}
326
327/** Waits for readers of reader_group to exit their readers sections. */
328static void wait_for_readers(size_t reader_group)
329{
330 fibril_rmutex_lock(&rcu.list_mutex);
331
332 list_t quiescent_fibrils;
333 list_initialize(&quiescent_fibrils);
334
335 while (!list_empty(&rcu.fibrils_list)) {
336 list_foreach_safe(rcu.fibrils_list, fibril_it, next_fibril) {
337 fibril_rcu_data_t *fib = member_to_inst(fibril_it,
338 fibril_rcu_data_t, link);
339
340 if (is_preexisting_reader(fib, reader_group)) {
341 fibril_rmutex_unlock(&rcu.list_mutex);
342 sync_sleep();
343 fibril_rmutex_lock(&rcu.list_mutex);
344 /* Break to while loop. */
345 break;
346 } else {
347 list_remove(fibril_it);
348 list_append(fibril_it, &quiescent_fibrils);
349 }
350 }
351 }
352
353 list_concat(&rcu.fibrils_list, &quiescent_fibrils);
354 fibril_rmutex_unlock(&rcu.list_mutex);
355}
356
357static void lock_sync(void)
358{
359 fibril_rmutex_lock(&rcu.sync_lock.mutex);
360 if (rcu.sync_lock.locked) {
361 blocked_fibril_t blocked_fib;
362 blocked_fib.unblock = FIBRIL_EVENT_INIT;
363
364 list_append(&blocked_fib.link, &rcu.sync_lock.blocked_fibrils);
365
366 do {
367 blocked_fib.is_ready = false;
368 fibril_rmutex_unlock(&rcu.sync_lock.mutex);
369 fibril_wait_for(&blocked_fib.unblock);
370 fibril_rmutex_lock(&rcu.sync_lock.mutex);
371 } while (rcu.sync_lock.locked);
372
373 list_remove(&blocked_fib.link);
374 rcu.sync_lock.locked = true;
375 } else {
376 rcu.sync_lock.locked = true;
377 }
378}
379
380static void unlock_sync(void)
381{
382 assert(rcu.sync_lock.locked);
383
384 /* Unlock but wake up any fibrils waiting for the lock. */
385
386 if (!list_empty(&rcu.sync_lock.blocked_fibrils)) {
387 blocked_fibril_t *blocked_fib = member_to_inst(
388 list_first(&rcu.sync_lock.blocked_fibrils), blocked_fibril_t, link);
389
390 if (!blocked_fib->is_ready) {
391 blocked_fib->is_ready = true;
392 fibril_notify(&blocked_fib->unblock);
393 }
394 }
395
396 rcu.sync_lock.locked = false;
397 fibril_rmutex_unlock(&rcu.sync_lock.mutex);
398}
399
400static void sync_sleep(void)
401{
402 assert(rcu.sync_lock.locked);
403 /*
404 * Release the futex to avoid deadlocks in singlethreaded apps
405 * but keep sync locked.
406 */
407 fibril_rmutex_unlock(&rcu.sync_lock.mutex);
408 fibril_usleep(RCU_SLEEP_MS * 1000);
409 fibril_rmutex_lock(&rcu.sync_lock.mutex);
410}
411
412
413static bool is_preexisting_reader(const fibril_rcu_data_t *fib, size_t group)
414{
415 size_t nesting_cnt = ACCESS_ONCE(fib->nesting_cnt);
416
417 return is_in_group(nesting_cnt, group) && is_in_reader_section(nesting_cnt);
418}
419
420static size_t get_other_group(size_t group)
421{
422 if (group == RCU_GROUP_A)
423 return RCU_GROUP_B;
424 else
425 return RCU_GROUP_A;
426}
427
428static bool is_in_reader_section(size_t nesting_cnt)
429{
430 return RCU_NESTING_INC <= nesting_cnt;
431}
432
433static bool is_in_group(size_t nesting_cnt, size_t group)
434{
435 return (nesting_cnt & RCU_GROUP_BIT_MASK) == (group & RCU_GROUP_BIT_MASK);
436}
437
438
439
440/** @}
441 */
Note: See TracBrowser for help on using the repository browser.