New upstream version 17.08
[deb_dpdk.git] / lib / librte_eal / linuxapp / eal / eal_memory.c
index ebe0683..5279128 100644 (file)
@@ -2,6 +2,7 @@
  *   BSD LICENSE
  *
  *   Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
+ *   Copyright(c) 2013 6WIND.
  *   All rights reserved.
  *
  *   Redistribution and use in source and binary forms, with or without
  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
-/*   BSD LICENSE
- *
- *   Copyright(c) 2013 6WIND.
- *
- *   Redistribution and use in source and binary forms, with or without
- *   modification, are permitted provided that the following conditions
- *   are met:
- *
- *     * Redistributions of source code must retain the above copyright
- *       notice, this list of conditions and the following disclaimer.
- *     * Redistributions in binary form must reproduce the above copyright
- *       notice, this list of conditions and the following disclaimer in
- *       the documentation and/or other materials provided with the
- *       distribution.
- *     * Neither the name of 6WIND S.A. nor the names of its
- *       contributors may be used to endorse or promote products derived
- *       from this software without specific prior written permission.
- *
- *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
- *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
- *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
- *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
- *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
- *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
- *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
- *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
- *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
- *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
 
 #define _FILE_OFFSET_BITS 64
 #include <errno.h>
@@ -70,7 +41,6 @@
 #include <stdint.h>
 #include <inttypes.h>
 #include <string.h>
-#include <stdarg.h>
 #include <sys/mman.h>
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <sys/file.h>
 #include <unistd.h>
 #include <limits.h>
-#include <errno.h>
 #include <sys/ioctl.h>
 #include <sys/time.h>
 #include <signal.h>
 #include <setjmp.h>
+#ifdef RTE_EAL_NUMA_AWARE_HUGEPAGES
+#include <numa.h>
+#include <numaif.h>
+#endif
 
 #include <rte_log.h>
 #include <rte_memory.h>
@@ -137,6 +110,13 @@ test_phys_addrs_available(void)
        if (rte_xen_dom0_supported())
                return;
 
+       if (!rte_eal_has_hugepages()) {
+               RTE_LOG(ERR, EAL,
+                       "Started without hugepages support, physical addresses not available\n");
+               phys_addrs_available = false;
+               return;
+       }
+
        physaddr = rte_mem_virt2phy(&tmp);
        if (physaddr == RTE_BAD_PHYS_ADDR) {
                RTE_LOG(ERR, EAL,
@@ -147,16 +127,6 @@ test_phys_addrs_available(void)
        }
 }
 
-/* Lock page in physical memory and prevent from swapping. */
-int
-rte_mem_lock_page(const void *virt)
-{
-       unsigned long virtual = (unsigned long)virt;
-       int page_size = getpagesize();
-       unsigned long aligned = (virtual & ~ (page_size - 1));
-       return mlock((void*)aligned, page_size);
-}
-
 /*
  * Get physical address of any mapped virtual address in the current process.
  */
@@ -387,6 +357,14 @@ static int huge_wrap_sigsetjmp(void)
        return sigsetjmp(huge_jmpenv, 1);
 }
 
+#ifdef RTE_EAL_NUMA_AWARE_HUGEPAGES
+/* Callback for numa library. */
+void numa_error(char *where)
+{
+       RTE_LOG(ERR, EAL, "%s failed: %s\n", where, strerror(errno));
+}
+#endif
+
 /*
  * Mmap all hugepages of hugepage table: it first open a file in
  * hugetlbfs, then mmap() hugepage_sz data in it. If orig is set, the
@@ -395,18 +373,78 @@ static int huge_wrap_sigsetjmp(void)
  * map continguous physical blocks in contiguous virtual blocks.
  */
 static unsigned
