Skip to contentSkip to conversation

xen/mm: Optimize init_heap_pages()

PATCH In review Julien Grall 3 participants 18 messages Jul 15, 2022 – Jul 20, 2022

lore
Share & tools
lore
#0 lore raw

v2 cover xen/mm: Optimize init_heap_pages()

From: Julien Grall <[email protected]>

Hi all,

As part of the Live-Update work, we noticed that a big part Xen boot is spent to add pages in the heap. For instance, on when running Xen in a nested envionment on a c5.metal with 90GB, it takes ~1.4s.

This small series is reworking init_heap_pages() to give the pages to free_heap_pages() by chunk rather than one by one.

With this approach, the time spent to init the heap is down to 96 ms in the setup mention above.

There is potentially one more optimization possible that would allow to further reduce the time spent. The new approach is accessing the page information multiple time in separate loop that can potentially be large.

Cheers,

Julien Grall (2):
xen: page_alloc: Don't open-code IS_ALIGNED() xen/heap: Split init_heap_pages() in two xen/heap: pass order to free_heap_pages() in heap init

 xen/common/page_alloc.c | 106 ++++++++++++++++++++++++++++++----------
 1 file changed, 79 insertions(+), 27 deletions(-)
Signature · 1 line
2.32.0
#1 lore raw

PATCH v2 1/3 xen: page_alloc: Don't open-code IS_ALIGNED()

From: Julien Grall <[email protected]>

init_heap_pages() is using an open-code version of IS_ALIGNED(). Replace it to improve the readability of the code.

No functional change intended.

---

Changes in v2:
- Patch added

---
 xen/common/page_alloc.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
