Skip to content

Commit cab056f

Browse files
soleenakpm00
authored andcommitted
liveupdate: luo_flb: introduce File-Lifecycle-Bound global state
Introduce a mechanism for managing global kernel state whose lifecycle is tied to the preservation of one or more files. This is necessary for subsystems where multiple preserved file descriptors depend on a single, shared underlying resource. An example is HugeTLB, where multiple file descriptors such as memfd and guest_memfd may rely on the state of a single HugeTLB subsystem. Preserving this state for each individual file would be redundant and incorrect. The state should be preserved only once when the first file is preserved, and restored/finished only once the last file is handled. This patch introduces File-Lifecycle-Bound (FLB) objects to solve this problem. An FLB is a global, reference-counted object with a defined set of operations: - A file handler (struct liveupdate_file_handler) declares a dependency on one or more FLBs via a new registration function, liveupdate_register_flb(). - When the first file depending on an FLB is preserved, the FLB's .preserve() callback is invoked to save the shared global state. The reference count is then incremented for each subsequent file. - Conversely, when the last file is unpreserved (before reboot) or finished (after reboot), the FLB's .unpreserve() or .finish() callback is invoked to clean up the global resource. The implementation includes: - A new set of ABI definitions (luo_flb_ser, luo_flb_head_ser) and a corresponding FDT node (luo-flb) to serialize the state of all active FLBs and pass them via Kexec Handover. - Core logic in luo_flb.c to manage FLB registration, reference counting, and the invocation of lifecycle callbacks. - An API (liveupdate_flb_get/_incoming/_outgoing) for other kernel subsystems to safely access the live object managed by an FLB, both before and after the live update. This framework provides the necessary infrastructure for more complex subsystems like IOMMU, VFIO, and KVM to integrate with the Live Update Orchestrator. Link: https://lkml.kernel.org/r/20251218155752.3045808-5-pasha.tatashin@soleen.com Signed-off-by: Pasha Tatashin <pasha.tatashin@soleen.com> Cc: Alexander Graf <graf@amazon.com> Cc: David Gow <davidgow@google.com> Cc: David Matlack <dmatlack@google.com> Cc: David Rientjes <rientjes@google.com> Cc: Jonathan Corbet <corbet@lwn.net> Cc: Kees Cook <kees@kernel.org> Cc: Mike Rapoport <rppt@kernel.org> Cc: Petr Mladek <pmladek@suse.com> Cc: Pratyush Yadav <pratyush@kernel.org> Cc: Samiullah Khawaja <skhawaja@google.com> Cc: Tamir Duberstein <tamird@gmail.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
1 parent 6845645 commit cab056f

8 files changed

Lines changed: 924 additions & 3 deletions

File tree

Documentation/core-api/liveupdate.rst

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@ LUO Preserving File Descriptors
1818
.. kernel-doc:: kernel/liveupdate/luo_file.c
1919
:doc: LUO File Descriptors
2020

21+
LUO File Lifecycle Bound Global Data
22+
====================================
23+
.. kernel-doc:: kernel/liveupdate/luo_flb.c
24+
:doc: LUO File Lifecycle Bound Global Data
25+
2126
Live Update Orchestrator ABI
2227
============================
2328
.. kernel-doc:: include/linux/kho/abi/luo.h
@@ -40,6 +45,9 @@ Public API
4045
.. kernel-doc:: kernel/liveupdate/luo_core.c
4146
:export:
4247

48+
.. kernel-doc:: kernel/liveupdate/luo_flb.c
49+
:export:
50+
4351
.. kernel-doc:: kernel/liveupdate/luo_file.c
4452
:export:
4553

@@ -48,6 +56,9 @@ Internal API
4856
.. kernel-doc:: kernel/liveupdate/luo_core.c
4957
:internal:
5058

59+
.. kernel-doc:: kernel/liveupdate/luo_flb.c
60+
:internal:
61+
5162
.. kernel-doc:: kernel/liveupdate/luo_session.c
5263
:internal:
5364

