From patchwork Thu Nov 9 11:55:41 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kai Huang X-Patchwork-Id: 163366 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b129:0:b0:403:3b70:6f57 with SMTP id q9csp379584vqs; Thu, 9 Nov 2023 03:57:38 -0800 (PST) X-Google-Smtp-Source: AGHT+IHgNorknGnpBzy0wy3JRp68VuyiZa0Jb7nkHYP6lVDlVzQWKhirTo5FbKVXW9Rm4NBUQ3Jv X-Received: by 2002:a17:902:ec89:b0:1cc:436f:70c2 with SMTP id x9-20020a170902ec8900b001cc436f70c2mr7918110plg.9.1699531058441; Thu, 09 Nov 2023 03:57:38 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1699531058; cv=none; d=google.com; s=arc-20160816; b=0WcLfMGb/HkYYBmfgKZwUIo/EJH6eYDA5dqL9jLOjCblG56G4tlzZoxXQvcqFMMSRY eTmKQMIVdIliLJvyQl9w3jssGdIgrkFH9OyxLu09OkTJhxjDsNaRtfHUN598K/Uzi8Da 7c2LS6HrQYP0gH3uIwWFDSjUrLKMoTmgWP0YfKW4UwGEFLKaMKENWDsehPEfVypkxn+F GWS3zg7S98r2ee4GRhdPXUTgnXAWSEkNn5bQOGsWgwYV6gXZo9FJnEKNOKIT1yo2BiFl /st9/6HRRnLgLtK5r6bUcjJaes5IAWSOrYlzPoIHvqoLsz6b03yxIr2u/Mf7HzU0taSN U5DQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=MEm5FRSqf4zRqvPtYWY1Vy7Q17+YPA9MgFczauEBFOM=; fh=WBgbLtMencYhgeHuu2sUs5b9THiYLgy17d2w1N+xuf4=; b=JVO5xUCwDCdivuzpQs57ZlTzXnzBd7MO2Kt/77cA8OAl1xhbN4Bf5jCpbqlByNZ2ju gQ3jFOjXAoSK9ILXlv/MxWbNSD4YrQCuKyYTfTbsGw0xhHjxvrVUbDRTWu5Lt9N8+1jN H62VXHM5PT44udPkJVZkSGeKA3oiSUqfYkCUujRpZs4GB55K3qoLbHvTJAULSaj8jK2Z B2aAUNl7ax8q3DDoodaMaxtzOhoa4UFK6u0XiKo9arj64tEsLyG1OmitZiWbPs9avJP7 tut1qkIdlD2JiEjuRO8qh0/y93UBEi+1u4w9SLSAiH0eKMeJoWvGGFfB8JfcGIhIVC1f mBVg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=DIOmW27A; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:5 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from groat.vger.email (groat.vger.email. [2620:137:e000::3:5]) by mx.google.com with ESMTPS id h10-20020a170902f54a00b001c9b5d2c3fasi5129006plf.523.2023.11.09.03.57.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 09 Nov 2023 03:57:38 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:5 as permitted sender) client-ip=2620:137:e000::3:5; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=DIOmW27A; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:5 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by groat.vger.email (Postfix) with ESMTP id 4F677825F12C; Thu, 9 Nov 2023 03:57:26 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at groat.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234219AbjKIL4v (ORCPT + 32 others); Thu, 9 Nov 2023 06:56:51 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36392 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234334AbjKIL4r (ORCPT ); Thu, 9 Nov 2023 06:56:47 -0500 Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.8]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3338230D2; Thu, 9 Nov 2023 03:56:41 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1699531001; x=1731067001; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=z0kCA+cJH8C11vmUwbqx0s+Ehwp/QU3vMncqW23is30=; b=DIOmW27AleuxGpA7qH05QWmCLkbQn/8b5I/6ujph9nDT2JCjXu+gTf5S NaoAnnpmqEIWl+oTsRF+TckI0OxremfDDZY9TJ6ldGhqIZkgdnkNKy3xc 8nG+iGZ+ZUpIFfJyyynfEOpOWUetQ8JWpg5cd2nyf7EEmZZYg0zHUgN3x XuFHOQM3YR40pipl8b/40gTnXQWbDKdlUq8RG6AczSdknAKBlZCDbHqPQ LCgbiigGMEKhNPxR+wpIweTH+pGazeE/GMOgcS/agrYpVw+DRL8vBrAW3 I2ziijiqg3DBRQVDYvPHEqkJ5aTXmC/h7dKQdzbris1RWqrlpx5NzrlXf A==; X-IronPort-AV: E=McAfee;i="6600,9927,10888"; a="2936340" X-IronPort-AV: E=Sophos;i="6.03,289,1694761200"; d="scan'208";a="2936340" Received: from fmsmga007.fm.intel.com ([10.253.24.52]) by fmvoesa102.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 09 Nov 2023 03:56:41 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10888"; a="766976630" X-IronPort-AV: E=Sophos;i="6.03,289,1694761200"; d="scan'208";a="766976630" Received: from shadphix-mobl.amr.corp.intel.com (HELO khuang2-desk.gar.corp.intel.com) ([10.209.83.35]) by fmsmga007-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 09 Nov 2023 03:56:34 -0800 From: Kai Huang To: linux-kernel@vger.kernel.org, kvm@vger.kernel.org Cc: x86@kernel.org, dave.hansen@intel.com, kirill.shutemov@linux.intel.com, peterz@infradead.org, tony.luck@intel.com, tglx@linutronix.de, bp@alien8.de, mingo@redhat.com, hpa@zytor.com, seanjc@google.com, pbonzini@redhat.com, rafael@kernel.org, david@redhat.com, dan.j.williams@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 v15 04/23] x86/cpu: Detect TDX partial write machine check erratum Date: Fri, 10 Nov 2023 00:55:41 +1300 Message-ID: X-Mailer: git-send-email 2.41.0 In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (groat.vger.email [0.0.0.0]); Thu, 09 Nov 2023 03:57:26 -0800 (PST) X-Spam-Status: No, score=-0.9 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on groat.vger.email X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1782087479075246979 X-GMAIL-MSGID: 1782087479075246979 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. To prepare for those changes, add a CPU bug bit to indicate this erratum. 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 Reviewed-by: Dave Hansen --- 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 4af140cf5719..d097e558e079 100644 --- a/arch/x86/include/asm/cpufeatures.h +++ b/arch/x86/include/asm/cpufeatures.h @@ -495,6 +495,7 @@ #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_GDS X86_BUG(30) /* CPU is affected by Gather Data Sampling */ +#define X86_BUG_TDX_PW_MCE X86_BUG(31) /* CPU may incur #MC if non-TD software does partial write to TDX private memory */ /* BUG word 2 */ #define X86_BUG_SRSO X86_BUG(1*32 + 0) /* AMD SRSO bug */ diff --git a/arch/x86/kernel/cpu/intel.c b/arch/x86/kernel/cpu/intel.c index a927a8fc9624..1304d29c0660 100644 --- a/arch/x86/kernel/cpu/intel.c +++ b/arch/x86/kernel/cpu/intel.c @@ -184,6 +184,21 @@ static bool bad_spectre_microcode(struct cpuinfo_x86 *c) return false; } +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; @@ -322,6 +337,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)