xen/common/page_alloc.c +1 -1
index fe0e15429af3..078c2990041d 100644
@@ -1823,7 +1823,7 @@ static void init_heap_pages(
             unsigned long s = mfn_x(page_to_mfn(pg + i));
             unsigned long e = mfn_x(mfn_add(page_to_mfn(pg + nr_pages - 1), 1));
             bool use_tail = (nid == phys_to_nid(pfn_to_paddr(e - 1))) &&
-                            !(s & ((1UL << MAX_ORDER) - 1)) &&
+                            IS_ALIGNED(s, 1UL << MAX_ORDER) &&
                             (find_first_set_bit(e) <= find_first_set_bit(s));
             unsigned long n;
 
Signature · 1 line
2.32.0
Reviewed-by on v2 1/3
#2 lore raw

Re: [PATCH v2 1/3] xen: page_alloc: Don't open-code IS_ALIGNED()

Hi Julien,

Quoted 22 lines · Julien Grall

From: Julien Grall <[email protected]>

init_heap_pages() is using an open-code version of IS_ALIGNED(). Replace it to improve the readability of the code.

No functional change intended.

---

Changes in v2:
- Patch added

---
  xen/common/page_alloc.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
xen/common/page_alloc.c +0 -0
index fe0e15429af3..078c2990041d 100644

xen/common/page_alloc.c static void init_heap_pages( · line 1829

Quoted 8 lines
xen/common/page_alloc.c +1 -1
@@ -1823,7 +1823,7 @@ static void init_heap_pages(
              unsigned long s = mfn_x(page_to_mfn(pg + i));
              unsigned long e = mfn_x(mfn_add(page_to_mfn(pg + nr_pages - 1), 1));
              bool use_tail = (nid == phys_to_nid(pfn_to_paddr(e - 1))) &&
-                            !(s & ((1UL << MAX_ORDER) - 1)) &&
+                            IS_ALIGNED(s, 1UL << MAX_ORDER) &&
                              (find_first_set_bit(e) <= find_first_set_bit(s));
              unsigned long n;
 

LGTM.

#4 lore raw

PATCH v2 2/3 xen/heap: Split init_heap_pages() in two

In reply to Julien Grall

From: Julien Grall <[email protected]>

At the moment, init_heap_pages() will call free_heap_pages() page by page. To reduce the time to initialize the heap, we will want to provide multiple pages at the same time.

init_heap_pages() is now split in two parts:
- init_heap_pages(): will break down the range in multiple set of contiguous pages. For now, the criteria is the pages should belong to the same NUMA node.
- _init_heap_pages(): will initialize a set of pages belonging to the same NUMA node. In a follow-up patch, new requirements will be added (e.g. pages should belong to the same zone). For now the pages are still passed one by one to free_heap_pages().

Note that the comment before init_heap_pages() is heavily outdated and does not reflect the current code. So update it.

This patch is a merge/rework of patches from David Woodhouse and Hongyan Xia.

---

Interestingly, I was expecting this patch to perform worse. However, from testing there is a small increase in perf.

That said, I mainly plit the patch because it keeps refactoring and optimization separated.

Changes in v2:
- Rename init_contig_pages() to _init_heap_pages()
- Fold is_contig_page()

---
 xen/common/page_alloc.c | 77 ++++++++++++++++++++++++++---------------
 1 file changed, 50 insertions(+), 27 deletions(-)
xen/common/page_alloc.c +50 -27
index 078c2990041d..eedb2fed77c3 100644
@@ -1778,16 +1778,44 @@ int query_page_offline(mfn_t mfn, uint32_t *status)
 }
 
 /*
- * Hand the specified arbitrary page range to the specified heap zone
- * checking the node_id of the previous page.  If they differ and the
- * latter is not on a MAX_ORDER boundary, then we reserve the page by
- * not freeing it to the buddy allocator.
+ * This function should only be called with valid pages from the same NUMA
+ * node.
  */
+static void _init_heap_pages(const struct page_info *pg,
+                             unsigned long nr_pages,
+                             bool need_scrub)
+{
+    unsigned long s, e;
+    unsigned int nid = phys_to_nid(page_to_maddr(pg));
+
+    s = mfn_x(page_to_mfn(pg));
+    e = mfn_x(mfn_add(page_to_mfn(pg + nr_pages - 1), 1));
+    if ( unlikely(!avail[nid]) )
+    {
+        bool use_tail = IS_ALIGNED(s, 1UL << MAX_ORDER) &&
+                        (find_first_set_bit(e) <= find_first_set_bit(s));
+        unsigned long n;
+
+        n = init_node_heap(nid, s, nr_pages, &use_tail);
+        BUG_ON(n > nr_pages);
+        if ( use_tail )
+            e -= n;
+        else
+            s += n;
+    }
+
+    while ( s < e )
+    {
+        free_heap_pages(mfn_to_page(_mfn(s)), 0, need_scrub);
+        s += 1UL;
+    }
+}
+
 static void init_heap_pages(
     struct page_info *pg, unsigned long nr_pages)
 {
     unsigned long i;
-    bool idle_scrub = false;
+    bool need_scrub = scrub_debug;
 
     /*
      * Keep MFN 0 away from the buddy allocator to avoid crossing zone
@@ -1812,35 +1840,30 @@ static void init_heap_pages(
     spin_unlock(&heap_lock);
 
     if ( system_state < SYS_STATE_active && opt_bootscrub == BOOTSCRUB_IDLE )
-        idle_scrub = true;
+        need_scrub = true;
 
-    for ( i = 0; i < nr_pages; i++ )
+    for ( i = 0; i < nr_pages; )
     {
-        unsigned int nid = phys_to_nid(page_to_maddr(pg+i));
+        unsigned int nid = phys_to_nid(page_to_maddr(pg));
+        unsigned long left = nr_pages - i;
+        unsigned long contig_pages;
 
-        if ( unlikely(!avail[nid]) )
+        /*
+         * _init_heap_pages() is only able to accept range following
+         * specific property (see comment on top of _init_heap_pages()).
+         *
+         * So break down the range in smaller set.
+         */
+        for ( contig_pages = 1; contig_pages < left; contig_pages++ )
         {
-            unsigned long s = mfn_x(page_to_mfn(pg + i));
-            unsigned long e = mfn_x(mfn_add(page_to_mfn(pg + nr_pages - 1), 1));
-            bool use_tail = (nid == phys_to_nid(pfn_to_paddr(e - 1))) &&
-                            IS_ALIGNED(s, 1UL << MAX_ORDER) &&
-                            (find_first_set_bit(e) <= find_first_set_bit(s));
-            unsigned long n;
-
-            n = init_node_heap(nid, mfn_x(page_to_mfn(pg + i)), nr_pages - i,
-                               &use_tail);
-            BUG_ON(i + n > nr_pages);
-            if ( n && !use_tail )
-            {
-                i += n - 1;
-                continue;
-            }
-            if ( i + n == nr_pages )
+            if ( nid != (phys_to_nid(page_to_maddr(pg))) )
                 break;
-            nr_pages -= n;
         }
 
-        free_heap_pages(pg + i, 0, scrub_debug || idle_scrub);
+        _init_heap_pages(pg, contig_pages, need_scrub);
+
+        pg += contig_pages;
+        i += contig_pages;
     }
 }
 
Signature · 1 line
2.32.0
#5 lore raw

Re: [PATCH v2 2/3] xen/heap: Split init_heap_pages() in two

Hi Julien,

Quoted 80 lines · Julien Grall

From: Julien Grall <[email protected]>

At the moment, init_heap_pages() will call free_heap_pages() page by page. To reduce the time to initialize the heap, we will want to provide multiple pages at the same time.

init_heap_pages() is now split in two parts:
- init_heap_pages(): will break down the range in multiple set of contiguous pages. For now, the criteria is the pages should belong to the same NUMA node.
- _init_heap_pages(): will initialize a set of pages belonging to the same NUMA node. In a follow-up patch, new requirements will be added (e.g. pages should belong to the same zone). For now the pages are still passed one by one to free_heap_pages().

Note that the comment before init_heap_pages() is heavily outdated and does not reflect the current code. So update it.

This patch is a merge/rework of patches from David Woodhouse and Hongyan Xia.

---

Interestingly, I was expecting this patch to perform worse. However, from testing there is a small increase in perf.

That said, I mainly plit the patch because it keeps refactoring and optimization separated.

Changes in v2:
- Rename init_contig_pages() to _init_heap_pages()
- Fold is_contig_page()

---
  xen/common/page_alloc.c | 77 ++++++++++++++++++++++++++---------------
  1 file changed, 50 insertions(+), 27 deletions(-)
xen/common/page_alloc.c +29 -4
index 078c2990041d..eedb2fed77c3 100644
@@ -1778,16 +1778,44 @@ int query_page_offline(mfn_t mfn, uint32_t *status)
  }
 
  /*
- * Hand the specified arbitrary page range to the specified heap zone
- * checking the node_id of the previous page.  If they differ and the
- * latter is not on a MAX_ORDER boundary, then we reserve the page by
- * not freeing it to the buddy allocator.
+ * This function should only be called with valid pages from the same NUMA
+ * node.
   */
