Skip to contentSkip to conversation

xen: arch_mfn_in_directmap: Reconciliate the name and the implementation

PATCH In review Julien Grall 2 participants 3 messages Dec 16, 2021

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 xen: arch_mfn_in_directmap: Reconciliate the name and the implementation

From: Julien Grall <[email protected]>

The name of arch_mfn_in_directmap() suggests that it will check against that the passed MFN should be in the directmap.

However, the current callers are passing the next MFN and the implementation will return true for up to one MFN past the directmap.

It would be more meaningful to test the exact MFN rather than the next one. So rework the implementation and the caller to match the name.

---

Looking at the history, it looks like the check in init_node_heap() was <= and it was simply moved to a new helper without any adjustment as part of c6fdc9696a "boot allocator: use arch helper for virt_to_mfn on DIRECTMAP_VIRT region".

---
 xen/arch/x86/include/asm/mm.h | 2 +-
 xen/common/page_alloc.c       | 4 ++--
 2 files changed, 3 insertions(+), 3 deletions(-)
xen/arch/x86/include/asm/mm.h +1 -1
index 5dbcee869624..a38e13e3c11e 100644
@@ -641,7 +641,7 @@ static inline bool arch_mfn_in_directmap(unsigned long mfn)
 {
     unsigned long eva = min(DIRECTMAP_VIRT_END, HYPERVISOR_VIRT_END);
 
-    return mfn <= (virt_to_mfn(eva - 1) + 1);
+    return mfn < (virt_to_mfn(eva - 1) + 1);
 }
 
 #endif /* __ASM_X86_MM_H__ */
xen/common/page_alloc.c +2 -2
index 38eea879c04a..d4789f81cd31 100644
@@ -588,7 +588,7 @@ static unsigned long init_node_heap(int node, unsigned long mfn,
         needed = 0;
     }
     else if ( *use_tail && nr >= needed &&
-              arch_mfn_in_directmap(mfn + nr) &&
+              arch_mfn_in_directmap(mfn + nr - 1) &&
               (!xenheap_bits ||
                !((mfn + nr - 1) >> (xenheap_bits - PAGE_SHIFT))) )
     {
@@ -597,7 +597,7 @@ static unsigned long init_node_heap(int node, unsigned long mfn,
                       PAGE_SIZE - sizeof(**avail) * NR_ZONES;
     }
     else if ( nr >= needed &&
-              arch_mfn_in_directmap(mfn + needed) &&
+              arch_mfn_in_directmap(mfn + needed - 1) &&
               (!xenheap_bits ||
                !((mfn + needed - 1) >> (xenheap_bits - PAGE_SHIFT))) )
     {
Signature · 1 line
2.32.0
#1 lore raw
Quoted 12 lines · Julien Grall

From: Julien Grall <[email protected]>

The name of arch_mfn_in_directmap() suggests that it will check against that the passed MFN should be in the directmap.

However, the current callers are passing the next MFN and the implementation will return true for up to one MFN past the directmap.

It would be more meaningful to test the exact MFN rather than the next one. So rework the implementation and the caller to match the name.

Quoted 8 lines

---

Looking at the history, it looks like the check in init_node_heap() was <= and it was simply moved to a new helper without any adjustment as part of c6fdc9696a "boot allocator: use arch helper for virt_to_mfn on DIRECTMAP_VIRT region".

Really the original intention was to check the entire range; maybe it would be better to express this again by ...

xen/arch/x86/include/asm/mm.h static inline bool arch_mfn_in_directmap(unsigned long mfn) · line 645

Quoted 9 lines
xen/arch/x86/include/asm/mm.h +1 -1
@@ -641,7 +641,7 @@ static inline bool arch_mfn_in_directmap(unsigned long mfn)
 {
     unsigned long eva = min(DIRECTMAP_VIRT_END, HYPERVISOR_VIRT_END);
 
-    return mfn <= (virt_to_mfn(eva - 1) + 1);
+    return mfn < (virt_to_mfn(eva - 1) + 1);
 }

... this instead:

static inline bool arch_mfns_in_directmap(unsigned long mfn, unsigned long nr)

{
    unsigned long eva = min(DIRECTMAP_VIRT_END, HYPERVISOR_VIRT_END);

    return mfn + nr <= (virt_to_mfn(eva - 1) + 1);
}

This would allow an arch hook to also go beyond verifying just the last MFN.

Jan

#2 lore raw

Hi Jan,

Quoted 38 lines · Jan Beulich
Quoted 12 lines · Julien Grall

From: Julien Grall <[email protected]>

The name of arch_mfn_in_directmap() suggests that it will check against that the passed MFN should be in the directmap.

However, the current callers are passing the next MFN and the implementation will return true for up to one MFN past the directmap.

It would be more meaningful to test the exact MFN rather than the next one. So rework the implementation and the caller to match the name.

Quoted 8 lines

---

Looking at the history, it looks like the check in init_node_heap() was <= and it was simply moved to a new helper without any adjustment as part of c6fdc9696a "boot allocator: use arch helper for virt_to_mfn on DIRECTMAP_VIRT region".

Really the original intention was to check the entire range; maybe it would be better to express this again by ...

xen/arch/x86/include/asm/mm.h static inline bool arch_mfn_in_directmap(unsigned long mfn) · line 645

Quoted 9 lines
xen/arch/x86/include/asm/mm.h +1 -1
@@ -641,7 +641,7 @@ static inline bool arch_mfn_in_directmap(unsigned long mfn)
  {
      unsigned long eva = min(DIRECTMAP_VIRT_END, HYPERVISOR_VIRT_END);
 
-    return mfn <= (virt_to_mfn(eva - 1) + 1);
+    return mfn < (virt_to_mfn(eva - 1) + 1);
  }

... this instead:

static inline bool arch_mfns_in_directmap(unsigned long mfn, unsigned long nr)

Quoted 8 lines
{
     unsigned long eva = min(DIRECTMAP_VIRT_END, HYPERVISOR_VIRT_END);

     return mfn + nr <= (virt_to_mfn(eva - 1) + 1);
}

This would allow an arch hook to also go beyond verifying just the last MFN.

I am fine with this approach. I will respin the patch.

Cheers,

Signature · 1 line
Julien Grall