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 #if RSEQ_BITS_PER_LONG == 64
56 # define DEFAULT_POISON_VALUE 0x5555555555555555ULL
58 # define DEFAULT_POISON_VALUE 0x55555555UL
61 struct free_list_node
;
63 struct free_list_node
{
64 struct free_list_node
*next
;
68 MEMPOOL_TYPE_GLOBAL
= 0, /* Default */
69 MEMPOOL_TYPE_PERCPU
= 1,
72 struct rseq_mempool_attr
{
74 void *(*mmap_func
)(void *priv
, size_t len
);
75 int (*munmap_func
)(void *priv
, void *ptr
, size_t len
);
79 int (*init_func
)(void *priv
, void *addr
, size_t len
, int cpu
);
84 enum mempool_type type
;
88 unsigned long max_nr_ranges
;
94 struct rseq_mempool_range
;
96 struct rseq_mempool_range
{
97 struct rseq_mempool_range
*next
; /* Linked list of ranges. */
98 struct rseq_mempool
*pool
; /* Backward reference to container pool. */
102 /* Track alloc/free. */
103 unsigned long *alloc_bitmap
;
106 struct rseq_mempool
{
107 /* Head of ranges linked-list. */
108 struct rseq_mempool_range
*range_list
;
109 unsigned long nr_ranges
;
115 * The free list chains freed items on the CPU 0 address range.
116 * We should rethink this decision if false sharing between
117 * malloc/free from other CPUs and data accesses from CPU 0
118 * becomes an issue. This is a NULL-terminated singly-linked
121 struct free_list_node
*free_list_head
;
123 /* This lock protects allocation/free within the pool. */
124 pthread_mutex_t lock
;
126 struct rseq_mempool_attr attr
;
131 * Pool set entries are indexed by item_len rounded to the next power of
132 * 2. A pool set can contain NULL pool entries, in which case the next
133 * large enough entry will be used for allocation.
135 struct rseq_mempool_set
{
136 /* This lock protects add vs malloc/zmalloc within the pool set. */
137 pthread_mutex_t lock
;
138 struct rseq_mempool
*entries
[POOL_SET_NR_ENTRIES
];
142 const char *get_pool_name(const struct rseq_mempool
*pool
)
144 return pool
->name
? : "<anonymous>";
148 void *__rseq_pool_range_percpu_ptr(const struct rseq_mempool_range
*range
, int cpu
,
149 uintptr_t item_offset
, size_t stride
)
151 return range
->base
+ (stride
* cpu
) + item_offset
;
155 void rseq_percpu_zero_item(struct rseq_mempool
*pool
,
156 struct rseq_mempool_range
*range
, uintptr_t item_offset
)
160 for (i
= 0; i
< pool
->attr
.max_nr_cpus
; i
++) {
161 char *p
= __rseq_pool_range_percpu_ptr(range
, i
,
162 item_offset
, pool
->attr
.stride
);
163 memset(p
, 0, pool
->item_len
);
168 void rseq_percpu_poison_item(struct rseq_mempool
*pool
,
169 struct rseq_mempool_range
*range
, uintptr_t item_offset
)
171 uintptr_t poison
= pool
->attr
.poison
;
174 for (i
= 0; i
< pool
->attr
.max_nr_cpus
; i
++) {
175 char *p
= __rseq_pool_range_percpu_ptr(range
, i
,
176 item_offset
, pool
->attr
.stride
);
179 for (offset
= 0; offset
< pool
->item_len
; offset
+= sizeof(uintptr_t))
180 *((uintptr_t *) (p
+ offset
)) = poison
;
184 /* Always inline for __builtin_return_address(0). */
185 static inline __attribute__((always_inline
))
186 void rseq_percpu_check_poison_item(const struct rseq_mempool
*pool
,
187 const struct rseq_mempool_range
*range
, uintptr_t item_offset
)
189 uintptr_t poison
= pool
->attr
.poison
;
192 if (!pool
->attr
.robust_set
)
194 for (i
= 0; i
< pool
->attr
.max_nr_cpus
; i
++) {
195 char *p
= __rseq_pool_range_percpu_ptr(range
, i
,
196 item_offset
, pool
->attr
.stride
);
199 for (offset
= 0; offset
< pool
->item_len
; offset
+= sizeof(uintptr_t)) {
202 /* Skip poison check for free-list pointer. */
203 if (i
== 0 && offset
== 0)
205 v
= *((uintptr_t *) (p
+ offset
));
207 fprintf(stderr
, "%s: Poison corruption detected (0x%lx) for pool: \"%s\" (%p), item offset: %zu, caller: %p.\n",
208 __func__
, (unsigned long) v
, get_pool_name(pool
), pool
, item_offset
, (void *) __builtin_return_address(0));
216 int rseq_mempool_range_init_numa(void *addr
, size_t len
, int cpu
, int numa_flags
)
218 unsigned long nr_pages
, page_len
;
219 int status
[MOVE_PAGES_BATCH_SIZE
];
220 int nodes
[MOVE_PAGES_BATCH_SIZE
];
221 void *pages
[MOVE_PAGES_BATCH_SIZE
];
228 page_len
= rseq_get_page_len();
229 nr_pages
= len
>> rseq_get_count_order_ulong(page_len
);
231 nodes
[0] = numa_node_of_cpu(cpu
);
235 for (size_t k
= 1; k
< RSEQ_ARRAY_SIZE(nodes
); ++k
) {
239 for (unsigned long page
= 0; page
< nr_pages
;) {
241 size_t max_k
= RSEQ_ARRAY_SIZE(pages
);
242 size_t left
= nr_pages
- page
;
248 for (size_t k
= 0; k
< max_k
; ++k
, ++page
) {
249 pages
[k
] = addr
+ (page
* page_len
);
253 ret
= move_pages(0, max_k
, pages
, nodes
, status
, numa_flags
);
259 fprintf(stderr
, "%lu pages were not migrated\n", ret
);
260 for (size_t k
= 0; k
< max_k
; ++k
) {
263 "Error while moving page %p to numa node %d: %u\n",
264 pages
[k
], nodes
[k
], -status
[k
]);
271 int rseq_mempool_range_init_numa(void *addr
__attribute__((unused
)),
272 size_t len
__attribute__((unused
)),
273 int cpu
__attribute__((unused
)),
274 int numa_flags
__attribute__((unused
)))
282 void *default_mmap_func(void *priv
__attribute__((unused
)), size_t len
)
286 base
= mmap(NULL
, len
, PROT_READ
| PROT_WRITE
,
287 MAP_ANONYMOUS
| MAP_PRIVATE
, -1, 0);
288 if (base
== MAP_FAILED
)
294 int default_munmap_func(void *priv
__attribute__((unused
)), void *ptr
, size_t len
)
296 return munmap(ptr
, len
);
300 int create_alloc_bitmap(struct rseq_mempool
*pool
, struct rseq_mempool_range
*range
)
304 count
= ((pool
->attr
.stride
>> pool
->item_order
) + BIT_PER_ULONG
- 1) / BIT_PER_ULONG
;
307 * Not being able to create the validation bitmap is an error
308 * that needs to be reported.
310 range
->alloc_bitmap
= calloc(count
, sizeof(unsigned long));
311 if (!range
->alloc_bitmap
)
317 bool addr_in_pool(const struct rseq_mempool
*pool
, void *addr
)
319 struct rseq_mempool_range
*range
;
321 for (range
= pool
->range_list
; range
; range
= range
->next
) {
322 if (addr
>= range
->base
&& addr
< range
->base
+ range
->next_unused
)
328 /* Always inline for __builtin_return_address(0). */
329 static inline __attribute__((always_inline
))
330 void check_free_list(const struct rseq_mempool
*pool
)
332 size_t total_item
= 0, total_never_allocated
= 0, total_freed
= 0,
333 max_list_traversal
= 0, traversal_iteration
= 0;
334 struct rseq_mempool_range
*range
;
336 if (!pool
->attr
.robust_set
)
339 for (range
= pool
->range_list
; range
; range
= range
->next
) {
340 total_item
+= pool
->attr
.stride
>> pool
->item_order
;
341 total_never_allocated
+= (pool
->attr
.stride
- range
->next_unused
) >> pool
->item_order
;
343 max_list_traversal
= total_item
- total_never_allocated
;
345 for (struct free_list_node
*node
= pool
->free_list_head
, *prev
= NULL
;
350 void *node_addr
= node
;
352 if (traversal_iteration
>= max_list_traversal
) {
353 fprintf(stderr
, "%s: Corrupted free-list; Possibly infinite loop in pool \"%s\" (%p), caller %p.\n",
354 __func__
, get_pool_name(pool
), pool
, __builtin_return_address(0));
358 /* Node is out of range. */
359 if (!addr_in_pool(pool
, node_addr
)) {
361 fprintf(stderr
, "%s: Corrupted free-list node %p -> [out-of-range %p] in pool \"%s\" (%p), caller %p.\n",
362 __func__
, prev
, node
, get_pool_name(pool
), pool
, __builtin_return_address(0));
364 fprintf(stderr
, "%s: Corrupted free-list node [out-of-range %p] in pool \"%s\" (%p), caller %p.\n",
365 __func__
, node
, get_pool_name(pool
), pool
, __builtin_return_address(0));
369 traversal_iteration
++;
373 if (total_never_allocated
+ total_freed
!= total_item
) {
374 fprintf(stderr
, "%s: Corrupted free-list in pool \"%s\" (%p); total-item: %zu total-never-used: %zu total-freed: %zu, caller %p.\n",
375 __func__
, get_pool_name(pool
), pool
, total_item
, total_never_allocated
, total_freed
, __builtin_return_address(0));
380 /* Always inline for __builtin_return_address(0). */
381 static inline __attribute__((always_inline
))
382 void check_range_poison(const struct rseq_mempool
*pool
,
383 const struct rseq_mempool_range
*range
)
387 for (item_offset
= 0; item_offset
< range
->next_unused
;
388 item_offset
+= pool
->item_len
)
389 rseq_percpu_check_poison_item(pool
, range
, item_offset
);
392 /* Always inline for __builtin_return_address(0). */
393 static inline __attribute__((always_inline
))
394 void check_pool_poison(const struct rseq_mempool
*pool
)
396 struct rseq_mempool_range
*range
;
398 if (!pool
->attr
.robust_set
)
400 for (range
= pool
->range_list
; range
; range
= range
->next
)
401 check_range_poison(pool
, range
);
404 /* Always inline for __builtin_return_address(0). */
405 static inline __attribute__((always_inline
))
406 void destroy_alloc_bitmap(struct rseq_mempool
*pool
, struct rseq_mempool_range
*range
)
408 unsigned long *bitmap
= range
->alloc_bitmap
;
409 size_t count
, total_leaks
= 0;
414 count
= ((pool
->attr
.stride
>> pool
->item_order
) + BIT_PER_ULONG
- 1) / BIT_PER_ULONG
;
416 /* Assert that all items in the pool were freed. */
417 for (size_t k
= 0; k
< count
; ++k
)
418 total_leaks
+= rseq_hweight_ulong(bitmap
[k
]);
420 fprintf(stderr
, "%s: Pool \"%s\" (%p) has %zu leaked items on destroy, caller: %p.\n",
421 __func__
, get_pool_name(pool
), pool
, total_leaks
, (void *) __builtin_return_address(0));
428 /* Always inline for __builtin_return_address(0). */
429 static inline __attribute__((always_inline
))
430 int rseq_mempool_range_destroy(struct rseq_mempool
*pool
,
431 struct rseq_mempool_range
*range
)
433 destroy_alloc_bitmap(pool
, range
);
434 /* range is a header located one page before the aligned mapping. */
435 return pool
->attr
.munmap_func(pool
->attr
.mmap_priv
, range
->header
,
436 (pool
->attr
.stride
* pool
->attr
.max_nr_cpus
) + rseq_get_page_len());
440 * Allocate a memory mapping aligned on @alignment, with an optional
441 * @pre_header before the mapping.
444 void *aligned_mmap_anonymous(struct rseq_mempool
*pool
,
445 size_t page_size
, size_t len
, size_t alignment
,
446 void **pre_header
, size_t pre_header_len
)
448 size_t minimum_page_count
, page_count
, extra
, total_allocate
= 0;
452 if (len
< page_size
|| alignment
< page_size
||
453 !is_pow2(alignment
) || (len
& (alignment
- 1))) {
457 page_order
= rseq_get_count_order_ulong(page_size
);
458 if (page_order
< 0) {
462 if (pre_header_len
&& (pre_header_len
& (page_size
- 1))) {
467 minimum_page_count
= (pre_header_len
+ len
) >> page_order
;
468 page_count
= (pre_header_len
+ len
+ alignment
- page_size
) >> page_order
;
470 assert(page_count
>= minimum_page_count
);
472 ptr
= pool
->attr
.mmap_func(pool
->attr
.mmap_priv
, page_count
<< page_order
);
476 total_allocate
= page_count
<< page_order
;
478 if (!(((uintptr_t) ptr
+ pre_header_len
) & (alignment
- 1))) {
479 /* Pointer is already aligned. ptr points to pre_header. */
483 /* Unmap extra before. */
484 extra
= offset_align((uintptr_t) ptr
+ pre_header_len
, alignment
);
485 assert(!(extra
& (page_size
- 1)));
486 if (pool
->attr
.munmap_func(pool
->attr
.mmap_priv
, ptr
, extra
)) {
490 total_allocate
-= extra
;
491 ptr
+= extra
; /* ptr points to pre_header */
492 page_count
-= extra
>> page_order
;
494 assert(page_count
>= minimum_page_count
);
496 if (page_count
> minimum_page_count
) {
499 /* Unmap extra after. */
500 extra_ptr
= ptr
+ (minimum_page_count
<< page_order
);
501 extra
= (page_count
- minimum_page_count
) << page_order
;
502 if (pool
->attr
.munmap_func(pool
->attr
.mmap_priv
, extra_ptr
, extra
)) {
506 total_allocate
-= extra
;
509 assert(!(((uintptr_t)ptr
+ pre_header_len
) & (alignment
- 1)));
510 assert(total_allocate
== len
+ pre_header_len
);
516 ptr
+= pre_header_len
;
522 struct rseq_mempool_range
*rseq_mempool_range_create(struct rseq_mempool
*pool
)
524 struct rseq_mempool_range
*range
;
525 unsigned long page_size
;
529 if (pool
->attr
.max_nr_ranges
&&
530 pool
->nr_ranges
>= pool
->attr
.max_nr_ranges
) {
534 page_size
= rseq_get_page_len();
536 base
= aligned_mmap_anonymous(pool
, page_size
,
537 pool
->attr
.stride
* pool
->attr
.max_nr_cpus
,
542 range
= (struct rseq_mempool_range
*) (base
- RANGE_HEADER_OFFSET
);
545 range
->header
= header
;
546 if (pool
->attr
.robust_set
) {
547 if (create_alloc_bitmap(pool
, range
))
550 if (pool
->attr
.init_set
) {
551 switch (pool
->attr
.type
) {
552 case MEMPOOL_TYPE_GLOBAL
:
553 if (pool
->attr
.init_func(pool
->attr
.init_priv
,
554 base
, pool
->attr
.stride
, -1)) {
558 case MEMPOOL_TYPE_PERCPU
:
561 for (cpu
= 0; cpu
< pool
->attr
.max_nr_cpus
; cpu
++) {
562 if (pool
->attr
.init_func(pool
->attr
.init_priv
,
563 base
+ (pool
->attr
.stride
* cpu
),
564 pool
->attr
.stride
, cpu
)) {
578 (void) rseq_mempool_range_destroy(pool
, range
);
582 int rseq_mempool_destroy(struct rseq_mempool
*pool
)
584 struct rseq_mempool_range
*range
, *next_range
;
589 check_free_list(pool
);
590 check_pool_poison(pool
);
591 /* Iteration safe against removal. */
592 for (range
= pool
->range_list
; range
&& (next_range
= range
->next
, 1); range
= next_range
) {
593 if (rseq_mempool_range_destroy(pool
, range
))
595 /* Update list head to keep list coherent in case of partial failure. */
596 pool
->range_list
= next_range
;
598 pthread_mutex_destroy(&pool
->lock
);
600 memset(pool
, 0, sizeof(*pool
));
605 struct rseq_mempool
*rseq_mempool_create(const char *pool_name
,
606 size_t item_len
, const struct rseq_mempool_attr
*_attr
)
608 struct rseq_mempool
*pool
;
609 struct rseq_mempool_attr attr
= {};
612 /* Make sure each item is large enough to contain free list pointers. */
613 if (item_len
< sizeof(void *))
614 item_len
= sizeof(void *);
616 /* Align item_len on next power of two. */
617 order
= rseq_get_count_order_ulong(item_len
);
622 item_len
= 1UL << order
;
625 memcpy(&attr
, _attr
, sizeof(attr
));
626 if (!attr
.mmap_set
) {
627 attr
.mmap_func
= default_mmap_func
;
628 attr
.munmap_func
= default_munmap_func
;
629 attr
.mmap_priv
= NULL
;
633 case MEMPOOL_TYPE_PERCPU
:
634 if (attr
.max_nr_cpus
< 0) {
638 if (attr
.max_nr_cpus
== 0) {
640 attr
.max_nr_cpus
= rseq_get_max_nr_cpus();
641 if (attr
.max_nr_cpus
== 0) {
647 case MEMPOOL_TYPE_GLOBAL
:
648 /* Use a 1-cpu pool for global mempool type. */
649 attr
.max_nr_cpus
= 1;
653 attr
.stride
= RSEQ_MEMPOOL_STRIDE
; /* Use default */
654 if (attr
.robust_set
&& !attr
.poison_set
) {
655 attr
.poison_set
= true;
656 attr
.poison
= DEFAULT_POISON_VALUE
;
658 if (item_len
> attr
.stride
|| attr
.stride
< (size_t) rseq_get_page_len() ||
659 !is_pow2(attr
.stride
)) {
664 pool
= calloc(1, sizeof(struct rseq_mempool
));
668 memcpy(&pool
->attr
, &attr
, sizeof(attr
));
669 pthread_mutex_init(&pool
->lock
, NULL
);
670 pool
->item_len
= item_len
;
671 pool
->item_order
= order
;
673 pool
->range_list
= rseq_mempool_range_create(pool
);
674 if (!pool
->range_list
)
678 pool
->name
= strdup(pool_name
);
685 rseq_mempool_destroy(pool
);
690 /* Always inline for __builtin_return_address(0). */
691 static inline __attribute__((always_inline
))
692 void set_alloc_slot(struct rseq_mempool
*pool
, struct rseq_mempool_range
*range
, size_t item_offset
)
694 unsigned long *bitmap
= range
->alloc_bitmap
;
695 size_t item_index
= item_offset
>> pool
->item_order
;
702 k
= item_index
/ BIT_PER_ULONG
;
703 mask
= 1ULL << (item_index
% BIT_PER_ULONG
);
705 /* Print error if bit is already set. */
706 if (bitmap
[k
] & mask
) {
707 fprintf(stderr
, "%s: Allocator corruption detected for pool: \"%s\" (%p), item offset: %zu, caller: %p.\n",
708 __func__
, get_pool_name(pool
), pool
, item_offset
, (void *) __builtin_return_address(0));
715 void __rseq_percpu
*__rseq_percpu_malloc(struct rseq_mempool
*pool
, bool zeroed
)
717 struct rseq_mempool_range
*range
;
718 struct free_list_node
*node
;
719 uintptr_t item_offset
;
720 void __rseq_percpu
*addr
;
722 pthread_mutex_lock(&pool
->lock
);
723 /* Get first entry from free list. */
724 node
= pool
->free_list_head
;
726 uintptr_t ptr
= (uintptr_t) node
;
727 void *range_base
= (void *) (ptr
& (~(pool
->attr
.stride
- 1)));
729 range
= (struct rseq_mempool_range
*) (range_base
- RANGE_HEADER_OFFSET
);
730 /* Remove node from free list (update head). */
731 pool
->free_list_head
= node
->next
;
732 item_offset
= (uintptr_t) ((void *) node
- range_base
);
733 rseq_percpu_check_poison_item(pool
, range
, item_offset
);
734 addr
= (void __rseq_percpu
*) node
;
738 * If the most recent range (first in list) does not have any
739 * room left, create a new range and prepend it to the list
742 range
= pool
->range_list
;
743 if (range
->next_unused
+ pool
->item_len
> pool
->attr
.stride
) {
744 range
= rseq_mempool_range_create(pool
);
750 /* Add range to head of list. */
751 range
->next
= pool
->range_list
;
752 pool
->range_list
= range
;
754 /* First range in list has room left. */
755 item_offset
= range
->next_unused
;
756 addr
= (void __rseq_percpu
*) (range
->base
+ item_offset
);
757 range
->next_unused
+= pool
->item_len
;
760 set_alloc_slot(pool
, range
, item_offset
);
761 pthread_mutex_unlock(&pool
->lock
);
763 rseq_percpu_zero_item(pool
, range
, item_offset
);
767 void __rseq_percpu
*rseq_mempool_percpu_malloc(struct rseq_mempool
*pool
)
769 return __rseq_percpu_malloc(pool
, false);
772 void __rseq_percpu
*rseq_mempool_percpu_zmalloc(struct rseq_mempool
*pool
)
774 return __rseq_percpu_malloc(pool
, true);
777 /* Always inline for __builtin_return_address(0). */
778 static inline __attribute__((always_inline
))
779 void clear_alloc_slot(struct rseq_mempool
*pool
, struct rseq_mempool_range
*range
, size_t item_offset
)
781 unsigned long *bitmap
= range
->alloc_bitmap
;
782 size_t item_index
= item_offset
>> pool
->item_order
;
789 k
= item_index
/ BIT_PER_ULONG
;
790 mask
= 1ULL << (item_index
% BIT_PER_ULONG
);
792 /* Print error if bit is not set. */
793 if (!(bitmap
[k
] & mask
)) {
794 fprintf(stderr
, "%s: Double-free detected for pool: \"%s\" (%p), item offset: %zu, caller: %p.\n",
795 __func__
, get_pool_name(pool
), pool
, item_offset
,
796 (void *) __builtin_return_address(0));
802 void librseq_mempool_percpu_free(void __rseq_percpu
*_ptr
, size_t stride
)
804 uintptr_t ptr
= (uintptr_t) _ptr
;
805 void *range_base
= (void *) (ptr
& (~(stride
- 1)));
806 struct rseq_mempool_range
*range
= (struct rseq_mempool_range
*) (range_base
- RANGE_HEADER_OFFSET
);
807 struct rseq_mempool
*pool
= range
->pool
;
808 uintptr_t item_offset
= ptr
& (stride
- 1);
809 struct free_list_node
*head
, *item
;
811 pthread_mutex_lock(&pool
->lock
);
812 clear_alloc_slot(pool
, range
, item_offset
);
813 /* Add ptr to head of free list */
814 head
= pool
->free_list_head
;
815 if (pool
->attr
.poison_set
)
816 rseq_percpu_poison_item(pool
, range
, item_offset
);
817 /* Free-list is in CPU 0 range. */
818 item
= (struct free_list_node
*) ptr
;
820 * Setting the next pointer will overwrite the first uintptr_t
824 pool
->free_list_head
= item
;
825 pthread_mutex_unlock(&pool
->lock
);
828 struct rseq_mempool_set
*rseq_mempool_set_create(void)
830 struct rseq_mempool_set
*pool_set
;
832 pool_set
= calloc(1, sizeof(struct rseq_mempool_set
));
835 pthread_mutex_init(&pool_set
->lock
, NULL
);
839 int rseq_mempool_set_destroy(struct rseq_mempool_set
*pool_set
)
843 for (order
= POOL_SET_MIN_ENTRY
; order
< POOL_SET_NR_ENTRIES
; order
++) {
844 struct rseq_mempool
*pool
= pool_set
->entries
[order
];
848 ret
= rseq_mempool_destroy(pool
);
851 pool_set
->entries
[order
] = NULL
;
853 pthread_mutex_destroy(&pool_set
->lock
);
858 /* Ownership of pool is handed over to pool set on success. */
859 int rseq_mempool_set_add_pool(struct rseq_mempool_set
*pool_set
, struct rseq_mempool
*pool
)
861 size_t item_order
= pool
->item_order
;
864 pthread_mutex_lock(&pool_set
->lock
);
865 if (pool_set
->entries
[item_order
]) {
870 pool_set
->entries
[pool
->item_order
] = pool
;
872 pthread_mutex_unlock(&pool_set
->lock
);
877 void __rseq_percpu
*__rseq_mempool_set_malloc(struct rseq_mempool_set
*pool_set
, size_t len
, bool zeroed
)
879 int order
, min_order
= POOL_SET_MIN_ENTRY
;
880 struct rseq_mempool
*pool
;
881 void __rseq_percpu
*addr
;
883 order
= rseq_get_count_order_ulong(len
);
884 if (order
> POOL_SET_MIN_ENTRY
)
887 pthread_mutex_lock(&pool_set
->lock
);
888 /* First smallest present pool where @len fits. */
889 for (order
= min_order
; order
< POOL_SET_NR_ENTRIES
; order
++) {
890 pool
= pool_set
->entries
[order
];
894 if (pool
->item_len
>= len
)
899 pthread_mutex_unlock(&pool_set
->lock
);
901 addr
= __rseq_percpu_malloc(pool
, zeroed
);
902 if (addr
== NULL
&& errno
== ENOMEM
) {
904 * If the allocation failed, try again with a
907 min_order
= order
+ 1;
918 void __rseq_percpu
*rseq_mempool_set_percpu_malloc(struct rseq_mempool_set
*pool_set
, size_t len
)
920 return __rseq_mempool_set_malloc(pool_set
, len
, false);
923 void __rseq_percpu
*rseq_mempool_set_percpu_zmalloc(struct rseq_mempool_set
*pool_set
, size_t len
)
925 return __rseq_mempool_set_malloc(pool_set
, len
, true);
928 struct rseq_mempool_attr
*rseq_mempool_attr_create(void)
930 return calloc(1, sizeof(struct rseq_mempool_attr
));
933 void rseq_mempool_attr_destroy(struct rseq_mempool_attr
*attr
)
938 int rseq_mempool_attr_set_mmap(struct rseq_mempool_attr
*attr
,
939 void *(*mmap_func
)(void *priv
, size_t len
),
940 int (*munmap_func
)(void *priv
, void *ptr
, size_t len
),
947 attr
->mmap_set
= true;
948 attr
->mmap_func
= mmap_func
;
949 attr
->munmap_func
= munmap_func
;
950 attr
->mmap_priv
= mmap_priv
;
954 int rseq_mempool_attr_set_init(struct rseq_mempool_attr
*attr
,
955 int (*init_func
)(void *priv
, void *addr
, size_t len
, int cpu
),
962 attr
->init_set
= true;
963 attr
->init_func
= init_func
;
964 attr
->init_priv
= init_priv
;
968 int rseq_mempool_attr_set_robust(struct rseq_mempool_attr
*attr
)
974 attr
->robust_set
= true;
978 int rseq_mempool_attr_set_percpu(struct rseq_mempool_attr
*attr
,
979 size_t stride
, int max_nr_cpus
)
985 attr
->type
= MEMPOOL_TYPE_PERCPU
;
986 attr
->stride
= stride
;
987 attr
->max_nr_cpus
= max_nr_cpus
;
991 int rseq_mempool_attr_set_global(struct rseq_mempool_attr
*attr
,
998 attr
->type
= MEMPOOL_TYPE_GLOBAL
;
999 attr
->stride
= stride
;
1000 attr
->max_nr_cpus
= 0;
1004 int rseq_mempool_attr_set_max_nr_ranges(struct rseq_mempool_attr
*attr
,
1005 unsigned long max_nr_ranges
)
1011 attr
->max_nr_ranges
= max_nr_ranges
;
1015 int rseq_mempool_attr_set_poison(struct rseq_mempool_attr
*attr
,
1022 attr
->poison_set
= true;
1023 attr
->poison
= poison
;
1027 int rseq_mempool_get_max_nr_cpus(struct rseq_mempool
*mempool
)
1029 if (!mempool
|| mempool
->attr
.type
!= MEMPOOL_TYPE_PERCPU
) {
1033 return mempool
->attr
.max_nr_cpus
;
This page took 0.092395 seconds and 4 git commands to generate.