Skip to content

livepatch: Split livepatch module per-object

Petr Mladek live-patching lkml oe-kbuild-all 1 version In review

Get series with b4
b4 am [email protected]

Versions newest first

  1. v1 Latest23 patches 13 repliesIn review

Patches in v1 No applies recorded yet

#Subject and filesSizeRepliesApplied
1/23module: Allow to delete module also from inside kernelinclude/linux/module.h kernel/module.c+80 -441 reply
2/23livepatch: Split livepatch modules per livepatched objectarch/x86/kernel/livepatch.c include/linux/livepatch.h kernel/livepatch/core.c +24 more+841 -4852 replies
3/23livepatch: Better checks of struct klp_object definitionkernel/livepatch/core.c+82 -91 reply
4/23livepatch: Prevent loading livepatch sub-module unintentionally.kernel/livepatch/core.c+77 -11 reply
5/23livepatch: Initialize and free livepatch submodulekernel/livepatch/core.c+27 -71 reply
6/23livepatch: Enable the livepatch submodulekernel/livepatch/core.c+22 -00 replies
7/23livepatch: Remove obsolete functionality from klp_module_coming()kernel/livepatch/core.c+0 -590 replies
8/23livepatch: Automatically load livepatch module when the patch module is loadedkernel/livepatch/core.c tools/testing/selftests/livepatch/test-callbacks.sh+77 -50 replies
9/23livepatch: Handle race when livepatches are reloaded during a module loadinclude/linux/livepatch.h kernel/livepatch/core.c+7 -41 reply
10/23livepatch: Handle modprobe exit codekernel/livepatch/core.c tools/testing/selftests/livepatch/test-callbacks.sh+4 -10 replies
11/23livepatch: Safely detect forced transition when removing split livepatch modulesinclude/linux/livepatch.h kernel/livepatch/core.c kernel/livepatch/transition.c+16 -71 reply
12/23livepatch: Automatically remove livepatch module when the object is freedinclude/linux/livepatch.h kernel/livepatch/core.c+22 -10 replies
13/23livepatch: Remove livepatch module when the livepatched module is unloadedkernel/livepatch/core.c+31 -530 replies
14/23livepatch: Never block livepatch modules when the related module is being removedkernel/livepatch/core.c+7 -00 replies
15/23livepatch: Prevent infinite loop when loading livepatch modulekernel/livepatch/core.c+32 -00 replies
16/23livepatch: Add patch into the global list earlykernel/livepatch/core.c+6 -30 replies
17/23livepatch: Load livepatches for modules when loading the main livepatchkernel/livepatch/core.c+79 -10 replies
18/23module: Refactor add_unformed_module()kernel/module.c+15 -130 replies
19/23module/livepatch: Allow to use exported symbols from livepatch module for "vmlinux"include/linux/livepatch.h kernel/livepatch/core.c kernel/module.c+34 -03 replies
20/23module/livepatch: Relocate local variables in the module loaded when the livepatch is being loadedinclude/linux/livepatch.h kernel/livepatch/core.c kernel/module.c+18 -641 reply
21/23livepatch: Remove obsolete arch_klp_init_object_loaded()Documentation/livepatch/module-elf-format.rst arch/x86/kernel/Makefile arch/x86/kernel/livepatch.c +2 more+0 -950 replies
22/23livepatch/module: Remove obsolete copy_module_elf()Documentation/livepatch/module-elf-format.rst include/linux/module.h kernel/module.c+18 -901 reply
23/23module: Remove obsolete module_disable_ro()include/linux/module.h kernel/module.c+0 -150 replies

Reviews trailers per patch

No Reviewed-by, Acked-by or Tested-by trailers for v1 yet.

Read the v1 thread

Cover letter [POC 00/23] livepatch: Split livepatch module per-object

Hi,

first, do not get scared by the size of the patchset. There are only few patches that are really complicated and need attention at this stage. I just wanted to split it as much as possible to review and discuss each change separately.

Now to the problem. There are long term complains about maintainability of the arch-specific code that is needed to livepatch modules that are loaded after the livepatch itself.

There was always an idea about splitting the livepatch module per-livepatched object. One interesting approach was drafted on the last Livepatch microconference at Linux Plubmers 2019.

I played with the idea and came up with this POC. Of course, there are pros and cons.

On the positive note:

+ The approach seems to work.

+ The same scenarios are supported. It is even newly possible to use the livepatch-specific relocations and reload the livepatched module.

+ The livepatch-specific relocations are still needed but they are handled together with other relocations. As a result, the other code modifications work out of box, e.g. alternatives, parainstructions.

+ Some problematic code could get removed (last 4 patches):

+ module_disable_ro()
+ arch_klp_init_object_loaded()
+ copy_module_elf()

+ The amount if livepatch-specific hooks in the module loader is about the same. They are _not_ longer arch-specific. But they are a bit tricky, see negatives below.

On the negative side:

+ It adds dependency on userspace tool "modprobe" called via usermodhelper. It brings several new problems:

+ How to distinguish modprobe called by user or by kernel when resolving races and errors.

+ How to pass the real error code to the usermodhelper caller.

+ Automatic dependencies are generated and handled in the userspace. Might create unwanted cyclic load. It requires crazy workarounds from the kernel side, see the patch 19.

+ There is a new bunch of races that sometimes need a tricky solution. For example, see the patches 8, 9, 15.

