Linux-HyperV Archive on lore.kernel.org
 help / color / Atom feed
* [PATCH V3] x86/Hyper-V: Balloon up according to request page number
@ 2020-01-20  8:41 lantianyu1986
  2020-01-20 21:17 ` Michael Kelley
  0 siblings, 1 reply; 3+ messages in thread
From: lantianyu1986 @ 2020-01-20  8:41 UTC (permalink / raw)
  To: kys, haiyangz, sthemmin, sashal, michael.h.kelley
  Cc: Tianyu Lan, linux-hyperv, linux-kernel, vkuznets, stable

From: Tianyu Lan <Tianyu.Lan@microsoft.com>

Current code has assumption that balloon request memory size aligns
with 2MB. But actually Hyper-V doesn't guarantee such alignment. When
balloon driver receives non-aligned balloon request, it produces warning
and balloon up more memory than requested in order to keep 2MB alignment.
Remove the warning and balloon up memory according to actual requested
memory size.

Fixes: f6712238471a ("hv: hv_balloon: avoid memory leak on alloc_error of 2MB memory block")
Cc: stable@vger.kernel.org
Reviewed-by: Vitaly Kuznetsov <vkuznets@redhat.com>
Signed-off-by: Tianyu Lan <Tianyu.Lan@microsoft.com>
---
Change since v2:
    - Remove check between request page number and alloc_unit
    in the alloc_balloon_pages() because it's redundant with
    new change.
    - Remove the "continue" just follwoing alloc_unit switch
     from 2MB to 4K in order to avoid skipping allocated
     memory.

Change since v1:
    - Change logic of switching alloc_unit from 2MB to 4KB
    in the balloon_up() to avoid redundant iteration when
    handle non-aligned page request.
    - Remove 2MB alignment operation and comment in balloon_up()
---
 drivers/hv/hv_balloon.c | 17 ++++-------------
 1 file changed, 4 insertions(+), 13 deletions(-)

diff --git a/drivers/hv/hv_balloon.c b/drivers/hv/hv_balloon.c
index 7f3e7ab22d5d..73092a7a3345 100644
--- a/drivers/hv/hv_balloon.c
+++ b/drivers/hv/hv_balloon.c
@@ -1681,10 +1681,7 @@ static unsigned int alloc_balloon_pages(struct hv_dynmem_device *dm,
 	unsigned int i, j;
 	struct page *pg;
 
-	if (num_pages < alloc_unit)
-		return 0;
-
-	for (i = 0; (i * alloc_unit) < num_pages; i++) {
+	for (i = 0; i < num_pages / alloc_unit; i++) {
 		if (bl_resp->hdr.size + sizeof(union dm_mem_page_range) >
 			HV_HYP_PAGE_SIZE)
 			return i * alloc_unit;
@@ -1722,7 +1719,7 @@ static unsigned int alloc_balloon_pages(struct hv_dynmem_device *dm,
 
 	}
 
-	return num_pages;
+	return i * alloc_unit;
 }
 
 static void balloon_up(union dm_msg_info *msg_info)
@@ -1737,9 +1734,6 @@ static void balloon_up(union dm_msg_info *msg_info)
 	long avail_pages;
 	unsigned long floor;
 
-	/* The host balloons pages in 2M granularity. */
-	WARN_ON_ONCE(num_pages % PAGES_IN_2M != 0);
-
 	/*
 	 * We will attempt 2M allocations. However, if we fail to
 	 * allocate 2M chunks, we will go back to PAGE_SIZE allocations.
@@ -1749,14 +1743,13 @@ static void balloon_up(union dm_msg_info *msg_info)
 	avail_pages = si_mem_available();
 	floor = compute_balloon_floor();
 
-	/* Refuse to balloon below the floor, keep the 2M granularity. */
+	/* Refuse to balloon below the floor. */
 	if (avail_pages < num_pages || avail_pages - num_pages < floor) {
 		pr_warn("Balloon request will be partially fulfilled. %s\n",
 			avail_pages < num_pages ? "Not enough memory." :
 			"Balloon floor reached.");
 
 		num_pages = avail_pages > floor ? (avail_pages - floor) : 0;
-		num_pages -= num_pages % PAGES_IN_2M;
 	}
 
 	while (!done) {
@@ -1770,10 +1763,8 @@ static void balloon_up(union dm_msg_info *msg_info)
 		num_ballooned = alloc_balloon_pages(&dm_device, num_pages,
 						    bl_resp, alloc_unit);
 
-		if (alloc_unit != 1 && num_ballooned == 0) {
+		if (alloc_unit != 1 && num_ballooned != num_pages)
 			alloc_unit = 1;
-			continue;
-		}
 
 		if (num_ballooned == 0 || num_ballooned == num_pages) {
 			pr_debug("Ballooned %u out of %u requested pages.\n",
-- 
2.14.5


^ permalink raw reply	[flat|nested] 3+ messages in thread

* RE: [PATCH V3] x86/Hyper-V: Balloon up according to request page number
  2020-01-20  8:41 [PATCH V3] x86/Hyper-V: Balloon up according to request page number lantianyu1986
@ 2020-01-20 21:17 ` Michael Kelley
  2020-01-21  3:36   ` Tianyu Lan
  0 siblings, 1 reply; 3+ messages in thread
From: Michael Kelley @ 2020-01-20 21:17 UTC (permalink / raw)
  To: lantianyu1986, KY Srinivasan, Haiyang Zhang, Stephen Hemminger, sashal
  Cc: Tianyu Lan, linux-hyperv, linux-kernel, vkuznets, stable

From: Tianyu Lan <Tianyu.Lan@microsoft.com> Sent: Monday, January 20, 2020 12:42 AM
> 
> Current code has assumption that balloon request memory size aligns
> with 2MB. But actually Hyper-V doesn't guarantee such alignment. When
> balloon driver receives non-aligned balloon request, it produces warning
> and balloon up more memory than requested in order to keep 2MB alignment.
> Remove the warning and balloon up memory according to actual requested
> memory size.
> 
> Fixes: f6712238471a ("hv: hv_balloon: avoid memory leak on alloc_error of 2MB memory
> block")
> Cc: stable@vger.kernel.org
> Reviewed-by: Vitaly Kuznetsov <vkuznets@redhat.com>
> Signed-off-by: Tianyu Lan <Tianyu.Lan@microsoft.com>
> ---
> Change since v2:
>     - Remove check between request page number and alloc_unit
>     in the alloc_balloon_pages() because it's redundant with
>     new change.
>     - Remove the "continue" just follwoing alloc_unit switch
>      from 2MB to 4K in order to avoid skipping allocated
>      memory.
> 
> Change since v1:
>     - Change logic of switching alloc_unit from 2MB to 4KB
>     in the balloon_up() to avoid redundant iteration when
>     handle non-aligned page request.
>     - Remove 2MB alignment operation and comment in balloon_up()
> ---
>  drivers/hv/hv_balloon.c | 17 ++++-------------
>  1 file changed, 4 insertions(+), 13 deletions(-)
> 
> diff --git a/drivers/hv/hv_balloon.c b/drivers/hv/hv_balloon.c
> index 7f3e7ab22d5d..73092a7a3345 100644
> --- a/drivers/hv/hv_balloon.c
> +++ b/drivers/hv/hv_balloon.c
> @@ -1681,10 +1681,7 @@ static unsigned int alloc_balloon_pages(struct
> hv_dynmem_device *dm,
>  	unsigned int i, j;
>  	struct page *pg;
> 
> -	if (num_pages < alloc_unit)
> -		return 0;
> -
> -	for (i = 0; (i * alloc_unit) < num_pages; i++) {
> +	for (i = 0; i < num_pages / alloc_unit; i++) {
>  		if (bl_resp->hdr.size + sizeof(union dm_mem_page_range) >
>  			HV_HYP_PAGE_SIZE)
>  			return i * alloc_unit;
> @@ -1722,7 +1719,7 @@ static unsigned int alloc_balloon_pages(struct
> hv_dynmem_device *dm,
> 
>  	}
> 
> -	return num_pages;
> +	return i * alloc_unit;
>  }
> 
>  static void balloon_up(union dm_msg_info *msg_info)
> @@ -1737,9 +1734,6 @@ static void balloon_up(union dm_msg_info *msg_info)
>  	long avail_pages;
>  	unsigned long floor;
> 
> -	/* The host balloons pages in 2M granularity. */
> -	WARN_ON_ONCE(num_pages % PAGES_IN_2M != 0);
> -
>  	/*
>  	 * We will attempt 2M allocations. However, if we fail to
>  	 * allocate 2M chunks, we will go back to PAGE_SIZE allocations.
> @@ -1749,14 +1743,13 @@ static void balloon_up(union dm_msg_info *msg_info)
>  	avail_pages = si_mem_available();
>  	floor = compute_balloon_floor();
> 
> -	/* Refuse to balloon below the floor, keep the 2M granularity. */
> +	/* Refuse to balloon below the floor. */
>  	if (avail_pages < num_pages || avail_pages - num_pages < floor) {
>  		pr_warn("Balloon request will be partially fulfilled. %s\n",
>  			avail_pages < num_pages ? "Not enough memory." :
>  			"Balloon floor reached.");
> 
>  		num_pages = avail_pages > floor ? (avail_pages - floor) : 0;
> -		num_pages -= num_pages % PAGES_IN_2M;
>  	}
> 
>  	while (!done) {
> @@ -1770,10 +1763,8 @@ static void balloon_up(union dm_msg_info *msg_info)
>  		num_ballooned = alloc_balloon_pages(&dm_device, num_pages,
>  						    bl_resp, alloc_unit);
> 
> -		if (alloc_unit != 1 && num_ballooned == 0) {
> +		if (alloc_unit != 1 && num_ballooned != num_pages)
>  			alloc_unit = 1;
> -			continue;
> -		}

I don't think removing the "continue" works either.   Suppose the requested
size is 1 Mbyte.   With an alloc_unit of 2M, alloc_balloon_pages() will return
zero.  The code above will set alloc_unit to 1, which is correct.  But without the
"continue", the code below will mark "done" as true, and we won't loop back
around to try again with alloc_unit set to 1.

I think the original code needs to stay as is.

Michael

> 
>  		if (num_ballooned == 0 || num_ballooned == num_pages) {
>  			pr_debug("Ballooned %u out of %u requested pages.\n",
> --
> 2.14.5


^ permalink raw reply	[flat|nested] 3+ messages in thread

* RE: [PATCH V3] x86/Hyper-V: Balloon up according to request page number
  2020-01-20 21:17 ` Michael Kelley
