mem: fix anonymous mapping on Power9
[dpdk.git] / lib / librte_eal / linuxapp / eal / eal_memalloc.c
index 14bc5dc..7849395 100644 (file)
@@ -34,6 +34,7 @@
 #include <rte_log.h>
 #include <rte_eal_memconfig.h>
 #include <rte_eal.h>
+#include <rte_errno.h>
 #include <rte_memory.h>
 #include <rte_spinlock.h>
 
@@ -51,6 +52,23 @@ const int anonymous_hugepages_supported =
 #define RTE_MAP_HUGE_SHIFT 26
 #endif
 
+/*
+ * we don't actually care if memfd itself is supported - we only need to check
+ * if memfd supports hugetlbfs, as that already implies memfd support.
+ *
+ * also, this is not a constant, because while we may be *compiled* with memfd
+ * hugetlbfs support, we might not be *running* on a system that supports memfd
+ * and/or memfd with hugetlbfs, so we need to be able to adjust this flag at
+ * runtime, and fall back to anonymous memory.
+ */
+static int memfd_create_supported =
+#ifdef MFD_HUGETLB
+#define MEMFD_SUPPORTED
+               1;
+#else
+               0;
+#endif
+
 /*
  * not all kernel version support fallocate on hugetlbfs, so fall back to
  * ftruncate and disallow deallocation if fallocate is not supported.
@@ -190,6 +208,31 @@ get_file_size(int fd)
        return st.st_size;
 }
 
+static inline uint32_t
+bsf64(uint64_t v)
+{
+       return (uint32_t)__builtin_ctzll(v);
+}
+
+static inline uint32_t
+log2_u64(uint64_t v)
+{
+       if (v == 0)
+               return 0;
+       v = rte_align64pow2(v);
+       return bsf64(v);
+}
+
+static int
+pagesz_flags(uint64_t page_sz)
+{
+       /* as per mmap() manpage, all page sizes are log2 of page size
+        * shifted by MAP_HUGE_SHIFT
+        */
+       int log2 = log2_u64(page_sz);
+       return log2 << RTE_MAP_HUGE_SHIFT;
+}
+
 /* returns 1 on successful lock, 0 on unsuccessful lock, -1 on error */
 static int lock(int fd, int type)
 {
@@ -286,12 +329,64 @@ static int unlock_segment(int list_idx, int seg_idx)
        return 0;
 }
 
