Implement VIRTIO_BALLOON_F_DEVICE_INIT_REPORTED (per virtio spec
proposal): when negotiated, the device initializes reported pages
(zeros, or poison_val if PAGE_POISON).

Check per-page used length returned by the device to determine
which reported pages were zeroed. If used_len matches the page
size, the device successfully initialized the page (e.g. via
MADV_DONTNEED), and we set the corresponding zeroed_bitmap bit.

Gate host_zeroes_pages on the feature bit and page content:
when PAGE_POISON is negotiated with non-zero poison_val, the
device fills with poison not zeros, so pages are not zeroed.

Clear the feature in validate() if REPORTING is not present
or if PAGE_POISON is active with non-zero poison_val.

See the virtio spec change:
https://github.com/oasis-tcs/virtio-spec/issues/244

Signed-off-by: Michael S. Tsirkin <[email protected]>
Assisted-by: Claude:claude-opus-4-6
Assisted-by: cursor-agent:GPT-5.4-xhigh
---
 drivers/virtio/virtio_balloon.c     | 22 ++++++++++++++++++++--
 include/uapi/linux/virtio_balloon.h |  1 +
 2 files changed, 21 insertions(+), 2 deletions(-)

diff --git a/drivers/virtio/virtio_balloon.c b/drivers/virtio/virtio_balloon.c
index fc43d0d7bbbc..6586699396b7 100644
--- a/drivers/virtio/virtio_balloon.c
+++ b/drivers/virtio/virtio_balloon.c
@@ -204,6 +204,8 @@ static int virtballoon_free_page_report(struct 
page_reporting_dev_info *pr_dev_i
        struct virtqueue *vq = vb->reporting_vq;
        unsigned int i, err = 0;
 
+       bitmap_zero(pr_dev_info->zeroed_bitmap, nents);
+
        /* We should always be able to add these buffers to an empty queue. */
        for (i = 0; i < nents; i++) {
                struct scatterlist one;
@@ -223,10 +225,14 @@ static int virtballoon_free_page_report(struct 
page_reporting_dev_info *pr_dev_i
 
                /* When host has read buffer, this completes via balloon_ack */
                for (i = 0; i < nents; i++) {
-                       unsigned int unused;
+                       struct scatterlist *entry;
+                       unsigned int used_len;
 
                        wait_event(vb->acked,
-                                  virtqueue_get_buf(vq, &unused));
+                                  (entry = virtqueue_get_buf(vq, &used_len)));
+                       if (used_len == entry->length)
+                               set_bit(entry - sg,
+                                       pr_dev_info->zeroed_bitmap);
                }
        }
 
@@ -1048,6 +1054,9 @@ static int virtballoon_probe(struct virtio_device *vdev)
 #endif
 
                vb->pr_dev_info.capacity = capacity;
+               vb->pr_dev_info.host_zeroes_pages =
+                       virtio_has_feature(vdev,
+                                          
VIRTIO_BALLOON_F_DEVICE_INIT_REPORTED);
                err = page_reporting_register(&vb->pr_dev_info);
                if (err)
                        goto out_unregister_oom;
@@ -1173,6 +1182,14 @@ static int virtballoon_validate(struct virtio_device 
*vdev)
        else if (!virtio_has_feature(vdev, VIRTIO_BALLOON_F_PAGE_POISON))
                __virtio_clear_bit(vdev, VIRTIO_BALLOON_F_REPORTING);
 
+       if (!virtio_has_feature(vdev, VIRTIO_BALLOON_F_REPORTING))
+               __virtio_clear_bit(vdev, VIRTIO_BALLOON_F_DEVICE_INIT_REPORTED);
+
+       /* Device fills with poison_val, not zeros; disable zeroed hint */
+       if (virtio_has_feature(vdev, VIRTIO_BALLOON_F_PAGE_POISON) &&
+           !want_init_on_free())
+               __virtio_clear_bit(vdev, VIRTIO_BALLOON_F_DEVICE_INIT_REPORTED);
+
        __virtio_clear_bit(vdev, VIRTIO_F_ACCESS_PLATFORM);
        return 0;
 }
@@ -1184,6 +1201,7 @@ static unsigned int features[] = {
        VIRTIO_BALLOON_F_FREE_PAGE_HINT,
        VIRTIO_BALLOON_F_PAGE_POISON,
        VIRTIO_BALLOON_F_REPORTING,
+       VIRTIO_BALLOON_F_DEVICE_INIT_REPORTED,
 };
 
 static struct virtio_driver virtio_balloon_driver = {
diff --git a/include/uapi/linux/virtio_balloon.h 
b/include/uapi/linux/virtio_balloon.h
index ee35a372805d..13074631f300 100644
--- a/include/uapi/linux/virtio_balloon.h
+++ b/include/uapi/linux/virtio_balloon.h
@@ -37,6 +37,7 @@
 #define VIRTIO_BALLOON_F_FREE_PAGE_HINT        3 /* VQ to report free pages */
 #define VIRTIO_BALLOON_F_PAGE_POISON   4 /* Guest is using page poisoning */
 #define VIRTIO_BALLOON_F_REPORTING     5 /* Page reporting virtqueue */
+#define VIRTIO_BALLOON_F_DEVICE_INIT_REPORTED  6 /* Device initializes 
reported pages */
 
 /* Size of a PFN in the balloon interface. */
 #define VIRTIO_BALLOON_PFN_SHIFT 12
-- 
MST


Reply via email to