[2/3] net/virtio: allocate fake mbuf in Rx queue

Message ID 20201221161456.31696-3-maxime.coquelin@redhat.com (mailing list archive)
State Superseded, archived
Delegated to: Maxime Coquelin
Headers
Series net/virtio: make virtqueue struct cache-friendly |

Checks

Context Check Description
ci/checkpatch warning coding style issues

Commit Message

Maxime Coquelin Dec. 21, 2020, 4:14 p.m. UTC
  While it is worth clarifying whether the fake mbuf
in virtnet_rx struct is really necessary, it is sure
that it heavily impacts cache usage by being part of
the struct. Indeed, it takes uses cachelines, and
requires alignement on a cacheline.

Before this series, it means it took 120 bytes in
virtnet_rx struct:

struct virtnet_rx {
	struct virtqueue *         vq;                   /*     0     8 */

	/* XXX 56 bytes hole, try to pack */

	/* --- cacheline 1 boundary (64 bytes) --- */
	struct rte_mbuf            fake_mbuf __attribute__((__aligned__(64))); /*    64   128 */
	/* --- cacheline 3 boundary (192 bytes) --- */

This patch allocates it using malloc in order to optimize
virtnet_rx cache usage and so virtqueue cache usage.

Signed-off-by: Maxime Coquelin <maxime.coquelin@redhat.com>
---
 drivers/net/virtio/virtio_ethdev.c | 10 ++++++++++
 drivers/net/virtio/virtio_rxtx.c   |  8 +++-----
 drivers/net/virtio/virtio_rxtx.h   |  2 +-
 3 files changed, 14 insertions(+), 6 deletions(-)
  

Comments

Chenbo Xia Jan. 11, 2021, 2:50 a.m. UTC | #1
Hi Maxime,

> -----Original Message-----
> From: Maxime Coquelin <maxime.coquelin@redhat.com>
> Sent: Tuesday, December 22, 2020 12:15 AM
> To: dev@dpdk.org; Xia, Chenbo <chenbo.xia@intel.com>; amorenoz@redhat.com;
> david.marchand@redhat.com; olivier.matz@6wind.com
> Cc: Maxime Coquelin <maxime.coquelin@redhat.com>
> Subject: [PATCH 2/3] net/virtio: allocate fake mbuf in Rx queue
> 
> While it is worth clarifying whether the fake mbuf
> in virtnet_rx struct is really necessary, it is sure
> that it heavily impacts cache usage by being part of
> the struct. Indeed, it takes uses cachelines, and

Did you mean 'uses cachelines'?

> requires alignement on a cacheline.

Alignment?

With above fixed:

Reviewed-by: Chenbo Xia <chenbo.xia@intel.com>

> 
> Before this series, it means it took 120 bytes in
> virtnet_rx struct:
> 
> struct virtnet_rx {
> 	struct virtqueue *         vq;                   /*     0     8 */
> 
> 	/* XXX 56 bytes hole, try to pack */
> 
> 	/* --- cacheline 1 boundary (64 bytes) --- */
> 	struct rte_mbuf            fake_mbuf __attribute__((__aligned__(64)));
> /*    64   128 */
> 	/* --- cacheline 3 boundary (192 bytes) --- */
> 
> This patch allocates it using malloc in order to optimize
> virtnet_rx cache usage and so virtqueue cache usage.
> 
> Signed-off-by: Maxime Coquelin <maxime.coquelin@redhat.com>
> ---
>  drivers/net/virtio/virtio_ethdev.c | 10 ++++++++++
>  drivers/net/virtio/virtio_rxtx.c   |  8 +++-----
>  drivers/net/virtio/virtio_rxtx.h   |  2 +-
>  3 files changed, 14 insertions(+), 6 deletions(-)
> 
> diff --git a/drivers/net/virtio/virtio_ethdev.c
> b/drivers/net/virtio/virtio_ethdev.c
> index 297c01a70d..a1351b36ca 100644
> --- a/drivers/net/virtio/virtio_ethdev.c
> +++ b/drivers/net/virtio/virtio_ethdev.c
> @@ -539,6 +539,7 @@ virtio_init_queue(struct rte_eth_dev *dev, uint16_t
> queue_idx)
>  	}
> 
>  	if (queue_type == VTNET_RQ) {
> +		struct rte_mbuf *fake_mbuf;
>  		size_t sz_sw = (RTE_PMD_VIRTIO_RX_MAX_BURST + vq_size) *
>  			       sizeof(vq->sw_ring[0]);
> 
> @@ -550,10 +551,18 @@ virtio_init_queue(struct rte_eth_dev *dev, uint16_t
> queue_idx)
>  			goto fail_q_alloc;
>  		}
> 
> +		fake_mbuf = malloc(sizeof(*fake_mbuf));
> +		if (!fake_mbuf) {
> +			PMD_INIT_LOG(ERR, "can not allocate fake mbuf");
> +			ret = -ENOMEM;
> +			goto fail_q_alloc;
> +		}
> +
>  		vq->sw_ring = sw_ring;
>  		rxvq = &vq->rxq;
>  		rxvq->port_id = dev->data->port_id;
>  		rxvq->mz = mz;
> +		rxvq->fake_mbuf = fake_mbuf;
>  	} else if (queue_type == VTNET_TQ) {
>  		txvq = &vq->txq;
>  		txvq->port_id = dev->data->port_id;
> @@ -636,6 +645,7 @@ virtio_free_queues(struct virtio_hw *hw)
> 
>  		queue_type = virtio_get_queue_type(hw, i);
>  		if (queue_type == VTNET_RQ) {
> +			free(vq->rxq.fake_mbuf);
>  			rte_free(vq->sw_ring);
>  			rte_memzone_free(vq->rxq.mz);
>  		} else if (queue_type == VTNET_TQ) {
> diff --git a/drivers/net/virtio/virtio_rxtx.c
> b/drivers/net/virtio/virtio_rxtx.c
> index 1fcce36cbd..d147d7300a 100644
> --- a/drivers/net/virtio/virtio_rxtx.c
> +++ b/drivers/net/virtio/virtio_rxtx.c
> @@ -703,11 +703,9 @@ virtio_dev_rx_queue_setup_finish(struct rte_eth_dev *dev,
> uint16_t queue_idx)
>  		virtio_rxq_vec_setup(rxvq);
>  	}
> 
> -	memset(&rxvq->fake_mbuf, 0, sizeof(rxvq->fake_mbuf));
> -	for (desc_idx = 0; desc_idx < RTE_PMD_VIRTIO_RX_MAX_BURST;
> -	     desc_idx++) {
> -		vq->sw_ring[vq->vq_nentries + desc_idx] =
> -			&rxvq->fake_mbuf;
> +	memset(rxvq->fake_mbuf, 0, sizeof(*rxvq->fake_mbuf));
> +	for (desc_idx = 0; desc_idx < RTE_PMD_VIRTIO_RX_MAX_BURST; desc_idx++) {
> +		vq->sw_ring[vq->vq_nentries + desc_idx] = rxvq->fake_mbuf;
>  	}
> 
>  	if (hw->use_vec_rx && !virtio_with_packed_queue(hw)) {
> diff --git a/drivers/net/virtio/virtio_rxtx.h
> b/drivers/net/virtio/virtio_rxtx.h
> index 7f1036be6f..6ce5d67d15 100644
> --- a/drivers/net/virtio/virtio_rxtx.h
> +++ b/drivers/net/virtio/virtio_rxtx.h
> @@ -19,7 +19,7 @@ struct virtnet_stats {
> 
>  struct virtnet_rx {
>  	/* dummy mbuf, for wraparound when processing RX ring. */
> -	struct rte_mbuf fake_mbuf;
> +	struct rte_mbuf *fake_mbuf;
>  	uint64_t mbuf_initializer; /**< value to init mbufs. */
>  	struct rte_mempool *mpool; /**< mempool for mbuf allocation */
> 
> --
> 2.29.2
  
Chenbo Xia Jan. 11, 2021, 5:39 a.m. UTC | #2
Hi Maxime,

> -----Original Message-----
> From: Maxime Coquelin <maxime.coquelin@redhat.com>
> Sent: Tuesday, December 22, 2020 12:15 AM
> To: dev@dpdk.org; Xia, Chenbo <chenbo.xia@intel.com>; amorenoz@redhat.com;
> david.marchand@redhat.com; olivier.matz@6wind.com
> Cc: Maxime Coquelin <maxime.coquelin@redhat.com>
> Subject: [PATCH 2/3] net/virtio: allocate fake mbuf in Rx queue
> 
> While it is worth clarifying whether the fake mbuf
> in virtnet_rx struct is really necessary, it is sure
> that it heavily impacts cache usage by being part of
> the struct. Indeed, it takes uses cachelines, and
> requires alignement on a cacheline.
> 
> Before this series, it means it took 120 bytes in
> virtnet_rx struct:
> 
> struct virtnet_rx {
> 	struct virtqueue *         vq;                   /*     0     8 */
> 
> 	/* XXX 56 bytes hole, try to pack */
> 
> 	/* --- cacheline 1 boundary (64 bytes) --- */
> 	struct rte_mbuf            fake_mbuf __attribute__((__aligned__(64)));
> /*    64   128 */
> 	/* --- cacheline 3 boundary (192 bytes) --- */
> 
> This patch allocates it using malloc in order to optimize
> virtnet_rx cache usage and so virtqueue cache usage.
> 
> Signed-off-by: Maxime Coquelin <maxime.coquelin@redhat.com>
> ---
>  drivers/net/virtio/virtio_ethdev.c | 10 ++++++++++
>  drivers/net/virtio/virtio_rxtx.c   |  8 +++-----
>  drivers/net/virtio/virtio_rxtx.h   |  2 +-
>  3 files changed, 14 insertions(+), 6 deletions(-)
> 
> diff --git a/drivers/net/virtio/virtio_ethdev.c
> b/drivers/net/virtio/virtio_ethdev.c
> index 297c01a70d..a1351b36ca 100644
> --- a/drivers/net/virtio/virtio_ethdev.c
> +++ b/drivers/net/virtio/virtio_ethdev.c
> @@ -539,6 +539,7 @@ virtio_init_queue(struct rte_eth_dev *dev, uint16_t
> queue_idx)
>  	}
> 
>  	if (queue_type == VTNET_RQ) {
> +		struct rte_mbuf *fake_mbuf;
>  		size_t sz_sw = (RTE_PMD_VIRTIO_RX_MAX_BURST + vq_size) *
>  			       sizeof(vq->sw_ring[0]);
> 
> @@ -550,10 +551,18 @@ virtio_init_queue(struct rte_eth_dev *dev, uint16_t
> queue_idx)
>  			goto fail_q_alloc;
>  		}
> 
> +		fake_mbuf = malloc(sizeof(*fake_mbuf));
> +		if (!fake_mbuf) {
> +			PMD_INIT_LOG(ERR, "can not allocate fake mbuf");
> +			ret = -ENOMEM;
> +			goto fail_q_alloc;
> +		}
> +
>  		vq->sw_ring = sw_ring;
>  		rxvq = &vq->rxq;
>  		rxvq->port_id = dev->data->port_id;
>  		rxvq->mz = mz;
> +		rxvq->fake_mbuf = fake_mbuf;
>  	} else if (queue_type == VTNET_TQ) {
>  		txvq = &vq->txq;
>  		txvq->port_id = dev->data->port_id;
> @@ -636,6 +645,7 @@ virtio_free_queues(struct virtio_hw *hw)
> 
>  		queue_type = virtio_get_queue_type(hw, i);
>  		if (queue_type == VTNET_RQ) {
> +			free(vq->rxq.fake_mbuf);

After thinking about this again, although you add the free of fake mbuf
here, it's better to add free in virtio_init_queue too after fail_q_alloc.
And when setup_queue(hw, vq) fails, it's better to goto fail_q_alloc to 
free fake mbuf. Now it will not memory leak as we use virtio_free_queues when
virtio_alloc_queues fails. But inside virtio_init_queue, it's better to
handle the errors well.. If you agree with above, it may also be good to
change the name 'fail_q_alloc' since now it may also fail when setting up
queues.

Sorry for an extra email about this...

Thanks,
Chenbo

>  			rte_free(vq->sw_ring);
>  			rte_memzone_free(vq->rxq.mz);
>  		} else if (queue_type == VTNET_TQ) {
> diff --git a/drivers/net/virtio/virtio_rxtx.c
> b/drivers/net/virtio/virtio_rxtx.c
> index 1fcce36cbd..d147d7300a 100644
> --- a/drivers/net/virtio/virtio_rxtx.c
> +++ b/drivers/net/virtio/virtio_rxtx.c
> @@ -703,11 +703,9 @@ virtio_dev_rx_queue_setup_finish(struct rte_eth_dev *dev,
> uint16_t queue_idx)
>  		virtio_rxq_vec_setup(rxvq);
>  	}
> 
> -	memset(&rxvq->fake_mbuf, 0, sizeof(rxvq->fake_mbuf));
> -	for (desc_idx = 0; desc_idx < RTE_PMD_VIRTIO_RX_MAX_BURST;
> -	     desc_idx++) {
> -		vq->sw_ring[vq->vq_nentries + desc_idx] =
> -			&rxvq->fake_mbuf;
> +	memset(rxvq->fake_mbuf, 0, sizeof(*rxvq->fake_mbuf));
> +	for (desc_idx = 0; desc_idx < RTE_PMD_VIRTIO_RX_MAX_BURST; desc_idx++) {
> +		vq->sw_ring[vq->vq_nentries + desc_idx] = rxvq->fake_mbuf;
>  	}
> 
>  	if (hw->use_vec_rx && !virtio_with_packed_queue(hw)) {
> diff --git a/drivers/net/virtio/virtio_rxtx.h
> b/drivers/net/virtio/virtio_rxtx.h
> index 7f1036be6f..6ce5d67d15 100644
> --- a/drivers/net/virtio/virtio_rxtx.h
> +++ b/drivers/net/virtio/virtio_rxtx.h
> @@ -19,7 +19,7 @@ struct virtnet_stats {
> 
>  struct virtnet_rx {
>  	/* dummy mbuf, for wraparound when processing RX ring. */
> -	struct rte_mbuf fake_mbuf;
> +	struct rte_mbuf *fake_mbuf;
>  	uint64_t mbuf_initializer; /**< value to init mbufs. */
>  	struct rte_mempool *mpool; /**< mempool for mbuf allocation */
> 
> --
> 2.29.2
  
Maxime Coquelin March 15, 2021, 11:29 a.m. UTC | #3
On 1/11/21 3:50 AM, Xia, Chenbo wrote:
> Hi Maxime,
> 
>> -----Original Message-----
>> From: Maxime Coquelin <maxime.coquelin@redhat.com>
>> Sent: Tuesday, December 22, 2020 12:15 AM
>> To: dev@dpdk.org; Xia, Chenbo <chenbo.xia@intel.com>; amorenoz@redhat.com;
>> david.marchand@redhat.com; olivier.matz@6wind.com
>> Cc: Maxime Coquelin <maxime.coquelin@redhat.com>
>> Subject: [PATCH 2/3] net/virtio: allocate fake mbuf in Rx queue
>>
>> While it is worth clarifying whether the fake mbuf
>> in virtnet_rx struct is really necessary, it is sure
>> that it heavily impacts cache usage by being part of
>> the struct. Indeed, it takes uses cachelines, and
> 
> Did you mean 'uses cachelines'?

I don't know what I meant here :) I will rework it!

>> requires alignement on a cacheline.
> 
> Alignment?
> 
> With above fixed:
> 
> Reviewed-by: Chenbo Xia <chenbo.xia@intel.com>
> 
>>
>> Before this series, it means it took 120 bytes in
>> virtnet_rx struct:
>>
>> struct virtnet_rx {
>> 	struct virtqueue *         vq;                   /*     0     8 */
>>
>> 	/* XXX 56 bytes hole, try to pack */
>>
>> 	/* --- cacheline 1 boundary (64 bytes) --- */
>> 	struct rte_mbuf            fake_mbuf __attribute__((__aligned__(64)));
>> /*    64   128 */
>> 	/* --- cacheline 3 boundary (192 bytes) --- */
>>
>> This patch allocates it using malloc in order to optimize
>> virtnet_rx cache usage and so virtqueue cache usage.
>>
>> Signed-off-by: Maxime Coquelin <maxime.coquelin@redhat.com>
>> ---
>>  drivers/net/virtio/virtio_ethdev.c | 10 ++++++++++
>>  drivers/net/virtio/virtio_rxtx.c   |  8 +++-----
>>  drivers/net/virtio/virtio_rxtx.h   |  2 +-
>>  3 files changed, 14 insertions(+), 6 deletions(-)
>>
>> diff --git a/drivers/net/virtio/virtio_ethdev.c
>> b/drivers/net/virtio/virtio_ethdev.c
>> index 297c01a70d..a1351b36ca 100644
>> --- a/drivers/net/virtio/virtio_ethdev.c
>> +++ b/drivers/net/virtio/virtio_ethdev.c
>> @@ -539,6 +539,7 @@ virtio_init_queue(struct rte_eth_dev *dev, uint16_t
>> queue_idx)
>>  	}
>>
>>  	if (queue_type == VTNET_RQ) {
>> +		struct rte_mbuf *fake_mbuf;
>>  		size_t sz_sw = (RTE_PMD_VIRTIO_RX_MAX_BURST + vq_size) *
>>  			       sizeof(vq->sw_ring[0]);
>>
>> @@ -550,10 +551,18 @@ virtio_init_queue(struct rte_eth_dev *dev, uint16_t
>> queue_idx)
>>  			goto fail_q_alloc;
>>  		}
>>
>> +		fake_mbuf = malloc(sizeof(*fake_mbuf));
>> +		if (!fake_mbuf) {
>> +			PMD_INIT_LOG(ERR, "can not allocate fake mbuf");
>> +			ret = -ENOMEM;
>> +			goto fail_q_alloc;
>> +		}
>> +
>>  		vq->sw_ring = sw_ring;
>>  		rxvq = &vq->rxq;
>>  		rxvq->port_id = dev->data->port_id;
>>  		rxvq->mz = mz;
>> +		rxvq->fake_mbuf = fake_mbuf;
>>  	} else if (queue_type == VTNET_TQ) {
>>  		txvq = &vq->txq;
>>  		txvq->port_id = dev->data->port_id;
>> @@ -636,6 +645,7 @@ virtio_free_queues(struct virtio_hw *hw)
>>
>>  		queue_type = virtio_get_queue_type(hw, i);
>>  		if (queue_type == VTNET_RQ) {
>> +			free(vq->rxq.fake_mbuf);
>>  			rte_free(vq->sw_ring);
>>  			rte_memzone_free(vq->rxq.mz);
>>  		} else if (queue_type == VTNET_TQ) {
>> diff --git a/drivers/net/virtio/virtio_rxtx.c
>> b/drivers/net/virtio/virtio_rxtx.c
>> index 1fcce36cbd..d147d7300a 100644
>> --- a/drivers/net/virtio/virtio_rxtx.c
>> +++ b/drivers/net/virtio/virtio_rxtx.c
>> @@ -703,11 +703,9 @@ virtio_dev_rx_queue_setup_finish(struct rte_eth_dev *dev,
>> uint16_t queue_idx)
>>  		virtio_rxq_vec_setup(rxvq);
>>  	}
>>
>> -	memset(&rxvq->fake_mbuf, 0, sizeof(rxvq->fake_mbuf));
>> -	for (desc_idx = 0; desc_idx < RTE_PMD_VIRTIO_RX_MAX_BURST;
>> -	     desc_idx++) {
>> -		vq->sw_ring[vq->vq_nentries + desc_idx] =
>> -			&rxvq->fake_mbuf;
>> +	memset(rxvq->fake_mbuf, 0, sizeof(*rxvq->fake_mbuf));
>> +	for (desc_idx = 0; desc_idx < RTE_PMD_VIRTIO_RX_MAX_BURST; desc_idx++) {
>> +		vq->sw_ring[vq->vq_nentries + desc_idx] = rxvq->fake_mbuf;
>>  	}
>>
>>  	if (hw->use_vec_rx && !virtio_with_packed_queue(hw)) {
>> diff --git a/drivers/net/virtio/virtio_rxtx.h
>> b/drivers/net/virtio/virtio_rxtx.h
>> index 7f1036be6f..6ce5d67d15 100644
>> --- a/drivers/net/virtio/virtio_rxtx.h
>> +++ b/drivers/net/virtio/virtio_rxtx.h
>> @@ -19,7 +19,7 @@ struct virtnet_stats {
>>
>>  struct virtnet_rx {
>>  	/* dummy mbuf, for wraparound when processing RX ring. */
>> -	struct rte_mbuf fake_mbuf;
>> +	struct rte_mbuf *fake_mbuf;
>>  	uint64_t mbuf_initializer; /**< value to init mbufs. */
>>  	struct rte_mempool *mpool; /**< mempool for mbuf allocation */
>>
>> --
>> 2.29.2
>
  
Maxime Coquelin March 15, 2021, 1:46 p.m. UTC | #4
On 1/11/21 6:39 AM, Xia, Chenbo wrote:
> Hi Maxime,
> 
>> -----Original Message-----
>> From: Maxime Coquelin <maxime.coquelin@redhat.com>
>> Sent: Tuesday, December 22, 2020 12:15 AM
>> To: dev@dpdk.org; Xia, Chenbo <chenbo.xia@intel.com>; amorenoz@redhat.com;
>> david.marchand@redhat.com; olivier.matz@6wind.com
>> Cc: Maxime Coquelin <maxime.coquelin@redhat.com>
>> Subject: [PATCH 2/3] net/virtio: allocate fake mbuf in Rx queue
>>
>> While it is worth clarifying whether the fake mbuf
>> in virtnet_rx struct is really necessary, it is sure
>> that it heavily impacts cache usage by being part of
>> the struct. Indeed, it takes uses cachelines, and
>> requires alignement on a cacheline.
>>
>> Before this series, it means it took 120 bytes in
>> virtnet_rx struct:
>>
>> struct virtnet_rx {
>> 	struct virtqueue *         vq;                   /*     0     8 */
>>
>> 	/* XXX 56 bytes hole, try to pack */
>>
>> 	/* --- cacheline 1 boundary (64 bytes) --- */
>> 	struct rte_mbuf            fake_mbuf __attribute__((__aligned__(64)));
>> /*    64   128 */
>> 	/* --- cacheline 3 boundary (192 bytes) --- */
>>
>> This patch allocates it using malloc in order to optimize
>> virtnet_rx cache usage and so virtqueue cache usage.
>>
>> Signed-off-by: Maxime Coquelin <maxime.coquelin@redhat.com>
>> ---
>>  drivers/net/virtio/virtio_ethdev.c | 10 ++++++++++
>>  drivers/net/virtio/virtio_rxtx.c   |  8 +++-----
>>  drivers/net/virtio/virtio_rxtx.h   |  2 +-
>>  3 files changed, 14 insertions(+), 6 deletions(-)
>>
>> diff --git a/drivers/net/virtio/virtio_ethdev.c
>> b/drivers/net/virtio/virtio_ethdev.c
>> index 297c01a70d..a1351b36ca 100644
>> --- a/drivers/net/virtio/virtio_ethdev.c
>> +++ b/drivers/net/virtio/virtio_ethdev.c
>> @@ -539,6 +539,7 @@ virtio_init_queue(struct rte_eth_dev *dev, uint16_t
>> queue_idx)
>>  	}
>>
>>  	if (queue_type == VTNET_RQ) {
>> +		struct rte_mbuf *fake_mbuf;
>>  		size_t sz_sw = (RTE_PMD_VIRTIO_RX_MAX_BURST + vq_size) *
>>  			       sizeof(vq->sw_ring[0]);
>>
>> @@ -550,10 +551,18 @@ virtio_init_queue(struct rte_eth_dev *dev, uint16_t
>> queue_idx)
>>  			goto fail_q_alloc;
>>  		}
>>
>> +		fake_mbuf = malloc(sizeof(*fake_mbuf));
>> +		if (!fake_mbuf) {
>> +			PMD_INIT_LOG(ERR, "can not allocate fake mbuf");
>> +			ret = -ENOMEM;
>> +			goto fail_q_alloc;
>> +		}
>> +
>>  		vq->sw_ring = sw_ring;
>>  		rxvq = &vq->rxq;
>>  		rxvq->port_id = dev->data->port_id;
>>  		rxvq->mz = mz;
>> +		rxvq->fake_mbuf = fake_mbuf;
>>  	} else if (queue_type == VTNET_TQ) {
>>  		txvq = &vq->txq;
>>  		txvq->port_id = dev->data->port_id;
>> @@ -636,6 +645,7 @@ virtio_free_queues(struct virtio_hw *hw)
>>
>>  		queue_type = virtio_get_queue_type(hw, i);
>>  		if (queue_type == VTNET_RQ) {
>> +			free(vq->rxq.fake_mbuf);
> 
> After thinking about this again, although you add the free of fake mbuf
> here, it's better to add free in virtio_init_queue too after fail_q_alloc.
> And when setup_queue(hw, vq) fails, it's better to goto fail_q_alloc to 
> free fake mbuf. Now it will not memory leak as we use virtio_free_queues when
> virtio_alloc_queues fails. But inside virtio_init_queue, it's better to
> handle the errors well.. If you agree with above, it may also be good to
> change the name 'fail_q_alloc' since now it may also fail when setting up
> queues.


The error path indeed needs some rework.
I will add a preliminary patch to rework it before this patch is
applied.

> Sorry for an extra email about this...

No worries, that's much appreciated!

Thanks,
Maxime

> Thanks,
> Chenbo
> 
>>  			rte_free(vq->sw_ring);
>>  			rte_memzone_free(vq->rxq.mz);
>>  		} else if (queue_type == VTNET_TQ) {
>> diff --git a/drivers/net/virtio/virtio_rxtx.c
>> b/drivers/net/virtio/virtio_rxtx.c
>> index 1fcce36cbd..d147d7300a 100644
>> --- a/drivers/net/virtio/virtio_rxtx.c
>> +++ b/drivers/net/virtio/virtio_rxtx.c
>> @@ -703,11 +703,9 @@ virtio_dev_rx_queue_setup_finish(struct rte_eth_dev *dev,
>> uint16_t queue_idx)
>>  		virtio_rxq_vec_setup(rxvq);
>>  	}
>>
>> -	memset(&rxvq->fake_mbuf, 0, sizeof(rxvq->fake_mbuf));
>> -	for (desc_idx = 0; desc_idx < RTE_PMD_VIRTIO_RX_MAX_BURST;
>> -	     desc_idx++) {
>> -		vq->sw_ring[vq->vq_nentries + desc_idx] =
>> -			&rxvq->fake_mbuf;
>> +	memset(rxvq->fake_mbuf, 0, sizeof(*rxvq->fake_mbuf));
>> +	for (desc_idx = 0; desc_idx < RTE_PMD_VIRTIO_RX_MAX_BURST; desc_idx++) {
>> +		vq->sw_ring[vq->vq_nentries + desc_idx] = rxvq->fake_mbuf;
>>  	}
>>
>>  	if (hw->use_vec_rx && !virtio_with_packed_queue(hw)) {
>> diff --git a/drivers/net/virtio/virtio_rxtx.h
>> b/drivers/net/virtio/virtio_rxtx.h
>> index 7f1036be6f..6ce5d67d15 100644
>> --- a/drivers/net/virtio/virtio_rxtx.h
>> +++ b/drivers/net/virtio/virtio_rxtx.h
>> @@ -19,7 +19,7 @@ struct virtnet_stats {
>>
>>  struct virtnet_rx {
>>  	/* dummy mbuf, for wraparound when processing RX ring. */
>> -	struct rte_mbuf fake_mbuf;
>> +	struct rte_mbuf *fake_mbuf;
>>  	uint64_t mbuf_initializer; /**< value to init mbufs. */
>>  	struct rte_mempool *mpool; /**< mempool for mbuf allocation */
>>
>> --
>> 2.29.2
>
  

Patch

diff --git a/drivers/net/virtio/virtio_ethdev.c b/drivers/net/virtio/virtio_ethdev.c
index 297c01a70d..a1351b36ca 100644
--- a/drivers/net/virtio/virtio_ethdev.c
+++ b/drivers/net/virtio/virtio_ethdev.c
@@ -539,6 +539,7 @@  virtio_init_queue(struct rte_eth_dev *dev, uint16_t queue_idx)
 	}
 
 	if (queue_type == VTNET_RQ) {
+		struct rte_mbuf *fake_mbuf;
 		size_t sz_sw = (RTE_PMD_VIRTIO_RX_MAX_BURST + vq_size) *
 			       sizeof(vq->sw_ring[0]);
 
@@ -550,10 +551,18 @@  virtio_init_queue(struct rte_eth_dev *dev, uint16_t queue_idx)
 			goto fail_q_alloc;
 		}
 
+		fake_mbuf = malloc(sizeof(*fake_mbuf));
+		if (!fake_mbuf) {
+			PMD_INIT_LOG(ERR, "can not allocate fake mbuf");
+			ret = -ENOMEM;
+			goto fail_q_alloc;
+		}
+
 		vq->sw_ring = sw_ring;
 		rxvq = &vq->rxq;
 		rxvq->port_id = dev->data->port_id;
 		rxvq->mz = mz;
+		rxvq->fake_mbuf = fake_mbuf;
 	} else if (queue_type == VTNET_TQ) {
 		txvq = &vq->txq;
 		txvq->port_id = dev->data->port_id;
@@ -636,6 +645,7 @@  virtio_free_queues(struct virtio_hw *hw)
 
 		queue_type = virtio_get_queue_type(hw, i);
 		if (queue_type == VTNET_RQ) {
+			free(vq->rxq.fake_mbuf);
 			rte_free(vq->sw_ring);
 			rte_memzone_free(vq->rxq.mz);
 		} else if (queue_type == VTNET_TQ) {
diff --git a/drivers/net/virtio/virtio_rxtx.c b/drivers/net/virtio/virtio_rxtx.c
index 1fcce36cbd..d147d7300a 100644
--- a/drivers/net/virtio/virtio_rxtx.c
+++ b/drivers/net/virtio/virtio_rxtx.c
@@ -703,11 +703,9 @@  virtio_dev_rx_queue_setup_finish(struct rte_eth_dev *dev, uint16_t queue_idx)
 		virtio_rxq_vec_setup(rxvq);
 	}
 
-	memset(&rxvq->fake_mbuf, 0, sizeof(rxvq->fake_mbuf));
-	for (desc_idx = 0; desc_idx < RTE_PMD_VIRTIO_RX_MAX_BURST;
-	     desc_idx++) {
-		vq->sw_ring[vq->vq_nentries + desc_idx] =
-			&rxvq->fake_mbuf;
+	memset(rxvq->fake_mbuf, 0, sizeof(*rxvq->fake_mbuf));
+	for (desc_idx = 0; desc_idx < RTE_PMD_VIRTIO_RX_MAX_BURST; desc_idx++) {
+		vq->sw_ring[vq->vq_nentries + desc_idx] = rxvq->fake_mbuf;
 	}
 
 	if (hw->use_vec_rx && !virtio_with_packed_queue(hw)) {
diff --git a/drivers/net/virtio/virtio_rxtx.h b/drivers/net/virtio/virtio_rxtx.h
index 7f1036be6f..6ce5d67d15 100644
--- a/drivers/net/virtio/virtio_rxtx.h
+++ b/drivers/net/virtio/virtio_rxtx.h
@@ -19,7 +19,7 @@  struct virtnet_stats {
 
 struct virtnet_rx {
 	/* dummy mbuf, for wraparound when processing RX ring. */
-	struct rte_mbuf fake_mbuf;
+	struct rte_mbuf *fake_mbuf;
 	uint64_t mbuf_initializer; /**< value to init mbufs. */
 	struct rte_mempool *mpool; /**< mempool for mbuf allocation */