Skip to contentSkip to conversation

populate/unpopulate memory when domain on static allocation

PATCH Superseded Penny Zheng 4 participants 24 messages Jul 20, 2022 – Aug 12, 2022

v9 of 12 Superseded Latest is v12 →

lore
Share & tools
lore
#0 lore raw

v9 cover populate/unpopulate memory when domain on static allocation

Today when a domain unpopulates the memory on runtime, they will always hand the memory over to the heap allocator. And it will be a problem if it is a static domain.
Pages used as guest RAM for static domain shall always be reserved to this domain only, and not be used for any other purposes, so they shall never go back to heap allocator.

This patch serie intends to fix this issue, by adding pages on the new list resv_page_list after having taken them off the "normal" list, when unpopulating memory, and retrieving pages from resv page list(resv_page_list) when populating memory.

---
v9 changes:
- move free_domheap_page into else-condition
- considering scrubbing static pages, domain dying case and opt_scrub_domheap both do not apply to static pages.
- as unowned static pages don't make themselves to free_domstatic_page at the moment, remove else-condition and add ASSERT(d) at the top of the function
- remove macro helper put_static_page, and just expand its code inside free_domstatic_page
- Use ASSERT_ALLOC_CONTEXT() in acquire_reserved_page
- Add free_staticmem_pages to undo prepare_staticmem_pages when assign_domstatic_pages fails
- Remove redundant static in error message
---
v8 changes:
- introduce new helper free_domstatic_page
- let put_page call free_domstatic_page for static page, when last ref drops
- #define PGC_static zero when !CONFIG_STATIC_MEMORY, as it is used outside page_alloc.c
- #ifdef-ary around is_domain_using_staticmem() is not needed anymore
- order as a parameter is not needed here, as all staticmem operations are limited to order-0 regions
- move d->page_alloc_lock after operation on d->resv_page_list
- As concurrent free/allocate could modify the resv_page_list, we still need the lock
---
v7 changes:
- protect free_staticmem_pages with heap_lock to match its reverse function acquire_staticmem_pages
- IS_ENABLED(CONFIG_STATIC_MEMORY) would not be needed anymore
- add page on the rsv_page_list *after* it has been freed
- remove the lock, since we add the page to rsv_page_list after it has been totally freed.
---
v6 changes:
- rename PGC_staticmem to PGC_static
- remove #ifdef aroud function declaration
- use domain instead of sub-systems
- move non-zero is_domain_using_staticmem() from ARM header to common header
- move PGC_static !CONFIG_STATIC_MEMORY definition to common header
- drop the lock before returning
---
v5 changes:
- introduce three new commits
- In order to avoid stub functions, we #define PGC_staticmem to non-zero only when CONFIG_STATIC_MEMORY
- use "unlikely()" around pg->count_info & PGC_staticmem
- remove pointless "if", since mark_page_free() is going to set count_info to PGC_state_free and by consequence clear PGC_staticmem
- move #define PGC_staticmem 0 to mm.h
- guard "is_domain_using_staticmem" under CONFIG_STATIC_MEMORY
- #define is_domain_using_staticmem zero if undefined
- extract common codes for assigning pages into a helper assign_domstatic_pages
- refine commit message
- remove stub function acquire_reserved_page
- Alloc/free of memory can happen concurrently. So access to rsv_page_list needs to be protected with a spinlock
---
v4 changes:
- commit message refinement
- miss dropping __init in acquire_domstatic_pages
- add the page back to the reserved list in case of error
- remove redundant printk
- refine log message and make it warn level
- guard "is_domain_using_staticmem" under CONFIG_STATIC_MEMORY
- #define is_domain_using_staticmem zero if undefined
---
v3 changes:
- fix possible racy issue in free_staticmem_pages()
- introduce a stub free_staticmem_pages() for the !CONFIG_STATIC_MEMORY case
- move the change to free_heap_pages() to cover other potential call sites
- change fixed width type uint32_t to unsigned int
- change "flags" to a more descriptive name "cdf"
- change name from "is_domain_static()" to "is_domain_using_staticmem"
- have page_list_del() just once out of the if()
- remove resv_pages counter
- make arch_free_heap_page be an expression, not a compound statement.
- move #ifndef is_domain_using_staticmem to the common header file
- remove #ifdef CONFIG_STATIC_MEMORY-ary
- remove meaningless page_to_mfn(page) in error log
---
v2 changes:
- let "flags" live in the struct domain. So other arch can take advantage of it in the future
- change name from "is_domain_on_static_allocation" to "is_domain_static()"
- put reserved pages on resv_page_list after having taken them off the "normal" list
- introduce acquire_reserved_page to retrieve reserved pages from resv_page_list
- forbid non-zero-order requests in populate_physmap
- let is_domain_static return ((void)(d), false) on x86
- fix coding style

Penny Zheng (8):
xen/arm: rename PGC_reserved to PGC_static
xen: do not free reserved memory into heap
xen: do not merge reserved pages in free_heap_pages()
xen: add field "flags" to cover all internal CDF_XXX xen/arm: introduce CDF_staticmem
xen/arm: unpopulate memory when domain is static
xen: introduce prepare_staticmem_pages
xen: retrieve reserved pages on populate_physmap

 xen/arch/arm/domain.c             |   2 -
 xen/arch/arm/domain_build.c       |   5 +-
 xen/arch/arm/include/asm/domain.h |   3 +-
 xen/arch/arm/include/asm/mm.h     |   8 +-
 xen/arch/arm/mm.c                 |   5 +-
 xen/common/domain.c               |   7 ++
 xen/common/memory.c               |  23 ++++
 xen/common/page_alloc.c           | 178 ++++++++++++++++++++++--------
 xen/include/xen/domain.h          |   8 ++
 xen/include/xen/mm.h              |   8 +-
 xen/include/xen/sched.h           |   6 +
 11 files changed, 193 insertions(+), 60 deletions(-)
Signature · 1 line
2.25.1
#1 lore raw

PATCH v9 1/8 xen/arm: rename PGC_reserved to PGC_static

PGC_reserved could be ambiguous, and we have to tell what the pages are reserved for, so this commit intends to rename PGC_reserved to PGC_static, which clearly indicates the page is reserved for static memory.

---
v8 changes:
- no change
---
v7 changes:
- no change
---
v6 changes:
- rename PGC_staticmem to PGC_static
---
v5 changes:
- new commit

---
 xen/arch/arm/include/asm/mm.h |  6 +++---
 xen/common/page_alloc.c       | 22 +++++++++++-----------
 2 files changed, 14 insertions(+), 14 deletions(-)
xen/arch/arm/include/asm/mm.h +3 -3
index c4bc3cd1e5..8b2481c1f3 100644
@@ -108,9 +108,9 @@ struct page_info
   /* Page is Xen heap? */
 #define _PGC_xen_heap     PG_shift(2)
 #define PGC_xen_heap      PG_mask(1, 2)
-  /* Page is reserved */
-#define _PGC_reserved     PG_shift(3)
-#define PGC_reserved      PG_mask(1, 3)
+  /* Page is static memory */
+#define _PGC_static    PG_shift(3)
+#define PGC_static     PG_mask(1, 3)
 /* ... */
 /* Page is broken? */
 #define _PGC_broken       PG_shift(7)
xen/common/page_alloc.c +11 -11
index fe0e15429a..ed56379b96 100644
@@ -151,8 +151,8 @@
 #define p2m_pod_offline_or_broken_replace(pg) BUG_ON(pg != NULL)
 #endif
 
-#ifndef PGC_reserved
-#define PGC_reserved 0
+#ifndef PGC_static
+#define PGC_static 0
 #endif
 
 /*
@@ -2286,7 +2286,7 @@ int assign_pages(
 
         for ( i = 0; i < nr; i++ )
         {
-            ASSERT(!(pg[i].count_info & ~(PGC_extra | PGC_reserved)));
+            ASSERT(!(pg[i].count_info & ~(PGC_extra | PGC_static)));
             if ( pg[i].count_info & PGC_extra )
                 extra_pages++;
         }
@@ -2346,7 +2346,7 @@ int assign_pages(
         page_set_owner(&pg[i], d);
         smp_wmb(); /* Domain pointer must be visible before updating refcnt. */
         pg[i].count_info =
-            (pg[i].count_info & (PGC_extra | PGC_reserved)) | PGC_allocated | 1;
+            (pg[i].count_info & (PGC_extra | PGC_static)) | PGC_allocated | 1;
 
         page_list_add_tail(&pg[i], page_to_list(d, &pg[i]));
     }
@@ -2652,8 +2652,8 @@ void __init free_staticmem_pages(struct page_info *pg, unsigned long nr_mfns,
             scrub_one_page(pg);
         }
 
-        /* In case initializing page of static memory, mark it PGC_reserved. */
-        pg[i].count_info |= PGC_reserved;
+        /* In case initializing page of static memory, mark it PGC_static. */
+        pg[i].count_info |= PGC_static;
     }
 }
 
