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