1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2014 Intel Corporation
11 * Memory-related RTE API.
22 #include <rte_bitops.h>
23 #include <rte_common.h>
24 #include <rte_compat.h>
25 #include <rte_config.h>
26 #include <rte_fbarray.h>
28 #define RTE_PGSIZE_4K (1ULL << 12)
29 #define RTE_PGSIZE_64K (1ULL << 16)
30 #define RTE_PGSIZE_256K (1ULL << 18)
31 #define RTE_PGSIZE_2M (1ULL << 21)
32 #define RTE_PGSIZE_16M (1ULL << 24)
33 #define RTE_PGSIZE_256M (1ULL << 28)
34 #define RTE_PGSIZE_512M (1ULL << 29)
35 #define RTE_PGSIZE_1G (1ULL << 30)
36 #define RTE_PGSIZE_4G (1ULL << 32)
37 #define RTE_PGSIZE_16G (1ULL << 34)
39 #define SOCKET_ID_ANY -1 /**< Any NUMA socket. */
41 /** Prevent this segment from being freed back to the OS. */
42 #define RTE_MEMSEG_FLAG_DO_NOT_FREE RTE_BIT32(0)
43 /** This segment is not filled with zeros. */
44 #define RTE_MEMSEG_FLAG_DIRTY RTE_BIT32(1)
47 * Physical memory segment descriptor.
50 rte_iova_t iova; /**< Start IO address. */
53 void *addr; /**< Start virtual address. */
54 uint64_t addr_64; /**< Makes sure addr is always 64 bits */
56 size_t len; /**< Length of the segment. */
57 uint64_t hugepage_sz; /**< The pagesize of underlying memory */
58 int32_t socket_id; /**< NUMA socket ID. */
59 uint32_t nchannel; /**< Number of channels. */
60 uint32_t nrank; /**< Number of ranks. */
61 uint32_t flags; /**< Memseg-specific flags */
65 * memseg list is a special case as we need to store a bunch of other data
66 * together with the array itself.
68 struct rte_memseg_list {
72 /**< Base virtual address for this memseg list. */
74 /**< Makes sure addr is always 64-bits */
76 uint64_t page_sz; /**< Page size for all memsegs in this list. */
77 int socket_id; /**< Socket ID for all memsegs in this list. */
78 volatile uint32_t version; /**< version number for multiprocess sync. */
79 size_t len; /**< Length of memory area covered by this memseg list. */
80 unsigned int external; /**< 1 if this list points to external memory */
81 unsigned int heap; /**< 1 if this list points to a heap */
82 struct rte_fbarray memseg_arr;
86 * Lock page in physical memory and prevent from swapping.
89 * The virtual address.
91 * 0 on success, negative on error.
93 int rte_mem_lock_page(const void *virt);
96 * Get physical address of any mapped virtual address in the current process.
97 * It is found by browsing the /proc/self/pagemap special file.
98 * The page must be locked.
101 * The virtual address.
103 * The physical address or RTE_BAD_IOVA on error.
105 phys_addr_t rte_mem_virt2phy(const void *virt);
108 * Get IO virtual address of any mapped virtual address in the current process.
110 * @note This function will not check internal page table. Instead, in IOVA as
111 * PA mode, it will fall back to getting real physical address (which may
112 * not match the expected IOVA, such as what was specified for external
116 * The virtual address.
118 * The IO address or RTE_BAD_IOVA on error.
120 rte_iova_t rte_mem_virt2iova(const void *virt);
123 * Get virtual memory address corresponding to iova address.
125 * @note This function read-locks the memory hotplug subsystem, and thus cannot
126 * be used within memory-related callback functions.
131 * Virtual address corresponding to iova address (or NULL if address does not
132 * exist within DPDK memory map).
135 rte_mem_iova2virt(rte_iova_t iova);
138 * Get memseg to which a particular virtual address belongs.
141 * The virtual address.
143 * The memseg list in which to look up based on ``virt`` address
146 * Memseg pointer on success, or NULL on error.
149 rte_mem_virt2memseg(const void *virt, const struct rte_memseg_list *msl);
152 * Get memseg list corresponding to virtual memory address.
155 * The virtual address.
157 * 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
214 rte_memseg_walk(rte_memseg_walk_t func, void *arg);
217 * Walk each VA-contiguous area.
219 * @note This function read-locks the memory hotplug subsystem, and thus cannot
220 * be used within memory-related callback functions.
222 * @note This function will also walk through externally allocated segments. It
223 * is up to the user to decide whether to skip through these segments.
228 * Argument passed to iterator
230 * 0 if walked over the entire list
231 * 1 if stopped by the user
232 * -1 if user function reported error
235 rte_memseg_contig_walk(rte_memseg_contig_walk_t func, void *arg);
238 * Walk each allocated memseg list.
240 * @note This function read-locks the memory hotplug subsystem, and thus cannot
241 * be used within memory-related callback functions.
243 * @note This function will also walk through externally allocated segments. It
244 * is up to the user to decide whether to skip through these segments.
249 * Argument passed to iterator
251 * 0 if walked over the entire list
252 * 1 if stopped by the user
253 * -1 if user function reported error
256 rte_memseg_list_walk(rte_memseg_list_walk_t func, void *arg);
259 * Walk list of all memsegs without performing any locking.
261 * @note This function does not perform any locking, and is only safe to call
262 * from within memory-related callback functions.
267 * Argument passed to iterator
269 * 0 if walked over the entire list
270 * 1 if stopped by the user
271 * -1 if user function reported error
274 rte_memseg_walk_thread_unsafe(rte_memseg_walk_t func, void *arg);
277 * Walk each VA-contiguous area without performing any locking.
279 * @note This function does not perform any locking, and is only safe to call
280 * from within memory-related callback functions.
285 * Argument passed to iterator
287 * 0 if walked over the entire list
288 * 1 if stopped by the user
289 * -1 if user function reported error
292 rte_memseg_contig_walk_thread_unsafe(rte_memseg_contig_walk_t func, void *arg);
295 * Walk each allocated memseg list without performing any locking.
297 * @note This function does not perform any locking, and is only safe to call
298 * from within memory-related callback functions.
303 * Argument passed to iterator
305 * 0 if walked over the entire list
306 * 1 if stopped by the user
307 * -1 if user function reported error
310 rte_memseg_list_walk_thread_unsafe(rte_memseg_list_walk_t func, void *arg);
313 * Return file descriptor associated with a particular memseg (if available).
315 * @note This function read-locks the memory hotplug subsystem, and thus cannot
316 * be used within memory-related callback functions.
318 * @note This returns an internal file descriptor. Performing any operations on
319 * this file descriptor is inherently dangerous, so it should be treated
320 * as read-only for all intents and purposes.
323 * A pointer to memseg for which to get file descriptor.
326 * Valid file descriptor in case of success.
327 * -1 in case of error, with ``rte_errno`` set to the following values:
328 * - EINVAL - ``ms`` pointer was NULL or did not point to a valid memseg
329 * - ENODEV - ``ms`` fd is not available
330 * - ENOENT - ``ms`` is an unused segment
331 * - ENOTSUP - segment fd's are not supported
334 rte_memseg_get_fd(const struct rte_memseg *ms);
337 * Return file descriptor associated with a particular memseg (if available).
339 * @note This function does not perform any locking, and is only safe to call
340 * from within memory-related callback functions.
342 * @note This returns an internal file descriptor. Performing any operations on
343 * this file descriptor is inherently dangerous, so it should be treated
344 * as read-only for all intents and purposes.
347 * A pointer to memseg for which to get file descriptor.
350 * Valid file descriptor in case of success.
351 * -1 in case of error, with ``rte_errno`` set to the following values:
352 * - EINVAL - ``ms`` pointer was NULL or did not point to a valid memseg
353 * - ENODEV - ``ms`` fd is not available
354 * - ENOENT - ``ms`` is an unused segment
355 * - ENOTSUP - segment fd's are not supported
358 rte_memseg_get_fd_thread_unsafe(const struct rte_memseg *ms);
361 * Get offset into segment file descriptor associated with a particular memseg
364 * @note This function read-locks the memory hotplug subsystem, and thus cannot
365 * be used within memory-related callback functions.
368 * A pointer to memseg for which to get file descriptor.
370 * A pointer to offset value where the result will be stored.
373 * Valid file descriptor in case of success.
374 * -1 in case of error, with ``rte_errno`` set to the following values:
375 * - EINVAL - ``ms`` pointer was NULL or did not point to a valid memseg
376 * - EINVAL - ``offset`` pointer was NULL
377 * - ENODEV - ``ms`` fd is not available
378 * - ENOENT - ``ms`` is an unused segment
379 * - ENOTSUP - segment fd's are not supported
382 rte_memseg_get_fd_offset(const struct rte_memseg *ms, size_t *offset);
385 * Get offset into segment file descriptor associated with a particular memseg
388 * @note This function does not perform any locking, and is only safe to call
389 * from within memory-related callback functions.
392 * A pointer to memseg for which to get file descriptor.
394 * A pointer to offset value where the result will be stored.
397 * Valid file descriptor in case of success.
398 * -1 in case of error, with ``rte_errno`` set to the following values:
399 * - EINVAL - ``ms`` pointer was NULL or did not point to a valid memseg
400 * - EINVAL - ``offset`` pointer was NULL
401 * - ENODEV - ``ms`` fd is not available
402 * - ENOENT - ``ms`` is an unused segment
403 * - ENOTSUP - segment fd's are not supported
406 rte_memseg_get_fd_offset_thread_unsafe(const struct rte_memseg *ms,
410 * Register external memory chunk with DPDK.
412 * @note Using this API is mutually exclusive with ``rte_malloc`` family of
415 * @note This API will not perform any DMA mapping. It is expected that user
416 * will do that themselves.
418 * @note Before accessing this memory in other processes, it needs to be
419 * attached in each of those processes by calling ``rte_extmem_attach`` in
420 * each other process.
423 * Start of virtual area to register. Must be aligned by ``page_sz``.
425 * Length of virtual area to register. Must be aligned by ``page_sz``.
427 * Array of page IOVA addresses corresponding to each page in this memory
428 * area. Can be NULL, in which case page IOVA addresses will be set to
431 * Number of elements in the iova_addrs array. Ignored if ``iova_addrs``
434 * Page size of the underlying memory
438 * - -1 in case of error, with rte_errno set to one of the following:
439 * EINVAL - one of the parameters was invalid
440 * EEXIST - memory chunk is already registered
441 * ENOSPC - no more space in internal config to store a new memory chunk
444 rte_extmem_register(void *va_addr, size_t len, rte_iova_t iova_addrs[],
445 unsigned int n_pages, size_t page_sz);
448 * Unregister external memory chunk with DPDK.
450 * @note Using this API is mutually exclusive with ``rte_malloc`` family of
453 * @note This API will not perform any DMA unmapping. It is expected that user
454 * will do that themselves.
456 * @note Before calling this function, all other processes must call
457 * ``rte_extmem_detach`` to detach from the memory area.
460 * Start of virtual area to unregister
462 * Length of virtual area to unregister
466 * - -1 in case of error, with rte_errno set to one of the following:
467 * EINVAL - one of the parameters was invalid
468 * ENOENT - memory chunk was not found
471 rte_extmem_unregister(void *va_addr, size_t len);
474 * Attach to external memory chunk registered in another process.
476 * @note Using this API is mutually exclusive with ``rte_malloc`` family of
479 * @note This API will not perform any DMA mapping. It is expected that user
480 * will do that themselves.
483 * Start of virtual area to register
485 * Length of virtual area to register
489 * - -1 in case of error, with rte_errno set to one of the following:
490 * EINVAL - one of the parameters was invalid
491 * ENOENT - memory chunk was not found
494 rte_extmem_attach(void *va_addr, size_t len);
497 * Detach from external memory chunk registered in another process.
499 * @note Using this API is mutually exclusive with ``rte_malloc`` family of
502 * @note This API will not perform any DMA unmapping. It is expected that user
503 * will do that themselves.
506 * Start of virtual area to unregister
508 * Length of virtual area to unregister
512 * - -1 in case of error, with rte_errno set to one of the following:
513 * EINVAL - one of the parameters was invalid
514 * ENOENT - memory chunk was not found
517 rte_extmem_detach(void *va_addr, size_t len);
520 * Dump the physical memory layout to a file.
522 * @note This function read-locks the memory hotplug subsystem, and thus cannot
523 * be used within memory-related callback functions.
526 * A pointer to a file for output
528 void rte_dump_physmem_layout(FILE *f);
531 * Get the total amount of available physical memory.
533 * @note This function read-locks the memory hotplug subsystem, and thus cannot
534 * be used within memory-related callback functions.
537 * The total amount of available physical memory in bytes.
539 uint64_t rte_eal_get_physmem_size(void);
542 * Get the number of memory channels.
545 * The number of memory channels on the system. The value is 0 if unknown
546 * or not the same on all devices.
548 unsigned rte_memory_get_nchannel(void);
551 * Get the number of memory ranks.
554 * The number of memory ranks on the system. The value is 0 if unknown or
555 * not the same on all devices.
557 unsigned rte_memory_get_nrank(void);
560 * Check if all currently allocated memory segments are compliant with
561 * supplied DMA address width.
564 * Address width to check against.
566 int rte_mem_check_dma_mask(uint8_t maskbits);
569 * Check if all currently allocated memory segments are compliant with
570 * supplied DMA address width. This function will use
571 * rte_memseg_walk_thread_unsafe instead of rte_memseg_walk implying
572 * memory_hotplug_lock will not be acquired avoiding deadlock during
573 * memory initialization.
575 * This function is just for EAL core memory internal use. Drivers should
576 * use the previous rte_mem_check_dma_mask.
579 * Address width to check against.
581 int rte_mem_check_dma_mask_thread_unsafe(uint8_t maskbits);
584 * Set dma mask to use once memory initialization is done. Previous functions
585 * rte_mem_check_dma_mask and rte_mem_check_dma_mask_thread_unsafe can not be
586 * used safely until memory has been initialized.
588 void rte_mem_set_dma_mask(uint8_t maskbits);
591 * Drivers based on uio will not load unless physical
592 * addresses are obtainable. It is only possible to get
593 * physical addresses when running as a privileged user.
596 * 1 if the system is able to obtain physical addresses.
597 * 0 if using DMA addresses through an IOMMU.
599 int rte_eal_using_phys_addrs(void);
603 * Enum indicating which kind of memory event has happened. Used by callbacks to
604 * distinguish between memory allocations and deallocations.
607 RTE_MEM_EVENT_ALLOC = 0, /**< Allocation event. */
608 RTE_MEM_EVENT_FREE, /**< Deallocation event. */
610 #define RTE_MEM_EVENT_CALLBACK_NAME_LEN 64
611 /**< maximum length of callback name */
614 * Function typedef used to register callbacks for memory events.
616 typedef void (*rte_mem_event_callback_t)(enum rte_mem_event event_type,
617 const void *addr, size_t len, void *arg);
620 * Function used to register callbacks for memory events.
622 * @note callbacks will happen while memory hotplug subsystem is write-locked,
623 * therefore some functions (e.g. `rte_memseg_walk()`) will cause a
624 * deadlock when called from within such callbacks.
626 * @note mem event callbacks not being supported is an expected error condition,
627 * so user code needs to handle this situation. In these cases, return
628 * value will be -1, and rte_errno will be set to ENOTSUP.
631 * Name associated with specified callback to be added to the list.
634 * Callback function pointer.
637 * Argument to pass to the callback.
640 * 0 on successful callback register
641 * -1 on unsuccessful callback register, with rte_errno value indicating
642 * reason for failure.
645 rte_mem_event_callback_register(const char *name, rte_mem_event_callback_t clb,
649 * Function used to unregister callbacks for memory events.
652 * Name associated with specified callback to be removed from the list.
655 * Argument to look for among callbacks with specified callback name.
658 * 0 on successful callback unregister
659 * -1 on unsuccessful callback unregister, with rte_errno value indicating
660 * reason for failure.
663 rte_mem_event_callback_unregister(const char *name, void *arg);
666 #define RTE_MEM_ALLOC_VALIDATOR_NAME_LEN 64
667 /**< maximum length of alloc validator name */
669 * Function typedef used to register memory allocation validation callbacks.
671 * Returning 0 will allow allocation attempt to continue. Returning -1 will
672 * prevent allocation from succeeding.
674 typedef int (*rte_mem_alloc_validator_t)(int socket_id,
675 size_t cur_limit, size_t new_len);
678 * @brief Register validator callback for memory allocations.
680 * Callbacks registered by this function will be called right before memory
681 * allocator is about to trigger allocation of more pages from the system if
682 * said allocation will bring total memory usage above specified limit on
683 * specified socket. User will be able to cancel pending allocation if callback
686 * @note callbacks will happen while memory hotplug subsystem is write-locked,
687 * therefore some functions (e.g. `rte_memseg_walk()`) will cause a
688 * deadlock when called from within such callbacks.
690 * @note validator callbacks not being supported is an expected error condition,
691 * so user code needs to handle this situation. In these cases, return
692 * value will be -1, and rte_errno will be set to ENOTSUP.
695 * Name associated with specified callback to be added to the list.
698 * Callback function pointer.
701 * Socket ID on which to watch for allocations.
704 * Limit above which to trigger callbacks.
707 * 0 on successful callback register
708 * -1 on unsuccessful callback register, with rte_errno value indicating
709 * reason for failure.
712 rte_mem_alloc_validator_register(const char *name,
713 rte_mem_alloc_validator_t clb, int socket_id, size_t limit);
716 * @brief Unregister validator callback for memory allocations.
719 * Name associated with specified callback to be removed from the list.
722 * Socket ID on which to watch for allocations.
725 * 0 on successful callback unregister
726 * -1 on unsuccessful callback unregister, with rte_errno value indicating
727 * reason for failure.
730 rte_mem_alloc_validator_unregister(const char *name, int socket_id);
736 #endif /* _RTE_MEMORY_H_ */