Debian Codegraph @

libgcc/unwind-dw2-btree.h

raw
1 /* Lock-free btree for manually registered unwind frames. */
2 /* Copyright (C) 2022-2025 Free Software Foundation, Inc.
3 Contributed by Thomas Neumann
4
5 This file is part of GCC.
6
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
10 version.
11
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
16
17 Under Section 7 of GPL version 3, you are granted additional
18 permissions described in the GCC Runtime Library Exception, version
19 3.1, as published by the Free Software Foundation.
20
21 You should have received a copy of the GNU General Public License and
22 a copy of the GCC Runtime Library Exception along with this program;
23 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
24 <http://www.gnu.org/licenses/>. */
25
26 #ifndef GCC_UNWIND_DW2_BTREE_H
27 #define GCC_UNWIND_DW2_BTREE_H
28
29 #include <stdbool.h>
30
31 // Common logic for version locks.
32 struct version_lock
33 {
34 // The lock itself. The lowest bit indicates an exclusive lock,
35 // the second bit indicates waiting threads. All other bits are
36 // used as counter to recognize changes.
37 // Overflows are okay here, we must only prevent overflow to the
38 // same value within one lock_optimistic/validate
39 // range. Even on 32 bit platforms that would require 1 billion
40 // frame registrations within the time span of a few assembler
41 // instructions.
42 uintptr_type version_lock;
43 };
44
45 #ifdef __GTHREAD_HAS_COND
46 // We should never get contention within the tree as it rarely changes.
47 // But if we ever do get contention we use these for waiting.
48 static __gthread_mutex_t version_lock_mutex = __GTHREAD_MUTEX_INIT;
49 static __gthread_cond_t version_lock_cond = __GTHREAD_COND_INIT;
50 #endif
51
52 // Initialize in locked state.
53 static inline void
54 version_lock_initialize_locked_exclusive (struct version_lock *vl)
55 {
56 vl->version_lock = 1;
57 }
58
59 // Try to lock the node exclusive.
60 static inline bool
61 version_lock_try_lock_exclusive (struct version_lock *vl)
62 {
63 uintptr_type state = __atomic_load_n (&vl->version_lock, __ATOMIC_SEQ_CST);
64 if (state & 1)
65 return false;
66 return __atomic_compare_exchange_n (&vl->version_lock, &state, state | 1,
67 false, __ATOMIC_SEQ_CST,
68 __ATOMIC_SEQ_CST);
69 }
70
71 // Lock the node exclusive, blocking as needed.
72 static void
73 version_lock_lock_exclusive (struct version_lock *vl)
74 {
75 #ifndef __GTHREAD_HAS_COND
76 restart:
77 #endif
78
79 // We should virtually never get contention here, as frame
80 // changes are rare.
81 uintptr_type state = __atomic_load_n (&vl->version_lock, __ATOMIC_SEQ_CST);
82 if (!(state & 1))
83 {
84 if (__atomic_compare_exchange_n (&vl->version_lock, &state, state | 1,
85 false, __ATOMIC_SEQ_CST,
86 __ATOMIC_SEQ_CST))
87 return;
88 }
89
90 // We did get contention, wait properly.
91 #ifdef __GTHREAD_HAS_COND
92 __gthread_mutex_lock (&version_lock_mutex);
93 state = __atomic_load_n (&vl->version_lock, __ATOMIC_SEQ_CST);
94 while (true)
95 {
96 // Check if the lock is still held.
97 if (!(state & 1))
98 {
99 if (__atomic_compare_exchange_n (&vl->version_lock, &state,
100 state | 1, false, __ATOMIC_SEQ_CST,
101 __ATOMIC_SEQ_CST))
102 {
103 __gthread_mutex_unlock (&version_lock_mutex);
104 return;
105 }
106 else
107 continue;
108 }
109
110 // Register waiting thread.
111 if (!(state & 2))
112 {
113 if (!__atomic_compare_exchange_n (&vl->version_lock, &state,
114 state | 2, false, __ATOMIC_SEQ_CST,
115 __ATOMIC_SEQ_CST))
116 continue;
117 }
118
119 // And sleep.
120 __gthread_cond_wait (&version_lock_cond, &version_lock_mutex);
121 state = __atomic_load_n (&vl->version_lock, __ATOMIC_SEQ_CST);
122 }
123 #else
124 // Spin if we do not have condition variables available.
125 // We expect no contention here, spinning should be okay.
126 goto restart;
127 #endif
128 }
129
130 // Release a locked node and increase the version lock.
131 static void
132 version_lock_unlock_exclusive (struct version_lock *vl)
133 {
134 // Increase version, reset exclusive lock bits.
135 uintptr_type state = __atomic_load_n (&vl->version_lock, __ATOMIC_SEQ_CST);
136 uintptr_type ns = (state + 4) & ~(uintptr_type) 3;
137 state = __atomic_exchange_n (&vl->version_lock, ns, __ATOMIC_SEQ_CST);
138
139 #ifdef __GTHREAD_HAS_COND
140 if (state & 2)
141 {
142 // Wake up waiting threads. This should be extremely rare.
143 __gthread_mutex_lock (&version_lock_mutex);
144 __gthread_cond_broadcast (&version_lock_cond);
145 __gthread_mutex_unlock (&version_lock_mutex);
146 }
147 #endif
148 }
149
150 // Acquire an optimistic "lock". Note that this does not lock at all, it
151 // only allows for validation later.
152 static inline bool
153 version_lock_lock_optimistic (const struct version_lock *vl, uintptr_type *lock)
154 {
155 uintptr_type state = __atomic_load_n (&vl->version_lock, __ATOMIC_SEQ_CST);
156 *lock = state;
157
158 // Acquiring the lock fails when there is currently an exclusive lock.
159 return !(state & 1);
160 }
161
162 // Validate a previously acquired "lock".
163 static inline bool
164 version_lock_validate (const struct version_lock *vl, uintptr_type lock)
165 {
166 // Prevent the reordering of non-atomic loads behind the atomic load.
167 // Hans Boehm, Can Seqlocks Get Along with Programming Language Memory
168 // Models?, Section 4.
169 __atomic_thread_fence (__ATOMIC_ACQUIRE);
170
171 // Check that the node is still in the same state.
172 uintptr_type state = __atomic_load_n (&vl->version_lock, __ATOMIC_SEQ_CST);
173 return state == lock;
174 }
175
176 // The largest possible separator value.
177 static const uintptr_type max_separator = ~(uintptr_type) 0;
178
179 struct btree_node;
180
181 // Inner entry. The child tree contains all entries <= separator.
182 struct inner_entry
183 {
184 uintptr_type separator;
185 struct btree_node *child;
186 };
187
188 // Leaf entry. Stores an object entry.
189 struct leaf_entry
190 {
191 uintptr_type base, size;
192 struct object *ob;
193 };
194
195 // Node types.
196 enum node_type
197 {
198 btree_node_inner,
199 btree_node_leaf,
200 btree_node_free
201 };
202
203 // Node sizes. Chosen such that the result size is roughly 256 bytes.
204 #define max_fanout_inner 15
205 #define max_fanout_leaf 10
206
207 // A btree node.
208 struct btree_node
209 {
210 // The version lock used for optimistic lock coupling.
211 struct version_lock version_lock;
212 // The number of entries.
213 unsigned entry_count;
214 // The type.
215 enum node_type type;
216 // The payload.
217 union
218 {
219 // The inner nodes have fence keys, i.e., the right-most entry includes a
220 // separator.
221 struct inner_entry children[max_fanout_inner];
222 struct leaf_entry entries[max_fanout_leaf];
223 } content;
224 };
225
226 // Is an inner node?
227 static inline bool
228 btree_node_is_inner (const struct btree_node *n)
229 {
230 return n->type == btree_node_inner;
231 }
232
233 // Is a leaf node?
234 static inline bool
235 btree_node_is_leaf (const struct btree_node *n)
236 {
237 return n->type == btree_node_leaf;
238 }
239
240 // Should the node be merged?
241 static inline bool
242 btree_node_needs_merge (const struct btree_node *n)
243 {
244 return n->entry_count < (btree_node_is_inner (n) ? max_fanout_inner / 2
245 : max_fanout_leaf / 2);
246 }
247
248 // Get the fence key for inner nodes.
249 static inline uintptr_type
250 btree_node_get_fence_key (const struct btree_node *n)
251 {
252 // For inner nodes we just return our right-most entry.
253 return n->content.children[n->entry_count - 1].separator;
254 }
255
256 // Find the position for a slot in an inner node.
257 static unsigned
258 btree_node_find_inner_slot (const struct btree_node *n, uintptr_type value)
259 {
260 for (unsigned index = 0, ec = n->entry_count; index != ec; ++index)
261 if (n->content.children[index].separator >= value)
262 return index;
263 return n->entry_count;
264 }
265
266 // Find the position for a slot in a leaf node.
267 static unsigned
268 btree_node_find_leaf_slot (const struct btree_node *n, uintptr_type value)
269 {
270 for (unsigned index = 0, ec = n->entry_count; index != ec; ++index)
271 if (n->content.entries[index].base + n->content.entries[index].size > value)
272 return index;
273 return n->entry_count;
274 }
275
276 // Try to lock the node exclusive.
277 static inline bool
278 btree_node_try_lock_exclusive (struct btree_node *n)
279 {
280 return version_lock_try_lock_exclusive (&n->version_lock);
281 }
282
283 // Lock the node exclusive, blocking as needed.
284 static inline void
285 btree_node_lock_exclusive (struct btree_node *n)
286 {
287 version_lock_lock_exclusive (&n->version_lock);
288 }
289
290 // Release a locked node and increase the version lock.
291 static inline void
292 btree_node_unlock_exclusive (struct btree_node *n)
293 {
294 version_lock_unlock_exclusive (&n->version_lock);
295 }
296
297 // Acquire an optimistic "lock". Note that this does not lock at all, it
298 // only allows for validation later.
299 static inline bool
300 btree_node_lock_optimistic (const struct btree_node *n, uintptr_type *lock)
301 {
302 return version_lock_lock_optimistic (&n->version_lock, lock);
303 }
304
305 // Validate a previously acquire lock.
306 static inline bool
307 btree_node_validate (const struct btree_node *n, uintptr_type lock)
308 {
309 return version_lock_validate (&n->version_lock, lock);
310 }
311
312 // Insert a new separator after splitting.
313 static void
314 btree_node_update_separator_after_split (struct btree_node *n,
315 uintptr_type old_separator,
316 uintptr_type new_separator,
317 struct btree_node *new_right)
318 {
319 unsigned slot = btree_node_find_inner_slot (n, old_separator);
320 for (unsigned index = n->entry_count; index > slot; --index)
321 n->content.children[index] = n->content.children[index - 1];
322 n->content.children[slot].separator = new_separator;
323 n->content.children[slot + 1].child = new_right;
324 n->entry_count++;
325 }
326
327 // A btree. Suitable for static initialization, all members are zero at the
328 // beginning.
329 struct btree
330 {
331 // The root of the btree.
332 struct btree_node *root;
333 // The free list of released node.
334 struct btree_node *free_list;
335 // The version lock used to protect the root.
336 struct version_lock root_lock;
337 };
338
339 // Initialize a btree. Not actually used, just for exposition.
340 static inline void
341 btree_init (struct btree *t)
342 {
343 t->root = NULL;
344 t->free_list = NULL;
345 t->root_lock.version_lock = 0;
346 };
347
348 static void
349 btree_release_tree_recursively (struct btree *t, struct btree_node *n);
350
351 // Destroy a tree and release all nodes.
352 static void
353 btree_destroy (struct btree *t)
354 {
355 // Disable the mechanism before cleaning up.
356 struct btree_node *old_root
357 = __atomic_exchange_n (&t->root, NULL, __ATOMIC_SEQ_CST);
358 if (old_root)
359 btree_release_tree_recursively (t, old_root);
360
361 // Release all free nodes.
362 while (t->free_list)
363 {
364 struct btree_node *next = t->free_list->content.children[0].child;
365 free (t->free_list);
366 t->free_list = next;
367 }
368 }
369
370 // Allocate a node. This node will be returned in locked exclusive state.
371 static struct btree_node *
372 btree_allocate_node (struct btree *t, bool inner)
373 {
374 while (true)
375 {
376 // Try the free list first.
377 struct btree_node *next_free
378 = __atomic_load_n (&t->free_list, __ATOMIC_SEQ_CST);
379 if (next_free)
380 {
381 if (!btree_node_try_lock_exclusive (next_free))
382 continue;
383 // The node might no longer be free, check that again after acquiring
384 // the exclusive lock.
385 if (next_free->type == btree_node_free)
386 {
387 struct btree_node *ex = next_free;
388 if (__atomic_compare_exchange_n (&t->free_list, &ex,
389 ex->content.children[0].child,
390 false, __ATOMIC_SEQ_CST,
391 __ATOMIC_SEQ_CST))
392 {
393 next_free->entry_count = 0;
394 next_free->type = inner ? btree_node_inner : btree_node_leaf;
395 return next_free;
396 }
397 }
398 btree_node_unlock_exclusive (next_free);
399 continue;
400 }
401
402 // No free node available, allocate a new one.
403 struct btree_node *new_node
404 = (struct btree_node *) malloc (sizeof (struct btree_node));
405 // Initialize the node in locked state.
406 version_lock_initialize_locked_exclusive (&new_node->version_lock);
407 new_node->entry_count = 0;
408 new_node->type = inner ? btree_node_inner : btree_node_leaf;
409 return new_node;
410 }
411 }
412
413 // Release a node. This node must be currently locked exclusively and will
414 // be placed in the free list.
415 static void
416 btree_release_node (struct btree *t, struct btree_node *node)
417 {
418 // We cannot release the memory immediately because there might still be
419 // concurrent readers on that node. Put it in the free list instead.
420 node->type = btree_node_free;
421 struct btree_node *next_free
422 = __atomic_load_n (&t->free_list, __ATOMIC_SEQ_CST);
423 do
424 node->content.children[0].child = next_free;
425 while (!__atomic_compare_exchange_n (&t->free_list, &next_free, node,
426 false, __ATOMIC_SEQ_CST,
427 __ATOMIC_SEQ_CST));
428 btree_node_unlock_exclusive (node);
429 }
430
431 // Recursively release a tree. The btree is by design very shallow, thus
432 // we can risk recursion here.
433 static void
434 btree_release_tree_recursively (struct btree *t, struct btree_node *node)
435 {
436 btree_node_lock_exclusive (node);
437 if (btree_node_is_inner (node))
438 {
439 for (unsigned index = 0; index < node->entry_count; ++index)
440 btree_release_tree_recursively (t, node->content.children[index].child);
441 }
442 btree_release_node (t, node);
443 }
444
445 // Check if we are splitting the root.
446 static void
447 btree_handle_root_split (struct btree *t, struct btree_node **node,
448 struct btree_node **parent)
449 {
450 // We want to keep the root pointer stable to allow for contention
451 // free reads. Thus, we split the root by first moving the content
452 // of the root node to a new node, and then split that new node.
453 if (!*parent)
454 {
455 // Allocate a new node, this guarantees us that we will have a parent
456 // afterwards.
457 struct btree_node *new_node
458 = btree_allocate_node (t, btree_node_is_inner (*node));
459 struct btree_node *old_node = *node;
460 new_node->entry_count = old_node->entry_count;
461 new_node->content = old_node->content;
462 old_node->content.children[0].separator = max_separator;
463 old_node->content.children[0].child = new_node;
464 old_node->entry_count = 1;
465 old_node->type = btree_node_inner;
466
467 *parent = old_node;
468 *node = new_node;
469 }
470 }
471
472 // Split an inner node.
473 static void
474 btree_split_inner (struct btree *t, struct btree_node **inner,
475 struct btree_node **parent, uintptr_type target,
476 uintptr_type size)
477 {
478 // Check for the root.
479 btree_handle_root_split (t, inner, parent);
480
481 // Create two inner node.
482 uintptr_type right_fence = btree_node_get_fence_key (*inner);
483 struct btree_node *left_inner = *inner;
484 struct btree_node *right_inner = btree_allocate_node (t, true);
485 unsigned split = left_inner->entry_count / 2;
486 right_inner->entry_count = left_inner->entry_count - split;
487 for (unsigned index = 0; index < right_inner->entry_count; ++index)
488 right_inner->content.children[index]
489 = left_inner->content.children[split + index];
490 left_inner->entry_count = split;
491 uintptr_type left_fence = btree_node_get_fence_key (left_inner);
492 if (left_fence >= target && left_fence < target + size - 1)
493 // See the PR119151 comment in btree_insert.
494 left_fence = target + size - 1;
495 btree_node_update_separator_after_split (*parent, right_fence, left_fence,
496 right_inner);
497 if (target <= left_fence)
498 {
499 *inner = left_inner;
500 btree_node_unlock_exclusive (right_inner);
501 }
502 else
503 {
504 *inner = right_inner;
505 btree_node_unlock_exclusive (left_inner);
506 }
507 }
508
509 // Split a leaf node.
510 static void
511 btree_split_leaf (struct btree *t, struct btree_node **leaf,
512 struct btree_node **parent, uintptr_type fence,
513 uintptr_type target)
514 {
515 // Check for the root.
516 btree_handle_root_split (t, leaf, parent);
517
518 // Create two leaf nodes.
519 uintptr_type right_fence = fence;
520 struct btree_node *left_leaf = *leaf;
521 struct btree_node *right_leaf = btree_allocate_node (t, false);
522 unsigned split = left_leaf->entry_count / 2;
523 right_leaf->entry_count = left_leaf->entry_count - split;
524 for (unsigned index = 0; index != right_leaf->entry_count; ++index)
525 right_leaf->content.entries[index]
526 = left_leaf->content.entries[split + index];
527 left_leaf->entry_count = split;
528 uintptr_type left_fence = right_leaf->content.entries[0].base - 1;
529 btree_node_update_separator_after_split (*parent, right_fence, left_fence,
530 right_leaf);
531 if (target <= left_fence)
532 {
533 *leaf = left_leaf;
534 btree_node_unlock_exclusive (right_leaf);
535 }
536 else
537 {
538 *leaf = right_leaf;
539 btree_node_unlock_exclusive (left_leaf);
540 }
541 }
542
543 // Merge (or balance) child nodes.
544 static struct btree_node *
545 btree_merge_node (struct btree *t, unsigned child_slot,
546 struct btree_node *parent, uintptr_type target)
547 {
548 // Choose the emptiest neighbor and lock both. The target child is already
549 // locked.
550 unsigned left_slot;
551 struct btree_node *left_node, *right_node;
552 if (child_slot == 0
553 || (child_slot + 1 < parent->entry_count
554 && (parent->content.children[child_slot + 1].child->entry_count
555 < parent->content.children[child_slot - 1].child->entry_count)))
556 {
557 left_slot = child_slot;
558 left_node = parent->content.children[left_slot].child;
559 right_node = parent->content.children[left_slot + 1].child;
560 btree_node_lock_exclusive (right_node);
561 }
562 else
563 {
564 left_slot = child_slot - 1;
565 left_node = parent->content.children[left_slot].child;
566 right_node = parent->content.children[left_slot + 1].child;
567 btree_node_lock_exclusive (left_node);
568 }
569
570 // Can we merge both nodes into one node?
571 unsigned total_count = left_node->entry_count + right_node->entry_count;
572 unsigned max_count
573 = btree_node_is_inner (left_node) ? max_fanout_inner : max_fanout_leaf;
574 if (total_count <= max_count)
575 {
576 // Merge into the parent?
577 if (parent->entry_count == 2)
578 {
579 // Merge children into parent. This can only happen at the root.
580 if (btree_node_is_inner (left_node))
581 {
582 for (unsigned index = 0; index != left_node->entry_count; ++index)
583 parent->content.children[index]
584 = left_node->content.children[index];
585 for (unsigned index = 0; index != right_node->entry_count;
586 ++index)
587 parent->content.children[index + left_node->entry_count]
588 = right_node->content.children[index];
589 }
590 else
591 {
592 parent->type = btree_node_leaf;
593 for (unsigned index = 0; index != left_node->entry_count; ++index)
594 parent->content.entries[index]
595 = left_node->content.entries[index];
596 for (unsigned index = 0; index != right_node->entry_count;
597 ++index)
598 parent->content.entries[index + left_node->entry_count]
599 = right_node->content.entries[index];
600 }
601 parent->entry_count = total_count;
602 btree_release_node (t, left_node);
603 btree_release_node (t, right_node);
604 return parent;
605 }
606 else
607 {
608 // Regular merge.
609 if (btree_node_is_inner (left_node))
610 {
611 for (unsigned index = 0; index != right_node->entry_count;
612 ++index)
613 left_node->content.children[left_node->entry_count++]
614 = right_node->content.children[index];
615 }
616 else
617 {
618 for (unsigned index = 0; index != right_node->entry_count;
619 ++index)
620 left_node->content.entries[left_node->entry_count++]
621 = right_node->content.entries[index];
622 }
623 parent->content.children[left_slot].separator
624 = parent->content.children[left_slot + 1].separator;
625 for (unsigned index = left_slot + 1; index + 1 < parent->entry_count;
626 ++index)
627 parent->content.children[index]
628 = parent->content.children[index + 1];
629 parent->entry_count--;
630 btree_release_node (t, right_node);
631 btree_node_unlock_exclusive (parent);
632 return left_node;
633 }
634 }
635
636 // No merge possible, rebalance instead.
637 if (left_node->entry_count > right_node->entry_count)
638 {
639 // Shift from left to right.
640 unsigned to_shift
641 = (left_node->entry_count - right_node->entry_count) / 2;
642 if (btree_node_is_inner (left_node))
643 {
644 for (unsigned index = 0; index != right_node->entry_count; ++index)
645 {
646 unsigned pos = right_node->entry_count - 1 - index;
647 right_node->content.children[pos + to_shift]
648 = right_node->content.children[pos];
649 }
650 for (unsigned index = 0; index != to_shift; ++index)
651 right_node->content.children[index]
652 = left_node->content
653 .children[left_node->entry_count - to_shift + index];
654 }
655 else
656 {
657 for (unsigned index = 0; index != right_node->entry_count; ++index)
658 {
659 unsigned pos = right_node->entry_count - 1 - index;
660 right_node->content.entries[pos + to_shift]
661 = right_node->content.entries[pos];
662 }
663 for (unsigned index = 0; index != to_shift; ++index)
664 right_node->content.entries[index]
665 = left_node->content
666 .entries[left_node->entry_count - to_shift + index];
667 }
668 left_node->entry_count -= to_shift;
669 right_node->entry_count += to_shift;
670 }
671 else
672 {
673 // Shift from right to left.
674 unsigned to_shift
675 = (right_node->entry_count - left_node->entry_count) / 2;
676 if (btree_node_is_inner (left_node))
677 {
678 for (unsigned index = 0; index != to_shift; ++index)
679 left_node->content.children[left_node->entry_count + index]
680 = right_node->content.children[index];
681 for (unsigned index = 0; index != right_node->entry_count - to_shift;
682 ++index)
683 right_node->content.children[index]
684 = right_node->content.children[index + to_shift];
685 }
686 else
687 {
688 for (unsigned index = 0; index != to_shift; ++index)
689 left_node->content.entries[left_node->entry_count + index]
690 = right_node->content.entries[index];
691 for (unsigned index = 0; index != right_node->entry_count - to_shift;
692 ++index)
693 right_node->content.entries[index]
694 = right_node->content.entries[index + to_shift];
695 }
696 left_node->entry_count += to_shift;
697 right_node->entry_count -= to_shift;
698 }
699 uintptr_type left_fence;
700 if (btree_node_is_leaf (left_node))
701 left_fence = right_node->content.entries[0].base - 1;
702 else
703 left_fence = btree_node_get_fence_key (left_node);
704 parent->content.children[left_slot].separator = left_fence;
705 btree_node_unlock_exclusive (parent);
706 if (target <= left_fence)
707 {
708 btree_node_unlock_exclusive (right_node);
709 return left_node;
710 }
711 else
712 {
713 btree_node_unlock_exclusive (left_node);
714 return right_node;
715 }
716 }
717
718 // Insert an entry.
719 static bool
720 btree_insert (struct btree *t, uintptr_type base, uintptr_type size,
721 struct object *ob)
722 {
723 // Sanity check.
724 if (!size)
725 return false;
726
727 // Access the root.
728 struct btree_node *iter, *parent = NULL;
729 version_lock_lock_exclusive (&t->root_lock);
730 iter = t->root;
731 if (iter)
732 btree_node_lock_exclusive (iter);
733 else
734 t->root = iter = btree_allocate_node (t, false);
735 version_lock_unlock_exclusive (&t->root_lock);
736
737 // Walk down the btree with classic lock coupling and eager splits.
738 // Strictly speaking this is not performance optimal, we could use
739 // optimistic lock coupling until we hit a node that has to be modified.
740 // But that is more difficult to implement and frame registration is
741 // rare anyway, we use simple locking for now.
742
743 uintptr_type fence = max_separator;
744 while (btree_node_is_inner (iter))
745 {
746 // Use eager splits to avoid lock coupling up.
747 if (iter->entry_count == max_fanout_inner)
748 btree_split_inner (t, &iter, &parent, base, size);
749
750 unsigned slot = btree_node_find_inner_slot (iter, base);
751 if (parent)
752 btree_node_unlock_exclusive (parent);
753 parent = iter;
754 fence = iter->content.children[slot].separator;
755 if (fence < base + size - 1)
756 // The separator was set to the base - 1 of the leftmost leaf child
757 // at some point but such an entry could have been removed afterwards.
758 // As both insertion and removal are just walking down the tree with
759 // only a few current nodes locked at a time, updating the separator
760 // on removal is not possible, especially because btree_remove does
761 // not know the size until it reaches leaf node. We must ensure that
762 // the separator is not in a middle of some entry though, as
763 // btree_lookup can look up any address in the entry's range and if
764 // the separator is in the middle, addresses below it or equal to it
765 // would be found while addresses above it would result in failed
766 // lookup. Update the separator now. Assumption that users
767 // ensure no overlapping registered ranges, there should be no
768 // current entry for any address in the range. See PR119151.
769 fence = iter->content.children[slot].separator = base + size - 1;
770 iter = iter->content.children[slot].child;
771 btree_node_lock_exclusive (iter);
772 }
773
774 // Make sure we have space.
775 if (iter->entry_count == max_fanout_leaf)
776 btree_split_leaf (t, &iter, &parent, fence, base);
777 if (parent)
778 btree_node_unlock_exclusive (parent);
779
780 // Insert in node.
781 unsigned slot = btree_node_find_leaf_slot (iter, base);
782 if (slot < iter->entry_count && iter->content.entries[slot].base == base)
783 {
784 // Duplicate entry, this should never happen.
785 btree_node_unlock_exclusive (iter);
786 return false;
787 }
788 for (unsigned index = iter->entry_count; index > slot; --index)
789 iter->content.entries[index] = iter->content.entries[index - 1];
790 struct leaf_entry *e = &iter->content.entries[slot];
791 e->base = base;
792 e->size = size;
793 e->ob = ob;
794 iter->entry_count++;
795 btree_node_unlock_exclusive (iter);
796 return true;
797 }
798
799 // Remove an entry.
800 static struct object *
801 btree_remove (struct btree *t, uintptr_type base)
802 {
803 // Access the root.
804 version_lock_lock_exclusive (&t->root_lock);
805 struct btree_node *iter = t->root;
806 if (iter)
807 btree_node_lock_exclusive (iter);
808 version_lock_unlock_exclusive (&t->root_lock);
809 if (!iter)
810 return NULL;
811
812 // Same strategy as with insert, walk down with lock coupling and
813 // merge eagerly.
814 while (btree_node_is_inner (iter))
815 {
816 unsigned slot = btree_node_find_inner_slot (iter, base);
817 struct btree_node *next = iter->content.children[slot].child;
818 btree_node_lock_exclusive (next);
819 if (btree_node_needs_merge (next))
820 // Use eager merges to avoid lock coupling up.
821 iter = btree_merge_node (t, slot, iter, base);
822 else
823 {
824 btree_node_unlock_exclusive (iter);
825 iter = next;
826 }
827 }
828
829 // Remove existing entry.
830 unsigned slot = btree_node_find_leaf_slot (iter, base);
831 if (slot >= iter->entry_count || iter->content.entries[slot].base != base)
832 {
833 // Not found, this should never happen.
834 btree_node_unlock_exclusive (iter);
835 return NULL;
836 }
837 struct object *ob = iter->content.entries[slot].ob;
838 for (unsigned index = slot; index + 1 < iter->entry_count; ++index)
839 iter->content.entries[index] = iter->content.entries[index + 1];
840 iter->entry_count--;
841 btree_node_unlock_exclusive (iter);
842 return ob;
843 }
844
845 // Find the corresponding entry for the given address.
846 static struct object *
847 btree_lookup (const struct btree *t, uintptr_type target_addr)
848 {
849 // Within this function many loads are relaxed atomic loads.
850 // Use a macro to keep the code reasonable.
851 #define RLOAD(x) __atomic_load_n (&(x), __ATOMIC_RELAXED)
852
853 // For targets where unwind info is usually not registered through these
854 // APIs anymore, avoid any sequential consistent atomics.
855 // Use relaxed MO here, it is up to the app to ensure that the library
856 // loading/initialization happens-before using that library in other
857 // threads (in particular unwinding with that library's functions
858 // appearing in the backtraces). Calling that library's functions
859 // without waiting for the library to initialize would be racy.
860 if (__builtin_expect (!RLOAD (t->root), 1))
861 return NULL;
862
863 // The unwinding tables are mostly static, they only change when
864 // frames are added or removed. This makes it extremely unlikely that they
865 // change during a given unwinding sequence. Thus, we optimize for the
866 // contention free case and use optimistic lock coupling. This does not
867 // require any writes to shared state, instead we validate every read. It is
868 // important that we do not trust any value that we have read until we call
869 // validate again. Data can change at arbitrary points in time, thus we always
870 // copy something into a local variable and validate again before acting on
871 // the read. In the unlikely event that we encounter a concurrent change we
872 // simply restart and try again.
873
874 restart:
875 struct btree_node *iter;
876 uintptr_type lock;
877 // Accessing the root node requires defending against concurrent pointer
878 // changes. Thus we couple root_lock -> lock on root node -> validate
879 // root_lock.
880 if (!version_lock_lock_optimistic (&t->root_lock, &lock))
881 goto restart;
882 iter = RLOAD (t->root);
883 if (!version_lock_validate (&t->root_lock, lock))
884 goto restart;
885 if (!iter)
886 return NULL;
887 uintptr_type child_root_lock;
888 if (!btree_node_lock_optimistic (iter, &child_root_lock)
889 || !version_lock_validate (&t->root_lock, lock))
890 goto restart;
891 lock = child_root_lock;
892
893 // Now we can walk down towards the right leaf node.
894 while (true)
895 {
896 enum node_type type = RLOAD (iter->type);
897 unsigned entry_count = RLOAD (iter->entry_count);
898 if (!btree_node_validate (iter, lock))
899 goto restart;
900 if (!entry_count)
901 return NULL;
902
903 if (type == btree_node_inner)
904 {
905 // We cannot call find_inner_slot here because we need (relaxed)
906 // atomic reads here.
907 unsigned slot = 0;
908 while (slot + 1 < entry_count
909 && (RLOAD (iter->content.children[slot].separator)
910 < target_addr))
911 ++slot;
912 struct btree_node *child = RLOAD (iter->content.children[slot].child);
913 if (!btree_node_validate (iter, lock))
914 goto restart;
915
916 // The node content can change at any point in time, thus we must
917 // interleave parent and child checks.
918 uintptr_type child_lock;
919 if (!btree_node_lock_optimistic (child, &child_lock))
920 goto restart;
921 if (!btree_node_validate (iter, lock))
922 goto restart; // Make sure we still point to the correct node after
923 // acquiring the optimistic lock.
924
925 // Go down.
926 iter = child;
927 lock = child_lock;
928 }
929 else
930 {
931 // We cannot call find_leaf_slot here because we need (relaxed)
932 // atomic reads here.
933 unsigned slot = 0;
934 while (slot + 1 < entry_count
935 && (RLOAD (iter->content.entries[slot].base)
936 + RLOAD (iter->content.entries[slot].size)
937 <= target_addr))
938 ++slot;
939 struct leaf_entry entry;
940 entry.base = RLOAD (iter->content.entries[slot].base);
941 entry.size = RLOAD (iter->content.entries[slot].size);
942 entry.ob = RLOAD (iter->content.entries[slot].ob);
943 if (!btree_node_validate (iter, lock))
944 goto restart;
945
946 // Check if we have a hit.
947 if (entry.base <= target_addr
948 && target_addr < entry.base + entry.size)
949 return entry.ob;
950 return NULL;
951 }
952 }
953 #undef RLOAD
954 }
955
956 #endif /* unwind-dw2-btree.h */