msm: board-8064: Fix overflow in memory calc.

An overflow occurs in locate_unstable_memory() when physical
memory address space ends at 0xFFFFFFFF. Change code to
handle overflow by ensuring that variables are limited to
0xFFFFFFFF and by rearranging additions and subtractions.

Change-Id: I11cb890c09ef18fe7c103cbdf1232677704fcc17
Signed-off-by: Olav Haugan <ohaugan@codeaurora.org>
diff --git a/arch/arm/mach-msm/board-8064.c b/arch/arm/mach-msm/board-8064.c
index a582260..dd6da2a 100644
--- a/arch/arm/mach-msm/board-8064.c
+++ b/arch/arm/mach-msm/board-8064.c
@@ -292,12 +292,21 @@
 	bank_size = apq8064_memory_bank_size();
 	low = meminfo.bank[0].start;
 	high = mb->start + mb->size;
+
+	/* Check if 32 bit overflow occured */
+	if (high < mb->start)
+		high = ~0UL;
+
 	low &= ~(bank_size - 1);
 
 	if (high - low <= bank_size)
 		return;
 	apq8064_reserve_info.low_unstable_address = low + bank_size;
-	apq8064_reserve_info.max_unstable_size = high - low - bank_size;
+	/* To avoid overflow of u32 compute max_unstable_size
+	 * by first subtracting low from mb->start)
+	 */
+	apq8064_reserve_info.max_unstable_size = (mb->start - low) +
+						mb->size - bank_size;
 	apq8064_reserve_info.bank_size = bank_size;
 	pr_info("low unstable address %lx max size %lx bank size %lx\n",
 		apq8064_reserve_info.low_unstable_address,