[PATCH v2] x86/kaslr: Remove two redundant if checks

Thorsten Blum posted 1 patch 2 months ago
There is a newer version of this series
arch/x86/boot/compressed/kaslr.c | 13 ++++---------
1 file changed, 4 insertions(+), 9 deletions(-)
[PATCH v2] x86/kaslr: Remove two redundant if checks
Posted by Thorsten Blum 2 months ago
The loop in slots_fetch_random() either returns the slot address or
exits with i == slot_area_index, making the if check redundant. Drop it.

In choose_random_location(), the if (*output != random_addr) check used
to guard an add_identity_map() call. Commit 8570978ea030
("x86/boot/compressed/64: Don't pre-map memory in KASLR code") removed
that call and the check now only guards an assignment. Remove it.

No functional change.

Signed-off-by: Thorsten Blum <thorsten.blum@linux.dev>
---
Changes in v2:
- Explain why the check in choose_random_location() is redundant (Boris)
- Reword subject
- v1: https://lore.kernel.org/r/20260714205752.133163-2-thorsten.blum@linux.dev/
---
 arch/x86/boot/compressed/kaslr.c | 13 ++++---------
 1 file changed, 4 insertions(+), 9 deletions(-)

diff --git a/arch/x86/boot/compressed/kaslr.c b/arch/x86/boot/compressed/kaslr.c
index 8e4bf5365ac6..181257a6e0aa 100644
--- a/arch/x86/boot/compressed/kaslr.c
+++ b/arch/x86/boot/compressed/kaslr.c
@@ -544,8 +544,7 @@ static u64 slots_fetch_random(void)
 		return slot_areas[i].addr + ((u64)slot * CONFIG_PHYSICAL_ALIGN);
 	}
 
-	if (i == slot_area_index)
-		debug_putstr("slots_fetch_random() failed!?\n");
+	debug_putstr("slots_fetch_random() failed!?\n");
 	return 0;
 }
 
@@ -893,14 +892,10 @@ void choose_random_location(unsigned long input,
 
 	/* Walk available memory entries to find a random address. */
 	random_addr = find_random_phys_addr(min_addr, output_size);
-	if (!random_addr) {
+	if (!random_addr)
 		warn("Physical KASLR disabled: no suitable memory region!");
-	} else {
-		/* Update the new physical address location. */
-		if (*output != random_addr)
-			*output = random_addr;
-	}
-
+	else
+		*output = random_addr;
 
 	/* Pick random virtual address starting from LOAD_PHYSICAL_ADDR. */
 	if (IS_ENABLED(CONFIG_X86_64))
Re: [PATCH v2] x86/kaslr: Remove two redundant if checks
Posted by Borislav Petkov 2 months ago
On Fri, Jul 31, 2026 at 09:39:34AM +0200, Thorsten Blum wrote:
> The loop in slots_fetch_random() either returns the slot address or
> exits with i == slot_area_index, making the if check redundant. Drop it.
> 
> In choose_random_location(), the if (*output != random_addr) check used
> to guard an add_identity_map() call.

Are you sure it did that?

-> 8391c73c96f2 ("x86/KASLR: Randomize virtual address separately")

You actually removed the comment which says what it did.

And what's wrong with the check?

-- 
Regards/Gruss,
    Boris.

https://people.kernel.org/tglx/notes-about-netiquette