xref: /xnu-8020.140.41/libkern/os/log_mem.c (revision 27b03b360a988dfd3dfdf34262bb0042026747cc)
1*27b03b36SApple OSS Distributions /*
2*27b03b36SApple OSS Distributions  * Copyright (c) 2020-2021 Apple Inc. All rights reserved.
3*27b03b36SApple OSS Distributions  *
4*27b03b36SApple OSS Distributions  * @APPLE_LICENSE_HEADER_START@
5*27b03b36SApple OSS Distributions  *
6*27b03b36SApple OSS Distributions  * This file contains Original Code and/or Modifications of Original Code
7*27b03b36SApple OSS Distributions  * as defined in and that are subject to the Apple Public Source License
8*27b03b36SApple OSS Distributions  * Version 2.0 (the 'License'). You may not use this file except in
9*27b03b36SApple OSS Distributions  * compliance with the License. Please obtain a copy of the License at
10*27b03b36SApple OSS Distributions  * http://www.opensource.apple.com/apsl/ and read it before using this
11*27b03b36SApple OSS Distributions  * file.
12*27b03b36SApple OSS Distributions  *
13*27b03b36SApple OSS Distributions  * The Original Code and all software distributed under the License are
14*27b03b36SApple OSS Distributions  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15*27b03b36SApple OSS Distributions  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16*27b03b36SApple OSS Distributions  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17*27b03b36SApple OSS Distributions  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18*27b03b36SApple OSS Distributions  * Please see the License for the specific language governing rights and
19*27b03b36SApple OSS Distributions  * limitations under the License.
20*27b03b36SApple OSS Distributions  *
21*27b03b36SApple OSS Distributions  * @APPLE_LICENSE_HEADER_END@
22*27b03b36SApple OSS Distributions  */
23*27b03b36SApple OSS Distributions 
24*27b03b36SApple OSS Distributions #include <stdbool.h>
25*27b03b36SApple OSS Distributions #include <stdint.h>
26*27b03b36SApple OSS Distributions #include <kern/assert.h>
27*27b03b36SApple OSS Distributions #include <kern/locks.h>
28*27b03b36SApple OSS Distributions #include <os/atomic_private.h>
29*27b03b36SApple OSS Distributions 
30*27b03b36SApple OSS Distributions #include "log_mem.h"
31*27b03b36SApple OSS Distributions 
32*27b03b36SApple OSS Distributions #define BLOCK_INVALID ((size_t)-1)
33*27b03b36SApple OSS Distributions #define BLOCK_LEVEL_BASE(level) ((1 << (level)) - 1)
34*27b03b36SApple OSS Distributions #define BLOCK_SIZE(level) (1 << (level))
35*27b03b36SApple OSS Distributions #define BLOCK_PARENT(b) (((b) % 2 == 0) ? ((b) >> 1) - 1 : ((b) >> 1))
36*27b03b36SApple OSS Distributions #define BLOCK_LCHILD(b) (((b) << 1) + 1)
37*27b03b36SApple OSS Distributions #define BLOCK_BUDDY(b) (((b) & 0x1) ? (b) + 1 : (b) - 1)
38*27b03b36SApple OSS Distributions #define BLOCK_INDEX(lm, l, a, s) \
39*27b03b36SApple OSS Distributions     (BLOCK_LEVEL_BASE(l) + ((uintptr_t)(a) - (uintptr_t)(lm)->lm_mem) / (s))
40*27b03b36SApple OSS Distributions 
41*27b03b36SApple OSS Distributions #define MAP_SIZE(size_order, min_order) \
42*27b03b36SApple OSS Distributions     MAX(1, (1 << ((size_order) - (min_order) + 1)) / 8)
43*27b03b36SApple OSS Distributions 
44*27b03b36SApple OSS Distributions #define BITMAP_BUCKET_SIZE (8 * sizeof(((logmem_t *)0)->lm_mem_map[0]))
45*27b03b36SApple OSS Distributions #define BITMAP_BUCKET(i) ((i) / BITMAP_BUCKET_SIZE)
46*27b03b36SApple OSS Distributions #define BITMAP_BIT(i) (1 << (BITMAP_BUCKET_SIZE - ((i) % BITMAP_BUCKET_SIZE) - 1))
47*27b03b36SApple OSS Distributions 
48*27b03b36SApple OSS Distributions #define logmem_lock(lock, lm) if ((lock)) lck_spin_lock(&(lm)->lm_lock)
49*27b03b36SApple OSS Distributions #define logmem_unlock(lock, lm) if ((lock)) lck_spin_unlock(&(lm)->lm_lock)
50*27b03b36SApple OSS Distributions 
51*27b03b36SApple OSS Distributions static LCK_GRP_DECLARE(logmem_lck_grp, "logmem_lck_grp");
52*27b03b36SApple OSS Distributions 
53*27b03b36SApple OSS Distributions 
54*27b03b36SApple OSS Distributions static bool
bitmap_get(logmem_t * lm,size_t block)55*27b03b36SApple OSS Distributions bitmap_get(logmem_t *lm, size_t block)
56*27b03b36SApple OSS Distributions {
57*27b03b36SApple OSS Distributions 	return lm->lm_mem_map[BITMAP_BUCKET(block)] & BITMAP_BIT(block);
58*27b03b36SApple OSS Distributions }
59*27b03b36SApple OSS Distributions 
60*27b03b36SApple OSS Distributions static void
bitmap_set(logmem_t * lm,size_t block)61*27b03b36SApple OSS Distributions bitmap_set(logmem_t *lm, size_t block)
62*27b03b36SApple OSS Distributions {
63*27b03b36SApple OSS Distributions 	lm->lm_mem_map[BITMAP_BUCKET(block)] |= BITMAP_BIT(block);
64*27b03b36SApple OSS Distributions }
65*27b03b36SApple OSS Distributions 
66*27b03b36SApple OSS Distributions static void
bitmap_clear(logmem_t * lm,size_t block)67*27b03b36SApple OSS Distributions bitmap_clear(logmem_t *lm, size_t block)
68*27b03b36SApple OSS Distributions {
69*27b03b36SApple OSS Distributions 	lm->lm_mem_map[BITMAP_BUCKET(block)] &= ~BITMAP_BIT(block);
70*27b03b36SApple OSS Distributions }
71*27b03b36SApple OSS Distributions 
72*27b03b36SApple OSS Distributions static void
bitmap_reserve_root(logmem_t * lm,size_t block)73*27b03b36SApple OSS Distributions bitmap_reserve_root(logmem_t *lm, size_t block)
74*27b03b36SApple OSS Distributions {
75*27b03b36SApple OSS Distributions 	const size_t top_block = BLOCK_LEVEL_BASE(lm->lm_cap_order - lm->lm_max_order);
76*27b03b36SApple OSS Distributions 
77*27b03b36SApple OSS Distributions 	for (ssize_t next = BLOCK_PARENT(block); next >= top_block; next = BLOCK_PARENT(next)) {
78*27b03b36SApple OSS Distributions 		/*
79*27b03b36SApple OSS Distributions 		 * If the rest of the root path is already marked as
80*27b03b36SApple OSS Distributions 		 * allocated we are done.
81*27b03b36SApple OSS Distributions 		 */
82*27b03b36SApple OSS Distributions 		if (bitmap_get(lm, next)) {
83*27b03b36SApple OSS Distributions 			break;
84*27b03b36SApple OSS Distributions 		}
85*27b03b36SApple OSS Distributions 		bitmap_set(lm, next);
86*27b03b36SApple OSS Distributions 	}
87*27b03b36SApple OSS Distributions }
88*27b03b36SApple OSS Distributions 
89*27b03b36SApple OSS Distributions static void
bitmap_release_root(logmem_t * lm,size_t block)90*27b03b36SApple OSS Distributions bitmap_release_root(logmem_t *lm, size_t block)
91*27b03b36SApple OSS Distributions {
92*27b03b36SApple OSS Distributions 	const size_t top_block = BLOCK_LEVEL_BASE(lm->lm_cap_order - lm->lm_max_order);
93*27b03b36SApple OSS Distributions 	int buddy_allocated = 0;
94*27b03b36SApple OSS Distributions 
95*27b03b36SApple OSS Distributions 	while (block > top_block) {
96*27b03b36SApple OSS Distributions 		buddy_allocated = bitmap_get(lm, BLOCK_BUDDY(block));
97*27b03b36SApple OSS Distributions 		block = BLOCK_PARENT(block);
98*27b03b36SApple OSS Distributions 		/*
99*27b03b36SApple OSS Distributions 		 * If there is another allocation within the parent subtree
100*27b03b36SApple OSS Distributions 		 * in place we cannot mark the rest of the root path as free.
101*27b03b36SApple OSS Distributions 		 */
102*27b03b36SApple OSS Distributions 		if (buddy_allocated) {
103*27b03b36SApple OSS Distributions 			break;
104*27b03b36SApple OSS Distributions 		}
105*27b03b36SApple OSS Distributions 		bitmap_clear(lm, block);
106*27b03b36SApple OSS Distributions 	}
107*27b03b36SApple OSS Distributions }
108*27b03b36SApple OSS Distributions 
109*27b03b36SApple OSS Distributions static void
bitmap_update_subtree(logmem_t * lm,size_t level,size_t block,void (* fun)(logmem_t *,size_t))110*27b03b36SApple OSS Distributions bitmap_update_subtree(logmem_t *lm, size_t level, size_t block, void (*fun)(logmem_t *, size_t))
111*27b03b36SApple OSS Distributions {
112*27b03b36SApple OSS Distributions 	const size_t lcount = lm->lm_cap_order - lm->lm_min_order - level + 1;
113*27b03b36SApple OSS Distributions 
114*27b03b36SApple OSS Distributions 	for (size_t l = 0, n = 1; l < lcount; l++, n <<= 1) {
115*27b03b36SApple OSS Distributions 		for (int i = 0; i < n; i++) {
116*27b03b36SApple OSS Distributions 			fun(lm, block + i);
117*27b03b36SApple OSS Distributions 		}
118*27b03b36SApple OSS Distributions 		block = BLOCK_LCHILD(block);
119*27b03b36SApple OSS Distributions 	}
120*27b03b36SApple OSS Distributions }
121*27b03b36SApple OSS Distributions 
122*27b03b36SApple OSS Distributions static void
bitmap_release_subtree(logmem_t * lm,size_t level,size_t block)123*27b03b36SApple OSS Distributions bitmap_release_subtree(logmem_t *lm, size_t level, size_t block)
124*27b03b36SApple OSS Distributions {
125*27b03b36SApple OSS Distributions 	bitmap_update_subtree(lm, level, block, bitmap_clear);
126*27b03b36SApple OSS Distributions }
127*27b03b36SApple OSS Distributions 
128*27b03b36SApple OSS Distributions static void
bitmap_reserve_subtree(logmem_t * lm,size_t level,size_t block)129*27b03b36SApple OSS Distributions bitmap_reserve_subtree(logmem_t *lm, size_t level, size_t block)
130*27b03b36SApple OSS Distributions {
131*27b03b36SApple OSS Distributions 	bitmap_update_subtree(lm, level, block, bitmap_set);
132*27b03b36SApple OSS Distributions }
133*27b03b36SApple OSS Distributions 
134*27b03b36SApple OSS Distributions static size_t
block_size_level(logmem_t * lm,size_t amount)135*27b03b36SApple OSS Distributions block_size_level(logmem_t *lm, size_t amount)
136*27b03b36SApple OSS Distributions {
137*27b03b36SApple OSS Distributions 	for (size_t l = lm->lm_min_order; l <= lm->lm_max_order; l++) {
138*27b03b36SApple OSS Distributions 		if (amount <= BLOCK_SIZE(l)) {
139*27b03b36SApple OSS Distributions 			return lm->lm_cap_order - l;
140*27b03b36SApple OSS Distributions 		}
141*27b03b36SApple OSS Distributions 	}
142*27b03b36SApple OSS Distributions 	return BLOCK_INVALID;
143*27b03b36SApple OSS Distributions }
144*27b03b36SApple OSS Distributions 
145*27b03b36SApple OSS Distributions static size_t
block_locate(logmem_t * lm,void * addr,size_t amount,size_t * block)146*27b03b36SApple OSS Distributions block_locate(logmem_t *lm, void *addr, size_t amount, size_t *block)
147*27b03b36SApple OSS Distributions {
148*27b03b36SApple OSS Distributions 	size_t level = block_size_level(lm, amount);
149*27b03b36SApple OSS Distributions 	if (level != BLOCK_INVALID) {
150*27b03b36SApple OSS Distributions 		*block = BLOCK_INDEX(lm, level, addr, amount);
151*27b03b36SApple OSS Distributions 	}
152*27b03b36SApple OSS Distributions 	return level;
153*27b03b36SApple OSS Distributions }
154*27b03b36SApple OSS Distributions 
155*27b03b36SApple OSS Distributions static size_t
block_reserve(logmem_t * lm,size_t level)156*27b03b36SApple OSS Distributions block_reserve(logmem_t *lm, size_t level)
157*27b03b36SApple OSS Distributions {
158*27b03b36SApple OSS Distributions 	assert(level != BLOCK_INVALID);
159*27b03b36SApple OSS Distributions 
160*27b03b36SApple OSS Distributions 	const size_t base = BLOCK_LEVEL_BASE(level);
161*27b03b36SApple OSS Distributions 	const size_t end = base + BLOCK_SIZE(level);
162*27b03b36SApple OSS Distributions 
163*27b03b36SApple OSS Distributions 	for (size_t block = base; block < end; block++) {
164*27b03b36SApple OSS Distributions 		if (!bitmap_get(lm, block)) {
165*27b03b36SApple OSS Distributions 			bitmap_reserve_root(lm, block);
166*27b03b36SApple OSS Distributions 			bitmap_reserve_subtree(lm, level, block);
167*27b03b36SApple OSS Distributions 			return block - base;
168*27b03b36SApple OSS Distributions 		}
169*27b03b36SApple OSS Distributions 	}
170*27b03b36SApple OSS Distributions 
171*27b03b36SApple OSS Distributions 	return BLOCK_INVALID;
172*27b03b36SApple OSS Distributions }
173*27b03b36SApple OSS Distributions 
174*27b03b36SApple OSS Distributions static void *
logmem_alloc_impl(logmem_t * lm,size_t * amount,bool lock_lm)175*27b03b36SApple OSS Distributions logmem_alloc_impl(logmem_t *lm, size_t *amount, bool lock_lm)
176*27b03b36SApple OSS Distributions {
177*27b03b36SApple OSS Distributions 	assert(amount);
178*27b03b36SApple OSS Distributions 
179*27b03b36SApple OSS Distributions 	os_atomic_inc(&lm->lm_cnt_allocations, relaxed);
180*27b03b36SApple OSS Distributions 
181*27b03b36SApple OSS Distributions 	if (!lm->lm_mem) {
182*27b03b36SApple OSS Distributions 		os_atomic_inc(&lm->lm_cnt_failed_lmoff, relaxed);
183*27b03b36SApple OSS Distributions 		return NULL;
184*27b03b36SApple OSS Distributions 	}
185*27b03b36SApple OSS Distributions 
186*27b03b36SApple OSS Distributions 	if (*amount == 0 || *amount > BLOCK_SIZE(lm->lm_max_order)) {
187*27b03b36SApple OSS Distributions 		os_atomic_inc(&lm->lm_cnt_failed_size, relaxed);
188*27b03b36SApple OSS Distributions 		return NULL;
189*27b03b36SApple OSS Distributions 	}
190*27b03b36SApple OSS Distributions 
191*27b03b36SApple OSS Distributions 	size_t level = block_size_level(lm, *amount);
192*27b03b36SApple OSS Distributions 	logmem_lock(lock_lm, lm);
193*27b03b36SApple OSS Distributions 	size_t block = block_reserve(lm, level);
194*27b03b36SApple OSS Distributions 	logmem_unlock(lock_lm, lm);
195*27b03b36SApple OSS Distributions 
196*27b03b36SApple OSS Distributions 	if (block == BLOCK_INVALID) {
197*27b03b36SApple OSS Distributions 		os_atomic_inc(&lm->lm_cnt_failed_full, relaxed);
198*27b03b36SApple OSS Distributions 		return NULL;
199*27b03b36SApple OSS Distributions 	}
200*27b03b36SApple OSS Distributions 
201*27b03b36SApple OSS Distributions 	*amount = BLOCK_SIZE(lm->lm_cap_order - level);
202*27b03b36SApple OSS Distributions 	os_atomic_sub(&lm->lm_cnt_free, (uint32_t)*amount, relaxed);
203*27b03b36SApple OSS Distributions 
204*27b03b36SApple OSS Distributions 	return &lm->lm_mem[block * *amount];
205*27b03b36SApple OSS Distributions }
206*27b03b36SApple OSS Distributions 
207*27b03b36SApple OSS Distributions void *
logmem_alloc_locked(logmem_t * lm,size_t * amount)208*27b03b36SApple OSS Distributions logmem_alloc_locked(logmem_t *lm, size_t *amount)
209*27b03b36SApple OSS Distributions {
210*27b03b36SApple OSS Distributions 	return logmem_alloc_impl(lm, amount, true);
211*27b03b36SApple OSS Distributions }
212*27b03b36SApple OSS Distributions 
213*27b03b36SApple OSS Distributions void *
logmem_alloc(logmem_t * lm,size_t * amount)214*27b03b36SApple OSS Distributions logmem_alloc(logmem_t *lm, size_t *amount)
215*27b03b36SApple OSS Distributions {
216*27b03b36SApple OSS Distributions 	return logmem_alloc_impl(lm, amount, false);
217*27b03b36SApple OSS Distributions }
218*27b03b36SApple OSS Distributions 
219*27b03b36SApple OSS Distributions static void
logmem_free_impl(logmem_t * lm,void * addr,size_t amount,bool lock_lm)220*27b03b36SApple OSS Distributions logmem_free_impl(logmem_t *lm, void *addr, size_t amount, bool lock_lm)
221*27b03b36SApple OSS Distributions {
222*27b03b36SApple OSS Distributions 	assert(addr);
223*27b03b36SApple OSS Distributions 	assert(amount > 0 && ((amount & (amount - 1)) == 0));
224*27b03b36SApple OSS Distributions 
225*27b03b36SApple OSS Distributions 	size_t block = BLOCK_INVALID;
226*27b03b36SApple OSS Distributions 	size_t level = block_locate(lm, addr, amount, &block);
227*27b03b36SApple OSS Distributions 	assert(level != BLOCK_INVALID);
228*27b03b36SApple OSS Distributions 	assert(block != BLOCK_INVALID);
229*27b03b36SApple OSS Distributions 
230*27b03b36SApple OSS Distributions 	assert(lm->lm_mem);
231*27b03b36SApple OSS Distributions 
232*27b03b36SApple OSS Distributions 	logmem_lock(lock_lm, lm);
233*27b03b36SApple OSS Distributions 	bitmap_release_root(lm, block);
234*27b03b36SApple OSS Distributions 	bitmap_release_subtree(lm, level, block);
235*27b03b36SApple OSS Distributions 	logmem_unlock(lock_lm, lm);
236*27b03b36SApple OSS Distributions 
237*27b03b36SApple OSS Distributions 	os_atomic_add(&lm->lm_cnt_free, (uint32_t)amount, relaxed);
238*27b03b36SApple OSS Distributions }
239*27b03b36SApple OSS Distributions 
240*27b03b36SApple OSS Distributions void
logmem_free_locked(logmem_t * lm,void * addr,size_t amount)241*27b03b36SApple OSS Distributions logmem_free_locked(logmem_t *lm, void *addr, size_t amount)
242*27b03b36SApple OSS Distributions {
243*27b03b36SApple OSS Distributions 	logmem_free_impl(lm, addr, amount, true);
244*27b03b36SApple OSS Distributions }
245*27b03b36SApple OSS Distributions 
246*27b03b36SApple OSS Distributions void
logmem_free(logmem_t * lm,void * addr,size_t amount)247*27b03b36SApple OSS Distributions logmem_free(logmem_t *lm, void *addr, size_t amount)
248*27b03b36SApple OSS Distributions {
249*27b03b36SApple OSS Distributions 	logmem_free_impl(lm, addr, amount, false);
250*27b03b36SApple OSS Distributions }
251*27b03b36SApple OSS Distributions 
252*27b03b36SApple OSS Distributions size_t
logmem_required_size(size_t size_order,size_t min_order)253*27b03b36SApple OSS Distributions logmem_required_size(size_t size_order, size_t min_order)
254*27b03b36SApple OSS Distributions {
255*27b03b36SApple OSS Distributions 	return round_page(BLOCK_SIZE(size_order)) + round_page(MAP_SIZE(size_order, min_order));
256*27b03b36SApple OSS Distributions }
257*27b03b36SApple OSS Distributions 
258*27b03b36SApple OSS Distributions size_t
logmem_max_size(const logmem_t * lm)259*27b03b36SApple OSS Distributions logmem_max_size(const logmem_t *lm)
260*27b03b36SApple OSS Distributions {
261*27b03b36SApple OSS Distributions 	return BLOCK_SIZE(lm->lm_max_order);
262*27b03b36SApple OSS Distributions }
263*27b03b36SApple OSS Distributions 
264*27b03b36SApple OSS Distributions bool
logmem_empty(const logmem_t * lm)265*27b03b36SApple OSS Distributions logmem_empty(const logmem_t *lm)
266*27b03b36SApple OSS Distributions {
267*27b03b36SApple OSS Distributions 	return lm->lm_cnt_free == BLOCK_SIZE(lm->lm_cap_order);
268*27b03b36SApple OSS Distributions }
269*27b03b36SApple OSS Distributions 
270*27b03b36SApple OSS Distributions bool
logmem_ready(const logmem_t * lm)271*27b03b36SApple OSS Distributions logmem_ready(const logmem_t *lm)
272*27b03b36SApple OSS Distributions {
273*27b03b36SApple OSS Distributions 	return lm->lm_mem != NULL;
274*27b03b36SApple OSS Distributions }
275*27b03b36SApple OSS Distributions 
276*27b03b36SApple OSS Distributions void
logmem_init(logmem_t * lm,void * lm_mem,size_t lm_mem_size,size_t size_order,size_t min_order,size_t max_order)277*27b03b36SApple OSS Distributions logmem_init(logmem_t *lm, void *lm_mem, size_t lm_mem_size, size_t size_order, size_t min_order, size_t max_order)
278*27b03b36SApple OSS Distributions {
279*27b03b36SApple OSS Distributions 	assert(lm_mem_size >= logmem_required_size(size_order, min_order));
280*27b03b36SApple OSS Distributions 	assert(size_order >= max_order);
281*27b03b36SApple OSS Distributions 	assert(max_order >= min_order);
282*27b03b36SApple OSS Distributions 
283*27b03b36SApple OSS Distributions 	bzero(lm, sizeof(*lm));
284*27b03b36SApple OSS Distributions 	bzero(lm_mem, lm_mem_size);
285*27b03b36SApple OSS Distributions 
286*27b03b36SApple OSS Distributions 	lck_spin_init(&lm->lm_lock, &logmem_lck_grp, LCK_ATTR_NULL);
287*27b03b36SApple OSS Distributions 	lm->lm_mem = lm_mem;
288*27b03b36SApple OSS Distributions 	lm->lm_mem_map = (uint8_t *)((uintptr_t)lm_mem + round_page(BLOCK_SIZE(size_order)));
289*27b03b36SApple OSS Distributions 	lm->lm_mem_size = lm_mem_size;
290*27b03b36SApple OSS Distributions 	lm->lm_cap_order = size_order;
291*27b03b36SApple OSS Distributions 	lm->lm_min_order = min_order;
292*27b03b36SApple OSS Distributions 	lm->lm_max_order = max_order;
293*27b03b36SApple OSS Distributions 	lm->lm_cnt_free = BLOCK_SIZE(size_order);
294*27b03b36SApple OSS Distributions }
295