Skip to contentSkip to conversation

luo: tmpfs preservation

RFC PATCH In review Pratyush Yadav 3 participants 15 messages Sep 23 – Sep 24

Subsystems (9)
lore
Share & tools
lore

Pratyush Yadav

author maintainer
#0 lore raw

cover luo: tmpfs preservation

From: "Pratyush Yadav (Google)" <[email protected]>

Hi,

I brought this idea up in this week's Hypervisor Live Update bi-weekly. I decided to try using an LLM to see if it can produce a proof-of-concept quickly. This series is the end result.

The main use case is preserving in-memory files that have a filesystem path. We already support memfd preservation, but memfds can't be linked to a (user-visible) filesystem. This is needed for storing VMM packages for live update on hosts that don't have a disk. A cold boot fetches the binaries from network, but that is too slow for a live update.

David tried to solve the problem by introducing LIVEUPDATE_SESSION_RETRIEVE_INTO_FD [0], which lets you provide a FD for LUO to retrieve into. This is an alternative to the idea. It uses the standard preservation and retrieval API that LUO already provides.

The core idea is to allow userspace to preserve a tmpfs mount FD. Once the mount is preserved, userspace can pass in regular files in that mount for preservation. The files take a dependency on the mount token, and that is used for retrieving the files in the right mount. This saves us from doing a full FS preservation and makes preservation of each file explicit.

Currently only files in the root are supported. Files in subdirectories will be rejected. This is mainly for simplicity. Complex mount features like memory policies, id mappings, or casefolding are also not supported. All these can be reconfigured after retrieve if really needed.

The code re-uses a lot of the preservation and retrieval logic from memfd preservation. It only adds some extra file and mount metadata on top.

As I mentioned earlier, this is heavily LLM generated. The code is not very polished and has some rough edges. That said, I have read all the code and did significant cleanups of the LLM output. This includes turning the 1600 or so lines it generated to a more modest 977 lines. So (I think) it isn't complete AI garbage. And I think it does get the core idea across.

The big exception to this is the changes to and use of VFS APIs. I am not competent with VFS at all so I have mostly taken the LLM at its word and haven't done my homework to see if the usage even makes sense. That's why I have not Cced any of the VFS maintainers. I'd rather spare them the pain.

[0] https://lore.kernel.org/kexec/[email protected]/T/#u

Regards,
Pratyush Yadav

Pratyush Yadav (Google) (6):
liveupdate: luo_file: look up outgoing tokens by id
shmem: add tmpfs_create_mount() to create tmpfs mounts internally fs/namespace: Add vfs_open_detached_mount() mm/memfd_luo: allow preserving a tmpfs mount mm/memfd_luo: allow preserving a tmpfs file selftests/liveupdate: add tmpfs kexec test

 Documentation/core-api/liveupdate.rst         |   1 +
 Documentation/mm/index.rst                    |   1 +
 Documentation/mm/tmpfs_preservation.rst       |  24 +
 MAINTAINERS                                   |   1 +
 fs/namespace.c                                |  59 ++
 include/linux/kho/abi/tmpfs.h                 |  83 +++
 include/linux/liveupdate.h                    |   6 +-
 include/linux/mount.h                         |   1 +
 include/linux/shmem_fs.h                      |   5 +
 kernel/liveupdate/luo_file.c                  |  21 +-
 mm/internal.h                                 |   1 +
 mm/memfd_luo.c                                | 558 +++++++++++++++++-
 mm/shmem.c                                    |  43 +-
 tools/testing/selftests/liveupdate/Makefile   |   1 +
 .../selftests/liveupdate/luo_kexec_tmpfs.c    | 191 ++++++
 .../selftests/liveupdate/run-vmtests.sh       |   1 +
 16 files changed, 977 insertions(+), 20 deletions(-)
 create mode 100644 Documentation/mm/tmpfs_preservation.rst
 create mode 100644 include/linux/kho/abi/tmpfs.h
 create mode 100644 tools/testing/selftests/liveupdate/luo_kexec_tmpfs.c

base-commit: db3db4c33a1cf89a201ad1a11c63f503b248ee32

Signature · 1 line
2.56.0.rc1.310.g51773c2048-goog
author maintainer
#1 lore raw

RFC PATCH 1/6 liveupdate: luo_file: look up outgoing tokens by id

From: "Pratyush Yadav (Google)" <[email protected]>

liveupdate_get_token_outgoing() is used to look up tokens of dependencies. It does the search based on struct file. LUO preserves resources; the files are only handles to those resources.

LUO provides file handlers a ->get_id() callback that can return the unique identifier of a resource. LUO uses it to ensure the same resource can't be preserved twice if it can be referred to by multiple files. The default ID is the struct file, but file handlers can implement the callback and return something else. For example, memfd returns the inode of the file, not the struct file itself.

Use ID to look up dependencies in liveupdate_get_token_outgoing() instead of struct file. This allows the lookup to work with handlers that use things other than the struct file.

---
 include/linux/liveupdate.h   |  6 +++---
 kernel/liveupdate/luo_file.c | 21 ++++++++++++---------
 2 files changed, 15 insertions(+), 12 deletions(-)
include/linux/liveupdate.h +3 -3
index 6051abc0612c..1cddbab50b98 100644
@@ -253,9 +253,9 @@ void liveupdate_flb_put_outgoing(struct liveupdate_flb *flb);
 int liveupdate_get_file_incoming(struct liveupdate_session *s, u64 token,
 				 struct file **filep);
 
-/* Get a token for an outgoing file, or -ENOENT if file is not preserved */
+/* Get a token for a preserved object, or -ENOENT if it is not preserved */
 int liveupdate_get_token_outgoing(struct liveupdate_session *s,
-				  struct file *file, u64 *tokenp);
+				  unsigned long id, u64 *tokenp);
 
 #else /* CONFIG_LIVEUPDATE */
 
@@ -316,7 +316,7 @@ static inline int liveupdate_get_file_incoming(struct liveupdate_session *s,
 }
 
 static inline int liveupdate_get_token_outgoing(struct liveupdate_session *s,
-						struct file *file, u64 *tokenp)
+						unsigned long id, u64 *tokenp)
 {
 	return -EOPNOTSUPP;
 }
