PCI/TSM: Support guest TSM drivers
Subsystems (8)
Aneesh Kumar K.V (Arm)
maintainercover PCI/TSM: Support guest TSM drivers
This series adds the PCI/TSM support needed by a TEE Security Manager (TSM) driver in a confidential guest. It adds device lock/unlock, helpers for recording device acceptance and parsing MMIO reports, and DMA callbacks used during PCI driver attachment. The guest TSM driver and attestation commands using these interfaces are in follow-up series.
This series is based on Dan Williams' "[PATCH v2 00/19] PCI/TSM: TEE I/O infrastructure" posting [1], particularly the guest LOCK/UNLOCK, ACCEPT and MMIO report work [2-4]. It retains device_cc_accepted() and the confidential-device acceptance model from that posting, with changes adapted to the current PCI/TSM structures and reference APIs. Dan's device-core acceptance helper and per-host-bridge IDE stream reporting patch are also included.
This series does not adopt the Device Trust changes from Dan's "Device Evidence and Trust for PCI Security Protocol (TDISP)" [5]. That work is being developed separately in Leon Romanovsky's "driver core: Authorize device trust before driver binding" series, [6]. Confidential-device acceptance is kept separate from that driver-binding trust policy.
The guest flow separates three operations:
* Lock the device through tsm/lock. This requests the TEE Device Interface Security Protocol (TDISP) LOCKED state, so userspace can verify evidence about the locked configuration. Unlock releases the device configuration and the TSM driver's per-device state. Neither operation is permitted while a PCI driver is attached.
* Request device use through the attestation evidence-accept operation. A follow-up guest TSM driver validates the reported MMIO mappings, requests TDISP RUN and marks the device accepted. This series supplies the device acceptance state, MMIO report helpers and PCI/TSM locking helpers needed for that work.
* Enable DMA during PCI driver attachment, after platform DMA configuration and IOMMU setup, before the PCI driver's probe function. Disable DMA during driver detach and setup cleanup. Hold TSM user, device and module references until DMA cleanup finishes calling the TSM driver.
Compared with the earlier posting, evidence retrieval and acceptance use attestation rather than PCI/TSM evidence storage and the sysfs accept attribute.
This series is based on the IOMMUFD/TSM vIOMMU and vDEVICE work [7]. It uses that series' TSM references, unregister waits and struct pci_tsm_context references. The architecture-specific CCA host and guest drivers and their SPDM attestation operations are in separate follow-up series.
The full patchset, including attestation, Device Trust, IOMMUFD, guest_memfd and the Arm CCA host and guest changes, is available at:
https://git.gitlab.arm.com/linux-arm/linux-cca.git
branch: cca/topics/7.4/cca-da-integration-v1
Notes:
* Codex assisted with implementation, review and series organization.
[1] https://lore.kernel.org/all/[email protected]/
[2] https://lore.kernel.org/all/[email protected]/
[3] https://lore.kernel.org/all/[email protected]/
[4] https://lore.kernel.org/all/[email protected]/
[5] https://lore.kernel.org/all/[email protected]/
[6] https://lore.kernel.org/all/[email protected]/
[7] https://lore.kernel.org/all/[email protected]/
Aneesh Kumar K.V (Arm) (9):
coco: tsm: Register the TSM class before device drivers PCI/TSM: Let TSM drivers discover the CMA DOE mailbox PCI/TSM: Defer TSM vDEVICE allocation until device setup completes PCI/TSM: Add association locking helpers for evidence acceptance PCI/TSM: Notify TSM drivers before waiting for TSM user references device core: Add helpers to record confidential device acceptance PCI/TSM: Add sysfs lock and unlock for guest-assigned devices PCI/TSM: Build encrypted MMIO descriptors from a supplied report PCI/TSM: Configure accepted device DMA during driver attachment
Dan Williams (1):
PCI/TSM: Report active IDE streams per host bridge
Documentation/ABI/testing/sysfs-bus-pci | 35 +- Documentation/ABI/testing/sysfs-class-tsm | 32 + Documentation/driver-api/pci/tsm.rst | 47 ++ MAINTAINERS | 1 + drivers/base/Kconfig | 4 + drivers/base/Makefile | 1 + drivers/base/base.h | 9 + drivers/base/coco.c | 68 ++ drivers/crypto/ccp/sev-dev-tsm.c | 7 + drivers/pci/Kconfig | 2 + drivers/pci/ide.c | 4 + drivers/pci/pci-driver.c | 31 +- drivers/pci/tsm.c | 749 ++++++++++++++++++++-- drivers/virt/coco/tsm-core.c | 150 ++++- include/linux/device.h | 22 + include/linux/ioport.h | 1 + include/linux/pci-ide.h | 2 + include/linux/pci-tsm.h | 130 +++- include/linux/pci.h | 1 + include/linux/tsm.h | 4 + 20 files changed, 1248 insertions(+), 52 deletions(-) create mode 100644 drivers/base/coco.c
base-commit: 65e4fd942f72a9dba0e4e257290c4fa1aa756854
prerequisite-patch-id: 121492d71cc22b2adbfdedd668868c5a449ad764
prerequisite-patch-id: 266d2641e47ccf8f57f8ca77d90d193f9cfabbbd
prerequisite-patch-id: 5502ecfa0b2659885b221f103d44d473c72fec03
prerequisite-patch-id: 6ec85de28f71ce761596e493a68a56242327c0b1
prerequisite-patch-id: f6e97602a5b06ffddd2cd3c2815bed27411d51e3
prerequisite-patch-id: 03d76de408efeb5a2205079bb61face09255ca60
prerequisite-patch-id: 51872ad4fcd244f3425b81f5a0f05acd4ffa5c39
prerequisite-patch-id: b56e823a40dc6d1e70e74a00cf3e1da2d0ecc9d7
prerequisite-patch-id: 53dcbe1d39b6848f778eba736a2779b0e587cfcb
prerequisite-patch-id: 66c884ec28e3d15a174dfb288858b174fb21480d
prerequisite-patch-id: 18a7425cf79e0a14ac934cb2a194b27a62d50c9d
prerequisite-patch-id: e47eb53d14d6f6180eb0606d721bfd55d74c96b3
prerequisite-patch-id: ce898fc01b883c3318a6bdb883578b8b4e4299a2
prerequisite-patch-id: 416778d72f4ade8c0f13b9fe3e619ec9d5e9634b
prerequisite-patch-id: d30d30bbe67196c41b592e79fd0c15a78bec0ae7
Signature · 1 line
2.43.0
Aneesh Kumar K.V (Arm)
maintainerPATCH 1/10 PCI/TSM: Report active IDE streams per host bridge
From: Dan Williams <[email protected]>
The first attempt at an ABI for this failed to account for naming collisions across host bridges:
Commit a4438f06b1db ("PCI/TSM: Report active IDE streams")
Revive this ABI with a per host bridge link that appears at first stream creation for a given host bridge and disappears after the last stream is removed.
For systems with many host bridge objects it allows:
ls /sys/class/tsm/tsmN/pci*/stream*
...to find all the host bridges with active streams without first iterating over all host bridges. Yilun notes that is handy to have this short cut [1] and from an administrator perspective it helps with inventory for constrained stream resources.
--- Documentation/ABI/testing/sysfs-class-tsm | 13 ++++ drivers/pci/ide.c | 4 + drivers/virt/coco/tsm-core.c | 95 +++++++++++++++++++++++ include/linux/pci-ide.h | 2 + include/linux/tsm.h | 4 + 5 files changed, 118 insertions(+)
Documentation/ABI/testing/sysfs-class-tsm +13 -0
@@ -7,3 +7,16 @@ Description: signals when the PCI layer is able to support establishment of link encryption and other device-security features coordinated through a platform tsm. + +What: /sys/class/tsm/tsmN/pciDDDD:BB +Contact: [email protected] +Description: + (RO) When a PCIe host bridge has established a secure connection + via a TSM to an endpoint, this symlink appears. It facilitates a + TSM instance scoped view of PCIe Link Encryption and Secure + Session resource consumption across host bridges. The symlink + appears when a host bridge has 1 or more IDE streams established + with this TSM, and disappears when that number returns to 0. See + Documentation/ABI/testing/sysfs-devices-pci-host-bridge for the + description of the pciDDDD:BB/streamH.R.E symlink and the + pciDDDD:BB/available_secure_streams attribute.
drivers/pci/ide.c +4 -0
@@ -11,6 +11,7 @@#include <linux/pci_regs.h> #include <linux/slab.h> #include <linux/sysfs.h> +#include <linux/tsm.h> #include "pci.h"
@@ -370,6 +371,9 @@ void pci_ide_stream_release(struct pci_ide *ide)if (ide->partner[PCI_IDE_EP ].enable)pci_ide_stream_disable (pdev, ide); +if (ide->tsm_dev) +tsm_ide_stream_unregister (ide); +if (ide->partner[PCI_IDE_RP ].setup)pci_ide_stream_teardown (rp, ide);
drivers/virt/coco/tsm-core.c +95 -0
118 lines not on this page · raw message
include/linux/pci-ide.h +2 -0
@@ -82,6 +82,7 @@ struct pci_ide_regs {* @host_bridge_stream: allocated from host bridge @ide_stream_ida pool * @stream_id: unique Stream ID (within Partner Port pairing) * @name: name of the established Selective IDE Stream in sysfs +* @tsm_dev: For TSM established IDE, the TSM device context * * Negative @stream_id values indicate "uninitialized" on the * expectation that with TSM established IDE the TSM owns the stream_id
@@ -93,6 +94,7 @@ struct pci_ide {u8 host_bridge_stream;int stream_id;const char *name; +struct tsm_dev *tsm_dev; };/*
include/linux/tsm.h +4 -0
@@ -162,4 +162,8 @@ tsm_viommu_get_ops(struct tsm_dev *tsm_dev, struct device *dev,return NULL ; }#endif + +struct pci_ide ; +int tsm_ide_stream_register (struct pci_ide *ide,struct tsm_dev *tsm_dev); +void tsm_ide_stream_unregister (struct pci_ide *ide);#endif /* __TSM_H */
Signature · 1 line
2.43.0
Aneesh Kumar K.V (Arm)
maintainerPATCH 2/10 coco: tsm: Register the TSM class before device drivers
In reply to Aneesh Kumar K.V (Arm)Register the built-in TSM class with subsys_initcall() so it exists before a platform TSM driver probes, independently of object link order. CONFIG_TSM is boolean; remove the unused module exit path.
--- drivers/virt/coco/tsm-core.c | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-)
drivers/virt/coco/tsm-core.c +1 -8
@@ -379,14 +379,7 @@ static int __init tsm_init(void) {return class_register (&tsm_class); } -module_init (tsm_init) - -static void __exit tsm_exit(void) -{ -class_unregister (&tsm_class); -xa_destroy (&tsm_ide_streams); -} -module_exit(tsm_exit) +subsys_initcall(tsm_init) MODULE_LICENSE("GPL");MODULE_DESCRIPTION ("TEE Security Manager Class Device" );
Signature · 1 line
2.43.0
Aneesh Kumar K.V (Arm)
maintainerPATCH 3/10 PCI/TSM: Let TSM drivers discover the CMA DOE mailbox
In reply to Aneesh Kumar K.V (Arm)Move CMA DOE discovery out of pci_tsm_pf0_constructor(). An external Endpoint requires a mailbox, while an RcIEP can use device assignment without DOE. Let each TSM driver enforce its own requirement.
Keep SEV-TIO requiring a CMA mailbox and reject devices without one. A later patch adds the CCA host driver's mailbox checks, allowing RcIEPs to operate without DOE.
--- drivers/crypto/ccp/sev-dev-tsm.c | 7 ++++++ drivers/pci/tsm.c | 39 ++++++++++++++++---------------- include/linux/pci-tsm.h | 4 ++-- 3 files changed, 29 insertions(+), 21 deletions(-)
drivers/crypto/ccp/sev-dev-tsm.c +7 -0
@@ -213,6 +213,13 @@ static struct pci_tsm *tio_pf0_probe(struct pci_dev *pdev, struct sev_device *seif (!dsm)return NULL ; + dsm->tsm.doe_mb =pci_find_doe_mailbox (pdev,PCI_VENDOR_ID_PCI_SIG , +PCI_DOE_FEATURE_CMA ); +if (!dsm->tsm.doe_mb) { +pci_warn (pdev,"TSM init failure, no CMA mailbox\n" ); +return NULL ; + } + rc =pci_tsm_pf0_constructor (pdev, &dsm->tsm, sev->tsmdev);if (rc)return NULL ;
drivers/pci/tsm.c +20 -19
@@ -714,30 +714,31 @@ EXPORT_SYMBOL_GPL(pci_tsm_link_constructor);/** * pci_tsm_pf0_constructor() - common 'struct pci_tsm_pf0' (DSM) initialization * @pdev: Physical Function 0 PCI device (as indicated by is_pci_tsm_pf0()) -* @tsm: context to initialize +* @pf0: PF0 state to initialize * @tsm_dev: Platform TEE Security Manager, initiator of security operations */ -int pci_tsm_pf0_constructor (struct pci_dev *pdev,struct pci_tsm_pf0 *tsm, +int pci_tsm_pf0_constructor (struct pci_dev *pdev,struct pci_tsm_pf0 *pf0,struct tsm_dev *tsm_dev) { -mutex_init (&tsm->lock); -init_completion (&tsm->dsm_context_drain_complete); - tsm->dsm_context_users =0 ; - tsm->removing = false; - tsm->doe_mb =pci_find_doe_mailbox (pdev,PCI_VENDOR_ID_PCI_SIG , -PCI_DOE_FEATURE_CMA ); -if (!tsm->doe_mb) { -pci_warn (pdev,"TSM init failure, no CMA mailbox\n" ); -return -ENODEV ; - } +mutex_init (&pf0->lock); +init_completion (&pf0->dsm_context_drain_complete); + pf0->dsm_context_users =0 ; + pf0->removing = false; +/* +* Note, low-level TSM driver responsible for determining if it wants to +* proceed with a device that has no DOE mailbox. TSM may have an +* alternate method for coordinating TDISP. +*/ +if (!pf0->doe_mb) +pci_dbg (pdev,"No DOE mailbox for Component Measurement and Authentication\n" ); -return pci_tsm_link_constructor (pdev, &tsm->base_tsm, tsm_dev); +return pci_tsm_link_constructor (pdev, &pf0->base_tsm, tsm_dev); }EXPORT_SYMBOL_GPL (pci_tsm_pf0_constructor); -void pci_tsm_pf0_destructor (struct pci_tsm_pf0 *pf0_tsm) +void pci_tsm_pf0_destructor (struct pci_tsm_pf0 *pf0) { -mutex_destroy (&pf0_tsm->lock); +mutex_destroy (&pf0->lock); }EXPORT_SYMBOL_GPL (pci_tsm_pf0_destructor);
@@ -954,16 +955,16 @@ void pci_tsm_unregister(struct tsm_dev *tsm_dev)int pci_tsm_doe_transfer (struct pci_dev *pdev,u8 type,const void *req,size_t req_sz,void *resp,size_t resp_sz) { -struct pci_tsm_pf0 *tsm; +struct pci_tsm_pf0 *pf0;if (!pdev->tsm || !is_pci_tsm_pf0 (pdev))return -ENXIO ; - tsm =to_pci_tsm_pf0 (pdev->tsm); -if (!tsm->doe_mb) + pf0 =to_pci_tsm_pf0 (pdev->tsm); +if (!pf0->doe_mb)return -ENXIO ; -return pci_doe (tsm->doe_mb,PCI_VENDOR_ID_PCI_SIG , type, req, req_sz, +return pci_doe (pf0->doe_mb,PCI_VENDOR_ID_PCI_SIG , type, req, req_sz, resp, resp_sz); }EXPORT_SYMBOL_GPL (pci_tsm_doe_transfer);
include/linux/pci-tsm.h +2 -2
@@ -160,9 +160,9 @@ int pci_tsm_register(struct tsm_dev *tsm_dev);void pci_tsm_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 *tsm, +int pci_tsm_pf0_constructor (struct pci_dev *pdev,struct pci_tsm_pf0 *pf0,struct tsm_dev *tsm_dev); -void pci_tsm_pf0_destructor (struct pci_tsm_pf0 *tsm); +void pci_tsm_pf0_destructor (struct pci_tsm_pf0 *pf0);int pci_tsm_doe_transfer (struct pci_dev *pdev,u8 type,const void *req,size_t req_sz,void *resp,size_t resp_sz);bool pci_tsm_is_configured (struct pci_dev *pdev);
Signature · 1 line
2.43.0
Aneesh Kumar K.V (Arm)
maintainerPATCH 4/10 PCI/TSM: Defer TSM vDEVICE allocation until device setup completes
In reply to Aneesh Kumar K.V (Arm)A successful connect does not necessarily complete the checks and setup needed to assign a device to a confidential guest. Authentication and userspace evidence acceptance may still be required.
Add pci_tsm_pf0::ready and pci_tsm_set_ready(). While ready is false, pci_tsm_context_get() returns -EACCES, preventing TSM virtual device (vDEVICE) allocation.
Initialize ready to true to preserve existing TSM driver behavior. A follow-up CCA patch will clear ready during connect and set it after evidence-accept completes the required device setup.
--- drivers/pci/tsm.c | 31 ++++++++++++++++++++++++++++++- include/linux/pci-tsm.h | 10 ++++++++++ 2 files changed, 40 insertions(+), 1 deletion(-)
drivers/pci/tsm.c +30 -1
@@ -27,7 +27,8 @@* Provide a read/write lock against the init / exit of pdev tsm * capabilities and arrival/departure of a TSM instance */ -static DECLARE_RWSEM (pci_tsm_rwsem); +DECLARE_RWSEM (pci_tsm_rwsem); +EXPORT_SYMBOL_GPL (pci_tsm_rwsem);/* * Count of TSMs registered that support physical link operations vs device
@@ -293,6 +294,26 @@ 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_set_ready() - Set when PF0 permits device assignment +* @ready: whether the device is ready for assignment +* +* pci_tsm_context_get() rejects requests with -EACCES while pf0->ready +* is false. A TSM driver can clear it during connect and set it after +* completing authentication, evidence acceptance and device setup. +* +* Hold pci_tsm_rwsem for write and pf0->lock through device setup and +* this update. pci_tsm_context_get() checks ready under pf0->lock, so +* device assignment cannot proceed before setup completes. +*/ +void pci_tsm_set_ready (struct pci_tsm_pf0 *pf0,bool ready) +{ +lockdep_assert_held_write (&pci_tsm_rwsem); +lockdep_assert_held (&pf0->lock); + pf0->ready = ready; +} +EXPORT_SYMBOL_GPL (pci_tsm_set_ready); +static ssize_t connect_store (struct device *dev,struct device_attribute *attr,const char *buf,size_t len) {
@@ -393,6 +414,12 @@ struct pci_tsm_context *pci_tsm_context_get(struct pci_dev *pdev, goto out_unlock_mutex; } +if (!pf0->ready) { +kfree (context); + context =ERR_PTR (-EACCES ); + goto out_unlock_mutex; + } +if (!pf0->dsm_context_users)reinit_completion (&pf0->dsm_context_drain_complete);
@@ -724,6 +751,8 @@ int pci_tsm_pf0_constructor(struct pci_dev *pdev, struct pci_tsm_pf0 *pf0,init_completion (&pf0->dsm_context_drain_complete); pf0->dsm_context_users =0 ; pf0->removing = false; +/* Default to ready for TSM drivers that complete setup during connect. */ + pf0->ready = true;/* * Note, low-level TSM driver responsible for determining if it wants to * proceed with a device that has no DOE mailbox. TSM may have an
include/linux/pci-tsm.h +10 -0
@@ -4,6 +4,7 @@#include <linux/completion.h> #include <linux/mutex.h> #include <linux/pci.h> +#include <linux/rwsem.h> #include <uapi/linux/iommufd.h> struct iommufd_viommu_ops ;
@@ -111,6 +112,7 @@ struct pci_tsm {* blocks link disconnect * @dsm_context_drain_complete: completed when the last context is released * @removing: reject new contexts while the DSM is being removed +* @ready: TSM driver has completed the checks and setup needed for assignment * @doe_mb: PCIe Data Object Exchange mailbox */ struct pci_tsm_pf0 {
@@ -119,6 +121,7 @@ struct pci_tsm_pf0 {unsigned int dsm_context_users;struct completion dsm_context_drain_complete;bool removing; +bool ready;struct pci_doe_mb *doe_mb; };
@@ -156,6 +159,7 @@ static inline bool is_pci_tsm_pf0(struct pci_dev *pdev) }#ifdef CONFIG_PCI_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); int pci_tsm_link_constructor(struct pci_dev *pdev, struct pci_tsm *tsm,
@@ -175,6 +179,10 @@ 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 void pci_tsm_set_ready (struct pci_tsm_pf0 *pf0,bool ready) +{ +} +static inline int pci_tsm_register (struct tsm_dev *tsm_dev) {return 0 ;
@@ -222,4 +230,6 @@ pci_tsm_context_match_device(struct pci_tsm_context *context,return false; }#endif +extern struct rw_semaphore pci_tsm_rwsem; +#endif /*__PCI_TSM_H */
Signature · 1 line
2.43.0
Aneesh Kumar K.V (Arm)
maintainerPATCH 5/10 PCI/TSM: Add association locking helpers for evidence acceptance
In reply to Aneesh Kumar K.V (Arm)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.
--- drivers/pci/tsm.c | 66 +++++++++++++++++++++++++++++++++++++++++ include/linux/pci-tsm.h | 17 +++++++++++ 2 files changed, 83 insertions(+)
drivers/pci/tsm.c +66 -0
@@ -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
include/linux/pci-tsm.h +17 -0
@@ -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 */
Signature · 1 line
2.43.0
Aneesh Kumar K.V (Arm)
maintainerPATCH 6/10 PCI/TSM: Notify TSM drivers before waiting for TSM user references
In reply to Aneesh Kumar K.V (Arm)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.
--- 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(-)
drivers/pci/tsm.c +47 -3
@@ -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 ;
drivers/virt/coco/tsm-core.c +2 -0
@@ -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. */
include/linux/pci-tsm.h +10 -0
@@ -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;
Signature · 1 line
2.43.0
Aneesh Kumar K.V (Arm)
maintainerPATCH 7/10 device core: Add helpers to record confidential device acceptance
In reply to Aneesh Kumar K.V (Arm)Add a flag to record whether a device is accepted for private memory access, with helpers to set, clear and read it. Setting or clearing the flag requires device::mutex and fails with -EBUSY if a driver is attached.
Follow-up patches will have TSM drivers mark devices accepted after verification and setup. PCI DMA setup will check this state before allowing the device to access private memory.
Based on the original patch from Dan Williams <[email protected]>.
--- MAINTAINERS | 1 + drivers/base/Kconfig | 4 +++ drivers/base/Makefile | 1 + drivers/base/base.h | 9 ++++++ drivers/base/coco.c | 68 ++++++++++++++++++++++++++++++++++++++++++ include/linux/device.h | 22 ++++++++++++++ 6 files changed, 105 insertions(+) create mode 100644 drivers/base/coco.c
MAINTAINERS +1 -0
@@ -28553,6 +28553,7 @@ T: git git://git.kernel.org/pub/scm/linux/kernel/git/tsm/linux.git F: Documentation/ABI/testing/configfs-tsm-report F: Documentation/driver-api/coco/ F: Documentation/driver-api/pci/tsm.rst +F: drivers/base/coco.c F: drivers/pci/tsm.c F: drivers/virt/coco/guest/ F: drivers/virt/coco/tsm-core.c
drivers/base/Kconfig +4 -0
@@ -242,4 +242,8 @@ config FW_DEVLINK_SYNC_STATE_TIMEOUT command line option on every system/board your kernel is expected to work on. +config CONFIDENTIAL_DEVICES + depends on ARCH_HAS_CC_PLATFORM + bool + endmenu
drivers/base/Makefile +1 -0
@@ -26,6 +26,7 @@ obj-$(CONFIG_DEV_COREDUMP) += devcoredump.o obj-$(CONFIG_GENERIC_MSI_IRQ) += platform-msi.o obj-$(CONFIG_GENERIC_ARCH_TOPOLOGY) += arch_topology.o obj-$(CONFIG_ACPI) += physical_location.o +obj-$(CONFIG_CONFIDENTIAL_DEVICES) += coco.o obj-y += test/
drivers/base/base.h +9 -0
@@ -108,8 +108,13 @@ struct driver_private {* @dead: This device is currently either in the process of or has been * removed from the system. Any asynchronous events scheduled for this * device should exit without taking any action. +* @cc_accepted: Device accepted for private memory access. Bus code checks +* this state before enabling DMA during driver attachment. * * Nothing outside of the driver core should ever touch these fields. +* +* All bitfield flags are manipulated under device_lock() to avoid +* read-modify-write collisions. */ struct device_private {struct klist klist_children;
@@ -122,6 +127,10 @@ struct device_private {char *deferred_probe_reason;struct device *device; u8 dead:1; +#ifdef CONFIG_CONFIDENTIAL_DEVICES + u8 cc_accepted:1; +#endif + };#define to_device_private_parent (obj) \ container_of(obj, struct device_private, knode_parent)
drivers/base/coco.c new file +68 -0
68 lines not on this page · raw message
include/linux/device.h +22 -0
@@ -1396,6 +1396,28 @@ static inline bool device_link_test(const struct device_link *link, u32 flags)return !!(link->flags & flags); } +/* Confidential Device state helpers */ +#ifdef CONFIG_CONFIDENTIAL_DEVICES +int device_cc_accept (struct device *dev); +int device_cc_reject (struct device *dev); +bool device_cc_accepted (struct device *dev); +#else +static inline int device_cc_accept (struct device *dev) +{ +lockdep_assert_held (&dev->mutex); +return 0 ; +} +static inline int device_cc_reject (struct device *dev) +{ +lockdep_assert_held (&dev->mutex); +return 0 ; +} +static inline bool device_cc_accepted (struct device *dev) +{ +return false; +} +#endif /* CONFIG_CONFIDENTIAL_DEVICES */ +/* Create alias, so I can be autoloaded. */ #define MODULE_ALIAS_CHARDEV (major,minor) \ MODULE_ALIAS("char-major-" __stringify(major) "-" __stringify(minor))
Signature · 1 line
2.43.0
Aneesh Kumar K.V (Arm)
maintainerPATCH 8/10 PCI/TSM: Add sysfs lock and unlock for guest-assigned devices
In reply to Aneesh Kumar K.V (Arm)A confidential guest needs to lock a PCI device before collecting and verifying evidence about it. Userspace also needs a way to unlock the device after use or when verification fails.
Add tsm/lock and tsm/unlock sysfs attributes for TSM drivers implementing devsec_ops. Writing a TSM device name to tsm/lock calls its lock callback. Unlock calls the matching driver's unlock callback
Take device::mutex to prevent driver attachment, then pci_tsm_rwsem for write to protect pci_dev::tsm during either operation. This order matches PCI DMA preparation, which runs with the device mutex held. Return -EBUSY if a PCI driver is already attached. Also call unlock during PCI device removal or TSM unregister, using the same lock order. Guest TSM unregister processes one PCI device at a time and releases both locks before moving to the next device.
Require ARCH_HAS_CC_PLATFORM and select CONFIDENTIAL_DEVICES so pci_tsm_unlock() clears the device's accepted state.
Expose the TSM class device's pci_mode attribute so userspace can distinguish host link TSM from guest device-security TSM. Evidence retrieval and acceptance are handled separately through the attestation subsystem.
Based on the original Device Security LOCK patch from
Dan Williams <[email protected]>, adapted to the current PCI/TSM structures and locking rules.
--- Documentation/ABI/testing/sysfs-bus-pci | 35 ++- Documentation/ABI/testing/sysfs-class-tsm | 19 ++ Documentation/driver-api/pci/tsm.rst | 47 ++++ drivers/pci/Kconfig | 2 + drivers/pci/tsm.c | 247 ++++++++++++++++++++-- drivers/virt/coco/tsm-core.c | 48 ++++- include/linux/pci-tsm.h | 21 +- 7 files changed, 401 insertions(+), 18 deletions(-)
Documentation/ABI/testing/sysfs-bus-pci +33 -2
@@ -653,14 +653,17 @@ Description: Encryption) establishment. Reads from this attribute return the name of the connected TSM or the empty string if not connected. A TSM device signals its readiness to accept PCI - connection via a KOBJ_CHANGE event. + connection via a KOBJ_CHANGE event. This is a "link" TSM + attribute, see Documentation/ABI/testing/sysfs-class-tsm. What: /sys/bus/pci/devices/.../tsm/disconnect Contact: [email protected] Description: (WO) Write the name of the TSM device that was specified to 'connect' to teardown the connection. - The write fails with EBUSY if the TSM is in use. + The write fails with EBUSY if the TSM is in use. This is a + "link" TSM attribute, see + Documentation/ABI/testing/sysfs-class-tsm. What: /sys/bus/pci/devices/.../tsm/dsm Contact: [email protected]
@@ -695,3 +698,31 @@ Description: When present and the tsm/ attribute directory is present, the authenticated attribute is an alias for the device 'connect' state. See the 'tsm/connect' attribute for more details. + This is a "link" TSM attribute, see + Documentation/ABI/testing/sysfs-class-tsm. + +What: /sys/bus/pci/devices/.../tsm/lock +Contact: [email protected] +Description: + (RW) Write the name of a TSM (TEE Security Manager) device + instance to this file to request that the platform transition + the PCIe device configuration to the TDISP LOCKED state. This + puts the device in a state where security sensitive + configuration settings cannot be changed without transitioning + the device to the PCIe TDISP ERROR state. Reads from this attribute + return the name of the TSM device instance that has arranged for + the device to be locked, or the empty string if not locked. A + TSM class device signals its readiness for lock requests via a + KOBJ_CHANGE event. Writes fail with EBUSY if this device is + bound to a driver. This is a "devsec" TSM attribute, see + Documentation/ABI/testing/sysfs-class-tsm. See + Documentation/driver-api/pci/tsm.rst for more details. + +What: /sys/bus/pci/devices/.../tsm/unlock +Contact: [email protected] +Description: + (WO) Write the name of the TSM device that was specified to + 'lock' to unlock the device configuration and release the TSM's + per-device state. Writes fail with EBUSY if this device is bound + to a driver. This is a "devsec" TSM + attribute, see Documentation/ABI/testing/sysfs-class-tsm.
Documentation/ABI/testing/sysfs-class-tsm +19 -0
@@ -20,3 +20,22 @@ Description: Documentation/ABI/testing/sysfs-devices-pci-host-bridge for the description of the pciDDDD:BB/streamH.R.E symlink and the pciDDDD:BB/available_secure_streams attribute. + +What: /sys/class/tsm/tsmN/pci_mode +Contact: [email protected] +Description: + (RO) A TSM with PCIe TDISP capability can be in one of two + modes. + + "link": typically for a hypervisor (VMM) to authenticate, + establish a secure session, and setup link + encryption. + + "devsec": typically for a confidential guest (TVM) to + transition assigned devices through the TDISP + state machine UNLOCKED->LOCKED->RUN. + + See the "tsm/" entries in + Documentation/ABI/testing/sysfs-bus-pci for the available PCI + device attributes when a TSM with the given "pci_mode" is + registered.
Documentation/driver-api/pci/tsm.rst +47 -0
53 lines not on this page · raw message
drivers/pci/Kconfig +2 -0
@@ -126,6 +126,8 @@ config PCI_IDE config PCI_TSM bool "PCI TSM: Device security protocol support" + depends on ARCH_HAS_CC_PLATFORM + select CONFIDENTIAL_DEVICES select PCI_IDE select PCI_DOE select TSM
drivers/pci/tsm.c +234 -13
331 lines not on this page · raw message
drivers/virt/coco/tsm-core.c +46 -2
66 lines not on this page · raw message
include/linux/pci-tsm.h +20 -1
@@ -78,7 +78,8 @@ struct pci_tsm_ops {* @unlock: destroy TSM context and return device to UNLOCKED state * * Context: @lock and @unlock run under pci_tsm_rwsem held for write to -* sync with TSM unregistration and each other +* sync with TSM unregistration and each other. All operations run under +* the device lock for mutual exclusion with driver attach and detach. */ struct_group_tagged (pci_tsm_devsec_ops, devsec_ops, struct pci_tsm *(*lock)(struct tsm_dev *tsm_dev,
@@ -103,6 +104,13 @@ struct pci_tsm_ops {* sub-function (SR-IOV virtual function, or non-function0 * multifunction-device), or a downstream endpoint (PCIe upstream switch-port as * DSM). +* +* For devsec operations it serves to indicate that the function / TDI has been +* locked to a given TSM. +* +* The common expectation is that there is only ever one TSM, but this is not +* enforced. The implementation only enforces that a device can be "connected" +* to a TSM instance or "locked" to a different TSM. */ struct pci_tsm {struct pci_dev *pdev;
@@ -131,6 +139,14 @@ struct pci_tsm_pf0 {struct pci_doe_mb *doe_mb; }; +/** +* struct pci_tsm_devsec - context for tracking private/accepted PCI resources +* @base_tsm: generic core "tsm" context +*/ +struct pci_tsm_devsec { +struct pci_tsm base_tsm; +}; +/* physical function0 and capable of 'connect' */ static inline bool is_pci_tsm_pf0 (struct pci_dev *pdev) {
@@ -175,6 +191,8 @@ 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,struct tsm_dev *tsm_dev); +int pci_tsm_devsec_constructor (struct pci_dev *pdev,struct pci_tsm_devsec *devsec, +struct tsm_dev *tsm_dev);void pci_tsm_pf0_destructor (struct pci_tsm_pf0 *pf0);int pci_tsm_doe_transfer (struct pci_dev *pdev,u8 type,const void *req,size_t req_sz,void *resp,size_t resp_sz);
@@ -187,6 +205,7 @@ struct pci_dev *pci_tsm_context_dsm_dev(struct pci_tsm_context *context);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); +struct pci_tsm_devsec *to_pci_tsm_devsec (struct pci_tsm *tsm);#else static inline struct pci_tsm *pci_tsm_trylock_association (struct pci_tsm *tsm) {
Signature · 1 line
2.43.0
Aneesh Kumar K.V (Arm)
maintainerPATCH 9/10 PCI/TSM: Build encrypted MMIO descriptors from a supplied report
In reply to Aneesh Kumar K.V (Arm)The TDISP Device Interface Report describes a device interface's configuration, including its MMIO ranges and their attributes. Each MMIO range identifies its BAR, starting physical address and size.
The device adds the MMIO Reporting Offset (MRO) to the physical addresses included in the report. When the MRO is a multiple of the BAR size, masking the reported address by the BAR size recovers the offset within that BAR. Combining this offset with the guest's PCI resource start gives the corresponding guest physical address.
TSM drivers need to parse these reports and identify the MMIO ranges requiring private mappings. Provide common PCI/TSM helpers so each driver does not have to implement the same parsing and resource setup.
Add pci_tsm_mmio_alloc_report() to parse a caller-supplied report. Record each range's guest physical address and retain its reported address for firmware mapping validation. Skip non-TEE and currently unsupported MSI-X ranges.
Add helpers to register the ranges in iomem with IORES_DESC_ENCRYPTED, Setup requires device::mutex and rejects devices with a PCI driver attached.
A later guest attestation patch uses these helpers during evidence-accept. The caller remains responsible for verifying the report and validating the MMIO mappings before allowing device use.
Based on the original patch from Dan Williams <[email protected]>
--- drivers/pci/tsm.c | 227 ++++++++++++++++++++++++++++++++++++++++ include/linux/ioport.h | 1 + include/linux/pci-tsm.h | 35 +++++++ 3 files changed, 263 insertions(+)
drivers/pci/tsm.c +227 -0
@@ -21,6 +21,7 @@#include <linux/slab.h> #include <linux/sysfs.h> #include <linux/tsm.h> +#include <linux/unaligned.h> #include <linux/xarray.h> #include "pci.h"
@@ -650,6 +651,232 @@ static ssize_t dsm_show(struct device *dev, struct device_attribute *attr, }static DEVICE_ATTR_RO (dsm); +static void mmio_teardown (struct pci_tsm_mmio *mmio,int nr) +{ +while (nr--) +remove_resource (pci_tsm_mmio_resource (mmio, nr)); +} + +/** +* pci_tsm_mmio_setup() - mark device MMIO as encrypted in iomem +* @pdev: device owner of MMIO resources +* @mmio: container of an array of resources to mark encrypted +*/ +int pci_tsm_mmio_setup (struct pci_dev *pdev,struct pci_tsm_mmio *mmio) +{ +int i; + +device_lock_assert (&pdev->dev); +if (pdev->dev.driver) +return -EBUSY ; + +for (i =0 ; i < mmio->nr; i++) { +struct resource *res =pci_tsm_mmio_resource (mmio, i); +int j; + +if (resource_size (res) ==0 || !res->end) +break ; + +/* Only require the caller to set the range, init remainder */ + *res =DEFINE_RES_NAMED_DESC (res->start,resource_size (res), +"PCI MMIO Encrypted" , +IORESOURCE_MEM , +IORES_DESC_ENCRYPTED ); + +for (j =0 ; j <PCI_NUM_RESOURCES ; j++) +if (resource_contains (pci_resource_n (pdev, j), res)) +break ; + +/* Request is outside of device MMIO */ +if (j >=PCI_NUM_RESOURCES ) +break ; + +if (insert_resource (&iomem_resource, res) !=0 ) +break ; + } + +if (i >= mmio->nr) +return 0 ; + +mmio_teardown (mmio, i); + +return -EINVAL ; +} +EXPORT_SYMBOL_GPL (pci_tsm_mmio_setup); + +void pci_tsm_mmio_teardown (struct pci_tsm_mmio *mmio) +{ +mmio_teardown (mmio, mmio->nr); +} +EXPORT_SYMBOL_GPL (pci_tsm_mmio_teardown); + +/* +* PCIe ECN TEE Device Interface Security Protocol (TDISP) +* +* Device Interface Report data object layout as defined by PCIe r7.0 section +* 11.3.11 +*/ +#define PCI_TSM_DEVIF_REPORT_MMIO_ATTR_MSIX_TABLE BIT(0) +#define PCI_TSM_DEVIF_REPORT_MMIO_ATTR_MSIX_PBA BIT(1) +#define PCI_TSM_DEVIF_REPORT_MMIO_ATTR_IS_NON_TEE BIT(2) +#define PCI_TSM_DEVIF_REPORT_MMIO_ATTR_IS_UPDATABLE BIT(3) +#define PCI_TSM_DEVIF_REPORT_MMIO_ATTR_RANGE_ID GENMASK(31, 16) + +/* An interface report 'pfn' is 4K in size */ +struct pci_tsm_devif_mmio { +__le64 pfn; +__le32 nr_pfns; +__le32 attributes; +}; + +struct pci_tsm_devif_report { +__le16 interface_info; +__le16 reserved; +__le16 msi_x_message_control; +__le16 lnr_control; +__le32 tph_control; +__le32 mmio_range_count; +struct pci_tsm_devif_mmio mmio[]; +}; + +/** +* pci_tsm_mmio_alloc_report() - allocate encrypted MMIO range descriptor +* @pdev: device owner of MMIO ranges +* @report_data: TDISP Device Interface (DevIf) Report blob +* @report_sz: DevIf Report size +* +* Return: the encrypted MMIO range descriptor on success, NULL on failure +* +* The caller keeps pdev->tsm valid throughout this call. +*/ +struct pci_tsm_mmio *pci_tsm_mmio_alloc_report (struct pci_dev *pdev, +const void *report_data,size_t report_sz) +{ +struct pci_tsm *tsm = pdev->tsm; +struct tsm_dev *tsm_dev = tsm->tsm_dev; +u64 last_reporting_end; +const struct pci_tsm_devif_report *report; +u32 mmio_range_count; +int last_bar =-1 ; +int i; + +if (report_sz <sizeof (struct pci_tsm_devif_report )) +return NULL ; + +if (dev_WARN_ONCE (&tsm_dev->dev, !IS_ALIGNED ((unsigned long ) report_data,8 ), +"misaligned report data\n" )) +return NULL ; + + report = report_data; + mmio_range_count =__le32_to_cpu (report->mmio_range_count); + +/* check that the report object is self-consistent on mmio entries */ +if (report_sz <struct_size (report, mmio, mmio_range_count)) +return NULL ; + +/* create pci_tsm_mmio descriptors from the report data */ + struct pci_tsm_mmio *mmio __free(kfree) = +kzalloc (struct_size (mmio, mmio, mmio_range_count),GFP_KERNEL ); +if (!mmio) +return NULL ; + +for (i =0 ; i < mmio_range_count; i++) { +u64 range_off; +struct range range; +const struct pci_tsm_devif_mmio *mmio_data = &report->mmio[i]; +struct pci_tsm_mmio_entry *entry; +/* report values in are in terms of 4K pages */ +u64 tsm_offset =__le64_to_cpu (mmio_data->pfn) *SZ_4K ; +u64 size = (u64 )__le32_to_cpu (mmio_data->nr_pfns) *SZ_4K ; +u32 attr =__le32_to_cpu (mmio_data->attributes); +int bar =FIELD_GET (PCI_TSM_DEVIF_REPORT_MMIO_ATTR_RANGE_ID , + attr); + +if (bar >=PCI_STD_NUM_BARS || + !(pci_resource_flags (pdev, bar) &IORESOURCE_MEM )) { +pci_dbg (pdev,"Invalid reporting bar ID %d\n" , bar); +return NULL ; + } + +if (last_bar > bar) { +pci_dbg (pdev,"Reporting bar ID not in ascending order\n" ); +return NULL ; + } + +if (last_bar < bar) { +/* transition to a new bar */ + last_bar = bar; + }else if (tsm_offset < last_reporting_end) { +pci_dbg (pdev,"Reporting ranges within BAR not in ascending order\n" ); +return NULL ; + } + + last_reporting_end = tsm_offset + size; +if (last_reporting_end < tsm_offset) { +pci_dbg (pdev,"Reporting range overflow\n" ); +return NULL ; + } + + range_off = tsm_offset & (pci_resource_len (pdev, bar) -1 ); + +if (pci_resource_len (pdev, bar) < range_off + size) { +pci_dbg (pdev,"Reporting range larger than BAR size\n" ); +return NULL ; + } + + range.start =pci_resource_start (pdev, bar) + range_off; + range.end = range.start + size -1 ; + +if (FIELD_GET (PCI_TSM_DEVIF_REPORT_MMIO_ATTR_IS_NON_TEE , + attr)) { +pci_dbg (pdev,"Skipping non-TEE range, BAR%d %pra\n" , + bar, &range); +continue ; + } + +/* Currently not supported */ +if (FIELD_GET (PCI_TSM_DEVIF_REPORT_MMIO_ATTR_MSIX_TABLE , + attr) || +FIELD_GET (PCI_TSM_DEVIF_REPORT_MMIO_ATTR_MSIX_PBA , attr)) { +pci_dbg (pdev,"Skipping MSIX range BAR%d %pra\n" , bar, + &range); +continue ; + } + +/* Update __counted_by(nr) before accessing the new entry. */ + mmio->nr++; + entry =pci_tsm_mmio_entry (mmio, mmio->nr -1 ); + entry->res.start = range.start; + entry->res.end = range.end; + entry->tsm_offset = tsm_offset; + } + +return_ptr (mmio); +} +EXPORT_SYMBOL_GPL (pci_tsm_mmio_alloc_report); + +/** +* pci_tsm_mmio_free() - free a pci_tsm_mmio instance +* @pdev: device owner of MMIO ranges +* @mmio: instance to free +* +* Returns 0 if @mmio was idle on entry, -EBUSY otherwise +*/ +int pci_tsm_mmio_free (struct pci_dev *pdev,struct pci_tsm_mmio *mmio) +{ +for (int i =0 ; i < mmio->nr; i++) { +struct resource *res =pci_tsm_mmio_resource (mmio, i); + +if (dev_WARN_ONCE (&pdev->dev,resource_assigned (res), +"MMIO resource still assigned %pr\n" , res)) +return -EBUSY ; + } + +kfree (mmio); +return 0 ; +} +EXPORT_SYMBOL_GPL (pci_tsm_mmio_free); +/** * pci_tsm_unlock() - Transition TDI from LOCKED/RUN to UNLOCKED * @pdev: TDI device to unlock
include/linux/ioport.h +1 -0
@@ -144,6 +144,7 @@ enum {IORES_DESC_RESERVED =7 ,IORES_DESC_SOFT_RESERVED =8 ,IORES_DESC_CXL =9 , + IORES_DESC_ENCRYPTED = 10, };/*
include/linux/pci-tsm.h +35 -0
@@ -139,12 +139,42 @@ struct pci_tsm_pf0 {struct pci_doe_mb *doe_mb; }; +/** +* struct pci_tsm_mmio_entry - an encrypted MMIO range +* @res: MMIO address range (typically Guest Physical Address, GPA) +* @tsm_offset: Host Physical Address, HPA obfuscation offset added by the TSM. +* Translates report addresses to GPA. +*/ +struct pci_tsm_mmio_entry { +struct resource res; +u64 tsm_offset; +}; + +struct pci_tsm_mmio { +int nr; + struct pci_tsm_mmio_entry mmio[] __counted_by(nr); +}; + +static inline struct pci_tsm_mmio_entry * +pci_tsm_mmio_entry (struct pci_tsm_mmio *mmio,int idx) +{ +return &mmio->mmio[idx]; +} + +static inline struct resource *pci_tsm_mmio_resource (struct pci_tsm_mmio *mmio, +int idx) +{ +return &mmio->mmio[idx].res; +} +/** * struct pci_tsm_devsec - context for tracking private/accepted PCI resources * @base_tsm: generic core "tsm" context +* @mmio: encrypted MMIO resources for this assigned device */ struct pci_tsm_devsec {struct pci_tsm base_tsm; +struct pci_tsm_mmio *mmio; };/* physical function0 and capable of 'connect' */
@@ -206,6 +236,11 @@ 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);struct pci_tsm_devsec *to_pci_tsm_devsec (struct pci_tsm *tsm); +int pci_tsm_mmio_setup (struct pci_dev *pdev,struct pci_tsm_mmio *mmio); +void pci_tsm_mmio_teardown (struct pci_tsm_mmio *mmio); +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 struct pci_tsm *pci_tsm_trylock_association (struct pci_tsm *tsm) {
Signature · 1 line
2.43.0
Aneesh Kumar K.V (Arm)
maintainerPATCH 10/10 PCI/TSM: Configure accepted device DMA during driver attachment
In reply to Aneesh Kumar K.V (Arm)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.
--- 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(-)
drivers/pci/pci-driver.c +30 -1
@@ -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); }
drivers/pci/tsm.c +89 -0
101 lines not on this page · raw message
include/linux/pci-tsm.h +32 -3
@@ -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 );
include/linux/pci.h +1 -0
@@ -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 */
Signature · 1 line
2.43.0
Greg Kroah-Hartman
maintainerQuoted 14 lines · Aneesh Kumar K.V (Arm)
This series adds the PCI/TSM support needed by a TEE Security Manager (TSM) driver in a confidential guest. It adds device lock/unlock, helpers for recording device acceptance and parsing MMIO reports, and DMA callbacks used during PCI driver attachment. The guest TSM driver and attestation commands using these interfaces are in follow-up series.
This series is based on Dan Williams' "[PATCH v2 00/19] PCI/TSM: TEE I/O infrastructure" posting [1], particularly the guest LOCK/UNLOCK, ACCEPT and MMIO report work [2-4]. It retains
device_cc_accepted()and the confidential-device acceptance model from that posting, with changes adapted to the current PCI/TSM structures and reference APIs. Dan's device-core acceptance helper and per-host-bridge IDE stream reporting patch are also included.
Quoted 5 lines
This series does not adopt the Device Trust changes from Dan's "Device Evidence and Trust for PCI Security Protocol (TDISP)" [5]. That work is being developed separately in Leon Romanovsky's "driver core: Authorize device trust before driver binding" series, [6]. Confidential-device acceptance is kept separate from that driver-binding trust policy.
But it should not, as you are making driver core changes that will mirror the same thing that that patchset is attempting to do. Please work together on this.
thanks,
greg k-h
Greg Kroah-Hartman
maintainerRe: [PATCH v1 07/10] device core: Add helpers to record confidential device acceptance
In reply to Aneesh Kumar K.V (Arm)Quoted 63 lines · Aneesh Kumar K.V (Arm)
Add a flag to record whether a device is accepted for private memory access, with helpers to set, clear and read it. Setting or clearing the flag requires
device::mutexand fails with-EBUSYif a driver is attached.Follow-up patches will have TSM drivers mark devices accepted after verification and setup. PCI DMA setup will check this state before allowing the device to access private memory.
Based on the original patch from Dan Williams <[email protected]>.
Signed-off-by Aneesh Kumar K.V (Arm) <[email protected]>--- MAINTAINERS | 1 + drivers/base/Kconfig | 4 +++ drivers/base/Makefile | 1 + drivers/base/base.h | 9 ++++++ drivers/base/coco.c | 68 ++++++++++++++++++++++++++++++++++++++++++ include/linux/device.h | 22 ++++++++++++++ 6 files changed, 105 insertions(+) create mode 100644 drivers/base/coco.cMAINTAINERS +1
-0@@ -28553,6 +28553,7 @@ T: git git://git.kernel.org/pub/scm/linux/kernel/git/tsm/linux.git F: Documentation/ABI/testing/configfs-tsm-report F: Documentation/driver-api/coco/ F: Documentation/driver-api/pci/tsm.rst +F: drivers/base/coco.c F: drivers/pci/tsm.c F: drivers/virt/coco/guest/ F: drivers/virt/coco/tsm-core.cdrivers/base/Kconfig +4
-0@@ -242,4 +242,8 @@ config FW_DEVLINK_SYNC_STATE_TIMEOUT command line option on every system/board your kernel is expected to work on. +config CONFIDENTIAL_DEVICES + depends on ARCH_HAS_CC_PLATFORM + bool + endmenudrivers/base/Makefile +1
-0@@ -26,6 +26,7 @@ obj-$(CONFIG_DEV_COREDUMP) += devcoredump.o obj-$(CONFIG_GENERIC_MSI_IRQ) += platform-msi.o obj-$(CONFIG_GENERIC_ARCH_TOPOLOGY) += arch_topology.o obj-$(CONFIG_ACPI) += physical_location.o +obj-$(CONFIG_CONFIDENTIAL_DEVICES) += coco.o obj-y += test/drivers/base/base.h +0
-0
drivers/base/base.h struct driver_private { · line 112
Quoted 5 lines
drivers/base/base.h +2
-0@@ -108,8 +108,13 @@ struct driver_private {* @dead: This device is currently either in the process of or has been * removed from the system. Any asynchronous events scheduled for this * device should exit without taking any action. +* @cc_accepted: Device accepted for private memory access. Bus code checks +* this state before enabling DMA during driver attachment.
No, please do not do this, make this a "state" like the trusted device series does.
drivers/base/base.h struct driver_private { · line 117
Quoted 5 lines
drivers/base/base.h +3
-0* * Nothing outside of the driver core should ever touch these fields. +* +* All bitfield flags are manipulated under device_lock() to avoid +* read-modify-write collisions.
Why is this added now?
drivers/base/base.h struct device_private { · line 131
Quoted 9 lines
drivers/base/base.h +2
-0*/ struct device_private {struct klist klist_children;@@ -122,6 +127,10 @@ struct device_private {char *deferred_probe_reason;struct device *device; u8 dead:1; +#ifdef CONFIG_CONFIDENTIAL_DEVICES + u8 cc_accepted:1;
It's a bitfield, just always use it. But again, this isn't going to be needed.
thanks,
greg k-h
Quoted 21 lines · Greg Kroah-Hartman
Quoted 14 lines · Aneesh Kumar K.V (Arm)
This series adds the PCI/TSM support needed by a TEE Security Manager (TSM) driver in a confidential guest. It adds device lock/unlock, helpers for recording device acceptance and parsing MMIO reports, and DMA callbacks used during PCI driver attachment. The guest TSM driver and attestation commands using these interfaces are in follow-up series.
This series is based on Dan Williams' "[PATCH v2 00/19] PCI/TSM: TEE I/O infrastructure" posting [1], particularly the guest LOCK/UNLOCK, ACCEPT and MMIO report work [2-4]. It retains
device_cc_accepted()and the confidential-device acceptance model from that posting, with changes adapted to the current PCI/TSM structures and reference APIs. Dan's device-core acceptance helper and per-host-bridge IDE stream reporting patch are also included.Quoted 5 lines
This series does not adopt the Device Trust changes from Dan's "Device Evidence and Trust for PCI Security Protocol (TDISP)" [5]. That work is being developed separately in Leon Romanovsky's "driver core: Authorize device trust before driver binding" series, [6]. Confidential-device acceptance is kept separate from that driver-binding trust policy.
Quoted 3 lines
But it should not, as you are making driver core changes that will mirror the same thing that that patchset is attempting to do. Please work together on this.
+10000000,
Greg,
This series came as a surprise to me given the fact that device trust series is exactly for that - allow confidential computing.
Adversarial is equal to the "cc_accepted".
Thanks
Quoted 4 lines
thanks,
greg k-h
Aneesh Kumar K.V
maintainerQuoted 26 lines · Leon Romanovsky
Quoted 21 lines · Greg Kroah-Hartman
Quoted 14 lines · Aneesh Kumar K.V (Arm)
This series adds the PCI/TSM support needed by a TEE Security Manager (TSM) driver in a confidential guest. It adds device lock/unlock, helpers for recording device acceptance and parsing MMIO reports, and DMA callbacks used during PCI driver attachment. The guest TSM driver and attestation commands using these interfaces are in follow-up series.
This series is based on Dan Williams' "[PATCH v2 00/19] PCI/TSM: TEE I/O infrastructure" posting [1], particularly the guest LOCK/UNLOCK, ACCEPT and MMIO report work [2-4]. It retains
device_cc_accepted()and the confidential-device acceptance model from that posting, with changes adapted to the current PCI/TSM structures and reference APIs. Dan's device-core acceptance helper and per-host-bridge IDE stream reporting patch are also included.Quoted 5 lines
This series does not adopt the Device Trust changes from Dan's "Device Evidence and Trust for PCI Security Protocol (TDISP)" [5]. That work is being developed separately in Leon Romanovsky's "driver core: Authorize device trust before driver binding" series, [6]. Confidential-device acceptance is kept separate from that driver-binding trust policy.
Quoted 3 lines
But it should not, as you are making driver core changes that will mirror the same thing that that patchset is attempting to do. Please work together on this.
Quoted 8 lines
+10000000,
Greg,
This series came as a surprise to me given the fact that device trust series is exactly for that - allow confidential computing.
Adversarial is equal to the "
cc_accepted".
I think the confusion stems from this thread:
https://lore.kernel.org/all/[email protected]
Based on that discussion, I understood TRUST and eligibility for private DMA to be complementary attributes. Device acceptance determines whether a device may access private memory, while trust determines whether the core must enforce additional restrictions. I understood that supporting an accepted but adversarial device would require either a private IOTLB to restrict its access or an IOMMU.
Therefore, with this series, a request to enable private memory DMA is handled as follows:
if (!device_cc_accepted(&pdev->dev)) {
ret = -EACCES;
goto out;
}
/* Adversarial devices are unsupported for confidential DMA. */
if (device_is_adversarial(&pdev->dev)) {
ret = -EOPNOTSUPP;
goto out;
}-aneesh
Quoted 39 lines · Aneesh Kumar K.V
Quoted 26 lines · Leon Romanovsky
Quoted 21 lines · Greg Kroah-Hartman
Quoted 14 lines · Aneesh Kumar K.V (Arm)
This series adds the PCI/TSM support needed by a TEE Security Manager (TSM) driver in a confidential guest. It adds device lock/unlock, helpers for recording device acceptance and parsing MMIO reports, and DMA callbacks used during PCI driver attachment. The guest TSM driver and attestation commands using these interfaces are in follow-up series.
This series is based on Dan Williams' "[PATCH v2 00/19] PCI/TSM: TEE I/O infrastructure" posting [1], particularly the guest LOCK/UNLOCK, ACCEPT and MMIO report work [2-4]. It retains
device_cc_accepted()and the confidential-device acceptance model from that posting, with changes adapted to the current PCI/TSM structures and reference APIs. Dan's device-core acceptance helper and per-host-bridge IDE stream reporting patch are also included.Quoted 5 lines
This series does not adopt the Device Trust changes from Dan's "Device Evidence and Trust for PCI Security Protocol (TDISP)" [5]. That work is being developed separately in Leon Romanovsky's "driver core: Authorize device trust before driver binding" series, [6]. Confidential-device acceptance is kept separate from that driver-binding trust policy.
Quoted 3 lines
But it should not, as you are making driver core changes that will mirror the same thing that that patchset is attempting to do. Please work together on this.
Quoted 8 lines
+10000000,
Greg,
This series came as a surprise to me given the fact that device trust series is exactly for that - allow confidential computing.
Adversarial is equal to the "
cc_accepted".I think the confusion stems from this thread:
Quoted 8 lines
https://lore.kernel.org/all/[email protected]
Based on that discussion, I understood TRUST and eligibility for private DMA to be complementary attributes. Device acceptance determines whether a device may access private memory, while trust determines whether the core must enforce additional restrictions. I understood that supporting an accepted but adversarial device would require either a private IOTLB to restrict its access or an IOMMU.
I reached a completely different conclusion from that discussion.
Device trust does little beyond preventing driver binding and recording the trust decision in "struct device". The missing piece, which I would expect you to address, is ensuring that the IOMMU and private DMA are configured only after the trust level has been decided or as part of that decision, rather than beforehand, as they are today.
Thanks
Quoted 4 lines
Therefore, with this series, a request to enable private memory DMA is handled as follows:
Quoted 10 lines
if (!device_cc_accepted(&pdev->dev)) {
ret = -EACCES;
goto out;
}
/* Adversarial devices are unsupported for confidential DMA. */
if (device_is_adversarial(&pdev->dev)) {
ret = -EOPNOTSUPP;
goto out;
}The snippet above clearly shows why it is a good idea to wait until support for device trust levels is accepted. An adversarial device is untrusted from the PCI perspective and must have private DMA and a separate IOMMU group.
Thanks
Quoted 4 lines
-aneesh
Aneesh Kumar K.V
maintainerQuoted 49 lines · Leon Romanovsky
Quoted 39 lines · Aneesh Kumar K.V
Quoted 26 lines · Leon Romanovsky
Quoted 21 lines · Greg Kroah-Hartman
Quoted 14 lines · Aneesh Kumar K.V (Arm)
This series adds the PCI/TSM support needed by a TEE Security Manager (TSM) driver in a confidential guest. It adds device lock/unlock, helpers for recording device acceptance and parsing MMIO reports, and DMA callbacks used during PCI driver attachment. The guest TSM driver and attestation commands using these interfaces are in follow-up series.
This series is based on Dan Williams' "[PATCH v2 00/19] PCI/TSM: TEE I/O infrastructure" posting [1], particularly the guest LOCK/UNLOCK, ACCEPT and MMIO report work [2-4]. It retains
device_cc_accepted()and the confidential-device acceptance model from that posting, with changes adapted to the current PCI/TSM structures and reference APIs. Dan's device-core acceptance helper and per-host-bridge IDE stream reporting patch are also included.Quoted 5 lines
This series does not adopt the Device Trust changes from Dan's "Device Evidence and Trust for PCI Security Protocol (TDISP)" [5]. That work is being developed separately in Leon Romanovsky's "driver core: Authorize device trust before driver binding" series, [6]. Confidential-device acceptance is kept separate from that driver-binding trust policy.
Quoted 3 lines
But it should not, as you are making driver core changes that will mirror the same thing that that patchset is attempting to do. Please work together on this.
Quoted 8 lines
+10000000,
Greg,
This series came as a surprise to me given the fact that device trust series is exactly for that - allow confidential computing.
Adversarial is equal to the "
cc_accepted".I think the confusion stems from this thread:
Quoted 8 lines
https://lore.kernel.org/all/[email protected]
Based on that discussion, I understood TRUST and eligibility for private DMA to be complementary attributes. Device acceptance determines whether a device may access private memory, while trust determines whether the core must enforce additional restrictions. I understood that supporting an accepted but adversarial device would require either a private IOTLB to restrict its access or an IOMMU.
Quoted 8 lines
I reached a completely different conclusion from that discussion.
Device trust does little beyond preventing driver binding and recording the trust decision in "struct device". The missing piece, which I would expect you to address, is ensuring that the IOMMU and private DMA are configured only after the trust level has been decided or as part of that decision, rather than beforehand, as they are today.
I agree that the trust decision must precede DMA configuration and driver probe. My integration tree shared in the cover letter already includes your trust framework, with PCI/TSM DMA setup following that decision.
What I am still trying to understand is how you view the DMA allocation and mapping paths determining whether a device can access private memory. Paths such as force_dma_unencrypted() and SWIOTLB receive a struct device pointer and need to decide whether to use private memory or shared buffers.
We could move cc_accepted into struct pci_tsm_devsec, outside driver core. How would you expect the generic DMA code to obtain that information? Adding another flag to struct device would effectively recreate the state we are trying to remove.
This goes back to my question here:
https://lore.kernel.org/all/[email protected]/
If we remove device_cc_accepted(), what should those callers use instead? How do you see your trust framework supporting that decision?
-aneesh
Quoted 66 lines · Aneesh Kumar K.V
Quoted 49 lines · Leon Romanovsky
Quoted 39 lines · Aneesh Kumar K.V
Quoted 26 lines · Leon Romanovsky
Quoted 21 lines · Greg Kroah-Hartman
Quoted 14 lines · Aneesh Kumar K.V (Arm)
This series adds the PCI/TSM support needed by a TEE Security Manager (TSM) driver in a confidential guest. It adds device lock/unlock, helpers for recording device acceptance and parsing MMIO reports, and DMA callbacks used during PCI driver attachment. The guest TSM driver and attestation commands using these interfaces are in follow-up series.
This series is based on Dan Williams' "[PATCH v2 00/19] PCI/TSM: TEE I/O infrastructure" posting [1], particularly the guest LOCK/UNLOCK, ACCEPT and MMIO report work [2-4]. It retains
device_cc_accepted()and the confidential-device acceptance model from that posting, with changes adapted to the current PCI/TSM structures and reference APIs. Dan's device-core acceptance helper and per-host-bridge IDE stream reporting patch are also included.Quoted 5 lines
This series does not adopt the Device Trust changes from Dan's "Device Evidence and Trust for PCI Security Protocol (TDISP)" [5]. That work is being developed separately in Leon Romanovsky's "driver core: Authorize device trust before driver binding" series, [6]. Confidential-device acceptance is kept separate from that driver-binding trust policy.
Quoted 3 lines
But it should not, as you are making driver core changes that will mirror the same thing that that patchset is attempting to do. Please work together on this.
Quoted 8 lines
+10000000,
Greg,
This series came as a surprise to me given the fact that device trust series is exactly for that - allow confidential computing.
Adversarial is equal to the "
cc_accepted".I think the confusion stems from this thread:
Quoted 8 lines
https://lore.kernel.org/all/[email protected]
Based on that discussion, I understood TRUST and eligibility for private DMA to be complementary attributes. Device acceptance determines whether a device may access private memory, while trust determines whether the core must enforce additional restrictions. I understood that supporting an accepted but adversarial device would require either a private IOTLB to restrict its access or an IOMMU.
Quoted 8 lines
I reached a completely different conclusion from that discussion.
Device trust does little beyond preventing driver binding and recording the trust decision in "struct device". The missing piece, which I would expect you to address, is ensuring that the IOMMU and private DMA are configured only after the trust level has been decided or as part of that decision, rather than beforehand, as they are today.
I agree that the trust decision must precede DMA configuration and driver probe. My integration tree shared in the cover letter already includes your trust framework, with PCI/TSM DMA setup following that decision.
Quoted 5 lines
What I am still trying to understand is how you view the DMA allocation and mapping paths determining whether a device can access private memory. Paths such as
force_dma_unencrypted()and SWIOTLB receive a struct device pointer and need to decide whether to use private memory or shared buffers.
After the IOMMU cleanup, I expect we’ll be able to create a cyclic trust-level state machine that lets us reset the trust level when a driver is unbound. We can then add internal states to the trust-level FSM, which DMA/SWIOTLB can check.
In theory, we could make this final improvement before the IOMMU cleanup. However, the risk of creating a mess is higher than if we follow a straightforward, sequential engineering approach.
Thanks
Quoted 15 lines
We could move
cc_acceptedinto structpci_tsm_devsec, outside driver core. How would you expect the generic DMA code to obtain that information? Adding another flag to struct device would effectively recreate the state we are trying to remove.This goes back to my question here:
https://lore.kernel.org/all/[email protected]/If we remove
device_cc_accepted(), what should those callers use instead? How do you see your trust framework supporting that decision?-aneesh
Quoted 10 lines · Leon Romanovsky
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 3 lines
But it should not, as you are making driver core changes that will mirror the same thing that that patchset is attempting to do. Please work together on this.
+10000000,
Greg,
This series came as a surprise to me given the fact that device trust series is exactly for that - allow confidential computing.
Device trust's primary role in enabling confidential computing is to give space for the userspace to do all the TSM and attestation stuff to get the device into T=1 private DMA mode.
It is the userspace that does all this and the userspace bridges from "I got the device into T=1 mode" to "Now it meets my userspace policy to be trust level Full" and then finally "Now I can bind and start a driver"
The two concepts work together, but they are not linked inside the kernel.
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Adversarial is equal to the "
cc_accepted".
I expect most policies would make "cc_accepted" as a full trust level, but we do want to allow adversarial as an option too.
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
I think the confusion stems from this thread:
https://lore.kernel.org/all/[email protected]
Based on that discussion, I understood TRUST and eligibility for private DMA to be complementary attributes. Device acceptance determines whether a device may access private memory, while trust determines whether the core must enforce additional restrictions.
Yes.
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Device trust does little beyond preventing driver binding and recording the trust decision in "struct device". The missing piece, which I would expect you to address, is ensuring that the IOMMU and private DMA are configured only after the trust level has been decided or as part of that decision, rather than beforehand, as they are today.
They are orthogonal and unrelated.
All the TDISP and T=1 DMA flow is about how the DMA API works and has no direct impact on trust level. You can choose any trust level for a device using T=1 DMA.
Getting the device into T=1 should influence how the userspace decides to set the trust level, but it doesn't interact inside the kernel.
Aneesh has it right that these are orthogonal and unrelated activities. There may be some confusion as Dan attempted to couple them, but after discussion we have moved away from that since it doesn't meet all the use cases.
Quoted 5 lines
Quoted 5 lines
What I am still trying to understand is how you view the DMA allocation and mapping paths determining whether a device can access private memory. Paths such as
force_dma_unencrypted()and SWIOTLB receive a struct device pointer and need to decide whether to use private memory or shared buffers.
It is Nicolin's work here that needs to go before the TSM drivers:
https://lore.kernel.org/all/[email protected]/
Because that is going in and starting to fix the DMA API to work with T=1 DMA devices.
The TSM side should sit on top of that to enable it for PCI devices, not just integrated devices.
I think for now lets just focus the TSM series on the attestation side and we will tackle the PCI and DMA API issues as another series.
Jason
Re: [PATCH v1 08/10] PCI/TSM: Add sysfs lock and unlock for guest-assigned devices
In reply to Aneesh Kumar K.V (Arm)Quoted 7 lines · Aneesh Kumar K.V (Arm)
A confidential guest needs to lock a PCI device before collecting and verifying evidence about it. Userspace also needs a way to unlock the device after use or when verification fails.
Add tsm/lock and tsm/unlock sysfs attributes for TSM drivers implementing
devsec_ops. Writing a TSM device name to tsm/lock calls its lock callback. Unlock calls the matching driver's unlock callback
I thought we agreed not to do this in sysfs as we worked through the SPDM stuff?
LOCKED should be the 'start SPDM' action in the guest RUN should be the 'accept SPDM' action in the guest
And though we didn't talk about it there is likely some 'destroy SPDM' action that is UNLOCK and UNRUN.
Jason
Re: [PATCH v1 02/10] coco: tsm: Register the TSM class before device drivers
In reply to Aneesh Kumar K.V (Arm)Quoted 3 lines · Aneesh Kumar K.V (Arm)
Register the built-in TSM class with
subsys_initcall()so it exists before a platform TSM driver probes, independently of object link order.CONFIG_TSMis boolean; remove the unused module exit path.
Why isn't it modular? This is going to force alot of stuff to be non-modular.
Jason
Re: [PATCH v1 05/10] PCI/TSM: Add association locking helpers for evidence acceptance
In reply to Aneesh Kumar K.V (Arm)Quoted 11 lines · Aneesh Kumar K.V (Arm)
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_tsmand must prevent disconnect or guest unlock from changingpci_dev::tsmor freeing the saved structure while setup is in progress.Add
pci_tsm_trylock_association()to acquirepci_tsm_rwsemfor write and check thatpci_dev::tsmstill points to the saved structure. For a TSM implementingdevsec_ops, first takedevice::mutexto prevent driver attachment during MMIO validation and device acceptance. This matches the device-mutex-before-rwsem order used by PCI DMA preparation.
This doesn't seem the API I'd expect here.
When we trigger SPDM session start AKA lock through the CTLV it needs to capture some kind of 'session cookie' for this locked that ties back to whatever the TSM is using to prevent a hostile host from locking/unlocking while we are working.
That cookie should be held in the attestation driver and when we get to acceptance it should be presented to confirm that nothing has been tampered with.
It is a further reason why we can't have lock under sysfs control.
It should be attestation that provides a general purpose lock that serializes all attestation operations including lock/unlock so we don't need a TSM lock like this.
The driver should rely on that lock and use its internal mechanism to process the cookie.
Jason
Aneesh Kumar K.V
maintainerQuoted 66 lines · Jason Gunthorpe
Quoted 10 lines · Leon Romanovsky
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 3 lines
But it should not, as you are making driver core changes that will mirror the same thing that that patchset is attempting to do. Please work together on this.
+10000000,
Greg,
This series came as a surprise to me given the fact that device trust series is exactly for that - allow confidential computing.
Device trust's primary role in enabling confidential computing is to give space for the userspace to do all the TSM and attestation stuff to get the device into T=1 private DMA mode.
It is the userspace that does all this and the userspace bridges from "I got the device into T=1 mode" to "Now it meets my userspace policy to be trust level Full" and then finally "Now I can bind and start a driver"
The two concepts work together, but they are not linked inside the kernel.
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Adversarial is equal to the "
cc_accepted".I expect most policies would make "
cc_accepted" as a full trust level, but we do want to allow adversarial as an option too.Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
I think the confusion stems from this thread:
https://lore.kernel.org/all/[email protected]
Based on that discussion, I understood TRUST and eligibility for private DMA to be complementary attributes. Device acceptance determines whether a device may access private memory, while trust determines whether the core must enforce additional restrictions.
Yes.
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Device trust does little beyond preventing driver binding and recording the trust decision in "struct device". The missing piece, which I would expect you to address, is ensuring that the IOMMU and private DMA are configured only after the trust level has been decided or as part of that decision, rather than beforehand, as they are today.
They are orthogonal and unrelated.
All the TDISP and T=1 DMA flow is about how the DMA API works and has no direct impact on trust level. You can choose any trust level for a device using T=1 DMA.
Getting the device into T=1 should influence how the userspace decides to set the trust level, but it doesn't interact inside the kernel.
Aneesh has it right that these are orthogonal and unrelated activities. There may be some confusion as Dan attempted to couple them, but after discussion we have moved away from that since it doesn't meet all the use cases.
Quoted 5 lines
Quoted 5 lines
What I am still trying to understand is how you view the DMA allocation and mapping paths determining whether a device can access private memory. Paths such as
force_dma_unencrypted()and SWIOTLB receive a struct device pointer and need to decide whether to use private memory or shared buffers.
Quoted 4 lines
It is Nicolin's work here that needs to go before the TSM drivers:
This adds
index 429e39f647cf9..01d7877d0b571 100644
include/linux/device.h +3 -0
@@ -604,6 +604,8 @@ struct device_physical_location {* ancestor device. * @DEV_FLAG_OFFLINE_DISABLED: If set, the device is permanently online. * @DEV_FLAG_OFFLINE: Set after successful invocation of bus type's .offline(). +* @DEV_FLAG_DMA_CC_PRIVATE: The device is able to access private (encrypted) +* memory, no shared memory bouncing is required from the DMA API. * @DEV_FLAG_COUNT: Number of defined struct_device_flags. */ enum struct_device_flags {
@@ -617,6 +619,7 @@ enum struct_device_flags {DEV_FLAG_OF_NODE_REUSED =7 ,DEV_FLAG_OFFLINE_DISABLED =8 ,DEV_FLAG_OFFLINE =9 , +DEV_FLAG_DMA_CC_PRIVATE =10 , DEV_FLAG_COUNT };
...
+void dma_set_cc_private(struct device *dev, bool private) +{
+ dev_assign_dma_cc_private(dev, private);
+}
This could easily replace device_cc_accept()/device_cc_accepted() used in this series. I can switch to it if that's the direction we want to take.
-aneesh
Quoted 28 lines · Aneesh Kumar K.V
index
429e39f647cf9..01d7877d0b571100644include/linux/device.h +7
-0@@ -604,6 +604,8 @@ struct device_physical_location {* ancestor device. * @DEV_FLAG_OFFLINE_DISABLED: If set, the device is permanently online. * @DEV_FLAG_OFFLINE: Set after successful invocation of bus type's .offline(). +* @DEV_FLAG_DMA_CC_PRIVATE: The device is able to access private (encrypted) +* memory, no shared memory bouncing is required from the DMA API. * @DEV_FLAG_COUNT: Number of defined struct_device_flags. */ enum struct_device_flags {@@ -617,6 +619,7 @@ enum struct_device_flags {DEV_FLAG_OF_NODE_REUSED =7 ,DEV_FLAG_OFFLINE_DISABLED =8 ,DEV_FLAG_OFFLINE =9 , +DEV_FLAG_DMA_CC_PRIVATE =10 , DEV_FLAG_COUNT }; +void dma_set_cc_private (struct device *dev,bool private) +{ +dev_assign_dma_cc_private (dev, private); +}
Quoted 3 lines
This could easily replace device_cc_accept()/device_cc_accepted() used in this series. I can switch to it if that's the direction we want to take.
I would prefer to settle this with Nicolin's series that is part of the point of it since it brings a real user and a justification..
So if you can confirm there than it does enough of what PCI needs that the PCI additional is incremental then great.
Jason
Quoted 15 lines · Jason Gunthorpe
Quoted 10 lines · Leon Romanovsky
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 3 lines
But it should not, as you are making driver core changes that will mirror the same thing that that patchset is attempting to do. Please work together on this.
+10000000,
Greg,
This series came as a surprise to me given the fact that device trust series is exactly for that - allow confidential computing.
Device trust's primary role in enabling confidential computing is to give space for the userspace to do all the TSM and attestation stuff to get the device into T=1 private DMA mode.
<...>
Quoted 2 lines
Getting the device into T=1 should influence how the userspace decides to set the trust level, but it doesn't interact inside the kernel.
The point is that I’m describing the reverse relationship: the trust level should determine how the device is configured, not the other way around, as you suggest.
Thanks