Skip to contentSkip to conversation

Introduce file handler dependency level

RFC PATCH In review Samiullah Khawaja 2 participants 8 messages Oct 1 – Oct 4

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

cover Introduce file handler dependency level

LUO allows preservation of FDs into sessions. An FD can depend on other FDs, and for some FDs this can mean the dependency must be preserved before it due to various reasons including immutability and performance. See discussion on FD dependency patch series [1] and documentation [2] for details. Also see the Liveupdate IOMMU [3] and guest_memfd preservation [4] patch series for examples. These dependencies are enforced by the file handlers, and userspace is responsible for preserving the FDs in the correct order.

This RFC proposes a mechanism that allows userspace to preserve the FDs as a batch, and the kernel preserves them in the required order. This allows the VMM to preserve a bag of FDs without worrying about the order. A new ioctl is added that allows userspace to provide an array of FDs and tokens for preservation. This gives the kernel the token and the intent to preserve every FD in the batch up front.

This RFC introduces the concept of file handler levels. Each file handler offers a certain type of resource or functionality that relates to other file handlers. These can be represented by levels. This RFC adds the following levels and more can be added later:

- Default (no level set)
- Memory Providers
- Memory Mappers (or users)
- Devices

The levels are optional static configurations of how the file handlers relate to each other. Each level represents a class of file handler, which fixes the order of preservation between them.

The levels are spaced out to allow addition of new levels. LUO can use these levels to deduce the order of preservation of a batch of FDs.

The batch is atomic: either all FDs are preserved or none are, and on failure the index of the failing FD is returned to userspace. The existing LIVEUPDATE_SESSION_PRESERVE_FD ioctl is unchanged and behaves as a batch of one. Note that this does not break compatibility and userspace can still attempt to preserve FDs individually using the existing preservation ioctl.

The patch series builds on top of the Liveupdate IOMMU series [3] to demonstrate the FD dependency. The full tree, including the dependencies, is available at:
https://github.com/samikhawaja/linux/tree/luo/fd-dependency-rfc

I will present this at the Live Update MC at LPC 2026. I will also talk about alternative solutions I considered.

Future work:

- Allow file handlers to be preserved at multiple levels to allow resolving circular dependencies.

Looking forward to your feedback on this.

[1] https://lore.kernel.org/all/[email protected]/#t
[2] https://lore.kernel.org/all/[email protected]/
[3] https://lore.kernel.org/all/[email protected]/
[4] https://lore.kernel.org/all/[email protected]/

Samiullah Khawaja (6):
liveupdate: Introduce file handler dependency level mm/memfd_luo: Set Liveupdate level of a memfd file handler
iommufd: Set liveupdate file handler level vfio/pci: Set live update file handler level selftests/liveupdate: Add API to preserve a batch of FDs iommufd/selftests: Preserve all the FDs in a batch

 drivers/iommu/iommufd/liveupdate.c            |   1 +
 drivers/vfio/pci/vfio_pci_liveupdate.c        |   1 +
 include/linux/liveupdate.h                    |  24 ++
 include/uapi/linux/liveupdate.h               |  39 +++
 kernel/liveupdate/luo_file.c                  | 316 ++++++++++++------
 kernel/liveupdate/luo_internal.h              |   2 +
 kernel/liveupdate/luo_session.c               |  45 +++
 mm/memfd_luo.c                                |   1 +
 .../iommu/iommufd_liveupdate_kexec_test.c     |  27 +-
 .../liveupdate/lib/include/libliveupdate.h    |   2 +
 .../selftests/liveupdate/lib/lu_utils.c       |  20 ++
 11 files changed, 379 insertions(+), 99 deletions(-)

base-commit: 945d61765894dda2f0344de50cb1ade2e51b66fd

Signature · 1 line
2.56.0.rc1.315.gc6ed9934b7-goog
#1 lore raw

RFC PATCH 1/6 liveupdate: Introduce file handler dependency level

Dependency level is an optional level in the file handler that can be used to indicate the order in which FDs are preserved. The level define the type of state an FD preserved and how it relates to other type of FDs. For example a memory provider FD might be a dependency of an FD that uses that memory.

---
 include/linux/liveupdate.h       |  24 +++
 include/uapi/linux/liveupdate.h  |  39 ++++
 kernel/liveupdate/luo_file.c     | 316 +++++++++++++++++++++----------
 kernel/liveupdate/luo_internal.h |   2 +
 kernel/liveupdate/luo_session.c  |  45 +++++
 5 files changed, 330 insertions(+), 96 deletions(-)
