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>
29 RTE_PGSIZE_4K = 1ULL << 12,
30 RTE_PGSIZE_64K = 1ULL << 16,
31 RTE_PGSIZE_256K = 1ULL << 18,
32 RTE_PGSIZE_2M = 1ULL << 21,
33 RTE_PGSIZE_16M = 1ULL << 24,
34 RTE_PGSIZE_256M = 1ULL << 28,
35 RTE_PGSIZE_512M = 1ULL << 29,
36 RTE_PGSIZE_1G = 1ULL << 30,
37 RTE_PGSIZE_4G = 1ULL << 32,
38 RTE_PGSIZE_16G = 1ULL << 34,
41 #define SOCKET_ID_ANY -1 /**< Any NUMA socket. */
44 * Physical memory segment descriptor.
46 #define RTE_MEMSEG_FLAG_DO_NOT_FREE (1 << 0)
47 /**< Prevent this segment from being freed back to the OS. */
51 phys_addr_t phys_addr; /**< deprecated - Start physical address. */
52 rte_iova_t iova; /**< Start IO address. */
56 void *addr; /**< Start virtual address. */
57 uint64_t addr_64; /**< Makes sure addr is always 64 bits */
59 size_t len; /**< Length of the segment. */
60 uint64_t hugepage_sz; /**< The pagesize of underlying memory */
61 int32_t socket_id; /**< NUMA socket ID. */
62 uint32_t nchannel; /**< Number of channels. */
63 uint32_t nrank; /**< Number of ranks. */
64 uint32_t flags; /**< Memseg-specific flags */
68 * memseg list is a special case as we need to store a bunch of other data
69 * together with the array itself.
71 struct rte_memseg_list {
75 /**< Base virtual address for this memseg list. */
77 /**< Makes sure addr is always 64-bits */
79 uint64_t page_sz; /**< Page size for all memsegs in this list. */
80 int socket_id; /**< Socket ID for all memsegs in this list. */
81 volatile uint32_t version; /**< version number for multiprocess sync. */
82 size_t len; /**< Length of memory area covered by this memseg list. */
83 unsigned int external; /**< 1 if this list points to external memory */
84 struct rte_fbarray memseg_arr;
88 * Lock page in physical memory and prevent from swapping.
91 * The virtual address.
93 * 0 on success, negative on error.
95 int rte_mem_lock_page(const void *virt);
98 * Get physical address of any mapped virtual address in the current process.
99 * It is found by browsing the /proc/self/pagemap special file.
100 * The page must be locked.
103 * The virtual address.
105 * The physical address or RTE_BAD_IOVA on error.
107 phys_addr_t rte_mem_virt2phy(const void *virt);
110 * Get IO virtual address of any mapped virtual address in the current process.
113 * The virtual address.
115 * The IO address or RTE_BAD_IOVA on error.
117 rte_iova_t rte_mem_virt2iova(const void *virt);
120 * Get virtual memory address corresponding to iova address.
122 * @note This function read-locks the memory hotplug subsystem, and thus cannot
123 * be used within memory-related callback functions.
128 * Virtual address corresponding to iova address (or NULL if address does not
129 * exist within DPDK memory map).
133 rte_mem_iova2virt(rte_iova_t iova);
136 * Get memseg to which a particular virtual address belongs.
139 * The virtual address.
141 * The memseg list in which to look up based on ``virt`` address
144 * Memseg pointer on success, or NULL on error.
148 rte_mem_virt2memseg(const void *virt, const struct rte_memseg_list *msl);
151 * Get memseg list corresponding to virtual memory address.
154 * The virtual address.
156 * Memseg list to which this virtual address belongs to.
159 struct rte_memseg_list *
160 rte_mem_virt2memseg_list(const void *virt);
163 * Memseg walk function prototype.
165 * Returning 0 will continue walk
166 * Returning 1 will stop the walk
167 * Returning -1 will stop the walk and report error
169 typedef int (*rte_memseg_walk_t)(const struct rte_memseg_list *msl,
170 const struct rte_memseg *ms, void *arg);
173 * Memseg contig walk function prototype. This will trigger a callback on every
174 * VA-contiguous area starting at memseg ``ms``, so total valid VA space at each
175 * callback call will be [``ms->addr``, ``ms->addr + len``).
177 * Returning 0 will continue walk
178 * Returning 1 will stop the walk
179 * Returning -1 will stop the walk and report error
181 typedef int (*rte_memseg_contig_walk_t)(const struct rte_memseg_list *msl,
182 const struct rte_memseg *ms, size_t len, void *arg);
185 * Memseg list walk function prototype. This will trigger a callback on every
186 * allocated memseg list.
188 * Returning 0 will continue walk
189 * Returning 1 will stop the walk
190 * Returning -1 will stop the walk and report error
192 typedef int (*rte_memseg_list_walk_t)(const struct rte_memseg_list *msl,
196 * Walk list of all memsegs.
198 * @note This function read-locks the memory hotplug subsystem, and thus cannot
199 * be used within memory-related callback functions.
201 * @note This function will also walk through externally allocated segments. It
202 * is up to the user to decide whether to skip through these segments.
207 * Argument passed to iterator
209 * 0 if walked over the entire list
210 * 1 if stopped by the user
211 * -1 if user function reported error
215 rte_memseg_walk(rte_memseg_walk_t func, void *arg);
218 * Walk each VA-contiguous area.
220 * @note This function read-locks the memory hotplug subsystem, and thus cannot
221 * be used within memory-related callback functions.
223 * @note This function will also walk through externally allocated segments. It
224 * is up to the user to decide whether to skip through these segments.
229 * Argument passed to iterator
231 * 0 if walked over the entire list
232 * 1 if stopped by the user
233 * -1 if user function reported error
237 rte_memseg_contig_walk(rte_memseg_contig_walk_t func, void *arg);
240 * Walk each allocated memseg list.
242 * @note This function read-locks the memory hotplug subsystem, and thus cannot
243 * be used within memory-related callback functions.
245 * @note This function will also walk through externally allocated segments. It
246 * is up to the user to decide whether to skip through these segments.
251 * Argument passed to iterator
253 * 0 if walked over the entire list
254 * 1 if stopped by the user
255 * -1 if user function reported error
259 rte_memseg_list_walk(rte_memseg_list_walk_t func, void *arg);
262 * Walk list of all memsegs without performing any locking.
264 * @note This function does not perform any locking, and is only safe to call
265 * from within memory-related callback functions.
270 * Argument passed to iterator
272 * 0 if walked over the entire list
273 * 1 if stopped by the user
274 * -1 if user function reported error
278 rte_memseg_walk_thread_unsafe(rte_memseg_walk_t func, void *arg);
281 * Walk each VA-contiguous area without performing any locking.
283 * @note This function does not perform any locking, and is only safe to call
284 * from within memory-related callback functions.
289 * Argument passed to iterator
291 * 0 if walked over the entire list
292 * 1 if stopped by the user
293 * -1 if user function reported error
297 rte_memseg_contig_walk_thread_unsafe(rte_memseg_contig_walk_t func, void *arg);
300 * Walk each allocated memseg list without performing any locking.
302 * @note This function does not perform any locking, and is only safe to call
303 * from within memory-related callback functions.
308 * Argument passed to iterator
310 * 0 if walked over the entire list
311 * 1 if stopped by the user
312 * -1 if user function reported error
316 rte_memseg_list_walk_thread_unsafe(rte_memseg_list_walk_t func, void *arg);
319 * Return file descriptor associated with a particular memseg (if available).
321 * @note This function read-locks the memory hotplug subsystem, and thus cannot
322 * be used within memory-related callback functions.
324 * @note This returns an internal file descriptor. Performing any operations on
325 * this file descriptor is inherently dangerous, so it should be treated
326 * as read-only for all intents and purposes.
329 * A pointer to memseg for which to get file descriptor.
332 * Valid file descriptor in case of success.
333 * -1 in case of error, with ``rte_errno`` set to the following values:
334 * - EINVAL - ``ms`` pointer was NULL or did not point to a valid memseg
335 * - ENODEV - ``ms`` fd is not available
336 * - ENOENT - ``ms`` is an unused segment
337 * - ENOTSUP - segment fd's are not supported
341 rte_memseg_get_fd(const struct rte_memseg *ms);
344 * Return file descriptor associated with a particular memseg (if available).
346 * @note This function does not perform any locking, and is only safe to call
347 * from within memory-related callback functions.
349 * @note This returns an internal file descriptor. Performing any operations on
350 * this file descriptor is inherently dangerous, so it should be treated
351 * as read-only for all intents and purposes.
354 * A pointer to memseg for which to get file descriptor.
357 * Valid file descriptor in case of success.
358 * -1 in case of error, with ``rte_errno`` set to the following values:
359 * - EINVAL - ``ms`` pointer was NULL or did not point to a valid memseg
360 * - ENODEV - ``ms`` fd is not available
361 * - ENOENT - ``ms`` is an unused segment
362 * - ENOTSUP - segment fd's are not supported
366 rte_memseg_get_fd_thread_unsafe(const struct rte_memseg *ms);
369 * Get offset into segment file descriptor associated with a particular memseg
372 * @note This function read-locks the memory hotplug subsystem, and thus cannot
373 * be used within memory-related callback functions.
376 * A pointer to memseg for which to get file descriptor.
378 * A pointer to offset value where the result will be stored.
381 * Valid file descriptor in case of success.
382 * -1 in case of error, with ``rte_errno`` set to the following values:
383 * - EINVAL - ``ms`` pointer was NULL or did not point to a valid memseg
384 * - EINVAL - ``offset`` pointer was NULL
385 * - ENODEV - ``ms`` fd is not available
386 * - ENOENT - ``ms`` is an unused segment
387 * - ENOTSUP - segment fd's are not supported
391 rte_memseg_get_fd_offset(const struct rte_memseg *ms, size_t *offset);
394 * Get offset into segment file descriptor associated with a particular memseg
397 * @note This function does not perform any locking, and is only safe to call
398 * from within memory-related callback functions.
401 * A pointer to memseg for which to get file descriptor.
403 * A pointer to offset value where the result will be stored.
406 * Valid file descriptor in case of success.
407 * -1 in case of error, with ``rte_errno`` set to the following values:
408 * - EINVAL - ``ms`` pointer was NULL or did not point to a valid memseg
409 * - EINVAL - ``offset`` pointer was NULL
410 * - ENODEV - ``ms`` fd is not available
411 * - ENOENT - ``ms`` is an unused segment
412 * - ENOTSUP - segment fd's are not supported
416 rte_memseg_get_fd_offset_thread_unsafe(const struct rte_memseg *ms,
421 * @b EXPERIMENTAL: this API may change without prior notice
423 * Register external memory chunk with DPDK.
425 * @note Using this API is mutually exclusive with ``rte_malloc`` family of
428 * @note This API will not perform any DMA mapping. It is expected that user
429 * will do that themselves.
431 * @note Before accessing this memory in other processes, it needs to be
432 * attached in each of those processes by calling ``rte_extmem_attach`` in
433 * each other process.
436 * Start of virtual area to register. Must be aligned by ``page_sz``.
438 * Length of virtual area to register. Must be aligned by ``page_sz``.
440 * Array of page IOVA addresses corresponding to each page in this memory
441 * area. Can be NULL, in which case page IOVA addresses will be set to
444 * Number of elements in the iova_addrs array. Ignored if ``iova_addrs``
447 * Page size of the underlying memory
451 * - -1 in case of error, with rte_errno set to one of the following:
452 * EINVAL - one of the parameters was invalid
453 * EEXIST - memory chunk is already registered
454 * ENOSPC - no more space in internal config to store a new memory chunk
458 rte_extmem_register(void *va_addr, size_t len, rte_iova_t iova_addrs[],
459 unsigned int n_pages, size_t page_sz);
463 * @b EXPERIMENTAL: this API may change without prior notice
465 * Unregister external memory chunk with DPDK.
467 * @note Using this API is mutually exclusive with ``rte_malloc`` family of
470 * @note This API will not perform any DMA unmapping. It is expected that user
471 * will do that themselves.
473 * @note Before calling this function, all other processes must call
474 * ``rte_extmem_detach`` to detach from the memory area.
477 * Start of virtual area to unregister
479 * Length of virtual area to unregister
483 * - -1 in case of error, with rte_errno set to one of the following:
484 * EINVAL - one of the parameters was invalid
485 * ENOENT - memory chunk was not found
489 rte_extmem_unregister(void *va_addr, size_t len);
493 * @b EXPERIMENTAL: this API may change without prior notice
495 * Attach to external memory chunk registered in another process.
497 * @note Using this API is mutually exclusive with ``rte_malloc`` family of
500 * @note This API will not perform any DMA mapping. It is expected that user
501 * will do that themselves.
504 * Start of virtual area to register
506 * Length of virtual area to register
510 * - -1 in case of error, with rte_errno set to one of the following:
511 * EINVAL - one of the parameters was invalid
512 * ENOENT - memory chunk was not found
516 rte_extmem_attach(void *va_addr, size_t len);
520 * @b EXPERIMENTAL: this API may change without prior notice
522 * Detach from external memory chunk registered in another process.
524 * @note Using this API is mutually exclusive with ``rte_malloc`` family of
527 * @note This API will not perform any DMA unmapping. It is expected that user
528 * will do that themselves.
531 * Start of virtual area to unregister
533 * Length of virtual area to unregister
537 * - -1 in case of error, with rte_errno set to one of the following:
538 * EINVAL - one of the parameters was invalid
539 * ENOENT - memory chunk was not found
543 rte_extmem_detach(void *va_addr, size_t len);
546 * Dump the physical memory layout to a file.
548 * @note This function read-locks the memory hotplug subsystem, and thus cannot
549 * be used within memory-related callback functions.
552 * A pointer to a file for output
554 void rte_dump_physmem_layout(FILE *f);
557 * Get the total amount of available physical memory.
559 * @note This function read-locks the memory hotplug subsystem, and thus cannot
560 * be used within memory-related callback functions.
563 * The total amount of available physical memory in bytes.
565 uint64_t rte_eal_get_physmem_size(void);
568 * Get the number of memory channels.
571 * The number of memory channels on the system. The value is 0 if unknown
572 * or not the same on all devices.
574 unsigned rte_memory_get_nchannel(void);
577 * Get the number of memory ranks.
580 * The number of memory ranks on the system. The value is 0 if unknown or
581 * not the same on all devices.
583 unsigned rte_memory_get_nrank(void);
587 * @b EXPERIMENTAL: this API may change without prior notice
589 * Check if all currently allocated memory segments are compliant with
590 * supplied DMA address width.
593 * Address width to check against.
596 int rte_mem_check_dma_mask(uint8_t maskbits);
600 * @b EXPERIMENTAL: this API may change without prior notice
602 * Check if all currently allocated memory segments are compliant with
603 * supplied DMA address width. This function will use
604 * rte_memseg_walk_thread_unsafe instead of rte_memseg_walk implying
605 * memory_hotplug_lock will not be acquired avoiding deadlock during
606 * memory initialization.
608 * This function is just for EAL core memory internal use. Drivers should
609 * use the previous rte_mem_check_dma_mask.
612 * Address width to check against.
615 int rte_mem_check_dma_mask_thread_unsafe(uint8_t maskbits);
619 * @b EXPERIMENTAL: this API may change without prior notice
621 * Set dma mask to use once memory initialization is done. Previous functions
622 * rte_mem_check_dma_mask and rte_mem_check_dma_mask_thread_unsafe can not be
623 * used safely until memory has been initialized.
626 void rte_mem_set_dma_mask(uint8_t maskbits);
629 * Drivers based on uio will not load unless physical
630 * addresses are obtainable. It is only possible to get
631 * physical addresses when running as a privileged user.
634 * 1 if the system is able to obtain physical addresses.
635 * 0 if using DMA addresses through an IOMMU.
637 int rte_eal_using_phys_addrs(void);
641 * Enum indicating which kind of memory event has happened. Used by callbacks to
642 * distinguish between memory allocations and deallocations.
645 RTE_MEM_EVENT_ALLOC = 0, /**< Allocation event. */
646 RTE_MEM_EVENT_FREE, /**< Deallocation event. */
648 #define RTE_MEM_EVENT_CALLBACK_NAME_LEN 64
649 /**< maximum length of callback name */
652 * Function typedef used to register callbacks for memory events.
654 typedef void (*rte_mem_event_callback_t)(enum rte_mem_event event_type,
655 const void *addr, size_t len, void *arg);
658 * Function used to register callbacks for memory events.
660 * @note callbacks will happen while memory hotplug subsystem is write-locked,
661 * therefore some functions (e.g. `rte_memseg_walk()`) will cause a
662 * deadlock when called from within such callbacks.
664 * @note mem event callbacks not being supported is an expected error condition,
665 * so user code needs to handle this situation. In these cases, return
666 * value will be -1, and rte_errno will be set to ENOTSUP.
669 * Name associated with specified callback to be added to the list.
672 * Callback function pointer.
675 * Argument to pass to the callback.
678 * 0 on successful callback register
679 * -1 on unsuccessful callback register, with rte_errno value indicating
680 * reason for failure.
684 rte_mem_event_callback_register(const char *name, rte_mem_event_callback_t clb,
688 * Function used to unregister callbacks for memory events.
691 * Name associated with specified callback to be removed from the list.
694 * Argument to look for among callbacks with specified callback name.
697 * 0 on successful callback unregister
698 * -1 on unsuccessful callback unregister, with rte_errno value indicating
699 * reason for failure.
703 rte_mem_event_callback_unregister(const char *name, void *arg);
706 #define RTE_MEM_ALLOC_VALIDATOR_NAME_LEN 64
707 /**< maximum length of alloc validator name */
709 * Function typedef used to register memory allocation validation callbacks.
711 * Returning 0 will allow allocation attempt to continue. Returning -1 will
712 * prevent allocation from succeeding.
714 typedef int (*rte_mem_alloc_validator_t)(int socket_id,
715 size_t cur_limit, size_t new_len);
718 * @brief Register validator callback for memory allocations.
720 * Callbacks registered by this function will be called right before memory
721 * allocator is about to trigger allocation of more pages from the system if
722 * said allocation will bring total memory usage above specified limit on
723 * specified socket. User will be able to cancel pending allocation if callback
726 * @note callbacks will happen while memory hotplug subsystem is write-locked,
727 * therefore some functions (e.g. `rte_memseg_walk()`) will cause a
728 * deadlock when called from within such callbacks.
730 * @note validator callbacks not being supported is an expected error condition,
731 * so user code needs to handle this situation. In these cases, return
732 * value will be -1, and rte_errno will be set to ENOTSUP.
735 * Name associated with specified callback to be added to the list.
738 * Callback function pointer.
741 * Socket ID on which to watch for allocations.
744 * Limit above which to trigger callbacks.
747 * 0 on successful callback register
748 * -1 on unsuccessful callback register, with rte_errno value indicating
749 * reason for failure.
753 rte_mem_alloc_validator_register(const char *name,
754 rte_mem_alloc_validator_t clb, int socket_id, size_t limit);
757 * @brief Unregister validator callback for memory allocations.
760 * Name associated with specified callback to be removed from the list.
763 * Socket ID on which to watch for allocations.
766 * 0 on successful callback unregister
767 * -1 on unsuccessful callback unregister, with rte_errno value indicating
768 * reason for failure.
772 rte_mem_alloc_validator_unregister(const char *name, int socket_id);
778 #endif /* _RTE_MEMORY_H_ */