+static int
+get_seg_memfd(struct hugepage_info *hi __rte_unused,
+               unsigned int list_idx __rte_unused,
+               unsigned int seg_idx __rte_unused)
+{
+#ifdef MEMFD_SUPPORTED
+       int fd;
+       char segname[250]; /* as per manpage, limit is 249 bytes plus null */
+
+       if (internal_config.single_file_segments) {
+               fd = fd_list[list_idx].memseg_list_fd;
+
+               if (fd < 0) {
+                       int flags = MFD_HUGETLB | pagesz_flags(hi->hugepage_sz);
+
+                       snprintf(segname, sizeof(segname), "seg_%i", list_idx);
+                       fd = memfd_create(segname, flags);
+                       if (fd < 0) {
+                               RTE_LOG(DEBUG, EAL, "%s(): memfd create failed: %s\n",
+                                       __func__, strerror(errno));
+                               return -1;
+                       }
+                       fd_list[list_idx].memseg_list_fd = fd;
+               }
+       } else {
+               fd = fd_list[list_idx].fds[seg_idx];
+
+               if (fd < 0) {
+                       int flags = MFD_HUGETLB | pagesz_flags(hi->hugepage_sz);
+
+                       snprintf(segname, sizeof(segname), "seg_%i-%i",
+                                       list_idx, seg_idx);
+                       fd = memfd_create(segname, flags);
+                       if (fd < 0) {
+                               RTE_LOG(DEBUG, EAL, "%s(): memfd create failed: %s\n",
+                                       __func__, strerror(errno));
+                               return -1;
+                       }
+                       fd_list[list_idx].fds[seg_idx] = fd;
+               }
+       }
+       return fd;
+#endif
+       return -1;
+}
+
 static int
 get_seg_fd(char *path, int buflen, struct hugepage_info *hi,
                unsigned int list_idx, unsigned int seg_idx)
 {
        int fd;
 
+       /* for in-memory mode, we only make it here when we're sure we support
+        * memfd, and this is a special case.
+        */
+       if (internal_config.in_memory)
+               return get_seg_memfd(hi, list_idx, seg_idx);
+
        if (internal_config.single_file_segments) {
                /* create a hugepage file path */
                eal_get_hugefile_path(path, buflen, hi->hugedir, list_idx);
@@ -318,18 +413,24 @@ get_seg_fd(char *path, int buflen, struct hugepage_info *hi,
                /* create a hugepage file path */
                eal_get_hugefile_path(path, buflen, hi->hugedir,
                                list_idx * RTE_MAX_MEMSEG_PER_LIST + seg_idx);
-               fd = open(path, O_CREAT | O_RDWR, 0600);
+
+               fd = fd_list[list_idx].fds[seg_idx];
+
                if (fd < 0) {
-                       RTE_LOG(DEBUG, EAL, "%s(): open failed: %s\n", __func__,
-                                       strerror(errno));
-                       return -1;
-               }
-               /* take out a read lock */
-               if (lock(fd, LOCK_SH) < 0) {
-                       RTE_LOG(ERR, EAL, "%s(): lock failed: %s\n",
-                               __func__, strerror(errno));
-                       close(fd);
-                       return -1;
+                       fd = open(path, O_CREAT | O_RDWR, 0600);
+                       if (fd < 0) {
+                               RTE_LOG(DEBUG, EAL, "%s(): open failed: %s\n",
+                                       __func__, strerror(errno));
+                               return -1;
+                       }
+                       /* take out a read lock */
+                       if (lock(fd, LOCK_SH) < 0) {
+                               RTE_LOG(ERR, EAL, "%s(): lock failed: %s\n",
+                                       __func__, strerror(errno));
+                               close(fd);
+                               return -1;
+                       }
+                       fd_list[list_idx].fds[seg_idx] = fd;
                }
        }
        return fd;
@@ -340,6 +441,33 @@ resize_hugefile(int fd, char *path, int list_idx, int seg_idx,
                uint64_t fa_offset, uint64_t page_sz, bool grow)
 {
        bool again = false;
+
+       /* in-memory mode is a special case, because we don't need to perform
+        * any locking, and we can be sure that fallocate() is supported.
+        */
+       if (internal_config.in_memory) {
+               int flags = grow ? 0 : FALLOC_FL_PUNCH_HOLE |
+                               FALLOC_FL_KEEP_SIZE;
+               int ret;
+
+               /* grow or shrink the file */
+               ret = fallocate(fd, flags, fa_offset, page_sz);
+
+               if (ret < 0) {
+                       RTE_LOG(DEBUG, EAL, "%s(): fallocate() failed: %s\n",
+                                       __func__,
+                                       strerror(errno));
+                       return -1;
+               }
+               /* increase/decrease total segment count */
+               fd_list[list_idx].count += (grow ? 1 : -1);
+               if (!grow && fd_list[list_idx].count == 0) {
+                       close(fd_list[list_idx].memseg_list_fd);
+                       fd_list[list_idx].memseg_list_fd = -1;
+               }
+               return 0;
+       }
+
        do {
                if (fallocate_supported == 0) {
                        /* we cannot deallocate memory if fallocate() is not
@@ -489,26 +617,34 @@ alloc_seg(struct rte_memseg *ms, void *addr, int socket_id,
        void *new_addr;
 
        alloc_sz = hi->hugepage_sz;
-       if (!internal_config.single_file_segments &&
-                       internal_config.in_memory &&
-                       anonymous_hugepages_supported) {
-               int log2, flags;
-
-               log2 = rte_log2_u32(alloc_sz);
-               /* as per mmap() manpage, all page sizes are log2 of page size
-                * shifted by MAP_HUGE_SHIFT
-                */
-               flags = (log2 << RTE_MAP_HUGE_SHIFT) | MAP_HUGETLB | MAP_FIXED |
+
+       /* these are checked at init, but code analyzers don't know that */
+       if (internal_config.in_memory && !anonymous_hugepages_supported) {
+               RTE_LOG(ERR, EAL, "Anonymous hugepages not supported, in-memory mode cannot allocate memory\n");
+               return -1;
+       }
+       if (internal_config.in_memory && !memfd_create_supported &&
+                       internal_config.single_file_segments) {
+               RTE_LOG(ERR, EAL, "Single-file segments are not supported without memfd support\n");
+               return -1;
+       }
+
+       /* in-memory without memfd is a special case */
+       int mmap_flags;
+
+       if (internal_config.in_memory && !memfd_create_supported) {
+               int pagesz_flag, flags;
+
+               pagesz_flag = pagesz_flags(alloc_sz);
+               flags = pagesz_flag | MAP_HUGETLB | MAP_FIXED |
                                MAP_PRIVATE | MAP_ANONYMOUS;
                fd = -1;
-               va = mmap(addr, alloc_sz, PROT_READ | PROT_WRITE, flags, -1, 0);
-
-               /* single-file segments codepath will never be active because
-                * in-memory mode is incompatible with it and it's stopped at
-                * EAL initialization stage, however the compiler doesn't know
-                * that and complains about map_offset being used uninitialized
-                * on failure codepaths while having in-memory mode enabled. so,
-                * assign a value here.
+               mmap_flags = flags;
+
+               /* single-file segments codepath will never be active
+                * here because in-memory mode is incompatible with the
+                * fallback path, and it's stopped at EAL initialization
+                * stage.
                 */
                map_offset = 0;
        } else {
@@ -532,7 +668,8 @@ alloc_seg(struct rte_memseg *ms, void *addr, int socket_id,
                                        __func__, strerror(errno));
                                goto resized;
                        }
-                       if (internal_config.hugepage_unlink) {
+                       if (internal_config.hugepage_unlink &&
+                                       !internal_config.in_memory) {
                                if (unlink(path)) {
                                        RTE_LOG(DEBUG, EAL, "%s(): unlink() failed: %s\n",
                                                __func__, strerror(errno));
@@ -540,16 +677,16 @@ alloc_seg(struct rte_memseg *ms, void *addr, int socket_id,
                                }
                        }
                }
-
-               /*
-                * map the segment, and populate page tables, the kernel fills
-                * this segment with zeros if it's a new page.
-                */
-               va = mmap(addr, alloc_sz, PROT_READ | PROT_WRITE,
-                               MAP_SHARED | MAP_POPULATE | MAP_FIXED, fd,
-                               map_offset);
+               mmap_flags = MAP_SHARED | MAP_POPULATE | MAP_FIXED;
        }
 
+       /*
+        * map the segment, and populate page tables, the kernel fills
+        * this segment with zeros if it's a new page.
+        */
+       va = mmap(addr, alloc_sz, PROT_READ | PROT_WRITE, mmap_flags, fd,
+                       map_offset);
+
        if (va == MAP_FAILED) {
                RTE_LOG(DEBUG, EAL, "%s(): mmap() failed: %s\n", __func__,
                        strerror(errno));
@@ -601,10 +738,6 @@ alloc_seg(struct rte_memseg *ms, void *addr, int socket_id,
                goto mapped;
        }
 #endif
-       /* for non-single file segments that aren't in-memory, we can close fd
-        * here */
-       if (!internal_config.single_file_segments && !internal_config.in_memory)
-               close(fd);
 
        ms->addr = addr;
        ms->hugepage_sz = alloc_sz;
@@ -620,9 +753,6 @@ mapped:
        munmap(addr, alloc_sz);
 unmapped:
        flags = MAP_FIXED;
-#ifdef RTE_ARCH_PPC_64
-       flags |= MAP_HUGETLB;
-#endif
        new_addr = eal_get_virtual_area(addr, &alloc_sz, alloc_sz, 0, flags);
        if (new_addr != addr) {
                if (new_addr != NULL)
@@ -634,7 +764,10 @@ unmapped:
                RTE_LOG(CRIT, EAL, "Can't mmap holes in our virtual address space\n");
        }
 resized:
-       /* in-memory mode will never be single-file-segments mode */
+       /* some codepaths will return negative fd, so exit early */
+       if (fd < 0)
+               return -1;
+
        if (internal_config.single_file_segments) {
                resize_hugefile(fd, path, list_idx, seg_idx, map_offset,
                                alloc_sz, false);
@@ -646,6 +779,7 @@ resized:
                                lock(fd, LOCK_EX) == 1)
                        unlink(path);
                close(fd);
+               fd_list[list_idx].fds[seg_idx] = -1;
        }
        return -1;
 }
@@ -656,7 +790,8 @@ free_seg(struct rte_memseg *ms, struct hugepage_info *hi,
 {
        uint64_t map_offset;
        char path[PATH_MAX];
-       int fd, ret;
+       int fd, ret = 0;
+       bool exit_early;
 
        /* erase page data */
        memset(ms->addr, 0, ms->len);
@@ -668,8 +803,17 @@ free_seg(struct rte_memseg *ms, struct hugepage_info *hi,
                return -1;
        }
 
+       exit_early = false;
+
+       /* if we're using anonymous hugepages, nothing to be done */
+       if (internal_config.in_memory && !memfd_create_supported)
+               exit_early = true;
+
        /* if we've already unlinked the page, nothing needs to be done */
-       if (internal_config.hugepage_unlink) {
+       if (!internal_config.in_memory && internal_config.hugepage_unlink)
+               exit_early = true;
+
+       if (exit_early) {
                memset(ms, 0, sizeof(*ms));
                return 0;
        }
@@ -692,14 +836,17 @@ free_seg(struct rte_memseg *ms, struct hugepage_info *hi,
                /* if we're able to take out a write lock, we're the last one
                 * holding onto this page.
                 */
-               ret = lock(fd, LOCK_EX);
-               if (ret >= 0) {
-                       /* no one else is using this page */
-                       if (ret == 1)
-                               unlink(path);
+               if (!internal_config.in_memory) {
+                       ret = lock(fd, LOCK_EX);
+                       if (ret >= 0) {
+                               /* no one else is using this page */
+                               if (ret == 1)
+                                       unlink(path);
+                       }
                }
                /* closing fd will drop the lock */
                close(fd);
+               fd_list[list_idx].fds[seg_idx] = -1;
        }
 
        memset(ms, 0, sizeof(*ms));
@@ -836,7 +983,7 @@ free_seg_walk(const struct rte_memseg_list *msl, void *arg)
        int msl_idx, seg_idx, ret, dir_fd = -1;
 
        start_addr = (uintptr_t) msl->base_va;
-       end_addr = start_addr + msl->memseg_arr.len * (size_t)msl->page_sz;
+       end_addr = start_addr + msl->len;
 
        if ((uintptr_t)wa->ms->addr < start_addr ||
                        (uintptr_t)wa->ms->addr >= end_addr)
@@ -1258,6 +1405,9 @@ sync_walk(const struct rte_memseg_list *msl, void *arg __rte_unused)
        unsigned int i;
        int msl_idx;
 
+       if (msl->external)
+               return 0;
+
        msl_idx = msl - mcfg->memsegs;
        primary_msl = &mcfg->memsegs[msl_idx];
        local_msl = &local_memsegs[msl_idx];
@@ -1306,6 +1456,9 @@ secondary_msl_create_walk(const struct rte_memseg_list *msl,
        char name[PATH_MAX];
        int msl_idx, ret;
 
+       if (msl->external)
+               return 0;
+
        msl_idx = msl - mcfg->memsegs;
        primary_msl = &mcfg->memsegs[msl_idx];
        local_msl = &local_memsegs[msl_idx];
@@ -1322,21 +1475,16 @@ secondary_msl_create_walk(const struct rte_memseg_list *msl,
                return -1;
        }
        local_msl->base_va = primary_msl->base_va;
+       local_msl->len = primary_msl->len;
 
        return 0;
 }
 
 static int
-fd_list_create_walk(const struct rte_memseg_list *msl,
-               void *arg __rte_unused)
+alloc_list(int list_idx, int len)
 {
-       struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
-       unsigned int i, len;
-       int msl_idx;
        int *data;
-
-       msl_idx = msl - mcfg->memsegs;
-       len = msl->memseg_arr.len;
+       int i;
 
        /* ensure we have space to store fd per each possible segment */
        data = malloc(sizeof(int) * len);
@@ -1348,24 +1496,155 @@ fd_list_create_walk(const struct rte_memseg_list *msl,
        for (i = 0; i < len; i++)
                data[i] = -1;
 
-       fd_list[msl_idx].fds = data;
-       fd_list[msl_idx].len = len;
-       fd_list[msl_idx].count = 0;
-       fd_list[msl_idx].memseg_list_fd = -1;
+       fd_list[list_idx].fds = data;
+       fd_list[list_idx].len = len;
+       fd_list[list_idx].count = 0;
+       fd_list[list_idx].memseg_list_fd = -1;
 
        return 0;
 }
 
+static int
+fd_list_create_walk(const struct rte_memseg_list *msl,
+               void *arg __rte_unused)
+{
+       struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
+       unsigned int len;
+       int msl_idx;
+
+       if (msl->external)
+               return 0;
+
+       msl_idx = msl - mcfg->memsegs;
+       len = msl->memseg_arr.len;
+
+       return alloc_list(msl_idx, len);
+}
+
+int
+eal_memalloc_set_seg_fd(int list_idx, int seg_idx, int fd)
+{
+       struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
+
+       /* if list is not allocated, allocate it */
+       if (fd_list[list_idx].len == 0) {
+               int len = mcfg->memsegs[list_idx].memseg_arr.len;
+
+               if (alloc_list(list_idx, len) < 0)
+                       return -ENOMEM;
+       }
+       fd_list[list_idx].fds[seg_idx] = fd;
+
+       return 0;
+}
+
+int
+eal_memalloc_get_seg_fd(int list_idx, int seg_idx)
+{
+       int fd;
+       if (internal_config.single_file_segments) {
+               fd = fd_list[list_idx].memseg_list_fd;
+       } else if (fd_list[list_idx].len == 0) {
+               /* list not initialized */
+               fd = -1;
+       } else {
+               fd = fd_list[list_idx].fds[seg_idx];
+       }
+       if (fd < 0)
+               return -ENODEV;
+       return fd;
+}
+
+static int
+test_memfd_create(void)
+{
+#ifdef MEMFD_SUPPORTED
+       unsigned int i;
+       for (i = 0; i < internal_config.num_hugepage_sizes; i++) {
+               uint64_t pagesz = internal_config.hugepage_info[i].hugepage_sz;
+               int pagesz_flag = pagesz_flags(pagesz);
+               int flags;
+
+               flags = pagesz_flag | MFD_HUGETLB;
+               int fd = memfd_create("test", flags);
+               if (fd < 0) {
+                       /* we failed - let memalloc know this isn't working */
+                       if (errno == EINVAL) {
+                               memfd_create_supported = 0;
+                               return 0; /* not supported */
+                       }
+
+                       /* we got other error - something's wrong */
+                       return -1; /* error */
+               }
+               close(fd);
+               return 1; /* supported */
+       }
+#endif
+       return 0; /* not supported */
+}
+
+int
+eal_memalloc_get_seg_fd_offset(int list_idx, int seg_idx, size_t *offset)
+{
+       struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
+
+       /* fd_list not initialized? */
+       if (fd_list[list_idx].len == 0)
+               return -ENODEV;
+       if (internal_config.single_file_segments) {
+               size_t pgsz = mcfg->memsegs[list_idx].page_sz;
+
+               /* segment not active? */
+               if (fd_list[list_idx].memseg_list_fd < 0)
+                       return -ENOENT;
+               *offset = pgsz * seg_idx;
+       } else {
+               /* segment not active? */
+               if (fd_list[list_idx].fds[seg_idx] < 0)
+                       return -ENOENT;
+               *offset = 0;
+       }
+       return 0;
+}
+
 int
 eal_memalloc_init(void)
 {
        if (rte_eal_process_type() == RTE_PROC_SECONDARY)
                if (rte_memseg_list_walk(secondary_msl_create_walk, NULL) < 0)
                        return -1;
+       if (rte_eal_process_type() == RTE_PROC_PRIMARY &&
+                       internal_config.in_memory) {
+               int mfd_res = test_memfd_create();
 
-       /* initialize all of the fd lists */
-       if (internal_config.single_file_segments)
-               if (rte_memseg_list_walk(fd_list_create_walk, NULL))
+               if (mfd_res < 0) {
+                       RTE_LOG(ERR, EAL, "Unable to check if memfd is supported\n");
+                       return -1;
+               }
+               if (mfd_res == 1)
+                       RTE_LOG(DEBUG, EAL, "Using memfd for anonymous memory\n");
+               else
+                       RTE_LOG(INFO, EAL, "Using memfd is not supported, falling back to anonymous hugepages\n");
+
+               /* we only support single-file segments mode with in-memory mode
+                * if we support hugetlbfs with memfd_create. this code will
+                * test if we do.
+                */
+               if (internal_config.single_file_segments &&
+                               mfd_res != 1) {
+                       RTE_LOG(ERR, EAL, "Single-file segments mode cannot be used without memfd support\n");
+                       return -1;
+               }
+               /* this cannot ever happen but better safe than sorry */
+               if (!anonymous_hugepages_supported) {
+                       RTE_LOG(ERR, EAL, "Using anonymous memory is not supported\n");
                        return -1;
+               }
+       }
+
+       /* initialize all of the fd lists */
+       if (rte_memseg_list_walk(fd_list_create_walk, NULL))
+               return -1;
        return 0;
 }