+static void _init_heap_pages(const struct page_info *pg,
+                             unsigned long nr_pages,
+                             bool need_scrub)
+{
+    unsigned long s, e;
+    unsigned int nid = phys_to_nid(page_to_maddr(pg));
+
+    s = mfn_x(page_to_mfn(pg));
+    e = mfn_x(mfn_add(page_to_mfn(pg + nr_pages - 1), 1));
+    if ( unlikely(!avail[nid]) )
+    {
+        bool use_tail = IS_ALIGNED(s, 1UL << MAX_ORDER) &&
+                        (find_first_set_bit(e) <= find_first_set_bit(s));
+        unsigned long n;
+
+        n = init_node_heap(nid, s, nr_pages, &use_tail);
+        BUG_ON(n > nr_pages);
+        if ( use_tail )
+            e -= n;
+        else
+            s += n;
+    }
+
+    while ( s < e )
+    {
+        free_heap_pages(mfn_to_page(_mfn(s)), 0, need_scrub);
+        s += 1UL;

xen/common/page_alloc.c int query_page_offline(mfn_t mfn, uint32_t *status) · line 1819

Quoted 10 lines
xen/common/page_alloc.c +4 -1
+    }
+}
+
  static void init_heap_pages(
      struct page_info *pg, unsigned long nr_pages)
  {
      unsigned long i;
-    bool idle_scrub = false;
+    bool need_scrub = scrub_debug;
 

You have changed idle_scrub to need_scrub, but haven't mentioned this in commit log, and I also haven't found related discussion in v1. I am very clear about this change.

Cheers,
Wei Chen

Quoted 55 lines
xen/common/page_alloc.c +17 -22
      /*
       * Keep MFN 0 away from the buddy allocator to avoid crossing zone
@@ -1812,35 +1840,30 @@ static void init_heap_pages(
      spin_unlock(&heap_lock);
 
      if ( system_state < SYS_STATE_active && opt_bootscrub == BOOTSCRUB_IDLE )
-        idle_scrub = true;
+        need_scrub = true;
 
-    for ( i = 0; i < nr_pages; i++ )
+    for ( i = 0; i < nr_pages; )
      {
-        unsigned int nid = phys_to_nid(page_to_maddr(pg+i));
+        unsigned int nid = phys_to_nid(page_to_maddr(pg));
+        unsigned long left = nr_pages - i;
+        unsigned long contig_pages;
 
-        if ( unlikely(!avail[nid]) )
+        /*
+         * _init_heap_pages() is only able to accept range following
+         * specific property (see comment on top of _init_heap_pages()).
+         *
+         * So break down the range in smaller set.
+         */
+        for ( contig_pages = 1; contig_pages < left; contig_pages++ )
          {
-            unsigned long s = mfn_x(page_to_mfn(pg + i));
-            unsigned long e = mfn_x(mfn_add(page_to_mfn(pg + nr_pages - 1), 1));
-            bool use_tail = (nid == phys_to_nid(pfn_to_paddr(e - 1))) &&
-                            IS_ALIGNED(s, 1UL << MAX_ORDER) &&
-                            (find_first_set_bit(e) <= find_first_set_bit(s));
-            unsigned long n;
-
-            n = init_node_heap(nid, mfn_x(page_to_mfn(pg + i)), nr_pages - i,
-                               &use_tail);
-            BUG_ON(i + n > nr_pages);
-            if ( n && !use_tail )
-            {
-                i += n - 1;
-                continue;
-            }
-            if ( i + n == nr_pages )
+            if ( nid != (phys_to_nid(page_to_maddr(pg))) )
                  break;
-            nr_pages -= n;
          }
 
-        free_heap_pages(pg + i, 0, scrub_debug || idle_scrub);
+        _init_heap_pages(pg, contig_pages, need_scrub);
+
+        pg += contig_pages;
+        i += contig_pages;
      }
  }
 
#6 lore raw

Re: [PATCH v2 2/3] xen/heap: Split init_heap_pages() in two

