xen: Introduce a xmemdup_bytes() helper
authorAndrew Cooper <andrew.cooper3@citrix.com>
Fri, 20 Mar 2020 20:53:58 +0000 (20:53 +0000)
committerAndrew Cooper <andrew.cooper3@citrix.com>
Fri, 27 Mar 2020 12:22:00 +0000 (12:22 +0000)
Use it to simplify the x86 microcode logic, taking the opportunity to drop the
-ENOMEM printks.

Signed-off-by: Andrew Cooper <andrew.cooper3@citrix.com>
Reviewed-by: Wei Liu <wl@xen.org>
Acked-by: Julien Grall <jgrall@amazon.com>
xen/arch/x86/cpu/microcode/amd.c
xen/arch/x86/cpu/microcode/intel.c
xen/include/xen/xmalloc.h

index 26b4d47567bb4e006a3e55779b1b95e4a449f88a..122b8309af0b15ded1e9904e903d9899e0212d2d 100644 (file)
@@ -288,11 +288,10 @@ static int get_ucode_from_buffer_amd(
         return -EINVAL;
     }
 
-    mc_amd->mpb = xmalloc_bytes(mpbuf->len);
+    mc_amd->mpb = xmemdup_bytes(mpbuf->data, mpbuf->len);
     if ( !mc_amd->mpb )
         return -ENOMEM;
     mc_amd->mpb_size = mpbuf->len;
-    memcpy(mc_amd->mpb, mpbuf->data, mpbuf->len);
 
     pr_debug("microcode: CPU%d size %zu, block size %u offset %zu equivID %#x rev %#x\n",
              smp_processor_id(), bufsize, mpbuf->len, *offset,
@@ -325,14 +324,10 @@ static int install_equiv_cpu_table(
         return -EINVAL;
     }
 
-    mc_amd->equiv_cpu_table = xmalloc_bytes(mpbuf->len);
+    mc_amd->equiv_cpu_table = xmemdup_bytes(mpbuf->data, mpbuf->len);
     if ( !mc_amd->equiv_cpu_table )
-    {
-        printk(KERN_ERR "microcode: Cannot allocate memory for equivalent cpu table\n");
         return -ENOMEM;
-    }
 
-    memcpy(mc_amd->equiv_cpu_table, mpbuf->data, mpbuf->len);
     mc_amd->equiv_cpu_table_size = mpbuf->len;
 
     return 0;
index 653934c183f47c81bc20cb9fd41d12b4c075790b..78455aa0aeae79e6f9dddac9be895bd35f4977db 100644 (file)
@@ -331,13 +331,10 @@ static long get_next_ucode_from_buffer(struct microcode_intel **mc,
         return -EINVAL;
     }
 
-    *mc = xmalloc_bytes(total_size);
+    *mc = xmemdup_bytes(mc_header, total_size);
     if ( *mc == NULL )
-    {
-        printk(KERN_ERR "microcode: error! Can not allocate memory\n");
         return -ENOMEM;
-    }
-    memcpy(*mc, (const void *)(buf + offset), total_size);
+
     return offset + total_size;
 }
 
index f515ceee2a7318a4c8f50088b8414e8ac7a3ebaf..16979a117c6af3044d656b2f8f9dd625885da578 100644 (file)
 #define xmalloc_bytes(_bytes) _xmalloc(_bytes, SMP_CACHE_BYTES)
 #define xzalloc_bytes(_bytes) _xzalloc(_bytes, SMP_CACHE_BYTES)
 
+/* Allocate untyped storage and copying an existing instance. */
+#define xmemdup_bytes(_src, _nr)                \
+    ({                                          \
+        unsigned long nr_ = (_nr);              \
+        void *dst_ = xmalloc_bytes(nr_);        \
+                                                \
+        if ( dst_ )                             \
+            memcpy(dst_, _src, nr_);            \
+        dst_;                                   \
+    })
+
 /* Free any of the above. */
 extern void xfree(void *);