mempool: Use MADV_DONTFORK on init values
[librseq.git] / src / rseq-mempool.c
CommitLineData
ef6695f1
MD
1// SPDX-License-Identifier: MIT
2// SPDX-FileCopyrightText: 2024 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
3
34337fec 4#include <rseq/mempool.h>
ef6695f1
MD
5#include <sys/mman.h>
6#include <assert.h>
7#include <string.h>
8#include <pthread.h>
9#include <unistd.h>
10#include <stdlib.h>
11#include <rseq/compiler.h>
12#include <errno.h>
13#include <stdint.h>
14#include <stdbool.h>
367e559c 15#include <stdio.h>
a5694a4d 16#include <fcntl.h>
367e559c
MD
17
18#ifdef HAVE_LIBNUMA
19# include <numa.h>
20# include <numaif.h>
21#endif
ef6695f1 22
34337fec 23#include "rseq-utils.h"
47c725dd 24#include <rseq/rseq.h>
19be9217 25
ef6695f1 26/*
b73b0c25 27 * rseq-mempool.c: rseq CPU-Local Storage (CLS) memory allocator.
ef6695f1 28 *
8ab16a24
MD
29 * The rseq per-CPU memory allocator allows the application the request
30 * memory pools of CPU-Local memory each of containing objects of a
8aa1462d
MD
31 * given size (rounded to next power of 2), reserving a given virtual
32 * address size per CPU, for a given maximum number of CPUs.
8ab16a24
MD
33 *
34 * The per-CPU memory allocator is analogous to TLS (Thread-Local
35 * Storage) memory: TLS is Thread-Local Storage, whereas the per-CPU
36 * memory allocator provides CPU-Local Storage.
ef6695f1
MD
37 */
38
3236da62 39#define POOL_SET_NR_ENTRIES RSEQ_BITS_PER_LONG
ef6695f1 40
72b100a1
MD
41/*
42 * Smallest allocation should hold enough space for a free list pointer.
43 */
ef6695f1
MD
44#if RSEQ_BITS_PER_LONG == 64
45# define POOL_SET_MIN_ENTRY 3 /* Smallest item_len=8 */
46#else
47# define POOL_SET_MIN_ENTRY 2 /* Smallest item_len=4 */
48#endif
49
0fdf7a4c
OD
50#define BIT_PER_ULONG (8 * sizeof(unsigned long))
51
57d8b586
OD
52#define MOVE_PAGES_BATCH_SIZE 4096
53
0ba2a93e 54#define RANGE_HEADER_OFFSET sizeof(struct rseq_mempool_range)
4aa3220c 55
3975084e 56#if RSEQ_BITS_PER_LONG == 64
dbccd436 57# define DEFAULT_PRIVATE_POISON_VALUE 0x5555555555555555ULL
3975084e 58#else
dbccd436 59# define DEFAULT_PRIVATE_POISON_VALUE 0x55555555UL
3975084e
MD
60#endif
61
ef6695f1
MD
62struct free_list_node;
63
64struct free_list_node {
65 struct free_list_node *next;
66};
67
cb475906 68enum mempool_type {
89b7e681
MD
69 MEMPOOL_TYPE_GLOBAL = 0, /* Default */
70 MEMPOOL_TYPE_PERCPU = 1,
cb475906
MD
71};
72
0ba2a93e 73struct rseq_mempool_attr {
135811f2 74 bool init_set;
6e329183 75 int (*init_func)(void *priv, void *addr, size_t len, int cpu);
135811f2
MD
76 void *init_priv;
77
d6acc8aa 78 bool robust_set;
cb475906
MD
79
80 enum mempool_type type;
81 size_t stride;
82 int max_nr_cpus;
e11a02d7
MD
83
84 unsigned long max_nr_ranges;
455e090e
MD
85
86 bool poison_set;
87 uintptr_t poison;
a5694a4d
MD
88
89 enum rseq_mempool_populate_policy populate_policy;
9bd07c29
MD
90};
91
0ba2a93e 92struct rseq_mempool_range;
b73b0c25 93
0ba2a93e 94struct rseq_mempool_range {
9d986353
MD
95 struct rseq_mempool_range *next; /* Linked list of ranges. */
96 struct rseq_mempool *pool; /* Backward reference to container pool. */
a5694a4d
MD
97
98 /*
99 * Memory layout of a mempool range:
100 * - Header page (contains struct rseq_mempool_range at the very end),
c0de0012
MD
101 * - Base of the per-cpu data, starting with CPU 0.
102 * Aliases with free-list for non-robust populate all pool.
a5694a4d
MD
103 * - CPU 1,
104 * ...
105 * - CPU max_nr_cpus - 1
4e8ae59d 106 * - init values (unpopulated for RSEQ_MEMPOOL_POPULATE_PRIVATE_ALL).
c0de0012
MD
107 * Aliases with free-list for non-robust populate none pool.
108 * - free list (for robust pool).
109 *
110 * The free list aliases the CPU 0 memory area for non-robust
111 * populate all pools. It aliases with init values for
112 * non-robust populate none pools. It is located immediately
113 * after the init values for robust pools.
a5694a4d 114 */
4aa3220c 115 void *header;
ef6695f1 116 void *base;
a5694a4d
MD
117 /*
118 * The init values contains malloc_init/zmalloc values.
4e8ae59d 119 * Pointer is NULL for RSEQ_MEMPOOL_POPULATE_PRIVATE_ALL.
a5694a4d
MD
120 */
121 void *init;
b73b0c25 122 size_t next_unused;
fa6a0fb3
MD
123
124 /* Pool range mmap/munmap */
125 void *mmap_addr;
126 size_t mmap_len;
127
b73b0c25
MD
128 /* Track alloc/free. */
129 unsigned long *alloc_bitmap;
130};
131
0ba2a93e 132struct rseq_mempool {
9d986353
MD
133 /* Head of ranges linked-list. */
134 struct rseq_mempool_range *range_list;
135 unsigned long nr_ranges;
b73b0c25 136
ef6695f1 137 size_t item_len;
ef6695f1 138 int item_order;
ef6695f1
MD
139
140 /*
8ab16a24 141 * The free list chains freed items on the CPU 0 address range.
ef6695f1 142 * We should rethink this decision if false sharing between
8ab16a24 143 * malloc/free from other CPUs and data accesses from CPU 0
ef6695f1
MD
144 * becomes an issue. This is a NULL-terminated singly-linked
145 * list.
146 */
147 struct free_list_node *free_list_head;
b73b0c25 148
ef6695f1
MD
149 /* This lock protects allocation/free within the pool. */
150 pthread_mutex_t lock;
9bd07c29 151
0ba2a93e 152 struct rseq_mempool_attr attr;
ca452fee 153 char *name;
ef6695f1
MD
154};
155
ef6695f1
MD
156/*
157 * Pool set entries are indexed by item_len rounded to the next power of
158 * 2. A pool set can contain NULL pool entries, in which case the next
159 * large enough entry will be used for allocation.
160 */
0ba2a93e 161struct rseq_mempool_set {
ef6695f1
MD
162 /* This lock protects add vs malloc/zmalloc within the pool set. */
163 pthread_mutex_t lock;
0ba2a93e 164 struct rseq_mempool *entries[POOL_SET_NR_ENTRIES];
ef6695f1
MD
165};
166
86617384
MD
167static
168const char *get_pool_name(const struct rseq_mempool *pool)
169{
170 return pool->name ? : "<anonymous>";
171}
172
367e559c 173static
6fbf1fb6 174void *__rseq_pool_range_percpu_ptr(const struct rseq_mempool_range *range, int cpu,
f2981623 175 uintptr_t item_offset, size_t stride)
367e559c 176{
15b63c9f 177 return range->base + (stride * cpu) + item_offset;
367e559c
MD
178}
179
a5694a4d
MD
180static
181void *__rseq_pool_range_init_ptr(const struct rseq_mempool_range *range,
182 uintptr_t item_offset)
183{
184 if (!range->init)
185 return NULL;
186 return range->init + item_offset;
187}
188
189static
190void __rseq_percpu *__rseq_free_list_to_percpu_ptr(const struct rseq_mempool *pool,
191 struct free_list_node *node)
192{
193 void __rseq_percpu *p = (void __rseq_percpu *) node;
194
c0de0012
MD
195 if (pool->attr.robust_set) {
196 /* Skip cpus. */
a5694a4d 197 p -= pool->attr.max_nr_cpus * pool->attr.stride;
c0de0012 198 /* Skip init values */
4e8ae59d 199 if (pool->attr.populate_policy != RSEQ_MEMPOOL_POPULATE_PRIVATE_ALL)
c0de0012
MD
200 p -= pool->attr.stride;
201
202 } else {
203 /* Populate none free list is in init values */
4e8ae59d 204 if (pool->attr.populate_policy != RSEQ_MEMPOOL_POPULATE_PRIVATE_ALL)
c0de0012
MD
205 p -= pool->attr.max_nr_cpus * pool->attr.stride;
206 }
a5694a4d
MD
207 return p;
208}
209
210static
211struct free_list_node *__rseq_percpu_to_free_list_ptr(const struct rseq_mempool *pool,
212 void __rseq_percpu *p)
213{
c0de0012
MD
214 if (pool->attr.robust_set) {
215 /* Skip cpus. */
a5694a4d 216 p += pool->attr.max_nr_cpus * pool->attr.stride;
c0de0012 217 /* Skip init values */
4e8ae59d 218 if (pool->attr.populate_policy != RSEQ_MEMPOOL_POPULATE_PRIVATE_ALL)
c0de0012
MD
219 p += pool->attr.stride;
220
221 } else {
222 /* Populate none free list is in init values */
4e8ae59d 223 if (pool->attr.populate_policy != RSEQ_MEMPOOL_POPULATE_PRIVATE_ALL)
c0de0012
MD
224 p += pool->attr.max_nr_cpus * pool->attr.stride;
225 }
a5694a4d
MD
226 return (struct free_list_node *) p;
227}
228
229static
14af0aa2 230intptr_t rseq_cmp_item(void *p, size_t item_len, intptr_t cmp_value, intptr_t *unexpected_value)
a5694a4d 231{
14af0aa2
MD
232 size_t offset;
233 intptr_t res = 0;
a5694a4d 234
14af0aa2
MD
235 for (offset = 0; offset < item_len; offset += sizeof(uintptr_t)) {
236 intptr_t v = *((intptr_t *) (p + offset));
237
238 if ((res = v - cmp_value) != 0) {
239 if (unexpected_value)
240 *unexpected_value = v;
a5694a4d 241 break;
14af0aa2
MD
242 }
243 }
a5694a4d
MD
244 return res;
245}
246
367e559c 247static
15b63c9f
MD
248void rseq_percpu_zero_item(struct rseq_mempool *pool,
249 struct rseq_mempool_range *range, uintptr_t item_offset)
367e559c 250{
a5694a4d 251 char *init_p = NULL;
367e559c
MD
252 int i;
253
a5694a4d
MD
254 init_p = __rseq_pool_range_init_ptr(range, item_offset);
255 if (init_p)
644298bb 256 bzero(init_p, pool->item_len);
cb475906 257 for (i = 0; i < pool->attr.max_nr_cpus; i++) {
15b63c9f 258 char *p = __rseq_pool_range_percpu_ptr(range, i,
cb475906 259 item_offset, pool->attr.stride);
a5694a4d 260
1b658191
MD
261 /*
262 * If item is already zeroed, either because the
263 * init range update has propagated or because the
264 * content is already zeroed (e.g. zero page), don't
265 * write to the page. This eliminates useless COW over
266 * the zero page just for overwriting it with zeroes.
267 *
268 * This means zmalloc() in populate all policy pool do
269 * not trigger COW for CPUs which are not actively
270 * writing to the pool. This is however not the case for
271 * malloc_init() in populate-all pools if it populates
272 * non-zero content.
273 */
14af0aa2 274 if (!rseq_cmp_item(p, pool->item_len, 0, NULL))
a5694a4d 275 continue;
644298bb 276 bzero(p, pool->item_len);
367e559c
MD
277 }
278}
279
6ff43d9a
MD
280static
281void rseq_percpu_init_item(struct rseq_mempool *pool,
282 struct rseq_mempool_range *range, uintptr_t item_offset,
283 void *init_ptr, size_t init_len)
284{
a5694a4d 285 char *init_p = NULL;
6ff43d9a
MD
286 int i;
287
a5694a4d
MD
288 init_p = __rseq_pool_range_init_ptr(range, item_offset);
289 if (init_p)
290 memcpy(init_p, init_ptr, init_len);
6ff43d9a
MD
291 for (i = 0; i < pool->attr.max_nr_cpus; i++) {
292 char *p = __rseq_pool_range_percpu_ptr(range, i,
293 item_offset, pool->attr.stride);
a5694a4d 294
1b658191
MD
295 /*
296 * If the update propagated through a shared mapping,
297 * or the item already has the correct content, skip
298 * writing it into the cpu item to eliminate useless
299 * COW of the page.
300 */
301 if (!memcmp(init_ptr, p, init_len))
a5694a4d 302 continue;
6ff43d9a
MD
303 memcpy(p, init_ptr, init_len);
304 }
305}
306
a5694a4d
MD
307static
308void rseq_poison_item(void *p, size_t item_len, uintptr_t poison)
309{
310 size_t offset;
311
312 for (offset = 0; offset < item_len; offset += sizeof(uintptr_t))
313 *((uintptr_t *) (p + offset)) = poison;
314}
315
455e090e
MD
316static
317void rseq_percpu_poison_item(struct rseq_mempool *pool,
318 struct rseq_mempool_range *range, uintptr_t item_offset)
319{
320 uintptr_t poison = pool->attr.poison;
a5694a4d 321 char *init_p = NULL;
455e090e
MD
322 int i;
323
a5694a4d
MD
324 init_p = __rseq_pool_range_init_ptr(range, item_offset);
325 if (init_p)
326 rseq_poison_item(init_p, pool->item_len, poison);
455e090e
MD
327 for (i = 0; i < pool->attr.max_nr_cpus; i++) {
328 char *p = __rseq_pool_range_percpu_ptr(range, i,
329 item_offset, pool->attr.stride);
455e090e 330
1b658191
MD
331 /*
332 * If the update propagated through a shared mapping,
333 * or the item already has the correct content, skip
334 * writing it into the cpu item to eliminate useless
335 * COW of the page.
336 *
337 * It is recommended to use zero as poison value for
338 * populate-all pools to eliminate COW due to writing
339 * poison to unused CPU memory.
340 */
14af0aa2 341 if (rseq_cmp_item(p, pool->item_len, poison, NULL) == 0)
a5694a4d
MD
342 continue;
343 rseq_poison_item(p, pool->item_len, poison);
344 }
345}
346
347/* Always inline for __builtin_return_address(0). */
348static inline __attribute__((always_inline))
349void rseq_check_poison_item(const struct rseq_mempool *pool, uintptr_t item_offset,
c0de0012 350 void *p, size_t item_len, uintptr_t poison)
a5694a4d 351{
1b658191 352 intptr_t unexpected_value;
a5694a4d 353
14af0aa2 354 if (rseq_cmp_item(p, item_len, poison, &unexpected_value) == 0)
1b658191 355 return;
a5694a4d 356
1b658191
MD
357 fprintf(stderr, "%s: Poison corruption detected (0x%lx) for pool: \"%s\" (%p), item offset: %zu, caller: %p.\n",
358 __func__, (unsigned long) unexpected_value, get_pool_name(pool), pool, item_offset, (void *) __builtin_return_address(0));
359 abort();
86617384
MD
360}
361
362/* Always inline for __builtin_return_address(0). */
363static inline __attribute__((always_inline))
6fbf1fb6
MD
364void rseq_percpu_check_poison_item(const struct rseq_mempool *pool,
365 const struct rseq_mempool_range *range, uintptr_t item_offset)
86617384
MD
366{
367 uintptr_t poison = pool->attr.poison;
a5694a4d 368 char *init_p;
86617384
MD
369 int i;
370
3975084e 371 if (!pool->attr.robust_set)
86617384 372 return;
a5694a4d
MD
373 init_p = __rseq_pool_range_init_ptr(range, item_offset);
374 if (init_p)
c0de0012 375 rseq_check_poison_item(pool, item_offset, init_p, pool->item_len, poison);
86617384
MD
376 for (i = 0; i < pool->attr.max_nr_cpus; i++) {
377 char *p = __rseq_pool_range_percpu_ptr(range, i,
378 item_offset, pool->attr.stride);
c0de0012 379 rseq_check_poison_item(pool, item_offset, p, pool->item_len, poison);
455e090e
MD
380 }
381}
382
15b63c9f 383#ifdef HAVE_LIBNUMA
c6fd3981 384int rseq_mempool_range_init_numa(void *addr, size_t len, int cpu, int numa_flags)
367e559c 385{
f2981623 386 unsigned long nr_pages, page_len;
c6fd3981
MD
387 int status[MOVE_PAGES_BATCH_SIZE];
388 int nodes[MOVE_PAGES_BATCH_SIZE];
389 void *pages[MOVE_PAGES_BATCH_SIZE];
f2981623 390 long ret;
367e559c 391
c6fd3981
MD
392 if (!numa_flags) {
393 errno = EINVAL;
394 return -1;
395 }
367e559c 396 page_len = rseq_get_page_len();
c6fd3981 397 nr_pages = len >> rseq_get_count_order_ulong(page_len);
57d8b586 398
c6fd3981
MD
399 nodes[0] = numa_node_of_cpu(cpu);
400 if (nodes[0] < 0)
401 return -1;
57d8b586 402
c6fd3981
MD
403 for (size_t k = 1; k < RSEQ_ARRAY_SIZE(nodes); ++k) {
404 nodes[k] = nodes[0];
405 }
57d8b586 406
c6fd3981 407 for (unsigned long page = 0; page < nr_pages;) {
57d8b586 408
c6fd3981
MD
409 size_t max_k = RSEQ_ARRAY_SIZE(pages);
410 size_t left = nr_pages - page;
57d8b586 411
c6fd3981
MD
412 if (left < max_k) {
413 max_k = left;
414 }
57d8b586 415
c6fd3981
MD
416 for (size_t k = 0; k < max_k; ++k, ++page) {
417 pages[k] = addr + (page * page_len);
418 status[k] = -EPERM;
367e559c 419 }
b73b0c25 420
c6fd3981
MD
421 ret = move_pages(0, max_k, pages, nodes, status, numa_flags);
422
423 if (ret < 0)
b73b0c25 424 return ret;
c6fd3981
MD
425
426 if (ret > 0) {
427 fprintf(stderr, "%lu pages were not migrated\n", ret);
428 for (size_t k = 0; k < max_k; ++k) {
429 if (status[k] < 0)
430 fprintf(stderr,
431 "Error while moving page %p to numa node %d: %u\n",
432 pages[k], nodes[k], -status[k]);
433 }
434 }
b73b0c25
MD
435 }
436 return 0;
437}
367e559c 438#else
c6fd3981
MD
439int rseq_mempool_range_init_numa(void *addr __attribute__((unused)),
440 size_t len __attribute__((unused)),
441 int cpu __attribute__((unused)),
367e559c
MD
442 int numa_flags __attribute__((unused)))
443{
c6fd3981
MD
444 errno = ENOSYS;
445 return -1;
367e559c
MD
446}
447#endif
448
0fdf7a4c 449static
0ba2a93e 450int create_alloc_bitmap(struct rseq_mempool *pool, struct rseq_mempool_range *range)
0fdf7a4c
OD
451{
452 size_t count;
453
cb475906 454 count = ((pool->attr.stride >> pool->item_order) + BIT_PER_ULONG - 1) / BIT_PER_ULONG;
0fdf7a4c
OD
455
456 /*
9649c7ee
MD
457 * Not being able to create the validation bitmap is an error
458 * that needs to be reported.
0fdf7a4c 459 */
b73b0c25
MD
460 range->alloc_bitmap = calloc(count, sizeof(unsigned long));
461 if (!range->alloc_bitmap)
9649c7ee
MD
462 return -1;
463 return 0;
0fdf7a4c
OD
464}
465
b73b0c25 466static
a5694a4d 467bool percpu_addr_in_pool(const struct rseq_mempool *pool, void __rseq_percpu *_addr)
b73b0c25 468{
0ba2a93e 469 struct rseq_mempool_range *range;
a5694a4d 470 void *addr = (void *) _addr;
b73b0c25 471
9d986353 472 for (range = pool->range_list; range; range = range->next) {
b73b0c25
MD
473 if (addr >= range->base && addr < range->base + range->next_unused)
474 return true;
475 }
476 return false;
477}
478
a9ec6111
OD
479/* Always inline for __builtin_return_address(0). */
480static inline __attribute__((always_inline))
0ba2a93e 481void check_free_list(const struct rseq_mempool *pool)
a9ec6111 482{
b73b0c25
MD
483 size_t total_item = 0, total_never_allocated = 0, total_freed = 0,
484 max_list_traversal = 0, traversal_iteration = 0;
0ba2a93e 485 struct rseq_mempool_range *range;
b73b0c25
MD
486
487 if (!pool->attr.robust_set)
488 return;
489
9d986353 490 for (range = pool->range_list; range; range = range->next) {
cb475906
MD
491 total_item += pool->attr.stride >> pool->item_order;
492 total_never_allocated += (pool->attr.stride - range->next_unused) >> pool->item_order;
b73b0c25
MD
493 }
494 max_list_traversal = total_item - total_never_allocated;
a9ec6111
OD
495
496 for (struct free_list_node *node = pool->free_list_head, *prev = NULL;
497 node;
498 prev = node,
499 node = node->next) {
500
a9ec6111 501 if (traversal_iteration >= max_list_traversal) {
ca452fee
MD
502 fprintf(stderr, "%s: Corrupted free-list; Possibly infinite loop in pool \"%s\" (%p), caller %p.\n",
503 __func__, get_pool_name(pool), pool, __builtin_return_address(0));
a9ec6111
OD
504 abort();
505 }
506
507 /* Node is out of range. */
a5694a4d 508 if (!percpu_addr_in_pool(pool, __rseq_free_list_to_percpu_ptr(pool, node))) {
a9ec6111 509 if (prev)
ca452fee
MD
510 fprintf(stderr, "%s: Corrupted free-list node %p -> [out-of-range %p] in pool \"%s\" (%p), caller %p.\n",
511 __func__, prev, node, get_pool_name(pool), pool, __builtin_return_address(0));
a9ec6111 512 else
ca452fee
MD
513 fprintf(stderr, "%s: Corrupted free-list node [out-of-range %p] in pool \"%s\" (%p), caller %p.\n",
514 __func__, node, get_pool_name(pool), pool, __builtin_return_address(0));
a9ec6111
OD
515 abort();
516 }
517
b73b0c25
MD
518 traversal_iteration++;
519 total_freed++;
a9ec6111
OD
520 }
521
522 if (total_never_allocated + total_freed != total_item) {
ca452fee
MD
523 fprintf(stderr, "%s: Corrupted free-list in pool \"%s\" (%p); total-item: %zu total-never-used: %zu total-freed: %zu, caller %p.\n",
524 __func__, get_pool_name(pool), pool, total_item, total_never_allocated, total_freed, __builtin_return_address(0));
a9ec6111
OD
525 abort();
526 }
a9ec6111
OD
527}
528
6fbf1fb6
MD
529/* Always inline for __builtin_return_address(0). */
530static inline __attribute__((always_inline))
531void check_range_poison(const struct rseq_mempool *pool,
532 const struct rseq_mempool_range *range)
533{
534 size_t item_offset;
535
536 for (item_offset = 0; item_offset < range->next_unused;
537 item_offset += pool->item_len)
538 rseq_percpu_check_poison_item(pool, range, item_offset);
539}
540
541/* Always inline for __builtin_return_address(0). */
542static inline __attribute__((always_inline))
543void check_pool_poison(const struct rseq_mempool *pool)
544{
545 struct rseq_mempool_range *range;
546
3975084e 547 if (!pool->attr.robust_set)
6fbf1fb6
MD
548 return;
549 for (range = pool->range_list; range; range = range->next)
550 check_range_poison(pool, range);
551}
552
e7cbbc10
MD
553/* Always inline for __builtin_return_address(0). */
554static inline __attribute__((always_inline))
0ba2a93e 555void destroy_alloc_bitmap(struct rseq_mempool *pool, struct rseq_mempool_range *range)
0fdf7a4c 556{
b73b0c25 557 unsigned long *bitmap = range->alloc_bitmap;
9649c7ee 558 size_t count, total_leaks = 0;
0fdf7a4c 559
9649c7ee 560 if (!bitmap)
0fdf7a4c 561 return;
0fdf7a4c 562
cb475906 563 count = ((pool->attr.stride >> pool->item_order) + BIT_PER_ULONG - 1) / BIT_PER_ULONG;
0fdf7a4c
OD
564
565 /* Assert that all items in the pool were freed. */
9649c7ee
MD
566 for (size_t k = 0; k < count; ++k)
567 total_leaks += rseq_hweight_ulong(bitmap[k]);
568 if (total_leaks) {
ca452fee
MD
569 fprintf(stderr, "%s: Pool \"%s\" (%p) has %zu leaked items on destroy, caller: %p.\n",
570 __func__, get_pool_name(pool), pool, total_leaks, (void *) __builtin_return_address(0));
9649c7ee 571 abort();
0fdf7a4c
OD
572 }
573
574 free(bitmap);
a5694a4d 575 range->alloc_bitmap = NULL;
0fdf7a4c
OD
576}
577
b73b0c25
MD
578/* Always inline for __builtin_return_address(0). */
579static inline __attribute__((always_inline))
0ba2a93e
MD
580int rseq_mempool_range_destroy(struct rseq_mempool *pool,
581 struct rseq_mempool_range *range)
b73b0c25
MD
582{
583 destroy_alloc_bitmap(pool, range);
a5694a4d 584
5c99f3d6 585 /* range is a header located one page before the aligned mapping. */
5cd72fc7 586 return munmap(range->mmap_addr, range->mmap_len);
5c99f3d6
MD
587}
588
589/*
590 * Allocate a memory mapping aligned on @alignment, with an optional
591 * @pre_header before the mapping.
592 */
593static
5cd72fc7 594void *aligned_mmap_anonymous(size_t page_size, size_t len, size_t alignment,
5c99f3d6
MD
595 void **pre_header, size_t pre_header_len)
596{
597 size_t minimum_page_count, page_count, extra, total_allocate = 0;
598 int page_order;
599 void *ptr;
600
601 if (len < page_size || alignment < page_size ||
b72b2d9e 602 !is_pow2(alignment) || (len & (alignment - 1))) {
5c99f3d6
MD
603 errno = EINVAL;
604 return NULL;
605 }
606 page_order = rseq_get_count_order_ulong(page_size);
607 if (page_order < 0) {
608 errno = EINVAL;
609 return NULL;
610 }
611 if (pre_header_len && (pre_header_len & (page_size - 1))) {
612 errno = EINVAL;
613 return NULL;
614 }
615
616 minimum_page_count = (pre_header_len + len) >> page_order;
617 page_count = (pre_header_len + len + alignment - page_size) >> page_order;
618
619 assert(page_count >= minimum_page_count);
620
5cd72fc7
MD
621 ptr = mmap(NULL, page_count << page_order, PROT_READ | PROT_WRITE,
622 MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
623 if (ptr == MAP_FAILED) {
624 ptr = NULL;
5c99f3d6 625 goto alloc_error;
5cd72fc7 626 }
5c99f3d6
MD
627
628 total_allocate = page_count << page_order;
629
630 if (!(((uintptr_t) ptr + pre_header_len) & (alignment - 1))) {
631 /* Pointer is already aligned. ptr points to pre_header. */
632 goto out;
633 }
634
635 /* Unmap extra before. */
636 extra = offset_align((uintptr_t) ptr + pre_header_len, alignment);
637 assert(!(extra & (page_size - 1)));
5cd72fc7 638 if (munmap(ptr, extra)) {
5c99f3d6
MD
639 perror("munmap");
640 abort();
641 }
642 total_allocate -= extra;
643 ptr += extra; /* ptr points to pre_header */
644 page_count -= extra >> page_order;
645out:
646 assert(page_count >= minimum_page_count);
647
648 if (page_count > minimum_page_count) {
649 void *extra_ptr;
650
651 /* Unmap extra after. */
652 extra_ptr = ptr + (minimum_page_count << page_order);
653 extra = (page_count - minimum_page_count) << page_order;
5cd72fc7 654 if (munmap(extra_ptr, extra)) {
5c99f3d6
MD
655 perror("munmap");
656 abort();
657 }
658 total_allocate -= extra;
659 }
660
661 assert(!(((uintptr_t)ptr + pre_header_len) & (alignment - 1)));
662 assert(total_allocate == len + pre_header_len);
663
664alloc_error:
665 if (ptr) {
666 if (pre_header)
667 *pre_header = ptr;
668 ptr += pre_header_len;
669 }
670 return ptr;
b73b0c25
MD
671}
672
a5694a4d 673static
cc0413ab 674int rseq_memfd_create_init(const char *poolname, size_t init_len)
a5694a4d 675{
a10c1c93 676 int fd;
cc0413ab
OD
677 char buf[249]; /* Limit is 249 bytes. */
678 const char *name;
a10c1c93 679
cc0413ab
OD
680 if (poolname) {
681 snprintf(buf, sizeof(buf), "%s:rseq-mempool", poolname);
682 name = buf;
683 } else {
684 name = "<anonymous>:rseq-mempool";
685 }
686
687 fd = memfd_create(name, MFD_CLOEXEC);
a10c1c93
MD
688 if (fd < 0) {
689 perror("memfd_create");
690 goto end;
a5694a4d 691 }
a10c1c93
MD
692 if (ftruncate(fd, (off_t) init_len)) {
693 fd = -1;
694 goto end;
695 }
696end:
697 return fd;
698}
699
700static
701void rseq_memfd_close(int fd)
702{
703 if (close(fd))
704 perror("close");
a5694a4d
MD
705}
706
b73b0c25 707static
0ba2a93e 708struct rseq_mempool_range *rseq_mempool_range_create(struct rseq_mempool *pool)
b73b0c25 709{
0ba2a93e 710 struct rseq_mempool_range *range;
5c99f3d6 711 unsigned long page_size;
4aa3220c 712 void *header;
b73b0c25 713 void *base;
a5694a4d 714 size_t range_len; /* Range len excludes header. */
b73b0c25 715
e11a02d7
MD
716 if (pool->attr.max_nr_ranges &&
717 pool->nr_ranges >= pool->attr.max_nr_ranges) {
9d986353
MD
718 errno = ENOMEM;
719 return NULL;
720 }
5c99f3d6 721 page_size = rseq_get_page_len();
b73b0c25 722
a5694a4d 723 range_len = pool->attr.stride * pool->attr.max_nr_cpus;
4e8ae59d 724 if (pool->attr.populate_policy != RSEQ_MEMPOOL_POPULATE_PRIVATE_ALL)
a5694a4d 725 range_len += pool->attr.stride; /* init values */
c0de0012
MD
726 if (pool->attr.robust_set)
727 range_len += pool->attr.stride; /* free list */
5cd72fc7
MD
728 base = aligned_mmap_anonymous(page_size, range_len,
729 pool->attr.stride, &header, page_size);
b73b0c25 730 if (!base)
5c99f3d6 731 return NULL;
0ba2a93e 732 range = (struct rseq_mempool_range *) (base - RANGE_HEADER_OFFSET);
5c99f3d6 733 range->pool = pool;
4aa3220c 734 range->header = header;
a5694a4d 735 range->base = base;
fa6a0fb3 736 range->mmap_addr = header;
a5694a4d
MD
737 range->mmap_len = page_size + range_len;
738
4e8ae59d 739 if (pool->attr.populate_policy != RSEQ_MEMPOOL_POPULATE_PRIVATE_ALL) {
a10c1c93
MD
740 int memfd;
741
a5694a4d
MD
742 range->init = base + (pool->attr.stride * pool->attr.max_nr_cpus);
743 /* Populate init values pages from memfd */
cc0413ab 744 memfd = rseq_memfd_create_init(pool->name, pool->attr.stride);
a10c1c93 745 if (memfd < 0)
a5694a4d
MD
746 goto error_alloc;
747 if (mmap(range->init, pool->attr.stride, PROT_READ | PROT_WRITE,
a10c1c93 748 MAP_SHARED | MAP_FIXED, memfd, 0) != (void *) range->init) {
a5694a4d
MD
749 goto error_alloc;
750 }
7f6667b5
MD
751 /*
752 * Make sure the init values shared mapping is not
753 * shared with the children processes across fork.
754 */
755 if (madvise(range->init, pool->attr.stride, MADV_DONTFORK))
756 goto error_alloc;
a5694a4d
MD
757 assert(pool->attr.type == MEMPOOL_TYPE_PERCPU);
758 /*
759 * Map per-cpu memory as private COW mappings of init values.
760 */
761 {
762 int cpu;
763
764 for (cpu = 0; cpu < pool->attr.max_nr_cpus; cpu++) {
765 void *p = base + (pool->attr.stride * cpu);
766 size_t len = pool->attr.stride;
767
768 if (mmap(p, len, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_FIXED,
a10c1c93 769 memfd, 0) != (void *) p) {
a5694a4d
MD
770 goto error_alloc;
771 }
772 }
773 }
a10c1c93 774 rseq_memfd_close(memfd);
a5694a4d
MD
775 }
776
b73b0c25
MD
777 if (pool->attr.robust_set) {
778 if (create_alloc_bitmap(pool, range))
779 goto error_alloc;
780 }
135811f2 781 if (pool->attr.init_set) {
374c2773
MD
782 switch (pool->attr.type) {
783 case MEMPOOL_TYPE_GLOBAL:
6e329183 784 if (pool->attr.init_func(pool->attr.init_priv,
374c2773 785 base, pool->attr.stride, -1)) {
6e329183
MD
786 goto error_alloc;
787 }
374c2773
MD
788 break;
789 case MEMPOOL_TYPE_PERCPU:
790 {
791 int cpu;
792 for (cpu = 0; cpu < pool->attr.max_nr_cpus; cpu++) {
793 if (pool->attr.init_func(pool->attr.init_priv,
794 base + (pool->attr.stride * cpu),
795 pool->attr.stride, cpu)) {
796 goto error_alloc;
797 }
798 }
799 break;
800 }
801 default:
802 abort();
135811f2
MD
803 }
804 }
9d986353 805 pool->nr_ranges++;
b73b0c25
MD
806 return range;
807
808error_alloc:
0ba2a93e 809 (void) rseq_mempool_range_destroy(pool, range);
b73b0c25
MD
810 return NULL;
811}
812
0ba2a93e 813int rseq_mempool_destroy(struct rseq_mempool *pool)
9649c7ee 814{
0ba2a93e 815 struct rseq_mempool_range *range, *next_range;
b73b0c25 816 int ret = 0;
9649c7ee 817
f510ddc5
MD
818 if (!pool)
819 return 0;
b73b0c25 820 check_free_list(pool);
6fbf1fb6 821 check_pool_poison(pool);
b73b0c25 822 /* Iteration safe against removal. */
9d986353 823 for (range = pool->range_list; range && (next_range = range->next, 1); range = next_range) {
0ba2a93e 824 if (rseq_mempool_range_destroy(pool, range))
b73b0c25
MD
825 goto end;
826 /* Update list head to keep list coherent in case of partial failure. */
9d986353 827 pool->range_list = next_range;
b73b0c25 828 }
9649c7ee 829 pthread_mutex_destroy(&pool->lock);
ca452fee 830 free(pool->name);
eb8db04d 831 free(pool);
9649c7ee 832end:
b73b0c25 833 return ret;
9649c7ee
MD
834}
835
0ba2a93e 836struct rseq_mempool *rseq_mempool_create(const char *pool_name,
cb475906 837 size_t item_len, const struct rseq_mempool_attr *_attr)
ef6695f1 838{
0ba2a93e
MD
839 struct rseq_mempool *pool;
840 struct rseq_mempool_attr attr = {};
ef6695f1 841 int order;
ef6695f1
MD
842
843 /* Make sure each item is large enough to contain free list pointers. */
844 if (item_len < sizeof(void *))
845 item_len = sizeof(void *);
846
847 /* Align item_len on next power of two. */
19be9217 848 order = rseq_get_count_order_ulong(item_len);
ef6695f1
MD
849 if (order < 0) {
850 errno = EINVAL;
851 return NULL;
852 }
853 item_len = 1UL << order;
854
a82006d0
MD
855 if (_attr)
856 memcpy(&attr, _attr, sizeof(attr));
a82006d0 857
cb475906
MD
858 switch (attr.type) {
859 case MEMPOOL_TYPE_PERCPU:
860 if (attr.max_nr_cpus < 0) {
861 errno = EINVAL;
862 return NULL;
863 }
864 if (attr.max_nr_cpus == 0) {
865 /* Auto-detect */
47c725dd 866 attr.max_nr_cpus = rseq_get_max_nr_cpus();
cb475906
MD
867 if (attr.max_nr_cpus == 0) {
868 errno = EINVAL;
869 return NULL;
870 }
871 }
872 break;
873 case MEMPOOL_TYPE_GLOBAL:
a5694a4d 874 /* Override populate policy for global type. */
4e8ae59d
MD
875 if (attr.populate_policy == RSEQ_MEMPOOL_POPULATE_PRIVATE_NONE)
876 attr.populate_policy = RSEQ_MEMPOOL_POPULATE_PRIVATE_ALL;
89b7e681
MD
877 /* Use a 1-cpu pool for global mempool type. */
878 attr.max_nr_cpus = 1;
cb475906
MD
879 break;
880 }
881 if (!attr.stride)
882 attr.stride = RSEQ_MEMPOOL_STRIDE; /* Use default */
3975084e
MD
883 if (attr.robust_set && !attr.poison_set) {
884 attr.poison_set = true;
dbccd436 885 attr.poison = DEFAULT_PRIVATE_POISON_VALUE;
3975084e 886 }
cb475906
MD
887 if (item_len > attr.stride || attr.stride < (size_t) rseq_get_page_len() ||
888 !is_pow2(attr.stride)) {
889 errno = EINVAL;
890 return NULL;
891 }
892
0ba2a93e 893 pool = calloc(1, sizeof(struct rseq_mempool));
bc510b60
MD
894 if (!pool)
895 return NULL;
ef6695f1 896
b73b0c25 897 memcpy(&pool->attr, &attr, sizeof(attr));
ef6695f1 898 pthread_mutex_init(&pool->lock, NULL);
ef6695f1
MD
899 pool->item_len = item_len;
900 pool->item_order = order;
b73b0c25 901
9d986353
MD
902 pool->range_list = rseq_mempool_range_create(pool);
903 if (!pool->range_list)
b73b0c25 904 goto error_alloc;
0fdf7a4c 905
ca452fee
MD
906 if (pool_name) {
907 pool->name = strdup(pool_name);
908 if (!pool->name)
909 goto error_alloc;
910 }
ef6695f1 911 return pool;
ef6695f1 912
9649c7ee 913error_alloc:
0ba2a93e 914 rseq_mempool_destroy(pool);
9649c7ee
MD
915 errno = ENOMEM;
916 return NULL;
ef6695f1
MD
917}
918
e7cbbc10
MD
919/* Always inline for __builtin_return_address(0). */
920static inline __attribute__((always_inline))
9d986353 921void set_alloc_slot(struct rseq_mempool *pool, struct rseq_mempool_range *range, size_t item_offset)
0fdf7a4c 922{
9d986353 923 unsigned long *bitmap = range->alloc_bitmap;
9649c7ee 924 size_t item_index = item_offset >> pool->item_order;
0fdf7a4c
OD
925 unsigned long mask;
926 size_t k;
927
9649c7ee 928 if (!bitmap)
0fdf7a4c 929 return;
0fdf7a4c 930
9649c7ee 931 k = item_index / BIT_PER_ULONG;
0fdf7a4c
OD
932 mask = 1ULL << (item_index % BIT_PER_ULONG);
933
9649c7ee
MD
934 /* Print error if bit is already set. */
935 if (bitmap[k] & mask) {
ca452fee
MD
936 fprintf(stderr, "%s: Allocator corruption detected for pool: \"%s\" (%p), item offset: %zu, caller: %p.\n",
937 __func__, get_pool_name(pool), pool, item_offset, (void *) __builtin_return_address(0));
9649c7ee
MD
938 abort();
939 }
0fdf7a4c
OD
940 bitmap[k] |= mask;
941}
942
ef6695f1 943static
6ff43d9a
MD
944void __rseq_percpu *__rseq_percpu_malloc(struct rseq_mempool *pool,
945 bool zeroed, void *init_ptr, size_t init_len)
ef6695f1 946{
9d986353 947 struct rseq_mempool_range *range;
ef6695f1
MD
948 struct free_list_node *node;
949 uintptr_t item_offset;
d24ee051 950 void __rseq_percpu *addr;
ef6695f1 951
6ff43d9a
MD
952 if (init_len > pool->item_len) {
953 errno = EINVAL;
954 return NULL;
955 }
ef6695f1
MD
956 pthread_mutex_lock(&pool->lock);
957 /* Get first entry from free list. */
958 node = pool->free_list_head;
959 if (node != NULL) {
a5694a4d 960 void *range_base, *ptr;
9d986353 961
a5694a4d
MD
962 ptr = __rseq_free_list_to_percpu_ptr(pool, node);
963 range_base = (void *) ((uintptr_t) ptr & (~(pool->attr.stride - 1)));
9d986353 964 range = (struct rseq_mempool_range *) (range_base - RANGE_HEADER_OFFSET);
ef6695f1
MD
965 /* Remove node from free list (update head). */
966 pool->free_list_head = node->next;
a5694a4d 967 item_offset = (uintptr_t) (ptr - range_base);
86617384 968 rseq_percpu_check_poison_item(pool, range, item_offset);
a5694a4d 969 addr = __rseq_free_list_to_percpu_ptr(pool, node);
ef6695f1
MD
970 goto end;
971 }
9d986353
MD
972 /*
973 * If the most recent range (first in list) does not have any
974 * room left, create a new range and prepend it to the list
975 * head.
976 */
977 range = pool->range_list;
978 if (range->next_unused + pool->item_len > pool->attr.stride) {
979 range = rseq_mempool_range_create(pool);
980 if (!range) {
981 errno = ENOMEM;
982 addr = NULL;
983 goto end;
984 }
985 /* Add range to head of list. */
986 range->next = pool->range_list;
987 pool->range_list = range;
ef6695f1 988 }
9d986353
MD
989 /* First range in list has room left. */
990 item_offset = range->next_unused;
991 addr = (void __rseq_percpu *) (range->base + item_offset);
992 range->next_unused += pool->item_len;
ef6695f1 993end:
8f28507f 994 if (addr)
9d986353 995 set_alloc_slot(pool, range, item_offset);
ef6695f1 996 pthread_mutex_unlock(&pool->lock);
6ff43d9a
MD
997 if (addr) {
998 if (zeroed)
999 rseq_percpu_zero_item(pool, range, item_offset);
1000 else if (init_ptr) {
1001 rseq_percpu_init_item(pool, range, item_offset,
1002 init_ptr, init_len);
1003 }
1004 }
ef6695f1
MD
1005 return addr;
1006}
1007
15da5c27 1008void __rseq_percpu *rseq_mempool_percpu_malloc(struct rseq_mempool *pool)
ef6695f1 1009{
6ff43d9a 1010 return __rseq_percpu_malloc(pool, false, NULL, 0);
ef6695f1
MD
1011}
1012
15da5c27 1013void __rseq_percpu *rseq_mempool_percpu_zmalloc(struct rseq_mempool *pool)
ef6695f1 1014{
6ff43d9a
MD
1015 return __rseq_percpu_malloc(pool, true, NULL, 0);
1016}
1017
1018void __rseq_percpu *rseq_mempool_percpu_malloc_init(struct rseq_mempool *pool,
1019 void *init_ptr, size_t len)
1020{
1021 return __rseq_percpu_malloc(pool, false, init_ptr, len);
ef6695f1
MD
1022}
1023
e7cbbc10
MD
1024/* Always inline for __builtin_return_address(0). */
1025static inline __attribute__((always_inline))
9d986353 1026void clear_alloc_slot(struct rseq_mempool *pool, struct rseq_mempool_range *range, size_t item_offset)
0fdf7a4c 1027{
9d986353 1028 unsigned long *bitmap = range->alloc_bitmap;
9649c7ee 1029 size_t item_index = item_offset >> pool->item_order;
0fdf7a4c
OD
1030 unsigned long mask;
1031 size_t k;
1032
9649c7ee 1033 if (!bitmap)
0fdf7a4c 1034 return;
0fdf7a4c 1035
9649c7ee
MD
1036 k = item_index / BIT_PER_ULONG;
1037 mask = 1ULL << (item_index % BIT_PER_ULONG);
0fdf7a4c 1038
9649c7ee
MD
1039 /* Print error if bit is not set. */
1040 if (!(bitmap[k] & mask)) {
ca452fee
MD
1041 fprintf(stderr, "%s: Double-free detected for pool: \"%s\" (%p), item offset: %zu, caller: %p.\n",
1042 __func__, get_pool_name(pool), pool, item_offset,
1043 (void *) __builtin_return_address(0));
9649c7ee
MD
1044 abort();
1045 }
0fdf7a4c
OD
1046 bitmap[k] &= ~mask;
1047}
1048
cb475906 1049void librseq_mempool_percpu_free(void __rseq_percpu *_ptr, size_t stride)
ef6695f1
MD
1050{
1051 uintptr_t ptr = (uintptr_t) _ptr;
cb475906 1052 void *range_base = (void *) (ptr & (~(stride - 1)));
0ba2a93e
MD
1053 struct rseq_mempool_range *range = (struct rseq_mempool_range *) (range_base - RANGE_HEADER_OFFSET);
1054 struct rseq_mempool *pool = range->pool;
cb475906 1055 uintptr_t item_offset = ptr & (stride - 1);
ef6695f1
MD
1056 struct free_list_node *head, *item;
1057
1058 pthread_mutex_lock(&pool->lock);
9d986353 1059 clear_alloc_slot(pool, range, item_offset);
ef6695f1
MD
1060 /* Add ptr to head of free list */
1061 head = pool->free_list_head;
455e090e
MD
1062 if (pool->attr.poison_set)
1063 rseq_percpu_poison_item(pool, range, item_offset);
a5694a4d 1064 item = __rseq_percpu_to_free_list_ptr(pool, _ptr);
455e090e
MD
1065 /*
1066 * Setting the next pointer will overwrite the first uintptr_t
a5694a4d
MD
1067 * poison for either CPU 0 (populate all) or init data (populate
1068 * none).
455e090e 1069 */
ef6695f1
MD
1070 item->next = head;
1071 pool->free_list_head = item;
1072 pthread_mutex_unlock(&pool->lock);
1073}
1074
0ba2a93e 1075struct rseq_mempool_set *rseq_mempool_set_create(void)
ef6695f1 1076{
0ba2a93e 1077 struct rseq_mempool_set *pool_set;
ef6695f1 1078
0ba2a93e 1079 pool_set = calloc(1, sizeof(struct rseq_mempool_set));
ef6695f1
MD
1080 if (!pool_set)
1081 return NULL;
1082 pthread_mutex_init(&pool_set->lock, NULL);
1083 return pool_set;
1084}
1085
0ba2a93e 1086int rseq_mempool_set_destroy(struct rseq_mempool_set *pool_set)
ef6695f1
MD
1087{
1088 int order, ret;
1089
1090 for (order = POOL_SET_MIN_ENTRY; order < POOL_SET_NR_ENTRIES; order++) {
0ba2a93e 1091 struct rseq_mempool *pool = pool_set->entries[order];
ef6695f1
MD
1092
1093 if (!pool)
1094 continue;
0ba2a93e 1095 ret = rseq_mempool_destroy(pool);
ef6695f1
MD
1096 if (ret)
1097 return ret;
1098 pool_set->entries[order] = NULL;
1099 }
1100 pthread_mutex_destroy(&pool_set->lock);
1101 free(pool_set);
1102 return 0;
1103}
1104
1105/* Ownership of pool is handed over to pool set on success. */
0ba2a93e 1106int rseq_mempool_set_add_pool(struct rseq_mempool_set *pool_set, struct rseq_mempool *pool)
ef6695f1
MD
1107{
1108 size_t item_order = pool->item_order;
1109 int ret = 0;
1110
1111 pthread_mutex_lock(&pool_set->lock);
1112 if (pool_set->entries[item_order]) {
1113 errno = EBUSY;
1114 ret = -1;
1115 goto end;
1116 }
1117 pool_set->entries[pool->item_order] = pool;
1118end:
1119 pthread_mutex_unlock(&pool_set->lock);
1120 return ret;
1121}
1122
1123static
6ff43d9a
MD
1124void __rseq_percpu *__rseq_mempool_set_malloc(struct rseq_mempool_set *pool_set,
1125 void *init_ptr, size_t len, bool zeroed)
ef6695f1
MD
1126{
1127 int order, min_order = POOL_SET_MIN_ENTRY;
0ba2a93e 1128 struct rseq_mempool *pool;
d24ee051 1129 void __rseq_percpu *addr;
ef6695f1 1130
d06f5cf5
MD
1131 order = rseq_get_count_order_ulong(len);
1132 if (order > POOL_SET_MIN_ENTRY)
1133 min_order = order;
ef6695f1
MD
1134again:
1135 pthread_mutex_lock(&pool_set->lock);
1136 /* First smallest present pool where @len fits. */
1137 for (order = min_order; order < POOL_SET_NR_ENTRIES; order++) {
1138 pool = pool_set->entries[order];
1139
1140 if (!pool)
1141 continue;
1142 if (pool->item_len >= len)
1143 goto found;
1144 }
1145 pool = NULL;
1146found:
1147 pthread_mutex_unlock(&pool_set->lock);
1148 if (pool) {
6ff43d9a 1149 addr = __rseq_percpu_malloc(pool, zeroed, init_ptr, len);
ef6695f1
MD
1150 if (addr == NULL && errno == ENOMEM) {
1151 /*
1152 * If the allocation failed, try again with a
1153 * larger pool.
1154 */
1155 min_order = order + 1;
1156 goto again;
1157 }
1158 } else {
1159 /* Not found. */
1160 errno = ENOMEM;
1161 addr = NULL;
1162 }
1163 return addr;
1164}
1165
15da5c27 1166void __rseq_percpu *rseq_mempool_set_percpu_malloc(struct rseq_mempool_set *pool_set, size_t len)
ef6695f1 1167{
6ff43d9a 1168 return __rseq_mempool_set_malloc(pool_set, NULL, len, false);
ef6695f1
MD
1169}
1170
15da5c27 1171void __rseq_percpu *rseq_mempool_set_percpu_zmalloc(struct rseq_mempool_set *pool_set, size_t len)
ef6695f1 1172{
6ff43d9a
MD
1173 return __rseq_mempool_set_malloc(pool_set, NULL, len, true);
1174}
1175
1176void __rseq_percpu *rseq_mempool_set_percpu_malloc_init(struct rseq_mempool_set *pool_set,
1177 void *init_ptr, size_t len)
1178{
1179 return __rseq_mempool_set_malloc(pool_set, init_ptr, len, true);
ef6695f1 1180}
9bd07c29 1181
0ba2a93e 1182struct rseq_mempool_attr *rseq_mempool_attr_create(void)
a82006d0 1183{
0ba2a93e 1184 return calloc(1, sizeof(struct rseq_mempool_attr));
a82006d0
MD
1185}
1186
0ba2a93e 1187void rseq_mempool_attr_destroy(struct rseq_mempool_attr *attr)
a82006d0
MD
1188{
1189 free(attr);
1190}
1191
135811f2 1192int rseq_mempool_attr_set_init(struct rseq_mempool_attr *attr,
6e329183 1193 int (*init_func)(void *priv, void *addr, size_t len, int cpu),
135811f2
MD
1194 void *init_priv)
1195{
1196 if (!attr) {
1197 errno = EINVAL;
1198 return -1;
1199 }
1200 attr->init_set = true;
1201 attr->init_func = init_func;
1202 attr->init_priv = init_priv;
1203 return 0;
1204}
1205
0ba2a93e 1206int rseq_mempool_attr_set_robust(struct rseq_mempool_attr *attr)
d6acc8aa
MD
1207{
1208 if (!attr) {
1209 errno = EINVAL;
1210 return -1;
1211 }
1212 attr->robust_set = true;
1213 return 0;
1214}
cb475906
MD
1215
1216int rseq_mempool_attr_set_percpu(struct rseq_mempool_attr *attr,
1217 size_t stride, int max_nr_cpus)
1218{
1219 if (!attr) {
1220 errno = EINVAL;
1221 return -1;
1222 }
1223 attr->type = MEMPOOL_TYPE_PERCPU;
1224 attr->stride = stride;
1225 attr->max_nr_cpus = max_nr_cpus;
1226 return 0;
1227}
1228
1229int rseq_mempool_attr_set_global(struct rseq_mempool_attr *attr,
1230 size_t stride)
1231{
1232 if (!attr) {
1233 errno = EINVAL;
1234 return -1;
1235 }
1236 attr->type = MEMPOOL_TYPE_GLOBAL;
1237 attr->stride = stride;
89b7e681 1238 attr->max_nr_cpus = 0;
cb475906
MD
1239 return 0;
1240}
6037d364 1241
e11a02d7
MD
1242int rseq_mempool_attr_set_max_nr_ranges(struct rseq_mempool_attr *attr,
1243 unsigned long max_nr_ranges)
1244{
1245 if (!attr) {
1246 errno = EINVAL;
1247 return -1;
1248 }
1249 attr->max_nr_ranges = max_nr_ranges;
1250 return 0;
1251}
1252
455e090e
MD
1253int rseq_mempool_attr_set_poison(struct rseq_mempool_attr *attr,
1254 uintptr_t poison)
1255{
1256 if (!attr) {
1257 errno = EINVAL;
1258 return -1;
1259 }
1260 attr->poison_set = true;
1261 attr->poison = poison;
1262 return 0;
1263}
1264
a5694a4d
MD
1265int rseq_mempool_attr_set_populate_policy(struct rseq_mempool_attr *attr,
1266 enum rseq_mempool_populate_policy policy)
1267{
1268 if (!attr) {
1269 errno = EINVAL;
1270 return -1;
1271 }
1272 attr->populate_policy = policy;
1273 return 0;
1274}
1275
6037d364
MD
1276int rseq_mempool_get_max_nr_cpus(struct rseq_mempool *mempool)
1277{
1278 if (!mempool || mempool->attr.type != MEMPOOL_TYPE_PERCPU) {
1279 errno = EINVAL;
1280 return -1;
1281 }
1282 return mempool->attr.max_nr_cpus;
1283}
This page took 0.119786 seconds and 4 git commands to generate.