1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * system_state.c - State of the system modified by livepatches
5 * Copyright (C) 2019 SUSE
8 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
10 #include <linux/livepatch.h>
13 #include "transition.h"
15 #define klp_for_each_state(patch, state) \
16 for (state = patch->states; state && state->id; state++)
19 * klp_get_state() - get information about system state modified by
21 * @patch: livepatch that modifies the given system state
22 * @id: custom identifier of the modified system state
24 * Checks whether the given patch modifies the given system state.
26 * The function can be called either from pre/post (un)patch
27 * callbacks or from the kernel code added by the livepatch.
29 * Return: pointer to struct klp_state when found, otherwise NULL.
31 struct klp_state *klp_get_state(struct klp_patch *patch, unsigned long id)
33 struct klp_state *state;
35 klp_for_each_state(patch, state) {
42 EXPORT_SYMBOL_GPL(klp_get_state);
45 * klp_get_prev_state() - get information about system state modified by
46 * the already installed livepatches
47 * @id: custom identifier of the modified system state
49 * Checks whether already installed livepatches modify the given
52 * The same system state can be modified by more non-cumulative
53 * livepatches. It is expected that the latest livepatch has
54 * the most up-to-date information.
56 * The function can be called only during transition when a new
57 * livepatch is being enabled or when such a transition is reverted.
58 * It is typically called only from pre/post (un)patch
61 * Return: pointer to the latest struct klp_state from already
62 * installed livepatches, NULL when not found.
64 struct klp_state *klp_get_prev_state(unsigned long id)
66 struct klp_patch *patch;
67 struct klp_state *state, *last_state = NULL;
69 if (WARN_ON_ONCE(!klp_transition_patch))
72 klp_for_each_patch(patch) {
73 if (patch == klp_transition_patch)
76 state = klp_get_state(patch, id);
84 EXPORT_SYMBOL_GPL(klp_get_prev_state);
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)
90 struct klp_state *state;
92 state = klp_get_state(patch, old_state->id);
94 /* A cumulative livepatch must handle all already modified states. */
96 return !patch->replace;
98 return state->version >= old_state->version;
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.
106 bool klp_is_patch_compatible(struct klp_patch *patch)
108 struct klp_patch *old_patch;
109 struct klp_state *old_state;
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))