Skip to contentSkip to conversation

virtio_balloon: fix Use-After-Free bugs during PM freeze

PATCH Superseded Link Lin 3 participants 9 messages Jul 17 – Jul 20

Subsystems (5)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Link Lin

author
#0 lore raw

v2 cover virtio_balloon: fix Use-After-Free bugs during PM freeze

This patch series addresses a class of Use-After-Free vulnerabilities in the virtio_balloon driver that can occur during system power management freeze (e.g., suspend or hibernation).

The core issue is a lifecycle mismatch: when the system enters freeze, virtballoon_freeze() resets the virtio device and deletes its virtqueues. However, several asynchronous or event-driven components of the driver and related subsystems (Free Page Reporting and Shrinker) remain registered and active. If they trigger during the freeze/suspend process, they attempt to access the now-deleted virtqueues, causing kernel crashes (General Protection Faults or UAF).

To fix this:
1. Migrate Free Page Reporting to use the system_freezable_wq, ensuring its worker is frozen before driver freeze callbacks run. (Suggested by David Hildenbrand).
2. Avoid shrinker execution while the device is suspended by adding a suspended flag to struct virtio_balloon. Wrap lockless reads of this flag in READ_ONCE() and writes in WRITE_ONCE() to prevent data races and KCSAN warnings.

Note: The OOM notifier (virtio_balloon_oom_notify) is also registered, but since the PM core disables the OOM killer (via oom_killer_disable()) during freeze_processes() before device drivers are frozen, it cannot trigger while the device is frozen. Thus, it does not require a fix.

Testing:
I have verified these fixes using Google’s virtualization infrastructure by running continuous suspend/resume iterations (40+ cycles) while churning memory using stress-ng (stress-ng --vm 4 --vm-bytes 60% --timeout 1) to constantly create free pages for the buddy allocator. We also set the page_reporting_order parameter to 0 to make the page reporting worker highly sensitive, forcing it to pick up any 4K free pages. This confirmed that the UAF crashes are no longer reproducible.

RFC: https://lore.kernel.org/r/[email protected]
---
RFC -> v2:
- Switched page reporting fix from unregister/re-register in driver to using system_freezable_wq in mm/page_reporting.c, which avoids complex restore rollback logic for that component.
- Switched shrinker fix from unregister/re-register to using a simple suspended boolean flag to avoid complex rollback logic. Wrap its reads and writes with READ_ONCE()/WRITE_ONCE() to prevent KCSAN data race warnings.
- Dropped OOM notifier fix since it's already protected by the PM core's oom_killer_disable().

Link Lin (2):
mm/page_reporting: use system_freezable_wq to fix UAF during suspend
virtio_balloon: avoid shrinker execution during PM suspend

 drivers/virtio/virtio_balloon.c | 49 +++++++++++++++++++++++++++------
 mm/page_reporting.c             |  6 ++--
 2 files changed, 45 insertions(+), 10 deletions(-)
Signature · 1 line
2.55.0.229.g6434b31f56-goog
author
#1 lore raw

PATCH v2 1/2 mm/page_reporting: use system_freezable_wq to fix UAF during suspend

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.
*/

Fixes 924a663f75e2 ("virtio-balloon: Reporting free page reservations")
---
 mm/page_reporting.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
mm/page_reporting.c +4 -2
index 7418f2e500..4dc6f4b852 100644
@@ -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);
Signature · 1 line
2.55.0.229.g6434b31f56-goog
author
#4 lore raw

PATCH v2 2/2 virtio_balloon: avoid shrinker execution during PM suspend

In reply to 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")
---
 drivers/virtio/virtio_balloon.c | 49 +++++++++++++++++++++++++++------
 1 file changed, 41 insertions(+), 8 deletions(-)
drivers/virtio/virtio_balloon.c +41 -8
index 088b3a0e6c..38e1166a64 100644
@@ -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);
Signature · 1 line
2.55.0.229.g6434b31f56-goog
author
#5 lore raw

Re: [PATCH v2 2/2] virtio_balloon: avoid shrinker execution during PM suspend

Quoted 20 lines · 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.

Quoted 4 lines
Fixes 71019de8219b ("virtio_balloon: Add free page hinting support")

Apologies, I missed adding a tag for the feedback that led to the TOCTOU locking fix in this patch.

Maintainers, if this series is applied, could you please add the following?

(I will be sure to include it if a v3 is otherwise required).

maintainer Acked-by on v2 1/2
#2 lore raw

Re: [PATCH v2 1/2] mm/page_reporting: use system_freezable_wq to fix UAF during suspend

In reply to Link Lin
Quoted 28 lines · Link Lin

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.
*/

Quoted 6 lines
Fixes 924a663f75e2 ("virtio-balloon: Reporting free page reservations")
Signature · 3 lines
Cheers,

David
#6 lore raw

Re: [PATCH v2 2/2] virtio_balloon: avoid shrinker execution during PM suspend

In reply to Link Lin
Quoted 10 lines · 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.

i feel this is the thing to fix then? not the freeing itself.

Quoted 8 lines

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.

so you prevented the warnings but did you fix the races? CB

