xref: /OK3568_Linux_fs/kernel/Documentation/livepatch/callbacks.rst (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun======================
2*4882a593Smuzhiyun(Un)patching Callbacks
3*4882a593Smuzhiyun======================
4*4882a593Smuzhiyun
5*4882a593SmuzhiyunLivepatch (un)patch-callbacks provide a mechanism for livepatch modules
6*4882a593Smuzhiyunto execute callback functions when a kernel object is (un)patched.  They
7*4882a593Smuzhiyuncan be considered a **power feature** that **extends livepatching abilities**
8*4882a593Smuzhiyunto include:
9*4882a593Smuzhiyun
10*4882a593Smuzhiyun  - Safe updates to global data
11*4882a593Smuzhiyun
12*4882a593Smuzhiyun  - "Patches" to init and probe functions
13*4882a593Smuzhiyun
14*4882a593Smuzhiyun  - Patching otherwise unpatchable code (i.e. assembly)
15*4882a593Smuzhiyun
16*4882a593SmuzhiyunIn most cases, (un)patch callbacks will need to be used in conjunction
17*4882a593Smuzhiyunwith memory barriers and kernel synchronization primitives, like
18*4882a593Smuzhiyunmutexes/spinlocks, or even stop_machine(), to avoid concurrency issues.
19*4882a593Smuzhiyun
20*4882a593Smuzhiyun1. Motivation
21*4882a593Smuzhiyun=============
22*4882a593Smuzhiyun
23*4882a593SmuzhiyunCallbacks differ from existing kernel facilities:
24*4882a593Smuzhiyun
25*4882a593Smuzhiyun  - Module init/exit code doesn't run when disabling and re-enabling a
26*4882a593Smuzhiyun    patch.
27*4882a593Smuzhiyun
28*4882a593Smuzhiyun  - A module notifier can't stop a to-be-patched module from loading.
29*4882a593Smuzhiyun
30*4882a593SmuzhiyunCallbacks are part of the klp_object structure and their implementation
31*4882a593Smuzhiyunis specific to that klp_object.  Other livepatch objects may or may not
32*4882a593Smuzhiyunbe patched, irrespective of the target klp_object's current state.
33*4882a593Smuzhiyun
34*4882a593Smuzhiyun2. Callback types
35*4882a593Smuzhiyun=================
36*4882a593Smuzhiyun
37*4882a593SmuzhiyunCallbacks can be registered for the following livepatch actions:
38*4882a593Smuzhiyun
39*4882a593Smuzhiyun  * Pre-patch
40*4882a593Smuzhiyun                 - before a klp_object is patched
41*4882a593Smuzhiyun
42*4882a593Smuzhiyun  * Post-patch
43*4882a593Smuzhiyun                 - after a klp_object has been patched and is active
44*4882a593Smuzhiyun                   across all tasks
45*4882a593Smuzhiyun
46*4882a593Smuzhiyun  * Pre-unpatch
47*4882a593Smuzhiyun                 - before a klp_object is unpatched (ie, patched code is
48*4882a593Smuzhiyun                   active), used to clean up post-patch callback
49*4882a593Smuzhiyun                   resources
50*4882a593Smuzhiyun
51*4882a593Smuzhiyun  * Post-unpatch
52*4882a593Smuzhiyun                 - after a klp_object has been patched, all code has
53*4882a593Smuzhiyun                   been restored and no tasks are running patched code,
54*4882a593Smuzhiyun                   used to cleanup pre-patch callback resources
55*4882a593Smuzhiyun
56*4882a593Smuzhiyun3. How it works
57*4882a593Smuzhiyun===============
58*4882a593Smuzhiyun
59*4882a593SmuzhiyunEach callback is optional, omitting one does not preclude specifying any
60*4882a593Smuzhiyunother.  However, the livepatching core executes the handlers in
61*4882a593Smuzhiyunsymmetry: pre-patch callbacks have a post-unpatch counterpart and
62*4882a593Smuzhiyunpost-patch callbacks have a pre-unpatch counterpart.  An unpatch
63*4882a593Smuzhiyuncallback will only be executed if its corresponding patch callback was
64*4882a593Smuzhiyunexecuted.  Typical use cases pair a patch handler that acquires and
65*4882a593Smuzhiyunconfigures resources with an unpatch handler tears down and releases
66*4882a593Smuzhiyunthose same resources.
67*4882a593Smuzhiyun
68*4882a593SmuzhiyunA callback is only executed if its host klp_object is loaded.  For
69*4882a593Smuzhiyunin-kernel vmlinux targets, this means that callbacks will always execute
70*4882a593Smuzhiyunwhen a livepatch is enabled/disabled.  For patch target kernel modules,
71*4882a593Smuzhiyuncallbacks will only execute if the target module is loaded.  When a
72*4882a593Smuzhiyunmodule target is (un)loaded, its callbacks will execute only if the
73*4882a593Smuzhiyunlivepatch module is enabled.
74*4882a593Smuzhiyun
75*4882a593SmuzhiyunThe pre-patch callback, if specified, is expected to return a status
76*4882a593Smuzhiyuncode (0 for success, -ERRNO on error).  An error status code indicates
77*4882a593Smuzhiyunto the livepatching core that patching of the current klp_object is not
78*4882a593Smuzhiyunsafe and to stop the current patching request.  (When no pre-patch
79*4882a593Smuzhiyuncallback is provided, the transition is assumed to be safe.)  If a
80*4882a593Smuzhiyunpre-patch callback returns failure, the kernel's module loader will:
81*4882a593Smuzhiyun
82*4882a593Smuzhiyun  - Refuse to load a livepatch, if the livepatch is loaded after
83*4882a593Smuzhiyun    targeted code.
84*4882a593Smuzhiyun
85*4882a593Smuzhiyun    or:
86*4882a593Smuzhiyun
87*4882a593Smuzhiyun  - Refuse to load a module, if the livepatch was already successfully
88*4882a593Smuzhiyun    loaded.
89*4882a593Smuzhiyun
90*4882a593SmuzhiyunNo post-patch, pre-unpatch, or post-unpatch callbacks will be executed
91*4882a593Smuzhiyunfor a given klp_object if the object failed to patch, due to a failed
92*4882a593Smuzhiyunpre_patch callback or for any other reason.
93*4882a593Smuzhiyun
94*4882a593SmuzhiyunIf a patch transition is reversed, no pre-unpatch handlers will be run
95*4882a593Smuzhiyun(this follows the previously mentioned symmetry -- pre-unpatch callbacks
96*4882a593Smuzhiyunwill only occur if their corresponding post-patch callback executed).
97*4882a593Smuzhiyun
98*4882a593SmuzhiyunIf the object did successfully patch, but the patch transition never
99*4882a593Smuzhiyunstarted for some reason (e.g., if another object failed to patch),
100*4882a593Smuzhiyunonly the post-unpatch callback will be called.
101*4882a593Smuzhiyun
102*4882a593Smuzhiyun4. Use cases
103*4882a593Smuzhiyun============
104*4882a593Smuzhiyun
105*4882a593SmuzhiyunSample livepatch modules demonstrating the callback API can be found in
106*4882a593Smuzhiyunsamples/livepatch/ directory.  These samples were modified for use in
107*4882a593Smuzhiyunkselftests and can be found in the lib/livepatch directory.
108*4882a593Smuzhiyun
109*4882a593SmuzhiyunGlobal data update
110*4882a593Smuzhiyun------------------
111*4882a593Smuzhiyun
112*4882a593SmuzhiyunA pre-patch callback can be useful to update a global variable.  For
113*4882a593Smuzhiyunexample, 75ff39ccc1bd ("tcp: make challenge acks less predictable")
114*4882a593Smuzhiyunchanges a global sysctl, as well as patches the tcp_send_challenge_ack()
115*4882a593Smuzhiyunfunction.
116*4882a593Smuzhiyun
117*4882a593SmuzhiyunIn this case, if we're being super paranoid, it might make sense to
118*4882a593Smuzhiyunpatch the data *after* patching is complete with a post-patch callback,
119*4882a593Smuzhiyunso that tcp_send_challenge_ack() could first be changed to read
120*4882a593Smuzhiyunsysctl_tcp_challenge_ack_limit with READ_ONCE.
121*4882a593Smuzhiyun
122*4882a593Smuzhiyun__init and probe function patches support
123*4882a593Smuzhiyun-----------------------------------------
124*4882a593Smuzhiyun
125*4882a593SmuzhiyunAlthough __init and probe functions are not directly livepatch-able, it
126*4882a593Smuzhiyunmay be possible to implement similar updates via pre/post-patch
127*4882a593Smuzhiyuncallbacks.
128*4882a593Smuzhiyun
129*4882a593SmuzhiyunThe commit ``48900cb6af42 ("virtio-net: drop NETIF_F_FRAGLIST")`` change the way that
130*4882a593Smuzhiyunvirtnet_probe() initialized its driver's net_device features.  A
131*4882a593Smuzhiyunpre/post-patch callback could iterate over all such devices, making a
132*4882a593Smuzhiyunsimilar change to their hw_features value.  (Client functions of the
133*4882a593Smuzhiyunvalue may need to be updated accordingly.)
134