source: mainline/kernel/generic/src/adt/cht.c@ 0b7bcb8

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 0b7bcb8 was 0b7bcb8, checked in by Adam Hraska <adam.hraska+hos@…>, 13 years ago

cht: Slightly changed CHT interface. It now allows to specify the maximum desired load of the table before it resizes.

  • Property mode set to 100644
File size: 53.7 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
30/** @addtogroup genericadt
31 * @{
32 */
33
34/**
35 * @file
36 * @brief Scalable resizable concurrent lock-free hash table.
37 *
38 */
39
40#include <adt/cht.h>
41#include <adt/hash.h>
42#include <debug.h>
43#include <memstr.h>
44#include <mm/slab.h>
45#include <arch/barrier.h>
46#include <compiler/barrier.h>
47#include <atomic.h>
48#include <synch/rcu.h>
49
50
51/* Logarithm of the min bucket count. Must be at least 3. 2^6 == 64 buckets. */
52#define CHT_MIN_ORDER 6
53/* Logarithm of the max bucket count. */
54#define CHT_MAX_ORDER (8 * sizeof(size_t))
55/* Minimum number of hash table buckets. */
56#define CHT_MIN_BUCKET_CNT (1 << CHT_MIN_ORDER)
57/* Does not have to be a power of 2. */
58#define CHT_MAX_LOAD 2
59
60typedef cht_ptr_t marked_ptr_t;
61typedef bool (*equal_pred_t)(void *arg, const cht_link_t *item);
62
63typedef enum mark {
64 N_NORMAL = 0,
65 N_DELETED = 1,
66 N_CONST = 1,
67 N_INVALID = 3,
68 N_JOIN = 2,
69 N_JOIN_FOLLOWS = 2,
70 N_MARK_MASK = 3
71} mark_t;
72
73typedef enum walk_mode {
74 WM_NORMAL = 4,
75 WM_LEAVE_JOIN,
76 WM_MOVE_JOIN_FOLLOWS
77} walk_mode_t;
78
79typedef struct wnd {
80 marked_ptr_t *ppred;
81 cht_link_t *cur;
82 cht_link_t *last;
83} wnd_t;
84
85
86static size_t size_to_order(size_t bucket_cnt, size_t min_order);
87static cht_buckets_t *alloc_buckets(size_t order, bool set_invalid);
88static cht_link_t *search_bucket(cht_t *h, marked_ptr_t head, void *key,
89 size_t search_hash);
90static cht_link_t *find_resizing(cht_t *h, void *key, size_t hash,
91 marked_ptr_t old_head, size_t old_idx);
92static bool insert_impl(cht_t *h, cht_link_t *item, bool unique);
93static bool insert_at(cht_link_t *item, const wnd_t *wnd, walk_mode_t walk_mode,
94 bool *resizing);
95static bool has_duplicates(cht_t *h, const cht_link_t *item, size_t hash,
96 const wnd_t *cwnd);
97static cht_link_t *find_duplicate(cht_t *h, const cht_link_t *item, size_t hash,
98 cht_link_t *start);
99static bool remove_pred(cht_t *h, size_t hash, equal_pred_t pred, void *pred_arg);
100static bool delete_at(cht_t *h, wnd_t *wnd, walk_mode_t walk_mode,
101 bool *deleted_but_gc, bool *resizing);
102static bool mark_deleted(cht_link_t *cur, walk_mode_t walk_mode, bool *resizing);
103static bool unlink_from_pred(wnd_t *wnd, walk_mode_t walk_mode, bool *resizing);
104static bool find_wnd_and_gc_pred(cht_t *h, size_t hash, walk_mode_t walk_mode,
105 equal_pred_t pred, void *pred_arg, wnd_t *wnd, bool *resizing);
106static bool find_wnd_and_gc(cht_t *h, size_t hash, walk_mode_t walk_mode,
107 wnd_t *wnd, bool *resizing);
108static bool gc_deleted_node(cht_t *h, walk_mode_t walk_mode, wnd_t *wnd,
109 bool *resizing);
110static bool join_completed(cht_t *h, const wnd_t *wnd);
111static void upd_resizing_head(cht_t *h, size_t hash, marked_ptr_t **phead,
112 bool *join_finishing, walk_mode_t *walk_mode);
113static void item_removed(cht_t *h);
114static void item_inserted(cht_t *h);
115static void free_later(cht_t *h, cht_link_t *item);
116static void help_head_move(marked_ptr_t *psrc_head, marked_ptr_t *pdest_head);
117static void start_head_move(marked_ptr_t *psrc_head);
118static void mark_const(marked_ptr_t *psrc_head);
119static void complete_head_move(marked_ptr_t *psrc_head, marked_ptr_t *pdest_head);
120static void split_bucket(cht_t *h, marked_ptr_t *psrc_head,
121 marked_ptr_t *pdest_head, size_t split_hash);
122static void mark_join_follows(cht_t *h, marked_ptr_t *psrc_head,
123 size_t split_hash, wnd_t *wnd);
124static void mark_join_node(cht_link_t *join_node);
125static void join_buckets(cht_t *h, marked_ptr_t *psrc_head,
126 marked_ptr_t *pdest_head, size_t split_hash);
127static void link_to_join_node(cht_t *h, marked_ptr_t *pdest_head,
128 cht_link_t *join_node, size_t split_hash);
129static void resize_table(work_t *arg);
130static void grow_table(cht_t *h);
131static void shrink_table(cht_t *h);
132static void cleanup_join_node(cht_t *h, marked_ptr_t *new_head);
133static void clear_join_and_gc(cht_t *h, cht_link_t *join_node,
134 marked_ptr_t *new_head);
135static void cleanup_join_follows(cht_t *h, marked_ptr_t *new_head);
136static marked_ptr_t make_link(const cht_link_t *next, mark_t mark);
137static cht_link_t * get_next(marked_ptr_t link);
138static mark_t get_mark(marked_ptr_t link);
139static void next_wnd(wnd_t *wnd);
140static bool same_node_pred(void *node, const cht_link_t *item2);
141static size_t key_hash(cht_t *h, void *key);
142static size_t node_hash(cht_t *h, const cht_link_t *item);
143static size_t calc_split_hash(size_t split_idx, size_t order);
144static size_t calc_bucket_idx(size_t hash, size_t order);
145static size_t grow_to_split_idx(size_t old_idx);
146static size_t grow_idx(size_t idx);
147static size_t shrink_idx(size_t idx);
148static marked_ptr_t cas_link(marked_ptr_t *link, const cht_link_t *cur_next,
149 mark_t cur_mark, const cht_link_t *new_next, mark_t new_mark);
150static marked_ptr_t _cas_link(marked_ptr_t *link, marked_ptr_t cur,
151 marked_ptr_t new);
152static void cas_order_barrier(void);
153
154
155bool cht_create(cht_t *h, size_t init_size, size_t min_size, size_t max_load,
156 cht_ops_t *op)
157{
158 ASSERT(h);
159 ASSERT(op && op->hash && op->key_hash && op->equal && op->key_equal);
160
161 /* All operations are compulsory. */
162 if (!op || !op->hash || !op->key_hash || !op->equal || !op->key_equal)
163 return false;
164
165 size_t min_order = size_to_order(min_size, CHT_MIN_ORDER);
166 size_t order = size_to_order(init_size, min_order);
167
168 h->b = alloc_buckets(order, false);
169
170 if (!h->b)
171 return false;
172
173 h->max_load = (max_load == 0) ? CHT_MAX_LOAD : max_load;
174 h->min_order = min_order;
175 h->new_b = 0;
176 h->op = op;
177 atomic_set(&h->item_cnt, 0);
178 atomic_set(&h->resize_reqs, 0);
179 /* Ensure the initialization takes place before we start using the table. */
180 write_barrier();
181
182 return true;
183}
184
185static cht_buckets_t *alloc_buckets(size_t order, bool set_invalid)
186{
187 size_t bucket_cnt = (1 << order);
188 size_t bytes =
189 sizeof(cht_buckets_t) + (bucket_cnt - 1) * sizeof(marked_ptr_t);
190 cht_buckets_t *b = malloc(bytes, FRAME_ATOMIC);
191
192 if (!b)
193 return 0;
194
195 b->order = order;
196
197 marked_ptr_t head_link
198 = set_invalid ? make_link(0, N_INVALID) : make_link(0, N_NORMAL);
199
200 for (size_t i = 0; i < bucket_cnt; ++i) {
201 b->head[i] = head_link;
202 }
203
204 return b;
205}
206
207static size_t size_to_order(size_t bucket_cnt, size_t min_order)
208{
209 size_t order = min_order;
210
211 /* Find a power of two such that bucket_cnt <= 2^order */
212 do {
213 if (bucket_cnt <= ((size_t)1 << order))
214 return order;
215
216 ++order;
217 } while (order < CHT_MAX_ORDER);
218
219 return order;
220}
221
222
223void cht_destroy(cht_t *h)
224{
225 /* Wait for resize to complete. */
226 while (0 < atomic_get(&h->resize_reqs)) {
227 rcu_barrier();
228 }
229
230 /* Wait for all remove_callback()s to complete. */
231 rcu_barrier();
232
233 free(h->b);
234 h->b = 0;
235}
236
237cht_link_t *cht_find(cht_t *h, void *key)
238{
239 /* Make the most recent changes to the table visible. */
240 read_barrier();
241 return cht_find_lazy(h, key);
242}
243
244
245cht_link_t *cht_find_lazy(cht_t *h, void *key)
246{
247 ASSERT(h);
248 ASSERT(rcu_read_locked());
249
250 size_t hash = key_hash(h, key);
251
252 cht_buckets_t *b = rcu_access(h->b);
253 size_t idx = calc_bucket_idx(hash, b->order);
254 /*
255 * No need for access_once. b->head[idx] will point to an allocated node
256 * even if marked invalid until we exit rcu read section.
257 */
258 marked_ptr_t head = b->head[idx];
259
260 if (N_INVALID == get_mark(head))
261 return find_resizing(h, key, hash, head, idx);
262
263 return search_bucket(h, head, key, hash);
264}
265
266cht_link_t *cht_find_next(cht_t *h, const cht_link_t *item)
267{
268 /* Make the most recent changes to the table visible. */
269 read_barrier();
270 return cht_find_next_lazy(h, item);
271}
272
273cht_link_t *cht_find_next_lazy(cht_t *h, const cht_link_t *item)
274{
275 ASSERT(h);
276 ASSERT(rcu_read_locked());
277 ASSERT(item);
278
279 return find_duplicate(h, item, node_hash(h, item), get_next(item->link));
280}
281
282static cht_link_t *search_bucket(cht_t *h, marked_ptr_t head, void *key,
283 size_t search_hash)
284{
285 cht_link_t *cur = get_next(head);
286
287 while (cur) {
288 /*
289 * It is safe to access nodes even outside of this bucket (eg when
290 * splitting the bucket). The resizer makes sure that any node we
291 * may find by following the next pointers is allocated.
292 */
293 size_t cur_hash = node_hash(h, cur);
294
295 if (cur_hash >= search_hash) {
296 if (cur_hash != search_hash)
297 return 0;
298
299 int present = !(N_DELETED & get_mark(cur->link));
300 if (present && h->op->key_equal(key, cur))
301 return cur;
302 }
303
304 cur = get_next(cur->link);
305 }
306
307 return 0;
308}
309
310static cht_link_t *find_resizing(cht_t *h, void *key, size_t hash,
311 marked_ptr_t old_head, size_t old_idx)
312{
313 ASSERT(N_INVALID == get_mark(old_head));
314 ASSERT(h->new_b);
315
316 size_t new_idx = calc_bucket_idx(hash, h->new_b->order);
317 marked_ptr_t new_head = h->new_b->head[new_idx];
318 marked_ptr_t search_head = new_head;
319
320 /* Growing. */
321 if (h->b->order < h->new_b->order) {
322 /*
323 * Old bucket head is invalid, so it must have been already
324 * moved. Make the new head visible if still not visible, ie
325 * invalid.
326 */
327 if (N_INVALID == get_mark(new_head)) {
328 /*
329 * We should be searching a newly added bucket but the old
330 * moved bucket has not yet been split (its marked invalid)
331 * or we have not yet seen the split.
332 */
333 if (grow_idx(old_idx) != new_idx) {
334 /*
335 * Search the moved bucket. It is guaranteed to contain
336 * items of the newly added bucket that were present
337 * before the moved bucket was split.
338 */
339 new_head = h->new_b->head[grow_idx(old_idx)];
340 }
341
342 /* new_head is now the moved bucket, either valid or invalid. */
343
344 /*
345 * The old bucket was definitely moved to new_head but the
346 * change of new_head had not yet propagated to this cpu.
347 */
348 if (N_INVALID == get_mark(new_head)) {
349 /*
350 * We could issue a read_barrier() and make the now valid
351 * moved bucket head new_head visible, but instead fall back
352 * on using the old bucket. Although the old bucket head is
353 * invalid, it points to a node that is allocated and in the
354 * right bucket. Before the node can be freed, it must be
355 * unlinked from the head (or another item after that item
356 * modified the new_head) and a grace period must elapse.
357 * As a result had the node been already freed the grace
358 * period preceeding the free() would make the unlink and
359 * any changes to new_head visible. Therefore, it is safe
360 * to use the node pointed to from the old bucket head.
361 */
362
363 search_head = old_head;
364 } else {
365 search_head = new_head;
366 }
367 }
368
369 return search_bucket(h, search_head, key, hash);
370 } else if (h->b->order > h->new_b->order) {
371 /* Shrinking. */
372
373 /* Index of the bucket in the old table that was moved. */
374 size_t move_src_idx = grow_idx(new_idx);
375 marked_ptr_t moved_old_head = h->b->head[move_src_idx];
376
377 /*
378 * h->b->head[move_src_idx] had already been moved to new_head
379 * but the change to new_head had not yet propagated to us.
380 */
381 if (N_INVALID == get_mark(new_head)) {
382 /*
383 * new_head is definitely valid and we could make it visible
384 * to this cpu with a read_barrier(). Instead, use the bucket
385 * in the old table that was moved even though it is now marked
386 * as invalid. The node it points to must be allocated because
387 * a grace period would have to elapse before it could be freed;
388 * and the grace period would make the now valid new_head
389 * visible to all cpus.
390 *
391 * Note that move_src_idx may not be the same as old_idx.
392 * If move_src_idx != old_idx then old_idx is the bucket
393 * in the old table that is not moved but instead it is
394 * appended to the moved bucket, ie it is added at the tail
395 * of new_head. In that case an invalid old_head notes that
396 * it had already been merged into (the moved) new_head.
397 * We will try to search that bucket first because it
398 * may contain some newly added nodes after the bucket
399 * join. Moreover, the bucket joining link may already be
400 * visible even if new_head is not. Therefore, if we're
401 * lucky we'll find the item via moved_old_head. In any
402 * case, we'll retry in proper old_head if not found.
403 */
404 search_head = moved_old_head;
405 }
406
407 cht_link_t *ret = search_bucket(h, search_head, key, hash);
408
409 if (ret)
410 return ret;
411 /*
412 * Bucket old_head was already joined with moved_old_head
413 * in the new table but we have not yet seen change of the
414 * joining link (or the item is not in the table).
415 */
416 if (move_src_idx != old_idx && get_next(old_head)) {
417 /*
418 * Note that old_head (the bucket to be merged into new_head)
419 * points to an allocated join node (if non-null) even if marked
420 * invalid. Before the resizer lets join nodes to be unlinked
421 * (and freed) it sets old_head to 0 and waits for a grace period.
422 * So either the invalid old_head points to join node; or old_head
423 * is null and we would have seen a completed bucket join while
424 * traversing search_head.
425 */
426 ASSERT(N_JOIN & get_mark(get_next(old_head)->link));
427 return search_bucket(h, old_head, key, hash);
428 }
429
430 return 0;
431 } else {
432 /*
433 * Resize is almost done. The resizer is waiting to make
434 * sure all cpus see that the new table replaced the old one.
435 */
436 ASSERT(h->b->order == h->new_b->order);
437 /*
438 * The resizer must ensure all new bucket heads are visible before
439 * replacing the old table.
440 */
441 ASSERT(N_NORMAL == get_mark(new_head));
442 return search_bucket(h, new_head, key, hash);
443 }
444}
445
446
447void cht_insert(cht_t *h, cht_link_t *item)
448{
449 insert_impl(h, item, false);
450}
451
452bool cht_insert_unique(cht_t *h, cht_link_t *item)
453{
454 return insert_impl(h, item, true);
455}
456
457static bool insert_impl(cht_t *h, cht_link_t *item, bool unique)
458{
459 rcu_read_lock();
460
461 cht_buckets_t *b = rcu_access(h->b);
462 size_t hash = node_hash(h, item);
463 size_t idx = calc_bucket_idx(hash, b->order);
464 marked_ptr_t *phead = &b->head[idx];
465
466 bool resizing = false;
467 bool inserted;
468
469 do {
470 walk_mode_t walk_mode = WM_NORMAL;
471 bool join_finishing;
472
473 resizing = resizing || (N_NORMAL != get_mark(*phead));
474
475 /* The table is resizing. Get the correct bucket head. */
476 if (resizing) {
477 upd_resizing_head(h, hash, &phead, &join_finishing, &walk_mode);
478 }
479
480 wnd_t wnd = {
481 .ppred = phead,
482 .cur = get_next(*phead),
483 .last = 0
484 };
485
486 if (!find_wnd_and_gc(h, hash, walk_mode, &wnd, &resizing)) {
487 /* Could not GC a node; or detected an unexpected resize. */
488 continue;
489 }
490
491 if (unique && has_duplicates(h, item, hash, &wnd)) {
492 rcu_read_unlock();
493 return false;
494 }
495
496 inserted = insert_at(item, &wnd, walk_mode, &resizing);
497 } while (!inserted);
498
499 rcu_read_unlock();
500
501 item_inserted(h);
502 return true;
503}
504
505static bool insert_at(cht_link_t *item, const wnd_t *wnd, walk_mode_t walk_mode,
506 bool *resizing)
507{
508 marked_ptr_t ret;
509
510 if (walk_mode == WM_NORMAL) {
511 item->link = make_link(wnd->cur, N_NORMAL);
512 /* Initialize the item before adding it to a bucket. */
513 memory_barrier();
514
515 /* Link a clean/normal predecessor to the item. */
516 ret = cas_link(wnd->ppred, wnd->cur, N_NORMAL, item, N_NORMAL);
517
518 if (ret == make_link(wnd->cur, N_NORMAL)) {
519 return true;
520 } else {
521 /* This includes an invalid head but not a const head. */
522 *resizing = ((N_JOIN_FOLLOWS | N_JOIN) & get_mark(ret));
523 return false;
524 }
525 } else if (walk_mode == WM_MOVE_JOIN_FOLLOWS) {
526 /* Move JOIN_FOLLOWS mark but filter out the DELETED mark. */
527 mark_t jf_mark = get_mark(*wnd->ppred) & N_JOIN_FOLLOWS;
528 item->link = make_link(wnd->cur, jf_mark);
529 /* Initialize the item before adding it to a bucket. */
530 memory_barrier();
531
532 /* Link the not-deleted predecessor to the item. Move its JF mark. */
533 ret = cas_link(wnd->ppred, wnd->cur, jf_mark, item, N_NORMAL);
534
535 return ret == make_link(wnd->cur, jf_mark);
536 } else {
537 ASSERT(walk_mode == WM_LEAVE_JOIN);
538
539 item->link = make_link(wnd->cur, N_NORMAL);
540 /* Initialize the item before adding it to a bucket. */
541 memory_barrier();
542
543 mark_t pred_mark = get_mark(*wnd->ppred);
544 /* If the predecessor is a join node it may be marked deleted.*/
545 mark_t exp_pred_mark = (N_JOIN & pred_mark) ? pred_mark : N_NORMAL;
546
547 ret = cas_link(wnd->ppred, wnd->cur, exp_pred_mark, item, exp_pred_mark);
548 return ret == make_link(wnd->cur, exp_pred_mark);
549 }
550}
551
552static bool has_duplicates(cht_t *h, const cht_link_t *item, size_t hash,
553 const wnd_t *wnd)
554{
555 ASSERT(0 == wnd->cur || hash <= node_hash(h, wnd->cur));
556
557 if (0 == wnd->cur || hash < node_hash(h, wnd->cur))
558 return false;
559
560 /*
561 * Load the most recent node marks. Otherwise we might pronounce a
562 * logically deleted node for a duplicate of the item just because
563 * the deleted node's DEL mark had not yet propagated to this cpu.
564 */
565 read_barrier();
566 return 0 != find_duplicate(h, item, hash, wnd->cur);
567}
568
569static cht_link_t *find_duplicate(cht_t *h, const cht_link_t *item, size_t hash,
570 cht_link_t *start)
571{
572 ASSERT(0 == start || hash <= node_hash(h, start));
573
574 cht_link_t *cur = start;
575
576 while (cur && node_hash(h, cur) == hash) {
577 bool deleted = (N_DELETED & get_mark(cur->link));
578
579 /* Skip logically deleted nodes. */
580 if (!deleted && h->op->equal(item, cur))
581 return cur;
582
583 cur = get_next(cur->link);
584 }
585
586 return 0;
587}
588
589size_t cht_remove_key(cht_t *h, void *key)
590{
591 ASSERT(h);
592
593 size_t hash = key_hash(h, key);
594 size_t removed = 0;
595
596 while (remove_pred(h, hash, h->op->key_equal, key))
597 ++removed;
598
599 return removed;
600}
601
602bool cht_remove_item(cht_t *h, cht_link_t *item)
603{
604 ASSERT(h);
605 ASSERT(item);
606
607 /*
608 * Even though we know the node we want to delete we must unlink it
609 * from the correct bucket and from a clean/normal predecessor. Therefore,
610 * we search for it again from the beginning of the correct bucket.
611 */
612 size_t hash = node_hash(h, item);
613 return remove_pred(h, hash, same_node_pred, item);
614}
615
616
617static bool remove_pred(cht_t *h, size_t hash, equal_pred_t pred, void *pred_arg)
618{
619 rcu_read_lock();
620
621 bool resizing = false;
622 bool deleted = false;
623 bool deleted_but_gc = false;
624
625 cht_buckets_t *b = rcu_access(h->b);
626 size_t idx = calc_bucket_idx(hash, b->order);
627 marked_ptr_t *phead = &b->head[idx];
628
629 do {
630 walk_mode_t walk_mode = WM_NORMAL;
631 bool join_finishing = false;
632
633 resizing = resizing || (N_NORMAL != get_mark(*phead));
634
635 /* The table is resizing. Get the correct bucket head. */
636 if (resizing) {
637 upd_resizing_head(h, hash, &phead, &join_finishing, &walk_mode);
638 }
639
640 wnd_t wnd = {
641 .ppred = phead,
642 .cur = get_next(*phead),
643 .last = 0
644 };
645
646 if (!find_wnd_and_gc_pred(
647 h, hash, walk_mode, pred, pred_arg, &wnd, &resizing)) {
648 /* Could not GC a node; or detected an unexpected resize. */
649 continue;
650 }
651
652 /*
653 * The item lookup is affected by a bucket join but effects of
654 * the bucket join have not been seen while searching for the item.
655 */
656 if (join_finishing && !join_completed(h, &wnd)) {
657 /*
658 * Bucket was appended at the end of another but the next
659 * ptr linking them together was not visible on this cpu.
660 * join_completed() makes this appended bucket visible.
661 */
662 continue;
663 }
664
665 /* Already deleted, but delete_at() requested one GC pass. */
666 if (deleted_but_gc)
667 break;
668
669 bool found = wnd.cur && pred(pred_arg, wnd.cur);
670
671 if (!found) {
672 rcu_read_unlock();
673 return false;
674 }
675
676 deleted = delete_at(h, &wnd, walk_mode, &deleted_but_gc, &resizing);
677 } while (!deleted || deleted_but_gc);
678
679 rcu_read_unlock();
680 return true;
681}
682
683
684static bool delete_at(cht_t *h, wnd_t *wnd, walk_mode_t walk_mode,
685 bool *deleted_but_gc, bool *resizing)
686{
687 ASSERT(wnd->cur);
688
689 *deleted_but_gc = false;
690
691 if (!mark_deleted(wnd->cur, walk_mode, resizing)) {
692 /* Already deleted, or unexpectedly marked as JOIN/JOIN_FOLLOWS. */
693 return false;
694 }
695
696 /* Marked deleted. Unlink from the bucket. */
697
698 /* Never unlink join nodes. */
699 if (walk_mode == WM_LEAVE_JOIN && (N_JOIN & get_mark(wnd->cur->link)))
700 return true;
701
702 cas_order_barrier();
703
704 if (unlink_from_pred(wnd, walk_mode, resizing)) {
705 free_later(h, wnd->cur);
706 } else {
707 *deleted_but_gc = true;
708 }
709
710 return true;
711}
712
713static bool mark_deleted(cht_link_t *cur, walk_mode_t walk_mode, bool *resizing)
714{
715 ASSERT(cur);
716
717 /*
718 * Btw, we could loop here if the cas fails but let's not complicate
719 * things and let's retry from the head of the bucket.
720 */
721
722 cht_link_t *next = get_next(cur->link);
723
724 if (walk_mode == WM_NORMAL) {
725 /* Only mark clean/normal nodes - JF/JN is used only during resize. */
726 marked_ptr_t ret = cas_link(&cur->link, next, N_NORMAL, next, N_DELETED);
727
728 if (ret != make_link(next, N_NORMAL)) {
729 *resizing = (N_JOIN | N_JOIN_FOLLOWS) & get_mark(ret);
730 return false;
731 }
732 } else {
733 ASSERT(N_JOIN == N_JOIN_FOLLOWS);
734
735 /* Keep the N_JOIN/N_JOIN_FOLLOWS mark but strip N_DELETED. */
736 mark_t cur_mark = get_mark(cur->link) & N_JOIN_FOLLOWS;
737
738 marked_ptr_t ret =
739 cas_link(&cur->link, next, cur_mark, next, cur_mark | N_DELETED);
740
741 if (ret != make_link(next, cur_mark))
742 return false;
743 }
744
745 return true;
746}
747
748static bool unlink_from_pred(wnd_t *wnd, walk_mode_t walk_mode, bool *resizing)
749{
750 ASSERT(wnd->cur && (N_DELETED & get_mark(wnd->cur->link)));
751
752 cht_link_t *next = get_next(wnd->cur->link);
753
754 if (walk_mode == WM_LEAVE_JOIN) {
755 /* Never try to unlink join nodes. */
756 ASSERT(!(N_JOIN & get_mark(wnd->cur->link)));
757
758 mark_t pred_mark = get_mark(*wnd->ppred);
759 /* Succeed only if the predecessor is clean/normal or a join node. */
760 mark_t exp_pred_mark = (N_JOIN & pred_mark) ? pred_mark : N_NORMAL;
761
762 marked_ptr_t pred_link = make_link(wnd->cur, exp_pred_mark);
763 marked_ptr_t next_link = make_link(next, exp_pred_mark);
764
765 if (pred_link != _cas_link(wnd->ppred, pred_link, next_link))
766 return false;
767 } else {
768 ASSERT(walk_mode == WM_MOVE_JOIN_FOLLOWS || walk_mode == WM_NORMAL);
769 /* Move the JF mark if set. Clear DEL mark. */
770 mark_t cur_mark = N_JOIN_FOLLOWS & get_mark(wnd->cur->link);
771
772 /* The predecessor must be clean/normal. */
773 marked_ptr_t pred_link = make_link(wnd->cur, N_NORMAL);
774 /* Link to cur's successor keeping/copying cur's JF mark. */
775 marked_ptr_t next_link = make_link(next, cur_mark);
776
777 marked_ptr_t ret = _cas_link(wnd->ppred, pred_link, next_link);
778
779 if (pred_link != ret) {
780 /* If we're not resizing the table there are no JF/JN nodes. */
781 *resizing = (walk_mode == WM_NORMAL)
782 && (N_JOIN_FOLLOWS & get_mark(ret));
783 return false;
784 }
785 }
786
787 return true;
788}
789
790
791static bool find_wnd_and_gc_pred(cht_t *h, size_t hash, walk_mode_t walk_mode,
792 equal_pred_t pred, void *pred_arg, wnd_t *wnd, bool *resizing)
793{
794 if (!wnd->cur)
795 return true;
796
797 /*
798 * A read barrier is not needed here to bring up the most recent
799 * node marks (esp the N_DELETED). At worst we'll try to delete
800 * an already deleted node; fail in delete_at(); and retry.
801 */
802
803 size_t cur_hash = node_hash(h, wnd->cur);
804
805 while (cur_hash <= hash) {
806 /* GC any deleted nodes on the way. */
807 if (N_DELETED & get_mark(wnd->cur->link)) {
808 if (!gc_deleted_node(h, walk_mode, wnd, resizing)) {
809 /* Retry from the head of a bucket. */
810 return false;
811 }
812 } else {
813 /* Is this the node we were looking for? */
814 if (cur_hash == hash && pred(pred_arg, wnd->cur))
815 return true;
816
817 next_wnd(wnd);
818 }
819
820 /* The searched for node is not in the current bucket. */
821 if (!wnd->cur)
822 return true;
823
824 cur_hash = node_hash(h, wnd->cur);
825 }
826
827 /* The searched for node is not in the current bucket. */
828 return true;
829}
830
831/* todo: comment different semantics (eg deleted JN first w/ specific hash) */
832static bool find_wnd_and_gc(cht_t *h, size_t hash, walk_mode_t walk_mode,
833 wnd_t *wnd, bool *resizing)
834{
835 while (wnd->cur && node_hash(h, wnd->cur) < hash) {
836 /* GC any deleted nodes along the way to our desired node. */
837 if (N_DELETED & get_mark(wnd->cur->link)) {
838 if (!gc_deleted_node(h, walk_mode, wnd, resizing)) {
839 /* Failed to remove the garbage node. Retry. */
840 return false;
841 }
842 } else {
843 next_wnd(wnd);
844 }
845 }
846
847 /* wnd->cur may be 0 or even marked N_DELETED. */
848 return true;
849}
850
851static bool gc_deleted_node(cht_t *h, walk_mode_t walk_mode, wnd_t *wnd,
852 bool *resizing)
853{
854 ASSERT(N_DELETED & get_mark(wnd->cur->link));
855
856 /* Skip deleted JOIN nodes. */
857 if (walk_mode == WM_LEAVE_JOIN && (N_JOIN & get_mark(wnd->cur->link))) {
858 next_wnd(wnd);
859 } else {
860 /* Ordinary deleted node or a deleted JOIN_FOLLOWS. */
861 ASSERT(walk_mode != WM_LEAVE_JOIN
862 || !((N_JOIN | N_JOIN_FOLLOWS) & get_mark(wnd->cur->link)));
863
864 /* Unlink an ordinary deleted node, move JOIN_FOLLOWS mark. */
865 if (!unlink_from_pred(wnd, walk_mode, resizing)) {
866 /* Retry. The predecessor was deleted, invalid, const, join_follows. */
867 return false;
868 }
869
870 free_later(h, wnd->cur);
871
872 /* Leave ppred as is. */
873 wnd->last = wnd->cur;
874 wnd->cur = get_next(wnd->cur->link);
875 }
876
877 return true;
878}
879
880static bool join_completed(cht_t *h, const wnd_t *wnd)
881{
882 /*
883 * The table is shrinking and the searched for item is in a bucket
884 * appended to another. Check that the link joining these two buckets
885 * is visible and if not, make it visible to this cpu.
886 */
887
888 /*
889 * Resizer ensures h->b->order stays the same for the duration of this
890 * func. We got here because there was an alternative head to search.
891 * The resizer waits for all preexisting readers to finish after
892 * it
893 */
894 ASSERT(h->b->order > h->new_b->order);
895
896 /* Either we did not need the joining link or we have already followed it.*/
897 if (wnd->cur)
898 return true;
899
900 /* We have reached the end of a bucket. */
901
902 if (wnd->last) {
903 size_t last_seen_hash = node_hash(h, wnd->last);
904 size_t last_old_idx = calc_bucket_idx(last_seen_hash, h->b->order);
905 size_t move_src_idx = grow_idx(shrink_idx(last_old_idx));
906
907 /*
908 * Last was in the joining bucket - if the searched for node is there
909 * we will find it.
910 */
911 if (move_src_idx != last_old_idx)
912 return true;
913 }
914
915 /*
916 * Reached the end of the bucket but no nodes from the joining bucket
917 * were seen. There should have at least been a JOIN node so we have
918 * definitely not seen (and followed) the joining link. Make the link
919 * visible and retry.
920 */
921 read_barrier();
922 return false;
923}
924
925static void upd_resizing_head(cht_t *h, size_t hash, marked_ptr_t **phead,
926 bool *join_finishing, walk_mode_t *walk_mode)
927{
928 cht_buckets_t *b = rcu_access(h->b);
929 size_t old_idx = calc_bucket_idx(hash, b->order);
930 size_t new_idx = calc_bucket_idx(hash, h->new_b->order);
931
932 marked_ptr_t *pold_head = &b->head[old_idx];
933 marked_ptr_t *pnew_head = &h->new_b->head[new_idx];
934
935 /* In any case, use the bucket in the new table. */
936 *phead = pnew_head;
937
938 /* Growing the table. */
939 if (b->order < h->new_b->order) {
940 size_t move_dest_idx = grow_idx(old_idx);
941 marked_ptr_t *pmoved_head = &h->new_b->head[move_dest_idx];
942
943 /* Complete moving the bucket from the old to the new table. */
944 help_head_move(pold_head, pmoved_head);
945
946 /* The hash belongs to the moved bucket. */
947 if (move_dest_idx == new_idx) {
948 ASSERT(pmoved_head == pnew_head);
949 /*
950 * move_head() makes the new head of the moved bucket visible.
951 * The new head may be marked with a JOIN_FOLLOWS
952 */
953 ASSERT(!(N_CONST & get_mark(*pmoved_head)));
954 *walk_mode = WM_MOVE_JOIN_FOLLOWS;
955 } else {
956 ASSERT(pmoved_head != pnew_head);
957 /*
958 * The hash belongs to the bucket that is the result of splitting
959 * the old/moved bucket, ie the bucket that contains the second
960 * half of the split/old/moved bucket.
961 */
962
963 /* The moved bucket has not yet been split. */
964 if (N_NORMAL != get_mark(*pnew_head)) {
965 size_t split_hash = calc_split_hash(new_idx, h->new_b->order);
966 split_bucket(h, pmoved_head, pnew_head, split_hash);
967 /*
968 * split_bucket() makes the new head visible. No
969 * JOIN_FOLLOWS in this part of split bucket.
970 */
971 ASSERT(N_NORMAL == get_mark(*pnew_head));
972 }
973
974 *walk_mode = WM_LEAVE_JOIN;
975 }
976 } else if (h->new_b->order < b->order ) {
977 /* Shrinking the table. */
978
979 size_t move_src_idx = grow_idx(new_idx);
980
981 /*
982 * Complete moving the bucket from the old to the new table.
983 * Makes a valid pnew_head visible if already moved.
984 */
985 help_head_move(&b->head[move_src_idx], pnew_head);
986
987 /* Hash belongs to the bucket to be joined with the moved bucket. */
988 if (move_src_idx != old_idx) {
989 /* Bucket join not yet completed. */
990 if (N_INVALID != get_mark(*pold_head)) {
991 size_t split_hash = calc_split_hash(old_idx, b->order);
992 join_buckets(h, pold_head, pnew_head, split_hash);
993 }
994
995 /* The resizer sets pold_head to 0 when all cpus see the bucket join.*/
996 *join_finishing = (0 != get_next(*pold_head));
997 }
998
999 /* move_head() or join_buckets() makes it so or makes the mark visible.*/
1000 ASSERT(N_INVALID == get_mark(*pold_head));
1001 /* move_head() makes it visible. No JOIN_FOLLOWS used when shrinking. */
1002 ASSERT(N_NORMAL == get_mark(*pnew_head));
1003
1004 *walk_mode = WM_LEAVE_JOIN;
1005 } else {
1006 /*
1007 * Final stage of resize. The resizer is waiting for all
1008 * readers to notice that the old table had been replaced.
1009 */
1010 ASSERT(b == h->new_b);
1011 *walk_mode = WM_NORMAL;
1012 }
1013}
1014
1015
1016#if 0
1017static void move_head(marked_ptr_t *psrc_head, marked_ptr_t *pdest_head)
1018{
1019 start_head_move(psrc_head);
1020 cas_order_barrier();
1021 complete_head_move(psrc_head, pdest_head);
1022}
1023#endif
1024
1025static void help_head_move(marked_ptr_t *psrc_head, marked_ptr_t *pdest_head)
1026{
1027 /* Head move has to in progress already when calling this func. */
1028 ASSERT(N_CONST & get_mark(*psrc_head));
1029
1030 /* Head already moved. */
1031 if (N_INVALID == get_mark(*psrc_head)) {
1032 /* Effects of the head move have not yet propagated to this cpu. */
1033 if (N_INVALID == get_mark(*pdest_head)) {
1034 /* Make the move visible on this cpu. */
1035 read_barrier();
1036 }
1037 } else {
1038 complete_head_move(psrc_head, pdest_head);
1039 }
1040
1041 ASSERT(!(N_CONST & get_mark(*pdest_head)));
1042}
1043
1044static void start_head_move(marked_ptr_t *psrc_head)
1045{
1046 /* Mark src head immutable. */
1047 mark_const(psrc_head);
1048}
1049
1050static void mark_const(marked_ptr_t *psrc_head)
1051{
1052 marked_ptr_t ret, src_link;
1053
1054 /* Mark src head immutable. */
1055 do {
1056 cht_link_t *next = get_next(*psrc_head);
1057 src_link = make_link(next, N_NORMAL);
1058
1059 /* Mark the normal/clean src link immutable/const. */
1060 ret = cas_link(psrc_head, next, N_NORMAL, next, N_CONST);
1061 } while(ret != src_link && !(N_CONST & get_mark(ret)));
1062}
1063
1064static void complete_head_move(marked_ptr_t *psrc_head, marked_ptr_t *pdest_head)
1065{
1066 ASSERT(N_JOIN_FOLLOWS != get_mark(*psrc_head));
1067 ASSERT(N_CONST & get_mark(*psrc_head));
1068
1069 cht_link_t *next = get_next(*psrc_head);
1070 marked_ptr_t ret;
1071
1072 ret = cas_link(pdest_head, 0, N_INVALID, next, N_NORMAL);
1073 ASSERT(ret == make_link(0, N_INVALID) || (N_NORMAL == get_mark(ret)));
1074 cas_order_barrier();
1075
1076 ret = cas_link(psrc_head, next, N_CONST, next, N_INVALID);
1077 ASSERT(ret == make_link(next, N_CONST) || (N_INVALID == get_mark(ret)));
1078 cas_order_barrier();
1079}
1080
1081static void split_bucket(cht_t *h, marked_ptr_t *psrc_head,
1082 marked_ptr_t *pdest_head, size_t split_hash)
1083{
1084 /* Already split. */
1085 if (N_NORMAL == get_mark(*pdest_head))
1086 return;
1087
1088 /*
1089 * L == Last node of the first part of the split bucket. That part
1090 * remains in the original/src bucket.
1091 * F == First node of the second part of the split bucket. That part
1092 * will be referenced from the dest bucket head.
1093 *
1094 * We want to first mark a clean L as JF so that updaters unaware of
1095 * the split (or table resize):
1096 * - do not insert a new node between L and F
1097 * - do not unlink L (that is why it has to be clean/normal)
1098 * - do not unlink F
1099 *
1100 * Then we can safely mark F as JN even if it has been marked deleted.
1101 * Once F is marked as JN updaters aware of table resize will not
1102 * attempt to unlink it (JN will have two predecessors - we cannot
1103 * safely unlink from both at the same time). Updaters unaware of
1104 * ongoing resize can reach F only via L and that node is already
1105 * marked JF, so they won't unlink F.
1106 *
1107 * Last, link the new/dest head to F.
1108 *
1109 *
1110 * 0) ,-- split_hash, first hash of the dest bucket
1111 * v
1112 * [src_head | N] -> .. -> [L] -> [F]
1113 * [dest_head | Inv]
1114 *
1115 * 1) ,-- split_hash
1116 * v
1117 * [src_head | N] -> .. -> [JF] -> [F]
1118 * [dest_head | Inv]
1119 *
1120 * 2) ,-- split_hash
1121 * v
1122 * [src_head | N] -> .. -> [JF] -> [JN]
1123 * [dest_head | Inv]
1124 *
1125 * 2) ,-- split_hash
1126 * v
1127 * [src_head | N] -> .. -> [JF] -> [JN]
1128 * ^
1129 * [dest_head | N] -----------------'
1130 */
1131 wnd_t wnd;
1132
1133 rcu_read_lock();
1134
1135 /* Mark the last node of the first part of the split bucket as JF. */
1136 mark_join_follows(h, psrc_head, split_hash, &wnd);
1137 cas_order_barrier();
1138
1139 /* There are nodes in the dest bucket, ie the second part of the split. */
1140 if (wnd.cur) {
1141 /*
1142 * Mark the first node of the dest bucket as a join node so
1143 * updaters do not attempt to unlink it if it is deleted.
1144 */
1145 mark_join_node(wnd.cur);
1146 cas_order_barrier();
1147 } else {
1148 /*
1149 * Second part of the split bucket is empty. There are no nodes
1150 * to mark as JOIN nodes and there never will be.
1151 */
1152 }
1153
1154 /* Link the dest head to the second part of the split. */
1155 marked_ptr_t ret = cas_link(pdest_head, 0, N_INVALID, wnd.cur, N_NORMAL);
1156 ASSERT(ret == make_link(0, N_INVALID) || (N_NORMAL == get_mark(ret)));
1157 cas_order_barrier();
1158
1159 rcu_read_unlock();
1160}
1161
1162static void mark_join_follows(cht_t *h, marked_ptr_t *psrc_head,
1163 size_t split_hash, wnd_t *wnd)
1164{
1165 /* See comment in split_bucket(). */
1166
1167 bool done;
1168 do {
1169 bool resizing = false;
1170 wnd->ppred = psrc_head;
1171 wnd->cur = get_next(*psrc_head);
1172
1173 /*
1174 * Find the split window, ie the last node of the first part of
1175 * the split bucket and the its successor - the first node of
1176 * the second part of the split bucket. Retry if GC failed.
1177 */
1178 if (!find_wnd_and_gc(h, split_hash, WM_MOVE_JOIN_FOLLOWS, wnd, &resizing))
1179 continue;
1180
1181 /* Must not report that the table is resizing if WM_MOVE_JOIN_FOLLOWS.*/
1182 ASSERT(!resizing);
1183 /*
1184 * Mark the last node of the first half of the split bucket
1185 * that a join node follows. It must be clean/normal.
1186 */
1187 marked_ptr_t ret
1188 = cas_link(wnd->ppred, wnd->cur, N_NORMAL, wnd->cur, N_JOIN_FOLLOWS);
1189
1190 /*
1191 * Successfully marked as a JF node or already marked that way (even
1192 * if also marked deleted - unlinking the node will move the JF mark).
1193 */
1194 done = (ret == make_link(wnd->cur, N_NORMAL))
1195 || (N_JOIN_FOLLOWS & get_mark(ret));
1196 } while (!done);
1197}
1198
1199static void mark_join_node(cht_link_t *join_node)
1200{
1201 /* See comment in split_bucket(). */
1202
1203 bool done;
1204 do {
1205 cht_link_t *next = get_next(join_node->link);
1206 mark_t mark = get_mark(join_node->link);
1207
1208 /*
1209 * May already be marked as deleted, but it won't be unlinked
1210 * because its predecessor is marked with JOIN_FOLLOWS or CONST.
1211 */
1212 marked_ptr_t ret
1213 = cas_link(&join_node->link, next, mark, next, mark | N_JOIN);
1214
1215 /* Successfully marked or already marked as a join node. */
1216 done = (ret == make_link(next, mark))
1217 || (N_JOIN & get_mark(ret));
1218 } while(!done);
1219}
1220
1221
1222static void join_buckets(cht_t *h, marked_ptr_t *psrc_head,
1223 marked_ptr_t *pdest_head, size_t split_hash)
1224{
1225 /* Buckets already joined. */
1226 if (N_INVALID == get_mark(*psrc_head))
1227 return;
1228 /*
1229 * F == First node of psrc_head, ie the bucket we want to append
1230 * to (ie join with) the bucket starting at pdest_head.
1231 * L == Last node of pdest_head, ie the bucket that psrc_head will
1232 * be appended to.
1233 *
1234 * (1) We first mark psrc_head immutable to signal that a join is
1235 * in progress and so that updaters unaware of the join (or table
1236 * resize):
1237 * - do not insert new nodes between the head psrc_head and F
1238 * - do not unlink F (it may already be marked deleted)
1239 *
1240 * (2) Next, F is marked as a join node. Updaters aware of table resize
1241 * will not attempt to unlink it. We cannot safely/atomically unlink
1242 * the join node because it will be pointed to from two different
1243 * buckets. Updaters unaware of resize will fail to unlink the join
1244 * node due to the head being marked immutable.
1245 *
1246 * (3) Then the tail of the bucket at pdest_head is linked to the join
1247 * node. From now on, nodes in both buckets can be found via pdest_head.
1248 *
1249 * (4) Last, mark immutable psrc_head as invalid. It signals updaters
1250 * that the join is complete and they can insert new nodes (originally
1251 * destined for psrc_head) into pdest_head.
1252 *
1253 * Note that pdest_head keeps pointing at the join node. This allows
1254 * lookups and updaters to determine if they should see a link between
1255 * the tail L and F when searching for nodes originally in psrc_head
1256 * via pdest_head. If they reach the tail of pdest_head without
1257 * encountering any nodes of psrc_head, either there were no nodes
1258 * in psrc_head to begin with or the link between L and F did not
1259 * yet propagate to their cpus. If psrc_head was empty, it remains
1260 * NULL. Otherwise psrc_head points to a join node (it will not be
1261 * unlinked until table resize completes) and updaters/lookups
1262 * should issue a read_barrier() to make the link [L]->[JN] visible.
1263 *
1264 * 0) ,-- split_hash, first hash of the src bucket
1265 * v
1266 * [dest_head | N]-> .. -> [L]
1267 * [src_head | N]--> [F] -> ..
1268 * ^
1269 * ` split_hash, first hash of the src bucket
1270 *
1271 * 1) ,-- split_hash
1272 * v
1273 * [dest_head | N]-> .. -> [L]
1274 * [src_head | C]--> [F] -> ..
1275 *
1276 * 2) ,-- split_hash
1277 * v
1278 * [dest_head | N]-> .. -> [L]
1279 * [src_head | C]--> [JN] -> ..
1280 *
1281 * 3) ,-- split_hash
1282 * v
1283 * [dest_head | N]-> .. -> [L] --+
1284 * v
1285 * [src_head | C]-------------> [JN] -> ..
1286 *
1287 * 4) ,-- split_hash
1288 * v
1289 * [dest_head | N]-> .. -> [L] --+
1290 * v
1291 * [src_head | Inv]-----------> [JN] -> ..
1292 */
1293
1294 rcu_read_lock();
1295
1296 /* Mark src_head immutable - signals updaters that bucket join started. */
1297 mark_const(psrc_head);
1298 cas_order_barrier();
1299
1300 cht_link_t *join_node = get_next(*psrc_head);
1301
1302 if (join_node) {
1303 mark_join_node(join_node);
1304 cas_order_barrier();
1305
1306 link_to_join_node(h, pdest_head, join_node, split_hash);
1307 cas_order_barrier();
1308 }
1309
1310 marked_ptr_t ret =
1311 cas_link(psrc_head, join_node, N_CONST, join_node, N_INVALID);
1312 ASSERT(ret == make_link(join_node, N_CONST) || (N_INVALID == get_mark(ret)));
1313 cas_order_barrier();
1314
1315 rcu_read_unlock();
1316}
1317
1318static void link_to_join_node(cht_t *h, marked_ptr_t *pdest_head,
1319 cht_link_t *join_node, size_t split_hash)
1320{
1321 bool done;
1322 do {
1323 wnd_t wnd = {
1324 .ppred = pdest_head,
1325 .cur = get_next(*pdest_head)
1326 };
1327
1328 bool resizing = false;
1329
1330 if (!find_wnd_and_gc(h, split_hash, WM_LEAVE_JOIN, &wnd, &resizing))
1331 continue;
1332
1333 ASSERT(!resizing);
1334
1335 if (wnd.cur) {
1336 /* Must be from the new appended bucket. */
1337 ASSERT(split_hash <= node_hash(h, wnd.cur));
1338 return;
1339 }
1340
1341 /* Reached the tail of pdest_head - link it to the join node. */
1342 marked_ptr_t ret = cas_link(wnd.ppred, 0, N_NORMAL, join_node, N_NORMAL);
1343
1344 done = (ret == make_link(0, N_NORMAL));
1345 } while (!done);
1346}
1347
1348static void free_later(cht_t *h, cht_link_t *item)
1349{
1350 /*
1351 * remove_callback only works as rcu_func_t because rcu_link is the first
1352 * field in cht_link_t.
1353 */
1354 rcu_call(&item->rcu_link, (rcu_func_t)h->op->remove_callback);
1355
1356 item_removed(h);
1357}
1358
1359static void item_removed(cht_t *h)
1360{
1361 size_t items = (size_t) atomic_predec(&h->item_cnt);
1362 size_t bucket_cnt = (1 << h->b->order);
1363
1364 bool need_shrink = (items == h->max_load * bucket_cnt / 4);
1365 bool missed_shrink = (items == h->max_load * bucket_cnt / 8);
1366
1367 if ((need_shrink || missed_shrink) && h->b->order > h->min_order) {
1368 atomic_count_t resize_reqs = atomic_preinc(&h->resize_reqs);
1369 /* The first resize request. Start the resizer. */
1370 if (1 == resize_reqs) {
1371 workq_global_enqueue_noblock(&h->resize_work, resize_table);
1372 }
1373 }
1374}
1375
1376static void item_inserted(cht_t *h)
1377{
1378 size_t items = (size_t) atomic_preinc(&h->item_cnt);
1379 size_t bucket_cnt = (1 << h->b->order);
1380
1381 bool need_grow = (items == h->max_load * bucket_cnt);
1382 bool missed_grow = (items == 2 * h->max_load * bucket_cnt);
1383
1384 if ((need_grow || missed_grow) && h->b->order < CHT_MAX_ORDER) {
1385 atomic_count_t resize_reqs = atomic_preinc(&h->resize_reqs);
1386 /* The first resize request. Start the resizer. */
1387 if (1 == resize_reqs) {
1388 workq_global_enqueue_noblock(&h->resize_work, resize_table);
1389 }
1390 }
1391}
1392
1393static void resize_table(work_t *arg)
1394{
1395 cht_t *h = member_to_inst(arg, cht_t, resize_work);
1396
1397#ifdef CONFIG_DEBUG
1398 ASSERT(h->b);
1399 /* Make resize_reqs visible. */
1400 read_barrier();
1401 ASSERT(0 < atomic_get(&h->resize_reqs));
1402#endif
1403
1404 bool done;
1405 do {
1406 /* Load the most recent h->item_cnt. */
1407 read_barrier();
1408 size_t cur_items = (size_t) atomic_get(&h->item_cnt);
1409 size_t bucket_cnt = (1 << h->b->order);
1410 size_t max_items = h->max_load * bucket_cnt;
1411
1412 if (cur_items >= max_items && h->b->order < CHT_MAX_ORDER) {
1413 grow_table(h);
1414 } else if (cur_items <= max_items / 4 && h->b->order > h->min_order) {
1415 shrink_table(h);
1416 } else {
1417 /* Table is just the right size. */
1418 atomic_count_t reqs = atomic_predec(&h->resize_reqs);
1419 done = (reqs == 0);
1420 }
1421 } while (!done);
1422}
1423
1424static void grow_table(cht_t *h)
1425{
1426 if (h->b->order >= CHT_MAX_ORDER)
1427 return;
1428
1429 h->new_b = alloc_buckets(h->b->order + 1, true);
1430
1431 /* Failed to alloc a new table - try next time the resizer is run. */
1432 if (!h->new_b)
1433 return;
1434
1435 /* Wait for all readers and updaters to see the initialized new table. */
1436 rcu_synchronize();
1437 size_t old_bucket_cnt = (1 << h->b->order);
1438
1439 /*
1440 * Give updaters a chance to help out with the resize. Do the minimum
1441 * work needed to announce a resize is in progress, ie start moving heads.
1442 */
1443 for (size_t idx = 0; idx < old_bucket_cnt; ++idx) {
1444 start_head_move(&h->b->head[idx]);
1445 }
1446
1447 /* Order start_head_move() wrt complete_head_move(). */
1448 cas_order_barrier();
1449
1450 /* Complete moving heads and split any buckets not yet split by updaters. */
1451 for (size_t old_idx = 0; old_idx < old_bucket_cnt; ++old_idx) {
1452 marked_ptr_t *move_dest_head = &h->new_b->head[grow_idx(old_idx)];
1453 marked_ptr_t *move_src_head = &h->b->head[old_idx];
1454
1455 /* Head move not yet completed. */
1456 if (N_INVALID != get_mark(*move_src_head)) {
1457 complete_head_move(move_src_head, move_dest_head);
1458 }
1459
1460 size_t split_idx = grow_to_split_idx(old_idx);
1461 size_t split_hash = calc_split_hash(split_idx, h->new_b->order);
1462 marked_ptr_t *split_dest_head = &h->new_b->head[split_idx];
1463
1464 split_bucket(h, move_dest_head, split_dest_head, split_hash);
1465 }
1466
1467 /*
1468 * Wait for all updaters to notice the new heads. Once everyone sees
1469 * the invalid old bucket heads they will know a resize is in progress
1470 * and updaters will modify the correct new buckets.
1471 */
1472 rcu_synchronize();
1473
1474 /* Clear the JOIN_FOLLOWS mark and remove the link between the split buckets.*/
1475 for (size_t old_idx = 0; old_idx < old_bucket_cnt; ++old_idx) {
1476 size_t new_idx = grow_idx(old_idx);
1477
1478 cleanup_join_follows(h, &h->new_b->head[new_idx]);
1479 }
1480
1481 /*
1482 * Wait for everyone to notice that buckets were split, ie link connecting
1483 * the join follows and join node has been cut.
1484 */
1485 rcu_synchronize();
1486
1487 /* Clear the JOIN mark and GC any deleted join nodes. */
1488 for (size_t old_idx = 0; old_idx < old_bucket_cnt; ++old_idx) {
1489 size_t new_idx = grow_to_split_idx(old_idx);
1490
1491 cleanup_join_node(h, &h->new_b->head[new_idx]);
1492 }
1493
1494 /* Wait for everyone to see that the table is clear of any resize marks. */
1495 rcu_synchronize();
1496
1497 cht_buckets_t *old_b = h->b;
1498 rcu_assign(h->b, h->new_b);
1499
1500 /* Wait for everyone to start using the new table. */
1501 rcu_synchronize();
1502
1503 free(old_b);
1504
1505 /* Not needed; just for increased readability. */
1506 h->new_b = 0;
1507}
1508
1509static void shrink_table(cht_t *h)
1510{
1511 if (h->b->order <= h->min_order)
1512 return;
1513
1514 h->new_b = alloc_buckets(h->b->order - 1, true);
1515
1516 /* Failed to alloc a new table - try next time the resizer is run. */
1517 if (!h->new_b)
1518 return;
1519
1520 /* Wait for all readers and updaters to see the initialized new table. */
1521 rcu_synchronize();
1522
1523 size_t old_bucket_cnt = (1 << h->b->order);
1524
1525 /*
1526 * Give updaters a chance to help out with the resize. Do the minimum
1527 * work needed to announce a resize is in progress, ie start moving heads.
1528 */
1529 for (size_t old_idx = 0; old_idx < old_bucket_cnt; ++old_idx) {
1530 size_t new_idx = shrink_idx(old_idx);
1531
1532 /* This bucket should be moved. */
1533 if (grow_idx(new_idx) == old_idx) {
1534 start_head_move(&h->b->head[old_idx]);
1535 } else {
1536 /* This bucket should join the moved bucket once the move is done.*/
1537 }
1538 }
1539
1540 /* Order start_head_move() wrt to complete_head_move(). */
1541 cas_order_barrier();
1542
1543 /* Complete moving heads and join buckets with the moved buckets. */
1544 for (size_t old_idx = 0; old_idx < old_bucket_cnt; ++old_idx) {
1545 size_t new_idx = shrink_idx(old_idx);
1546 size_t move_src_idx = grow_idx(new_idx);
1547
1548 /* This bucket should be moved. */
1549 if (move_src_idx == old_idx) {
1550 /* Head move not yet completed. */
1551 if (N_INVALID != get_mark(h->b->head[old_idx])) {
1552 complete_head_move(&h->b->head[old_idx], &h->new_b->head[new_idx]);
1553 }
1554 } else {
1555 /* This bucket should join the moved bucket. */
1556 size_t split_hash = calc_split_hash(old_idx, h->b->order);
1557 join_buckets(h, &h->b->head[old_idx], &h->new_b->head[new_idx],
1558 split_hash);
1559 }
1560 }
1561
1562 /*
1563 * Wait for all updaters to notice the new heads. Once everyone sees
1564 * the invalid old bucket heads they will know a resize is in progress
1565 * and updaters will modify the correct new buckets.
1566 */
1567 rcu_synchronize();
1568
1569 /* Let everyone know joins are complete and fully visible. */
1570 for (size_t old_idx = 0; old_idx < old_bucket_cnt; ++old_idx) {
1571 size_t move_src_idx = grow_idx(shrink_idx(old_idx));
1572
1573 /* Set the invalid joinee head to NULL. */
1574 if (old_idx != move_src_idx) {
1575 ASSERT(N_INVALID == get_mark(h->b->head[old_idx]));
1576
1577 if (0 != get_next(h->b->head[old_idx]))
1578 h->b->head[old_idx] = make_link(0, N_INVALID);
1579 }
1580 }
1581
1582 /* todo comment join node vs reset joinee head*/
1583 rcu_synchronize();
1584
1585 size_t new_bucket_cnt = (1 << h->new_b->order);
1586
1587 /* Clear the JOIN mark and GC any deleted join nodes. */
1588 for (size_t new_idx = 0; new_idx < new_bucket_cnt; ++new_idx) {
1589 cleanup_join_node(h, &h->new_b->head[new_idx]);
1590 }
1591
1592 /* Wait for everyone to see that the table is clear of any resize marks. */
1593 rcu_synchronize();
1594
1595 cht_buckets_t *old_b = h->b;
1596 rcu_assign(h->b, h->new_b);
1597
1598 /* Wait for everyone to start using the new table. */
1599 rcu_synchronize();
1600
1601 free(old_b);
1602
1603 /* Not needed; just for increased readability. */
1604 h->new_b = 0;
1605}
1606
1607static void cleanup_join_node(cht_t *h, marked_ptr_t *new_head)
1608{
1609 rcu_read_lock();
1610
1611 cht_link_t *cur = get_next(*new_head);
1612
1613 while (cur) {
1614 /* Clear the join node's JN mark - even if it is marked as deleted. */
1615 if (N_JOIN & get_mark(cur->link)) {
1616 clear_join_and_gc(h, cur, new_head);
1617 break;
1618 }
1619
1620 cur = get_next(cur->link);
1621 }
1622
1623 rcu_read_unlock();
1624}
1625
1626static void clear_join_and_gc(cht_t *h, cht_link_t *join_node,
1627 marked_ptr_t *new_head)
1628{
1629 ASSERT(join_node && (N_JOIN & get_mark(join_node->link)));
1630
1631 bool done;
1632
1633 /* Clear the JN mark. */
1634 do {
1635 marked_ptr_t jn_link = join_node->link;
1636 cht_link_t *next = get_next(jn_link);
1637 /* Clear the JOIN mark but keep the DEL mark if present. */
1638 mark_t cleared_mark = get_mark(jn_link) & N_DELETED;
1639
1640 marked_ptr_t ret =
1641 _cas_link(&join_node->link, jn_link, make_link(next, cleared_mark));
1642
1643 /* Done if the mark was cleared. Retry if a new node was inserted. */
1644 done = (ret == jn_link);
1645 ASSERT(ret == jn_link || (get_mark(ret) & N_JOIN));
1646 } while (!done);
1647
1648 if (!(N_DELETED & get_mark(join_node->link)))
1649 return;
1650
1651 /* The join node had been marked as deleted - GC it. */
1652
1653 /* Clear the JOIN mark before trying to unlink the deleted join node.*/
1654 cas_order_barrier();
1655
1656 size_t jn_hash = node_hash(h, join_node);
1657 do {
1658 bool resizing = false;
1659
1660 wnd_t wnd = {
1661 .ppred = new_head,
1662 .cur = get_next(*new_head)
1663 };
1664
1665 done = find_wnd_and_gc_pred(h, jn_hash, WM_NORMAL, same_node_pred,
1666 join_node, &wnd, &resizing);
1667
1668 ASSERT(!resizing);
1669 } while (!done);
1670}
1671
1672static void cleanup_join_follows(cht_t *h, marked_ptr_t *new_head)
1673{
1674 ASSERT(new_head);
1675
1676 rcu_read_lock();
1677
1678 wnd_t wnd = {
1679 .ppred = 0,
1680 .cur = 0
1681 };
1682 marked_ptr_t *cur_link = new_head;
1683
1684 /*
1685 * Find the non-deleted node with a JF mark and clear the JF mark.
1686 * The JF node may be deleted and/or the mark moved to its neighbors
1687 * at any time. Therefore, we GC deleted nodes until we find the JF
1688 * node in order to remove stale/deleted JF nodes left behind eg by
1689 * delayed threads that did not yet get a chance to unlink the deleted
1690 * JF node and move its mark.
1691 *
1692 * Note that the head may be marked JF (but never DELETED).
1693 */
1694 while (true) {
1695 bool is_jf_node = N_JOIN_FOLLOWS & get_mark(*cur_link);
1696
1697 /* GC any deleted nodes on the way - even deleted JOIN_FOLLOWS. */
1698 if (N_DELETED & get_mark(*cur_link)) {
1699 ASSERT(cur_link != new_head);
1700 ASSERT(wnd.ppred && wnd.cur);
1701 ASSERT(cur_link == &wnd.cur->link);
1702
1703 bool dummy;
1704 bool deleted = gc_deleted_node(h, WM_MOVE_JOIN_FOLLOWS, &wnd, &dummy);
1705
1706 /* Failed to GC or collected a deleted JOIN_FOLLOWS. */
1707 if (!deleted || is_jf_node) {
1708 /* Retry from the head of the bucket. */
1709 cur_link = new_head;
1710 continue;
1711 }
1712 } else {
1713 /* Found a non-deleted JF. Clear its JF mark. */
1714 if (is_jf_node) {
1715 cht_link_t *next = get_next(*cur_link);
1716 marked_ptr_t ret
1717 = cas_link(cur_link, next, N_JOIN_FOLLOWS, 0, N_NORMAL);
1718
1719 ASSERT(!next || ((N_JOIN | N_JOIN_FOLLOWS) & get_mark(ret)));
1720
1721 /* Successfully cleared the JF mark of a non-deleted node. */
1722 if (ret == make_link(next, N_JOIN_FOLLOWS)) {
1723 break;
1724 } else {
1725 /*
1726 * The JF node had been deleted or a new node inserted
1727 * right after it. Retry from the head.
1728 */
1729 cur_link = new_head;
1730 continue;
1731 }
1732 } else {
1733 wnd.ppred = cur_link;
1734 wnd.cur = get_next(*cur_link);
1735 }
1736 }
1737
1738 /* We must encounter a JF node before we reach the end of the bucket. */
1739 ASSERT(wnd.cur);
1740 cur_link = &wnd.cur->link;
1741 }
1742
1743 rcu_read_unlock();
1744}
1745
1746
1747static size_t calc_split_hash(size_t split_idx, size_t order)
1748{
1749 ASSERT(1 <= order && order <= 8 * sizeof(size_t));
1750 return split_idx << (8 * sizeof(size_t) - order);
1751}
1752
1753static size_t calc_bucket_idx(size_t hash, size_t order)
1754{
1755 ASSERT(1 <= order && order <= 8 * sizeof(size_t));
1756 return hash >> (8 * sizeof(size_t) - order);
1757}
1758
1759static size_t grow_to_split_idx(size_t old_idx)
1760{
1761 return grow_idx(old_idx) | 1;
1762}
1763
1764static size_t grow_idx(size_t idx)
1765{
1766 return idx << 1;
1767}
1768
1769static size_t shrink_idx(size_t idx)
1770{
1771 return idx >> 1;
1772}
1773
1774
1775static size_t key_hash(cht_t *h, void *key)
1776{
1777 return hash_mix(h->op->key_hash(key));
1778}
1779
1780static size_t node_hash(cht_t *h, const cht_link_t *item)
1781{
1782 return hash_mix(h->op->hash(item));
1783}
1784
1785
1786static marked_ptr_t make_link(const cht_link_t *next, mark_t mark)
1787{
1788 marked_ptr_t ptr = (marked_ptr_t) next;
1789
1790 ASSERT(!(ptr & N_MARK_MASK));
1791 ASSERT(!((unsigned)mark & ~N_MARK_MASK));
1792
1793 return ptr | mark;
1794}
1795
1796
1797static cht_link_t * get_next(marked_ptr_t link)
1798{
1799 return (cht_link_t*)(link & ~N_MARK_MASK);
1800}
1801
1802
1803static mark_t get_mark(marked_ptr_t link)
1804{
1805 return (mark_t)(link & N_MARK_MASK);
1806}
1807
1808
1809static void next_wnd(wnd_t *wnd)
1810{
1811 ASSERT(wnd);
1812 ASSERT(wnd->cur);
1813
1814 wnd->last = wnd->cur;
1815 wnd->ppred = &wnd->cur->link;
1816 wnd->cur = get_next(wnd->cur->link);
1817}
1818
1819
1820static bool same_node_pred(void *node, const cht_link_t *item2)
1821{
1822 const cht_link_t *item1 = (const cht_link_t*) node;
1823 return item1 == item2;
1824}
1825
1826static marked_ptr_t cas_link(marked_ptr_t *link, const cht_link_t *cur_next,
1827 mark_t cur_mark, const cht_link_t *new_next, mark_t new_mark)
1828{
1829 return _cas_link(link, make_link(cur_next, cur_mark),
1830 make_link(new_next, new_mark));
1831}
1832
1833static marked_ptr_t _cas_link(marked_ptr_t *link, marked_ptr_t cur,
1834 marked_ptr_t new)
1835{
1836 /*
1837 * cas(x) on the same location x on one cpu must be ordered, but do not
1838 * have to be ordered wrt to other cas(y) to a different location y
1839 * on the same cpu.
1840 *
1841 * cas(x) must act as a write barrier on x, ie if cas(x) succeeds
1842 * and is observed by another cpu, then all cpus must be able to
1843 * make the effects of cas(x) visible just by issuing a load barrier.
1844 * For example:
1845 * cpu1 cpu2 cpu3
1846 * cas(x, 0 -> 1), succeeds
1847 * cas(x, 0 -> 1), fails
1848 * MB
1849 * y = 7
1850 * sees y == 7
1851 * loadMB must be enough to make cas(x) on cpu3 visible to cpu1, ie x == 1.
1852 *
1853 * If cas() did not work this way:
1854 * - our head move protocol would not be correct.
1855 * - freeing an item linked to a moved head after another item was
1856 * inserted in front of it, would require more than one grace period.
1857 */
1858 void *ret = atomic_cas_ptr((void**)link, (void *)cur, (void *)new);
1859 return (marked_ptr_t) ret;
1860}
1861
1862static void cas_order_barrier(void)
1863{
1864 /* Make sure CAS to different memory locations are ordered. */
1865 write_barrier();
1866}
1867
1868
1869/** @}
1870 */
Note: See TracBrowser for help on using the repository browser.