Linux Confidential Computing Development
 help / color / mirror / Atom feed
From: "Aneesh Kumar K.V (Arm)" <aneesh.kumar@kernel.org>
To: linux-coco@lists.linux.dev
Cc: "Aneesh Kumar K.V (Arm)" <aneesh.kumar@kernel.org>,
	Dan Williams <djbw@kernel.org>, Leon Romanovsky <leon@kernel.org>,
	Bjorn Helgaas <bhelgaas@google.com>,
	Jason Gunthorpe <jgg@ziepe.ca>,
	Jonathan Cameron <jonathan.cameron@huawei.com>,
	Xu Yilun <yilun.xu@linux.intel.com>,
	Alexey Kardashevskiy <aik@amd.com>,
	Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
	"Rafael J . Wysocki" <rafael@kernel.org>,
	Steven Price <steven.price@arm.com>,
	Suzuki K Poulose <suzuki.poulose@arm.com>,
	Lukas Wunner <lukas@wunner.de>,
	Ashish Kalra <ashish.kalra@amd.com>,
	Tom Lendacky <thomas.lendacky@amd.com>,
	Herbert Xu <herbert@gondor.apana.org.au>,
	driver-core@lists.linux.dev, linux-pci@vger.kernel.org,
	kvm@vger.kernel.org, patches@lists.linux.dev
Subject: [PATCH v1 10/10] PCI/TSM: Configure accepted device DMA during driver attachment
Date: Mon, 23 Nov 2026 14:03:37 +0530	[thread overview]
Message-ID: <20261123083337.2731019-11-aneesh.kumar@kernel.org> (raw)
In-Reply-To: <20261123083337.2731019-1-aneesh.kumar@kernel.org>

Device acceptance and DMA enablement are separate operations. An
accepted device may still need TSM-specific setup before it can perform
DMA.

Add dma_enable and dma_disable callbacks for device-security TSMs. For
TSMs implementing these callbacks, require the device to be accepted
before configuring DMA. Reject drivers setting driver_managed_dma, which
are not supported by this patch.

Take a TSM user reference before DMA configuration and retain it until
DMA cleanup finishes. This also references the TSM device and its driver
module, so tsm_unregister() waits for cleanup and the callback code
remains available. Record whether DMA setup holds this reference in
pci_dev::tsm_dma_pinned.

Signed-off-by: Aneesh Kumar K.V (Arm) <aneesh.kumar@kernel.org>
---
 drivers/pci/pci-driver.c | 31 +++++++++++++-
 drivers/pci/tsm.c        | 89 ++++++++++++++++++++++++++++++++++++++++
 include/linux/pci-tsm.h  | 35 ++++++++++++++--
 include/linux/pci.h      |  1 +
 4 files changed, 152 insertions(+), 4 deletions(-)

diff --git a/drivers/pci/pci-driver.c b/drivers/pci/pci-driver.c
index 7efe808cdf0c..edabdf30b289 100644
--- a/drivers/pci/pci-driver.c
+++ b/drivers/pci/pci-driver.c
@@ -5,6 +5,7 @@
  */
 
 #include <linux/pci.h>
+#include <linux/pci-tsm.h>
 #include <linux/module.h>
 #include <linux/init.h>
 #include <linux/device.h>
