Skip to contentSkip to conversation

virtio_balloon: fix Use-After-Free in page reporting during PM freeze

RFC Superseded Link Lin 5 participants 11 messages Jul 9 – Jul 17

Subsystems (4)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Link Lin

author
#0 lore raw

RFC 1/1 virtio_balloon: fix Use-After-Free in page reporting during PM freeze

During system power management freeze (e.g. ACPI S3 suspend or S4 hibernation), virtballoon_freeze() calls remove_common() to reset the virtio device and delete all virtqueues via vdev->config->del_vqs(). However, unlike virtballoon_remove(), virtballoon_freeze() fails to call page_reporting_unregister(&vb->pr_dev_info).

The comment in virtballoon_freeze() states:
/*
* The workqueue is already frozen by the PM core before this
* function is called.
*/

While this comment was accurate in 2011 for balloon-internal workqueues (such as balloon_wq, which was created with WQ_FREEZABLE and is paused by the PM freezer), it is invalid for Free Page Reporting.

Free Page Reporting (mm/page_reporting.c) schedules its delayed work (prdev->work) on the global system_wq. Because system_wq lacks the WQ_FREEZABLE flag, the PM freezer (freeze_workqueues_busy()) explicitly skips it. Consequently, page_reporting_process() on system_wq remains active and unfrozen throughout device suspend.

If memory is freed into the buddy allocator or a delayed work timer expires while the device is being frozen, page_reporting_process() fires on system_wq and calls virtballoon_free_page_report(). This function passes vb->reporting_vq into virtqueue_add_inbuf() / virtqueue_add_split(). Because the virtqueues were already destroyed by del_vqs(), this results in a Use-After-Free / General Protection Fault:

    [  250.709271] general protection fault, probably for non-canonical address 0x7f728084daf08d5e: 0000 [#1] SMP PTI
    [  250.732967] CPU: 2 PID: 38 Comm: kworker/2:1 Not tainted 5.10.0-44-cloud-amd64 #1 Debian 5.10.257-1
    [  250.751575] Workqueue: events page_reporting_process
    [  250.756665] RIP: 0010:virtqueue_add_split+0x233/0x4c0 [virtio_ring]
    ...
    [  250.867678] virtballoon_free_page_report+0x3a/0xe0 [virtio_balloon]
    [  250.883446] page_reporting_process+0x225/0x4f0

(Note: The OOM Notifier and Shrinker/Free Page Hinting features suffer from an identical lifecycle flaw and are also vulnerable to UAFs during S4 hibernation when memory pressure spikes. This patch focuses on Free Page Reporting, which runs periodically, to ensure clean backports to stable kernels).

Fix this by:
1. Unregistering page reporting in virtballoon_freeze() prior to calling remove_common(). This clears the RCU pr_dev_info pointer and flushes/ cancels prdev->work on system_wq via cancel_delayed_work_sync().
2. Re-registering page reporting in virtballoon_restore() after the virtqueues are re-initialized and virtio_device_ready() has been called.
3. Unwinding virtqueue initialization via remove_common() in virtballoon_restore() if page_reporting_register() fails.

Fixes 924a663f75e2 ("virtio-balloon: Reporting free page reservations")
Cc Ammar Faizi <[email protected]>
Cc Greg Thelen <[email protected]>
Cc Alexander Duyck <[email protected]>
---
 drivers/virtio/virtio_balloon.c | 11 +++++++++++
 1 file changed, 11 insertions(+)
drivers/virtio/virtio_balloon.c +3 -0
index a1b2c3d4e5f6..45a90fb3abf8 100640
@@ -1055,6 +1055,9 @@ static int virtballoon_freeze(struct virtio_device *vdev)
 	 * The workqueue is already frozen by the PM core before this
 	 * function is called.
 	 */
+	if (virtio_has_feature(vb->vdev, VIRTIO_BALLOON_F_REPORTING))
+		page_reporting_unregister(&vb->pr_dev_info);
+
 	remove_common(vb);
 	return 0;
 }

