Skip to contentSkip to conversation

x86/domctl: Conditionalise x86 domctl using DCE rather than ifdef

PATCH In review Alejandro Vallejo 2 participants 3 messages Feb 10 – Feb 11

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 x86/domctl: Conditionalise x86 domctl using DCE rather than ifdef

Make them uniformly return EOPNOTSUPP when their dependent features are absent. Otherwise the compiler loses context and they might return ENOSYS instead.

debug_op, mem_sharing_op, p2m_audit and psr_alloc change behaviour and return EOPNOTSUPP when compiled out, rather than ENOSYS.

While at it, remove the public headers from mem_sharing.h (forward declarations are fine) and add a missing xen/sched.h include (for complete struct domain definition).

---
v2:
* Also cover p2m_audit
* in mem_sharing.h, restrict the xen/sched.h inclusion to the ifdef.
* bugfix: make psr handler correctly exit early when !IS_ENABLED(...PSR)

---
 xen/arch/x86/domctl.c                  | 33 +++++++++++++++-----------
 xen/arch/x86/include/asm/mem_sharing.h | 12 ++++++----
 xen/arch/x86/include/asm/p2m.h         | 10 ++++----
 3 files changed, 31 insertions(+), 24 deletions(-)
xen/arch/x86/domctl.c +19 -14
index d9521808dc..942f41c584 100644
@@ -220,15 +220,15 @@ long arch_do_domctl(
     {
 
     case XEN_DOMCTL_shadow_op:
-#ifdef CONFIG_PAGING
+        ret = -EOPNOTSUPP;
+        if ( !IS_ENABLED(CONFIG_PAGING) )
+            break;
+
         ret = paging_domctl(d, &domctl->u.shadow_op, u_domctl, 0);
         if ( ret == -ERESTART )
             return hypercall_create_continuation(
                        __HYPERVISOR_paging_domctl_cont, "h", u_domctl);
         copyback = true;
-#else
-        ret = -EOPNOTSUPP;
-#endif
         break;
 
     case XEN_DOMCTL_ioport_permission:
@@ -842,11 +842,14 @@ long arch_do_domctl(
         }
         break;
 
-#ifdef CONFIG_HVM
     case XEN_DOMCTL_debug_op:
     {
         struct vcpu *v;
 
+        ret = -EOPNOTSUPP;
+        if ( !IS_ENABLED(CONFIG_HVM) )
+            break;
+
         ret = -EINVAL;
         if ( (domctl->u.debug_op.vcpu >= d->max_vcpus) ||
              ((v = d->vcpu[domctl->u.debug_op.vcpu]) == NULL) )
@@ -860,7 +863,6 @@ long arch_do_domctl(
         ret = hvm_debug_op(v, domctl->u.debug_op.op);
         break;
     }
-#endif
 
     case XEN_DOMCTL_gdbsx_guestmemio:
     case XEN_DOMCTL_gdbsx_pausevcpu:
@@ -1033,15 +1035,18 @@ long arch_do_domctl(
         break;
     }
 
-#ifdef CONFIG_MEM_SHARING
     case XEN_DOMCTL_mem_sharing_op:
+        ret = -EOPNOTSUPP;
+        if ( !IS_ENABLED(CONFIG_MEM_SHARING) )
+            break;
+
         ret = mem_sharing_domctl(d, &domctl->u.mem_sharing_op);
         break;
-#endif
 
-#if P2M_AUDIT
     case XEN_DOMCTL_audit_p2m:
-        if ( d == currd )
+        if ( !P2M_AUDIT )
+            ret = -EOPNOTSUPP;
+        else if ( d == currd )
             ret = -EPERM;
         else
         {
@@ -1052,7 +1057,6 @@ long arch_do_domctl(
             copyback = true;
         }
         break;
-#endif /* P2M_AUDIT */
 
     case XEN_DOMCTL_set_broken_page_p2m:
     {
@@ -1240,9 +1244,12 @@ long arch_do_domctl(
         break;
 
     case XEN_DOMCTL_psr_alloc:
+        ret = -EOPNOTSUPP;
+        if ( !IS_ENABLED(CONFIG_X86_PSR) )
+            break;
+
         switch ( domctl->u.psr_alloc.cmd )
         {
-#ifdef CONFIG_X86_PSR
         case XEN_DOMCTL_PSR_SET_L3_CBM:
             ret = psr_set_val(d, domctl->u.psr_alloc.target,
                               domctl->u.psr_alloc.data,
@@ -1305,8 +1312,6 @@ long arch_do_domctl(
 
 #undef domctl_psr_get_val
 
-#endif /* CONFIG_X86_PSR */
-
         default:
             ret = -EOPNOTSUPP;
             break;
xen/arch/x86/include/asm/mem_sharing.h +8 -4
index 040962f690..c66cc912db 100644
@@ -9,10 +9,12 @@
 #ifndef __MEM_SHARING_H__
 #define __MEM_SHARING_H__
 
-#include <public/domctl.h>
-#include <public/memory.h>
+struct xen_domctl_mem_sharing_op;
 
 #ifdef CONFIG_MEM_SHARING
+#include <xen/sched.h>
+
+struct xen_mem_sharing_op;
 
 #define mem_sharing_enabled(d) ((d)->arch.hvm.mem_sharing.enabled)
 
@@ -92,8 +94,6 @@ int mem_sharing_fork_reset(struct domain *d, bool reset_state,
 int mem_sharing_notify_enomem(struct domain *d, unsigned long gfn,
                               bool allow_sleep);
 int mem_sharing_memop(XEN_GUEST_HANDLE_PARAM(xen_mem_sharing_op_t) arg);
-int mem_sharing_domctl(struct domain *d,
-                       struct xen_domctl_mem_sharing_op *mec);
 
 /*
  * Scans the p2m and relinquishes any shared pages, destroying
@@ -103,6 +103,7 @@ int mem_sharing_domctl(struct domain *d,
 int relinquish_shared_pages(struct domain *d);
 
 #else
+struct domctl;
 
 #define mem_sharing_enabled(d) false
 
@@ -147,4 +148,7 @@ static inline int mem_sharing_fork_reset(struct domain *d, bool reset_state,
 
 #endif
 
+int mem_sharing_domctl(struct domain *d,
+                       struct xen_domctl_mem_sharing_op *mec);
+
 #endif /* __MEM_SHARING_H__ */
xen/arch/x86/include/asm/p2m.h +4 -6
index 9016e88411..b2721f8c11 100644
@@ -806,12 +806,10 @@ static inline void p2m_pt_init(struct p2m_domain *p2m) {}
 void *map_domain_gfn(struct p2m_domain *p2m, gfn_t gfn, mfn_t *mfn,
                      p2m_query_t q, uint32_t *pfec);
 
-#if P2M_AUDIT
-extern void audit_p2m(struct domain *d,
-                      uint64_t *orphans,
-                      uint64_t *m2p_bad,
-                      uint64_t *p2m_bad);
-#endif /* P2M_AUDIT */
+void audit_p2m(struct domain *d,
+               uint64_t *orphans,
+               uint64_t *m2p_bad,
+               uint64_t *p2m_bad);
 
 /* Printouts */
 #define P2M_PRINTK(f, a...)                                \

base-commit: 2fa468919c39aac189623b6c580ce4ff8592d799

Signature · 1 line
2.43.0
Reviewed-by on v2 1/1
#1 lore raw
Quoted 27 lines · Alejandro Vallejo
xen/arch/x86/include/asm/mem_sharing.h +4 -4
@@ -9,10 +9,12 @@
 #ifndef __MEM_SHARING_H__
 #define __MEM_SHARING_H__
 
-#include <public/domctl.h>
-#include <public/memory.h>
+struct xen_domctl_mem_sharing_op;
 
 #ifdef CONFIG_MEM_SHARING
+#include <xen/sched.h>
+
+struct xen_mem_sharing_op;
 
 #define mem_sharing_enabled(d) ((d)->arch.hvm.mem_sharing.enabled)
 
@@ -92,8 +94,6 @@ int mem_sharing_fork_reset(struct domain *d, bool reset_state,
 int mem_sharing_notify_enomem(struct domain *d, unsigned long gfn,
                               bool allow_sleep);
 int mem_sharing_memop(XEN_GUEST_HANDLE_PARAM(xen_mem_sharing_op_t) arg);
-int mem_sharing_domctl(struct domain *d,
-                       struct xen_domctl_mem_sharing_op *mec);
 
 /*
  * Scans the p2m and relinquishes any shared pages, destroying

xen/arch/x86/include/asm/mem_sharing.h int mem_sharing_domctl(struct domain *d, · line 106

Quoted 4 lines
xen/arch/x86/include/asm/mem_sharing.h +1 -0
@@ -103,6 +103,7 @@ int mem_sharing_domctl(struct domain *d,
 int relinquish_shared_pages(struct domain *d);
 
 #else
+struct domctl;

DYM struct domain? If so (can adjust while committing):

Cc-ing Tamas at least for awareness; I think ./MAINTAINERS should enumerate this file in the X86 MEMORY SHARING section.

Quoted 6 lines
xen/arch/x86/include/asm/p2m.h +0 -0
@@ -806,12 +806,10 @@ static inline void p2m_pt_init(struct p2m_domain *p2m) {}
 void *map_domain_gfn(struct p2m_domain *p2m, gfn_t gfn, mfn_t *mfn,
                      p2m_query_t q, uint32_t *pfec);
 

xen/arch/x86/include/asm/p2m.h static inline void p2m_pt_init(struct p2m_domain *p2m) {} · line 812

Quoted 10 lines
xen/arch/x86/include/asm/p2m.h +4 -6
-#if P2M_AUDIT
-extern void audit_p2m(struct domain *d,
-                      uint64_t *orphans,
-                      uint64_t *m2p_bad,
-                      uint64_t *p2m_bad);
-#endif /* P2M_AUDIT */
+void audit_p2m(struct domain *d,
+               uint64_t *orphans,
+               uint64_t *m2p_bad,
+               uint64_t *p2m_bad);

If already you adjust this beyond dropping the #ifdef, I think we also want to make use of line length, to better match e.g. the other prototype in patch context above. I'm likely to take the liberty of doing so while committing.

Jan

#2 lore raw
Quoted 30 lines · Jan Beulich
Quoted 29 lines · Alejandro Vallejo
xen/arch/x86/include/asm/mem_sharing.h +4 -4
@@ -9,10 +9,12 @@
 #ifndef __MEM_SHARING_H__
 #define __MEM_SHARING_H__
 
-#include <public/domctl.h>
-#include <public/memory.h>
+struct xen_domctl_mem_sharing_op;
 
 #ifdef CONFIG_MEM_SHARING
+#include <xen/sched.h>
+
+struct xen_mem_sharing_op;
 
 #define mem_sharing_enabled(d) ((d)->arch.hvm.mem_sharing.enabled)
 
@@ -92,8 +94,6 @@ int mem_sharing_fork_reset(struct domain *d, bool reset_state,
 int mem_sharing_notify_enomem(struct domain *d, unsigned long gfn,
                               bool allow_sleep);
 int mem_sharing_memop(XEN_GUEST_HANDLE_PARAM(xen_mem_sharing_op_t) arg);
-int mem_sharing_domctl(struct domain *d,
-                       struct xen_domctl_mem_sharing_op *mec);
 
 /*
  * Scans the p2m and relinquishes any shared pages, destroying
@@ -103,6 +103,7 @@ int mem_sharing_domctl(struct domain *d,
 int relinquish_shared_pages(struct domain *d);
 
Quoted 5 lines

xen/arch/x86/include/asm/mem_sharing.h int mem_sharing_domctl(struct domain *d, · line 106

Quoted 2 lines
xen/arch/x86/include/asm/mem_sharing.h +1 -0
 #else
+struct domctl;

DYM struct domain? If so (can adjust while committing):

I did. Annoyingly I only recompiled on with mem_sharing enabled.

Quoted 25 lines

Cc-ing Tamas at least for awareness; I think ./MAINTAINERS should enumerate this file in the X86 MEMORY SHARING section.

Quoted 6 lines
xen/arch/x86/include/asm/p2m.h +0 -0
@@ -806,12 +806,10 @@ static inline void p2m_pt_init(struct p2m_domain *p2m) {}
 void *map_domain_gfn(struct p2m_domain *p2m, gfn_t gfn, mfn_t *mfn,
                      p2m_query_t q, uint32_t *pfec);
 

xen/arch/x86/include/asm/p2m.h static inline void p2m_pt_init(struct p2m_domain *p2m) {} · line 812

Quoted 10 lines
xen/arch/x86/include/asm/p2m.h +4 -6
-#if P2M_AUDIT
-extern void audit_p2m(struct domain *d,
-                      uint64_t *orphans,
-                      uint64_t *m2p_bad,
-                      uint64_t *p2m_bad);
-#endif /* P2M_AUDIT */
+void audit_p2m(struct domain *d,
+               uint64_t *orphans,
+               uint64_t *m2p_bad,
+               uint64_t *p2m_bad);

If already you adjust this beyond dropping the #ifdef, I think we also want to make use of line length, to better match e.g. the other prototype in patch context above. I'm likely to take the liberty of doing so while committing.

Quoted 1 line

Jan

Thanks, Jan.

Alejandro