Skip to contentSkip to conversation

Enable the ability to disable/remove gnttabv2

PATCH In review Daniel P. Smith 2 participants 6 messages Jan 10, 2025 – Jan 27, 2025

lore
Share & tools
lore
#0 lore raw

cover Enable the ability to disable/remove gnttabv2

OpenXT has carried a patch for some time that allows the disabling and removal of the grant table v2 capability. This is a rework of that patch in an attempt to make an upstreamable series.

The original patch was developed under funding provided by BAE, therefore a separate Authored-by tag to reflect that is included.

Daniel P. Smith (2):
gnttab: introduce version agnostic macros
gnttab: make grant table v2 support configurable

 docs/misc/xen-command-line.pandoc |   4 +-
 xen/common/Kconfig                |  18 +++
 xen/common/compat/grant_table.c   |   4 +
 xen/common/grant_table.c          | 200 +++++++++++++++++++-----------
 4 files changed, 150 insertions(+), 76 deletions(-)
Signature · 1 line
2.34.1
#1 lore raw

PATCH 1/2 gnttab: introduce version agnostic macros

Introduce a set of macros that abstract version specifics of the grant table structures. This lays the ground work for being able to turn off v2 logic and code.

---
 xen/common/grant_table.c | 114 +++++++++++++++++----------------------
 1 file changed, 48 insertions(+), 66 deletions(-)
xen/common/grant_table.c +48 -66
index 6c77867f8c..f70a5333d8 100644
@@ -181,6 +181,7 @@ static int cf_check parse_gnttab_max_maptrack_frames(const char *arg)
 #ifndef GNTTAB_MAX_VERSION
 #define GNTTAB_MAX_VERSION 2
 #endif
+#define get_gt_version(gt) ((gt)->gt_version)
 
 unsigned int __read_mostly opt_gnttab_max_version = GNTTAB_MAX_VERSION;
 static bool __read_mostly opt_transitive_grants = true;
@@ -310,16 +311,30 @@ nr_maptrack_frames(struct grant_table *t)
 #define SHGNT_PER_PAGE_V1 (PAGE_SIZE / sizeof(grant_entry_v1_t))
 #define shared_entry_v1(t, e) \
     ((t)->shared_v1[(e)/SHGNT_PER_PAGE_V1][(e)%SHGNT_PER_PAGE_V1])
+
+/* Operations providing a single interface agnostic to grant table version */
 #define SHGNT_PER_PAGE_V2 (PAGE_SIZE / sizeof(grant_entry_v2_t))
 #define shared_entry_v2(t, e) \
     ((t)->shared_v2[(e)/SHGNT_PER_PAGE_V2][(e)%SHGNT_PER_PAGE_V2])
+
+#define shared_entry_full_frame(gt, ref) \
+    ( get_gt_version(gt) == 1 ? shared_entry_v1((gt), (ref)).frame : \
+                                shared_entry_v2((gt), (ref)).full_page.frame )
+#define set_shared_entry(gt, ref, val) \
+    ( get_gt_version(gt) == 1 ? (shared_entry_v1((gt), (ref)).frame = (val)) : \
+                                (shared_entry_v2((gt), (ref)).full_page.frame = (val)) )
+#define status_addr(gt, ref, flags_addr) \
+    ( evaluate_nospec(get_gt_version(gt) == 1) ? (flags_addr) : &status_entry((gt), (ref)) )
+
 #define STGNT_PER_PAGE (PAGE_SIZE / sizeof(grant_status_t))
 #define status_entry(t, e) \
     ((t)->status[(e)/STGNT_PER_PAGE][(e)%STGNT_PER_PAGE])
+
+
 static grant_entry_header_t *
 shared_entry_header(struct grant_table *t, grant_ref_t ref)
 {
-    switch ( t->gt_version )
+    switch ( get_gt_version(t) )
     {
     case 1:
         /* Returned values should be independent of speculative execution */
@@ -709,7 +724,7 @@ get_maptrack_handle(
 /* Number of grant table entries. Caller must hold d's grant table lock. */
 static unsigned int nr_grant_entries(struct grant_table *gt)
 {
-    switch ( gt->gt_version )
+    switch ( get_gt_version(gt) )
     {
 #define f2e(nr, ver) (((nr) << PAGE_SHIFT) / sizeof(grant_entry_v##ver##_t))
     case 1:
@@ -1090,23 +1105,20 @@ map_grant_ref(
     }
 
     /* Make sure we do not access memory speculatively */
-    status = evaluate_nospec(rgt->gt_version == 1) ? &shah->flags
-                                                   : &status_entry(rgt, ref);
+    status = status_addr(rgt, ref, &shah->flags);
 
     if ( !act->pin ||
          (!(op->flags & GNTMAP_readonly) &&
           !(act->pin & (GNTPIN_hstw_mask|GNTPIN_devw_mask))) )
     {
-        if ( (rc = _set_status(shah, status, rd, rgt->gt_version, act,
+        if ( (rc = _set_status(shah, status, rd, get_gt_version(rgt), act,
                                op->flags & GNTMAP_readonly, 1,
                                ld->domain_id)) != GNTST_okay )
             goto act_release_out;
 
         if ( !act->pin )
         {
-            unsigned long gfn = evaluate_nospec(rgt->gt_version == 1) ?
-                                shared_entry_v1(rgt, ref).frame :
-                                shared_entry_v2(rgt, ref).full_page.frame;
+            unsigned long gfn = shared_entry_full_frame(rgt, ref);
 
             rc = get_paged_frame(gfn, &mfn, &pg,
                                  op->flags & GNTMAP_readonly, rd);
@@ -1603,11 +1615,7 @@ unmap_common_complete(struct gnttab_unmap_common *op)
 
     act = active_entry_acquire(rgt, op->ref);
     sha = shared_entry_header(rgt, op->ref);
-
-    if ( evaluate_nospec(rgt->gt_version == 1) )
-        status = &sha->flags;
-    else
-        status = &status_entry(rgt, op->ref);
+    status = status_addr(rgt, op->ref, &sha->flags);
 
     pg = !is_iomem_page(act->mfn) ? mfn_to_page(op->mfn) : NULL;
 
@@ -1930,7 +1938,7 @@ gnttab_grow_table(struct domain *d, unsigned int req_nr_frames)
     }
 
     /* Status pages - version 2 */
-    if ( evaluate_nospec(gt->gt_version > 1) )
+    if ( evaluate_nospec(get_gt_version(gt) > 1) )
     {
         if ( gnttab_populate_status_frames(d, gt, req_nr_frames) )
             goto shared_alloc_failed;
@@ -2105,7 +2113,7 @@ gnttab_setup_table(
     }
 
     if ( (op.nr_frames > nr_grant_frames(gt) ||
-          ((gt->gt_version > 1) &&
+          ((get_gt_version(gt) > 1) &&
            (grant_to_status_frames(op.nr_frames) > nr_status_frames(gt)))) &&
          gnttab_grow_table(d, op.nr_frames) )
     {
@@ -2267,6 +2275,7 @@ gnttab_transfer(
     mfn_t mfn;
     unsigned int max_bitsize;
     struct active_grant_entry *act;
+    unsigned long frame;
 
     if ( !opt_grant_transfer )
         return -EOPNOTSUPP;
@@ -2354,7 +2363,7 @@ gnttab_transfer(
         }
 
         max_bitsize = domain_clamp_alloc_bitsize(
-            e, e->grant_table->gt_version > 1 || paging_mode_translate(e)
+            e, get_gt_version(e->grant_table) > 1 || paging_mode_translate(e)
                ? BITS_PER_LONG + PAGE_SHIFT : 32 + PAGE_SHIFT);
         if ( max_bitsize < BITS_PER_LONG + PAGE_SHIFT &&
              (mfn_x(mfn) >> (max_bitsize - PAGE_SHIFT)) )
@@ -2452,22 +2461,12 @@ gnttab_transfer(
         grant_read_lock(e->grant_table);
         act = active_entry_acquire(e->grant_table, gop.ref);
 
-        if ( evaluate_nospec(e->grant_table->gt_version == 1) )
-        {
-            grant_entry_v1_t *sha = &shared_entry_v1(e->grant_table, gop.ref);
+        frame = shared_entry_full_frame(e->grant_table, gop.ref);
+        rc = guest_physmap_add_page(e, _gfn(frame), mfn, 0);
 
-            rc = guest_physmap_add_page(e, _gfn(sha->frame), mfn, 0);
-            if ( !paging_mode_translate(e) )
-                sha->frame = mfn_x(mfn);
-        }
-        else
-        {
-            grant_entry_v2_t *sha = &shared_entry_v2(e->grant_table, gop.ref);
+        if ( !paging_mode_translate(e) )
+            set_shared_entry(e->grant_table, gop.ref, mfn_x(mfn));
 
-            rc = guest_physmap_add_page(e, _gfn(sha->full_page.frame), mfn, 0);
-            if ( !paging_mode_translate(e) )
-                sha->full_page.frame = mfn_x(mfn);
-        }
         smp_wmb();
         shared_entry_header(e->grant_table, gop.ref)->flags |=
             GTF_transfer_completed;
@@ -2512,16 +2511,15 @@ release_grant_for_copy(
     act = active_entry_acquire(rgt, gref);
     sha = shared_entry_header(rgt, gref);
     mfn = act->mfn;
+    status = status_addr(rgt, gref, &sha->flags);
 
-    if ( evaluate_nospec(rgt->gt_version == 1) )
+    if ( evaluate_nospec(get_gt_version(rgt) == 1) )
     {
-        status = &sha->flags;
         td = rd;
         trans_gref = gref;
     }
     else
     {
-        status = &status_entry(rgt, gref);
         td = (act->src_domid == rd->domain_id)
              ? rd : knownalive_domain_from_domid(act->src_domid);
         trans_gref = act->trans_gref;
@@ -2573,7 +2571,6 @@ acquire_grant_for_copy(
     struct active_grant_entry *act;
     grant_status_t *status;
     uint32_t old_pin;
-    domid_t trans_domid;
     grant_ref_t trans_gref;
     struct domain *td;
     mfn_t grant_mfn;
@@ -2597,6 +2594,7 @@ acquire_grant_for_copy(
     /* This call also ensures the above check cannot be passed speculatively */
     shah = shared_entry_header(rgt, gref);
     act = active_entry_acquire(rgt, gref);
+    old_pin = act->pin;
 
     /* If already pinned, check the active domid and avoid refcnt overflow. */
     if ( act->pin &&
@@ -2610,7 +2608,7 @@ acquire_grant_for_copy(
         goto unlock_out;
     }
 
-    if ( evaluate_nospec(rgt->gt_version == 1) )
+    if ( evaluate_nospec(get_gt_version(rgt) == 1) )
     {
         sha2 = NULL;
         status = &shah->flags;
@@ -2621,9 +2619,10 @@ acquire_grant_for_copy(
         status = &status_entry(rgt, gref);
     }
 
-    old_pin = act->pin;
     if ( sha2 && (shah->flags & GTF_type_mask) == GTF_transitive )
     {
+        domid_t trans_domid;
+
         if ( (!old_pin || (!readonly &&
                            !(old_pin & (GNTPIN_devw_mask|GNTPIN_hstw_mask)))) &&
              (rc = _set_status_v2(shah, status, rd, act, readonly, 0,
@@ -2752,7 +2751,7 @@ acquire_grant_for_copy(
     else if ( !old_pin ||
               (!readonly && !(old_pin & (GNTPIN_devw_mask|GNTPIN_hstw_mask))) )
     {
-        if ( (rc = _set_status(shah, status, rd, rgt->gt_version, act,
+        if ( (rc = _set_status(shah, status, rd, get_gt_version(rgt), act,
                                readonly, 0, ldom)) != GNTST_okay )
              goto unlock_out;
 
@@ -3406,7 +3405,7 @@ gnttab_get_version(XEN_GUEST_HANDLE_PARAM(gnttab_get_version_t) uop)
         return rc;
     }
 
-    op.version = d->grant_table->gt_version;
+    op.version = get_gt_version(d->grant_table);
 
     rcu_unlock_domain(d);
 
@@ -3464,7 +3463,7 @@ swap_grant_ref(grant_ref_t ref_a, grant_ref_t ref_b)
         goto out;
     }
 
-    if ( evaluate_nospec(gt->gt_version == 1) )
+    if ( evaluate_nospec(get_gt_version(gt) == 1) )
     {
         grant_entry_v1_t shared;
 
@@ -3878,10 +3877,7 @@ int gnttab_release_mappings(struct domain *d)
 
         act = active_entry_acquire(rgt, ref);
         sha = shared_entry_header(rgt, ref);
-        if ( rgt->gt_version == 1 )
-            status = &sha->flags;
-        else
-            status = &status_entry(rgt, ref);
+        status = status_addr(rgt, ref, &sha->flags);
 
         pg = !is_iomem_page(act->mfn) ? mfn_to_page(act->mfn) : NULL;
 
@@ -4047,11 +4043,11 @@ int mem_sharing_gref_to_gfn(struct grant_table *gt, grant_ref_t ref,
 
     grant_read_lock(gt);
 
-    if ( gt->gt_version < 1 )
+    if ( get_gt_version(gt) < 1 )
         rc = -EINVAL;
     else if ( ref >= nr_grant_entries(gt) )
         rc = -ENOENT;
-    else if ( evaluate_nospec(gt->gt_version == 1) )
+    else if ( evaluate_nospec(get_gt_version(gt) == 1) )
     {
         const grant_entry_v1_t *sha1 = &shared_entry_v1(gt, ref);
 
@@ -4072,12 +4068,7 @@ int mem_sharing_gref_to_gfn(struct grant_table *gt, grant_ref_t ref,
     if ( !rc && (flags & GTF_type_mask) != GTF_permit_access )
         rc = -ENXIO;
     else if ( !rc && status )
-    {
-        if ( evaluate_nospec(gt->gt_version == 1) )
-            *status = flags;
-        else
-            *status = status_entry(gt, ref);
-    }
+        *status = *status_addr(gt, ref, &flags);
 
     grant_read_unlock(gt);
 
@@ -4131,7 +4122,7 @@ static int gnttab_get_shared_frame_mfn(struct domain *d,
 {
     const struct grant_table *gt = d->grant_table;
 
-    ASSERT(gt->gt_version != 0);
+    ASSERT(get_gt_version(gt) != 0);
 
     if ( idx >= nr_grant_frames(gt) )
     {
@@ -4204,7 +4195,7 @@ int gnttab_acquire_resource(
         break;
 
     case XENMEM_resource_grant_table_id_status:
-        if ( gt->gt_version != 2 )
+        if ( get_gt_version(gt) != 2 )
             break;
 
         /* Check that void ** is a suitable representation for gt->status. */
@@ -4256,7 +4247,7 @@ int gnttab_map_frame(struct domain *d, unsigned long idx, gfn_t gfn, mfn_t *mfn)
 
     grant_write_lock(gt);
 
-    if ( evaluate_nospec(gt->gt_version == 2) && (idx & XENMAPIDX_grant_table_status) )
+    if ( evaluate_nospec(get_gt_version(gt) == 2) && (idx & XENMAPIDX_grant_table_status) )
     {
         idx &= ~XENMAPIDX_grant_table_status;
         status = true;
@@ -4299,7 +4290,7 @@ static void gnttab_usage_print(struct domain *rd)
 
     printk("grant-table for remote d%d (v%u)\n"
            "  %u frames (%u max), %u maptrack frames (%u max)\n",
-           rd->domain_id, gt->gt_version,
+           rd->domain_id, get_gt_version(gt),
            nr_grant_frames(gt), gt->max_grant_frames,
            nr_maptrack_frames(gt), gt->max_maptrack_frames);
 
@@ -4319,17 +4310,8 @@ static void gnttab_usage_print(struct domain *rd)
         }
 
         sha = shared_entry_header(gt, ref);
-
-        if ( gt->gt_version == 1 )
-        {
-            status = sha->flags;
-            frame = shared_entry_v1(gt, ref).frame;
-        }
-        else
-        {
-            frame = shared_entry_v2(gt, ref).full_page.frame;
-            status = status_entry(gt, ref);
-        }
+        frame = shared_entry_full_frame(gt, ref);
+        status = *status_addr(gt, ref, &sha->flags);
 
         first = 0;
 
Signature · 1 line
2.34.1
#2 lore raw

Re: [PATCH 1/2] gnttab: introduce version agnostic macros

xen/common/grant_table.c nr_maptrack_frames(struct grant_table *t) · line 318

Quoted 9 lines · Daniel P. Smith
xen/common/grant_table.c +2 -0
@@ -310,16 +311,30 @@ nr_maptrack_frames(struct grant_table *t)
 #define SHGNT_PER_PAGE_V1 (PAGE_SIZE / sizeof(grant_entry_v1_t))
 #define shared_entry_v1(t, e) \
     ((t)->shared_v1[(e)/SHGNT_PER_PAGE_V1][(e)%SHGNT_PER_PAGE_V1])
+
+/* Operations providing a single interface agnostic to grant table version */
 #define SHGNT_PER_PAGE_V2 (PAGE_SIZE / sizeof(grant_entry_v2_t))
 #define shared_entry_v2(t, e) \
     ((t)->shared_v2[(e)/SHGNT_PER_PAGE_V2][(e)%SHGNT_PER_PAGE_V2])

The comment looks like it was misplaced, and rather wanted to go ahead of ...

xen/common/grant_table.c nr_maptrack_frames(struct grant_table *t) · line 327

Quoted 9 lines
xen/common/grant_table.c +9 -0
+
+#define shared_entry_full_frame(gt, ref) \
+    ( get_gt_version(gt) == 1 ? shared_entry_v1((gt), (ref)).frame : \
+                                shared_entry_v2((gt), (ref)).full_page.frame )
+#define set_shared_entry(gt, ref, val) \
+    ( get_gt_version(gt) == 1 ? (shared_entry_v1((gt), (ref)).frame = (val)) : \
+                                (shared_entry_v2((gt), (ref)).full_page.frame = (val)) )
+#define status_addr(gt, ref, flags_addr) \
+    ( evaluate_nospec(get_gt_version(gt) == 1) ? (flags_addr) : &status_entry((gt), (ref)) )

... this set of new #define-s?

Also a nit: There shouldn't be blanks immediately inside the opening parentheses here. For readability you further want to omit parentheses around parameters which are passed on unaltered (i.e. not becoming part of an expression). Plus some of the lines are pretty obviously too long.

As to the single use of evaluate_nospec(): That looks odd here; on the surface one would expect all three constructs to use it, or none of them. Deviations from that likely require a comment, or at least some explanation in the description.

xen/common/grant_table.c nr_maptrack_frames(struct grant_table *t) · line 333

Quoted 6 lines
xen/common/grant_table.c +3 -0
+
 #define STGNT_PER_PAGE (PAGE_SIZE / sizeof(grant_status_t))
 #define status_entry(t, e) \
     ((t)->status[(e)/STGNT_PER_PAGE][(e)%STGNT_PER_PAGE])
+
+

No double blank lines please.

xen/common/grant_table.c map_grant_ref( · line 1108

Quoted 7 lines
xen/common/grant_table.c +1 -2
@@ -1090,23 +1105,20 @@ map_grant_ref(
     }
 
     /* Make sure we do not access memory speculatively */
-    status = evaluate_nospec(rgt->gt_version == 1) ? &shah->flags
-                                                   : &status_entry(rgt, ref);
+    status = status_addr(rgt, ref, &shah->flags);

I was already wondering at the macro definition: How can it be that a version-abstracting macro is passed something that's clearly version 1 only? That is, consider the opposite of the goal of this series, someone wanting to allow building a gnttab-v2-only hypervisor. In that case grant_entry_header_t would ideally not be defined at all (as a struct; there may be a placeholder type). It would then be impossible to de-reference the pointer. Hence I think that de-referencing ought to live inside the macro.

xen/common/grant_table.c gnttab_grow_table(struct domain *d, unsigned int req_nr_frames) · line 1941

Quoted 6 lines
xen/common/grant_table.c +1 -1
@@ -1930,7 +1938,7 @@ gnttab_grow_table(struct domain *d, unsigned int req_nr_frames)
     }
 
     /* Status pages - version 2 */
-    if ( evaluate_nospec(gt->gt_version > 1) )
+    if ( evaluate_nospec(get_gt_version(gt) > 1) )

At this example: Assuming in the next patch get_gt_version() can end up expanding to constant 1, the evaluate_nospec() would still be there, for no good reason. I expect that wants further abstracting.

xen/common/grant_table.c gnttab_transfer( · line 2278

Quoted 5 lines
xen/common/grant_table.c +1 -0
@@ -2267,6 +2275,7 @@ gnttab_transfer(
     mfn_t mfn;
     unsigned int max_bitsize;
     struct active_grant_entry *act;
+    unsigned long frame;

Just going from indentation, ...

Quoted 26 lines
xen/common/grant_table.c +3 -11
@@ -2354,7 +2363,7 @@ gnttab_transfer(
         }
 
         max_bitsize = domain_clamp_alloc_bitsize(
-            e, e->grant_table->gt_version > 1 || paging_mode_translate(e)
+            e, get_gt_version(e->grant_table) > 1 || paging_mode_translate(e)
                ? BITS_PER_LONG + PAGE_SHIFT : 32 + PAGE_SHIFT);
         if ( max_bitsize < BITS_PER_LONG + PAGE_SHIFT &&
              (mfn_x(mfn) >> (max_bitsize - PAGE_SHIFT)) )
@@ -2452,22 +2461,12 @@ gnttab_transfer(
         grant_read_lock(e->grant_table);
         act = active_entry_acquire(e->grant_table, gop.ref);
 
-        if ( evaluate_nospec(e->grant_table->gt_version == 1) )
-        {
-            grant_entry_v1_t *sha = &shared_entry_v1(e->grant_table, gop.ref);
+        frame = shared_entry_full_frame(e->grant_table, gop.ref);
+        rc = guest_physmap_add_page(e, _gfn(frame), mfn, 0);
 
-            rc = guest_physmap_add_page(e, _gfn(sha->frame), mfn, 0);
-            if ( !paging_mode_translate(e) )
-                sha->frame = mfn_x(mfn);
-        }
-        else
-        {
-            grant_entry_v2_t *sha = &shared_entry_v2(e->grant_table, gop.ref);

xen/common/grant_table.c gnttab_transfer( · line 2472

Quoted 10 lines
xen/common/grant_table.c +2 -4
+        if ( !paging_mode_translate(e) )
+            set_shared_entry(e->grant_table, gop.ref, mfn_x(mfn));
 
-            rc = guest_physmap_add_page(e, _gfn(sha->full_page.frame), mfn, 0);
-            if ( !paging_mode_translate(e) )
-                sha->full_page.frame = mfn_x(mfn);
-        }
         smp_wmb();
         shared_entry_header(e->grant_table, gop.ref)->flags |=
             GTF_transfer_completed;

... the variable looks to want declaring in a more narrow scope.

Also, related to an earlier comment: Where did the evaluate_nospec() go?

Quoted 9 lines
xen/common/grant_table.c +2 -1
@@ -2512,16 +2511,15 @@ release_grant_for_copy(
     act = active_entry_acquire(rgt, gref);
     sha = shared_entry_header(rgt, gref);
     mfn = act->mfn;
+    status = status_addr(rgt, gref, &sha->flags);
 
-    if ( evaluate_nospec(rgt->gt_version == 1) )
+    if ( evaluate_nospec(get_gt_version(rgt) == 1) )
     {

xen/common/grant_table.c release_grant_for_copy( · line 2525

Quoted 10 lines
xen/common/grant_table.c +0 -2
-        status = &sha->flags;
         td = rd;
         trans_gref = gref;
     }
     else
     {
-        status = &status_entry(rgt, gref);
         td = (act->src_domid == rd->domain_id)
              ? rd : knownalive_domain_from_domid(act->src_domid);
         trans_gref = act->trans_gref;

Same here - you're moving code across that speculation barrier. Anything like this - assuming it is deliberate in the first place - needs justifying.

xen/common/grant_table.c acquire_grant_for_copy( · line 2576

Quoted 8 lines
xen/common/grant_table.c +0 -1
@@ -2573,7 +2571,6 @@ acquire_grant_for_copy(
     struct active_grant_entry *act;
     grant_status_t *status;
     uint32_t old_pin;
-    domid_t trans_domid;
     grant_ref_t trans_gref;
     struct domain *td;
     mfn_t grant_mfn;

I fear I understand neither the movement of this variable, ...

xen/common/grant_table.c acquire_grant_for_copy( · line 2597

Quoted 5 lines
xen/common/grant_table.c +1 -0
@@ -2597,6 +2594,7 @@ acquire_grant_for_copy(
     /* This call also ensures the above check cannot be passed speculatively */
     shah = shared_entry_header(rgt, gref);
     act = active_entry_acquire(rgt, gref);
+    old_pin = act->pin;

... nor that of this assignment ...

Quoted 10 lines
xen/common/grant_table.c +1 -1
@@ -2610,7 +2608,7 @@ acquire_grant_for_copy(
         goto unlock_out;
     }
 
-    if ( evaluate_nospec(rgt->gt_version == 1) )
+    if ( evaluate_nospec(get_gt_version(rgt) == 1) )
     {
         sha2 = NULL;
         status = &shah->flags;

xen/common/grant_table.c acquire_grant_for_copy( · line 2624

Quoted 4 lines
xen/common/grant_table.c +0 -1
@@ -2621,9 +2619,10 @@ acquire_grant_for_copy(
         status = &status_entry(rgt, gref);
     }
 
-    old_pin = act->pin;

... (from here).

xen/common/grant_table.c acquire_grant_for_copy( · line 2624

Quoted 3 lines
xen/common/grant_table.c +1 -0
     if ( sha2 && (shah->flags & GTF_type_mask) == GTF_transitive )
     {
+        domid_t trans_domid;

This makes me guess you want to narrow the scope of the variable, yet
then: How's that related to the subject of this patch?

xen/common/grant_table.c int gnttab_release_mappings(struct domain *d) · line 3880

Quoted 9 lines
xen/common/grant_table.c +1 -4
@@ -3878,10 +3877,7 @@ int gnttab_release_mappings(struct domain *d)
 
         act = active_entry_acquire(rgt, ref);
         sha = shared_entry_header(rgt, ref);
-        if ( rgt->gt_version == 1 )
-            status = &sha->flags;
-        else
-            status = &status_entry(rgt, ref);
+        status = status_addr(rgt, ref, &sha->flags);

Again relating to earlier comments, yet this time the other way around:
Suddenly we're gaining evaluate_nospec() here, for no apparent reason.

xen/common/grant_table.c int gnttab_map_frame(struct domain *d, unsigned long idx, gfn_t gfn, mfn_t *mfn) · line 4250

Quoted 6 lines
xen/common/grant_table.c +1 -1
@@ -4256,7 +4247,7 @@ int gnttab_map_frame(struct domain *d, unsigned long idx, gfn_t gfn, mfn_t *mfn)
 
     grant_write_lock(gt);
 
-    if ( evaluate_nospec(gt->gt_version == 2) && (idx & XENMAPIDX_grant_table_status) )
+    if ( evaluate_nospec(get_gt_version(gt) == 2) && (idx & XENMAPIDX_grant_table_status) )

I realize the line was already too long, but since you touch it you also want to wrap it.

Jan

#3 lore raw

PATCH 2/2 gnttab: make grant table v2 support configurable

In reply to Daniel P. Smith

If the v2 interface support is not required, disabling this option will remove substantial amounts of unused code in a critical subsystem.

Disables the v2-only GNTTABOP_get_status_frames grant table op.

---
 docs/misc/xen-command-line.pandoc |  4 +-
 xen/common/Kconfig                | 18 +++++++
 xen/common/compat/grant_table.c   |  4 ++
 xen/common/grant_table.c          | 86 ++++++++++++++++++++++++++++---
 4 files changed, 102 insertions(+), 10 deletions(-)
docs/misc/xen-command-line.pandoc +2 -2
index 08b0053f9c..61570308c5 100644
@@ -1278,8 +1278,8 @@ does not provide `VM_ENTRY_LOAD_GUEST_PAT`.
 
 Control various aspects of the grant table behaviour available to guests.
 
-* `max-ver` Select the maximum grant table version to offer to guests.  Valid
-version are 1 and 2.
+* `max-ver` Select the maximum grant table version to offer to guests. Only
+available when CONFIG_GRANT_TABLE_V2 is set. Valid version are 1 and 2.
 * `transitive` Permit or disallow the use of transitive grants.  Note that the
 use of grant table v2 without transitive grants is an ABI breakage from the
 guests point of view.
xen/common/Kconfig +18 -0
index 6166327f4d..378436ca2f 100644
@@ -23,6 +23,24 @@ config GRANT_TABLE
 
 	  If unsure, say Y.
 
+config GRANT_TABLE_V2
+	bool "Grant table version 2 support" if EXPERT
+	depends on GRANT_TABLE && X86
+	help
+	  Grant table interface version 2 is not the default. It has never
+	  been implemented for ARM.
+
+	  The version 2 interface enables support for systems with large amounts
+	  of memory and some exotic grant primitives that are not in use by the
+	  supported PV drivers.
+
+	  Disabling this option reduces the amount of complex security-critical
+	  hypervisor code in a subsystem of Xen responsible for approximately
+	  5% of Xen Security Advisories.
+
+	  If you do not require large memory support, say N.
+	  If you are paranoid, say N. If unsure, say Y.
+
 config PDX_COMPRESSION
 	bool "PDX (Page inDeX) compression" if EXPERT && !X86 && !RISCV
 	default ARM || PPC
xen/common/compat/grant_table.c +4 -0
index bbb717bf64..03b99f99dd 100644
@@ -296,6 +296,9 @@ int compat_grant_table_op(
             break;
 
         case GNTTABOP_get_status_frames:
+#ifndef CONFIG_GRANT_TABLE_V2
+            rc = -ENOSYS;
+#else
             if ( count != 1)
             {
                 rc = -EINVAL;
@@ -328,6 +331,7 @@ int compat_grant_table_op(
                 else
                     i = 1;
             }
+#endif
             break;
 
         default:
xen/common/grant_table.c +78 -8

295 lines not on this page · raw message

Signature · 1 line
2.34.1
#4 lore raw

Re: [PATCH 2/2] gnttab: make grant table v2 support configurable

Quoted 4 lines · Daniel P. Smith

If the v2 interface support is not required, disabling this option will remove substantial amounts of unused code in a critical subsystem.

Disables the v2-only GNTTABOP_get_status_frames grant table op.

Why's this explicitly mentioned, but not e.g. GNTTABOP_{get,set}_version, which ought to disappear altogether as well is you mean to truly limit functionality to a pre-v2 hypervisor? Even a post-v2 addition like GNTTABOP_swap_grant_ref might arguably need disabling then.

docs/misc/xen-command-line.pandoc does not provide `VM_ENTRY_LOAD_GUEST_PAT`. · line 1282

Quoted 10 lines
docs/misc/xen-command-line.pandoc +2 -2
@@ -1278,8 +1278,8 @@ does not provide `VM_ENTRY_LOAD_GUEST_PAT`.
 
 Control various aspects of the grant table behaviour available to guests.
 
-* `max-ver` Select the maximum grant table version to offer to guests.  Valid
-version are 1 and 2.
+* `max-ver` Select the maximum grant table version to offer to guests. Only
+available when CONFIG_GRANT_TABLE_V2 is set. Valid version are 1 and 2.

No, the option ought to still be legitimate to use with value 1.

xen/common/Kconfig config GRANT_TABLE · line 27

Quoted 8 lines
xen/common/Kconfig +2 -0
@@ -23,6 +23,24 @@ config GRANT_TABLE
 
 	  If unsure, say Y.
 
+config GRANT_TABLE_V2
+	bool "Grant table version 2 support" if EXPERT

While often I'm a proponent of using EXPERT, here I'm uncertain it really takes an expert to turn this off. Or wait, it's "to turn this on", as you have no default at all (see also below). This means a non-expert has no way to configure a hypervisor compatible with the previous version; that's a no-go imo.

Quoted 5 lines
xen/common/Kconfig +5 -0
+	depends on GRANT_TABLE && X86
+	help
+	  Grant table interface version 2 is not the default. It has never
+	  been implemented for ARM.
+

xen/common/Kconfig config GRANT_TABLE · line 42

Quoted 10 lines
xen/common/Kconfig +10 -0
+	  The version 2 interface enables support for systems with large amounts
+	  of memory and some exotic grant primitives that are not in use by the
+	  supported PV drivers.
+
+	  Disabling this option reduces the amount of complex security-critical
+	  hypervisor code in a subsystem of Xen responsible for approximately
+	  5% of Xen Security Advisories.
+
+	  If you do not require large memory support, say N.
+	  If you are paranoid, say N. If unsure, say Y.

Should this therefore perhaps have "default BIGMEM"?

xen/common/compat/grant_table.c int compat_grant_table_op( · line 300

Quoted 8 lines
xen/common/compat/grant_table.c +2 -0
@@ -296,6 +296,9 @@ int compat_grant_table_op(
             break;
 
         case GNTTABOP_get_status_frames:
+#ifndef CONFIG_GRANT_TABLE_V2
+            rc = -ENOSYS;

I understand ENOSYS is abused elsewhere like this, but can we please not widen the issue and use EOPNOTSUPP instead?

Quoted 6 lines
xen/common/grant_table.c +0 -0
@@ -59,11 +59,13 @@ struct grant_table {
     /* Lock protecting the maptrack limit */
     spinlock_t            maptrack_lock;
     unsigned int          max_version;

xen/common/grant_table.c struct grant_table { · line 71

Quoted 10 lines
xen/common/grant_table.c +2 -0
+#ifdef CONFIG_GRANT_TABLE_V2
     /*
      * Defaults to v1.  May be changed with GNTTABOP_set_version.  All other
      * values are invalid.
      */
     unsigned int          gt_version;
+#endif
     /* Resource limits of the domain. */
     unsigned int          max_grant_frames;
     unsigned int          max_maptrack_frames;

Between this and the following hunk there's also nr_status_frames, which is v2-only too.

Quoted 5 lines
xen/common/grant_table.c +1 -0
@@ -178,11 +182,20 @@ static int cf_check parse_gnttab_max_maptrack_frames(const char *arg)
                               opt_max_maptrack_frames_val);
 }
 
+#ifdef CONFIG_GRANT_TABLE_V2

xen/common/grant_table.c static int cf_check parse_gnttab_max_maptrack_frames(const char *arg) · line 195

Quoted 10 lines
xen/common/grant_table.c +5 -0
+
 #ifndef GNTTAB_MAX_VERSION
 #define GNTTAB_MAX_VERSION 2
 #endif
 #define get_gt_version(gt) ((gt)->gt_version)
 
+#else
+
+#define GNTTAB_MAX_VERSION 1
+#define get_gt_version(gt) 1

What about mem_sharing_gref_to_gfn(), which checks for the version not having been set yet? gnttab_get_shared_frame_mfn() also has an assertion to this effect.

Quoted 5 lines
xen/common/grant_table.c +1 -0
@@ -204,12 +217,17 @@ static int __init cf_check parse_gnttab(const char *s)
         if ( !strncmp(s, "max-ver:", 8) ||
              !strncmp(s, "max_ver:", 8) ) /* Alias for original XSA-226 patch */
         {
+#ifdef CONFIG_GRANT_TABLE_V2

xen/common/grant_table.c static int __init cf_check parse_gnttab(const char *s) · line 230

Quoted 10 lines
xen/common/grant_table.c +4 -0
+            const char *e;
             long ver = simple_strtol(s + 8, &e, 10);
 
             if ( e == ss && ver >= 1 && ver <= 2 )
                 opt_gnttab_max_version = ver;
             else
                 rc = -EINVAL;
+#else
+            no_config_param("GRANT_TABLE_V2", "max_ver", s, ss);
+#endif

See respective comment on the cmdline doc.

xen/common/grant_table.c nr_maptrack_frames(struct grant_table *t) · line 360

Quoted 12 lines
xen/common/grant_table.c +8 -0
@@ -330,6 +350,14 @@ nr_maptrack_frames(struct grant_table *t)
 #define status_entry(t, e) \
     ((t)->status[(e)/STGNT_PER_PAGE][(e)%STGNT_PER_PAGE])
 
+#else /* CONFIG_GRANT_TABLE_V2 */
+
+#define shared_entry_full_frame(gt, ref) ( shared_entry_v1((gt), (ref)).frame )
+#define set_shared_entry(gt, ref, val) \
+    ( shared_entry_v1((gt), (ref)).frame = (val) )
+#define status_addr(gt, ref, flags_addr) (flags_addr)
+
+#endif /* CONFIG_GRANT_TABLE_V2 */

See style related comments on patch 1.

xen/common/grant_table.c static unsigned int nr_grant_entries(struct grant_table *gt) · line 770

Quoted 9 lines
xen/common/grant_table.c +1 -1
@@ -734,7 +764,7 @@ static unsigned int nr_grant_entries(struct grant_table *gt)
         /* Make sure we return a value independently of speculative execution */
         block_speculation();
         return f2e(nr_grant_frames(gt), 1);
-
+#ifdef CONFIG_GRANT_TABLE_V2
     case 2:
         BUILD_BUG_ON(f2e(INITIAL_NR_GRANT_FRAMES, 2) <
                      GNTTAB_NR_RESERVED_ENTRIES);

Please don't remove blank lines like this.

xen/common/grant_table.c static int · line 1834

Quoted 8 lines
xen/common/grant_table.c +4 -0
@@ -1796,6 +1828,12 @@ static int
 gnttab_populate_status_frames(struct domain *d, struct grant_table *gt,
                               unsigned int req_nr_frames)
 {
+#ifndef CONFIG_GRANT_TABLE_V2
+    ASSERT_UNREACHABLE();
+
+    return 0;

For a release build you want to return an error here.

xen/common/grant_table.c static int · line 1835

Quoted 1 line
xen/common/grant_table.c +1 -0
+}

Hmm, the opening figure brace above then has two closing counterparts. People may find this confusing, and since Misra is almost all about confusion, I wonder whether they actually permit such (albeit I don't recall any rule on this matter).

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

Quoted 9 lines
xen/common/grant_table.c +2 -0
+#else
     unsigned int i;
     unsigned int req_status_frames;
 
@@ -1898,6 +1936,7 @@ gnttab_unpopulate_status_frames(struct domain *d, struct grant_table *gt)
 
     return 0;
 }
+#endif

In fact, to add to the confusion the #else part then even extends across function boundaries, if the hunk header are to be trusted.

xen/common/grant_table.c release_grant_for_copy( · line 2569

Quoted 12 lines
xen/common/grant_table.c +2 -0
@@ -2518,12 +2559,14 @@ release_grant_for_copy(
         td = rd;
         trans_gref = gref;
     }
+#ifdef CONFIG_GRANT_TABLE_V2
     else
     {
         td = (act->src_domid == rd->domain_id)
              ? rd : knownalive_domain_from_domid(act->src_domid);
         trans_gref = act->trans_gref;
     }
+#endif

Why's this needed? Can't leave it to the compiler's DCE?

xen/common/grant_table.c acquire_grant_for_copy( · line 2799

Quoted 10 lines
xen/common/grant_table.c +3 -1
@@ -2748,7 +2792,9 @@ acquire_grant_for_copy(
             act->is_sub_page = true;
         }
     }
-    else if ( !old_pin ||
+    else
+#endif
+    if ( !old_pin ||
               (!readonly && !(old_pin & (GNTPIN_devw_mask|GNTPIN_hstw_mask))) )
     {

Hmm, this #if extending across multiple not really related constructs looks to be the reason why patch 1 moves the assignment to old_pin.

Below from here is an assignment to act->trans_gref. That's another field that probably better wouldn't exits in a v1-only build. Much like is_sub_page and perhaps others.

Quoted 4 lines
xen/common/grant_table.c +0 -0
@@ -3165,6 +3211,17 @@ static long
 gnttab_set_version(XEN_GUEST_HANDLE_PARAM(gnttab_set_version_t) uop)
 {
     gnttab_set_version_t op;

xen/common/grant_table.c static long · line 3223

Quoted 10 lines
xen/common/grant_table.c +10 -0
+#ifndef CONFIG_GRANT_TABLE_V2
+
+    if ( copy_from_guest(&op, uop, 1) )
+        return -EFAULT;
+
+    if ( op.version == 1 )
+        return 0;
+
+    /* Behave as before set_version was introduced. */
+    return -ENOSYS;

Imo in a case like this one if ( !IS_ENABLED() ) would be preferable to use, to keep as much code as possible exposed to the compiler, thus reducing the chance of someone not noticing that it also needs changing for whatever (perhaps) purely mechanical adjustment. I.e. use #ifdef / #ifndef only in cases where lexical elements would be missing, thus breaking the build.

Quoted 11 lines
xen/common/grant_table.c +3 -0
@@ -4080,6 +4146,9 @@ int mem_sharing_gref_to_gfn(struct grant_table *gt, grant_ref_t ref,
 static int gnttab_get_status_frame_mfn(struct domain *d,
                                        unsigned int idx, mfn_t *mfn)
 {
+#ifndef CONFIG_GRANT_TABLE_V2
+    ASSERT_UNREACHABLE();
+#else
     const struct grant_table *gt = d->grant_table;
 
     ASSERT(gt->gt_version == 2);

xen/common/grant_table.c static int gnttab_get_status_frame_mfn(struct domain *d, · line 4187

Quoted 6 lines
xen/common/grant_table.c +1 -0
@@ -4113,6 +4182,7 @@ static int gnttab_get_status_frame_mfn(struct domain *d,
     /* Make sure idx is bounded wrt nr_status_frames */
     *mfn = _mfn(virt_to_mfn(
                 gt->status[array_index_nospec(idx, nr_status_frames(gt))]));
+#endif
     return 0;
 }

As in the earlier case - the function ought to return an error in a release build.

Jan

#5 lore raw
In reply to Daniel P. Smith

---- On Fri, 10 Jan 2025 08:37:09 -0500 Daniel P. Smith wrote ---

Quoted 6 lines

OpenXT has carried a patch for some time that allows the disabling and removal of the grant table v2 capability. This is a rework of that patch in an attempt to make an upstreamable series.

The original patch was developed under funding provided by BAE, therefore a separate Authored-by tag to reflect that is included.

Apologies as I should have added a "--suppress-cc=misc=by". If you want to avoid bounce messages, I would recommed dropping the BAE address on response.

v/r,
dps