[PATCH v2 2/3] iommufd/tsm: add vdevice TSM bind/unbind ioctl

Aneesh Kumar K.V (Arm) posted 3 patches 1 month ago
[PATCH v2 2/3] iommufd/tsm: add vdevice TSM bind/unbind ioctl
Posted by Aneesh Kumar K.V (Arm) 1 month ago
Introduce IOMMU_VDEVICE_TSM_OP to allow userspace to issue TSM bind/unbind
operations for an iommufd vdevice.

The new ioctl:
- looks up the vdevice object from vdevice_id
- resolves the associated KVM VM from the vIOMMU KVM file reference
- dispatches bind/unbind via tsm_bind()/tsm_unbind()

Also add common TSM helpers in tsm-core and wire vdevice teardown to unbind
the device from TSM state.

This provides iommufd plumbing to bind a TDI to a confidential guest through
the TSM layer.

Cc: Kevin Tian <kevin.tian@intel.com>
Cc: Joerg Roedel <joro@8bytes.org>
Cc: Will Deacon <will@kernel.org>
Cc: Bjorn Helgaas <helgaas@kernel.org>
Cc: Jonathan Cameron <Jonathan.Cameron@huawei.com>
Cc: Dan Williams <dan.j.williams@intel.com>
Cc: Alexey Kardashevskiy <aik@amd.com>
Cc: Samuel Ortiz <sameo@rivosinc.com>
Cc: Xu Yilun <yilun.xu@linux.intel.com>
Cc: Jason Gunthorpe <jgg@ziepe.ca>
Cc: Suzuki K Poulose <Suzuki.Poulose@arm.com>
Cc: Steven Price <steven.price@arm.com>
Signed-off-by: Aneesh Kumar K.V (Arm) <aneesh.kumar@kernel.org>
---
 drivers/iommu/iommufd/Makefile          |  2 +
 drivers/iommu/iommufd/iommufd_private.h |  8 +++
 drivers/iommu/iommufd/main.c            |  3 ++
 drivers/iommu/iommufd/tsm.c             | 67 +++++++++++++++++++++++++
 drivers/iommu/iommufd/viommu.c          |  3 ++
 drivers/virt/coco/tsm-core.c            | 19 +++++++
 include/linux/tsm.h                     | 18 +++++++
 include/uapi/linux/iommufd.h            | 18 +++++++
 8 files changed, 138 insertions(+)
 create mode 100644 drivers/iommu/iommufd/tsm.c

diff --git a/drivers/iommu/iommufd/Makefile b/drivers/iommu/iommufd/Makefile
index 71d692c9a8f4..431089089ee9 100644
--- a/drivers/iommu/iommufd/Makefile
+++ b/drivers/iommu/iommufd/Makefile
@@ -10,6 +10,8 @@ iommufd-y := \
 	vfio_compat.o \
 	viommu.o
 
+iommufd-$(CONFIG_TSM) += tsm.o
+
 iommufd-$(CONFIG_IOMMUFD_TEST) += selftest.o
 
 obj-$(CONFIG_IOMMUFD) += iommufd.o
diff --git a/drivers/iommu/iommufd/iommufd_private.h b/drivers/iommu/iommufd/iommufd_private.h
index 6ac1965199e9..aa1ceed924c2 100644
--- a/drivers/iommu/iommufd/iommufd_private.h
+++ b/drivers/iommu/iommufd/iommufd_private.h
@@ -697,6 +697,14 @@ void iommufd_vdevice_destroy(struct iommufd_object *obj);
 void iommufd_vdevice_abort(struct iommufd_object *obj);
 int iommufd_hw_queue_alloc_ioctl(struct iommufd_ucmd *ucmd);
 void iommufd_hw_queue_destroy(struct iommufd_object *obj);
+#ifdef CONFIG_TSM
+int iommufd_vdevice_tsm_op_ioctl(struct iommufd_ucmd *ucmd);
+#else
+static inline int iommufd_vdevice_tsm_op_ioctl(struct iommufd_ucmd *ucmd)
+{
+	return -EOPNOTSUPP;
+}
+#endif
 
 static inline struct iommufd_vdevice *
 iommufd_get_vdevice(struct iommufd_ctx *ictx, u32 id)
diff --git a/drivers/iommu/iommufd/main.c b/drivers/iommu/iommufd/main.c
index 8c6d43601afb..d73e6b391c6f 100644
--- a/drivers/iommu/iommufd/main.c
+++ b/drivers/iommu/iommufd/main.c
@@ -432,6 +432,7 @@ union ucmd_buffer {
 	struct iommu_veventq_alloc veventq;
 	struct iommu_vfio_ioas vfio_ioas;
 	struct iommu_viommu_alloc viommu;
+	struct iommu_vdevice_tsm_op tsm_op;
 #ifdef CONFIG_IOMMUFD_TEST
 	struct iommu_test_cmd test;
 #endif
@@ -493,6 +494,8 @@ static const struct iommufd_ioctl_op iommufd_ioctl_ops[] = {
 		 __reserved),
 	IOCTL_OP(IOMMU_VIOMMU_ALLOC, iommufd_viommu_alloc_ioctl,
 		 struct iommu_viommu_alloc, out_viommu_id),
+	IOCTL_OP(IOMMU_VDEVICE_TSM_OP, iommufd_vdevice_tsm_op_ioctl,
+		 struct iommu_vdevice_tsm_op, vdevice_id),
 #ifdef CONFIG_IOMMUFD_TEST
 	IOCTL_OP(IOMMU_TEST_CMD, iommufd_test, struct iommu_test_cmd, last),
 #endif
diff --git a/drivers/iommu/iommufd/tsm.c b/drivers/iommu/iommufd/tsm.c
new file mode 100644
index 000000000000..401469110752
--- /dev/null
+++ b/drivers/iommu/iommufd/tsm.c
@@ -0,0 +1,67 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Copyright (C) 2026 ARM Ltd.
+ */
+
+#include "iommufd_private.h"
+#include <linux/tsm.h>
+
+/**
+ * iommufd_vdevice_tsm_op_ioctl - Handle vdevice TSM operations
+ * @ucmd: user command data for IOMMU_VDEVICE_TSM_OP
+ *
+ * Currently only supports TSM bind/unbind operations
+ * Resolve @iommu_vdevice_tsm_op::vdevice_id to a vdevice and dispatch the
+ * requested bind/unbind operation through the TSM core.
+ *
+ * Return: 0 on success, or a negative error code on failure.
+ */
+int iommufd_vdevice_tsm_op_ioctl(struct iommufd_ucmd *ucmd)
+{
+	int rc;
+	struct kvm *kvm;
+	struct iommufd_vdevice *vdev;
+	struct iommu_vdevice_tsm_op *cmd = ucmd->cmd;
+
+	if (cmd->flags)
+		return -EOPNOTSUPP;
+
+	vdev = container_of(iommufd_get_object(ucmd->ictx, cmd->vdevice_id,
+					       IOMMUFD_OBJ_VDEVICE),
+			    struct iommufd_vdevice, obj);
+	if (IS_ERR(vdev))
+		return PTR_ERR(vdev);
+
+	if (!vdev->viommu->kvm_filp) {
+		rc = -ENODEV;
+		goto out_put_vdev;
+	}
+
+	kvm = vdev->viommu->kvm_filp->private_data;
+	if (!kvm) {
+		rc = -ENODEV;
+		goto out_put_vdev;
+	}
+
+	/* tsm layer will take care of parallel calls to tsm_bind/unbind */
+	switch (cmd->op) {
+	case IOMMU_VDEVICE_TSM_BIND:
+		rc = tsm_bind(vdev->idev->dev, kvm, vdev->virt_id);
+		break;
+	case IOMMU_VDEVICE_TSM_UNBIND:
+		rc = tsm_unbind(vdev->idev->dev);
+		break;
+	default:
+		rc = -EINVAL;
+		goto out_put_vdev;
+	}
+
+	if (rc)
+		goto out_put_vdev;
+
+	rc = iommufd_ucmd_respond(ucmd, sizeof(*cmd));
+
+out_put_vdev:
+	iommufd_put_object(ucmd->ictx, &vdev->obj);
+	return rc;
+}
diff --git a/drivers/iommu/iommufd/viommu.c b/drivers/iommu/iommufd/viommu.c
index 08f8930c86da..8eb7a3441a61 100644
--- a/drivers/iommu/iommufd/viommu.c
+++ b/drivers/iommu/iommufd/viommu.c
@@ -3,6 +3,7 @@
  */
 #include "iommufd_private.h"
 #include <linux/cleanup.h>
+#include <linux/tsm.h>
 
 #if IS_ENABLED(CONFIG_KVM)
 #include <linux/kvm_host.h>
@@ -171,6 +172,8 @@ void iommufd_vdevice_abort(struct iommufd_object *obj)
 
 	lockdep_assert_held(&idev->igroup->lock);
 
+	tsm_unbind(idev->dev);
+
 	if (vdev->destroy)
 		vdev->destroy(vdev);
 	/* xa_cmpxchg is okay to fail if alloc failed xa_cmpxchg previously */
diff --git a/drivers/virt/coco/tsm-core.c b/drivers/virt/coco/tsm-core.c
index ae3617abd2ac..f0e35fc38776 100644
--- a/drivers/virt/coco/tsm-core.c
+++ b/drivers/virt/coco/tsm-core.c
@@ -240,6 +240,25 @@ void tsm_ide_stream_unregister(struct pci_ide *ide)
 }
 EXPORT_SYMBOL_GPL(tsm_ide_stream_unregister);
 