@@ -1732,9 +1733,14 @@ static int pci_bus_num_vf(struct device *dev)
 static int pci_dma_configure(struct device *dev)
 {
 	const struct device_driver *drv = READ_ONCE(dev->driver);
+	bool use_default_domain = drv && !to_pci_driver(drv)->driver_managed_dma;
 	struct device *bridge;
 	int ret = 0;
 
+	ret = pci_tsm_dma_prepare(to_pci_dev(dev));
+	if (ret)
+		return ret;
+
 	bridge = pci_get_host_bridge_device(to_pci_dev(dev));
 
 	if (IS_ENABLED(CONFIG_OF) && bridge->parent &&
@@ -1757,12 +1763,34 @@ static int pci_dma_configure(struct device *dev)
 	pci_put_host_bridge_device(bridge);
 
 	/* @drv may not be valid when we're called from the IOMMU layer */
-	if (!ret && drv && !to_pci_driver(drv)->driver_managed_dma) {
+	if (!ret && use_default_domain) {
 		ret = iommu_device_use_default_domain(dev);
 		if (ret)
+			goto err_arch;
+	}
+
+	if (ret)
+		goto err_arch;
+
+	if (drv) {
+		ret = pci_tsm_dma_enable(to_pci_dev(dev));
+		if (ret) {
+			/* Driver core does not call dma_cleanup on this failure. */
+			pci_tsm_dma_cleanup(to_pci_dev(dev));
+			if (use_default_domain)
+				iommu_device_unuse_default_domain(dev);
+
 			arch_teardown_dma_ops(dev);
+			return ret;
+		}
 	}
 
+	return 0;
+
+err_arch:
+	pci_tsm_dma_cleanup(to_pci_dev(dev));
+
+	arch_teardown_dma_ops(dev);
 	return ret;
 }
 
@@ -1770,6 +1798,7 @@ static void pci_dma_cleanup(struct device *dev)
 {
 	struct pci_driver *driver = to_pci_driver(dev->driver);
 
+	pci_tsm_dma_cleanup(to_pci_dev(dev));
 	if (!driver->driver_managed_dma)
 		iommu_device_unuse_default_domain(dev);
 }
diff --git a/drivers/pci/tsm.c b/drivers/pci/tsm.c
index c21066e55d3b..6464670dde08 100644
--- a/drivers/pci/tsm.c
+++ b/drivers/pci/tsm.c
@@ -315,6 +315,92 @@ static bool is_devsec_tsm(struct tsm_dev *tsm_dev)
 	return tsm_dev && tsm_dev->pci_ops && tsm_dev->pci_ops->devsec_ops.lock;
 }
 
+/*
+ * Driver core already holds pdev->dev.mutex. Guest PCI/TSM operations take
+ * that mutex before pci_tsm_rwsem, so the read acquisition can wait here.
+ * The device mutex and refusal to unlock a driver-attached device keep
+ * pdev->tsm unchanged through DMA configuration and driver attachment.
+ */
+int pci_tsm_dma_prepare(struct pci_dev *pdev)
+{
+	const struct pci_tsm_ops *ops;
+	struct pci_tsm *tsm;
+	int ret = 0;
+
+	device_lock_assert(&pdev->dev);
+	if (!pdev->dev.driver)
+		return 0;
+
+	down_read(&pci_tsm_rwsem);
+
+	tsm = pdev->tsm;
+	if (!tsm || !is_devsec_tsm(tsm->tsm_dev))
+		goto out;
+
+	ops = to_pci_tsm_ops(tsm);
+	if (!ops->dma_enable)
+		goto out;
+
+	/* driver_managed_dma is unsupported. */
+	if (to_pci_driver(pdev->dev.driver)->driver_managed_dma) {
+		ret = -EOPNOTSUPP;
+		goto out;
+	}
+
+	if (!device_cc_accepted(&pdev->dev)) {
+		ret = -EACCES;
+		goto out;
+	}
+
+	/*
+	 * pdev->tsm does not hold a TSM user reference. Take one so
+	 * tsm_unregister() waits until pci_tsm_dma_cleanup() finishes
+	 * calling dma_disable(). tsm_get() also references the TSM device
+	 * and module containing the callbacks.
+	 *
+	 * tsm_dma_pinned records whether DMA setup holds this reference;
+	 * pdev->tsm can also be set when no PCI driver is attached.
+	 */
+	if (IS_ERR(tsm_get(tsm->tsm_dev))) {
+		ret = -ENODEV;
+		goto out;
+	}
+
+	pdev->tsm_dma_pinned = true;
+out:
+	up_read(&pci_tsm_rwsem);
+	return ret;
+}
+
+int pci_tsm_dma_enable(struct pci_dev *pdev)
+{
+	device_lock_assert(&pdev->dev);
+	if (!pdev->tsm_dma_pinned)
+		return 0;
+
+	return to_pci_tsm_ops(pdev->tsm)->dma_enable(pdev);
+}
+
+void pci_tsm_dma_cleanup(struct pci_dev *pdev)
+{
+	const struct pci_tsm_ops *ops;
+	struct tsm_dev *tsm_dev;
+	int ret;
+
+	device_lock_assert(&pdev->dev);
+	if (!pdev->tsm_dma_pinned)
+		return;
+
+	ops = to_pci_tsm_ops(pdev->tsm);
+	tsm_dev = pdev->tsm->tsm_dev;
+	ret = ops->dma_disable(pdev);
+	if (ret)
+		/* TODO: Prevent DMA to private memory before releasing the TSM reference. */
+		pci_err(pdev, "Failed to revoke confidential DMA (%d)\n", ret);
+	pdev->tsm_dma_pinned = false;
+	tsm_put_device(tsm_dev);
+}
+
 /**
  * pci_tsm_trylock_association() - Lock and check a PCI function's TSM state
  * @tsm: struct pci_tsm kept allocated by the caller throughout the operation
@@ -1265,6 +1351,9 @@ int pci_tsm_register(struct tsm_dev *tsm_dev)
 	if (is_link_tsm(tsm_dev) && is_devsec_tsm(tsm_dev))
 		return -EINVAL;
 
+	if (!!tsm_dev->pci_ops->dma_enable != !!tsm_dev->pci_ops->dma_disable)
+		return -EINVAL;
+
 	guard(rwsem_write)(&pci_tsm_rwsem);
 
 	/* On first enable, update sysfs groups */
diff --git a/include/linux/pci-tsm.h b/include/linux/pci-tsm.h
index aef6cfb87082..4da2a24cc0fe 100644
--- a/include/linux/pci-tsm.h
+++ b/include/linux/pci-tsm.h
@@ -24,7 +24,7 @@ struct tsm_dev;
  *		operations).
  * @viommu_get_ops: Return operations for a supported TSM-owned vIOMMU type,
  *		  or NULL if unsupported
