Live Patching
 help / color / mirror / Atom feed
From: Petr Mladek <pmladek@suse.com>
To: Marcos Paulo de Souza <mpdesouza@suse.com>
Cc: Josh Poimboeuf <jpoimboe@kernel.org>,
	Jiri Kosina <jikos@kernel.org>, Miroslav Benes <mbenes@suse.cz>,
	Joe Lawrence <joe.lawrence@redhat.com>,
	Shuah Khan <shuah@kernel.org>,
	live-patching@vger.kernel.org, linux-kselftest@vger.kernel.org,
	linux-kernel@vger.kernel.org, marcos@mpdesouza.com
Subject: Re: [PATCH 2/4] selftests: livepatch: Remove leftover modules when a testcase fails
Date: Fri, 5 Jun 2026 15:59:44 +0200	[thread overview]
Message-ID: <aiLWUHAGtF8qaE_R@pathway.suse.cz> (raw)
In-Reply-To: <20260524-livepatch-unload-on-fail-v1-2-7465de7f741d@suse.com>

On Sun 2026-05-24 20:50:31, Marcos Paulo de Souza wrote:
> The current livepatch selftest scripts load modules, run tests and
> unloads them. If the test fails, it can leave loaded modules behind, and
> in some cases making it impossible to run the next tests.
> 
> This approach tracks down the loaded modules, and in case of a test
> failure, or premature exit of the script, the cleanup function will
> be called by the trap installed on setup_config function.
> 
> The cleanup function iterates over the list of leftover loaded modules,
> unloading them. The function also checks if the given module is a
> livepatch, properly disabling it before unloading.
> 
> Signed-off-by: Marcos Paulo de Souza <mpdesouza@suse.com>
> ---
>  tools/testing/selftests/livepatch/functions.sh | 23 ++++++++++++++++++++++-
>  1 file changed, 22 insertions(+), 1 deletion(-)
> 
> diff --git a/tools/testing/selftests/livepatch/functions.sh b/tools/testing/selftests/livepatch/functions.sh
> index 3ec0b7962fc5..25f137003865 100644
> --- a/tools/testing/selftests/livepatch/functions.sh
> +++ b/tools/testing/selftests/livepatch/functions.sh
> @@ -15,6 +15,8 @@ if [[ -e /sys/kernel/tracing/trace ]]; then
>  else
>  	SYSFS_TRACING_DIR="$SYSFS_DEBUG_DIR/tracing"
>  fi
> +# List of loaded modules used in tests
> +TEST_MODS=()
>  
>  # Kselftest framework requirement - SKIP code is 4
>  ksft_skip=4
> @@ -125,6 +127,14 @@ function set_ftrace_enabled() {
>  }
>  
>  function cleanup() {
> +	# Remove leftover modules in reverse order to handle dependencies
> +	for mod_item in "${TEST_MODS[@]}"; do

It would make sense to check here that the module is loaded.
I would add a helper function is_mod_loaded which
would check whether /sys/module/$mod exists.


> +		if is_livepatch_mod "$mod_item"; then

It might make sense to 1st check here whether the livepatch is
enabled. Again, I would add a helper function for this.

> +			disable_lp "$mod_item"
> +		fi
> +		_remove_mod "$mod_item"
> +	done

I would put this into a helper function, for example,
called remove_test_mods().

> +
>  	pop_config
>  }
>  
> @@ -181,6 +191,9 @@ function __load_mod() {
>  	# Wait for module in sysfs ...
>  	loop_until '[[ -e "/sys/module/$mod" ]]' ||
>  		die "failed to load module $mod"
> +
> +	# Store the module in the modules list
> +	TEST_MODS+=("$mod")

I would put this into a helper function, for example,
called track_test_mod().

>  }
>  
>  
> @@ -262,12 +275,20 @@ function _remove_mod() {
>  		die "failed to unload module $mod (/sys/module)"
>  }
>  
> -# unload_mod(modname) - unload a kernel module
> +# unload_mod(modname) - unload a kernel module and remove it from TEST_MODS
>  #	modname - module name to unload
>  function unload_mod() {
>  	local mod="$1"
>  
>  	_remove_mod "$mod"
> +
> +	# Remove from TEST_MODS array
> +	for i in "${!TEST_MODS[@]}"; do
> +		if [[ "${TEST_MODS[$i]}" == "$mod" ]]; then
> +			unset 'TEST_MODS[$i]'
> +			break
> +		fi
> +	done

And this into a helper called, for example, untrack_test_mod().

>  }
>  
>  # unload_lp(modname) - unload a kernel module with a livepatch

Best Regards,
Petr

  parent reply	other threads:[~2026-06-05 13:59 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-05-24 23:50 [PATCH 0/4] selftests: livepatch: Support 4.12 kernels Marcos Paulo de Souza
2026-05-24 23:50 ` [PATCH 1/4] selftests: livepatch: Introduce _remove_mod function Marcos Paulo de Souza
2026-05-24 23:59   ` sashiko-bot
2026-05-25 11:49     ` Marcos Paulo de Souza
2026-06-05 13:44       ` Petr Mladek
2026-05-24 23:50 ` [PATCH 2/4] selftests: livepatch: Remove leftover modules when a testcase fails Marcos Paulo de Souza
2026-05-25  0:06   ` sashiko-bot
2026-06-05 14:15     ` Petr Mladek
2026-06-05 13:59   ` Petr Mladek [this message]
2026-05-24 23:50 ` [PATCH 3/4] selftests: livepatch: Adapt mod_target module to pass on 4.12 kernels Marcos Paulo de Souza
2026-05-25  0:24   ` sashiko-bot
2026-06-05 14:05     ` Marcos Paulo de Souza
2026-06-05 14:39       ` Petr Mladek
2026-06-05 14:36   ` Petr Mladek
2026-05-24 23:50 ` [PATCH 4/4] selftests: livepatch: Add information about minimum kernel support Marcos Paulo de Souza
2026-05-25  0:29   ` sashiko-bot
2026-06-05 15:14   ` Petr Mladek

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=aiLWUHAGtF8qaE_R@pathway.suse.cz \
    --to=pmladek@suse.com \
    --cc=jikos@kernel.org \
    --cc=joe.lawrence@redhat.com \
    --cc=jpoimboe@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=live-patching@vger.kernel.org \
    --cc=marcos@mpdesouza.com \
    --cc=mbenes@suse.cz \
    --cc=mpdesouza@suse.com \
    --cc=shuah@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox