Skip to contentSkip to conversation

x86/boot: Force error checking for reserve_e820_ram()

PATCH In review Andrew Cooper 3 participants 5 messages Apr 17 – Apr 20

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 x86/boot: Force error checking for reserve_e820_ram()

Failing to mark Xen as Reserved in the E820 is catastrophic; RAM regions get handed to the physical memory allocator for general use. Similarly, failure to mark the boot modules as reserved is not going to result in a working system.

Mark reserve_e820_ram() as __must_check, and panic() on failure. To avoid opencoding the range in every caller, print a general failure message in reserve_e820_ram().

---
CC: Jan Beulich <[email protected]>
CC: Roger Pau Monné <[email protected]>
CC: Teddy Astie <[email protected]>

Slightly RFC; only compile tested so far.

There's no obvious fixes tag. This has been many variations of broken since forever.

---
 xen/arch/x86/e820.c             |  8 +++++++-
 xen/arch/x86/include/asm/e820.h |  2 +-
 xen/arch/x86/setup.c            | 11 +++++++----
 3 files changed, 15 insertions(+), 6 deletions(-)
xen/arch/x86/e820.c +7 -1
index 872208ab3722..f09a01f0c50a 100644
@@ -670,7 +670,13 @@ int __init e820_change_range_type(
 /* Set E820_RAM area (@s,@e) as RESERVED in specified e820 map. */
 int __init reserve_e820_ram(struct e820map *map, uint64_t s, uint64_t e)
 {
-    return e820_change_range_type(map, s, e, E820_RAM, E820_RESERVED);
+    int res = e820_change_range_type(map, s, e, E820_RAM, E820_RESERVED);
+
+    if ( !res )
+        printk("Failed to convert E820 RAM %"PRIx64"-%"PRIx64" to RESERVED\n",
+               s, e);
+
+    return res;
 }
 
 unsigned long __init init_e820(const char *str, struct e820map *raw)
xen/arch/x86/include/asm/e820.h +1 -1
index 8e7644f8870b..a86d60ce3e77 100644
@@ -25,7 +25,7 @@ struct e820map {
 
 extern int sanitize_e820_map(struct e820entry *biosmap, unsigned int *pnr_map);
 extern int e820_all_mapped(u64 start, u64 end, unsigned type);
-extern int reserve_e820_ram(struct e820map *map, uint64_t s, uint64_t e);
+extern int __must_check reserve_e820_ram(struct e820map *map, uint64_t s, uint64_t e);
 extern int e820_change_range_type(
     struct e820map *map, uint64_t s, uint64_t e,
     uint32_t orig_type, uint32_t new_type);
xen/arch/x86/setup.c +7 -4
index d041cbd5f6f1..9c1f1eafa0d7 100644
@@ -1639,7 +1639,8 @@ void asmlinkage __init noreturn __start_xen(void)
     {
         uint64_t s = bi->mods[i].start, l = bi->mods[i].size;
 
-        reserve_e820_ram(&boot_e820, s, s + PAGE_ALIGN(l));
+        if ( !reserve_e820_ram(&boot_e820, s, s + PAGE_ALIGN(l)) )
+            panic("Failed to reserve boot module %u in E820\n", i);
     }
 
     if ( !xen_phys_start )
@@ -1652,11 +1653,13 @@ void asmlinkage __init noreturn __start_xen(void)
     /* This needs to remain in sync with remove_xen_ranges(). */
     if ( efi_boot_mem_unused(&eb_start, &eb_end) )
     {
-        reserve_e820_ram(&boot_e820, __pa(_stext), __pa(eb_start));
-        reserve_e820_ram(&boot_e820, __pa(eb_end), __pa(__2M_rwdata_end));
+        if ( !reserve_e820_ram(&boot_e820, __pa(_stext), __pa(eb_start)) ||
+             !reserve_e820_ram(&boot_e820, __pa(eb_end), __pa(__2M_rwdata_end)) )
+            panic("Failed to reserve Xen in E820\n");
     }
     else
-        reserve_e820_ram(&boot_e820, __pa(_stext), __pa(__2M_rwdata_end));
+        if ( reserve_e820_ram(&boot_e820, __pa(_stext), __pa(__2M_rwdata_end)) )
+            panic("Failed to reserve Xen in E820\n");
 
     /* Late kexec reservation (dynamic start address). */
     kexec_reserve_area();

base-commit: 986707b461eb56d75f55581dd1c8e2633f814795

Signature · 1 line
2.39.5
#1 lore raw
Quoted 77 lines · Andrew Cooper

Failing to mark Xen as Reserved in the E820 is catastrophic; RAM regions get handed to the physical memory allocator for general use. Similarly, failure to mark the boot modules as reserved is not going to result in a working system.

Mark reserve_e820_ram() as __must_check, and panic() on failure. To avoid opencoding the range in every caller, print a general failure message in reserve_e820_ram().

---
CC: Jan Beulich <[email protected]>
CC: Roger Pau Monné <[email protected]>
CC: Teddy Astie <[email protected]>

Slightly RFC; only compile tested so far.

There's no obvious fixes tag. This has been many variations of broken since forever.

---
  xen/arch/x86/e820.c             |  8 +++++++-
  xen/arch/x86/include/asm/e820.h |  2 +-
  xen/arch/x86/setup.c            | 11 +++++++----
  3 files changed, 15 insertions(+), 6 deletions(-)
xen/arch/x86/e820.c +7 -1
index 872208ab3722..f09a01f0c50a 100644
@@ -670,7 +670,13 @@ int __init e820_change_range_type(
  /* Set E820_RAM area (@s,@e) as RESERVED in specified e820 map. */
  int __init reserve_e820_ram(struct e820map *map, uint64_t s, uint64_t e)
  {
-    return e820_change_range_type(map, s, e, E820_RAM, E820_RESERVED);
+    int res = e820_change_range_type(map, s, e, E820_RAM, E820_RESERVED);
+
+    if ( !res )
+        printk("Failed to convert E820 RAM %"PRIx64"-%"PRIx64" to RESERVED\n",
+               s, e);
+
+    return res;
  }
 
  unsigned long __init init_e820(const char *str, struct e820map *raw)
xen/arch/x86/include/asm/e820.h +1 -1
index 8e7644f8870b..a86d60ce3e77 100644
@@ -25,7 +25,7 @@ struct e820map {
 
  extern int sanitize_e820_map(struct e820entry *biosmap, unsigned int *pnr_map);
  extern int e820_all_mapped(u64 start, u64 end, unsigned type);
-extern int reserve_e820_ram(struct e820map *map, uint64_t s, uint64_t e);
+extern int __must_check reserve_e820_ram(struct e820map *map, uint64_t s, uint64_t e);
  extern int e820_change_range_type(
      struct e820map *map, uint64_t s, uint64_t e,
      uint32_t orig_type, uint32_t new_type);
xen/arch/x86/setup.c +2 -2
index d041cbd5f6f1..9c1f1eafa0d7 100644
@@ -1639,7 +1639,8 @@ void asmlinkage __init noreturn __start_xen(void)
      {
          uint64_t s = bi->mods[i].start, l = bi->mods[i].size;
 
-        reserve_e820_ram(&boot_e820, s, s + PAGE_ALIGN(l));
+        if ( !reserve_e820_ram(&boot_e820, s, s + PAGE_ALIGN(l)) )
+            panic("Failed to reserve boot module %u in E820\n", i);
      }
 
      if ( !xen_phys_start )
@@ -1652,11 +1653,13 @@ void asmlinkage __init noreturn __start_xen(void)
      /* This needs to remain in sync with remove_xen_ranges(). */
      if ( efi_boot_mem_unused(&eb_start, &eb_end) )
      {
-        reserve_e820_ram(&boot_e820, __pa(_stext), __pa(eb_start));

xen/arch/x86/setup.c void asmlinkage __init noreturn __start_xen(void) · line 1663

Quoted 10 lines
xen/arch/x86/setup.c +5 -2
-        reserve_e820_ram(&boot_e820, __pa(eb_end), __pa(__2M_rwdata_end));
+        if ( !reserve_e820_ram(&boot_e820, __pa(_stext), __pa(eb_start)) ||
+             !reserve_e820_ram(&boot_e820, __pa(eb_end), __pa(__2M_rwdata_end)) )
+            panic("Failed to reserve Xen in E820\n");
      }
      else
-        reserve_e820_ram(&boot_e820, __pa(_stext), __pa(__2M_rwdata_end));
+        if ( reserve_e820_ram(&boot_e820, __pa(_stext), __pa(__2M_rwdata_end)) )
+            panic("Failed to reserve Xen in E820\n");
 

This condition is inverted.
Otherwise this looks like a sensible change.

Ross

#2 lore raw
Quoted 96 lines · Ross Lagerwall
Quoted 95 lines · Andrew Cooper

Failing to mark Xen as Reserved in the E820 is catastrophic; RAM regions get
handed to the physical memory allocator for general use.  Similarly, failure
to mark the boot modules as reserved is not going to result in a working system.

Mark reserve_e820_ram() as __must_check, and panic() on failure.  To avoid
opencoding the range in every caller, print a general failure message in reserve_e820_ram().

---
CC: Jan Beulich <[email protected]>
CC: Roger Pau Monné <[email protected]>
CC: Teddy Astie <[email protected]>

Slightly RFC; only compile tested so far.

There's no obvious fixes tag.  This has been many variations of broken since
forever.
---
  xen/arch/x86/e820.c             |  8 +++++++- xen/arch/x86/include/asm/e820.h |  2 +- xen/arch/x86/setup.c            | 11 +++++++----

  3 files changed, 15 insertions(+), 6 deletions(-)
xen/arch/x86/e820.c +7 -1
index 872208ab3722..f09a01f0c50a 100644
@@ -670,7 +670,13 @@ int __init e820_change_range_type(
   /* Set E820_RAM area (@s,@e) as RESERVED in specified e820 map. */
   int __init reserve_e820_ram(struct e820map *map, uint64_t s,
 uint64_t e)
   {
-    return e820_change_range_type(map, s, e, E820_RAM, E820_RESERVED);
+    int res = e820_change_range_type(map, s, e, E820_RAM,
 E820_RESERVED);
+
+    if ( !res )
+        printk("Failed to convert E820 RAM %"PRIx64"-%"PRIx64" to
 RESERVED\n",
+               s, e);
+
+    return res;

  }
    unsigned long __init init_e820(const char *str, struct e820map *raw)

xen/arch/x86/include/asm/e820.h +0 -0
index 8e7644f8870b..a86d60ce3e77 100644
@@ -25,7 +25,7 @@ struct e820map {

    extern int sanitize_e820_map(struct e820entry *biosmap, unsigned int *pnr_map);
  extern int e820_all_mapped(u64 start, u64 end, unsigned type); -extern int reserve_e820_ram(struct e820map *map, uint64_t s, uint64_t e);
+extern int __must_check reserve_e820_ram(struct e820map *map, uint64_t s, uint64_t e);
  extern int e820_change_range_type(
      struct e820map *map, uint64_t s, uint64_t e, uint32_t orig_type, uint32_t new_type);

xen/arch/x86/setup.c +3 -2
index d041cbd5f6f1..9c1f1eafa0d7 100644
@@ -1639,7 +1639,8 @@ void asmlinkage __init noreturn __start_xen(void)
       {
           uint64_t s = bi->mods[i].start, l = bi->mods[i].size;
   -        reserve_e820_ram(&boot_e820, s, s + PAGE_ALIGN(l));
+        if ( !reserve_e820_ram(&boot_e820, s, s + PAGE_ALIGN(l)) )
+            panic("Failed to reserve boot module %u in E820\n", i);
       }
         if ( !xen_phys_start )
@@ -1652,11 +1653,13 @@ void asmlinkage __init noreturn
 __start_xen(void)
       /* This needs to remain in sync with remove_xen_ranges(). */
       if ( efi_boot_mem_unused(&eb_start, &eb_end) )
       {
-        reserve_e820_ram(&boot_e820, __pa(_stext), __pa(eb_start));
-        reserve_e820_ram(&boot_e820, __pa(eb_end),
 __pa(__2M_rwdata_end));
+        if ( !reserve_e820_ram(&boot_e820, __pa(_stext),

__pa(eb_start)) ||
+             !reserve_e820_ram(&boot_e820, __pa(eb_end), __pa(__2M_rwdata_end)) )
+            panic("Failed to reserve Xen in E820\n"); }
      else
-        reserve_e820_ram(&boot_e820, __pa(_stext),

Quoted 8 lines
Quoted 5 lines

__pa(__2M_rwdata_end));
+        if ( reserve_e820_ram(&boot_e820, __pa(_stext), __pa(__2M_rwdata_end)) )
+            panic("Failed to reserve Xen in E820\n");

This condition is inverted.
Otherwise this looks like a sensible change.

Oops, yes.  Fixed.

This is a horrible function of type int but returning a boolean success.  Still, I'm not altering the error scheme in the same patch as this fix.

~Andrew

Reviewed-by
#3 lore raw
Quoted 71 lines · Andrew Cooper
Quoted 70 lines · Ross Lagerwall
Quoted 69 lines · Andrew Cooper

Failing to mark Xen as Reserved in the E820 is catastrophic; RAM regions get
handed to the physical memory allocator for general use. Similarly, failure
to mark the boot modules as reserved is not going to result in a working system.

Mark reserve_e820_ram() as __must_check, and panic() on failure. To avoid
opencoding the range in every caller, print a general failure message in reserve_e820_ram().

---
CC: Jan Beulich <[email protected]>
CC: Roger Pau Monné <[email protected]>
CC: Teddy Astie <[email protected]>

Slightly RFC; only compile tested so far.

There's no obvious fixes tag. This has been many variations of broken since
forever.

---
   xen/arch/x86/e820.c             |  8 +++++++-
   xen/arch/x86/include/asm/e820.h |  2 +-
   xen/arch/x86/setup.c            | 11 +++++++----
   3 files changed, 15 insertions(+), 6 deletions(-)
xen/arch/x86/e820.c +7 -1
index 872208ab3722..f09a01f0c50a 100644
@@ -670,7 +670,13 @@ int __init e820_change_range_type(
   /* Set E820_RAM area (@s,@e) as RESERVED in specified e820 map. */
   int __init reserve_e820_ram(struct e820map *map, uint64_t s,
 uint64_t e)
   {
-    return e820_change_range_type(map, s, e, E820_RAM, E820_RESERVED);
+    int res = e820_change_range_type(map, s, e, E820_RAM,
 E820_RESERVED);
+
+    if ( !res )
+        printk("Failed to convert E820 RAM %"PRIx64"-%"PRIx64" to
 RESERVED\n",
+               s, e);
+
+    return res;
   }
     unsigned long __init init_e820(const char *str, struct e820map *raw)
xen/arch/x86/include/asm/e820.h +1 -1
index 8e7644f8870b..a86d60ce3e77 100644
@@ -25,7 +25,7 @@ struct e820map {
     extern int sanitize_e820_map(struct e820entry *biosmap, unsigned
 int *pnr_map);
   extern int e820_all_mapped(u64 start, u64 end, unsigned type);
-extern int reserve_e820_ram(struct e820map *map, uint64_t s,
 uint64_t e);
+extern int __must_check reserve_e820_ram(struct e820map *map,
 uint64_t s, uint64_t e);
   extern int e820_change_range_type(
       struct e820map *map, uint64_t s, uint64_t e,
       uint32_t orig_type, uint32_t new_type);
xen/arch/x86/setup.c +0 -0
index d041cbd5f6f1..9c1f1eafa0d7 100644
Quoted 8 lines
Quoted 8 lines

xen/arch/x86/setup.c void asmlinkage __init noreturn __start_xen(void) · line 1643

Quoted 8 lines
xen/arch/x86/setup.c +2 -0
@@ -1639,7 +1639,8 @@ void asmlinkage __init noreturn __start_xen(void)
       {
           uint64_t s = bi->mods[i].start, l = bi->mods[i].size;
   -        reserve_e820_ram(&boot_e820, s, s + PAGE_ALIGN(l));
+        if ( !reserve_e820_ram(&boot_e820, s, s + PAGE_ALIGN(l)) )
+            panic("Failed to reserve boot module %u in E820\n", i);

i is an int, so it should be %d.

Quoted 27 lines
Quoted 18 lines
Quoted 18 lines
xen/arch/x86/setup.c +1 -2
       }
         if ( !xen_phys_start )
@@ -1652,11 +1653,13 @@ void asmlinkage __init noreturn
 __start_xen(void)
       /* This needs to remain in sync with remove_xen_ranges(). */
       if ( efi_boot_mem_unused(&eb_start, &eb_end) )
       {
-        reserve_e820_ram(&boot_e820, __pa(_stext), __pa(eb_start));
-        reserve_e820_ram(&boot_e820, __pa(eb_end),
 __pa(__2M_rwdata_end));
+        if ( !reserve_e820_ram(&boot_e820, __pa(_stext),

__pa(eb_start)) ||
+ !reserve_e820_ram(&boot_e820, __pa(eb_end),
__pa(__2M_rwdata_end)) )

xen/arch/x86/setup.c +1 -1
+            panic("Failed to reserve Xen in E820\n");
       }
       else
-        reserve_e820_ram(&boot_e820, __pa(_stext),
Quoted 8 lines
Quoted 5 lines

__pa(__2M_rwdata_end));
+ if ( reserve_e820_ram(&boot_e820, __pa(_stext),
__pa(__2M_rwdata_end)) )
+ panic("Failed to reserve Xen in E820\n");

This condition is inverted.
Otherwise this looks like a sensible change.

Quoted 1 line

Oops, yes. Fixed.

With those changes,

Reviewed-by on v1 1/1
#4 lore raw
In reply to Andrew Cooper

xen/arch/x86/include/asm/e820.h struct e820map { · line 28

Quoted 8 lines · Andrew Cooper
xen/arch/x86/include/asm/e820.h +1 -1
@@ -25,7 +25,7 @@ struct e820map {
 
 extern int sanitize_e820_map(struct e820entry *biosmap, unsigned int *pnr_map);
 extern int e820_all_mapped(u64 start, u64 end, unsigned type);
-extern int reserve_e820_ram(struct e820map *map, uint64_t s, uint64_t e);
+extern int __must_check reserve_e820_ram(struct e820map *map, uint64_t s, uint64_t e);

Nit: This line has grown too long now. With this and the adjustments Ross has asked for:

Jan