static int virtballoon_restore(struct virtio_device *vdev) {
struct virtio_balloon *vb = vdev->priv; int ret;

ret = init_vqs(vdev->priv);
if (ret)
return ret;

virtio_device_ready(vdev);

+ if (virtio_has_feature(vb->vdev, VIRTIO_BALLOON_F_REPORTING)) {
+ ret = page_reporting_register(&vb->pr_dev_info);
+ if (ret)
+ goto out_remove_vqs;
+ }
+
if (towards_target(vb))
virtballoon_changed(vdev);
update_balloon_size(vb);
return 0;
+
+out_remove_vqs:
+ remove_common(vb);
+ return ret;
}

Signature · 1 line
2.45.0
maintainer
#1 lore raw
Quoted 15 lines · On Thu, 9 Jul 2026 22:43:30 +0000 Link Lin <[email protected]> wrote:

During system power management freeze (e.g. ACPI S3 suspend or S4 hibernation), virtballoon_freeze() calls remove_common() to reset the virtio device and delete all virtqueues via vdev->config->del_vqs(). However, unlike virtballoon_remove(), virtballoon_freeze() fails to call page_reporting_unregister(&vb->pr_dev_info).

...

If memory is freed into the buddy allocator or a delayed work timer expires while the device is being frozen, page_reporting_process() fires on system_wq and calls virtballoon_free_page_report(). This function passes vb->reporting_vq into virtqueue_add_inbuf() / virtqueue_add_split(). Because the virtqueues were already destroyed by del_vqs(), this results in a Use-After-Free / General Protection Fault:

Quoted 7 lines
    [  250.709271] general protection fault, probably for non-canonical address 0x7f728084daf08d5e: 0000 [#1] SMP PTI
    [  250.732967] CPU: 2 PID: 38 Comm: kworker/2:1 Not tainted 5.10.0-44-cloud-amd64 #1 Debian 5.10.257-1
    [  250.751575] Workqueue: events page_reporting_process
    [  250.756665] RIP: 0010:virtqueue_add_split+0x233/0x4c0 [virtio_ring]
    ...
    [  250.867678] virtballoon_free_page_report+0x3a/0xe0 [virtio_balloon]
    [  250.883446] page_reporting_process+0x225/0x4f0

whoops

Quoted 8 lines

Fix this by:
1. Unregistering page reporting in virtballoon_freeze() prior to calling remove_common(). This clears the RCU pr_dev_info pointer and flushes/ cancels prdev->work on system_wq via cancel_delayed_work_sync().
2. Re-registering page reporting in virtballoon_restore() after the virtqueues are re-initialized and virtio_device_ready() has been called.
3. Unwinding virtqueue initialization via remove_common() in virtballoon_restore() if page_reporting_register() fails.

AI review thinks the patch didn't do the above:

	https://sashiko.dev/#/patchset/[email protected]

It also might have found a couple of pre-existing bugs in there.

author
#2 lore raw
Quoted 12 lines · Andrew Morton
Quoted 8 lines · On Thu, 9 Jul 2026 22:43:30 +0000 Link Lin <[email protected]> wrote:

Fix this by:
1. Unregistering page reporting in virtballoon_freeze() prior to calling remove_common(). This clears the RCU pr_dev_info pointer and flushes/ cancels prdev->work on system_wq via cancel_delayed_work_sync().
2. Re-registering page reporting in virtballoon_restore() after the virtqueues are re-initialized and virtio_device_ready() has been called.
3. Unwinding virtqueue initialization via remove_common() in virtballoon_restore() if page_reporting_register() fails.

Quoted 2 lines

AI review thinks the patch didn't do the above:

        https://sashiko.dev/#/patchset/[email protected]

The AI reviewer might not have parsed the entirety of the fix I proposed. The patch submitted definitely includes the changes to virtballoon_restore() for steps 2 and 3 (re-registering page reporting and unwinding init_vqs on failure). It seems the AI failed to parse the diff correctly. See:

+ if (virtio_has_feature(vb->vdev, VIRTIO_BALLOON_F_REPORTING)) {
+ ret = page_reporting_register(&vb->pr_dev_info);
+ if (ret)
+ goto out_remove_vqs;
+ }
+

        if (towards_target(vb))
                virtballoon_changed(vdev);
        update_balloon_size(vb);
        return 0;

+
+out_remove_vqs:
+ remove_common(vb);
+ return ret;
}

