[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <20251202230303.1017519-33-skhawaja@google.com>
Date: Tue, 2 Dec 2025 23:03:02 +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: Samiullah Khawaja <skhawaja@...gle.com>, Robin Murphy <robin.murphy@....com>,
Pratyush Yadav <pratyush@...nel.org>, 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, YiFei Zhu <zhuyifei@...gle.com>,
Chris Li <chrisl@...nel.org>, praan@...gle.com
Subject: [RFC PATCH v2 32/32] iommufd/selftest: Add test to verify iommufd preservation
Test iommufd preservation by setting up an iommufd and vfio cdev and
preserve it across live update. Test takes VFIO cdev path of a device
bound to vfio-pci driver and binds it to an iommufd being preserved. It
also preserves the vfio cdev so the iommufd state associated with it is
also preserved.
The restore path is tested by restoring the preserved vfio cdev, iommufd
and HWPT and hotswapping it with the a new HWPT. Test also tries to
finish the session before restoring iommufd, after restoring iommufd and
after restoring HWPT to verify that it fails as the hotswap has not
happened.
Note that the helper functions setup_cdev, open_iommufd, and
setup_iommufd will be replaced with VFIO selftest library. Similarly the
helper function defined to open and interface with Live Update
Orchestrator device will be replaced with a common helper library.
Signed-off-by: Samiullah Khawaja <skhawaja@...gle.com>
Signed-off-by: YiFei Zhu <zhuyifei@...gle.com>
---
tools/testing/selftests/iommu/Makefile | 1 +
.../selftests/iommu/iommufd_liveupdate.c | 291 ++++++++++++++++++
2 files changed, 292 insertions(+)
create mode 100644 tools/testing/selftests/iommu/iommufd_liveupdate.c
diff --git a/tools/testing/selftests/iommu/Makefile b/tools/testing/selftests/iommu/Makefile
index 84abeb2f0949..42c962c5e612 100644
--- a/tools/testing/selftests/iommu/Makefile
+++ b/tools/testing/selftests/iommu/Makefile
@@ -6,5 +6,6 @@ LDLIBS += -lcap
TEST_GEN_PROGS :=
TEST_GEN_PROGS += iommufd
TEST_GEN_PROGS += iommufd_fail_nth
+TEST_GEN_PROGS += iommufd_liveupdate
include ../lib.mk
diff --git a/tools/testing/selftests/iommu/iommufd_liveupdate.c b/tools/testing/selftests/iommu/iommufd_liveupdate.c
new file mode 100644
index 000000000000..5f767adb10fa
--- /dev/null
+++ b/tools/testing/selftests/iommu/iommufd_liveupdate.c
@@ -0,0 +1,291 @@
+// SPDX-License-Identifier: GPL-2.0-only
+
+/*
+ * Copyright (c) 2025, Google LLC.
+ * Samiullah Khawaja <skhawaja@...gle.com>
+ */
+
+#include <fcntl.h>
+#include <sys/ioctl.h>
+#include <stdbool.h>
+#include <unistd.h>
+
+#define __EXPORTED_HEADERS__
+#include <linux/liveupdate.h>
+#include <linux/iommufd.h>
+#include <linux/types.h>
+#include <linux/vfio.h>
+
+#include "../kselftest.h"
+
+#define ksft_assert(condition) \
+ do { if (!(condition)) \
+ ksft_exit_fail_msg("Failed: %s at %s %d: %s\n", \
+ #condition, __FILE__, __LINE__, strerror(errno)); } while (0)
+
+int setup_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;
+}
+
+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;
+}
+
+int setup_iommufd(int iommufd, int cdev_fd, int hwpt_token)
+{
+ int ret;
+
+ struct vfio_device_bind_iommufd bind = {
+ .argsz = sizeof(bind),
+ .flags = 0,
+ };
+ 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_lu_set_preserved set_preserved = {
+ .size = sizeof(set_preserved),
+ .hwpt_token = hwpt_token,
+ .preserved = 1,
+ };
+
+ bind.iommufd = iommufd;
+ ret = ioctl(cdev_fd, VFIO_DEVICE_BIND_IOMMUFD, &bind);
+ ksft_assert(!ret);
+
+ ret = ioctl(iommufd, IOMMU_IOAS_ALLOC, &alloc_data);
+ ksft_assert(!ret);
+
+ hwpt_alloc.pt_id = bind.out_devid;
+ hwpt_alloc.pt_id = alloc_data.out_ioas_id;
+ ret = ioctl(iommufd, IOMMU_HWPT_ALLOC, &hwpt_alloc);
+ ksft_assert(ret);
+
+ attach_data.pt_id = hwpt_alloc.pt_id;
+ ret = ioctl(cdev_fd, VFIO_DEVICE_ATTACH_IOMMUFD_PT, &attach_data);
+ ksft_assert(!ret);
+
+ set_preserved.hwpt_id = attach_data.pt_id;
+ ret = ioctl(iommufd, IOMMU_HWPT_LU_SET_PRESERVED, &set_preserved);
+ ksft_assert(!ret);
+
+ return ret;
+}
+
+int luo_session_finish(int session_fd)
+{
+ struct liveupdate_session_finish arg = { .size = sizeof(arg) };
+
+ if (ioctl(session_fd, LIVEUPDATE_SESSION_FINISH, &arg) < 0)
+ return -errno;
+
+ return 0;
+}
+
+int restore_iommufd(int session, int iommufd, int cdev_fd, int hwpt_token)
+{
+ int ret;
+
+ struct vfio_device_bind_iommufd bind = {
+ .argsz = sizeof(bind),
+ .flags = 0,
+ };
+ 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 iommu_hwpt_lu_restore restore = {
+ .size = sizeof(restore),
+ .hwpt_token = hwpt_token,
+ .hwpt_alloc_flags = 0,
+ };
+ struct vfio_device_attach_iommufd_pt attach_data = {
+ .argsz = sizeof(attach_data),
+ .flags = 0,
+ };
+
+ bind.iommufd = iommufd;
+ ret = ioctl(cdev_fd, VFIO_DEVICE_BIND_IOMMUFD, &bind);
+ ksft_assert(!ret);
+
+ ret = ioctl(iommufd, IOMMU_IOAS_ALLOC, &alloc_data);
+ ksft_assert(!ret);
+
+ ret = ioctl(iommufd, IOMMU_HWPT_LU_RESTORE, &restore);
+ ksft_assert(!ret);
+
+ /* Should fail */
+ ret = luo_session_finish(session);
+ ksft_assert(ret);
+
+ hwpt_alloc.pt_id = bind.out_devid;
+ hwpt_alloc.pt_id = alloc_data.out_ioas_id;
+ ret = ioctl(iommufd, IOMMU_HWPT_ALLOC, &hwpt_alloc);
+ ksft_assert(ret);
+
+ attach_data.pt_id = hwpt_alloc.pt_id;
+ ret = ioctl(cdev_fd, VFIO_DEVICE_ATTACH_IOMMUFD_PT, &attach_data);
+ ksft_assert(!ret);
+ attach_data.pt_id = alloc_data.out_ioas_id;
+ ret = ioctl(cdev_fd, VFIO_DEVICE_ATTACH_IOMMUFD_PT, &attach_data);
+ ksft_assert(!ret);
+
+ return ret;
+}
+
+int open_liveupdate_orchestrator(void)
+{
+ int luo;
+
+ luo = open("/dev/liveupdate", O_RDWR);
+ ksft_assert(luo > 0);
+
+ return luo;
+}
+
+int luo_create_session(int luo_fd, const char *name)
+{
+ struct liveupdate_ioctl_create_session arg = { .size = sizeof(arg) };
+ int ret;
+
+ snprintf((char *)arg.name, LIVEUPDATE_SESSION_NAME_LENGTH, "%.*s",
+ LIVEUPDATE_SESSION_NAME_LENGTH - 1, name);
+ ret = ioctl(luo_fd, LIVEUPDATE_IOCTL_CREATE_SESSION, &arg);
+ ksft_assert(!ret);
+ ksft_assert(arg.fd > 0);
+
+ return arg.fd;
+}
+
+int luo_retrieve_session(int luo_fd, const char *name)
+{
+ struct liveupdate_ioctl_retrieve_session arg = { .size = sizeof(arg) };
+ int ret;
+
+ snprintf((char *)arg.name, LIVEUPDATE_SESSION_NAME_LENGTH, "%.*s",
+ LIVEUPDATE_SESSION_NAME_LENGTH - 1, name);
+ ret = ioctl(luo_fd, LIVEUPDATE_IOCTL_RETRIEVE_SESSION, &arg);
+ ksft_assert(!ret || errno == ENOENT);
+
+ if (ret && errno == ENOENT)
+ return -errno;
+
+ return arg.fd;
+}
+
+int liveupdate_preserve_fd(int session_fd, int fd, int token)
+{
+ struct liveupdate_session_preserve_fd preserve;
+ int ret;
+
+ preserve.fd = fd;
+ preserve.token = token;
+ preserve.size = sizeof(preserve);
+
+ ret = ioctl(session_fd, LIVEUPDATE_SESSION_PRESERVE_FD, &preserve);
+ ksft_assert(!ret);
+
+ return ret;
+}
+
+int liveupdate_restore_fd(int session_fd, int token)
+{
+ struct liveupdate_session_retrieve_fd arg = { .size = sizeof(arg) };
+ int ret;
+
+ arg.token = token;
+
+ ret = ioctl(session_fd, LIVEUPDATE_SESSION_RETRIEVE_FD, &arg);
+ ksft_assert(!ret);
+ ksft_assert(arg.fd > 0);
+
+ return arg.fd;
+}
+
+int main(int argc, char *argv[])
+{
+ int iommufd, cdev_fd, luo, session, ret;
+ const int token = 0x123456;
+ const int cdev_token = 0x654321;
+ const int hwpt_token = 0x789012;
+ bool updated = false;
+
+ if (argc < 2) {
+ printf("Usage: ./iommufd_liveupdate <vfio_cdev_path>\n");
+ return 1;
+ }
+
+ luo = open_liveupdate_orchestrator();
+ ksft_assert(luo > 0);
+
+ session = luo_retrieve_session(luo, "iommufd-test");
+ if (session == -ENOENT) {
+ session = luo_create_session(luo, "iommufd-test");
+
+ iommufd = open_iommufd();
+ cdev_fd = setup_cdev(argv[1]);
+ } else {
+ updated = true;
+
+ /* Should fail */
+ ret = luo_session_finish(session);
+ ksft_assert(ret);
+
+ iommufd = liveupdate_restore_fd(session, token);
+ cdev_fd = liveupdate_restore_fd(session, cdev_token);
+ }
+
+ if (!updated) {
+ ret = setup_iommufd(iommufd, cdev_fd, hwpt_token);
+ ksft_assert(!ret);
+ } else {
+ /* Should fail */
+ ret = luo_session_finish(session);
+ ksft_assert(ret);
+
+ ret = restore_iommufd(session, iommufd, cdev_fd, hwpt_token);
+ ksft_assert(!ret);
+ }
+
+ if (!updated) {
+ ret = liveupdate_preserve_fd(session, iommufd, token);
+ ksft_assert(!ret);
+
+ ret = liveupdate_preserve_fd(session, cdev_fd, cdev_token);
+ ksft_assert(!ret);
+
+ while (1)
+ sleep(5);
+ } else {
+ ret = luo_session_finish(session);
+ ksft_assert(!ret);
+ }
+
+ return 0;
+}
--
2.52.0.158.g65b55ccf14-goog
Powered by blists - more mailing lists