source: mainline/uspace/lib/c/generic/rcu.c@ 42f5860

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

Clean up userspace RCU.

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