Quoted 1 line

It also might have found a couple of pre-existing bugs in there.

Indeed. Regarding the first pre-existing bug found by the AI (leaving the OOM notifier registered during suspend, leading to a UAF if memory pressure spikes during S4 hibernation):

I actually addressed this in the commit message:

"(Note: The OOM Notifier and Shrinker/Free Page Hinting features suffer from an identical lifecycle flaw and are also vulnerable to UAFs during S4 hibernation when memory pressure spikes. This patch focuses on Free Page Reporting, which runs periodically, to ensure clean backports to stable kernels)."

Regarding the second pre-existing bug the AI flagged (leaving uncancelled works on system_freezable_wq if virtballoon_restore fails on the cold path):
the AI is correct that this asynchronous work cancellation failure exists.

Since these are separate, pre-existing lifecycle bugs, would you prefer I roll fixes for the OOM notifier, shrinker/free page hinting, and work cancellations into a v2 of this patch, or submit them as a separate patch series to keep the stable backports clean?

Thanks,
Link

#3 lore raw
Quoted 47 lines · Link Lin
Quoted 11 lines
Quoted 8 lines

Fix this by:
1. Unregistering page reporting in virtballoon_freeze() prior to calling remove_common(). This clears the RCU pr_dev_info pointer and flushes/ cancels prdev->work on system_wq via cancel_delayed_work_sync().
2. Re-registering page reporting in virtballoon_restore() after the virtqueues are re-initialized and virtio_device_ready() has been called.
3. Unwinding virtqueue initialization via remove_common() in virtballoon_restore() if page_reporting_register() fails.

AI review thinks the patch didn't do the above:

        https://sashiko.dev/#/patchset/[email protected]

The AI reviewer might not have parsed the entirety of the fix I proposed. The patch submitted definitely includes the changes to virtballoon_restore() for steps 2 and 3 (re-registering page reporting and unwinding init_vqs on failure). It seems the AI failed to parse the diff correctly. See:

(quoted diff) +10 -0
+       if (virtio_has_feature(vb->vdev, VIRTIO_BALLOON_F_REPORTING)) {
+               ret = page_reporting_register(&vb->pr_dev_info);
+               if (ret)
+                       goto out_remove_vqs;
+       }
+
        if (towards_target(vb))
                virtballoon_changed(vdev);
        update_balloon_size(vb);
        return 0;
+
+out_remove_vqs:
+       remove_common(vb);
+       return ret;
 }
Quoted 2 lines

It also might have found a couple of pre-existing bugs in there.

Indeed. Regarding the first pre-existing bug found by the AI (leaving the OOM notifier registered during suspend, leading to a UAF if memory pressure spikes during S4 hibernation):

I actually addressed this in the commit message:

"(Note: The OOM Notifier and Shrinker/Free Page Hinting features suffer from an identical lifecycle flaw and are also vulnerable to UAFs during S4 hibernation when memory pressure spikes. This patch focuses on Free Page Reporting, which runs periodically, to ensure clean backports to stable kernels)."

Quoted 9 lines

Regarding the second pre-existing bug the AI flagged (leaving uncancelled works on system_freezable_wq if virtballoon_restore fails on the cold path):
the AI is correct that this asynchronous work cancellation failure exists.

Since these are separate, pre-existing lifecycle bugs, would you prefer I roll fixes for the OOM notifier, shrinker/free page hinting, and work cancellations into a v2 of this patch, or submit them as a separate patch series to keep the stable backports clean?

