Skip to contentSkip to conversation

gnttab: follow-on to XSAs 500 and 501

PATCH In review Jan Beulich 4 participants 16 messages Jul 28 – Nov 4

lore
Share & tools
lore
#0 lore raw

cover gnttab: follow-on to XSAs 500 and 501

1: add another compiler barrier to acquire_grant_for_copy() 2: limit scope of transitive-only variables in acquire_grant_for_copy() 3: fold redundant error code paths in acquire_grant_for_copy() 4: use available local var in map_grant_ref()
5: drop local "ld" from unmap_common_complete()
6: rename a parameter of gnttab_populate_status_frames()
7: unreachable code when GNTTAB_MAX_VERSION < 2

Jan

#1 lore raw

PATCH 1/7 gnttab: add another compiler barrier to acquire_grant_for_copy()

... and mem_sharing_gref_to_gfn().

Like for the fetching of trans_{domid,gref} in the former we ought to also fetch other fields exactly once.

---
Strictly speaking mem_sharing_gref_to_gfn() would need another barriers added on the v2 path, but it being different sub-fields of the flags looks to make the uses okay there. Thoughts?

Note that the calculation of cache_flags in map_grant_ref() has enough of a barrier after it already, by virtue of active_entry_release() being called right afterwards.

The fetching of the frame in gnttab_transfer() is hopefully good enough, by each fetch happening exactly once. The sole risk there is load tearing, which can occur elsewhere as well.

