Received: by 2002:a05:7412:b10a:b0:f3:1519:9f41 with SMTP id az10csp2851042rdb; Mon, 4 Dec 2023 09:07:31 -0800 (PST) X-Google-Smtp-Source: AGHT+IGpQ3jtZxwi1ktaQ1Te5mirmbAvx/wNlz3DVIpcKPsWQTN2x6jNidRj0hjEdl8QDEOggbJH X-Received: by 2002:a05:6a20:c1a3:b0:18b:aceb:7a79 with SMTP id bg35-20020a056a20c1a300b0018baceb7a79mr33135985pzb.12.1701709651526; Mon, 04 Dec 2023 09:07:31 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1701709651; cv=none; d=google.com; s=arc-20160816; b=WJnsdX+w6JlWWFwjJdLWev2G2YwuRGpPDKdHPVGwe8GNg8at/nIMwZ2GZNLkxTtKA3 y73cghxEhs5d+6ppczkDL/XLifbBu9Jm1teZRJVE8y9Ef3smUU7kPB3HpQzFqvr5ggQk 6kFLdr1Xniot2HsAw+XIuafxY/2C2Fl2bYL3pxaeEr7j4n6oT9LOIAtOZhLf0CJp6IVY gNqIi6Ryv7JAvPQg/9OUj3EptdFZZ21c2wdtXGOpmhGatoLEqB7mdPDqN5tQXCsxDzKi spxWsbxoXEif7gnxghZDBbX2A9fcwjWxw70iHTZZTwcFRYHqibWzCCio2VIHh9LtKo9i lvPA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:in-reply-to:autocrypt :from:references:cc:to:content-language:subject:user-agent :mime-version:date:message-id:dkim-signature; bh=+OGgCdh9b++s9FLgOge+OaqXhjePeG09q/OYM5PvlZw=; fh=cZNxSSVB+wg++tJ4gHtNQ+YjTGt9XbRM4vl8PXKXCjk=; b=OrwZrwPGvAD4mp/jFy0DDXlO5NlNzSVYzRIHD+ZZytlbEugxtPayTFYrikg7gI6YGJ R4h1F7algcqkAZn5ZXCqySwYgidv13bQr73UmO3MP/qq+nX3VjbrG+2bEL6nbkGtbkPk ujAdEpH0jM1ILbpGKufOWxs1YTYmZsOqiE0L7y0/aZTFVar4HF7Np1cRrUu7t6hUlBq9 wHPYY54aznFoF1WPu/W48FAzujamZvv4yl/tqrQSy90jdyerEmpgtUYqp+Bn/lCbzlxs RT6uhWZbH3vZafxkbTdKLbWjcKrkkmdNe+qVNJQFUH0tINEgTV3AnVItMuX6G9n5DgDA 9zRA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=cqjQZDeT; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from snail.vger.email (snail.vger.email. [23.128.96.37]) by mx.google.com with ESMTPS id i13-20020a63584d000000b005c62491dcf3si8067934pgm.841.2023.12.04.09.07.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 04 Dec 2023 09:07:31 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) client-ip=23.128.96.37; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=cqjQZDeT; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 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 snail.vger.email (Postfix) with ESMTP id 5EA2C804DD9D; Mon, 4 Dec 2023 09:07:29 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at snail.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230366AbjLDRHR (ORCPT + 99 others); Mon, 4 Dec 2023 12:07:17 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53960 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229561AbjLDRHQ (ORCPT ); Mon, 4 Dec 2023 12:07:16 -0500 Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.9]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7C62ABB; Mon, 4 Dec 2023 09:07:22 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1701709642; x=1733245642; h=message-id:date:mime-version:subject:to:cc:references: from:in-reply-to:content-transfer-encoding; bh=qOapASXW9sV5EN1o2BAcOJimFv0X/vGsorTUtaMTOWw=; b=cqjQZDeTeQMaVdp9c0M7CgAWVQ51Hahx0Bd9zJtgbSUVOVBJ48LaRrRW 5Aw7fe0d1R7bR6oHQDsPGhh+B6XsAER4RQwwxLMOV4lf5uaCyQa2rypKb 49D8C1l264/WmACUubnYV7f8bE0v0+LUymUX/6m/a3KMah/NdsAULWIb7 D2decWzanCkH7l1hIe6T/GME3FQRhCLDVdxa5Md+9/zNpr9pAjLG4N1tL vkOaGTMQbahsVRsK2XiBRNT3Co8ExU8rocYYSP93HuGhVvmeSLBCXlyqz ZcdCLQyE5Txfc6j6w9cl0tjM3MP6DYRI86WexmV/eSu47UJrFefTsSGwE w==; X-IronPort-AV: E=McAfee;i="6600,9927,10914"; a="12480099" X-IronPort-AV: E=Sophos;i="6.04,250,1695711600"; d="scan'208";a="12480099" Received: from fmsmga007.fm.intel.com ([10.253.24.52]) by orvoesa101.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 04 Dec 2023 09:07:22 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10914"; a="774316847" X-IronPort-AV: E=Sophos;i="6.04,250,1695711600"; d="scan'208";a="774316847" Received: from gauravs1-mobl.amr.corp.intel.com (HELO [10.209.53.199]) ([10.209.53.199]) by fmsmga007-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 04 Dec 2023 09:07:18 -0800 Message-ID: Date: Mon, 4 Dec 2023 09:07:17 -0800 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH v15 22/23] x86/mce: Improve error log of kernel space TDX #MC due to erratum Content-Language: en-US To: "Huang, Kai" , "kvm@vger.kernel.org" , "linux-kernel@vger.kernel.org" Cc: "sathyanarayanan.kuppuswamy@linux.intel.com" , "Luck, Tony" , "david@redhat.com" , "bagasdotme@gmail.com" , "ak@linux.intel.com" , "kirill.shutemov@linux.intel.com" , "seanjc@google.com" , "mingo@redhat.com" , "pbonzini@redhat.com" , "tglx@linutronix.de" , "Yamahata, Isaku" , "nik.borisov@suse.com" , "hpa@zytor.com" , "peterz@infradead.org" , "sagis@google.com" , "imammedo@redhat.com" , "bp@alien8.de" , "Gao, Chao" , "Brown, Len" , "rafael@kernel.org" , "Huang, Ying" , "Williams, Dan J" , "x86@kernel.org" References: <9e80873fac878aa5d697cbcd4d456d01e1009d1f.1699527082.git.kai.huang@intel.com> From: Dave Hansen Autocrypt: addr=dave.hansen@intel.com; keydata= xsFNBE6HMP0BEADIMA3XYkQfF3dwHlj58Yjsc4E5y5G67cfbt8dvaUq2fx1lR0K9h1bOI6fC oAiUXvGAOxPDsB/P6UEOISPpLl5IuYsSwAeZGkdQ5g6m1xq7AlDJQZddhr/1DC/nMVa/2BoY 2UnKuZuSBu7lgOE193+7Uks3416N2hTkyKUSNkduyoZ9F5twiBhxPJwPtn/wnch6n5RsoXsb ygOEDxLEsSk/7eyFycjE+btUtAWZtx+HseyaGfqkZK0Z9bT1lsaHecmB203xShwCPT49Blxz VOab8668QpaEOdLGhtvrVYVK7x4skyT3nGWcgDCl5/Vp3TWA4K+IofwvXzX2ON/Mj7aQwf5W iC+3nWC7q0uxKwwsddJ0Nu+dpA/UORQWa1NiAftEoSpk5+nUUi0WE+5DRm0H+TXKBWMGNCFn c6+EKg5zQaa8KqymHcOrSXNPmzJuXvDQ8uj2J8XuzCZfK4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71+0z2jMzbclKAyRHKU9JdN6Hkkgr2nPb561yjcB8 sIq1pFXKyO+nKy6SZYxOvHxCcjk2fkw6UmPU6/j/nQlj2lfOAgNVKuDLothIxzi8pndB8Jju KktE5HJqUUMXePkAYIxEQ0mMc8Po7tuXdejgPMwgP7x65xtfEqI0RuzbUioFltsp1jUaRwQZ MTsCeQDdjpgHsj+P2ZDeEKCbma4m6Ez/YWs4+zDm1X8uZDkZcfQlD9NldbKDJEXLIjYWo1PH hYepSffIWPyvBMBTW2W5FRjJ4vLRrJSUoEfJuPQ3vW9Y73foyo/qFoURHO48AinGPZ7PC7TF vUaNOTjKedrqHkaOcqB185ahG2had0xnFsDPlx5y In-Reply-To: Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,SPF_HELO_NONE, SPF_NONE,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net 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 (snail.vger.email [0.0.0.0]); Mon, 04 Dec 2023 09:07:29 -0800 (PST) On 12/3/23 03:44, Huang, Kai wrote: ... >> It doesn't need perfect accuracy. But how do we know it's not going to >> go, for instance, chase a bad pointer? >> >>> + if (tdx_module_status != TDX_MODULE_INITIALIZED) >>> + return false; >> >> As an example, what prevents this CPU from observing >> tdx_module_status==TDX_MODULE_INITIALIZED while the PAMT structure is >> being assembled? > > There are two types of memory order serializing operations between assembling > the TDMR/PAMT structure and setting the tdx_module_status to > TDX_MODULE_INITIALIZED: 1) wbvind_on_all_cpus(); 2) bunch of SEAMCALLs; > > WBINVD is a serializing instruction. SEAMCALL is a VMEXIT to the TDX module, > which involves GDT/LDT/control registers/MSRs switch so it is also a serializing > operation. > > But perhaps we can explicitly add a smp_wmb() between assembling TDMR/PAMT > structure and setting tdx_module_status if that's better. ... and there's zero documentation of this dependency because ... ? I suspect it's because it was never looked at until Tony made a comment about it and we started looking at it. In other words, it worked by coincidence. >>> + for (i = 0; i < tdmr_list->nr_consumed_tdmrs; i++) { >>> + unsigned long base, size; >>> + >>> + tdmr_get_pamt(tdmr_entry(tdmr_list, i), &base, &size); >>> + >>> + if (phys >= base && phys < (base + size)) >>> + return true; >>> + } >>> + >>> + return false; >>> +} >>> + >>> +/* >>> + * Return whether the memory page at the given physical address is TDX >>> + * private memory or not. Called from #MC handler do_machine_check(). >>> + * >>> + * Note this function may not return an accurate result in rare cases. >>> + * This is fine as the #MC handler doesn't need a 100% accurate result, >>> + * because it cannot distinguish #MC between software bug and real >>> + * hardware error anyway. >>> + */ >>> +bool tdx_is_private_mem(unsigned long phys) >>> +{ >>> + struct tdx_module_args args = { >>> + .rcx = phys & PAGE_MASK, >>> + }; >>> + u64 sret; >>> + >>> + if (!platform_tdx_enabled()) >>> + return false; >>> + >>> + /* Get page type from the TDX module */ >>> + sret = __seamcall_ret(TDH_PHYMEM_PAGE_RDMD, &args); >>> + /* >>> + * Handle the case that CPU isn't in VMX operation. >>> + * >>> + * KVM guarantees no VM is running (thus no TDX guest) >>> + * when there's any online CPU isn't in VMX operation. >>> + * This means there will be no TDX guest private memory >>> + * and Secure-EPT pages. However the TDX module may have >>> + * been initialized and the memory page could be PAMT. >>> + */ >>> + if (sret == TDX_SEAMCALL_UD) >>> + return is_pamt_page(phys); >> >> Either this is comment is wonky or the module initialization is buggy. >> >> config_global_keyid() goes and does SEAMCALLs on all CPUs. There are >> zero checks or special handling in there for whether the CPU has done >> VMXON. So, by the time we've started initializing the TDX module >> (including the PAMT), all online CPUs must be able to do SEAMCALLs. Right? >> >> So how can we have a working PAMT here when this CPU can't do SEAMCALLs? > > The corner case is KVM can enable VMX on all cpus, initialize the TDX module, > and then disable VMX on all cpus. One example is KVM can be unloaded after it > initializes the TDX module. > > In this case CPU cannot do SEAMCALL but PAMTs are already working :-) > > However if SEAMCALL cannot be made (due to out of VMX), then the module can only > be initialized or the initialization hasn't been tried, so both > tdx_module_status and the tdx_tdmr_list are stable to access. None of this even matters. Let's remind ourselves how unbelievably unlikely this is: 1. You're on an affected system that has the erratum 2. The KVM module gets unloaded, runs vmxoff 3. A kernel bug using a very rare partial write corrupts the PAMT 4. A second bug reads the PAMT consuming poison, #MC is generated 5. Enter #MC handler, SEAMCALL fails 6. #MC handler just reports a plain hardware error The only thing even remotely wrong with this situation is that the report won't pin the #MC on TDX. Play stupid games (removing modules), win stupid prizes (worse error message). Can we dynamically mark a module as unsafe to remove? If so, I'd happily just say that we should make kvm_intel.ko unsafe to remove when TDX is supported and move on with life. tl;dr: I think even looking a #MC on the PAMT after the kvm module is removed is a fool's errand.