[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <20251202230303.1017519-6-skhawaja@google.com>
Date: Tue, 2 Dec 2025 23:02:35 +0000
From: Samiullah Khawaja <skhawaja@...gle.com>
To: David Woodhouse <dwmw2@...radead.org>, Lu Baolu <baolu.lu@...ux.intel.com>,
Joerg Roedel <joro@...tes.org>, Will Deacon <will@...nel.org>,
Pasha Tatashin <pasha.tatashin@...een.com>, Jason Gunthorpe <jgg@...pe.ca>, iommu@...ts.linux.dev
Cc: YiFei Zhu <zhuyifei@...gle.com>, Robin Murphy <robin.murphy@....com>,
Pratyush Yadav <pratyush@...nel.org>, Samiullah Khawaja <skhawaja@...gle.com>,
Kevin Tian <kevin.tian@...el.com>, Alex Williamson <alex@...zbot.org>, linux-kernel@...r.kernel.org,
Saeed Mahameed <saeedm@...dia.com>, Adithya Jayachandran <ajayachandra@...dia.com>,
Parav Pandit <parav@...dia.com>, Leon Romanovsky <leonro@...dia.com>, William Tu <witu@...dia.com>,
Vipin Sharma <vipinsh@...gle.com>, dmatlack@...gle.com, Chris Li <chrisl@...nel.org>,
praan@...gle.com
Subject: [RFC PATCH v2 05/32] iommufd-lu: Implement ioctl to let userspace
mark an HWPT to be preserved
From: YiFei Zhu <zhuyifei@...gle.com>
Userspace provides a token, which will then be used at restore to
identify this HWPT.
A skeleton for te restore-side ioctl is also added.
Signed-off-by: YiFei Zhu <zhuyifei@...gle.com>
---
drivers/iommu/iommufd/iommufd_private.h | 18 ++++++++++
drivers/iommu/iommufd/liveupdate.c | 46 +++++++++++++++++++++++++
drivers/iommu/iommufd/main.c | 4 +++
include/uapi/linux/iommufd.h | 41 ++++++++++++++++++++++
4 files changed, 109 insertions(+)
diff --git a/drivers/iommu/iommufd/iommufd_private.h b/drivers/iommu/iommufd/iommufd_private.h
index dfe9120aced0..54c7c9888de3 100644
--- a/drivers/iommu/iommufd/iommufd_private.h
+++ b/drivers/iommu/iommufd/iommufd_private.h
@@ -381,6 +381,10 @@ struct iommufd_hwpt_paging {
bool auto_domain : 1;
bool enforce_cache_coherency : 1;
bool nest_parent : 1;
+#ifdef CONFIG_LIVEUPDATE
+ bool lu_preserved : 1;
+ u32 lu_token;
+#endif
/* Head at iommufd_ioas::hwpt_list */
struct list_head hwpt_item;
struct iommufd_sw_msi_maps present_sw_msi;
@@ -721,6 +725,10 @@ iommufd_get_vdevice(struct iommufd_ctx *ictx, u32 id)
#ifdef CONFIG_LIVEUPDATE
int iommufd_liveupdate_register_lufs(void);
int iommufd_liveupdate_unregister_lufs(void);
+
+
+int iommufd_hwpt_lu_set_preserved(struct iommufd_ucmd *ucmd);
+int iommufd_hwpt_lu_restore(struct iommufd_ucmd *ucmd);
#else
static inline int iommufd_liveupdate_register_lufs(void)
{
@@ -731,6 +739,16 @@ static inline int iommufd_liveupdate_unregister_lufs(void)
{
return 0;
}
+
+static inline int iommufd_hwpt_lu_set_preserved(struct iommufd_ucmd *ucmd)
+{
+ return -ENOTTY;
+}
+
+static inline int iommufd_hwpt_lu_restore(struct iommufd_ucmd *ucmd)
+{
+ return -ENOTTY;
+}
#endif
#ifdef CONFIG_IOMMUFD_TEST
diff --git a/drivers/iommu/iommufd/liveupdate.c b/drivers/iommu/iommufd/liveupdate.c
index a9b5956c0dee..83d1b888d914 100644
--- a/drivers/iommu/iommufd/liveupdate.c
+++ b/drivers/iommu/iommufd/liveupdate.c
@@ -12,6 +12,47 @@
#include "iommufd_private.h"
+int iommufd_hwpt_lu_set_preserved(struct iommufd_ucmd *ucmd)
+{
+ struct iommu_hwpt_lu_set_preserved *cmd = ucmd->cmd;
+ struct iommufd_hwpt_paging *hwpt_target, *hwpt;
+ struct iommufd_ctx *ictx = ucmd->ictx;
+ struct iommufd_object *obj;
+ unsigned long index;
+ int rc = 0;
+
+ /* TODO: return error if iommufd is already preserved. */
+
+ hwpt_target = iommufd_get_hwpt_paging(ucmd, cmd->hwpt_id);
+ if (IS_ERR(hwpt_target))
+ return PTR_ERR(hwpt_target);
+
+ xa_lock(&ictx->objects);
+ xa_for_each(&ictx->objects, index, obj) {
+ if (obj->type != IOMMUFD_OBJ_HWPT_PAGING)
+ continue;
+
+ hwpt = container_of(obj, struct iommufd_hwpt_paging, common.obj);
+
+ if (hwpt == hwpt_target)
+ continue;
+ if (!hwpt->lu_preserved)
+ continue;
+ if (hwpt->lu_token == cmd->hwpt_token) {
+ rc = -EADDRINUSE;
+ goto out;
+ }
+ }
+
+ hwpt_target->lu_preserved = cmd->preserved;
+ hwpt_target->lu_token = cmd->hwpt_token;
+
+out:
+ xa_unlock(&ictx->objects);
+ iommufd_put_object(ictx, &hwpt_target->common.obj);
+ return rc;
+}
+
static int iommufd_liveupdate_preserve(struct liveupdate_file_op_args *args)
{
struct iommufd_ctx *ictx = iommufd_ctx_from_file(args->file);
@@ -121,6 +162,11 @@ static bool iommufd_liveupdate_can_finish(struct liveupdate_file_op_args *args)
return true;
}
+int iommufd_hwpt_lu_restore(struct iommufd_ucmd *ucmd)
+{
+ return -ENOTTY;
+}
+
static void iommufd_liveupdate_finish(struct liveupdate_file_op_args *args)
{
struct iommufd_lu *iommufd_lu;
diff --git a/drivers/iommu/iommufd/main.c b/drivers/iommu/iommufd/main.c
index 12601f9ad217..b63f61331cae 100644
--- a/drivers/iommu/iommufd/main.c
+++ b/drivers/iommu/iommufd/main.c
@@ -493,6 +493,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_HWPT_LU_SET_PRESERVED, iommufd_hwpt_lu_set_preserved,
+ struct iommu_hwpt_lu_set_preserved, preserved),
+ IOCTL_OP(IOMMU_HWPT_LU_RESTORE, iommufd_hwpt_lu_restore,
+ struct iommu_hwpt_lu_restore, hwpt_token),
#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 2c41920b641d..4b953129f8d8 100644
--- a/include/uapi/linux/iommufd.h
+++ b/include/uapi/linux/iommufd.h
@@ -57,6 +57,8 @@ enum {
IOMMUFD_CMD_IOAS_CHANGE_PROCESS = 0x92,
IOMMUFD_CMD_VEVENTQ_ALLOC = 0x93,
IOMMUFD_CMD_HW_QUEUE_ALLOC = 0x94,
+ IOMMUFD_CMD_HWPT_LU_SET_PRESERVED = 0x95,
+ IOMMUFD_CMD_HWPT_LU_RESTORE = 0x96,
};
/**
@@ -1299,4 +1301,43 @@ struct iommu_hw_queue_alloc {
__aligned_u64 length;
};
#define IOMMU_HW_QUEUE_ALLOC _IO(IOMMUFD_TYPE, IOMMUFD_CMD_HW_QUEUE_ALLOC)
+
+/**
+ * struct iommu_hwpt_lu_set_preserved - ioctl(IOMMU_HWPT_LU_SET_PRESERVED)
+ * @size: sizeof(struct iommu_hwpt_lu_set_preserved)
+ * @hwpt_id: Iommufd object ID of the target HWPT
+ * @hwpt_token: Token to identify this hwpt upon restore
+ * @preserved: If non-zero, HWPT is preserved by liveupdate
+ *
+ * If preserved set to non-zero, the target HWPT will be preserved.
+ *
+ * The hwpt_token is provided by userspace. If userspace enters a token
+ * already in use within this iommufd, -EADDRINUSE is returned from this ioctl.
+ */
+struct iommu_hwpt_lu_set_preserved {
+ __u32 size;
+ __u32 hwpt_id;
+ __u32 hwpt_token;
+ __u8 preserved;
+};
+#define IOMMU_HWPT_LU_SET_PRESERVED _IO(IOMMUFD_TYPE, IOMMUFD_CMD_HWPT_LU_SET_PRESERVED)
+
+/**
+ * struct iommu_hwpt_lu_restore - ioctl(IOMMU_HWPT_LU_RESTORE)
+ * @size: sizeof(struct iommu_hwpt_lu_restore)
+ * @pt_id: Output the ID of the recreated HWPT.
+ * @hwpt_token: Token to identify this hwpt
+ * @hwpt_alloc_flags: Combination of enum iommufd_hwpt_alloc_flags
+
+ * An immutable HWPT is restored without a parent IOAS, and the ID
+ * of this new HWPT is returned.
+ */
+
+struct iommu_hwpt_lu_restore {
+ __u32 size;
+ __u32 pt_id;
+ __u32 hwpt_token;
+ __u32 hwpt_alloc_flags;
+};
+#define IOMMU_HWPT_LU_RESTORE _IO(IOMMUFD_TYPE, IOMMUFD_CMD_HWPT_LU_RESTORE)
#endif
--
2.52.0.158.g65b55ccf14-goog
Powered by blists - more mailing lists