1 /** 2 * Copyright (C) Advanced Micro Devices, Inc. All rights reserved. 3 * 4 * You may not use this software and documentation (if any) (collectively, the 5 * "Materials") except in compliance with the terms and conditions of the 6 * Software License Agreement included with the Materials or otherwise as set 7 * forth in writing and signed by you and an authorized signatory of AMD. 8 * 9 * If you do not have a copy of the Software License Agreement, contact your AMD 10 * representative for a copy. You agree that you will not reverse engineer or 11 * decompile the Materials, in whole or in part, except as allowed by applicable 12 * law. 13 * 14 * THE MATERIALS ARE DISTRIBUTED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OR 15 * REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. 16 */ 17 #include "dc_memory_pool.h" 18 19 #include <linux/atomic.h> 20 21 enum { 22 DC_MEMORY_POOL_SENTINEL = -1, 23 DC_MEMORY_POOL_ACQUIRED = -2, 24 DC_MEMORY_POOL_RELEASED = -3, 25 26 // Correct autoincrement for negative numbers 27 DC_MEMORY_POOL_ENUM_SIZE_IMPL, 28 DC_MEMORY_POOL_ENUM_SIZE = 1 - DC_MEMORY_POOL_ENUM_SIZE_IMPL, 29 }; 30 31 // Align everything to size of page to avoid false sharing 32 struct dc_memory_pool_page { 33 __aligned(PAGE_SIZE) char _dummy[PAGE_SIZE]; 34 }; 35 36 // Generation counter protects against ABA-problem 37 union index_t { 38 struct { 39 int32_t value; 40 uint32_t generation; 41 } s; 42 43 int64_t raw; 44 }; 45 46 struct dc_memory_pool { 47 // Values and pointers constant after initialization, can share page 48 __aligned(PAGE_SIZE) size_t size; 49 size_t capacity; 50 51 void *unaligned_pool; 52 void *unaligned_memory; 53 struct dc_memory_pool_page *memory; 54 atomic_t *free_list; 55 56 // Updated every operation, use separate page to avoid false sharing 57 __aligned(PAGE_SIZE) atomic64_t free_head; // index_t 58 char _reserved2[PAGE_SIZE - sizeof(atomic64_t)]; 59 }; 60 61 static_assert( 62 sizeof(struct dc_memory_pool) == 2 * PAGE_SIZE, 63 "dc_memory_pool size should be 2 pages" 64 ); 65 static_assert( 66 offsetof(struct dc_memory_pool, free_head) == PAGE_SIZE, 67 "dc_memory_pool free_head should be aligned to page" 68 ); 69 70 static size_t divide_ceiling(size_t x, size_t d) 71 { 72 return (x + d - 1) / d; 73 } 74 75 static size_t round_up_to_multiple(size_t x, size_t m) 76 { 77 return divide_ceiling(x, m) * m; 78 } 79 80 static size_t size_in_pages(size_t x) 81 { 82 return divide_ceiling(x, PAGE_SIZE); 83 } 84 85 static void *page_align_up(void *p) 86 { 87 intptr_t i = (intptr_t)p; 88 89 i = (intptr_t)round_up_to_multiple((size_t)i, PAGE_SIZE); 90 return (void *)i; 91 } 92 93 __must_check struct dc_memory_pool *dc_memory_pool_create(size_t size, 94 size_t capacity) 95 { 96 if (!size || !capacity) 97 return NULL; 98 99 // Limited by split between size and generation in index_t 100 if (capacity >= (uint32_t)-DC_MEMORY_POOL_ENUM_SIZE) 101 return NULL; 102 103 // uint32_t because that's what alloc functions take 104 const uint32_t block_pages = (uint32_t)size_in_pages(size); 105 const uint32_t lines = (uint32_t)capacity * block_pages; 106 const uint32_t padded_struct_size = 107 sizeof(struct dc_memory_pool) + PAGE_SIZE; 108 109 void *unaligned_pool = kzalloc(padded_struct_size, GFP_KERNEL); 110 111 if (!unaligned_pool) 112 return NULL; 113 114 struct dc_memory_pool *pool = page_align_up(unaligned_pool); 115 116 // Compound literals create temporary in debug driver, exceeding stack size 117 pool->size = size; 118 pool->capacity = capacity; 119 pool->unaligned_pool = unaligned_pool; 120 pool->unaligned_memory = kcalloc(lines + 1, PAGE_SIZE, GFP_KERNEL); 121 pool->memory = page_align_up(pool->unaligned_memory); 122 pool->free_list = kzalloc_objs(atomic_t, (uint32_t)capacity); 123 pool->free_head = (atomic64_t)ATOMIC_INIT(0); 124 125 if (!pool->unaligned_memory || !pool->free_list) { 126 dc_memory_pool_destroy(pool); 127 return NULL; 128 } 129 130 for (int32_t i = 0; i < (int32_t)capacity; i++) 131 atomic_set_release(&pool->free_list[i], i + 1); 132 133 atomic_set_release(&pool->free_list[capacity - 1], 134 DC_MEMORY_POOL_SENTINEL); 135 136 return pool; 137 } 138 139 void dc_memory_pool_destroy(struct dc_memory_pool *pool) 140 { 141 if (!pool) 142 return; 143 144 kfree(pool->free_list); 145 kfree(pool->unaligned_memory); 146 kfree(pool->unaligned_pool); 147 } 148 149 static int32_t dc_memory_pool_get_index(const struct dc_memory_pool *pool, 150 const void *memory) 151 { 152 if (!pool || !memory) { 153 ASSERT(false); 154 return DC_MEMORY_POOL_SENTINEL; 155 } 156 157 // Direct pointer arithmetic would be UB if called by false `owns()` 158 if ((uintptr_t)memory < (uintptr_t)pool->memory) 159 return DC_MEMORY_POOL_SENTINEL; 160 161 const uintptr_t distance = (uintptr_t)memory - (uintptr_t)pool->memory; 162 const size_t block_size = size_in_pages(pool->size) * PAGE_SIZE; 163 const size_t i = (size_t)distance / block_size; 164 165 if (distance % (uintptr_t)block_size != 0) 166 return DC_MEMORY_POOL_SENTINEL; 167 168 if (i >= pool->capacity) 169 return DC_MEMORY_POOL_SENTINEL; 170 171 return (int32_t)i; 172 } 173 174 static void *dc_memory_pool_get_page(const struct dc_memory_pool *pool, 175 int32_t index) 176 { 177 if (!pool) { 178 ASSERT(false); 179 return NULL; 180 } 181 182 if (index < 0 || index >= (int32_t)pool->capacity) { 183 ASSERT(false); 184 return NULL; 185 } 186 187 return &pool->memory[(size_t)index * size_in_pages(pool->size)]; 188 } 189 190 __must_check void *dc_memory_pool_acquire(struct dc_memory_pool *pool) 191 { 192 if (!pool) { 193 ASSERT(false); 194 return NULL; 195 } 196 197 // CAS-atomic `out = head; head = head->next;` 198 union index_t old_head = { 199 .raw = atomic64_read_acquire(&pool->free_head), 200 }; 201 union index_t new_head = { 202 .raw = 0, 203 }; 204 int32_t i = 0; 205 206 do { 207 i = old_head.s.value; 208 if (i == DC_MEMORY_POOL_SENTINEL) 209 return NULL; 210 211 new_head = (union index_t){ 212 .s.value = atomic_read_acquire(&pool->free_list[i]), 213 .s.generation = old_head.s.generation + 1, 214 }; 215 } while (!atomic64_try_cmpxchg(&pool->free_head, &old_head.raw, 216 new_head.raw)); 217 218 atomic_set_release(&pool->free_list[i], DC_MEMORY_POOL_ACQUIRED); 219 return dc_memory_pool_get_page(pool, i); 220 } 221 222 void dc_memory_pool_release(struct dc_memory_pool *pool, void *memory) 223 { 224 if (!dc_memory_pool_owns(pool, memory)) { 225 // Likely acquired in different pool or (racing?) double free 226 ASSERT(false); 227 return; 228 } 229 230 const int32_t i = dc_memory_pool_get_index(pool, memory); 231 232 if (atomic_xchg(&pool->free_list[i], DC_MEMORY_POOL_RELEASED) != 233 DC_MEMORY_POOL_ACQUIRED) { 234 // Double free from two racing threads, use krefs to sync 235 ASSERT(false); 236 return; 237 } 238 239 // CAS-atomic `in->next = head; head = in;` 240 union index_t old_head = { 241 .raw = atomic64_read_acquire(&pool->free_head), 242 }; 243 union index_t new_head = { 244 .raw = 0, 245 }; 246 247 do { 248 atomic_set_release(&pool->free_list[i], old_head.s.value); 249 new_head = (union index_t){ 250 .s.value = i, 251 .s.generation = old_head.s.generation + 1, 252 }; 253 } while (!atomic64_try_cmpxchg(&pool->free_head, &old_head.raw, 254 new_head.raw)); 255 } 256 257 __must_check bool dc_memory_pool_owns(const struct dc_memory_pool *pool, 258 const void *memory) 259 { 260 const int32_t i = dc_memory_pool_get_index(pool, memory); 261 262 if (i < 0) 263 return false; 264 265 if (atomic_read_acquire(&pool->free_list[i]) != DC_MEMORY_POOL_ACQUIRED) 266 return false; 267 268 return true; 269 } 270 271 __must_check size_t dc_memory_pool_size(const struct dc_memory_pool *pool) 272 { 273 ASSERT(pool); 274 return pool->size; 275 } 276 277 __must_check size_t dc_memory_pool_capacity(const struct dc_memory_pool *pool) 278 { 279 ASSERT(pool); 280 return pool->capacity; 281 } 282