1 // SPDX-License-Identifier: MIT
2 // SPDX-FileCopyrightText: 2024 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
4 #include <rseq/mempool.h>
11 #include <rseq/compiler.h>
22 #include "rseq-utils.h"
23 #include <rseq/rseq.h>
26 * rseq-mempool.c: rseq CPU-Local Storage (CLS) memory allocator.
28 * The rseq per-CPU memory allocator allows the application the request
29 * memory pools of CPU-Local memory each of containing objects of a
30 * given size (rounded to next power of 2), reserving a given virtual
31 * address size per CPU, for a given maximum number of CPUs.
33 * The per-CPU memory allocator is analogous to TLS (Thread-Local
34 * Storage) memory: TLS is Thread-Local Storage, whereas the per-CPU
35 * memory allocator provides CPU-Local Storage.
38 #define POOL_SET_NR_ENTRIES RSEQ_BITS_PER_LONG
41 * Smallest allocation should hold enough space for a free list pointer.
43 #if RSEQ_BITS_PER_LONG == 64
44 # define POOL_SET_MIN_ENTRY 3 /* Smallest item_len=8 */
46 # define POOL_SET_MIN_ENTRY 2 /* Smallest item_len=4 */
49 #define BIT_PER_ULONG (8 * sizeof(unsigned long))
51 #define MOVE_PAGES_BATCH_SIZE 4096
53 #define RANGE_HEADER_OFFSET sizeof(struct rseq_mempool_range)
55 struct free_list_node
;
57 struct free_list_node
{
58 struct free_list_node
*next
;
62 MEMPOOL_TYPE_GLOBAL
= 0, /* Default */
63 MEMPOOL_TYPE_PERCPU
= 1,
66 struct rseq_mempool_attr
{
68 void *(*mmap_func
)(void *priv
, size_t len
);
69 int (*munmap_func
)(void *priv
, void *ptr
, size_t len
);
73 int (*init_func
)(void *priv
, void *addr
, size_t len
, int cpu
);
78 enum mempool_type type
;
82 unsigned long max_nr_ranges
;
88 struct rseq_mempool_range
;
90 struct rseq_mempool_range
{
91 struct rseq_mempool_range
*next
; /* Linked list of ranges. */
92 struct rseq_mempool
*pool
; /* Backward reference to container pool. */
96 /* Track alloc/free. */
97 unsigned long *alloc_bitmap
;
100 struct rseq_mempool
{
101 /* Head of ranges linked-list. */
102 struct rseq_mempool_range
*range_list
;
103 unsigned long nr_ranges
;
109 * The free list chains freed items on the CPU 0 address range.
110 * We should rethink this decision if false sharing between
111 * malloc/free from other CPUs and data accesses from CPU 0
112 * becomes an issue. This is a NULL-terminated singly-linked
115 struct free_list_node
*free_list_head
;
117 /* This lock protects allocation/free within the pool. */
118 pthread_mutex_t lock
;
120 struct rseq_mempool_attr attr
;
125 * Pool set entries are indexed by item_len rounded to the next power of
126 * 2. A pool set can contain NULL pool entries, in which case the next
127 * large enough entry will be used for allocation.
129 struct rseq_mempool_set
{
130 /* This lock protects add vs malloc/zmalloc within the pool set. */
131 pthread_mutex_t lock
;
132 struct rseq_mempool
*entries
[POOL_SET_NR_ENTRIES
];
136 const char *get_pool_name(const struct rseq_mempool
*pool
)
138 return pool
->name
? : "<anonymous>";
142 void *__rseq_pool_range_percpu_ptr(const struct rseq_mempool_range
*range
, int cpu
,
143 uintptr_t item_offset
, size_t stride
)
145 return range
->base
+ (stride
* cpu
) + item_offset
;
149 void rseq_percpu_zero_item(struct rseq_mempool
*pool
,
150 struct rseq_mempool_range
*range
, uintptr_t item_offset
)
154 for (i
= 0; i
< pool
->attr
.max_nr_cpus
; i
++) {
155 char *p
= __rseq_pool_range_percpu_ptr(range
, i
,
156 item_offset
, pool
->attr
.stride
);
157 memset(p
, 0, pool
->item_len
);
162 void rseq_percpu_poison_item(struct rseq_mempool
*pool
,
163 struct rseq_mempool_range
*range
, uintptr_t item_offset
)
165 uintptr_t poison
= pool
->attr
.poison
;
168 for (i
= 0; i
< pool
->attr
.max_nr_cpus
; i
++) {
169 char *p
= __rseq_pool_range_percpu_ptr(range
, i
,
170 item_offset
, pool
->attr
.stride
);
173 for (offset
= 0; offset
< pool
->item_len
; offset
+= sizeof(uintptr_t))
174 *((uintptr_t *) (p
+ offset
)) = poison
;
178 /* Always inline for __builtin_return_address(0). */
179 static inline __attribute__((always_inline
))
180 void rseq_percpu_check_poison_item(const struct rseq_mempool
*pool
,
181 const struct rseq_mempool_range
*range
, uintptr_t item_offset
)
183 uintptr_t poison
= pool
->attr
.poison
;
186 if (!pool
->attr
.robust_set
|| !pool
->attr
.poison_set
)
188 for (i
= 0; i
< pool
->attr
.max_nr_cpus
; i
++) {
189 char *p
= __rseq_pool_range_percpu_ptr(range
, i
,
190 item_offset
, pool
->attr
.stride
);
193 for (offset
= 0; offset
< pool
->item_len
; offset
+= sizeof(uintptr_t)) {
196 /* Skip poison check for free-list pointer. */
197 if (i
== 0 && offset
== 0)
199 v
= *((uintptr_t *) (p
+ offset
));
201 fprintf(stderr
, "%s: Poison corruption detected (0x%lx) for pool: \"%s\" (%p), item offset: %zu, caller: %p.\n",
202 __func__
, (unsigned long) v
, get_pool_name(pool
), pool
, item_offset
, (void *) __builtin_return_address(0));
210 int rseq_mempool_range_init_numa(void *addr
, size_t len
, int cpu
, int numa_flags
)
212 unsigned long nr_pages
, page_len
;
213 int status
[MOVE_PAGES_BATCH_SIZE
];
214 int nodes
[MOVE_PAGES_BATCH_SIZE
];
215 void *pages
[MOVE_PAGES_BATCH_SIZE
];
222 page_len
= rseq_get_page_len();
223 nr_pages
= len
>> rseq_get_count_order_ulong(page_len
);
225 nodes
[0] = numa_node_of_cpu(cpu
);
229 for (size_t k
= 1; k
< RSEQ_ARRAY_SIZE(nodes
); ++k
) {
233 for (unsigned long page
= 0; page
< nr_pages
;) {
235 size_t max_k
= RSEQ_ARRAY_SIZE(pages
);
236 size_t left
= nr_pages
- page
;
242 for (size_t k
= 0; k
< max_k
; ++k
, ++page
) {
243 pages
[k
] = addr
+ (page
* page_len
);
247 ret
= move_pages(0, max_k
, pages
, nodes
, status
, numa_flags
);
253 fprintf(stderr
, "%lu pages were not migrated\n", ret
);
254 for (size_t k
= 0; k
< max_k
; ++k
) {
257 "Error while moving page %p to numa node %d: %u\n",
258 pages
[k
], nodes
[k
], -status
[k
]);
265 int rseq_mempool_range_init_numa(void *addr
__attribute__((unused
)),
266 size_t len
__attribute__((unused
)),
267 int cpu
__attribute__((unused
)),
268 int numa_flags
__attribute__((unused
)))
276 void *default_mmap_func(void *priv
__attribute__((unused
)), size_t len
)
280 base
= mmap(NULL
, len
, PROT_READ
| PROT_WRITE
,
281 MAP_ANONYMOUS
| MAP_PRIVATE
, -1, 0);
282 if (base
== MAP_FAILED
)
288 int default_munmap_func(void *priv
__attribute__((unused
)), void *ptr
, size_t len
)
290 return munmap(ptr
, len
);
294 int create_alloc_bitmap(struct rseq_mempool
*pool
, struct rseq_mempool_range
*range
)
298 count
= ((pool
->attr
.stride
>> pool
->item_order
) + BIT_PER_ULONG
- 1) / BIT_PER_ULONG
;
301 * Not being able to create the validation bitmap is an error
302 * that needs to be reported.
304 range
->alloc_bitmap
= calloc(count
, sizeof(unsigned long));
305 if (!range
->alloc_bitmap
)
311 bool addr_in_pool(const struct rseq_mempool
*pool
, void *addr
)
313 struct rseq_mempool_range
*range
;
315 for (range
= pool
->range_list
; range
; range
= range
->next
) {
316 if (addr
>= range
->base
&& addr
< range
->base
+ range
->next_unused
)
322 /* Always inline for __builtin_return_address(0). */
323 static inline __attribute__((always_inline
))
324 void check_free_list(const struct rseq_mempool
*pool
)
326 size_t total_item
= 0, total_never_allocated
= 0, total_freed
= 0,
327 max_list_traversal
= 0, traversal_iteration
= 0;
328 struct rseq_mempool_range
*range
;
330 if (!pool
->attr
.robust_set
)
333 for (range
= pool
->range_list
; range
; range
= range
->next
) {
334 total_item
+= pool
->attr
.stride
>> pool
->item_order
;
335 total_never_allocated
+= (pool
->attr
.stride
- range
->next_unused
) >> pool
->item_order
;
337 max_list_traversal
= total_item
- total_never_allocated
;
339 for (struct free_list_node
*node
= pool
->free_list_head
, *prev
= NULL
;
344 void *node_addr
= node
;
346 if (traversal_iteration
>= max_list_traversal
) {
347 fprintf(stderr
, "%s: Corrupted free-list; Possibly infinite loop in pool \"%s\" (%p), caller %p.\n",
348 __func__
, get_pool_name(pool
), pool
, __builtin_return_address(0));
352 /* Node is out of range. */
353 if (!addr_in_pool(pool
, node_addr
)) {
355 fprintf(stderr
, "%s: Corrupted free-list node %p -> [out-of-range %p] in pool \"%s\" (%p), caller %p.\n",
356 __func__
, prev
, node
, get_pool_name(pool
), pool
, __builtin_return_address(0));
358 fprintf(stderr
, "%s: Corrupted free-list node [out-of-range %p] in pool \"%s\" (%p), caller %p.\n",
359 __func__
, node
, get_pool_name(pool
), pool
, __builtin_return_address(0));
363 traversal_iteration
++;
367 if (total_never_allocated
+ total_freed
!= total_item
) {
368 fprintf(stderr
, "%s: Corrupted free-list in pool \"%s\" (%p); total-item: %zu total-never-used: %zu total-freed: %zu, caller %p.\n",
369 __func__
, get_pool_name(pool
), pool
, total_item
, total_never_allocated
, total_freed
, __builtin_return_address(0));
374 /* Always inline for __builtin_return_address(0). */
375 static inline __attribute__((always_inline
))
376 void check_range_poison(const struct rseq_mempool
*pool
,
377 const struct rseq_mempool_range
*range
)
381 for (item_offset
= 0; item_offset
< range
->next_unused
;
382 item_offset
+= pool
->item_len
)
383 rseq_percpu_check_poison_item(pool
, range
, item_offset
);
386 /* Always inline for __builtin_return_address(0). */
387 static inline __attribute__((always_inline
))
388 void check_pool_poison(const struct rseq_mempool
*pool
)
390 struct rseq_mempool_range
*range
;
392 if (!pool
->attr
.robust_set
|| !pool
->attr
.poison_set
)
394 for (range
= pool
->range_list
; range
; range
= range
->next
)
395 check_range_poison(pool
, range
);
398 /* Always inline for __builtin_return_address(0). */
399 static inline __attribute__((always_inline
))
400 void destroy_alloc_bitmap(struct rseq_mempool
*pool
, struct rseq_mempool_range
*range
)
402 unsigned long *bitmap
= range
->alloc_bitmap
;
403 size_t count
, total_leaks
= 0;
408 count
= ((pool
->attr
.stride
>> pool
->item_order
) + BIT_PER_ULONG
- 1) / BIT_PER_ULONG
;
410 /* Assert that all items in the pool were freed. */
411 for (size_t k
= 0; k
< count
; ++k
)
412 total_leaks
+= rseq_hweight_ulong(bitmap
[k
]);
414 fprintf(stderr
, "%s: Pool \"%s\" (%p) has %zu leaked items on destroy, caller: %p.\n",
415 __func__
, get_pool_name(pool
), pool
, total_leaks
, (void *) __builtin_return_address(0));
422 /* Always inline for __builtin_return_address(0). */
423 static inline __attribute__((always_inline
))
424 int rseq_mempool_range_destroy(struct rseq_mempool
*pool
,
425 struct rseq_mempool_range
*range
)
427 destroy_alloc_bitmap(pool
, range
);
428 /* range is a header located one page before the aligned mapping. */
429 return pool
->attr
.munmap_func(pool
->attr
.mmap_priv
, range
->header
,
430 (pool
->attr
.stride
* pool
->attr
.max_nr_cpus
) + rseq_get_page_len());
434 * Allocate a memory mapping aligned on @alignment, with an optional
435 * @pre_header before the mapping.
438 void *aligned_mmap_anonymous(struct rseq_mempool
*pool
,
439 size_t page_size
, size_t len
, size_t alignment
,
440 void **pre_header
, size_t pre_header_len
)
442 size_t minimum_page_count
, page_count
, extra
, total_allocate
= 0;
446 if (len
< page_size
|| alignment
< page_size
||
447 !is_pow2(alignment
) || (len
& (alignment
- 1))) {
451 page_order
= rseq_get_count_order_ulong(page_size
);
452 if (page_order
< 0) {
456 if (pre_header_len
&& (pre_header_len
& (page_size
- 1))) {
461 minimum_page_count
= (pre_header_len
+ len
) >> page_order
;
462 page_count
= (pre_header_len
+ len
+ alignment
- page_size
) >> page_order
;
464 assert(page_count
>= minimum_page_count
);
466 ptr
= pool
->attr
.mmap_func(pool
->attr
.mmap_priv
, page_count
<< page_order
);
470 total_allocate
= page_count
<< page_order
;
472 if (!(((uintptr_t) ptr
+ pre_header_len
) & (alignment
- 1))) {
473 /* Pointer is already aligned. ptr points to pre_header. */
477 /* Unmap extra before. */
478 extra
= offset_align((uintptr_t) ptr
+ pre_header_len
, alignment
);
479 assert(!(extra
& (page_size
- 1)));
480 if (pool
->attr
.munmap_func(pool
->attr
.mmap_priv
, ptr
, extra
)) {
484 total_allocate
-= extra
;
485 ptr
+= extra
; /* ptr points to pre_header */
486 page_count
-= extra
>> page_order
;
488 assert(page_count
>= minimum_page_count
);
490 if (page_count
> minimum_page_count
) {
493 /* Unmap extra after. */
494 extra_ptr
= ptr
+ (minimum_page_count
<< page_order
);
495 extra
= (page_count
- minimum_page_count
) << page_order
;
496 if (pool
->attr
.munmap_func(pool
->attr
.mmap_priv
, extra_ptr
, extra
)) {
500 total_allocate
-= extra
;
503 assert(!(((uintptr_t)ptr
+ pre_header_len
) & (alignment
- 1)));
504 assert(total_allocate
== len
+ pre_header_len
);
510 ptr
+= pre_header_len
;
516 struct rseq_mempool_range
*rseq_mempool_range_create(struct rseq_mempool
*pool
)
518 struct rseq_mempool_range
*range
;
519 unsigned long page_size
;
523 if (pool
->attr
.max_nr_ranges
&&
524 pool
->nr_ranges
>= pool
->attr
.max_nr_ranges
) {
528 page_size
= rseq_get_page_len();
530 base
= aligned_mmap_anonymous(pool
, page_size
,
531 pool
->attr
.stride
* pool
->attr
.max_nr_cpus
,
536 range
= (struct rseq_mempool_range
*) (base
- RANGE_HEADER_OFFSET
);
539 range
->header
= header
;
540 if (pool
->attr
.robust_set
) {
541 if (create_alloc_bitmap(pool
, range
))
544 if (pool
->attr
.init_set
) {
545 switch (pool
->attr
.type
) {
546 case MEMPOOL_TYPE_GLOBAL
:
547 if (pool
->attr
.init_func(pool
->attr
.init_priv
,
548 base
, pool
->attr
.stride
, -1)) {
552 case MEMPOOL_TYPE_PERCPU
:
555 for (cpu
= 0; cpu
< pool
->attr
.max_nr_cpus
; cpu
++) {
556 if (pool
->attr
.init_func(pool
->attr
.init_priv
,
557 base
+ (pool
->attr
.stride
* cpu
),
558 pool
->attr
.stride
, cpu
)) {
572 (void) rseq_mempool_range_destroy(pool
, range
);
576 int rseq_mempool_destroy(struct rseq_mempool
*pool
)
578 struct rseq_mempool_range
*range
, *next_range
;
583 check_free_list(pool
);
584 check_pool_poison(pool
);
585 /* Iteration safe against removal. */
586 for (range
= pool
->range_list
; range
&& (next_range
= range
->next
, 1); range
= next_range
) {
587 if (rseq_mempool_range_destroy(pool
, range
))
589 /* Update list head to keep list coherent in case of partial failure. */
590 pool
->range_list
= next_range
;
592 pthread_mutex_destroy(&pool
->lock
);
594 memset(pool
, 0, sizeof(*pool
));
599 struct rseq_mempool
*rseq_mempool_create(const char *pool_name
,
600 size_t item_len
, const struct rseq_mempool_attr
*_attr
)
602 struct rseq_mempool
*pool
;
603 struct rseq_mempool_attr attr
= {};
606 /* Make sure each item is large enough to contain free list pointers. */
607 if (item_len
< sizeof(void *))
608 item_len
= sizeof(void *);
610 /* Align item_len on next power of two. */
611 order
= rseq_get_count_order_ulong(item_len
);
616 item_len
= 1UL << order
;
619 memcpy(&attr
, _attr
, sizeof(attr
));
620 if (!attr
.mmap_set
) {
621 attr
.mmap_func
= default_mmap_func
;
622 attr
.munmap_func
= default_munmap_func
;
623 attr
.mmap_priv
= NULL
;
627 case MEMPOOL_TYPE_PERCPU
:
628 if (attr
.max_nr_cpus
< 0) {
632 if (attr
.max_nr_cpus
== 0) {
634 attr
.max_nr_cpus
= rseq_get_max_nr_cpus();
635 if (attr
.max_nr_cpus
== 0) {
641 case MEMPOOL_TYPE_GLOBAL
:
642 /* Use a 1-cpu pool for global mempool type. */
643 attr
.max_nr_cpus
= 1;
647 attr
.stride
= RSEQ_MEMPOOL_STRIDE
; /* Use default */
648 if (item_len
> attr
.stride
|| attr
.stride
< (size_t) rseq_get_page_len() ||
649 !is_pow2(attr
.stride
)) {
654 pool
= calloc(1, sizeof(struct rseq_mempool
));
658 memcpy(&pool
->attr
, &attr
, sizeof(attr
));
659 pthread_mutex_init(&pool
->lock
, NULL
);
660 pool
->item_len
= item_len
;
661 pool
->item_order
= order
;
663 pool
->range_list
= rseq_mempool_range_create(pool
);
664 if (!pool
->range_list
)
668 pool
->name
= strdup(pool_name
);
675 rseq_mempool_destroy(pool
);
680 /* Always inline for __builtin_return_address(0). */
681 static inline __attribute__((always_inline
))
682 void set_alloc_slot(struct rseq_mempool
*pool
, struct rseq_mempool_range
*range
, size_t item_offset
)
684 unsigned long *bitmap
= range
->alloc_bitmap
;
685 size_t item_index
= item_offset
>> pool
->item_order
;
692 k
= item_index
/ BIT_PER_ULONG
;
693 mask
= 1ULL << (item_index
% BIT_PER_ULONG
);
695 /* Print error if bit is already set. */
696 if (bitmap
[k
] & mask
) {
697 fprintf(stderr
, "%s: Allocator corruption detected for pool: \"%s\" (%p), item offset: %zu, caller: %p.\n",
698 __func__
, get_pool_name(pool
), pool
, item_offset
, (void *) __builtin_return_address(0));
705 void __rseq_percpu
*__rseq_percpu_malloc(struct rseq_mempool
*pool
, bool zeroed
)
707 struct rseq_mempool_range
*range
;
708 struct free_list_node
*node
;
709 uintptr_t item_offset
;
710 void __rseq_percpu
*addr
;
712 pthread_mutex_lock(&pool
->lock
);
713 /* Get first entry from free list. */
714 node
= pool
->free_list_head
;
716 uintptr_t ptr
= (uintptr_t) node
;
717 void *range_base
= (void *) (ptr
& (~(pool
->attr
.stride
- 1)));
719 range
= (struct rseq_mempool_range
*) (range_base
- RANGE_HEADER_OFFSET
);
720 /* Remove node from free list (update head). */
721 pool
->free_list_head
= node
->next
;
722 item_offset
= (uintptr_t) ((void *) node
- range_base
);
723 rseq_percpu_check_poison_item(pool
, range
, item_offset
);
724 addr
= (void __rseq_percpu
*) node
;
728 * If the most recent range (first in list) does not have any
729 * room left, create a new range and prepend it to the list
732 range
= pool
->range_list
;
733 if (range
->next_unused
+ pool
->item_len
> pool
->attr
.stride
) {
734 range
= rseq_mempool_range_create(pool
);
740 /* Add range to head of list. */
741 range
->next
= pool
->range_list
;
742 pool
->range_list
= range
;
744 /* First range in list has room left. */
745 item_offset
= range
->next_unused
;
746 addr
= (void __rseq_percpu
*) (range
->base
+ item_offset
);
747 range
->next_unused
+= pool
->item_len
;
750 set_alloc_slot(pool
, range
, item_offset
);
751 pthread_mutex_unlock(&pool
->lock
);
753 rseq_percpu_zero_item(pool
, range
, item_offset
);
757 void __rseq_percpu
*rseq_mempool_percpu_malloc(struct rseq_mempool
*pool
)
759 return __rseq_percpu_malloc(pool
, false);
762 void __rseq_percpu
*rseq_mempool_percpu_zmalloc(struct rseq_mempool
*pool
)
764 return __rseq_percpu_malloc(pool
, true);
767 /* Always inline for __builtin_return_address(0). */
768 static inline __attribute__((always_inline
))
769 void clear_alloc_slot(struct rseq_mempool
*pool
, struct rseq_mempool_range
*range
, size_t item_offset
)
771 unsigned long *bitmap
= range
->alloc_bitmap
;
772 size_t item_index
= item_offset
>> pool
->item_order
;
779 k
= item_index
/ BIT_PER_ULONG
;
780 mask
= 1ULL << (item_index
% BIT_PER_ULONG
);
782 /* Print error if bit is not set. */
783 if (!(bitmap
[k
] & mask
)) {
784 fprintf(stderr
, "%s: Double-free detected for pool: \"%s\" (%p), item offset: %zu, caller: %p.\n",
785 __func__
, get_pool_name(pool
), pool
, item_offset
,
786 (void *) __builtin_return_address(0));
792 void librseq_mempool_percpu_free(void __rseq_percpu
*_ptr
, size_t stride
)
794 uintptr_t ptr
= (uintptr_t) _ptr
;
795 void *range_base
= (void *) (ptr
& (~(stride
- 1)));
796 struct rseq_mempool_range
*range
= (struct rseq_mempool_range
*) (range_base
- RANGE_HEADER_OFFSET
);
797 struct rseq_mempool
*pool
= range
->pool
;
798 uintptr_t item_offset
= ptr
& (stride
- 1);
799 struct free_list_node
*head
, *item
;
801 pthread_mutex_lock(&pool
->lock
);
802 clear_alloc_slot(pool
, range
, item_offset
);
803 /* Add ptr to head of free list */
804 head
= pool
->free_list_head
;
805 if (pool
->attr
.poison_set
)
806 rseq_percpu_poison_item(pool
, range
, item_offset
);
807 /* Free-list is in CPU 0 range. */
808 item
= (struct free_list_node
*) ptr
;
810 * Setting the next pointer will overwrite the first uintptr_t
814 pool
->free_list_head
= item
;
815 pthread_mutex_unlock(&pool
->lock
);
818 struct rseq_mempool_set
*rseq_mempool_set_create(void)
820 struct rseq_mempool_set
*pool_set
;
822 pool_set
= calloc(1, sizeof(struct rseq_mempool_set
));
825 pthread_mutex_init(&pool_set
->lock
, NULL
);
829 int rseq_mempool_set_destroy(struct rseq_mempool_set
*pool_set
)
833 for (order
= POOL_SET_MIN_ENTRY
; order
< POOL_SET_NR_ENTRIES
; order
++) {
834 struct rseq_mempool
*pool
= pool_set
->entries
[order
];
838 ret
= rseq_mempool_destroy(pool
);
841 pool_set
->entries
[order
] = NULL
;
843 pthread_mutex_destroy(&pool_set
->lock
);
848 /* Ownership of pool is handed over to pool set on success. */
849 int rseq_mempool_set_add_pool(struct rseq_mempool_set
*pool_set
, struct rseq_mempool
*pool
)
851 size_t item_order
= pool
->item_order
;
854 pthread_mutex_lock(&pool_set
->lock
);
855 if (pool_set
->entries
[item_order
]) {
860 pool_set
->entries
[pool
->item_order
] = pool
;
862 pthread_mutex_unlock(&pool_set
->lock
);
867 void __rseq_percpu
*__rseq_mempool_set_malloc(struct rseq_mempool_set
*pool_set
, size_t len
, bool zeroed
)
869 int order
, min_order
= POOL_SET_MIN_ENTRY
;
870 struct rseq_mempool
*pool
;
871 void __rseq_percpu
*addr
;
873 order
= rseq_get_count_order_ulong(len
);
874 if (order
> POOL_SET_MIN_ENTRY
)
877 pthread_mutex_lock(&pool_set
->lock
);
878 /* First smallest present pool where @len fits. */
879 for (order
= min_order
; order
< POOL_SET_NR_ENTRIES
; order
++) {
880 pool
= pool_set
->entries
[order
];
884 if (pool
->item_len
>= len
)
889 pthread_mutex_unlock(&pool_set
->lock
);
891 addr
= __rseq_percpu_malloc(pool
, zeroed
);
892 if (addr
== NULL
&& errno
== ENOMEM
) {
894 * If the allocation failed, try again with a
897 min_order
= order
+ 1;
908 void __rseq_percpu
*rseq_mempool_set_percpu_malloc(struct rseq_mempool_set
*pool_set
, size_t len
)
910 return __rseq_mempool_set_malloc(pool_set
, len
, false);
913 void __rseq_percpu
*rseq_mempool_set_percpu_zmalloc(struct rseq_mempool_set
*pool_set
, size_t len
)
915 return __rseq_mempool_set_malloc(pool_set
, len
, true);
918 struct rseq_mempool_attr
*rseq_mempool_attr_create(void)
920 return calloc(1, sizeof(struct rseq_mempool_attr
));
923 void rseq_mempool_attr_destroy(struct rseq_mempool_attr
*attr
)
928 int rseq_mempool_attr_set_mmap(struct rseq_mempool_attr
*attr
,
929 void *(*mmap_func
)(void *priv
, size_t len
),
930 int (*munmap_func
)(void *priv
, void *ptr
, size_t len
),
937 attr
->mmap_set
= true;
938 attr
->mmap_func
= mmap_func
;
939 attr
->munmap_func
= munmap_func
;
940 attr
->mmap_priv
= mmap_priv
;
944 int rseq_mempool_attr_set_init(struct rseq_mempool_attr
*attr
,
945 int (*init_func
)(void *priv
, void *addr
, size_t len
, int cpu
),
952 attr
->init_set
= true;
953 attr
->init_func
= init_func
;
954 attr
->init_priv
= init_priv
;
958 int rseq_mempool_attr_set_robust(struct rseq_mempool_attr
*attr
)
964 attr
->robust_set
= true;
968 int rseq_mempool_attr_set_percpu(struct rseq_mempool_attr
*attr
,
969 size_t stride
, int max_nr_cpus
)
975 attr
->type
= MEMPOOL_TYPE_PERCPU
;
976 attr
->stride
= stride
;
977 attr
->max_nr_cpus
= max_nr_cpus
;
981 int rseq_mempool_attr_set_global(struct rseq_mempool_attr
*attr
,
988 attr
->type
= MEMPOOL_TYPE_GLOBAL
;
989 attr
->stride
= stride
;
990 attr
->max_nr_cpus
= 0;
994 int rseq_mempool_attr_set_max_nr_ranges(struct rseq_mempool_attr
*attr
,
995 unsigned long max_nr_ranges
)
1001 attr
->max_nr_ranges
= max_nr_ranges
;
1005 int rseq_mempool_attr_set_poison(struct rseq_mempool_attr
*attr
,
1012 attr
->poison_set
= true;
1013 attr
->poison
= poison
;
1017 int rseq_mempool_get_max_nr_cpus(struct rseq_mempool
*mempool
)
1019 if (!mempool
|| mempool
->attr
.type
!= MEMPOOL_TYPE_PERCPU
) {
1023 return mempool
->attr
.max_nr_cpus
;
This page took 0.077796 seconds and 4 git commands to generate.