mem: fix potential bad unmap on map failure
[dpdk.git] / lib / librte_eal / linuxapp / eal / eal_memalloc.c
index f48d10d..559e3f2 100644 (file)
@@ -146,7 +146,7 @@ resotre_numa(int *oldpolicy, struct bitmask *oldmask)
 {
        RTE_LOG(DEBUG, EAL,
                "Restoring previous memory policy: %d\n", *oldpolicy);
-       if (oldpolicy == MPOL_DEFAULT) {
+       if (*oldpolicy == MPOL_DEFAULT) {
                numa_set_localalloc();
        } else if (set_mempolicy(*oldpolicy, oldmask->maskp,
                                 oldmask->size + 1) < 0) {
@@ -466,7 +466,8 @@ alloc_seg(struct rte_memseg *ms, void *addr, int socket_id,
        }
        if (va != addr) {
                RTE_LOG(DEBUG, EAL, "%s(): wrong mmap() address\n", __func__);
-               goto mapped;
+               munmap(va, alloc_sz);
+               goto resized;
        }
 
        rte_iova_t iova = rte_mem_virt2iova(addr);
@@ -522,13 +523,14 @@ resized:
                resize_hugefile(fd, map_offset, alloc_sz, false);
                if (is_zero_length(fd)) {
                        struct msl_entry *te = get_msl_entry_by_idx(list_idx);
-                       if (te != NULL && te->fd >= 0) {
-                               close(te->fd);
+                       /* te->fd is equivalent to fd */
+                       if (te != NULL && te->fd >= 0)
                                te->fd = -1;
-                       }
                        /* ignore errors, can't make it any worse */
                        unlink(path);
+                       close(fd);
                }
+               /* if we're not removing the file, fd stays in the tailq */
        } else {
                close(fd);
                unlink(path);
@@ -567,12 +569,13 @@ free_seg(struct rte_memseg *ms, struct hugepage_info *hi,
                 */
                if (is_zero_length(fd)) {
                        struct msl_entry *te = get_msl_entry_by_idx(list_idx);
-                       if (te != NULL && te->fd >= 0) {
-                               close(te->fd);
+                       /* te->fd is equivalent to fd */
+                       if (te != NULL && te->fd >= 0)
                                te->fd = -1;
-                       }
                        unlink(path);
+                       close(fd);
                }
+               /* if we're not removing the file, fd stays in the tailq */
                ret = 0;
        } else {
                /* if we're able to take out a write lock, we're the last one