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 BIT_PER_ULONG (8 * sizeof(unsigned long))
71 #define MOVE_PAGES_BATCH_SIZE 4096
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_pool_attr
{
84 void *(*mmap_func
)(void *priv
, size_t len
);
85 int (*munmap_func
)(void *priv
, void *ptr
, size_t len
);
91 struct rseq_percpu_pool
{
100 * The free list chains freed items on the CPU 0 address range.
101 * We should rethink this decision if false sharing between
102 * malloc/free from other CPUs and data accesses from CPU 0
103 * becomes an issue. This is a NULL-terminated singly-linked
106 struct free_list_node
*free_list_head
;
108 /* This lock protects allocation/free within the pool. */
109 pthread_mutex_t lock
;
111 struct rseq_pool_attr attr
;
114 /* Track alloc/free. */
115 unsigned long *alloc_bitmap
;
118 //TODO: the array of pools should grow dynamically on create.
119 static struct rseq_percpu_pool rseq_percpu_pool
[MAX_NR_POOLS
];
122 * Pool set entries are indexed by item_len rounded to the next power of
123 * 2. A pool set can contain NULL pool entries, in which case the next
124 * large enough entry will be used for allocation.
126 struct rseq_percpu_pool_set
{
127 /* This lock protects add vs malloc/zmalloc within the pool set. */
128 pthread_mutex_t lock
;
129 struct rseq_percpu_pool
*entries
[POOL_SET_NR_ENTRIES
];
133 void *__rseq_pool_percpu_ptr(struct rseq_percpu_pool
*pool
, int cpu
, uintptr_t item_offset
)
135 return pool
->base
+ (pool
->percpu_len
* cpu
) + item_offset
;
138 void *__rseq_percpu_ptr(void __rseq_percpu
*_ptr
, int cpu
)
140 uintptr_t ptr
= (uintptr_t) _ptr
;
141 uintptr_t item_offset
= ptr
& MAX_POOL_LEN_MASK
;
142 uintptr_t pool_index
= ptr
>> POOL_INDEX_SHIFT
;
143 struct rseq_percpu_pool
*pool
= &rseq_percpu_pool
[pool_index
];
146 return __rseq_pool_percpu_ptr(pool
, cpu
, item_offset
);
150 void rseq_percpu_zero_item(struct rseq_percpu_pool
*pool
, uintptr_t item_offset
)
154 for (i
= 0; i
< pool
->max_nr_cpus
; i
++) {
155 char *p
= __rseq_pool_percpu_ptr(pool
, i
, item_offset
);
156 memset(p
, 0, pool
->item_len
);
161 int rseq_percpu_pool_init_numa(struct rseq_percpu_pool
*pool
, int numa_flags
)
163 unsigned long nr_pages
;
169 page_len
= rseq_get_page_len();
170 nr_pages
= pool
->percpu_len
>> rseq_get_count_order_ulong(page_len
);
171 for (cpu
= 0; cpu
< pool
->max_nr_cpus
; cpu
++) {
173 int status
[MOVE_PAGES_BATCH_SIZE
];
174 int nodes
[MOVE_PAGES_BATCH_SIZE
];
175 void *pages
[MOVE_PAGES_BATCH_SIZE
];
177 nodes
[0] = numa_node_of_cpu(cpu
);
178 for (size_t k
= 1; k
< RSEQ_ARRAY_SIZE(nodes
); ++k
) {
182 for (unsigned long page
= 0; page
< nr_pages
;) {
184 size_t max_k
= RSEQ_ARRAY_SIZE(pages
);
185 size_t left
= nr_pages
- page
;
191 for (size_t k
= 0; k
< max_k
; ++k
, ++page
) {
192 pages
[k
] = __rseq_pool_percpu_ptr(pool
, cpu
, page
* page_len
);
196 ret
= move_pages(0, max_k
, pages
, nodes
, status
, numa_flags
);
202 fprintf(stderr
, "%lu pages were not migrated\n", ret
);
203 for (size_t k
= 0; k
< max_k
; ++k
) {
206 "Error while moving page %p to numa node %d: %u\n",
207 pages
[k
], nodes
[k
], -status
[k
]);
215 void rseq_percpu_pool_init_numa(struct rseq_percpu_pool
*pool
__attribute__((unused
)),
216 int numa_flags
__attribute__((unused
)))
223 void *default_mmap_func(void *priv
__attribute__((unused
)), size_t len
)
227 base
= mmap(NULL
, len
, PROT_READ
| PROT_WRITE
,
228 MAP_ANONYMOUS
| MAP_PRIVATE
, -1, 0);
229 if (base
== MAP_FAILED
)
235 int default_munmap_func(void *priv
__attribute__((unused
)), void *ptr
, size_t len
)
237 return munmap(ptr
, len
);
241 int create_alloc_bitmap(struct rseq_percpu_pool
*pool
)
245 count
= ((pool
->percpu_len
>> pool
->item_order
) + BIT_PER_ULONG
- 1) / BIT_PER_ULONG
;
248 * Not being able to create the validation bitmap is an error
249 * that needs to be reported.
251 pool
->alloc_bitmap
= calloc(count
, sizeof(unsigned long));
252 if (!pool
->alloc_bitmap
)
258 const char *get_pool_name(const struct rseq_percpu_pool
*pool
)
260 return pool
->name
? : "<anonymous>";
263 /* Always inline for __builtin_return_address(0). */
264 static inline __attribute__((always_inline
))
265 void check_free_list(const struct rseq_percpu_pool
*pool
)
267 size_t total_item
= pool
->percpu_len
>> pool
->item_order
;
268 size_t total_never_allocated
= (pool
->percpu_len
- pool
->next_unused
) >> pool
->item_order
;
269 size_t total_freed
= 0;
270 size_t max_list_traversal
= total_item
- total_never_allocated
;
271 size_t traversal_iteration
= 0;
273 for (struct free_list_node
*node
= pool
->free_list_head
, *prev
= NULL
;
278 void *node_addr
= node
;
280 if (traversal_iteration
>= max_list_traversal
) {
281 fprintf(stderr
, "%s: Corrupted free-list; Possibly infinite loop in pool \"%s\" (%p), caller %p.\n",
282 __func__
, get_pool_name(pool
), pool
, __builtin_return_address(0));
286 /* Node is out of range. */
287 if ((node_addr
< pool
->base
) ||
288 (node_addr
>= pool
->base
+ pool
->next_unused
)) {
290 fprintf(stderr
, "%s: Corrupted free-list node %p -> [out-of-range %p] in pool \"%s\" (%p), caller %p.\n",
291 __func__
, prev
, node
, get_pool_name(pool
), pool
, __builtin_return_address(0));
293 fprintf(stderr
, "%s: Corrupted free-list node [out-of-range %p] in pool \"%s\" (%p), caller %p.\n",
294 __func__
, node
, get_pool_name(pool
), pool
, __builtin_return_address(0));
298 traversal_iteration
+= 1;
302 if (total_never_allocated
+ total_freed
!= total_item
) {
303 fprintf(stderr
, "%s: Corrupted free-list in pool \"%s\" (%p); total-item: %zu total-never-used: %zu total-freed: %zu, caller %p.\n",
304 __func__
, get_pool_name(pool
), pool
, total_item
, total_never_allocated
, total_freed
, __builtin_return_address(0));
310 /* Always inline for __builtin_return_address(0). */
311 static inline __attribute__((always_inline
))
312 void destroy_alloc_bitmap(struct rseq_percpu_pool
*pool
)
314 unsigned long *bitmap
= pool
->alloc_bitmap
;
315 size_t count
, total_leaks
= 0;
320 count
= ((pool
->percpu_len
>> pool
->item_order
) + BIT_PER_ULONG
- 1) / BIT_PER_ULONG
;
322 /* Assert that all items in the pool were freed. */
323 for (size_t k
= 0; k
< count
; ++k
)
324 total_leaks
+= rseq_hweight_ulong(bitmap
[k
]);
326 fprintf(stderr
, "%s: Pool \"%s\" (%p) has %zu leaked items on destroy, caller: %p.\n",
327 __func__
, get_pool_name(pool
), pool
, total_leaks
, (void *) __builtin_return_address(0));
331 check_free_list(pool
);
336 /* Always inline for __builtin_return_address(0). */
337 static inline __attribute__((always_inline
))
338 int __rseq_percpu_pool_destroy(struct rseq_percpu_pool
*pool
)
348 * This must be done before releasing pool->base for checking the
351 destroy_alloc_bitmap(pool
);
352 ret
= pool
->attr
.munmap_func(pool
->attr
.mmap_priv
, pool
->base
,
353 pool
->percpu_len
* pool
->max_nr_cpus
);
356 pthread_mutex_destroy(&pool
->lock
);
358 memset(pool
, 0, sizeof(*pool
));
363 int rseq_percpu_pool_destroy(struct rseq_percpu_pool
*pool
)
367 pthread_mutex_lock(&pool_lock
);
368 ret
= __rseq_percpu_pool_destroy(pool
);
369 pthread_mutex_unlock(&pool_lock
);
373 struct rseq_percpu_pool
*rseq_percpu_pool_create(const char *pool_name
,
374 size_t item_len
, size_t percpu_len
, int max_nr_cpus
,
375 const struct rseq_pool_attr
*_attr
)
377 struct rseq_percpu_pool
*pool
;
378 struct rseq_pool_attr attr
= {};
383 /* Make sure each item is large enough to contain free list pointers. */
384 if (item_len
< sizeof(void *))
385 item_len
= sizeof(void *);
387 /* Align item_len on next power of two. */
388 order
= rseq_get_count_order_ulong(item_len
);
393 item_len
= 1UL << order
;
395 /* Align percpu_len on page size. */
396 percpu_len
= rseq_align(percpu_len
, rseq_get_page_len());
398 if (max_nr_cpus
< 0 || item_len
> percpu_len
||
399 percpu_len
> (UINTPTR_MAX
>> POOL_INDEX_BITS
)) {
405 memcpy(&attr
, _attr
, sizeof(attr
));
406 if (!attr
.mmap_set
) {
407 attr
.mmap_func
= default_mmap_func
;
408 attr
.munmap_func
= default_munmap_func
;
409 attr
.mmap_priv
= NULL
;
412 pthread_mutex_lock(&pool_lock
);
413 /* Linear scan in array of pools to find empty spot. */
414 for (i
= FIRST_POOL
; i
< MAX_NR_POOLS
; i
++) {
415 pool
= &rseq_percpu_pool
[i
];
424 base
= attr
.mmap_func(attr
.mmap_priv
, percpu_len
* max_nr_cpus
);
427 pthread_mutex_init(&pool
->lock
, NULL
);
429 pool
->percpu_len
= percpu_len
;
430 pool
->max_nr_cpus
= max_nr_cpus
;
432 pool
->item_len
= item_len
;
433 pool
->item_order
= order
;
434 memcpy(&pool
->attr
, &attr
, sizeof(attr
));
437 pool
->name
= strdup(pool_name
);
442 if (attr
.robust_set
) {
443 if (create_alloc_bitmap(pool
))
447 pthread_mutex_unlock(&pool_lock
);
451 __rseq_percpu_pool_destroy(pool
);
452 pthread_mutex_unlock(&pool_lock
);
457 /* Always inline for __builtin_return_address(0). */
458 static inline __attribute__((always_inline
))
459 void set_alloc_slot(struct rseq_percpu_pool
*pool
, size_t item_offset
)
461 unsigned long *bitmap
= pool
->alloc_bitmap
;
462 size_t item_index
= item_offset
>> pool
->item_order
;
469 k
= item_index
/ BIT_PER_ULONG
;
470 mask
= 1ULL << (item_index
% BIT_PER_ULONG
);
472 /* Print error if bit is already set. */
473 if (bitmap
[k
] & mask
) {
474 fprintf(stderr
, "%s: Allocator corruption detected for pool: \"%s\" (%p), item offset: %zu, caller: %p.\n",
475 __func__
, get_pool_name(pool
), pool
, item_offset
, (void *) __builtin_return_address(0));
482 void __rseq_percpu
*__rseq_percpu_malloc(struct rseq_percpu_pool
*pool
, bool zeroed
)
484 struct free_list_node
*node
;
485 uintptr_t item_offset
;
486 void __rseq_percpu
*addr
;
488 pthread_mutex_lock(&pool
->lock
);
489 /* Get first entry from free list. */
490 node
= pool
->free_list_head
;
492 /* Remove node from free list (update head). */
493 pool
->free_list_head
= node
->next
;
494 item_offset
= (uintptr_t) ((void *) node
- pool
->base
);
495 addr
= (void *) (((uintptr_t) pool
->index
<< POOL_INDEX_SHIFT
) | item_offset
);
498 if (pool
->next_unused
+ pool
->item_len
> pool
->percpu_len
) {
503 item_offset
= pool
->next_unused
;
504 addr
= (void *) (((uintptr_t) pool
->index
<< POOL_INDEX_SHIFT
) | item_offset
);
505 pool
->next_unused
+= pool
->item_len
;
506 set_alloc_slot(pool
, item_offset
);
508 pthread_mutex_unlock(&pool
->lock
);
510 rseq_percpu_zero_item(pool
, item_offset
);
514 void __rseq_percpu
*rseq_percpu_malloc(struct rseq_percpu_pool
*pool
)
516 return __rseq_percpu_malloc(pool
, false);
519 void __rseq_percpu
*rseq_percpu_zmalloc(struct rseq_percpu_pool
*pool
)
521 return __rseq_percpu_malloc(pool
, true);
524 /* Always inline for __builtin_return_address(0). */
525 static inline __attribute__((always_inline
))
526 void clear_alloc_slot(struct rseq_percpu_pool
*pool
, size_t item_offset
)
528 unsigned long *bitmap
= pool
->alloc_bitmap
;
529 size_t item_index
= item_offset
>> pool
->item_order
;
536 k
= item_index
/ BIT_PER_ULONG
;
537 mask
= 1ULL << (item_index
% BIT_PER_ULONG
);
539 /* Print error if bit is not set. */
540 if (!(bitmap
[k
] & mask
)) {
541 fprintf(stderr
, "%s: Double-free detected for pool: \"%s\" (%p), item offset: %zu, caller: %p.\n",
542 __func__
, get_pool_name(pool
), pool
, item_offset
,
543 (void *) __builtin_return_address(0));
549 void rseq_percpu_free(void __rseq_percpu
*_ptr
)
551 uintptr_t ptr
= (uintptr_t) _ptr
;
552 uintptr_t item_offset
= ptr
& MAX_POOL_LEN_MASK
;
553 uintptr_t pool_index
= ptr
>> POOL_INDEX_SHIFT
;
554 struct rseq_percpu_pool
*pool
= &rseq_percpu_pool
[pool_index
];
555 struct free_list_node
*head
, *item
;
557 pthread_mutex_lock(&pool
->lock
);
558 clear_alloc_slot(pool
, item_offset
);
559 /* Add ptr to head of free list */
560 head
= pool
->free_list_head
;
561 /* Free-list is in CPU 0 range. */
562 item
= (struct free_list_node
*)__rseq_pool_percpu_ptr(pool
, 0, item_offset
);
564 pool
->free_list_head
= item
;
565 pthread_mutex_unlock(&pool
->lock
);
568 struct rseq_percpu_pool_set
*rseq_percpu_pool_set_create(void)
570 struct rseq_percpu_pool_set
*pool_set
;
572 pool_set
= calloc(1, sizeof(struct rseq_percpu_pool_set
));
575 pthread_mutex_init(&pool_set
->lock
, NULL
);
579 int rseq_percpu_pool_set_destroy(struct rseq_percpu_pool_set
*pool_set
)
583 for (order
= POOL_SET_MIN_ENTRY
; order
< POOL_SET_NR_ENTRIES
; order
++) {
584 struct rseq_percpu_pool
*pool
= pool_set
->entries
[order
];
588 ret
= rseq_percpu_pool_destroy(pool
);
591 pool_set
->entries
[order
] = NULL
;
593 pthread_mutex_destroy(&pool_set
->lock
);
598 /* Ownership of pool is handed over to pool set on success. */
599 int rseq_percpu_pool_set_add_pool(struct rseq_percpu_pool_set
*pool_set
, struct rseq_percpu_pool
*pool
)
601 size_t item_order
= pool
->item_order
;
604 pthread_mutex_lock(&pool_set
->lock
);
605 if (pool_set
->entries
[item_order
]) {
610 pool_set
->entries
[pool
->item_order
] = pool
;
612 pthread_mutex_unlock(&pool_set
->lock
);
617 void __rseq_percpu
*__rseq_percpu_pool_set_malloc(struct rseq_percpu_pool_set
*pool_set
, size_t len
, bool zeroed
)
619 int order
, min_order
= POOL_SET_MIN_ENTRY
;
620 struct rseq_percpu_pool
*pool
;
621 void __rseq_percpu
*addr
;
623 order
= rseq_get_count_order_ulong(len
);
624 if (order
> POOL_SET_MIN_ENTRY
)
627 pthread_mutex_lock(&pool_set
->lock
);
628 /* First smallest present pool where @len fits. */
629 for (order
= min_order
; order
< POOL_SET_NR_ENTRIES
; order
++) {
630 pool
= pool_set
->entries
[order
];
634 if (pool
->item_len
>= len
)
639 pthread_mutex_unlock(&pool_set
->lock
);
641 addr
= __rseq_percpu_malloc(pool
, zeroed
);
642 if (addr
== NULL
&& errno
== ENOMEM
) {
644 * If the allocation failed, try again with a
647 min_order
= order
+ 1;
658 void __rseq_percpu
*rseq_percpu_pool_set_malloc(struct rseq_percpu_pool_set
*pool_set
, size_t len
)
660 return __rseq_percpu_pool_set_malloc(pool_set
, len
, false);
663 void __rseq_percpu
*rseq_percpu_pool_set_zmalloc(struct rseq_percpu_pool_set
*pool_set
, size_t len
)
665 return __rseq_percpu_pool_set_malloc(pool_set
, len
, true);
668 struct rseq_pool_attr
*rseq_pool_attr_create(void)
670 return calloc(1, sizeof(struct rseq_pool_attr
));
673 void rseq_pool_attr_destroy(struct rseq_pool_attr
*attr
)
678 int rseq_pool_attr_set_mmap(struct rseq_pool_attr
*attr
,
679 void *(*mmap_func
)(void *priv
, size_t len
),
680 int (*munmap_func
)(void *priv
, void *ptr
, size_t len
),
687 attr
->mmap_set
= true;
688 attr
->mmap_func
= mmap_func
;
689 attr
->munmap_func
= munmap_func
;
690 attr
->mmap_priv
= mmap_priv
;
694 int rseq_pool_attr_set_robust(struct rseq_pool_attr
*attr
)
700 attr
->robust_set
= true;