Skip to contentSkip to conversation

selftests: livepatch: test livepatching a kprobed function

PATCH Superseded Michael Vetter 5 participants 11 messages Sep 20, 2024 – Sep 27, 2024

Subsystems (2)

v3 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

v3 cover selftests: livepatch: test livepatching a kprobed function

This patchset adds a test for livepatching a kprobed function.

Thanks to Petr and Marcos for the reviews!

V3:
Save and restore kprobe state also when test fails, by integrating it into setup_config() and cleanup().
Rename SYSFS variables in a more logical way.
Sort test modules in alphabetical order.
Rename module description.

V2:
Save and restore kprobe state.

Michael Vetter (3):
selftests: livepatch: rename KLP_SYSFS_DIR to SYSFS_KLP_DIR
selftests: livepatch: save and restore kprobe state
selftests: livepatch: test livepatching a kprobed function

 tools/testing/selftests/livepatch/Makefile    |  3 +-
 .../testing/selftests/livepatch/functions.sh  | 13 +++-
 .../selftests/livepatch/test-kprobe.sh        | 62 +++++++++++++++++++
 .../selftests/livepatch/test_modules/Makefile |  3 +-
 .../livepatch/test_modules/test_klp_kprobe.c  | 38 ++++++++++++
 5 files changed, 114 insertions(+), 5 deletions(-)
 create mode 100755 tools/testing/selftests/livepatch/test-kprobe.sh
 create mode 100644 tools/testing/selftests/livepatch/test_modules/test_klp_kprobe.c
Signature · 1 line
2.46.0
#1 lore raw

PATCH v3 1/3 selftests: livepatch: rename KLP_SYSFS_DIR to SYSFS_KLP_DIR

This naming makes more sense according to the directory structure. Especially when we later add more paths.

---
 tools/testing/selftests/livepatch/functions.sh | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
tools/testing/selftests/livepatch/functions.sh +3 -3
index fc4c6a016d38..50361fceff06 100644
@@ -6,7 +6,7 @@
 
 MAX_RETRIES=600
 RETRY_INTERVAL=".1"	# seconds
-KLP_SYSFS_DIR="/sys/kernel/livepatch"
+SYSFS_KLP_DIR="/sys/kernel/livepatch"
 
 # Kselftest framework requirement - SKIP code is 4
 ksft_skip=4
