Skip to contentSkip to conversation

vfio/ccw: Flush notoper_work before returning from reset

PATCH In review Yuho Choi 4 participants 6 messages Nov 6 – Nov 9

Subsystems (4)
lore
Share & tools
lore

Yuho Choi

author
#0 lore raw

PATCH v2 1/1 vfio/ccw: Flush notoper_work before returning from reset

vfio_ccw_dma_unmap() is the DMA invalidation callback, which must unpin the pages covering the range before it returns. It calls vfio_ccw_mdev_reset(), which raises a CLOSE event.

On the normal path fsm_close() calls cp_free() itself, so the pages are unpinned synchronously. But if the subchannel cannot be disabled or quiesced, fsm_close() falls through to a NOT_OPER event. Since commit 0c11f61a876e ("s390/vfio_ccw: Move cp cleanup out of not operational"), fsm_notoper() only queues notoper_work, which is where cp_free() then runs. That commit flushes the work in vfio_ccw_mdev_close_device() and vfio_ccw_mdev_release_dev(), but nothing waits for it on the reset path, so the callback returns with the pages still pinned.

Because notoper_work runs asynchronously, vfio_dma_do_unmap() can exhaust its 10 retries before cp_free() completes, hitting BUG_ON(++retries > 10) and taking the host down. A further CLOSE cannot help either, as both CLOSE and OPEN are fsm_nop in the NOT_OPER state.

Flush notoper_work after the CLOSE event, as vfio_ccw_mdev_close_device() already does. The flush returns immediately when the work was never queued, so the normal path is unaffected.

Fixes 0c11f61a876e ("s390/vfio_ccw: Move cp cleanup out of not operational")

---
v2:
- Point the Fixes tag at 0c11f61a876e, which moved cp_free() into notoper_work. v1 named ce4b4657ff18 by mistake.
- Mention that commit in the changelog.
- Rebased on v7.4-rc1.

v1: https://lore.kernel.org/r/[email protected]/

Found by code review. Cross-compiled for s390 with gcc 14.2.0, W=1 clean, but not tested on s390 hardware and I have no reproducer.

VFIO_DEVICE_RESET also reaches vfio_ccw_mdev_reset() and benefits from this flush, ensuring channel program memory is cleanly released before the reset ioctl returns.

On locking: the flush is not called with io_mutex held. cp_iova_pinned() takes and drops it before vfio_ccw_mdev_reset() runs, vfio_ccw_fsm_event() takes no lock, and the VFIO_DEVICE_RESET path holds nothing. This matches vfio_ccw_mdev_close_device(), which already flushes the same work.

 drivers/s390/cio/vfio_ccw_ops.c | 8 ++++++++
 1 file changed, 8 insertions(+)
drivers/s390/cio/vfio_ccw_ops.c +8 -0
index 5ce91285c7d5..8e0a2edbd34a 100644
@@ -25,6 +25,14 @@ static int vfio_ccw_mdev_reset(struct vfio_ccw_private *private)
 	 * and re-opening the mdev, return an error.
 	 */
 	vfio_ccw_fsm_event(private, VFIO_CCW_EVENT_CLOSE);
+
+	/*
+	 * A failed CLOSE leaves the FSM Not Operational and defers cp_free()
+	 * to notoper_work. Wait for it, so the channel program pages are
+	 * unpinned before this returns.
+	 */
+	flush_work(&private->notoper_work);
+
 	vfio_ccw_fsm_event(private, VFIO_CCW_EVENT_OPEN);
 	if (private->state == VFIO_CCW_STATE_NOT_OPER)
 		return -EINVAL;

base-commit: 6628e7d4c6b25c8d18f1345e3a20d21829aae7e4

Signature · 1 line
2.43.0
#1 lore raw
Sashiko has reviewed this patch and found no issues.
Quoted 18 lines

vfio_ccw_dma_unmap() is the DMA invalidation callback, which must unpin the pages covering the range before it returns. It calls vfio_ccw_mdev_reset(), which raises a CLOSE event.

On the normal path fsm_close() calls cp_free() itself, so the pages are unpinned synchronously. But if the subchannel cannot be disabled or quiesced, fsm_close() falls through to a NOT_OPER event. Since commit 0c11f61a876e ("s390/vfio_ccw: Move cp cleanup out of not operational"), fsm_notoper() only queues notoper_work, which is where cp_free() then runs. That commit flushes the work in vfio_ccw_mdev_close_device() and vfio_ccw_mdev_release_dev(), but nothing waits for it on the reset path, so the callback returns with the pages still pinned.

Because notoper_work runs asynchronously, vfio_dma_do_unmap() can exhaust its 10 retries before cp_free() completes, hitting BUG_ON(++retries > 10) and taking the host down. A further CLOSE cannot help either, as both CLOSE and OPEN are fsm_nop in the NOT_OPER state.

Quoted 6 lines

Flush notoper_work after the CLOSE event, as vfio_ccw_mdev_close_device() already does. The flush returns immediately when the work was never queued, so the normal path is unaffected.

Fixes 0c11f61a876e ("s390/vfio_ccw: Move cp cleanup out of not operational")

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
#2 lore raw
In reply to Yuho Choi

Am 06.11.26 um 20:10 schrieb Yuho Choi:

Quoted 13 lines

vfio_ccw_dma_unmap() is the DMA invalidation callback, which must unpin the pages covering the range before it returns. It calls vfio_ccw_mdev_reset(), which raises a CLOSE event.

