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