xref: /linux/kernel/liveupdate/luo_internal.h (revision 509d3f45847627f4c5cdce004c3ec79262b5239c)
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 
luo_ucmd_respond(struct luo_ucmd * ucmd,size_t kernel_cmd_size)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 /* Mimics list_for_each_entry() but for private list head entries */
44 #define luo_list_for_each_private(pos, head, member)				\
45 	for (struct list_head *__iter = (head)->next;				\
46 	     __iter != (head) &&						\
47 	     ({ pos = container_of(__iter, typeof(*(pos)), member); 1; });	\
48 	     __iter = __iter->next)
49 
50 /**
51  * struct luo_file_set - A set of files that belong to the same sessions.
52  * @files_list: An ordered list of files associated with this session, it is
53  *              ordered by preservation time.
54  * @files:      The physically contiguous memory block that holds the serialized
55  *              state of files.
56  * @count:      A counter tracking the number of files currently stored in the
57  *              @files_list for this session.
58  */
59 struct luo_file_set {
60 	struct list_head files_list;
61 	struct luo_file_ser *files;
62 	long count;
63 };
64 
65 /**
66  * struct luo_session - Represents an active or incoming Live Update session.
67  * @name:       A unique name for this session, used for identification and
68  *              retrieval.
69  * @ser:        Pointer to the serialized data for this session.
70  * @list:       A list_head member used to link this session into a global list
71  *              of either outgoing (to be preserved) or incoming (restored from
72  *              previous kernel) sessions.
73  * @retrieved:  A boolean flag indicating whether this session has been
74  *              retrieved by a consumer in the new kernel.
75  * @file_set:   A set of files that belong to this session.
76  * @mutex:      protects fields in the luo_session.
77  */
78 struct luo_session {
79 	char name[LIVEUPDATE_SESSION_NAME_LENGTH];
80 	struct luo_session_ser *ser;
81 	struct list_head list;
82 	bool retrieved;
83 	struct luo_file_set file_set;
84 	struct mutex mutex;
85 };
86 
87 int luo_session_create(const char *name, struct file **filep);
88 int luo_session_retrieve(const char *name, struct file **filep);
89 int __init luo_session_setup_outgoing(void *fdt);
90 int __init luo_session_setup_incoming(void *fdt);
91 int luo_session_serialize(void);
92 int luo_session_deserialize(void);
93 bool luo_session_quiesce(void);
94 void luo_session_resume(void);
95 
96 int luo_preserve_file(struct luo_file_set *file_set, u64 token, int fd);
97 void luo_file_unpreserve_files(struct luo_file_set *file_set);
98 int luo_file_freeze(struct luo_file_set *file_set,
99 		    struct luo_file_set_ser *file_set_ser);
100 void luo_file_unfreeze(struct luo_file_set *file_set,
101 		       struct luo_file_set_ser *file_set_ser);
102 int luo_retrieve_file(struct luo_file_set *file_set, u64 token,
103 		      struct file **filep);
104 int luo_file_finish(struct luo_file_set *file_set);
105 int luo_file_deserialize(struct luo_file_set *file_set,
106 			 struct luo_file_set_ser *file_set_ser);
107 void luo_file_set_init(struct luo_file_set *file_set);
108 void luo_file_set_destroy(struct luo_file_set *file_set);
109 
110 #endif /* _LINUX_LUO_INTERNAL_H */
111