liveupdate: block outgoing session updates during reboot
Subsystems (4)
PATCH v3 1/5 liveupdate: block outgoing session updates during reboot
kernel_kexec() serializes outgoing sessions before the reboot path freezes tasks, so close() and session ioctls can still mutate a session while handover state is being prepared. The original v2 code also let incoming lookups keep a bare session pointer after dropping the list lock.
That leaves two correctness problems in the reboot path: outgoing state can change after serialization starts, and incoming sessions can be freed while another thread still holds a pointer to them.
Add refcounted session lifetime management, track in-flight outgoing close() paths with an atomic closing counter, and make serialization wait for closing to drain before setting rebooting. Reject phase-invalid ioctls, keep incoming release on a common cleanup path, and make the release wait freezable without spinning.
--- kernel/liveupdate/luo_internal.h | 12 +- kernel/liveupdate/luo_session.c | 236 +++++++++++++++++++++++++++---- 2 files changed, 221 insertions(+), 27 deletions(-)
kernel/liveupdate/luo_internal.h +9 -3
@@ -9,6 +9,7 @@#define _LINUX_LUO_INTERNAL_H#include <linux/liveupdate.h> +#include <linux/refcount.h> #include <linux/uaccess.h> struct luo_ucmd {
@@ -63,8 +64,7 @@ struct luo_file_set {* @list: A list_head member used to link this session into a global list * of either outgoing (to be preserved) or incoming (restored from * previous kernel) sessions. -* @retrieved: A boolean flag indicating whether this session has been -* retrieved by a consumer in the new kernel. +* @state: Current lifecycle phase of the session. * @file_set: A set of files that belong to this session. * @mutex: protects fields in the luo_session. */
@@ -72,8 +72,14 @@ struct luo_session {char name[LIVEUPDATE_SESSION_NAME_LENGTH ];struct luo_session_ser *ser;struct list_head list; -bool retrieved; +enum { +LUO_SESSION_OUTGOING , +LUO_SESSION_INCOMING , +LUO_SESSION_RETRIEVED , +LUO_SESSION_CLOSED , + } state;struct luo_file_set file_set; +refcount_t refs;struct mutex mutex; };
kernel/liveupdate/luo_session.c +212 -24
393 lines not on this page · raw message
Signature · 1 line
2.53.0
PATCH v3 2/5 kexec: abort liveupdate handover on kernel_kexec() unwind
liveupdate_reboot() can freeze outgoing session state before the kexec transition completes. v2 only unwound that state when kernel_kexec() returned a non-zero error, which misses the preserve_context path where machine_kexec() can return to the old kernel with error == 0.
If that return path skips the abort, outgoing sessions remain stuck in the reboot state and later user space operations cannot make progress.
Treat every return to the original kernel after a prepared liveupdate handover as an unwind point and call liveupdate_reboot_abort() there as well.
--- include/linux/liveupdate.h | 5 +++++ kernel/kexec_core.c | 6 +++++- kernel/liveupdate/luo_core.c | 11 ++++++++++- kernel/liveupdate/luo_internal.h | 1 + kernel/liveupdate/luo_session.c | 23 +++++++++++++++++++++++ 5 files changed, 44 insertions(+), 2 deletions(-)
include/linux/liveupdate.h +5 -0
@@ -226,6 +226,7 @@ bool liveupdate_enabled(void);/* Called during kexec to tell LUO that entered into reboot */ int liveupdate_reboot (void ); +void liveupdate_reboot_abort (void );int liveupdate_register_file_handler (struct liveupdate_file_handler *fh);int liveupdate_unregister_file_handler (struct liveupdate_file_handler *fh);
@@ -250,6 +251,10 @@ static inline int liveupdate_reboot(void)return 0 ; } +static inline void liveupdate_reboot_abort (void ) +{ +} +static inline int liveupdate_register_file_handler (struct liveupdate_file_handler *fh) {return -EOPNOTSUPP ;
kernel/kexec_core.c +5 -1
@@ -1139,6 +1139,7 @@ bool kexec_load_permitted(int kexec_image_type)int kernel_kexec (void ) {int error =0 ; +bool liveupdate_prepared = false;if (!kexec_trylock ())return -EBUSY ;
@@ -1147,6 +1148,7 @@ int kernel_kexec(void) goto Unlock; } + liveupdate_prepared = true; error =liveupdate_reboot ();if (error) goto Unlock;
@@ -1230,7 +1232,9 @@ int kernel_kexec(void) }#endif - Unlock: +Unlock: +if (liveupdate_prepared) +liveupdate_reboot_abort ();kexec_unlock ();return error; }
kernel/liveupdate/luo_core.c +10 -1
@@ -233,8 +233,9 @@ int liveupdate_reboot(void) err =kho_finalize ();if (err) {pr_err ("kho_finalize failed %d\n" , err); +liveupdate_reboot_abort ();/* -* kho_finalize() may return libfdt errors, to aboid passing to +* kho_finalize() may return libfdt errors, to avoid passing to * userspace unknown errors, change this to EAGAIN. */ err = -EAGAIN ;
@@ -243,6 +244,14 @@ int liveupdate_reboot(void)return err; } +void liveupdate_reboot_abort (void ) +{ +if (!liveupdate_enabled ()) +return ; + +luo_session_abort_reboot (); +} +/** * liveupdate_enabled - Check if the live update feature is enabled. *
kernel/liveupdate/luo_internal.h +1 -0
@@ -88,6 +88,7 @@ int luo_session_retrieve(const char *name, struct file **filep);int __init luo_session_setup_outgoing (void *fdt);int __init luo_session_setup_incoming (void *fdt);int luo_session_serialize (void ); +void luo_session_abort_reboot (void );int luo_session_deserialize (void );bool luo_session_quiesce (void );void luo_session_resume (void );
kernel/liveupdate/luo_session.c +23 -0
@@ -788,6 +788,29 @@ int luo_session_serialize(void)return err; } +void luo_session_abort_reboot (void ) +{ +struct luo_session_header *sh = &luo_session_global.outgoing; +struct luo_session *session; +int i =0 ; + +guard (rwsem_write)(&sh->rwsem); +if (!READ_ONCE (sh->rebooting)) +return ; + +list_for_each_entry (session, &sh->list, list) { +if (i >= sh->header_ser->count) +break ; + +luo_session_unfreeze_one (session, &sh->ser[i]); +memset (&sh->ser[i],0 ,sizeof (sh->ser[i])); + i++; + } + + sh->header_ser->count =0 ; +luo_session_reboot_done (sh); +} +/** * luo_session_quiesce - Ensure no active sessions exist and lock session lists. *
Signature · 1 line
2.53.0
PATCH v3 3/5 liveupdate: fail session restore on file deserialization errors
Session restore can fail after inserting a partially constructed session into the incoming list. The v2 cleanup also reused finish-style teardown for that rollback path even though finish is the normal completion path for a fully restored session.
That can leave stale incoming sessions behind after a deserialize error and can run completion callbacks against state that was never fully rebuilt.
Require an explicit abort callback for file handlers, use it to unwind partially restored files, and route every deserialize failure through a single cleanup path that drops restored sessions, frees serialized file metadata and records the cached error code.
--- include/linux/liveupdate.h | 3 ++ kernel/liveupdate/luo_file.c | 67 +++++++++++++++++++++++--------- kernel/liveupdate/luo_internal.h | 1 + kernel/liveupdate/luo_session.c | 42 +++++++++----------- mm/memfd_luo.c | 24 +++++++----- 5 files changed, 86 insertions(+), 51 deletions(-)
include/linux/liveupdate.h +3 -0
@@ -63,6 +63,8 @@ struct liveupdate_file_op_args {* finish, in order to do successful finish calls for all * resources in the session. * @finish: Required. Final cleanup in the new kernel. +* @abort: Required. Discard preserved state in the new kernel without +* completing finish(). * @owner: Module reference * * All operations (except can_preserve) receive a pointer to a
@@ -78,6 +80,7 @@ struct liveupdate_file_ops {int (*retrieve)(struct liveupdate_file_op_args *args);bool (*can_finish)(struct liveupdate_file_op_args *args);void (*finish)(struct liveupdate_file_op_args *args); +void (*abort)(struct liveupdate_file_op_args *args);struct module *owner; };
kernel/liveupdate/luo_file.c +48 -19
112 lines not on this page · raw message
kernel/liveupdate/luo_internal.h +1 -0
@@ -102,6 +102,7 @@ void luo_file_unfreeze(struct luo_file_set *file_set,int luo_retrieve_file (struct luo_file_set *file_set,u64 token,struct file **filep);int luo_file_finish (struct luo_file_set *file_set); +void luo_file_abort_deserialized (struct luo_file_set *file_set);int luo_file_deserialize (struct luo_file_set *file_set,struct luo_file_set_ser *file_set_ser);void luo_file_set_init (struct luo_file_set *file_set);
kernel/liveupdate/luo_session.c +19 -23
71 lines not on this page · raw message
mm/memfd_luo.c +15 -9
@@ -358,19 +358,11 @@ 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) +static void memfd_luo_abort (struct liveupdate_file_op_args *args) {struct memfd_luo_folio_ser *folios_ser;struct memfd_luo_ser *ser; -/* -* 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_status) -return ; - ser =phys_to_virt (args->serialized_data);if (!ser)return ;
@@ -388,6 +380,19 @@ static void memfd_luo_finish(struct liveupdate_file_op_args *args)kho_restore_free (ser); } +static void memfd_luo_finish (struct liveupdate_file_op_args *args) +{ +/* +* 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_status) +return ; + +memfd_luo_abort (args); +} +static int memfd_luo_retrieve_folios (struct file *file,struct memfd_luo_folio_ser *folios_ser,u64 nr_folios)
@@ -532,6 +537,7 @@ static bool memfd_luo_can_preserve(struct liveupdate_file_handler *handler,static const struct liveupdate_file_ops memfd_luo_file_ops = { .freeze = memfd_luo_freeze, .finish = memfd_luo_finish, + .abort = memfd_luo_abort, .retrieve = memfd_luo_retrieve, .preserve = memfd_luo_preserve, .unpreserve = memfd_luo_unpreserve,
Signature · 1 line
2.53.0
PATCH v3 4/5 liveupdate: validate handover metadata before using it
The incoming handover path was still trusting counts and preserved addresses more than it should. Corrupted metadata could drive phys_to_virt() or kho_restore_free() with invalid addresses, and memfd restore could walk beyond the serialized folio array.
That turns malformed handover state into out-of-bounds iteration, invalid restore frees, or partially restored incoming state.
Validate session, file and FLB metadata before consuming it, reject non-restorable preserved addresses, and bound memfd folio restore by both the serialized size and the restored vmalloc backing.
--- include/linux/kexec_handover.h | 5 +++ kernel/liveupdate/kexec_handover.c | 13 +++++++ kernel/liveupdate/luo_file.c | 22 +++++++++--- kernel/liveupdate/luo_flb.c | 33 ++++++++++++++++-- kernel/liveupdate/luo_session.c | 29 ++++++++++++++-- mm/memfd_luo.c | 56 ++++++++++++++++++++++++++++-- 6 files changed, 147 insertions(+), 11 deletions(-)
include/linux/kexec_handover.h +5 -0
@@ -29,6 +29,7 @@ void kho_unpreserve_vmalloc(struct kho_vmalloc *preservation);void *kho_alloc_preserve (size_t size);void kho_unpreserve_free (void *mem);void kho_restore_free (void *mem); +bool kho_is_restorable_phys (phys_addr_t phys);struct folio *kho_restore_folio (phys_addr_t phys);struct page *kho_restore_pages (phys_addr_t phys,unsigned long nr_pages);void *kho_restore_vmalloc (const struct kho_vmalloc *preservation);
@@ -80,6 +81,10 @@ static inline void *kho_alloc_preserve(size_t size)static inline void kho_unpreserve_free (void *mem) { }static inline void kho_restore_free (void *mem) { } +static inline bool kho_is_restorable_phys (phys_addr_t phys) +{ +return false; +}static inline struct folio *kho_restore_folio (phys_addr_t phys) {
kernel/liveupdate/kexec_handover.c +13 -0
@@ -291,6 +291,19 @@ struct folio *kho_restore_folio(phys_addr_t phys) }EXPORT_SYMBOL_GPL (kho_restore_folio); +bool kho_is_restorable_phys (phys_addr_t phys) +{ +struct page *page =pfn_to_online_page (PHYS_PFN (phys)); +union kho_page_info info; + +if (!page || !PAGE_ALIGNED (phys)) +return false; + + info.page_private =READ_ONCE (page->private); +return info.magic ==KHO_PAGE_MAGIC && info.order <=MAX_PAGE_ORDER ; +} +EXPORT_SYMBOL_GPL (kho_is_restorable_phys); +/** * kho_restore_pages - restore list of contiguous order 0 pages. * @phys: physical address of the first page.
kernel/liveupdate/luo_file.c +18 -4
@@ -785,10 +785,17 @@ int luo_file_deserialize(struct luo_file_set *file_set,u64 i;int err; -if (!file_set_ser->files) { -WARN_ON (file_set_ser->count); -return 0 ; - } +if (!file_set_ser->count) +return file_set_ser->files ? -EINVAL :0 ; + +if (!file_set_ser->files) +return -EINVAL ; + +if (file_set_ser->count >LUO_FILE_MAX ) +return -EINVAL ; + +if (!kho_is_restorable_phys (file_set_ser->files)) +return -EINVAL ; file_set->count = file_set_ser->count; file_set->files =phys_to_virt (file_set_ser->files);
@@ -799,6 +806,13 @@ int luo_file_deserialize(struct luo_file_set *file_set,bool handler_found = false;struct luo_file *luo_file; +if (strnlen (file_ser[i].compatible, +sizeof (file_ser[i].compatible)) == +sizeof (file_ser[i].compatible)) { + err = -EINVAL ; + goto err_discard; + } +list_private_for_each_entry (fh, &luo_file_handler_list, list) {if (!strcmp (fh->compatible, file_ser[i].compatible)) { handler_found = true;
kernel/liveupdate/luo_flb.c +31 -2
@@ -165,7 +165,14 @@ static int luo_flb_retrieve_one(struct liveupdate_flb *flb)return -ENODATA ;for (int i =0 ; i < fh->header_ser->count; i++) { +if (strnlen (fh->ser[i].name,sizeof (fh->ser[i].name)) == +sizeof (fh->ser[i].name)) +return -EINVAL ; +if (!strcmp (fh->ser[i].name, flb->compatible)) { +if (!fh->ser[i].count) +return -EINVAL ; + private->incoming.data = fh->ser[i].data; private->incoming.count = fh->ser[i].count; found = true;
@@ -578,6 +585,7 @@ int __init luo_flb_setup_outgoing(void *fdt_out)int __init luo_flb_setup_incoming (void *fdt_in) { +struct luo_flb_header_ser *header_copy;struct luo_flb_header_ser *header_ser;int err, header_size, offset;const void *ptr;
@@ -609,10 +617,31 @@ int __init luo_flb_setup_incoming(void *fdt_in) } header_ser_pa =get_unaligned ((u64 *)ptr); +if (!header_ser_pa) { +pr_err ("FLB header address is missing\n" ); +return -EINVAL ; + } + +if (!kho_is_restorable_phys (header_ser_pa)) +return -EINVAL ; + header_ser =phys_to_virt (header_ser_pa); +if (header_ser->pgcnt !=LUO_FLB_PGCNT || header_ser->count >LUO_FLB_MAX ) { +kho_restore_free (header_ser); +return -EINVAL ; + } + + header_copy =kmemdup (header_ser, +sizeof (*header_copy) + + header_ser->count * +sizeof (*luo_flb_global.incoming.ser), +GFP_KERNEL ); +kho_restore_free (header_ser); +if (!header_copy) +return -ENOMEM ; - luo_flb_global.incoming.header_ser = header_ser; - luo_flb_global.incoming.ser = (void *)(header_ser +1 ); + luo_flb_global.incoming.header_ser = header_copy; + luo_flb_global.incoming.ser = (void *)(header_copy +1 ); luo_flb_global.incoming.active = true;return 0 ;
kernel/liveupdate/luo_session.c +26 -3
@@ -673,6 +673,14 @@ int __init luo_session_setup_incoming(void *fdt_in) } header_ser_pa =get_unaligned ((u64 *)ptr); +if (!header_ser_pa) { +pr_err ("Session header address is missing\n" ); +return -EINVAL ; + } + +if (!kho_is_restorable_phys (header_ser_pa)) +return -EINVAL ; + header_ser =phys_to_virt (header_ser_pa); luo_session_global.incoming.header_ser = header_ser;
@@ -697,9 +705,22 @@ int luo_session_deserialize(void)if (!sh->active)return err; +if (sh->header_ser->count >LUO_SESSION_MAX ) { +pr_warn ("Invalid session count %llu\n" , sh->header_ser->count); + err = -EINVAL ; + goto out_free_header; + } +for (int i =0 ; i < sh->header_ser->count; i++) {struct luo_session *session; +if (strnlen (sh->ser[i].name,sizeof (sh->ser[i].name)) == +sizeof (sh->ser[i].name)) { +pr_warn ("Session name is not NUL-terminated\n" ); + err = -EINVAL ; + goto out_discard; + } + session =luo_session_alloc (sh->ser[i].name);if (IS_ERR (session)) { pr_warn("Failed to allocate session [%s] during deserialization %pe\n",
@@ -728,9 +749,11 @@ int luo_session_deserialize(void) } out_free_header: -kho_restore_free (sh->header_ser); - sh->header_ser =NULL ; - sh->ser =NULL ; +if (sh->header_ser) { +kho_restore_free (sh->header_ser); + sh->header_ser =NULL ; + sh->ser =NULL ; + }return err;
mm/memfd_luo.c +54 -2
103 lines not on this page · raw message
Signature · 1 line
2.53.0
PATCH v3 5/5 liveupdate: harden FLB lifetime and teardown paths
The remaining FLB teardown paths still had a few lifetime holes. Incoming counters could underflow, the returned incoming object was not pinned against concurrent finish, and copied incoming FLB metadata could be freed while another thread was still scanning it.
That makes incoming FLB teardown racy and can leave callers with stale objects or freed metadata.
Guard the FLB counters, make liveupdate_flb_get_incoming() hold the incoming FLB lock until the caller releases it, and serialize access to the copied incoming FLB metadata while it is being retrieved or discarded.
--- include/linux/liveupdate.h | 5 ++ kernel/liveupdate/luo_flb.c | 78 ++++++++++++++++++++++++-------- kernel/liveupdate/luo_internal.h | 1 + lib/tests/liveupdate.c | 2 + 4 files changed, 66 insertions(+), 20 deletions(-)
include/linux/liveupdate.h +5 -0
@@ -240,6 +240,7 @@ int liveupdate_unregister_flb(struct liveupdate_file_handler *fh, struct liveupdate_flb *flb);int liveupdate_flb_get_incoming (struct liveupdate_flb *flb,void **objp); +void liveupdate_flb_put_incoming (struct liveupdate_flb *flb);int liveupdate_flb_get_outgoing (struct liveupdate_flb *flb,void **objp);#else /* CONFIG_LIVEUPDATE */
@@ -286,6 +287,10 @@ static inline int liveupdate_flb_get_incoming(struct liveupdate_flb *flb,return -EOPNOTSUPP ; } +static inline void liveupdate_flb_put_incoming (struct liveupdate_flb *flb) +{ +} +static inline int liveupdate_flb_get_outgoing (struct liveupdate_flb *flb,void **objp) {
kernel/liveupdate/luo_flb.c +58 -20
139 lines not on this page · raw message
kernel/liveupdate/luo_internal.h +1 -0
@@ -113,6 +113,7 @@ void luo_flb_file_unpreserve(struct liveupdate_file_handler *fh);void luo_flb_file_finish (struct liveupdate_file_handler *fh);int __init luo_flb_setup_outgoing (void *fdt);int __init luo_flb_setup_incoming (void *fdt); +void luo_flb_discard_incoming (void );void luo_flb_serialize (void );#ifdef CONFIG_LIVEUPDATE_TEST
lib/tests/liveupdate.c +2 -0
@@ -105,6 +105,8 @@ static void liveupdate_test_init(void)pr_err ("liveupdate_flb_get_incoming for %s failed: %pe\n" , flb->compatible,ERR_PTR (err)); } +if (!err) +liveupdate_flb_put_incoming (flb); } initialized = true; }
Signature · 1 line
2.53.0
Pasha Tatashin
maintainerRe: [PATCH v3 2/5] kexec: abort liveupdate handover on kernel_kexec() unwind
In reply to Oskar Gerlicz KowalczukQuoted 9 lines · Oskar Gerlicz Kowalczuk
liveupdate_reboot()can freeze outgoing session state before the kexec transition completes. v2 only unwound that state whenkernel_kexec()returned a non-zero error, which misses thepreserve_contextpath wheremachine_kexec()can return to the old kernel with error == 0.If that return path skips the abort, outgoing sessions remain stuck in the reboot state and later user space operations cannot make progress.
Quoted 6 lines
Treat every return to the original kernel after a prepared liveupdate handover as an unwind point and call
liveupdate_reboot_abort()there as well.Fixes 8b32d3ced269 ("kexec: abort liveupdate handover on kernel_kexec() unwind")Signed-off-by Oskar Gerlicz Kowalczuk <[email protected]>
AFAIK, preserve-context reboot is no longer used; in fact, it is currently broken. I tried using it six years ago and hit an old spinlock bug in the x86 code that prevented it, which means this path touches dead code.
It was originally developed for machines lacking hardware support for hibernation, and I can't imagine anyone using it today. (I know Amazon is using it for a non intended reason and that is a very specific scenario in a costumized kernel that internally fixes the upstream bugs, and would not conlict with live update).
During live update, we cannot return to userspace after a successful liveupdate_reboot(). We specifically place liveupdate_reboot() in the kexec path as the last possible point of failure. Perhap, to guarantee this behavior, we could add !KEXEC_JUMP as a dependency to KHO and LUO or crash the kernel.
Pasha
Quoted 142 lines
--- include/linux/liveupdate.h | 5 +++++ kernel/kexec_core.c | 6 +++++- kernel/liveupdate/luo_core.c | 11 ++++++++++- kernel/liveupdate/luo_internal.h | 1 + kernel/liveupdate/luo_session.c | 23 +++++++++++++++++++++++ 5 files changed, 44 insertions(+), 2 deletions(-)include/linux/liveupdate.h +5
-0@@ -226,6 +226,7 @@ bool liveupdate_enabled(void);/* Called during kexec to tell LUO that entered into reboot */ int liveupdate_reboot (void ); +void liveupdate_reboot_abort (void );int liveupdate_register_file_handler (struct liveupdate_file_handler *fh);int liveupdate_unregister_file_handler (struct liveupdate_file_handler *fh);@@ -250,6 +251,10 @@ static inline int liveupdate_reboot(void)return 0 ; } +static inline void liveupdate_reboot_abort (void ) +{ +} +static inline int liveupdate_register_file_handler (struct liveupdate_file_handler *fh) {return -EOPNOTSUPP ;kernel/kexec_core.c +5
-1@@ -1139,6 +1139,7 @@ bool kexec_load_permitted(int kexec_image_type)int kernel_kexec (void ) {int error =0 ; +bool liveupdate_prepared = false;if (!kexec_trylock ())return -EBUSY ;@@ -1147,6 +1148,7 @@ int kernel_kexec(void) goto Unlock; } + liveupdate_prepared = true; error =liveupdate_reboot ();if (error) goto Unlock;@@ -1230,7 +1232,9 @@ int kernel_kexec(void) }#endif - Unlock: +Unlock: +if (liveupdate_prepared) +liveupdate_reboot_abort ();kexec_unlock ();return error; }kernel/liveupdate/luo_core.c +10
-1@@ -233,8 +233,9 @@ int liveupdate_reboot(void) err =kho_finalize ();if (err) {pr_err ("kho_finalize failed %d\n" , err); +liveupdate_reboot_abort ();/* -* kho_finalize() may return libfdt errors, to aboid passing to +* kho_finalize() may return libfdt errors, to avoid passing to * userspace unknown errors, change this to EAGAIN. */ err = -EAGAIN ;@@ -243,6 +244,14 @@ int liveupdate_reboot(void)return err; } +void liveupdate_reboot_abort (void ) +{ +if (!liveupdate_enabled ()) +return ; + +luo_session_abort_reboot (); +} +/** * liveupdate_enabled - Check if the live update feature is enabled. * kernel/liveupdate/luo_internal.h +1
-0@@ -88,6 +88,7 @@ int luo_session_retrieve(const char *name, struct file **filep);int __init luo_session_setup_outgoing (void *fdt);int __init luo_session_setup_incoming (void *fdt);int luo_session_serialize (void ); +void luo_session_abort_reboot (void );int luo_session_deserialize (void );bool luo_session_quiesce (void );void luo_session_resume (void );kernel/liveupdate/luo_session.c +23
-1@@ -788,6 +788,29 @@ int luo_session_serialize(void)return err; } +void luo_session_abort_reboot (void ) +{ +struct luo_session_header *sh = &luo_session_global.outgoing; +struct luo_session *session; +int i =0 ; + +guard (rwsem_write)(&sh->rwsem); +if (!READ_ONCE (sh->rebooting)) +return ; + +list_for_each_entry (session, &sh->list, list) { +if (i >= sh->header_ser->count) +break ; + +luo_session_unfreeze_one (session, &sh->ser[i]); +memset (&sh->ser[i],0 ,sizeof (sh->ser[i])); + i++; + } + + sh->header_ser->count =0 ; +luo_session_reboot_done (sh); +} +/** * luo_session_quiesce - Ensure no active sessions exist and lock session lists. * -- 2.53.0
Pasha Tatashin
maintainerRe: [PATCH v3 2/5] kexec: abort liveupdate handover on kernel_kexec() unwind
Quoted 30 lines · Pasha Tatashin
Quoted 9 lines · Oskar Gerlicz Kowalczuk
liveupdate_reboot()can freeze outgoing session state before the kexec transition completes. v2 only unwound that state whenkernel_kexec()returned a non-zero error, which misses thepreserve_contextpath wheremachine_kexec()can return to the old kernel with error == 0.If that return path skips the abort, outgoing sessions remain stuck in the reboot state and later user space operations cannot make progress.
Quoted 6 lines
Treat every return to the original kernel after a prepared liveupdate handover as an unwind point and call
liveupdate_reboot_abort()there as well.Fixes 8b32d3ced269 ("kexec: abort liveupdate handover on kernel_kexec() unwind")Signed-off-by Oskar Gerlicz Kowalczuk <[email protected]>AFAIK, preserve-context reboot is no longer used; in fact, it is currently broken. I tried using it six years ago and hit an old spinlock bug in the x86 code that prevented it, which means this path touches dead code.
It was originally developed for machines lacking hardware support for hibernation, and I can't imagine anyone using it today. (I know Amazon is using it for a non intended reason and that is a very specific scenario in a costumized kernel that internally fixes the upstream bugs, and would not conlict with live update).
Quoted 5 lines
During live update, we cannot return to userspace after a successful
liveupdate_reboot(). We specifically placeliveupdate_reboot()in the kexec path as the last possible point of failure. Perhap, to guarantee this behavior, we could add !KEXEC_JUMP as a dependency to KHO and LUO or crash the kernel.
So, I think the right approach to address this issue is simply skip liveupdate_reboot() call when we are doing preserve context kexec. With preserve context we are going to get back to the current kernel, there is absolutly no need to do serialization. So something like this should work:
https://git.kernel.org/pub/scm/linux/kernel/git/tatashin/linux.git/commit/?h=luo-reboot-sync/rfc/1&id=b13cf19ebbf2f8483a3c5a61a7358611b391edf6
Quoted 145 lines
Pasha
Quoted 142 lines
--- include/linux/liveupdate.h | 5 +++++ kernel/kexec_core.c | 6 +++++- kernel/liveupdate/luo_core.c | 11 ++++++++++- kernel/liveupdate/luo_internal.h | 1 + kernel/liveupdate/luo_session.c | 23 +++++++++++++++++++++++ 5 files changed, 44 insertions(+), 2 deletions(-)include/linux/liveupdate.h +5
-0@@ -226,6 +226,7 @@ bool liveupdate_enabled(void);/* Called during kexec to tell LUO that entered into reboot */ int liveupdate_reboot (void ); +void liveupdate_reboot_abort (void );int liveupdate_register_file_handler (struct liveupdate_file_handler *fh);int liveupdate_unregister_file_handler (struct liveupdate_file_handler *fh);@@ -250,6 +251,10 @@ static inline int liveupdate_reboot(void)return 0 ; } +static inline void liveupdate_reboot_abort (void ) +{ +} +static inline int liveupdate_register_file_handler (struct liveupdate_file_handler *fh) {return -EOPNOTSUPP ;kernel/kexec_core.c +5
-1@@ -1139,6 +1139,7 @@ bool kexec_load_permitted(int kexec_image_type)int kernel_kexec (void ) {int error =0 ; +bool liveupdate_prepared = false;if (!kexec_trylock ())return -EBUSY ;@@ -1147,6 +1148,7 @@ int kernel_kexec(void) goto Unlock; } + liveupdate_prepared = true; error =liveupdate_reboot ();if (error) goto Unlock;@@ -1230,7 +1232,9 @@ int kernel_kexec(void) }#endif - Unlock: +Unlock: +if (liveupdate_prepared) +liveupdate_reboot_abort ();kexec_unlock ();return error; }kernel/liveupdate/luo_core.c +10
-1@@ -233,8 +233,9 @@ int liveupdate_reboot(void) err =kho_finalize ();if (err) {pr_err ("kho_finalize failed %d\n" , err); +liveupdate_reboot_abort ();/* -* kho_finalize() may return libfdt errors, to aboid passing to +* kho_finalize() may return libfdt errors, to avoid passing to * userspace unknown errors, change this to EAGAIN. */ err = -EAGAIN ;@@ -243,6 +244,14 @@ int liveupdate_reboot(void)return err; } +void liveupdate_reboot_abort (void ) +{ +if (!liveupdate_enabled ()) +return ; + +luo_session_abort_reboot (); +} +/** * liveupdate_enabled - Check if the live update feature is enabled. * kernel/liveupdate/luo_internal.h +1
-0@@ -88,6 +88,7 @@ int luo_session_retrieve(const char *name, struct file **filep);int __init luo_session_setup_outgoing (void *fdt);int __init luo_session_setup_incoming (void *fdt);int luo_session_serialize (void ); +void luo_session_abort_reboot (void );int luo_session_deserialize (void );bool luo_session_quiesce (void );void luo_session_resume (void );kernel/liveupdate/luo_session.c +23
-1@@ -788,6 +788,29 @@ int luo_session_serialize(void)return err; } +void luo_session_abort_reboot (void ) +{ +struct luo_session_header *sh = &luo_session_global.outgoing; +struct luo_session *session; +int i =0 ; + +guard (rwsem_write)(&sh->rwsem); +if (!READ_ONCE (sh->rebooting)) +return ; + +list_for_each_entry (session, &sh->list, list) { +if (i >= sh->header_ser->count) +break ; + +luo_session_unfreeze_one (session, &sh->ser[i]); +memset (&sh->ser[i],0 ,sizeof (sh->ser[i])); + i++; + } + + sh->header_ser->count =0 ; +luo_session_reboot_done (sh); +} +/** * luo_session_quiesce - Ensure no active sessions exist and lock session lists. * -- 2.53.0
Andrew Morton
maintainerQuoted 10 lines · On Sat, 21 Mar 2026 15:36:38 +0100 Oskar Gerlicz Kowalczuk…
kernel_kexec()serializes outgoing sessions before the reboot path freezes tasks, soclose()and session ioctls can still mutate a session while handover state is being prepared. The original v2 code also let incoming lookups keep a bare session pointer after dropping the list lock.That leaves two correctness problems in the reboot path: outgoing state can change after serialization starts, and incoming sessions can be freed while another thread still holds a pointer to them.
Quoted 5 lines
Add refcounted session lifetime management, track in-flight outgoing
close()paths with an atomic closing counter, and make serialization wait for closing to drain before setting rebooting. Reject phase-invalid ioctls, keep incoming release on a common cleanup path, and make the release wait freezable without spinning.
A [0/N] cover letter would be appropriate.
Please have a think about which patches should be backported into -stable kernels. Possibly "none", given how new LUO is.
If there are any backportable patches, please ensure that they are at the start-of-series, or separate them out into a separate series, against current mainline.
AI review hasn't completed yet, but it has things to say:
https://sashiko.dev/#/patchset/[email protected]
Pasha Tatashin
maintainerQuoted 17 lines · Oskar Gerlicz Kowalczuk
kernel_kexec()serializes outgoing sessions before the reboot path freezes tasks, soclose()and session ioctls can still mutate a session while handover state is being prepared. The original v2 code also let incoming lookups keep a bare session pointer after dropping the list lock.That leaves two correctness problems in the reboot path: outgoing state can change after serialization starts, and incoming sessions can be freed while another thread still holds a pointer to them.
Add refcounted session lifetime management, track in-flight outgoing
close()paths with an atomic closing counter, and make serialization wait for closing to drain before setting rebooting. Reject phase-invalid ioctls, keep incoming release on a common cleanup path, and make the release wait freezable without spinning.
Quoted 6 lines
Fixes fc5acd5c89fe ("liveupdate: block outgoing session updates during reboot")Signed-off-by Oskar Gerlicz Kowalczuk <[email protected]>--- kernel/liveupdate/luo_internal.h | 12 +- kernel/liveupdate/luo_session.c | 236 +++++++++++++++++++++++++++---- 2 files changed, 221 insertions(+), 27 deletions(-)
Hi Oskar,
Thank you for sending this series and finding these bugs in LUO. I agree with Andrew that a cover letter would help to understand the summary of the overall effort.
I have not reviewed the other patches yet, but for this patch, my understanding is that it solves two specific races during reboot()
syscalls: session closure after serialization, and the addition of new sessions or preserving new files after serialization.
Given that KHO is now stateless, and liveupdate_reboot() is specifically placed at the last point where we can still return an error to userspace, we should simply return an error if a userspace is doing something unexpected.
Instead of creating a new state machine, let's just reuse the file references and simply take them for each session at the beginning of serialization. This ensures that no session closes will happen later. For file preservation and session addition, we can block them by simply adding a new boolean.
Please take a look at the two patches below and see if this approach would work. It is a much smaller change compared to the proposed state machine in this patch.
https://git.kernel.org/pub/scm/linux/kernel/git/tatashin/linux.git/log/?h=luo-reboot-sync/rfc/1
Thanks,
Pasha
Quoted 460 lines
kernel/liveupdate/luo_internal.h +9
-3@@ -9,6 +9,7 @@#define _LINUX_LUO_INTERNAL_H#include <linux/liveupdate.h> +#include <linux/refcount.h> #include <linux/uaccess.h> struct luo_ucmd {@@ -63,8 +64,7 @@ struct luo_file_set {* @list: A list_head member used to link this session into a global list * of either outgoing (to be preserved) or incoming (restored from * previous kernel) sessions. -* @retrieved: A boolean flag indicating whether this session has been -* retrieved by a consumer in the new kernel. +* @state: Current lifecycle phase of the session. * @file_set: A set of files that belong to this session. * @mutex: protects fields in the luo_session. */ @@ -72,8 +72,14 @@ struct luo_session {char name[LIVEUPDATE_SESSION_NAME_LENGTH ];struct luo_session_ser *ser;struct list_head list; -bool retrieved; +enum { +LUO_SESSION_OUTGOING , +LUO_SESSION_INCOMING , +LUO_SESSION_RETRIEVED , +LUO_SESSION_CLOSED , + } state;struct luo_file_set file_set; +refcount_t refs;struct mutex mutex; };kernel/liveupdate/luo_session.c +212
-25@@ -51,6 +51,7 @@#define pr_fmt (fmt) KBUILD_MODNAME ": " fmt#include <linux/anon_inodes.h> +#include <linux/atomic.h> #include <linux/cleanup.h> #include <linux/err.h> #include <linux/errno.h> @@ -66,6 +67,7 @@#include <linux/rwsem.h> #include <linux/slab.h> #include <linux/unaligned.h> +#include <linux/wait.h> #include <uapi/linux/liveupdate.h> #include "luo_internal.h" @@ -89,10 +91,13 @@*/ struct luo_session_header {long count; +atomic_t closing;struct list_head list;struct rw_semaphore rwsem; +wait_queue_head_t reboot_waitq;struct luo_session_header_ser *header_ser;struct luo_session_ser *ser; +bool rebooting;bool active; };@@ -110,13 +115,24 @@ static struct luo_session_global luo_session_global = { .incoming = { .list = LIST_HEAD_INIT(luo_session_global.incoming.list), .rwsem = __RWSEM_INITIALIZER(luo_session_global.incoming.rwsem), + .reboot_waitq = + __WAIT_QUEUE_HEAD_INITIALIZER(luo_session_global.incoming.reboot_waitq), }, .outgoing = { + .closing = ATOMIC_INIT(0), .list = LIST_HEAD_INIT(luo_session_global.outgoing.list), .rwsem = __RWSEM_INITIALIZER(luo_session_global.outgoing.rwsem), + .reboot_waitq = + __WAIT_QUEUE_HEAD_INITIALIZER(luo_session_global.outgoing.reboot_waitq), }, }; +static void luo_session_reboot_done (struct luo_session_header *sh) +{ +WRITE_ONCE (sh->rebooting, false); +wake_up_all (&sh->reboot_waitq); +} +static struct luo_session *luo_session_alloc (const char *name) {struct luo_session *session =kzalloc_obj (*session);@@ -128,6 +144,8 @@ static struct luo_session *luo_session_alloc(const char *name)INIT_LIST_HEAD (&session->file_set.files_list);luo_file_set_init (&session->file_set);INIT_LIST_HEAD (&session->list); + session->state =LUO_SESSION_OUTGOING ; +refcount_set (&session->refs,1 );mutex_init (&session->mutex);return session;@@ -140,6 +158,17 @@ static void luo_session_free(struct luo_session *session)kfree (session); } +static void luo_session_get (struct luo_session *session) +{ +refcount_inc (&session->refs); +} + +static void luo_session_put (struct luo_session *session) +{ +if (refcount_dec_and_test (&session->refs)) +luo_session_free (session); +} +static int luo_session_insert (struct luo_session_header *sh,struct luo_session *session) {@@ -152,6 +181,9 @@ static int luo_session_insert(struct luo_session_header *sh,* for new session. */ if (sh == &luo_session_global.outgoing) { +if (READ_ONCE (sh->rebooting)) +return -EBUSY ; +if (sh->count ==LUO_SESSION_MAX )return -ENOMEM ; }@@ -172,17 +204,98 @@ static int luo_session_insert(struct luo_session_header *sh,return 0 ; } +static void __luo_session_remove (struct luo_session_header *sh, +struct luo_session *session) +{ +list_del (&session->list); + sh->count--; +} +static void luo_session_remove (struct luo_session_header *sh,struct luo_session *session) {guard (rwsem_write)(&sh->rwsem); -list_del (&session->list); - sh->count--; +__luo_session_remove (sh, session); +} + +static int luo_session_outgoing_begin (struct luo_session *session, +struct luo_session_header **shp) +{ +struct luo_session_header *sh; + +if (READ_ONCE (session->state) !=LUO_SESSION_OUTGOING ) +return -EINVAL ; + + sh = &luo_session_global.outgoing; +down_read (&sh->rwsem); +if (READ_ONCE (sh->rebooting)) { +up_read (&sh->rwsem); +return -EBUSY ; + } + + *shp = sh; +return 0 ; +} + +static void luo_session_outgoing_end (struct luo_session_header *sh) +{ +if (sh) +up_read (&sh->rwsem); +} + +static void luo_session_wait_reboot (struct luo_session_header *sh) +{ +DEFINE_WAIT (wait); + +for (;;) { +prepare_to_wait (&sh->reboot_waitq, &wait, +TASK_UNINTERRUPTIBLE |TASK_FREEZABLE ); +if (!READ_ONCE (sh->rebooting)) +break ; +schedule (); + } + +finish_wait (&sh->reboot_waitq, &wait); +} + +static int luo_session_finish_retrieved (struct luo_session *session) +{ +int err; + +guard (mutex)(&session->mutex); +if (session->state !=LUO_SESSION_RETRIEVED ) +return -EINVAL ; + + err =luo_file_finish (&session->file_set); +if (err) + session->state =LUO_SESSION_INCOMING ; + +return err; +} + +static void luo_session_discard_deserialized (struct luo_session_header *sh) +{ +struct luo_session *session; + +down_write (&sh->rwsem); +while (!list_empty (&sh->list)) { + session = list_last_entry(&sh->list, struct luo_session, list); +__luo_session_remove (sh, session); + session->state =LUO_SESSION_CLOSED ; +luo_file_abort_deserialized (&session->file_set); +luo_session_put (session); + } +up_write (&sh->rwsem); + +luo_flb_discard_incoming (); }static int luo_session_finish_one (struct luo_session *session) {guard (mutex)(&session->mutex); +if (session->state !=LUO_SESSION_RETRIEVED ) +return -EINVAL ; +return luo_file_finish (&session->file_set); }@@ -204,26 +317,67 @@ static int luo_session_release(struct inode *inodep, struct file *filep) {struct luo_session *session = filep->private_data;struct luo_session_header *sh; +int state =READ_ONCE (session->state); +int ret =0 ; +bool discard_flb = false; -/* If retrieved is set, it means this session is from incoming list */ -if (session->retrieved) { -int err =luo_session_finish_one (session); +if (state ==LUO_SESSION_RETRIEVED ) { + ret =luo_session_finish_retrieved (session); -if (err) { +if (ret) {pr_warn ("Unable to finish session [%s] on release\n" , session->name); -return err; +luo_session_put (session); +return ret; } - sh = &luo_session_global.incoming; - }else {scoped_guard (mutex, &session->mutex) -luo_file_unpreserve_files (&session->file_set); - sh = &luo_session_global.outgoing; + session->state = LUO_SESSION_CLOSED; + sh = &luo_session_global.incoming; +down_write (&sh->rwsem); +__luo_session_remove (sh, session); + discard_flb = !sh->count; +up_write (&sh->rwsem); +luo_session_put (session); +luo_session_put (session); +if (discard_flb) +luo_flb_discard_incoming (); +return 0 ; } -luo_session_remove (sh, session); -luo_session_free (session); +if (state !=LUO_SESSION_OUTGOING ) { +WARN_ON_ONCE (1 ); +luo_session_put (session); +return 0 ; + } + sh = &luo_session_global.outgoing; + +for (;;) { +down_write (&sh->rwsem); +if (READ_ONCE (sh->rebooting)) { +up_write (&sh->rwsem); +luo_session_wait_reboot (sh); +continue ; + } + +atomic_inc (&sh->closing); +__luo_session_remove (sh, session); +up_write (&sh->rwsem); +break ; + } + + scoped_guard(mutex, &session->mutex) { + session->state =LUO_SESSION_CLOSED ; +luo_file_unpreserve_files (&session->file_set); + } + +down_write (&sh->rwsem); +if (atomic_dec_and_test (&sh->closing)) +wake_up_all (&sh->reboot_waitq); +up_write (&sh->rwsem); + +luo_session_put (session); +luo_session_put (session);return 0 ; }@@ -231,10 +385,16 @@ static int luo_session_preserve_fd(struct luo_session *session, struct luo_ucmd *ucmd) {struct liveupdate_session_preserve_fd *argp = ucmd->cmd; +struct luo_session_header *sh =NULL ;int err; -guard (mutex)(&session->mutex); - err =luo_preserve_file (&session->file_set, argp->token, argp->fd); + err =luo_session_outgoing_begin (session, &sh); +if (err) +return err; + +scoped_guard (mutex, &session->mutex) + err = luo_preserve_file(&session->file_set, argp->token, argp->fd); +luo_session_outgoing_end (sh);if (err)return err;@@ -252,6 +412,11 @@ static int luo_session_retrieve_fd(struct luo_session *session,struct file *file;int err; + scoped_guard(mutex, &session->mutex) { +if (session->state !=LUO_SESSION_RETRIEVED ) +return -EINVAL ; + } + argp->fd =get_unused_fd_flags (O_CLOEXEC );if (argp->fd <0 )return argp->fd;@@ -281,8 +446,9 @@ static int luo_session_finish(struct luo_session *session, struct luo_ucmd *ucmd) {struct liveupdate_session_finish *argp = ucmd->cmd; -int err =luo_session_finish_one (session); +int err; + err =luo_session_finish_one (session);if (err)return err;@@ -371,9 +537,12 @@ static int luo_session_getfile(struct luo_session *session, struct file **filep)lockdep_assert_held (&session->mutex);snprintf (name_buf,sizeof (name_buf),"[luo_session] %s" , session->name); +luo_session_get (session); file =anon_inode_getfile (name_buf, &luo_session_fops, session,O_RDWR ); -if (IS_ERR (file)) +if (IS_ERR (file)) { +luo_session_put (session);return PTR_ERR (file); + } *filep = file;@@ -403,7 +572,7 @@ int luo_session_create(const char *name, struct file **filep) err_remove:luo_session_remove (&luo_session_global.outgoing, session); err_free: -luo_session_free (session); +luo_session_put (session);return err; }@@ -418,6 +587,7 @@ int luo_session_retrieve(const char *name, struct file **filep) scoped_guard(rwsem_read, &sh->rwsem) {list_for_each_entry (it, &sh->list, list) {if (!strncmp (it->name, name,sizeof (it->name))) { +luo_session_get (it); session = it;break ; }@@ -428,12 +598,14 @@ int luo_session_retrieve(const char *name, struct file **filep)return -ENOENT ;guard (mutex)(&session->mutex); -if (session->retrieved) -return -EINVAL ; +if (session->state !=LUO_SESSION_INCOMING ) + err = -EINVAL ; +else + err =luo_session_getfile (session, filep); - err =luo_session_getfile (session, filep);if (!err) - session->retrieved = true; + session->state =LUO_SESSION_RETRIEVED ; +luo_session_put (session);return err; }@@ -548,6 +720,7 @@ int luo_session_deserialize(void) sh->ser[i].name, session);return PTR_ERR (session); } + session->state =LUO_SESSION_INCOMING ; err =luo_session_insert (sh, session);if (err) {@@ -578,6 +751,17 @@ int luo_session_serialize(void)int err;guard (rwsem_write)(&sh->rwsem); +if (READ_ONCE (sh->rebooting)) +return -EBUSY ; + +while (atomic_read (&sh->closing)) { +up_write (&sh->rwsem); +wait_event (sh->reboot_waitq, !atomic_read (&sh->closing)); +down_write (&sh->rwsem); +if (READ_ONCE (sh->rebooting)) +return -EBUSY ; + } +WRITE_ONCE (sh->rebooting, true);list_for_each_entry (session, &sh->list, list) { err =luo_session_freeze_one (session, &sh->ser[i]);if (err)@@ -595,8 +779,11 @@ int luo_session_serialize(void)list_for_each_entry_continue_reverse (session, &sh->list, list) { i--;luo_session_unfreeze_one (session, &sh->ser[i]); -memset (sh->ser[i].name,0 ,sizeof (sh->ser[i].name)); +memset (&sh->ser[i],0 ,sizeof (sh->ser[i])); } + sh->header_ser->count =0 ; +/* Reset rebooting flag on serialization failure. */ +luo_session_reboot_done (sh);return err; }@@ -624,7 +811,8 @@ bool luo_session_quiesce(void)down_write (&luo_session_global.outgoing.rwsem);if (luo_session_global.incoming.count || - luo_session_global.outgoing.count) { + luo_session_global.outgoing.count || +atomic_read (&luo_session_global.outgoing.closing)) {up_write (&luo_session_global.outgoing.rwsem);up_write (&luo_session_global.incoming.rwsem);return false; --2.53.0
Pasha Tatashin
maintainerQuoted 49 lines · Pasha Tatashin
Quoted 17 lines · Oskar Gerlicz Kowalczuk
kernel_kexec()serializes outgoing sessions before the reboot path freezes tasks, soclose()and session ioctls can still mutate a session while handover state is being prepared. The original v2 code also let incoming lookups keep a bare session pointer after dropping the list lock.That leaves two correctness problems in the reboot path: outgoing state can change after serialization starts, and incoming sessions can be freed while another thread still holds a pointer to them.
Add refcounted session lifetime management, track in-flight outgoing
close()paths with an atomic closing counter, and make serialization wait for closing to drain before setting rebooting. Reject phase-invalid ioctls, keep incoming release on a common cleanup path, and make the release wait freezable without spinning.Quoted 6 lines
Fixes fc5acd5c89fe ("liveupdate: block outgoing session updates during reboot")Signed-off-by Oskar Gerlicz Kowalczuk <[email protected]>--- kernel/liveupdate/luo_internal.h | 12 +- kernel/liveupdate/luo_session.c | 236 +++++++++++++++++++++++++++---- 2 files changed, 221 insertions(+), 27 deletions(-)Hi Oskar,
Thank you for sending this series and finding these bugs in LUO. I agree with Andrew that a cover letter would help to understand the summary of the overall effort.
I have not reviewed the other patches yet, but for this patch, my understanding is that it solves two specific races during
reboot()
syscalls: session closure after serialization, and the addition of new sessions or preserving new files after serialization.Given that KHO is now stateless, and
liveupdate_reboot()is specifically placed at the last point where we can still return an error to userspace, we should simply return an error if a userspace is doing something unexpected.Instead of creating a new state machine, let's just reuse the file references and simply take them for each session at the beginning of serialization. This ensures that no session closes will happen later. For file preservation and session addition, we can block them by simply adding a new boolean.
Quoted 5 lines
Please take a look at the two patches below and see if this approach would work. It is a much smaller change compared to the proposed state machine in this patch.
https://git.kernel.org/pub/scm/linux/kernel/git/tatashin/linux.git/log/?h=luo-reboot-sync/rfc/1
Oskar, I made a few more changes to avoid returning an error if get_file_active() fails. This prevents a race condition where the user might call close(session_fd) right before calling reboot(). I force-updated the above branch. Please let me know if you want to take these changes and use them to in the next version.
Pasha
Quoted 57 lines · Pasha Tatashin
Quoted 49 lines · Pasha Tatashin
Quoted 17 lines · Oskar Gerlicz Kowalczuk
kernel_kexec()serializes outgoing sessions before the reboot path freezes tasks, soclose()and session ioctls can still mutate a session while handover state is being prepared. The original v2 code also let incoming lookups keep a bare session pointer after dropping the list lock.That leaves two correctness problems in the reboot path: outgoing state can change after serialization starts, and incoming sessions can be freed while another thread still holds a pointer to them.
Add refcounted session lifetime management, track in-flight outgoing
close()paths with an atomic closing counter, and make serialization wait for closing to drain before setting rebooting. Reject phase-invalid ioctls, keep incoming release on a common cleanup path, and make the release wait freezable without spinning.Quoted 6 lines
Fixes fc5acd5c89fe ("liveupdate: block outgoing session updates during reboot")Signed-off-by Oskar Gerlicz Kowalczuk <[email protected]>--- kernel/liveupdate/luo_internal.h | 12 +- kernel/liveupdate/luo_session.c | 236 +++++++++++++++++++++++++++---- 2 files changed, 221 insertions(+), 27 deletions(-)Hi Oskar,
Thank you for sending this series and finding these bugs in LUO. I agree with Andrew that a cover letter would help to understand the summary of the overall effort.
I have not reviewed the other patches yet, but for this patch, my understanding is that it solves two specific races during
reboot()
syscalls: session closure after serialization, and the addition of new sessions or preserving new files after serialization.Given that KHO is now stateless, and
liveupdate_reboot()is specifically placed at the last point where we can still return an error to userspace, we should simply return an error if a userspace is doing something unexpected.Instead of creating a new state machine, let's just reuse the file references and simply take them for each session at the beginning of serialization. This ensures that no session closes will happen later. For file preservation and session addition, we can block them by simply adding a new boolean.
Quoted 5 lines
Please take a look at the two patches below and see if this approach would work. It is a much smaller change compared to the proposed state machine in this patch.
https://git.kernel.org/pub/scm/linux/kernel/git/tatashin/linux.git/log/?h=luo-reboot-sync/rfc/1
Quoted 7 lines
Oskar, I made a few more changes to avoid returning an error if
get_file_active()fails. This prevents a race condition where the user might callclose(session_fd)right before callingreboot(). I force-updated the above branch. Please let me know if you want to take these changes and use them to in the next version.Pasha
Hi Pasha,
thank you for taking the time to prototype this approach and for the detailed explanation, I really appreciate it.
I agree that reusing file references and introducing a simple blocking mechanism makes the solution much smaller and easier to reason about compared to a dedicated state machine. Your patches definitely move things in a nice direction in terms of simplicity.
While going through it, I was wondering if there might still be a couple of corner cases worth discussing. In particular, do you think a boolean gate is sufficient to cover in-flight operations that may have already passed the check before serialization starts? It seems like those paths could still potentially mutate session state during serialization.
I was also thinking about the lifetime of incoming sessions (especially lookups holding pointers). Do you think file reference handling alone is enough there, or would we still need some explicit lifetime protection?
I’m currently working on v4 and will take a closer look at your branch to see if we can combine both approaches in a way that keeps the solution simple while still covering these cases.
Thanks,
Oskar Gerlicz Kowalczuk
Pasha Tatashin
maintainerQuoted 77 lines · On Mon, Mar 23, 2026 at 4:54 PM <[email protected]> wrote:
Quoted 57 lines · Pasha Tatashin
Quoted 49 lines · Pasha Tatashin
Quoted 17 lines · Oskar Gerlicz Kowalczuk
kernel_kexec()serializes outgoing sessions before the reboot path freezes tasks, soclose()and session ioctls can still mutate a session while handover state is being prepared. The original v2 code also let incoming lookups keep a bare session pointer after dropping the list lock.That leaves two correctness problems in the reboot path: outgoing state can change after serialization starts, and incoming sessions can be freed while another thread still holds a pointer to them.
Add refcounted session lifetime management, track in-flight outgoing
close()paths with an atomic closing counter, and make serialization wait for closing to drain before setting rebooting. Reject phase-invalid ioctls, keep incoming release on a common cleanup path, and make the release wait freezable without spinning.Quoted 6 lines
Fixes fc5acd5c89fe ("liveupdate: block outgoing session updates during reboot")Signed-off-by Oskar Gerlicz Kowalczuk <[email protected]>--- kernel/liveupdate/luo_internal.h | 12 +- kernel/liveupdate/luo_session.c | 236 +++++++++++++++++++++++++++---- 2 files changed, 221 insertions(+), 27 deletions(-)Hi Oskar,
Thank you for sending this series and finding these bugs in LUO. I agree with Andrew that a cover letter would help to understand the summary of the overall effort.
I have not reviewed the other patches yet, but for this patch, my understanding is that it solves two specific races during
reboot()
syscalls: session closure after serialization, and the addition of new sessions or preserving new files after serialization.Given that KHO is now stateless, and
liveupdate_reboot()is specifically placed at the last point where we can still return an error to userspace, we should simply return an error if a userspace is doing something unexpected.Instead of creating a new state machine, let's just reuse the file references and simply take them for each session at the beginning of serialization. This ensures that no session closes will happen later. For file preservation and session addition, we can block them by simply adding a new boolean.
Quoted 5 lines
Please take a look at the two patches below and see if this approach would work. It is a much smaller change compared to the proposed state machine in this patch.
https://git.kernel.org/pub/scm/linux/kernel/git/tatashin/linux.git/log/?h=luo-reboot-sync/rfc/1
Quoted 7 lines
Oskar, I made a few more changes to avoid returning an error if
get_file_active()fails. This prevents a race condition where the user might callclose(session_fd)right before callingreboot(). I force-updated the above branch. Please let me know if you want to take these changes and use them to in the next version.Pasha
Hi Pasha,
thank you for taking the time to prototype this approach and for the detailed explanation, I really appreciate it.
I agree that reusing file references and introducing a simple blocking mechanism makes the solution much smaller and easier to reason about compared to a dedicated state machine. Your patches definitely move things in a nice direction in terms of simplicity.
Quoted 5 lines
While going through it, I was wondering if there might still be a couple of corner cases worth discussing. In particular, do you think a boolean gate is sufficient to cover in-flight operations that may have already passed the check before serialization starts? It seems like those paths could still potentially mutate session state during serialization.
I think it is robust, it works in conjucition with session mutex. If an operation already passed the check, it already holds the session mutex, and since serialization also takes this mutex, it will see consistent data after pinning sessions via luo_session_get_all_outgoing().
Quoted 4 lines
I was also thinking about the lifetime of incoming sessions (especially lookups holding pointers). Do you think file reference handling alone is enough there, or would we still need some explicit lifetime protection?
I'm not sure about that; I have not looked into those patches in your series yet.
Quoted 4 lines
I’m currently working on v4 and will take a closer look at your branch to see if we can combine both approaches in a way that keeps the solution simple while still covering these cases.
Thanks!
Pasha