Skip to contentSkip to conversation

Fix display of Maximum Memory

PATCH Applied Michael Bringmann 5 participants 6 messages Jan 15, 2020 – Jan 29, 2020 PowerPC

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 Fix display of Maximum Memory

Correct overflow problem in calculation+display of Maximum Memory value to syscfg where 32bits is insufficient.

---
 arch/powerpc/platforms/pseries/lparcfg.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
arch/powerpc/platforms/pseries/lparcfg.c +4 -4
index e33e8bc..f00411c 100644
@@ -433,12 +433,12 @@ static void parse_em_data(struct seq_file *m)
 
 static void maxmem_data(struct seq_file *m)
 {
-	unsigned long maxmem = 0;
+	u64 maxmem = 0;
 
-	maxmem += drmem_info->n_lmbs * drmem_info->lmb_size;
-	maxmem += hugetlb_total_pages() * PAGE_SIZE;
+	maxmem += (u64)drmem_info->n_lmbs * drmem_info->lmb_size;
+	maxmem += (u64)hugetlb_total_pages() * PAGE_SIZE;
 
-	seq_printf(m, "MaxMem=%ld\n", maxmem);
+	seq_printf(m, "MaxMem=%llu\n", maxmem);
 }
 
 static int pseries_lparcfg_data(struct seq_file *m, void *v)
Signature · 1 line
1.8.3.1
Reviewed-by on v2 1/1
#1 lore raw

Le 15/01/2020 à 15:53, Michael Bringmann a écrit :

Quoted 4 lines

Correct overflow problem in calculation+display of Maximum Memory value to syscfg where 32bits is insufficient.

Quoted 26 lines
---
  arch/powerpc/platforms/pseries/lparcfg.c | 8 ++++----
  1 file changed, 4 insertions(+), 4 deletions(-)
arch/powerpc/platforms/pseries/lparcfg.c +4 -4
index e33e8bc..f00411c 100644
@@ -433,12 +433,12 @@ static void parse_em_data(struct seq_file *m)
 
  static void maxmem_data(struct seq_file *m)
  {
-	unsigned long maxmem = 0;
+	u64 maxmem = 0;
 
-	maxmem += drmem_info->n_lmbs * drmem_info->lmb_size;
-	maxmem += hugetlb_total_pages() * PAGE_SIZE;
+	maxmem += (u64)drmem_info->n_lmbs * drmem_info->lmb_size;
+	maxmem += (u64)hugetlb_total_pages() * PAGE_SIZE;
 
-	seq_printf(m, "MaxMem=%ld\n", maxmem);
+	seq_printf(m, "MaxMem=%llu\n", maxmem);
  }
 
  static int pseries_lparcfg_data(struct seq_file *m, void *v)
Reviewed-by on v2 1/1
#2 lore raw
In reply to Michael Bringmann
Quoted 3 lines · Michael Bringmann

Correct overflow problem in calculation+display of Maximum Memory value to syscfg where 32bits is insufficient.

Probably needs the following Fixes tag:

Fixes 772b039fd9a7 ("powerpc/pseries: Export maximum memory value")

otherwise,

Quoted 27 lines
---
 arch/powerpc/platforms/pseries/lparcfg.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
arch/powerpc/platforms/pseries/lparcfg.c +4 -4
index e33e8bc..f00411c 100644
@@ -433,12 +433,12 @@ static void parse_em_data(struct seq_file *m)
 
 static void maxmem_data(struct seq_file *m)
 {
-	unsigned long maxmem = 0;
+	u64 maxmem = 0;
 
-	maxmem += drmem_info->n_lmbs * drmem_info->lmb_size;
-	maxmem += hugetlb_total_pages() * PAGE_SIZE;
+	maxmem += (u64)drmem_info->n_lmbs * drmem_info->lmb_size;
+	maxmem += (u64)hugetlb_total_pages() * PAGE_SIZE;
 
-	seq_printf(m, "MaxMem=%ld\n", maxmem);
+	seq_printf(m, "MaxMem=%llu\n", maxmem);
 }
 
 static int pseries_lparcfg_data(struct seq_file *m, void *v)
#3 lore raw
In reply to Michael Bringmann
Quoted 13 lines · Michael Bringmann

Correct overflow problem in calculation+display of Maximum Memory value to syscfg where 32bits is insufficient.

---
 arch/powerpc/platforms/pseries/lparcfg.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
arch/powerpc/platforms/pseries/lparcfg.c +0 -0
index e33e8bc..f00411c 100644

arch/powerpc/platforms/pseries/lparcfg.c static void parse_em_data(struct seq_file *m) · line 436

