Skip to contentSkip to conversation

gnttab: Improve scaleability

PATCH Superseded David Vrabel 4 participants 16 messages Apr 30, 2015 – May 12, 2015

v7 of 15 Superseded Latest is v15 →

lore
Share & tools
lore
#0 lore raw

v7 cover gnttab: Improve scaleability

The series makes the grant table locking more fine-grained and adds per-VCPU maptrack free lists, which greatly improves scalability.

The series builds on the original series by Matt Wilson and Christoph Egger from Amazon.

Performance results for aggregate intrahost network throughput (between 20 VM pairs, with 16 dom0 VCPUs) show substantial improvements.

Throughput/Gbit/s

Base 9.7
Split locks 25.8
Split locks 42.9
+ per-VCPU maptrack lists

v7:
* Re-add grant table rwlock (locking was broken without it and the gains are minimal anyway).
* Remove unneeded locks from grant_table_destroy().
* Fix get_maptrack_handle() locking.
v6:
* Remove most uses of the grant table lock.
* Make the grant table lock a spin lock again (there were only writers left after the above)
* Add per-VCPU maptrack free lists.
v5:
* Addressed locking issue pointed out by Jan Beulich
* Fixed git rebase merge issue introduced in v4 (acquiring locking twice)
* Change for ()-loop in grant_map_exists
* Coding style fixes
v4:
* Coding style nits from Jan Beulich
* Fixup read locks pointed out by Jan Beulich
* renamed double_gt_(un)lock to double_maptrack_(un)lock per request from Jan Beulich
* Addressed ASSERT()'s from Jan Beulich
* Addressed locking issues in unmap_common pointed out by Jan Beulich
v3:
* Addressed gnttab_swap_grant_ref() comment from Andrew Cooper
v2:
* Add arm part per request from Julien Grall

David

#1 lore raw

PATCH v7 1/3 gnttab: Introduce rwlock to protect updates to grant table state

From: Christoph Egger <[email protected]>

Split grant table lock into two separate locks. One to protect maptrack state and change the other into a rwlock.

The rwlock is used to prevent readers from accessing inconsistent grant table state such as current version, partially initialized active table pages, etc.

[chegger: ported to xen-staging, split into multiple commits]
---
 docs/misc/grant-tables.txt    |   28 ++++++++-
 xen/arch/arm/mm.c             |    4 +-
 xen/arch/x86/mm.c             |    4 +-
 xen/common/grant_table.c      |  140 +++++++++++++++++++++++------------------
 xen/include/xen/grant_table.h |   11 +++-
 5 files changed, 118 insertions(+), 69 deletions(-)
docs/misc/grant-tables.txt +27 -1
index 19db4ec..c9ae8f2 100644
@@ -74,7 +74,33 @@ is complete.
  matching map track entry is then removed, as if unmap had been invoked.
  These are not used by the transfer mechanism.
   map->domid         : owner of the mapped frame
-  map->ref_and_flags : grant reference, ro/rw, mapped for host or device access
+  map->ref           : grant reference
+  map->flags         : ro/rw, mapped for host or device access
+
+********************************************************************************
+ Locking
+ ~~~~~~~
+ Xen uses several locks to serialize access to the internal grant table state.
+
+  grant_table->lock          : rwlock used to prevent readers from accessing
+                               inconsistent grant table state such as current
+                               version, partially initialized active table pages,
+                               etc.
+  grant_table->maptrack_lock : spinlock used to protect the maptrack state
+
+ The primary lock for the grant table is a read/write spinlock. All
+ functions that access members of struct grant_table must acquire a
+ read lock around critical sections. Any modification to the members
+ of struct grant_table (e.g., nr_status_frames, nr_grant_frames,
+ active frames, etc.) must only be made if the write lock is
+ held. These elements are read-mostly, and read critical sections can
+ be large, which makes a rwlock a good choice.
+
+ The maptrack state is protected by its own spinlock. Any access (read
+ or write) of struct grant_table members that have a "maptrack_"
+ prefix must be made while holding the maptrack lock. The maptrack
+ state can be rapidly modified under some workloads, and the critical
+ sections are very small, thus we use a spinlock to protect them.
 
 ********************************************************************************
 
