RE: [PATCH v3 5/9] iommu/vt-d: Block GCMD and ECMD while acquired by the TDX module

From: Tian, Kevin

Date: Tue Dec 01 2026 - 23:57:47 EST


> From: Baolu Lu <baolu.lu@xxxxxxxxxxxxxxx>
> Sent: Thursday, November 26, 2026 10:11 AM
>
> On 11/25/2026 11:49 AM, Tian, Kevin wrote:
> >> From: Lu Baolu <baolu.lu@xxxxxxxxxxxxxxx>
> >> Sent: Thursday, November 12, 2026 12:31 PM
> >>
> >> + * context (e.g. perf pmu::start/stop), so don't sleep; just reject
> >> + * the request.
> >> + */
> >> + if (iommu->tdxc_acquired) {
> >> + ret = -EBUSY;
> >> + goto err;
> >> + }
> >> +
> >
> > iommu_pmu_stop() doesn't check the return value of this. It's a void
> > function. So returning error here still breaks the perf feature.
> >
> > we may need a way to disable iommu perf completely in the acquire
> > function.
>
> Good catch. Returning an error there leaves events that look like
> they're running but never count, or that can't be stopped.
>
> I'll make perf events and TDX mode mutually exclusive instead:
>
> - The iommu pmu counts its live events under register_lock.
> - event_init() fails with -EBUSY while the IOMMU is acquired.
> - acquire() fails with -EBUSY while any event exists.
>
> Something like the following(compiled but not tested). Does this work
> for you?

we could simply unregister iommu pmu at acquisition time? that makes
the entire capability disappeared when tdx is acquired, clearer than
presenting a capability w/o any usable counters...

>
> diff --git a/drivers/iommu/intel/iommu.h b/drivers/iommu/intel/iommu.h
> index 1bbfb1b6f905..0945e63c901a 100644
> --- a/drivers/iommu/intel/iommu.h
> +++ b/drivers/iommu/intel/iommu.h
> @@ -674,6 +674,7 @@ struct iommu_pmu {
> struct pmu pmu;
> DECLARE_BITMAP(used_mask, IOMMU_PMU_IDX_MAX);
> struct perf_event *event_list[IOMMU_PMU_IDX_MAX];
> + unsigned int nr_events; /* Protected by iommu-
> >register_lock */
> unsigned char irq_name[16];
> };
>
> diff --git a/drivers/iommu/intel/dmar.c b/drivers/iommu/intel/dmar.c
> index f0ac8c433198..1ae27198b999 100644
> --- a/drivers/iommu/intel/dmar.c
> +++ b/drivers/iommu/intel/dmar.c
> @@ -2588,10 +2588,15 @@ static void dmar_tdxc_unreserve_did(struct
> intel_iommu *iommu)
> }
>
> /* GCMD and ECMD writers complete under register_lock, so none is in
> flight. */
> -static void dmar_tdxc_set_acquired(struct intel_iommu *iommu, bool
> acquired)
> +static int dmar_tdxc_set_acquired(struct intel_iommu *iommu, bool
> acquired)
> {
> guard(raw_spinlock_irqsave)(&iommu->register_lock);
> + /* Perf issues enhanced commands at runtime and can't handle
> failure. */
> + if (acquired && iommu->pmu && iommu->pmu->nr_events)
> + return -EBUSY;
> +
> iommu->tdx_acquired = acquired;
> + return 0;
> }
>
> static struct dmar_drhd_unit *dmar_tdxc_find_drhd(struct pci_dev *pdev)
> @@ -2677,9 +2682,11 @@ int dmar_tdx_acquire(struct pci_dev *pdev)
> if (ret)
> return ret;
>
> - dmar_tdxc_set_acquired(iommu, true);
> + ret = dmar_tdxc_set_acquired(iommu, true);
> + if (ret)
> + dmar_tdxc_unreserve_did(iommu);
>
> - return 0;
> + return ret;
> }
> EXPORT_SYMBOL_FOR_MODULES(dmar_tdx_acquire, "tdx-host");
>
> diff --git a/drivers/iommu/intel/iommu.c b/drivers/iommu/intel/iommu.c
> index 6dbb421d8099..c810b4776fd7 100644
> --- a/drivers/iommu/intel/iommu.c
> +++ b/drivers/iommu/intel/iommu.c
> @@ -4358,12 +4358,8 @@ int ecmd_submit_sync(struct intel_iommu
> *iommu,
> u8 ecmd, u64 oa, u64 ob)
>
> raw_spin_lock_irqsave(&iommu->register_lock, flags);
>
> - /*
> - * The TDX module owns this IOMMU. This may be called from
> atomic
> - * context (e.g. perf pmu::start/stop), so don't sleep; just reject
> - * the request.
> - */
> - if (iommu->tdx_acquired) {
> + /* Perf, the only caller, is kept off IOMMUs acquired for TDX mode. */
> + if (WARN_ON_ONCE(iommu->tdx_acquired)) {
> ret = -EBUSY;
> goto err;
> }
> diff --git a/drivers/iommu/intel/perfmon.c b/drivers/iommu/intel/perfmon.c
> index e40ed253eaa9..e4df1aee4fcd 100644
> --- a/drivers/iommu/intel/perfmon.c
> +++ b/drivers/iommu/intel/perfmon.c
> @@ -276,9 +276,19 @@ static int iommu_pmu_validate_group(struct
> perf_event *event)
> return 0;
> }
>
> +static void iommu_pmu_event_destroy(struct perf_event *event)
> +{
> + struct iommu_pmu *iommu_pmu = iommu_event_to_pmu(event);
> +
> + guard(raw_spinlock_irqsave)(&iommu_pmu->iommu->register_lock);
> + iommu_pmu->nr_events--;
> +}
> +
> static int iommu_pmu_event_init(struct perf_event *event)
> {
> struct hw_perf_event *hwc = &event->hw;
> + struct iommu_pmu *iommu_pmu;
> + int ret;
>
> if (event->attr.type != event->pmu->type)
> return -ENOENT;
> @@ -295,7 +305,20 @@ static int iommu_pmu_event_init(struct perf_event
> *event)
>
> hwc->config = iommu_event_config(event);
>
> - return iommu_pmu_validate_group(event);
> + ret = iommu_pmu_validate_group(event);
> + if (ret)
> + return ret;
> +
> + iommu_pmu = iommu_event_to_pmu(event);
> + guard(raw_spinlock_irqsave)(&iommu_pmu->iommu->register_lock);
> + /* The enhanced commands that perf relies on are blocked in TDX
> mode. */
> + if (iommu_pmu->iommu->tdx_acquired)
> + return -EBUSY;
> +
> + iommu_pmu->nr_events++;
> + event->destroy = iommu_pmu_event_destroy;
> +
> + return 0;
> }
>
> static void iommu_pmu_event_update(struct perf_event *event)
> --
> 2.53.0
>
> Thanks,
> baolu