Skip to contentSkip to conversation

memsize: Fix for bug in memory sizing code

PATCH Superseded Gerd Hoffmann 3 participants 8 messages Oct 21, 2014 – Jan 15, 2015

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 memsize: Fix for bug in memory sizing code

The original memory sizing code in get_ram_size clobbers the word at the base address, but forgets to restore it.

Unlike the (reverted) commit b8496cced856ff411f1eb2e4eff20f5abe7080b0 we'll go save and restore the base address value at the start and then end of the function. It needs to stay cleared until the detection is done, otherwise we'll fail to detect the same piece of memory being mapped multiple times into the address space.

Cc Wolfgang Denk <[email protected]>
Cc Iwo Mergler <[email protected]>
---
 common/memsize.c | 11 +++++++----
 1 file changed, 7 insertions(+), 4 deletions(-)
common/memsize.c +7 -4
index 589400d..72d0156 100644
@@ -27,12 +27,15 @@ DECLARE_GLOBAL_DATA_PTR;
 long get_ram_size(long *base, long maxsize)
 {
 	volatile long *addr;
-	long           save[32];
+	long           save[32], save_base;
 	long           cnt;
 	long           val;
 	long           size;
 	int            i = 0;
 
+	save_base = base[0];
+	sync ();
+
 	for (cnt = (maxsize / sizeof (long)) >> 1; cnt > 0; cnt >>= 1) {
 		addr = base + cnt;	/* pointer arith! */
 		sync ();
@@ -43,8 +46,6 @@ long get_ram_size(long *base, long maxsize)
 
 	addr = base;
 	sync ();
-	save[i] = *addr;
-	sync ();
 	*addr = 0;
 
 	sync ();
@@ -52,12 +53,12 @@ long get_ram_size(long *base, long maxsize)
 		/* Restore the original data before leaving the function.
 		 */
 		sync ();
-		*addr = save[i];
 		for (cnt = 1; cnt < maxsize / sizeof(long); cnt <<= 1) {
 			addr  = base + cnt;
 			sync ();
 			*addr = save[--i];
 		}
+		base[0] = save_base;
 		return (0);
 	}
 
@@ -73,10 +74,12 @@ long get_ram_size(long *base, long maxsize)
 				addr  = base + cnt;
 				*addr = save[--i];
 			}
+			base[0] = save_base;
 			return (size);
 		}
 	}
 
+	base[0] = save_base;
 	return (maxsize);
 }
 
Signature · 1 line
1.8.3.1
#1 lore raw

PATCH 1/3 common/memsize.c: Coding style cleanup

Prepare code to make later modifications checkpatch-clean.

---
 common/memsize.c | 31 +++++++++++++++++--------------
 1 file changed, 17 insertions(+), 14 deletions(-)
common/memsize.c +17 -14
index 589400d..0fb9ba5 100644
@@ -33,43 +33,46 @@ long get_ram_size(long *base, long maxsize)
 	long           size;
 	int            i = 0;
 
