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

[Xen-devel] [PATCH 1/2] xen: Introduce an xmemdup() helper



... 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@xxxxxxxxxx>
---
CC: Jan Beulich <JBeulich@xxxxxxxx>
CC: Wei Liu <wei.liu2@xxxxxxxxxx>
CC: Roger Pau Monné <roger.pau@xxxxxxxxxx>
CC: Sergey Dyasli <sergey.dyasli@xxxxxxxxxx>
---
 xen/arch/x86/cpuid.c      | 13 +++++++------
 xen/arch/x86/msr.c        | 18 ++++++------------
 xen/include/xen/xmalloc.h | 10 ++++++++++
 3 files changed, 23 insertions(+), 18 deletions(-)

diff --git a/xen/arch/x86/cpuid.c b/xen/arch/x86/cpuid.c
index c33c6d4..e13c657 100644
--- a/xen/arch/x86/cpuid.c
+++ b/xen/arch/x86/cpuid.c
@@ -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);
 
diff --git a/xen/arch/x86/msr.c b/xen/arch/x86/msr.c
index d035c67..27c4915 100644
--- a/xen/arch/x86/msr.c
+++ b/xen/arch/x86/msr.c
@@ -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;
diff --git a/xen/include/xen/xmalloc.h b/xen/include/xen/xmalloc.h
index cc2673d..34c6588 100644
--- a/xen/include/xen/xmalloc.h
+++ b/xen/include/xen/xmalloc.h
@@ -13,6 +13,16 @@
 #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. */
+#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))
-- 
2.1.4


_______________________________________________
Xen-devel mailing list
Xen-devel@xxxxxxxxxxxxxxxxxxxx
https://lists.xenproject.org/mailman/listinfo/xen-devel

 


Rackspace

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