@@ -2682,8 +2682,8 @@ static struct page_info * __init acquire_staticmem_pages(mfn_t smfn,
 
     for ( i = 0; i < nr_mfns; i++ )
     {
-        /* The page should be reserved and not yet allocated. */
-        if ( pg[i].count_info != (PGC_state_free | PGC_reserved) )
+        /* The page should be static and not yet allocated. */
+        if ( pg[i].count_info != (PGC_state_free | PGC_static) )
         {
             printk(XENLOG_ERR
                    "pg[%lu] Static MFN %"PRI_mfn" c=%#lx t=%#x\n",
@@ -2697,10 +2697,10 @@ static struct page_info * __init acquire_staticmem_pages(mfn_t smfn,
                                 &tlbflush_timestamp);
 
         /*
-         * Preserve flag PGC_reserved and change page state
+         * Preserve flag PGC_static and change page state
          * to PGC_state_inuse.
          */
-        pg[i].count_info = PGC_reserved | PGC_state_inuse;
+        pg[i].count_info = PGC_static | PGC_state_inuse;
         /* Initialise fields which have other uses for free pages. */
         pg[i].u.inuse.type_info = 0;
         page_set_owner(&pg[i], NULL);
@@ -2722,7 +2722,7 @@ static struct page_info * __init acquire_staticmem_pages(mfn_t smfn,
 
  out_err:
     while ( i-- )
-        pg[i].count_info = PGC_reserved | PGC_state_free;
+        pg[i].count_info = PGC_static | PGC_state_free;
 
     spin_unlock(&heap_lock);
 
Signature · 1 line
2.25.1
#2 lore raw

PATCH v9 2/8 xen: do not free reserved memory into heap

In reply to Penny Zheng

Pages used as guest RAM for static domain, shall be reserved to this domain only.
So in case reserved pages being used for other purpose, users shall not free them back to heap, even when last ref gets dropped.

This commit introduces a new helper free_domstatic_page to free static page in runtime, and free_staticmem_pages will be called by it in runtime, so let's drop the __init flag.

---
v9 changes:
- move free_domheap_page into else-condition
- considering scrubbing static pages, domain dying case and opt_scrub_domheap both donot apply to static pages.
- as unowned static pages don't make themselves to free_domstatic_page at the moment, remove else-condition and add ASSERT(d) at the top of the function
---
v8 changes:
- introduce new helper free_domstatic_page
- let put_page call free_domstatic_page for static page, when last ref drops
- #define PGC_static zero when !CONFIG_STATIC_MEMORY, as it is used outside page_alloc.c
---
v7 changes:
- protect free_staticmem_pages with heap_lock to match its reverse function acquire_staticmem_pages
---
v6 changes:
- adapt to PGC_static
- remove #ifdef aroud function declaration
---
v5 changes:
- In order to avoid stub functions, we #define PGC_staticmem to non-zero only when CONFIG_STATIC_MEMORY
- use "unlikely()" around pg->count_info & PGC_staticmem
- remove pointless "if", since mark_page_free() is going to set count_info to PGC_state_free and by consequence clear PGC_staticmem
- move #define PGC_staticmem 0 to mm.h
---
v4 changes:
- no changes
---
v3 changes:
- fix possible racy issue in free_staticmem_pages()
- introduce a stub free_staticmem_pages() for the !CONFIG_STATIC_MEMORY case
- move the change to free_heap_pages() to cover other potential call sites
- fix the indentation
---
v2 changes:
- new commit
---

---
 xen/arch/arm/include/asm/mm.h |  4 +++-
 xen/arch/arm/mm.c             |  5 ++++-
 xen/common/page_alloc.c       | 37 ++++++++++++++++++++++++++++-------
 xen/include/xen/mm.h          |  7 +++++--
 4 files changed, 42 insertions(+), 11 deletions(-)
xen/arch/arm/include/asm/mm.h +3 -1
index 8b2481c1f3..f1640bbda4 100644
@@ -108,9 +108,11 @@ struct page_info
   /* Page is Xen heap? */
 #define _PGC_xen_heap     PG_shift(2)
 #define PGC_xen_heap      PG_mask(1, 2)
-  /* Page is static memory */
+#ifdef CONFIG_STATIC_MEMORY
+/* Page is static memory */
 #define _PGC_static    PG_shift(3)
 #define PGC_static     PG_mask(1, 3)
+#endif
 /* ... */
 /* Page is broken? */
 #define _PGC_broken       PG_shift(7)
xen/arch/arm/mm.c +4 -1
index 009b8cd9ef..9132fb9472 100644
@@ -1622,7 +1622,10 @@ void put_page(struct page_info *page)
 
     if ( unlikely((nx & PGC_count_mask) == 0) )
     {
-        free_domheap_page(page);
+        if ( unlikely(nx & PGC_static) )
+            free_domstatic_page(page);
+        else
+            free_domheap_page(page);
     }
 }
 
xen/common/page_alloc.c +30 -7
index ed56379b96..a12622e921 100644
@@ -151,10 +151,6 @@
 #define p2m_pod_offline_or_broken_replace(pg) BUG_ON(pg != NULL)
 #endif
 
-#ifndef PGC_static
-#define PGC_static 0
-#endif
-
 /*
  * Comma-separated list of hexadecimal page numbers containing bad bytes.
  * e.g. 'badpage=0x3f45,0x8a321'.
@@ -2636,12 +2632,14 @@ struct domain *get_pg_owner(domid_t domid)
 
 #ifdef CONFIG_STATIC_MEMORY
 /* Equivalent of free_heap_pages to free nr_mfns pages of static memory. */
-void __init free_staticmem_pages(struct page_info *pg, unsigned long nr_mfns,
-                                 bool need_scrub)
+void free_staticmem_pages(struct page_info *pg, unsigned long nr_mfns,
+                          bool need_scrub)
 {
     mfn_t mfn = page_to_mfn(pg);
     unsigned long i;
 
+    spin_lock(&heap_lock);
+
     for ( i = 0; i < nr_mfns; i++ )
     {
         mark_page_free(&pg[i], mfn_add(mfn, i));
@@ -2652,9 +2650,34 @@ void __init free_staticmem_pages(struct page_info *pg, unsigned long nr_mfns,
             scrub_one_page(pg);
         }
 
-        /* In case initializing page of static memory, mark it PGC_static. */
         pg[i].count_info |= PGC_static;
     }
+
+    spin_unlock(&heap_lock);
+}
+
+void free_domstatic_page(struct page_info *page)
+{
+    struct domain *d = page_get_owner(page);
+    bool drop_dom_ref;
+
+    ASSERT(d);
+
+    ASSERT_ALLOC_CONTEXT();
+
+    /* NB. May recursively lock from relinquish_memory(). */
+    spin_lock_recursive(&d->page_alloc_lock);
+
+    arch_free_heap_page(d, page);
+
+    drop_dom_ref = !domain_adjust_tot_pages(d, -1);
+
+    spin_unlock_recursive(&d->page_alloc_lock);
+
+    free_staticmem_pages(page, 1, scrub_debug);
+
+    if ( drop_dom_ref )
+        put_domain(d);
 }
 
 /*
xen/include/xen/mm.h +5 -2
index 3be754da92..f1a7d5c991 100644
@@ -85,13 +85,12 @@ bool scrub_free_pages(void);
 } while ( false )
 #define FREE_XENHEAP_PAGE(p) FREE_XENHEAP_PAGES(p, 0)
 
-#ifdef CONFIG_STATIC_MEMORY
 /* These functions are for static memory */
 void free_staticmem_pages(struct page_info *pg, unsigned long nr_mfns,
                           bool need_scrub);
+void free_domstatic_page(struct page_info *page);
 int acquire_domstatic_pages(struct domain *d, mfn_t smfn, unsigned int nr_mfns,
                             unsigned int memflags);
-#endif
 
 /* Map machine page range in Xen virtual address space. */
 int map_pages_to_xen(
@@ -212,6 +211,10 @@ extern struct domain *dom_cow;
 
 #include <asm/mm.h>
 
+#ifndef PGC_static
+#define PGC_static 0
+#endif
+
 static inline bool is_special_page(const struct page_info *page)
 {
     return is_xen_heap_page(page) || (page->count_info & PGC_extra);
Signature · 1 line
2.25.1
Reviewed-by on v9 2/8
#3 lore raw

Re: [PATCH v9 2/8] xen: do not free reserved memory into heap

Quoted 10 lines · Penny Zheng

Pages used as guest RAM for static domain, shall be reserved to this domain only.
So in case reserved pages being used for other purpose, users shall not free them back to heap, even when last ref gets dropped.

This commit introduces a new helper free_domstatic_page to free static page in runtime, and free_staticmem_pages will be called by it in runtime, so let's drop the __init flag.

Technically

Nevertheless two remarks:

xen/common/page_alloc.c void __init free_staticmem_pages(struct page_info *pg, unsigned long nr_mfns, · line 2664

Quoted 6 lines
xen/common/page_alloc.c +6 -0
+void free_domstatic_page(struct page_info *page)
+{
+    struct domain *d = page_get_owner(page);
+    bool drop_dom_ref;
+
+    ASSERT(d);

I wonder whether

    if ( unlikely(!d) )
    {
        ASSERT_UNREACHABLE();
        return;
    }

wouldn't be more robust looking forward.

Quoted 18 lines
xen/include/xen/mm.h +1 -2
@@ -85,13 +85,12 @@ bool scrub_free_pages(void);
 } while ( false )
 #define FREE_XENHEAP_PAGE(p) FREE_XENHEAP_PAGES(p, 0)
 
-#ifdef CONFIG_STATIC_MEMORY
 /* These functions are for static memory */
 void free_staticmem_pages(struct page_info *pg, unsigned long nr_mfns,
                           bool need_scrub);
+void free_domstatic_page(struct page_info *page);
 int acquire_domstatic_pages(struct domain *d, mfn_t smfn, unsigned int nr_mfns,
                             unsigned int memflags);
-#endif
 
 /* Map machine page range in Xen virtual address space. */
 int map_pages_to_xen(

xen/include/xen/mm.h extern struct domain *dom_cow; · line 216

Quoted 6 lines
xen/include/xen/mm.h +3 -0
@@ -212,6 +211,10 @@ extern struct domain *dom_cow;
 
 #include <asm/mm.h>
 
+#ifndef PGC_static
+#define PGC_static 0
+#endif

This disconnect from all other PGC_* values isn't very nice. I wonder as how bad it would be seen if Arm kept its #define to 0 private, with the generic fallback remaining in page_alloc.c.

Jan

#4 lore raw

RE: [PATCH v9 2/8] xen: do not free reserved memory into heap

Hi Jan

Quoted 65 lines

-----Original Message-----
From: Jan Beulich <[email protected]>
Sent: Monday, July 25, 2022 11:30 PM
To: Penny Zheng <[email protected]>
Cc: Wei Chen <[email protected]>; Stefano Stabellini <[email protected]>; Julien Grall <[email protected]>; Bertrand Marquis <[email protected]>; Volodymyr Babchuk <[email protected]>; Andrew Cooper <[email protected]>; George Dunlap <[email protected]>; Wei Liu <[email protected]>; [email protected]
Subject: Re: [PATCH v9 2/8] xen: do not free reserved memory into heap

Quoted 10 lines · Penny Zheng

Pages used as guest RAM for static domain, shall be reserved to this domain only.
So in case reserved pages being used for other purpose, users shall not free them back to heap, even when last ref gets dropped.

This commit introduces a new helper free_domstatic_page to free static page in runtime, and free_staticmem_pages will be called by it in runtime, so let's drop the __init flag.

Technically

Nevertheless two remarks:

xen/common/page_alloc.c void __init free_staticmem_pages(struct page_info *pg, unsigned long nr_mfns, · line 2664

Quoted 5 lines
xen/common/page_alloc.c +5 -0
+void free_domstatic_page(struct page_info *page) {
+    struct domain *d = page_get_owner(page);
+    bool drop_dom_ref;
+
+    ASSERT(d);

I wonder whether

    if ( unlikely(!d) )
    {
        ASSERT_UNREACHABLE();
        return;
    }

wouldn't be more robust looking forward.

xen/include/xen/mm.h bool scrub_free_pages(void); } while ( false ) · line 91

Quoted 11 lines
xen/include/xen/mm.h +1 -1
@@ -85,13 +85,12 @@ bool scrub_free_pages(void);  } while ( false )
 #define FREE_XENHEAP_PAGE(p) FREE_XENHEAP_PAGES(p, 0)
 
-#ifdef CONFIG_STATIC_MEMORY
 /* These functions are for static memory */  void
 free_staticmem_pages(struct page_info *pg, unsigned long nr_mfns,
                           bool need_scrub);
+void free_domstatic_page(struct page_info *page);
 int acquire_domstatic_pages(struct domain *d, mfn_t smfn, unsigned int

nr_mfns,

Quoted 4 lines
xen/include/xen/mm.h +0 -0
                             unsigned int memflags); -#endif
 
 /* Map machine page range in Xen virtual address space. */  int

map_pages_to_xen( @@ -212,6 +211,10 @@ extern struct domain

*dom_cow;

Quoted 3 lines

#include <asm/mm.h>

Quoted 8 lines

xen/include/xen/mm.h extern struct domain *dom_cow; · line 216

Quoted 3 lines
xen/include/xen/mm.h +3 -0
+#ifndef PGC_static
+#define PGC_static 0
+#endif

This disconnect from all other PGC_* values isn't very nice. I wonder as how bad it would be seen if Arm kept its #define to 0 private, with the generic fallback remaining in page_alloc.c.

It, right now, is only used in xen/arch/arm/mm.c and xen/common/page_alloc.c. It is ok to let Arm keep its #define to 0 private, with the generic fallback remaining in page_alloc.c.

Quoted 1 line

Jan

Reviewed-by on v9 2/8
#5 lore raw

Re: [PATCH v9 2/8] xen: do not free reserved memory into heap

In reply to Penny Zheng

Hi Penny,

Quoted 10 lines · Penny Zheng

Pages used as guest RAM for static domain, shall be reserved to this domain only.
So in case reserved pages being used for other purpose, users shall not free them back to heap, even when last ref gets dropped.

This commit introduces a new helper free_domstatic_page to free static page in runtime, and free_staticmem_pages will be called by it in runtime, so let's drop the __init flag.

With a couple of comments below:

Quoted 39 lines
xen/common/page_alloc.c +4 -7
index ed56379b96..a12622e921 100644
@@ -151,10 +151,6 @@
  #define p2m_pod_offline_or_broken_replace(pg) BUG_ON(pg != NULL)
  #endif
 
-#ifndef PGC_static
-#define PGC_static 0
-#endif
-
  /*
   * Comma-separated list of hexadecimal page numbers containing bad bytes.
   * e.g. 'badpage=0x3f45,0x8a321'.
@@ -2636,12 +2632,14 @@ struct domain *get_pg_owner(domid_t domid)
 
  #ifdef CONFIG_STATIC_MEMORY
  /* Equivalent of free_heap_pages to free nr_mfns pages of static memory. */
-void __init free_staticmem_pages(struct page_info *pg, unsigned long nr_mfns,
-                                 bool need_scrub)
+void free_staticmem_pages(struct page_info *pg, unsigned long nr_mfns,
+                          bool need_scrub)
  {
      mfn_t mfn = page_to_mfn(pg);
      unsigned long i;
 
+    spin_lock(&heap_lock);
+
      for ( i = 0; i < nr_mfns; i++ )
      {
          mark_page_free(&pg[i], mfn_add(mfn, i));
@@ -2652,9 +2650,34 @@ void __init free_staticmem_pages(struct page_info *pg, unsigned long nr_mfns,
              scrub_one_page(pg);
          }
 
-        /* In case initializing page of static memory, mark it PGC_static. */
          pg[i].count_info |= PGC_static;
      }

xen/common/page_alloc.c void __init free_staticmem_pages(struct page_info *pg, unsigned long nr_mfns, · line 2664

Quoted 10 lines
xen/common/page_alloc.c +10 -0
+
+    spin_unlock(&heap_lock);
+}
+
+void free_domstatic_page(struct page_info *page)
+{
+    struct domain *d = page_get_owner(page);
+    bool drop_dom_ref;
+
+    ASSERT(d);

I saw Jan commenting on this. I agree with him to switch to

if ( d )
{
ASSERT_UNREACHABLE();
return;
}

I would even go further and add a printk() to log the problem in prod.

Quoted 25 lines
xen/common/page_alloc.c +16 -0
+
+    ASSERT_ALLOC_CONTEXT();
+
+    /* NB. May recursively lock from relinquish_memory(). */
+    spin_lock_recursive(&d->page_alloc_lock);
+
+    arch_free_heap_page(d, page);
+
+    drop_dom_ref = !domain_adjust_tot_pages(d, -1);
+
+    spin_unlock_recursive(&d->page_alloc_lock);
+
+    free_staticmem_pages(page, 1, scrub_debug);
+
+    if ( drop_dom_ref )
+        put_domain(d);
  }
 
  /*
xen/include/xen/mm.h +0 -0
index 3be754da92..f1a7d5c991 100644
@@ -85,13 +85,12 @@ bool scrub_free_pages(void);
  } while ( false )

xen/include/xen/mm.h bool scrub_free_pages(void); · line 94

Quoted 10 lines
xen/include/xen/mm.h +1 -2
  #define FREE_XENHEAP_PAGE(p) FREE_XENHEAP_PAGES(p, 0)
 
-#ifdef CONFIG_STATIC_MEMORY
  /* These functions are for static memory */
  void free_staticmem_pages(struct page_info *pg, unsigned long nr_mfns,
                            bool need_scrub);
+void free_domstatic_page(struct page_info *page);
  int acquire_domstatic_pages(struct domain *d, mfn_t smfn, unsigned int nr_mfns,
                              unsigned int memflags);
-#endif

NIT: The removal of #ifdef seems to be unrelated to this patch. If you plan to send a v10, then I would suggest to mention it on the commit message.

xen/include/xen/mm.h extern struct domain *dom_cow; · line 216

Quoted 10 lines
xen/include/xen/mm.h +3 -0
  /* Map machine page range in Xen virtual address space. */
  int map_pages_to_xen(
@@ -212,6 +211,10 @@ extern struct domain *dom_cow;
 
  #include <asm/mm.h>
 
+#ifndef PGC_static
+#define PGC_static 0
+#endif

I saw Jan commenting on this change. So, FYI, I am OK either way.

Quoted 4 lines
xen/include/xen/mm.h +1 -0
+
  static inline bool is_special_page(const struct page_info *page)
  {
      return is_xen_heap_page(page) || (page->count_info & PGC_extra);
Signature · 1 line
Julien Grall
#6 lore raw

PATCH v9 3/8 xen: do not merge reserved pages in free_heap_pages()

In reply to Penny Zheng

The code in free_heap_pages() will try to merge pages with the successor/predecessor if pages are suitably aligned. So if the pages reserved are right next to the pages given to the heap allocator, free_heap_pages() will merge them, and give the reserved pages to heap allocator accidently as a result.

So in order to avoid the above scenario, this commit updates free_heap_pages() to check whether the predecessor and/or successor has PGC_reserved set, when trying to merge the about-to-be-freed chunk with the predecessor and/or successor.

---
v9 changes:
- no change
---
v8 changes:
- no change
---
v7 changes:
- no change
---
v6 changes:
- adapt to PGC_static
---
v5 changes:
- change PGC_reserved to adapt to PGC_staticmem
---
v4 changes:
- no changes
---
v3 changes:
- no changes
---
v2 changes:
- new commit

---
 xen/common/page_alloc.c | 2 ++
 1 file changed, 2 insertions(+)
xen/common/page_alloc.c +2 -0
index a12622e921..45bd88a685 100644
@@ -1475,6 +1475,7 @@ static void free_heap_pages(
             /* Merge with predecessor block? */
             if ( !mfn_valid(page_to_mfn(predecessor)) ||
                  !page_state_is(predecessor, free) ||
+                 (predecessor->count_info & PGC_static) ||
                  (PFN_ORDER(predecessor) != order) ||
                  (phys_to_nid(page_to_maddr(predecessor)) != node) )
                 break;
@@ -1498,6 +1499,7 @@ static void free_heap_pages(
             /* Merge with successor block? */
             if ( !mfn_valid(page_to_mfn(successor)) ||
                  !page_state_is(successor, free) ||
+                 (successor->count_info & PGC_static) ||
                  (PFN_ORDER(successor) != order) ||
                  (phys_to_nid(page_to_maddr(successor)) != node) )
                 break;
Signature · 1 line
2.25.1
#7 lore raw

Re: [PATCH v9 3/8] xen: do not merge reserved pages in free_heap_pages()

Hi Penny,

Sorry I only spotted a couple of typos in the commit message now.

Quoted 5 lines · Penny Zheng

The code in free_heap_pages() will try to merge pages with the successor/predecessor if pages are suitably aligned. So if the pages reserved are right next to the pages given to the heap allocator, free_heap_pages() will merge them, and give the reserved pages to heap allocator accidently as a result.

s/accidently/accidentally/ I think.

Quoted 3 lines

So in order to avoid the above scenario, this commit updates free_heap_pages() to check whether the predecessor and/or successor has PGC_reserved set,

s/PGC_reserved/PGC_staticmem/

I think both #1 and #3 can go in now. So I would be happy to handle the typoes on commit.

Quoted 48 lines

when trying to merge the about-to-be-freed chunk with the predecessor and/or successor.

---
v9 changes:
- no change
---
v8 changes:
- no change
---
v7 changes:
- no change
---
v6 changes:
- adapt to PGC_static
---
v5 changes:
- change PGC_reserved to adapt to PGC_staticmem
---
v4 changes:
- no changes
---
v3 changes:
- no changes
---
v2 changes:
- new commit

---
  xen/common/page_alloc.c | 2 ++
  1 file changed, 2 insertions(+)
xen/common/page_alloc.c +1 -0
index a12622e921..45bd88a685 100644
@@ -1475,6 +1475,7 @@ static void free_heap_pages(
              /* Merge with predecessor block? */
              if ( !mfn_valid(page_to_mfn(predecessor)) ||
                   !page_state_is(predecessor, free) ||
+                 (predecessor->count_info & PGC_static) ||
                   (PFN_ORDER(predecessor) != order) ||
                   (phys_to_nid(page_to_maddr(predecessor)) != node) )
                  break;

xen/common/page_alloc.c static void free_heap_pages( · line 1505

Quoted 7 lines
xen/common/page_alloc.c +1 -0
@@ -1498,6 +1499,7 @@ static void free_heap_pages(
              /* Merge with successor block? */
              if ( !mfn_valid(page_to_mfn(successor)) ||
                   !page_state_is(successor, free) ||
+                 (successor->count_info & PGC_static) ||
                   (PFN_ORDER(successor) != order) ||
                   (phys_to_nid(page_to_maddr(successor)) != node) )
                  break;

Cheers,

Signature · 1 line
Julien Grall
#8 lore raw

PATCH v9 4/8 xen: add field "flags" to cover all internal CDF_XXX

In reply to Penny Zheng

With more and more CDF_xxx internal flags in and to save the space, this commit introduces a new field "flags" in struct domain to store CDF_* internal flags directly.

Another new CDF_xxx will be introduced in the next patch.

---
v9 changes:
- no change
---
v8 changes:
- no change
---
v7 changes:
- no change
---
v6 changes:
- no change
---
v5 changes:
- no change
---
v4 changes:
- no change
---
v3 changes:
- change fixed width type uint32_t to unsigned int
- change "flags" to a more descriptive name "cdf"
---
v2 changes:
- let "flags" live in the struct domain. So other arch can take advantage of it in the future
- fix coding style

---
 xen/arch/arm/domain.c             | 2 --
 xen/arch/arm/include/asm/domain.h | 3 +--
 xen/common/domain.c               | 3 +++
 xen/include/xen/sched.h           | 3 +++
 4 files changed, 7 insertions(+), 4 deletions(-)
xen/arch/arm/domain.c +0 -2
index 2f8eaab7b5..4722988ee7 100644
@@ -709,8 +709,6 @@ int arch_domain_create(struct domain *d,
     ioreq_domain_init(d);
 #endif
 
-    d->arch.directmap = flags & CDF_directmap;
-
     /* p2m_init relies on some value initialized by the IOMMU subsystem */
     if ( (rc = iommu_domain_init(d, config->iommu_opts)) != 0 )
         goto fail;
xen/arch/arm/include/asm/domain.h +1 -2
index cd9ce19b4b..26a8348eed 100644
@@ -29,7 +29,7 @@ enum domain_type {
 #define is_64bit_domain(d) (0)
 #endif
 
-#define is_domain_direct_mapped(d) (d)->arch.directmap
+#define is_domain_direct_mapped(d) ((d)->cdf & CDF_directmap)
 
 /*
  * Is the domain using the host memory layout?
@@ -104,7 +104,6 @@ struct arch_domain
     void *tee;
 #endif
 
-    bool directmap;
 }  __cacheline_aligned;
 
 struct arch_vcpu
xen/common/domain.c +3 -0
index 618410e3b2..7062393e37 100644
@@ -567,6 +567,9 @@ struct domain *domain_create(domid_t domid,
     /* Sort out our idea of is_system_domain(). */
     d->domain_id = domid;
 
+    /* Holding CDF_* internal flags. */
+    d->cdf = flags;
+
     /* Debug sanity. */
     ASSERT(is_system_domain(d) ? config == NULL : config != NULL);
 
xen/include/xen/sched.h +3 -0
index b9515eb497..98e8001c89 100644
@@ -596,6 +596,9 @@ struct domain
         struct ioreq_server     *server[MAX_NR_IOREQ_SERVERS];
     } ioreq_server;
 #endif
+
+    /* Holding CDF_* constant. Internal flags for domain creation. */
+    unsigned int cdf;
 };
 
 static inline struct page_list_head *page_to_list(
Signature · 1 line
2.25.1
#9 lore raw

PATCH v9 5/8 xen/arm: introduce CDF_staticmem

In reply to Penny Zheng

In order to have an easy and quick way to find out whether this domain memory is statically configured, this commit introduces a new flag CDF_staticmem and a new helper is_domain_using_staticmem() to tell.

---
v9 changes:
- no change
---
v8 changes:
- #ifdef-ary around is_domain_using_staticmem() is not needed anymore
---
v7 changes:
- IS_ENABLED(CONFIG_STATIC_MEMORY) would not be needed anymore
---
v6 changes:
- move non-zero is_domain_using_staticmem() from ARM header to common header
---
v5 changes:
- guard "is_domain_using_staticmem" under CONFIG_STATIC_MEMORY
- #define is_domain_using_staticmem zero if undefined
---
v4 changes:
- no changes
---
v3 changes:
- change name from "is_domain_static()" to "is_domain_using_staticmem"
---
v2 changes:
- change name from "is_domain_on_static_allocation" to "is_domain_static()"

---
 xen/arch/arm/domain_build.c | 5 ++++-
 xen/include/xen/domain.h    | 8 ++++++++
 2 files changed, 12 insertions(+), 1 deletion(-)
xen/arch/arm/domain_build.c +4 -1
index 3fd1186b53..b76a84e8f5 100644
@@ -3287,9 +3287,12 @@ void __init create_domUs(void)
         if ( !dt_device_is_compatible(node, "xen,domain") )
             continue;
 
+        if ( dt_find_property(node, "xen,static-mem", NULL) )
+            flags |= CDF_staticmem;
+
         if ( dt_property_read_bool(node, "direct-map") )
         {
-            if ( !IS_ENABLED(CONFIG_STATIC_MEMORY) || !dt_find_property(node, "xen,static-mem", NULL) )
+            if ( !(flags & CDF_staticmem) )
                 panic("direct-map is not valid for domain %s without static allocation.\n",
                       dt_node_name(node));
 
xen/include/xen/domain.h +8 -0
index 628b14b086..2c8116afba 100644
@@ -35,6 +35,14 @@ void arch_get_domain_info(const struct domain *d,
 /* Should domain memory be directly mapped? */
 #define CDF_directmap            (1U << 1)
 #endif
+/* Is domain memory on static allocation? */
+#ifdef CONFIG_STATIC_MEMORY
+#define CDF_staticmem            (1U << 2)
+#else
+#define CDF_staticmem            0
+#endif
+
+#define is_domain_using_staticmem(d) ((d)->cdf & CDF_staticmem)
 
 /*
  * Arch-specifics.
Signature · 1 line
2.25.1
#10 lore raw

PATCH v9 6/8 xen/arm: unpopulate memory when domain is static

In reply to Penny Zheng

Today when a domain unpopulates the memory on runtime, they will always hand the memory back to the heap allocator. And it will be a problem if domain is static.

Pages as guest RAM for static domain shall be reserved to only this domain and not be used for any other purposes, so they shall never go back to heap allocator.

This commit puts reserved pages on the new list resv_page_list only after having taken them off the "normal" list, when the last ref dropped.

---
v9 change:
- remove macro helper put_static_page, and just expand its code inside free_domstatic_page
---
v8 changes:
- adapt this patch for newly introduced free_domstatic_page
- order as a parameter is not needed here, as all staticmem operations are limited to order-0 regions
- move d->page_alloc_lock after operation on d->resv_page_list
---
v7 changes:
- Add page on the rsv_page_list *after* it has been freed
---
v6 changes:
- refine in-code comment
- move PGC_static !CONFIG_STATIC_MEMORY definition to common header
---
v5 changes:
- adapt this patch for PGC_staticmem
---
v4 changes:
- no changes
---
v3 changes:
- have page_list_del() just once out of the if()
- remove resv_pages counter
- make arch_free_heap_page be an expression, not a compound statement.
---
v2 changes:
- put reserved pages on resv_page_list after having taken them off the "normal" list

---
 xen/common/domain.c     | 4 ++++
 xen/common/page_alloc.c | 8 ++++++--
 xen/include/xen/sched.h | 3 +++
 3 files changed, 13 insertions(+), 2 deletions(-)
xen/common/domain.c +4 -0
index 7062393e37..c23f449451 100644
@@ -604,6 +604,10 @@ struct domain *domain_create(domid_t domid,
     INIT_PAGE_LIST_HEAD(&d->page_list);
     INIT_PAGE_LIST_HEAD(&d->extra_page_list);
     INIT_PAGE_LIST_HEAD(&d->xenpage_list);
+#ifdef CONFIG_STATIC_MEMORY
+    INIT_PAGE_LIST_HEAD(&d->resv_page_list);
+#endif
+
 
     spin_lock_init(&d->node_affinity_lock);
     d->node_affinity = NODE_MASK_ALL;
xen/common/page_alloc.c +6 -2
index 45bd88a685..a568be55e3 100644
@@ -2674,10 +2674,14 @@ void free_domstatic_page(struct page_info *page)
 
     drop_dom_ref = !domain_adjust_tot_pages(d, -1);
 
-    spin_unlock_recursive(&d->page_alloc_lock);
-
     free_staticmem_pages(page, 1, scrub_debug);
 
+    /* Add page on the resv_page_list *after* it has been freed. */
+    if ( !drop_dom_ref )
+        page_list_add_tail(page, &d->resv_page_list);
+
+    spin_unlock_recursive(&d->page_alloc_lock);
+
     if ( drop_dom_ref )
         put_domain(d);
 }
xen/include/xen/sched.h +3 -0
index 98e8001c89..d4fbd3dea7 100644
@@ -381,6 +381,9 @@ struct domain
     struct page_list_head page_list;  /* linked list */
     struct page_list_head extra_page_list; /* linked list (size extra_pages) */
     struct page_list_head xenpage_list; /* linked list (size xenheap_pages) */
+#ifdef CONFIG_STATIC_MEMORY
+    struct page_list_head resv_page_list; /* linked list */
+#endif
 
     /*
      * This field should only be directly accessed by domain_adjust_tot_pages()
Signature · 1 line
2.25.1
#11 lore raw

Re: [PATCH v9 6/8] xen/arm: unpopulate memory when domain is static

Quoted 10 lines · Penny Zheng

Today when a domain unpopulates the memory on runtime, they will always hand the memory back to the heap allocator. And it will be a problem if domain is static.

Pages as guest RAM for static domain shall be reserved to only this domain and not be used for any other purposes, so they shall never go back to heap allocator.

This commit puts reserved pages on the new list resv_page_list only after having taken them off the "normal" list, when the last ref dropped.

I guess this wording somehow relates to ...

xen/common/page_alloc.c void free_domstatic_page(struct page_info *page) · line 2681

Quoted 13 lines
xen/common/page_alloc.c +3 -2
@@ -2674,10 +2674,14 @@ void free_domstatic_page(struct page_info *page)
 
     drop_dom_ref = !domain_adjust_tot_pages(d, -1);
 
-    spin_unlock_recursive(&d->page_alloc_lock);
-
     free_staticmem_pages(page, 1, scrub_debug);
 
+    /* Add page on the resv_page_list *after* it has been freed. */
+    if ( !drop_dom_ref )
+        page_list_add_tail(page, &d->resv_page_list);

... the conditional used here. I cannot, however, figure why there is this conditional (and said part of the description also doesn't help me figure it out).

As an aside: A title prefix of xen/arm: suggests you're mostly touching Arm code. But you're touching exclusively common code here. I for one would almost have skipped this patch (more than once) when deciding which ones (may) want me looking at them.

Jan

#12 lore raw

RE: [PATCH v9 6/8] xen/arm: unpopulate memory when domain is static

Hi Jan

Quoted 36 lines

-----Original Message-----
From: Jan Beulich <[email protected]>
Sent: Monday, July 25, 2022 11:36 PM
To: Penny Zheng <[email protected]>
Cc: Wei Chen <[email protected]>; Andrew Cooper <[email protected]>; George Dunlap <[email protected]>; Julien Grall <[email protected]>; Stefano Stabellini <[email protected]>; Wei Liu <[email protected]>; [email protected]
Subject: Re: [PATCH v9 6/8] xen/arm: unpopulate memory when domain is static

Quoted 10 lines · Penny Zheng

Today when a domain unpopulates the memory on runtime, they will always hand the memory back to the heap allocator. And it will be a problem if domain is static.

Pages as guest RAM for static domain shall be reserved to only this domain and not be used for any other purposes, so they shall never go back to heap allocator.

This commit puts reserved pages on the new list resv_page_list only after having taken them off the "normal" list, when the last ref dropped.

I guess this wording somehow relates to ...

Quoted 11 lines
xen/common/page_alloc.c +0 -2
@@ -2674,10 +2674,14 @@ void free_domstatic_page(struct page_info
 *page)
 
     drop_dom_ref = !domain_adjust_tot_pages(d, -1);
 
-    spin_unlock_recursive(&d->page_alloc_lock);
-
     free_staticmem_pages(page, 1, scrub_debug);
 
Quoted 8 lines

xen/common/page_alloc.c void free_domstatic_page(struct page_info *page) · line 2681

Quoted 3 lines
xen/common/page_alloc.c +3 -0
+    /* Add page on the resv_page_list *after* it has been freed. */
+    if ( !drop_dom_ref )
+        page_list_add_tail(page, &d->resv_page_list);

... the conditional used here. I cannot, however, figure why there is this conditional (and said part of the description also doesn't help me figure it out).

I was thinking that if drop_dom_ref true, then later the whole domain struct will be released, then there is no need to add the page to d->resv_page_list

Quoted 5 lines

As an aside: A title prefix of xen/arm: suggests you're mostly touching Arm code. But you're touching exclusively common code here.
I for one would almost have skipped this patch (more than once) when deciding which ones (may) want me looking at them.

Sorry for that, I’ll fix it

Quoted 1 line

Jan

#13 lore raw

Re: [PATCH v9 6/8] xen/arm: unpopulate memory when domain is static

Quoted 38 lines · Penny Zheng
Quoted 29 lines

-----Original Message-----
From: Jan Beulich <[email protected]>
Sent: Monday, July 25, 2022 11:36 PM

Quoted 10 lines · Penny Zheng

Today when a domain unpopulates the memory on runtime, they will always hand the memory back to the heap allocator. And it will be a problem if domain is static.

Pages as guest RAM for static domain shall be reserved to only this domain and not be used for any other purposes, so they shall never go back to heap allocator.

This commit puts reserved pages on the new list resv_page_list only after having taken them off the "normal" list, when the last ref dropped.

I guess this wording somehow relates to ...

Quoted 11 lines
xen/common/page_alloc.c +0 -2
@@ -2674,10 +2674,14 @@ void free_domstatic_page(struct page_info
 *page)
 
     drop_dom_ref = !domain_adjust_tot_pages(d, -1);
 
-    spin_unlock_recursive(&d->page_alloc_lock);
-
     free_staticmem_pages(page, 1, scrub_debug);
 
Quoted 8 lines

xen/common/page_alloc.c void free_domstatic_page(struct page_info *page) · line 2681

Quoted 3 lines
xen/common/page_alloc.c +3 -0
+    /* Add page on the resv_page_list *after* it has been freed. */
+    if ( !drop_dom_ref )
+        page_list_add_tail(page, &d->resv_page_list);

... the conditional used here. I cannot, however, figure why there is this conditional (and said part of the description also doesn't help me figure it out).

Quoted 2 lines

I was thinking that if drop_dom_ref true, then later the whole domain struct will be released, then there is no need to add the page to d->resv_page_list

If that was the intention, then it should have been spelled out imo. But I think it's not a good idea to skip the list addition in that one special case, when keeping things uniform is actually cheaper _and_ more consistent. In the end I expect sooner or later there'll be a desire to restart DomU-s which have crashed, which would include re-using their designated static memory. Then you want to avoid leaking pages like it would happen with your approach.

Jan

#14 lore raw

PATCH v9 7/8 xen: introduce prepare_staticmem_pages

In reply to Penny Zheng

Later, we want to use acquire_domstatic_pages() for populating memory for static domain on runtime, however, there are a lot of pointless work (checking mfn_valid(), scrubbing the free part, cleaning the cache...) considering we know the page is valid and belong to the guest.

This commit splits acquire_staticmem_pages() in two parts, and introduces prepare_staticmem_pages to bypass all "pointless work".

---
v8 changes:
- no change
---
v7 changes:
- no change
---
v6 changes:
- adapt to PGC_static
---
v5 changes:
- new commit

---
 xen/common/page_alloc.c | 61 ++++++++++++++++++++++++-----------------
 1 file changed, 36 insertions(+), 25 deletions(-)
xen/common/page_alloc.c +36 -25
index a568be55e3..9e150946f9 100644
@@ -2686,26 +2686,13 @@ void free_domstatic_page(struct page_info *page)
         put_domain(d);
 }
 
-/*
- * Acquire nr_mfns contiguous reserved pages, starting at #smfn, of
- * static memory.
- * This function needs to be reworked if used outside of boot.
- */
-static struct page_info * __init acquire_staticmem_pages(mfn_t smfn,
-                                                         unsigned long nr_mfns,
-                                                         unsigned int memflags)
+static bool __init prepare_staticmem_pages(struct page_info *pg,
+                                           unsigned long nr_mfns,
+                                           unsigned int memflags)
 {
     bool need_tlbflush = false;
     uint32_t tlbflush_timestamp = 0;
     unsigned long i;
-    struct page_info *pg;
-
-    ASSERT(nr_mfns);
-    for ( i = 0; i < nr_mfns; i++ )
-        if ( !mfn_valid(mfn_add(smfn, i)) )
-            return NULL;
-
-    pg = mfn_to_page(smfn);
 
     spin_lock(&heap_lock);
 
@@ -2716,7 +2703,7 @@ static struct page_info * __init acquire_staticmem_pages(mfn_t smfn,
         {
             printk(XENLOG_ERR
                    "pg[%lu] Static MFN %"PRI_mfn" c=%#lx t=%#x\n",
-                   i, mfn_x(smfn) + i,
+                   i, mfn_x(page_to_mfn(pg)) + i,
                    pg[i].count_info, pg[i].tlbflush_timestamp);
             goto out_err;
         }
@@ -2740,6 +2727,38 @@ static struct page_info * __init acquire_staticmem_pages(mfn_t smfn,
     if ( need_tlbflush )
         filtered_flush_tlb_mask(tlbflush_timestamp);
 
+    return true;
+
+ out_err:
+    while ( i-- )
+        pg[i].count_info = PGC_static | PGC_state_free;
+
+    spin_unlock(&heap_lock);
+
+    return false;
+}
+
+/*
+ * Acquire nr_mfns contiguous reserved pages, starting at #smfn, of
+ * static memory.
+ * This function needs to be reworked if used outside of boot.
+ */
+static struct page_info * __init acquire_staticmem_pages(mfn_t smfn,
+                                                         unsigned long nr_mfns,
+                                                         unsigned int memflags)
+{
+    unsigned long i;
+    struct page_info *pg;
+
+    ASSERT(nr_mfns);
+    for ( i = 0; i < nr_mfns; i++ )
+        if ( !mfn_valid(mfn_add(smfn, i)) )
+            return NULL;
+
+    pg = mfn_to_page(smfn);
+    if ( !prepare_staticmem_pages(pg, nr_mfns, memflags) )
+        return NULL;
+
     /*
      * Ensure cache and RAM are consistent for platforms where the guest
      * can control its own visibility of/through the cache.
@@ -2748,14 +2767,6 @@ static struct page_info * __init acquire_staticmem_pages(mfn_t smfn,
         flush_page_to_ram(mfn_x(smfn) + i, !(memflags & MEMF_no_icache_flush));
 
     return pg;
-
- out_err:
-    while ( i-- )
-        pg[i].count_info = PGC_static | PGC_state_free;
-
-    spin_unlock(&heap_lock);
-
-    return NULL;
 }
 
 /*
Signature · 1 line
2.25.1
#15 lore raw

PATCH v9 8/8 xen: retrieve reserved pages on populate_physmap

In reply to Penny Zheng

When a static domain populates memory through populate_physmap at runtime, it shall retrieve reserved pages from resv_page_list to make sure that guest RAM is still restricted in statically configured memory regions. This commit also introduces a new helper acquire_reserved_page to make it work.

---
v9 changes:
- Use ASSERT_ALLOC_CONTEXT() in acquire_reserved_page
- Add free_staticmem_pages to undo prepare_staticmem_pages when assign_domstatic_pages fails
- Remove redundant static in error message
---
v8 changes:
- As concurrent free/allocate could modify the resv_page_list, we still need the lock
---
v7 changes:
- remove the lock, since we add the page to rsv_page_list after it has been totally freed.
---
v6 changes:
- drop the lock before returning
---
v5 changes:
- extract common codes for assigning pages into a helper assign_domstatic_pages
- refine commit message
- remove stub function acquire_reserved_page
- Alloc/free of memory can happen concurrently. So access to rsv_page_list needs to be protected with a spinlock
---
v4 changes:
- miss dropping __init in acquire_domstatic_pages
- add the page back to the reserved list in case of error
- remove redundant printk
- refine log message and make it warn level
---
v3 changes:
- move is_domain_using_staticmem to the common header file
- remove #ifdef CONFIG_STATIC_MEMORY-ary
- remove meaningless page_to_mfn(page) in error log
---
v2 changes:
- introduce acquire_reserved_page to retrieve reserved pages from resv_page_list
- forbid non-zero-order requests in populate_physmap
- let is_domain_static return ((void)(d), false) on x86

---
 xen/common/memory.c     | 23 +++++++++++++
 xen/common/page_alloc.c | 74 +++++++++++++++++++++++++++++++----------
 xen/include/xen/mm.h    |  1 +
 3 files changed, 81 insertions(+), 17 deletions(-)
xen/common/memory.c +23 -0
index f6f794914d..d486ebd8b9 100644
@@ -245,6 +245,29 @@ static void populate_physmap(struct memop_args *a)
 
                 mfn = _mfn(gpfn);
             }
+            else if ( is_domain_using_staticmem(d) )
+            {
+                /*
+                 * No easy way to guarantee the retrieved pages are contiguous,
+                 * so forbid non-zero-order requests here.
+                 */
+                if ( a->extent_order != 0 )
+                {
+                    gdprintk(XENLOG_WARNING,
+                             "Cannot allocate static order-%u pages for %pd\n",
+                             a->extent_order, d);
+                    goto out;
+                }
+
+                mfn = acquire_reserved_page(d, a->memflags);
+                if ( mfn_eq(mfn, INVALID_MFN) )
+                {
+                    gdprintk(XENLOG_WARNING,
+                             "%pd: failed to retrieve a reserved page\n",
+                             d);
+                    goto out;
+                }
+            }
             else
             {
                 page = alloc_domheap_pages(d, a->extent_order, a->memflags);
xen/common/page_alloc.c +57 -17

93 lines not on this page · raw message

xen/include/xen/mm.h +1 -0
index f1a7d5c991..9ea87d2199 100644
@@ -198,6 +198,7 @@ struct npfec {
 #else
 #define MAX_ORDER 20 /* 2^20 contiguous pages */
 #endif
+mfn_t acquire_reserved_page(struct domain *d, unsigned int memflags);
 
 /* Private domain structs for DOMID_XEN, DOMID_IO, etc. */
 extern struct domain *dom_xen, *dom_io;
Signature · 1 line
2.25.1
#16 lore raw

Re: [PATCH v9 8/8] xen: retrieve reserved pages on populate_physmap

Quoted 11 lines · Penny Zheng

When a static domain populates memory through populate_physmap at runtime, it shall retrieve reserved pages from resv_page_list to make sure that guest RAM is still restricted in statically configured memory regions. This commit also introduces a new helper acquire_reserved_page to make it work.

---
v9 changes:
- Use ASSERT_ALLOC_CONTEXT() in acquire_reserved_page
- Add free_staticmem_pages to undo prepare_staticmem_pages when assign_domstatic_pages fails

May I suggest to re-consider naming of the various functions? Undoing what "prepare" did by "free" is, well, counterintuitive.

Quoted 21 lines
xen/common/page_alloc.c +21 -0
+/*
+ * Acquire a page from reserved page list(resv_page_list), when populating
+ * memory for static domain on runtime.
+ */
+mfn_t acquire_reserved_page(struct domain *d, unsigned int memflags)
+{
+    struct page_info *page;
+
+    ASSERT_ALLOC_CONTEXT();
+
+    /* Acquire a page from reserved page list(resv_page_list). */
+    spin_lock(&d->page_alloc_lock);
+    page = page_list_remove_head(&d->resv_page_list);
+    spin_unlock(&d->page_alloc_lock);
+    if ( unlikely(!page) )
+        return INVALID_MFN;
+
+    if ( !prepare_staticmem_pages(page, 1, memflags) )
+        goto fail;
+
+    if ( assign_domstatic_pages(d, page, 1, memflags) )

xen/common/page_alloc.c int __init acquire_domstatic_pages(struct domain *d, mfn_t smfn, · line 2844

Quoted 10 lines
xen/common/page_alloc.c +10 -0
+        goto fail_assign;
+
+    return page_to_mfn(page);
+
+ fail_assign:
+    free_staticmem_pages(page, 1, memflags & MEMF_no_scrub);
+ fail:
+    page_list_add_tail(page, &d->resv_page_list);
+    return INVALID_MFN;
+}

What about locking on this error path?

Jan

#17 lore raw

RE: [PATCH v9 8/8] xen: retrieve reserved pages on populate_physmap

Hi Jan

Sorry for the delayed response.

Quoted 27 lines

-----Original Message-----
From: Xen-devel <[email protected]> On Behalf Of Jan Beulich
Sent: Monday, July 25, 2022 11:44 PM
To: Penny Zheng <[email protected]>
Cc: Wei Chen <[email protected]>; Andrew Cooper <[email protected]>; George Dunlap <[email protected]>; Julien Grall <[email protected]>; Stefano Stabellini <[email protected]>; Wei Liu <[email protected]>; [email protected]
Subject: Re: [PATCH v9 8/8] xen: retrieve reserved pages on populate_physmap

Quoted 3 lines · Penny Zheng

When a static domain populates memory through populate_physmap at runtime, it shall retrieve reserved pages from resv_page_list to make sure that guest RAM is still restricted in statically configured memory

regions.

Quoted 1 line

This commit also introduces a new helper acquire_reserved_page to make

it work.

Quoted 7 lines

---
v9 changes:
- Use ASSERT_ALLOC_CONTEXT() in acquire_reserved_page
- Add free_staticmem_pages to undo prepare_staticmem_pages when assign_domstatic_pages fails

Quoted 3 lines

May I suggest to re-consider naming of the various functions? Undoing what "prepare" did by "free" is, well, counterintuitive.

How about change the name "prepare_staticmem_pages" to "allocate_staticmem_pages"?

Quoted 26 lines
Quoted 26 lines
xen/common/page_alloc.c +26 -0
+/*
+ * Acquire a page from reserved page list(resv_page_list), when
+populating
+ * memory for static domain on runtime.
+ */
+mfn_t acquire_reserved_page(struct domain *d, unsigned int memflags)
+{
+    struct page_info *page;
+
+    ASSERT_ALLOC_CONTEXT();
+
+    /* Acquire a page from reserved page list(resv_page_list). */
+    spin_lock(&d->page_alloc_lock);
+    page = page_list_remove_head(&d->resv_page_list);
+    spin_unlock(&d->page_alloc_lock);
+    if ( unlikely(!page) )
+        return INVALID_MFN;
+
+    if ( !prepare_staticmem_pages(page, 1, memflags) )
+        goto fail;
+
+    if ( assign_domstatic_pages(d, page, 1, memflags) )
+        goto fail_assign;
+
+    return page_to_mfn(page);
+
Quoted 9 lines

xen/common/page_alloc.c int __init acquire_domstatic_pages(struct domain *d, mfn_t smfn, · line 2844

Quoted 6 lines

+ fail_assign:

xen/common/page_alloc.c +5 -0
+    free_staticmem_pages(page, 1, memflags & MEMF_no_scrub);
+ fail:
+    page_list_add_tail(page, &d->resv_page_list);
+    return INVALID_MFN;
+}

What about locking on this error path?

Right, locking is needed here too:
fail:
spin_lock(&d->page_alloc_lock);
page_list_add_tail(page, &d->resv_page_list);
spin_unlock(&d->page_alloc_lock);

Quoted 1 line

Jan

#18 lore raw

Re: [PATCH v9 8/8] xen: retrieve reserved pages on populate_physmap

Quoted 20 lines · Penny Zheng
Quoted 20 lines

-----Original Message-----
From: Xen-devel <[email protected]> On Behalf Of Jan Beulich
Sent: Monday, July 25, 2022 11:44 PM

Quoted 3 lines · Penny Zheng

When a static domain populates memory through populate_physmap at runtime, it shall retrieve reserved pages from resv_page_list to make sure that guest RAM is still restricted in statically configured memory

regions.

Quoted 1 line

This commit also introduces a new helper acquire_reserved_page to make

it work.

Quoted 7 lines

---
v9 changes:
- Use ASSERT_ALLOC_CONTEXT() in acquire_reserved_page
- Add free_staticmem_pages to undo prepare_staticmem_pages when assign_domstatic_pages fails

Quoted 5 lines
Quoted 3 lines

May I suggest to re-consider naming of the various functions? Undoing what "prepare" did by "free" is, well, counterintuitive.

How about change the name "prepare_staticmem_pages" to "allocate_staticmem_pages"?

Perhaps - if what the function does really resembles allocation in some way. So far I wasn't really certain in that regard, and hence I was wondering whether "prepare" doesn't better describe what it does, but then its inverse also doesn't really "free" anything.

Jan

#19 lore raw

RE: [PATCH v9 8/8] xen: retrieve reserved pages on populate_physmap

Hi jan

Quoted 38 lines

-----Original Message-----
From: Jan Beulich <[email protected]>
Sent: Tuesday, August 9, 2022 3:59 PM
To: Penny Zheng <[email protected]>
Cc: Wei Chen <[email protected]>; Andrew Cooper <[email protected]>; George Dunlap <[email protected]>; Julien Grall <[email protected]>; Stefano Stabellini <[email protected]>; Wei Liu <[email protected]>; [email protected]
Subject: Re: [PATCH v9 8/8] xen: retrieve reserved pages on populate_physmap

Quoted 26 lines · Penny Zheng
Quoted 25 lines

-----Original Message-----
From: Xen-devel <[email protected]> On Behalf Of Jan Beulich
Sent: Monday, July 25, 2022 11:44 PM

Quoted 4 lines · Penny Zheng

When a static domain populates memory through populate_physmap at runtime, it shall retrieve reserved pages from resv_page_list to make sure that guest RAM is still restricted in statically configured memory

regions.

Quoted 2 lines

This commit also introduces a new helper acquire_reserved_page to make

it work.

Quoted 7 lines

---
v9 changes:
- Use ASSERT_ALLOC_CONTEXT() in acquire_reserved_page
- Add free_staticmem_pages to undo prepare_staticmem_pages when assign_domstatic_pages fails

May I suggest to re-consider naming of the various functions? Undoing what "prepare" did by "free" is, well, counterintuitive.

Quoted 8 lines
Quoted 1 line

How about change the name "prepare_staticmem_pages" to

"allocate_staticmem_pages"?

Perhaps - if what the function does really resembles allocation in some way. So far I wasn't really certain in that regard, and hence I was wondering whether "prepare" doesn't better describe what it does, but then its inverse also doesn't really "free" anything.

Hmmmm, “prepare” with “destroy” in its inverse? Do you have any suggestion in mind?

Quoted 1 line

Jan

#20 lore raw

Re: [PATCH v9 8/8] xen: retrieve reserved pages on populate_physmap

Quoted 34 lines · Penny Zheng
Quoted 34 lines

-----Original Message-----
From: Jan Beulich <[email protected]>
Sent: Tuesday, August 9, 2022 3:59 PM

Quoted 22 lines · Penny Zheng
Quoted 22 lines

-----Original Message-----
From: Xen-devel <[email protected]> On Behalf Of Jan Beulich
Sent: Monday, July 25, 2022 11:44 PM

Quoted 4 lines · Penny Zheng

When a static domain populates memory through populate_physmap at runtime, it shall retrieve reserved pages from resv_page_list to make sure that guest RAM is still restricted in statically configured memory

regions.

Quoted 2 lines

This commit also introduces a new helper acquire_reserved_page to make

it work.

Quoted 7 lines

---
v9 changes:
- Use ASSERT_ALLOC_CONTEXT() in acquire_reserved_page
- Add free_staticmem_pages to undo prepare_staticmem_pages when assign_domstatic_pages fails

Quoted 5 lines
Quoted 3 lines

May I suggest to re-consider naming of the various functions? Undoing what "prepare" did by "free" is, well, counterintuitive.

How about change the name "prepare_staticmem_pages" to

"allocate_staticmem_pages"?

Quoted 7 lines
Quoted 5 lines

Perhaps - if what the function does really resembles allocation in some way. So far I wasn't really certain in that regard, and hence I was wondering whether "prepare" doesn't better describe what it does, but then its inverse also doesn't really "free" anything.

Hmmmm, “prepare” with “destroy” in its inverse? Do you have any suggestion in mind?

To be honest I was hoping you would make an attempt at finding a suitable pair of verbs. To me "destroy" is more the opposite of "create", and I'm unable to think of a good opposite of "prepare" (short of resorting to "unprepare"); if I really needed to come up with something then it would likely be "cleanup", albeit I'd not be overly happy with that either.

Jan

#21 lore raw

RE: [PATCH v9 8/8] xen: retrieve reserved pages on populate_physmap

Hi jan

Quoted 54 lines

-----Original Message-----
From: Jan Beulich <[email protected]>
Sent: Tuesday, August 9, 2022 4:27 PM
To: Penny Zheng <[email protected]>
Cc: Wei Chen <[email protected]>; Andrew Cooper <[email protected]>; George Dunlap <[email protected]>; Julien Grall <[email protected]>; Stefano Stabellini <[email protected]>; Wei Liu <[email protected]>; [email protected]
Subject: Re: [PATCH v9 8/8] xen: retrieve reserved pages on populate_physmap

Quoted 4 lines · Penny Zheng
Quoted 4 lines

-----Original Message-----
From: Jan Beulich <[email protected]>
Sent: Tuesday, August 9, 2022 3:59 PM

Quoted 8 lines
Quoted 8 lines · Penny Zheng
Quoted 7 lines
Quoted 7 lines

-----Original Message-----
From: Xen-devel <[email protected]> On Behalf Of Jan Beulich
Sent: Monday, July 25, 2022 11:44 PM

Quoted 1 line · Penny Zheng

When a static domain populates memory through populate_physmap

at

Quoted 15 lines
Quoted 15 lines
Quoted 15 lines
Quoted 15 lines
Quoted 3 lines

runtime, it shall retrieve reserved pages from resv_page_list to make sure that guest RAM is still restricted in statically configured memory

regions.

Quoted 2 lines

This commit also introduces a new helper acquire_reserved_page to make

it work.

Quoted 7 lines

---
v9 changes:
- Use ASSERT_ALLOC_CONTEXT() in acquire_reserved_page
- Add free_staticmem_pages to undo prepare_staticmem_pages when assign_domstatic_pages fails

Quoted 8 lines
Quoted 8 lines
Quoted 5 lines
Quoted 3 lines

May I suggest to re-consider naming of the various functions? Undoing what "prepare" did by "free" is, well, counterintuitive.

How about change the name "prepare_staticmem_pages" to

"allocate_staticmem_pages"?

Perhaps - if what the function does really resembles allocation in some

way.

Quoted 5 lines
Quoted 4 lines

So far I wasn't really certain in that regard, and hence I was wondering whether "prepare" doesn't better describe what it does, but then its inverse also doesn't really "free" anything.

Quoted 9 lines
Quoted 1 line

Hmmmm, “prepare” with “destroy” in its inverse? Do you have any

suggestion in mind?

To be honest I was hoping you would make an attempt at finding a suitable pair of verbs. To me "destroy" is more the opposite of "create", and I'm unable to think of a good opposite of "prepare" (short of resorting to "unprepare"); if I really needed to come up with something then it would likely be "cleanup", albeit I'd not be overly happy with that either.

Maybe unprepare is better here, I was searching linux code for the help, and they are using prepare/unprepare as a pair of verbs a lot in drivers codes.

For the renaming here, I suggest to fix it with a new commit, since free_staticmem_pages has already been merged.

Quoted 1 line

Jan

#22 lore raw

Re: [PATCH v9 8/8] xen: retrieve reserved pages on populate_physmap

Hi Penny,

Quoted 68 lines · Penny Zheng

Hi jan

Quoted 54 lines

-----Original Message-----
From: Jan Beulich <[email protected]>
Sent: Tuesday, August 9, 2022 4:27 PM
To: Penny Zheng <[email protected]>
Cc: Wei Chen <[email protected]>; Andrew Cooper <[email protected]>; George Dunlap <[email protected]>; Julien Grall <[email protected]>; Stefano Stabellini <[email protected]>; Wei Liu <[email protected]>; [email protected]
Subject: Re: [PATCH v9 8/8] xen: retrieve reserved pages on populate_physmap

Quoted 4 lines · Penny Zheng
Quoted 4 lines

-----Original Message-----
From: Jan Beulich <[email protected]>
Sent: Tuesday, August 9, 2022 3:59 PM

Quoted 8 lines
Quoted 8 lines · Penny Zheng
Quoted 7 lines
Quoted 7 lines

-----Original Message-----
From: Xen-devel <[email protected]> On Behalf Of Jan Beulich
Sent: Monday, July 25, 2022 11:44 PM

Quoted 1 line · Penny Zheng

When a static domain populates memory through populate_physmap

at

Quoted 15 lines
Quoted 15 lines
Quoted 15 lines
Quoted 15 lines
Quoted 3 lines

runtime, it shall retrieve reserved pages from resv_page_list to make sure that guest RAM is still restricted in statically configured memory

regions.

Quoted 2 lines

This commit also introduces a new helper acquire_reserved_page to make

it work.

Quoted 7 lines

---
v9 changes:
- Use ASSERT_ALLOC_CONTEXT() in acquire_reserved_page
- Add free_staticmem_pages to undo prepare_staticmem_pages when assign_domstatic_pages fails

Quoted 8 lines
Quoted 8 lines
Quoted 5 lines
Quoted 3 lines

May I suggest to re-consider naming of the various functions? Undoing what "prepare" did by "free" is, well, counterintuitive.

How about change the name "prepare_staticmem_pages" to

"allocate_staticmem_pages"?

Perhaps - if what the function does really resembles allocation in some

way.

Quoted 5 lines
Quoted 4 lines

So far I wasn't really certain in that regard, and hence I was wondering whether "prepare" doesn't better describe what it does, but then its inverse also doesn't really "free" anything.

Quoted 9 lines
Quoted 1 line

Hmmmm, “prepare” with “destroy” in its inverse? Do you have any

suggestion in mind?

To be honest I was hoping you would make an attempt at finding a suitable pair of verbs. To me "destroy" is more the opposite of "create", and I'm unable to think of a good opposite of "prepare" (short of resorting to "unprepare"); if I really needed to come up with something then it would likely be "cleanup", albeit I'd not be overly happy with that either.

Quoted 5 lines

Maybe unprepare is better here, I was searching linux code for the help, and they are using prepare/unprepare as a pair of verbs a lot in drivers codes.

For the renaming here, I suggest to fix it with a new commit, since free_staticmem_pages has already been merged.

I think that unprepare is ok and it make sense to do this in an independent patch.

@jan: can you confirm that you agree with this way to go ?

Cheers
Bertrand

#23 lore raw

Re: [PATCH v9 8/8] xen: retrieve reserved pages on populate_physmap

Quoted 61 lines · Bertrand Marquis
Quoted 61 lines · Penny Zheng
Quoted 47 lines

-----Original Message-----
From: Jan Beulich <[email protected]>
Sent: Tuesday, August 9, 2022 4:27 PM

Quoted 4 lines · Penny Zheng
Quoted 4 lines

-----Original Message-----
From: Jan Beulich <[email protected]>
Sent: Tuesday, August 9, 2022 3:59 PM

Quoted 8 lines
Quoted 8 lines · Penny Zheng
Quoted 7 lines
Quoted 7 lines

-----Original Message-----
From: Xen-devel <[email protected]> On Behalf Of Jan Beulich
Sent: Monday, July 25, 2022 11:44 PM

Quoted 1 line · Penny Zheng

When a static domain populates memory through populate_physmap

at

Quoted 15 lines
Quoted 15 lines
Quoted 15 lines
Quoted 15 lines
Quoted 3 lines

runtime, it shall retrieve reserved pages from resv_page_list to make sure that guest RAM is still restricted in statically configured memory

regions.

Quoted 2 lines

This commit also introduces a new helper acquire_reserved_page to make

it work.

Quoted 7 lines

---
v9 changes:
- Use ASSERT_ALLOC_CONTEXT() in acquire_reserved_page
- Add free_staticmem_pages to undo prepare_staticmem_pages when assign_domstatic_pages fails

Quoted 8 lines
Quoted 8 lines
Quoted 5 lines
Quoted 3 lines

May I suggest to re-consider naming of the various functions? Undoing what "prepare" did by "free" is, well, counterintuitive.

How about change the name "prepare_staticmem_pages" to

"allocate_staticmem_pages"?

Perhaps - if what the function does really resembles allocation in some

way.

Quoted 5 lines
Quoted 4 lines

So far I wasn't really certain in that regard, and hence I was wondering whether "prepare" doesn't better describe what it does, but then its inverse also doesn't really "free" anything.

Quoted 9 lines
Quoted 1 line

Hmmmm, “prepare” with “destroy” in its inverse? Do you have any

suggestion in mind?

To be honest I was hoping you would make an attempt at finding a suitable pair of verbs. To me "destroy" is more the opposite of "create", and I'm unable to think of a good opposite of "prepare" (short of resorting to "unprepare"); if I really needed to come up with something then it would likely be "cleanup", albeit I'd not be overly happy with that either.

Maybe unprepare is better here, I was searching linux code for the help, and they are using prepare/unprepare as a pair of verbs a lot in drivers codes.

Quoted 6 lines
Quoted 2 lines

For the renaming here, I suggest to fix it with a new commit, since free_staticmem_pages has already been merged.

I think that unprepare is ok and it make sense to do this in an independent patch.

@jan: can you confirm that you agree with this way to go ?

I don't mind this, yes.

Jan