Skip to content

Commit d9defe4

Browse files
pmladekJonathan Corbet
authored andcommitted
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>
1 parent 89e33ea commit d9defe4

2 files changed

Lines changed: 104 additions & 115 deletions

File tree

‎Documentation/livepatch/callbacks.rst‎

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
Livepatch (un)patch-callbacks provide a mechanism for livepatch modules
66
to execute callback functions when a kernel object is (un)patched. They
7-
can be considered a "power feature" that extends livepatching abilities
7+
can be considered a **power feature** that **extends livepatching abilities**
88
to include:
99

1010
- Safe updates to global data
@@ -17,6 +17,9 @@ In most cases, (un)patch callbacks will need to be used in conjunction
1717
with memory barriers and kernel synchronization primitives, like
1818
mutexes/spinlocks, or even stop_machine(), to avoid concurrency issues.
1919

20+
1. Motivation
21+
=============
22+
2023
Callbacks differ from existing kernel facilities:
2124

2225
- 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
2831
is specific to that klp_object. Other livepatch objects may or may not
2932
be patched, irrespective of the target klp_object's current state.
3033

34+
2. Callback types
35+
=================
36+
3137
Callbacks can be registered for the following livepatch actions:
3238

3339
* Pre-patch
@@ -47,6 +53,9 @@ Callbacks can be registered for the following livepatch actions:
4753
been restored and no tasks are running patched code,
4854
used to cleanup pre-patch callback resources
4955

56+
3. How it works
57+
===============
58+
5059
Each callback is optional, omitting one does not preclude specifying any
5160
other. However, the livepatching core executes the handlers in
5261
symmetry: pre-patch callbacks have a post-unpatch counterpart and
@@ -90,11 +99,14 @@ If the object did successfully patch, but the patch transition never
9099
started for some reason (e.g., if another object failed to patch),
91100
only the post-unpatch callback will be called.
92101

102+
4. Use cases
103+
============
93104

94-
Example Use-cases
95-
=================
105+
Sample livepatch modules demonstrating the callback API can be found in
106+
samples/livepatch/ directory. These samples were modified for use in
107+
kselftests and can be found in the lib/livepatch directory.
96108

97-
Update global data
109+
Global data update
98110
------------------
99111

100112
A 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,
107119
so that tcp_send_challenge_ack() could first be changed to read
108120
sysctl_tcp_challenge_ack_limit with READ_ONCE.
109121

110-
111-
Support __init and probe function patches
122+
__init and probe function patches support
112123
-----------------------------------------
113124

114125
Although __init and probe functions are not directly livepatch-able, it
115126
may be possible to implement similar updates via pre/post-patch
116127
callbacks.
117128

118-
48900cb6af42 ("virtio-net: drop NETIF_F_FRAGLIST") change the way that
129+
The commit ``48900cb6af42 ("virtio-net: drop NETIF_F_FRAGLIST")`` change the way that
119130
virtnet_probe() initialized its driver's net_device features. A
120131
pre/post-patch callback could iterate over all such devices, making a
121132
similar change to their hw_features value. (Client functions of the
122133
value may need to be updated accordingly.)
123-
124-
125-
Other Examples
126-
==============
127-
128-
Sample livepatch modules demonstrating the callback API can be found in
129-
samples/livepatch/ directory. These samples were modified for use in
130-
kselftests and can be found in the lib/livepatch directory.

‎Documentation/livepatch/module-elf-format.rst‎

Lines changed: 86 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -7,30 +7,18 @@ This document outlines the Elf format requirements that livepatch modules must f
77

88
.. Table of Contents
99
10-
0. Background and motivation
11-
1. Livepatch modinfo field
12-
2. Livepatch relocation sections
13-
2.1 What are livepatch relocation sections?
14-
2.2 Livepatch relocation section format
15-
2.2.1 Required flags
16-
2.2.2 Required name format
17-
2.2.3 Example livepatch relocation section names
18-
2.2.4 Example `readelf --sections` output
19-
2.2.5 Example `readelf --relocs` output
20-
3. Livepatch symbols
21-
3.1 What are livepatch symbols?
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-
----------------------------
32-
0. Background and motivation
33-
----------------------------
10+
1. Background and motivation
11+
2. Livepatch modinfo field
12+
3. Livepatch relocation sections
13+
3.1 Livepatch relocation section format
14+
4. Livepatch symbols
15+
4.1 A livepatch module's symbol table
16+
4.2 Livepatch symbol format
17+
5. Architecture-specific sections
18+
6. Symbol table and Elf section access
19+
20+
1. Background and motivation
21+
============================
3422

3523
Formerly, livepatch required separate architecture-specific code to write
3624
relocations. However, arch-specific code to write relocations already
@@ -52,8 +40,8 @@ relocation sections and symbols, which are described in this document. The
5240
Elf constants used to mark livepatch symbols and relocation sections were
5341
selected from OS-specific ranges according to the definitions from glibc.
5442

