X-Git-Url: http://git.droids-corp.org/?a=blobdiff_plain;f=lib%2Flibrte_eal%2Fcommon%2Feal_common_memzone.c;h=a1fcdfdfd0003780623089b9856f828f4b479d57;hb=148f963fb5323c1c6b6d5cea95084deb25cc73f8;hp=5532f0d59daf04281c4e78eb9f0ee631293b7076;hpb=b6df9fc8715f9a925136006b18fdd65f9c621757;p=dpdk.git diff --git a/lib/librte_eal/common/eal_common_memzone.c b/lib/librte_eal/common/eal_common_memzone.c index 5532f0d59d..a1fcdfdfd0 100644 --- a/lib/librte_eal/common/eal_common_memzone.c +++ b/lib/librte_eal/common/eal_common_memzone.c @@ -1,35 +1,34 @@ /*- * BSD LICENSE * - * Copyright(c) 2010-2013 Intel Corporation. All rights reserved. + * 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 + * 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 + * * 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 + * * 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 + * * 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 + * 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. - * */ #include @@ -83,22 +82,58 @@ memzone_lookup_thread_unsafe(const char *name) * allocation cannot be done, return NULL. */ const struct rte_memzone * -rte_memzone_reserve(const char *name, uint64_t len, int socket_id, +rte_memzone_reserve(const char *name, size_t len, int socket_id, unsigned flags) { return rte_memzone_reserve_aligned(name, len, socket_id, flags, CACHE_LINE_SIZE); } +/* + * Helper function for memzone_reserve_aligned_thread_unsafe(). + * Calculate address offset from the start of the segment. + * Align offset in that way that it satisfy istart alignmnet and + * buffer of the requested length would not cross specified boundary. + */ +static inline phys_addr_t +align_phys_boundary(const struct rte_memseg *ms, size_t len, size_t align, + size_t bound) +{ + phys_addr_t addr_offset, bmask, end, start; + size_t step; + + step = RTE_MAX(align, bound); + bmask = ~((phys_addr_t)bound - 1); + + /* calculate offset to closest alignment */ + start = RTE_ALIGN_CEIL(ms->phys_addr, align); + addr_offset = start - ms->phys_addr; + + while (addr_offset + len < ms->len) { + + /* check, do we meet boundary condition */ + end = start + len - (len != 0); + if ((start & bmask) == (end & bmask)) + break; + + /* calculate next offset */ + start = RTE_ALIGN_CEIL(start + 1, step); + addr_offset = start - ms->phys_addr; + } + + return (addr_offset); +} + static const struct rte_memzone * -memzone_reserve_aligned_thread_unsafe(const char *name, uint64_t len, - int socket_id, unsigned flags, unsigned align) +memzone_reserve_aligned_thread_unsafe(const char *name, size_t len, + int socket_id, unsigned flags, unsigned align, unsigned bound) { struct rte_mem_config *mcfg; unsigned i = 0; int memseg_idx = -1; - uint64_t addr_offset, requested_len; - uint64_t memseg_len = 0; + uint64_t addr_offset, seg_offset = 0; + size_t requested_len; + size_t memseg_len = 0; phys_addr_t memseg_physaddr; void *memseg_addr; @@ -120,23 +155,37 @@ memzone_reserve_aligned_thread_unsafe(const char *name, uint64_t len, return NULL; } - /* align length on cache boundary */ - len += CACHE_LINE_MASK; - len &= ~((uint64_t) CACHE_LINE_MASK); + /* if alignment is not a power of two */ + if (!rte_is_power_of_2(align)) { + RTE_LOG(ERR, EAL, "%s(): Invalid alignment: %u\n", __func__, + align); + rte_errno = EINVAL; + return NULL; + } - /* save original length */ - requested_len = len; + /* alignment less than cache size is not allowed */ + if (align < CACHE_LINE_SIZE) + align = CACHE_LINE_SIZE; - /* reserve extra space for future alignment */ - if (len) - len += align; - /* save requested length */ - requested_len = len; + /* align length on cache boundary. Check for overflow before doing so */ + if (len > SIZE_MAX - CACHE_LINE_MASK) { + rte_errno = EINVAL; /* requested size too big */ + return NULL; + } - /* reserve extra space for future alignment */ - if (len) - len += align; + len += CACHE_LINE_MASK; + len &= ~((size_t) CACHE_LINE_MASK); + + /* save minimal requested length */ + requested_len = RTE_MAX((size_t)CACHE_LINE_SIZE, len); + + /* check that boundary condition is valid */ + if (bound != 0 && + (requested_len > bound || !rte_is_power_of_2(bound))) { + rte_errno = EINVAL; + return NULL; + } /* find the smallest segment matching requirements */ for (i = 0; i < RTE_MAX_MEMSEG; i++) { @@ -153,8 +202,15 @@ memzone_reserve_aligned_thread_unsafe(const char *name, uint64_t len, socket_id != free_memseg[i].socket_id) continue; + /* + * calculate offset to closest alignment that + * meets boundary conditions. + */ + addr_offset = align_phys_boundary(free_memseg + i, + requested_len, align, bound); + /* check len */ - if (len != 0 && len > free_memseg[i].len) + if ((requested_len + addr_offset) > free_memseg[i].len) continue; /* check flags for hugepage sizes */ @@ -169,21 +225,26 @@ memzone_reserve_aligned_thread_unsafe(const char *name, uint64_t len, if (memseg_idx == -1) { memseg_idx = i; memseg_len = free_memseg[i].len; + seg_offset = addr_offset; } /* find the biggest contiguous zone */ else if (len == 0) { if (free_memseg[i].len > memseg_len) { memseg_idx = i; memseg_len = free_memseg[i].len; + seg_offset = addr_offset; } } /* * find the smallest (we already checked that current * zone length is > len */ - else if (free_memseg[i].len < memseg_len) { + else if (free_memseg[i].len + align < memseg_len || + (free_memseg[i].len <= memseg_len + align && + addr_offset < seg_offset)) { memseg_idx = i; memseg_len = free_memseg[i].len; + seg_offset = addr_offset; } } @@ -195,30 +256,32 @@ memzone_reserve_aligned_thread_unsafe(const char *name, uint64_t len, */ if ((flags & RTE_MEMZONE_SIZE_HINT_ONLY) && ((flags & RTE_MEMZONE_1GB) || (flags & RTE_MEMZONE_2MB))) - return memzone_reserve_aligned_thread_unsafe(name, len - align, - socket_id, 0, align); + return memzone_reserve_aligned_thread_unsafe(name, + len, socket_id, 0, align, bound); - RTE_LOG(ERR, EAL, "%s(%s, %" PRIu64 ", %d): " + RTE_LOG(ERR, EAL, "%s(%s, %zu, %d): " "No appropriate segment found\n", __func__, name, requested_len, socket_id); rte_errno = ENOMEM; return NULL; } - /* get offset needed to adjust alignment */ - addr_offset = RTE_ALIGN_CEIL(free_memseg[memseg_idx].phys_addr, align) - - free_memseg[memseg_idx].phys_addr; - /* save aligned physical and virtual addresses */ - memseg_physaddr = free_memseg[memseg_idx].phys_addr + addr_offset; - memseg_addr = RTE_PTR_ADD(free_memseg[memseg_idx].addr, (uintptr_t) addr_offset); + memseg_physaddr = free_memseg[memseg_idx].phys_addr + seg_offset; + memseg_addr = RTE_PTR_ADD(free_memseg[memseg_idx].addr, + (uintptr_t) seg_offset); /* if we are looking for a biggest memzone */ - if (requested_len == 0) - requested_len = memseg_len - addr_offset; + if (len == 0) { + if (bound == 0) + requested_len = memseg_len - seg_offset; + else + requested_len = RTE_ALIGN_CEIL(memseg_physaddr + 1, + bound) - memseg_physaddr; + } /* set length to correct value */ - len = addr_offset + requested_len; + len = (size_t)seg_offset + requested_len; /* update our internal state */ free_memseg[memseg_idx].len -= len; @@ -235,6 +298,7 @@ memzone_reserve_aligned_thread_unsafe(const char *name, uint64_t len, mz->hugepage_sz = free_memseg[memseg_idx].hugepage_sz; mz->socket_id = free_memseg[memseg_idx].socket_id; mz->flags = 0; + mz->memseg_id = memseg_idx; return mz; } @@ -244,7 +308,7 @@ memzone_reserve_aligned_thread_unsafe(const char *name, uint64_t len, * specified alignment). If the allocation cannot be done, return NULL. */ const struct rte_memzone * -rte_memzone_reserve_aligned(const char *name, uint64_t len, +rte_memzone_reserve_aligned(const char *name, size_t len, int socket_id, unsigned flags, unsigned align) { struct rte_mem_config *mcfg; @@ -256,31 +320,51 @@ rte_memzone_reserve_aligned(const char *name, uint64_t len, return NULL; } - /* if alignment is not a power of two */ - if (!rte_is_power_of_2(align)) { - RTE_LOG(ERR, EAL, "%s(): Invalid alignment: %u\n", __func__, - align); + /* get pointer to global configuration */ + mcfg = rte_eal_get_configuration()->mem_config; + + rte_rwlock_write_lock(&mcfg->mlock); + + mz = memzone_reserve_aligned_thread_unsafe( + name, len, socket_id, flags, align, 0); + + rte_rwlock_write_unlock(&mcfg->mlock); + + return mz; +} + +/* + * Return a pointer to a correctly filled memzone descriptor (with a + * specified alignment and boundary). + * If the allocation cannot be done, return NULL. + */ +const struct rte_memzone * +rte_memzone_reserve_bounded(const char *name, size_t len, + int socket_id, unsigned flags, unsigned align, unsigned bound) +{ + struct rte_mem_config *mcfg; + const struct rte_memzone *mz = NULL; + + /* both sizes cannot be explicitly called for */ + if ((flags & RTE_MEMZONE_1GB) && (flags & RTE_MEMZONE_2MB)) { rte_errno = EINVAL; return NULL; } - /* alignment less than cache size is not allowed */ - if (align < CACHE_LINE_SIZE) - align = CACHE_LINE_SIZE; - /* get pointer to global configuration */ mcfg = rte_eal_get_configuration()->mem_config; rte_rwlock_write_lock(&mcfg->mlock); mz = memzone_reserve_aligned_thread_unsafe( - name, len, socket_id, flags, align); + name, len, socket_id, flags, align, bound); rte_rwlock_write_unlock(&mcfg->mlock); return mz; } + /* * Lookup for the memzone identified by the given name */ @@ -316,7 +400,7 @@ rte_memzone_dump(void) for (i=0; imemzone[i].addr == NULL) break; - printf("Zone %o: name:<%s>, phys:0x%"PRIx64", len:0x%"PRIx64"" + printf("Zone %u: name:<%s>, phys:0x%"PRIx64", len:0x%zx" ", virt:%p, socket_id:%"PRId32", flags:%"PRIx32"\n", i, mcfg->memzone[i].name, mcfg->memzone[i].phys_addr, @@ -395,11 +479,17 @@ rte_eal_memzone_init(void) rte_rwlock_write_lock(&mcfg->mlock); - /* duplicate the memsegs from config */ - memcpy(free_memseg, memseg, sizeof(struct rte_memseg) * RTE_MAX_MEMSEG); + /* fill in uninitialized free_memsegs */ + for (i = 0; i < RTE_MAX_MEMSEG; i++) { + if (memseg[i].addr == NULL) + break; + if (free_memseg[i].addr != NULL) + continue; + memcpy(&free_memseg[i], &memseg[i], sizeof(struct rte_memseg)); + } /* make all zones cache-aligned */ - for (i=0; i