Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags   |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0-or-later
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  * system_state.c - State of the system modified by livepatches
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  * Copyright (C) 2019 SUSE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8) #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #include <linux/livepatch.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include "core.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include "state.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include "transition.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #define klp_for_each_state(patch, state)		\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) 	for (state = patch->states; state && state->id; state++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19)  * klp_get_state() - get information about system state modified by
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20)  *	the given patch
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21)  * @patch:	livepatch that modifies the given system state
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22)  * @id:		custom identifier of the modified system state
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24)  * Checks whether the given patch modifies the given system state.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26)  * The function can be called either from pre/post (un)patch
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27)  * callbacks or from the kernel code added by the livepatch.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29)  * Return: pointer to struct klp_state when found, otherwise NULL.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) struct klp_state *klp_get_state(struct klp_patch *patch, unsigned long id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 	struct klp_state *state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 	klp_for_each_state(patch, state) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 		if (state->id == id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 			return state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 	return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) EXPORT_SYMBOL_GPL(klp_get_state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45)  * klp_get_prev_state() - get information about system state modified by
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46)  *	the already installed livepatches
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47)  * @id:		custom identifier of the modified system state
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49)  * Checks whether already installed livepatches modify the given
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50)  * system state.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52)  * The same system state can be modified by more non-cumulative
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53)  * livepatches. It is expected that the latest livepatch has
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54)  * the most up-to-date information.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56)  * The function can be called only during transition when a new
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57)  * livepatch is being enabled or when such a transition is reverted.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58)  * It is typically called only from pre/post (un)patch
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59)  * callbacks.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61)  * Return: pointer to the latest struct klp_state from already
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62)  *	installed livepatches, NULL when not found.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) struct klp_state *klp_get_prev_state(unsigned long id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 	struct klp_patch *patch;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 	struct klp_state *state, *last_state = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 	if (WARN_ON_ONCE(!klp_transition_patch))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 	klp_for_each_patch(patch) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 		if (patch == klp_transition_patch)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 		state = klp_get_state(patch, id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 		if (state)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 			last_state = state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 	return last_state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) EXPORT_SYMBOL_GPL(klp_get_prev_state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) /* Check if the patch is able to deal with the existing system state. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) static bool klp_is_state_compatible(struct klp_patch *patch,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 				    struct klp_state *old_state)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 	struct klp_state *state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 	state = klp_get_state(patch, old_state->id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 	/* A cumulative livepatch must handle all already modified states. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 	if (!state)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 		return !patch->replace;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 	return state->version >= old_state->version;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102)  * Check that the new livepatch will not break the existing system states.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103)  * Cumulative patches must handle all already modified states.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104)  * Non-cumulative patches can touch already modified states.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) bool klp_is_patch_compatible(struct klp_patch *patch)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 	struct klp_patch *old_patch;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 	struct klp_state *old_state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 	klp_for_each_patch(old_patch) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 		klp_for_each_state(old_patch, old_state) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 			if (!klp_is_state_compatible(patch, old_state))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 				return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 	return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) }