[PATCH v6 4/7] liveupdate: luo_flb: serialize outgoing FLBs incrementally

From: Pasha Tatashin

Date: Wed Nov 18 2026 - 16:29:45 EST


Currently, outgoing FLB metadata is written in a single pass by
luo_flb_serialize() during liveupdate_reboot(). When sessions are
serialized ahead of reboot, any FLBs their files depend on must also
be serialized at that time so the outgoing KHO state stays consistent.

Serialize each file's FLB dependencies from luo_session_serialize_one(),
appending new FLB entries or incrementing the serialized file count in
place. Since every serialized session now updates its FLBs as it is
serialized, drop the separate luo_flb_serialize() pass from
liveupdate_reboot().

Because serialized sessions never unpreserve their files, FLB
serialization is one-way: once an FLB is serialized, its .unpreserve()
callback will not run and its root handle from .preserve() remains
fixed for the rest of the boot.

Signed-off-by: Pasha Tatashin <pasha.tatashin@xxxxxxxxxx>
---
kernel/liveupdate/luo_core.c | 10 +--
kernel/liveupdate/luo_file.c | 15 +++++
kernel/liveupdate/luo_flb.c | 102 ++++++++++++++++++++-----------
kernel/liveupdate/luo_internal.h | 3 +-
kernel/liveupdate/luo_session.c | 2 +
5 files changed, 85 insertions(+), 47 deletions(-)

diff --git a/kernel/liveupdate/luo_core.c b/kernel/liveupdate/luo_core.c
index 1b2bda22902d..9fef5161a1f3 100644
--- a/kernel/liveupdate/luo_core.c
+++ b/kernel/liveupdate/luo_core.c
@@ -219,18 +219,10 @@ late_initcall(luo_late_startup);
*/
int liveupdate_reboot(void)
{
- int err;
-
if (!liveupdate_enabled())
return 0;

- err = luo_session_serialize();
- if (err)
- return err;
-
- luo_flb_serialize();
-
- return 0;
+ return luo_session_serialize();
}

