1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright 2020 Mellanox Technologies, Ltd
6 #include <rte_malloc.h>
11 #include "mlx5_common_utils.h"
12 #include "mlx5_malloc.h"
15 uint32_t init:1; /* Memory allocator initialized. */
16 uint32_t enable:1; /* System memory select. */
17 uint32_t reserve:30; /* Reserve. */
18 struct rte_memseg_list *last_msl;
19 /* last allocated rte memory memseg list. */
20 #ifdef RTE_LIBRTE_MLX5_DEBUG
22 /* Memory allocated from system count. */
24 /* Memory allocated from hugepage count. */
26 /* Memory reallocate from system count. */
28 /* Memory reallocate from hugepage count. */
30 /* Memory free to system count. */
32 /* Memory free to hugepage count. */
36 /* MSL update count. */
40 /* Initialize default as not */
41 static struct mlx5_sys_mem mlx5_sys_mem = {
44 #ifdef RTE_LIBRTE_MLX5_DEBUG
57 * Check if the address belongs to memory seg list.
60 * Memory address to be ckeced.
65 * True if it belongs, false otherwise.
68 mlx5_mem_check_msl(void *addr, struct rte_memseg_list *msl)
75 end = RTE_PTR_ADD(start, msl->len);
76 if (addr >= start && addr < end)
82 * Update the msl if memory belongs to new msl.
88 mlx5_mem_update_msl(void *addr)
91 * Update the cache msl if the new addr comes from the new msl
92 * different with the cached msl.
94 if (addr && !mlx5_mem_check_msl(addr,
95 (struct rte_memseg_list *)__atomic_load_n
96 (&mlx5_sys_mem.last_msl, __ATOMIC_RELAXED))) {
97 __atomic_store_n(&mlx5_sys_mem.last_msl,
98 rte_mem_virt2memseg_list(addr),
100 #ifdef RTE_LIBRTE_MLX5_DEBUG
101 __atomic_add_fetch(&mlx5_sys_mem.msl_update, 1,
108 * Check if the address belongs to rte memory.
111 * Memory address to be ckeced.
114 * True if it belongs, false otherwise.
117 mlx5_mem_is_rte(void *addr)
120 * Check if the last cache msl matches. Drop to slow path
121 * to check if the memory belongs to rte memory.
123 if (!mlx5_mem_check_msl(addr, (struct rte_memseg_list *)
124 __atomic_load_n(&mlx5_sys_mem.last_msl, __ATOMIC_RELAXED))) {
125 if (!rte_mem_virt2memseg_list(addr))
127 #ifdef RTE_LIBRTE_MLX5_DEBUG
128 __atomic_add_fetch(&mlx5_sys_mem.msl_miss, 1, __ATOMIC_RELAXED);
135 * Allocate memory with alignment.
138 * Memory size to be allocated.
142 * Clear the allocated memory or not.
145 * Pointer of the allocated memory, NULL otherwise.
148 mlx5_alloc_align(size_t size, unsigned int align, unsigned int zero)
153 ret = posix_memalign(&buf, align, size);
156 "Couldn't allocate buf size=%zu align=%u. Err=%d\n",
162 memset(buf, 0, size);
167 mlx5_malloc(uint32_t flags, size_t size, unsigned int align, int socket)
173 * If neither system memory nor rte memory is required, allocate
174 * memory according to mlx5_sys_mem.enable.
176 if (flags & MLX5_MEM_RTE)
178 else if (flags & MLX5_MEM_SYS)
181 rte_mem = mlx5_sys_mem.enable ? false : true;
183 if (flags & MLX5_MEM_ZERO)
184 addr = rte_zmalloc_socket(NULL, size, align, socket);
186 addr = rte_malloc_socket(NULL, size, align, socket);
187 mlx5_mem_update_msl(addr);
188 #ifdef RTE_LIBRTE_MLX5_DEBUG
190 __atomic_add_fetch(&mlx5_sys_mem.malloc_rte, 1,
195 /* The memory will be allocated from system. */
196 if (align > MLX5_MALLOC_ALIGNMENT)
197 addr = mlx5_alloc_align(size, align, !!(flags & MLX5_MEM_ZERO));
198 else if (flags & MLX5_MEM_ZERO)
199 addr = calloc(1, size);
202 #ifdef RTE_LIBRTE_MLX5_DEBUG
204 __atomic_add_fetch(&mlx5_sys_mem.malloc_sys, 1,
211 mlx5_realloc(void *addr, uint32_t flags, size_t size, unsigned int align,
217 /* Allocate directly if old memory address is NULL. */
219 return mlx5_malloc(flags, size, align, socket);
220 /* Get the memory type. */
221 if (flags & MLX5_MEM_RTE)
223 else if (flags & MLX5_MEM_SYS)
226 rte_mem = mlx5_sys_mem.enable ? false : true;
227 /* Check if old memory and to be allocated memory are the same type. */
228 if (rte_mem != mlx5_mem_is_rte(addr)) {
229 DRV_LOG(ERR, "Couldn't reallocate to different memory type.");
232 /* Allocate memory from rte memory. */
234 new_addr = rte_realloc_socket(addr, size, align, socket);
235 mlx5_mem_update_msl(new_addr);
236 #ifdef RTE_LIBRTE_MLX5_DEBUG
238 __atomic_add_fetch(&mlx5_sys_mem.realloc_rte, 1,
243 /* Align is not supported for system memory. */
245 DRV_LOG(ERR, "Couldn't reallocate with alignment");
248 new_addr = realloc(addr, size);
249 #ifdef RTE_LIBRTE_MLX5_DEBUG
251 __atomic_add_fetch(&mlx5_sys_mem.realloc_sys, 1,
258 mlx5_free(void *addr)
262 if (!mlx5_mem_is_rte(addr)) {
263 #ifdef RTE_LIBRTE_MLX5_DEBUG
264 __atomic_add_fetch(&mlx5_sys_mem.free_sys, 1,
269 #ifdef RTE_LIBRTE_MLX5_DEBUG
270 __atomic_add_fetch(&mlx5_sys_mem.free_rte, 1,
278 mlx5_memory_stat_dump(void)
280 #ifdef RTE_LIBRTE_MLX5_DEBUG
281 DRV_LOG(INFO, "System memory malloc:%"PRIi64", realloc:%"PRIi64","
282 " free:%"PRIi64"\nRTE memory malloc:%"PRIi64","
283 " realloc:%"PRIi64", free:%"PRIi64"\nMSL miss:%"PRIi64","
285 __atomic_load_n(&mlx5_sys_mem.malloc_sys, __ATOMIC_RELAXED),
286 __atomic_load_n(&mlx5_sys_mem.realloc_sys, __ATOMIC_RELAXED),
287 __atomic_load_n(&mlx5_sys_mem.free_sys, __ATOMIC_RELAXED),
288 __atomic_load_n(&mlx5_sys_mem.malloc_rte, __ATOMIC_RELAXED),
289 __atomic_load_n(&mlx5_sys_mem.realloc_rte, __ATOMIC_RELAXED),
290 __atomic_load_n(&mlx5_sys_mem.free_rte, __ATOMIC_RELAXED),
291 __atomic_load_n(&mlx5_sys_mem.msl_miss, __ATOMIC_RELAXED),
292 __atomic_load_n(&mlx5_sys_mem.msl_update, __ATOMIC_RELAXED));
297 mlx5_malloc_mem_select(uint32_t sys_mem_en)
300 * The initialization should be called only once and all devices
301 * should use the same memory type. Otherwise, when new device is
302 * being attached with some different memory allocation configuration,
303 * the memory will get wrong behavior or a failure will be raised.
305 if (!mlx5_sys_mem.init) {
307 mlx5_sys_mem.enable = 1;
308 mlx5_sys_mem.init = 1;
309 DRV_LOG(INFO, "%s is selected.", sys_mem_en ? "SYS_MEM" : "RTE_MEM");
310 } else if (mlx5_sys_mem.enable != sys_mem_en) {
311 DRV_LOG(WARNING, "%s is already selected.",
312 mlx5_sys_mem.enable ? "SYS_MEM" : "RTE_MEM");