Skip to contentSkip to conversation

iommufd live update

PATCH Applied Steve Sistare 2 participants 8 messages Nov 13, 2024 – Nov 14, 2024

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

v5 cover iommufd live update

Live update is a technique wherein an application saves its state, launches an updated version of itself, and restores its state. Clients of the application experience a brief suspension of service, on the order of 100's of milliseconds, but are otherwise unaffected.

Define the IOMMU_IOAS_CHANGE_PROCESS ioctl to allow management and use of an iommufd device to be transferred from one process to another. The application is responsible for transferring the device descriptor to the new process, eg either by preservation across fork and exec or via SCM_RIGHTS. It atomically updates everything to the current process, grabbing the new mm, and transferring pinned page counts.

IOMMU_IOAS_CHANGE_PROCESS only supports DMA mappings created with IOMMU_IOAS_MAP_FILE, because the kernel metadata for such mappings does not depend on the userland VA of the pages (which is different in the new process). IOMMU_IOAS_CHANGE_PROCESS fails if other types of mappings are present.

Thanks to Jason Gunthorpe for code and ideas in this series.

This series depends on the series:
[PATCH V3] iommu_ioas_map_file

This is implemented in QEMU by the series:
[PATCH] Live update: iommufd (V2 to be posted)

Changes in V2:
* deleted interface to update VA
* add selftest cases

Changes in V3:
* replaced lockdep_off with down_write_nest_lock
* cosmetic changes as requested
* check for zero length in iommufd_ioas_map_file

Changes in V4:
* added TEST_LENGTH for iommu_ioas_change_process
* fixed context vs patch "Selftest coverage for IOMMU_IOAS_MAP_FILE"
* changed IOMMUFD_CMD_IOAS_CHANGE_PROCESS to 0x92
* added RB's

Changes in V5:
* renamed iopt_update_pinned -> iopt_pages_update_pinned
* reworded lock all *IOAS* objects
* added WARN for iopt_pages_update_pinned failure
* added RB's

Steve Sistare (4):
iommufd: Export do_update_pinned
iommufd: Lock all IOAS objects
iommufd: Add IOMMU_IOAS_CHANGE_PROCESS
iommufd: IOMMU_IOAS_CHANGE_PROCESS selftest

 drivers/iommu/iommufd/io_pagetable.h          |   6 +
 drivers/iommu/iommufd/ioas.c                  | 212 ++++++++++++++++++++++++++
 drivers/iommu/iommufd/iommufd_private.h       |   2 +
 drivers/iommu/iommufd/main.c                  |   3 +
 drivers/iommu/iommufd/pages.c                 |  10 +-
 include/uapi/linux/iommufd.h                  |  23 +++
 tools/testing/selftests/iommu/Makefile        |   1 +
 tools/testing/selftests/iommu/iommufd.c       | 141 +++++++++++++++++
 tools/testing/selftests/iommu/iommufd_utils.h |   6 +
 9 files changed, 399 insertions(+), 5 deletions(-)
Signature · 1 line
1.8.3.1
#1 lore raw

PATCH v5 1/4 iommufd: Export do_update_pinned

Export do_update_pinned. No functional change.

---
 drivers/iommu/iommufd/io_pagetable.h |  5 +++++
 drivers/iommu/iommufd/pages.c        | 10 +++++-----
 2 files changed, 10 insertions(+), 5 deletions(-)
drivers/iommu/iommufd/io_pagetable.h +5 -0
index 9b40b22..f5f20fa 100644
@@ -252,4 +252,9 @@ struct iopt_pages_access {
 	unsigned int users;
 };
 
+struct pfn_reader_user;
+
+int iopt_pages_update_pinned(struct iopt_pages *pages, unsigned long npages,
+			     bool inc, struct pfn_reader_user *user);
+
 #endif
drivers/iommu/iommufd/pages.c +5 -5
index 8f24916..3427749 100644
@@ -985,8 +985,8 @@ static int update_mm_locked_vm(struct iopt_pages *pages, unsigned long npages,
 	return rc;
 }
 
