Skip to contentSkip to conversation

luo: migrate serialized_data to type-safe KHO pointers

PATCH Superseded Tarun Sahu 2 participants 6 messages Jun 16 – Jun 22

Subsystems (3)

v2 of 5 Superseded Latest is v5 →

lore
Share & tools
lore

Tarun Sahu

author
#0 lore raw

v2 cover luo: migrate serialized_data to type-safe KHO pointers

Covvert raw serialized_data to KHO serializeable pointer (KHOSER_PTR). This series also takes care of resolving the bug with memfd of using phys_to_virt before checking the args->serialized_data value.

Tarun Sahu (2):
kho: add KHOSER_COPY_TYPE(UN)SAFE for phys copy
luo: Update serialized data to serializeable pointer

 include/linux/kho/abi/kexec_handover.h | 12 ++++++++++++
 include/linux/kho/abi/luo.h            |  6 ++++--
 include/linux/liveupdate.h             |  4 ++--
 kernel/liveupdate/luo_file.c           | 24 ++++++++++++------------
 mm/memfd_luo.c                         | 18 +++++++++---------
 5 files changed, 39 insertions(+), 25 deletions(-)

base-commit: 0e0611827f3349d0a2ac121c023a6d3260dcecdb

Signature · 1 line
2.54.0.1136.gdb2ca164c4-goog
author
#1 lore raw

PATCH v2 1/2 kho: add KHOSER_COPY_TYPE(UN)SAFE for phys copy

Adding KHOSER_COPY_TYPESAFE and KHOSER_COPY_TYPEUNSAFE to copy one serializeable pointer to another. It basically allows copy of phys val of the serializeable pointer.

---
 include/linux/kho/abi/kexec_handover.h | 12 ++++++++++++
 1 file changed, 12 insertions(+)
include/linux/kho/abi/kexec_handover.h +12 -0
index db9bda6dd310..1606369dc1ec 100644
@@ -139,6 +139,18 @@
 		(typeof((s).ptr))((s).phys ? phys_to_virt((s).phys) : NULL); \
 	})
 
+/* Copies one serializable pointer to another of same type. */
+#define KHOSER_COPY_TYPESAFE(dest, src) \
+	({ \
+		typecheck(typeof((dest).ptr), (src).ptr); \
+		(dest).phys = (src).phys; \
+	})
+
+/* Copies one serializable pointer to another. */
+#define KHOSER_COPY_TYPEUNSAFE(dest, src) \
+	({ \
+		(dest).phys = (src).phys; \
+	})
 /*
  * This header is embedded at the beginning of each `kho_vmalloc_chunk`
  * and contains a pointer to the next chunk in the linked list,
Signature · 1 line
2.54.0.1136.gdb2ca164c4-goog
author
#2 lore raw

PATCH v2 2/2 luo: Update serialized data to KHOSER_PTR

In reply to Tarun Sahu

Currently luo_file_ser, luo_file and liveupdate_file_op_args use the raw serialized_data which stores the phys address of the serialized data. There are two issues
1. It uses raw update, and there is not type safety check
2. user of liveupdate_file_op_args, uses virt_to_phys and phys_to_virt to convert the serializeable pointer to physical address and vice versa. User makes mistakes mistake to check the serializeable pointer/phys value check before converting like in memfd_luo.

Converting them to serializeable pointer enforce type safety check ( it has its limitation as you can not have generic serializeable pointer for all the subsystem. So only void * is the only choice, and updating it require user to use KHOSER_COPY_TYPEUNSAFE) and the null value check.

This patch also updates the memfd_luo to use the newly introduced the serializeable pointer. Which mitigates the issue of not checking args->serialized_data pointer before using it.

Fixes b3749f174d68 ("mm: memfd_luo: allow preserving memfd")
---
 include/linux/kho/abi/luo.h  |  6 ++++--
 include/linux/liveupdate.h   |  4 ++--
 kernel/liveupdate/luo_file.c | 24 ++++++++++++------------
 mm/memfd_luo.c               | 18 +++++++++---------
 4 files changed, 27 insertions(+), 25 deletions(-)
include/linux/kho/abi/luo.h +4 -2
index 46750a0ddf88..6b08fbf7ad59 100644
@@ -119,17 +119,19 @@
 
 #define LIVEUPDATE_HNDL_COMPAT_LENGTH	48
 
+#include <linux/kho/abi/kexec_handover.h>
+
 /**
  * struct luo_file_ser - Represents the serialized preserves files.
  * @compatible:  File handler compatible string.
- * @data:        Private data
+ * @serialized_data: Serializable pointer union for private data.
  * @token:       User provided token for this file
  *
  * If this structure is modified, LUO_SESSION_COMPATIBLE must be updated.
  */
 struct luo_file_ser {
 	char compatible[LIVEUPDATE_HNDL_COMPAT_LENGTH];
-	u64 data;
+	DECLARE_KHOSER_PTR(serialized_data, void *);
 	u64 token;
 } __packed;
 