- * @owner: Module providing the vIOMMU operations, if built as a module
+ * @owner: Module providing the vIOMMU or DMA operations, if built as a module
  * @prepare_remove: Optional, reject new device operations before TSM unregister
  *		  waits for users, or before forced PF0 removal.
  *
@@ -76,15 +76,22 @@ struct pci_tsm_ops {
 	 *	  pci_tsm') for follow-on security state transitions from the
 	 *	  LOCKED state
 	 * @unlock: destroy TSM context and return device to UNLOCKED state
+	 * @dma_enable: enable DMA after PCI DMA configuration, before probe
+	 * @dma_disable: disable DMA on driver detach or DMA/probe failure
 	 *
 	 * Context: @lock and @unlock run under pci_tsm_rwsem held for write to
 	 * sync with TSM unregistration and each other. All operations run under
-	 * the device lock for mutual exclusion with driver attach and detach.
+	 * device::mutex for mutual exclusion with driver attach and detach.
+	 * @dma_enable and @dma_disable run under device::mutex with the TSM user
+	 * reference held, without pci_tsm_rwsem. They must not acquire
+	 * pci_tsm_rwsem or attestation_dev::lock.
 	 */
 	struct_group_tagged(pci_tsm_devsec_ops, devsec_ops,
 		struct pci_tsm *(*lock)(struct tsm_dev *tsm_dev,
 					struct pci_dev *pdev);
 		void (*unlock)(struct pci_tsm *tsm);
+		int (*dma_enable)(struct pci_dev *pdev);
+		int (*dma_disable)(struct pci_dev *pdev);
 	);
 };
 
@@ -177,6 +184,7 @@ struct pci_tsm_devsec {
 	struct pci_tsm_mmio *mmio;
 };
 
+#ifdef CONFIG_PCI_TSM
 /* physical function0 and capable of 'connect' */
 static inline bool is_pci_tsm_pf0(struct pci_dev *pdev)
 {
@@ -210,7 +218,9 @@ static inline bool is_pci_tsm_pf0(struct pci_dev *pdev)
 	return PCI_FUNC(pdev->devfn) == 0;
 }
 
-#ifdef CONFIG_PCI_TSM
+int pci_tsm_dma_prepare(struct pci_dev *pdev);
+int pci_tsm_dma_enable(struct pci_dev *pdev);
+void pci_tsm_dma_cleanup(struct pci_dev *pdev);
 struct pci_tsm *pci_tsm_trylock_association(struct pci_tsm *tsm);
 void pci_tsm_unlock_association(struct pci_tsm *tsm);
 void pci_tsm_set_ready(struct pci_tsm_pf0 *pf0, bool ready);
@@ -242,6 +252,25 @@ struct pci_tsm_mmio *pci_tsm_mmio_alloc_report(struct pci_dev *pdev,
 					    const void *report_data, size_t report_sz);
 int pci_tsm_mmio_free(struct pci_dev *pdev, struct pci_tsm_mmio *mmio);
 #else
