Skip to contentSkip to conversation

x86/setup: properly update PTEs if src/dst overlaps when relocating Xen image

PATCH Superseded Daniel Kiper 2 participants 5 messages Apr 19, 2018 – Apr 20, 2018

v6 of 7 Superseded Latest is v7 →

lore
Share & tools
lore
#0 lore raw

PATCH v6 1/1 x86/setup: properly update PTEs if src/dst overlaps when relocating Xen image

Commit 0d31d16 (x86/setup: do not relocate Xen over current Xen image placement) disallowed src/dst images overlaps when relocating Xen image. Though it deliberately allowed destination region between __image_base__ and (__image_base__ + XEN_IMG_OFFSET) overlaps with the end of source image. And here is the problem. If anything between __page_tables_start and __page_tables_end in source image lands in the overlap then some or even all page table entries may not be updated. This usually means boom in early boot which will be difficult to the investigate. So, I think that we have three choices to fix the issue:
- drop XEN_IMG_OFFSET from

    if ( (end > s) && (end - reloc_size + XEN_IMG_OFFSET >= __pa(_end)) )

- add XEN_IMG_OFFSET to xen_phys_start in PFN_DOWN(xen_phys_start) used in loops as one of conditions and replace ">" with ">=",
- change PFN_DOWN(xen_phys_start) to PFN_DOWN(xen_remap_end_pfn) proposed in earlier version of this patch.

This patch implements the second option. This way we still allow source and destination partial overlap as described above but PTEs are properly updated now.

---
 xen/arch/x86/setup.c |   16 +++++++++++++---
 1 file changed, 13 insertions(+), 3 deletions(-)
xen/arch/x86/setup.c +13 -3
index b2baee3..f034a31 100644
@@ -1012,6 +1012,7 @@ void __init noreturn __start_xen(unsigned long mbi_p)
             l3_pgentry_t *pl3e;
             l2_pgentry_t *pl2e;
             int i, j, k;
+            unsigned long pte_update_limit;
 
             /* Select relocation address. */
             e = end - reloc_size;
@@ -1019,6 +1020,15 @@ void __init noreturn __start_xen(unsigned long mbi_p)
             bootsym(trampoline_xen_phys_start) = e;
 
             /*
+             * All PTEs pointing above that address are not candidates for
+             * relocation. Due to possibility of partial overlap of the end
+             * of source image and the beginning of region for destination
+             * image some PTEs may point to addresses in
+             * range [e, e + XEN_IMG_OFFSET).
+             */
+            pte_update_limit = PFN_DOWN(e + XEN_IMG_OFFSET);
+
+            /*
              * Perform relocation to new physical address.
              * Before doing so we must sync static/global data with main memory
              * with a barrier(). After this we must *not* modify static/global
@@ -1041,7 +1051,7 @@ void __init noreturn __start_xen(unsigned long mbi_p)
                     /* Not present, 1GB mapping, or already relocated? */
                     if ( !(l3e_get_flags(*pl3e) & _PAGE_PRESENT) ||
                          (l3e_get_flags(*pl3e) & _PAGE_PSE) ||
-                         (l3e_get_pfn(*pl3e) > PFN_DOWN(xen_phys_start)) )
+                         (l3e_get_pfn(*pl3e) >= pte_update_limit) )
                         continue;
                     *pl3e = l3e_from_intpte(l3e_get_intpte(*pl3e) +
                                             xen_phys_start);
@@ -1051,7 +1061,7 @@ void __init noreturn __start_xen(unsigned long mbi_p)
                         /* Not present, PSE, or already relocated? */
                         if ( !(l2e_get_flags(*pl2e) & _PAGE_PRESENT) ||
                              (l2e_get_flags(*pl2e) & _PAGE_PSE) ||
-                             (l2e_get_pfn(*pl2e) > PFN_DOWN(xen_phys_start)) )
+                             (l2e_get_pfn(*pl2e) >= pte_update_limit) )
                             continue;
                         *pl2e = l2e_from_intpte(l2e_get_intpte(*pl2e) +
                                                 xen_phys_start);
