Skip to content

Commit b7af27b

Browse files
committed
Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/livepatching/livepatching
Pull livepatching updates from Jiri Kosina: - support for something we call 'atomic replace', and allows for much better handling of cumulative patches (which is something very useful for distros), from Jason Baron with help of Petr Mladek and Joe Lawrence - improvement of handling of tasks blocking finalization, from Miroslav Benes - update of MAINTAINERS file to reflect move towards group maintainership * 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/livepatching/livepatching: (22 commits) livepatch/selftests: use "$@" to preserve argument list livepatch: Module coming and going callbacks can proceed with all listed patches livepatch: Proper error handling in the shadow variables selftest livepatch: return -ENOMEM on ptr_id() allocation failure livepatch: Introduce klp_for_each_patch macro livepatch: core: Return EOPNOTSUPP instead of ENOSYS selftests/livepatch: add DYNAMIC_DEBUG config dependency livepatch: samples: non static warnings fix livepatch: update MAINTAINERS livepatch: Remove signal sysfs attribute livepatch: Send a fake signal periodically selftests/livepatch: introduce tests livepatch: Remove ordering (stacking) of the livepatches livepatch: Atomic replace and cumulative patches documentation livepatch: Remove Nop structures when unused livepatch: Add atomic replace livepatch: Use lists to manage patches, objects and functions livepatch: Simplify API by removing registration step livepatch: Don't block the removal of patches loaded after a forced transition livepatch: Consolidate klp_free functions ...
2 parents 851ca77 + f9d1381 commit b7af27b

35 files changed

+2649
-1071
lines changed

Documentation/ABI/testing/sysfs-kernel-livepatch

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -33,18 +33,6 @@ Description:
3333
An attribute which indicates whether the patch is currently in
3434
transition.
3535

36-
What: /sys/kernel/livepatch/<patch>/signal
37-
Date: Nov 2017
38-
KernelVersion: 4.15.0
39-
40-
Description:
41-
A writable attribute that allows administrator to affect the
42-
course of an existing transition. Writing 1 sends a fake
43-
signal to all remaining blocking tasks. The fake signal
44-
means that no proper signal is delivered (there is no data in
45-
signal pending structures). Tasks are interrupted or woken up,
46-
and forced to change their patched state.
47-
4836
What: /sys/kernel/livepatch/<patch>/force
4937
Date: Nov 2017
5038
KernelVersion: 4.15.0

Documentation/livepatch/callbacks.txt

Lines changed: 5 additions & 484 deletions
Large diffs are not rendered by default.
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
===================================
2+
Atomic Replace & Cumulative Patches
3+
===================================
4+
5+
There might be dependencies between livepatches. If multiple patches need
6+
to do different changes to the same function(s) then we need to define
7+
an order in which the patches will be installed. And function implementations
8+
from any newer livepatch must be done on top of the older ones.
9+
10+
This might become a maintenance nightmare. Especially when more patches
11+
modified the same function in different ways.
12+
13+
An elegant solution comes with the feature called "Atomic Replace". It allows
14+
creation of so called "Cumulative Patches". They include all wanted changes
15+
from all older livepatches and completely replace them in one transition.
16+
17+
Usage
18+
-----
19+
20+
The atomic replace can be enabled by setting "replace" flag in struct klp_patch,
21+
for example:
22+
23+
static struct klp_patch patch = {
24+
.mod = THIS_MODULE,
25+
.objs = objs,
26+
.replace = true,
27+
};
28+
29+
All processes are then migrated to use the code only from the new patch.
30+
Once the transition is finished, all older patches are automatically
31+
disabled.
32+
33+
Ftrace handlers are transparently removed from functions that are no
34+
longer modified by the new cumulative patch.
35+
36+
As a result, the livepatch authors might maintain sources only for one
37+
cumulative patch. It helps to keep the patch consistent while adding or
38+
removing various fixes or features.
39+
40+
Users could keep only the last patch installed on the system after
41+
the transition to has finished. It helps to clearly see what code is
42+
actually in use. Also the livepatch might then be seen as a "normal"
43+
module that modifies the kernel behavior. The only difference is that
44+
it can be updated at runtime without breaking its functionality.
45+
46+
47+
Features
48+
--------
49+
50+
The atomic replace allows:
51+
52+
+ Atomically revert some functions in a previous patch while
53+
upgrading other functions.
54+
55+
+ Remove eventual performance impact caused by core redirection
56+
for functions that are no longer patched.
57+
58+
+ Decrease user confusion about dependencies between livepatches.
59+
60+
61+
Limitations:
62+
------------
63+
64+
+ Once the operation finishes, there is no straightforward way
65+
to reverse it and restore the replaced patches atomically.
66+
67+
A good practice is to set .replace flag in any released livepatch.
68+
Then re-adding an older livepatch is equivalent to downgrading
69+
to that patch. This is safe as long as the livepatches do _not_ do
70+
extra modifications in (un)patching callbacks or in the module_init()
71+
or module_exit() functions, see below.
72+
73+
Also note that the replaced patch can be removed and loaded again
74+
only when the transition was not forced.
75+
76+
77+
+ Only the (un)patching callbacks from the _new_ cumulative livepatch are
78+
executed. Any callbacks from the replaced patches are ignored.
79+
80+
In other words, the cumulative patch is responsible for doing any actions
81+
that are necessary to properly replace any older patch.
82+
83+
As a result, it might be dangerous to replace newer cumulative patches by
84+
older ones. The old livepatches might not provide the necessary callbacks.
85+
86+
This might be seen as a limitation in some scenarios. But it makes life
87+
easier in many others. Only the new cumulative livepatch knows what
88+
fixes/features are added/removed and what special actions are necessary
89+
for a smooth transition.
90+
91+
In any case, it would be a nightmare to think about the order of
92+
the various callbacks and their interactions if the callbacks from all
93+
enabled patches were called.
94+
95+
96+
+ There is no special handling of shadow variables. Livepatch authors
97+
must create their own rules how to pass them from one cumulative
98+
patch to the other. Especially that they should not blindly remove
99+
them in module_exit() functions.
100+
101+
A good practice might be to remove shadow variables in the post-unpatch
102+
callback. It is called only when the livepatch is properly disabled.

