Skip to contentSkip to conversation

purge free_shared_domheap_page()

PATCH Superseded Paul Durrant 6 participants 22 messages Jan 24, 2020 – Jan 29, 2020

v4 of 9 Superseded Latest is v9 →

lore
Share & tools
lore
#0 lore raw

v4 cover purge free_shared_domheap_page()

The last patch is unrelated cleanup for something I happened to notice along the way.

Paul Durrant (7):
x86 / vmx: make apic_access_mfn type-safe x86 / hvm: add domain_relinquish_resources() method x86 / hvm: make domain_destroy() method optional x86 / vmx: move teardown from domain_destroy()...
mm: make MEMF_no_refcount pages safe to assign x86 / vmx: use a MEMF_no_refcount domheap page for
APIC_DEFAULT_PHYS_BASE
mm: remove donate_page()

 xen/arch/arm/mm.c                  |  6 ----
 xen/arch/x86/hvm/hvm.c             |  7 +++-
 xen/arch/x86/hvm/mtrr.c            |  2 +-
 xen/arch/x86/hvm/svm/svm.c         |  5 ---
 xen/arch/x86/hvm/vmx/vmx.c         | 37 +++++++++++++++-------
 xen/arch/x86/mm.c                  | 51 ------------------------------
 xen/common/page_alloc.c            | 39 ++++++++++++++++-------
 xen/include/asm-arm/mm.h           |  5 ++-
 xen/include/asm-x86/hvm/hvm.h      |  1 +
 xen/include/asm-x86/hvm/vmx/vmcs.h |  2 +-
 xen/include/asm-x86/mm.h           |  9 +++---
 xen/include/xen/mm.h               |  2 --
 12 files changed, 72 insertions(+), 94 deletions(-)
Signature · 7 lines
2.20.1


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#1 lore raw

PATCH v4 1/7 x86 / vmx: make apic_access_mfn type-safe

Use mfn_t rather than unsigned long and change previous tests against 0 to tests against INVALID_MFN (also introducing initialization to that value).

---
Cc: Andrew Cooper <[email protected]>
Cc: Wei Liu <[email protected]>
Cc: "Roger Pau Monné" <[email protected]>
Cc: Jun Nakajima <[email protected]>

v3:
- Use _mfn(0) instead of INVALID_MFN

v2:
- Set apic_access_mfn to INVALID_MFN in vmx_free_vlapic_mapping() to make the function idempotent

---
 xen/arch/x86/hvm/mtrr.c            |  2 +-
 xen/arch/x86/hvm/vmx/vmx.c         | 14 +++++++-------
 xen/include/asm-x86/hvm/vmx/vmcs.h |  2 +-
 3 files changed, 9 insertions(+), 9 deletions(-)
