From nobody Tue Feb 10 00:58:39 2026 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=fail(p=none dis=none) header.from=citrix.com ARC-Seal: i=1; a=rsa-sha256; t=1585649200; cv=none; d=zohomail.com; s=zohoarc; b=FnJaAmUT0lZ2dIfJXgXr0acUgVy9JYQAEIzwUgxP4c277Bs71T6kOo5rR8PQddXcckvY10dFRYsP/rFVBfCn1KWZ5RTEHjaoaxMXap0FjA8NlMAWcaYb/lXISUi0Ty52aTW2xHfaNKQDE4T7lHfhMXdiqe1qoo5fIv7AQKZjakI= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1585649200; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=KxoqkWohyIWEmHBQSnLJq4rys6PG7XJtTgStWCtYJbg=; b=Bf51qOBmRuCKxQEAw5vn097H5JEoAXNvWdeHu8/AbJY0sFe6FRUT2wU+90sw5dO2LBlr0q55JcZJweK56+pa8jfKKMYs5c+M3jKHWlERZyvRkFkbKAsMGcYYnf7Zfk+9FOEHo7KwStwU7G7yJpDQit54jsBebI4AqtNg8wKfwak= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1585649200869526.1937535070946; Tue, 31 Mar 2020 03:06:40 -0700 (PDT) Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.89) (envelope-from ) id 1jJDmV-00060A-Hb; Tue, 31 Mar 2020 10:06:03 +0000 Received: from us1-rack-iad1.inumbo.com ([172.99.69.81]) by lists.xenproject.org with esmtp (Exim 4.89) (envelope-from ) id 1jJDmU-0005yp-4u for xen-devel@lists.xenproject.org; Tue, 31 Mar 2020 10:06:02 +0000 Received: from esa2.hc3370-68.iphmx.com (unknown [216.71.145.153]) by us1-rack-iad1.inumbo.com (Halon) with ESMTPS id 3227e9c6-7337-11ea-b58d-bc764e2007e4; Tue, 31 Mar 2020 10:05:49 +0000 (UTC) X-Inumbo-ID: 3227e9c6-7337-11ea-b58d-bc764e2007e4 DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=citrix.com; s=securemail; t=1585649150; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=+goJqvpvmGqGZD1x/qIZwuQWNz0zmMiWRwF4m9iscHc=; b=eKhJ0VVAV1TaBI9MvhxHTcgFa+ELfXerWO75cirO/DhJa53bZgthyOUJ MK1PYroG59fd9zyyICwX19um1mwrhOdwpN71xM4FXRK6qjKj4foK8VcZR T5U6TMvHyRgdK3NpT7qTCxoIvrg/8Iq3ls9b+9nTQWjejIYMrE+ZTVzpY 0=; Authentication-Results: esa2.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: pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Received-SPF: None (esa2.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=esa2.hc3370-68.iphmx.com; envelope-from="Andrew.Cooper3@citrix.com"; x-sender="andrew.cooper3@citrix.com"; x-conformance=sidf_compatible Received-SPF: Pass (esa2.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=esa2.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 (esa2.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=esa2.hc3370-68.iphmx.com; envelope-from="Andrew.Cooper3@citrix.com"; x-sender="postmaster@mail.citrix.com"; x-conformance=sidf_compatible IronPort-SDR: O8X6k0mJS9C/QIl5W+npqArkraNq0mX+20v63LmYZsWDs904NWhswITkfkXddI7tvNKfraRhxQ GU66pbG20KrDBLKzW43RHNdWMi+Khh89aP8T4EQVMCPtfGZAsF1Grl+5LvoIbhvIyqg6BW8BOQ GoczDJx20uIfs2+Je3UIpp4FH987qlZSgul73fUwXmOMVzLGUdNzYbj5asRJZtc0sCC5Nt11yo 0jx8Xowb+9n96+EqqM3QejxNsUSUL4aYRBAOVCCyo6uAdyHc8j1ZUUFP6GZOEVT55cfHPunvwl SoY= X-SBRS: 2.7 X-MesageID: 14930062 X-Ironport-Server: esa2.hc3370-68.iphmx.com X-Remote-IP: 162.221.158.21 X-Policy: $RELAYED X-IronPort-AV: E=Sophos;i="5.72,327,1580792400"; d="scan'208";a="14930062" From: Andrew Cooper To: Xen-devel Subject: [PATCH 09/11] x86/ucode/amd: Remove gratuitous memory allocations from cpu_request_microcode() Date: Tue, 31 Mar 2020 11:05:29 +0100 Message-ID: <20200331100531.4294-10-andrew.cooper3@citrix.com> X-Mailer: git-send-email 2.11.0 In-Reply-To: <20200331100531.4294-1-andrew.cooper3@citrix.com> References: <20200331100531.4294-1-andrew.cooper3@citrix.com> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable 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=20Pau=20Monn=C3=A9?= Errors-To: xen-devel-bounces@lists.xenproject.org Sender: "Xen-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Just as on the Intel side, there is no point having get_ucode_from_buffer_amd() make $N memory allocations and free $N-1 of the= m. Delete get_ucode_from_buffer_amd() and rewrite the loop in cpu_request_microcode() to have 'saved' point into 'buf' until we finally decide to duplicate that blob and return it to our caller. Introduce a new struct container_microcode to simplify interpreting the container format. Doubly indent the logic to substantially reduce the churn in a later change. Signed-off-by: Andrew Cooper Reviewed-by: Jan Beulich --- CC: Jan Beulich CC: Wei Liu CC: Roger Pau Monn=C3=A9 --- xen/arch/x86/cpu/microcode/amd.c | 138 +++++++++++++----------------------= ---- 1 file changed, 47 insertions(+), 91 deletions(-) diff --git a/xen/arch/x86/cpu/microcode/amd.c b/xen/arch/x86/cpu/microcode/= amd.c index 980e61c547..ae1276988f 100644 --- a/xen/arch/x86/cpu/microcode/amd.c +++ b/xen/arch/x86/cpu/microcode/amd.c @@ -70,6 +70,11 @@ struct mpbhdr { uint32_t len; uint8_t data[]; }; +struct container_microcode { + uint32_t type; /* UCODE_UCODE_TYPE */ + uint32_t len; + struct microcode_header_amd patch[]; +}; =20 /* * Microcode updates for different CPUs are distinguished by their @@ -280,52 +285,6 @@ static int apply_microcode(const struct microcode_patc= h *patch) return 0; } =20 -static int get_ucode_from_buffer_amd( - struct microcode_amd *mc_amd, - const void *buf, - size_t bufsize, - size_t *offset) -{ - const struct mpbhdr *mpbuf =3D buf + *offset; - - /* No more data */ - if ( *offset >=3D bufsize ) - { - printk(KERN_ERR "microcode: Microcode buffer overrun\n"); - return -EINVAL; - } - - if ( mpbuf->type !=3D UCODE_UCODE_TYPE ) - { - printk(KERN_ERR "microcode: Wrong microcode payload type field\n"); - return -EINVAL; - } - - if ( (*offset + mpbuf->len) > bufsize ) - { - printk(KERN_ERR "microcode: Bad data in microcode data file\n"); - return -EINVAL; - } - - if ( !verify_patch_size(mpbuf->len) ) - { - printk(XENLOG_ERR "microcode: patch size mismatch\n"); - return -EINVAL; - } - - mc_amd->mpb =3D xmemdup_bytes(mpbuf->data, mpbuf->len); - if ( !mc_amd->mpb ) - return -ENOMEM; - - pr_debug("microcode: CPU%d size %zu, block size %u offset %zu equivID = %#x rev %#x\n", - smp_processor_id(), bufsize, mpbuf->len, *offset, - mc_amd->mpb->processor_rev_id, mc_amd->mpb->patch_id); - - *offset +=3D mpbuf->len + SECTION_HDR_SIZE; - - return 0; -} - static int scan_equiv_cpu_table( const void *data, size_t size_left, @@ -430,9 +389,9 @@ static int container_fast_forward(const void *data, siz= e_t size_left, size_t *of static struct microcode_patch *cpu_request_microcode(const void *buf, size= _t size) { struct microcode_amd *mc_amd; - struct microcode_header_amd *saved =3D NULL; + const struct microcode_header_amd *saved =3D NULL; struct microcode_patch *patch =3D NULL; - size_t offset =3D 0; + size_t offset =3D 0, saved_size =3D 0; int error =3D 0; unsigned int cpu =3D smp_processor_id(); const struct cpu_signature *sig =3D &per_cpu(cpu_sig, cpu); @@ -497,57 +456,54 @@ static struct microcode_patch *cpu_request_microcode(= const void *buf, size_t siz * It's possible the data file has multiple matching ucode, * lets keep searching till the latest version */ - while ( (error =3D get_ucode_from_buffer_amd(mc_amd, buf, size, - &offset)) =3D=3D 0 ) + buf +=3D offset; + size -=3D offset; { - /* - * If the new ucode covers current CPU, compare ucodes and store t= he - * one with higher revision. - */ - if ( (microcode_fits(mc_amd->mpb) !=3D MIS_UCODE) && - (!saved || (compare_header(mc_amd->mpb, saved) =3D=3D NEW_UCO= DE)) ) + while ( size ) { - xfree(saved); - saved =3D mc_amd->mpb; - } - else - { - xfree(mc_amd->mpb); - mc_amd->mpb =3D NULL; - } + const struct container_microcode *mc; + + if ( size < sizeof(*mc) || + (mc =3D buf)->type !=3D UCODE_UCODE_TYPE || + size - sizeof(*mc) < mc->len || + !verify_patch_size(mc->len) ) + { + printk(XENLOG_ERR "microcode: Bad microcode data\n"); + error =3D -EINVAL; + break; + } =20 - if ( offset >=3D size ) - break; + /* + * If the new ucode covers current CPU, compare ucodes and sto= re the + * one with higher revision. + */ + if ( (microcode_fits(mc->patch) !=3D MIS_UCODE) && + (!saved || (compare_header(mc->patch, saved) =3D=3D NEW_U= CODE)) ) + { + saved =3D mc->patch; + saved_size =3D mc->len; + } =20 - /* - * 1. Given a situation where multiple containers exist and correct - * patch lives on a container that is not the last container. - * 2. We match equivalent ids using find_equiv_cpu_id() from the - * earlier while() (On this case, matches on earlier container - * file and we break) - * 3. Proceed to while ( (error =3D get_ucode_from_buffer_amd(mc_a= md, - * buf, size, &offset)) =3D=3D 0 ) - * 4. Find correct patch using microcode_fits() and apply the patch - * (Assume: apply_microcode() is successful) - * 5. The while() loop from (3) continues to parse the binary as - * there is a subsequent container file, but... - * 6. ...a correct patch can only be on one container and not on a= ny - * subsequent ones. (Refer docs for more info) Therefore, we - * don't have to parse a subsequent container. So, we can abort - * the process here. - * 7. This ensures that we retain a success value (=3D 0) to 'erro= r' - * before if ( mpbuf->type !=3D UCODE_UCODE_TYPE ) evaluates to - * false and returns -EINVAL. - */ - if ( offset + SECTION_HDR_SIZE <=3D size && - *(const uint32_t *)(buf + offset) =3D=3D UCODE_MAGIC ) - break; + /* Move over the microcode blob. */ + buf +=3D sizeof(*mc) + mc->len; + size -=3D sizeof(*mc) + mc->len; + + /* + * Peek ahead. If we see the start of another container, we've + * exhaused all microcode blobs in this container. Exit clean= ly. + */ + if ( size >=3D 4 && *(const uint32_t *)buf =3D=3D UCODE_MAGIC ) + break; + } } =20 if ( saved ) { - mc_amd->mpb =3D saved; - patch =3D mc_amd; + mc_amd->mpb =3D xmemdup_bytes(saved, saved_size); + if ( mc_amd->mpb ) + patch =3D mc_amd; + else + error =3D -ENOMEM; } else free_patch(mc_amd); --=20 2.11.0