eal: lock IPC directory on init and send
[dpdk.git] / lib / librte_eal / common / eal_common_memzone.c
index febc56b..1ab3ade 100644 (file)
@@ -1,34 +1,5 @@
-/*-
- *   BSD LICENSE
- *
- *   Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
- *   All rights reserved.
- *
- *   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 Intel Corporation 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.
+/* SPDX-License-Identifier: BSD-3-Clause
+ * Copyright(c) 2010-2014 Intel Corporation
  */
 
 #include <stdlib.h>
@@ -119,13 +90,17 @@ find_heap_max_free_elem(int *s, unsigned align)
                }
        }
 
-       return (len - MALLOC_ELEM_OVERHEAD - align);
+       if (len < MALLOC_ELEM_OVERHEAD + align)
+               return 0;
+
+       return len - MALLOC_ELEM_OVERHEAD - align;
 }
 
 static const struct rte_memzone *
 memzone_reserve_aligned_thread_unsafe(const char *name, size_t len,
                int socket_id, unsigned flags, unsigned align, unsigned bound)
 {
+       struct rte_memzone *mz;
        struct rte_mem_config *mcfg;
        size_t requested_len;
        int socket, i;
@@ -140,6 +115,13 @@ memzone_reserve_aligned_thread_unsafe(const char *name, size_t len,
                return NULL;
        }
 
+       if (strlen(name) > sizeof(mz->name) - 1) {
+               RTE_LOG(DEBUG, EAL, "%s(): memzone <%s>: name too long\n",
+                       __func__, name);
+               rte_errno = ENAMETOOLONG;
+               return NULL;
+       }
+
        /* zone already exist */
        if ((memzone_lookup_thread_unsafe(name)) != NULL) {
                RTE_LOG(DEBUG, EAL, "%s(): memzone <%s> already exists\n",
@@ -178,7 +160,8 @@ memzone_reserve_aligned_thread_unsafe(const char *name, size_t len,
                return NULL;
        }
 
-       if ((socket_id != SOCKET_ID_ANY) && (socket_id >= RTE_MAX_NUMA_NODES)) {
+       if ((socket_id != SOCKET_ID_ANY) &&
+           (socket_id >= RTE_MAX_NUMA_NODES || socket_id < 0)) {
                rte_errno = EINVAL;
                return NULL;
        }
@@ -189,8 +172,13 @@ memzone_reserve_aligned_thread_unsafe(const char *name, size_t len,
        if (len == 0) {
                if (bound != 0)
                        requested_len = bound;
-               else
+               else {
                        requested_len = find_heap_max_free_elem(&socket_id, align);
+                       if (requested_len == 0) {
+                               rte_errno = ENOMEM;
+                               return NULL;
+                       }
+               }
        }
 
        if (socket_id == SOCKET_ID_ANY)
@@ -220,21 +208,22 @@ memzone_reserve_aligned_thread_unsafe(const char *name, size_t len,
                return NULL;
        }
 
-       const struct malloc_elem *elem = malloc_elem_from_data(mz_addr);
+       struct malloc_elem *elem = malloc_elem_from_data(mz_addr);
 
        /* fill the zone in config */
-       struct rte_memzone *mz = get_next_free_memzone();
+       mz = get_next_free_memzone();
 
        if (mz == NULL) {
                RTE_LOG(ERR, EAL, "%s(): Cannot find free memzone but there is room "
                                "in config!\n", __func__);
+               malloc_elem_free(elem);
                rte_errno = ENOSPC;
                return NULL;
        }
 
        mcfg->memzone_cnt++;
        snprintf(mz->name, sizeof(mz->name), "%s", name);
-       mz->phys_addr = rte_malloc_virt2phy(mz_addr);
+       mz->iova = rte_malloc_virt2iova(mz_addr);
        mz->addr = mz_addr;
        mz->len = (requested_len == 0 ? elem->size : requested_len);
        mz->hugepage_sz = elem->ms->hugepage_sz;
@@ -322,14 +311,6 @@ rte_memzone_free(const struct rte_memzone *mz)
        idx = idx / sizeof(struct rte_memzone);
 
        addr = mcfg->memzone[idx].addr;
-#ifdef RTE_LIBRTE_IVSHMEM
-       /*
-        * If ioremap_addr is set, it's an IVSHMEM memzone and we cannot
-        * free it.
-        */
-       if (mcfg->memzone[idx].ioremap_addr != 0)
-               ret = -EINVAL;
-#endif
        if (addr == NULL)
                ret = -EINVAL;
        else if (mcfg->memzone_cnt == 0) {
@@ -382,10 +363,10 @@ rte_memzone_dump(FILE *f)
        for (i=0; i<RTE_MAX_MEMZONE; i++) {
                if (mcfg->memzone[i].addr == NULL)
                        break;
-               fprintf(f, "Zone %u: name:<%s>, phys:0x%"PRIx64", len:0x%zx"
+               fprintf(f, "Zone %u: name:<%s>, IO:0x%"PRIx64", len:0x%zx"
                       ", virt:%p, socket_id:%"PRId32", flags:%"PRIx32"\n", i,
                       mcfg->memzone[i].name,
-                      mcfg->memzone[i].phys_addr,
+                      mcfg->memzone[i].iova,
                       mcfg->memzone[i].len,
                       mcfg->memzone[i].addr,
                       mcfg->memzone[i].socket_id,