Quoted 11 lines · Wei Chen
Quoted 7 lines

  static void init_heap_pages(
      struct page_info *pg, unsigned long nr_pages) {
      unsigned long i;
-    bool idle_scrub = false;
+    bool need_scrub = scrub_debug;

You have changed idle_scrub to need_scrub, but haven't mentioned this in commit log, and I also haven't found related discussion in v1. I am very clear about this change.

The meaning/use of the variable is now different. Before this patch, the variable was only indicating whether idle scrub was enabled (this is configurable by the admin). This was then or-ed with 'scrub_debug' when calling free_heap_pages().

With this patch, we now store the result of the or-ed in the local variable.

This is not something I felt was necessary to mention in the commit message.

Cheers,

Signature · 1 line
Julien Grall
Reviewed-by on v2 2/3
#7 lore raw

Re: [PATCH v2 2/3] xen/heap: Split init_heap_pages() in two

In reply to Julien Grall
Quoted 15 lines · Julien Grall

From: Julien Grall <[email protected]>

At the moment, init_heap_pages() will call free_heap_pages() page by page. To reduce the time to initialize the heap, we will want to provide multiple pages at the same time.

init_heap_pages() is now split in two parts:
- init_heap_pages(): will break down the range in multiple set of contiguous pages. For now, the criteria is the pages should belong to the same NUMA node.
- _init_heap_pages(): will initialize a set of pages belonging to the same NUMA node. In a follow-up patch, new requirements will be added (e.g. pages should belong to the same zone). For now the pages are still passed one by one to free_heap_pages().

Quoted 7 lines

Note that the comment before init_heap_pages() is heavily outdated and does not reflect the current code. So update it.

This patch is a merge/rework of patches from David Woodhouse and Hongyan Xia.

Albeit maybe with ...

Quoted 30 lines
xen/common/page_alloc.c +19 -4
@@ -1778,16 +1778,44 @@ int query_page_offline(mfn_t mfn, uint32_t *status)
 }
 
 /*
- * Hand the specified arbitrary page range to the specified heap zone
- * checking the node_id of the previous page.  If they differ and the
- * latter is not on a MAX_ORDER boundary, then we reserve the page by
- * not freeing it to the buddy allocator.
+ * This function should only be called with valid pages from the same NUMA
+ * node.
  */
