alloc largest
authorOlivier Matz <olivier.matz@6wind.com>
Wed, 8 Jan 2020 15:19:15 +0000 (16:19 +0100)
committerOlivier Matz <olivier.matz@6wind.com>
Wed, 8 Jan 2020 16:14:42 +0000 (17:14 +0100)
lib/librte_mempool/rte_mempool.c

index b05b417..1cb19f4 100644 (file)
@@ -480,6 +480,7 @@ rte_mempool_populate_default(struct rte_mempool *mp)
        unsigned mz_id, n;
        int ret;
        bool need_iova_contig_obj;
+       size_t max_alloc_size = SIZE_MAX;
 
        ret = mempool_ops_alloc_once(mp);
        if (ret != 0)
@@ -559,29 +560,32 @@ rte_mempool_populate_default(struct rte_mempool *mp)
                if (min_chunk_size == (size_t)mem_size)
                        mz_flags |= RTE_MEMZONE_IOVA_CONTIG;
 
-               mz = rte_memzone_reserve_aligned(mz_name, mem_size,
+               /* Allocate a memzone, reducing size be 2 on failure */
+               do {
+                       mz = rte_memzone_reserve_aligned(
+                               mz_name, RTE_MIN((size_t)mem_size, max_alloc_size),
                                mp->socket_id, mz_flags, align);
 
-               /* don't try reserving with 0 size if we were asked to reserve
-                * IOVA-contiguous memory.
-                */
-               if (min_chunk_size < (size_t)mem_size && mz == NULL) {
-                       /* not enough memory, retry with the biggest zone we
-                        * have
+                       /* don't try with a smaller size if we were asked to
+                        * reserve IOVA-contiguous memory.
                         */
-                       mz = rte_memzone_reserve_aligned(mz_name, 0,
-                                       mp->socket_id, mz_flags, align);
-               }
-               if (mz == NULL) {
-                       ret = -rte_errno;
-                       goto fail;
-               }
+                       if (min_chunk_size >= (size_t)mem_size && mz == NULL) {
+                               ret = -rte_errno;
+                               goto fail;
+                       }
 
-               if (mz->len < min_chunk_size) {
-                       rte_memzone_free(mz);
-                       ret = -ENOMEM;
-                       goto fail;
-               }
+                       if (mz == NULL && rte_errno == ENOMEM) {
+                               max_alloc_size = RTE_MIN(max_alloc_size,
+                                                       (size_t)mem_size) / 2;
+                               if (max_alloc_size < min_chunk_size) {
+                                       ret = -ENOMEM;
+                                       goto fail;
+                               }
+                       } else if (mz == NULL) {
+                               ret = -rte_errno;
+                               goto fail;
+                       }
+               } while (mz == NULL);
 
                if (need_iova_contig_obj)
                        iova = mz->iova;