[PATCH 1/2] virtio_balloon: fix race by fill and leak

Michael S. Tsirkin
Fri, 01 Jan 2016 08:26:32 -0000

On Mon, Dec 28, 2015 at 08:35:12AM +0900, Minchan Kim
wrote:
> During my compaction-related stuff, I encountered a bug
> with ballooning.
>
> With repeated inflating and deflating cycle, guest memory(
> ie, cat /proc/meminfo | grep MemTotal) is decreased and
> couldn't be recovered.
>
> The reason is balloon_lock doesn't cover release_pages_balloon
> so struct virtio_balloon fields could be overwritten by race
> of fill_balloon(e,g, vb->*pfns could be critical).
>
> This patch fixes it in my test.
>
> Cc: <stable at vger.kernel.org>
> Signed-off-by: Minchan Kim <minchan at kernel.org>

Acked-by: Michael S. Tsirkin <mst at redhat.com>

> ---
>  drivers/virtio/virtio_balloon.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/drivers/virtio/virtio_balloon.c
b/drivers/virtio/virtio_balloon.c
> index 7efc32945810..7d3e5d0e9aa4 100644
> --- a/drivers/virtio/virtio_balloon.c
> +++ b/drivers/virtio/virtio_balloon.c
> @@ -209,8 +209,8 @@ static unsigned leak_balloon(struct virtio_balloon *vb,
size_t num)
>  	 */
>  	if (vb->num_pfns != 0)
>  		tell_host(vb, vb->deflate_vq);
> -	mutex_unlock(&vb->balloon_lock);
>  	release_pages_balloon(vb);
> +	mutex_unlock(&vb->balloon_lock);
>  	return num_freed_pages;
>  }
>
> --
> 1.9.1