* [PATCH v2 0/2] memfd_luo: keep memfd frozen until finish
@ 2026-11-10 17:58 Pratyush Yadav
2026-11-10 17:58 ` [PATCH v2 1/2] liveupdate: luo_file: allow keeping private_data from retrieve " Pratyush Yadav
2026-11-10 17:58 ` [PATCH v2 2/2] mm/memfd_luo: keep memfd frozen " Pratyush Yadav
0 siblings, 2 replies; 7+ messages in thread
From: Pratyush Yadav @ 2026-11-10 17:58 UTC (permalink / raw)
To: Pasha Tatashin, Mike Rapoport, Pratyush Yadav, Andrew Morton,
Samiullah Khawaja
Cc: kexec, linux-kernel, linux-mm
From: "Pratyush Yadav (Google)" <pratyush@kernel.org>
Hi,
This series keeps a retrieved memfd frozen until finish. The main
motivation is to let dependents like iommufd have a chance to acquire a
reference. It also fits conceptually: finish is the point when the file
should be back to "normal".
Patch 1 allows keeping private_data from retrieve to finish. This is
needed by memfd to keep hold of folios_ser, which it uses to give up its
pins.
Patch 2 does the main change.
The series is based on
https://lore.kernel.org/kexec/20261103172251.2348370-1-pratyush@kernel.org/T/#u.
Changes in v2:
- Replace the stale reference to the 'data' field in the struct
liveupdate_file_op_args kerneldoc with 'file', 'serialized_data' and
'private_data'. (Pasha)
- Collect Reviewed-by tags from Pasha.
- Rebase on the latest liveupdate/next.
v1: https://lore.kernel.org/all/20261103181916.2462825-1-pratyush@kernel.org/
Regards,
Pratyush Yadav
Pratyush Yadav (Google) (2):
liveupdate: luo_file: allow keeping private_data from retrieve until
finish
mm/memfd_luo: keep memfd frozen until finish
include/linux/liveupdate.h | 11 +++--
kernel/liveupdate/luo_file.c | 11 +++--
mm/memfd_luo.c | 87 ++++++++++++++++++++++++++++--------
3 files changed, 83 insertions(+), 26 deletions(-)
--
2.56.0.542.g3ef89b7020-goog
^ permalink raw reply [flat|nested] 7+ messages in thread
* [PATCH v2 1/2] liveupdate: luo_file: allow keeping private_data from retrieve until finish
2026-11-10 17:58 [PATCH v2 0/2] memfd_luo: keep memfd frozen until finish Pratyush Yadav
@ 2026-11-10 17:58 ` Pratyush Yadav
2026-11-10 18:29 ` Samiullah Khawaja
2026-11-15 6:05 ` Chris Down
2026-11-10 17:58 ` [PATCH v2 2/2] mm/memfd_luo: keep memfd frozen " Pratyush Yadav
1 sibling, 2 replies; 7+ messages in thread
From: Pratyush Yadav @ 2026-11-10 17:58 UTC (permalink / raw)
To: Pasha Tatashin, Mike Rapoport, Pratyush Yadav, Andrew Morton,
Samiullah Khawaja
Cc: kexec, linux-kernel, linux-mm
From: "Pratyush Yadav (Google)" <pratyush@kernel.org>
The private_data field of struct liveupdate_file_op_args lets file
handlers keep runtime state that is not preserved. It is set by
.preserve() and passed to the later outgoing callbacks, up to
.unpreserve(). But in the new kernel, nothing set by .retrieve() is
kept, and .can_finish() and .finish() always get a NULL private_data.
Handlers might need to keep runtime state from retrieve until finish.
For example, memfd will need to remember the folios it holds on to
until finish so it can release them there.
Save the private_data set by a successful .retrieve() in the luo_file,
and pass it to .can_finish() and .finish(). The handler must free it in
.finish().
While at it, update a stale comment under struct
liveupdate_file_op_args. There is no 'data', but there is
'serialized_data' and 'private_data'. Refer to them instead.
Reviewed-by: Pasha Tatashin <pasha.tatashin@soleen.com>
Signed-off-by: Pratyush Yadav (Google) <pratyush@kernel.org>
---
include/linux/liveupdate.h | 11 +++++++----
kernel/liveupdate/luo_file.c | 11 ++++++++---
2 files changed, 15 insertions(+), 7 deletions(-)
diff --git a/include/linux/liveupdate.h b/include/linux/liveupdate.h
index 6051abc0612ce..f01e9ab68d996 100644
--- a/include/linux/liveupdate.h
+++ b/include/linux/liveupdate.h
@@ -37,12 +37,15 @@ struct file;
* @serialized_data: The opaque u64 handle, preserve/prepare/freeze may update
* this field.
* @private_data: Private data for the file used to hold runtime state that
- * is not preserved. Set by the handler's .preserve()
- * callback, and must be freed in the handler's
- * .unpreserve() callback.
+ * is not preserved. For outgoing, set by the handler's
+ * .preserve() callback, and must be freed in the handler's
+ * .unpreserve() callback. For incoming, set by the handler's
+ * .retrieve() callback on success, and must be freed in the
+ * handler's .finish() callback.
*
* This structure bundles all parameters for the file operation callbacks.
- * The 'data' and 'file' fields are used for both input and output.
+ * The 'file', 'serialized_data' and 'private_data' fields are used for both
+ * input and output.
*/
struct liveupdate_file_op_args {
struct liveupdate_file_handler *handler;
diff --git a/kernel/liveupdate/luo_file.c b/kernel/liveupdate/luo_file.c
index a39992da504fb..9bda37b0dc4f1 100644
--- a/kernel/liveupdate/luo_file.c
+++ b/kernel/liveupdate/luo_file.c
@@ -145,9 +145,11 @@ static DEFINE_XARRAY(luo_preserved_files);
* .retrieve(), and .finish() callbacks, allowing it to track
* and update its serialized state across phases.
* @private_data: Pointer to the private data for the file used to hold runtime
- * state that is not preserved. Set by the handler's .preserve()
- * callback, and must be freed in the handler's .unpreserve()
- * callback.
+ * state that is not preserved. For outgoing, set by the
+ * handler's .preserve() callback, and must be freed in the
+ * handler's .unpreserve() callback. For incoming, set by the
+ * handler's .retrieve() callback, and must be freed in the
+ * handler's .finish() callback.
* @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()
@@ -616,6 +618,7 @@ int luo_retrieve_file(struct luo_file_set *file_set, u64 token,
}
luo_file->file = args.file;
+ luo_file->private_data = args.private_data;
/* Get reference so we can keep this file in LUO until finish */
get_file(luo_file->file);
@@ -643,6 +646,7 @@ static int luo_file_can_finish_one(struct luo_file_set *file_set,
args.session = luo_session_from_file_set(file_set);
args.file = luo_file->file;
args.serialized_data = luo_file->serialized_data;
+ args.private_data = luo_file->private_data;
args.retrieve_status = luo_file->retrieve_status;
can_finish = luo_file->fh->ops->can_finish(&args);
}
@@ -661,6 +665,7 @@ static void luo_file_finish_one(struct luo_file_set *file_set,
args.session = luo_session_from_file_set(file_set);
args.file = luo_file->file;
args.serialized_data = luo_file->serialized_data;
+ args.private_data = luo_file->private_data;
args.retrieve_status = luo_file->retrieve_status;
luo_file->fh->ops->finish(&args);
--
2.56.0.542.g3ef89b7020-goog
^ permalink raw reply related [flat|nested] 7+ messages in thread
* [PATCH v2 2/2] mm/memfd_luo: keep memfd frozen until finish
2026-11-10 17:58 [PATCH v2 0/2] memfd_luo: keep memfd frozen until finish Pratyush Yadav
2026-11-10 17:58 ` [PATCH v2 1/2] liveupdate: luo_file: allow keeping private_data from retrieve " Pratyush Yadav
@ 2026-11-10 17:58 ` Pratyush Yadav
2026-11-10 18:36 ` Samiullah Khawaja
2026-11-15 6:05 ` Chris Down
1 sibling, 2 replies; 7+ messages in thread
From: Pratyush Yadav @ 2026-11-10 17:58 UTC (permalink / raw)
To: Pasha Tatashin, Mike Rapoport, Pratyush Yadav, Andrew Morton,
Samiullah Khawaja
Cc: kexec, linux-kernel, linux-mm
From: "Pratyush Yadav (Google)" <pratyush@kernel.org>
After a memfd is retrieved, it immediately goes back into "normal"
operation. The file comes back unfrozen, and its pages can migrate, it
can be truncated, and so on. If the memfd is depended on by something,
like iommufd, that can open a window for folios to move around before
the consumer can re-establish its references.
Finish is the conceptual point when a retrieved file can start to behave
as "normal". So take an extra reference on the folios and freeze the
memfd on retrieve until finish.
Suggested-by: Pasha Tatashin <pasha.tatashin@soleen.com>
Reviewed-by: Pasha Tatashin <pasha.tatashin@soleen.com>
Signed-off-by: Pratyush Yadav (Google) <pratyush@kernel.org>
---
mm/memfd_luo.c | 87 +++++++++++++++++++++++++++++++++++++++-----------
1 file changed, 68 insertions(+), 19 deletions(-)
diff --git a/mm/memfd_luo.c b/mm/memfd_luo.c
index 9fd9abf9fedf6..50dbbda47ad3c 100644
--- a/mm/memfd_luo.c
+++ b/mm/memfd_luo.c
@@ -55,6 +55,14 @@
* Only seals known to this LUO version (see ``MEMFD_LUO_ALL_SEALS``) may
* be present; preservation fails with ``-EOPNOTSUPP`` otherwise.
*
+ * Restrictions
+ * ============
+ *
+ * While a memfd is preserved, and after it is retrieved until the session is
+ * finished, its size can't be changed and holes can't be punched in it.
+ * Operations that would do so (``ftruncate()``, ``fallocate()``, writes past
+ * the end of the file) fail.
+ *
* Non-Preserved Properties
* ========================
*
@@ -456,33 +464,56 @@ static void memfd_luo_discard_folios(const struct memfd_luo_folio_ser *folios_se
}
}
+/* Drop the references retrieve() holds on the folios added to the file. */
+static void
+memfd_luo_put_retrieved_folios(const struct memfd_luo_folio_ser *folios_ser,
+ u64 nr_folios)
+{
+ for (u64 i = 0; i < nr_folios; i++) {
+ const struct memfd_luo_folio_ser *pfolio = &folios_ser[i];
+
+ if (pfolio->pfn)
+ folio_put(pfn_folio(pfolio->pfn));
+ }
+}
+
static void memfd_luo_finish(struct liveupdate_file_op_args *args)
{
struct memfd_luo_folio_ser *folios_ser;
struct memfd_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 (args->retrieve_status)
+ /* If retrieve failed, it already cleaned up everything it could. */
+ if (args->retrieve_status < 0)
return;
ser = phys_to_virt(args->serialized_data);
if (!ser)
return;
- if (ser->nr_folios) {
- folios_ser = kho_restore_vmalloc(&ser->folios);
- if (!folios_ser)
- goto out;
+ if (args->retrieve_status > 0) {
+ /*
+ * Retrieve was successful, release the folios and unfreeze the
+ * file so it can be used normally.
+ */
+ struct inode *inode = file_inode(args->file);
+
+ folios_ser = args->private_data;
+
+ inode_lock(inode);
+ memfd_luo_put_retrieved_folios(folios_ser, ser->nr_folios);
+ shmem_freeze(inode, false);
+ inode_unlock(inode);
- memfd_luo_discard_folios(folios_ser, ser->nr_folios);
vfree(folios_ser);
+ } else if (args->retrieve_status == 0 && ser->nr_folios) {
+ /* Retrieve was not called, discard the preserved folios. */
+ folios_ser = kho_restore_vmalloc(&ser->folios);
+ if (folios_ser) {
+ memfd_luo_discard_folios(folios_ser, ser->nr_folios);
+ vfree(folios_ser);
+ }
}
-out:
kho_restore_free(ser);
}
@@ -552,7 +583,10 @@ static int memfd_luo_retrieve_folios(struct file *file,
nr_added_pages += npages;
folio_add_lru(folio);
folio_unlock(folio);
- folio_put(folio);
+ /*
+ * Keep the reference from kho_restore_folio(). It is dropped
+ * in finish().
+ */
}
shmem_recalc_inode(inode, nr_added_pages, 0);
@@ -566,9 +600,12 @@ static int memfd_luo_retrieve_folios(struct file *file,
folio_put(folio);
put_folios:
/*
- * Note: don't free the folios already added to the file. They will be
- * freed when the file is freed. Free the ones not added yet here.
+ * Drop the references held on the folios already added to the file.
+ * The folios themselves will be freed when the file is freed.
*/
+ memfd_luo_put_retrieved_folios(folios_ser, i);
+
+ /* Free the ones not added yet here. */
for (long j = i + 1; j < nr_folios; j++) {
const struct memfd_luo_folio_ser *pfolio = &folios_ser[j];
phys_addr_t phys;
@@ -589,7 +626,7 @@ static int memfd_luo_retrieve_folios(struct file *file,
static int memfd_luo_retrieve(struct liveupdate_file_op_args *args)
{
- struct memfd_luo_folio_ser *folios_ser;
+ struct memfd_luo_folio_ser *folios_ser = NULL;
struct memfd_luo_ser *ser;
struct file *file;
int err;
@@ -624,6 +661,17 @@ static int memfd_luo_retrieve(struct liveupdate_file_op_args *args)
vfs_setpos(file, ser->pos, MAX_LFS_FILESIZE);
i_size_write(file_inode(file), ser->size);
+ /*
+ * Keep the file frozen until finish(). Other preserved objects might
+ * depend on it.
+ *
+ * The file isn't visible to anyone yet, but take the lock anyway since
+ * shmem_freeze() requires it.
+ */
+ inode_lock(file_inode(file));
+ shmem_freeze(file_inode(file), true);
+ inode_unlock(file_inode(file));
+
if (ser->nr_folios) {
folios_ser = kho_restore_vmalloc(&ser->folios);
if (!folios_ser) {
@@ -632,16 +680,17 @@ static int memfd_luo_retrieve(struct liveupdate_file_op_args *args)
}
err = memfd_luo_retrieve_folios(file, folios_ser, ser->nr_folios);
- vfree(folios_ser);
if (err)
- goto put_file;
+ goto free_folios_ser;
}
args->file = file;
- kho_restore_free(ser);
+ args->private_data = folios_ser;
return 0;
+free_folios_ser:
+ vfree(folios_ser);
put_file:
fput(file);
free_ser:
--
2.56.0.542.g3ef89b7020-goog
^ permalink raw reply related [flat|nested] 7+ messages in thread
* Re: [PATCH v2 1/2] liveupdate: luo_file: allow keeping private_data from retrieve until finish
2026-11-10 17:58 ` [PATCH v2 1/2] liveupdate: luo_file: allow keeping private_data from retrieve " Pratyush Yadav
@ 2026-11-10 18:29 ` Samiullah Khawaja
2026-11-15 6:05 ` Chris Down
1 sibling, 0 replies; 7+ messages in thread
From: Samiullah Khawaja @ 2026-11-10 18:29 UTC (permalink / raw)
To: Pratyush Yadav
Cc: Pasha Tatashin, Mike Rapoport, Andrew Morton, kexec, linux-kernel,
linux-mm
On Tue, Nov 10, 2026 at 06:58:52PM +0100, Pratyush Yadav wrote:
>From: "Pratyush Yadav (Google)" <pratyush@kernel.org>
>
>The private_data field of struct liveupdate_file_op_args lets file
>handlers keep runtime state that is not preserved. It is set by
>.preserve() and passed to the later outgoing callbacks, up to
>.unpreserve(). But in the new kernel, nothing set by .retrieve() is
>kept, and .can_finish() and .finish() always get a NULL private_data.
>
>Handlers might need to keep runtime state from retrieve until finish.
>For example, memfd will need to remember the folios it holds on to
>until finish so it can release them there.
>
>Save the private_data set by a successful .retrieve() in the luo_file,
>and pass it to .can_finish() and .finish(). The handler must free it in
>.finish().
>
>While at it, update a stale comment under struct
>liveupdate_file_op_args. There is no 'data', but there is
>'serialized_data' and 'private_data'. Refer to them instead.
>
>Reviewed-by: Pasha Tatashin <pasha.tatashin@soleen.com>
>Signed-off-by: Pratyush Yadav (Google) <pratyush@kernel.org>
>---
> include/linux/liveupdate.h | 11 +++++++----
> kernel/liveupdate/luo_file.c | 11 ++++++++---
> 2 files changed, 15 insertions(+), 7 deletions(-)
>
Reviewed-by: Samiullah Khawaja <skhawaja@google.com>
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH v2 2/2] mm/memfd_luo: keep memfd frozen until finish
2026-11-10 17:58 ` [PATCH v2 2/2] mm/memfd_luo: keep memfd frozen " Pratyush Yadav
@ 2026-11-10 18:36 ` Samiullah Khawaja
2026-11-15 6:05 ` Chris Down
1 sibling, 0 replies; 7+ messages in thread
From: Samiullah Khawaja @ 2026-11-10 18:36 UTC (permalink / raw)
To: Pratyush Yadav
Cc: Pasha Tatashin, Mike Rapoport, Andrew Morton, kexec, linux-kernel,
linux-mm
On Tue, Nov 10, 2026 at 06:58:53PM +0100, Pratyush Yadav wrote:
>From: "Pratyush Yadav (Google)" <pratyush@kernel.org>
>
>After a memfd is retrieved, it immediately goes back into "normal"
>operation. The file comes back unfrozen, and its pages can migrate, it
>can be truncated, and so on. If the memfd is depended on by something,
>like iommufd, that can open a window for folios to move around before
>the consumer can re-establish its references.
>
>Finish is the conceptual point when a retrieved file can start to behave
>as "normal". So take an extra reference on the folios and freeze the
>memfd on retrieve until finish.
>
>Suggested-by: Pasha Tatashin <pasha.tatashin@soleen.com>
>Reviewed-by: Pasha Tatashin <pasha.tatashin@soleen.com>
>Signed-off-by: Pratyush Yadav (Google) <pratyush@kernel.org>
>---
> mm/memfd_luo.c | 87 +++++++++++++++++++++++++++++++++++++++-----------
> 1 file changed, 68 insertions(+), 19 deletions(-)
>
Reviewed-by: Samiullah Khawaja <skhawaja@google.com>
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH v2 1/2] liveupdate: luo_file: allow keeping private_data from retrieve until finish
2026-11-10 17:58 ` [PATCH v2 1/2] liveupdate: luo_file: allow keeping private_data from retrieve " Pratyush Yadav
2026-11-10 18:29 ` Samiullah Khawaja
@ 2026-11-15 6:05 ` Chris Down
1 sibling, 0 replies; 7+ messages in thread
From: Chris Down @ 2026-11-15 6:05 UTC (permalink / raw)
To: Pratyush Yadav
Cc: Pasha Tatashin, Mike Rapoport, Andrew Morton, Samiullah Khawaja,
kexec, linux-kernel, linux-mm
Hi Pratyush,
I've tested this together with 2/2 across a kexec and it works well.
Reviewed-by: Chris Down <chris@chrisdown.name>
Tested-by: Chris Down <chris@chrisdown.name>
Thanks!
Chris
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH v2 2/2] mm/memfd_luo: keep memfd frozen until finish
2026-11-10 17:58 ` [PATCH v2 2/2] mm/memfd_luo: keep memfd frozen " Pratyush Yadav
2026-11-10 18:36 ` Samiullah Khawaja
@ 2026-11-15 6:05 ` Chris Down
1 sibling, 0 replies; 7+ messages in thread
From: Chris Down @ 2026-11-15 6:05 UTC (permalink / raw)
To: Pratyush Yadav
Cc: Pasha Tatashin, Mike Rapoport, Andrew Morton, Samiullah Khawaja,
kexec, linux-kernel, linux-mm
Hi Pratyush,
Pratyush Yadav writes:
>+ * Restrictions
>+ * ============
>+ *
>+ * While a memfd is preserved, and after it is retrieved until the session is
>+ * finished, its size can't be changed and holes can't be punched in it.
>+ * Operations that would do so (``ftruncate()``, ``fallocate()``, writes past
>+ * the end of the file) fail.
Not a big deal, but there's one more thing that fails in this window and
isn't listed here. Adding F_SEAL_WRITE to a retrieved memfd before finish
now waits about 150ms and then fails with EBUSY. That's because
memfd_wait_for_pins() sees the reference kept from kho_restore_folio() as
an extra reference on every folio. Once the session is finished it
succeeds straight away. The same already happens while the memfd is
preserved in the old kernel, so maybe it's worth mentioning here for both?
Also, fallocate() fails with EPERM in every mode while the file is frozen.
That includes FALLOC_FL_KEEP_SIZE inside i_size, which neither changes the
size nor punches a hole. "Operations that would do so" makes it sound like
only some fallocate() calls fail.
Otherwise I've tested this across a kexec and it works well.
Once those are fixed, please feel free to add:
Reviewed-by: Chris Down <chris@chrisdown.name>
Tested-by: Chris Down <chris@chrisdown.name>
Thanks!
Chris
^ permalink raw reply [flat|nested] 7+ messages in thread
end of thread, other threads:[~2026-11-15 6:05 UTC | newest]
Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-10 17:58 [PATCH v2 0/2] memfd_luo: keep memfd frozen until finish Pratyush Yadav
2026-11-10 17:58 ` [PATCH v2 1/2] liveupdate: luo_file: allow keeping private_data from retrieve " Pratyush Yadav
2026-11-10 18:29 ` Samiullah Khawaja
2026-11-15 6:05 ` Chris Down
2026-11-10 17:58 ` [PATCH v2 2/2] mm/memfd_luo: keep memfd frozen " Pratyush Yadav
2026-11-10 18:36 ` Samiullah Khawaja
2026-11-15 6:05 ` Chris Down
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox