gnttab: Improve scaleability
v8 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
v8:
* Misc locking fixes.
* Use read/write_atomic() in per-VCPU maptrack free list.
* Improve locking docs.
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
PATCH v8 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.
--- docs/misc/grant-tables.txt | 31 ++++++++- xen/arch/arm/mm.c | 4 +- xen/arch/x86/mm.c | 4 +- xen/common/grant_table.c | 138 +++++++++++++++++++++++------------------ xen/include/xen/grant_table.h | 11 +++- 5 files changed, 119 insertions(+), 69 deletions(-)
docs/misc/grant-tables.txt +30 -1
@@ -74,7 +74,36 @@ 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. + + The maptrack lock may be locked while holding the read or write grant + table lock. ********************************************************************************
xen/arch/arm/mm.c +2 -2
@@ -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
@@ -4593,7 +4593,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 ;
@@ -4615,7 +4615,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 +77 -61
456 lines not on this page · raw message
xen/include/xen/grant_table.h +8 -3
@@ -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
Re: [PATCHv8 1/3] gnttab: Introduce rwlock to protect updates to grant table state
Quoted 11 lines
Quoted 1 line
Quoted 1 line · On 12.05.15 at 16:15, <[email protected]> wrote:
xen/common/grant_table.c +1
-1@@ -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,
xen/common/grant_table.c __gnttab_map_grant_ref( · line 705
Quoted 5 lines
xen/common/grant_table.c +1
-1@@ -702,7 +702,7 @@ __gnttab_map_grant_ref( cache_flags = (shah->flags & (GTF_PAT |GTF_PWT |GTF_PCD ) ); -spin_unlock (&rgt->lock); +read_unlock (&rgt->lock);
Lock dropped once, and ...
Quoted 19 lines
xen/common/grant_table.c +2
-2@@ -778,7 +778,7 @@ __gnttab_map_grant_ref( goto undo_out; } -double_gt_lock (lgt, rgt); +double_maptrack_lock (lgt, rgt);if (gnttab_need_iommu_mapping (ld) ) {@@ -801,7 +801,7 @@ __gnttab_map_grant_ref( }if ( err ) { -double_gt_unlock (lgt, rgt); +double_maptrack_unlock (lgt, rgt); rc = GNTST_general_error; goto undo_out; }
xen/common/grant_table.c __gnttab_map_grant_ref( · line 818
Quoted 6 lines
xen/common/grant_table.c +2
-1@@ -814,7 +814,8 @@ __gnttab_map_grant_ref( mt->ref = op->ref; mt->flags = op->flags; -double_gt_unlock (lgt, rgt); +double_maptrack_unlock (lgt, rgt); +read_unlock (&rgt->lock);
... again without being re-acquired another time. Since this appears to be a recurring theme on this first patch - did this patch alone ever get tested (I'm sure I'll again find one of the following patches to fix this issue as a "side effect")?
Quoted 12 lines
xen/common/grant_table.c +3
-1@@ -939,7 +941,9 @@ __gnttab_unmap_common(TRACE_1D (TRC_MEM_PAGE_GRANT_UNMAP , dom); rgt = rd->grant_table; -double_gt_lock (lgt, rgt); + +read_lock (&rgt->lock); +double_maptrack_lock (lgt, rgt); op->flags = op->map->flags;if (unlikely (!op->flags) ||unlikely (op->map->domid != dom) )
xen/common/grant_table.c __gnttab_unmap_common( · line 1017
Quoted 6 lines
xen/common/grant_table.c +2
-1@@ -1009,7 +1013,9 @@ __gnttab_unmap_common(gnttab_mark_dirty (rd, op->frame); unmap_out: -double_gt_unlock (lgt, rgt); +double_maptrack_unlock (lgt, rgt); +read_unlock (&rgt->lock);
Am I right to conclude from this that the fix for the issue above is to add a read_lock(), not to drop the unbalanced read_unlock()?
Which then of course raises the question - is taking the read lock here and in several other places really sufficient? The thing that the original spin lock appears to protect here is not only the grant table structure itself, but also the active entry. I.e. relaxing to a read lock would seem possible only after having put per-active-entry locking in place.
Quoted 13 lines
xen/include/xen/grant_table.h +5
-0@@ -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). */
xen/include/xen/grant_table.h struct grant_table { · line 94
Quoted 10 lines
xen/include/xen/grant_table.h +2
-2@@ -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;
So in order for fields protected by one of the locks to be as likely as possible on the same cache line as the lock itself, I think gt_version should also be moved up in the structure. We may even want/need to add some separator between basic and maptrack data to ensure they end up on different cache lines.
Jan
Re: [PATCHv8 1/3] gnttab: Introduce rwlock to protect updates to grant table state
Quoted 7 lines · Jan Beulich
Which then of course raises the question - is taking the read lock here and in several other places really sufficient? The thing that the original spin lock appears to protect here is not only the grant table structure itself, but also the active entry. I.e. relaxing to a read lock would seem possible only after having put per-active-entry locking in place.
Yes, this series was split the wrong way round. I could:
a) squash the first two patches back together;
b) refactor the first two patches into
- introduce active entry locks
- introduce maptrack lock
- convert grant table lock to rw lock.
Which approach is preferred (obviously (a) is a lot less work)?
Quoted 21 lines
Quoted 21 lines
xen/include/xen/grant_table.h +7
-2@@ -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
Quoted 8 lines
xen/include/xen/grant_table.h struct grant_table { · line 94
Quoted 2 lines
xen/include/xen/grant_table.h +0
-0what version to use yet. */ unsigned gt_version;So in order for fields protected by one of the locks to be as likely as possible on the same cache line as the lock itself, I think
gt_versionshould also be moved up in the structure. We may even want/need to add some separator between basic and maptrack data to ensure they end up on different cache lines.
I think this sort of structure field shuffling should be a follow on patch.
David
Re: [PATCHv8 1/3] gnttab: Introduce rwlock to protect updates to grant table state
Quoted 12 lines
Quoted 1 line
Quoted 1 line · On 19.05.15 at 15:20, <[email protected]> wrote:
Quoted 7 lines · Jan Beulich
Which then of course raises the question - is taking the read lock here and in several other places really sufficient? The thing that the original spin lock appears to protect here is not only the grant table structure itself, but also the active entry. I.e. relaxing to a read lock would seem possible only after having put per-active-entry locking in place.
Yes, this series was split the wrong way round. I could:
Quoted 7 lines
a) squash the first two patches back together;
b) refactor the first two patches into
- introduce active entry locks
- introduce maptrack lock
- convert grant table lock to rw lock.Which approach is preferred (obviously (a) is a lot less work)?
Unless it results in a very hard to review patch, I guess I'd be fine with (a). Another alternative I would see is to have the current first patch use write_lock() in most places, and convert them to read_lock() as the active entry locks get introduced (which I'd expect to still be less work than (b)).
Quoted 30 lines
Quoted 21 lines
Quoted 21 lines
xen/include/xen/grant_table.h +7
-2@@ -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 Quoted 8 lines
xen/include/xen/grant_table.h struct grant_table { · line 94
Quoted 2 lines
xen/include/xen/grant_table.h +0
-0what version to use yet. */ unsigned gt_version;So in order for fields protected by one of the locks to be as likely as possible on the same cache line as the lock itself, I think
gt_versionshould also be moved up in the structure. We may even want/need to add some separator between basic and maptrack data to ensure they end up on different cache lines.
Quoted 1 line
I think this sort of structure field shuffling should be a follow on patch.
Sure, if you so prefer (with "lock" being moved I personally wouldn't mind gt_version to also move).
Jan
PATCH v8 2/3 gnttab: per-active entry locking
In reply to David VrabelFrom: Matt Wilson <[email protected]>
Introduce a per-active entry spin lock to protect active entry state instead of requiring a double maptrack lock. The grant table read lock must be locked before acquiring (locking) a active entry.
The reduces lock contention on grant map/unmap to the maptrack lock during maptrack handle get/put, significantly improving performance with many concurrent map/unmap operations.
--- docs/misc/grant-tables.txt | 25 ++++++ xen/common/grant_table.c | 212 +++++++++++++++++++++++++++----------------- 2 files changed, 154 insertions(+), 83 deletions(-)
docs/misc/grant-tables.txt +25 -0
@@ -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
@@ -105,6 +108,28 @@ is complete. The maptrack lock may be locked while holding the read or write grant table lock. + 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(>->lock); + act = active_entry_acquire(gt, ref); + ... + active_entry_release(act); + read_unlock(>->lock); + + Active entries cannot be acquired while holding the maptrack lock. + Multiple active entries can only be acquired while holding the grant + table _write_ lock. + ******************************************************************************** Granting a foreign domain access to frames
xen/common/grant_table.c +129 -83
554 lines not on this page · raw message
Signature · 1 line
1.7.10.4
Re: [PATCHv8 2/3] gnttab: per-active entry locking
xen/common/grant_table.c nr_active_grant_frames(struct grant_table *gt) · line 202
Quoted 10 lines
Quoted 1 line
Quoted 1 line · On 12.05.15 at 16:15, <[email protected]> wrote:
xen/common/grant_table.c +9
-0+static inline struct active_grant_entry * +active_entry_acquire (struct grant_table *t,grant_ref_t e) +{ +struct active_grant_entry *act; + +/* +* not perfect, but better than nothing for a debug build +* sanity check +*/
Comment style.
xen/common/grant_table.c static void mapcount( · line 565
Quoted 13 lines
xen/common/grant_table.c +9
-0@@ -546,15 +554,28 @@ static void mapcount( *wrc = *rdc =0 ; +/* +* N.B.: while taking the local maptrack spinlock prevents any +* mapping changes, the remote active entries could be changing +* while we are counting. The caller has to hold the grant table +* write lock, or some other mechanism should be used to prevent +* concurrent changes during this operation. This is tricky +* because we can't promote a read lock into a write lock. +*/ +ASSERT (rw_is_locked (&rd->grant_table->lock));
So both callers only hold the read lock - what is this "other mechanism" that they employ?
xen/common/grant_table.c __gnttab_swap_grant_ref(grant_ref_t ref_a, grant_ref_t ref_b) · line 2639
Quoted 13 lines
xen/common/grant_table.c +6
-1@@ -2602,9 +2629,17 @@ __gnttab_swap_grant_ref(grant_ref_t ref_a, grant_ref_t ref_b) {struct domain *d =rcu_lock_current_domain ();struct grant_table *gt = d->grant_table; -struct active_grant_entry *act; +struct active_grant_entry *act_a =NULL ; +struct active_grant_entry *act_b =NULL ;s16 rc = GNTST_okay; +if ( ref_a == ref_b ) +/* +* noop, so avoid acquiring the same active entry +* twice which would case a deadlock.
cause
Jan
Re: [PATCHv8 2/3] gnttab: per-active entry locking
Quoted 5 lines · Jan Beulich
Quoted 5 lines
Quoted 1 line
Quoted 1 line · On 12.05.15 at 16:15, <[email protected]> wrote:
xen/common/grant_table.c +0
-0@@ -546,15 +554,28 @@ static void mapcount( *wrc = *rdc =0 ;
Quoted 12 lines
xen/common/grant_table.c static void mapcount( · line 565
Quoted 9 lines
xen/common/grant_table.c +9
-0+/* +* N.B.: while taking the local maptrack spinlock prevents any +* mapping changes, the remote active entries could be changing +* while we are counting. The caller has to hold the grant table +* write lock, or some other mechanism should be used to prevent +* concurrent changes during this operation. This is tricky +* because we can't promote a read lock into a write lock. +*/ +ASSERT (rw_is_locked (&rd->grant_table->lock));So both callers only hold the read lock - what is this "other mechanism" that they employ?
This comment is just nonsense. We must hold the grant table write lock for mapcount() to be safe -- the maptrack lock doesn't help and as you say, this "other mechanism" just doesn't exist.
I think the callers of mapcount() need to be refactored so iommu updates and the mapcount() call are done with the write lock held only (the maptrack nor active entry locks are relevant here).
David
PATCH v8 3/3 gnttab: use per-VCPU maptrack free lists
In reply to David VrabelFrom: 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 | 139 ++++++++++++++++++++++++++--------------- xen/include/xen/grant_table.h | 8 ++- xen/include/xen/sched.h | 5 ++ 3 files changed, 97 insertions(+), 55 deletions(-)
xen/common/grant_table.c +87 -52
@@ -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,101 @@ static int __get_paged_frame(unsigned long gfn, unsigned long *frame, struct pagstatic 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; + +/* No maptrack pages allocated for this VCPU yet? */ + head = v->maptrack_head; +if (unlikely (head ==MAPTRACK_TAIL ) ) +return -1 ; + +/* +* Always keep one entry in the free list to make it easier to add +* free entries to the tail. +*/ + next =read_atomic (&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; +unsigned int prev_tail, cur_tail; + +/* 1. Set entry to be a tail. */ +maptrack_entry (t, handle).ref =MAPTRACK_TAIL ; + +/* 2. Add entry to the tail of the list on the original VCPU. */ + v = d->vcpu[maptrack_entry (t,handle).vcpu]; + + cur_tail =read_atomic (&v->maptrack_tail); +do { + prev_tail = cur_tail; + cur_tail =cmpxchg (&v->maptrack_tail, prev_tail, handle); + }while ( cur_tail != prev_tail ); + +/* 3. Update the old tail entry to point to the new entry. */ +write_atomic (&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; +unsigned int nr_frames; -spin_lock (&lgt->maptrack_lock); + handle =__get_maptrack_handle (lgt, v); +if (likely (handle !=-1 ) ) +return handle; -while (unlikely ((handle =__get_maptrack_handle (lgt)) ==-1 ) ) - { - nr_frames =nr_maptrack_frames (lgt); -if ( nr_frames >= max_maptrack_frames ) -break ; + nr_frames =nr_vcpu_maptrack_frames (v); +if ( nr_frames >= max_maptrack_frames ) +return -1 ; - new_mt =alloc_xenheap_page (); -if ( !new_mt ) -break ; + new_mt =alloc_xenheap_page (); +if ( !new_mt ) +return -1 ; +clear_page (new_mt); -clear_page (new_mt); +spin_lock (&lgt->maptrack_lock); - new_mt_limit = lgt->maptrack_limit +MAPTRACK_PER_PAGE ; +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; -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; + v->maptrack_head = lgt->maptrack_pages *MAPTRACK_PER_PAGE ; - lgt->maptrack[nr_frames] = new_mt; -smp_wmb (); - lgt->maptrack_limit = new_mt_limit; +/* Set tail directly if this is the first page for this VCPU. */ +if ( v->maptrack_tail ==MAPTRACK_TAIL ) + v->maptrack_tail = (lgt->maptrack_pages *MAPTRACK_PER_PAGE ) + +MAPTRACK_PER_PAGE -1 ; -gdprintk (XENLOG_INFO ,"Increased maptrack size to %u frames\n" , - nr_frames +1 ); - } + 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); }/*
@@ -565,7 +600,8 @@ 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)) ||
@@ -913,7 +949,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;
@@ -1386,6 +1422,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;
@@ -1427,6 +1464,17 @@ gnttab_setup_table( gt = d->grant_table;write_lock (>->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 ;
@@ -3017,18 +3065,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;
@@ -3059,8 +3095,6 @@ 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++ )
@@ -3088,7 +3122,8 @@ 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)) )
@@ -3193,7 +3228,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
@@ -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
@@ -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
Quoted 13 lines
Quoted 1 line
Quoted 1 line · On 12.05.15 at 16:15, <[email protected]> wrote:
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
Quoted 7 lines
Base 9.7
Split locks 25.8
Split locks 42.9
+ per-VCPU maptrack listsv8:
* Misc locking fixes.
This is of course rather unhelpful a comment, especially without the patches themselves also having an updates-in-v8 section. May I please ask that going forward you include at least the most recent revision's changes in each patch?
Jan