Skip to contentSkip to conversation

arm64: Clear the initial ID map correctly before remapping

PATCH Applied Zenghui Yu 4 participants 4 messages Jun 21, 2024 – Jun 24, 2024 arm64

lore
Share & tools
lore

Zenghui Yu

author
#0 lore raw

PATCH 1/1 arm64: Clear the initial ID map correctly before remapping

In the attempt to clear and recreate the initial ID map for LPA2, we wrongly use 'start - end' as the map size and make the memset() almost a nop.

Fix it by passing the correct map size.

Fixes 9684ec186f8f ("arm64: Enable LPA2 at boot if supported by the system")

---

Found by code inspection (don't have the appropriate HW to test it).

 arch/arm64/kernel/pi/map_kernel.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/arm64/kernel/pi/map_kernel.c +1 -1
index 5fa08e13e17e..f374a3e5a5fe 100644
@@ -173,7 +173,7 @@ static void __init remap_idmap_for_lpa2(void)
 	 * Don't bother with the FDT, we no longer need it after this.
 	 */
 	memset(init_idmap_pg_dir, 0,
-	       (u64)init_idmap_pg_dir - (u64)init_idmap_pg_end);
+	       (u64)init_idmap_pg_end - (u64)init_idmap_pg_dir);
 
 	create_init_idmap(init_idmap_pg_dir, mask);
 	dsb(ishst);
Signature · 1 line
2.33.0
Reviewed-by on v1 1/1
#1 lore raw
Quoted 5 lines · Zenghui Yu

In the attempt to clear and recreate the initial ID map for LPA2, we wrongly use 'start - end' as the map size and make the memset() almost a nop.

Quoted 8 lines

Fix it by passing the correct map size.

Fixes 9684ec186f8f ("arm64: Enable LPA2 at boot if supported by the system")

---

Found by code inspection (don't have the appropriate HW to test it).

Good catch!

Even though memset() takes an unsigned size_t, the zeroing path in arm64's memset.S does a signed compare on the provided size, and will zero at most 63 bytes if the size has the sign bit set. So in the end, it does not clear anything. Note that in this particular case, that doesn't actually matter - the memory is reused immediately to create another copy of the ID map, and any unused regions containing garbage will just be ignored.

Nonetheless,

Quoted 19 lines
 arch/arm64/kernel/pi/map_kernel.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/arm64/kernel/pi/map_kernel.c +1 -2
index 5fa08e13e17e..f374a3e5a5fe 100644
@@ -173,7 +173,7 @@ static void __init remap_idmap_for_lpa2(void)
         * Don't bother with the FDT, we no longer need it after this.
         */
        memset(init_idmap_pg_dir, 0,
-              (u64)init_idmap_pg_dir - (u64)init_idmap_pg_end);
+              (u64)init_idmap_pg_end - (u64)init_idmap_pg_dir);
 
        create_init_idmap(init_idmap_pg_dir, mask);
        dsb(ishst);
--

2.33.0

#2 lore raw
Quoted 15 lines · Ard Biesheuvel
Quoted 5 lines · Zenghui Yu

In the attempt to clear and recreate the initial ID map for LPA2, we wrongly use 'start - end' as the map size and make the memset() almost a nop.

Quoted 8 lines

Fix it by passing the correct map size.

Fixes 9684ec186f8f ("arm64: Enable LPA2 at boot if supported by the system")

---

Found by code inspection (don't have the appropriate HW to test it).

Quoted 6 lines

Good catch!

Even though memset() takes an unsigned size_t, the zeroing path in arm64's memset.S does a signed compare on the provided size, and will zero at most 63 bytes if the size has the sign bit set. So in the end, it does not clear anything.

Yup! It took me a while to figure out why memset() with a VERY large size doesn't corrupt the memory. ;-)

Quoted 4 lines

Note that in this particular case, that
doesn't actually matter - the memory is reused immediately to create another copy of the ID map, and any unused regions containing garbage will just be ignored.

Agreed. I can fold it into the commit message if Will/Catalin ask for a respin. And it looks like an alternate "fix" would be just removing the memset().

Quoted 3 lines

Nonetheless,

Thanks!

Zenghui

maintainer Applied
#3 lore raw
In reply to Zenghui Yu
Quoted 8 lines · Zenghui Yu

In the attempt to clear and recreate the initial ID map for LPA2, we wrongly use 'start - end' as the map size and make the memset() almost a nop.

Fix it by passing the correct map size.

[...]

Applied to arm64 (for-next/fixes), thanks!

[1/1] arm64: Clear the initial ID map correctly before remapping

      https://git.kernel.org/arm64/c/ecc54006f158

Cheers,

Signature · 5 lines
Will

https://fixes.arm64.dev
https://next.arm64.dev
https://will.arm64.dev