Quoted 5 lines
arch/powerpc/platforms/pseries/lparcfg.c +1 -1
@@ -433,12 +433,12 @@ static void parse_em_data(struct seq_file *m)
 
 static void maxmem_data(struct seq_file *m)
 {
-	unsigned long maxmem = 0;
+	u64 maxmem = 0;

This is 64-bit only code, so u64 == unsigned long.

arch/powerpc/platforms/pseries/lparcfg.c static void parse_em_data(struct seq_file *m) · line 437

Quoted 3 lines
arch/powerpc/platforms/pseries/lparcfg.c +1 -2
-	maxmem += drmem_info->n_lmbs * drmem_info->lmb_size;
-	maxmem += hugetlb_total_pages() * PAGE_SIZE;
+	maxmem += (u64)drmem_info->n_lmbs * drmem_info->lmb_size;

The only problem AFAICS is n_lmbs is int and lmb_size is u32, so this multiplication will overflow.

arch/powerpc/platforms/pseries/lparcfg.c static void parse_em_data(struct seq_file *m) · line 438

Quoted 1 line
arch/powerpc/platforms/pseries/lparcfg.c +1 -0
+	maxmem += (u64)hugetlb_total_pages() * PAGE_SIZE;

hugetlb_total_pages() already returns unsigned long.

arch/powerpc/platforms/pseries/lparcfg.c static void parse_em_data(struct seq_file *m) · line 440

Quoted 3 lines
arch/powerpc/platforms/pseries/lparcfg.c +1 -1
-	seq_printf(m, "MaxMem=%ld\n", maxmem);
+	seq_printf(m, "MaxMem=%llu\n", maxmem);
 }

This should be sufficient?

arch/powerpc/platforms/pseries/lparcfg.c +2 -2
index e33e8bc4b69b..38c306551f76 100644
@@ -435,10 +435,10 @@ static void maxmem_data(struct seq_file *m)
 {
        unsigned long maxmem = 0;
 
-       maxmem += drmem_info->n_lmbs * drmem_info->lmb_size;
+       maxmem += (unsigned long)drmem_info->n_lmbs * drmem_info->lmb_size;
        maxmem += hugetlb_total_pages() * PAGE_SIZE;
 
-       seq_printf(m, "MaxMem=%ld\n", maxmem);
+       seq_printf(m, "MaxMem=%lu\n", maxmem);
 }
 
 static int pseries_lparcfg_data(struct seq_file *m, void *v)

cheers

#4 lore raw
Quoted 58 lines · Michael Ellerman
Quoted 13 lines · Michael Bringmann

Correct overflow problem in calculation+display of Maximum Memory value to syscfg where 32bits is insufficient.

---
 arch/powerpc/platforms/pseries/lparcfg.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
arch/powerpc/platforms/pseries/lparcfg.c +0 -0
index e33e8bc..f00411c 100644

arch/powerpc/platforms/pseries/lparcfg.c static void parse_em_data(struct seq_file *m) · line 436

Quoted 5 lines
arch/powerpc/platforms/pseries/lparcfg.c +1 -1
@@ -433,12 +433,12 @@ static void parse_em_data(struct seq_file *m)
 
 static void maxmem_data(struct seq_file *m)
 {
-	unsigned long maxmem = 0;
+	u64 maxmem = 0;

This is 64-bit only code, so u64 == unsigned long.

arch/powerpc/platforms/pseries/lparcfg.c static void parse_em_data(struct seq_file *m) · line 437

Quoted 3 lines
arch/powerpc/platforms/pseries/lparcfg.c +1 -2
-	maxmem += drmem_info->n_lmbs * drmem_info->lmb_size;
-	maxmem += hugetlb_total_pages() * PAGE_SIZE;
+	maxmem += (u64)drmem_info->n_lmbs * drmem_info->lmb_size;

The only problem AFAICS is n_lmbs is int and lmb_size is u32, so this multiplication will overflow.

arch/powerpc/platforms/pseries/lparcfg.c static void parse_em_data(struct seq_file *m) · line 438

Quoted 1 line
arch/powerpc/platforms/pseries/lparcfg.c +1 -0
+	maxmem += (u64)hugetlb_total_pages() * PAGE_SIZE;

hugetlb_total_pages() already returns unsigned long.

arch/powerpc/platforms/pseries/lparcfg.c static void maxmem_data(struct seq_file *m) · line 442

Quoted 3 lines
arch/powerpc/platforms/pseries/lparcfg.c +1 -1
-	seq_printf(m, "MaxMem=%ld\n", maxmem);
+	seq_printf(m, "MaxMem=%llu\n", maxmem);
 }

This should be sufficient?

arch/powerpc/platforms/pseries/lparcfg.c +2 -2
index e33e8bc4b69b..38c306551f76 100644
@@ -435,10 +435,10 @@ static void maxmem_data(struct seq_file *m)
 {
        unsigned long maxmem = 0;
 
-       maxmem += drmem_info->n_lmbs * drmem_info->lmb_size;
+       maxmem += (unsigned long)drmem_info->n_lmbs * drmem_info->lmb_size;
        maxmem += hugetlb_total_pages() * PAGE_SIZE;
 
-       seq_printf(m, "MaxMem=%ld\n", maxmem);
+       seq_printf(m, "MaxMem=%lu\n", maxmem);
 }
 
 static int pseries_lparcfg_data(struct seq_file *m, void *v)
Quoted 2 lines

cheers

Trying it out.

Signature · 7 lines
Michael W. Bringmann
Linux Technology Center
IBM Corporation
Tie-Line  363-5196
External: (512) 286-5196
Cell:       (512) 466-0650
[email protected]