-map_all_hugepages(struct hugepage_file *hugepg_tbl,
-               struct hugepage_info *hpi, int orig)
+map_all_hugepages(struct hugepage_file *hugepg_tbl, struct hugepage_info *hpi,
+                 uint64_t *essential_memory __rte_unused, int orig)
 {
        int fd;
        unsigned i;
        void *virtaddr;
        void *vma_addr = NULL;
        size_t vma_len = 0;
+#ifdef RTE_EAL_NUMA_AWARE_HUGEPAGES
+       int node_id = -1;
+       int essential_prev = 0;
+       int oldpolicy;
+       struct bitmask *oldmask = numa_allocate_nodemask();
+       bool have_numa = true;
+       unsigned long maxnode = 0;
+
+       /* Check if kernel supports NUMA. */
+       if (numa_available() != 0) {
+               RTE_LOG(DEBUG, EAL, "NUMA is not supported.\n");
+               have_numa = false;
+       }
+
+       if (orig && have_numa) {
+               RTE_LOG(DEBUG, EAL, "Trying to obtain current memory policy.\n");
+               if (get_mempolicy(&oldpolicy, oldmask->maskp,
+                                 oldmask->size + 1, 0, 0) < 0) {
+                       RTE_LOG(ERR, EAL,
+                               "Failed to get current mempolicy: %s. "
+                               "Assuming MPOL_DEFAULT.\n", strerror(errno));
+                       oldpolicy = MPOL_DEFAULT;
+               }
+               for (i = 0; i < RTE_MAX_NUMA_NODES; i++)
+                       if (internal_config.socket_mem[i])
+                               maxnode = i + 1;
+       }
+#endif
 
        for (i = 0; i < hpi->num_pages[0]; i++) {
                uint64_t hugepage_sz = hpi->hugepage_sz;
 
+#ifdef RTE_EAL_NUMA_AWARE_HUGEPAGES
+               if (maxnode) {
+                       unsigned int j;
+
+                       for (j = 0; j < maxnode; j++)
+                               if (essential_memory[j])
+                                       break;
+
+                       if (j == maxnode) {
+                               node_id = (node_id + 1) % maxnode;
+                               while (!internal_config.socket_mem[node_id]) {
+                                       node_id++;
+                                       node_id %= maxnode;
+                               }
+                               essential_prev = 0;
+                       } else {
+                               node_id = j;
+                               essential_prev = essential_memory[j];
+
+                               if (essential_memory[j] < hugepage_sz)
+                                       essential_memory[j] = 0;
+                               else
+                                       essential_memory[j] -= hugepage_sz;
+                       }
+
+                       RTE_LOG(DEBUG, EAL,
+                               "Setting policy MPOL_PREFERRED for socket %d\n",
+                               node_id);
+                       numa_set_preferred(node_id);
+               }
+#endif
+
                if (orig) {
                        hugepg_tbl[i].file_id = i;
                        hugepg_tbl[i].size = hugepage_sz;
@@ -461,7 +499,7 @@ map_all_hugepages(struct hugepage_file *hugepg_tbl,
                if (fd < 0) {
                        RTE_LOG(DEBUG, EAL, "%s(): open failed: %s\n", __func__,
                                        strerror(errno));
-                       return i;
+                       goto out;
                }
 
                /* map the segment, and populate page tables,
@@ -472,7 +510,7 @@ map_all_hugepages(struct hugepage_file *hugepg_tbl,
                        RTE_LOG(DEBUG, EAL, "%s(): mmap failed: %s\n", __func__,
                                        strerror(errno));
                        close(fd);
-                       return i;
+                       goto out;
                }
 
                if (orig) {
@@ -497,7 +535,12 @@ map_all_hugepages(struct hugepage_file *hugepg_tbl,
                                munmap(virtaddr, hugepage_sz);
                                close(fd);
                                unlink(hugepg_tbl[i].filepath);
-                               return i;
+#ifdef RTE_EAL_NUMA_AWARE_HUGEPAGES
+                               if (maxnode)
+                                       essential_memory[node_id] =
+                                               essential_prev;
+#endif
+                               goto out;
                        }
                        *(int *)virtaddr = 0;
                }
@@ -508,7 +551,7 @@ map_all_hugepages(struct hugepage_file *hugepg_tbl,
                        RTE_LOG(DEBUG, EAL, "%s(): Locking file failed:%s \n",
                                __func__, strerror(errno));
                        close(fd);
-                       return i;
+                       goto out;
                }
 
                close(fd);
@@ -517,6 +560,22 @@ map_all_hugepages(struct hugepage_file *hugepg_tbl,
                vma_len -= hugepage_sz;
        }
 
+out:
+#ifdef RTE_EAL_NUMA_AWARE_HUGEPAGES
+       if (maxnode) {
+               RTE_LOG(DEBUG, EAL,
+                       "Restoring previous memory policy: %d\n", oldpolicy);
+               if (oldpolicy == MPOL_DEFAULT) {
+                       numa_set_localalloc();
+               } else if (set_mempolicy(oldpolicy, oldmask->maskp,
+                                        oldmask->size + 1) < 0) {
+                       RTE_LOG(ERR, EAL, "Failed to restore mempolicy: %s\n",
+                               strerror(errno));
+                       numa_set_localalloc();
+               }
+       }
+       numa_free_cpumask(oldmask);
+#endif
        return i;
 }
 
@@ -551,8 +610,8 @@ find_numasocket(struct hugepage_file *hugepg_tbl, struct hugepage_info *hpi)
 
        f = fopen("/proc/self/numa_maps", "r");
        if (f == NULL) {
-               RTE_LOG(NOTICE, EAL, "cannot open /proc/self/numa_maps,"
-                               " consider that all memory is in socket_id 0\n");
+               RTE_LOG(NOTICE, EAL, "NUMA support not available"
+                       " consider that all memory is in socket_id 0\n");
                return 0;
        }
 
@@ -601,6 +660,11 @@ find_numasocket(struct hugepage_file *hugepg_tbl, struct hugepage_info *hpi)
                        if (hugepg_tbl[i].orig_va == va) {
                                hugepg_tbl[i].socket_id = socket_id;
                                hp_count++;
+#ifdef RTE_EAL_NUMA_AWARE_HUGEPAGES
+                               RTE_LOG(DEBUG, EAL,
+                                       "Hugepage %s is on socket %d\n",
+                                       hugepg_tbl[i].filepath, socket_id);
+#endif
                        }
                }
        }
@@ -995,7 +1059,7 @@ rte_eal_hugepage_init(void)
                                        strerror(errno));
                        return -1;
                }
-               mcfg->memseg[0].phys_addr = (phys_addr_t)(uintptr_t)addr;
+               mcfg->memseg[0].phys_addr = RTE_BAD_PHYS_ADDR;
                mcfg->memseg[0].addr = addr;
                mcfg->memseg[0].hugepage_sz = RTE_PGSIZE_4K;
                mcfg->memseg[0].len = internal_config.memory;
@@ -1039,6 +1103,11 @@ rte_eal_hugepage_init(void)
 
        huge_register_sigbus();
 
+       /* make a copy of socket_mem, needed for balanced allocation. */
+       for (i = 0; i < RTE_MAX_NUMA_NODES; i++)
+               memory[i] = internal_config.socket_mem[i];
+
+
        /* map all hugepages and sort them */
        for (i = 0; i < (int)internal_config.num_hugepage_sizes; i ++){
                unsigned pages_old, pages_new;
@@ -1056,7 +1125,8 @@ rte_eal_hugepage_init(void)
 
                /* map all hugepages available */
                pages_old = hpi->num_pages[0];
-               pages_new = map_all_hugepages(&tmp_hp[hp_offset], hpi, 1);
+               pages_new = map_all_hugepages(&tmp_hp[hp_offset], hpi,
+                                             memory, 1);
                if (pages_new < pages_old) {
                        RTE_LOG(DEBUG, EAL,
                                "%d not %d hugepages of size %u MB allocated\n",
@@ -1099,7 +1169,7 @@ rte_eal_hugepage_init(void)
                      sizeof(struct hugepage_file), cmp_physaddr);
 
                /* remap all hugepages */
-               if (map_all_hugepages(&tmp_hp[hp_offset], hpi, 0) !=
+               if (map_all_hugepages(&tmp_hp[hp_offset], hpi, NULL, 0) !=
                    hpi->num_pages[0]) {
                        RTE_LOG(ERR, EAL, "Failed to remap %u MB pages\n",
                                        (unsigned)(hpi->hugepage_sz / 0x100000));