Skip to contentSkip to conversation

x86/Hyper-V: Balloon up according to request page number

PATCH Superseded [email protected] 4 participants 5 messages Jan 16, 2020 – Jan 20, 2020

Subsystems (3)

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 x86/Hyper-V: Balloon up according to request page number

From: Tianyu Lan <[email protected]>

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")

---
Change since v2:
- 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 | 12 ++++--------
 1 file changed, 4 insertions(+), 8 deletions(-)
drivers/hv/hv_balloon.c +4 -8
index 7f3e7ab22d5d..536807efbc35 100644
@@ -1684,7 +1684,7 @@ static unsigned int alloc_balloon_pages(struct hv_dynmem_device *dm,
 	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 +1722,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 +1737,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 +1746,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,7 +1766,7 @@ 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;
 		}
Signature · 1 line
2.14.5
Reviewed-by on v2 1/1
#1 lore raw
Quoted 71 lines · [email protected] writes:

From: Tianyu Lan <[email protected]>

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")

---
Change since v2:
- 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 | 12 ++++--------
 1 file changed, 4 insertions(+), 8 deletions(-)
drivers/hv/hv_balloon.c +3 -7
index 7f3e7ab22d5d..536807efbc35 100644
@@ -1684,7 +1684,7 @@ static unsigned int alloc_balloon_pages(struct hv_dynmem_device *dm,
 	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 +1722,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 +1737,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 +1746,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) {

drivers/hv/hv_balloon.c static void balloon_up(union dm_msg_info *msg_info) · line 1772

Quoted 8 lines
drivers/hv/hv_balloon.c +1 -1
@@ -1770,7 +1766,7 @@ 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;
 		}

Thank you for addressing my comments on v1, this all looks good to me now so:

Signature · 1 line
Vitaly
#2 lore raw
In reply to [email protected]

From: Tianyu Lan <[email protected]> Sent: Thursday, January 16, 2020 6:16 AM

Quoted 27 lines

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")

---
Change since v2:
- 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 | 12 ++++--------
 1 file changed, 4 insertions(+), 8 deletions(-)
drivers/hv/hv_balloon.c +0 -0
index 7f3e7ab22d5d..536807efbc35 100644

drivers/hv/hv_balloon.c static unsigned int alloc_balloon_pages(struct · line 1686

Quoted 3 lines
drivers/hv/hv_balloon.c +0 -0
@@ -1684,7 +1684,7 @@ static unsigned int alloc_balloon_pages(struct
 hv_dynmem_device *dm,
 	if (num_pages < alloc_unit)
 		return 0;

The above test is no longer necessary. The num_pages < alloc_unit case is handled implicitly by your new 'for' loop condition.

Quoted 43 lines
drivers/hv/hv_balloon.c +2 -2
-	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 +1722,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)

drivers/hv/hv_balloon.c +1 -5
@@ -1737,9 +1737,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 +1746,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) {

drivers/hv/hv_balloon.c static void balloon_up(union dm_msg_info *msg_info) · line 1769

Quoted 5 lines
drivers/hv/hv_balloon.c +1 -1
@@ -1770,7 +1766,7 @@ 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) {

Maybe I'm missing something, but I don't think Vitaly's optimization works. If alloc_unit specifies 2 Mbytes, and num_pages specifies 3 Mbytes, then num_ballooned will come back as 2 Mbytes, which is correct. But if we revert alloc_unit to 1 page and "continue" in that case, we will lose the 2 Mbytes of memory (it's not freed), and the next time through the loop will try to allocate only 1 Mbyte (because num_pages will be decremented by num_ballooned). I think the original code does the right thing.

Michael

Quoted 5 lines
drivers/hv/hv_balloon.c +0 -1
 			alloc_unit = 1;
 			continue;
 		}
--

2.14.5

#3 lore raw

Hi Michael:
Thanks for your review.

Quoted 37 lines

From: Michael Kelley <[email protected]>
Sent: Sunday, January 19, 2020 3:29 AM
To: [email protected]; KY Srinivasan <[email protected]>; Haiyang Zhang <[email protected]>; Stephen Hemminger <[email protected]>; [email protected]
Cc: Tianyu Lan <[email protected]>; [email protected]; [email protected]; vkuznets <[email protected]>; [email protected]
Subject: RE: [PATCH V2] x86/Hyper-V: Balloon up according to request page number

From: Tianyu Lan <[email protected]> Sent: Thursday, January 16, 2020 6:16 AM

Quoted 5 lines

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.

Quoted 18 lines

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")

---
Change since v2:
- 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 | 12 ++++--------
 1 file changed, 4 insertions(+), 8 deletions(-)
Quoted 12 lines

