From patchwork Fri Aug 13 14:02:39 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Gaoxiang Liu X-Patchwork-Id: 96907 X-Patchwork-Delegate: maxime.coquelin@redhat.com Return-Path: X-Original-To: patchwork@inbox.dpdk.org Delivered-To: patchwork@inbox.dpdk.org Received: from mails.dpdk.org (mails.dpdk.org [217.70.189.124]) by inbox.dpdk.org (Postfix) with ESMTP id 498E2A0C4D; Fri, 13 Aug 2021 16:03:04 +0200 (CEST) Received: from [217.70.189.124] (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id C2403410E8; Fri, 13 Aug 2021 16:03:03 +0200 (CEST) Received: from m12-16.163.com (m12-16.163.com [220.181.12.16]) by mails.dpdk.org (Postfix) with ESMTP id A866940DF4 for ; Fri, 13 Aug 2021 16:03:01 +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=p4NZA aotq8Za1ZpalIqXsywCVTDZz0NoNL9fDE6xrKg=; b=fEUb/WKxOpwt6dR37XxlB Qt5HC7LruIl6a/6ZOj0baHKXLZ8F1rfZWeOrru2sT1YIaLNbbzI0Feqz5GjC08f1 4cyJLrDYr6mMHejbkld4GfxOLLFe+oyEMiWZrFNNaB0tyT6m7LFOPMJ4js8coIiZ Jj68Pa004DW21AO2kem8oI= Received: from DESKTOP-ONA2IA7.localdomain (unknown [39.182.55.95]) by smtp12 (Coremail) with SMTP id EMCowABnr2OBexZhOZ1Z7Q--.34955S4; Fri, 13 Aug 2021 22:02:56 +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, 13 Aug 2021 22:02:39 +0800 Message-Id: <20210813140239.124-1-gaoxiangliu0@163.com> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20210807231252.660-1-gaoxiangliu0@163.com> References: <20210807231252.660-1-gaoxiangliu0@163.com> MIME-Version: 1.0 X-CM-TRANSID: EMCowABnr2OBexZhOZ1Z7Q--.34955S4 X-Coremail-Antispam: 1Uf129KBjvJXoW7WryxZFWfuw1UZryxXFy7ZFb_yoW8Zr15pF yagF9xJryxGrn5Z39xAFs7Xr1rCan5ua17G3srG3W5JFWDGwn0qa9F93WF9r18JF95ZFyU tF12gF4Y9FWUt3DanT9S1TB71UUUUUUqnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDUYxBIdaVFxhVjvjDU0xZFpf9x07j4KZJUUUUU= X-Originating-IP: [39.182.55.95] X-CM-SenderInfo: xjdr5xxdqjzxjxq6il2tof0z/xtbB3QntOmBHJ-4ckwAAsy Subject: [dpdk-dev] [PATCH] vhost: fix crash on port deletion 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". Because socket fd has not been deleted from poll waiting fds, "vhost-events" thread calls fdset_event_dispatch, then calls vhost_user_read_cb(), and accesses invalid memory of "conn". 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 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") v2: fix coding style issues v3: add detailed log Signed-off-by: Gaoxiang Liu --- 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);