From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) by dpdk.org (Postfix) with ESMTP id 2FACF7D4A for ; Tue, 11 Dec 2018 19:11:07 +0100 (CET) Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.phx2.redhat.com [10.5.11.22]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 897D458E3F; Tue, 11 Dec 2018 18:11:06 +0000 (UTC) Received: from [10.36.112.28] (ovpn-112-28.ams2.redhat.com [10.36.112.28]) by smtp.corp.redhat.com (Postfix) with ESMTPS id EDB511057069; Tue, 11 Dec 2018 18:11:04 +0000 (UTC) To: Matthias Gatto , dev@dpdk.org Cc: tiwei.bie@intel.com, zhihong.wang@intel.com References: <1544112007-23177-1-git-send-email-matthias.gatto@outscale.com> From: Maxime Coquelin Message-ID: Date: Tue, 11 Dec 2018 19:11:02 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.2.1 MIME-Version: 1.0 In-Reply-To: <1544112007-23177-1-git-send-email-matthias.gatto@outscale.com> Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 7bit X-Scanned-By: MIMEDefang 2.84 on 10.5.11.22 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.39]); Tue, 11 Dec 2018 18:11:06 +0000 (UTC) Subject: Re: [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: Tue, 11 Dec 2018 18:11:07 -0000 Hi Matthias, On 12/6/18 5:00 PM, Matthias Gatto wrote: > 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); Any reason we cannot use the existing fd_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, >