Re: [PATCH 1/2] virtio_balloon: fix pages_to_free calculation

From: Khazhismel Kumykov
Date: Mon Nov 18 2019 - 18:21:04 EST


On Mon, Nov 18, 2019 at 3:09 PM Michael S. Tsirkin <mst@xxxxxxxxxx> wrote:
>
>
> So I really think we should do something like the below instead.
> Limit playing with balloon pages so we can gradually limit it to legacy.
> Testing, review would be appreciated.
>
> Signed-off-by: Michael S. Tsirkin <mst@xxxxxxxxxx>
>
>
> diff --git a/drivers/virtio/virtio_balloon.c b/drivers/virtio/virtio_balloon.c
> index 226fbb995fb0..7cee05cdf3fb 100644
> --- a/drivers/virtio/virtio_balloon.c
> +++ b/drivers/virtio/virtio_balloon.c
> @@ -772,6 +772,13 @@ static unsigned long shrink_free_pages(struct virtio_balloon *vb,
> return blocks_freed << VIRTIO_BALLOON_FREE_PAGE_ORDER;
> }
>
> +static unsigned long leak_balloon_pages(struct virtio_balloon *vb,
> + unsigned long pages_to_free)
> +{
> + return leak_balloon(vb, pages_to_free * VIRTIO_BALLOON_PAGES_PER_PAGE) /
> + VIRTIO_BALLOON_PAGES_PER_PAGE;
> +}
> +
> static unsigned long shrink_balloon_pages(struct virtio_balloon *vb,
> unsigned long pages_to_free)
> {
> @@ -782,11 +789,9 @@ static unsigned long shrink_balloon_pages(struct virtio_balloon *vb,
> * VIRTIO_BALLOON_ARRAY_PFNS_MAX balloon pages, so we call it
> * multiple times to deflate pages till reaching pages_to_free.
> */
> - while (vb->num_pages && pages_to_free) {
> - pages_freed += leak_balloon(vb, pages_to_free) /
> - VIRTIO_BALLOON_PAGES_PER_PAGE;
> - pages_to_free -= pages_freed;
> - }
> + while (vb->num_pages && pages_freed < pages_to_free)
> + pages_freed += leak_balloon_pages(vb, pages_to_free);
> +
> update_balloon_size(vb);
>
> return pages_freed;
> @@ -799,7 +804,7 @@ static unsigned long virtio_balloon_shrinker_scan(struct shrinker *shrinker,
> struct virtio_balloon *vb = container_of(shrinker,
> struct virtio_balloon, shrinker);
>
> - pages_to_free = sc->nr_to_scan * VIRTIO_BALLOON_PAGES_PER_PAGE;
> + pages_to_free = sc->nr_to_scan;
>
> if (virtio_has_feature(vb->vdev, VIRTIO_BALLOON_F_FREE_PAGE_HINT))
> pages_freed = shrink_free_pages(vb, pages_to_free);
>

I got confused about shrink_free_pages, since, looking at the
operations the function *should* return the same units it takes in.
However, it does look like it should be operating on system pages
(VIRTIO_BALLOON_FREE_PAGE_ORDER is the order of our real page
allocations), so the multiplication on nr_to_scan is where we messed
up. OK. Sorry for the mixup :)

Your patch looks correct to me.

Attachment: smime.p7s
Description: S/MIME Cryptographic Signature