Skip to content

Commit 52d3709

Browse files
author
CKI KWF Bot
committed
Merge: livepatch: backport v6.17 updates to rhel-9.8
MR: https://gitlab.com/redhat/centos-stream/src/kernel/centos-stream-9/-/merge_requests/7570 Selected livepatching subsystem backports up to v6.17. JIRA: https://issues.redhat.com/browse/RHEL-114920 Consists of code and documentation cleanup as well as a new livepatch/ftrace kselftest. Signed-off-by: Joe Lawrence <joe.lawrence@redhat.com> Approved-by: Rado Vrbovsky <rvrbovsk@redhat.com> Approved-by: Yannick Cote <ycote@redhat.com> Approved-by: CKI KWF Bot <cki-ci-bot+kwf-gitlab-com@redhat.com> Merged-by: CKI GitLab Kmaint Pipeline Bot <26919896-cki-kmaint-pipeline-bot@users.noreply.gitlab.com>
2 parents 2e76db4 + 8f5e394 commit 52d3709

File tree

11 files changed

+105
-8
lines changed

11 files changed

+105
-8
lines changed

Documentation/livepatch/module-elf-format.rst

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -217,16 +217,19 @@ relocation section refer to their respective symbols with their symbol indices,
217217
and the original symbol indices (and thus the symtab ordering) must be
218218
preserved in order for apply_relocate_add() to find the right symbol.
219219

220-
For example, take this particular rela from a livepatch module:::
220+
For example, take this particular rela from a livepatch module::
221221

222222
Relocation section '.klp.rela.btrfs.text.btrfs_feature_attr_show' at offset 0x2ba0 contains 4 entries:
223223
Offset Info Type Symbol's Value Symbol's Name + Addend
224224
000000000000001f 0000005e00000002 R_X86_64_PC32 0000000000000000 .klp.sym.vmlinux.printk,0 - 4
225225

226-
This rela refers to the symbol '.klp.sym.vmlinux.printk,0', and the symbol index is encoded
227-
in 'Info'. Here its symbol index is 0x5e, which is 94 in decimal, which refers to the
228-
symbol index 94.
229-
And in this patch module's corresponding symbol table, symbol index 94 refers to that very symbol:
226+
This rela refers to the symbol '.klp.sym.vmlinux.printk,0', and the symbol
227+
index is encoded in 'Info'. Here its symbol index is 0x5e, which is 94 in
228+
decimal, which refers to the symbol index 94.
229+
230+
And in this patch module's corresponding symbol table, symbol index 94 refers
231+
to that very symbol::
232+
230233
[ snip ]
231234
94: 0000000000000000 0 NOTYPE GLOBAL DEFAULT OS [0xff20] .klp.sym.vmlinux.printk,0
232235
[ snip ]

kernel/livepatch/core.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -612,9 +612,12 @@ static int klp_add_object_nops(struct klp_patch *patch,
612612
}
613613