Quoted 74 lines
Fixes 71019de8219b ("virtio_balloon: Add free page hinting support")
---
 drivers/virtio/virtio_balloon.c | 49 +++++++++++++++++++++++++++------
 1 file changed, 41 insertions(+), 8 deletions(-)
drivers/virtio/virtio_balloon.c +22 -6
index 088b3a0e6c..38e1166a64 100644
@@ -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))

drivers/virtio/virtio_balloon.c static unsigned long virtio_balloon_shrinker_scan(struct shrinker *shrinker, · line 892

Quoted 6 lines
drivers/virtio/virtio_balloon.c +3 -0
@@ -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;
+

what if vb->suspended is set right here?

drivers/virtio/virtio_balloon.c static unsigned long virtio_balloon_shrinker_count(struct shrinker *shrinker, · line 903

Quoted 10 lines
drivers/virtio/virtio_balloon.c +3 -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;
+

or here?

drivers/virtio/virtio_balloon.c static void remove_common(struct virtio_balloon *vb) · line 1117

Quoted 13 lines
drivers/virtio/virtio_balloon.c +4 -2
 	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);

this is remove, taking locks seems futile - if something will acquire the lock here, we will be in trouble.

Quoted 29 lines
drivers/virtio/virtio_balloon.c +1 -0
+	}
 
 
 	/* Now we reset the device so we can clean up the queues. */
drivers/virtio/virtio_balloon.c +8 -0
 	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);
Signature · 1 line
2.55.0.229.g6434b31f56-goog
maintainer Acked-by on v2 1/2
#3 lore raw

Re: [PATCH v2 1/2] mm/page_reporting: use system_freezable_wq to fix UAF during suspend

In reply to Link Lin
Quoted 28 lines · Link Lin

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.
*/

Quoted 6 lines
Fixes 924a663f75e2 ("virtio-balloon: Reporting free page reservations")
Quoted 30 lines
---
 mm/page_reporting.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
mm/page_reporting.c +4 -2
index 7418f2e500..4dc6f4b852 100644
@@ -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);
Signature · 1 line
2.55.0.229.g6434b31f56-goog
#7 lore raw

Re: [PATCH v2 2/2] virtio_balloon: avoid shrinker execution during PM suspend

In reply to Link Lin
Quoted 10 lines · 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.

Yes, this does seem possible.

Quoted 25 lines
drivers/virtio/virtio_balloon.c +7 -0
+
 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;
 }
 

drivers/virtio/virtio_balloon.c static void remove_common(struct virtio_balloon *vb) · line 1118

Quoted 10 lines
drivers/virtio/virtio_balloon.c +5 -2
@@ -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);
+	}

Why not set the boolean immediately after this, and call it "shrinker_disabled" instead of "suspended"?

Then you can keep calling "return_free_pages_to_mm(vb, ULONG_MAX);" here.

Signature · 3 lines
Cheers,

David
author
#8 lore raw

Re: [PATCH v2 2/2] virtio_balloon: avoid shrinker execution during PM suspend

Quoted 10 lines · On Fri, 17 Jul 2026 02:17:24 -0400 "Michael S. Tsirkin" <[email protected]> wrote:
Quoted 3 lines

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.

i feel this is the thing to fix then? not the freeing itself.

...

this is remove, taking locks seems futile - if something will acquire the lock here, we will be in trouble.

Quoted 4 lines · On Fri, 17 Jul 2026 11:30:44 +0200 David Hildenbrand <[email protected]> wrote:

Why not set the boolean immediately after this, and call it "shrinker_disabled" instead of "suspended"?

Then you can keep calling "return_free_pages_to_mm(vb, ULONG_MAX);" here.

Hi Michael, David,

Michael, your intuition here is exactly right: the freeing itself is perfectly safe, and taking locks in the teardown path is an anti-pattern we should absolutely avoid.

After re-tracing the execution paths based on your feedback, I realized that Patch 2/2 is completely unnecessary.

The FPH shrinker doesn't touch virtqueues itself. It simply returns pages to the buddy allocator. While this can trigger Free Page Reporting, Patch 1/2 migrates the reporting worker to system_freezable_wq, meaning the PM core natively guarantees that worker is frozen before virtballoon_freeze() ever runs.

Furthermore, any asynchronous FPH hinting work happens on vb->balloon_wq (triggered via config changes). Because vb->balloon_wq is natively allocated with WQ_FREEZABLE, the PM core successfully freezes this worker prior to virtballoon_freeze() as well.

Because all downstream paths are safely frozen by the PM core, a concurrent shrinker call during suspend is entirely harmless. It will merely return 0 (since remove_common() safely drains the free_page_list anyway using ULONG_MAX) or safely execute without interacting with a teardown state.

David, thank you for the cleaner shrinker_disabled suggestion, but since the asynchronous workers are already natively frozen, we can avoid touching the teardown paths and locking logic altogether.

I am going to drop this patch entirely from the series. I will send out a single-patch v3 consisting solely of the page reporting fix (Patch 1/2) shortly. Thank you both for the rigorous review that steered us away from this unnecessary complexity!

Best,
Link