From: Baolu Lu <baolu.lu@linux.intel.com>
To: Xu Yilun <yilun.xu@linux.intel.com>
Cc: baolu.lu@linux.intel.com, iommu@lists.linux.dev, x86@kernel.org,
linux-coco@lists.linux.dev, kvm@vger.kernel.org,
Jason Gunthorpe <jgg@ziepe.ca>, Kevin Tian <kevin.tian@intel.com>,
Kiryl Shutsemau <kas@kernel.org>,
Rick Edgecombe <rick.p.edgecombe@intel.com>,
xiaoyao.li@intel.com, Chao Gao <chao.gao@intel.com>,
Binbin Wu <binbin.wu@linux.intel.com>,
linux-kernel@vger.kernel.org
Subject: Re: [PATCH v3 9/9] coco/tdx-host: Enable trusted DMA on IOMMUs on device connect
Date: Wed, 2 Dec 2026 08:55:08 +0800 [thread overview]
Message-ID: <9584f888-6781-46fc-b6c4-acdbb5eb4480@linux.intel.com> (raw)
In-Reply-To: <awlT8wJYM3q1xMr7@yilunxu-OptiPlex-7050>
On 11/27/2026 5:57 PM, Xu Yilun wrote:
>> static int tdx_tsm_link_connect(struct pci_dev *pdev)
>> {
>> + int ret;
>> +
>> + ret = tdx_iommu_get(pdev);
>> + if (ret)
>> + return ret;
>> +
>> + /* TDISP connect is not implemented yet. */
>> + tdx_iommu_put(pdev);
>
> In TDX, usually we prevent the on-going feature implementation from
> reachable by a stub and finally enable it when full support lands,
> like 1e3d79135327. So can we do like that?
>
> Oh, we've already done this in Patch 7.
Yes. tdx_supports_tdisp() still returns false, so no TSM is registered
and this path can't be reached yet.
>
>> return -ENXIO;
>> }
>
> [...]
>
>> +/* These describe the trusted invalidation queue buffers. */
>> +#define IQ_BUFFER_PAGES 2
>> +#define IQ_BUFFER_SIZE SZ_8K
>
> I don't think we have to define 2 instant values, how about?
>
> #define IQ_BUFFER_SIZE IQ_BUFFER_PAGES * SZ_4K
Agreed. I'll write it with parentheses:
#define IQ_BUFFER_SIZE (IQ_BUFFER_PAGES * SZ_4K)
>> +
>> +/*
>> + * Represents a list of pages for TDX module defined IOMMU_MT object.
>> + * Typically it uses a "root page" as the medium to exchange a list of
>> + * data pages between host and TDX module.
>> + */
>> +struct tdx_iommu_pages {
>> + struct tdx_iommu_mt_param *root;
>> + void **pages;
>> + unsigned int nr_entries;
>> +};
>> +
>> +/**
>> + * struct tdx_iommu - Host state of an IOMMU in Secure TDX Mode
>> + * @node: Link in tdx_iommu_list
>> + * @params: IOMMU information from the VT-d driver
>> + * @tdx_iommu_id: Identifier returned by TDH.IOMMU.SETUP
>> + * @mt_pages: Pages handed to the TDX module by TDH.IOMMU.SETUP
>> + * @users: Number of devices using trusted DMA through this IOMMU
>> + * @stuck: TDH.IOMMU.CLEAR failed. The TDX module may still own
>> + * @mt_pages, so they are leaked and the IOMMU is not reused.
>> + */
>> +struct tdx_iommu {
>> + struct list_head node;
>> + struct dmar_tdxc_params params;
>> + u64 tdx_iommu_id;
>> + struct tdx_iommu_pages *mt_pages;
>> + unsigned int users;
>> + bool stuck;
>> +};
>> +
>> +/*
>> + * Serializes the refcounts together with TDH.IOMMU.SETUP and
>> + * TDH.IOMMU.CLEAR, so a device can't join an IOMMU that is being
>> + * cleared, or one whose setup hasn't finished.
>> + */
>> +static DEFINE_MUTEX(tdx_iommu_lock);
>> +static LIST_HEAD(tdx_iommu_list);
>> +
>> +static void *tdx_iommu_alloc_pages(int node, size_t size)
>> +{
>> + struct folio *folio;
>> +
>> + if (node == NUMA_NO_NODE)
>> + node = numa_mem_id();
>> +
>> + folio = __folio_alloc_node(GFP_KERNEL | __GFP_ZERO, get_order(size), node);
>> + if (!folio)
>> + return NULL;
>> +
>> + return folio_address(folio);
>> +}
>> +
>> +static void tdx_iommu_free_pages(void *virt)
>> +{
>> + if (!virt)
>> + return;
>> +
>> + folio_put(virt_to_folio(virt));
>> +}
>> +
>> +/* Number of 4K pages backing IOMMU_MT entry @i. */
>> +static unsigned int mt_entry_nr_pages(unsigned int i)
>> +{
>> + return i < TDX_IOMMU_MT_IQ_NR ? IQ_BUFFER_SIZE >> PAGE_SHIFT : 1;
>> +}
>> +
>
> [...]
>
>> +static void free_mt_pages(struct tdx_iommu_pages *array)
>> +{
>> + unsigned int i;
>> +
>> + if (IS_ERR_OR_NULL(array))
>> + return;
>> +
>> + mt_pamt_put(array, UINT_MAX);
>> +
>> + for (i = 0; i < array->nr_entries; i++)
>> + tdx_iommu_free_pages(array->pages[i]);
>> +
>> + tdx_iommu_free_pages(array->root);
>
> The root page is just to hold ABI params, doesn't have to be kept until
> we exit TDX mode.
>
>> + kfree(array->pages);
>> + kfree(array);
>
> Maybe we don't need so many objects to hold mt page info, see below...
Right. The TDX module only reads the parameter page during
TDH.IOMMU.SETUP and doesn't take ownership of it. I'll allocate it in
tdx_iommu_setup() as a local with __free() and drop it once the
SEAMCALL returns, whether it succeeds or fails.
>
>> +}
>> +
>> +DEFINE_FREE(free_mt_pages, struct tdx_iommu_pages *, free_mt_pages(_T))
>> +
>> +static void **alloc_mt_pages(unsigned int nr_entries, int node)
>> +{
>> + unsigned int i;
>> + void **pages;
>> + void *vaddr;
>> +
>> + pages = kzalloc_objs(*pages, nr_entries);
>> + if (!pages)
>> + return NULL;
>> +
>> + /* Allocate two contiguous buffers for the invalidation queue. */
>> + pages[0] = tdx_iommu_alloc_pages(node, IQ_BUFFER_SIZE);
>> + if (!pages[0])
>> + goto free_pages;
>> +
>> + pages[1] = tdx_iommu_alloc_pages(node, IQ_BUFFER_SIZE);
>> + if (!pages[1])
>> + goto free_pages;
>> +
>> + /* Allocate the required number of pages for the IOMMU metadata. */
>> + for (i = TDX_IOMMU_MT_IQ_NR; i < nr_entries; i++) {
>> + vaddr = tdx_iommu_alloc_pages(node, SZ_4K);
>> + if (!vaddr)
>> + goto free_pages;
>> + pages[i] = vaddr;
>> + }
>
> I'm wondering if we could make the page allocation/free/populate simpler,
> how about allocating a contiguous page bulk, like:
>
> unsigned int nr_pages = sysinfo->tdx_connect.iommu_mt_page_count +
> TDX_IOMMU_MT_IQ_NR * IQ_BUFFER_PAGES;
> void *iommu_mt_pages;
>
> iommu_mt_pages = alloc_pages_exact_nid(node, nr_pages, ...);
>
> Making all pages contiguous is more than needed but it saves a lot of LOC,
> is it? We don't need to allocate void **pages, maybe we don't even need
> to define struct tdx_iommu_pages.
Fair enough. Let me try this direction and see how many lines it saves.
>
>> +
>> + return pages;
>> +
>> +free_pages:
>> + for (i = 0; i < nr_entries; i++)
>> + tdx_iommu_free_pages(pages[i]);
>> + kfree(pages);
>> +
>> + return NULL;
>> +}
>> +
>> +static void populate_mt_pages(struct tdx_iommu_pages *array)
>> +{
>> + struct tdx_iommu_mt_param *param = array->root;
>> + void **pages = array->pages;
>> + unsigned int i;
>> +
>> + for (i = 0; i < TDX_IOMMU_MT_IQ_NR; i++)
>> + param->iq[i] = __pa(pages[i]) | IQ_BUFFER_PAGES;
>> +
>> + for (; i < array->nr_entries; i++)
>> + param->mt[i - TDX_IOMMU_MT_IQ_NR] = __pa(pages[i]);
>> +}
>> +
>> +/* Allocate the IOMMU_MT pages with PAMT backing. Free with free_mt_pages(). */
>> +static struct tdx_iommu_pages *alloc_iommu_mt(int node, unsigned int nr_mt_pages)
>> +{
>> + unsigned int nr_entries = nr_mt_pages + TDX_IOMMU_MT_IQ_NR;
>> + struct tdx_iommu_pages *array;
>> + unsigned int i;
>> + int ret = -ENOMEM;
>> +
>> + if (!nr_mt_pages || nr_mt_pages > TDX_IOMMU_MT_MAX_PAGES)
>> + return ERR_PTR(-EINVAL);
>> +
>> + array = kzalloc_obj(*array);
>> + if (!array)
>> + return ERR_PTR(-ENOMEM);
>> +
>> + array->root = tdx_iommu_alloc_pages(node, sizeof(*array->root));
>
> The root page is just to hold the ABI parameter, doesn't have to
> allocate per node, is it?
Agreed. A plain kzalloc(PAGE_SIZE) is enough. It's naturally aligned
and only read by the SEAMCALL, so NUMA locality doesn't matter.
>
>> + if (!array->root)
>> + goto free_array;
>> +
>> + array->nr_entries = nr_entries;
>> + array->pages = alloc_mt_pages(nr_entries, node);
>> + if (!array->pages)
>> + goto free_root;
>> +
>> + populate_mt_pages(array);
>> +
>> + ret = mt_pamt_get(array);
>> + if (ret)
>> + goto free_pages;
>> +
>> + return array;
>> +
>> +free_pages:
>> + for (i = 0; i < nr_entries; i++)
>> + tdx_iommu_free_pages(array->pages[i]);
>> + kfree(array->pages);
>> +free_root:
>> + tdx_iommu_free_pages(array->root);
>> +free_array:
>> + kfree(array);
>> + return ERR_PTR(ret);
>> +}
>> +
>> +static struct tdx_iommu *tdx_iommu_find(u64 reg_base_addr)
>> +{
>> + struct tdx_iommu *tiommu;
>> +
>> + lockdep_assert_held(&tdx_iommu_lock);
>> +
>> + list_for_each_entry(tiommu, &tdx_iommu_list, node)
>> + if (tiommu->params.reg_base_addr == reg_base_addr)
>> + return tiommu;
>> +
>> + return NULL;
>> +}
>> +
>> +/* Allocate the IOMMU_MT pages and issue TDH.IOMMU.SETUP. */
>> +static int tdx_iommu_setup(struct pci_dev *pdev, struct tdx_iommu *tiommu)
>> +{
>> + const struct tdx_sys_info *sysinfo = tdx_get_sysinfo();
>> + struct dmar_tdxc_params *params = &tiommu->params;
>> + u64 r, tdx_iommu_id;
>> + int ret;
>> +
>> + if (!sysinfo)
>> + return -ENXIO;
>> +
>> + struct tdx_iommu_pages *mt __free(free_mt_pages) =
>> + alloc_iommu_mt(params->node,
>> + sysinfo->tdx_connect.iommu_mt_page_count);
>> + if (IS_ERR(mt))
>> + return PTR_ERR(mt);
>> +
>> + ret = dmar_tdxc_acquire(pdev, params);
>> + if (ret)
>> + return ret;
>> +
>> + r = tdh_iommu_setup(params->reg_base_addr, mt->root, &tdx_iommu_id);
>> + if (r) {
>> + dmar_tdxc_release(pdev, params);
>> + pr_err("TDH.IOMMU.SETUP failed, regbase 0x%llx status 0x%llx\n",
>> + params->reg_base_addr, r);
>> + return -EIO;
>> + }
>> +
>> + tiommu->tdx_iommu_id = tdx_iommu_id;
>> + tiommu->mt_pages = no_free_ptr(mt);
>> +
>> + pr_info("Trusted DMA initialized, regbase 0x%llx\n", params->reg_base_addr);
>> +
>> + return 0;
>> +}
>> +
>> +/* Issue TDH.IOMMU.CLEAR and release what TDH.IOMMU.SETUP took. */
>> +static int tdx_iommu_clear(struct pci_dev *pdev, struct tdx_iommu *tiommu)
>> +{
>> + u64 r;
>> +
>> + r = tdh_iommu_clear(tiommu->tdx_iommu_id);
>> + if (r) {
>> + pr_err("TDH.IOMMU.CLEAR failed, regbase 0x%llx tdx_iommu_id 0x%llx status 0x%llx\n",
>> + tiommu->params.reg_base_addr, tiommu->tdx_iommu_id, r);
>> + /* The pages and their PAMT backing stay with the TDX module. */
>> + return -EIO;
>> + }
>> +
>> + dmar_tdxc_release(pdev, &tiommu->params);
>> + free_mt_pages(tiommu->mt_pages);
>> + tiommu->mt_pages = NULL;
>> +
>> + return 0;
>> +}
>> +
>> +/*
>> + * tdx_iommu_get() - Enable trusted DMA on the IOMMU translating a device
>> + * @pdev: PCI device that will use trusted DMA
>> + *
>> + * The first device behind an IOMMU transitions that IOMMU into Secure
>> + * TDX Mode. Later devices only take a reference. Every successful call
>> + * must be balanced by tdx_iommu_put().
>> + *
>> + * Return: 0 on success and error number on failure.
>> + */
>> +int tdx_iommu_get(struct pci_dev *pdev)
>> +{
>> + struct dmar_tdxc_params params;
>> + struct tdx_iommu *tiommu;
>> + int ret;
>> +
>> + ret = dmar_tdxc_get_params(pdev, ¶ms);
>> + if (ret)
>> + return ret;
>> +
>> + guard(mutex)(&tdx_iommu_lock);
>> +
>> + tiommu = tdx_iommu_find(params.reg_base_addr);
>> + if (tiommu) {
>> + if (tiommu->stuck)
>> + return -EBUSY;
>> + if (WARN_ON(tiommu->users == UINT_MAX))
>> + return -EOVERFLOW;
>> + tiommu->users++;
>> + return 0;
>> + }
>> +
>> + tiommu = kzalloc_obj(*tiommu);
>> + if (!tiommu)
>> + return -ENOMEM;
>> +
>> + tiommu->params = params;
>> + ret = tdx_iommu_setup(pdev, tiommu);
>> + if (ret) {
>> + kfree(tiommu);
>> + return ret;
>> + }
>> +
>> + tiommu->users = 1;
>> + list_add(&tiommu->node, &tdx_iommu_list);
>> +
>> + return 0;
>> +}
>> +
>> +/*
>> + * tdx_iommu_put() - Drop a reference taken by tdx_iommu_get()
>> + * @pdev: Same device passed to tdx_iommu_get()
>> + *
>> + * The last device behind an IOMMU takes that IOMMU out of Secure TDX
>
> I'm wondering if we really needs to take the IOMMU out of TDX mode when
> all devices are disconnected. Imagine people connect a device,
> disconnect and reconnect because of link error, why bother switch TDX
> mode and reserve/release DomainIDs again and again?
> > Previously we setup TDX mode for all IOMMUs at tdx-host probe, that's
> not a bad idea except quite a waste of resouces (e.g. DomainID). There
> may even be no TSM capable device plugged.
>
> Now we setup TDX mode per IOMMU on-demand, that solves the concern. We
> don't really need runtime switch off. We don't have to let another device
> gets a big DID then fail the reconnect.
>
> How about we only exit TDX mode for all IOMMUs on module unload, can
> that simplify some logic like user count, stuck? and locking? There will
> be no connect after we unregister TSM.
Good point, and the domain ID argument is a strong one. With the
current scheme, once the last device disconnects, the host may hand out
a domain ID from the upper half. The next connect then fails in
dmar_tdx_acquire() until that domain goes away. A device that
reconnects after a link error would hit exactly that. I will take this
approach in the next version.
Thanks,
baolu
prev parent reply other threads:[~2026-12-02 0:55 UTC|newest]
Thread overview: 47+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-12 4:30 [PATCH v3 0/9] coco/tdx-host: Introduce trusted DMA initialization support Lu Baolu
2026-11-12 4:30 ` [PATCH v3 1/9] x86/virt/tdx: Add SEAMCALL wrappers for IOMMU setup/clear Lu Baolu
2026-11-18 11:02 ` Binbin Wu
2026-11-23 4:03 ` Baolu Lu
2026-11-23 9:55 ` Xu Yilun
2026-11-24 5:46 ` Baolu Lu
2026-11-24 8:07 ` Tian, Kevin
2026-11-25 2:47 ` Baolu Lu
2026-11-25 3:08 ` Tian, Kevin
2026-11-25 4:26 ` Baolu Lu
2026-11-12 4:30 ` [PATCH v3 2/9] x86/virt/tdx: Read global metadata for trusted IOMMU Lu Baolu
2026-11-18 13:39 ` Binbin Wu
2026-11-23 4:09 ` Baolu Lu
2026-11-24 8:13 ` Tian, Kevin
2026-11-25 2:57 ` Baolu Lu
2026-11-12 4:30 ` [PATCH v3 3/9] x86/virt/tdx: Export Dynamic PAMT helpers to tdx-host Lu Baolu
2026-11-23 10:25 ` Xu Yilun
2026-11-23 16:28 ` Edgecombe, Rick P
2026-11-24 6:07 ` Baolu Lu
2026-11-12 4:30 ` [PATCH v3 4/9] iommu/vt-d: Add interface for tdx-host when enabling trusted DMA Lu Baolu
2026-11-23 15:08 ` Xu Yilun
2026-11-24 7:47 ` Baolu Lu
2026-11-25 3:32 ` Tian, Kevin
2026-11-25 3:29 ` Tian, Kevin
2026-11-25 23:33 ` Baolu Lu
[not found] ` <9a92bb91-3dac-4fc5-8f38-130b7442bdcc@linux.intel.com>
2026-11-27 7:59 ` Tian, Kevin
2026-12-02 0:22 ` Baolu Lu
2026-11-12 4:30 ` [PATCH v3 5/9] iommu/vt-d: Block GCMD and ECMD while acquired by the TDX module Lu Baolu
2026-11-25 3:49 ` Tian, Kevin
2026-11-26 2:10 ` Baolu Lu
2026-11-12 4:30 ` [PATCH v3 6/9] iommu/vt-d: Refuse system suspend " Lu Baolu
2026-11-25 4:01 ` Tian, Kevin
2026-11-25 6:27 ` Baolu Lu
2026-11-12 4:30 ` [PATCH v3 7/9] coco/tdx-host: Add skeleton to support PCI link encryption Lu Baolu
2026-11-18 7:16 ` Chao Gao
2026-11-23 3:30 ` Baolu Lu
2026-11-23 15:26 ` Xu Yilun
2026-11-25 2:24 ` Baolu Lu
2026-11-19 2:52 ` Binbin Wu
2026-11-23 4:18 ` Baolu Lu
2026-11-23 15:43 ` Xu Yilun
2026-11-12 4:30 ` [PATCH v3 8/9] coco/tdx-host: Rename tdx-host.c to main.c Lu Baolu
2026-11-12 4:30 ` [PATCH v3 9/9] coco/tdx-host: Enable trusted DMA on IOMMUs on device connect Lu Baolu
2026-11-18 8:11 ` Chao Gao
2026-11-23 4:51 ` Baolu Lu
2026-11-27 9:57 ` Xu Yilun
2026-12-02 0:55 ` Baolu Lu [this message]
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=9584f888-6781-46fc-b6c4-acdbb5eb4480@linux.intel.com \
--to=baolu.lu@linux.intel.com \
--cc=binbin.wu@linux.intel.com \
--cc=chao.gao@intel.com \
--cc=iommu@lists.linux.dev \
--cc=jgg@ziepe.ca \
--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=rick.p.edgecombe@intel.com \
--cc=x86@kernel.org \
--cc=xiaoyao.li@intel.com \
--cc=yilun.xu@linux.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