From nobody Mon Nov 3 06:32:40 2025 Delivered-To: importer@patchew.org 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; Authentication-Results: mx.zohomail.com; dkim=pass; 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=pass(p=reject dis=none) header.from=citrix.com ARC-Seal: i=1; a=rsa-sha256; t=1731446392; cv=none; d=zohomail.com; s=zohoarc; b=K79QFvYVezx9d0cgrmZ6b7uDanIwd66LyDq6wRHn94TbLW1406JQ33TxajxzYdDhB6weURzfdb3M4Ywh16w4wVisdyoKAkqmPGTnLTDeN2jMChxTCU2AU8aLSLAqd5X1zJ6PPU1LCY9YHDFZrcsBJybzb6IpYTZK7lwV4MlYNlo= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1731446392; h=Content-Type:Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=jsa12gmbMNZ4JYTlfQsRQq4C/O5aUYd1O0nPblfp3iE=; b=c2qzQVEi65HoXzk8m9GH1GBzyguEXMKKX0my44Ht97IJ7Y9osDHfPER6DahwoVlmB+6qoVeScUjoeNUaDxoelhG6usCwRrk4kmxwn4JHrnW0nk5jQZjGUa/B6SjW/uhJt+PWrRBru8Bn9h2RQPdB4eO90dC5kgElrajyGEMVLX8= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; 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=pass header.from= (p=reject dis=none) Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1731446392025391.4015284956025; Tue, 12 Nov 2024 13:19:52 -0800 (PST) Received: from list by lists.xenproject.org with outflank-mailman.834936.1250736 (Exim 4.92) (envelope-from ) id 1tAyIK-0002Gk-7l; Tue, 12 Nov 2024 21:19:28 +0000 Received: by outflank-mailman (output) from mailman id 834936.1250736; Tue, 12 Nov 2024 21:19:28 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1tAyIK-0002Gd-4T; Tue, 12 Nov 2024 21:19:28 +0000 Received: by outflank-mailman (input) for mailman id 834936; Tue, 12 Nov 2024 21:19:26 +0000 Received: from se1-gles-flk1-in.inumbo.com ([94.247.172.50] helo=se1-gles-flk1.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1tAyII-0002GE-Jy for xen-devel@lists.xenproject.org; Tue, 12 Nov 2024 21:19:26 +0000 Received: from mail-ej1-x630.google.com (mail-ej1-x630.google.com [2a00:1450:4864:20::630]) by se1-gles-flk1.inumbo.com (Halon) with ESMTPS id c8326aed-a13b-11ef-99a3-01e77a169b0f; Tue, 12 Nov 2024 22:19:20 +0100 (CET) Received: by mail-ej1-x630.google.com with SMTP id a640c23a62f3a-aa1e51ce601so174359166b.3 for ; Tue, 12 Nov 2024 13:19:20 -0800 (PST) Received: from andrewcoop.eng.citrite.net ([185.25.67.249]) by smtp.gmail.com with ESMTPSA id a640c23a62f3a-a9ee0defaefsm762049066b.164.2024.11.12.13.19.19 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 12 Nov 2024 13:19:19 -0800 (PST) X-Outflank-Mailman: Message body and most headers restored to incoming version X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" X-Inumbo-ID: c8326aed-a13b-11ef-99a3-01e77a169b0f X-Custom-Connection: eyJyZW1vdGVpcCI6IjJhMDA6MTQ1MDo0ODY0OjIwOjo2MzAiLCJoZWxvIjoibWFpbC1lajEteDYzMC5nb29nbGUuY29tIn0= X-Custom-Transaction: eyJpZCI6ImM4MzI2YWVkLWExM2ItMTFlZi05OWEzLTAxZTc3YTE2OWIwZiIsInRzIjoxNzMxNDQ2MzYwLjcyMTg0LCJzZW5kZXIiOiJhbmRyZXcuY29vcGVyQGNsb3VkLmNvbSIsInJlY2lwaWVudCI6Inhlbi1kZXZlbEBsaXN0cy54ZW5wcm9qZWN0Lm9yZyJ9 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=citrix.com; s=google; t=1731446360; x=1732051160; darn=lists.xenproject.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=jsa12gmbMNZ4JYTlfQsRQq4C/O5aUYd1O0nPblfp3iE=; b=Im5nBlCwWip2eFlaCHX4aY+OqjPY8wk+qhyyrWvQXO+6TmNsw2gi9fu1HVUqF3Xjq6 Q6hs/eVpxVv+2H5CWv/vhCq3hxyOejrlWypwa3s9+nXJXlHdrlf7A5UcdBhIE9GGohIY xr4mY112BRi5Tko4fII7SrbQlS244r4meTUHU= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1731446360; x=1732051160; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=jsa12gmbMNZ4JYTlfQsRQq4C/O5aUYd1O0nPblfp3iE=; b=BhYYcSDcTzj6y36KLV5MH/lVUDOTSPrSs0302jYi4/17e+qMCmKD7g3jCB8UBnDmDn 5ispBdSP8vf2Q/CjNFi8L6brluJtt4tXlnxPo+fT2a8PIgNMGT74F2FjRlYu2Y03yPY5 7cnWUb7T2dgOSxviQryccr0G6/eKzqguUekq/jPTqBsPNxhpAu0XebZ4XQXnRa4KI3i5 K9jQqMo8wpq/QUnnQ2BfFPmwevxCbyx+Xsl1VcSFBcavFfCx8GVJwtsV3oapI5aWpraw mvJnduRy4kJ2NVMFiW/tSMVT6LqJdOVX13STehJx90wRabBeCU73Lq9HKH+j+p5Fg7ZJ lf5A== X-Gm-Message-State: AOJu0YyjTFQBabnQxdmPPg8QM2srJRiZ4F9F+Gm7YOjJTdnZOFCNTlYL GUygjCl+Vm8r41zAOGbQDFxlKxFZJDTdl0VrrsXPvtkyUMpYbIrt2cOHM1B7QahVxeu5ygZMycb T X-Google-Smtp-Source: AGHT+IEI+uILRhB7vfT10ROk8wzxcMHa3aAYpcdopfiCXVqktNFi+WzYOeKEVsFBdreGQMhTBDC9fA== X-Received: by 2002:a17:907:9486:b0:a99:4f40:3e82 with SMTP id a640c23a62f3a-a9eefeb2bacmr1612502366b.7.1731446359564; Tue, 12 Nov 2024 13:19:19 -0800 (PST) From: Andrew Cooper To: Xen-devel Cc: Andrew Cooper , Jan Beulich , =?UTF-8?q?Roger=20Pau=20Monn=C3=A9?= Subject: [PATCH v2 1/3] x86/ucode: Remove the collect_cpu_info() call from parse_blob() Date: Tue, 12 Nov 2024 21:19:13 +0000 Message-Id: <20241112211915.1473121-2-andrew.cooper3@citrix.com> X-Mailer: git-send-email 2.39.5 In-Reply-To: <20241112211915.1473121-1-andrew.cooper3@citrix.com> References: <20241112211915.1473121-1-andrew.cooper3@citrix.com> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @citrix.com) X-ZM-MESSAGEID: 1731446392988116600 With the tangle of logic starting to come under control, it is now plain to see that parse_blob()'s side effect of re-gathering the signature/revision = is pointless. The cpu_request_microcode() hooks need the signature only. The BSP gathers this in early_microcode_init(), the APs and S3 in microcode_update_cpu(). = For good measure, the apply_microcode() hooks also keep the revision correct as load attempts are made. This finally gets us down to a single call per CPU on boot / S3 resume, and= no calls during late-load hypercalls. No functional change. Signed-off-by: Andrew Cooper Reviewed-by: Jan Beulich --- CC: Jan Beulich CC: Roger Pau Monn=C3=A9 --- xen/arch/x86/cpu/microcode/core.c | 2 -- 1 file changed, 2 deletions(-) diff --git a/xen/arch/x86/cpu/microcode/core.c b/xen/arch/x86/cpu/microcode= /core.c index fd4b08b45388..5897ec54032a 100644 --- a/xen/arch/x86/cpu/microcode/core.c +++ b/xen/arch/x86/cpu/microcode/core.c @@ -189,8 +189,6 @@ static struct patch_with_flags nmi_patch =3D */ static struct microcode_patch *parse_blob(const char *buf, size_t len) { - alternative_vcall(ucode_ops.collect_cpu_info); - return alternative_call(ucode_ops.cpu_request_microcode, buf, len, tru= e); } =20 --=20 2.39.5 From nobody Mon Nov 3 06:32:40 2025 Delivered-To: importer@patchew.org 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; Authentication-Results: mx.zohomail.com; dkim=pass; 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=pass(p=reject dis=none) header.from=citrix.com ARC-Seal: i=1; a=rsa-sha256; t=1731446391; cv=none; d=zohomail.com; s=zohoarc; b=AB5o5/5zvERT5xcJDZwY4g1Oomt1DCaVp+zYifit+mrPlH8arHlXTDz6BuuoLFeRnIjzm5t2B4nTYujK6CVR4GpSQtv6nYLuH+c6D0sK6gVdwhsYJ+q9PDWhR61FUoTLi/3EhOXHYfm0EVKiCrA/A1rot1zuwjHhx4U7jcGRJvE= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1731446391; h=Content-Type:Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=bXeqEdvAbZY1hWBgh5PB6EHEOkfnXP4Z7DiK6RDTCRc=; b=d7qzrc26WyLO6FzGbz1BmIxDGVcULmC1C2z2O05rrbAaGPm2ZGyWVJtXDFjckEX2Y0Iy0n3ch2PzN0Z+KZCV4oYK+Hk0dcPMu3RPxCo4+f2Hcw2Ue+yquQnh137A4dEemaem+KtldAUe9lAq5NiJOsDD226pzqiKeATN6gGCt9c= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; 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=pass header.from= (p=reject dis=none) Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1731446391240709.5453283885049; Tue, 12 Nov 2024 13:19:51 -0800 (PST) Received: from list by lists.xenproject.org with outflank-mailman.834939.1250756 (Exim 4.92) (envelope-from ) id 1tAyIL-0002cZ-7c; Tue, 12 Nov 2024 21:19:29 +0000 Received: by outflank-mailman (output) from mailman id 834939.1250756; Tue, 12 Nov 2024 21:19:29 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1tAyIL-0002aE-2e; Tue, 12 Nov 2024 21:19:29 +0000 Received: by outflank-mailman (input) for mailman id 834939; Tue, 12 Nov 2024 21:19:27 +0000 Received: from se1-gles-flk1-in.inumbo.com ([94.247.172.50] helo=se1-gles-flk1.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1tAyIJ-0002GE-OU for xen-devel@lists.xenproject.org; Tue, 12 Nov 2024 21:19:27 +0000 Received: from mail-ej1-x62c.google.com (mail-ej1-x62c.google.com [2a00:1450:4864:20::62c]) by se1-gles-flk1.inumbo.com (Halon) with ESMTPS id c90b5a56-a13b-11ef-99a3-01e77a169b0f; Tue, 12 Nov 2024 22:19:22 +0100 (CET) Received: by mail-ej1-x62c.google.com with SMTP id a640c23a62f3a-a99f646ff1bso922054666b.2 for ; Tue, 12 Nov 2024 13:19:22 -0800 (PST) Received: from andrewcoop.eng.citrite.net ([185.25.67.249]) by smtp.gmail.com with ESMTPSA id a640c23a62f3a-a9ee0defaefsm762049066b.164.2024.11.12.13.19.19 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 12 Nov 2024 13:19:19 -0800 (PST) X-Outflank-Mailman: Message body and most headers restored to incoming version X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" X-Inumbo-ID: c90b5a56-a13b-11ef-99a3-01e77a169b0f X-Custom-Connection: eyJyZW1vdGVpcCI6IjJhMDA6MTQ1MDo0ODY0OjIwOjo2MmMiLCJoZWxvIjoibWFpbC1lajEteDYyYy5nb29nbGUuY29tIn0= X-Custom-Transaction: eyJpZCI6ImM5MGI1YTU2LWExM2ItMTFlZi05OWEzLTAxZTc3YTE2OWIwZiIsInRzIjoxNzMxNDQ2MzYyLjE1MzI3Miwic2VuZGVyIjoiYW5kcmV3LmNvb3BlckBjbG91ZC5jb20iLCJyZWNpcGllbnQiOiJ4ZW4tZGV2ZWxAbGlzdHMueGVucHJvamVjdC5vcmcifQ== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=citrix.com; s=google; t=1731446361; x=1732051161; darn=lists.xenproject.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=bXeqEdvAbZY1hWBgh5PB6EHEOkfnXP4Z7DiK6RDTCRc=; b=Y6eo8II4Sft87xefMFJ5TAX4WO5E7olFZJqfqbEh9+EhcIGtX9UJdH6nPGTuyKJ4jS n+Emac6sgclu57N2Bz/TMrg44yfbyn+cfTcpbpyBLS4vFSX011hXASBG4xwJQKndFnKP CELceyCN3WoyuJspyOjnaf1UnX0ZcHGnjgUxE= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1731446361; x=1732051161; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=bXeqEdvAbZY1hWBgh5PB6EHEOkfnXP4Z7DiK6RDTCRc=; b=aD2Zlm6SaN1oNHvZopxa+X19UkR6mfOZdKMoFHnQl4Ze566pLjhqxTeT82nhj8AoJ5 PYw6bWlD1kBHre1e+RatGJa3bam0Yg3i9IKcTNUET6ZmhzOkEwi//JPHc/ONvdKVAkOv pefygGdB7LYuVO8HTQlGoolD/BAcPksFcjXfs0XERtFEvuaHTvoqYTDElNSVbu3hYth7 ywD8BWQtuNE3dRLsMfPHxyWWIpPVSVZwSfdrXDU/TCb2/fwCq6Bn+tOMwDKglWO/gY5c hHuZQhxPLpKxFjRR26/XJ6/w20tOFcWFlhq3YDbu6wgjnXRhs+ahSU3zI7O+x2ERG+wJ Udgw== X-Gm-Message-State: AOJu0Yx7I9qt/Uzp2alfkog29x24aohe8RVRtcnfiBKPPwDtSxfTh6MU owCt/+QapeSyimFeI2+/8Qynth5UIpMT21MwOaEbPn3T7QBqz8F3VjtE2DQH3A8TnOJUCTqy0Pz l X-Google-Smtp-Source: AGHT+IFjrmJwiNSREOn7qFXBleK0HcSvHQ14DMXgAqdWjXwH2NYh+4Itd++JTpf1sV0e8HYJTjMHjw== X-Received: by 2002:a17:907:1b0e:b0:a9a:1792:f05 with SMTP id a640c23a62f3a-aa1b10a45a5mr450483466b.31.1731446361142; Tue, 12 Nov 2024 13:19:21 -0800 (PST) From: Andrew Cooper To: Xen-devel Cc: Andrew Cooper , Jan Beulich , =?UTF-8?q?Roger=20Pau=20Monn=C3=A9?= , Stefano Stabellini Subject: [PATCH v2 2/3] x86/ucode: Fix cache handling in microcode_update_helper() Date: Tue, 12 Nov 2024 21:19:14 +0000 Message-Id: <20241112211915.1473121-3-andrew.cooper3@citrix.com> X-Mailer: git-send-email 2.39.5 In-Reply-To: <20241112211915.1473121-1-andrew.cooper3@citrix.com> References: <20241112211915.1473121-1-andrew.cooper3@citrix.com> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @citrix.com) X-ZM-MESSAGEID: 1731446393216116600 microcode_update_cache() now has a single caller, but inlining it shows how unnecessarily complicated the logic really is. Outside of error paths, there is always one microcode patch to free. Its either result of parse_blob(), or it's the old cached value. In order to fix this, have a local patch pointer (mostly to avoid the unnecessary verbosity of patch_with_flags.patch), and always free it at the end. The only error path needing care is the IS_ERR(patch) path, which is easy enough to handle. Also, widen the scope of result. We only need to call compare_patch() once, and the answer is still good later when updating the cache. In order to update the cache, simply SWAP() the patch and the cache pointers, allowing = the singular xfree() at the end to cover both cases. This also removes all callers microcode_free_patch() which fixes the need to cast away const to allow it to compile. This also removed several violatio= ns of MISRA Rule 11.8 which disallows casting away const. No functional change. Signed-off-by: Andrew Cooper Reviewed-by: Jan Beulich --- CC: Jan Beulich CC: Roger Pau Monn=C3=A9 CC: Stefano Stabellini --- xen/arch/x86/cpu/microcode/core.c | 66 +++++++++++-------------------- 1 file changed, 24 insertions(+), 42 deletions(-) diff --git a/xen/arch/x86/cpu/microcode/core.c b/xen/arch/x86/cpu/microcode= /core.c index 5897ec54032a..0cc5daa251e2 100644 --- a/xen/arch/x86/cpu/microcode/core.c +++ b/xen/arch/x86/cpu/microcode/core.c @@ -86,7 +86,7 @@ struct patch_with_flags { static bool ucode_in_nmi =3D true; =20 /* Protected by microcode_mutex */ -static const struct microcode_patch *microcode_cache; +static struct microcode_patch *microcode_cache; =20 /* * opt_mod_idx and opt_scan have subtle semantics. @@ -192,33 +192,6 @@ static struct microcode_patch *parse_blob(const char *= buf, size_t len) return alternative_call(ucode_ops.cpu_request_microcode, buf, len, tru= e); } =20 -static void microcode_free_patch(const struct microcode_patch *patch) -{ - xfree((struct microcode_patch *)patch); -} - -/* Return true if cache gets updated. Otherwise, return false */ -static bool microcode_update_cache(const struct microcode_patch *patch) -{ - ASSERT(spin_is_locked(µcode_mutex)); - - if ( !microcode_cache ) - microcode_cache =3D patch; - else if ( alternative_call(ucode_ops.compare_patch, - patch, microcode_cache) =3D=3D NEW_UCODE ) - { - microcode_free_patch(microcode_cache); - microcode_cache =3D patch; - } - else - { - microcode_free_patch(patch); - return false; - } - - return true; -} - /* Returns true if ucode should be loaded on a given cpu */ static bool is_cpu_primary(unsigned int cpu) { @@ -496,6 +469,8 @@ struct ucode_buf { =20 static long cf_check microcode_update_helper(void *data) { + struct microcode_patch *patch =3D NULL; + enum microcode_match_result result; int ret; struct ucode_buf *buffer =3D data; unsigned int cpu, updated; @@ -524,17 +499,20 @@ static long cf_check microcode_update_helper(void *da= ta) goto put; } =20 - patch_with_flags.patch =3D parse_blob(buffer->buffer, buffer->len); + patch =3D parse_blob(buffer->buffer, buffer->len); patch_with_flags.flags =3D buffer->flags; + xfree(buffer); - if ( IS_ERR(patch_with_flags.patch) ) + + if ( IS_ERR(patch) ) { - ret =3D PTR_ERR(patch_with_flags.patch); + ret =3D PTR_ERR(patch); + patch =3D NULL; printk(XENLOG_WARNING "Parsing microcode blob error %d\n", ret); goto put; } =20 - if ( !patch_with_flags.patch ) + if ( !patch ) { printk(XENLOG_WARNING "microcode: couldn't find any matching ucode= in " "the provided blob!\n"); @@ -549,10 +527,7 @@ static long cf_check microcode_update_helper(void *dat= a) spin_lock(µcode_mutex); if ( microcode_cache ) { - enum microcode_match_result result; - - result =3D alternative_call(ucode_ops.compare_patch, - patch_with_flags.patch, microcode_cache); + result =3D alternative_call(ucode_ops.compare_patch, patch, microc= ode_cache); =20 if ( result !=3D NEW_UCODE && !(ucode_force && (result =3D=3D OLD_UCODE || result =3D=3D SA= ME_UCODE)) ) @@ -561,12 +536,13 @@ static long cf_check microcode_update_helper(void *da= ta) printk(XENLOG_WARNING "microcode: couldn't find any newer%s revision in the p= rovided blob!\n", ucode_force ? " (or a valid)" : ""); - microcode_free_patch(patch_with_flags.patch); ret =3D -EEXIST; =20 goto put; } } + else + result =3D NEW_UCODE; spin_unlock(µcode_mutex); =20 cpumask_clear(&cpu_callin_map); @@ -593,14 +569,18 @@ static long cf_check microcode_update_helper(void *da= ta) * this requirement can be relaxed in the future. Right now, this is * conservative and good. */ + patch_with_flags.patch =3D patch; ret =3D stop_machine_run(do_microcode_update, &patch_with_flags, NR_CP= US); =20 updated =3D atomic_read(&cpu_updated); if ( updated > 0 ) { - spin_lock(µcode_mutex); - microcode_update_cache(patch_with_flags.patch); - spin_unlock(µcode_mutex); + if ( result =3D=3D NEW_UCODE ) + { + spin_lock(µcode_mutex); + SWAP(patch, microcode_cache); + spin_unlock(µcode_mutex); + } =20 /* * Refresh the raw CPU policy, in case the features have changed. @@ -615,8 +595,6 @@ static long cf_check microcode_update_helper(void *data) if ( ctxt_switch_masking ) alternative_vcall(ctxt_switch_masking, current); } - else - microcode_free_patch(patch_with_flags.patch); =20 if ( updated && updated !=3D nr_cores ) printk(XENLOG_ERR "ERROR: Updating microcode succeeded on %u cores= and failed\n" @@ -627,6 +605,10 @@ static long cf_check microcode_update_helper(void *dat= a) =20 put: put_cpu_maps(); + + /* The parsed blob or old cached value, whichever we're not keeping. */ + xfree(patch); + return ret; } =20 --=20 2.39.5 From nobody Mon Nov 3 06:32:40 2025 Delivered-To: importer@patchew.org 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; Authentication-Results: mx.zohomail.com; dkim=pass; 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=pass(p=reject dis=none) header.from=citrix.com ARC-Seal: i=1; a=rsa-sha256; t=1731446391; cv=none; d=zohomail.com; s=zohoarc; b=f9opJnJe9ygiw0xj3Tt/hfmIk3XsJWHpl/SZgOQfQGjSqUys1HGN2LAFcZdizxYbpmXZACaEIG17R3H3dj1IUOGHtusF0KotXnZsSr5ACFJHD7M8jKp81XzxEOtM/NtW2kQJcrCyZpZ9zamgU2fsRMPHVINzQrJDBpGFfgygWkw= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1731446391; h=Content-Type:Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=eAYyhUk3NiNBRpvH7yor3qXtJxAbrlhbvjZAvpbioVA=; b=UV9R+cOF/Uj3nyQV3XbCz1kj6SFhD2/9RSd9PI1G8ouwmtxLPXK5ngjeVfBCM6Ky0sRF2gGHigFLQ0gz7hp+jaFvveBF/JTH/yQL8i+NQHrIKWu1mwvJvgW+1vnyXb/12suZBDnrebbjbuXu5taDnCSsaFfwZ79kMaPc4AiYLhY= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; 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=pass header.from= (p=reject dis=none) Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 173144639161969.47039735234262; Tue, 12 Nov 2024 13:19:51 -0800 (PST) Received: from list by lists.xenproject.org with outflank-mailman.834938.1250748 (Exim 4.92) (envelope-from ) id 1tAyIK-0002Og-P8; Tue, 12 Nov 2024 21:19:28 +0000 Received: by outflank-mailman (output) from mailman id 834938.1250748; Tue, 12 Nov 2024 21:19:28 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1tAyIK-0002OC-Jx; Tue, 12 Nov 2024 21:19:28 +0000 Received: by outflank-mailman (input) for mailman id 834938; Tue, 12 Nov 2024 21:19:27 +0000 Received: from se1-gles-sth1-in.inumbo.com ([159.253.27.254] helo=se1-gles-sth1.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1tAyIJ-0002GG-9a for xen-devel@lists.xenproject.org; Tue, 12 Nov 2024 21:19:27 +0000 Received: from mail-ed1-x52d.google.com (mail-ed1-x52d.google.com [2a00:1450:4864:20::52d]) by se1-gles-sth1.inumbo.com (Halon) with ESMTPS id c9e406e4-a13b-11ef-a0c7-8be0dac302b0; Tue, 12 Nov 2024 22:19:23 +0100 (CET) Received: by mail-ed1-x52d.google.com with SMTP id 4fb4d7f45d1cf-5cedea84d77so8066428a12.1 for ; Tue, 12 Nov 2024 13:19:23 -0800 (PST) Received: from andrewcoop.eng.citrite.net ([185.25.67.249]) by smtp.gmail.com with ESMTPSA id a640c23a62f3a-a9ee0defaefsm762049066b.164.2024.11.12.13.19.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 12 Nov 2024 13:19:21 -0800 (PST) X-Outflank-Mailman: Message body and most headers restored to incoming version X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" X-Inumbo-ID: c9e406e4-a13b-11ef-a0c7-8be0dac302b0 X-Custom-Connection: eyJyZW1vdGVpcCI6IjJhMDA6MTQ1MDo0ODY0OjIwOjo1MmQiLCJoZWxvIjoibWFpbC1lZDEteDUyZC5nb29nbGUuY29tIn0= X-Custom-Transaction: eyJpZCI6ImM5ZTQwNmU0LWExM2ItMTFlZi1hMGM3LThiZTBkYWMzMDJiMCIsInRzIjoxNzMxNDQ2MzYzLjYzNzQwOSwic2VuZGVyIjoiYW5kcmV3LmNvb3BlckBjbG91ZC5jb20iLCJyZWNpcGllbnQiOiJ4ZW4tZGV2ZWxAbGlzdHMueGVucHJvamVjdC5vcmcifQ== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=citrix.com; s=google; t=1731446362; x=1732051162; darn=lists.xenproject.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=eAYyhUk3NiNBRpvH7yor3qXtJxAbrlhbvjZAvpbioVA=; b=gNtcUJaes2Hvh6MPO+Ge8IqrX7nXE0lO/X3RNjH7gAHJp60TfTuEnwHJfHA6r/uv0/ aAHXag6UlxcGn5MbZyneXfSIBWa79tfY5uzhgROizesTFnGe/f1vpUwFB8j0s2kkYlp4 p3GVCpwdY8TZJMMnh+TFhzXhA6vzEp/F3aZ1s= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1731446362; x=1732051162; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=eAYyhUk3NiNBRpvH7yor3qXtJxAbrlhbvjZAvpbioVA=; b=OMHY1uhbI1z6U665eI4hEyFYJ30+tMGmu4UFL8wmWFxImnsb9PdsTIHavuXDtRlRsl rp4SyCX/UwOyme2G3mgYw4DLQbsew7w8KCTkCpsmRuRdycuXs+kOQsmuHCPeHOMZCaNy RGDzaXrZQYcTZFrBRA/sQnPdHF73FIfFFc65qZ/cQ9fJ/elTmlA1HL+fwl2IjMEGmFpp yv6CMdvGLqGoEytaBtndqxkg+LeEbCY0bdXQu+RKdXqm/mH6qsswU6MjOk9InSj1y8PP Z0v9Oe8Q7jIBHOjYOn1SEM8RKcxGCGMPe2+AFd0O0DRpCAbCGksETU4GyZN46AvSS5ko bORQ== X-Gm-Message-State: AOJu0YxRDRwUGK7//CKeuBUFRnT2xNSqa/9z0J6MrSbQdqY8fApOH87r lGOv7KGE9Qom76Zdr9AXF4SDdtOfZBXj0mxX78whRvg7fsVFEhm32QXIDAGKzXUI/gHuhRmfOgk Q X-Google-Smtp-Source: AGHT+IH1SJMQqG4ZDRMaVvXg/u4VtZfUdJ5zz7Ex1zwQLqVw/ITZH7FI+RC1MgIh6+bk2m9pwjGAww== X-Received: by 2002:a17:907:31c2:b0:a99:fb56:39cc with SMTP id a640c23a62f3a-aa1f810644amr37088166b.38.1731446362440; Tue, 12 Nov 2024 13:19:22 -0800 (PST) From: Andrew Cooper To: Xen-devel Cc: Andrew Cooper , Jan Beulich , =?UTF-8?q?Roger=20Pau=20Monn=C3=A9?= Subject: [PATCH v2 3/3] x86/ucode: Drop MIS_UCODE and microcode_match_result Date: Tue, 12 Nov 2024 21:19:15 +0000 Message-Id: <20241112211915.1473121-4-andrew.cooper3@citrix.com> X-Mailer: git-send-email 2.39.5 In-Reply-To: <20241112211915.1473121-1-andrew.cooper3@citrix.com> References: <20241112211915.1473121-1-andrew.cooper3@citrix.com> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @citrix.com) X-ZM-MESSAGEID: 1731446393136116600 All uses of MIS_UCODE, have been removed, leaving only a simple ordering relation, and microcode_match_result being a stale name. Drop the enum entirely, and use a simple int -1/0/1 scheme like other stand= ard ordering primitives in C. Swap the order or parameters to compare_patch(), to reduce cognitive complexity; all other logic operates the other way around. No functional change. Signed-off-by: Andrew Cooper Reviewed-by: Jan Beulich --- CC: Jan Beulich CC: Roger Pau Monn=C3=A9 I don't particular like keeping "result" as a variable name, but nothing better comes to mind. --- xen/arch/x86/cpu/microcode/amd.c | 10 ++++------ xen/arch/x86/cpu/microcode/core.c | 5 ++--- xen/arch/x86/cpu/microcode/intel.c | 9 ++++----- xen/arch/x86/cpu/microcode/private.h | 21 ++++++++++----------- 4 files changed, 20 insertions(+), 25 deletions(-) diff --git a/xen/arch/x86/cpu/microcode/amd.c b/xen/arch/x86/cpu/microcode/= amd.c index 3861fec6565a..366c8c59e93a 100644 --- a/xen/arch/x86/cpu/microcode/amd.c +++ b/xen/arch/x86/cpu/microcode/amd.c @@ -170,8 +170,7 @@ static bool check_final_patch_levels(const struct cpu_s= ignature *sig) return false; } =20 -static enum microcode_match_result compare_revisions( - uint32_t old_rev, uint32_t new_rev) +static int compare_revisions(uint32_t old_rev, uint32_t new_rev) { if ( new_rev > old_rev ) return NEW_UCODE; @@ -199,8 +198,8 @@ static bool microcode_fits_cpu(const struct microcode_p= atch *patch) return equiv.id =3D=3D patch->processor_rev_id; } =20 -static enum microcode_match_result cf_check compare_patch( - const struct microcode_patch *new, const struct microcode_patch *old) +static int cf_check compare_patch( + const struct microcode_patch *old, const struct microcode_patch *new) { /* Both patches to compare are supposed to be applicable to local CPU.= */ ASSERT(microcode_fits_cpu(new)); @@ -212,11 +211,10 @@ static enum microcode_match_result cf_check compare_p= atch( static int cf_check apply_microcode(const struct microcode_patch *patch, unsigned int flags) { - int hw_err; + int hw_err, result; unsigned int cpu =3D smp_processor_id(); struct cpu_signature *sig =3D &per_cpu(cpu_sig, cpu); uint32_t rev, old_rev =3D sig->rev; - enum microcode_match_result result; bool ucode_force =3D flags & XENPF_UCODE_FORCE; =20 if ( !microcode_fits_cpu(patch) ) diff --git a/xen/arch/x86/cpu/microcode/core.c b/xen/arch/x86/cpu/microcode= /core.c index 0cc5daa251e2..05d0d68d8158 100644 --- a/xen/arch/x86/cpu/microcode/core.c +++ b/xen/arch/x86/cpu/microcode/core.c @@ -470,8 +470,7 @@ struct ucode_buf { static long cf_check microcode_update_helper(void *data) { struct microcode_patch *patch =3D NULL; - enum microcode_match_result result; - int ret; + int ret, result; struct ucode_buf *buffer =3D data; unsigned int cpu, updated; struct patch_with_flags patch_with_flags; @@ -527,7 +526,7 @@ static long cf_check microcode_update_helper(void *data) spin_lock(µcode_mutex); if ( microcode_cache ) { - result =3D alternative_call(ucode_ops.compare_patch, patch, microc= ode_cache); + result =3D alternative_call(ucode_ops.compare_patch, microcode_cac= he, patch); =20 if ( result !=3D NEW_UCODE && !(ucode_force && (result =3D=3D OLD_UCODE || result =3D=3D SA= ME_UCODE)) ) diff --git a/xen/arch/x86/cpu/microcode/intel.c b/xen/arch/x86/cpu/microcod= e/intel.c index 3f37792ab4b5..9616a5e9db4b 100644 --- a/xen/arch/x86/cpu/microcode/intel.c +++ b/xen/arch/x86/cpu/microcode/intel.c @@ -229,8 +229,7 @@ static int microcode_sanity_check(const struct microcod= e_patch *patch) * Production microcode has a positive revision. Pre-production microcode= has * a negative revision. */ -static enum microcode_match_result compare_revisions( - int32_t old_rev, int32_t new_rev) +static int compare_revisions(int32_t old_rev, int32_t new_rev) { if ( new_rev > old_rev ) return NEW_UCODE; @@ -270,8 +269,8 @@ static bool microcode_fits_cpu(const struct microcode_p= atch *mc) return false; } =20 -static enum microcode_match_result cf_check compare_patch( - const struct microcode_patch *new, const struct microcode_patch *old) +static int cf_check compare_patch( + const struct microcode_patch *old, const struct microcode_patch *new) { /* * Both patches to compare are supposed to be applicable to local CPU. @@ -290,7 +289,7 @@ static int cf_check apply_microcode(const struct microc= ode_patch *patch, unsigned int cpu =3D smp_processor_id(); struct cpu_signature *sig =3D &this_cpu(cpu_sig); uint32_t rev, old_rev =3D sig->rev; - enum microcode_match_result result; + int result; bool ucode_force =3D flags & XENPF_UCODE_FORCE; =20 if ( !microcode_fits_cpu(patch) ) diff --git a/xen/arch/x86/cpu/microcode/private.h b/xen/arch/x86/cpu/microc= ode/private.h index c9dd8ba066f9..957d4d4293d0 100644 --- a/xen/arch/x86/cpu/microcode/private.h +++ b/xen/arch/x86/cpu/microcode/private.h @@ -5,13 +5,6 @@ =20 #include =20 -enum microcode_match_result { - OLD_UCODE, /* signature matched, but revision id is older */ - SAME_UCODE, /* signature matched, but revision id is the same */ - NEW_UCODE, /* signature matched, but revision id is newer */ - MIS_UCODE, /* signature mismatched */ -}; - /* Opaque. Internals are vendor-specific. */ struct microcode_patch; =20 @@ -54,11 +47,17 @@ struct microcode_ops { unsigned int flags); =20 /* - * Given two patches, are they both applicable to the current CPU, and= is - * new a higher revision than old? + * Given a current patch, and a proposed new patch, order them based o= n revision. + * + * This operation is not necessarily symmetrical. In some cases, a de= bug + * "new" patch will always considered to be newer, on the expectation = that + * whomever is using debug patches knows exactly what they're doing. */ - enum microcode_match_result (*compare_patch)( - const struct microcode_patch *new, const struct microcode_patch *o= ld); +#define OLD_UCODE -1 +#define SAME_UCODE 0 +#define NEW_UCODE 1 + int (*compare_patch)(const struct microcode_patch *old, + const struct microcode_patch *new); =20 /* * For Linux inird microcode compatibliity. --=20 2.39.5