1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2014 Intel Corporation
11 * Memory-related RTE API.
22 #include <rte_common.h>
23 #include <rte_compat.h>
24 #include <rte_config.h>
25 #include <rte_fbarray.h>
27 #define RTE_PGSIZE_4K (1ULL << 12)
28 #define RTE_PGSIZE_64K (1ULL << 16)
29 #define RTE_PGSIZE_256K (1ULL << 18)
30 #define RTE_PGSIZE_2M (1ULL << 21)
31 #define RTE_PGSIZE_16M (1ULL << 24)
32 #define RTE_PGSIZE_256M (1ULL << 28)
33 #define RTE_PGSIZE_512M (1ULL << 29)
34 #define RTE_PGSIZE_1G (1ULL << 30)
35 #define RTE_PGSIZE_4G (1ULL << 32)
36 #define RTE_PGSIZE_16G (1ULL << 34)
38 #define SOCKET_ID_ANY -1 /**< Any NUMA socket. */
41 * Physical memory segment descriptor.
43 #define RTE_MEMSEG_FLAG_DO_NOT_FREE (1 << 0)
44 /**< Prevent this segment from being freed back to the OS. */
46 rte_iova_t iova; /**< Start IO address. */
49 void *addr; /**< Start virtual address. */
50 uint64_t addr_64; /**< Makes sure addr is always 64 bits */
52 size_t len; /**< Length of the segment. */
53 uint64_t hugepage_sz; /**< The pagesize of underlying memory */
54 int32_t socket_id; /**< NUMA socket ID. */
55 uint32_t nchannel; /**< Number of channels. */
56 uint32_t nrank; /**< Number of ranks. */
57 uint32_t flags; /**< Memseg-specific flags */
61 * memseg list is a special case as we need to store a bunch of other data
62 * together with the array itself.
64 struct rte_memseg_list {
68 /**< Base virtual address for this memseg list. */
70 /**< Makes sure addr is always 64-bits */
72 uint64_t page_sz; /**< Page size for all memsegs in this list. */
73 int socket_id; /**< Socket ID for all memsegs in this list. */
74 volatile uint32_t version; /**< version number for multiprocess sync. */
75 size_t len; /**< Length of memory area covered by this memseg list. */
76 unsigned int external; /**< 1 if this list points to external memory */
77 unsigned int heap; /**< 1 if this list points to a heap */
78 struct rte_fbarray memseg_arr;
82 * Lock page in physical memory and prevent from swapping.
85 * The virtual address.
87 * 0 on success, negative on error.
89 int rte_mem_lock_page(const void *virt);
92 * Get physical address of any mapped virtual address in the current process.
93 * It is found by browsing the /proc/self/pagemap special file.
94 * The page must be locked.
97 * The virtual address.
99 * The physical address or RTE_BAD_IOVA on error.
101 phys_addr_t rte_mem_virt2phy(const void *virt);
104 * Get IO virtual address of any mapped virtual address in the current process.
106 * @note This function will not check internal page table. Instead, in IOVA as
107 * PA mode, it will fall back to getting real physical address (which may
108 * not match the expected IOVA, such as what was specified for external
112 * The virtual address.
114 * The IO address or RTE_BAD_IOVA on error.
116 rte_iova_t rte_mem_virt2iova(const void *virt);
119 * Get virtual memory address corresponding to iova address.
121 * @note This function read-locks the memory hotplug subsystem, and thus cannot
122 * be used within memory-related callback functions.
127 * Virtual address corresponding to iova address (or NULL if address does not
128 * exist within DPDK memory map).
131 rte_mem_iova2virt(rte_iova_t iova);
134 * Get memseg to which a particular virtual address belongs.
137 * The virtual address.
139 * The memseg list in which to look up based on ``virt`` address
142 * Memseg pointer on success, or NULL on error.
145 rte_mem_virt2memseg(const void *virt, const struct rte_memseg_list *msl);
148 * Get memseg list corresponding to virtual memory address.
151 * The virtual address.
153 * Memseg list to which this virtual address belongs to.
155 struct rte_memseg_list *
156 rte_mem_virt2memseg_list(const void *virt);
159 * Memseg walk function prototype.
161 * Returning 0 will continue walk
162 * Returning 1 will stop the walk
163 * Returning -1 will stop the walk and report error
165 typedef int (*rte_memseg_walk_t)(const struct rte_memseg_list *msl,
166 const struct rte_memseg *ms, void *arg);
169 * Memseg contig walk function prototype. This will trigger a callback on every
170 * VA-contiguous area starting at memseg ``ms``, so total valid VA space at each
171 * callback call will be [``ms->addr``, ``ms->addr + len``).
173 * Returning 0 will continue walk
174 * Returning 1 will stop the walk
175 * Returning -1 will stop the walk and report error
177 typedef int (*rte_memseg_contig_walk_t)(const struct rte_memseg_list *msl,
178 const struct rte_memseg *ms, size_t len, void *arg);
181 * Memseg list walk function prototype. This will trigger a callback on every
182 * allocated memseg list.
184 * Returning 0 will continue walk
185 * Returning 1 will stop the walk
186 * Returning -1 will stop the walk and report error
188 typedef int (*rte_memseg_list_walk_t)(const struct rte_memseg_list *msl,
192 * Walk list of all memsegs.
194 * @note This function read-locks the memory hotplug subsystem, and thus cannot
195 * be used within memory-related callback functions.
197 * @note This function will also walk through externally allocated segments. It
198 * is up to the user to decide whether to skip through these segments.
203 * Argument passed to iterator
205 * 0 if walked over the entire list
206 * 1 if stopped by the user
207 * -1 if user function reported error
210 rte_memseg_walk(rte_memseg_walk_t func, void *arg);
213 * Walk each VA-contiguous area.
215 * @note This function read-locks the memory hotplug subsystem, and thus cannot
216 * be used within memory-related callback functions.
218 * @note This function will also walk through externally allocated segments. It
219 * is up to the user to decide whether to skip through these segments.
224 * Argument passed to iterator
226 * 0 if walked over the entire list
227 * 1 if stopped by the user
228 * -1 if user function reported error
231 rte_memseg_contig_walk(rte_memseg_contig_walk_t func, void *arg);
234 * Walk each allocated memseg list.
236 * @note This function read-locks the memory hotplug subsystem, and thus cannot
237 * be used within memory-related callback functions.
239 * @note This function will also walk through externally allocated segments. It
240 * is up to the user to decide whether to skip through these segments.
245 * Argument passed to iterator
247 * 0 if walked over the entire list
248 * 1 if stopped by the user
249 * -1 if user function reported error
252 rte_memseg_list_walk(rte_memseg_list_walk_t func, void *arg);
255 * Walk list of all memsegs without performing any locking.
257 * @note This function does not perform any locking, and is only safe to call
258 * from within memory-related callback functions.
263 * Argument passed to iterator
265 * 0 if walked over the entire list
266 * 1 if stopped by the user
267 * -1 if user function reported error
270 rte_memseg_walk_thread_unsafe(rte_memseg_walk_t func, void *arg);
273 * Walk each VA-contiguous area without performing any locking.
275 * @note This function does not perform any locking, and is only safe to call
276 * from within memory-related callback functions.
281 * Argument passed to iterator
283 * 0 if walked over the entire list
284 * 1 if stopped by the user
285 * -1 if user function reported error
288 rte_memseg_contig_walk_thread_unsafe(rte_memseg_contig_walk_t func, void *arg);
291 * Walk each allocated memseg list without performing any locking.
293 * @note This function does not perform any locking, and is only safe to call
294 * from within memory-related callback functions.
299 * Argument passed to iterator
301 * 0 if walked over the entire list
302 * 1 if stopped by the user
303 * -1 if user function reported error
306 rte_memseg_list_walk_thread_unsafe(rte_memseg_list_walk_t func, void *arg);
309 * Return file descriptor associated with a particular memseg (if available).
311 * @note This function read-locks the memory hotplug subsystem, and thus cannot
312 * be used within memory-related callback functions.
314 * @note This returns an internal file descriptor. Performing any operations on
315 * this file descriptor is inherently dangerous, so it should be treated
316 * as read-only for all intents and purposes.
319 * A pointer to memseg for which to get file descriptor.
322 * Valid file descriptor in case of success.
323 * -1 in case of error, with ``rte_errno`` set to the following values:
324 * - EINVAL - ``ms`` pointer was NULL or did not point to a valid memseg
325 * - ENODEV - ``ms`` fd is not available
326 * - ENOENT - ``ms`` is an unused segment
327 * - ENOTSUP - segment fd's are not supported
330 rte_memseg_get_fd(const struct rte_memseg *ms);
333 * Return file descriptor associated with a particular memseg (if available).
335 * @note This function does not perform any locking, and is only safe to call
336 * from within memory-related callback functions.
338 * @note This returns an internal file descriptor. Performing any operations on
339 * this file descriptor is inherently dangerous, so it should be treated
340 * as read-only for all intents and purposes.
343 * A pointer to memseg for which to get file descriptor.
346 * Valid file descriptor in case of success.
347 * -1 in case of error, with ``rte_errno`` set to the following values:
348 * - EINVAL - ``ms`` pointer was NULL or did not point to a valid memseg
349 * - ENODEV - ``ms`` fd is not available
350 * - ENOENT - ``ms`` is an unused segment
351 * - ENOTSUP - segment fd's are not supported
354 rte_memseg_get_fd_thread_unsafe(const struct rte_memseg *ms);
357 * Get offset into segment file descriptor associated with a particular memseg
360 * @note This function read-locks the memory hotplug subsystem, and thus cannot
361 * be used within memory-related callback functions.
364 * A pointer to memseg for which to get file descriptor.
366 * A pointer to offset value where the result will be stored.
369 * Valid file descriptor in case of success.
370 * -1 in case of error, with ``rte_errno`` set to the following values:
371 * - EINVAL - ``ms`` pointer was NULL or did not point to a valid memseg
372 * - EINVAL - ``offset`` pointer was NULL
373 * - ENODEV - ``ms`` fd is not available
374 * - ENOENT - ``ms`` is an unused segment
375 * - ENOTSUP - segment fd's are not supported
378 rte_memseg_get_fd_offset(const struct rte_memseg *ms, size_t *offset);
381 * Get offset into segment file descriptor associated with a particular memseg
384 * @note This function does not perform any locking, and is only safe to call
385 * from within memory-related callback functions.
388 * A pointer to memseg for which to get file descriptor.
390 * A pointer to offset value where the result will be stored.
393 * Valid file descriptor in case of success.
394 * -1 in case of error, with ``rte_errno`` set to the following values:
395 * - EINVAL - ``ms`` pointer was NULL or did not point to a valid memseg
396 * - EINVAL - ``offset`` pointer was NULL
397 * - ENODEV - ``ms`` fd is not available
398 * - ENOENT - ``ms`` is an unused segment
399 * - ENOTSUP - segment fd's are not supported
402 rte_memseg_get_fd_offset_thread_unsafe(const struct rte_memseg *ms,
406 * Register external memory chunk with DPDK.
408 * @note Using this API is mutually exclusive with ``rte_malloc`` family of
411 * @note This API will not perform any DMA mapping. It is expected that user
412 * will do that themselves.
414 * @note Before accessing this memory in other processes, it needs to be
415 * attached in each of those processes by calling ``rte_extmem_attach`` in
416 * each other process.
419 * Start of virtual area to register. Must be aligned by ``page_sz``.
421 * Length of virtual area to register. Must be aligned by ``page_sz``.
423 * Array of page IOVA addresses corresponding to each page in this memory
424 * area. Can be NULL, in which case page IOVA addresses will be set to
427 * Number of elements in the iova_addrs array. Ignored if ``iova_addrs``
430 * Page size of the underlying memory
434 * - -1 in case of error, with rte_errno set to one of the following:
435 * EINVAL - one of the parameters was invalid
436 * EEXIST - memory chunk is already registered
437 * ENOSPC - no more space in internal config to store a new memory chunk
440 rte_extmem_register(void *va_addr, size_t len, rte_iova_t iova_addrs[],
441 unsigned int n_pages, size_t page_sz);
444 * Unregister external memory chunk with DPDK.
446 * @note Using this API is mutually exclusive with ``rte_malloc`` family of
449 * @note This API will not perform any DMA unmapping. It is expected that user
450 * will do that themselves.
452 * @note Before calling this function, all other processes must call
453 * ``rte_extmem_detach`` to detach from the memory area.
456 * Start of virtual area to unregister
458 * Length of virtual area to unregister
462 * - -1 in case of error, with rte_errno set to one of the following:
463 * EINVAL - one of the parameters was invalid
464 * ENOENT - memory chunk was not found
467 rte_extmem_unregister(void *va_addr, size_t len);
470 * Attach to external memory chunk registered in another process.
472 * @note Using this API is mutually exclusive with ``rte_malloc`` family of
475 * @note This API will not perform any DMA mapping. It is expected that user
476 * will do that themselves.
479 * Start of virtual area to register
481 * Length of virtual area to register
485 * - -1 in case of error, with rte_errno set to one of the following:
486 * EINVAL - one of the parameters was invalid
487 * ENOENT - memory chunk was not found
490 rte_extmem_attach(void *va_addr, size_t len);
493 * Detach from external memory chunk registered in another process.
495 * @note Using this API is mutually exclusive with ``rte_malloc`` family of
498 * @note This API will not perform any DMA unmapping. It is expected that user
499 * will do that themselves.
502 * Start of virtual area to unregister
504 * Length of virtual area to unregister
508 * - -1 in case of error, with rte_errno set to one of the following:
509 * EINVAL - one of the parameters was invalid
510 * ENOENT - memory chunk was not found
513 rte_extmem_detach(void *va_addr, size_t len);
516 * Dump the physical memory layout to a file.
518 * @note This function read-locks the memory hotplug subsystem, and thus cannot
519 * be used within memory-related callback functions.
522 * A pointer to a file for output
524 void rte_dump_physmem_layout(FILE *f);
527 * Get the total amount of available physical memory.
529 * @note This function read-locks the memory hotplug subsystem, and thus cannot
530 * be used within memory-related callback functions.
533 * The total amount of available physical memory in bytes.
535 uint64_t rte_eal_get_physmem_size(void);
538 * Get the number of memory channels.
541 * The number of memory channels on the system. The value is 0 if unknown
542 * or not the same on all devices.
544 unsigned rte_memory_get_nchannel(void);
547 * Get the number of memory ranks.
550 * The number of memory ranks on the system. The value is 0 if unknown or
551 * not the same on all devices.
553 unsigned rte_memory_get_nrank(void);
556 * Check if all currently allocated memory segments are compliant with
557 * supplied DMA address width.
560 * Address width to check against.
562 int rte_mem_check_dma_mask(uint8_t maskbits);
565 * Check if all currently allocated memory segments are compliant with
566 * supplied DMA address width. This function will use
567 * rte_memseg_walk_thread_unsafe instead of rte_memseg_walk implying
568 * memory_hotplug_lock will not be acquired avoiding deadlock during
569 * memory initialization.
571 * This function is just for EAL core memory internal use. Drivers should
572 * use the previous rte_mem_check_dma_mask.
575 * Address width to check against.
577 int rte_mem_check_dma_mask_thread_unsafe(uint8_t maskbits);
580 * Set dma mask to use once memory initialization is done. Previous functions
581 * rte_mem_check_dma_mask and rte_mem_check_dma_mask_thread_unsafe can not be
582 * used safely until memory has been initialized.
584 void rte_mem_set_dma_mask(uint8_t maskbits);
587 * Drivers based on uio will not load unless physical
588 * addresses are obtainable. It is only possible to get
589 * physical addresses when running as a privileged user.
592 * 1 if the system is able to obtain physical addresses.
593 * 0 if using DMA addresses through an IOMMU.
595 int rte_eal_using_phys_addrs(void);
599 * Enum indicating which kind of memory event has happened. Used by callbacks to
600 * distinguish between memory allocations and deallocations.
603 RTE_MEM_EVENT_ALLOC = 0, /**< Allocation event. */
604 RTE_MEM_EVENT_FREE, /**< Deallocation event. */
606 #define RTE_MEM_EVENT_CALLBACK_NAME_LEN 64
607 /**< maximum length of callback name */
610 * Function typedef used to register callbacks for memory events.
612 typedef void (*rte_mem_event_callback_t)(enum rte_mem_event event_type,
613 const void *addr, size_t len, void *arg);
616 * Function used to register callbacks for memory events.
618 * @note callbacks will happen while memory hotplug subsystem is write-locked,
619 * therefore some functions (e.g. `rte_memseg_walk()`) will cause a
620 * deadlock when called from within such callbacks.
622 * @note mem event callbacks not being supported is an expected error condition,
623 * so user code needs to handle this situation. In these cases, return
624 * value will be -1, and rte_errno will be set to ENOTSUP.
627 * Name associated with specified callback to be added to the list.
630 * Callback function pointer.
633 * Argument to pass to the callback.
636 * 0 on successful callback register
637 * -1 on unsuccessful callback register, with rte_errno value indicating
638 * reason for failure.
641 rte_mem_event_callback_register(const char *name, rte_mem_event_callback_t clb,
645 * Function used to unregister callbacks for memory events.
648 * Name associated with specified callback to be removed from the list.
651 * Argument to look for among callbacks with specified callback name.
654 * 0 on successful callback unregister
655 * -1 on unsuccessful callback unregister, with rte_errno value indicating
656 * reason for failure.
659 rte_mem_event_callback_unregister(const char *name, void *arg);
662 #define RTE_MEM_ALLOC_VALIDATOR_NAME_LEN 64
663 /**< maximum length of alloc validator name */
665 * Function typedef used to register memory allocation validation callbacks.
667 * Returning 0 will allow allocation attempt to continue. Returning -1 will
668 * prevent allocation from succeeding.
670 typedef int (*rte_mem_alloc_validator_t)(int socket_id,
671 size_t cur_limit, size_t new_len);
674 * @brief Register validator callback for memory allocations.
676 * Callbacks registered by this function will be called right before memory
677 * allocator is about to trigger allocation of more pages from the system if
678 * said allocation will bring total memory usage above specified limit on
679 * specified socket. User will be able to cancel pending allocation if callback
682 * @note callbacks will happen while memory hotplug subsystem is write-locked,
683 * therefore some functions (e.g. `rte_memseg_walk()`) will cause a
684 * deadlock when called from within such callbacks.
686 * @note validator callbacks not being supported is an expected error condition,
687 * so user code needs to handle this situation. In these cases, return
688 * value will be -1, and rte_errno will be set to ENOTSUP.
691 * Name associated with specified callback to be added to the list.
694 * Callback function pointer.
697 * Socket ID on which to watch for allocations.
700 * Limit above which to trigger callbacks.
703 * 0 on successful callback register
704 * -1 on unsuccessful callback register, with rte_errno value indicating
705 * reason for failure.
708 rte_mem_alloc_validator_register(const char *name,
709 rte_mem_alloc_validator_t clb, int socket_id, size_t limit);
712 * @brief Unregister validator callback for memory allocations.
715 * Name associated with specified callback to be removed from the list.
718 * Socket ID on which to watch for allocations.
721 * 0 on successful callback unregister
722 * -1 on unsuccessful callback unregister, with rte_errno value indicating
723 * reason for failure.
726 rte_mem_alloc_validator_unregister(const char *name, int socket_id);
732 #endif /* _RTE_MEMORY_H_ */