1 // SPDX-License-Identifier: MIT
2 // SPDX-FileCopyrightText: 2024 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
4 #include <rseq/percpu-alloc.h>
11 #include <rseq/compiler.h>
22 #include "rseq-alloc-utils.h"
25 * rseq-percpu-alloc.c: rseq CPU-Local Storage (CLS) memory allocator.
27 * The rseq per-CPU memory allocator allows the application the request
28 * memory pools of CPU-Local memory each of containing objects of a
29 * given size (rounded to next power of 2), reserving a given virtual
30 * address size per CPU, for a given maximum number of CPUs.
32 * The per-CPU memory allocator is analogous to TLS (Thread-Local
33 * Storage) memory: TLS is Thread-Local Storage, whereas the per-CPU
34 * memory allocator provides CPU-Local Storage.
38 * Use high bits of per-CPU addresses to index the pool.
39 * This leaves the low bits of available to the application for pointer
40 * tagging (based on next power of 2 alignment of the allocations).
42 #if RSEQ_BITS_PER_LONG == 64
43 # define POOL_INDEX_BITS 16
45 # define POOL_INDEX_BITS 8
47 #define MAX_NR_POOLS (1UL << POOL_INDEX_BITS)
48 #define POOL_INDEX_SHIFT (RSEQ_BITS_PER_LONG - POOL_INDEX_BITS)
49 #define MAX_POOL_LEN (1UL << POOL_INDEX_SHIFT)
50 #define MAX_POOL_LEN_MASK (MAX_POOL_LEN - 1)
52 #define POOL_SET_NR_ENTRIES POOL_INDEX_SHIFT
55 * Smallest allocation should hold enough space for a free list pointer.
57 #if RSEQ_BITS_PER_LONG == 64
58 # define POOL_SET_MIN_ENTRY 3 /* Smallest item_len=8 */
60 # define POOL_SET_MIN_ENTRY 2 /* Smallest item_len=4 */
64 * Skip pool index 0 to ensure allocated entries at index 0 do not match
69 #define RSEQ_POOL_FLAGS (RSEQ_POOL_ROBUST)
71 #define BIT_PER_ULONG (8 * sizeof(unsigned long))
73 struct free_list_node
;
75 struct free_list_node
{
76 struct free_list_node
*next
;
79 /* This lock protects pool create/destroy. */
80 static pthread_mutex_t pool_lock
= PTHREAD_MUTEX_INITIALIZER
;
82 struct rseq_mmap_attr
{
83 void *(*mmap_func
)(void *priv
, size_t len
);
84 int (*munmap_func
)(void *priv
, void *ptr
, size_t len
);
88 struct rseq_percpu_pool
{
97 * The free list chains freed items on the CPU 0 address range.
98 * We should rethink this decision if false sharing between
99 * malloc/free from other CPUs and data accesses from CPU 0
100 * becomes an issue. This is a NULL-terminated singly-linked
103 struct free_list_node
*free_list_head
;
105 /* This lock protects allocation/free within the pool. */
106 pthread_mutex_t lock
;
108 struct rseq_mmap_attr mmap_attr
;
110 /* Track alloc/free. */
111 unsigned long *alloc_bitmap
;
114 //TODO: the array of pools should grow dynamically on create.
115 static struct rseq_percpu_pool rseq_percpu_pool
[MAX_NR_POOLS
];
118 * Pool set entries are indexed by item_len rounded to the next power of
119 * 2. A pool set can contain NULL pool entries, in which case the next
120 * large enough entry will be used for allocation.
122 struct rseq_percpu_pool_set
{
123 /* This lock protects add vs malloc/zmalloc within the pool set. */
124 pthread_mutex_t lock
;
125 struct rseq_percpu_pool
*entries
[POOL_SET_NR_ENTRIES
];
129 void *__rseq_pool_percpu_ptr(struct rseq_percpu_pool
*pool
, int cpu
, uintptr_t item_offset
)
131 return pool
->base
+ (pool
->percpu_len
* cpu
) + item_offset
;
134 void *__rseq_percpu_ptr(void __rseq_percpu
*_ptr
, int cpu
)
136 uintptr_t ptr
= (uintptr_t) _ptr
;
137 uintptr_t item_offset
= ptr
& MAX_POOL_LEN_MASK
;
138 uintptr_t pool_index
= ptr
>> POOL_INDEX_SHIFT
;
139 struct rseq_percpu_pool
*pool
= &rseq_percpu_pool
[pool_index
];
142 return __rseq_pool_percpu_ptr(pool
, cpu
, item_offset
);
146 void rseq_percpu_zero_item(struct rseq_percpu_pool
*pool
, uintptr_t item_offset
)
150 for (i
= 0; i
< pool
->max_nr_cpus
; i
++) {
151 char *p
= __rseq_pool_percpu_ptr(pool
, i
, item_offset
);
152 memset(p
, 0, pool
->item_len
);
157 int rseq_percpu_pool_init_numa(struct rseq_percpu_pool
*pool
, int numa_flags
)
159 unsigned long nr_pages
, page
;
165 page_len
= rseq_get_page_len();
166 nr_pages
= pool
->percpu_len
>> rseq_get_count_order_ulong(page_len
);
167 for (cpu
= 0; cpu
< pool
->max_nr_cpus
; cpu
++) {
168 int node
= numa_node_of_cpu(cpu
);
170 /* TODO: batch move_pages() call with an array of pages. */
171 for (page
= 0; page
< nr_pages
; page
++) {
172 void *pageptr
= __rseq_pool_percpu_ptr(pool
, cpu
, page
* page_len
);
175 ret
= move_pages(0, 1, &pageptr
, &node
, &status
, numa_flags
);
183 void rseq_percpu_pool_init_numa(struct rseq_percpu_pool
*pool
__attribute__((unused
)),
184 int numa_flags
__attribute__((unused
)))
191 void *default_mmap_func(void *priv
__attribute__((unused
)), size_t len
)
195 base
= mmap(NULL
, len
, PROT_READ
| PROT_WRITE
,
196 MAP_ANONYMOUS
| MAP_PRIVATE
, -1, 0);
197 if (base
== MAP_FAILED
)
203 int default_munmap_func(void *priv
__attribute__((unused
)), void *ptr
, size_t len
)
205 return munmap(ptr
, len
);
209 int create_alloc_bitmap(struct rseq_percpu_pool
*pool
)
213 count
= ((pool
->percpu_len
>> pool
->item_order
) + BIT_PER_ULONG
- 1) / BIT_PER_ULONG
;
216 * Not being able to create the validation bitmap is an error
217 * that needs to be reported.
219 pool
->alloc_bitmap
= calloc(count
, sizeof(unsigned long));
220 if (!pool
->alloc_bitmap
)
225 /* Always inline for __builtin_return_address(0). */
226 static inline __attribute__((always_inline
))
227 void destroy_alloc_bitmap(struct rseq_percpu_pool
*pool
)
229 unsigned long *bitmap
= pool
->alloc_bitmap
;
230 size_t count
, total_leaks
= 0;
235 count
= ((pool
->percpu_len
>> pool
->item_order
) + BIT_PER_ULONG
- 1) / BIT_PER_ULONG
;
237 /* Assert that all items in the pool were freed. */
238 for (size_t k
= 0; k
< count
; ++k
)
239 total_leaks
+= rseq_hweight_ulong(bitmap
[k
]);
241 fprintf(stderr
, "%s: Pool has %zu leaked items on destroy, caller: %p.\n",
242 __func__
, total_leaks
, (void *) __builtin_return_address(0));
249 /* Always inline for __builtin_return_address(0). */
250 static inline __attribute__((always_inline
))
251 int __rseq_percpu_pool_destroy(struct rseq_percpu_pool
*pool
)
260 ret
= pool
->mmap_attr
.munmap_func(pool
->mmap_attr
.mmap_priv
, pool
->base
,
261 pool
->percpu_len
* pool
->max_nr_cpus
);
264 pthread_mutex_destroy(&pool
->lock
);
265 destroy_alloc_bitmap(pool
);
266 memset(pool
, 0, sizeof(*pool
));
271 int rseq_percpu_pool_destroy(struct rseq_percpu_pool
*pool
)
275 pthread_mutex_lock(&pool_lock
);
276 ret
= __rseq_percpu_pool_destroy(pool
);
277 pthread_mutex_unlock(&pool_lock
);
281 struct rseq_percpu_pool
*rseq_percpu_pool_create(size_t item_len
,
282 size_t percpu_len
, int max_nr_cpus
,
283 const struct rseq_mmap_attr
*mmap_attr
,
286 void *(*mmap_func
)(void *priv
, size_t len
);
287 int (*munmap_func
)(void *priv
, void *ptr
, size_t len
);
289 struct rseq_percpu_pool
*pool
;
294 if (flags
& ~RSEQ_POOL_FLAGS
) {
299 /* Make sure each item is large enough to contain free list pointers. */
300 if (item_len
< sizeof(void *))
301 item_len
= sizeof(void *);
303 /* Align item_len on next power of two. */
304 order
= rseq_get_count_order_ulong(item_len
);
309 item_len
= 1UL << order
;
311 /* Align percpu_len on page size. */
312 percpu_len
= rseq_align(percpu_len
, rseq_get_page_len());
314 if (max_nr_cpus
< 0 || item_len
> percpu_len
||
315 percpu_len
> (UINTPTR_MAX
>> POOL_INDEX_BITS
)) {
321 mmap_func
= mmap_attr
->mmap_func
;
322 munmap_func
= mmap_attr
->munmap_func
;
323 mmap_priv
= mmap_attr
->mmap_priv
;
325 mmap_func
= default_mmap_func
;
326 munmap_func
= default_munmap_func
;
329 pthread_mutex_lock(&pool_lock
);
330 /* Linear scan in array of pools to find empty spot. */
331 for (i
= FIRST_POOL
; i
< MAX_NR_POOLS
; i
++) {
332 pool
= &rseq_percpu_pool
[i
];
341 base
= mmap_func(mmap_priv
, percpu_len
* max_nr_cpus
);
344 pthread_mutex_init(&pool
->lock
, NULL
);
346 pool
->percpu_len
= percpu_len
;
347 pool
->max_nr_cpus
= max_nr_cpus
;
349 pool
->item_len
= item_len
;
350 pool
->item_order
= order
;
351 pool
->mmap_attr
.mmap_func
= mmap_func
;
352 pool
->mmap_attr
.munmap_func
= munmap_func
;
353 pool
->mmap_attr
.mmap_priv
= mmap_priv
;
355 if (RSEQ_POOL_ROBUST
& flags
) {
356 if (create_alloc_bitmap(pool
))
360 pthread_mutex_unlock(&pool_lock
);
364 __rseq_percpu_pool_destroy(pool
);
365 pthread_mutex_unlock(&pool_lock
);
370 /* Always inline for __builtin_return_address(0). */
371 static inline __attribute__((always_inline
))
372 void set_alloc_slot(struct rseq_percpu_pool
*pool
, size_t item_offset
)
374 unsigned long *bitmap
= pool
->alloc_bitmap
;
375 size_t item_index
= item_offset
>> pool
->item_order
;
382 k
= item_index
/ BIT_PER_ULONG
;
383 mask
= 1ULL << (item_index
% BIT_PER_ULONG
);
385 /* Print error if bit is already set. */
386 if (bitmap
[k
] & mask
) {
387 fprintf(stderr
, "%s: Allocator corruption detected for pool: %p, item offset: %zu, caller: %p.\n",
388 __func__
, pool
, item_offset
, (void *) __builtin_return_address(0));
395 void __rseq_percpu
*__rseq_percpu_malloc(struct rseq_percpu_pool
*pool
, bool zeroed
)
397 struct free_list_node
*node
;
398 uintptr_t item_offset
;
399 void __rseq_percpu
*addr
;
401 pthread_mutex_lock(&pool
->lock
);
402 /* Get first entry from free list. */
403 node
= pool
->free_list_head
;
405 /* Remove node from free list (update head). */
406 pool
->free_list_head
= node
->next
;
407 item_offset
= (uintptr_t) ((void *) node
- pool
->base
);
408 addr
= (void *) (((uintptr_t) pool
->index
<< POOL_INDEX_SHIFT
) | item_offset
);
411 if (pool
->next_unused
+ pool
->item_len
> pool
->percpu_len
) {
416 item_offset
= pool
->next_unused
;
417 addr
= (void *) (((uintptr_t) pool
->index
<< POOL_INDEX_SHIFT
) | item_offset
);
418 pool
->next_unused
+= pool
->item_len
;
419 set_alloc_slot(pool
, item_offset
);
421 pthread_mutex_unlock(&pool
->lock
);
423 rseq_percpu_zero_item(pool
, item_offset
);
427 void __rseq_percpu
*rseq_percpu_malloc(struct rseq_percpu_pool
*pool
)
429 return __rseq_percpu_malloc(pool
, false);
432 void __rseq_percpu
*rseq_percpu_zmalloc(struct rseq_percpu_pool
*pool
)
434 return __rseq_percpu_malloc(pool
, true);
437 /* Always inline for __builtin_return_address(0). */
438 static inline __attribute__((always_inline
))
439 void clear_alloc_slot(struct rseq_percpu_pool
*pool
, size_t item_offset
)
441 unsigned long *bitmap
= pool
->alloc_bitmap
;
442 size_t item_index
= item_offset
>> pool
->item_order
;
449 k
= item_index
/ BIT_PER_ULONG
;
450 mask
= 1ULL << (item_index
% BIT_PER_ULONG
);
452 /* Print error if bit is not set. */
453 if (!(bitmap
[k
] & mask
)) {
454 fprintf(stderr
, "%s: Double-free detected for pool: %p, item offset: %zu, caller: %p.\n",
455 __func__
, pool
, item_offset
, (void *) __builtin_return_address(0));
461 void rseq_percpu_free(void __rseq_percpu
*_ptr
)
463 uintptr_t ptr
= (uintptr_t) _ptr
;
464 uintptr_t item_offset
= ptr
& MAX_POOL_LEN_MASK
;
465 uintptr_t pool_index
= ptr
>> POOL_INDEX_SHIFT
;
466 struct rseq_percpu_pool
*pool
= &rseq_percpu_pool
[pool_index
];
467 struct free_list_node
*head
, *item
;
469 pthread_mutex_lock(&pool
->lock
);
470 clear_alloc_slot(pool
, item_offset
);
471 /* Add ptr to head of free list */
472 head
= pool
->free_list_head
;
473 /* Free-list is in CPU 0 range. */
474 item
= (struct free_list_node
*)__rseq_pool_percpu_ptr(pool
, 0, item_offset
);
476 pool
->free_list_head
= item
;
477 pthread_mutex_unlock(&pool
->lock
);
480 struct rseq_percpu_pool_set
*rseq_percpu_pool_set_create(void)
482 struct rseq_percpu_pool_set
*pool_set
;
484 pool_set
= calloc(1, sizeof(struct rseq_percpu_pool_set
));
487 pthread_mutex_init(&pool_set
->lock
, NULL
);
491 int rseq_percpu_pool_set_destroy(struct rseq_percpu_pool_set
*pool_set
)
495 for (order
= POOL_SET_MIN_ENTRY
; order
< POOL_SET_NR_ENTRIES
; order
++) {
496 struct rseq_percpu_pool
*pool
= pool_set
->entries
[order
];
500 ret
= rseq_percpu_pool_destroy(pool
);
503 pool_set
->entries
[order
] = NULL
;
505 pthread_mutex_destroy(&pool_set
->lock
);
510 /* Ownership of pool is handed over to pool set on success. */
511 int rseq_percpu_pool_set_add_pool(struct rseq_percpu_pool_set
*pool_set
, struct rseq_percpu_pool
*pool
)
513 size_t item_order
= pool
->item_order
;
516 pthread_mutex_lock(&pool_set
->lock
);
517 if (pool_set
->entries
[item_order
]) {
522 pool_set
->entries
[pool
->item_order
] = pool
;
524 pthread_mutex_unlock(&pool_set
->lock
);
529 void __rseq_percpu
*__rseq_percpu_pool_set_malloc(struct rseq_percpu_pool_set
*pool_set
, size_t len
, bool zeroed
)
531 int order
, min_order
= POOL_SET_MIN_ENTRY
;
532 struct rseq_percpu_pool
*pool
;
533 void __rseq_percpu
*addr
;
535 order
= rseq_get_count_order_ulong(len
);
536 if (order
> POOL_SET_MIN_ENTRY
)
539 pthread_mutex_lock(&pool_set
->lock
);
540 /* First smallest present pool where @len fits. */
541 for (order
= min_order
; order
< POOL_SET_NR_ENTRIES
; order
++) {
542 pool
= pool_set
->entries
[order
];
546 if (pool
->item_len
>= len
)
551 pthread_mutex_unlock(&pool_set
->lock
);
553 addr
= __rseq_percpu_malloc(pool
, zeroed
);
554 if (addr
== NULL
&& errno
== ENOMEM
) {
556 * If the allocation failed, try again with a
559 min_order
= order
+ 1;
570 void __rseq_percpu
*rseq_percpu_pool_set_malloc(struct rseq_percpu_pool_set
*pool_set
, size_t len
)
572 return __rseq_percpu_pool_set_malloc(pool_set
, len
, false);
575 void __rseq_percpu
*rseq_percpu_pool_set_zmalloc(struct rseq_percpu_pool_set
*pool_set
, size_t len
)
577 return __rseq_percpu_pool_set_malloc(pool_set
, len
, true);
580 struct rseq_mmap_attr
*rseq_mmap_attr_create(void *(*mmap_func
)(void *priv
, size_t len
),
581 int (*munmap_func
)(void *priv
, void *ptr
, size_t len
),
584 struct rseq_mmap_attr
*attr
= calloc(1, sizeof(struct rseq_mmap_attr
));
588 attr
->mmap_func
= mmap_func
;
589 attr
->munmap_func
= munmap_func
;
590 attr
->mmap_priv
= mmap_priv
;
594 void rseq_mmap_attr_destroy(struct rseq_mmap_attr
*attr
)