Skip to contentSkip to conversation

Memory scrubbing from idle loop

PATCH Superseded Boris Ostrovsky 3 participants 35 messages Jun 22, 2017 – Aug 2, 2017

v5 of 8 Superseded Latest is v8 →

lore
Share & tools
lore
#0 lore raw

v5 cover Memory scrubbing from idle loop

Changes in v5:
* Make page_info.u.free and union and use bitfields there.
* Bug fixes

(see per-patch notes)

When a domain is destroyed the hypervisor must scrub domain's pages before giving them to another guest in order to prevent leaking the deceased guest's data. Currently this is done during guest's destruction, possibly causing very lengthy cleanup process.

This series adds support for scrubbing released pages from idle loop, making guest destruction significantly faster. For example, destroying a 1TB guest can now be completed in 40+ seconds as opposed to about 9 minutes using existing scrubbing algorithm.

Briefly, the new algorithm places dirty pages at the end of heap's page list for each node/zone/order to avoid having to scan full list while searching for dirty pages. One processor form each node checks whether the node has any dirty pages and, if such pages are found, scrubs them. Scrubbing itself happens without holding heap lock so other users may access heap in the meantime. If while idle loop is scrubbing a particular chunk of pages this chunk is requested by the heap allocator, scrubbing is immediately stopped.

On the allocation side, alloc_heap_pages() first tries to satisfy allocation request using only clean pages. If this is not possible, the search is repeated and dirty pages are scrubbed by the allocator.

This series is somewhat based on earlier work by Bob Liu.

V1:
* Only set PGC_need_scrub bit for the buddy head, thus making it unnecessary to scan whole buddy
* Fix spin_lock_cb()
* Scrub CPU-less nodes
* ARM support. Note that I have not been able to test this, only built the binary
* Added scrub test patch (last one). Not sure whether it should be considered for committing but I have been running with it.

V2:
* merge_chunks() returns new buddy head
* scrub_free_pages() returns softirq pending status in addition to (factored out) status of unscrubbed memory
* spin_lock uses inlined spin_lock_cb()
* scrub debugging code checks whole page, not just the first word.

V3:
* Keep dirty bit per page
* Simplify merge_chunks() (now merge_and_free_buddy())
* When scrubbing memmory-only nodes try to find the closest node.

V4:
* Keep track of dirty pages in a buddy with page_info.u.free.first_dirty.
* Drop patch 1 (factoring out merge_and_free_buddy()) since there is only one caller now
* Drop patch patch 5 (from V3) since we are not breaking partially-scrubbed buddy anymore
* Extract search loop in alloc_heap_pages() into get_free_buddy() (patch 2)
* Add MEMF_no_scrub flag

Deferred:
* Per-node heap locks. In addition to (presumably) improving performance in general, once they are available we can parallelize scrubbing further by allowing more than one core per node to do idle loop scrubbing.
* AVX-based scrubbing
* Use idle loop scrubbing during boot.

Boris Ostrovsky (8):
mm: Place unscrubbed pages at the end of pagelist
mm: Extract allocation loop from alloc_heap_pages()
mm: Scrub pages in alloc_heap_pages() if needed
mm: Scrub memory from idle loop
spinlock: Introduce spin_lock_cb()
mm: Keep heap accessible to others while scrubbing
mm: Print number of unscrubbed pages in 'H' debug handler
mm: Make sure pages are scrubbed

 xen/Kconfig.debug          |   7 +
 xen/arch/arm/domain.c      |   2 +-
 xen/arch/x86/domain.c      |   2 +-
 xen/arch/x86/domain_page.c |   6 +-
 xen/common/page_alloc.c    | 612 ++++++++++++++++++++++++++++++++++++++-------
 xen/common/spinlock.c      |   9 +-
 xen/include/asm-arm/mm.h   |  30 ++-
 xen/include/asm-x86/mm.h   |  30 ++-
 xen/include/xen/mm.h       |   5 +-
 xen/include/xen/spinlock.h |   8 +
 10 files changed, 603 insertions(+), 108 deletions(-)
Signature · 7 lines
1.8.3.1


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

PATCH v5 1/8 mm: Place unscrubbed pages at the end of pagelist

. so that it's easy to find pages that need to be scrubbed (those pages are now marked with _PGC_need_scrub bit).

We keep track of the first unscrubbed page in a page buddy using first_dirty field. For now it can have two values, 0 (whole buddy needs scrubbing) or INVALID_DIRTY_IDX (the buddy does not need to be scrubbed). Subsequent patches will allow scrubbing to be interrupted, resulting in first_dirty taking any value.

---
Changes in v5:
* Be more careful when returning unused portion of a buddy to the heap in alloc_heap_pages() and don't set first_dirty if we know that the sub-buddy is clean
* In reserve_offlined_page(), don't try to find dirty pages in sub-buddies if we can figure out that there is none.
* Drop unnecessary setting of first_dirty in free_heap_pages()
* Switch to using bitfields in page_info.u.free

I kept node_need_scrub[] as a global array and not a "per-node". I think splitting it should be part of making heap_lock a per-node lock, together with increasing scrub concurrency by having more than one CPU scrub a node.

 xen/common/page_alloc.c  | 190 +++++++++++++++++++++++++++++++++++++++--------
 xen/include/asm-arm/mm.h |  18 ++++-
 xen/include/asm-x86/mm.h |  17 ++++-
 3 files changed, 190 insertions(+), 35 deletions(-)
xen/common/page_alloc.c +157 -33
index 8bcef6a..570d1f7 100644
@@ -383,6 +383,8 @@ typedef struct page_list_head heap_by_zone_and_order_t[NR_ZONES][MAX_ORDER+1];
 static heap_by_zone_and_order_t *_heap[MAX_NUMNODES];
 #define heap(node, zone, order) ((*_heap[node])[zone][order])
 
+static unsigned long node_need_scrub[MAX_NUMNODES];
+
 static unsigned long *avail[MAX_NUMNODES];
 static long total_avail_pages;
 
@@ -678,6 +680,20 @@ static void check_low_mem_virq(void)
     }
 }
 
+/* Pages that need a scrub are added to tail, otherwise to head. */
+static void page_list_add_scrub(struct page_info *pg, unsigned int node,
+                                unsigned int zone, unsigned int order,
+                                unsigned int first_dirty)
+{
+    PFN_ORDER(pg) = order;
+    pg->u.free.first_dirty = first_dirty;
+
+    if ( first_dirty != INVALID_DIRTY_IDX )
+        page_list_add_tail(pg, &heap(node, zone, order));
+    else
+        page_list_add(pg, &heap(node, zone, order));
+}
+
 /* Allocate 2^@order contiguous pages. */
 static struct page_info *alloc_heap_pages(
     unsigned int zone_lo, unsigned int zone_hi,
@@ -687,7 +703,7 @@ static struct page_info *alloc_heap_pages(
     unsigned int i, j, zone = 0, nodemask_retry = 0;
     nodeid_t first_node, node = MEMF_get_node(memflags), req_node = node;
     unsigned long request = 1UL << order;
-    struct page_info *pg;
+    struct page_info *pg, *first_dirty_pg = NULL;
     nodemask_t nodemask = (d != NULL ) ? d->node_affinity : node_online_map;
     bool_t need_tlbflush = 0;
     uint32_t tlbflush_timestamp = 0;
@@ -798,11 +814,26 @@ static struct page_info *alloc_heap_pages(
     return NULL;
 
  found: 
+
+    if ( pg->u.free.first_dirty != INVALID_DIRTY_IDX )
+        first_dirty_pg = pg + pg->u.free.first_dirty;
+
     /* We may have to halve the chunk a number of times. */
     while ( j != order )
     {
-        PFN_ORDER(pg) = --j;
-        page_list_add_tail(pg, &heap(node, zone, j));
+        unsigned int first_dirty;
+
+        if ( first_dirty_pg && ((pg + (1 << j)) > first_dirty_pg) )
+        {
+            if ( pg < first_dirty_pg )
+                first_dirty = (first_dirty_pg - pg) / sizeof(*pg);
+            else
+                first_dirty = 0;
+        }
+        else
+            first_dirty = INVALID_DIRTY_IDX;
+
+        page_list_add_scrub(pg, node, zone, --j, first_dirty);
         pg += 1 << j;
     }
 
@@ -849,13 +880,22 @@ static int reserve_offlined_page(struct page_info *head)
 {
     unsigned int node = phys_to_nid(page_to_maddr(head));
     int zone = page_to_zone(head), i, head_order = PFN_ORDER(head), count = 0;
-    struct page_info *cur_head;
+    struct page_info *cur_head, *first_dirty_pg = NULL;
     int cur_order;
 
     ASSERT(spin_is_locked(&heap_lock));
 
     cur_head = head;
 
+    /*
+     * We may break the buddy so let's mark the head as clean. Then, when
+     * merging chunks back into the heap, we will see whether the chunk has
+     * unscrubbed pages and set its first_dirty properly.
+     */
+    if (head->u.free.first_dirty != INVALID_DIRTY_IDX)
+        first_dirty_pg = head + head->u.free.first_dirty;
+    head->u.free.first_dirty = INVALID_DIRTY_IDX;
+
     page_list_del(head, &heap(node, zone, head_order));
 
     while ( cur_head < (head + (1 << head_order)) )
@@ -873,6 +913,8 @@ static int reserve_offlined_page(struct page_info *head)
 
         while ( cur_order < head_order )
         {
+            unsigned int first_dirty = INVALID_DIRTY_IDX;
+
             next_order = cur_order + 1;
 
             if ( (cur_head + (1 << next_order)) >= (head + ( 1 << head_order)) )
@@ -892,8 +934,25 @@ static int reserve_offlined_page(struct page_info *head)
             {
             merge:
                 /* We don't consider merging outside the head_order. */
-                page_list_add_tail(cur_head, &heap(node, zone, cur_order));
-                PFN_ORDER(cur_head) = cur_order;
+
+                /* See if any of the pages indeed need scrubbing. */
+                if ( first_dirty_pg && (cur_head + (1 << cur_order) > first_dirty_pg) )
+                {
+                    if ( cur_head < first_dirty_pg )
+                        i = (first_dirty_pg - cur_head) / sizeof(*cur_head);
+                    else
+                        i = 0;
+
+                    for ( ; i < (1 << cur_order); i++ )
+                        if ( test_bit(_PGC_need_scrub,
+                                      &cur_head[i].count_info) )
+                        {
+                            first_dirty = i;
+                            break;
+                        }
+                }
+                page_list_add_scrub(cur_head, node, zone,
+                                    cur_order, first_dirty);
                 cur_head += (1 << cur_order);
                 break;
             }
@@ -919,9 +978,53 @@ static int reserve_offlined_page(struct page_info *head)
     return count;
 }
 
+static void scrub_free_pages(unsigned int node)
+{
+    struct page_info *pg;
+    unsigned int zone;
+
+    ASSERT(spin_is_locked(&heap_lock));
+
+    if ( !node_need_scrub[node] )
+        return;
+
+    for ( zone = 0; zone < NR_ZONES; zone++ )
+    {
+        unsigned int order = MAX_ORDER;
+
+        do {
+            while ( !page_list_empty(&heap(node, zone, order)) )
+            {
+                unsigned int i;
+
+                /* Unscrubbed pages are always at the end of the list. */
+                pg = page_list_last(&heap(node, zone, order));
+                if ( pg->u.free.first_dirty == INVALID_DIRTY_IDX )
+                    break;
+
+                for ( i = pg->u.free.first_dirty; i < (1U << order); i++)
+                {
+                    if ( test_bit(_PGC_need_scrub, &pg[i].count_info) )
+                    {
+                        scrub_one_page(&pg[i]);
+                        pg[i].count_info &= ~PGC_need_scrub;
+                        node_need_scrub[node]--;
+                    }
+                }
+
+                page_list_del(pg, &heap(node, zone, order));
+                page_list_add_scrub(pg, node, zone, order, INVALID_DIRTY_IDX);
+
+                if ( node_need_scrub[node] == 0 )
+                    return;
+            }
+        } while ( order-- != 0 );
+    }
+}
+
 /* Free 2^@order set of pages. */
 static void free_heap_pages(
-    struct page_info *pg, unsigned int order)
+    struct page_info *pg, unsigned int order, bool need_scrub)
 {
     unsigned long mask, mfn = page_to_mfn(pg);
     unsigned int i, node = phys_to_nid(page_to_maddr(pg)), tainted = 0;
@@ -961,10 +1064,20 @@ static void free_heap_pages(
         /* This page is not a guest frame any more. */
         page_set_owner(&pg[i], NULL); /* set_gpfn_from_mfn snoops pg owner */
         set_gpfn_from_mfn(mfn + i, INVALID_M2P_ENTRY);
+
+        if ( need_scrub )
+            pg[i].count_info |= PGC_need_scrub;
     }
 
     avail[node][zone] += 1 << order;
     total_avail_pages += 1 << order;
+    if ( need_scrub )
+    {
+        node_need_scrub[node] += 1 << order;
+        pg->u.free.first_dirty = 0;
+    }
+    else
+        pg->u.free.first_dirty = INVALID_DIRTY_IDX;
 
     if ( tmem_enabled() )
         midsize_alloc_zone_pages = max(
@@ -977,35 +1090,53 @@ static void free_heap_pages(
 
         if ( (page_to_mfn(pg) & mask) )
         {
+            struct page_info *predecessor = pg - mask;
+
             /* Merge with predecessor block? */
-            if ( !mfn_valid(_mfn(page_to_mfn(pg-mask))) ||
-                 !page_state_is(pg-mask, free) ||
-                 (PFN_ORDER(pg-mask) != order) ||
-                 (phys_to_nid(page_to_maddr(pg-mask)) != node) )
+            if ( !mfn_valid(_mfn(page_to_mfn(predecessor))) ||
+                 !page_state_is(predecessor, free) ||
+                 (PFN_ORDER(predecessor) != order) ||
+                 (phys_to_nid(page_to_maddr(predecessor)) != node) )
                 break;
-            pg -= mask;
-            page_list_del(pg, &heap(node, zone, order));
+
+            page_list_del(predecessor, &heap(node, zone, order));
+
+            if ( predecessor->u.free.first_dirty != INVALID_DIRTY_IDX )
+                need_scrub = true;
+                /* ... and keep predecessor's first_dirty. */
+            else if ( pg->u.free.first_dirty != INVALID_DIRTY_IDX )
+                predecessor->u.free.first_dirty = (1U << order) +
+                                                  pg->u.free.first_dirty;
+
+            pg = predecessor;
         }
         else
         {
+            struct page_info *successor = pg + mask;
+
             /* Merge with successor block? */
-            if ( !mfn_valid(_mfn(page_to_mfn(pg+mask))) ||
-                 !page_state_is(pg+mask, free) ||
-                 (PFN_ORDER(pg+mask) != order) ||
-                 (phys_to_nid(page_to_maddr(pg+mask)) != node) )
+            if ( !mfn_valid(_mfn(page_to_mfn(successor))) ||
+                 !page_state_is(successor, free) ||
+                 (PFN_ORDER(successor) != order) ||
+                 (phys_to_nid(page_to_maddr(successor)) != node) )
                 break;
-            page_list_del(pg + mask, &heap(node, zone, order));
+            page_list_del(successor, &heap(node, zone, order));
+
+            if ( successor->u.free.first_dirty != INVALID_DIRTY_IDX )
+                need_scrub = true;
         }
 
         order++;
     }
 
-    PFN_ORDER(pg) = order;
-    page_list_add_tail(pg, &heap(node, zone, order));
+    page_list_add_scrub(pg, node, zone, order, pg->u.free.first_dirty);
 
     if ( tainted )
         reserve_offlined_page(pg);
 
+    if ( need_scrub )
+        scrub_free_pages(node);
+
     spin_unlock(&heap_lock);
 }
 
@@ -1226,7 +1357,7 @@ unsigned int online_page(unsigned long mfn, uint32_t *status)
     spin_unlock(&heap_lock);
 
     if ( (y & PGC_state) == PGC_state_offlined )
-        free_heap_pages(pg, 0);
+        free_heap_pages(pg, 0, false);
 
     return ret;
 }
@@ -1295,7 +1426,7 @@ static void init_heap_pages(
             nr_pages -= n;
         }
 
-        free_heap_pages(pg+i, 0);
+        free_heap_pages(pg + i, 0, false);
     }
 }
 
@@ -1622,7 +1753,7 @@ void free_xenheap_pages(void *v, unsigned int order)
 
     memguard_guard_range(v, 1 << (order + PAGE_SHIFT));
 
-    free_heap_pages(virt_to_page(v), order);
+    free_heap_pages(virt_to_page(v), order, false);
 }
 
 #else
