From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <dev-bounces@dpdk.org>
Received: from mails.dpdk.org (mails.dpdk.org [217.70.189.124])
	by inbox.dpdk.org (Postfix) with ESMTP id DBCD3A00C2;
	Thu, 13 Oct 2022 10:07:37 +0200 (CEST)
Received: from [217.70.189.124] (localhost [127.0.0.1])
	by mails.dpdk.org (Postfix) with ESMTP id 8FB4B42D90;
	Thu, 13 Oct 2022 10:07:37 +0200 (CEST)
Received: from us-smtp-delivery-124.mimecast.com
 (us-smtp-delivery-124.mimecast.com [170.10.133.124])
 by mails.dpdk.org (Postfix) with ESMTP id CE98142C27
 for <dev@dpdk.org>; Thu, 13 Oct 2022 10:07:35 +0200 (CEST)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com;
 s=mimecast20190719; t=1665648455;
 h=from:from:reply-to:subject:subject:date:date:message-id:message-id:
 to:to:cc:cc:mime-version:mime-version:content-type:content-type:
 content-transfer-encoding:content-transfer-encoding:
 in-reply-to:in-reply-to:references:references;
 bh=oV+EV7OpZODHSBHE4JhEozHgSR4Qp7IC/xh9OTTNf94=;
 b=jBnfSms2UgM0Uru56XwcTz1AzY7I0vax/Yeg7f4fbmZzDevQXb8NPDb6PMsXHSoBrU6yfw
 M80wu/uIRxbrI2K4zX8dalKVo08D8nhYy31dOp9tAs6kX3oEd9kIf4mYqaD19133s/525h
 uLDp10S0hp8G6ej68sg9zQoUYDjvFLk=
Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com
 [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS
 (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id
 us-mta-42-Yp9h1CEoNwCnENzLByED4Q-1; Thu, 13 Oct 2022 04:07:28 -0400
X-MC-Unique: Yp9h1CEoNwCnENzLByED4Q-1
Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.rdu2.redhat.com
 [10.11.54.6])
 (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits))
 (No client certificate requested)
 by mimecast-mx02.redhat.com (Postfix) with ESMTPS id B86903801F55;
 Thu, 13 Oct 2022 08:07:27 +0000 (UTC)
Received: from [10.39.208.28] (unknown [10.39.208.28])
 by smtp.corp.redhat.com (Postfix) with ESMTPS id 008A42166BA9;
 Thu, 13 Oct 2022 08:07:25 +0000 (UTC)
Message-ID: <d4e192bb-eebf-dac4-f2b5-135c3c446978@redhat.com>
Date: Thu, 13 Oct 2022 10:07:24 +0200
MIME-Version: 1.0
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101
 Thunderbird/102.3.1
Subject: Re: [PATCH v4 2/2] examples/vhost: unconfigure DMA vchannel
To: xuan.ding@intel.com, chenbo.xia@intel.com
Cc: dev@dpdk.org, jiayu.hu@intel.com, xingguang.he@intel.com,
 yvonnex.yang@intel.com, cheng1.jiang@intel.com, yuanx.wang@intel.com,
 wenwux.ma@intel.com
References: <20220814140442.82525-1-xuan.ding@intel.com>
 <20221013064040.98489-1-xuan.ding@intel.com>
 <20221013064040.98489-3-xuan.ding@intel.com>
From: Maxime Coquelin <maxime.coquelin@redhat.com>
In-Reply-To: <20221013064040.98489-3-xuan.ding@intel.com>
X-Scanned-By: MIMEDefang 3.1 on 10.11.54.6
X-Mimecast-Spam-Score: 0
X-Mimecast-Originator: redhat.com
Content-Language: en-US
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
X-BeenThere: dev@dpdk.org
X-Mailman-Version: 2.1.29
Precedence: list
List-Id: DPDK patches and discussions <dev.dpdk.org>
List-Unsubscribe: <https://mails.dpdk.org/options/dev>,
 <mailto:dev-request@dpdk.org?subject=unsubscribe>
List-Archive: <http://mails.dpdk.org/archives/dev/>
List-Post: <mailto:dev@dpdk.org>
List-Help: <mailto:dev-request@dpdk.org?subject=help>
List-Subscribe: <https://mails.dpdk.org/listinfo/dev>,
 <mailto:dev-request@dpdk.org?subject=subscribe>
Errors-To: dev-bounces@dpdk.org