614614
/*
615-
* Add 'nop' functions which simply return to the caller to run
616-
* the original function. The 'nop' functions are added to a
617-
* patch to facilitate a 'replace' mode.
615+
* Add 'nop' functions which simply return to the caller to run the
616+
* original function.
617+
*
618+
* They are added only when the atomic replace mode is used and only for
619+
* functions which are currently livepatched but are no longer included
620+
* in the new livepatch.
618621
*/
619622
static int klp_add_nops(struct klp_patch *patch)
620623
{

samples/livepatch/livepatch-callbacks-busymod.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,4 +56,5 @@ static void livepatch_callbacks_mod_exit(void)
5656

5757
module_init(livepatch_callbacks_mod_init);
5858
module_exit(livepatch_callbacks_mod_exit);
59+
MODULE_DESCRIPTION("Live patching demo for (un)patching callbacks, support module");
5960
MODULE_LICENSE("GPL");

samples/livepatch/livepatch-callbacks-demo.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,5 +192,6 @@ static void livepatch_callbacks_demo_exit(void)
192192

193193
module_init(livepatch_callbacks_demo_init);
194194
module_exit(livepatch_callbacks_demo_exit);
195+
MODULE_DESCRIPTION("Live patching demo for (un)patching callbacks");
195196
MODULE_LICENSE("GPL");
196197
MODULE_INFO(livepatch, "Y");

samples/livepatch/livepatch-callbacks-mod.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,5 @@ static void livepatch_callbacks_mod_exit(void)
3838

3939
module_init(livepatch_callbacks_mod_init);
4040
module_exit(livepatch_callbacks_mod_exit);
41+
MODULE_DESCRIPTION("Live patching demo for (un)patching callbacks, support module");
4142
MODULE_LICENSE("GPL");

samples/livepatch/livepatch-sample.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,5 +66,6 @@ static void livepatch_exit(void)
6666

6767
module_init(livepatch_init);
6868
module_exit(livepatch_exit);
69+
MODULE_DESCRIPTION("Kernel Live Patching Sample Module");
6970
MODULE_LICENSE("GPL");
7071
MODULE_INFO(livepatch, "Y");

samples/livepatch/livepatch-shadow-fix1.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,5 +168,6 @@ static void livepatch_shadow_fix1_exit(void)
168168

169169
module_init(livepatch_shadow_fix1_init);
170170
module_exit(livepatch_shadow_fix1_exit);
171+
MODULE_DESCRIPTION("Live patching demo for shadow variables");
171172
MODULE_LICENSE("GPL");
172173
MODULE_INFO(livepatch, "Y");

samples/livepatch/livepatch-shadow-fix2.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,5 +128,6 @@ static void livepatch_shadow_fix2_exit(void)
128128

129129
module_init(livepatch_shadow_fix2_init);
130130
module_exit(livepatch_shadow_fix2_exit);
131+
MODULE_DESCRIPTION("Live patching demo for shadow variables");
131132
MODULE_LICENSE("GPL");
132133
MODULE_INFO(livepatch, "Y");

tools/testing/selftests/livepatch/functions.sh

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ SYSFS_KERNEL_DIR="/sys/kernel"
1010
SYSFS_KLP_DIR="$SYSFS_KERNEL_DIR/livepatch"
1111
SYSFS_DEBUG_DIR="$SYSFS_KERNEL_DIR/debug"
1212
SYSFS_KPROBES_DIR="$SYSFS_DEBUG_DIR/kprobes"
13+
SYSFS_TRACING_DIR="$SYSFS_DEBUG_DIR/tracing"
1314

1415
# Kselftest framework requirement - SKIP code is 4
1516
ksft_skip=4
@@ -50,6 +51,9 @@ function push_config() {
5051
awk -F'[: ]' '{print "file " $1 " line " $2 " " $4}')
5152
FTRACE_ENABLED=$(sysctl --values kernel.ftrace_enabled)
5253
KPROBE_ENABLED=$(cat "$SYSFS_KPROBES_DIR/enabled")
54+
TRACING_ON=$(cat "$SYSFS_TRACING_DIR/tracing_on")
55+
CURRENT_TRACER=$(cat "$SYSFS_TRACING_DIR/current_tracer")
56+
FTRACE_FILTER=$(cat "$SYSFS_TRACING_DIR/set_ftrace_filter")
5357
}
5458