On the normal path fsm_close() calls cp_free() itself, so the pages are unpinned synchronously. But if the subchannel cannot be disabled or quiesced, fsm_close() falls through to a NOT_OPER event. Since commit 0c11f61a876e ("s390/vfio_ccw: Move cp cleanup out of not operational"), fsm_notoper() only queues notoper_work, which is where cp_free() then runs. That commit flushes the work in vfio_ccw_mdev_close_device() and vfio_ccw_mdev_release_dev(), but nothing waits for it on the reset path, so the callback returns with the pages still pinned.

Quoted 4 lines

Because notoper_work runs asynchronously, vfio_dma_do_unmap() can exhaust its 10 retries before cp_free() completes, hitting BUG_ON(++retries > 10) and taking the host down. A further CLOSE cannot help either, as both CLOSE and OPEN are fsm_nop in the NOT_OPER state.

Does the the iommufd backend have the same problem? iopt_unmap_iova_range() keeps calling the unmap callback and gives up with WARN_ON(tries > 100) and -EDEADLOCK. So that path is also fixed, I guess.

[..]

Quoted 1 line
Fixes 0c11f61a876e ("s390/vfio_ccw: Move cp cleanup out of not operational")

cc stable ?

Quoted 1 line

Eric, Matt,

can you have a look?

[..]

#3 lore raw
Quoted 19 lines · Christian Borntraeger

Am 06.11.26 um 20:10 schrieb Yuho Choi:

Quoted 13 lines

vfio_ccw_dma_unmap() is the DMA invalidation callback, which must unpin the pages covering the range before it returns. It calls vfio_ccw_mdev_reset(), which raises a CLOSE event.

On the normal path fsm_close() calls cp_free() itself, so the pages are unpinned synchronously. But if the subchannel cannot be disabled or quiesced, fsm_close() falls through to a NOT_OPER event. Since commit 0c11f61a876e ("s390/vfio_ccw: Move cp cleanup out of not operational"), fsm_notoper() only queues notoper_work, which is where cp_free() then runs. That commit flushes the work in vfio_ccw_mdev_close_device() and vfio_ccw_mdev_release_dev(), but nothing waits for it on the reset path, so the callback returns with the pages still pinned.

Quoted 4 lines

Because notoper_work runs asynchronously, vfio_dma_do_unmap() can exhaust its 10 retries before cp_free() completes, hitting BUG_ON(++retries > 10) and taking the host down. A further CLOSE cannot help either, as both CLOSE and OPEN are fsm_nop in the NOT_OPER state.

Quoted 4 lines

Does the the iommufd backend have the same problem? iopt_unmap_iova_range() keeps calling the unmap callback and gives up with WARN_ON(tries > 100) and -EDEADLOCK. So that path is also fixed, I guess.

Drivers are not allowed to refuse the shootdown, it is broken if someone has done this.

Jason

#4 lore raw

Am 09.11.26 um 15:49 schrieb Jason Gunthorpe:

Quoted 20 lines · Christian Borntraeger
Quoted 19 lines

Am 06.11.26 um 20:10 schrieb Yuho Choi:

Quoted 17 lines

vfio_ccw_dma_unmap() is the DMA invalidation callback, which must unpin the pages covering the range before it returns. It calls vfio_ccw_mdev_reset(), which raises a CLOSE event.

On the normal path fsm_close() calls cp_free() itself, so the pages are unpinned synchronously. But if the subchannel cannot be disabled or quiesced, fsm_close() falls through to a NOT_OPER event. Since commit 0c11f61a876e ("s390/vfio_ccw: Move cp cleanup out of not operational"), fsm_notoper() only queues notoper_work, which is where cp_free() then runs. That commit flushes the work in vfio_ccw_mdev_close_device() and vfio_ccw_mdev_release_dev(), but nothing waits for it on the reset path, so the callback returns with the pages still pinned.

Because notoper_work runs asynchronously, vfio_dma_do_unmap() can exhaust its 10 retries before cp_free() completes, hitting BUG_ON(++retries > 10) and taking the host down. A further CLOSE cannot help either, as both CLOSE and OPEN are fsm_nop in the NOT_OPER state.

Quoted 7 lines
Quoted 4 lines

Does the the iommufd backend have the same problem? iopt_unmap_iova_range() keeps calling the unmap callback and gives up with WARN_ON(tries > 100) and -EDEADLOCK. So that path is also fixed, I guess.

Drivers are not allowed to refuse the shootdown, it is broken if someone has done this.

Agreed, but isnt that what the patch fixes? when CLOSE fails, vfio-ccw currently returns from the unmap callback with the pages still pinned and leaves the unpin to notoper_work. With the flush the pages are unpinned before the callback returns, so the driver keeps the contract on both backends.

Quoted 1 line

Jason

author
#5 lore raw
In reply to Christian Borntraeger
Quoted 4 lines · Christian Borntraeger

Does the the iommufd backend have the same problem? iopt_unmap_iova_range() keeps calling the unmap callback and gives up with WARN_ON(tries > 100) and -EDEADLOCK. So that path is also fixed, I guess.

Yes. With iommufd, vfio_ccw_dma_unmap() is reached through vfio_emulated_unmap(), so the pages are left pinned in the same way. iopt_unmap_iova_range() then retries, and if notoper_work has not run by then, it warns and fails the unmap with -EDEADLOCK instead of hitting the BUG_ON. With the flush, the pages are unpinned before the callback returns on both backends.

Quoted 2 lines
Quoted 1 line
Fixes 0c11f61a876e ("s390/vfio_ccw: Move cp cleanup out of not operational")

cc stable ?

I'm not sure, so I'll leave that to you.