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 05/10] PCI/TSM: Add association locking helpers for evidence acceptance
Date: Mon, 23 Nov 2026 14:03:32 +0530 [thread overview]
Message-ID: <20261123083337.2731019-6-aneesh.kumar@kernel.org> (raw)
In-Reply-To: <20261123083337.2731019-1-aneesh.kumar@kernel.org>
Later attestation patches let userspace finish device setup through
evidence-accept after host sysfs connect or guest sysfs lock returns.
Those commands use a saved struct pci_tsm and must prevent disconnect or
guest unlock from changing pci_dev::tsm or freeing the saved structure
while setup is in progress.
Add pci_tsm_trylock_association() to acquire pci_tsm_rwsem for write and
check that pci_dev::tsm still points to the saved structure. For a TSM
implementing devsec_ops, first take device::mutex to prevent driver
attachment during MMIO validation and device acceptance. This matches
the device-mutex-before-rwsem order used by PCI DMA preparation.
Neither lock acquisition waits. Host disconnect holds pci_tsm_rwsem for
write; guest unlock holds device::mutex and the rwsem. Both unregister
attestation, which takes ctlv::registration_lock for write to wait for
running CTLV ioctls. The evidence-accept ioctl holds that lock for read
when calling this helper, so waiting for disconnect or unlock would
deadlock. Return ERR_PTR(-EAGAIN) on contention and ERR_PTR(-ENODEV) if
the saved pointer no longer matches.
Signed-off-by: Aneesh Kumar K.V (Arm) <aneesh.kumar@kernel.org>
---
drivers/pci/tsm.c | 66 +++++++++++++++++++++++++++++++++++++++++
include/linux/pci-tsm.h | 17 +++++++++++
2 files changed, 83 insertions(+)
diff --git a/drivers/pci/tsm.c b/drivers/pci/tsm.c
index 4dafc5dc6fe3..56b6c812a8f9 100644
--- a/drivers/pci/tsm.c
+++ b/drivers/pci/tsm.c
@@ -294,6 +294,72 @@ static bool is_devsec_tsm(struct tsm_dev *tsm_dev)
return tsm_dev && tsm_dev->pci_ops && tsm_dev->pci_ops->devsec_ops.lock;
}
+/**
+ * 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
+ *
+ * For a TSM implementing devsec_ops, first trylock tsm->pdev->dev.mutex to
+ * prevent PCI driver attachment during MMIO validation and device setup.
+ * Then acquire pci_tsm_rwsem for write before checking that tsm->pdev->tsm
+ * still points to @tsm. A TSM implementing link_ops only needs the rwsem.
+ *
+ * Host disconnect holds pci_tsm_rwsem for write; guest unlock also
+ * holds pdev->dev.mutex. Both unregister attestation, which waits for
+ * running CTLV ioctls by taking ctlv::registration_lock for write.
+ *
+ * evidence-accept holds that registration lock for read when calling
+ * this helper. It must return -EAGAIN rather than wait for the locks
+ * held by disconnect or unlock, otherwise neither call can finish.
+ *
+ * Return: @tsm with the locks held, ERR_PTR(-EAGAIN) on lock contention, or
+ * ERR_PTR(-ENODEV) if tsm->pdev->tsm no longer points to @tsm.
+ */
+struct pci_tsm *pci_tsm_trylock_association(struct pci_tsm *tsm)
+{
+ struct pci_dev *pdev = tsm->pdev;
+ bool devsec = is_devsec_tsm(tsm->tsm_dev);
+
+ if (devsec && !device_trylock(&pdev->dev))
+ return ERR_PTR(-EAGAIN);
+
+ if (!down_write_trylock(&pci_tsm_rwsem)) {
+ if (devsec)
+ device_unlock(&pdev->dev);
+
+ return ERR_PTR(-EAGAIN);
+ }
+
+ if (pdev->tsm != tsm) {
+ up_write(&pci_tsm_rwsem);
+
+ if (devsec)
+ device_unlock(&pdev->dev);
+
+ return ERR_PTR(-ENODEV);
+ }
+
+ return tsm;
+}
+EXPORT_SYMBOL_GPL(pci_tsm_trylock_association);
+
+/**
+ * pci_tsm_unlock_association() - Release locks protecting a PCI function's TSM state
+ * @tsm: struct pci_tsm returned by a successful pci_tsm_trylock_association()
+ *
+ */
+void pci_tsm_unlock_association(struct pci_tsm *tsm)
+{
+ struct pci_dev *pdev = tsm->pdev;
+ bool devsec = is_devsec_tsm(tsm->tsm_dev);
+
+ lockdep_assert_held_write(&pci_tsm_rwsem);
+ up_write(&pci_tsm_rwsem);
+
+ if (devsec)
+ device_unlock(&pdev->dev);
+}
+EXPORT_SYMBOL_GPL(pci_tsm_unlock_association);
+
/**
* pci_tsm_set_ready() - Set when PF0 permits device assignment
* @ready: whether the device is ready for assignment
diff --git a/include/linux/pci-tsm.h b/include/linux/pci-tsm.h
index 9c5f0b6c0f6d..d93d1c30d734 100644
--- a/include/linux/pci-tsm.h
+++ b/include/linux/pci-tsm.h
@@ -1,6 +1,7 @@
/* SPDX-License-Identifier: GPL-2.0 */
#ifndef __PCI_TSM_H
#define __PCI_TSM_H
+#include <linux/cleanup.h>
#include <linux/completion.h>
#include <linux/mutex.h>
#include <linux/pci.h>
@@ -159,6 +160,8 @@ static inline bool is_pci_tsm_pf0(struct pci_dev *pdev)
}
#ifdef CONFIG_PCI_TSM
+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);
int pci_tsm_register(struct tsm_dev *tsm_dev);
void pci_tsm_unregister(struct tsm_dev *tsm_dev);
@@ -179,6 +182,15 @@ bool pci_tsm_context_match_device(struct pci_tsm_context *context,
struct pci_dev *pdev);
struct tsm_dev *pci_tsm_get_device(struct pci_dev *pdev);
#else
+static inline struct pci_tsm *pci_tsm_trylock_association(struct pci_tsm *tsm)
+{
+ return ERR_PTR(-ENODEV);
+}
+
+static inline void pci_tsm_unlock_association(struct pci_tsm *tsm)
+{
+}
+
static inline void pci_tsm_set_ready(struct pci_tsm_pf0 *pf0, bool ready)
{
}
@@ -230,6 +242,11 @@ pci_tsm_context_match_device(struct pci_tsm_context *context,
return false;
}
#endif
+
+DEFINE_FREE(pci_tsm_unlock, struct pci_tsm *,
+ if (!IS_ERR_OR_NULL(_T))
+ pci_tsm_unlock_association(_T))
+
extern struct rw_semaphore pci_tsm_rwsem;
#endif /*__PCI_TSM_H */
--
2.43.0
next prev 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 ` Aneesh Kumar K.V (Arm) [this message]
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 ` [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-6-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