Skip to contentSkip to conversation

x86/PV: avoid HVM-copying alternatives in PV-only code

RFC PATCH Superseded Jan Beulich 2 participants 3 messages Jun 30 – Jul 21

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

RFC PATCH 1/1 x86/PV: avoid HVM-copying alternatives in PV-only code

raw_copy_*_guest*() expanding to both a HVM and a PV alternative leaves a lot of unreachable code, violating Misra C:2012 rule 2.1 (without Eclair being able to spot this). Introduce a mechanism to avoid that in handling of PV-only hypercalls (i.e. ones which only PV domains can issue and which only act on PV domains [or which are not domain related]).

---
In my default test build this reduces overall code size by almost 2k, and that's with PV_SHIM=n.

RFC: Is something like this going to be acceptable at all?

RFC: The HVM-only enabling is just in case; right now it doesn't look as
if that would actually be usable anywhere.

Overriding is_hvm_vcpu() (and/or is_hvm_domain()) of course has yet better effects, which pv/shim.c shows particularly well. Yet Misra won't like us doing so.

xen/arch/x86/compat.c +2 -0
@@ -5,6 +5,8 @@
  * hypercall after doing necessary argument munging.
  */
 
+#define PV_ONLY_SOURCE 1
+
 #include <xen/guest_access.h>
 #include <xen/hypercall.h>
 #include <xen/trace.h>
xen/arch/x86/cpu/mcheck/mce.c +4 -0
@@ -1262,6 +1262,8 @@ static void cf_check __maybe_unused x86_
 
 #ifdef CONFIG_PV /* do_mca() hypercall is PV-only */
 
+#define PV_ONLY_SOURCE 1
+
 #if BITS_PER_LONG == 64
 
 /* Two layers of casting to cover Misra C:2012 rule 11.2. */