Documentation/livepatch/livepatch.txt

Lines changed: 87 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@ Table of Contents:
1212
4. Livepatch module
1313
4.1. New functions
1414
4.2. Metadata
15-
4.3. Livepatch module handling
1615
5. Livepatch life-cycle
17-
5.1. Registration
16+
5.1. Loading
1817
5.2. Enabling
19-
5.3. Disabling
20-
5.4. Unregistration
18+
5.3. Replacing
19+
5.4. Disabling
20+
5.5. Removing
2121
6. Sysfs
2222
7. Limitations
2323

@@ -143,9 +143,9 @@ without HAVE_RELIABLE_STACKTRACE are not considered fully supported by
143143
the kernel livepatching.
144144

145145
The /sys/kernel/livepatch/<patch>/transition file shows whether a patch
146-
is in transition. Only a single patch (the topmost patch on the stack)
147-
can be in transition at a given time. A patch can remain in transition
148-
indefinitely, if any of the tasks are stuck in the initial patch state.
146+
is in transition. Only a single patch can be in transition at a given
147+
time. A patch can remain in transition indefinitely, if any of the tasks
148+
are stuck in the initial patch state.
149149

150150
A transition can be reversed and effectively canceled by writing the
151151
opposite value to the /sys/kernel/livepatch/<patch>/enabled file while
@@ -158,12 +158,11 @@ If a patch is in transition, this file shows 0 to indicate the task is
158158
unpatched and 1 to indicate it's patched. Otherwise, if no patch is in
159159
transition, it shows -1. Any tasks which are blocking the transition
160160
can be signaled with SIGSTOP and SIGCONT to force them to change their
161-
patched state. This may be harmful to the system though.
162-
/sys/kernel/livepatch/<patch>/signal attribute provides a better alternative.
163-
Writing 1 to the attribute sends a fake signal to all remaining blocking
164-
tasks. No proper signal is actually delivered (there is no data in signal
165-
pending structures). Tasks are interrupted or woken up, and forced to change
166-
their patched state.
161+
patched state. This may be harmful to the system though. Sending a fake signal
162+
to all remaining blocking tasks is a better alternative. No proper signal is
163+
actually delivered (there is no data in signal pending structures). Tasks are
164+
interrupted or woken up, and forced to change their patched state. The fake
165+
signal is automatically sent every 15 seconds.
167166

168167
Administrator can also affect a transition through
169168
/sys/kernel/livepatch/<patch>/force attribute. Writing 1 there clears
@@ -298,117 +297,110 @@ into three levels:
298297
see the "Consistency model" section.
299298

300299

301-
4.3. Livepatch module handling
302-
------------------------------
303-
304-
The usual behavior is that the new functions will get used when
305-
the livepatch module is loaded. For this, the module init() function
306-
has to register the patch (struct klp_patch) and enable it. See the
307-
section "Livepatch life-cycle" below for more details about these
308-
two operations.
309-
310-
Module removal is only safe when there are no users of the underlying
311-
functions. This is the reason why the force feature permanently disables
312-
the removal. The forced tasks entered the functions but we cannot say
313-
that they returned back. Therefore it cannot be decided when the
314-
livepatch module can be safely removed. When the system is successfully
315-
transitioned to a new patch state (patched/unpatched) without being
316-
forced it is guaranteed that no task sleeps or runs in the old code.
317-
318-
319300
5. Livepatch life-cycle
320301
=======================
321302

