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 unsigned int heap; /**< 1 if this list points to a heap */
85 struct rte_fbarray memseg_arr;
89 * Lock page in physical memory and prevent from swapping.
92 * The virtual address.
94 * 0 on success, negative on error.
96 int rte_mem_lock_page(const void *virt);
99 * Get physical address of any mapped virtual address in the current process.
100 * It is found by browsing the /proc/self/pagemap special file.
101 * The page must be locked.
104 * The virtual address.
106 * The physical address or RTE_BAD_IOVA on error.
108 phys_addr_t rte_mem_virt2phy(const void *virt);
111 * Get IO virtual address of any mapped virtual address in the current process.
114 * The virtual address.
116 * The IO address or RTE_BAD_IOVA on error.
118 rte_iova_t rte_mem_virt2iova(const void *virt);
121 * Get virtual memory address corresponding to iova address.
123 * @note This function read-locks the memory hotplug subsystem, and thus cannot
124 * be used within memory-related callback functions.
129 * Virtual address corresponding to iova address (or NULL if address does not
130 * exist within DPDK memory map).
134 rte_mem_iova2virt(rte_iova_t iova);
137 * Get memseg to which a particular virtual address belongs.
140 * The virtual address.
142 * The memseg list in which to look up based on ``virt`` address
145 * 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.
160 struct rte_memseg_list *
161 rte_mem_virt2memseg_list(const void *virt);
164 * Memseg walk function prototype.
166 * Returning 0 will continue walk
167 * Returning 1 will stop the walk
168 * Returning -1 will stop the walk and report error
170 typedef int (*rte_memseg_walk_t)(const struct rte_memseg_list *msl,
171 const struct rte_memseg *ms, void *arg);
174 * Memseg contig walk function prototype. This will trigger a callback on every
175 * VA-contiguous area starting at memseg ``ms``, so total valid VA space at each
176 * callback call will be [``ms->addr``, ``ms->addr + len``).
178 * Returning 0 will continue walk
179 * Returning 1 will stop the walk
180 * Returning -1 will stop the walk and report error
182 typedef int (*rte_memseg_contig_walk_t)(const struct rte_memseg_list *msl,
183 const struct rte_memseg *ms, size_t len, void *arg);
186 * Memseg list walk function prototype. This will trigger a callback on every
187 * allocated memseg list.
189 * Returning 0 will continue walk
190 * Returning 1 will stop the walk
191 * Returning -1 will stop the walk and report error
193 typedef int (*rte_memseg_list_walk_t)(const struct rte_memseg_list *msl,
197 * Walk list of all memsegs.
199 * @note This function read-locks the memory hotplug subsystem, and thus cannot
200 * be used within memory-related callback functions.
202 * @note This function will also walk through externally allocated segments. It
203 * is up to the user to decide whether to skip through these segments.
208 * Argument passed to iterator
210 * 0 if walked over the entire list
211 * 1 if stopped by the user
212 * -1 if user function reported error
216 rte_memseg_walk(rte_memseg_walk_t func, void *arg);
219 * Walk each VA-contiguous area.
221 * @note This function read-locks the memory hotplug subsystem, and thus cannot
222 * be used within memory-related callback functions.
224 * @note This function will also walk through externally allocated segments. It
225 * is up to the user to decide whether to skip through these segments.
230 * Argument passed to iterator
232 * 0 if walked over the entire list
233 * 1 if stopped by the user
234 * -1 if user function reported error
238 rte_memseg_contig_walk(rte_memseg_contig_walk_t func, void *arg);
241 * Walk each allocated memseg list.
243 * @note This function read-locks the memory hotplug subsystem, and thus cannot
244 * be used within memory-related callback functions.
246 * @note This function will also walk through externally allocated segments. It
247 * is up to the user to decide whether to skip through these segments.
252 * Argument passed to iterator
254 * 0 if walked over the entire list
255 * 1 if stopped by the user
256 * -1 if user function reported error
260 rte_memseg_list_walk(rte_memseg_list_walk_t func, void *arg);
263 * Walk list of all memsegs without performing any locking.
265 * @note This function does not perform any locking, and is only safe to call
266 * from within memory-related callback functions.
271 * Argument passed to iterator
273 * 0 if walked over the entire list
274 * 1 if stopped by the user
275 * -1 if user function reported error
279 rte_memseg_walk_thread_unsafe(rte_memseg_walk_t func, void *arg);
282 * Walk each VA-contiguous area without performing any locking.
284 * @note This function does not perform any locking, and is only safe to call
285 * from within memory-related callback functions.
290 * Argument passed to iterator
292 * 0 if walked over the entire list
293 * 1 if stopped by the user
294 * -1 if user function reported error
298 rte_memseg_contig_walk_thread_unsafe(rte_memseg_contig_walk_t func, void *arg);
301 * Walk each allocated memseg list without performing any locking.
303 * @note This function does not perform any locking, and is only safe to call
304 * from within memory-related callback functions.
309 * Argument passed to iterator
311 * 0 if walked over the entire list
312 * 1 if stopped by the user
313 * -1 if user function reported error
317 rte_memseg_list_walk_thread_unsafe(rte_memseg_list_walk_t func, void *arg);
320 * Return file descriptor associated with a particular memseg (if available).
322 * @note This function read-locks the memory hotplug subsystem, and thus cannot
323 * be used within memory-related callback functions.
325 * @note This returns an internal file descriptor. Performing any operations on
326 * this file descriptor is inherently dangerous, so it should be treated
327 * as read-only for all intents and purposes.
330 * A pointer to memseg for which to get file descriptor.
333 * Valid file descriptor in case of success.
334 * -1 in case of error, with ``rte_errno`` set to the following values:
335 * - EINVAL - ``ms`` pointer was NULL or did not point to a valid memseg
336 * - ENODEV - ``ms`` fd is not available
337 * - ENOENT - ``ms`` is an unused segment
338 * - ENOTSUP - segment fd's are not supported
342 rte_memseg_get_fd(const struct rte_memseg *ms);
345 * Return file descriptor associated with a particular memseg (if available).
347 * @note This function does not perform any locking, and is only safe to call
348 * from within memory-related callback functions.
350 * @note This returns an internal file descriptor. Performing any operations on
351 * this file descriptor is inherently dangerous, so it should be treated
352 * as read-only for all intents and purposes.
355 * A pointer to memseg for which to get file descriptor.
358 * Valid file descriptor in case of success.
359 * -1 in case of error, with ``rte_errno`` set to the following values:
360 * - EINVAL - ``ms`` pointer was NULL or did not point to a valid memseg
361 * - ENODEV - ``ms`` fd is not available
362 * - ENOENT - ``ms`` is an unused segment
363 * - ENOTSUP - segment fd's are not supported
367 rte_memseg_get_fd_thread_unsafe(const struct rte_memseg *ms);
370 * Get offset into segment file descriptor associated with a particular memseg
373 * @note This function read-locks the memory hotplug subsystem, and thus cannot
374 * be used within memory-related callback functions.
377 * A pointer to memseg for which to get file descriptor.
379 * A pointer to offset value where the result will be stored.
382 * Valid file descriptor in case of success.
383 * -1 in case of error, with ``rte_errno`` set to the following values:
384 * - EINVAL - ``ms`` pointer was NULL or did not point to a valid memseg
385 * - EINVAL - ``offset`` pointer was NULL
386 * - ENODEV - ``ms`` fd is not available
387 * - ENOENT - ``ms`` is an unused segment
388 * - ENOTSUP - segment fd's are not supported
392 rte_memseg_get_fd_offset(const struct rte_memseg *ms, size_t *offset);
395 * Get offset into segment file descriptor associated with a particular memseg
398 * @note This function does not perform any locking, and is only safe to call
399 * from within memory-related callback functions.
402 * A pointer to memseg for which to get file descriptor.
404 * A pointer to offset value where the result will be stored.
407 * Valid file descriptor in case of success.
408 * -1 in case of error, with ``rte_errno`` set to the following values:
409 * - EINVAL - ``ms`` pointer was NULL or did not point to a valid memseg
410 * - EINVAL - ``offset`` pointer was NULL
411 * - ENODEV - ``ms`` fd is not available
412 * - ENOENT - ``ms`` is an unused segment
413 * - ENOTSUP - segment fd's are not supported
417 rte_memseg_get_fd_offset_thread_unsafe(const struct rte_memseg *ms,
422 * @b EXPERIMENTAL: this API may change without prior notice
424 * Register external memory chunk with DPDK.
426 * @note Using this API is mutually exclusive with ``rte_malloc`` family of
429 * @note This API will not perform any DMA mapping. It is expected that user
430 * will do that themselves.
432 * @note Before accessing this memory in other processes, it needs to be
433 * attached in each of those processes by calling ``rte_extmem_attach`` in
434 * each other process.
437 * Start of virtual area to register. Must be aligned by ``page_sz``.
439 * Length of virtual area to register. Must be aligned by ``page_sz``.
441 * Array of page IOVA addresses corresponding to each page in this memory
442 * area. Can be NULL, in which case page IOVA addresses will be set to
445 * Number of elements in the iova_addrs array. Ignored if ``iova_addrs``
448 * Page size of the underlying memory
452 * - -1 in case of error, with rte_errno set to one of the following:
453 * EINVAL - one of the parameters was invalid
454 * EEXIST - memory chunk is already registered
455 * ENOSPC - no more space in internal config to store a new memory chunk
459 rte_extmem_register(void *va_addr, size_t len, rte_iova_t iova_addrs[],
460 unsigned int n_pages, size_t page_sz);
464 * @b EXPERIMENTAL: this API may change without prior notice
466 * Unregister external memory chunk with DPDK.
468 * @note Using this API is mutually exclusive with ``rte_malloc`` family of
471 * @note This API will not perform any DMA unmapping. It is expected that user
472 * will do that themselves.
474 * @note Before calling this function, all other processes must call
475 * ``rte_extmem_detach`` to detach from the memory area.
478 * Start of virtual area to unregister
480 * Length of virtual area to unregister
484 * - -1 in case of error, with rte_errno set to one of the following:
485 * EINVAL - one of the parameters was invalid
486 * ENOENT - memory chunk was not found
490 rte_extmem_unregister(void *va_addr, size_t len);
494 * @b EXPERIMENTAL: this API may change without prior notice
496 * Attach to external memory chunk registered in another process.
498 * @note Using this API is mutually exclusive with ``rte_malloc`` family of
501 * @note This API will not perform any DMA mapping. It is expected that user
502 * will do that themselves.
505 * Start of virtual area to register
507 * Length of virtual area to register
511 * - -1 in case of error, with rte_errno set to one of the following:
512 * EINVAL - one of the parameters was invalid
513 * ENOENT - memory chunk was not found
517 rte_extmem_attach(void *va_addr, size_t len);
521 * @b EXPERIMENTAL: this API may change without prior notice
523 * Detach from external memory chunk registered in another process.
525 * @note Using this API is mutually exclusive with ``rte_malloc`` family of
528 * @note This API will not perform any DMA unmapping. It is expected that user
529 * will do that themselves.
532 * Start of virtual area to unregister
534 * Length of virtual area to unregister
538 * - -1 in case of error, with rte_errno set to one of the following:
539 * EINVAL - one of the parameters was invalid
540 * ENOENT - memory chunk was not found
544 rte_extmem_detach(void *va_addr, size_t len);
547 * Dump the physical memory layout to a file.
549 * @note This function read-locks the memory hotplug subsystem, and thus cannot
550 * be used within memory-related callback functions.
553 * A pointer to a file for output
555 void rte_dump_physmem_layout(FILE *f);
558 * Get the total amount of available physical memory.
560 * @note This function read-locks the memory hotplug subsystem, and thus cannot
561 * be used within memory-related callback functions.
564 * The total amount of available physical memory in bytes.
566 uint64_t rte_eal_get_physmem_size(void);
569 * Get the number of memory channels.
572 * The number of memory channels on the system. The value is 0 if unknown
573 * or not the same on all devices.
575 unsigned rte_memory_get_nchannel(void);
578 * Get the number of memory ranks.
581 * The number of memory ranks on the system. The value is 0 if unknown or
582 * not the same on all devices.
584 unsigned rte_memory_get_nrank(void);
588 * @b EXPERIMENTAL: this API may change without prior notice
590 * Check if all currently allocated memory segments are compliant with
591 * supplied DMA address width.
594 * Address width to check against.
597 int rte_mem_check_dma_mask(uint8_t maskbits);
601 * @b EXPERIMENTAL: this API may change without prior notice
603 * Check if all currently allocated memory segments are compliant with
604 * supplied DMA address width. This function will use
605 * rte_memseg_walk_thread_unsafe instead of rte_memseg_walk implying
606 * memory_hotplug_lock will not be acquired avoiding deadlock during
607 * memory initialization.
609 * This function is just for EAL core memory internal use. Drivers should
610 * use the previous rte_mem_check_dma_mask.
613 * Address width to check against.
616 int rte_mem_check_dma_mask_thread_unsafe(uint8_t maskbits);
620 * @b EXPERIMENTAL: this API may change without prior notice
622 * Set dma mask to use once memory initialization is done. Previous functions
623 * rte_mem_check_dma_mask and rte_mem_check_dma_mask_thread_unsafe can not be
624 * used safely until memory has been initialized.
627 void rte_mem_set_dma_mask(uint8_t maskbits);
630 * Drivers based on uio will not load unless physical
631 * addresses are obtainable. It is only possible to get
632 * physical addresses when running as a privileged user.
635 * 1 if the system is able to obtain physical addresses.
636 * 0 if using DMA addresses through an IOMMU.
638 int rte_eal_using_phys_addrs(void);
642 * Enum indicating which kind of memory event has happened. Used by callbacks to
643 * distinguish between memory allocations and deallocations.
646 RTE_MEM_EVENT_ALLOC = 0, /**< Allocation event. */
647 RTE_MEM_EVENT_FREE, /**< Deallocation event. */
649 #define RTE_MEM_EVENT_CALLBACK_NAME_LEN 64
650 /**< maximum length of callback name */
653 * Function typedef used to register callbacks for memory events.
655 typedef void (*rte_mem_event_callback_t)(enum rte_mem_event event_type,
656 const void *addr, size_t len, void *arg);
659 * Function used to register callbacks for memory events.
661 * @note callbacks will happen while memory hotplug subsystem is write-locked,
662 * therefore some functions (e.g. `rte_memseg_walk()`) will cause a
663 * deadlock when called from within such callbacks.
665 * @note mem event callbacks not being supported is an expected error condition,
666 * so user code needs to handle this situation. In these cases, return
667 * value will be -1, and rte_errno will be set to ENOTSUP.
670 * Name associated with specified callback to be added to the list.
673 * Callback function pointer.
676 * Argument to pass to the callback.
679 * 0 on successful callback register
680 * -1 on unsuccessful callback register, with rte_errno value indicating
681 * reason for failure.
685 rte_mem_event_callback_register(const char *name, rte_mem_event_callback_t clb,
689 * Function used to unregister callbacks for memory events.
692 * Name associated with specified callback to be removed from the list.
695 * Argument to look for among callbacks with specified callback name.
698 * 0 on successful callback unregister
699 * -1 on unsuccessful callback unregister, with rte_errno value indicating
700 * reason for failure.
704 rte_mem_event_callback_unregister(const char *name, void *arg);
707 #define RTE_MEM_ALLOC_VALIDATOR_NAME_LEN 64
708 /**< maximum length of alloc validator name */
710 * Function typedef used to register memory allocation validation callbacks.
712 * Returning 0 will allow allocation attempt to continue. Returning -1 will
713 * prevent allocation from succeeding.
715 typedef int (*rte_mem_alloc_validator_t)(int socket_id,
716 size_t cur_limit, size_t new_len);
719 * @brief Register validator callback for memory allocations.
721 * Callbacks registered by this function will be called right before memory
722 * allocator is about to trigger allocation of more pages from the system if
723 * said allocation will bring total memory usage above specified limit on
724 * specified socket. User will be able to cancel pending allocation if callback
727 * @note callbacks will happen while memory hotplug subsystem is write-locked,
728 * therefore some functions (e.g. `rte_memseg_walk()`) will cause a
729 * deadlock when called from within such callbacks.
731 * @note validator callbacks not being supported is an expected error condition,
732 * so user code needs to handle this situation. In these cases, return
733 * value will be -1, and rte_errno will be set to ENOTSUP.
736 * Name associated with specified callback to be added to the list.
739 * Callback function pointer.
742 * Socket ID on which to watch for allocations.
745 * Limit above which to trigger callbacks.
748 * 0 on successful callback register
749 * -1 on unsuccessful callback register, with rte_errno value indicating
750 * reason for failure.
754 rte_mem_alloc_validator_register(const char *name,
755 rte_mem_alloc_validator_t clb, int socket_id, size_t limit);
758 * @brief Unregister validator callback for memory allocations.
761 * Name associated with specified callback to be removed from the list.
764 * Socket ID on which to watch for allocations.
767 * 0 on successful callback unregister
768 * -1 on unsuccessful callback unregister, with rte_errno value indicating
769 * reason for failure.
773 rte_mem_alloc_validator_unregister(const char *name, int socket_id);
779 #endif /* _RTE_MEMORY_H_ */