1 /* SPDX-License-Identifier: MIT */
2 /* SPDX-FileCopyrightText: 2024 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> */
4 #ifndef _RSEQ_MEMPOOL_H
5 #define _RSEQ_MEMPOOL_H
7 #include <rseq/compiler.h>
13 * rseq/mempool.h: rseq memory pool allocator.
15 * The rseq memory pool allocator can be configured as either a global
16 * allocator (default) or a per-CPU memory allocator.
18 * The rseq global memory allocator allows the application to request
19 * memory pools of global memory each of containing objects of a
20 * given size (rounded to next power of 2), reserving a given virtual
21 * address size of the requested stride.
23 * The rseq per-CPU memory allocator allows the application the request
24 * memory pools of CPU-Local memory each of containing objects of a
25 * given size (rounded to next power of 2), reserving a given virtual
26 * address size per CPU, for a given maximum number of CPUs.
28 * The per-CPU memory allocator is analogous to TLS (Thread-Local
29 * Storage) memory: TLS is Thread-Local Storage, whereas the per-CPU
30 * memory allocator provides CPU-Local Storage.
32 * Memory pool sets can be created by adding one or more pools into
33 * them. They can be used to perform allocation of variable length
42 * The percpu offset stride can be overridden by the user code.
43 * The stride *must* match for all objects belonging to a given pool
44 * between arguments to:
46 * - rseq_mempool_create(),
47 * - rseq_percpu_ptr().
48 * - rseq_mempool_percpu_free(),
50 #define RSEQ_MEMPOOL_STRIDE (1U << 16) /* stride: 64kB */
53 * Tag pointers returned by:
54 * - rseq_mempool_percpu_malloc(),
55 * - rseq_mempool_percpu_zmalloc(),
56 * - rseq_mempool_set_percpu_malloc(),
57 * - rseq_mempool_set_percpu_zmalloc().
59 * and passed as parameter to:
60 * - rseq_percpu_ptr(),
61 * - rseq_mempool_percpu_free().
63 * with __rseq_percpu for use by static analyzers.
67 struct rseq_mempool_attr
;
71 * rseq_mempool_create: Create a memory pool.
73 * Create a memory pool for items of size @item_len (rounded to
76 * The @attr pointer used to specify the pool attributes. If NULL, use a
77 * default attribute values. The @attr can be destroyed immediately
78 * after rseq_mempool_create() returns. The caller keeps ownership
79 * of @attr. Default attributes select a global mempool type.
81 * The argument @pool_name can be used to given a name to the pool for
82 * debugging purposes. It can be NULL if no name is given.
84 * Returns a pointer to the created percpu pool. Return NULL on error,
85 * with errno set accordingly:
87 * EINVAL: Invalid argument.
88 * ENOMEM: Not enough resources (memory or pool indexes) available to
91 * In addition, if the attr mmap callback fails, NULL is returned and
92 * errno is propagated from the callback. The default callback can
93 * return errno=ENOMEM.
95 * This API is MT-safe.
97 struct rseq_mempool
*rseq_mempool_create(const char *pool_name
,
98 size_t item_len
, const struct rseq_mempool_attr
*attr
);
101 * rseq_mempool_destroy: Destroy a per-cpu memory pool.
103 * Destroy a per-cpu memory pool, unmapping its memory and removing the
104 * pool entry from the global index. No pointers allocated from the
105 * pool should be used when it is destroyed. This includes rseq_percpu_ptr().
107 * Argument @pool is a pointer to the per-cpu pool to destroy.
109 * Return values: 0 on success, -1 on error, with errno set accordingly:
111 * ENOENT: Trying to free a pool which was not allocated.
113 * If the munmap_func callback fails, -1 is returned and errno is
114 * propagated from the callback. The default callback can return
117 * This API is MT-safe.
119 int rseq_mempool_destroy(struct rseq_mempool
*pool
);
122 * rseq_mempool_percpu_malloc: Allocate memory from a per-cpu pool.
124 * Allocate an item from a per-cpu @pool. The allocation will reserve
125 * an item of the size specified by @item_len (rounded to next power of
126 * two) at pool creation. This effectively reserves space for this item
129 * On success, return a "__rseq_percpu" encoded pointer to the pool
130 * item. This encoded pointer is meant to be passed to rseq_percpu_ptr()
131 * to be decoded to a valid address before being accessed.
133 * Return NULL (errno=ENOMEM) if there is not enough space left in the
134 * pool to allocate an item.
136 * This API is MT-safe.
138 void __rseq_percpu
*rseq_mempool_percpu_malloc(struct rseq_mempool
*pool
);
141 * rseq_mempool_percpu_zmalloc: Allocated zero-initialized memory from a per-cpu pool.
143 * Allocate memory for an item within the pool, and zero-initialize its
144 * memory on all CPUs. See rseq_mempool_percpu_malloc for details.
146 * This API is MT-safe.
148 void __rseq_percpu
*rseq_mempool_percpu_zmalloc(struct rseq_mempool
*pool
);
151 * rseq_mempool_malloc: Allocate memory from a global pool.
153 * Wrapper to allocate memory from a global pool, which can be
154 * used directly without per-cpu indexing. Would normally be used
155 * with pools created with max_nr_cpus=1.
158 void *rseq_mempool_malloc(struct rseq_mempool
*pool
)
160 return (void *) rseq_mempool_percpu_malloc(pool
);
164 * rseq_mempool_zmalloc: Allocate zero-initialized memory from a global pool.
166 * Wrapper to allocate memory from a global pool, which can be
167 * used directly without per-cpu indexing. Would normally be used
168 * with pools created with max_nr_cpus=1.
171 void *rseq_mempool_zmalloc(struct rseq_mempool
*pool
)
173 return (void *) rseq_mempool_percpu_zmalloc(pool
);
177 * rseq_mempool_percpu_free: Free memory from a per-cpu pool.
179 * Free an item pointed to by @ptr from its per-cpu pool.
181 * The @ptr argument is a __rseq_percpu encoded pointer returned by
184 * - rseq_mempool_percpu_malloc(),
185 * - rseq_mempool_percpu_zmalloc(),
186 * - rseq_mempool_set_percpu_malloc(),
187 * - rseq_mempool_set_percpu_zmalloc().
189 * The @stride optional argument to rseq_percpu_free() is a configurable
190 * stride, which must match the stride received by pool creation.
191 * If the argument is not present, use the default RSEQ_MEMPOOL_STRIDE.
193 * This API is MT-safe.
195 void librseq_mempool_percpu_free(void __rseq_percpu
*ptr
, size_t stride
);
197 #define rseq_mempool_percpu_free(_ptr, _stride...) \
198 librseq_mempool_percpu_free(_ptr, RSEQ_PARAM_SELECT_ARG1(_, ##_stride, RSEQ_MEMPOOL_STRIDE))
201 * rseq_free: Free memory from a global pool.
203 * Free an item pointed to by @ptr from its global pool. Would normally
204 * be used with pools created with max_nr_cpus=1.
206 * The @ptr argument is a pointer returned by either:
208 * - rseq_mempool_malloc(),
209 * - rseq_mempool_zmalloc(),
210 * - rseq_mempool_set_malloc(),
211 * - rseq_mempool_set_zmalloc().
213 * The @stride optional argument to rseq_free() is a configurable
214 * stride, which must match the stride received by pool creation. If
215 * the argument is not present, use the default RSEQ_MEMPOOL_STRIDE.
216 * The stride is needed even for a global pool to know the mapping
219 * This API is MT-safe.
221 #define rseq_mempool_free(_ptr, _stride...) \
222 librseq_percpu_free((void __rseq_percpu *) _ptr, RSEQ_PARAM_SELECT_ARG1(_, ##_stride, RSEQ_MEMPOOL_STRIDE))
225 * rseq_percpu_ptr: Offset a per-cpu pointer for a given CPU.
227 * Offset a per-cpu pointer @ptr to get the associated pointer for the
228 * given @cpu. The @ptr argument is a __rseq_percpu pointer returned by
231 * - rseq_mempool_percpu_malloc(),
232 * - rseq_mempool_percpu_zmalloc(),
233 * - rseq_mempool_set_percpu_malloc(),
234 * - rseq_mempool_set_percpu_zmalloc().
236 * The macro rseq_percpu_ptr() preserves the type of the @ptr parameter
237 * for the returned pointer, but removes the __rseq_percpu annotation.
239 * The macro rseq_percpu_ptr() takes an optional @stride argument. If
240 * the argument is not present, use the default RSEQ_MEMPOOL_STRIDE.
241 * This must match the stride used for pool creation.
243 * This API is MT-safe.
245 #define rseq_percpu_ptr(_ptr, _cpu, _stride...) \
246 ((__typeof__(*(_ptr)) *) ((uintptr_t) (_ptr) + \
247 ((unsigned int) (_cpu) * \
248 (uintptr_t) RSEQ_PARAM_SELECT_ARG1(_, ##_stride, RSEQ_MEMPOOL_STRIDE))))
251 * rseq_mempool_set_create: Create a pool set.
253 * Create a set of pools. Its purpose is to offer a memory allocator API
254 * for variable-length items (e.g. variable length strings). When
255 * created, the pool set has no pool. Pools can be created and added to
256 * the set. One common approach would be to create pools for each
257 * relevant power of two allocation size useful for the application.
258 * Only one pool can be added to the pool set for each power of two
261 * Returns a pool set pointer on success, else returns NULL with
262 * errno=ENOMEM (out of memory).
264 * This API is MT-safe.
266 struct rseq_mempool_set
*rseq_mempool_set_create(void);
269 * rseq_mempool_set_destroy: Destroy a pool set.
271 * Destroy a pool set and its associated resources. The pools that were
272 * added to the pool set are destroyed as well.
274 * Returns 0 on success, -1 on failure (or partial failure), with errno
275 * set by rseq_percpu_pool_destroy(). Using a pool set after destroy
276 * failure is undefined.
278 * This API is MT-safe.
280 int rseq_mempool_set_destroy(struct rseq_mempool_set
*pool_set
);
283 * rseq_mempool_set_add_pool: Add a pool to a pool set.
285 * Add a @pool to the @pool_set. On success, its ownership is handed
286 * over to the pool set, so the caller should not destroy it explicitly.
287 * Only one pool can be added to the pool set for each power of two
290 * Returns 0 on success, -1 on error with the following errno:
291 * - EBUSY: A pool already exists in the pool set for this power of two
294 * This API is MT-safe.
296 int rseq_mempool_set_add_pool(struct rseq_mempool_set
*pool_set
,
297 struct rseq_mempool
*pool
);
300 * rseq_mempool_set_percpu_malloc: Allocate memory from a per-cpu pool set.
302 * Allocate an item from a per-cpu @pool. The allocation will reserve
303 * an item of the size specified by @len (rounded to next power of
304 * two). This effectively reserves space for this item on all CPUs.
306 * The space reservation will search for the smallest pool within
307 * @pool_set which respects the following conditions:
309 * - it has an item size large enough to fit @len,
310 * - it has space available.
312 * On success, return a "__rseq_percpu" encoded pointer to the pool
313 * item. This encoded pointer is meant to be passed to rseq_percpu_ptr()
314 * to be decoded to a valid address before being accessed.
316 * Return NULL (errno=ENOMEM) if there is not enough space left in the
317 * pool to allocate an item.
319 * This API is MT-safe.
321 void __rseq_percpu
*rseq_mempool_set_percpu_malloc(struct rseq_mempool_set
*pool_set
, size_t len
);
324 * rseq_mempool_set_percpu_zmalloc: Allocated zero-initialized memory from a per-cpu pool set.
326 * Allocate memory for an item within the pool, and zero-initialize its
327 * memory on all CPUs. See rseq_mempool_set_percpu_malloc for details.
329 * This API is MT-safe.
331 void __rseq_percpu
*rseq_mempool_set_percpu_zmalloc(struct rseq_mempool_set
*pool_set
, size_t len
);
334 * rseq_mempool_set_malloc: Allocate memory from a global pool set.
336 * Wrapper to allocate memory from a global pool, which can be
337 * used directly without per-cpu indexing. Would normally be used
338 * with pools created with max_nr_cpus=1.
341 void *rseq_mempool_set_malloc(struct rseq_mempool_set
*pool_set
, size_t len
)
343 return (void *) rseq_mempool_set_percpu_malloc(pool_set
, len
);
347 * rseq_mempool_set_zmalloc: Allocate zero-initialized memory from a global pool set.
349 * Wrapper to allocate memory from a global pool, which can be
350 * used directly without per-cpu indexing. Would normally be used
351 * with pools created with max_nr_cpus=1.
354 void *rseq_mempool_set_zmalloc(struct rseq_mempool_set
*pool_set
, size_t len
)
356 return (void *) rseq_mempool_set_percpu_zmalloc(pool_set
, len
);
360 * rseq_mempool_init_numa: Move pages to the NUMA node associated to their CPU topology.
362 * For pages allocated within @pool, invoke move_pages(2) with the given
363 * @numa_flags to move the pages to the NUMA node associated to their
366 * Argument @numa_flags are passed to move_pages(2). The expected flags are:
367 * MPOL_MF_MOVE: move process-private pages to cpu-specific numa nodes.
368 * MPOL_MF_MOVE_ALL: move shared pages to cpu-specific numa nodes
369 * (requires CAP_SYS_NICE).
371 * Returns 0 on success, else return -1 with errno set by move_pages(2).
373 int rseq_mempool_init_numa(struct rseq_mempool
*pool
, int numa_flags
);
376 * rseq_mempool_attr_create: Create a pool attribute structure.
378 struct rseq_mempool_attr
*rseq_mempool_attr_create(void);
381 * rseq_mempool_attr_destroy: Destroy a pool attribute structure.
383 void rseq_mempool_attr_destroy(struct rseq_mempool_attr
*attr
);
386 * rseq_mempool_attr_set_mmap: Set pool attribute structure mmap functions.
388 * The @mmap_func callback used to map the memory for the pool.
390 * The @munmap_func callback used to unmap the memory when the pool
393 * The @mmap_priv argument is a private data pointer passed to both
394 * @mmap_func and @munmap_func callbacks.
396 * Returns 0 on success, -1 with errno=EINVAL if arguments are invalid.
398 int rseq_mempool_attr_set_mmap(struct rseq_mempool_attr
*attr
,
399 void *(*mmap_func
)(void *priv
, size_t len
),
400 int (*munmap_func
)(void *priv
, void *ptr
, size_t len
),
404 * rseq_mempool_attr_set_init: Set pool attribute structure memory init functions.
406 * The @init_func callback used to initialized memory after allocation
409 * The @init_priv argument is a private data pointer passed to the
410 * @init_func callback.
412 * Returns 0 on success, -1 with errno=EINVAL if arguments are invalid.
414 int rseq_mempool_attr_set_init(struct rseq_mempool_attr
*attr
,
415 void (*init_func
)(void *priv
, void *addr
, size_t len
, int cpu
),
419 * rseq_mempool_attr_set_robust: Set pool robust attribute.
421 * The robust pool attribute enables runtime validation of the pool:
423 * - Check for double-free of pointers.
425 * - Detect memory leaks on pool destruction.
427 * - Detect free-list corruption on pool destruction.
429 * There is a marginal runtime overhead on malloc/free operations.
431 * The memory overhead is (pool->percpu_len / pool->item_len) / CHAR_BIT
432 * bytes, over the lifetime of the pool.
434 * Returns 0 on success, -1 with errno=EINVAL if arguments are invalid.
436 int rseq_mempool_attr_set_robust(struct rseq_mempool_attr
*attr
);
439 * rseq_mempool_attr_set_percpu: Set pool type as percpu.
441 * A pool created with this type is a per-cpu memory pool. The reserved
442 * allocation size is @stride, and the maximum CPU value expected
443 * is (@max_nr_cpus - 1). A @stride of 0 uses the default
444 * RSEQ_MEMPOOL_STRIDE.
446 * Returns 0 on success, -1 with errno=EINVAL if arguments are invalid.
448 int rseq_mempool_attr_set_percpu(struct rseq_mempool_attr
*attr
,
449 size_t stride
, int max_nr_cpus
);
452 * rseq_mempool_attr_set_global: Set pool type as global.
454 * A pool created with this type is a global memory pool. The reserved
455 * allocation size is @stride. A @stride of 0 uses the default
456 * RSEQ_MEMPOOL_STRIDE.
458 * Returns 0 on success, -1 with errno=EINVAL if arguments are invalid.
460 int rseq_mempool_attr_set_global(struct rseq_mempool_attr
*attr
, size_t stride
);
463 * rseq_mempool_range_init_numa: NUMA initialization helper for memory range.
465 * Helper which can be used from mempool_attr @init_func to move a CPU
466 * memory range to the NUMA node associated to its topology.
468 * Returns 0 on success, -1 with errno set by move_pages(2) on error.
469 * Returns -1, errno=ENOSYS if NUMA support is not present.
471 int rseq_mempool_range_init_numa(void *addr
, size_t len
, int cpu
, int numa_flags
);
474 * rseq_mempool_get_max_nr_cpus: Get the max_nr_cpus value configured for a pool.
476 * Returns a value >= 0 for a per-cpu pool.
477 * Returns -1, errno=EINVAL if the mempool is NULL or if the pool has a
480 int rseq_mempool_get_max_nr_cpus(struct rseq_mempool
*mempool
);
486 #endif /* _RSEQ_MEMPOOL_H */
This page took 0.040766 seconds and 4 git commands to generate.