Skip to content

Commit 958ef1e

Browse files
pmladekJiri Kosina
authored andcommitted
livepatch: Simplify API by removing registration step
The possibility to re-enable a registered patch was useful for immediate patches where the livepatch module had to stay until the system reboot. The improved consistency model allows to achieve the same result by unloading and loading the livepatch module again. Also we are going to add a feature called atomic replace. It will allow to create a patch that would replace all already registered patches. The aim is to handle dependent patches more securely. It will obsolete the stack of patches that helped to handle the dependencies so far. Then it might be unclear when a cumulative patch re-enabling is safe. It would be complicated to support the many modes. Instead we could actually make the API and code easier to understand. Therefore, remove the two step public API. All the checks and init calls are moved from klp_register_patch() to klp_enabled_patch(). Also the patch is automatically freed, including the sysfs interface when the transition to the disabled state is completed. As a result, there is never a disabled patch on the top of the stack. Therefore we do not need to check the stack in __klp_enable_patch(). And we could simplify the check in __klp_disable_patch(). Also the API and logic is much easier. It is enough to call klp_enable_patch() in module_init() call. The patch can be disabled by writing '0' into /sys/kernel/livepatch/<patch>/enabled. Then the module can be removed once the transition finishes and sysfs interface is freed. The only problem is how to free the structures and kobjects safely. The operation is triggered from the sysfs interface. We could not put the related kobject from there because it would cause lock inversion between klp_mutex and kernfs locks, see kn->count lockdep map. Therefore, offload the free task to a workqueue. It is perfectly fine: + The patch can no longer be used in the livepatch operations. + The module could not be removed until the free operation finishes and module_put() is called. + The operation is asynchronous already when the first klp_try_complete_transition() fails and another call is queued with a delay. Suggested-by: Josh Poimboeuf <[email protected]> Signed-off-by: Petr Mladek <[email protected]> Acked-by: Miroslav Benes <[email protected]> Acked-by: Josh Poimboeuf <[email protected]> Signed-off-by: Jiri Kosina <[email protected]>
1 parent 6800728 commit 958ef1e

File tree

9 files changed

+168
-329
lines changed

9 files changed

+168
-329
lines changed

Documentation/livepatch/livepatch.txt

Lines changed: 53 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,11 @@ 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
1918
5.3. Disabling
20-
5.4. Unregistration
19+
5.4. Removing
2120
6. Sysfs
2221
7. Limitations
2322

@@ -298,117 +297,89 @@ 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.
325-
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.
303+
Livepatching can be described by four basic operations:
304+
loading, enabling, disabling, removing.
329305

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.
336306

307+
5.1. Loading
308+
------------
337309

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

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.
314+
First, only the module has an easy access to the related struct klp_patch.
344315

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.
316+
Second, the error code might be used to refuse loading the module when
317+
the patch cannot get enabled.
351318

352319

353320
5.2. Enabling
354321
-------------
355322

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.
323+
The livepatch gets enabled by calling klp_enable_patch() from
324+
the module_init() callback. The system will start using the new
325+
implementation of the patched functions at this stage.
359326

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.
327+
First, the addresses of the patched functions are found according to their
328+
names. The special relocations, mentioned in the section "New functions",
329+
are applied. The relevant entries are created under
330+
/sys/kernel/livepatch/<name>. The patch is rejected when any above
331+
operation fails.
365332

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.
333+
Second, livepatch enters into a transition state where tasks are converging
334+
to the patched state. If an original function is patched for the first
335+
time, a function specific struct klp_ops is created and an universal
336+
ftrace handler is registered[*]. This stage is indicated by a value of '1'
337+
in /sys/kernel/livepatch/<name>/transition. For more information about
338+
this process, see the "Consistency model" section.
369339

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.
340+
Finally, once all tasks have been patched, the 'transition' value changes
341+
to '0'.
375342

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

379349

380350
5.3. Disabling
381351
--------------
382352

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.
353+
Enabled patches might get disabled by writing '0' to
354+
/sys/kernel/livepatch/<name>/enabled.
387355

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.
356+
First, livepatch enters into a transition state where tasks are converging
357+
to the unpatched state. The system starts using either the code from
358+
the previously enabled patch or even the original one. This stage is
359+
indicated by a value of '1' in /sys/kernel/livepatch/<name>/transition.
360+
For more information about this process, see the "Consistency model"
361+
section.
393362

394-
Here all the functions (struct klp_func) associated with the to-be-disabled
363+
Second, once all tasks have been unpatched, the 'transition' value changes
364+
to '0'. All the functions (struct klp_func) associated with the to-be-disabled
395365
patch are removed from the corresponding struct klp_ops. The ftrace handler
396366
is unregistered and the struct klp_ops is freed when the func_stack list
397367
becomes empty.
398368

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.
369+
Third, the sysfs interface is destroyed.
401370

371+
Note that patches must be disabled in exactly the reverse order in which
372+
they were enabled. It makes the problem and the implementation much easier.
402373

403-
5.4. Unregistration
404-
-------------------
405374

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.
375+
5.4. Removing
376+
-------------
409377

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

413384

414385
6. Sysfs

include/linux/livepatch.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -139,11 +139,12 @@ struct klp_object {
139139
* struct klp_patch - patch structure for live patching
140140
* @mod: reference to the live patch module
141141
* @objs: object entries for kernel objects to be patched
142-
* @list: list node for global list of registered patches
142+
* @list: list node for global list of actively used patches
143143
* @kobj: kobject for sysfs resources
144144
* @kobj_added: @kobj has been added and needs freeing
145145
* @enabled: the patch is enabled (but operation may be incomplete)
146146
* @forced: was involved in a forced transition
147+
* @free_work: patch cleanup from workqueue-context
147148
* @finish: for waiting till it is safe to remove the patch module
148149
*/
149150
struct klp_patch {
@@ -157,6 +158,7 @@ struct klp_patch {
157158
bool kobj_added;
158159
bool enabled;
159160
bool forced;
161+
struct work_struct free_work;
160162
struct completion finish;
161163
};
162164

@@ -168,10 +170,7 @@ struct klp_patch {
168170
func->old_name || func->new_func || func->old_sympos; \
169171
func++)
170172

171-
int klp_register_patch(struct klp_patch *);
172-
int klp_unregister_patch(struct klp_patch *);
173173
int klp_enable_patch(struct klp_patch *);
174-
int klp_disable_patch(struct klp_patch *);
175174

176175
void arch_klp_init_object_loaded(struct klp_patch *patch,
177176
struct klp_object *obj);

0 commit comments

Comments
 (0)