xen-devel.lists.xenproject.org archive mirror
 help / color / mirror / Atom feed
From: Andrew Cooper <andrew.cooper3@citrix.com>
To: Xen-devel <xen-devel@lists.xenproject.org>
Cc: "Andrew Cooper" <andrew.cooper3@citrix.com>,
	"Wei Liu" <wl@xen.org>, "Jan Beulich" <JBeulich@suse.com>,
	"Roger Pau Monné" <roger.pau@citrix.com>
Subject: [Xen-devel] [PATCH 4/4] xen: Introduce a xmemdup_bytes() helper
Date: Fri, 20 Mar 2020 21:24:52 +0000	[thread overview]
Message-ID: <20200320212453.21685-5-andrew.cooper3@citrix.com> (raw)
In-Reply-To: <20200320212453.21685-1-andrew.cooper3@citrix.com>

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>
---
CC: Jan Beulich <JBeulich@suse.com>
CC: Wei Liu <wl@xen.org>
CC: Roger Pau Monné <roger.pau@citrix.com>
---
 xen/arch/x86/cpu/microcode/amd.c   |  9 ++-------
 xen/arch/x86/cpu/microcode/intel.c |  7 ++-----
 xen/include/xen/xmalloc.h          | 11 +++++++++++
 3 files changed, 15 insertions(+), 12 deletions(-)

diff --git a/xen/arch/x86/cpu/microcode/amd.c b/xen/arch/x86/cpu/microcode/amd.c
index 0998a36b5c..12a3b6b32c 100644
--- a/xen/arch/x86/cpu/microcode/amd.c
+++ b/xen/arch/x86/cpu/microcode/amd.c
@@ -299,11 +299,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,
@@ -336,14 +335,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;
diff --git a/xen/arch/x86/cpu/microcode/intel.c b/xen/arch/x86/cpu/microcode/intel.c
index 6ac5f98694..f26511da98 100644
--- a/xen/arch/x86/cpu/microcode/intel.c
+++ b/xen/arch/x86/cpu/microcode/intel.c
@@ -339,13 +339,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;
 }
 
diff --git a/xen/include/xen/xmalloc.h b/xen/include/xen/xmalloc.h
index f515ceee2a..16979a117c 100644
--- a/xen/include/xen/xmalloc.h
+++ b/xen/include/xen/xmalloc.h
@@ -51,6 +51,17 @@
 #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 *);
 
-- 
2.11.0


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

  parent reply	other threads:[~2020-03-20 21:25 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-03-20 21:24 [Xen-devel] [PATCH 0/4] x86/ucode: Cleanup - Part 2/n Andrew Cooper
2020-03-20 21:24 ` [Xen-devel] [PATCH 1/4] x86/ucode/amd: Fix assertion in compare_patch() Andrew Cooper
2020-03-21 16:45   ` Wei Liu
2020-03-20 21:24 ` [Xen-devel] [PATCH 2/4] x86/ucode: Fix error paths in apply_microcode() Andrew Cooper
2020-03-21 16:49   ` Wei Liu
2020-03-23  8:32   ` Jan Beulich
2020-03-20 21:24 ` [Xen-devel] [PATCH 3/4] xen: Drop raw_smp_processor_id() Andrew Cooper
2020-03-21 10:14   ` Julien Grall
2020-03-21 16:50   ` Wei Liu
2020-03-20 21:24 ` Andrew Cooper [this message]
2020-03-21 16:51   ` [Xen-devel] [PATCH 4/4] xen: Introduce a xmemdup_bytes() helper Wei Liu
2020-03-21 22:19   ` Julien Grall
2020-03-23  8:38     ` Jan Beulich
2020-03-26 15:38       ` Andrew Cooper
2020-03-26 15:47         ` Jan Beulich
2020-03-26 14:53     ` Andrew Cooper
2020-03-26 19:05       ` Julien Grall
2020-03-23  8:41 ` [Xen-devel] [PATCH 0/4] x86/ucode: Cleanup - Part 2/n Jan Beulich

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20200320212453.21685-5-andrew.cooper3@citrix.com \
    --to=andrew.cooper3@citrix.com \
    --cc=JBeulich@suse.com \
    --cc=roger.pau@citrix.com \
    --cc=wl@xen.org \
    --cc=xen-devel@lists.xenproject.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).