-	for (cnt = (maxsize / sizeof (long)) >> 1; cnt > 0; cnt >>= 1) {
+	for (cnt = (maxsize / sizeof(long)) >> 1; cnt > 0; cnt >>= 1) {
 		addr = base + cnt;	/* pointer arith! */
-		sync ();
+		sync();
 		save[i++] = *addr;
-		sync ();
+		sync();
 		*addr = ~cnt;
 	}
 
 	addr = base;
-	sync ();
+	sync();
 	save[i] = *addr;
-	sync ();
+	sync();
 	*addr = 0;
 
-	sync ();
+	sync();
 	if ((val = *addr) != 0) {
-		/* Restore the original data before leaving the function.
-		 */
-		sync ();
+		/* Restore the original data before leaving the function. */
+		sync();
 		*addr = save[i];
 		for (cnt = 1; cnt < maxsize / sizeof(long); cnt <<= 1) {
 			addr  = base + cnt;
-			sync ();
+			sync();
 			*addr = save[--i];
 		}
 		return (0);
 	}
 
-	for (cnt = 1; cnt < maxsize / sizeof (long); cnt <<= 1) {
+	for (cnt = 1; cnt < maxsize / sizeof(long); cnt <<= 1) {
 		addr = base + cnt;	/* pointer arith! */
 		val = *addr;
 		*addr = save[--i];
 		if (val != ~cnt) {
-			size = cnt * sizeof (long);
-			/* Restore the original data before leaving the function.
+			size = cnt * sizeof(long);
+			/*
+			 * Restore the original data
+			 * before leaving the function.
 			 */
-			for (cnt <<= 1; cnt < maxsize / sizeof (long); cnt <<= 1) {
+			for (cnt <<= 1;
+			     cnt < maxsize / sizeof(long);
+			     cnt <<= 1) {
 				addr  = base + cnt;
 				*addr = save[--i];
 			}
Signature · 1 line
1.8.3.1
#2 lore raw

PATCH 2/3 common/memsize.c: correctly restore all modified memory loations

The original memory sizing code in get_ram_size clobbers the word at the base address, but forgets to restore it.

Unlike the (reverted) commit b8496cced856ff411f1eb2e4eff20f5abe7080b0 we save and restore the base address value at the start resp. at the end of the function. It needs to stay cleared until the detection is done, otherwise it will fail to detect the same piece of memory being mapped multiple times into the address space.

---
 common/memsize.c | 16 +++++++++++++---
 1 file changed, 13 insertions(+), 3 deletions(-)
common/memsize.c +13 -3
index 0fb9ba5..d5827dd 100644
@@ -1,5 +1,5 @@
 /*
- * (C) Copyright 2004
+ * (C) Copyright 2004-2014
  * Wolfgang Denk, DENX Software Engineering, wd at denx.de.
  *
  * SPDX-License-Identifier:	GPL-2.0+
@@ -31,7 +31,7 @@ long get_ram_size(long *base, long maxsize)
 	long           cnt;
 	long           val;
 	long           size;
-	int            i = 0;
+	int            i = 0, last;
 
 	for (cnt = (maxsize / sizeof(long)) >> 1; cnt > 0; cnt >>= 1) {
 		addr = base + cnt;	/* pointer arith! */
@@ -44,6 +44,7 @@ long get_ram_size(long *base, long maxsize)
 	addr = base;
 	sync();
 	save[i] = *addr;
+	last = i;
 	sync();
 	*addr = 0;
 
@@ -51,7 +52,7 @@ long get_ram_size(long *base, long maxsize)
 	if ((val = *addr) != 0) {
 		/* Restore the original data before leaving the function. */
 		sync();
-		*addr = save[i];
+		*addr = save[last];
 		for (cnt = 1; cnt < maxsize / sizeof(long); cnt <<= 1) {
 			addr  = base + cnt;
 			sync();
@@ -62,7 +63,9 @@ long get_ram_size(long *base, long maxsize)
 
 	for (cnt = 1; cnt < maxsize / sizeof(long); cnt <<= 1) {
 		addr = base + cnt;	/* pointer arith! */
+		sync();
 		val = *addr;
+		sync();
 		*addr = save[--i];
 		if (val != ~cnt) {
 			size = cnt * sizeof(long);
@@ -74,12 +77,19 @@ long get_ram_size(long *base, long maxsize)
 			     cnt < maxsize / sizeof(long);
 			     cnt <<= 1) {
 				addr  = base + cnt;
+				sync();
 				*addr = save[--i];
 			}
+			addr = base;
+			sync();
+			*addr = save[last];
 			return (size);
 		}
 	}
 
+	addr = base;
+	sync();
+	*addr = save[last];
 	return (maxsize);
 }
 
Signature · 1 line
1.8.3.1
#3 lore raw

PATCH 2/3 common/memsize.c: correctly restore all modified memory loations

Note: there is no patch 3/3 in his series.

It was originally

[PATCH] powerpc: TQM5200: convert to generic board

but this is actually not related to these changes here, so I decided to post it independently.

Sorry for the confusion.

Best regards,

Wolfgang Denk

Signature · 7 lines
DENX Software Engineering GmbH,     MD: Wolfgang Denk & Detlev Zundel
HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
Phone: (+49)-8142-66989-10 Fax: (+49)-8142-66989-80 Email: wd at denx.de
"I knew then (in 1970) that a 4-kbyte minicomputer would cost as much
as a house. So I reasoned  that  after  college,  I'd  have  to  live
cheaply in an apartment and put all my money into owning a computer."
      - Apple co-founder Steve Wozniak, EE Times, June 6, 1988, pg 45
#4 lore raw

PATCH 2/3 common/memsize.c: correctly restore all modified memory loations

In reply to Wolfgang Denk

Hi,

Quoted 1 line

No, I didn't signed this patch off.
And I will not because it is broken.

common/memsize.c long get_ram_size(long *base, long maxsize) · line 46

Quoted 6 lines
common/memsize.c +0 -0
 	for (cnt = (maxsize / sizeof(long)) >> 1; cnt > 0; cnt >>= 1) {
 		addr = base + cnt;	/* pointer arith! */
@@ -44,6 +44,7 @@ long get_ram_size(long *base, long maxsize)
 	addr = base;
 	sync();
 	save[i] = *addr;

base[0] might not be the original value any more at this point ...

common/memsize.c long get_ram_size(long *base, long maxsize) · line 93

Quoted 4 lines
common/memsize.c +3 -0
+	addr = base;
+	sync();
+	*addr = save[last];
 	return (maxsize);

... so this may corrupt memory.

cheers,
Gerd

#5 lore raw

PATCH 1/3 common/memsize.c: Coding style cleanup

In reply to Wolfgang Denk
Quoted 3 lines · Wolfgang Denk

Prepare code to make later modifications checkpatch-clean.

Applied to u-boot/master, thanks!

Signature · 8 lines
Tom
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 836 bytes
Desc: Digital signature
URL: <http://lists.denx.de/pipermail/u-boot/attachments/20150115/162fb415/attachment.pgp>
#6 lore raw

PATCH v2 1/1 memsize: Fix for bug in memory sizing code

In reply to Gerd Hoffmann

Dear Gerd Hoffmann,

Quoted 2 lines · In message <[email protected]> you wrote:

The original memory sizing code in get_ram_size clobbers the word at the base address, but forgets to restore it.

The funny thing is that it avtually works on some boards. Do you have an explanation for that?

common/memsize.c DECLARE_GLOBAL_DATA_PTR; · line 30

Quoted 2 lines
common/memsize.c +1 -1
-	long           save[32];
+	long           save[32], save_base;

Why do you need another variable? The original code stores the value as last entry in save[] - what's wrong with that?

common/memsize.c DECLARE_GLOBAL_DATA_PTR; · line 37

Quoted 2 lines
common/memsize.c +2 -0
+	save_base = base[0];
+	sync ();

You add this code here, but...

common/memsize.c long get_ram_size(long *base, long maxsize) · line 47

Quoted 9 lines
common/memsize.c +0 -2
 	for (cnt = (maxsize / sizeof (long)) >> 1; cnt > 0; cnt >>= 1) {
 		addr = base + cnt;	/* pointer arith! */
 		sync ();
@@ -43,8 +46,6 @@ long get_ram_size(long *base, long maxsize)
 
 	addr = base;
 	sync ();
-	save[i] = *addr;
-	sync ();

...remove it's equivalent here. Why would your code be any better than the existing one?

common/memsize.c long get_ram_size(long *base, long maxsize) · line 61

Quoted 7 lines
common/memsize.c +1 -1
-		*addr = save[i];
 		for (cnt = 1; cnt < maxsize / sizeof(long); cnt <<= 1) {
 			addr  = base + cnt;
 			sync ();
 			*addr = save[--i];
 		}
+		base[0] = save_base;

Same here - the line you removed above does the very same as the one you add here. In which way is the new code supposed to be different or even better?

common/memsize.c long get_ram_size(long *base, long maxsize) · line 83

Quoted 11 lines
common/memsize.c +2 -0
@@ -73,10 +74,12 @@ long get_ram_size(long *base, long maxsize)
 				addr  = base + cnt;
 				*addr = save[--i];
 			}
+			base[0] = save_base;
 			return (size);
 		}
 	}
 
+	base[0] = save_base;
 	return (maxsize);

Agreed here. These two make sense to me. I still wonder why it works on the boards I used for testing, while it's failing on yours.

Which exit path are you taking? The one at the end of the function, i. e. "return (maxsize)" ? What happens if you double the memory size to be checked?

I'll resend a slightly reworked patch in a minute; could you please test if this works for you, too?

Thanks.

Best regards,

Wolfgang Denk

Signature · 5 lines
DENX Software Engineering GmbH,     MD: Wolfgang Denk & Detlev Zundel
HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
Phone: (+49)-8142-66989-10 Fax: (+49)-8142-66989-80 Email: wd at denx.de
Q:  Why do mountain climbers rope themselves together?
A:  To prevent the sensible ones from going home.
#7 lore raw

PATCH v2 1/1 memsize: Fix for bug in memory sizing code

Quoted 8 lines · Wolfgang Denk

Dear Gerd Hoffmann,

Quoted 2 lines · In message <[email protected]> you wrote:

The original memory sizing code in get_ram_size clobbers the word at the base address, but forgets to restore it.

The funny thing is that it avtually works on some boards. Do you have an explanation for that?

Sure. If the same piece of memory appears multiple times in the address space the base address value is saved and restored multiple times too.

Quoted 5 lines

common/memsize.c DECLARE_GLOBAL_DATA_PTR; · line 30

Quoted 2 lines
common/memsize.c +1 -1
-	long           save[32];
+	long           save[32], save_base;

Why do you need another variable? The original code stores the value as last entry in save[] - what's wrong with that?

last entry index isn't known at the places where I save/restore the value, so I did it this way. Alternative approach would be to use the first index instead and shift all the other values by one. Matter of taste, both will get the job done.

Quoted 10 lines

common/memsize.c DECLARE_GLOBAL_DATA_PTR; · line 37

Quoted 2 lines
common/memsize.c +2 -0
+	save_base = base[0];
+	sync ();

You add this code here, but...

Quoted 5 lines
common/memsize.c +0 -0
 	for (cnt = (maxsize / sizeof (long)) >> 1; cnt > 0; cnt >>= 1) {
 		addr = base + cnt;	/* pointer arith! */
 		sync ();
@@ -43,8 +46,6 @@ long get_ram_size(long *base, long maxsize)
 
Quoted 7 lines

common/memsize.c long get_ram_size(long *base, long maxsize) · line 47

Quoted 4 lines
common/memsize.c +0 -2
 	addr = base;
 	sync ();
-	save[i] = *addr;
-	sync ();

...remove it's equivalent here. Why would your code be any better than the existing one?

There can be multiple saves/restores for the base memory location (see above). Therefore the ordering matters, restore must use the reverse order of save. No exception for base[0], so when we restore it last we must save it first.

Quoted 12 lines

common/memsize.c long get_ram_size(long *base, long maxsize) · line 61

Quoted 8 lines
common/memsize.c +1 -1
-		*addr = save[i];
 		for (cnt = 1; cnt < maxsize / sizeof(long); cnt <<= 1) {
 			addr  = base + cnt;
 			sync ();
 			*addr = save[--i];
 		}
+		base[0] = save_base;

Same here - the line you removed above does the very same as the one you add here. In which way is the new code supposed to be different or even better?

... and we must restore it last everywhere.

Quoted 15 lines

common/memsize.c long get_ram_size(long *base, long maxsize) · line 83

Quoted 11 lines
common/memsize.c +2 -0
@@ -73,10 +74,12 @@ long get_ram_size(long *base, long maxsize)
 				addr  = base + cnt;
 				*addr = save[--i];
 			}
+			base[0] = save_base;
 			return (size);
 		}
 	}
 
+	base[0] = save_base;
 	return (maxsize);

Agreed here. These two make sense to me. I still wonder why it works on the boards I used for testing, while it's failing on yours.

Quoted 3 lines

Which exit path are you taking? The one at the end of the function, i. e. "return (maxsize)" ? What happens if you double the memory size to be checked?

Both cases can happen, depending on how much memory I configure for qemu, and both are working correctly for me.

cheers,
Gerd