[PATCH PATCH 2/2] selftests: livepatch: test if ftrace can trace a livepatched function

Filipe Xavier posted 2 patches 11 months, 1 week ago
There is a newer version of this series
[PATCH PATCH 2/2] selftests: livepatch: test if ftrace can trace a livepatched function
Posted by Filipe Xavier 11 months, 1 week ago
This new test makes sure that ftrace can trace a
function that was introduced by a livepatch.

Signed-off-by: Filipe Xavier <felipeaggger@gmail.com>
---
 tools/testing/selftests/livepatch/test-ftrace.sh | 35 ++++++++++++++++++++++++
 1 file changed, 35 insertions(+)

diff --git a/tools/testing/selftests/livepatch/test-ftrace.sh b/tools/testing/selftests/livepatch/test-ftrace.sh
index fe14f248913acbec46fb6c0fec38a2fc84209d39..35774c42e239b466599052d6a9cc3cf75b78d63d 100755
--- a/tools/testing/selftests/livepatch/test-ftrace.sh
+++ b/tools/testing/selftests/livepatch/test-ftrace.sh
@@ -61,4 +61,39 @@ livepatch: '$MOD_LIVEPATCH': unpatching complete
 % rmmod $MOD_LIVEPATCH"
 
 
+# - verify livepatch can load
+# - check if traces have a patched function
+# - reset trace and unload livepatch
+
+start_test "trace livepatched function and check that the live patch remains in effect"
+
+FUNCTION_NAME="livepatch_cmdline_proc_show"
+
+load_lp $MOD_LIVEPATCH
+trace_function "$FUNCTION_NAME"
+
+if [[ "$(cat /proc/cmdline)" == "$MOD_LIVEPATCH: this has been live patched" ]] ; then
+	log "livepatch: ok"
+fi
+
+check_traced_function "$FUNCTION_NAME"
+
+cleanup_tracing
+disable_lp $MOD_LIVEPATCH
+unload_lp $MOD_LIVEPATCH
+
+check_result "% insmod test_modules/$MOD_LIVEPATCH.ko
+livepatch: enabling patch '$MOD_LIVEPATCH'
+livepatch: '$MOD_LIVEPATCH': initializing patching transition
+livepatch: '$MOD_LIVEPATCH': starting patching transition
+livepatch: '$MOD_LIVEPATCH': completing patching transition
+livepatch: '$MOD_LIVEPATCH': patching complete
+livepatch: ok
+% echo 0 > /sys/kernel/livepatch/$MOD_LIVEPATCH/enabled
+livepatch: '$MOD_LIVEPATCH': initializing unpatching transition
+livepatch: '$MOD_LIVEPATCH': starting unpatching transition
+livepatch: '$MOD_LIVEPATCH': completing unpatching transition
+livepatch: '$MOD_LIVEPATCH': unpatching complete
+% rmmod $MOD_LIVEPATCH"
+
 exit 0

-- 
2.46.2
Re: [PATCH PATCH 2/2] selftests: livepatch: test if ftrace can trace a livepatched function
Posted by Miroslav Benes 11 months ago
Hi,

> +start_test "trace livepatched function and check that the live patch remains in effect"
> +
> +FUNCTION_NAME="livepatch_cmdline_proc_show"
> +
> +load_lp $MOD_LIVEPATCH
> +trace_function "$FUNCTION_NAME"

trace_funtion() calls cleanup_ftrace() to prepare the test. Ok.

> +if [[ "$(cat /proc/cmdline)" == "$MOD_LIVEPATCH: this has been live patched" ]] ; then
> +	log "livepatch: ok"
> +fi
> +
> +check_traced_function "$FUNCTION_NAME"
> +
> +cleanup_tracing

Here, I suppose, cleanup_tracing() is called to clean up after the check 
above so that nothing stays and more tests can be added later. Right? 
Would it make sense then to call cleanup_tracing() in 
check_traced_function()? I think it would less error prone. 
If needed, check_traced_function() can always be upgraded so that it 
checks for more traced functions.

Miroslav
Re: [PATCH PATCH 2/2] selftests: livepatch: test if ftrace can trace a livepatched function
Posted by Filipe Xavier 11 months ago
On 3/14/25 10:14 AM, Miroslav Benes wrote:

