x86/mm: miscellaneous fixes
v3 cover x86/mm: miscellaneous fixes
Hello,
The attempt to fix destroy_xen_mappings() so that L2 tables are consistently freed uncovered some errors in the memory management code. The following series attempts to fix them.
All patches except for 4/4 are new in v2, and 4/4 has no change from v1, hence kept Jan's Reviewed-by tag in 4/4.
Thanks, Roger.
Roger Pau Monne (4):
x86/mm: introduce helpers to detect super page alignment x86/mm: skip super-page alignment checks for non-present entries x86/setup: remove bootstrap_map_addr() usage of destroy_xen_mappings() x86/mm: ensure L2 is always freed if empty
xen/arch/x86/mm.c | 33 ++++++++++++++++++++++----------- xen/arch/x86/setup.c | 4 +++- 2 files changed, 25 insertions(+), 12 deletions(-)
Signature · 1 line
2.46.0
PATCH v3 1/4 x86/mm: introduce helpers to detect super page alignment
Split the code that detects whether the physical and linear address of a mapping request are suitable to be used in an L3 or L2 slot.
No functional change intended.
---
Changes since v2:
- Fix parenthesization of macro parameter.
- Add another usage of IS_L2E_ALIGNED().
Changes since v1:
- Make the macros local to map_pages_to_xen().
- Some adjustments to macro logic.
--- xen/arch/x86/mm.c | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-)
xen/arch/x86/mm.c +14 -11
@@ -5493,6 +5493,13 @@ int map_pages_to_xen( } \ }while (0 ) +/* Check if a (virt, mfn) tuple is aligned for a given slot level. */ +#define IS_LnE_ALIGNED (v, m, n) \ + IS_ALIGNED(PFN_DOWN(v) | mfn_x(m), \ + (1UL << (PAGETABLE_ORDER * ((n) - 1))) - 1) +#define IS_L2E_ALIGNED (v, m) IS_LnE_ALIGNED(v, m, 2) +#define IS_L3E_ALIGNED (v, m) IS_LnE_ALIGNED(v, m, 3) +L3T_INIT (current_l3page);while ( nr_mfns !=0 )
@@ -5510,9 +5517,7 @@ int map_pages_to_xen(L3T_LOCK (current_l3page); ol3e = *pl3e; -if ( cpu_has_page1gb && - !(((virt >>PAGE_SHIFT ) |mfn_x (mfn)) & - ((1UL << (L3_PAGETABLE_SHIFT -PAGE_SHIFT )) -1 )) && +if ( cpu_has_page1gb &&IS_L3E_ALIGNED (virt, mfn) && nr_mfns >= (1UL << (L3_PAGETABLE_SHIFT -PAGE_SHIFT )) && !(flags & (_PAGE_PAT |MAP_SMALL_PAGES )) ) {
@@ -5631,8 +5636,7 @@ int map_pages_to_xen(if ( !pl2e ) goto out; -if ( ((((virt >>PAGE_SHIFT ) |mfn_x (mfn)) & - ((1u <<PAGETABLE_ORDER ) -1 )) ==0 ) && +if (IS_L2E_ALIGNED (virt, mfn) && (nr_mfns >= (1u <<PAGETABLE_ORDER )) && !(flags & (_PAGE_PAT|MAP_SMALL_PAGES )) ) {
@@ -5749,9 +5753,7 @@ int map_pages_to_xen( nr_mfns -=1UL ;if ( (flags ==PAGE_HYPERVISOR ) && - ((nr_mfns ==0 ) || - ((((virt >>PAGE_SHIFT ) |mfn_x (mfn)) & - ((1u <<PAGETABLE_ORDER ) -1 )) ==0 )) ) + ((nr_mfns ==0 ) ||IS_L2E_ALIGNED (virt, mfn)) ) {unsigned long base_mfn;const l1_pgentry_t *l1t;
@@ -5802,9 +5804,7 @@ int map_pages_to_xen( check_l3:if ( cpu_has_page1gb && (flags ==PAGE_HYPERVISOR ) && - ((nr_mfns ==0 ) || - !(((virt >>PAGE_SHIFT ) |mfn_x (mfn)) & - ((1UL << (L3_PAGETABLE_SHIFT -PAGE_SHIFT )) -1 ))) ) + ((nr_mfns ==0 ) ||IS_L3E_ALIGNED (virt, mfn)) ) {unsigned long base_mfn;const l2_pgentry_t *l2t;
@@ -5848,6 +5848,9 @@ int map_pages_to_xen( } } +#undef IS_L3E_ALIGNED +#undef IS_L2E_ALIGNED +#undef IS_LnE_ALIGNED#undef flush_flags rc =0 ;
Signature · 1 line
2.46.0
PATCH v3 2/4 x86/mm: skip super-page alignment checks for non-present entries
In reply to Roger Pau MonneINVALID_MFN is ~0, so by it having all bits as 1s it doesn't fulfill the super-page address alignment checks for L3 and L2 entries. Skip the alignment checks if the new entry is a non-present one.
This fixes a regression introduced by 0b6b51a69f4d, where the switch from 0 to INVALID_MFN caused all super-pages to be shattered when attempting to remove mappings by passing INVALID_MFN instead of 0.
---
Changees since v2:
- Remove unneeded page present check.
Changes since v1:
- Detect non-present entries from the flags contents rather than checking the mfn parameter.
--- xen/arch/x86/mm.c | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-)
xen/arch/x86/mm.c +14 -6
@@ -5493,10 +5493,17 @@ int map_pages_to_xen( } \ }while (0 ) -/* Check if a (virt, mfn) tuple is aligned for a given slot level. */ -#define IS_LnE_ALIGNED (v, m, n) \ - IS_ALIGNED(PFN_DOWN(v) | mfn_x(m), \ - (1UL << (PAGETABLE_ORDER * ((n) - 1))) - 1) +/* +* Check if a (virt, mfn) tuple is aligned for a given slot level. m must not +* be INVALID_MFN, since alignment is only relevant for present entries. + */ +#define IS_LnE_ALIGNED (v, m, n) ({ \ + mfn_t m_ = m; \ + \ + ASSERT(!mfn_eq(m_, INVALID_MFN)); \ + IS_ALIGNED(PFN_DOWN(v) | mfn_x(m_), \ + (1UL << (PAGETABLE_ORDER * ((n) - 1))) - 1); \ +})#define IS_L2E_ALIGNED (v, m) IS_LnE_ALIGNED(v, m, 2)#define IS_L3E_ALIGNED (v, m) IS_LnE_ALIGNED(v, m, 3)
@@ -5517,7 +5524,8 @@ int map_pages_to_xen(L3T_LOCK (current_l3page); ol3e = *pl3e; -if ( cpu_has_page1gb &&IS_L3E_ALIGNED (virt, mfn) && +if ( cpu_has_page1gb && + (!(flags & _PAGE_PRESENT) ||IS_L3E_ALIGNED (virt, mfn)) && nr_mfns >= (1UL << (L3_PAGETABLE_SHIFT -PAGE_SHIFT )) && !(flags & (_PAGE_PAT |MAP_SMALL_PAGES )) ) {
@@ -5636,7 +5644,7 @@ int map_pages_to_xen(if ( !pl2e ) goto out; -if (IS_L2E_ALIGNED (virt, mfn) && +if ( (!(flags & _PAGE_PRESENT) ||IS_L2E_ALIGNED (virt, mfn)) && (nr_mfns >= (1u <<PAGETABLE_ORDER )) && !(flags & (_PAGE_PAT|MAP_SMALL_PAGES )) ) {
Signature · 1 line
2.46.0
Re: [PATCH v3 2/4] x86/mm: skip super-page alignment checks for non-present entries
Quoted 11 lines · Roger Pau Monne
xen/arch/x86/mm.c +2
-1@@ -5517,7 +5524,8 @@ int map_pages_to_xen(L3T_LOCK (current_l3page); ol3e = *pl3e; -if ( cpu_has_page1gb &&IS_L3E_ALIGNED (virt, mfn) && +if ( cpu_has_page1gb && + (!(flags & _PAGE_PRESENT) ||IS_L3E_ALIGNED (virt, mfn)) && nr_mfns >= (1UL << (L3_PAGETABLE_SHIFT -PAGE_SHIFT )) && !(flags & (_PAGE_PAT |MAP_SMALL_PAGES )) ) {
xen/arch/x86/mm.c int map_pages_to_xen( · line 5650
Quoted 8 lines
xen/arch/x86/mm.c +1
-1@@ -5636,7 +5644,7 @@ int map_pages_to_xen(if ( !pl2e ) goto out; -if (IS_L2E_ALIGNED (virt, mfn) && +if ( (!(flags & _PAGE_PRESENT) ||IS_L2E_ALIGNED (virt, mfn)) && (nr_mfns >= (1u <<PAGETABLE_ORDER )) && !(flags & (_PAGE_PAT|MAP_SMALL_PAGES )) ) {
Upon inspecting Andrew's report of crashes I noticed that this can't be quite right. We can't entirely skip the alignment check when non-present mappings are requested; we merely need to limit the check to the VA. In a reply to the 1st v2 I actually had it arranged to match that requirement:
if ( (cpu_has_page1gb || !(flags & _PAGE_PRESENT)) &&
IS_L3E_ALIGNED(virt, flags & _PAGE_PRESENT ? mfn : _mfn(0)) &&
nr_mfns >= (1UL << (L3_PAGETABLE_SHIFT - PAGE_SHIFT)) &&
!(flags & (_PAGE_PAT | MAP_SMALL_PAGES)) )Yet then I didn't pay attention to the difference when reviewing the 2nd v2 (that versioning issue of course isn't helping here either).
I'm afraid I can't (yet) connect the observed bad behavior with this issue, though.
Jan
Re: [PATCH v3 2/4] x86/mm: skip super-page alignment checks for non-present entries
Quoted 31 lines · Jan Beulich
Quoted 11 lines · Roger Pau Monne
xen/arch/x86/mm.c +2
-1@@ -5517,7 +5524,8 @@ int map_pages_to_xen(L3T_LOCK (current_l3page); ol3e = *pl3e; -if ( cpu_has_page1gb &&IS_L3E_ALIGNED (virt, mfn) && +if ( cpu_has_page1gb && + (!(flags & _PAGE_PRESENT) ||IS_L3E_ALIGNED (virt, mfn)) && nr_mfns >= (1UL << (L3_PAGETABLE_SHIFT -PAGE_SHIFT )) && !(flags & (_PAGE_PAT |MAP_SMALL_PAGES )) ) {xen/arch/x86/mm.c int map_pages_to_xen( · line 5650
Quoted 8 lines
xen/arch/x86/mm.c +1
-1@@ -5636,7 +5644,7 @@ int map_pages_to_xen(if ( !pl2e ) goto out; -if (IS_L2E_ALIGNED (virt, mfn) && +if ( (!(flags & _PAGE_PRESENT) ||IS_L2E_ALIGNED (virt, mfn)) && (nr_mfns >= (1u <<PAGETABLE_ORDER )) && !(flags & (_PAGE_PAT|MAP_SMALL_PAGES )) ) {Upon inspecting Andrew's report of crashes I noticed that this can't be quite right. We can't entirely skip the alignment check when non-present mappings are requested; we merely need to limit the check to the VA. In a reply to the 1st v2 I actually had it arranged to match that requirement:
if ( (cpu_has_page1gb || !(flags & _PAGE_PRESENT)) && IS_L3E_ALIGNED(virt, flags & _PAGE_PRESENT ? mfn : _mfn(0)) && nr_mfns >= (1UL << (L3_PAGETABLE_SHIFT - PAGE_SHIFT)) && !(flags & (_PAGE_PAT | MAP_SMALL_PAGES)) )
Quoted 5 lines
Yet then I didn't pay attention to the difference when reviewing the 2nd v2 (that versioning issue of course isn't helping here either).
I'm afraid I can't (yet) connect the observed bad behavior with this issue, though.
I think this might be caused by map_pages_to_xen() now freeing vmap regions still under use, and that seems to manifest with the memnodemap array being unintentionally unmapped (because it's allocated with vmap_contig()). See the usage of mfn_to_nid() in free_heap_pages().
Will prepare a patch, sorry.
Thanks, Roger.
Re: [PATCH v3 2/4] x86/mm: skip super-page alignment checks for non-present entries
In reply to Jan BeulichQuoted 31 lines · Jan Beulich
Quoted 11 lines · Roger Pau Monne
xen/arch/x86/mm.c +2
-1@@ -5517,7 +5524,8 @@ int map_pages_to_xen(L3T_LOCK (current_l3page); ol3e = *pl3e; -if ( cpu_has_page1gb &&IS_L3E_ALIGNED (virt, mfn) && +if ( cpu_has_page1gb && + (!(flags & _PAGE_PRESENT) ||IS_L3E_ALIGNED (virt, mfn)) && nr_mfns >= (1UL << (L3_PAGETABLE_SHIFT -PAGE_SHIFT )) && !(flags & (_PAGE_PAT |MAP_SMALL_PAGES )) ) {xen/arch/x86/mm.c int map_pages_to_xen( · line 5650
Quoted 8 lines
xen/arch/x86/mm.c +1
-1@@ -5636,7 +5644,7 @@ int map_pages_to_xen(if ( !pl2e ) goto out; -if (IS_L2E_ALIGNED (virt, mfn) && +if ( (!(flags & _PAGE_PRESENT) ||IS_L2E_ALIGNED (virt, mfn)) && (nr_mfns >= (1u <<PAGETABLE_ORDER )) && !(flags & (_PAGE_PAT|MAP_SMALL_PAGES )) ) {Upon inspecting Andrew's report of crashes I noticed that this can't be quite right. We can't entirely skip the alignment check when non-present mappings are requested; we merely need to limit the check to the VA. In a reply to the 1st v2 I actually had it arranged to match that requirement:
if ( (cpu_has_page1gb || !(flags & _PAGE_PRESENT)) && IS_L3E_ALIGNED(virt, flags & _PAGE_PRESENT ? mfn : _mfn(0)) && nr_mfns >= (1UL << (L3_PAGETABLE_SHIFT - PAGE_SHIFT)) && !(flags & (_PAGE_PAT | MAP_SMALL_PAGES)) )
Quoted 5 lines
Yet then I didn't pay attention to the difference when reviewing the 2nd v2 (that versioning issue of course isn't helping here either).
I'm afraid I can't (yet) connect the observed bad behavior with this issue, though.
I think I now can: memnodemap is set using vmap_contig(). The VESA frame buffer unmap, neglecting to check VA alignment, will have wrongly unmapped memnodemap.
Jan
PATCH v3 3/4 x86/setup: remove bootstrap_map_addr() usage of destroy_xen_mappings()
In reply to Roger Pau Monnebootstrap_map_addr() needs to be careful to not remove existing page-table structures when tearing down mappings, as such pagetable structures might be needed to fulfill subsequent mappings requests. The comment ahead of the function already notes that pagetable memory shouldn't be allocated.
Fix this by using map_pages_to_xen(), which does zap the page-table entries, but does not free page-table structures even when empty.
---
Changes since v1:
- Adjust commit message.
--- xen/arch/x86/setup.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
xen/arch/x86/setup.c +3 -1
@@ -462,7 +462,9 @@ static void *__init bootstrap_map_addr(paddr_t start, paddr_t end)if ( !end ) { -destroy_xen_mappings (BOOTSTRAP_MAP_BASE ,BOOTSTRAP_MAP_LIMIT ); +map_pages_to_xen (BOOTSTRAP_MAP_BASE ,INVALID_MFN , +PFN_DOWN (map_cur -BOOTSTRAP_MAP_BASE ), + _PAGE_NONE); map_cur =BOOTSTRAP_MAP_BASE ;return NULL ; }
Signature · 1 line
2.46.0
PATCH v3 4/4 x86/mm: ensure L2 is always freed if empty
In reply to Roger Pau MonneThe current logic in modify_xen_mappings() allows for fully empty L2 tables to not be freed and unhooked from the parent L3 if the last L2 slot is not populated.
Ensure that even when an L2 slot is empty the logic to check whether the whole L2 can be removed is not skipped.
--- xen/arch/x86/mm.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
xen/arch/x86/mm.c +1 -1
@@ -5989,7 +5989,7 @@ int modify_xen_mappings(unsigned long s, unsigned long e, unsigned int nf) v +=1UL <<L2_PAGETABLE_SHIFT ; v &= ~((1UL <<L2_PAGETABLE_SHIFT ) -1 ); -continue ; + goto check_l3; }if (l2e_get_flags (*pl2e) & _PAGE_PSE )
Signature · 1 line
2.46.0
Quoted 9 lines · Roger Pau Monne
Hello,
The attempt to fix
destroy_xen_mappings()so that L2 tables are consistently freed uncovered some errors in the memory management code. The following series attempts to fix them.All patches except for 4/4 are new in v2, and 4/4 has no change from v1, hence kept Jan's Reviewed-by tag in 4/4.
Quoted 7 lines
Thanks, Roger.
Roger Pau Monne (4):
x86/mm: introduce helpers to detect super page alignment x86/mm: skip super-page alignment checks for non-present entries x86/setup: removebootstrap_map_addr()usage ofdestroy_xen_mappings()x86/mm: ensure L2 is always freed if empty
Still broken. This happened to be a Cascade Lake:
(XEN) 3... 2... 1...
(XEN) Xen is relinquishing VGA console.
(XEN) ----[ Xen-4.20.0-4-d x86_64 debug=y Not tainted ]---- (XEN) CPU: 0
(XEN) RIP: e008:[<ffff82d04022cdc8>] common/page_alloc.c#free_heap_pages+0x52/0x753 <snip>
(XEN) Xen call trace:
(XEN) [<ffff82d04022cdc8>] R common/page_alloc.c#free_heap_pages+0x52/0x753
(XEN) [<ffff82d04022e98f>] F free_domheap_pages+0x445/0x447 (XEN) [<ffff82d040320f88>] F free_xen_pagetable+0x2f/0x31 (XEN) [<ffff82d04032193d>] F map_pages_to_xen+0x5df/0x1028 (XEN) [<ffff82d040238ab1>] F vunmap+0x27/0xde (XEN) [<ffff82d040624b61>] F vesa_endboot+0x86/0xb3 (XEN) [<ffff82d0406244c1>] F video_endboot+0x93/0x261 (XEN) [<ffff82d040616e3d>] F console_endboot+0x92/0x120 (XEN) [<ffff82d0406407e2>] F __start_xen+0x2139/0x2307 (XEN) [<ffff82d0402033ae>] F __high_start+0x8e/0x90 (XEN)
(XEN) Pagetable walk from ffff82c000205001:
(XEN) L4[0x105] = 0000000060431063 ffffffffffffffff (XEN) L3[0x100] = 000000006fffd063 ffffffffffffffff (XEN) L2[0x001] = fffffffffffff000 ffffffffffffffff (XEN)
(XEN) ****************************************
(XEN) Panic on CPU 0:
(XEN) FATAL PAGE FAULT
(XEN) [error_code=0000]
(XEN) Faulting linear address: ffff82c000205001 (XEN) ****************************************
Note the definitely bogus L2e, which I'm guessing is an issue with the use of INVALID_MFN.
But the same crash is seen on BDX, SKX, SPR, Naples, Rome and Milan, all including the bad L2e.
ICX crashed but there was no serial console.
Genoa was weird. It was:
(XEN) ----[ Xen-4.20.0-4-d x86_64 debug=y Not tainted ]---- (XEN) CPU: 24
(XEN) RIP: e008:[<ffff82d040330509>] __node_distance+0x20/0x62 <snip>
(XEN) Xen call trace:
(XEN) [<ffff82d040330509>] R __node_distance+0x20/0x62 (XEN) [<ffff82d040240ee5>] F do_sysctl+0xcf8/0x140e (XEN) [<ffff82d0402f6b21>] F pv_hypercall+0x59b/0x62b (XEN) [<ffff82d0402012d3>] F lstar_enter+0x143/0x150 (XEN)
(XEN) Pagetable walk from ffff82c000203024:
(XEN) L4[0x105] = 000000005fc31063 ffffffffffffffff (XEN) L3[0x100] = 00000000677cd063 ffffffffffffffff (XEN) L2[0x001] = 000000207a79a063 ffffffffffffffff (XEN) L1[0x003] = 0000000000000000 ffffffffffffffff (XEN)
(XEN) ****************************************
(XEN) Panic on CPU 24:
(XEN) FATAL PAGE FAULT
(XEN) [error_code=0000]
(XEN) Faulting linear address: ffff82c000203024
(XEN) ****************************************
while sat at the login prompt, which means it had survived a full install and first boot.
The range of the change was 64d2290cf96bb^..d2c214ede05ca (starting from Jan's fix to _xvrealloc()) but there are no other obvious candidates.
This crash isn't obviously related to the others, but there's nothing else like it seen before. Memory corruption isn't out of the question.
~Andrew
Quoted 52 lines · Andrew Cooper
Quoted 9 lines · Roger Pau Monne
Hello,
The attempt to fix
destroy_xen_mappings()so that L2 tables are consistently freed uncovered some errors in the memory management code. The following series attempts to fix them.All patches except for 4/4 are new in v2, and 4/4 has no change from v1, hence kept Jan's Reviewed-by tag in 4/4.
Quoted 7 lines
Thanks, Roger.
Roger Pau Monne (4):
x86/mm: introduce helpers to detect super page alignment x86/mm: skip super-page alignment checks for non-present entries x86/setup: removebootstrap_map_addr()usage ofdestroy_xen_mappings()x86/mm: ensure L2 is always freed if emptyStill broken. This happened to be a Cascade Lake:
(XEN) 3... 2... 1...
(XEN) Xen is relinquishing VGA console.
(XEN) ----[ Xen-4.20.0-4-d x86_64 debug=y Not tainted ]---- (XEN) CPU: 0
(XEN) RIP: e008:[<ffff82d04022cdc8>] common/page_alloc.c#free_heap_pages+0x52/0x753 <snip>
(XEN) Xen call trace:
(XEN) [<ffff82d04022cdc8>] R common/page_alloc.c#free_heap_pages+0x52/0x753
(XEN) [<ffff82d04022e98f>] Ffree_domheap_pages+0x445/0x447(XEN) [<ffff82d040320f88>] Ffree_xen_pagetable+0x2f/0x31(XEN) [<ffff82d04032193d>] Fmap_pages_to_xen+0x5df/0x1028(XEN) [<ffff82d040238ab1>] Fvunmap+0x27/0xde(XEN) [<ffff82d040624b61>] Fvesa_endboot+0x86/0xb3(XEN) [<ffff82d0406244c1>] Fvideo_endboot+0x93/0x261(XEN) [<ffff82d040616e3d>] Fconsole_endboot+0x92/0x120(XEN) [<ffff82d0406407e2>] F__start_xen+0x2139/0x2307(XEN) [<ffff82d0402033ae>] F__high_start+0x8e/0x90(XEN)
(XEN) Pagetable walk fromffff82c000205001:
(XEN) L4[0x105] = 0000000060431063 ffffffffffffffff (XEN) L3[0x100] =000000006fffd063ffffffffffffffff (XEN) L2[0x001] =fffffffffffff000ffffffffffffffff (XEN)
(XEN) ****************************************
(XEN) Panic on CPU 0:
(XEN) FATAL PAGE FAULT
(XEN) [error_code=0000]
(XEN) Faulting linear address:ffff82c000205001(XEN) ****************************************
Quoted 2 lines
Note the definitely bogus L2e, which I'm guessing is an issue with the use of
INVALID_MFN.
Why bogus? That's precisely what vunmap() requests:
map_pages_to_xen(addr, INVALID_MFN, pages, _PAGE_NONE);
What I agree with is that INVALID_MFN better wouldn't spill into the upper non-address part of the PTE, but that doesn't look to be important here.
I can't, however, explain why free_heap_pages() would access VMAP space. It's all page / MFN based and shouldn't even have a notion of the VA space underlying vmap(). Is that address in CR2 inside the range that was mapping the frame buffer (i.e. part of what vunmap() is in the process of removing)?
What I also don't understand: How did this pass CI then? Are all tests there perhaps done without vga= designating a VESA mode to use?
Jan