drivers/hv/hv_balloon.c static unsigned int alloc_balloon_pages(struct · line 1686

Quoted 8 lines
drivers/hv/hv_balloon.c index renamed from drivers/hv/hv_balloon.c +0 -0

7f3e7ab22d5d..536807efbc35 100644

drivers/hv/hv_balloon.c +0 -0
@@ -1684,7 +1684,7 @@ static unsigned int alloc_balloon_pages(struct
 hv_dynmem_device *dm,
 	if (num_pages < alloc_unit)
 		return 0;

The above test is no longer necessary. The num_pages < alloc_unit case is handled implicitly by your new 'for' loop condition.

Yes, will update in the next version.

Quoted 60 lines

drivers/hv/hv_balloon.c static void balloon_up(union dm_msg_info

Quoted 27 lines
drivers/hv/hv_balloon.c +2 -2
-	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 +1722,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
+1737,6 @@ static void balloon_up(union dm_msg_info *msg_info)

drivers/hv/hv_balloon.c +0 -3
 	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 +1746,13 @@ static void balloon_up(union dm_msg_info

*msg_info)

drivers/hv/hv_balloon.c static void balloon_up(union dm_msg_info

Quoted 16 lines
drivers/hv/hv_balloon.c +1 -2
 	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,7 +1766,7 @@ static void balloon_up(union dm_msg_info

*msg_info)

drivers/hv/hv_balloon.c static void balloon_up(union dm_msg_info · line 1766

Quoted 1 line
drivers/hv/hv_balloon.c +0 -0
 		num_ballooned = alloc_balloon_pages(&dm_device,

num_pages,

drivers/hv/hv_balloon.c static void balloon_up(union dm_msg_info *msg_info) · line 1769

Quoted 4 lines
drivers/hv/hv_balloon.c +1 -1
 						    bl_resp, alloc_unit);
 
-		if (alloc_unit != 1 && num_ballooned == 0) {
+		if (alloc_unit != 1 && num_ballooned != num_pages) {

Maybe I'm missing something, but I don't think Vitaly's optimization works. If alloc_unit specifies 2 Mbytes, and num_pages specifies 3 Mbytes, then num_ballooned will come back as 2 Mbytes, which is correct. But if we revert alloc_unit to 1 page and "continue" in that case, we will lose the 2 Mbytes of memory (it's not freed), and the next time through the loop will try to allocate only 1 Mbyte (because num_pages will be decremented by num_ballooned). I think the original code does the right thing.

Quoted 1 line

Michael

Sorry. I should remove the "continue" here and then it will work. Will fix this in the next version.

Quoted 6 lines
Quoted 6 lines
drivers/hv/hv_balloon.c +0 -1
 			alloc_unit = 1;
 			continue;
 		}
--

2.14.5

#4 lore raw
In reply to Michael Kelley
Quoted 81 lines · Michael Kelley

From: Tianyu Lan <[email protected]> Sent: Thursday, January 16, 2020 6:16 AM

Quoted 27 lines

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")

---
Change since v2:
- 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 | 12 ++++--------
 1 file changed, 4 insertions(+), 8 deletions(-)
drivers/hv/hv_balloon.c +0 -0
index 7f3e7ab22d5d..536807efbc35 100644

drivers/hv/hv_balloon.c static unsigned int alloc_balloon_pages(struct · line 1686

Quoted 3 lines
drivers/hv/hv_balloon.c +0 -0
@@ -1684,7 +1684,7 @@ static unsigned int alloc_balloon_pages(struct
 hv_dynmem_device *dm,
 	if (num_pages < alloc_unit)
 		return 0;

The above test is no longer necessary. The num_pages < alloc_unit case is handled implicitly by your new 'for' loop condition.

Quoted 46 lines
drivers/hv/hv_balloon.c +2 -2
-	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 +1722,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)

drivers/hv/hv_balloon.c +1 -5
@@ -1737,9 +1737,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 +1746,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,7 +1766,7 @@ static void balloon_up(union dm_msg_info *msg_info)
 		num_ballooned = alloc_balloon_pages(&dm_device, num_pages,
 						    bl_resp, alloc_unit);
 
Quoted 10 lines

drivers/hv/hv_balloon.c static void balloon_up(union dm_msg_info *msg_info) · line 1769

Quoted 2 lines
drivers/hv/hv_balloon.c +1 -1
-		if (alloc_unit != 1 && num_ballooned == 0) {
+		if (alloc_unit != 1 && num_ballooned != num_pages) {

Maybe I'm missing something, but I don't think Vitaly's optimization works. If alloc_unit specifies 2 Mbytes, and num_pages specifies 3 Mbytes, then num_ballooned will come back as 2 Mbytes, which is correct. But if we revert alloc_unit to 1 page and "continue" in that case, we will lose the 2 Mbytes of memory (it's not freed), and the next time through the loop will try to allocate only 1 Mbyte (because num_pages will be decremented by num_ballooned). I think the original code does the right thing.

True, nice catch! What I meant to say is that we can avoid sending two replies to the host and get away with just one. Unfortunately, "num_ballooned != num_pages" modification is not sufficient as alloc_balloon_pages() will overwrite bl_resp->range_array[] (as it always starts from 0). I think we can still achieve the goal by allocating bl_resp outside of the loop and filling it from range_array[range_count] instead of range_array[i] but it seems to big of a change for now particular gain. Let's just drop the idea.

Quoted 8 lines

Michael

Quoted 5 lines
drivers/hv/hv_balloon.c +0 -1
 			alloc_unit = 1;
 			continue;
 		}
--

2.14.5

Signature · 1 line
Vitaly