xref: /linux/tools/testing/selftests/livepatch/test-sysfs.sh (revision d53b8e36925256097a08d7cb749198d85cbf9b2b)
1#!/bin/bash
2# SPDX-License-Identifier: GPL-2.0
3# Copyright (C) 2022 Song Liu <song@kernel.org>
4
5. $(dirname $0)/functions.sh
6
7MOD_LIVEPATCH=test_klp_livepatch
8
9setup_config
10
11# - load a livepatch and verifies the sysfs entries work as expected
12
13start_test "sysfs test"
14
15load_lp $MOD_LIVEPATCH
16
17check_sysfs_rights "$MOD_LIVEPATCH" "" "drwxr-xr-x"
18check_sysfs_rights "$MOD_LIVEPATCH" "enabled" "-rw-r--r--"
19check_sysfs_value  "$MOD_LIVEPATCH" "enabled" "1"
20check_sysfs_rights "$MOD_LIVEPATCH" "force" "--w-------"
21check_sysfs_rights "$MOD_LIVEPATCH" "replace" "-r--r--r--"
22check_sysfs_rights "$MOD_LIVEPATCH" "transition" "-r--r--r--"
23check_sysfs_value  "$MOD_LIVEPATCH" "transition" "0"
24check_sysfs_rights "$MOD_LIVEPATCH" "vmlinux/patched" "-r--r--r--"
25check_sysfs_value  "$MOD_LIVEPATCH" "vmlinux/patched" "1"
26
27disable_lp $MOD_LIVEPATCH
28
29unload_lp $MOD_LIVEPATCH
30
31check_result "% insmod test_modules/$MOD_LIVEPATCH.ko
32livepatch: enabling patch '$MOD_LIVEPATCH'
33livepatch: '$MOD_LIVEPATCH': initializing patching transition
34livepatch: '$MOD_LIVEPATCH': starting patching transition
35livepatch: '$MOD_LIVEPATCH': completing patching transition
36livepatch: '$MOD_LIVEPATCH': patching complete
37% echo 0 > /sys/kernel/livepatch/$MOD_LIVEPATCH/enabled
38livepatch: '$MOD_LIVEPATCH': initializing unpatching transition
39livepatch: '$MOD_LIVEPATCH': starting unpatching transition
40livepatch: '$MOD_LIVEPATCH': completing unpatching transition
41livepatch: '$MOD_LIVEPATCH': unpatching complete
42% rmmod $MOD_LIVEPATCH"
43
44start_test "sysfs test object/patched"
45
46MOD_LIVEPATCH=test_klp_callbacks_demo
47MOD_TARGET=test_klp_callbacks_mod
48load_lp $MOD_LIVEPATCH
49
50# check the "patch" file changes as target module loads/unloads
51check_sysfs_value  "$MOD_LIVEPATCH" "$MOD_TARGET/patched" "0"
52load_mod $MOD_TARGET
53check_sysfs_value  "$MOD_LIVEPATCH" "$MOD_TARGET/patched" "1"
54unload_mod $MOD_TARGET
55check_sysfs_value  "$MOD_LIVEPATCH" "$MOD_TARGET/patched" "0"
56
57disable_lp $MOD_LIVEPATCH
58unload_lp $MOD_LIVEPATCH
59
60check_result "% insmod test_modules/test_klp_callbacks_demo.ko
61livepatch: enabling patch 'test_klp_callbacks_demo'
62livepatch: 'test_klp_callbacks_demo': initializing patching transition
63test_klp_callbacks_demo: pre_patch_callback: vmlinux
64livepatch: 'test_klp_callbacks_demo': starting patching transition
65livepatch: 'test_klp_callbacks_demo': completing patching transition
66test_klp_callbacks_demo: post_patch_callback: vmlinux
67livepatch: 'test_klp_callbacks_demo': patching complete
68% insmod test_modules/test_klp_callbacks_mod.ko
69livepatch: applying patch 'test_klp_callbacks_demo' to loading module 'test_klp_callbacks_mod'
70test_klp_callbacks_demo: pre_patch_callback: test_klp_callbacks_mod -> [MODULE_STATE_COMING] Full formed, running module_init
71test_klp_callbacks_demo: post_patch_callback: test_klp_callbacks_mod -> [MODULE_STATE_COMING] Full formed, running module_init
72test_klp_callbacks_mod: test_klp_callbacks_mod_init
73% rmmod test_klp_callbacks_mod
74test_klp_callbacks_mod: test_klp_callbacks_mod_exit
75test_klp_callbacks_demo: pre_unpatch_callback: test_klp_callbacks_mod -> [MODULE_STATE_GOING] Going away
76livepatch: reverting patch 'test_klp_callbacks_demo' on unloading module 'test_klp_callbacks_mod'
77test_klp_callbacks_demo: post_unpatch_callback: test_klp_callbacks_mod -> [MODULE_STATE_GOING] Going away
78% echo 0 > /sys/kernel/livepatch/test_klp_callbacks_demo/enabled
79livepatch: 'test_klp_callbacks_demo': initializing unpatching transition
80test_klp_callbacks_demo: pre_unpatch_callback: vmlinux
81livepatch: 'test_klp_callbacks_demo': starting unpatching transition
82livepatch: 'test_klp_callbacks_demo': completing unpatching transition
83test_klp_callbacks_demo: post_unpatch_callback: vmlinux
84livepatch: 'test_klp_callbacks_demo': unpatching complete
85% rmmod test_klp_callbacks_demo"
86
87start_test "sysfs test replace enabled"
88
89MOD_LIVEPATCH=test_klp_atomic_replace
90load_lp $MOD_LIVEPATCH replace=1
91
92check_sysfs_rights "$MOD_LIVEPATCH" "replace" "-r--r--r--"
93check_sysfs_value  "$MOD_LIVEPATCH" "replace" "1"
94
95disable_lp $MOD_LIVEPATCH
96unload_lp $MOD_LIVEPATCH
97
98check_result "% insmod test_modules/$MOD_LIVEPATCH.ko replace=1
99livepatch: enabling patch '$MOD_LIVEPATCH'
100livepatch: '$MOD_LIVEPATCH': initializing patching transition
101livepatch: '$MOD_LIVEPATCH': starting patching transition
102livepatch: '$MOD_LIVEPATCH': completing patching transition
103livepatch: '$MOD_LIVEPATCH': patching complete
104% echo 0 > /sys/kernel/livepatch/$MOD_LIVEPATCH/enabled
105livepatch: '$MOD_LIVEPATCH': initializing unpatching transition
106livepatch: '$MOD_LIVEPATCH': starting unpatching transition
107livepatch: '$MOD_LIVEPATCH': completing unpatching transition
108livepatch: '$MOD_LIVEPATCH': unpatching complete
109% rmmod $MOD_LIVEPATCH"
110
111start_test "sysfs test replace disabled"
112
113load_lp $MOD_LIVEPATCH replace=0
114
115check_sysfs_rights "$MOD_LIVEPATCH" "replace" "-r--r--r--"
116check_sysfs_value  "$MOD_LIVEPATCH" "replace" "0"
117
118disable_lp $MOD_LIVEPATCH
119unload_lp $MOD_LIVEPATCH
120
121check_result "% insmod test_modules/$MOD_LIVEPATCH.ko replace=0
122livepatch: enabling patch '$MOD_LIVEPATCH'
123livepatch: '$MOD_LIVEPATCH': initializing patching transition
124livepatch: '$MOD_LIVEPATCH': starting patching transition
125livepatch: '$MOD_LIVEPATCH': completing patching transition
126livepatch: '$MOD_LIVEPATCH': patching complete
127% echo 0 > /sys/kernel/livepatch/$MOD_LIVEPATCH/enabled
128livepatch: '$MOD_LIVEPATCH': initializing unpatching transition
129livepatch: '$MOD_LIVEPATCH': starting unpatching transition
130livepatch: '$MOD_LIVEPATCH': completing unpatching transition
131livepatch: '$MOD_LIVEPATCH': unpatching complete
132% rmmod $MOD_LIVEPATCH"
133
134exit 0
135