xen/arch/arm/mm.c +2 -2
index 11a0be8..f39e978 100644
@@ -1048,7 +1048,7 @@ int xenmem_add_to_physmap_one(
     switch ( space )
     {
     case XENMAPSPACE_grant_table:
-        spin_lock(&d->grant_table->lock);
+        write_lock(&d->grant_table->lock);
 
         if ( d->grant_table->gt_version == 0 )
             d->grant_table->gt_version = 1;
@@ -1078,7 +1078,7 @@ int xenmem_add_to_physmap_one(
 
         t = p2m_ram_rw;
 
-        spin_unlock(&d->grant_table->lock);
+        write_unlock(&d->grant_table->lock);
         break;
     case XENMAPSPACE_shared_info:
         if ( idx != 0 )
xen/arch/x86/mm.c +2 -2
index b724aa5..a46d2ff 100644
@@ -4569,7 +4569,7 @@ int xenmem_add_to_physmap_one(
                 mfn = virt_to_mfn(d->shared_info);
             break;
         case XENMAPSPACE_grant_table:
-            spin_lock(&d->grant_table->lock);
+            write_lock(&d->grant_table->lock);
 
             if ( d->grant_table->gt_version == 0 )
                 d->grant_table->gt_version = 1;
@@ -4591,7 +4591,7 @@ int xenmem_add_to_physmap_one(
                     mfn = virt_to_mfn(d->grant_table->shared_raw[idx]);
             }
 
-            spin_unlock(&d->grant_table->lock);
+            write_unlock(&d->grant_table->lock);
             break;
         case XENMAPSPACE_gmfn_range:
         case XENMAPSPACE_gmfn:
xen/common/grant_table.c +79 -61

458 lines not on this page · raw message

xen/include/xen/grant_table.h +8 -3
index 32f5786..98cc94a 100644
@@ -64,6 +64,11 @@ struct grant_mapping {
 
 /* Per-domain grant information. */
 struct grant_table {
+    /* 
+     * Lock protecting updates to grant table state (version, active
+     * entry list, etc.)
+     */
+    rwlock_t              lock;
     /* Table size. Number of frames shared with guest */
     unsigned int          nr_grant_frames;
     /* Shared grant table (see include/public/grant_table.h). */
@@ -82,8 +87,8 @@ struct grant_table {
     struct grant_mapping **maptrack;
     unsigned int          maptrack_head;
     unsigned int          maptrack_limit;
-    /* Lock protecting updates to active and shared grant tables. */
-    spinlock_t            lock;
+    /* Lock protecting the maptrack page list, head, and limit */
+    spinlock_t            maptrack_lock;
     /* The defined versions are 1 and 2.  Set to 0 if we don't know
        what version to use yet. */
     unsigned              gt_version;
@@ -101,7 +106,7 @@ gnttab_release_mappings(
     struct domain *d);
 
 /* Increase the size of a domain's grant table.
- * Caller must hold d's grant table lock.
+ * Caller must hold d's grant table write lock.
  */
 int
 gnttab_grow_table(struct domain *d, unsigned int req_nr_frames);
Signature · 1 line
1.7.10.4
#2 lore raw

Re: [PATCHv7 1/3] gnttab: Introduce rwlock to protect updates to grant table state

Quoted 25 lines
Quoted 1 line
Quoted 1 line · On 30.04.15 at 15:28, <[email protected]> wrote:
docs/misc/grant-tables.txt +17 -1
@@ -74,7 +74,33 @@ is complete.
  matching map track entry is then removed, as if unmap had been invoked.
  These are not used by the transfer mechanism.
   map->domid         : owner of the mapped frame
-  map->ref_and_flags : grant reference, ro/rw, mapped for host or device access
+  map->ref           : grant reference
+  map->flags         : ro/rw, mapped for host or device access
+
+********************************************************************************
+ Locking
+ ~~~~~~~
+ Xen uses several locks to serialize access to the internal grant table state.
+
+  grant_table->lock          : rwlock used to prevent readers from accessing
+                               inconsistent grant table state such as current
+                               version, partially initialized active table pages,
+                               etc.
+  grant_table->maptrack_lock : spinlock used to protect the maptrack state
+
+ The primary lock for the grant table is a read/write spinlock. All
+ functions that access members of struct grant_table must acquire a
+ read lock around critical sections. Any modification to the members

docs/misc/grant-tables.txt is complete. · line 103

Quoted 10 lines
docs/misc/grant-tables.txt +10 -0
+ of struct grant_table (e.g., nr_status_frames, nr_grant_frames,
+ active frames, etc.) must only be made if the write lock is
+ held. These elements are read-mostly, and read critical sections can
+ be large, which makes a rwlock a good choice.
+
+ The maptrack state is protected by its own spinlock. Any access (read
+ or write) of struct grant_table members that have a "maptrack_"
+ prefix must be made while holding the maptrack lock. The maptrack
+ state can be rapidly modified under some workloads, and the critical
+ sections are very small, thus we use a spinlock to protect them.

Still no word about nesting between the two?

Quoted 19 lines
docs/misc/grant-tables.txt +1 -3
@@ -629,7 +629,7 @@ __gnttab_map_grant_ref(
     }
 
     rgt = rd->grant_table;
-    spin_lock(&rgt->lock);
+    read_lock(&rgt->lock);
 
     if ( rgt->gt_version == 0 )
         PIN_FAIL(unlock_out, GNTST_general_error,
@@ -702,8 +702,6 @@ __gnttab_map_grant_ref(
 
     cache_flags = (shah->flags & (GTF_PAT | GTF_PWT | GTF_PCD) );
 
-    spin_unlock(&rgt->lock);
-
     /* pg may be set, with a refcount included, from __get_paged_frame */
     if ( !pg )
     {

docs/misc/grant-tables.txt __gnttab_map_grant_ref( · line 779

Quoted 5 lines
docs/misc/grant-tables.txt +1 -1
@@ -778,7 +776,7 @@ __gnttab_map_grant_ref(
         goto undo_out;
     }
 
-    double_gt_lock(lgt, rgt);
+    double_maptrack_lock(lgt, rgt);

So looking just at the patch context here together with the two earlier
hunks: You now keep holding the rwlock, but the code following the undo_out label read-locks the lock another time.

Quoted 4 lines
docs/misc/grant-tables.txt +0 -0
@@ -1290,10 +1294,14 @@ gnttab_unpopulate_status_frames(struct domain *d, 
 struct grant_table *gt)
     gt->nr_status_frames = 0;
 }

docs/misc/grant-tables.txt gnttab_unpopulate_status_frames(struct domain *d, · line 1305

Quoted 10 lines
docs/misc/grant-tables.txt +5 -1
 
+/* 
+ * Grow the grant table. The caller must hold the grant table's
+ * write lock before calling this function.
+ */
 int
 gnttab_grow_table(struct domain *d, unsigned int req_nr_frames)
 {
-    /* d's grant table lock must be held by the caller */
+    /* d's grant table write lock must be held by the caller */

This comment is now redundant with the one you add ahead of the function.

docs/misc/grant-tables.txt gnttab_release_mappings( · line 3072

Quoted 7 lines
docs/misc/grant-tables.txt +2 -1
@@ -3052,7 +3068,8 @@ gnttab_release_mappings(
         }
 
         rgt = rd->grant_table;
-        spin_lock(&rgt->lock);
+        read_lock(&rgt->lock);
+        double_maptrack_lock(gt, rgt);

What is it that requires gt's maptrack lock to also be acquired here?

Jan

#3 lore raw

PATCH v7 2/3 gnttab: refactor locking for scalability

In reply to David Vrabel

From: Matt Wilson <[email protected]>

This patch refactors grant table locking. It splits the previous single spinlock per grant table into multiple locks. The heavily modified components of the grant table (the maptrack state and the active entries) are now protected by their own spinlocks. The remaining elements of the grant table are read-mostly, so the main grant table lock is modified to be a rwlock to improve concurrency.

Workloads with high grant table operation rates, especially map/unmap operations as used by blkback/blkfront when persistent grants are not supported, show marked improvement with these changes. A domU with 24 VBDs in a streaming 2M write workload achieved 1,400 MB/sec before this change. Performance more than doubles with this patch, reaching 3,000 MB/sec before tuning and 3,600 MB/sec after adjusting event channel vCPU bindings.

[chegger: ported to xen-staging, split into multiple commits]
---
 docs/misc/grant-tables.txt |   21 +++++
 xen/common/grant_table.c   |  213 +++++++++++++++++++++++++++-----------------
 2 files changed, 153 insertions(+), 81 deletions(-)
docs/misc/grant-tables.txt +21 -0
index c9ae8f2..1ada018 100644
@@ -63,6 +63,7 @@ is complete.
   act->domid : remote domain being granted rights
   act->frame : machine frame being granted
   act->pin   : used to hold reference counts
+  act->lock  : spinlock used to serialize access to active entry state
 
  Map tracking
  ~~~~~~~~~~~~
@@ -87,6 +88,8 @@ is complete.
                                version, partially initialized active table pages,
                                etc.
   grant_table->maptrack_lock : spinlock used to protect the maptrack state
+  active_grant_entry->lock   : spinlock used to serialize modifications to
+                               active entries
 
  The primary lock for the grant table is a read/write spinlock. All
  functions that access members of struct grant_table must acquire a
@@ -102,6 +105,24 @@ is complete.
  state can be rapidly modified under some workloads, and the critical
  sections are very small, thus we use a spinlock to protect them.
 
+ Active entries are obtained by calling active_entry_acquire(gt, ref).
+ This function returns a pointer to the active entry after locking its
+ spinlock. The caller must hold the rwlock for the gt in question
+ before calling active_entry_acquire(). This is because the grant
+ table can be dynamically extended via gnttab_grow_table() while a
+ domain is running and must be fully initialized. Once all access to
+ the active entry is complete, release the lock by calling
+ active_entry_release(act).
+
+ Summary of rules for locking:
+  active_entry_acquire() and active_entry_release() can only be
+  called when holding the relevant grant table's lock. I.e.:
+    read_lock(&gt->lock);
+    act = active_entry_acquire(gt, ref);
+    ...
+    active_entry_release(act);
+    read_unlock(&gt->lock);
+
 ********************************************************************************
 
  Granting a foreign domain access to frames
xen/common/grant_table.c +132 -81

556 lines not on this page · raw message

Signature · 1 line
1.7.10.4
#4 lore raw

Re: [PATCHv7 2/3] gnttab: refactor locking for scalability

xen/common/grant_table.c static void mapcount(

Quoted 4 lines · David Vrabel
xen/common/grant_table.c +4 -0
+    /* 
+     * N.B.: while taking the left side maptrack spinlock prevents
+     * any mapping changes, the right side active entries could be
+     * changing while we are counting.

IIRC, the lX/rX variables are local/remote rather than left/right.

Quoted 36 lines
xen/common/grant_table.c +3 -4
@@ -639,7 +660,7 @@ __gnttab_map_grant_ref(
     if ( unlikely(op->ref >= nr_grant_entries(rgt)))
         PIN_FAIL(unlock_out, GNTST_bad_gntref, "Bad ref (%d).\n", op->ref);
 
-    act = &active_entry(rgt, op->ref);
+    act = active_entry_acquire(rgt, op->ref);
     shah = shared_entry_header(rgt, op->ref);
     if (rgt->gt_version == 1) {
         sha1 = &shared_entry_v1(rgt, op->ref);
@@ -656,7 +677,7 @@ __gnttab_map_grant_ref(
          ((act->domid != ld->domain_id) ||
           (act->pin & 0x80808080U) != 0 ||
           (act->is_sub_page)) )
-        PIN_FAIL(unlock_out, GNTST_general_error,
+        PIN_FAIL(act_release_out, GNTST_general_error,
                  "Bad domain (%d != %d), or risk of counter overflow %08x, or subpage %d\n",
                  act->domid, ld->domain_id, act->pin, act->is_sub_page);
 
@@ -667,7 +688,7 @@ __gnttab_map_grant_ref(
         if ( (rc = _set_status(rgt->gt_version, ld->domain_id,
                                op->flags & GNTMAP_readonly,
                                1, shah, act, status) ) != GNTST_okay )
-             goto unlock_out;
+            goto act_release_out;
 
         if ( !act->pin )
         {
@@ -698,7 +719,6 @@ __gnttab_map_grant_ref(
             GNTPIN_hstr_inc : GNTPIN_hstw_inc;
 
     frame = act->frame;
-    act_pin = act->pin;
 
     cache_flags = (shah->flags & (GTF_PAT | GTF_PWT | GTF_PCD) );
 

xen/common/grant_table.c __gnttab_map_grant_ref( · line 780

Quoted 5 lines
xen/common/grant_table.c +0 -2
@@ -776,8 +796,6 @@ __gnttab_map_grant_ref(
         goto undo_out;
     }
 
-    double_maptrack_lock(lgt, rgt);
-

OK, so here we hold rd->grant_table->lock and act->lock (which is in the rd table) and are going to acquire lgt->maptrack_lock in mapcount().

That means we can't ever have a path holding domA's maptrack lock that acquires domB's gt lock or one of domB's act->locks. I think that's OK because after this patch the only paths left that hold the maptrack lock can't acquire anything except an act->lock of the same domain. Do I understand that correctly?

Also: because mapcount() itself doesn't take any act->lock, there's no path that holds two act->locks at the same time?

I think it would be nice to expand the locking-discipline comment to make those restrictions clear.

Cheers,

Tim.

#5 lore raw

Re: [PATCHv7 2/3] gnttab: refactor locking for scalability

Quoted 4 lines · Tim Deegan

OK, so here we hold rd->grant_table->lock and act->lock (which is in the rd table) and are going to acquire lgt->maptrack_lock in mapcount().

Quoted 8 lines

That means we can't ever have a path holding domA's maptrack lock that acquires domB's gt lock or one of domB's act->locks. I think that's OK because after this patch the only paths left that hold the maptrack lock can't acquire anything except an act->lock of the same domain. Do I understand that correctly?

Also: because mapcount() itself doesn't take any act->lock, there's no path that holds two act->locks at the same time?

Um. I think it's easier if we just say you cannot take another lock while holding a maptrack_lock since that's currently the case.

David

#6 lore raw

Re: [PATCHv7 2/3] gnttab: refactor locking for scalability

Quoted 11 lines · David Vrabel
Quoted 10 lines · Tim Deegan

OK, so here we hold rd->grant_table->lock and act->lock (which is in the rd table) and are going to acquire lgt->maptrack_lock in mapcount().

That means we can't ever have a path holding domA's maptrack lock that acquires domB's gt lock or one of domB's act->locks. I think that's OK because after this patch the only paths left that hold the maptrack lock can't acquire anything except an act->lock of the same domain. Do I understand that correctly?

Quoted 5 lines
Quoted 2 lines

Also: because mapcount() itself doesn't take any act->lock, there's no path that holds two act->locks at the same time?

Um. I think it's easier if we just say you cannot take another lock while holding a maptrack_lock since that's currently the case.

Er, yes. I had misread the locking order, sorry. So we just need to say (a) no locks inside the maptrack lock and (b) never take two act->locks at once.

Tim.

#7 lore raw

Re: [PATCHv7 2/3] gnttab: refactor locking for scalability

In reply to David Vrabel
Quoted 8 lines
Quoted 2 lines
Quoted 2 lines · On 30.04.15 at 15:28, <[email protected]> wrote:

On 30.04.15 at 15:28, <[email protected]> wrote:

This patch refactors grant table locking. It splits the previous single spinlock per grant table into multiple locks. The heavily modified components of the grant table (the maptrack state and the active entries) are now protected by their own spinlocks. The remaining elements of the grant table are read-mostly, so the main grant table lock is modified to be a rwlock to improve concurrency.

This looks to be (partly) stale.

Quoted 21 lines
xen/common/grant_table.c +1 -5
@@ -812,7 +829,7 @@ __gnttab_map_grant_ref(
     mt->ref   = op->ref;
     mt->flags = op->flags;
 
-    double_maptrack_unlock(lgt, rgt);
+    active_entry_release(act);
     read_unlock(&rgt->lock);
 
     op->dev_bus_addr = (u64)frame << PAGE_SHIFT;
@@ -836,10 +853,6 @@ __gnttab_map_grant_ref(
         put_page(pg);
     }
 
-    read_lock(&rgt->lock);
-
-    act = &active_entry(rgt, op->ref);
-
     if ( op->flags & GNTMAP_device_map )
         act->pin -= (op->flags & GNTMAP_readonly) ?
             GNTPIN_devr_inc : GNTPIN_devw_inc;

xen/common/grant_table.c __gnttab_map_grant_ref( · line 875

Quoted 8 lines
xen/common/grant_table.c +3 -0
@@ -855,6 +868,9 @@ __gnttab_map_grant_ref(
     if ( !act->pin )
         gnttab_clear_flag(_GTF_reading, status);
 
+ act_release_out:
+    active_entry_release(act);
+
  unlock_out:
     read_unlock(&rgt->lock);

This again looks like broken locking: You drop the lock twice, but the intermediate re-acquire gets deleted. Additionally it looks like you let go of "act" before being done with modifying it (act->pin adjustment visible in context above) and reading it (act->pin check further down), and then you release it another time here. Am I overlooking something?

Quoted 8 lines
xen/common/grant_table.c +1 -1
@@ -2414,16 +2446,18 @@ gnttab_set_version(XEN_GUEST_HANDLE_PARAM(gnttab_set_version_t) uop)
     {
         for ( i = GNTTAB_NR_RESERVED_ENTRIES; i < nr_grant_entries(gt); i++ )
         {
-            act = &active_entry(gt, i);
+            act = active_entry_acquire(gt, i);
             if ( act->pin != 0 )
             {

xen/common/grant_table.c gnttab_set_version(XEN_GUEST_HANDLE_PARAM(gnttab_set_version_t) uop) · line 2461

Quoted 10 lines
xen/common/grant_table.c +2 -0
                 gdprintk(XENLOG_WARNING,
                          "tried to change grant table version from %d to %d, but some grant entries still in use\n",
                          gt->gt_version,
                          op.version);
+                active_entry_release(act);
                 res = -EBUSY;
                 goto out_unlock;
             }
+            active_entry_release(act);
         }

Is it really useful to acquire the entry here? I.e. wouldn't read_atomic(act->pin) suffice?

Jan

#8 lore raw

PATCH v7 3/3 gnttab: use per-VCPU maptrack free lists

In reply to David Vrabel

From: Malcolm Crossley <[email protected]>

Performance analysis of aggregate network throughput with many VMs shows that performance is signficantly limited by contention on the maptrack lock when obtaining/releasing maptrack handles from the free list.

Instead of a single free list use a per-VCPU list. This avoids any contention when obtaining a handle. Handles must be released back to their original list and since this may occur on a different VCPU there is some contention on the destination VCPU's free list tail pointer (but this is much better than a per-domain lock).

A domain's maptrack limit is multiplied by the number of VCPUs. This ensures that a worst case domain that only performs grant table operations via one VCPU will not see a lower map track limit.

---
 xen/common/grant_table.c      |  131 ++++++++++++++++++++++++-----------------
 xen/include/xen/grant_table.h |    8 ++-
 xen/include/xen/sched.h       |    5 ++
 3 files changed, 87 insertions(+), 57 deletions(-)
xen/common/grant_table.c +77 -54
index 569b3d3..d8e7373 100644
@@ -118,9 +118,9 @@ struct gnttab_unmap_common {
     ((t)->maptrack[(e)/MAPTRACK_PER_PAGE][(e)%MAPTRACK_PER_PAGE])
 
 static inline unsigned int
-nr_maptrack_frames(struct grant_table *t)
+nr_vcpu_maptrack_frames(struct vcpu *v)
 {
-    return t->maptrack_limit / MAPTRACK_PER_PAGE;
+    return v->maptrack_limit / MAPTRACK_PER_PAGE;
 }
 
 #define MAPTRACK_TAIL (~0u)
@@ -255,66 +255,91 @@ static int __get_paged_frame(unsigned long gfn, unsigned long *frame, struct pag
 
 static inline int
 __get_maptrack_handle(
-    struct grant_table *t)
+    struct grant_table *t,
+    struct vcpu *v)
 {
-    unsigned int h;
-    if ( unlikely((h = t->maptrack_head) == MAPTRACK_TAIL) )
+    unsigned int head, next;
+
+    head = v->maptrack_head;
+    if ( unlikely(head == MAPTRACK_TAIL) )
+        return -1;
+
+    next = maptrack_entry(t, head).ref;
+    if ( unlikely(next == MAPTRACK_TAIL) )
         return -1;
-    t->maptrack_head = maptrack_entry(t, h).ref;
-    return h;
+
+    v->maptrack_head = next;
+
+    return head;
 }
 
 static inline void
 put_maptrack_handle(
     struct grant_table *t, int handle)
 {
-    spin_lock(&t->maptrack_lock);
-    maptrack_entry(t, handle).ref = t->maptrack_head;
-    t->maptrack_head = handle;
-    spin_unlock(&t->maptrack_lock);
+    struct domain *d = current->domain;
+    struct vcpu *v;
+    u32 prev_tail, cur_tail;
+
+    /* Set current hande to have TAIL reference */
+    maptrack_entry(t, handle).ref = MAPTRACK_TAIL;
+    v = d->vcpu[maptrack_entry(t,handle).vcpu];
+    cur_tail = v->maptrack_tail;
+    do {
+        prev_tail = cur_tail;
+        cur_tail = cmpxchg((u32 *)&v->maptrack_tail, prev_tail, handle);
+    } while ( cur_tail != prev_tail );
+    maptrack_entry(t, prev_tail).ref = handle;
 }
 
 static inline int
 get_maptrack_handle(
     struct grant_table *lgt)
 {
+    struct vcpu          *v = current;
     int                   i;
     grant_handle_t        handle;
     struct grant_mapping *new_mt;
-    unsigned int          new_mt_limit, nr_frames;
-
-    spin_lock(&lgt->maptrack_lock);
+    unsigned int          nr_frames;
 
-    while ( unlikely((handle = __get_maptrack_handle(lgt)) == -1) )
-    {
-        nr_frames = nr_maptrack_frames(lgt);
-        if ( nr_frames >= max_maptrack_frames )
-            break;
-
-        new_mt = alloc_xenheap_page();
-        if ( !new_mt )
-            break;
+    handle = __get_maptrack_handle(lgt, v);
+    if ( likely(handle != -1) )
+        return handle;
 
-        clear_page(new_mt);
+    nr_frames = nr_vcpu_maptrack_frames(v);
+    if ( nr_frames >= max_maptrack_frames )
+        return -1;
 
-        new_mt_limit = lgt->maptrack_limit + MAPTRACK_PER_PAGE;
+    new_mt = alloc_xenheap_page();
+    if ( !new_mt )
+        return -1;
+    clear_page(new_mt);
 
-        for ( i = 1; i < MAPTRACK_PER_PAGE; i++ )
-            new_mt[i - 1].ref = lgt->maptrack_limit + i;
-        new_mt[i - 1].ref = lgt->maptrack_head;
-        lgt->maptrack_head = lgt->maptrack_limit;
+    spin_lock(&lgt->maptrack_lock);
 
-        lgt->maptrack[nr_frames] = new_mt;
-        smp_wmb();
-        lgt->maptrack_limit      = new_mt_limit;
+    for ( i = 1; i < MAPTRACK_PER_PAGE; i++ ){
+        new_mt[i - 1].ref = (lgt->maptrack_pages * MAPTRACK_PER_PAGE) + i;
+        new_mt[i - 1].vcpu = v->vcpu_id;
+    }
+    /* Set last entry vcpu and ref */
+    new_mt[i - 1].ref = v->maptrack_head;
+    new_mt[i - 1].vcpu = v->vcpu_id;
 
-        gdprintk(XENLOG_INFO, "Increased maptrack size to %u frames\n",
-                 nr_frames + 1);
+    v->maptrack_head = lgt->maptrack_pages * MAPTRACK_PER_PAGE;
+    if (v->maptrack_tail == MAPTRACK_TAIL)
+    {
+        v->maptrack_tail = (lgt->maptrack_pages * MAPTRACK_PER_PAGE)
+            + MAPTRACK_PER_PAGE - 1;
+        new_mt[i - 1].ref = MAPTRACK_TAIL;
     }
 
+    lgt->maptrack[lgt->maptrack_pages++] = new_mt;
+
     spin_unlock(&lgt->maptrack_lock);
 
-    return handle;
+    v->maptrack_limit += MAPTRACK_PER_PAGE;
+
+    return __get_maptrack_handle(lgt, v);
 }
 
 /* 
@@ -566,7 +591,7 @@ static void mapcount(
     ASSERT(rw_is_locked(&rd->grant_table->lock));
     spin_lock(&lgt->maptrack_lock);
 
-    for ( handle = 0; handle < lgt->maptrack_limit; handle++ )
+    for ( handle = 0; handle < lgt->maptrack_pages * MAPTRACK_PER_PAGE; handle++ )
     {
         map = &maptrack_entry(lgt, handle);
         if ( !(map->flags & (GNTMAP_device_map|GNTMAP_host_map)) ||
@@ -914,7 +939,7 @@ __gnttab_unmap_common(
 
     op->frame = (unsigned long)(op->dev_bus_addr >> PAGE_SHIFT);
 
-    if ( unlikely(op->handle >= lgt->maptrack_limit) )
+    if ( unlikely(op->handle >= lgt->maptrack_pages * MAPTRACK_PER_PAGE) )
     {
         gdprintk(XENLOG_INFO, "Bad handle (%d).\n", op->handle);
         op->status = GNTST_bad_handle;
@@ -1389,6 +1414,7 @@ gnttab_setup_table(
     struct gnttab_setup_table op;
     struct domain *d;
     struct grant_table *gt;
+    struct vcpu *v;
     int            i;
     xen_pfn_t  gmfn;
 
@@ -1430,6 +1456,17 @@ gnttab_setup_table(
     gt = d->grant_table;
     write_lock(&gt->lock);
 
+    /* Tracking of mapped foreign frames table */
+    if ( (gt->maptrack = xzalloc_array(struct grant_mapping *,
+                                       max_maptrack_frames * d->max_vcpus)) == NULL )
+        goto out3;
+    for_each_vcpu( d, v )
+    {
+        v->maptrack_head = MAPTRACK_TAIL;
+        v->maptrack_tail = MAPTRACK_TAIL;
+    }
+    gt->maptrack_pages = 0;
+
     if ( gt->gt_version == 0 )
         gt->gt_version = 1;
 
@@ -3024,18 +3061,6 @@ grant_table_create(
             spin_lock_init(&t->active[i][j].lock);
     }
 
-    /* Tracking of mapped foreign frames table */
-    if ( (t->maptrack = xzalloc_array(struct grant_mapping *,
-                                      max_maptrack_frames)) == NULL )
-        goto no_mem_2;
-    if ( (t->maptrack[0] = alloc_xenheap_page()) == NULL )
-        goto no_mem_3;
-    clear_page(t->maptrack[0]);
-    t->maptrack_limit = MAPTRACK_PER_PAGE;
-    for ( i = 1; i < MAPTRACK_PER_PAGE; i++ )
-        t->maptrack[0][i - 1].ref = i;
-    t->maptrack[0][i - 1].ref = MAPTRACK_TAIL;
-
     /* Shared grant table. */
     if ( (t->shared_raw = xzalloc_array(void *, max_grant_frames)) == NULL )
         goto no_mem_3;
@@ -3066,12 +3091,10 @@ grant_table_create(
         free_xenheap_page(t->shared_raw[i]);
     xfree(t->shared_raw);
  no_mem_3:
-    free_xenheap_page(t->maptrack[0]);
-    xfree(t->maptrack);
- no_mem_2:
     for ( i = 0;
           i < num_act_frames_from_sha_frames(INITIAL_NR_GRANT_FRAMES); i++ )
         free_xenheap_page(t->active[i]);
+ no_mem_2:
     xfree(t->active);
  no_mem_1:
     xfree(t);
@@ -3095,7 +3118,7 @@ gnttab_release_mappings(
 
     BUG_ON(!d->is_dying);
 
-    for ( handle = 0; handle < gt->maptrack_limit; handle++ )
+    for ( handle = 0; handle < (gt->maptrack_pages * MAPTRACK_PER_PAGE); handle++ )
     {
         map = &maptrack_entry(gt, handle);
         if ( !(map->flags & (GNTMAP_device_map|GNTMAP_host_map)) )
@@ -3200,7 +3223,7 @@ grant_table_destroy(
         free_xenheap_page(t->shared_raw[i]);
     xfree(t->shared_raw);
 
-    for ( i = 0; i < nr_maptrack_frames(t); i++ )
+    for ( i = 0; i < t->maptrack_pages; i++ )
         free_xenheap_page(t->maptrack[i]);
     xfree(t->maptrack);
 
xen/include/xen/grant_table.h +5 -3
index 98cc94a..c10464c 100644
@@ -60,6 +60,8 @@ struct grant_mapping {
     u32      ref;           /* grant ref */
     u16      flags;         /* 0-4: GNTMAP_* ; 5-15: unused */
     domid_t  domid;         /* granting domain */
+    u32      vcpu;          /* vcpu which created the grant mapping */
+    u32      pad;           /* round size to a power of 2 */
 };
 
 /* Per-domain grant information. */
@@ -83,10 +85,10 @@ struct grant_table {
     grant_status_t       **status;
     /* Active grant table. */
     struct active_grant_entry **active;
-    /* Mapping tracking table. */
+    /* Mapping tracking table per vcpu. */
     struct grant_mapping **maptrack;
-    unsigned int          maptrack_head;
-    unsigned int          maptrack_limit;
+    /* Total pages used for mapping tracking table */
+    unsigned int          maptrack_pages;
     /* Lock protecting the maptrack page list, head, and limit */
     spinlock_t            maptrack_lock;
     /* The defined versions are 1 and 2.  Set to 0 if we don't know
xen/include/xen/sched.h +5 -0
index 80c6f62..7f8422e 100644
@@ -219,6 +219,11 @@ struct vcpu
     /* VCPU paused by system controller. */
     int              controller_pause_count;
 
+    /* Maptrack */
+    unsigned int     maptrack_head;
+    unsigned int     maptrack_tail;
+    unsigned int     maptrack_limit;
+
     /* IRQ-safe virq_lock protects against delivering VIRQ to stale evtchn. */
     evtchn_port_t    virq_to_evtchn[NR_VIRQS];
     spinlock_t       virq_lock;
Signature · 1 line
1.7.10.4
Acked-by on v7 3/3
#9 lore raw

Re: [PATCHv7 3/3] gnttab: use per-VCPU maptrack free lists

Quoted 50 lines · David Vrabel

From: Malcolm Crossley <[email protected]>

Performance analysis of aggregate network throughput with many VMs shows that performance is signficantly limited by contention on the maptrack lock when obtaining/releasing maptrack handles from the free list.

Instead of a single free list use a per-VCPU list. This avoids any contention when obtaining a handle. Handles must be released back to their original list and since this may occur on a different VCPU there is some contention on the destination VCPU's free list tail pointer (but this is much better than a per-domain lock).

A domain's maptrack limit is multiplied by the number of VCPUs. This ensures that a worst case domain that only performs grant table operations via one VCPU will not see a lower map track limit.

---
 xen/common/grant_table.c      |  131 ++++++++++++++++++++++++-----------------
 xen/include/xen/grant_table.h |    8 ++-
 xen/include/xen/sched.h       |    5 ++
 3 files changed, 87 insertions(+), 57 deletions(-)
xen/common/grant_table.c +4 -4
index 569b3d3..d8e7373 100644
@@ -118,9 +118,9 @@ struct gnttab_unmap_common {
     ((t)->maptrack[(e)/MAPTRACK_PER_PAGE][(e)%MAPTRACK_PER_PAGE])
 
 static inline unsigned int
-nr_maptrack_frames(struct grant_table *t)
+nr_vcpu_maptrack_frames(struct vcpu *v)
 {
-    return t->maptrack_limit / MAPTRACK_PER_PAGE;
+    return v->maptrack_limit / MAPTRACK_PER_PAGE;
 }
 
 #define MAPTRACK_TAIL (~0u)
@@ -255,66 +255,91 @@ static int __get_paged_frame(unsigned long gfn, unsigned long *frame, struct pag
 
 static inline int
 __get_maptrack_handle(
-    struct grant_table *t)
+    struct grant_table *t,
+    struct vcpu *v)
 {
-    unsigned int h;

xen/common/grant_table.c static int __get_paged_frame(unsigned long gfn, unsigned long *frame, struct pag · line 269

Quoted 10 lines
xen/common/grant_table.c +8 -1
-    if ( unlikely((h = t->maptrack_head) == MAPTRACK_TAIL) )
+    unsigned int head, next;
+
+    head = v->maptrack_head;
+    if ( unlikely(head == MAPTRACK_TAIL) )
+        return -1;
+
+    next = maptrack_entry(t, head).ref;
+    if ( unlikely(next == MAPTRACK_TAIL) )
         return -1;

So this is an extra restriction over the old code: the list shall never be empty. Is that to make freeing onto the tail of a remote freelist easier? If so can you please add a comment so that nobody drops this later?

Quoted 18 lines
xen/common/grant_table.c +6 -6
-    t->maptrack_head = maptrack_entry(t, h).ref;
-    return h;
+
+    v->maptrack_head = next;
+
+    return head;
 }
 
 static inline void
 put_maptrack_handle(
     struct grant_table *t, int handle)
 {
-    spin_lock(&t->maptrack_lock);
-    maptrack_entry(t, handle).ref = t->maptrack_head;
-    t->maptrack_head = handle;
-    spin_unlock(&t->maptrack_lock);
+    struct domain *d = current->domain;
+    struct vcpu *v;

xen/common/grant_table.c static int __get_paged_frame(unsigned long gfn, unsigned long *frame, struct pag · line 291

Quoted 10 lines
xen/common/grant_table.c +10 -0
+    u32 prev_tail, cur_tail;
+
+    /* Set current hande to have TAIL reference */
+    maptrack_entry(t, handle).ref = MAPTRACK_TAIL;
+    v = d->vcpu[maptrack_entry(t,handle).vcpu];
+    cur_tail = v->maptrack_tail;
+    do {
+        prev_tail = cur_tail;
+        cur_tail = cmpxchg((u32 *)&v->maptrack_tail, prev_tail, handle);
+    } while ( cur_tail != prev_tail );

Thinking out loud here: this is the matching code that needs a tail entry to append to. Because we advance the tail pointer befiore threading the linst in the .ref field, there's a potentially large set of half-freed entries here that are/were the tail pointer but haven't had their .ref fields updated yet. Eventually all freeing threads will execute the line below and stitch things together. And cmpxchg() is a full barrier so we're safe against reordering here: any fragment of list will end in MAPTRACK_TAIL. Good.

But this:

xen/common/grant_table.c static int __get_paged_frame(unsigned long gfn, unsigned long *frame, struct pag · line 292

Quoted 1 line
xen/common/grant_table.c +1 -0
+    maptrack_entry(t, prev_tail).ref = handle;

ought to be a write_atomic(), and the reads in
__get_maptrack_handle() should be atomic too.

Quoted 39 lines
xen/common/grant_table.c +12 -14
 }
 
 static inline int
 get_maptrack_handle(
     struct grant_table *lgt)
 {
+    struct vcpu          *v = current;
     int                   i;
     grant_handle_t        handle;
     struct grant_mapping *new_mt;
-    unsigned int          new_mt_limit, nr_frames;
-
-    spin_lock(&lgt->maptrack_lock);
+    unsigned int          nr_frames;
 
-    while ( unlikely((handle = __get_maptrack_handle(lgt)) == -1) )
-    {
-        nr_frames = nr_maptrack_frames(lgt);
-        if ( nr_frames >= max_maptrack_frames )
-            break;
-
-        new_mt = alloc_xenheap_page();
-        if ( !new_mt )
-            break;
+    handle = __get_maptrack_handle(lgt, v);
+    if ( likely(handle != -1) )
+        return handle;
 
-        clear_page(new_mt);
+    nr_frames = nr_vcpu_maptrack_frames(v);
+    if ( nr_frames >= max_maptrack_frames )
+        return -1;
 
-        new_mt_limit = lgt->maptrack_limit + MAPTRACK_PER_PAGE;
+    new_mt = alloc_xenheap_page();
+    if ( !new_mt )
+        return -1;
+    clear_page(new_mt);
 

xen/common/grant_table.c static int __get_paged_frame(unsigned long gfn, unsigned long *frame, struct pag · line 320

Quoted 10 lines
xen/common/grant_table.c +2 -7
-        for ( i = 1; i < MAPTRACK_PER_PAGE; i++ )
-            new_mt[i - 1].ref = lgt->maptrack_limit + i;
-        new_mt[i - 1].ref = lgt->maptrack_head;
-        lgt->maptrack_head = lgt->maptrack_limit;
+    spin_lock(&lgt->maptrack_lock);
 
-        lgt->maptrack[nr_frames] = new_mt;
-        smp_wmb();
-        lgt->maptrack_limit      = new_mt_limit;
+    for ( i = 1; i < MAPTRACK_PER_PAGE; i++ ){

Style nit: brace on its own line, please.

Quoted 5 lines
xen/common/grant_table.c +5 -0
+        new_mt[i - 1].ref = (lgt->maptrack_pages * MAPTRACK_PER_PAGE) + i;
+        new_mt[i - 1].vcpu = v->vcpu_id;
+    }
+    /* Set last entry vcpu and ref */
+    new_mt[i - 1].ref = v->maptrack_head;

xen/common/grant_table.c static int __get_paged_frame(unsigned long gfn, unsigned long *frame, struct pag · line 333

Quoted 10 lines
xen/common/grant_table.c +7 -2
+    new_mt[i - 1].vcpu = v->vcpu_id;
 
-        gdprintk(XENLOG_INFO, "Increased maptrack size to %u frames\n",
-                 nr_frames + 1);
+    v->maptrack_head = lgt->maptrack_pages * MAPTRACK_PER_PAGE;
+    if (v->maptrack_tail == MAPTRACK_TAIL)
+    {
+        v->maptrack_tail = (lgt->maptrack_pages * MAPTRACK_PER_PAGE)
+            + MAPTRACK_PER_PAGE - 1;
+        new_mt[i - 1].ref = MAPTRACK_TAIL;

This clause was slightly confusing to me until I realised that it's only for the case where no maptrack entries have been allocated to this vcpu at all before (because you keep at least one on the freelist). And therefore, there's no need to be careful about this update to maptrack_tail. I think that deserves a comment.

So I think my only concerns about this whole series are some comments and using atomic read & writes in the linked-list code. I'm happy to believe that you'll DTRT with all that, so for the next spin,

Cheers,

Tim.

#10 lore raw

Re: [PATCHv7 3/3] gnttab: use per-VCPU maptrack free lists

Quoted 58 lines · Tim Deegan
Quoted 57 lines · David Vrabel

From: Malcolm Crossley <[email protected]>

Performance analysis of aggregate network throughput with many VMs shows that performance is signficantly limited by contention on the maptrack lock when obtaining/releasing maptrack handles from the free list.

Instead of a single free list use a per-VCPU list. This avoids any contention when obtaining a handle. Handles must be released back to their original list and since this may occur on a different VCPU there is some contention on the destination VCPU's free list tail pointer (but this is much better than a per-domain lock).

A domain's maptrack limit is multiplied by the number of VCPUs. This ensures that a worst case domain that only performs grant table operations via one VCPU will not see a lower map track limit.

---
 xen/common/grant_table.c      |  131 ++++++++++++++++++++++++-----------------
 xen/include/xen/grant_table.h |    8 ++-
 xen/include/xen/sched.h       |    5 ++
 3 files changed, 87 insertions(+), 57 deletions(-)
xen/common/grant_table.c +10 -5
index 569b3d3..d8e7373 100644
@@ -118,9 +118,9 @@ struct gnttab_unmap_common {
     ((t)->maptrack[(e)/MAPTRACK_PER_PAGE][(e)%MAPTRACK_PER_PAGE])
 
 static inline unsigned int
-nr_maptrack_frames(struct grant_table *t)
+nr_vcpu_maptrack_frames(struct vcpu *v)
 {
-    return t->maptrack_limit / MAPTRACK_PER_PAGE;
+    return v->maptrack_limit / MAPTRACK_PER_PAGE;
 }
 
 #define MAPTRACK_TAIL (~0u)
@@ -255,66 +255,91 @@ static int __get_paged_frame(unsigned long gfn, unsigned long *frame, struct pag
 
 static inline int
 __get_maptrack_handle(
-    struct grant_table *t)
+    struct grant_table *t,
+    struct vcpu *v)
 {
-    unsigned int h;
-    if ( unlikely((h = t->maptrack_head) == MAPTRACK_TAIL) )
+    unsigned int head, next;
+
+    head = v->maptrack_head;
+    if ( unlikely(head == MAPTRACK_TAIL) )
+        return -1;
+
Quoted 8 lines

xen/common/grant_table.c static int __get_paged_frame(unsigned long gfn, unsigned long *frame, struct pag · line 269

Quoted 3 lines
xen/common/grant_table.c +2 -0
+    next = maptrack_entry(t, head).ref;
+    if ( unlikely(next == MAPTRACK_TAIL) )
         return -1;

So this is an extra restriction over the old code: the list shall never be empty. Is that to make freeing onto the tail of a remote freelist easier? If so can you please add a comment so that nobody drops this later?

Previously, on maptrack table init we allocated a page, with per vCPU maptrack we would need to allocate per vcpu. As domains could have large numbers of vCPU's, this patch change's the logic so that each vcpu maptrack entries are only allocated when they are needed (or are full). This behavioural change hopefully keeps the Xen memory overhead for large vCPU domains down.

We detect a vCPU without any allocated maptrack pages by setting it's head pointer to the MAPTRACK_TAIL define.

Quoted 30 lines
Quoted 19 lines
xen/common/grant_table.c +6 -6
-    t->maptrack_head = maptrack_entry(t, h).ref;
-    return h;
+
+    v->maptrack_head = next;
+
+    return head;
 }
 
 static inline void
 put_maptrack_handle(
     struct grant_table *t, int handle)
 {
-    spin_lock(&t->maptrack_lock);
-    maptrack_entry(t, handle).ref = t->maptrack_head;
-    t->maptrack_head = handle;
-    spin_unlock(&t->maptrack_lock);
+    struct domain *d = current->domain;
+    struct vcpu *v;

xen/common/grant_table.c static int __get_paged_frame(unsigned long gfn, unsigned long *frame, struct pag · line 291

Quoted 10 lines
xen/common/grant_table.c +10 -0
+    u32 prev_tail, cur_tail;
+
+    /* Set current hande to have TAIL reference */
+    maptrack_entry(t, handle).ref = MAPTRACK_TAIL;
+    v = d->vcpu[maptrack_entry(t,handle).vcpu];
+    cur_tail = v->maptrack_tail;
+    do {
+        prev_tail = cur_tail;
+        cur_tail = cmpxchg((u32 *)&v->maptrack_tail, prev_tail, handle);
+    } while ( cur_tail != prev_tail );
Quoted 8 lines

Thinking out loud here: this is the matching code that needs a tail entry to append to. Because we advance the tail pointer befiore threading the linst in the .ref field, there's a potentially large set of half-freed entries here that are/were the tail pointer but haven't had their .ref fields updated yet. Eventually all freeing threads will execute the line below and stitch things together. And cmpxchg() is a full barrier so we're safe against reordering here: any fragment of list will end in MAPTRACK_TAIL. Good.

That's the theory of how it works but you are correct that all accesses to ref must be atomic, otherwise we won't splice the linked lists back together again.

Quoted 7 lines

But this:

xen/common/grant_table.c static int __get_paged_frame(unsigned long gfn, unsigned long *frame, struct pag · line 292

Quoted 1 line
xen/common/grant_table.c +1 -0
+    maptrack_entry(t, prev_tail).ref = handle;

ought to be a write_atomic(), and the reads in
__get_maptrack_handle() should be atomic too.

You are right, we probably get away with it on x86 as it's a 32bit write but we should use the atomic helpers.

Quoted 69 lines
Quoted 40 lines
xen/common/grant_table.c +12 -14
 }
 
 static inline int
 get_maptrack_handle(
     struct grant_table *lgt)
 {
+    struct vcpu          *v = current;
     int                   i;
     grant_handle_t        handle;
     struct grant_mapping *new_mt;
-    unsigned int          new_mt_limit, nr_frames;
-
-    spin_lock(&lgt->maptrack_lock);
+    unsigned int          nr_frames;
 
-    while ( unlikely((handle = __get_maptrack_handle(lgt)) == -1) )
-    {
-        nr_frames = nr_maptrack_frames(lgt);
-        if ( nr_frames >= max_maptrack_frames )
-            break;
-
-        new_mt = alloc_xenheap_page();
-        if ( !new_mt )
-            break;
+    handle = __get_maptrack_handle(lgt, v);
+    if ( likely(handle != -1) )
+        return handle;
 
-        clear_page(new_mt);
+    nr_frames = nr_vcpu_maptrack_frames(v);
+    if ( nr_frames >= max_maptrack_frames )
+        return -1;
 
-        new_mt_limit = lgt->maptrack_limit + MAPTRACK_PER_PAGE;
+    new_mt = alloc_xenheap_page();
+    if ( !new_mt )
+        return -1;
+    clear_page(new_mt);
 

xen/common/grant_table.c static int __get_paged_frame(unsigned long gfn, unsigned long *frame, struct pag · line 320

Quoted 10 lines
xen/common/grant_table.c +2 -7
-        for ( i = 1; i < MAPTRACK_PER_PAGE; i++ )
-            new_mt[i - 1].ref = lgt->maptrack_limit + i;
-        new_mt[i - 1].ref = lgt->maptrack_head;
-        lgt->maptrack_head = lgt->maptrack_limit;
+    spin_lock(&lgt->maptrack_lock);
 
-        lgt->maptrack[nr_frames] = new_mt;
-        smp_wmb();
-        lgt->maptrack_limit      = new_mt_limit;
+    for ( i = 1; i < MAPTRACK_PER_PAGE; i++ ){

Style nit: brace on its own line, please.

Quoted 5 lines
xen/common/grant_table.c +5 -0
+        new_mt[i - 1].ref = (lgt->maptrack_pages * MAPTRACK_PER_PAGE) + i;
+        new_mt[i - 1].vcpu = v->vcpu_id;
+    }
+    /* Set last entry vcpu and ref */
+    new_mt[i - 1].ref = v->maptrack_head;

xen/common/grant_table.c static int __get_paged_frame(unsigned long gfn, unsigned long *frame, struct pag · line 333

Quoted 10 lines
xen/common/grant_table.c +7 -2
+    new_mt[i - 1].vcpu = v->vcpu_id;
 
-        gdprintk(XENLOG_INFO, "Increased maptrack size to %u frames\n",
-                 nr_frames + 1);
+    v->maptrack_head = lgt->maptrack_pages * MAPTRACK_PER_PAGE;
+    if (v->maptrack_tail == MAPTRACK_TAIL)
+    {
+        v->maptrack_tail = (lgt->maptrack_pages * MAPTRACK_PER_PAGE)
+            + MAPTRACK_PER_PAGE - 1;
+        new_mt[i - 1].ref = MAPTRACK_TAIL;
Quoted 5 lines

This clause was slightly confusing to me until I realised that it's only for the case where no maptrack entries have been allocated to this vcpu at all before (because you keep at least one on the freelist). And therefore, there's no need to be careful about this update to maptrack_tail. I think that deserves a comment.

As I detailed above, we have changed behaviour slightly so there is a case where there is nothing allocated on the freelist. We'll add a comment explaining this.

Quoted 7 lines

So I think my only concerns about this whole series are some comments and using atomic read & writes in the linked-list code. I'm happy to believe that you'll DTRT with all that, so for the next spin,

Thanks for your review.

Malcolm

Quoted 4 lines

Cheers,

Tim.

#11 lore raw

Re: [PATCHv7 3/3] gnttab: use per-VCPU maptrack free lists

In reply to David Vrabel
Quoted 75 lines
Quoted 1 line
Quoted 1 line · On 30.04.15 at 15:28, <[email protected]> wrote:

From: Malcolm Crossley <[email protected]>

Performance analysis of aggregate network throughput with many VMs shows that performance is signficantly limited by contention on the maptrack lock when obtaining/releasing maptrack handles from the free list.

Instead of a single free list use a per-VCPU list. This avoids any contention when obtaining a handle. Handles must be released back to their original list and since this may occur on a different VCPU there is some contention on the destination VCPU's free list tail pointer (but this is much better than a per-domain lock).

A domain's maptrack limit is multiplied by the number of VCPUs. This ensures that a worst case domain that only performs grant table operations via one VCPU will not see a lower map track limit.

---
 xen/common/grant_table.c      |  131 ++++++++++++++++++++++++-----------------
 xen/include/xen/grant_table.h |    8 ++-
 xen/include/xen/sched.h       |    5 ++
 3 files changed, 87 insertions(+), 57 deletions(-)
xen/common/grant_table.c +2 -2
index 569b3d3..d8e7373 100644
@@ -118,9 +118,9 @@ struct gnttab_unmap_common {
     ((t)->maptrack[(e)/MAPTRACK_PER_PAGE][(e)%MAPTRACK_PER_PAGE])
 
 static inline unsigned int
-nr_maptrack_frames(struct grant_table *t)
+nr_vcpu_maptrack_frames(struct vcpu *v)
 {
-    return t->maptrack_limit / MAPTRACK_PER_PAGE;
+    return v->maptrack_limit / MAPTRACK_PER_PAGE;
 }
 
 #define MAPTRACK_TAIL (~0u)
@@ -255,66 +255,91 @@ static int __get_paged_frame(unsigned long gfn, 

unsigned long *frame, struct pag

static inline int
__get_maptrack_handle(

xen/common/grant_table.c +14 -6
-    struct grant_table *t)
+    struct grant_table *t,
+    struct vcpu *v)
 {
-    unsigned int h;
-    if ( unlikely((h = t->maptrack_head) == MAPTRACK_TAIL) )
+    unsigned int head, next;
+
+    head = v->maptrack_head;
+    if ( unlikely(head == MAPTRACK_TAIL) )
+        return -1;
+
+    next = maptrack_entry(t, head).ref;
+    if ( unlikely(next == MAPTRACK_TAIL) )
         return -1;
-    t->maptrack_head = maptrack_entry(t, h).ref;
-    return h;
+
+    v->maptrack_head = next;
+
+    return head;
 }
 
 static inline void
 put_maptrack_handle(
     struct grant_table *t, int handle)
 {
-    spin_lock(&t->maptrack_lock);

xen/common/grant_table.c static int __get_paged_frame(unsigned long gfn, unsigned long *frame, struct pag · line 286

Quoted 10 lines
xen/common/grant_table.c +7 -3
-    maptrack_entry(t, handle).ref = t->maptrack_head;
-    t->maptrack_head = handle;
-    spin_unlock(&t->maptrack_lock);
+    struct domain *d = current->domain;
+    struct vcpu *v;
+    u32 prev_tail, cur_tail;
+
+    /* Set current hande to have TAIL reference */
+    maptrack_entry(t, handle).ref = MAPTRACK_TAIL;
+    v = d->vcpu[maptrack_entry(t,handle).vcpu];

There's a missing blank here; the comment above has a typo and is missing a stop. (There are further similar issues below.)

Quoted 1 line

+ cur_tail = v->maptrack_tail;

read_atomic()?

xen/common/grant_table.c static int __get_paged_frame(unsigned long gfn, unsigned long *frame, struct pag · line 290

Quoted 3 lines
xen/common/grant_table.c +3 -0
+    do {
+        prev_tail = cur_tail;
+        cur_tail = cmpxchg((u32 *)&v->maptrack_tail, prev_tail, handle);

Please adjust the type of maptrack_tail (or the types of {prev,cur}_tail) such that no cast is needed here.

Quoted 52 lines
xen/common/grant_table.c +17 -21
+    } while ( cur_tail != prev_tail );
+    maptrack_entry(t, prev_tail).ref = handle;
 }
 
 static inline int
 get_maptrack_handle(
     struct grant_table *lgt)
 {
+    struct vcpu          *v = current;
     int                   i;
     grant_handle_t        handle;
     struct grant_mapping *new_mt;
-    unsigned int          new_mt_limit, nr_frames;
-
-    spin_lock(&lgt->maptrack_lock);
+    unsigned int          nr_frames;
 
-    while ( unlikely((handle = __get_maptrack_handle(lgt)) == -1) )
-    {
-        nr_frames = nr_maptrack_frames(lgt);
-        if ( nr_frames >= max_maptrack_frames )
-            break;
-
-        new_mt = alloc_xenheap_page();
-        if ( !new_mt )
-            break;
+    handle = __get_maptrack_handle(lgt, v);
+    if ( likely(handle != -1) )
+        return handle;
 
-        clear_page(new_mt);
+    nr_frames = nr_vcpu_maptrack_frames(v);
+    if ( nr_frames >= max_maptrack_frames )
+        return -1;
 
-        new_mt_limit = lgt->maptrack_limit + MAPTRACK_PER_PAGE;
+    new_mt = alloc_xenheap_page();
+    if ( !new_mt )
+        return -1;
+    clear_page(new_mt);
 
-        for ( i = 1; i < MAPTRACK_PER_PAGE; i++ )
-            new_mt[i - 1].ref = lgt->maptrack_limit + i;
-        new_mt[i - 1].ref = lgt->maptrack_head;
-        lgt->maptrack_head = lgt->maptrack_limit;
+    spin_lock(&lgt->maptrack_lock);
 
-        lgt->maptrack[nr_frames] = new_mt;
-        smp_wmb();
-        lgt->maptrack_limit      = new_mt_limit;
+    for ( i = 1; i < MAPTRACK_PER_PAGE; i++ ){
+        new_mt[i - 1].ref = (lgt->maptrack_pages * MAPTRACK_PER_PAGE) + i;

xen/common/grant_table.c static int __get_paged_frame(unsigned long gfn, unsigned long *frame, struct pag · line 329

Quoted 10 lines
xen/common/grant_table.c +7 -2
+        new_mt[i - 1].vcpu = v->vcpu_id;
+    }
+    /* Set last entry vcpu and ref */
+    new_mt[i - 1].ref = v->maptrack_head;
+    new_mt[i - 1].vcpu = v->vcpu_id;
 
-        gdprintk(XENLOG_INFO, "Increased maptrack size to %u frames\n",
-                 nr_frames + 1);
+    v->maptrack_head = lgt->maptrack_pages * MAPTRACK_PER_PAGE;
+    if (v->maptrack_tail == MAPTRACK_TAIL)

Doesn't this imply v->maptrack_head == MAPTRACK_TAIL (before the assignment right above) and hence ...

xen/common/grant_table.c static int __get_paged_frame(unsigned long gfn, unsigned long *frame, struct pag · line 333

Quoted 4 lines
xen/common/grant_table.c +4 -0
+    {
+        v->maptrack_tail = (lgt->maptrack_pages * MAPTRACK_PER_PAGE)
+            + MAPTRACK_PER_PAGE - 1;
+        new_mt[i - 1].ref = MAPTRACK_TAIL;

... this being redundant with the setting of the last entry above? In which case this could better be an ASSERT() than an assignment.

xen/common/grant_table.c static void mapcount( · line 594

Quoted 6 lines
xen/common/grant_table.c +1 -1
@@ -566,7 +591,7 @@ static void mapcount(
     ASSERT(rw_is_locked(&rd->grant_table->lock));
     spin_lock(&lgt->maptrack_lock);
 
-    for ( handle = 0; handle < lgt->maptrack_limit; handle++ )
+    for ( handle = 0; handle < lgt->maptrack_pages * MAPTRACK_PER_PAGE; handle++ )

Long line.

xen/common/grant_table.c gnttab_setup_table( · line 1462

Quoted 8 lines
xen/common/grant_table.c +4 -0
@@ -1430,6 +1456,17 @@ gnttab_setup_table(
     gt = d->grant_table;
     write_lock(&gt->lock);
 
+    /* Tracking of mapped foreign frames table */
+    if ( (gt->maptrack = xzalloc_array(struct grant_mapping *,
+                                       max_maptrack_frames * d->max_vcpus)) == NULL )
+        goto out3;

This surely can easily become an allocation of far more than a page, and hence needs to be broken up (perhaps using vmap() to map individually allocated pages).

xen/common/grant_table.c gnttab_setup_table( · line 1468

Quoted 6 lines
xen/common/grant_table.c +6 -0
+    for_each_vcpu( d, v )
+    {
+        v->maptrack_head = MAPTRACK_TAIL;
+        v->maptrack_tail = MAPTRACK_TAIL;
+    }
+    gt->maptrack_pages = 0;

I don't think this is needed. Or if it is (i.e. the value can be reset here) then doing the allocation above unconditionally would look wrong too. (I suppose it gets moved here because of the dependency on d->max_vcpus.) Indeed I don't see anything preventing a guest from invoking GNTTABOP_setup_table more than once.

xen/common/grant_table.c grant_table_create( · line 3098

Quoted 12 lines
xen/common/grant_table.c +1 -3
@@ -3066,12 +3091,10 @@ grant_table_create(
         free_xenheap_page(t->shared_raw[i]);
     xfree(t->shared_raw);
  no_mem_3:
-    free_xenheap_page(t->maptrack[0]);
-    xfree(t->maptrack);
- no_mem_2:
     for ( i = 0;
           i < num_act_frames_from_sha_frames(INITIAL_NR_GRANT_FRAMES); i++ )
         free_xenheap_page(t->active[i]);
+ no_mem_2:
     xfree(t->active);

This movement of the no_mem_2 label looks wrong.

Jan

#12 lore raw

Re: [PATCHv7 3/3] gnttab: use per-VCPU maptrack free lists

Quoted 17 lines · Jan Beulich
Quoted 17 lines
Quoted 1 line
Quoted 1 line · On 30.04.15 at 15:28, <[email protected]> wrote:

From: Malcolm Crossley <[email protected]>

Performance analysis of aggregate network throughput with many VMs shows that performance is signficantly limited by contention on the maptrack lock when obtaining/releasing maptrack handles from the free list.

Instead of a single free list use a per-VCPU list. This avoids any contention when obtaining a handle. Handles must be released back to their original list and since this may occur on a different VCPU there is some contention on the destination VCPU's free list tail pointer (but this is much better than a per-domain lock).

A domain's maptrack limit is multiplied by the number of VCPUs. This ensures that a worst case domain that only performs grant table operations via one VCPU will not see a lower map track limit.

[...]

Quoted 3 lines
Quoted 1 line

+ cur_tail = v->maptrack_tail;

read_atomic()?

It's not required since if this load gets inconsistent state, the cmpxchg loop will just go around once more. I've added the read_atomic() anyway, though.

Quoted 4 lines
Quoted 4 lines
xen/common/grant_table.c +0 -0
@@ -1430,6 +1456,17 @@ gnttab_setup_table(
     gt = d->grant_table;
     write_lock(&gt->lock);
 
Quoted 8 lines

xen/common/grant_table.c gnttab_setup_table( · line 1462

Quoted 4 lines
xen/common/grant_table.c +4 -0
+    /* Tracking of mapped foreign frames table */
+    if ( (gt->maptrack = xzalloc_array(struct grant_mapping *,
+                                       max_maptrack_frames * d->max_vcpus)) == NULL )
+        goto out3;

This surely can easily become an allocation of far more than a page, and hence needs to be broken up (perhaps using vmap() to map individually allocated pages).

I think there should be a common vzalloc_array() function. Do you agree? This will use xzalloc_array() if the alloc is < PAGE_SIZE to avoid needlessly using vmap space.

David

#13 lore raw

Re: [PATCHv7 3/3] gnttab: use per-VCPU maptrack free lists

Quoted 16 lines
Quoted 1 line
Quoted 1 line · On 12.05.15 at 13:01, <[email protected]> wrote:
Quoted 5 lines · Jan Beulich
Quoted 5 lines
Quoted 1 line
Quoted 1 line · On 30.04.15 at 15:28, <[email protected]> wrote:
xen/common/grant_table.c +0 -0
@@ -1430,6 +1456,17 @@ gnttab_setup_table(
     gt = d->grant_table;
     write_lock(&gt->lock);
 
Quoted 8 lines

xen/common/grant_table.c gnttab_setup_table( · line 1462

Quoted 4 lines
xen/common/grant_table.c +4 -0
+    /* Tracking of mapped foreign frames table */
+    if ( (gt->maptrack = xzalloc_array(struct grant_mapping *,
+                                       max_maptrack_frames * d->max_vcpus)) == NULL )
+        goto out3;

This surely can easily become an allocation of far more than a page, and hence needs to be broken up (perhaps using vmap() to map individually allocated pages).

Quoted 3 lines

I think there should be a common vzalloc_array() function. Do you agree? This will use xzalloc_array() if the alloc is < PAGE_SIZE to avoid needlessly using vmap space.

For the _array flavor I'm not sure, but vmalloc()/vzalloc() are already in the process of being added.

Jan

#14 lore raw

Re: [PATCHv7 3/3] gnttab: use per-VCPU maptrack free lists

Quoted 16 lines · Jan Beulich
Quoted 16 lines
Quoted 1 line
Quoted 1 line · On 12.05.15 at 13:01, <[email protected]> wrote:
Quoted 13 lines · Jan Beulich

xen/common/grant_table.c gnttab_setup_table( · line 1462

Quoted 9 lines
Quoted 1 line
Quoted 1 line · On 30.04.15 at 15:28, <[email protected]> wrote:
xen/common/grant_table.c +4 -0
@@ -1430,6 +1456,17 @@ gnttab_setup_table(
     gt = d->grant_table;
     write_lock(&gt->lock);
 
+    /* Tracking of mapped foreign frames table */
+    if ( (gt->maptrack = xzalloc_array(struct grant_mapping *,
+                                       max_maptrack_frames * d->max_vcpus)) == NULL )
+        goto out3;

This surely can easily become an allocation of far more than a page, and hence needs to be broken up (perhaps using vmap() to map individually allocated pages).

Quoted 6 lines
Quoted 3 lines

I think there should be a common vzalloc_array() function. Do you agree? This will use xzalloc_array() if the alloc is < PAGE_SIZE to avoid needlessly using vmap space.

For the _array flavor I'm not sure, but vmalloc()/vzalloc() are already in the process of being added.

Can I leave this as-is (with the xzalloc_array()) and fix it up once vzalloc() exists?

David

#15 lore raw

Re: [PATCHv7 3/3] gnttab: use per-VCPU maptrack free lists

Quoted 22 lines
Quoted 1 line
Quoted 1 line · On 12.05.15 at 14:37, <[email protected]> wrote:
Quoted 20 lines · Jan Beulich
Quoted 19 lines
Quoted 1 line
Quoted 1 line · On 12.05.15 at 13:01, <[email protected]> wrote:
Quoted 5 lines · Jan Beulich
Quoted 5 lines
Quoted 1 line
Quoted 1 line · On 30.04.15 at 15:28, <[email protected]> wrote:
xen/common/grant_table.c +0 -0
@@ -1430,6 +1456,17 @@ gnttab_setup_table(
     gt = d->grant_table;
     write_lock(&gt->lock);
 
Quoted 8 lines

xen/common/grant_table.c gnttab_setup_table( · line 1462

Quoted 4 lines
xen/common/grant_table.c +4 -0
+    /* Tracking of mapped foreign frames table */
+    if ( (gt->maptrack = xzalloc_array(struct grant_mapping *,
+                                       max_maptrack_frames * d->max_vcpus)) == NULL )
+        goto out3;

This surely can easily become an allocation of far more than a page, and hence needs to be broken up (perhaps using vmap() to map individually allocated pages).

I think there should be a common vzalloc_array() function. Do you agree? This will use xzalloc_array() if the alloc is < PAGE_SIZE to avoid needlessly using vmap space.

Quoted 5 lines
Quoted 2 lines

For the _array flavor I'm not sure, but vmalloc()/vzalloc() are already in the process of being added.

Can I leave this as-is (with the xzalloc_array()) and fix it up once vzalloc() exists?

Sure.

Jan