From: Nicolin Chen <nicolinc@nvidia.com>
To: <jgg@nvidia.com>, <kevin.tian@intel.com>, <will@kernel.org>
Cc: <joro@8bytes.org>, <suravee.suthikulpanit@amd.com>,
<robin.murphy@arm.com>, <dwmw2@infradead.org>,
<baolu.lu@linux.intel.com>, <shuah@kernel.org>,
<linux-kernel@vger.kernel.org>, <iommu@lists.linux.dev>,
<linux-arm-kernel@lists.infradead.org>,
<linux-kselftest@vger.kernel.org>
Subject: [PATCH v1 05/16] iommufd/viommu: Add IOMMU_VIOMMU_SET/UNSET_VDEV_ID ioctl
Date: Wed, 7 Aug 2024 13:10:46 -0700 [thread overview]
Message-ID: <e35a24d4337b985aabbcfe7857cac2186d4f61e9.1723061378.git.nicolinc@nvidia.com> (raw)
In-Reply-To: <cover.1723061377.git.nicolinc@nvidia.com>
Introduce a pair of new ioctls to set/unset a per-viommu virtual device id
that should be linked to a physical device id via a struct device pointer.
Continue the support IOMMU_VIOMMU_TYPE_DEFAULT for a core-managed viommu.
Provide a lookup function for drivers to load device pointer by a virtual
device id.
Signed-off-by: Nicolin Chen <nicolinc@nvidia.com>
---
drivers/iommu/iommufd/device.c | 9 ++
drivers/iommu/iommufd/iommufd_private.h | 20 ++++
drivers/iommu/iommufd/main.c | 6 ++
drivers/iommu/iommufd/viommu.c | 118 ++++++++++++++++++++++++
include/uapi/linux/iommufd.h | 40 ++++++++
5 files changed, 193 insertions(+)
diff --git a/drivers/iommu/iommufd/device.c b/drivers/iommu/iommufd/device.c
index 5fd3dd420290..ed29bc606f5e 100644
--- a/drivers/iommu/iommufd/device.c
+++ b/drivers/iommu/iommufd/device.c
@@ -135,7 +135,14 @@ void iommufd_device_destroy(struct iommufd_object *obj)
{
struct iommufd_device *idev =
container_of(obj, struct iommufd_device, obj);
+ struct iommufd_vdev_id *vdev_id, *curr;
+ list_for_each_entry(vdev_id, &idev->vdev_id_list, idev_item) {
+ curr = xa_cmpxchg(&vdev_id->viommu->vdev_ids, vdev_id->vdev_id,
+ vdev_id, NULL, GFP_KERNEL);
+ WARN_ON(curr != vdev_id);
+ kfree(vdev_id);
+ }
iommu_device_release_dma_owner(idev->dev);
iommufd_put_group(idev->igroup);
if (!iommufd_selftest_is_mock_dev(idev->dev))
@@ -217,6 +224,8 @@ struct iommufd_device *iommufd_device_bind(struct iommufd_ctx *ictx,
idev->igroup = igroup;
mutex_init(&idev->iopf_lock);
+ INIT_LIST_HEAD(&idev->vdev_id_list);
+
/*
* If the caller fails after this success it must call
* iommufd_unbind_device() which is safe since we hold this refcount.
diff --git a/drivers/iommu/iommufd/iommufd_private.h b/drivers/iommu/iommufd/iommufd_private.h
index 443575fd3dd4..10c63972b9ab 100644
--- a/drivers/iommu/iommufd/iommufd_private.h
+++ b/drivers/iommu/iommufd/iommufd_private.h
@@ -417,6 +417,7 @@ struct iommufd_device {
struct iommufd_ctx *ictx;
struct iommufd_group *igroup;
struct list_head group_item;
+ struct list_head vdev_id_list;
/* always the physical device */
struct device *dev;
bool enforce_cache_coherency;
@@ -533,12 +534,31 @@ struct iommufd_viommu {
struct iommufd_ctx *ictx;
struct iommu_device *iommu_dev;
struct iommufd_hwpt_paging *hwpt;
+ struct xarray vdev_ids;
unsigned int type;
};
+struct iommufd_vdev_id {
+ struct iommufd_viommu *viommu;
+ struct device *dev;
+ u64 vdev_id;
+
+ struct list_head idev_item;
+};
+
+static inline struct iommufd_viommu *
+iommufd_get_viommu(struct iommufd_ucmd *ucmd, u32 id)
+{
+ return container_of(iommufd_get_object(ucmd->ictx, id,
+ IOMMUFD_OBJ_VIOMMU),
+ struct iommufd_viommu, obj);
+}
+
int iommufd_viommu_alloc_ioctl(struct iommufd_ucmd *ucmd);
void iommufd_viommu_destroy(struct iommufd_object *obj);
+int iommufd_viommu_set_vdev_id(struct iommufd_ucmd *ucmd);
+int iommufd_viommu_unset_vdev_id(struct iommufd_ucmd *ucmd);
#ifdef CONFIG_IOMMUFD_TEST
int iommufd_test(struct iommufd_ucmd *ucmd);
diff --git a/drivers/iommu/iommufd/main.c b/drivers/iommu/iommufd/main.c
index 288ee51b6829..199ad90fa36b 100644
--- a/drivers/iommu/iommufd/main.c
+++ b/drivers/iommu/iommufd/main.c
@@ -334,6 +334,8 @@ union ucmd_buffer {
struct iommu_option option;
struct iommu_vfio_ioas vfio_ioas;
struct iommu_viommu_alloc viommu;
+ struct iommu_viommu_set_vdev_id set_vdev_id;
+ struct iommu_viommu_unset_vdev_id unset_vdev_id;
#ifdef CONFIG_IOMMUFD_TEST
struct iommu_test_cmd test;
#endif
@@ -387,6 +389,10 @@ 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_VIOMMU_SET_VDEV_ID, iommufd_viommu_set_vdev_id,
+ struct iommu_viommu_set_vdev_id, vdev_id),
+ IOCTL_OP(IOMMU_VIOMMU_UNSET_VDEV_ID, iommufd_viommu_unset_vdev_id,
+ struct iommu_viommu_unset_vdev_id, vdev_id),
#ifdef CONFIG_IOMMUFD_TEST
IOCTL_OP(IOMMU_TEST_CMD, iommufd_test, struct iommu_test_cmd, last),
#endif
diff --git a/drivers/iommu/iommufd/viommu.c b/drivers/iommu/iommufd/viommu.c
index 35ad6a77c9c1..05a688a471db 100644
--- a/drivers/iommu/iommufd/viommu.c
+++ b/drivers/iommu/iommufd/viommu.c
@@ -10,7 +10,14 @@ void iommufd_viommu_destroy(struct iommufd_object *obj)
{
struct iommufd_viommu *viommu =
container_of(obj, struct iommufd_viommu, obj);
+ struct iommufd_vdev_id *vdev_id;
+ unsigned long index;
+ xa_for_each(&viommu->vdev_ids, index, vdev_id) {
+ list_del(&vdev_id->idev_item);
+ kfree(vdev_id);
+ }
+ xa_destroy(&viommu->vdev_ids);
refcount_dec(&viommu->hwpt->common.obj.users);
}
@@ -73,3 +80,114 @@ int iommufd_viommu_alloc_ioctl(struct iommufd_ucmd *ucmd)
iommufd_put_object(ucmd->ictx, &idev->obj);
return rc;
}
+
+int iommufd_viommu_set_vdev_id(struct iommufd_ucmd *ucmd)
+{
+ struct iommu_viommu_set_vdev_id *cmd = ucmd->cmd;
+ struct iommufd_hwpt_nested *hwpt_nested;
+ struct iommufd_vdev_id *vdev_id, *curr;
+ struct iommufd_hw_pagetable *hwpt;
+ struct iommufd_viommu *viommu;
+ struct iommufd_device *idev;
+ int rc = 0;
+
+ if (cmd->vdev_id > ULONG_MAX)
+ return -EINVAL;
+
+ idev = iommufd_get_device(ucmd, cmd->dev_id);
+ if (IS_ERR(idev))
+ return PTR_ERR(idev);
+ hwpt = idev->igroup->hwpt;
+
+ if (hwpt == NULL || hwpt->obj.type != IOMMUFD_OBJ_HWPT_NESTED) {
+ rc = -EINVAL;
+ goto out_put_idev;
+ }
+ hwpt_nested = container_of(hwpt, struct iommufd_hwpt_nested, common);
+
+ viommu = iommufd_get_viommu(ucmd, cmd->viommu_id);
+ if (IS_ERR(viommu)) {
+ rc = PTR_ERR(viommu);
+ goto out_put_idev;
+ }
+
+ if (hwpt_nested->viommu != viommu) {
+ rc = -EINVAL;
+ goto out_put_viommu;
+ }
+
+ vdev_id = kzalloc(sizeof(*vdev_id), GFP_KERNEL);
+ if (IS_ERR(vdev_id)) {
+ rc = PTR_ERR(vdev_id);
+ goto out_put_viommu;
+ }
+
+ vdev_id->viommu = viommu;
+ vdev_id->dev = idev->dev;
+ vdev_id->vdev_id = cmd->vdev_id;
+
+ curr = xa_cmpxchg(&viommu->vdev_ids, cmd->vdev_id,
+ NULL, vdev_id, GFP_KERNEL);
+ if (curr) {
+ rc = xa_err(curr) ? : -EBUSY;
+ goto out_free_vdev_id;
+ }
+
+ list_add_tail(&vdev_id->idev_item, &idev->vdev_id_list);
+ goto out_put_viommu;
+
+out_free_vdev_id:
+ kfree(vdev_id);
+out_put_viommu:
+ iommufd_put_object(ucmd->ictx, &viommu->obj);
+out_put_idev:
+ iommufd_put_object(ucmd->ictx, &idev->obj);
+ return rc;
+}
+
+static struct device *
+iommufd_viommu_find_device(struct iommufd_viommu *viommu, u64 id)
+{
+ struct iommufd_vdev_id *vdev_id;
+
+ xa_lock(&viommu->vdev_ids);
+ vdev_id = xa_load(&viommu->vdev_ids, (unsigned long)id);
+ xa_unlock(&viommu->vdev_ids);
+ if (!vdev_id || vdev_id->vdev_id != id)
+ return NULL;
+ return vdev_id->dev;
+}
+
+int iommufd_viommu_unset_vdev_id(struct iommufd_ucmd *ucmd)
+{
+ struct iommu_viommu_unset_vdev_id *cmd = ucmd->cmd;
+ struct iommufd_vdev_id *vdev_id;
+ struct iommufd_viommu *viommu;
+ struct iommufd_device *idev;
+ int rc = 0;
+
+ idev = iommufd_get_device(ucmd, cmd->dev_id);
+ if (IS_ERR(idev))
+ return PTR_ERR(idev);
+
+ viommu = iommufd_get_viommu(ucmd, cmd->viommu_id);
+ if (IS_ERR(viommu)) {
+ rc = PTR_ERR(viommu);
+ goto out_put_idev;
+ }
+
+ if (idev->dev != iommufd_viommu_find_device(viommu, cmd->vdev_id)) {
+ rc = -EINVAL;
+ goto out_put_viommu;
+ }
+
+ vdev_id = xa_erase(&viommu->vdev_ids, cmd->vdev_id);
+ list_del(&vdev_id->idev_item);
+ kfree(vdev_id);
+
+out_put_viommu:
+ iommufd_put_object(ucmd->ictx, &viommu->obj);
+out_put_idev:
+ iommufd_put_object(ucmd->ictx, &idev->obj);
+ return rc;
+}
diff --git a/include/uapi/linux/iommufd.h b/include/uapi/linux/iommufd.h
index 0e384331a9c8..d5e72682ba57 100644
--- a/include/uapi/linux/iommufd.h
+++ b/include/uapi/linux/iommufd.h
@@ -52,6 +52,8 @@ enum {
IOMMUFD_CMD_HWPT_INVALIDATE = 0x8d,
IOMMUFD_CMD_FAULT_QUEUE_ALLOC = 0x8e,
IOMMUFD_CMD_VIOMMU_ALLOC = 0x8f,
+ IOMMUFD_CMD_VIOMMU_SET_VDEV_ID = 0x90,
+ IOMMUFD_CMD_VIOMMU_UNSET_VDEV_ID = 0x91,
};
/**
@@ -906,4 +908,42 @@ struct iommu_viommu_alloc {
__u32 out_viommu_id;
};
#define IOMMU_VIOMMU_ALLOC _IO(IOMMUFD_TYPE, IOMMUFD_CMD_VIOMMU_ALLOC)
+
+/**
+ * struct iommu_viommu_set_vdev_id - ioctl(IOMMU_VIOMMU_SET_VDEV_ID)
+ * @size: sizeof(struct iommu_viommu_set_vdev_id)
+ * @viommu_id: viommu ID to associate with the device to store its virtual ID
+ * @dev_id: device ID to set its virtual ID
+ * @__reserved: Must be 0
+ * @vdev_id: Virtual device ID
+ *
+ * Set a viommu-specific virtual ID of a device
+ */
+struct iommu_viommu_set_vdev_id {
+ __u32 size;
+ __u32 viommu_id;
+ __u32 dev_id;
+ __u32 __reserved;
+ __aligned_u64 vdev_id;
+};
+#define IOMMU_VIOMMU_SET_VDEV_ID _IO(IOMMUFD_TYPE, IOMMUFD_CMD_VIOMMU_SET_VDEV_ID)
+
+/**
+ * struct iommu_viommu_unset_vdev_id - ioctl(IOMMU_VIOMMU_UNSET_VDEV_ID)
+ * @size: sizeof(struct iommu_viommu_unset_vdev_id)
+ * @viommu_id: viommu ID associated with the device to delete its virtual ID
+ * @dev_id: device ID to unset its virtual ID
+ * @__reserved: Must be 0
+ * @vdev_id: Virtual device ID (for verification)
+ *
+ * Unset a viommu-specific virtual ID of a device
+ */
+struct iommu_viommu_unset_vdev_id {
+ __u32 size;
+ __u32 viommu_id;
+ __u32 dev_id;
+ __u32 __reserved;
+ __aligned_u64 vdev_id;
+};
+#define IOMMU_VIOMMU_UNSET_VDEV_ID _IO(IOMMUFD_TYPE, IOMMUFD_CMD_VIOMMU_UNSET_VDEV_ID)
#endif
--
2.43.0
next prev parent reply other threads:[~2024-08-07 20:11 UTC|newest]
Thread overview: 47+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-08-07 20:10 [PATCH v1 00/16] iommufd: Add VIOMMU infrastructure (Part-1) Nicolin Chen
2024-08-07 20:10 ` [PATCH v1 01/16] iommufd/viommu: Add IOMMUFD_OBJ_VIOMMU and IOMMU_VIOMMU_ALLOC ioctl Nicolin Chen
2024-08-14 16:50 ` Nicolin Chen
2024-08-15 18:11 ` Jason Gunthorpe
2024-08-15 18:20 ` Nicolin Chen
2024-08-15 23:37 ` Jason Gunthorpe
2024-08-15 18:31 ` Jason Gunthorpe
2024-08-07 20:10 ` [PATCH v1 02/16] iommu: Pass in a viommu pointer to domain_alloc_user op Nicolin Chen
2024-08-07 20:10 ` [PATCH v1 03/16] iommufd: Allow pt_id to carry viommu_id for IOMMU_HWPT_ALLOC Nicolin Chen
2024-08-07 20:10 ` [PATCH v1 04/16] iommufd/selftest: Add IOMMU_VIOMMU_ALLOC test coverage Nicolin Chen
2024-08-07 20:10 ` Nicolin Chen [this message]
2024-08-14 17:09 ` [PATCH v1 05/16] iommufd/viommu: Add IOMMU_VIOMMU_SET/UNSET_VDEV_ID ioctl Nicolin Chen
2024-08-14 22:02 ` Jason Gunthorpe
2024-08-15 19:08 ` Jason Gunthorpe
2024-08-15 19:46 ` Nicolin Chen
2024-08-15 19:53 ` Nicolin Chen
2024-08-15 23:42 ` Jason Gunthorpe
2024-08-15 23:41 ` Jason Gunthorpe
2024-08-16 0:21 ` Nicolin Chen
2024-08-19 17:33 ` Jason Gunthorpe
2024-08-19 18:10 ` Nicolin Chen
2024-08-19 18:26 ` Jason Gunthorpe
2024-08-07 20:10 ` [PATCH v1 06/16] iommufd/selftest: Add IOMMU_VIOMMU_SET/UNSET_VDEV_ID test coverage Nicolin Chen
2024-08-07 20:10 ` [PATCH v1 07/16] iommufd/viommu: Add cache_invalidate for IOMMU_VIOMMU_TYPE_DEFAULT Nicolin Chen
2024-08-07 20:10 ` [PATCH v1 08/16] iommufd/viommu: Add IOMMU_VIOMMU_INVALIDATE ioctl Nicolin Chen
2024-08-15 23:24 ` Jason Gunthorpe
2024-08-15 23:51 ` Nicolin Chen
2024-08-19 17:30 ` Jason Gunthorpe
2024-08-19 17:49 ` Nicolin Chen
2024-08-19 18:20 ` Jason Gunthorpe
2024-08-19 18:22 ` Nicolin Chen
2024-08-07 20:10 ` [PATCH v1 09/16] iommufd/viommu: Make iommufd_viommu_find_device a public API Nicolin Chen
2024-08-07 20:10 ` [PATCH v1 10/16] iommufd/selftest: Add mock_viommu_invalidate_user op Nicolin Chen
2024-08-07 20:10 ` [PATCH v1 11/16] iommufd/selftest: Add IOMMU_TEST_OP_DEV_CHECK_CACHE test command Nicolin Chen
2024-08-07 20:10 ` [PATCH v1 12/16] iommufd/selftest: Add coverage for IOMMU_VIOMMU_INVALIDATE ioctl Nicolin Chen
2024-08-07 20:10 ` [PATCH v1 13/16] iommufd/viommu: Add iommufd_viommu_to_parent_domain helper Nicolin Chen
2024-08-07 20:10 ` [PATCH v1 14/16] iommu/arm-smmu-v3: Extract an __arm_smmu_cache_invalidate_user helper Nicolin Chen
2024-08-07 20:10 ` [PATCH v1 15/16] iommu/arm-smmu-v3: Add viommu cache invalidation support Nicolin Chen
2024-08-15 23:36 ` Jason Gunthorpe
2024-08-16 0:50 ` Nicolin Chen
2024-08-19 17:36 ` Jason Gunthorpe
2024-08-19 18:19 ` Nicolin Chen
2024-08-19 18:28 ` Jason Gunthorpe
2024-08-19 18:38 ` Nicolin Chen
2024-08-19 18:47 ` Jason Gunthorpe
2024-08-19 18:54 ` Nicolin Chen
2024-08-07 20:10 ` [PATCH v1 16/16] iommu/arm-smmu-v3: Allow ATS for IOMMU_DOMAIN_NESTED Nicolin Chen
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=e35a24d4337b985aabbcfe7857cac2186d4f61e9.1723061378.git.nicolinc@nvidia.com \
--to=nicolinc@nvidia.com \
--cc=baolu.lu@linux.intel.com \
--cc=dwmw2@infradead.org \
--cc=iommu@lists.linux.dev \
--cc=jgg@nvidia.com \
--cc=joro@8bytes.org \
--cc=kevin.tian@intel.com \
--cc=linux-arm-kernel@lists.infradead.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-kselftest@vger.kernel.org \
--cc=robin.murphy@arm.com \
--cc=shuah@kernel.org \
--cc=suravee.suthikulpanit@amd.com \
--cc=will@kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®