From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-wr1-f97.google.com (mail-wr1-f97.google.com [209.85.221.97]) by dpdk.org (Postfix) with ESMTP id 6551E1F28 for ; Thu, 6 Dec 2018 17:00:17 +0100 (CET) Received: by mail-wr1-f97.google.com with SMTP id p4so993909wrt.7 for ; Thu, 06 Dec 2018 08:00:17 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=outscale-com.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id; bh=g7uKecm2QmAqcM9DS4lfDCPV9OKqo88R+/Fc+tadGa8=; b=MHgfY7eZ9B5ZveNn5j7wSXuMvDFlYS0sfoLBRtR48sMgoQ64Hmnhrfe04enBoUbiGB RCPo0CDDtx7PEKHwdGLwc+q4LTQp27gZDHB4EHxs+rz4gUKtNz546+Kkhd8U9/0X1Aus pTBswHZH8rEZ+bG7RLRSzWYZHV5tgb3eyZi1YwFlxuwDd8y8LxOdFqkpm7WpRdc57mgd U3RfZ9sW3/kHAy5qU+umg07nRnC309e3uXnijD2Uoz122ne+LAn75naGBOoJ7PTbj8zF xvtY7yqx8txnFc9o9QfpBQR0vNg6OF/un2m1+VKlPZkw/eFSMgOFL/wxzF7DgYXeE13E jHTw== 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=g7uKecm2QmAqcM9DS4lfDCPV9OKqo88R+/Fc+tadGa8=; b=QLzQCVg8DrWSwD5bMjvThH1Pn5PsQFZCRSz6wqQgsVWGeZz1KuHHyIW2lp3IbpX2dA BGVY/DsgfAJhyyaNfsFQ5T49XSK0paXMy2phoxxgmMaYsylUsE0K2dlqM1GntU0TBGO+ 8Z8qzVIDL0lc+lsyU/wy9ZAG2RmO5fZpwG71Kb3DvqvxThMz5oIsvoeur8kaF/sGj30O C4K44iCWqaFLZcwYlJ661rRi2thQs1I/LGXQQIul7PWMRneSYcfizH/+ZYPaxP/DPCi2 fO7lTnAAxtzHWAOdKte/zH96zRVqdKP2d7+81iIEoDChvEkMg/JyL1D4v18OnPFKwyRy qfIA== X-Gm-Message-State: AA+aEWZy6UnmgQMeuiOubYv3iFzXCO+0jc+qZ23EMkRqf24S1eOXOyBf 2rK3H8oMJJdkgwuXtttyxlJ9S3u/KvcmwHKLI8stxBxfT1pKtA== X-Google-Smtp-Source: AFSGD/VdK/EZscMuRaJjt74OqMvjLZZSBIxFM1KzRKhu7K/KPJ4fDePGyXF77BGHPUxzIfN5Y9lPC76vJC6t X-Received: by 2002:adf:ca13:: with SMTP id o19mr25496509wrh.148.1544112017045; Thu, 06 Dec 2018 08:00:17 -0800 (PST) Received: from ip-10-9-13-52.eu-west-2.compute.eu-west-2.compute.internal (ows-109-232-232-115.eu-west-2.compute.outscale.com. [109.232.232.115]) by smtp-relay.gmail.com with ESMTPS id v72sm19103wmd.28.2018.12.06.08.00.16 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 06 Dec 2018 08:00:17 -0800 (PST) X-Relaying-Domain: outscale.com From: Matthias Gatto To: dev@dpdk.org Cc: Matthias Gatto , maxime.coquelin@redhat.com, tiwei.bie@intel.com, zhihong.wang@intel.com Date: Thu, 6 Dec 2018 16:00:07 +0000 Message-Id: <1544112007-23177-1-git-send-email-matthias.gatto@outscale.com> X-Mailer: git-send-email 1.8.3.1 Subject: [dpdk-dev] [PATCH] vhost: fix race condition in fdset_add X-BeenThere: dev@dpdk.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: DPDK patches and discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 06 Dec 2018 16:00:17 -0000 fdset_add can call fdset_shrink_nolock which call fdset_move concurrently to poll that is call in fdset_event_dispatch. This patch add a mutex to protect poll from been call at the same time fdset_add call fdset_shrink_nolock. Signed-off-by: Matthias Gatto --- lib/librte_vhost/fd_man.c | 4 ++++ lib/librte_vhost/fd_man.h | 1 + lib/librte_vhost/socket.c | 1 + 3 files changed, 6 insertions(+) diff --git a/lib/librte_vhost/fd_man.c b/lib/librte_vhost/fd_man.c index 38347ab..55d4856 100644 --- a/lib/librte_vhost/fd_man.c +++ b/lib/librte_vhost/fd_man.c @@ -129,7 +129,9 @@ pthread_mutex_lock(&pfdset->fd_mutex); i = pfdset->num < MAX_FDS ? pfdset->num++ : -1; if (i == -1) { + pthread_mutex_lock(&pfdset->fd_pooling_mutex); fdset_shrink_nolock(pfdset); + pthread_mutex_unlock(&pfdset->fd_pooling_mutex); i = pfdset->num < MAX_FDS ? pfdset->num++ : -1; if (i == -1) { pthread_mutex_unlock(&pfdset->fd_mutex); @@ -246,7 +248,9 @@ numfds = pfdset->num; pthread_mutex_unlock(&pfdset->fd_mutex); + pthread_mutex_lock(&pfdset->fd_pooling_mutex); val = poll(pfdset->rwfds, numfds, 1000 /* millisecs */); + pthread_mutex_unlock(&pfdset->fd_pooling_mutex); if (val < 0) continue; diff --git a/lib/librte_vhost/fd_man.h b/lib/librte_vhost/fd_man.h index 3331bcd..3ab5cfd 100644 --- a/lib/librte_vhost/fd_man.h +++ b/lib/librte_vhost/fd_man.h @@ -24,6 +24,7 @@ struct fdset { struct pollfd rwfds[MAX_FDS]; struct fdentry fd[MAX_FDS]; pthread_mutex_t fd_mutex; + pthread_mutex_t fd_pooling_mutex; int num; /* current fd number of this fdset */ union pipefds { diff --git a/lib/librte_vhost/socket.c b/lib/librte_vhost/socket.c index d630317..cc4e748 100644 --- a/lib/librte_vhost/socket.c +++ b/lib/librte_vhost/socket.c @@ -88,6 +88,7 @@ struct vhost_user { .fdset = { .fd = { [0 ... MAX_FDS - 1] = {-1, NULL, NULL, NULL, 0} }, .fd_mutex = PTHREAD_MUTEX_INITIALIZER, + .fd_pooling_mutex = PTHREAD_MUTEX_INITIALIZER, .num = 0 }, .vsocket_cnt = 0, -- 1.8.3.1