Skip to contentSkip to conversation

riscv: Fix a number of free'd resources in init_resources()

PATCH In review Petr Pavlu 4 participants 4 messages Aug 7, 2021 – Aug 20, 2021 RISC-V

lore
Share & tools
lore

Petr Pavlu

author
#0 lore raw

PATCH 1/1 riscv: Fix a number of free'd resources in init_resources()

Function init_resources() allocates a boot memory block to hold an array of resources which it adds to iomem_resource. The array is filled in from its end and the function then attempts to free any unused memory at the beginning. The problem is that size of the unused memory is incorrectly calculated and this can result in releasing memory which is in use by active resources. Their data then gets corrupted later when the memory is reused by a different part of the system.

Fix the size of the released memory to correctly match the number of unused resource entries.

Fixes ffe0e5261268 ("RISC-V: Improve init_resources()")
---
 arch/riscv/kernel/setup.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
arch/riscv/kernel/setup.c +2 -2
index 18bd0e4bc36c..120b2f6f71bc 100644
@@ -229,8 +229,8 @@ static void __init init_resources(void)
 	}
 
 	/* Clean-up any unused pre-allocated resources */
-	mem_res_sz = (num_resources - res_idx + 1) * sizeof(*mem_res);
-	memblock_free(__pa(mem_res), mem_res_sz);
+	if (res_idx >= 0)
+		memblock_free(__pa(mem_res), (res_idx + 1) * sizeof(*mem_res));
 	return;
 
  error:
Signature · 7 lines
2.32.0


_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv
Acked-by on v1 1/1
#1 lore raw

Στις 2021-08-07 20:54, Petr Pavlu έγραψε:

Quoted 22 lines

Function init_resources() allocates a boot memory block to hold an array of resources which it adds to iomem_resource. The array is filled in from its end and the function then attempts to free any unused memory at the beginning. The problem is that size of the unused memory is incorrectly calculated and this can result in releasing memory which is in use by active resources. Their data then gets corrupted later when the memory is reused by a different part of the system.

Fix the size of the released memory to correctly match the number of unused resource entries.

Fixes ffe0e5261268 ("RISC-V: Improve init_resources()")
---
 arch/riscv/kernel/setup.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
arch/riscv/kernel/setup.c +0 -0
index 18bd0e4bc36c..120b2f6f71bc 100644

arch/riscv/kernel/setup.c static void __init init_resources(void) · line 236

Quoted 10 lines
arch/riscv/kernel/setup.c +2 -2
@@ -229,8 +229,8 @@ static void __init init_resources(void)
 	}
 
 	/* Clean-up any unused pre-allocated resources */
-	mem_res_sz = (num_resources - res_idx + 1) * sizeof(*mem_res);
-	memblock_free(__pa(mem_res), mem_res_sz);
+	if (res_idx >= 0)
+		memblock_free(__pa(mem_res), (res_idx + 1) * sizeof(*mem_res));
 	return;
 
  error:

Ouch! Thanks for the fix.

_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv

Reviewed-by Tested-by on v1 1/1
#2 lore raw
In reply to Petr Pavlu
Quoted 22 lines · Petr Pavlu

Function init_resources() allocates a boot memory block to hold an array of resources which it adds to iomem_resource. The array is filled in from its end and the function then attempts to free any unused memory at the beginning. The problem is that size of the unused memory is incorrectly calculated and this can result in releasing memory which is in use by active resources. Their data then gets corrupted later when the memory is reused by a different part of the system.

Fix the size of the released memory to correctly match the number of unused resource entries.

Fixes ffe0e5261268 ("RISC-V: Improve init_resources()")
---
 arch/riscv/kernel/setup.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
arch/riscv/kernel/setup.c +0 -0
index 18bd0e4bc36c..120b2f6f71bc 100644

arch/riscv/kernel/setup.c static void __init init_resources(void) · line 236

Quoted 10 lines
arch/riscv/kernel/setup.c +2 -2
@@ -229,8 +229,8 @@ static void __init init_resources(void)
 	}
 
 	/* Clean-up any unused pre-allocated resources */
-	mem_res_sz = (num_resources - res_idx + 1) * sizeof(*mem_res);
-	memblock_free(__pa(mem_res), mem_res_sz);
+	if (res_idx >= 0)
+		memblock_free(__pa(mem_res), (res_idx + 1) * sizeof(*mem_res));
 	return;
 
  error:

I encountered this corruption and your patch came in time.

Thank you very much!

Quoted 8 lines
Signature · 7 lines
2.32.0


_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv

_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv

maintainer
#3 lore raw
In reply to Petr Pavlu
Quoted 22 lines · On Sat, 07 Aug 2021 10:54:50 PDT (-0700), [email protected] wrote:

Function init_resources() allocates a boot memory block to hold an array of resources which it adds to iomem_resource. The array is filled in from its end and the function then attempts to free any unused memory at the beginning. The problem is that size of the unused memory is incorrectly calculated and this can result in releasing memory which is in use by active resources. Their data then gets corrupted later when the memory is reused by a different part of the system.

Fix the size of the released memory to correctly match the number of unused resource entries.

Fixes ffe0e5261268 ("RISC-V: Improve init_resources()")
---
 arch/riscv/kernel/setup.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
arch/riscv/kernel/setup.c +0 -0
index 18bd0e4bc36c..120b2f6f71bc 100644

arch/riscv/kernel/setup.c static void __init init_resources(void) · line 236

Quoted 10 lines
arch/riscv/kernel/setup.c +2 -2
@@ -229,8 +229,8 @@ static void __init init_resources(void)
 	}
 
 	/* Clean-up any unused pre-allocated resources */
-	mem_res_sz = (num_resources - res_idx + 1) * sizeof(*mem_res);
-	memblock_free(__pa(mem_res), mem_res_sz);
+	if (res_idx >= 0)
+		memblock_free(__pa(mem_res), (res_idx + 1) * sizeof(*mem_res));
 	return;
 
  error:

Thanks, this is on fixes.

_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv