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

[Xen-changelog] [xen master] xen: Introduce an xmemdup() helper



commit 298253b676a86ccdbc9b1a5f2bfe31dc6d7dd327
Author:     Andrew Cooper <andrew.cooper3@xxxxxxxxxx>
AuthorDate: Thu Jul 5 14:19:00 2018 +0000
Commit:     Andrew Cooper <andrew.cooper3@xxxxxxxxxx>
CommitDate: Mon Jul 9 14:02:58 2018 +0100

    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>
    Reviewed-by: Jan Beulich <jbeulich@xxxxxxxx>
---
 xen/arch/x86/cpuid.c      | 13 +++++++------
 xen/arch/x86/msr.c        | 18 ++++++------------
 xen/include/xen/xmalloc.h | 16 ++++++++++++++++
 3 files changed, 29 insertions(+), 18 deletions(-)

diff --git a/xen/arch/x86/cpuid.c b/xen/arch/x86/cpuid.c
index c33c6d4766..3c29191a1b 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 d035c67d4c..267fac9fdf 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 cc2673d8ae..6e75ad6986 100644
--- a/xen/include/xen/xmalloc.h
+++ b/xen/include/xen/xmalloc.h
@@ -13,6 +13,22 @@
 #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))
--
generated by git-patchbot for /home/xen/git/xen.git#master

_______________________________________________
Xen-changelog mailing list
Xen-changelog@xxxxxxxxxxxxxxxxxxxx
https://lists.xenproject.org/xen-changelog

 


Rackspace

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