source: mainline/uspace/lib/c/generic/rcu.c@ 9b1baac

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

Privatize <thread.h>.

  • Property mode set to 100644
File size: 12.9 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 <futex.h>
76#include <macros.h>
77#include <async.h>
78#include <adt/list.h>
79#include <smp_memory_barrier.h>
80#include <assert.h>
81#include <time.h>
82
83#include "private/fibril.h"
84
85
86/** RCU sleeps for RCU_SLEEP_MS before polling an active RCU reader again. */
87#define RCU_SLEEP_MS 10
88
89#define RCU_NESTING_SHIFT 1
90#define RCU_NESTING_INC (1 << RCU_NESTING_SHIFT)
91#define RCU_GROUP_BIT_MASK (size_t)(RCU_NESTING_INC - 1)
92#define RCU_GROUP_A (size_t)(0 | RCU_NESTING_INC)
93#define RCU_GROUP_B (size_t)(1 | RCU_NESTING_INC)
94
95
96/** Fibril local RCU data. */
97typedef struct fibril_rcu_data {
98 size_t nesting_cnt;
99 link_t link;
100 bool registered;
101} fibril_rcu_data_t;
102
103/** Process global RCU data. */
104typedef struct rcu_data {
105 size_t cur_gp;
106 size_t reader_group;
107 futex_t list_futex;
108 list_t fibrils_list;
109 struct {
110 futex_t futex;
111 bool locked;
112 list_t blocked_fibrils;
113 size_t blocked_thread_cnt;
114 futex_t futex_blocking_threads;
115 } sync_lock;
116} rcu_data_t;
117
118typedef struct blocked_fibril {
119 fid_t id;
120 link_t link;
121 bool is_ready;
122} blocked_fibril_t;
123
124
125/** Fibril local RCU data. */
126static fibril_local fibril_rcu_data_t fibril_rcu = {
127 .nesting_cnt = 0,
128 .link = {
129 .next = NULL,
130 .prev = NULL
131 },
132 .registered = false
133};
134
135/** Process global RCU data. */
136static rcu_data_t rcu = {
137 .cur_gp = 0,
138 .reader_group = RCU_GROUP_A,
139 .list_futex = FUTEX_INITIALIZER,
140 .fibrils_list = LIST_INITIALIZER(rcu.fibrils_list),
141 .sync_lock = {
142 .futex = FUTEX_INITIALIZER,
143 .locked = false,
144 .blocked_fibrils = LIST_INITIALIZER(rcu.sync_lock.blocked_fibrils),
145 .blocked_thread_cnt = 0,
146 .futex_blocking_threads = FUTEX_INITIALIZE(0),
147 },
148};
149
150
151static void wait_for_readers(size_t reader_group);
152static void force_mb_in_all_threads(void);
153static bool is_preexisting_reader(const fibril_rcu_data_t *fib, size_t group);
154
155static void lock_sync(void);
156static void unlock_sync(void);
157static void sync_sleep(void);
158
159static bool is_in_group(size_t nesting_cnt, size_t group);
160static bool is_in_reader_section(size_t nesting_cnt);
161static size_t get_other_group(size_t group);
162
163
164/** Registers a fibril so it may start using RCU read sections.
165 *
166 * A fibril must be registered with rcu before it can enter RCU critical
167 * sections delineated by rcu_read_lock() and rcu_read_unlock().
168 */
169void rcu_register_fibril(void)
170{
171 assert(!fibril_rcu.registered);
172
173 futex_lock(&rcu.list_futex);
174 list_append(&fibril_rcu.link, &rcu.fibrils_list);
175 futex_unlock(&rcu.list_futex);
176
177 fibril_rcu.registered = true;
178}
179
180/** Deregisters a fibril that had been using RCU read sections.
181 *
182 * A fibril must be deregistered before it exits if it had
183 * been registered with rcu via rcu_register_fibril().
184 */
185void rcu_deregister_fibril(void)
186{
187 assert(fibril_rcu.registered);
188
189 /*
190 * Forcefully unlock any reader sections. The fibril is exiting
191 * so it is not holding any references to data protected by the
192 * rcu section. Therefore, it is safe to unlock. Otherwise,
193 * rcu_synchronize() would wait indefinitely.
194 */
195 memory_barrier();
196 fibril_rcu.nesting_cnt = 0;
197
198 futex_lock(&rcu.list_futex);
199 list_remove(&fibril_rcu.link);
200 futex_unlock(&rcu.list_futex);
201
202 fibril_rcu.registered = false;
203}
204
205/** Delimits the start of an RCU reader critical section.
206 *
207 * RCU reader sections may be nested.
208 */
209void rcu_read_lock(void)
210{
211 assert(fibril_rcu.registered);
212
213 size_t nesting_cnt = ACCESS_ONCE(fibril_rcu.nesting_cnt);
214
215 if (0 == (nesting_cnt >> RCU_NESTING_SHIFT)) {
216 ACCESS_ONCE(fibril_rcu.nesting_cnt) = ACCESS_ONCE(rcu.reader_group);
217 /* Required by MB_FORCE_L */
218 compiler_barrier(); /* CC_BAR_L */
219 } else {
220 ACCESS_ONCE(fibril_rcu.nesting_cnt) = nesting_cnt + RCU_NESTING_INC;
221 }
222}
223
224/** Delimits the start of an RCU reader critical section. */
225void rcu_read_unlock(void)
226{
227 assert(fibril_rcu.registered);
228 assert(rcu_read_locked());
229
230 /* Required by MB_FORCE_U */
231 compiler_barrier(); /* CC_BAR_U */
232 /* todo: ACCESS_ONCE(nesting_cnt) ? */
233 fibril_rcu.nesting_cnt -= RCU_NESTING_INC;
234}
235
236/** Returns true if the current fibril is in an RCU reader section. */
237bool rcu_read_locked(void)
238{
239 return 0 != (fibril_rcu.nesting_cnt >> RCU_NESTING_SHIFT);
240}
241
242/** Blocks until all preexisting readers exit their critical sections. */
243void rcu_synchronize(void)
244{
245 assert(!rcu_read_locked());
246
247 /* Contain load of rcu.cur_gp. */
248 memory_barrier();
249
250 /* Approximately the number of the GP in progress. */
251 size_t gp_in_progress = ACCESS_ONCE(rcu.cur_gp);
252
253 lock_sync();
254
255 /*
256 * Exit early if we were stuck waiting for the mutex for a full grace
257 * period. Started waiting during gp_in_progress (or gp_in_progress + 1
258 * if the value propagated to this cpu too late) so wait for the next
259 * full GP, gp_in_progress + 1, to finish. Ie don't wait if the GP
260 * after that, gp_in_progress + 2, already started.
261 */
262 /* rcu.cur_gp >= gp_in_progress + 2, but tolerates overflows. */
263 if (rcu.cur_gp != gp_in_progress && rcu.cur_gp + 1 != gp_in_progress) {
264 unlock_sync();
265 return;
266 }
267
268 ++ACCESS_ONCE(rcu.cur_gp);
269
270 /*
271 * Pairs up with MB_FORCE_L (ie CC_BAR_L). Makes changes prior
272 * to rcu_synchronize() visible to new readers.
273 */
274 memory_barrier(); /* MB_A */
275
276 /*
277 * Pairs up with MB_A.
278 *
279 * If the memory barrier is issued before CC_BAR_L in the target
280 * thread, it pairs up with MB_A and the thread sees all changes
281 * prior to rcu_synchronize(). Ie any reader sections are new
282 * rcu readers.
283 *
284 * If the memory barrier is issued after CC_BAR_L, it pairs up
285 * with MB_B and it will make the most recent nesting_cnt visible
286 * in this thread. Since the reader may have already accessed
287 * memory protected by RCU (it ran instructions passed CC_BAR_L),
288 * it is a preexisting reader. Seeing the most recent nesting_cnt
289 * ensures the thread will be identified as a preexisting reader
290 * and we will wait for it in wait_for_readers(old_reader_group).
291 */
292 force_mb_in_all_threads(); /* MB_FORCE_L */
293
294 /*
295 * Pairs with MB_FORCE_L (ie CC_BAR_L, CC_BAR_U) and makes the most
296 * current fibril.nesting_cnt visible to this cpu.
297 */
298 read_barrier(); /* MB_B */
299
300 size_t new_reader_group = get_other_group(rcu.reader_group);
301 wait_for_readers(new_reader_group);
302
303 /* Separates waiting for readers in new_reader_group from group flip. */
304 memory_barrier();
305
306 /* Flip the group new readers should associate with. */
307 size_t old_reader_group = rcu.reader_group;
308 rcu.reader_group = new_reader_group;
309
310 /* Flip the group before waiting for preexisting readers in the old group.*/
311 memory_barrier();
312
313 wait_for_readers(old_reader_group);
314
315 /* MB_FORCE_U */
316 force_mb_in_all_threads(); /* MB_FORCE_U */
317
318 unlock_sync();
319}
320
321/** Issues a memory barrier in each thread of this process. */
322static void force_mb_in_all_threads(void)
323{
324 /*
325 * Only issue barriers in running threads. The scheduler will
326 * execute additional memory barriers when switching to threads
327 * of the process that are currently not running.
328 */
329 smp_memory_barrier();
330}
331
332/** Waits for readers of reader_group to exit their readers sections. */
333static void wait_for_readers(size_t reader_group)
334{
335 futex_lock(&rcu.list_futex);
336
337 list_t quiescent_fibrils;
338 list_initialize(&quiescent_fibrils);
339
340 while (!list_empty(&rcu.fibrils_list)) {
341 list_foreach_safe(rcu.fibrils_list, fibril_it, next_fibril) {
342 fibril_rcu_data_t *fib = member_to_inst(fibril_it,
343 fibril_rcu_data_t, link);
344
345 if (is_preexisting_reader(fib, reader_group)) {
346 futex_unlock(&rcu.list_futex);
347 sync_sleep();
348 futex_lock(&rcu.list_futex);
349 /* Break to while loop. */
350 break;
351 } else {
352 list_remove(fibril_it);
353 list_append(fibril_it, &quiescent_fibrils);
354 }
355 }
356 }
357
358 list_concat(&rcu.fibrils_list, &quiescent_fibrils);
359 futex_unlock(&rcu.list_futex);
360}
361
362static void lock_sync(void)
363{
364 futex_lock(&rcu.sync_lock.futex);
365 if (rcu.sync_lock.locked) {
366 blocked_fibril_t blocked_fib;
367 blocked_fib.id = fibril_get_id();
368
369 list_append(&blocked_fib.link, &rcu.sync_lock.blocked_fibrils);
370
371 do {
372 blocked_fib.is_ready = false;
373 futex_unlock(&rcu.sync_lock.futex);
374 futex_lock(&async_futex);
375 fibril_switch(FIBRIL_FROM_BLOCKED);
376 futex_unlock(&async_futex);
377 futex_lock(&rcu.sync_lock.futex);
378 } while (rcu.sync_lock.locked);
379
380 list_remove(&blocked_fib.link);
381 rcu.sync_lock.locked = true;
382 } else {
383 rcu.sync_lock.locked = true;
384 }
385}
386
387static void unlock_sync(void)
388{
389 assert(rcu.sync_lock.locked);
390
391 /*
392 * Blocked threads have a priority over fibrils when accessing sync().
393 * Pass the lock onto a waiting thread.
394 */
395 if (0 < rcu.sync_lock.blocked_thread_cnt) {
396 --rcu.sync_lock.blocked_thread_cnt;
397 futex_unlock(&rcu.sync_lock.futex_blocking_threads);
398 } else {
399 /* Unlock but wake up any fibrils waiting for the lock. */
400
401 if (!list_empty(&rcu.sync_lock.blocked_fibrils)) {
402 blocked_fibril_t *blocked_fib = member_to_inst(
403 list_first(&rcu.sync_lock.blocked_fibrils), blocked_fibril_t, link);
404
405 if (!blocked_fib->is_ready) {
406 blocked_fib->is_ready = true;
407 fibril_add_ready(blocked_fib->id);
408 }
409 }
410
411 rcu.sync_lock.locked = false;
412 futex_unlock(&rcu.sync_lock.futex);
413 }
414}
415
416static void sync_sleep(void)
417{
418 assert(rcu.sync_lock.locked);
419 /*
420 * Release the futex to avoid deadlocks in singlethreaded apps
421 * but keep sync locked.
422 */
423 futex_unlock(&rcu.sync_lock.futex);
424 fibril_usleep(RCU_SLEEP_MS * 1000);
425 futex_lock(&rcu.sync_lock.futex);
426}
427
428
429static bool is_preexisting_reader(const fibril_rcu_data_t *fib, size_t group)
430{
431 size_t nesting_cnt = ACCESS_ONCE(fib->nesting_cnt);
432
433 return is_in_group(nesting_cnt, group) && is_in_reader_section(nesting_cnt);
434}
435
436static size_t get_other_group(size_t group)
437{
438 if (group == RCU_GROUP_A)
439 return RCU_GROUP_B;
440 else
441 return RCU_GROUP_A;
442}
443
444static bool is_in_reader_section(size_t nesting_cnt)
445{
446 return RCU_NESTING_INC <= nesting_cnt;
447}
448
449static bool is_in_group(size_t nesting_cnt, size_t group)
450{
451 return (nesting_cnt & RCU_GROUP_BIT_MASK) == (group & RCU_GROUP_BIT_MASK);
452}
453
454
455
456/** @}
457 */
Note: See TracBrowser for help on using the repository browser.