virtio_balloon: quiesce balloon work before device shutdown
Subsystems (2)
v2 cover virtio_balloon: quiesce balloon work before device shutdown
Hi,
This backports the fix for CVE-2026-90129 to 6.12.y, the same four commits 6.18.y has:
183a08715af1 virtgpu: don't reset on shutdown
0d8aebe089b4 virtio: add virtio_device_shutdown() helper 29536a923a94 virtio_balloon: factor out virtballoon_quiesce() 7e17eef04600 virtio_balloon: quiesce balloon work before device shutdown
Since 8bd2fa086a04 the generic virtio shutdown breaks and resets the device while the balloon's work items can still be running against it. Patch 4 quiesces that work first. Patches 1-3 are what it builds on.
3 and 4 are clean picks. 1 and 2 needed small changes. 6.12 has no reset_prepare/reset_done in struct virtio_driver, so patch 1 adds only the .shutdown member. 6.12 has no virtio_irq_get_affinity(), so patch 2 puts the new helper right above virtio_dev_shutdown(). The function bodies match upstream.
Tested on v6.12.119:
- x86_64 allmodconfig W=1 build of the touched objects, base vs patched, no new warnings
- x86_64 defconfig plus the touched virtio options boots and powers off under QEMU/KVM
- The race needs a balloon with free page reporting plus a reboot or kexec while it's busy. I didn't reproduce that.
6.1.y and 6.6.y need this too. I'll follow up for those once they're tested.
Changes in v2
v2: resent through a whitespace-safe path; v1 was damaged by my mail client and does not apply.
The backport and the test runs were prepared with AI coding tools (OpenAI Codex agents, with review by Claude). I went over the result before signing off.
Thanks,
Dickson
---
Denis V. Lunev (3):
virtio: add virtio_device_shutdown() helper
virtio_balloon: factor out virtballoon_quiesce()
virtio_balloon: quiesce balloon work before device shutdown
Michael S. Tsirkin (1):
virtgpu: don't reset on shutdown
drivers/gpu/drm/virtio/virtgpu_drv.c | 9 ++++++++ drivers/virtio/virtio.c | 41 ++++++++++++++++++++++++++---------- drivers/virtio/virtio_balloon.c | 34 ++++++++++++++++++++++++------ include/linux/virtio.h | 4 ++++ 4 files changed, 70 insertions(+), 18 deletions(-)
---
base-commit: e2e4b92c2b2a3ae78423cb343951cb5d2241b405
change-id: 20261126-virtio-final-875140b7fdaa
Best regards,
Signature · 1 line
Dickson Wu <[email protected]>
PATCH v2 1/4 virtgpu: don't reset on shutdown
From: "Michael S. Tsirkin" <[email protected]>
commit 183a08715af1491d381b4e22efd61578fbe05fa5 upstream.
It looks like GPUs are used after shutdown is invoked.
Thus, breaking virtio gpu in the shutdown callback is not a good idea - guest hangs attempting to finish console drawing, with these warnings:
[ 20.504464] WARNING: CPU: 0 PID: 568 at drivers/gpu/drm/virtio/virtgpu_vq.c:358 virtio_gpu_queue_ctrl_sgs+0x236/0x290 [virtio_gpu] [ 20.505685] Modules linked in: nft_fib_inet nft_fib_ipv4 nft_fib_ipv6 nft_fib nft_reject_inet nf_reject_ipv4 nf_reject_ipv6 nft_reject nft_ct nft_chain_nat nf_nat nf_conntrack nf_defrag_ipv6 nf_defrag_ipv4 rfkill ip_set nf_tables nfnetlink vfat fat intel_rapl_msr intel_rapl_common intel_uncore_frequency_common nfit libnvdimm kvm_intel kvm rapl iTCO_wdt iTCO_vendor_support virtio_gpu virtio_dma_buf pcspkr drm_shmem_helper i2c_i801 drm_kms_helper lpc_ich i2c_smbus virtio_balloon joydev drm fuse xfs libcrc32c ahci libahci crct10dif_pclmul crc32_pclmul crc32c_intel libata virtio_net ghash_clmulni_intel net_failover virtio_blk failover serio_raw dm_mirror dm_region_hash dm_log dm_mod [ 20.511847] CPU: 0 PID: 568 Comm: kworker/0:3 Kdump: loaded Tainted: G W ------- --- 5.14.0-578.6675_1757216455.el9.x86_64 #1 [ 20.513157] Hardware name: Red Hat KVM/RHEL, BIOS edk2-20241117-3.el9 11/17/2024 [ 20.513918] Workqueue: events drm_fb_helper_damage_work [drm_kms_helper] [ 20.514626] RIP: 0010:virtio_gpu_queue_ctrl_sgs+0x236/0x290 [virtio_gpu] [ 20.515332] Code: 00 00 48 85 c0 74 0c 48 8b 78 08 48 89 ee e8 51 50 00 00 65 ff 0d 42 e3 74 3f 0f 85 69 ff ff ff 0f 1f 44 00 00 e9 5f ff ff ff <0f> 0b e9 3f ff ff ff 48 83 3c 24 00 74 0e 49 8b 7f 40 48 85 ff 74 [ 20.517272] RSP: 0018:ff34f0a8c0787ad8 EFLAGS: 00010282 [ 20.517820] RAX: 00000000fffffffb RBX: 0000000000000000 RCX: 0000000000000820 [ 20.518565] RDX: 0000000000000000 RSI: ff34f0a8c0787be0 RDI: ff218bef03a26300 [ 20.519308] RBP: ff218bef03a26300 R08: 0000000000000001 R09: ff218bef07224360 [ 20.520059] R10: 0000000000008dc0 R11: 0000000000000002 R12: ff218bef02630028 [ 20.520806] R13: ff218bef0263fb48 R14: ff218bef00cb8000 R15: ff218bef07224360 [ 20.521555] FS: 0000000000000000(0000) GS:ff218bef7ba00000(0000) knlGS:0000000000000000 [ 20.522397] CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033 [ 20.522996] CR2: 000055ac4f7871c0 CR3: 000000010b9f2002 CR4: 0000000000771ef0 [ 20.523740] DR0: 0000000000000000 DR1: 0000000000000000 DR2: 0000000000000000 [ 20.524477] DR3: 0000000000000000 DR6: 00000000fffe07f0 DR7: 0000000000000400 [ 20.525223] PKRU: 55555554 [ 20.525515] Call Trace: [ 20.525777] <TASK> [ 20.526003] ? show_trace_log_lvl+0x1c4/0x2df [ 20.526464] ? show_trace_log_lvl+0x1c4/0x2df [ 20.526925] ? virtio_gpu_queue_fenced_ctrl_buffer+0x82/0x2c0 [virtio_gpu] [ 20.527643] ? virtio_gpu_queue_ctrl_sgs+0x236/0x290 [virtio_gpu] [ 20.528282] ? __warn+0x7e/0xd0 [ 20.528621] ? virtio_gpu_queue_ctrl_sgs+0x236/0x290 [virtio_gpu] [ 20.529256] ? report_bug+0x100/0x140 [ 20.529643] ? handle_bug+0x3c/0x70 [ 20.530010] ? exc_invalid_op+0x14/0x70 [ 20.530421] ? asm_exc_invalid_op+0x16/0x20 [ 20.530862] ? virtio_gpu_queue_ctrl_sgs+0x236/0x290 [virtio_gpu] [ 20.531506] ? virtio_gpu_queue_ctrl_sgs+0x174/0x290 [virtio_gpu] [ 20.532148] virtio_gpu_queue_fenced_ctrl_buffer+0x82/0x2c0 [virtio_gpu] [ 20.532843] virtio_gpu_primary_plane_update+0x3e2/0x460 [virtio_gpu] [ 20.533520] drm_atomic_helper_commit_planes+0x108/0x320 [drm_kms_helper] [ 20.534233] drm_atomic_helper_commit_tail+0x45/0x80 [drm_kms_helper] [ 20.534914] commit_tail+0xd2/0x130 [drm_kms_helper] [ 20.535446] drm_atomic_helper_commit+0x11b/0x140 [drm_kms_helper] [ 20.536097] drm_atomic_commit+0xa4/0xe0 [drm] [ 20.536588] ? __pfx___drm_printfn_info+0x10/0x10 [drm] [ 20.537162] drm_atomic_helper_dirtyfb+0x192/0x270 [drm_kms_helper] [ 20.537823] drm_fbdev_shmem_helper_fb_dirty+0x43/0xa0 [drm_shmem_helper] [ 20.538536] drm_fb_helper_damage_work+0x87/0x160 [drm_kms_helper] [ 20.539188] process_one_work+0x194/0x380 [ 20.539612] worker_thread+0x2fe/0x410 [ 20.540007] ? __pfx_worker_thread+0x10/0x10 [ 20.540456] kthread+0xdd/0x100 [ 20.540791] ? __pfx_kthread+0x10/0x10 [ 20.541190] ret_from_fork+0x29/0x50 [ 20.541566] </TASK> [ 20.541802] ---[ end trace 0000000000000000 ]---
It looks like the shutdown is called in the middle of console drawing, so we should either wait for it to finish, or let drm handle the shutdown.
This patch implements this second option:
Add an option for drivers to bypass the common break+reset handling. As DRM is careful to flush/synchronize outstanding buffers, it looks like GPU can just have a NOP there.
Message-Id: <8490dbeb6f79ed039e6c11d121002618972538a3.1744293540.git.mst@redhat.com>
--- drivers/gpu/drm/virtio/virtgpu_drv.c | 9 +++++++++ drivers/virtio/virtio.c | 6 ++++++ include/linux/virtio.h | 3 +++ 3 files changed, 18 insertions(+)
Changes in v2
v2: resent through a whitespace-safe path; v1 <CAFaZ9eqZCgDaXJ=2QSBmHVz9WEL4cpthQX02xVesUB5G3NRQWg@mail.gmail.com> was damaged by my mail client and does not apply.
The backport and the test runs were prepared with AI coding tools (OpenAI Codex agents, with review by Claude). I went over the result before signing off.
drivers/gpu/drm/virtio/virtgpu_drv.c +9 -0
@@ -126,6 +126,14 @@ static void virtio_gpu_remove(struct virtio_device *vdev)drm_dev_put (dev); } +static void virtio_gpu_shutdown (struct virtio_device *vdev) +{ +/* +* drm does its own synchronization on shutdown. +* Do nothing here, opt out of device reset. +*/ +} +static void virtio_gpu_config_changed (struct virtio_device *vdev) {struct drm_device *dev = vdev->priv;
@@ -160,6 +168,7 @@ static struct virtio_driver virtio_gpu_driver = { .id_table = id_table, .probe = virtio_gpu_probe, .remove = virtio_gpu_remove, + .shutdown = virtio_gpu_shutdown, .config_changed = virtio_gpu_config_changed };
drivers/virtio/virtio.c +6 -0
@@ -391,6 +391,12 @@ static void virtio_dev_shutdown(struct device *_d)if (!drv)return ; +/* If the driver has its own shutdown method, use that. */ +if (drv->shutdown) { + drv->shutdown (dev); +return ; + } +/* * Some devices get wedged if you kick them after they are * reset. Mark all vqs as broken to make sure we don't.
include/linux/virtio.h +3 -0
@@ -204,6 +204,8 @@ size_t virtio_max_dma_size(const struct virtio_device *vdev);* changes; may be called in interrupt context. * @freeze: optional function to call during suspend/hibernation. * @restore: optional function to call on resume. +* @shutdown: synchronize with the device on shutdown. If provided, replaces +* the virtio core implementation. */ struct virtio_driver {struct device_driver driver;
@@ -219,6 +221,7 @@ struct virtio_driver {void (*config_changed)(struct virtio_device *dev);int (*freeze)(struct virtio_device *dev);int (*restore)(struct virtio_device *dev); +void (*shutdown)(struct virtio_device *dev); };#define drv_to_virtio (__drv) container_of_const(__drv, struct virtio_driver, driver)
PATCH v2 2/4 virtio: add virtio_device_shutdown() helper
In reply to Dickson WuFrom: "Denis V. Lunev" <[email protected]>
commit 0d8aebe089b4ba887e792884cf041ce9f1040ff4 upstream.
The generic virtio bus .shutdown handler, virtio_dev_shutdown(), breaks and resets a device once it has established that the driver has no .shutdown of its own. A driver that does implement .shutdown, to quiesce its own activity first, still needs the same break and reset afterwards and would otherwise have to open code it.
Factor the break + synchronize_cbs + reset sequence out of virtio_dev_shutdown() into an exported virtio_device_shutdown() helper so such drivers can reuse it instead of duplicating the core logic.
No functional change.
Message-ID: <[email protected]>
--- drivers/virtio/virtio.c | 41 +++++++++++++++++++++++++++-------------- include/linux/virtio.h | 1 + 2 files changed, 28 insertions(+), 14 deletions(-)
Changes in v2
v2: resent through a whitespace-safe path; v1 <CAFaZ9eovUow4CR0U-iaUtFHbvf2Dk97Vs6hz6hZWh747R6Xr-Q@mail.gmail.com> was damaged by my mail client and does not apply.
The backport and the test runs were prepared with AI coding tools (OpenAI Codex agents, with review by Claude). I went over the result before signing off.
drivers/virtio/virtio.c +27 -14
@@ -379,6 +379,32 @@ static void virtio_dev_remove(struct device *_d)of_node_put (dev->dev.of_node); } +/** +* virtio_device_shutdown - break and reset a device on shutdown +* @dev: the device +* +* Drivers with their own .shutdown method should quiesce their activity and +* then call this to stop the device the way the generic shutdown path does. +*/ +void virtio_device_shutdown (struct virtio_device *dev) +{ +/* +* Some devices get wedged if you kick them after they are +* reset. Mark all vqs as broken to make sure we don't. +*/ +virtio_break_device (dev); +/* +* Guarantee that any callback will see vq->broken as true. +*/ +virtio_synchronize_cbs (dev); +/* +* As IOMMUs are reset on shutdown, this will block device access to memory. +* Some devices get wedged if this happens, so reset to make sure it does not. +*/ + dev->config->reset (dev); +} +EXPORT_SYMBOL_GPL (virtio_device_shutdown); +static void virtio_dev_shutdown (struct device *_d) {struct virtio_device *dev =dev_to_virtio (_d);
@@ -397,20 +423,7 @@ static void virtio_dev_shutdown(struct device *_d)return ; } -/* -* Some devices get wedged if you kick them after they are -* reset. Mark all vqs as broken to make sure we don't. -*/ -virtio_break_device (dev); -/* -* Guarantee that any callback will see vq->broken as true. -*/ -virtio_synchronize_cbs (dev); -/* -* As IOMMUs are reset on shutdown, this will block device access to memory. -* Some devices get wedged if this happens, so reset to make sure it does not. -*/ - dev->config->reset (dev); +virtio_device_shutdown (dev); }static const struct bus_type virtio_bus = {
include/linux/virtio.h +1 -0
@@ -180,6 +180,7 @@ int virtio_device_freeze(struct virtio_device *dev);int virtio_device_restore (struct virtio_device *dev);#endif void virtio_reset_device (struct virtio_device *dev); +void virtio_device_shutdown (struct virtio_device *dev);size_t virtio_max_dma_size (const struct virtio_device *vdev);
PATCH v2 3/4 virtio_balloon: factor out virtballoon_quiesce()
In reply to Dickson WuFrom: "Denis V. Lunev" <[email protected]>
commit 29536a923a9412812eb3e378258019b54538a220 upstream.
virtballoon_remove() stops all of the balloon's asynchronous work (the free page reporting worker, the inflate/deflate and stats workers, the OOM notifier and the free page shrinker) before tearing the device down. A following change needs the same teardown from a .shutdown handler, so move it into a virtballoon_quiesce() helper.
No functional change.
Message-ID: <[email protected]>
--- drivers/virtio/virtio_balloon.c | 27 ++++++++++++++++++++------- 1 file changed, 20 insertions(+), 7 deletions(-)
Changes in v2
v2: resent through a whitespace-safe path; v1 <CAFaZ9eopsb3vgcpzOjiZ500oafv02QzNV3dEUrvOMN1vV90RuQ@mail.gmail.com> was damaged by my mail client and does not apply.
The backport and the test runs were prepared with AI coding tools (OpenAI Codex agents, with review by Claude). I went over the result before signing off.
drivers/virtio/virtio_balloon.c +20 -7
@@ -1121,26 +1121,39 @@ static void remove_common(struct virtio_balloon *vb) vb->vdev->config->del_vqs (vb->vdev); } -static void virtballoon_remove (struct virtio_device *vdev) +/* +* Stop all asynchronous balloon work. The device must still be alive so that +* in-flight requests can drain via the host before it is reset or freed. +*/ +static void virtballoon_quiesce (struct virtio_balloon *vb) { -struct virtio_balloon *vb = vdev->priv; +struct virtio_device *vdev = vb->vdev; -if (virtio_has_feature (vb->vdev,VIRTIO_BALLOON_F_REPORTING )) +if (virtio_has_feature (vdev,VIRTIO_BALLOON_F_REPORTING ))page_reporting_unregister (&vb->pr_dev_info); -if (virtio_has_feature (vb->vdev,VIRTIO_BALLOON_F_DEFLATE_ON_OOM )) +if (virtio_has_feature (vdev,VIRTIO_BALLOON_F_DEFLATE_ON_OOM ))unregister_oom_notifier (&vb->oom_nb); -if (virtio_has_feature (vb->vdev,VIRTIO_BALLOON_F_FREE_PAGE_HINT )) +if (virtio_has_feature (vdev,VIRTIO_BALLOON_F_FREE_PAGE_HINT ))virtio_balloon_unregister_shrinker (vb); +spin_lock_irq (&vb->stop_update_lock); vb->stop_update = true;spin_unlock_irq (&vb->stop_update_lock);cancel_work_sync (&vb->update_balloon_size_work);cancel_work_sync (&vb->update_balloon_stats_work); -if (virtio_has_feature (vdev,VIRTIO_BALLOON_F_FREE_PAGE_HINT )) { +if (virtio_has_feature (vdev,VIRTIO_BALLOON_F_FREE_PAGE_HINT ))cancel_work_sync (&vb->report_free_page_work); +} + +static void virtballoon_remove (struct virtio_device *vdev) +{ +struct virtio_balloon *vb = vdev->priv; + +virtballoon_quiesce (vb); + +if (virtio_has_feature (vdev,VIRTIO_BALLOON_F_FREE_PAGE_HINT ))destroy_workqueue (vb->balloon_wq); - }remove_common (vb);kfree (vb);
PATCH v2 4/4 virtio_balloon: quiesce balloon work before device shutdown
In reply to Dickson WuFrom: "Denis V. Lunev" <[email protected]>
commit 7e17eef04600c399c7e0f5ce765da5cf9d40d8e1 upstream.
Commit 8bd2fa086a04 ("virtio: break and reset virtio devices on device_shutdown()") added a generic virtio bus .shutdown handler that breaks and resets every virtio device during device_shutdown(), i.e. on reboot and kexec.
virtio_balloon provides no .shutdown of its own, so that generic path runs while the balloon's asynchronous work is still armed. Once the device has been broken, virtqueue_add_inbuf() in virtballoon_free_page_report() returns -EIO and trips its WARN_ON_ONCE(). On a kernel booted with panic_on_warn that turns an ordinary reboot, for example a kexec based upgrade, into a fatal panic in the middle of device_shutdown(), so the machine never reaches the new kernel.
Relaxing that single WARN_ON_ONCE() would only hide the symptom: the inflate/deflate and OOM paths do not warn, they call wait_event(vb->acked, ...) and would instead block forever on a broken queue that can no longer complete. The device has to be quiesced, not just kept quiet.
Add a .shutdown handler that quiesces the balloon via the shared virtballoon_quiesce() helper while the device is still alive, and only then breaks and resets it via virtio_device_shutdown(). Unlike virtballoon_remove() the balloon workqueue is not destroyed, as shutdown does not free the device and cancel_work_sync() together with stop_update already prevent any further work from being queued.
Message-ID: <[email protected]>
--- drivers/virtio/virtio_balloon.c | 7 +++++++ 1 file changed, 7 insertions(+)
Changes in v2
v2: resent through a whitespace-safe path; v1 <CAFaZ9epp-Y8efod8a7CQuQqYEjk5L_ruwP+c1SoBRPjdu9uGyQ@mail.gmail.com> was damaged by my mail client and does not apply.
The backport and the test runs were prepared with AI coding tools (OpenAI Codex agents, with review by Claude). I went over the result before signing off.
drivers/virtio/virtio_balloon.c +7 -0
@@ -1159,6 +1159,12 @@ static void virtballoon_remove(struct virtio_device *vdev)kfree (vb); } +static void virtballoon_shutdown (struct virtio_device *vdev) +{ +virtballoon_quiesce (vdev->priv); +virtio_device_shutdown (vdev); +} +#ifdef CONFIG_PM_SLEEP static int virtballoon_freeze (struct virtio_device *vdev) {
@@ -1229,6 +1235,7 @@ static struct virtio_driver virtio_balloon_driver = { .validate = virtballoon_validate, .probe = virtballoon_probe, .remove = virtballoon_remove, + .shutdown = virtballoon_shutdown, .config_changed = virtballoon_changed, #ifdef CONFIG_PM_SLEEP .freeze = virtballoon_freeze,
[email protected]
AI reviewSashiko has reviewed this 4-patch series and found no issues.
Sashiko has reviewed this 4-patch series and found no issues.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]