@@ -1666,6 +1668,8 @@ long do_mca(XEN_GUEST_HANDLE_PARAM(xen_m
     return ret;
 }
 
+#undef PV_ONLY_SOURCE
+
 #endif /* CONFIG_PV */
 
 static int mcinfo_dumped;
xen/arch/x86/include/asm/guest_access.h +12 -4
@@ -14,11 +14,15 @@
 
 /* Raw access functions: no type checking. */
 #define raw_copy_to_guest(dst, src, len)        \
-    (is_hvm_vcpu(current) ?                     \
+    ((IS_ENABLED(HVM_ONLY_SOURCE) ||            \
+      (!IS_ENABLED(PV_ONLY_SOURCE) &&           \
+       is_hvm_vcpu(current))) ?                 \
      copy_to_user_hvm((dst), (src), (len)) :    \
      copy_to_guest_pv(dst, src, len))
 #define raw_copy_from_guest(dst, src, len)      \
-    (is_hvm_vcpu(current) ?                     \
+    ((IS_ENABLED(HVM_ONLY_SOURCE) ||            \
+      (!IS_ENABLED(PV_ONLY_SOURCE) &&           \
+       is_hvm_vcpu(current))) ?                 \
      copy_from_user_hvm((dst), (src), (len)) :  \
      copy_from_guest_pv(dst, src, len))
 #define raw_clear_guest(dst,  len)              \
@@ -26,11 +30,15 @@
      clear_user_hvm((dst), (len)) :             \
      clear_guest_pv(dst, len))
 #define __raw_copy_to_guest(dst, src, len)      \
-    (is_hvm_vcpu(current) ?                     \
+    ((IS_ENABLED(HVM_ONLY_SOURCE) ||            \
+      (!IS_ENABLED(PV_ONLY_SOURCE) &&           \
+       is_hvm_vcpu(current))) ?                 \
      copy_to_user_hvm((dst), (src), (len)) :    \
      __copy_to_guest_pv(dst, src, len))
 #define __raw_copy_from_guest(dst, src, len)    \
-    (is_hvm_vcpu(current) ?                     \
+    ((IS_ENABLED(HVM_ONLY_SOURCE) ||            \
+      (!IS_ENABLED(PV_ONLY_SOURCE) &&           \
+       is_hvm_vcpu(current))) ?                 \
      copy_from_user_hvm((dst), (src), (len)) :  \
      __copy_from_guest_pv(dst, src, len))
 
xen/arch/x86/mm.c +10 -0
@@ -3407,6 +3407,9 @@ int new_guest_cr3(mfn_t mfn)
 #endif
 
 #ifdef CONFIG_PV
+
+#define PV_ONLY_SOURCE 1
+
 static int vcpumask_to_pcpumask(
     struct domain *d, XEN_GUEST_HANDLE_PARAM(const_void) bmap, cpumask_t *pmask)
 {
@@ -3447,6 +3450,8 @@ static int vcpumask_to_pcpumask(
     }
 }
 
+#undef PV_ONLY_SOURCE
+
 long do_mmuext_op(
     XEN_GUEST_HANDLE_PARAM(mmuext_op_t) uops,
     unsigned int count,
@@ -3983,6 +3988,8 @@ long do_mmuext_op(
     return rc;
 }
 
+#define PV_ONLY_SOURCE 1
+
 long do_mmu_update(
     XEN_GUEST_HANDLE_PARAM(mmu_update_t) ureqs,
     unsigned int count,
@@ -4361,6 +4364,9 @@ long do_mmu_update(
 
     return rc;
 }
+
+#undef PV_ONLY_SOURCE
+
 #endif /* CONFIG_PV */
 
 /*
xen/arch/x86/pv/callback.c +2 -0
@@ -5,6 +5,8 @@
  * hypercall handles and helper functions for guest callback
  */
 
+#define PV_ONLY_SOURCE 1
+
 #include <xen/event.h>
 #include <xen/hypercall.h>
 #include <xen/guest_access.h>
xen/arch/x86/pv/descriptor-tables.c +2 -0
@@ -8,6 +8,8 @@
  * Copyright (c) 2004 Christian Limpach
  */
 
+#define PV_ONLY_SOURCE 1
+
 #include <xen/guest_access.h>
 #include <xen/hypercall.h>
 
xen/arch/x86/pv/shim.c +3 -0
@@ -6,6 +6,9 @@
  *
  * Copyright (c) 2017 Citrix Systems Ltd.
  */
+
+#define PV_ONLY_SOURCE 1
+
 #include <xen/event.h>
 #include <xen/guest_access.h>
 #include <xen/hypercall.h>
xen/common/kexec.c +2 -0
@@ -6,6 +6,8 @@
  * - Magnus Damm <[email protected]>
  */
 
+#define PV_ONLY_SOURCE 1 /* Relevant only for x86. */
+
 #include <xen/init.h>
 #include <xen/lib.h>
 #include <xen/acpi.h>
#1 lore raw
Quoted 27 lines · Jan Beulich
xen/arch/x86/include/asm/guest_access.h +8 -3
@@ -14,11 +14,15 @@
 
 /* Raw access functions: no type checking. */
 #define raw_copy_to_guest(dst, src, len)        \
-    (is_hvm_vcpu(current) ?                     \
+    ((IS_ENABLED(HVM_ONLY_SOURCE) ||            \
+      (!IS_ENABLED(PV_ONLY_SOURCE) &&           \
+       is_hvm_vcpu(current))) ?                 \
      copy_to_user_hvm((dst), (src), (len)) :    \
      copy_to_guest_pv(dst, src, len))
 #define raw_copy_from_guest(dst, src, len)      \
-    (is_hvm_vcpu(current) ?                     \
+    ((IS_ENABLED(HVM_ONLY_SOURCE) ||            \
+      (!IS_ENABLED(PV_ONLY_SOURCE) &&           \
+       is_hvm_vcpu(current))) ?                 \
      copy_from_user_hvm((dst), (src), (len)) :  \
      copy_from_guest_pv(dst, src, len))
 #define raw_clear_guest(dst,  len)              \
@@ -26,11 +30,15 @@
      clear_user_hvm((dst), (len)) :             \
      clear_guest_pv(dst, len))
 #define __raw_copy_to_guest(dst, src, len)      \
-    (is_hvm_vcpu(current) ?                     \
+    ((IS_ENABLED(HVM_ONLY_SOURCE) ||            \
+      (!IS_ENABLED(PV_ONLY_SOURCE) &&           \

xen/arch/x86/include/asm/guest_access.h line 43

Quoted 10 lines
xen/arch/x86/include/asm/guest_access.h +4 -1
+       is_hvm_vcpu(current))) ?                 \
      copy_to_user_hvm((dst), (src), (len)) :    \
      __copy_to_guest_pv(dst, src, len))
 #define __raw_copy_from_guest(dst, src, len)    \
-    (is_hvm_vcpu(current) ?                     \
+    ((IS_ENABLED(HVM_ONLY_SOURCE) ||            \
+      (!IS_ENABLED(PV_ONLY_SOURCE) &&           \
+       is_hvm_vcpu(current))) ?                 \
      copy_from_user_hvm((dst), (src), (len)) :  \
      __copy_from_guest_pv(dst, src, len))

Looking back at my own attempt to do this, which was from August 2024, I started by cleaning up this code block as such:

xen/arch/x86/include/asm/guest_access.h +0 -0
index 69716c8b41bb..49920e181832 100644
@@ -12,27 +12,33 @@

 #include <asm/hvm/support.h>
 #include <asm/hvm/guest_access.h>

+#define GUEST_ACCESS_HVM is_hvm_vcpu(current) +
 /* Raw access functions: no type checking. */ #define raw_copy_to_guest(dst, src, len)        \
-    (is_hvm_vcpu(current) ?                     \
-     copy_to_user_hvm((dst), (src), (len)) :    \
-     copy_to_guest_pv(dst, src, len))
+    (GUEST_ACCESS_HVM                           \
+     ? copy_to_user_hvm(dst, src, len)          \
+     : copy_to_guest_pv(dst, src, len))
+
 #define raw_copy_from_guest(dst, src, len)      \
-    (is_hvm_vcpu(current) ?                     \
-     copy_from_user_hvm((dst), (src), (len)) :  \
-     copy_from_guest_pv(dst, src, len))
+    (GUEST_ACCESS_HVM                           \
+     ? copy_from_user_hvm(dst, src, len)        \
+     : copy_from_guest_pv(dst, src, len)) +
 #define raw_clear_guest(dst,  len)              \
-    (is_hvm_vcpu(current) ?                     \
-     clear_user_hvm((dst), (len)) :             \
-     clear_guest_pv(dst, len))
+    (GUEST_ACCESS_HVM                           \
+     ? clear_user_hvm(dst, len)                 \
+     : clear_guest_pv(dst, len))
+
 #define __raw_copy_to_guest(dst, src, len)      \
-    (is_hvm_vcpu(current) ?                     \
-     copy_to_user_hvm((dst), (src), (len)) :    \
-     __copy_to_guest_pv(dst, src, len))
+    (GUEST_ACCESS_HVM                           \
+     ?   copy_to_user_hvm(dst, src, len)        \
+     : __copy_to_guest_pv(dst, src, len)) +
 #define __raw_copy_from_guest(dst, src, len)    \
-    (is_hvm_vcpu(current) ?                     \
-     copy_from_user_hvm((dst), (src), (len)) :  \
-     __copy_from_guest_pv(dst, src, len)) +    (GUEST_ACCESS_HVM                           \
+     ?   copy_from_user_hvm(dst, src, len)      \
+     : __copy_from_guest_pv(dst, src, len))
 /*
  * Pre-validate a guest handle.

so they're all consistent and legible, then extended GUEST_ACCESS_HVM to:

xen/arch/x86/include/asm/guest_access.h +0 -0
index 49920e181832..eba47e46de88 100644
@@ -12,7 +12,12 @@

 #include <asm/hvm/support.h>
 #include <asm/hvm/guest_access.h>

-#define GUEST_ACCESS_HVM is_hvm_vcpu(current) +#define GUEST_ACCESS_HVM                        \
+    (IS_ENABLED(GA_ALWAYS_PV)                   \
+     ? 0                                        \
+     : (IS_ENABLED(GA_ALWAYS_HVM)               \
+        ? 1                                     \
+        : is_hvm_vcpu(current)))                \

 /* Raw access functions: no type checking. */ #define raw_copy_to_guest(dst, src, len)        \

to allow ways to force it one way or another.

For forcing it one way or another, while ...

xen/arch/x86/mm.c int new_guest_cr3(mfn_t mfn) · line 3412

Quoted 10 lines
xen/arch/x86/mm.c +3 -0
@@ -3407,6 +3407,9 @@ int new_guest_cr3(mfn_t mfn)
 #endif
 
 #ifdef CONFIG_PV
+
+#define PV_ONLY_SOURCE 1
+

... mm.c is certainly complicated, others like ...

Quoted 25 lines
xen/arch/x86/pv/callback.c +2 -0
@@ -5,6 +5,8 @@
  * hypercall handles and helper functions for guest callback
  */
 
+#define PV_ONLY_SOURCE 1
+
 #include <xen/event.h>
 #include <xen/hypercall.h>
 #include <xen/guest_access.h>
xen/arch/x86/pv/descriptor-tables.c +2 -0
@@ -8,6 +8,8 @@
  * Copyright (c) 2004 Christian Limpach
  */
 
+#define PV_ONLY_SOURCE 1
+
 #include <xen/guest_access.h>
 #include <xen/hypercall.h>
 

xen/arch/x86/pv/shim.c line 14

Quoted 9 lines
xen/arch/x86/pv/shim.c +3 -0
@@ -6,6 +6,9 @@
  *
  * Copyright (c) 2017 Citrix Systems Ltd.
  */
+
+#define PV_ONLY_SOURCE 1
+
 #include <xen/event.h>
 #include <xen/guest_access.h>
 #include <xen/hypercall.h>

all of these could have a general CFLAGS += in arch/x86/pv/Makefile rather than having to state it in every file.

Naming wise, while PV_ONLY_SOURCE works in pv/, it's more dubious in arch/x86/mm.c.  GUEST_ACCESS_* at least identifies the property that's being decided upon.

~Andrew

#2 lore raw
Quoted 96 lines · Andrew Cooper
Quoted 27 lines · Jan Beulich
xen/arch/x86/include/asm/guest_access.h +8 -3
@@ -14,11 +14,15 @@
 
 /* Raw access functions: no type checking. */
 #define raw_copy_to_guest(dst, src, len)        \
-    (is_hvm_vcpu(current) ?                     \
+    ((IS_ENABLED(HVM_ONLY_SOURCE) ||            \
+      (!IS_ENABLED(PV_ONLY_SOURCE) &&           \
+       is_hvm_vcpu(current))) ?                 \
      copy_to_user_hvm((dst), (src), (len)) :    \
      copy_to_guest_pv(dst, src, len))
 #define raw_copy_from_guest(dst, src, len)      \
-    (is_hvm_vcpu(current) ?                     \
+    ((IS_ENABLED(HVM_ONLY_SOURCE) ||            \
+      (!IS_ENABLED(PV_ONLY_SOURCE) &&           \
+       is_hvm_vcpu(current))) ?                 \
      copy_from_user_hvm((dst), (src), (len)) :  \
      copy_from_guest_pv(dst, src, len))
 #define raw_clear_guest(dst,  len)              \
@@ -26,11 +30,15 @@
      clear_user_hvm((dst), (len)) :             \
      clear_guest_pv(dst, len))
 #define __raw_copy_to_guest(dst, src, len)      \
-    (is_hvm_vcpu(current) ?                     \
+    ((IS_ENABLED(HVM_ONLY_SOURCE) ||            \
+      (!IS_ENABLED(PV_ONLY_SOURCE) &&           \

xen/arch/x86/include/asm/guest_access.h line 43

Quoted 10 lines
xen/arch/x86/include/asm/guest_access.h +4 -1
+       is_hvm_vcpu(current))) ?                 \
      copy_to_user_hvm((dst), (src), (len)) :    \
      __copy_to_guest_pv(dst, src, len))
 #define __raw_copy_from_guest(dst, src, len)    \
-    (is_hvm_vcpu(current) ?                     \
+    ((IS_ENABLED(HVM_ONLY_SOURCE) ||            \
+      (!IS_ENABLED(PV_ONLY_SOURCE) &&           \
+       is_hvm_vcpu(current))) ?                 \
      copy_from_user_hvm((dst), (src), (len)) :  \
      __copy_from_guest_pv(dst, src, len))

Looking back at my own attempt to do this, which was from August 2024, I started by cleaning up this code block as such:

xen/arch/x86/include/asm/guest_access.h +0 -0
@@ -12,27 +12,33 @@
  #include <asm/hvm/support.h>
  #include <asm/hvm/guest_access.h>

xen/arch/x86/include/asm/guest_access.h +2 -0
+#define GUEST_ACCESS_HVM is_hvm_vcpu(current)
+

 /* Raw access functions: no type checking. */ #define raw_copy_to_guest(dst, src, len)        \
-    (is_hvm_vcpu(current) ?                     \
-     copy_to_user_hvm((dst), (src), (len)) :    \
-     copy_to_guest_pv(dst, src, len))
+    (GUEST_ACCESS_HVM                           \
+     ? copy_to_user_hvm(dst, src, len)          \
+     : copy_to_guest_pv(dst, src, len))
+
 #define raw_copy_from_guest(dst, src, len)      \
-    (is_hvm_vcpu(current) ?                     \
-     copy_from_user_hvm((dst), (src), (len)) :  \
-     copy_from_guest_pv(dst, src, len))
+    (GUEST_ACCESS_HVM                           \
+     ? copy_from_user_hvm(dst, src, len)        \
+     : copy_from_guest_pv(dst, src, len)) +
 #define raw_clear_guest(dst,  len)              \
-    (is_hvm_vcpu(current) ?                     \
-     clear_user_hvm((dst), (len)) :             \
-     clear_guest_pv(dst, len))
+    (GUEST_ACCESS_HVM                           \
+     ? clear_user_hvm(dst, len)                 \
+     : clear_guest_pv(dst, len))
+
 #define __raw_copy_to_guest(dst, src, len)      \
-    (is_hvm_vcpu(current) ?                     \
-     copy_to_user_hvm((dst), (src), (len)) :    \
-     __copy_to_guest_pv(dst, src, len))
+    (GUEST_ACCESS_HVM                           \
+     ?   copy_to_user_hvm(dst, src, len)        \
+     : __copy_to_guest_pv(dst, src, len)) +
 #define __raw_copy_from_guest(dst, src, len)    \
-    (is_hvm_vcpu(current) ?                     \
-     copy_from_user_hvm((dst), (src), (len)) :  \
-     __copy_from_guest_pv(dst, src, len)) +    (GUEST_ACCESS_HVM                           \
+     ?   copy_from_user_hvm(dst, src, len)      \
+     : __copy_from_guest_pv(dst, src, len))
 /*
  * Pre-validate a guest handle.

Quoted 1 line

so they're all consistent and legible,

Sure, why not. There's one Misra concern though: In my patch I'm specifically avoiding (naming aside)

#define GUEST_ACCESS_HVM is_hvm_vcpu(current)

in the header, as that then requires #undef elsewhere to override. Misra wants us to avoid #undef as much as possible. In x86/mm.c this then would also require the original form to be re-instated. (Yes, I'm not getting away without #undef either, but it's truly removal of the definition, not removal to then re-instate the macro with a different expansion.)

Perhaps something like

#define IS_HVM_ACCESS                    \
    (IS_ENABLED(GUEST_ACCESS_HVM) ||     \
     (!IS_ENABLED(GUEST_ACCESS_PV) &&    \
      is_hvm_vcpu(current)))             \

to reduce redundancy in the macros while keeping the property of my patch?

Quoted 21 lines

then extended GUEST_ACCESS_HVM to:

xen/arch/x86/include/asm/guest_access.h +0 -0
@@ -12,7 +12,12 @@
  #include <asm/hvm/support.h>
  #include <asm/hvm/guest_access.h>

xen/arch/x86/include/asm/guest_access.h +6 -1
-#define GUEST_ACCESS_HVM is_hvm_vcpu(current)
+#define GUEST_ACCESS_HVM                        \
+    (IS_ENABLED(GA_ALWAYS_PV)                   \
+     ? 0                                        \
+     : (IS_ENABLED(GA_ALWAYS_HVM)               \
+        ? 1                                     \
+        : is_hvm_vcpu(current)))                \


 /* Raw access functions: no type checking. */ #define raw_copy_to_guest(dst, src, len)        \

Quoted 1 line

to allow ways to force it one way or another.

Sure, perhaps simplified to

#define GUEST_ACCESS_HVM                        \
    (IS_ENABLED(GA_ALWAYS_PV)                   \
     ? false                                    \
     : (IS_ENABLED(GA_ALWAYS_HVM) ||            \
        is_hvm_vcpu(current)))                  \

or something yet closer to what I have in my patch. The conditional operators aren't overly useful here.

Yet one of the RFC questions remains with this: Is there anywhere we'd actually be able to use GA_ALWAYS_HVM?

Quoted 36 lines

For forcing it one way or another, while ...

xen/arch/x86/mm.c int new_guest_cr3(mfn_t mfn) · line 3412

Quoted 9 lines
xen/arch/x86/mm.c +3 -0
@@ -3407,6 +3407,9 @@ int new_guest_cr3(mfn_t mfn)
 #endif
 
 #ifdef CONFIG_PV
+
+#define PV_ONLY_SOURCE 1
+

... mm.c is certainly complicated, others like ...

Quoted 22 lines
xen/arch/x86/pv/callback.c +2 -0
@@ -5,6 +5,8 @@
  * hypercall handles and helper functions for guest callback
  */
 
+#define PV_ONLY_SOURCE 1
+
 #include <xen/event.h>
 #include <xen/hypercall.h>
 #include <xen/guest_access.h>
xen/arch/x86/pv/descriptor-tables.c +2 -0
@@ -8,6 +8,8 @@
  * Copyright (c) 2004 Christian Limpach
  */
 
+#define PV_ONLY_SOURCE 1
+
 #include <xen/guest_access.h>
 #include <xen/hypercall.h>
 
Quoted 15 lines

xen/arch/x86/pv/shim.c line 14

Quoted 12 lines
xen/arch/x86/pv/shim.c +3 -0
@@ -6,6 +6,9 @@
  *
  * Copyright (c) 2017 Citrix Systems Ltd.
  */
+
+#define PV_ONLY_SOURCE 1
+
 #include <xen/event.h>
 #include <xen/guest_access.h>
 #include <xen/hypercall.h>

all of these could have a general CFLAGS += in arch/x86/pv/Makefile rather than having to state it in every file.

A general CFLAGS += is (at least latently / conceptually) wrong in pv/, as code there may come into play when a PVH Dom0 controls a PV DomU. Much like a fair bit of care is needed in x86/mm.c (where we may want to try to move some more code to x86/pv/mm.c).

I did consider this on a per-object basis and meant to put in a remark to this effect, but then forgot. The issue I see with using this approach is the disconnect between setting of the control (Makefile) and it being correct to use for the particular source file (in particular when making changes down the road). Having the #define near the top of the files has (imo) at least slightly better chances of being noticed when opening the file for editing.

Quoted 3 lines

Naming wise, while PV_ONLY_SOURCE works in pv/, it's more dubious in arch/x86/mm.c.  GUEST_ACCESS_* at least identifies the property that's being decided upon.

I'm happy to see your naming used; I wasn't overly happy with my naming choice anyway.

Jan