Received: by 2002:a05:6a10:1287:0:0:0:0 with SMTP id d7csp7020378pxv; Fri, 30 Jul 2021 08:10:30 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwFTAdOODDoq49D6kZEFMEa5oaRzE/U1HBIkMJPcsYt2FftWiBtcerFTHHGO1p+wlhzcOI8 X-Received: by 2002:a17:907:2a45:: with SMTP id fe5mr3021666ejc.415.1627657828399; Fri, 30 Jul 2021 08:10:28 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1627657828; cv=none; d=google.com; s=arc-20160816; b=zVQ8JsnIh45SwLBjp0wulQ2sMULhSIRjAlf6huGr9bhc5ZsMvXIX96mzsMsasCM2gQ GNWmkiJ0LN/tnmaP6S0WPOKnEwHsErqWren2LhG1S/o+/zUox0R+NtP2okrWvQwW6Uo7 4Ep6QQBIJpmF4g5qJ3Yv2Y9l82+b33SOWp0hhUiezkOE3+WkDgpGpdHs94rX6R/tLgGE Ct9zQoHR2tI1RrYDT4twFOEStnz1ExwQ5BhgSgDpGWwX439WDpGvBikM4QBYa2CZqCS8 PxO2uSN9mjSN6bZ2ZURkIGKBmoEoOLCWazVxAUjhOWS4B2XZjqhf9NHtArOQ1naqKPeO /vqw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:references:in-reply-to:message-id:date:subject :cc:to:from; bh=icRl7k3z1UEN0FjUV6fKjc4xT5rFwC5YLv08yxjQA+I=; b=rK+WaAs7fu3ywzZTmc+A96mgjCOQ2aSDV+Pb6ibXgYzQtaiaL6i3opT6gtZ691MdjR /PsZGEl9DeA3N3jgDtXjcXBpYU+EuxBk5Fqw6bpxII3FrLp1uCoWBcx5UGUZzJFQ5e7B WQ+hM9sA/k/Gq+5W5hsG5g++gjfRt01g1LeU0qDL9b53ojAnNieexE7GlvIMa14P8UqH o+ZSnO10CzYo2cKPjx+7anq4GTkeDXVCHMGw1v1xALwb3PVFZtGeHi9akG+uaddFGajt oGFn9iFS7UxPobEmkYSAsORylAtK0yDIOXSQU1Jct3rPFMgdbjj3QmegI4m2Go7YsaCg ywpA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id r21si1805428ejo.665.2021.07.30.08.10.05; Fri, 30 Jul 2021 08:10:28 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239831AbhG3PHB (ORCPT + 99 others); Fri, 30 Jul 2021 11:07:01 -0400 Received: from mga05.intel.com ([192.55.52.43]:52628 "EHLO mga05.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239617AbhG3PGY (ORCPT ); Fri, 30 Jul 2021 11:06:24 -0400 X-IronPort-AV: E=McAfee;i="6200,9189,10061"; a="298699134" X-IronPort-AV: E=Sophos;i="5.84,282,1620716400"; d="scan'208";a="298699134" Received: from fmsmga003.fm.intel.com ([10.253.24.29]) by fmsmga105.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 30 Jul 2021 08:06:06 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.84,282,1620716400"; d="scan'208";a="508154954" Received: from chang-linux-3.sc.intel.com ([172.25.66.175]) by FMSMGA003.fm.intel.com with ESMTP; 30 Jul 2021 08:06:06 -0700 From: "Chang S. Bae" To: bp@suse.de, luto@kernel.org, tglx@linutronix.de, mingo@kernel.org, x86@kernel.org Cc: len.brown@intel.com, dave.hansen@intel.com, thiago.macieira@intel.com, jing2.liu@intel.com, ravi.v.shankar@intel.com, linux-kernel@vger.kernel.org, chang.seok.bae@intel.com Subject: [PATCH v9 17/26] x86/fpu/xstate: Disable XSTATE support if an inconsistent state is detected Date: Fri, 30 Jul 2021 07:59:48 -0700 Message-Id: <20210730145957.7927-18-chang.seok.bae@intel.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20210730145957.7927-1-chang.seok.bae@intel.com> References: <20210730145957.7927-1-chang.seok.bae@intel.com> Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org The kernel has a sanity check between two methods to calculate XSTATE size. In the unlikely event that they disagree, disable the use of XSTATE. Suggested-by: Thomas Gleixner Signed-off-by: Chang S. Bae Reviewed-by: Len Brown Cc: x86@kernel.org Cc: linux-kernel@vger.kernel.org --- Changes from v4: * Added as a new patch. (Thomas Gleixner) --- arch/x86/kernel/fpu/xstate.c | 40 ++++++++++++++++++++++++------------ 1 file changed, 27 insertions(+), 13 deletions(-) diff --git a/arch/x86/kernel/fpu/xstate.c b/arch/x86/kernel/fpu/xstate.c index 930e72f55e75..a36e24028ca7 100644 --- a/arch/x86/kernel/fpu/xstate.c +++ b/arch/x86/kernel/fpu/xstate.c @@ -654,11 +654,11 @@ static void __xstate_dump_leaves(void) } while (0) #define XCHECK_SZ(sz, nr, nr_macro, __struct) do { \ - if ((nr == nr_macro) && \ - WARN_ONCE(sz != sizeof(__struct), \ - "%s: struct is %zu bytes, cpu state %d bytes\n", \ - __stringify(nr_macro), sizeof(__struct), sz)) { \ + if ((nr == nr_macro) && (sz != sizeof(__struct))) { \ + pr_err("%s: struct is %zu bytes, cpu state %d bytes\n", \ + __stringify(nr_macro), sizeof(__struct), sz); \ __xstate_dump_leaves(); \ + return -EINVAL; \ } \ } while (0) @@ -667,7 +667,7 @@ static void __xstate_dump_leaves(void) * that our software representation matches what the CPU * tells us about the state's size. */ -static void check_xstate_against_struct(int nr) +static int check_xstate_against_struct(int nr) { /* * Ask the CPU for the size of the state. @@ -695,9 +695,12 @@ static void check_xstate_against_struct(int nr) (nr >= XFEATURE_MAX) || (nr == XFEATURE_PT_UNIMPLEMENTED_SO_FAR) || ((nr >= XFEATURE_RSRVD_COMP_11) && (nr <= XFEATURE_LBR))) { - WARN_ONCE(1, "no structure for xstate: %d\n", nr); + pr_err("no structure for xstate: %d\n", nr); XSTATE_WARN_ON(1); + return -EINVAL; } + + return 0; } /** @@ -707,13 +710,14 @@ static void check_xstate_against_struct(int nr) * excluded. Only the size of the buffer for task->fpu is checked here. * * @include_dynamic_states: A knob to include dynamic states or not. + * @size: A pointer to record the size. * - * Return: The calculated xstate size. + * Return: 0 if successful; otherwise, error code. */ -static unsigned int calculate_xstate_size(bool include_dynamic_states) +static int calculate_xstate_size(bool include_dynamic_states, unsigned int *size) { unsigned int xstate_size = FXSAVE_SIZE + XSAVE_HDR_SIZE; - int i; + int i, err; for (i = FIRST_EXTENDED_XFEATURE; i < XFEATURE_MAX; i++) { if (!xfeature_enabled(i)) @@ -722,7 +726,10 @@ static unsigned int calculate_xstate_size(bool include_dynamic_states) if (!include_dynamic_states && (xfeatures_mask_user_dynamic & BIT_ULL(i))) continue; - check_xstate_against_struct(i); + err = check_xstate_against_struct(i); + if (err) + return err; + /* * Supervisor state components can be managed only by * XSAVES. @@ -748,7 +755,9 @@ static unsigned int calculate_xstate_size(bool include_dynamic_states) xstate_size += xfeature_size(i); } - return xstate_size; + if (size) + *size = xstate_size; + return 0; } @@ -835,6 +844,7 @@ static int __init init_xstate_size(void) /* Recompute the context size for enabled features: */ unsigned int possible_xstate_size, xstate_size; unsigned int xsave_size; + int err; xsave_size = get_xsave_size(); @@ -848,7 +858,9 @@ static int __init init_xstate_size(void) * 'true' to include dynamic states. Cross-check with the CPUID- * provided size and record it. */ - xstate_size = calculate_xstate_size(true); + err = calculate_xstate_size(true, &xstate_size); + if (err) + return err; XSTATE_WARN_ON(possible_xstate_size != xstate_size); set_xstate_config(XSTATE_MAX_SIZE, possible_xstate_size); @@ -857,7 +869,9 @@ static int __init init_xstate_size(void) * 'false' to exclude dynamic states. Ensure the size fits in * the statically-allocated buffer and record it. */ - xstate_size = calculate_xstate_size(false); + err = calculate_xstate_size(false, &xstate_size); + if (err) + return err; if (!is_supported_xstate_size(xstate_size)) return -EINVAL; set_xstate_config(XSTATE_MIN_SIZE, xstate_size); -- 2.17.1