x86/mm: Fix boot with some memory above MAXMEM
Subsystems (2)
PATCH 1/1 x86/mm: Fix boot with some memory above MAXMEM
A 5-level paging capable machine can have memory above 46-bit in the physical address space. This memory is only addressable in the 5-level paging mode: we don't have enough virtual address space to create direct mapping for such memory in the 4-level paging mode.
Currently, we fail boot completely: NULL pointer dereference in subsection_map_init().
Skip creating a memblock for such memory instead and notify user that some memory is not addressable.
---
Tested with a hacked QEMU: https://gist.github.com/kiryl/d45eb54110944ff95e544972d8bdac1d
--- arch/x86/kernel/e820.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-)
arch/x86/kernel/e820.c +8 -1
@@ -1307,7 +1307,14 @@ void __init e820__memblock_setup(void)if (entry->type !=E820_TYPE_RAM && entry->type !=E820_TYPE_RESERVED_KERN )continue ; -memblock_add (entry->addr, entry->size); +if (entry->addr >=MAXMEM || end >=MAXMEM ) +pr_err_once ("Some physical memory is not addressable in the paging mode.\n" ); + +if (entry->addr >=MAXMEM ) +continue ; + + end =min_t (u64, end,MAXMEM -1 ); +memblock_add (entry->addr, end - entry->addr); }/* Throw away partial pages: */
Signature · 1 line
2.26.2
arch/x86/kernel/e820.c void __init e820__memblock_setup(void) · line 1311
Quoted 3 lines · Kirill A. Shutemov
arch/x86/kernel/e820.c +2
-1-memblock_add (entry->addr, entry->size); +if (entry->addr >=MAXMEM || end >=MAXMEM ) +pr_err_once ("Some physical memory is not addressable in the paging mode.\n" );
Hi Kirill,
Thanks for fixing this!
Could we make the pr_err() a bit more informative, though? It would be nice to print out how much memory (or which addresses at least) are being thrown away.
I was also thinking that it would be handy to tell folks how to rectify the situation. Should we perhaps dump out the runtime status of X86_FEATURE_LA57?
Quoted 9 lines · Dave Hansen
Review
Quoted 3 lines · Kirill A. Shutemov
(quoted diff) +2
-1- memblock_add(entry->addr, entry->size); + if (entry->addr >= MAXMEM || end >= MAXMEM) + pr_err_once("Some physical memory is not addressable in the paging mode.\n");Hi Kirill,
Thanks for fixing this!
Quoted 7 lines
Could we make the
pr_err()a bit more informative, though? It would be nice to print out how much memory (or which addresses at least) are being thrown away.I was also thinking that it would be handy to tell folks how to rectify the situation. Should we perhaps dump out the runtime status of
X86_FEATURE_LA57?
Something like this (incremental patch)?
arch/x86/kernel/e820.c +13 -5
@@ -1280,8 +1280,8 @@ void __init e820__memory_setup(void)void __init e820__memblock_setup (void ) { +u64 size, end, not_addressable =0 ;int i; -u64 end;/* * The bootstrap memblock region count maximum is 128 entries
@@ -1307,16 +1307,24 @@ void __init e820__memblock_setup(void)if (entry->type !=E820_TYPE_RAM && entry->type !=E820_TYPE_RESERVED_KERN )continue ; -if (entry->addr >=MAXMEM || end >=MAXMEM ) -pr_err_once ("Some physical memory is not addressable in the paging mode.\n" ); - -if (entry->addr >=MAXMEM ) +if (entry->addr >=MAXMEM ) { + not_addressable += entry->size;continue ; + } end =min_t (u64, end,MAXMEM -1 ); + size = end - entry->addr; + not_addressable += entry->size - size;memblock_add (entry->addr, end - entry->addr); } +if (not_addressable) { +pr_err ("%lldMB of physical memory is not addressable in the paging mode\n" , + not_addressable >>20 ); +if (!pgtable_l5_enabled ()) +pr_err ("Consider enabling 5-level paging\n" ); + } +/* Throw away partial pages: */ memblock_trim_memory (PAGE_SIZE );
Signature · 1 line
Kirill A. Shutemov
Dave Hansen
Reviewed-byarch/x86/kernel/e820.c void __init e820__memblock_setup(void) · line 1326
Quoted 6 lines · Kirill A. Shutemov
arch/x86/kernel/e820.c +6
-0+if (not_addressable) { +pr_err ("%lldMB of physical memory is not addressable in the paging mode\n" , + not_addressable >>20 ); +if (!pgtable_l5_enabled ()) +pr_err ("Consider enabling 5-level paging\n" ); + }
Looks sane to me. Definitely good enough until we get the first bug reports from an end user about how they screwed this up in practice.
For the aggregate patch:
BTW, it's a shame that 0day and friends can't find stuff like this. I have the feeling we have more bugs like this coming.
arch/x86/kernel/e820.c void __init e820__memblock_setup(void) · line 1323
Quoted 2 lines
arch/x86/kernel/e820.c +2
-0+pr_err ("%lldMB of physical memory is not addressable in the paging mode\n" , + not_addressable >>20 );
Is "MB" the right unit for this. The problem seems to happen for systems with >64TB ... I doubt the unaddressable memory is just a couple of MBbytes
-Tony
Quoted 5 lines · Tony
arch/x86/kernel/e820.c void __init e820__memblock_setup(void) · line 1323
Quoted 2 lines
arch/x86/kernel/e820.c +2
-0+pr_err ("%lldMB of physical memory is not addressable in the paging mode\n" , + not_addressable >>20 );Is "MB" the right unit for this. The problem seems to happen for systems with >64TB ... I doubt the unaddressable memory is just a couple of MBbytes
Change it to GB?
Signature · 1 line
Kirill A. Shutemov
Quoted 7 lines
Quoted 5 lines
arch/x86/kernel/e820.c void __init e820__memblock_setup(void) · line 1323
Quoted 2 lines
arch/x86/kernel/e820.c +2
-0+pr_err ("%lldMB of physical memory is not addressable in the paging mode\n" , + not_addressable >>20 );Is "MB" the right unit for this. The problem seems to happen for systems with >64TB ... I doubt the unaddressable memory is just a couple of MBbytes
Change it to GB?
I think it would be more readable.
[Maybe Linux needs a magic %p{something} that does auto-sizing to print in the most appropriate out of KB, MB, GB, TB, PB?]
-Tony
Quoted 11 lines · Tony
Quoted 7 lines
Quoted 5 lines
arch/x86/kernel/e820.c void __init e820__memblock_setup(void) · line 1323
Quoted 2 lines
arch/x86/kernel/e820.c +2
-0+pr_err ("%lldMB of physical memory is not addressable in the paging mode\n" , + not_addressable >>20 );Is "MB" the right unit for this. The problem seems to happen for systems with >64TB ... I doubt the unaddressable memory is just a couple of MBbytes
Change it to GB?
I think it would be more readable.
[Maybe Linux needs a magic %p{something} that does auto-sizing to print in the most appropriate out of KB, MB, GB, TB, PB?]
We have one in string_helpers.c.
Signature · 2 lines
With Best Regards, Andy Shevchenko
Quoted 7 lines · Andy Shevchenko
Quoted 6 lines · Tony
Quoted 6 lines
Quoted 5 lines
arch/x86/kernel/e820.c void __init e820__memblock_setup(void) · line 1323
Quoted 2 lines
arch/x86/kernel/e820.c +2
-0+pr_err ("%lldMB of physical memory is not addressable in the paging mode\n" , + not_addressable >>20 );Is "MB" the right unit for this. The problem seems to happen for systems with >64TB ... I doubt the unaddressable memory is just a couple of MBbytes
Quoted 7 lines
Quoted 5 lines
Quoted 1 line
Change it to GB?
I think it would be more readable.
[Maybe Linux needs a magic %p{something} that does auto-sizing to print in the most appropriate out of KB, MB, GB, TB, PB?]
We have one in
string_helpers.c.
Ah, nice. So:
#include <linux/string_helpers.h>
char tmp[10]; /* Bother, no #define for this, just a comment in string_helpers.c */
string_get_size(not_addressable, 1, STRING_UNITS_2, tmp, sizeof(tmp);
pr_err("%s of physical memory is not addressable in the paging mode\n", tmp);-Tony
Quoted 21 lines · Tony
Quoted 7 lines · Andy Shevchenko
Quoted 6 lines · Tony
Quoted 6 lines
Quoted 5 lines
Review
Quoted 2 lines
(quoted diff) +2
-0+ pr_err("%lldMB of physical memory is not addressable in the paging mode\n", + not_addressable >> 20);Is "MB" the right unit for this. The problem seems to happen for systems with >64TB ... I doubt the unaddressable memory is just a couple of MBbytes
Quoted 7 lines
Quoted 5 lines
Quoted 1 line
Change it to GB?
I think it would be more readable.
[Maybe Linux needs a magic %p{something} that does auto-sizing to print in the most appropriate out of KB, MB, GB, TB, PB?]
We have one in
string_helpers.c.Ah, nice. So:
#include <
linux/string_helpers.h>
Quoted 6 lines
char tmp[10]; /* Bother, no #define for this, just a comment in string_helpers.c */
string_get_size(not_addressable, 1, STRING_UNITS_2, tmp, sizeof(tmp);
pr_err("%s of physical memory is not addressable in the paging mode\n", tmp);I've already submitted the patch upstream. I'll add it if a new revision is needed.
Signature · 1 line
Kirill A. Shutemov
Quoted 8 lines · Kirill A. Shutemov
A 5-level paging capable machine can have memory above 46-bit in the physical address space. This memory is only addressable in the 5-level paging mode: we don't have enough virtual address space to create direct mapping for such memory in the 4-level paging mode.
Currently, we fail boot completely: NULL pointer dereference in
subsection_map_init().
Quoted 8 lines
Skip creating a memblock for such memory instead and notify user that some memory is not addressable.
Signed-off-by Kirill A. Shutemov <[email protected]>Cc [email protected] # v4.14---
Tested with a hacked QEMU: https://gist.github.com/kiryl/d45eb54110944ff95e544972d8bdac1d
BTW, I was only able to boot with legacy SeaBIOS, not with OVMF. No idea why.
Signature · 1 line
Kirill A. Shutemov