From patchwork Fri Mar 20 21:24:52 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Andrew Cooper X-Patchwork-Id: 11450381 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 7D68417E6 for ; Fri, 20 Mar 2020 21:26:12 +0000 (UTC) Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 5693220724 for ; Fri, 20 Mar 2020 21:26:12 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=fail reason="signature verification failed" (1024-bit key) header.d=citrix.com header.i=@citrix.com header.b="fPlCFntX" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 5693220724 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=citrix.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=xen-devel-bounces@lists.xenproject.org Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.89) (envelope-from ) id 1jFP8c-0003tJ-HC; Fri, 20 Mar 2020 21:25:06 +0000 Received: from us1-rack-iad1.inumbo.com ([172.99.69.81]) by lists.xenproject.org with esmtp (Exim 4.89) (envelope-from ) id 1jFP8b-0003t6-2f for xen-devel@lists.xenproject.org; Fri, 20 Mar 2020 21:25:05 +0000 X-Inumbo-ID: 434b48b8-6af1-11ea-a6c1-bc764e2007e4 Received: from esa6.hc3370-68.iphmx.com (unknown [216.71.155.175]) by us1-rack-iad1.inumbo.com (Halon) with ESMTPS id 434b48b8-6af1-11ea-a6c1-bc764e2007e4; Fri, 20 Mar 2020 21:25:04 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=citrix.com; s=securemail; t=1584739504; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=s0Zn8myHzjApfW+BC4Q0BA7rfRL+0OX3tz6cOs5uv+8=; b=fPlCFntXIqtYAZ/iIoew6FT38+PJceG/MiYzvkDz86cwZ/4yXZoPeOVD 9VVc92SzLFYhrNjdWxzBdBZ9WYXoQO+pBqtsN4GfJ8sFZMG9JBIk/n7jN wExoqDGb85eyVcyYdN8ez2hdAhVvKFhffUDtoWLYyxwFJsyYY20dLsWLY g=; Authentication-Results: esa6.hc3370-68.iphmx.com; dkim=none (message not signed) header.i=none; spf=None smtp.pra=andrew.cooper3@citrix.com; spf=Pass smtp.mailfrom=Andrew.Cooper3@citrix.com; spf=None smtp.helo=postmaster@mail.citrix.com Received-SPF: None (esa6.hc3370-68.iphmx.com: no sender authenticity information available from domain of andrew.cooper3@citrix.com) identity=pra; client-ip=162.221.158.21; receiver=esa6.hc3370-68.iphmx.com; envelope-from="Andrew.Cooper3@citrix.com"; x-sender="andrew.cooper3@citrix.com"; x-conformance=sidf_compatible Received-SPF: Pass (esa6.hc3370-68.iphmx.com: domain of Andrew.Cooper3@citrix.com designates 162.221.158.21 as permitted sender) identity=mailfrom; client-ip=162.221.158.21; receiver=esa6.hc3370-68.iphmx.com; envelope-from="Andrew.Cooper3@citrix.com"; x-sender="Andrew.Cooper3@citrix.com"; x-conformance=sidf_compatible; x-record-type="v=spf1"; x-record-text="v=spf1 ip4:209.167.231.154 ip4:178.63.86.133 ip4:195.66.111.40/30 ip4:85.115.9.32/28 ip4:199.102.83.4 ip4:192.28.146.160 ip4:192.28.146.107 ip4:216.52.6.88 ip4:216.52.6.188 ip4:162.221.158.21 ip4:162.221.156.83 ip4:168.245.78.127 ~all" Received-SPF: None (esa6.hc3370-68.iphmx.com: no sender authenticity information available from domain of postmaster@mail.citrix.com) identity=helo; client-ip=162.221.158.21; receiver=esa6.hc3370-68.iphmx.com; envelope-from="Andrew.Cooper3@citrix.com"; x-sender="postmaster@mail.citrix.com"; x-conformance=sidf_compatible IronPort-SDR: JRHYcfyRl5KSUmPI3Ohe9Nq4kMcJOVMvaXu89Mq1qCEr/S+FFeoMI/DgBjxkOsSh+9jR+PVWAy oQ97mB4qZMzUL0EmNQRcDu3f86J94/T6RK8T4t18UXougtk3pv/8dzF7Y9EIf11PY0CZBujTqT rjpWYTWt41826lNJqe5TPFGuj3CyLP1PEAdpECTwwWV60TkjPLdg1CJHs7tTuWMbXSU6Mkw+HP qNJn10qMf/aAtFcy5r2DlFMBycKafvNkTw55hYwjfA2GV+En+W5mNAO+IR9cA9x0DccZxDxVSc ryk= X-SBRS: 2.7 X-MesageID: 14795899 X-Ironport-Server: esa6.hc3370-68.iphmx.com X-Remote-IP: 162.221.158.21 X-Policy: $RELAYED X-IronPort-AV: E=Sophos;i="5.72,286,1580792400"; d="scan'208";a="14795899" From: Andrew Cooper To: Xen-devel Date: Fri, 20 Mar 2020 21:24:52 +0000 Message-ID: <20200320212453.21685-5-andrew.cooper3@citrix.com> X-Mailer: git-send-email 2.11.0 In-Reply-To: <20200320212453.21685-1-andrew.cooper3@citrix.com> References: <20200320212453.21685-1-andrew.cooper3@citrix.com> MIME-Version: 1.0 Subject: [Xen-devel] [PATCH 4/4] xen: Introduce a xmemdup_bytes() helper X-BeenThere: xen-devel@lists.xenproject.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Cc: Andrew Cooper , Wei Liu , Jan Beulich , =?utf-8?q?Roger_Pau_Monn=C3=A9?= Errors-To: xen-devel-bounces@lists.xenproject.org Sender: "Xen-devel" Use it to simplify the x86 microcode logic, taking the opportunity to drop the -ENOMEM printks. Signed-off-by: Andrew Cooper Reviewed-by: Wei Liu Acked-by: Julien Grall --- CC: Jan Beulich CC: Wei Liu CC: Roger Pau Monné --- 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 *);