All of lore.kernel.org
 help / color / mirror / Atom feed
From: Max Gurtovoy <mgurtovoy@nvidia.com>
To: guodeqing <geffrey.guo@huawei.com>, <mst@redhat.com>
Cc: <jasowang@redhat.com>, <davem@davemloft.net>, <kuba@kernel.org>,
	<virtualization@lists.linux-foundation.org>,
	<netdev@vger.kernel.org>
Subject: Re: [PATCH] virtio-net: fix the kzalloc/kfree mismatch problem
Date: Sun, 23 May 2021 10:24:34 +0300	[thread overview]
Message-ID: <6f7b729d-38df-9bf8-f023-bc1986da9a9e@nvidia.com> (raw)
In-Reply-To: <20210522080231.54760-1-geffrey.guo@huawei.com>


On 5/22/2021 11:02 AM, guodeqing wrote:
> If the virtio_net device does not suppurt the ctrl queue feature,
> the vi->ctrl was not allocated, so there is no need to free it.

you don't need this check.

from kfree doc:

"If @objp is NULL, no operation is performed."

This is not a bug. I've set vi->ctrl to be NULL in case !vi->has_cvq.


>
> Here I adjust the initialization sequence and the check of vi->has_cvq
> to slove this problem.
>
> Fixes: 	122b84a1267a ("virtio-net: don't allocate control_buf if not supported")
> Signed-off-by: guodeqing <geffrey.guo@huawei.com>
> ---
>   drivers/net/virtio_net.c | 20 ++++++++++----------
>   1 file changed, 10 insertions(+), 10 deletions(-)
>
> diff --git a/drivers/net/virtio_net.c b/drivers/net/virtio_net.c
> index 9b6a4a875c55..894f894d3a29 100644
> --- a/drivers/net/virtio_net.c
> +++ b/drivers/net/virtio_net.c
> @@ -2691,7 +2691,8 @@ static void virtnet_free_queues(struct virtnet_info *vi)
>   
>   	kfree(vi->rq);
>   	kfree(vi->sq);
> -	kfree(vi->ctrl);
> +	if (vi->has_cvq)
> +		kfree(vi->ctrl);
>   }
>   
>   static void _free_receive_bufs(struct virtnet_info *vi)
> @@ -2870,13 +2871,6 @@ static int virtnet_alloc_queues(struct virtnet_info *vi)
>   {
>   	int i;
>   
> -	if (vi->has_cvq) {
> -		vi->ctrl = kzalloc(sizeof(*vi->ctrl), GFP_KERNEL);
> -		if (!vi->ctrl)
> -			goto err_ctrl;
> -	} else {
> -		vi->ctrl = NULL;
> -	}
>   	vi->sq = kcalloc(vi->max_queue_pairs, sizeof(*vi->sq), GFP_KERNEL);
>   	if (!vi->sq)
>   		goto err_sq;
> @@ -2884,6 +2878,12 @@ static int virtnet_alloc_queues(struct virtnet_info *vi)
>   	if (!vi->rq)
>   		goto err_rq;
>   
> +	if (vi->has_cvq) {
> +		vi->ctrl = kzalloc(sizeof(*vi->ctrl), GFP_KERNEL);
> +		if (!vi->ctrl)
> +			goto err_ctrl;
> +	}
> +
>   	INIT_DELAYED_WORK(&vi->refill, refill_work);
>   	for (i = 0; i < vi->max_queue_pairs; i++) {
>   		vi->rq[i].pages = NULL;
> @@ -2902,11 +2902,11 @@ static int virtnet_alloc_queues(struct virtnet_info *vi)
>   
>   	return 0;
>   
> +err_ctrl:
> +	kfree(vi->rq);
>   err_rq:
>   	kfree(vi->sq);
>   err_sq:
> -	kfree(vi->ctrl);
> -err_ctrl:
>   	return -ENOMEM;
>   }
>   

  reply	other threads:[~2021-05-23  7:24 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-05-22  8:02 [PATCH] virtio-net: fix the kzalloc/kfree mismatch problem guodeqing
2021-05-23  7:24 ` Max Gurtovoy [this message]
2021-05-24  1:48   ` Guodeqing (A)
2021-05-24  2:06     ` Xuan Zhuo
2021-05-24  2:37       ` Jason Wang
2021-05-24  2:37         ` Jason Wang
2021-05-24  8:34         ` Max Gurtovoy
2021-05-25  1:12           ` Guodeqing (A)
2021-05-25  1:11         ` Guodeqing (A)
2021-06-02  5:50         ` Leon Romanovsky
2021-06-02  5:50           ` Leon Romanovsky
2021-06-02  7:19           ` Jason Wang
2021-06-02  7:19             ` Jason Wang
2021-06-02 12:59             ` Leon Romanovsky
2021-06-02 12:59               ` Leon Romanovsky

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=6f7b729d-38df-9bf8-f023-bc1986da9a9e@nvidia.com \
    --to=mgurtovoy@nvidia.com \
    --cc=davem@davemloft.net \
    --cc=geffrey.guo@huawei.com \
    --cc=jasowang@redhat.com \
    --cc=kuba@kernel.org \
    --cc=mst@redhat.com \
    --cc=netdev@vger.kernel.org \
    --cc=virtualization@lists.linux-foundation.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.