@@ -1075,7 +1085,7 @@ void __init noreturn __start_xen(unsigned long mbi_p)
                 unsigned int flags;
 
                 if ( !(l2e_get_flags(*pl2e) & _PAGE_PRESENT) ||
-                     (l2e_get_pfn(*pl2e) > PFN_DOWN(xen_phys_start)) )
+                     (l2e_get_pfn(*pl2e) >= pte_update_limit) )
                     continue;
 
                 if ( !using_2M_mapping() )
Signature · 7 lines
1.7.10.4


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Reviewed-by on v6 1/1
#1 lore raw
Quoted 17 lines
Quoted 1 line
Quoted 1 line · On 19.04.18 at 16:28, <[email protected]> wrote:

Commit 0d31d16 (x86/setup: do not relocate Xen over current Xen image placement) disallowed src/dst images overlaps when relocating Xen image. Though it deliberately allowed destination region between __image_base__ and (__image_base__ + XEN_IMG_OFFSET) overlaps with the end of source image. And here is the problem. If anything between __page_tables_start and __page_tables_end in source image lands in the overlap then some or even all page table entries may not be updated. This usually means boom in early boot which will be difficult to the investigate. So, I think that we have three choices to fix the issue:
- drop XEN_IMG_OFFSET from

    if ( (end > s) && (end - reloc_size + XEN_IMG_OFFSET >= __pa(_end)) )

- add XEN_IMG_OFFSET to xen_phys_start in PFN_DOWN(xen_phys_start) used in loops as one of conditions and replace ">" with ">=",
- change PFN_DOWN(xen_phys_start) to PFN_DOWN(xen_remap_end_pfn) proposed in earlier version of this patch.

Quoted 5 lines

This patch implements the second option. This way we still allow source and destination partial overlap as described above but PTEs are properly updated now.

and queuing for after 4.11.

One further small suggestion though:

xen/arch/x86/setup.c void __init noreturn __start_xen(unsigned long mbi_p) · line 1029

Quoted 11 lines
xen/arch/x86/setup.c +7 -0
@@ -1019,6 +1020,15 @@ void __init noreturn __start_xen(unsigned long mbi_p)
             bootsym(trampoline_xen_phys_start) = e;
 
             /*
+             * All PTEs pointing above that address are not candidates for
+             * relocation. Due to possibility of partial overlap of the end
+             * of source image and the beginning of region for destination
+             * image some PTEs may point to addresses in
+             * range [e, e + XEN_IMG_OFFSET).
+             */
+            pte_update_limit = PFN_DOWN(e + XEN_IMG_OFFSET);

Perhaps better start with "No PTEs pointing above this address are candidates ..."?

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#2 lore raw
Quoted 17 lines · Jan Beulich
Quoted 17 lines
Quoted 1 line
Quoted 1 line · On 19.04.18 at 16:28, <[email protected]> wrote:

Commit 0d31d16 (x86/setup: do not relocate Xen over current Xen image placement) disallowed src/dst images overlaps when relocating Xen image. Though it deliberately allowed destination region between __image_base__ and (__image_base__ + XEN_IMG_OFFSET) overlaps with the end of source image. And here is the problem. If anything between __page_tables_start and __page_tables_end in source image lands in the overlap then some or even all page table entries may not be updated. This usually means boom in early boot which will be difficult to the investigate. So, I think that we have three choices to fix the issue:
- drop XEN_IMG_OFFSET from

    if ( (end > s) && (end - reloc_size + XEN_IMG_OFFSET >= __pa(_end)) )

- add XEN_IMG_OFFSET to xen_phys_start in PFN_DOWN(xen_phys_start) used in loops as one of conditions and replace ">" with ">=",
- change PFN_DOWN(xen_phys_start) to PFN_DOWN(xen_remap_end_pfn) proposed in earlier version of this patch.

Quoted 8 lines
Quoted 5 lines

This patch implements the second option. This way we still allow source and destination partial overlap as described above but PTEs are properly updated now.