+int tsm_bind(struct device *dev, struct kvm *kvm, u64 tdi_id)
+{
+	if (!dev_is_pci(dev))
+		return -EINVAL;
+
+	return pci_tsm_bind(to_pci_dev(dev), kvm, tdi_id);
+}
+EXPORT_SYMBOL_GPL(tsm_bind);
+
+int tsm_unbind(struct device *dev)
+{
+	if (!dev_is_pci(dev))
+		return -EINVAL;
+
+	pci_tsm_unbind(to_pci_dev(dev));
+	return 0;
+}
+EXPORT_SYMBOL_GPL(tsm_unbind);
+
 static void tsm_release(struct device *dev)
 {
 	struct tsm_dev *tsm_dev = container_of(dev, typeof(*tsm_dev), dev);
diff --git a/include/linux/tsm.h b/include/linux/tsm.h
index 7f72a154b6b2..9f2a7868021a 100644
--- a/include/linux/tsm.h
+++ b/include/linux/tsm.h
@@ -124,6 +124,24 @@ struct tsm_dev *tsm_register(struct device *parent, struct pci_tsm_ops *ops);
 void tsm_unregister(struct tsm_dev *tsm_dev);
 struct tsm_dev *find_tsm_dev(int id);
 struct pci_ide;
+struct kvm;
 int tsm_ide_stream_register(struct pci_ide *ide);
 void tsm_ide_stream_unregister(struct pci_ide *ide);
+#ifdef CONFIG_TSM
+int tsm_bind(struct device *dev, struct kvm *kvm, u64 tdi_id);
+int tsm_unbind(struct device *dev);
+
+#else
+
+static inline int tsm_bind(struct device *dev, struct kvm *kvm, u64 tdi_id)
+{
+	return -EINVAL;
+}
+
+static inline int tsm_unbind(struct device *dev)
+{
+	return 0;
+}
+#endif
+
 #endif /* __TSM_H */
diff --git a/include/uapi/linux/iommufd.h b/include/uapi/linux/iommufd.h
index b862c3e57133..653402e7048a 100644
--- a/include/uapi/linux/iommufd.h
+++ b/include/uapi/linux/iommufd.h
@@ -57,6 +57,7 @@ enum {
 	IOMMUFD_CMD_IOAS_CHANGE_PROCESS = 0x92,
 	IOMMUFD_CMD_VEVENTQ_ALLOC = 0x93,
 	IOMMUFD_CMD_HW_QUEUE_ALLOC = 0x94,
+	IOMMUFD_CMD_VDEVICE_TSM_OP = 0x95,
 };
 
 /**
@@ -1174,6 +1175,23 @@ enum iommu_veventq_flag {
 	IOMMU_VEVENTQ_FLAG_LOST_EVENTS = (1U << 0),
 };
 
+/**
+ * struct iommu_vdevice_tsm_op - ioctl(IOMMU_VDEVICE_TSM_OP)
+ * @size: sizeof(struct iommu_vdevice_tsm_op)
+ * @op: Either TSM_BIND or TSM_UNBIND
+ * @flags: Must be 0
+ * @vdevice_id: Object handle for the vDevice. Returned from IOMMU_VDEVICE_ALLOC
+ */
+struct iommu_vdevice_tsm_op {
+	__u32 size;
+	__u32 op;
+	__u32 flags;
+	__u32 vdevice_id;
+};
+#define IOMMU_VDEVICE_TSM_OP	_IO(IOMMUFD_TYPE, IOMMUFD_CMD_VDEVICE_TSM_OP)
+#define IOMMU_VDEVICE_TSM_BIND		0x1
+#define IOMMU_VDEVICE_TSM_UNBIND	0x2
+
 /**
  * struct iommufd_vevent_header - Virtual Event Header for a vEVENTQ Status
  * @flags: Combination of enum iommu_veventq_flag
-- 
2.43.0
RE: [PATCH v2 2/3] iommufd/tsm: add vdevice TSM bind/unbind ioctl
Posted by Tian, Kevin 3 weeks, 3 days ago
> From: Aneesh Kumar K.V (Arm) <aneesh.kumar@kernel.org>
> Sent: Monday, March 9, 2026 7:17 PM
> 
> Introduce IOMMU_VDEVICE_TSM_OP to allow userspace to issue TSM
> bind/unbind
> operations for an iommufd vdevice.

How many ops are planned? If just a few I'd prefer to openly defining
it:

	IOMMU_VDEVICE_ALLOC,
	IOMMU_VDEVICE_TSM_BIND,
	IOMMU_VDEVICE_TSM_UNBIND,

just like how we did for IOAS.

> @@ -432,6 +432,7 @@ union ucmd_buffer {
>  	struct iommu_veventq_alloc veventq;
>  	struct iommu_vfio_ioas vfio_ioas;
>  	struct iommu_viommu_alloc viommu;
> +	struct iommu_vdevice_tsm_op tsm_op;

please follow the alphabetical order

> @@ -493,6 +494,8 @@ static const struct iommufd_ioctl_op
> iommufd_ioctl_ops[] = {
>  		 __reserved),
>  	IOCTL_OP(IOMMU_VIOMMU_ALLOC, iommufd_viommu_alloc_ioctl,
>  		 struct iommu_viommu_alloc, out_viommu_id),
> +	IOCTL_OP(IOMMU_VDEVICE_TSM_OP,
> iommufd_vdevice_tsm_op_ioctl,
> +		 struct iommu_vdevice_tsm_op, vdevice_id),

ditto

> +int iommufd_vdevice_tsm_op_ioctl(struct iommufd_ucmd *ucmd)
> +{
> +	int rc;
> +	struct kvm *kvm;
> +	struct iommufd_vdevice *vdev;
> +	struct iommu_vdevice_tsm_op *cmd = ucmd->cmd;
> +
> +	if (cmd->flags)
> +		return -EOPNOTSUPP;
> +
> +	vdev = container_of(iommufd_get_object(ucmd->ictx, cmd-
> >vdevice_id,
> +					       IOMMUFD_OBJ_VDEVICE),
> +			    struct iommufd_vdevice, obj);
> +	if (IS_ERR(vdev))
> +		return PTR_ERR(vdev);
> +
> +	if (!vdev->viommu->kvm_filp) {
> +		rc = -ENODEV;
> +		goto out_put_vdev;
> +	}
> +
> +	kvm = vdev->viommu->kvm_filp->private_data;
> +	if (!kvm) {
> +		rc = -ENODEV;
> +		goto out_put_vdev;
> +	}

	if (!vdev->viommu->kvm_filp ||
	    !vdev->viommu->kvm_filp->private_data) {
		rc = -ENODEV;
		goto out_put_vdev;
	}

	kvm = vdev->viommu->kvm_filp->private_data;
RE: [PATCH v2 2/3] iommufd/tsm: add vdevice TSM bind/unbind ioctl
Posted by Aneesh Kumar K.V 3 weeks, 3 days ago
"Tian, Kevin" <kevin.tian@intel.com> writes:

>> From: Aneesh Kumar K.V (Arm) <aneesh.kumar@kernel.org>
>> Sent: Monday, March 9, 2026 7:17 PM
>> 
>> Introduce IOMMU_VDEVICE_TSM_OP to allow userspace to issue TSM
>> bind/unbind
>> operations for an iommufd vdevice.
>
> How many ops are planned? If just a few I'd prefer to openly defining
> it:
>
> 	IOMMU_VDEVICE_ALLOC,
> 	IOMMU_VDEVICE_TSM_BIND,
> 	IOMMU_VDEVICE_TSM_UNBIND,
>
> just like how we did for IOAS.
>

Right now only TSM_BIND and TSM_UNBIND. This was done based on earlier
review feedback.

https://lore.kernel.org/all/20250530181842.GU233377@nvidia.com/

>
>> @@ -432,6 +432,7 @@ union ucmd_buffer {
>>  	struct iommu_veventq_alloc veventq;
>>  	struct iommu_vfio_ioas vfio_ioas;
>>  	struct iommu_viommu_alloc viommu;
>> +	struct iommu_vdevice_tsm_op tsm_op;
>
> please follow the alphabetical order
>
>> @@ -493,6 +494,8 @@ static const struct iommufd_ioctl_op
>> iommufd_ioctl_ops[] = {
>>  		 __reserved),
>>  	IOCTL_OP(IOMMU_VIOMMU_ALLOC, iommufd_viommu_alloc_ioctl,
>>  		 struct iommu_viommu_alloc, out_viommu_id),
>> +	IOCTL_OP(IOMMU_VDEVICE_TSM_OP,
>> iommufd_vdevice_tsm_op_ioctl,
>> +		 struct iommu_vdevice_tsm_op, vdevice_id),
>
> ditto
>
>> +int iommufd_vdevice_tsm_op_ioctl(struct iommufd_ucmd *ucmd)
>> +{
>> +	int rc;
>> +	struct kvm *kvm;
>> +	struct iommufd_vdevice *vdev;
>> +	struct iommu_vdevice_tsm_op *cmd = ucmd->cmd;
>> +
>> +	if (cmd->flags)
>> +		return -EOPNOTSUPP;
>> +
>> +	vdev = container_of(iommufd_get_object(ucmd->ictx, cmd-
>> >vdevice_id,
>> +					       IOMMUFD_OBJ_VDEVICE),
>> +			    struct iommufd_vdevice, obj);
>> +	if (IS_ERR(vdev))
>> +		return PTR_ERR(vdev);
>> +
>> +	if (!vdev->viommu->kvm_filp) {
>> +		rc = -ENODEV;
>> +		goto out_put_vdev;
>> +	}
>> +
>> +	kvm = vdev->viommu->kvm_filp->private_data;
>> +	if (!kvm) {
>> +		rc = -ENODEV;
>> +		goto out_put_vdev;
>> +	}
>
> 	if (!vdev->viommu->kvm_filp ||
> 	    !vdev->viommu->kvm_filp->private_data) {
> 		rc = -ENODEV;
> 		goto out_put_vdev;
> 	}
>
> 	kvm = vdev->viommu->kvm_filp->private_data;

-aneesh
Re: [PATCH v2 2/3] iommufd/tsm: add vdevice TSM bind/unbind ioctl
Posted by Jason Gunthorpe 3 weeks, 5 days ago
On Mon, Mar 09, 2026 at 04:47:03PM +0530, Aneesh Kumar K.V (Arm) wrote:
> @@ -1174,6 +1175,23 @@ enum iommu_veventq_flag {
>  	IOMMU_VEVENTQ_FLAG_LOST_EVENTS = (1U << 0),
>  };
>  
> +/**
> + * struct iommu_vdevice_tsm_op - ioctl(IOMMU_VDEVICE_TSM_OP)
> + * @size: sizeof(struct iommu_vdevice_tsm_op)
> + * @op: Either TSM_BIND or TSM_UNBIND
> + * @flags: Must be 0
> + * @vdevice_id: Object handle for the vDevice. Returned from IOMMU_VDEVICE_ALLOC
> + */
> +struct iommu_vdevice_tsm_op {
> +	__u32 size;
> +	__u32 op;
> +	__u32 flags;
> +	__u32 vdevice_id;
> +};
> +#define IOMMU_VDEVICE_TSM_OP	_IO(IOMMUFD_TYPE, IOMMUFD_CMD_VDEVICE_TSM_OP)
> +#define IOMMU_VDEVICE_TSM_BIND		0x1
> +#define IOMMU_VDEVICE_TSM_UNBIND	0x2

use enum for an op, put it before the struct, add kdocs for the enum
members.

Jason
Re: [PATCH v2 2/3] iommufd/tsm: add vdevice TSM bind/unbind ioctl
Posted by Jonathan Cameron 4 weeks ago
On Mon,  9 Mar 2026 16:47:03 +0530
"Aneesh Kumar K.V (Arm)" <aneesh.kumar@kernel.org> wrote:

> Introduce IOMMU_VDEVICE_TSM_OP to allow userspace to issue TSM bind/unbind
> operations for an iommufd vdevice.
> 
> The new ioctl:
> - looks up the vdevice object from vdevice_id
> - resolves the associated KVM VM from the vIOMMU KVM file reference
> - dispatches bind/unbind via tsm_bind()/tsm_unbind()
> 
> Also add common TSM helpers in tsm-core and wire vdevice teardown to unbind
> the device from TSM state.
> 
> This provides iommufd plumbing to bind a TDI to a confidential guest through
> the TSM layer.

> Signed-off-by: Aneesh Kumar K.V (Arm) <aneesh.kumar@kernel.org>
Hi Aneesh,

Some superficial code flow suggestions.  I've not gotten
my head around the broader picture yet, so may well come back with
more comments later.

Thanks,
Jonathan

> ---
>  drivers/iommu/iommufd/Makefile          |  2 +
>  drivers/iommu/iommufd/iommufd_private.h |  8 +++
>  drivers/iommu/iommufd/main.c            |  3 ++
>  drivers/iommu/iommufd/tsm.c             | 67 +++++++++++++++++++++++++
>  drivers/iommu/iommufd/viommu.c          |  3 ++
>  drivers/virt/coco/tsm-core.c            | 19 +++++++
>  include/linux/tsm.h                     | 18 +++++++
>  include/uapi/linux/iommufd.h            | 18 +++++++
>  8 files changed, 138 insertions(+)
>  create mode 100644 drivers/iommu/iommufd/tsm.c
> 
> diff --git a/drivers/iommu/iommufd/Makefile b/drivers/iommu/iommufd/Makefile
> index 71d692c9a8f4..431089089ee9 100644
> --- a/drivers/iommu/iommufd/Makefile
> +++ b/drivers/iommu/iommufd/Makefile
> @@ -10,6 +10,8 @@ iommufd-y := \
>  	vfio_compat.o \
>  	viommu.o
>  
> +iommufd-$(CONFIG_TSM) += tsm.o
> +
Probably no blank line here. Style choices in these are a bit random
though so this is kind of a taste thing.

>  iommufd-$(CONFIG_IOMMUFD_TEST) += selftest.o
>  
>  obj-$(CONFIG_IOMMUFD) += iommufd.o

> diff --git a/drivers/iommu/iommufd/tsm.c b/drivers/iommu/iommufd/tsm.c
> new file mode 100644
> index 000000000000..401469110752
> --- /dev/null
> +++ b/drivers/iommu/iommufd/tsm.c
> @@ -0,0 +1,67 @@
> +// SPDX-License-Identifier: GPL-2.0-only
> +/*
> + * Copyright (C) 2026 ARM Ltd.
> + */
> +
> +#include "iommufd_private.h"
> +#include <linux/tsm.h>
> +
> +/**
> + * iommufd_vdevice_tsm_op_ioctl - Handle vdevice TSM operations
> + * @ucmd: user command data for IOMMU_VDEVICE_TSM_OP
> + *
> + * Currently only supports TSM bind/unbind operations
> + * Resolve @iommu_vdevice_tsm_op::vdevice_id to a vdevice and dispatch the
> + * requested bind/unbind operation through the TSM core.
> + *
> + * Return: 0 on success, or a negative error code on failure.
> + */
> +int iommufd_vdevice_tsm_op_ioctl(struct iommufd_ucmd *ucmd)
> +{
> +	int rc;
> +	struct kvm *kvm;
> +	struct iommufd_vdevice *vdev;
> +	struct iommu_vdevice_tsm_op *cmd = ucmd->cmd;
> +
> +	if (cmd->flags)
> +		return -EOPNOTSUPP;
> +
> +	vdev = container_of(iommufd_get_object(ucmd->ictx, cmd->vdevice_id,
> +					       IOMMUFD_OBJ_VDEVICE),
> +			    struct iommufd_vdevice, obj);

I'd be tempted to do something with a helper function to simplify flow.

	obj = iommufd_get_object();...

	if (IS_ERR(obj) //I think it can be?
		return PTR_ERR(obj);

	rc = iommufd_vdevice_do_tsm_op_ioctl(obj, ...) //name could be improved
	iommufd_put_object(ucmd->ictx, &vdev->obj);

	return rc;

Then the helper function can do direct returns on errors given the
iommfd_object is managed in outer function.

> +	if (IS_ERR(vdev))
> +		return PTR_ERR(vdev);
> +
> +	if (!vdev->viommu->kvm_filp) {
> +		rc = -ENODEV;
> +		goto out_put_vdev;
> +	}
> +
> +	kvm = vdev->viommu->kvm_filp->private_data;
> +	if (!kvm) {
> +		rc = -ENODEV;
> +		goto out_put_vdev;
> +	}
> +
> +	/* tsm layer will take care of parallel calls to tsm_bind/unbind */
> +	switch (cmd->op) {
> +	case IOMMU_VDEVICE_TSM_BIND:
> +		rc = tsm_bind(vdev->idev->dev, kvm, vdev->virt_id);
> +		break;
> +	case IOMMU_VDEVICE_TSM_UNBIND:
> +		rc = tsm_unbind(vdev->idev->dev);
> +		break;
> +	default:
> +		rc = -EINVAL;
> +		goto out_put_vdev;
> +	}
> +
> +	if (rc)
> +		goto out_put_vdev;
> +
> +	rc = iommufd_ucmd_respond(ucmd, sizeof(*cmd));
> +
> +out_put_vdev:
> +	iommufd_put_object(ucmd->ictx, &vdev->obj);
> +	return rc;
> +}

> diff --git a/include/linux/tsm.h b/include/linux/tsm.h
> index 7f72a154b6b2..9f2a7868021a 100644
> --- a/include/linux/tsm.h
> +++ b/include/linux/tsm.h
> @@ -124,6 +124,24 @@ struct tsm_dev *tsm_register(struct device *parent, struct pci_tsm_ops *ops);
>  void tsm_unregister(struct tsm_dev *tsm_dev);
>  struct tsm_dev *find_tsm_dev(int id);
>  struct pci_ide;
> +struct kvm;

Why up here? struct pci_ide is here for the next two calls, so no need
to group with that.

>  int tsm_ide_stream_register(struct pci_ide *ide);
>  void tsm_ide_stream_unregister(struct pci_ide *ide);

Feels like the forwards def belongs here.

> +#ifdef CONFIG_TSM
> +int tsm_bind(struct device *dev, struct kvm *kvm, u64 tdi_id);
> +int tsm_unbind(struct device *dev);
> +
White space is a bit inconsistent. I don't mind seeing some
here, but if so, add some around the ifdef above.

> +#else
> +
> +static inline int tsm_bind(struct device *dev, struct kvm *kvm, u64 tdi_id)
> +{
> +	return -EINVAL;
> +}
> +
> +static inline int tsm_unbind(struct device *dev)
> +{
> +	return 0;
> +}
> +#endif
> +
>  #endif /* __TSM_H */
Re: [PATCH v2 2/3] iommufd/tsm: add vdevice TSM bind/unbind ioctl
Posted by Jason Gunthorpe 3 weeks, 5 days ago
On Wed, Mar 11, 2026 at 09:35:55PM +0000, Jonathan Cameron wrote:
> > +int iommufd_vdevice_tsm_op_ioctl(struct iommufd_ucmd *ucmd)
> > +{
> > +	int rc;
> > +	struct kvm *kvm;
> > +	struct iommufd_vdevice *vdev;
> > +	struct iommu_vdevice_tsm_op *cmd = ucmd->cmd;
> > +
> > +	if (cmd->flags)
> > +		return -EOPNOTSUPP;
> > +
> > +	vdev = container_of(iommufd_get_object(ucmd->ictx, cmd->vdevice_id,
> > +					       IOMMUFD_OBJ_VDEVICE),
> > +			    struct iommufd_vdevice, obj);
> 
> I'd be tempted to do something with a helper function to simplify flow.

It is already

static inline struct iommufd_vdevice *
iommufd_get_vdevice(struct iommufd_ctx *ictx, u32 id)
{
        return container_of(iommufd_get_object(ictx, id,
                                               IOMMUFD_OBJ_VDEVICE),
                            struct iommufd_vdevice, obj);
}

> Then the helper function can do direct returns on errors given the
> iommfd_object is managed in outer function.

Use cleanup.h ? It is not the iommufd style the break things up into
little functions for error unwind reasons
 
> > +	if (!vdev->viommu->kvm_filp) {
> > +		rc = -ENODEV;
> > +		goto out_put_vdev;
> > +	}
> > +
> > +	kvm = vdev->viommu->kvm_filp->private_data;
> > +	if (!kvm) {

Ah this I don't think you should be doing. That needs a helper from
kvm

Jason