[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH] gnttab: avoid use of raw_shah and union grant_combo in _set_status_v2()


  • To: "xen-devel@xxxxxxxxxxxxxxxxxxxx" <xen-devel@xxxxxxxxxxxxxxxxxxxx>
  • From: Jan Beulich <jbeulich@xxxxxxxx>
  • Date: Thu, 8 Oct 2026 11:28:50 +0200
  • Authentication-results: eu.smtp.expurgate.cloud; dkim=pass header.s=google header.d=suse.com header.i="@suse.com" header.h="Content-Transfer-Encoding:Content-Type:Autocrypt:Subject:From:Cc:To:Content-Language:User-Agent:MIME-Version:Date:Message-ID"
  • Autocrypt: addr=jbeulich@xxxxxxxx; keydata= xsDiBFk3nEQRBADAEaSw6zC/EJkiwGPXbWtPxl2xCdSoeepS07jW8UgcHNurfHvUzogEq5xk hu507c3BarVjyWCJOylMNR98Yd8VqD9UfmX0Hb8/BrA+Hl6/DB/eqGptrf4BSRwcZQM32aZK 7Pj2XbGWIUrZrd70x1eAP9QE3P79Y2oLrsCgbZJfEwCgvz9JjGmQqQkRiTVzlZVCJYcyGGsD /0tbFCzD2h20ahe8rC1gbb3K3qk+LpBtvjBu1RY9drYk0NymiGbJWZgab6t1jM7sk2vuf0Py O9Hf9XBmK0uE9IgMaiCpc32XV9oASz6UJebwkX+zF2jG5I1BfnO9g7KlotcA/v5ClMjgo6Gl MDY4HxoSRu3i1cqqSDtVlt+AOVBJBACrZcnHAUSuCXBPy0jOlBhxPqRWv6ND4c9PH1xjQ3NP nxJuMBS8rnNg22uyfAgmBKNLpLgAGVRMZGaGoJObGf72s6TeIqKJo/LtggAS9qAUiuKVnygo 3wjfkS9A3DRO+SpU7JqWdsveeIQyeyEJ/8PTowmSQLakF+3fote9ybzd880fSmFuIEJldWxp Y2ggPGpiZXVsaWNoQHN1c2UuY29tPsJgBBMRAgAgBQJZN5xEAhsDBgsJCAcDAgQVAggDBBYC AwECHgECF4AACgkQoDSui/t3IH4J+wCfQ5jHdEjCRHj23O/5ttg9r9OIruwAn3103WUITZee e7Sbg12UgcQ5lv7SzsFNBFk3nEQQCACCuTjCjFOUdi5Nm244F+78kLghRcin/awv+IrTcIWF hUpSs1Y91iQQ7KItirz5uwCPlwejSJDQJLIS+QtJHaXDXeV6NI0Uef1hP20+y8qydDiVkv6l IreXjTb7DvksRgJNvCkWtYnlS3mYvQ9NzS9PhyALWbXnH6sIJd2O9lKS1Mrfq+y0IXCP10eS FFGg+Av3IQeFatkJAyju0PPthyTqxSI4lZYuJVPknzgaeuJv/2NccrPvmeDg6Coe7ZIeQ8Yj t0ARxu2xytAkkLCel1Lz1WLmwLstV30g80nkgZf/wr+/BXJW/oIvRlonUkxv+IbBM3dX2OV8 AmRv1ySWPTP7AAMFB/9PQK/VtlNUJvg8GXj9ootzrteGfVZVVT4XBJkfwBcpC/XcPzldjv+3 HYudvpdNK3lLujXeA5fLOH+Z/G9WBc5pFVSMocI71I8bT8lIAzreg0WvkWg5V2WZsUMlnDL9 mpwIGFhlbM3gfDMs7MPMu8YQRFVdUvtSpaAs8OFfGQ0ia3LGZcjA6Ik2+xcqscEJzNH+qh8V m5jjp28yZgaqTaRbg3M/+MTbMpicpZuqF4rnB0AQD12/3BNWDR6bmh+EkYSMcEIpQmBM51qM EKYTQGybRCjpnKHGOxG0rfFY1085mBDZCH5Kx0cl0HVJuQKC+dV2ZY5AqjcKwAxpE75MLFkr wkkEGBECAAkFAlk3nEQCGwwACgkQoDSui/t3IH7nnwCfcJWUDUFKdCsBH/E5d+0ZnMQi+G0A nAuWpQkjM1ASeQwSHEeAWPgskBQL
  • Cc: Andrew Cooper <andrew.cooper3@xxxxxxxxxx>, Julien Grall <julien@xxxxxxx>, Stefano Stabellini <sstabellini@xxxxxxxxxx>, Anthony PERARD <anthony.perard@xxxxxxxxxx>, Michal Orzel <michal.orzel@xxxxxxx>, Roger Pau Monné <roger@xxxxxxxxxxxxxx>
  • Delivery-date: Thu, 08 Oct 2026 09:29:13 +0000
  • List-id: Xen developer discussion <xen-devel.lists.xenproject.org>

