Skip to contentSkip to conversation

powerpc/pseries: cleanups for dlpar_memory_add* functions

PATCH Applied Daniel Henrique Barboza 3 participants 9 messages Jun 22, 2021 – Jun 25, 2021 PowerPC

lore
Share & tools
lore
#1 lore raw

PATCH 1/3 powerpc/pseries: skip reserved LMBs in dlpar_memory_add_by_count()

In reply to Daniel Henrique Barboza

The function is counting reserved LMBs as available to be added, but they aren't. This will cause the function to miscalculate the available LMBs and can trigger errors later on when executing dlpar_add_lmb().

---
 arch/powerpc/platforms/pseries/hotplug-memory.c | 3 +++
 1 file changed, 3 insertions(+)
arch/powerpc/platforms/pseries/hotplug-memory.c +3 -0
index 36f66556a7c6..28a7fd90232f 100644
@@ -683,6 +683,9 @@ static int dlpar_memory_add_by_count(u32 lmbs_to_add)
 
 	/* Validate that there are enough LMBs to satisfy the request */
 	for_each_drmem_lmb(lmb) {
+		if (lmb->flags & DRCONF_MEM_RESERVED)
+			continue;
+
 		if (!(lmb->flags & DRCONF_MEM_ASSIGNED))
 			lmbs_available++;
 
Signature · 1 line
2.31.1
#0 lore raw

cover powerpc/pseries: cleanups for dlpar_memory_add* functions

Hi,

These are a couple of cleanups for the dlpar_memory_add* functions that are similar to those I did a month or so ago in dlpar_memory_remove_by_count and dlpar_memory_remove_by_ic.

Daniel Henrique Barboza (3):
powerpc/pseries: skip reserved LMBs in dlpar_memory_add_by_count() powerpc/pseries: break early in dlpar_memory_add_by_count() loops powerpc/pseries: fail quicker in dlpar_memory_add_by_ic()

 .../platforms/pseries/hotplug-memory.c        | 34 ++++++++++++-------
 1 file changed, 21 insertions(+), 13 deletions(-)
Signature · 1 line
2.31.1
#3 lore raw

PATCH 2/3 powerpc/pseries: break early in dlpar_memory_add_by_count() loops

After a successful dlpar_add_lmb() call the LMB is marked as reserved. Later on, depending whether we added enough LMBs or not, we rely on the marked LMBs to see which ones might need to be removed, and we remove the reservation of all of them.

These are done in for_each_drmem_lmb() loops without any break condition. This means that we're going to check all LMBs of the partition even after going through all the reserved ones.

This patch adds break conditions in both loops to avoid this. The 'lmbs_added' variable was renamed to 'lmbs_reserved', and it's now being decremented each time a lmb reservation is removed, indicating if there are still marked LMBs to be processed.

---
 arch/powerpc/platforms/pseries/hotplug-memory.c | 17 ++++++++++++-----
 1 file changed, 12 insertions(+), 5 deletions(-)
arch/powerpc/platforms/pseries/hotplug-memory.c +12 -5
index 28a7fd90232f..c0a03e1537cb 100644
@@ -673,7 +673,7 @@ static int dlpar_memory_add_by_count(u32 lmbs_to_add)
 {
 	struct drmem_lmb *lmb;
 	int lmbs_available = 0;
-	int lmbs_added = 0;
+	int lmbs_reserved = 0;
 	int rc;
 
 	pr_info("Attempting to hot-add %d LMB(s)\n", lmbs_to_add);
@@ -714,13 +714,12 @@ static int dlpar_memory_add_by_count(u32 lmbs_to_add)
 		 * requested LMBs cannot be added.
 		 */
 		drmem_mark_lmb_reserved(lmb);
-
-		lmbs_added++;
-		if (lmbs_added == lmbs_to_add)
+		lmbs_reserved++;
+		if (lmbs_reserved == lmbs_to_add)
 			break;
 	}
 
-	if (lmbs_added != lmbs_to_add) {
+	if (lmbs_reserved != lmbs_to_add) {
 		pr_err("Memory hot-add failed, removing any added LMBs\n");
 
 		for_each_drmem_lmb(lmb) {
@@ -735,6 +734,10 @@ static int dlpar_memory_add_by_count(u32 lmbs_to_add)
 				dlpar_release_drc(lmb->drc_index);
 
 			drmem_remove_lmb_reservation(lmb);
+			lmbs_reserved--;
+
+			if (lmbs_reserved == 0)
+				break;
 		}
 		rc = -EINVAL;
 	} else {
@@ -745,6 +748,10 @@ static int dlpar_memory_add_by_count(u32 lmbs_to_add)
 			pr_debug("Memory at %llx (drc index %x) was hot-added\n",
 				 lmb->base_addr, lmb->drc_index);
 			drmem_remove_lmb_reservation(lmb);
+			lmbs_reserved--;
+
+			if (lmbs_reserved == 0)
+				break;
 		}
 		rc = 0;
 	}
Signature · 1 line
2.31.1
#6 lore raw

PATCH 3/3 powerpc/pseries: fail quicker in dlpar_memory_add_by_ic()

In reply to Daniel Henrique Barboza

The validation done at the start of dlpar_memory_add_by_ic() is an all of nothing scenario - if any LMBs in the range is marked as RESERVED we can fail right away.

We then can remove the 'lmbs_available' var and its check with 'lmbs_to_add' since the whole LMB range was already validated in the previous step.

---
 arch/powerpc/platforms/pseries/hotplug-memory.c | 14 ++++++--------
 1 file changed, 6 insertions(+), 8 deletions(-)
arch/powerpc/platforms/pseries/hotplug-memory.c +6 -8
index c0a03e1537cb..377d852f5a9a 100644
@@ -796,7 +796,6 @@ static int dlpar_memory_add_by_index(u32 drc_index)
 static int dlpar_memory_add_by_ic(u32 lmbs_to_add, u32 drc_index)
 {
 	struct drmem_lmb *lmb, *start_lmb, *end_lmb;
-	int lmbs_available = 0;
 	int rc;
 
 	pr_info("Attempting to hot-add %u LMB(s) at index %x\n",
@@ -811,15 +810,14 @@ static int dlpar_memory_add_by_ic(u32 lmbs_to_add, u32 drc_index)
 
 	/* Validate that the LMBs in this range are not reserved */
 	for_each_drmem_lmb_in_range(lmb, start_lmb, end_lmb) {
-		if (lmb->flags & DRCONF_MEM_RESERVED)
-			break;
-
-		lmbs_available++;
+		/* Fail immediately if the whole range can't be hot-added */
+		if (lmb->flags & DRCONF_MEM_RESERVED) {
+			pr_err("Memory at %llx (drc index %x) is reserved\n",
+					lmb->base_addr, lmb->drc_index);
+			return -EINVAL;
+		}
 	}
 
-	if (lmbs_available < lmbs_to_add)
-		return -EINVAL;
-
 	for_each_drmem_lmb_in_range(lmb, start_lmb, end_lmb) {
 		if (lmb->flags & DRCONF_MEM_ASSIGNED)
 			continue;
Signature · 1 line
2.31.1
Reviewed-by on v1 1/3
#2 lore raw

Re: [PATCH 1/3] powerpc/pseries: skip reserved LMBs in dlpar_memory_add_by_count()

In reply to Daniel Henrique Barboza

Le 22/06/2021 à 15:39, Daniel Henrique Barboza a écrit :

Quoted 3 lines

The function is counting reserved LMBs as available to be added, but they aren't. This will cause the function to miscalculate the available LMBs and can trigger errors later on when executing dlpar_add_lmb().

Indeed I'm wondering if dlpar_add_lmb() would fail in that case, so that's even better to check for that flag earlier.

Quoted 20 lines
---
  arch/powerpc/platforms/pseries/hotplug-memory.c | 3 +++
  1 file changed, 3 insertions(+)
arch/powerpc/platforms/pseries/hotplug-memory.c +3 -0
index 36f66556a7c6..28a7fd90232f 100644
@@ -683,6 +683,9 @@ static int dlpar_memory_add_by_count(u32 lmbs_to_add)
 
  	/* Validate that there are enough LMBs to satisfy the request */
  	for_each_drmem_lmb(lmb) {
+		if (lmb->flags & DRCONF_MEM_RESERVED)
+			continue;
+
  		if (!(lmb->flags & DRCONF_MEM_ASSIGNED))
  			lmbs_available++;
 
Reviewed-by on v1 2/3
#4 lore raw

Re: [PATCH 2/3] powerpc/pseries: break early in dlpar_memory_add_by_count() loops

In reply to Daniel Henrique Barboza

Le 22/06/2021 à 15:39, Daniel Henrique Barboza a écrit :

Quoted 5 lines

After a successful dlpar_add_lmb() call the LMB is marked as reserved. Later on, depending whether we added enough LMBs or not, we rely on the marked LMBs to see which ones might need to be removed, and we remove the reservation of all of them.

Quoted 8 lines

These are done in for_each_drmem_lmb() loops without any break condition. This means that we're going to check all LMBs of the partition even after going through all the reserved ones.

This patch adds break conditions in both loops to avoid this. The 'lmbs_added' variable was renamed to 'lmbs_reserved', and it's now being decremented each time a lmb reservation is removed, indicating if there are still marked LMBs to be processed.

Quoted 58 lines
---
  arch/powerpc/platforms/pseries/hotplug-memory.c | 17 ++++++++++++-----
  1 file changed, 12 insertions(+), 5 deletions(-)
arch/powerpc/platforms/pseries/hotplug-memory.c +12 -5
index 28a7fd90232f..c0a03e1537cb 100644
@@ -673,7 +673,7 @@ static int dlpar_memory_add_by_count(u32 lmbs_to_add)
  {
  	struct drmem_lmb *lmb;
  	int lmbs_available = 0;
-	int lmbs_added = 0;
+	int lmbs_reserved = 0;
  	int rc;
 
  	pr_info("Attempting to hot-add %d LMB(s)\n", lmbs_to_add);
@@ -714,13 +714,12 @@ static int dlpar_memory_add_by_count(u32 lmbs_to_add)
  		 * requested LMBs cannot be added.
  		 */
  		drmem_mark_lmb_reserved(lmb);
-
-		lmbs_added++;
-		if (lmbs_added == lmbs_to_add)
+		lmbs_reserved++;
+		if (lmbs_reserved == lmbs_to_add)
  			break;
  	}
 
-	if (lmbs_added != lmbs_to_add) {
+	if (lmbs_reserved != lmbs_to_add) {
  		pr_err("Memory hot-add failed, removing any added LMBs\n");
 
  		for_each_drmem_lmb(lmb) {
@@ -735,6 +734,10 @@ static int dlpar_memory_add_by_count(u32 lmbs_to_add)
  				dlpar_release_drc(lmb->drc_index);
 
  			drmem_remove_lmb_reservation(lmb);
+			lmbs_reserved--;
+
+			if (lmbs_reserved == 0)
+				break;
  		}
  		rc = -EINVAL;
  	} else {
@@ -745,6 +748,10 @@ static int dlpar_memory_add_by_count(u32 lmbs_to_add)
  			pr_debug("Memory at %llx (drc index %x) was hot-added\n",
  				 lmb->base_addr, lmb->drc_index);
  			drmem_remove_lmb_reservation(lmb);
+			lmbs_reserved--;
+
+			if (lmbs_reserved == 0)
+				break;
  		}
  		rc = 0;
  	}
Reviewed-by on v1 3/3
#7 lore raw

Re: [PATCH 3/3] powerpc/pseries: fail quicker in dlpar_memory_add_by_ic()

In reply to Daniel Henrique Barboza

Le 22/06/2021 à 15:39, Daniel Henrique Barboza a écrit :

Quoted 7 lines

The validation done at the start of dlpar_memory_add_by_ic() is an all of nothing scenario - if any LMBs in the range is marked as RESERVED we can fail right away.

We then can remove the 'lmbs_available' var and its check with 'lmbs_to_add' since the whole LMB range was already validated in the previous step.

Quoted 40 lines
---
  arch/powerpc/platforms/pseries/hotplug-memory.c | 14 ++++++--------
  1 file changed, 6 insertions(+), 8 deletions(-)
arch/powerpc/platforms/pseries/hotplug-memory.c +6 -8
index c0a03e1537cb..377d852f5a9a 100644
@@ -796,7 +796,6 @@ static int dlpar_memory_add_by_index(u32 drc_index)
  static int dlpar_memory_add_by_ic(u32 lmbs_to_add, u32 drc_index)
  {
  	struct drmem_lmb *lmb, *start_lmb, *end_lmb;
-	int lmbs_available = 0;
  	int rc;
 
  	pr_info("Attempting to hot-add %u LMB(s) at index %x\n",
@@ -811,15 +810,14 @@ static int dlpar_memory_add_by_ic(u32 lmbs_to_add, u32 drc_index)
 
  	/* Validate that the LMBs in this range are not reserved */
  	for_each_drmem_lmb_in_range(lmb, start_lmb, end_lmb) {
-		if (lmb->flags & DRCONF_MEM_RESERVED)
-			break;
-
-		lmbs_available++;
+		/* Fail immediately if the whole range can't be hot-added */
+		if (lmb->flags & DRCONF_MEM_RESERVED) {
+			pr_err("Memory at %llx (drc index %x) is reserved\n",
+					lmb->base_addr, lmb->drc_index);
+			return -EINVAL;
+		}
  	}
 
-	if (lmbs_available < lmbs_to_add)
-		return -EINVAL;
-
  	for_each_drmem_lmb_in_range(lmb, start_lmb, end_lmb) {
  		if (lmb->flags & DRCONF_MEM_ASSIGNED)
  			continue;
#8 lore raw
In reply to Daniel Henrique Barboza
Quoted 6 lines · Daniel Henrique Barboza

These are a couple of cleanups for the dlpar_memory_add* functions that are similar to those I did a month or so ago in dlpar_memory_remove_by_count and dlpar_memory_remove_by_ic.

Quoted 6 lines

Daniel Henrique Barboza (3):
powerpc/pseries: skip reserved LMBs in dlpar_memory_add_by_count() powerpc/pseries: break early in dlpar_memory_add_by_count() loops powerpc/pseries: fail quicker in dlpar_memory_add_by_ic()

[...]

Applied to powerpc/next.

[1/3] powerpc/pseries: skip reserved LMBs in dlpar_memory_add_by_count()

      https://git.kernel.org/powerpc/c/b3e3b4db7a9bafdd81735e6f8db0c5ee9b9d0b5f
[2/3] powerpc/pseries: break early in dlpar_memory_add_by_count() loops
      https://git.kernel.org/powerpc/c/c2aaddcc65b343fad4ed184e625abd3e68f63b9b
[3/3] powerpc/pseries: fail quicker in dlpar_memory_add_by_ic()
      https://git.kernel.org/powerpc/c/0e5962b2ec3d07204fd995ed07870bff3322525c

cheers

#5 lore raw

Re: [PATCH 2/3] powerpc/pseries: break early in dlpar_memory_add_by_count() loops

In reply to Daniel Henrique Barboza
Quoted 10 lines · Daniel Henrique Barboza

After a successful dlpar_add_lmb() call the LMB is marked as reserved. Later on, depending whether we added enough LMBs or not, we rely on the marked LMBs to see which ones might need to be removed, and we remove the reservation of all of them.

These are done in for_each_drmem_lmb() loops without any break condition. This means that we're going to check all LMBs of the partition even after going through all the reserved ones.

[...]

Applied to powerpc/next.

[2/3] powerpc/pseries: break early in dlpar_memory_add_by_count() loops

      https://git.kernel.org/powerpc/c/c2aaddcc65b343fad4ed184e625abd3e68f63b9b

cheers