xen/common/grant_table.c +21 -18
@@ -2606,6 +2606,7 @@ acquire_grant_for_copy(
     grant_entry_header_t *shah;
     struct active_grant_entry *act;
     grant_status_t *status;
+    unsigned int shflags;
     uint32_t old_pin;
     domid_t trans_domid;
     grant_ref_t trans_gref;
@@ -2655,8 +2656,11 @@ acquire_grant_for_copy(
         status = &status_entry(rgt, gref);
     }
 
+    shflags = shah->flags;
+    barrier();
+
     old_pin = act->pin;
-    if ( sha2 && (shah->flags & GTF_type_mask) == GTF_transitive )
+    if ( sha2 && (shflags & GTF_type_mask) == GTF_transitive )
     {
         if ( (!old_pin || (!readonly &&
                            !(old_pin & (GNTPIN_devw_mask|GNTPIN_hstw_mask)))) &&
@@ -2793,6 +2797,8 @@ acquire_grant_for_copy(
     else if ( !old_pin ||
               (!readonly && !(old_pin & (GNTPIN_devw_mask|GNTPIN_hstw_mask))) )
     {
+        unsigned long gfn;
+
         if ( (rc = _set_status(shah, status, rd, rgt->gt_version, act,
                                readonly, 0, ldom)) != GNTST_okay )
              goto unlock_out;
@@ -2801,39 +2807,34 @@ acquire_grant_for_copy(
         trans_gref = gref;
         if ( !sha2 )
         {
-            unsigned long gfn = shared_entry_v1(rgt, gref).frame;
-
-            rc = get_paged_frame(gfn, &grant_mfn, page, readonly, rd);
-            if ( rc != GNTST_okay )
-                goto unlock_out_clear;
-            act_set_gfn(act, _gfn(gfn));
+            gfn = shared_entry_v1(rgt, gref).frame;
             is_sub_page = false;
             trans_page_off = 0;
             trans_length = PAGE_SIZE;
         }
-        else if ( !(sha2->hdr.flags & GTF_sub_page) )
+        else if ( !(shflags & GTF_sub_page) )
         {
-            rc = get_paged_frame(sha2->full_page.frame, &grant_mfn, page,
-                                 readonly, rd);
-            if ( rc != GNTST_okay )
-                goto unlock_out_clear;
-            act_set_gfn(act, _gfn(sha2->full_page.frame));
+            gfn = sha2->full_page.frame;
             is_sub_page = false;
             trans_page_off = 0;
             trans_length = PAGE_SIZE;
         }
         else
         {
-            rc = get_paged_frame(sha2->sub_page.frame, &grant_mfn, page,
-                                 readonly, rd);
-            if ( rc != GNTST_okay )
-                goto unlock_out_clear;
-            act_set_gfn(act, _gfn(sha2->sub_page.frame));
+            gfn = sha2->sub_page.frame;
             is_sub_page = true;
             trans_page_off = sha2->sub_page.page_off;
             trans_length = sha2->sub_page.length;
         }
 
+        /* Use latched copies of the shared entry fields from here on. */
+        barrier();
+
+        rc = get_paged_frame(gfn, &grant_mfn, page, readonly, rd);
+        if ( rc != GNTST_okay )
+            goto unlock_out_clear;
+        act_set_gfn(act, _gfn(gfn));
+
         if ( !act->pin )
         {
             act->domid = ldom;
@@ -4145,6 +4146,8 @@ int mem_sharing_gref_to_gfn(struct grant
            *gfn = _gfn(sha2->full_page.frame);
     }
 
+    barrier();
+
     if ( !rc && (flags & GTF_type_mask) != GTF_permit_access )
         rc = -ENXIO;
     else if ( !rc && status )
#2 lore raw

PATCH 2/7 gnttab: limit scope of transitive-only variables in acquire_grant_for_copy()

In reply to Jan Beulich

Especially their use outside of the transitive grant handling code can be confusing. Two of the variables were merely used as aliases in the "needs pin update" path, while one of them was purely used because it's available in the "already pinned" case.

trans_{page_off,length} remain where they are: They're misnamed imo, and would better be sub_page_{off,len}.

xen/common/grant_table.c +13 -12
@@ -2608,9 +2608,6 @@ acquire_grant_for_copy(
     grant_status_t *status;
     unsigned int shflags;
     uint32_t old_pin;
-    domid_t trans_domid;
-    grant_ref_t trans_gref;
-    struct domain *td;
     mfn_t grant_mfn;
     uint16_t trans_page_off;
     uint16_t trans_length;
@@ -2662,6 +2659,10 @@ acquire_grant_for_copy(
     old_pin = act->pin;
     if ( sha2 && (shflags & GTF_type_mask) == GTF_transitive )
     {
+        domid_t trans_domid;
+        grant_ref_t trans_gref;
+        struct domain *td;
+
         if ( (!old_pin || (!readonly &&
                            !(old_pin & (GNTPIN_devw_mask|GNTPIN_hstw_mask)))) &&
              (rc = _set_status_v2(shah, status, rd, act, readonly, 0,
@@ -2803,8 +2804,6 @@ acquire_grant_for_copy(
                                readonly, 0, ldom)) != GNTST_okay )
              goto unlock_out;
 
-        td = rd;
-        trans_gref = gref;
         if ( !sha2 )
         {
             gfn = shared_entry_v1(rgt, gref).frame;
@@ -2841,16 +2840,16 @@ acquire_grant_for_copy(
             act->is_sub_page = is_sub_page;
             act->start = trans_page_off;
             act->length = trans_length;
-            act->src_domid = td->domain_id;
-            act->trans_gref = trans_gref;
+            act->src_domid = rd->domain_id;
+            act->trans_gref = gref;
             act->mfn = grant_mfn;
 
             if ( is_sub_page )
                 atomic_inc(&rgt->nr_v2_ops);
         }
         else if ( !mfn_eq(act->mfn, grant_mfn) ||
-                  act->src_domid != td->domain_id ||
-                  act->trans_gref != trans_gref ||
+                  act->src_domid != rd->domain_id ||
+                  act->trans_gref != gref ||
                   (act->is_sub_page &&
                    (!is_sub_page ||
                     act->start != trans_page_off ||
@@ -2866,9 +2865,11 @@ acquire_grant_for_copy(
     }
     else
     {
+        struct domain *d;
+
         ASSERT(mfn_valid(act->mfn));
         *page = mfn_to_page(act->mfn);
-        td = page_get_owner_and_reference(*page);
+        d = page_get_owner_and_reference(*page);
         /*
          * act->pin being non-zero should guarantee the page to have a
          * non-zero refcount and hence a valid owner (matching the one on
@@ -2877,9 +2878,9 @@ acquire_grant_for_copy(
          * updates pin counts before obtaining page references, for
          * example).
          */
-        if ( td != rd || rd->is_dying )
+        if ( d != rd || rd->is_dying )
         {
-            if ( td )
+            if ( d )
                 put_page(*page);
             *page = NULL;
             rc = GNTST_bad_domain;
#3 lore raw

PATCH 3/7 gnttab: fold redundant error code paths in acquire_grant_for_copy()

In reply to Jan Beulich

One of the open-coded error paths of transitive grant handling is an exact match of the code at the unlock_out_clear label. The other can be made so by moving the put_page() earlier.

xen/common/grant_table.c +6 -10
@@ -2729,10 +2729,7 @@ acquire_grant_for_copy(
         {
             atomic_dec(&rgt->nr_v2_ops);
             rcu_unlock_domain(td);
-            reduce_status_for_pin(rd, act, status, readonly);
-            active_entry_release(act);
-            grant_read_unlock(rgt);
-            return rc;
+            goto unlock_out_clear;
         }
 
         /*
@@ -2763,17 +2760,16 @@ acquire_grant_for_copy(
             release_grant_for_copy(td, trans_gref, readonly);
             rcu_unlock_domain(td);
 
+            put_page(*page);
+            *page = NULL;
+
             grant_read_lock(rgt);
             atomic_dec(&rgt->nr_v2_ops);
 
             act = active_entry_acquire(rgt, gref);
-            reduce_status_for_pin(rd, act, status, readonly);
-            active_entry_release(act);
-            grant_read_unlock(rgt);
 
-            put_page(*page);
-            *page = NULL;
-            return ERESTART;
+            rc = ERESTART;
+            goto unlock_out_clear;
         }
 
         if ( !old_pin )
#4 lore raw

PATCH 4/7 gnttab: use available local var in map_grant_ref()

In reply to Jan Beulich

Commit a62776332cac ("grant_table: harden bound accesses") introduced a local variable "ref" in map_grant_ref(), but then didn't use it consistently throughout the function.

xen/common/grant_table.c +2 -2
@@ -1324,7 +1324,7 @@ map_grant_ref(
      */
     mt = &maptrack_entry(lgt, handle);
     mt->domid = op->dom;
-    mt->ref   = op->ref;
+    mt->ref   = ref;
     smp_wmb();
     write_atomic(&mt->flags, op->flags);
 
@@ -1366,7 +1366,7 @@ map_grant_ref(
             status = NULL;
     }
 
-    act = active_entry_acquire(rgt, op->ref);
+    act = active_entry_acquire(rgt, ref);
     act->pin -= pin_incr;
 
  unlock_out_clear:
#5 lore raw

PATCH 5/7 gnttab: drop local "ld" from unmap_common_complete()

In reply to Jan Beulich

It's used exactly once, and even then only conditionally. No need to establish the value in all cases.

xen/common/grant_table.c +2 -4
@@ -1606,7 +1606,7 @@ unmap_common(
 static void
 unmap_common_complete(struct gnttab_unmap_common *op)
 {
-    struct domain *ld, *rd = op->rd;
+    struct domain *rd = op->rd;
     struct grant_table *rgt;
     struct active_grant_entry *act;
     struct page_info *pg;
@@ -1618,8 +1618,6 @@ unmap_common_complete(struct gnttab_unma
         return;
     }
 
-    ld = current->domain;
-
     rcu_lock_domain(rd);
     rgt = rd->grant_table;
 
@@ -1656,7 +1654,7 @@ unmap_common_complete(struct gnttab_unma
         if ( pg )
         {
             if ( gnttab_host_mapping_get_page_type(op->done & GNTMAP_readonly,
-                                                   ld, rd) )
+                                                   current->domain, rd) )
                 put_page_type(pg);
             put_page(pg);
         }
#6 lore raw

PATCH 6/7 gnttab: rename a parameter of gnttab_populate_status_frames()

In reply to Jan Beulich

In a function of this name, "nr" (and hence req_nr_frames) is, at the first and second glance, the number of frames to populate. That's misleading, and it's also only what one of the two callers (gnttab_grow_table()) actually passes in; the other (gnttab_set_version()) passes the number of grant frames already in place.

Fold declarations while at it, and convert the assignment to an initializer.

xen/common/grant_table.c +2 -5
@@ -1807,12 +1807,9 @@ fault:
 
 static int
 gnttab_populate_status_frames(struct domain *d, struct grant_table *gt,
-                              unsigned int req_nr_frames)
+                              unsigned int grant_frames)
 {
-    unsigned int i;
-    unsigned int req_status_frames;
-
-    req_status_frames = grant_to_status_frames(req_nr_frames);
+    unsigned int i, req_status_frames = grant_to_status_frames(grant_frames);
 
     /* Make sure, prior version checks are architectural visible */
     block_speculation();
#7 lore raw

Re: [PATCH 6/7] gnttab: rename a parameter of gnttab_populate_status_frames()

Quoted 19 lines · Jan Beulich

In a function of this name, "nr" (and hence req_nr_frames) is, at the first and second glance, the number of frames to populate. That's misleading, and it's also only what one of the two callers (gnttab_grow_table()) actually passes in; the other (gnttab_set_version()) passes the number of grant frames already in place.

Fold declarations while at it, and convert the assignment to an initializer.

xen/common/grant_table.c +0 -1
@@ -1807,12 +1807,9 @@ fault:
 
 static int
 gnttab_populate_status_frames(struct domain *d, struct grant_table *gt,
-                              unsigned int req_nr_frames)

xen/common/grant_table.c fault: · line 1815

Quoted 10 lines
xen/common/grant_table.c +2 -4
+                              unsigned int grant_frames)
 {
-    unsigned int i;
-    unsigned int req_status_frames;
-
-    req_status_frames = grant_to_status_frames(req_nr_frames);
+    unsigned int i, req_status_frames = grant_to_status_frames(grant_frames);
 
     /* Make sure, prior version checks are architectural visible */
     block_speculation();

I fully agree that it's misnamed.

However, I'd suggest req_grant_frames to match the req_status_frames, and also to hint at why it's not just gt->nr_grant_frames.

It might also be worth a comment saying /* The number of status frames is always derived from the number of grant frames */ or something similar?

~Andrew

#8 lore raw

Re: [PATCH 6/7] gnttab: rename a parameter of gnttab_populate_status_frames()

Quoted 16 lines · Andrew Cooper
Quoted 15 lines · Jan Beulich
xen/common/grant_table.c +2 -5
@@ -1807,12 +1807,9 @@ fault:
 
 static int
 gnttab_populate_status_frames(struct domain *d, struct grant_table *gt,
-                              unsigned int req_nr_frames)
+                              unsigned int grant_frames)
 {
-    unsigned int i;
-    unsigned int req_status_frames;
-
-    req_status_frames = grant_to_status_frames(req_nr_frames);
+    unsigned int i, req_status_frames = grant_to_status_frames(grant_frames);
 
Quoted 8 lines

xen/common/grant_table.c fault: · line 1815

Quoted 3 lines
xen/common/grant_table.c +0 -0
     /* Make sure, prior version checks are architectural visible */
     block_speculation();

I fully agree that it's misnamed.

However, I'd suggest req_grant_frames to match the req_status_frames, and also to hint at why it's not just gt->nr_grant_frames.

When we discussed this earlier on, I think we had settled on deliberately omitting the req_ prefix here: It's correct only for the use of the function from gnttab_grow_table(), but not quite for that from gnttab_set_version().

Quoted 2 lines

It might also be worth a comment saying /* The number of status frames is always derived from the number of grant frames */ or something similar?

That's a comment we may want to have in or ahead of grant_to_status_frames(), but not at every of its call sites.

Jan

#9 lore raw

PATCH 7/7 gnttab: unreachable code when GNTTAB_MAX_VERSION < 2

In reply to Jan Beulich

I'm surprised Eclair doesn't spot the large chunks of unreachable code on Arm, i.e. violations of Misra C:2012 rule 2.1.

---
This pretty certainly isn't dealing with everything. For example, with another helper the gt_version field could likely also become conditional. With some more effort the nr_status_frames field similarly could become conditional.

xen/common/grant_table.c +106 -39
@@ -72,8 +72,10 @@ struct grant_table {
     /* Number of grant status frames shared with guest (for version 2) */
     unsigned int          nr_status_frames;
 
+#if GNTTAB_MAX_VERSION >= 2
     /* Number of version 2 operations in progress. */
     atomic_t              nr_v2_ops;
+#endif /* GNTTAB_MAX_VERSION >= 2 */
 
     /*
      * Number of available maptrack entries.  For cleanup purposes it is
@@ -182,12 +184,10 @@ static int cf_check parse_gnttab_max_map
                               opt_max_maptrack_frames_val);
 }
 
-#ifndef GNTTAB_MAX_VERSION
-#define GNTTAB_MAX_VERSION 2
-#endif
-
+#if GNTTAB_MAX_VERSION >= 2
 unsigned int __read_mostly opt_gnttab_max_version = GNTTAB_MAX_VERSION;
 static bool __read_mostly opt_transitive_grants = true;
+#endif
 #ifdef CONFIG_PV
 static bool __ro_after_init opt_grant_transfer = true;
 #else
@@ -196,17 +196,24 @@ static bool __ro_after_init opt_grant_tr
 
 static int __init cf_check parse_gnttab(const char *s)
 {
-    const char *ss, *e;
-    int val, rc = 0;
+    const char *ss;
+    int rc = 0;
 
     do {
+        int val;
+
         ss = strchr(s, ',');
         if ( !ss )
             ss = strchr(s, '\0');
 
-        if ( !strncmp(s, "max-ver:", 8) ||
-             !strncmp(s, "max_ver:", 8) ) /* Alias for original XSA-226 patch */
+        if ( false )
+            (void)val; /* Nothing. */
+#ifndef opt_gnttab_max_version
+        else if ( !strncmp(s, "max-ver:", 8) ||
+                  /* Alias for original XSA-226 patch */
+                  !strncmp(s, "max_ver:", 8) )
         {
+            const char *e;
             long ver = simple_strtol(s + 8, &e, 10);
 
             if ( e == ss && ver >= 1 && ver <= 2 )
@@ -216,6 +223,7 @@ static int __init cf_check parse_gnttab(
         }
         else if ( (val = parse_boolean("transitive", s, ss)) >= 0 )
             opt_transitive_grants = val;
+#endif
 #ifndef opt_grant_transfer
         else if ( (val = parse_boolean("transfer", s, ss)) >= 0 )
             opt_grant_transfer = val;
@@ -330,10 +338,12 @@ shared_entry_header(struct grant_table *
         block_speculation();
         return (grant_entry_header_t*)&shared_entry_v1(t, ref);
 
+#if GNTTAB_MAX_VERSION >= 2
     case 2:
         /* Returned values should be independent of speculative execution */
         block_speculation();
         return &shared_entry_v2(t, ref).hdr;
+#endif
     }
 
     ASSERT_UNREACHABLE();
@@ -378,6 +388,9 @@ struct active_grant_entry {
 })
 
     domid_t       domid;  /* Domain being granted access.             */
+
+#if GNTTAB_MAX_VERSION >= 2
+
     domid_t       src_domid; /* Original domain granting access.      */
     unsigned int  start:15; /* For sub-page grants, the start offset
                                in the page.                           */
@@ -385,6 +398,9 @@ struct active_grant_entry {
     unsigned int  length:16; /* For sub-page grants, the length of the
                                 grant.                                */
     grant_ref_t   trans_gref;
+
+#endif /* GNTTAB_MAX_VERSION < 2 */
+
     mfn_t         mfn;    /* Machine frame being granted.             */
 #ifndef NDEBUG
     gfn_t         gfn;    /* Guest's idea of the frame being granted. */
@@ -405,6 +421,15 @@ static inline void act_set_gfn(struct ac
 #endif
 }
 
+static bool act_is_sub_page(const struct active_grant_entry *act)
+{
+#if GNTTAB_MAX_VERSION >= 2
+    return act->is_sub_page;
+#else
+    return false;
+#endif
+}
+
 static DEFINE_PERCPU_RWLOCK_GLOBAL(grant_rwlock);
 
 static always_inline void grant_read_lock(struct grant_table *gt)
@@ -724,6 +749,7 @@ static unsigned int nr_grant_entries(str
         block_speculation();
         return f2e(nr_grant_frames(gt), 1);
 
+#if GNTTAB_MAX_VERSION >= 2
     case 2:
         BUILD_BUG_ON(f2e(INITIAL_NR_GRANT_FRAMES, 2) <
                      GNTTAB_NR_RESERVED_ENTRIES);
@@ -731,6 +757,7 @@ static unsigned int nr_grant_entries(str
         /* Make sure we return a value independently of speculative execution */
         block_speculation();
         return f2e(nr_grant_frames(gt), 2);
+#endif /* GNTTAB_MAX_VERSION >= 2 */
 #undef f2e
     }
 
@@ -919,7 +946,7 @@ static int _set_status(const grant_entry
                        domid_t ldomid)
 {
 
-    if ( evaluate_nospec(rgt_version == 1) )
+    if ( GNTTAB_MAX_VERSION < 2 || evaluate_nospec(rgt_version == 1) )
         return _set_status_v1(shah, rd, act, readonly, mapflag, ldomid);
     else
         return _set_status_v2(shah, status, rd, act, readonly, mapflag, ldomid);
@@ -1089,17 +1116,18 @@ map_grant_ref(
     if ( act->pin &&
          ((act->domid != ld->domain_id) ||
           (act->pin & GNTPIN_incr2oflow_mask(pin_incr)) ||
-          (act->is_sub_page)) )
+          act_is_sub_page(act)) )
     {
         gdprintk(XENLOG_WARNING,
                  "Bad domain (%d != %d), or risk of counter overflow %08x, or subpage %d\n",
-                 act->domid, ld->domain_id, act->pin, act->is_sub_page);
+                 act->domid, ld->domain_id, act->pin, act_is_sub_page(act));
         rc = GNTST_general_error;
         goto act_release_out;
     }
 
     /* Make sure we do not access memory speculatively */
-    status = evaluate_nospec(rgt->gt_version == 1) ? &shah->flags
+    status = GNTTAB_MAX_VERSION < 2 ||
+             evaluate_nospec(rgt->gt_version == 1) ? &shah->flags
                                                    : &status_entry(rgt, ref);
 
     if ( !act->pin ||
@@ -1113,9 +1141,10 @@ map_grant_ref(
 
         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 = GNTTAB_MAX_VERSION < 2 ||
+                                evaluate_nospec(rgt->gt_version == 1)
+                                ? shared_entry_v1(rgt, ref).frame
+                                : shared_entry_v2(rgt, ref).full_page.frame;
 
             rc = get_paged_frame(gfn, &mfn, &pg,
                                  op->flags & GNTMAP_readonly, rd);
@@ -1124,11 +1153,13 @@ map_grant_ref(
             act_set_gfn(act, _gfn(gfn));
             act->domid = ld->domain_id;
             act->mfn = mfn;
+#if GNTTAB_MAX_VERSION >= 2
             act->start = 0;
             act->length = PAGE_SIZE;
             act->is_sub_page = false;
             act->src_domid = rd->domain_id;
             act->trans_gref = ref;
+#endif /* GNTTAB_MAX_VERSION >= 2 */
         }
     }
 
@@ -1350,7 +1381,8 @@ map_grant_ref(
 
     grant_read_lock(rgt);
 
-    if ( unlikely(evaluate_nospec((rgt->gt_version == 1) !=
+    if ( GNTTAB_MAX_VERSION >= 2 &&
+         unlikely(evaluate_nospec((rgt->gt_version == 1) !=
                                   (status == &shah->flags))) )
     {
         /*
@@ -1625,7 +1657,7 @@ unmap_common_complete(struct gnttab_unma
 
     act = active_entry_acquire(rgt, op->ref);
 
-    if ( evaluate_nospec(rgt->gt_version == 1) )
+    if ( GNTTAB_MAX_VERSION < 2 || evaluate_nospec(rgt->gt_version == 1) )
         status = &shared_entry_v1(rgt, op->ref).flags;
     else if ( evaluate_nospec(op->ref < nr_grant_entries(rgt)) )
         status = &status_entry(rgt, op->ref);
@@ -1947,7 +1979,7 @@ gnttab_grow_table(struct domain *d, unsi
     }
 
     /* Status pages - version 2 */
-    if ( evaluate_nospec(gt->gt_version > 1) )
+    if ( GNTTAB_MAX_VERSION >= 2 && evaluate_nospec(gt->gt_version > 1) )
     {
         if ( gnttab_populate_status_frames(d, gt, req_nr_frames) )
             goto shared_alloc_failed;
@@ -2122,7 +2154,7 @@ gnttab_setup_table(
     }
 
     if ( (op.nr_frames > nr_grant_frames(gt) ||
-          ((gt->gt_version > 1) &&
+          ((GNTTAB_MAX_VERSION >= 2) && (gt->gt_version > 1) &&
            (grant_to_status_frames(op.nr_frames) > nr_status_frames(gt)))) &&
          gnttab_grow_table(d, op.nr_frames) )
     {
@@ -2472,7 +2504,8 @@ gnttab_transfer(
         grant_read_lock(e->grant_table);
         act = active_entry_acquire(e->grant_table, gop.ref);
 
-        if ( unlikely(evaluate_nospec(e->grant_table->gt_version != ver)) )
+        if ( GNTTAB_MAX_VERSION >= 2 &&
+             unlikely(evaluate_nospec(e->grant_table->gt_version != ver)) )
         {
             rc = -EILSEQ;
             goto release;
@@ -2538,12 +2571,13 @@ release_grant_for_copy(
     act = active_entry_acquire(rgt, gref);
     mfn = act->mfn;
 
-    if ( evaluate_nospec(rgt->gt_version == 1) )
+    if ( GNTTAB_MAX_VERSION < 2 || evaluate_nospec(rgt->gt_version == 1) )
     {
         status = &shared_entry_v1(rgt, gref).flags;
         td = rd;
         trans_gref = gref;
     }
+#if GNTTAB_MAX_VERSION >= 2
     else
     {
         if ( evaluate_nospec(gref < nr_grant_entries(rgt)) )
@@ -2552,6 +2586,7 @@ release_grant_for_copy(
              ? rd : knownalive_domain_from_domid(act->src_domid);
         trans_gref = act->trans_gref;
     }
+#endif /* GNTTAB_MAX_VERSION >= 2 */
 
     if ( readonly )
     {
@@ -2566,13 +2601,15 @@ release_grant_for_copy(
 
     reduce_status_for_pin(rd, act, status, readonly);
 
+#if GNTTAB_MAX_VERSION >= 2
     if ( !act->pin && act->is_sub_page )
         atomic_dec(&rgt->nr_v2_ops);
+#endif
 
     active_entry_release(act);
     grant_read_unlock(rgt);
 
-    if ( td != rd )
+    if ( GNTTAB_MAX_VERSION >= 2 && td != rd )
     {
         /*
          * Recursive call, but it is bounded (acquire permits only a single
@@ -2593,8 +2630,11 @@ release_grant_for_copy(
 static int
 acquire_grant_for_copy(
     struct domain *rd, grant_ref_t gref, domid_t ldom, bool readonly,
-    mfn_t *mfn, struct page_info **page, uint16_t *page_off,
-    uint16_t *length, bool allow_transitive)
+    mfn_t *mfn, struct page_info **page
+#if GNTTAB_MAX_VERSION >= 2
+    , uint16_t *page_off, uint16_t *length, bool allow_transitive
+#endif
+    )
 {
     struct grant_table *rgt = rd->grant_table;
     grant_entry_v2_t *sha2;
@@ -2637,7 +2677,7 @@ acquire_grant_for_copy(
         goto unlock_out;
     }
 
-    if ( evaluate_nospec(rgt->gt_version == 1) )
+    if ( GNTTAB_MAX_VERSION < 2 || evaluate_nospec(rgt->gt_version == 1) )
     {
         sha2 = NULL;
         status = &shah->flags;
@@ -2652,6 +2692,7 @@ acquire_grant_for_copy(
     barrier();
 
     old_pin = act->pin;
+#if GNTTAB_MAX_VERSION >= 2
     if ( sha2 && (shflags & GTF_type_mask) == GTF_transitive )
     {
         domid_t trans_domid;
@@ -2786,8 +2827,10 @@ acquire_grant_for_copy(
         else
             atomic_dec(&rgt->nr_v2_ops);
     }
-    else if ( !old_pin ||
-              (!readonly && !(old_pin & (GNTPIN_devw_mask|GNTPIN_hstw_mask))) )
+    else
+#endif /* GNTTAB_MAX_VERSION >= 2 */
+    if ( !old_pin ||
+         (!readonly && !(old_pin & (GNTPIN_devw_mask|GNTPIN_hstw_mask))) )
     {
         unsigned long gfn;
 
@@ -2828,23 +2871,29 @@ acquire_grant_for_copy(
         if ( !act->pin )
         {
             act->domid = ldom;
+            act->mfn = grant_mfn;
+
+#if GNTTAB_MAX_VERSION >= 2
             act->is_sub_page = is_sub_page;
             act->start = trans_page_off;
             act->length = trans_length;
             act->src_domid = rd->domain_id;
             act->trans_gref = gref;
-            act->mfn = grant_mfn;
 
             if ( is_sub_page )
                 atomic_inc(&rgt->nr_v2_ops);
+#endif /* GNTTAB_MAX_VERSION >= 2 */
         }
         else if ( !mfn_eq(act->mfn, grant_mfn) ||
+#if GNTTAB_MAX_VERSION >= 2
                   act->src_domid != rd->domain_id ||
                   act->trans_gref != gref ||
                   (act->is_sub_page &&
                    (!is_sub_page ||
                     act->start != trans_page_off ||
-                    act->length != trans_length)) )
+                    act->length != trans_length)) ||
+#endif /* GNTTAB_MAX_VERSION >= 2 */
+                  false )
         {
             put_page(*page);
             *page = NULL;
@@ -2881,8 +2930,10 @@ acquire_grant_for_copy(
 
     act->pin += pin_incr;
 
+#if GNTTAB_MAX_VERSION >= 2
     *page_off = act->start;
     *length = act->length;
+#endif
     *mfn = act->mfn;
 
     active_entry_release(act);
@@ -3014,13 +3065,20 @@ static int gnttab_copy_claim_buf(const s
         rc = acquire_grant_for_copy(buf->domain, ptr->u.ref,
                                     current->domain->domain_id,
                                     buf->read_only,
-                                    &buf->mfn, &buf->page,
-                                    &buf->ptr.offset, &buf->len,
-                                    opt_transitive_grants);
+                                    &buf->mfn, &buf->page
+#if GNTTAB_MAX_VERSION >= 2
+                                    , &buf->ptr.offset, &buf->len,
+                                    opt_transitive_grants
+#endif
+                                    );
         if ( rc != GNTST_okay )
             goto out;
         buf->ptr.u.ref = ptr->u.ref;
         buf->have_grant = 1;
+#if GNTTAB_MAX_VERSION < 2
+        buf->ptr.offset = 0;
+        buf->len = PAGE_SIZE;
+#endif
     }
     else
     {
@@ -3241,6 +3299,12 @@ gnttab_set_version(XEN_GUEST_HANDLE_PARA
     if ( gt->gt_version == op.version )
         goto out_unlock;
 
+    if ( GNTTAB_MAX_VERSION < 2 )
+    {
+        res = -EOPNOTSUPP;
+        goto out_unlock;
+    }
+
     /*
      * Make sure that the grant table isn't currently in use when we
      * change the version number, except for the first 8 entries which
@@ -3270,6 +3334,7 @@ gnttab_set_version(XEN_GUEST_HANDLE_PARA
         break;
 
     case 2:
+#if GNTTAB_MAX_VERSION >= 2
         if ( atomic_read(&gt->nr_v2_ops) )
         {
             gdprintk(XENLOG_WARNING,
@@ -3278,6 +3343,7 @@ gnttab_set_version(XEN_GUEST_HANDLE_PARA
             res = -EAGAIN;
             goto out_unlock;
         }
+#endif /* GNTTAB_MAX_VERSION >= 2 */
 
         for ( i = 0; i < GNTTAB_NR_RESERVED_ENTRIES; i++ )
         {
@@ -3527,7 +3593,7 @@ swap_grant_ref(grant_ref_t ref_a, grant_
         goto out;
     }
 
-    if ( evaluate_nospec(gt->gt_version == 1) )
+    if ( GNTTAB_MAX_VERSION < 2 || evaluate_nospec(gt->gt_version == 1) )
     {
         grant_entry_v1_t shared;
 
@@ -3945,7 +4011,7 @@ int gnttab_release_mappings(struct domai
 
         act = active_entry_acquire(rgt, ref);
         sha = shared_entry_header(rgt, ref);
-        if ( rgt->gt_version == 1 )
+        if ( GNTTAB_MAX_VERSION < 2 || rgt->gt_version == 1 )
             status = &sha->flags;
         else
             status = &status_entry(rgt, ref);
@@ -4120,7 +4186,7 @@ int mem_sharing_gref_to_gfn(struct grant
         rc = -EINVAL;
     else if ( ref >= nr_grant_entries(gt) )
         rc = -ENOENT;
-    else if ( evaluate_nospec(gt->gt_version == 1) )
+    else if ( GNTTAB_MAX_VERSION < 2 || evaluate_nospec(gt->gt_version == 1) )
     {
         const grant_entry_v1_t *sha1 = &shared_entry_v1(gt, ref);
 
@@ -4144,7 +4210,7 @@ int mem_sharing_gref_to_gfn(struct grant
         rc = -ENXIO;
     else if ( !rc && status )
     {
-        if ( evaluate_nospec(gt->gt_version == 1) )
+        if ( GNTTAB_MAX_VERSION < 2 || evaluate_nospec(gt->gt_version == 1) )
             *status = flags;
         else
             *status = status_entry(gt, ref);
@@ -4275,7 +4341,7 @@ int gnttab_acquire_resource(
         break;
 
     case XENMEM_resource_grant_table_id_status:
-        if ( gt->gt_version != 2 )
+        if ( GNTTAB_MAX_VERSION < 2 || gt->gt_version != 2 )
             break;
 
         /* Check that void ** is a suitable representation for gt->status. */
@@ -4327,7 +4393,8 @@ int gnttab_map_frame_begin(
 
     grant_write_lock(gt);
 
-    if ( evaluate_nospec(gt->gt_version == 2) && (idx & XENMAPIDX_grant_table_status) )
+    if ( GNTTAB_MAX_VERSION >= 2 && evaluate_nospec(gt->gt_version == 2) &&
+         (idx & XENMAPIDX_grant_table_status) )
     {
         idx &= ~XENMAPIDX_grant_table_status;
         rc = gnttab_get_status_frame_mfn(d, idx, mfn);
@@ -4395,7 +4462,7 @@ static void gnttab_usage_print(struct do
 
         sha = shared_entry_header(gt, ref);
 
-        if ( gt->gt_version == 1 )
+        if ( GNTTAB_MAX_VERSION < 2 || gt->gt_version == 1 )
         {
             status = sha->flags;
             frame = shared_entry_v1(gt, ref).frame;
xen/include/xen/grant_table.h +9 -0
@@ -39,7 +39,16 @@ void gnttab_seed_entry(const struct doma
 
 #ifdef CONFIG_GRANT_TABLE
 
+#ifndef GNTTAB_MAX_VERSION
+#define GNTTAB_MAX_VERSION 2
+#endif
+
+#if GNTTAB_MAX_VERSION >= 2
 extern unsigned int opt_gnttab_max_version;
+#else
+# define opt_gnttab_max_version GNTTAB_MAX_VERSION
+#endif
+
 extern unsigned int opt_max_grant_frames;
 
 /* Create/destroy per-domain grant table context. */
#10 lore raw

Re: [PATCH 7/7] gnttab: unreachable code when GNTTAB_MAX_VERSION < 2

Quoted 10 lines · Jan Beulich

I'm surprised Eclair doesn't spot the large chunks of unreachable code on Arm, i.e. violations of Misra C:2012 rule 2.1.

---
This pretty certainly isn't dealing with everything. For example, with another helper the gt_version field could likely also become conditional. With some more effort the nr_status_frames field similarly could become conditional.

[...]

Hard to judge only looking at the diff. Is this code unreachable in the arm64-allcode analysis configuration as well?

Signature · 4 lines
Nicola Vetrini, B.Sc.
Software Engineer
BUGSENG (https://bugseng.com)
LinkedIn: https://www.linkedin.com/in/nicola-vetrini-a42471253
#11 lore raw

Re: [PATCH 7/7] gnttab: unreachable code when GNTTAB_MAX_VERSION < 2

Quoted 13 lines · Nicola Vetrini
Quoted 4 lines · Jan Beulich

I'm surprised Eclair doesn't spot the large chunks of unreachable code on
Arm, i.e. violations of Misra C:2012 rule 2.1.

Quoted 8 lines

---
This pretty certainly isn't dealing with everything. For example, with another helper the gt_version field could likely also become conditional.
With some more effort the nr_status_frames field similarly could become conditional.

Quoted 4 lines

[...]

Hard to judge only looking at the diff. Is this code unreachable in the arm64-allcode analysis configuration as well?

Arm uniformly has

#define GNTTAB_MAX_VERSION 1

so all gnttab-v2 code is unreachable there, no matter what configuration.

Jan

#12 lore raw

Re: [PATCH 7/7] gnttab: unreachable code when GNTTAB_MAX_VERSION < 2

Quoted 19 lines · Jan Beulich
Quoted 13 lines · Nicola Vetrini
Quoted 4 lines · Jan Beulich

I'm surprised Eclair doesn't spot the large chunks of unreachable code on
Arm, i.e. violations of Misra C:2012 rule 2.1.

Quoted 8 lines

---
This pretty certainly isn't dealing with everything. For example, with another helper the gt_version field could likely also become conditional.
With some more effort the nr_status_frames field similarly could become conditional.

Quoted 4 lines

[...]

Hard to judge only looking at the diff. Is this code unreachable in the arm64-allcode analysis configuration as well?

Quoted 7 lines

Arm uniformly has

#define GNTTAB_MAX_VERSION 1

so all gnttab-v2 code is unreachable there, no matter what configuration.

Jan

I see. Existing deviations may play a role here in excluding caution reports that would otherwise be generated, particularly

-config=MC3A2.R2.1,+reports={safe,"first_area(^.*has an invariantly.*$)"}

I see only one condition "GNTTAB_MAX_VERSION" is used directly, while the others are via "opt_gnttab_max_version" which is why I reasoned there might be cautions involved, instead of violations.

Signature · 4 lines
Nicola Vetrini, B.Sc.
Software Engineer
BUGSENG (https://bugseng.com)
LinkedIn: https://www.linkedin.com/in/nicola-vetrini-a42471253
#13 lore raw

Re: [PATCH 7/7] gnttab: unreachable code when GNTTAB_MAX_VERSION < 2

In reply to Jan Beulich
Quoted 2 lines · Jan Beulich

I'm surprised Eclair doesn't spot the large chunks of unreachable code on Arm, i.e. violations of Misra C:2012 rule 2.1.

This was a thinko of mine. By using "gnttab=max-ver:2" v2 can actually be engaged on Arm as well, so related code isn't unreachable. I'm withdrawing the patch.

Jan

#14 lore raw

Re: [PATCH 7/7] gnttab: unreachable code when GNTTAB_MAX_VERSION < 2

In reply to Jan Beulich

Hi,

Quoted 10 lines · Jan Beulich

I'm surprised Eclair doesn't spot the large chunks of unreachable code on Arm, i.e. violations of Misra C:2012 rule 2.1.

---
This pretty certainly isn't dealing with everything. For example, with another helper the gt_version field could likely also become conditional. With some more effort the nr_status_frames field similarly could become conditional.

Could we also skip the gt->status allocation and cleanup on Arm? grant_table_init() still allocates this pointer array, although status pages are only used by v2. This could be done in a follow-up patch, together with the fields you mentioned.

Quoted 492 lines
xen/common/grant_table.c +106 -39
@@ -72,8 +72,10 @@ struct grant_table {
     /* Number of grant status frames shared with guest (for version 2) */
     unsigned int          nr_status_frames;
 
+#if GNTTAB_MAX_VERSION >= 2
     /* Number of version 2 operations in progress. */
     atomic_t              nr_v2_ops;
+#endif /* GNTTAB_MAX_VERSION >= 2 */
 
     /*
      * Number of available maptrack entries.  For cleanup purposes it is
@@ -182,12 +184,10 @@ static int cf_check parse_gnttab_max_map
                               opt_max_maptrack_frames_val);
 }
 
-#ifndef GNTTAB_MAX_VERSION
-#define GNTTAB_MAX_VERSION 2
-#endif
-
+#if GNTTAB_MAX_VERSION >= 2
 unsigned int __read_mostly opt_gnttab_max_version = GNTTAB_MAX_VERSION;
 static bool __read_mostly opt_transitive_grants = true;
+#endif
 #ifdef CONFIG_PV
 static bool __ro_after_init opt_grant_transfer = true;
 #else
@@ -196,17 +196,24 @@ static bool __ro_after_init opt_grant_tr
 
 static int __init cf_check parse_gnttab(const char *s)
 {
-    const char *ss, *e;
-    int val, rc = 0;
+    const char *ss;
+    int rc = 0;
 
     do {
+        int val;
+
         ss = strchr(s, ',');
         if ( !ss )
             ss = strchr(s, '\0');
 
-        if ( !strncmp(s, "max-ver:", 8) ||
-             !strncmp(s, "max_ver:", 8) ) /* Alias for original XSA-226 patch */
+        if ( false )
+            (void)val; /* Nothing. */
+#ifndef opt_gnttab_max_version
+        else if ( !strncmp(s, "max-ver:", 8) ||
+                  /* Alias for original XSA-226 patch */
+                  !strncmp(s, "max_ver:", 8) )
         {
+            const char *e;
             long ver = simple_strtol(s + 8, &e, 10);
 
             if ( e == ss && ver >= 1 && ver <= 2 )
@@ -216,6 +223,7 @@ static int __init cf_check parse_gnttab(
         }
         else if ( (val = parse_boolean("transitive", s, ss)) >= 0 )
             opt_transitive_grants = val;
+#endif
 #ifndef opt_grant_transfer
         else if ( (val = parse_boolean("transfer", s, ss)) >= 0 )
             opt_grant_transfer = val;
@@ -330,10 +338,12 @@ shared_entry_header(struct grant_table *
         block_speculation();
         return (grant_entry_header_t*)&shared_entry_v1(t, ref);
 
+#if GNTTAB_MAX_VERSION >= 2
     case 2:
         /* Returned values should be independent of speculative execution */
         block_speculation();
         return &shared_entry_v2(t, ref).hdr;
+#endif
     }
 
     ASSERT_UNREACHABLE();
@@ -378,6 +388,9 @@ struct active_grant_entry {
 })
 
     domid_t       domid;  /* Domain being granted access.             */
+
+#if GNTTAB_MAX_VERSION >= 2
+
     domid_t       src_domid; /* Original domain granting access.      */
     unsigned int  start:15; /* For sub-page grants, the start offset
                                in the page.                           */
@@ -385,6 +398,9 @@ struct active_grant_entry {
     unsigned int  length:16; /* For sub-page grants, the length of the
                                 grant.                                */
     grant_ref_t   trans_gref;
+
+#endif /* GNTTAB_MAX_VERSION < 2 */
+
     mfn_t         mfn;    /* Machine frame being granted.             */
 #ifndef NDEBUG
     gfn_t         gfn;    /* Guest's idea of the frame being granted. */
@@ -405,6 +421,15 @@ static inline void act_set_gfn(struct ac
 #endif
 }
 
+static bool act_is_sub_page(const struct active_grant_entry *act)
+{
+#if GNTTAB_MAX_VERSION >= 2
+    return act->is_sub_page;
+#else
+    return false;
+#endif
+}
+
 static DEFINE_PERCPU_RWLOCK_GLOBAL(grant_rwlock);
 
 static always_inline void grant_read_lock(struct grant_table *gt)
@@ -724,6 +749,7 @@ static unsigned int nr_grant_entries(str
         block_speculation();
         return f2e(nr_grant_frames(gt), 1);
 
+#if GNTTAB_MAX_VERSION >= 2
     case 2:
         BUILD_BUG_ON(f2e(INITIAL_NR_GRANT_FRAMES, 2) <
                      GNTTAB_NR_RESERVED_ENTRIES);
@@ -731,6 +757,7 @@ static unsigned int nr_grant_entries(str
         /* Make sure we return a value independently of speculative execution */
         block_speculation();
         return f2e(nr_grant_frames(gt), 2);
+#endif /* GNTTAB_MAX_VERSION >= 2 */
 #undef f2e
     }
 
@@ -919,7 +946,7 @@ static int _set_status(const grant_entry
                        domid_t ldomid)
 {
 
-    if ( evaluate_nospec(rgt_version == 1) )
+    if ( GNTTAB_MAX_VERSION < 2 || evaluate_nospec(rgt_version == 1) )
         return _set_status_v1(shah, rd, act, readonly, mapflag, ldomid);
     else
         return _set_status_v2(shah, status, rd, act, readonly, mapflag, ldomid);
@@ -1089,17 +1116,18 @@ map_grant_ref(
     if ( act->pin &&
          ((act->domid != ld->domain_id) ||
           (act->pin & GNTPIN_incr2oflow_mask(pin_incr)) ||
-          (act->is_sub_page)) )
+          act_is_sub_page(act)) )
     {
         gdprintk(XENLOG_WARNING,
                  "Bad domain (%d != %d), or risk of counter overflow %08x, or subpage %d\n",
-                 act->domid, ld->domain_id, act->pin, act->is_sub_page);
+                 act->domid, ld->domain_id, act->pin, act_is_sub_page(act));
         rc = GNTST_general_error;
         goto act_release_out;
     }
 
     /* Make sure we do not access memory speculatively */
-    status = evaluate_nospec(rgt->gt_version == 1) ? &shah->flags
+    status = GNTTAB_MAX_VERSION < 2 ||
+             evaluate_nospec(rgt->gt_version == 1) ? &shah->flags
                                                    : &status_entry(rgt, ref);
 
     if ( !act->pin ||
@@ -1113,9 +1141,10 @@ map_grant_ref(
 
         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 = GNTTAB_MAX_VERSION < 2 ||
+                                evaluate_nospec(rgt->gt_version == 1)
+                                ? shared_entry_v1(rgt, ref).frame
+                                : shared_entry_v2(rgt, ref).full_page.frame;
 
             rc = get_paged_frame(gfn, &mfn, &pg,
                                  op->flags & GNTMAP_readonly, rd);
@@ -1124,11 +1153,13 @@ map_grant_ref(
             act_set_gfn(act, _gfn(gfn));
             act->domid = ld->domain_id;
             act->mfn = mfn;
+#if GNTTAB_MAX_VERSION >= 2
             act->start = 0;
             act->length = PAGE_SIZE;
             act->is_sub_page = false;
             act->src_domid = rd->domain_id;
             act->trans_gref = ref;
+#endif /* GNTTAB_MAX_VERSION >= 2 */
         }
     }
 
@@ -1350,7 +1381,8 @@ map_grant_ref(
 
     grant_read_lock(rgt);
 
-    if ( unlikely(evaluate_nospec((rgt->gt_version == 1) !=
+    if ( GNTTAB_MAX_VERSION >= 2 &&
+         unlikely(evaluate_nospec((rgt->gt_version == 1) !=
                                   (status == &shah->flags))) )
     {
         /*
@@ -1625,7 +1657,7 @@ unmap_common_complete(struct gnttab_unma
 
     act = active_entry_acquire(rgt, op->ref);
 
-    if ( evaluate_nospec(rgt->gt_version == 1) )
+    if ( GNTTAB_MAX_VERSION < 2 || evaluate_nospec(rgt->gt_version == 1) )
         status = &shared_entry_v1(rgt, op->ref).flags;
     else if ( evaluate_nospec(op->ref < nr_grant_entries(rgt)) )
         status = &status_entry(rgt, op->ref);
@@ -1947,7 +1979,7 @@ gnttab_grow_table(struct domain *d, unsi
     }
 
     /* Status pages - version 2 */
-    if ( evaluate_nospec(gt->gt_version > 1) )
+    if ( GNTTAB_MAX_VERSION >= 2 && evaluate_nospec(gt->gt_version > 1) )
     {
         if ( gnttab_populate_status_frames(d, gt, req_nr_frames) )
             goto shared_alloc_failed;
@@ -2122,7 +2154,7 @@ gnttab_setup_table(
     }
 
     if ( (op.nr_frames > nr_grant_frames(gt) ||
-          ((gt->gt_version > 1) &&
+          ((GNTTAB_MAX_VERSION >= 2) && (gt->gt_version > 1) &&
            (grant_to_status_frames(op.nr_frames) > nr_status_frames(gt)))) &&
          gnttab_grow_table(d, op.nr_frames) )
     {
@@ -2472,7 +2504,8 @@ gnttab_transfer(
         grant_read_lock(e->grant_table);
         act = active_entry_acquire(e->grant_table, gop.ref);
 
-        if ( unlikely(evaluate_nospec(e->grant_table->gt_version != ver)) )
+        if ( GNTTAB_MAX_VERSION >= 2 &&
+             unlikely(evaluate_nospec(e->grant_table->gt_version != ver)) )
         {
             rc = -EILSEQ;
             goto release;
@@ -2538,12 +2571,13 @@ release_grant_for_copy(
     act = active_entry_acquire(rgt, gref);
     mfn = act->mfn;
 
-    if ( evaluate_nospec(rgt->gt_version == 1) )
+    if ( GNTTAB_MAX_VERSION < 2 || evaluate_nospec(rgt->gt_version == 1) )
     {
         status = &shared_entry_v1(rgt, gref).flags;
         td = rd;
         trans_gref = gref;
     }
+#if GNTTAB_MAX_VERSION >= 2
     else
     {
         if ( evaluate_nospec(gref < nr_grant_entries(rgt)) )
@@ -2552,6 +2586,7 @@ release_grant_for_copy(
              ? rd : knownalive_domain_from_domid(act->src_domid);
         trans_gref = act->trans_gref;
     }
+#endif /* GNTTAB_MAX_VERSION >= 2 */
 
     if ( readonly )
     {
@@ -2566,13 +2601,15 @@ release_grant_for_copy(
 
     reduce_status_for_pin(rd, act, status, readonly);
 
+#if GNTTAB_MAX_VERSION >= 2
     if ( !act->pin && act->is_sub_page )
         atomic_dec(&rgt->nr_v2_ops);
+#endif
 
     active_entry_release(act);
     grant_read_unlock(rgt);
 
-    if ( td != rd )
+    if ( GNTTAB_MAX_VERSION >= 2 && td != rd )
     {
         /*
          * Recursive call, but it is bounded (acquire permits only a single
@@ -2593,8 +2630,11 @@ release_grant_for_copy(
 static int
 acquire_grant_for_copy(
     struct domain *rd, grant_ref_t gref, domid_t ldom, bool readonly,
-    mfn_t *mfn, struct page_info **page, uint16_t *page_off,
-    uint16_t *length, bool allow_transitive)
+    mfn_t *mfn, struct page_info **page
+#if GNTTAB_MAX_VERSION >= 2
+    , uint16_t *page_off, uint16_t *length, bool allow_transitive
+#endif
+    )
 {
     struct grant_table *rgt = rd->grant_table;
     grant_entry_v2_t *sha2;
@@ -2637,7 +2677,7 @@ acquire_grant_for_copy(
         goto unlock_out;
     }
 
-    if ( evaluate_nospec(rgt->gt_version == 1) )
+    if ( GNTTAB_MAX_VERSION < 2 || evaluate_nospec(rgt->gt_version == 1) )
     {
         sha2 = NULL;
         status = &shah->flags;
@@ -2652,6 +2692,7 @@ acquire_grant_for_copy(
     barrier();
 
     old_pin = act->pin;
+#if GNTTAB_MAX_VERSION >= 2
     if ( sha2 && (shflags & GTF_type_mask) == GTF_transitive )
     {
         domid_t trans_domid;
@@ -2786,8 +2827,10 @@ acquire_grant_for_copy(
         else
             atomic_dec(&rgt->nr_v2_ops);
     }
-    else if ( !old_pin ||
-              (!readonly && !(old_pin & (GNTPIN_devw_mask|GNTPIN_hstw_mask))) )
+    else
+#endif /* GNTTAB_MAX_VERSION >= 2 */
+    if ( !old_pin ||
+         (!readonly && !(old_pin & (GNTPIN_devw_mask|GNTPIN_hstw_mask))) )
     {
         unsigned long gfn;
 
@@ -2828,23 +2871,29 @@ acquire_grant_for_copy(
         if ( !act->pin )
         {
             act->domid = ldom;
+            act->mfn = grant_mfn;
+
+#if GNTTAB_MAX_VERSION >= 2
             act->is_sub_page = is_sub_page;
             act->start = trans_page_off;
             act->length = trans_length;
             act->src_domid = rd->domain_id;
             act->trans_gref = gref;
-            act->mfn = grant_mfn;
 
             if ( is_sub_page )
                 atomic_inc(&rgt->nr_v2_ops);
+#endif /* GNTTAB_MAX_VERSION >= 2 */
         }
         else if ( !mfn_eq(act->mfn, grant_mfn) ||
+#if GNTTAB_MAX_VERSION >= 2
                   act->src_domid != rd->domain_id ||
                   act->trans_gref != gref ||
                   (act->is_sub_page &&
                    (!is_sub_page ||
                     act->start != trans_page_off ||
-                    act->length != trans_length)) )
+                    act->length != trans_length)) ||
+#endif /* GNTTAB_MAX_VERSION >= 2 */
+                  false )
         {
             put_page(*page);
             *page = NULL;
@@ -2881,8 +2930,10 @@ acquire_grant_for_copy(
 
     act->pin += pin_incr;
 
+#if GNTTAB_MAX_VERSION >= 2
     *page_off = act->start;
     *length = act->length;
+#endif
     *mfn = act->mfn;
 
     active_entry_release(act);
@@ -3014,13 +3065,20 @@ static int gnttab_copy_claim_buf(const s
         rc = acquire_grant_for_copy(buf->domain, ptr->u.ref,
                                     current->domain->domain_id,
                                     buf->read_only,
-                                    &buf->mfn, &buf->page,
-                                    &buf->ptr.offset, &buf->len,
-                                    opt_transitive_grants);
+                                    &buf->mfn, &buf->page
+#if GNTTAB_MAX_VERSION >= 2
+                                    , &buf->ptr.offset, &buf->len,
+                                    opt_transitive_grants
+#endif
+                                    );
         if ( rc != GNTST_okay )
             goto out;
         buf->ptr.u.ref = ptr->u.ref;
         buf->have_grant = 1;
+#if GNTTAB_MAX_VERSION < 2
+        buf->ptr.offset = 0;
+        buf->len = PAGE_SIZE;
+#endif
     }
     else
     {
@@ -3241,6 +3299,12 @@ gnttab_set_version(XEN_GUEST_HANDLE_PARA
     if ( gt->gt_version == op.version )
         goto out_unlock;
 
+    if ( GNTTAB_MAX_VERSION < 2 )
+    {
+        res = -EOPNOTSUPP;
+        goto out_unlock;
+    }
+
     /*
      * Make sure that the grant table isn't currently in use when we
      * change the version number, except for the first 8 entries which
@@ -3270,6 +3334,7 @@ gnttab_set_version(XEN_GUEST_HANDLE_PARA
         break;
 
     case 2:
+#if GNTTAB_MAX_VERSION >= 2
         if ( atomic_read(&gt->nr_v2_ops) )
         {
             gdprintk(XENLOG_WARNING,
@@ -3278,6 +3343,7 @@ gnttab_set_version(XEN_GUEST_HANDLE_PARA
             res = -EAGAIN;
             goto out_unlock;
         }
+#endif /* GNTTAB_MAX_VERSION >= 2 */
 
         for ( i = 0; i < GNTTAB_NR_RESERVED_ENTRIES; i++ )
         {
@@ -3527,7 +3593,7 @@ swap_grant_ref(grant_ref_t ref_a, grant_
         goto out;
     }
 
-    if ( evaluate_nospec(gt->gt_version == 1) )
+    if ( GNTTAB_MAX_VERSION < 2 || evaluate_nospec(gt->gt_version == 1) )
     {
         grant_entry_v1_t shared;
 
@@ -3945,7 +4011,7 @@ int gnttab_release_mappings(struct domai
 
         act = active_entry_acquire(rgt, ref);
         sha = shared_entry_header(rgt, ref);
-        if ( rgt->gt_version == 1 )
+        if ( GNTTAB_MAX_VERSION < 2 || rgt->gt_version == 1 )
             status = &sha->flags;
         else
             status = &status_entry(rgt, ref);
@@ -4120,7 +4186,7 @@ int mem_sharing_gref_to_gfn(struct grant
         rc = -EINVAL;
     else if ( ref >= nr_grant_entries(gt) )
         rc = -ENOENT;
-    else if ( evaluate_nospec(gt->gt_version == 1) )
+    else if ( GNTTAB_MAX_VERSION < 2 || evaluate_nospec(gt->gt_version == 1) )
     {
         const grant_entry_v1_t *sha1 = &shared_entry_v1(gt, ref);
 
@@ -4144,7 +4210,7 @@ int mem_sharing_gref_to_gfn(struct grant
         rc = -ENXIO;
     else if ( !rc && status )
     {
-        if ( evaluate_nospec(gt->gt_version == 1) )
+        if ( GNTTAB_MAX_VERSION < 2 || evaluate_nospec(gt->gt_version == 1) )
             *status = flags;
         else
             *status = status_entry(gt, ref);
@@ -4275,7 +4341,7 @@ int gnttab_acquire_resource(
         break;
 
     case XENMEM_resource_grant_table_id_status:
-        if ( gt->gt_version != 2 )
+        if ( GNTTAB_MAX_VERSION < 2 || gt->gt_version != 2 )
             break;
 
         /* Check that void ** is a suitable representation for gt->status. */
@@ -4327,7 +4393,8 @@ int gnttab_map_frame_begin(
 
     grant_write_lock(gt);
 
-    if ( evaluate_nospec(gt->gt_version == 2) && (idx & XENMAPIDX_grant_table_status) )
+    if ( GNTTAB_MAX_VERSION >= 2 && evaluate_nospec(gt->gt_version == 2) &&
+         (idx & XENMAPIDX_grant_table_status) )
     {
         idx &= ~XENMAPIDX_grant_table_status;
         rc = gnttab_get_status_frame_mfn(d, idx, mfn);
@@ -4395,7 +4462,7 @@ static void gnttab_usage_print(struct do
 
         sha = shared_entry_header(gt, ref);
 
-        if ( gt->gt_version == 1 )
+        if ( GNTTAB_MAX_VERSION < 2 || gt->gt_version == 1 )
         {
             status = sha->flags;
             frame = shared_entry_v1(gt, ref).frame;
xen/include/xen/grant_table.h +3 -0
@@ -39,7 +39,16 @@ void gnttab_seed_entry(const struct doma
 
 #ifdef CONFIG_GRANT_TABLE
 
+#ifndef GNTTAB_MAX_VERSION
+#define GNTTAB_MAX_VERSION 2
+#endif

xen/include/xen/grant_table.h void gnttab_seed_entry(const struct doma · line 54

Quoted 10 lines
xen/include/xen/grant_table.h +6 -0
+
+#if GNTTAB_MAX_VERSION >= 2
 extern unsigned int opt_gnttab_max_version;
+#else
+# define opt_gnttab_max_version GNTTAB_MAX_VERSION
+#endif
+
 extern unsigned int opt_max_grant_frames;
 
 /* Create/destroy per-domain grant table context. */

Best regards,
Mykola

#15 lore raw

Re: [PATCH 7/7] gnttab: unreachable code when GNTTAB_MAX_VERSION < 2

Quoted 15 lines · Mykola Kvach

Hi,

Quoted 10 lines · Jan Beulich

I'm surprised Eclair doesn't spot the large chunks of unreachable code on Arm, i.e. violations of Misra C:2012 rule 2.1.

---
This pretty certainly isn't dealing with everything. For example, with another helper the gt_version field could likely also become conditional. With some more effort the nr_status_frames field similarly could become conditional.

Quoted 4 lines

Could we also skip the gt->status allocation and cleanup on Arm? grant_table_init() still allocates this pointer array, although status pages are only used by v2. This could be done in a follow-up patch, together with the fields you mentioned.

Sorry, I missed that gnttab=max-ver:2 can still enable v2 on Arm. My suggestion to skip the status allocation would only apply if v2 were completely disabled on Arm.

~Mykola