Skip to content

Commit 92c9abf

Browse files
committed
livepatch: Allow to distinguish different version of system state changes
The atomic replace runs pre/post (un)install callbacks only from the new livepatch. There are several reasons for this: + Simplicity: clear ordering of operations, no interactions between old and new callbacks. + Reliability: only new livepatch knows what changes can already be made by older livepatches and how to take over the state. + Testing: the atomic replace can be properly tested only when a newer livepatch is available. It might be too late to fix unwanted effect of callbacks from older livepatches. It might happen that an older change is not enough and the same system state has to be modified another way. Different changes need to get distinguished by a version number added to struct klp_state. The version can also be used to prevent loading incompatible livepatches. The check is done when the livepatch is enabled. The rules are: + Any completely new system state modification is allowed. + System state modifications with the same or higher version are allowed for already modified system states. + Cumulative livepatches must handle all system state modifications from already installed livepatches. + Non-cumulative livepatches are allowed to touch already modified system states. Link: http://lkml.kernel.org/r/20191030154313.13263-4-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 73727f4 commit 92c9abf

4 files changed

Lines changed: 55 additions & 0 deletions

File tree

include/linux/livepatch.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,10 +133,12 @@ struct klp_object {
133133
/**
134134
* struct klp_state - state of the system modified by the livepatch
135135
* @id: system state identifier (non-zero)
136+
* @version: version of the change
136137
* @data: custom data
137138
*/
138139
struct klp_state {
139140
unsigned long id;
141+
unsigned int version;
140142
void *data;
141143
};
142144

kernel/livepatch/core.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include <asm/cacheflush.h>
2323
#include "core.h"
2424
#include "patch.h"
25+
#include "state.h"
2526
#include "transition.h"
2627

2728
/*
@@ -1009,6 +1010,13 @@ int klp_enable_patch(struct klp_patch *patch)
10091010

10101011
mutex_lock(&klp_mutex);
10111012

1013+
if (!klp_is_patch_compatible(patch)) {
1014+
pr_err("Livepatch patch (%s) is not compatible with the already installed livepatches.\n",
1015+
patch->mod->name);
1016+
mutex_unlock(&klp_mutex);
1017+
return -EINVAL;
1018+
}
1019+
10121020
ret = klp_init_patch_early(patch);
10131021
if (ret) {
10141022
mutex_unlock(&klp_mutex);

kernel/livepatch/state.c

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
#include <linux/livepatch.h>
1111
#include "core.h"
12+
#include "state.h"
1213
#include "transition.h"
1314

1415
#define klp_for_each_state(patch, state) \
@@ -81,3 +82,38 @@ struct klp_state *klp_get_prev_state(unsigned long id)
8182
return last_state;
8283
}
8384
EXPORT_SYMBOL_GPL(klp_get_prev_state);
85+
86+
/* Check if the patch is able to deal with the existing system state. */
87+
static bool klp_is_state_compatible(struct klp_patch *patch,
88+
struct klp_state *old_state)
89+
{
90+
struct klp_state *state;
91+
92+
state = klp_get_state(patch, old_state->id);
93+
94+
/* A cumulative livepatch must handle all already modified states. */
95+
if (!state)
96+
return !patch->replace;
97+
98+
return state->version >= old_state->version;
99+
}
100+
101+
/*
102+
* Check that the new livepatch will not break the existing system states.
103+
* Cumulative patches must handle all already modified states.
104+
* Non-cumulative patches can touch already modified states.
105+
*/
106+
bool klp_is_patch_compatible(struct klp_patch *patch)
107+
{
108+
struct klp_patch *old_patch;
109+
struct klp_state *old_state;
110+
111+
klp_for_each_patch(old_patch) {
112+
klp_for_each_state(old_patch, old_state) {
113+
if (!klp_is_state_compatible(patch, old_state))
114+
return false;
115+
}
116+
}
117+
118+
return true;
119+
}

kernel/livepatch/state.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/* SPDX-License-Identifier: GPL-2.0 */
2+
#ifndef _LIVEPATCH_STATE_H
3+
#define _LIVEPATCH_STATE_H
4+
5+
#include <linux/livepatch.h>
6+
7+
bool klp_is_patch_compatible(struct klp_patch *patch);
8+
9+
#endif /* _LIVEPATCH_STATE_H */

0 commit comments

Comments
 (0)