include/linux/liveupdate.h +2 -2
index 30c5a39ff9e9..8b3466801159 100644
@@ -32,7 +32,7 @@ struct file;
  * @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.
- * @serialized_data:  The opaque u64 handle, preserve/prepare/freeze may update
+ * @serialized_data:  The serialized pointer union, 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()
@@ -46,7 +46,7 @@ struct liveupdate_file_op_args {
 	struct liveupdate_file_handler *handler;
 	int retrieve_status;
 	struct file *file;
-	u64 serialized_data;
+	DECLARE_KHOSER_PTR(serialized_data, void *);
 	void *private_data;
 };
 
kernel/liveupdate/luo_file.c +12 -12
index a0a419085e28..c50c8239a34d 100644
@@ -130,7 +130,7 @@ static DEFINE_XARRAY(luo_preserved_files);
  * @file:          Pointer to the kernel's &struct file that is being preserved.
  *                 This is NULL in the new kernel until the file is successfully
  *                 retrieved.
- * @serialized_data: The opaque u64 handle to the serialized state of the file.
+ * @serialized_data: The serialized pointer union to the serialized state of the file.
  *                 This handle is passed back to the handler's .freeze(),
  *                 .retrieve(), and .finish() callbacks, allowing it to track
  *                 and update its serialized state across phases.
