Skip to contentSkip to conversation

x86: Misra rule 13.6

PATCH In review Jan Beulich 2 participants 9 messages May 21

lore
Share & tools
lore
#1 lore raw

PATCH 1/4 x86/shadow: split get_unsafe() invocations

Misra C:2012 rule 13.6 takes issue with is_hvm_vcpu() uses (deep) inside sizeof(), as used by get_unsafe(). While sh_linear_l1_table() doesn't involve is_hvm_vcpu(), the rule there kicks in for "call to function __builtin_expect(long, long)' (unevaluated nothrow' call effect)". Break out the address calculations.

This, overall, turns out to also improve generated code (with gcc15 at least). No functional change intended.

xen/arch/x86/mm/shadow/multi.c +16 -15
@@ -2197,16 +2197,19 @@ static int cf_check sh_page_fault(
 #if (SHADOW_OPTIMIZATIONS & SHOPT_FAST_FAULT_PATH)
     if ( (regs->error_code & PFEC_reserved_bit) )
     {
+        const shadow_l1e_t *psl1e;
+
 #if (SHADOW_OPTIMIZATIONS & SHOPT_OUT_OF_SYNC)
         /* First, need to check that this isn't an out-of-sync
          * shadow l1e.  If it is, we fall back to the slow path, which
          * will sync it up again. */
         {
             shadow_l2e_t sl2e;
+            const shadow_l2e_t *psl2e = sh_linear_l2_table(v) +
+                                        shadow_l2_linear_offset(va);
             mfn_t gl1mfn;
-            if ( (get_unsafe(sl2e,
-                             (sh_linear_l2_table(v) +
-                              shadow_l2_linear_offset(va))) != 0)
+
+            if ( (get_unsafe(sl2e, psl2e) != 0)
                  || !(shadow_l2e_get_flags(sl2e) & _PAGE_PRESENT)
                  || !mfn_valid(gl1mfn = backpointer(mfn_to_page(
                                   shadow_l2e_get_mfn(sl2e))))
@@ -2222,9 +2225,8 @@ static int cf_check sh_page_fault(
 #endif /* SHOPT_OUT_OF_SYNC */
         /* The only reasons for reserved bits to be set in shadow entries
          * are the two "magic" shadow_l1e entries. */
-        if ( likely((get_unsafe(sl1e,
-                                (sh_linear_l1_table(v) +
-                                 shadow_l1_linear_offset(va))) == 0)
+        psl1e = sh_linear_l1_table(v) + shadow_l1_linear_offset(va);
+        if ( likely((get_unsafe(sl1e, psl1e) == 0)
                     && sh_l1e_is_magic(sl1e)) )
         {
 
@@ -2879,6 +2881,7 @@ static bool cf_check sh_invlpg(struct vc
 {
     mfn_t sl1mfn;
     shadow_l2e_t sl2e;
+    const shadow_l2e_t *psl2e;
 
     perfc_incr(shadow_invlpg);
 
@@ -2897,6 +2900,9 @@ static bool cf_check sh_invlpg(struct vc
 #if SHADOW_PAGING_LEVELS == 4
     {
         shadow_l3e_t sl3e;
+        const shadow_l3e_t *psl3e = sh_linear_l3_table(v) +
+                                    shadow_l3_linear_offset(linear);
+
         if ( !(shadow_l4e_get_flags(
                    sh_linear_l4_table(v)[shadow_l4_linear_offset(linear)])
                & _PAGE_PRESENT) )
@@ -2904,9 +2910,7 @@ static bool cf_check sh_invlpg(struct vc
         /* This must still be a copy-from-unsafe because we don't have the
          * paging lock, and the higher-level shadows might disappear
          * under our feet. */
-        if ( get_unsafe(sl3e,
-                        (sh_linear_l3_table(v) +
-                         shadow_l3_linear_offset(linear))) != 0 )
+        if ( get_unsafe(sl3e, psl3e) != 0 )
         {
             perfc_incr(shadow_invlpg_fault);
             return false;
@@ -2925,9 +2929,8 @@ static bool cf_check sh_invlpg(struct vc
 
     /* This must still be a copy-from-unsafe because we don't have the shadow
      * lock, and the higher-level shadows might disappear under our feet. */
-    if ( get_unsafe(sl2e,
-                    (sh_linear_l2_table(v) +
-                     shadow_l2_linear_offset(linear))) != 0 )
+    psl2e = sh_linear_l2_table(v) + shadow_l2_linear_offset(linear);
+    if ( get_unsafe(sl2e, psl2e) != 0 )
     {
         perfc_incr(shadow_invlpg_fault);
         return false;
@@ -2968,9 +2971,7 @@ static bool cf_check sh_invlpg(struct vc
              * have the paging lock last time we checked, and the
              * higher-level shadows might have disappeared under our
              * feet. */
-            if ( get_unsafe(sl2e,
-                            (sh_linear_l2_table(v) +
-                             shadow_l2_linear_offset(linear))) != 0 )
+            if ( get_unsafe(sl2e, psl2e) != 0 )
             {
                 perfc_incr(shadow_invlpg_fault);
                 paging_unlock(d);
Reviewed-by on v1 1/4
#2 lore raw

Re: [PATCH 1/4] x86/shadow: split get_unsafe() invocations

Quoted 10 lines · Jan Beulich

Misra C:2012 rule 13.6 takes issue with is_hvm_vcpu() uses (deep) inside sizeof(), as used by get_unsafe(). While sh_linear_l1_table() doesn't involve is_hvm_vcpu(), the rule there kicks in for "call to function __builtin_expect(long, long)' (unevaluated nothrow' call effect)". Break out the address calculations.

This, overall, turns out to also improve generated code (with gcc15 at least). No functional change intended.

Code generation is improved with GCC 12 as well.

~Andrew

#3 lore raw

PATCH 2/4 x86/shadow: avoid sizeof(this_cpu(...))

In reply to Jan Beulich

Misra C:2012 rule 13.6 takes issue with RELOC_HIDE() uses inside sizeof(), as used by this_cpu(). Latch the address of the per-CPU variable into a local variable, and use that in the two sizeof().

No difference in generated code for 2- and 4-level code. There's a small improvement for the 3-level case.

xen/arch/x86/mm/shadow/multi.c +7 -5
@@ -2057,22 +2057,24 @@ static DEFINE_PER_CPU(guest_l1e_t, trace
 static void cf_check trace_emulate_write_val(
     const void *ptr, unsigned long vaddr, const void *src, unsigned int bytes)
 {
-    if ( bytes > sizeof(this_cpu(trace_emulate_write_val)) )
-        bytes = sizeof(this_cpu(trace_emulate_write_val));
+    guest_l1e_t *pval = &this_cpu(trace_emulate_write_val);
+
+    if ( bytes > sizeof(*pval) )
+        bytes = sizeof(*pval);
 
 #if GUEST_PAGING_LEVELS == 3
     if ( vaddr == this_cpu(trace_emulate_initial_va) )
-        memcpy(&this_cpu(trace_emulate_write_val), src, bytes);
+        memcpy(pval, src, bytes);
     else if ( (vaddr & ~(GUEST_PTE_SIZE - 1)) ==
               this_cpu(trace_emulate_initial_va) )
     {
         TRACE_SHADOW_PATH_FLAG(TRCE_SFLAG_EMULATE_FULL_PT);
-        memcpy(&this_cpu(trace_emulate_write_val),
+        memcpy(pval,
                (typeof(ptr))((unsigned long)ptr & ~(GUEST_PTE_SIZE - 1)),
                GUEST_PTE_SIZE);
     }
 #else
-    memcpy(&this_cpu(trace_emulate_write_val), src, bytes);
+    memcpy(pval, src, bytes);
 #endif
 }
 #endif /* CONFIG_TRACEBUFFER */
Reviewed-by on v1 2/4
#4 lore raw

Re: [PATCH 2/4] x86/shadow: avoid sizeof(this_cpu(...))

Quoted 8 lines · Jan Beulich

Misra C:2012 rule 13.6 takes issue with RELOC_HIDE() uses inside sizeof(), as used by this_cpu(). Latch the address of the per-CPU variable into a local variable, and use that in the two sizeof().

No difference in generated code for 2- and 4-level code. There's a small improvement for the 3-level case.

#5 lore raw

PATCH 3/4 x86/P2M: split a read_atomic() invocation

In reply to Jan Beulich

Misra C:2012 rule 13.6 takes issue with pfn_to_pdx() and mfn_x() uses (deep) inside sizeof(), as used by read_atomic(). Break out the MFN -> struct page_info * translation.

No difference in generated code, except for a line number change.

xen/arch/x86/mm/p2m.c +3 -1
@@ -1332,6 +1332,7 @@ int set_shared_p2m_entry(struct domain *
     p2m_access_t a;
     p2m_type_t ot;
     mfn_t omfn;
+    const struct page_info *pg;
     unsigned long pg_type;
 
     if ( !paging_mode_translate(p2m->domain) )
@@ -1345,7 +1346,8 @@ int set_shared_p2m_entry(struct domain *
     ASSERT(mfn_valid(omfn));
     /* Set the m2p entry to invalid only if there are no further type
      * refs to this page as shared */
-    pg_type = read_atomic(&(mfn_to_page(omfn)->u.inuse.type_info));
+    pg = mfn_to_page(omfn);
+    pg_type = read_atomic(&pg->u.inuse.type_info);
     if ( (pg_type & PGT_count_mask) == 0
          || (pg_type & PGT_type_mask) != PGT_shared_page )
         set_gpfn_from_mfn(mfn_x(omfn), INVALID_M2P_ENTRY);
Reviewed-by on v1 3/4
#6 lore raw

Re: [PATCH 3/4] x86/P2M: split a read_atomic() invocation

Quoted 7 lines · Jan Beulich

Misra C:2012 rule 13.6 takes issue with pfn_to_pdx() and mfn_x() uses (deep) inside sizeof(), as used by read_atomic(). Break out the MFN -> struct page_info * translation.

No difference in generated code, except for a line number change.

#7 lore raw

PATCH 4/4 x86/PV: split a get_unsafe() invocation

In reply to Jan Beulich

Misra C:2012 rule 13.6 takes issue with the gdt_ldt_desc_ptr() call appearing (deep) inside sizeof(), as used by get_unsafe().

No functional change intended.

xen/arch/x86/pv/emulate.c +8 -2
@@ -29,8 +29,14 @@ int pv_emul_read_descriptor(unsigned int
           */
          ((sel & 4) && (sel >> 3) >= v->arch.pv.ldt_ents) )
         desc.b = desc.a = 0;
-    else if ( get_unsafe(desc, gdt_ldt_desc_ptr(sel)) )
-        return 0;
+    else
+    {
+        const seg_desc_t *pdesc = gdt_ldt_desc_ptr(sel);
+
+        if ( get_unsafe(desc, pdesc) )
+            return 0;
+    }
+
     if ( !insn_fetch )
         desc.b &= ~_SEGMENT_L;