virtio: fix memory leak of virtqueue memzones
authorJianfeng Tan <jianfeng.tan@intel.com>
Fri, 29 Apr 2016 00:48:46 +0000 (00:48 +0000)
committerYuanhan Liu <yuanhan.liu@linux.intel.com>
Tue, 10 May 2016 18:22:39 +0000 (11:22 -0700)
When virtio was proposed in DPDK, there is no API to free memzones.
But this has changed since rte_memzone_free() has been implemented by
commit ff909fe21f0a ("mem: introduce memzone freeing").

This patch is to make sure memzones in struct virtqueue, like mz and
virtio_net_hdr_mz, are freed when queue is released or setup fails.

Fixes: c1f86306a026 ("virtio: add new driver")

Signed-off-by: Jianfeng Tan <jianfeng.tan@intel.com>
Acked-by: Yuanhan Liu <yuanhan.liu@linux.intel.com>
drivers/net/virtio/virtio_ethdev.c
drivers/net/virtio/virtqueue.h

index ddc1b2b..c3fb628 100644 (file)
@@ -260,12 +260,18 @@ virtio_set_multiple_queues(struct rte_eth_dev *dev, uint16_t nb_queues)
 }
 
 void
-virtio_dev_queue_release(struct virtqueue *vq) {
+virtio_dev_queue_release(struct virtqueue *vq)
+{
        struct virtio_hw *hw;
 
        if (vq) {
                hw = vq->hw;
-               hw->vtpci_ops->del_queue(hw, vq);
+               if (vq->configured)
+                       hw->vtpci_ops->del_queue(hw, vq);
+
+               rte_memzone_free(vq->mz);
+               if (vq->virtio_net_hdr_mz)
+                       rte_memzone_free(vq->virtio_net_hdr_mz);
 
                rte_free(vq->sw_ring);
                rte_free(vq);
@@ -325,7 +331,7 @@ int virtio_dev_queue_setup(struct rte_eth_dev *dev,
                                                 socket_id);
                if (!vq->sw_ring) {
                        PMD_INIT_LOG(ERR, "Can not allocate RX soft ring");
-                       rte_free(vq);
+                       virtio_dev_queue_release(vq);
                        return -ENOMEM;
                }
        }
@@ -353,7 +359,7 @@ int virtio_dev_queue_setup(struct rte_eth_dev *dev,
                if (rte_errno == EEXIST)
                        mz = rte_memzone_lookup(vq_name);
                if (mz == NULL) {
-                       rte_free(vq);
+                       virtio_dev_queue_release(vq);
                        return -ENOMEM;
                }
        }
@@ -365,7 +371,7 @@ int virtio_dev_queue_setup(struct rte_eth_dev *dev,
         */
        if ((mz->phys_addr + vq->vq_ring_size - 1) >> (VIRTIO_PCI_QUEUE_ADDR_SHIFT + 32)) {
                PMD_INIT_LOG(ERR, "vring address shouldn't be above 16TB!");
-               rte_free(vq);
+               virtio_dev_queue_release(vq);
                return -ENOMEM;
        }
 
@@ -397,7 +403,7 @@ int virtio_dev_queue_setup(struct rte_eth_dev *dev,
                        if (rte_errno == EEXIST)
                                hdr_mz = rte_memzone_lookup(vq_name);
                        if (hdr_mz == NULL) {
-                               rte_free(vq);
+                               virtio_dev_queue_release(vq);
                                return -ENOMEM;
                        }
                }
@@ -431,7 +437,7 @@ int virtio_dev_queue_setup(struct rte_eth_dev *dev,
                                vq->virtio_net_hdr_mz =
                                        rte_memzone_lookup(vq_name);
                        if (vq->virtio_net_hdr_mz == NULL) {
-                               rte_free(vq);
+                               virtio_dev_queue_release(vq);
                                return -ENOMEM;
                        }
                }
@@ -442,6 +448,7 @@ int virtio_dev_queue_setup(struct rte_eth_dev *dev,
 
        hw->vtpci_ops->setup_queue(hw, vq);
 
+       vq->configured = 1;
        *pvq = vq;
        return 0;
 }
index 8c46a83..4e543d2 100644 (file)
@@ -201,6 +201,8 @@ struct virtqueue {
 
        uint16_t        *notify_addr;
 
+       int             configured;
+
        struct vq_desc_extra {
                void              *cookie;
                uint16_t          ndescs;