summaryrefslogtreecommitdiffstats
path: root/Documentation/livepatch
diff options
context:
space:
mode:
authorPetr Mladek <pmladek@suse.com>2019-05-03 10:30:24 -0400
committerJonathan Corbet <corbet@lwn.net>2019-05-07 18:06:28 -0400
commitd9defe448f4c7b88ca2ae636a321ef8970fa718d (patch)
tree7de5bc4dd12f5d1544f1985f96c3717a2be22ee9 /Documentation/livepatch
parent89e33ea73295327f22fd1594f97cc70a5381b74a (diff)
docs/livepatch: Unify style of livepatch documentation in the ReST format
Make the structure of "Livepatch module Elf format" document similar to the main "Livepatch" document. Also make the structure of "(Un)patching Callbacks" document similar to the "Shadow Variables" document. It fixes the most visible inconsistencies of the documentation generated from the ReST format. Signed-off-by: Petr Mladek <pmladek@suse.com> Acked-by: Joe Lawrence <joe.lawrence@redhat.com> Acked-by: Josh Poimboeuf <jpoimboe@redhat.com> Acked-by: Miroslav Benes <mbenes@suse.cz> Reviewed-by: Mauro Carvalho Chehab <mchehab+samsung@kernel.org> Reviewed-by: Kamalesh Babulal <kamalesh@linux.vnet.ibm.com> Signed-off-by: Jonathan Corbet <corbet@lwn.net>
Diffstat (limited to 'Documentation/livepatch')
-rw-r--r--Documentation/livepatch/callbacks.rst33
-rw-r--r--Documentation/livepatch/module-elf-format.rst186
2 files changed, 104 insertions, 115 deletions
diff --git a/Documentation/livepatch/callbacks.rst b/Documentation/livepatch/callbacks.rst
index d76d1f0d9fcf..470944aa8658 100644
--- a/Documentation/livepatch/callbacks.rst
+++ b/Documentation/livepatch/callbacks.rst
@@ -4,7 +4,7 @@
4 4
5Livepatch (un)patch-callbacks provide a mechanism for livepatch modules 5Livepatch (un)patch-callbacks provide a mechanism for livepatch modules
6to execute callback functions when a kernel object is (un)patched. They 6to execute callback functions when a kernel object is (un)patched. They
7can be considered a "power feature" that extends livepatching abilities 7can be considered a **power feature** that **extends livepatching abilities**
8to include: 8to include:
9 9
10 - Safe updates to global data 10 - Safe updates to global data
@@ -17,6 +17,9 @@ In most cases, (un)patch callbacks will need to be used in conjunction
17with memory barriers and kernel synchronization primitives, like 17with memory barriers and kernel synchronization primitives, like
18mutexes/spinlocks, or even stop_machine(), to avoid concurrency issues. 18mutexes/spinlocks, or even stop_machine(), to avoid concurrency issues.
19 19
201. Motivation
21=============
22
20Callbacks differ from existing kernel facilities: 23Callbacks differ from existing kernel facilities:
21 24
22 - Module init/exit code doesn't run when disabling and re-enabling a 25 - Module init/exit code doesn't run when disabling and re-enabling a
@@ -28,6 +31,9 @@ Callbacks are part of the klp_object structure and their implementation
28is specific to that klp_object. Other livepatch objects may or may not 31is specific to that klp_object. Other livepatch objects may or may not
29be patched, irrespective of the target klp_object's current state. 32be patched, irrespective of the target klp_object's current state.
30 33
342. Callback types
35=================
36
31Callbacks can be registered for the following livepatch actions: 37Callbacks can be registered for the following livepatch actions:
32 38
33 * Pre-patch 39 * Pre-patch
@@ -47,6 +53,9 @@ Callbacks can be registered for the following livepatch actions:
47 been restored and no tasks are running patched code, 53 been restored and no tasks are running patched code,
48 used to cleanup pre-patch callback resources 54 used to cleanup pre-patch callback resources
49 55
563. How it works
57===============
58
50Each callback is optional, omitting one does not preclude specifying any 59Each callback is optional, omitting one does not preclude specifying any
51other. However, the livepatching core executes the handlers in 60other. However, the livepatching core executes the handlers in
52symmetry: pre-patch callbacks have a post-unpatch counterpart and 61symmetry: pre-patch callbacks have a post-unpatch counterpart and
@@ -90,11 +99,14 @@ If the object did successfully patch, but the patch transition never
90started for some reason (e.g., if another object failed to patch), 99started for some reason (e.g., if another object failed to patch),
91only the post-unpatch callback will be called. 100only the post-unpatch callback will be called.
92 101
1024. Use cases
103============
93 104
94Example Use-cases 105Sample livepatch modules demonstrating the callback API can be found in
95================= 106samples/livepatch/ directory. These samples were modified for use in
107kselftests and can be found in the lib/livepatch directory.
96 108
97Update global data 109Global data update
98------------------ 110------------------
99 111
100A pre-patch callback can be useful to update a global variable. For 112A pre-patch callback can be useful to update a global variable. For
@@ -107,24 +119,15 @@ patch the data *after* patching is complete with a post-patch callback,
107so that tcp_send_challenge_ack() could first be changed to read 119so that tcp_send_challenge_ack() could first be changed to read
108sysctl_tcp_challenge_ack_limit with READ_ONCE. 120sysctl_tcp_challenge_ack_limit with READ_ONCE.
109 121
110 122__init and probe function patches support
111Support __init and probe function patches
112----------------------------------------- 123-----------------------------------------
113 124
114Although __init and probe functions are not directly livepatch-able, it 125Although __init and probe functions are not directly livepatch-able, it
115may be possible to implement similar updates via pre/post-patch 126may be possible to implement similar updates via pre/post-patch
116callbacks. 127callbacks.
117 128
11848900cb6af42 ("virtio-net: drop NETIF_F_FRAGLIST") change the way that 129The commit ``48900cb6af42 ("virtio-net: drop NETIF_F_FRAGLIST")`` change the way that
119virtnet_probe() initialized its driver's net_device features. A 130virtnet_probe() initialized its driver's net_device features. A
120pre/post-patch callback could iterate over all such devices, making a 131pre/post-patch callback could iterate over all such devices, making a
121similar change to their hw_features value. (Client functions of the 132similar change to their hw_features value. (Client functions of the
122value may need to be updated accordingly.) 133value may need to be updated accordingly.)
123
124
125Other Examples
126==============
127
128Sample livepatch modules demonstrating the callback API can be found in
129samples/livepatch/ directory. These samples were modified for use in
130kselftests and can be found in the lib/livepatch directory.
diff --git a/Documentation/livepatch/module-elf-format.rst b/Documentation/livepatch/module-elf-format.rst
index 7f557c6f6deb..2a591e6f8e6c 100644
--- a/Documentation/livepatch/module-elf-format.rst
+++ b/Documentation/livepatch/module-elf-format.rst
@@ -7,30 +7,18 @@ This document outlines the Elf format requirements that livepatch modules must f
7 7
8.. Table of Contents 8.. Table of Contents
9 9
10 0. Background and motivation 10 1. Background and motivation
11 1. Livepatch modinfo field 11 2. Livepatch modinfo field
12 2. Livepatch relocation sections 12 3. Livepatch relocation sections
13 2.1 What are livepatch relocation sections? 13 3.1 Livepatch relocation section format
14 2.2 Livepatch relocation section format 14 4. Livepatch symbols
15 2.2.1 Required flags 15 4.1 A livepatch module's symbol table
16 2.2.2 Required name format 16 4.2 Livepatch symbol format
17 2.2.3 Example livepatch relocation section names 17 5. Architecture-specific sections
18 2.2.4 Example `readelf --sections` output 18 6. Symbol table and Elf section access
19 2.2.5 Example `readelf --relocs` output 19
20 3. Livepatch symbols 201. Background and motivation
21 3.1 What are livepatch symbols? 21============================
22 3.2 A livepatch module's symbol table
23 3.3 Livepatch symbol format
24 3.3.1 Required flags
25 3.3.2 Required name format
26 3.3.3 Example livepatch symbol names
27 3.3.4 Example `readelf --symbols` output
28 4. Architecture-specific sections
29 5. Symbol table and Elf section access
30
31----------------------------
320. Background and motivation
33----------------------------
34 22
35Formerly, livepatch required separate architecture-specific code to write 23Formerly, livepatch required separate architecture-specific code to write
36relocations. However, arch-specific code to write relocations already 24relocations. However, arch-specific code to write relocations already
@@ -52,8 +40,8 @@ relocation sections and symbols, which are described in this document. The
52Elf constants used to mark livepatch symbols and relocation sections were 40Elf constants used to mark livepatch symbols and relocation sections were
53selected from OS-specific ranges according to the definitions from glibc. 41selected from OS-specific ranges according to the definitions from glibc.
54 42
550.1 Why does livepatch need to write its own relocations? 43Why does livepatch need to write its own relocations?
56--------------------------------------------------------- 44-----------------------------------------------------
57A typical livepatch module contains patched versions of functions that can 45A typical livepatch module contains patched versions of functions that can
58reference non-exported global symbols and non-included local symbols. 46reference non-exported global symbols and non-included local symbols.
59Relocations referencing these types of symbols cannot be left in as-is 47Relocations referencing these types of symbols cannot be left in as-is
@@ -72,13 +60,8 @@ relas reference are special livepatch symbols (see section 2 and 3). The
72arch-specific livepatch relocation code is replaced by a call to 60arch-specific livepatch relocation code is replaced by a call to
73apply_relocate_add(). 61apply_relocate_add().
74 62
75================================ 632. Livepatch modinfo field
76PATCH MODULE FORMAT REQUIREMENTS 64==========================
77================================
78
79--------------------------
801. Livepatch modinfo field
81--------------------------
82 65
83Livepatch modules are required to have the "livepatch" modinfo attribute. 66Livepatch modules are required to have the "livepatch" modinfo attribute.
84See the sample livepatch module in samples/livepatch/ for how this is done. 67See the sample livepatch module in samples/livepatch/ for how this is done.
@@ -87,8 +70,10 @@ Livepatch modules can be identified by users by using the 'modinfo' command
87and looking for the presence of the "livepatch" field. This field is also 70and looking for the presence of the "livepatch" field. This field is also
88used by the kernel module loader to identify livepatch modules. 71used by the kernel module loader to identify livepatch modules.
89 72
90Example modinfo output: 73Example:
91----------------------- 74--------
75
76**Modinfo output:**
92 77
93:: 78::
94 79
@@ -99,13 +84,9 @@ Example modinfo output:
99 depends: 84 depends:
100 vermagic: 4.3.0+ SMP mod_unload 85 vermagic: 4.3.0+ SMP mod_unload
101 86
102-------------------------------- 873. Livepatch relocation sections
1032. Livepatch relocation sections 88================================
104--------------------------------
105 89
106-------------------------------------------
1072.1 What are livepatch relocation sections?
108-------------------------------------------
109A livepatch module manages its own Elf relocation sections to apply 90A livepatch module manages its own Elf relocation sections to apply
110relocations to modules as well as to the kernel (vmlinux) at the 91relocations to modules as well as to the kernel (vmlinux) at the
111appropriate time. For example, if a patch module patches a driver that is 92appropriate time. For example, if a patch module patches a driver that is
@@ -130,12 +111,9 @@ Every symbol referenced by a rela in a livepatch relocation section is a
130livepatch symbol. These must be resolved before livepatch can call 111livepatch symbol. These must be resolved before livepatch can call
131apply_relocate_add(). See Section 3 for more information. 112apply_relocate_add(). See Section 3 for more information.
132 113
133--------------------------------------- 1143.1 Livepatch relocation section format
1342.2 Livepatch relocation section format 115=======================================
135---------------------------------------
136 116
1372.2.1 Required flags
138--------------------
139Livepatch relocation sections must be marked with the SHF_RELA_LIVEPATCH 117Livepatch relocation sections must be marked with the SHF_RELA_LIVEPATCH
140section flag. See include/uapi/linux/elf.h for the definition. The module 118section flag. See include/uapi/linux/elf.h for the definition. The module
141loader recognizes this flag and will avoid applying those relocation sections 119loader recognizes this flag and will avoid applying those relocation sections
@@ -143,8 +121,6 @@ at patch module load time. These sections must also be marked with SHF_ALLOC,
143so that the module loader doesn't discard them on module load (i.e. they will 121so that the module loader doesn't discard them on module load (i.e. they will
144be copied into memory along with the other SHF_ALLOC sections). 122be copied into memory along with the other SHF_ALLOC sections).
145 123
1462.2.2 Required name format
147--------------------------
148The name of a livepatch relocation section must conform to the following 124The name of a livepatch relocation section must conform to the following
149format:: 125format::
150 126
@@ -153,19 +129,28 @@ format::
153 |________||_____| |__________| 129 |________||_____| |__________|
154 [A] [B] [C] 130 [A] [B] [C]
155 131
156 [A] The relocation section name is prefixed with the string ".klp.rela." 132[A]
157 [B] The name of the object (i.e. "vmlinux" or name of module) to 133 The relocation section name is prefixed with the string ".klp.rela."
158 which the relocation section belongs follows immediately after the prefix.
159 [C] The actual name of the section to which this relocation section applies.
160 134
1612.2.3 Example livepatch relocation section names: 135[B]
162------------------------------------------------- 136 The name of the object (i.e. "vmlinux" or name of module) to
163.klp.rela.ext4.text.ext4_attr_store 137 which the relocation section belongs follows immediately after the prefix.
164.klp.rela.vmlinux.text.cmdline_proc_show
165 138
1662.2.4 Example `readelf --sections` output for a patch 139[C]
167module that patches vmlinux and modules 9p, btrfs, ext4: 140 The actual name of the section to which this relocation section applies.
168-------------------------------------------------------- 141
142Examples:
143---------
144
145**Livepatch relocation section names:**
146
147::
148
149 .klp.rela.ext4.text.ext4_attr_store
150 .klp.rela.vmlinux.text.cmdline_proc_show
151
152**`readelf --sections` output for a patch
153module that patches vmlinux and modules 9p, btrfs, ext4:**
169 154
170:: 155::
171 156
@@ -182,13 +167,14 @@ module that patches vmlinux and modules 9p, btrfs, ext4:
182 [ snip ] ^ ^ 167 [ snip ] ^ ^
183 | | 168 | |
184 [*] [*] 169 [*] [*]
185 [*] Livepatch relocation sections are SHT_RELA sections but with a few special 170
171[*]
172 Livepatch relocation sections are SHT_RELA sections but with a few special
186 characteristics. Notice that they are marked SHF_ALLOC ("A") so that they will 173 characteristics. Notice that they are marked SHF_ALLOC ("A") so that they will
187 not be discarded when the module is loaded into memory, as well as with the 174 not be discarded when the module is loaded into memory, as well as with the
188 SHF_RELA_LIVEPATCH flag ("o" - for OS-specific). 175 SHF_RELA_LIVEPATCH flag ("o" - for OS-specific).
189 176
1902.2.5 Example `readelf --relocs` output for a patch module: 177**`readelf --relocs` output for a patch module:**
191-----------------------------------------------------------
192 178
193:: 179::
194 180
@@ -200,16 +186,14 @@ module that patches vmlinux and modules 9p, btrfs, ext4:
200 000000000000004c 0000004900000002 R_X86_64_PC32 0000000000000000 .klp.sym.vmlinux.snprintf,0 - 4 186 000000000000004c 0000004900000002 R_X86_64_PC32 0000000000000000 .klp.sym.vmlinux.snprintf,0 - 4
201 [ snip ] ^ 187 [ snip ] ^
202 | 188 |
203 [*] 189 [*]
204 [*] Every symbol referenced by a relocation is a livepatch symbol. 190
191[*]
192 Every symbol referenced by a relocation is a livepatch symbol.
205 193
206-------------------- 1944. Livepatch symbols
2073. Livepatch symbols 195====================
208--------------------
209 196
210-------------------------------
2113.1 What are livepatch symbols?
212-------------------------------
213Livepatch symbols are symbols referred to by livepatch relocation sections. 197Livepatch symbols are symbols referred to by livepatch relocation sections.
214These are symbols accessed from new versions of functions for patched 198These are symbols accessed from new versions of functions for patched
215objects, whose addresses cannot be resolved by the module loader (because 199objects, whose addresses cannot be resolved by the module loader (because
@@ -229,9 +213,8 @@ loader can identify and ignore them. Livepatch modules keep these symbols
229in their symbol tables, and the symbol table is made accessible through 213in their symbol tables, and the symbol table is made accessible through
230module->symtab. 214module->symtab.
231 215
232------------------------------------- 2164.1 A livepatch module's symbol table
2333.2 A livepatch module's symbol table 217=====================================
234-------------------------------------
235Normally, a stripped down copy of a module's symbol table (containing only 218Normally, a stripped down copy of a module's symbol table (containing only
236"core" symbols) is made available through module->symtab (See layout_symtab() 219"core" symbols) is made available through module->symtab (See layout_symtab()
237in kernel/module.c). For livepatch modules, the symbol table copied into memory 220in kernel/module.c). For livepatch modules, the symbol table copied into memory
@@ -255,18 +238,13 @@ For example, take this particular rela from a livepatch module:::
255 94: 0000000000000000 0 NOTYPE GLOBAL DEFAULT OS [0xff20] .klp.sym.vmlinux.printk,0 238 94: 0000000000000000 0 NOTYPE GLOBAL DEFAULT OS [0xff20] .klp.sym.vmlinux.printk,0
256 [ snip ] 239 [ snip ]
257 240
258--------------------------- 2414.2 Livepatch symbol format
2593.3 Livepatch symbol format 242===========================
260---------------------------
261 243
2623.3.1 Required flags
263--------------------
264Livepatch symbols must have their section index marked as SHN_LIVEPATCH, so 244Livepatch symbols must have their section index marked as SHN_LIVEPATCH, so
265that the module loader can identify them and not attempt to resolve them. 245that the module loader can identify them and not attempt to resolve them.
266See include/uapi/linux/elf.h for the actual definitions. 246See include/uapi/linux/elf.h for the actual definitions.
267 247
2683.3.2 Required name format
269--------------------------
270Livepatch symbol names must conform to the following format:: 248Livepatch symbol names must conform to the following format::
271 249
272 .klp.sym.objname.symbol_name,sympos 250 .klp.sym.objname.symbol_name,sympos
@@ -274,17 +252,26 @@ Livepatch symbol names must conform to the following format::
274 |_______||_____| |_________| | 252 |_______||_____| |_________| |
275 [A] [B] [C] [D] 253 [A] [B] [C] [D]
276 254
277 [A] The symbol name is prefixed with the string ".klp.sym." 255[A]
278 [B] The name of the object (i.e. "vmlinux" or name of module) to 256 The symbol name is prefixed with the string ".klp.sym."
279 which the symbol belongs follows immediately after the prefix. 257
280 [C] The actual name of the symbol. 258[B]
281 [D] The position of the symbol in the object (as according to kallsyms) 259 The name of the object (i.e. "vmlinux" or name of module) to
282 This is used to differentiate duplicate symbols within the same 260 which the symbol belongs follows immediately after the prefix.
283 object. The symbol position is expressed numerically (0, 1, 2...).
284 The symbol position of a unique symbol is 0.
285 261
2863.3.3 Example livepatch symbol names: 262[C]
287------------------------------------- 263 The actual name of the symbol.
264
265[D]
266 The position of the symbol in the object (as according to kallsyms)
267 This is used to differentiate duplicate symbols within the same
268 object. The symbol position is expressed numerically (0, 1, 2...).
269 The symbol position of a unique symbol is 0.
270
271Examples:
272---------
273
274**Livepatch symbol names:**
288 275
289:: 276::
290 277
@@ -292,8 +279,7 @@ Livepatch symbol names must conform to the following format::
292 .klp.sym.vmlinux.printk,0 279 .klp.sym.vmlinux.printk,0
293 .klp.sym.btrfs.btrfs_ktype,0 280 .klp.sym.btrfs.btrfs_ktype,0
294 281
2953.3.4 Example `readelf --symbols` output for a patch module: 282**`readelf --symbols` output for a patch module:**
296------------------------------------------------------------
297 283
298:: 284::
299 285
@@ -307,12 +293,13 @@ Livepatch symbol names must conform to the following format::
307 [ snip ] ^ 293 [ snip ] ^
308 | 294 |
309 [*] 295 [*]
310 [*] Note that the 'Ndx' (Section index) for these symbols is SHN_LIVEPATCH (0xff20).
311 "OS" means OS-specific.
312 296
313--------------------------------- 297[*]
3144. Architecture-specific sections 298 Note that the 'Ndx' (Section index) for these symbols is SHN_LIVEPATCH (0xff20).
315--------------------------------- 299 "OS" means OS-specific.
300
3015. Architecture-specific sections
302=================================
316Architectures may override arch_klp_init_object_loaded() to perform 303Architectures may override arch_klp_init_object_loaded() to perform
317additional arch-specific tasks when a target module loads, such as applying 304additional arch-specific tasks when a target module loads, such as applying
318arch-specific sections. On x86 for example, we must apply per-object 305arch-specific sections. On x86 for example, we must apply per-object
@@ -321,9 +308,8 @@ These sections must be prefixed with ".klp.arch.$objname." so that they can
321be easily identified when iterating through a patch module's Elf sections 308be easily identified when iterating through a patch module's Elf sections
322(See arch/x86/kernel/livepatch.c for a complete example). 309(See arch/x86/kernel/livepatch.c for a complete example).
323 310
324-------------------------------------- 3116. Symbol table and Elf section access
3255. Symbol table and Elf section access 312======================================
326--------------------------------------
327A livepatch module's symbol table is accessible through module->symtab. 313A livepatch module's symbol table is accessible through module->symtab.
328 314
329Since apply_relocate_add() requires access to a module's section headers, 315Since apply_relocate_add() requires access to a module's section headers,