/**
diff --git a/kernel/liveupdate/luo_file.c b/kernel/liveupdate/luo_file.c
index 988eb313a135..cdd3c466e5ac 100644
--- a/kernel/liveupdate/luo_file.c
+++ b/kernel/liveupdate/luo_file.c
@@ -380,6 +380,21 @@ void luo_file_unpreserve_files(struct luo_file_set *file_set)
kho_block_set_destroy(&file_set->block_set);
}

+/**
+ * luo_file_serialize_flbs - Serialize FLBs for all preserved files in a file_set.
+ * @file_set: The file_set being serialized.
+ *
+ * Iterates through all preserved files in @file_set and serializes their
+ * associated FLB metadata into the outgoing KHO FLB array.
+ */
+void luo_file_serialize_flbs(struct luo_file_set *file_set)
+{
+ struct luo_file *luo_file;
+
+ list_for_each_entry(luo_file, &file_set->files_list, list)
+ luo_flb_file_serialize(luo_file->fh);
+}
+
static int luo_file_freeze_one(struct luo_file_set *file_set,
struct luo_file *luo_file)
{
diff --git a/kernel/liveupdate/luo_flb.c b/kernel/liveupdate/luo_flb.c
index cd715a7c1d99..4ac534171d42 100644
--- a/kernel/liveupdate/luo_flb.c
+++ b/kernel/liveupdate/luo_flb.c
@@ -34,6 +34,12 @@
* This reference-counted approach ensures that shared state is saved exactly
* once and restored exactly once, regardless of how many files depend on it,
* and that its lifecycle is correctly managed across the kexec transition.
+ *
+ * Serialization: Outgoing FLB metadata is serialized into the KHO FLB array
+ * incrementally as sessions are serialized. Once serialized, the FLB's
+ * .unpreserve() callback is never invoked and its root KHO handle from
+ * .preserve() remains fixed, while later sessions may still update per-FD
+ * state in the shared live object under the FLB handler's own locking.
*/

#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
@@ -72,6 +78,13 @@ static struct luo_flb_global luo_flb_global = {
.list = LIST_HEAD_INIT(luo_flb_global.list),
};

+/*
+ * Serializes updates to luo_flb_global.outgoing.header_ser and .ser[].
+ * Lock hierarchy:
+ * luo_register_rwlock -> private->outgoing.lock -> luo_flb_ser_lock
+ */
+static DEFINE_MUTEX(luo_flb_ser_lock);
+
/*
* struct luo_flb_link - Links an FLB definition to a file handler's internal
* list of dependencies.
@@ -104,6 +117,32 @@ static struct luo_flb_private *luo_flb_get_private(struct liveupdate_flb *flb)
return private;
}

+static void luo_flb_serialize_one(const struct liveupdate_flb *flb, u64 data)
+{
+ struct luo_flb_header *fh = &luo_flb_global.outgoing;
+ u64 nr;
+
+ guard(mutex)(&luo_flb_ser_lock);
+ if (!fh->active)
+ return;
+
+ nr = fh->header_ser->count;
+ for (u64 i = 0; i < nr; i++) {
+ if (!strcmp(fh->ser[i].name, flb->compatible)) {
+ fh->ser[i].count++;
+ return;
+ }
+ }
+
+ if (WARN_ON_ONCE(nr >= LUO_FLB_MAX))
+ return;
+
+ strscpy(fh->ser[nr].name, flb->compatible, sizeof(fh->ser[nr].name));
+ fh->ser[nr].data = data;
+ fh->ser[nr].count = 1;
+ fh->header_ser->count = nr + 1;
+}
+
static int luo_flb_file_preserve_one(struct liveupdate_flb *flb)
{
struct luo_flb_private *private = luo_flb_get_private(flb);
@@ -296,6 +335,32 @@ void luo_flb_file_unpreserve(struct liveupdate_file_handler *fh)
liveupdate_flb_put_outgoing(iter->flb);
}

+/**
+ * luo_flb_file_serialize - Serialize FLB metadata for a preserved file's handler.
+ * @fh: The file handler of the preserved file being serialized.
+ *
+ * Adds each FLB associated with @fh to the outgoing KHO FLB array on first
+ * use, or increments its serialized file reference count by one.
+ *
+ * Context: Called when a session containing preserved files is serialized.
+ */
+void luo_flb_file_serialize(struct liveupdate_file_handler *fh)
+{
+ struct list_head *flb_list = &ACCESS_PRIVATE(fh, flb_list);
+ struct luo_flb_link *iter;
+
+ guard(rwsem_read)(&luo_register_rwlock);
+ list_for_each_entry(iter, flb_list, list) {
+ struct luo_flb_private *private = luo_flb_get_private(iter->flb);
+
+ scoped_guard(mutex, &private->outgoing.lock) {
+ if (WARN_ON_ONCE(!refcount_read(&private->outgoing.count)))
+ continue;
+ luo_flb_serialize_one(iter->flb, private->outgoing.data);
+ }
+ }
+}
+
/**
* luo_flb_file_finish - Notifies FLBs that a dependent file has been finished.
* @fh: The file handler for the finished file.
@@ -587,40 +652,3 @@ void __init luo_flb_setup_incoming(u64 flbs_pa)
luo_flb_global.incoming.ser = (void *)(header_ser + 1);
luo_flb_global.incoming.active = true;
}
-
-/**
- * luo_flb_serialize - Serializes all active FLB objects for KHO.
- *
- * This function is called from the reboot path. It iterates through all
- * registered File-Lifecycle-Bound (FLB) objects. For each FLB that has been
- * preserved (i.e., its reference count is greater than zero), it writes its
- * metadata into the memory region designated for Kexec Handover.
- *
- * The serialized data includes the FLB's compatibility string, its opaque
- * data handle, and the final reference count. This allows the new kernel to
- * find the appropriate handler and reconstruct the FLB's state.
- *
- * Context: Called from liveupdate_reboot() just before kho_finalize().
- */
-void luo_flb_serialize(void)
-{
- struct luo_flb_header *fh = &luo_flb_global.outgoing;
- struct liveupdate_flb *gflb;
- int i = 0;
-
- guard(rwsem_read)(&luo_register_rwlock);
- list_private_for_each_entry(gflb, &luo_flb_global.list, private.list) {
- struct luo_flb_private *private = luo_flb_get_private(gflb);
- long count = refcount_read(&private->outgoing.count);
-
- if (count > 0) {
- strscpy(fh->ser[i].name, gflb->compatible,
- sizeof(fh->ser[i].name));
- fh->ser[i].data = private->outgoing.data;
- fh->ser[i].count = count;
- i++;
- }
- }
-
- fh->header_ser->count = i;
-}
diff --git a/kernel/liveupdate/luo_internal.h b/kernel/liveupdate/luo_internal.h
index 77dac2742877..3d948279f018 100644
--- a/kernel/liveupdate/luo_internal.h
+++ b/kernel/liveupdate/luo_internal.h
@@ -106,6 +106,7 @@ int luo_session_deserialize(void);

int luo_preserve_file(struct luo_file_set *file_set, u64 token, int fd);
void luo_file_unpreserve_files(struct luo_file_set *file_set);
+void luo_file_serialize_flbs(struct luo_file_set *file_set);
int luo_file_freeze(struct luo_file_set *file_set);
void luo_file_unfreeze(struct luo_file_set *file_set);
int luo_retrieve_file(struct luo_file_set *file_set, u64 token,
@@ -118,11 +119,11 @@ void luo_file_set_destroy(struct luo_file_set *file_set);

int luo_flb_file_preserve(struct liveupdate_file_handler *fh);
void luo_flb_file_unpreserve(struct liveupdate_file_handler *fh);
+void luo_flb_file_serialize(struct liveupdate_file_handler *fh);
void luo_flb_file_finish(struct liveupdate_file_handler *fh);
void luo_flb_unregister_all(struct liveupdate_file_handler *fh);
int __init luo_flb_setup_outgoing(u64 *flbs_pa);
void __init luo_flb_setup_incoming(u64 flbs_pa);
-void luo_flb_serialize(void);

#ifdef CONFIG_LIVEUPDATE_TEST
void liveupdate_test_register(struct liveupdate_file_handler *fh);
diff --git a/kernel/liveupdate/luo_session.c b/kernel/liveupdate/luo_session.c
index 56d7f76c0282..013386534b69 100644
--- a/kernel/liveupdate/luo_session.c
+++ b/kernel/liveupdate/luo_session.c
@@ -222,6 +222,8 @@ static void luo_session_serialize_one(struct luo_session *session)
if (WARN_ON_ONCE(!ser))
return;

+ luo_file_serialize_flbs(&session->file_set);
+
strscpy(ser->name, session->name, sizeof(ser->name));
ser->file_set_ser.count = session->file_set.count;
ser->file_set_ser.files =
--
2.56.1.708.g77570a6a1d-goog