On 10/13/22 08:40, xuan.ding@intel.com wrote:
> From: Xuan Ding <xuan.ding@intel.com>
> 
> This patch applies rte_vhost_async_dma_unconfigure() to manually
> free DMA vchannels. Before unconfiguration, need make sure the
> specified DMA device is no longer used by any vhost ports.
> 
> Signed-off-by: Xuan Ding <xuan.ding@intel.com>
> ---
>   examples/vhost/main.c | 38 +++++++++++++++++++++++++-------------
>   1 file changed, 25 insertions(+), 13 deletions(-)
> 
> diff --git a/examples/vhost/main.c b/examples/vhost/main.c
> index ac78704d79..bfeb808dcc 100644
> --- a/examples/vhost/main.c
> +++ b/examples/vhost/main.c
> @@ -73,6 +73,7 @@ static int total_num_mbufs = NUM_MBUFS_DEFAULT;
>   
>   struct dma_for_vhost dma_bind[RTE_MAX_VHOST_DEVICE];
>   int16_t dmas_id[RTE_DMADEV_DEFAULT_MAX];
> +int16_t dma_ref_count[RTE_DMADEV_DEFAULT_MAX];
>   static int dma_count;
>   
>   /* mask of enabled ports */
> @@ -371,6 +372,7 @@ open_dma(const char *value)
>   done:
>   		(dma_info + socketid)->dmas[vring_id].dev_id = dev_id;
>   		(dma_info + socketid)->async_flag |= async_flag;
> +		dma_ref_count[dev_id]++;
>   		i++;
>   	}
>   out:
> @@ -1562,6 +1564,27 @@ vhost_clear_queue(struct vhost_dev *vdev, uint16_t queue_id)
>   	}
>   }
>   
> +static void
> +vhost_clear_async(struct vhost_dev *vdev, int vid, uint16_t queue_id)
> +{
> +	int16_t dma_id;
> +
> +	if (dma_bind[vid].dmas[queue_id].async_enabled) {

if (!dma_bind[vid].dmas[queue_id].async_enabled)
	return;

> +		vhost_clear_queue(vdev, queue_id);
> +		rte_vhost_async_channel_unregister(vid, queue_id);
> +		dma_bind[vid].dmas[queue_id].async_enabled = false;
> +
> +		dma_id = dma_bind[vid2socketid[vdev->vid]].dmas[queue_id].dev_id;
> +		dma_ref_count[dma_id]--;
> +
> +		if (dma_ref_count[dma_id] == 0) {

if (dma_ref_count[dma_id] > 0)
	return;

Doing this should improve readability.

> +			if (rte_vhost_async_dma_unconfigure(dma_id, 0) < 0)
> +				RTE_LOG(ERR, VHOST_CONFIG,
> +				       "Failed to unconfigure DMA %d in vhost.\n", dma_id);
> +		}
> +	}
> +}
> +
>   /*
>    * Remove a device from the specific data core linked list and from the
>    * main linked list. Synchronization  occurs through the use of the
> @@ -1618,17 +1641,8 @@ destroy_device(int vid)
>   		"(%d) device has been removed from data core\n",
>   		vdev->vid);
>   
> -	if (dma_bind[vid].dmas[VIRTIO_RXQ].async_enabled) {
> -		vhost_clear_queue(vdev, VIRTIO_RXQ);
> -		rte_vhost_async_channel_unregister(vid, VIRTIO_RXQ);
> -		dma_bind[vid].dmas[VIRTIO_RXQ].async_enabled = false;
> -	}
> -
> -	if (dma_bind[vid].dmas[VIRTIO_TXQ].async_enabled) {
> -		vhost_clear_queue(vdev, VIRTIO_TXQ);
> -		rte_vhost_async_channel_unregister(vid, VIRTIO_TXQ);
> -		dma_bind[vid].dmas[VIRTIO_TXQ].async_enabled = false;
> -	}
> +	vhost_clear_async(vdev, vid, VIRTIO_RXQ);
> +	vhost_clear_async(vdev, vid, VIRTIO_TXQ);
>   
>   	rte_free(vdev);
>   }
> @@ -1690,8 +1704,6 @@ vhost_async_channel_register(int vid)
>   	return rx_ret | tx_ret;
>   }
>   
> -
> -
>   /*
>    * A new device is added to a data core. First the device is added to the main linked list
>    * and then allocated to a specific data core.