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