Skip to content

Commit 73727f4

Browse files
committed
livepatch: Basic API to track system state changes
This is another step how to help maintaining more livepatches. One big help was the atomic replace and cumulative livepatches. These livepatches replace the already installed ones. Therefore it should be enough when each cumulative livepatch is consistent. The problems might come with shadow variables and callbacks. They might change the system behavior or state so that it is no longer safe to go back and use an older livepatch or the original kernel code. Also, a new livepatch must be able to detect changes which were made by the already installed livepatches. This is where the livepatch system state tracking gets useful. It allows to: - find whether a system state has already been modified by previous livepatches - store data needed to manipulate and restore the system state The information about the manipulated system states is stored in an array of struct klp_state. It can be searched by two new functions klp_get_state() and klp_get_prev_state(). The dependencies are going to be solved by a version field added later. The only important information is that it will be allowed to modify the same state by more non-cumulative livepatches. It is similar to allowing to modify the same function several times. The livepatch author is responsible for preventing incompatible changes. Link: http://lkml.kernel.org/r/20191030154313.13263-3-pmladek@suse.com To: Jiri Kosina <jikos@kernel.org> Cc: Kamalesh Babulal <kamalesh@linux.vnet.ibm.com> Cc: Nicolai Stange <nstange@suse.de> Cc: live-patching@vger.kernel.org Cc: linux-kernel@vger.kernel.org Acked-by: Miroslav Benes <mbenes@suse.cz> Acked-by: Joe Lawrence <joe.lawrence@redhat.com> Acked-by: Josh Poimboeuf <jpoimboe@redhat.com> Signed-off-by: Petr Mladek <pmladek@suse.com>
1 parent 7e35e4e commit 73727f4

3 files changed

Lines changed: 99 additions & 1 deletion

File tree

include/linux/livepatch.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,10 +130,21 @@ struct klp_object {
130130
bool patched;
131131
};
132132

133+
/**
134+
* struct klp_state - state of the system modified by the livepatch
135+
* @id: system state identifier (non-zero)
136+
* @data: custom data
137+
*/
138+
struct klp_state {
139+
unsigned long id;
140+
void *data;
141+
};
142+
133143
/**
134144
* struct klp_patch - patch structure for live patching
135145
* @mod: reference to the live patch module
136146
* @objs: object entries for kernel objects to be patched
147+
* @states: system states that can get modified
137148
* @replace: replace all actively used patches
138149
* @list: list node for global list of actively used patches
139150
* @kobj: kobject for sysfs resources
@@ -147,6 +158,7 @@ struct klp_patch {
147158
/* external */
148159
struct module *mod;
149160
struct klp_object *objs;
161+
struct klp_state *states;
150162
bool replace;
151163

152164
/* internal */
@@ -217,6 +229,9 @@ void *klp_shadow_get_or_alloc(void *obj, unsigned long id,
217229
void klp_shadow_free(void *obj, unsigned long id, klp_shadow_dtor_t dtor);
218230
void klp_shadow_free_all(unsigned long id, klp_shadow_dtor_t dtor);
219231

232+
struct klp_state *klp_get_state(struct klp_patch *patch, unsigned long id);
233+
struct klp_state *klp_get_prev_state(unsigned long id);
234+
220235
#else /* !CONFIG_LIVEPATCH */
221236

222237
static inline int klp_module_coming(struct module *mod) { return 0; }

kernel/livepatch/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
# SPDX-License-Identifier: GPL-2.0-only
22
obj-$(CONFIG_LIVEPATCH) += livepatch.o
33

4-
livepatch-objs := core.o patch.o shadow.o transition.o
4+
livepatch-objs := core.o patch.o shadow.o state.o transition.o

kernel/livepatch/state.c

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
// SPDX-License-Identifier: GPL-2.0-or-later
2+
/*
3+
* system_state.c - State of the system modified by livepatches
4+
*
5+
* Copyright (C) 2019 SUSE
6+
*/
7+
8+
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
9+
10+
#include <linux/livepatch.h>
11+
#include "core.h"
12+
#include "transition.h"
13+
14+
#define klp_for_each_state(patch, state) \
15+
for (state = patch->states; state && state->id; state++)
16+
17+
/**
18+
* klp_get_state() - get information about system state modified by
19+
* the given patch
20+
* @patch: livepatch that modifies the given system state
21+
* @id: custom identifier of the modified system state
22+
*
23+
* Checks whether the given patch modifies the given system state.
24+
*
25+
* The function can be called either from pre/post (un)patch
26+
* callbacks or from the kernel code added by the livepatch.
27+
*
28+
* Return: pointer to struct klp_state when found, otherwise NULL.
29+
*/
30+
struct klp_state *klp_get_state(struct klp_patch *patch, unsigned long id)
31+
{
32+
struct klp_state *state;
33+
34+
klp_for_each_state(patch, state) {
35+
if (state->id == id)
36+
return state;
37+
}
38+
39+
return NULL;
40+
}
41+
EXPORT_SYMBOL_GPL(klp_get_state);
42+
43+
/**
44+
* klp_get_prev_state() - get information about system state modified by
45+
* the already installed livepatches
46+
* @id: custom identifier of the modified system state
47+
*
48+
* Checks whether already installed livepatches modify the given
49+
* system state.
50+
*
51+
* The same system state can be modified by more non-cumulative
52+
* livepatches. It is expected that the latest livepatch has
53+
* the most up-to-date information.
54+
*
55+
* The function can be called only during transition when a new
56+
* livepatch is being enabled or when such a transition is reverted.
57+
* It is typically called only from from pre/post (un)patch
58+
* callbacks.
59+
*
60+
* Return: pointer to the latest struct klp_state from already
61+
* installed livepatches, NULL when not found.
62+
*/
63+
struct klp_state *klp_get_prev_state(unsigned long id)
64+
{
65+
struct klp_patch *patch;
66+
struct klp_state *state, *last_state = NULL;
67+
68+
if (WARN_ON_ONCE(!klp_transition_patch))
69+
return NULL;
70+
71+
klp_for_each_patch(patch) {
72+
if (patch == klp_transition_patch)
73+
goto out;
74+
75+
state = klp_get_state(patch, id);
76+
if (state)
77+
last_state = state;
78+
}
79+
80+
out:
81+
return last_state;
82+
}
83+
EXPORT_SYMBOL_GPL(klp_get_prev_state);

0 commit comments

Comments
 (0)