2 * Copyright (c) 2016 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
28 #include <kern/cpu_data.h>
29 #include <kern/kern_types.h>
30 #include <kern/locks.h>
31 #include <kern/ltable.h>
32 #include <kern/zalloc.h>
33 #include <libkern/OSAtomic.h>
34 #include <pexpert/pexpert.h>
35 #include <vm/vm_kern.h>
38 #define P2ROUNDUP(x, align) (-(-((uint32_t)(x)) & -(align)))
39 #define ROUNDDOWN(x, y) (((x)/(y))*(y))
41 /* ----------------------------------------------------------------------
43 * Lockless Link Table Interface
45 * ---------------------------------------------------------------------- */
47 vm_size_t g_lt_max_tbl_size
;
48 static lck_grp_t g_lt_lck_grp
;
50 /* default VA space for link tables (zone allocated) */
51 #define DEFAULT_MAX_TABLE_SIZE P2ROUNDUP(8 * 1024 * 1024, PAGE_SIZE)
53 #if DEVELOPMENT || DEBUG
54 /* global for lldb macros */
55 uint64_t g_lt_idx_max
= LT_IDX_MAX
;
59 /* construct a link table element from an offset and mask into a slab */
60 #define lt_elem_ofst_slab(slab, slab_msk, ofst) \
61 /* cast through 'void *' to avoid compiler alignment warning messages */ \
62 ((struct lt_elem *)((void *)((uintptr_t)(slab) + ((ofst) & (slab_msk)))))
64 #if CONFIG_LTABLE_STATS
65 /* version that makes no assumption on waste within a slab */
66 static inline struct lt_elem
*
67 lt_elem_idx(struct link_table
*table
, uint32_t idx
)
69 int slab_idx
= idx
/ table
->slab_elem
;
70 struct lt_elem
*slab
= table
->table
[slab_idx
];
72 panic("Invalid index:%d slab:%d (NULL) for table:%p\n",
73 idx
, slab_idx
, table
);
75 assert(slab
->lt_id
.idx
<= idx
&& (slab
->lt_id
.idx
+ table
->slab_elem
) > idx
);
76 return lt_elem_ofst_slab(slab
, table
->slab_msk
, (idx
- slab
->lt_id
.idx
) * table
->elem_sz
);
78 #else /* !CONFIG_LTABLE_STATS */
79 /* verion that assumes 100% ultilization of slabs (no waste) */
80 static inline struct lt_elem
*
81 lt_elem_idx(struct link_table
*table
, uint32_t idx
)
83 uint32_t ofst
= idx
* table
->elem_sz
;
84 struct lt_elem
*slab
= table
->table
[ofst
>> table
->slab_shift
];
86 panic("Invalid index:%d slab:%d (NULL) for table:%p\n",
87 idx
, (ofst
>> table
->slab_shift
), table
);
89 assert(slab
->lt_id
.idx
<= idx
&& (slab
->lt_id
.idx
+ table
->slab_elem
) > idx
);
90 return lt_elem_ofst_slab(slab
, table
->slab_msk
, ofst
);
92 #endif /* CONFIG_LTABLE_STATS */
94 static int __assert_only
95 lt_elem_in_range(struct lt_elem
*elem
, struct link_table
*table
)
97 struct lt_elem
**base
= table
->table
;
98 uintptr_t e
= (uintptr_t)elem
;
100 while (*base
!= NULL
) {
101 uintptr_t b
= (uintptr_t)(*base
);
102 if (e
>= b
&& e
< b
+ table
->slab_sz
) {
106 if ((uintptr_t)base
>= (uintptr_t)table
->table
+ PAGE_SIZE
) {
115 * lt_elem_invalidate: mark 'elem' as invalid
117 * NOTE: this does _not_ get or put a reference on 'elem'
120 lt_elem_invalidate(struct lt_elem
*elem
)
122 uint32_t __assert_only old
= OSBitAndAtomic(~LT_BITS_VALID
, &elem
->lt_bits
);
124 assert(((lt_bits_type(old
) != LT_RESERVED
) && (old
& LT_BITS_VALID
)) ||
125 ((lt_bits_type(old
) == LT_RESERVED
) && !(old
& LT_BITS_VALID
)));
129 * lt_elem_mkvalid: mark 'elem' as valid
131 * NOTE: this does _not_ get or put a reference on 'elem'
134 lt_elem_mkvalid(struct lt_elem
*elem
)
136 uint32_t __assert_only old
= OSBitOrAtomic(LT_BITS_VALID
, &elem
->lt_bits
);
138 assert(!(old
& LT_BITS_VALID
));
142 lt_elem_set_type(struct lt_elem
*elem
, int type
)
144 uint32_t old_bits
, new_bits
;
146 old_bits
= elem
->lt_bits
;
147 new_bits
= (old_bits
& ~LT_BITS_TYPE
) |
148 ((type
& LT_BITS_TYPE_MASK
) << LT_BITS_TYPE_SHIFT
);
149 } while (OSCompareAndSwap(old_bits
, new_bits
, &elem
->lt_bits
) == FALSE
);
155 * ltable_bootstrap: bootstrap a link table
157 * Called once at system boot
160 ltable_bootstrap(void)
162 static int s_is_bootstrapped
= 0;
166 if (s_is_bootstrapped
) {
169 s_is_bootstrapped
= 1;
171 g_lt_max_tbl_size
= DEFAULT_MAX_TABLE_SIZE
;
172 if (PE_parse_boot_argn("lt_tbl_size", &tmp32
, sizeof(tmp32
)) == TRUE
) {
173 g_lt_max_tbl_size
= (vm_size_t
)P2ROUNDUP(tmp32
, PAGE_SIZE
);
176 lck_grp_init(&g_lt_lck_grp
, "link_table_locks", LCK_GRP_ATTR_NULL
);
180 * ltable_init: initialize a link table with given parameters
184 ltable_init(struct link_table
*table
, const char *name
,
185 uint32_t max_tbl_elem
, uint32_t elem_sz
,
186 ltable_poison_func poison
)
189 uint32_t slab_sz
, slab_shift
, slab_msk
, slab_elem
;
192 struct lt_elem
*e
, **base
;
194 #ifndef CONFIG_LTABLE_STATS
195 /* the element size _must_ be a power of two! */
196 if ((elem_sz
& (elem_sz
- 1)) != 0) {
197 panic("elem_sz:%d for table:'%s' must be a power of two!",
203 * First, allocate a single page of memory to act as the base
204 * for the table's element slabs
206 kr
= kernel_memory_allocate(kernel_map
, (vm_offset_t
*)&base
,
207 PAGE_SIZE
, 0, KMA_NOPAGEWAIT
, VM_KERN_MEMORY_LTABLE
);
208 if (kr
!= KERN_SUCCESS
) {
209 panic("Cannot initialize %s table: "
210 "kernel_memory_allocate failed:%d\n", name
, kr
);
212 memset(base
, 0, PAGE_SIZE
);
215 * Based on the maximum table size, calculate the slab size:
216 * we allocate 1 page of slab pointers for the table, and we need to
217 * index elements of 'elem_sz', this gives us the slab size based on
218 * the maximum size the table should grow.
220 max_tbl_sz
= (max_tbl_elem
* elem_sz
);
221 max_tbl_sz
= P2ROUNDUP(max_tbl_sz
, PAGE_SIZE
);
223 /* system maximum table size divided by number of slots in a page */
224 slab_sz
= (uint32_t)(max_tbl_sz
/ (PAGE_SIZE
/ (sizeof(void *))));
225 if (slab_sz
< PAGE_SIZE
) {
229 /* make sure the slab size is a power of two */
232 for (uint32_t i
= 0; i
< 31; i
++) {
233 uint32_t bit
= (1 << i
);
234 if ((slab_sz
& bit
) == slab_sz
) {
237 for (uint32_t j
= 0; j
< i
; j
++) {
238 slab_msk
|= (1 << j
);
244 slab_elem
= slab_sz
/ elem_sz
;
246 /* initialize the table's slab zone (for table growth) */
247 ltdbg("Initializing %s zone: slab:%d (%d,0x%x) max:%ld",
248 name
, slab_sz
, slab_shift
, slab_msk
, max_tbl_sz
);
249 slab_zone
= zinit(slab_sz
, max_tbl_sz
, slab_sz
, name
);
250 assert(slab_zone
!= ZONE_NULL
);
252 /* allocate the first slab and populate it */
253 base
[0] = (struct lt_elem
*)zalloc(slab_zone
);
254 if (base
[0] == NULL
) {
255 panic("Can't allocate a %s table slab from zone:%p",
259 memset(base
[0], 0, slab_sz
);
261 /* setup the initial freelist */
262 ltdbg("initializing %d links (%d bytes each)...", slab_elem
, elem_sz
);
263 for (unsigned l
= 0; l
< slab_elem
; l
++) {
264 e
= lt_elem_ofst_slab(base
[0], slab_msk
, l
* elem_sz
);
267 * setting generation to 0 ensures that a setid of 0 is
268 * invalid because the generation will be incremented before
269 * each element's allocation.
271 e
->lt_id
.generation
= 0;
272 e
->lt_next_idx
= l
+ 1;
275 /* make sure the last free element points to a never-valid idx */
276 e
= lt_elem_ofst_slab(base
[0], slab_msk
, (slab_elem
- 1) * elem_sz
);
277 e
->lt_next_idx
= LT_IDX_MAX
;
279 lck_mtx_init(&table
->lock
, &g_lt_lck_grp
, LCK_ATTR_NULL
);
281 table
->slab_sz
= slab_sz
;
282 table
->slab_shift
= slab_shift
;
283 table
->slab_msk
= slab_msk
;
284 table
->slab_elem
= slab_elem
;
285 table
->slab_zone
= slab_zone
;
287 table
->elem_sz
= elem_sz
;
288 table
->nelem
= slab_elem
;
289 table
->used_elem
= 0;
290 table
->elem_sz
= elem_sz
;
291 table
->poison
= poison
;
294 table
->next_free_slab
= &base
[1];
295 table
->free_list
.id
= base
[0]->lt_id
.id
;
297 #if CONFIG_LTABLE_STATS
300 table
->nreallocs
= 0;
301 table
->npreposts
= 0;
302 table
->nreservations
= 0;
303 table
->nreserved_releases
= 0;
307 table
->max_reservations
= 0;
308 table
->avg_reservations
= 0;
314 * ltable_grow: grow a link table by adding another 'slab' of table elements
317 * table mutex is unlocked
318 * calling thread can block
321 ltable_grow(struct link_table
*table
, uint32_t min_free
)
323 struct lt_elem
*slab
, **slot
;
324 struct lt_elem
*e
= NULL
, *first_new_elem
, *last_new_elem
;
325 struct ltable_id free_id
;
328 assert(get_preemption_level() == 0);
329 assert(table
&& table
->slab_zone
);
331 lck_mtx_lock(&table
->lock
);
333 free_elem
= table
->nelem
- table
->used_elem
;
336 * If the caller just wanted to ensure a minimum number of elements,
337 * do that (and don't just blindly grow the table). Also, don't grow
338 * the table unnecessarily - we could have been beaten by a higher
339 * priority thread who acquired the lock and grew the table before we
342 if (free_elem
> min_free
) {
343 lck_mtx_unlock(&table
->lock
);
347 /* we are now committed to table growth */
350 if (table
->next_free_slab
== NULL
) {
352 * before we panic, check one more time to see if any other
353 * threads have free'd from space in the table.
355 if ((table
->nelem
- table
->used_elem
) > 0) {
356 /* there's at least 1 free element: don't panic yet */
357 lck_mtx_unlock(&table
->lock
);
360 panic("No more room to grow table: %p (nelem: %d, used: %d)",
361 table
, table
->nelem
, table
->used_elem
);
363 slot
= table
->next_free_slab
;
364 table
->next_free_slab
++;
365 if ((uintptr_t)table
->next_free_slab
>= (uintptr_t)table
->table
+ PAGE_SIZE
) {
366 table
->next_free_slab
= NULL
;
369 assert(*slot
== NULL
);
371 /* allocate another slab */
372 slab
= (struct lt_elem
*)zalloc(table
->slab_zone
);
374 panic("Can't allocate a %s table (%p) slab from zone:%p",
375 table
->slab_zone
->zone_name
, table
, table
->slab_zone
);
378 memset(slab
, 0, table
->slab_sz
);
380 /* put the new elements into a freelist */
381 ltdbg_v(" init %d new links...", table
->slab_elem
);
382 for (unsigned l
= 0; l
< table
->slab_elem
; l
++) {
383 uint32_t idx
= l
+ table
->nelem
;
384 if (idx
>= (LT_IDX_MAX
- 1)) {
385 break; /* the last element of the last slab */
387 e
= lt_elem_ofst_slab(slab
, table
->slab_msk
, l
* table
->elem_sz
);
389 e
->lt_next_idx
= idx
+ 1;
392 assert(last_new_elem
!= NULL
);
394 first_new_elem
= lt_elem_ofst_slab(slab
, table
->slab_msk
, 0);
396 /* update table book keeping, and atomically swap the freelist head */
398 if (table
->nelem
+ table
->slab_elem
>= LT_IDX_MAX
) {
399 table
->nelem
= LT_IDX_MAX
- 1;
401 table
->nelem
+= table
->slab_elem
;
404 #if CONFIG_LTABLE_STATS
409 * The atomic swap of the free list head marks the end of table
410 * growth. Incoming requests may now use the newly allocated slab
413 free_id
= table
->free_list
;
414 /* connect the existing free list to the end of the new free list */
415 last_new_elem
->lt_next_idx
= free_id
.idx
;
416 while (OSCompareAndSwap64(free_id
.id
, first_new_elem
->lt_id
.id
,
417 &table
->free_list
.id
) == FALSE
) {
419 free_id
= table
->free_list
;
420 last_new_elem
->lt_next_idx
= free_id
.idx
;
424 lck_mtx_unlock(&table
->lock
);
429 #if DEVELOPMENT || DEBUG
432 ltable_nelem(struct link_table
*table
)
436 lck_mtx_lock(&table
->lock
);
438 nelem
= table
->used_elem
;
440 lck_mtx_unlock(&table
->lock
);
447 * ltable_alloc_elem: allocate one or more elements from a given table
449 * The returned element(s) will be of type 'type', but will remain invalid.
451 * If the caller has disabled preemption, then this function may (rarely) spin
452 * waiting either for another thread to either release 'nelem' table elements,
455 * If the caller can block, then this function may (rarely) block while
456 * the table grows to meet the demand for 'nelem' element(s).
458 __attribute__((noinline
))
460 ltable_alloc_elem(struct link_table
*table
, int type
,
461 int nelem
, int nattempts
)
463 int nspins
= 0, ntries
= 0, nalloc
= 0;
465 struct lt_elem
*elem
= NULL
;
466 struct ltable_id free_id
, next_id
;
468 static const int max_retries
= 500;
470 if (type
!= LT_ELEM
&& type
!= LT_LINK
&& type
!= LT_RESERVED
) {
471 panic("link_table_aloc of invalid elem type:%d from table @%p",
478 * If the callers only wants to try a certain number of times, make it
479 * look like we've already made (MAX - nattempts) tries at allocation
481 if (nattempts
> 0 && nattempts
<= max_retries
) {
482 ntries
= max_retries
- nattempts
;
487 if (ntries
++ > max_retries
) {
491 * The caller specified a particular number of
492 * attempts before failure, so it's expected that
493 * they're prepared to handle a NULL return.
498 if (table
->used_elem
+ nelem
>= table_size
) {
499 panic("No more room to grow table: 0x%p size:%d, used:%d, requested elem:%d",
500 table
, table_size
, table
->used_elem
, nelem
);
503 panic("Too many alloc retries: %d, table:%p, type:%d, nelem:%d",
504 ntries
, table
, type
, nelem
);
506 /* don't panic: try allocating one-at-a-time */
508 tmp
= ltable_alloc_elem(table
, type
, 1, nattempts
);
510 lt_elem_list_link(table
, tmp
, elem
);
515 assert(elem
!= NULL
);
520 table_size
= table
->nelem
;
522 if (table
->used_elem
+ nelem
>= table_size
) {
523 if (get_preemption_level() != 0) {
524 #if CONFIG_LTABLE_STATS
528 * We may have just raced with table growth: check
529 * again to make sure there really isn't any space.
532 panic("Can't grow table %p with preemption"
533 " disabled!", table
);
538 ltable_grow(table
, nelem
);
542 /* read this value only once before the CAS */
543 free_id
= table
->free_list
;
544 if (free_id
.idx
>= table_size
) {
549 * Find the item on the free list which will become the new free list
550 * head, but be careful not to modify any memory (read only)! Other
551 * threads can alter table state at any time up until the CAS. We
552 * don't modify any memory until we've successfully swapped out the
553 * free list head with the one we've investigated.
555 for (struct lt_elem
*next_elem
= lt_elem_idx(table
, free_id
.idx
);
559 next_id
.generation
= 0;
560 next_id
.idx
= next_elem
->lt_next_idx
;
561 if (next_id
.idx
< table
->nelem
) {
562 next_elem
= lt_elem_idx(table
, next_id
.idx
);
563 next_id
.id
= next_elem
->lt_id
.id
;
568 /* 'elem' points to the last element being allocated */
570 if (OSCompareAndSwap64(free_id
.id
, next_id
.id
,
571 &table
->free_list
.id
) == FALSE
) {
579 * After the CAS, we know that we own free_id, and it points to a
580 * valid table entry (checked above). Grab the table pointer and
583 OSAddAtomic(nelem
, &table
->used_elem
);
585 /* end the list of allocated elements */
586 elem
->lt_next_idx
= LT_IDX_MAX
;
587 /* reset 'elem' to point to the first allocated element */
588 elem
= lt_elem_idx(table
, free_id
.idx
);
591 * Update the generation count, and return the element(s)
592 * with a single reference (and no valid bit). If the
593 * caller immediately calls _put() on any element, then
594 * it will be released back to the free list. If the caller
595 * subsequently marks the element as valid, then the put
596 * will simply drop the reference.
598 for (struct lt_elem
*tmp
= elem
;;) {
599 assert(!lt_bits_valid(tmp
->lt_bits
) &&
600 (lt_bits_refcnt(tmp
->lt_bits
) == 0));
602 tmp
->lt_id
.generation
+= 1;
604 lt_elem_set_type(tmp
, type
);
605 if (tmp
->lt_next_idx
== LT_IDX_MAX
) {
608 assert(tmp
->lt_next_idx
!= LT_IDX_MAX
);
609 tmp
= lt_elem_idx(table
, tmp
->lt_next_idx
);
613 #if CONFIG_LTABLE_STATS
614 uint64_t nreservations
;
615 table
->nallocs
+= nelem
;
616 if (type
== LT_RESERVED
) {
617 OSIncrementAtomic64(&table
->nreservations
);
619 nreservations
= table
->nreservations
;
620 if (table
->used_elem
> table
->max_used
) {
621 table
->max_used
= table
->used_elem
;
623 if (nreservations
> table
->max_reservations
) {
624 table
->max_reservations
= nreservations
;
626 table
->avg_used
= (table
->avg_used
+ table
->used_elem
) / 2;
627 table
->avg_reservations
= (table
->avg_reservations
+ nreservations
) / 2;
635 * ltable_realloc_elem: convert a reserved element to a particular type
637 * This funciton is used to convert reserved elements (not yet marked valid)
638 * to the given 'type'. The generation of 'elem' is incremented, the element
639 * is disconnected from any list to which it belongs, and its type is set to
643 ltable_realloc_elem(struct link_table
*table
, struct lt_elem
*elem
, int type
)
646 assert(lt_elem_in_range(elem
, table
) &&
647 !lt_bits_valid(elem
->lt_bits
));
649 #if CONFIG_LTABLE_STATS
650 table
->nreallocs
+= 1;
651 if (lt_bits_type(elem
->lt_bits
) == LT_RESERVED
&& type
!= LT_RESERVED
) {
653 * This isn't under any lock, so we'll clamp it.
654 * the stats are meant to be informative, not perfectly
657 OSDecrementAtomic64(&table
->nreservations
);
659 table
->avg_reservations
= (table
->avg_reservations
+ table
->nreservations
) / 2;
663 * Return the same element with a new generation count, and a
664 * (potentially) new type. Don't touch the refcount: the caller
665 * is responsible for getting that (and the valid bit) correct.
667 elem
->lt_id
.generation
+= 1;
668 elem
->lt_next_idx
= LT_IDX_MAX
;
669 lt_elem_set_type(elem
, type
);
676 * ltable_free_elem: release an element back to a link table
678 * Do not call this function directly: use ltable_[get|put]_elem!
681 * 'elem' was originally allocated from 'table'
682 * 'elem' is _not_ marked valid
683 * 'elem' has a reference count of 0
686 ltable_free_elem(struct link_table
*table
, struct lt_elem
*elem
)
688 struct ltable_id next_id
;
690 assert(lt_elem_in_range(elem
, table
) &&
691 !lt_bits_valid(elem
->lt_bits
) &&
692 (lt_bits_refcnt(elem
->lt_bits
) == 0));
694 OSDecrementAtomic(&table
->used_elem
);
696 #if CONFIG_LTABLE_STATS
697 table
->avg_used
= (table
->avg_used
+ table
->used_elem
) / 2;
698 if (lt_bits_type(elem
->lt_bits
) == LT_RESERVED
) {
699 OSDecrementAtomic64(&table
->nreservations
);
701 table
->avg_reservations
= (table
->avg_reservations
+ table
->nreservations
) / 2;
707 (table
->poison
)(table
, elem
);
711 next_id
= table
->free_list
;
712 if (next_id
.idx
>= table
->nelem
) {
713 elem
->lt_next_idx
= LT_IDX_MAX
;
715 elem
->lt_next_idx
= next_id
.idx
;
720 if (OSCompareAndSwap64(next_id
.id
, elem
->lt_id
.id
,
721 &table
->free_list
.id
) == FALSE
) {
728 * ltable_get_elem: get a reference to a table element identified by 'id'
730 * Returns a reference to the table element associated with the given 'id', or
731 * NULL if the 'id' was invalid or does not exist in 'table'. The caller is
732 * responsible to release the reference using ltable_put_elem().
734 * NOTE: if the table element pointed to by 'id' is marked as invalid,
735 * this function will return NULL.
738 ltable_get_elem(struct link_table
*table
, uint64_t id
)
740 struct lt_elem
*elem
;
741 uint32_t idx
, bits
, new_bits
;
744 * Here we have a reference to the table which is guaranteed to remain
745 * valid until we drop the reference
748 idx
= ((struct ltable_id
*)&id
)->idx
;
750 if (idx
>= table
->nelem
) {
751 panic("id:0x%llx : idx:%d > %d", id
, idx
, table
->nelem
);
754 elem
= lt_elem_idx(table
, idx
);
756 /* verify the validity by taking a reference on the table object */
757 bits
= elem
->lt_bits
;
758 if (!lt_bits_valid(bits
)) {
763 * do a pre-verify on the element ID to potentially
764 * avoid 2 compare-and-swaps
766 if (elem
->lt_id
.id
!= id
) {
772 /* check for overflow */
773 assert(lt_bits_refcnt(new_bits
) > 0);
775 while (OSCompareAndSwap(bits
, new_bits
, &elem
->lt_bits
) == FALSE
) {
777 * either the element became invalid,
778 * or someone else grabbed/removed a reference.
780 bits
= elem
->lt_bits
;
781 if (!lt_bits_valid(bits
)) {
782 /* don't return invalid elements */
786 assert(lt_bits_refcnt(new_bits
) > 0);
792 /* check to see that our reference is to the same generation! */
793 if (elem
->lt_id
.id
!= id
) {
795 * ltdbg("ID:0x%llx table generation (%d) != %d",
796 * id, elem->lt_id.generation,
797 * ((struct ltable_id *)&id)->generation);
799 ltable_put_elem(table
, elem
);
803 /* We now have a reference on a valid object */
808 * ltable_put_elem: release a reference to table element
810 * This function releases a reference taken on a table element via
811 * ltable_get_elem(). This function will release the element back to 'table'
812 * when the reference count goes to 0 AND the element has been marked as
816 ltable_put_elem(struct link_table
*table
, struct lt_elem
*elem
)
818 uint32_t bits
, new_bits
;
820 assert(lt_elem_in_range(elem
, table
));
822 bits
= elem
->lt_bits
;
825 /* check for underflow */
826 assert(lt_bits_refcnt(new_bits
) < LT_BITS_REFCNT_MASK
);
828 while (OSCompareAndSwap(bits
, new_bits
, &elem
->lt_bits
) == FALSE
) {
829 bits
= elem
->lt_bits
;
831 /* catch underflow */
832 assert(lt_bits_refcnt(new_bits
) < LT_BITS_REFCNT_MASK
);
839 * if this was the last reference, and it was marked as invalid,
840 * then we can add this link object back to the free list
842 if (!lt_bits_valid(new_bits
) && (lt_bits_refcnt(new_bits
) == 0)) {
843 ltable_free_elem(table
, elem
);
850 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
852 * API: lt_elem_list_...
854 * Reuse the free list linkage member, 'lt_next_idx' of a table element
855 * in a slightly more generic singly-linked list. All members of this
856 * list have been allocated from a table, but have not been made valid.
858 * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
861 * lt_elem_list_link: link a child onto a parent
863 * Note that if 'parent' is the head of a list, this function will follow that
864 * list and attach 'child' to the end of it. In the simplest case, this
865 * results in: parent->child
866 * however this could also result in: parent->...->child
869 lt_elem_list_link(struct link_table
*table
, struct lt_elem
*parent
, struct lt_elem
*child
)
873 assert(lt_elem_in_range(parent
, table
));
875 /* find the end of the parent's list */
876 while (parent
->lt_next_idx
!= LT_IDX_MAX
) {
877 assert(parent
->lt_next_idx
< table
->nelem
);
878 parent
= lt_elem_idx(table
, parent
->lt_next_idx
);
883 assert(lt_elem_in_range(child
, table
));
884 parent
->lt_next_idx
= child
->lt_id
.idx
;
892 * lt_elem_list_first: obtain a pointer to the first element of a list.
894 * This function converts the head of a singly-linked list, 'id', into a real
895 * lt_elem object and returns a pointer to the object.
897 * It does _not_ take an extra reference on the object: the list implicitly
898 * holds that reference.
901 lt_elem_list_first(struct link_table
*table
, uint64_t id
)
904 struct lt_elem
*elem
= NULL
;
910 idx
= ((struct ltable_id
*)&id
)->idx
;
912 if (idx
> table
->nelem
) {
913 panic("Invalid element for id:0x%llx", id
);
915 elem
= lt_elem_idx(table
, idx
);
917 /* invalid element: reserved ID was probably already reallocated */
918 if (elem
->lt_id
.id
!= id
) {
922 /* the returned element should _not_ be marked valid! */
923 if (lt_bits_valid(elem
->lt_bits
) ||
924 lt_bits_type(elem
->lt_bits
) != LT_RESERVED
||
925 lt_bits_refcnt(elem
->lt_bits
) != 1) {
926 panic("Valid/unreserved element %p (0x%x) in reserved list",
927 elem
, elem
->lt_bits
);
935 * lt_elem_list_next: return the item subsequent to 'elem' in a list
937 * Note that this will return NULL if 'elem' is actually the end of the list.
940 lt_elem_list_next(struct link_table
*table
, struct lt_elem
*head
)
942 struct lt_elem
*elem
;
947 if (head
->lt_next_idx
>= table
->nelem
) {
951 elem
= lt_elem_idx(table
, head
->lt_next_idx
);
952 assert(lt_elem_in_range(elem
, table
));
959 * lt_elem_list_break: break a list in two around 'elem'
961 * This function will reset the next_idx field of 'elem' (making it the end of
962 * the list), and return the element subsequent to 'elem' in the list
963 * (which could be NULL)
966 lt_elem_list_break(struct link_table
*table
, struct lt_elem
*elem
)
968 struct lt_elem
*next
;
973 next
= lt_elem_list_next(table
, elem
);
974 elem
->lt_next_idx
= LT_IDX_MAX
;
981 * lt_elem_list_pop: pop an item off the head of a list
983 * The list head is pointed to by '*id', the element corresponding to '*id' is
984 * returned by this function, and the new list head is returned in the in/out
985 * parameter, '*id'. The caller is responsible for the reference on the
986 * returned object. A realloc is done to reset the type of the object, but it
987 * is still left invalid.
990 lt_elem_list_pop(struct link_table
*table
, uint64_t *id
, int type
)
992 struct lt_elem
*first
, *next
;
994 if (!id
|| *id
== 0) {
998 /* pop an item off the reserved stack */
1000 first
= lt_elem_list_first(table
, *id
);
1006 next
= lt_elem_list_next(table
, first
);
1008 *id
= next
->lt_id
.id
;
1013 ltable_realloc_elem(table
, first
, type
);
1019 * lt_elem_list_release: free an entire list of reserved elements
1021 * All elements in the list whose first member is 'head' will be released back
1022 * to 'table' as free elements. The 'type' parameter is used in development
1023 * kernels to assert that all elements on the list are of the given type.
1026 lt_elem_list_release(struct link_table
*table
, struct lt_elem
*head
,
1027 int __assert_only type
)
1029 struct lt_elem
*elem
;
1030 struct ltable_id free_id
;
1037 for (elem
= head
;;) {
1038 assert(lt_elem_in_range(elem
, table
));
1039 assert(!lt_bits_valid(elem
->lt_bits
) && (lt_bits_refcnt(elem
->lt_bits
) == 1));
1040 assert(lt_bits_type(elem
->lt_bits
) == type
);
1044 if (table
->poison
) {
1045 (table
->poison
)(table
, elem
);
1048 if (elem
->lt_next_idx
== LT_IDX_MAX
) {
1051 assert(elem
->lt_next_idx
< table
->nelem
);
1052 elem
= lt_elem_idx(table
, elem
->lt_next_idx
);
1056 * 'elem' now points to the end of our list, and 'head' points to the
1057 * beginning. We want to atomically swap the free list pointer with
1058 * the 'head' and ensure that 'elem' points to the previous free list
1063 free_id
= table
->free_list
;
1064 if (free_id
.idx
>= table
->nelem
) {
1065 elem
->lt_next_idx
= LT_IDX_MAX
;
1067 elem
->lt_next_idx
= free_id
.idx
;
1072 if (OSCompareAndSwap64(free_id
.id
, head
->lt_id
.id
,
1073 &table
->free_list
.id
) == FALSE
) {
1077 OSAddAtomic(-nelem
, &table
->used_elem
);