[PATCH v8 18/18] iommufd/selftest: Add test to verify iommufd preservation

From: Samiullah Khawaja

Date: Sun Nov 22 2026 - 23:51:32 EST


Test iommufd preservation by setting up an iommufd with a HWPT and
preserving it across live update. The test takes the VFIO cdev path of a
device bound to the vfio-pci driver, binds it to the iommufd and attaches
it to the HWPT that is marked for preservation. It also checks that the
iommufd can't be preserved without its memfd or with hole-punched pages.

Add IOMMU_TEST_OP_LIVEUPDATE_PRESERVE_DEV to preserve the device, since
the vfio cdev preservation is not available yet.

On restore, the test verifies that the restored device can't be bound to
a new iommufd, that the iommufd can't be retrieved, as the iommufd
restore is not supported yet, and that the LUO session finish fails.

Signed-off-by: Samiullah Khawaja <skhawaja@xxxxxxxxxx>
Signed-off-by: YiFei Zhu <zhuyifei@xxxxxxxxxx>
---
drivers/iommu/iommufd/device.c | 47 ++-
drivers/iommu/iommufd/iommufd_private.h | 1 +
drivers/iommu/iommufd/iommufd_test.h | 1 +
drivers/iommu/iommufd/selftest.c | 29 ++
tools/testing/selftests/iommu/Makefile | 21 ++
.../iommu/iommufd_liveupdate_kexec_test.c | 276 ++++++++++++++++++
6 files changed, 359 insertions(+), 16 deletions(-)
create mode 100644 tools/testing/selftests/iommu/iommufd_liveupdate_kexec_test.c

diff --git a/drivers/iommu/iommufd/device.c b/drivers/iommu/iommufd/device.c
index ebfc96445f71..95e31d398b84 100644
--- a/drivers/iommu/iommufd/device.c
+++ b/drivers/iommu/iommufd/device.c
@@ -1774,17 +1774,8 @@ static bool _iommufd_device_has_pasid_attachments(struct iommufd_device *idev)
return false;
}

