* [PATCH v2 1/2] mm/page_reporting: use system_freezable_wq to fix UAF during suspend
2026-07-17 0:22 [PATCH v2 0/2] virtio_balloon: fix Use-After-Free bugs during PM freeze Link Lin
@ 2026-07-17 0:22 ` Link Lin
2026-07-17 0:22 ` [PATCH v2 2/2] virtio_balloon: avoid shrinker execution during PM suspend Link Lin
1 sibling, 0 replies; 4+ messages in thread
From: Link Lin @ 2026-07-17 0:22 UTC (permalink / raw)
To: Andrew Morton, Vlastimil Babka, Michael S . Tsirkin,
David Hildenbrand
Cc: virtualization, linux-mm, linux-kernel, prasin, rientjes, duenwen,
jasowang, xuanzhuo, Ammar Faizi, jiaqiyan, ahwilkins, Greg Thelen,
Alexander Duyck, jthoughton, stable, Link Lin, David Hildenbrand
During PM freeze (e.g. S3 suspend or S4 hibernation), device drivers like
virtio_balloon reset their underlying virtio devices and delete their
virtqueues via vdev->config->del_vqs().
However, page reporting work (page_reporting_process) was scheduled on
the global system_wq. Because system_wq lacks the WQ_FREEZABLE flag, the
PM freezer skips it, leaving page_reporting_process active during
suspend. If pages are freed into the buddy allocator while suspending,
page reporting invokes virtballoon_free_page_report() on deleted
virtqueues:
[ 196.795226] general protection fault, probably for non-canonical address 0xaa1436fe70dae6df: 0000 [#1] SMP NOPTI
[ 196.825967] Workqueue: events page_reporting_process
[ 196.831038] RIP: 0010:virtqueue_add_split+0x233/0x4c0 [virtio_ring]
[ 196.927073] virtballoon_free_page_report+0x3a/0xe0 [virtio_balloon]
[ 196.946943] page_reporting_process+0x370/0x4f0
Fix this by switching page reporting work to system_freezable_wq. This
ensures that the PM freezer pauses page_reporting_process before device
drivers destroy their reporting virtqueues.
This aligns with the driver's existing design. The comment in
virtballoon_freeze() states:
/*
* The workqueue is already frozen by the PM core before this
* function is called.
*/
Suggested-by: David Hildenbrand <david@redhat.com>
Suggested-by: Michael S. Tsirkin <mst@redhat.com>
Acked-by: David Rientjes <rientjes@google.com>
Fixes: 924a663f75e2 ("virtio-balloon: Reporting free page reservations")
Cc: stable@vger.kernel.org
Signed-off-by: Link Lin <linkl@google.com>
---
mm/page_reporting.c | 6 ++++--
1 file changed, 4 insertions(+), 2 deletions(-)
diff --git a/mm/page_reporting.c b/mm/page_reporting.c
index 7418f2e500..4dc6f4b852 100644
--- a/mm/page_reporting.c
+++ b/mm/page_reporting.c
@@ -80,7 +80,8 @@ __page_reporting_request(struct page_reporting_dev_info *prdev)
* now we are limiting this to running no more than once every
* couple of seconds.
*/
- schedule_delayed_work(&prdev->work, PAGE_REPORTING_DELAY);
+ queue_delayed_work(system_freezable_wq, &prdev->work,
+ PAGE_REPORTING_DELAY);
}
/* notify prdev of free page reporting request */
@@ -343,7 +344,8 @@ static void page_reporting_process(struct work_struct *work)
*/
state = atomic_cmpxchg(&prdev->state, state, PAGE_REPORTING_IDLE);
if (state == PAGE_REPORTING_REQUESTED)
- schedule_delayed_work(&prdev->work, PAGE_REPORTING_DELAY);
+ queue_delayed_work(system_freezable_wq, &prdev->work,
+ PAGE_REPORTING_DELAY);
}
static DEFINE_MUTEX(page_reporting_mutex);
--
2.55.0.229.g6434b31f56-goog
^ permalink raw reply related [flat|nested] 4+ messages in thread* [PATCH v2 2/2] virtio_balloon: avoid shrinker execution during PM suspend
2026-07-17 0:22 [PATCH v2 0/2] virtio_balloon: fix Use-After-Free bugs during PM freeze Link Lin
2026-07-17 0:22 ` [PATCH v2 1/2] mm/page_reporting: use system_freezable_wq to fix UAF during suspend Link Lin
@ 2026-07-17 0:22 ` Link Lin
2026-07-17 3:08 ` Link Lin
1 sibling, 1 reply; 4+ messages in thread
From: Link Lin @ 2026-07-17 0:22 UTC (permalink / raw)
To: Andrew Morton, Vlastimil Babka, Michael S . Tsirkin,
David Hildenbrand
Cc: virtualization, linux-mm, linux-kernel, prasin, rientjes, duenwen,
jasowang, xuanzhuo, Ammar Faizi, jiaqiyan, ahwilkins, Greg Thelen,
Alexander Duyck, jthoughton, stable, Link Lin
During PM freeze (e.g. S4 hibernation), virtballoon_freeze() calls
remove_common() which resets the virtio device and deletes all virtqueues.
However, the balloon shrinker remains registered with core MM.
If memory pressure occurs during S4 hibernation image creation/saving, MM
invokes virtio_balloon_shrinker_scan(), which attempts to reclaim free
pages. Although return_free_pages_to_mm() only frees pages back to MM,
reclaiming free pages under memory pressure can trigger page reporting
which might access the deleted reporting virtqueue if it is not yet
frozen, or interact with other parts of the driver in a teardown state.
Avoid this by adding a `suspended` flag to `struct virtio_balloon`. Set
this flag to true in virtballoon_freeze() and false in
virtballoon_restore(). Check this flag in both shrinker callbacks (scan
and count) and return 0 if the device is suspended, preventing any
shrinker execution while virtqueues are deleted. Wrap the lockless reads
in READ_ONCE() and writes in WRITE_ONCE() to prevent compiler
optimization issues and KCSAN data race warnings.
Fixes: 71019de8219b ("virtio_balloon: Add free page hinting support")
Cc: stable@vger.kernel.org
Acked-by: David Rientjes <rientjes@google.com>
Signed-off-by: Link Lin <linkl@google.com>
---
drivers/virtio/virtio_balloon.c | 49 +++++++++++++++++++++++++++------
1 file changed, 41 insertions(+), 8 deletions(-)
diff --git a/drivers/virtio/virtio_balloon.c b/drivers/virtio/virtio_balloon.c
index 088b3a0e6c..38e1166a64 100644
--- a/drivers/virtio/virtio_balloon.c
+++ b/drivers/virtio/virtio_balloon.c
@@ -68,6 +68,8 @@ struct virtio_balloon {
/* Prevent updating balloon when it is being canceled. */
spinlock_t stop_update_lock;
bool stop_update;
+ /* Prevent shrinker from running while device is suspended. */
+ bool suspended;
/* Bitmap to indicate if reading the related config fields are needed */
unsigned long config_read_bitmap;
@@ -471,9 +473,9 @@ static inline s64 towards_target(struct virtio_balloon *vb)
return target - vb->num_pages;
}
-/* Gives back @num_to_return blocks of free pages to mm. */
-static unsigned long return_free_pages_to_mm(struct virtio_balloon *vb,
- unsigned long num_to_return)
+/* Helper: must be called with free_page_list_lock held */
+static unsigned long __return_free_pages_to_mm(struct virtio_balloon *vb,
+ unsigned long num_to_return)
{
unsigned long num_returned = 0;
struct page *page, *next;
@@ -481,8 +483,6 @@ static unsigned long return_free_pages_to_mm(struct virtio_balloon *vb,
if (unlikely(!num_to_return))
return 0;
- spin_lock_irq(&vb->free_page_list_lock);
-
list_for_each_entry_safe(page, next, &vb->free_page_list, lru) {
list_del(&page->lru);
__free_pages(page, VIRTIO_BALLOON_HINT_BLOCK_ORDER);
@@ -490,11 +490,27 @@ static unsigned long return_free_pages_to_mm(struct virtio_balloon *vb,
break;
}
vb->num_free_page_blocks -= num_returned;
- spin_unlock_irq(&vb->free_page_list_lock);
return num_returned;
}
+/* Gives back @num_to_return blocks of free pages to mm. */
+static unsigned long return_free_pages_to_mm(struct virtio_balloon *vb,
+ unsigned long num_to_return)
+{
+ unsigned long ret;
+
+ spin_lock_irq(&vb->free_page_list_lock);
+ if (vb->suspended) {
+ spin_unlock_irq(&vb->free_page_list_lock);
+ return 0;
+ }
+ ret = __return_free_pages_to_mm(vb, num_to_return);
+ spin_unlock_irq(&vb->free_page_list_lock);
+
+ return ret;
+}
+
static void virtio_balloon_queue_free_page_work(struct virtio_balloon *vb)
{
if (!virtio_has_feature(vb->vdev, VIRTIO_BALLOON_F_FREE_PAGE_HINT))
@@ -871,6 +887,9 @@ static unsigned long virtio_balloon_shrinker_scan(struct shrinker *shrinker,
{
struct virtio_balloon *vb = shrinker->private_data;
+ if (READ_ONCE(vb->suspended))
+ return 0;
+
return shrink_free_pages(vb, sc->nr_to_scan);
}
@@ -879,6 +898,9 @@ static unsigned long virtio_balloon_shrinker_count(struct shrinker *shrinker,
{
struct virtio_balloon *vb = shrinker->private_data;
+ if (READ_ONCE(vb->suspended))
+ return 0;
+
return vb->num_free_page_blocks * VIRTIO_BALLOON_HINT_BLOCK_PAGES;
}
@@ -1089,8 +1111,11 @@ static void remove_common(struct virtio_balloon *vb)
update_balloon_size(vb);
/* There might be free pages that are being reported: release them. */
- if (virtio_has_feature(vb->vdev, VIRTIO_BALLOON_F_FREE_PAGE_HINT))
- return_free_pages_to_mm(vb, ULONG_MAX);
+ if (virtio_has_feature(vb->vdev, VIRTIO_BALLOON_F_FREE_PAGE_HINT)) {
+ spin_lock_irq(&vb->free_page_list_lock);
+ __return_free_pages_to_mm(vb, ULONG_MAX);
+ spin_unlock_irq(&vb->free_page_list_lock);
+ }
/* Now we reset the device so we can clean up the queues. */
virtio_reset_device(vb->vdev);
@@ -1133,6 +1158,10 @@ static int virtballoon_freeze(struct virtio_device *vdev)
* The workqueue is already frozen by the PM core before this
* function is called.
*/
+ spin_lock_irq(&vb->free_page_list_lock);
+ WRITE_ONCE(vb->suspended, true);
+ spin_unlock_irq(&vb->free_page_list_lock);
+
remove_common(vb);
return 0;
}
@@ -1148,6 +1177,10 @@ static int virtballoon_restore(struct virtio_device *vdev)
virtio_device_ready(vdev);
+ spin_lock_irq(&vb->free_page_list_lock);
+ WRITE_ONCE(vb->suspended, false);
+ spin_unlock_irq(&vb->free_page_list_lock);
+
if (towards_target(vb))
virtballoon_changed(vdev);
update_balloon_size(vb);
--
2.55.0.229.g6434b31f56-goog
^ permalink raw reply related [flat|nested] 4+ messages in thread