and queuing for after 4.11.

Thank you.

Quoted 5 lines

One further small suggestion though:

Quoted 3 lines
xen/arch/x86/setup.c +0 -0
@@ -1019,6 +1020,15 @@ void __init noreturn __start_xen(unsigned long mbi_p)
             bootsym(trampoline_xen_phys_start) = e;
 
Quoted 11 lines

xen/arch/x86/setup.c void __init noreturn __start_xen(unsigned long mbi_p) · line 1029

Quoted 8 lines
xen/arch/x86/setup.c +7 -0
             /*
+             * All PTEs pointing above that address are not candidates for
+             * relocation. Due to possibility of partial overlap of the end
+             * of source image and the beginning of region for destination
+             * image some PTEs may point to addresses in
+             * range [e, e + XEN_IMG_OFFSET).
+             */
+            pte_update_limit = PFN_DOWN(e + XEN_IMG_OFFSET);

Perhaps better start with "No PTEs pointing above this address are candidates ..."?

I am OK with that change. Should I post v7 or are you going to update the comment before committing? Both works for me.

Daniel

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#3 lore raw
Quoted 18 lines
Quoted 1 line
Quoted 1 line · On 20.04.18 at 11:15, <[email protected]> wrote:
Quoted 4 lines · Jan Beulich
Quoted 4 lines
Quoted 1 line
Quoted 1 line · On 19.04.18 at 16:28, <[email protected]> wrote:
xen/arch/x86/setup.c +0 -0
@@ -1019,6 +1020,15 @@ void __init noreturn __start_xen(unsigned long mbi_p)
             bootsym(trampoline_xen_phys_start) = e;
 
Quoted 11 lines

xen/arch/x86/setup.c void __init noreturn __start_xen(unsigned long mbi_p) · line 1029

Quoted 8 lines
xen/arch/x86/setup.c +7 -0
             /*
+             * All PTEs pointing above that address are not candidates for
+             * relocation. Due to possibility of partial overlap of the end
+             * of source image and the beginning of region for destination
+             * image some PTEs may point to addresses in
+             * range [e, e + XEN_IMG_OFFSET).
+             */
+            pte_update_limit = PFN_DOWN(e + XEN_IMG_OFFSET);

Perhaps better start with "No PTEs pointing above this address are candidates ..."?

Quoted 2 lines

I am OK with that change. Should I post v7 or are you going to update the comment before committing? Both works for me.

I've recorded a note to that effect, but I certainly wouldn't mind if I didn't have to edit the patch before committing.

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#4 lore raw
Quoted 18 lines · Jan Beulich
Quoted 18 lines
Quoted 1 line
Quoted 1 line · On 20.04.18 at 11:15, <[email protected]> wrote:
Quoted 15 lines · Jan Beulich

xen/arch/x86/setup.c void __init noreturn __start_xen(unsigned long mbi_p) · line 1029

Quoted 12 lines
Quoted 1 line
Quoted 1 line · On 19.04.18 at 16:28, <[email protected]> wrote:
xen/arch/x86/setup.c +7 -0
@@ -1019,6 +1020,15 @@ void __init noreturn __start_xen(unsigned long mbi_p)
             bootsym(trampoline_xen_phys_start) = e;
 
             /*
+             * All PTEs pointing above that address are not candidates for
+             * relocation. Due to possibility of partial overlap of the end
+             * of source image and the beginning of region for destination
+             * image some PTEs may point to addresses in
+             * range [e, e + XEN_IMG_OFFSET).
+             */
+            pte_update_limit = PFN_DOWN(e + XEN_IMG_OFFSET);

Perhaps better start with "No PTEs pointing above this address are candidates ..."?

Quoted 5 lines
Quoted 2 lines

I am OK with that change. Should I post v7 or are you going to update the comment before committing? Both works for me.

I've recorded a note to that effect, but I certainly wouldn't mind if I didn't have to edit the patch before committing.

I have just sent v7 for your convenience.

Daniel

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel