New upstream version 18.11-rc2
[deb_dpdk.git] / lib / librte_eal / common / eal_common_memory.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2014 Intel Corporation
3  */
4
5 #include <fcntl.h>
6 #include <errno.h>
7 #include <stdio.h>
8 #include <stdint.h>
9 #include <stdlib.h>
10 #include <stdarg.h>
11 #include <string.h>
12 #include <unistd.h>
13 #include <inttypes.h>
14 #include <sys/mman.h>
15 #include <sys/queue.h>
16
17 #include <rte_fbarray.h>
18 #include <rte_memory.h>
19 #include <rte_eal.h>
20 #include <rte_eal_memconfig.h>
21 #include <rte_errno.h>
22 #include <rte_log.h>
23
24 #include "eal_memalloc.h"
25 #include "eal_private.h"
26 #include "eal_internal_cfg.h"
27
28 /*
29  * Try to mmap *size bytes in /dev/zero. If it is successful, return the
30  * pointer to the mmap'd area and keep *size unmodified. Else, retry
31  * with a smaller zone: decrease *size by hugepage_sz until it reaches
32  * 0. In this case, return NULL. Note: this function returns an address
33  * which is a multiple of hugepage size.
34  */
35
36 #define MEMSEG_LIST_FMT "memseg-%" PRIu64 "k-%i-%i"
37
38 static void *next_baseaddr;
39 static uint64_t system_page_sz;
40
41 #ifdef RTE_ARCH_64
42 /*
43  * Linux kernel uses a really high address as starting address for serving
44  * mmaps calls. If there exists addressing limitations and IOVA mode is VA,
45  * this starting address is likely too high for those devices. However, it
46  * is possible to use a lower address in the process virtual address space
47  * as with 64 bits there is a lot of available space.
48  *
49  * Current known limitations are 39 or 40 bits. Setting the starting address
50  * at 4GB implies there are 508GB or 1020GB for mapping the available
51  * hugepages. This is likely enough for most systems, although a device with
52  * addressing limitations should call rte_mem_check_dma_mask for ensuring all
53  * memory is within supported range.
54  */
55 static uint64_t baseaddr = 0x100000000;
56 #endif
57
58 void *
59 eal_get_virtual_area(void *requested_addr, size_t *size,
60                 size_t page_sz, int flags, int mmap_flags)
61 {
62         bool addr_is_hint, allow_shrink, unmap, no_align;
63         uint64_t map_sz;
64         void *mapped_addr, *aligned_addr;
65
66         if (system_page_sz == 0)
67                 system_page_sz = sysconf(_SC_PAGESIZE);
68
69         mmap_flags |= MAP_PRIVATE | MAP_ANONYMOUS;
70
71         RTE_LOG(DEBUG, EAL, "Ask a virtual area of 0x%zx bytes\n", *size);
72
73         addr_is_hint = (flags & EAL_VIRTUAL_AREA_ADDR_IS_HINT) > 0;
74         allow_shrink = (flags & EAL_VIRTUAL_AREA_ALLOW_SHRINK) > 0;
75         unmap = (flags & EAL_VIRTUAL_AREA_UNMAP) > 0;
76
77         if (next_baseaddr == NULL && internal_config.base_virtaddr != 0 &&
78                         rte_eal_process_type() == RTE_PROC_PRIMARY)
79                 next_baseaddr = (void *) internal_config.base_virtaddr;
80
81 #ifdef RTE_ARCH_64
82         if (next_baseaddr == NULL && internal_config.base_virtaddr == 0 &&
83                         rte_eal_process_type() == RTE_PROC_PRIMARY)
84                 next_baseaddr = (void *) baseaddr;
85 #endif
86         if (requested_addr == NULL && next_baseaddr != NULL) {
87                 requested_addr = next_baseaddr;
88                 requested_addr = RTE_PTR_ALIGN(requested_addr, page_sz);
89                 addr_is_hint = true;
90         }
91
92         /* we don't need alignment of resulting pointer in the following cases:
93          *
94          * 1. page size is equal to system size
95          * 2. we have a requested address, and it is page-aligned, and we will
96          *    be discarding the address if we get a different one.
97          *
98          * for all other cases, alignment is potentially necessary.
99          */
100         no_align = (requested_addr != NULL &&
101                 requested_addr == RTE_PTR_ALIGN(requested_addr, page_sz) &&
102                 !addr_is_hint) ||
103                 page_sz == system_page_sz;
104
105         do {
106                 map_sz = no_align ? *size : *size + page_sz;
107                 if (map_sz > SIZE_MAX) {
108                         RTE_LOG(ERR, EAL, "Map size too big\n");
109                         rte_errno = E2BIG;
110                         return NULL;
111                 }
112
113                 mapped_addr = mmap(requested_addr, (size_t)map_sz, PROT_READ,
114                                 mmap_flags, -1, 0);
115                 if (mapped_addr == MAP_FAILED && allow_shrink)
116                         *size -= page_sz;
117
118                 if (mapped_addr != MAP_FAILED && addr_is_hint &&
119                     mapped_addr != requested_addr) {
120                         /* hint was not used. Try with another offset */
121                         munmap(mapped_addr, map_sz);
122                         mapped_addr = MAP_FAILED;
123                         next_baseaddr = RTE_PTR_ADD(next_baseaddr, page_sz);
124                         requested_addr = next_baseaddr;
125                 }
126         } while ((allow_shrink || addr_is_hint) &&
127                  mapped_addr == MAP_FAILED && *size > 0);
128
129         /* align resulting address - if map failed, we will ignore the value
130          * anyway, so no need to add additional checks.
131          */
132         aligned_addr = no_align ? mapped_addr :
133                         RTE_PTR_ALIGN(mapped_addr, page_sz);
134
135         if (*size == 0) {
136                 RTE_LOG(ERR, EAL, "Cannot get a virtual area of any size: %s\n",
137                         strerror(errno));
138                 rte_errno = errno;
139                 return NULL;
140         } else if (mapped_addr == MAP_FAILED) {
141                 RTE_LOG(ERR, EAL, "Cannot get a virtual area: %s\n",
142                         strerror(errno));
143                 /* pass errno up the call chain */
144                 rte_errno = errno;
145                 return NULL;
146         } else if (requested_addr != NULL && !addr_is_hint &&
147                         aligned_addr != requested_addr) {
148                 RTE_LOG(ERR, EAL, "Cannot get a virtual area at requested address: %p (got %p)\n",
149                         requested_addr, aligned_addr);
150                 munmap(mapped_addr, map_sz);
151                 rte_errno = EADDRNOTAVAIL;
152                 return NULL;
153         } else if (requested_addr != NULL && addr_is_hint &&
154                         aligned_addr != requested_addr) {
155                 RTE_LOG(WARNING, EAL, "WARNING! Base virtual address hint (%p != %p) not respected!\n",
156                         requested_addr, aligned_addr);
157                 RTE_LOG(WARNING, EAL, "   This may cause issues with mapping memory into secondary processes\n");
158         } else if (next_baseaddr != NULL) {
159                 next_baseaddr = RTE_PTR_ADD(aligned_addr, *size);
160         }
161
162         RTE_LOG(DEBUG, EAL, "Virtual area found at %p (size = 0x%zx)\n",
163                 aligned_addr, *size);
164
165         if (unmap) {
166                 munmap(mapped_addr, map_sz);
167         } else if (!no_align) {
168                 void *map_end, *aligned_end;
169                 size_t before_len, after_len;
170
171                 /* when we reserve space with alignment, we add alignment to
172                  * mapping size. On 32-bit, if 1GB alignment was requested, this
173                  * would waste 1GB of address space, which is a luxury we cannot
174                  * afford. so, if alignment was performed, check if any unneeded
175                  * address space can be unmapped back.
176                  */
177
178                 map_end = RTE_PTR_ADD(mapped_addr, (size_t)map_sz);
179                 aligned_end = RTE_PTR_ADD(aligned_addr, *size);
180
181                 /* unmap space before aligned mmap address */
182                 before_len = RTE_PTR_DIFF(aligned_addr, mapped_addr);
183                 if (before_len > 0)
184                         munmap(mapped_addr, before_len);
185
186                 /* unmap space after aligned end mmap address */
187                 after_len = RTE_PTR_DIFF(map_end, aligned_end);
188                 if (after_len > 0)
189                         munmap(aligned_end, after_len);
190         }
191
192         return aligned_addr;
193 }
194
195 static struct rte_memseg *
196 virt2memseg(const void *addr, const struct rte_memseg_list *msl)
197 {
198         const struct rte_fbarray *arr;
199         void *start, *end;
200         int ms_idx;
201
202         if (msl == NULL)
203                 return NULL;
204
205         /* a memseg list was specified, check if it's the right one */
206         start = msl->base_va;
207         end = RTE_PTR_ADD(start, msl->len);
208
209         if (addr < start || addr >= end)
210                 return NULL;
211
212         /* now, calculate index */
213         arr = &msl->memseg_arr;
214         ms_idx = RTE_PTR_DIFF(addr, msl->base_va) / msl->page_sz;
215         return rte_fbarray_get(arr, ms_idx);
216 }
217
218 static struct rte_memseg_list *
219 virt2memseg_list(const void *addr)
220 {
221         struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
222         struct rte_memseg_list *msl;
223         int msl_idx;
224
225         for (msl_idx = 0; msl_idx < RTE_MAX_MEMSEG_LISTS; msl_idx++) {
226                 void *start, *end;
227                 msl = &mcfg->memsegs[msl_idx];
228
229                 start = msl->base_va;
230                 end = RTE_PTR_ADD(start, msl->len);
231                 if (addr >= start && addr < end)
232                         break;
233         }
234         /* if we didn't find our memseg list */
235         if (msl_idx == RTE_MAX_MEMSEG_LISTS)
236                 return NULL;
237         return msl;
238 }
239
240 __rte_experimental struct rte_memseg_list *
241 rte_mem_virt2memseg_list(const void *addr)
242 {
243         return virt2memseg_list(addr);
244 }
245
246 struct virtiova {
247         rte_iova_t iova;
248         void *virt;
249 };
250 static int
251 find_virt(const struct rte_memseg_list *msl __rte_unused,
252                 const struct rte_memseg *ms, void *arg)
253 {
254         struct virtiova *vi = arg;
255         if (vi->iova >= ms->iova && vi->iova < (ms->iova + ms->len)) {
256                 size_t offset = vi->iova - ms->iova;
257                 vi->virt = RTE_PTR_ADD(ms->addr, offset);
258                 /* stop the walk */
259                 return 1;
260         }
261         return 0;
262 }
263 static int
264 find_virt_legacy(const struct rte_memseg_list *msl __rte_unused,
265                 const struct rte_memseg *ms, size_t len, void *arg)
266 {
267         struct virtiova *vi = arg;
268         if (vi->iova >= ms->iova && vi->iova < (ms->iova + len)) {
269                 size_t offset = vi->iova - ms->iova;
270                 vi->virt = RTE_PTR_ADD(ms->addr, offset);
271                 /* stop the walk */
272                 return 1;
273         }
274         return 0;
275 }
276
277 __rte_experimental void *
278 rte_mem_iova2virt(rte_iova_t iova)
279 {
280         struct virtiova vi;
281
282         memset(&vi, 0, sizeof(vi));
283
284         vi.iova = iova;
285         /* for legacy mem, we can get away with scanning VA-contiguous segments,
286          * as we know they are PA-contiguous as well
287          */
288         if (internal_config.legacy_mem)
289                 rte_memseg_contig_walk(find_virt_legacy, &vi);
290         else
291                 rte_memseg_walk(find_virt, &vi);
292
293         return vi.virt;
294 }
295
296 __rte_experimental struct rte_memseg *
297 rte_mem_virt2memseg(const void *addr, const struct rte_memseg_list *msl)
298 {
299         return virt2memseg(addr, msl != NULL ? msl :
300                         rte_mem_virt2memseg_list(addr));
301 }
302
303 static int
304 physmem_size(const struct rte_memseg_list *msl, void *arg)
305 {
306         uint64_t *total_len = arg;
307
308         if (msl->external)
309                 return 0;
310
311         *total_len += msl->memseg_arr.count * msl->page_sz;
312
313         return 0;
314 }
315
316 /* get the total size of memory */
317 uint64_t
318 rte_eal_get_physmem_size(void)
319 {
320         uint64_t total_len = 0;
321
322         rte_memseg_list_walk(physmem_size, &total_len);
323
324         return total_len;
325 }
326
327 static int
328 dump_memseg(const struct rte_memseg_list *msl, const struct rte_memseg *ms,
329                 void *arg)
330 {
331         struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
332         int msl_idx, ms_idx, fd;
333         FILE *f = arg;
334
335         msl_idx = msl - mcfg->memsegs;
336         if (msl_idx < 0 || msl_idx >= RTE_MAX_MEMSEG_LISTS)
337                 return -1;
338
339         ms_idx = rte_fbarray_find_idx(&msl->memseg_arr, ms);
340         if (ms_idx < 0)
341                 return -1;
342
343         fd = eal_memalloc_get_seg_fd(msl_idx, ms_idx);
344         fprintf(f, "Segment %i-%i: IOVA:0x%"PRIx64", len:%zu, "
345                         "virt:%p, socket_id:%"PRId32", "
346                         "hugepage_sz:%"PRIu64", nchannel:%"PRIx32", "
347                         "nrank:%"PRIx32" fd:%i\n",
348                         msl_idx, ms_idx,
349                         ms->iova,
350                         ms->len,
351                         ms->addr,
352                         ms->socket_id,
353                         ms->hugepage_sz,
354                         ms->nchannel,
355                         ms->nrank,
356                         fd);
357
358         return 0;
359 }
360
361 /*
362  * Defining here because declared in rte_memory.h, but the actual implementation
363  * is in eal_common_memalloc.c, like all other memalloc internals.
364  */
365 int __rte_experimental
366 rte_mem_event_callback_register(const char *name, rte_mem_event_callback_t clb,
367                 void *arg)
368 {
369         /* FreeBSD boots with legacy mem enabled by default */
370         if (internal_config.legacy_mem) {
371                 RTE_LOG(DEBUG, EAL, "Registering mem event callbacks not supported\n");
372                 rte_errno = ENOTSUP;
373                 return -1;
374         }
375         return eal_memalloc_mem_event_callback_register(name, clb, arg);
376 }
377
378 int __rte_experimental
379 rte_mem_event_callback_unregister(const char *name, void *arg)
380 {
381         /* FreeBSD boots with legacy mem enabled by default */
382         if (internal_config.legacy_mem) {
383                 RTE_LOG(DEBUG, EAL, "Registering mem event callbacks not supported\n");
384                 rte_errno = ENOTSUP;
385                 return -1;
386         }
387         return eal_memalloc_mem_event_callback_unregister(name, arg);
388 }
389
390 int __rte_experimental
391 rte_mem_alloc_validator_register(const char *name,
392                 rte_mem_alloc_validator_t clb, int socket_id, size_t limit)
393 {
394         /* FreeBSD boots with legacy mem enabled by default */
395         if (internal_config.legacy_mem) {
396                 RTE_LOG(DEBUG, EAL, "Registering mem alloc validators not supported\n");
397                 rte_errno = ENOTSUP;
398                 return -1;
399         }
400         return eal_memalloc_mem_alloc_validator_register(name, clb, socket_id,
401                         limit);
402 }
403
404 int __rte_experimental
405 rte_mem_alloc_validator_unregister(const char *name, int socket_id)
406 {
407         /* FreeBSD boots with legacy mem enabled by default */
408         if (internal_config.legacy_mem) {
409                 RTE_LOG(DEBUG, EAL, "Registering mem alloc validators not supported\n");
410                 rte_errno = ENOTSUP;
411                 return -1;
412         }
413         return eal_memalloc_mem_alloc_validator_unregister(name, socket_id);
414 }
415
416 /* Dump the physical memory layout on console */
417 void
418 rte_dump_physmem_layout(FILE *f)
419 {
420         rte_memseg_walk(dump_memseg, f);
421 }
422
423 static int
424 check_iova(const struct rte_memseg_list *msl __rte_unused,
425                 const struct rte_memseg *ms, void *arg)
426 {
427         uint64_t *mask = arg;
428         rte_iova_t iova;
429
430         /* higher address within segment */
431         iova = (ms->iova + ms->len) - 1;
432         if (!(iova & *mask))
433                 return 0;
434
435         RTE_LOG(DEBUG, EAL, "memseg iova %"PRIx64", len %zx, out of range\n",
436                             ms->iova, ms->len);
437
438         RTE_LOG(DEBUG, EAL, "\tusing dma mask %"PRIx64"\n", *mask);
439         return 1;
440 }
441
442 #if defined(RTE_ARCH_64)
443 #define MAX_DMA_MASK_BITS 63
444 #else
445 #define MAX_DMA_MASK_BITS 31
446 #endif
447
448 /* check memseg iovas are within the required range based on dma mask */
449 static int __rte_experimental
450 check_dma_mask(uint8_t maskbits, bool thread_unsafe)
451 {
452         struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
453         uint64_t mask;
454         int ret;
455
456         /* sanity check */
457         if (maskbits > MAX_DMA_MASK_BITS) {
458                 RTE_LOG(ERR, EAL, "wrong dma mask size %u (Max: %u)\n",
459                                    maskbits, MAX_DMA_MASK_BITS);
460                 return -1;
461         }
462
463         /* create dma mask */
464         mask = ~((1ULL << maskbits) - 1);
465
466         if (thread_unsafe)
467                 ret = rte_memseg_walk_thread_unsafe(check_iova, &mask);
468         else
469                 ret = rte_memseg_walk(check_iova, &mask);
470
471         if (ret)
472                 /*
473                  * Dma mask precludes hugepage usage.
474                  * This device can not be used and we do not need to keep
475                  * the dma mask.
476                  */
477                 return 1;
478
479         /*
480          * we need to keep the more restricted maskbit for checking
481          * potential dynamic memory allocation in the future.
482          */
483         mcfg->dma_maskbits = mcfg->dma_maskbits == 0 ? maskbits :
484                              RTE_MIN(mcfg->dma_maskbits, maskbits);
485
486         return 0;
487 }
488
489 int __rte_experimental
490 rte_mem_check_dma_mask(uint8_t maskbits)
491 {
492         return check_dma_mask(maskbits, false);
493 }
494
495 int __rte_experimental
496 rte_mem_check_dma_mask_thread_unsafe(uint8_t maskbits)
497 {
498         return check_dma_mask(maskbits, true);
499 }
500
501 /*
502  * Set dma mask to use when memory initialization is done.
503  *
504  * This function should ONLY be used by code executed before the memory
505  * initialization. PMDs should use rte_mem_check_dma_mask if addressing
506  * limitations by the device.
507  */
508 void __rte_experimental
509 rte_mem_set_dma_mask(uint8_t maskbits)
510 {
511         struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
512
513         mcfg->dma_maskbits = mcfg->dma_maskbits == 0 ? maskbits :
514                              RTE_MIN(mcfg->dma_maskbits, maskbits);
515 }
516
517 /* return the number of memory channels */
518 unsigned rte_memory_get_nchannel(void)
519 {
520         return rte_eal_get_configuration()->mem_config->nchannel;
521 }
522
523 /* return the number of memory rank */
524 unsigned rte_memory_get_nrank(void)
525 {
526         return rte_eal_get_configuration()->mem_config->nrank;
527 }
528
529 static int
530 rte_eal_memdevice_init(void)
531 {
532         struct rte_config *config;
533
534         if (rte_eal_process_type() == RTE_PROC_SECONDARY)
535                 return 0;
536
537         config = rte_eal_get_configuration();
538         config->mem_config->nchannel = internal_config.force_nchannel;
539         config->mem_config->nrank = internal_config.force_nrank;
540
541         return 0;
542 }
543
544 /* Lock page in physical memory and prevent from swapping. */
545 int
546 rte_mem_lock_page(const void *virt)
547 {
548         unsigned long virtual = (unsigned long)virt;
549         int page_size = getpagesize();
550         unsigned long aligned = (virtual & ~(page_size - 1));
551         return mlock((void *)aligned, page_size);
552 }
553
554 int __rte_experimental
555 rte_memseg_contig_walk_thread_unsafe(rte_memseg_contig_walk_t func, void *arg)
556 {
557         struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
558         int i, ms_idx, ret = 0;
559
560         for (i = 0; i < RTE_MAX_MEMSEG_LISTS; i++) {
561                 struct rte_memseg_list *msl = &mcfg->memsegs[i];
562                 const struct rte_memseg *ms;
563                 struct rte_fbarray *arr;
564
565                 if (msl->memseg_arr.count == 0)
566                         continue;
567
568                 arr = &msl->memseg_arr;
569
570                 ms_idx = rte_fbarray_find_next_used(arr, 0);
571                 while (ms_idx >= 0) {
572                         int n_segs;
573                         size_t len;
574
575                         ms = rte_fbarray_get(arr, ms_idx);
576
577                         /* find how many more segments there are, starting with
578                          * this one.
579                          */
580                         n_segs = rte_fbarray_find_contig_used(arr, ms_idx);
581                         len = n_segs * msl->page_sz;
582
583                         ret = func(msl, ms, len, arg);
584                         if (ret)
585                                 return ret;
586                         ms_idx = rte_fbarray_find_next_used(arr,
587                                         ms_idx + n_segs);
588                 }
589         }
590         return 0;
591 }
592
593 int __rte_experimental
594 rte_memseg_contig_walk(rte_memseg_contig_walk_t func, void *arg)
595 {
596         struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
597         int ret = 0;
598
599         /* do not allow allocations/frees/init while we iterate */
600         rte_rwlock_read_lock(&mcfg->memory_hotplug_lock);
601         ret = rte_memseg_contig_walk_thread_unsafe(func, arg);
602         rte_rwlock_read_unlock(&mcfg->memory_hotplug_lock);
603
604         return ret;
605 }
606
607 int __rte_experimental
608 rte_memseg_walk_thread_unsafe(rte_memseg_walk_t func, void *arg)
609 {
610         struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
611         int i, ms_idx, ret = 0;
612
613         for (i = 0; i < RTE_MAX_MEMSEG_LISTS; i++) {
614                 struct rte_memseg_list *msl = &mcfg->memsegs[i];
615                 const struct rte_memseg *ms;
616                 struct rte_fbarray *arr;
617
618                 if (msl->memseg_arr.count == 0)
619                         continue;
620
621                 arr = &msl->memseg_arr;
622
623                 ms_idx = rte_fbarray_find_next_used(arr, 0);
624                 while (ms_idx >= 0) {
625                         ms = rte_fbarray_get(arr, ms_idx);
626                         ret = func(msl, ms, arg);
627                         if (ret)
628                                 return ret;
629                         ms_idx = rte_fbarray_find_next_used(arr, ms_idx + 1);
630                 }
631         }
632         return 0;
633 }
634
635 int __rte_experimental
636 rte_memseg_walk(rte_memseg_walk_t func, void *arg)
637 {
638         struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
639         int ret = 0;
640
641         /* do not allow allocations/frees/init while we iterate */
642         rte_rwlock_read_lock(&mcfg->memory_hotplug_lock);
643         ret = rte_memseg_walk_thread_unsafe(func, arg);
644         rte_rwlock_read_unlock(&mcfg->memory_hotplug_lock);
645
646         return ret;
647 }
648
649 int __rte_experimental
650 rte_memseg_list_walk_thread_unsafe(rte_memseg_list_walk_t func, void *arg)
651 {
652         struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
653         int i, ret = 0;
654
655         for (i = 0; i < RTE_MAX_MEMSEG_LISTS; i++) {
656                 struct rte_memseg_list *msl = &mcfg->memsegs[i];
657
658                 if (msl->base_va == NULL)
659                         continue;
660
661                 ret = func(msl, arg);
662                 if (ret)
663                         return ret;
664         }
665         return 0;
666 }
667
668 int __rte_experimental
669 rte_memseg_list_walk(rte_memseg_list_walk_t func, void *arg)
670 {
671         struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
672         int ret = 0;
673
674         /* do not allow allocations/frees/init while we iterate */
675         rte_rwlock_read_lock(&mcfg->memory_hotplug_lock);
676         ret = rte_memseg_list_walk_thread_unsafe(func, arg);
677         rte_rwlock_read_unlock(&mcfg->memory_hotplug_lock);
678
679         return ret;
680 }
681
682 int __rte_experimental
683 rte_memseg_get_fd_thread_unsafe(const struct rte_memseg *ms)
684 {
685         struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
686         struct rte_memseg_list *msl;
687         struct rte_fbarray *arr;
688         int msl_idx, seg_idx, ret;
689
690         if (ms == NULL) {
691                 rte_errno = EINVAL;
692                 return -1;
693         }
694
695         msl = rte_mem_virt2memseg_list(ms->addr);
696         if (msl == NULL) {
697                 rte_errno = EINVAL;
698                 return -1;
699         }
700         arr = &msl->memseg_arr;
701
702         msl_idx = msl - mcfg->memsegs;
703         seg_idx = rte_fbarray_find_idx(arr, ms);
704
705         if (!rte_fbarray_is_used(arr, seg_idx)) {
706                 rte_errno = ENOENT;
707                 return -1;
708         }
709
710         ret = eal_memalloc_get_seg_fd(msl_idx, seg_idx);
711         if (ret < 0) {
712                 rte_errno = -ret;
713                 ret = -1;
714         }
715         return ret;
716 }
717
718 int __rte_experimental
719 rte_memseg_get_fd(const struct rte_memseg *ms)
720 {
721         struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
722         int ret;
723
724         rte_rwlock_read_lock(&mcfg->memory_hotplug_lock);
725         ret = rte_memseg_get_fd_thread_unsafe(ms);
726         rte_rwlock_read_unlock(&mcfg->memory_hotplug_lock);
727
728         return ret;
729 }
730
731 int __rte_experimental
732 rte_memseg_get_fd_offset_thread_unsafe(const struct rte_memseg *ms,
733                 size_t *offset)
734 {
735         struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
736         struct rte_memseg_list *msl;
737         struct rte_fbarray *arr;
738         int msl_idx, seg_idx, ret;
739
740         if (ms == NULL || offset == NULL) {
741                 rte_errno = EINVAL;
742                 return -1;
743         }
744
745         msl = rte_mem_virt2memseg_list(ms->addr);
746         if (msl == NULL) {
747                 rte_errno = EINVAL;
748                 return -1;
749         }
750         arr = &msl->memseg_arr;
751
752         msl_idx = msl - mcfg->memsegs;
753         seg_idx = rte_fbarray_find_idx(arr, ms);
754
755         if (!rte_fbarray_is_used(arr, seg_idx)) {
756                 rte_errno = ENOENT;
757                 return -1;
758         }
759
760         ret = eal_memalloc_get_seg_fd_offset(msl_idx, seg_idx, offset);
761         if (ret < 0) {
762                 rte_errno = -ret;
763                 ret = -1;
764         }
765         return ret;
766 }
767
768 int __rte_experimental
769 rte_memseg_get_fd_offset(const struct rte_memseg *ms, size_t *offset)
770 {
771         struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
772         int ret;
773
774         rte_rwlock_read_lock(&mcfg->memory_hotplug_lock);
775         ret = rte_memseg_get_fd_offset_thread_unsafe(ms, offset);
776         rte_rwlock_read_unlock(&mcfg->memory_hotplug_lock);
777
778         return ret;
779 }
780
781 /* init memory subsystem */
782 int
783 rte_eal_memory_init(void)
784 {
785         struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
786         int retval;
787         RTE_LOG(DEBUG, EAL, "Setting up physically contiguous memory...\n");
788
789         if (!mcfg)
790                 return -1;
791
792         /* lock mem hotplug here, to prevent races while we init */
793         rte_rwlock_read_lock(&mcfg->memory_hotplug_lock);
794
795         if (rte_eal_memseg_init() < 0)
796                 goto fail;
797
798         if (eal_memalloc_init() < 0)
799                 goto fail;
800
801         retval = rte_eal_process_type() == RTE_PROC_PRIMARY ?
802                         rte_eal_hugepage_init() :
803                         rte_eal_hugepage_attach();
804         if (retval < 0)
805                 goto fail;
806
807         if (internal_config.no_shconf == 0 && rte_eal_memdevice_init() < 0)
808                 goto fail;
809
810         return 0;
811 fail:
812         rte_rwlock_read_unlock(&mcfg->memory_hotplug_lock);
813         return -1;
814 }