Skip to contentSkip to conversation

efi/libstub: Ensure instruction cache coherency after kernel relocation

PATCH RESEND Applied WANG Rui 3 participants 6 messages Apr 27 – Apr 29

Subsystems (2)
lore
Share & tools
lore

WANG Rui

author
#0 lore raw

v2 cover efi/libstub: Ensure instruction cache coherency after kernel relocation

Changes since [v1]:
* Move efi_cache_sync_image() after check_platform_features(), matching arm64.
* Add "()" after function name in the subject.

The EFI stub relocates the kernel image by copying it to a new memory location using memcpy(). On architectures where the instruction and data caches are not automatically coherent, the copied instructions may not be visible to the instruction fetch unit without explicit cache maintenance.

This series adds the missing call to efi_cache_sync_image() in the generic relocation path, and provides a LoongArch implementation using the ibar instruction to ensure instruction fetches observe the updated contents.

[v1]: https://lore.kernel.org/loongarch/[email protected]

WANG Rui (2):
efi/loongarch: Implement efi_cache_sync_image efi/libstub: Synchronize instruction cache after kernel relocation

 drivers/firmware/efi/libstub/loongarch.c | 5 +++++
 drivers/firmware/efi/libstub/relocate.c  | 1 +
 2 files changed, 6 insertions(+)
Signature · 1 line
2.54.0
author
#1 lore raw

PATCH RESEND v2 1/2 efi/loongarch: Implement efi_cache_sync_image()

Provide a LoongArch implementation of efi_cache_sync_image() to ensure instruction cache coherency after the kernel image is relocated.

---
 drivers/firmware/efi/libstub/loongarch.c | 5 +++++
 1 file changed, 5 insertions(+)
drivers/firmware/efi/libstub/loongarch.c +5 -0
index 9825f5218137..f7938d5c196a 100644
@@ -18,6 +18,11 @@ efi_status_t check_platform_features(void)
 	return EFI_SUCCESS;
 }
 
+void efi_cache_sync_image(unsigned long image_base, unsigned long alloc_size)
+{
+	asm volatile ("ibar 0" ::: "memory");
+}
+
 struct exit_boot_struct {
 	efi_memory_desc_t	*runtime_map;
 	int			runtime_entry_count;
Signature · 1 line
2.54.0
author
#2 lore raw

PATCH RESEND v2 2/2 efi/libstub: Synchronize instruction cache after kernel relocation

In reply to WANG Rui

The relocated kernel image is copied to its new location using memcpy(). On architectures with separate instruction and data caches, the copied instructions may remain stale in the instruction cache, leading to the execution of outdated contents.

Call efi_cache_sync_image() after the relocation copy to ensure the instruction cache is synchronized with the updated memory contents before control is transferred to the relocated kernel.

---
 drivers/firmware/efi/libstub/relocate.c | 1 +
 1 file changed, 1 insertion(+)
drivers/firmware/efi/libstub/relocate.c +1 -0
index d4264bfb6dc1..913b425d089d 100644
@@ -158,6 +158,7 @@ efi_status_t efi_relocate_kernel(unsigned long *image_addr,
 	 * have been allocated by UEFI, so we can safely use memcpy.
 	 */
 	memcpy((void *)new_addr, (void *)cur_image_addr, image_size);
+	efi_cache_sync_image(new_addr, image_size);
 
 	/* Return the new address of the relocated image. */
 	*image_addr = new_addr;
Signature · 1 line
2.54.0
maintainer Applied
#3 lore raw
In reply to WANG Rui
Quoted 13 lines · WANG Rui

Changes since [v1]:
* Move efi_cache_sync_image() after check_platform_features(), matching arm64.
* Add "()" after function name in the subject.

The EFI stub relocates the kernel image by copying it to a new memory location using memcpy(). On architectures where the instruction and data caches are not automatically coherent, the copied instructions may not be visible to the instruction fetch unit without explicit cache maintenance.

This series adds the missing call to efi_cache_sync_image() in the generic relocation path, and provides a LoongArch implementation using the ibar instruction to ensure instruction fetches observe the updated contents.

Quoted 6 lines

[v1]: https://lore.kernel.org/loongarch/[email protected]

WANG Rui (2):
efi/loongarch: Implement efi_cache_sync_image efi/libstub: Synchronize instruction cache after kernel relocation

Queued up in efi/urgent - thanks.

Quoted 6 lines
 drivers/firmware/efi/libstub/loongarch.c | 5 +++++
 drivers/firmware/efi/libstub/relocate.c  | 1 +
 2 files changed, 6 insertions(+)
Signature · 1 line
2.54.0
maintainer
#4 lore raw
Quoted 14 lines · Ard Biesheuvel
Quoted 13 lines · WANG Rui

Changes since [v1]:
* Move efi_cache_sync_image() after check_platform_features(), matching arm64.
* Add "()" after function name in the subject.

The EFI stub relocates the kernel image by copying it to a new memory location using memcpy(). On architectures where the instruction and data caches are not automatically coherent, the copied instructions may not be visible to the instruction fetch unit without explicit cache maintenance.

This series adds the missing call to efi_cache_sync_image() in the generic relocation path, and provides a LoongArch implementation using the ibar instruction to ensure instruction fetches observe the updated contents.

Quoted 9 lines
Quoted 6 lines

[v1]: https://lore.kernel.org/loongarch/[email protected]

WANG Rui (2):
efi/loongarch: Implement efi_cache_sync_image efi/libstub: Synchronize instruction cache after kernel relocation

Queued up in efi/urgent - thanks.

This breaks the ARM build.

Since LoongArch is the only remaining user of efi_relocate_kernel(), I'll move it into drivers/firmware/efi/libstub/loongarch-stub.c first, and fix up these patches accordingly afterwards (no need to resend anything)

maintainer
#5 lore raw
Quoted 18 lines · Ard Biesheuvel
Quoted 14 lines · Ard Biesheuvel
Quoted 13 lines · WANG Rui

Changes since [v1]:
* Move efi_cache_sync_image() after check_platform_features(), matching arm64.
* Add "()" after function name in the subject.

The EFI stub relocates the kernel image by copying it to a new memory location using memcpy(). On architectures where the instruction and data caches are not automatically coherent, the copied instructions may not be visible to the instruction fetch unit without explicit cache maintenance.

This series adds the missing call to efi_cache_sync_image() in the generic relocation path, and provides a LoongArch implementation using the ibar instruction to ensure instruction fetches observe the updated contents.

Quoted 8 lines
Quoted 8 lines
Quoted 6 lines

[v1]: https://lore.kernel.org/loongarch/[email protected]

WANG Rui (2):
efi/loongarch: Implement efi_cache_sync_image efi/libstub: Synchronize instruction cache after kernel relocation

Queued up in efi/urgent - thanks.

Maybe this series should cc stable?

Huacai

Quoted 6 lines
Quoted 1 line

This breaks the ARM build.

Since LoongArch is the only remaining user of efi_relocate_kernel(), I'll move it into drivers/firmware/efi/libstub/loongarch-stub.c first, and fix up these patches accordingly afterwards (no need to resend anything)