From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B29DA3F4830 for ; Thu, 16 Jul 2026 08:57:34 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784192260; cv=none; b=h2PrD2mSSq3KNbhSUGHnVAkRaVeYUE6CNpz/UsMrjfd4OEMKbVIPsOh5WCm5+yh565Yg3m6owtiuLLTZ1kt5snXkRI2cACx3QHjrmRL5MB5ARH6pU7EMQYWHkIHiSOgXjl33HRXiSeU0l/mmxUZrHlS+r6lUCO1IwSZm1KXvZXE= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784192260; c=relaxed/simple; bh=mEjKg4AVLSWrUhKw9xfuQN61xzTZPl4Xk3fNow6F/Kw=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=F3MzSzfK5w38MVUTuy0IR/VqMrliaidRAOuhIYLAveRmEZ0lOtqrPhAREhmb3k0Xr4D3zZivfuq2fGECYov0Vj6W6f9IcXZVqqJPUDrlAPNjojv+QMIwWwhHptwA56vqK7Z2O/6GX5wgP0vGM/utU20Gd/ndE6BduBVAFSNh/z8= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=ZQDgewVY; dkim=pass (2048-bit key) header.d=redhat.com header.i=@redhat.com header.b=PXGnpmu8; arc=none smtp.client-ip=170.10.133.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="ZQDgewVY"; dkim=pass (2048-bit key) header.d=redhat.com header.i=@redhat.com header.b="PXGnpmu8" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1784192251; 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: in-reply-to:in-reply-to:references:references; bh=FhxSO78Yf0RPA4wZRxf7Fd3u153hjXB7c8vtU2XnoBg=; b=ZQDgewVYA7DD3oe3ZVg7jY2BgmCNKBbSe+t26Dduv/9uLyAEG8niz/ouldT3Yu2mavRP3H RLy48DgJ8HMWcxr3iGRkFnZ1CxcKqds2bWSLT3qClC+rNKmeeSNJFhZatFJe/tzBW/hFue RbhoevmmxkMlY6SpVL/IvjwxvCruQw8= Received: from mail-wr1-f70.google.com (mail-wr1-f70.google.com [209.85.221.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-662-t-45ItZ9O5SdjBrhZFJhhA-1; Thu, 16 Jul 2026 04:57:30 -0400 X-MC-Unique: t-45ItZ9O5SdjBrhZFJhhA-1 X-Mimecast-MFC-AGG-ID: t-45ItZ9O5SdjBrhZFJhhA_1784192249 Received: by mail-wr1-f70.google.com with SMTP id ffacd0b85a97d-47485fde05aso3706808f8f.0 for ; Thu, 16 Jul 2026 01:57:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=google; t=1784192249; x=1784797049; darn=vger.kernel.org; h=in-reply-to:content-disposition:content-type:mime-version :references:message-id:subject:cc:to:from:date:from:to:cc:subject :date:message-id:reply-to:content-type; bh=FhxSO78Yf0RPA4wZRxf7Fd3u153hjXB7c8vtU2XnoBg=; b=PXGnpmu8dok8k9tzU3nIvPSEnkW6Wz6OKd19OrLDlcn/LkKcai0eezaT9/Ou1qhODf CiJwLHf9mJVlx84oQj+LQKNzxJhFmTj92WnfgMYzx2hSDHrwbB7xnBk+Y+M3Ml+8ldnL sSQSbkbUrJwcLvSvS9fFY+Vnq/UCd1v36RhwZ4SipNNAA/vat80K2yGmCCI8trLvAb56 ifXCqFUm5vlFtFp1vX/KT/96hN2P92tws7HlECHneN6ZhJ/DI80cWKwPujW0NZgegdqk RAkGnw+IYFB03o198jILu0sm9a60RhYnrFDNUMxlaj1oX8i91wbNSmbzl6zf0aD4C0oc mthw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1784192249; x=1784797049; h=in-reply-to:content-disposition:content-type:mime-version :references:message-id:subject:cc:to:from:date:x-gm-gg :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to :content-type; bh=FhxSO78Yf0RPA4wZRxf7Fd3u153hjXB7c8vtU2XnoBg=; b=TJB8qQC3TA1zhakMg7/JPlJ81LzJjgMb/h6M5puWuaklS8SQmmEoh+YS+c0EuvzNEJ NgzJnQRAr8B+oiUW9pvOW8HdCvSFBhROdIa+nIEVg7sLKPUxfiF7MJ6LahUs2J9G8GLV c15uRZh6ibKfydlR/6FBVDjvrg7lWS5h2dznvBspn7CFxWir2CnlIhaWlUKV0zBcda7F vclPiypkI8xURBWPeZHn/eVnjKKqxoIE4Uyi7CAqNHBZbcMKzJC7ULSdAEH1zcC2qn36 u4KfhBIH9Xo93aCkqwl8YnJrk1cqfgvxTJASOHY4Jqzw0Fby+diZun0Z2+Sfu7ZqCOBl 3b7g== X-Gm-Message-State: AOJu0YzUowveVSftNGvNjTwVoz5Kq4VpS/hGBOYidZF9xFpeKQwTFDWW tQ0D2pam7UVY9XI1P9vlDPxXkA7aqSB2f6QQd0ry0NfoygV8W00zzNI+VHqVrdH00mhx3RwtDar 6Jrnlm5FmDs0ePEmWB2qLG1IOx1BjZ5j4CvFFVK+rKKyHjwUppAQJbD0pMJta7K/tMQ== X-Gm-Gg: AfdE7cmhZUPtYifE4/QBKhlZIdttLYRXC+UC/yQrqaMK+bL1pxHrbw7OpM4y+FdQKQL 42kxNkiU5qVmx0xbGA7KMrzYhEZbv4qSLYRqgBgpXOhVq2eax4v2GF6OrXb4+2CN7Re49b9vU+J jqBX0oIsFHHwea/H4Y3rnVMR7yw1qE7mogNpOS6RkWckgGdivId+ccYew4Z3obdJPi1UBpy8PJg CltAeKoguGjBKxP375lxja4CEg+bfktA+funsBV8AHrit/h7UG/tS1jJ+llImdfDQgtMyrYrUr7 7lQh3KMCrxeWep40W2OcuXd/vJaMfAfRj0ECCqKHshfN41Nt2zSCbwNcmRpYGB7eNlAyPBRkJpq LFZqXorsm9Z6JabCcfqybzYO6hWQcjMx27Ft/s4gevz5uuCEuOg== X-Received: by 2002:a05:6000:46c2:b0:47f:5298:a010 with SMTP id ffacd0b85a97d-47f5298a31emr4384416f8f.41.1784192249013; Thu, 16 Jul 2026 01:57:29 -0700 (PDT) X-Received: by 2002:a05:6000:46c2:b0:47f:5298:a010 with SMTP id ffacd0b85a97d-47f5298a31emr4384387f8f.41.1784192248495; Thu, 16 Jul 2026 01:57:28 -0700 (PDT) Received: from sgarzare-redhat (host-82-53-135-65.retail.telecomitalia.it. [82.53.135.65]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-47f4635aa34sm20900447f8f.15.2026.07.16.01.57.26 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 16 Jul 2026 01:57:27 -0700 (PDT) Date: Thu, 16 Jul 2026 10:57:19 +0200 From: Stefano Garzarella To: Andrey Drobyshev Cc: linux-kernel@vger.kernel.org, kvm@vger.kernel.org, virtualization@lists.linux.dev, netdev@vger.kernel.org, mst@redhat.com, stefanha@redhat.com, dongli.zhang@oracle.com, maciej.szmigiero@oracle.com, bchaney@akamai.com, mark.kanda@oracle.com, ptikhomirov@virtuozzo.com, den@openvz.org Subject: Re: [PATCH v4 4/5] vhost: synchronize with RCU readers when freeing workers Message-ID: References: <20260714151638.143019-1-andrey.drobyshev@virtuozzo.com> <20260714151638.143019-5-andrey.drobyshev@virtuozzo.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii; format=flowed Content-Disposition: inline In-Reply-To: <20260714151638.143019-5-andrey.drobyshev@virtuozzo.com> On Tue, Jul 14, 2026 at 06:16:37PM +0300, Andrey Drobyshev wrote: >vhost_vq_work_queue() only holds the RCU read lock while it dereferences >vq->worker and queues work on it. vhost_workers_free() however clears >the vq->worker pointers and immediately frees the workers, without >waiting for a grace period. A caller that fetched the worker right >before the pointer was cleared can therefore still be queueing work on >it while it is freed. And even when the queueing itself wins the race, >the work is never run, so its VHOST_WORK_QUEUED bit stays set and all >future attempts to queue it are silently skipped. > >None of the current callers can actually hit this: net and scsi stop >their virtqueues before the workers are freed, and vsock unhashes the >device and does synchronize_rcu() of its own in vhost_vsock_dev_release() >before the workers go away. But the upcoming VHOST_RESET_OWNER support >in vhost-vsock keeps the device hashed while its workers are freed, so >the lockless send/cancel paths become able to race with the teardown. > >Close this the way vhost_worker_killed() already does: clear the >vq->worker pointers, wait for a grace period, run whatever the last >readers may have queued, and only then free the workers. The >synchronize_rcu() is skipped if the device has no workers, so cleanup of >devices which never got an owner stays cheap. > Do we need a Fixes tag for this? Thanks for pointing out that the issue wasn't occurring, but I think we should add it because it's a sneaky problem we discovered by chance. IMO the code should already have `synchronize_rcu()` after `rcu_assign_pointer()` loop. @Michael, what do you think? >Suggested-by: Stefano Garzarella >Signed-off-by: Andrey Drobyshev >--- > drivers/vhost/vhost.c | 15 +++++++++++++++ > 1 file changed, 15 insertions(+) > >diff --git a/drivers/vhost/vhost.c b/drivers/vhost/vhost.c >index 4c525b3e16ea..0d1414d40f4e 100644 >--- a/drivers/vhost/vhost.c >+++ b/drivers/vhost/vhost.c >@@ -729,6 +729,21 @@ static void vhost_workers_free(struct vhost_dev *dev) > > for (i = 0; i < dev->nvqs; i++) > rcu_assign_pointer(dev->vqs[i]->worker, NULL); >+ >+ /* >+ * vhost_vq_work_queue() reads vq->worker under rcu_read_lock(), so a >+ * caller that fetched a worker before we cleared the pointers above >+ * may still be about to queue work on it. Wait for those RCU readers >+ * to finish before freeing the worker, then run whatever they queued >+ * so nothing is left with VHOST_WORK_QUEUED set. Mirrors >+ * vhost_worker_killed(). >+ */ >+ if (!xa_empty(&dev->worker_xa)) { >+ synchronize_rcu(); >+ xa_for_each(&dev->worker_xa, i, worker) >+ vhost_run_work_list(worker); >+ } >+ Following sashiko review [1], I tried to undersand why we need this, but TBH I'm really confused. That said, this seems wrong also because it will work only with vhost_tasks, and not with kthreads. IIUC vhost_worker_killed() will be called anyway when calling vhost_worker_destroy(). For vhost_tasks, it will call vhost_task_do_stop() that calls vhost_task_stop(). This sets VHOST_TASK_FLAGS_STOP and wait the worker on vtsk->exited before freeing stuff. The worker breaks the loop and calls vtsk->handle_sigkill() that is exactly vhost_worker_killed() you mentioned we are mirroring here. So, why we need this? Should be enough to call synchronize_rcu() in any case after the rcu_assign_pointer() loop? Thanks, Stefano [1] https://sashiko.dev/#/patchset/20260714151638.143019-1-andrey.drobyshev@virtuozzo.com?part=4