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 952E942AD6;
	Thu, 11 May 2023 17:00:10 +0200 (CEST)
Received: from mails.dpdk.org (localhost [127.0.0.1])
	by mails.dpdk.org (Postfix) with ESMTP id 6F3E842D9C;
	Thu, 11 May 2023 17:00:10 +0200 (CEST)
Received: from baidu.com (mx20.baidu.com [111.202.115.85])
 by mails.dpdk.org (Postfix) with ESMTP id 6C504406B6
 for <dev@dpdk.org>; Thu, 11 May 2023 17:00:08 +0200 (CEST)
From: suntianyuan <suntianyuan@baidu.com>
To: <chenbo.xia@intel.com>
CC: <dev@dpdk.org>, <maxime.coquelin@redhat.com>, <suntianyuan@baidu.com>
Subject: [PATCH v2] vhost: fix crash on vhost-user client port deletion
Date: Thu, 11 May 2023 22:59:57 +0800
Message-ID: <20230511145957.33904-1-suntianyuan@baidu.com>
X-Mailer: git-send-email 2.32.0 (Apple Git-132)
In-Reply-To: <SN6PR11MB35042FDAA797B9969AE05AF99C749@SN6PR11MB3504.namprd11.prod.outlook.com>
References: <SN6PR11MB35042FDAA797B9969AE05AF99C749@SN6PR11MB3504.namprd11.prod.outlook.com>
MIME-Version: 1.0
Content-Transfer-Encoding: 8bit
Content-Type: text/plain
X-Originating-IP: [172.31.62.13]
X-ClientProxiedBy: BC-Mail-EX04.internal.baidu.com (172.31.51.44) To
 BJHW-Mail-Ex07.internal.baidu.com (10.127.64.17)
X-FEAS-Client-IP: 10.127.64.17
X-FE-Policy-ID: 15:10:21:SYSTEM
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

The rte_vhost_driver_unregister() and vhost_user_read_cb() can be
called at the same time by 2 threads. reconn may be added back to
reconn_list by vhost_user_read_cb() after rte_vhost_driver_unregister()
removed from reconn_list. Then rte_vhost_driver_unregister free vsocket,
cause vhost_user_client_reconnect access invalid vsocket memory.

Timeline is as below:
rte_vhost_driver_unregister thread execute vhost_user_remove_reconnect
vhost_user_read_cb thread execute vhost_user_start_client add reconn to
reconn_list
vhost_user_read_cb thread free conn
rte_vhost_driver_unregister thread cannot find conn, then execute
vhost_user_socket_mem_free
vhost_user_client_reconnect access invalid mem, crash

Signed-off-by: suntianyuan <suntianyuan@baidu.com>
---
v2:
* Fixed coding style issues.

 lib/vhost/socket.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/lib/vhost/socket.c b/lib/vhost/socket.c
index 669c322e12..72c776d15c 100644
--- a/lib/vhost/socket.c
+++ b/lib/vhost/socket.c
@@ -1046,8 +1046,6 @@ rte_vhost_driver_unregister(const char *path)
 				pthread_mutex_unlock(&vhost_user.mutex);
 				goto again;
 			}
-		} else if (vsocket->reconnect) {
-			vhost_user_remove_reconnect(vsocket);
 		}
 
 		pthread_mutex_lock(&vsocket->conn_mutex);
@@ -1080,6 +1078,8 @@ rte_vhost_driver_unregister(const char *path)
 		if (vsocket->is_server) {
 			close(vsocket->socket_fd);
 			unlink(path);
+		} else if (vsocket->reconnect) {
+			vhost_user_remove_reconnect(vsocket);
 		}
 
 		pthread_mutex_destroy(&vsocket->conn_mutex);
-- 
2.32.0 (Apple Git-132)