include/linux/kho/abi/luo.h

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@
3737
* compatible = "luo-session-v1";
3838
* luo-session-header = <phys_addr_of_session_header_ser>;
3939
* };
40+
*
41+
* luo-flb {
42+
* compatible = "luo-flb-v1";
43+
* luo-flb-header = <phys_addr_of_flb_header_ser>;
44+
* };
4045
* };
4146
*
4247
* Main LUO Node (/):
@@ -56,6 +61,17 @@
5661
* is the header for a contiguous block of memory containing an array of
5762
* `struct luo_session_ser`, one for each preserved session.
5863
*
64+
* File-Lifecycle-Bound Node (luo-flb):
65+
* This node describes all preserved global objects whose lifecycle is bound
66+
* to that of the preserved files (e.g., shared IOMMU state).
67+
*
68+
* - compatible: "luo-flb-v1"
69+
* Identifies the FLB ABI version.
70+
* - luo-flb-header: u64
71+
* The physical address of a `struct luo_flb_header_ser`. This structure is
72+
* the header for a contiguous block of memory containing an array of
73+
* `struct luo_flb_ser`, one for each preserved global object.
74+
*
5975
* Serialization Structures:
6076
* The FDT properties point to memory regions containing arrays of simple,
6177
* `__packed` structures. These structures contain the actual preserved state.
@@ -74,6 +90,16 @@
7490
* Metadata for a single preserved file. Contains the `compatible` string to
7591
* find the correct handler in the new kernel, a user-provided `token` for
7692
* identification, and an opaque `data` handle for the handler to use.
93+
*
94+
* - struct luo_flb_header_ser:
95+
* Header for the FLB array. Contains the total page count of the
96+
* preserved memory block and the number of `struct luo_flb_ser` entries
97+
* that follow.
98+
*
99+
* - struct luo_flb_ser:
100+
* Metadata for a single preserved global object. Contains its `name`
101+
* (compatible string), an opaque `data` handle, and the `count`
102+
* number of files depending on it.
77103
*/
78104

79105
#ifndef _LINUX_KHO_ABI_LUO_H
@@ -163,4 +189,54 @@ struct luo_session_ser {
163189
struct luo_file_set_ser file_set_ser;
164190
} __packed;
165191

192+
/* The max size is set so it can be reliably used during in serialization */
193+
#define LIVEUPDATE_FLB_COMPAT_LENGTH 48
194+
195+
#define LUO_FDT_FLB_NODE_NAME "luo-flb"
196+
#define LUO_FDT_FLB_COMPATIBLE "luo-flb-v1"
197+
#define LUO_FDT_FLB_HEADER "luo-flb-header"
198+
199+
/**
200+
* struct luo_flb_header_ser - Header for the serialized FLB data block.
201+
* @pgcnt: The total number of pages occupied by the entire preserved memory
202+
* region, including this header and the subsequent array of
203+
* &struct luo_flb_ser entries.
204+
* @count: The number of &struct luo_flb_ser entries that follow this header
205+
* in the memory block.
206+
*
207+
* This structure is located at the physical address specified by the
208+
* `LUO_FDT_FLB_HEADER` FDT property. It provides the new kernel with the
209+
* necessary information to find and iterate over the array of preserved
210+
* File-Lifecycle-Bound objects and to manage the underlying memory.
211+
*
212+
* If this structure is modified, LUO_FDT_FLB_COMPATIBLE must be updated.
213+
*/
214+
struct luo_flb_header_ser {
215+
u64 pgcnt;
216+
u64 count;
217+
} __packed;
218+
219+
/**
220+
* struct luo_flb_ser - Represents the serialized state of a single FLB object.
221+
* @name: The unique compatibility string of the FLB object, used to find the
222+
* corresponding &struct liveupdate_flb handler in the new kernel.
223+
* @data: The opaque u64 handle returned by the FLB's .preserve() operation
224+
* in the old kernel. This handle encapsulates the entire state needed
225+
* for restoration.
226+
* @count: The reference count at the time of serialization; i.e., the number
227+
* of preserved files that depended on this FLB. This is used by the
228+
* new kernel to correctly manage the FLB's lifecycle.
229+
*
230+
* An array of these structures is created in a preserved memory region and
231+
* passed to the new kernel. Each entry allows the LUO core to restore one
232+
* global, shared object.
233+
*
234+
* If this structure is modified, LUO_FDT_FLB_COMPATIBLE must be updated.
235+
*/
236+
struct luo_flb_ser {
237+
char name[LIVEUPDATE_FLB_COMPAT_LENGTH];
238+
u64 data;
239+
u64 count;
240+
} __packed;
241+
166242
#endif /* _LINUX_KHO_ABI_LUO_H */

include/linux/liveupdate.h

Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,13 @@
1111
#include <linux/compiler.h>
1212
#include <linux/kho/abi/luo.h>
1313
#include <linux/list.h>
14+
#include <linux/mutex.h>
1415
#include <linux/types.h>
1516
#include <uapi/linux/liveupdate.h>
1617