include/linux/liveupdate.h +24 -0
index 6051abc0612c..ba0af91126e1 100644
@@ -87,6 +87,27 @@ struct liveupdate_file_ops {
 	struct module *owner;
 };
 
+/**
+ * enum liveupdate_level - Preservation order of a file handler.
+ * @LIVEUPDATE_LEVEL_NONE:   Level not set. The handler does not depend on
+ *                           any other preserved file. Preserved first.
+ * @LIVEUPDATE_LEVEL_MEM:    Files that provide memory.
+ * @LIVEUPDATE_LEVEL_MAPPER: Files that map memory provided by a lower level.
+ * @LIVEUPDATE_LEVEL_DEVICE: Files that represent devices.
+ *
+ * When a batch of files is preserved with LIVEUPDATE_SESSION_PRESERVE_FDS,
+ * LUO calls the handlers' .preserve() in ascending level order.
+ *
+ * The values are spaced so that new levels can be inserted without
+ * renumbering. The level is kernel internal and not part of any ABI.
+ */
+enum liveupdate_level {
+	LIVEUPDATE_LEVEL_NONE	= 0x0,
+	LIVEUPDATE_LEVEL_MEM	= 0x10,
+	LIVEUPDATE_LEVEL_MAPPER	= 0x20,
+	LIVEUPDATE_LEVEL_DEVICE	= 0x30,
+};
+
 /**
  * struct liveupdate_file_handler - Represents a handler for a live-updatable file type.
  * @ops:                Callback functions
@@ -94,6 +115,8 @@ struct liveupdate_file_ops {
  *                      that uniquely identifies the file type this handler
  *                      supports. This is matched against the compatible string
  *                      associated with individual &struct file instances.
+ * @level:              Optional. Preservation order of this handler. See &enum
+ *                      liveupdate_level.
  *
  * Modules that want to support live update for specific file types should
  * register an instance of this structure. LUO uses this registration to
@@ -103,6 +126,7 @@ struct liveupdate_file_ops {
 struct liveupdate_file_handler {
 	const struct liveupdate_file_ops *ops;
 	const char compatible[LIVEUPDATE_HNDL_COMPAT_LENGTH];
+	enum liveupdate_level level;
 
 	/* private: */
 
include/uapi/linux/liveupdate.h +39 -0
index 4043d4038712..122275ab7ca2 100644
@@ -60,6 +60,7 @@ enum {
 	LIVEUPDATE_CMD_SESSION_RETRIEVE_FD = 0x41,
 	LIVEUPDATE_CMD_SESSION_FINISH = 0x42,
 	LIVEUPDATE_CMD_SESSION_GET_NAME = 0x43,
+	LIVEUPDATE_CMD_SESSION_PRESERVE_FDS = 0x44,
 };
 
 /**
@@ -236,4 +237,42 @@ struct liveupdate_session_get_name {
 #define LIVEUPDATE_SESSION_GET_NAME					\
 	_IO(LIVEUPDATE_IOCTL_TYPE, LIVEUPDATE_CMD_SESSION_GET_NAME)
 
+/**
+ * struct liveupdate_session_preserve_fds - ioctl(LIVEUPDATE_SESSION_PRESERVE_FDS)
+ * @size:             Input; sizeof(struct liveupdate_session_preserve_fds)
+ * @nr:               Input; Number of entries in @fds and @tokens. Must not be
+ *                    zero.
+ * @fds:              Input; User pointer to an array of @nr __s32 file
+ *                    descriptors to be preserved.
+ * @tokens:           Input; User pointer to an array of @nr __u64 opaque,
+ *                    unique tokens, one for each entry in @fds.
+ * @out_failed_index: Output; On failure, the index of the entry in @fds that
+ *                    caused the failure, or @nr if the failure is not
+ *                    specific to an entry.
+ * @__reserved:       Input; Must be zero.
+ *
+ * Preserve a batch of file descriptors, some of which may depend on others in
+ * the same batch. The entries may be given in any order; the kernel preserves
+ * them in dependency order.
+ *
+ * All dependencies of a file must either be part of the same batch or have
+ * been preserved earlier in this session.
+ *
+ * The operation is atomic: either all file descriptors in the batch are
+ * preserved, or none are and the session is unchanged.
+ *
+ * Return: 0 on success, negative error code on failure.
+ */
+struct liveupdate_session_preserve_fds {
+	__u32		size;
+	__u32		nr;
+	__aligned_u64	fds;
+	__aligned_u64	tokens;
+	__u32		out_failed_index;
+	__u32		__reserved;
+};
+
+#define LIVEUPDATE_SESSION_PRESERVE_FDS					\
+	_IO(LIVEUPDATE_IOCTL_TYPE, LIVEUPDATE_CMD_SESSION_PRESERVE_FDS)
+
 #endif /* _UAPI_LIVEUPDATE_H */
