Re: [PATCH] selftests/livepatch: add test for module function patching

Miroslav Benes
2026-03-27T10:46:49Z

> > > +disable_lp $MOD_TARGET_PATCH
> > > +unload_lp $MOD_TARGET_PATCH
> > > +
> > > +if [[ "$(cat /proc/$MOD_TARGET)" != "$MOD_TARGET: original output" ]] ; then
> > > + echo -e "FAIL\n\n"
> > > + die "livepatch kselftest(s) failed"
> > > +fi
> > > +
> > > +unload_mod $MOD_TARGET
> > > +
> > > +check_result "% insmod test_modules/$MOD_TARGET.ko
> > > +$MOD_TARGET: test_klp_mod_target_init
> > > +% insmod test_modules/$MOD_TARGET_PATCH.ko
> >
>
> So following this technique, all the other tests with command sequences
> would need to be re-written as '&&' chains, e.g. the "patch getpid
> syscall while being heavily hammered" one like:
>
> pid_list=$(echo "${pids[@]}" | tr ' ' ',') && \
> load_lp $MOD_SYSCALL klp_pids=$pid_list && \
> loop_until "grep -q '^0$' $SYSFS_KERNEL_DIR/$MOD_SYSCALL/npids" && \
> log "$MOD_SYSCALL: Remaining not livepatched processes: $(cat $SYSFS_KERNEL_DIR/$MOD_SYSCALL/npids)"
>
> so that we only continue down a particular test for as long as it's
> successful, then the cleanup code is unconditional:
>
> pending_pids=$(cat $SYSFS_KERNEL_DIR/$MOD_SYSCALL/npids)
> log "$MOD_SYSCALL: Remaining not livepatched processes: $pending_pids"
>
> for pid in ${pids[@]}; do
> kill $pid || true
> done
>
> disable_lp $MOD_SYSCALL
> unload_lp $MOD_SYSCALL
>
> check_result <- flags a problem
>
> Yeah, may be that's not so bad. The functions.sh helpers may need to be
> hardened a little (can they cancel / bust a transition? it's been a
> while since I've looked.)
>
> Or maybe ... ugh, bash is not a programming language ... each test is
> split into its own script, the die calls can remain as they are, but we
> move the cleanup logic into a trap EXIT handler so it always runs?

We use this technique in OOT https://github.com/SUSE/qa_test_klp/ tests
(slowly being upstreamed). See
https://github.com/SUSE/qa_test_klp/blob/master/klp_tc_functions.sh.
Mainly klp_tc_init(), klp_tc_exit() and klp_tc_abort(). Different tests
then use what you proposed above... caching pids and modules to clean up.

Miroslav