I think it would be best to have separate patches for each fix; Andrew, please correct me if you'd prefer one patch to address everything.

#4 lore raw
Quoted 53 lines · David Rientjes
Quoted 51 lines · Link Lin
Quoted 11 lines
Quoted 8 lines

Fix this by:
1. Unregistering page reporting in virtballoon_freeze() prior to calling remove_common(). This clears the RCU pr_dev_info pointer and flushes/ cancels prdev->work on system_wq via cancel_delayed_work_sync().
2. Re-registering page reporting in virtballoon_restore() after the virtqueues are re-initialized and virtio_device_ready() has been called.
3. Unwinding virtqueue initialization via remove_common() in virtballoon_restore() if page_reporting_register() fails.

AI review thinks the patch didn't do the above:

        https://sashiko.dev/#/patchset/[email protected]

The AI reviewer might not have parsed the entirety of the fix I proposed. The patch submitted definitely includes the changes to virtballoon_restore() for steps 2 and 3 (re-registering page reporting and unwinding init_vqs on failure). It seems the AI failed to parse the diff correctly. See:

(quoted diff) +10 -0
+       if (virtio_has_feature(vb->vdev, VIRTIO_BALLOON_F_REPORTING)) {
+               ret = page_reporting_register(&vb->pr_dev_info);
+               if (ret)
+                       goto out_remove_vqs;
+       }
+
        if (towards_target(vb))
                virtballoon_changed(vdev);
        update_balloon_size(vb);
        return 0;
+
+out_remove_vqs:
+       remove_common(vb);
+       return ret;
 }
Quoted 2 lines

It also might have found a couple of pre-existing bugs in there.

Indeed. Regarding the first pre-existing bug found by the AI (leaving the OOM notifier registered during suspend, leading to a UAF if memory pressure spikes during S4 hibernation):

I actually addressed this in the commit message:

"(Note: The OOM Notifier and Shrinker/Free Page Hinting features suffer from an identical lifecycle flaw and are also vulnerable to UAFs during S4 hibernation when memory pressure spikes. This patch focuses on Free Page Reporting, which runs periodically, to ensure clean backports to stable kernels)."

Regarding the second pre-existing bug the AI flagged (leaving uncancelled works on system_freezable_wq if virtballoon_restore fails on the cold path):
the AI is correct that this asynchronous work cancellation failure exists.

Quoted 8 lines
Quoted 5 lines

Since these are separate, pre-existing lifecycle bugs, would you prefer I roll fixes for the OOM notifier, shrinker/free page hinting, and work cancellations into a v2 of this patch, or submit them as a separate patch series to keep the stable backports clean?

I think it would be best to have separate patches for each fix; Andrew, please correct me if you'd prefer one patch to address everything.

It does not matter much but yes separate ones are a bit better if each can be applied independently.

#5 lore raw
In reply to Link Lin
Quoted 87 lines · Link Lin

During system power management freeze (e.g. ACPI S3 suspend or S4 hibernation), virtballoon_freeze() calls remove_common() to reset the virtio device and delete all virtqueues via vdev->config->del_vqs(). However, unlike virtballoon_remove(), virtballoon_freeze() fails to call page_reporting_unregister(&vb->pr_dev_info).

The comment in virtballoon_freeze() states:
/*
* The workqueue is already frozen by the PM core before this
* function is called.
*/

While this comment was accurate in 2011 for balloon-internal workqueues (such as balloon_wq, which was created with WQ_FREEZABLE and is paused by the PM freezer), it is invalid for Free Page Reporting.

Free Page Reporting (mm/page_reporting.c) schedules its delayed work (prdev->work) on the global system_wq. Because system_wq lacks the WQ_FREEZABLE flag, the PM freezer (freeze_workqueues_busy()) explicitly skips it. Consequently, page_reporting_process() on system_wq remains active and unfrozen throughout device suspend.