322-
Livepatching defines four basic operations that define the life cycle of each
323-
live patch: registration, enabling, disabling and unregistration. There are
324-
several reasons why it is done this way.
303+
Livepatching can be described by five basic operations:
304+
loading, enabling, replacing, disabling, removing.
325305

326-
First, the patch is applied only when all patched symbols for already
327-
loaded objects are found. The error handling is much easier if this
328-
check is done before particular functions get redirected.
306+
Where the replacing and the disabling operations are mutually
307+
exclusive. They have the same result for the given patch but
308+
not for the system.
329309

330-
Second, it might take some time until the entire system is migrated with
331-
the hybrid consistency model being used. The patch revert might block
332-
the livepatch module removal for too long. Therefore it is useful to
333-
revert the patch using a separate operation that might be called
334-
explicitly. But it does not make sense to remove all information until
335-
the livepatch module is really removed.
336310

311+
5.1. Loading
312+
------------
337313

338-
5.1. Registration
339-
-----------------
314+
The only reasonable way is to enable the patch when the livepatch kernel
315+
module is being loaded. For this, klp_enable_patch() has to be called
316+
in the module_init() callback. There are two main reasons:
340317

341-
Each patch first has to be registered using klp_register_patch(). This makes
342-
the patch known to the livepatch framework. Also it does some preliminary
343-
computing and checks.
318+
First, only the module has an easy access to the related struct klp_patch.
344319

345-
In particular, the patch is added into the list of known patches. The
346-
addresses of the patched functions are found according to their names.
347-
The special relocations, mentioned in the section "New functions", are
348-
applied. The relevant entries are created under
349-
/sys/kernel/livepatch/<name>. The patch is rejected when any operation
350-
fails.
320+
Second, the error code might be used to refuse loading the module when
321+
the patch cannot get enabled.
351322

352323

353324
5.2. Enabling
354325
-------------
355326

356-
Registered patches might be enabled either by calling klp_enable_patch() or
357-
by writing '1' to /sys/kernel/livepatch/<name>/enabled. The system will
358-
start using the new implementation of the patched functions at this stage.
327+
The livepatch gets enabled by calling klp_enable_patch() from
328+
the module_init() callback. The system will start using the new
329+
implementation of the patched functions at this stage.
359330

360-
When a patch is enabled, livepatch enters into a transition state where
361-
tasks are converging to the patched state. This is indicated by a value
362-
of '1' in /sys/kernel/livepatch/<name>/transition. Once all tasks have
363-
been patched, the 'transition' value changes to '0'. For more
364-
information about this process, see the "Consistency model" section.
331+
First, the addresses of the patched functions are found according to their
332+
names. The special relocations, mentioned in the section "New functions",
333+
are applied. The relevant entries are created under
334+
/sys/kernel/livepatch/<name>. The patch is rejected when any above
335+
operation fails.
365336

366-
If an original function is patched for the first time, a function
367-
specific struct klp_ops is created and an universal ftrace handler is
368-
registered.
337+
Second, livepatch enters into a transition state where tasks are converging
338+
to the patched state. If an original function is patched for the first
339+
time, a function specific struct klp_ops is created and an universal
340+
ftrace handler is registered[*]. This stage is indicated by a value of '1'
341+
in /sys/kernel/livepatch/<name>/transition. For more information about
342+
this process, see the "Consistency model" section.
369343

370-
Functions might be patched multiple times. The ftrace handler is registered
371-
only once for the given function. Further patches just add an entry to the
372-
list (see field `func_stack`) of the struct klp_ops. The last added
373-
entry is chosen by the ftrace handler and becomes the active function
374-
replacement.
344+
Finally, once all tasks have been patched, the 'transition' value changes
345+
to '0'.
375346

376-
Note that the patches might be enabled in a different order than they were
377-
registered.
347+
[*] Note that functions might be patched multiple times. The ftrace handler
348+
is registered only once for a given function. Further patches just add
349+
an entry to the list (see field `func_stack`) of the struct klp_ops.
350+
The right implementation is selected by the ftrace handler, see
351+
the "Consistency model" section.
378352

353+
That said, it is highly recommended to use cumulative livepatches
354+
because they help keeping the consistency of all changes. In this case,
355+
functions might be patched two times only during the transition period.
379356

380-
5.3. Disabling
357+
358+
5.3. Replacing
381359
--------------
382360