@ 2020-01-21  3:36   ` Tianyu Lan
  0 siblings, 0 replies; 3+ messages in thread
From: Tianyu Lan @ 2020-01-21  3:36 UTC (permalink / raw)
  To: Michael Kelley, lantianyu1986, KY Srinivasan, Haiyang Zhang,
	Stephen Hemminger, sashal
  Cc: linux-hyperv, linux-kernel, vkuznets, stable

> From: Michael Kelley <mikelley@microsoft.com>
> Sent: Tuesday, January 21, 2020 5:18 AM
> To: lantianyu1986@gmail.com; KY Srinivasan <kys@microsoft.com>; Haiyang
> Zhang <haiyangz@microsoft.com>; Stephen Hemminger
> <sthemmin@microsoft.com>; sashal@kernel.org
> Cc: Tianyu Lan <Tianyu.Lan@microsoft.com>; linux-hyperv@vger.kernel.org;
> linux-kernel@vger.kernel.org; vkuznets <vkuznets@redhat.com>;
> stable@vger.kernel.org
> Subject: RE: [PATCH V3] x86/Hyper-V: Balloon up according to request page
> number
> 
> From: Tianyu Lan <Tianyu.Lan@microsoft.com> Sent: Monday, January 20,
> 2020 12:42 AM
> >
> > Current code has assumption that balloon request memory size aligns
> > with 2MB. But actually Hyper-V doesn't guarantee such alignment. When
> > balloon driver receives non-aligned balloon request, it produces
> > warning and balloon up more memory than requested in order to keep 2MB
> alignment.
> > Remove the warning and balloon up memory according to actual requested
> > memory size.
> >
> > Fixes: f6712238471a ("hv: hv_balloon: avoid memory leak on alloc_error
> > of 2MB memory
> > block")
> > Cc: stable@vger.kernel.org
> > Reviewed-by: Vitaly Kuznetsov <vkuznets@redhat.com>
> > Signed-off-by: Tianyu Lan <Tianyu.Lan@microsoft.com>
> > ---
> > Change since v2:
> >     - Remove check between request page number and alloc_unit
> >     in the alloc_balloon_pages() because it's redundant with
> >     new change.
> >     - Remove the "continue" just follwoing alloc_unit switch
> >      from 2MB to 4K in order to avoid skipping allocated
> >      memory.
> >
> > Change since v1:
> >     - Change logic of switching alloc_unit from 2MB to 4KB
> >     in the balloon_up() to avoid redundant iteration when
> >     handle non-aligned page request.
> >     - Remove 2MB alignment operation and comment in balloon_up()
> > ---
> >  drivers/hv/hv_balloon.c | 17 ++++-------------
> >  1 file changed, 4 insertions(+), 13 deletions(-)
> >
> > diff --git a/drivers/hv/hv_balloon.c b/drivers/hv/hv_balloon.c index
> > 7f3e7ab22d5d..73092a7a3345 100644
> > --- a/drivers/hv/hv_balloon.c
> > +++ b/drivers/hv/hv_balloon.c
> > @@ -1681,10 +1681,7 @@ static unsigned int alloc_balloon_pages(struct
> > hv_dynmem_device *dm,
> >  	unsigned int i, j;
> >  	struct page *pg;
> >
> > -	if (num_pages < alloc_unit)
> > -		return 0;
> > -
> > -	for (i = 0; (i * alloc_unit) < num_pages; i++) {
> > +	for (i = 0; i < num_pages / alloc_unit; i++) {
> >  		if (bl_resp->hdr.size + sizeof(union dm_mem_page_range) >
> >  			HV_HYP_PAGE_SIZE)
> >  			return i * alloc_unit;
> > @@ -1722,7 +1719,7 @@ static unsigned int alloc_balloon_pages(struct
> > hv_dynmem_device *dm,
> >
> >  	}
> >
> > -	return num_pages;
> > +	return i * alloc_unit;
> >  }
> >
> >  static void balloon_up(union dm_msg_info *msg_info) @@ -1737,9
> > +1734,6 @@ static void balloon_up(union dm_msg_info *msg_info)
> >  	long avail_pages;
> >  	unsigned long floor;
> >
> > -	/* The host balloons pages in 2M granularity. */
> > -	WARN_ON_ONCE(num_pages % PAGES_IN_2M != 0);
> > -
> >  	/*
> >  	 * We will attempt 2M allocations. However, if we fail to
> >  	 * allocate 2M chunks, we will go back to PAGE_SIZE allocations.
> > @@ -1749,14 +1743,13 @@ static void balloon_up(union dm_msg_info
> *msg_info)
> >  	avail_pages = si_mem_available();
> >  	floor = compute_balloon_floor();
> >
> > -	/* Refuse to balloon below the floor, keep the 2M granularity. */
> > +	/* Refuse to balloon below the floor. */
> >  	if (avail_pages < num_pages || avail_pages - num_pages < floor) {
> >  		pr_warn("Balloon request will be partially fulfilled. %s\n",
> >  			avail_pages < num_pages ? "Not enough memory." :
> >  			"Balloon floor reached.");
> >
> >  		num_pages = avail_pages > floor ? (avail_pages - floor) : 0;
> > -		num_pages -= num_pages % PAGES_IN_2M;
> >  	}
> >
> >  	while (!done) {
> > @@ -1770,10 +1763,8 @@ static void balloon_up(union dm_msg_info
> *msg_info)
> >  		num_ballooned = alloc_balloon_pages(&dm_device,
> num_pages,
> >  						    bl_resp, alloc_unit);
> >
> > -		if (alloc_unit != 1 && num_ballooned == 0) {
> > +		if (alloc_unit != 1 && num_ballooned != num_pages)
> >  			alloc_unit = 1;
> > -			continue;
> > -		}
> 
> I don't think removing the "continue" works either.   Suppose the requested
> size is 1 Mbyte.   With an alloc_unit of 2M, alloc_balloon_pages() will return
> zero.  The code above will set alloc_unit to 1, which is correct.  But without the
> "continue", the code below will mark "done" as true, and we won't loop back
> around to try again with alloc_unit set to 1.
> 
> I think the original code needs to stay as is.

Yes,  you are right. Will revert the change in the next version. Thanks.

> 
> Michael
> 
> >
> >  		if (num_ballooned == 0 || num_ballooned == num_pages) {
> >  			pr_debug("Ballooned %u out of %u requested
> pages.\n",
> > --
> > 2.14.5


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, back to index

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-01-20  8:41 [PATCH V3] x86/Hyper-V: Balloon up according to request page number lantianyu1986
2020-01-20 21:17 ` Michael Kelley
2020-01-21  3:36   ` Tianyu Lan

Linux-HyperV Archive on lore.kernel.org

Archives are clonable:
	git clone --mirror https://lore.kernel.org/linux-hyperv/0 linux-hyperv/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 linux-hyperv linux-hyperv/ https://lore.kernel.org/linux-hyperv \
		linux-hyperv@vger.kernel.org
	public-inbox-index linux-hyperv

Example config snippet for mirrors

Newsgroup available over NNTP:
	nntp://nntp.lore.kernel.org/org.kernel.vger.linux-hyperv


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git