From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-pf0-f194.google.com (mail-pf0-f194.google.com [209.85.192.194]) by dpdk.org (Postfix) with ESMTP id AD0C17CEB for ; Fri, 27 Apr 2018 16:49:03 +0200 (CEST) Received: by mail-pf0-f194.google.com with SMTP id a14so1670517pfi.1 for ; Fri, 27 Apr 2018 07:49:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id; bh=/JYDdHKxQa8cHH+231Qy+HflZDOHXnxAcq4pMkqQ/Us=; b=OHB9R9ZeqbmAQMbhJvkMUW5gpGFwp3iRijZOH/IjU8Y9W38QjG1AqRbfUyWBCV9ozd bzaLbli1Kbnn1h5lCaQyi0Y+3e9s8W9o/WNdfCoUVnACuI6lFX0beU7zWu65HS9JZAHV zW/Iuf/L8ApnSQDoC5dPI6o+t3ueCm9rI7BSBp4aaox3geYX7NN3kAUJAdWUT0u1wx3/ QI6tMki/cMrki54GEn1e0QfJx+N7FUkuuog50SCop8foam8geMY0Cv7L9KLnx0kxvkca lQ9gi/bvXA8MLZhfAzgobCQzry+i5clvmsZ6BfDJXh65YNlyWjSBveJGfYwh9u5ivfNz z3Pw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id; bh=/JYDdHKxQa8cHH+231Qy+HflZDOHXnxAcq4pMkqQ/Us=; b=HcTW1wnEFFFLtk1bj8cdF1SBCc9YdLTN/qKcVaWIF+qSERYHEFFsSqK9lDo5CmHnVH ZBNPhqyWBY/iPrIrVYSIMf6VR9NnQuOw7dog884g0Z3c/x+RenBggJKX6kX/swkTwQhV bJN5j/Oob4GcWfk9P66xyC40ekiJluPFKxTJ+EW7BrBnu/y90YCU2x9ehjv2xk7QeXbF l0yCHa2yrs3I5SGHE5M23KEgBK03iQ3oWQVGLcw3SNAnJtjfPvp9kPmGTD6i34DH+UtU bt6aMvv68Ym/l2BZs3fxPM/VaTDy0+BvsaNHPcPbmplI8+WJk9UYdDAdPk9S6xX959j8 Fp9w== X-Gm-Message-State: ALQs6tD6etYi7zbqA8RlgUSZjOE9MBvrelVVCxRwx8v6KgbAZP8x9kSO CFf9huq3ZDpQXgwoTtF2MM8= X-Google-Smtp-Source: AB8JxZpSYWTyRxptY+ltWnu+lvBg1Mmx08TiR4Ca+CTc5eJRj+Kn4OyGLv43Cnkcxi0zMZSYh/f3Wg== X-Received: by 10.98.86.143 with SMTP id h15mr2163403pfj.131.1524840542962; Fri, 27 Apr 2018 07:49:02 -0700 (PDT) Received: from local.opencloud.tech.localdomain ([36.102.208.99]) by smtp.gmail.com with ESMTPSA id v16sm5662845pfj.123.2018.04.27.07.49.00 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 27 Apr 2018 07:49:02 -0700 (PDT) From: xiangxia.m.yue@gmail.com To: maxime.coquelin@redhat.com, jianfeng.tan@intel.com, yliu@fridaylinux.org Cc: Tonghao Zhang , stable@dpdk.org Date: Fri, 27 Apr 2018 07:48:56 -0700 Message-Id: <1524840538-3941-1-git-send-email-xiangxia.m.yue@gmail.com> X-Mailer: git-send-email 1.8.3.1 Subject: [dpdk-stable] [PATCH 1/3] vhost: fix deadlock due to vhostuser socket and fdset X-BeenThere: stable@dpdk.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: patches for DPDK stable branches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 27 Apr 2018 14:49:04 -0000 From: Tonghao Zhang When qemu close the unix socket fd of the vhostuser as a server, and then immediately delete the vhostuser port on openvswitch. There will be a deadlock. A thread (fdset event thread): B thread: 1. fdset_event_dispatch rte_vhost_driver_unregister 2. set the fd busy to 1. lock vsocket->conn_mutex 3. vhost_user_read_cb fdset_del waits busy changed to 0. 4. vhost peer closed, remove the conn from vsocket->conn_list: lock vsocket->conn_mutex 5. set the fd busy to 0 Fixes: 65388b43 ("vhost: fix fd leaks for vhost-user server mode") Cc: stable@dpdk.org Cc: Yuanhan Liu Signed-off-by: Tonghao Zhang --- lib/librte_vhost/fd_man.c | 32 ++++++++++++++++++++++++++++++++ lib/librte_vhost/fd_man.h | 1 + lib/librte_vhost/socket.c | 13 ++++++++++++- 3 files changed, 45 insertions(+), 1 deletion(-) diff --git a/lib/librte_vhost/fd_man.c b/lib/librte_vhost/fd_man.c index 8590ee5..b24c27d 100644 --- a/lib/librte_vhost/fd_man.c +++ b/lib/librte_vhost/fd_man.c @@ -174,6 +174,38 @@ return dat; } +/** + * Unregister the fd from the fdset. + * + * If parameters are invalid, return directly -2. + * And check whether fd is busy, if yes, return -1. + * Otherwise, try to delete the fd from fdset and + * return true. + */ +int +fdset_try_del(struct fdset *pfdset, int fd) +{ + int i; + + if (pfdset == NULL || fd == -1) + return -2; + + pthread_mutex_lock(&pfdset->fd_mutex); + i = fdset_find_fd(pfdset, fd); + if (i != -1 && pfdset->fd[i].busy) { + pthread_mutex_unlock(&pfdset->fd_mutex); + return -1; + } + + if (i != -1) { + pfdset->fd[i].fd = -1; + pfdset->fd[i].rcb = pfdset->fd[i].wcb = NULL; + pfdset->fd[i].dat = NULL; + } + + pthread_mutex_unlock(&pfdset->fd_mutex); + return 0; +} /** * This functions runs in infinite blocking loop until there is no fd in diff --git a/lib/librte_vhost/fd_man.h b/lib/librte_vhost/fd_man.h index 76a42fb..3331bcd 100644 --- a/lib/librte_vhost/fd_man.h +++ b/lib/librte_vhost/fd_man.h @@ -44,6 +44,7 @@ int fdset_add(struct fdset *pfdset, int fd, fd_cb rcb, fd_cb wcb, void *dat); void *fdset_del(struct fdset *pfdset, int fd); +int fdset_try_del(struct fdset *pfdset, int fd); void *fdset_event_dispatch(void *arg); diff --git a/lib/librte_vhost/socket.c b/lib/librte_vhost/socket.c index 4a561ad..822db41 100644 --- a/lib/librte_vhost/socket.c +++ b/lib/librte_vhost/socket.c @@ -922,13 +922,24 @@ struct vhost_user_reconnect_list { vhost_user_remove_reconnect(vsocket); } +again: pthread_mutex_lock(&vsocket->conn_mutex); for (conn = TAILQ_FIRST(&vsocket->conn_list); conn != NULL; conn = next) { next = TAILQ_NEXT(conn, next); - fdset_del(&vhost_user.fdset, conn->connfd); + /* + * If r/wcb is executing, release the + * conn_mutex lock, and try again since + * the r/wcb may use the conn_mutex lock. + */ + if (fdset_try_del(&vhost_user.fdset, + conn->connfd) == -1) { + pthread_mutex_unlock(&vsocket->conn_mutex); + goto again; + } + RTE_LOG(INFO, VHOST_CONFIG, "free connfd = %d for device '%s'\n", conn->connfd, path); -- 1.8.3.1