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 06/10] PCI/TSM: Notify TSM drivers before waiting for TSM user references
Date: Mon, 23 Nov 2026 14:03:33 +0530	[thread overview]
Message-ID: <20261123083337.2731019-7-aneesh.kumar@kernel.org> (raw)
In-Reply-To: <20261123083337.2731019-1-aneesh.kumar@kernel.org>

Call the optional prepare_remove callback from tsm_unregister() before
waiting for TSM user references. Host PF0 callbacks run with
pci_tsm_rwsem held for write, followed by pci_tsm_pf0::lock. Guest
device-security callbacks take device::mutex before pci_tsm_rwsem for
write, matching PCI DMA preparation.

Later CCA patches use this callback to reject new host physical device
(PDEV) operations and guest evidence requests before unregister waits
for existing users. The guest DMA patch also uses this ordering to
reject DMA-enable requests while permitting DMA disable.

Also invoke prepare_remove() from pci_tsm_destroy() before waiting for
existing struct pci_tsm_context objects to be released. Sysfs disconnect
continues to return -EBUSY while those objects remain.

Signed-off-by: Aneesh Kumar K.V (Arm) <aneesh.kumar@kernel.org>
---
 drivers/pci/tsm.c            | 50 +++++++++++++++++++++++++++++++++---
 drivers/virt/coco/tsm-core.c |  2 ++
 include/linux/pci-tsm.h      | 10 ++++++++
 3 files changed, 59 insertions(+), 3 deletions(-)

diff --git a/drivers/pci/tsm.c b/drivers/pci/tsm.c
index 56b6c812a8f9..f04498265383 100644
--- a/drivers/pci/tsm.c
+++ b/drivers/pci/tsm.c
@@ -906,6 +906,21 @@ static void __pci_tsm_destroy(struct pci_dev *pdev, struct tsm_dev *tsm_dev)
 	}
 }
 
