liveupdate: Preserve tmpfs files across Live Update
Subsystems (8)
v3 cover liveupdate: Preserve tmpfs files across Live Update
This series can be found on GitHub:
https://github.com/dmatlack/linux/tree/liveupdate/tmpfs/v3
This series allows userspace to preserve files on tmpfs mounts across a Live Update, in addition to memfds. Today the memfd file handler only accepts unlinked shmem files, and LIVEUPDATE_SESSION_RETRIEVE_FD always restores them as anonymous memfds. Userspace therefore has to put every in-memory file it wants to preserve in a memfd, which works for VM guest memory but not for other files that live on tmpfs mounts, such as in-memory logs, binaries, and configuration files.
The kernel preserves the contents, size, file position and seals of each file. After the Live Update, userspace retrieves each file onto a tmpfs mount of its choice and links it at the path of its choice, so userspace decides the directory structure, names, owners and permissions of the restored files.
This series builds on the per-file-handler argument for LIVEUPDATE_SESSION_PRESERVE_FD and LIVEUPDATE_SESSION_RETRIEVE_FD from Pasha's v1 [1], following Pratyush's suggestion on the RFC [2], and on the design discussion on Pasha's v1 [3].
[1] https://lore.kernel.org/kexec/[email protected]/
[2] https://lore.kernel.org/kexec/[email protected]/
[3] https://lore.kernel.org/kexec/[email protected]/
Series Overview
---------------
This series implements the following:
1. Let userspace pass a file-handler-specific argument to LIVEUPDATE_SESSION_PRESERVE_FD and LIVEUPDATE_SESSION_RETRIEVE_FD, and let file handlers mark retrieve failures as retryable, so that userspace can fix a bad argument and try again without losing the preserved file (patches 1-3).
2. Rename the memfd file handler (memfd_luo) to shmem_luo, since it preserves shmem files, which will include files on tmpfs mounts. Preserving memfds created with MFD_HUGETLB will need a separate handler, so naming the handler after the filesystem avoids another rename later. The compatible string stays "memfd-v2" (patch 4).
3. Fix two existing problems in the retrieve path: a WARN_ON() that userspace can trigger when tracking a retrieved file fails, and a leak of the preserved folios when a retrieve fails before it restores them (patches 5-6).
4. Retrieve files that were preserved from a tmpfs mount onto a tmpfs mount chosen by userspace, via the new struct liveupdate_shmem_retrieve_arg. The kernel creates the file as if by openat(dirfd, ".", O_TMPFILE | flags, mode), so the file gets the same flags, owner, permission checks and security labels as any other new file in that directory, and is charged to that mount. The kernel fills the file before returning it, so nothing else can access the file before it is complete, and userspace links it at the path it wants with linkat() (patch 7).
5. Allow preserving tmpfs files that are still linked, and require permission to change the size of a file to preserve it, since preserving a file freezes its size for all users of the file (patch 8).
6. Allow retrieving tmpfs files with O_RDONLY so that restored binaries can be executed before the session is finished (patch 9).
7. Add a kexec selftest for preserving and retrieving tmpfs files
(patch 10).
uAPI Changes
------------
- struct liveupdate_session_preserve_fd and struct liveupdate_session_retrieve_fd gain a 64-bit @arg field, a pointer to a handler-specific argument that starts with a 32-bit size field. Callers that pass the old struct size are not affected.
- Passing a non-zero @arg when retrieving a token that was already retrieved fails with -EBUSY instead of silently ignoring @arg.
- New struct liveupdate_shmem_retrieve_arg, with @dirfd, @flags and @mode, for retrieving tmpfs files. Files that were preserved from a tmpfs mount can only be retrieved with it, and memfds can only be retrieved without it. Unlinked tmpfs files (e.g. created with O_TMPFILE) were retrieved as memfds before this series, and now require the argument too.
- Preserving a file now requires permission to change its size (see Open Questions).
ABI Changes
-----------
- struct shmem_luo_ser (formerly struct memfd_luo_ser) gains a SHMEM_LUO_FLAG_TMPFS flag in its existing @flags field, which records that a file was on a tmpfs mount rather than the internal shmem mount that memfds use.
Open Questions
--------------
- Preserving a file freezes its size until it is unpreserved: For all users of the file, truncating it, fallocate() and writes past its end fail with -EPERM, and the owner of the file can't undo that. Linked files can be reached by anyone who can read them, so without a permission check, anyone with a session fd could preserve e.g. a world-readable file in /dev/shm through a read-only fd and break its owner. Patch 8 therefore requires permission to write to the file (inode_permission(MAY_WRITE), but neither a file opened for writing nor a writable mount), or ownership if the file is immutable or on a read-only superblock, since its size can't change anyway. Like FS_IOC_ENABLE_VERITY, it also requires the file to be opened for reading and rejects append-only files. The checks apply to all files, including memfds, where they pass in practice since memfds are created with mode 0777 (or 0666). Is this the right permission model for preserving files? Should this apply all files or just shmem files?
- Is patch 9 (retrieving tmpfs files with O_RDONLY) worth merging? Without it, userspace can only retrieve files O_RDWR or O_WRONLY which then LUO keeps open until the session is finished, so executing a restored binary fails with -ETXTBSY until then. Supporting O_RDONLY retrieve is fairly simple so I think it is worth supporting.
Merge Plan
----------
This series applies on top of the next branch of liveupdate.git (commit 27be83a12b24 ("Merge branch 'lu-pci-preservation' into next")) and is targeting Linux 7.5.
The series will be merged through liveupdate.git.
This series conflicts with Pratyush's "mm: allow freezing mappings" series [4], which also changes mm/memfd_luo.c and replaces shmem_freeze() with mapping_set_frozen(). Whichever series lands second will need to be rebased. Pasha suggested keeping retrieved files frozen until the session is finished [5]. That builds on AS_FROZEN, so I think it fits better in Pratyush's series.
[4] https://lore.kernel.org/kexec/[email protected]/
[5] https://lore.kernel.org/kexec/179322496574.1894369.544573858973645958.b4-review@b4/
Testing
-------
All patches were build-tested individually. The series was tested on x86_64 and arm64 in QEMU using the Live Update selftests:
- luo_tmpfs (new): 33 test cases covering the contents, size, position, seals and holes of restored files; creating the file like open(O_TMPFILE), with the requested flags and mode, the caller as owner, and O_EXCL; executing restored binaries before and after the session is finished; preserving files through read-only fds, on read-only mounts and superblocks, and immutable files; rejecting files that the caller can't write to, write-only fds and append-only files; block accounting on the target mount, including large folios that extend past the end of the file; mount and memcg limits; memfds; invalid arguments; retrieving a token twice; and that no kernel warnings are triggered.
The main downside of this new test is that it is large (2k lines). It was trivial to generate with AI, but can understand if it is too large to review. I can look for ways to simplify it.
- luo_kexec_simple, luo_multi_session, luo_stress_files and luo_stress_sessions.
- liveupdate (including the new preserve_memfd_with_arg test).
The selftests were run with vmtest.sh:
$ tools/testing/selftests/liveupdate/vmtest.sh -T luo_tmpfs
luo_tmpfs was also run on bare-metal x86_64.
Changelog
---------
v3:
- Rebase onto liveupdate/next.
- Collect Acked-by tags from Pasha on patches 4-6.
- Drop the Assisted-by tags (Pasha).
- Explain in patch 6 why it has no Fixes tag (Pasha).
- Define shmem_inode_on_internal_mount() under CONFIG_SHMEM, like the other shmem helpers declared in mm/internal.h, to fix a -Wmissing-prototypes warning with CONFIG_SHMEM=n (kernel test robot).
- Require a file to be opened for reading to preserve it, and reject append-only files (Sashiko).
- Reopen files retrieved with O_RDONLY with the flags of the writable file, which file_open_root() normalized, e.g. by dropping O_CLOEXEC (Sashiko).
v2: https://lore.kernel.org/kexec/[email protected]/
v1: https://lore.kernel.org/kexec/[email protected]/
rfc: https://lore.kernel.org/kexec/[email protected]/
David Matlack (7):
mm: Rename memfd_luo to shmem_luo
liveupdate: Don't WARN if tracking a retrieved file fails mm/shmem_luo: Don't make early retrieve failures permanent mm/shmem_luo: Retrieve tmpfs files onto a tmpfs mount chosen by
userspace
mm/shmem_luo: Allow preserving tmpfs files that are still linked mm/shmem_luo: Allow retrieving tmpfs files with O_RDONLY selftests/liveupdate: Add a test for preserving tmpfs files
Pasha Tatashin (3):
liveupdate: Pass user argument to file preserve and retrieve
selftests: liveupdate: Add tests for preserve and retrieve user
argument
liveupdate: Allow file handlers to mark retrieve failures as retryable
Documentation/core-api/liveupdate.rst | 2 +-
Documentation/mm/index.rst | 2 +-
Documentation/mm/memfd_preservation.rst | 23 -
Documentation/mm/shmem_preservation.rst | 23 +
MAINTAINERS | 4 +-
include/linux/kho/abi/{memfd.h => shmem.h} | 56 +-
include/linux/liveupdate.h | 55 +-
include/uapi/linux/liveupdate.h | 52 +-
kernel/liveupdate/Kconfig | 8 +-
kernel/liveupdate/luo_file.c | 110 +-
kernel/liveupdate/luo_internal.h | 5 +-
kernel/liveupdate/luo_session.c | 6 +-
mm/Makefile | 2 +-
mm/internal.h | 1 +
mm/memfd_luo.c | 621 -----
mm/shmem.c | 9 +
mm/shmem_luo.c | 834 +++++++
tools/testing/selftests/liveupdate/Makefile | 1 +
tools/testing/selftests/liveupdate/config | 4 +
.../liveupdate/lib/include/libliveupdate.h | 3 +
.../selftests/liveupdate/lib/lu_utils.c | 17 +-
.../testing/selftests/liveupdate/liveupdate.c | 44 +
.../testing/selftests/liveupdate/luo_tmpfs.c | 2055 +++++++++++++++++
.../selftests/liveupdate/run-vmtests.sh | 1 +
24 files changed, 3237 insertions(+), 701 deletions(-)
delete mode 100644 Documentation/mm/memfd_preservation.rst
create mode 100644 Documentation/mm/shmem_preservation.rst
rename include/linux/kho/abi/{memfd.h => shmem.h} (54%)
delete mode 100644 mm/memfd_luo.c
create mode 100644 mm/shmem_luo.c
create mode 100644 tools/testing/selftests/liveupdate/luo_tmpfs.cbase-commit: 27be83a12b246b6b3783e7d3ac5c378ff7520f25
Signature · 1 line
2.56.0.385.gac671c819a-goog
PATCH v3 1/10 liveupdate: Pass user argument to file preserve and retrieve
From: Pasha Tatashin <[email protected]>
Allow userspace to pass a file-handler-specific argument structure when preserving or retrieving an FD via LUO. File handlers may need additional context during preservation or retrieval, such as a target FD, device location, or handler-specific parameters, without introducing separate ioctls for each file type. Whether the argument is required, optional, or unused is defined by the file handler.
Extend struct liveupdate_session_preserve_fd and
struct liveupdate_session_retrieve_fd with a 64-bit arg field pointing to a handler-defined argument structure that begins with a 32-bit size field. Because IOCTL_OP validates a minimum size up to the token field, existing callers passing the 16-byte struct remain compatible and have arg zero-filled by copy_struct_from_user().
Add parg and rarg (struct liveupdate_arg_spec) to
struct liveupdate_file_ops, initialized via LIVEUPDATE_ARG(_struct, _min_last), which records the full struct size and minimum required size while enforcing at compile time that handler argument structures begin with a 32-bit size field. When arg is non-zero, LUO validates the minimum size and copies the user argument via copy_struct_from_user() up to the handler's expected size and passes the kernel copy in struct liveupdate_file_op_args, or returns -EOPNOTSUPP if the handler does not accept an argument.
The argument only applies to the retrieve that restores the file. Retrieving a token that was already retrieved returns the existing file, so fail with -EBUSY if a non-zero argument is passed then, instead of silently ignoring it.
--- include/linux/liveupdate.h | 44 +++++++++++++++++++ include/uapi/linux/liveupdate.h | 10 +++++ kernel/liveupdate/luo_file.c | 73 +++++++++++++++++++++++++++++--- kernel/liveupdate/luo_internal.h | 5 ++- kernel/liveupdate/luo_session.c | 6 ++- 5 files changed, 128 insertions(+), 10 deletions(-)
include/linux/liveupdate.h +44 -0
@@ -22,6 +22,36 @@ struct liveupdate_flb;struct liveupdate_session ;struct file ; +/** +* struct liveupdate_arg_spec - Specification of a file handler user argument. +* @size: Full size of the argument structure in the current kernel. +* @min_size: Minimum valid user-supplied size (offsetofend of the last +* required field). +*/ +struct liveupdate_arg_spec { +size_t size; +size_t min_size; +}; + +/** +* LIVEUPDATE_ARG - Validated specification of a file handler argument struct. +* @_struct: The argument structure type. +* @_min_last: The last required member in @_struct. +* +* All file handler argument structures passed via +* &liveupdate_session_preserve_fd.arg or &liveupdate_session_retrieve_fd.arg +* must begin with a 32-bit @size field so they can be extended compatibly. +* Use this macro to initialize &liveupdate_file_ops.parg and +* &liveupdate_file_ops.rarg. +*/ +#define LIVEUPDATE_ARG (_struct, _min_last) \ + { \ + .size = sizeof(_struct) + \ + BUILD_BUG_ON_ZERO(offsetof(_struct, size) != 0) + \ + BUILD_BUG_ON_ZERO(sizeof_field(_struct, size) != sizeof(u32)), \ + .min_size = offsetofend(_struct, _min_last), \ + } +/** * struct liveupdate_file_op_args - Arguments for file operation callbacks. * @handler: The file handler being called.
@@ -40,6 +70,11 @@ struct file;* is not preserved. Set by the handler's .preserve() * callback, and must be freed in the handler's * .unpreserve() callback. +* @arg: Kernel copy of the user-provided argument for .preserve() +* and .retrieve(), or %NULL if none was provided. Allocated +* to the handler's expected argument size with any trailing +* bytes beyond the user-supplied size zero-filled. Valid +* only for the duration of the callback. * * This structure bundles all parameters for the file operation callbacks. * The 'data' and 'file' fields are used for both input and output.
@@ -51,6 +86,7 @@ struct liveupdate_file_op_args {struct file *file;u64 serialized_data;void *private_data; +const void *arg; };/**
@@ -68,6 +104,12 @@ struct liveupdate_file_op_args {* resources in the session. * @finish: Required. Final cleanup in the new kernel. * @get_id: Optional. Returns a unique identifier for the file. +* @parg: Optional. Specification of the user argument structure +* accepted by @preserve, initialized via LIVEUPDATE_ARG(). If +* unset, non-zero user arguments are rejected with -EOPNOTSUPP. +* @rarg: Optional. Specification of the user argument structure +* accepted by @retrieve, initialized via LIVEUPDATE_ARG(). If +* unset, non-zero user arguments are rejected with -EOPNOTSUPP. * @owner: Module reference * * All operations (except can_preserve) receive a pointer to a
@@ -84,6 +126,8 @@ struct liveupdate_file_ops {bool (*can_finish)(struct liveupdate_file_op_args *args);void (*finish)(struct liveupdate_file_op_args *args);unsigned long (*get_id)(struct file *file); +struct liveupdate_arg_spec parg; +struct liveupdate_arg_spec rarg;struct module *owner; };
include/uapi/linux/liveupdate.h +10 -0
@@ -126,6 +126,8 @@ struct liveupdate_ioctl_retrieve_session {* @size: Input; sizeof(struct liveupdate_session_preserve_fd) * @fd: Input; The user-space file descriptor to be preserved. * @token: Input; An opaque, unique token for preserved resource. +* @arg: Input; User-space pointer to a file-handler-specific argument +* structure starting with a 32-bit size field, or 0 if unused. * * Holds parameters for preserving a file descriptor. *
@@ -149,6 +151,7 @@ struct liveupdate_session_preserve_fd {__u32 size;__s32 fd;__aligned_u64 token; +__aligned_u64 arg; };#define LIVEUPDATE_SESSION_PRESERVE_FD \
@@ -160,6 +163,8 @@ struct liveupdate_session_preserve_fd {* @fd: Output; The new file descriptor representing the fully restored * kernel resource. * @token: Input; An opaque, token that was used to preserve the resource. +* @arg: Input; User-space pointer to a file-handler-specific argument +* structure starting with a 32-bit size field, or 0 if unused. * * Retrieve a previously preserved file descriptor. *
@@ -173,12 +178,17 @@ struct liveupdate_session_preserve_fd {* attempts to retrieve the token fail with the same error code. Failed * retrieves are not retried. * +* Retrieving a token that was already retrieved successfully returns a new file +* descriptor for the same file. The @arg only applies to the retrieve that +* restores the file, so passing a non-zero @arg then fails with -EBUSY. +* * Return: 0 on success, negative error code on failure (e.g., invalid token). */ struct liveupdate_session_retrieve_fd {__u32 size;__s32 fd;__aligned_u64 token; +__aligned_u64 arg; };#define LIVEUPDATE_SESSION_RETRIEVE_FD \
kernel/liveupdate/luo_file.c +67 -6
162 lines not on this page · raw message
kernel/liveupdate/luo_internal.h +3 -2
@@ -101,13 +101,14 @@ int __init luo_session_setup_incoming(u64 sessions_pa);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_file (struct luo_file_set *file_set,u64 token,int fd, +u64 arg);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);void luo_file_unfreeze (struct luo_file_set *file_set,struct luo_file_set_ser *file_set_ser); -int luo_retrieve_file (struct luo_file_set *file_set,u64 token, +int luo_retrieve_file (struct luo_file_set *file_set,u64 token,u64 arg,struct file **filep);int luo_file_finish (struct luo_file_set *file_set); int luo_file_deserialize(struct luo_file_set *file_set,
kernel/liveupdate/luo_session.c +4 -2
@@ -267,7 +267,8 @@ static int luo_session_preserve_fd(struct luo_session *session,int err;guard (mutex)(&session->mutex); - err =luo_preserve_file (&session->file_set, argp->token, argp->fd); + err =luo_preserve_file (&session->file_set, argp->token, argp->fd, + argp->arg);if (err)return err;
@@ -290,7 +291,8 @@ static int luo_session_retrieve_fd(struct luo_session *session,return argp->fd;mutex_lock (&session->mutex); - err =luo_retrieve_file (&session->file_set, argp->token, &file); + err =luo_retrieve_file (&session->file_set, argp->token, argp->arg, + &file);mutex_unlock (&session->mutex);if (err <0 ) goto err_put_fd;
Signature · 1 line
2.56.0.385.gac671c819a-goog
PATCH v3 2/10 selftests: liveupdate: Add tests for preserve and retrieve user argument
In reply to David MatlackFrom: Pasha Tatashin <[email protected]>
Verify the user argument support in LIVEUPDATE_SESSION_PRESERVE_FD and LIVEUPDATE_SESSION_RETRIEVE_FD. Add luo_session_preserve_fd_arg() and luo_session_retrieve_fd_arg() helpers to libliveupdate so tests can pass a handler-specific argument.
Add a preserve_memfd_with_arg test case to verify that memfd preservation rejects a non-zero argument with -EOPNOTSUPP, accepts an explicit zero argument, and remains compatible with callers passing the legacy struct size up to the token field.
--- .../liveupdate/lib/include/libliveupdate.h | 3 ++ .../selftests/liveupdate/lib/lu_utils.c | 17 ++++++- .../testing/selftests/liveupdate/liveupdate.c | 44 +++++++++++++++++++ 3 files changed, 62 insertions(+), 2 deletions(-)
tools/testing/selftests/liveupdate/lib/include/libliveupdate.h +3 -0
@@ -29,7 +29,10 @@ 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_fd_arg (int session_fd,int fd,__u64 token, +__u64 user_arg);int luo_session_retrieve_fd (int session_fd,__u64 token); +int luo_session_retrieve_fd_arg (int session_fd,__u64 token,__u64 user_arg);int create_and_preserve_memfd (int session_fd,int token,const char *data);int restore_and_verify_memfd (int session_fd,int token,const char *expected_data);
tools/testing/selftests/liveupdate/lib/lu_utils.c +15 -2
@@ -79,12 +79,14 @@ int luo_retrieve_session(int luo_fd, const char *name)return arg.fd; } -int luo_session_preserve_fd (int session_fd,int fd,__u64 token) +int luo_session_preserve_fd_arg (int session_fd,int fd,__u64 token, +__u64 user_arg) {struct liveupdate_session_preserve_fd arg = { .size =sizeof (arg), .fd = fd, .token = token, + .arg = user_arg, };if (ioctl (session_fd,LIVEUPDATE_SESSION_PRESERVE_FD , &arg))
@@ -93,11 +95,17 @@ int luo_session_preserve_fd(int session_fd, int fd, __u64 token)return 0 ; } -int luo_session_retrieve_fd (int session_fd,__u64 token) +int luo_session_preserve_fd (int session_fd,int fd,__u64 token) +{ +return luo_session_preserve_fd_arg (session_fd, fd, token,0 ); +} + +int luo_session_retrieve_fd_arg (int session_fd,__u64 token,__u64 user_arg) {struct liveupdate_session_retrieve_fd arg = { .size =sizeof (arg), .token = token, + .arg = user_arg, };if (ioctl (session_fd,LIVEUPDATE_SESSION_RETRIEVE_FD , &arg))
@@ -106,6 +114,11 @@ int luo_session_retrieve_fd(int session_fd, __u64 token)return arg.fd; } +int luo_session_retrieve_fd (int session_fd,__u64 token) +{ +return luo_session_retrieve_fd_arg (session_fd, token,0 ); +} +/* Helper function to get a session name via ioctl. */ int luo_get_session_name (int session_fd,char *name,size_t name_len) {
tools/testing/selftests/liveupdate/liveupdate.c +44 -0
54 lines not on this page · raw message
Signature · 1 line
2.56.0.385.gac671c819a-goog
PATCH v3 3/10 liveupdate: Allow file handlers to mark retrieve failures as retryable
In reply to David MatlackFrom: Pasha Tatashin <[email protected]>
When luo_retrieve_file() invokes a handler's .retrieve() callback and it returns an error, LUO currently records the error in luo_file->retrieve_status and blocks any subsequent retrieve attempts. This is necessary when the handler has already partially consumed or freed the preserved KHO state.
However, with user arguments passed to .retrieve(), a handler may fail early while validating caller-supplied parameters (such as a missing argument, an invalid or read-only target file descriptor, or a non-empty target file) before touching any preserved KHO state. In such cases, userspace should be able to fix the argument and retry LIVEUPDATE_SESSION_RETRIEVE_FD, and if the file is never retrieved, .finish() must still run to release the preserved KHO state.
Add a retryable output flag to struct liveupdate_file_op_args. When a handler sets args->retryable = true on error, luo_retrieve_file() leaves luo_file->retrieve_status at 0 so the retrieve operation can be retried and .finish() will still clean up the unconsumed preserved state if the session is finished without retrieving the file.
--- include/linux/liveupdate.h | 11 ++++++++--- include/uapi/linux/liveupdate.h | 7 ++++--- kernel/liveupdate/luo_file.c | 20 ++++++++++++++------ 3 files changed, 26 insertions(+), 12 deletions(-)
include/linux/liveupdate.h +8 -3
@@ -58,9 +58,13 @@ struct liveupdate_arg_spec {* @session: The session this file belongs to. * @retrieve_status: The retrieve status for the 'can_finish / finish' * operation. A value of 0 means the retrieve has not been -* attempted, a positive value means the retrieve was -* successful, and a negative value means the retrieve failed, -* and the value is the error code of the call. +* attempted (or only failed with a retryable error), a +* positive value means the retrieve was successful, and a +* negative value means the retrieve permanently failed, and +* the value is the error code of the call. +* @retryable: For .retrieve(): [OUT] Set to true by the callback on +* error when the preserved state has not been consumed and +* retrieval may be retried. * @file: The file object. For retrieve: [OUT] The callback sets * this to the new file. For other ops: [IN] The caller sets * this to the file being operated on.
@@ -83,6 +87,7 @@ struct liveupdate_file_op_args {struct liveupdate_file_handler *handler;struct liveupdate_session *session;int retrieve_status; +bool retryable;struct file *file;u64 serialized_data;void *private_data;
include/uapi/linux/liveupdate.h +4 -3
@@ -174,9 +174,10 @@ struct liveupdate_session_preserve_fd {* associated with the token and populates the @fd field with a new file * descriptor referencing the restored resource in the current (new) kernel. * This operation must be performed *before* signaling completion via -* %LIVEUPDATE_IOCTL_FINISH. If a retrieve of a token fails, subsequent -* attempts to retrieve the token fail with the same error code. Failed -* retrieves are not retried. +* %LIVEUPDATE_IOCTL_FINISH. If a retrieve of a token fails after consuming +* preserved state, subsequent attempts to retrieve the token fail with the +* same error code. Early argument or target validation errors before +* preserved state is consumed leave the token intact and may be retried. * * Retrieving a token that was already retrieved successfully returns a new file * descriptor for the same file. The @arg only applies to the retrieve that
kernel/liveupdate/luo_file.c +14 -6
@@ -152,9 +152,10 @@ static DEFINE_XARRAY(luo_preserved_files);* @retrieve_status: Status code indicating whether a user/kernel in the new kernel has * successfully called retrieve() on this file. This prevents * multiple retrieval attempts. A value of 0 means a retrieve() -* has not been attempted, a positive value means the retrieve() -* was successful, and a negative value means the retrieve() -* failed, and the value is the error code of the call. +* has not been attempted (or only failed with a retryable +* error), a positive value means the retrieve() was successful, +* and a negative value means the retrieve() permanently failed, +* and the value is the error code of the call. * @mutex: A mutex that protects the fields of this specific instance * (e.g., @retrieved, @file), ensuring that operations like * retrieving or finishing a file are atomic.
@@ -604,7 +605,9 @@ void luo_file_unfreeze(struct luo_file_set *file_set,* The operation is idempotent: if a file has already been successfully * retrieved, this function will simply return a pointer to the existing * 'struct file' and report success without re-executing the retrieve -* operation. This is handled by checking the 'retrieved' flag under a lock. +* operation. If a previous retrieve failed permanently, it returns that +* saved error code; if a previous retrieve failed with a retryable error +* before consuming preserved state, the retrieve operation is re-attempted. * * File retrieval can happen in any order; it is not bound by the order of * preservation.
@@ -671,8 +674,13 @@ int luo_retrieve_file(struct luo_file_set *file_set, u64 token, u64 arg, err = luo_file->fh->ops->retrieve (&args);kfree (args.arg);if (err) { -/* Keep the error code for later use. */ - luo_file->retrieve_status = err; +/* +* Keep the error code for later use unless the handler marked +* the failure as retryable (e.g. invalid user argument before +* preserved state was consumed). +*/ +if (!args.retryable) + luo_file->retrieve_status = err;return err; }
Signature · 1 line
2.56.0.385.gac671c819a-goog
PATCH v3 4/10 mm: Rename memfd_luo to shmem_luo
In reply to David MatlackRename the memfd Live Update file handler from memfd_luo to shmem_luo, including the source file, its KHO ABI header, its documentation and its Kconfig option (CONFIG_LIVEUPDATE_MEMFD to CONFIG_LIVEUPDATE_SHMEM).
The handler preserves shmem files. Today those are memfds, but following commits extend it to other files on tmpfs mounts, for which the memfd name is confusing. Preserving memfds created with MFD_HUGETLB, which are hugetlbfs files, will need a separate handler, so naming the handler after the filesystem that backs the files also avoids having to rename it again then.
Don't change the compatible string of the handler, "memfd-v2". It is matched against the preserved state from the previous kernel, so changing it would break live updates from kernels that use the old name. The serialized format doesn't change either.
Also clarify in the documentation that memfd means a memfd created without MFD_HUGETLB, and name the ABI documentation after the handler.
No functional change intended.
---
Documentation/core-api/liveupdate.rst | 2 +-
Documentation/mm/index.rst | 2 +-
...reservation.rst => shmem_preservation.rst} | 8 +-
MAINTAINERS | 4 +-
include/linux/kho/abi/{memfd.h => shmem.h} | 41 ++++---
kernel/liveupdate/Kconfig | 2 +-
mm/Makefile | 2 +-
mm/{memfd_luo.c => shmem_luo.c} | 115 +++++++++---------
8 files changed, 89 insertions(+), 87 deletions(-)
rename Documentation/mm/{memfd_preservation.rst => shmem_preservation.rst} (66%)
rename include/linux/kho/abi/{memfd.h => shmem.h} (65%)
rename mm/{memfd_luo.c => shmem_luo.c} (81%)Documentation/core-api/liveupdate.rst +1 -1
@@ -37,7 +37,7 @@ The following types of file descriptors can be preserved .. toctree:: :maxdepth: 1 - ../mm/memfd_preservation + ../mm/shmem_preservation Public API ==========
Documentation/mm/index.rst +1 -1
@@ -61,7 +61,7 @@ documentation, or deleted if it has served its purpose. hugetlbfs_reserv ksm memory-model - memfd_preservation + shmem_preservation mmu_notifier multigen_lru numa
Documentation/mm/shmem_preservation.rst renamed from Documentation/mm/memfd_preservation.rst +4 -4
@@ -4,16 +4,16 @@ Memfd Preservation via LUO ========================== -.. kernel-doc:: mm/memfd_luo.c +.. kernel-doc:: mm/shmem_luo.c :doc: Memfd Preservation via LUO Memfd Preservation ABI ====================== -.. kernel-doc:: include/linux/kho/abi/memfd.h - :doc: memfd Live Update ABI +.. kernel-doc:: include/linux/kho/abi/shmem.h + :doc: shmem Live Update ABI -.. kernel-doc:: include/linux/kho/abi/memfd.h +.. kernel-doc:: include/linux/kho/abi/shmem.h :internal: See Also
MAINTAINERS +2 -2
@@ -15184,14 +15184,14 @@ L: [email protected] 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/shmem_preservation.rst F: Documentation/userspace-api/liveupdate.rst F: include/linux/kho/abi/ F: include/linux/liveupdate.h F: include/uapi/linux/liveupdate.h F: kernel/liveupdate/ F: lib/tests/liveupdate.c -F: mm/memfd_luo.c +F: mm/shmem_luo.c F: tools/testing/selftests/liveupdate/ LLC (802.2)
include/linux/kho/abi/shmem.h renamed from include/linux/kho/abi/memfd.h +21 -20
100 lines not on this page · raw message
kernel/liveupdate/Kconfig +1 -1
@@ -69,7 +69,7 @@ config LIVEUPDATE If unsure, say N. -config LIVEUPDATE_MEMFD +config LIVEUPDATE_SHMEM bool "Live update support for memfd" depends on LIVEUPDATE depends on MEMFD_CREATE
mm/Makefile +1 -1
@@ -100,7 +100,7 @@ obj-$(CONFIG_NUMA) += memory-tiers.o obj-$(CONFIG_DEVICE_MIGRATION) += migrate_device.o obj-$(CONFIG_TRANSPARENT_HUGEPAGE) += huge_memory.o khugepaged.o obj-$(CONFIG_PAGE_COUNTER) += page_counter.o -obj-$(CONFIG_LIVEUPDATE_MEMFD) += memfd_luo.o +obj-$(CONFIG_LIVEUPDATE_SHMEM) += shmem_luo.o obj-$(CONFIG_MEMCG_V1) += memcontrol-v1.o obj-$(CONFIG_MEMCG) += memcontrol.o vmpressure.o ifdef CONFIG_BPF_SYSCALL
mm/shmem_luo.c renamed from mm/memfd_luo.c +58 -57
307 lines not on this page · raw message
Signature · 1 line
2.56.0.385.gac671c819a-goog
PATCH v3 5/10 liveupdate: Don't WARN if tracking a retrieved file fails
In reply to David MatlackHandle a failure to add a retrieved file to luo_preserved_files instead of triggering a WARN_ON(). Make the failure permanent, and keep the file until the file set is finished, like any other retrieved file.
xa_insert() can fail with -ENOMEM, and userspace can trigger that with LIVEUPDATE_SESSION_RETRIEVE_FD, so warning about it lets userspace crash systems with panic_on_warn.
The handler has already consumed the preserved state when this happens, so the retrieve can't be retried. Keeping the file ensures that its resources are not released before the file set is finished. Finishing the file set then erases the file from luo_preserved_files, which does nothing for a file that was never added.
--- kernel/liveupdate/luo_file.c | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-)
kernel/liveupdate/luo_file.c +15 -4
@@ -684,14 +684,25 @@ int luo_retrieve_file(struct luo_file_set *file_set, u64 token, u64 arg,return err; } + err =xa_insert (&luo_preserved_files, +luo_get_id (luo_file->fh, args.file), args.file, +GFP_KERNEL ); +if (err) { +/* +* The handler has consumed the preserved state, so the retrieve +* can't be retried. Keep the file until the file set is +* finished, so that its resources are released at the same +* time as those of other retrieved files. +*/ + luo_file->file = args.file; + luo_file->retrieve_status = err; +return err; + } + luo_file->file = args.file;/* Get reference so we can keep this file in LUO until finish */ get_file (luo_file->file); -WARN_ON (xa_insert (&luo_preserved_files, -luo_get_id (luo_file->fh, luo_file->file), - luo_file->file,GFP_KERNEL )); - *filep = luo_file->file; luo_file->retrieve_status =1 ;
Signature · 1 line
2.56.0.385.gac671c819a-goog
PATCH v3 6/10 mm/shmem_luo: Don't make early retrieve failures permanent
In reply to David MatlackMark failures in shmem_luo_retrieve() before it starts restoring the folios as retryable, and return the error without freeing the serialized state. If no retrieve succeeds, finish() frees the preserved folios, like it does for a file that was never retrieved.
Nothing has consumed the preserved state before the folios are restored, so the retrieve can be tried again.
This also fixes leaking the preserved folios when one of these early steps fails, e.g. because the file has seals that this kernel doesn't support. The retrieve freed the serialized state but not the folios, and finish() then skipped the file because its retrieve had failed. There is no Fixes tag, since the fix relies on args->retryable, which was only added earlier in this series.
--- mm/shmem_luo.c | 33 ++++++++++++++++++++------------- 1 file changed, 20 insertions(+), 13 deletions(-)
mm/shmem_luo.c +20 -13
@@ -390,9 +390,9 @@ static void shmem_luo_finish(struct liveupdate_file_op_args *args)struct shmem_luo_ser *ser;/* -* If retrieve was successful, nothing to do. If it failed, retrieve() -* already cleaned up everything it could. So nothing to do there -* either. Only need to clean up when retrieve was not called. +* If retrieve was successful, nothing to do. If it failed permanently, +* retrieve() already cleaned up everything it could. So nothing to do +* there either. Only need to clean up when the file was not retrieved. */ if (args->retrieve_status)return ;
@@ -526,11 +526,16 @@ static int shmem_luo_retrieve(struct liveupdate_file_op_args *args)if (!ser)return -EINVAL ; +/* +* Nothing has consumed the preserved state until the folios are +* restored below, so failures before that can be retried. If no +* retrieve succeeds, finish() frees the preserved state. +*/ + args->retryable = true; +/* Make sure the file only has seals supported by this version. */ -if (ser->seals & ~SHMEM_LUO_ALL_SEALS ) { - err = -EOPNOTSUPP ; - goto free_ser; - } +if (ser->seals & ~SHMEM_LUO_ALL_SEALS ) +return -EOPNOTSUPP ;/* * The seals are preserved. Allow sealing here so they can be added
@@ -539,8 +544,7 @@ static int shmem_luo_retrieve(struct liveupdate_file_op_args *args) file =memfd_alloc_file ("" ,MFD_ALLOW_SEALING );if (IS_ERR (file)) {pr_err ("failed to setup file: %pe\n" , file); - err =PTR_ERR (file); - goto free_ser; +return PTR_ERR (file); } err =memfd_add_seals (file, ser->seals);
@@ -552,17 +556,20 @@ static int shmem_luo_retrieve(struct liveupdate_file_op_args *args)vfs_setpos (file, ser->pos,MAX_LFS_FILESIZE );i_size_write (file_inode (file), ser->size); +/* The preserved state is consumed from here on. */ + args->retryable = false; +if (ser->nr_folios) { folios_ser =kho_restore_vmalloc (&ser->folios);if (!folios_ser) { err = -EINVAL ; - goto put_file; + goto free_ser; } err =shmem_luo_retrieve_folios (file, folios_ser, ser->nr_folios);vfree (folios_ser);if (err) - goto put_file; + goto free_ser; } args->file = file;
@@ -570,10 +577,10 @@ static int shmem_luo_retrieve(struct liveupdate_file_op_args *args)return 0 ; -put_file: -fput (file); free_ser:kho_restore_free (ser); +put_file: +fput (file);return err; }
Signature · 1 line
2.56.0.385.gac671c819a-goog
PATCH v3 7/10 mm/shmem_luo: Retrieve tmpfs files onto a tmpfs mount chosen by userspace
In reply to David MatlackRetrieve files that were preserved from a tmpfs mount onto a tmpfs mount chosen by userspace, instead of as memfds on the internal tmpfs mount. Add struct liveupdate_shmem_retrieve_arg, which userspace passes as the retrieve argument of LIVEUPDATE_SESSION_RETRIEVE_FD to choose the mount. Record which mount a file was on with a new SHMEM_LUO_FLAG_TMPFS flag in struct shmem_luo_ser, based on the superblock of the file.
shmem_luo already accepts unlinked tmpfs files, such as files created with O_TMPFILE, and a following commit also accepts linked ones. The mount such a file was on doesn't exist in the new kernel, and userspace may want to put the file elsewhere anyway.
Model struct liveupdate_shmem_retrieve_arg on open(): The kernel creates a new file as if by openat(dirfd, ".", O_TMPFILE | flags, mode), so the file gets the owner, permission checks and security labels of any new file in that directory, and is charged to that mount. The kernel then fills the file with the preserved contents, size, position and seals before returning it, so nothing else can access the file before it is complete. Userspace links the file at the path it wants with linkat(), or passes O_EXCL to keep it unlinked. Neither the path nor any other inode attribute of the file is part of the preserved state, which keeps them out of the KHO ABI.
Pass the flags from userspace to file_open_root(), so that the VFS checks them like for open(). file_open_root() silently ignores unknown flags and mode bits, so reject those like openat2() does. Also reject O_PATH, with which file_open_root() would ignore O_TMPFILE and open the directory.
Keep the type of the file: memfds can only be retrieved as memfds, and files on tmpfs mounts only onto tmpfs mounts, so that e.g. a file can't be moved off the mount whose size limit and quotas it was subject to. Passing the argument when retrieving a memfd, or not passing it when retrieving a file from a tmpfs mount, fails with -EINVAL. Unlinked tmpfs files were retrieved as memfds until now, but nothing relies on that, as the LUO uAPI is not stable yet.
Like the other failures before the folios are restored, problems with the argument and with creating the file can be retried.
--- Documentation/mm/shmem_preservation.rst | 8 +- include/linux/kho/abi/shmem.h | 21 ++- include/uapi/linux/liveupdate.h | 35 +++++ kernel/liveupdate/Kconfig | 6 +- mm/internal.h | 1 + mm/shmem.c | 9 ++ mm/shmem_luo.c | 180 ++++++++++++++++++++---- 7 files changed, 221 insertions(+), 39 deletions(-)
Documentation/mm/shmem_preservation.rst +4 -4
@@ -1,11 +1,11 @@ .. SPDX-License-Identifier: GPL-2.0-or-later -========================== -Memfd Preservation via LUO -========================== +==================================== +Memfd and Tmpfs Preservation via LUO +==================================== .. kernel-doc:: mm/shmem_luo.c - :doc: Memfd Preservation via LUO + :doc: Memfd and Tmpfs Preservation via LUO Memfd Preservation ABI ======================
include/linux/kho/abi/shmem.h +17 -4
@@ -18,8 +18,8 @@* DOC: shmem Live Update ABI * * The shmem file handler, which preserves memfds created without -* ``MFD_HUGETLB``, uses the ABI defined below for preserving their state across -* a kexec reboot using the LUO. +* ``MFD_HUGETLB`` and tmpfs files, uses the ABI defined below for preserving +* their state across a kexec reboot using the LUO. * * The state is serialized into a packed structure `struct shmem_luo_ser` * which is handed over to the next kernel via the KHO mechanism.
@@ -69,12 +69,25 @@ struct shmem_luo_folio_ser { F_SEAL_EXEC)/** -* struct shmem_luo_ser - Main serialization structure for a memfd. +* SHMEM_LUO_FLAG_TMPFS - The file was on a tmpfs mount. +* +* Set for files that were not on the internal shmem mount that memfds are +* created on. Such a file is retrieved onto a tmpfs mount chosen by userspace, +* instead of as a memfd. +*/ +#define SHMEM_LUO_FLAG_TMPFS BIT(0) + +/* The set of flags this version supports. */ +#define SHMEM_LUO_ALL_FLAGS SHMEM_LUO_FLAG_TMPFS + +/** +* struct shmem_luo_ser - Main serialization structure for a memfd or tmpfs file. * @pos: The file's current position (f_pos). * @size: The total size of the file in bytes (i_size). * @seals: The seals present on the memfd. The seals are uABI so it is safe * to directly use them in the ABI. -* @flags: Flags for the file. Unused flag bits must be set to 0. +* @flags: Flags for the file (SHMEM_LUO_FLAG_*). 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 shmem_luo_folio_ser.
include/uapi/linux/liveupdate.h +35 -0
@@ -247,4 +247,39 @@ struct liveupdate_session_get_name {#define LIVEUPDATE_SESSION_GET_NAME \ _IO(LIVEUPDATE_IOCTL_TYPE, LIVEUPDATE_CMD_SESSION_GET_NAME) +/** +* struct liveupdate_shmem_retrieve_arg - Argument to retrieve a tmpfs file +* @size: Input; sizeof(struct liveupdate_shmem_retrieve_arg) +* @dirfd: Input; A file descriptor for a directory on the tmpfs mount to create +* the file on. +* @flags: Input; The flags of the file, like the flags argument of open(), +* except for O_TMPFILE, which is implied, and O_PATH, which is not +* allowed. +* @mode: Input; The mode of the file, like the mode argument of open(). +* +* Passed via &struct liveupdate_session_retrieve_fd.arg to retrieve a file that +* was preserved from a tmpfs mount. Such files can only be retrieved with this +* argument, and memfds, i.e. files that were preserved from the kernel's +* internal shmem mount, can only be retrieved without it. Otherwise, the +* retrieve fails with -EINVAL. +* +* The kernel creates a new file as if by openat(@dirfd, ".", O_TMPFILE | +* @flags, @mode), fills it with the preserved contents, and returns it in +* &struct liveupdate_session_retrieve_fd.fd. Like any O_TMPFILE file, the file +* is not linked into the filesystem. Userspace can link it with linkat(), +* unless @flags contains O_EXCL. +* +* If @flags contains unknown flags or O_PATH, if @mode contains unknown mode +* bits, or if @dirfd is not on a tmpfs mount, the retrieve fails with -EINVAL. +* If the file can't be created, the retrieve fails with the same errors as +* open(). A retrieve that fails before the preserved contents are restored can +* be retried. +*/ +struct liveupdate_shmem_retrieve_arg { +__u32 size; +__s32 dirfd; +__aligned_u64 flags; +__aligned_u64 mode; +}; +#endif /* _UAPI_LIVEUPDATE_H */
kernel/liveupdate/Kconfig +3 -3
@@ -70,14 +70,14 @@ config LIVEUPDATE If unsure, say N. config LIVEUPDATE_SHMEM - bool "Live update support for memfd" + bool "Live update support for memfd and tmpfs files" depends on LIVEUPDATE depends on MEMFD_CREATE depends on SHMEM default LIVEUPDATE help - Enable live update support for memfd regions. This allows preserving - memfd-backed memory across kernel live updates. + Enable live update support for memfds and tmpfs files. This allows + preserving their contents across kernel live updates. This can be used to back VM memory with memfds, allowing the guest memory to persist, or for other user workloads needing to preserve
mm/internal.h +1 -0
@@ -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); +bool shmem_inode_on_internal_mount (const struct inode *inode);#ifdef CONFIG_SHRINKER_DEBUG static inline __printf(2, 0) int shrinker_debugfs_name_alloc(
mm/shmem.c +9 -0
@@ -468,6 +468,15 @@ bool shmem_recalc_inode(struct inode *inode, long alloced, long swapped)return first_swapped; } +/* +* Whether @inode is on the internal shmem mount, which is used by +* shmem_file_setup() and thus by memfd_create(). +*/ +bool shmem_inode_on_internal_mount (const struct inode *inode) +{ +return inode->i_sb == shm_mnt->mnt_sb; +} +bool shmem_charge (struct inode *inode,long pages) {struct address_space *mapping = inode->i_mapping;
mm/shmem_luo.c +152 -28
261 lines not on this page · raw message
Signature · 1 line
2.56.0.385.gac671c819a-goog
PATCH v3 8/10 mm/shmem_luo: Allow preserving tmpfs files that are still linked
In reply to David MatlackAllow preserving tmpfs files that are still linked. To preserve any file, require permission to write to it and the file to be opened for reading, and reject append-only files.
shmem_luo already accepts any shmem file without links, which includes unlinked tmpfs files and files created with O_TMPFILE. Accepting linked files means that userspace doesn't have to unlink files to preserve them and can keep using them by path until the live update.
The path of the file isn't preserved. Like other files from tmpfs mounts, a linked file is retrieved onto a tmpfs mount chosen by userspace, which can then link it at the path it wants.
While a file is preserved, its size is frozen, so truncating it, fallocate() and writes past its end fail with -EPERM. For a linked file, this affects everyone who opens the file by its path, so a caller that can only read the file, e.g. a world-readable file in /dev/shm, could use it to break the owner of the file. So require permission to write to the file to preserve it, for all files. Like FS_IOC_ENABLE_VERITY, don't require a file opened for writing, so that binaries that are being executed can still be preserved through read-only fds. Also don't require a writable mount, since preserving a file doesn't write to the filesystem. If the size of the file can't change anyway, because it is immutable or on a read-only superblock, also let its owner preserve it. Don't let others do that, since the size could change again once the file is made writable. Like FS_IOC_ENABLE_VERITY, reject append-only files, since their writers can only append to them, and freezing their size would stop all appends.
Also like FS_IOC_ENABLE_VERITY, require the file to be opened for reading. The retrieved file can be read, so otherwise a caller that can only write to a file could preserve it to read its contents.
Preserving a file also fills its holes, but don't require any permission for that. Anyone who can read a tmpfs file can already fill its holes by faulting them in through a read-only mapping, also on read-only mounts and for immutable files.
Document the frozen size and the permission requirements.
--- mm/shmem_luo.c | 67 +++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 64 insertions(+), 3 deletions(-)
mm/shmem_luo.c +64 -3
@@ -28,6 +28,24 @@* the file are preserved. All others are reset to default. The preserved * properties are described below. * +* A tmpfs file can be preserved even if it is linked, but its path is not +* preserved. Userspace chooses where to put the file when retrieving it, as +* described in "Retrieving Tmpfs Files" below. +* +* Preserving a file requires it to be opened for reading, since the retrieved +* file can be read. It also requires permission to write to the file, since +* preserving it affects all of its users, as described below. But the file +* doesn't need to be opened for writing. For example, a binary that is being +* executed can be preserved through a read-only file descriptor. The owner of +* a file whose size can't change anyway, because it is immutable or on a +* read-only superblock, may also preserve it. The file can be preserved +* through a read-only mount. Append-only files can't be preserved. +* +* While a file is preserved, its size can't change and no holes can be punched +* into it. Truncating the file, ``fallocate()`` and writes past the end of the +* file fail with ``-EPERM``. This applies to all users of the file, including +* users that open it by its path. +* * .. note:: * The LUO API is not stabilized yet, so the preserved properties of a memfd * or tmpfs file are also not stable and are subject to backwards
@@ -298,6 +316,45 @@ static void shmem_luo_unpreserve_folios(struct kho_vmalloc *kho_vmalloc,vfree (folios_ser); } +/* +* Check whether the caller may preserve @file. The retrieved file can be read, +* so require @file to be opened for reading, like FS_IOC_ENABLE_VERITY does. +* +* Preserving the file freezes its size until it is unpreserved, for all users +* of the file, so require permission to write to the file. Like +* FS_IOC_ENABLE_VERITY, don't require a file opened for writing, so that e.g. +* a binary that is being executed can be preserved. Also don't require a +* writable mount, since preserving the file doesn't write to the filesystem. +* If the size of the file can't change anyway, because it is on a read-only +* superblock or immutable, its owner may also preserve it. The size could only +* change again after the file is made writable, which is up to the owner or +* root. Reject append-only files: Their writers can only append to them, and +* freezing the size would stop all appends. +* +* Preserving the file also fills its holes, but that doesn't need any more +* permission: Anyone who can read the file can fill its holes by faulting +* them in through a read-only mapping. +*/ +static int shmem_luo_may_preserve (struct file *file) +{ +struct mnt_idmap *idmap =file_mnt_idmap (file); +struct inode *inode =file_inode (file); +int err; + +if (!(file->f_mode &FMODE_READ )) +return -EBADF ; + +if (IS_APPEND (inode)) +return -EPERM ; + + err =inode_permission (idmap, inode,MAY_WRITE ); +if (err && (sb_rdonly (inode->i_sb) ||IS_IMMUTABLE (inode)) && +inode_owner_or_capable (idmap, inode)) +return 0 ; + +return err; +} +static int shmem_luo_preserve (struct liveupdate_file_op_args *args) {struct inode *inode =file_inode (args->file);
@@ -307,6 +364,11 @@ static int shmem_luo_preserve(struct liveupdate_file_op_args *args)int err =0 , seals;inode_lock (inode); + + err =shmem_luo_may_preserve (args->file); +if (err) + goto err_unlock_inode; +shmem_freeze (inode, true);/* Allocate the main serialization structure in preserved memory */
@@ -362,6 +424,7 @@ static int shmem_luo_preserve(struct liveupdate_file_op_args *args)kho_unpreserve_free (ser); err_unlock:shmem_freeze (inode, false); +err_unlock_inode:inode_unlock (inode);return err; }
@@ -710,9 +773,7 @@ static int shmem_luo_retrieve(struct liveupdate_file_op_args *args)static bool shmem_luo_can_preserve (struct liveupdate_file_handler *handler,struct file *file) { -struct inode *inode =file_inode (file); - -return shmem_file (file) && !inode->i_nlink; +return shmem_file (file); }static unsigned long shmem_luo_get_id (struct file *file)
Signature · 1 line
2.56.0.385.gac671c819a-goog
PATCH v3 9/10 mm/shmem_luo: Allow retrieving tmpfs files with O_RDONLY
In reply to David MatlackAllow userspace to pass O_RDONLY in struct liveupdate_shmem_retrieve_arg to retrieve a tmpfs file read-only.
open() requires write access for O_TMPFILE, so create the file writable like before, then reopen it read-only with dentry_open() using the flags of the writable file, which file_open_root() normalized, and drop the writable file. Like open(), which grants the requested access to a new file regardless of its mode, don't check permission for the read-only open.
Retrieving a file read-only allows a restored binary to be executed before the session is finished. LUO keeps the retrieved file open until then, and executing a binary that is open for writing fails with -ETXTBSY. Being able to execute restored binaries right away matters when they are needed to finish restoring the system, e.g. to bring up the services that retrieve and finish the remaining sessions.
--- include/uapi/linux/liveupdate.h | 6 +++--- mm/shmem_luo.c | 34 ++++++++++++++++++++++++++------- 2 files changed, 30 insertions(+), 10 deletions(-)
include/uapi/linux/liveupdate.h +3 -3
@@ -265,9 +265,9 @@ struct liveupdate_session_get_name {* * The kernel creates a new file as if by openat(@dirfd, ".", O_TMPFILE | * @flags, @mode), fills it with the preserved contents, and returns it in -* &struct liveupdate_session_retrieve_fd.fd. Like any O_TMPFILE file, the file -* is not linked into the filesystem. Userspace can link it with linkat(), -* unless @flags contains O_EXCL. +* &struct liveupdate_session_retrieve_fd.fd. Unlike open(), O_RDONLY is +* allowed. Like any O_TMPFILE file, the file is not linked into the filesystem. +* Userspace can link it with linkat(), unless @flags contains O_EXCL. * * If @flags contains unknown flags or O_PATH, if @mode contains unknown mode * bits, or if @dirfd is not on a tmpfs mount, the retrieve fails with -EINVAL.
mm/shmem_luo.c +27 -7
@@ -113,9 +113,10 @@* * Like any ``O_TMPFILE`` file, the new file is not linked into the filesystem. * Userspace can link it at the path it wants with ``linkat()``, unless it asked -* for ``O_EXCL``. Like with ``open()``, the file must be opened for writing. -* LUO keeps the file open until the session is finished, so a retrieved binary -* can't be executed until then. +* for ``O_EXCL``. Unlike ``open()``, the file can be retrieved with +* ``O_RDONLY``. LUO keeps the file open until the session is finished, so a +* binary retrieved with ``O_RDWR`` or ``O_WRONLY`` can't be executed until +* then. * * The blocks of the file are charged to the target mount, so the size limit * and quotas of the mount apply. If the file doesn't fit, the retrieve fails
@@ -661,7 +662,9 @@ static struct file *shmem_luo_create_memfd(const struct liveupdate_shmem_retrievstatic struct file *shmem_luo_create_tmpfs_file (const struct liveupdate_shmem_retrieve_arg *arg,unsigned int seals) { -struct file *file; +struct file *file, *ro_file; +bool rdonly; +int flags;int err;if (!arg)
@@ -685,10 +688,16 @@ static struct file *shmem_luo_create_tmpfs_file(const struct liveupdate_shmem_re/* * Create the file like open(O_TMPFILE) does, so that it gets the same -* flag checks, permission checks, owner and security labels. +* flag checks, permission checks, owner and security labels. open() +* requires write access for O_TMPFILE, so create the file writable and +* reopen it read-only below if that is what userspace asked for. LUO +* keeps the file until the session is finished, and a file opened for +* writing couldn't be executed until then. */ + flags =O_TMPFILE |O_LARGEFILE | arg->flags; + rdonly = (flags &O_ACCMODE ) ==O_RDONLY ; file =file_open_root (&fd_file (dir)->f_path,"." , -O_TMPFILE |O_LARGEFILE | arg->flags, arg->mode); + flags | (rdonly ?O_RDWR :0 ), arg->mode);if (IS_ERR (file))return file;
@@ -706,7 +715,18 @@ static struct file *shmem_luo_create_tmpfs_file(const struct liveupdate_shmem_re }SHMEM_I (file_inode (file))->seals = seals; -return file; +if (!rdonly) +return file; + +/* +* Reopen the file with the flags of the writable file, which +* file_open_root() normalized, e.g. by dropping O_CLOEXEC. +*/ + ro_file =dentry_open (&file->f_path, + (file->f_flags & ~O_ACCMODE ) |O_RDONLY , +current_cred ()); +fput (file); +return ro_file; }static int shmem_luo_retrieve (struct liveupdate_file_op_args *args)
Signature · 1 line
2.56.0.385.gac671c819a-goog
PATCH v3 10/10 selftests/liveupdate: Add a test for preserving tmpfs files
In reply to David MatlackAdd luo_tmpfs, a kexec test for preserving tmpfs files and memfds, and for retrieving tmpfs files onto a tmpfs mount with struct liveupdate_shmem_retrieve_arg.
Stage 1 preserves the source files of each test case. Stage 2 runs each test case in its own directory, with its own tmpfs mounts, so that block usage can be checked exactly with statfs() and st_blocks. The test cases cover:
- The restored file: contents, size, position, seals and holes, for linked and unlinked source files.
- Creating the file: it is created like with open(O_TMPFILE), with the requested mode minus the umask, owned by the caller, and not linked until userspace links it, unless it asked for O_EXCL. Also O_RDONLY and O_WRONLY files, other open() flags such as O_APPEND, O_NONBLOCK and O_CLOEXEC, read-only mounts, and directories that the caller can't write to.
- Preserving files that can't be written to: a binary while it is executed, through a read-only fd, files on read-only mounts and superblocks, and immutable files, including sparse ones, whose holes are filled. Preserving someone else's file that the caller can't write to fails, and so does preserving a file through a write-only fd or an append-only file.
- Executing restored binaries: a restored binary can be executed before the session is finished if it was retrieved with O_RDONLY, but not if it was retrieved for writing, since LUO keeps the file open until then. These test cases execute copies of the test binary.
- Block accounting: the file fits a mount of exactly its size, and a large folio that extends past the end of the file is charged in full. Running out of space, or hitting the memcg limit, fails permanently, and leaves nothing charged to the mount or the memcg. The session can still be finished.
- memfds: they are only retrieved as memfds, with their seals, and files from tmpfs mounts only onto a tmpfs mount.
- Interactions with LUO: invalid arguments, which can be retried, retrieving a token twice, and outgoing sessions.
Also check that the size of a preserved file is frozen, and that the test doesn't trigger kernel warnings.
Enable CONFIG_TRANSPARENT_HUGEPAGE for the large folio test cases, CONFIG_MEMCG for the memcg test case and CONFIG_TMPFS_XATTR for FS_IOC_SETFLAGS. Those test cases are skipped without them.
tmpfs quotas are not tested, since the test runs as root, which is not subject to quota limits.
--- tools/testing/selftests/liveupdate/Makefile | 1 + tools/testing/selftests/liveupdate/config | 4 + .../testing/selftests/liveupdate/luo_tmpfs.c | 2055 +++++++++++++++++ .../selftests/liveupdate/run-vmtests.sh | 1 + 4 files changed, 2061 insertions(+) create mode 100644 tools/testing/selftests/liveupdate/luo_tmpfs.c
tools/testing/selftests/liveupdate/Makefile +1 -0
@@ -6,6 +6,7 @@ TEST_GEN_PROGS_EXTENDED += luo_kexec_simple TEST_GEN_PROGS_EXTENDED += luo_multi_session TEST_GEN_PROGS_EXTENDED += luo_stress_sessions TEST_GEN_PROGS_EXTENDED += luo_stress_files +TEST_GEN_PROGS_EXTENDED += luo_tmpfs TEST_FILES += do_kexec.sh
tools/testing/selftests/liveupdate/config +4 -0
@@ -1,4 +1,5 @@ CONFIG_BLK_DEV_INITRD=y +CONFIG_CGROUPS=y CONFIG_DEVTMPFS=y CONFIG_KEXEC_FILE=y CONFIG_KEXEC_HANDOVER=y
@@ -7,6 +8,9 @@ CONFIG_KEXEC_HANDOVER_DEBUGFS=y CONFIG_KEXEC_HANDOVER_DEBUG=y CONFIG_LIVEUPDATE=y CONFIG_LIVEUPDATE_TEST=y +CONFIG_MEMCG=y CONFIG_MEMFD_CREATE=y CONFIG_TMPFS=y +CONFIG_TMPFS_XATTR=y CONFIG_SHMEM=y +CONFIG_TRANSPARENT_HUGEPAGE=y
tools/testing/selftests/liveupdate/luo_tmpfs.c new file +2055 -0
2,055 lines not on this page · raw message
tools/testing/selftests/liveupdate/run-vmtests.sh +1 -0
@@ -20,6 +20,7 @@ TEST_NAMES=( "luo_multi_session" "luo_stress_files" "luo_stress_sessions" + "luo_tmpfs" ) function usage() {
Signature · 1 line
2.56.0.385.gac671c819a-goog
Re: [PATCH v3 07/10] mm/shmem_luo: Retrieve tmpfs files onto a tmpfs mount chosen by userspace
In reply to David MatlackQuoted 37 lines
Retrieve files that were preserved from a tmpfs mount onto a tmpfs mount chosen by userspace, instead of as memfds on the internal tmpfs mount. Add struct
liveupdate_shmem_retrieve_arg, which userspace passes as the retrieve argument ofLIVEUPDATE_SESSION_RETRIEVE_FDto choose the mount. Record which mount a file was on with a newSHMEM_LUO_FLAG_TMPFSflag in structshmem_luo_ser, based on the superblock of the file.
shmem_luoalready accepts unlinked tmpfs files, such as files created withO_TMPFILE, and a following commit also accepts linked ones. The mount such a file was on doesn't exist in the new kernel, and userspace may want to put the file elsewhere anyway.Model struct
liveupdate_shmem_retrieve_argonopen(): The kernel creates a new file as if byopenat(dirfd, ".", O_TMPFILE | flags, mode), so the file gets the owner, permission checks and security labels of any new file in that directory, and is charged to that mount. The kernel then fills the file with the preserved contents, size, position and seals before returning it, so nothing else can access the file before it is complete. Userspace links the file at the path it wants withlinkat(), or passesO_EXCLto keep it unlinked. Neither the path nor any other inode attribute of the file is part of the preserved state, which keeps them out of the KHO ABI.Pass the flags from userspace to
file_open_root(), so that the VFS checks them like foropen().file_open_root()silently ignores unknown flags and mode bits, so reject those likeopenat2()does. Also rejectO_PATH, with whichfile_open_root()would ignoreO_TMPFILEand open the directory.Keep the type of the file: memfds can only be retrieved as memfds, and files on tmpfs mounts only onto tmpfs mounts, so that e.g. a file can't be moved off the mount whose size limit and quotas it was subject to. Passing the argument when retrieving a memfd, or not passing it when retrieving a file from a tmpfs mount, fails with
-EINVAL. Unlinked tmpfs files were retrieved as memfds until now, but nothing relies on that, as the LUO uAPI is not stable yet.
Quoted 5 lines
Like the other failures before the folios are restored, problems with the argument and with creating the file can be retried.
Signature · 1 line
Pasha
Re: [PATCH v3 08/10] mm/shmem_luo: Allow preserving tmpfs files that are still linked
In reply to David MatlackQuoted 38 lines
Allow preserving tmpfs files that are still linked. To preserve any file, require permission to write to it and the file to be opened for reading, and reject append-only files.
shmem_luoalready accepts any shmem file without links, which includes unlinked tmpfs files and files created withO_TMPFILE. Accepting linked files means that userspace doesn't have to unlink files to preserve them and can keep using them by path until the live update.The path of the file isn't preserved. Like other files from tmpfs mounts, a linked file is retrieved onto a tmpfs mount chosen by userspace, which can then link it at the path it wants.
While a file is preserved, its size is frozen, so truncating it,
fallocate()and writes past its end fail with-EPERM. For a linked file, this affects everyone who opens the file by its path, so a caller that can only read the file, e.g. a world-readable file in/dev/shm, could use it to break the owner of the file. So require permission to write to the file to preserve it, for all files. LikeFS_IOC_ENABLE_VERITY, don't require a file opened for writing, so that binaries that are being executed can still be preserved through read-only fds. Also don't require a writable mount, since preserving a file doesn't write to the filesystem. If the size of the file can't change anyway, because it is immutable or on a read-only superblock, also let its owner preserve it. Don't let others do that, since the size could change again once the file is made writable. LikeFS_IOC_ENABLE_VERITY, reject append-only files, since their writers can only append to them, and freezing their size would stop all appends.Also like
FS_IOC_ENABLE_VERITY, require the file to be opened for reading. The retrieved file can be read, so otherwise a caller that can only write to a file could preserve it to read its contents.Preserving a file also fills its holes, but don't require any permission for that. Anyone who can read a tmpfs file can already fill its holes by faulting them in through a read-only mapping, also on read-only mounts and for immutable files.
Quoted 4 lines
Document the frozen size and the permission requirements.
Signature · 1 line
Pasha
Re: [PATCH v3 09/10] mm/shmem_luo: Allow retrieving tmpfs files with O_RDONLY
In reply to David MatlackQuoted 17 lines
Allow userspace to pass
O_RDONLYin structliveupdate_shmem_retrieve_argto retrieve a tmpfs file read-only.
open()requires write access forO_TMPFILE, so create the file writable like before, then reopen it read-only withdentry_open()using the flags of the writable file, whichfile_open_root()normalized, and drop the writable file. Likeopen(), which grants the requested access to a new file regardless of its mode, don't check permission for the read-only open.Retrieving a file read-only allows a restored binary to be executed before the session is finished. LUO keeps the retrieved file open until then, and executing a binary that is open for writing fails with
-ETXTBSY. Being able to execute restored binaries right away matters when they are needed to finish restoring the system, e.g. to bring up the services that retrieve and finish the remaining sessions.
Quoted 2 lines
Signature · 1 line
Pasha
Re: [PATCH v3 10/10] selftests/liveupdate: Add a test for preserving tmpfs files
In reply to David MatlackQuoted 50 lines
Add
luo_tmpfs, a kexec test for preserving tmpfs files and memfds, and for retrieving tmpfs files onto a tmpfs mount with structliveupdate_shmem_retrieve_arg.Stage 1 preserves the source files of each test case. Stage 2 runs each test case in its own directory, with its own tmpfs mounts, so that block usage can be checked exactly with
statfs()andst_blocks. The test cases cover:- The restored file: contents, size, position, seals and holes, for linked and unlinked source files.
- Creating the file: it is created like with
open(O_TMPFILE), with the requested mode minus the umask, owned by the caller, and not linked until userspace links it, unless it asked forO_EXCL. AlsoO_RDONLYandO_WRONLYfiles, otheropen()flags such asO_APPEND,O_NONBLOCKandO_CLOEXEC, read-only mounts, and directories that the caller can't write to.- Preserving files that can't be written to: a binary while it is executed, through a read-only fd, files on read-only mounts and superblocks, and immutable files, including sparse ones, whose holes are filled. Preserving someone else's file that the caller can't write to fails, and so does preserving a file through a write-only fd or an append-only file.
- Executing restored binaries: a restored binary can be executed before the session is finished if it was retrieved with
O_RDONLY, but not if it was retrieved for writing, since LUO keeps the file open until then. These test cases execute copies of the test binary.- Block accounting: the file fits a mount of exactly its size, and a large folio that extends past the end of the file is charged in full. Running out of space, or hitting the memcg limit, fails permanently, and leaves nothing charged to the mount or the memcg. The session can still be finished.
- memfds: they are only retrieved as memfds, with their seals, and files from tmpfs mounts only onto a tmpfs mount.
- Interactions with LUO: invalid arguments, which can be retried, retrieving a token twice, and outgoing sessions.
Also check that the size of a preserved file is frozen, and that the test doesn't trigger kernel warnings.
Enable
CONFIG_TRANSPARENT_HUGEPAGEfor the large folio test cases,CONFIG_MEMCGfor the memcg test case andCONFIG_TMPFS_XATTRforFS_IOC_SETFLAGS. Those test cases are skipped without them.
Quoted 5 lines
tmpfs quotas are not tested, since the test runs as root, which is not subject to quota limits.
Overall, I like the series. As a follow-up, it would also be nice to add non-kexec tests to liveupdate.c covering preserve/unpreserve/freeze and restrictions on various tmpfs files without rebooting.
While that won't test retrieval, liveupdate.c runs as part of regular kselftest by pretty much everyone, unlike the much heavier vmtest.sh run.
Signature · 1 line
Pasha
[email protected]
AI reviewRe: [PATCH v3 01/10] liveupdate: Pass user argument to file preserve and retrieve
In reply to David MatlackThank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Low] liveupdate: karg __free(kfree) declared as NULL at top of luo_copy_user_arg() --
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Low] liveupdate: karg __free(kfree) declared as NULL at top of luo_copy_user_arg() --
commit c4f18d427b088994ba26ccc665552744e8d63e5a
Author: Pasha Tatashin <[email protected]>
liveupdate: Pass user argument to file preserve and retrieve
This commit extends LIVEUPDATE_SESSION_PRESERVE_FD and LIVEUPDATE_SESSION_RETRIEVE_FD with an optional user-space argument pointer, allowing file handlers to specify and receive handler-specific argument structures during file preservation and retrieval.
Quoted 4 lines
kernel/liveupdate/luo_file.c +0
-0
[ ... ]
Quoted 18 lines
kernel/liveupdate/luo_file.c +14
-0@@ -202,11 +203,45 @@ static bool luo_token_is_used(struct luo_file_set *file_set, u64 token)return false; } +static int luo_copy_user_arg (u64 user_arg,const void **argp, +const struct liveupdate_arg_spec *spec) +{ + void *karg __free(kfree) = NULL; +void __user *uptr; +u32 user_size; +int err; + +if (!user_arg) +return 0 ; +if (!spec->size) +return -EOPNOTSUPP ; + + uptr =u64_to_user_ptr (user_arg);
kernel/liveupdate/luo_file.c static bool luo_token_is_used(struct luo_file_set *file_set, u64 token) · line 229
Quoted 10 lines
kernel/liveupdate/luo_file.c +10
-0+if (get_user (user_size, (u32 __user *)uptr)) +return -EFAULT ; +if (user_size < spec->min_size) +return -EINVAL ; +if (user_size >PAGE_SIZE ) +return -E2BIG ; + + karg =kmalloc (spec->size,GFP_KERNEL ); +if (!karg) +return -ENOMEM ;
[Severity: Low]
This isn't a bug, but in luo_copy_user_arg(), karg is declared with __free(kfree) = NULL at the top of the function and assigned later after the early validation checks.
Per the Cleanup API conventions in include/linux/cleanup.h, could karg be defined and initialized in a single statement at the allocation site instead:
void *karg __free(kfree) = kmalloc(spec->size, GFP_KERNEL); if (!karg) return -ENOMEM;
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
[email protected]
AI reviewRe: [PATCH v3 02/10] selftests: liveupdate: Add tests for preserve and retrieve user argument
In reply to David MatlackThank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Low] selftests/liveupdate: zero min_arg.arg in preserve_memfd_with_arg() --
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Low] selftests/liveupdate: zero min_arg.arg in preserve_memfd_with_arg() --
commit 0eae9757b9fca17ee60f5d085e6dce0d9304a977
Author: Pasha Tatashin <[email protected]>
selftests: liveupdate: Add tests for preserve and retrieve user argument
This commit adds libliveupdate helpers for passing user arguments to LIVEUPDATE_SESSION_PRESERVE_FD and LIVEUPDATE_SESSION_RETRIEVE_FD, and adds the preserve_memfd_with_arg test case to verify memfd argument validation and legacy struct size handling.
Quoted 4 lines
tools/testing/selftests/liveupdate/liveupdate.c +0
-0
[ ... ]
Quoted 26 lines
tools/testing/selftests/liveupdate/liveupdate.c +22
-0@@ -530,4 +531,47 @@ TEST_F(liveupdate_device, preserve_many_files)ASSERT_EQ (close (session_fd),0 ); } +/* +* Test Case: Preserve MemFD with User Argument +* +* Verifies that passing a non-zero user argument to memfd preservation fails +* with -EOPNOTSUPP, while passing a zero argument or using the minimum struct +* size without the arg field succeeds. +*/ +TEST_F(liveupdate_device, preserve_memfd_with_arg) +{ +struct liveupdate_session_preserve_fd min_arg = {}; +int session_fd, mem_fd1, mem_fd2; + + self->fd1 =open (LIVEUPDATE_DEV ,O_RDWR ); +if (self->fd1 <0 && errno ==ENOENT ) +SKIP (return,"%s does not exist" ,LIVEUPDATE_DEV ); +ASSERT_GE (self->fd1,0 ); + + session_fd =luo_create_session (self->fd1,"preserve-arg-test" ); +ASSERT_GE (session_fd,0 ); + + mem_fd1 =memfd_create ("arg-memfd-1" ,0 ); +ASSERT_GE (mem_fd1,0 );
tools/testing/selftests/liveupdate/liveupdate.c TEST_F(liveupdate_device, preserve_many_files) · line 565
Quoted 10 lines
tools/testing/selftests/liveupdate/liveupdate.c +10
-0+ +/* Non-zero arg is not supported by memfd handler */ +EXPECT_EQ (luo_session_preserve_fd_arg (session_fd, mem_fd1,0x1111 ,1 ), + -EOPNOTSUPP ); + +/* Legacy struct size (up to token) should zero-fill arg and succeed */ + min_arg.size = offsetof(struct liveupdate_session_preserve_fd , arg); + min_arg.fd = mem_fd1; + min_arg.token =0x1111 ; +ASSERT_EQ (ioctl (session_fd,LIVEUPDATE_SESSION_PRESERVE_FD , &min_arg),0 );
[Severity: Low]
In preserve_memfd_with_arg, min_arg is zero-initialized at declaration:
struct liveupdate_session_preserve_fd min_arg = {};Because min_arg.arg is already 0 in userspace memory when ioctl() is called with LIVEUPDATE_SESSION_PRESERVE_FD, the call would still see min_arg.arg == 0 even if the kernel read the entire struct from userspace instead of truncating at min_arg.size and zero-filling the trailing arg field.
Should min_arg.arg be set to a non-zero value before the ioctl() call so that this subtest verifies the kernel ignores bytes beyond min_arg.size and zero-fills arg?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
[email protected]
AI reviewRe: [PATCH v3 03/10] liveupdate: Allow file handlers to mark retrieve failures as retryable
In reply to David MatlackSashiko has reviewed this patch and found no issues.
Quoted 16 lines
From: Pasha Tatashin <[email protected]>
When
luo_retrieve_file()invokes a handler's .retrieve() callback and it returns an error, LUO currently records the error inluo_file->retrieve_statusand blocks any subsequent retrieve attempts. This is necessary when the handler has already partially consumed or freed the preserved KHO state.However, with user arguments passed to .retrieve(), a handler may fail early while validating caller-supplied parameters (such as a missing argument, an invalid or read-only target file descriptor, or a non-empty target file) before touching any preserved KHO state. In such cases, userspace should be able to fix the argument and retry
LIVEUPDATE_SESSION_RETRIEVE_FD, and if the file is never retrieved, .finish() must still run to release the preserved KHO state.
Quoted 8 lines
Add a retryable output flag to struct
liveupdate_file_op_args. When a handler setsargs->retryable= true on error,luo_retrieve_file()leavesluo_file->retrieve_statusat 0 so the retrieve operation can be retried and .finish() will still clean up the unconsumed preserved state if the session is finished without retrieving the file.Signed-off-by Pasha Tatashin <[email protected]>Signed-off-by David Matlack <[email protected]>
Sashiko has reviewed this patch and found no issues.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
[email protected]
AI reviewRe: [PATCH v3 04/10] mm: Rename memfd_luo to shmem_luo
In reply to David MatlackSashiko has reviewed this patch and found no issues.
Quoted 16 lines
Rename the memfd Live Update file handler from
memfd_luotoshmem_luo, including the source file, its KHO ABI header, its documentation and itsKconfigoption (CONFIG_LIVEUPDATE_MEMFDtoCONFIG_LIVEUPDATE_SHMEM).The handler preserves shmem files. Today those are memfds, but following commits extend it to other files on tmpfs mounts, for which the memfd name is confusing. Preserving memfds created with
MFD_HUGETLB, which are hugetlbfs files, will need a separate handler, so naming the handler after the filesystem that backs the files also avoids having to rename it again then.Don't change the compatible string of the handler, "memfd-v2". It is matched against the preserved state from the previous kernel, so changing it would break live updates from kernels that use the old name. The serialized format doesn't change either.
Quoted 6 lines
Also clarify in the documentation that memfd means a memfd created without
MFD_HUGETLB, and name the ABI documentation after the handler.No functional change intended.
Signed-off-by David Matlack <[email protected]>
Sashiko has reviewed this patch and found no issues.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4
[email protected]
AI reviewRe: [PATCH v3 05/10] liveupdate: Don't WARN if tracking a retrieved file fails
In reply to David MatlackSashiko has reviewed this patch and found no issues.
Quoted 8 lines
Handle a failure to add a retrieved file to
luo_preserved_filesinstead of triggering aWARN_ON(). Make the failure permanent, and keep the file until the file set is finished, like any other retrieved file.
xa_insert()can fail with-ENOMEM, and userspace can trigger that withLIVEUPDATE_SESSION_RETRIEVE_FD, so warning about it lets userspace crash systems withpanic_on_warn.
Quoted 8 lines
The handler has already consumed the preserved state when this happens, so the retrieve can't be retried. Keeping the file ensures that its resources are not released before the file set is finished. Finishing the file set then erases the file from
luo_preserved_files, which does nothing for a file that was never added.Fixes 00d0b372374f ("liveupdate: prevent double management of files")Signed-off-by David Matlack <[email protected]>
Sashiko has reviewed this patch and found no issues.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=5
[email protected]
AI reviewRe: [PATCH v3 06/10] mm/shmem_luo: Don't make early retrieve failures permanent
In reply to David MatlackSashiko has reviewed this patch and found no issues.
Quoted 8 lines
Mark failures in
shmem_luo_retrieve()before it starts restoring the folios as retryable, and return the error without freeing the serialized state. If no retrieve succeeds,finish()frees the preserved folios, like it does for a file that was never retrieved.Nothing has consumed the preserved state before the folios are restored, so the retrieve can be tried again.
Quoted 8 lines
This also fixes leaking the preserved folios when one of these early steps fails, e.g. because the file has seals that this kernel doesn't support. The retrieve freed the serialized state but not the folios, and
finish()then skipped the file because its retrieve had failed. There is no Fixes tag, since the fix relies onargs->retryable, which was only added earlier in this series.Signed-off-by David Matlack <[email protected]>
Sashiko has reviewed this patch and found no issues.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=6
[email protected]
AI reviewRe: [PATCH v3 07/10] mm/shmem_luo: Retrieve tmpfs files onto a tmpfs mount chosen by userspace
In reply to David MatlackSashiko has reviewed this patch and found no issues.
Quoted 17 lines
Retrieve files that were preserved from a tmpfs mount onto a tmpfs mount chosen by userspace, instead of as memfds on the internal tmpfs mount. Add struct
liveupdate_shmem_retrieve_arg, which userspace passes as the retrieve argument ofLIVEUPDATE_SESSION_RETRIEVE_FDto choose the mount. Record which mount a file was on with a newSHMEM_LUO_FLAG_TMPFSflag in structshmem_luo_ser, based on the superblock of the file.
shmem_luoalready accepts unlinked tmpfs files, such as files created withO_TMPFILE, and a following commit also accepts linked ones. The mount such a file was on doesn't exist in the new kernel, and userspace may want to put the file elsewhere anyway.Model struct
liveupdate_shmem_retrieve_argonopen(): The kernel creates a new file as if byopenat(dirfd, ".", O_TMPFILE | flags, mode), so the file gets the owner, permission checks and security labels of any new [ ... ]
Quoted 4 lines
Like the other failures before the folios are restored, problems with the argument and with creating the file can be retried.
Signed-off-by David Matlack <[email protected]>
Sashiko has reviewed this patch and found no issues.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=7
[email protected]
AI reviewRe: [PATCH v3 08/10] mm/shmem_luo: Allow preserving tmpfs files that are still linked
In reply to David MatlackSashiko has reviewed this patch and found no issues.
Quoted 13 lines
Allow preserving tmpfs files that are still linked. To preserve any file, require permission to write to it and the file to be opened for reading, and reject append-only files.
shmem_luoalready accepts any shmem file without links, which includes unlinked tmpfs files and files created withO_TMPFILE. Accepting linked files means that userspace doesn't have to unlink files to preserve them and can keep using them by path until the live update.The path of the file isn't preserved. Like other files from tmpfs mounts, a linked file is retrieved onto a tmpfs mount chosen by userspace, which can then link it at the path it wants.
Quoted 8 lines
While a file is preserved, its size is frozen, so truncating it,
fallocate()and writes past its end fail with-EPERM. For a linked file, [ ... ]
and for immutable files.Document the frozen size and the permission requirements.
Signed-off-by David Matlack <[email protected]>
Sashiko has reviewed this patch and found no issues.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=8
[email protected]
AI reviewRe: [PATCH v3 09/10] mm/shmem_luo: Allow retrieving tmpfs files with O_RDONLY
In reply to David MatlackSashiko has reviewed this patch and found no issues.
Quoted 10 lines
Allow userspace to pass
O_RDONLYin structliveupdate_shmem_retrieve_argto retrieve a tmpfs file read-only.
open()requires write access forO_TMPFILE, so create the file writable like before, then reopen it read-only withdentry_open()using the flags of the writable file, whichfile_open_root()normalized, and drop the writable file. Likeopen(), which grants the requested access to a new file regardless of its mode, don't check permission for the read-only open.
Quoted 8 lines
Retrieving a file read-only allows a restored binary to be executed before the session is finished. LUO keeps the retrieved file open until then, and executing a binary that is open for writing fails with
-ETXTBSY. Being able to execute restored binaries right away matters when they are needed to finish restoring the system, e.g. to bring up the services that retrieve and finish the remaining sessions.Signed-off-by David Matlack <[email protected]>
Sashiko has reviewed this patch and found no issues.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=9
[email protected]
AI reviewRe: [PATCH v3 10/10] selftests/liveupdate: Add a test for preserving tmpfs files
In reply to David MatlackSashiko has reviewed this patch and found no issues.
Quoted 17 lines
Add
luo_tmpfs, a kexec test for preserving tmpfs files and memfds, and for retrieving tmpfs files onto a tmpfs mount with structliveupdate_shmem_retrieve_arg.Stage 1 preserves the source files of each test case. Stage 2 runs each test case in its own directory, with its own tmpfs mounts, so that block usage can be checked exactly with
statfs()andst_blocks. The test cases cover:- The restored file: contents, size, position, seals and holes, for linked and unlinked source files.
- Creating the file: it is created like with
open(O_TMPFILE), with the requested mode minus the umask, owned by the caller, and not linked until userspace links it, unless it asked forO_EXCL. AlsoO_RDONLY
[ ... ]
Quoted 4 lines
tmpfs quotas are not tested, since the test runs as root, which is not subject to quota limits.
Signed-off-by David Matlack <[email protected]>
Sashiko has reviewed this patch and found no issues.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=10
Pratyush Yadav
maintainerRe: [PATCH v3 01/10] liveupdate: Pass user argument to file preserve and retrieve
In reply to David MatlackHi David,
Quoted 26 lines · David Matlack
From: Pasha Tatashin <[email protected]>
Allow userspace to pass a file-handler-specific argument structure when preserving or retrieving an FD via LUO. File handlers may need additional context during preservation or retrieval, such as a target FD, device location, or handler-specific parameters, without introducing separate ioctls for each file type. Whether the argument is required, optional, or unused is defined by the file handler.
Extend struct
liveupdate_session_preserve_fdand
structliveupdate_session_retrieve_fdwith a 64-bit arg field pointing to a handler-defined argument structure that begins with a 32-bit size field. BecauseIOCTL_OPvalidates a minimum size up to the token field, existing callers passing the 16-byte struct remain compatible and have arg zero-filled bycopy_struct_from_user().Add parg and rarg (struct
liveupdate_arg_spec) to
structliveupdate_file_ops, initialized viaLIVEUPDATE_ARG(_struct, _min_last), which records the full struct size and minimum required size while enforcing at compile time that handler argument structures begin with a 32-bit size field. When arg is non-zero, LUO validates the minimum size and copies the user argument viacopy_struct_from_user()up to the handler's expected size and passes the kernel copy in structliveupdate_file_op_args, or returns-EOPNOTSUPPif the handler does not accept an argument.
Quoted 7 lines
The argument only applies to the retrieve that restores the file. Retrieving a token that was already retrieved returns the existing file, so fail with
-EBUSYif a non-zero argument is passed then, instead of silently ignoring it.Signed-off-by Pasha Tatashin <[email protected]>Signed-off-by David Matlack <[email protected]>
LGTM for the most part, only a couple nitpicks below.
[...]
include/uapi/linux/liveupdate.h struct liveupdate_session_preserve_fd { · line 184
Quoted 8 lines
include/uapi/linux/liveupdate.h +4
-0@@ -173,12 +178,17 @@ struct liveupdate_session_preserve_fd {* attempts to retrieve the token fail with the same error code. Failed * retrieves are not retried. * +* Retrieving a token that was already retrieved successfully returns a new file +* descriptor for the same file. The @arg only applies to the retrieve that +* restores the file, so passing a non-zero @arg then fails with -EBUSY. +*
Hmm, let's see how that plays out in practice. I would guess that code which does multiple retrieves isn't smart enough to know the file was already retrieved by someone else.
Anyway, that's not really a kernel problem, so no need to change anything. Just thinking out loud.
[...]
Quoted 10 lines
kernel/liveupdate/luo_file.c +1
-0/** * luo_preserve_file - Initiate the preservation of a file descriptor. * @file_set: The file_set to which the preserved file will be added. * @token: A unique, user-provided identifier for the file. * @fd: The file descriptor to be preserved. +* @arg: User-space pointer to the handler argument structure, or 0. * * This function orchestrates the first phase of preserving a file. Upon entry, * it takes a reference to the 'struct file' via fget(), effectively making LUO
kernel/liveupdate/luo_file.c static bool luo_token_is_used(struct luo_file_set *file_set, u64 token) · line 264
Quoted 10 lines
kernel/liveupdate/luo_file.c +4
-2@@ -222,9 +257,11 @@ static bool luo_token_is_used(struct luo_file_set *file_set, u64 token)* (allocates if needed). * 3. Iterates through registered handlers, calling can_preserve() to find one * compatible with the given @fd. -* 4. Calls the handler's .preserve() operation, which saves the file's state +* 4. Copies the user argument structure (if @arg is non-zero) according to the +* handler's parg specification. +* 5. Calls the handler's .preserve() operation, which saves the file's state * and returns an opaque private data handle. -* 5. Adds the new instance to the file_set's internal list. +* 6. Adds the new instance to the file_set's internal list.
TBH, I think this whole list is pointless. There is no reason to list what the code does in a comment. It's only purpose would be to go stale. The comment should be a high level description of the function, and the details are already in the code.
It would be very nice if you add a preparatory patch deleting the whole thing, but since it isn't something added by your patch, I won't insist on it. It's optional for this series.
Quoted 11 lines
kernel/liveupdate/luo_file.c +1
-0* * On success, LUO takes a reference to the 'struct file' and considers it * under its management until it is unpreserved or finished. @@ -239,10 +276,12 @@ static bool luo_token_is_used(struct luo_file_set *file_set, u64 token)* -EBADF if the file descriptor is invalid. * -ENOSPC if the file_set is full. * -ENOENT if no compatible handler is found. +* -EOPNOTSUPP if @arg is non-zero and the handler does not accept an arg. * -ENOMEM on memory allocation failure. * Other erros might be returned by .preserve(). */
[...]
kernel/liveupdate/luo_session.c static int luo_session_preserve_fd(struct luo_session *session, · line 271
Quoted 11 lines
kernel/liveupdate/luo_session.c +2
-1@@ -267,7 +267,8 @@ static int luo_session_preserve_fd(struct luo_session *session,int err;guard (mutex)(&session->mutex); - err =luo_preserve_file (&session->file_set, argp->token, argp->fd); + err =luo_preserve_file (&session->file_set, argp->token, argp->fd, + argp->arg);
Nit: I would prefer to start marking this as __user from here on out, instead of keeping it u64 and hiding the fact that this is a user pointer.
So luo_preserve_file() should take a "const void __user *arg" as argument, instead of "u64 arg". And here we should pass u64_to_user_ptr(argp->arg). Same for retrieve.
Quoted 13 lines
kernel/liveupdate/luo_session.c +2
-1if (err)return err;@@ -290,7 +291,8 @@ static int luo_session_retrieve_fd(struct luo_session *session,return argp->fd;mutex_lock (&session->mutex); - err =luo_retrieve_file (&session->file_set, argp->token, &file); + err =luo_retrieve_file (&session->file_set, argp->token, argp->arg, + &file);mutex_unlock (&session->mutex);if (err <0 ) goto err_put_fd;
Signature · 2 lines
Regards, Pratyush Yadav
Re: [PATCH v3 02/10] selftests: liveupdate: Add tests for preserve and retrieve user argument
In reply to David MatlackQuoted 7 lines · David Matlack
From: Pasha Tatashin <[email protected]>
Verify the user argument support in
LIVEUPDATE_SESSION_PRESERVE_FDandLIVEUPDATE_SESSION_RETRIEVE_FD. Addluo_session_preserve_fd_arg()andluo_session_retrieve_fd_arg()helpers to libliveupdate so tests can pass a handler-specific argument.
Quoted 7 lines
Add a
preserve_memfd_with_argtest case to verify that memfd preservation rejects a non-zero argument with-EOPNOTSUPP, accepts an explicit zero argument, and remains compatible with callers passing the legacy struct size up to the token field.Signed-off-by Pasha Tatashin <[email protected]>Signed-off-by David Matlack <[email protected]>
[...]
Signature · 2 lines
Regards, Pratyush Yadav
Pratyush Yadav
maintainerRe: [PATCH v3 03/10] liveupdate: Allow file handlers to mark retrieve failures as retryable
In reply to David MatlackQuoted 16 lines · David Matlack
From: Pasha Tatashin <[email protected]>
When
luo_retrieve_file()invokes a handler's .retrieve() callback and it returns an error, LUO currently records the error inluo_file->retrieve_statusand blocks any subsequent retrieve attempts. This is necessary when the handler has already partially consumed or freed the preserved KHO state.However, with user arguments passed to .retrieve(), a handler may fail early while validating caller-supplied parameters (such as a missing argument, an invalid or read-only target file descriptor, or a non-empty target file) before touching any preserved KHO state. In such cases, userspace should be able to fix the argument and retry
LIVEUPDATE_SESSION_RETRIEVE_FD, and if the file is never retrieved, .finish() must still run to release the preserved KHO state.
Quoted 5 lines
Add a retryable output flag to struct
liveupdate_file_op_args. When a handler setsargs->retryable= true on error,luo_retrieve_file()leavesluo_file->retrieve_statusat 0 so the retrieve operation can be retried and .finish() will still clean up the unconsumed preserved state if the session is finished without retrieving the file.
Should we give some sort of signal to userspace whether the error is retriable or not? How would it know it can or should try again?
Quoted 3 lines
Signed-off-by Pasha Tatashin <[email protected]>Signed-off-by David Matlack <[email protected]>
[...]
Signature · 2 lines
Regards, Pratyush Yadav
Re: [PATCH v3 04/10] mm: Rename memfd_luo to shmem_luo
In reply to David MatlackQuoted 21 lines · David Matlack
Rename the memfd Live Update file handler from
memfd_luotoshmem_luo, including the source file, its KHO ABI header, its documentation and itsKconfigoption (CONFIG_LIVEUPDATE_MEMFDtoCONFIG_LIVEUPDATE_SHMEM).The handler preserves shmem files. Today those are memfds, but following commits extend it to other files on tmpfs mounts, for which the memfd name is confusing. Preserving memfds created with
MFD_HUGETLB, which are hugetlbfs files, will need a separate handler, so naming the handler after the filesystem that backs the files also avoids having to rename it again then.Don't change the compatible string of the handler, "memfd-v2". It is matched against the preserved state from the previous kernel, so changing it would break live updates from kernels that use the old name. The serialized format doesn't change either.
Also clarify in the documentation that memfd means a memfd created without
MFD_HUGETLB, and name the ABI documentation after the handler.No functional change intended.
[...]
kernel/liveupdate/Kconfig config LIVEUPDATE · line 72
Quoted 10 lines
kernel/liveupdate/Kconfig +1
-1@@ -69,7 +69,7 @@ config LIVEUPDATE If unsure, say N. -config LIVEUPDATE_MEMFD +config LIVEUPDATE_SHMEM
Update the description too?
LGTM otherwise.
Quoted 3 lines
kernel/liveupdate/Kconfig +0
-0bool "Live update support for memfd" depends on LIVEUPDATE depends on MEMFD_CREATE
[...]
Signature · 2 lines
Regards, Pratyush Yadav
Pratyush Yadav
maintainerRe: [PATCH v3 05/10] liveupdate: Don't WARN if tracking a retrieved file fails
In reply to David MatlackQuoted 26 lines · David Matlack
Handle a failure to add a retrieved file to
luo_preserved_filesinstead of triggering aWARN_ON(). Make the failure permanent, and keep the file until the file set is finished, like any other retrieved file.
xa_insert()can fail with-ENOMEM, and userspace can trigger that withLIVEUPDATE_SESSION_RETRIEVE_FD, so warning about it lets userspace crash systems withpanic_on_warn.The handler has already consumed the preserved state when this happens, so the retrieve can't be retried. Keeping the file ensures that its resources are not released before the file set is finished. Finishing the file set then erases the file from
luo_preserved_files, which does nothing for a file that was never added.Fixes 00d0b372374f ("liveupdate: prevent double management of files")Signed-off-by David Matlack <[email protected]>Acked-by Pasha Tatashin <[email protected]>--- kernel/liveupdate/luo_file.c | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-)kernel/liveupdate/luo_file.c +0
-0
kernel/liveupdate/luo_file.c int luo_retrieve_file(struct luo_file_set *file_set, u64 token, u64 arg, · line 690
Quoted 7 lines
kernel/liveupdate/luo_file.c +4
-0@@ -684,14 +684,25 @@ int luo_retrieve_file(struct luo_file_set *file_set, u64 token, u64 arg,return err; } + err =xa_insert (&luo_preserved_files, +luo_get_id (luo_file->fh, args.file), args.file, +GFP_KERNEL ); +if (err) {
I think you should do a WARN_ON_ONCE(err == -EBUSY) here. Because if someone can somehow retrieve the same file twice, something really bad has happened and must be reported.
Quoted 21 lines
kernel/liveupdate/luo_file.c +11
-4+/* +* The handler has consumed the preserved state, so the retrieve +* can't be retried. Keep the file until the file set is +* finished, so that its resources are released at the same +* time as those of other retrieved files. +*/ + luo_file->file = args.file; + luo_file->retrieve_status = err; +return err; + } + luo_file->file = args.file;/* Get reference so we can keep this file in LUO until finish */ get_file (luo_file->file); -WARN_ON (xa_insert (&luo_preserved_files, -luo_get_id (luo_file->fh, luo_file->file), - luo_file->file,GFP_KERNEL )); - *filep = luo_file->file; luo_file->retrieve_status =1 ;
Signature · 2 lines
Regards, Pratyush Yadav
Pratyush Yadav
maintainerRe: [PATCH v3 06/10] mm/shmem_luo: Don't make early retrieve failures permanent
In reply to David MatlackQuoted 8 lines · David Matlack
Mark failures in
shmem_luo_retrieve()before it starts restoring the folios as retryable, and return the error without freeing the serialized state. If no retrieve succeeds,finish()frees the preserved folios, like it does for a file that was never retrieved.Nothing has consumed the preserved state before the folios are restored, so the retrieve can be tried again.
Quoted 6 lines
This also fixes leaking the preserved folios when one of these early steps fails, e.g. because the file has seals that this kernel doesn't support. The retrieve freed the serialized state but not the folios, and
finish()then skipped the file because its retrieve had failed. There is no Fixes tag, since the fix relies onargs->retryable, which was only added earlier in this series.
If the seals don't match, the retrieve is not retryable. It will always fail.
I think the only retriable part is memfd_alloc_file(), although even that is quite unlikely to fail. But I think that also fits pretty well with the most important type of error you want to retry on, which is bad arguments. In later patches, you check the argument on shmem_luo_create_tmpfs_file() or shmem_luo_create_memfd(), so perhaps set retriable in those functions?
[...]
Signature · 2 lines
Regards, Pratyush Yadav
Pasha Tatashin
maintainerRe: [PATCH v3 03/10] liveupdate: Allow file handlers to mark retrieve failures as retryable
In reply to Pratyush YadavQuoted 18 lines · Pratyush Yadav
Quoted 16 lines · David Matlack
From: Pasha Tatashin <[email protected]>
When
luo_retrieve_file()invokes a handler's .retrieve() callback and it returns an error, LUO currently records the error inluo_file->retrieve_statusand blocks any subsequent retrieve attempts. This is necessary when the handler has already partially consumed or freed the preserved KHO state.However, with user arguments passed to .retrieve(), a handler may fail early while validating caller-supplied parameters (such as a missing argument, an invalid or read-only target file descriptor, or a non-empty target file) before touching any preserved KHO state. In such cases, userspace should be able to fix the argument and retry
LIVEUPDATE_SESSION_RETRIEVE_FD, and if the file is never retrieved, .finish() must still run to release the preserved KHO state.
Quoted 8 lines
Quoted 5 lines
Add a retryable output flag to struct
liveupdate_file_op_args. When a handler setsargs->retryable= true on error,luo_retrieve_file()leavesluo_file->retrieve_statusat 0 so the retrieve operation can be retried and .finish() will still clean up the unconsumed preserved state if the session is finished without retrieving the file.Should we give some sort of signal to userspace whether the error is retriable or not? How would it know it can or should try again?
The goal of args->retryable is internal kernel state tracking.
From userspace's perspective, LUO ioctls behave like any other syscall where errno is the signal: userspace decides whether and how to retry based on the error code (e.g. retrying on -EAGAIN, or picking a different mount on -EROFS/-EACCES, or adjusting flags/arguments on -EINVAL).
Pratyush Yadav
maintainerRe: [PATCH v3 07/10] mm/shmem_luo: Retrieve tmpfs files onto a tmpfs mount chosen by userspace
In reply to David MatlackHi David,
Quoted 9 lines · David Matlack
Retrieve files that were preserved from a tmpfs mount onto a tmpfs mount chosen by userspace, instead of as memfds on the internal tmpfs mount. Add struct
liveupdate_shmem_retrieve_arg, which userspace passes as the retrieve argument ofLIVEUPDATE_SESSION_RETRIEVE_FDto choose the mount. Record which mount a file was on with a newSHMEM_LUO_FLAG_TMPFSflag in structshmem_luo_ser, based on the superblock of the file.
shmem_luoalready accepts unlinked tmpfs files, such as files created withO_TMPFILE, and a following commit also accepts linked ones. The
Nit: that's a bug, not a supported feature (well, before this patch at least).
Quoted 20 lines
mount such a file was on doesn't exist in the new kernel, and userspace may want to put the file elsewhere anyway.
Model struct
liveupdate_shmem_retrieve_argonopen(): The kernel creates a new file as if byopenat(dirfd, ".", O_TMPFILE | flags, mode), so the file gets the owner, permission checks and security labels of any new file in that directory, and is charged to that mount. The kernel then fills the file with the preserved contents, size, position and seals before returning it, so nothing else can access the file before it is complete. Userspace links the file at the path it wants withlinkat(), or passesO_EXCLto keep it unlinked. Neither the path nor any other inode attribute of the file is part of the preserved state, which keeps them out of the KHO ABI.Pass the flags from userspace to
file_open_root(), so that the VFS checks them like foropen().file_open_root()silently ignores unknown flags and mode bits, so reject those likeopenat2()does. Also rejectO_PATH, with whichfile_open_root()would ignoreO_TMPFILEand open the directory.
Quoted 3 lines
Keep the type of the file: memfds can only be retrieved as memfds, and files on tmpfs mounts only onto tmpfs mounts, so that e.g. a file can't be moved off the mount whose size limit and quotas it was subject
Nit: that's not true. It only can't be moved to the internal mount. All other tmpfs mounts are fair game. You can take 5 files from a single mount on preserve, and put them on 5 different mounts on retrieve. Nothing wrong with that, just that this claim isn't exactly accurate.
Quoted 4 lines
to. Passing the argument when retrieving a memfd, or not passing it when retrieving a file from a tmpfs mount, fails with
-EINVAL. Unlinked tmpfs files were retrieved as memfds until now, but nothing relies on that, as the LUO uAPI is not stable yet.
LUO uAPI _is_ stable. We have to keep backwards compatibility. It is only the preservation ABI that is not stable.
And as I said above, that was a bug and not a supported feature. This patch happens to fix that bug, and hopefully no user was relying on it. If a user is relying on it, we'd either have to ask them to move to the new uAPI or reinstate the old bug (as long as the end result is reasonable and secure). That's the promise we have to keep with uAPI.
Quoted 51 lines
Like the other failures before the folios are restored, problems with the argument and with creating the file can be retried.
Signed-off-by David Matlack <[email protected]>--- Documentation/mm/shmem_preservation.rst | 8 +- include/linux/kho/abi/shmem.h | 21 ++- include/uapi/linux/liveupdate.h | 35 +++++ kernel/liveupdate/Kconfig | 6 +- mm/internal.h | 1 + mm/shmem.c | 9 ++ mm/shmem_luo.c | 180 ++++++++++++++++++++---- 7 files changed, 221 insertions(+), 39 deletions(-)Documentation/mm/shmem_preservation.rst +4
-4@@ -1,11 +1,11 @@ .. SPDX-License-Identifier: GPL-2.0-or-later -========================== -Memfd Preservation via LUO -========================== +==================================== +Memfd and Tmpfs Preservation via LUO +==================================== .. kernel-doc:: mm/shmem_luo.c - :doc: Memfd Preservation via LUO + :doc: Memfd and Tmpfs Preservation via LUO Memfd Preservation ABI ======================include/linux/kho/abi/shmem.h +2
-2@@ -18,8 +18,8 @@* DOC: shmem Live Update ABI * * The shmem file handler, which preserves memfds created without -* ``MFD_HUGETLB``, uses the ABI defined below for preserving their state across -* a kexec reboot using the LUO. +* ``MFD_HUGETLB`` and tmpfs files, uses the ABI defined below for preserving +* their state across a kexec reboot using the LUO. * * The state is serialized into a packed structure `struct shmem_luo_ser` * which is handed over to the next kernel via the KHO mechanism.
include/linux/kho/abi/shmem.h struct shmem_luo_folio_ser { · line 77
Quoted 10 lines
include/linux/kho/abi/shmem.h +6
-1@@ -69,12 +69,25 @@ struct shmem_luo_folio_ser { F_SEAL_EXEC)/** -* struct shmem_luo_ser - Main serialization structure for a memfd. +* SHMEM_LUO_FLAG_TMPFS - The file was on a tmpfs mount. +* +* Set for files that were not on the internal shmem mount that memfds are +* created on. Such a file is retrieved onto a tmpfs mount chosen by userspace, +* instead of as a memfd. +*/
Nit: perhaps the below is slightly better?
Set for files on a user-visible tmpfs mount. Such files can only be retrieved into a tmpfs mount, and never as a memfd.
Quoted 29 lines
include/linux/kho/abi/shmem.h +9
-1+#define SHMEM_LUO_FLAG_TMPFS BIT(0) + +/* The set of flags this version supports. */ +#define SHMEM_LUO_ALL_FLAGS SHMEM_LUO_FLAG_TMPFS + +/** +* struct shmem_luo_ser - Main serialization structure for a memfd or tmpfs file. * @pos: The file's current position (f_pos). * @size: The total size of the file in bytes (i_size). * @seals: The seals present on the memfd. The seals are uABI so it is safe * to directly use them in the ABI. -* @flags: Flags for the file. Unused flag bits must be set to 0. +* @flags: Flags for the file (SHMEM_LUO_FLAG_*). 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 shmem_luo_folio_ser. include/uapi/linux/liveupdate.h +4
-0@@ -247,4 +247,39 @@ struct liveupdate_session_get_name {#define LIVEUPDATE_SESSION_GET_NAME \ _IO(LIVEUPDATE_IOCTL_TYPE, LIVEUPDATE_CMD_SESSION_GET_NAME) +/** +* struct liveupdate_shmem_retrieve_arg - Argument to retrieve a tmpfs file +* @size: Input; sizeof(struct liveupdate_shmem_retrieve_arg) +* @dirfd: Input; A file descriptor for a directory on the tmpfs mount to create
include/uapi/linux/liveupdate.h struct liveupdate_session_get_name { · line 263
Quoted 10 lines
include/uapi/linux/liveupdate.h +10
-0+* the file on. +* @flags: Input; The flags of the file, like the flags argument of open(), +* except for O_TMPFILE, which is implied, and O_PATH, which is not +* allowed. +* @mode: Input; The mode of the file, like the mode argument of open(). +* +* Passed via &struct liveupdate_session_retrieve_fd.arg to retrieve a file that +* was preserved from a tmpfs mount. Such files can only be retrieved with this +* argument, and memfds, i.e. files that were preserved from the kernel's +* internal shmem mount, can only be retrieved without it. Otherwise, the
Nit: "internal shmem mount" is a kernel internal detail, no point of it in user facing documentation.
include/uapi/linux/liveupdate.h struct liveupdate_session_get_name { · line 276
Quoted 13 lines
include/uapi/linux/liveupdate.h +13
-0+* retrieve fails with -EINVAL. +* +* The kernel creates a new file as if by openat(@dirfd, ".", O_TMPFILE | +* @flags, @mode), fills it with the preserved contents, and returns it in +* &struct liveupdate_session_retrieve_fd.fd. Like any O_TMPFILE file, the file +* is not linked into the filesystem. Userspace can link it with linkat(), +* unless @flags contains O_EXCL. +* +* If @flags contains unknown flags or O_PATH, if @mode contains unknown mode +* bits, or if @dirfd is not on a tmpfs mount, the retrieve fails with -EINVAL. +* If the file can't be created, the retrieve fails with the same errors as +* open(). A retrieve that fails before the preserved contents are restored can +* be retried.
How is userspace supposed to tell if preserved contents were restored?
Quoted 147 lines
include/uapi/linux/liveupdate.h +8
-0+*/ +struct liveupdate_shmem_retrieve_arg { +__u32 size; +__s32 dirfd; +__aligned_u64 flags; +__aligned_u64 mode; +}; +#endif /* _UAPI_LIVEUPDATE_H */ kernel/liveupdate/Kconfig +3
-3@@ -70,14 +70,14 @@ config LIVEUPDATE If unsure, say N. config LIVEUPDATE_SHMEM - bool "Live update support for memfd" + bool "Live update support for memfd and tmpfs files" depends on LIVEUPDATE depends on MEMFD_CREATE depends on SHMEM default LIVEUPDATE help - Enable live update support for memfd regions. This allows preserving - memfd-backed memory across kernel live updates. + Enable live update support for memfds and tmpfs files. This allows + preserving their contents across kernel live updates. This can be used to back VM memory with memfds, allowing the guest memory to persist, or for other user workloads needing to preservemm/internal.h +1
-0@@ -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); +bool shmem_inode_on_internal_mount (const struct inode *inode);#ifdef CONFIG_SHRINKER_DEBUG static inline __printf(2, 0) int shrinker_debugfs_name_alloc(mm/shmem.c +9
-0@@ -468,6 +468,15 @@ bool shmem_recalc_inode(struct inode *inode, long alloced, long swapped)return first_swapped; } +/* +* Whether @inode is on the internal shmem mount, which is used by +* shmem_file_setup() and thus by memfd_create(). +*/ +bool shmem_inode_on_internal_mount (const struct inode *inode) +{ +return inode->i_sb == shm_mnt->mnt_sb; +} +bool shmem_charge (struct inode *inode,long pages) {struct address_space *mapping = inode->i_mapping;mm/shmem_luo.c +32
-11@@ -9,19 +9,20 @@*/ /** -* DOC: Memfd Preservation via LUO +* DOC: Memfd and Tmpfs Preservation via LUO * * Overview * ======== * -* Memory file descriptors (memfd) can be preserved over a kexec using the Live -* Update Orchestrator (LUO) file preservation. This allows userspace to -* transfer its memory contents to the next kernel after a kexec. +* Memory file descriptors (memfd) and tmpfs files can be preserved over a kexec +* using the Live Update Orchestrator (LUO) file preservation. This allows +* userspace to transfer its memory contents to the next kernel after a kexec. * * Here, a memfd is a memfd created without ``MFD_HUGETLB``, which is a shmem -* file on the kernel's internal shmem mount. Memfds created with -* ``MFD_HUGETLB`` are hugetlbfs files, which this file handler doesn't -* preserve. Preserving them fails with ``-ENOENT``. +* file on the kernel's internal shmem mount, and a tmpfs file is a file on any +* other tmpfs mount. Memfds created with ``MFD_HUGETLB`` and other files on +* hugetlbfs are not shmem files, and this file handler doesn't preserve them. +* Preserving them fails with ``-ENOENT``. * * The preservation is not intended to be transparent. Only select properties of * the file are preserved. All others are reset to default. The preserved @@ -29,12 +30,13 @@* * .. note:: * The LUO API is not stabilized yet, so the preserved properties of a memfd -* are also not stable and are subject to backwards incompatible changes. +* or tmpfs file are also not stable and are subject to backwards +* incompatible changes. * * Preserved Properties * ==================== * -* The following properties of the memfd are preserved across kexec: +* The following properties of the file are preserved across kexec: * * File Contents * All data stored in the file is preserved. @@ -49,10 +51,12 @@* * File Status Flags * memfds are always opened with ``O_RDWR`` and ``O_LARGEFILE``. This property -* is maintained. +* is maintained. The file status flags of tmpfs files are not preserved. +* Userspace chooses the flags when retrieving them, as described in +* "Retrieving Tmpfs Files" below. * * Seals -* File seals set on the memfd are preserved and re-applied on restore. +* The seals of the file are preserved and re-applied on restore. * Only seals known to this LUO version (see ``SHMEM_LUO_ALL_SEALS``) may * be present; preservation fails with ``-EOPNOTSUPP`` otherwise. * @@ -67,12 +71,50 @@* A memfd can be created with the ``MFD_CLOEXEC`` flag that sets the * ``FD_CLOEXEC`` on the file. This flag is not preserved and must be set * again after restore via ``fcntl()``. +* +* Inode Attributes +* The name, path, owner, mode, timestamps and extended attributes of the +* file are not preserved. +* +* Retrieving Tmpfs Files +* ====================== +* +* A tmpfs file can be retrieved onto any tmpfs mount that userspace chooses. +* The mount that the file was on doesn't exist in the new kernel, and userspace +* may want to put the file elsewhere anyway. +* +* To retrieve a tmpfs file, userspace passes a +* ``struct liveupdate_shmem_retrieve_arg`` to +* ``LIVEUPDATE_SESSION_RETRIEVE_FD``. The argument specifies a directory on the +* target mount, and the flags and mode of the new file. The kernel creates the +* file as if by ``openat(dirfd, ".", O_TMPFILE | flags, mode)``, so the file
mm/shmem_luo.c line 100
Quoted 10 lines
mm/shmem_luo.c +10
-0+* gets the same flags, owner, permission checks and security labels as any +* other new file in that directory. The kernel then fills the file with the +* preserved contents, size, position and seals before it returns the file, so +* nothing else can access the file before it is complete. +* +* Like any ``O_TMPFILE`` file, the new file is not linked into the filesystem. +* Userspace can link it at the path it wants with ``linkat()``, unless it asked +* for ``O_EXCL``. Like with ``open()``, the file must be opened for writing. +* LUO keeps the file open until the session is finished, so a retrieved binary +* can't be executed until then.
Nit: this information seems to be repeated in the uAPI documentation. Perhaps keep it only there and refer to it from here?
Quoted 39 lines
mm/shmem_luo.c +14
-0+* +* The blocks of the file are charged to the target mount, so the size limit +* and quotas of the mount apply. If the file doesn't fit, the retrieve fails +* permanently, since the preserved contents have already been restored at that +* point. If the argument is invalid or the file can't be created, the retrieve +* fails before the preserved contents are restored, so it can be retried. +* +* memfds can only be retrieved as memfds, and files on tmpfs mounts can only be +* retrieved onto tmpfs mounts. Retrieving a memfd with the argument, or a tmpfs +* file without it, fails with ``-EINVAL``. */ #define pr_fmt (fmt) KBUILD_MODNAME ": " fmt#include <linux/bits.h> #include <linux/err.h> +#include <linux/fcntl.h> #include <linux/file.h> #include <linux/io.h> #include <linux/kexec_handover.h> @@ -81,6 +123,7 @@#include <linux/shmem_fs.h> #include <linux/vmalloc.h> #include <linux/memfd.h> +#include <uapi/linux/magic.h> #include <uapi/linux/memfd.h> #include "internal.h" @@ -299,6 +342,8 @@ static int shmem_luo_preserve(struct liveupdate_file_op_args *args) ser->size = inode_size; ser->seals = seals; +if (!shmem_inode_on_internal_mount (inode)) + ser->flags |=SHMEM_LUO_FLAG_TMPFS ; err =shmem_luo_preserve_folios (args->file, &ser->folios, &folios_ser, &nr_folios);@@ -515,6 +560,92 @@ static int shmem_luo_retrieve_folios(struct file *file,return err;
mm/shmem_luo.c static int shmem_luo_retrieve_folios(struct file *file, · line 570
Quoted 10 lines
mm/shmem_luo.c +8
-0} +/* Create the memfd to retrieve a file from the internal shmem mount into. */ +static struct file *shmem_luo_create_memfd (const struct liveupdate_shmem_retrieve_arg *arg, +unsigned int seals) +{ +struct file *file; +int err; + +/* memfds are retrieved as memfds, so there is nothing to choose. */
Nit: I can't make any sense of this comment. "memfds are retrieved as memfds": well I'd hope so. "there is nothing to choose": ... what are we choosing?
Perhaps something simpler like "no arguments needed with memfds". Something along these lines.
Quoted 64 lines
mm/shmem_luo.c +64
-0+if (arg) +return ERR_PTR (-EINVAL ); + +/* +* The seals are preserved. Allow sealing here so they can be added +* later. +*/ + file =memfd_alloc_file ("" ,MFD_ALLOW_SEALING ); +if (IS_ERR (file)) { +pr_err ("failed to setup file: %pe\n" , file); +return file; + } + + err =memfd_add_seals (file, seals); +if (err) { +pr_err ("failed to add seals: %pe\n" ,ERR_PTR (err)); +fput (file); +return ERR_PTR (err); + } + +return file; +} + +/* +* Create the file to retrieve a file from a tmpfs mount into, on the tmpfs +* mount chosen by userspace. +*/ +static struct file *shmem_luo_create_tmpfs_file (const struct liveupdate_shmem_retrieve_arg *arg, +unsigned int seals) +{ +struct file *file; +int err; + +if (!arg) +return ERR_PTR (-EINVAL ); + +/* +* Like openat2(), reject unknown flags and mode bits instead of +* ignoring them. Also reject O_PATH, which would make file_open_root() +* ignore O_TMPFILE and open the directory instead of creating a file. +*/ +if (arg->flags & ~(u64 )VALID_OPEN_FLAGS || arg->flags &O_PATH || + arg->mode & ~(u64 )S_IALLUGO ) +return ERR_PTR (-EINVAL ); + +CLASS (fd_raw, dir)(arg->dirfd); +if (fd_empty (dir)) +return ERR_PTR (-EBADF ); + +if (fd_file (dir)->f_path.dentry->d_sb->s_magic !=TMPFS_MAGIC ) +return ERR_PTR (-EINVAL ); + +/* +* Create the file like open(O_TMPFILE) does, so that it gets the same +* flag checks, permission checks, owner and security labels. +*/ + file =file_open_root (&fd_file (dir)->f_path,"." , +O_TMPFILE |O_LARGEFILE | arg->flags, arg->mode); +if (IS_ERR (file)) +return file; + +/* +* Nothing else can access the new file yet, so set the preserved seals +* directly. memfd_add_seals() refuses to, since files on tmpfs mounts
mm/shmem_luo.c static int shmem_luo_retrieve_folios(struct file *file, · line 644
Quoted 10 lines
mm/shmem_luo.c +10
-0+* are created with F_SEAL_SEAL. +*/ +if (seals &F_SEAL_WRITE ) { + err =mapping_deny_writable (file->f_mapping); +if (err) { +fput (file); +return ERR_PTR (err); + } + } +SHMEM_I (file_inode (file))->seals = seals;
No, no, no. If a tmpfs payload has seals other than F_SEAL_SEAL, it is _invalid_ and must be rejected. Let's not do things on retrieve that can't be done on a normal file.
Quoted 8 lines
mm/shmem_luo.c +4
-0+ +return file; +} +static int shmem_luo_retrieve (struct liveupdate_file_op_args *args) {struct shmem_luo_folio_ser *folios_ser;
mm/shmem_luo.c static int shmem_luo_retrieve(struct liveupdate_file_op_args *args) · line 669
Quoted 8 lines
mm/shmem_luo.c +3
-2@@ -533,25 +664,18 @@ static int shmem_luo_retrieve(struct liveupdate_file_op_args *args)*/ args->retryable = true; -/* Make sure the file only has seals supported by this version. */ -if (ser->seals & ~SHMEM_LUO_ALL_SEALS ) +/* Make sure the file only has seals and flags this version supports. */ +if ((ser->seals & ~SHMEM_LUO_ALL_SEALS ) || + (ser->flags & ~SHMEM_LUO_ALL_FLAGS ))
As I said above, the set of valid seals depends on memfd or tmpfs, so it should be checked in their respective functions.
This also isn't backwards compatible, but we haven't solved that problem yet so it's fine for now.
mm/shmem_luo.c static int shmem_luo_retrieve(struct liveupdate_file_op_args *args) · line 672
Quoted 10 lines
mm/shmem_luo.c +1
-7return -EOPNOTSUPP ; -/* -* The seals are preserved. Allow sealing here so they can be added -* later. -*/ - file =memfd_alloc_file ("" ,MFD_ALLOW_SEALING ); -if (IS_ERR (file)) { -pr_err ("failed to setup file: %pe\n" , file); +/* Retrieve memfds as memfds, and other files onto a tmpfs mount. */
Nit: this comment doesn't add any value.
Quoted 31 lines
mm/shmem_luo.c +6
-8+if (ser->flags &SHMEM_LUO_FLAG_TMPFS ) + file =shmem_luo_create_tmpfs_file (args->arg, ser->seals); +else + file =shmem_luo_create_memfd (args->arg, ser->seals); +if (IS_ERR (file))return PTR_ERR (file); - } - - err =memfd_add_seals (file, ser->seals); -if (err) { -pr_err ("failed to add seals: %pe\n" ,ERR_PTR (err)); - goto put_file; - }vfs_setpos (file, ser->pos,MAX_LFS_FILESIZE );i_size_write (file_inode (file), ser->size);@@ -579,7 +703,6 @@ static int shmem_luo_retrieve(struct liveupdate_file_op_args *args) free_ser:kho_restore_free (ser); -put_file:fput (file);return err; }@@ -605,6 +728,7 @@ static const struct liveupdate_file_ops shmem_luo_file_ops = { .unpreserve = shmem_luo_unpreserve, .can_preserve = shmem_luo_can_preserve, .get_id = shmem_luo_get_id, + .rarg = LIVEUPDATE_ARG(struct liveupdate_shmem_retrieve_arg, mode), .owner = THIS_MODULE, };
Signature · 2 lines
Regards, Pratyush Yadav
Pratyush Yadav
maintainerRe: [PATCH v3 08/10] mm/shmem_luo: Allow preserving tmpfs files that are still linked
In reply to David MatlackHi David,
Quoted 8 lines · David Matlack
Allow preserving tmpfs files that are still linked. To preserve any file, require permission to write to it and the file to be opened for reading, and reject append-only files.
shmem_luoalready accepts any shmem file without links, which includes unlinked tmpfs files and files created withO_TMPFILE. Accepting linked files means that userspace doesn't have to unlink files to preserve them and can keep using them by path until the live update.
No, no. As I wrote in the previous patch too, this is a *bug*, and not "supported". Please don't treat it as such. Tell your LLM to also not treat it as such ;-)
This patch doesn't do "Allow preserving tmpfs files that are still linked", it does "Allow preserving tmpfs files".
Quoted 11 lines
The path of the file isn't preserved. Like other files from tmpfs mounts, a linked file is retrieved onto a tmpfs mount chosen by userspace, which can then link it at the path it wants.
While a file is preserved, its size is frozen, so truncating it,
fallocate()and writes past its end fail with-EPERM. For a linked file, this affects everyone who opens the file by its path, so a caller that can only read the file, e.g. a world-readable file in/dev/shm, could use it to break the owner of the file. So require permission to write to the file to preserve it, for all files. LikeFS_IOC_ENABLE_VERITY, don't
That makes sense, but can you do it for memfd as well please, as a preparatory patch?
Quoted 10 lines
require a file opened for writing, so that binaries that are being executed can still be preserved through read-only fds. Also don't require a writable mount, since preserving a file doesn't write to the filesystem. If the size of the file can't change anyway, because it is immutable or on a read-only superblock, also let its owner preserve it. Don't let others do that, since the size could change again once the file is made writable. Like
FS_IOC_ENABLE_VERITY, reject append-only files, since their writers can only append to them, and freezing their size would stop all appends.
Quoted 3 lines
Also like
FS_IOC_ENABLE_VERITY, require the file to be opened for reading. The retrieved file can be read, so otherwise a caller that can only write to a file could preserve it to read its contents.
I've stopped reading it by this point. Sounds too LLM-ey. Please try to make this easier to read for humans.
For example, why are we even talking about FS_IOC_ENABLE_VERITY? I reckon you have it in your chat as an example to draw design from, but then LLMs have a tendency to leak those information in patch logs. I usually spawn a subagent with clean context and a high level description of the goal to review the changes and changelogs, to clean up these things, and then of course review everything manually.
Same thing in your comments.
Please clean them up.
Quoted 20 lines
Preserving a file also fills its holes, but don't require any permission for that. Anyone who can read a tmpfs file can already fill its holes by faulting them in through a read-only mapping, also on read-only mounts and for immutable files.
Document the frozen size and the permission requirements.
Signed-off-by David Matlack <[email protected]>--- mm/shmem_luo.c | 67 +++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 64 insertions(+), 3 deletions(-)mm/shmem_luo.c +0
-0@@ -28,6 +28,24 @@* the file are preserved. All others are reset to default. The preserved * properties are described below.
mm/shmem_luo.c line 39
Quoted 10 lines
mm/shmem_luo.c +9
-0* +* A tmpfs file can be preserved even if it is linked, but its path is not +* preserved. Userspace chooses where to put the file when retrieving it, as +* described in "Retrieving Tmpfs Files" below. +* +* Preserving a file requires it to be opened for reading, since the retrieved +* file can be read. It also requires permission to write to the file, since +* preserving it affects all of its users, as described below. But the file +* doesn't need to be opened for writing. For example, a binary that is being +* executed can be preserved through a read-only file descriptor. The owner of
Why are you talking about preserving binaries being executed? Seems oddly specific. I didn't know that was a use case we cared about.
Quoted 24 lines
mm/shmem_luo.c +17
-0+* a file whose size can't change anyway, because it is immutable or on a +* read-only superblock, may also preserve it. The file can be preserved +* through a read-only mount. Append-only files can't be preserved. +* +* While a file is preserved, its size can't change and no holes can be punched +* into it. Truncating the file, ``fallocate()`` and writes past the end of the +* file fail with ``-EPERM``. This applies to all users of the file, including +* users that open it by its path. +* * .. note:: * The LUO API is not stabilized yet, so the preserved properties of a memfd * or tmpfs file are also not stable and are subject to backwards @@ -298,6 +316,45 @@ static void shmem_luo_unpreserve_folios(struct kho_vmalloc *kho_vmalloc,vfree (folios_ser); } +/* +* Check whether the caller may preserve @file. The retrieved file can be read, +* so require @file to be opened for reading, like FS_IOC_ENABLE_VERITY does. +* +* Preserving the file freezes its size until it is unpreserved, for all users +* of the file, so require permission to write to the file. Like +* FS_IOC_ENABLE_VERITY, don't require a file opened for writing, so that e.g. +* a binary that is being executed can be preserved. Also don't require a
mm/shmem_luo.c static void shmem_luo_unpreserve_folios(struct kho_vmalloc *kho_vmalloc, · line 336
Quoted 10 lines
mm/shmem_luo.c +10
-0+* writable mount, since preserving the file doesn't write to the filesystem. +* If the size of the file can't change anyway, because it is on a read-only +* superblock or immutable, its owner may also preserve it. The size could only +* change again after the file is made writable, which is up to the owner or +* root. Reject append-only files: Their writers can only append to them, and +* freezing the size would stop all appends. +* +* Preserving the file also fills its holes, but that doesn't need any more +* permission: Anyone who can read the file can fill its holes by faulting +* them in through a read-only mapping.
Please rework this whole block, it's unreadable.
Quoted 6 lines
mm/shmem_luo.c +6
-0+*/ +static int shmem_luo_may_preserve (struct file *file) +{ +struct mnt_idmap *idmap =file_mnt_idmap (file); +struct inode *inode =file_inode (file); +int err;
mm/shmem_luo.c static void shmem_luo_unpreserve_folios(struct kho_vmalloc *kho_vmalloc, · line 352
Quoted 10 lines
mm/shmem_luo.c +10
-0+ +if (!(file->f_mode &FMODE_READ )) +return -EBADF ; + +if (IS_APPEND (inode)) +return -EPERM ; + + err =inode_permission (idmap, inode,MAY_WRITE ); +if (err && (sb_rdonly (inode->i_sb) ||IS_IMMUTABLE (inode)) && +inode_owner_or_capable (idmap, inode))
Nit: can you move these conditions up, and directly return 0 there? Much more readable.
Quoted 39 lines
mm/shmem_luo.c +12
-3+return 0 ; + +return err; +} +static int shmem_luo_preserve (struct liveupdate_file_op_args *args) {struct inode *inode =file_inode (args->file);@@ -307,6 +364,11 @@ static int shmem_luo_preserve(struct liveupdate_file_op_args *args)int err =0 , seals;inode_lock (inode); + + err =shmem_luo_may_preserve (args->file); +if (err) + goto err_unlock_inode; +shmem_freeze (inode, true);/* Allocate the main serialization structure in preserved memory */ @@ -362,6 +424,7 @@ static int shmem_luo_preserve(struct liveupdate_file_op_args *args)kho_unpreserve_free (ser); err_unlock:shmem_freeze (inode, false); +err_unlock_inode:inode_unlock (inode);return err; }@@ -710,9 +773,7 @@ static int shmem_luo_retrieve(struct liveupdate_file_op_args *args)static bool shmem_luo_can_preserve (struct liveupdate_file_handler *handler,struct file *file) { -struct inode *inode =file_inode (file); - -return shmem_file (file) && !inode->i_nlink; +return shmem_file (file); }static unsigned long shmem_luo_get_id (struct file *file)
Signature · 2 lines
Regards, Pratyush Yadav
Pratyush Yadav
maintainerRe: [PATCH v3 09/10] mm/shmem_luo: Allow retrieving tmpfs files with O_RDONLY
In reply to David MatlackQuoted 10 lines · David Matlack
Allow userspace to pass
O_RDONLYin structliveupdate_shmem_retrieve_argto retrieve a tmpfs file read-only.
open()requires write access forO_TMPFILE, so create the file writable like before, then reopen it read-only withdentry_open()using the flags of the writable file, whichfile_open_root()normalized, and drop the writable file. Likeopen(), which grants the requested access to a new file regardless of its mode, don't check permission for the read-only open.
Quoted 6 lines
Retrieving a file read-only allows a restored binary to be executed before the session is finished. LUO keeps the retrieved file open until then, and executing a binary that is open for writing fails with
-ETXTBSY. Being able to execute restored binaries right away matters when they are needed to finish restoring the system, e.g. to bring up the services that retrieve and finish the remaining sessions.
Can't you put all your binaries in a separate session, retrieve it as the very first thing, and then finish the session? Then you can execute those and retrieve all the other sessions.
Why do we need to support this in the kernel?
Also, please put the motivation as on the top-ish of the commit message, and the mechanics later. For example, your second paragraph should likely come below this one.
[...]
Signature · 2 lines
Regards, Pratyush Yadav
Pratyush Yadav
maintainerRe: [PATCH v3 10/10] selftests/liveupdate: Add a test for preserving tmpfs files
In reply to David MatlackQuoted 50 lines · David Matlack
Add
luo_tmpfs, a kexec test for preserving tmpfs files and memfds, and for retrieving tmpfs files onto a tmpfs mount with structliveupdate_shmem_retrieve_arg.Stage 1 preserves the source files of each test case. Stage 2 runs each test case in its own directory, with its own tmpfs mounts, so that block usage can be checked exactly with
statfs()andst_blocks. The test cases cover:- The restored file: contents, size, position, seals and holes, for linked and unlinked source files.
- Creating the file: it is created like with
open(O_TMPFILE), with the requested mode minus the umask, owned by the caller, and not linked until userspace links it, unless it asked forO_EXCL. AlsoO_RDONLYandO_WRONLYfiles, otheropen()flags such asO_APPEND,O_NONBLOCKandO_CLOEXEC, read-only mounts, and directories that the caller can't write to.- Preserving files that can't be written to: a binary while it is executed, through a read-only fd, files on read-only mounts and superblocks, and immutable files, including sparse ones, whose holes are filled. Preserving someone else's file that the caller can't write to fails, and so does preserving a file through a write-only fd or an append-only file.
- Executing restored binaries: a restored binary can be executed before the session is finished if it was retrieved with
O_RDONLY, but not if it was retrieved for writing, since LUO keeps the file open until then. These test cases execute copies of the test binary.- Block accounting: the file fits a mount of exactly its size, and a large folio that extends past the end of the file is charged in full. Running out of space, or hitting the memcg limit, fails permanently, and leaves nothing charged to the mount or the memcg. The session can still be finished.
- memfds: they are only retrieved as memfds, with their seals, and files from tmpfs mounts only onto a tmpfs mount.
- Interactions with LUO: invalid arguments, which can be retried, retrieving a token twice, and outgoing sessions.
Also check that the size of a preserved file is frozen, and that the test doesn't trigger kernel warnings.
Enable
CONFIG_TRANSPARENT_HUGEPAGEfor the large folio test cases,CONFIG_MEMCGfor the memcg test case andCONFIG_TMPFS_XATTRforFS_IOC_SETFLAGS. Those test cases are skipped without them.
Quoted 8 lines
tmpfs quotas are not tested, since the test runs as root, which is not subject to quota limits.
Signed-off-by David Matlack <[email protected]>--- tools/testing/selftests/liveupdate/Makefile | 1 + tools/testing/selftests/liveupdate/config | 4 + .../testing/selftests/liveupdate/luo_tmpfs.c | 2055 +++++++++++++++++
Umm, no way I am reading all this...
LLMs are great at generating tests, and I'm not saying there is no value in this. It is just that this massive code dump is unreviewable by humans. It is also unfair to the human reviewer because it took many orders of magnitude less effort to _write_ them with an LLM.
What do you all reckon we should do about this? Trust the LLM to get the testing right? Do we give up human maintenance of tests entirely? Do we do fault injection to test the tests themselves?
Or do we make the tests smaller and more human reviewable?
Quoted 4 lines
.../selftests/liveupdate/run-vmtests.sh | 1 + 4 files changed, 2061 insertions(+) create mode 100644 tools/testing/selftests/liveupdate/luo_tmpfs.c
[...]
Signature · 2 lines
Regards, Pratyush Yadav
Pasha Tatashin
maintainerRe: [PATCH v3 10/10] selftests/liveupdate: Add a test for preserving tmpfs files
Quoted 68 lines · Pratyush Yadav
Quoted 50 lines · David Matlack
Add
luo_tmpfs, a kexec test for preserving tmpfs files and memfds, and for retrieving tmpfs files onto a tmpfs mount with structliveupdate_shmem_retrieve_arg.Stage 1 preserves the source files of each test case. Stage 2 runs each test case in its own directory, with its own tmpfs mounts, so that block usage can be checked exactly with
statfs()andst_blocks. The test cases cover:- The restored file: contents, size, position, seals and holes, for linked and unlinked source files.
- Creating the file: it is created like with
open(O_TMPFILE), with the requested mode minus the umask, owned by the caller, and not linked until userspace links it, unless it asked forO_EXCL. AlsoO_RDONLYandO_WRONLYfiles, otheropen()flags such asO_APPEND,O_NONBLOCKandO_CLOEXEC, read-only mounts, and directories that the caller can't write to.- Preserving files that can't be written to: a binary while it is executed, through a read-only fd, files on read-only mounts and superblocks, and immutable files, including sparse ones, whose holes are filled. Preserving someone else's file that the caller can't write to fails, and so does preserving a file through a write-only fd or an append-only file.
- Executing restored binaries: a restored binary can be executed before the session is finished if it was retrieved with
O_RDONLY, but not if it was retrieved for writing, since LUO keeps the file open until then. These test cases execute copies of the test binary.- Block accounting: the file fits a mount of exactly its size, and a large folio that extends past the end of the file is charged in full. Running out of space, or hitting the memcg limit, fails permanently, and leaves nothing charged to the mount or the memcg. The session can still be finished.
- memfds: they are only retrieved as memfds, with their seals, and files from tmpfs mounts only onto a tmpfs mount.
- Interactions with LUO: invalid arguments, which can be retried, retrieving a token twice, and outgoing sessions.
Also check that the size of a preserved file is frozen, and that the test doesn't trigger kernel warnings.
Enable
CONFIG_TRANSPARENT_HUGEPAGEfor the large folio test cases,CONFIG_MEMCGfor the memcg test case andCONFIG_TMPFS_XATTRforFS_IOC_SETFLAGS. Those test cases are skipped without them.Quoted 8 lines
tmpfs quotas are not tested, since the test runs as root, which is not subject to quota limits.
Signed-off-by David Matlack <[email protected]>--- tools/testing/selftests/liveupdate/Makefile | 1 + tools/testing/selftests/liveupdate/config | 4 + .../testing/selftests/liveupdate/luo_tmpfs.c | 2055 +++++++++++++++++Umm, no way I am reading all this...
LLMs are great at generating tests, and I'm not saying there is no value in this. It is just that this massive code dump is unreviewable by humans. It is also unfair to the human reviewer because it took many orders of magnitude less effort to _write_ them with an LLM.
Quoted 5 lines
What do you all reckon we should do about this? Trust the LLM to get the testing right? Do we give up human maintenance of tests entirely? Do we do fault injection to test the tests themselves?
Or do we make the tests smaller and more human reviewable?
We need good test coverage, but I believe we can break this patch down into smaller changes where tested features are incrementally added.
Quoted 10 lines
Quoted 5 lines
.../selftests/liveupdate/run-vmtests.sh | 1 + 4 files changed, 2061 insertions(+) create mode 100644 tools/testing/selftests/liveupdate/luo_tmpfs.c[...]
Signature · 2 lines
Regards, Pratyush Yadav
Signature · 1 line
Pasha
Pasha Tatashin
maintainerRe: [PATCH v3 09/10] mm/shmem_luo: Allow retrieving tmpfs files with O_RDONLY
In reply to Pratyush YadavQuoted 19 lines · Pratyush Yadav
Quoted 10 lines · David Matlack
Allow userspace to pass
O_RDONLYin structliveupdate_shmem_retrieve_argto retrieve a tmpfs file read-only.
open()requires write access forO_TMPFILE, so create the file writable like before, then reopen it read-only withdentry_open()using the flags of the writable file, whichfile_open_root()normalized, and drop the writable file. Likeopen(), which grants the requested access to a new file regardless of its mode, don't check permission for the read-only open.Quoted 6 lines
Retrieving a file read-only allows a restored binary to be executed before the session is finished. LUO keeps the retrieved file open until then, and executing a binary that is open for writing fails with
-ETXTBSY. Being able to execute restored binaries right away matters when they are needed to finish restoring the system, e.g. to bring up the services that retrieve and finish the remaining sessions.
Quoted 3 lines
Can't you put all your binaries in a separate session, retrieve it as the very first thing, and then finish the session? Then you can execute those and retrieve all the other sessions.
This is a good workaround. I am not opposed to this patch for completeness of functionality, but if we decide to keep it, perhaps it should simply be squashed into an earlier patch. The current diffstat looks bad, as it mostly deletes code that was introduced earlier in this series.
In any case, given Pratyush's suggestion, we could also drop it.
Quoted 12 lines
Why do we need to support this in the kernel?
Also, please put the motivation as on the top-ish of the commit message, and the mechanics later. For example, your second paragraph should likely come below this one.
[...]
Signature · 2 lines
Regards, Pratyush Yadav
Signature · 1 line
Pasha
Re: [PATCH v3 06/10] mm/shmem_luo: Don't make early retrieve failures permanent
In reply to Pratyush YadavQuoted 20 lines · Pratyush Yadav
Quoted 8 lines · David Matlack
Mark failures in
shmem_luo_retrieve()before it starts restoring the folios as retryable, and return the error without freeing the serialized state. If no retrieve succeeds,finish()frees the preserved folios, like it does for a file that was never retrieved.Nothing has consumed the preserved state before the folios are restored, so the retrieve can be tried again.
Quoted 6 lines
This also fixes leaking the preserved folios when one of these early steps fails, e.g. because the file has seals that this kernel doesn't support. The retrieve freed the serialized state but not the folios, and
finish()then skipped the file because its retrieve had failed. There is no Fixes tag, since the fix relies onargs->retryable, which was only added earlier in this series.If the seals don't match, the retrieve is not retryable. It will always fail.
Quoted 6 lines
I think the only retriable part is
memfd_alloc_file(), although even that is quite unlikely to fail. But I think that also fits pretty well with the most important type of error you want to retry on, which is bad arguments. In later patches, you check the argument onshmem_luo_create_tmpfs_file()orshmem_luo_create_memfd(), so perhaps set retriable in those functions?
When retryable is set, LUO leaves the file unretrieved and finish() frees the preserved folios. If the seal failure did not set retryable, finish() would skip the file and the folios would leak.
In v4 I can move the seal checks into shmem_luo_create_memfd() and shmem_luo_create_tmpfs_file(), since the valid seals differ between the two, and reword the commit message so it doesn't suggest that a bad seal can be fixed by retrying.
Re: [PATCH v3 07/10] mm/shmem_luo: Retrieve tmpfs files onto a tmpfs mount chosen by userspace
In reply to Pratyush YadavQuoted 7 lines · Pratyush Yadav
Quoted 5 lines
+ * If @flags contains unknown flags or
O_PATH, if @mode contains unknown mode
+ * bits, or if @dirfd is not on a tmpfs mount, the retrieve fails with-EINVAL.
+ * If the file can't be created, the retrieve fails with the same errors as
+ *open(). A retrieve that fails before the preserved contents are restored can
+ * be retried.How is userspace supposed to tell if preserved contents were restored?
Through the error code, like Pasha said on patch 3. I'll reword this in v4 in terms of errors instead of kernel internals: errors from checking the argument and creating the file, e.g. -EINVAL, -EBADF and the errors that open() returns, can be retried after fixing the argument. Errors while restoring the contents, e.g. -ENOMEM, or -ENOSPC when the contents don't fit on the mount, are permanent, and retrieving the token again returns the same error.
Thanks for the review. I'll address the rest in v4.
Re: [PATCH v3 08/10] mm/shmem_luo: Allow preserving tmpfs files that are still linked
In reply to Pratyush YadavOn 2026-11-04 05:45 PM, Pratyush Yadav wrote:
[...]
Quoted 8 lines
Quoted 5 lines
+ * Preserving a file requires it to be opened for reading, since the retrieved
+ * file can be read. It also requires permission to write to the file, since
+ * preserving it affects all of its users, as described below. But the file
+ * doesn't need to be opened for writing. For example, a binary that is being
+ * executed can be preserved through a read-only file descriptor. The owner ofWhy are you talking about preserving binaries being executed? Seems oddly specific. I didn't know that was a use case we cared about.
It's the reason the check looks at permission on the inode instead of requiring an fd opened for writing: a running binary can't be opened for writing (-ETXTBSY), so requiring a writable fd would make it impossible to preserve. Preserving binaries is a use case we care about, e.g. to carry over the binaries of services across a Live Update. I'll keep the comment shorter and more general in v4.
Re: [PATCH v3 09/10] mm/shmem_luo: Allow retrieving tmpfs files with O_RDONLY
In reply to Pasha TatashinQuoted 8 lines · Pasha Tatashin
Quoted 6 lines · Pratyush Yadav
On Tue, Nov 03 2026, David Matlack wrote:
[...]Can't you put all your binaries in a separate session, retrieve it as the very first thing, and then finish the session? Then you can execute those and retrieve all the other sessions.
Quoted 10 lines
This is a good workaround. I am not opposed to this patch for completeness of functionality, but if we decide to keep it, perhaps it should simply be squashed into an earlier patch. The current diffstat looks bad, as it mostly deletes code that was introduced earlier in this series.
In any case, given Pratyush's suggestion, we could also drop it.
Quoted 2 lines
Why do we need to support this in the kernel?
That would work for binaries, and I agree that use case alone doesn't justify this. But I see O_RDONLY as part of the retrieve interface, not a feature for one use case. The retrieve argument is modeled on openat(), and openat() lets you open an existing file read-only.
open(O_TMPFILE) only requires write access because it creates an empty file, and an empty read-only file is useless. A retrieved file isn't empty, it has the preserved contents, so O_RDONLY makes as much sense as it does for any other existing file. Without it, every retrieved file is writable, even when userspace only needs to read it.
Preserve already works the same way: a file can be preserved through a read-only fd, as long as the caller has permission to write to it.
If we keep it, I'll squash it into patch 7 in v4, so retrieve supports O_RDONLY from the start. The commit message will lead with the openat() reasoning and keep binaries as just an example.
Re: [PATCH v3 10/10] selftests/liveupdate: Add a test for preserving tmpfs files
In reply to Pasha TatashinQuoted 11 lines · Pasha Tatashin
Quoted 10 lines · Pratyush Yadav
On Tue, Nov 03 2026, David Matlack wrote:
[...]Umm, no way I am reading all this...
LLMs are great at generating tests, and I'm not saying there is no value in this. It is just that this massive code dump is unreviewable by humans. It is also unfair to the human reviewer because it took many orders of magnitude less effort to _write_ them with an LLM.
Quoted 8 lines
Quoted 5 lines
What do you all reckon we should do about this? Trust the LLM to get the testing right? Do we give up human maintenance of tests entirely? Do we do fault injection to test the tests themselves?
Or do we make the tests smaller and more human reviewable?
We need good test coverage, but I believe we can break this patch down into smaller changes where tested features are incrementally added.
Agreed that it needs to be easier to review, but I'd like to keep the coverage. Here's what I have in mind for v4:
- Move the preserve-side checks that don't need a kexec into liveupdate.c, like Pasha suggested in his earlier reply: which files can be preserved (permissions, read-only and append-only files), the frozen size while preserved, and unpreserve. Those then run with the regular kselftests instead of only in vmtest.sh.
- Split the rest of luo_tmpfs into several smaller commits that add the tested features incrementally, e.g. one for the test infrastructure and basic retrieve, one for the open() flags and creation checks, and one for block accounting. That makes the series longer, but each commit is a few hundred lines and can be reviewed on its own.
It will still be long, but I think having the test coverage is worth it.
Does that sound reasonable?
Re: [PATCH v3 08/10] mm/shmem_luo: Allow preserving tmpfs files that are still linked
In reply to Pratyush YadavQuoted 3 lines · Pratyush Yadav
I usually spawn a subagent with clean context and a high level description of the goal to review the changes and changelogs, to clean up these things, and then of course review everything manually.
This is a very good suggestion. I'll do this for all my patches in the future. Thanks!
Quoted 5 lines · David Matlack
The kernel preserves the contents, size, file position and seals of each file. After the Live Update, userspace retrieves each file onto a tmpfs mount of its choice and links it at the path of its choice, so userspace decides the directory structure, names, owners and permissions of the restored files.
[...]
Quoted 9 lines
4. Retrieve files that were preserved from a tmpfs mount onto a tmpfs mount chosen by userspace, via the new struct
liveupdate_shmem_retrieve_arg. The kernel creates the file as if byopenat(dirfd, ".", O_TMPFILE | flags, mode), so the file gets the same flags, owner, permission checks and security labels as any other new file in that directory, and is charged to that mount. The kernel fills the file before returning it, so nothing else can access the file before it is complete, and userspace links it at the path it wants withlinkat()(patch 7).
No, sorry. That's not what we discussed at LPC and it's not what the RFC and v1 did.
There userspace created the file and LUO filled in the contents. That got turned around in the v1 thread and I think that was a big mistake.
The kernel is now responsible for creating the file and for its permissions but it preserves none of that? No owner, no mode, no xattrs, no security labels, nothing.
Instead userspace passes a struct that duplicates the open() argument list and the kernel runs umask, setgid inheritance, default ACLs and LSM labeling on it. That makes no sense whatsoever. Either the kernel owns the metadata as well or it stays out of it completely.
Spoiler, it should stay out of it.
Userspace is responsible for everything but the contents. Take systemd wanting to preserve a whole tmpfs. We already know how that works and discussed that at LPC with you.
- We generate a manifest of the filesystem ith every file with its owner, mode and xattrs using UAPI.17 manifests (cf. [1]).
We stash it in a memfd and reassemble the tree in userspace after the live update. All it needs from the kernel are the contents. Having the kernel recreate any of that is pointlessly complex.
So instead of LUO handing out a file descriptor for a file it invented, let userspace create the file, O_TMPFILE, O_CREAT, whatever it likes, with the owner, mode and xattrs it wants, and let LUO put the contents into it. That's what the RFC did.
The checks on the target are trivial: a writable fd to an empty shmem file with the inode lock held while you fill it. A tmpfs file can't be sealed, it only ever has F_SEAL_SEAL, so there's nothing to restore but contents and size anyway.
Pasha, your argument for creating the file in the kernel was that nobody can reach it while it's being filled. **That's what O_TMPFILE is for**: Userspace creates it unlinked, LUO fills it, userspace links it. If userspace hands a half-filled file to iommufd that's its own problem.
I also really dislike file_open_root() for this. It's an openat() emulation inside an ioctl which is completely terrible.
It redoes the openat2() validation by hand (reject O_PATH, unknown flags, stray mode bits), force O_LARGEFILE, and because O_TMPFILE insists on write access you open the file O_RDWR, copy the normalized f_flags out of the first struct file, dentry_open() the same path a second time read-only without any permission check and drop the first file again.
So an LSM sees an O_RDWR open for what userspace asked to be O_RDONLY. Whatever path_openat() happens to do for that synthesized flag combination becomes LUO's behavior. That's brittle as fsck. All of it goes away when userspace is responsible for that.
Fwiw, the permission model you end up with on retrieve is write and search permission on the target directory, i.e. vfs_tmpfile()'s inode_permission(dir, MAY_WRITE | MAY_EXEC) plus mnt_want_write() on that mount. The new inode is owned by whoever runs the ioctl with whatever mode they asked for. Nothing about the original file matters. IOW, the kernel has nothing to contribute here that open() doesn't already do.
Really, please make the kernel's job stupidly simple: let userspace get the contents and let it be responsible for the actual complex restore and the filesystem layout.
A hybrid helps absolutely no one.
Quoted 1 line
- Is patch 9 (retrieving tmpfs files with
O_RDONLY) worth merging?
No. With userspace creating the file this doesn't exist.
Quoted 3 lines
are created with mode 0777 (or 0666). Is this the right permission model for preserving files? Should this apply all files or just shmem files?
The preserve side seems fine to me. If you freeze the size of a file for everyone you better be allowed to write to it.
[1]: https://uapi-group.org/specifications/specs/file_manifest/
Quoted 13 lines · Christian Brauner
Quoted 9 lines · David Matlack
4. Retrieve files that were preserved from a tmpfs mount onto a tmpfs mount chosen by userspace, via the new struct
liveupdate_shmem_retrieve_arg. The kernel creates the file as if byopenat(dirfd, ".", O_TMPFILE | flags, mode), so the file gets the same flags, owner, permission checks and security labels as any other new file in that directory, and is charged to that mount. The kernel fills the file before returning it, so nothing else can access the file before it is complete, and userspace links it at the path it wants withlinkat()(patch 7).No, sorry. That's not what we discussed at LPC and it's not what the RFC and v1 did.
[...]
Quoted 4 lines
So instead of LUO handing out a file descriptor for a file it invented, let userspace create the file,
O_TMPFILE,O_CREAT, whatever it likes, with the owner, mode and xattrs it wants, and let LUO put the contents into it. That's what the RFC did.
I'm fine with going back to that in v4. The retrieve argument will just be a file descriptor for the target file, and LUO will only restore the contents and size into it. Everything else about the file is up to userspace.
Quoted 4 lines
The checks on the target are trivial: a writable fd to an empty shmem file with the inode lock held while you fill it. A tmpfs file can't be sealed, it only ever has
F_SEAL_SEAL, so there's nothing to restore but contents and size anyway.
Sounds good. The one other check I plan to add is rejecting a target that LUO is already managing, e.g. a file that is preserved in an outgoing session and whose size is frozen.
LUO will also add the folios while i_size is still 0, and set i_size last. So reads, page faults and memfd_pin_folios() wouldn't see the folios until the file is complete, and if the retrieve fails partway through the target is left empty.
Quoted 2 lines
I also really dislike
file_open_root()for this. It's anopenat()emulation inside an ioctl which is completely terrible.
That is fair, it is ugly. That all goes away in v4.
Quoted 3 lines
Quoted 1 line
- Is patch 9 (retrieving tmpfs files with
O_RDONLY) worth merging?No. With userspace creating the file this doesn't exist.
Ack.
LUO holds a reference to every retrieved file until the session is finished. Since the target has to be writable, executing a restored file will fail with -ETXTBSY until userspace finishes the session. But as Pratyush pointed out, userspace can put binaries in a special session that can be finished immediately so that the binaries can than be executed.
Quoted 2 lines
The preserve side seems fine to me. If you freeze the size of a file for everyone you better be allowed to write to it.
Thanks for taking a look.
Pratyush Yadav
maintainerRe: [PATCH v3 09/10] mm/shmem_luo: Allow retrieving tmpfs files with O_RDONLY
In reply to David MatlackQuoted 31 lines · David Matlack
Quoted 8 lines · Pasha Tatashin
Quoted 6 lines · Pratyush Yadav
On Tue, Nov 03 2026, David Matlack wrote:
[...]Can't you put all your binaries in a separate session, retrieve it as the very first thing, and then finish the session? Then you can execute those and retrieve all the other sessions.
Quoted 10 lines
This is a good workaround. I am not opposed to this patch for completeness of functionality, but if we decide to keep it, perhaps it should simply be squashed into an earlier patch. The current diffstat looks bad, as it mostly deletes code that was introduced earlier in this series.
In any case, given Pratyush's suggestion, we could also drop it.
Quoted 2 lines
Why do we need to support this in the kernel?
That would work for binaries, and I agree that use case alone doesn't justify this. But I see
O_RDONLYas part of the retrieve interface, not a feature for one use case. The retrieve argument is modeled onopenat(), andopenat()lets you open an existing file read-only.
open(O_TMPFILE)only requires write access because it creates an empty file, and an empty read-only file is useless. A retrieved file isn't empty, it has the preserved contents, soO_RDONLYmakes as much sense as it does for any other existing file. Without it, every retrieved file is writable, even when userspace only needs to read it.
Quoted 6 lines
Preserve already works the same way: a file can be preserved through a read-only fd, as long as the caller has permission to write to it.
If we keep it, I'll squash it into patch 7 in v4, so retrieve supports
O_RDONLYfrom the start. The commit message will lead with theopenat()reasoning and keep binaries as just an example.
I'm guessing this all goes away after you do Christian's suggestion? I don't think we should be retrieving anything into a file that userspace has opened O_RDONLY. Retrieval is in effect a write operation. So userspace _has_ to retrieve a file and reopen it O_RDONLY after finishing the session.
Signature · 2 lines
Regards, Pratyush Yadav
Pratyush Yadav
maintainerRe: [PATCH v3 06/10] mm/shmem_luo: Don't make early retrieve failures permanent
In reply to David MatlackQuoted 28 lines · David Matlack
Quoted 20 lines · Pratyush Yadav
Quoted 8 lines · David Matlack
Mark failures in
shmem_luo_retrieve()before it starts restoring the folios as retryable, and return the error without freeing the serialized state. If no retrieve succeeds,finish()frees the preserved folios, like it does for a file that was never retrieved.Nothing has consumed the preserved state before the folios are restored, so the retrieve can be tried again.
Quoted 6 lines
This also fixes leaking the preserved folios when one of these early steps fails, e.g. because the file has seals that this kernel doesn't support. The retrieve freed the serialized state but not the folios, and
finish()then skipped the file because its retrieve had failed. There is no Fixes tag, since the fix relies onargs->retryable, which was only added earlier in this series.If the seals don't match, the retrieve is not retryable. It will always fail.
Quoted 6 lines
I think the only retriable part is
memfd_alloc_file(), although even that is quite unlikely to fail. But I think that also fits pretty well with the most important type of error you want to retry on, which is bad arguments. In later patches, you check the argument onshmem_luo_create_tmpfs_file()orshmem_luo_create_memfd(), so perhaps set retriable in those functions?
Quoted 3 lines
When retryable is set, LUO leaves the file unretrieved and
finish()frees the preserved folios. If the seal failure did not set retryable,finish()would skip the file and the folios would leak.
Yes, but in that case you free the folios on the error path of retrieve().
Quoted 5 lines
In v4 I can move the seal checks into
shmem_luo_create_memfd()andshmem_luo_create_tmpfs_file(), since the valid seals differ between the two, and reword the commit message so it doesn't suggest that a bad seal can be fixed by retrying.
I think it is important to signal to _userspace_ too that bad seals aren't retriable either. So I would really suggest doing some sort of non-retriable errors for bad payloads.
Signature · 2 lines
Regards, Pratyush Yadav
Pratyush Yadav
maintainerRe: [PATCH v3 10/10] selftests/liveupdate: Add a test for preserving tmpfs files
In reply to David MatlackQuoted 30 lines · David Matlack
Quoted 11 lines · Pasha Tatashin
Quoted 10 lines · Pratyush Yadav
On Tue, Nov 03 2026, David Matlack wrote:
[...]Umm, no way I am reading all this...
LLMs are great at generating tests, and I'm not saying there is no value in this. It is just that this massive code dump is unreviewable by humans. It is also unfair to the human reviewer because it took many orders of magnitude less effort to _write_ them with an LLM.
Quoted 8 lines
Quoted 5 lines
What do you all reckon we should do about this? Trust the LLM to get the testing right? Do we give up human maintenance of tests entirely? Do we do fault injection to test the tests themselves?
Or do we make the tests smaller and more human reviewable?
We need good test coverage, but I believe we can break this patch down into smaller changes where tested features are incrementally added.
Agreed that it needs to be easier to review, but I'd like to keep the coverage. Here's what I have in mind for v4:
- Move the preserve-side checks that don't need a kexec into
liveupdate.c, like Pasha suggested in his earlier reply: which files can be preserved (permissions, read-only and append-only files), the frozen size while preserved, and unpreserve. Those then run with the regular kselftests instead of only invmtest.sh.
Quoted 7 lines
- Split the rest of
luo_tmpfsinto several smaller commits that add the tested features incrementally, e.g. one for the test infrastructure and basic retrieve, one for theopen()flags and creation checks, and one for block accounting. That makes the series longer, but each commit is a few hundred lines and can be reviewed on its own.It will still be long, but I think having the test coverage is worth it.
Agreed.
Quoted 2 lines
Does that sound reasonable?
Sounds good. But I think practically speaking, it is not possible to review the tests as carefully as the main changes. I think I'll rely more on my LLM for those.
Signature · 2 lines
Regards, Pratyush Yadav
Pratyush Yadav
maintainerRe: [PATCH v3 07/10] mm/shmem_luo: Retrieve tmpfs files onto a tmpfs mount chosen by userspace
In reply to David MatlackQuoted 10 lines · David Matlack
Quoted 7 lines · Pratyush Yadav
Quoted 5 lines
+ * If @flags contains unknown flags or
O_PATH, if @mode contains unknown mode
+ * bits, or if @dirfd is not on a tmpfs mount, the retrieve fails with-EINVAL.
+ * If the file can't be created, the retrieve fails with the same errors as
+ *open(). A retrieve that fails before the preserved contents are restored can
+ * be retried.How is userspace supposed to tell if preserved contents were restored?
Quoted 7 lines
Through the error code, like Pasha said on patch 3. I'll reword this in v4 in terms of errors instead of kernel internals: errors from checking the argument and creating the file, e.g.
-EINVAL,-EBADFand the errors thatopen()returns, can be retried after fixing the argument. Errors while restoring the contents, e.g.-ENOMEM, or-ENOSPCwhen the contents don't fit on the mount, are permanent, and retrieving the token again returns the same error.
I wonder if we should document per-file type error codes and what they mean. Perhaps some time in the future...
Quoted 2 lines
Thanks for the review. I'll address the rest in v4.
Signature · 2 lines
Regards, Pratyush Yadav
Pratyush Yadav
maintainerHi David,
Quoted 2 lines · David Matlack
Quoted 1 line · Christian Brauner
On Tue, Nov 03, 2026 at 01:02:06AM +0000, David Matlack wrote:
[...]
Quoted 10 lines
Quoted 4 lines
So instead of LUO handing out a file descriptor for a file it invented, let userspace create the file,
O_TMPFILE,O_CREAT, whatever it likes, with the owner, mode and xattrs it wants, and let LUO put the contents into it. That's what the RFC did.I'm fine with going back to that in v4. The retrieve argument will just be a file descriptor for the target file, and LUO will only restore the contents and size into it. Everything else about the file is up to userspace.
Quoted 8 lines
Quoted 4 lines
The checks on the target are trivial: a writable fd to an empty shmem file with the inode lock held while you fill it. A tmpfs file can't be sealed, it only ever has
F_SEAL_SEAL, so there's nothing to restore but contents and size anyway.Sounds good. The one other check I plan to add is rejecting a target that LUO is already managing, e.g. a file that is preserved in an outgoing session and whose size is frozen.
I have been playing with this idea too, and I also stumbled onto this problem.
You also need to make sure a preserve doesn't race with a retrieve. Since userspace has access to the file, it can do both at the same time. I think LUO should expose a "register file" function that registers the file in luo_preserved_files. This should be the very first thing retrieve does.
Also, today LUO core expects a fresh file from file handlers, so it always installs a FD to that file in the task. When doing the "retrieve into FD" thing, that will result in two FDs to the same file. While it isn't a strict problem, it seems kinda ugly from a uAPI prespective.
Perhaps we should let retrieve signal to LUO core that "I already installed the file", or a "I have no file to install", or something similar?
Quoted 6 lines
LUO will also add the folios while
i_sizeis still 0, and seti_sizelast. So reads, page faults andmemfd_pin_folios()wouldn't see the folios until the file is complete, and if the retrieve fails partway through the target is left empty.
[...]
Signature · 2 lines
Regards, Pratyush Yadav
Quoted 11 lines · Pratyush Yadav
Quoted 9 lines · David Matlack
Quoted 4 lines · Christian Brauner
The checks on the target are trivial: a writable fd to an empty shmem file with the inode lock held while you fill it. A tmpfs file can't be sealed, it only ever has
F_SEAL_SEAL, so there's nothing to restore but contents and size anyway.Sounds good. The one other check I plan to add is rejecting a target that LUO is already managing, e.g. a file that is preserved in an outgoing session and whose size is frozen.
Quoted 8 lines
I have been playing with this idea too, and I also stumbled onto this problem.
You also need to make sure a preserve doesn't race with a retrieve. Since userspace has access to the file, it can do both at the same time. I think LUO should expose a "register file" function that registers the file in
luo_preserved_files. This should be the very first thing retrieve does.
Agreed, that's what I have queued up for v4, which I'm hoping to send by tomorrow.
LUO gets two new functions for .retrieve() callbacks:
- liveupdate_claim_file() inserts the target into luo_preserved_files, and fails with -EBUSY if it's already there, e.g. because it's preserved in an outgoing session or was retrieved before.
- liveupdate_unclaim_file() undoes it. If .retrieve() claimed the file it returns, LUO doesn't insert it again after the callback, and a callback that fails after claiming must unclaim.
On the preserve side, LUO inserts the file into luo_preserved_files before calling the file handler .preserve(), so whichever of the two inserts first wins and the other fails with -EBUSY.
Quoted 8 lines
Also, today LUO core expects a fresh file from file handlers, so it always installs a FD to that file in the task. When doing the "retrieve into FD" thing, that will result in two FDs to the same file. While it isn't a strict problem, it seems kinda ugly from a uAPI prespective.
Perhaps we should let retrieve signal to LUO core that "I already installed the file", or a "I have no file to install", or something similar?
Makes sense, I'll do that in v4. The file handler will tell LUO that it restored into an existing fd, and LIVEUPDATE_SESSION_RETRIEVE_FD will return that fd instead of installing a new one. Retrieving the token again later still returns a new fd for the same file, like it does today for any file that was already retrieved.