If memory is freed into the buddy allocator or a delayed work timer expires while the device is being frozen, page_reporting_process() fires on system_wq and calls virtballoon_free_page_report(). This function passes vb->reporting_vq into virtqueue_add_inbuf() / virtqueue_add_split(). Because the virtqueues were already destroyed by del_vqs(), this results in a Use-After-Free / General Protection Fault:

    [  250.709271] general protection fault, probably for non-canonical address 0x7f728084daf08d5e: 0000 [#1] SMP PTI
    [  250.732967] CPU: 2 PID: 38 Comm: kworker/2:1 Not tainted 5.10.0-44-cloud-amd64 #1 Debian 5.10.257-1
    [  250.751575] Workqueue: events page_reporting_process
    [  250.756665] RIP: 0010:virtqueue_add_split+0x233/0x4c0 [virtio_ring]
    ...
    [  250.867678] virtballoon_free_page_report+0x3a/0xe0 [virtio_balloon]
    [  250.883446] page_reporting_process+0x225/0x4f0

(Note: The OOM Notifier and Shrinker/Free Page Hinting features suffer from an identical lifecycle flaw and are also vulnerable to UAFs during S4 hibernation when memory pressure spikes. This patch focuses on Free Page Reporting, which runs periodically, to ensure clean backports to stable kernels).

Fix this by:
1. Unregistering page reporting in virtballoon_freeze() prior to calling remove_common(). This clears the RCU pr_dev_info pointer and flushes/ cancels prdev->work on system_wq via cancel_delayed_work_sync().
2. Re-registering page reporting in virtballoon_restore() after the virtqueues are re-initialized and virtio_device_ready() has been called.
3. Unwinding virtqueue initialization via remove_common() in virtballoon_restore() if page_reporting_register() fails.

Fixes 924a663f75e2 ("virtio-balloon: Reporting free page reservations")
Cc Ammar Faizi <[email protected]>
Cc Greg Thelen <[email protected]>
Cc Alexander Duyck <[email protected]>
---
 drivers/virtio/virtio_balloon.c | 11 +++++++++++
 1 file changed, 11 insertions(+)
drivers/virtio/virtio_balloon.c +3 -0
index a1b2c3d4e5f6..45a90fb3abf8 100640
@@ -1055,6 +1055,9 @@ static int virtballoon_freeze(struct virtio_device *vdev)
 	 * The workqueue is already frozen by the PM core before this
 	 * function is called.
 	 */
+	if (virtio_has_feature(vb->vdev, VIRTIO_BALLOON_F_REPORTING))
+		page_reporting_unregister(&vb->pr_dev_info);
+
 	remove_common(vb);
 	return 0;
 }

