* [PATCH RFC v3 1/4] iommu: Add set/unset_dev_user_data ops
2023-04-23 7:40 [PATCH RFC v3 0/4] Add set_dev_data and unset_dev_data support Nicolin Chen
@ 2023-04-23 7:40 ` Nicolin Chen
2023-04-23 7:40 ` [PATCH RFC v3 2/4] iommufd: Add IOMMUFD_CMD_DEVICE_SET_DATA and IOMMUFD_CMD_DEVICE_UNSET_DATA Nicolin Chen
` (2 subsequent siblings)
3 siblings, 0 replies; 7+ messages in thread
From: Nicolin Chen @ 2023-04-23 7:40 UTC (permalink / raw)
To: jgg, kevin.tian
Cc: robin.murphy, eric.auger, yi.l.liu, baolu.lu, will, joro,
shameerali.kolothum.thodi, jean-philippe, linux-arm-kernel, iommu,
linux-kernel
The device behind an IOMMU might be used in the user space by a VM. So, it
might have some user space data. For example, a device behind an SMMU has
a static stream ID. In a virtualization use case, both a host environment
and a guest environment have their own Stream IDs. A link (a lookup table)
between the physical Stream ID and the virtual (user) Stream ID is needed
when the host handles the user cache invalidation commands.
Add a pair of new ops to allow user space to forward user_data of a device
via iommufd, and a new dev_user_data_len for data structure sanity done by
the iommufd core.
Signed-off-by: Nicolin Chen <nicolinc@nvidia.com>
---
include/linux/iommu.h | 13 +++++++++++++
1 file changed, 13 insertions(+)
diff --git a/include/linux/iommu.h b/include/linux/iommu.h
index 5c99aeaccd1d..33b11d87b374 100644
--- a/include/linux/iommu.h
+++ b/include/linux/iommu.h
@@ -257,6 +257,15 @@ struct iommu_iotlb_gather {
* @remove_dev_pasid: Remove any translation configurations of a specific
* pasid, so that any DMA transactions with this pasid
* will be blocked by the hardware.
+ * @set/unset_dev_user_data: set/unset an iommu specific device data from user
+ * space. The user device data info will be used by
+ * the driver to take care of user space requests.
+ * The device data structure must be defined in
+ * include/uapi/linux/iommufd.h.
+ * @dev_user_data_len: Length of the device data from user space (in bytes),
+ * simply the "sizeof" the data structure defined in the
+ * include/uapi/linux/iommufd.h. This is used by iommufd
+ * core to run a data length validation.
* @hw_info_type: One of enum iommu_hw_info_type defined in
* include/uapi/linux/iommufd.h. It is used to tag the type
* of data returned by .hw_info callback. The drivers that
@@ -303,6 +312,10 @@ struct iommu_ops {
int (*def_domain_type)(struct device *dev);
void (*remove_dev_pasid)(struct device *dev, ioasid_t pasid);
+ int (*set_dev_user_data)(struct device *dev, const void *user_data);
+ void (*unset_dev_user_data)(struct device *dev);
+ size_t dev_user_data_len;
+
const struct iommu_domain_ops *default_domain_ops;
enum iommu_hw_info_type hw_info_type;
unsigned long long hwpt_type_bitmap;
--
2.40.0
_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
^ permalink raw reply related [flat|nested] 7+ messages in thread* [PATCH RFC v3 2/4] iommufd: Add IOMMUFD_CMD_DEVICE_SET_DATA and IOMMUFD_CMD_DEVICE_UNSET_DATA
2023-04-23 7:40 [PATCH RFC v3 0/4] Add set_dev_data and unset_dev_data support Nicolin Chen
2023-04-23 7:40 ` [PATCH RFC v3 1/4] iommu: Add set/unset_dev_user_data ops Nicolin Chen
@ 2023-04-23 7:40 ` Nicolin Chen
2023-04-24 2:44 ` Baolu Lu
2023-04-23 7:40 ` [PATCH RFC v3 3/4] iommufd/selftest: Add IOMMU_TEST_OP_DEV_CHECK_DATA Nicolin Chen
2023-04-23 7:40 ` [PATCH RFC v3 4/4] iommufd/selftests: Add coverage for IOMMU_DEVICE_SET/UNSET_DATA Nicolin Chen
3 siblings, 1 reply; 7+ messages in thread
From: Nicolin Chen @ 2023-04-23 7:40 UTC (permalink / raw)
To: jgg, kevin.tian
Cc: robin.murphy, eric.auger, yi.l.liu, baolu.lu, will, joro,
shameerali.kolothum.thodi, jean-philippe, linux-arm-kernel, iommu,
linux-kernel
Add a new pair of ioctls to allow user space to set and unset its iommu-
specific device data for a passthrough device that's behind the iommu.
On platforms with SMMUv3, this new uAPIs will be used to forward a user
space virtual Stream ID of a passthrough device to link to its physical
Stream ID and log into a lookup table, in order for the host kernel to
later run sanity on ATC invalidation requests from the user space, with
ATC_INV commands that have SID fields (virtual Stream IDs).
Signed-off-by: Nicolin Chen <nicolinc@nvidia.com>
---
drivers/iommu/iommufd/device.c | 81 +++++++++++++++++++++++++
drivers/iommu/iommufd/iommufd_private.h | 3 +
drivers/iommu/iommufd/main.c | 4 ++
include/uapi/linux/iommufd.h | 32 ++++++++++
4 files changed, 120 insertions(+)
diff --git a/drivers/iommu/iommufd/device.c b/drivers/iommu/iommufd/device.c
index c649a3403797..9480cd36a8bd 100644
--- a/drivers/iommu/iommufd/device.c
+++ b/drivers/iommu/iommufd/device.c
@@ -136,6 +136,8 @@ void iommufd_device_destroy(struct iommufd_object *obj)
struct iommufd_device *idev =
container_of(obj, struct iommufd_device, obj);
+ if (WARN_ON(idev->has_user_data))
+ dev_iommu_ops(idev->dev)->unset_dev_user_data(idev->dev);
iommu_device_release_dma_owner(idev->dev);
iommufd_put_group(idev->igroup);
if (!iommufd_selftest_is_mock_dev(idev->dev))
@@ -726,6 +728,85 @@ void iommufd_device_detach(struct iommufd_device *idev)
}
EXPORT_SYMBOL_NS_GPL(iommufd_device_detach, IOMMUFD);
+int iommufd_device_set_data(struct iommufd_ucmd *ucmd)
+{
+ struct iommu_device_set_data *cmd = ucmd->cmd;
+ struct iommufd_device *idev;
+ const struct iommu_ops *ops;
+ void *data = NULL;
+ int rc;
+
+ if (!cmd->data_uptr || !cmd->data_len)
+ return -EINVAL;
+
+ idev = iommufd_get_device(ucmd, cmd->dev_id);
+ if (IS_ERR(idev))
+ return PTR_ERR(idev);
+
+ mutex_lock(&idev->igroup->lock);
+ if (idev->has_user_data) {
+ rc = -EEXIST;
+ goto out_unlock;
+ }
+
+ ops = dev_iommu_ops(idev->dev);
+ if (!ops->dev_user_data_len ||
+ !ops->set_dev_user_data ||
+ !ops->unset_dev_user_data) {
+ rc = -EOPNOTSUPP;
+ goto out_unlock;
+ }
+
+ data = kzalloc(ops->dev_user_data_len, GFP_KERNEL);
+ if (!data) {
+ rc = -ENOMEM;
+ goto out_unlock;
+ }
+
+ if (copy_struct_from_user(data, ops->dev_user_data_len,
+ u64_to_user_ptr(cmd->data_uptr),
+ cmd->data_len)) {
+ rc = -EFAULT;
+ goto out_free_data;
+ }
+
+ rc = ops->set_dev_user_data(idev->dev, data);
+ if (rc)
+ goto out_free_data;
+
+ idev->has_user_data = true;
+out_free_data:
+ kfree(data);
+out_unlock:
+ mutex_unlock(&idev->igroup->lock);
+ iommufd_put_object(&idev->obj);
+ return rc;
+}
+
+int iommufd_device_unset_data(struct iommufd_ucmd *ucmd)
+{
+ struct iommu_device_unset_data *cmd = ucmd->cmd;
+ struct iommufd_device *idev;
+ int rc = 0;
+
+ idev = iommufd_get_device(ucmd, cmd->dev_id);
+ if (IS_ERR(idev))
+ return PTR_ERR(idev);
+
+ mutex_lock(&idev->igroup->lock);
+ if (!idev->has_user_data) {
+ rc = -ENOENT;
+ goto out_unlock;
+ }
+
+ dev_iommu_ops(idev->dev)->unset_dev_user_data(idev->dev);
+ idev->has_user_data = false;
+out_unlock:
+ mutex_unlock(&idev->igroup->lock);
+ iommufd_put_object(&idev->obj);
+ return rc;
+}
+
void iommufd_access_destroy_object(struct iommufd_object *obj)
{
struct iommufd_access *access =
diff --git a/drivers/iommu/iommufd/iommufd_private.h b/drivers/iommu/iommufd/iommufd_private.h
index 35d1294b2f06..98eaef9c41d3 100644
--- a/drivers/iommu/iommufd/iommufd_private.h
+++ b/drivers/iommu/iommufd/iommufd_private.h
@@ -309,6 +309,7 @@ struct iommufd_device {
/* always the physical device */
struct device *dev;
bool enforce_cache_coherency;
+ bool has_user_data;
};
static inline struct iommufd_device *
@@ -321,6 +322,8 @@ iommufd_get_device(struct iommufd_ucmd *ucmd, u32 id)
void iommufd_device_destroy(struct iommufd_object *obj);
int iommufd_device_get_hw_info(struct iommufd_ucmd *ucmd);
+int iommufd_device_set_data(struct iommufd_ucmd *ucmd);
+int iommufd_device_unset_data(struct iommufd_ucmd *ucmd);
struct iommufd_access {
struct iommufd_object obj;
diff --git a/drivers/iommu/iommufd/main.c b/drivers/iommu/iommufd/main.c
index b84b22487b7d..bd4753512990 100644
--- a/drivers/iommu/iommufd/main.c
+++ b/drivers/iommu/iommufd/main.c
@@ -326,6 +326,10 @@ static const struct iommufd_ioctl_op iommufd_ioctl_ops[] = {
val64),
IOCTL_OP(IOMMU_VFIO_IOAS, iommufd_vfio_ioas, struct iommu_vfio_ioas,
__reserved),
+ IOCTL_OP(IOMMU_DEVICE_SET_DATA, iommufd_device_set_data,
+ struct iommu_device_set_data, data_len),
+ IOCTL_OP(IOMMU_DEVICE_UNSET_DATA, iommufd_device_unset_data,
+ struct iommu_device_unset_data, dev_id),
#ifdef CONFIG_IOMMUFD_TEST
IOCTL_OP(IOMMU_TEST_CMD, iommufd_test, struct iommu_test_cmd, last),
#endif
diff --git a/include/uapi/linux/iommufd.h b/include/uapi/linux/iommufd.h
index ac023ea6040a..b39b0e2e46b1 100644
--- a/include/uapi/linux/iommufd.h
+++ b/include/uapi/linux/iommufd.h
@@ -51,6 +51,8 @@ enum {
IOMMUFD_CMD_HWPT_ALLOC,
IOMMUFD_CMD_DEVICE_GET_HW_INFO,
IOMMUFD_CMD_HWPT_INVALIDATE,
+ IOMMUFD_CMD_DEVICE_SET_DATA,
+ IOMMUFD_CMD_DEVICE_UNSET_DATA,
};
/**
@@ -626,4 +628,34 @@ struct iommu_hwpt_invalidate {
__aligned_u64 data_uptr;
};
#define IOMMU_HWPT_INVALIDATE _IO(IOMMUFD_TYPE, IOMMUFD_CMD_HWPT_INVALIDATE)
+
+/**
+ * struct iommu_device_set_data - ioctl(IOMMU_DEVICE_SET_DATA)
+ * @size: sizeof(struct iommu_device_set_data)
+ * @dev_id: The device to set an iommu specific device data
+ * @data_uptr: User pointer of the device user data
+ * @data_len: Length of the device user data
+ *
+ * The device data must be unset using ioctl(IOMMU_DEVICE_UNSET_DATA), before
+ * another ioctl(IOMMU_DEVICE_SET_DATA) call or before the device itself gets
+ * unbind'd from the iommufd context.
+ */
+struct iommu_device_set_data {
+ __u32 size;
+ __u32 dev_id;
+ __aligned_u64 data_uptr;
+ __u32 data_len;
+};
+#define IOMMU_DEVICE_SET_DATA _IO(IOMMUFD_TYPE, IOMMUFD_CMD_DEVICE_SET_DATA)
+
+/**
+ * struct iommu_device_unset_data - ioctl(IOMMU_DEVICE_UNSET_DATA)
+ * @size: sizeof(struct iommu_device_unset_data)
+ * @dev_id: The device to unset its device user data
+ */
+struct iommu_device_unset_data {
+ __u32 size;
+ __u32 dev_id;
+};
+#define IOMMU_DEVICE_UNSET_DATA _IO(IOMMUFD_TYPE, IOMMUFD_CMD_DEVICE_UNSET_DATA)
#endif
--
2.40.0
_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
^ permalink raw reply related [flat|nested] 7+ messages in thread* Re: [PATCH RFC v3 2/4] iommufd: Add IOMMUFD_CMD_DEVICE_SET_DATA and IOMMUFD_CMD_DEVICE_UNSET_DATA
2023-04-23 7:40 ` [PATCH RFC v3 2/4] iommufd: Add IOMMUFD_CMD_DEVICE_SET_DATA and IOMMUFD_CMD_DEVICE_UNSET_DATA Nicolin Chen
@ 2023-04-24 2:44 ` Baolu Lu
2023-04-24 18:50 ` Nicolin Chen
0 siblings, 1 reply; 7+ messages in thread
From: Baolu Lu @ 2023-04-24 2:44 UTC (permalink / raw)
To: Nicolin Chen, jgg, kevin.tian
Cc: baolu.lu, robin.murphy, eric.auger, yi.l.liu, will, joro,
shameerali.kolothum.thodi, jean-philippe, linux-arm-kernel, iommu,
linux-kernel
On 4/23/23 3:40 PM, Nicolin Chen wrote:
> Add a new pair of ioctls to allow user space to set and unset its iommu-
> specific device data for a passthrough device that's behind the iommu.
>
> On platforms with SMMUv3, this new uAPIs will be used to forward a user
> space virtual Stream ID of a passthrough device to link to its physical
> Stream ID and log into a lookup table, in order for the host kernel to
> later run sanity on ATC invalidation requests from the user space, with
> ATC_INV commands that have SID fields (virtual Stream IDs).
>
> Signed-off-by: Nicolin Chen <nicolinc@nvidia.com>
> ---
> drivers/iommu/iommufd/device.c | 81 +++++++++++++++++++++++++
> drivers/iommu/iommufd/iommufd_private.h | 3 +
> drivers/iommu/iommufd/main.c | 4 ++
> include/uapi/linux/iommufd.h | 32 ++++++++++
> 4 files changed, 120 insertions(+)
>
> diff --git a/drivers/iommu/iommufd/device.c b/drivers/iommu/iommufd/device.c
> index c649a3403797..9480cd36a8bd 100644
> --- a/drivers/iommu/iommufd/device.c
> +++ b/drivers/iommu/iommufd/device.c
> @@ -136,6 +136,8 @@ void iommufd_device_destroy(struct iommufd_object *obj)
> struct iommufd_device *idev =
> container_of(obj, struct iommufd_device, obj);
>
> + if (WARN_ON(idev->has_user_data))
> + dev_iommu_ops(idev->dev)->unset_dev_user_data(idev->dev);
Do you really need this WARN_ON()? The user space application can easily
trigger this kernel WARN() by setting the user data and forgetting to
unset it.
Best regards,
baolu
_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH RFC v3 2/4] iommufd: Add IOMMUFD_CMD_DEVICE_SET_DATA and IOMMUFD_CMD_DEVICE_UNSET_DATA
2023-04-24 2:44 ` Baolu Lu
@ 2023-04-24 18:50 ` Nicolin Chen
0 siblings, 0 replies; 7+ messages in thread
From: Nicolin Chen @ 2023-04-24 18:50 UTC (permalink / raw)
To: Baolu Lu
Cc: jgg, kevin.tian, robin.murphy, eric.auger, yi.l.liu, will, joro,
shameerali.kolothum.thodi, jean-philippe, linux-arm-kernel, iommu,
linux-kernel
Hi Baolu,
On Mon, Apr 24, 2023 at 10:44:08AM +0800, Baolu Lu wrote:
> On 4/23/23 3:40 PM, Nicolin Chen wrote:
> > Add a new pair of ioctls to allow user space to set and unset its iommu-
> > specific device data for a passthrough device that's behind the iommu.
> >
> > On platforms with SMMUv3, this new uAPIs will be used to forward a user
> > space virtual Stream ID of a passthrough device to link to its physical
> > Stream ID and log into a lookup table, in order for the host kernel to
> > later run sanity on ATC invalidation requests from the user space, with
> > ATC_INV commands that have SID fields (virtual Stream IDs).
> >
> > Signed-off-by: Nicolin Chen <nicolinc@nvidia.com>
> > ---
> > drivers/iommu/iommufd/device.c | 81 +++++++++++++++++++++++++
> > drivers/iommu/iommufd/iommufd_private.h | 3 +
> > drivers/iommu/iommufd/main.c | 4 ++
> > include/uapi/linux/iommufd.h | 32 ++++++++++
> > 4 files changed, 120 insertions(+)
> >
> > diff --git a/drivers/iommu/iommufd/device.c b/drivers/iommu/iommufd/device.c
> > index c649a3403797..9480cd36a8bd 100644
> > --- a/drivers/iommu/iommufd/device.c
> > +++ b/drivers/iommu/iommufd/device.c
> > @@ -136,6 +136,8 @@ void iommufd_device_destroy(struct iommufd_object *obj)
> > struct iommufd_device *idev =
> > container_of(obj, struct iommufd_device, obj);
> >
> > + if (WARN_ON(idev->has_user_data))
> > + dev_iommu_ops(idev->dev)->unset_dev_user_data(idev->dev);
>
> Do you really need this WARN_ON()? The user space application can easily
> trigger this kernel WARN() by setting the user data and forgetting to
> unset it.
I can drop that, since it's a user triggerable one.
Thanks
Nic
_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
^ permalink raw reply [flat|nested] 7+ messages in thread
* [PATCH RFC v3 3/4] iommufd/selftest: Add IOMMU_TEST_OP_DEV_CHECK_DATA
2023-04-23 7:40 [PATCH RFC v3 0/4] Add set_dev_data and unset_dev_data support Nicolin Chen
2023-04-23 7:40 ` [PATCH RFC v3 1/4] iommu: Add set/unset_dev_user_data ops Nicolin Chen
2023-04-23 7:40 ` [PATCH RFC v3 2/4] iommufd: Add IOMMUFD_CMD_DEVICE_SET_DATA and IOMMUFD_CMD_DEVICE_UNSET_DATA Nicolin Chen
@ 2023-04-23 7:40 ` Nicolin Chen
2023-04-23 7:40 ` [PATCH RFC v3 4/4] iommufd/selftests: Add coverage for IOMMU_DEVICE_SET/UNSET_DATA Nicolin Chen
3 siblings, 0 replies; 7+ messages in thread
From: Nicolin Chen @ 2023-04-23 7:40 UTC (permalink / raw)
To: jgg, kevin.tian
Cc: robin.murphy, eric.auger, yi.l.liu, baolu.lu, will, joro,
shameerali.kolothum.thodi, jean-philippe, linux-arm-kernel, iommu,
linux-kernel
Add mock_domain_set/unset_dev_user_data and iommufd_test_dev_check_data to
allow testing on IOMMUFD_CMD_DEVICE_SET/UNSET_DATA ioctls from user space
selftest via IOMMU_TEST_OP_DEV_CHECK_DATA.
Signed-off-by: Nicolin Chen <nicolinc@nvidia.com>
---
drivers/iommu/iommufd/iommufd_test.h | 15 ++++++++++
drivers/iommu/iommufd/selftest.c | 42 ++++++++++++++++++++++++++++
2 files changed, 57 insertions(+)
diff --git a/drivers/iommu/iommufd/iommufd_test.h b/drivers/iommu/iommufd/iommufd_test.h
index 8f93d83fb52a..b27db4cfb230 100644
--- a/drivers/iommu/iommufd/iommufd_test.h
+++ b/drivers/iommu/iommufd/iommufd_test.h
@@ -20,6 +20,7 @@ enum {
IOMMU_TEST_OP_MOCK_DOMAIN_REPLACE,
IOMMU_TEST_OP_ACCESS_REPLACE_IOAS,
IOMMU_TEST_OP_MD_CHECK_IOTLB,
+ IOMMU_TEST_OP_DEV_CHECK_DATA,
};
enum {
@@ -99,6 +100,9 @@ struct iommu_test_cmd {
struct {
__u32 iotlb;
} check_iotlb;
+ struct {
+ __u32 val;
+ } check_dev_data;
};
__u32 last;
};
@@ -119,6 +123,17 @@ struct iommu_test_hw_info {
__u32 test_reg;
};
+#define IOMMU_DEVICE_DATA_SELFTEST 0xdadbeef
+
+/**
+ * struct iommu_test_device_data
+ *
+ * @val: Should be set to IOMMU_DEVICE_DATA_SELFTEST or unset to 0x0
+ */
+struct iommu_test_device_data {
+ __u32 val;
+};
+
/* Should not be equal to any defined value in enum iommu_hwpt_type */
#define IOMMU_HWPT_TYPE_SELFTTEST 0xbadbeef
#define IOMMU_HWPT_TYPE_BITMAP_SELFTTEST U64_MAX
diff --git a/drivers/iommu/iommufd/selftest.c b/drivers/iommu/iommufd/selftest.c
index dc2b696acf2a..698976547d2d 100644
--- a/drivers/iommu/iommufd/selftest.c
+++ b/drivers/iommu/iommufd/selftest.c
@@ -95,6 +95,7 @@ enum selftest_obj_type {
struct mock_dev {
struct device dev;
+ u32 dev_data;
};
struct selftest_obj {
@@ -332,6 +333,23 @@ static void mock_domain_set_plaform_dma_ops(struct device *dev)
*/
}
+static int mock_domain_set_dev_user_data(struct device *dev,
+ const void *user_data)
+{
+ struct mock_dev *mdev = container_of(dev, struct mock_dev, dev);
+ const struct iommu_test_device_data *data = user_data;
+
+ mdev->dev_data = data->val;
+ return 0;
+}
+
+static void mock_domain_unset_dev_user_data(struct device *dev)
+{
+ struct mock_dev *mdev = container_of(dev, struct mock_dev, dev);
+
+ mdev->dev_data = 0;
+}
+
static const struct iommu_ops mock_ops = {
.owner = THIS_MODULE,
.pgsize_bitmap = MOCK_IO_PAGE_SIZE,
@@ -342,6 +360,9 @@ static const struct iommu_ops mock_ops = {
.domain_alloc_user = mock_domain_alloc_user,
.capable = mock_domain_capable,
.set_platform_dma_ops = mock_domain_set_plaform_dma_ops,
+ .set_dev_user_data = mock_domain_set_dev_user_data,
+ .unset_dev_user_data = mock_domain_unset_dev_user_data,
+ .dev_user_data_len = sizeof(struct iommu_test_device_data),
.default_domain_ops =
&(struct iommu_domain_ops){
.free = mock_domain_free,
@@ -704,6 +725,24 @@ static int iommufd_test_md_check_iotlb(struct iommufd_ucmd *ucmd,
return rc;
}
+static int iommufd_test_dev_check_data(struct iommufd_ucmd *ucmd,
+ u32 dev_id, u32 val)
+{
+ struct iommufd_device *idev;
+ struct mock_dev *mdev;
+ int rc = 0;
+
+ idev = iommufd_get_device(ucmd, dev_id);
+ if (IS_ERR(idev))
+ return PTR_ERR(idev);
+ mdev = container_of(idev->dev, struct mock_dev, dev);
+
+ if (mdev->dev_data != val)
+ rc = -EINVAL;
+ iommufd_put_object(&idev->obj);
+ return rc;
+}
+
struct selftest_access {
struct iommufd_access *access;
struct file *file;
@@ -1120,6 +1159,9 @@ int iommufd_test(struct iommufd_ucmd *ucmd)
case IOMMU_TEST_OP_MD_CHECK_IOTLB:
return iommufd_test_md_check_iotlb(ucmd, cmd->id,
cmd->check_iotlb.iotlb);
+ case IOMMU_TEST_OP_DEV_CHECK_DATA:
+ return iommufd_test_dev_check_data(ucmd, cmd->id,
+ cmd->check_dev_data.val);
case IOMMU_TEST_OP_CREATE_ACCESS:
return iommufd_test_create_access(ucmd, cmd->id,
cmd->create_access.flags);
--
2.40.0
_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
^ permalink raw reply related [flat|nested] 7+ messages in thread* [PATCH RFC v3 4/4] iommufd/selftests: Add coverage for IOMMU_DEVICE_SET/UNSET_DATA
2023-04-23 7:40 [PATCH RFC v3 0/4] Add set_dev_data and unset_dev_data support Nicolin Chen
` (2 preceding siblings ...)
2023-04-23 7:40 ` [PATCH RFC v3 3/4] iommufd/selftest: Add IOMMU_TEST_OP_DEV_CHECK_DATA Nicolin Chen
@ 2023-04-23 7:40 ` Nicolin Chen
3 siblings, 0 replies; 7+ messages in thread
From: Nicolin Chen @ 2023-04-23 7:40 UTC (permalink / raw)
To: jgg, kevin.tian
Cc: robin.murphy, eric.auger, yi.l.liu, baolu.lu, will, joro,
shameerali.kolothum.thodi, jean-philippe, linux-arm-kernel, iommu,
linux-kernel
Add a new IOMMU_TEST_OP_DEV_CHECK_DATA to verify whether the dev_data
is properly set/unset by the IOMMU_DEVICE_SET/UNSET_DATA.
Signed-off-by: Nicolin Chen <nicolinc@nvidia.com>
---
tools/testing/selftests/iommu/iommufd.c | 20 +++++++
tools/testing/selftests/iommu/iommufd_utils.h | 59 +++++++++++++++++++
2 files changed, 79 insertions(+)
diff --git a/tools/testing/selftests/iommu/iommufd.c b/tools/testing/selftests/iommu/iommufd.c
index 7291e1c4242a..8265ec6a0606 100644
--- a/tools/testing/selftests/iommu/iommufd.c
+++ b/tools/testing/selftests/iommu/iommufd.c
@@ -1483,6 +1483,26 @@ TEST_F(iommufd_mock_domain, alloc_hwpt)
}
}
+TEST_F(iommufd_mock_domain, set_dev_data)
+{
+ struct iommu_test_device_data dev_data = {
+ .val = IOMMU_DEVICE_DATA_SELFTEST,
+ };
+ int i;
+
+ for (i = 0; i != variant->mock_domains; i++) {
+ test_err_device_set_data(ENOENT, 0, &dev_data);
+ test_err_device_set_data(EINVAL, self->idev_ids[i], NULL);
+ test_cmd_device_set_data(self->idev_ids[i], &dev_data);
+ test_err_device_set_data(EEXIST, self->idev_ids[i], &dev_data);
+ test_cmd_dev_check_data(self->idev_ids[i], dev_data.val);
+ test_err_device_unset_data(ENOENT, 0);
+ test_cmd_device_unset_data(self->idev_ids[i]);
+ test_err_device_unset_data(ENOENT, self->idev_ids[i]);
+ test_cmd_dev_check_data(self->idev_ids[i], 0);
+ }
+}
+
/* VFIO compatibility IOCTLs */
TEST_F(iommufd, simple_ioctls)
diff --git a/tools/testing/selftests/iommu/iommufd_utils.h b/tools/testing/selftests/iommu/iommufd_utils.h
index f8ba7b09078b..da04f802c675 100644
--- a/tools/testing/selftests/iommu/iommufd_utils.h
+++ b/tools/testing/selftests/iommu/iommufd_utils.h
@@ -445,3 +445,62 @@ static int _test_cmd_device_get_hw_info(int fd, __u32 device_id,
EXPECT_ERRNO(_errno, \
_test_cmd_device_get_hw_info(self->fd, device_id, \
data_len, data))
+
+#define test_cmd_dev_check_data(device_id, expected) \
+ ({ \
+ struct iommu_test_cmd test_cmd = { \
+ .size = sizeof(test_cmd), \
+ .op = IOMMU_TEST_OP_DEV_CHECK_DATA, \
+ .id = device_id, \
+ .check_dev_data = { .val = expected }, \
+ }; \
+ ASSERT_EQ(0, \
+ ioctl(self->fd, \
+ _IOMMU_TEST_CMD(IOMMU_TEST_OP_DEV_CHECK_DATA), \
+ &test_cmd)); \
+ })
+
+static int _test_cmd_device_set_data(int fd, __u32 device_id,
+ struct iommu_test_device_data *dev_data)
+{
+ struct iommu_device_set_data cmd = {
+ .size = sizeof(cmd),
+ .dev_id = device_id,
+ .data_uptr = (uint64_t)dev_data,
+ .data_len = sizeof(*dev_data),
+ };
+ int ret;
+
+ ret = ioctl(fd, IOMMU_DEVICE_SET_DATA, &cmd);
+ if (ret)
+ return ret;
+ return 0;
+}
+
+#define test_cmd_device_set_data(device_id, dev_data) \
+ ASSERT_EQ(0, _test_cmd_device_set_data(self->fd, device_id, dev_data))
+
+#define test_err_device_set_data(_errno, device_id, dev_data) \
+ EXPECT_ERRNO(_errno, \
+ _test_cmd_device_set_data(self->fd, device_id, dev_data))
+
+static int _test_cmd_device_unset_data(int fd, __u32 device_id)
+{
+ struct iommu_device_unset_data cmd = {
+ .size = sizeof(cmd),
+ .dev_id = device_id,
+ };
+ int ret;
+
+ ret = ioctl(fd, IOMMU_DEVICE_UNSET_DATA, &cmd);
+ if (ret)
+ return ret;
+ return 0;
+}
+
+#define test_cmd_device_unset_data(device_id) \
+ ASSERT_EQ(0, _test_cmd_device_unset_data(self->fd, device_id))
+
+#define test_err_device_unset_data(_errno, device_id) \
+ EXPECT_ERRNO(_errno, \
+ _test_cmd_device_unset_data(self->fd, device_id))
--
2.40.0
_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
^ permalink raw reply related [flat|nested] 7+ messages in thread