+ It might be slightly more complicated to prepare and use the livepatches. There are more modules that need to built and are visibly to the administrators. Also it complicates sharing some common helper functionality.

From my point of view. The new code is much less arch-dependent and more self-contained. Therefore it should be easier to maintain in the long term.

On the other hand, it is more tricky regarding possible races and infinite loops. They are not always easy to solve because of "modprobe" called via userspace and because of more switches between klp_mutex and module_mutex guarded code. Anyway, once this is solved, it should just work for a long time as is.

All in all, I think that this approach is worth exploring. I am curious about your opinion.

Best Regards,
Petr

PS: The patchset applies against Linus' master (v5.5-rc6).

Petr Mladek (23):
module: Allow to delete module also from inside kernel
livepatch: Split livepatch modules per livepatched object
livepatch: Better checks of struct klp_object definition
livepatch: Prevent loading livepatch sub-module unintentionally.
livepatch: Initialize and free livepatch submodule
livepatch: Enable the livepatch submodule
livepatch: Remove obsolete functionality from klp_module_coming()
livepatch: Automatically load livepatch module when the patch module
is loaded
livepatch: Handle race when livepatches are reloaded during a module
load
livepatch: Handle modprobe exit code
livepatch: Safely detect forced transition when removing split
livepatch modules
livepatch: Automatically remove livepatch module when the object is
freed
livepatch: Remove livepatch module when the livepatched module is
unloaded
livepatch: Never block livepatch modules when the related module is
being removed
livepatch: Prevent infinite loop when loading livepatch module
livepatch: Add patch into the global list early
livepatch: Load livepatches for modules when loading the main
livepatch
module: Refactor add_unformed_module()
module/livepatch: Allow to use exported symbols from livepatch module
for "vmlinux"
module/livepatch: Relocate local variables in the module loaded when
the livepatch is being loaded
livepatch: Remove obsolete arch_klp_init_object_loaded() livepatch/module: Remove obsolete copy_module_elf()
module: Remove obsolete module_disable_ro()

 Documentation/livepatch/module-elf-format.rst      |  15 +-
 arch/x86/kernel/Makefile                           |   1 -
 arch/x86/kernel/livepatch.c                        |  53 --
 include/linux/livepatch.h                          |  36 +-
 include/linux/module.h                             |  10 +-
 kernel/livepatch/core.c                            | 743 ++++++++++++++-------
 kernel/livepatch/core.h                            |   5 -
 kernel/livepatch/transition.c                      |  22 +-
 kernel/module.c                                    | 279 ++++----
 lib/livepatch/Makefile                             |   2 +
 lib/livepatch/test_klp_atomic_replace.c            |  18 +-
 lib/livepatch/test_klp_callbacks_demo.c            |  90 ++-
 lib/livepatch/test_klp_callbacks_demo.h            |  11 +
 lib/livepatch/test_klp_callbacks_demo2.c           |  62 +-
 lib/livepatch/test_klp_callbacks_demo2.h           |  11 +
 ...t_klp_callbacks_demo__test_klp_callbacks_busy.c |  50 ++
 ...st_klp_callbacks_demo__test_klp_callbacks_mod.c |  42 ++
 lib/livepatch/test_klp_livepatch.c                 |  18 +-
 lib/livepatch/test_klp_state.c                     |  53 +-
 lib/livepatch/test_klp_state2.c                    |  53 +-
 samples/livepatch/Makefile                         |   4 +
 samples/livepatch/livepatch-callbacks-demo.c       |  90 ++-
 samples/livepatch/livepatch-callbacks-demo.h       |  11 +
 ...h-callbacks-demo__livepatch-callbacks-busymod.c |  54 ++
 ...patch-callbacks-demo__livepatch-callbacks-mod.c |  46 ++
 samples/livepatch/livepatch-sample.c               |  18 +-
 samples/livepatch/livepatch-shadow-fix1.c          | 120 +---
 .../livepatch-shadow-fix1__livepatch-shadow-mod.c  | 155 +++++
 samples/livepatch/livepatch-shadow-fix2.c          |  92 +--
 .../livepatch-shadow-fix2__livepatch-shadow-mod.c  | 127 ++++
 .../testing/selftests/livepatch/test-callbacks.sh  |  19 +-
 31 files changed, 1424 insertions(+), 886 deletions(-)
 delete mode 100644 arch/x86/kernel/livepatch.c
 create mode 100644 lib/livepatch/test_klp_callbacks_demo.h
 create mode 100644 lib/livepatch/test_klp_callbacks_demo2.h
 create mode 100644 lib/livepatch/test_klp_callbacks_demo__test_klp_callbacks_busy.c
 create mode 100644 lib/livepatch/test_klp_callbacks_demo__test_klp_callbacks_mod.c
 create mode 100644 samples/livepatch/livepatch-callbacks-demo.h
 create mode 100644 samples/livepatch/livepatch-callbacks-demo__livepatch-callbacks-busymod.c
 create mode 100644 samples/livepatch/livepatch-callbacks-demo__livepatch-callbacks-mod.c
 create mode 100644 samples/livepatch/livepatch-shadow-fix1__livepatch-shadow-mod.c
 create mode 100644 samples/livepatch/livepatch-shadow-fix2__livepatch-shadow-mod.c
Signature · 1 line
2.16.4