Message ID | 8d8285cc5efa6302cf42a3fe2c9153d1a9dbcdac.1668988357.git.kai.huang@intel.com |
---|---|
State | New |
Headers |
Return-Path: <linux-kernel-owner@vger.kernel.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:f944:0:0:0:0:0 with SMTP id q4csp1325612wrr; Sun, 20 Nov 2022 16:40:37 -0800 (PST) X-Google-Smtp-Source: AA0mqf6sTXLBH/fu9RcRpzJFY+I0Co82x36M7wXX6EG7IIS2ZxBGoXM5tGleAaTsMKSF7MGISEiZ X-Received: by 2002:a05:6a00:24c1:b0:56e:a001:8cb0 with SMTP id d1-20020a056a0024c100b0056ea0018cb0mr17780052pfv.60.1668991236612; Sun, 20 Nov 2022 16:40:36 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1668991236; cv=none; d=google.com; s=arc-20160816; b=1JFJ2VFkcEW+e4XX7DyOySlwWrDd7R1/4FFDaEcSwQ/4GqOMfXCiIApfo9AVtjsjXy sQna5mktKOsS9rWGUN2FNmHdNQcd0kx/Sq92mHPeGcLtfz2psZK1csQnSB6+MiCPqCOZ 1h4RZmFz9NsyCPBSCLJ8dj7vr/Kj8T3FMpr6S1f0UpA8JfiHZEgCNrKqYBatQfUG0Lev Ht34e3W6q03B8XXdvPfiL6D9OhIWG7saaF4DUEosh5di3FhsIAo0WzJQP5pc5/xFaP27 hMLKLvCCj5JK/TF3IHSgpDOJVo6ZZhxW7K/e9Y81djuSKDudq6lFlI3yrvdsZ1BEA77j NXYw== 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=1ZDrmZlNcxV6f+e5/5Q37GQIdNjaxpt17aZEM/WACYk=; b=EJ/Rs4JJ+TorNImNThIMmSUDYMxk9a0qcAW0mL5wMt3FSHCkBUU3WLCuvSrdFqgEbR ZUKrAAF2ox8mfpOWx1y9w1n0KF9a0mc26FsYpgco2RXleTYp82ecvomUJjU2ewORndhD LrR/b8LgH8bShTVOC9ZkUz+O7G5iz1/0JG6g55wnPVjX+rz3OEtv9KiBwqS0Uo3P94I/ yo9BeoINQefCG2vErvweBwkb4iIwsoMiQU/YpdnOhFmljmcJ+hdR8XQceNBTCq+ADxh8 ZDWMk+Po/LLCjnonNqd/RYk4xlzTLppT6C8H393RiEQCkOr9L6KEDC3PGZlNpNH2piNZ fMjQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=T3Vg7vCu; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 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 (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id j13-20020a170903028d00b00186b59eba21si10211423plr.574.2022.11.20.16.40.20; Sun, 20 Nov 2022 16:40:36 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=T3Vg7vCu; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230113AbiKUAbE (ORCPT <rfc822;leviz.kernel.dev@gmail.com> + 99 others); Sun, 20 Nov 2022 19:31:04 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58626 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229929AbiKUAa2 (ORCPT <rfc822;linux-kernel@vger.kernel.org>); Sun, 20 Nov 2022 19:30:28 -0500 Received: from mga07.intel.com (mga07.intel.com [134.134.136.100]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E65991E708; Sun, 20 Nov 2022 16:28:53 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1668990537; x=1700526537; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=jIBcb5cVMwmxp99dDnDGXF9eYbfG3mWlVut9+yUNmLc=; b=T3Vg7vCuQSdz5CNeHk1ybLQkedvecjgDu3L+33rOOaE5zbN81zHyls4w 0MA6UySZb7sSsoOTizQ1K5mRux81qCxeK2w7PmjjP75Y/lTk0ufiL0u3n 5oNBKyc02m+uPl88OI0g4kuN0eljC7TI1LqU+Rpsi2+gu3ewvg4Kh3uHJ JqnOIlqaiM0KsZzJe6IjNhhfJ1Odw5cWf8pkN2C7Eofb74ihh5ejkoWUV Pe2eStsG3Z4pwxbNBRuatz/am5jBL003yO3nOaw1MT/7IMbgxZTbLOGi4 ibrn2GKqMWx6v6fHsSc1a9OFiQlTzqL9iNhSBmGW+QXlYUkBvlF5jAEdc A==; X-IronPort-AV: E=McAfee;i="6500,9779,10537"; a="377705756" X-IronPort-AV: E=Sophos;i="5.96,180,1665471600"; d="scan'208";a="377705756" Received: from fmsmga003.fm.intel.com ([10.253.24.29]) by orsmga105.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 20 Nov 2022 16:28:09 -0800 X-IronPort-AV: E=McAfee;i="6500,9779,10537"; a="729825533" X-IronPort-AV: E=Sophos;i="5.96,180,1665471600"; d="scan'208";a="729825533" Received: from tomnavar-mobl.amr.corp.intel.com (HELO khuang2-desk.gar.corp.intel.com) ([10.209.176.15]) by fmsmga003-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 20 Nov 2022 16:28:05 -0800 From: Kai Huang <kai.huang@intel.com> To: linux-kernel@vger.kernel.org, kvm@vger.kernel.org Cc: linux-mm@kvack.org, seanjc@google.com, pbonzini@redhat.com, dave.hansen@intel.com, dan.j.williams@intel.com, rafael.j.wysocki@intel.com, kirill.shutemov@linux.intel.com, ying.huang@intel.com, reinette.chatre@intel.com, len.brown@intel.com, tony.luck@intel.com, peterz@infradead.org, ak@linux.intel.com, isaku.yamahata@intel.com, chao.gao@intel.com, sathyanarayanan.kuppuswamy@linux.intel.com, bagasdotme@gmail.com, sagis@google.com, imammedo@redhat.com, kai.huang@intel.com Subject: [PATCH v7 17/20] x86/virt/tdx: Configure global KeyID on all packages Date: Mon, 21 Nov 2022 13:26:39 +1300 Message-Id: <8d8285cc5efa6302cf42a3fe2c9153d1a9dbcdac.1668988357.git.kai.huang@intel.com> X-Mailer: git-send-email 2.38.1 In-Reply-To: <cover.1668988357.git.kai.huang@intel.com> References: <cover.1668988357.git.kai.huang@intel.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED, SPF_HELO_NONE,SPF_NONE 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: <linux-kernel.vger.kernel.org> X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1750064154423130313?= X-GMAIL-MSGID: =?utf-8?q?1750064154423130313?= |
Series |
TDX host kernel support
|
|
Commit Message
Kai Huang
Nov. 21, 2022, 12:26 a.m. UTC
After the array of TDMRs and the global KeyID are configured to the TDX module, use TDH.SYS.KEY.CONFIG to configure the key of the global KeyID on all packages. TDH.SYS.KEY.CONFIG must be done on one (any) cpu for each package. And it cannot run concurrently on different CPUs. Implement a helper to run SEAMCALL on one cpu for each package one by one, and use it to configure the global KeyID on all packages. Intel hardware doesn't guarantee cache coherency across different KeyIDs. The kernel needs to flush PAMT's dirty cachelines (associated with KeyID 0) before the TDX module uses the global KeyID to access the PAMT. Following the TDX module specification, flush cache before configuring the global KeyID on all packages. Given the PAMT size can be large (~1/256th of system RAM), just use WBINVD on all CPUs to flush. Note if any TDH.SYS.KEY.CONFIG fails, the TDX module may already have used the global KeyID to write any PAMT. Therefore, need to use WBINVD to flush cache before freeing the PAMTs back to the kernel. Note using MOVDIR64B (which changes the page's associated KeyID from the old TDX private KeyID back to KeyID 0, which is used by the kernel) to clear PMATs isn't needed, as the KeyID 0 doesn't support integrity check. Reviewed-by: Isaku Yamahata <isaku.yamahata@intel.com> Signed-off-by: Kai Huang <kai.huang@intel.com> --- v6 -> v7: - Improved changelong and comment to explain why MOVDIR64B isn't used when returning PAMTs back to the kernel. --- arch/x86/virt/vmx/tdx/tdx.c | 89 ++++++++++++++++++++++++++++++++++++- arch/x86/virt/vmx/tdx/tdx.h | 1 + 2 files changed, 88 insertions(+), 2 deletions(-)
Comments
On 11/20/22 16:26, Kai Huang wrote: > After the array of TDMRs and the global KeyID are configured to the TDX > module, use TDH.SYS.KEY.CONFIG to configure the key of the global KeyID > on all packages. > > TDH.SYS.KEY.CONFIG must be done on one (any) cpu for each package. And > it cannot run concurrently on different CPUs. Implement a helper to > run SEAMCALL on one cpu for each package one by one, and use it to > configure the global KeyID on all packages. This has the same problems as SYS.LP.INIT. It's basically snake oil in some TDX configurations. This really only needs to be done when the TDX module has memory mappings on a socket for which it needs to use the "global KeyID". If there's no PAMT on a socket, there are probably no allocations there to speak of and no *technical* reason to call TDH.SYS.KEY.CONFIG on that socket. At least none I can see. So, let's check up on this requirement as well. This could also turn out to be a real pain if all the CPUs on a socket are offline. > Intel hardware doesn't guarantee cache coherency across different > KeyIDs. The kernel needs to flush PAMT's dirty cachelines (associated > with KeyID 0) before the TDX module uses the global KeyID to access the > PAMT. Following the TDX module specification, flush cache before > configuring the global KeyID on all packages. I think it's probably worth an aside here about why TDX security isn't dependent on this step. I *think* it boils down to the memory integrity protections. If the caches aren't flushed, a dirty KeyID-0 cacheline could be written back to RAM. The TDX module would come along later and read the cacheline using KeyID-whatever, get an integrity mismatch, machine check, and then everyone would be sad. Everyone is sad, but TDX security remains intact because memory integrity saved us. Is it memory integrity or the TD bit, actually? > Given the PAMT size can be large (~1/256th of system RAM), just use > WBINVD on all CPUs to flush. <sigh> > Note if any TDH.SYS.KEY.CONFIG fails, the TDX module may already have > used the global KeyID to write any PAMT. Therefore, need to use WBINVD > to flush cache before freeing the PAMTs back to the kernel. Note using > MOVDIR64B (which changes the page's associated KeyID from the old TDX > private KeyID back to KeyID 0, which is used by the kernel) to clear > PMATs isn't needed, as the KeyID 0 doesn't support integrity check. I hope this is covered in the code well. > diff --git a/arch/x86/virt/vmx/tdx/tdx.c b/arch/x86/virt/vmx/tdx/tdx.c > index 3a032930e58a..99d1be5941a7 100644 > --- a/arch/x86/virt/vmx/tdx/tdx.c > +++ b/arch/x86/virt/vmx/tdx/tdx.c > @@ -224,6 +224,46 @@ static void seamcall_on_each_cpu(struct seamcall_ctx *sc) > on_each_cpu(seamcall_smp_call_function, sc, true); > } > > +/* > + * Call one SEAMCALL on one (any) cpu for each physical package in > + * serialized way. Return immediately in case of any error if > + * SEAMCALL fails on any cpu. > + * > + * Note for serialized calls 'struct seamcall_ctx::err' doesn't have > + * to be atomic, but for simplicity just reuse it instead of adding > + * a new one. > + */ > +static int seamcall_on_each_package_serialized(struct seamcall_ctx *sc) > +{ > + cpumask_var_t packages; > + int cpu, ret = 0; > + > + if (!zalloc_cpumask_var(&packages, GFP_KERNEL)) > + return -ENOMEM; > + > + for_each_online_cpu(cpu) { > + if (cpumask_test_and_set_cpu(topology_physical_package_id(cpu), > + packages)) > + continue; > + > + ret = smp_call_function_single(cpu, seamcall_smp_call_function, > + sc, true); > + if (ret) > + break; > + > + /* > + * Doesn't have to use atomic_read(), but it doesn't > + * hurt either. > + */ I don't think you need to cover this twice. Just do it in one comment. > + ret = atomic_read(&sc->err); > + if (ret) > + break; > + } > + > + free_cpumask_var(packages); > + return ret; > +} > + > static int tdx_module_init_cpus(void) > { > struct seamcall_ctx sc = { .fn = TDH_SYS_LP_INIT }; > @@ -1010,6 +1050,22 @@ static int config_tdx_module(struct tdmr_info *tdmr_array, int tdmr_num, > return ret; > } > > +static int config_global_keyid(void) > +{ > + struct seamcall_ctx sc = { .fn = TDH_SYS_KEY_CONFIG }; > + > + /* > + * Configure the key of the global KeyID on all packages by > + * calling TDH.SYS.KEY.CONFIG on all packages in a serialized > + * way as it cannot run concurrently on different CPUs. > + * > + * TDH.SYS.KEY.CONFIG may fail with entropy error (which is > + * a recoverable error). Assume this is exceedingly rare and > + * just return error if encountered instead of retrying. > + */ > + return seamcall_on_each_package_serialized(&sc); > +} > + > /* > * Detect and initialize the TDX module. > * > @@ -1098,15 +1154,44 @@ static int init_tdx_module(void) > if (ret) > goto out_free_pamts; > > + /* > + * Hardware doesn't guarantee cache coherency across different > + * KeyIDs. The kernel needs to flush PAMT's dirty cachelines > + * (associated with KeyID 0) before the TDX module can use the > + * global KeyID to access the PAMT. Given PAMTs are potentially > + * large (~1/256th of system RAM), just use WBINVD on all cpus > + * to flush the cache. > + * > + * Follow the TDX spec to flush cache before configuring the > + * global KeyID on all packages. > + */ > + wbinvd_on_all_cpus(); > + > + /* Config the key of global KeyID on all packages */ > + ret = config_global_keyid(); > + if (ret) > + goto out_free_pamts; > + > /* > * Return -EINVAL until all steps of TDX module initialization > * process are done. > */ > ret = -EINVAL; > out_free_pamts: > - if (ret) > + if (ret) { > + /* > + * Part of PAMT may already have been initialized by s/initialized/written/ > + * TDX module. Flush cache before returning PAMT back > + * to the kernel. > + * > + * Note there's no need to do MOVDIR64B (which changes > + * the page's associated KeyID from the old TDX private > + * KeyID back to KeyID 0, which is used by the kernel), > + * as KeyID 0 doesn't support integrity check. > + */ MOVDIR64B is the tiniest of implementation details and also not the only way to initialize memory integrity metadata. Just keep this high level: * No need to worry about memory integrity checks here. * KeyID 0 has integrity checking disabled. > + wbinvd_on_all_cpus(); > tdmrs_free_pamt_all(tdmr_array, tdmr_num); > - else > + } else > pr_info("%lu pages allocated for PAMT.\n", > tdmrs_count_pamt_pages(tdmr_array, tdmr_num)); > out_free_tdmrs: > diff --git a/arch/x86/virt/vmx/tdx/tdx.h b/arch/x86/virt/vmx/tdx/tdx.h > index c26bab2555ca..768d097412ab 100644 > --- a/arch/x86/virt/vmx/tdx/tdx.h > +++ b/arch/x86/virt/vmx/tdx/tdx.h > @@ -15,6 +15,7 @@ > /* > * TDX module SEAMCALL leaf functions > */ > +#define TDH_SYS_KEY_CONFIG 31 > #define TDH_SYS_INFO 32 > #define TDH_SYS_INIT 33 > #define TDH_SYS_LP_INIT 35
On Wed, 2022-11-23 at 16:28 -0800, Dave Hansen wrote: > > On 11/20/22 16:26, Kai Huang wrote: > > > > After the array of TDMRs and the global KeyID are configured to the TDX > > > > module, use TDH.SYS.KEY.CONFIG to configure the key of the global KeyID > > > > on all packages. > > > > > > > > TDH.SYS.KEY.CONFIG must be done on one (any) cpu for each package. And > > > > it cannot run concurrently on different CPUs. Implement a helper to > > > > run SEAMCALL on one cpu for each package one by one, and use it to > > > > configure the global KeyID on all packages. > > > > This has the same problems as SYS.LP.INIT. It's basically snake oil in > > some TDX configurations. > > > > This really only needs to be done when the TDX module has memory > > mappings on a socket for which it needs to use the "global KeyID". > > I think so. > > If > > there's no PAMT on a socket, there are probably no allocations there to > > speak of and no *technical* reason to call TDH.SYS.KEY.CONFIG on that > > socket. At least none I can see. Right PAMT is the main user. Besides PAMT, each TDX guest has a "Trust Domain Root" (TDR) page, and this TDR page is also encrypted using the global KeyID. > > > > So, let's check up on this requirement as well. This could also turn > > out to be a real pain if all the CPUs on a socket are offline. Yes I'll also check. > > > > > > Intel hardware doesn't guarantee cache coherency across different > > > > KeyIDs. The kernel needs to flush PAMT's dirty cachelines (associated > > > > with KeyID 0) before the TDX module uses the global KeyID to access the > > > > PAMT. Following the TDX module specification, flush cache before > > > > configuring the global KeyID on all packages. > > > > I think it's probably worth an aside here about why TDX security isn't > > dependent on this step. I *think* it boils down to the memory integrity > > protections. If the caches aren't flushed, a dirty KeyID-0 cacheline > > could be written back to RAM. The TDX module would come along later and > > read the cacheline using KeyID-whatever, get an integrity mismatch, > > machine check, and then everyone would be sad. > > > > Everyone is sad, but TDX security remains intact because memory > > integrity saved us. > > > > Is it memory integrity or the TD bit, actually? For this case, I think memory integrity. The TD bit is mainly used to prevent host kernel from being able to read the integrity checksum (MAC) of TD memory, which could result in potential blute- force attack on the MAC. Specifically, there's such attack that: the host kernel can establish a shared (non-TDX private) KeyID mapping, and repeatedly use different keys via that mapping to speculatively read TDX guest memory. W/o the TD bit, the hardware always performs the integrity check and thus there's possibility that the host eventually can find a key which matches the integrity check. To prevent such case, the TD bit is added. Hardware will check the TD bit match first, and only perform integrity check _after_ TD bit match. So in above case, host kernel speculatively read TDX memory via shared KeyID mapping will always get a TD bit mismatch, thus won't be able to achieve above attack. > > > > Given the PAMT size can be large (~1/256th of system RAM), just use > > > > WBINVD on all CPUs to flush. > > > > <sigh> > > > > > > Note if any TDH.SYS.KEY.CONFIG fails, the TDX module may already have > > > > used the global KeyID to write any PAMT. Therefore, need to use WBINVD > > > > to flush cache before freeing the PAMTs back to the kernel. Note using > > > > MOVDIR64B (which changes the page's associated KeyID from the old TDX > > > > private KeyID back to KeyID 0, which is used by the kernel) to clear > > > > PMATs isn't needed, as the KeyID 0 doesn't support integrity check. > > > > I hope this is covered in the code well. > > > > > > diff --git a/arch/x86/virt/vmx/tdx/tdx.c b/arch/x86/virt/vmx/tdx/tdx.c > > > > index 3a032930e58a..99d1be5941a7 100644 > > > > --- a/arch/x86/virt/vmx/tdx/tdx.c > > > > +++ b/arch/x86/virt/vmx/tdx/tdx.c > > > > @@ -224,6 +224,46 @@ static void seamcall_on_each_cpu(struct > > > > seamcall_ctx *sc) > > > > on_each_cpu(seamcall_smp_call_function, sc, true); > > > > } > > > > > > > > +/* > > > > + * Call one SEAMCALL on one (any) cpu for each physical package in > > > > + * serialized way. Return immediately in case of any error if > > > > + * SEAMCALL fails on any cpu. > > > > + * > > > > + * Note for serialized calls 'struct seamcall_ctx::err' doesn't have > > > > + * to be atomic, but for simplicity just reuse it instead of adding > > > > + * a new one. > > > > + */ > > > > +static int seamcall_on_each_package_serialized(struct seamcall_ctx *sc) > > > > +{ > > > > + cpumask_var_t packages; > > > > + int cpu, ret = 0; > > > > + > > > > + if (!zalloc_cpumask_var(&packages, GFP_KERNEL)) > > > > + return -ENOMEM; > > > > + > > > > + for_each_online_cpu(cpu) { > > > > + if > > > > (cpumask_test_and_set_cpu(topology_physical_package_id(cpu), > > > > + packages)) > > > > + continue; > > > > + > > > > + ret = smp_call_function_single(cpu, > > > > seamcall_smp_call_function, > > > > + sc, true); > > > > + if (ret) > > > > + break; > > > > + > > > > + /* > > > > + * Doesn't have to use atomic_read(), but it doesn't > > > > + * hurt either. > > > > + */ > > > > I don't think you need to cover this twice. Just do it in one comment. Right. I'll remove. I'll be more careful next time to avoid such pattern. > > > > > > + ret = atomic_read(&sc->err); > > > > + if (ret) > > > > + break; > > > > + } > > > > + > > > > + free_cpumask_var(packages); > > > > + return ret; > > > > +} > > > > + > > > > static int tdx_module_init_cpus(void) > > > > { > > > > struct seamcall_ctx sc = { .fn = TDH_SYS_LP_INIT }; > > > > @@ -1010,6 +1050,22 @@ static int config_tdx_module(struct tdmr_info *tdmr_array, int tdmr_num, > > > > return ret; > > > > } > > > > > > > > +static int config_global_keyid(void) > > > > +{ > > > > + struct seamcall_ctx sc = { .fn = TDH_SYS_KEY_CONFIG }; > > > > + > > > > + /* > > > > + * Configure the key of the global KeyID on all packages by > > > > + * calling TDH.SYS.KEY.CONFIG on all packages in a serialized > > > > + * way as it cannot run concurrently on different CPUs. > > > > + * > > > > + * TDH.SYS.KEY.CONFIG may fail with entropy error (which is > > > > + * a recoverable error). Assume this is exceedingly rare and > > > > + * just return error if encountered instead of retrying. > > > > + */ > > > > + return seamcall_on_each_package_serialized(&sc); > > > > +} > > > > + > > > > /* > > > > * Detect and initialize the TDX module. > > > > * > > > > @@ -1098,15 +1154,44 @@ static int init_tdx_module(void) > > > > if (ret) > > > > goto out_free_pamts; > > > > > > > > + /* > > > > + * Hardware doesn't guarantee cache coherency across different > > > > + * KeyIDs. The kernel needs to flush PAMT's dirty cachelines > > > > + * (associated with KeyID 0) before the TDX module can use the > > > > + * global KeyID to access the PAMT. Given PAMTs are potentially > > > > + * large (~1/256th of system RAM), just use WBINVD on all cpus > > > > + * to flush the cache. > > > > + * > > > > + * Follow the TDX spec to flush cache before configuring the > > > > + * global KeyID on all packages. > > > > + */ > > > > + wbinvd_on_all_cpus(); > > > > + > > > > + /* Config the key of global KeyID on all packages */ > > > > + ret = config_global_keyid(); > > > > + if (ret) > > > > + goto out_free_pamts; > > > > + > > > > /* > > > > * Return -EINVAL until all steps of TDX module initialization > > > > * process are done. > > > > */ > > > > ret = -EINVAL; > > > > out_free_pamts: > > > > - if (ret) > > > > + if (ret) { > > > > + /* > > > > + * Part of PAMT may already have been initialized by > > > > s/initialized/written/ Will do. > > > > > > + * TDX module. Flush cache before returning PAMT back > > > > + * to the kernel. > > > > + * > > > > + * Note there's no need to do MOVDIR64B (which changes > > > > + * the page's associated KeyID from the old TDX private > > > > + * KeyID back to KeyID 0, which is used by the kernel), > > > > + * as KeyID 0 doesn't support integrity check. > > > > + */ > > > > MOVDIR64B is the tiniest of implementation details and also not the only > > way to initialize memory integrity metadata. > > > > Just keep this high level: > > > > * No need to worry about memory integrity checks here. > > * KeyID 0 has integrity checking disabled. Will do. Thanks.
On Thu, 2022-11-24 at 22:28 +0000, Huang, Kai wrote: > > > > > Intel hardware doesn't guarantee cache coherency across different > > > > > KeyIDs. The kernel needs to flush PAMT's dirty cachelines (associated > > > > > with KeyID 0) before the TDX module uses the global KeyID to access > > > > > the > > > > > PAMT. Following the TDX module specification, flush cache before > > > > > configuring the global KeyID on all packages. > > > > > > I think it's probably worth an aside here about why TDX security isn't > > > dependent on this step. I *think* it boils down to the memory integrity > > > protections. If the caches aren't flushed, a dirty KeyID-0 cacheline > > > could be written back to RAM. The TDX module would come along later and > > > read the cacheline using KeyID-whatever, get an integrity mismatch, > > > machine check, and then everyone would be sad. > > > > > > Everyone is sad, but TDX security remains intact because memory > > > integrity saved us. > > > > > > Is it memory integrity or the TD bit, actually? > > For this case, I think memory integrity. Sorry thinking again, I was wrong. It should be the TD bit, since TD bit check happens before integrity check. So the follow is: 1) Dirty KeyID-0 cacheline written back to RAM, which clears the TD bit. 2) TDX module reads PAMT using TDX private KeyId causes TD mismatch, which in this case, results in poison. 3) Further consume of poisoned data results in #MC. > > The TD bit is mainly used to prevent host kernel from being able to read the > integrity checksum (MAC) of TD memory, which could result in potential blute- > force attack on the MAC. > > Specifically, there's such attack that: the host kernel can establish a shared > (non-TDX private) KeyID mapping, and repeatedly use different keys via that > mapping to speculatively read TDX guest memory. W/o the TD bit, the hardware > always performs the integrity check and thus there's possibility that the host > eventually can find a key which matches the integrity check. > > To prevent such case, the TD bit is added. Hardware will check the TD bit > match > first, and only perform integrity check _after_ TD bit match. So in above > case, > host kernel speculatively read TDX memory via shared KeyID mapping will always > get a TD bit mismatch, thus won't be able to achieve above attack.
On 11/21/2022 8:26 AM, Kai Huang wrote: > After the array of TDMRs and the global KeyID are configured to the TDX > module, use TDH.SYS.KEY.CONFIG to configure the key of the global KeyID > on all packages. > > TDH.SYS.KEY.CONFIG must be done on one (any) cpu for each package. And > it cannot run concurrently on different CPUs. Implement a helper to > run SEAMCALL on one cpu for each package one by one, and use it to > configure the global KeyID on all packages. > > Intel hardware doesn't guarantee cache coherency across different > KeyIDs. The kernel needs to flush PAMT's dirty cachelines (associated > with KeyID 0) before the TDX module uses the global KeyID to access the > PAMT. Following the TDX module specification, flush cache before > configuring the global KeyID on all packages. > > Given the PAMT size can be large (~1/256th of system RAM), just use > WBINVD on all CPUs to flush. > > Note if any TDH.SYS.KEY.CONFIG fails, the TDX module may already have > used the global KeyID to write any PAMT. Therefore, need to use WBINVD > to flush cache before freeing the PAMTs back to the kernel. Note using > MOVDIR64B (which changes the page's associated KeyID from the old TDX > private KeyID back to KeyID 0, which is used by the kernel) It seems not accurate to say MOVDIR64B changes the page's associated KeyID. It just uses the current KeyID for memory operations. > to clear > PMATs isn't needed, as the KeyID 0 doesn't support integrity check. For integrity check, is KeyID 0 special or it just has the same behavior as non-zero shared KeyID (if any)? By saying "KeyID 0 doesn't support integrity check", is it because of the implementation of this patch set or hardware behavior? According to Architecure Specification 1.0 of TDX Module (344425-004US), shared KeyID could also enable integrity check. > > Reviewed-by: Isaku Yamahata <isaku.yamahata@intel.com> > Signed-off-by: Kai Huang <kai.huang@intel.com> > --- > > v6 -> v7: > - Improved changelong and comment to explain why MOVDIR64B isn't used > when returning PAMTs back to the kernel. > > --- > arch/x86/virt/vmx/tdx/tdx.c | 89 ++++++++++++++++++++++++++++++++++++- > arch/x86/virt/vmx/tdx/tdx.h | 1 + > 2 files changed, 88 insertions(+), 2 deletions(-) > > diff --git a/arch/x86/virt/vmx/tdx/tdx.c b/arch/x86/virt/vmx/tdx/tdx.c > index 3a032930e58a..99d1be5941a7 100644 > --- a/arch/x86/virt/vmx/tdx/tdx.c > +++ b/arch/x86/virt/vmx/tdx/tdx.c > @@ -224,6 +224,46 @@ static void seamcall_on_each_cpu(struct seamcall_ctx *sc) > on_each_cpu(seamcall_smp_call_function, sc, true); > } > > +/* > + * Call one SEAMCALL on one (any) cpu for each physical package in > + * serialized way. Return immediately in case of any error if > + * SEAMCALL fails on any cpu. > + * > + * Note for serialized calls 'struct seamcall_ctx::err' doesn't have > + * to be atomic, but for simplicity just reuse it instead of adding > + * a new one. > + */ > +static int seamcall_on_each_package_serialized(struct seamcall_ctx *sc) > +{ > + cpumask_var_t packages; > + int cpu, ret = 0; > + > + if (!zalloc_cpumask_var(&packages, GFP_KERNEL)) > + return -ENOMEM; > + > + for_each_online_cpu(cpu) { > + if (cpumask_test_and_set_cpu(topology_physical_package_id(cpu), > + packages)) > + continue; > + > + ret = smp_call_function_single(cpu, seamcall_smp_call_function, > + sc, true); > + if (ret) > + break; > + > + /* > + * Doesn't have to use atomic_read(), but it doesn't > + * hurt either. > + */ > + ret = atomic_read(&sc->err); > + if (ret) > + break; > + } > + > + free_cpumask_var(packages); > + return ret; > +} > + > static int tdx_module_init_cpus(void) > { > struct seamcall_ctx sc = { .fn = TDH_SYS_LP_INIT }; > @@ -1010,6 +1050,22 @@ static int config_tdx_module(struct tdmr_info *tdmr_array, int tdmr_num, > return ret; > } > > +static int config_global_keyid(void) > +{ > + struct seamcall_ctx sc = { .fn = TDH_SYS_KEY_CONFIG }; > + > + /* > + * Configure the key of the global KeyID on all packages by > + * calling TDH.SYS.KEY.CONFIG on all packages in a serialized > + * way as it cannot run concurrently on different CPUs. > + * > + * TDH.SYS.KEY.CONFIG may fail with entropy error (which is > + * a recoverable error). Assume this is exceedingly rare and > + * just return error if encountered instead of retrying. > + */ > + return seamcall_on_each_package_serialized(&sc); > +} > + > /* > * Detect and initialize the TDX module. > * > @@ -1098,15 +1154,44 @@ static int init_tdx_module(void) > if (ret) > goto out_free_pamts; > > + /* > + * Hardware doesn't guarantee cache coherency across different > + * KeyIDs. The kernel needs to flush PAMT's dirty cachelines > + * (associated with KeyID 0) before the TDX module can use the > + * global KeyID to access the PAMT. Given PAMTs are potentially > + * large (~1/256th of system RAM), just use WBINVD on all cpus > + * to flush the cache. > + * > + * Follow the TDX spec to flush cache before configuring the > + * global KeyID on all packages. > + */ > + wbinvd_on_all_cpus(); > + > + /* Config the key of global KeyID on all packages */ > + ret = config_global_keyid(); > + if (ret) > + goto out_free_pamts; > + > /* > * Return -EINVAL until all steps of TDX module initialization > * process are done. > */ > ret = -EINVAL; > out_free_pamts: > - if (ret) > + if (ret) { > + /* > + * Part of PAMT may already have been initialized by > + * TDX module. Flush cache before returning PAMT back > + * to the kernel. > + * > + * Note there's no need to do MOVDIR64B (which changes > + * the page's associated KeyID from the old TDX private > + * KeyID back to KeyID 0, which is used by the kernel), > + * as KeyID 0 doesn't support integrity check. > + */ > + wbinvd_on_all_cpus(); > tdmrs_free_pamt_all(tdmr_array, tdmr_num); > - else > + } else > pr_info("%lu pages allocated for PAMT.\n", > tdmrs_count_pamt_pages(tdmr_array, tdmr_num)); > out_free_tdmrs: > diff --git a/arch/x86/virt/vmx/tdx/tdx.h b/arch/x86/virt/vmx/tdx/tdx.h > index c26bab2555ca..768d097412ab 100644 > --- a/arch/x86/virt/vmx/tdx/tdx.h > +++ b/arch/x86/virt/vmx/tdx/tdx.h > @@ -15,6 +15,7 @@ > /* > * TDX module SEAMCALL leaf functions > */ > +#define TDH_SYS_KEY_CONFIG 31 > #define TDH_SYS_INFO 32 > #define TDH_SYS_INIT 33 > #define TDH_SYS_LP_INIT 35
On Wed, 2022-11-30 at 11:35 +0800, Binbin Wu wrote: > On 11/21/2022 8:26 AM, Kai Huang wrote: > > After the array of TDMRs and the global KeyID are configured to the TDX > > module, use TDH.SYS.KEY.CONFIG to configure the key of the global KeyID > > on all packages. > > > > TDH.SYS.KEY.CONFIG must be done on one (any) cpu for each package. And > > it cannot run concurrently on different CPUs. Implement a helper to > > run SEAMCALL on one cpu for each package one by one, and use it to > > configure the global KeyID on all packages. > > > > Intel hardware doesn't guarantee cache coherency across different > > KeyIDs. The kernel needs to flush PAMT's dirty cachelines (associated > > with KeyID 0) before the TDX module uses the global KeyID to access the > > PAMT. Following the TDX module specification, flush cache before > > configuring the global KeyID on all packages. > > > > Given the PAMT size can be large (~1/256th of system RAM), just use > > WBINVD on all CPUs to flush. > > > > Note if any TDH.SYS.KEY.CONFIG fails, the TDX module may already have > > used the global KeyID to write any PAMT. Therefore, need to use WBINVD > > to flush cache before freeing the PAMTs back to the kernel. Note using > > MOVDIR64B (which changes the page's associated KeyID from the old TDX > > private KeyID back to KeyID 0, which is used by the kernel) > > It seems not accurate to say MOVDIR64B changes the page's associated KeyID. > It just uses the current KeyID for memory operations. The "write" to the memory changes the page's associated KeyID to the KeyID that does the "write". A more accurate expression perhaps should be MOVDIR64B + MFENSE, but I think it doesn't matter in changelog. > > > > to clear > > PMATs isn't needed, as the KeyID 0 doesn't support integrity check. > > For integrity check, is KeyID 0 special or it just has the same behavior > as non-zero shared KeyID (if any)? KeyID 0 is TME KeyID. It is special. Hardware treats the KeyID 0 differently. > > By saying "KeyID 0 doesn't support integrity check", is it because of > the implementation of this patch set or hardware behavior? It's hardware behaviour. > > According to Architecure Specification 1.0 of TDX Module (344425-004US), > shared KeyID could also enable integrity check. > KeyID 0 is different from non-0 MKTME shared KeyID. For example, you cannot do PCONFIG on KeyID 0.
On Wed, Nov 30, 2022 at 08:34:46AM +0000, Huang, Kai wrote: > On Wed, 2022-11-30 at 11:35 +0800, Binbin Wu wrote: > > On 11/21/2022 8:26 AM, Kai Huang wrote: > > > After the array of TDMRs and the global KeyID are configured to the TDX > > > module, use TDH.SYS.KEY.CONFIG to configure the key of the global KeyID > > > on all packages. > > > > > > TDH.SYS.KEY.CONFIG must be done on one (any) cpu for each package. And > > > it cannot run concurrently on different CPUs. Implement a helper to > > > run SEAMCALL on one cpu for each package one by one, and use it to > > > configure the global KeyID on all packages. > > > > > > Intel hardware doesn't guarantee cache coherency across different > > > KeyIDs. The kernel needs to flush PAMT's dirty cachelines (associated > > > with KeyID 0) before the TDX module uses the global KeyID to access the > > > PAMT. Following the TDX module specification, flush cache before > > > configuring the global KeyID on all packages. > > > > > > Given the PAMT size can be large (~1/256th of system RAM), just use > > > WBINVD on all CPUs to flush. > > > > > > Note if any TDH.SYS.KEY.CONFIG fails, the TDX module may already have > > > used the global KeyID to write any PAMT. Therefore, need to use WBINVD > > > to flush cache before freeing the PAMTs back to the kernel. Note using > > > MOVDIR64B (which changes the page's associated KeyID from the old TDX > > > private KeyID back to KeyID 0, which is used by the kernel) > > > > It seems not accurate to say MOVDIR64B changes the page's associated KeyID. > > It just uses the current KeyID for memory operations. > > The "write" to the memory changes the page's associated KeyID to the KeyID that > does the "write". A more accurate expression perhaps should be MOVDIR64B + > MFENSE, but I think it doesn't matter in changelog. MOVDIR64B KeyID for the cache line, not the page. Integrity tracked on per-cacheline basis.
On 11/30/22 00:34, Huang, Kai wrote: > On Wed, 2022-11-30 at 11:35 +0800, Binbin Wu wrote: >> On 11/21/2022 8:26 AM, Kai Huang wrote: >>> After the array of TDMRs and the global KeyID are configured to the TDX >>> module, use TDH.SYS.KEY.CONFIG to configure the key of the global KeyID >>> on all packages. >>> >>> TDH.SYS.KEY.CONFIG must be done on one (any) cpu for each package. And >>> it cannot run concurrently on different CPUs. Implement a helper to >>> run SEAMCALL on one cpu for each package one by one, and use it to >>> configure the global KeyID on all packages. >>> >>> Intel hardware doesn't guarantee cache coherency across different >>> KeyIDs. The kernel needs to flush PAMT's dirty cachelines (associated >>> with KeyID 0) before the TDX module uses the global KeyID to access the >>> PAMT. Following the TDX module specification, flush cache before >>> configuring the global KeyID on all packages. >>> >>> Given the PAMT size can be large (~1/256th of system RAM), just use >>> WBINVD on all CPUs to flush. >>> >>> Note if any TDH.SYS.KEY.CONFIG fails, the TDX module may already have >>> used the global KeyID to write any PAMT. Therefore, need to use WBINVD >>> to flush cache before freeing the PAMTs back to the kernel. Note using >>> MOVDIR64B (which changes the page's associated KeyID from the old TDX >>> private KeyID back to KeyID 0, which is used by the kernel) >> >> It seems not accurate to say MOVDIR64B changes the page's associated KeyID. >> It just uses the current KeyID for memory operations. > > The "write" to the memory changes the page's associated KeyID to the KeyID that > does the "write". A more accurate expression perhaps should be MOVDIR64B + > MFENSE, but I think it doesn't matter in changelog. Just delete it from the changelog. It's a distraction. I'm not even sure it's *necessary* to do any memory content conversion after the TDX module has written gunk. There won't be any integrity issues because integrity errors don't do anything for KeyID-0 (no #MC). I _think_ the reads of the page using KeyID-0 will see abort page semantics. That's *FINE*.
On 11/20/22 16:26, Kai Huang wrote: > After the array of TDMRs and the global KeyID are configured to the TDX > module, use TDH.SYS.KEY.CONFIG to configure the key of the global KeyID > on all packages. I want to circle back to this because it potentially has the same class of issue that TDH.SYS.LP.INIT had. So, here's some more background followed by the key question: is TDH.SYS.KEY.CONFIG too strict? Should we explore relaxing it? Here's the very long-winded way of asking the same thing: This key is used to protect TDX module memory which is too large to fit into the limited range-register-protected (SMRR) areas that most of the module uses. Right now, that metadata includes the Physical Address Metadata Tables (PAMT) and "TD Root" (TDR) pages. Using this "global KeyID" provides stronger isolation and integrity protection for these structures than is provided by KeyID-0. The "global KeyID" only strictly needs to be programmed into a memory controllers if a PAMT or TDR page is allocated in memory attached to that controller. However, the TDX module currently requires that TDH.SYS.KEY.CONFIG be executed on one processor in each package. This is true even if there is no TDX Memory Region (TDMR) attached to that package. This was likely done for simplicity in the TDX module. It currently has no NUMA awareness (or even trusted NUMA metadata) and no ability to correlate processor packages with the memory attached to their memory controllers. The TDH.SYS.KEY.CONFIG design is actually pretty similar to Kirill's MKTME implementation[1]. Basically blast the KeyID configuration out to one processor in each package, regardless of whether the KeyID will ever get used on that package. While this requirement from the TDX module is _slightly_ too strict, I'm not quite as worried about it as I was about the *super* strict TDH.SYS.LP.INIT requirements. It's a lot harder and more rare to have an entire package of CPUs unavailable versus a single logical CPU. There is, for instance, no side-channel mitigation that disables an entire package worth of CPUs. I'm not even sure if we allow an entire package worth of NOHZ_FULL-indisposed processors. I'm happy to go run the same drill for TDH.SYS.KEY.CONFIG that we did for TDH.SYS.LP.INIT. Basically, can we relax the too-strict restrictions? But, I'm not sure anyone will ever reap a practical benefit from it. I'm tempted to just leave it as-is. Does anyone feel differently? 1. https://lore.kernel.org/lkml/20190508144422.13171-1-kirill.shutemov@linux.intel.com/T/#m936f260a345284687f8e929675f68f3d514725f5
> On 11/30/22 00:34, Huang, Kai wrote: > > On Wed, 2022-11-30 at 11:35 +0800, Binbin Wu wrote: > >> On 11/21/2022 8:26 AM, Kai Huang wrote: > >>> After the array of TDMRs and the global KeyID are configured to the > >>> TDX module, use TDH.SYS.KEY.CONFIG to configure the key of the > >>> global KeyID on all packages. > >>> > >>> TDH.SYS.KEY.CONFIG must be done on one (any) cpu for each package. > >>> And it cannot run concurrently on different CPUs. Implement a > >>> helper to run SEAMCALL on one cpu for each package one by one, and > >>> use it to configure the global KeyID on all packages. > >>> > >>> Intel hardware doesn't guarantee cache coherency across different > >>> KeyIDs. The kernel needs to flush PAMT's dirty cachelines > >>> (associated with KeyID 0) before the TDX module uses the global > >>> KeyID to access the PAMT. Following the TDX module specification, > >>> flush cache before configuring the global KeyID on all packages. > >>> > >>> Given the PAMT size can be large (~1/256th of system RAM), just use > >>> WBINVD on all CPUs to flush. > >>> > >>> Note if any TDH.SYS.KEY.CONFIG fails, the TDX module may already > >>> have used the global KeyID to write any PAMT. Therefore, need to > >>> use WBINVD to flush cache before freeing the PAMTs back to the > >>> kernel. Note using MOVDIR64B (which changes the page's associated > >>> KeyID from the old TDX private KeyID back to KeyID 0, which is used > >>> by the kernel) > >> > >> It seems not accurate to say MOVDIR64B changes the page's associated > KeyID. > >> It just uses the current KeyID for memory operations. > > > > The "write" to the memory changes the page's associated KeyID to the > > KeyID that does the "write". A more accurate expression perhaps > > should be MOVDIR64B + MFENSE, but I think it doesn't matter in changelog. > > Just delete it from the changelog. It's a distraction. I'm not even sure it's > *necessary* to do any memory content conversion after the TDX module has > written gunk. > > There won't be any integrity issues because integrity errors don't do anything for > KeyID-0 (no #MC). > > I _think_ the reads of the page using KeyID-0 will see abort page semantics. > That's *FINE*. I'll remove this from the changelog. Thanks!
diff --git a/arch/x86/virt/vmx/tdx/tdx.c b/arch/x86/virt/vmx/tdx/tdx.c index 3a032930e58a..99d1be5941a7 100644 --- a/arch/x86/virt/vmx/tdx/tdx.c +++ b/arch/x86/virt/vmx/tdx/tdx.c @@ -224,6 +224,46 @@ static void seamcall_on_each_cpu(struct seamcall_ctx *sc) on_each_cpu(seamcall_smp_call_function, sc, true); } +/* + * Call one SEAMCALL on one (any) cpu for each physical package in + * serialized way. Return immediately in case of any error if + * SEAMCALL fails on any cpu. + * + * Note for serialized calls 'struct seamcall_ctx::err' doesn't have + * to be atomic, but for simplicity just reuse it instead of adding + * a new one. + */ +static int seamcall_on_each_package_serialized(struct seamcall_ctx *sc) +{ + cpumask_var_t packages; + int cpu, ret = 0; + + if (!zalloc_cpumask_var(&packages, GFP_KERNEL)) + return -ENOMEM; + + for_each_online_cpu(cpu) { + if (cpumask_test_and_set_cpu(topology_physical_package_id(cpu), + packages)) + continue; + + ret = smp_call_function_single(cpu, seamcall_smp_call_function, + sc, true); + if (ret) + break; + + /* + * Doesn't have to use atomic_read(), but it doesn't + * hurt either. + */ + ret = atomic_read(&sc->err); + if (ret) + break; + } + + free_cpumask_var(packages); + return ret; +} + static int tdx_module_init_cpus(void) { struct seamcall_ctx sc = { .fn = TDH_SYS_LP_INIT }; @@ -1010,6 +1050,22 @@ static int config_tdx_module(struct tdmr_info *tdmr_array, int tdmr_num, return ret; } +static int config_global_keyid(void) +{ + struct seamcall_ctx sc = { .fn = TDH_SYS_KEY_CONFIG }; + + /* + * Configure the key of the global KeyID on all packages by + * calling TDH.SYS.KEY.CONFIG on all packages in a serialized + * way as it cannot run concurrently on different CPUs. + * + * TDH.SYS.KEY.CONFIG may fail with entropy error (which is + * a recoverable error). Assume this is exceedingly rare and + * just return error if encountered instead of retrying. + */ + return seamcall_on_each_package_serialized(&sc); +} + /* * Detect and initialize the TDX module. * @@ -1098,15 +1154,44 @@ static int init_tdx_module(void) if (ret) goto out_free_pamts; + /* + * Hardware doesn't guarantee cache coherency across different + * KeyIDs. The kernel needs to flush PAMT's dirty cachelines + * (associated with KeyID 0) before the TDX module can use the + * global KeyID to access the PAMT. Given PAMTs are potentially + * large (~1/256th of system RAM), just use WBINVD on all cpus + * to flush the cache. + * + * Follow the TDX spec to flush cache before configuring the + * global KeyID on all packages. + */ + wbinvd_on_all_cpus(); + + /* Config the key of global KeyID on all packages */ + ret = config_global_keyid(); + if (ret) + goto out_free_pamts; + /* * Return -EINVAL until all steps of TDX module initialization * process are done. */ ret = -EINVAL; out_free_pamts: - if (ret) + if (ret) { + /* + * Part of PAMT may already have been initialized by + * TDX module. Flush cache before returning PAMT back + * to the kernel. + * + * Note there's no need to do MOVDIR64B (which changes + * the page's associated KeyID from the old TDX private + * KeyID back to KeyID 0, which is used by the kernel), + * as KeyID 0 doesn't support integrity check. + */ + wbinvd_on_all_cpus(); tdmrs_free_pamt_all(tdmr_array, tdmr_num); - else + } else pr_info("%lu pages allocated for PAMT.\n", tdmrs_count_pamt_pages(tdmr_array, tdmr_num)); out_free_tdmrs: diff --git a/arch/x86/virt/vmx/tdx/tdx.h b/arch/x86/virt/vmx/tdx/tdx.h index c26bab2555ca..768d097412ab 100644 --- a/arch/x86/virt/vmx/tdx/tdx.h +++ b/arch/x86/virt/vmx/tdx/tdx.h @@ -15,6 +15,7 @@ /* * TDX module SEAMCALL leaf functions */ +#define TDH_SYS_KEY_CONFIG 31 #define TDH_SYS_INFO 32 #define TDH_SYS_INIT 33 #define TDH_SYS_LP_INIT 35