Skip to content

Commit

Permalink
livepatch: Basic API to track system state changes
Browse files Browse the repository at this point in the history
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/[email protected]
To: Jiri Kosina <[email protected]>
Cc: Kamalesh Babulal <[email protected]>
Cc: Nicolai Stange <[email protected]>
Cc: [email protected]
Cc: [email protected]
Acked-by: Miroslav Benes <[email protected]>
Acked-by: Joe Lawrence <[email protected]>
Acked-by: Josh Poimboeuf <[email protected]>
Signed-off-by: Petr Mladek <[email protected]>
  • Loading branch information
pmladek committed Nov 1, 2019
1 parent 7e35e4e commit 73727f4
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 1 deletion.
15 changes: 15 additions & 0 deletions include/linux/livepatch.h
Original file line number Diff line number Diff line change
Expand Up @@ -130,10 +130,21 @@ struct klp_object {
bool patched;
};

/**
* struct klp_state - state of the system modified by the livepatch
* @id: system state identifier (non-zero)
* @data: custom data
*/
struct klp_state {
unsigned long id;
void *data;
};

/**
* struct klp_patch - patch structure for live patching
* @mod: reference to the live patch module
* @objs: object entries for kernel objects to be patched
* @states: system states that can get modified
* @replace: replace all actively used patches
* @list: list node for global list of actively used patches
* @kobj: kobject for sysfs resources
Expand All @@ -147,6 +158,7 @@ struct klp_patch {
/* external */
struct module *mod;
struct klp_object *objs;
struct klp_state *states;
bool replace;

/* internal */
Expand Down Expand Up @@ -217,6 +229,9 @@ void *klp_shadow_get_or_alloc(void *obj, unsigned long id,
void klp_shadow_free(void *obj, unsigned long id, klp_shadow_dtor_t dtor);
void klp_shadow_free_all(unsigned long id, klp_shadow_dtor_t dtor);

struct klp_state *klp_get_state(struct klp_patch *patch, unsigned long id);
struct klp_state *klp_get_prev_state(unsigned long id);

#else /* !CONFIG_LIVEPATCH */

static inline int klp_module_coming(struct module *mod) { return 0; }
Expand Down
2 changes: 1 addition & 1 deletion kernel/livepatch/Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# SPDX-License-Identifier: GPL-2.0-only
obj-$(CONFIG_LIVEPATCH) += livepatch.o

livepatch-objs := core.o patch.o shadow.o transition.o
livepatch-objs := core.o patch.o shadow.o state.o transition.o
83 changes: 83 additions & 0 deletions kernel/livepatch/state.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
// SPDX-License-Identifier: GPL-2.0-or-later
/*
* system_state.c - State of the system modified by livepatches
*
* Copyright (C) 2019 SUSE
*/

#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt

#include <linux/livepatch.h>
#include "core.h"
#include "transition.h"

#define klp_for_each_state(patch, state) \
for (state = patch->states; state && state->id; state++)

/**
* klp_get_state() - get information about system state modified by
* the given patch
* @patch: livepatch that modifies the given system state
* @id: custom identifier of the modified system state
*
* Checks whether the given patch modifies the given system state.
*
* The function can be called either from pre/post (un)patch
* callbacks or from the kernel code added by the livepatch.
*
* Return: pointer to struct klp_state when found, otherwise NULL.
*/
struct klp_state *klp_get_state(struct klp_patch *patch, unsigned long id)
{
struct klp_state *state;

klp_for_each_state(patch, state) {
if (state->id == id)
return state;
}

return NULL;
}
EXPORT_SYMBOL_GPL(klp_get_state);

/**
* klp_get_prev_state() - get information about system state modified by
* the already installed livepatches
* @id: custom identifier of the modified system state
*
* Checks whether already installed livepatches modify the given
* system state.
*
* The same system state can be modified by more non-cumulative
* livepatches. It is expected that the latest livepatch has
* the most up-to-date information.
*
* The function can be called only during transition when a new
* livepatch is being enabled or when such a transition is reverted.
* It is typically called only from from pre/post (un)patch
* callbacks.
*
* Return: pointer to the latest struct klp_state from already
* installed livepatches, NULL when not found.
*/
struct klp_state *klp_get_prev_state(unsigned long id)
{
struct klp_patch *patch;
struct klp_state *state, *last_state = NULL;

if (WARN_ON_ONCE(!klp_transition_patch))
return NULL;

klp_for_each_patch(patch) {
if (patch == klp_transition_patch)
goto out;

state = klp_get_state(patch, id);
if (state)
last_state = state;
}

out:
return last_state;
}
EXPORT_SYMBOL_GPL(klp_get_prev_state);

0 comments on commit 73727f4

Please sign in to comment.