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