Skip to contentSkip to conversation

x86/mm: miscellaneous fixes

PATCH Superseded Roger Pau Monne 2 participants 8 messages Nov 8, 2024 – Nov 11, 2024

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

v2 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    | 30 ++++++++++++++++++++++--------
 xen/arch/x86/setup.c |  4 +++-
 2 files changed, 25 insertions(+), 9 deletions(-)
Signature · 1 line
2.46.0
#1 lore raw

PATCH v2 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 v1:
- Make the macros local to map_pages_to_xen().
- Some adjustments to macro logic.

---
 xen/arch/x86/mm.c | 20 ++++++++++++--------
 1 file changed, 12 insertions(+), 8 deletions(-)
xen/arch/x86/mm.c +12 -8
index d537a799bced..8afb63c855b9 100644
@@ -5232,6 +5232,12 @@ 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 )
@@ -5249,9 +5255,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)) )
         {
@@ -5370,8 +5374,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)) )
         {
@@ -5541,9 +5544,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;
@@ -5587,6 +5588,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
Reviewed-by on v2 1/4
#2 lore raw

Re: [PATCH v2 1/4] x86/mm: introduce helpers to detect super page alignment

Quoted 6 lines · Roger Pau Monne

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.

with one further tweak:

xen/arch/x86/mm.c int map_pages_to_xen( · line 5237

Quoted 9 lines
xen/arch/x86/mm.c +3 -0
@@ -5232,6 +5232,12 @@ 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)

n wants parenthesizing here, for Misra's sake. I'll take the liberty to do so while committing.

Jan

#3 lore raw

Re: [PATCH v2 1/4] x86/mm: introduce helpers to detect super page alignment

In reply to Roger Pau Monne

xen/arch/x86/mm.c int map_pages_to_xen(

Quoted 37 lines · Roger Pau Monne
xen/arch/x86/mm.c +8 -5
@@ -5232,6 +5232,12 @@ 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 )
@@ -5249,9 +5255,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)) )
         {
@@ -5370,8 +5374,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)) )
         {
@@ -5541,9 +5544,7 @@ int map_pages_to_xen(

About 50 lines up from here there's another place where I think IS_L2E_ALIGNED() wants using. That becomes noticeable with the remark I'm about to make on patch 2, on this ...

xen/arch/x86/mm.c int map_pages_to_xen( · line 5548

Quoted 8 lines
xen/arch/x86/mm.c +1 -3
  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)) )
         {

... similar construct (checking "flags == PAGE_HYPERVISOR"). Not sure if I should also make this kind of an adjustment while committing ...

Jan

#4 lore raw

PATCH v2 2/4 x86/mm: skip super-page alignment checks for non-present entries

In reply to Roger Pau Monne

INVALID_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.

Fixes 0b6b51a69f4d ('xen/mm: Switch map_pages_to_xen to use MFN typesafe')

---
Changes since v1:
- Detect non-present entries from the flags contents rather than checking the mfn parameter.

---
 xen/arch/x86/mm.c | 22 ++++++++++++++++------
 1 file changed, 16 insertions(+), 6 deletions(-)
xen/arch/x86/mm.c +16 -6
index 8afb63c855b9..64b8054891da 100644
@@ -5232,9 +5232,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)
 
@@ -5255,7 +5263,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)) )
         {
@@ -5374,7 +5383,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)) )
         {
@@ -5544,7 +5553,8 @@ int map_pages_to_xen(
  check_l3:
         if ( cpu_has_page1gb &&
              (flags == PAGE_HYPERVISOR) &&
-             ((nr_mfns == 0) || IS_L3E_ALIGNED(virt, mfn)) )
+             ((nr_mfns == 0) || !(flags & _PAGE_PRESENT) ||
+              IS_L3E_ALIGNED(virt, mfn)) )
         {
             unsigned long base_mfn;
             const l2_pgentry_t *l2t;
Signature · 1 line
2.46.0
Reviewed-by on v2 2/4
#5 lore raw

Re: [PATCH v2 2/4] x86/mm: skip super-page alignment checks for non-present entries

xen/arch/x86/mm.c int map_pages_to_xen( · line 5560

Quoted 10 lines · Roger Pau Monne
xen/arch/x86/mm.c +2 -1
@@ -5544,7 +5553,8 @@ int map_pages_to_xen(
  check_l3:
         if ( cpu_has_page1gb &&
              (flags == PAGE_HYPERVISOR) &&
-             ((nr_mfns == 0) || IS_L3E_ALIGNED(virt, mfn)) )
+             ((nr_mfns == 0) || !(flags & _PAGE_PRESENT) ||
+              IS_L3E_ALIGNED(virt, mfn)) )
         {
             unsigned long base_mfn;
             const l2_pgentry_t *l2t;

With the "flags == PAGE_HYPERVISOR" check I don't think any change is needed here? With this dropped (again: uncertain whether to offer making the adjustment while committing)

Jan

#6 lore raw

PATCH v2 3/4 x86/setup: remove bootstrap_map_addr() usage of destroy_xen_mappings()

In reply to Roger Pau Monne

bootstrap_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.

Fixes 4376c05c3113 ('x86-64: use 1GB pages in 1:1 mapping if available')

---
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
index eac8488c4ca5..be7ca26b3dd5 100644
@@ -461,7 +461,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
#7 lore raw

PATCH v2 4/4 x86/mm: ensure L2 is always freed if empty

In reply to Roger Pau Monne

The 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.

Fixes 4376c05c3113 ('x86-64: use 1GB pages in 1:1 mapping if available')
---
 xen/arch/x86/mm.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
xen/arch/x86/mm.c +1 -1
index 64b8054891da..489c71150255 100644
@@ -5731,7 +5731,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