@@ -166,7 +166,7 @@ static DEFINE_XARRAY(luo_preserved_files);
 struct luo_file {
 	struct liveupdate_file_handler *fh;
 	struct file *file;
-	u64 serialized_data;
+	DECLARE_KHOSER_PTR(serialized_data, void *);
 	void *private_data;
 	int retrieve_status;
 	struct mutex mutex;
@@ -328,7 +328,7 @@ int luo_preserve_file(struct luo_file_set *file_set, u64 token, int fd)
 	if (err)
 		goto err_kfree;
 
-	luo_file->serialized_data = args.serialized_data;
+	KHOSER_COPY_TYPESAFE(luo_file->serialized_data, args.serialized_data);
 	luo_file->private_data = args.private_data;
 	list_add_tail(&luo_file->list, &file_set->files_list);
 	file_set->count++;
@@ -381,7 +381,7 @@ void luo_file_unpreserve_files(struct luo_file_set *file_set)
 
 		args.handler = luo_file->fh;
 		args.file = luo_file->file;
-		args.serialized_data = luo_file->serialized_data;
+		KHOSER_COPY_TYPESAFE(args.serialized_data, luo_file->serialized_data);
 		args.private_data = luo_file->private_data;
 		luo_file->fh->ops->unpreserve(&args);
 		luo_flb_file_unpreserve(luo_file->fh);
@@ -412,12 +412,12 @@ static int luo_file_freeze_one(struct luo_file_set *file_set,
 
 		args.handler = luo_file->fh;
 		args.file = luo_file->file;
-		args.serialized_data = luo_file->serialized_data;
+		KHOSER_COPY_TYPESAFE(args.serialized_data, luo_file->serialized_data);
 		args.private_data = luo_file->private_data;
 
 		err = luo_file->fh->ops->freeze(&args);
 		if (!err)
-			luo_file->serialized_data = args.serialized_data;
+			KHOSER_COPY_TYPESAFE(luo_file->serialized_data, args.serialized_data);
 	}
 
 	return err;
@@ -433,7 +433,7 @@ static void luo_file_unfreeze_one(struct luo_file_set *file_set,
 
 		args.handler = luo_file->fh;
 		args.file = luo_file->file;
-		args.serialized_data = luo_file->serialized_data;
+		KHOSER_COPY_TYPESAFE(args.serialized_data, luo_file->serialized_data);
 		args.private_data = luo_file->private_data;
 
 		luo_file->fh->ops->unfreeze(&args);
@@ -515,7 +515,7 @@ int luo_file_freeze(struct luo_file_set *file_set,
 
 		strscpy(file_ser[i].compatible, luo_file->fh->compatible,
 			sizeof(file_ser[i].compatible));
-		file_ser[i].data = luo_file->serialized_data;
+		KHOSER_COPY_TYPESAFE(file_ser[i].serialized_data, luo_file->serialized_data);
 		file_ser[i].token = luo_file->token;
 		i++;
 	}
@@ -621,7 +621,7 @@ int luo_retrieve_file(struct luo_file_set *file_set, u64 token,
 	}
 
 	args.handler = luo_file->fh;
-	args.serialized_data = luo_file->serialized_data;
+	KHOSER_COPY_TYPESAFE(args.serialized_data, luo_file->serialized_data);
 	err = luo_file->fh->ops->retrieve(&args);
 	if (err) {
 		/* Keep the error code for later use. */
@@ -655,7 +655,7 @@ static int luo_file_can_finish_one(struct luo_file_set *file_set,
 
 		args.handler = luo_file->fh;
 		args.file = luo_file->file;
-		args.serialized_data = luo_file->serialized_data;
+		KHOSER_COPY_TYPESAFE(args.serialized_data, luo_file->serialized_data);
 		args.retrieve_status = luo_file->retrieve_status;
 		can_finish = luo_file->fh->ops->can_finish(&args);
 	}
@@ -672,7 +672,7 @@ static void luo_file_finish_one(struct luo_file_set *file_set,
 
 	args.handler = luo_file->fh;
 	args.file = luo_file->file;
-	args.serialized_data = luo_file->serialized_data;
+	KHOSER_COPY_TYPESAFE(args.serialized_data, luo_file->serialized_data);
 	args.retrieve_status = luo_file->retrieve_status;
 
 	luo_file->fh->ops->finish(&args);
@@ -837,7 +837,7 @@ int luo_file_deserialize(struct luo_file_set *file_set,
 
 		luo_file->fh = fh;
 		luo_file->file = NULL;
-		luo_file->serialized_data = file_ser[i].data;
+		KHOSER_COPY_TYPESAFE(luo_file->serialized_data, file_ser[i].serialized_data);
 		luo_file->token = file_ser[i].token;
 		mutex_init(&luo_file->mutex);
 		list_add_tail(&luo_file->list, &file_set->files_list);
mm/memfd_luo.c +9 -9
index 59de210bee5f..daf2c4c3da35 100644
@@ -257,6 +257,7 @@ static void memfd_luo_unpreserve_folios(struct kho_vmalloc *kho_vmalloc,
 
 static int memfd_luo_preserve(struct liveupdate_file_op_args *args)
 {
+	DECLARE_KHOSER_PTR(sd, struct memfd_luo_ser *);
 	struct inode *inode = file_inode(args->file);
 	struct memfd_luo_folio_ser *folios_ser;
 	struct memfd_luo_ser *ser;
@@ -309,7 +310,8 @@ static int memfd_luo_preserve(struct liveupdate_file_op_args *args)
 	inode_unlock(inode);
 
 	args->private_data = folios_ser;
-	args->serialized_data = virt_to_phys(ser);
+	KHOSER_STORE_PTR(sd, ser);
+	KHOSER_COPY_TYPEUNSAFE(args->serialized_data, sd);
 
 	return 0;
 
@@ -325,11 +327,10 @@ static int memfd_luo_freeze(struct liveupdate_file_op_args *args)
 {
 	struct memfd_luo_ser *ser;
 
-	if (WARN_ON_ONCE(!args->serialized_data))
+	ser = KHOSER_LOAD_PTR(args->serialized_data);
+	if (WARN_ON_ONCE(!ser))
 		return -EINVAL;
 
-	ser = phys_to_virt(args->serialized_data);
-
 	/*
 	 * The pos might have changed since prepare. Everything else stays the
 	 * same.
@@ -344,14 +345,13 @@ static void memfd_luo_unpreserve(struct liveupdate_file_op_args *args)
 	struct inode *inode = file_inode(args->file);
 	struct memfd_luo_ser *ser;
 
-	if (WARN_ON_ONCE(!args->serialized_data))
+	ser = KHOSER_LOAD_PTR(args->serialized_data);
+	if (WARN_ON_ONCE(!ser))
 		return;
 
 	inode_lock(inode);
 	shmem_freeze(inode, false);
 
-	ser = phys_to_virt(args->serialized_data);
-
 	memfd_luo_unpreserve_folios(&ser->folios, args->private_data,
 				    ser->nr_folios);
 
@@ -397,7 +397,7 @@ static void memfd_luo_finish(struct liveupdate_file_op_args *args)
 	if (args->retrieve_status)
 		return;
 
-	ser = phys_to_virt(args->serialized_data);
+	ser = KHOSER_LOAD_PTR(args->serialized_data);
 	if (!ser)
 		return;
 
@@ -522,7 +522,7 @@ static int memfd_luo_retrieve(struct liveupdate_file_op_args *args)
 	struct file *file;
 	int err;
 
-	ser = phys_to_virt(args->serialized_data);
+	ser = KHOSER_LOAD_PTR(args->serialized_data);
 	if (!ser)
 		return -EINVAL;
 
Signature · 1 line
2.54.0.1136.gdb2ca164c4-goog
#3 lore raw
In reply to Tarun Sahu

+Mike, +Pasha

This is currently rebased on top of mailine, also can be applied to liveupdate/fixes with no conflict. I would like to know which release cycle we are targeting for this, so That I can rebase it accordingly?

As for liveupdate/next, it needs some conlfict resolution.

Quoted 19 lines · Tarun Sahu

Covvert raw serialized_data to KHO serializeable pointer (KHOSER_PTR). This series also takes care of resolving the bug with memfd of using phys_to_virt before checking the args->serialized_data value.

Tarun Sahu (2):
kho: add KHOSER_COPY_TYPE(UN)SAFE for phys copy
luo: Update serialized data to serializeable pointer

 include/linux/kho/abi/kexec_handover.h | 12 ++++++++++++
 include/linux/kho/abi/luo.h            |  6 ++++--
 include/linux/liveupdate.h             |  4 ++--
 kernel/liveupdate/luo_file.c           | 24 ++++++++++++------------
 mm/memfd_luo.c                         | 18 +++++++++---------
 5 files changed, 39 insertions(+), 25 deletions(-)

base-commit: 0e0611827f3349d0a2ac121c023a6d3260dcecdb

Signature · 1 line
2.54.0.1136.gdb2ca164c4-goog
maintainer
#4 lore raw
Quoted 7 lines · On Fri, Jun 19 2026, [email protected] wrote:

+Mike, +Pasha

This is currently rebased on top of mailine, also can be applied to liveupdate/fixes with no conflict. I would like to know which release cycle we are targeting for this, so That I can rebase it accordingly?

As for liveupdate/next, it needs some conlfict resolution.

Base it on liveupdate/next after the merge window. Since nothing is actively broken (as far as I can tell), I don't think we should take it via fixes. We should do it via the 7.3 merge window.

Quoted 6 lines
Quoted 3 lines · Tarun Sahu

Covvert raw serialized_data to KHO serializeable pointer (KHOSER_PTR). This series also takes care of resolving the bug with memfd of using phys_to_virt before checking the args->serialized_data value.

[...]

Signature · 2 lines
Regards,
Pratyush Yadav
#5 lore raw
Quoted 8 lines · Pratyush Yadav
Quoted 6 lines · On Fri, Jun 19 2026, [email protected] wrote:

+Mike, +Pasha

This is currently rebased on top of mailine, also can be applied to liveupdate/fixes with no conflict. I would like to know which release cycle we are targeting for this, so That I can rebase it accordingly?

Quoted 5 lines
Quoted 1 line

As for liveupdate/next, it needs some conlfict resolution.

Base it on liveupdate/next after the merge window. Since nothing is actively broken (as far as I can tell), I don't think we should take it via fixes. We should do it via the 7.3 merge window.

This is gonna be used in guest_memfd preservation as well. Next version (V3) Should be based on top of it. And if we delay this (as it is gonna be with liveupdate/next) it will delay the review cycle of guest_memfd as well.

~Tarun

Quoted 12 lines
Quoted 7 lines
Quoted 3 lines · Tarun Sahu

Covvert raw serialized_data to KHO serializeable pointer (KHOSER_PTR). This series also takes care of resolving the bug with memfd of using phys_to_virt before checking the args->serialized_data value.

[...]

Signature · 2 lines
Regards,
Pratyush Yadav