xen/arch/x86/hvm/mtrr.c +1 -1
index 5ad15eafe0..8356e8de3d 100644
@@ -818,7 +818,7 @@ int epte_get_entry_emt(struct domain *d, unsigned long gfn, mfn_t mfn,
 
     if ( direct_mmio )
     {
-        if ( (mfn_x(mfn) ^ d->arch.hvm.vmx.apic_access_mfn) >> order )
+        if ( (mfn_x(mfn) ^ mfn_x(d->arch.hvm.vmx.apic_access_mfn)) >> order )
             return MTRR_TYPE_UNCACHABLE;
         if ( order )
             return -1;
xen/arch/x86/hvm/vmx/vmx.c +7 -7
index f83f102638..b262d38a7c 100644
@@ -3034,7 +3034,7 @@ static int vmx_alloc_vlapic_mapping(struct domain *d)
     mfn = page_to_mfn(pg);
     clear_domain_page(mfn);
     share_xen_page_with_guest(pg, d, SHARE_rw);
-    d->arch.hvm.vmx.apic_access_mfn = mfn_x(mfn);
+    d->arch.hvm.vmx.apic_access_mfn = mfn;
 
     return set_mmio_p2m_entry(d, paddr_to_pfn(APIC_DEFAULT_PHYS_BASE), mfn,
                               PAGE_ORDER_4K,
@@ -3043,24 +3043,24 @@ static int vmx_alloc_vlapic_mapping(struct domain *d)
 
 static void vmx_free_vlapic_mapping(struct domain *d)
 {
-    unsigned long mfn = d->arch.hvm.vmx.apic_access_mfn;
+    mfn_t mfn = d->arch.hvm.vmx.apic_access_mfn;
 
-    if ( mfn != 0 )
-        free_shared_domheap_page(mfn_to_page(_mfn(mfn)));
+    d->arch.hvm.vmx.apic_access_mfn = _mfn(0);
+    if ( !mfn_eq(mfn, _mfn(0)) )
+        free_shared_domheap_page(mfn_to_page(mfn));
 }
 
 static void vmx_install_vlapic_mapping(struct vcpu *v)
 {
     paddr_t virt_page_ma, apic_page_ma;
 
-    if ( v->domain->arch.hvm.vmx.apic_access_mfn == 0 )
+    if ( mfn_eq(v->domain->arch.hvm.vmx.apic_access_mfn, _mfn(0)) )
         return;
 
     ASSERT(cpu_has_vmx_virtualize_apic_accesses);
 
     virt_page_ma = page_to_maddr(vcpu_vlapic(v)->regs_page);
-    apic_page_ma = v->domain->arch.hvm.vmx.apic_access_mfn;
-    apic_page_ma <<= PAGE_SHIFT;
+    apic_page_ma = mfn_to_maddr(v->domain->arch.hvm.vmx.apic_access_mfn);
 
     vmx_vmcs_enter(v);
     __vmwrite(VIRTUAL_APIC_PAGE_ADDR, virt_page_ma);
xen/include/asm-x86/hvm/vmx/vmcs.h +1 -1
index a514299144..be4661a929 100644
@@ -59,7 +59,7 @@ struct ept_data {
 #define _VMX_DOMAIN_PML_ENABLED    0
 #define VMX_DOMAIN_PML_ENABLED     (1ul << _VMX_DOMAIN_PML_ENABLED)
 struct vmx_domain {
-    unsigned long apic_access_mfn;
+    mfn_t apic_access_mfn;
     /* VMX_DOMAIN_* */
     unsigned int status;
 
Signature · 7 lines
2.20.1


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#2 lore raw

PATCH v4 2/7 x86 / hvm: add domain_relinquish_resources() method

In reply to Paul Durrant

There are two functions in hvm.c to deal with tear-down and a domain:
hvm_domain_relinquish_resources() and hvm_domain_destroy(). However, only the latter has an associated method in 'hvm_funcs'. This patch adds a method for the former.

A subsequent patch will define a VMX implementation.

---
Cc: Andrew Cooper <[email protected]>
Cc: Wei Liu <[email protected]>
Cc: "Roger Pau Monné" <[email protected]>
Cc: Jun Nakajima <[email protected]>
Cc: Kevin Tian <[email protected]>

v2:
- Make the new method optional and make it an alternative_vcall

---
 xen/arch/x86/hvm/hvm.c        | 3 +++
 xen/include/asm-x86/hvm/hvm.h | 1 +
 2 files changed, 4 insertions(+)
xen/arch/x86/hvm/hvm.c +3 -0
index d899594888..6333ae6aba 100644
@@ -716,6 +716,9 @@ int hvm_domain_initialise(struct domain *d)
 
 void hvm_domain_relinquish_resources(struct domain *d)
 {
+    if ( hvm_funcs.domain_relinquish_resources )
+        alternative_vcall(hvm_funcs.domain_relinquish_resources, d);
+
     if ( hvm_funcs.nhvm_domain_relinquish_resources )
         hvm_funcs.nhvm_domain_relinquish_resources(d);
 
xen/include/asm-x86/hvm/hvm.h +1 -0
index 09793c12e9..9eab1d7493 100644
@@ -107,6 +107,7 @@ struct hvm_function_table {
      * Initialise/destroy HVM domain/vcpu resources
      */
     int  (*domain_initialise)(struct domain *d);
+    void (*domain_relinquish_resources)(struct domain *d);
     void (*domain_destroy)(struct domain *d);
     int  (*vcpu_initialise)(struct vcpu *v);
     void (*vcpu_destroy)(struct vcpu *v);
Signature · 7 lines
2.20.1


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#3 lore raw

PATCH v4 3/7 x86 / hvm: make domain_destroy() method optional

In reply to Paul Durrant

This method is currently empty for SVM so make it optional and, while in the neighbourhood, make it an alternative_vcall().

---
Cc: Jan Beulich <[email protected]>
Cc: Andrew Cooper <[email protected]>
Cc: Wei Liu <[email protected]>
Cc: "Roger Pau Monné" <[email protected]>
Cc: George Dunlap <[email protected]>

v4:
- New in v4 (disaggregated from v3 patch #3)

---
 xen/arch/x86/hvm/hvm.c     | 4 +++-
 xen/arch/x86/hvm/svm/svm.c | 5 -----
 2 files changed, 3 insertions(+), 6 deletions(-)
xen/arch/x86/hvm/hvm.c +3 -1
index 6333ae6aba..0b93609a82 100644
@@ -747,7 +747,9 @@ void hvm_domain_destroy(struct domain *d)
 
     hvm_destroy_cacheattr_region_list(d);
 
-    hvm_funcs.domain_destroy(d);
+    if ( hvm_funcs.domain_destroy )
+        alternative_vcall(hvm_funcs.domain_destroy, d);
+
     rtc_deinit(d);
     stdvga_deinit(d);
     vioapic_deinit(d);
xen/arch/x86/hvm/svm/svm.c +0 -5
index b1c376d455..b7f67f9f03 100644
@@ -1155,10 +1155,6 @@ static int svm_domain_initialise(struct domain *d)
     return 0;
 }
 
-static void svm_domain_destroy(struct domain *d)
-{
-}
-
 static int svm_vcpu_initialise(struct vcpu *v)
 {
     int rc;
@@ -2425,7 +2421,6 @@ static struct hvm_function_table __initdata svm_function_table = {
     .cpu_up               = svm_cpu_up,
     .cpu_down             = svm_cpu_down,
     .domain_initialise    = svm_domain_initialise,
-    .domain_destroy       = svm_domain_destroy,
     .vcpu_initialise      = svm_vcpu_initialise,
     .vcpu_destroy         = svm_vcpu_destroy,
     .save_cpu_ctxt        = svm_save_vmcb_ctxt,
Signature · 7 lines
2.20.1


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#5 lore raw

PATCH v4 4/7 x86 / vmx: move teardown from domain_destroy()...

In reply to Paul Durrant

... to domain_relinquish_resources().

The teardown code frees the APICv page. This does not need to be done late so do it in domain_relinquish_resources() rather than domain_destroy().

---
Cc: Jun Nakajima <[email protected]>
Cc: Kevin Tian <[email protected]>
Cc: Jan Beulich <[email protected]>
Cc: Andrew Cooper <[email protected]>
Cc: Wei Liu <[email protected]>
Cc: "Roger Pau Monné" <[email protected]>
Cc: George Dunlap <[email protected]>

v4:
- New in v4 (disaggregated from v3 patch #3)

---
 xen/arch/x86/hvm/vmx/vmx.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
xen/arch/x86/hvm/vmx/vmx.c +2 -2
index b262d38a7c..606f3dc2eb 100644
@@ -419,7 +419,7 @@ static int vmx_domain_initialise(struct domain *d)
     return 0;
 }
 
-static void vmx_domain_destroy(struct domain *d)
+static void vmx_domain_relinquish_resources(struct domain *d)
 {
     if ( !has_vlapic(d) )
         return;
@@ -2240,7 +2240,7 @@ static struct hvm_function_table __initdata vmx_function_table = {
     .cpu_up_prepare       = vmx_cpu_up_prepare,
     .cpu_dead             = vmx_cpu_dead,
     .domain_initialise    = vmx_domain_initialise,
-    .domain_destroy       = vmx_domain_destroy,
+    .domain_relinquish_resources = vmx_domain_relinquish_resources,
     .vcpu_initialise      = vmx_vcpu_initialise,
     .vcpu_destroy         = vmx_vcpu_destroy,
     .save_cpu_ctxt        = vmx_save_vmcs_ctxt,
Signature · 7 lines
2.20.1


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#9 lore raw

PATCH v4 5/7 mm: make MEMF_no_refcount pages safe to assign

In reply to Paul Durrant

Currently it is unsafe to assign a domheap page allocated with MEMF_no_refcount to a domain because the domain't 'tot_pages' will not be incremented, but will be decrement when the page is freed (since free_domheap_pages() has no way of telling that the increment was skipped).

This patch allocates a new 'count_info' bit for a PGC_no_refcount flag which is then used to mark domheap pages allocated with MEMF_no_refcount. This then allows free_domheap_pages() to skip decrementing tot_pages when appropriate and hence makes the pages safe to assign.

NOTE: The patch sets MEMF_no_refcount directly in alloc_domheap_pages()
rather than in assign_pages() because the latter is called with MEMF_no_refcount by memory_exchange() as an optimization, to avoid too many calls to domain_adjust_tot_pages() (which acquires and releases the global 'heap_lock').

---
Cc: Andrew Cooper <[email protected]>
Cc: George Dunlap <[email protected]>
Cc: Ian Jackson <[email protected]>
Cc: Jan Beulich <[email protected]>
Cc: Julien Grall <[email protected]>
Cc: Konrad Rzeszutek Wilk <[email protected]>
Cc: Stefano Stabellini <[email protected]>
Cc: Wei Liu <[email protected]>
Cc: Volodymyr Babchuk <[email protected]>
Cc: "Roger Pau Monné" <[email protected]>

v4:
- New in v4

---
 xen/common/page_alloc.c  | 39 ++++++++++++++++++++++++++++-----------
 xen/include/asm-arm/mm.h |  5 ++++-
 xen/include/asm-x86/mm.h |  7 +++++--
 3 files changed, 37 insertions(+), 14 deletions(-)
xen/common/page_alloc.c +28 -11
index 919a270587..6103f14a4e 100644
@@ -460,6 +460,9 @@ unsigned long domain_adjust_tot_pages(struct domain *d, long pages)
 {
     long dom_before, dom_after, dom_claimed, sys_before, sys_after;
 
+    if ( !pages )
+        goto out;
+
     ASSERT(spin_is_locked(&d->page_alloc_lock));
     d->tot_pages += pages;
 
@@ -1389,6 +1392,8 @@ static void free_heap_pages(
 
     for ( i = 0; i < (1 << order); i++ )
     {
+        ASSERT(!(pg[i].count_info & PGC_no_refcount));
+
         /*
          * Cannot assume that count_info == 0, as there are some corner cases
          * where it isn't the case and yet it isn't a bug:
@@ -2314,11 +2319,6 @@ struct page_info *alloc_domheap_pages(
 
     if ( memflags & MEMF_no_owner )
         memflags |= MEMF_no_refcount;
-    else if ( (memflags & MEMF_no_refcount) && d )
-    {
-        ASSERT(!(memflags & MEMF_no_refcount));
-        return NULL;
-    }
 
     if ( !dma_bitsize )
         memflags &= ~MEMF_no_dma;
@@ -2331,11 +2331,20 @@ struct page_info *alloc_domheap_pages(
                                   memflags, d)) == NULL)) )
          return NULL;
 
-    if ( d && !(memflags & MEMF_no_owner) &&
-         assign_pages(d, pg, order, memflags) )
+    if ( d && !(memflags & MEMF_no_owner) )
     {
-        free_heap_pages(pg, order, memflags & MEMF_no_scrub);
-        return NULL;
+        if ( assign_pages(d, pg, order, memflags) )
+        {
+            free_heap_pages(pg, order, memflags & MEMF_no_scrub);
+            return NULL;
+        }
+        if ( memflags & MEMF_no_refcount )
+        {
+            unsigned long i;
+
+            for ( i = 0; i < (1 << order); i++ )
+                pg[i].count_info |= PGC_no_refcount;
+        }
     }
 
     return pg;
@@ -2368,24 +2377,32 @@ void free_domheap_pages(struct page_info *pg, unsigned int order)
 
         if ( likely(d) && likely(d != dom_cow) )
         {
+            long pages = 0;
+
             /* NB. May recursively lock from relinquish_memory(). */
             spin_lock_recursive(&d->page_alloc_lock);
 
             for ( i = 0; i < (1 << order); i++ )
             {
+                unsigned long count_info = pg[i].count_info;
+
                 if ( pg[i].u.inuse.type_info & PGT_count_mask )
                 {
                     printk(XENLOG_ERR
                            "pg[%u] MFN %"PRI_mfn" c=%#lx o=%u v=%#lx t=%#x\n",
                            i, mfn_x(page_to_mfn(pg + i)),
-                           pg[i].count_info, pg[i].v.free.order,
+                           count_info, pg[i].v.free.order,
                            pg[i].u.free.val, pg[i].tlbflush_timestamp);
                     BUG();
                 }
                 arch_free_heap_page(d, &pg[i]);
+                if ( count_info & PGC_no_refcount )
+                    pg[i].count_info &= ~PGC_no_refcount;
+                else
+                    pages--;
             }
 
-            drop_dom_ref = !domain_adjust_tot_pages(d, -(1 << order));
+            drop_dom_ref = !domain_adjust_tot_pages(d, pages);
 
             spin_unlock_recursive(&d->page_alloc_lock);
 
xen/include/asm-arm/mm.h +4 -1
index 333efd3a60..1076cc9713 100644
@@ -119,9 +119,12 @@ struct page_info
 #define PGC_state_offlined PG_mask(2, 9)
 #define PGC_state_free    PG_mask(3, 9)
 #define page_state_is(pg, st) (((pg)->count_info&PGC_state) == PGC_state_##st)
+/* Page is not reference counted */
+#define _PGC_no_refcount  PG_shift(10)
+#define PGC_no_refcount   PG_mask(1, 10)
 
 /* Count of references to this frame. */
-#define PGC_count_width   PG_shift(9)
+#define PGC_count_width   PG_shift(10)
 #define PGC_count_mask    ((1UL<<PGC_count_width)-1)
 
 /*
xen/include/asm-x86/mm.h +5 -2
index 2ca8882ad0..e75feea15e 100644
@@ -77,9 +77,12 @@
 #define PGC_state_offlined PG_mask(2, 9)
 #define PGC_state_free    PG_mask(3, 9)
 #define page_state_is(pg, st) (((pg)->count_info&PGC_state) == PGC_state_##st)
+/* Page is not reference counted */
+#define _PGC_no_refcount  PG_shift(10)
+#define PGC_no_refcount   PG_mask(1, 10)
 
- /* Count of references to this frame. */
-#define PGC_count_width   PG_shift(9)
+/* Count of references to this frame. */
+#define PGC_count_width   PG_shift(10)
 #define PGC_count_mask    ((1UL<<PGC_count_width)-1)
 
 /*
Signature · 7 lines
2.20.1


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#15 lore raw

PATCH v4 6/7 x86 / vmx: use a MEMF_no_refcount domheap page for APIC_DEFAULT_PHYS_BASE

In reply to Paul Durrant

vmx_alloc_vlapic_mapping() currently contains some very odd looking code that allocates a MEMF_no_owner domheap page and then shares with the guest as if it were a xenheap page. This then requires vmx_free_vlapic_mapping() to call a special function in the mm code: free_shared_domheap_page().

By using a MEMF_no_refcount domheap page instead, the odd looking code in vmx_alloc_vlapic_mapping() can simply use get_page_and_type() to set up a writable mapping before insertion in the P2M and vmx_free_vlapic_mapping() can simply release the page using put_page_alloc_ref() followed by put_page_and_type(). This then allows free_shared_domheap_page() to be purged.

---
Cc: Jun Nakajima <[email protected]>
Cc: Kevin Tian <[email protected]>
Cc: Jan Beulich <[email protected]>
Cc: Andrew Cooper <[email protected]>
Cc: Wei Liu <[email protected]>
Cc: "Roger Pau Monné" <[email protected]>

v4:
- Use a MEMF_no_refcount page rather than a 'normal' page

v2:
- Set an initial value for max_pages rather than avoiding the check in assign_pages()
- Make domain_destroy() optional

---
 xen/arch/x86/hvm/vmx/vmx.c | 21 ++++++++++++++++++---
 xen/arch/x86/mm.c          | 10 ----------
 xen/include/asm-x86/mm.h   |  2 --
 3 files changed, 18 insertions(+), 15 deletions(-)
xen/arch/x86/hvm/vmx/vmx.c +18 -3
index 606f3dc2eb..7423d2421b 100644
@@ -3028,12 +3028,22 @@ static int vmx_alloc_vlapic_mapping(struct domain *d)
     if ( !cpu_has_vmx_virtualize_apic_accesses )
         return 0;
 
-    pg = alloc_domheap_page(d, MEMF_no_owner);
+    pg = alloc_domheap_page(d, MEMF_no_refcount);
     if ( !pg )
         return -ENOMEM;
+
+    if ( !get_page_and_type(pg, d, PGT_writable_page) )
+    {
+        /*
+         * The domain can't possibly know about this page yet, so failure
+         * here is a clear indication of something fishy going on.
+         */
+        domain_crash(d);
+        return -ENODATA;
+    }
+
     mfn = page_to_mfn(pg);
     clear_domain_page(mfn);
-    share_xen_page_with_guest(pg, d, SHARE_rw);
     d->arch.hvm.vmx.apic_access_mfn = mfn;
 
     return set_mmio_p2m_entry(d, paddr_to_pfn(APIC_DEFAULT_PHYS_BASE), mfn,
@@ -3047,7 +3057,12 @@ static void vmx_free_vlapic_mapping(struct domain *d)
 
     d->arch.hvm.vmx.apic_access_mfn = _mfn(0);
     if ( !mfn_eq(mfn, _mfn(0)) )
-        free_shared_domheap_page(mfn_to_page(mfn));
+    {
+        struct page_info *pg = mfn_to_page(mfn);
+
+        put_page_alloc_ref(pg);
+        put_page_and_type(pg);
+    }
 }
 
 static void vmx_install_vlapic_mapping(struct vcpu *v)
xen/arch/x86/mm.c +0 -10
index 654190e9e9..2a6d2e8af9 100644
@@ -496,16 +496,6 @@ void share_xen_page_with_guest(struct page_info *page, struct domain *d,
     spin_unlock(&d->page_alloc_lock);
 }
 
-void free_shared_domheap_page(struct page_info *page)
-{
-    put_page_alloc_ref(page);
-    if ( !test_and_clear_bit(_PGC_xen_heap, &page->count_info) )
-        ASSERT_UNREACHABLE();
-    page->u.inuse.type_info = 0;
-    page_set_owner(page, NULL);
-    free_domheap_page(page);
-}
-
 void make_cr3(struct vcpu *v, mfn_t mfn)
 {
     struct domain *d = v->domain;
xen/include/asm-x86/mm.h +0 -2
index e75feea15e..036d7ac22f 100644
@@ -320,8 +320,6 @@ struct page_info
 
 #define maddr_get_owner(ma)   (page_get_owner(maddr_to_page((ma))))
 
-extern void free_shared_domheap_page(struct page_info *page);
-
 #define frame_table ((struct page_info *)FRAMETABLE_VIRT_START)
 extern unsigned long max_page;
 extern unsigned long total_pages;
Signature · 7 lines
2.20.1


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#17 lore raw

PATCH v4 7/7 mm: remove donate_page()

In reply to Paul Durrant

This function appears to have no callers.

---
Cc: Stefano Stabellini <[email protected]>
Cc: Julien Grall <[email protected]>
Cc: Volodymyr Babchuk <[email protected]>
Cc: Andrew Cooper <[email protected]>
Cc: George Dunlap <[email protected]>
Cc: Ian Jackson <[email protected]>
Cc: Jan Beulich <[email protected]>
Cc: Konrad Rzeszutek Wilk <[email protected]>
Cc: Wei Liu <[email protected]>
Cc: "Roger Pau Monné" <[email protected]>

v4:
- New in v4

---
 xen/arch/arm/mm.c    |  6 ------
 xen/arch/x86/mm.c    | 41 -----------------------------------------
 xen/include/xen/mm.h |  2 --
 3 files changed, 49 deletions(-)
xen/arch/arm/mm.c +0 -6
index 4d6c971f37..727107eefa 100644
@@ -1353,12 +1353,6 @@ void arch_dump_shared_mem_info(void)
 {
 }
 
-int donate_page(struct domain *d, struct page_info *page, unsigned int memflags)
-{
-    ASSERT_UNREACHABLE();
-    return -ENOSYS;
-}
-
 int steal_page(
     struct domain *d, struct page_info *page, unsigned int memflags)
 {
xen/arch/x86/mm.c +0 -41

47 lines not on this page · raw message

xen/include/xen/mm.h +0 -2
index 8d0ddfb60c..d0d095d9c7 100644
@@ -599,8 +599,6 @@ int xenmem_add_to_physmap(struct domain *d, struct xen_add_to_physmap *xatp,
 int __must_check guest_remove_page(struct domain *d, unsigned long gmfn);
 int __must_check steal_page(struct domain *d, struct page_info *page,
                             unsigned int memflags);
-int __must_check donate_page(struct domain *d, struct page_info *page,
-                             unsigned int memflags);
 
 #define RAM_TYPE_CONVENTIONAL 0x00000001
 #define RAM_TYPE_RESERVED     0x00000002
Signature · 7 lines
2.20.1


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Acked-by on v4 3/7
#4 lore raw

Re: [Xen-devel] [PATCH v4 3/7] x86 / hvm: make domain_destroy() method optional

In reply to Paul Durrant
Quoted 4 lines · Paul Durrant

This method is currently empty for SVM so make it optional and, while in the neighbourhood, make it an alternative_vcall().

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

Acked-by on v4 7/7
#19 lore raw

Re: [Xen-devel] [PATCH v4 7/7] mm: remove donate_page()

In reply to Paul Durrant

Hi Paul,

Quoted 3 lines · Paul Durrant

This function appears to have no callers.

Cheers,

Signature · 6 lines
Julien Grall

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#20 lore raw

Re: [Xen-devel] [PATCH v4 7/7] mm: remove donate_page()

In reply to Paul Durrant
Quoted 3 lines · Paul Durrant

This function appears to have no callers.

Looks like tmem was the sole user.  donate_page() was introduced by 6009f4ddb2 and the last caller was dropped by c492e19fdd.

This patch is standalone, and I can tweak the commit message on commit, if you're happy with that.

~Andrew

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#21 lore raw

Re: [Xen-devel] [PATCH v4 7/7] mm: remove donate_page()

Sent from Workspace ONE Boxer

Quoted 4 lines · Andrew Cooper
Quoted 2 lines · Paul Durrant

This function appears to have no callers.

Quoted 8 lines
Quoted 1 line

Looks like tmem was the sole user. donate_page() was introduced by 6009f4ddb2 and the last caller was dropped by c492e19fdd.

This patch is standalone, and I can tweak the commit message on commit, if you're happy with that.

Fine with me.

Paul

Quoted 1 line

~Andrew

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#6 lore raw

Re: [Xen-devel] [PATCH v4 4/7] x86 / vmx: move teardown from domain_destroy()...

In reply to Paul Durrant
Quoted 4 lines · Paul Durrant

... to domain_relinquish_resources().

The teardown code frees the APICv page. This does not need to be done late so do it in domain_relinquish_resources() rather than domain_destroy().

For the normal domain destruction path this is fine. For the error path of domain_create(), however, this will leak the page, as in this case hvm_domain_relinquish_resources() won't be called. I'm afraid there already is a similar issue with e.g. viridian_domain_deinit(). I guess I'll make a patch.

Jan

Quoted 31 lines

---
Cc: Jun Nakajima <[email protected]>
Cc: Kevin Tian <[email protected]>
Cc: Jan Beulich <[email protected]>
Cc: Andrew Cooper <[email protected]>
Cc: Wei Liu <[email protected]>
Cc: "Roger Pau Monné" <[email protected]>
Cc: George Dunlap <[email protected]>

v4:
- New in v4 (disaggregated from v3 patch #3)

---
 xen/arch/x86/hvm/vmx/vmx.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
xen/arch/x86/hvm/vmx/vmx.c +1 -1
index b262d38a7c..606f3dc2eb 100644
@@ -419,7 +419,7 @@ static int vmx_domain_initialise(struct domain *d)
     return 0;
 }
 
-static void vmx_domain_destroy(struct domain *d)
+static void vmx_domain_relinquish_resources(struct domain *d)
 {
     if ( !has_vlapic(d) )
         return;

xen/arch/x86/hvm/vmx/vmx.c static struct hvm_function_table __initdata vmx_function_table = { · line 2246

Quoted 8 lines
xen/arch/x86/hvm/vmx/vmx.c +1 -1
@@ -2240,7 +2240,7 @@ static struct hvm_function_table __initdata vmx_function_table = {
     .cpu_up_prepare       = vmx_cpu_up_prepare,
     .cpu_dead             = vmx_cpu_dead,
     .domain_initialise    = vmx_domain_initialise,
-    .domain_destroy       = vmx_domain_destroy,
+    .domain_relinquish_resources = vmx_domain_relinquish_resources,
     .vcpu_initialise      = vmx_vcpu_initialise,
     .vcpu_destroy         = vmx_vcpu_destroy,
     .save_cpu_ctxt        = vmx_save_vmcs_ctxt,

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#7 lore raw

Re: [Xen-devel] [PATCH v4 4/7] x86 / vmx: move teardown from domain_destroy()...

Quoted 14 lines

-----Original Message-----
From: Jan Beulich <[email protected]>
Sent: 28 January 2020 08:15
To: Durrant, Paul <[email protected]>
Cc: [email protected]; Jun Nakajima <[email protected]>; Kevin Tian <[email protected]>; Andrew Cooper <[email protected]>; Wei Liu <[email protected]>; Roger Pau Monné <[email protected]>; George Dunlap <[email protected]>
Subject: Re: [PATCH v4 4/7] x86 / vmx: move teardown from domain_destroy()...

Quoted 2 lines · Paul Durrant

... to domain_relinquish_resources().

Quoted 7 lines
Quoted 1 line

The teardown code frees the APICv page. This does not need to be done

late

Quoted 1 line

so do it in domain_relinquish_resources() rather than domain_destroy().

For the normal domain destruction path this is fine. For the error path of domain_create(), however, this will leak the page, as in this case hvm_domain_relinquish_resources() won't be called.

Well it's really arch_domain_create() that's at fault but, yes that needs fixing.

Quoted 4 lines

I'm afraid there
already is a similar issue with e.g. viridian_domain_deinit(). I guess I'll make a patch.

Ok, thanks.

Paul

Quoted 26 lines

Jan

Quoted 24 lines

---
Cc: Jun Nakajima <[email protected]>
Cc: Kevin Tian <[email protected]>
Cc: Jan Beulich <[email protected]>
Cc: Andrew Cooper <[email protected]>
Cc: Wei Liu <[email protected]>
Cc: "Roger Pau Monné" <[email protected]>
Cc: George Dunlap <[email protected]>

v4:
- New in v4 (disaggregated from v3 patch #3)

---
 xen/arch/x86/hvm/vmx/vmx.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
xen/arch/x86/hvm/vmx/vmx.c +0 -0
index b262d38a7c..606f3dc2eb 100644
@@ -419,7 +419,7 @@ static int vmx_domain_initialise(struct domain *d)
     return 0;
 }
 
Quoted 16 lines

xen/arch/x86/hvm/vmx/vmx.c static struct hvm_function_table __initdata

Quoted 6 lines
xen/arch/x86/hvm/vmx/vmx.c +1 -1
-static void vmx_domain_destroy(struct domain *d)
+static void vmx_domain_relinquish_resources(struct domain *d)
 {
     if ( !has_vlapic(d) )
         return;
@@ -2240,7 +2240,7 @@ static struct hvm_function_table __initdata

vmx_function_table = {

Quoted 9 lines
xen/arch/x86/hvm/vmx/vmx.c +1 -1
     .cpu_up_prepare       = vmx_cpu_up_prepare,
     .cpu_dead             = vmx_cpu_dead,
     .domain_initialise    = vmx_domain_initialise,
-    .domain_destroy       = vmx_domain_destroy,
+    .domain_relinquish_resources = vmx_domain_relinquish_resources,
     .vcpu_initialise      = vmx_vcpu_initialise,
     .vcpu_destroy         = vmx_vcpu_destroy,
     .save_cpu_ctxt        = vmx_save_vmcs_ctxt,

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#8 lore raw

Re: [Xen-devel] [PATCH v4 4/7] x86 / vmx: move teardown from domain_destroy()...

Quoted 15 lines · Paul
Quoted 15 lines

-----Original Message-----
From: Jan Beulich <[email protected]>
Sent: 28 January 2020 08:15
To: Durrant, Paul <[email protected]>
Cc: [email protected]; Jun Nakajima <[email protected]>; Kevin Tian <[email protected]>; Andrew Cooper <[email protected]>; Wei Liu <[email protected]>; Roger Pau Monné <[email protected]>; George Dunlap <[email protected]>
Subject: Re: [PATCH v4 4/7] x86 / vmx: move teardown from domain_destroy()...

Quoted 3 lines · Paul Durrant

... to domain_relinquish_resources().

The teardown code frees the APICv page. This does not need to be done

Quoted 8 lines
Quoted 6 lines

late

Quoted 1 line

so do it in domain_relinquish_resources() rather than domain_destroy().

For the normal domain destruction path this is fine. For the error path of domain_create(), however, this will leak the page, as in this case hvm_domain_relinquish_resources() won't be called.

Well it's really arch_domain_create() that's at fault but, yes that needs fixing.

Why arch_domain_create()? For HVM domains hvm_domain_initialise() is the last thing tried, and hence no further cleanup is needed (assuming hvm_domain_initialise() cleans up in case of failure after itself). It's failures encountered by domain_create() after arch_domain_create() has succeeded which are a problem here. In this case arch_domain_destroy() will be called, but nothing down from there has called hvm_domain_relinquish_resources() so far.

Quoted 5 lines
Quoted 3 lines

I'm afraid there
already is a similar issue with e.g. viridian_domain_deinit(). I guess I'll make a patch.

Ok, thanks.

Which turned out to take more time because of other issues I've found in the course, but I think I now have something I can actually test.

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#10 lore raw

Re: [Xen-devel] [PATCH v4 5/7] mm: make MEMF_no_refcount pages safe to assign

In reply to Paul Durrant
Quoted 10 lines · Paul Durrant

Currently it is unsafe to assign a domheap page allocated with MEMF_no_refcount to a domain because the domain't 'tot_pages' will not be incremented, but will be decrement when the page is freed (since free_domheap_pages() has no way of telling that the increment was skipped).

This patch allocates a new 'count_info' bit for a PGC_no_refcount flag which is then used to mark domheap pages allocated with MEMF_no_refcount. This then allows free_domheap_pages() to skip decrementing tot_pages when appropriate and hence makes the pages safe to assign.

Quoted 5 lines

NOTE: The patch sets MEMF_no_refcount directly in alloc_domheap_pages()
rather than in assign_pages() because the latter is called with MEMF_no_refcount by memory_exchange() as an optimization, to avoid too many calls to domain_adjust_tot_pages() (which acquires and releases the global 'heap_lock').

I don't think there were any optimization thoughts with this. The MEMF_no_refcount use is because otherwise for a domain with tot_pages == max_pages the assignment would fail.

xen/common/page_alloc.c unsigned long domain_adjust_tot_pages(struct domain *d, long pages) · line 464

Quoted 8 lines
xen/common/page_alloc.c +2 -0
@@ -460,6 +460,9 @@ unsigned long domain_adjust_tot_pages(struct domain *d, long pages)
 {
     long dom_before, dom_after, dom_claimed, sys_before, sys_after;
 
+    if ( !pages )
+        goto out;

Unrelated change? Are there, in fact, any callers passing in 0? Oh, further down you add one which may do so, but then perhaps better to make the caller not call here (as is done e.g. in memory_exchange())?

Quoted 12 lines
xen/common/page_alloc.c +3 -4
@@ -2331,11 +2331,20 @@ struct page_info *alloc_domheap_pages(
                                   memflags, d)) == NULL)) )
          return NULL;
 
-    if ( d && !(memflags & MEMF_no_owner) &&
-         assign_pages(d, pg, order, memflags) )
+    if ( d && !(memflags & MEMF_no_owner) )
     {
-        free_heap_pages(pg, order, memflags & MEMF_no_scrub);
-        return NULL;
+        if ( assign_pages(d, pg, order, memflags) )
+        {

xen/common/page_alloc.c struct page_info *alloc_domheap_pages( · line 2347

Quoted 10 lines
xen/common/page_alloc.c +10 -0
+            free_heap_pages(pg, order, memflags & MEMF_no_scrub);
+            return NULL;
+        }
+        if ( memflags & MEMF_no_refcount )
+        {
+            unsigned long i;
+
+            for ( i = 0; i < (1 << order); i++ )
+                pg[i].count_info |= PGC_no_refcount;
+        }

I would seem to me that this needs doing the other way around:
First set PGC_no_refcount, then assign_pages(). After all, the moment assign_pages() drops its lock, the domain could also decide to get rid of (some of) the pages again. For this (and also to slightly simplify things in free_domheap_pages()) perhaps it would be better not to add that ASSERT() to free_heap_pages(). The function shouldn't really be concerned of any refcounting, and hence could as well be ignorant to PGC_no_refcount being set on a page.

Quoted 18 lines
xen/common/page_alloc.c +4 -0
@@ -2368,24 +2377,32 @@ void free_domheap_pages(struct page_info *pg, unsigned int order)
 
         if ( likely(d) && likely(d != dom_cow) )
         {
+            long pages = 0;
+
             /* NB. May recursively lock from relinquish_memory(). */
             spin_lock_recursive(&d->page_alloc_lock);
 
             for ( i = 0; i < (1 << order); i++ )
             {
+                unsigned long count_info = pg[i].count_info;
+
                 if ( pg[i].u.inuse.type_info & PGT_count_mask )
                 {
                     printk(XENLOG_ERR
                            "pg[%u] MFN %"PRI_mfn" c=%#lx o=%u v=%#lx t=%#x\n",
                            i, mfn_x(page_to_mfn(pg + i)),

xen/common/page_alloc.c void free_domheap_pages(struct page_info *pg, unsigned int order) · line 2402

Quoted 10 lines
xen/common/page_alloc.c +5 -1
-                           pg[i].count_info, pg[i].v.free.order,
+                           count_info, pg[i].v.free.order,
                            pg[i].u.free.val, pg[i].tlbflush_timestamp);
                     BUG();
                 }
                 arch_free_heap_page(d, &pg[i]);
+                if ( count_info & PGC_no_refcount )
+                    pg[i].count_info &= ~PGC_no_refcount;
+                else
+                    pages--;

Not only to reduce code churn, may I recommend to avoid introducing the local variable? There's no strict rule preventing arch_free_heap_page() from possibly playing with the field you latch up front.

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

Reviewed-by on v4 6/7
#16 lore raw

Re: [Xen-devel] [PATCH v4 6/7] x86 / vmx: use a MEMF_no_refcount domheap page for APIC_DEFAULT_PHYS_BASE

In reply to Paul Durrant
Quoted 5 lines · Paul Durrant

vmx_alloc_vlapic_mapping() currently contains some very odd looking code that allocates a MEMF_no_owner domheap page and then shares with the guest as if it were a xenheap page. This then requires vmx_free_vlapic_mapping() to call a special function in the mm code: free_shared_domheap_page().

Quoted 8 lines

By using a MEMF_no_refcount domheap page instead, the odd looking code in vmx_alloc_vlapic_mapping() can simply use get_page_and_type() to set up a writable mapping before insertion in the P2M and vmx_free_vlapic_mapping() can simply release the page using put_page_alloc_ref() followed by put_page_and_type(). This then allows free_shared_domheap_page() to be purged.

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#11 lore raw

Re: [Xen-devel] [PATCH v4 5/7] mm: make MEMF_no_refcount pages safe to assign

In reply to Jan Beulich
Quoted 32 lines

-----Original Message-----
From: Jan Beulich <[email protected]>
Sent: 28 January 2020 15:23
To: Durrant, Paul <[email protected]>
Cc: [email protected]; Andrew Cooper <[email protected]>; George Dunlap <[email protected]>; Ian Jackson <[email protected]>; Julien Grall <[email protected]>; Konrad Rzeszutek Wilk <[email protected]>; Stefano Stabellini <[email protected]>; Wei Liu <[email protected]>; Volodymyr Babchuk <[email protected]>; Roger Pau Monné <[email protected]>
Subject: Re: [PATCH v4 5/7] mm: make MEMF_no_refcount pages safe to assign

Quoted 4 lines · Paul Durrant

Currently it is unsafe to assign a domheap page allocated with MEMF_no_refcount to a domain because the domain't 'tot_pages' will not be incremented, but will be decrement when the page is freed (since free_domheap_pages() has no way of telling that the increment was

skipped).

Quoted 3 lines

This patch allocates a new 'count_info' bit for a PGC_no_refcount flag which is then used to mark domheap pages allocated with

MEMF_no_refcount.

Quoted 1 line

This then allows free_domheap_pages() to skip decrementing tot_pages

when

Quoted 7 lines

appropriate and hence makes the pages safe to assign.

NOTE: The patch sets MEMF_no_refcount directly in alloc_domheap_pages()
rather than in assign_pages() because the latter is called with MEMF_no_refcount by memory_exchange() as an optimization, to avoid too many calls to domain_adjust_tot_pages() (which acquires and releases the global 'heap_lock').

Quoted 4 lines

I don't think there were any optimization thoughts with this. The MEMF_no_refcount use is because otherwise for a domain with tot_pages == max_pages the assignment would fail.

That would not be the case if the calls to steal_page() further up didn't pass MEMF_no_refcount (which would be the correct thing to do if not passing it to assign_pages(). I had originally considered doing that because I think it allows the somewhat complex error path after assign_pages() to be dropped. But avoiding thrashing the global lock seemed a good reason to leave memory_exchange() the way it is.

Quoted 7 lines

xen/common/page_alloc.c unsigned long domain_adjust_tot_pages(struct domain

Quoted 3 lines
xen/common/page_alloc.c +0 -0
@@ -460,6 +460,9 @@ unsigned long domain_adjust_tot_pages(struct domain

*d, long pages)

Quoted 3 lines
xen/common/page_alloc.c +0 -0
 {
     long dom_before, dom_after, dom_claimed, sys_before, sys_after;
 
Quoted 7 lines

xen/common/page_alloc.c unsigned long domain_adjust_tot_pages(struct domain *d, long pages) · line 464

Quoted 2 lines
xen/common/page_alloc.c +2 -0
+    if ( !pages )
+        goto out;

Unrelated change? Are there, in fact, any callers passing in 0? Oh, further down you add one which may do so, but then perhaps better to make the caller not call here (as is done e.g. in memory_exchange())?

I think it's preferable for domain_adjust_tot_pages() to handle zero gracefully.

Quoted 20 lines
Quoted 20 lines
xen/common/page_alloc.c +10 -4
@@ -2331,11 +2331,20 @@ struct page_info *alloc_domheap_pages(
                                   memflags, d)) == NULL)) )
          return NULL;
 
-    if ( d && !(memflags & MEMF_no_owner) &&
-         assign_pages(d, pg, order, memflags) )
+    if ( d && !(memflags & MEMF_no_owner) )
     {
-        free_heap_pages(pg, order, memflags & MEMF_no_scrub);
-        return NULL;
+        if ( assign_pages(d, pg, order, memflags) )
+        {
+            free_heap_pages(pg, order, memflags & MEMF_no_scrub);
+            return NULL;
+        }
+        if ( memflags & MEMF_no_refcount )
+        {
+            unsigned long i;
+
Quoted 8 lines

xen/common/page_alloc.c struct page_info *alloc_domheap_pages( · line 2347

Quoted 3 lines
xen/common/page_alloc.c +3 -0
+            for ( i = 0; i < (1 << order); i++ )
+                pg[i].count_info |= PGC_no_refcount;
+        }

I would seem to me that this needs doing the other way around:
First set PGC_no_refcount, then assign_pages(). After all, the moment assign_pages() drops its lock, the domain could also decide to get rid of (some of) the pages again.

True. Yes, this needs to be swapped.

Quoted 7 lines

For this (and
also to slightly simplify things in free_domheap_pages()) perhaps it would be better not to add that ASSERT() to free_heap_pages(). The function shouldn't really be concerned of any refcounting, and hence could as well be ignorant to PGC_no_refcount being set on a page.

Not sure I understand here. What would you like to see free_heap_pages() assert?

Quoted 31 lines

xen/common/page_alloc.c void free_domheap_pages(struct page_info *pg,

Quoted 1 line
xen/common/page_alloc.c +0 -0
@@ -2368,24 +2377,32 @@ void free_domheap_pages(struct page_info *pg,

unsigned int order)

Quoted 6 lines
xen/common/page_alloc.c +2 -0
         if ( likely(d) && likely(d != dom_cow) )
         {
+            long pages = 0;
+
             /* NB. May recursively lock from relinquish_memory(). */

xen/common/page_alloc.c void free_domheap_pages(struct page_info *pg, unsigned int order)

Quoted 10 lines
xen/common/page_alloc.c +2 -0
             spin_lock_recursive(&d->page_alloc_lock);
 
             for ( i = 0; i < (1 << order); i++ )
             {
+                unsigned long count_info = pg[i].count_info;
+
                 if ( pg[i].u.inuse.type_info & PGT_count_mask )
                 {
                     printk(XENLOG_ERR
                            "pg[%u] MFN %"PRI_mfn" c=%#lx o=%u v=%#lx

t=%#x\n",

xen/common/page_alloc.c void free_domheap_pages(struct page_info *pg, unsigned int order) · line 2402

Quoted 11 lines
xen/common/page_alloc.c +5 -1
                            i, mfn_x(page_to_mfn(pg + i)),
-                           pg[i].count_info, pg[i].v.free.order,
+                           count_info, pg[i].v.free.order,
                            pg[i].u.free.val, pg[i].tlbflush_timestamp);
                     BUG();
                 }
                 arch_free_heap_page(d, &pg[i]);
+                if ( count_info & PGC_no_refcount )
+                    pg[i].count_info &= ~PGC_no_refcount;
+                else
+                    pages--;
Quoted 4 lines

Not only to reduce code churn, may I recommend to avoid introducing the local variable? There's no strict rule preventing arch_free_heap_page() from possibly playing with the field you latch up front.

Ok.

Paul

Quoted 2 lines

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#14 lore raw

Re: [Xen-devel] [PATCH v4 5/7] mm: make MEMF_no_refcount pages safe to assign

In reply to Jan Beulich
Quoted 1 line

-----Original Message-----

[snip]

Quoted 38 lines
Quoted 19 lines
Quoted 19 lines
xen/common/page_alloc.c +10 -4
@@ -2331,11 +2331,20 @@ struct page_info *alloc_domheap_pages(
                                   memflags, d)) == NULL)) )
          return NULL;
 
-    if ( d && !(memflags & MEMF_no_owner) &&
-         assign_pages(d, pg, order, memflags) )
+    if ( d && !(memflags & MEMF_no_owner) )
     {
-        free_heap_pages(pg, order, memflags & MEMF_no_scrub);
-        return NULL;
+        if ( assign_pages(d, pg, order, memflags) )
+        {
+            free_heap_pages(pg, order, memflags & MEMF_no_scrub);
+            return NULL;
+        }
+        if ( memflags & MEMF_no_refcount )
+        {
+            unsigned long i;
+
Quoted 8 lines

xen/common/page_alloc.c struct page_info *alloc_domheap_pages( · line 2347

Quoted 3 lines
xen/common/page_alloc.c +3 -0
+            for ( i = 0; i < (1 << order); i++ )
+                pg[i].count_info |= PGC_no_refcount;
+        }

I would seem to me that this needs doing the other way around:
First set PGC_no_refcount, then assign_pages(). After all, the moment assign_pages() drops its lock, the domain could also decide to get rid of (some of) the pages again.

True. Yes, this needs to be swapped.

Quoted 7 lines

For this (and
also to slightly simplify things in free_domheap_pages()) perhaps it would be better not to add that ASSERT() to free_heap_pages(). The function shouldn't really be concerned of any refcounting, and hence could as well be ignorant to PGC_no_refcount being set on a page.

Quoted 3 lines

Not sure I understand here. What would you like to see free_heap_pages() assert?

Oh, I misread. You want me to remove the ASSERT that I added... that's fine.

Paul
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#12 lore raw

Re: [Xen-devel] [PATCH v4 5/7] mm: make MEMF_no_refcount pages safe to assign

In reply to Durrant, Paul
Quoted 23 lines · Paul
Quoted 23 lines

From: Jan Beulich <[email protected]>
Sent: 28 January 2020 15:23

Quoted 4 lines · Paul Durrant

Currently it is unsafe to assign a domheap page allocated with MEMF_no_refcount to a domain because the domain't 'tot_pages' will not be incremented, but will be decrement when the page is freed (since free_domheap_pages() has no way of telling that the increment was

skipped).

Quoted 3 lines

This patch allocates a new 'count_info' bit for a PGC_no_refcount flag which is then used to mark domheap pages allocated with

MEMF_no_refcount.

Quoted 1 line

This then allows free_domheap_pages() to skip decrementing tot_pages

when

Quoted 7 lines

appropriate and hence makes the pages safe to assign.

NOTE: The patch sets MEMF_no_refcount directly in alloc_domheap_pages()
rather than in assign_pages() because the latter is called with MEMF_no_refcount by memory_exchange() as an optimization, to avoid too many calls to domain_adjust_tot_pages() (which acquires and releases the global 'heap_lock').

Quoted 8 lines
Quoted 4 lines

I don't think there were any optimization thoughts with this. The MEMF_no_refcount use is because otherwise for a domain with tot_pages == max_pages the assignment would fail.

That would not be the case if the calls to steal_page() further up didn't pass MEMF_no_refcount (which would be the correct thing to do if not passing it to assign_pages().

No, that's not an option either: steal_page() would otherwise decrement ->tot_pages, allowing the domain to allocate new memory on another vCPU. This would again result in the exchange failing for no reason. (And no, I don't think a guest should be required to serialize e.g. ballooning operations with exchanges.)

Quoted 10 lines
Quoted 10 lines

xen/common/page_alloc.c unsigned long domain_adjust_tot_pages(struct domain

Quoted 3 lines
xen/common/page_alloc.c +0 -0
@@ -460,6 +460,9 @@ unsigned long domain_adjust_tot_pages(struct domain

*d, long pages)

Quoted 5 lines
xen/common/page_alloc.c +2 -0
 {
     long dom_before, dom_after, dom_claimed, sys_before, sys_after;
 
+    if ( !pages )
+        goto out;
Quoted 7 lines
Quoted 4 lines

Unrelated change? Are there, in fact, any callers passing in 0? Oh, further down you add one which may do so, but then perhaps better to make the caller not call here (as is done e.g. in memory_exchange())?

I think it's preferable for domain_adjust_tot_pages() to handle zero gracefully.

That's an option, but imo would then better be a separate change (to also drop present guards of calls to the function).

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#13 lore raw

Re: [Xen-devel] [PATCH v4 5/7] mm: make MEMF_no_refcount pages safe to assign

Quoted 45 lines

-----Original Message-----
From: Jan Beulich <[email protected]>
Sent: 29 January 2020 08:22
To: Durrant, Paul <[email protected]>
Cc: [email protected]; Andrew Cooper <[email protected]>; George Dunlap <[email protected]>; Ian Jackson <[email protected]>; Julien Grall <[email protected]>; Konrad Rzeszutek Wilk <[email protected]>; Stefano Stabellini <[email protected]>; Wei Liu <[email protected]>; Volodymyr Babchuk <[email protected]>; Roger Pau Monné <[email protected]>
Subject: Re: [PATCH v4 5/7] mm: make MEMF_no_refcount pages safe to assign

Quoted 10 lines · Paul
Quoted 10 lines

From: Jan Beulich <[email protected]>
Sent: 28 January 2020 15:23

Quoted 4 lines · Paul Durrant

Currently it is unsafe to assign a domheap page allocated with MEMF_no_refcount to a domain because the domain't 'tot_pages' will not be incremented, but will be decrement when the page is freed (since free_domheap_pages() has no way of telling that the increment was

skipped).

Quoted 1 line
Quoted 8 lines
Quoted 8 lines
Quoted 2 lines

This patch allocates a new 'count_info' bit for a PGC_no_refcount flag which is then used to mark domheap pages allocated with

MEMF_no_refcount.

Quoted 1 line

This then allows free_domheap_pages() to skip decrementing tot_pages

when

Quoted 3 lines

appropriate and hence makes the pages safe to assign.

NOTE: The patch sets MEMF_no_refcount directly in

alloc_domheap_pages()

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

rather than in assign_pages() because the latter is called with MEMF_no_refcount by memory_exchange() as an optimization, to

avoid

Quoted 9 lines
Quoted 7 lines
Quoted 2 lines

too many calls to domain_adjust_tot_pages() (which acquires and releases the global 'heap_lock').

I don't think there were any optimization thoughts with this. The MEMF_no_refcount use is because otherwise for a domain with tot_pages == max_pages the assignment would fail.

That would not be the case if the calls to steal_page() further up

didn't

Quoted 9 lines
Quoted 2 lines

pass MEMF_no_refcount (which would be the correct thing to do if not passing it to assign_pages().

No, that's not an option either: steal_page() would otherwise decrement ->tot_pages, allowing the domain to allocate new memory on another vCPU. This would again result in the exchange failing for no reason. (And no, I don't think a guest should be required to serialize e.g. ballooning operations with exchanges.)

Ok, yes it does make it non-atomic but my view would be that the guest should not be simultaneously ballooning; however, we clearly differ there.

Quoted 16 lines
Quoted 3 lines
Quoted 3 lines

xen/common/page_alloc.c unsigned long domain_adjust_tot_pages(struct

Quoted 3 lines
xen/common/page_alloc.c +0 -0
@@ -460,6 +460,9 @@ unsigned long domain_adjust_tot_pages(struct

domain

Quoted 12 lines
Quoted 11 lines

*d, long pages)

xen/common/page_alloc.c unsigned long domain_adjust_tot_pages(struct domain *d, long pages) · line 464

Quoted 5 lines
xen/common/page_alloc.c +2 -0
 {
     long dom_before, dom_after, dom_claimed, sys_before, sys_after;
 
+    if ( !pages )
+        goto out;

Unrelated change? Are there, in fact, any callers passing in 0? Oh, further down you add one which may do so, but then perhaps better to make the caller not call here (as is done e.g. in memory_exchange())?

Quoted 5 lines
Quoted 2 lines

I think it's preferable for domain_adjust_tot_pages() to handle zero gracefully.

That's an option, but imo would then better be a separate change (to also drop present guards of calls to the function).

Ok, I'll split it out into a separate patch.

Paul

Quoted 2 lines

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel