@@ -12,12 +12,12 @@ Table of Contents:
12
12
4. Livepatch module
13
13
4.1. New functions
14
14
4.2. Metadata
15
- 4.3. Livepatch module handling
16
15
5. Livepatch life-cycle
17
- 5.1. Registration
16
+ 5.1. Loading
18
17
5.2. Enabling
19
- 5.3. Disabling
20
- 5.4. Unregistration
18
+ 5.3. Replacing
19
+ 5.4. Disabling
20
+ 5.5. Removing
21
21
6. Sysfs
22
22
7. Limitations
23
23
@@ -143,9 +143,9 @@ without HAVE_RELIABLE_STACKTRACE are not considered fully supported by
143
143
the kernel livepatching.
144
144
145
145
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.
149
149
150
150
A transition can be reversed and effectively canceled by writing the
151
151
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
158
158
unpatched and 1 to indicate it's patched. Otherwise, if no patch is in
159
159
transition, it shows -1. Any tasks which are blocking the transition
160
160
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.
167
166
168
167
Administrator can also affect a transition through
169
168
/sys/kernel/livepatch/<patch>/force attribute. Writing 1 there clears
@@ -298,117 +297,110 @@ into three levels:
298
297
see the "Consistency model" section.
299
298
300
299
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
-
319
300
5. Livepatch life-cycle
320
301
=======================
321
302
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.
325
305
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 .
329
309
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.
336
310
311
+ 5.1. Loading
312
+ ------------
337
313
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:
340
317
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.
344
319
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.
351
322
352
323
353
324
5.2. Enabling
354
325
-------------
355
326
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.
359
330
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 .
365
336
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.
369
343
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'.
375
346
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.
378
352
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.
379
356
380
- 5.3. Disabling
357
+
358
+ 5.3. Replacing
381
359
--------------
382
360
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.
387
372
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.
393
373
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
395
389
patch are removed from the corresponding struct klp_ops. The ftrace handler
396
390
is unregistered and the struct klp_ops is freed when the func_stack list
397
391
becomes empty.
398
392
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.
402
394
403
- 5.4. Unregistration
404
- -------------------
405
395
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
+ -------------
409
398
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.
412
404
413
405
414
406
6. Sysfs
@@ -418,8 +410,8 @@ Information about the registered patches can be found under
418
410
/sys/kernel/livepatch. The patches could be enabled and disabled
419
411
by writing there.
420
412
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.
423
415
424
416
See Documentation/ABI/testing/sysfs-kernel-livepatch for more details.
425
417
0 commit comments