mem: add validator callback
[dpdk.git] / lib / librte_eal / common / include / rte_memory.h
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2014 Intel Corporation
3  */
4
5 #ifndef _RTE_MEMORY_H_
6 #define _RTE_MEMORY_H_
7
8 /**
9  * @file
10  *
11  * Memory-related RTE API.
12  */
13
14 #include <stdint.h>
15 #include <stddef.h>
16 #include <stdio.h>
17
18 #ifdef __cplusplus
19 extern "C" {
20 #endif
21
22 #include <rte_common.h>
23 #include <rte_compat.h>
24 #include <rte_config.h>
25
26 /* forward declaration for pointers */
27 struct rte_memseg_list;
28
29 __extension__
30 enum rte_page_sizes {
31         RTE_PGSIZE_4K    = 1ULL << 12,
32         RTE_PGSIZE_64K   = 1ULL << 16,
33         RTE_PGSIZE_256K  = 1ULL << 18,
34         RTE_PGSIZE_2M    = 1ULL << 21,
35         RTE_PGSIZE_16M   = 1ULL << 24,
36         RTE_PGSIZE_256M  = 1ULL << 28,
37         RTE_PGSIZE_512M  = 1ULL << 29,
38         RTE_PGSIZE_1G    = 1ULL << 30,
39         RTE_PGSIZE_4G    = 1ULL << 32,
40         RTE_PGSIZE_16G   = 1ULL << 34,
41 };
42
43 #define SOCKET_ID_ANY -1                    /**< Any NUMA socket. */
44 #define RTE_CACHE_LINE_MASK (RTE_CACHE_LINE_SIZE-1) /**< Cache line mask. */
45
46 #define RTE_CACHE_LINE_ROUNDUP(size) \
47         (RTE_CACHE_LINE_SIZE * ((size + RTE_CACHE_LINE_SIZE - 1) / RTE_CACHE_LINE_SIZE))
48 /**< Return the first cache-aligned value greater or equal to size. */
49
50 /**< Cache line size in terms of log2 */
51 #if RTE_CACHE_LINE_SIZE == 64
52 #define RTE_CACHE_LINE_SIZE_LOG2 6
53 #elif RTE_CACHE_LINE_SIZE == 128
54 #define RTE_CACHE_LINE_SIZE_LOG2 7
55 #else
56 #error "Unsupported cache line size"
57 #endif
58
59 #define RTE_CACHE_LINE_MIN_SIZE 64      /**< Minimum Cache line size. */
60
61 /**
62  * Force alignment to cache line.
63  */
64 #define __rte_cache_aligned __rte_aligned(RTE_CACHE_LINE_SIZE)
65
66 /**
67  * Force minimum cache line alignment.
68  */
69 #define __rte_cache_min_aligned __rte_aligned(RTE_CACHE_LINE_MIN_SIZE)
70
71 typedef uint64_t phys_addr_t; /**< Physical address. */
72 #define RTE_BAD_PHYS_ADDR ((phys_addr_t)-1)
73 /**
74  * IO virtual address type.
75  * When the physical addressing mode (IOVA as PA) is in use,
76  * the translation from an IO virtual address (IOVA) to a physical address
77  * is a direct mapping, i.e. the same value.
78  * Otherwise, in virtual mode (IOVA as VA), an IOMMU may do the translation.
79  */
80 typedef uint64_t rte_iova_t;
81 #define RTE_BAD_IOVA ((rte_iova_t)-1)
82
83 /**
84  * Physical memory segment descriptor.
85  */
86 struct rte_memseg {
87         RTE_STD_C11
88         union {
89                 phys_addr_t phys_addr;  /**< deprecated - Start physical address. */
90                 rte_iova_t iova;        /**< Start IO address. */
91         };
92         RTE_STD_C11
93         union {
94                 void *addr;         /**< Start virtual address. */
95                 uint64_t addr_64;   /**< Makes sure addr is always 64 bits */
96         };
97         size_t len;               /**< Length of the segment. */
98         uint64_t hugepage_sz;       /**< The pagesize of underlying memory */
99         int32_t socket_id;          /**< NUMA socket ID. */
100         uint32_t nchannel;          /**< Number of channels. */
101         uint32_t nrank;             /**< Number of ranks. */
102 } __rte_packed;
103
104 /**
105  * Lock page in physical memory and prevent from swapping.
106  *
107  * @param virt
108  *   The virtual address.
109  * @return
110  *   0 on success, negative on error.
111  */
112 int rte_mem_lock_page(const void *virt);
113
114 /**
115  * Get physical address of any mapped virtual address in the current process.
116  * It is found by browsing the /proc/self/pagemap special file.
117  * The page must be locked.
118  *
119  * @param virt
120  *   The virtual address.
121  * @return
122  *   The physical address or RTE_BAD_IOVA on error.
123  */
124 phys_addr_t rte_mem_virt2phy(const void *virt);
125
126 /**
127  * Get IO virtual address of any mapped virtual address in the current process.
128  *
129  * @param virt
130  *   The virtual address.
131  * @return
132  *   The IO address or RTE_BAD_IOVA on error.
133  */
134 rte_iova_t rte_mem_virt2iova(const void *virt);
135
136 /**
137  * Get virtual memory address corresponding to iova address.
138  *
139  * @note This function read-locks the memory hotplug subsystem, and thus cannot
140  *       be used within memory-related callback functions.
141  *
142  * @param iova
143  *   The iova address.
144  * @return
145  *   Virtual address corresponding to iova address (or NULL if address does not
146  *   exist within DPDK memory map).
147  */
148 __rte_experimental void *
149 rte_mem_iova2virt(rte_iova_t iova);
150
151 /**
152  * Get memseg to which a particular virtual address belongs.
153  *
154  * @param virt
155  *   The virtual address.
156  * @param msl
157  *   The memseg list in which to look up based on ``virt`` address
158  *   (can be NULL).
159  * @return
160  *   Memseg pointer on success, or NULL on error.
161  */
162 __rte_experimental struct rte_memseg *
163 rte_mem_virt2memseg(const void *virt, const struct rte_memseg_list *msl);
164
165 /**
166  * Get memseg list corresponding to virtual memory address.
167  *
168  * @param virt
169  *   The virtual address.
170  * @return
171  *   Memseg list to which this virtual address belongs to.
172  */
173 __rte_experimental struct rte_memseg_list *
174 rte_mem_virt2memseg_list(const void *virt);
175
176 /**
177  * Memseg walk function prototype.
178  *
179  * Returning 0 will continue walk
180  * Returning 1 will stop the walk
181  * Returning -1 will stop the walk and report error
182  */
183 typedef int (*rte_memseg_walk_t)(const struct rte_memseg_list *msl,
184                 const struct rte_memseg *ms, void *arg);
185
186 /**
187  * Memseg contig walk function prototype. This will trigger a callback on every
188  * VA-contiguous are starting at memseg ``ms``, so total valid VA space at each
189  * callback call will be [``ms->addr``, ``ms->addr + len``).
190  *
191  * Returning 0 will continue walk
192  * Returning 1 will stop the walk
193  * Returning -1 will stop the walk and report error
194  */
195 typedef int (*rte_memseg_contig_walk_t)(const struct rte_memseg_list *msl,
196                 const struct rte_memseg *ms, size_t len, void *arg);
197
198 /**
199  * Memseg list walk function prototype. This will trigger a callback on every
200  * allocated memseg list.
201  *
202  * Returning 0 will continue walk
203  * Returning 1 will stop the walk
204  * Returning -1 will stop the walk and report error
205  */
206 typedef int (*rte_memseg_list_walk_t)(const struct rte_memseg_list *msl,
207                 void *arg);
208
209 /**
210  * Walk list of all memsegs.
211  *
212  * @note This function read-locks the memory hotplug subsystem, and thus cannot
213  *       be used within memory-related callback functions.
214  *
215  * @param func
216  *   Iterator function
217  * @param arg
218  *   Argument passed to iterator
219  * @return
220  *   0 if walked over the entire list
221  *   1 if stopped by the user
222  *   -1 if user function reported error
223  */
224 int __rte_experimental
225 rte_memseg_walk(rte_memseg_walk_t func, void *arg);
226
227 /**
228  * Walk each VA-contiguous area.
229  *
230  * @note This function read-locks the memory hotplug subsystem, and thus cannot
231  *       be used within memory-related callback functions.
232  *
233  * @param func
234  *   Iterator function
235  * @param arg
236  *   Argument passed to iterator
237  * @return
238  *   0 if walked over the entire list
239  *   1 if stopped by the user
240  *   -1 if user function reported error
241  */
242 int __rte_experimental
243 rte_memseg_contig_walk(rte_memseg_contig_walk_t func, void *arg);
244
245 /**
246  * Walk each allocated memseg list.
247  *
248  * @note This function read-locks the memory hotplug subsystem, and thus cannot
249  *       be used within memory-related callback functions.
250  *
251  * @param func
252  *   Iterator function
253  * @param arg
254  *   Argument passed to iterator
255  * @return
256  *   0 if walked over the entire list
257  *   1 if stopped by the user
258  *   -1 if user function reported error
259  */
260 int __rte_experimental
261 rte_memseg_list_walk(rte_memseg_list_walk_t func, void *arg);
262
263 /**
264  * Dump the physical memory layout to a file.
265  *
266  * @note This function read-locks the memory hotplug subsystem, and thus cannot
267  *       be used within memory-related callback functions.
268  *
269  * @param f
270  *   A pointer to a file for output
271  */
272 void rte_dump_physmem_layout(FILE *f);
273
274 /**
275  * Get the total amount of available physical memory.
276  *
277  * @note This function read-locks the memory hotplug subsystem, and thus cannot
278  *       be used within memory-related callback functions.
279  *
280  * @return
281  *    The total amount of available physical memory in bytes.
282  */
283 uint64_t rte_eal_get_physmem_size(void);
284
285 /**
286  * Get the number of memory channels.
287  *
288  * @return
289  *   The number of memory channels on the system. The value is 0 if unknown
290  *   or not the same on all devices.
291  */
292 unsigned rte_memory_get_nchannel(void);
293
294 /**
295  * Get the number of memory ranks.
296  *
297  * @return
298  *   The number of memory ranks on the system. The value is 0 if unknown or
299  *   not the same on all devices.
300  */
301 unsigned rte_memory_get_nrank(void);
302
303 /**
304  * Drivers based on uio will not load unless physical
305  * addresses are obtainable. It is only possible to get
306  * physical addresses when running as a privileged user.
307  *
308  * @return
309  *   1 if the system is able to obtain physical addresses.
310  *   0 if using DMA addresses through an IOMMU.
311  */
312 int rte_eal_using_phys_addrs(void);
313
314
315 /**
316  * Enum indicating which kind of memory event has happened. Used by callbacks to
317  * distinguish between memory allocations and deallocations.
318  */
319 enum rte_mem_event {
320         RTE_MEM_EVENT_ALLOC = 0, /**< Allocation event. */
321         RTE_MEM_EVENT_FREE,      /**< Deallocation event. */
322 };
323 #define RTE_MEM_EVENT_CALLBACK_NAME_LEN 64
324 /**< maximum length of callback name */
325
326 /**
327  * Function typedef used to register callbacks for memory events.
328  */
329 typedef void (*rte_mem_event_callback_t)(enum rte_mem_event event_type,
330                 const void *addr, size_t len);
331
332 /**
333  * Function used to register callbacks for memory events.
334  *
335  * @note callbacks will happen while memory hotplug subsystem is write-locked,
336  *       therefore some functions (e.g. `rte_memseg_walk()`) will cause a
337  *       deadlock when called from within such callbacks.
338  *
339  * @param name
340  *   Name associated with specified callback to be added to the list.
341  *
342  * @param clb
343  *   Callback function pointer.
344  *
345  * @return
346  *   0 on successful callback register
347  *   -1 on unsuccessful callback register, with rte_errno value indicating
348  *   reason for failure.
349  */
350 int __rte_experimental
351 rte_mem_event_callback_register(const char *name, rte_mem_event_callback_t clb);
352
353 /**
354  * Function used to unregister callbacks for memory events.
355  *
356  * @param name
357  *   Name associated with specified callback to be removed from the list.
358  *
359  * @return
360  *   0 on successful callback unregister
361  *   -1 on unsuccessful callback unregister, with rte_errno value indicating
362  *   reason for failure.
363  */
364 int __rte_experimental
365 rte_mem_event_callback_unregister(const char *name);
366
367
368 #define RTE_MEM_ALLOC_VALIDATOR_NAME_LEN 64
369 /**< maximum length of alloc validator name */
370 /**
371  * Function typedef used to register memory allocation validation callbacks.
372  *
373  * Returning 0 will allow allocation attempt to continue. Returning -1 will
374  * prevent allocation from succeeding.
375  */
376 typedef int (*rte_mem_alloc_validator_t)(int socket_id,
377                 size_t cur_limit, size_t new_len);
378
379 /**
380  * @brief Register validator callback for memory allocations.
381  *
382  * Callbacks registered by this function will be called right before memory
383  * allocator is about to trigger allocation of more pages from the system if
384  * said allocation will bring total memory usage above specified limit on
385  * specified socket. User will be able to cancel pending allocation if callback
386  * returns -1.
387  *
388  * @note callbacks will happen while memory hotplug subsystem is write-locked,
389  *       therefore some functions (e.g. `rte_memseg_walk()`) will cause a
390  *       deadlock when called from within such callbacks.
391  *
392  * @param name
393  *   Name associated with specified callback to be added to the list.
394  *
395  * @param clb
396  *   Callback function pointer.
397  *
398  * @param socket_id
399  *   Socket ID on which to watch for allocations.
400  *
401  * @param limit
402  *   Limit above which to trigger callbacks.
403  *
404  * @return
405  *   0 on successful callback register
406  *   -1 on unsuccessful callback register, with rte_errno value indicating
407  *   reason for failure.
408  */
409 int __rte_experimental
410 rte_mem_alloc_validator_register(const char *name,
411                 rte_mem_alloc_validator_t clb, int socket_id, size_t limit);
412
413 /**
414  * @brief Unregister validator callback for memory allocations.
415  *
416  * @param name
417  *   Name associated with specified callback to be removed from the list.
418  *
419  * @param socket_id
420  *   Socket ID on which to watch for allocations.
421  *
422  * @return
423  *   0 on successful callback unregister
424  *   -1 on unsuccessful callback unregister, with rte_errno value indicating
425  *   reason for failure.
426  */
427 int __rte_experimental
428 rte_mem_alloc_validator_unregister(const char *name, int socket_id);
429
430 #ifdef __cplusplus
431 }
432 #endif
433
434 #endif /* _RTE_MEMORY_H_ */