Skip to contentSkip to conversation

efi: introduce efi_arch_flush_dcache_area

PATCH Superseded Stefano Stabellini 5 participants 7 messages Sep 7, 2015

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 efi: introduce efi_arch_flush_dcache_area

Objects loaded by FileHandle->Read need to be flushed to dcache, otherwise copy_from_paddr will read stale data when copying the kernel, causing a failure to boot.

Introduce efi_arch_flush_dcache_area and call it from read_file.

This commit introduces no functional changes on x86.

---
 xen/arch/arm/efi/efi-boot.h |    7 +++++++
 xen/arch/x86/efi/efi-boot.h |    2 ++
 xen/common/efi/boot.c       |    3 ++-
 3 files changed, 11 insertions(+), 1 deletion(-)
xen/arch/arm/efi/efi-boot.h +7 -0
index 6a12d91..fd79658 100644
@@ -9,6 +9,7 @@
 #include <asm/smp.h>
 
 void noreturn efi_xen_start(void *fdt_ptr, uint32_t fdt_size);
+void __flush_dcache_area(const void * vaddr, unsigned long size);
 
 #define DEVICE_TREE_GUID \
 {0xb1b621d5, 0xf19c, 0x41a5, {0x83, 0x0b, 0xd9, 0x15, 0x2c, 0x69, 0xaa, 0xe0}}
@@ -571,6 +572,12 @@ static void __init efi_arch_video_init(EFI_GRAPHICS_OUTPUT_PROTOCOL *gop,
                                        EFI_GRAPHICS_OUTPUT_MODE_INFORMATION *mode_info)
 {
 }
+
+static void efi_arch_flush_dcache_area(const void *vaddr, UINTN size)
+{
+    __flush_dcache_area(vaddr, size);
+}
+
 /*
  * Local variables:
  * mode: C
xen/arch/x86/efi/efi-boot.h +2 -0
index 2dd69f6..4c7f383 100644
@@ -640,6 +640,8 @@ static bool_t __init efi_arch_use_config_file(EFI_SYSTEM_TABLE *SystemTable)
     return 1; /* x86 always uses a config file */
 }
 