55-
0.1 Why does livepatch need to write its own relocations?
56-
---------------------------------------------------------
43+
Why does livepatch need to write its own relocations?
44+
-----------------------------------------------------
5745
A typical livepatch module contains patched versions of functions that can
5846
reference non-exported global symbols and non-included local symbols.
5947
Relocations 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
7260
arch-specific livepatch relocation code is replaced by a call to
7361
apply_relocate_add().
7462

75-
================================
76-
PATCH MODULE FORMAT REQUIREMENTS
77-
================================
78-
79-
--------------------------
80-
1. Livepatch modinfo field
81-
--------------------------
63+
2. Livepatch modinfo field
64+
==========================
8265

8366
Livepatch modules are required to have the "livepatch" modinfo attribute.
8467
See 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
8770
and looking for the presence of the "livepatch" field. This field is also
8871
used by the kernel module loader to identify livepatch modules.
8972

90-
Example modinfo output:
91-
-----------------------
73+
Example:
74+
--------
75+
76+
**Modinfo output:**
9277

9378
::
9479

@@ -99,13 +84,9 @@ Example modinfo output:
9984
depends:
10085
vermagic: 4.3.0+ SMP mod_unload
10186

102-
--------------------------------
103-
2. Livepatch relocation sections
104-
--------------------------------
87+
3. Livepatch relocation sections
88+
================================
10589

106-
-------------------------------------------
107-
2.1 What are livepatch relocation sections?
108-
-------------------------------------------
10990
A livepatch module manages its own Elf relocation sections to apply
11091
relocations to modules as well as to the kernel (vmlinux) at the
11192
appropriate time. For example, if a patch module patches a driver that is
@@ -130,21 +111,16 @@ Every symbol referenced by a rela in a livepatch relocation section is a
130111
livepatch symbol. These must be resolved before livepatch can call
131112
apply_relocate_add(). See Section 3 for more information.
132113

133-
---------------------------------------
134-
2.2 Livepatch relocation section format
135-
---------------------------------------
114+
3.1 Livepatch relocation section format
115+
=======================================
136116

137-
2.2.1 Required flags
138-
--------------------
139117
Livepatch relocation sections must be marked with the SHF_RELA_LIVEPATCH
140118
section flag. See include/uapi/linux/elf.h for the definition. The module
141119
loader recognizes this flag and will avoid applying those relocation sections
142120
at patch module load time. These sections must also be marked with SHF_ALLOC,
143121
so that the module loader doesn't discard them on module load (i.e. they will
144122
be copied into memory along with the other SHF_ALLOC sections).
145123

146-
2.2.2 Required name format
147-
--------------------------
148124
The name of a livepatch relocation section must conform to the following
149125
format::
150126

@@ -153,19 +129,28 @@ format::
153129
|________||_____| |__________|
154130
[A] [B] [C]
155131

156-
[A] The relocation section name is prefixed with the string ".klp.rela."
157-
[B] The name of the object (i.e. "vmlinux" or name of module) to
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.
132+
[A]
133+
The relocation section name is prefixed with the string ".klp.rela."
160134

161-
2.2.3 Example livepatch relocation section names:
162-
-------------------------------------------------
163-
.klp.rela.ext4.text.ext4_attr_store
164-
.klp.rela.vmlinux.text.cmdline_proc_show
135+
[B]
136+
The name of the object (i.e. "vmlinux" or name of module) to
137+
which the relocation section belongs follows immediately after the prefix.
165138

166-
2.2.4 Example `readelf --sections` output for a patch
167-
module that patches vmlinux and modules 9p, btrfs, ext4:
168-
--------------------------------------------------------
139+
[C]
140+
The actual name of the section to which this relocation section applies.
141+
142+
Examples:
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
153+
module that patches vmlinux and modules 9p, btrfs, ext4:**
169154

170155
::
171156

@@ -182,13 +167,14 @@ module that patches vmlinux and modules 9p, btrfs, ext4:
182167
[ snip ] ^ ^
183168
| |
184169
[*] [*]
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
186173
characteristics. Notice that they are marked SHF_ALLOC ("A") so that they will
187174
not be discarded when the module is loaded into memory, as well as with the
188175
SHF_RELA_LIVEPATCH flag ("o" - for OS-specific).
189176

190-
2.2.5 Example `readelf --relocs` output for a patch module:
191-
-----------------------------------------------------------
177+
**`readelf --relocs` output for a patch module:**
192178

193179
::
194180

