Skip to contentSkip to conversation

gnttab: Improve scaleability

PATCH Superseded Christoph Egger 2 participants 7 messages Jan 9, 2015 – Jan 12, 2015

v4 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

v4 cover gnttab: Improve scaleability

This patch series changes the grant table locking to a more fain grained locking protocol. The result is a performance boost measured with blkfront/blkback. Document the locking protocol.

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

v3:
* Addressed gnttab_swap_grant_ref() comment from Andrew Cooper
v2:
* Add arm part per request from Julien Grall

Christoph Egger (1):
gnttab: Introduce rwlock to protect updates to grant table state

Matt Wilson (1):
gnttab: refactor locking for scalability

 docs/misc/grant-tables.txt    |   49 ++++++-
 xen/arch/arm/mm.c             |    4 +-
 xen/arch/x86/mm.c             |    4 +-
 xen/common/grant_table.c      |  321 +++++++++++++++++++++++++----------------
 xen/include/xen/grant_table.h |    9 +-
 5 files changed, 258 insertions(+), 129 deletions(-)
Signature · 1 line
1.7.9.5
#1 lore raw

PATCH v4 1/2 gnttab: Introduce rwlock to protect updates to grant table state

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]

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
v3:
* Addressed gnttab_swap_grant_ref() comment from Andrew Cooper
v2:
* Add arm part per request from Julien Grall

Cc Jan Beulich <[email protected]>
Cc Keir Fraser <[email protected]>
Cc Julien Grall <[email protected]>
---
 docs/misc/grant-tables.txt    |   28 ++++++++-
 xen/arch/arm/mm.c             |    4 +-
 xen/arch/x86/mm.c             |    4 +-
 xen/common/grant_table.c      |  134 +++++++++++++++++++++++------------------
 xen/include/xen/grant_table.h |   17 ++++--
 5 files changed, 117 insertions(+), 70 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 7d4ba0c..2765683 100644
@@ -1037,7 +1037,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;
@@ -1067,7 +1067,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 6e9c2c0..d371ff9 100644
@@ -4565,7 +4565,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;
@@ -4587,7 +4587,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 +74 -60

472 lines not on this page · raw message

xen/include/xen/grant_table.h +12 -5
index 32f5786..298031e 100644
@@ -82,10 +82,17 @@ 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;
-    /* The defined versions are 1 and 2.  Set to 0 if we don't know
-       what version to use yet. */
+    /* Lock protecting the maptrack page list, head, and limit */
+    spinlock_t            maptrack_lock;
+    /* 
+     * Lock protecting updates to grant table state (version, active
+     * entry list, etc.)
+     */
+    rwlock_t              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 +108,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.9.5
#2 lore raw

Re: [PATCH v4 1/2] gnttab: Introduce rwlock to protect updates to grant table state