+static void pci_tsm_prepare_remove(struct pci_dev *pdev)
+{
+	struct pci_tsm_pf0 *pf0 = to_pci_tsm_pf0(pdev->tsm);
+	const struct pci_tsm_ops *ops = pdev->tsm->tsm_dev->pci_ops;
+
+	lockdep_assert_held_write(&pci_tsm_rwsem);
+	guard(mutex)(&pf0->lock);
+	if (pf0->removing)
+		return;
+
+	pf0->removing = true;
+	if (ops->prepare_remove)
+		ops->prepare_remove(pdev);
+}
+
 void pci_tsm_destroy(struct pci_dev *pdev)
 {
 	struct tsm_dev *tsm_dev = NULL;
@@ -916,9 +931,7 @@ void pci_tsm_destroy(struct pci_dev *pdev)
 	    is_pci_tsm_pf0(pdev)) {
 		struct pci_tsm_pf0 *pf0 = to_pci_tsm_pf0(pdev->tsm);
 
-		mutex_lock(&pf0->lock);
-		pf0->removing = true;
-		mutex_unlock(&pf0->lock);
+		pci_tsm_prepare_remove(pdev);
 
 		if (pf0->dsm_context_users) {
 			struct completion *drain_complete;
@@ -1034,6 +1047,37 @@ void pci_tsm_init(struct pci_dev *pdev)
 	up_read(&pci_tsm_rwsem);
 }
 
+/* Reject new device operations before tsm_unregister() waits for users. */
+void pci_tsm_prepare_unregister(struct tsm_dev *tsm_dev)
+{
+	const struct pci_tsm_ops *ops = tsm_dev->pci_ops;
+	struct pci_dev *pdev = NULL;
+
+	if (is_devsec_tsm(tsm_dev)) {
+		if (!ops->prepare_remove)
+			return;
+
+		for_each_pci_dev(pdev) {
+			guard(device)(&pdev->dev);
+			guard(rwsem_write)(&pci_tsm_rwsem);
+
+			if (pdev->tsm && pdev->tsm->tsm_dev == tsm_dev)
+				ops->prepare_remove(pdev);
+		}
+
+		return;
+	}
+
+	guard(rwsem_write)(&pci_tsm_rwsem);
+	for_each_pci_dev(pdev) {
+		if (!pdev->tsm || pdev->tsm->tsm_dev != tsm_dev)
+			continue;
+
+		if (is_pci_tsm_pf0(pdev))
+			pci_tsm_prepare_remove(pdev);
+	}
+}
+
 void pci_tsm_unregister(struct tsm_dev *tsm_dev)
 {
 	struct pci_dev *pdev = NULL;
diff --git a/drivers/virt/coco/tsm-core.c b/drivers/virt/coco/tsm-core.c
index 31ef48203eac..ba9183783dd1 100644
--- a/drivers/virt/coco/tsm-core.c
+++ b/drivers/virt/coco/tsm-core.c
@@ -260,6 +260,8 @@ int tsm_unregister(struct tsm_dev *tsm_dev)
 	}
 
 	tsm_dev->unregistering = true;
+	if (IS_ENABLED(CONFIG_PCI_TSM) && tsm_dev->pci_ops)
+		pci_tsm_prepare_unregister(tsm_dev);
 
 	device_del(&tsm_dev->dev);
 	/* No new user references can be acquired while the write lock is held. */
diff --git a/include/linux/pci-tsm.h b/include/linux/pci-tsm.h
index d93d1c30d734..75df64a2e97d 100644
--- a/include/linux/pci-tsm.h
+++ b/include/linux/pci-tsm.h
@@ -25,6 +25,8 @@ struct tsm_dev;
  * @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
+ * @prepare_remove: Optional, reject new device operations before TSM unregister
+ *		  waits for users, or before forced PF0 removal.
  *
  * The vIOMMU owner holds a TSM user reference, a device reference and a
  * backend module reference before querying @viommu_get_ops, releasing them
@@ -36,11 +38,14 @@ struct tsm_dev;
  *
  * @viommu_get_ops runs with the backend module pinned. The PCI/TSM
  * association is checked during acquisition under pci_tsm_rwsem.
+ * @prepare_remove runs under pci_tsm_rwsem for write, with pci_tsm_pf0::lock
+ * for link devices or device::mutex for device-security operations.
  */
 struct pci_tsm_ops {
 	struct module *owner;
 	const struct iommufd_viommu_ops *(*viommu_get_ops)(
 		struct device *dev, enum iommu_viommu_type type);
+	void (*prepare_remove)(struct pci_dev *pdev);
 
 	/*
 	 * struct pci_tsm_link_ops - Manage physical link and the TSM/DSM session
@@ -165,6 +170,7 @@ void pci_tsm_unlock_association(struct pci_tsm *tsm);
 void pci_tsm_set_ready(struct pci_tsm_pf0 *pf0, bool ready);
 int pci_tsm_register(struct tsm_dev *tsm_dev);
 void pci_tsm_unregister(struct tsm_dev *tsm_dev);
+void pci_tsm_prepare_unregister(struct tsm_dev *tsm_dev);
 int pci_tsm_link_constructor(struct pci_dev *pdev, struct pci_tsm *tsm,
 			     struct tsm_dev *tsm_dev);
 int pci_tsm_pf0_constructor(struct pci_dev *pdev, struct pci_tsm_pf0 *pf0,
@@ -203,6 +209,10 @@ static inline void pci_tsm_unregister(struct tsm_dev *tsm_dev)
 {
 }
 
+static inline void pci_tsm_prepare_unregister(struct tsm_dev *tsm_dev)
+{
+}
+
 static inline bool pci_tsm_is_configured(struct pci_dev *pdev)
 {
 	return false;
-- 
2.43.0


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

Thread overview: 15+ 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 ` Aneesh Kumar K.V (Arm) [this message]
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 ` [PATCH v1 10/10] PCI/TSM: Configure accepted device DMA during driver attachment Aneesh Kumar K.V (Arm)
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
2026-11-23  9:43     ` Aneesh Kumar K.V

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-7-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