1e553d2a5SPetr Mladek==================== 2e553d2a5SPetr MladekSystem State Changes 3e553d2a5SPetr Mladek==================== 4e553d2a5SPetr Mladek 5e553d2a5SPetr MladekSome users are really reluctant to reboot a system. This brings the need 6e553d2a5SPetr Mladekto provide more livepatches and maintain some compatibility between them. 7e553d2a5SPetr Mladek 8e553d2a5SPetr MladekMaintaining more livepatches is much easier with cumulative livepatches. 9e553d2a5SPetr MladekEach new livepatch completely replaces any older one. It can keep, 10e553d2a5SPetr Mladekadd, and even remove fixes. And it is typically safe to replace any version 11e553d2a5SPetr Mladekof the livepatch with any other one thanks to the atomic replace feature. 12e553d2a5SPetr Mladek 13e553d2a5SPetr MladekThe problems might come with shadow variables and callbacks. They might 14e553d2a5SPetr Mladekchange the system behavior or state so that it is no longer safe to 15e553d2a5SPetr Mladekgo back and use an older livepatch or the original kernel code. Also 16e553d2a5SPetr Mladekany new livepatch must be able to detect what changes have already been 17e553d2a5SPetr Mladekdone by the already installed livepatches. 18e553d2a5SPetr Mladek 19e553d2a5SPetr MladekThis is where the livepatch system state tracking gets useful. It 20e553d2a5SPetr Mladekallows to: 21e553d2a5SPetr Mladek 22e553d2a5SPetr Mladek - store data needed to manipulate and restore the system state 23e553d2a5SPetr Mladek 24e553d2a5SPetr Mladek - define compatibility between livepatches using a change id 25e553d2a5SPetr Mladek and version 26e553d2a5SPetr Mladek 27e553d2a5SPetr Mladek 28e553d2a5SPetr Mladek1. Livepatch system state API 29e553d2a5SPetr Mladek============================= 30e553d2a5SPetr Mladek 31e553d2a5SPetr MladekThe state of the system might get modified either by several livepatch callbacks 32e553d2a5SPetr Mladekor by the newly used code. Also it must be possible to find changes done by 33e553d2a5SPetr Mladekalready installed livepatches. 34e553d2a5SPetr Mladek 35e553d2a5SPetr MladekEach modified state is described by struct klp_state, see 36e553d2a5SPetr Mladekinclude/linux/livepatch.h. 37e553d2a5SPetr Mladek 38e553d2a5SPetr MladekEach livepatch defines an array of struct klp_states. They mention 39e553d2a5SPetr Mladekall states that the livepatch modifies. 40e553d2a5SPetr Mladek 41e553d2a5SPetr MladekThe livepatch author must define the following two fields for each 42e553d2a5SPetr Mladekstruct klp_state: 43e553d2a5SPetr Mladek 44e553d2a5SPetr Mladek - *id* 45e553d2a5SPetr Mladek 46e553d2a5SPetr Mladek - Non-zero number used to identify the affected system state. 47e553d2a5SPetr Mladek 48e553d2a5SPetr Mladek - *version* 49e553d2a5SPetr Mladek 50e553d2a5SPetr Mladek - Number describing the variant of the system state change that 51e553d2a5SPetr Mladek is supported by the given livepatch. 52e553d2a5SPetr Mladek 53e553d2a5SPetr MladekThe state can be manipulated using two functions: 54e553d2a5SPetr Mladek 55*e368cd72SDavid Vernet - klp_get_state() 56e553d2a5SPetr Mladek 57e553d2a5SPetr Mladek - Get struct klp_state associated with the given livepatch 58e553d2a5SPetr Mladek and state id. 59e553d2a5SPetr Mladek 60*e368cd72SDavid Vernet - klp_get_prev_state() 61e553d2a5SPetr Mladek 62e553d2a5SPetr Mladek - Get struct klp_state associated with the given feature id and 63e553d2a5SPetr Mladek already installed livepatches. 64e553d2a5SPetr Mladek 65e553d2a5SPetr Mladek2. Livepatch compatibility 66e553d2a5SPetr Mladek========================== 67e553d2a5SPetr Mladek 68e553d2a5SPetr MladekThe system state version is used to prevent loading incompatible livepatches. 69e553d2a5SPetr MladekThe check is done when the livepatch is enabled. The rules are: 70e553d2a5SPetr Mladek 71e553d2a5SPetr Mladek - Any completely new system state modification is allowed. 72e553d2a5SPetr Mladek 73e553d2a5SPetr Mladek - System state modifications with the same or higher version are allowed 74e553d2a5SPetr Mladek for already modified system states. 75e553d2a5SPetr Mladek 76e553d2a5SPetr Mladek - Cumulative livepatches must handle all system state modifications from 77e553d2a5SPetr Mladek already installed livepatches. 78e553d2a5SPetr Mladek 79e553d2a5SPetr Mladek - Non-cumulative livepatches are allowed to touch already modified 80e553d2a5SPetr Mladek system states. 81e553d2a5SPetr Mladek 82e553d2a5SPetr Mladek3. Supported scenarios 83e553d2a5SPetr Mladek====================== 84e553d2a5SPetr Mladek 85e553d2a5SPetr MladekLivepatches have their life-cycle and the same is true for the system 86e553d2a5SPetr Mladekstate changes. Every compatible livepatch has to support the following 87e553d2a5SPetr Mladekscenarios: 88e553d2a5SPetr Mladek 89e553d2a5SPetr Mladek - Modify the system state when the livepatch gets enabled and the state 90e553d2a5SPetr Mladek has not been already modified by a livepatches that are being 91e553d2a5SPetr Mladek replaced. 92e553d2a5SPetr Mladek 93e553d2a5SPetr Mladek - Take over or update the system state modification when is has already 94e553d2a5SPetr Mladek been done by a livepatch that is being replaced. 95e553d2a5SPetr Mladek 96e553d2a5SPetr Mladek - Restore the original state when the livepatch is disabled. 97e553d2a5SPetr Mladek 98e553d2a5SPetr Mladek - Restore the previous state when the transition is reverted. 99e553d2a5SPetr Mladek It might be the original system state or the state modification 100e553d2a5SPetr Mladek done by livepatches that were being replaced. 101e553d2a5SPetr Mladek 102e553d2a5SPetr Mladek - Remove any already made changes when error occurs and the livepatch 103e553d2a5SPetr Mladek cannot get enabled. 104e553d2a5SPetr Mladek 105e553d2a5SPetr Mladek4. Expected usage 106e553d2a5SPetr Mladek================= 107e553d2a5SPetr Mladek 108e553d2a5SPetr MladekSystem states are usually modified by livepatch callbacks. The expected 109e553d2a5SPetr Mladekrole of each callback is as follows: 110e553d2a5SPetr Mladek 111e553d2a5SPetr Mladek*pre_patch()* 112e553d2a5SPetr Mladek 113e553d2a5SPetr Mladek - Allocate *state->data* when necessary. The allocation might fail 114e553d2a5SPetr Mladek and *pre_patch()* is the only callback that could stop loading 115e553d2a5SPetr Mladek of the livepatch. The allocation is not needed when the data 116e553d2a5SPetr Mladek are already provided by previously installed livepatches. 117e553d2a5SPetr Mladek 118e553d2a5SPetr Mladek - Do any other preparatory action that is needed by 119e553d2a5SPetr Mladek the new code even before the transition gets finished. 120e553d2a5SPetr Mladek For example, initialize *state->data*. 121e553d2a5SPetr Mladek 122e553d2a5SPetr Mladek The system state itself is typically modified in *post_patch()* 123e553d2a5SPetr Mladek when the entire system is able to handle it. 124e553d2a5SPetr Mladek 125e553d2a5SPetr Mladek - Clean up its own mess in case of error. It might be done by a custom 126e553d2a5SPetr Mladek code or by calling *post_unpatch()* explicitly. 127e553d2a5SPetr Mladek 128e553d2a5SPetr Mladek*post_patch()* 129e553d2a5SPetr Mladek 130e553d2a5SPetr Mladek - Copy *state->data* from the previous livepatch when they are 131e553d2a5SPetr Mladek compatible. 132e553d2a5SPetr Mladek 133e553d2a5SPetr Mladek - Do the actual system state modification. Eventually allow 134e553d2a5SPetr Mladek the new code to use it. 135e553d2a5SPetr Mladek 136e553d2a5SPetr Mladek - Make sure that *state->data* has all necessary information. 137e553d2a5SPetr Mladek 138e553d2a5SPetr Mladek - Free *state->data* from replaces livepatches when they are 139e553d2a5SPetr Mladek not longer needed. 140e553d2a5SPetr Mladek 141e553d2a5SPetr Mladek*pre_unpatch()* 142e553d2a5SPetr Mladek 143e553d2a5SPetr Mladek - Prevent the code, added by the livepatch, relying on the system 144e553d2a5SPetr Mladek state change. 145e553d2a5SPetr Mladek 146e553d2a5SPetr Mladek - Revert the system state modification.. 147e553d2a5SPetr Mladek 148e553d2a5SPetr Mladek*post_unpatch()* 149e553d2a5SPetr Mladek 150e553d2a5SPetr Mladek - Distinguish transition reverse and livepatch disabling by 151e553d2a5SPetr Mladek checking *klp_get_prev_state()*. 152e553d2a5SPetr Mladek 153e553d2a5SPetr Mladek - In case of transition reverse, restore the previous system 154e553d2a5SPetr Mladek state. It might mean doing nothing. 155e553d2a5SPetr Mladek 156e553d2a5SPetr Mladek - Remove any not longer needed setting or data. 157e553d2a5SPetr Mladek 158e553d2a5SPetr Mladek.. note:: 159e553d2a5SPetr Mladek 160e553d2a5SPetr Mladek *pre_unpatch()* typically does symmetric operations to *post_patch()*. 161e553d2a5SPetr Mladek Except that it is called only when the livepatch is being disabled. 162e553d2a5SPetr Mladek Therefore it does not need to care about any previously installed 163e553d2a5SPetr Mladek livepatch. 164e553d2a5SPetr Mladek 165e553d2a5SPetr Mladek *post_unpatch()* typically does symmetric operations to *pre_patch()*. 166e553d2a5SPetr Mladek It might be called also during the transition reverse. Therefore it 167e553d2a5SPetr Mladek has to handle the state of the previously installed livepatches. 168