1718
struct liveupdate_file_handler;
19+
struct liveupdate_flb;
20+
struct liveupdate_session;
1821
struct file;
1922

2023
/**
@@ -99,6 +102,118 @@ struct liveupdate_file_handler {
99102
* registered file handlers.
100103
*/
101104
struct list_head __private list;
105+
/* A list of FLB dependencies. */
106+
struct list_head __private flb_list;
107+
};
108+
109+
/**
110+
* struct liveupdate_flb_op_args - Arguments for FLB operation callbacks.
111+
* @flb: The global FLB instance for which this call is performed.
112+
* @data: For .preserve(): [OUT] The callback sets this field.
113+
* For .unpreserve(): [IN] The handle from .preserve().
114+
* For .retrieve(): [IN] The handle from .preserve().
115+
* @obj: For .preserve(): [OUT] Sets this to the live object.
116+
* For .retrieve(): [OUT] Sets this to the live object.
117+
* For .finish(): [IN] The live object from .retrieve().
118+
*
119+
* This structure bundles all parameters for the FLB operation callbacks.
120+
*/
121+
struct liveupdate_flb_op_args {
122+
struct liveupdate_flb *flb;
123+
u64 data;
124+
void *obj;
125+
};
126+
127+
/**
128+
* struct liveupdate_flb_ops - Callbacks for global File-Lifecycle-Bound data.
129+
* @preserve: Called when the first file using this FLB is preserved.
130+
* The callback must save its state and return a single,
131+
* self-contained u64 handle by setting the 'argp->data'
132+
* field and 'argp->obj'.
133+
* @unpreserve: Called when the last file using this FLB is unpreserved
134+
* (aborted before reboot). Receives the handle via
135+
* 'argp->data' and live object via 'argp->obj'.
136+
* @retrieve: Called on-demand in the new kernel, the first time a
137+
* component requests access to the shared object. It receives
138+
* the preserved handle via 'argp->data' and must reconstruct
139+
* the live object, returning it by setting the 'argp->obj'
140+
* field.
141+
* @finish: Called in the new kernel when the last file using this FLB
142+
* is finished. Receives the live object via 'argp->obj' for
143+
* cleanup.
144+
* @owner: Module reference
145+
*
146+
* Operations that manage global shared data with file bound lifecycle,
147+
* triggered by the first file that uses it and concluded by the last file that
148+
* uses it, across all sessions.
149+
*/
150+
struct liveupdate_flb_ops {
151+
int (*preserve)(struct liveupdate_flb_op_args *argp);
152+
void (*unpreserve)(struct liveupdate_flb_op_args *argp);
153+
int (*retrieve)(struct liveupdate_flb_op_args *argp);
154+
void (*finish)(struct liveupdate_flb_op_args *argp);
155+
struct module *owner;
156+
};
157+
158+
/*
159+
* struct luo_flb_private_state - Private FLB state structures.
160+
* @count: The number of preserved files currently depending on this FLB.
161+
* This is used to trigger the preserve/unpreserve/finish ops on the
162+
* first/last file.
163+
* @data: The opaque u64 handle returned by .preserve() or passed to
164+
* .retrieve().
165+
* @obj: The live kernel object returned by .preserve() or .retrieve().
166+
* @lock: A mutex that protects all fields within this structure, providing
167+
* the synchronization service for the FLB's ops.
168+
* @finished: True once the FLB's finish() callback has run.
169+
* @retrieved: True once the FLB's retrieve() callback has run.
170+
*/
171+
struct luo_flb_private_state {
172+
long count;
173+
u64 data;
174+
void *obj;
175+
struct mutex lock;
176+
bool finished;
177+
bool retrieved;
178+
};
179+
180+
/*
181+
* struct luo_flb_private - Keep separate incoming and outgoing states.
182+
* @list: A global list of registered FLBs.
183+
* @outgoing: The runtime state for the pre-reboot
184+
* (preserve/unpreserve) lifecycle.
185+
* @incoming: The runtime state for the post-reboot (retrieve/finish)
186+
* lifecycle.
187+
* @users: With how many File-Handlers this FLB is registered.
188+
* @initialized: true when private fields have been initialized.
189+
*/
190+
struct luo_flb_private {
191+
struct list_head list;
192+
struct luo_flb_private_state outgoing;
193+
struct luo_flb_private_state incoming;
194+
int users;
195+
bool initialized;
196+
};
197+
198+
/**
199+
* struct liveupdate_flb - A global definition for a shared data object.
200+
* @ops: Callback functions
201+
* @compatible: The compatibility string (e.g., "iommu-core-v1"
202+
* that uniquely identifies the FLB type this handler
203+
* supports. This is matched against the compatible string
204+
* associated with individual &struct liveupdate_flb
205+
* instances.
206+
*
207+
* This struct is the "template" that a driver registers to define a shared,
208+
* file-lifecycle-bound object. The actual runtime state (the live object,
209+
* refcount, etc.) is managed privately by the LUO core.
210+
*/
211+
struct liveupdate_flb {
212+
const struct liveupdate_flb_ops *ops;
213+
const char compatible[LIVEUPDATE_FLB_COMPAT_LENGTH];
214+
215+
/* private: */
216+
struct luo_flb_private __private private;
102217
};
103218