@@ -200,16 +186,14 @@ module that patches vmlinux and modules 9p, btrfs, ext4:
200186
000000000000004c 0000004900000002 R_X86_64_PC32 0000000000000000 .klp.sym.vmlinux.snprintf,0 - 4
201187
[ snip ] ^
202188
|
203-
[*]
204-
[*] Every symbol referenced by a relocation is a livepatch symbol.
189+
[*]
190+
191+
[*]
192+
Every symbol referenced by a relocation is a livepatch symbol.
205193

206-
--------------------
207-
3. Livepatch symbols
208-
--------------------
194+
4. Livepatch symbols
195+
====================
209196

210-
-------------------------------
211-
3.1 What are livepatch symbols?
212-
-------------------------------
213197
Livepatch symbols are symbols referred to by livepatch relocation sections.
214198
These are symbols accessed from new versions of functions for patched
215199
objects, 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
229213
in their symbol tables, and the symbol table is made accessible through
230214
module->symtab.
231215

232-
-------------------------------------
233-
3.2 A livepatch module's symbol table
234-
-------------------------------------
216+
4.1 A livepatch module's symbol table
217+
=====================================
235218
Normally, a stripped down copy of a module's symbol table (containing only
236219
"core" symbols) is made available through module->symtab (See layout_symtab()
237220
in kernel/module.c). For livepatch modules, the symbol table copied into memory
@@ -255,45 +238,48 @@ For example, take this particular rela from a livepatch module:::
255238
94: 0000000000000000 0 NOTYPE GLOBAL DEFAULT OS [0xff20] .klp.sym.vmlinux.printk,0
256239
[ snip ]
257240

258-
---------------------------
259-
3.3 Livepatch symbol format
260-
---------------------------
241+
4.2 Livepatch symbol format
242+
===========================
261243

262-
3.3.1 Required flags
263-
--------------------
264244
Livepatch symbols must have their section index marked as SHN_LIVEPATCH, so
265245
that the module loader can identify them and not attempt to resolve them.
266246
See include/uapi/linux/elf.h for the actual definitions.
267247

268-
3.3.2 Required name format
269-
--------------------------
270248
Livepatch symbol names must conform to the following format::
271249

272250
.klp.sym.objname.symbol_name,sympos
273251
^ ^^ ^ ^ ^ ^
274252
|_______||_____| |_________| |
275253
[A] [B] [C] [D]
276254

277-
[A] The symbol name is prefixed with the string ".klp.sym."
278-
[B] The name of the object (i.e. "vmlinux" or name of module) to
279-
which the symbol belongs follows immediately after the prefix.
280-
[C] The actual name of the symbol.
281-
[D] The position of the symbol in the object (as according to kallsyms)
282-
This is used to differentiate duplicate symbols within the same
283-
object. The symbol position is expressed numerically (0, 1, 2...).
284-
The symbol position of a unique symbol is 0.
255+
[A]
256+
The symbol name is prefixed with the string ".klp.sym."
257+
258+
[B]
259+
The name of the object (i.e. "vmlinux" or name of module) to
260+
which the symbol belongs follows immediately after the prefix.
285261

286-
3.3.3 Example livepatch symbol names:
287-
-------------------------------------
262+
[C]
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+
271+
Examples:
272+
---------
273+
274+
**Livepatch symbol names:**
288275

289276
::
290277

291278
.klp.sym.vmlinux.snprintf,0
292279
.klp.sym.vmlinux.printk,0
293280
.klp.sym.btrfs.btrfs_ktype,0
294281

295-
3.3.4 Example `readelf --symbols` output for a patch module:
296-
------------------------------------------------------------
282+
**`readelf --symbols` output for a patch module:**
297283

298284
::
299285

@@ -307,12 +293,13 @@ Livepatch symbol names must conform to the following format::
307293
[ snip ] ^
308294
|
309295
[*]
310-
[*] Note that the 'Ndx' (Section index) for these symbols is SHN_LIVEPATCH (0xff20).
311-
"OS" means OS-specific.
312296

313-
---------------------------------
314-
4. Architecture-specific sections
315-
---------------------------------
297+
[*]
298+
Note that the 'Ndx' (Section index) for these symbols is SHN_LIVEPATCH (0xff20).
299+
"OS" means OS-specific.
300+
301+
5. Architecture-specific sections
302+
=================================
316303
Architectures may override arch_klp_init_object_loaded() to perform
317304
additional arch-specific tasks when a target module loads, such as applying
318305
arch-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
321308
be easily identified when iterating through a patch module's Elf sections
322309
(See arch/x86/kernel/livepatch.c for a complete example).
323310

324-
--------------------------------------
325-
5. Symbol table and Elf section access
326-
--------------------------------------
311+
6. Symbol table and Elf section access
312+
======================================
327313
A livepatch module's symbol table is accessible through module->symtab.
328314

329315
Since apply_relocate_add() requires access to a module's section headers,

0 commit comments

Comments
 (0)