[PATCH v2 0/4] kprobes: permit use without modules

From: Mark Rutland
Date: Wed Apr 03 2024 - 11:02:10 EST


Currently KPROBES depends on MODULES and cannot be built when support
for modules is not enabled. This is largely an artifact of the
default/generic code for allocating insn pages using module_alloc(),
though several architectures do not use this and have no strict
dependency on MODULES. It would be nice to allow KPROBES to be used
without MODULES, as this can be useful for testing and/or in certain
constrained environments.

This series (based on v6.9-rc1) removes the artificial dependency on
MODULES. This permits (but does not require) that architectures which
don't use module_alloc() to allocate kprobe insn pages can support
kprobes when module support is not enabled.

The series deliberately avoids adding a common text allocator, as the
requirements for allocating kprobe memory van vary by architecture, and
can differ from other text allocations. However, architectures can
easily call a common allocator if they wish, and this series does not
preclude using common allocators immediately or in future.

The key change is in patch 3. This requires that architectures which
provide their own alloc function must provide the corresponding free
function and select HAVE_KPROBES_ALLOC with any appropriate dependencies
for their implementation. Architectures which use the generic functions
are left as-is with a dependency on MODULES.

The final patch allows the core kprobes code to be built without
MODULES, and removes the explicit dependency from Kconfig. This is
derived from Jarkko's recent v6 attempt:

https://lore.kernel.org/lkml/20240326012102.27438-1-jarkko@xxxxxxxxxx/

With the series applied, arm64 and riscv can enable KPROBES without
MODULES, while powerpc/s390/x86 are still depend on MODULES as their
alloc functions currently use module_alloc(), and all other
architectures with KPROBES uses the generic implementation that depends
on MODULES. I believe it should be relatively easy to enable
powerpc/s390/x86 to not depend on MODULES.

Since v1 [1]:
* Rebase to v6.9-rc2 (trivial)
* Update to Jarkko's v7 cleanups for core kprobes
* Fix accidental use of KPROBES_USE_MODULE_ALLOC
* Improve commit messages
* Update Cc lists

[1] https://lore.kernel.org/lkml/20240326163624.3253157-1-mark.rutland@xxxxxxx/

Mark.

Jarkko Sakkinen (1):
kprobes: Remove core dependency on modules

Mark Rutland (3):
arm64: patching: always use fixmap
kprobes/treewide: Add kprobes_ prefix to insn alloc/free functions
kprobes/treewide: Explicitly override alloc/free functions

arch/Kconfig | 5 ++-
arch/arm64/Kconfig | 1 +
arch/arm64/kernel/patching.c | 10 ++---
arch/arm64/kernel/probes/kprobes.c | 7 ++-
arch/powerpc/Kconfig | 3 +-
arch/powerpc/kernel/kprobes.c | 7 ++-
arch/powerpc/kernel/optprobes.c | 4 +-
arch/riscv/Kconfig | 1 +
arch/riscv/kernel/probes/kprobes.c | 7 ++-
arch/s390/Kconfig | 3 +-
arch/s390/kernel/kprobes.c | 7 ++-
arch/x86/Kconfig | 3 +-
arch/x86/kernel/kprobes/core.c | 7 ++-
include/linux/kprobes.h | 7 +--
kernel/kprobes.c | 68 +++++++++++++++++-------------
kernel/trace/trace_kprobe.c | 15 ++++++-
16 files changed, 103 insertions(+), 52 deletions(-)

--
2.30.2