104219
#ifdef CONFIG_LIVEUPDATE
@@ -112,6 +227,14 @@ int liveupdate_reboot(void);
112227
int liveupdate_register_file_handler(struct liveupdate_file_handler *fh);
113228
int liveupdate_unregister_file_handler(struct liveupdate_file_handler *fh);
114229

230+
int liveupdate_register_flb(struct liveupdate_file_handler *fh,
231+
struct liveupdate_flb *flb);
232+
int liveupdate_unregister_flb(struct liveupdate_file_handler *fh,
233+
struct liveupdate_flb *flb);
234+
235+
int liveupdate_flb_get_incoming(struct liveupdate_flb *flb, void **objp);
236+
int liveupdate_flb_get_outgoing(struct liveupdate_flb *flb, void **objp);
237+
115238
#else /* CONFIG_LIVEUPDATE */
116239

117240
static inline bool liveupdate_enabled(void)
@@ -134,5 +257,29 @@ static inline int liveupdate_unregister_file_handler(struct liveupdate_file_hand
134257
return -EOPNOTSUPP;
135258
}
136259

260+
static inline int liveupdate_register_flb(struct liveupdate_file_handler *fh,
261+
struct liveupdate_flb *flb)
262+
{
263+
return -EOPNOTSUPP;
264+
}
265+
266+
static inline int liveupdate_unregister_flb(struct liveupdate_file_handler *fh,
267+
struct liveupdate_flb *flb)
268+
{
269+
return -EOPNOTSUPP;
270+
}
271+
272+
static inline int liveupdate_flb_get_incoming(struct liveupdate_flb *flb,
273+
void **objp)
274+
{
275+
return -EOPNOTSUPP;
276+
}
277+
278+
static inline int liveupdate_flb_get_outgoing(struct liveupdate_flb *flb,
279+
void **objp)
280+
{
281+
return -EOPNOTSUPP;
282+
}
283+
137284
#endif /* CONFIG_LIVEUPDATE */
138285
#endif /* _LINUX_LIVEUPDATE_H */

kernel/liveupdate/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
luo-y := \
44
luo_core.o \
55
luo_file.o \
6+
luo_flb.o \
67
luo_session.o
78

89
obj-$(CONFIG_KEXEC_HANDOVER) += kexec_handover.o

kernel/liveupdate/luo_core.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,9 @@ static int __init luo_early_startup(void)
127127
if (err)
128128
return err;
129129

130-
return 0;
130+
err = luo_flb_setup_incoming(luo_global.fdt_in);
131+
132+
return err;
131133
}
132134

133135
static int __init liveupdate_early_init(void)
@@ -164,6 +166,7 @@ static int __init luo_fdt_setup(void)
164166
err |= fdt_property_string(fdt_out, "compatible", LUO_FDT_COMPATIBLE);
165167
err |= fdt_property(fdt_out, LUO_FDT_LIVEUPDATE_NUM, &ln, sizeof(ln));
166168
err |= luo_session_setup_outgoing(fdt_out);
169+
err |= luo_flb_setup_outgoing(fdt_out);
167170
err |= fdt_end_node(fdt_out);
168171
err |= fdt_finish(fdt_out);
169172
if (err)
@@ -225,6 +228,8 @@ int liveupdate_reboot(void)
225228
if (err)
226229
return err;
227230

231+
luo_flb_serialize();
232+
228233
err = kho_finalize();
229234
if (err) {
230235
pr_err("kho_finalize failed %d\n", err);

0 commit comments

Comments
 (0)