state.c 3.1 KB

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