+static void _init_heap_pages(const struct page_info *pg,
+                             unsigned long nr_pages,
+                             bool need_scrub)
+{
+    unsigned long s, e;
+    unsigned int nid = phys_to_nid(page_to_maddr(pg));
+
+    s = mfn_x(page_to_mfn(pg));
+    e = mfn_x(mfn_add(page_to_mfn(pg + nr_pages - 1), 1));
+    if ( unlikely(!avail[nid]) )
+    {
+        bool use_tail = IS_ALIGNED(s, 1UL << MAX_ORDER) &&
+                        (find_first_set_bit(e) <= find_first_set_bit(s));
+        unsigned long n;
+
+        n = init_node_heap(nid, s, nr_pages, &use_tail);
+        BUG_ON(n > nr_pages);

xen/common/page_alloc.c int query_page_offline(mfn_t mfn, uint32_t *status) · line 1810

Quoted 10 lines
xen/common/page_alloc.c +10 -0
+        if ( use_tail )
+            e -= n;
+        else
+            s += n;
+    }
+
+    while ( s < e )
+    {
+        free_heap_pages(mfn_to_page(_mfn(s)), 0, need_scrub);
+        s += 1UL;

... the more conventional s++ or ++s used here?

Jan

#8 lore raw

Re: [PATCH v2 2/3] xen/heap: Split init_heap_pages() in two

Hi Jan,

Quoted 19 lines · Jan Beulich
Quoted 18 lines · Julien Grall

From: Julien Grall <[email protected]>

At the moment, init_heap_pages() will call free_heap_pages() page by page. To reduce the time to initialize the heap, we will want to provide multiple pages at the same time.

init_heap_pages() is now split in two parts:
- init_heap_pages(): will break down the range in multiple set of contiguous pages. For now, the criteria is the pages should belong to the same NUMA node.
- _init_heap_pages(): will initialize a set of pages belonging to the same NUMA node. In a follow-up patch, new requirements will be added (e.g. pages should belong to the same zone). For now the pages are still passed one by one to free_heap_pages().

Note that the comment before init_heap_pages() is heavily outdated and does not reflect the current code. So update it.

Quoted 6 lines
Quoted 4 lines

This patch is a merge/rework of patches from David Woodhouse and Hongyan Xia.

Thanks.

Quoted 34 lines
Quoted 34 lines
xen/common/page_alloc.c +23 -4
@@ -1778,16 +1778,44 @@ int query_page_offline(mfn_t mfn, uint32_t *status)
  }
 
  /*
- * Hand the specified arbitrary page range to the specified heap zone
- * checking the node_id of the previous page.  If they differ and the
- * latter is not on a MAX_ORDER boundary, then we reserve the page by
- * not freeing it to the buddy allocator.
+ * This function should only be called with valid pages from the same NUMA
+ * node.
   */
+static void _init_heap_pages(const struct page_info *pg,
+                             unsigned long nr_pages,
+                             bool need_scrub)
+{
+    unsigned long s, e;
+    unsigned int nid = phys_to_nid(page_to_maddr(pg));
+
+    s = mfn_x(page_to_mfn(pg));
+    e = mfn_x(mfn_add(page_to_mfn(pg + nr_pages - 1), 1));
+    if ( unlikely(!avail[nid]) )
+    {
+        bool use_tail = IS_ALIGNED(s, 1UL << MAX_ORDER) &&
+                        (find_first_set_bit(e) <= find_first_set_bit(s));
+        unsigned long n;
+
+        n = init_node_heap(nid, s, nr_pages, &use_tail);
+        BUG_ON(n > nr_pages);
+        if ( use_tail )
+            e -= n;
+        else
+            s += n;
Quoted 8 lines

xen/common/page_alloc.c int query_page_offline(mfn_t mfn, uint32_t *status) · line 1810

Quoted 6 lines
xen/common/page_alloc.c +6 -0
+    }
+
+    while ( s < e )
+    {
+        free_heap_pages(mfn_to_page(_mfn(s)), 0, need_scrub);
+        s += 1UL;

... the more conventional s++ or ++s used here?

I would prefer to keep using "s += 1UL" here because:
* it will be replace with a proper order in the follow-up patch. So
this is temporary.
* one could argue that if I use "s++" then I should also switch to a
for loop which would make sense here but not in the next patch.

Cheers,

Signature · 1 line
Julien Grall
#9 lore raw

Re: [PATCH v2 2/3] xen/heap: Split init_heap_pages() in two

Quoted 45 lines · Julien Grall
Quoted 35 lines · Jan Beulich
Quoted 34 lines · Julien Grall
xen/common/page_alloc.c +23 -4
@@ -1778,16 +1778,44 @@ int query_page_offline(mfn_t mfn, uint32_t *status)
  }
 
  /*
- * Hand the specified arbitrary page range to the specified heap zone
- * checking the node_id of the previous page.  If they differ and the
- * latter is not on a MAX_ORDER boundary, then we reserve the page by
- * not freeing it to the buddy allocator.
+ * This function should only be called with valid pages from the same NUMA
+ * node.
   */
+static void _init_heap_pages(const struct page_info *pg,
+                             unsigned long nr_pages,
+                             bool need_scrub)
+{
+    unsigned long s, e;
+    unsigned int nid = phys_to_nid(page_to_maddr(pg));
+
+    s = mfn_x(page_to_mfn(pg));
+    e = mfn_x(mfn_add(page_to_mfn(pg + nr_pages - 1), 1));
+    if ( unlikely(!avail[nid]) )
+    {
+        bool use_tail = IS_ALIGNED(s, 1UL << MAX_ORDER) &&
+                        (find_first_set_bit(e) <= find_first_set_bit(s));
+        unsigned long n;
+
+        n = init_node_heap(nid, s, nr_pages, &use_tail);
+        BUG_ON(n > nr_pages);
+        if ( use_tail )
+            e -= n;
+        else
+            s += n;
Quoted 8 lines

xen/common/page_alloc.c int query_page_offline(mfn_t mfn, uint32_t *status) · line 1810

Quoted 6 lines
xen/common/page_alloc.c +6 -0
+    }
+
+    while ( s < e )
+    {
+        free_heap_pages(mfn_to_page(_mfn(s)), 0, need_scrub);
+        s += 1UL;

... the more conventional s++ or ++s used here?

Quoted 3 lines

I would prefer to keep using "s += 1UL" here because:
* it will be replace with a proper order in the follow-up patch. So
this is temporary.

Fair enough.

Jan

Quoted 5 lines

* one could argue that if I use "s++" then I should also switch to a
for loop which would make sense here but not in the next patch.

Cheers,

#10 lore raw

PATCH v2 3/3 xen/heap: pass order to free_heap_pages() in heap init

In reply to Julien Grall

From: Hongyan Xia <[email protected]>

The idea is to split the range into multiple aligned power-of-2 regions which only needs to call free_heap_pages() once each. We check the least significant set bit of the start address and use its bit index as the order of this increment. This makes sure that each increment is both power-of-2 and properly aligned, which can be safely passed to free_heap_pages(). Of course, the order also needs to be sanity checked against the upper bound and MAX_ORDER.

Tested on a nested environment on c5.metal with various amount of RAM and CONFIG_DEBUG=n. Time for end_boot_allocator() to complete:
Before After
- 90GB: 1445 ms 96 ms
- 8GB: 126 ms 8 ms
- 4GB: 62 ms 4 ms

---

Changes in v2:
- Update comment
- Update the numbers. They are slightly better as is_contig_page() has been folded in init_heap_pages().

---
 xen/common/page_alloc.c | 35 ++++++++++++++++++++++++++++++++---
 1 file changed, 32 insertions(+), 3 deletions(-)
xen/common/page_alloc.c +32 -3
index eedb2fed77c3..2b99801d2ea3 100644
@@ -1779,7 +1779,7 @@ int query_page_offline(mfn_t mfn, uint32_t *status)
 
 /*
  * This function should only be called with valid pages from the same NUMA
- * node.
+ * node and zone.
  */
 static void _init_heap_pages(const struct page_info *pg,
                              unsigned long nr_pages,
@@ -1806,8 +1806,22 @@ static void _init_heap_pages(const struct page_info *pg,
 
     while ( s < e )
     {
-        free_heap_pages(mfn_to_page(_mfn(s)), 0, need_scrub);
-        s += 1UL;
+        /*
+         * For s == 0, we simply use the largest increment by checking the
+         * MSB of the region size. For s != 0, we also need to ensure that the
+         * chunk is properly sized to end at power-of-two alignment. We do this
+         * by checking the LSB of the start address and use its index as
+         * the increment. Both cases need to be guarded by MAX_ORDER.
+         *
+         * Note that the value of ffsl() and flsl() starts from 1 so we need
+         * to decrement it by 1.
+         */
+        int inc_order = min(MAX_ORDER, flsl(e - s) - 1);
+
+        if ( s )
+            inc_order = min(inc_order, ffsl(s) - 1);
+        free_heap_pages(mfn_to_page(_mfn(s)), inc_order, need_scrub);
+        s += (1UL << inc_order);
     }
 }
 
@@ -1844,6 +1858,9 @@ static void init_heap_pages(
 
     for ( i = 0; i < nr_pages; )
     {
+#ifdef CONFIG_SEPARATE_XENHEAP
+        unsigned int zone = page_to_zone(pg);
+#endif
         unsigned int nid = phys_to_nid(page_to_maddr(pg));
         unsigned long left = nr_pages - i;
         unsigned long contig_pages;
@@ -1856,6 +1873,18 @@ static void init_heap_pages(
          */
         for ( contig_pages = 1; contig_pages < left; contig_pages++ )
         {
+            /*
+             * No need to check for the zone when !CONFIG_SEPARATE_XENHEAP
+             * because free_heap_pages() can only take power-of-two ranges
+             * which never cross zone boundaries. But for separate xenheap
+             * which is manually defined, it is possible for power-of-two
+             * range to cross zones.
+             */
+#ifdef CONFIG_SEPARATE_XENHEAP
+            if ( zone != page_to_zone(pg) )
+                break;
+#endif
+
             if ( nid != (phys_to_nid(page_to_maddr(pg))) )
                 break;
         }
Signature · 1 line
2.32.0
Reviewed-by on v2 3/3
#11 lore raw

Re: [PATCH v2 3/3] xen/heap: pass order to free_heap_pages() in heap init

Hi Julien,

Quoted 87 lines · Julien Grall

From: Hongyan Xia <[email protected]>

The idea is to split the range into multiple aligned power-of-2 regions which only needs to call free_heap_pages() once each. We check the least significant set bit of the start address and use its bit index as the order of this increment. This makes sure that each increment is both power-of-2 and properly aligned, which can be safely passed to free_heap_pages(). Of course, the order also needs to be sanity checked against the upper bound and MAX_ORDER.

Tested on a nested environment on c5.metal with various amount of RAM and CONFIG_DEBUG=n. Time for end_boot_allocator() to complete:
Before After
- 90GB: 1445 ms 96 ms
- 8GB: 126 ms 8 ms
- 4GB: 62 ms 4 ms

---

Changes in v2:
- Update comment
- Update the numbers. They are slightly better as is_contig_page() has been folded in init_heap_pages().

---
  xen/common/page_alloc.c | 35 ++++++++++++++++++++++++++++++++---
  1 file changed, 32 insertions(+), 3 deletions(-)
xen/common/page_alloc.c +25 -3
index eedb2fed77c3..2b99801d2ea3 100644
@@ -1779,7 +1779,7 @@ int query_page_offline(mfn_t mfn, uint32_t *status)
 
  /*
   * This function should only be called with valid pages from the same NUMA
- * node.
+ * node and zone.
   */
  static void _init_heap_pages(const struct page_info *pg,
                               unsigned long nr_pages,
@@ -1806,8 +1806,22 @@ static void _init_heap_pages(const struct page_info *pg,
 
      while ( s < e )
      {
-        free_heap_pages(mfn_to_page(_mfn(s)), 0, need_scrub);
-        s += 1UL;
+        /*
+         * For s == 0, we simply use the largest increment by checking the
+         * MSB of the region size. For s != 0, we also need to ensure that the
+         * chunk is properly sized to end at power-of-two alignment. We do this
+         * by checking the LSB of the start address and use its index as
+         * the increment. Both cases need to be guarded by MAX_ORDER.
+         *
+         * Note that the value of ffsl() and flsl() starts from 1 so we need
+         * to decrement it by 1.
+         */
+        int inc_order = min(MAX_ORDER, flsl(e - s) - 1);
+
+        if ( s )
+            inc_order = min(inc_order, ffsl(s) - 1);
+        free_heap_pages(mfn_to_page(_mfn(s)), inc_order, need_scrub);
+        s += (1UL << inc_order);
      }
  }
 
@@ -1844,6 +1858,9 @@ static void init_heap_pages(
 
      for ( i = 0; i < nr_pages; )
      {
+#ifdef CONFIG_SEPARATE_XENHEAP
+        unsigned int zone = page_to_zone(pg);
+#endif
          unsigned int nid = phys_to_nid(page_to_maddr(pg));
          unsigned long left = nr_pages - i;
          unsigned long contig_pages;
@@ -1856,6 +1873,18 @@ static void init_heap_pages(
           */
          for ( contig_pages = 1; contig_pages < left; contig_pages++ )
          {
+            /*
+             * No need to check for the zone when !CONFIG_SEPARATE_XENHEAP
+             * because free_heap_pages() can only take power-of-two ranges
+             * which never cross zone boundaries. But for separate xenheap
+             * which is manually defined, it is possible for power-of-two

xen/common/page_alloc.c static void init_heap_pages( · line 1890

Quoted 10 lines
xen/common/page_alloc.c +7 -0
+             * range to cross zones.
+             */
+#ifdef CONFIG_SEPARATE_XENHEAP
+            if ( zone != page_to_zone(pg) )
+                break;
+#endif
+
              if ( nid != (phys_to_nid(page_to_maddr(pg))) )
                  break;
          }
Reviewed-by on v2 3/3
#12 lore raw

Re: [PATCH v2 3/3] xen/heap: pass order to free_heap_pages() in heap init

In reply to Julien Grall

xen/common/page_alloc.c static void _init_heap_pages(const struct page_info *pg, · line 1814

Quoted 12 lines · Julien Grall
xen/common/page_alloc.c +6 -2
@@ -1806,8 +1806,22 @@ static void _init_heap_pages(const struct page_info *pg,
 
     while ( s < e )
     {
-        free_heap_pages(mfn_to_page(_mfn(s)), 0, need_scrub);
-        s += 1UL;
+        /*
+         * For s == 0, we simply use the largest increment by checking the
+         * MSB of the region size. For s != 0, we also need to ensure that the
+         * chunk is properly sized to end at power-of-two alignment. We do this
+         * by checking the LSB of the start address and use its index as
+         * the increment. Both cases need to be guarded by MAX_ORDER.

s/guarded/bounded/ ?

xen/common/page_alloc.c static void _init_heap_pages(const struct page_info *pg, · line 1819

Quoted 4 lines
xen/common/page_alloc.c +4 -0
+         * Note that the value of ffsl() and flsl() starts from 1 so we need
+         * to decrement it by 1.
+         */
+        int inc_order = min(MAX_ORDER, flsl(e - s) - 1);

unsigned int, since ...

xen/common/page_alloc.c static void _init_heap_pages(const struct page_info *pg, · line 1823

Quoted 3 lines
xen/common/page_alloc.c +3 -0
+        if ( s )
+            inc_order = min(inc_order, ffsl(s) - 1);
+        free_heap_pages(mfn_to_page(_mfn(s)), inc_order, need_scrub);

... that's what the function parameter says, and the value also can go negative? Preferably with these adjustments

Jan

#13 lore raw

Re: [PATCH v2 3/3] xen/heap: pass order to free_heap_pages() in heap init

Hi Jan,

Quoted 16 lines · Jan Beulich

xen/common/page_alloc.c static void _init_heap_pages(const struct page_info *pg, · line 1814

Quoted 12 lines · Julien Grall
xen/common/page_alloc.c +6 -2
@@ -1806,8 +1806,22 @@ static void _init_heap_pages(const struct page_info *pg,
 
      while ( s < e )
      {
-        free_heap_pages(mfn_to_page(_mfn(s)), 0, need_scrub);
-        s += 1UL;
+        /*
+         * For s == 0, we simply use the largest increment by checking the
+         * MSB of the region size. For s != 0, we also need to ensure that the
+         * chunk is properly sized to end at power-of-two alignment. We do this
+         * by checking the LSB of the start address and use its index as
+         * the increment. Both cases need to be guarded by MAX_ORDER.

s/guarded/bounded/ ?

Quoted 6 lines

xen/common/page_alloc.c static void _init_heap_pages(const struct page_info *pg, · line 1819

Quoted 4 lines
xen/common/page_alloc.c +4 -0
+         * Note that the value of ffsl() and flsl() starts from 1 so we need
+         * to decrement it by 1.
+         */
+        int inc_order = min(MAX_ORDER, flsl(e - s) - 1);

unsigned int, since ...

MAX_ORDER, flsl(), ffsl() are both returning signed value. So inc_order should be 'int' to avoid any compilation issue.

Quoted 7 lines

xen/common/page_alloc.c static void _init_heap_pages(const struct page_info *pg, · line 1823

Quoted 4 lines
xen/common/page_alloc.c +3 -0
+        if ( s )
+            inc_order = min(inc_order, ffsl(s) - 1);
+        free_heap_pages(mfn_to_page(_mfn(s)), inc_order, need_scrub);

... that's what the function parameter says, and the value also can go negative?

AFAICT, none of the values are negative. But per above, if we use min() then the local variable should be 'int'. The two possible alternatives are:
1) Use min_t()
2) Update MAX_ORDER, flsl(), ffsl() to return an unsigned value

The ideal would be #2 but it will require at least an extra patch and effort. If we use #1, then they use may become stale if we implement #2.

So I would prefer to keep min(). That said, I would be open to use min_t() if you strongly prefer it.

Quoted 2 lines

Preferably with these adjustments

Thanks!

Cheers,

Signature · 1 line
Julien Grall
#14 lore raw

Re: [PATCH v2 3/3] xen/heap: pass order to free_heap_pages() in heap init

Quoted 37 lines · Julien Grall

Hi Jan,

Quoted 16 lines · Jan Beulich

xen/common/page_alloc.c static void _init_heap_pages(const struct page_info *pg, · line 1814

Quoted 12 lines · Julien Grall
xen/common/page_alloc.c +6 -2
@@ -1806,8 +1806,22 @@ static void _init_heap_pages(const struct page_info *pg,
 
      while ( s < e )
      {
-        free_heap_pages(mfn_to_page(_mfn(s)), 0, need_scrub);
-        s += 1UL;
+        /*
+         * For s == 0, we simply use the largest increment by checking the
+         * MSB of the region size. For s != 0, we also need to ensure that the
+         * chunk is properly sized to end at power-of-two alignment. We do this
+         * by checking the LSB of the start address and use its index as
+         * the increment. Both cases need to be guarded by MAX_ORDER.

s/guarded/bounded/ ?

Quoted 6 lines

xen/common/page_alloc.c static void _init_heap_pages(const struct page_info *pg, · line 1819

Quoted 4 lines
xen/common/page_alloc.c +4 -0
+         * Note that the value of ffsl() and flsl() starts from 1 so we need
+         * to decrement it by 1.
+         */
+        int inc_order = min(MAX_ORDER, flsl(e - s) - 1);

unsigned int, since ...

MAX_ORDER, flsl(), ffsl() are both returning signed value. So inc_order should be 'int' to avoid any compilation issue.

Quoted 7 lines

xen/common/page_alloc.c static void _init_heap_pages(const struct page_info *pg, · line 1823

Quoted 4 lines
xen/common/page_alloc.c +3 -0
+        if ( s )
+            inc_order = min(inc_order, ffsl(s) - 1);
+        free_heap_pages(mfn_to_page(_mfn(s)), inc_order, need_scrub);

... that's what the function parameter says, and the value also can go negative?

Quoted 7 lines

AFAICT, none of the values are negative. But per above, if we use min() then the local variable should be 'int'. The two possible alternatives are:
1) Use min_t()
2) Update MAX_ORDER, flsl(), ffsl() to return an unsigned value

The ideal would be #2 but it will require at least an extra patch and effort. If we use #1, then they use may become stale if we implement #2.

I'm not sure #2 is a good option - we'd deviate from conventions used elsewhere on what flsl() and ffsl() return. And constants would imo best remain suffix-less unless a suffix is very relevant to have (for MAX_ORDER, which isn't at risk of being subject to ~ or -, it may be warranted).

3)

        unsigned int inc_order = min(MAX_ORDER, flsl(e - s) - 1);

        if ( s )
            inc_order = min(inc_order, ffsl(s) - 1U);

No compilation issues to expect here, afaict.

Quoted 2 lines

So I would prefer to keep min(). That said, I would be open to use min_t() if you strongly prefer it.

I consider max_t() / min_t() dangerous, so I'd like to limit their use to cases where no good alternatives exist.

Jan

#15 lore raw

Re: [PATCH v2 3/3] xen/heap: pass order to free_heap_pages() in heap init

Hi Jan,

Quoted 6 lines · Jan Beulich

On 18.07.2022 12:24, Julien Grall wrote:
3)

         unsigned int inc_order = min(MAX_ORDER, flsl(e - s) - 1);

         if ( s )
             inc_order = min(inc_order, ffsl(s) - 1U);

I like this idea!

Quoted 2 lines

No compilation issues to expect here, afaict.

Correct, GCC is happy with this approach. Assuming there are no other comments, are you happy if I make the change on commit?

Cheers,

Signature · 1 line
Julien Grall
#16 lore raw

Re: [PATCH v2 3/3] xen/heap: pass order to free_heap_pages() in heap init

Quoted 8 lines · Julien Grall
Quoted 6 lines · Jan Beulich

On 18.07.2022 12:24, Julien Grall wrote:
3)

         unsigned int inc_order = min(MAX_ORDER, flsl(e - s) - 1);

         if ( s )
             inc_order = min(inc_order, ffsl(s) - 1U);
Quoted 7 lines

I like this idea!

Quoted 2 lines

No compilation issues to expect here, afaict.

Correct, GCC is happy with this approach. Assuming there are no other comments, are you happy if I make the change on commit?

Sure - iirc I gave my R-b already.

Jan

#17 lore raw

Re: [PATCH v2 3/3] xen/heap: pass order to free_heap_pages() in heap init

Hi Jan,

Quoted 12 lines · Jan Beulich
Quoted 11 lines · Julien Grall
Quoted 6 lines · Jan Beulich

On 18.07.2022 12:24, Julien Grall wrote:
3)

          unsigned int inc_order = min(MAX_ORDER, flsl(e - s) - 1);

          if ( s )
              inc_order = min(inc_order, ffsl(s) - 1U);

I like this idea!

Quoted 1 line
Quoted 6 lines
Quoted 4 lines
Quoted 1 line

No compilation issues to expect here, afaict.

Correct, GCC is happy with this approach. Assuming there are no other comments, are you happy if I make the change on commit?

Sure - iirc I gave my R-b already.

You did. Just wanted to make sure your reviewed-by was valid with your proposal.

I have now committed the series.

Cheers,

Signature · 1 line
Julien Grall