From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mails.dpdk.org (mails.dpdk.org [217.70.189.124]) by inbox.dpdk.org (Postfix) with ESMTP id 2FF0343C36 for ; Thu, 29 Feb 2024 13:25:13 +0100 (CET) Received: from mails.dpdk.org (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id 2A453427D7; Thu, 29 Feb 2024 13:25:13 +0100 (CET) Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by mails.dpdk.org (Postfix) with ESMTP id 52FDC41148 for ; Thu, 29 Feb 2024 13:25:12 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1709209511; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=Xypbq0nHIUw3GjM/Pybi6OIeg99F/kSbPa6LqQhXBzs=; b=NxFx/MlTwgN2F/GgeBMVsQf86uL6L07JVxIl4wlnMgPeQ4JopXZ5PQbmfJcTO3jYs9MPsy K2rDan92kbTEu7RO9LL0wtBOvsPQFdnQysTdl0edsVySr9M7VLDop15DRaVRC3oELsI5BZ yzHwhOruTQ1PIeCnGDYirFP2iCrRDpI= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-91-Mw5sRnjxPGmyuRwApqYK6w-1; Thu, 29 Feb 2024 07:25:08 -0500 X-MC-Unique: Mw5sRnjxPGmyuRwApqYK6w-1 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.rdu2.redhat.com [10.11.54.3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 391F3185A787; Thu, 29 Feb 2024 12:25:07 +0000 (UTC) Received: from max-p1.redhat.com (unknown [10.39.208.20]) by smtp.corp.redhat.com (Postfix) with ESMTP id B77FC112132A; Thu, 29 Feb 2024 12:25:05 +0000 (UTC) From: Maxime Coquelin To: dev@dpdk.org, david.marchand@redhat.com, chenbox@nvidia.com Cc: Maxime Coquelin , stable@dpdk.org Subject: [PATCH 1/7] vhost: fix VDUSE device destruction failure Date: Thu, 29 Feb 2024 13:24:56 +0100 Message-ID: <20240229122502.2572343-2-maxime.coquelin@redhat.com> In-Reply-To: <20240229122502.2572343-1-maxime.coquelin@redhat.com> References: <20240229122502.2572343-1-maxime.coquelin@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.3 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: 8bit Content-Type: text/plain; charset="US-ASCII"; x-default=true X-BeenThere: stable@dpdk.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: patches for DPDK stable branches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: stable-bounces@dpdk.org VDUSE_DESTROY_DEVICE ioctl can fail because the device's chardev is not released despite close syscall having been called. It happens because the events handler thread is still polling the file descriptor. fdset_pipe_notify() is not enough because it does not ensure the notification has been handled by the event thread, it just returns once the notification is sent. To fix this, this patch introduces a synchronization mechanism based on pthread's condition, so that fdset_pipe_notify() only returns once the pipe's read callback has been executed. Fixes: 51d018fdac4e ("vhost: add VDUSE events handler") Cc: stable@dpdk.org Signed-off-by: Maxime Coquelin --- lib/vhost/fd_man.c | 21 ++++++++++++++++++--- lib/vhost/fd_man.h | 5 +++++ 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/lib/vhost/fd_man.c b/lib/vhost/fd_man.c index 79a8d2c006..42ce059039 100644 --- a/lib/vhost/fd_man.c +++ b/lib/vhost/fd_man.c @@ -309,10 +309,11 @@ fdset_event_dispatch(void *arg) } static void -fdset_pipe_read_cb(int readfd, void *dat __rte_unused, +fdset_pipe_read_cb(int readfd, void *dat, int *remove __rte_unused) { char charbuf[16]; + struct fdset *fdset = dat; int r = read(readfd, charbuf, sizeof(charbuf)); /* * Just an optimization, we don't care if read() failed @@ -320,6 +321,11 @@ fdset_pipe_read_cb(int readfd, void *dat __rte_unused, * compiler happy */ RTE_SET_USED(r); + + pthread_mutex_lock(&fdset->sync_mutex); + fdset->sync = true; + pthread_cond_broadcast(&fdset->sync_cond); + pthread_mutex_unlock(&fdset->sync_mutex); } void @@ -342,7 +348,7 @@ fdset_pipe_init(struct fdset *fdset) } ret = fdset_add(fdset, fdset->u.readfd, - fdset_pipe_read_cb, NULL, NULL); + fdset_pipe_read_cb, NULL, fdset); if (ret < 0) { VHOST_FDMAN_LOG(ERR, @@ -359,7 +365,12 @@ fdset_pipe_init(struct fdset *fdset) void fdset_pipe_notify(struct fdset *fdset) { - int r = write(fdset->u.writefd, "1", 1); + int r; + + pthread_mutex_lock(&fdset->sync_mutex); + + fdset->sync = false; + r = write(fdset->u.writefd, "1", 1); /* * Just an optimization, we don't care if write() failed * so ignore explicitly its return value to make the @@ -367,4 +378,8 @@ fdset_pipe_notify(struct fdset *fdset) */ RTE_SET_USED(r); + while (!fdset->sync) + pthread_cond_wait(&fdset->sync_cond, &fdset->sync_mutex); + + pthread_mutex_unlock(&fdset->sync_mutex); } diff --git a/lib/vhost/fd_man.h b/lib/vhost/fd_man.h index 6315904c8e..cc19937612 100644 --- a/lib/vhost/fd_man.h +++ b/lib/vhost/fd_man.h @@ -6,6 +6,7 @@ #define _FD_MAN_H_ #include #include +#include #define MAX_FDS 1024 @@ -35,6 +36,10 @@ struct fdset { int writefd; }; } u; + + pthread_mutex_t sync_mutex; + pthread_cond_t sync_cond; + bool sync; }; -- 2.43.2