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 F3109A0C41;
	Sat,  7 Aug 2021 10:25:24 +0200 (CEST)
Received: from [217.70.189.124] (localhost [127.0.0.1])
	by mails.dpdk.org (Postfix) with ESMTP id AA43740691;
	Sat,  7 Aug 2021 10:25:24 +0200 (CEST)
Received: from m12-13.163.com (m12-13.163.com [220.181.12.13])
 by mails.dpdk.org (Postfix) with ESMTP id 19AB34067E
 for <dev@dpdk.org>; Sat,  7 Aug 2021 10:25:22 +0200 (CEST)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=163.com;
 s=s110527; h=From:Subject:Date:Message-Id:MIME-Version; bh=d9zEp
 wkGFuNq2pVN+RZQYmkDqRyj+yxqd3l0WbSpd4c=; b=p2qnd8J7p3ps3sNbJM+9g
 10SHrcB8AQvoUFswh0QOSO0hKhwxbYzWyHOwnTYwwmlVK0bCJ/xQUegRiuJgmhet
 D9IVfnqR/udwD1HVhCgK7zkUi6tHzykQzUSlrAYSwH6j0AdpgacK893o/7WaEN/d
 NUpj25zMJg0dvx8UbyOkw8=
Received: from DESKTOP-ONA2IA7.localdomain (unknown [39.182.55.95])
 by smtp9 (Coremail) with SMTP id DcCowADH1GliQw5hRBInOg--.27868S4;
 Sat, 07 Aug 2021 16:25:20 +0800 (CST)
From: Gaoxiang Liu <gaoxiangliu0@163.com>
To: Maxime Coquelin <maxime.coquelin@redhat.com>,
 Chenbo Xia <chenbo.xia@intel.com>
Cc: dev@dpdk.org, liugaoxiang@huawei.com, Gaoxiang Liu <gaoxiangliu0@163.com>
Date: Sat,  7 Aug 2021 16:25:03 +0800
Message-Id: <20210807082504.1432-1-gaoxiangliu0@163.com>
X-Mailer: git-send-email 2.32.0
MIME-Version: 1.0
Content-Transfer-Encoding: 8bit
X-CM-TRANSID: DcCowADH1GliQw5hRBInOg--.27868S4
X-Coremail-Antispam: 1Uf129KBjvJXoW7CF43JF4UtrW5trWrJF1kZrb_yoW8ur47pF
 y3KFy3Jr97Jrn5Z39xAF4DXFyrCan5ua17G3srG3W5AF4DGws0qayq93WF9F18JFy8XFyU
 tF1jgF4S9FWUJw7anT9S1TB71UUUUU7qnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2
 9KBjDUYxBIdaVFxhVjvjDU0xZFpf9x07jSjgxUUUUU=
X-Originating-IP: [39.182.55.95]
X-CM-SenderInfo: xjdr5xxdqjzxjxq6il2tof0z/1tbiMhDnOlWBu7FVGAAAss
Subject: [dpdk-dev] [PATCH] vhost: fix coredump on port deletion
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
Sender: "dev" <dev-bounces@dpdk.org>

The rte_vhost_driver_unregister() and vhost_user_read_cb()
can be called at the same time by 2 threads.
Eg thread1 calls rte_vhost_driver_unregister() and frees memory of conn,
then thread2 calls vhost_user_read_cb() and access invalid memory of
conn.

The fix is to move the "fdset_try_del" in front of free memory of conn ,
then avoid the race condition.

Fixes: 52d874dc6705 ("vhost: fix crash on closing in client mode")

Signed-off-by: Gaoxiang Liu <liugaoxiang@huawei.com>
---
 lib/vhost/socket.c | 26 +++++++++++++-------------
 1 file changed, 13 insertions(+), 13 deletions(-)

diff --git a/lib/vhost/socket.c b/lib/vhost/socket.c
index 5d0d728d5..bc7278e8a 100644
--- a/lib/vhost/socket.c
+++ b/lib/vhost/socket.c
@@ -1024,6 +1024,19 @@ rte_vhost_driver_unregister(const char *path)
 	for (i = 0; i < vhost_user.vsocket_cnt; i++) {
 		struct vhost_user_socket *vsocket = vhost_user.vsockets[i];
 
+		if (vsocket->is_server) {
+			/*
+			 * If r/wcb is executing, release vhost_user's
+			 * mutex lock, and try again since the r/wcb
+			 * may use the mutex lock.
+			 */
+			if (fdset_try_del(&vhost_user.fdset, vsocket->socket_fd) == -1) {
+				pthread_mutex_unlock(&vhost_user.mutex);
+				goto again;
+			}
+		 } else if (vsocket->reconnect)
+			vhost_user_remove_reconnect(vsocket);
+
 		if (!strcmp(vsocket->path, path)) {
 			pthread_mutex_lock(&vsocket->conn_mutex);
 			for (conn = TAILQ_FIRST(&vsocket->conn_list);
@@ -1056,21 +1069,8 @@ rte_vhost_driver_unregister(const char *path)
 			pthread_mutex_unlock(&vsocket->conn_mutex);
 
 			if (vsocket->is_server) {
-				/*
-				 * If r/wcb is executing, release vhost_user's
-				 * mutex lock, and try again since the r/wcb
-				 * may use the mutex lock.
-				 */
-				if (fdset_try_del(&vhost_user.fdset,
-						vsocket->socket_fd) == -1) {
-					pthread_mutex_unlock(&vhost_user.mutex);
-					goto again;
-				}
-
 				close(vsocket->socket_fd);
 				unlink(path);
-			} else if (vsocket->reconnect) {
-				vhost_user_remove_reconnect(vsocket);
 			}
 
 			pthread_mutex_destroy(&vsocket->conn_mutex);
-- 
2.32.0