static int virtballoon_restore(struct virtio_device *vdev) {

drivers/virtio/virtio_balloon.c +0 -0
 	struct virtio_balloon *vb = vdev->priv;
 	int ret;
 
 	ret = init_vqs(vdev->priv);

drivers/virtio/virtio_balloon.c

Quoted 10 lines
drivers/virtio/virtio_balloon.c +5 -0
 	if (ret)
 		return ret;
 
 	virtio_device_ready(vdev);
 
+	if (virtio_has_feature(vb->vdev, VIRTIO_BALLOON_F_REPORTING)) {
+		ret = page_reporting_register(&vb->pr_dev_info);
+		if (ret)
+			goto out_remove_vqs;
+	}

Hm, that failure handling is rather nasty.

In virtballoon_freeze() we document:

"The workqueue is already frozen by the PM core before this function is called"

Your report states:

"Workqueue: events page_reporting_process"

I assume that workqueue is not frozen yet because ... it's not freezable :)

So could we queue to system_freezable_wq instead, or define our own freezable workqueue there? Then a driver doesn't have to worry about that.

Signature · 3 lines
Cheers,

David
#6 lore raw
Quoted 108 lines · David Hildenbrand (Arm)
Quoted 87 lines · Link Lin

During system power management freeze (e.g. ACPI S3 suspend or S4 hibernation), virtballoon_freeze() calls remove_common() to reset the virtio device and delete all virtqueues via vdev->config->del_vqs(). However, unlike virtballoon_remove(), virtballoon_freeze() fails to call page_reporting_unregister(&vb->pr_dev_info).

The comment in virtballoon_freeze() states:
/*
* The workqueue is already frozen by the PM core before this
* function is called.
*/

While this comment was accurate in 2011 for balloon-internal workqueues (such as balloon_wq, which was created with WQ_FREEZABLE and is paused by the PM freezer), it is invalid for Free Page Reporting.

Free Page Reporting (mm/page_reporting.c) schedules its delayed work (prdev->work) on the global system_wq. Because system_wq lacks the WQ_FREEZABLE flag, the PM freezer (freeze_workqueues_busy()) explicitly skips it. Consequently, page_reporting_process() on system_wq remains active and unfrozen throughout device suspend.

If memory is freed into the buddy allocator or a delayed work timer expires while the device is being frozen, page_reporting_process() fires on system_wq and calls virtballoon_free_page_report(). This function passes vb->reporting_vq into virtqueue_add_inbuf() / virtqueue_add_split(). Because the virtqueues were already destroyed by del_vqs(), this results in a Use-After-Free / General Protection Fault:

    [  250.709271] general protection fault, probably for non-canonical address 0x7f728084daf08d5e: 0000 [#1] SMP PTI
    [  250.732967] CPU: 2 PID: 38 Comm: kworker/2:1 Not tainted 5.10.0-44-cloud-amd64 #1 Debian 5.10.257-1
    [  250.751575] Workqueue: events page_reporting_process
    [  250.756665] RIP: 0010:virtqueue_add_split+0x233/0x4c0 [virtio_ring]
    ...
    [  250.867678] virtballoon_free_page_report+0x3a/0xe0 [virtio_balloon]
    [  250.883446] page_reporting_process+0x225/0x4f0

(Note: The OOM Notifier and Shrinker/Free Page Hinting features suffer from an identical lifecycle flaw and are also vulnerable to UAFs during S4 hibernation when memory pressure spikes. This patch focuses on Free Page Reporting, which runs periodically, to ensure clean backports to stable kernels).

Fix this by:
1. Unregistering page reporting in virtballoon_freeze() prior to calling remove_common(). This clears the RCU pr_dev_info pointer and flushes/ cancels prdev->work on system_wq via cancel_delayed_work_sync().
2. Re-registering page reporting in virtballoon_restore() after the virtqueues are re-initialized and virtio_device_ready() has been called.
3. Unwinding virtqueue initialization via remove_common() in virtballoon_restore() if page_reporting_register() fails.

Fixes 924a663f75e2 ("virtio-balloon: Reporting free page reservations")
Cc Ammar Faizi <[email protected]>
Cc Greg Thelen <[email protected]>
Cc Alexander Duyck <[email protected]>
---
 drivers/virtio/virtio_balloon.c | 11 +++++++++++
 1 file changed, 11 insertions(+)
drivers/virtio/virtio_balloon.c +3 -0
index a1b2c3d4e5f6..45a90fb3abf8 100640
@@ -1055,6 +1055,9 @@ static int virtballoon_freeze(struct virtio_device *vdev)
 	 * The workqueue is already frozen by the PM core before this
 	 * function is called.
 	 */
+	if (virtio_has_feature(vb->vdev, VIRTIO_BALLOON_F_REPORTING))
+		page_reporting_unregister(&vb->pr_dev_info);
+
 	remove_common(vb);
 	return 0;
 }

static int virtballoon_restore(struct virtio_device *vdev) {

drivers/virtio/virtio_balloon.c +0 -0
 	struct virtio_balloon *vb = vdev->priv;
 	int ret;
 
 	ret = init_vqs(vdev->priv);

drivers/virtio/virtio_balloon.c

Quoted 10 lines
drivers/virtio/virtio_balloon.c +5 -0
 	if (ret)
 		return ret;
 
 	virtio_device_ready(vdev);
 
+	if (virtio_has_feature(vb->vdev, VIRTIO_BALLOON_F_REPORTING)) {
+		ret = page_reporting_register(&vb->pr_dev_info);
+		if (ret)
+			goto out_remove_vqs;
+	}

Hm, that failure handling is rather nasty.

In virtballoon_freeze() we document:

"The workqueue is already frozen by the PM core before this function is called"

Your report states:

Quoted 12 lines

"Workqueue: events page_reporting_process"

I assume that workqueue is not frozen yet because ... it's not freezable :)

So could we queue to system_freezable_wq instead, or define our own freezable workqueue there? Then a driver doesn't have to worry about that.

Signature · 3 lines
Cheers,

David

+1. Just system_freezable_wq will do the trick.

Signature · 1 line
MST
author
#7 lore raw
Quoted 3 lines · David Hildenbrand

I assume that workqueue is not frozen yet because ... it's not freezable :) So could we queue to system_freezable_wq instead, or define our own freezable workqueue there? Then a driver doesn't have to worry about that.

Exactly. As noted in the RFC, the root cause is indeed that system_wq lacks the WQ_FREEZABLE flag, leaving it active during suspend.

Switching the worker over to system_freezable_wq is a brilliant idea. It's a much cleaner abstraction that solves the problem at the core, saving individual drivers from having to micromanage this lifecycle and handle failure unwinding during freeze/restore.

Quoted 1 line · Michael S. Tsirkin

+1. Just system_freezable_wq will do the trick.

Thanks, David and Michael. I will drop the virtio-balloon specific unregister logic and adopt this approach.

Per the earlier feedback to keep these fixes independent, I'll format v2 as a 3-part patch series:
[PATCH v2 1/3] mm/page_reporting: use system_freezable_wq [PATCH v2 2/3] virtio_balloon: fix shrinker UAF during PM freeze [PATCH v2 3/3] virtio_balloon: fix OOM notifier UAF during PM freeze

I'll send that out shortly.

Thanks,
Link

#9 lore raw
In reply to Michael S. Tsirkin
Quoted 49 lines · Michael S. Tsirkin
Quoted 49 lines
Quoted 21 lines
drivers/virtio/virtio_balloon.c +3 -0
index a1b2c3d4e5f6..45a90fb3abf8 100640
@@ -1055,6 +1055,9 @@ static int virtballoon_freeze(struct virtio_device *vdev)
 	 * The workqueue is already frozen by the PM core before this
 	 * function is called.
 	 */
+	if (virtio_has_feature(vb->vdev, VIRTIO_BALLOON_F_REPORTING))
+		page_reporting_unregister(&vb->pr_dev_info);
+
 	remove_common(vb);
 	return 0;
 }

static int virtballoon_restore(struct virtio_device *vdev) {

drivers/virtio/virtio_balloon.c +0 -0
 	struct virtio_balloon *vb = vdev->priv;
 	int ret;
 
 	ret = init_vqs(vdev->priv);

drivers/virtio/virtio_balloon.c

Quoted 10 lines
drivers/virtio/virtio_balloon.c +5 -0
 	if (ret)
 		return ret;
 
 	virtio_device_ready(vdev);
 
+	if (virtio_has_feature(vb->vdev, VIRTIO_BALLOON_F_REPORTING)) {
+		ret = page_reporting_register(&vb->pr_dev_info);
+		if (ret)
+			goto out_remove_vqs;
+	}

Hm, that failure handling is rather nasty.

In virtballoon_freeze() we document:

"The workqueue is already frozen by the PM core before this function is called"

Your report states:

"Workqueue: events page_reporting_process"

I assume that workqueue is not frozen yet because ... it's not freezable :)

So could we queue to system_freezable_wq instead, or define our own freezable workqueue there? Then a driver doesn't have to worry about that.

Quoted 7 lines
Quoted 4 lines
Signature · 3 lines
Cheers,

David

+1. Just system_freezable_wq will do the trick.

This makes sense.

I'm curious why this bug hasn't popped up earlier, presumably any VM that has gone through suspend while reporting free pages through FPR is vulnerable to it and could have panicked as a result. Which would suggest maybe >99% of FPR is done by guests that never suspend? While under pressure this issue seems reproducible.

#10 lore raw
Quoted 59 lines · David Rientjes
Quoted 49 lines · Michael S. Tsirkin
Quoted 49 lines
Quoted 21 lines
drivers/virtio/virtio_balloon.c +3 -0
index a1b2c3d4e5f6..45a90fb3abf8 100640
@@ -1055,6 +1055,9 @@ static int virtballoon_freeze(struct virtio_device *vdev)
 	 * The workqueue is already frozen by the PM core before this
 	 * function is called.
 	 */
+	if (virtio_has_feature(vb->vdev, VIRTIO_BALLOON_F_REPORTING))
+		page_reporting_unregister(&vb->pr_dev_info);
+
 	remove_common(vb);
 	return 0;
 }

static int virtballoon_restore(struct virtio_device *vdev) {

drivers/virtio/virtio_balloon.c +0 -0
 	struct virtio_balloon *vb = vdev->priv;
 	int ret;
 
 	ret = init_vqs(vdev->priv);

drivers/virtio/virtio_balloon.c

Quoted 10 lines
drivers/virtio/virtio_balloon.c +5 -0
 	if (ret)
 		return ret;
 
 	virtio_device_ready(vdev);
 
+	if (virtio_has_feature(vb->vdev, VIRTIO_BALLOON_F_REPORTING)) {
+		ret = page_reporting_register(&vb->pr_dev_info);
+		if (ret)
+			goto out_remove_vqs;
+	}

Hm, that failure handling is rather nasty.

In virtballoon_freeze() we document:

"The workqueue is already frozen by the PM core before this function is called"

Your report states:

"Workqueue: events page_reporting_process"

I assume that workqueue is not frozen yet because ... it's not freezable :)

So could we queue to system_freezable_wq instead, or define our own freezable workqueue there? Then a driver doesn't have to worry about that.

Quoted 7 lines
Quoted 4 lines
Signature · 3 lines
Cheers,

David

+1. Just system_freezable_wq will do the trick.

Quoted 6 lines

This makes sense.

I'm curious why this bug hasn't popped up earlier, presumably any VM that has gone through suspend while reporting free pages through FPR is vulnerable to it and could have panicked as a result. Which would suggest maybe >99% of FPR is done by guests that never suspend?

Quite possible.

Quoted 2 lines

While under
pressure this issue seems reproducible.

author
#8 lore raw
In reply to Link Lin
Quoted 8 lines · Link Lin

Per the earlier feedback to keep these fixes independent, I'll format v2 as a 3-part patch series:
[PATCH v2 1/3] mm/page_reporting: use system_freezable_wq [PATCH v2 2/3] virtio_balloon: fix shrinker UAF during PM freeze [PATCH v2 3/3] virtio_balloon: fix OOM notifier UAF during PM freeze

I'll send that out shortly.

Hi everyone,

I have just sent out the v2 patch series in a new thread:
https://lore.kernel.org/all/[email protected]/

Just a quick update: I previously mentioned I would include a fix for the OOM notifier as part 3. However, upon further audit, the PM core explicitly disables the OOM killer (via oom_killer_disable()) during freeze_processes() before device drivers are frozen.

Therefore, the virtio_balloon_oom_notify callback cannot trigger while the virtqueues are deleted. Since it is already safe, I dropped it from the series.

v2 now consists solely of the page reporting workqueue migration and the shrinker TOCTOU fix. Thanks again for the reviews!

Best,
Link