]> git.proxmox.com Git - mirror_ubuntu-hirsute-kernel.git/blame - Documentation/livepatch/callbacks.txt
scripts/documentation-file-ref-check: detect broken :doc:`foo`
[mirror_ubuntu-hirsute-kernel.git] / Documentation / livepatch / callbacks.txt
CommitLineData
93862e38
JL
1======================
2(Un)patching Callbacks
3======================
4
5Livepatch (un)patch-callbacks provide a mechanism for livepatch modules
6to execute callback functions when a kernel object is (un)patched. They
7can be considered a "power feature" that extends livepatching abilities
8to include:
9
10 - Safe updates to global data
11
12 - "Patches" to init and probe functions
13
14 - Patching otherwise unpatchable code (i.e. assembly)
15
16In most cases, (un)patch callbacks will need to be used in conjunction
17with memory barriers and kernel synchronization primitives, like
18mutexes/spinlocks, or even stop_machine(), to avoid concurrency issues.
19
20Callbacks differ from existing kernel facilities:
21
22 - Module init/exit code doesn't run when disabling and re-enabling a
23 patch.
24
25 - A module notifier can't stop a to-be-patched module from loading.
26
27Callbacks are part of the klp_object structure and their implementation
28is specific to that klp_object. Other livepatch objects may or may not
29be patched, irrespective of the target klp_object's current state.
30
31Callbacks can be registered for the following livepatch actions:
32
33 * Pre-patch - before a klp_object is patched
34
35 * Post-patch - after a klp_object has been patched and is active
36 across all tasks
37
38 * Pre-unpatch - before a klp_object is unpatched (ie, patched code is
39 active), used to clean up post-patch callback
40 resources
41
42 * Post-unpatch - after a klp_object has been patched, all code has
43 been restored and no tasks are running patched code,
44 used to cleanup pre-patch callback resources
45
46Each callback is optional, omitting one does not preclude specifying any
47other. However, the livepatching core executes the handlers in
48symmetry: pre-patch callbacks have a post-unpatch counterpart and
49post-patch callbacks have a pre-unpatch counterpart. An unpatch
50callback will only be executed if its corresponding patch callback was
51executed. Typical use cases pair a patch handler that acquires and
52configures resources with an unpatch handler tears down and releases
53those same resources.
54
55A callback is only executed if its host klp_object is loaded. For
56in-kernel vmlinux targets, this means that callbacks will always execute
57when a livepatch is enabled/disabled. For patch target kernel modules,
58callbacks will only execute if the target module is loaded. When a
59module target is (un)loaded, its callbacks will execute only if the
60livepatch module is enabled.
61
62The pre-patch callback, if specified, is expected to return a status
63code (0 for success, -ERRNO on error). An error status code indicates
64to the livepatching core that patching of the current klp_object is not
65safe and to stop the current patching request. (When no pre-patch
66callback is provided, the transition is assumed to be safe.) If a
67pre-patch callback returns failure, the kernel's module loader will:
68
69 - Refuse to load a livepatch, if the livepatch is loaded after
70 targeted code.
71
72 or:
73
74 - Refuse to load a module, if the livepatch was already successfully
75 loaded.
76
77No post-patch, pre-unpatch, or post-unpatch callbacks will be executed
78for a given klp_object if the object failed to patch, due to a failed
79pre_patch callback or for any other reason.
80
81If a patch transition is reversed, no pre-unpatch handlers will be run
82(this follows the previously mentioned symmetry -- pre-unpatch callbacks
83will only occur if their corresponding post-patch callback executed).
84
85If the object did successfully patch, but the patch transition never
86started for some reason (e.g., if another object failed to patch),
87only the post-unpatch callback will be called.
88
89
90Example Use-cases
91=================
92
93Update global data
94------------------
95
96A pre-patch callback can be useful to update a global variable. For
97example, 75ff39ccc1bd ("tcp: make challenge acks less predictable")
98changes a global sysctl, as well as patches the tcp_send_challenge_ack()
99function.
100
101In this case, if we're being super paranoid, it might make sense to
102patch the data *after* patching is complete with a post-patch callback,
103so that tcp_send_challenge_ack() could first be changed to read
104sysctl_tcp_challenge_ack_limit with READ_ONCE.
105
106
107Support __init and probe function patches
108-----------------------------------------
109
110Although __init and probe functions are not directly livepatch-able, it
111may be possible to implement similar updates via pre/post-patch
112callbacks.
113
11448900cb6af42 ("virtio-net: drop NETIF_F_FRAGLIST") change the way that
115virtnet_probe() initialized its driver's net_device features. A
116pre/post-patch callback could iterate over all such devices, making a
117similar change to their hw_features value. (Client functions of the
118value may need to be updated accordingly.)
119
120
a2818ee4
JL
121Other Examples
122==============
93862e38 123
a2818ee4
JL
124Sample livepatch modules demonstrating the callback API can be found in
125samples/livepatch/ directory. These samples were modified for use in
126kselftests and can be found in the lib/livepatch directory.