@@ -322,7 +322,7 @@ function check_sysfs_rights() {
 	local rel_path="$1"; shift
 	local expected_rights="$1"; shift
 
-	local path="$KLP_SYSFS_DIR/$mod/$rel_path"
+	local path="$SYSFS_KLP_DIR/$mod/$rel_path"
 	local rights=$(/bin/stat --format '%A' "$path")
 	if test "$rights" != "$expected_rights" ; then
 		die "Unexpected access rights of $path: $expected_rights vs. $rights"
@@ -338,7 +338,7 @@ function check_sysfs_value() {
 	local rel_path="$1"; shift
 	local expected_value="$1"; shift
 
-	local path="$KLP_SYSFS_DIR/$mod/$rel_path"
+	local path="$SYSFS_KLP_DIR/$mod/$rel_path"
 	local value=`cat $path`
 	if test "$value" != "$expected_value" ; then
 		die "Unexpected value in $path: $expected_value vs. $value"
Signature · 1 line
2.46.0
#3 lore raw

PATCH v3 2/3 selftests: livepatch: save and restore kprobe state

In reply to Michael Vetter

Save the state of /sys/kernel/livepatch/debug/kprobes/enabled during setup_config() and restore it during cleanup().

This is in preparation for a future commit that will add a test that should confirm that we cannot livepatch a kprobed function if that kprobe has a post handler.

---
 tools/testing/selftests/livepatch/functions.sh | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)
tools/testing/selftests/livepatch/functions.sh +8 -1
index 50361fceff06..6684c01c0567 100644
@@ -6,7 +6,10 @@
 
 MAX_RETRIES=600
 RETRY_INTERVAL=".1"	# seconds
-SYSFS_KLP_DIR="/sys/kernel/livepatch"
+SYSFS_KERNEL_DIR="/sys/kernel"
+SYSFS_KLP_DIR="$SYSFS_KERNEL_DIR/livepatch"
+SYSFS_DEBUG_DIR="$SYSFS_KERNEL_DIR/debug"
+SYSFS_KPROBES_DIR="$SYSFS_DEBUG_DIR/kprobes"
 
 # Kselftest framework requirement - SKIP code is 4
 ksft_skip=4
@@ -58,6 +61,7 @@ function push_config() {
 	DYNAMIC_DEBUG=$(grep '^kernel/livepatch' /sys/kernel/debug/dynamic_debug/control | \
 			awk -F'[: ]' '{print "file " $1 " line " $2 " " $4}')
 	FTRACE_ENABLED=$(sysctl --values kernel.ftrace_enabled)
+	KPROBE_ENABLED=$(cat $SYSFS_KPROBES_DIR/enabled)
 }
 
 function pop_config() {
@@ -67,6 +71,9 @@ function pop_config() {
 	if [[ -n "$FTRACE_ENABLED" ]]; then
 		sysctl kernel.ftrace_enabled="$FTRACE_ENABLED" &> /dev/null
 	fi
+	if [[ -n "$KPROBE_ENABLED" ]]; then
+		echo "$KPROBE_ENABLED" > "$SYSFS_KPROBES_DIR/enabled"
+	fi
 }
 
 function set_dynamic_debug() {
Signature · 1 line
2.46.0
maintainer
#4 lore raw

Re: [PATCH v3 2/3] selftests: livepatch: save and restore kprobe state

Hi,

Quoted 1 line · Michael Vetter

Save the state of /sys/kernel/livepatch/debug/kprobes/enabled

I believe it is /sys/kernel/debug/kprobes/enabled.

Quoted 16 lines

during setup_config() and restore it during cleanup().

This is in preparation for a future commit that will add a test that should confirm that we cannot livepatch a kprobed function if that kprobe has a post handler.

---
 tools/testing/selftests/livepatch/functions.sh | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)
tools/testing/selftests/livepatch/functions.sh +0 -0
index 50361fceff06..6684c01c0567 100644

tools/testing/selftests/livepatch/functions.sh line 12

Quoted 8 lines
tools/testing/selftests/livepatch/functions.sh +4 -1
@@ -6,7 +6,10 @@
 
 MAX_RETRIES=600
 RETRY_INTERVAL=".1"	# seconds
-SYSFS_KLP_DIR="/sys/kernel/livepatch"
+SYSFS_KERNEL_DIR="/sys/kernel"
+SYSFS_KLP_DIR="$SYSFS_KERNEL_DIR/livepatch"
+SYSFS_DEBUG_DIR="$SYSFS_KERNEL_DIR/debug"
+SYSFS_KPROBES_DIR="$SYSFS_DEBUG_DIR/kprobes"

Personally I find this harder to read but it is a nit and Petr asked you to do it, but

tools/testing/selftests/livepatch/functions.sh function push_config() { · line 61

Quoted 4 lines
tools/testing/selftests/livepatch/functions.sh +0 -0
 # Kselftest framework requirement - SKIP code is 4
 ksft_skip=4
@@ -58,6 +61,7 @@ function push_config() {
 	DYNAMIC_DEBUG=$(grep '^kernel/livepatch' /sys/kernel/debug/dynamic_debug/control | \

it stays opencoded here and also elsewhere which is not great. Could it be consistent, please?

Otherwise I would just squash the patch to the next one.

Thank you,
Miroslav

#5 lore raw

PATCH v3 3/3 selftests: livepatch: test livepatching a kprobed function

In reply to Michael Vetter

The test proves that a function that is being kprobed and uses a post_handler cannot be livepatched.

Only one ftrace_ops with FTRACE_OPS_FL_IPMODIFY set may be registered to any given function at a time.

---
 tools/testing/selftests/livepatch/Makefile    |  3 +-
 .../selftests/livepatch/test-kprobe.sh        | 62 +++++++++++++++++++
 .../selftests/livepatch/test_modules/Makefile |  3 +-
 .../livepatch/test_modules/test_klp_kprobe.c  | 38 ++++++++++++
 4 files changed, 104 insertions(+), 2 deletions(-)
 create mode 100755 tools/testing/selftests/livepatch/test-kprobe.sh
 create mode 100644 tools/testing/selftests/livepatch/test_modules/test_klp_kprobe.c
tools/testing/selftests/livepatch/Makefile +2 -1
index 35418a4790be..a080eb54a215 100644
@@ -10,7 +10,8 @@ TEST_PROGS := \
 	test-state.sh \
 	test-ftrace.sh \
 	test-sysfs.sh \
-	test-syscall.sh
+	test-syscall.sh \
+	test-kprobe.sh
 
 TEST_FILES := settings
 
tools/testing/selftests/livepatch/test-kprobe.sh new file +62 -0

62 lines not on this page · raw message

tools/testing/selftests/livepatch/test_modules/Makefile +2 -1
index e6e638c4bcba..939230e571f5 100644
@@ -6,11 +6,12 @@ obj-m += test_klp_atomic_replace.o \
 	test_klp_callbacks_demo.o \
 	test_klp_callbacks_demo2.o \
 	test_klp_callbacks_mod.o \
+	test_klp_kprobe.o \
 	test_klp_livepatch.o \
+	test_klp_shadow_vars.o \
 	test_klp_state.o \
 	test_klp_state2.o \
 	test_klp_state3.o \
-	test_klp_shadow_vars.o \
 	test_klp_syscall.o
 
 # Ensure that KDIR exists, otherwise skip the compilation
tools/testing/selftests/livepatch/test_modules/test_klp_kprobe.c new file +38 -0
new file mode 100644
index 000000000000..67a8d29012f6
@@ -0,0 +1,38 @@
+// SPDX-License-Identifier: GPL-2.0
+// Copyright (C) 2024 Marcos Paulo de Souza <[email protected]>
+// Copyright (C) 2024 Michael Vetter <[email protected]>
+
+#include <linux/kernel.h>
+#include <linux/module.h>
+#include <linux/kprobes.h>
+
+static bool has_post_handler = true;
+module_param(has_post_handler, bool, 0444);
+
+static void __kprobes post_handler(struct kprobe *p, struct pt_regs *regs,
+				unsigned long flags)
+{
+}
+
+static struct kprobe kp = {
+	.symbol_name = "cmdline_proc_show",
+};
+
+static int __init kprobe_init(void)
+{
+	if (has_post_handler)
+		kp.post_handler = post_handler;
+
+	return register_kprobe(&kp);
+}
+
+static void __exit kprobe_exit(void)
+{
+	unregister_kprobe(&kp);
+}
+
+module_init(kprobe_init)
+module_exit(kprobe_exit)
+MODULE_LICENSE("GPL");
+MODULE_AUTHOR("Michael Vetter <[email protected]>");
+MODULE_DESCRIPTION("Livepatch test: kprobe function");
Signature · 1 line
2.46.0
maintainer
#6 lore raw

Hi,

tools/testing/selftests/livepatch/test-kprobe.sh line 19

Quoted 7 lines · Michael Vetter
tools/testing/selftests/livepatch/test-kprobe.sh new file +7 -0
+# Kprobe a function and verify that we can't livepatch that same function
+# 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 > /sys/kernel/debug/kprobes/enabled

opencoded here again.

The rest looks good to me.

Miroslav

Reviewed-by on the v3 series
#7 lore raw
In reply to Michael Vetter
Quoted 11 lines · Michael Vetter

This patchset adds a test for livepatching a kprobed function.

Thanks to Petr and Marcos for the reviews!

V3:
Save and restore kprobe state also when test fails, by integrating it into setup_config() and cleanup().
Rename SYSFS variables in a more logical way.
Sort test modules in alphabetical order.
Rename module description.

Quoted 8 lines

V2:
Save and restore kprobe state.

Michael Vetter (3):
  selftests: livepatch: rename KLP_SYSFS_DIR to SYSFS_KLP_DIR
  selftests: livepatch: save and restore kprobe state
  selftests: livepatch: test livepatching a kprobed function

Thanks for the new version! LGTM, so the series is

Quoted 11 lines

 tools/testing/selftests/livepatch/Makefile    |  3 +- .../testing/selftests/livepatch/functions.sh  | 13 +++- .../selftests/livepatch/test-kprobe.sh        | 62
+++++++++++++++++++
 .../selftests/livepatch/test_modules/Makefile |  3 +- .../livepatch/test_modules/test_klp_kprobe.c  | 38 ++++++++++++

 5 files changed, 114 insertions(+), 5 deletions(-)

 create mode 100755 tools/testing/selftests/livepatch/test-kprobe.sh create mode 100644 tools/testing/selftests/livepatch/test_modules/test_klp_kprobe.c

maintainer Acked-by
#8 lore raw
Quoted 21 lines · Marcos Paulo de Souza
Quoted 11 lines · Michael Vetter

This patchset adds a test for livepatching a kprobed function.

Thanks to Petr and Marcos for the reviews!

V3:
Save and restore kprobe state also when test fails, by integrating it into setup_config() and cleanup().
Rename SYSFS variables in a more logical way.
Sort test modules in alphabetical order.
Rename module description.

Quoted 8 lines

V2:
Save and restore kprobe state.

Michael Vetter (3):
  selftests: livepatch: rename KLP_SYSFS_DIR to SYSFS_KLP_DIR
  selftests: livepatch: save and restore kprobe state
  selftests: livepatch: test livepatching a kprobed function

Quoted 16 lines

Thanks for the new version! LGTM, so the series is

Quoted 11 lines
  tools/testing/selftests/livepatch/Makefile    |  3 +-
  .../testing/selftests/livepatch/functions.sh  | 13 +++-
  .../selftests/livepatch/test-kprobe.sh        | 62
+++++++++++++++++++
  .../selftests/livepatch/test_modules/Makefile |  3 +-
  .../livepatch/test_modules/test_klp_kprobe.c  | 38 ++++++++++++
  5 files changed, 114 insertions(+), 5 deletions(-)

 create mode 100755 tools/testing/selftests/livepatch/test-kprobe.sh create mode 100644
tools/testing/selftests/livepatch/test_modules/test_klp_kprobe.c

Assuming this is going through livepatch tree:

thanks,
-- Shuah

maintainer
#9 lore raw
Quoted 4 lines · Shuah Khan
Quoted 3 lines · Marcos Paulo de Souza
Quoted 2 lines · Michael Vetter

This patchset adds a test for livepatching a kprobed function.

Quoted 8 lines
Quoted 6 lines
Quoted 5 lines

Michael Vetter (3):
  selftests: livepatch: rename KLP_SYSFS_DIR to SYSFS_KLP_DIR
  selftests: livepatch: save and restore kprobe state
  selftests: livepatch: test livepatching a kprobed function

Assuming this is going through livepatch tree:

Yup, I am going to take it via the livepatch tree. Though I am going to wait for more potential feedback a week or so.

Quoted 1 line

Thanks for the review.

Best Regards,
Petr

maintainer Reviewed-by Tested-by on the v3 series
#10 lore raw
In reply to Michael Vetter
Quoted 6 lines · Michael Vetter

This patchset adds a test for livepatching a kprobed function.

Michael Vetter (3):
selftests: livepatch: rename KLP_SYSFS_DIR to SYSFS_KLP_DIR
selftests: livepatch: save and restore kprobe state
selftests: livepatch: test livepatching a kprobed function

Looks good. For the entire patchset:

Best Regards,
Petr