@@ -1676,12 +1807,9 @@ void free_xenheap_pages(void *v, unsigned int order)
     pg = virt_to_page(v);
 
     for ( i = 0; i < (1u << order); i++ )
-    {
-        scrub_one_page(&pg[i]);
         pg[i].count_info &= ~PGC_xen_heap;
-    }
 
-    free_heap_pages(pg, order);
+    free_heap_pages(pg, order, true);
 }
 
 #endif
@@ -1790,7 +1918,7 @@ struct page_info *alloc_domheap_pages(
     if ( d && !(memflags & MEMF_no_owner) &&
          assign_pages(d, pg, order, memflags) )
     {
-        free_heap_pages(pg, order);
+        free_heap_pages(pg, order, false);
         return NULL;
     }
     
@@ -1858,11 +1986,7 @@ void free_domheap_pages(struct page_info *pg, unsigned int order)
             scrub = 1;
         }
 
-        if ( unlikely(scrub) )
-            for ( i = 0; i < (1 << order); i++ )
-                scrub_one_page(&pg[i]);
-
-        free_heap_pages(pg, order);
+        free_heap_pages(pg, order, scrub);
     }
 
     if ( drop_dom_ref )
xen/include/asm-arm/mm.h +17 -1
index 13c673a..889a85e 100644
@@ -44,7 +44,16 @@ struct page_info
         /* Page is on a free list: ((count_info & PGC_count_mask) == 0). */
         struct {
             /* Do TLBs need flushing for safety before next page use? */
-            bool_t need_tlbflush;
+            unsigned long need_tlbflush:1;
+
+            /*
+             * Index of the first *possibly* unscrubbed page in the buddy.
+             * One more than maximum possible order (MAX_ORDER+1) to
+             * accommodate INVALID_DIRTY_IDX.
+             */
+#define INVALID_DIRTY_IDX (-1UL & (((1UL<<MAX_ORDER) + 2) - 1))
+            unsigned long first_dirty:MAX_ORDER + 2;
+
         } free;
 
     } u;
@@ -107,6 +116,13 @@ struct page_info
 #define PGC_count_width   PG_shift(9)
 #define PGC_count_mask    ((1UL<<PGC_count_width)-1)
 
+/*
+ * Page needs to be scrubbed. Since this bit can only be set on a page that is
+ * free (i.e. in PGC_state_free) we can reuse PGC_allocated bit.
+ */
+#define _PGC_need_scrub   _PGC_allocated
+#define PGC_need_scrub    PGC_allocated
+
 extern unsigned long xenheap_mfn_start, xenheap_mfn_end;
 extern vaddr_t xenheap_virt_end;
 #ifdef CONFIG_ARM_64
xen/include/asm-x86/mm.h +16 -1
index d33599d..cd00bef 100644
@@ -88,7 +88,15 @@ struct page_info
         /* Page is on a free list: ((count_info & PGC_count_mask) == 0). */
         struct {
             /* Do TLBs need flushing for safety before next page use? */
-            bool_t need_tlbflush;
+            unsigned long need_tlbflush:1;
+
+            /*
+             * Index of the first *possibly* unscrubbed page in the buddy.
+             * One more than maximum possible order (MAX_ORDER+1) to
+             * accommodate INVALID_DIRTY_IDX.
+             */
+#define INVALID_DIRTY_IDX (-1UL & (((1UL<<MAX_ORDER) + 2) - 1))
+            unsigned long first_dirty:MAX_ORDER + 2;
         } free;
 
     } u;
@@ -233,6 +241,13 @@ struct page_info
 #define PGC_count_width   PG_shift(9)
 #define PGC_count_mask    ((1UL<<PGC_count_width)-1)
 
