xref: /linux/kernel/liveupdate/luo_internal.h (revision 23b0f90ba871f096474e1c27c3d14f455189d2d9)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 
3 /*
4  * Copyright (c) 2025, Google LLC.
5  * Pasha Tatashin <pasha.tatashin@soleen.com>
6  */
7 
8 #ifndef _LINUX_LUO_INTERNAL_H
9 #define _LINUX_LUO_INTERNAL_H
10 
11 #include <linux/liveupdate.h>
12 #include <linux/uaccess.h>
13 
14 struct luo_ucmd {
15 	void __user *ubuffer;
16 	u32 user_size;
17 	void *cmd;
18 };
19 
20 static inline int luo_ucmd_respond(struct luo_ucmd *ucmd,
21 				   size_t kernel_cmd_size)
22 {
23 	/*
24 	 * Copy the minimum of what the user provided and what we actually
25 	 * have.
26 	 */
27 	if (copy_to_user(ucmd->ubuffer, ucmd->cmd,
28 			 min_t(size_t, ucmd->user_size, kernel_cmd_size))) {
29 		return -EFAULT;
30 	}
31 	return 0;
32 }
33 
34 /*
35  * Handles a deserialization failure: devices and memory is in unpredictable
36  * state.
37  *
38  * Continuing the boot process after a failure is dangerous because it could
39  * lead to leaks of private data.
40  */
41 #define luo_restore_fail(__fmt, ...) panic(__fmt, ##__VA_ARGS__)
42 
43 /**
44  * struct luo_file_set - A set of files that belong to the same sessions.
45  * @files_list: An ordered list of files associated with this session, it is
46  *              ordered by preservation time.
47  * @files:      The physically contiguous memory block that holds the serialized
48  *              state of files.
49  * @count:      A counter tracking the number of files currently stored in the
50  *              @files_list for this session.
51  */
52 struct luo_file_set {
53 	struct list_head files_list;
54 	struct luo_file_ser *files;
55 	long count;
56 };
57 
58 /**
59  * struct luo_session - Represents an active or incoming Live Update session.
60  * @name:       A unique name for this session, used for identification and
61  *              retrieval.
62  * @ser:        Pointer to the serialized data for this session.
63  * @list:       A list_head member used to link this session into a global list
64  *              of either outgoing (to be preserved) or incoming (restored from
65  *              previous kernel) sessions.
66  * @retrieved:  A boolean flag indicating whether this session has been
67  *              retrieved by a consumer in the new kernel.
68  * @file_set:   A set of files that belong to this session.
69  * @mutex:      protects fields in the luo_session.
70  */
71 struct luo_session {
72 	char name[LIVEUPDATE_SESSION_NAME_LENGTH];
73 	struct luo_session_ser *ser;
74 	struct list_head list;
75 	bool retrieved;
76 	struct luo_file_set file_set;
77 	struct mutex mutex;
78 };
79 
80 int luo_session_create(const char *name, struct file **filep);
81 int luo_session_retrieve(const char *name, struct file **filep);
82 int __init luo_session_setup_outgoing(void *fdt);
83 int __init luo_session_setup_incoming(void *fdt);
84 int luo_session_serialize(void);
85 int luo_session_deserialize(void);
86 bool luo_session_quiesce(void);
87 void luo_session_resume(void);
88 
89 int luo_preserve_file(struct luo_file_set *file_set, u64 token, int fd);
90 void luo_file_unpreserve_files(struct luo_file_set *file_set);
91 int luo_file_freeze(struct luo_file_set *file_set,
92 		    struct luo_file_set_ser *file_set_ser);
93 void luo_file_unfreeze(struct luo_file_set *file_set,
94 		       struct luo_file_set_ser *file_set_ser);
95 int luo_retrieve_file(struct luo_file_set *file_set, u64 token,
96 		      struct file **filep);
97 int luo_file_finish(struct luo_file_set *file_set);
98 int luo_file_deserialize(struct luo_file_set *file_set,
99 			 struct luo_file_set_ser *file_set_ser);
100 void luo_file_set_init(struct luo_file_set *file_set);
101 void luo_file_set_destroy(struct luo_file_set *file_set);
102 
103 int luo_flb_file_preserve(struct liveupdate_file_handler *fh);
104 void luo_flb_file_unpreserve(struct liveupdate_file_handler *fh);
105 void luo_flb_file_finish(struct liveupdate_file_handler *fh);
106 int __init luo_flb_setup_outgoing(void *fdt);
107 int __init luo_flb_setup_incoming(void *fdt);
108 void luo_flb_serialize(void);
109 
110 #ifdef CONFIG_LIVEUPDATE_TEST
111 void liveupdate_test_register(struct liveupdate_file_handler *fh);
112 void liveupdate_test_unregister(struct liveupdate_file_handler *fh);
113 #else
114 static inline void liveupdate_test_register(struct liveupdate_file_handler *fh) { }
115 static inline void liveupdate_test_unregister(struct liveupdate_file_handler *fh) { }
116 #endif
117 
118 #endif /* _LINUX_LUO_INTERNAL_H */
119