Skip to contentSkip to conversation

hv: hv_balloon: avoid memory leak on alloc_error of 2MB memory block

PATCH In review Dexuan Cui 3 participants 3 messages Nov 25, 2014 Hyper-V/Azure core and drivers

lore
Share & tools
lore

Dexuan Cui

author maintainer
#0 lore raw

PATCH v2 1/1 hv: hv_balloon: avoid memory leak on alloc_error of 2MB memory block

If num_ballooned is not 0, we shouldn't neglect the already-partially-allocated 2MB memory block(s).

Cc Jason Wang <[email protected]>
Cc K. Y. Srinivasan <[email protected]>

---

v2: I fixed the logic error in v1, pointed by Jason Wang:
In v1: in the case of partially-allocated 2MB, alloc_error is true, so we'll run "done = true" and hence we won't proceed with the next iteration of trying 4K allocation.

I also changed the WARN_ON to WARN_ON_ONCE in case the host behavior changes in the future.

 drivers/hv/hv_balloon.c | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)
drivers/hv/hv_balloon.c +7 -3
index 5e90c5d..b958ded 100644
@@ -1087,10 +1087,12 @@ static void balloon_up(struct work_struct *dummy)
 	struct dm_balloon_response *bl_resp;
 	int alloc_unit;
 	int ret;
-	bool alloc_error = false;
+	bool alloc_error;
 	bool done = false;
 	int i;
 
+	/* 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
@@ -1107,16 +1109,18 @@ static void balloon_up(struct work_struct *dummy)
 
 
 		num_pages -= num_ballooned;
+		alloc_error = false;
 		num_ballooned = alloc_balloon_pages(&dm_device, num_pages,
 						bl_resp, alloc_unit,
 						 &alloc_error);
 
-		if ((alloc_error) && (alloc_unit != 1)) {
+		if (alloc_unit != 1 && num_ballooned == 0) {
 			alloc_unit = 1;
 			continue;
 		}
 
-		if ((alloc_error) || (num_ballooned == num_pages)) {
+		if ((alloc_unit == 1 && alloc_error) ||
+			(num_ballooned == num_pages)) {
 			bl_resp->more_pages = 0;
 			done = true;
 			dm_device.state = DM_INITIALIZED;
Signature · 1 line
1.9.1
Acked-by on v2 1/1
#1 lore raw
Quoted 48 lines · Dexuan Cui

If num_ballooned is not 0, we shouldn't neglect the already-partially-allocated 2MB memory block(s).

Cc Jason Wang <[email protected]>
Cc K. Y. Srinivasan <[email protected]>

---

v2: I fixed the logic error in v1, pointed by Jason Wang:
In v1: in the case of partially-allocated 2MB, alloc_error is true, so we'll run "done = true" and hence we won't proceed with the next iteration of trying 4K allocation.

I also changed the WARN_ON to WARN_ON_ONCE in case the host behavior changes in the future.

 drivers/hv/hv_balloon.c | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)
drivers/hv/hv_balloon.c +5 -2
index 5e90c5d..b958ded 100644
@@ -1087,10 +1087,12 @@ static void balloon_up(struct work_struct *dummy)
 	struct dm_balloon_response *bl_resp;
 	int alloc_unit;
 	int ret;
-	bool alloc_error = false;
+	bool alloc_error;
 	bool done = false;
 	int i;
 
+	/* 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
@@ -1107,16 +1109,18 @@ static void balloon_up(struct work_struct *dummy)
 
 
 		num_pages -= num_ballooned;
+		alloc_error = false;
 		num_ballooned = alloc_balloon_pages(&dm_device, num_pages,
 						bl_resp, alloc_unit,
 						 &alloc_error);
 
-		if ((alloc_error) && (alloc_unit != 1)) {
+		if (alloc_unit != 1 && num_ballooned == 0) {

drivers/hv/hv_balloon.c static void balloon_up(struct work_struct *dummy) · line 1126

Quoted 10 lines
drivers/hv/hv_balloon.c +2 -1
 			alloc_unit = 1;
 			continue;
 		}
 
-		if ((alloc_error) || (num_ballooned == num_pages)) {
+		if ((alloc_unit == 1 && alloc_error) ||
+			(num_ballooned == num_pages)) {
 			bl_resp->more_pages = 0;
 			done = true;
 			dm_device.state = DM_INITIALIZED;

Thanks

#2 lore raw
In reply to Dexuan Cui
Quoted 12 lines

-----Original Message-----
From: Dexuan Cui [mailto:[email protected]]
Sent: Monday, November 24, 2014 8:33 PM
To: [email protected]; [email protected]; driverdev- [email protected]; [email protected]; [email protected]; [email protected]; KY Srinivasan
Cc: Haiyang Zhang
Subject: [PATCH v2] hv: hv_balloon: avoid memory leak on alloc_error of 2MB memory block

Quoted 6 lines

If num_ballooned is not 0, we shouldn't neglect the already-partially- allocated 2MB memory block(s).

Cc Jason Wang <[email protected]>
Cc K. Y. Srinivasan <[email protected]>
Quoted 57 lines

---

v2: I fixed the logic error in v1, pointed by Jason Wang:
In v1: in the case of partially-allocated 2MB, alloc_error is true, so we'll run "done = true" and hence we won't proceed with the next iteration of trying 4K allocation.

I also changed the WARN_ON to WARN_ON_ONCE in case the host
behavior
changes in the future.

 drivers/hv/hv_balloon.c | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)
drivers/hv/hv_balloon.c index renamed from drivers/hv/hv_balloon.c +0 -0

5e90c5d..b958ded 100644

drivers/hv/hv_balloon.c +3 -1
@@ -1087,10 +1087,12 @@ static void balloon_up(struct work_struct
 *dummy)
 	struct dm_balloon_response *bl_resp;
 	int alloc_unit;
 	int ret;
-	bool alloc_error = false;
+	bool alloc_error;
 	bool done = false;
 	int i;
 
+	/* The host balloons pages in 2M granularity. */
+	WARN_ON_ONCE(num_pages % PAGES_IN_2M != 0);
 
 	/*
drivers/hv/hv_balloon.c +2 -0
 	 * We will attempt 2M allocations. However, if we fail to @@ -1107,16
+1109,18 @@ static void balloon_up(struct work_struct *dummy)
 
 
 		num_pages -= num_ballooned;
+		alloc_error = false;
 		num_ballooned = alloc_balloon_pages(&dm_device,

num_pages,

drivers/hv/hv_balloon.c +3 -3
 						bl_resp, alloc_unit,
 						 &alloc_error);
 
-		if ((alloc_error) && (alloc_unit != 1)) {
+		if (alloc_unit != 1 && num_ballooned == 0) {
 			alloc_unit = 1;
 			continue;
 		}
 
-		if ((alloc_error) || (num_ballooned == num_pages)) {
+		if ((alloc_unit == 1 && alloc_error) ||
+			(num_ballooned == num_pages)) {
 			bl_resp->more_pages = 0;
 			done = true;
 			dm_device.state = DM_INITIALIZED;
--

1.9.1