1 // SPDX-License-Identifier: BSD-2-Clause 2 /* 3 * Copyright (c) 2014, STMicroelectronics International N.V. 4 * Copyright (c) 2018-2019, Linaro Limited 5 */ 6 7 8 #include <assert.h> 9 #include <compiler.h> 10 #include <malloc.h> 11 #include <mempool.h> 12 #include <string.h> 13 #include <util.h> 14 15 #if defined(__KERNEL__) 16 #include <kernel/mutex.h> 17 #include <kernel/panic.h> 18 #include <kernel/thread.h> 19 #include <kernel/refcount.h> 20 #endif 21 22 /* 23 * Allocation of temporary memory buffers which are used in a stack like 24 * fashion. One exmaple is when a Big Number is needed for a temporary 25 * variable in a Big Number computation: Big Number operations (add,...), 26 * crypto algorithms (rsa, ecc,,...). 27 * 28 * The allocation algorithm takes memory buffers from a pool, 29 * characterized by (cf. struct mempool): 30 * - the total size (in bytes) of the pool 31 * - the offset of the last item allocated in the pool (struct 32 * mempool_item). This offset is -1 is nothing is allocated yet. 33 * 34 * Each item consists of (struct mempool_item) 35 * - the size of the item 36 * - the offsets, in the pool, of the previous and next items 37 * 38 * The allocation allocates an item for a given size. 39 * The allocation is performed in the pool after the last 40 * allocated items. This means: 41 * - the heap is never used. 42 * - there is no assumption on the size of the allocated memory buffers. Only 43 * the size of the pool will limit the allocation. 44 * - a constant time allocation and free as there is no list scan 45 * - but a potentially fragmented memory as the allocation does not take into 46 * account "holes" in the pool (allocation is performed after the last 47 * allocated variable). Indeed, this interface is supposed to be used 48 * with stack like allocations to avoid this issue. This means that 49 * allocated items: 50 * - should have a short life cycle 51 * - if an item A is allocated before another item B, then A should be 52 * released after B. 53 * So the potential fragmentation is mitigated. 54 */ 55 56 #define POOL_ALIGN __alignof__(long) 57 58 struct mempool { 59 size_t size; /* size of the memory pool, in bytes */ 60 ssize_t last_offset; /* offset to the last one */ 61 vaddr_t data; 62 #ifdef CFG_MEMPOOL_REPORT_LAST_OFFSET 63 ssize_t max_last_offset; 64 #endif 65 #if defined(__KERNEL__) 66 void (*release_mem)(void *ptr, size_t size); 67 struct mutex mu; 68 struct condvar cv; 69 struct refcount refc; 70 int owner; 71 #endif 72 }; 73 74 static void get_pool(struct mempool *pool __maybe_unused) 75 { 76 #if defined(__KERNEL__) 77 /* 78 * Owner matches our thread it cannot be changed. If it doesn't 79 * match it can change any at time we're not holding the mutex to 80 * any value but our thread id. 81 */ 82 if (atomic_load_int(&pool->owner) == thread_get_id()) { 83 if (!refcount_inc(&pool->refc)) 84 panic(); 85 return; 86 } 87 88 mutex_lock(&pool->mu); 89 90 /* Wait until the pool is available */ 91 while (pool->owner != THREAD_ID_INVALID) 92 condvar_wait(&pool->cv, &pool->mu); 93 94 pool->owner = thread_get_id(); 95 refcount_set(&pool->refc, 1); 96 97 mutex_unlock(&pool->mu); 98 #endif 99 } 100 101 static void put_pool(struct mempool *pool __maybe_unused) 102 { 103 #if defined(__KERNEL__) 104 assert(atomic_load_int(&pool->owner) == thread_get_id()); 105 106 if (refcount_dec(&pool->refc)) { 107 mutex_lock(&pool->mu); 108 109 /* 110 * Do an atomic store to match the atomic load in 111 * get_pool() above. 112 */ 113 atomic_store_int(&pool->owner, THREAD_ID_INVALID); 114 condvar_signal(&pool->cv); 115 116 /* As the refcount is 0 there should be no items left */ 117 if (pool->last_offset >= 0) 118 panic(); 119 if (pool->release_mem) 120 pool->release_mem((void *)pool->data, pool->size); 121 122 mutex_unlock(&pool->mu); 123 } 124 #endif 125 } 126 127 struct mempool * 128 mempool_alloc_pool(void *data, size_t size, 129 void (*release_mem)(void *ptr, size_t size) __maybe_unused) 130 { 131 struct mempool *pool = calloc(1, sizeof(*pool)); 132 133 COMPILE_TIME_ASSERT(POOL_ALIGN >= __alignof__(struct mempool_item)); 134 assert(!((vaddr_t)data & (POOL_ALIGN - 1))); 135 136 if (pool) { 137 pool->size = size; 138 pool->data = (vaddr_t)data; 139 pool->last_offset = -1; 140 #if defined(__KERNEL__) 141 pool->release_mem = release_mem; 142 mutex_init(&pool->mu); 143 condvar_init(&pool->cv); 144 pool->owner = THREAD_ID_INVALID; 145 #endif 146 } 147 148 return pool; 149 } 150 151 void *mempool_alloc(struct mempool *pool, size_t size) 152 { 153 size_t offset; 154 struct mempool_item *new_item; 155 struct mempool_item *last_item = NULL; 156 157 get_pool(pool); 158 159 if (pool->last_offset < 0) { 160 offset = 0; 161 } else { 162 last_item = (struct mempool_item *)(pool->data + 163 pool->last_offset); 164 offset = pool->last_offset + last_item->size; 165 166 offset = ROUNDUP(offset, POOL_ALIGN); 167 if (offset > pool->size) 168 goto error; 169 } 170 171 size = sizeof(struct mempool_item) + size; 172 size = ROUNDUP(size, POOL_ALIGN); 173 if (offset + size > pool->size) 174 goto error; 175 176 new_item = (struct mempool_item *)(pool->data + offset); 177 new_item->size = size; 178 new_item->prev_item_offset = pool->last_offset; 179 if (last_item) 180 last_item->next_item_offset = offset; 181 new_item->next_item_offset = -1; 182 pool->last_offset = offset; 183 #ifdef CFG_MEMPOOL_REPORT_LAST_OFFSET 184 if (pool->last_offset > pool->max_last_offset) { 185 pool->max_last_offset = pool->last_offset; 186 DMSG("Max memory usage increased to %zu", 187 (size_t)pool->max_last_offset); 188 } 189 #endif 190 191 return new_item + 1; 192 193 error: 194 EMSG("Failed to allocate %zu bytes, please tune the pool size", size); 195 put_pool(pool); 196 return NULL; 197 } 198 199 void *mempool_calloc(struct mempool *pool, size_t nmemb, size_t size) 200 { 201 size_t sz; 202 void *p; 203 204 if (MUL_OVERFLOW(nmemb, size, &sz)) 205 return NULL; 206 207 p = mempool_alloc(pool, sz); 208 if (p) 209 memset(p, 0, sz); 210 211 return p; 212 } 213 214 void mempool_free(struct mempool *pool, void *ptr) 215 { 216 struct mempool_item *item; 217 struct mempool_item *prev_item; 218 struct mempool_item *next_item; 219 ssize_t last_offset = -1; 220 221 if (!ptr) 222 return; 223 224 item = (struct mempool_item *)((vaddr_t)ptr - 225 sizeof(struct mempool_item)); 226 if (item->prev_item_offset >= 0) { 227 prev_item = (struct mempool_item *)(pool->data + 228 item->prev_item_offset); 229 prev_item->next_item_offset = item->next_item_offset; 230 last_offset = item->prev_item_offset; 231 } 232 233 if (item->next_item_offset >= 0) { 234 next_item = (struct mempool_item *)(pool->data + 235 item->next_item_offset); 236 next_item->prev_item_offset = item->prev_item_offset; 237 last_offset = pool->last_offset; 238 } 239 240 pool->last_offset = last_offset; 241 put_pool(pool); 242 } 243