Skip to contentSkip to conversation

selftests/livepatch: add test skip handling

PATCH Superseded Joe Lawrence 5 participants 9 messages Jul 14, 2019 – Jul 20, 2019

Subsystems (2)

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

Joe Lawrence

author maintainer
#0 lore raw

PATCH 1/1 selftests/livepatch: add test skip handling

Before running a livpeatch self-test, first verify that we've built and installed the livepatch self-test kernel modules by running a 'modprobe --dry-run'. This should catch a few environment issues, including !CONFIG_LIVEPATCH and !CONFIG_TEST_LIVEPATCH. In these cases, exit gracefully with test-skip status rather than test-fail status.

---
 tools/testing/selftests/livepatch/functions.sh | 18 ++++++++++++++++++
 .../selftests/livepatch/test-callbacks.sh      |  5 +++++
 .../selftests/livepatch/test-livepatch.sh      |  3 +++
 .../selftests/livepatch/test-shadow-vars.sh    |  2 ++
 4 files changed, 28 insertions(+)
tools/testing/selftests/livepatch/functions.sh +18 -0
index 30195449c63c..92d6cfb49365 100644
@@ -13,6 +13,14 @@ function log() {
 	echo "$1" > /dev/kmsg
 }
 
+# skip(msg) - testing can't proceed
+#	msg - explanation
+function skip() {
+	log "SKIP: $1"
+	echo "SKIP: $1" >&2
+	exit 4
+}
+
 # die(msg) - game over, man
 #	msg - dying words
 function die() {
@@ -43,6 +51,16 @@ function loop_until() {
 	done
 }
 
+function assert_mod() {
+	local mod="$1"
+
+	if ! modprobe --dry-run "$mod" &>/dev/null ; then
+		skip "Failed modprobe --dry-run of module: $mod"
+	fi
+
+	return 1
+}
+
 function is_livepatch_mod() {
 	local mod="$1"
 
tools/testing/selftests/livepatch/test-callbacks.sh +5 -0
index e97a9dcb73c7..87a407cee7fd 100755
@@ -9,6 +9,11 @@ MOD_LIVEPATCH2=test_klp_callbacks_demo2
 MOD_TARGET=test_klp_callbacks_mod
 MOD_TARGET_BUSY=test_klp_callbacks_busy
 
+assert_mod $MOD_LIVEPATCH
+assert_mod $MOD_LIVEPATCH2
+assert_mod $MOD_TARGET
+assert_mod $MOD_TARGET_BUSY
+
 set_dynamic_debug
 
 
tools/testing/selftests/livepatch/test-livepatch.sh +3 -0
index f05268aea859..8d3b75ceeeff 100755
@@ -7,6 +7,9 @@
 MOD_LIVEPATCH=test_klp_livepatch
 MOD_REPLACE=test_klp_atomic_replace
 
+assert_mod $MOD_LIVEPATCH
+assert_mod $MOD_REPLACE
+
 set_dynamic_debug
 
 
tools/testing/selftests/livepatch/test-shadow-vars.sh +2 -0
index 04a37831e204..1ab09bc50363 100755
@@ -6,6 +6,8 @@
 
 MOD_TEST=test_klp_shadow_vars
 
+assert_mod $MOD_TEST
+
 set_dynamic_debug
 
 
Signature · 1 line
2.21.0
author maintainer
#1 lore raw
Quoted 98 lines · Joe Lawrence

Before running a livpeatch self-test, first verify that we've built and installed the livepatch self-test kernel modules by running a 'modprobe --dry-run'. This should catch a few environment issues, including !CONFIG_LIVEPATCH and !CONFIG_TEST_LIVEPATCH. In these cases, exit gracefully with test-skip status rather than test-fail status.

---
 tools/testing/selftests/livepatch/functions.sh | 18 ++++++++++++++++++
 .../selftests/livepatch/test-callbacks.sh      |  5 +++++
 .../selftests/livepatch/test-livepatch.sh      |  3 +++
 .../selftests/livepatch/test-shadow-vars.sh    |  2 ++
 4 files changed, 28 insertions(+)
tools/testing/selftests/livepatch/functions.sh +18 -0
index 30195449c63c..92d6cfb49365 100644
@@ -13,6 +13,14 @@ function log() {
 	echo "$1" > /dev/kmsg
 }
 
+# skip(msg) - testing can't proceed
+#	msg - explanation
+function skip() {
+	log "SKIP: $1"
+	echo "SKIP: $1" >&2
+	exit 4
+}
+
 # die(msg) - game over, man
 #	msg - dying words
 function die() {
@@ -43,6 +51,16 @@ function loop_until() {
 	done
 }
 
+function assert_mod() {
+	local mod="$1"
+
+	if ! modprobe --dry-run "$mod" &>/dev/null ; then
+		skip "Failed modprobe --dry-run of module: $mod"
+	fi
+
+	return 1
+}
+
 function is_livepatch_mod() {
 	local mod="$1"
 
tools/testing/selftests/livepatch/test-callbacks.sh +5 -0
index e97a9dcb73c7..87a407cee7fd 100755
@@ -9,6 +9,11 @@ MOD_LIVEPATCH2=test_klp_callbacks_demo2
 MOD_TARGET=test_klp_callbacks_mod
 MOD_TARGET_BUSY=test_klp_callbacks_busy
 
+assert_mod $MOD_LIVEPATCH
+assert_mod $MOD_LIVEPATCH2
+assert_mod $MOD_TARGET
+assert_mod $MOD_TARGET_BUSY
+
 set_dynamic_debug
 
 
tools/testing/selftests/livepatch/test-livepatch.sh +3 -0
index f05268aea859..8d3b75ceeeff 100755
@@ -7,6 +7,9 @@
 MOD_LIVEPATCH=test_klp_livepatch
 MOD_REPLACE=test_klp_atomic_replace
 
+assert_mod $MOD_LIVEPATCH
+assert_mod $MOD_REPLACE
+
 set_dynamic_debug
 
 
tools/testing/selftests/livepatch/test-shadow-vars.sh +2 -0
index 04a37831e204..1ab09bc50363 100755
@@ -6,6 +6,8 @@
 
 MOD_TEST=test_klp_shadow_vars
 
+assert_mod $MOD_TEST
+
 set_dynamic_debug
 
 
Signature · 1 line
2.21.0

Testing:

Here's the output if modprobe --dry-run doesn't like the modules (not built, etc.):

TAP version 13
selftests: livepatch: test-livepatch.sh
========================================
SKIP: Failed modprobe --dry-run of module: test_klp_livepatch not ok 1..1 selftests: livepatch: test-livepatch.sh [SKIP]
selftests: livepatch: test-callbacks.sh
========================================
SKIP: Failed modprobe --dry-run of module: test_klp_callbacks_demo not ok 1..2 selftests: livepatch: test-callbacks.sh [SKIP]
selftests: livepatch: test-shadow-vars.sh
========================================
SKIP: Failed modprobe --dry-run of module: test_klp_shadow_vars not ok 1..3 selftests: livepatch: test-shadow-vars.sh [SKIP]

We could fold assert_mod() into __load_mod() if folks perfer. I don't have strong opinion either way.

-- Joe

maintainer
#2 lore raw
Quoted 119 lines · Joe Lawrence
Quoted 98 lines · Joe Lawrence

Before running a livpeatch self-test, first verify that we've built and installed the livepatch self-test kernel modules by running a 'modprobe --dry-run'. This should catch a few environment issues, including !CONFIG_LIVEPATCH and !CONFIG_TEST_LIVEPATCH. In these cases, exit gracefully with test-skip status rather than test-fail status.

---
 tools/testing/selftests/livepatch/functions.sh | 18 ++++++++++++++++++
 .../selftests/livepatch/test-callbacks.sh      |  5 +++++
 .../selftests/livepatch/test-livepatch.sh      |  3 +++
 .../selftests/livepatch/test-shadow-vars.sh    |  2 ++
 4 files changed, 28 insertions(+)
tools/testing/selftests/livepatch/functions.sh +18 -0
index 30195449c63c..92d6cfb49365 100644
@@ -13,6 +13,14 @@ function log() {
 	echo "$1" > /dev/kmsg
 }
 
+# skip(msg) - testing can't proceed
+#	msg - explanation
+function skip() {
+	log "SKIP: $1"
+	echo "SKIP: $1" >&2
+	exit 4
+}
+
 # die(msg) - game over, man
 #	msg - dying words
 function die() {
@@ -43,6 +51,16 @@ function loop_until() {
 	done
 }
 
+function assert_mod() {
+	local mod="$1"
+
+	if ! modprobe --dry-run "$mod" &>/dev/null ; then
+		skip "Failed modprobe --dry-run of module: $mod"
+	fi
+
+	return 1
+}
+
 function is_livepatch_mod() {
 	local mod="$1"
 
tools/testing/selftests/livepatch/test-callbacks.sh +5 -0
index e97a9dcb73c7..87a407cee7fd 100755
@@ -9,6 +9,11 @@ MOD_LIVEPATCH2=test_klp_callbacks_demo2
 MOD_TARGET=test_klp_callbacks_mod
 MOD_TARGET_BUSY=test_klp_callbacks_busy
 
+assert_mod $MOD_LIVEPATCH
+assert_mod $MOD_LIVEPATCH2
+assert_mod $MOD_TARGET
+assert_mod $MOD_TARGET_BUSY
+
 set_dynamic_debug
 
 
tools/testing/selftests/livepatch/test-livepatch.sh +3 -0
index f05268aea859..8d3b75ceeeff 100755
@@ -7,6 +7,9 @@
 MOD_LIVEPATCH=test_klp_livepatch
 MOD_REPLACE=test_klp_atomic_replace
 
+assert_mod $MOD_LIVEPATCH
+assert_mod $MOD_REPLACE
+
 set_dynamic_debug
 
 
tools/testing/selftests/livepatch/test-shadow-vars.sh +2 -0
index 04a37831e204..1ab09bc50363 100755
@@ -6,6 +6,8 @@
 
 MOD_TEST=test_klp_shadow_vars
 
+assert_mod $MOD_TEST
+
 set_dynamic_debug
 
 
Signature · 1 line
2.21.0

Testing:

Here's the output if modprobe --dry-run doesn't like the modules (not built, etc.):

TAP version 13
selftests: livepatch: test-livepatch.sh
========================================
SKIP: Failed modprobe --dry-run of module: test_klp_livepatch not ok 1..1 selftests: livepatch: test-livepatch.sh [SKIP]
selftests: livepatch: test-callbacks.sh
========================================
SKIP: Failed modprobe --dry-run of module: test_klp_callbacks_demo not ok 1..2 selftests: livepatch: test-callbacks.sh [SKIP]
selftests: livepatch: test-shadow-vars.sh
========================================
SKIP: Failed modprobe --dry-run of module: test_klp_shadow_vars not ok 1..3 selftests: livepatch: test-shadow-vars.sh [SKIP]

Quoted 2 lines

We could fold assert_mod() into __load_mod() if folks perfer. I don't have strong opinion either way.

I think it would be better to move it there. Otherwise, we might forget to add assert_module call for new modules in the future.

Miroslav

maintainer
#3 lore raw
In reply to Joe Lawrence
Quoted 119 lines · Joe Lawrence
Quoted 98 lines · Joe Lawrence

Before running a livpeatch self-test, first verify that we've built and installed the livepatch self-test kernel modules by running a 'modprobe --dry-run'. This should catch a few environment issues, including !CONFIG_LIVEPATCH and !CONFIG_TEST_LIVEPATCH. In these cases, exit gracefully with test-skip status rather than test-fail status.

---
  tools/testing/selftests/livepatch/functions.sh | 18 ++++++++++++++++++
  .../selftests/livepatch/test-callbacks.sh      |  5 +++++
  .../selftests/livepatch/test-livepatch.sh      |  3 +++
  .../selftests/livepatch/test-shadow-vars.sh    |  2 ++
  4 files changed, 28 insertions(+)
tools/testing/selftests/livepatch/functions.sh +18 -0
index 30195449c63c..92d6cfb49365 100644
@@ -13,6 +13,14 @@ function log() {
  	echo "$1" > /dev/kmsg
  }
 
+# skip(msg) - testing can't proceed
+#	msg - explanation
+function skip() {
+	log "SKIP: $1"
+	echo "SKIP: $1" >&2
+	exit 4
+}
+
  # die(msg) - game over, man
  #	msg - dying words
  function die() {
@@ -43,6 +51,16 @@ function loop_until() {
  	done
  }
 
+function assert_mod() {
+	local mod="$1"
+
+	if ! modprobe --dry-run "$mod" &>/dev/null ; then
+		skip "Failed modprobe --dry-run of module: $mod"
+	fi
+
+	return 1
+}
+
  function is_livepatch_mod() {
  	local mod="$1"
 
tools/testing/selftests/livepatch/test-callbacks.sh +5 -0
index e97a9dcb73c7..87a407cee7fd 100755
@@ -9,6 +9,11 @@ MOD_LIVEPATCH2=test_klp_callbacks_demo2
  MOD_TARGET=test_klp_callbacks_mod
  MOD_TARGET_BUSY=test_klp_callbacks_busy
 
+assert_mod $MOD_LIVEPATCH
+assert_mod $MOD_LIVEPATCH2
+assert_mod $MOD_TARGET
+assert_mod $MOD_TARGET_BUSY
+
  set_dynamic_debug
 
 
tools/testing/selftests/livepatch/test-livepatch.sh +3 -0
index f05268aea859..8d3b75ceeeff 100755
@@ -7,6 +7,9 @@
  MOD_LIVEPATCH=test_klp_livepatch
  MOD_REPLACE=test_klp_atomic_replace
 
+assert_mod $MOD_LIVEPATCH
+assert_mod $MOD_REPLACE
+
  set_dynamic_debug
 
 
tools/testing/selftests/livepatch/test-shadow-vars.sh +2 -0
index 04a37831e204..1ab09bc50363 100755
@@ -6,6 +6,8 @@
 
  MOD_TEST=test_klp_shadow_vars
 
+assert_mod $MOD_TEST
+
  set_dynamic_debug
 
 
Signature · 1 line
2.21.0

Testing:

Here's the output if modprobe --dry-run doesn't like the modules (not built, etc.):

TAP version 13
selftests: livepatch: test-livepatch.sh
========================================
SKIP: Failed modprobe --dry-run of module: test_klp_livepatch not ok 1..1 selftests: livepatch: test-livepatch.sh [SKIP]
selftests: livepatch: test-callbacks.sh
========================================
SKIP: Failed modprobe --dry-run of module: test_klp_callbacks_demo not ok 1..2 selftests: livepatch: test-callbacks.sh [SKIP]
selftests: livepatch: test-shadow-vars.sh
========================================
SKIP: Failed modprobe --dry-run of module: test_klp_shadow_vars not ok 1..3 selftests: livepatch: test-shadow-vars.sh [SKIP]

Quoted 3 lines

We could fold assert_mod() into __load_mod() if folks perfer. I don't have strong opinion either way.

Please refine these messages to say what users should do. In addition to what failed, also add what is missing - enable config option etc.

thanks,
-- Shuah

author maintainer
#4 lore raw
Quoted 25 lines · shuah
Quoted 9 lines · Joe Lawrence

On Sun, Jul 14, 2019 at 10:28:29AM -0400, Joe Lawrence wrote:

[ ... snip ... ]

Testing:

Here's the output if modprobe --dry-run doesn't like the modules (not built, etc.):

Quoted 14 lines

TAP version 13
selftests: livepatch: test-livepatch.sh
========================================
SKIP: Failed modprobe --dry-run of module: test_klp_livepatch not ok 1..1 selftests: livepatch: test-livepatch.sh [SKIP]
selftests: livepatch: test-callbacks.sh
========================================
SKIP: Failed modprobe --dry-run of module: test_klp_callbacks_demo not ok 1..2 selftests: livepatch: test-callbacks.sh [SKIP]
selftests: livepatch: test-shadow-vars.sh
========================================
SKIP: Failed modprobe --dry-run of module: test_klp_shadow_vars not ok 1..3 selftests: livepatch: test-shadow-vars.sh [SKIP]

Quoted 3 lines

Please refine these messages to say what users should do. In addition to what failed, also add what is missing - enable config option etc.

Hi Shuah,

Note that v2 was posted [1], but the output remains basically the same, so your comments still apply.

Off the top of my head, modprobe can fail for many reasons: unprivileged user, missing .ko files, missing modules.dep entry, kernel vermagic, interface versions, etc.

What would you think about modifying our skip() function to provide a catch-all list of CONFIG, environment, etc. requirements? Something like, "Please ensure that the kernel was build with CONFIG_XYZ and that the tests are run with foo privileges"? That would let us avoid trying to figure out exactly why the modprobe failed, but still help out the user.

Alternatively, are there existing modprobe callers in the selftests that already do a better job?

[1] https://lore.kernel.org/linux-kselftest/[email protected]/T/#mc2f70095215738cb6a539e2c2e6a415c78a8add6

Thanks,

-- Joe

maintainer
#5 lore raw
Quoted 32 lines · Joe Lawrence
Quoted 25 lines · shuah
Quoted 9 lines · Joe Lawrence

On Sun, Jul 14, 2019 at 10:28:29AM -0400, Joe Lawrence wrote:

[ ... snip ... ]

Testing:

Here's the output if modprobe --dry-run doesn't like the modules (not built, etc.):

Quoted 14 lines

    TAP version 13
    selftests: livepatch: test-livepatch.sh
    ========================================
    SKIP: Failed modprobe --dry-run of module: test_klp_livepatch not ok 1..1 selftests: livepatch: test-livepatch.sh [SKIP]
    selftests: livepatch: test-callbacks.sh
    ========================================
    SKIP: Failed modprobe --dry-run of module: test_klp_callbacks_demo not ok 1..2 selftests: livepatch: test-callbacks.sh [SKIP]
    selftests: livepatch: test-shadow-vars.sh
    ========================================
    SKIP: Failed modprobe --dry-run of module: test_klp_shadow_vars not ok 1..3 selftests: livepatch: test-shadow-vars.sh [SKIP]

Quoted 3 lines

Please refine these messages to say what users should do. In addition to what failed, also add what is missing - enable config option etc.

Hi Shuah,

Quoted 8 lines

Note that v2 was posted [1], but the output remains basically the same, so your comments still apply.

Off the top of my head, modprobe can fail for many reasons: unprivileged user, missing .ko files, missing modules.dep entry, kernel vermagic, interface versions, etc.

What would you think about modifying our skip() function to provide a catch-all list of CONFIG, environment, etc. requirements?  Something like, "Please ensure that the kernel was build with CONFIG_XYZ and that the tests are run with foo privileges"?  That would let us avoid trying to figure out exactly why the modprobe failed, but still help out the user.

I understand. I am not suggesting that you have to figure out why. I am suggesting that instead of "Failed modprobe --dry-run of module", say "Unable to load test_klp_shadow_vars module. Check if config option is enabled" which is user friendly compared to the current message.

thanks,
-- Shuah

Reviewed-by on v1 1/1
#6 lore raw
In reply to Joe Lawrence
Quoted 9 lines · Joe Lawrence

Before running a livpeatch self-test, first verify that we've built and installed the livepatch self-test kernel modules by running a 'modprobe --dry-run'. This should catch a few environment issues, including !CONFIG_LIVEPATCH and !CONFIG_TEST_LIVEPATCH. In these cases, exit gracefully with test-skip status rather than test-fail status.

[...]

tools/testing/selftests/livepatch/functions.sh function loop_until() { · line 57

Quoted 5 lines
tools/testing/selftests/livepatch/functions.sh +4 -0
+function assert_mod() {
+	local mod="$1"
+
+	if ! modprobe --dry-run "$mod" &>/dev/null ; then

Just a preference comment, shorter version 'modprobe -q -n' can be used here.

Thanks,
Kamalesh

author maintainer
#7 lore raw
Quoted 15 lines · Kamalesh Babulal
Quoted 9 lines · Joe Lawrence

Before running a livpeatch self-test, first verify that we've built and installed the livepatch self-test kernel modules by running a 'modprobe --dry-run'. This should catch a few environment issues, including !CONFIG_LIVEPATCH and !CONFIG_TEST_LIVEPATCH. In these cases, exit gracefully with test-skip status rather than test-fail status.

[...]

Quoted 1 line
Quoted 8 lines

tools/testing/selftests/livepatch/functions.sh function loop_until() { · line 57

Quoted 4 lines
tools/testing/selftests/livepatch/functions.sh +4 -0
+function assert_mod() {
+	local mod="$1"
+
+	if ! modprobe --dry-run "$mod" &>/dev/null ; then

Just a preference comment, shorter version 'modprobe -q -n' can be used here.

Hi Kamalesh,

Re: command line options: my preference has been to use the long form command switches inside scripts as they are more likely to be self documenting than their short counterparts. e.g. I could have guessed that -q is --quiet, but not that -n is --dry-run.

Re: --quiet vs. command redirection: Another detail I don't have a strong opinion about. I guess I very slightly prefer the redirect so I don't have to research various modprobe versions to determine if --quiet is universally supported (it probably is).

In both cases, I'll defer to whatever reviewers think is more readable/conventional for the self-tests.

-- Joe

maintainer
#8 lore raw
In reply to Joe Lawrence
Quoted 26 lines · Joe Lawrence

Before running a livpeatch self-test, first verify that we've built and installed the livepatch self-test kernel modules by running a 'modprobe --dry-run'. This should catch a few environment issues, including !CONFIG_LIVEPATCH and !CONFIG_TEST_LIVEPATCH. In these cases, exit gracefully with test-skip status rather than test-fail status.

tools/testing/selftests/livepatch/functions.sh +8 -0
index 30195449c63c..92d6cfb49365 100644
@@ -13,6 +13,14 @@ function log() {
 	echo "$1" > /dev/kmsg
 }
 
+# skip(msg) - testing can't proceed
+#	msg - explanation
+function skip() {
+	log "SKIP: $1"
+	echo "SKIP: $1" >&2
+	exit 4
+}
+
 # die(msg) - game over, man
 #	msg - dying words
 function die() {

tools/testing/selftests/livepatch/functions.sh function loop_until() { · line 57

Quoted 7 lines
tools/testing/selftests/livepatch/functions.sh +4 -0
@@ -43,6 +51,16 @@ function loop_until() {
 	done
 }
 
+function assert_mod() {
+	local mod="$1"
+
+	if ! modprobe --dry-run "$mod" &>/dev/null ; then

JFYI, I do not have strong opinion but the long option and redirection looks fine to me.

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

Quoted 4 lines
tools/testing/selftests/livepatch/functions.sh +4 -0
+		skip "Failed modprobe --dry-run of module: $mod"
+	fi
+
+	return 1

return 0 or nothing?

Quoted 10 lines
tools/testing/selftests/livepatch/functions.sh +2 -0
+}
+
 function is_livepatch_mod() {
 	local mod="$1"
 
tools/testing/selftests/livepatch/test-callbacks.sh +0 -0
index e97a9dcb73c7..87a407cee7fd 100755

tools/testing/selftests/livepatch/test-callbacks.sh MOD_LIVEPATCH2=test_klp_callbacks_demo2 · line 15

Quoted 7 lines
tools/testing/selftests/livepatch/test-callbacks.sh +4 -0
@@ -9,6 +9,11 @@ MOD_LIVEPATCH2=test_klp_callbacks_demo2
 MOD_TARGET=test_klp_callbacks_mod
 MOD_TARGET_BUSY=test_klp_callbacks_busy
 
+assert_mod $MOD_LIVEPATCH
+assert_mod $MOD_LIVEPATCH2
+assert_mod $MOD_TARGET
+assert_mod $MOD_TARGET_BUSY

I agree that moving this into __load_mod() is less error prone.

Best Regards,
Petr