liveupdate: fixes in error handling
Subsystems (2)
Pratyush Yadav
maintainercover liveupdate: fixes in error handling
From: "Pratyush Yadav (Google)" <[email protected]>
Hi Andrew,
This series contains some fixes in LUO's error handling paths.
The first patch deals with failed freeze() attempts. The cleanup path calls unfreeze, and that clears some data needed by later unpreserve calls.
The second patch is a bit more involved. It deals with failed retrieve() attempts. To do so properly, it reworks some of the error handling logic in luo_file core.
Both these fixes are "theoretical" -- in the sense that I have not been able to reproduce either of them in normal operation. The only supported file type right now is memfd, and there is nothing userspace can do right now to make it fail its retrieve or freeze. I need to make the retrieve or freeze fail by artificially injecting errors. The injected errors trigger a use-after-free and a double-free.
That said, once more complex file handlers are added or memfd preservation is used in ways not currently expected or covered by the tests, we will be able to see them on real systems.
Regards,
Pratyush Yadav
Pratyush Yadav (Google) (2):
liveupdate: luo_file: do not clear serialized_data on unfreeze
liveupdate: luo_file: remember retrieve() status
include/linux/liveupdate.h | 7 ++++-- kernel/liveupdate/luo_file.c | 43 +++++++++++++++++++++--------------- mm/memfd_luo.c | 7 +++++- 3 files changed, 36 insertions(+), 21 deletions(-)
base-commit: 2e833046d66a40f9ad3de2948e6ecd791939fa59
Signature · 1 line
2.52.0.457.g6b5491de43-goog
Pratyush Yadav
maintainerPATCH 1/2 liveupdate: luo_file: do not clear serialized_data on unfreeze
From: "Pratyush Yadav (Google)" <[email protected]>
The unfreeze operation is supposed to undo the effects of the freeze operation. serialized_data is not set by freeze, but by preserve. Consequently, the unpreserve operation needs to access serialized_data to undo the effects of the preserve operation. This includes freeing the serialized data structures for example.
If a freeze callback fails, unfreeze is called for all frozen files. This would clear serialized_data for them. Since live update has failed, it can be expected that userspace aborts, releasing all sessions. When the sessions are released, unpreserve will be called for all files. The unfrozen files will see 0 in their serialized_data. This is not expected by file handlers, and they might either fail, leaking data and state, or might even crash or cause invalid memory access.
Do not clear serialized_data on unfreeze so it gets passed on to unpreserve. There is no need to clear it on unpreserve since luo_file will be freed immediately after.
--- kernel/liveupdate/luo_file.c | 2 -- 1 file changed, 2 deletions(-)
kernel/liveupdate/luo_file.c +0 -2
@@ -402,8 +402,6 @@ static void luo_file_unfreeze_one(struct luo_file_set *file_set, luo_file->fh->ops->unfreeze (&args); } - - luo_file->serialized_data =0 ; } static void __luo_file_unfreeze(struct luo_file_set *file_set,
Signature · 1 line
2.52.0.457.g6b5491de43-goog
Re: [PATCH 1/2] liveupdate: luo_file: do not clear serialized_data on unfreeze
Quoted 16 lines · Pratyush Yadav
From: "Pratyush Yadav (Google)" <[email protected]>
The unfreeze operation is supposed to undo the effects of the freeze operation.
serialized_datais not set by freeze, but by preserve. Consequently, the unpreserve operation needs to accessserialized_datato undo the effects of the preserve operation. This includes freeing the serialized data structures for example.If a freeze callback fails, unfreeze is called for all frozen files. This would clear
serialized_datafor them. Since live update has failed, it can be expected that userspace aborts, releasing all sessions. When the sessions are released, unpreserve will be called for all files. The unfrozen files will see 0 in theirserialized_data. This is not expected by file handlers, and they might either fail, leaking data and state, or might even crash or cause invalid memory access.
Quoted 6 lines
Do not clear
serialized_dataon unfreeze so it gets passed on to unpreserve. There is no need to clear it on unpreserve sinceluo_filewill be freed immediately after.Fixes 7c722a7f44e0 ("liveupdate: luo_file: implement file systems callbacks")Signed-off-by Pratyush Yadav (Google) <[email protected]>
Quoted 20 lines
--- kernel/liveupdate/luo_file.c | 2 -- 1 file changed, 2 deletions(-)kernel/liveupdate/luo_file.c +0
-2@@ -402,8 +402,6 @@ static void luo_file_unfreeze_one(struct luo_file_set *file_set, luo_file->fh->ops->unfreeze (&args); } - - luo_file->serialized_data =0 ; } static void __luo_file_unfreeze(struct luo_file_set *file_set,Signature · 1 line
2.52.0.457.g6b5491de43-goog
Signature · 2 lines
Sincerely yours, Mike.
Pratyush Yadav
maintainerRe: [PATCH 1/2] liveupdate: luo_file: do not clear serialized_data on unfreeze
Quoted 17 lines · Mike Rapoport
Quoted 16 lines · Pratyush Yadav
From: "Pratyush Yadav (Google)" <[email protected]>
The unfreeze operation is supposed to undo the effects of the freeze operation.
serialized_datais not set by freeze, but by preserve. Consequently, the unpreserve operation needs to accessserialized_datato undo the effects of the preserve operation. This includes freeing the serialized data structures for example.If a freeze callback fails, unfreeze is called for all frozen files. This would clear
serialized_datafor them. Since live update has failed, it can be expected that userspace aborts, releasing all sessions. When the sessions are released, unpreserve will be called for all files. The unfrozen files will see 0 in theirserialized_data. This is not expected by file handlers, and they might either fail, leaking data and state, or might even crash or cause invalid memory access.
Quoted 8 lines
Quoted 6 lines
Do not clear
serialized_dataon unfreeze so it gets passed on to unpreserve. There is no need to clear it on unpreserve sinceluo_filewill be freed immediately after.Fixes 7c722a7f44e0 ("liveupdate: luo_file: implement file systems callbacks")Signed-off-by Pratyush Yadav (Google) <[email protected]>Reviewed-by Mike Rapoport (Microsoft) <[email protected]>
Thanks.
Andrew, do you mind taking this patch in v6.19-rc8/v6.19 hotfixes? The second patch needs a re-roll and I don't think I would be able to do it this week.
This is an independent fix and I see no reason to block this waiting for the other one.
[...]
Signature · 2 lines
Regards, Pratyush Yadav
Andrew Morton
maintainerRe: [PATCH 1/2] liveupdate: luo_file: do not clear serialized_data on unfreeze
Quoted 8 lines · On Fri, 30 Jan 2026 19:56:28 +0100 Pratyush Yadav <[email protected]> wrote:
Quoted 4 lines
Quoted 2 lines
Fixes 7c722a7f44e0 ("liveupdate: luo_file: implement file systems callbacks")Signed-off-by Pratyush Yadav (Google) <[email protected]>Reviewed-by Mike Rapoport (Microsoft) <[email protected]>Thanks.
Andrew, do you mind taking this patch in v6.19-rc8/v6.19 hotfixes?
Yep, that's where it was.
Quoted 6 lines
The
second patch needs a re-roll and I don't think I would be able to do it this week.This is an independent fix and I see no reason to block this waiting for the other one.
OK, I moved "liveupdate: luo_file: remember retrieve() status" to tail-of-everything and added a note that a new version is expected.
Pratyush Yadav
maintainerRe: [PATCH 1/2] liveupdate: luo_file: do not clear serialized_data on unfreeze
Quoted 5 lines · Andrew Morton
Quoted 3 lines · On Fri, 30 Jan 2026 19:56:28 +0100 Pratyush Yadav <[email protected]> wrote:
Quoted 3 lines
Quoted 2 lines
Fixes 7c722a7f44e0 ("liveupdate: luo_file: implement file systems callbacks")Signed-off-by Pratyush Yadav (Google) <[email protected]>
Quoted 7 lines
Quoted 5 lines
Quoted 1 line
Reviewed-by Mike Rapoport (Microsoft) <[email protected]>Thanks.
Andrew, do you mind taking this patch in v6.19-rc8/v6.19 hotfixes?
Yep, that's where it was.
Thanks. I didn't realize there was going to be an -rc8 and I didn't want this patch to miss the final cut of v6.19 in case you were expecting a re-roll of the whole series.
Quoted 10 lines
Quoted 7 lines
The
second patch needs a re-roll and I don't think I would be able to do it this week.This is an independent fix and I see no reason to block this waiting for the other one.
OK, I moved "liveupdate:
luo_file: rememberretrieve()status" to tail-of-everything and added a note that a new version is expected.
Sounds good. I'll try to send out the new version in the next couple days so we can include it in v6.19 final.
Signature · 2 lines
Regards, Pratyush Yadav
Pratyush Yadav
maintainerPATCH 2/2 liveupdate: luo_file: remember retrieve() status
In reply to Pratyush YadavFrom: "Pratyush Yadav (Google)" <[email protected]>
LUO keeps track of successful retrieve attempts on a LUO file. It does so to avoid multiple retrievals of the same file. Doing so will cause problems because once the file is retrieved, the serialized data structures are likely freed and the file is likely in a very different state from what the code expects.
This is kept track of by the retrieved boolean in struct luo_file, and is passed to the finish callback so it knows what work was already done and what it has left to do.
All this works well when retrieve succeeds. When it fails, luo_retrieve_file() returns the error immediately, without ever storing anywhere that a retrieve was attempted or what its error code was. This results in an errored LIVEUPDATE_SESSION_RETRIEVE_FD ioctl to userspace, but nothing prevents it from trying this again.
The retry is problematic for much of the same reasons listed above. The file is likely in a very different state than what the retrieve logic normally expects, and it might even have freed some serialization data structures. Attempting to access them or free them again is going to break things.
For example, if memfd managed to restore 8 of its 10 folios, but fails on the 9th, a subsequent retrieve attempt will try to call kho_restore_folio() on the first folio again, and that will fail with a warning since it is an invalid operation.
Apart from the retry, finish() also breaks. Since on failure the retrieved bool in luo_file is never touched, the finish() call on session close will tell the file handler that retrieve was never attempted, and it will try to access or free the data structures that might not exist, much in the same way as the retry attempt.
There is no sane way of attempting the retrieve again. Remember the error retrieve returned and directly return it on a retry. Also pass this status code to finish() so it can make the right decision on the work it needs to do.
This is done by changing the bool to an integer. A value of 0 means retrieve was never attempted, a positive value means it succeeded, and a negative value means it failed and the error code is the value.
--- include/linux/liveupdate.h | 7 ++++-- kernel/liveupdate/luo_file.c | 41 ++++++++++++++++++++++-------------- mm/memfd_luo.c | 7 +++++- 3 files changed, 36 insertions(+), 19 deletions(-)
include/linux/liveupdate.h +5 -2
@@ -21,7 +21,10 @@ struct file;* struct liveupdate_file_op_args - Arguments for file operation callbacks. * @handler: The file handler being called. * @retrieved: The retrieve status for the 'can_finish / finish' -* operation. +* operation. A value of 0 means the retrieve has not been +* attempted, a positive value means the retrieve was +* successful, and a negative value means the retrieve failed, +* and the value is the error code of the call. * @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.
@@ -37,7 +40,7 @@ struct file;*/ struct liveupdate_file_op_args {struct liveupdate_file_handler *handler; -bool retrieved; +bool retrieve_sts;struct file *file;u64 serialized_data;void *private_data;
kernel/liveupdate/luo_file.c +25 -16
97 lines not on this page · raw message
mm/memfd_luo.c +6 -1
@@ -326,7 +326,12 @@ static void memfd_luo_finish(struct liveupdate_file_op_args *args)struct memfd_luo_folio_ser *folios_ser;struct memfd_luo_ser *ser; -if (args->retrieved) +/* +* 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_sts)return ; ser =phys_to_virt (args->serialized_data);
Signature · 1 line
2.52.0.457.g6b5491de43-goog
Mike Rapoport
maintainerRe: [PATCH 2/2] liveupdate: luo_file: remember retrieve() status
Hi Pratyush,
Quoted 4 lines · Pratyush Yadav
From: "Pratyush Yadav (Google)" <[email protected]>
LUO keeps track of successful retrieve attempts on a LUO file. It does so to avoid multiple retrievals of the same file. Doing so will cause
^ Multiple retrievals
Quoted 5 lines
problems because once the file is retrieved, the serialized data structures are likely freed and the file is likely in a very different state from what the code expects.
This is kept track of by the retrieved boolean in struct
luo_file, and
The 'retrieve' boolean in struct luo_file keeps track of this,
Quoted 60 lines
is passed to the finish callback so it knows what work was already done and what it has left to do.
All this works well when retrieve succeeds. When it fails,
luo_retrieve_file()returns the error immediately, without ever storing anywhere that a retrieve was attempted or what its error code was. This results in an erroredLIVEUPDATE_SESSION_RETRIEVE_FDioctl to userspace, but nothing prevents it from trying this again.The retry is problematic for much of the same reasons listed above. The file is likely in a very different state than what the retrieve logic normally expects, and it might even have freed some serialization data structures. Attempting to access them or free them again is going to break things.
For example, if memfd managed to restore 8 of its 10 folios, but fails on the 9th, a subsequent retrieve attempt will try to call
kho_restore_folio()on the first folio again, and that will fail with a warning since it is an invalid operation.Apart from the retry,
finish()also breaks. Since on failure the retrieved bool inluo_fileis never touched, thefinish()call on session close will tell the file handler that retrieve was never attempted, and it will try to access or free the data structures that might not exist, much in the same way as the retry attempt.There is no sane way of attempting the retrieve again. Remember the error retrieve returned and directly return it on a retry. Also pass this status code to
finish()so it can make the right decision on the work it needs to do.This is done by changing the bool to an integer. A value of 0 means retrieve was never attempted, a positive value means it succeeded, and a negative value means it failed and the error code is the value.
Fixes 7c722a7f44e0 ("liveupdate: luo_file: implement file systems callbacks")Signed-off-by Pratyush Yadav (Google) <[email protected]>--- include/linux/liveupdate.h | 7 ++++-- kernel/liveupdate/luo_file.c | 41 ++++++++++++++++++++++-------------- mm/memfd_luo.c | 7 +++++- 3 files changed, 36 insertions(+), 19 deletions(-)include/linux/liveupdate.h +4
-1@@ -21,7 +21,10 @@ struct file;* struct liveupdate_file_op_args - Arguments for file operation callbacks. * @handler: The file handler being called. * @retrieved: The retrieve status for the 'can_finish / finish' -* operation. +* operation. A value of 0 means the retrieve has not been +* attempted, a positive value means the retrieve was +* successful, and a negative value means the retrieve failed, +* and the value is the error code of the call. * @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.
include/linux/liveupdate.h struct file; · line 43
Quoted 5 lines
include/linux/liveupdate.h +1
-1@@ -37,7 +40,7 @@ struct file;*/ struct liveupdate_file_op_args {struct liveupdate_file_handler *handler; -bool retrieved; +bool retrieve_sts;
int retrieve_sts?
and maybe spell out _status rather than _sts?
Quoted 32 lines
include/linux/liveupdate.h +0
-0struct file *file;u64 serialized_data;void *private_data;kernel/liveupdate/luo_file.c +6
-3@@ -133,9 +133,12 @@ static LIST_HEAD(luo_file_handler_list);* state that is not preserved. Set by the handler's .preserve() * callback, and must be freed in the handler's .unpreserve() * callback. -* @retrieved: A flag indicating whether a user/kernel in the new kernel has +* @retrieve_sts: Status code indicating whether a user/kernel in the new kernel has * successfully called retrieve() on this file. This prevents -* multiple retrieval attempts. +* multiple retrieval attempts. A value of 0 means a retrieve() +* has not been attempted, a positive value means the retrieve() +* was successful, and a negative value means the retrieve() +* failed, and the value is the error code of the call. * @mutex: A mutex that protects the fields of this specific instance * (e.g., @retrieved, @file), ensuring that operations like * retrieving or finishing a file are atomic. @@ -160,7 +163,7 @@ struct luo_file {struct file *file;u64 serialized_data;void *private_data; -bool retrieved; +int retrieve_sts;struct mutex mutex;struct list_head list;u64 token;
kernel/liveupdate/luo_file.c int luo_preserve_file(struct luo_file_set *file_set, u64 token, int fd) · line 299
Quoted 5 lines
kernel/liveupdate/luo_file.c +1
-1@@ -293,7 +296,7 @@ int luo_preserve_file(struct luo_file_set *file_set, u64 token, int fd) luo_file->file = file; luo_file->fh = fh; luo_file->token = token; - luo_file->retrieved = false; + luo_file->retrieve_sts =0 ;
We kzalloc() luo_file, so this is not strictly required.
Quoted 13 lines
kernel/liveupdate/luo_file.c +1
-1mutex_init (&luo_file->mutex); args.handler = fh;@@ -569,7 +572,7 @@ int luo_retrieve_file(struct luo_file_set *file_set, u64 token,return -ENOENT ;guard (mutex)(&luo_file->mutex); -if (luo_file->retrieved) { +if (luo_file->retrieve_sts >0 ) {/* * Someone is asking for this file again, so get a reference * for them.
kernel/liveupdate/luo_file.c int luo_retrieve_file(struct luo_file_set *file_set, u64 token, · line 586
Quoted 7 lines
kernel/liveupdate/luo_file.c +3
-0@@ -577,21 +580,27 @@ int luo_retrieve_file(struct luo_file_set *file_set, u64 token,get_file (luo_file->file); *filep = luo_file->file;return 0 ; + }else if (luo_file->retrieve_sts <0 ) { +/* Retrieve was attempted and it failed. Return the error code. */ +return luo_file->retrieve_sts; }
I'd put it before the check for > 0, i.e
if (luo_file->retrieve_sts < 0) return luo_file->retrieve_sts; if (luo_file->retrieve_sts > 0) ...
Quoted 46 lines
kernel/liveupdate/luo_file.c +13
-10args.handler = luo_file->fh; args.serialized_data = luo_file->serialized_data; err = luo_file->fh->ops->retrieve (&args); -if (!err) { - luo_file->file = args.file; - -/* Get reference so we can keep this file in LUO until finish */ -get_file (luo_file->file); - *filep = luo_file->file; - luo_file->retrieved = true; +if (err) { +/* Keep the error code for later use. */ + luo_file->retrieve_sts = err; +return err; } -return err; + luo_file->file = args.file; +/* Get reference so we can keep this file in LUO until finish */ +get_file (luo_file->file); + *filep = luo_file->file; + luo_file->retrieve_sts =1 ; + +return 0 ; } static int luo_file_can_finish_one(struct luo_file_set *file_set,@@ -607,7 +616,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; - args.retrieved = luo_file->retrieved; + args.retrieve_sts = luo_file->retrieve_sts; can_finish = luo_file->fh->ops->can_finish (&args); }@@ -624,7 +633,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; - args.retrieved = luo_file->retrieved; + args.retrieve_sts = luo_file->retrieve_sts; luo_file->fh->ops->finish (&args); }
kernel/liveupdate/luo_file.c int luo_file_deserialize(struct luo_file_set *file_set, · line 791
Quoted 5 lines
kernel/liveupdate/luo_file.c +1
-1@@ -779,7 +788,7 @@ int luo_file_deserialize(struct luo_file_set *file_set, luo_file->file =NULL ; luo_file->serialized_data = file_ser[i].data; luo_file->token = file_ser[i].token; - luo_file->retrieved = false; + luo_file->retrieve_sts =0 ;
Here as well, we kzalloc() luo_file, so zeroing out of the fields is not strictly required.
Quoted 24 lines
kernel/liveupdate/luo_file.c +0
-0mutex_init (&luo_file->mutex);list_add_tail (&luo_file->list, &file_set->files_list); }mm/memfd_luo.c +6
-1@@ -326,7 +326,12 @@ static void memfd_luo_finish(struct liveupdate_file_op_args *args)struct memfd_luo_folio_ser *folios_ser;struct memfd_luo_ser *ser; -if (args->retrieved) +/* +* 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_sts)return ; ser =phys_to_virt (args->serialized_data);Signature · 1 line
2.52.0.457.g6b5491de43-goog
Signature · 2 lines
Sincerely yours, Mike.
Pratyush Yadav
maintainerRe: [PATCH 2/2] liveupdate: luo_file: remember retrieve() status
Hi Mike,
Thanks for the review.
Quoted 9 lines · Mike Rapoport
Hi Pratyush,
Quoted 4 lines · Pratyush Yadav
From: "Pratyush Yadav (Google)" <[email protected]>
LUO keeps track of successful retrieve attempts on a LUO file. It does so to avoid multiple retrievals of the same file. Doing so will cause
^ Multiple retrievals
Will fix.
Quoted 8 lines
Quoted 6 lines
problems because once the file is retrieved, the serialized data structures are likely freed and the file is likely in a very different state from what the code expects.
This is kept track of by the retrieved boolean in struct
luo_file, andThe 'retrieve' boolean in struct
luo_filekeeps track of this,
ACK.
Quoted 60 lines
Quoted 60 lines
is passed to the finish callback so it knows what work was already done and what it has left to do.
All this works well when retrieve succeeds. When it fails,
luo_retrieve_file()returns the error immediately, without ever storing anywhere that a retrieve was attempted or what its error code was. This results in an erroredLIVEUPDATE_SESSION_RETRIEVE_FDioctl to userspace, but nothing prevents it from trying this again.The retry is problematic for much of the same reasons listed above. The file is likely in a very different state than what the retrieve logic normally expects, and it might even have freed some serialization data structures. Attempting to access them or free them again is going to break things.
For example, if memfd managed to restore 8 of its 10 folios, but fails on the 9th, a subsequent retrieve attempt will try to call
kho_restore_folio()on the first folio again, and that will fail with a warning since it is an invalid operation.Apart from the retry,
finish()also breaks. Since on failure the retrieved bool inluo_fileis never touched, thefinish()call on session close will tell the file handler that retrieve was never attempted, and it will try to access or free the data structures that might not exist, much in the same way as the retry attempt.There is no sane way of attempting the retrieve again. Remember the error retrieve returned and directly return it on a retry. Also pass this status code to
finish()so it can make the right decision on the work it needs to do.This is done by changing the bool to an integer. A value of 0 means retrieve was never attempted, a positive value means it succeeded, and a negative value means it failed and the error code is the value.
Fixes 7c722a7f44e0 ("liveupdate: luo_file: implement file systems callbacks")Signed-off-by Pratyush Yadav (Google) <[email protected]>--- include/linux/liveupdate.h | 7 ++++-- kernel/liveupdate/luo_file.c | 41 ++++++++++++++++++++++-------------- mm/memfd_luo.c | 7 +++++- 3 files changed, 36 insertions(+), 19 deletions(-)include/linux/liveupdate.h +4
-1@@ -21,7 +21,10 @@ struct file;* struct liveupdate_file_op_args - Arguments for file operation callbacks. * @handler: The file handler being called. * @retrieved: The retrieve status for the 'can_finish / finish' -* operation. +* operation. A value of 0 means the retrieve has not been +* attempted, a positive value means the retrieve was +* successful, and a negative value means the retrieve failed, +* and the value is the error code of the call. * @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.
Quoted 8 lines
include/linux/liveupdate.h struct file; · line 43
Quoted 6 lines
include/linux/liveupdate.h +1
-1@@ -37,7 +40,7 @@ struct file;*/ struct liveupdate_file_op_args {struct liveupdate_file_handler *handler; -bool retrieved; +bool retrieve_sts;int
retrieve_sts?
Ugh, stupid mistake... Thanks for catching.
Quoted 2 lines
and maybe spell out
_statusrather than_sts?
Will do.
Quoted 32 lines
Quoted 32 lines
include/linux/liveupdate.h +0
-0struct file *file;u64 serialized_data;void *private_data;kernel/liveupdate/luo_file.c +6
-3@@ -133,9 +133,12 @@ static LIST_HEAD(luo_file_handler_list);* state that is not preserved. Set by the handler's .preserve() * callback, and must be freed in the handler's .unpreserve() * callback. -* @retrieved: A flag indicating whether a user/kernel in the new kernel has +* @retrieve_sts: Status code indicating whether a user/kernel in the new kernel has * successfully called retrieve() on this file. This prevents -* multiple retrieval attempts. +* multiple retrieval attempts. A value of 0 means a retrieve() +* has not been attempted, a positive value means the retrieve() +* was successful, and a negative value means the retrieve() +* failed, and the value is the error code of the call. * @mutex: A mutex that protects the fields of this specific instance * (e.g., @retrieved, @file), ensuring that operations like * retrieving or finishing a file are atomic. @@ -160,7 +163,7 @@ struct luo_file {struct file *file;u64 serialized_data;void *private_data; -bool retrieved; +int retrieve_sts;struct mutex mutex;struct list_head list;u64 token;
Quoted 8 lines
kernel/liveupdate/luo_file.c int luo_preserve_file(struct luo_file_set *file_set, u64 token, int fd) · line 299
Quoted 6 lines
kernel/liveupdate/luo_file.c +1
-1@@ -293,7 +296,7 @@ int luo_preserve_file(struct luo_file_set *file_set, u64 token, int fd) luo_file->file = file; luo_file->fh = fh; luo_file->token = token; - luo_file->retrieved = false; + luo_file->retrieve_sts =0 ;We
kzalloc()luo_file, so this is not strictly required.
Okay, will drop.
Quoted 24 lines
Quoted 14 lines
kernel/liveupdate/luo_file.c +1
-1mutex_init (&luo_file->mutex); args.handler = fh;@@ -569,7 +572,7 @@ int luo_retrieve_file(struct luo_file_set *file_set, u64 token,return -ENOENT ;guard (mutex)(&luo_file->mutex); -if (luo_file->retrieved) { +if (luo_file->retrieve_sts >0 ) {/* * Someone is asking for this file again, so get a reference * for them. kernel/liveupdate/luo_file.c int luo_retrieve_file(struct luo_file_set *file_set, u64 token, · line 586
Quoted 7 lines
kernel/liveupdate/luo_file.c +3
-0@@ -577,21 +580,27 @@ int luo_retrieve_file(struct luo_file_set *file_set, u64 token,get_file (luo_file->file); *filep = luo_file->file;return 0 ; + }else if (luo_file->retrieve_sts <0 ) { +/* Retrieve was attempted and it failed. Return the error code. */ +return luo_file->retrieve_sts; }I'd put it before the check for > 0, i.e
Quoted 5 lines
if (luo_file->retrieve_sts < 0) return luo_file->retrieve_sts; if (luo_file->retrieve_sts > 0) ...
ACK. Will do.
Quoted 45 lines
Quoted 45 lines
kernel/liveupdate/luo_file.c +13
-10args.handler = luo_file->fh; args.serialized_data = luo_file->serialized_data; err = luo_file->fh->ops->retrieve (&args); -if (!err) { - luo_file->file = args.file; - -/* Get reference so we can keep this file in LUO until finish */ -get_file (luo_file->file); - *filep = luo_file->file; - luo_file->retrieved = true; +if (err) { +/* Keep the error code for later use. */ + luo_file->retrieve_sts = err; +return err; } -return err; + luo_file->file = args.file; +/* Get reference so we can keep this file in LUO until finish */ +get_file (luo_file->file); + *filep = luo_file->file; + luo_file->retrieve_sts =1 ; + +return 0 ; } static int luo_file_can_finish_one(struct luo_file_set *file_set,@@ -607,7 +616,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; - args.retrieved = luo_file->retrieved; + args.retrieve_sts = luo_file->retrieve_sts; can_finish = luo_file->fh->ops->can_finish (&args); }@@ -624,7 +633,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; - args.retrieved = luo_file->retrieved; + args.retrieve_sts = luo_file->retrieve_sts;
Quoted 11 lines
kernel/liveupdate/luo_file.c int luo_file_deserialize(struct luo_file_set *file_set, · line 791
Quoted 8 lines
kernel/liveupdate/luo_file.c +1
-1luo_file->fh->ops->finish (&args); }@@ -779,7 +788,7 @@ int luo_file_deserialize(struct luo_file_set *file_set, luo_file->file =NULL ; luo_file->serialized_data = file_ser[i].data; luo_file->token = file_ser[i].token; - luo_file->retrieved = false; + luo_file->retrieve_sts =0 ;Here as well, we
kzalloc()luo_file, so zeroing out of the fields is not strictly required.
ACK.
Quoted 25 lines
Quoted 25 lines
kernel/liveupdate/luo_file.c +0
-0mutex_init (&luo_file->mutex);list_add_tail (&luo_file->list, &file_set->files_list); }mm/memfd_luo.c +6
-1@@ -326,7 +326,12 @@ static void memfd_luo_finish(struct liveupdate_file_op_args *args)struct memfd_luo_folio_ser *folios_ser;struct memfd_luo_ser *ser; -if (args->retrieved) +/* +* 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_sts)return ; ser =phys_to_virt (args->serialized_data);Signature · 1 line
2.52.0.457.g6b5491de43-goog
Signature · 2 lines
Regards, Pratyush Yadav
Pasha Tatashin
maintainerRe: [PATCH 2/2] liveupdate: luo_file: remember retrieve() status
In reply to Pratyush YadavQuoted 58 lines · Pratyush Yadav
From: "Pratyush Yadav (Google)" <[email protected]>
LUO keeps track of successful retrieve attempts on a LUO file. It does so to avoid multiple retrievals of the same file. Doing so will cause problems because once the file is retrieved, the serialized data structures are likely freed and the file is likely in a very different state from what the code expects.
This is kept track of by the retrieved boolean in struct
luo_file, and is passed to the finish callback so it knows what work was already done and what it has left to do.All this works well when retrieve succeeds. When it fails,
luo_retrieve_file()returns the error immediately, without ever storing anywhere that a retrieve was attempted or what its error code was. This results in an erroredLIVEUPDATE_SESSION_RETRIEVE_FDioctl to userspace, but nothing prevents it from trying this again.The retry is problematic for much of the same reasons listed above. The file is likely in a very different state than what the retrieve logic normally expects, and it might even have freed some serialization data structures. Attempting to access them or free them again is going to break things.
For example, if memfd managed to restore 8 of its 10 folios, but fails on the 9th, a subsequent retrieve attempt will try to call
kho_restore_folio()on the first folio again, and that will fail with a warning since it is an invalid operation.Apart from the retry,
finish()also breaks. Since on failure the retrieved bool inluo_fileis never touched, thefinish()call on session close will tell the file handler that retrieve was never attempted, and it will try to access or free the data structures that might not exist, much in the same way as the retry attempt.There is no sane way of attempting the retrieve again. Remember the error retrieve returned and directly return it on a retry. Also pass this status code to
finish()so it can make the right decision on the work it needs to do.This is done by changing the bool to an integer. A value of 0 means retrieve was never attempted, a positive value means it succeeded, and a negative value means it failed and the error code is the value.
Fixes 7c722a7f44e0 ("liveupdate: luo_file: implement file systems callbacks")Signed-off-by Pratyush Yadav (Google) <[email protected]>--- include/linux/liveupdate.h | 7 ++++-- kernel/liveupdate/luo_file.c | 41 ++++++++++++++++++++++-------------- mm/memfd_luo.c | 7 +++++- 3 files changed, 36 insertions(+), 19 deletions(-)include/linux/liveupdate.h +0
-0
include/linux/liveupdate.h struct file; · line 23
Quoted 3 lines
include/linux/liveupdate.h +0
-0@@ -21,7 +21,10 @@ struct file;* struct liveupdate_file_op_args - Arguments for file operation callbacks. * @handler: The file handler being called. * @retrieved: The retrieve status for the 'can_finish / finish'
Rename retrieved to retrieve_sts
Quoted 147 lines
include/linux/liveupdate.h +5
-2-* operation. +* operation. A value of 0 means the retrieve has not been +* attempted, a positive value means the retrieve was +* successful, and a negative value means the retrieve failed, +* and the value is the error code of the call. * @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. @@ -37,7 +40,7 @@ struct file;*/ struct liveupdate_file_op_args {struct liveupdate_file_handler *handler; -bool retrieved; +bool retrieve_sts;struct file *file;u64 serialized_data;void *private_data;kernel/liveupdate/luo_file.c +25
-16@@ -133,9 +133,12 @@ static LIST_HEAD(luo_file_handler_list);* state that is not preserved. Set by the handler's .preserve() * callback, and must be freed in the handler's .unpreserve() * callback. -* @retrieved: A flag indicating whether a user/kernel in the new kernel has +* @retrieve_sts: Status code indicating whether a user/kernel in the new kernel has * successfully called retrieve() on this file. This prevents -* multiple retrieval attempts. +* multiple retrieval attempts. A value of 0 means a retrieve() +* has not been attempted, a positive value means the retrieve() +* was successful, and a negative value means the retrieve() +* failed, and the value is the error code of the call. * @mutex: A mutex that protects the fields of this specific instance * (e.g., @retrieved, @file), ensuring that operations like * retrieving or finishing a file are atomic. @@ -160,7 +163,7 @@ struct luo_file {struct file *file;u64 serialized_data;void *private_data; -bool retrieved; +int retrieve_sts;struct mutex mutex;struct list_head list;u64 token;@@ -293,7 +296,7 @@ int luo_preserve_file(struct luo_file_set *file_set, u64 token, int fd) luo_file->file = file; luo_file->fh = fh; luo_file->token = token; - luo_file->retrieved = false; + luo_file->retrieve_sts =0 ;mutex_init (&luo_file->mutex); args.handler = fh;@@ -569,7 +572,7 @@ int luo_retrieve_file(struct luo_file_set *file_set, u64 token,return -ENOENT ;guard (mutex)(&luo_file->mutex); -if (luo_file->retrieved) { +if (luo_file->retrieve_sts >0 ) {/* * Someone is asking for this file again, so get a reference * for them. @@ -577,21 +580,27 @@ int luo_retrieve_file(struct luo_file_set *file_set, u64 token,get_file (luo_file->file); *filep = luo_file->file;return 0 ; + }else if (luo_file->retrieve_sts <0 ) { +/* Retrieve was attempted and it failed. Return the error code. */ +return luo_file->retrieve_sts; } args.handler = luo_file->fh; args.serialized_data = luo_file->serialized_data; err = luo_file->fh->ops->retrieve (&args); -if (!err) { - luo_file->file = args.file; - -/* Get reference so we can keep this file in LUO until finish */ -get_file (luo_file->file); - *filep = luo_file->file; - luo_file->retrieved = true; +if (err) { +/* Keep the error code for later use. */ + luo_file->retrieve_sts = err; +return err; } -return err; + luo_file->file = args.file; +/* Get reference so we can keep this file in LUO until finish */ +get_file (luo_file->file); + *filep = luo_file->file; + luo_file->retrieve_sts =1 ; + +return 0 ; } static int luo_file_can_finish_one(struct luo_file_set *file_set,@@ -607,7 +616,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; - args.retrieved = luo_file->retrieved; + args.retrieve_sts = luo_file->retrieve_sts; can_finish = luo_file->fh->ops->can_finish (&args); }@@ -624,7 +633,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; - args.retrieved = luo_file->retrieved; + args.retrieve_sts = luo_file->retrieve_sts; luo_file->fh->ops->finish (&args); }@@ -779,7 +788,7 @@ int luo_file_deserialize(struct luo_file_set *file_set, luo_file->file =NULL ; luo_file->serialized_data = file_ser[i].data; luo_file->token = file_ser[i].token; - luo_file->retrieved = false; + luo_file->retrieve_sts =0 ;mutex_init (&luo_file->mutex);list_add_tail (&luo_file->list, &file_set->files_list); }mm/memfd_luo.c +6
-2@@ -326,7 +326,12 @@ static void memfd_luo_finish(struct liveupdate_file_op_args *args)struct memfd_luo_folio_ser *folios_ser;struct memfd_luo_ser *ser; -if (args->retrieved) +/* +* 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_sts)return ; ser =phys_to_virt (args->serialized_data); --2.52.0.457.g6b5491de43-goog
Pratyush Yadav
maintainerRe: [PATCH 2/2] liveupdate: luo_file: remember retrieve() status
Quoted 1 line · Pasha Tatashin
On Mon, Jan 26, 2026 at 6:03 PM Pratyush Yadav <[email protected]> wrote:
[...]
Quoted 10 lines
include/linux/liveupdate.h struct file; · line 23
Quoted 8 lines
include/linux/liveupdate.h +0
-0@@ -21,7 +21,10 @@ struct file;* struct liveupdate_file_op_args - Arguments for file operation callbacks. * @handler: The file handler being called. * @retrieved: The retrieve status for the 'can_finish / finish' Rename retrieved to
retrieve_sts
Will fix. Thanks.
Quoted 18 lines
Quoted 18 lines
include/linux/liveupdate.h +5
-2-* operation. +* operation. A value of 0 means the retrieve has not been +* attempted, a positive value means the retrieve was +* successful, and a negative value means the retrieve failed, +* and the value is the error code of the call. * @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. @@ -37,7 +40,7 @@ struct file;*/ struct liveupdate_file_op_args {struct liveupdate_file_handler *handler; -bool retrieved; +bool retrieve_sts;struct file *file;u64 serialized_data;void *private_data;
[...]
Signature · 2 lines
Regards, Pratyush Yadav