1 // SPDX-License-Identifier: BSD-2-Clause 2 /* 3 * Copyright (c) 2014, STMicroelectronics International N.V. 4 * Copyright (c) 2018, 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 if (refcount_inc(&pool->refc)) { 78 if (pool->owner == thread_get_id()) 79 return; 80 refcount_dec(&pool->refc); 81 } 82 83 mutex_lock(&pool->mu); 84 85 /* Wait until the pool is available */ 86 while (pool->owner != THREAD_ID_INVALID) 87 condvar_wait(&pool->cv, &pool->mu); 88 89 pool->owner = thread_get_id(); 90 refcount_set(&pool->refc, 1); 91 92 mutex_unlock(&pool->mu); 93 #endif 94 } 95 96 static void put_pool(struct mempool *pool __maybe_unused) 97 { 98 #if defined(__KERNEL__) 99 assert(pool->owner == thread_get_id()); 100 101 if (refcount_dec(&pool->refc)) { 102 mutex_lock(&pool->mu); 103 104 pool->owner = THREAD_ID_INVALID; 105 condvar_signal(&pool->cv); 106 107 /* As the refcount is 0 there should be no items left */ 108 if (pool->last_offset >= 0) 109 panic(); 110 if (pool->release_mem) 111 pool->release_mem((void *)pool->data, pool->size); 112 113 mutex_unlock(&pool->mu); 114 } 115 #endif 116 } 117 118 struct mempool * 119 mempool_alloc_pool(void *data, size_t size, 120 void (*release_mem)(void *ptr, size_t size) __maybe_unused) 121 { 122 struct mempool *pool = calloc(1, sizeof(*pool)); 123 124 COMPILE_TIME_ASSERT(POOL_ALIGN >= __alignof__(struct mempool_item)); 125 assert(!((vaddr_t)data & (POOL_ALIGN - 1))); 126 127 if (pool) { 128 pool->size = size; 129 pool->data = (vaddr_t)data; 130 pool->last_offset = -1; 131 #if defined(__KERNEL__) 132 pool->release_mem = release_mem; 133 mutex_init(&pool->mu); 134 condvar_init(&pool->cv); 135 pool->owner = THREAD_ID_INVALID; 136 #endif 137 } 138 139 return pool; 140 } 141 142 void *mempool_alloc(struct mempool *pool, size_t size) 143 { 144 size_t offset; 145 struct mempool_item *new_item; 146 struct mempool_item *last_item = NULL; 147 148 get_pool(pool); 149 150 if (pool->last_offset < 0) { 151 offset = 0; 152 } else { 153 last_item = (struct mempool_item *)(pool->data + 154 pool->last_offset); 155 offset = pool->last_offset + last_item->size; 156 157 offset = ROUNDUP(offset, POOL_ALIGN); 158 if (offset > pool->size) 159 goto error; 160 } 161 162 size = sizeof(struct mempool_item) + size; 163 size = ROUNDUP(size, POOL_ALIGN); 164 if (offset + size > pool->size) 165 goto error; 166 167 new_item = (struct mempool_item *)(pool->data + offset); 168 new_item->size = size; 169 new_item->prev_item_offset = pool->last_offset; 170 if (last_item) 171 last_item->next_item_offset = offset; 172 new_item->next_item_offset = -1; 173 pool->last_offset = offset; 174 #ifdef CFG_MEMPOOL_REPORT_LAST_OFFSET 175 if (pool->last_offset > pool->max_last_offset) { 176 pool->max_last_offset = pool->last_offset; 177 DMSG("Max memory usage increased to %zu", 178 (size_t)pool->max_last_offset); 179 } 180 #endif 181 182 return new_item + 1; 183 184 error: 185 EMSG("Failed to allocate %zu bytes, please tune the pool size", size); 186 put_pool(pool); 187 return NULL; 188 } 189 190 void *mempool_calloc(struct mempool *pool, size_t nmemb, size_t size) 191 { 192 size_t sz; 193 void *p; 194 195 if (MUL_OVERFLOW(nmemb, size, &sz)) 196 return NULL; 197 198 p = mempool_alloc(pool, sz); 199 if (p) 200 memset(p, 0, sz); 201 202 return p; 203 } 204 205 void mempool_free(struct mempool *pool, void *ptr) 206 { 207 struct mempool_item *item; 208 struct mempool_item *prev_item; 209 struct mempool_item *next_item; 210 ssize_t last_offset = -1; 211 212 if (!ptr) 213 return; 214 215 item = (struct mempool_item *)((vaddr_t)ptr - 216 sizeof(struct mempool_item)); 217 if (item->prev_item_offset >= 0) { 218 prev_item = (struct mempool_item *)(pool->data + 219 item->prev_item_offset); 220 prev_item->next_item_offset = item->next_item_offset; 221 last_offset = item->prev_item_offset; 222 } 223 224 if (item->next_item_offset >= 0) { 225 next_item = (struct mempool_item *)(pool->data + 226 item->next_item_offset); 227 next_item->prev_item_offset = item->prev_item_offset; 228 last_offset = pool->last_offset; 229 } 230 231 pool->last_offset = last_offset; 232 put_pool(pool); 233 } 234