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) --- 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 #include #include #include @@ -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