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