5 Some users are really reluctant to reboot a system. This brings the need
6 to provide more livepatches and maintain some compatibility between them.
8 Maintaining more livepatches is much easier with cumulative livepatches.
9 Each new livepatch completely replaces any older one. It can keep,
10 add, and even remove fixes. And it is typically safe to replace any version
11 of the livepatch with any other one thanks to the atomic replace feature.
13 The problems might come with shadow variables and callbacks. They might
14 change the system behavior or state so that it is no longer safe to
15 go back and use an older livepatch or the original kernel code. Also
16 any new livepatch must be able to detect what changes have already been
17 done by the already installed livepatches.
19 This is where the livepatch system state tracking gets useful. It
22 - store data needed to manipulate and restore the system state
24 - define compatibility between livepatches using a change id
28 1. Livepatch system state API
29 =============================
31 The state of the system might get modified either by several livepatch callbacks
32 or by the newly used code. Also it must be possible to find changes done by
33 already installed livepatches.
35 Each modified state is described by struct klp_state, see
36 include/linux/livepatch.h.
38 Each livepatch defines an array of struct klp_states. They mention
39 all states that the livepatch modifies.
41 The livepatch author must define the following two fields for each
46 - Non-zero number used to identify the affected system state.
50 - Number describing the variant of the system state change that
51 is supported by the given livepatch.
53 The state can be manipulated using two functions:
55 - *klp_get_state(patch, id)*
57 - Get struct klp_state associated with the given livepatch
60 - *klp_get_prev_state(id)*
62 - Get struct klp_state associated with the given feature id and
63 already installed livepatches.
65 2. Livepatch compatibility
66 ==========================
68 The system state version is used to prevent loading incompatible livepatches.
69 The check is done when the livepatch is enabled. The rules are:
71 - Any completely new system state modification is allowed.
73 - System state modifications with the same or higher version are allowed
74 for already modified system states.
76 - Cumulative livepatches must handle all system state modifications from
77 already installed livepatches.
79 - Non-cumulative livepatches are allowed to touch already modified
82 3. Supported scenarios
83 ======================
85 Livepatches have their life-cycle and the same is true for the system
86 state changes. Every compatible livepatch has to support the following
89 - Modify the system state when the livepatch gets enabled and the state
90 has not been already modified by a livepatches that are being
93 - Take over or update the system state modification when is has already
94 been done by a livepatch that is being replaced.
96 - Restore the original state when the livepatch is disabled.
98 - Restore the previous state when the transition is reverted.
99 It might be the original system state or the state modification
100 done by livepatches that were being replaced.
102 - Remove any already made changes when error occurs and the livepatch
108 System states are usually modified by livepatch callbacks. The expected
109 role of each callback is as follows:
113 - Allocate *state->data* when necessary. The allocation might fail
114 and *pre_patch()* is the only callback that could stop loading
115 of the livepatch. The allocation is not needed when the data
116 are already provided by previously installed livepatches.
118 - Do any other preparatory action that is needed by
119 the new code even before the transition gets finished.
120 For example, initialize *state->data*.
122 The system state itself is typically modified in *post_patch()*
123 when the entire system is able to handle it.
125 - Clean up its own mess in case of error. It might be done by a custom
126 code or by calling *post_unpatch()* explicitly.
130 - Copy *state->data* from the previous livepatch when they are
133 - Do the actual system state modification. Eventually allow
134 the new code to use it.
136 - Make sure that *state->data* has all necessary information.
138 - Free *state->data* from replaces livepatches when they are
143 - Prevent the code, added by the livepatch, relying on the system
146 - Revert the system state modification..
150 - Distinguish transition reverse and livepatch disabling by
151 checking *klp_get_prev_state()*.
153 - In case of transition reverse, restore the previous system
154 state. It might mean doing nothing.
156 - Remove any not longer needed setting or data.
160 *pre_unpatch()* typically does symmetric operations to *post_patch()*.
161 Except that it is called only when the livepatch is being disabled.
162 Therefore it does not need to care about any previously installed
165 *post_unpatch()* typically does symmetric operations to *pre_patch()*.
166 It might be called also during the transition reverse. Therefore it
167 has to handle the state of the previously installed livepatches.