net/ena/base: prevent allocation of zero sized memory
authorIgor Chauskin <igorch@amazon.com>
Wed, 8 Apr 2020 08:28:54 +0000 (10:28 +0200)
committerFerruh Yigit <ferruh.yigit@intel.com>
Tue, 21 Apr 2020 11:57:06 +0000 (13:57 +0200)
rte_memzone_reserve() will reserve the biggest contiguous memzone
available if received 0 as size param.

Fixes: 9ba7981ec992 ("ena: add communication layer for DPDK")
Cc: stable@dpdk.org
Signed-off-by: Igor Chauskin <igorch@amazon.com>
Reviewed-by: Michal Krawczyk <mk@semihalf.com>
Reviewed-by: Guy Tzalik <gtzalik@amazon.com>
drivers/net/ena/base/ena_plat_dpdk.h

index 70261bd..4b8fe01 100644 (file)
@@ -184,15 +184,18 @@ extern rte_atomic32_t ena_alloc_cnt;
 
 #define ENA_MEM_ALLOC_COHERENT(dmadev, size, virt, phys, handle)       \
        do {                                                            \
-               const struct rte_memzone *mz;                           \
-               char z_name[RTE_MEMZONE_NAMESIZE];                      \
+               const struct rte_memzone *mz = NULL;                    \
                ENA_TOUCH(dmadev); ENA_TOUCH(handle);                   \
-               snprintf(z_name, sizeof(z_name),                        \
+               if (size > 0) {                                         \
+                       char z_name[RTE_MEMZONE_NAMESIZE];              \
+                       snprintf(z_name, sizeof(z_name),                \
                         "ena_alloc_%d",                                \
                         rte_atomic32_add_return(&ena_alloc_cnt, 1));   \
-               mz = rte_memzone_reserve(z_name, size, SOCKET_ID_ANY,   \
-                               RTE_MEMZONE_IOVA_CONTIG);               \
-               handle = mz;                                            \
+                       mz = rte_memzone_reserve(z_name, size,          \
+                                       SOCKET_ID_ANY,                  \
+                                       RTE_MEMZONE_IOVA_CONTIG);       \
+                       handle = mz;                                    \
+               }                                                       \
                if (mz == NULL) {                                       \
                        virt = NULL;                                    \
                        phys = 0;                                       \
@@ -210,15 +213,17 @@ extern rte_atomic32_t ena_alloc_cnt;
 #define ENA_MEM_ALLOC_COHERENT_NODE(                                   \
        dmadev, size, virt, phys, mem_handle, node, dev_node)           \
        do {                                                            \
-               const struct rte_memzone *mz;                           \
-               char z_name[RTE_MEMZONE_NAMESIZE];                      \
+               const struct rte_memzone *mz = NULL;                    \
                ENA_TOUCH(dmadev); ENA_TOUCH(dev_node);                 \
-               snprintf(z_name, sizeof(z_name),                        \
+               if (size > 0) {                                         \
+                       char z_name[RTE_MEMZONE_NAMESIZE];              \
+                       snprintf(z_name, sizeof(z_name),                \
                         "ena_alloc_%d",                                \
-                        rte_atomic32_add_return(&ena_alloc_cnt, 1));   \
-               mz = rte_memzone_reserve(z_name, size, node,            \
+                        rte_atomic32_add_return(&ena_alloc_cnt, 1));   \
+                       mz = rte_memzone_reserve(z_name, size, node,    \
                                RTE_MEMZONE_IOVA_CONTIG);               \
-               mem_handle = mz;                                        \
+                       mem_handle = mz;                                \
+               }                                                       \
                if (mz == NULL) {                                       \
                        virt = NULL;                                    \
                        phys = 0;                                       \