From patchwork Mon Jun 26 14:12:34 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Huang, Kai" X-Patchwork-Id: 13292973 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id A84F1EB64DD for ; Mon, 26 Jun 2023 14:14:03 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 4C8668D0007; Mon, 26 Jun 2023 10:14:03 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 4794E8D0001; Mon, 26 Jun 2023 10:14:03 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 31BB68D0007; Mon, 26 Jun 2023 10:14:03 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id 25EBB8D0001 for ; Mon, 26 Jun 2023 10:14:03 -0400 (EDT) Received: from smtpin17.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id BBA6640750 for ; Mon, 26 Jun 2023 14:14:02 +0000 (UTC) X-FDA: 80945093124.17.C815257 Received: from mga18.intel.com (mga18.intel.com [134.134.136.126]) by imf09.hostedemail.com (Postfix) with ESMTP id 01A6A140018 for ; Mon, 26 Jun 2023 14:13:59 +0000 (UTC) Authentication-Results: imf09.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=ND2unvI+; spf=pass (imf09.hostedemail.com: domain of kai.huang@intel.com designates 134.134.136.126 as permitted sender) smtp.mailfrom=kai.huang@intel.com; dmarc=pass (policy=none) header.from=intel.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1687788840; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=uMYH98VLvVbrlnbeMUHyzXSdXftnu7n6AD9u6XzmM4o=; b=LCfbFFe+f0zNiClcCp+SmX3b0zKGmrnVA9g5CLXuJsn6VgmvblfTMg0adlVqVZxiZ6WwFe K8IrReeeB5TgCKd0urT2/rA4MdbCET3FnnQUtsX58l3PVdYUkOEA8XuSqUzDx1qnfcn96X RJ2WjxliF2leNG11WWjPnm13HM0Z3QM= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1687788840; a=rsa-sha256; cv=none; b=QM7Pf6Gjbx1EkFIR9r9i6/H99fuJHpr0l6puHhuzYnccWuDafmxPU1VoBLTd+DitqDOXNj f0JsNzMdOpN/GUyZOqvHU+sOQyg2w7et/Z89qxCHQGJLZGUCwt9fRGMRJh7xRiSxfA7dSr GAr3cJVe36eYRN2+/wSzjRmsT+5cG2Q= ARC-Authentication-Results: i=1; imf09.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=ND2unvI+; spf=pass (imf09.hostedemail.com: domain of kai.huang@intel.com designates 134.134.136.126 as permitted sender) smtp.mailfrom=kai.huang@intel.com; dmarc=pass (policy=none) header.from=intel.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1687788840; x=1719324840; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=AT1tL9/WjIAyxJSzeIJnm4LmfMyuPmLuqOcY2UkVzlU=; b=ND2unvI+xOygO7VWdNAgTSS6tzfKcv/HWHI7MO/wKQqRyn8SW+ma8XB8 ix93gpnlBj7O0PAhVcttvkAxpomAP+PYnL9F9bTHmBpq4PyhvcTl2JQtJ XGD/vxdtcnALbE8FivOHd2ZVPFiLyOOOlkmkGwNWqeqKvGZ2v7iIUPGa5 +2VznLxY+IgSnnt0/VIb3dxZRECnkchwuHKmfLPbwZG27alXCUIScftay m8bRwop2SUCzmB7fjSUBlaxS4+5125L2ka31dqriaoZW6WLbjlk2kMwDA dQ2RauBVn75BsyoRdRqW7BpFuNwqhNBav7Osjp+nx8dnTvPzoIvyFmJRs A==; X-IronPort-AV: E=McAfee;i="6600,9927,10753"; a="346033609" X-IronPort-AV: E=Sophos;i="6.01,159,1684825200"; d="scan'208";a="346033609" Received: from orsmga005.jf.intel.com ([10.7.209.41]) by orsmga106.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 26 Jun 2023 07:13:59 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10753"; a="890292271" X-IronPort-AV: E=Sophos;i="6.01,159,1684825200"; d="scan'208";a="890292271" Received: from smithau-mobl1.amr.corp.intel.com (HELO khuang2-desk.gar.corp.intel.com) ([10.213.179.223]) by orsmga005-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 26 Jun 2023 07:13:52 -0700 From: Kai Huang To: linux-kernel@vger.kernel.org, kvm@vger.kernel.org Cc: linux-mm@kvack.org, x86@kernel.org, dave.hansen@intel.com, kirill.shutemov@linux.intel.com, tony.luck@intel.com, peterz@infradead.org, tglx@linutronix.de, bp@alien8.de, mingo@redhat.com, hpa@zytor.com, seanjc@google.com, pbonzini@redhat.com, david@redhat.com, dan.j.williams@intel.com, rafael.j.wysocki@intel.com, ashok.raj@intel.com, reinette.chatre@intel.com, len.brown@intel.com, ak@linux.intel.com, isaku.yamahata@intel.com, ying.huang@intel.com, chao.gao@intel.com, sathyanarayanan.kuppuswamy@linux.intel.com, nik.borisov@suse.com, bagasdotme@gmail.com, sagis@google.com, imammedo@redhat.com, kai.huang@intel.com Subject: [PATCH v12 04/22] x86/cpu: Detect TDX partial write machine check erratum Date: Tue, 27 Jun 2023 02:12:34 +1200 Message-Id: <0f701502157029989617bcb3f5940ff48e19a2b2.1687784645.git.kai.huang@intel.com> X-Mailer: git-send-email 2.40.1 In-Reply-To: References: MIME-Version: 1.0 X-Rspamd-Queue-Id: 01A6A140018 X-Rspam-User: X-Stat-Signature: gbusrky7se1fen7t4ryg34qiq9hfciym X-Rspamd-Server: rspam03 X-HE-Tag: 1687788839-460416 X-HE-Meta: U2FsdGVkX18U0zbxOG7buv25iJfwGeyf+QXXLu9DrbPGF59LEZ0vpBR3Qpk3WmwJ8VDLq5bRBZNe/1rOdKbgoE5K1eUkvXw9mtfUVNCDKzu+Psu5a4NqEP2Dpy1XXqXz0RZ6kBbgF0T0e1//9kOqNafl8nYSH3XZiy4+KKnUowmOjo2QqTJAB53PrI9HjEGNiDLFITGY7pfspSMdbP3/k21eyC7jRtSzB81cOUhuJV+WUB3cxJ8qRTJKrY5wLj4hPY0TpZVc843pTeZ8FonhjDPeFlb/AzMINQe6Dsvoj9d9mfUsTRSh1z2qr9lZgewNRfWZLKFOQ/nhQV2zQB+tS+quXQ/rfGbKzLGxxgV/HOSJjXTzZXTZxV7O6OeDIyhiUW1yd/TlO8HZoK9PD6XbqEcp9C0E+FE/myrv8JapKtunFS1y/Cmukm/4YMwdPAwz3/HpXiLSzqKncFwleaCbuvMWsmxHMqKdDQnSJmtb+GThHlLj5kWrF87dUMD6QXLRbwhDmPKjxo5jT6zScmTKJueHJA45WJutYcrEYxB6jCeGO1zN4RJDa1BnrYcMwcSdZYzpzfocXqfNa48tiKt1InHiZhevJsQmfI1jL50Na8btPiTrYL2ClNJoMonDWxzu2nupb+cGMrsiBX7JOn70fBF4iLL2/3xehizXjYxkBDhjeXwmz6gTkFPK70ic3Tb0tsiAuqj/omAqBR1HZqod/68RXJx4xWu+bZMLs9fjFJJWLCok9K3/R8YH8CNCU9lVqOIm5nSxpFHva9iF40HiI+LCQ3pjd0RL40oS/FzSl6Tz+kF2H6/4R3kSpQdFNgy3pUSQ5SQUUdLUILfOCYGg0IN7cKjNYNp7ckFPhruJobn6qtTptQZcTkXFtYUTAo7OHSKO2pjRuoF7Z6nmmRtLTzBPrVXEwWhaOx+LSUUykUfj4Qku6OzDjNUH+i/tm3YGoU7thp34d/xTCbBl5gw w0hEAToU fnkQSiLPpYl8Pe9PP/lFBFTW4mRgUSk93QoILqWJEMhTsJfOUiY17e7pCx8qgpLnYAMjwc3BmcpUNI+vZMRrcCLCYvyIA/if57YzNZZtbj0aL39pC5REZn1QEwfydHqkTO44xyvkQgoZJGxt5MvQCaevf4mPTuziSWg/b X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: TDX memory has integrity and confidentiality protections. Violations of this integrity protection are supposed to only affect TDX operations and are never supposed to affect the host kernel itself. In other words, the host kernel should never, itself, see machine checks induced by the TDX integrity hardware. Alas, the first few generations of TDX hardware have an erratum. A partial write to a TDX private memory cacheline will silently "poison" the line. Subsequent reads will consume the poison and generate a machine check. According to the TDX hardware spec, neither of these things should have happened. Virtually all kernel memory accesses operations happen in full cachelines. In practice, writing a "byte" of memory usually reads a 64 byte cacheline of memory, modifies it, then writes the whole line back. Those operations do not trigger this problem. This problem is triggered by "partial" writes where a write transaction of less than cacheline lands at the memory controller. The CPU does these via non-temporal write instructions (like MOVNTI), or through UC/WC memory mappings. The issue can also be triggered away from the CPU by devices doing partial writes via DMA. With this erratum, there are additional things need to be done. Similar to other CPU bugs, use a CPU bug bit to indicate this erratum, and detect this erratum during early boot. Note this bug reflects the hardware thus it is detected regardless of whether the kernel is built with TDX support or not. Signed-off-by: Kai Huang Reviewed-by: Kirill A. Shutemov Reviewed-by: David Hildenbrand --- v11 -> v12: - Added Kirill's tag - Changed to detect the erratum in early_init_intel() (Kirill) v10 -> v11: - New patch --- arch/x86/include/asm/cpufeatures.h | 1 + arch/x86/kernel/cpu/intel.c | 17 +++++++++++++++++ 2 files changed, 18 insertions(+) diff --git a/arch/x86/include/asm/cpufeatures.h b/arch/x86/include/asm/cpufeatures.h index cb8ca46213be..dc8701f8d88b 100644 --- a/arch/x86/include/asm/cpufeatures.h +++ b/arch/x86/include/asm/cpufeatures.h @@ -483,5 +483,6 @@ #define X86_BUG_RETBLEED X86_BUG(27) /* CPU is affected by RETBleed */ #define X86_BUG_EIBRS_PBRSB X86_BUG(28) /* EIBRS is vulnerable to Post Barrier RSB Predictions */ #define X86_BUG_SMT_RSB X86_BUG(29) /* CPU is vulnerable to Cross-Thread Return Address Predictions */ +#define X86_BUG_TDX_PW_MCE X86_BUG(30) /* CPU may incur #MC if non-TD software does partial write to TDX private memory */ #endif /* _ASM_X86_CPUFEATURES_H */ diff --git a/arch/x86/kernel/cpu/intel.c b/arch/x86/kernel/cpu/intel.c index 1c4639588ff9..e6c3107adc15 100644 --- a/arch/x86/kernel/cpu/intel.c +++ b/arch/x86/kernel/cpu/intel.c @@ -358,6 +358,21 @@ int intel_microcode_sanity_check(void *mc, bool print_err, int hdr_type) } EXPORT_SYMBOL_GPL(intel_microcode_sanity_check); +static void check_tdx_erratum(struct cpuinfo_x86 *c) +{ + /* + * These CPUs have an erratum. A partial write from non-TD + * software (e.g. via MOVNTI variants or UC/WC mapping) to TDX + * private memory poisons that memory, and a subsequent read of + * that memory triggers #MC. + */ + switch (c->x86_model) { + case INTEL_FAM6_SAPPHIRERAPIDS_X: + case INTEL_FAM6_EMERALDRAPIDS_X: + setup_force_cpu_bug(X86_BUG_TDX_PW_MCE); + } +} + static void early_init_intel(struct cpuinfo_x86 *c) { u64 misc_enable; @@ -509,6 +524,8 @@ static void early_init_intel(struct cpuinfo_x86 *c) */ if (detect_extended_topology_early(c) < 0) detect_ht_early(c); + + check_tdx_erratum(c); } static void bsp_init_intel(struct cpuinfo_x86 *c)