[PATCH v6 2/7] kho: link outgoing blocks lazily in kho_block

From: Pasha Tatashin

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


Currently, kho_block_add() links each preallocated block onto the
serialized chain immediately, even while it is still empty. This
assumes all preallocated blocks are populated together before the
chain is referenced by KHO.

When sessions are serialized incrementally ahead of reboot, extra
blocks may be preallocated for sessions that have not been serialized
yet. Keeping those blocks linked leaves empty trailing blocks on the
KHO chain.

Keep preallocated outgoing blocks unlinked in kho_block_add() and link
each block lazily in kho_block_set_it_reserve_entry() when advancing
into it. Also unlink blocks in kho_block_set_clear() and stop at
unlinked blocks in kho_block_set_it_read_entry().

Signed-off-by: Pasha Tatashin <pasha.tatashin@xxxxxxxxxx>
---
kernel/liveupdate/kho_block.c | 38 +++++++++++++++++++++++------------
1 file changed, 25 insertions(+), 13 deletions(-)

diff --git a/kernel/liveupdate/kho_block.c b/kernel/liveupdate/kho_block.c
index 0d2a342ef422..21cee45fa1cc 100644
--- a/kernel/liveupdate/kho_block.c
+++ b/kernel/liveupdate/kho_block.c
@@ -16,6 +16,11 @@
* array of serialized entries. Multiple blocks are linked together via a
* physical pointer in the header, forming a linked list that can be easily
* traversed in both the current and the next kernel.
+ *
+ * Outgoing blocks preallocated via kho_block_set_grow() remain unlinked until
+ * entries are reserved via kho_block_set_it_reserve_entry(). This allows
+ * entries to be serialized incrementally without exposing empty trailing blocks
+ * on the physical chain.
*/

#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
@@ -79,7 +84,7 @@ static struct kho_block_header_ser *kho_block_alloc_ser(struct kho_block_set *bs
static int kho_block_add(struct kho_block_set *bs,
struct kho_block_header_ser *ser)
{
- struct kho_block *block, *last;
+ struct kho_block *block;

if (bs->nblocks >= KHO_MAX_BLOCKS)
return -ENOSPC;
@@ -89,15 +94,11 @@ static int kho_block_add(struct kho_block_set *bs,
return -ENOMEM;

block->ser = ser;
- last = list_last_entry_or_null(&bs->blocks, struct kho_block, list);
+ if (list_empty(&bs->blocks))
+ bs->head_pa = virt_to_phys(ser);
list_add_tail(&block->list, &bs->blocks);
bs->nblocks++;

- if (last)
- last->ser->next = virt_to_phys(ser);
- else
- bs->head_pa = virt_to_phys(ser);
-
return 0;
}

@@ -144,9 +145,9 @@ static void kho_block_set_shrink_one(struct kho_block_set *bs)
* @bs: The block set.
* @count: The target number of valid entries to accommodate.
*
- * Dynamically preallocates and links preserved memory blocks if the target
- * entry count exceeds the current total capacity of the set, ensuring they
- * are available during serialization/deserialization.
+ * Dynamically preallocates preserved memory blocks if the target entry count
+ * exceeds the current total capacity of the set, ensuring they are available
+ * during serialization/deserialization.
*
* Context: Caller must hold a lock protecting the block set.
* Return: 0 on success, or a negative errno on failure.
@@ -312,12 +313,15 @@ void kho_block_set_destroy(struct kho_block_set *bs)
/**
* kho_block_set_clear - Clear all serialized data in a block set.
* @bs: The block set to clear.
+ *
+ * Resets entry counts, unlinks blocks, and zeroes all entry payloads.
*/
void kho_block_set_clear(struct kho_block_set *bs)
{
struct kho_block *block;

list_for_each_entry(block, &bs->blocks, list) {
+ block->ser->next = 0;
block->ser->count = 0;
memset(block->ser + 1, 0, KHO_BLOCK_SIZE - sizeof(*block->ser));
}
@@ -340,14 +344,16 @@ void kho_block_set_it_init(struct kho_block_set_it *it, struct kho_block_set *bs
* @it: The block iterator.
*
* Reserves a slot in the current block during state serialization to add a new
- * entry, advancing the internal index. If the current block is full, it
- * automatically moves to the next block in the set.
+ * entry, advancing the internal index. If the current block is full, it moves
+ * to the next preallocated block and links it onto the serialized chain via
+ * the previous block's header.
*
* Return: A pointer to the reserved entry slot, or NULL if the block set's
* capacity is fully exhausted.
*/
void *kho_block_set_it_reserve_entry(struct kho_block_set_it *it)
{
+ struct kho_block *prev = NULL;
void *entry;

if (!it->block)
@@ -356,12 +362,16 @@ void *kho_block_set_it_reserve_entry(struct kho_block_set_it *it)
if (it->i == it->bs->count_per_block) {
if (list_is_last(&it->block->list, &it->bs->blocks))
return NULL;
+ prev = it->block;
it->block = list_next_entry(it->block, list);
it->i = 0;
}

entry = kho_block_entry(it, it->i++);
it->block->ser->count = it->i;
+ if (prev)
+ prev->ser->next = virt_to_phys(it->block->ser);
+
return entry;
}

@@ -381,7 +391,9 @@ void *kho_block_set_it_read_entry(struct kho_block_set_it *it)
return NULL;

if (it->i == it->block->ser->count) {
- if (list_is_last(&it->block->list, &it->bs->blocks))
+ if (!it->block->ser->next)
+ return NULL;
+ if (WARN_ON_ONCE(list_is_last(&it->block->list, &it->bs->blocks)))
return NULL;
it->block = list_next_entry(it->block, list);
it->i = 0;
--
2.56.1.708.g77570a6a1d-goog