[PATCH v10 13/18] PCI/TSM: Add helpers to look up and reference a device's TSM
Baolu Lu
baolu.lu at linux.intel.com
Wed Dec 2 20:45:02 PST 2026
On 11/25/2026 4:11 PM, Aneesh Kumar K.V (Arm) wrote:
> Add tsm_get_device() to find and reference the TSM attached to a device,
> using pci_tsm_get_device() for PCI devices. Resolve the PCI association
> under pci_tsm_rwsem and acquire TSM device and backend module
> references. Return NULL when no TSM is attached and an error if the
> module cannot be pinned.
>
> Add tsm_put_device() to release the device reference before the module
> reference, since the final device put may call into the backend module.
>
> Set the AMD SEV TSM operations owner to THIS_MODULE so lookup pins the
> module implementing its callbacks.
>
> Following patch lets IOMMUFD use vIOMMU operations implemented by the
> selected device's TSM. vIOMMU allocation must look up that TSM to find
> its operations. The vIOMMU keeps the TSM device and module references
> until it is destroyed, so the TSM allocation cannot be freed and its
> callback code cannot be unloaded while the vIOMMU can still call it.
>
> Signed-off-by: Aneesh Kumar K.V (Arm) <aneesh.kumar at kernel.org>
> ---
> drivers/crypto/ccp/sev-dev-tsm.c | 1 +
> drivers/pci/tsm.c | 19 +++++++++++++++++++
> drivers/virt/coco/tsm-core.c | 32 ++++++++++++++++++++++++++++++++
> include/linux/pci-tsm.h | 10 ++++++++++
> include/linux/tsm.h | 19 +++++++++++++++++++
> 5 files changed, 81 insertions(+)
>
[...]
> diff --git a/drivers/virt/coco/tsm-core.c b/drivers/virt/coco/tsm-core.c
> index bf09bb4546de..4f735c628ec3 100644
> --- a/drivers/virt/coco/tsm-core.c
> +++ b/drivers/virt/coco/tsm-core.c
> @@ -16,6 +16,38 @@ static const struct class tsm_class = {
> };
> static DEFINE_IDA(tsm_ida);
>
> +/**
> + * tsm_get_device() - Pin the TSM attached to a device
> + * @dev: device whose TSM is to be pinned
> + *
> + * A successful lookup takes a TSM device reference and pins the backend
> + * module. Release both with tsm_put_device(). The TSM must remain registered
> + * while the module is pinned.
How can a caller enforce "the TSM must remain registered while the
module is pinned"? This should be guaranteed by the TSM core, not a
requirement on the caller.
> + * Return: NULL if no TSM is attached, an error if the module
> + * cannot be pinned, or the TSM device associated with the pinned backend.
Nit: a blank line before "Return:".
> + */
> +struct tsm_dev *tsm_get_device(struct device *dev)
> +{
> + if (!dev_is_pci(dev))
> + return NULL;
> +
> + return pci_tsm_get_device(to_pci_dev(dev));
> +}
> +EXPORT_SYMBOL_GPL(tsm_get_device);
> +
> +void tsm_put_device(struct tsm_dev *tsm_dev)
> +{
> + struct module *owner;
> +
> + if (!tsm_dev)
> + return;
> +
> + owner = tsm_dev->pci_ops->owner;
> + put_device(&tsm_dev->dev);
> + module_put(owner);
> +}
> +EXPORT_SYMBOL_GPL(tsm_put_device);
> +
> static int match_id(struct device *dev, const void *data)
> {
> struct tsm_dev *tsm_dev = container_of(dev, struct tsm_dev, dev);
[...]
Thanks,
baolu
More information about the linux-arm-kernel
mailing list