xref: /openbmc/linux/Documentation/livepatch/cumulative-patches.rst (revision c39f2d9db0fd81ea20bb5cce9b3f082ca63753e2)
1*89e33ea7SMauro Carvalho Chehab===================================
2*89e33ea7SMauro Carvalho ChehabAtomic Replace & Cumulative Patches
3*89e33ea7SMauro Carvalho Chehab===================================
4*89e33ea7SMauro Carvalho Chehab
5*89e33ea7SMauro Carvalho ChehabThere might be dependencies between livepatches. If multiple patches need
6*89e33ea7SMauro Carvalho Chehabto do different changes to the same function(s) then we need to define
7*89e33ea7SMauro Carvalho Chehaban order in which the patches will be installed. And function implementations
8*89e33ea7SMauro Carvalho Chehabfrom any newer livepatch must be done on top of the older ones.
9*89e33ea7SMauro Carvalho Chehab
10*89e33ea7SMauro Carvalho ChehabThis might become a maintenance nightmare. Especially when more patches
11*89e33ea7SMauro Carvalho Chehabmodified the same function in different ways.
12*89e33ea7SMauro Carvalho Chehab
13*89e33ea7SMauro Carvalho ChehabAn elegant solution comes with the feature called "Atomic Replace". It allows
14*89e33ea7SMauro Carvalho Chehabcreation of so called "Cumulative Patches". They include all wanted changes
15*89e33ea7SMauro Carvalho Chehabfrom all older livepatches and completely replace them in one transition.
16*89e33ea7SMauro Carvalho Chehab
17*89e33ea7SMauro Carvalho ChehabUsage
18*89e33ea7SMauro Carvalho Chehab-----
19*89e33ea7SMauro Carvalho Chehab
20*89e33ea7SMauro Carvalho ChehabThe atomic replace can be enabled by setting "replace" flag in struct klp_patch,
21*89e33ea7SMauro Carvalho Chehabfor example::
22*89e33ea7SMauro Carvalho Chehab
23*89e33ea7SMauro Carvalho Chehab	static struct klp_patch patch = {
24*89e33ea7SMauro Carvalho Chehab		.mod = THIS_MODULE,
25*89e33ea7SMauro Carvalho Chehab		.objs = objs,
26*89e33ea7SMauro Carvalho Chehab		.replace = true,
27*89e33ea7SMauro Carvalho Chehab	};
28*89e33ea7SMauro Carvalho Chehab
29*89e33ea7SMauro Carvalho ChehabAll processes are then migrated to use the code only from the new patch.
30*89e33ea7SMauro Carvalho ChehabOnce the transition is finished, all older patches are automatically
31*89e33ea7SMauro Carvalho Chehabdisabled.
32*89e33ea7SMauro Carvalho Chehab
33*89e33ea7SMauro Carvalho ChehabFtrace handlers are transparently removed from functions that are no
34*89e33ea7SMauro Carvalho Chehablonger modified by the new cumulative patch.
35*89e33ea7SMauro Carvalho Chehab
36*89e33ea7SMauro Carvalho ChehabAs a result, the livepatch authors might maintain sources only for one
37*89e33ea7SMauro Carvalho Chehabcumulative patch. It helps to keep the patch consistent while adding or
38*89e33ea7SMauro Carvalho Chehabremoving various fixes or features.
39*89e33ea7SMauro Carvalho Chehab
40*89e33ea7SMauro Carvalho ChehabUsers could keep only the last patch installed on the system after
41*89e33ea7SMauro Carvalho Chehabthe transition to has finished. It helps to clearly see what code is
42*89e33ea7SMauro Carvalho Chehabactually in use. Also the livepatch might then be seen as a "normal"
43*89e33ea7SMauro Carvalho Chehabmodule that modifies the kernel behavior. The only difference is that
44*89e33ea7SMauro Carvalho Chehabit can be updated at runtime without breaking its functionality.
45*89e33ea7SMauro Carvalho Chehab
46*89e33ea7SMauro Carvalho Chehab
47*89e33ea7SMauro Carvalho ChehabFeatures
48*89e33ea7SMauro Carvalho Chehab--------
49*89e33ea7SMauro Carvalho Chehab
50*89e33ea7SMauro Carvalho ChehabThe atomic replace allows:
51*89e33ea7SMauro Carvalho Chehab
52*89e33ea7SMauro Carvalho Chehab  - Atomically revert some functions in a previous patch while
53*89e33ea7SMauro Carvalho Chehab    upgrading other functions.
54*89e33ea7SMauro Carvalho Chehab
55*89e33ea7SMauro Carvalho Chehab  - Remove eventual performance impact caused by core redirection
56*89e33ea7SMauro Carvalho Chehab    for functions that are no longer patched.
57*89e33ea7SMauro Carvalho Chehab
58*89e33ea7SMauro Carvalho Chehab  - Decrease user confusion about dependencies between livepatches.
59*89e33ea7SMauro Carvalho Chehab
60*89e33ea7SMauro Carvalho Chehab
61*89e33ea7SMauro Carvalho ChehabLimitations:
62*89e33ea7SMauro Carvalho Chehab------------
63*89e33ea7SMauro Carvalho Chehab
64*89e33ea7SMauro Carvalho Chehab  - Once the operation finishes, there is no straightforward way
65*89e33ea7SMauro Carvalho Chehab    to reverse it and restore the replaced patches atomically.
66*89e33ea7SMauro Carvalho Chehab
67*89e33ea7SMauro Carvalho Chehab    A good practice is to set .replace flag in any released livepatch.
68*89e33ea7SMauro Carvalho Chehab    Then re-adding an older livepatch is equivalent to downgrading
69*89e33ea7SMauro Carvalho Chehab    to that patch. This is safe as long as the livepatches do _not_ do
70*89e33ea7SMauro Carvalho Chehab    extra modifications in (un)patching callbacks or in the module_init()
71*89e33ea7SMauro Carvalho Chehab    or module_exit() functions, see below.
72*89e33ea7SMauro Carvalho Chehab
73*89e33ea7SMauro Carvalho Chehab    Also note that the replaced patch can be removed and loaded again
74*89e33ea7SMauro Carvalho Chehab    only when the transition was not forced.
75*89e33ea7SMauro Carvalho Chehab
76*89e33ea7SMauro Carvalho Chehab
77*89e33ea7SMauro Carvalho Chehab  - Only the (un)patching callbacks from the _new_ cumulative livepatch are
78*89e33ea7SMauro Carvalho Chehab    executed. Any callbacks from the replaced patches are ignored.
79*89e33ea7SMauro Carvalho Chehab
80*89e33ea7SMauro Carvalho Chehab    In other words, the cumulative patch is responsible for doing any actions
81*89e33ea7SMauro Carvalho Chehab    that are necessary to properly replace any older patch.
82*89e33ea7SMauro Carvalho Chehab
83*89e33ea7SMauro Carvalho Chehab    As a result, it might be dangerous to replace newer cumulative patches by
84*89e33ea7SMauro Carvalho Chehab    older ones. The old livepatches might not provide the necessary callbacks.
85*89e33ea7SMauro Carvalho Chehab
86*89e33ea7SMauro Carvalho Chehab    This might be seen as a limitation in some scenarios. But it makes life
87*89e33ea7SMauro Carvalho Chehab    easier in many others. Only the new cumulative livepatch knows what
88*89e33ea7SMauro Carvalho Chehab    fixes/features are added/removed and what special actions are necessary
89*89e33ea7SMauro Carvalho Chehab    for a smooth transition.
90*89e33ea7SMauro Carvalho Chehab
91*89e33ea7SMauro Carvalho Chehab    In any case, it would be a nightmare to think about the order of
92*89e33ea7SMauro Carvalho Chehab    the various callbacks and their interactions if the callbacks from all
93*89e33ea7SMauro Carvalho Chehab    enabled patches were called.
94*89e33ea7SMauro Carvalho Chehab
95*89e33ea7SMauro Carvalho Chehab
96*89e33ea7SMauro Carvalho Chehab  - There is no special handling of shadow variables. Livepatch authors
97*89e33ea7SMauro Carvalho Chehab    must create their own rules how to pass them from one cumulative
98*89e33ea7SMauro Carvalho Chehab    patch to the other. Especially that they should not blindly remove
99*89e33ea7SMauro Carvalho Chehab    them in module_exit() functions.
100*89e33ea7SMauro Carvalho Chehab
101*89e33ea7SMauro Carvalho Chehab    A good practice might be to remove shadow variables in the post-unpatch
102*89e33ea7SMauro Carvalho Chehab    callback. It is called only when the livepatch is properly disabled.
103