-/**
- * iommufd_device_preserve() - Preserve an iommufd device across live update
- * @s: Live update session
- * @idev: Target iommufd device
- * @iommufd_tokenp: Pointer to store outgoing iommufd token
- *
- * Return: 0 on success, or negative error code.
- */
-int iommufd_device_preserve(struct liveupdate_session *s,
- struct iommufd_device *idev,
- u64 *iommufd_tokenp)
+/* Preserve @idev using @token as its DMA owner token. */
+int __iommufd_device_preserve(struct iommufd_device *idev, u64 token)
{
struct iommufd_hwpt_paging *hwpt_paging;
struct iommufd_hw_pagetable *hwpt;
@@ -1828,10 +1819,6 @@ int iommufd_device_preserve(struct liveupdate_session *s,
}

hwpt_paging = to_hwpt_paging(hwpt);
- ret = liveupdate_get_token_outgoing(s, idev->ictx->file, iommufd_tokenp);
- if (ret)
- goto out;
-
if (!hwpt_paging->liveupdate_preserved) {
ret = -EINVAL;
goto out;
@@ -1839,7 +1826,7 @@ int iommufd_device_preserve(struct liveupdate_session *s,

ret = iommu_preserve_device(hwpt_paging->common.domain,
idev->dev,
- *iommufd_tokenp);
+ token);

if (!ret)
idev->liveupdate_preserved = true;
@@ -1847,6 +1834,34 @@ int iommufd_device_preserve(struct liveupdate_session *s,
mutex_unlock(&igroup->lock);
return ret;
}
+
+/**
+ * iommufd_device_preserve() - Preserve an iommufd device across live update
+ * @s: Live update session
+ * @idev: Target iommufd device
+ * @iommufd_tokenp: Pointer to store outgoing iommufd token
+ *
+ * Return: 0 on success, or negative error code.
+ */
+int iommufd_device_preserve(struct liveupdate_session *s,
+ struct iommufd_device *idev,
+ u64 *iommufd_tokenp)
+{
+ int ret;
+
+ if (!idev)
+ return -EINVAL;
+
+ /*
+ * The iommufd must be preserved in the same session, which also makes
+ * sure its HWPT preservation is done before the device is preserved.
+ */
+ ret = liveupdate_get_token_outgoing(s, idev->ictx->file, iommufd_tokenp);
+ if (ret)
+ return ret;
+
+ return __iommufd_device_preserve(idev, *iommufd_tokenp);
+}
EXPORT_SYMBOL_NS_GPL(iommufd_device_preserve, "IOMMUFD");

/**
diff --git a/drivers/iommu/iommufd/iommufd_private.h b/drivers/iommu/iommufd/iommufd_private.h
index df231d1e7d09..b342ee5b8ed3 100644
--- a/drivers/iommu/iommufd/iommufd_private.h
+++ b/drivers/iommu/iommufd/iommufd_private.h
@@ -749,6 +749,7 @@ int iommufd_liveupdate_register(void);
void iommufd_liveupdate_unregister(void);

int iommufd_hwpt_liveupdate_mark_preserve(struct iommufd_ucmd *ucmd);
+int __iommufd_device_preserve(struct iommufd_device *idev, u64 token);
#else
static inline int iommufd_liveupdate_register(void)
{
diff --git a/drivers/iommu/iommufd/iommufd_test.h b/drivers/iommu/iommufd/iommufd_test.h
index 52b78cbcc920..2f68abe52bfe 100644
--- a/drivers/iommu/iommufd/iommufd_test.h
+++ b/drivers/iommu/iommufd/iommufd_test.h
@@ -31,6 +31,7 @@ enum {
IOMMU_TEST_OP_PASID_CHECK_HWPT,
IOMMU_TEST_OP_DMABUF_GET,
IOMMU_TEST_OP_DMABUF_REVOKE,
+ IOMMU_TEST_OP_LIVEUPDATE_PRESERVE_DEV,
};

enum {
diff --git a/drivers/iommu/iommufd/selftest.c b/drivers/iommu/iommufd/selftest.c
index 037e54f3d2ba..d904482124e1 100644
--- a/drivers/iommu/iommufd/selftest.c
+++ b/drivers/iommu/iommufd/selftest.c
@@ -2115,6 +2115,33 @@ static int iommufd_test_dmabuf_revoke(struct iommufd_ucmd *ucmd, int fd,
return rc;
}

+/*
+ * Preserve the IOMMU state of a device bound to this iommufd, as a vfio cdev
+ * preserved in the same live update session would. The iommufd must already
+ * be preserved, so the HWPT of the device is preserved.
+ */
+static int iommufd_test_liveupdate_preserve_dev(struct iommufd_ucmd *ucmd,
+ u32 dev_id)
+{
+#ifdef CONFIG_IOMMU_LIVEUPDATE
+ struct iommufd_device *idev;
+ int rc;
+
+ idev = iommufd_get_device(ucmd, dev_id);
+ if (IS_ERR(idev))
+ return PTR_ERR(idev);
+
+ mutex_lock(&ucmd->ictx->liveupdate_mutex);
+ rc = __iommufd_device_preserve(idev, 0);
+ mutex_unlock(&ucmd->ictx->liveupdate_mutex);
+
+ iommufd_put_object(ucmd->ictx, &idev->obj);
+ return rc;
+#else
+ return -EOPNOTSUPP;
+#endif
+}
+
int iommufd_test(struct iommufd_ucmd *ucmd)
{
struct iommu_test_cmd *cmd = ucmd->cmd;
@@ -2200,6 +2227,8 @@ int iommufd_test(struct iommufd_ucmd *ucmd)
return iommufd_test_dmabuf_revoke(ucmd,
cmd->dmabuf_revoke.dmabuf_fd,
cmd->dmabuf_revoke.revoked);
+ case IOMMU_TEST_OP_LIVEUPDATE_PRESERVE_DEV:
+ return iommufd_test_liveupdate_preserve_dev(ucmd, cmd->id);
default:
return -EOPNOTSUPP;
}
diff --git a/tools/testing/selftests/iommu/Makefile b/tools/testing/selftests/iommu/Makefile
index 84abeb2f0949..89be2ff830c6 100644
--- a/tools/testing/selftests/iommu/Makefile
+++ b/tools/testing/selftests/iommu/Makefile
@@ -7,4 +7,25 @@ TEST_GEN_PROGS :=
TEST_GEN_PROGS += iommufd
TEST_GEN_PROGS += iommufd_fail_nth

+TEST_GEN_PROGS_EXTENDED += iommufd_liveupdate_kexec_test
+
include ../lib.mk
+include ../liveupdate/lib/libliveupdate.mk
+
+CFLAGS += -I$(top_srcdir)/tools/include
+CFLAGS += -MD
+CFLAGS += $(EXTRA_CFLAGS)
+
+TEST_O := $(patsubst %, %.o, $(TEST_GEN_PROGS_EXTENDED))
+$(TEST_O): $(OUTPUT)/%.o: %.c
+ $(CC) $(CFLAGS) $(CPPFLAGS) $(TARGET_ARCH) -c $< -o $@
+
+TEST_DEP_FILES := $(patsubst %.o, %.d, $(LIBLIVEUPDATE_O) $(TEST_O))
+-include $(TEST_DEP_FILES)
+
+$(TEST_GEN_PROGS_EXTENDED): $(OUTPUT)/%: $(OUTPUT)/%.o $(LIBLIVEUPDATE_O)
+ $(CC) $(CFLAGS) $(CPPFLAGS) $(LDFLAGS) $(TARGET_ARCH) $< $(LIBLIVEUPDATE_O) $(LDLIBS) -o $@
+
+EXTRA_CLEAN += $(TEST_O)
+EXTRA_CLEAN += $(TEST_DEP_FILES)
+EXTRA_CLEAN += $(patsubst %, %.d, $(TEST_GEN_PROGS))
diff --git a/tools/testing/selftests/iommu/iommufd_liveupdate_kexec_test.c b/tools/testing/selftests/iommu/iommufd_liveupdate_kexec_test.c
new file mode 100644
index 000000000000..3202272ec27c
--- /dev/null
+++ b/tools/testing/selftests/iommu/iommufd_liveupdate_kexec_test.c
@@ -0,0 +1,276 @@
+// SPDX-License-Identifier: GPL-2.0-only
+
+/*
+ * Copyright (c) 2026, Google LLC.
+ * Samiullah Khawaja <skhawaja@xxxxxxxxxx>
+ */
+
+#define _GNU_SOURCE
+#include <errno.h>
+#include <fcntl.h>
+#include <sys/ioctl.h>
+#include <sys/mman.h>
+#include <stdbool.h>
+#include <unistd.h>
+#include <limits.h>
+
+#define __EXPORTED_HEADERS__
+#include <linux/iommufd.h>
+#include <linux/types.h>
+#include <linux/vfio.h>
+#include <linux/sizes.h>
+#include <libliveupdate.h>
+
+#include "../kselftest.h"
+#include "../../../../drivers/iommu/iommufd/iommufd_test.h"
+
+#define ksft_assert(condition) \
+ do { \
+ if (!(condition)) \
+ fail_exit("Failed: %s", #condition); \
+ } while (0)
+
+static const char *device_cdev_path;
+static const char state_session[] = "state-iommufd-cdev";
+static const char iommufd_session[] = "iommufd-test-cdev";
+
+static const uint64_t STATE_TOKEN;
+static const uint64_t IOMMUFD_TOKEN = 0x123456;
+static const uint64_t HWPT_TOKEN = 0x789012;
+static const uint64_t MEMFD_TOKEN = 0x890123;
+
+static int open_cdev(const char *vfio_cdev_path)
+{
+ int cdev_fd;
+
+ cdev_fd = open(vfio_cdev_path, O_RDWR);
+ if (cdev_fd < 0)
+ ksft_exit_skip("Failed to open VFIO cdev: %s\n", vfio_cdev_path);
+
+ return cdev_fd;
+}
+
+static int open_iommufd(void)
+{
+ int iommufd;
+
+ iommufd = open("/dev/iommu", O_RDWR);
+ if (iommufd < 0)
+ ksft_exit_skip("Failed to open /dev/iommu. IOMMUFD support not enabled.\n");
+
+ return iommufd;
+}
+
+static int create_memfd(size_t size)
+{
+ int fd, ret;
+
+ fd = memfd_create("buffer", 0);
+ if (fd < 0)
+ fail_exit("memfd_create failed");
+
+ ret = ftruncate(fd, size);
+ if (ret)
+ fail_exit("ftruncate failed");
+
+ return fd;
+}
+
+#define test_ioctl(fd, cmd, arg) \
+ do { \
+ if (ioctl(fd, cmd, arg)) \
+ fail_exit("ioctl(%s) failed", #cmd); \
+ } while (0)
+
+#define test_luo_session_preserve_fd(session, fd, token) \
+ do { \
+ if (luo_session_preserve_fd(session, fd, token)) \
+ fail_exit("luo_session_preserve_fd(%s) failed", #token); \
+ } while (0)
+
+static uint32_t setup_iommufd(int iommufd, int memfd, int cdev_fd,
+ uint32_t *dev_id)
+{
+ struct vfio_device_bind_iommufd bind = {
+ .argsz = sizeof(bind),
+ .flags = 0,
+ .iommufd = iommufd,
+ };
+ struct iommu_ioas_alloc alloc_data = {
+ .size = sizeof(alloc_data),
+ .flags = 0,
+ };
+ struct iommu_hwpt_alloc hwpt_alloc = {
+ .size = sizeof(hwpt_alloc),
+ .flags = 0,
+ };
+ struct vfio_device_attach_iommufd_pt attach_data = {
+ .argsz = sizeof(attach_data),
+ .flags = 0,
+ };
+ struct iommu_hwpt_liveupdate_mark_preserve mark_preserve = {
+ .size = sizeof(mark_preserve),
+ .hwpt_token = HWPT_TOKEN,
+ };
+ struct iommu_ioas_map_file map_file = {
+ .size = sizeof(map_file),
+ .length = SZ_1M,
+ .flags = IOMMU_IOAS_MAP_WRITEABLE |
+ IOMMU_IOAS_MAP_READABLE |
+ IOMMU_IOAS_MAP_FIXED_IOVA,
+ .iova = SZ_4G,
+ .fd = memfd,
+ .start = 0,
+ };
+
+ test_ioctl(cdev_fd, VFIO_DEVICE_BIND_IOMMUFD, &bind);
+
+ test_ioctl(iommufd, IOMMU_IOAS_ALLOC, &alloc_data);
+
+ hwpt_alloc.dev_id = bind.out_devid;
+ hwpt_alloc.pt_id = alloc_data.out_ioas_id;
+ test_ioctl(iommufd, IOMMU_HWPT_ALLOC, &hwpt_alloc);
+
+ attach_data.pt_id = hwpt_alloc.out_hwpt_id;
+ test_ioctl(cdev_fd, VFIO_DEVICE_ATTACH_IOMMUFD_PT, &attach_data);
+
+ map_file.ioas_id = alloc_data.out_ioas_id;
+ test_ioctl(iommufd, IOMMU_IOAS_MAP_FILE, &map_file);
+
+ map_file.iova = SZ_4G + SZ_1M;
+ map_file.start = SZ_1M;
+ test_ioctl(iommufd, IOMMU_IOAS_MAP_FILE, &map_file);
+
+ mark_preserve.hwpt_id = attach_data.pt_id;
+ test_ioctl(iommufd, IOMMU_HWPT_LIVEUPDATE_MARK_PRESERVE, &mark_preserve);
+
+ *dev_id = bind.out_devid;
+ return alloc_data.out_ioas_id;
+}
+
+/* Preserve the IOMMU state of the device, using the iommufd selftest support. */
+static int preserve_device(int iommufd, uint32_t dev_id)
+{
+ struct iommu_test_cmd cmd = {
+ .size = sizeof(cmd),
+ .op = IOMMU_TEST_OP_LIVEUPDATE_PRESERVE_DEV,
+ .id = dev_id,
+ };
+
+ return ioctl(iommufd, IOMMU_TEST_CMD, &cmd);
+}
+
+static void before_kexec(int luo_fd)
+{
+ struct iommu_ioas_unmap unmap = {
+ .size = sizeof(unmap),
+ .iova = SZ_4G + SZ_1M,
+ .length = SZ_1M,
+ };
+ int iommufd, cdev_fd, memfd, session;
+ uint32_t ioas_id, dev_id;
+
+ create_state_file(luo_fd, state_session, STATE_TOKEN, /*next_stage=*/2);
+
+ session = luo_create_session(luo_fd, iommufd_session);
+ if (session < 0)
+ fail_exit("luo_create_session failed");
+
+ iommufd = open_iommufd();
+ memfd = create_memfd(SZ_2M);
+ cdev_fd = open_cdev(device_cdev_path);
+
+ ioas_id = setup_iommufd(iommufd, memfd, cdev_fd, &dev_id);
+
+ /* Cannot preserve the device before its HWPT is preserved. */
+ if (!preserve_device(iommufd, dev_id))
+ fail_exit("Preserving device before iommufd should fail");
+ if (errno == ENOTTY)
+ ksft_exit_skip("IOMMUFD selftest support not enabled\n");
+
+ /* Cannot preserve iommufd without preserving memfd. */
+ if (!luo_session_preserve_fd(session, iommufd, IOMMUFD_TOKEN))
+ fail_exit("Preserving iommufd without memfd should fail");
+
+ if (fallocate(memfd, FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE,
+ SZ_1M, SZ_1M))
+ fail_exit("fallocate punch hole failed");
+
+ test_luo_session_preserve_fd(session, memfd, MEMFD_TOKEN);
+
+ /* Cannot preserve iommufd when mapped memfd pages have a hole punched. */
+ if (!luo_session_preserve_fd(session, iommufd, IOMMUFD_TOKEN))
+ fail_exit("Preserving iommufd with hole-punched pages should fail");
+
+ unmap.ioas_id = ioas_id;
+ test_ioctl(iommufd, IOMMU_IOAS_UNMAP, &unmap);
+
+ test_luo_session_preserve_fd(session, iommufd, IOMMUFD_TOKEN);
+
+ if (preserve_device(iommufd, dev_id))
+ fail_exit("Preserving device failed");
+
+ if (!preserve_device(iommufd, dev_id) || errno != EBUSY)
+ fail_exit("Preserving device twice should fail with EBUSY");
+
+ /* Closing the session unpreserves the device and the iommufd. */
+ close(session);
+ session = luo_create_session(luo_fd, iommufd_session);
+ if (session < 0)
+ fail_exit("luo_create_session failed");
+
+ test_luo_session_preserve_fd(session, memfd, MEMFD_TOKEN);
+ test_luo_session_preserve_fd(session, iommufd, IOMMUFD_TOKEN);
+
+ if (preserve_device(iommufd, dev_id))
+ fail_exit("Preserving device failed");
+
+ close(luo_fd);
+ daemonize_and_wait();
+}
+
+static void after_kexec(int luo_fd, int state_session_fd)
+{
+ struct vfio_device_bind_iommufd bind = {
+ .argsz = sizeof(bind),
+ .flags = 0,
+ };
+ int iommufd, cdev_fd, session, stage;
+
+ restore_and_read_stage(state_session_fd, STATE_TOKEN, &stage);
+ ksft_assert(stage == 2);
+
+ /* The restored device is owned until its iommufd is restored. */
+ cdev_fd = open_cdev(device_cdev_path);
+ bind.iommufd = open_iommufd();
+ if (!ioctl(cdev_fd, VFIO_DEVICE_BIND_IOMMUFD, &bind) || errno != EPERM)
+ fail_exit("Binding the restored device should fail with EPERM");
+
+ session = luo_retrieve_session(luo_fd, iommufd_session);
+ if (session < 0)
+ fail_exit("luo_retrieve_session failed");
+
+ iommufd = luo_session_retrieve_fd(session, IOMMUFD_TOKEN);
+ if (iommufd >= 0)
+ fail_exit("iommufd should not be retrievable yet");
+
+ /* Should fail */
+ if (luo_session_finish(session) == 0)
+ fail_exit("luo_session_finish should fail if iommufd is not restored");
+}
+
+int main(int argc, char *argv[])
+{
+ /* Skip the stage option, luo_test() parses it again. */
+ while (getopt(argc, argv, "s:") != -1)
+ ;
+
+ if (optind >= argc) {
+ printf("Usage: %s [-s <stage>] <vfio_cdev_path>\n", argv[0]);
+ return 1;
+ }
+
+ device_cdev_path = argv[optind];
+
+ return luo_test(argc, argv, state_session, before_kexec, after_kexec);
+}
--
2.56.1.809.ga0b61035b5-goog