189e33ea7SMauro Carvalho Chehab===================================
289e33ea7SMauro Carvalho ChehabAtomic Replace & Cumulative Patches
389e33ea7SMauro Carvalho Chehab===================================
489e33ea7SMauro Carvalho Chehab
589e33ea7SMauro Carvalho ChehabThere might be dependencies between livepatches. If multiple patches need
689e33ea7SMauro Carvalho Chehabto do different changes to the same function(s) then we need to define
789e33ea7SMauro Carvalho Chehaban order in which the patches will be installed. And function implementations
889e33ea7SMauro Carvalho Chehabfrom any newer livepatch must be done on top of the older ones.
989e33ea7SMauro Carvalho Chehab
1089e33ea7SMauro Carvalho ChehabThis might become a maintenance nightmare. Especially when more patches
1189e33ea7SMauro Carvalho Chehabmodified the same function in different ways.
1289e33ea7SMauro Carvalho Chehab
1389e33ea7SMauro Carvalho ChehabAn elegant solution comes with the feature called "Atomic Replace". It allows
1489e33ea7SMauro Carvalho Chehabcreation of so called "Cumulative Patches". They include all wanted changes
1589e33ea7SMauro Carvalho Chehabfrom all older livepatches and completely replace them in one transition.
1689e33ea7SMauro Carvalho Chehab
1789e33ea7SMauro Carvalho ChehabUsage
1889e33ea7SMauro Carvalho Chehab-----
1989e33ea7SMauro Carvalho Chehab
2089e33ea7SMauro Carvalho ChehabThe atomic replace can be enabled by setting "replace" flag in struct klp_patch,
2189e33ea7SMauro Carvalho Chehabfor example::
2289e33ea7SMauro Carvalho Chehab
2389e33ea7SMauro Carvalho Chehab	static struct klp_patch patch = {
2489e33ea7SMauro Carvalho Chehab		.mod = THIS_MODULE,
2589e33ea7SMauro Carvalho Chehab		.objs = objs,
2689e33ea7SMauro Carvalho Chehab		.replace = true,
2789e33ea7SMauro Carvalho Chehab	};
2889e33ea7SMauro Carvalho Chehab
2989e33ea7SMauro Carvalho ChehabAll processes are then migrated to use the code only from the new patch.
3089e33ea7SMauro Carvalho ChehabOnce the transition is finished, all older patches are automatically
3189e33ea7SMauro Carvalho Chehabdisabled.
3289e33ea7SMauro Carvalho Chehab
3389e33ea7SMauro Carvalho ChehabFtrace handlers are transparently removed from functions that are no
3489e33ea7SMauro Carvalho Chehablonger modified by the new cumulative patch.
3589e33ea7SMauro Carvalho Chehab
3689e33ea7SMauro Carvalho ChehabAs a result, the livepatch authors might maintain sources only for one
3789e33ea7SMauro Carvalho Chehabcumulative patch. It helps to keep the patch consistent while adding or
3889e33ea7SMauro Carvalho Chehabremoving various fixes or features.
3989e33ea7SMauro Carvalho Chehab
4089e33ea7SMauro Carvalho ChehabUsers could keep only the last patch installed on the system after
4189e33ea7SMauro Carvalho Chehabthe transition to has finished. It helps to clearly see what code is
4289e33ea7SMauro Carvalho Chehabactually in use. Also the livepatch might then be seen as a "normal"
4389e33ea7SMauro Carvalho Chehabmodule that modifies the kernel behavior. The only difference is that
4489e33ea7SMauro Carvalho Chehabit can be updated at runtime without breaking its functionality.
4589e33ea7SMauro Carvalho Chehab
4689e33ea7SMauro Carvalho Chehab
4789e33ea7SMauro Carvalho ChehabFeatures
4889e33ea7SMauro Carvalho Chehab--------
4989e33ea7SMauro Carvalho Chehab
5089e33ea7SMauro Carvalho ChehabThe atomic replace allows:
5189e33ea7SMauro Carvalho Chehab
5289e33ea7SMauro Carvalho Chehab  - Atomically revert some functions in a previous patch while
5389e33ea7SMauro Carvalho Chehab    upgrading other functions.
5489e33ea7SMauro Carvalho Chehab
5589e33ea7SMauro Carvalho Chehab  - Remove eventual performance impact caused by core redirection
5689e33ea7SMauro Carvalho Chehab    for functions that are no longer patched.
5789e33ea7SMauro Carvalho Chehab
5889e33ea7SMauro Carvalho Chehab  - Decrease user confusion about dependencies between livepatches.
5989e33ea7SMauro Carvalho Chehab
6089e33ea7SMauro Carvalho Chehab
6189e33ea7SMauro Carvalho ChehabLimitations:
6289e33ea7SMauro Carvalho Chehab------------
6389e33ea7SMauro Carvalho Chehab
6489e33ea7SMauro Carvalho Chehab  - Once the operation finishes, there is no straightforward way
6589e33ea7SMauro Carvalho Chehab    to reverse it and restore the replaced patches atomically.
6689e33ea7SMauro Carvalho Chehab
6789e33ea7SMauro Carvalho Chehab    A good practice is to set .replace flag in any released livepatch.
6889e33ea7SMauro Carvalho Chehab    Then re-adding an older livepatch is equivalent to downgrading
6989e33ea7SMauro Carvalho Chehab    to that patch. This is safe as long as the livepatches do _not_ do
7089e33ea7SMauro Carvalho Chehab    extra modifications in (un)patching callbacks or in the module_init()
7189e33ea7SMauro Carvalho Chehab    or module_exit() functions, see below.
7289e33ea7SMauro Carvalho Chehab
7389e33ea7SMauro Carvalho Chehab    Also note that the replaced patch can be removed and loaded again
7489e33ea7SMauro Carvalho Chehab    only when the transition was not forced.
7589e33ea7SMauro Carvalho Chehab
7689e33ea7SMauro Carvalho Chehab
7789e33ea7SMauro Carvalho Chehab  - Only the (un)patching callbacks from the _new_ cumulative livepatch are
7889e33ea7SMauro Carvalho Chehab    executed. Any callbacks from the replaced patches are ignored.
7989e33ea7SMauro Carvalho Chehab
8089e33ea7SMauro Carvalho Chehab    In other words, the cumulative patch is responsible for doing any actions
8189e33ea7SMauro Carvalho Chehab    that are necessary to properly replace any older patch.
8289e33ea7SMauro Carvalho Chehab
8389e33ea7SMauro Carvalho Chehab    As a result, it might be dangerous to replace newer cumulative patches by
8489e33ea7SMauro Carvalho Chehab    older ones. The old livepatches might not provide the necessary callbacks.
8589e33ea7SMauro Carvalho Chehab
8689e33ea7SMauro Carvalho Chehab    This might be seen as a limitation in some scenarios. But it makes life
8789e33ea7SMauro Carvalho Chehab    easier in many others. Only the new cumulative livepatch knows what
8889e33ea7SMauro Carvalho Chehab    fixes/features are added/removed and what special actions are necessary
8989e33ea7SMauro Carvalho Chehab    for a smooth transition.
9089e33ea7SMauro Carvalho Chehab
9189e33ea7SMauro Carvalho Chehab    In any case, it would be a nightmare to think about the order of
9289e33ea7SMauro Carvalho Chehab    the various callbacks and their interactions if the callbacks from all
9389e33ea7SMauro Carvalho Chehab    enabled patches were called.
9489e33ea7SMauro Carvalho Chehab
9589e33ea7SMauro Carvalho Chehab
9689e33ea7SMauro Carvalho Chehab  - There is no special handling of shadow variables. Livepatch authors
9789e33ea7SMauro Carvalho Chehab    must create their own rules how to pass them from one cumulative
9889e33ea7SMauro Carvalho Chehab    patch to the other. Especially that they should not blindly remove
9989e33ea7SMauro Carvalho Chehab    them in module_exit() functions.
10089e33ea7SMauro Carvalho Chehab
10189e33ea7SMauro Carvalho Chehab    A good practice might be to remove shadow variables in the post-unpatch
10289e33ea7SMauro Carvalho Chehab    callback. It is called only when the livepatch is properly disabled.
103