xref: /xnu-10063.121.3/osfmk/kern/compact_id.c (revision 2c2f96dc2b9a4408a43d3150ae9c105355ca3daa)
1*2c2f96dcSApple OSS Distributions /*
2*2c2f96dcSApple OSS Distributions  * Copyright (c) 2022 Apple Inc. All rights reserved.
3*2c2f96dcSApple OSS Distributions  *
4*2c2f96dcSApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5*2c2f96dcSApple OSS Distributions  *
6*2c2f96dcSApple OSS Distributions  * This file contains Original Code and/or Modifications of Original Code
7*2c2f96dcSApple OSS Distributions  * as defined in and that are subject to the Apple Public Source License
8*2c2f96dcSApple OSS Distributions  * Version 2.0 (the 'License'). You may not use this file except in
9*2c2f96dcSApple OSS Distributions  * compliance with the License. The rights granted to you under the License
10*2c2f96dcSApple OSS Distributions  * may not be used to create, or enable the creation or redistribution of,
11*2c2f96dcSApple OSS Distributions  * unlawful or unlicensed copies of an Apple operating system, or to
12*2c2f96dcSApple OSS Distributions  * circumvent, violate, or enable the circumvention or violation of, any
13*2c2f96dcSApple OSS Distributions  * terms of an Apple operating system software license agreement.
14*2c2f96dcSApple OSS Distributions  *
15*2c2f96dcSApple OSS Distributions  * Please obtain a copy of the License at
16*2c2f96dcSApple OSS Distributions  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17*2c2f96dcSApple OSS Distributions  *
18*2c2f96dcSApple OSS Distributions  * The Original Code and all software distributed under the License are
19*2c2f96dcSApple OSS Distributions  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20*2c2f96dcSApple OSS Distributions  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21*2c2f96dcSApple OSS Distributions  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22*2c2f96dcSApple OSS Distributions  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23*2c2f96dcSApple OSS Distributions  * Please see the License for the specific language governing rights and
24*2c2f96dcSApple OSS Distributions  * limitations under the License.
25*2c2f96dcSApple OSS Distributions  *
26*2c2f96dcSApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27*2c2f96dcSApple OSS Distributions  */
28*2c2f96dcSApple OSS Distributions 
29*2c2f96dcSApple OSS Distributions #include <kern/compact_id.h>
30*2c2f96dcSApple OSS Distributions #include <kern/locks.h>
31*2c2f96dcSApple OSS Distributions #include <kern/thread.h>
32*2c2f96dcSApple OSS Distributions 
33*2c2f96dcSApple OSS Distributions static LCK_GRP_DECLARE(compact_id_lck_grp, "compact_id");
34*2c2f96dcSApple OSS Distributions 
35*2c2f96dcSApple OSS Distributions #define compact_id_table_sleep(table) \
36*2c2f96dcSApple OSS Distributions 	lck_mtx_sleep_with_inheritor(&(table)->cidt_lock, \
37*2c2f96dcSApple OSS Distributions 	    LCK_SLEEP_DEFAULT, (event_t)(table), (table)->cidt_allocator, \
38*2c2f96dcSApple OSS Distributions 	    THREAD_UNINT, TIMEOUT_WAIT_FOREVER)
39*2c2f96dcSApple OSS Distributions #define compact_id_table_wake(table) \
40*2c2f96dcSApple OSS Distributions 	wakeup_all_with_inheritor((event_t)(table), THREAD_AWAKENED)
41*2c2f96dcSApple OSS Distributions 
42*2c2f96dcSApple OSS Distributions static inline uint32_t
compact_id_prev_max(uint32_t idx)43*2c2f96dcSApple OSS Distributions compact_id_prev_max(uint32_t idx)
44*2c2f96dcSApple OSS Distributions {
45*2c2f96dcSApple OSS Distributions 	/*
46*2c2f96dcSApple OSS Distributions 	 * |BASE|BASE|2BASE|4BASE|...|2^(index-2)BASE|2^(index-1)BASE|
47*2c2f96dcSApple OSS Distributions 	 *   0    1     2     3          index - 1        index
48*2c2f96dcSApple OSS Distributions 	 *
49*2c2f96dcSApple OSS Distributions 	 * the maximum number of values that can be stored on all
50*2c2f96dcSApple OSS Distributions 	 * entries previous to table_index is:
51*2c2f96dcSApple OSS Distributions 	 * CTID_BASE_TABLE * 2^(table_index - 1)
52*2c2f96dcSApple OSS Distributions 	 * for table_index greater then 0.
53*2c2f96dcSApple OSS Distributions 	 */
54*2c2f96dcSApple OSS Distributions 	return idx ? COMPACT_ID_COUNT_BASE << (idx - 1) : 0;
55*2c2f96dcSApple OSS Distributions }
56*2c2f96dcSApple OSS Distributions 
57*2c2f96dcSApple OSS Distributions static uint32_t
compact_id_slab_size(uint32_t table_index)58*2c2f96dcSApple OSS Distributions compact_id_slab_size(uint32_t table_index)
59*2c2f96dcSApple OSS Distributions {
60*2c2f96dcSApple OSS Distributions 	if (table_index == 0) {
61*2c2f96dcSApple OSS Distributions 		return COMPACT_ID_COUNT_BASE;
62*2c2f96dcSApple OSS Distributions 	}
63*2c2f96dcSApple OSS Distributions 	return COMPACT_ID_COUNT_BASE << (table_index - 1);
64*2c2f96dcSApple OSS Distributions }
65*2c2f96dcSApple OSS Distributions 
66*2c2f96dcSApple OSS Distributions static uint32_t
compact_id_slab_index(compact_id_t cid)67*2c2f96dcSApple OSS Distributions compact_id_slab_index(compact_id_t cid)
68*2c2f96dcSApple OSS Distributions {
69*2c2f96dcSApple OSS Distributions 	/*
70*2c2f96dcSApple OSS Distributions 	 * The first 2 entries have size COMPACT_ID_COUNT_BASE,
71*2c2f96dcSApple OSS Distributions 	 * all others have size COMPACT_ID_COUNT_BASE * 2^(i - 1).
72*2c2f96dcSApple OSS Distributions 	 *
73*2c2f96dcSApple OSS Distributions 	 * If you get the the number of the most significant 0s
74*2c2f96dcSApple OSS Distributions 	 * on COMPACT_ID_COUNT_BASE and you subtract how many there
75*2c2f96dcSApple OSS Distributions 	 * are in cidt, you get the index in the table.
76*2c2f96dcSApple OSS Distributions 	 */
77*2c2f96dcSApple OSS Distributions 	cid |= COMPACT_ID_COUNT_BASE - 1;
78*2c2f96dcSApple OSS Distributions 	return __builtin_clz(COMPACT_ID_COUNT_BASE) - __builtin_clz(cid) + 1;
79*2c2f96dcSApple OSS Distributions }
80*2c2f96dcSApple OSS Distributions 
81*2c2f96dcSApple OSS Distributions void
compact_id_table_init(compact_id_table_t table)82*2c2f96dcSApple OSS Distributions compact_id_table_init(compact_id_table_t table)
83*2c2f96dcSApple OSS Distributions {
84*2c2f96dcSApple OSS Distributions 	/* lck_group_init knows about what this function does */
85*2c2f96dcSApple OSS Distributions 	lck_mtx_init(&table->cidt_lock, &compact_id_lck_grp, LCK_ATTR_NULL);
86*2c2f96dcSApple OSS Distributions }
87*2c2f96dcSApple OSS Distributions 
88*2c2f96dcSApple OSS Distributions void **
compact_id_resolve(compact_id_table_t table,compact_id_t cid)89*2c2f96dcSApple OSS Distributions compact_id_resolve(compact_id_table_t table, compact_id_t cid)
90*2c2f96dcSApple OSS Distributions {
91*2c2f96dcSApple OSS Distributions 	return table->cidt_array[compact_id_slab_index(cid)] + cid;
92*2c2f96dcSApple OSS Distributions }
93*2c2f96dcSApple OSS Distributions 
94*2c2f96dcSApple OSS Distributions __attribute__((noinline))
95*2c2f96dcSApple OSS Distributions static void
compact_id_table_grow(compact_id_table_t table,uint32_t idx)96*2c2f96dcSApple OSS Distributions compact_id_table_grow(compact_id_table_t table, uint32_t idx)
97*2c2f96dcSApple OSS Distributions {
98*2c2f96dcSApple OSS Distributions 	uint32_t size;
99*2c2f96dcSApple OSS Distributions 
100*2c2f96dcSApple OSS Distributions 	/*
101*2c2f96dcSApple OSS Distributions 	 * Let's check if is someone is already
102*2c2f96dcSApple OSS Distributions 	 * allocating memory.
103*2c2f96dcSApple OSS Distributions 	 */
104*2c2f96dcSApple OSS Distributions 	if (table->cidt_allocator != NULL) {
105*2c2f96dcSApple OSS Distributions 		table->cidt_waiters = true;
106*2c2f96dcSApple OSS Distributions 		compact_id_table_sleep(table);
107*2c2f96dcSApple OSS Distributions 		return;
108*2c2f96dcSApple OSS Distributions 	}
109*2c2f96dcSApple OSS Distributions 
110*2c2f96dcSApple OSS Distributions 	/*
111*2c2f96dcSApple OSS Distributions 	 * We need to allocate more memory.
112*2c2f96dcSApple OSS Distributions 	 * Let's unlock and notify
113*2c2f96dcSApple OSS Distributions 	 * other thread who is allocating.
114*2c2f96dcSApple OSS Distributions 	 */
115*2c2f96dcSApple OSS Distributions 	table->cidt_allocator = current_thread();
116*2c2f96dcSApple OSS Distributions 	compact_id_table_unlock(table);
117*2c2f96dcSApple OSS Distributions 
118*2c2f96dcSApple OSS Distributions 	size = compact_id_slab_size(idx);
119*2c2f96dcSApple OSS Distributions 	table->cidt_bitmap[idx] = zalloc_permanent(BITMAP_SIZE(size), ZALIGN(bitmap_t));
120*2c2f96dcSApple OSS Distributions 	table->cidt_array[idx]  = zalloc_permanent(size * sizeof(thread_t), ZALIGN_PTR);
121*2c2f96dcSApple OSS Distributions 	/*
122*2c2f96dcSApple OSS Distributions 	 * Note: because we expect lookups to be common,
123*2c2f96dcSApple OSS Distributions 	 *       cidt_array isn't the real array but shifted
124*2c2f96dcSApple OSS Distributions 	 *       so that dereferencing it with the compact ID
125*2c2f96dcSApple OSS Distributions 	 *       works for any slab.
126*2c2f96dcSApple OSS Distributions 	 */
127*2c2f96dcSApple OSS Distributions 	table->cidt_array[idx] -= compact_id_prev_max(idx);
128*2c2f96dcSApple OSS Distributions 	bitmap_full(table->cidt_bitmap[idx], size);
129*2c2f96dcSApple OSS Distributions 
130*2c2f96dcSApple OSS Distributions 	compact_id_table_lock(table);
131*2c2f96dcSApple OSS Distributions 	assert(table->cidt_allocator == current_thread());
132*2c2f96dcSApple OSS Distributions 	table->cidt_allocator = NULL;
133*2c2f96dcSApple OSS Distributions 	if (table->cidt_waiters) {
134*2c2f96dcSApple OSS Distributions 		table->cidt_waiters = false;
135*2c2f96dcSApple OSS Distributions 		compact_id_table_wake(table);
136*2c2f96dcSApple OSS Distributions 	}
137*2c2f96dcSApple OSS Distributions }
138*2c2f96dcSApple OSS Distributions 
139*2c2f96dcSApple OSS Distributions compact_id_t
compact_id_get_locked(compact_id_table_t table,compact_id_t limit,void * value)140*2c2f96dcSApple OSS Distributions compact_id_get_locked(
141*2c2f96dcSApple OSS Distributions 	compact_id_table_t      table,
142*2c2f96dcSApple OSS Distributions 	compact_id_t            limit,
143*2c2f96dcSApple OSS Distributions 	void                   *value)
144*2c2f96dcSApple OSS Distributions {
145*2c2f96dcSApple OSS Distributions 	compact_id_t cid;
146*2c2f96dcSApple OSS Distributions 	uint32_t slab_size;
147*2c2f96dcSApple OSS Distributions 	uint32_t idx = 0;
148*2c2f96dcSApple OSS Distributions 	int bit_index;
149*2c2f96dcSApple OSS Distributions 
150*2c2f96dcSApple OSS Distributions again:
151*2c2f96dcSApple OSS Distributions 	idx = compact_id_slab_index(table->cidt_first_free);
152*2c2f96dcSApple OSS Distributions 	for (; idx < COMPACT_ID_SLAB_COUNT; idx++) {
153*2c2f96dcSApple OSS Distributions 		bitmap_t *map = table->cidt_bitmap[idx];
154*2c2f96dcSApple OSS Distributions 		void    **arr = table->cidt_array[idx];
155*2c2f96dcSApple OSS Distributions 
156*2c2f96dcSApple OSS Distributions 		if (arr == NULL) {
157*2c2f96dcSApple OSS Distributions 			compact_id_table_grow(table, idx);
158*2c2f96dcSApple OSS Distributions 			goto again;
159*2c2f96dcSApple OSS Distributions 		}
160*2c2f96dcSApple OSS Distributions 
161*2c2f96dcSApple OSS Distributions 		slab_size = compact_id_slab_size(idx);
162*2c2f96dcSApple OSS Distributions 		bit_index = bitmap_lsb_first(map, slab_size);
163*2c2f96dcSApple OSS Distributions 		if (bit_index >= 0) {
164*2c2f96dcSApple OSS Distributions 			cid = compact_id_prev_max(idx) + bit_index;
165*2c2f96dcSApple OSS Distributions 			if (cid > limit) {
166*2c2f96dcSApple OSS Distributions 				break;
167*2c2f96dcSApple OSS Distributions 			}
168*2c2f96dcSApple OSS Distributions 
169*2c2f96dcSApple OSS Distributions 			table->cidt_count++;
170*2c2f96dcSApple OSS Distributions 			table->cidt_first_free = cid + 1;
171*2c2f96dcSApple OSS Distributions 			bitmap_clear(map, bit_index);
172*2c2f96dcSApple OSS Distributions 			assert(arr[cid] == NULL);
173*2c2f96dcSApple OSS Distributions 			arr[cid] = value;
174*2c2f96dcSApple OSS Distributions 			return cid;
175*2c2f96dcSApple OSS Distributions 		}
176*2c2f96dcSApple OSS Distributions 	}
177*2c2f96dcSApple OSS Distributions 
178*2c2f96dcSApple OSS Distributions 	panic("table %p ran out of compact IDs", table);
179*2c2f96dcSApple OSS Distributions }
180*2c2f96dcSApple OSS Distributions 
181*2c2f96dcSApple OSS Distributions compact_id_t
compact_id_get(compact_id_table_t table,compact_id_t limit,void * value)182*2c2f96dcSApple OSS Distributions compact_id_get(
183*2c2f96dcSApple OSS Distributions 	compact_id_table_t      table,
184*2c2f96dcSApple OSS Distributions 	compact_id_t            limit,
185*2c2f96dcSApple OSS Distributions 	void                   *value)
186*2c2f96dcSApple OSS Distributions {
187*2c2f96dcSApple OSS Distributions 	compact_id_t cid;
188*2c2f96dcSApple OSS Distributions 
189*2c2f96dcSApple OSS Distributions 	compact_id_table_lock(table);
190*2c2f96dcSApple OSS Distributions 	cid = compact_id_get_locked(table, limit, value);
191*2c2f96dcSApple OSS Distributions 	compact_id_table_unlock(table);
192*2c2f96dcSApple OSS Distributions 	return cid;
193*2c2f96dcSApple OSS Distributions }
194*2c2f96dcSApple OSS Distributions 
195*2c2f96dcSApple OSS Distributions void *
compact_id_put(compact_id_table_t table,compact_id_t cid)196*2c2f96dcSApple OSS Distributions compact_id_put(compact_id_table_t table, compact_id_t cid)
197*2c2f96dcSApple OSS Distributions {
198*2c2f96dcSApple OSS Distributions 	uint32_t  idx = compact_id_slab_index(cid);
199*2c2f96dcSApple OSS Distributions 	bitmap_t *map = table->cidt_bitmap[idx];
200*2c2f96dcSApple OSS Distributions 	void    **arr = table->cidt_array[idx];
201*2c2f96dcSApple OSS Distributions 	void *value;
202*2c2f96dcSApple OSS Distributions 
203*2c2f96dcSApple OSS Distributions 	compact_id_table_lock(table);
204*2c2f96dcSApple OSS Distributions 	value = arr[cid];
205*2c2f96dcSApple OSS Distributions 	arr[cid] = NULL;
206*2c2f96dcSApple OSS Distributions 	bitmap_set(map, cid - compact_id_prev_max(idx));
207*2c2f96dcSApple OSS Distributions 	if (cid < table->cidt_first_free) {
208*2c2f96dcSApple OSS Distributions 		table->cidt_first_free = cid;
209*2c2f96dcSApple OSS Distributions 	}
210*2c2f96dcSApple OSS Distributions 	table->cidt_count--;
211*2c2f96dcSApple OSS Distributions 	compact_id_table_unlock(table);
212*2c2f96dcSApple OSS Distributions 
213*2c2f96dcSApple OSS Distributions 	return value;
214*2c2f96dcSApple OSS Distributions }
215*2c2f96dcSApple OSS Distributions 
216*2c2f96dcSApple OSS Distributions void
217*2c2f96dcSApple OSS Distributions compact_id_for_each(
218*2c2f96dcSApple OSS Distributions 	compact_id_table_t      table,
219*2c2f96dcSApple OSS Distributions 	uint32_t                stride,
220*2c2f96dcSApple OSS Distributions 	bool                  (^cb)(void *v))
221*2c2f96dcSApple OSS Distributions {
222*2c2f96dcSApple OSS Distributions 	const uint64_t pause_init = stride;
223*2c2f96dcSApple OSS Distributions 	uint64_t pause = pause_init;
224*2c2f96dcSApple OSS Distributions 
225*2c2f96dcSApple OSS Distributions 	compact_id_table_lock(table);
226*2c2f96dcSApple OSS Distributions 	for (uint32_t sidx = 0; sidx < COMPACT_ID_SLAB_COUNT; sidx++) {
227*2c2f96dcSApple OSS Distributions 		void    **arr = table->cidt_array[sidx];
228*2c2f96dcSApple OSS Distributions 		uint32_t  size = compact_id_slab_size(sidx);
229*2c2f96dcSApple OSS Distributions 		uint32_t  prev = compact_id_prev_max(sidx);
230*2c2f96dcSApple OSS Distributions 
231*2c2f96dcSApple OSS Distributions 		if (arr == NULL) {
232*2c2f96dcSApple OSS Distributions 			break;
233*2c2f96dcSApple OSS Distributions 		}
234*2c2f96dcSApple OSS Distributions 		for (compact_id_t cid = prev; cid < prev + size; cid++) {
235*2c2f96dcSApple OSS Distributions 			if (arr[cid] == NULL) {
236*2c2f96dcSApple OSS Distributions 				continue;
237*2c2f96dcSApple OSS Distributions 			}
238*2c2f96dcSApple OSS Distributions 			if (!cb(arr[cid])) {
239*2c2f96dcSApple OSS Distributions 				break;
240*2c2f96dcSApple OSS Distributions 			}
241*2c2f96dcSApple OSS Distributions 			if (pause-- == 0) {
242*2c2f96dcSApple OSS Distributions 				compact_id_table_unlock(table);
243*2c2f96dcSApple OSS Distributions 				pause = pause_init;
244*2c2f96dcSApple OSS Distributions 				compact_id_table_lock(table);
245*2c2f96dcSApple OSS Distributions 			}
246*2c2f96dcSApple OSS Distributions 		}
247*2c2f96dcSApple OSS Distributions 	}
248*2c2f96dcSApple OSS Distributions 	compact_id_table_unlock(table);
249*2c2f96dcSApple OSS Distributions }
250*2c2f96dcSApple OSS Distributions 
251*2c2f96dcSApple OSS Distributions void
compact_id_table_lock(compact_id_table_t table)252*2c2f96dcSApple OSS Distributions compact_id_table_lock(compact_id_table_t table)
253*2c2f96dcSApple OSS Distributions {
254*2c2f96dcSApple OSS Distributions 	lck_mtx_lock(&table->cidt_lock);
255*2c2f96dcSApple OSS Distributions }
256*2c2f96dcSApple OSS Distributions 
257*2c2f96dcSApple OSS Distributions void
compact_id_table_unlock(compact_id_table_t table)258*2c2f96dcSApple OSS Distributions compact_id_table_unlock(compact_id_table_t table)
259*2c2f96dcSApple OSS Distributions {
260*2c2f96dcSApple OSS Distributions 	lck_mtx_unlock(&table->cidt_lock);
261*2c2f96dcSApple OSS Distributions }
262