From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mails.dpdk.org (mails.dpdk.org [217.70.189.124]) by inbox.dpdk.org (Postfix) with ESMTP id 30C41A0C4D; Fri, 20 Aug 2021 17:46:33 +0200 (CEST) Received: from [217.70.189.124] (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id B2E7840141; Fri, 20 Aug 2021 17:46:32 +0200 (CEST) Received: from m12-16.163.com (m12-16.163.com [220.181.12.16]) by mails.dpdk.org (Postfix) with ESMTP id 4AE084013F for ; Fri, 20 Aug 2021 17:46:30 +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=fNHt3 LHm2b/l0fR3YPmK56fd+qwE0r4mCusbZ38kI10=; b=hUe8C8OGAlFeC2KYqWuRe eVfTmmPwVvVc+DKmrLyrPtZMmaFBD/QJA3CO6P8j96mcftj7jg/r779vlMt7LzEm nSKDcSm4x3n5Qbr/P/VeS4tK4/4QWQ+4iHZdTmB0V5eVsDLvd4HwsOc+07b5Y9Io wWJe7CHBb0M+6/Mk9qtrzw= Received: from DESKTOP-ONA2IA7.localdomain (unknown [39.182.52.167]) by smtp12 (Coremail) with SMTP id EMCowAAXJFBLzh9hRPuz8g--.23275S4; Fri, 20 Aug 2021 23:46:27 +0800 (CST) From: Gaoxiang Liu To: maxime.coquelin@redhat.com, chenbo.xia@intel.com Cc: dev@dpdk.org, liugaoxiang@huawei.com, Gaoxiang Liu Date: Fri, 20 Aug 2021 23:46:15 +0800 Message-Id: <20210820154615.551-1-gaoxiangliu0@163.com> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20210818160857.1729-1-gaoxiangliu0@163.com> References: <20210818160857.1729-1-gaoxiangliu0@163.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-CM-TRANSID: EMCowAAXJFBLzh9hRPuz8g--.23275S4 X-Coremail-Antispam: 1Uf129KBjvJXoWxArWUCF1rArW7Wr13ur4rZrb_yoW5ZF1DpF y7ta43Jryktr1fZrZxAa1kXFy8Ca1kCa17G3srGF15Ja1DGw4Yvayqk3W09F1UJFW8XFyU tF1jgr4S9FWUt3DanT9S1TB71UUUUUUqnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDUYxBIdaVFxhVjvjDU0xZFpf9x07j4KZJUUUUU= X-Originating-IP: [39.182.52.167] X-CM-SenderInfo: xjdr5xxdqjzxjxq6il2tof0z/xtbBORP0Ol-PJDObIgAAsa Subject: [dpdk-dev] [PATCH v5] vhost: fix crash on port deletion X-BeenThere: dev@dpdk.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: DPDK patches and discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dev-bounces@dpdk.org Sender: "dev" The rte_vhost_driver_unregister() and vhost_user_read_cb() can be called at the same time by 2 threads. when memory of vsocket is freed in rte_vhost_driver_unregister(), the invalid memory of vsocket is accessd in vhost_user_read_cb(). It's a bug of both mode for vhost as server or client. Eg vhostuser port is created as server. Thread1 calls rte_vhost_driver_unregister(). Before the listen fd is deleted from poll waiting fds, "vhost-events" thread then calls vhost_user_server_new_connection(), then a new conn fd is added in fdset when trying to reconnect. "vhost-events" thread then calls vhost_user_read_cb() and access invalid memory of socket while thread1 frees the memory of vsocket. Eg vhostuser port is created as client. Thread1 calls rte_vhost_driver_unregister(). Before vsocket of reconn is deleted from reconn list, "vhost_reconn" thread then calls vhost_user_add_connection() then a new conn fd is added in fdset when trying to reconnect. "vhost-events" thread then calls vhost_user_read_cb() and access invalid memory of socket while thread1 frees the memory of vsocket. The fix is to move the "fdset_try_del" in front of free memory of conn, then avoid the race condition. The core trace is: Program terminated with signal 11, Segmentation fault. Fixes: 52d874dc6705 ("vhost: fix crash on closing in client mode") Signed-off-by: Gaoxiang Liu v2: * Fix coding style issues. v3: * Add detailed log. v4: * Add the reason when vhostuser port is created as server. v5: * Add detailed log when vhostuser port is created as client. --- lib/vhost/socket.c | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/lib/vhost/socket.c b/lib/vhost/socket.c index 5d0d728d5..2eb8fcadd 100644 --- a/lib/vhost/socket.c +++ b/lib/vhost/socket.c @@ -1024,6 +1024,20 @@ 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 +1070,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