From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=198.175.65.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795649625; b=IxxqVfpW4YQ4OdLFee0it8FRH3966HS/KlMZUnmS5HWV43lF4zO8HGJh6YWkyeY34Iwx ap6QmoTn7qDkxD0o1zPe86hJ+Nr60uaHhPWGZ2kTZX8w9TXiKNqE34fsx6bdQFyT7FR8l l7EmRbQrIRHWFXLj7lsclSRBGfwA4gOvHn/4uX6brOjubJoQadJzjh+pyKyE7ee3d2n9k RPQ/l9/MRI7pqmgfHPNM1iaDnmrQkfOWxmGpWqu2fkBMveJUHMt/wVEzWpovvsZHwsFkM sqzkFHFtAwTG6KiN2/jfqQaZ5pRWkYnu4yCA7JJ8CkK906wScHVH+fEgRX3A3Tsct9g== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795649625; h=DMARC-Filter:DKIM-Signature:X-CSE-ConnectionGUID:X-CSE-MsgGUID: X-IronPort-AV:X-IronPort-AV:Received:X-CSE-ConnectionGUID: X-CSE-MsgGUID:X-ExtLoop1:X-IronPort-AV:Received:Message-ID:Date: MIME-Version:User-Agent:From:Subject:To:Cc:References: Content-Language:In-Reply-To:Content-Type:Content-Transfer-Encoding; bh=b2ogpMRdOGvQOxnTYrtAYFBvprrsM3s8fDuelvc9zDg=; b=feXH8834BeNB/RMerJ4dxQmi8fkApqUs0hSHtvCsD6xgG4VXnidfmZyZ4l5cO7mRMqcb v14QdskTg6HrKkrBiWuTNpGRHGxF2ArMReOc4qzCFEcfFscPxieDXwtAlw47/xP+CKCj7 qQu+zmy/4H0Sv+1nVSb5Yn554bYoOQEY1DbS3AtdotToOkz/XSjI9JaJbqw0rAFkx7QB4 8ctDfToVYJ6qYFLMS0vrQPoIYuA1ION4dfxvvuSKFCmvwRew3gYftcks0LD9fe4y3SF98 lauDE6etiMvDbYJke5K1wf/dJD0pEGTMpogabnSSHSV5KMMwSGTY1rbC/H4ZkBk0o2A== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=linux.intel.com; spf=pass smtp.mailfrom=linux.intel.com; dkim=pass header.d=intel.com header.i=@intel.com header.a=rsa-sha256 header.s=Intel header.b=QCuwMB17; arc=none smtp.remote-ip=198.175.65.18 Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.18]) (using TLSv1.2 with cipher ECDHE-ECDSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 68FA4606A31E; Wed, 25 Nov 2026 23:33:45 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 68FA4606A31E Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.a=rsa-sha256 header.s=Intel header.b=QCuwMB17 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1795649625; x=1827185625; h=message-id:date:mime-version:from:subject:to:cc: references:in-reply-to:content-transfer-encoding; bh=pCVtF8v55FmaexWS2gyF6Vx3HQfifyvY5lhhfzgms4c=; b=QCuwMB17XM/COhqSiU3QLiaqeetPk+1rdpVNO1JNMstBMmymYMN8wkBo yDStQ3InSPPlcLi1yTD536Ndgis4UAvYEMShd5XN9mbWVOBx5m6Xbvp+k VLd4Ym+a9rlQuF8U8LcazgGt9NaXzrihWMaFLE8uLiylZuWjXcftOUKoh bsCid8rNzdrDTNL56L7NCDwALmy1NC90iHlsjw9/Slo7fBYY7kWf1cF54 OglOYNRNkjYK5jfynFLhaXkanYbOOIq6gA2d83HIJu18MPqEI0AgFvtwK 67ukGINMSN2AO6u8vFGrWrMMqjdx16aUNhNaj4msxStHWaH+LYLgbAHvc g==; X-CSE-ConnectionGUID: mugdgGKfQM6LUCgx4S5qUQ== X-CSE-MsgGUID: TeylpNvZR06ME7mnka6YqQ== X-IronPort-AV: E=McAfee;i="6800,10657,11977"; a="4555517" X-IronPort-AV: E=Sophos;i="6.27,233,1787036400"; d="scan'208";a="4555517" Received: from fmviesa011.fm.intel.com ([10.60.135.151]) by orvoesa110.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 25 Nov 2026 15:33:45 -0800 X-CSE-ConnectionGUID: sAWRZUW0RhOM423J0jnZyw== X-CSE-MsgGUID: Qc2Duxs7RI+1Nhvs8BdKqw== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.27,233,1787036400"; d="scan'208";a="13784609" Received: from unknown (HELO [10.238.1.144]) ([10.238.1.144]) by smtpauth.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 25 Nov 2026 15:33:41 -0800 Message-ID: Date: Thu, 26 Nov 2026 07:33:39 +0800 X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 User-Agent: Mozilla Thunderbird From: Baolu Lu Subject: Re: [PATCH v3 4/9] iommu/vt-d: Add interface for tdx-host when enabling trusted DMA To: "Tian, Kevin" , "iommu@lists.linux.dev" , "x86@kernel.org" , "linux-coco@lists.linux.dev" , "kvm@vger.kernel.org" Cc: baolu.lu@linux.intel.com, Jason Gunthorpe , Kiryl Shutsemau , "Edgecombe, Rick P" , "yilun.xu@linux.intel.com" , "Li, Xiaoyao" , "Gao, Chao" , Binbin Wu , "linux-kernel@vger.kernel.org" References: <20261112043054.2664857-1-baolu.lu@linux.intel.com> <20261112043054.2664857-5-baolu.lu@linux.intel.com> Content-Language: en-US In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit On 11/25/2026 11:29 AM, Tian, Kevin wrote: >> From: Lu Baolu >> Sent: Thursday, November 12, 2026 12:31 PM >> >> An Intel IOMMU that supports trusted DMA (ECAP.TDXCS) provides a second >> set of DMA translation resources, managed by the TDX module and used >> only for devices assigned to TDs. Before they can be used, the tdx-host >> driver issues TDH.IOMMU.SETUP to put the IOMMU into Secure TDX Mode, >> and >> issues TDH.IOMMU.CLEAR to take it out again. >> >> The VT-d driver owns the hardware state these SEAMCALLs depend on, so >> it checks that state and keeps its own use of the IOMMU out of the TDX >> module's way. This is cooperation, not enforcement: the TDX module >> protects trusted DMA regardless, and a misbehaving host can only cause >> failures. >> >> Add a per-device interface so that an IOMMU is put into Secure TDX Mode >> only when a device behind it needs trusted DMA. tdx-host tracks how many >> devices use each IOMMU and calls into the VT-d driver only for the first >> and the last of them: >> >> - dmar_tdxc_get_params() looks up the DMAR unit translating the device >> and returns its register base address, which is how the TDX module >> identifies an IOMMU, and its NUMA node, so that tdx-host can allocate >> the TDX module's metadata pages local to that IOMMU. >> >> - dmar_tdxc_acquire() is called right before TDH.IOMMU.SETUP. It checks >> the preconditions TDH.IOMMU.SETUP requires (DMA translation, queued >> invalidation and scalable mode enabled) and confines the host to the >> lower half of the domain ID space, since the TDX module owns the half >> with the MSB set. >> >> - dmar_tdxc_release() undoes acquire(). tdx-host calls it if >> TDH.IOMMU.SETUP fails, or after TDH.IOMMU.CLEAR succeeds for the last >> user. If TDH.IOMMU.CLEAR fails, tdx-host doesn't call it, so the upper >> half of the domain ID space stays reserved for the TDX module. >> >> DMAR units advertising ECAP.TDXCS are excluded from DMAR hotplug. No >> platform currently supports both trusted DMA and DMAR hotplug, so that >> flow can't be validated on hardware. Hot-removing such a unit is also >> unsafe, because tdx-host may hold a reference to it and the TDX module >> may own its trusted DMA resources. This also keeps the unit handle >> returned by dmar_tdxc_get_params() valid for the lifetime of the system. > > Here we used multiple terms (Intel IOMMU, VT-d, DMAR) to talk about > a same thing. Intel IOMMU and VT-d are interchangeable, commonly used > in many places. But DMAR is more a term in ACPI structure. Better avoid > it in description, especially in exported interface names. Intel IOMMU and VT-d refer to the hardware that the intel-iommu driver drives. DMAR is the ACPI DMA remapping table, and the term is sometimes used for the remapping hardware units it describes. The interface here is not an intel-iommu driver interface, since those should all go through the iommu core. It's exported from the ACPI/DMAR layer instead, so dmar_tdx_* seems the more fitting name. > >> @@ -689,6 +690,8 @@ struct intel_iommu { >> u64 ecmdcap[DMA_MAX_NUM_ECMDCAP]; >> u32 gcmd; /* Holds TE, EAFL. Don't need SRTP, SFL, WBF >> */ >> raw_spinlock_t register_lock; /* protect register handling */ >> + /* Acquired for the TDX module. Protected by register_lock. */ >> + bool tdxc_acquired; > > "for the TDX module" or "for the TDX mode"? The TDX mode. Will fix it. > > s/tdxc_acquired/tdx_acquired/ Sure. > >> @@ -47,9 +47,32 @@ struct dmar_drhd_unit { >> u8 ignored:1; /* ignore drhd */ >> u8 include_all:1; >> u8 gfx_dedicated:1; /* graphic dedicated */ >> + struct mutex tdxc_lock; /* serializes acquire/release */ > > s/tdxc_lock/tdx_lock/ Sure. > >> struct intel_iommu *iommu; >> }; >> >> +/* >> + * struct dmar_tdxc_params - IOMMU information for driving >> TDH.IOMMU.* >> + * @reg_base_addr: Register base address of the IOMMU. The TDX module >> + * identifies an IOMMU by it. >> + * @node: NUMA node of the IOMMU, for allocating the TDX module's >> + * metadata pages local to it. >> + * @drhd: VT-d internal handle; the caller must not dereference it. It >> + * stays valid for the lifetime of the system because TDX >> + * Connect-capable units are never hot-removed. > > what will happen if the caller deferences it? This will be dropped as discussed. > >> @@ -2518,3 +2548,158 @@ bool dmar_platform_optin(void) >> return ret; >> } >> EXPORT_SYMBOL_GPL(dmar_platform_optin); > > code starting here better fits in iommu.c? See above. > >> + >> +static void dmar_tdxc_fill_params(struct dmar_drhd_unit *drhd, >> + struct dmar_tdxc_params *params) >> +{ >> + params->reg_base_addr = drhd->reg_base_addr; >> + params->node = drhd->iommu->node; >> + /* >> + * params->drhd stays valid because trusted DMA-capable units are >> + * never hot-removed. > > hmm now the 4th term for IOMMU... This comment is gone as discussed. > >> +/* >> + * dmar_tdxc_acquire() - Acquire an IOMMU for TDH.IOMMU.SETUP >> + * @dev: PCI device that will use trusted DMA >> + * @params: Returned by dmar_tdxc_get_params() for @dev >> + * >> + * Call this right before issuing TDH.IOMMU.SETUP for the first user of >> + * the IOMMU. On success, the caller must issue TDH.IOMMU.SETUP and >> call > > why defining the caller behavior here? what would happen if the caller > just calls acquire()/release() w/o doing any SEAMCALL in between? Nothing breaks. release() just undoes what acquire() did. You're right that the interface shouldn't define the caller's behavior. > >> + * dmar_tdxc_release() if it fails. The caller owns the per-IOMMU refcount >> + * and must serialize all calls for the same IOMMU. > > this function already serializes internally. What is the rationale asking for > extra serialization in the caller? None, it isn't needed. I'll describe what acquire() and release() do to the IOMMU, and drop the requirements on the caller. > >> + * >> + * Return: 0 on success and error code on failure. >> + */ >> +int dmar_tdxc_acquire(struct pci_dev *pdev, >> + const struct dmar_tdxc_params *params) >> +{ >> + struct dmar_drhd_unit *drhd = params->drhd; >> + struct intel_iommu *iommu = drhd->iommu; >> + int ret; >> + >> + if (WARN_ON(dmar_tdxc_find_drhd(pdev) != drhd)) >> + return -EINVAL; >> + >> + guard(mutex)(&drhd->tdxc_lock); >> + /* Still set if TDH.IOMMU.CLEAR failed and tdx-host was reloaded. */ >> + if (iommu->tdxc_acquired) >> + return -EBUSY; > > what does "tdx-host was reloaded" actually mean? It's stale. After a failed TDH.IOMMU.CLEAR, the flag stays set and the next acquire() fails, whether or not tdx-host is reloaded. I'll reword it to: /* Still set if a previous TDH.IOMMU.CLEAR failed. */ > >> + >> + /* >> + * TDH.IOMMU.SETUP requires DMA translation, queued invalidation >> and >> + * scalable mode to be enabled. Catch these here so the error is > > Remove the 1st sentence as it is explained by the code. Will do. >> + * -EINVAL rather than an opaque TDX_IOMMU_INVALID_STATE. >> + */ >> + if (!(iommu->gcmd & DMA_GCMD_TE) || !iommu->qi >> || !sm_supported(iommu)) >> + return -EINVAL; >> + Thanks, baolu