-static int do_update_pinned(struct iopt_pages *pages, unsigned long npages,
-			    bool inc, struct pfn_reader_user *user)
+int iopt_pages_update_pinned(struct iopt_pages *pages, unsigned long npages,
+			     bool inc, struct pfn_reader_user *user)
 {
 	int rc = 0;
 
@@ -1020,8 +1020,8 @@ static void update_unpinned(struct iopt_pages *pages)
 		return;
 	if (pages->npinned == pages->last_npinned)
 		return;
-	do_update_pinned(pages, pages->last_npinned - pages->npinned, false,
-			 NULL);
+	iopt_pages_update_pinned(pages, pages->last_npinned - pages->npinned,
+				 false, NULL);
 }
 
 /*
@@ -1051,7 +1051,7 @@ static int pfn_reader_user_update_pinned(struct pfn_reader_user *user,
 		npages = pages->npinned - pages->last_npinned;
 		inc = true;
 	}
-	return do_update_pinned(pages, npages, inc, user);
+	return iopt_pages_update_pinned(pages, npages, inc, user);
 }
 
 /*
Signature · 1 line
1.8.3.1
#2 lore raw

PATCH v5 2/4 iommufd: Lock all IOAS objects

In reply to Steve Sistare

Define helpers to lock and unlock all IOAS objects.
This will allow DMA mappings to be updated atomically during live update. This code is substantially the same as an initial version provided by Jason, plus fixes.

---
 drivers/iommu/iommufd/ioas.c            | 65 +++++++++++++++++++++++++++++++++
 drivers/iommu/iommufd/iommufd_private.h |  1 +
 drivers/iommu/iommufd/main.c            |  1 +
 3 files changed, 67 insertions(+)
drivers/iommu/iommufd/ioas.c +65 -0
index c05d33f..c82ed5a 100644
@@ -52,7 +52,10 @@ int iommufd_ioas_alloc_ioctl(struct iommufd_ucmd *ucmd)
 	rc = iommufd_ucmd_respond(ucmd, sizeof(*cmd));
 	if (rc)
 		goto out_table;
+
+	down_read(&ucmd->ictx->ioas_creation_lock);
 	iommufd_object_finalize(ucmd->ictx, &ioas->obj);
+	up_read(&ucmd->ictx->ioas_creation_lock);
 	return 0;
 
 out_table:
@@ -374,6 +377,68 @@ int iommufd_ioas_unmap(struct iommufd_ucmd *ucmd)
 	return rc;
 }
 
+static void iommufd_release_all_iova_rwsem(struct iommufd_ctx *ictx,
+					   struct xarray *ioas_list)
+{
+	struct iommufd_ioas *ioas;
+	unsigned long index;
+
+	xa_for_each(ioas_list, index, ioas) {
+		up_write(&ioas->iopt.iova_rwsem);
+		refcount_dec(&ioas->obj.users);
+	}
+	up_write(&ictx->ioas_creation_lock);
+	xa_destroy(ioas_list);
+}
+
+static int iommufd_take_all_iova_rwsem(struct iommufd_ctx *ictx,
+				       struct xarray *ioas_list)
+{
+	struct iommufd_object *obj;
+	unsigned long index;
+	int rc;
+
+	/*
+	 * This is very ugly, it is done instead of adding a lock around
+	 * pages->source_mm, which is a performance path for mdev, we just
+	 * obtain the write side of all the iova_rwsems which also protects the
+	 * pages->source_*. Due to copies we can't know which IOAS could read
+	 * from the pages, so we just lock everything. This is the only place
+	 * locks are nested and they are uniformly taken in ID order.
+	 *
+	 * ioas_creation_lock prevents new IOAS from being installed in the
+	 * xarray while we do this, and also prevents more than one thread from
+	 * holding nested locks.
+	 */
+	down_write(&ictx->ioas_creation_lock);
+	xa_lock(&ictx->objects);
+	xa_for_each(&ictx->objects, index, obj) {
+		struct iommufd_ioas *ioas;
+
+		if (!obj || obj->type != IOMMUFD_OBJ_IOAS)
+			continue;
+
+		if (!refcount_inc_not_zero(&obj->users))
+			continue;
+
+		xa_unlock(&ictx->objects);
+
+		ioas = container_of(obj, struct iommufd_ioas, obj);
+		down_write_nest_lock(&ioas->iopt.iova_rwsem,
+				     &ictx->ioas_creation_lock);
+
+		rc = xa_err(xa_store(ioas_list, index, ioas, GFP_KERNEL));
+		if (rc) {
+			iommufd_release_all_iova_rwsem(ictx, ioas_list);
+			return rc;
+		}
+
+		xa_lock(&ictx->objects);
+	}
+	xa_unlock(&ictx->objects);
+	return 0;
+}
+
 int iommufd_option_rlimit_mode(struct iommu_option *cmd,
 			       struct iommufd_ctx *ictx)
 {
drivers/iommu/iommufd/iommufd_private.h +1 -0
index 8f3c21a..a2b5a35 100644
@@ -24,6 +24,7 @@ struct iommufd_ctx {
 	struct xarray objects;
 	struct xarray groups;
 	wait_queue_head_t destroy_wait;
+	struct rw_semaphore ioas_creation_lock;
 
 	u8 account_mode;
 	/* Compatibility with VFIO no iommu */
drivers/iommu/iommufd/main.c +1 -0
index 826a2b2..577f251e 100644
@@ -248,6 +248,7 @@ static int iommufd_fops_open(struct inode *inode, struct file *filp)
 		pr_info_once("IOMMUFD is providing /dev/vfio/vfio, not VFIO.\n");
 	}
 
+	init_rwsem(&ictx->ioas_creation_lock);
 	xa_init_flags(&ictx->objects, XA_FLAGS_ALLOC1 | XA_FLAGS_ACCOUNT);
 	xa_init(&ictx->groups);
 	ictx->file = filp;
Signature · 1 line
1.8.3.1
#3 lore raw

PATCH v5 3/4 iommufd: Add IOMMU_IOAS_CHANGE_PROCESS

In reply to Steve Sistare

Add an ioctl that updates all DMA mappings to reflect the current process, Change the mm and transfer locked memory accounting from old to current mm. This will be used for live update, allowing an old process to hand the iommufd device descriptor to a new process. The new process calls the ioctl.

IOMMU_IOAS_CHANGE_PROCESS only supports DMA mappings created with IOMMU_IOAS_MAP_FILE, because the kernel metadata for such mappings does not depend on the userland VA of the pages (which is different in the new process).
IOMMU_IOAS_CHANGE_PROCESS fails if other types of mappings are present.

This is a revised version of code originally provided by Jason.

---
 drivers/iommu/iommufd/io_pagetable.h    |   1 +
 drivers/iommu/iommufd/ioas.c            | 147 ++++++++++++++++++++++++++++++++
 drivers/iommu/iommufd/iommufd_private.h |   1 +
 drivers/iommu/iommufd/main.c            |   2 +
 include/uapi/linux/iommufd.h            |  23 +++++
 5 files changed, 174 insertions(+)
drivers/iommu/iommufd/io_pagetable.h +1 -0
index f5f20fa..10c928a 100644
@@ -173,6 +173,7 @@ enum {
 	IOPT_PAGES_ACCOUNT_NONE = 0,
 	IOPT_PAGES_ACCOUNT_USER = 1,
 	IOPT_PAGES_ACCOUNT_MM = 2,
+	IOPT_PAGES_ACCOUNT_MODE_NUM = 3,
 };
 
 enum iopt_address_type {
drivers/iommu/iommufd/ioas.c +147 -0

153 lines not on this page · raw message

drivers/iommu/iommufd/iommufd_private.h +1 -0
index a2b5a35..bce20ef 100644
@@ -282,6 +282,7 @@ static inline struct iommufd_ioas *iommufd_get_ioas(struct iommufd_ctx *ictx,
 int iommufd_ioas_allow_iovas(struct iommufd_ucmd *ucmd);
 int iommufd_ioas_map(struct iommufd_ucmd *ucmd);
 int iommufd_ioas_map_file(struct iommufd_ucmd *ucmd);
+int iommufd_ioas_change_process(struct iommufd_ucmd *ucmd);
 int iommufd_ioas_copy(struct iommufd_ucmd *ucmd);
 int iommufd_ioas_unmap(struct iommufd_ucmd *ucmd);
 int iommufd_ioas_option(struct iommufd_ucmd *ucmd);
drivers/iommu/iommufd/main.c +2 -0
index 577f251e..c3cb6c3 100644
@@ -373,6 +373,8 @@ struct iommufd_ioctl_op {
 		 struct iommu_ioas_alloc, out_ioas_id),
 	IOCTL_OP(IOMMU_IOAS_ALLOW_IOVAS, iommufd_ioas_allow_iovas,
 		 struct iommu_ioas_allow_iovas, allowed_iovas),
+	IOCTL_OP(IOMMU_IOAS_CHANGE_PROCESS, iommufd_ioas_change_process,
+		 struct iommu_ioas_change_process, __reserved),
 	IOCTL_OP(IOMMU_IOAS_COPY, iommufd_ioas_copy, struct iommu_ioas_copy,
 		 src_iova),
 	IOCTL_OP(IOMMU_IOAS_IOVA_RANGES, iommufd_ioas_iova_ranges,
include/uapi/linux/iommufd.h +23 -0
index 41b1a01..19b646c 100644
@@ -52,6 +52,7 @@ enum {
 	IOMMUFD_CMD_HWPT_INVALIDATE = 0x8d,
 	IOMMUFD_CMD_FAULT_QUEUE_ALLOC = 0x8e,
 	IOMMUFD_CMD_IOAS_MAP_FILE = 0x8f,
+	IOMMUFD_CMD_IOAS_CHANGE_PROCESS = 0x92,
 };
 
 /**
@@ -822,4 +823,26 @@ struct iommu_fault_alloc {
 	__u32 out_fault_fd;
 };
 #define IOMMU_FAULT_QUEUE_ALLOC _IO(IOMMUFD_TYPE, IOMMUFD_CMD_FAULT_QUEUE_ALLOC)
+
+/**
+ * struct iommu_ioas_change_process - ioctl(VFIO_IOAS_CHANGE_PROCESS)
+ * @size: sizeof(struct iommu_ioas_change_process)
+ * @__reserved: Must be 0
+ *
+ * This transfers pinned memory counts for every memory map in every IOAS
+ * in the context to the current process.  This only supports maps created
+ * with IOMMU_IOAS_MAP_FILE, and returns EINVAL if other maps are present.
+ * If the ioctl returns a failure status, then nothing is changed.
+ *
+ * This API is useful for transferring operation of a device from one process
+ * to another, such as during userland live update.
+ */
+struct iommu_ioas_change_process {
+	__u32 size;
+	__u32 __reserved;
+};
+
+#define IOMMU_IOAS_CHANGE_PROCESS \
+	_IO(IOMMUFD_TYPE, IOMMUFD_CMD_IOAS_CHANGE_PROCESS)
+
 #endif
Signature · 1 line
1.8.3.1
#4 lore raw

PATCH v5 4/4 iommufd: IOMMU_IOAS_CHANGE_PROCESS selftest

In reply to Steve Sistare

Add selftest cases for IOMMU_IOAS_CHANGE_PROCESS.

---
 tools/testing/selftests/iommu/Makefile        |   1 +
 tools/testing/selftests/iommu/iommufd.c       | 141 ++++++++++++++++++++++++++
 tools/testing/selftests/iommu/iommufd_utils.h |   6 ++
 3 files changed, 148 insertions(+)
tools/testing/selftests/iommu/Makefile +1 -0
index fd64779..bd23ac9 100644
@@ -1,6 +1,7 @@
 # SPDX-License-Identifier: GPL-2.0-only
 CFLAGS += -Wall -O2 -Wno-unused-function
 CFLAGS += $(KHDR_INCLUDES)
+CFLAGS += -lcap
 
 TEST_GEN_PROGS :=
 TEST_GEN_PROGS += iommufd
tools/testing/selftests/iommu/iommufd.c +141 -0

159 lines not on this page · raw message

tools/testing/selftests/iommu/iommufd_utils.h +6 -0
index 2506914..cd8dd39 100644
@@ -23,6 +23,12 @@
 #define BIT_MASK(nr) (1UL << ((nr) % __BITS_PER_LONG))
 #define BIT_WORD(nr) ((nr) / __BITS_PER_LONG)
 
+enum {
+	IOPT_PAGES_ACCOUNT_NONE = 0,
+	IOPT_PAGES_ACCOUNT_USER = 1,
+	IOPT_PAGES_ACCOUNT_MM = 2,
+};
+
 #define DIV_ROUND_UP(n, d) (((n) + (d) - 1) / (d))
 
 static inline void set_bit(unsigned int nr, unsigned long *addr)
Signature · 1 line
1.8.3.1
maintainer
#5 lore raw

Re: [PATCH V5 4/4] iommufd: IOMMU_IOAS_CHANGE_PROCESS selftest

Quoted 15 lines · Steve Sistare

Add selftest cases for IOMMU_IOAS_CHANGE_PROCESS.

---
 tools/testing/selftests/iommu/Makefile        |   1 +
 tools/testing/selftests/iommu/iommufd.c       | 141 ++++++++++++++++++++++++++
 tools/testing/selftests/iommu/iommufd_utils.h |   6 ++
 3 files changed, 148 insertions(+)
tools/testing/selftests/iommu/Makefile +0 -0
index fd64779..bd23ac9 100644

tools/testing/selftests/iommu/Makefile line 4

Quoted 4 lines
tools/testing/selftests/iommu/Makefile +1 -0
@@ -1,6 +1,7 @@
 # SPDX-License-Identifier: GPL-2.0-only
 CFLAGS += -Wall -O2 -Wno-unused-function
 CFLAGS += $(KHDR_INCLUDES)
+CFLAGS += -lcap

This should be

LDLIBS += -lcap

Otherwise it doesn't compile:

iommufd.c:(.text+0xe54a): undefined reference to cap_get_proc' /usr/bin/ld: iommufd.c:(.text+0xe57a): undefined reference to cap_set_flag' /usr/bin/ld: iommufd.c:(.text+0xe596): undefined reference to cap_set_proc' /usr/bin/ld: iommufd.c:(.text+0xe5b5): undefined reference to cap_free'

Because the -l option is in the wrong order

Jason

#6 lore raw

Re: [PATCH V5 4/4] iommufd: IOMMU_IOAS_CHANGE_PROCESS selftest

Quoted 25 lines · Jason Gunthorpe
Quoted 15 lines · Steve Sistare

Add selftest cases for IOMMU_IOAS_CHANGE_PROCESS.

---
  tools/testing/selftests/iommu/Makefile        |   1 +
  tools/testing/selftests/iommu/iommufd.c       | 141 ++++++++++++++++++++++++++
  tools/testing/selftests/iommu/iommufd_utils.h |   6 ++
  3 files changed, 148 insertions(+)
tools/testing/selftests/iommu/Makefile +0 -0
index fd64779..bd23ac9 100644

tools/testing/selftests/iommu/Makefile line 4

Quoted 4 lines
tools/testing/selftests/iommu/Makefile +1 -0
@@ -1,6 +1,7 @@
  # SPDX-License-Identifier: GPL-2.0-only
  CFLAGS += -Wall -O2 -Wno-unused-function
  CFLAGS += $(KHDR_INCLUDES)
+CFLAGS += -lcap

This should be

LDLIBS += -lcap

Quoted 8 lines

Otherwise it doesn't compile:

iommufd.c:(.text+0xe54a): undefined reference to cap_get_proc' /usr/bin/ld: iommufd.c:(.text+0xe57a): undefined reference to cap_set_flag' /usr/bin/ld: iommufd.c:(.text+0xe596): undefined reference to cap_set_proc' /usr/bin/ld: iommufd.c:(.text+0xe5b5): undefined reference to cap_free'

Because the -l option is in the wrong order

Yes, should be LDLIBS, thanks for fixing it. For whatever reason it has been building fine in my workspace using CFLAGS, so I did not catch it.

- Steve

maintainer Applied
#7 lore raw
In reply to Steve Sistare
Quoted 5 lines · Steve Sistare

Steve Sistare (4):
iommufd: Export do_update_pinned
iommufd: Lock all IOAS objects
iommufd: Add IOMMU_IOAS_CHANGE_PROCESS
iommufd: IOMMU_IOAS_CHANGE_PROCESS selftest

Applied to iommufd for-next thanks

Jason