+/*
+ * Page needs to be scrubbed. Since this bit can only be set on a page that is
+ * free (i.e. in PGC_state_free) we can reuse PGC_allocated bit.
+ */
+#define _PGC_need_scrub   _PGC_allocated
+#define PGC_need_scrub    PGC_allocated
+
 struct spage_info
 {
        unsigned long type_info;
Signature · 7 lines
1.8.3.1


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

Re: [PATCH v5 1/8] mm: Place unscrubbed pages at the end of pagelist

Quoted 4 lines
Quoted 1 line
Quoted 1 line

Boris Ostrovsky <[email protected]> 06/22/17 8:55 PM >>>

I kept node_need_scrub[] as a global array and not a "per-node". I think splitting it should be part of making heap_lock a per-node lock, together with increasing scrub concurrency by having more than one CPU scrub a node.

Agreed - I hadn't meant my earlier comment to necessarily affect this series.

Quoted 6 lines
xen/common/page_alloc.c +2 -0
@@ -798,11 +814,26 @@ static struct page_info *alloc_heap_pages(
     return NULL;
 
  found: 
+
+    if ( pg->u.free.first_dirty != INVALID_DIRTY_IDX )

xen/common/page_alloc.c static struct page_info *alloc_heap_pages( · line 826

Quoted 10 lines
xen/common/page_alloc.c +5 -2
+        first_dirty_pg = pg + pg->u.free.first_dirty;
+
     /* We may have to halve the chunk a number of times. */
     while ( j != order )
     {
-        PFN_ORDER(pg) = --j;
-        page_list_add_tail(pg, &heap(node, zone, j));
+        unsigned int first_dirty;
+
+        if ( first_dirty_pg && ((pg + (1 << j)) > first_dirty_pg) )

Despite the various examples of doing it this way, please at least use 1u.

xen/common/page_alloc.c static struct page_info *alloc_heap_pages( · line 829

Quoted 3 lines
xen/common/page_alloc.c +3 -0
+        {
+            if ( pg < first_dirty_pg )
+                first_dirty = (first_dirty_pg - pg) / sizeof(*pg);

Pointer subtraction already includes the involved division. Otoh I wonder if you couldn't get away without pointer comparison/subtraction here altogether.

Quoted 8 lines
xen/common/page_alloc.c +1 -1
@@ -849,13 +880,22 @@ static int reserve_offlined_page(struct page_info *head)
 {
     unsigned int node = phys_to_nid(page_to_maddr(head));
     int zone = page_to_zone(head), i, head_order = PFN_ORDER(head), count = 0;
-    struct page_info *cur_head;
+    struct page_info *cur_head, *first_dirty_pg = NULL;
     int cur_order;
 

xen/common/page_alloc.c static int reserve_offlined_page(struct page_info *head) · line 895

Quoted 10 lines
xen/common/page_alloc.c +6 -0
     ASSERT(spin_is_locked(&heap_lock));
 
     cur_head = head;
 
+    /*
+     * We may break the buddy so let's mark the head as clean. Then, when
+     * merging chunks back into the heap, we will see whether the chunk has
+     * unscrubbed pages and set its first_dirty properly.
+     */
+    if (head->u.free.first_dirty != INVALID_DIRTY_IDX)

Coding style.

Quoted 12 lines
xen/common/page_alloc.c +6 -2
@@ -892,8 +934,25 @@ static int reserve_offlined_page(struct page_info *head)
             {
             merge:
                 /* We don't consider merging outside the head_order. */
-                page_list_add_tail(cur_head, &heap(node, zone, cur_order));
-                PFN_ORDER(cur_head) = cur_order;
+
+                /* See if any of the pages indeed need scrubbing. */
+                if ( first_dirty_pg && (cur_head + (1 << cur_order) > first_dirty_pg) )
+                {
+                    if ( cur_head < first_dirty_pg )
+                        i = (first_dirty_pg - cur_head) / sizeof(*cur_head);

xen/common/page_alloc.c static int reserve_offlined_page(struct page_info *head) · line 952

Quoted 10 lines
xen/common/page_alloc.c +10 -0
+                    else
+                        i = 0;
+
+                    for ( ; i < (1 << cur_order); i++ )
+                        if ( test_bit(_PGC_need_scrub,
+                                      &cur_head[i].count_info) )
+                        {
+                            first_dirty = i;
+                            break;
+                        }

Perhaps worth having ASSERT(first_dirty != INVALID_DIRTY_IDX) here? Or are there cases where ->u.free.first_dirty of a page may be wrong?

Quoted 13 lines
xen/common/page_alloc.c +4 -4
@@ -977,35 +1090,53 @@ static void free_heap_pages(
 
         if ( (page_to_mfn(pg) & mask) )
         {
+            struct page_info *predecessor = pg - mask;
+
             /* Merge with predecessor block? */
-            if ( !mfn_valid(_mfn(page_to_mfn(pg-mask))) ||
-                 !page_state_is(pg-mask, free) ||
-                 (PFN_ORDER(pg-mask) != order) ||
-                 (phys_to_nid(page_to_maddr(pg-mask)) != node) )
+            if ( !mfn_valid(_mfn(page_to_mfn(predecessor))) ||
+                 !page_state_is(predecessor, free) ||

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

Quoted 10 lines
xen/common/page_alloc.c +7 -2
+                 (PFN_ORDER(predecessor) != order) ||
+                 (phys_to_nid(page_to_maddr(predecessor)) != node) )
                 break;
-            pg -= mask;
-            page_list_del(pg, &heap(node, zone, order));
+
+            page_list_del(predecessor, &heap(node, zone, order));
+
+            if ( predecessor->u.free.first_dirty != INVALID_DIRTY_IDX )
+                need_scrub = true;

I'm afraid I continue to be confused by this: Why does need_scrub depend on the state of pages not being the subject of the current free operation? I realize that at this point in the series the path can't be taken yet, but won't later patches need to rip it out or change it anyway, in which case it would be better to introduce the then correct check (if any) only there?

xen/include/asm-x86/mm.h struct page_info · line 95

Quoted 12 lines
xen/include/asm-x86/mm.h +5 -1
@@ -88,7 +88,15 @@ struct page_info
         /* Page is on a free list: ((count_info & PGC_count_mask) == 0). */
         struct {
             /* Do TLBs need flushing for safety before next page use? */
-            bool_t need_tlbflush;
+            unsigned long need_tlbflush:1;
+
+            /*
+             * Index of the first *possibly* unscrubbed page in the buddy.
+             * One more than maximum possible order (MAX_ORDER+1) to

Why +1 here and hence ...

xen/include/asm-x86/mm.h struct page_info · line 99

Quoted 4 lines
xen/include/asm-x86/mm.h +4 -0
+             * accommodate INVALID_DIRTY_IDX.
+             */
+#define INVALID_DIRTY_IDX (-1UL & (((1UL<<MAX_ORDER) + 2) - 1))
+            unsigned long first_dirty:MAX_ORDER + 2;

... why +2 instead of +1? And isn't the expression INVALID_DIRTY_IDX wrongly parenthesized (apart from lacking blanks around the shift operator)? I'd expect you want a value with MAX_ORDER+1 set bits, i.e.
(1UL << (MAX_ORDER + 1)) - 1. ANDing with -1UL seems quite pointless too.

Jan

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

#3 lore raw

Re: [PATCH v5 1/8] mm: Place unscrubbed pages at the end of pagelist

Quoted 26 lines · Jan Beulich
Quoted 4 lines
Quoted 1 line
Quoted 1 line

Boris Ostrovsky <[email protected]> 06/22/17 8:55 PM >>>

I kept node_need_scrub[] as a global array and not a "per-node". I think splitting it should be part of making heap_lock a per-node lock, together with increasing scrub concurrency by having more than one CPU scrub a node.

Agreed - I hadn't meant my earlier comment to necessarily affect this series.

Quoted 6 lines
xen/common/page_alloc.c +2 -0
@@ -798,11 +814,26 @@ static struct page_info *alloc_heap_pages(
      return NULL;
 
   found:
+
+    if ( pg->u.free.first_dirty != INVALID_DIRTY_IDX )

xen/common/page_alloc.c static struct page_info *alloc_heap_pages( · line 826

Quoted 10 lines
xen/common/page_alloc.c +5 -2
+        first_dirty_pg = pg + pg->u.free.first_dirty;
+
      /* We may have to halve the chunk a number of times. */
      while ( j != order )
      {
-        PFN_ORDER(pg) = --j;
-        page_list_add_tail(pg, &heap(node, zone, j));
+        unsigned int first_dirty;
+
+        if ( first_dirty_pg && ((pg + (1 << j)) > first_dirty_pg) )

Despite the various examples of doing it this way, please at least use 1u.

Quoted 5 lines

xen/common/page_alloc.c static struct page_info *alloc_heap_pages( · line 829

Quoted 3 lines
xen/common/page_alloc.c +3 -0
+        {
+            if ( pg < first_dirty_pg )
+                first_dirty = (first_dirty_pg - pg) / sizeof(*pg);

Pointer subtraction already includes the involved division.

Yes, this was a mistake.

Quoted 3 lines

Otoh I wonder
if you couldn't get away without pointer comparison/subtraction here altogether.

Without comparison I can only assume that first_dirty is zero (i.e. the whole buddy is potentially dirty). Is there something else I could do?

Quoted 22 lines
Quoted 9 lines
xen/common/page_alloc.c +1 -1
@@ -849,13 +880,22 @@ static int reserve_offlined_page(struct page_info *head)
  {
      unsigned int node = phys_to_nid(page_to_maddr(head));
      int zone = page_to_zone(head), i, head_order = PFN_ORDER(head), count = 0;
-    struct page_info *cur_head;
+    struct page_info *cur_head, *first_dirty_pg = NULL;
      int cur_order;
 

xen/common/page_alloc.c static int reserve_offlined_page(struct page_info *head) · line 895

Quoted 10 lines
xen/common/page_alloc.c +6 -0
      ASSERT(spin_is_locked(&heap_lock));
 
      cur_head = head;
 
+    /*
+     * We may break the buddy so let's mark the head as clean. Then, when
+     * merging chunks back into the heap, we will see whether the chunk has
+     * unscrubbed pages and set its first_dirty properly.
+     */
+    if (head->u.free.first_dirty != INVALID_DIRTY_IDX)

Coding style.

Quoted 12 lines

xen/common/page_alloc.c static int reserve_offlined_page(struct page_info *head) · line 942

Quoted 12 lines
xen/common/page_alloc.c +6 -2
@@ -892,8 +934,25 @@ static int reserve_offlined_page(struct page_info *head)
              {
              merge:
                  /* We don't consider merging outside the head_order. */
-                page_list_add_tail(cur_head, &heap(node, zone, cur_order));
-                PFN_ORDER(cur_head) = cur_order;
+
+                /* See if any of the pages indeed need scrubbing. */
+                if ( first_dirty_pg && (cur_head + (1 << cur_order) > first_dirty_pg) )
+                {
+                    if ( cur_head < first_dirty_pg )
+                        i = (first_dirty_pg - cur_head) / sizeof(*cur_head);

I assume the same comment as above applies here.

Quoted 11 lines

xen/common/page_alloc.c static int reserve_offlined_page(struct page_info *head) · line 952

Quoted 10 lines
xen/common/page_alloc.c +10 -0
+                    else
+                        i = 0;
+
+                    for ( ; i < (1 << cur_order); i++ )
+                        if ( test_bit(_PGC_need_scrub,
+                                      &cur_head[i].count_info) )
+                        {
+                            first_dirty = i;
+                            break;
+                        }
Quoted 2 lines

Perhaps worth having ASSERT(first_dirty != INVALID_DIRTY_IDX) here? Or are there cases where ->u.free.first_dirty of a page may be wrong?

When we merge in free_heap_pages we don't clear first_dirty of the successor buddy (at some point I did have this done but you questioned whether it was needed and I dropped it).

Quoted 22 lines
Quoted 22 lines
xen/common/page_alloc.c +9 -6
@@ -977,35 +1090,53 @@ static void free_heap_pages(
 
          if ( (page_to_mfn(pg) & mask) )
          {
+            struct page_info *predecessor = pg - mask;
+
              /* Merge with predecessor block? */
-            if ( !mfn_valid(_mfn(page_to_mfn(pg-mask))) ||
-                 !page_state_is(pg-mask, free) ||
-                 (PFN_ORDER(pg-mask) != order) ||
-                 (phys_to_nid(page_to_maddr(pg-mask)) != node) )
+            if ( !mfn_valid(_mfn(page_to_mfn(predecessor))) ||
+                 !page_state_is(predecessor, free) ||
+                 (PFN_ORDER(predecessor) != order) ||
+                 (phys_to_nid(page_to_maddr(predecessor)) != node) )
                  break;
-            pg -= mask;
-            page_list_del(pg, &heap(node, zone, order));
+
+            page_list_del(predecessor, &heap(node, zone, order));
+
Quoted 8 lines

Review static void free_heap_pages( · line 1105

Quoted 2 lines
(quoted diff) +2 -0
+            if ( predecessor->u.free.first_dirty != INVALID_DIRTY_IDX )
+                need_scrub = true;

I'm afraid I continue to be confused by this: Why does need_scrub depend on the state of pages not being the subject of the current free operation? I realize that at this point in the series the path can't be taken yet, but won't later patches need to rip it out or change it anyway, in which case it would be better to introduce the then correct check (if any) only there?

Right, at this point we indeed will never have the 'if' evaluate to true since heap is always clean. And when we switch to scrubbing from idle need_scrub is never looked at.

I suspect this check will not be needed in the intermediate patches neither.

Quoted 15 lines

xen/include/asm-x86/mm.h struct page_info · line 95

Quoted 13 lines
xen/include/asm-x86/mm.h +5 -1
@@ -88,7 +88,15 @@ struct page_info
          /* Page is on a free list: ((count_info & PGC_count_mask) == 0). */
          struct {
              /* Do TLBs need flushing for safety before next page use? */
-            bool_t need_tlbflush;
+            unsigned long need_tlbflush:1;
+
+            /*
+             * Index of the first *possibly* unscrubbed page in the buddy.
+             * One more than maximum possible order (MAX_ORDER+1) to

Why +1 here and hence ...

Don't we have MAX_ORDER+1 orders?

Quoted 10 lines

xen/include/asm-x86/mm.h struct page_info · line 99

Quoted 5 lines
xen/include/asm-x86/mm.h +4 -0
+             * accommodate INVALID_DIRTY_IDX.
+             */
+#define INVALID_DIRTY_IDX (-1UL & (((1UL<<MAX_ORDER) + 2) - 1))
+            unsigned long first_dirty:MAX_ORDER + 2;

... why +2 instead of +1? And isn't the expression INVALID_DIRTY_IDX wrongly parenthesized (apart from lacking blanks around the shift operator)? I'd expect you want a value with MAX_ORDER+1 set bits, i.e.
(1UL << (MAX_ORDER + 1)) - 1. ANDing with -1UL seems quite pointless too.

Yes to parentheses and AND. Should be (1UL << (MAX_ORDER + 2)) - 1

-boris

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

#4 lore raw

Re: [PATCH v5 1/8] mm: Place unscrubbed pages at the end of pagelist

Quoted 12 lines
Quoted 1 line
Quoted 1 line

Boris Ostrovsky <[email protected]> 07/23/17 4:01 AM >>>

Quoted 6 lines · Jan Beulich

xen/common/page_alloc.c static struct page_info *alloc_heap_pages( · line 829

Quoted 4 lines
Quoted 1 line
Quoted 1 line

Boris Ostrovsky <[email protected]> 06/22/17 8:55 PM >>>

xen/common/page_alloc.c +3 -0
+        {
+            if ( pg < first_dirty_pg )
+                first_dirty = (first_dirty_pg - pg) / sizeof(*pg);

Pointer subtraction already includes the involved division.

Yes, this was a mistake.

Quoted 7 lines
Quoted 3 lines

Otoh I wonder
if you couldn't get away without pointer comparison/subtraction here altogether.

Without comparison I can only assume that first_dirty is zero (i.e. the whole buddy is potentially dirty). Is there something else I could do?

I was thinking of tracking indexes instead of pointers. But maybe that would more hamper readability of the overall result than help it.

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
xen/common/page_alloc.c +0 -2
@@ -892,8 +934,25 @@ static int reserve_offlined_page(struct page_info *head)
              {
              merge:
                  /* We don't consider merging outside the head_order. */
-                page_list_add_tail(cur_head, &heap(node, zone, cur_order));
-                PFN_ORDER(cur_head) = cur_order;
Quoted 8 lines
Quoted 6 lines

xen/common/page_alloc.c static int reserve_offlined_page(struct page_info *head) · line 942

Quoted 6 lines
xen/common/page_alloc.c +6 -0
+
+                /* See if any of the pages indeed need scrubbing. */
+                if ( first_dirty_pg && (cur_head + (1 << cur_order) > first_dirty_pg) )
+                {
+                    if ( cur_head < first_dirty_pg )
+                        i = (first_dirty_pg - cur_head) / sizeof(*cur_head);

I assume the same comment as above applies here.

Of course. I usually avoid repeating the same comment, except maybe when reviewing patches of first time contributors.

Quoted 11 lines
Quoted 11 lines
Quoted 10 lines
xen/common/page_alloc.c +10 -0
+                    else
+                        i = 0;
+
+                    for ( ; i < (1 << cur_order); i++ )
+                        if ( test_bit(_PGC_need_scrub,
+                                      &cur_head[i].count_info) )
+                        {
+                            first_dirty = i;
+                            break;
+                        }
Quoted 7 lines
Quoted 2 lines

Perhaps worth having ASSERT(first_dirty != INVALID_DIRTY_IDX) here? Or are there cases where ->u.free.first_dirty of a page may be wrong?

When we merge in free_heap_pages we don't clear first_dirty of the successor buddy (at some point I did have this done but you questioned whether it was needed and I dropped it).

Hmm, this indeed answers my question, but doesn't help (me) understanding whether the suggested ASSERT() could be wrong.

Quoted 15 lines
Quoted 6 lines
Quoted 6 lines
xen/include/asm-x86/mm.h +0 -0
@@ -88,7 +88,15 @@ struct page_info
          /* Page is on a free list: ((count_info & PGC_count_mask) == 0). */
          struct {
              /* Do TLBs need flushing for safety before next page use? */
Quoted 8 lines

xen/include/asm-x86/mm.h struct page_info · line 95

Quoted 6 lines
xen/include/asm-x86/mm.h +5 -1
-            bool_t need_tlbflush;
+            unsigned long need_tlbflush:1;
+
+            /*
+             * Index of the first *possibly* unscrubbed page in the buddy.
+             * One more than maximum possible order (MAX_ORDER+1) to

Why +1 here and hence ...

Quoted 1 line

Don't we have MAX_ORDER+1 orders?

So here there might be a simple misunderstanding: I understand the parenthesized MAX_ORDER+1 to represent "maximum possible order", i.e. excluding the "one more than", not the least because of the ...

Quoted 4 lines

xen/include/asm-arm/mm.h struct page_info · line 55

Quoted 4 lines
xen/include/asm-arm/mm.h +4 -0
+             * accommodate INVALID_DIRTY_IDX.
+             */
+#define INVALID_DIRTY_IDX (-1UL & (((1UL<<MAX_ORDER) + 2) - 1))
+            unsigned long first_dirty:MAX_ORDER + 2;

+2 here.

Quoted 6 lines
Quoted 4 lines

... why +2 instead of +1? And isn't the expression INVALID_DIRTY_IDX wrongly parenthesized (apart from lacking blanks around the shift operator)? I'd expect you want a value with MAX_ORDER+1 set bits, i.e.
(1UL << (MAX_ORDER + 1)) - 1. ANDing with -1UL seems quite pointless too.

Yes to parentheses and AND. Should be (1UL << (MAX_ORDER + 2)) - 1

I.e. I would still expect it to be (1UL << (MAX_ORDER + 1)) - 1 here.

Jan

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

#5 lore raw

Re: [PATCH v5 1/8] mm: Place unscrubbed pages at the end of pagelist

Quoted 10 lines · Jan Beulich
Quoted 10 lines
Quoted 1 line
Quoted 1 line

Boris Ostrovsky <[email protected]> 07/23/17 4:01 AM >>>

Quoted 5 lines · Jan Beulich

xen/common/page_alloc.c static struct page_info *alloc_heap_pages( · line 829

Quoted 4 lines
Quoted 1 line
Quoted 1 line

Boris Ostrovsky <[email protected]> 06/22/17 8:55 PM >>>

xen/common/page_alloc.c +3 -0
+        {
+            if ( pg < first_dirty_pg )
+                first_dirty = (first_dirty_pg - pg) / sizeof(*pg);

Pointer subtraction already includes the involved division.

Yes, this was a mistake.

Quoted 8 lines
Quoted 6 lines
Quoted 3 lines

Otoh I wonder
if you couldn't get away without pointer comparison/subtraction here altogether.

Without comparison I can only assume that first_dirty is zero (i.e. the whole buddy is potentially dirty). Is there something else I could do?

I was thinking of tracking indexes instead of pointers. But maybe that would more hamper readability of the overall result than help it.

I'll try to see how it looks.

Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
xen/common/page_alloc.c +10 -0
+                    else
+                        i = 0;
+
+                    for ( ; i < (1 << cur_order); i++ )
+                        if ( test_bit(_PGC_need_scrub,
+                                      &cur_head[i].count_info) )
+                        {
+                            first_dirty = i;
+                            break;
+                        }
Quoted 8 lines
Quoted 6 lines
Quoted 2 lines

Perhaps worth having ASSERT(first_dirty != INVALID_DIRTY_IDX) here? Or are there cases where ->u.free.first_dirty of a page may be wrong?

When we merge in free_heap_pages we don't clear first_dirty of the successor buddy (at some point I did have this done but you questioned whether it was needed and I dropped it).

Hmm, this indeed answers my question, but doesn't help (me) understanding whether the suggested ASSERT() could be wrong.

Oh, I see what you were asking --- ASSERT() *after* the loop, to make sure we indeed found the first dirty page. Yes, I will add it.

Quoted 26 lines
Quoted 14 lines
Quoted 14 lines

xen/include/asm-x86/mm.h struct page_info · line 95

Quoted 13 lines
xen/include/asm-x86/mm.h +5 -1
@@ -88,7 +88,15 @@ struct page_info
          /* Page is on a free list: ((count_info & PGC_count_mask) == 0). */
          struct {
              /* Do TLBs need flushing for safety before next page use? */
-            bool_t need_tlbflush;
+            unsigned long need_tlbflush:1;
+
+            /*
+             * Index of the first *possibly* unscrubbed page in the buddy.
+             * One more than maximum possible order (MAX_ORDER+1) to

Why +1 here and hence ...

Quoted 1 line

Don't we have MAX_ORDER+1 orders?

So here there might be a simple misunderstanding: I understand the parenthesized MAX_ORDER+1 to represent "maximum possible order", i.e. excluding the "one more than", not the least because of the ...

Quoted 4 lines

xen/include/asm-arm/mm.h struct page_info · line 55

Quoted 4 lines
xen/include/asm-arm/mm.h +4 -0
+             * accommodate INVALID_DIRTY_IDX.
+             */
+#define INVALID_DIRTY_IDX (-1UL & (((1UL<<MAX_ORDER) + 2) - 1))
+            unsigned long first_dirty:MAX_ORDER + 2;

+2 here.

Quoted 7 lines
Quoted 5 lines
Quoted 4 lines

... why +2 instead of +1? And isn't the expression INVALID_DIRTY_IDX wrongly parenthesized (apart from lacking blanks around the shift operator)? I'd expect you want a value with MAX_ORDER+1 set bits, i.e.
(1UL << (MAX_ORDER + 1)) - 1. ANDing with -1UL seems quite pointless too.

Yes to parentheses and AND. Should be (1UL << (MAX_ORDER + 2)) - 1

I.e. I would still expect it to be (1UL << (MAX_ORDER + 1)) - 1 here.

Sorry, I still don't get it.

Say, MAX_ORDER is 1. Since this implies that indexes 0, 1, 2 and 3 are all valid (because we can have up to 2^(MAX_ORDER+1) pages), don't we need 3 bits to indicate an invalid index?

-boris

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

#6 lore raw

Re: [PATCH v5 1/8] mm: Place unscrubbed pages at the end of pagelist

Quoted 1 line
Quoted 1 line
Quoted 1 line

Boris Ostrovsky <[email protected]> 07/31/17 6:03 PM >>>

Quoted 37 lines · Jan Beulich
Quoted 28 lines
Quoted 1 line
Quoted 1 line

Boris Ostrovsky <[email protected]> 07/23/17 4:01 AM >>>

Quoted 14 lines · Jan Beulich
Quoted 14 lines

xen/include/asm-x86/mm.h struct page_info · line 95

Quoted 13 lines
Quoted 1 line

Boris Ostrovsky <[email protected]> 06/22/17 8:55 PM >>>

xen/include/asm-x86/mm.h +5 -1
@@ -88,7 +88,15 @@ struct page_info
          /* Page is on a free list: ((count_info & PGC_count_mask) == 0). */
          struct {
              /* Do TLBs need flushing for safety before next page use? */
-            bool_t need_tlbflush;
+            unsigned long need_tlbflush:1;
+
+            /*
+             * Index of the first *possibly* unscrubbed page in the buddy.
+             * One more than maximum possible order (MAX_ORDER+1) to

Why +1 here and hence ...

Quoted 1 line

Don't we have MAX_ORDER+1 orders?

So here there might be a simple misunderstanding: I understand the parenthesized MAX_ORDER+1 to represent "maximum possible order", i.e. excluding the "one more than", not the least because of the ...

Quoted 4 lines

xen/include/asm-arm/mm.h struct page_info · line 55

Quoted 4 lines
xen/include/asm-arm/mm.h +4 -0
+             * accommodate INVALID_DIRTY_IDX.
+             */
+#define INVALID_DIRTY_IDX (-1UL & (((1UL<<MAX_ORDER) + 2) - 1))
+            unsigned long first_dirty:MAX_ORDER + 2;

+2 here.

Quoted 7 lines
Quoted 5 lines
Quoted 4 lines

... why +2 instead of +1? And isn't the expression INVALID_DIRTY_IDX wrongly parenthesized (apart from lacking blanks around the shift operator)? I'd expect you want a value with MAX_ORDER+1 set bits, i.e.
(1UL << (MAX_ORDER + 1)) - 1. ANDing with -1UL seems quite pointless too.

Yes to parentheses and AND. Should be (1UL << (MAX_ORDER + 2)) - 1

I.e. I would still expect it to be (1UL << (MAX_ORDER + 1)) - 1 here.

Quoted 5 lines

Sorry, I still don't get it.

Say, MAX_ORDER is 1. Since this implies that indexes 0, 1, 2 and 3 are all valid (because we can have up to 2^(MAX_ORDER+1) pages), don't we need 3 bits to indicate an invalid index?

Why 0, 1, 2, and 3? When MAX_ORDER is 1, we only have a single bit, i.e. valid values 0 and 1 (plus one more for the invalid indicator), i.e. need 2 bits for representation of all used values.

Jan

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

#7 lore raw

Re: [PATCH v5 1/8] mm: Place unscrubbed pages at the end of pagelist

Quoted 38 lines · Jan Beulich
Quoted 1 line
Quoted 1 line
Quoted 1 line

Boris Ostrovsky <[email protected]> 07/31/17 6:03 PM >>>

Quoted 36 lines · Jan Beulich
Quoted 35 lines
Quoted 1 line
Quoted 1 line

Boris Ostrovsky <[email protected]> 07/23/17 4:01 AM >>>

Quoted 14 lines · Jan Beulich
Quoted 14 lines

xen/include/asm-x86/mm.h struct page_info · line 95

Quoted 13 lines
Quoted 1 line

Boris Ostrovsky <[email protected]> 06/22/17 8:55 PM >>>

xen/include/asm-x86/mm.h +5 -1
@@ -88,7 +88,15 @@ struct page_info
          /* Page is on a free list: ((count_info & PGC_count_mask) == 0). */
          struct {
              /* Do TLBs need flushing for safety before next page use? */
-            bool_t need_tlbflush;
+            unsigned long need_tlbflush:1;
+
+            /*
+             * Index of the first *possibly* unscrubbed page in the buddy.
+             * One more than maximum possible order (MAX_ORDER+1) to

Why +1 here and hence ...

Quoted 1 line

Don't we have MAX_ORDER+1 orders?

So here there might be a simple misunderstanding: I understand the parenthesized MAX_ORDER+1 to represent "maximum possible order", i.e. excluding the "one more than", not the least because of the ...

Quoted 4 lines

xen/include/asm-arm/mm.h struct page_info · line 55

Quoted 4 lines
xen/include/asm-arm/mm.h +4 -0
+             * accommodate INVALID_DIRTY_IDX.
+             */
+#define INVALID_DIRTY_IDX (-1UL & (((1UL<<MAX_ORDER) + 2) - 1))
+            unsigned long first_dirty:MAX_ORDER + 2;

+2 here.

Quoted 5 lines
Quoted 4 lines

... why +2 instead of +1? And isn't the expression INVALID_DIRTY_IDX wrongly parenthesized (apart from lacking blanks around the shift operator)? I'd expect you want a value with MAX_ORDER+1 set bits, i.e.
(1UL << (MAX_ORDER + 1)) - 1. ANDing with -1UL seems quite pointless too.

Yes to parentheses and AND. Should be (1UL << (MAX_ORDER + 2)) - 1

I.e. I would still expect it to be (1UL << (MAX_ORDER + 1)) - 1 here.

Quoted 6 lines
Quoted 5 lines

Sorry, I still don't get it.

Say, MAX_ORDER is 1. Since this implies that indexes 0, 1, 2 and 3 are all valid (because we can have up to 2^(MAX_ORDER+1) pages), don't we need 3 bits to indicate an invalid index?

Why 0, 1, 2, and 3?

Of course, it's 0 and 1 only. MAX_ORDER+1 completely threw me off.

-boris

Quoted 6 lines

When MAX_ORDER is 1, we only have a single bit, i.e.
valid values 0 and 1 (plus one more for the invalid indicator), i.e. need 2 bits for representation of all used values.

Jan

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

#8 lore raw

PATCH v5 2/8 mm: Extract allocation loop from alloc_heap_pages()

In reply to Boris Ostrovsky

This will make code a bit more readable, especially with changes that will be introduced in subsequent patches.

---
Changes in v5:
* Constified get_free_buddy()'s struct domain argument
* Dropped request local variable in get_free_buddy().

Because of rebasing there were few more changes in this patch so I decided not to keep Jan's ACK.

 xen/common/page_alloc.c | 143 ++++++++++++++++++++++++++----------------------
 1 file changed, 79 insertions(+), 64 deletions(-)
xen/common/page_alloc.c +79 -64
index 570d1f7..89fe3ce 100644
@@ -694,22 +694,15 @@ static void page_list_add_scrub(struct page_info *pg, unsigned int node,
         page_list_add(pg, &heap(node, zone, order));
 }
 
-/* Allocate 2^@order contiguous pages. */
-static struct page_info *alloc_heap_pages(
-    unsigned int zone_lo, unsigned int zone_hi,
-    unsigned int order, unsigned int memflags,
-    struct domain *d)
+static struct page_info *get_free_buddy(unsigned int zone_lo,
+                                        unsigned int zone_hi,
+                                        unsigned int order, unsigned int memflags,
+                                        const struct domain *d)
 {
-    unsigned int i, j, zone = 0, nodemask_retry = 0;
     nodeid_t first_node, node = MEMF_get_node(memflags), req_node = node;
-    unsigned long request = 1UL << order;
-    struct page_info *pg, *first_dirty_pg = NULL;
-    nodemask_t nodemask = (d != NULL ) ? d->node_affinity : node_online_map;
-    bool_t need_tlbflush = 0;
-    uint32_t tlbflush_timestamp = 0;
-
-    /* Make sure there are enough bits in memflags for nodeID. */
-    BUILD_BUG_ON((_MEMF_bits - _MEMF_node) < (8 * sizeof(nodeid_t)));
+    nodemask_t nodemask = d ? d->node_affinity : node_online_map;
+    unsigned int j, zone, nodemask_retry = 0;
+    struct page_info *pg;
 
     if ( node == NUMA_NO_NODE )
     {
@@ -725,34 +718,6 @@ static struct page_info *alloc_heap_pages(
     first_node = node;
 
     ASSERT(node < MAX_NUMNODES);
-    ASSERT(zone_lo <= zone_hi);
-    ASSERT(zone_hi < NR_ZONES);
-
-    if ( unlikely(order > MAX_ORDER) )
-        return NULL;
-
-    spin_lock(&heap_lock);
-
-    /*
-     * Claimed memory is considered unavailable unless the request
-     * is made by a domain with sufficient unclaimed pages.
-     */
-    if ( (outstanding_claims + request >
-          total_avail_pages + tmem_freeable_pages()) &&
-          ((memflags & MEMF_no_refcount) ||
-           !d || d->outstanding_pages < request) )
-        goto not_found;
-
-    /*
-     * TMEM: When available memory is scarce due to tmem absorbing it, allow
-     * only mid-size allocations to avoid worst of fragmentation issues.
-     * Others try tmem pools then fail.  This is a workaround until all
-     * post-dom0-creation-multi-page allocations can be eliminated.
-     */
-    if ( ((order == 0) || (order >= 9)) &&
-         (total_avail_pages <= midsize_alloc_zone_pages) &&
-         tmem_freeable_pages() )
-        goto try_tmem;
 
     /*
      * Start with requested node, but exhaust all node memory in requested 
@@ -764,17 +729,17 @@ static struct page_info *alloc_heap_pages(
         zone = zone_hi;
         do {
             /* Check if target node can support the allocation. */
-            if ( !avail[node] || (avail[node][zone] < request) )
+            if ( !avail[node] || (avail[node][zone] < (1UL << order)) )
                 continue;
 
             /* Find smallest order which can satisfy the request. */
             for ( j = order; j <= MAX_ORDER; j++ )
                 if ( (pg = page_list_remove_head(&heap(node, zone, j))) )
-                    goto found;
+                    return pg;
         } while ( zone-- > zone_lo ); /* careful: unsigned zone may wrap */
 
         if ( (memflags & MEMF_exact_node) && req_node != NUMA_NO_NODE )
-            goto not_found;
+            return NULL;
 
         /* Pick next node. */
         if ( !node_isset(node, nodemask) )
@@ -791,39 +756,89 @@ static struct page_info *alloc_heap_pages(
         {
             /* When we have tried all in nodemask, we fall back to others. */
             if ( (memflags & MEMF_exact_node) || nodemask_retry++ )
-                goto not_found;
+                return NULL;
             nodes_andnot(nodemask, node_online_map, nodemask);
             first_node = node = first_node(nodemask);
             if ( node >= MAX_NUMNODES )
-                goto not_found;
+                return NULL;
         }
     }
+}
 
- try_tmem:
-    /* Try to free memory from tmem */
-    if ( (pg = tmem_relinquish_pages(order, memflags)) != NULL )
+/* Allocate 2^@order contiguous pages. */
+static struct page_info *alloc_heap_pages(
+    unsigned int zone_lo, unsigned int zone_hi,
+    unsigned int order, unsigned int memflags,
+    struct domain *d)
+{
+    nodeid_t node;
+    unsigned int i, buddy_order, zone;
+    unsigned long request = 1UL << order;
+    struct page_info *pg, *first_dirty_pg = NULL;
+    bool_t need_tlbflush = 0;
+    uint32_t tlbflush_timestamp = 0;
+
+    /* Make sure there are enough bits in memflags for nodeID. */
+    BUILD_BUG_ON((_MEMF_bits - _MEMF_node) < (8 * sizeof(nodeid_t)));
+
+    ASSERT(zone_lo <= zone_hi);
+    ASSERT(zone_hi < NR_ZONES);
+
+    if ( unlikely(order > MAX_ORDER) )
+        return NULL;
+
+    spin_lock(&heap_lock);
+
+    /*
+     * Claimed memory is considered unavailable unless the request
+     * is made by a domain with sufficient unclaimed pages.
+     */
+    if ( (outstanding_claims + request >
+          total_avail_pages + tmem_freeable_pages()) &&
+          ((memflags & MEMF_no_refcount) ||
+           !d || d->outstanding_pages < request) )
     {
-        /* reassigning an already allocated anonymous heap page */
         spin_unlock(&heap_lock);
-        return pg;
+        return NULL;
+    }
+ 
+    /*
+     * TMEM: When available memory is scarce due to tmem absorbing it, allow
+     * only mid-size allocations to avoid worst of fragmentation issues.
+     * Others try tmem pools then fail.  This is a workaround until all
+     * post-dom0-creation-multi-page allocations can be eliminated.
+     */
+    if ( ((order == 0) || (order >= 9)) &&
+         (total_avail_pages <= midsize_alloc_zone_pages) &&
+         tmem_freeable_pages() )
+     {
+        /* Try to free memory from tmem. */
+         pg = tmem_relinquish_pages(order, memflags);
+         spin_unlock(&heap_lock);
+         return pg;
+     }
+ 
+    pg = get_free_buddy(zone_lo, zone_hi, order, memflags, d);
+    if ( !pg )
+    {
+        /* No suitable memory blocks. Fail the request. */
+        spin_unlock(&heap_lock);
+        return NULL;
     }
 
- not_found:
-    /* No suitable memory blocks. Fail the request. */
-    spin_unlock(&heap_lock);
-    return NULL;
-
- found: 
+    node = phys_to_nid(page_to_maddr(pg));
+    zone = page_to_zone(pg);
+    buddy_order = PFN_ORDER(pg);
 
     if ( pg->u.free.first_dirty != INVALID_DIRTY_IDX )
         first_dirty_pg = pg + pg->u.free.first_dirty;
-
-    /* We may have to halve the chunk a number of times. */
-    while ( j != order )
+ 
+     /* We may have to halve the chunk a number of times. */
+    while ( buddy_order != order )
     {
         unsigned int first_dirty;
 
-        if ( first_dirty_pg && ((pg + (1 << j)) > first_dirty_pg) )
+        if ( first_dirty_pg && ((pg + (1 << buddy_order)) > first_dirty_pg) )
         {
             if ( pg < first_dirty_pg )
                 first_dirty = (first_dirty_pg - pg) / sizeof(*pg);
@@ -833,8 +848,8 @@ static struct page_info *alloc_heap_pages(
         else
             first_dirty = INVALID_DIRTY_IDX;
 
-        page_list_add_scrub(pg, node, zone, --j, first_dirty);
-        pg += 1 << j;
+        page_list_add_scrub(pg, node, zone, --buddy_order, first_dirty);
+        pg += 1 << buddy_order;
     }
 
     ASSERT(avail[node][zone] >= request);
Signature · 7 lines
1.8.3.1


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
Acked-by on v5 2/8
#9 lore raw

Re: [PATCH v5 2/8] mm: Extract allocation loop from alloc_heap_pages()

Quoted 5 lines
Quoted 1 line
Quoted 1 line

Boris Ostrovsky <[email protected]> 06/22/17 8:56 PM >>>

This will make code a bit more readable, especially with changes that will be introduced in subsequent patches.

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

#10 lore raw

PATCH v5 3/8 mm: Scrub pages in alloc_heap_pages() if needed

In reply to Boris Ostrovsky

When allocating pages in alloc_heap_pages() first look for clean pages. If none is found then retry, take pages marked as unscrubbed and scrub them.

Note that we shouldn't find unscrubbed pages in alloc_heap_pages() yet. However, this will become possible when we stop scrubbing from free_heap_pages() and instead do it from idle loop.

Since not all allocations require clean pages (such as xenheap allocations) introduce MEMF_no_scrub flag that callers can set if they are willing to consume unscrubbed pages.

---
Changes in v5:
* Added comment explaining why we always grab order 0 pages in alloc_heap_pages)
* Dropped the somewhat confusing comment about not needing to set first_dirty in alloc_heap_pages().
* Moved first bit of _MEMF_node by 8 to accommodate MEMF_no_scrub (bit 7 is no longer available)

 xen/common/page_alloc.c | 36 +++++++++++++++++++++++++++++++-----
 xen/include/xen/mm.h    |  4 +++-
 2 files changed, 34 insertions(+), 6 deletions(-)
xen/common/page_alloc.c +31 -5
index 89fe3ce..9aac196 100644
@@ -703,6 +703,7 @@ static struct page_info *get_free_buddy(unsigned int zone_lo,
     nodemask_t nodemask = d ? d->node_affinity : node_online_map;
     unsigned int j, zone, nodemask_retry = 0;
     struct page_info *pg;
+    bool use_unscrubbed = (memflags & MEMF_no_scrub);
 
     if ( node == NUMA_NO_NODE )
     {
@@ -734,8 +735,20 @@ static struct page_info *get_free_buddy(unsigned int zone_lo,
 
             /* Find smallest order which can satisfy the request. */
             for ( j = order; j <= MAX_ORDER; j++ )
+            {
                 if ( (pg = page_list_remove_head(&heap(node, zone, j))) )
-                    return pg;
+                {
+                    /*
+                     * We grab single pages (order=0) even if they are
+                     * unscrubbed. Given that scrubbing one page is fairly quick
+                     * it is not worth breaking higher orders.
+                     */
+                    if ( (order == 0) || use_unscrubbed ||
+                         pg->u.free.first_dirty == INVALID_DIRTY_IDX)
+                        return pg;
+                    page_list_add_tail(pg, &heap(node, zone, j));
+                }
+            }
         } while ( zone-- > zone_lo ); /* careful: unsigned zone may wrap */
 
         if ( (memflags & MEMF_exact_node) && req_node != NUMA_NO_NODE )
@@ -775,7 +788,7 @@ static struct page_info *alloc_heap_pages(
     unsigned int i, buddy_order, zone;
     unsigned long request = 1UL << order;
     struct page_info *pg, *first_dirty_pg = NULL;
-    bool_t need_tlbflush = 0;
+    bool need_scrub, need_tlbflush = false;
     uint32_t tlbflush_timestamp = 0;
 
     /* Make sure there are enough bits in memflags for nodeID. */
@@ -819,6 +832,10 @@ static struct page_info *alloc_heap_pages(
      }
  
     pg = get_free_buddy(zone_lo, zone_hi, order, memflags, d);
+    /* Try getting a dirty buddy if we couldn't get a clean one. */
+    if ( !pg && !(memflags & MEMF_no_scrub) )
+        pg = get_free_buddy(zone_lo, zone_hi, order,
+                            memflags | MEMF_no_scrub, d);
     if ( !pg )
     {
         /* No suitable memory blocks. Fail the request. */
@@ -862,10 +879,19 @@ static struct page_info *alloc_heap_pages(
     if ( d != NULL )
         d->last_alloc_node = node;
 
+    need_scrub = !!first_dirty_pg && !(memflags & MEMF_no_scrub);
     for ( i = 0; i < (1 << order); i++ )
     {
         /* Reference count must continuously be zero for free pages. */
-        BUG_ON(pg[i].count_info != PGC_state_free);
+        BUG_ON((pg[i].count_info & ~PGC_need_scrub) != PGC_state_free);
+
+        if ( test_bit(_PGC_need_scrub, &pg[i].count_info) )
+        {
+            if ( need_scrub )
+                scrub_one_page(&pg[i]);
+            node_need_scrub[node]--;
+        }
+
         pg[i].count_info = PGC_state_inuse;
 
         if ( !(memflags & MEMF_no_tlbflush) )
@@ -1749,7 +1775,7 @@ void *alloc_xenheap_pages(unsigned int order, unsigned int memflags)
     ASSERT(!in_irq());
 
     pg = alloc_heap_pages(MEMZONE_XEN, MEMZONE_XEN,
-                          order, memflags, NULL);
+                          order, memflags | MEMF_no_scrub, NULL);
     if ( unlikely(pg == NULL) )
         return NULL;
 
@@ -1799,7 +1825,7 @@ void *alloc_xenheap_pages(unsigned int order, unsigned int memflags)
     if ( !(memflags >> _MEMF_bits) )
         memflags |= MEMF_bits(xenheap_bits);
 
-    pg = alloc_domheap_pages(NULL, order, memflags);
+    pg = alloc_domheap_pages(NULL, order, memflags | MEMF_no_scrub);
     if ( unlikely(pg == NULL) )
         return NULL;
 
xen/include/xen/mm.h +3 -1
index 3d3f31b..5f3d84a 100644
@@ -238,7 +238,9 @@ struct npfec {
 #define  MEMF_no_tlbflush (1U<<_MEMF_no_tlbflush)
 #define _MEMF_no_icache_flush 7
 #define  MEMF_no_icache_flush (1U<<_MEMF_no_icache_flush)
-#define _MEMF_node        8
+#define _MEMF_no_scrub    8
+#define  MEMF_no_scrub    (1U<<_MEMF_no_scrub)
+#define _MEMF_node        16
 #define  MEMF_node_mask   ((1U << (8 * sizeof(nodeid_t))) - 1)
 #define  MEMF_node(n)     ((((n) + 1) & MEMF_node_mask) << _MEMF_node)
 #define  MEMF_get_node(f) ((((f) >> _MEMF_node) - 1) & MEMF_node_mask)
Signature · 7 lines
1.8.3.1


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
Reviewed-by on v5 3/8
#11 lore raw

Re: [PATCH v5 3/8] mm: Scrub pages in alloc_heap_pages() if needed

xen/common/page_alloc.c static struct page_info *alloc_heap_pages( · line 882

Quoted 6 lines
Quoted 1 line
Quoted 1 line

Boris Ostrovsky <[email protected]> 06/22/17 8:55 PM >>>

xen/common/page_alloc.c +1 -0
@@ -862,10 +879,19 @@ static struct page_info *alloc_heap_pages(
     if ( d != NULL )
         d->last_alloc_node = node;
 
+    need_scrub = !!first_dirty_pg && !(memflags & MEMF_no_scrub);

No need for !! here. But I wonder whether that part of the check is really useful anyway, considering the sole use ...

xen/common/page_alloc.c static struct page_info *alloc_heap_pages( · line 891

Quoted 10 lines
xen/common/page_alloc.c +6 -1
     for ( i = 0; i < (1 << order); i++ )
     {
         /* Reference count must continuously be zero for free pages. */
-        BUG_ON(pg[i].count_info != PGC_state_free);
+        BUG_ON((pg[i].count_info & ~PGC_need_scrub) != PGC_state_free);
+
+        if ( test_bit(_PGC_need_scrub, &pg[i].count_info) )
+        {
+            if ( need_scrub )
+                scrub_one_page(&pg[i]);

... here. If it isn't, I think the local variable isn't warranted either. If you agree, the thus adjusted patch can have

(otherwise I'll wait with it to understand the reason first).

Jan

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

#12 lore raw

Re: [PATCH v5 3/8] mm: Scrub pages in alloc_heap_pages() if needed

Quoted 21 lines · Jan Beulich

xen/common/page_alloc.c static struct page_info *alloc_heap_pages( · line 882

Quoted 6 lines
Quoted 1 line
Quoted 1 line

Boris Ostrovsky <[email protected]> 06/22/17 8:55 PM >>>

xen/common/page_alloc.c +1 -0
@@ -862,10 +879,19 @@ static struct page_info *alloc_heap_pages(
      if ( d != NULL )
          d->last_alloc_node = node;
 
+    need_scrub = !!first_dirty_pg && !(memflags & MEMF_no_scrub);

No need for !! here. But I wonder whether that part of the check is really useful anyway, considering the sole use ...

xen/common/page_alloc.c static struct page_info *alloc_heap_pages( · line 891

Quoted 10 lines
xen/common/page_alloc.c +6 -1
      for ( i = 0; i < (1 << order); i++ )
      {
          /* Reference count must continuously be zero for free pages. */
-        BUG_ON(pg[i].count_info != PGC_state_free);
+        BUG_ON((pg[i].count_info & ~PGC_need_scrub) != PGC_state_free);
+
+        if ( test_bit(_PGC_need_scrub, &pg[i].count_info) )
+        {
+            if ( need_scrub )
+                scrub_one_page(&pg[i]);
Quoted 4 lines

... here. If it isn't, I think the local variable isn't warranted either. If you agree, the thus adjusted patch can have

(otherwise I'll wait with it to understand the reason first).

first_dirty_pg is indeed unnecessary but I think local variable is useful to avoid ANDing memflags inside the loop on each iteration (unless you think compiler is smart enough to realize that memflags is not changing).

-boris

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

#13 lore raw

Re: [PATCH v5 3/8] mm: Scrub pages in alloc_heap_pages() if needed

Quoted 28 lines
Quoted 1 line
Quoted 1 line

Boris Ostrovsky <[email protected]> 07/23/17 4:07 AM >>>

Quoted 21 lines · Jan Beulich

xen/common/page_alloc.c static struct page_info *alloc_heap_pages( · line 882

Quoted 6 lines
Quoted 1 line
Quoted 1 line

Boris Ostrovsky <[email protected]> 06/22/17 8:55 PM >>>

xen/common/page_alloc.c +1 -0
@@ -862,10 +879,19 @@ static struct page_info *alloc_heap_pages(
      if ( d != NULL )
          d->last_alloc_node = node;
 
+    need_scrub = !!first_dirty_pg && !(memflags & MEMF_no_scrub);

No need for !! here. But I wonder whether that part of the check is really useful anyway, considering the sole use ...

xen/common/page_alloc.c static struct page_info *alloc_heap_pages( · line 891

Quoted 10 lines
xen/common/page_alloc.c +6 -1
      for ( i = 0; i < (1 << order); i++ )
      {
          /* Reference count must continuously be zero for free pages. */
-        BUG_ON(pg[i].count_info != PGC_state_free);
+        BUG_ON((pg[i].count_info & ~PGC_need_scrub) != PGC_state_free);
+
+        if ( test_bit(_PGC_need_scrub, &pg[i].count_info) )
+        {
+            if ( need_scrub )
+                scrub_one_page(&pg[i]);
Quoted 4 lines

... here. If it isn't, I think the local variable isn't warranted either. If you agree, the thus adjusted patch can have

(otherwise I'll wait with it to understand the reason first).

Quoted 4 lines

first_dirty_pg is indeed unnecessary but I think local variable is useful to avoid ANDing memflags inside the loop on each iteration (unless you think compiler is smart enough to realize that memflags is not changing).

I don't understand: At least on x86 I'd expect the compiler to use a single TEST if you used memflags inside the loop, whereas the local variable would likely be a single CMP inside the loop plus setup code outside of it.

Jan

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

#14 lore raw

Re: [PATCH v5 3/8] mm: Scrub pages in alloc_heap_pages() if needed

Quoted 25 lines · Jan Beulich
Quoted 25 lines
Quoted 1 line
Quoted 1 line

Boris Ostrovsky <[email protected]> 07/23/17 4:07 AM >>>

Quoted 23 lines · Jan Beulich

xen/common/page_alloc.c static struct page_info *alloc_heap_pages( · line 882

Quoted 6 lines
Quoted 1 line
Quoted 1 line

Boris Ostrovsky <[email protected]> 06/22/17 8:55 PM >>>

xen/common/page_alloc.c +1 -0
@@ -862,10 +879,19 @@ static struct page_info *alloc_heap_pages(
      if ( d != NULL )
          d->last_alloc_node = node;
 
+    need_scrub = !!first_dirty_pg && !(memflags & MEMF_no_scrub);

No need for !! here. But I wonder whether that part of the check is really useful anyway, considering the sole use ...

xen/common/page_alloc.c static struct page_info *alloc_heap_pages( · line 891

Quoted 10 lines
xen/common/page_alloc.c +6 -1
      for ( i = 0; i < (1 << order); i++ )
      {
          /* Reference count must continuously be zero for free pages. */
-        BUG_ON(pg[i].count_info != PGC_state_free);
+        BUG_ON((pg[i].count_info & ~PGC_need_scrub) != PGC_state_free);
+
+        if ( test_bit(_PGC_need_scrub, &pg[i].count_info) )
+        {
+            if ( need_scrub )
+                scrub_one_page(&pg[i]);

... here. If it isn't, I think the local variable isn't warranted either. If you agree, the thus adjusted patch can have

(otherwise I'll wait with it to understand the reason first).

Quoted 8 lines
Quoted 4 lines

first_dirty_pg is indeed unnecessary but I think local variable is useful to avoid ANDing memflags inside the loop on each iteration (unless you think compiler is smart enough to realize that memflags is not changing).

I don't understand: At least on x86 I'd expect the compiler to use a single TEST if you used memflags inside the loop, whereas the local variable would likely be a single CMP inside the loop plus setup code outside of it.

OK, I haven't considered that you don't actually need to AND and then CMP. Then yes, local variable is unnecessary.

-boris

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

#15 lore raw

PATCH v5 4/8 mm: Scrub memory from idle loop

In reply to Boris Ostrovsky

Instead of scrubbing pages during guest destruction (from free_heap_pages()) do this opportunistically, from the idle loop.

We might come to scrub_free_pages()from idle loop while another CPU uses mapcache override, resulting in a fault while trying to do __map_domain_page() in scrub_one_page(). To avoid this, make mapcache vcpu override a per-cpu variable.

---
CC: Dario Faggioli <[email protected]>
---
Changes in v5:
* Added explanation in commit message for making mapcache override VCPU a per-cpu variable
* Fixed loop counting in scrub_free_pages()
* Fixed the off-by-one error in setting first_dirty in scrub_free_pages().
* Various style fixes
* Added a comment in node_to_scrub() explaining why it should be OK to prevent another CPU from scrubbing a node that ths current CPU temporarily claimed. (I decided against using locks there)

 xen/arch/arm/domain.c      |   2 +-
 xen/arch/x86/domain.c      |   2 +-
 xen/arch/x86/domain_page.c |   6 +--
 xen/common/page_alloc.c    | 118 ++++++++++++++++++++++++++++++++++++++++-----
 xen/include/xen/mm.h       |   1 +
 5 files changed, 111 insertions(+), 18 deletions(-)
xen/arch/arm/domain.c +1 -1
index 2dc8b0a..d282cd8 100644
@@ -51,7 +51,7 @@ void idle_loop(void)
         /* Are we here for running vcpu context tasklets, or for idling? */
         if ( unlikely(tasklet_work_to_do(cpu)) )
             do_tasklet();
-        else
+        else if ( !softirq_pending(cpu) && !scrub_free_pages() )
         {
             local_irq_disable();
             if ( cpu_is_haltable(cpu) )
xen/arch/x86/domain.c +1 -1
index f7873da..71f1ef4 100644
@@ -122,7 +122,7 @@ static void idle_loop(void)
         /* Are we here for running vcpu context tasklets, or for idling? */
         if ( unlikely(tasklet_work_to_do(cpu)) )
             do_tasklet();
-        else
+        else if ( !softirq_pending(cpu) && !scrub_free_pages() )
             pm_idle();
         do_softirq();
         /*
xen/arch/x86/domain_page.c +3 -3
index 71baede..0783c1e 100644
@@ -18,12 +18,12 @@
 #include <asm/hardirq.h>
 #include <asm/setup.h>
 
-static struct vcpu *__read_mostly override;
+static DEFINE_PER_CPU(struct vcpu *, override);
 
 static inline struct vcpu *mapcache_current_vcpu(void)
 {
     /* In the common case we use the mapcache of the running VCPU. */
-    struct vcpu *v = override ?: current;
+    struct vcpu *v = this_cpu(override) ?: current;
 
     /*
      * When current isn't properly set up yet, this is equivalent to
@@ -59,7 +59,7 @@ static inline struct vcpu *mapcache_current_vcpu(void)
 
 void __init mapcache_override_current(struct vcpu *v)
 {
-    override = v;
+    this_cpu(override) = v;
 }
 
 #define mapcache_l2_entry(e) ((e) >> PAGETABLE_ORDER)
xen/common/page_alloc.c +105 -13

146 lines not on this page · raw message

xen/include/xen/mm.h +1 -0
index 5f3d84a..a9829c2 100644
@@ -150,6 +150,7 @@ void init_xenheap_pages(paddr_t ps, paddr_t pe);
 void xenheap_max_mfn(unsigned long mfn);
 void *alloc_xenheap_pages(unsigned int order, unsigned int memflags);
 void free_xenheap_pages(void *v, unsigned int order);
+bool scrub_free_pages(void);
 #define alloc_xenheap_page() (alloc_xenheap_pages(0,0))
 #define free_xenheap_page(v) (free_xenheap_pages(v,0))
 /* Map machine page range in Xen virtual address space. */
Signature · 7 lines
1.8.3.1


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
Reviewed-by on v5 4/8
#16 lore raw

Re: [PATCH v5 4/8] mm: Scrub memory from idle loop

Quoted 10 lines · Boris Ostrovsky

Instead of scrubbing pages during guest destruction (from free_heap_pages()) do this opportunistically, from the idle loop.

We might come to scrub_free_pages()from idle loop while another CPU uses mapcache override, resulting in a fault while trying to do __map_domain_page() in scrub_one_page(). To avoid this, make mapcache vcpu override a per-cpu variable.

Regards,
Dario

Signature · 4 lines
<<This happens because I choose it to happen!>> (Raistlin Majere)
-----------------------------------------------------------------
Dario Faggioli, Ph.D, http://about.me/dario.faggioli
Senior Software Engineer, Citrix Systems R&D Ltd., Cambridge (UK)

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

#17 lore raw

Re: [PATCH v5 4/8] mm: Scrub memory from idle loop

In reply to Boris Ostrovsky
Quoted 48 lines
Quoted 1 line
Quoted 1 line

Boris Ostrovsky <[email protected]> 06/22/17 8:56 PM >>>

xen/common/page_alloc.c +32 -6
@@ -1019,15 +1019,85 @@ static int reserve_offlined_page(struct page_info *head)
     return count;
 }
 
-static void scrub_free_pages(unsigned int node)
+static nodemask_t node_scrubbing;
+
+/*
+ * If get_node is true this will return closest node that needs to be scrubbed,
+ * with appropriate bit in node_scrubbing set.
+ * If get_node is not set, this will return *a* node that needs to be scrubbed.
+ * node_scrubbing bitmask will no be updated.
+ * If no node needs scrubbing then NUMA_NO_NODE is returned.
+ */
+static unsigned int node_to_scrub(bool get_node)
 {
-    struct page_info *pg;
-    unsigned int zone;
+    nodeid_t node = cpu_to_node(smp_processor_id()), local_node;
+    nodeid_t closest = NUMA_NO_NODE;
+    u8 dist, shortest = 0xff;
 
-    ASSERT(spin_is_locked(&heap_lock));
+    if ( node == NUMA_NO_NODE )
+        node = 0;
 
-    if ( !node_need_scrub[node] )
-        return;
+    if ( node_need_scrub[node] &&
+         (!get_node || !node_test_and_set(node, node_scrubbing)) )
+        return node;
+
+    /*
+     * See if there are memory-only nodes that need scrubbing and choose
+     * the closest one.
+     */
+    local_node = node;
+    for ( ; ; )
+    {
+        do {
+            node = cycle_node(node, node_online_map);
+        } while ( !cpumask_empty(&node_to_cpumask(node)) &&
+                  (node != local_node) );
+
+        if ( node == local_node )

xen/common/page_alloc.c static int reserve_offlined_page(struct page_info *head) · line 1066

Quoted 10 lines
xen/common/page_alloc.c +10 -0
+            break;
+
+        /*
+         * Grab the node right away. If we find a closer node later we will
+         * release this one. While there is a chance that another CPU will
+         * not be able to scrub that node when it is searching for scrub work
+         * at the same time it will be able to do so next time it wakes up.
+         * The alternative would be to perform this search under a lock but
+         * then we'd need to take this lock every time we come in here.
+         */

I'm fine with your choice of simply explaining your decision, but ...

xen/common/page_alloc.c static int reserve_offlined_page(struct page_info *head) · line 1074

Quoted 8 lines
xen/common/page_alloc.c +8 -0
+        if ( node_need_scrub[node] )
+        {
+            if ( !get_node )
+                return node;
+
+            dist = __node_distance(local_node, node);
+            if ( (dist < shortest || closest == NUMA_NO_NODE) &&
+                 !node_test_and_set(node, node_scrubbing) )

... wouldn't the comment better be placed ahead of this if()?

Quoted 34 lines
xen/common/page_alloc.c +22 -4
@@ -1050,17 +1120,42 @@ static void scrub_free_pages(unsigned int node)
                         scrub_one_page(&pg[i]);
                         pg[i].count_info &= ~PGC_need_scrub;
                         node_need_scrub[node]--;
+                        cnt += 100; /* scrubbed pages add heavier weight. */
+                    }
+                    else
+                        cnt++;
+
+                    /*
+                     * Scrub a few (8) pages before becoming eligible for
+                     * preemption. But also count non-scrubbing loop iterations
+                     * so that we don't get stuck here with an almost clean
+                     * heap.
+                     */
+                    if ( cnt > 800 && softirq_pending(cpu) )
+                    {
+                        preempt = true;
+                        break;
                     }
                 }
 
-                page_list_del(pg, &heap(node, zone, order));
-                page_list_add_scrub(pg, node, zone, order, INVALID_DIRTY_IDX);
+                if ( i >= (1U << order) - 1 )
+                {
+                    page_list_del(pg, &heap(node, zone, order));
+                    page_list_add_scrub(pg, node, zone, order, INVALID_DIRTY_IDX);
+                }
+                else
+                    pg->u.free.first_dirty = i + 1;
 
-                if ( node_need_scrub[node] == 0 )
-                    return;

xen/common/page_alloc.c static void scrub_free_pages(unsigned int node) · line 1158

Quoted 10 lines
xen/common/page_alloc.c +7 -0
+                if ( preempt || (node_need_scrub[node] == 0) )
+                    goto out;
             }
         } while ( order-- != 0 );
     }
+
+ out:
+    spin_unlock(&heap_lock);
+    node_clear(node, node_scrubbing);
+    return softirq_pending(cpu) || (node_to_scrub(false) != NUMA_NO_NODE);

While I can see why you use it here, the softirq_pending() looks sort of
misplaced: While invoking it twice in the caller will look a little odd too, I still think that's where the check belongs.

Jan

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

#18 lore raw

Re: [PATCH v5 4/8] mm: Scrub memory from idle loop

Quoted 34 lines
Quoted 34 lines
xen/common/page_alloc.c +22 -2
@@ -1050,17 +1120,42 @@ static void scrub_free_pages(unsigned int node)
                          scrub_one_page(&pg[i]);
                          pg[i].count_info &= ~PGC_need_scrub;
                          node_need_scrub[node]--;
+                        cnt += 100; /* scrubbed pages add heavier weight. */
+                    }
+                    else
+                        cnt++;
+
+                    /*
+                     * Scrub a few (8) pages before becoming eligible for
+                     * preemption. But also count non-scrubbing loop iterations
+                     * so that we don't get stuck here with an almost clean
+                     * heap.
+                     */
+                    if ( cnt > 800 && softirq_pending(cpu) )
+                    {
+                        preempt = true;
+                        break;
                      }
                  }
 
-                page_list_del(pg, &heap(node, zone, order));
-                page_list_add_scrub(pg, node, zone, order, INVALID_DIRTY_IDX);
+                if ( i >= (1U << order) - 1 )
+                {
+                    page_list_del(pg, &heap(node, zone, order));
+                    page_list_add_scrub(pg, node, zone, order, INVALID_DIRTY_IDX);
+                }
+                else
+                    pg->u.free.first_dirty = i + 1;
 
Quoted 16 lines

xen/common/page_alloc.c static void scrub_free_pages(unsigned int node) · line 1158

Quoted 12 lines
xen/common/page_alloc.c +7 -2
-                if ( node_need_scrub[node] == 0 )
-                    return;
+                if ( preempt || (node_need_scrub[node] == 0) )
+                    goto out;
              }
          } while ( order-- != 0 );
      }
+
+ out:
+    spin_unlock(&heap_lock);
+    node_clear(node, node_scrubbing);
+    return softirq_pending(cpu) || (node_to_scrub(false) != NUMA_NO_NODE);

While I can see why you use it here, the softirq_pending() looks sort of
misplaced: While invoking it twice in the caller will look a little odd too, I still think that's where the check belongs.

scrub_free_pages is called from idle loop as

	else if ( !softirq_pending(cpu) && !scrub_free_pages() )
            pm_idle();

so softirq_pending() is unnecessary here.

(Not sure why you are saying it would be invoked twice)

-boris

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

#19 lore raw

Re: [PATCH v5 4/8] mm: Scrub memory from idle loop

Quoted 20 lines
Quoted 10 lines
Quoted 10 lines

Boris Ostrovsky <[email protected]> 07/23/17 4:14 AM >>>

xen/common/page_alloc.c +3 -2
@@ -1050,17 +1120,42 @@ static void scrub_free_pages(unsigned int node)
-                if ( node_need_scrub[node] == 0 )
-                    return;
+                if ( preempt || (node_need_scrub[node] == 0) )
+                    goto out;
              }
          } while ( order-- != 0 );
      }
+
Quoted 8 lines

xen/common/page_alloc.c static void scrub_free_pages(unsigned int node) · line 1158

Quoted 4 lines
xen/common/page_alloc.c +4 -0
+ out:
+    spin_unlock(&heap_lock);
+    node_clear(node, node_scrubbing);
+    return softirq_pending(cpu) || (node_to_scrub(false) != NUMA_NO_NODE);

While I can see why you use it here, the softirq_pending() looks sort of
misplaced: While invoking it twice in the caller will look a little odd too, I still think that's where the check belongs.

Quoted 8 lines

scrub_free_pages is called from idle loop as

else if ( !softirq_pending(cpu) && !scrub_free_pages() ) pm_idle();

so softirq_pending() is unnecessary here.

(Not sure why you are saying it would be invoked twice)

That was sort of implicit - the caller would want to become

    else if ( !softirq_pending(cpu) && !scrub_free_pages() && !softirq_pending(cpu) )
    pm_idle();

to account for the fact that a softirq may become pending while scrubbing.

Jan

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

#20 lore raw

Re: [PATCH v5 4/8] mm: Scrub memory from idle loop

Quoted 25 lines · Jan Beulich
Quoted 25 lines
Quoted 9 lines
Quoted 9 lines

Boris Ostrovsky <[email protected]> 07/23/17 4:14 AM >>>

xen/common/page_alloc.c +2 -2
@@ -1050,17 +1120,42 @@ static void scrub_free_pages(unsigned int node)
-                if ( node_need_scrub[node] == 0 )
-                    return;
+                if ( preempt || (node_need_scrub[node] == 0) )
+                    goto out;
              }
          } while ( order-- != 0 );
      }
Quoted 8 lines

xen/common/page_alloc.c static void scrub_free_pages(unsigned int node) · line 1158

Quoted 5 lines
xen/common/page_alloc.c +5 -0
+
+ out:
+    spin_unlock(&heap_lock);
+    node_clear(node, node_scrubbing);
+    return softirq_pending(cpu) || (node_to_scrub(false) != NUMA_NO_NODE);

While I can see why you use it here, the softirq_pending() looks sort of
misplaced: While invoking it twice in the caller will look a little odd too, I still think that's where the check belongs.

scrub_free_pages is called from idle loop as

else if ( !softirq_pending(cpu) && !scrub_free_pages() ) pm_idle();

so softirq_pending() is unnecessary here.

Quoted 8 lines
Quoted 1 line

(Not sure why you are saying it would be invoked twice)

That was sort of implicit - the caller would want to become

    else if ( !softirq_pending(cpu) && !scrub_free_pages() && !softirq_pending(cpu) )
    pm_idle();

to account for the fact that a softirq may become pending while scrubbing.

That would look really odd IMO.

Would

else if ( !softirq_pending(cpu) )

    if ( !scrub_free_pages() && !softirq_pending(cpu) )
       pm_idle();

or

else if ( !softirq_pending(cpu) && !scrub_free_pages() )
if ( !softirq_pending(cpu) )
pm_idle();

be better? (I'd prefer the first)

-boris

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

#21 lore raw

Re: [PATCH v5 4/8] mm: Scrub memory from idle loop

Quoted 44 lines
Quoted 1 line
Quoted 1 line

Boris Ostrovsky <[email protected]> 07/31/17 6:16 PM >>>

Quoted 25 lines · Jan Beulich
Quoted 25 lines
Quoted 9 lines
Quoted 9 lines

Boris Ostrovsky <[email protected]> 07/23/17 4:14 AM >>>

xen/common/page_alloc.c +2 -2
@@ -1050,17 +1120,42 @@ static void scrub_free_pages(unsigned int node)
-                if ( node_need_scrub[node] == 0 )
-                    return;
+                if ( preempt || (node_need_scrub[node] == 0) )
+                    goto out;
              }
          } while ( order-- != 0 );
      }
Quoted 8 lines

xen/common/page_alloc.c static void scrub_free_pages(unsigned int node) · line 1158

Quoted 5 lines
xen/common/page_alloc.c +5 -0
+
+ out:
+    spin_unlock(&heap_lock);
+    node_clear(node, node_scrubbing);
+    return softirq_pending(cpu) || (node_to_scrub(false) != NUMA_NO_NODE);

While I can see why you use it here, the softirq_pending() looks sort of
misplaced: While invoking it twice in the caller will look a little odd too, I still think that's where the check belongs.

scrub_free_pages is called from idle loop as

else if ( !softirq_pending(cpu) && !scrub_free_pages() ) pm_idle();

so softirq_pending() is unnecessary here.

Quoted 8 lines
Quoted 1 line

(Not sure why you are saying it would be invoked twice)

That was sort of implicit - the caller would want to become

    else if ( !softirq_pending(cpu) && !scrub_free_pages() && !softirq_pending(cpu) )
    pm_idle();

to account for the fact that a softirq may become pending while scrubbing.

That would look really odd IMO.

Would

else if ( !softirq_pending(cpu) )
if ( !scrub_free_pages() && !softirq_pending(cpu) ) pm_idle();

Quoted 8 lines

or

else if ( !softirq_pending(cpu) && !scrub_free_pages() ) if ( !softirq_pending(cpu) )
pm_idle();

be better? (I'd prefer the first)

I dislike both (as we always as people to fold such chained if()s), and hence would prefer my variant plus a suitable comment explaining the oddity.

Jan

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

#22 lore raw

PATCH v5 5/8 spinlock: Introduce spin_lock_cb()

In reply to Boris Ostrovsky

While waiting for a lock we may want to periodically run some code. This code may, for example, allow the caller to release resources held by it that are no longer needed in the critical section protected by the lock.

Specifically, this feature will be needed by scrubbing code where the scrubber, while waiting for heap lock to merge back clean pages, may be requested by page allocator (which is currently holding the lock) to abort merging and release the buddy page head that the allocator wants.

We could use spin_trylock() but since it doesn't take lock ticket it may take long time until the lock is taken. Instead we add spin_lock_cb() that allows us to grab the ticket and execute a callback while waiting. This callback is executed on every iteration of the spinlock waiting loop.

Since we may be sleeping in the lock until it is released we need a mechanism that will make sure that the callback has a chance to run. We add spin_lock_kick() that will wake up the waiter.

---
Changes in v5:
* Added a sentence in commit message to note that callback function is called on every iteration of the spin loop.

 xen/common/spinlock.c      | 9 ++++++++-
 xen/include/xen/spinlock.h | 8 ++++++++
 2 files changed, 16 insertions(+), 1 deletion(-)
xen/common/spinlock.c +8 -1
index 2a06406..3c1caae 100644
@@ -129,7 +129,7 @@ static always_inline u16 observe_head(spinlock_tickets_t *t)
     return read_atomic(&t->head);
 }
 
-void _spin_lock(spinlock_t *lock)
+void inline _spin_lock_cb(spinlock_t *lock, void (*cb)(void *), void *data)
 {
     spinlock_tickets_t tickets = SPINLOCK_TICKET_INC;
     LOCK_PROFILE_VAR;
@@ -140,6 +140,8 @@ void _spin_lock(spinlock_t *lock)
     while ( tickets.tail != observe_head(&lock->tickets) )
     {
         LOCK_PROFILE_BLOCK;
+        if ( unlikely(cb) )
+            cb(data);
         arch_lock_relax();
     }
     LOCK_PROFILE_GOT;
@@ -147,6 +149,11 @@ void _spin_lock(spinlock_t *lock)
     arch_lock_acquire_barrier();
 }
 
+void _spin_lock(spinlock_t *lock)
+{
+     _spin_lock_cb(lock, NULL, NULL);
+}
+
 void _spin_lock_irq(spinlock_t *lock)
 {
     ASSERT(local_irq_is_enabled());
xen/include/xen/spinlock.h +8 -0
index c1883bd..91bfb95 100644
@@ -153,6 +153,7 @@ typedef struct spinlock {
 #define spin_lock_init(l) (*(l) = (spinlock_t)SPIN_LOCK_UNLOCKED)
 
 void _spin_lock(spinlock_t *lock);
+void _spin_lock_cb(spinlock_t *lock, void (*cond)(void *), void *data);
 void _spin_lock_irq(spinlock_t *lock);
 unsigned long _spin_lock_irqsave(spinlock_t *lock);
 
@@ -169,6 +170,7 @@ void _spin_lock_recursive(spinlock_t *lock);
 void _spin_unlock_recursive(spinlock_t *lock);
 
 #define spin_lock(l)                  _spin_lock(l)
+#define spin_lock_cb(l, c, d)         _spin_lock_cb(l, c, d)
 #define spin_lock_irq(l)              _spin_lock_irq(l)
 #define spin_lock_irqsave(l, f)                                 \
     ({                                                          \
@@ -190,6 +192,12 @@ void _spin_unlock_recursive(spinlock_t *lock);
     1 : ({ local_irq_restore(flags); 0; });     \
 })
 
+#define spin_lock_kick(l)                       \
+({                                              \
+    smp_mb();                                   \
+    arch_lock_signal();                         \
+})
+
 /* Ensure a lock is quiescent between two critical operations. */
 #define spin_barrier(l)               _spin_barrier(l)
 
Signature · 7 lines
1.8.3.1


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

PATCH v5 6/8 mm: Keep heap accessible to others while scrubbing

In reply to Boris Ostrovsky

Instead of scrubbing pages while holding heap lock we can mark buddy's head as being scrubbed and drop the lock temporarily. If someone (most likely alloc_heap_pages()) tries to access this chunk it will signal the scrubber to abort scrub by setting head's BUDDY_SCRUB_ABORT bit. The scrubber checks this bit after processing each page and stops its work as soon as it sees it.

---
Changes in v5:
* Fixed off-by-one error in setting first_dirty
* Changed struct page_info.u.free to a union to permit use of ACCESS_ONCE in check_and_stop_scrub()
* Renamed PAGE_SCRUBBING etc. macros to BUDDY_SCRUBBING etc

 xen/common/page_alloc.c  | 105 +++++++++++++++++++++++++++++++++++++++++++++--
 xen/include/asm-arm/mm.h |  28 ++++++++-----
 xen/include/asm-x86/mm.h |  29 ++++++++-----
 3 files changed, 138 insertions(+), 24 deletions(-)
xen/common/page_alloc.c +101 -4
index 4e2775f..f0e5399 100644
@@ -687,6 +687,7 @@ static void page_list_add_scrub(struct page_info *pg, unsigned int node,
 {
     PFN_ORDER(pg) = order;
     pg->u.free.first_dirty = first_dirty;
+    pg->u.free.scrub_state = BUDDY_NOT_SCRUBBING;
 
     if ( first_dirty != INVALID_DIRTY_IDX )
         page_list_add_tail(pg, &heap(node, zone, order));
@@ -694,6 +695,25 @@ static void page_list_add_scrub(struct page_info *pg, unsigned int node,
         page_list_add(pg, &heap(node, zone, order));
 }
 
+static void check_and_stop_scrub(struct page_info *head)
+{
+    if ( head->u.free.scrub_state == BUDDY_SCRUBBING )
+    {
+        struct page_info pg;
+
+        head->u.free.scrub_state = BUDDY_SCRUB_ABORT;
+        spin_lock_kick();
+        for ( ; ; )
+        {
+            /* Can't ACCESS_ONCE() a bitfield. */
+            pg.u.free.val = ACCESS_ONCE(head->u.free.val);
+            if ( pg.u.free.scrub_state != BUDDY_SCRUB_ABORT )
+                break;
+            cpu_relax();
+        }
+    }
+}
+
 static struct page_info *get_free_buddy(unsigned int zone_lo,
                                         unsigned int zone_hi,
                                         unsigned int order, unsigned int memflags,
@@ -738,14 +758,19 @@ static struct page_info *get_free_buddy(unsigned int zone_lo,
             {
                 if ( (pg = page_list_remove_head(&heap(node, zone, j))) )
                 {
+                    if ( pg->u.free.first_dirty == INVALID_DIRTY_IDX )
+                        return pg;
                     /*
                      * We grab single pages (order=0) even if they are
                      * unscrubbed. Given that scrubbing one page is fairly quick
                      * it is not worth breaking higher orders.
                      */
-                    if ( (order == 0) || use_unscrubbed ||
-                         pg->u.free.first_dirty == INVALID_DIRTY_IDX)
+                    if ( (order == 0) || use_unscrubbed )
+                    {
+                        check_and_stop_scrub(pg);
                         return pg;
+                    }
+
                     page_list_add_tail(pg, &heap(node, zone, j));
                 }
             }
@@ -928,6 +953,7 @@ static int reserve_offlined_page(struct page_info *head)
 
     cur_head = head;
 
+    check_and_stop_scrub(head);
     /*
      * We may break the buddy so let's mark the head as clean. Then, when
      * merging chunks back into the heap, we will see whether the chunk has
@@ -1084,6 +1110,29 @@ static unsigned int node_to_scrub(bool get_node)
     return closest;
 }
 
+struct scrub_wait_state {
+    struct page_info *pg;
+    unsigned int first_dirty;
+    bool drop;
+};
+
+static void scrub_continue(void *data)
+{
+    struct scrub_wait_state *st = data;
+
+    if ( st->drop )
+        return;
+
+    if ( st->pg->u.free.scrub_state == BUDDY_SCRUB_ABORT )
+    {
+        /* There is a waiter for this buddy. Release it. */
+        st->drop = true;
+        st->pg->u.free.first_dirty = st->first_dirty;
+        smp_wmb();
+        st->pg->u.free.scrub_state = BUDDY_NOT_SCRUBBING;
+    }
+}
+
 bool scrub_free_pages(void)
 {
     struct page_info *pg;
@@ -1106,25 +1155,53 @@ bool scrub_free_pages(void)
         do {
             while ( !page_list_empty(&heap(node, zone, order)) )
             {
-                unsigned int i;
+                unsigned int i, dirty_cnt;
+                struct scrub_wait_state st;
 
                 /* Unscrubbed pages are always at the end of the list. */
                 pg = page_list_last(&heap(node, zone, order));
                 if ( pg->u.free.first_dirty == INVALID_DIRTY_IDX )
                     break;
 
+                ASSERT(!pg->u.free.scrub_state);
+                pg->u.free.scrub_state = BUDDY_SCRUBBING;
+
+                spin_unlock(&heap_lock);
+
+                dirty_cnt = 0;
+
                 for ( i = pg->u.free.first_dirty; i < (1U << order); i++)
                 {
                     if ( test_bit(_PGC_need_scrub, &pg[i].count_info) )
                     {
                         scrub_one_page(&pg[i]);
+                        /*
+                         * We can modify count_info without holding heap
+                         * lock since we effectively locked this buddy by
+                         * setting its scrub_state.
+                         */
                         pg[i].count_info &= ~PGC_need_scrub;
-                        node_need_scrub[node]--;
+                        dirty_cnt++;
                         cnt += 100; /* scrubbed pages add heavier weight. */
                     }
                     else
                         cnt++;
 
+                    if ( pg->u.free.scrub_state == BUDDY_SCRUB_ABORT )
+                    {
+                        /* Someone wants this chunk. Drop everything. */
+
+                        pg->u.free.first_dirty = (i == (1U << order) - 1) ?
+                            INVALID_DIRTY_IDX : i + 1; 
+                        smp_wmb();
+                        pg->u.free.scrub_state = BUDDY_NOT_SCRUBBING;
+
+                        spin_lock(&heap_lock);
+                        node_need_scrub[node] -= dirty_cnt;
+                        spin_unlock(&heap_lock);
+                        goto out_nolock;
+                    }
+
                     /*
                      * Scrub a few (8) pages before becoming eligible for
                      * preemption. But also count non-scrubbing loop iterations
@@ -1138,6 +1215,17 @@ bool scrub_free_pages(void)
                     }
                 }
 
+                st.pg = pg;
+                st.first_dirty = (i >= (1UL << order) - 1) ?
+                    INVALID_DIRTY_IDX : i + 1;
+                st.drop = false;
+                spin_lock_cb(&heap_lock, scrub_continue, &st);
+
+                node_need_scrub[node] -= dirty_cnt;
+
+                if ( st.drop )
+                    goto out;
+
                 if ( i >= (1U << order) - 1 )
                 {
                     page_list_del(pg, &heap(node, zone, order));
@@ -1146,6 +1234,8 @@ bool scrub_free_pages(void)
                 else
                     pg->u.free.first_dirty = i + 1;
 
+                pg->u.free.scrub_state = BUDDY_NOT_SCRUBBING;
+
                 if ( preempt || (node_need_scrub[node] == 0) )
                     goto out;
             }
@@ -1154,6 +1244,8 @@ bool scrub_free_pages(void)
 
  out:
     spin_unlock(&heap_lock);
+
+ out_nolock:
     node_clear(node, node_scrubbing);
     return softirq_pending(cpu) || (node_to_scrub(false) != NUMA_NO_NODE);
 }
@@ -1235,6 +1327,8 @@ static void free_heap_pages(
                  (phys_to_nid(page_to_maddr(predecessor)) != node) )
                 break;
 
+            check_and_stop_scrub(predecessor);
+
             page_list_del(predecessor, &heap(node, zone, order));
 
             if ( predecessor->u.free.first_dirty != INVALID_DIRTY_IDX )
@@ -1256,6 +1350,9 @@ static void free_heap_pages(
                  (PFN_ORDER(successor) != order) ||
                  (phys_to_nid(page_to_maddr(successor)) != node) )
                 break;
+
+            check_and_stop_scrub(successor);
+
             page_list_del(successor, &heap(node, zone, order));
 
             if ( successor->u.free.first_dirty != INVALID_DIRTY_IDX )
xen/include/asm-arm/mm.h +18 -10
index 889a85e..625aa16 100644
@@ -42,18 +42,26 @@ struct page_info
             unsigned long type_info;
         } inuse;
         /* Page is on a free list: ((count_info & PGC_count_mask) == 0). */
-        struct {
-            /* Do TLBs need flushing for safety before next page use? */
-            unsigned long need_tlbflush:1;
-
-            /*
-             * Index of the first *possibly* unscrubbed page in the buddy.
-             * One more than maximum possible order (MAX_ORDER+1) to
-             * accommodate INVALID_DIRTY_IDX.
-             */
+        union {
+            struct {
+                /* Do TLBs need flushing for safety before next page use? */
+                unsigned long need_tlbflush:1;
+
+                /*
+                 * Index of the first *possibly* unscrubbed page in the buddy.
+                 * One more than maximum possible order (MAX_ORDER+1) to
+                 * accommodate INVALID_DIRTY_IDX.
+                 */
 #define INVALID_DIRTY_IDX (-1UL & (((1UL<<MAX_ORDER) + 2) - 1))
-            unsigned long first_dirty:MAX_ORDER + 2;
+                unsigned long first_dirty:MAX_ORDER + 2;
+
+#define BUDDY_NOT_SCRUBBING   0
+#define BUDDY_SCRUBBING       1
+#define BUDDY_SCRUB_ABORT     2
+                unsigned long scrub_state:2;
+            };
 
+           unsigned long val;
         } free;
 
     } u;
xen/include/asm-x86/mm.h +19 -10
index cd00bef..db6f3a5 100644
@@ -86,17 +86,26 @@ struct page_info
         } sh;
 
         /* Page is on a free list: ((count_info & PGC_count_mask) == 0). */
-        struct {
-            /* Do TLBs need flushing for safety before next page use? */
-            unsigned long need_tlbflush:1;
-
-            /*
-             * Index of the first *possibly* unscrubbed page in the buddy.
-             * One more than maximum possible order (MAX_ORDER+1) to
-             * accommodate INVALID_DIRTY_IDX.
-             */
+        union {
+	    struct {
+		/* Do TLBs need flushing for safety before next page use? */
+		unsigned long need_tlbflush:1;
+
+		/*
+		 * Index of the first *possibly* unscrubbed page in the buddy.
+		 * One more than maximum possible order (MAX_ORDER+1) to
+		 * accommodate INVALID_DIRTY_IDX.
+		 */
 #define INVALID_DIRTY_IDX (-1UL & (((1UL<<MAX_ORDER) + 2) - 1))
-            unsigned long first_dirty:MAX_ORDER + 2;
+		unsigned long first_dirty:MAX_ORDER + 2;
+
+#define BUDDY_NOT_SCRUBBING   0
+#define BUDDY_SCRUBBING       1
+#define BUDDY_SCRUB_ABORT     2
+		unsigned long scrub_state:2;
+	    };
+
+	    unsigned long val;
         } free;
 
     } u;
Signature · 7 lines
1.8.3.1


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

Re: [PATCH v5 6/8] mm: Keep heap accessible to others while scrubbing

Quoted 5 lines
Quoted 1 line
Quoted 1 line

Boris Ostrovsky <[email protected]> 06/22/17 8:56 PM >>>

Changes in v5:
* Fixed off-by-one error in setting first_dirty
* Changed struct page_info.u.free to a union to permit use of ACCESS_ONCE in check_and_stop_scrub()

I don't see the need for this:

xen/common/page_alloc.c static void page_list_add_scrub(struct page_info *pg, unsigned int node, · line 709

Quoted 12 lines
xen/common/page_alloc.c +12 -0
+static void check_and_stop_scrub(struct page_info *head)
+{
+    if ( head->u.free.scrub_state == BUDDY_SCRUBBING )
+    {
+        struct page_info pg;
+
+        head->u.free.scrub_state = BUDDY_SCRUB_ABORT;
+        spin_lock_kick();
+        for ( ; ; )
+        {
+            /* Can't ACCESS_ONCE() a bitfield. */
+            pg.u.free.val = ACCESS_ONCE(head->u.free.val);

Something like ACCESS_ONCE(head->u.free).val ought to work (or read_atomic(), due to the questionable scalar type check in ACCESS_ONCE()).

Quoted 4 lines
xen/common/page_alloc.c +0 -0
@@ -1106,25 +1155,53 @@ bool scrub_free_pages(void)
         do {
             while ( !page_list_empty(&heap(node, zone, order)) )
             {

xen/common/page_alloc.c bool scrub_free_pages(void) · line 1166

Quoted 10 lines
xen/common/page_alloc.c +3 -1
-                unsigned int i;
+                unsigned int i, dirty_cnt;
+                struct scrub_wait_state st;
 
                 /* Unscrubbed pages are always at the end of the list. */
                 pg = page_list_last(&heap(node, zone, order));
                 if ( pg->u.free.first_dirty == INVALID_DIRTY_IDX )
                     break;
 
+                ASSERT(!pg->u.free.scrub_state);

Please use BUDDY_NOT_SCRUBBING here.

xen/common/page_alloc.c bool scrub_free_pages(void) · line 1220

Quoted 7 lines
xen/common/page_alloc.c +3 -0
@@ -1138,6 +1215,17 @@ bool scrub_free_pages(void)
                     }
                 }
 
+                st.pg = pg;
+                st.first_dirty = (i >= (1UL << order) - 1) ?
+                    INVALID_DIRTY_IDX : i + 1;

Would you mind explaining to me (again?) why you can't set pg's first_dirty directly here? In case I'm not mistaken and this has been asked before, maybe this is a hint that a comment might be warranted.

Jan

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

#25 lore raw

Re: [PATCH v5 6/8] mm: Keep heap accessible to others while scrubbing

Quoted 7 lines

xen/common/page_alloc.c static void page_list_add_scrub(struct page_info *pg, unsigned int node, · line 709

Quoted 4 lines
Quoted 2 lines

"Jan Beulich" <[email protected]> 06/27/17 9:29 PM >>>

Quoted 1 line

Boris Ostrovsky <[email protected]> 06/22/17 8:56 PM >>>

xen/common/page_alloc.c +2 -0
+            /* Can't ACCESS_ONCE() a bitfield. */
+            pg.u.free.val = ACCESS_ONCE(head->u.free.val);

Something like ACCESS_ONCE(head->u.free).val ought to work (or read_atomic(), due to the questionable scalar type check in ACCESS_ONCE()).

And (of course) I did mean .scrub_state here instead of .val.

Jan

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

#26 lore raw

Re: [PATCH v5 6/8] mm: Keep heap accessible to others while scrubbing

In reply to Jan Beulich
Quoted 8 lines · Jan Beulich
Quoted 5 lines
Quoted 1 line
Quoted 1 line

Boris Ostrovsky <[email protected]> 06/22/17 8:56 PM >>>

Changes in v5:
* Fixed off-by-one error in setting first_dirty
* Changed struct page_info.u.free to a union to permit use of ACCESS_ONCE in check_and_stop_scrub()

I don't see the need for this:

Quoted 15 lines

xen/common/page_alloc.c static void page_list_add_scrub(struct page_info *pg, unsigned int node, · line 709

Quoted 12 lines
xen/common/page_alloc.c +12 -0
+static void check_and_stop_scrub(struct page_info *head)
+{
+    if ( head->u.free.scrub_state == BUDDY_SCRUBBING )
+    {
+        struct page_info pg;
+
+        head->u.free.scrub_state = BUDDY_SCRUB_ABORT;
+        spin_lock_kick();
+        for ( ; ; )
+        {
+            /* Can't ACCESS_ONCE() a bitfield. */
+            pg.u.free.val = ACCESS_ONCE(head->u.free.val);

Something like ACCESS_ONCE(head->u.free).val ought to work (or read_atomic(), due to the questionable scalar type check in ACCESS_ONCE()).

Hmm... I couldn't get this to work with either suggestion.

page_alloc.c:751:13: error: conversion to non-scalar type requested

             pg.u.free = read_atomic(&head->u.free);

page_alloc.c:753:6: error: conversion to non-scalar type requested

      if ( ACCESS_ONCE(head->u.free).scrub_state != BUDDY_SCRUB_ABORT )
Quoted 22 lines
Quoted 5 lines
xen/common/page_alloc.c +0 -0
@@ -1106,25 +1155,53 @@ bool scrub_free_pages(void)
          do {
              while ( !page_list_empty(&heap(node, zone, order)) )
              {

xen/common/page_alloc.c bool scrub_free_pages(void) · line 1166

Quoted 10 lines
xen/common/page_alloc.c +3 -1
-                unsigned int i;
+                unsigned int i, dirty_cnt;
+                struct scrub_wait_state st;
 
                  /* Unscrubbed pages are always at the end of the list. */
                  pg = page_list_last(&heap(node, zone, order));
                  if ( pg->u.free.first_dirty == INVALID_DIRTY_IDX )
                      break;
 
+                ASSERT(!pg->u.free.scrub_state);

Please use BUDDY_NOT_SCRUBBING here.

Quoted 4 lines
(quoted diff) +0 -0
@@ -1138,6 +1215,17 @@ bool scrub_free_pages(void)
                      }
                  }
 
Quoted 7 lines

xen/common/page_alloc.c bool scrub_free_pages(void) · line 1220

Quoted 3 lines
xen/common/page_alloc.c +3 -0
+                st.pg = pg;
+                st.first_dirty = (i >= (1UL << order) - 1) ?
+                    INVALID_DIRTY_IDX : i + 1;

Would you mind explaining to me (again?) why you can't set pg's first_dirty directly here? In case I'm not mistaken and this has been asked before, maybe this is a hint that a comment might be warranted.

In get_free_buddy() (formerly part of alloc_heap_pages()) I have

           /* Find smallest order which can satisfy the request. */
            for ( j = order; j <= MAX_ORDER; j++ )
            {
                if ( (pg = page_list_remove_head(&heap(node, zone, j))) )
                {
                    if ( pg->u.free.first_dirty == INVALID_DIRTY_IDX )
                        return pg;
                    /*
                     * We grab single pages (order=0) even if they are
                     * unscrubbed. Given that scrubbing one page is 
fairly quick
                     * it is not worth breaking higher orders.
                     */
                    if ( (order == 0) || use_unscrubbed )
                    {
                        check_and_stop_scrub(pg);
                        return pg;
                    }

If first_dirty gets assigned INVALID_DIRTY_IDX then get_free_buddy() will return pg right away without telling the scrubber that the buddy has been taken for use. The scrubber will then put the buddy back on the heap.

-boris

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

#27 lore raw

Re: [PATCH v5 6/8] mm: Keep heap accessible to others while scrubbing

Quoted 19 lines
Quoted 1 line
Quoted 1 line

Boris Ostrovsky <[email protected]> 07/23/17 4:28 AM >>>

Quoted 16 lines · Jan Beulich

xen/common/page_alloc.c static void page_list_add_scrub(struct page_info *pg, unsigned int node, · line 709

Quoted 13 lines
Quoted 1 line
Quoted 1 line

Boris Ostrovsky <[email protected]> 06/22/17 8:56 PM >>>

xen/common/page_alloc.c +12 -0
+static void check_and_stop_scrub(struct page_info *head)
+{
+    if ( head->u.free.scrub_state == BUDDY_SCRUBBING )
+    {
+        struct page_info pg;
+
+        head->u.free.scrub_state = BUDDY_SCRUB_ABORT;
+        spin_lock_kick();
+        for ( ; ; )
+        {
+            /* Can't ACCESS_ONCE() a bitfield. */
+            pg.u.free.val = ACCESS_ONCE(head->u.free.val);

Something like ACCESS_ONCE(head->u.free).val ought to work (or read_atomic(), due to the questionable scalar type check in ACCESS_ONCE()).

Quoted 7 lines

Hmm... I couldn't get this to work with either suggestion.

page_alloc.c:751:13: error: conversion to non-scalar type requested pg.u.free = read_atomic(&head->u.free);

page_alloc.c:753:6: error: conversion to non-scalar type requested if ( ACCESS_ONCE(head->u.free).scrub_state != BUDDY_SCRUB_ABORT )

Oh, indeed. That's rather unfortunate.

Jan

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

#28 lore raw

PATCH v5 7/8 mm: Print number of unscrubbed pages in 'H' debug handler

In reply to Boris Ostrovsky
---
 xen/common/page_alloc.c | 7 +++++++
 1 file changed, 7 insertions(+)
xen/common/page_alloc.c +7 -0
index f0e5399..da5ffc2 100644
@@ -2315,6 +2315,13 @@ static void dump_heap(unsigned char key)
             printk("heap[node=%d][zone=%d] -> %lu pages\n",
                    i, j, avail[i][j]);
     }
+
+    for ( i = 0; i < MAX_NUMNODES; i++ )
+    {
+        if ( !node_need_scrub[i] )
+            continue;
+        printk("Node %d has %lu unscrubbed pages\n", i, node_need_scrub[i]);
+    }
 }
 
 static __init int register_heap_trigger(void)
Signature · 7 lines
1.8.3.1


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

PATCH v5 8/8 mm: Make sure pages are scrubbed

In reply to Boris Ostrovsky

Add a debug Kconfig option that will make page allocator verify that pages that were supposed to be scrubbed are, in fact, clean.

---
Changes in v5:
* Defined SCRUB_PATTERN for NDEBUG
* Style chages

 xen/Kconfig.debug       |  7 ++++++
 xen/common/page_alloc.c | 63 ++++++++++++++++++++++++++++++++++++++++++++++++-
 2 files changed, 69 insertions(+), 1 deletion(-)
xen/Kconfig.debug +7 -0
index 689f297..195d504 100644
@@ -114,6 +114,13 @@ config DEVICE_TREE_DEBUG
 	  logged in the Xen ring buffer.
 	  If unsure, say N here.
 
+config SCRUB_DEBUG
+	bool "Page scrubbing test"
+	default DEBUG
+	---help---
+	  Verify that pages that need to be scrubbed before being allocated to
+	  a guest are indeed scrubbed.
+
 endif # DEBUG || EXPERT
 
 endmenu
xen/common/page_alloc.c +62 -1

119 lines not on this page · raw message

Signature · 7 lines
1.8.3.1


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
Reviewed-by on v5 8/8
#30 lore raw

Re: [PATCH v5 8/8] mm: Make sure pages are scrubbed

Quoted 5 lines
Quoted 1 line
Quoted 1 line

Boris Ostrovsky <[email protected]> 06/22/17 8:56 PM >>>

Add a debug Kconfig option that will make page allocator verify that pages that were supposed to be scrubbed are, in fact, clean.

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

#31 lore raw
In reply to Boris Ostrovsky
Quoted 5 lines
Quoted 1 line
Quoted 1 line · On 22.06.17 at 20:57, <[email protected]> wrote:

Deferred:
* Per-node heap locks. In addition to (presumably) improving performance in general, once they are available we can parallelize scrubbing further by allowing more than one core per node to do idle loop scrubbing.

I don't understand: A per-node lock still calls for just one CPU doing the scrubbing on that node, in order to not congest the lock.

Jan

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

#32 lore raw
Quoted 9 lines · Jan Beulich
Quoted 5 lines
Quoted 1 line
Quoted 1 line · On 22.06.17 at 20:57, <[email protected]> wrote:

Deferred:
* Per-node heap locks. In addition to (presumably) improving performance in general, once they are available we can parallelize scrubbing further by allowing more than one core per node to do idle loop scrubbing.

I don't understand: A per-node lock still calls for just one CPU doing the scrubbing on that node, in order to not congest the lock.

Is this necessarily true? Maybe not allow all cores on a node to scrub but I'd think having more than one core do the work may be beneficial. Don't forget that actual scrubbing is performed without holding locks. We only grab the lock to find dirty buddies in the heap.

-boris

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

#33 lore raw
Quoted 13 lines
Quoted 1 line
Quoted 1 line · On 23.06.17 at 15:11, <[email protected]> wrote:
Quoted 9 lines · Jan Beulich
Quoted 5 lines
Quoted 1 line
Quoted 1 line · On 22.06.17 at 20:57, <[email protected]> wrote:

Deferred:
* Per-node heap locks. In addition to (presumably) improving performance in general, once they are available we can parallelize scrubbing further by allowing more than one core per node to do idle loop scrubbing.

I don't understand: A per-node lock still calls for just one CPU doing the scrubbing on that node, in order to not congest the lock.

Quoted 4 lines

Is this necessarily true? Maybe not allow all cores on a node to scrub but I'd think having more than one core do the work may be beneficial. Don't forget that actual scrubbing is performed without holding locks. We only grab the lock to find dirty buddies in the heap.

Hmm, true, but then I still don't see the connection between breaking up the lock and parallelizing scrubbing.

Jan

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

#34 lore raw
Quoted 13 lines · Jan Beulich
Quoted 13 lines
Quoted 1 line
Quoted 1 line · On 23.06.17 at 15:11, <[email protected]> wrote:
Quoted 9 lines · Jan Beulich
Quoted 5 lines
Quoted 1 line
Quoted 1 line · On 22.06.17 at 20:57, <[email protected]> wrote:

Deferred:
* Per-node heap locks. In addition to (presumably) improving performance in general, once they are available we can parallelize scrubbing further by allowing more than one core per node to do idle loop scrubbing.

I don't understand: A per-node lock still calls for just one CPU doing the scrubbing on that node, in order to not congest the lock.

Quoted 7 lines
Quoted 4 lines

Is this necessarily true? Maybe not allow all cores on a node to scrub but I'd think having more than one core do the work may be beneficial. Don't forget that actual scrubbing is performed without holding locks. We only grab the lock to find dirty buddies in the heap.

Hmm, true, but then I still don't see the connection between breaking up the lock and parallelizing scrubbing.

With a single heap lock we might indeed start seeing lock contention when multiple CPUs from many nodes are scrubbing. Making it per-node should lessen the pressure.

-boris

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