Kernel KVM virtualization development
 help / color / mirror / Atom feed
From: "Edgecombe, Rick P" <rick.p.edgecombe@intel.com>
To: "kvm@vger.kernel.org" <kvm@vger.kernel.org>,
	"linux-coco@lists.linux.dev" <linux-coco@lists.linux.dev>,
	"linux-kernel@vger.kernel.org" <linux-kernel@vger.kernel.org>,
	"Gao, Chao" <chao.gao@intel.com>
Cc: "Xu, Yilun" <yilun.xu@intel.com>,
	"dave.hansen@linux.intel.com" <dave.hansen@linux.intel.com>,
	"baolu.lu@linux.intel.com" <baolu.lu@linux.intel.com>,
	"Peng, Chao P" <chao.p.peng@intel.com>,
	"bp@alien8.de" <bp@alien8.de>,
	"mingo@redhat.com" <mingo@redhat.com>,
	"x86@kernel.org" <x86@kernel.org>,
	"tglx@kernel.org" <tglx@kernel.org>,
	"Tian, Kevin" <kevin.tian@intel.com>,
	"hpa@zytor.com" <hpa@zytor.com>,
	"kas@kernel.org" <kas@kernel.org>
Subject: Re: [RFC PATCH 1/7] coco/tdx-host: Create metadata for an SPDM session
Date: Sat, 21 Nov 2026 02:05:50 +0000	[thread overview]
Message-ID: <ae111331c1b71ffdc1875ae25f8079e7c4bebd69.camel@intel.com> (raw)
In-Reply-To: <20261116111037.3478844-2-chao.gao@intel.com>

On Mon, 2026-11-16 at 03:10 -0800, Chao Gao wrote:
> The TDX module talks to the device over a Security Protocol and Data
> Model (SPDM) session. It needs host memory for each session's metadata,
> and reports the number of pages required in its global metadata.

Since we usually talk about metadata and mean the global metadata, we will
probably need to explain what session metadata is. Is it just data?

> 
> When setting up an SPDM session, allocate the memory and hand it to the
> TDX module with TDH.SPDM.CREATE. Keep the returned session ID for later
> SEAMCALLs.
> 

This is getting to be a pretty familiar concept. TDX needs per-something scoped
memory. Give it to the TDX module per some seamcall. Can we make it sounds
especially ordinary and then just call out any thing that is different?

>  When tearing the session down, reclaim the memory with
> TDH.SPDM.DELETE. If that fails, leak the memory, as the TDX module may
> still be using it.

When would it fail?