5559
function pop_config() {
@@ -62,6 +66,17 @@ function pop_config() {
6266
if [[ -n "$KPROBE_ENABLED" ]]; then
6367
echo "$KPROBE_ENABLED" > "$SYSFS_KPROBES_DIR/enabled"
6468
fi
69+
if [[ -n "$TRACING_ON" ]]; then
70+
echo "$TRACING_ON" > "$SYSFS_TRACING_DIR/tracing_on"
71+
fi
72+
if [[ -n "$CURRENT_TRACER" ]]; then
73+
echo "$CURRENT_TRACER" > "$SYSFS_TRACING_DIR/current_tracer"
74+
fi
75+
if [[ -n "$FTRACE_FILTER" ]]; then
76+
echo "$FTRACE_FILTER" \
77+
| sed -e "/#### all functions enabled ####/d" \
78+
> "$SYSFS_TRACING_DIR/set_ftrace_filter"
79+
fi
6580
}
6681

6782
function set_dynamic_debug() {
@@ -347,3 +362,37 @@ function check_sysfs_value() {
347362
die "Unexpected value in $path: $expected_value vs. $value"
348363
fi
349364
}
365+
366+
# cleanup_tracing() - stop and clean up function tracing
367+
function cleanup_tracing() {
368+
echo 0 > "$SYSFS_TRACING_DIR/tracing_on"
369+
echo "" > "$SYSFS_TRACING_DIR/set_ftrace_filter"
370+
echo "nop" > "$SYSFS_TRACING_DIR/current_tracer"
371+
echo "" > "$SYSFS_TRACING_DIR/trace"
372+
}
373+
374+
# trace_function(function) - start tracing of a function
375+
# function - to be traced function
376+
function trace_function() {
377+
local function="$1"; shift
378+
379+
cleanup_tracing
380+
381+
echo "function" > "$SYSFS_TRACING_DIR/current_tracer"
382+
echo "$function" > "$SYSFS_TRACING_DIR/set_ftrace_filter"
383+
echo 1 > "$SYSFS_TRACING_DIR/tracing_on"
384+
}
385+
386+
# check_traced_functions(functions...) - check whether each function appeared in the trace log
387+
# functions - list of functions to be checked
388+
function check_traced_functions() {
389+
local function
390+
391+
for function in "$@"; do
392+
if ! grep -Fwq "$function" "$SYSFS_TRACING_DIR/trace" ; then
393+
die "Function ($function) did not appear in the trace"
394+
fi
395+
done
396+
397+
cleanup_tracing
398+
}

tools/testing/selftests/livepatch/test-ftrace.sh

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,4 +61,38 @@ livepatch: '$MOD_LIVEPATCH': unpatching complete
6161
% rmmod $MOD_LIVEPATCH"
6262

6363

64+
# - verify livepatch can load
65+
# - check if traces have a patched function
66+
# - reset trace and unload livepatch
67+
68+
start_test "trace livepatched function and check that the live patch remains in effect"
69+
70+
FUNCTION_NAME="livepatch_cmdline_proc_show"
71+
72+
load_lp $MOD_LIVEPATCH
73+
trace_function "$FUNCTION_NAME"
74+
75+
if [[ "$(cat /proc/cmdline)" == "$MOD_LIVEPATCH: this has been live patched" ]] ; then
76+
log "livepatch: ok"
77+
fi
78+
79+
check_traced_functions "$FUNCTION_NAME"
80+
81+
disable_lp $MOD_LIVEPATCH
82+
unload_lp $MOD_LIVEPATCH
83+
84+
check_result "% modprobe $MOD_LIVEPATCH
85+
livepatch: enabling patch '$MOD_LIVEPATCH'
86+
livepatch: '$MOD_LIVEPATCH': initializing patching transition
87+
livepatch: '$MOD_LIVEPATCH': starting patching transition
88+
livepatch: '$MOD_LIVEPATCH': completing patching transition
89+
livepatch: '$MOD_LIVEPATCH': patching complete
90+
livepatch: ok
91+
% echo 0 > $SYSFS_KLP_DIR/$MOD_LIVEPATCH/enabled
92+
livepatch: '$MOD_LIVEPATCH': initializing unpatching transition
93+
livepatch: '$MOD_LIVEPATCH': starting unpatching transition
94+
livepatch: '$MOD_LIVEPATCH': completing unpatching transition
95+
livepatch: '$MOD_LIVEPATCH': unpatching complete
96+
% rmmod $MOD_LIVEPATCH"
97+
6498
exit 0

0 commit comments

Comments
 (0)