kernel/liveupdate/luo_file.c +220 -96

407 lines not on this page · raw message

kernel/liveupdate/luo_internal.h +2 -0
index dac6644bfb18..98ff5c629ac0 100644
@@ -102,6 +102,8 @@ int luo_session_serialize(void);
 int luo_session_deserialize(void);
 
 int luo_preserve_file(struct luo_file_set *file_set, u64 token, int fd);
+int luo_preserve_files(struct luo_file_set *file_set, const u64 *tokens,
+		       const int *fds, u32 nr, u32 *failed_idx);
 void luo_file_unpreserve_files(struct luo_file_set *file_set);
 int luo_file_freeze(struct luo_file_set *file_set,
 		    struct luo_file_set_ser *file_set_ser);
kernel/liveupdate/luo_session.c +45 -0

63 lines not on this page · raw message

Signature · 1 line
2.56.0.rc1.315.gc6ed9934b7-goog
#2 lore raw

RFC PATCH 2/6 mm/memfd_luo: Set Liveupdate level of a memfd file handler

In reply to Samiullah Khawaja

Set level of the memfd file handler to LIVEUPDATE_LEVEL_MEM as it is a memory provider.

---
 mm/memfd_luo.c | 1 +
 1 file changed, 1 insertion(+)
mm/memfd_luo.c +1 -0
index 59de210bee5f..91ddd7ae64b3 100644
@@ -604,6 +604,7 @@ static const struct liveupdate_file_ops memfd_luo_file_ops = {
 static struct liveupdate_file_handler memfd_luo_handler = {
 	.ops = &memfd_luo_file_ops,
 	.compatible = MEMFD_LUO_FH_COMPATIBLE,
+	.level = LIVEUPDATE_LEVEL_MEM,
 };
 
 static int __init memfd_luo_init(void)
Signature · 1 line
2.56.0.rc1.315.gc6ed9934b7-goog
#3 lore raw

RFC PATCH 3/6 iommufd: Set liveupdate file handler level

In reply to Samiullah Khawaja

Set level of iommufd preservation file handler to
LIVEUPDATE_LEVEL_MAPPER.

---
 drivers/iommu/iommufd/liveupdate.c | 1 +
 1 file changed, 1 insertion(+)
drivers/iommu/iommufd/liveupdate.c +1 -0
index 423369e3a8af..2f2ae1a52be4 100644
@@ -351,6 +351,7 @@ static struct liveupdate_file_ops iommufd_ser_file_ops = {
 static struct liveupdate_file_handler iommufd_ser_handler = {
 	.compatible = IOMMUFD_LUO_COMPATIBLE,
 	.ops = &iommufd_ser_file_ops,
+	.level = LIVEUPDATE_LEVEL_MAPPER,
 };
 
 int iommufd_liveupdate_register(void)
Signature · 1 line
2.56.0.rc1.315.gc6ed9934b7-goog
#4 lore raw

RFC PATCH 4/6 vfio/pci: Set live update file handler level

In reply to Samiullah Khawaja

Set live update file handler to LIVEUPDATE_LEVEL_DEVICE.

---
 drivers/vfio/pci/vfio_pci_liveupdate.c | 1 +
 1 file changed, 1 insertion(+)
drivers/vfio/pci/vfio_pci_liveupdate.c +1 -0
index fb2141ee8942..4b8a350b867a 100644
@@ -335,6 +335,7 @@ static const struct liveupdate_file_ops vfio_pci_liveupdate_file_ops = {
 static struct liveupdate_file_handler vfio_pci_liveupdate_fh = {
 	.ops = &vfio_pci_liveupdate_file_ops,
 	.compatible = VFIO_PCI_LUO_FH_COMPATIBLE,
+	.level = LIVEUPDATE_LEVEL_DEVICE,
 };
 
 int __init vfio_pci_liveupdate_init(void)
Signature · 1 line
2.56.0.rc1.315.gc6ed9934b7-goog
#5 lore raw

RFC PATCH 5/6 selftests/liveupdate: Add API to preserve a batch of FDs

In reply to Samiullah Khawaja

Add luo_session_preserve_fds() that can be used to preserve a batch of FDs using the LIVEUPDATE_SESSION_PRESERVE_FDS ioctl.

---
 .../liveupdate/lib/include/libliveupdate.h    |  2 ++
 .../selftests/liveupdate/lib/lu_utils.c       | 20 +++++++++++++++++++
 2 files changed, 22 insertions(+)
tools/testing/selftests/liveupdate/lib/include/libliveupdate.h +2 -0
index fa07fed08364..9aaf6f28ef56 100644
@@ -29,6 +29,8 @@ int luo_get_session_name(int session_fd, char *name, size_t name_len);
 
 int luo_ensure_nofile_limit(long min_limit);
 int luo_session_preserve_fd(int session_fd, int fd, __u64 token);
+int luo_session_preserve_fds(int session_fd, const int *fds,
+			     const __u64 *tokens, __u32 nr, __u32 *failed_idx);
 int luo_session_retrieve_fd(int session_fd, __u64 token);
 
 int create_and_preserve_memfd(int session_fd, int token, const char *data);
tools/testing/selftests/liveupdate/lib/lu_utils.c +20 -0
index 74d41115c281..1290f27ff004 100644
@@ -93,6 +93,26 @@ int luo_session_preserve_fd(int session_fd, int fd, __u64 token)
 	return 0;
 }
 
+int luo_session_preserve_fds(int session_fd, const int *fds,
+			     const __u64 *tokens, __u32 nr, __u32 *failed_idx)
+{
+	struct liveupdate_session_preserve_fds arg = {
+		.size = sizeof(arg),
+		.nr = nr,
+		.fds = (__u64)(uintptr_t)fds,
+		.tokens = (__u64)(uintptr_t)tokens,
+	};
+	int ret = 0;
+
+	if (ioctl(session_fd, LIVEUPDATE_SESSION_PRESERVE_FDS, &arg))
+		ret = -errno;
+
+	if (failed_idx)
+		*failed_idx = arg.out_failed_index;
+
+	return ret;
+}
+
 int luo_session_retrieve_fd(int session_fd, __u64 token)
 {
 	struct liveupdate_session_retrieve_fd arg = {
Signature · 1 line
2.56.0.rc1.315.gc6ed9934b7-goog
#6 lore raw

RFC PATCH 6/6 iommufd/selftests: Preserve all the FDs in a batch

In reply to Samiullah Khawaja

Use the luo_session_preserve_fds() to preserve vfio cdev, iommufd and memfd together in a batch.

---
 .../iommu/iommufd_liveupdate_kexec_test.c     | 27 ++++++++++++++++---
 1 file changed, 24 insertions(+), 3 deletions(-)
tools/testing/selftests/iommu/iommufd_liveupdate_kexec_test.c +24 -3
index 26c7e15b6187..3ce282fc1a33 100644
@@ -153,6 +153,9 @@ static void setup_iommufd(int iommufd, int memfd, int cdev_fd)
 static void before_kexec(int luo_fd)
 {
 	int iommufd, cdev_fd, memfd, session;
+	__u64 tokens[3];
+	__u32 failed;
+	int fds[3];
 
 	create_state_file(luo_fd, state_session, STATE_TOKEN, /*next_stage=*/2);
 
@@ -174,9 +177,27 @@ static void before_kexec(int luo_fd)
 	if (!luo_session_preserve_fd(session, iommufd, IOMMUFD_TOKEN))
 		fail_exit("Preserving iommufd without memfd should fail");
 
-	test_luo_session_preserve_fd(session, memfd, MEMFD_TOKEN);
-	test_luo_session_preserve_fd(session, iommufd, IOMMUFD_TOKEN);
-	test_luo_session_preserve_fd(session, cdev_fd, CDEV_TOKEN);
+	/*
+	 * Batch without iommufd: memfd is preserved first, then cdev fails.
+	 * The whole batch must be rolled back, including the memfd.
+	 */
+	fds[0] = cdev_fd;
+	tokens[0] = CDEV_TOKEN;
+	fds[1] = memfd;
+	tokens[1] = MEMFD_TOKEN;
+	if (!luo_session_preserve_fds(session, fds, tokens, 2, &failed))
+		fail_exit("Batch preserve without iommufd should fail");
+	ksft_assert(failed == 0);
+
+	/* Full batch, passed in reverse dependency order. */
+	fds[0] = cdev_fd;
+	tokens[0] = CDEV_TOKEN;
+	fds[1] = iommufd;
+	tokens[1] = IOMMUFD_TOKEN;
+	fds[2] = memfd;
+	tokens[2] = MEMFD_TOKEN;
+	if (luo_session_preserve_fds(session, fds, tokens, 3, &failed))
+		fail_exit("Batch preserve failed at index %u", failed);
 
 	close(session);
 	session = luo_create_session(luo_fd, iommufd_session);
Signature · 1 line
2.56.0.rc1.315.gc6ed9934b7-goog
maintainer
#7 lore raw
In reply to Samiullah Khawaja

Hi Sami,

I've skimmed the patches. I didn't dive too deep into the code, but here is at least my first reaction.

Quoted 26 lines · Samiullah Khawaja

LUO allows preservation of FDs into sessions. An FD can depend on other FDs, and for some FDs this can mean the dependency must be preserved before it due to various reasons including immutability and performance. See discussion on FD dependency patch series [1] and documentation [2] for details. Also see the Liveupdate IOMMU [3] and guest_memfd preservation [4] patch series for examples. These dependencies are enforced by the file handlers, and userspace is responsible for preserving the FDs in the correct order.

This RFC proposes a mechanism that allows userspace to preserve the FDs as a batch, and the kernel preserves them in the required order. This allows the VMM to preserve a bag of FDs without worrying about the order. A new ioctl is added that allows userspace to provide an array of FDs and tokens for preservation. This gives the kernel the token and the intent to preserve every FD in the batch up front.

This RFC introduces the concept of file handler levels. Each file handler offers a certain type of resource or functionality that relates to other file handlers. These can be represented by levels. This RFC adds the following levels and more can be added later:

- Default (no level set)
- Memory Providers
- Memory Mappers (or users)
- Devices

Quoted 6 lines

The levels are optional static configurations of how the file handlers relate to each other. Each level represents a class of file handler, which fixes the order of preservation between them.

The levels are spaced out to allow addition of new levels. LUO can use these levels to deduce the order of preservation of a batch of FDs.

I think you need to explain what problem you actually solve here.

This seems like a lot of complexity to essentially turn file handler dependencies into heuristics. Because you basically assign a "priority number" to each file type. But dependencies can really be complicated and I think modelling them as absolute integers is going to paint us in a corner real quick.

And I don't get what this buys us. Why not take the much much simpler path where a file handler just rejects a file if its dependency isn't there? This would force userspace to preserve files in the right order.

And userspace doesn't necessarily have to to anything complicated either. It just needs to write the code in a way that a memfd is preserved before its iommufd. So we leave it up to userspace to figure out the order, and in the kernel we only _enforce_ it.

This also lets us express the real dependencies and we don't have to do the dance of giving each file type a number and trying to make sure the number sits in the right place on the list for all use cases.

Or, if you _don't_ want to burden userspace with this work, why not take the files in any order and do the final check at freeze()?

Quoted 51 lines

The batch is atomic: either all FDs are preserved or none are, and on failure the index of the failing FD is returned to userspace. The existing LIVEUPDATE_SESSION_PRESERVE_FD ioctl is unchanged and behaves as a batch of one. Note that this does not break compatibility and userspace can still attempt to preserve FDs individually using the existing preservation ioctl.

The patch series builds on top of the Liveupdate IOMMU series [3] to demonstrate the FD dependency. The full tree, including the dependencies, is available at:
https://github.com/samikhawaja/linux/tree/luo/fd-dependency-rfc

I will present this at the Live Update MC at LPC 2026. I will also talk about alternative solutions I considered.

Future work:

- Allow file handlers to be preserved at multiple levels to allow resolving circular dependencies.

Looking forward to your feedback on this.

[1] https://lore.kernel.org/all/[email protected]/#t
[2] https://lore.kernel.org/all/[email protected]/
[3] https://lore.kernel.org/all/[email protected]/
[4] https://lore.kernel.org/all/[email protected]/

Samiullah Khawaja (6):
liveupdate: Introduce file handler dependency level mm/memfd_luo: Set Liveupdate level of a memfd file handler
iommufd: Set liveupdate file handler level vfio/pci: Set live update file handler level selftests/liveupdate: Add API to preserve a batch of FDs iommufd/selftests: Preserve all the FDs in a batch

 drivers/iommu/iommufd/liveupdate.c            |   1 +
 drivers/vfio/pci/vfio_pci_liveupdate.c        |   1 +
 include/linux/liveupdate.h                    |  24 ++
 include/uapi/linux/liveupdate.h               |  39 +++
 kernel/liveupdate/luo_file.c                  | 316 ++++++++++++------
 kernel/liveupdate/luo_internal.h              |   2 +
 kernel/liveupdate/luo_session.c               |  45 +++
 mm/memfd_luo.c                                |   1 +
 .../iommu/iommufd_liveupdate_kexec_test.c     |  27 +-
 .../liveupdate/lib/include/libliveupdate.h    |   2 +
 .../selftests/liveupdate/lib/lu_utils.c       |  20 ++
 11 files changed, 379 insertions(+), 99 deletions(-)

base-commit: 945d61765894dda2f0344de50cb1ade2e51b66fd

Signature · 2 lines
Regards,
Pratyush Yadav