cumulative-patches.rst 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. ===================================
  2. Atomic Replace & Cumulative Patches
  3. ===================================
  4. There might be dependencies between livepatches. If multiple patches need
  5. to do different changes to the same function(s) then we need to define
  6. an order in which the patches will be installed. And function implementations
  7. from any newer livepatch must be done on top of the older ones.
  8. This might become a maintenance nightmare. Especially when more patches
  9. modified the same function in different ways.
  10. An elegant solution comes with the feature called "Atomic Replace". It allows
  11. creation of so called "Cumulative Patches". They include all wanted changes
  12. from all older livepatches and completely replace them in one transition.
  13. Usage
  14. -----
  15. The atomic replace can be enabled by setting "replace" flag in struct klp_patch,
  16. for example::
  17. static struct klp_patch patch = {
  18. .mod = THIS_MODULE,
  19. .objs = objs,
  20. .replace = true,
  21. };
  22. All processes are then migrated to use the code only from the new patch.
  23. Once the transition is finished, all older patches are automatically
  24. disabled.
  25. Ftrace handlers are transparently removed from functions that are no
  26. longer modified by the new cumulative patch.
  27. As a result, the livepatch authors might maintain sources only for one
  28. cumulative patch. It helps to keep the patch consistent while adding or
  29. removing various fixes or features.
  30. Users could keep only the last patch installed on the system after
  31. the transition to has finished. It helps to clearly see what code is
  32. actually in use. Also the livepatch might then be seen as a "normal"
  33. module that modifies the kernel behavior. The only difference is that
  34. it can be updated at runtime without breaking its functionality.
  35. Features
  36. --------
  37. The atomic replace allows:
  38. - Atomically revert some functions in a previous patch while
  39. upgrading other functions.
  40. - Remove eventual performance impact caused by core redirection
  41. for functions that are no longer patched.
  42. - Decrease user confusion about dependencies between livepatches.
  43. Limitations:
  44. ------------
  45. - Once the operation finishes, there is no straightforward way
  46. to reverse it and restore the replaced patches atomically.
  47. A good practice is to set .replace flag in any released livepatch.
  48. Then re-adding an older livepatch is equivalent to downgrading
  49. to that patch. This is safe as long as the livepatches do _not_ do
  50. extra modifications in (un)patching callbacks or in the module_init()
  51. or module_exit() functions, see below.
  52. Also note that the replaced patch can be removed and loaded again
  53. only when the transition was not forced.
  54. - Only the (un)patching callbacks from the _new_ cumulative livepatch are
  55. executed. Any callbacks from the replaced patches are ignored.
  56. In other words, the cumulative patch is responsible for doing any actions
  57. that are necessary to properly replace any older patch.
  58. As a result, it might be dangerous to replace newer cumulative patches by
  59. older ones. The old livepatches might not provide the necessary callbacks.
  60. This might be seen as a limitation in some scenarios. But it makes life
  61. easier in many others. Only the new cumulative livepatch knows what
  62. fixes/features are added/removed and what special actions are necessary
  63. for a smooth transition.
  64. In any case, it would be a nightmare to think about the order of
  65. the various callbacks and their interactions if the callbacks from all
  66. enabled patches were called.
  67. - There is no special handling of shadow variables. Livepatch authors
  68. must create their own rules how to pass them from one cumulative
  69. patch to the other. Especially that they should not blindly remove
  70. them in module_exit() functions.
  71. A good practice might be to remove shadow variables in the post-unpatch
  72. callback. It is called only when the livepatch is properly disabled.