kernel/liveupdate/luo_file.c +12 -9
index a39992da504f..51817d202ce2 100644
@@ -950,29 +950,32 @@ void liveupdate_unregister_file_handler(struct liveupdate_file_handler *fh)
 }
 
 /**
- * liveupdate_get_token_outgoing - Get the token for a preserved file.
+ * liveupdate_get_token_outgoing - Get the token for a preserved object.
  * @s:      The outgoing liveupdate session.
- * @file:   The file object to search for.
+ * @id:     The identifier of the preserved object to search for, as returned
+ *          by the owning handler's ->get_id().
  * @tokenp: Output parameter for the found token.
  *
- * Searches the list of preserved files in an outgoing session for a matching
- * file object. If found, the corresponding user-provided token is returned.
+ * Searches the list of preserved files in an outgoing session for an object
+ * with a matching identifier. If found, the corresponding user-provided token
+ * is returned.
  *
- * This function is intended for in-kernel callers that need to correlate a
- * file with its liveupdate token.
+ * For handlers that do not implement ->get_id() use the 'struct file' pointer
+ * as their identifier.
  *
  * Context: It must be called with session mutex acquired.
- * Return: 0 on success, -ENOENT if the file is not preserved in this session.
+ * Return: 0 on success, -ENOENT if no such object is preserved in this
+ *         session.
  */
 int liveupdate_get_token_outgoing(struct liveupdate_session *s,
-				  struct file *file, u64 *tokenp)
+				  unsigned long id, u64 *tokenp)
 {
 	struct luo_file_set *file_set = luo_file_set_from_session_locked(s);
 	struct luo_file *luo_file;
 	int err = -ENOENT;
 
 	list_for_each_entry(luo_file, &file_set->files_list, list) {
-		if (luo_file->file == file) {
+		if (luo_get_id(luo_file->fh, luo_file->file) == id) {
 			if (tokenp)
 				*tokenp = luo_file->token;
 			err = 0;
Signature · 1 line
2.56.0.rc1.310.g51773c2048-goog
#2 lore raw

Re: [RFC PATCH 1/6] liveupdate: luo_file: look up outgoing tokens by id

Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider: - [High] liveupdate: NULL pointer dereference in liveupdate_get_token_outgoing() on incoming sessions…

Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
- [High] liveupdate: NULL pointer dereference in liveupdate_get_token_outgoing() on incoming sessions
- [Medium] liveupdate: flat ID namespace in liveupdate_get_token_outgoing() risks cross-handler collisions
- [Low] liveupdate: O(N) indirect function calls in liveupdate_get_token_outgoing() list traversal --

commit 17b042e4901cfc25a86dec1038092fbe1eb5d060
Author: Pratyush Yadav (Google) <[email protected]>

liveupdate: luo_file: look up outgoing tokens by id

liveupdate_get_token_outgoing() is used to look up tokens of dependencies. It does the search based on struct file. LUO provides file handlers a get_id callback that can return the unique identifier of a resource. This patch uses ID to look up dependencies in liveupdate_get_token_outgoing() instead of struct file.

Quoted 4 lines
kernel/liveupdate/luo_file.c +0 -0
index a39992da504fb..51817d202ce26 100644

[ ... ]

kernel/liveupdate/luo_file.c

Quoted 3 lines

int liveupdate_get_token_outgoing(struct liveupdate_session *s,

kernel/liveupdate/luo_file.c +1 -1
-				  struct file *file, u64 *tokenp)
+				  unsigned long id, u64 *tokenp)

[Severity: Medium]
Will this flat ID namespace risk cross-handler collisions?

Because liveupdate_get_token_outgoing() searches across all preserved files using just an unsigned long id, it loses the context of which file handler the ID belongs to.

If different handlers use overlapping namespaces, such as IDR indices or pointers that might alias depending on object lifetime, a lookup intended for one handler's object could incorrectly match an object from a different handler that happens to appear earlier in the list.

Could the API require the expected file handler context to scope the search safely?

kernel/liveupdate/luo_file.c void liveupdate_unregister_file_handler(struct liveupdate_file_handler *fh) · line 978

Quoted 8 lines

{

kernel/liveupdate/luo_file.c +1 -1
 	struct luo_file_set *file_set = luo_file_set_from_session_locked(s);
 	struct luo_file *luo_file;
 	int err = -ENOENT;
 
 	list_for_each_entry(luo_file, &file_set->files_list, list) {
-		if (luo_file->file == file) {
+		if (luo_get_id(luo_file->fh, luo_file->file) == id) {

[Severity: High]
Can this cause a NULL pointer dereference on incoming sessions?

In an incoming session after deserialization, luo_file->file is initialized to NULL and remains NULL until the file is explicitly retrieved.

Because liveupdate_get_token_outgoing() is an exported API that accepts a generic struct liveupdate_session, an in-kernel caller could mistakenly pass an incoming session.

Prior to this patch, doing so would safely return -ENOENT because NULL == file is false. With this patch, luo_get_id() will blindly pass the NULL file pointer to the handler's get_id callback.

Handlers like memfd_luo call file_inode(file), which would cause an immediate NULL pointer dereference and kernel crash.

[Severity: Low]
Are these indirect function calls necessary during the list traversal?

Replacing the direct pointer comparison with luo_get_id() performs an indirect function call (fh->ops->get_id) if defined, on every iteration of the files_list traversal.

Because this function is used to look up tokens for dependencies, it can be called for every dependency of every preserved object, resulting in O(N * M) indirect calls.

Since the ID is a constant property of the preserved file, could it be evaluated exactly once during luo_preserve_file() and cached directly in struct luo_file?

Quoted 3 lines
kernel/liveupdate/luo_file.c +0 -0
 			if (tokenp)
 				*tokenp = luo_file->token;
 			err = 0;
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
author maintainer
#3 lore raw

Re: [RFC PATCH 1/6] liveupdate: luo_file: look up outgoing tokens by id

Quoted 5 lines · On Wed, Sep 23 2026, [email protected] wrote:

Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
- [High] liveupdate: NULL pointer dereference in liveupdate_get_token_outgoing() on incoming sessions
- [Medium] liveupdate: flat ID namespace in liveupdate_get_token_outgoing() risks cross-handler collisions
- [Low] liveupdate: O(N) indirect function calls in liveupdate_get_token_outgoing() list traversal --

Let me reply to Sashiko for this patch since I think we should take this regardless of where this series goes. I think struct file is the wrong primitive for liveupdate_get_token_outgoing().

I'll ignore Sashiko on other patches. It does raise some good points, but code correctness isn't the main point of the series. I'll fix them up on the main posting if we agree to go in this direction.

Quoted 24 lines

commit 17b042e4901cfc25a86dec1038092fbe1eb5d060
Author: Pratyush Yadav (Google) <[email protected]>

liveupdate: luo_file: look up outgoing tokens by id

liveupdate_get_token_outgoing() is used to look up tokens of dependencies. It does the search based on struct file. LUO provides file handlers a get_id callback that can return the unique identifier of a resource. This patch uses ID to look up dependencies in liveupdate_get_token_outgoing() instead of struct file.

Quoted 4 lines
kernel/liveupdate/luo_file.c +0 -0
index a39992da504fb..51817d202ce26 100644

[ ... ]

kernel/liveupdate/luo_file.c

Quoted 3 lines

int liveupdate_get_token_outgoing(struct liveupdate_session *s,

kernel/liveupdate/luo_file.c +1 -1
-				  struct file *file, u64 *tokenp)
+				  unsigned long id, u64 *tokenp)

[Severity: Medium]
Will this flat ID namespace risk cross-handler collisions?

Quoted 8 lines

Because liveupdate_get_token_outgoing() searches across all preserved files using just an unsigned long id, it loses the context of which file handler the ID belongs to.

If different handlers use overlapping namespaces, such as IDR indices or pointers that might alias depending on object lifetime, a lookup intended for one handler's object could incorrectly match an object from a different handler that happens to appear earlier in the list.

The IDs are supposed to be unique in the whole session. That's the whole point of the ID mechanism. If handlers use overlapping IDs, the handlers are broken and should be fixed.

Quoted 3 lines

Could the API require the expected file handler context to scope the search safely?

This can be done but only when there is a need for it, which we have not seen so far. Without a use-case, namespacing the IDs per-file-handler is overengineering.

Quoted 13 lines

kernel/liveupdate/luo_file.c void liveupdate_unregister_file_handler(struct liveupdate_file_handler *fh) · line 978

Quoted 9 lines

{

kernel/liveupdate/luo_file.c +1 -1
 	struct luo_file_set *file_set = luo_file_set_from_session_locked(s);
 	struct luo_file *luo_file;
 	int err = -ENOENT;
 
 	list_for_each_entry(luo_file, &file_set->files_list, list) {
-		if (luo_file->file == file) {
+		if (luo_get_id(luo_file->fh, luo_file->file) == id) {

[Severity: High]
Can this cause a NULL pointer dereference on incoming sessions?

Quoted 6 lines

In an incoming session after deserialization, luo_file->file is initialized to NULL and remains NULL until the file is explicitly retrieved.

Because liveupdate_get_token_outgoing() is an exported API that accepts a generic struct liveupdate_session, an in-kernel caller could mistakenly pass an incoming session.

That's a broken caller and should be fixed.

Quoted 11 lines

Prior to this patch, doing so would safely return -ENOENT because NULL == file is false. With this patch, luo_get_id() will blindly pass the NULL file pointer to the handler's get_id callback.

Handlers like memfd_luo call file_inode(file), which would cause an immediate NULL pointer dereference and kernel crash.

[Severity: Low]
Are these indirect function calls necessary during the list traversal?

Quoted 7 lines

Replacing the direct pointer comparison with luo_get_id() performs an indirect function call (fh->ops->get_id) if defined, on every iteration of the files_list traversal.

Because this function is used to look up tokens for dependencies, it can be called for every dependency of every preserved object, resulting in O(N * M) indirect calls.

A very large number of files with dependencies need to be preserved for this to be a problem. We haven't hit this bottleneck so far and don't think will hit it at least in the near future.

Quoted 4 lines

Since the ID is a constant property of the preserved file, could it be evaluated exactly once during luo_preserve_file() and cached directly in struct luo_file?

Not a bad idea to be fair. But that's an independent fixup.

Quoted 4 lines
Quoted 4 lines
kernel/liveupdate/luo_file.c +0 -0
 			if (tokenp)
 				*tokenp = luo_file->token;
 			err = 0;
Signature · 2 lines
Regards,
Pratyush Yadav
author maintainer
#4 lore raw

RFC PATCH 2/6 shmem: add tmpfs_create_mount() to create tmpfs mounts internally

In reply to Pratyush Yadav

From: "Pratyush Yadav (Google)" <[email protected]>

Upcoming commits will add support for preserving a tmpfs mount via LUO. While the retrieve logic can create the mount by constructing an options string from its preserved data, it is simpler to let it directly pass the parameters.

tmpfs_create_mount() allows creating a mount with the specified max_blocks and mode. The function only takes max_blocks and mode for now because that is all the tmpfs preservation will use. All other values are left to their defaults.

---
 mm/internal.h |  1 +
 mm/shmem.c    | 38 ++++++++++++++++++++++++++++++++++++++
 2 files changed, 39 insertions(+)
mm/internal.h +1 -0
index 38b1165212c9..d82c92c8fddd 100644
@@ -1436,6 +1436,7 @@ int shmem_add_to_page_cache(struct folio *folio,
 			    pgoff_t index, void *expected, gfp_t gfp);
 int shmem_inode_acct_blocks(struct inode *inode, long pages);
 bool shmem_recalc_inode(struct inode *inode, long alloced, long swapped);
+struct vfsmount *tmpfs_create_mount(unsigned long max_blocks, umode_t mode);
 
 #ifdef CONFIG_SHRINKER_DEBUG
 static inline __printf(2, 0) int shrinker_debugfs_name_alloc(
mm/shmem.c +38 -0
index 897fa2b61346..8660405b6ab8 100644
@@ -5313,6 +5313,44 @@ static struct file_system_type shmem_fs_type = {
 	.fs_flags	= FS_USERNS_MOUNT | FS_ALLOW_IDMAP | FS_MGTIME,
 };
 
+/**
+ * tmpfs_create_mount - Create a new tmpfs instance from explicit parameters.
+ * @max_blocks: Block limit of the filesystem, in PAGE_SIZE units, as held by
+ *              shmem_sb_info::max_blocks.
+ * @mode:       Mode of the root directory.
+ *
+ * Creates a new tmpfs superblock and returns a mount for it. Everything not
+ * named above is left at its default.
+ *
+ * The returned mount belongs to no mount namespace and is owned by the caller,
+ * who must either mntput() it or hand it to something that takes it over.
+ *
+ * Return: the new mount, or an ERR_PTR.
+ */
+struct vfsmount *tmpfs_create_mount(unsigned long max_blocks, umode_t mode)
+{
+	struct shmem_options *ctx;
+	struct fs_context *fc;
+	struct vfsmount *mnt;
+
+	if (!IS_ENABLED(CONFIG_TMPFS))
+		return ERR_PTR(-EOPNOTSUPP);
+
+	fc = fs_context_for_mount(&shmem_fs_type, 0);
+	if (IS_ERR(fc))
+		return ERR_CAST(fc);
+
+	ctx = fc->fs_private;
+	ctx->blocks = max_blocks;
+	ctx->seen |= SHMEM_SEEN_BLOCKS;
+	ctx->mode = mode;
+
+	mnt = fc_mount(fc);
+	put_fs_context(fc);
+
+	return mnt;
+}
+
 #if defined(CONFIG_SYSFS) && defined(CONFIG_TMPFS)
 
 #define __INIT_KOBJ_ATTR(_name, _mode, _show, _store)			\
Signature · 1 line
2.56.0.rc1.310.g51773c2048-goog
#5 lore raw

Re: [RFC PATCH 2/6] shmem: add tmpfs_create_mount() to create tmpfs mounts internally

Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 7 lines

From: "Pratyush Yadav (Google)" <[email protected]>

Upcoming commits will add support for preserving a tmpfs mount via LUO. While the retrieve logic can create the mount by constructing an options string from its preserved data, it is simpler to let it directly pass the parameters.

Quoted 6 lines

tmpfs_create_mount() allows creating a mount with the specified max_blocks and mode. The function only takes max_blocks and mode for now because that is all the tmpfs preservation will use. All other values are left to their defaults.

Sashiko has reviewed this patch and found no issues. It looks great!

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
author maintainer
#6 lore raw

RFC PATCH 3/6 fs/namespace: Add vfs_open_detached_mount()

In reply to Pratyush Yadav

From: "Pratyush Yadav (Google)" <[email protected]>

A mount created in the kernel by fc_mount() and friends has mnt_ns == NULL, and userspace cannot attach such a mount anywhere.

The missing step is the tail of fsmount(2): allocate an anonymous mount namespace, insert the mount as its root, open an O_PATH file on it and mark the file FMODE_NEED_UNMOUNT so the mount is torn down on the final fput() if it is never attached. The anonymous namespace is what gives the detached mount an owning user namespace for permission checks, a place for child mounts, and a lifetime tied to the file.

None of the pieces are reachable from outside fs/namespace.c:
alloc_mnt_ns() and mnt_add_to_ns() are static, and the sequence is already open-coded twice, in fsmount(2) and in open_detached_copy().

Factor it out so an in-kernel creator of a mount can hand it to userspace. The Live Update Orchestrator needs this to return a tmpfs it re-created from state preserved across a kexec; userspace attaches the resulting fd with move_mount(), exactly as it would an fsmount(2) fd.

Only add the helper. Converting fsmount() and open_detached_copy() to it is an obvious follow-up but is left out to keep this small. It is not exported, as the only caller is built in.

---

This patch is entirely LLM generated. It works, but I am not competent enough with VFS APIs to even guess if it is sensible. So please don't take it too seriously. It makes the RFC testable, but for a proper series I will do a lot more homework to make sure this doesn't completely abuse VFS APIs.

---
 fs/namespace.c        | 59 +++++++++++++++++++++++++++++++++++++++++++
 include/linux/mount.h |  1 +
 2 files changed, 60 insertions(+)
fs/namespace.c +59 -0
index 1ecd96c918b3..5e7ba2b61dd9 100644
@@ -4435,6 +4435,65 @@ static unsigned int attr_flags_to_mnt_flags(u64 attr_flags)
 	return mnt_flags;
 }
 
+/**
+ * vfs_open_detached_mount - Publish a new mount as a detached mount file.
+ * @mnt: The mount to publish. Must not be attached to a mount namespace. The
+ *       caller's reference is consumed on success.
+ *
+ * Places @mnt into a new anonymous mount namespace and opens an O_PATH file on
+ * its root, marked FMODE_NEED_UNMOUNT. This is what fsmount(2) hands back, and
+ * the resulting file behaves the same way: it can be attached to the
+ * filesystem hierarchy with
+ *
+ *	move_mount(fd, "", dfd, path, MOVE_MOUNT_F_EMPTY_PATH)
+ *
+ * and the mount is torn down on the final fput() if it never was.
+ *
+ * A mount that is not the root of an anonymous mount namespace cannot be
+ * attached by userspace at all, so a mount freshly made by fc_mount() and
+ * friends has to go through here before it can be given away.
+ *
+ * Return: the new file, or an ERR_PTR. On failure the caller's reference to
+ *         @mnt is dropped, as the mount cannot be published.
+ */
+struct file *vfs_open_detached_mount(struct vfsmount *mnt)
+{
+	struct path path __free(path_put) = {};
+	struct mnt_namespace *ns;
+	struct file *file;
+
+	if (WARN_ON_ONCE(real_mount(mnt)->mnt_ns))
+		return ERR_PTR(-EINVAL);
+
+	ns = alloc_mnt_ns(current->nsproxy->mnt_ns->user_ns, true);
+	if (IS_ERR(ns)) {
+		mntput(mnt);
+		return ERR_CAST(ns);
+	}
+
+	/* The caller's reference becomes the namespace's reference. */
+	ns->root = real_mount(mnt);
+	ns->nr_mounts = 1;
+	mnt_add_to_ns(ns, real_mount(mnt));
+
+	path.mnt = mntget(mnt);
+	path.dentry = dget(mnt->mnt_root);
+
+	file = dentry_open(&path, O_PATH, current_cred());
+	if (IS_ERR(file)) {
+		dissolve_on_fput(mnt);
+		return file;
+	}
+
+	/*
+	 * An apparent O_PATH fd, with a note that the mount needs to be
+	 * unmounted on the final fput() rather than simply put.
+	 */
+	file->f_mode |= FMODE_NEED_UNMOUNT;
+
+	return file;
+}
+
 /*
  * Create a kernel mount representation for a new, prepared superblock
  * (specified by fs_fd) and attach to an open_tree-like file descriptor.
include/linux/mount.h +1 -0
index acfe7ef86a1b..44c1141bc574 100644
@@ -89,6 +89,7 @@ extern struct vfsmount *vfs_create_mount(struct fs_context *fc);
 extern struct vfsmount *vfs_kern_mount(struct file_system_type *type,
 				      int flags, const char *name,
 				      void *data);
+struct file *vfs_open_detached_mount(struct vfsmount *mnt);
 
 extern void mnt_set_expiry(struct vfsmount *mnt, struct list_head *expiry_list);
 extern void mark_mounts_for_expiry(struct list_head *mounts);
Signature · 1 line
2.56.0.rc1.310.g51773c2048-goog
#7 lore raw

Re: [RFC PATCH 3/6] fs/namespace: Add vfs_open_detached_mount()

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] fs: mount reference leak in vfs_open_detached_mount() error path --

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] fs: mount reference leak in vfs_open_detached_mount() error path --

commit 7bbd41a97d183f30490f0856f394037fd1846a6f
Author: Pratyush Yadav (Google) <[email protected]>

fs/namespace: Add vfs_open_detached_mount()

This commit factors out the creation of an anonymous mount namespace and an O_PATH file for a detached mount into a new helper, vfs_open_detached_mount. This allows in-kernel mount creators to hand a detached mount file to userspace, similar to what fsmount does.

Quoted 4 lines
fs/namespace.c +0 -0
index 1ecd96c918b33..5e7ba2b61dd9a 100644

[ ... ]

Quoted 4 lines

+/**
+ * vfs_open_detached_mount - Publish a new mount as a detached mount file.
+ * @mnt: The mount to publish. Must not be attached to a mount namespace. The
+ * caller's reference is consumed on success.

[ ... ]

fs/namespace.c static unsigned int attr_flags_to_mnt_flags(u64 attr_flags) · line 4466

Quoted 11 lines

+ * Return: the new file, or an ERR_PTR. On failure the caller's reference to
+ * @mnt is dropped, as the mount cannot be published.
+ */

fs/namespace.c +8 -0
+struct file *vfs_open_detached_mount(struct vfsmount *mnt)
+{
+	struct path path __free(path_put) = {};
+	struct mnt_namespace *ns;
+	struct file *file;
+
+	if (WARN_ON_ONCE(real_mount(mnt)->mnt_ns))
+		return ERR_PTR(-EINVAL);

[Severity: Medium]
Does this error path leak the vfsmount reference?

The function documentation explicitly guarantees that on failure, the caller's reference to mnt is dropped. If a caller inadvertently passes a mount already attached to a mount namespace, this WARN_ON_ONCE condition evaluates to true and the function returns an error without dropping the reference.

For comparison, the error path immediately below for alloc_mnt_ns failure correctly calls mntput(mnt) before returning.

Quoted 6 lines
fs/namespace.c +6 -0
+
+	ns = alloc_mnt_ns(current->nsproxy->mnt_ns->user_ns, true);
+	if (IS_ERR(ns)) {
+		mntput(mnt);
+		return ERR_CAST(ns);
+	}

[ ... ]

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
author maintainer
#8 lore raw

RFC PATCH 4/6 mm/memfd_luo: allow preserving a tmpfs mount

In reply to Pratyush Yadav

From: "Pratyush Yadav (Google)" <[email protected]>

A memfd can be preserved across a live update, but it has no name, so anything that needs a filesystem path cannot use one.

The main use case is to fetch VMM binaries that need to be kept on the host memory during a live update to avoid the latency of fetching them from network. The host does not have any attached local storage to store the binaries.

Allow preserving a tmpfs mount. The mount is identified with a file pointing to it. Only preserve the number of blocks and root directory mode. All other options are reset to their default values and can be set up again by a remount. The retrieved file points to a detached mount that can be attached to a path with move_mount(2).

Mounts with memory policies, quotas, ID mappings, and casefolding are not supported.

Move SHMEM_SB() into shmem_fs.h to it can be used in memfd_luo.

---
 Documentation/core-api/liveupdate.rst   |   1 +
 Documentation/mm/index.rst              |   1 +
 Documentation/mm/tmpfs_preservation.rst |  24 +++
 MAINTAINERS                             |   1 +
 include/linux/kho/abi/tmpfs.h           |  48 +++++
 include/linux/shmem_fs.h                |   5 +
 mm/memfd_luo.c                          | 238 +++++++++++++++++++++++-
 mm/shmem.c                              |   5 -
 8 files changed, 315 insertions(+), 8 deletions(-)
 create mode 100644 Documentation/mm/tmpfs_preservation.rst
 create mode 100644 include/linux/kho/abi/tmpfs.h
Documentation/core-api/liveupdate.rst +1 -0
index 5a292d0f3706..c92a0a8f6b90 100644
@@ -34,6 +34,7 @@ The following types of file descriptors can be preserved
    :maxdepth: 1
 
    ../mm/memfd_preservation
+   ../mm/tmpfs_preservation
 
 Public API
 ==========
Documentation/mm/index.rst +1 -0
index 13a79f5d092c..46c83fa8db8a 100644
@@ -72,6 +72,7 @@ documentation, or deleted if it has served its purpose.
    page_table_check
    remap_file_pages
    split_page_table_lock
+   tmpfs_preservation
    transhuge
    unevictable-lru
    vmalloced-kernel-stacks
Documentation/mm/tmpfs_preservation.rst new file +24 -0
new file mode 100644
index 000000000000..0c7f5bdaf7ee
@@ -0,0 +1,24 @@
+.. SPDX-License-Identifier: GPL-2.0-or-later
+
+==========================
+tmpfs Preservation via LUO
+==========================
+
+.. kernel-doc:: mm/memfd_luo.c
+   :doc: tmpfs Preservation via LUO
+
+tmpfs Preservation ABI
+======================
+
+.. kernel-doc:: include/linux/kho/abi/tmpfs.h
+   :doc: tmpfs Live Update ABI
+
+.. kernel-doc:: include/linux/kho/abi/tmpfs.h
+   :internal:
+
+See Also
+========
+
+- :doc:`/mm/memfd_preservation`
+- :doc:`/core-api/liveupdate`
+- :doc:`/core-api/kho/index`
MAINTAINERS +1 -0
index 3a19da74d00c..0527e92fd7ad 100644
@@ -15185,6 +15185,7 @@ S:	Maintained
 T:	git git://git.kernel.org/pub/scm/linux/kernel/git/liveupdate/linux.git
 F:	Documentation/core-api/liveupdate.rst
 F:	Documentation/mm/memfd_preservation.rst
+F:	Documentation/mm/tmpfs_preservation.rst
 F:	Documentation/userspace-api/liveupdate.rst
 F:	include/linux/kho/abi/
 F:	include/linux/liveupdate.h
include/linux/kho/abi/tmpfs.h new file +48 -0

48 lines not on this page · raw message

include/linux/shmem_fs.h +5 -0
index 5663dff53186..d41d38541c73 100644
@@ -95,6 +95,11 @@ static inline struct shmem_inode_info *SHMEM_I(struct inode *inode)
 	return container_of(inode, struct shmem_inode_info, vfs_inode);
 }
 
+static inline struct shmem_sb_info *SHMEM_SB(struct super_block *sb)
+{
+	return sb->s_fs_info;
+}
+
 /*
  * Functions in mm/shmem.c called directly from elsewhere:
  */
mm/memfd_luo.c +235 -3

264 lines not on this page · raw message

mm/shmem.c +0 -5
index 8660405b6ab8..c76dd3646f6f 100644
@@ -162,11 +162,6 @@ static int shmem_swapin_folio(struct inode *inode, pgoff_t index,
 			struct folio **foliop, enum sgp_type sgp, gfp_t gfp,
 			struct vm_fault *vmf, vm_fault_t *fault_type);
 
-static inline struct shmem_sb_info *SHMEM_SB(struct super_block *sb)
-{
-	return sb->s_fs_info;
-}
-
 /*
  * shmem_file_setup pre-accounts the whole fixed size of a VM object,
  * for shared memory and for shared anonymous (/dev/zero) mappings
Signature · 1 line
2.56.0.rc1.310.g51773c2048-goog
#9 lore raw

Re: [RFC PATCH 4/6] mm/memfd_luo: allow preserving a tmpfs mount

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [Medium] mm: tmpfs LUO drops group quotas due to incomplete USRQUOTA check - [Medium] mm: TOCTOU race…

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [Medium] mm: tmpfs LUO drops group quotas due to incomplete USRQUOTA check
- [Medium] mm: TOCTOU race allows unsupported ID mappings and quotas to bypass LUO validation --

commit 20e62ff0b162f0ffc76cd0d972cc6c760a83d003
Author: Pratyush Yadav (Google) <[email protected]>

mm/memfd_luo: allow preserving a tmpfs mount

This commit adds support for preserving a tmpfs mount across a live update. It preserves the filesystem block limit and root directory mode, while resetting other options to default, identifying the mount via a file pointing to it.

Quoted 3 lines
mm/memfd_luo.c +0 -0

[ ... ]

Quoted 3 lines
mm/memfd_luo.c +3 -0
+static bool tmpfs_luo_mnt_can_preserve(struct liveupdate_file_handler *fh,
+				       struct file *file)
+{

[ ... ]

mm/memfd_luo.c static struct liveupdate_file_handler memfd_luo_handler = { · line 702

Quoted 4 lines
mm/memfd_luo.c +4 -0
+	/* These features are not supported. */
+	if (SHMEM_SB(sb)->mpol || sb_has_quota_active(sb, USRQUOTA) ||
+	    sb_has_encoding(sb) || is_idmapped_mnt(mnt))
+		return false;

[Severity: Medium]
Does this condition improperly accept tmpfs mounts with group or project quotas active?

The check only tests for USRQUOTA, which means mounts with grpquota or prjquota will bypass this rejection and their quota state will be silently lost across kexec.

Should this use sb_any_quota_loaded(sb) to properly detect all quota types?

[ ... ]

mm/memfd_luo.c static struct liveupdate_file_handler memfd_luo_handler = { · line 740

Quoted 12 lines
mm/memfd_luo.c +12 -0
+static int tmpfs_luo_mnt_freeze(struct liveupdate_file_op_args *args)
+{
+	struct super_block *sb = args->file->f_path.mnt->mnt_sb;
+	struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
+	struct tmpfs_luo_mnt_ser *ser;
+
+	/*
+	 * A remount can install a memory policy after can_preserve() accepted
+	 * the mount.
+	 */
+	if (sbinfo->mpol)
+		return -EOPNOTSUPP;

[Severity: Medium]
Does this leave a TOCTOU window for ID mappings and quotas?

While mpol is re-validated here to handle concurrent remounts, checks for is_idmapped_mnt() and quotas are missing. If a user concurrently adds an ID mapping via mount_setattr() or enables quotas via remount after the tmpfs_luo_mnt_can_preserve() check passes, those unsupported properties will bypass validation and be silently dropped across kexec.

Should this re-validate the ID mapping and quota state as well?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4
author maintainer
#10 lore raw

RFC PATCH 5/6 mm/memfd_luo: allow preserving a tmpfs file

In reply to Pratyush Yadav

From: "Pratyush Yadav (Google)" <[email protected]>

A memfd can be preserved across a live update, but it has no name, so anything that needs a filesystem path cannot use one.

The main use case is to fetch VMM binaries that need to be kept on the host memory during a live update to avoid the latency of fetching them from network. The host does not have any attached local storage to store the binaries.

Allow preserving files in a tmpfs mount. Only regular files in the mount root are supported to keep things simple. Files in subdirectories will be rejected. The mount must be preserved before the files because the file takes a dependency on the mount. Restoration can be done in any order.

Files are allowed to be renamed after preservation, but they can not be unlinked. Unlinked files will cause the freeze() callback to fail and result in a failed live update.

Re-use the memfd preservation logic and ABI since it already knows how to preserve a shmem file. The same restrictions around file resizing apply.

---

The usage of liveupdate_get_file_incoming() makes lockdep scream. The locking itself is fine, but two locks of the same class are taken. Likely need some annotation in liveupdate_get_file_incoming(). That is separate work and not done by this series.

---
 include/linux/kho/abi/tmpfs.h |  35 ++++
 mm/memfd_luo.c                | 342 ++++++++++++++++++++++++++++++++--
 2 files changed, 366 insertions(+), 11 deletions(-)
include/linux/kho/abi/tmpfs.h +35 -0
index bed41048a19b..cd47e2eb5041 100644
@@ -19,6 +19,11 @@
  * tmpfs uses the ABI defined below for preserving a mount and the regular
  * files in it across a kexec reboot using the LUO.
  *
+ * Regular files in the root of such a mount are preserved individually into
+ * `struct tmpfs_luo_file_ser` and reference their mount by its LUO token. Only
+ * the folios holding the file contents are true handover payload; they use the
+ * memfd ABI (`struct memfd_luo_folio_ser`) unchanged.
+ *
  * The mount metadata is preserved via `tmpfs_luo_mnt_ser`.
  *
  * This interface is a contract. Any modification to the structure layout
@@ -42,7 +47,37 @@ struct tmpfs_luo_mnt_ser {
 	u32 flags;
 } __packed;
 
+/**
+ * struct tmpfs_luo_file_ser - Serialized state of a preserved tmpfs file.
+ * @mnt_token: The LUO token of the tmpfs mount this file lives in.
+ * @pos:       The file's current position (f_pos).
+ * @size:      The total size of the file in bytes (i_size).
+ * @mode:      The permission bits of the file (i_mode). The file type is
+ *             always S_IFREG.
+ * @flags:     Flags for the file. Unused flag bits must be set to 0.
+ * @nr_folios: Number of folios in the folios array.
+ * @folios:    KHO vmalloc descriptor pointing to the array of
+ *             struct memfd_luo_folio_ser.
+ * @name:      The NUL-terminated name of the file in the root of the mount,
+ *             This is a single path component: it never contains '/', is never
+ *             "." or "..", and all bytes after the terminator must be 0.
+ */
+struct tmpfs_luo_file_ser {
+	u64 mnt_token;
+	u64 pos;
+	u64 size;
+	u32 mode;
+	u32 flags;
+	u64 nr_folios;
+	struct kho_vmalloc folios;
+	/* NAME_MAX is uAPI. */
+	char name[NAME_MAX + 1];
+} __packed;
+
 /* The compatibility string for the tmpfs mount file handler */
 #define TMPFS_LUO_MNT_FH_COMPATIBLE	"tmpfs-mnt-v1"
 
+/* The compatibility string for the tmpfs file handler */
+#define TMPFS_LUO_FILE_FH_COMPATIBLE	"tmpfs-file-v1"
+
 #endif /* _LINUX_KHO_ABI_TMPFS_H */
mm/memfd_luo.c +331 -11

395 lines not on this page · raw message

Signature · 1 line
2.56.0.rc1.310.g51773c2048-goog
#11 lore raw

Re: [RFC PATCH 5/6] mm/memfd_luo: allow preserving a tmpfs file

Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider: - [High] mm: restored file permission bits are corrupted by current_umask() - [High] mm: TOCTOU race allows…

Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider:
- [High] mm: restored file permission bits are corrupted by current_umask()
- [High] mm: TOCTOU race allows unprivileged userspace to disrupt tmpfs file retrieve
- [High] mm: memory leak of KHO folios on tmpfs_luo_file_retrieve failure
- [Medium] mm: potential double free / UAF of ser and folios on successful retrieve --

commit 4087da995ba7fe7684120abaf9ec59deb366b97b
Author: Pratyush Yadav (Google) <[email protected]>

mm/memfd_luo: allow preserving a tmpfs file

This commit introduces tmpfs file preservation across kexec live updates. It preserves regular files within a tmpfs mount by serializing their properties and memory folios, and restores them when the mount is retrieved.

Quoted 4 lines
mm/memfd_luo.c +0 -0
@@ -1005,62 +1005,51 @@ static int tmpfs_luo_file_retrieve(struct liveupdate_file_op_args *args)

[ ... ]

Quoted 6 lines
	err = liveupdate_get_file_incoming(args->session, ser->mnt_token,
					   &mnt_file);
	if (err) {
		pr_err("failed to retrieve tmpfs mount: %pe\n", ERR_PTR(err));
		goto free_ser;
	}

[Severity: High]
If this path jumps to the free_ser error label, does it permanently leak the physical memory reserved for the KHO folios?

The free_ser error label frees the ser metadata, but does not discard the physical folios or the ser->folios vmalloc region. Because the function returns an error, tmpfs_luo_file_finish() may skip its cleanup logic if args->retrieve_status is set to a non-zero error code. This would permanently leak the memory of all files preserved inside that mount.

[ ... ]

Quoted 9 lines

/*
* TODO: This is racy. This will link the file in the mount so after
* this call userspace can already open the file and write to it.
* Ideally we should first create the inode and set it up, and only
* then link it to the root.
*/
file = file_open_root(&mnt_file->f_path, ser->name,
O_RDWR | O_CREAT | O_EXCL | O_LARGEFILE, ser->mode);

[Severity: High]
Does this code inadvertently strip the file permissions via the orchestrator's umask?

VFS creation functions like file_open_root(O_CREAT) will apply ~current_umask() to the newly created file, which could alter the preserved ser->mode. Since the kernel code lacks a subsequent chmod() or vfs_setattr() step to enforce the exact preserved mode, the restored file might have incorrect permission bits.

[Severity: High]
Does linking the file into the namespace here open a TOCTOU race window?

Since file_open_root() immediately links the restored file into the tmpfs namespace, unprivileged userspace could potentially open and write or mmap the file before memfd_luo_retrieve_folios() is called. This would instantiate page cache folios, causing the subsequent shmem_add_to_page_cache() inside memfd_luo_retrieve_folios() to hit an -EEXIST collision and permanently drop the preserved data folios.

[ ... ]

Quoted 4 lines
	args->file = file;
	kho_restore_free(ser);

	return 0;

[Severity: Medium]
Could this lead to a double-free or use-after-free regression of the KHO folios on a successful retrieve?

This code frees the ser object but leaves args->serialized_data as a dangling pointer.

[ ... ]

Quoted 12 lines

static void tmpfs_luo_file_finish(struct liveupdate_file_op_args *args)

{
	struct memfd_luo_folio_ser *folios_ser;
	struct tmpfs_luo_file_ser *ser;

	/*
	 * A successful retrieve() already consumed the preserved memory, and a
	 * failed one cleaned up what it could. Only a file that was never
	 * retrieved is left to discard here.
	 */
	if (args->retrieve_status || !args->serialized_data)
		return;

[Severity: Medium]
If the LUO core framework sets args->retrieve_status to 0 upon a successful return, tmpfs_luo_file_finish() might bypass this early return, dereference the dangling args->serialized_data pointer, and mistakenly double-free the folios. Could args->serialized_data be explicitly cleared after it is freed to prevent this?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=5
author maintainer
#12 lore raw

RFC PATCH 6/6 selftests/liveupdate: add tmpfs kexec test

In reply to Pratyush Yadav

From: "Pratyush Yadav (Google)" <[email protected]>

Add a two-stage kexec test for tmpfs preservation, in the same shape as luo_kexec_simple.

The first stage create a tmpfs mount, creates a file in it, write some data to it, and preserves both. In the second stage, it retrieves both and verifies the contents of the file. It also verifies it can be reached by a filesystem path.

Add the test to run_vmtests.sh to allow automated runs.

---
 tools/testing/selftests/liveupdate/Makefile   |   1 +
 .../selftests/liveupdate/luo_kexec_tmpfs.c    | 191 ++++++++++++++++++
 .../selftests/liveupdate/run-vmtests.sh       |   1 +
 3 files changed, 193 insertions(+)
 create mode 100644 tools/testing/selftests/liveupdate/luo_kexec_tmpfs.c
tools/testing/selftests/liveupdate/Makefile +1 -0
index 634211c66652..c35aee04b4a3 100644
@@ -3,6 +3,7 @@
 TEST_GEN_PROGS += liveupdate
 
 TEST_GEN_PROGS_EXTENDED += luo_kexec_simple
+TEST_GEN_PROGS_EXTENDED += luo_kexec_tmpfs
 TEST_GEN_PROGS_EXTENDED += luo_multi_session
 TEST_GEN_PROGS_EXTENDED += luo_stress_sessions
 TEST_GEN_PROGS_EXTENDED += luo_stress_files
tools/testing/selftests/liveupdate/luo_kexec_tmpfs.c new file +191 -0

191 lines not on this page · raw message

tools/testing/selftests/liveupdate/run-vmtests.sh +1 -0
index d656ce58c5a7..624d1916eeda 100755
@@ -17,6 +17,7 @@ skipped=0
 
 TEST_NAMES=(
 	"luo_kexec_simple"
+	"luo_kexec_tmpfs"
 	"luo_multi_session"
 	"luo_stress_files"
 	"luo_stress_sessions"
Signature · 1 line
2.56.0.rc1.310.g51773c2048-goog
#13 lore raw

Re: [RFC PATCH 6/6] selftests/liveupdate: add tmpfs kexec test

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] selftests/liveupdate: luo_kexec_tmpfs fails on persistent rootfs --

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] selftests/liveupdate: luo_kexec_tmpfs fails on persistent rootfs --

commit 656981de91db4b31da49ddcf77d65f446841090b
Author: Pratyush Yadav (Google) <[email protected]>

selftests/liveupdate: add tmpfs kexec test

This commit adds a two-stage kexec test for tmpfs preservation. The first stage creates a tmpfs mount, creates a file, and preserves both. The second stage retrieves both and verifies the contents.

Quoted 5 lines
tools/testing/selftests/liveupdate/luo_kexec_tmpfs.c new file +0 -0
new file mode 100644
index 0000000000000..608a6eddbd7ba

[ ... ]

Quoted 3 lines
tools/testing/selftests/liveupdate/luo_kexec_tmpfs.c new file +3 -0
+/* Stage 1: Executed before the kexec reboot. */
+static void run_stage_1(int luo_fd)
+{

[ ... ]

tools/testing/selftests/liveupdate/luo_kexec_tmpfs.c line 91

Quoted 3 lines
tools/testing/selftests/liveupdate/luo_kexec_tmpfs.c new file +3 -0
+	ksft_print_msg("[STAGE 1] Mounting tmpfs at %s...\n", TMPFS_DIR);
+	if (mkdir(TMPFS_DIR, 0755) < 0)
+		fail_exit("mkdir %s", TMPFS_DIR);

[Severity: Medium]
Will this fail on test re-runs if the directory is left behind from a prior run?

Quoted 2 lines
tools/testing/selftests/liveupdate/luo_kexec_tmpfs.c new file +2 -0
+	if (mount("tmpfs", TMPFS_DIR, "tmpfs", 0, NULL) < 0)
+		fail_exit("mount tmpfs at %s", TMPFS_DIR);

[ ... ]

Quoted 3 lines
tools/testing/selftests/liveupdate/luo_kexec_tmpfs.c new file +3 -0
+/* Stage 2: Executed after the kexec reboot. */
+static void run_stage_2(int luo_fd, int state_session_fd)
+{

[ ... ]

tools/testing/selftests/liveupdate/luo_kexec_tmpfs.c line 151

Quoted 4 lines
tools/testing/selftests/liveupdate/luo_kexec_tmpfs.c new file +4 -0
+	ksft_print_msg("[STAGE 2] Attaching the restored mount at %s...\n",
+		       TMPFS_DIR);
+	if (mkdir(TMPFS_DIR, 0755) < 0)
+		fail_exit("mkdir %s", TMPFS_DIR);

[Severity: Medium]
Will this unconditionally fail with EEXIST in run_stage_2() if the underlying root filesystem is persistent across kexec?

Quoted 3 lines
tools/testing/selftests/liveupdate/luo_kexec_tmpfs.c new file +3 -0
+	if (move_mount_empty_from(mnt_fd, TMPFS_DIR) < 0)
+		fail_exit("move_mount of the restored mount to %s", TMPFS_DIR);
+	close(mnt_fd);

[ ... ]

tools/testing/selftests/liveupdate/luo_kexec_tmpfs.c line 185

Quoted 11 lines
tools/testing/selftests/liveupdate/luo_kexec_tmpfs.c new file +11 -0
+	ksft_print_msg("[STAGE 2] Test data verified successfully.\n");
+	if (luo_session_finish(session_fd) < 0)
+		fail_exit("luo_session_finish for test session");
+	close(session_fd);
+
+	if (luo_session_finish(state_session_fd) < 0)
+		fail_exit("luo_session_finish for state session");
+	close(state_session_fd);
+
+	ksft_print_msg("\n--- TMPFS KEXEC TEST PASSED ---\n");
+}

[Severity: Medium]
Does the test exit successfully here without calling umount() or rmdir() to clean up the environment? If so, does it leak the /tmpfs directory and its mount on the host system?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=6
#14 lore raw
In reply to Pratyush Yadav
Quoted 34 lines · Pratyush Yadav

From: "Pratyush Yadav (Google)" <[email protected]>

I brought this idea up in this week's Hypervisor Live Update bi-weekly. I decided to try using an LLM to see if it can produce a proof-of-concept quickly. This series is the end result.

The main use case is preserving in-memory files that have a filesystem path. We already support memfd preservation, but memfds can't be linked to a (user-visible) filesystem. This is needed for storing VMM packages for live update on hosts that don't have a disk. A cold boot fetches the binaries from network, but that is too slow for a live update.

David tried to solve the problem by introducing LIVEUPDATE_SESSION_RETRIEVE_INTO_FD [0], which lets you provide a FD for LUO to retrieve into. This is an alternative to the idea. It uses the standard preservation and retrieval API that LUO already provides.

The core idea is to allow userspace to preserve a tmpfs mount FD. Once the mount is preserved, userspace can pass in regular files in that mount for preservation. The files take a dependency on the mount token, and that is used for retrieving the files in the right mount. This saves us from doing a full FS preservation and makes preservation of each file explicit.

Currently only files in the root are supported. Files in subdirectories will be rejected. This is mainly for simplicity. Complex mount features like memory policies, id mappings, or casefolding are also not supported. All these can be reconfigured after retrieve if really needed.

The code re-uses a lot of the preservation and retrieval logic from memfd preservation. It only adds some extra file and mount metadata on top.

Quoted 6 lines

As I mentioned earlier, this is heavily LLM generated. The code is not very polished and has some rough edges. That said, I have read all the code and did significant cleanups of the LLM output. This includes turning the 1600 or so lines it generated to a more modest 977 lines. So (I think) it isn't complete AI garbage. And I think it does get the core idea across.

Hi Pratyush,

Thanks for posting this. I wanted to compare this series against the RETRIEVE_INTO_FD approach, since both are trying to solve the same
problem: preserving files with filesystem paths across a Live Update. Both are RFCs, so I'm going to ignore implementation details and focus on the design.

As we discussed at the Hypervisor Live Update bi-weekly, this will be a topic to discuss at LPC. I'm hoping we can use this thread to align on the pros and cons of the two approaches so I don't unintentionally mischaracterize anything.

At the memory level the two approaches are the same: file contents are handed over using the existing memfd folio ABI and re-inserted into a shmem inode in the new kernel. The difference is who owns the filesystem topology around those folios.

- RETRIEVE_INTO_FD: The kernel preserves only memory. Userspace own the fileystem topology, recreates it after kexec with normal syscalls, and then asks LUO to fill each empty file with its preserved contents.

- tmpfs handlers (this series): The kernel preserves the mount and the files as first-class LUO objects (name, mode, pos, etc.), rebuilds them after kexec, and hands back a detached mount.

Everything else below follows from that difference.

Comparison
==========

What LUO preserves:
RETRIEVE_INTO_FD: Memory only.
tmpfs handlers: Memory plus filesystem objects and metadata.

New uAPI:
RETRIEVE_INTO_FD: One new generic ioctl, or a generic extension to
the existing RETRIEVE ioctl.
tmpfs handlers: None. Existing PRESERVE/RETRIEVE with new file
types.

New LUO ABI:
RETRIEVE_INTO_FD: None
tmpfs handlers: Mount and file structs, plus a version bump for
every new attribute added in the future.

New VFS surface:
RETRIEVE_INTO_FD: None.
tmpfs handlers: Kernel-created mounts handed to userspace as
detached mounts.

Where the filesystem topology description lives:
RETRIEVE_INTO_FD: Userspace, in a private format it can evolve
freely.
tmpfs handlers: Kernel ABI.

Mount options:
RETRIEVE_INTO_FD: Anything userspace can pass to mount.
tmpfs handlers: Only what the ABI enumerates (currently the block
limit and root mode).

Dirs, hardlinks, symlinks, ownership, xattrs:
RETRIEVE_INTO_FD: Free. Userspace recreates them with normal
syscalls.
tmpfs handlers: Each requires new kernel code and ABI.

Userspace complexity:
RETRIEVE_INTO_FD: Higher. Userspace needs a manifest, then must
mkdir, create, chown, etc. before calling retrieve_into.
tmpfs handlers: Low. Preserve the mount and files, retrieve them,
and move_mount().

Metadata consistency:
RETRIEVE_INTO_FD: Userspace must snapshot and keep its manifest
consistent with what it preserved.
tmpfs handlers: The kernel captures metadata at freeze time,
atomically with the contents.

Trust surface for previous-kernel data:
RETRIEVE_INTO_FD: Userspace validates its own manifest. The kernel
only validates the folio list.
tmpfs handlers: The kernel must validate names, modes, etc.

Generality:
RETRIEVE_INTO_FD: Applies to any handler where "fill a
userspace-created object" makes sense.
tmpfs handlers: tmpfs only.

Arguments for RETRIEVE_INTO_FD
==============================

1. It follows the principle that LUO should only preserve what userspace cannot recreate. Names, modes, ownership, directories, and mount options can all be recreated by userspace. Memory contents cannot. This approach draws the line exactly there.

2. The kernel ABI stays small. Every attribute the tmpfs handlers learn to preserve becomes a stable KHO ABI that has to be maintained across kernel versions. Reaching parity with what users will eventually want (subdirectories, ownership, xattrs, ACLs, symlinks, hardlinks, huge=, mpol, quotas, idmaps) implies a long series of ABI revisions. RETRIEVE_INTO_FD gets all of that for free via existing syscalls.

3. Policy stays in userspace. The target file lives on a mount that userspace configured, in a cgroup and namespace of its choosing. The kernel never has to guess the right configuration for a recreated object.

4. It is a reusable LUO primitive rather than a tmpfs feature. "Retrieve into an object that userspace already set up" could be useful for other handlers where the object must be created in a specific context, e.g. HugeTLBfs files come to mind. The tmpfs series adds a one-off pair of handlers.

5. It requires no new VFS surface. There are no kernel-created mounts being handed to userspace, so the change stays contained to LUO and memfd, which lowers the cost of getting it upstream.

Arguments for the tmpfs handlers
================================

1. It gives a filesystem-shaped abstraction from the kernel's side. The kernel knows that the preserved files belong to a mount, tracks that dependency, and returns a working filesystem. This maps directly onto the stated use case of VMM binaries on hosts without local storage.

2. Metadata is captured atomically. Name, mode, size, and pos are captured at freeze time alongside the contents, so they cannot drift. With RETRIEVE_INTO_FD, userspace must keep its manifest correct across any renames or chmods that happen after it takes its snapshot.

3. The restored mount can be hidden until it is ready. The mount comes back detached, and userspace chooses when and where to attach it with move_mount(). Nobody can observe a half-built tree.

4. There is less for userspace to do. For the simple case of a single tmpfs mount with a few files, userspace needs very little new code.

5. It leaves the door open for whole-tree preservation. In the future the kernel could preserve an entire mount with a single token by walking the tree, which RETRIEVE_INTO_FD cannot express.

Where each approach hurts
=========================

RETRIEVE_INTO_FD pushes work and correctness onto userspace, likely requiring a shared userspace library or daemon. The kernel also has no notion that a set of tokens formed a single filesystem. It just sees unrelated files.

The tmpfs handlers are narrow in scope today, and every extension is expensive. There is a real risk of slowly growing a partial filesystem serializer in the kernel. It also couples LUO to VFS concepts such as mounts, namespaces, and names.

A possible middle ground
========================

The two approaches are not mutually exclusive. We could use RETRIEVE_INTO_FD as the core primitive for file contents, and provide a shared userspace library for the common "rebuild this tmpfs" case. Kernel-side mount preservation could then be added later, if and when a concrete need comes up that userspace cannot meet, such as an atomic snapshot or availability before userspace runs.

That keeps the kernel ABI limited to memory, which is the part only the kernel can preserve, while leaving room for the more integrated model if it turns out to be needed.

Thanks,
David