The main issue here being the cast to establish raw_shah; where possible
such casts are best avoided. (The (ab)use of unions is an issue as well,
but we have uses like this all over the place.)

With shared and status entries separated, we don't really need to mirror
_set_status_v1() behavior here. The shared entry fields can be accessed
normally, merely using compiler barriers to ensure checks are done on
stable local copies. In particular, torn reads (not that we expect the
compiler to emit such, i.e. this is a purely theoretical concern here
anyway) are not a problem.

While there also avoid unnecessary use of a fixed-width type for the
"mask" local variable (which wants to match "flags" anyway).

Signed-off-by: Jan Beulich <jbeulich@xxxxxxxx>
---
I'm puzzled by gcc's codegen with uint16_t vs unsigned int for the two
local variables: Use of unsigned int in principle would allow to generate
better code, if only the compiler wouldn't (as per observations with
gcc16)
- emit redundant MOVZWL,
- needlessly clobber a register variable, thus causing use of %ebx (and
  hence saving/restoring of %rbx).
Overall there's no change in size of compiled code (when really it could
shrink). Note that no similar odd effects are observable for Arm.

--- a/xen/common/grant_table.c
+++ b/xen/common/grant_table.c
@@ -832,26 +832,26 @@ static int _set_status_v2(const grant_en
                           domid_t  ldomid)
 {
     int      rc    = GNTST_okay;
-    const uint32_t *raw_shah = (const uint32_t *)shah;
-    union grant_combo scombo;
-    uint16_t mask  = GTF_type_mask;
+    domid_t  domid = shah->domid;
+    unsigned int flags = shah->flags, mask = GTF_type_mask;
 
-    scombo.raw = ACCESS_ONCE(*raw_shah);
-
-    /* if this is a grant mapping operation we should ensure GTF_sub_page
+    /* If this is a grant mapping operation we should ensure GTF_sub_page
        is not set */
     if ( mapflag )
         mask |= GTF_sub_page;
 
+    /* Make sure all checks below are done on stable local copies. */
+    barrier();
+
     /* If not already pinned, check the grant domid and type. */
     if ( !act->pin &&
-         ((((scombo.flags & mask) != GTF_permit_access) &&
-           (mapflag || ((scombo.flags & mask) != GTF_transitive))) ||
-          (scombo.domid != ldomid)) )
+         ((((flags & mask) != GTF_permit_access) &&
+           (mapflag || ((flags & mask) != GTF_transitive))) ||
+          (domid != ldomid)) )
     {
         gdprintk(XENLOG_WARNING,
                  "Bad flags (%x) or dom (%d); expected d%d, flags %x\n",
-                 scombo.flags, scombo.domid, ldomid, mask);
+                 flags, domid, ldomid, mask);
         rc = GNTST_general_error;
         goto done;
     }
@@ -862,7 +862,7 @@ static int _set_status_v2(const grant_en
     }
     else
     {
-        if ( unlikely(scombo.flags & GTF_readonly) )
+        if ( unlikely(flags & GTF_readonly) )
         {
             gdprintk(XENLOG_WARNING,
                      "Attempt to write-pin a r/o grant entry\n");
@@ -876,26 +876,30 @@ static int _set_status_v2(const grant_en
        still valid */
     smp_mb();
 
-    scombo.raw = ACCESS_ONCE(*raw_shah);
+    flags = shah->flags;
+    domid = shah->domid;
+
+    /* Again, all checks below need doing on stable local copies. */
+    barrier();
 
     if ( !act->pin )
     {
-        if ( (((scombo.flags & mask) != GTF_permit_access) &&
-              (mapflag || ((scombo.flags & mask) != GTF_transitive))) ||
-             (scombo.domid != ldomid) ||
-             (!readonly && (scombo.flags & GTF_readonly)) )
+        if ( (((flags & mask) != GTF_permit_access) &&
+              (mapflag || ((flags & mask) != GTF_transitive))) ||
+             (domid != ldomid) ||
+             (!readonly && (flags & GTF_readonly)) )
         {
             gnttab_clear_flags(rd, GTF_writing | GTF_reading, status);
             gdprintk(XENLOG_WARNING,
                      "Unstable flags (%x) or dom (%d); expected d%d (r/w: 
%d)\n",
-                     scombo.flags, scombo.domid, ldomid, !readonly);
+                     flags, domid, ldomid, !readonly);
             rc = GNTST_general_error;
             goto done;
         }
     }
     else
     {
-        if ( unlikely(scombo.flags & GTF_readonly) )
+        if ( unlikely(flags & GTF_readonly) )
         {
             gnttab_clear_flags(rd, GTF_writing, status);
             gdprintk(XENLOG_WARNING, "Unstable grant readonly flag\n");



 


Rackspace

Lists.xenproject.org is hosted with RackSpace, monitoring our
servers 24x7x365 and backed by RackSpace's Fanatical Support®.