Thank you for your contribution! Sashiko AI review found 3 potential
issues to consider:
- [High] virtio_balloon: 32-bit used length overflow in
  virtballoon_free_page_report()
- [High] virtio_balloon: stale PG_zeroed flag on free pages across
  hibernation resume
- [Medium] virtio_balloon: missing feature dependency checks in
  virtballoon_validate()
--

commit 45401c714e001e18d0bb9e4b0f46a6691e93f0cb
Author: Michael S. Tsirkin <[email protected]>

virtio_balloon: add DEVICE_INIT_REPORTED feature

This commit adds support for the VIRTIO_BALLOON_F_DEVICE_INIT_REPORTED
feature, allowing page reporting to mark reported pages as zeroed when the
host initializes them on reclaim.

> diff --git a/drivers/virtio/virtio_balloon.c b/drivers/virtio/virtio_balloon.c
> index 8c1b47ec17a29..4c6630c5f9599 100644
> --- a/drivers/virtio/virtio_balloon.c
> +++ b/drivers/virtio/virtio_balloon.c
[ ... ]
> @@ -205,7 +206,9 @@ static int virtballoon_free_page_report(struct 
> page_reporting_dev_info *pr_dev_i
>       struct virtio_balloon *vb =
>               container_of(pr_dev_info, struct virtio_balloon, pr_dev_info);
>       struct virtqueue *vq = vb->reporting_vq;
> -     unsigned int unused, err;
> +     struct scatterlist *entry;
> +     unsigned int len, i;
> +     int err;
>  
>       /* We should always be able to add these buffers to an empty queue. */
>       err = virtqueue_add_inbuf(vq, sg, nents, vb, GFP_NOWAIT);
> @@ -221,9 +224,24 @@ static int virtballoon_free_page_report(struct 
> page_reporting_dev_info *pr_dev_i
>       virtqueue_kick(vq);
>  
>       /* When host has read buffer, this completes via balloon_ack */
> -     wait_event(vb->acked, virtqueue_get_buf(vq, &unused));
> +     wait_event(vb->acked, virtqueue_get_buf(vq, &len));
>  
> -     return 0;
> +     if (!pr_dev_info->host_zeroes_pages)
> +             return 0;
> +
> +     /*
> +      * Only a fully initialized batch may be marked reported when reported
> +      * pages are treated as zeroed. A short completion does not identify
> +      * which pages were initialized, so reject the entire batch.
> +      * Subtract each entry to avoid overflowing a total-length accumulator.
> +      */
> +     for_each_sg(sg, entry, nents, i) {
> +             if (len < entry->length)
> +                     return -EIO;
> +             len -= entry->length;
> +     }
> +
> +     return len ? -EIO : 0;
>  }

[Severity: High]
Can virtqueue_get_buf() ever return a len large enough for a batch of 4 GiB
or more?

The Virtio used ring stores a 32-bit __virtio32 len field, so len is at most
U32_MAX (4 GiB - 1).  On CONFIG_ARM64_64K_PAGES, MAX_PAGE_ORDER is 13
(512 MiB per block) and capacity is up to PAGE_REPORTING_CAPACITY (32), so a
single scatterlist batch of 8 or more order-13 blocks totals 4 GiB to 16 GiB.
For any batch of 4 GiB or more, subtracting entry->length from the 32-bit len
always hits len < entry->length and returns -EIO, causing
page_reporting_cycle() to abort without marking the pages reported and retry
the same blocks on every reporting pass.

> @@ -1043,6 +1061,9 @@ static int virtballoon_probe(struct virtio_device *vdev)
>  #if defined(CONFIG_ARM64) && defined(CONFIG_ARM64_64K_PAGES)
>               vb->pr_dev_info.order = 5;
>  #endif
> +             if (virtio_has_feature(vb->vdev,
> +                                   VIRTIO_BALLOON_F_DEVICE_INIT_REPORTED))
> +                     vb->pr_dev_info.host_zeroes_pages = true;

[Severity: High]
Does enabling host_zeroes_pages leave stale PG_zeroed flags on free buddy
pages across hibernation resume when want_init_on_free() and
page_poisoning_enabled() are false?

During hibernation snapshot creation, free buddy page frames are excluded
from the image, while the vmemmap pages containing their struct page entries
(with PG_zeroed set by page_reporting_drain()) are saved.  During resume, the
boot kernel uses those free physical page frames as temporary safe pages for
struct pbe chains and image buffers, and swsusp_arch_resume() restores the
saved struct page entries with PG_zeroed still set.  Afterward,
clear_or_poison_free_pages() only zeroes free pages when
page_poisoning_enabled() or want_init_on_free() is true:

kernel/power/snapshot.c:clear_or_poison_free_pages() {
    ...
        if (page_poisoning_enabled() || want_init_on_free()) {
                memory_bm_position_reset(bm);
                pfn = memory_bm_next_pfn(bm);
                while (pfn != BM_END_OF_MAP) {
                        if (pfn_valid(pfn))
                                clear_or_poison_free_page(pfn_to_page(pfn));
    ...
}

In the default configuration where both are disabled, post_alloc_hook() and
vma_alloc_zeroed_movable_folio() see PG_zeroed set and skip zeroing, handing
dirty resume-kernel memory to __GFP_ZERO allocations and userspace page
faults.

> @@ -1189,6 +1210,11 @@ 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 (cc_platform_has(CC_ATTR_GUEST_MEM_ENCRYPT) ||
> +         page_poisoning_enabled_static())
> +             __virtio_clear_bit(vdev,
> +                                VIRTIO_BALLOON_F_DEVICE_INIT_REPORTED);

[Severity: Medium]
Should virtballoon_validate() clear VIRTIO_BALLOON_F_DEVICE_INIT_REPORTED
when VIRTIO_BALLOON_F_REPORTING is not set, and keep VIRTIO_BALLOON_F_REPORTING
enabled when want_init_on_free() is satisfied by
VIRTIO_BALLOON_F_DEVICE_INIT_REPORTED?

If VIRTIO_BALLOON_F_REPORTING is absent or cleared above,
VIRTIO_BALLOON_F_DEVICE_INIT_REPORTED remains set in vdev->features_array,
which can cause virtio_features_ok() to fail probe with -ENODEV on a device
that requires VIRTIO_BALLOON_F_REPORTING as a prerequisite.  In addition,
when want_init_on_free() is true and page_poisoning_enabled_static() is
false, the check above clears VIRTIO_BALLOON_F_REPORTING if the device does
not offer VIRTIO_BALLOON_F_PAGE_POISON, even when
VIRTIO_BALLOON_F_DEVICE_INIT_REPORTED guarantees zeroed pages.

-- 
Sashiko AI review ยท 
https://sashiko.dev/#/patchset/[email protected]?part=6

Reply via email to