From patchwork Thu Feb 29 12:24:56 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Maxime Coquelin X-Patchwork-Id: 137480 X-Patchwork-Delegate: maxime.coquelin@redhat.com Return-Path: X-Original-To: patchwork@inbox.dpdk.org Delivered-To: patchwork@inbox.dpdk.org Received: from mails.dpdk.org (mails.dpdk.org [217.70.189.124]) by inbox.dpdk.org (Postfix) with ESMTP id 2F0CB43C36; Thu, 29 Feb 2024 13:25:14 +0100 (CET) Received: from mails.dpdk.org (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id 1AD9A41133; Thu, 29 Feb 2024 13:25:12 +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 5FAC240E4A for ; Thu, 29 Feb 2024 13:25:10 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1709209509; 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=YfMF/HXXlRzxlKZWAWTyyQb6EBFsnfGMf+BeOkJefKmIrYP0Js3e+AucbajYvuMW2+8YS1 TAbUk4lhvgUmdVM4OvZh9F88OQ9pK9jrYvXcMhHtaoMqd7yMHPWUxf95ehuQEeoBikMOhr 5PO35eGy1665eZkbGsIeMQcAC9ccdHk= 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 X-BeenThere: dev@dpdk.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: DPDK patches and discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dev-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; };