+static inline bool is_pci_tsm_pf0(struct pci_dev *pdev)
+{
+	return false;
+}
+
+static inline int pci_tsm_dma_prepare(struct pci_dev *pdev)
+{
+	return 0;
+}
+
+static inline int pci_tsm_dma_enable(struct pci_dev *pdev)
+{
+	return 0;
+}
+
+static inline void pci_tsm_dma_cleanup(struct pci_dev *pdev)
+{
+}
+
 static inline struct pci_tsm *pci_tsm_trylock_association(struct pci_tsm *tsm)
 {
 	return ERR_PTR(-ENODEV);
diff --git a/include/linux/pci.h b/include/linux/pci.h
index aa3765a20a96..487ef6c5dea6 100644
--- a/include/linux/pci.h
+++ b/include/linux/pci.h
@@ -581,6 +581,7 @@ struct pci_dev {
 #endif
 #ifdef CONFIG_PCI_TSM
 	struct pci_tsm *tsm;		/* TSM operation state */
+	bool tsm_dma_pinned;		/* DMA binding holds a TSM user reference */
 #endif
 	u16		acs_cap;	/* ACS Capability offset */
 	u16		acs_capabilities; /* ACS Capabilities */
-- 
2.43.0


  parent reply	other threads:[~2026-11-23  8:34 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-23  8:33 [PATCH v1 00/10] PCI/TSM: Support guest TSM drivers Aneesh Kumar K.V (Arm)
2026-11-23  8:33 ` [PATCH v1 01/10] PCI/TSM: Report active IDE streams per host bridge Aneesh Kumar K.V (Arm)
2026-11-23  8:33 ` [PATCH v1 02/10] coco: tsm: Register the TSM class before device drivers Aneesh Kumar K.V (Arm)
2026-11-23  8:33 ` [PATCH v1 03/10] PCI/TSM: Let TSM drivers discover the CMA DOE mailbox Aneesh Kumar K.V (Arm)
2026-11-23  8:33 ` [PATCH v1 04/10] PCI/TSM: Defer TSM vDEVICE allocation until device setup completes Aneesh Kumar K.V (Arm)
2026-11-23  8:33 ` [PATCH v1 05/10] PCI/TSM: Add association locking helpers for evidence acceptance Aneesh Kumar K.V (Arm)
2026-11-23  8:33 ` [PATCH v1 06/10] PCI/TSM: Notify TSM drivers before waiting for TSM user references Aneesh Kumar K.V (Arm)
2026-11-23  8:33 ` [PATCH v1 07/10] device core: Add helpers to record confidential device acceptance Aneesh Kumar K.V (Arm)
2026-11-23  8:42   ` Greg Kroah-Hartman
2026-11-23  8:33 ` [PATCH v1 08/10] PCI/TSM: Add sysfs lock and unlock for guest-assigned devices Aneesh Kumar K.V (Arm)
2026-11-23  8:33 ` [PATCH v1 09/10] PCI/TSM: Build encrypted MMIO descriptors from a supplied report Aneesh Kumar K.V (Arm)
2026-11-23  8:33 ` Aneesh Kumar K.V (Arm) [this message]
2026-11-23  8:40 ` [PATCH v1 00/10] PCI/TSM: Support guest TSM drivers Greg Kroah-Hartman
2026-11-23  9:06   ` Leon Romanovsky

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=20261123083337.2731019-11-aneesh.kumar@kernel.org \
    --to=aneesh.kumar@kernel.org \
    --cc=aik@amd.com \
    --cc=ashish.kalra@amd.com \
    --cc=bhelgaas@google.com \
    --cc=djbw@kernel.org \
    --cc=driver-core@lists.linux.dev \
    --cc=gregkh@linuxfoundation.org \
    --cc=herbert@gondor.apana.org.au \
    --cc=jgg@ziepe.ca \
    --cc=jonathan.cameron@huawei.com \
    --cc=kvm@vger.kernel.org \
    --cc=leon@kernel.org \
    --cc=linux-coco@lists.linux.dev \
    --cc=linux-pci@vger.kernel.org \
    --cc=lukas@wunner.de \
    --cc=patches@lists.linux.dev \
    --cc=rafael@kernel.org \
    --cc=steven.price@arm.com \
    --cc=suzuki.poulose@arm.com \
    --cc=thomas.lendacky@amd.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