xen: Introduce an xmemdup() helper
authorAndrew Cooper <andrew.cooper3@citrix.com>
Thu, 5 Jul 2018 14:19:00 +0000 (14:19 +0000)
committerAndrew Cooper <andrew.cooper3@citrix.com>
Mon, 9 Jul 2018 13:02:58 +0000 (14:02 +0100)
... and use it in place of the opencoded instances.

For consistency, restructure init_domain_cpuid_policy() to be like
init_{domain,vcpu}_msr_policy() by operating on the local pointer where
possible.

No change in behaviour.

Signed-off-by: Andrew Cooper <andrew.cooper3@citrix.com>
Reviewed-by: Jan Beulich <jbeulich@suse.com>
xen/arch/x86/cpuid.c
xen/arch/x86/msr.c
xen/include/xen/xmalloc.h

index c33c6d476673f994a86a727b651bb43f17ace708..3c29191a1b22c34a5aef61c57c533bb5c80dd996 100644 (file)
@@ -703,16 +703,17 @@ void recalculate_cpuid_policy(struct domain *d)
 
 int init_domain_cpuid_policy(struct domain *d)
 {
-    d->arch.cpuid = xmalloc(struct cpuid_policy);
+    struct cpuid_policy *p =
+        xmemdup(is_pv_domain(d) ?  &pv_max_cpuid_policy
+                                : &hvm_max_cpuid_policy);
 
-    if ( !d->arch.cpuid )
+    if ( !p )
         return -ENOMEM;
 
-    *d->arch.cpuid = is_pv_domain(d)
-        ? pv_max_cpuid_policy : hvm_max_cpuid_policy;
-
     if ( d->disable_migrate )
-        d->arch.cpuid->extd.itsc = cpu_has_itsc;
+        p->extd.itsc = cpu_has_itsc;
+
+    d->arch.cpuid = p;
 
     recalculate_cpuid_policy(d);
 
index d035c67d4ce0500234775b699dc31308729574a0..267fac9fdf82b11c0d2e64f676a38cc694b481e4 100644 (file)
@@ -81,16 +81,13 @@ void __init init_guest_msr_policy(void)
 
 int init_domain_msr_policy(struct domain *d)
 {
-    struct msr_domain_policy *dp;
-
-    dp = xmalloc(struct msr_domain_policy);
+    struct msr_domain_policy *dp =
+        xmemdup(is_pv_domain(d) ?  &pv_max_msr_domain_policy
+                                : &hvm_max_msr_domain_policy);
 
     if ( !dp )
         return -ENOMEM;
 
-    *dp = is_pv_domain(d) ? pv_max_msr_domain_policy :
-                            hvm_max_msr_domain_policy;
-
     /* See comment in intel_ctxt_switch_levelling() */
     if ( is_control_domain(d) )
         dp->plaform_info.cpuid_faulting = false;
@@ -103,16 +100,13 @@ int init_domain_msr_policy(struct domain *d)
 int init_vcpu_msr_policy(struct vcpu *v)
 {
     struct domain *d = v->domain;
-    struct msr_vcpu_policy *vp;
-
-    vp = xmalloc(struct msr_vcpu_policy);
+    struct msr_vcpu_policy *vp =
+        xmemdup(is_pv_domain(d) ?  &pv_max_msr_vcpu_policy
+                                : &hvm_max_msr_vcpu_policy);
 
     if ( !vp )
         return -ENOMEM;
 
-    *vp = is_pv_domain(d) ? pv_max_msr_vcpu_policy :
-                            hvm_max_msr_vcpu_policy;
-
     v->arch.msr = vp;
 
     return 0;
index cc2673d8aed25e938312d2056d18588d16e82a91..6e75ad698626732ede056307691511eed4101742 100644 (file)
 #define xmalloc(_type) ((_type *)_xmalloc(sizeof(_type), __alignof__(_type)))
 #define xzalloc(_type) ((_type *)_xzalloc(sizeof(_type), __alignof__(_type)))
 
+/*
+ * Allocate space for a typed object and copy an existing instance.
+ *
+ * Note: Due to const propagating in the typeof(), ptr needs to be mutable.
+ * This can be fixed by changing n_ to being void *, but then we lose type
+ * safety on the return value.
+ */
+#define xmemdup(ptr)                                         \
+({                                                           \
+    typeof(*(ptr)) *p_ = (ptr), *n_ = xmalloc(typeof(*p_));  \
+                                                             \
+    if ( n_ )                                                \
+        memcpy(n_, p_, sizeof(*n_));                         \
+    n_;                                                      \
+})
+
 /* Allocate space for array of typed objects. */
 #define xmalloc_array(_type, _num) \
     ((_type *)_xmalloc_array(sizeof(_type), __alignof__(_type), _num))