Quoted 6 lines
Quoted 1 line
Quoted 1 line · On 09.01.15 at 16:12, <[email protected]> wrote:
xen/common/grant_table.c +1 -0
@@ -899,26 +899,27 @@ __gnttab_unmap_common(
 
     op->frame = (unsigned long)(op->dev_bus_addr >> PAGE_SHIFT);
 
+    read_lock(&lgt->lock);

xen/common/grant_table.c __gnttab_unmap_common( · line 910

Quoted 10 lines
xen/common/grant_table.c +1 -1
     if ( unlikely(op->handle >= lgt->maptrack_limit) )
     {
+        read_unlock(&lgt->lock);
         gdprintk(XENLOG_INFO, "Bad handle (%d).\n", op->handle);
         op->status = GNTST_bad_handle;
         return;
     }
 
     op->map = &maptrack_entry(lgt, op->handle);
-    spin_lock(&lgt->lock);

The extension of the locked region is still not being mentioned in the description - as said for v3, if this is really needed, it should then be fixed by a separate, much smaller change. (The main reason why the first if() doesn't need to happen under lock is - afair - that lgt->maptrack_limit can only ever increase.)

xen/common/grant_table.c __gnttab_unmap_common( · line 944

Quoted 7 lines
xen/common/grant_table.c +2 -1
@@ -939,7 +940,8 @@ __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);

Repeating what I said for v3: "The nesting of the two locks should be mentioned in the doc change" (at the very least).

xen/common/grant_table.c gnttab_unpopulate_status_frames(struct domain *d, struct grant_table *gt) · line 1297

Quoted 6 lines
xen/common/grant_table.c +2 -0
@@ -1290,10 +1293,12 @@ gnttab_unpopulate_status_frames(struct domain *d, struct grant_table *gt)
     gt->nr_status_frames = 0;
 }
 
+/* Grow the grant table. The caller must hold the grant table's
+   write lock before calling this function. */

Above you said you fixed the coding style issues, but at least here you didn't.

xen/common/grant_table.c __acquire_grant_for_copy( · line 1982

Quoted 9 lines
xen/common/grant_table.c +4 -1
@@ -1971,17 +1976,21 @@ __acquire_grant_for_copy(
                 PIN_FAIL(unlock_out_clear, GNTST_general_error,
                          "transitive grant referenced bad domain %d\n",
                          trans_domid);
-            spin_unlock(&rgt->lock);
+
+            /* __acquire_grant_for_copy() could take the read lock on
+               the right table (if rd == td), so we have to drop the
+               lock here and reacquire */

Nor here.

xen/common/grant_table.c __gnttab_swap_grant_ref(grant_ref_t ref_a, grant_ref_t · line 2460

Quoted 7 lines
xen/common/grant_table.c +1 -1
@@ -2447,7 +2456,7 @@ __gnttab_swap_grant_ref(grant_ref_t ref_a, grant_ref_t 
 ref_b)
     struct active_grant_entry *act;
     s16 rc = GNTST_okay;
 
-    spin_lock(&gt->lock);
+    read_lock(&gt->lock);

Considering the purpose of this function, wouldn't this need to be a write lock?

Quoted 10 lines
xen/common/grant_table.c +1 -1
@@ -2909,7 +2919,7 @@ gnttab_release_mappings(
         }
 
         rgt = rd->grant_table;
-        spin_lock(&rgt->lock);
+        read_lock(&rgt->lock);
 
         act = &active_entry(rgt, ref);
         sha = shared_entry_header(rgt, ref);

xen/common/grant_table.c gnttab_release_mappings( · line 2982

Quoted 5 lines
xen/common/grant_table.c +1 -1
@@ -2969,7 +2979,7 @@ gnttab_release_mappings(
         if ( act->pin == 0 )
             gnttab_clear_flag(_GTF_reading, status);
 
-        spin_unlock(&rgt->lock);
+        read_unlock(&rgt->lock);

Repeating the question on v3: "The maptrack entries are being accessed between these two - don't you need both locks here?"

Overall I find it quite unfriendly (wasting reviewing bandwidth) to submit a new version with a meaningful number of comments on the previous version un-addressed.

Jan

#3 lore raw

Re: [PATCH v4 1/2] gnttab: Introduce rwlock to protect updates to grant table state

Quoted 14 lines · Jan Beulich
Quoted 14 lines
Quoted 1 line
Quoted 1 line · On 09.01.15 at 16:12, <[email protected]> wrote:
xen/common/grant_table.c +2 -0
@@ -899,26 +899,27 @@ __gnttab_unmap_common(
 
     op->frame = (unsigned long)(op->dev_bus_addr >> PAGE_SHIFT);
 
+    read_lock(&lgt->lock);
     if ( unlikely(op->handle >= lgt->maptrack_limit) )
     {
+        read_unlock(&lgt->lock);
         gdprintk(XENLOG_INFO, "Bad handle (%d).\n", op->handle);
         op->status = GNTST_bad_handle;
         return;
     }
 
Quoted 8 lines

xen/common/grant_table.c __gnttab_unmap_common( · line 910

Quoted 2 lines
xen/common/grant_table.c +0 -1
     op->map = &maptrack_entry(lgt, op->handle);
-    spin_lock(&lgt->lock);

The extension of the locked region is still not being mentioned in the description - as said for v3, if this is really needed, it should then be fixed by a separate, much smaller change. (The main reason why the first if() doesn't need to happen under lock is - afair - that lgt->maptrack_limit can only ever increase.)

It is mentioned in the doc change to docs/misc/grant-tables.txt:

+ 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.

Quoted 10 lines

xen/common/grant_table.c __gnttab_unmap_common( · line 944

Quoted 7 lines
xen/common/grant_table.c +2 -1
@@ -939,7 +940,8 @@ __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);

Repeating what I said for v3: "The nesting of the two locks should be mentioned in the doc change" (at the very least).

... to be removed again in the second patch? double_maptrack_lock() goes away in the second patch.

Quoted 9 lines

xen/common/grant_table.c gnttab_unpopulate_status_frames(struct domain *d, struct grant_table *gt) · line 1297

Quoted 6 lines
xen/common/grant_table.c +2 -0
@@ -1290,10 +1293,12 @@ gnttab_unpopulate_status_frames(struct domain *d, struct grant_table *gt)
     gt->nr_status_frames = 0;
 }
 
+/* Grow the grant table. The caller must hold the grant table's
+   write lock before calling this function. */

Above you said you fixed the coding style issues, but at least here you didn't.

The comments match with the style as done everywhere in that file.

Quoted 10 lines

xen/common/grant_table.c __gnttab_swap_grant_ref(grant_ref_t ref_a, grant_ref_t · line 2460

Quoted 7 lines
xen/common/grant_table.c +1 -1
@@ -2447,7 +2456,7 @@ __gnttab_swap_grant_ref(grant_ref_t ref_a, grant_ref_t 
 ref_b)
     struct active_grant_entry *act;
     s16 rc = GNTST_okay;
 
-    spin_lock(&gt->lock);
+    read_lock(&gt->lock);

Considering the purpose of this function, wouldn't this need to be a write lock?

Yes, right.

Quoted 13 lines
Quoted 13 lines
xen/common/grant_table.c +1 -1
@@ -2909,7 +2919,7 @@ gnttab_release_mappings(
         }
 
         rgt = rd->grant_table;
-        spin_lock(&rgt->lock);
+        read_lock(&rgt->lock);
 
         act = &active_entry(rgt, ref);
         sha = shared_entry_header(rgt, ref);
@@ -2969,7 +2979,7 @@ gnttab_release_mappings(
         if ( act->pin == 0 )
             gnttab_clear_flag(_GTF_reading, status);
 
Quoted 5 lines

Review gnttab_release_mappings( · line 2982

Quoted 2 lines
(quoted diff) +1 -1
-        spin_unlock(&rgt->lock);
+        read_unlock(&rgt->lock);

Repeating the question on v3: "The maptrack entries are being accessed between these two - don't you need both locks here?"

yes, and be removed in the second patch again.

Quoted 3 lines

Overall I find it quite unfriendly (wasting reviewing bandwidth) to submit a new version with a meaningful number of comments on the previous version un-addressed.

Sorry, I lost track of what I did and what I did not.

Christoph

#4 lore raw

Re: [PATCH v4 1/2] gnttab: Introduce rwlock to protect updates to grant table state

Quoted 25 lines
Quoted 1 line
Quoted 1 line · On 12.01.15 at 17:03, <[email protected]> wrote:
Quoted 14 lines · Jan Beulich
Quoted 14 lines
Quoted 1 line
Quoted 1 line · On 09.01.15 at 16:12, <[email protected]> wrote:
xen/common/grant_table.c +2 -0
@@ -899,26 +899,27 @@ __gnttab_unmap_common(
 
     op->frame = (unsigned long)(op->dev_bus_addr >> PAGE_SHIFT);
 
+    read_lock(&lgt->lock);
     if ( unlikely(op->handle >= lgt->maptrack_limit) )
     {
+        read_unlock(&lgt->lock);
         gdprintk(XENLOG_INFO, "Bad handle (%d).\n", op->handle);
         op->status = GNTST_bad_handle;
         return;
     }
 
Quoted 8 lines

xen/common/grant_table.c __gnttab_unmap_common( · line 910

Quoted 2 lines
xen/common/grant_table.c +0 -1
     op->map = &maptrack_entry(lgt, op->handle);
-    spin_lock(&lgt->lock);

The extension of the locked region is still not being mentioned in the description - as said for v3, if this is really needed, it should then be fixed by a separate, much smaller change. (The main reason why the first if() doesn't need to happen under lock is - afair - that lgt->maptrack_limit can only ever increase.)

Quoted 7 lines

It is mentioned in the doc change to docs/misc/grant-tables.txt:

+ 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.

That's only a very remote connection. If there are (perceived) locking deficiencies, they should be called out explicitly (and fixed separately). But as mentioned - I think you should not be altering locking here.

Quoted 11 lines
Quoted 10 lines

xen/common/grant_table.c __gnttab_unmap_common( · line 944

Quoted 7 lines
xen/common/grant_table.c +2 -1
@@ -939,7 +940,8 @@ __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);

Repeating what I said for v3: "The nesting of the two locks should be mentioned in the doc change" (at the very least).

Quoted 2 lines

... to be removed again in the second patch? double_maptrack_lock() goes away in the second patch.

Yes, sorry, any patch series needs to be fully bisectable (or at the very least no intentionally be broken, knowing that a later patch will fix the brokenness). If a patch really depends on a later one to fix it, the patches shouldn't have been split.

Quoted 11 lines
Quoted 9 lines

xen/common/grant_table.c gnttab_unpopulate_status_frames(struct domain *d, struct grant_table *gt) · line 1297

Quoted 6 lines
xen/common/grant_table.c +2 -0
@@ -1290,10 +1293,12 @@ gnttab_unpopulate_status_frames(struct domain *d, struct grant_table *gt)
     gt->nr_status_frames = 0;
 }
 
+/* Grow the grant table. The caller must hold the grant table's
+   write lock before calling this function. */

Above you said you fixed the coding style issues, but at least here you didn't.

The comments match with the style as done everywhere in that file.

The existing style in the file is not the reference; please see ./CODING_STYLE.

Quoted 19 lines
Quoted 13 lines
Quoted 13 lines
xen/common/grant_table.c +1 -1
@@ -2909,7 +2919,7 @@ gnttab_release_mappings(
         }
 
         rgt = rd->grant_table;
-        spin_lock(&rgt->lock);
+        read_lock(&rgt->lock);
 
         act = &active_entry(rgt, ref);
         sha = shared_entry_header(rgt, ref);
@@ -2969,7 +2979,7 @@ gnttab_release_mappings(
         if ( act->pin == 0 )
             gnttab_clear_flag(_GTF_reading, status);
 
Quoted 5 lines

Review gnttab_release_mappings( · line 2982

Quoted 2 lines
(quoted diff) +1 -1
-        spin_unlock(&rgt->lock);
+        read_unlock(&rgt->lock);

Repeating the question on v3: "The maptrack entries are being accessed between these two - don't you need both locks here?"

Quoted 7 lines

yes, and be removed in the second patch again.

Quoted 3 lines

Overall I find it quite unfriendly (wasting reviewing bandwidth) to submit a new version with a meaningful number of comments on the previous version un-addressed.

Sorry, I lost track of what I did and what I did not.

But it can't be that difficult to go through the comments you got on the previous version again before you send out the new one. Please appreciate that reviewing bandwidth is limited.

Jan

#5 lore raw

PATCH v4 2/2 gnttab: refactor locking for scalability

In reply to Christoph Egger

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]

v4:
* 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

Cc Anthony Liguori <[email protected]>
Cc Jan Beulich <[email protected]>
Cc Keir Fraser <[email protected]>
---
 docs/misc/grant-tables.txt |   21 +++++
 xen/common/grant_table.c   |  219 +++++++++++++++++++++++++++-----------------
 2 files changed, 158 insertions(+), 82 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 +137 -82

572 lines not on this page · raw message

Signature · 1 line
1.7.9.5
#6 lore raw

Re: [PATCH v4 2/2] gnttab: refactor locking for scalability

xen/common/grant_table.c nr_active_grant_frames(struct grant_table *gt) · line 200

Quoted 12 lines
Quoted 1 line
Quoted 1 line · On 09.01.15 at 16:12, <[email protected]> wrote:
xen/common/grant_table.c +7 -0
@@ -188,6 +191,26 @@ nr_active_grant_frames(struct grant_table *gt)
     return num_act_frames_from_sha_frames(nr_grant_frames(gt));
 }
 
+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 */

When coding style issues are being pointed out, please make sure you go through your patch series and address _all_ of them, even if not each and every violation was explicitly named.

Quoted 11 lines
xen/common/grant_table.c +3 -3
@@ -514,17 +513,22 @@ static int grant_map_exists(const struct domain *ld,
                    nr_grant_entries(rgt));
     for ( ref = *ref_count; ref < max_iter; ref++ )
     {
-        act = &active_entry(rgt, ref);
+        struct active_grant_entry *act;
+        act = active_entry_acquire(rgt, ref);
 
-        if ( !act->pin )
-            continue;
+#define CONTINUE_IF(cond) \

xen/common/grant_table.c static int grant_map_exists(const struct domain *ld, · line 527

Quoted 10 lines
xen/common/grant_table.c +7 -2
+if ((cond)) { \
+    active_entry_release(act); \
+    continue; \
+}
 
-        if ( act->domid != ld->domain_id )
-            continue;
+        CONTINUE_IF(!act->pin);
+        CONTINUE_IF(act->domid != ld->domain_id);
+        CONTINUE_IF(act->frame != mfn);

I highly question this is in any way better than the v3 code. Is there a clear reason not to follow the advice of putting the active_entry_release(act) into the third of the for() expressions?

And if you _really_ want/need it this way, please again get the coding style right (and drop the pointless redundant parentheses).

Quoted 5 lines
xen/common/grant_table.c +1 -0
@@ -545,16 +555,32 @@ static void mapcount(
     grant_handle_t handle;
 
     *wrc = *rdc = 0;
+    ASSERT(rw_is_locked(&rd->grant_table->lock));

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

Quoted 10 lines
xen/common/grant_table.c +10 -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. To be perfectly correct, the
+     * caller should 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.
+     */
+    spin_lock(&lgt->maptrack_lock);

So you added the suggested ASSERT(), but you didn't adjust the comment (which actually should precede the ASSERT() imo) in any way.

Quoted 14 lines
xen/common/grant_table.c +0 -3
@@ -698,12 +723,9 @@ __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) );
 
-    read_unlock(&rgt->lock);
-
     /* pg may be set, with a refcount included, from __get_paged_frame */
     if ( !pg )
     {

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

Quoted 4 lines
xen/common/grant_table.c +0 -1
@@ -778,8 +800,6 @@ __gnttab_map_grant_ref(
         goto undo_out;
     }
 
-    double_maptrack_lock(lgt, rgt);

Again an un-addressed comment from v3: "Taking these two together
- isn't patch 1 wrong then, in that it acquires both maptrack locks in double_gt_lock()?"

xen/common/grant_table.c __gnttab_unmap_common( · line 964

Quoted 7 lines
xen/common/grant_table.c +2 -1
@@ -941,18 +960,19 @@ __gnttab_unmap_common(
 
     rgt = rd->grant_table;
     read_lock(&rgt->lock);
-    double_maptrack_lock(lgt, rgt);
+
+    read_lock(&rgt->lock);

Acquiring the same read lock twice in a row?

xen/common/grant_table.c static void gnttab_usage_print(struct domain *rd) · line 3101

Quoted 8 lines
xen/common/grant_table.c +2 -2
@@ -3045,9 +3097,11 @@ static void gnttab_usage_print(struct domain *rd)
         uint16_t status;
         uint64_t frame;
 
-        act = &active_entry(gt, ref);
-        if ( !act->pin )
+        act = active_entry_acquire(gt, ref);
+        if ( !act->pin ) {

Coding style again.

Jan