Skip to contentSkip to conversation

mm/memfd_luo: use KHOSER_PTR for serialized_data checks

PATCH In review Tarun Sahu 2 participants 3 messages Jun 15 – Jun 16

Subsystems (3)
lore
Share & tools
lore

Tarun Sahu

author
#0 lore raw

cover mm/memfd_luo: use KHOSER_PTR for serialized_data checks

Use the KHOSER_PTR interface for serialized_data in memfd_luo to perform type-safe pointer conversions and checks.

I have tried to keep the changes minimal. So only changed the memfd_luo. Would love the suggestions for if we should be changing struct liveupdate_file_op_args like the following.

@@ -46,7 +47,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;
};

Ofcourse, it will require change in liveupdate and luo_file.

Tarun Sahu (1):
mm/memfd_luo: use KHOSER_PTR for serialized_data checks

 mm/memfd_luo.c | 30 +++++++++++++++++++++---------
 1 file changed, 21 insertions(+), 9 deletions(-)

base-commit: 0e0611827f3349d0a2ac121c023a6d3260dcecdb

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

PATCH 1/1 mm/memfd_luo: use KHOSER_PTR for serialized_data checks

In all callbacks (freeze, unpreserve, finish, retrieve), use the standard KHOSER_LOAD_PTR() and KHOSER_STORE_PTR() macros to safely load and store the serialized structure pointer.

This resolves the issue where phys_to_virt() was previously called before checking if the physical address is valid, since KHOSER_LOAD_PTR() automatically handles translation and returns NULL if the physical address is 0.

Fixes b3749f174d68 ("mm: memfd_luo: allow preserving memfd")
---
 mm/memfd_luo.c | 30 +++++++++++++++++++++---------
 1 file changed, 21 insertions(+), 9 deletions(-)
mm/memfd_luo.c +21 -9
index 59de210bee5f..ec37693841a6 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);
+	args->serialized_data = sd.phys;
 
 	return 0;
 
@@ -323,13 +325,15 @@ static int memfd_luo_preserve(struct liveupdate_file_op_args *args)
 
 static int memfd_luo_freeze(struct liveupdate_file_op_args *args)
 {
+	DECLARE_KHOSER_PTR(sd, struct memfd_luo_ser *) = {
+		.phys = args->serialized_data
+	};
 	struct memfd_luo_ser *ser;
 
-	if (WARN_ON_ONCE(!args->serialized_data))
+	ser = KHOSER_LOAD_PTR(sd);
+	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.
@@ -341,17 +345,19 @@ static int memfd_luo_freeze(struct liveupdate_file_op_args *args)
 
 static void memfd_luo_unpreserve(struct liveupdate_file_op_args *args)
 {
+	DECLARE_KHOSER_PTR(sd, struct memfd_luo_ser *) = {
+		.phys = args->serialized_data
+	};
 	struct inode *inode = file_inode(args->file);
 	struct memfd_luo_ser *ser;
 
-	if (WARN_ON_ONCE(!args->serialized_data))
+	ser = KHOSER_LOAD_PTR(sd);
+	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);
 
@@ -386,6 +392,9 @@ static void memfd_luo_discard_folios(const struct memfd_luo_folio_ser *folios_se
 
 static void memfd_luo_finish(struct liveupdate_file_op_args *args)
 {
+	DECLARE_KHOSER_PTR(sd, struct memfd_luo_ser *) = {
+		.phys = args->serialized_data
+	};
 	struct memfd_luo_folio_ser *folios_ser;
 	struct memfd_luo_ser *ser;
 
@@ -397,7 +406,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(sd);
 	if (!ser)
 		return;
 
@@ -517,12 +526,15 @@ static int memfd_luo_retrieve_folios(struct file *file,
 
 static int memfd_luo_retrieve(struct liveupdate_file_op_args *args)
 {
+	DECLARE_KHOSER_PTR(sd, struct memfd_luo_ser *) = {
+		.phys = args->serialized_data
+	};
 	struct memfd_luo_folio_ser *folios_ser;
 	struct memfd_luo_ser *ser;
 	struct file *file;
 	int err;
 
-	ser = phys_to_virt(args->serialized_data);
+	ser = KHOSER_LOAD_PTR(sd);
 	if (!ser)
 		return -EINVAL;
 
Signature · 1 line
2.54.0.1136.gdb2ca164c4-goog
maintainer
#2 lore raw
In reply to Tarun Sahu

Hi,

Review struct liveupdate_file_op_args { · line 50

Quoted 13 lines · Tarun Sahu

Use the KHOSER_PTR interface for serialized_data in memfd_luo to perform type-safe pointer conversions and checks.

I have tried to keep the changes minimal. So only changed the memfd_luo. Would love the suggestions for if we should be changing struct liveupdate_file_op_args like the following.

(quoted diff) +1 -1
@@ -46,7 +47,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 *);

Yes, this one please :)

Quoted 16 lines
(quoted diff) +0 -0
 	void *private_data;
 };
 

Ofcourse, it will require change in liveupdate and luo_file.

Tarun Sahu (1):
mm/memfd_luo: use KHOSER_PTR for serialized_data checks

 mm/memfd_luo.c | 30 +++++++++++++++++++++---------
 1 file changed, 21 insertions(+), 9 deletions(-)

base-commit: 0e0611827f3349d0a2ac121c023a6d3260dcecdb

Signature · 1 line
2.54.0.1136.gdb2ca164c4-goog
Signature · 2 lines
Sincerely yours,
Mike.