+static void efi_arch_flush_dcache_area(const void *vaddr, UINTN size) { }
+
 /*
  * Local variables:
  * mode: C
xen/common/efi/boot.c +2 -1
index 75a939f..f0f26c1 100644
@@ -526,7 +526,8 @@ static bool_t __init read_file(EFI_FILE_HANDLE dir_handle, CHAR16 *name,
         PrintErr(what);
         PrintErr(L" failed for ");
         PrintErrMesg(name, ret);
-    }
+    } else
+        efi_arch_flush_dcache_area(file->ptr, file->size);
 
     return 1;
 }
Signature · 1 line
1.7.10.4
Acked-by on v1 1/1
#1 lore raw
Quoted 6 lines · Stefano Stabellini

Objects loaded by FileHandle->Read need to be flushed to dcache, otherwise copy_from_paddr will read stale data when copying the kernel, causing a failure to boot.

Introduce efi_arch_flush_dcache_area and call it from read_file.

Quoted 7 lines

This commit introduces no functional changes on x86.

#2 lore raw
In reply to Stefano Stabellini
Quoted 4 lines
Quoted 1 line
Quoted 1 line · On 07.09.15 at 16:13, <[email protected]> wrote:

Objects loaded by FileHandle->Read need to be flushed to dcache, otherwise copy_from_paddr will read stale data when copying the kernel, causing a failure to boot.

I have to admit that I'm a little puzzled by this description: If this was a general requirement (which is how it reads to me), why does ->Read() not do the necessary flushing? Or if it's not a general requirement, perhaps the description could be changed to make clear what exact dependency exists that does not exist for all users of ->Read()?

xen/arch/arm/efi/efi-boot.h line 12

Quoted 7 lines
xen/arch/arm/efi/efi-boot.h +1 -0
@@ -9,6 +9,7 @@
 #include <asm/smp.h>
 
 void noreturn efi_xen_start(void *fdt_ptr, uint32_t fdt_size);
+void __flush_dcache_area(const void * vaddr, unsigned long size);

While I see Ian ack-ed this, I find it wrong to replicate a declaration here that now can get out of sync with the canonical one at any point in time, without anyone noticing at build time. Or wait - this looks to be a boot time only interface that so far didn't even have a C declaration. That's fine then except for the minor coding style issue (stray blank between "*" and "vaddr").

xen/common/efi/boot.c static bool_t __init read_file(EFI_FILE_HANDLE dir_handle, CHAR16 *name, · line 530

Quoted 9 lines
xen/common/efi/boot.c +2 -1
@@ -526,7 +526,8 @@ static bool_t __init read_file(EFI_FILE_HANDLE dir_handle, CHAR16 *name,
         PrintErr(what);
         PrintErr(L" failed for ");
         PrintErrMesg(name, ret);
-    }
+    } else
+        efi_arch_flush_dcache_area(file->ptr, file->size);

No need for the (misplaced) "else" - PrintErrMesg() does not return.

Jan

#3 lore raw
Quoted 7 lines · Jan Beulich
Quoted 4 lines
Quoted 1 line
Quoted 1 line · On 07.09.15 at 16:13, <[email protected]> wrote:

Objects loaded by FileHandle->Read need to be flushed to dcache, otherwise copy_from_paddr will read stale data when copying the kernel, causing a failure to boot.

I have to admit that I'm a little puzzled by this description: If this was a general requirement (which is how it reads to me)

Yes, it is

Quoted 4 lines

why does > ->Read() not do the necessary flushing? Or if it's not a general requirement, perhaps the description could be changed to make clear what exact dependency exists that does not exist for all users of ->Read()?

It is a general requirement: anything that could be accessed without a cacheable mapping needs to be flushed. Unfortunately the UEFI spec is not clear enough on who should do the flushing on what, and in fact the Forum is working on improving it

Quoted 5 lines
Quoted 5 lines
xen/arch/arm/efi/efi-boot.h +0 -0
@@ -9,6 +9,7 @@
 #include <asm/smp.h>
 
Quoted 8 lines

xen/arch/arm/efi/efi-boot.h line 12

Quoted 2 lines
xen/arch/arm/efi/efi-boot.h +1 -0
 void noreturn efi_xen_start(void *fdt_ptr, uint32_t fdt_size);
+void __flush_dcache_area(const void * vaddr, unsigned long size);

While I see Ian ack-ed this, I find it wrong to replicate a declaration here that now can get out of sync with the canonical one at any point in time, without anyone noticing at build time. Or wait - this looks to be a boot time only interface that so far didn't even have a C declaration.

That's right

Quoted 2 lines

That's fine then except for the minor coding style issue (stray blank between "*" and "vaddr").

Thanks for spotting that

Quoted 11 lines

xen/common/efi/boot.c static bool_t __init read_file(EFI_FILE_HANDLE dir_handle, CHAR16 *name, · line 530

Quoted 9 lines
xen/common/efi/boot.c +2 -1
@@ -526,7 +526,8 @@ static bool_t __init read_file(EFI_FILE_HANDLE dir_handle, CHAR16 *name,
         PrintErr(what);
         PrintErr(L" failed for ");
         PrintErrMesg(name, ret);
-    }
+    } else
+        efi_arch_flush_dcache_area(file->ptr, file->size);

No need for the (misplaced) "else" - PrintErrMesg() does not return.

Ah! Thanks!

#4 lore raw
Quoted 18 lines · Stefano Stabellini
Quoted 8 lines · Jan Beulich
Quoted 5 lines
Quoted 1 line
Quoted 1 line · On 07.09.15 at 16:13, <[email protected]> wrote:

Objects loaded by FileHandle->Read need to be flushed to dcache, otherwise copy_from_paddr will read stale data when copying the kernel,
causing a failure to boot.

I have to admit that I'm a little puzzled by this description: If this was a general requirement (which is how it reads to me)

Yes, it is

Quoted 4 lines

why does > ->Read() not do the necessary flushing? Or if it's not a general requirement, perhaps the description could be changed to make clear what exact dependency exists that does not exist for all users of ->Read()?

Quoted 4 lines

It is a general requirement: anything that could be accessed without a cacheable mapping needs to be flushed. Unfortunately the UEFI spec is not clear enough on who should do the flushing on what, and in fact the Forum is working on improving it

I might be wrong and/or misremembering but I think this stems partly from the fact that the ARM UEFI stub in Xen needs to turn off caches (and paging?) before jumping to the usual Xen entry point.

Then when caches come back on you get inconsistencies because of stale stuff in the cache which suddenly reappears etc.

Ideally we would flush the whole cache when we disable the cache, but the mechanism for doing so for the whole cache (by set/way) is not architecturally required to be snooped by external cache controllers. Only flush by VA is required to be snooped but that would take a very long time on a system of any size. At this level Xen is not yet aware of any external cache controllers to flush them explicitly etc, so we are a bit stuck and end up having to flush each thing we load by VA.

I'm not sure but an alternative could be to make ARM's head.S work regardless of the cache being enabled or disabled. I don't know if that is practical though.

Ian.

Quoted 39 lines
Quoted 7 lines
Quoted 7 lines
xen/arch/arm/efi/efi-boot.h +0 -0
@@ -9,6 +9,7 @@
 #include <asm/smp.h>
 
Quoted 8 lines

xen/arch/arm/efi/efi-boot.h line 12

Quoted 2 lines
xen/arch/arm/efi/efi-boot.h +1 -0
 void noreturn efi_xen_start(void *fdt_ptr, uint32_t fdt_size);
+void __flush_dcache_area(const void * vaddr, unsigned long size);

While I see Ian ack-ed this, I find it wrong to replicate a declaration here that now can get out of sync with the canonical one at any point in time, without anyone noticing at build time. Or wait - this looks to be a boot time only interface that so far didn't even have a C declaration.

That's right

Quoted 2 lines

That's fine then except for the minor coding style issue (stray blank between "*" and "vaddr").

Thanks for spotting that

Quoted 4 lines
Quoted 4 lines
xen/common/efi/boot.c +0 -0
@@ -526,7 +526,8 @@ static bool_t __init read_file(EFI_FILE_HANDLE 
 dir_handle, CHAR16 *name,
Quoted 8 lines

xen/common/efi/boot.c static bool_t __init read_file(EFI_FILE_HANDLE dir_handle, CHAR16 *name, · line 530

Quoted 6 lines
xen/common/efi/boot.c +2 -1
         PrintErr(what);
         PrintErr(L" failed for ");
         PrintErrMesg(name, ret);
-    }
+    } else
+        efi_arch_flush_dcache_area(file->ptr, file->size);

No need for the (misplaced) "else" - PrintErrMesg() does not return.

Ah! Thanks!

#5 lore raw
Quoted 24 lines · Ian Campbell
Quoted 18 lines · Stefano Stabellini
Quoted 8 lines · Jan Beulich
Quoted 5 lines
Quoted 1 line
Quoted 1 line · On 07.09.15 at 16:13, <[email protected]> wrote:

Objects loaded by FileHandle->Read need to be flushed to dcache, otherwise copy_from_paddr will read stale data when copying the kernel,
causing a failure to boot.

I have to admit that I'm a little puzzled by this description: If this was a general requirement (which is how it reads to me)

Yes, it is

Quoted 4 lines

why does > ->Read() not do the necessary flushing? Or if it's not a general requirement, perhaps the description could be changed to make clear what exact dependency exists that does not exist for all users of ->Read()?

Quoted 4 lines

It is a general requirement: anything that could be accessed without a cacheable mapping needs to be flushed. Unfortunately the UEFI spec is not clear enough on who should do the flushing on what, and in fact the Forum is working on improving it

Quoted 6 lines

I might be wrong and/or misremembering but I think this stems partly from the fact that the ARM UEFI stub in Xen needs to turn off caches (and paging?) before jumping to the usual Xen entry point.

Then when caches come back on you get inconsistencies because of stale stuff in the cache which suddenly reappears etc.

It's more to do with using inconsistent attributes than about whether the caches are enabled -- at both points the kernel image is manipulated, SCTLR_EL2.{C,M} == {1,1}.

EFI_FILE_PROTOCOL.Read() may place data into the caches without updating memory because of coherent IO or simply because it copies to a cacheable alias. If Xen were to modify the image in any way (e.g. decompressing it), it would only update the cached copy.

Later kernel_zimage_load calls copy_from_paddr, which uses a non-cacheable alias, bypassing the caches and going straight to memory.

Even if the two aliases were in use simultaneously, they wouldn't be coherent with each other.

For more background, Marc Zyngier did a good talk at KVM Forum regarding the usual behaviour of the caches [1,2].

Mark.

[1] http://events.linuxfoundation.org/sites/events/files/slides/slides_10.pdf
[2] https://www.youtube.com/watch?v=A_zCxzpxzmE

#6 lore raw
In reply to Stefano Stabellini
Quoted 6 lines · Stefano Stabellini

Objects loaded by FileHandle->Read need to be flushed to dcache, otherwise copy_from_paddr will read stale data when copying the kernel, causing a failure to boot.

Introduce efi_arch_flush_dcache_area and call it from read_file.

Quoted 7 lines

This commit introduces no functional changes on x86.

I'm expecting a v2 with minor adjustments.

In any case this is a valid and straightforward bug fix:

I will wait for v2 and keep an eye on our testing progress. This will probably go in after RC3 (i.e. after branching) unless OSSTest is wedged again and we somehow need to retest staging.

Wei.