[PATCH 3/8] selftests: livepatch: test-kprobe: Check if kprobes can work with livepatches

Marcos Paulo de Souza
2026-03-13T20:58:55Z

Running the upstream selftests on older kernels can presente some issues
regarding features being not present. One of such issues if the missing
capability of having both kprobes and livepatches on the same function.

The support was introduced in commit 0bc11ed5ab60c
("kprobes: Allow kprobes coexist with livepatch"), which means that older
kernels may lack this change.

The lack of this feature can be checked when a kprobe without a
post_handler is loaded and checking that the enabled_function's file
shows the flag "I". A kernel with the proper support for kprobes and
livepatches would presente the flag only when a post_handler is
registered.

No functional changes.

Signed-off-by: Marcos Paulo de Souza <mpdesouza@suse.com>
---
tools/testing/selftests/livepatch/test-kprobe.sh | 52 ++++++++++++++----------
1 file changed, 31 insertions(+), 21 deletions(-)

diff --git a/tools/testing/selftests/livepatch/test-kprobe.sh b/tools/testing/selftests/livepatch/test-kprobe.sh
index cdf31d0e51955..44cd16156dbd4 100755
--- a/tools/testing/selftests/livepatch/test-kprobe.sh
+++ b/tools/testing/selftests/livepatch/test-kprobe.sh
@@ -16,30 +16,19 @@ setup_config
# when it uses a post_handler since only one IPMODIFY maybe be registered
# to any given function at a time.

-start_test "livepatch interaction with kprobed function with post_handler"
-
-echo 1 > "$SYSFS_KPROBES_DIR/enabled"
-
-load_mod $MOD_KPROBE has_post_handler=1
-load_failing_mod $MOD_LIVEPATCH
-unload_mod $MOD_KPROBE
-
-check_result "% insmod test_modules/test_klp_kprobe.ko has_post_handler=1
-% insmod test_modules/$MOD_LIVEPATCH.ko
-livepatch: enabling patch '$MOD_LIVEPATCH'
-livepatch: '$MOD_LIVEPATCH': initializing patching transition
-livepatch: failed to register ftrace handler for function 'cmdline_proc_show' (-16)
-livepatch: failed to patch object 'vmlinux'
-livepatch: failed to enable patch '$MOD_LIVEPATCH'
-livepatch: '$MOD_LIVEPATCH': canceling patching transition, going to unpatch
-livepatch: '$MOD_LIVEPATCH': completing unpatching transition
-livepatch: '$MOD_LIVEPATCH': unpatching complete
-insmod: ERROR: could not insert module test_modules/$MOD_LIVEPATCH.ko: Device or resource busy
-% rmmod test_klp_kprobe"
-
start_test "livepatch interaction with kprobed function without post_handler"

load_mod $MOD_KPROBE has_post_handler=0
+
+# Check if commit 0bc11ed5ab60c ("kprobes: Allow kprobes coexist with livepatch")
+# is missing, meaning that livepatches and kprobes can't be used together.
+# When the commit is missing, kprobes always set IPMODIFY (the I flag), even
+# when the post handler is missing.
+if grep --quiet ") R I" "$SYSFS_DEBUG_DIR/tracing/enabled_functions"; then
+ unload_mod $MOD_KPROBE
+ skip "Running kernel doesn't support kprobes along livepatches"
+fi
+
load_lp $MOD_LIVEPATCH

unload_mod $MOD_KPROBE
@@ -61,4 +50,25 @@ livepatch: '$MOD_LIVEPATCH': completing unpatching transition
livepatch: '$MOD_LIVEPATCH': unpatching complete
% rmmod $MOD_LIVEPATCH"

+start_test "livepatch interaction with kprobed function with post_handler"
+
+echo 1 > "$SYSFS_KPROBES_DIR/enabled"
+
+load_mod $MOD_KPROBE has_post_handler=1
+load_failing_mod $MOD_LIVEPATCH
+unload_mod $MOD_KPROBE
+
+check_result "% insmod test_modules/test_klp_kprobe.ko has_post_handler=1
+% insmod test_modules/$MOD_LIVEPATCH.ko
+livepatch: enabling patch '$MOD_LIVEPATCH'
+livepatch: '$MOD_LIVEPATCH': initializing patching transition
+livepatch: failed to register ftrace handler for function 'cmdline_proc_show' (-16)
+livepatch: failed to patch object 'vmlinux'
+livepatch: failed to enable patch '$MOD_LIVEPATCH'
+livepatch: '$MOD_LIVEPATCH': canceling patching transition, going to unpatch
+livepatch: '$MOD_LIVEPATCH': completing unpatching transition
+livepatch: '$MOD_LIVEPATCH': unpatching complete
+insmod: ERROR: could not insert module test_modules/$MOD_LIVEPATCH.ko: Device or resource busy
+% rmmod test_klp_kprobe"
+
exit 0

--
2.52.0