> 
> Based on work by Yilun and Zhenzhong.
> 
> Signed-off-by: Chao Gao <chao.gao@intel.com>
> ---
>  arch/x86/include/asm/tdx_global_metadata.h  |   1 +
>  arch/x86/virt/vmx/tdx/tdx_global_metadata.c |   6 ++
>  drivers/virt/coco/tdx-host/main.c           | 104 +++++++++++++++++++-
>  3 files changed, 109 insertions(+), 2 deletions(-)
> 
> diff --git a/arch/x86/include/asm/tdx_global_metadata.h b/arch/x86/include/asm/tdx_global_metadata.h
> index e5e54159ad36..537e872eeb0e 100644
> --- a/arch/x86/include/asm/tdx_global_metadata.h
> +++ b/arch/x86/include/asm/tdx_global_metadata.h
> @@ -52,6 +52,7 @@ struct tdx_sys_info_ext {
>  };
>  
>  struct tdx_sys_info_connect {
> +	u16 spdm_mt_page_count;
>  	u16 iommu_mt_page_count;
>  };
>  
> diff --git a/arch/x86/virt/vmx/tdx/tdx_global_metadata.c b/arch/x86/virt/vmx/tdx/tdx_global_metadata.c
> index 9b46f34cb469..f20e029f240b 100644
> --- a/arch/x86/virt/vmx/tdx/tdx_global_metadata.c
> +++ b/arch/x86/virt/vmx/tdx/tdx_global_metadata.c
> @@ -144,6 +144,12 @@ static __init int get_tdx_sys_info_connect(struct tdx_sys_info_connect *sysinfo_
>  	int ret;
>  	u64 val;
>  
> +	ret = read_sys_metadata_field(0x3000000100000002, &val);
> +	if (ret)
> +		return ret;
> +
> +	sysinfo_connect->spdm_mt_page_count = val;
> +

We'll hopefully see this hunk get updated once the metadata cleanup series
lands.

>  	ret = read_sys_metadata_field(0x3000000100000003, &val);
>  	if (ret)
>  		return ret;
> diff --git a/drivers/virt/coco/tdx-host/main.c b/drivers/virt/coco/tdx-host/main.c
> index a815dbe5127a..919e77ae2326 100644
> --- a/drivers/virt/coco/tdx-host/main.c
> +++ b/drivers/virt/coco/tdx-host/main.c
> @@ -5,6 +5,7 @@
>   * Copyright (C) 2025 Intel Corporation
>   */
>  
> +#include <linux/bitfield.h>
>  #include <linux/device/faux.h>
>  #include <linux/firmware.h>
>  #include <linux/module.h>
> @@ -26,8 +27,30 @@ static const struct x86_cpu_id tdx_host_ids[] = {
>  };
>  MODULE_DEVICE_TABLE(x86cpu, tdx_host_ids);
>  
> +#define TDISP_FUNC_ID		GENMASK(15, 0)
> +#define TDISP_FUNC_ID_SEGMENT		GENMASK(23, 16)
> +#define TDISP_FUNC_ID_SEG_VALID		BIT(24)
> +
> +static inline u32 tdisp_func_id(struct pci_dev *pdev)
> +{
> +	u32 func_id;
> +
> +	func_id = FIELD_PREP(TDISP_FUNC_ID_SEGMENT, pci_domain_nr(pdev->bus));
> +	if (func_id)
> +		func_id |= TDISP_FUNC_ID_SEG_VALID;
> +	func_id |= FIELD_PREP(TDISP_FUNC_ID,
> +			      PCI_DEVID(pdev->bus->number, pdev->devfn));
> +
> +	return func_id;
> +}

Is this TDISP specific encoding or a TDX-TDISP specific encoding? I think TDISP.
There isn't a more general place to put it?

> +
>  struct tdx_tsm_link {
>  	struct pci_tsm_pf0 pci;
> +	u32 func_id;
> +
> +	u64 spdm_id;
> +	unsigned int spdm_mt_nr_pages;
> +	void *spdm_mt;
>  };
>  
>  static struct tdx_tsm_link *to_tdx_tsm_link(struct pci_tsm *tsm)
> @@ -35,21 +58,97 @@ static struct tdx_tsm_link *to_tdx_tsm_link(struct pci_tsm *tsm)
>  	return container_of(tsm, struct tdx_tsm_link, pci.base_tsm);
>  }
>  
> +static int tdx_spdm_create(struct tdx_tsm_link *tlink)
> +{
> +	const struct tdx_sys_info *tdx_sysinfo = tdx_get_sysinfo();
> +	struct tdx_hpa_list_info info;
> +	unsigned int nr_pages;
> +	u64 spdm_id, sret;
> +	void *spdm_mt;
> +	int ret;
> +
> +	if (!tdx_sysinfo)
> +		return -ENXIO;
> +
> +	nr_pages = tdx_sysinfo->tdx_connect.spdm_mt_page_count;
> +
> +	spdm_mt = alloc_pages_exact(nr_pages * PAGE_SIZE,
> +				    GFP_KERNEL | __GFP_ZERO);

Why zero?

> +	if (!spdm_mt)
> +		return -ENOMEM;
> +
> +	ret = tdx_hpa_list_info_setup(&info, spdm_mt, nr_pages);
> +	if (ret)
> +		goto out_free_spdm_mt;
> +
> +	sret = tdh_spdm_create(tlink->func_id, &info, &spdm_id);
> +
> +	tdx_hpa_list_info_free(&info);
> +
> +	if (sret) {
> +		ret = -EIO;
> +		goto out_free_spdm_mt;
> +	}
> +
> +	tlink->spdm_id = spdm_id;
> +	tlink->spdm_mt = spdm_mt;
> +	tlink->spdm_mt_nr_pages = nr_pages;
> +	return 0;
> +
> +out_free_spdm_mt:
> +	free_pages_exact(spdm_mt, nr_pages * PAGE_SIZE);
> +	return ret;
> +}
> +
> +static void tdx_spdm_delete(struct tdx_tsm_link *tlink)
> +{
> +	struct pci_dev *pdev = tlink->pci.base_tsm.pdev;
> +	u64 sret;
> +
> +	sret = tdh_spdm_delete(tlink->spdm_id);
> +	if (sret) {
> +		/* leak the metadata pages */
> +		pci_err(pdev, "fail to delete spdm 0x%llx\n", sret);
> +		return;
> +	}
> +
> +	free_pages_exact(tlink->spdm_mt, tlink->spdm_mt_nr_pages * PAGE_SIZE);
> +}
> +
> +static int tdx_spdm_session_setup(struct tdx_tsm_link *tlink)
> +{
> +	return tdx_spdm_create(tlink);

Hmm, its not clear from this patch why we would need so many levels of wrapper
for the setup/teardown seamcall. But it grows in later patches. But it looks
weird in this patch and will probably throw off the tip review.

> +}
> +
> +static void tdx_spdm_session_teardown(struct tdx_tsm_link *tlink)
> +{
> +	tdx_spdm_delete(tlink);
> +}
> +
>  static int tdx_tsm_link_connect(struct pci_dev *pdev)
>  {
> +	struct tdx_tsm_link *tlink = to_tdx_tsm_link(pdev->tsm);
>  	int ret;
>  
>  	ret = tdx_iommu_get(pdev);
>  	if (ret)
>  		return ret;
>  
> -	/* TDISP connect is not implemented yet. */
> +	ret = tdx_spdm_session_setup(tlink);
> +	if (ret)
> +		goto out_iommu_put;
> +	return 0;
> +
> +out_iommu_put:
>  	tdx_iommu_put(pdev);
> -	return -ENXIO;
> +	return ret;
>  }
>  
>  static void tdx_tsm_link_disconnect(struct pci_dev *pdev)
>  {
> +	struct tdx_tsm_link *tlink = to_tdx_tsm_link(pdev->tsm);
> +
> +	tdx_spdm_session_teardown(tlink);
>  	tdx_iommu_put(pdev);
>  }
>  
> @@ -66,6 +165,7 @@ static struct pci_tsm *tdx_tsm_link_pf0_probe(struct tsm_dev *tsm_dev,
>  	if (rc)
>  		return NULL;
>  
> +	tlink->func_id = tdisp_func_id(pdev);

Why is this here instead of tdx_spdm_create()?

>  	return &no_free_ptr(tlink)->pci.base_tsm;
>  }
>  


  reply	other threads:[~2026-11-21  2:06 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-16 11:10 [RFC PATCH 0/7] TDX: Enable PCIe link encryption for trusted I/O Chao Gao
2026-11-16 11:10 ` [RFC PATCH 1/7] coco/tdx-host: Create metadata for an SPDM session Chao Gao
2026-11-21  2:05   ` Edgecombe, Rick P [this message]
2026-11-16 11:10 ` [RFC PATCH 2/7] coco/tdx-host: Establish an SPDM session with the device Chao Gao
2026-11-16 11:10 ` [RFC PATCH 3/7] coco/tdx-host: Exchange SPDM messages through DOE Chao Gao
2026-11-16 11:10 ` [RFC PATCH 4/7] coco/tdx-host: Pass a device info buffer to tdh_spdm_connect() Chao Gao
2026-11-16 11:10 ` [RFC PATCH 5/7] coco/tdx-host: Configure an IDE stream Chao Gao
2026-11-16 11:10 ` [RFC PATCH 6/7] coco/tdx-host: Program IDE keys and enable the " Chao Gao
2026-11-16 11:10 ` [RFC PATCH 7/7] x86/virt/tdx: Enable PCIe link encryption Chao Gao
2026-11-21  1:35 ` [RFC PATCH 0/7] TDX: Enable PCIe link encryption for trusted I/O Edgecombe, Rick P

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=ae111331c1b71ffdc1875ae25f8079e7c4bebd69.camel@intel.com \
    --to=rick.p.edgecombe@intel.com \
    --cc=baolu.lu@linux.intel.com \
    --cc=bp@alien8.de \
    --cc=chao.gao@intel.com \
    --cc=chao.p.peng@intel.com \
    --cc=dave.hansen@linux.intel.com \
    --cc=hpa@zytor.com \
    --cc=kas@kernel.org \
    --cc=kevin.tian@intel.com \
    --cc=kvm@vger.kernel.org \
    --cc=linux-coco@lists.linux.dev \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mingo@redhat.com \
    --cc=tglx@kernel.org \
    --cc=x86@kernel.org \
    --cc=yilun.xu@intel.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox