From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796384322; b=2IJcKR7SAcqB8UZ53PCpKL/D7/rII1rwqJ8swxA8feifzGef/ndIojOrPyKF11dqTraA KAYXTH7rs4qtyEfIcqjWHC5I9kDdxYtjo3CIxr51q4CuywKRfp1ip3geCSKwdley1v3QK 6V6dyt9bt3YAujUM0lNGJ5WbUxqmpO7wtsLCf+3VPO7bZ1C/QJC/QXctiKlOaZsQZDTJg xEZeRVscfr3yQSs9aPrfQ5SiRlyZfIJqI2Vd2SPcE3f+oCk+aVji8JU8xNGFjPn2fHUOz L0afSQprNEHTJI33RBaPPqIaO2SeRJYqta+naiJuFu40sxXt0fX4UL7DE2FmwVCAAcw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796384322; h=Received:DKIM-Signature:From:To:Cc:Subject:Date:Message-ID:X-Mailer: In-Reply-To:References:User-Agent:X-stable:X-Patchwork-Hint: MIME-Version:Content-Transfer-Encoding; bh=WA9pblSL06h4jtECKjr7WKT9v3ZAEONs8lIEAK+ig+Y=; b=tobXV7aSWXjKf5vyC0igMSikGKoY0QswbkIdZdo7bfBgNpN0UzyfhMVcGuWbL8esaD97 2Dr9TtdgXgiYuf5DhEa4voft/SKJ4Wa0zydQNAMf+qw5KVrzcBH8Kx2p37k1qF3ns/2dh LgcgveQIZHLRxUEg1M8+d6D/YR/X8/4hI6Ils6dRXMKgUOOg40xamFQUyyn8IVpEXaHgp zl36eAYp8mKwW015vj74XIBA6qRlCwvHsWR5E13waftKJNVRuRsCLLJSVCHkyH/vxvsRp g5JIAMgR7gC5g90ULrzbseRIiP0H8VeAfNpEffo8UvupE8vSOHXB7J+a3L+1CeqUX3w== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=linuxfoundation.org header.i=@linuxfoundation.org header.a=rsa-sha256 header.s=korg header.b=QUZg82bc; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 2DC66606A32D; Fri, 4 Dec 2026 11:38:42 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.a=rsa-sha256 header.s=korg header.b=QUZg82bc Received: by smtp.kernel.org (Postfix) with ESMTPSA id 9E4B31F000C2; Fri, 4 Dec 2026 11:38:41 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linuxfoundation.org; s=korg; t=1796384322; bh=WA9pblSL06h4jtECKjr7WKT9v3ZAEONs8lIEAK+ig+Y=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=QUZg82bc55LMiuElh36NN3pfj6xGdftZgVP3o3xbSk2dBVoXw5M1bPaVYGQonyeoQ gKvTk3zbKE+NpUERrBUlXHXEqbOKPc0jh7+mw3hQ62Gbgf81MWXtSHEUDIg/m0+K7T KmdhZ6XLsWBnoDDzxS9AUoM7m1bg7HNUA6e9jig0= From: Greg Kroah-Hartman To: stable@vger.kernel.org Cc: Greg Kroah-Hartman , patches@lists.linux.dev, Yu Zhang , "Michael S. Tsirkin" , Sasha Levin Subject: [PATCH 6.6 0101/1167] vhost-vdpa: drop the parents vq callback before the call fd is released Date: Fri, 4 Dec 2026 18:52:08 +0900 Message-ID: <20261204100704.726343179@linuxfoundation.org> X-Mailer: git-send-email 2.56.1 In-Reply-To: <20261204100702.047403948@linuxfoundation.org> References: <20261204100702.047403948@linuxfoundation.org> User-Agent: quilt/0.69 X-stable: review X-Patchwork-Hint: ignore X-Mailing-List: patches@lists.linux.dev List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 6.6-stable review patch. If anyone has any objections, please let me know. ------------------ From: Yu Zhang [ Upstream commit c9c1c1b7b31fc8b9a5d6c04f5f0ae647d081a39d ] VHOST_SET_VRING_CALL releases the previous call eventfd inside vhost_vring_ioctl() -- it swaps the new context into vq->call_ctx.ctx and then eventfd_ctx_put()s the old one, which is a synchronous kfree(). The parent vdpa device is only told about the change afterwards, when vhost_vdpa_vring_ioctl() reaches ops->set_vq_cb(). Parent drivers cache the pointer handed to them in vdpa_callback::trigger and do not take a reference on it, so throughout that window the parent holds a dangling eventfd_ctx and may signal it. The documentation added with the field describes what signalling it means but says nothing about how long it stays valid. This is the same hazard that "vhost_vdpa: assign irq bypass producer token correctly" addressed for the irq bypass producer token, by moving vhost_vdpa_unsetup_vq_irq() ahead of the vhost_vring_ioctl() call. The producer token was only one of the two consumers of that pointer; the one the parent keeps via ->set_vq_cb() was left behind the free. With VDUSE the window is directly reachable from userspace, because the device emulation daemon can inject an interrupt at any time from a different fd, and neither side shares a lock with the other: VDUSE takes vq->irq_lock, vhost takes vhost_dev.mutex + vq->mutex. BUG: KASAN: slab-use-after-free in _raw_spin_lock_irqsave+0x76/0xe0 Write of size 4 at addr ffff8881084e8788 by task vduse_uaf/2987 _raw_spin_lock_irqsave+0x76/0xe0 eventfd_signal_mask+0x69/0x120 vduse_dev_ioctl+0x337/0x1a60 <- vduse_vq_signal_irqfd(), inlined __x64_sys_ioctl+0x120/0x170 <- VDUSE_VQ_INJECT_IRQ Allocated by task 2986: do_eventfd+0x50/0x200 __x64_sys_eventfd2+0x2e/0x40 kmalloc-64, freed 64-byte region [ffff8881084e8780, ffff8881084e87c0) One thread loops VHOST_SET_VRING_CALL on /dev/vhost-vdpa-N with a fresh eventfd and then unbinds it, while another loops VDUSE_VQ_INJECT_IRQ on /dev/vduse/. This reproduces in 5 out of 5 ten-second runs on v7.1.6 and 3 out of 3 on v7.2-rc6. With the patch there are no reports in 3 out of 3 runs on either, while the same workload still gets ~30000 interrupts per run delivered into live eventfds, so the path is still being exercised. Tell the parent to drop the callback before vhost_vring_ioctl() can free the eventfd, mirroring what is already done for the bypass producer, and restore it if the ioctl fails -- on failure the swap never happened, the old context is still installed, and leaving the parent without a callback would silently drop that vq's interrupts. Clearing the callback only stops a parent from starting to use the context; a handler that already loaded it can still be running. For parents with a real interrupt, wait for it with synchronize_irq() on the vq's irq. VDUSE does not implement get_vq_irq, so that case stays covered by the teardown above. Fixes: 5e68470f4e80 ("vdpa: Add eventfd for the vdpa callback") Signed-off-by: Yu Zhang Signed-off-by: Michael S. Tsirkin Message-ID: <20260918040436.47982-1-yuz08559@gmail.com> Signed-off-by: Sasha Levin --- drivers/vhost/vdpa.c | 35 ++++++++++++++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/drivers/vhost/vdpa.c b/drivers/vhost/vdpa.c index 9a4f33b0ccf7a..ac0da85b6df81 100644 --- a/drivers/vhost/vdpa.c +++ b/drivers/vhost/vdpa.c @@ -711,13 +711,46 @@ static long vhost_vdpa_vring_ioctl(struct vhost_vdpa *v, unsigned int cmd, if (ops->get_status(vdpa) & VIRTIO_CONFIG_S_DRIVER_OK) vhost_vdpa_unsetup_vq_irq(v, idx); + /* + * The parent caches call_ctx.ctx in cb.trigger without + * holding a reference, so it has to stop using it + * before vhost_vring_ioctl() drops the last one. + */ + cb.callback = NULL; + cb.private = NULL; + cb.trigger = NULL; + ops->set_vq_cb(vdpa, idx, &cb); + + /* + * A parent interrupt handler that loaded the callback + * before that store can still be running, so wait for + * it to finish with the context too. + */ + if (ops->get_vq_irq) { + int irq = ops->get_vq_irq(vdpa, idx); + + if (irq >= 0) + synchronize_irq(irq); + } } break; } r = vhost_vring_ioctl(&v->vdev, cmd, argp); - if (r) + if (r) { + /* + * A failure here means the swap never happened and the old + * context is still installed, so give the parent back the + * callback that was torn down above. + */ + if (cmd == VHOST_SET_VRING_CALL && vq->call_ctx.ctx) { + cb.callback = vhost_vdpa_virtqueue_cb; + cb.private = vq; + cb.trigger = vq->call_ctx.ctx; + ops->set_vq_cb(vdpa, idx, &cb); + } return r; + } switch (cmd) { case VHOST_SET_VRING_ADDR: -- 2.53.0