From mboxrd@z Thu Jan 1 00:00:00 1970 From: Maxime Coquelin Subject: Re: [PATCH] vhost: fix race condition in fdset_add Date: Fri, 14 Dec 2018 10:51:45 +0100 Message-ID: <1f9db33f-b281-b794-bc00-ad83490c2fbd@redhat.com> References: <1544112007-23177-1-git-send-email-matthias.gatto@outscale.com> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit Cc: dev@dpdk.org, tiwei.bie@intel.com, zhihong.wang@intel.com To: Matthias Gatto Return-path: Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) by dpdk.org (Postfix) with ESMTP id 1A5D31B925 for ; Fri, 14 Dec 2018 10:51:50 +0100 (CET) In-Reply-To: Content-Language: en-US List-Id: DPDK patches and discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dev-bounces@dpdk.org Sender: "dev" On 12/14/18 10:32 AM, Matthias Gatto wrote: > On Tue, Dec 11, 2018 at 7:11 PM Maxime Coquelin > wrote: >> >> 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? > > yes, using the existing fd_mutex would block fdset_add during the polling in > fdset_event_dispatch. > > here fd_pooling_mutex block only fdset_shrink_nolock inside > fdset_add which happen only in very rare occasions. Thanks for the clarification: Reviewed-by: Maxime Coquelin Maxime