383-
Enabled patches might get disabled either by calling klp_disable_patch() or
384-
by writing '0' to /sys/kernel/livepatch/<name>/enabled. At this stage
385-
either the code from the previously enabled patch or even the original
386-
code gets used.
361+
All enabled patches might get replaced by a cumulative patch that
362+
has the .replace flag set.
363+
364+
Once the new patch is enabled and the 'transition' finishes then
365+
all the functions (struct klp_func) associated with the replaced
366+
patches are removed from the corresponding struct klp_ops. Also
367+
the ftrace handler is unregistered and the struct klp_ops is
368+
freed when the related function is not modified by the new patch
369+
and func_stack list becomes empty.
370+
371+
See Documentation/livepatch/cumulative-patches.txt for more details.
387372

388-
When a patch is disabled, livepatch enters into a transition state where
389-
tasks are converging to the unpatched state. This is indicated by a
390-
value of '1' in /sys/kernel/livepatch/<name>/transition. Once all tasks
391-
have been unpatched, the 'transition' value changes to '0'. For more
392-
information about this process, see the "Consistency model" section.
393373

394-
Here all the functions (struct klp_func) associated with the to-be-disabled
374+
5.4. Disabling
375+
--------------
376+
377+
Enabled patches might get disabled by writing '0' to
378+
/sys/kernel/livepatch/<name>/enabled.
379+
380+
First, livepatch enters into a transition state where tasks are converging
381+
to the unpatched state. The system starts using either the code from
382+
the previously enabled patch or even the original one. This stage is
383+
indicated by a value of '1' in /sys/kernel/livepatch/<name>/transition.
384+
For more information about this process, see the "Consistency model"
385+
section.
386+
387+
Second, once all tasks have been unpatched, the 'transition' value changes
388+
to '0'. All the functions (struct klp_func) associated with the to-be-disabled
395389
patch are removed from the corresponding struct klp_ops. The ftrace handler
396390
is unregistered and the struct klp_ops is freed when the func_stack list
397391
becomes empty.
398392

399-
Patches must be disabled in exactly the reverse order in which they were
400-
enabled. It makes the problem and the implementation much easier.
401-
393+
Third, the sysfs interface is destroyed.
402394

403-
5.4. Unregistration
404-
-------------------
405395

406-
Disabled patches might be unregistered by calling klp_unregister_patch().
407-
This can be done only when the patch is disabled and the code is no longer
408-
used. It must be called before the livepatch module gets unloaded.
396+
5.5. Removing
397+
-------------
409398

410-
At this stage, all the relevant sys-fs entries are removed and the patch
411-
is removed from the list of known patches.
399+
Module removal is only safe when there are no users of functions provided
400+
by the module. This is the reason why the force feature permanently
401+
disables the removal. Only when the system is successfully transitioned
402+
to a new patch state (patched/unpatched) without being forced it is
403+
guaranteed that no task sleeps or runs in the old code.
412404

413405

414406
6. Sysfs
@@ -418,8 +410,8 @@ Information about the registered patches can be found under
418410
/sys/kernel/livepatch. The patches could be enabled and disabled
419411
by writing there.
420412

421-
/sys/kernel/livepatch/<patch>/signal and /sys/kernel/livepatch/<patch>/force
422-
attributes allow administrator to affect a patching operation.
413+
/sys/kernel/livepatch/<patch>/force attributes allow administrator to affect a
414+
patching operation.
423415

424416
See Documentation/ABI/testing/sysfs-kernel-livepatch for more details.
425417

MAINTAINERS

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8967,10 +8967,10 @@ F: drivers/platform/x86/hp_accel.c
89678967

89688968
LIVE PATCHING
89698969
M: Josh Poimboeuf <[email protected]>
8970-
M: Jessica Yu <[email protected]>
89718970
M: Jiri Kosina <[email protected]>
89728971
M: Miroslav Benes <[email protected]>
8973-
R: Petr Mladek <[email protected]>
8972+
M: Petr Mladek <[email protected]>
8973+
R: Joe Lawrence <[email protected]>
89748974
S: Maintained
89758975
F: kernel/livepatch/
89768976
F: include/linux/livepatch.h
@@ -8979,8 +8979,9 @@ F: arch/x86/kernel/livepatch.c
89798979
F: Documentation/livepatch/
89808980
F: Documentation/ABI/testing/sysfs-kernel-livepatch
89818981
F: samples/livepatch/
8982+
F: tools/testing/selftests/livepatch/
89828983
8983-
T: git git://git.kernel.org/pub/scm/linux/kernel/git/jikos/livepatching.git
8984+
T: git git://git.kernel.org/pub/scm/linux/kernel/git/livepatching/livepatching.git
89848985

89858986
LLC (802.2)
89868987

0 commit comments

Comments
 (0)