> Hi,
>
>> +start_test "trace livepatched function and check that the live patch remains in effect"
>> +
>> +FUNCTION_NAME="livepatch_cmdline_proc_show"
>> +
>> +load_lp $MOD_LIVEPATCH
>> +trace_function "$FUNCTION_NAME"
> trace_funtion() calls cleanup_ftrace() to prepare the test. Ok.
>
>> +if [[ "$(cat /proc/cmdline)" == "$MOD_LIVEPATCH: this has been live patched" ]] ; then
>> +	log "livepatch: ok"
>> +fi
>> +
>> +check_traced_function "$FUNCTION_NAME"
>> +
>> +cleanup_tracing
> Here, I suppose, cleanup_tracing() is called to clean up after the check
> above so that nothing stays and more tests can be added later. Right?
> Would it make sense then to call cleanup_tracing() in
> check_traced_function()? I think it would less error prone.
> If needed, check_traced_function() can always be upgraded so that it
> checks for more traced functions.

In cases where we need to check two or more functions with 
check_traced_function,

if there is cleanup_tracing, it will not be possible, make sense?

e.g: function1 call -> function2 call -> function3.

>
> Miroslav
Re: [PATCH PATCH 2/2] selftests: livepatch: test if ftrace can trace a livepatched function
Posted by Miroslav Benes 10 months, 4 weeks ago
On Fri, 14 Mar 2025, Filipe Xavier wrote:

> On 3/14/25 10:14 AM, Miroslav Benes wrote:
> 
> > Hi,
> >
> >> +start_test "trace livepatched function and check that the live patch
> >> remains in effect"
> >> +
> >> +FUNCTION_NAME="livepatch_cmdline_proc_show"
> >> +
> >> +load_lp $MOD_LIVEPATCH
> >> +trace_function "$FUNCTION_NAME"
> > trace_funtion() calls cleanup_ftrace() to prepare the test. Ok.
> >
> >> +if [[ "$(cat /proc/cmdline)" == "$MOD_LIVEPATCH: this has been live
> >> patched" ]] ; then
> >> +	log "livepatch: ok"
> >> +fi
> >> +
> >> +check_traced_function "$FUNCTION_NAME"
> >> +
> >> +cleanup_tracing
> > Here, I suppose, cleanup_tracing() is called to clean up after the check
> > above so that nothing stays and more tests can be added later. Right?
> > Would it make sense then to call cleanup_tracing() in
> > check_traced_function()? I think it would less error prone.
> > If needed, check_traced_function() can always be upgraded so that it
> > checks for more traced functions.
> 
> In cases where we need to check two or more functions with
> check_traced_function,
> 
> if there is cleanup_tracing, it will not be possible, make sense?
> 
> e.g: function1 call -> function2 call -> function3.

I meant... check_traced_function() (or check_traced_functions() in this 
case) can have multiple arguments. You would loop over them inside and 
then clean up. Or did I misunderstood?

Miroslav
Re: [PATCH PATCH 2/2] selftests: livepatch: test if ftrace can trace a livepatched function
Posted by Filipe Xavier 10 months, 3 weeks ago
On 3/17/25 6:07 AM, Miroslav Benes wrote:

> On Fri, 14 Mar 2025, Filipe Xavier wrote:
>
>> On 3/14/25 10:14 AM, Miroslav Benes wrote:
>>
>>> Hi,
>>>
>>>> +start_test "trace livepatched function and check that the live patch
>>>> remains in effect"
>>>> +
>>>> +FUNCTION_NAME="livepatch_cmdline_proc_show"
>>>> +
>>>> +load_lp $MOD_LIVEPATCH
>>>> +trace_function "$FUNCTION_NAME"
>>> trace_funtion() calls cleanup_ftrace() to prepare the test. Ok.
>>>
>>>> +if [[ "$(cat /proc/cmdline)" == "$MOD_LIVEPATCH: this has been live
>>>> patched" ]] ; then
>>>> +	log "livepatch: ok"
>>>> +fi
>>>> +
>>>> +check_traced_function "$FUNCTION_NAME"
>>>> +
>>>> +cleanup_tracing
>>> Here, I suppose, cleanup_tracing() is called to clean up after the check
>>> above so that nothing stays and more tests can be added later. Right?
>>> Would it make sense then to call cleanup_tracing() in
>>> check_traced_function()? I think it would less error prone.
>>> If needed, check_traced_function() can always be upgraded so that it
>>> checks for more traced functions.
>> In cases where we need to check two or more functions with
>> check_traced_function,
>>
>> if there is cleanup_tracing, it will not be possible, make sense?
>>
>> e.g: function1 call -> function2 call -> function3.
> I meant... check_traced_function() (or check_traced_functions() in this
> case) can have multiple arguments. You would loop over them inside and
> then clean up. Or did I misunderstood?

I hadn't thought of it that way, it makes perfect sense. I'll send a new 
version with this adjustment.

>
> Miroslav